From 76f31e2c4e9cda441a0edcdb85cd2a564d8c8573 Mon Sep 17 00:00:00 2001 From: Mathias Baumann Date: Tue, 12 Oct 2021 15:24:55 +0200 Subject: [PATCH 1/2] Whiskers: Support conditional value parameters for lists --- libsolutil/Whiskers.cpp | 12 +++++++----- libsolutil/Whiskers.h | 7 ++++--- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/libsolutil/Whiskers.cpp b/libsolutil/Whiskers.cpp index e236104c7..340527bdd 100644 --- a/libsolutil/Whiskers.cpp +++ b/libsolutil/Whiskers.cpp @@ -189,11 +189,13 @@ string Whiskers::replace( if (conditionName[0] == '+') { string tag = conditionName.substr(1); - assertThrow( - _parameters.count(tag), - WhiskersError, "Tag " + tag + " used as condition but was not set." - ); - conditionValue = !_parameters.at(tag).empty(); + + if (_parameters.count(tag)) + conditionValue = !_parameters.at(tag).empty(); + else if (_listParameters.count(tag)) + conditionValue = !_listParameters.at(tag).empty(); + else + assertThrow(false, WhiskersError, "Tag " + tag + " used as condition but was not set."); } else { diff --git a/libsolutil/Whiskers.h b/libsolutil/Whiskers.h index 113fbe6ed..7bbf74f09 100644 --- a/libsolutil/Whiskers.h +++ b/libsolutil/Whiskers.h @@ -60,12 +60,13 @@ DEV_SIMPLE_EXCEPTION(WhiskersError); * - Condition parameter: ......, where "" is optional * replaced (and recursively expanded) by the first part if the condition is true * and by the second (or empty string if missing) if the condition is false - * - Conditional string parameter: ...... - * Works similar to a conditional parameter where the checked condition is - * that the regular (string) parameter called "name" is non-empty. * - List parameter: <#list>... * The part between the tags is repeated as often as values are provided * in the mapping. Each list element can have its own parameter -> value mapping. + * - Conditional value parameter: ...... + * Works similar to a conditional parameter where the checked condition is + * that the string or list parameter called "name" is non-empty or contains + * no elements respectively. */ class Whiskers { From 454860d1f51c8f9f28454006619b5fc036ded23e Mon Sep 17 00:00:00 2001 From: Mathias Baumann Date: Tue, 12 Oct 2021 15:25:28 +0200 Subject: [PATCH 2/2] CodeGen: Only output switch when it has cases --- Changelog.md | 1 + libsolidity/codegen/ir/IRGenerator.cpp | 6 ++--- .../debug_info_in_yul_snippet_escaping/output | 22 +++---------------- test/cmdlineTests/exp_base_literal/output | 2 +- test/cmdlineTests/name_simplifier/output | 4 ++-- test/cmdlineTests/revert_strings/output | 2 +- .../output.json | 20 ++++++++--------- .../output.json | 1 - .../standard_ir_requested/output.json | 2 +- .../standard_viair_requested/output.json | 18 +-------------- test/cmdlineTests/viair_abicoder_v1/output | 2 +- test/cmdlineTests/yul_optimizer_steps/output | 8 ------- .../output | 1 - .../yul_string_format_ascii/output.json | 2 +- .../output.json | 2 +- .../output.json | 2 +- .../yul_string_format_ascii_long/output.json | 2 +- .../yul_string_format_hex/output.json | 2 +- 18 files changed, 29 insertions(+), 70 deletions(-) diff --git a/Changelog.md b/Changelog.md index 9dd650599..9a22fed2a 100644 --- a/Changelog.md +++ b/Changelog.md @@ -15,6 +15,7 @@ Bugfixes: * Commandline Interface: Report output selection options unsupported by the selected input mode instead of ignoring them. * SMTChecker: Fix internal error in magic type access (``block``, ``msg``, ``tx``). * TypeChecker: Fix internal error when using user defined value types in public library functions. + * Yul IR Generator: Do not output empty switches/if-bodies for empty contracts. diff --git a/libsolidity/codegen/ir/IRGenerator.cpp b/libsolidity/codegen/ir/IRGenerator.cpp index c1bfce4e3..5a23dbea7 100644 --- a/libsolidity/codegen/ir/IRGenerator.cpp +++ b/libsolidity/codegen/ir/IRGenerator.cpp @@ -944,7 +944,7 @@ string IRGenerator::callValueCheck() string IRGenerator::dispatchRoutine(ContractDefinition const& _contract) { Whiskers t(R"X( - if iszero(lt(calldatasize(), 4)) + if iszero(lt(calldatasize(), 4)) { let selector := (calldataload(0)) switch selector @@ -962,8 +962,8 @@ string IRGenerator::dispatchRoutine(ContractDefinition const& _contract) } default {} - } - if iszero(calldatasize()) { } + } + if iszero(calldatasize()) { } )X"); t("shr224", m_utils.shiftRightFunction(224)); diff --git a/test/cmdlineTests/debug_info_in_yul_snippet_escaping/output b/test/cmdlineTests/debug_info_in_yul_snippet_escaping/output index a9dc1a290..b595a771e 100644 --- a/test/cmdlineTests/debug_info_in_yul_snippet_escaping/output +++ b/test/cmdlineTests/debug_info_in_yul_snippet_escaping/output @@ -44,14 +44,6 @@ object "C_2" { /// @src 0:265:278 "contract C {}" mstore(64, 128) - if iszero(lt(calldatasize(), 4)) - { - let selector := shift_right_224_unsigned(calldataload(0)) - switch selector - - default {} - } - if iszero(calldatasize()) { } revert_error_42b3090547df1d2001c96683413b8cf91c1b902ef5e3cb8d9f6f304cf7446f74() function shift_right_224_unsigned(value) -> newValue { @@ -175,7 +167,7 @@ object "D_27" { default {} } - if iszero(calldatasize()) { } + revert_error_42b3090547df1d2001c96683413b8cf91c1b902ef5e3cb8d9f6f304cf7446f74() function shift_right_224_unsigned(value) -> newValue { @@ -413,14 +405,6 @@ object "D_27" { /// @src 0:265:278 "contract C {}" mstore(64, 128) - if iszero(lt(calldatasize(), 4)) - { - let selector := shift_right_224_unsigned(calldataload(0)) - switch selector - - default {} - } - if iszero(calldatasize()) { } revert_error_42b3090547df1d2001c96683413b8cf91c1b902ef5e3cb8d9f6f304cf7446f74() function shift_right_224_unsigned(value) -> newValue { @@ -499,7 +483,7 @@ object "D_27" { revert(pos, returndatasize()) } mstore(add(allocate_memory_array_string(), 32), "/*") - let memPtr := allocate_memory_array_string_482() + let memPtr := allocate_memory_array_string_480() mstore(add(memPtr, 32), 0x2f2a2a204073726320303a39363a313635202022636f6e74726163742044207b) mstore(add(memPtr, 64), shl(200, 0x2e2e2e22202a2f)) let memPos := mload(64) @@ -540,7 +524,7 @@ object "D_27" { memPtr := memPtr_1 mstore(memPtr_1, 2) } - function allocate_memory_array_string_482() -> memPtr + function allocate_memory_array_string_480() -> memPtr { let memPtr_1 := mload(64) let newFreePtr := add(memPtr_1, 96) diff --git a/test/cmdlineTests/exp_base_literal/output b/test/cmdlineTests/exp_base_literal/output index c4b71dcac..9c3efdc1f 100644 --- a/test/cmdlineTests/exp_base_literal/output +++ b/test/cmdlineTests/exp_base_literal/output @@ -63,7 +63,7 @@ object "C_81" { default {} } - if iszero(calldatasize()) { } + revert_error_42b3090547df1d2001c96683413b8cf91c1b902ef5e3cb8d9f6f304cf7446f74() function shift_right_224_unsigned(value) -> newValue { diff --git a/test/cmdlineTests/name_simplifier/output b/test/cmdlineTests/name_simplifier/output index 73c9cc1aa..44852d1a8 100644 --- a/test/cmdlineTests/name_simplifier/output +++ b/test/cmdlineTests/name_simplifier/output @@ -49,7 +49,7 @@ object "C_59" { for { } lt(src, srcEnd) { src := add(src, _2) } { if slt(sub(calldatasize(), src), _2) { revert(_1, _1) } - let value := allocate_memory_1174() + let value := allocate_memory_1172() mstore(value, calldataload(src)) mstore(dst, value) dst := add(dst, _2) @@ -67,7 +67,7 @@ object "C_59" { mstore(4, 0x41) revert(0, 0x24) } - function allocate_memory_1174() -> memPtr + function allocate_memory_1172() -> memPtr { memPtr := mload(64) let newFreePtr := add(memPtr, 32) diff --git a/test/cmdlineTests/revert_strings/output b/test/cmdlineTests/revert_strings/output index 718cf0ba9..9ca27eac4 100644 --- a/test/cmdlineTests/revert_strings/output +++ b/test/cmdlineTests/revert_strings/output @@ -78,7 +78,7 @@ object "C_15" { default {} } - if iszero(calldatasize()) { } + revert_error_42b3090547df1d2001c96683413b8cf91c1b902ef5e3cb8d9f6f304cf7446f74() function shift_right_224_unsigned(value) -> newValue { diff --git a/test/cmdlineTests/standard_debug_info_in_yul_location/output.json b/test/cmdlineTests/standard_debug_info_in_yul_location/output.json index 0ba3b09a3..7a77d60f4 100644 --- a/test/cmdlineTests/standard_debug_info_in_yul_location/output.json +++ b/test/cmdlineTests/standard_debug_info_in_yul_location/output.json @@ -218,7 +218,7 @@ object \"C_54\" { default {} } - if iszero(calldatasize()) { } + revert_error_42b3090547df1d2001c96683413b8cf91c1b902ef5e3cb8d9f6f304cf7446f74() function shift_right_224_unsigned(value) -> newValue { @@ -639,7 +639,7 @@ object \"C_54\" { case 0x26121ff0 { if callvalue() { revert(_1, _1) } abi_decode(calldatasize()) - let ret := /** @src 0:286:305 \"constVar + immutVar\" */ checked_add_int256_568(/** @src 0:297:305 \"immutVar\" */ loadimmutable(\"8\")) + let ret := /** @src 0:286:305 \"constVar + immutVar\" */ checked_add_int256_566(/** @src 0:297:305 \"immutVar\" */ loadimmutable(\"8\")) /// @src 0:79:435 \"contract C...\" let memPos := mload(64) return(memPos, sub(abi_encode_int256(memPos, ret), memPos)) @@ -663,7 +663,7 @@ object \"C_54\" { if callvalue() { revert(_1, _1) } abi_decode(calldatasize()) let memPos_3 := mload(64) - return(memPos_3, sub(abi_encode_int256_567(memPos_3), memPos_3)) + return(memPos_3, sub(abi_encode_int256_565(memPos_3), memPos_3)) } } revert(0, 0) @@ -672,7 +672,7 @@ object \"C_54\" { { if slt(add(dataEnd, not(3)), 0) { revert(0, 0) } } - function abi_encode_int256_567(headStart) -> tail + function abi_encode_int256_565(headStart) -> tail { tail := add(headStart, 32) mstore(headStart, /** @src 0:124:126 \"41\" */ 0x29) @@ -689,7 +689,7 @@ object \"C_54\" { mstore(4, 0x11) revert(0, 0x24) } - function checked_add_int256_568(y) -> sum + function checked_add_int256_566(y) -> sum { if and(1, sgt(y, sub(shl(255, 1), 42))) { panic_error_0x11() } sum := add(/** @src 0:124:126 \"41\" */ 0x29, /** @src 0:79:435 \"contract C...\" */ y) @@ -1050,7 +1050,7 @@ object \"D_72\" { default {} } - if iszero(calldatasize()) { } + revert_error_42b3090547df1d2001c96683413b8cf91c1b902ef5e3cb8d9f6f304cf7446f74() function shift_right_224_unsigned(value) -> newValue { @@ -1479,7 +1479,7 @@ object \"D_72\" { case 0x26121ff0 { if callvalue() { revert(_1, _1) } abi_decode(calldatasize()) - let ret := /** @src 0:286:305 \"constVar + immutVar\" */ checked_add_int256_568(/** @src 0:297:305 \"immutVar\" */ loadimmutable(\"8\")) + let ret := /** @src 0:286:305 \"constVar + immutVar\" */ checked_add_int256_566(/** @src 0:297:305 \"immutVar\" */ loadimmutable(\"8\")) /// @src 1:91:166 \"contract D is C(3)...\" let memPos := mload(64) return(memPos, sub(abi_encode_int256(memPos, ret), memPos)) @@ -1503,7 +1503,7 @@ object \"D_72\" { if callvalue() { revert(_1, _1) } abi_decode(calldatasize()) let memPos_3 := mload(64) - return(memPos_3, sub(abi_encode_int256_567(memPos_3), memPos_3)) + return(memPos_3, sub(abi_encode_int256_565(memPos_3), memPos_3)) } } revert(0, 0) @@ -1512,7 +1512,7 @@ object \"D_72\" { { if slt(add(dataEnd, not(3)), 0) { revert(0, 0) } } - function abi_encode_int256_567(headStart) -> tail + function abi_encode_int256_565(headStart) -> tail { tail := add(headStart, 32) mstore(headStart, /** @src 0:124:126 \"41\" */ 0x29) @@ -1529,7 +1529,7 @@ object \"D_72\" { mstore(4, 0x11) revert(0, 0x24) } - function checked_add_int256_568(y) -> sum + function checked_add_int256_566(y) -> sum { if and(1, sgt(y, sub(shl(255, 1), 42))) { panic_error_0x11() } sum := add(/** @src 0:124:126 \"41\" */ 0x29, /** @src 1:91:166 \"contract D is C(3)...\" */ y) diff --git a/test/cmdlineTests/standard_irOptimized_requested/output.json b/test/cmdlineTests/standard_irOptimized_requested/output.json index c636adcc4..78499140c 100644 --- a/test/cmdlineTests/standard_irOptimized_requested/output.json +++ b/test/cmdlineTests/standard_irOptimized_requested/output.json @@ -47,7 +47,6 @@ object \"C_7\" { } default { } } - if iszero(calldatasize()) { } revert_error_42b3090547df1d2001c96683413b8cf91c1b902ef5e3cb8d9f6f304cf7446f74() function shift_right_224_unsigned(value) -> newValue { newValue := shr(224, value) } diff --git a/test/cmdlineTests/standard_ir_requested/output.json b/test/cmdlineTests/standard_ir_requested/output.json index 2c01878f3..2da612abc 100644 --- a/test/cmdlineTests/standard_ir_requested/output.json +++ b/test/cmdlineTests/standard_ir_requested/output.json @@ -62,7 +62,7 @@ object \"C_7\" { default {} } - if iszero(calldatasize()) { } + revert_error_42b3090547df1d2001c96683413b8cf91c1b902ef5e3cb8d9f6f304cf7446f74() function shift_right_224_unsigned(value) -> newValue { diff --git a/test/cmdlineTests/standard_viair_requested/output.json b/test/cmdlineTests/standard_viair_requested/output.json index 9ae88b90c..4bf34fd7a 100644 --- a/test/cmdlineTests/standard_viair_requested/output.json +++ b/test/cmdlineTests/standard_viair_requested/output.json @@ -43,14 +43,6 @@ object \"C_3\" { /// @src 0:79:92 \"contract C {}\" mstore(64, 128) - if iszero(lt(calldatasize(), 4)) - { - let selector := shift_right_224_unsigned(calldataload(0)) - switch selector - - default {} - } - if iszero(calldatasize()) { } revert_error_42b3090547df1d2001c96683413b8cf91c1b902ef5e3cb8d9f6f304cf7446f74() function shift_right_224_unsigned(value) -> newValue { @@ -139,7 +131,7 @@ object \"D_16\" { default {} } - if iszero(calldatasize()) { } + revert_error_42b3090547df1d2001c96683413b8cf91c1b902ef5e3cb8d9f6f304cf7446f74() function shift_right_224_unsigned(value) -> newValue { @@ -253,14 +245,6 @@ object \"D_16\" { /// @src 0:79:92 \"contract C {}\" mstore(64, 128) - if iszero(lt(calldatasize(), 4)) - { - let selector := shift_right_224_unsigned(calldataload(0)) - switch selector - - default {} - } - if iszero(calldatasize()) { } revert_error_42b3090547df1d2001c96683413b8cf91c1b902ef5e3cb8d9f6f304cf7446f74() function shift_right_224_unsigned(value) -> newValue { diff --git a/test/cmdlineTests/viair_abicoder_v1/output b/test/cmdlineTests/viair_abicoder_v1/output index 18d02a0e9..a23730702 100644 --- a/test/cmdlineTests/viair_abicoder_v1/output +++ b/test/cmdlineTests/viair_abicoder_v1/output @@ -63,7 +63,7 @@ object "test_11" { default {} } - if iszero(calldatasize()) { } + revert_error_42b3090547df1d2001c96683413b8cf91c1b902ef5e3cb8d9f6f304cf7446f74() function shift_right_224_unsigned(value) -> newValue { diff --git a/test/cmdlineTests/yul_optimizer_steps/output b/test/cmdlineTests/yul_optimizer_steps/output index 478c844db..8e4cc7e88 100644 --- a/test/cmdlineTests/yul_optimizer_steps/output +++ b/test/cmdlineTests/yul_optimizer_steps/output @@ -31,16 +31,8 @@ object "C_7" { { /// @src 0:80:112 "contract C..." mstore(64, 128) - if iszero(lt(calldatasize(), 4)) - { - let selector := shift_right_unsigned(calldataload(0)) - pop(selector) - } - pop(iszero(calldatasize())) revert_error_42b3090547df1d2001c96683413b8cf91c1b902ef5e3cb8d9f6f304cf7446f74() } - function shift_right_unsigned(value) -> newValue - { newValue := shr(224, value) } function revert_error_42b3090547df1d2001c96683413b8cf91c1b902ef5e3cb8d9f6f304cf7446f74() { revert(0, 0) } } diff --git a/test/cmdlineTests/yul_optimizer_steps_nested_brackets/output b/test/cmdlineTests/yul_optimizer_steps_nested_brackets/output index d4c026105..63f08a35d 100644 --- a/test/cmdlineTests/yul_optimizer_steps_nested_brackets/output +++ b/test/cmdlineTests/yul_optimizer_steps_nested_brackets/output @@ -138,7 +138,6 @@ object "C_6" { return(memPos, sub(memEnd, memPos)) } } - pop(iszero(calldatasize())) revert_error_42b3090547df1d2001c96683413b8cf91c1b902ef5e3cb8d9f6f304cf7446f74() } function shift_right_unsigned(value) -> newValue diff --git a/test/cmdlineTests/yul_string_format_ascii/output.json b/test/cmdlineTests/yul_string_format_ascii/output.json index f68f984c7..ae7a926fe 100644 --- a/test/cmdlineTests/yul_string_format_ascii/output.json +++ b/test/cmdlineTests/yul_string_format_ascii/output.json @@ -62,7 +62,7 @@ object \"C_11\" { default {} } - if iszero(calldatasize()) { } + revert_error_42b3090547df1d2001c96683413b8cf91c1b902ef5e3cb8d9f6f304cf7446f74() function shift_right_224_unsigned(value) -> newValue { diff --git a/test/cmdlineTests/yul_string_format_ascii_bytes32/output.json b/test/cmdlineTests/yul_string_format_ascii_bytes32/output.json index e2757135e..59a93a7bf 100644 --- a/test/cmdlineTests/yul_string_format_ascii_bytes32/output.json +++ b/test/cmdlineTests/yul_string_format_ascii_bytes32/output.json @@ -62,7 +62,7 @@ object \"C_11\" { default {} } - if iszero(calldatasize()) { } + revert_error_42b3090547df1d2001c96683413b8cf91c1b902ef5e3cb8d9f6f304cf7446f74() function shift_right_224_unsigned(value) -> newValue { diff --git a/test/cmdlineTests/yul_string_format_ascii_bytes32_from_number/output.json b/test/cmdlineTests/yul_string_format_ascii_bytes32_from_number/output.json index c977d791d..79d35a338 100644 --- a/test/cmdlineTests/yul_string_format_ascii_bytes32_from_number/output.json +++ b/test/cmdlineTests/yul_string_format_ascii_bytes32_from_number/output.json @@ -62,7 +62,7 @@ object \"C_11\" { default {} } - if iszero(calldatasize()) { } + revert_error_42b3090547df1d2001c96683413b8cf91c1b902ef5e3cb8d9f6f304cf7446f74() function shift_right_224_unsigned(value) -> newValue { diff --git a/test/cmdlineTests/yul_string_format_ascii_long/output.json b/test/cmdlineTests/yul_string_format_ascii_long/output.json index 19428f29d..932a5c28c 100644 --- a/test/cmdlineTests/yul_string_format_ascii_long/output.json +++ b/test/cmdlineTests/yul_string_format_ascii_long/output.json @@ -62,7 +62,7 @@ object \"C_11\" { default {} } - if iszero(calldatasize()) { } + revert_error_42b3090547df1d2001c96683413b8cf91c1b902ef5e3cb8d9f6f304cf7446f74() function shift_right_224_unsigned(value) -> newValue { diff --git a/test/cmdlineTests/yul_string_format_hex/output.json b/test/cmdlineTests/yul_string_format_hex/output.json index b17922a55..da8f873a3 100644 --- a/test/cmdlineTests/yul_string_format_hex/output.json +++ b/test/cmdlineTests/yul_string_format_hex/output.json @@ -62,7 +62,7 @@ object \"C_11\" { default {} } - if iszero(calldatasize()) { } + revert_error_42b3090547df1d2001c96683413b8cf91c1b902ef5e3cb8d9f6f304cf7446f74() function shift_right_224_unsigned(value) -> newValue {