From b44b9285e4978c83785939ff970b8052f1aa686c Mon Sep 17 00:00:00 2001 From: Marenz Date: Tue, 4 Oct 2022 01:14:00 +0200 Subject: [PATCH] Asm output --- libevmasm/Assembly.cpp | 50 +- libevmasm/Disassemble.cpp | 12 +- libevmasm/Disassemble.h | 5 +- libevmasm/Instruction.cpp | 649 +++++++++++---------- libevmasm/Instruction.h | 347 ++++++++--- liblangutil/EVMVersion.cpp | 4 + liblangutil/EVMVersion.h | 2 +- libsolidity/interface/StandardCompiler.cpp | 7 +- libyul/AsmAnalysis.cpp | 73 ++- libyul/backends/evm/EVMDialect.cpp | 30 +- solc/CommandLineInterface.cpp | 6 +- test/libevmasm/Assembler.cpp | 8 +- test/libyul/ObjectCompilerTest.cpp | 2 +- 13 files changed, 739 insertions(+), 456 deletions(-) diff --git a/libevmasm/Assembly.cpp b/libevmasm/Assembly.cpp index 8e06da8ae..e3bcb068a 100644 --- a/libevmasm/Assembly.cpp +++ b/libevmasm/Assembly.cpp @@ -426,7 +426,7 @@ map const& Assembly::optimiseInternal( AssemblyItems optimisedItems; bool usesMSize = ranges::any_of(m_items, [](AssemblyItem const& _i) { - return _i == AssemblyItem{Instruction::MSIZE} || _i.type() == VerbatimBytecode; + return _i == AssemblyItem{InternalInstruction::MSIZE} || _i.type() == VerbatimBytecode; }); auto iter = m_items.begin(); @@ -537,16 +537,22 @@ LinkerObject const& Assembly::assemble() const multimap subRef; vector sizeRef; ///< Pointers to code locations where the size of the program is inserted unsigned bytesPerTag = numberEncodingSize(bytesRequiredForCode); - uint8_t tagPush = static_cast(pushInstruction(bytesPerTag)); + InternalInstruction tagPush = pushInstruction(bytesPerTag); unsigned bytesRequiredIncludingData = bytesRequiredForCode + 1 + static_cast(m_auxiliaryData.size()); for (auto const& sub: m_subs) bytesRequiredIncludingData += static_cast(sub->assemble().bytecode.size()); unsigned bytesPerDataRef = numberEncodingSize(bytesRequiredIncludingData); - uint8_t dataRefPush = static_cast(pushInstruction(bytesPerDataRef)); + InternalInstruction dataRefPush = pushInstruction(bytesPerDataRef); + ret.bytecode.reserve(bytesRequiredIncludingData); + auto const appendInstruction = [&](InternalInstruction _instruction) + { + ret.bytecode.push_back(static_cast((evmasm::instructionOpcode(_instruction)))); + }; + for (AssemblyItem const& i: m_items) { // store position of the invalid jump destination @@ -556,12 +562,12 @@ LinkerObject const& Assembly::assemble() const switch (i.type()) { case Operation: - ret.bytecode.push_back(static_cast(i.instruction())); + appendInstruction(i.instruction()); break; case Push: { unsigned b = max(1, numberEncodingSize(i.data())); - ret.bytecode.push_back(static_cast(pushInstruction(b))); + appendInstruction(pushInstruction(b)); ret.bytecode.resize(ret.bytecode.size() + b); bytesRef byr(&ret.bytecode.back() + 1 - b, b); toBigEndian(i.data(), byr); @@ -569,19 +575,19 @@ LinkerObject const& Assembly::assemble() const } case PushTag: { - ret.bytecode.push_back(tagPush); + appendInstruction(tagPush); tagRef[ret.bytecode.size()] = i.splitForeignPushTag(); ret.bytecode.resize(ret.bytecode.size() + bytesPerTag); break; } case PushData: - ret.bytecode.push_back(dataRefPush); + appendInstruction(dataRefPush); dataRef.insert(make_pair(h256(i.data()), ret.bytecode.size())); ret.bytecode.resize(ret.bytecode.size() + bytesPerDataRef); break; case PushSub: assertThrow(i.data() <= numeric_limits::max(), AssemblyException, ""); - ret.bytecode.push_back(dataRefPush); + appendInstruction(dataRefPush); subRef.insert(make_pair(static_cast(i.data()), ret.bytecode.size())); ret.bytecode.resize(ret.bytecode.size() + bytesPerDataRef); break; @@ -591,7 +597,7 @@ LinkerObject const& Assembly::assemble() const auto s = subAssemblyById(static_cast(i.data()))->assemble().bytecode.size(); i.setPushedValue(u256(s)); unsigned b = max(1, numberEncodingSize(s)); - ret.bytecode.push_back(static_cast(pushInstruction(b))); + appendInstruction(pushInstruction(b)); ret.bytecode.resize(ret.bytecode.size() + b); bytesRef byr(&ret.bytecode.back() + 1 - b, b); toBigEndian(s, byr); @@ -599,18 +605,18 @@ LinkerObject const& Assembly::assemble() const } case PushProgramSize: { - ret.bytecode.push_back(dataRefPush); + appendInstruction(dataRefPush); sizeRef.push_back(static_cast(ret.bytecode.size())); ret.bytecode.resize(ret.bytecode.size() + bytesPerDataRef); break; } case PushLibraryAddress: - ret.bytecode.push_back(static_cast(Instruction::PUSH20)); + appendInstruction(InternalInstruction::PUSH20); ret.linkReferences[ret.bytecode.size()] = m_libraries.at(i.data()); ret.bytecode.resize(ret.bytecode.size() + 20); break; case PushImmutable: - ret.bytecode.push_back(static_cast(Instruction::PUSH32)); + appendInstruction(InternalInstruction::PUSH32); // Maps keccak back to the "identifier" string of that immutable. ret.immutableReferences[i.data()].first = m_immutables.at(i.data()); // Record the bytecode offset of the PUSH32 argument. @@ -629,26 +635,26 @@ LinkerObject const& Assembly::assemble() const { if (i != offsets.size() - 1) { - ret.bytecode.push_back(uint8_t(Instruction::DUP2)); - ret.bytecode.push_back(uint8_t(Instruction::DUP2)); + appendInstruction(InternalInstruction::DUP2); + appendInstruction(InternalInstruction::DUP2); } // TODO: should we make use of the constant optimizer methods for pushing the offsets? bytes offsetBytes = toCompactBigEndian(u256(offsets[i])); - ret.bytecode.push_back(static_cast(pushInstruction(static_cast(offsetBytes.size())))); + appendInstruction(pushInstruction(static_cast(offsetBytes.size()))); ret.bytecode += offsetBytes; - ret.bytecode.push_back(uint8_t(Instruction::ADD)); - ret.bytecode.push_back(uint8_t(Instruction::MSTORE)); + appendInstruction(InternalInstruction::ADD); + appendInstruction(InternalInstruction::MSTORE); } if (offsets.empty()) { - ret.bytecode.push_back(uint8_t(Instruction::POP)); - ret.bytecode.push_back(uint8_t(Instruction::POP)); + appendInstruction(InternalInstruction::POP); + appendInstruction(InternalInstruction::POP); } immutableReferencesBySub.erase(i.data()); break; } case PushDeployTimeAddress: - ret.bytecode.push_back(static_cast(Instruction::PUSH20)); + appendInstruction(InternalInstruction::PUSH20); ret.bytecode.resize(ret.bytecode.size() + 20); break; case Tag: @@ -659,7 +665,7 @@ LinkerObject const& Assembly::assemble() const assertThrow(ret.bytecode.size() < 0xffffffffL, AssemblyException, "Tag too large."); assertThrow(m_tagPositionsInBytecode[tagId] == numeric_limits::max(), AssemblyException, "Duplicate tag position."); m_tagPositionsInBytecode[tagId] = ret.bytecode.size(); - ret.bytecode.push_back(static_cast(Instruction::JUMPDEST)); + appendInstruction(InternalInstruction::JUMPDEST); break; } default: @@ -677,7 +683,7 @@ LinkerObject const& Assembly::assemble() const if (!m_subs.empty() || !m_data.empty() || !m_auxiliaryData.empty()) // Append an INVALID here to help tests find miscompilation. - ret.bytecode.push_back(static_cast(Instruction::INVALID)); + appendInstruction(InternalInstruction::INVALID); for (auto const& [subIdPath, bytecodeOffset]: subRef) { diff --git a/libevmasm/Disassemble.cpp b/libevmasm/Disassemble.cpp index aa5b2820c..e4cae8d7d 100644 --- a/libevmasm/Disassemble.cpp +++ b/libevmasm/Disassemble.cpp @@ -16,6 +16,8 @@ */ // SPDX-License-Identifier: GPL-3.0 +#include "libevmasm/Instruction.h" +#include "liblangutil/EVMVersion.h" #include #include @@ -27,15 +29,15 @@ using namespace solidity; using namespace solidity::util; using namespace solidity::evmasm; - void solidity::evmasm::eachInstruction( bytes const& _mem, - function const& _onInstruction + langutil::EVMVersion _evmVersion, + function const& _onInstruction ) { for (auto it = _mem.begin(); it < _mem.end(); ++it) { - Instruction const instr{*it}; + InternalInstruction instr = internalInstruction(InstructionOpCode(*it), _evmVersion); int additional = 0; if (isValidInstruction(instr)) additional = instructionInfo(instr).additional; @@ -57,10 +59,10 @@ void solidity::evmasm::eachInstruction( } } -string solidity::evmasm::disassemble(bytes const& _mem, string const& _delimiter) +string solidity::evmasm::disassemble(bytes const& _mem, langutil::EVMVersion _evmVersion, string const& _delimiter) { stringstream ret; - eachInstruction(_mem, [&](Instruction _instr, u256 const& _data) { + eachInstruction(_mem, _evmVersion, [&](InternalInstruction _instr, u256 const& _data) { if (!isValidInstruction(_instr)) ret << "0x" << std::uppercase << std::hex << static_cast(_instr) << _delimiter; else diff --git a/libevmasm/Disassemble.h b/libevmasm/Disassemble.h index cf194e24b..82705c081 100644 --- a/libevmasm/Disassemble.h +++ b/libevmasm/Disassemble.h @@ -18,6 +18,7 @@ #pragma once +#include "liblangutil/EVMVersion.h" #include #include @@ -30,9 +31,9 @@ namespace solidity::evmasm { /// Iterate through EVM code and call a function on each instruction. -void eachInstruction(bytes const& _mem, std::function const& _onInstruction); +void eachInstruction(bytes const& _mem, langutil::EVMVersion _evmVersion, std::function const& _onInstruction); /// Convert from EVM code to simple EVM assembly language. -std::string disassemble(bytes const& _mem, std::string const& _delimiter = " "); +std::string disassemble(bytes const& _mem, langutil::EVMVersion _evmVersion, std::string const& _delimiter = " "); } diff --git a/libevmasm/Instruction.cpp b/libevmasm/Instruction.cpp index 78eaefac3..7e609305d 100644 --- a/libevmasm/Instruction.cpp +++ b/libevmasm/Instruction.cpp @@ -15,328 +15,379 @@ along with solidity. If not, see . */ // SPDX-License-Identifier: GPL-3.0 -/** @file Instruction.cpp +/** @file InternalInstruction.cpp * @author Gav Wood * @date 2014 */ +#include #include +#include +#include + +#include using namespace std; using namespace solidity; using namespace solidity::util; using namespace solidity::evmasm; -std::map const solidity::evmasm::c_instructions = +/// Caches the opcode to internal mapping upon first usage. +std::multimap opcode2InternalCache; + +std::map const solidity::evmasm::c_instructions = { - { "STOP", Instruction::STOP }, - { "ADD", Instruction::ADD }, - { "SUB", Instruction::SUB }, - { "MUL", Instruction::MUL }, - { "DIV", Instruction::DIV }, - { "SDIV", Instruction::SDIV }, - { "MOD", Instruction::MOD }, - { "SMOD", Instruction::SMOD }, - { "EXP", Instruction::EXP }, - { "NOT", Instruction::NOT }, - { "LT", Instruction::LT }, - { "GT", Instruction::GT }, - { "SLT", Instruction::SLT }, - { "SGT", Instruction::SGT }, - { "EQ", Instruction::EQ }, - { "ISZERO", Instruction::ISZERO }, - { "AND", Instruction::AND }, - { "OR", Instruction::OR }, - { "XOR", Instruction::XOR }, - { "BYTE", Instruction::BYTE }, - { "SHL", Instruction::SHL }, - { "SHR", Instruction::SHR }, - { "SAR", Instruction::SAR }, - { "ADDMOD", Instruction::ADDMOD }, - { "MULMOD", Instruction::MULMOD }, - { "SIGNEXTEND", Instruction::SIGNEXTEND }, - { "KECCAK256", Instruction::KECCAK256 }, - { "ADDRESS", Instruction::ADDRESS }, - { "BALANCE", Instruction::BALANCE }, - { "ORIGIN", Instruction::ORIGIN }, - { "CALLER", Instruction::CALLER }, - { "CALLVALUE", Instruction::CALLVALUE }, - { "CALLDATALOAD", Instruction::CALLDATALOAD }, - { "CALLDATASIZE", Instruction::CALLDATASIZE }, - { "CALLDATACOPY", Instruction::CALLDATACOPY }, - { "CODESIZE", Instruction::CODESIZE }, - { "CODECOPY", Instruction::CODECOPY }, - { "GASPRICE", Instruction::GASPRICE }, - { "EXTCODESIZE", Instruction::EXTCODESIZE }, - { "EXTCODECOPY", Instruction::EXTCODECOPY }, - { "RETURNDATASIZE", Instruction::RETURNDATASIZE }, - { "RETURNDATACOPY", Instruction::RETURNDATACOPY }, - { "EXTCODEHASH", Instruction::EXTCODEHASH }, - { "BLOCKHASH", Instruction::BLOCKHASH }, - { "COINBASE", Instruction::COINBASE }, - { "TIMESTAMP", Instruction::TIMESTAMP }, - { "NUMBER", Instruction::NUMBER }, - { "DIFFICULTY", Instruction::DIFFICULTY }, - { "PREVRANDAO", Instruction::PREVRANDAO }, - { "GASLIMIT", Instruction::GASLIMIT }, - { "CHAINID", Instruction::CHAINID }, - { "SELFBALANCE", Instruction::SELFBALANCE }, - { "BASEFEE", Instruction::BASEFEE }, - { "POP", Instruction::POP }, - { "MLOAD", Instruction::MLOAD }, - { "MSTORE", Instruction::MSTORE }, - { "MSTORE8", Instruction::MSTORE8 }, - { "SLOAD", Instruction::SLOAD }, - { "SSTORE", Instruction::SSTORE }, - { "JUMP", Instruction::JUMP }, - { "JUMPI", Instruction::JUMPI }, - { "PC", Instruction::PC }, - { "MSIZE", Instruction::MSIZE }, - { "GAS", Instruction::GAS }, - { "JUMPDEST", Instruction::JUMPDEST }, - { "PUSH1", Instruction::PUSH1 }, - { "PUSH2", Instruction::PUSH2 }, - { "PUSH3", Instruction::PUSH3 }, - { "PUSH4", Instruction::PUSH4 }, - { "PUSH5", Instruction::PUSH5 }, - { "PUSH6", Instruction::PUSH6 }, - { "PUSH7", Instruction::PUSH7 }, - { "PUSH8", Instruction::PUSH8 }, - { "PUSH9", Instruction::PUSH9 }, - { "PUSH10", Instruction::PUSH10 }, - { "PUSH11", Instruction::PUSH11 }, - { "PUSH12", Instruction::PUSH12 }, - { "PUSH13", Instruction::PUSH13 }, - { "PUSH14", Instruction::PUSH14 }, - { "PUSH15", Instruction::PUSH15 }, - { "PUSH16", Instruction::PUSH16 }, - { "PUSH17", Instruction::PUSH17 }, - { "PUSH18", Instruction::PUSH18 }, - { "PUSH19", Instruction::PUSH19 }, - { "PUSH20", Instruction::PUSH20 }, - { "PUSH21", Instruction::PUSH21 }, - { "PUSH22", Instruction::PUSH22 }, - { "PUSH23", Instruction::PUSH23 }, - { "PUSH24", Instruction::PUSH24 }, - { "PUSH25", Instruction::PUSH25 }, - { "PUSH26", Instruction::PUSH26 }, - { "PUSH27", Instruction::PUSH27 }, - { "PUSH28", Instruction::PUSH28 }, - { "PUSH29", Instruction::PUSH29 }, - { "PUSH30", Instruction::PUSH30 }, - { "PUSH31", Instruction::PUSH31 }, - { "PUSH32", Instruction::PUSH32 }, - { "DUP1", Instruction::DUP1 }, - { "DUP2", Instruction::DUP2 }, - { "DUP3", Instruction::DUP3 }, - { "DUP4", Instruction::DUP4 }, - { "DUP5", Instruction::DUP5 }, - { "DUP6", Instruction::DUP6 }, - { "DUP7", Instruction::DUP7 }, - { "DUP8", Instruction::DUP8 }, - { "DUP9", Instruction::DUP9 }, - { "DUP10", Instruction::DUP10 }, - { "DUP11", Instruction::DUP11 }, - { "DUP12", Instruction::DUP12 }, - { "DUP13", Instruction::DUP13 }, - { "DUP14", Instruction::DUP14 }, - { "DUP15", Instruction::DUP15 }, - { "DUP16", Instruction::DUP16 }, - { "SWAP1", Instruction::SWAP1 }, - { "SWAP2", Instruction::SWAP2 }, - { "SWAP3", Instruction::SWAP3 }, - { "SWAP4", Instruction::SWAP4 }, - { "SWAP5", Instruction::SWAP5 }, - { "SWAP6", Instruction::SWAP6 }, - { "SWAP7", Instruction::SWAP7 }, - { "SWAP8", Instruction::SWAP8 }, - { "SWAP9", Instruction::SWAP9 }, - { "SWAP10", Instruction::SWAP10 }, - { "SWAP11", Instruction::SWAP11 }, - { "SWAP12", Instruction::SWAP12 }, - { "SWAP13", Instruction::SWAP13 }, - { "SWAP14", Instruction::SWAP14 }, - { "SWAP15", Instruction::SWAP15 }, - { "SWAP16", Instruction::SWAP16 }, - { "LOG0", Instruction::LOG0 }, - { "LOG1", Instruction::LOG1 }, - { "LOG2", Instruction::LOG2 }, - { "LOG3", Instruction::LOG3 }, - { "LOG4", Instruction::LOG4 }, - { "CREATE", Instruction::CREATE }, - { "CALL", Instruction::CALL }, - { "CALLCODE", Instruction::CALLCODE }, - { "STATICCALL", Instruction::STATICCALL }, - { "RETURN", Instruction::RETURN }, - { "DELEGATECALL", Instruction::DELEGATECALL }, - { "CREATE2", Instruction::CREATE2 }, - { "REVERT", Instruction::REVERT }, - { "INVALID", Instruction::INVALID }, - { "SELFDESTRUCT", Instruction::SELFDESTRUCT } + { "STOP", InternalInstruction::STOP }, + { "ADD", InternalInstruction::ADD }, + { "SUB", InternalInstruction::SUB }, + { "MUL", InternalInstruction::MUL }, + { "DIV", InternalInstruction::DIV }, + { "SDIV", InternalInstruction::SDIV }, + { "MOD", InternalInstruction::MOD }, + { "SMOD", InternalInstruction::SMOD }, + { "EXP", InternalInstruction::EXP }, + { "NOT", InternalInstruction::NOT }, + { "LT", InternalInstruction::LT }, + { "GT", InternalInstruction::GT }, + { "SLT", InternalInstruction::SLT }, + { "SGT", InternalInstruction::SGT }, + { "EQ", InternalInstruction::EQ }, + { "ISZERO", InternalInstruction::ISZERO }, + { "AND", InternalInstruction::AND }, + { "OR", InternalInstruction::OR }, + { "XOR", InternalInstruction::XOR }, + { "BYTE", InternalInstruction::BYTE }, + { "SHL", InternalInstruction::SHL }, + { "SHR", InternalInstruction::SHR }, + { "SAR", InternalInstruction::SAR }, + { "ADDMOD", InternalInstruction::ADDMOD }, + { "MULMOD", InternalInstruction::MULMOD }, + { "SIGNEXTEND", InternalInstruction::SIGNEXTEND }, + { "KECCAK256", InternalInstruction::KECCAK256 }, + { "ADDRESS", InternalInstruction::ADDRESS }, + { "BALANCE", InternalInstruction::BALANCE }, + { "ORIGIN", InternalInstruction::ORIGIN }, + { "CALLER", InternalInstruction::CALLER }, + { "CALLVALUE", InternalInstruction::CALLVALUE }, + { "CALLDATALOAD", InternalInstruction::CALLDATALOAD }, + { "CALLDATASIZE", InternalInstruction::CALLDATASIZE }, + { "CALLDATACOPY", InternalInstruction::CALLDATACOPY }, + { "CODESIZE", InternalInstruction::CODESIZE }, + { "CODECOPY", InternalInstruction::CODECOPY }, + { "GASPRICE", InternalInstruction::GASPRICE }, + { "EXTCODESIZE", InternalInstruction::EXTCODESIZE }, + { "EXTCODECOPY", InternalInstruction::EXTCODECOPY }, + { "RETURNDATASIZE", InternalInstruction::RETURNDATASIZE }, + { "RETURNDATACOPY", InternalInstruction::RETURNDATACOPY }, + { "EXTCODEHASH", InternalInstruction::EXTCODEHASH }, + { "BLOCKHASH", InternalInstruction::BLOCKHASH }, + { "COINBASE", InternalInstruction::COINBASE }, + { "TIMESTAMP", InternalInstruction::TIMESTAMP }, + { "NUMBER", InternalInstruction::NUMBER }, + { "DIFFICULTY", InternalInstruction::DIFFICULTY }, + { "PREVRANDAO", InternalInstruction::PREVRANDAO }, + { "GASLIMIT", InternalInstruction::GASLIMIT }, + { "CHAINID", InternalInstruction::CHAINID }, + { "SELFBALANCE", InternalInstruction::SELFBALANCE }, + { "BASEFEE", InternalInstruction::BASEFEE }, + { "POP", InternalInstruction::POP }, + { "MLOAD", InternalInstruction::MLOAD }, + { "MSTORE", InternalInstruction::MSTORE }, + { "MSTORE8", InternalInstruction::MSTORE8 }, + { "SLOAD", InternalInstruction::SLOAD }, + { "SSTORE", InternalInstruction::SSTORE }, + { "JUMP", InternalInstruction::JUMP }, + { "JUMPI", InternalInstruction::JUMPI }, + { "PC", InternalInstruction::PC }, + { "MSIZE", InternalInstruction::MSIZE }, + { "GAS", InternalInstruction::GAS }, + { "JUMPDEST", InternalInstruction::JUMPDEST }, + { "PUSH1", InternalInstruction::PUSH1 }, + { "PUSH2", InternalInstruction::PUSH2 }, + { "PUSH3", InternalInstruction::PUSH3 }, + { "PUSH4", InternalInstruction::PUSH4 }, + { "PUSH5", InternalInstruction::PUSH5 }, + { "PUSH6", InternalInstruction::PUSH6 }, + { "PUSH7", InternalInstruction::PUSH7 }, + { "PUSH8", InternalInstruction::PUSH8 }, + { "PUSH9", InternalInstruction::PUSH9 }, + { "PUSH10", InternalInstruction::PUSH10 }, + { "PUSH11", InternalInstruction::PUSH11 }, + { "PUSH12", InternalInstruction::PUSH12 }, + { "PUSH13", InternalInstruction::PUSH13 }, + { "PUSH14", InternalInstruction::PUSH14 }, + { "PUSH15", InternalInstruction::PUSH15 }, + { "PUSH16", InternalInstruction::PUSH16 }, + { "PUSH17", InternalInstruction::PUSH17 }, + { "PUSH18", InternalInstruction::PUSH18 }, + { "PUSH19", InternalInstruction::PUSH19 }, + { "PUSH20", InternalInstruction::PUSH20 }, + { "PUSH21", InternalInstruction::PUSH21 }, + { "PUSH22", InternalInstruction::PUSH22 }, + { "PUSH23", InternalInstruction::PUSH23 }, + { "PUSH24", InternalInstruction::PUSH24 }, + { "PUSH25", InternalInstruction::PUSH25 }, + { "PUSH26", InternalInstruction::PUSH26 }, + { "PUSH27", InternalInstruction::PUSH27 }, + { "PUSH28", InternalInstruction::PUSH28 }, + { "PUSH29", InternalInstruction::PUSH29 }, + { "PUSH30", InternalInstruction::PUSH30 }, + { "PUSH31", InternalInstruction::PUSH31 }, + { "PUSH32", InternalInstruction::PUSH32 }, + { "DUP1", InternalInstruction::DUP1 }, + { "DUP2", InternalInstruction::DUP2 }, + { "DUP3", InternalInstruction::DUP3 }, + { "DUP4", InternalInstruction::DUP4 }, + { "DUP5", InternalInstruction::DUP5 }, + { "DUP6", InternalInstruction::DUP6 }, + { "DUP7", InternalInstruction::DUP7 }, + { "DUP8", InternalInstruction::DUP8 }, + { "DUP9", InternalInstruction::DUP9 }, + { "DUP10", InternalInstruction::DUP10 }, + { "DUP11", InternalInstruction::DUP11 }, + { "DUP12", InternalInstruction::DUP12 }, + { "DUP13", InternalInstruction::DUP13 }, + { "DUP14", InternalInstruction::DUP14 }, + { "DUP15", InternalInstruction::DUP15 }, + { "DUP16", InternalInstruction::DUP16 }, + { "SWAP1", InternalInstruction::SWAP1 }, + { "SWAP2", InternalInstruction::SWAP2 }, + { "SWAP3", InternalInstruction::SWAP3 }, + { "SWAP4", InternalInstruction::SWAP4 }, + { "SWAP5", InternalInstruction::SWAP5 }, + { "SWAP6", InternalInstruction::SWAP6 }, + { "SWAP7", InternalInstruction::SWAP7 }, + { "SWAP8", InternalInstruction::SWAP8 }, + { "SWAP9", InternalInstruction::SWAP9 }, + { "SWAP10", InternalInstruction::SWAP10 }, + { "SWAP11", InternalInstruction::SWAP11 }, + { "SWAP12", InternalInstruction::SWAP12 }, + { "SWAP13", InternalInstruction::SWAP13 }, + { "SWAP14", InternalInstruction::SWAP14 }, + { "SWAP15", InternalInstruction::SWAP15 }, + { "SWAP16", InternalInstruction::SWAP16 }, + { "LOG0", InternalInstruction::LOG0 }, + { "LOG1", InternalInstruction::LOG1 }, + { "LOG2", InternalInstruction::LOG2 }, + { "LOG3", InternalInstruction::LOG3 }, + { "LOG4", InternalInstruction::LOG4 }, + { "CREATE", InternalInstruction::CREATE }, + { "CALL", InternalInstruction::CALL }, + { "CALLCODE", InternalInstruction::CALLCODE }, + { "STATICCALL", InternalInstruction::STATICCALL }, + { "RETURN", InternalInstruction::RETURN }, + { "DELEGATECALL", InternalInstruction::DELEGATECALL }, + { "CREATE2", InternalInstruction::CREATE2 }, + { "REVERT", InternalInstruction::REVERT }, + { "INVALID", InternalInstruction::INVALID }, + { "SELFDESTRUCT", InternalInstruction::SELFDESTRUCT } }; -static std::map const c_instructionInfo = -{ // Add, Args, Ret, SideEffects, GasPriceTier - { Instruction::STOP, { "STOP", 0, 0, 0, true, Tier::Zero } }, - { Instruction::ADD, { "ADD", 0, 2, 1, false, Tier::VeryLow } }, - { Instruction::SUB, { "SUB", 0, 2, 1, false, Tier::VeryLow } }, - { Instruction::MUL, { "MUL", 0, 2, 1, false, Tier::Low } }, - { Instruction::DIV, { "DIV", 0, 2, 1, false, Tier::Low } }, - { Instruction::SDIV, { "SDIV", 0, 2, 1, false, Tier::Low } }, - { Instruction::MOD, { "MOD", 0, 2, 1, false, Tier::Low } }, - { Instruction::SMOD, { "SMOD", 0, 2, 1, false, Tier::Low } }, - { Instruction::EXP, { "EXP", 0, 2, 1, false, Tier::Special } }, - { Instruction::NOT, { "NOT", 0, 1, 1, false, Tier::VeryLow } }, - { Instruction::LT, { "LT", 0, 2, 1, false, Tier::VeryLow } }, - { Instruction::GT, { "GT", 0, 2, 1, false, Tier::VeryLow } }, - { Instruction::SLT, { "SLT", 0, 2, 1, false, Tier::VeryLow } }, - { Instruction::SGT, { "SGT", 0, 2, 1, false, Tier::VeryLow } }, - { Instruction::EQ, { "EQ", 0, 2, 1, false, Tier::VeryLow } }, - { Instruction::ISZERO, { "ISZERO", 0, 1, 1, false, Tier::VeryLow } }, - { Instruction::AND, { "AND", 0, 2, 1, false, Tier::VeryLow } }, - { Instruction::OR, { "OR", 0, 2, 1, false, Tier::VeryLow } }, - { Instruction::XOR, { "XOR", 0, 2, 1, false, Tier::VeryLow } }, - { Instruction::BYTE, { "BYTE", 0, 2, 1, false, Tier::VeryLow } }, - { Instruction::SHL, { "SHL", 0, 2, 1, false, Tier::VeryLow } }, - { Instruction::SHR, { "SHR", 0, 2, 1, false, Tier::VeryLow } }, - { Instruction::SAR, { "SAR", 0, 2, 1, false, Tier::VeryLow } }, - { Instruction::ADDMOD, { "ADDMOD", 0, 3, 1, false, Tier::Mid } }, - { Instruction::MULMOD, { "MULMOD", 0, 3, 1, false, Tier::Mid } }, - { Instruction::SIGNEXTEND, { "SIGNEXTEND", 0, 2, 1, false, Tier::Low } }, - { Instruction::KECCAK256, { "KECCAK256", 0, 2, 1, true, Tier::Special } }, - { Instruction::ADDRESS, { "ADDRESS", 0, 0, 1, false, Tier::Base } }, - { Instruction::BALANCE, { "BALANCE", 0, 1, 1, false, Tier::Balance } }, - { Instruction::ORIGIN, { "ORIGIN", 0, 0, 1, false, Tier::Base } }, - { Instruction::CALLER, { "CALLER", 0, 0, 1, false, Tier::Base } }, - { Instruction::CALLVALUE, { "CALLVALUE", 0, 0, 1, false, Tier::Base } }, - { Instruction::CALLDATALOAD,{ "CALLDATALOAD", 0, 1, 1, false, Tier::VeryLow } }, - { Instruction::CALLDATASIZE,{ "CALLDATASIZE", 0, 0, 1, false, Tier::Base } }, - { Instruction::CALLDATACOPY,{ "CALLDATACOPY", 0, 3, 0, true, Tier::VeryLow } }, - { Instruction::CODESIZE, { "CODESIZE", 0, 0, 1, false, Tier::Base } }, - { Instruction::CODECOPY, { "CODECOPY", 0, 3, 0, true, Tier::VeryLow } }, - { Instruction::GASPRICE, { "GASPRICE", 0, 0, 1, false, Tier::Base } }, - { Instruction::EXTCODESIZE, { "EXTCODESIZE", 0, 1, 1, false, Tier::ExtCode } }, - { Instruction::EXTCODECOPY, { "EXTCODECOPY", 0, 4, 0, true, Tier::ExtCode } }, - { Instruction::RETURNDATASIZE, {"RETURNDATASIZE", 0, 0, 1, false, Tier::Base } }, - { Instruction::RETURNDATACOPY, {"RETURNDATACOPY", 0, 3, 0, true, Tier::VeryLow } }, - { Instruction::EXTCODEHASH, { "EXTCODEHASH", 0, 1, 1, false, Tier::Balance } }, - { Instruction::BLOCKHASH, { "BLOCKHASH", 0, 1, 1, false, Tier::Ext } }, - { Instruction::COINBASE, { "COINBASE", 0, 0, 1, false, Tier::Base } }, - { Instruction::TIMESTAMP, { "TIMESTAMP", 0, 0, 1, false, Tier::Base } }, - { Instruction::NUMBER, { "NUMBER", 0, 0, 1, false, Tier::Base } }, -// { Instruction::PREVRANDAO, { "PREVRANDAO", 0, 0, 1, false, Tier::Base } }, -// TODO Replace with PREVRANDAO in the next breaking release - { Instruction::DIFFICULTY, { "DIFFICULTY", 0, 0, 1, false, Tier::Base } }, - { Instruction::GASLIMIT, { "GASLIMIT", 0, 0, 1, false, Tier::Base } }, - { Instruction::CHAINID, { "CHAINID", 0, 0, 1, false, Tier::Base } }, - { Instruction::SELFBALANCE, { "SELFBALANCE", 0, 0, 1, false, Tier::Low } }, - { Instruction::BASEFEE, { "BASEFEE", 0, 0, 1, false, Tier::Base } }, - { Instruction::POP, { "POP", 0, 1, 0, false, Tier::Base } }, - { Instruction::MLOAD, { "MLOAD", 0, 1, 1, true, Tier::VeryLow } }, - { Instruction::MSTORE, { "MSTORE", 0, 2, 0, true, Tier::VeryLow } }, - { Instruction::MSTORE8, { "MSTORE8", 0, 2, 0, true, Tier::VeryLow } }, - { Instruction::SLOAD, { "SLOAD", 0, 1, 1, false, Tier::Special } }, - { Instruction::SSTORE, { "SSTORE", 0, 2, 0, true, Tier::Special } }, - { Instruction::JUMP, { "JUMP", 0, 1, 0, true, Tier::Mid } }, - { Instruction::JUMPI, { "JUMPI", 0, 2, 0, true, Tier::High } }, - { Instruction::PC, { "PC", 0, 0, 1, false, Tier::Base } }, - { Instruction::MSIZE, { "MSIZE", 0, 0, 1, false, Tier::Base } }, - { Instruction::GAS, { "GAS", 0, 0, 1, false, Tier::Base } }, - { Instruction::JUMPDEST, { "JUMPDEST", 0, 0, 0, true, Tier::Special } }, - { Instruction::PUSH1, { "PUSH1", 1, 0, 1, false, Tier::VeryLow } }, - { Instruction::PUSH2, { "PUSH2", 2, 0, 1, false, Tier::VeryLow } }, - { Instruction::PUSH3, { "PUSH3", 3, 0, 1, false, Tier::VeryLow } }, - { Instruction::PUSH4, { "PUSH4", 4, 0, 1, false, Tier::VeryLow } }, - { Instruction::PUSH5, { "PUSH5", 5, 0, 1, false, Tier::VeryLow } }, - { Instruction::PUSH6, { "PUSH6", 6, 0, 1, false, Tier::VeryLow } }, - { Instruction::PUSH7, { "PUSH7", 7, 0, 1, false, Tier::VeryLow } }, - { Instruction::PUSH8, { "PUSH8", 8, 0, 1, false, Tier::VeryLow } }, - { Instruction::PUSH9, { "PUSH9", 9, 0, 1, false, Tier::VeryLow } }, - { Instruction::PUSH10, { "PUSH10", 10, 0, 1, false, Tier::VeryLow } }, - { Instruction::PUSH11, { "PUSH11", 11, 0, 1, false, Tier::VeryLow } }, - { Instruction::PUSH12, { "PUSH12", 12, 0, 1, false, Tier::VeryLow } }, - { Instruction::PUSH13, { "PUSH13", 13, 0, 1, false, Tier::VeryLow } }, - { Instruction::PUSH14, { "PUSH14", 14, 0, 1, false, Tier::VeryLow } }, - { Instruction::PUSH15, { "PUSH15", 15, 0, 1, false, Tier::VeryLow } }, - { Instruction::PUSH16, { "PUSH16", 16, 0, 1, false, Tier::VeryLow } }, - { Instruction::PUSH17, { "PUSH17", 17, 0, 1, false, Tier::VeryLow } }, - { Instruction::PUSH18, { "PUSH18", 18, 0, 1, false, Tier::VeryLow } }, - { Instruction::PUSH19, { "PUSH19", 19, 0, 1, false, Tier::VeryLow } }, - { Instruction::PUSH20, { "PUSH20", 20, 0, 1, false, Tier::VeryLow } }, - { Instruction::PUSH21, { "PUSH21", 21, 0, 1, false, Tier::VeryLow } }, - { Instruction::PUSH22, { "PUSH22", 22, 0, 1, false, Tier::VeryLow } }, - { Instruction::PUSH23, { "PUSH23", 23, 0, 1, false, Tier::VeryLow } }, - { Instruction::PUSH24, { "PUSH24", 24, 0, 1, false, Tier::VeryLow } }, - { Instruction::PUSH25, { "PUSH25", 25, 0, 1, false, Tier::VeryLow } }, - { Instruction::PUSH26, { "PUSH26", 26, 0, 1, false, Tier::VeryLow } }, - { Instruction::PUSH27, { "PUSH27", 27, 0, 1, false, Tier::VeryLow } }, - { Instruction::PUSH28, { "PUSH28", 28, 0, 1, false, Tier::VeryLow } }, - { Instruction::PUSH29, { "PUSH29", 29, 0, 1, false, Tier::VeryLow } }, - { Instruction::PUSH30, { "PUSH30", 30, 0, 1, false, Tier::VeryLow } }, - { Instruction::PUSH31, { "PUSH31", 31, 0, 1, false, Tier::VeryLow } }, - { Instruction::PUSH32, { "PUSH32", 32, 0, 1, false, Tier::VeryLow } }, - { Instruction::DUP1, { "DUP1", 0, 1, 2, false, Tier::VeryLow } }, - { Instruction::DUP2, { "DUP2", 0, 2, 3, false, Tier::VeryLow } }, - { Instruction::DUP3, { "DUP3", 0, 3, 4, false, Tier::VeryLow } }, - { Instruction::DUP4, { "DUP4", 0, 4, 5, false, Tier::VeryLow } }, - { Instruction::DUP5, { "DUP5", 0, 5, 6, false, Tier::VeryLow } }, - { Instruction::DUP6, { "DUP6", 0, 6, 7, false, Tier::VeryLow } }, - { Instruction::DUP7, { "DUP7", 0, 7, 8, false, Tier::VeryLow } }, - { Instruction::DUP8, { "DUP8", 0, 8, 9, false, Tier::VeryLow } }, - { Instruction::DUP9, { "DUP9", 0, 9, 10, false, Tier::VeryLow } }, - { Instruction::DUP10, { "DUP10", 0, 10, 11, false, Tier::VeryLow } }, - { Instruction::DUP11, { "DUP11", 0, 11, 12, false, Tier::VeryLow } }, - { Instruction::DUP12, { "DUP12", 0, 12, 13, false, Tier::VeryLow } }, - { Instruction::DUP13, { "DUP13", 0, 13, 14, false, Tier::VeryLow } }, - { Instruction::DUP14, { "DUP14", 0, 14, 15, false, Tier::VeryLow } }, - { Instruction::DUP15, { "DUP15", 0, 15, 16, false, Tier::VeryLow } }, - { Instruction::DUP16, { "DUP16", 0, 16, 17, false, Tier::VeryLow } }, - { Instruction::SWAP1, { "SWAP1", 0, 2, 2, false, Tier::VeryLow } }, - { Instruction::SWAP2, { "SWAP2", 0, 3, 3, false, Tier::VeryLow } }, - { Instruction::SWAP3, { "SWAP3", 0, 4, 4, false, Tier::VeryLow } }, - { Instruction::SWAP4, { "SWAP4", 0, 5, 5, false, Tier::VeryLow } }, - { Instruction::SWAP5, { "SWAP5", 0, 6, 6, false, Tier::VeryLow } }, - { Instruction::SWAP6, { "SWAP6", 0, 7, 7, false, Tier::VeryLow } }, - { Instruction::SWAP7, { "SWAP7", 0, 8, 8, false, Tier::VeryLow } }, - { Instruction::SWAP8, { "SWAP8", 0, 9, 9, false, Tier::VeryLow } }, - { Instruction::SWAP9, { "SWAP9", 0, 10, 10, false, Tier::VeryLow } }, - { Instruction::SWAP10, { "SWAP10", 0, 11, 11, false, Tier::VeryLow } }, - { Instruction::SWAP11, { "SWAP11", 0, 12, 12, false, Tier::VeryLow } }, - { Instruction::SWAP12, { "SWAP12", 0, 13, 13, false, Tier::VeryLow } }, - { Instruction::SWAP13, { "SWAP13", 0, 14, 14, false, Tier::VeryLow } }, - { Instruction::SWAP14, { "SWAP14", 0, 15, 15, false, Tier::VeryLow } }, - { Instruction::SWAP15, { "SWAP15", 0, 16, 16, false, Tier::VeryLow } }, - { Instruction::SWAP16, { "SWAP16", 0, 17, 17, false, Tier::VeryLow } }, - { Instruction::LOG0, { "LOG0", 0, 2, 0, true, Tier::Special } }, - { Instruction::LOG1, { "LOG1", 0, 3, 0, true, Tier::Special } }, - { Instruction::LOG2, { "LOG2", 0, 4, 0, true, Tier::Special } }, - { Instruction::LOG3, { "LOG3", 0, 5, 0, true, Tier::Special } }, - { Instruction::LOG4, { "LOG4", 0, 6, 0, true, Tier::Special } }, - { Instruction::CREATE, { "CREATE", 0, 3, 1, true, Tier::Special } }, - { Instruction::CALL, { "CALL", 0, 7, 1, true, Tier::Special } }, - { Instruction::CALLCODE, { "CALLCODE", 0, 7, 1, true, Tier::Special } }, - { Instruction::RETURN, { "RETURN", 0, 2, 0, true, Tier::Zero } }, - { Instruction::DELEGATECALL, { "DELEGATECALL", 0, 6, 1, true, Tier::Special } }, - { Instruction::STATICCALL, { "STATICCALL", 0, 6, 1, true, Tier::Special } }, - { Instruction::CREATE2, { "CREATE2", 0, 4, 1, true, Tier::Special } }, - { Instruction::REVERT, { "REVERT", 0, 2, 0, true, Tier::Zero } }, - { Instruction::INVALID, { "INVALID", 0, 0, 0, true, Tier::Zero } }, - { Instruction::SELFDESTRUCT, { "SELFDESTRUCT", 0, 1, 0, true, Tier::Special } } -}; +static std::array(InternalInstruction::MAX_INTERNAL_INSTRUCTION)> const c_instructionInfo = +{{ // Add, Args, Ret, SideEffects, GasPriceTier + { "STOP", InstructionOpCode::STOP, 0, 0, 0, true, Tier::Zero }, + { "ADD", InstructionOpCode::ADD, 0, 2, 1, false, Tier::VeryLow }, + { "SUB", InstructionOpCode::SUB, 0, 2, 1, false, Tier::VeryLow }, + { "MUL", InstructionOpCode::MUL, 0, 2, 1, false, Tier::Low }, + { "DIV", InstructionOpCode::DIV, 0, 2, 1, false, Tier::Low }, + { "SDIV", InstructionOpCode::SDIV, 0, 2, 1, false, Tier::Low }, + { "MOD", InstructionOpCode::MOD, 0, 2, 1, false, Tier::Low }, + { "SMOD", InstructionOpCode::SMOD, 0, 2, 1, false, Tier::Low }, + { "ADDMOD", InstructionOpCode::ADDMOD, 0, 3, 1, false, Tier::Mid }, + { "MULMOD", InstructionOpCode::MULMOD, 0, 3, 1, false, Tier::Mid }, + { "EXP", InstructionOpCode::EXP, 0, 2, 1, false, Tier::Special }, + { "SIGNEXTEND", InstructionOpCode::SIGNEXTEND, 0, 2, 1, false, Tier::Low }, -InstructionInfo solidity::evmasm::instructionInfo(Instruction _inst) + { "LT", InstructionOpCode::LT, 0, 2, 1, false, Tier::VeryLow }, + { "GT", InstructionOpCode::GT, 0, 2, 1, false, Tier::VeryLow }, + { "SLT", InstructionOpCode::SLT, 0, 2, 1, false, Tier::VeryLow }, + { "SGT", InstructionOpCode::SGT, 0, 2, 1, false, Tier::VeryLow }, + { "EQ", InstructionOpCode::EQ, 0, 2, 1, false, Tier::VeryLow }, + { "ISZERO", InstructionOpCode::ISZERO, 0, 1, 1, false, Tier::VeryLow }, + { "AND", InstructionOpCode::AND, 0, 2, 1, false, Tier::VeryLow }, + { "OR", InstructionOpCode::OR, 0, 2, 1, false, Tier::VeryLow }, + { "XOR", InstructionOpCode::XOR, 0, 2, 1, false, Tier::VeryLow }, + { "NOT", InstructionOpCode::NOT, 0, 1, 1, false, Tier::VeryLow }, + { "BYTE", InstructionOpCode::BYTE, 0, 2, 1, false, Tier::VeryLow }, + { "SHL", InstructionOpCode::SHL, 0, 2, 1, false, Tier::VeryLow }, + { "SHR", InstructionOpCode::SHR, 0, 2, 1, false, Tier::VeryLow }, + { "SAR", InstructionOpCode::SAR, 0, 2, 1, false, Tier::VeryLow }, + + { "KECCAK256", InstructionOpCode::KECCAK256, 0, 2, 1, true, Tier::Special }, + + { "ADDRESS", InstructionOpCode::ADDRESS, 0, 0, 1, false, Tier::Base }, + { "BALANCE", InstructionOpCode::BALANCE, 0, 1, 1, false, Tier::Balance }, + { "ORIGIN", InstructionOpCode::ORIGIN, 0, 0, 1, false, Tier::Base }, + { "CALLER", InstructionOpCode::CALLER, 0, 0, 1, false, Tier::Base }, + { "CALLVALUE", InstructionOpCode::CALLVALUE, 0, 0, 1, false, Tier::Base }, + { "CALLDATALOAD", InstructionOpCode::CALLDATALOAD, 0, 1, 1, false, Tier::VeryLow }, + { "CALLDATASIZE", InstructionOpCode::CALLDATASIZE, 0, 0, 1, false, Tier::Base }, + { "CALLDATACOPY", InstructionOpCode::CALLDATACOPY, 0, 3, 0, true, Tier::VeryLow }, + { "CODESIZE", InstructionOpCode::CODESIZE, 0, 0, 1, false, Tier::Base }, + { "CODECOPY", InstructionOpCode::CODECOPY, 0, 3, 0, true, Tier::VeryLow }, + { "GASPRICE", InstructionOpCode::GASPRICE, 0, 0, 1, false, Tier::Base }, + { "EXTCODESIZE", InstructionOpCode::EXTCODESIZE, 0, 1, 1, false, Tier::ExtCode }, + { "EXTCODECOPY", InstructionOpCode::EXTCODECOPY, 0, 4, 0, true, Tier::ExtCode }, + { "RETURNDATASIZE", InstructionOpCode::RETURNDATASIZE, 0, 0, 1, false, Tier::Base }, + { "RETURNDATACOPY", InstructionOpCode::RETURNDATACOPY, 0, 3, 0, true, Tier::VeryLow }, + { "EXTCODEHASH", InstructionOpCode::EXTCODEHASH, 0, 1, 1, false, Tier::Balance }, + + { "BLOCKHASH", InstructionOpCode::BLOCKHASH, 0, 1, 1, false, Tier::Ext }, + { "COINBASE", InstructionOpCode::COINBASE, 0, 0, 1, false, Tier::Base }, + { "TIMESTAMP", InstructionOpCode::TIMESTAMP, 0, 0, 1, false, Tier::Base }, + { "NUMBER", InstructionOpCode::NUMBER, 0, 0, 1, false, Tier::Base }, + { "DIFFICULTY", InstructionOpCode::DIFFICULTY, 0, 0, 1, false, Tier::Base }, + { "PREVRANDAO", InstructionOpCode::PREVRANDAO, 0, 0, 1, false, Tier::Base }, + { "GASLIMIT", InstructionOpCode::GASLIMIT, 0, 0, 1, false, Tier::Base }, + { "CHAINID", InstructionOpCode::CHAINID, 0, 0, 1, false, Tier::Base }, + { "SELFBALANCE", InstructionOpCode::SELFBALANCE, 0, 0, 1, false, Tier::Low }, + { "BASEFEE", InstructionOpCode::BASEFEE, 0, 0, 1, false, Tier::Base }, + + { "POP", InstructionOpCode::POP, 0, 1, 0, false, Tier::Base }, + { "MLOAD", InstructionOpCode::MLOAD, 0, 1, 1, true, Tier::VeryLow }, + { "MSTORE", InstructionOpCode::MSTORE, 0, 2, 0, true, Tier::VeryLow }, + { "MSTORE8", InstructionOpCode::MSTORE8, 0, 2, 0, true, Tier::VeryLow }, + { "SLOAD", InstructionOpCode::SLOAD, 0, 1, 1, false, Tier::Special }, + { "SSTORE", InstructionOpCode::SSTORE, 0, 2, 0, true, Tier::Special }, + { "JUMP", InstructionOpCode::JUMP, 0, 1, 0, true, Tier::Mid }, + { "JUMPI", InstructionOpCode::JUMPI, 0, 2, 0, true, Tier::High }, + { "PC", InstructionOpCode::PC, 0, 0, 1, false, Tier::Base }, + { "MSIZE", InstructionOpCode::MSIZE, 0, 0, 1, false, Tier::Base }, + { "GAS", InstructionOpCode::GAS, 0, 0, 1, false, Tier::Base }, + { "JUMPDEST", InstructionOpCode::JUMPDEST, 0, 0, 0, true, Tier::Special }, + + { "PUSH1", InstructionOpCode::PUSH1, 1, 0, 1, false, Tier::VeryLow }, + { "PUSH2", InstructionOpCode::PUSH2, 2, 0, 1, false, Tier::VeryLow }, + { "PUSH3", InstructionOpCode::PUSH3, 3, 0, 1, false, Tier::VeryLow }, + { "PUSH4", InstructionOpCode::PUSH4, 4, 0, 1, false, Tier::VeryLow }, + { "PUSH5", InstructionOpCode::PUSH5, 5, 0, 1, false, Tier::VeryLow }, + { "PUSH6", InstructionOpCode::PUSH6, 6, 0, 1, false, Tier::VeryLow }, + { "PUSH7", InstructionOpCode::PUSH7, 7, 0, 1, false, Tier::VeryLow }, + { "PUSH8", InstructionOpCode::PUSH8, 8, 0, 1, false, Tier::VeryLow }, + { "PUSH9", InstructionOpCode::PUSH9, 9, 0, 1, false, Tier::VeryLow }, + { "PUSH10", InstructionOpCode::PUSH10, 10, 0, 1, false, Tier::VeryLow }, + { "PUSH11", InstructionOpCode::PUSH11, 11, 0, 1, false, Tier::VeryLow }, + { "PUSH12", InstructionOpCode::PUSH12, 12, 0, 1, false, Tier::VeryLow }, + { "PUSH13", InstructionOpCode::PUSH13, 13, 0, 1, false, Tier::VeryLow }, + { "PUSH14", InstructionOpCode::PUSH14, 14, 0, 1, false, Tier::VeryLow }, + { "PUSH15", InstructionOpCode::PUSH15, 15, 0, 1, false, Tier::VeryLow }, + { "PUSH16", InstructionOpCode::PUSH16, 16, 0, 1, false, Tier::VeryLow }, + { "PUSH17", InstructionOpCode::PUSH17, 17, 0, 1, false, Tier::VeryLow }, + { "PUSH18", InstructionOpCode::PUSH18, 18, 0, 1, false, Tier::VeryLow }, + { "PUSH19", InstructionOpCode::PUSH19, 19, 0, 1, false, Tier::VeryLow }, + { "PUSH20", InstructionOpCode::PUSH20, 20, 0, 1, false, Tier::VeryLow }, + { "PUSH21", InstructionOpCode::PUSH21, 21, 0, 1, false, Tier::VeryLow }, + { "PUSH22", InstructionOpCode::PUSH22, 22, 0, 1, false, Tier::VeryLow }, + { "PUSH23", InstructionOpCode::PUSH23, 23, 0, 1, false, Tier::VeryLow }, + { "PUSH24", InstructionOpCode::PUSH24, 24, 0, 1, false, Tier::VeryLow }, + { "PUSH25", InstructionOpCode::PUSH25, 25, 0, 1, false, Tier::VeryLow }, + { "PUSH26", InstructionOpCode::PUSH26, 26, 0, 1, false, Tier::VeryLow }, + { "PUSH27", InstructionOpCode::PUSH27, 27, 0, 1, false, Tier::VeryLow }, + { "PUSH28", InstructionOpCode::PUSH28, 28, 0, 1, false, Tier::VeryLow }, + { "PUSH29", InstructionOpCode::PUSH29, 29, 0, 1, false, Tier::VeryLow }, + { "PUSH30", InstructionOpCode::PUSH30, 30, 0, 1, false, Tier::VeryLow }, + { "PUSH31", InstructionOpCode::PUSH31, 31, 0, 1, false, Tier::VeryLow }, + { "PUSH32", InstructionOpCode::PUSH32, 32, 0, 1, false, Tier::VeryLow }, + + { "DUP1", InstructionOpCode::DUP1, 0, 1, 2, false, Tier::VeryLow }, + { "DUP2", InstructionOpCode::DUP2, 0, 2, 3, false, Tier::VeryLow }, + { "DUP3", InstructionOpCode::DUP3, 0, 3, 4, false, Tier::VeryLow }, + { "DUP4", InstructionOpCode::DUP4, 0, 4, 5, false, Tier::VeryLow }, + { "DUP5", InstructionOpCode::DUP5, 0, 5, 6, false, Tier::VeryLow }, + { "DUP6", InstructionOpCode::DUP6, 0, 6, 7, false, Tier::VeryLow }, + { "DUP7", InstructionOpCode::DUP7, 0, 7, 8, false, Tier::VeryLow }, + { "DUP8", InstructionOpCode::DUP8, 0, 8, 9, false, Tier::VeryLow }, + { "DUP9", InstructionOpCode::DUP9, 0, 9, 10, false, Tier::VeryLow }, + { "DUP10", InstructionOpCode::DUP10, 0, 10, 11, false, Tier::VeryLow }, + { "DUP11", InstructionOpCode::DUP11, 0, 11, 12, false, Tier::VeryLow }, + { "DUP12", InstructionOpCode::DUP12, 0, 12, 13, false, Tier::VeryLow }, + { "DUP13", InstructionOpCode::DUP13, 0, 13, 14, false, Tier::VeryLow }, + { "DUP14", InstructionOpCode::DUP14, 0, 14, 15, false, Tier::VeryLow }, + { "DUP15", InstructionOpCode::DUP15, 0, 15, 16, false, Tier::VeryLow }, + { "DUP16", InstructionOpCode::DUP16, 0, 16, 17, false, Tier::VeryLow }, + + { "SWAP1", InstructionOpCode::SWAP1, 0, 2, 2, false, Tier::VeryLow }, + { "SWAP2", InstructionOpCode::SWAP2, 0, 3, 3, false, Tier::VeryLow }, + { "SWAP3", InstructionOpCode::SWAP3, 0, 4, 4, false, Tier::VeryLow }, + { "SWAP4", InstructionOpCode::SWAP4, 0, 5, 5, false, Tier::VeryLow }, + { "SWAP5", InstructionOpCode::SWAP5, 0, 6, 6, false, Tier::VeryLow }, + { "SWAP6", InstructionOpCode::SWAP6, 0, 7, 7, false, Tier::VeryLow }, + { "SWAP7", InstructionOpCode::SWAP7, 0, 8, 8, false, Tier::VeryLow }, + { "SWAP8", InstructionOpCode::SWAP8, 0, 9, 9, false, Tier::VeryLow }, + { "SWAP9", InstructionOpCode::SWAP9, 0, 10, 10, false, Tier::VeryLow }, + { "SWAP10", InstructionOpCode::SWAP10, 0, 11, 11, false, Tier::VeryLow }, + { "SWAP11", InstructionOpCode::SWAP11, 0, 12, 12, false, Tier::VeryLow }, + { "SWAP12", InstructionOpCode::SWAP12, 0, 13, 13, false, Tier::VeryLow }, + { "SWAP13", InstructionOpCode::SWAP13, 0, 14, 14, false, Tier::VeryLow }, + { "SWAP14", InstructionOpCode::SWAP14, 0, 15, 15, false, Tier::VeryLow }, + { "SWAP15", InstructionOpCode::SWAP15, 0, 16, 16, false, Tier::VeryLow }, + { "SWAP16", InstructionOpCode::SWAP16, 0, 17, 17, false, Tier::VeryLow }, + + { "LOG0", InstructionOpCode::LOG0, 0, 2, 0, true, Tier::Special }, + { "LOG1", InstructionOpCode::LOG1, 0, 3, 0, true, Tier::Special }, + { "LOG2", InstructionOpCode::LOG2, 0, 4, 0, true, Tier::Special }, + { "LOG3", InstructionOpCode::LOG3, 0, 5, 0, true, Tier::Special }, + { "LOG4", InstructionOpCode::LOG4, 0, 6, 0, true, Tier::Special }, + + { "CREATE", InstructionOpCode::CREATE, 0, 3, 1, true, Tier::Special }, + { "CALL", InstructionOpCode::CALL, 0, 7, 1, true, Tier::Special }, + { "CALLCODE", InstructionOpCode::CALLCODE, 0, 7, 1, true, Tier::Special }, + { "RETURN", InstructionOpCode::RETURN, 0, 2, 0, true, Tier::Zero }, + { "DELEGATECALL", InstructionOpCode::DELEGATECALL, 0, 6, 1, true, Tier::Special }, + { "CREATE2", InstructionOpCode::CREATE2, 0, 4, 1, true, Tier::Special }, + { "STATICCALL", InstructionOpCode::STATICCALL, 0, 6, 1, true, Tier::Special }, + { "REVERT", InstructionOpCode::REVERT, 0, 2, 0, true, Tier::Zero }, + { "INVALID", InstructionOpCode::INVALID, 0, 0, 0, true, Tier::Zero }, + { "SELFDESTRUCT", InstructionOpCode::SELFDESTRUCT, 0, 1, 0, true, Tier::Special } +}}; + +InstructionInfo const& solidity::evmasm::instructionInfo(InternalInstruction _inst) { try { - return c_instructionInfo.at(_inst); + return c_instructionInfo.at(static_cast(_inst)); } catch (...) { - return InstructionInfo({"(_inst)) + ">", 0, 0, 0, false, Tier::Invalid}); + static InstructionInfo invalidInstruction = InstructionInfo({ + "(_inst)) + ">", + InstructionOpCode::INVALID, + 0, + 0, + 0, + false, + Tier::Invalid + }); + return invalidInstruction; } } -bool solidity::evmasm::isValidInstruction(Instruction _inst) +/// Get the Opcode of an instruction. +InstructionOpCode solidity::evmasm::instructionOpcode(InternalInstruction _inst) { - return !!c_instructionInfo.count(_inst); + return instructionInfo(_inst).opCode; +} + +InternalInstruction solidity::evmasm::internalInstruction(InstructionOpCode _opcode, langutil::EVMVersion _evmVersion) +{ + // Create cached table if not existing + if (opcode2InternalCache.empty()) + for (size_t i = 0; i < c_instructionInfo.size(); i++) + if (c_instructionInfo[i].opCode == _opcode) + opcode2InternalCache.insert({_opcode, static_cast(i)}); + + SetOnce validInstruction{}; + auto [start, end] = opcode2InternalCache.equal_range(_opcode); + + // Check which instructions work for the given evm version + for (;start != end; start++) + if (_evmVersion.hasInstruction(start->second)) + validInstruction = start->second; + + return *validInstruction; +} + +bool solidity::evmasm::isValidInstruction(InternalInstruction _inst) +{ + return _inst < InternalInstruction::MAX_INTERNAL_INSTRUCTION; } diff --git a/libevmasm/Instruction.h b/libevmasm/Instruction.h index 17ee8b4a7..d6a045d96 100644 --- a/libevmasm/Instruction.h +++ b/libevmasm/Instruction.h @@ -26,11 +26,185 @@ #include #include +#include + +namespace solidity::langutil +{ + +/** + * A version specifier of the EVM we want to compile to. + * Defaults to the latest version deployed on Ethereum Mainnet at the time of compiler release. + */ +class EVMVersion; +} + namespace solidity::evmasm { +/// Internal only enum of all instructions +enum class InternalInstruction: uint8_t +{ + STOP = 0, ///< halts execution + ADD, ///< addition operation + MUL, ///< multiplication operation + SUB, ///< subtraction operation + DIV, ///< integer division operation + SDIV, ///< signed integer division operation + MOD, ///< modulo remainder operation + SMOD, ///< signed modulo remainder operation + ADDMOD, ///< unsigned modular addition + MULMOD, ///< unsigned modular multiplication + EXP, ///< exponential operation + SIGNEXTEND, ///< extend length of signed integer + + LT, ///< less-than comparison + GT, ///< greater-than comparison + SLT, ///< signed less-than comparison + SGT, ///< signed greater-than comparison + EQ, ///< equality comparison + ISZERO, ///< simple not operator + AND, ///< bitwise AND operation + OR, ///< bitwise OR operation + XOR, ///< bitwise XOR operation + NOT, ///< bitwise NOT operation + BYTE, ///< retrieve single byte from word + SHL, ///< bitwise SHL operation + SHR, ///< bitwise SHR operation + SAR, ///< bitwise SAR operation + + KECCAK256, ///< compute KECCAK-256 hash + + ADDRESS, ///< get address of currently executing account + BALANCE, ///< get balance of the given account + ORIGIN, ///< get execution origination address + CALLER, ///< get caller address + CALLVALUE, ///< get deposited value by the instruction/transaction responsible for this execution + CALLDATALOAD, ///< get input data of current environment + CALLDATASIZE, ///< get size of input data in current environment + CALLDATACOPY, ///< copy input data in current environment to memory + CODESIZE, ///< get size of code running in current environment + CODECOPY, ///< copy code running in current environment to memory + GASPRICE, ///< get price of gas in current environment + EXTCODESIZE, ///< get external code size (from another contract) + EXTCODECOPY, ///< copy external code (from another contract) + RETURNDATASIZE, ///< get size of return data buffer + RETURNDATACOPY, ///< copy return data in current environment to memory + EXTCODEHASH, ///< get external code hash (from another contract) + + BLOCKHASH, ///< get hash of most recent complete block + COINBASE, ///< get the block's coinbase address + TIMESTAMP, ///< get the block's timestamp + NUMBER, ///< get the block's number + DIFFICULTY, ///< get the block's difficulty + PREVRANDAO, ///< get randomness provided by the beacon chain + GASLIMIT, ///< get the block's gas limit + CHAINID, ///< get the config's chainid param + SELFBALANCE, ///< get balance of the current account + BASEFEE, ///< get the block's basefee + + POP, ///< remove item from stack + MLOAD, ///< load word from memory + MSTORE, ///< save word to memory + MSTORE8, ///< save byte to memory + SLOAD, ///< load word from storage + SSTORE, ///< save word to storage + JUMP, ///< alter the program counter + JUMPI, ///< conditionally alter the program counter + PC, ///< get the program counter + MSIZE, ///< get the size of active memory + GAS, ///< get the amount of available gas + JUMPDEST, ///< set a potential jump destination + + PUSH1, ///< place 1 byte item on stack + PUSH2, ///< place 2 byte item on stack + PUSH3, ///< place 3 byte item on stack + PUSH4, ///< place 4 byte item on stack + PUSH5, ///< place 5 byte item on stack + PUSH6, ///< place 6 byte item on stack + PUSH7, ///< place 7 byte item on stack + PUSH8, ///< place 8 byte item on stack + PUSH9, ///< place 9 byte item on stack + PUSH10, ///< place 10 byte item on stack + PUSH11, ///< place 11 byte item on stack + PUSH12, ///< place 12 byte item on stack + PUSH13, ///< place 13 byte item on stack + PUSH14, ///< place 14 byte item on stack + PUSH15, ///< place 15 byte item on stack + PUSH16, ///< place 16 byte item on stack + PUSH17, ///< place 17 byte item on stack + PUSH18, ///< place 18 byte item on stack + PUSH19, ///< place 19 byte item on stack + PUSH20, ///< place 20 byte item on stack + PUSH21, ///< place 21 byte item on stack + PUSH22, ///< place 22 byte item on stack + PUSH23, ///< place 23 byte item on stack + PUSH24, ///< place 24 byte item on stack + PUSH25, ///< place 25 byte item on stack + PUSH26, ///< place 26 byte item on stack + PUSH27, ///< place 27 byte item on stack + PUSH28, ///< place 28 byte item on stack + PUSH29, ///< place 29 byte item on stack + PUSH30, ///< place 30 byte item on stack + PUSH31, ///< place 31 byte item on stack + PUSH32, ///< place 32 byte item on stack + + DUP1, ///< copies the highest item in the stack to the top of the stack + DUP2, ///< copies the second highest item in the stack to the top of the stack + DUP3, ///< copies the third highest item in the stack to the top of the stack + DUP4, ///< copies the 4th highest item in the stack to the top of the stack + DUP5, ///< copies the 5th highest item in the stack to the top of the stack + DUP6, ///< copies the 6th highest item in the stack to the top of the stack + DUP7, ///< copies the 7th highest item in the stack to the top of the stack + DUP8, ///< copies the 8th highest item in the stack to the top of the stack + DUP9, ///< copies the 9th highest item in the stack to the top of the stack + DUP10, ///< copies the 10th highest item in the stack to the top of the stack + DUP11, ///< copies the 11th highest item in the stack to the top of the stack + DUP12, ///< copies the 12th highest item in the stack to the top of the stack + DUP13, ///< copies the 13th highest item in the stack to the top of the stack + DUP14, ///< copies the 14th highest item in the stack to the top of the stack + DUP15, ///< copies the 15th highest item in the stack to the top of the stack + DUP16, ///< copies the 16th highest item in the stack to the top of the stack + + SWAP1, ///< swaps the highest and second highest value on the stack + SWAP2, ///< swaps the highest and third highest value on the stack + SWAP3, ///< swaps the highest and 4th highest value on the stack + SWAP4, ///< swaps the highest and 5th highest value on the stack + SWAP5, ///< swaps the highest and 6th highest value on the stack + SWAP6, ///< swaps the highest and 7th highest value on the stack + SWAP7, ///< swaps the highest and 8th highest value on the stack + SWAP8, ///< swaps the highest and 9th highest value on the stack + SWAP9, ///< swaps the highest and 10th highest value on the stack + SWAP10, ///< swaps the highest and 11th highest value on the stack + SWAP11, ///< swaps the highest and 12th highest value on the stack + SWAP12, ///< swaps the highest and 13th highest value on the stack + SWAP13, ///< swaps the highest and 14th highest value on the stack + SWAP14, ///< swaps the highest and 15th highest value on the stack + SWAP15, ///< swaps the highest and 16th highest value on the stack + SWAP16, ///< swaps the highest and 17th highest value on the stack + + LOG0, ///< Makes a log entry; no topics. + LOG1, ///< Makes a log entry; 1 topic. + LOG2, ///< Makes a log entry; 2 topics. + LOG3, ///< Makes a log entry; 3 topics. + LOG4, ///< Makes a log entry; 4 topics. + + CREATE, ///< create a new account with associated code + CALL, ///< message-call into an account + CALLCODE, ///< message-call with another account's code only + RETURN, ///< halt execution returning output data + DELEGATECALL, ///< like CALLCODE but keeps caller's value and sender + CREATE2, ///< create new account with associated code at address `sha3(0xff + sender + salt + init code) % 2**160` + STATICCALL, ///< like CALL but disallow state modifications + + REVERT, ///< halt execution, revert state and return output data + INVALID, ///< invalid instruction for expressing runtime errors (e.g., division-by-zero) + SELFDESTRUCT, ///< halt execution and register account for later deletion + /// + MAX_INTERNAL_INSTRUCTION ///< Marker for largest internal instruction value +}; + /// Virtual machine bytecode instruction. -enum class Instruction: uint8_t +enum class InstructionOpCode: uint8_t { STOP = 0x00, ///< halts execution ADD, ///< addition operation @@ -189,82 +363,6 @@ enum class Instruction: uint8_t SELFDESTRUCT = 0xff ///< halt execution and register account for later deletion }; -/// @returns true if the instruction is a PUSH -inline bool isPushInstruction(Instruction _inst) -{ - return Instruction::PUSH1 <= _inst && _inst <= Instruction::PUSH32; -} - -/// @returns true if the instruction is a DUP -inline bool isDupInstruction(Instruction _inst) -{ - return Instruction::DUP1 <= _inst && _inst <= Instruction::DUP16; -} - -/// @returns true if the instruction is a SWAP -inline bool isSwapInstruction(Instruction _inst) -{ - return Instruction::SWAP1 <= _inst && _inst <= Instruction::SWAP16; -} - -/// @returns true if the instruction is a LOG -inline bool isLogInstruction(Instruction _inst) -{ - return Instruction::LOG0 <= _inst && _inst <= Instruction::LOG4; -} - -/// @returns the number of PUSH Instruction _inst -inline unsigned getPushNumber(Instruction _inst) -{ - return static_cast(_inst) - unsigned(Instruction::PUSH1) + 1; -} - -/// @returns the number of DUP Instruction _inst -inline unsigned getDupNumber(Instruction _inst) -{ - return static_cast(_inst) - unsigned(Instruction::DUP1) + 1; -} - -/// @returns the number of SWAP Instruction _inst -inline unsigned getSwapNumber(Instruction _inst) -{ - return static_cast(_inst) - unsigned(Instruction::SWAP1) + 1; -} - -/// @returns the number of LOG Instruction _inst -inline unsigned getLogNumber(Instruction _inst) -{ - return static_cast(_inst) - unsigned(Instruction::LOG0); -} - -/// @returns the PUSH<_number> instruction -inline Instruction pushInstruction(unsigned _number) -{ - assertThrow(1 <= _number && _number <= 32, InvalidOpcode, std::string("Invalid PUSH instruction requested (") + std::to_string(_number) + ")."); - return Instruction(unsigned(Instruction::PUSH1) + _number - 1); -} - -/// @returns the DUP<_number> instruction -inline Instruction dupInstruction(unsigned _number) -{ - assertThrow(1 <= _number && _number <= 16, InvalidOpcode, std::string("Invalid DUP instruction requested (") + std::to_string(_number) + ")."); - return Instruction(unsigned(Instruction::DUP1) + _number - 1); -} - -/// @returns the SWAP<_number> instruction -inline Instruction swapInstruction(unsigned _number) -{ - assertThrow(1 <= _number && _number <= 16, InvalidOpcode, std::string("Invalid SWAP instruction requested (") + std::to_string(_number) + ")."); - return Instruction(unsigned(Instruction::SWAP1) + _number - 1); -} - -/// @returns the LOG<_number> instruction -inline Instruction logInstruction(unsigned _number) -{ - assertThrow(_number <= 4, InvalidOpcode, std::string("Invalid LOG instruction requested (") + std::to_string(_number) + ")."); - return Instruction(unsigned(Instruction::LOG0) + _number); -} - enum class Tier { Zero = 0, // 0, Zero @@ -284,6 +382,7 @@ enum class Tier struct InstructionInfo { std::string name; ///< The name of the instruction. + InstructionOpCode opCode; ///< InternalInstruction OpCode int additional; ///< Additional items required in memory for this instructions (only for PUSH). int args; ///< Number of items required on the stack for this instruction (and, for the purposes of ret, the number taken from the stack). int ret; ///< Number of items placed (back) on the stack by this instruction, assuming args items were removed. @@ -292,12 +391,98 @@ struct InstructionInfo }; /// Information on all the instructions. -InstructionInfo instructionInfo(Instruction _inst); +InstructionInfo const& instructionInfo(InternalInstruction _inst); + +/// Get the Opcode of an internal instruction. +InstructionOpCode instructionOpcode(InternalInstruction _inst); + + + +/// Get internal instruction for a given opcode +InternalInstruction internalInstruction(InstructionOpCode _opcode, langutil::EVMVersion _evmVersion); /// check whether instructions exists. -bool isValidInstruction(Instruction _inst); +bool isValidInstruction(InternalInstruction _inst); + +/// Convert from string mnemonic to InternalInstruction type. +extern const std::map c_instructions; + + +/// @returns true if the instruction is a PUSH +inline bool isPushInstruction(InternalInstruction _inst) +{ + return InstructionOpCode::PUSH1 <= instructionOpcode(_inst) && instructionOpcode(_inst) <= InstructionOpCode::PUSH32; +} + +/// @returns true if the instruction is a DUP +inline bool isDupInstruction(InternalInstruction _inst) +{ + return InstructionOpCode::DUP1 <= instructionOpcode(_inst) && instructionOpcode(_inst) <= InstructionOpCode::DUP16; +} + +/// @returns true if the instruction is a SWAP +inline bool isSwapInstruction(InternalInstruction _inst) +{ + return InstructionOpCode::SWAP1 <= instructionOpcode(_inst) && instructionOpcode(_inst) <= InstructionOpCode::SWAP16; +} + +/// @returns true if the instruction is a LOG +inline bool isLogInstruction(InternalInstruction _inst) +{ + return InstructionOpCode::LOG0 <= instructionOpcode(_inst) && instructionOpcode(_inst) <= InstructionOpCode::LOG4; +} + +/// @returns the number of PUSH InternalInstruction _inst +inline unsigned getPushNumber(InternalInstruction _inst) +{ + return static_cast(_inst) - unsigned(InstructionOpCode::PUSH1) + 1; +} + +/// @returns the number of DUP InternalInstruction _inst +inline unsigned getDupNumber(InternalInstruction _inst) +{ + return static_cast(_inst) - unsigned(InstructionOpCode::DUP1) + 1; +} + +/// @returns the number of SWAP InternalInstruction _inst +inline unsigned getSwapNumber(InternalInstruction _inst) +{ + return static_cast(_inst) - unsigned(InstructionOpCode::SWAP1) + 1; +} + +/// @returns the number of LOG InternalInstruction _inst +inline unsigned getLogNumber(InternalInstruction _inst) +{ + return static_cast(_inst) - unsigned(InstructionOpCode::LOG0); +} + +/// @returns the PUSH<_number> instruction +inline InternalInstruction pushInstruction(unsigned _number) +{ + assertThrow(1 <= _number && _number <= 32, InvalidOpcode, std::string("Invalid PUSH instruction requested (") + std::to_string(_number) + ")."); + return InternalInstruction(unsigned(InternalInstruction::PUSH1) + _number - 1); +} + +/// @returns the DUP<_number> instruction +inline InternalInstruction dupInstruction(unsigned _number) +{ + assertThrow(1 <= _number && _number <= 16, InvalidOpcode, std::string("Invalid DUP instruction requested (") + std::to_string(_number) + ")."); + return InternalInstruction(unsigned(InternalInstruction::DUP1) + _number - 1); +} + +/// @returns the SWAP<_number> instruction +inline InternalInstruction swapInstruction(unsigned _number) +{ + assertThrow(1 <= _number && _number <= 16, InvalidOpcode, std::string("Invalid SWAP instruction requested (") + std::to_string(_number) + ")."); + return InternalInstruction(unsigned(InternalInstruction::SWAP1) + _number - 1); +} + +/// @returns the LOG<_number> instruction +inline InternalInstruction logInstruction(unsigned _number) +{ + assertThrow(_number <= 4, InvalidOpcode, std::string("Invalid LOG instruction requested (") + std::to_string(_number) + ")."); + return InternalInstruction(unsigned(InternalInstruction::LOG0) + _number); +} -/// Convert from string mnemonic to Instruction type. -extern const std::map c_instructions; } diff --git a/liblangutil/EVMVersion.cpp b/liblangutil/EVMVersion.cpp index 7b34fe7d2..64da991e8 100644 --- a/liblangutil/EVMVersion.cpp +++ b/liblangutil/EVMVersion.cpp @@ -48,6 +48,10 @@ bool EVMVersion::hasOpcode(Instruction _opcode) const return hasSelfBalance(); case Instruction::BASEFEE: return hasBaseFee(); + case InternalInstruction::DIFFICULTY: + return hasDifficulty(); + case InternalInstruction::PREVRANDAO: + return hasPrevRandao(); default: return true; } diff --git a/liblangutil/EVMVersion.h b/liblangutil/EVMVersion.h index 0fa4b03f3..082d2b45b 100644 --- a/liblangutil/EVMVersion.h +++ b/liblangutil/EVMVersion.h @@ -95,7 +95,7 @@ public: bool hasPrevRandao() const { return *this >= paris(); } bool hasDifficulty() const { return *this < paris(); } - bool hasOpcode(evmasm::Instruction _opcode) const; + bool hasInstruction(evmasm::InternalInstruction _instruction) const; /// Whether we have to retain the costs for the call opcode itself (false), /// or whether we can just forward easily all remaining gas (true). diff --git a/libsolidity/interface/StandardCompiler.cpp b/libsolidity/interface/StandardCompiler.cpp index 15e713d5b..6c6600d89 100644 --- a/libsolidity/interface/StandardCompiler.cpp +++ b/libsolidity/interface/StandardCompiler.cpp @@ -21,6 +21,7 @@ * Standard JSON compiler interface. */ +#include "libevmasm/Instruction.h" #include #include @@ -384,6 +385,7 @@ Json::Value collectEVMObject( evmasm::LinkerObject const& _object, string const* _sourceMap, Json::Value _generatedSources, + EVMVersion _evmVersion, bool _runtimeObject, function const& _artifactRequested ) @@ -392,7 +394,7 @@ Json::Value collectEVMObject( if (_artifactRequested("object")) output["object"] = _object.toHex(); if (_artifactRequested("opcodes")) - output["opcodes"] = evmasm::disassemble(_object.bytecode); + output["opcodes"] = evmasm::disassemble(_object.bytecode, _evmVersion); if (_artifactRequested("sourceMap")) output["sourceMap"] = _sourceMap ? *_sourceMap : ""; if (_artifactRequested("functionDebugData")) @@ -1314,6 +1316,7 @@ Json::Value StandardCompiler::compileSolidity(StandardCompiler::InputsAndSetting compilerStack.object(contractName), compilerStack.sourceMapping(contractName), compilerStack.generatedSources(contractName), + _inputsAndSettings.evmVersion, false, [&](string const& _element) { return isArtifactRequested( _inputsAndSettings.outputSelection, @@ -1335,6 +1338,7 @@ Json::Value StandardCompiler::compileSolidity(StandardCompiler::InputsAndSetting compilerStack.runtimeObject(contractName), compilerStack.runtimeSourceMapping(contractName), compilerStack.generatedSources(contractName, true), + _inputsAndSettings.evmVersion, true, [&](string const& _element) { return isArtifactRequested( _inputsAndSettings.outputSelection, @@ -1476,6 +1480,7 @@ Json::Value StandardCompiler::compileYul(InputsAndSettings _inputsAndSettings) *o.bytecode, o.sourceMappings.get(), Json::arrayValue, + _inputsAndSettings.evmVersion, isDeployed, [&, kind = kind](string const& _element) { return isArtifactRequested( _inputsAndSettings.outputSelection, diff --git a/libyul/AsmAnalysis.cpp b/libyul/AsmAnalysis.cpp index 00fd00ba4..83fb0f5fc 100644 --- a/libyul/AsmAnalysis.cpp +++ b/libyul/AsmAnalysis.cpp @@ -19,6 +19,7 @@ * Analyzer part of inline assembly. */ +#include "libevmasm/Instruction.h" #include #include @@ -308,31 +309,48 @@ vector AsmAnalyzer::operator()(FunctionCall const& _funCall) vector const* returnTypes = nullptr; vector> const* literalArguments = nullptr; - if (BuiltinFunction const* f = m_dialect.builtin(_funCall.functionName.name)) + auto inScope = [&](YulString const& _function) { - parameterTypes = &f->parameters; - returnTypes = &f->returns; - if (!f->literalArguments.empty()) - literalArguments = &f->literalArguments; + return m_currentScope->lookup(_function, GenericVisitor{ + [&](Scope::Variable const&) + { + m_errorReporter.typeError( + 4202_error, + nativeLocationOf(_funCall.functionName), + "Attempt to call variable instead of function." + ); + }, + [&](Scope::Function const& _fun) + { + parameterTypes = &_fun.arguments; + returnTypes = &_fun.returns; + } + }); + }; + + // TODO remove this in 0.9.0. We allow creating functions or identifiers in Yul with the name + // prevrandao for VMs before paris. + auto prevrandaoInScopeException = [&](YulString const& _instrName) -> bool + { + return _instrName.str() == "prevrandao" + && m_evmVersion < langutil::EVMVersion::paris() + && inScope(_instrName); + }; + + YulString const& functionName = _funCall.functionName.name; + + BuiltinFunction const* builtinFunction = m_dialect.builtin(functionName); + if (builtinFunction && !prevrandaoInScopeException(functionName)) + { + parameterTypes = &builtinFunction->parameters; + returnTypes = &builtinFunction->returns; + if (!builtinFunction->literalArguments.empty()) + literalArguments = &builtinFunction->literalArguments; validateInstructions(_funCall); - m_sideEffects += f->sideEffects; + m_sideEffects += builtinFunction->sideEffects; } - else if (m_currentScope->lookup(_funCall.functionName.name, GenericVisitor{ - [&](Scope::Variable const&) - { - m_errorReporter.typeError( - 4202_error, - nativeLocationOf(_funCall.functionName), - "Attempt to call variable instead of function." - ); - }, - [&](Scope::Function const& _fun) - { - parameterTypes = &_fun.arguments; - returnTypes = &_fun.returns; - } - })) + else if (inScope(functionName)) { if (m_resolver) // We found a local reference, make sure there is no external reference. @@ -745,6 +763,19 @@ bool AsmAnalyzer::validateInstructions(evmasm::Instruction _instr, SourceLocatio "PC instruction is a low-level EVM feature. " "Because of that PC is disallowed in strict assembly." ); + else if (_instr == evmasm::InternalInstruction::PREVRANDAO && !m_evmVersion.hasPrevRandao()) + m_errorReporter.warning( + 5761_error, + _location, + "\"prevrandao\" is not supported by the VM version and will be treated like \"difficulty\"." + ); + else if (_instr == evmasm::InternalInstruction::DIFFICULTY && !m_evmVersion.hasDifficulty()) + m_errorReporter.warning( + 3242_error, + _location, + "\"difficulty\" was renamed and supplanted by \"prevrandao\" in the VM version paris." + ); + return returnValue; } diff --git a/libyul/backends/evm/EVMDialect.cpp b/libyul/backends/evm/EVMDialect.cpp index 2927b57d1..f4fc1b44f 100644 --- a/libyul/backends/evm/EVMDialect.cpp +++ b/libyul/backends/evm/EVMDialect.cpp @@ -120,7 +120,7 @@ set createReservedIdentifiers(langutil::EVMVersion _evmVersion) }; // TODO remove this in 0.9.0. We allow creating functions or identifiers in Yul with the name // prevrandao for VMs before paris. - auto prevRandAOException = [&](string const& _instrName) -> bool + auto prevRandaoException = [&](string const& _instrName) -> bool { // Using string comparison as the opcode is the same as for "difficulty" return _instrName == "prevrandao" && _evmVersion < langutil::EVMVersion::paris(); @@ -130,7 +130,7 @@ set createReservedIdentifiers(langutil::EVMVersion _evmVersion) for (auto const& instr: evmasm::c_instructions) { string name = toLower(instr.first); - if (!baseFeeException(instr.second) && !prevRandAOException(name)) + if (!baseFeeException(instr.second) && !prevRandaoException(name)) reserved.emplace(name); } reserved += vector{ @@ -146,22 +146,20 @@ set createReservedIdentifiers(langutil::EVMVersion _evmVersion) map createBuiltins(langutil::EVMVersion _evmVersion, bool _objectAccess) { - /*auto hasOpCode = [&](evmasm::Instruction _instr, string const& _instrName) -> bool - { - if ( - _instr == evmasm::Instruction::PREVRANDAO && - _instrName == "prevrandao" - ) - return _evmVersion.hasPrevRandao(); - - return _evmVersion.hasOpcode(_instr); - };*/ - map builtins; for (auto const& instr: evmasm::c_instructions) { string name = toLower(instr.first); - auto const opcode = instr.second; + auto instruction = instr.second; + + // Replace PREVRANDAO/DIFFICULTY instructions based on availability in evm version, + // but keep the function name the same so both can be used. + // This effectively creates an alias between prevrandao <-> difficulty. + // TODO remove in 0.9.0 + if (instruction == evmasm::InternalInstruction::DIFFICULTY && _evmVersion.hasPrevRandao()) + instruction = evmasm::InternalInstruction::PREVRANDAO; + else if (instruction == evmasm::InternalInstruction::PREVRANDAO && !_evmVersion.hasPrevRandao()) + instruction = evmasm::InternalInstruction::DIFFICULTY; if ( !evmasm::isDupInstruction(opcode) && @@ -170,9 +168,9 @@ map createBuiltins(langutil::EVMVersion _evmVe opcode != evmasm::Instruction::JUMP && opcode != evmasm::Instruction::JUMPI && opcode != evmasm::Instruction::JUMPDEST && - _evmVersion.hasOpcode(opcode) + _evmVersion.hasInstruction(instruction) ) - builtins.emplace(createEVMFunction(name, opcode)); + builtins.emplace(createEVMFunction(name, instruction)); } if (_objectAccess) diff --git a/solc/CommandLineInterface.cpp b/solc/CommandLineInterface.cpp index 0902e13f8..56cf581bd 100644 --- a/solc/CommandLineInterface.cpp +++ b/solc/CommandLineInterface.cpp @@ -189,11 +189,11 @@ void CommandLineInterface::handleOpcode(string const& _contract) solAssert(m_options.input.mode == InputMode::Compiler || m_options.input.mode == InputMode::CompilerWithASTImport, ""); if (!m_options.output.dir.empty()) - createFile(m_compiler->filesystemFriendlyName(_contract) + ".opcode", evmasm::disassemble(m_compiler->object(_contract).bytecode)); + createFile(m_compiler->filesystemFriendlyName(_contract) + ".opcode", evmasm::disassemble(m_compiler->object(_contract).bytecode, m_options.output.evmVersion)); else { sout() << "Opcodes:" << endl; - sout() << std::uppercase << evmasm::disassemble(m_compiler->object(_contract).bytecode); + sout() << std::uppercase << evmasm::disassemble(m_compiler->object(_contract).bytecode, m_options.output.evmVersion); sout() << endl; } } @@ -813,7 +813,7 @@ void CommandLineInterface::handleCombinedJSON() if (m_options.compiler.combinedJsonRequests->binaryRuntime && m_compiler->compilationSuccessful()) contractData[g_strBinaryRuntime] = m_compiler->runtimeObject(contractName).toHex(); if (m_options.compiler.combinedJsonRequests->opcodes && m_compiler->compilationSuccessful()) - contractData[g_strOpcodes] = evmasm::disassemble(m_compiler->object(contractName).bytecode); + contractData[g_strOpcodes] = evmasm::disassemble(m_compiler->object(contractName).bytecode, m_options.output.evmVersion); if (m_options.compiler.combinedJsonRequests->asm_ && m_compiler->compilationSuccessful()) contractData[g_strAsm] = m_compiler->assemblyJSON(contractName); if (m_options.compiler.combinedJsonRequests->storageLayout && m_compiler->compilationSuccessful()) diff --git a/test/libevmasm/Assembler.cpp b/test/libevmasm/Assembler.cpp index 680531799..ef9477bcf 100644 --- a/test/libevmasm/Assembler.cpp +++ b/test/libevmasm/Assembler.cpp @@ -270,13 +270,13 @@ BOOST_AUTO_TEST_CASE(immutables_and_its_source_maps) auto const numberOfMappings = std::count(sourceMappings.begin(), sourceMappings.end(), ';'); LinkerObject const& obj = assembly.assemble(); - string const disassembly = disassemble(obj.bytecode, "\n"); + string const disassembly = disassemble(obj.bytecode, EVMVersion(), "\n"); auto const numberOfOpcodes = std::count(disassembly.begin(), disassembly.end(), '\n'); #if 0 // {{{ debug prints { LinkerObject const& subObj = assembly.sub(0).assemble(); - string const subDisassembly = disassemble(subObj.bytecode, "\n"); + string const subDisassembly = disassemble(subObj.bytecode, EVMVersion(), "\n"); cout << '\n'; cout << "### immutables: " << numImmutables << ", refs: " << numActualRefs << '\n'; cout << " - srcmap: \"" << sourceMappings << "\"\n"; @@ -285,8 +285,8 @@ BOOST_AUTO_TEST_CASE(immutables_and_its_source_maps) cout << " - subs: " << assembly.numSubs() << '\n'; cout << " - sub opcodes " << std::count(subDisassembly.begin(), subDisassembly.end(), '\n') << '\n'; cout << " - sub srcmaps " << AssemblyItem::computeSourceMapping(subAsm->items(), indices) << '\n'; - cout << " - main bytecode:\n\t" << disassemble(obj.bytecode, "\n\t"); - cout << "\r - sub bytecode:\n\t" << disassemble(subObj.bytecode, "\n\t"); + cout << " - main bytecode:\n\t" << disassemble(obj.bytecode, EVMVersion(), "\n\t"); + cout << "\r - sub bytecode:\n\t" << disassemble(subObj.bytecode, EVMVersion(), "\n\t"); } #endif // }}} diff --git a/test/libyul/ObjectCompilerTest.cpp b/test/libyul/ObjectCompilerTest.cpp index dcb7158c2..c0d5addda 100644 --- a/test/libyul/ObjectCompilerTest.cpp +++ b/test/libyul/ObjectCompilerTest.cpp @@ -100,7 +100,7 @@ TestCase::TestResult ObjectCompilerTest::run(ostream& _stream, string const& _li "Bytecode: " + util::toHex(obj.bytecode->bytecode) + "\nOpcodes: " + - boost::trim_copy(evmasm::disassemble(obj.bytecode->bytecode)) + + boost::trim_copy(evmasm::disassemble(obj.bytecode->bytecode, EVMVersion())) + "\nSourceMappings:" + (obj.sourceMappings->empty() ? "" : " " + *obj.sourceMappings) + "\n";