From e9180481e9c192f762ff321ae3205e2139f19e84 Mon Sep 17 00:00:00 2001 From: Daniel Kirchner Date: Mon, 15 Mar 2021 17:57:42 +0100 Subject: [PATCH] Allocate memory for deployed code. --- libsolidity/codegen/ir/IRGenerator.cpp | 41 +++++++------------ ...2_in_function_inherited_in_v1_contract.sol | 2 +- .../array/fixed_arrays_as_return_type.sol | 2 +- .../array/function_array_cross_calls.sol | 2 +- .../semanticTests/array/reusing_memory.sol | 2 +- .../constructor/arrays_in_constructors.sol | 2 +- .../bytes_in_constructors_packer.sol | 2 +- .../constructor/no_callvalue_check.sol | 2 +- .../functionTypes/store_function.sol | 2 +- .../immutable/multi_creation.sol | 2 +- .../address_overload_resolution.sol | 4 +- ...d_function_calldata_calldata_interface.sol | 2 +- ...ted_function_calldata_memory_interface.sol | 2 +- .../interface_inheritance_conversions.sol | 6 +-- .../salted_create/salted_create.sol | 2 +- .../salted_create_with_value.sol | 2 +- .../various/staticcall_for_view_and_pure.sol | 4 +- 17 files changed, 35 insertions(+), 46 deletions(-) diff --git a/libsolidity/codegen/ir/IRGenerator.cpp b/libsolidity/codegen/ir/IRGenerator.cpp index 51d59c3f8..b1064b43d 100644 --- a/libsolidity/codegen/ir/IRGenerator.cpp +++ b/libsolidity/codegen/ir/IRGenerator.cpp @@ -804,31 +804,27 @@ void IRGenerator::generateImplicitConstructors(ContractDefinition const& _contra string IRGenerator::deployCode(ContractDefinition const& _contract) { Whiskers t(R"X( - <#loadImmutables> - let := mload() - + let codeOffset := (datasize("")) + codecopy(codeOffset, dataoffset(""), datasize("")) - codecopy(0, dataoffset(""), datasize("")) + <#immutables> + setimmutable(codeOffset, "", ) + - <#storeImmutables> - setimmutable(0, "", ) - - - return(0, datasize("")) + return(codeOffset, datasize("")) )X"); t("object", IRNames::deployedObject(_contract)); + t("allocate", m_utils.allocationFunction()); - vector> loadImmutables; - vector> storeImmutables; + vector> immutables; if (_contract.isLibrary()) { solAssert(ContractType(_contract).immutableVariables().empty(), ""); - storeImmutables.emplace_back(map{ - {"var"s, "address()"}, - {"immutableName"s, IRNames::libraryAddressImmutable()} + immutables.emplace_back(map{ + {"name"s, IRNames::libraryAddressImmutable()}, + {"value"s, "address()"} }); - } else for (VariableDeclaration const* immutable: ContractType(_contract).immutableVariables()) @@ -836,19 +832,12 @@ string IRGenerator::deployCode(ContractDefinition const& _contract) solUnimplementedAssert(immutable->type()->isValueType(), ""); solUnimplementedAssert(immutable->type()->sizeOnStack() == 1, ""); string yulVar = m_context.newYulVariable(); - loadImmutables.emplace_back(map{ - {"var"s, yulVar}, - {"memoryOffset"s, to_string(m_context.immutableMemoryOffset(*immutable))} - }); - storeImmutables.emplace_back(map{ - {"var"s, yulVar}, - {"immutableName"s, to_string(immutable->id())} + immutables.emplace_back(map{ + {"name"s, to_string(immutable->id())}, + {"value"s, "mload("s + to_string(m_context.immutableMemoryOffset(*immutable)) + ")"s} }); } - t("loadImmutables", std::move(loadImmutables)); - // reverse order to ease stack strain - reverse(storeImmutables.begin(), storeImmutables.end()); - t("storeImmutables", std::move(storeImmutables)); + t("immutables", std::move(immutables)); return t.render(); } diff --git a/test/libsolidity/semanticTests/abiEncoderV2/abi_encode_v2_in_function_inherited_in_v1_contract.sol b/test/libsolidity/semanticTests/abiEncoderV2/abi_encode_v2_in_function_inherited_in_v1_contract.sol index e94601c57..9870024e3 100644 --- a/test/libsolidity/semanticTests/abiEncoderV2/abi_encode_v2_in_function_inherited_in_v1_contract.sol +++ b/test/libsolidity/semanticTests/abiEncoderV2/abi_encode_v2_in_function_inherited_in_v1_contract.sol @@ -30,6 +30,6 @@ contract C is B { // compileViaYul: also // ---- // test() -> 77 -// gas irOptimized: 139834 +// gas irOptimized: 139931 // gas legacy: 156573 // gas legacyOptimized: 112983 diff --git a/test/libsolidity/semanticTests/array/fixed_arrays_as_return_type.sol b/test/libsolidity/semanticTests/array/fixed_arrays_as_return_type.sol index 738260026..22a96b99d 100644 --- a/test/libsolidity/semanticTests/array/fixed_arrays_as_return_type.sol +++ b/test/libsolidity/semanticTests/array/fixed_arrays_as_return_type.sol @@ -21,6 +21,6 @@ contract B { // compileViaYul: also // ---- // f() -> 2, 3, 4, 5, 6, 1000, 1001, 1002, 1003, 1004 -// gas irOptimized: 179491 +// gas irOptimized: 179582 // gas legacy: 264410 // gas legacyOptimized: 134899 diff --git a/test/libsolidity/semanticTests/array/function_array_cross_calls.sol b/test/libsolidity/semanticTests/array/function_array_cross_calls.sol index ba045d5cc..fe40045f5 100644 --- a/test/libsolidity/semanticTests/array/function_array_cross_calls.sol +++ b/test/libsolidity/semanticTests/array/function_array_cross_calls.sol @@ -45,6 +45,6 @@ contract C { // compileViaYul: also // ---- // test() -> 5, 6, 7 -// gas irOptimized: 360048 +// gas irOptimized: 360145 // gas legacy: 500424 // gas legacyOptimized: 307615 diff --git a/test/libsolidity/semanticTests/array/reusing_memory.sol b/test/libsolidity/semanticTests/array/reusing_memory.sol index 3436065d3..95e1641b3 100644 --- a/test/libsolidity/semanticTests/array/reusing_memory.sol +++ b/test/libsolidity/semanticTests/array/reusing_memory.sol @@ -26,6 +26,6 @@ contract Main { // compileViaYul: also // ---- // f(uint256): 0x34 -> 0x46bddb1178e94d7f2892ff5f366840eb658911794f2c3a44c450aa2c505186c1 -// gas irOptimized: 117287 +// gas irOptimized: 117459 // gas legacy: 127152 // gas legacyOptimized: 113679 diff --git a/test/libsolidity/semanticTests/constructor/arrays_in_constructors.sol b/test/libsolidity/semanticTests/constructor/arrays_in_constructors.sol index 33e372e01..485e466a5 100644 --- a/test/libsolidity/semanticTests/constructor/arrays_in_constructors.sol +++ b/test/libsolidity/semanticTests/constructor/arrays_in_constructors.sol @@ -26,6 +26,6 @@ contract Creator { // compileViaYul: also // ---- // f(uint256,address[]): 7, 0x40, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 -> 7, 8 -// gas irOptimized: 472538 +// gas irOptimized: 472752 // gas legacy: 570900 // gas legacyOptimized: 436164 diff --git a/test/libsolidity/semanticTests/constructor/bytes_in_constructors_packer.sol b/test/libsolidity/semanticTests/constructor/bytes_in_constructors_packer.sol index 2cfee3ab0..29e3a182a 100644 --- a/test/libsolidity/semanticTests/constructor/bytes_in_constructors_packer.sol +++ b/test/libsolidity/semanticTests/constructor/bytes_in_constructors_packer.sol @@ -26,6 +26,6 @@ contract Creator { // compileViaYul: also // ---- // f(uint256,bytes): 7, 0x40, 78, "abcdefghijklmnopqrstuvwxyzabcdef", "ghijklmnopqrstuvwxyzabcdefghijkl", "mnopqrstuvwxyz" -> 7, "h" -// gas irOptimized: 335246 +// gas irOptimized: 335413 // gas legacy: 414850 // gas legacyOptimized: 290276 diff --git a/test/libsolidity/semanticTests/constructor/no_callvalue_check.sol b/test/libsolidity/semanticTests/constructor/no_callvalue_check.sol index 4efebba23..11082d161 100644 --- a/test/libsolidity/semanticTests/constructor/no_callvalue_check.sol +++ b/test/libsolidity/semanticTests/constructor/no_callvalue_check.sol @@ -19,6 +19,6 @@ contract C { // compileViaYul: also // ---- // f(), 2000 ether -> true -// gas irOptimized: 123853 +// gas irOptimized: 124114 // gas legacy: 123226 // gas legacyOptimized: 123092 diff --git a/test/libsolidity/semanticTests/functionTypes/store_function.sol b/test/libsolidity/semanticTests/functionTypes/store_function.sol index 60ec452c4..d4be523f4 100644 --- a/test/libsolidity/semanticTests/functionTypes/store_function.sol +++ b/test/libsolidity/semanticTests/functionTypes/store_function.sol @@ -28,6 +28,6 @@ contract C { // compileViaYul: also // ---- // t() -> 9 -// gas irOptimized: 124896 +// gas irOptimized: 124993 // gas legacy: 161097 // gas legacyOptimized: 111516 diff --git a/test/libsolidity/semanticTests/immutable/multi_creation.sol b/test/libsolidity/semanticTests/immutable/multi_creation.sol index 2186dedbe..993b89330 100644 --- a/test/libsolidity/semanticTests/immutable/multi_creation.sol +++ b/test/libsolidity/semanticTests/immutable/multi_creation.sol @@ -29,7 +29,7 @@ contract C { // compileViaYul: also // ---- // f() -> 3, 7, 5 -// gas irOptimized: 133517 +// gas irOptimized: 133717 // gas legacy: 153990 // gas legacyOptimized: 127822 // x() -> 7 diff --git a/test/libsolidity/semanticTests/inheritance/address_overload_resolution.sol b/test/libsolidity/semanticTests/inheritance/address_overload_resolution.sol index 23e4b624d..4217e009f 100644 --- a/test/libsolidity/semanticTests/inheritance/address_overload_resolution.sol +++ b/test/libsolidity/semanticTests/inheritance/address_overload_resolution.sol @@ -23,8 +23,8 @@ contract D { // compileViaYul: also // ---- // f() -> 1 -// gas irOptimized: 111246 +// gas irOptimized: 111343 // gas legacy: 114412 // g() -> 5 -// gas irOptimized: 111379 +// gas irOptimized: 111476 // gas legacy: 114872 diff --git a/test/libsolidity/semanticTests/inheritance/inherited_function_calldata_calldata_interface.sol b/test/libsolidity/semanticTests/inheritance/inherited_function_calldata_calldata_interface.sol index 40a8b5f15..f1d9eb738 100644 --- a/test/libsolidity/semanticTests/inheritance/inherited_function_calldata_calldata_interface.sol +++ b/test/libsolidity/semanticTests/inheritance/inherited_function_calldata_calldata_interface.sol @@ -25,5 +25,5 @@ contract B { // compileViaYul: also // ---- // g() -> 42 -// gas irOptimized: 107179 +// gas irOptimized: 107276 // gas legacy: 117797 diff --git a/test/libsolidity/semanticTests/inheritance/inherited_function_calldata_memory_interface.sol b/test/libsolidity/semanticTests/inheritance/inherited_function_calldata_memory_interface.sol index d121f1c20..d5a2383fc 100644 --- a/test/libsolidity/semanticTests/inheritance/inherited_function_calldata_memory_interface.sol +++ b/test/libsolidity/semanticTests/inheritance/inherited_function_calldata_memory_interface.sol @@ -25,6 +25,6 @@ contract B { // compileViaYul: also // ---- // g() -> 42 -// gas irOptimized: 127021 +// gas irOptimized: 127118 // gas legacy: 180597 // gas legacyOptimized: 116153 diff --git a/test/libsolidity/semanticTests/interface_inheritance_conversions.sol b/test/libsolidity/semanticTests/interface_inheritance_conversions.sol index 78aff8ede..1e67ff7f5 100644 --- a/test/libsolidity/semanticTests/interface_inheritance_conversions.sol +++ b/test/libsolidity/semanticTests/interface_inheritance_conversions.sol @@ -37,10 +37,10 @@ contract C { // compileViaYul: also // ---- // convertParent() -> 1 -// gas irOptimized: 122356 +// gas irOptimized: 122453 // convertSubA() -> 1, 2 -// gas irOptimized: 124555 +// gas irOptimized: 124652 // gas legacy: 101703 // convertSubB() -> 1, 3 -// gas irOptimized: 124489 +// gas irOptimized: 124586 // gas legacy: 101637 diff --git a/test/libsolidity/semanticTests/salted_create/salted_create.sol b/test/libsolidity/semanticTests/salted_create/salted_create.sol index 8d26c4120..832872cdd 100644 --- a/test/libsolidity/semanticTests/salted_create/salted_create.sol +++ b/test/libsolidity/semanticTests/salted_create/salted_create.sol @@ -22,6 +22,6 @@ contract A { // ---- // different_salt() -> true // same_salt() -> true -// gas irOptimized: 98439083 +// gas irOptimized: 98439085 // gas legacy: 98439116 // gas legacyOptimized: 98438982 diff --git a/test/libsolidity/semanticTests/salted_create/salted_create_with_value.sol b/test/libsolidity/semanticTests/salted_create/salted_create_with_value.sol index 6f9991857..0d413e68c 100644 --- a/test/libsolidity/semanticTests/salted_create/salted_create_with_value.sol +++ b/test/libsolidity/semanticTests/salted_create/salted_create_with_value.sol @@ -22,6 +22,6 @@ contract A { // compileViaYul: also // ---- // f(), 10 ether -> 3007, 3008, 3009 -// gas irOptimized: 338630 +// gas irOptimized: 339266 // gas legacy: 422027 // gas legacyOptimized: 287256 diff --git a/test/libsolidity/semanticTests/various/staticcall_for_view_and_pure.sol b/test/libsolidity/semanticTests/various/staticcall_for_view_and_pure.sol index d2277df7e..2a7105d51 100644 --- a/test/libsolidity/semanticTests/various/staticcall_for_view_and_pure.sol +++ b/test/libsolidity/semanticTests/various/staticcall_for_view_and_pure.sol @@ -38,10 +38,10 @@ contract D { // f() -> 0x1 # This should work, next should throw # // gas legacy: 102944 // fview() -> FAILURE -// gas irOptimized: 98438674 +// gas irOptimized: 98438675 // gas legacy: 98438822 // gas legacyOptimized: 98438615 // fpure() -> FAILURE -// gas irOptimized: 98438674 +// gas irOptimized: 98438675 // gas legacy: 98438822 // gas legacyOptimized: 98438616