Remove POPs from ASSIGNIMMUTABLE generated bytecode, if possible.

This commit is contained in:
Daniel Kirchner 2020-10-14 17:31:01 +02:00
parent 0dca223b45
commit 68667f0e84
2 changed files with 19 additions and 14 deletions

View File

@ -672,21 +672,30 @@ LinkerObject const& Assembly::assemble() const
ret.bytecode.resize(ret.bytecode.size() + 32);
break;
case AssignImmutable:
for (auto const& offset: immutableReferencesBySub[i.data()].second)
{
auto const& offsets = immutableReferencesBySub[i.data()].second;
for (size_t i = 0; i < offsets.size(); ++i)
{
ret.bytecode.push_back(uint8_t(Instruction::DUP2));
ret.bytecode.push_back(uint8_t(Instruction::DUP2));
if (i != offsets.size() - 1)
{
ret.bytecode.push_back(uint8_t(Instruction::DUP2));
ret.bytecode.push_back(uint8_t(Instruction::DUP2));
}
// TODO: should we make use of the constant optimizer methods for pushing the offsets?
bytes offsetBytes = toCompactBigEndian(u256(offset));
bytes offsetBytes = toCompactBigEndian(u256(offsets[i]));
ret.bytecode.push_back(uint8_t(Instruction::PUSH1) - 1 + offsetBytes.size());
ret.bytecode += offsetBytes;
ret.bytecode.push_back(uint8_t(Instruction::ADD));
ret.bytecode.push_back(uint8_t(Instruction::MSTORE));
}
if (offsets.empty())
{
ret.bytecode.push_back(uint8_t(Instruction::POP));
ret.bytecode.push_back(uint8_t(Instruction::POP));
}
immutableReferencesBySub.erase(i.data());
ret.bytecode.push_back(uint8_t(Instruction::POP));
ret.bytecode.push_back(uint8_t(Instruction::POP));
break;
}
case PushDeployTimeAddress:
ret.bytecode.push_back(uint8_t(Instruction::PUSH20));
ret.bytecode.resize(ret.bytecode.size() + 20);

View File

@ -104,9 +104,9 @@ BOOST_AUTO_TEST_CASE(all_assembly_items)
BOOST_CHECK_EQUAL(
_assembly.assemble().toHex(),
"5b6001600220607373__$bf005014d9d0f534b8fcb268bd84c491a2$__"
"600056606b602260497300000000000000000000000000000000000000005050"
"60028181600101525050"
"5b6001600220606f73__$bf005014d9d0f534b8fcb268bd84c491a2$__"
"6000566067602260457300000000000000000000000000000000000000005050"
"600260010152"
"00fe"
"7f0000000000000000000000000000000000000000000000000000000000000000"
"fe010203044266eeaa"
@ -205,21 +205,17 @@ BOOST_AUTO_TEST_CASE(immutable)
"6001" // PUSH1 1 - offset of first someImmutable in sub_0
"01" // ADD - add offset of immutable to offset of code
"52" // MSTORE
"8181" // DUP2 DUP2
"6043" // PUSH1 67 - offset of second someImmutable in sub_0
"01" // ADD - add offset of immutable to offset of code
"52" // MSTORE
"5050" // POP POP
// assign "someOtherImmutable"
"6017" // PUSH1 23 - value for someOtherImmutable
"6000" // PUSH1 0 - offset of code into which to insert the immutable
"8181" // DUP2 DUP2
"6022" // PUSH1 34 - offset of someOtherImmutable in sub_0
"01" // ADD - add offset of immutable to offset of code
"52" // MSTORE
"5050" // POP POP
"6063" // PUSH1 0x63 - dataSize(sub_0)
"6023" // PUSH1 0x23 - dataOffset(sub_0)
"601b" // PUSH1 0x23 - dataOffset(sub_0)
"fe" // INVALID
// end of root.asm
// sub.asm