Merge pull request #10769 from ethereum/allocationCleanup

Cleanup allocation.
This commit is contained in:
chriseth authored and GitHub committed 2021-01-26 11:58:11 +01:00
commit 4697beeab2
21 files changed
+239 -164

No files matched your search

+38 -35
View File
@@ -183,15 +183,15 @@ string YulUtilFunctions::requireOrAssertFunction(bool _assert, Type const* _mess
return Whiskers(R"(
function <functionName>(condition <messageVars>) {
if iszero(condition) {
let fmp := mload(<freeMemPointer>)
mstore(fmp, <errorHash>)
let end := <abiEncodeFunc>(add(fmp, <hashHeaderSize>) <messageVars>)
revert(fmp, sub(end, fmp))
let memPtr := <allocateUnbounded>()
mstore(memPtr, <errorHash>)
let end := <abiEncodeFunc>(add(memPtr, <hashHeaderSize>) <messageVars>)
revert(memPtr, sub(end, memPtr))
}
}
)")
("functionName", functionName)
("freeMemPointer", to_string(CompilerUtils::freeMemoryPointer))
("allocateUnbounded", allocateUnboundedFunction())
("errorHash", formatNumber(errorHash))
("abiEncodeFunc", encodeFunc)
("hashHeaderSize", to_string(hashHeaderSize))
@@ -2301,19 +2301,19 @@ string YulUtilFunctions::copyArrayFromStorageToMemoryFunction(ArrayType const& _
solAssert(_from.baseType() == _to.baseType(), "");
ABIFunctions abi(m_evmVersion, m_revertStrings, m_functionCollector);
return Whiskers(R"(
function <functionName>(slot) -> memptr {
memptr := <allocateTemp>()
let end := <encode>(slot, memptr)
mstore(<freeMemoryPointer>, end)
function <functionName>(slot) -> memPtr {
memPtr := <allocateUnbounded>()
let end := <encode>(slot, memPtr)
<finalizeAllocation>(memPtr, sub(end, memPtr))
}
)")
("functionName", functionName)
("allocateTemp", allocationTemporaryMemoryFunction())
("allocateUnbounded", allocateUnboundedFunction())
(
"encode",
abi.abiEncodeAndReturnUpdatedPosFunction(_from, _to, ABIFunctions::EncodingOptions{})
)
("freeMemoryPointer", to_string(CompilerUtils::freeMemoryPointer))
("finalizeAllocation", finalizeAllocationFunction())
.render();
}
else
@@ -2324,10 +2324,10 @@ string YulUtilFunctions::copyArrayFromStorageToMemoryFunction(ArrayType const& _
solAssert(!_from.isByteArray(), "");
solAssert(*_to.withLocation(DataLocation::Storage, _from.isPointer()) == _from, "");
return Whiskers(R"(
function <functionName>(slot) -> memptr {
function <functionName>(slot) -> memPtr {
let length := <lengthFunction>(slot)
memptr := <allocateArray>(length)
let mpos := memptr
memPtr := <allocateArray>(length)
let mpos := memPtr
<?dynamic>mpos := add(mpos, 0x20)</dynamic>
let spos := <arrayDataArea>(slot)
for { let i := 0 } lt(i, length) { i := add(i, 1) } {
@@ -2793,28 +2793,24 @@ string YulUtilFunctions::prepareStoreFunction(Type const& _type)
string YulUtilFunctions::allocationFunction()
{
string functionName = "allocateMemory";
string functionName = "allocate_memory";
return m_functionCollector.createFunction(functionName, [&]() {
return Whiskers(R"(
function <functionName>(size) -> memPtr {
memPtr := mload(<freeMemoryPointer>)
let newFreePtr := add(memPtr, <roundUp>(size))
// protect against overflow
if or(gt(newFreePtr, 0xffffffffffffffff), lt(newFreePtr, memPtr)) { <panic>() }
mstore(<freeMemoryPointer>, newFreePtr)
memPtr := <allocateUnbounded>()
<finalizeAllocation>(memPtr, size)
}
)")
("functionName", functionName)
("freeMemoryPointer", to_string(CompilerUtils::freeMemoryPointer))
("roundUp", roundUpFunction())
("panic", panicFunction(PanicCode::ResourceError))
("allocateUnbounded", allocateUnboundedFunction())
("finalizeAllocation", finalizeAllocationFunction())
.render();
});
}
string YulUtilFunctions::allocationTemporaryMemoryFunction()
string YulUtilFunctions::allocateUnboundedFunction()
{
string functionName = "allocateTemporaryMemory";
string functionName = "allocate_unbounded";
return m_functionCollector.createFunction(functionName, [&]() {
return Whiskers(R"(
function <functionName>() -> memPtr {
@@ -2827,15 +2823,22 @@ string YulUtilFunctions::allocationTemporaryMemoryFunction()
});
}
string YulUtilFunctions::releaseTemporaryMemoryFunction()
string YulUtilFunctions::finalizeAllocationFunction()
{
string functionName = "releaseTemporaryMemory";
return m_functionCollector.createFunction(functionName, [&](){
string functionName = "finalize_allocation";
return m_functionCollector.createFunction(functionName, [&]() {
return Whiskers(R"(
function <functionName>() {
function <functionName>(memPtr, size) {
let newFreePtr := add(memPtr, <roundUp>(size))
// protect against overflow
if or(gt(newFreePtr, 0xffffffffffffffff), lt(newFreePtr, memPtr)) { <panic>() }
mstore(<freeMemoryPointer>, newFreePtr)
}
)")
("functionName", functionName)
("freeMemoryPointer", to_string(CompilerUtils::freeMemoryPointer))
("roundUp", roundUpFunction())
("panic", panicFunction(PanicCode::ResourceError))
.render();
});
}
@@ -3625,7 +3628,7 @@ string YulUtilFunctions::packedHashFunction(
return m_functionCollector.createFunction(functionName, [&]() {
Whiskers templ(R"(
function <functionName>(<variables>) -> hash {
let pos := mload(<freeMemoryPointer>)
let pos := <allocateUnbounded>()
let end := <packedEncode>(pos <comma> <variables>)
hash := keccak256(pos, sub(end, pos))
}
@@ -3633,7 +3636,7 @@ string YulUtilFunctions::packedHashFunction(
templ("functionName", functionName);
templ("variables", suffixedVariableNameList("var_", 1, 1 + sizeOnStack));
templ("comma", sizeOnStack > 0 ? "," : "");
templ("freeMemoryPointer", to_string(CompilerUtils::freeMemoryPointer));
templ("allocateUnbounded", allocateUnboundedFunction());
templ("packedEncode", ABIFunctions(m_evmVersion, m_revertStrings, m_functionCollector).tupleEncoderPacked(_givenTypes, _targetTypes));
return templ.render();
});
@@ -4149,7 +4152,7 @@ string YulUtilFunctions::tryDecodeErrorMessageFunction()
function <functionName>() -> ret {
if lt(returndatasize(), 0x44) { leave }
let data := mload(<freeMemoryPointer>)
let data := <allocateUnbounded>()
returndatacopy(data, 4, sub(returndatasize(), 4))
let offset := mload(data)
@@ -4167,13 +4170,13 @@ string YulUtilFunctions::tryDecodeErrorMessageFunction()
let end := add(add(msg, 0x20), length)
if gt(end, add(data, sub(returndatasize(), 4))) { leave }
mstore(<freeMemoryPointer>, add(add(msg, 0x20), <roundUp>(length)))
<finalizeAllocation>(data, add(offset, add(0x20, length)))
ret := msg
}
)")
("functionName", functionName)
("freeMemoryPointer", to_string(CompilerUtils::freeMemoryPointer))
("roundUp", roundUpFunction())
("allocateUnbounded", allocateUnboundedFunction())
("finalizeAllocation", finalizeAllocationFunction())
.render();
});
}