diff --git a/libevmasm/ConstantOptimiser.cpp b/libevmasm/ConstantOptimiser.cpp index bead74407..70fbb0ef7 100644 --- a/libevmasm/ConstantOptimiser.cpp +++ b/libevmasm/ConstantOptimiser.cpp @@ -153,21 +153,17 @@ AssemblyItems CodeCopyMethod::execute(Assembly& _assembly) const { bytes data = toBigEndian(m_value); assertThrow(data.size() == 32, OptimizerException, "Invalid number encoding."); - AssemblyItems actualCopyRoutine = copyRoutine(); - if (m_params.evmVersion.hasPush0()) - actualCopyRoutine[3] = _assembly.newData(data); - else - actualCopyRoutine[4] = _assembly.newData(data); - return actualCopyRoutine; + AssemblyItem newPushData = _assembly.newData(data); + return copyRoutine(&newPushData); } -AssemblyItems const& CodeCopyMethod::copyRoutine() const +AssemblyItems CodeCopyMethod::copyRoutine(AssemblyItem* _pushData) const { // PUSH0 is cheaper than PUSHn/DUP/SWAP. if (m_params.evmVersion.hasPush0()) { // This costs ~29 gas. - AssemblyItems static copyRoutine{ + AssemblyItems copyRoutine{ // back up memory // mload(0) u256(0), @@ -175,7 +171,7 @@ AssemblyItems const& CodeCopyMethod::copyRoutine() const // codecopy(0, , 32) u256(32), - AssemblyItem(PushData, u256(1) << 16), // replaced above in actualCopyRoutine[3] + (_pushData ? *_pushData : AssemblyItem(PushData, u256(1) << 16)), u256(0), Instruction::CODECOPY, @@ -194,7 +190,7 @@ AssemblyItems const& CodeCopyMethod::copyRoutine() const else { // This costs ~33 gas. - AssemblyItems static copyRoutine{ + AssemblyItems copyRoutine{ // constant to be reused 3+ times u256(0), @@ -205,7 +201,7 @@ AssemblyItems const& CodeCopyMethod::copyRoutine() const // codecopy(0, , 32) u256(32), - AssemblyItem(PushData, u256(1) << 16), // replaced above in actualCopyRoutine[4] + (_pushData ? *_pushData : AssemblyItem(PushData, u256(1) << 16)), Instruction::DUP4, Instruction::CODECOPY, diff --git a/libevmasm/ConstantOptimiser.h b/libevmasm/ConstantOptimiser.h index 6199c2296..6009756d7 100644 --- a/libevmasm/ConstantOptimiser.h +++ b/libevmasm/ConstantOptimiser.h @@ -123,7 +123,7 @@ public: AssemblyItems execute(Assembly& _assembly) const override; protected: - AssemblyItems const& copyRoutine() const; + AssemblyItems copyRoutine(AssemblyItem* _pushData = nullptr) const; }; /**