Skip to content

Commit

Permalink
fixup! Use MCOPY when copying byte arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
nikola-matic committed Feb 7, 2024
1 parent 46621e5 commit c67dfe2
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 25 deletions.
37 changes: 12 additions & 25 deletions libsolidity/codegen/YulUtilFunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,47 +88,34 @@ std::string YulUtilFunctions::copyToMemoryFunction(bool _fromCalldata, bool _cle
"_to_memory"s +
(_cleanup ? "_with_cleanup"s : ""s);

return m_functionCollector.createFunction(functionName, [&]() {
// calldata -> memory
return m_functionCollector.createFunction(functionName, [&](std::vector<std::string>& _args, std::vector<std::string>&) {
_args = {"src", "dst", "length"};

if (_fromCalldata)
{
return Whiskers(R"(
function <functionName>(src, dst, length) {
calldatacopy(dst, src, length)
<?cleanup>mstore(add(dst, length), 0)</cleanup>
}
calldatacopy(dst, src, length)
<?cleanup>mstore(add(dst, length), 0)</cleanup>
)")
("functionName", functionName)
("cleanup", _cleanup)
.render();
}
// memory -> memory
else
{
// use MCOPY (cancun+)
if (m_evmVersion.hasMcopy())
return Whiskers(R"(
function <functionName>(src, dst, length) {
mcopy(dst, src, length)
<?cleanup>mstore(add(dst, length), 0)</cleanup>
}
mcopy(dst, src, length)
<?cleanup>mstore(add(dst, length), 0)</cleanup>
)")
("functionName", functionName)
("cleanup", _cleanup)
.render();
// mstore mload loop
else
return Whiskers(R"(
function <functionName>(src, dst, length) {
let i := 0
for { } lt(i, length) { i := add(i, 32) }
{
mstore(add(dst, i), mload(add(src, i)))
}
<?cleanup>mstore(add(dst, length), 0)</cleanup>
let i := 0
for { } lt(i, length) { i := add(i, 32) }
{
mstore(add(dst, i), mload(add(src, i)))
}
<?cleanup>mstore(add(dst, length), 0)</cleanup>
)")
("functionName", functionName)
("cleanup", _cleanup)
.render();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,10 @@ object "C_14" {
}

function copy_memory_to_memory_with_cleanup(src, dst, length) {

mcopy(dst, src, length)
mstore(add(dst, length), 0)

}

function round_up_to_mul_of_32(value) -> result {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,10 @@ object "C_10" {
}

function copy_memory_to_memory_with_cleanup(src, dst, length) {

mcopy(dst, src, length)
mstore(add(dst, length), 0)

}

function round_up_to_mul_of_32(value) -> result {
Expand Down

0 comments on commit c67dfe2

Please sign in to comment.