mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Legacy codegeneration for immutable state variables.
This commit is contained in:
committed by
chriseth
parent
83cbfbb7bf
commit
04d8ad2ae1
@@ -112,6 +112,44 @@ namespace
|
||||
|
||||
BOOST_AUTO_TEST_SUITE(Optimiser)
|
||||
|
||||
BOOST_AUTO_TEST_CASE(cse_push_immutable_same)
|
||||
{
|
||||
AssemblyItem pushImmutable{PushImmutable, 0x1234};
|
||||
checkCSE({pushImmutable, pushImmutable}, {pushImmutable, Instruction::DUP1});
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(cse_push_immutable_different)
|
||||
{
|
||||
AssemblyItems input{{PushImmutable, 0x1234},{PushImmutable, 0xABCD}};
|
||||
checkCSE(input, input);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(cse_assign_immutable)
|
||||
{
|
||||
{
|
||||
AssemblyItems input{u256(0x42), {AssignImmutable, 0x1234}};
|
||||
checkCSE(input, input);
|
||||
}
|
||||
{
|
||||
AssemblyItems input{{AssignImmutable, 0x1234}};
|
||||
checkCSE(input, input);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
BOOST_AUTO_TEST_CASE(cse_assign_immutable_breaks)
|
||||
{
|
||||
AssemblyItems input = addDummyLocations(AssemblyItems{
|
||||
u256(0x42),
|
||||
{AssignImmutable, 0x1234},
|
||||
Instruction::ORIGIN
|
||||
});
|
||||
|
||||
evmasm::CommonSubexpressionEliminator cse{evmasm::KnownState()};
|
||||
// Make sure CSE breaks after AssignImmutable.
|
||||
BOOST_REQUIRE(cse.feedItems(input.begin(), input.end(), false) == input.begin() + 2);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(cse_intermediate_swap)
|
||||
{
|
||||
evmasm::KnownState state;
|
||||
@@ -798,6 +836,68 @@ BOOST_AUTO_TEST_CASE(block_deduplicator)
|
||||
BOOST_CHECK_EQUAL(pushTags.size(), 2);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(block_deduplicator_assign_immutable_same)
|
||||
{
|
||||
AssemblyItems blocks{
|
||||
AssemblyItem(Tag, 1),
|
||||
u256(42),
|
||||
AssemblyItem{AssignImmutable, 0x1234},
|
||||
Instruction::JUMP,
|
||||
AssemblyItem(Tag, 2),
|
||||
u256(42),
|
||||
AssemblyItem{AssignImmutable, 0x1234},
|
||||
Instruction::JUMP
|
||||
};
|
||||
|
||||
AssemblyItems input = AssemblyItems{
|
||||
AssemblyItem(PushTag, 2),
|
||||
AssemblyItem(PushTag, 1),
|
||||
} + blocks;
|
||||
AssemblyItems output = AssemblyItems{
|
||||
AssemblyItem(PushTag, 1),
|
||||
AssemblyItem(PushTag, 1),
|
||||
} + blocks;
|
||||
BlockDeduplicator dedup(input);
|
||||
dedup.deduplicate();
|
||||
BOOST_CHECK_EQUAL_COLLECTIONS(input.begin(), input.end(), output.begin(), output.end());
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(block_deduplicator_assign_immutable_different_value)
|
||||
{
|
||||
AssemblyItems input{
|
||||
AssemblyItem(PushTag, 2),
|
||||
AssemblyItem(PushTag, 1),
|
||||
AssemblyItem(Tag, 1),
|
||||
u256(42),
|
||||
AssemblyItem{AssignImmutable, 0x1234},
|
||||
Instruction::JUMP,
|
||||
AssemblyItem(Tag, 2),
|
||||
u256(23),
|
||||
AssemblyItem{AssignImmutable, 0x1234},
|
||||
Instruction::JUMP
|
||||
};
|
||||
BlockDeduplicator dedup(input);
|
||||
BOOST_CHECK(!dedup.deduplicate());
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(block_deduplicator_assign_immutable_different_hash)
|
||||
{
|
||||
AssemblyItems input{
|
||||
AssemblyItem(PushTag, 2),
|
||||
AssemblyItem(PushTag, 1),
|
||||
AssemblyItem(Tag, 1),
|
||||
u256(42),
|
||||
AssemblyItem{AssignImmutable, 0x1234},
|
||||
Instruction::JUMP,
|
||||
AssemblyItem(Tag, 2),
|
||||
u256(42),
|
||||
AssemblyItem{AssignImmutable, 0xABCD},
|
||||
Instruction::JUMP
|
||||
};
|
||||
BlockDeduplicator dedup(input);
|
||||
BOOST_CHECK(!dedup.deduplicate());
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(block_deduplicator_loops)
|
||||
{
|
||||
AssemblyItems input{
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
contract D {
|
||||
function f() external view returns (uint256) {
|
||||
return 42;
|
||||
}
|
||||
}
|
||||
contract C {
|
||||
D d;
|
||||
function() external view returns(uint256) immutable z;
|
||||
constructor() public {
|
||||
d = new D();
|
||||
z = d.f;
|
||||
}
|
||||
function f() public view returns (uint256) {
|
||||
assert(z.address == address(d));
|
||||
assert(z.selector == D.f.selector);
|
||||
return z();
|
||||
}
|
||||
}
|
||||
// ----
|
||||
// f() -> 42
|
||||
@@ -0,0 +1,13 @@
|
||||
contract C {
|
||||
uint256 immutable x;
|
||||
uint256 immutable y;
|
||||
constructor() public {
|
||||
x = 42;
|
||||
y = x;
|
||||
}
|
||||
function f() public view returns (uint256, uint256) {
|
||||
return (x+x,y);
|
||||
}
|
||||
}
|
||||
// ----
|
||||
// f() -> 84, 42
|
||||
@@ -0,0 +1,13 @@
|
||||
contract C {
|
||||
uint256 immutable x;
|
||||
uint256 immutable y;
|
||||
constructor() public {
|
||||
x = 42;
|
||||
y = 23;
|
||||
}
|
||||
function f() public view returns (uint256, uint256) {
|
||||
return (x+x,y);
|
||||
}
|
||||
}
|
||||
// ----
|
||||
// f() -> 84, 23
|
||||
Reference in New Issue
Block a user