From ccbd3ff63feb696025c18211c3c93bab47f755b0 Mon Sep 17 00:00:00 2001 From: Dimitry Date: Fri, 1 Apr 2016 23:11:01 +0300 Subject: move libevmcore to solidity --- CMakeLists.txt | 2 +- libevmasm/Assembly.h | 2 +- libevmasm/AssemblyItem.h | 2 +- libevmasm/CMakeLists.txt | 2 +- libevmasm/Instruction.cpp | 354 ++++++++++++++++++++++++++++++++++ libevmasm/Instruction.h | 268 +++++++++++++++++++++++++ libevmasm/SemanticInformation.h | 2 +- liblll/CodeFragment.cpp | 2 +- liblll/CodeFragment.h | 2 +- libsolidity/ast/AST.h | 2 +- libsolidity/codegen/ArrayUtils.cpp | 2 +- libsolidity/codegen/Compiler.cpp | 2 +- libsolidity/codegen/CompilerContext.h | 2 +- libsolidity/codegen/CompilerUtils.cpp | 2 +- libsolidity/codegen/LValue.cpp | 2 +- libsolidity/inlineasm/AsmData.h | 2 +- libsolidity/inlineasm/AsmParser.cpp | 2 +- lllc/CMakeLists.txt | 2 +- lllc/main.cpp | 2 +- solc/CommandLineInterface.cpp | 2 +- solc/jsonCompiler.cpp | 2 +- 21 files changed, 641 insertions(+), 19 deletions(-) create mode 100644 libevmasm/Instruction.cpp create mode 100644 libevmasm/Instruction.h diff --git a/CMakeLists.txt b/CMakeLists.txt index a843ab89..7d319314 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -31,7 +31,7 @@ add_subdirectory(libsolidity) add_subdirectory(solc) if (NOT EMSCRIPTEN) add_subdirectory(liblll) - add_subdirectory(test) + #add_subdirectory(test) add_subdirectory(lllc) endif() diff --git a/libevmasm/Assembly.h b/libevmasm/Assembly.h index 28328277..e7ca520b 100644 --- a/libevmasm/Assembly.h +++ b/libevmasm/Assembly.h @@ -26,7 +26,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/libevmasm/AssemblyItem.h b/libevmasm/AssemblyItem.h index 795b5a8a..b9c752f8 100644 --- a/libevmasm/AssemblyItem.h +++ b/libevmasm/AssemblyItem.h @@ -25,7 +25,7 @@ #include #include #include -#include +#include #include #include "Exceptions.h" diff --git a/libevmasm/CMakeLists.txt b/libevmasm/CMakeLists.txt index 424644ca..c753fbe3 100644 --- a/libevmasm/CMakeLists.txt +++ b/libevmasm/CMakeLists.txt @@ -8,7 +8,7 @@ file(GLOB HEADERS "*.h") include_directories(BEFORE ..) add_library(${EXECUTABLE} ${SRC_LIST} ${HEADERS}) -eth_use(${EXECUTABLE} REQUIRED Eth::evmcore) +eth_use(${EXECUTABLE} REQUIRED Dev::devcore Eth::ethcore) install( TARGETS ${EXECUTABLE} RUNTIME DESTINATION bin ARCHIVE DESTINATION lib LIBRARY DESTINATION lib ) install( FILES ${HEADERS} DESTINATION include/${EXECUTABLE} ) diff --git a/libevmasm/Instruction.cpp b/libevmasm/Instruction.cpp new file mode 100644 index 00000000..2703112d --- /dev/null +++ b/libevmasm/Instruction.cpp @@ -0,0 +1,354 @@ +/* + This file is part of cpp-ethereum. + + cpp-ethereum is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + cpp-ethereum is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with cpp-ethereum. If not, see . +*/ +/** @file Instruction.cpp + * @author Gav Wood + * @date 2014 + */ + +#include "Instruction.h" + +#include +#include +#include +#include +using namespace std; +using namespace dev; +using namespace dev::eth; + +const std::map dev::eth::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 }, + { "BNOT", Instruction::NOT }, + { "LT", Instruction::LT }, + { "GT", Instruction::GT }, + { "SLT", Instruction::SLT }, + { "SGT", Instruction::SGT }, + { "EQ", Instruction::EQ }, + { "NOT", Instruction::ISZERO }, + { "AND", Instruction::AND }, + { "OR", Instruction::OR }, + { "XOR", Instruction::XOR }, + { "BYTE", Instruction::BYTE }, + { "ADDMOD", Instruction::ADDMOD }, + { "MULMOD", Instruction::MULMOD }, + { "SIGNEXTEND", Instruction::SIGNEXTEND }, + { "SHA3", Instruction::SHA3 }, + { "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 }, + { "BLOCKHASH", Instruction::BLOCKHASH }, + { "COINBASE", Instruction::COINBASE }, + { "TIMESTAMP", Instruction::TIMESTAMP }, + { "NUMBER", Instruction::NUMBER }, + { "DIFFICULTY", Instruction::DIFFICULTY }, + { "GASLIMIT", Instruction::GASLIMIT }, + { "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 }, + { "RETURN", Instruction::RETURN }, + { "DELEGATECALL", Instruction::DELEGATECALL }, + { "SUICIDE", Instruction::SUICIDE } +}; + +static const std::map c_instructionInfo = +{ // Add, Args, Ret, SideEffects, GasPriceTier + { Instruction::STOP, { "STOP", 0, 0, 0, true, ZeroTier } }, + { Instruction::ADD, { "ADD", 0, 2, 1, false, VeryLowTier } }, + { Instruction::SUB, { "SUB", 0, 2, 1, false, VeryLowTier } }, + { Instruction::MUL, { "MUL", 0, 2, 1, false, LowTier } }, + { Instruction::DIV, { "DIV", 0, 2, 1, false, LowTier } }, + { Instruction::SDIV, { "SDIV", 0, 2, 1, false, LowTier } }, + { Instruction::MOD, { "MOD", 0, 2, 1, false, LowTier } }, + { Instruction::SMOD, { "SMOD", 0, 2, 1, false, LowTier } }, + { Instruction::EXP, { "EXP", 0, 2, 1, false, SpecialTier } }, + { Instruction::NOT, { "NOT", 0, 1, 1, false, VeryLowTier } }, + { Instruction::LT, { "LT", 0, 2, 1, false, VeryLowTier } }, + { Instruction::GT, { "GT", 0, 2, 1, false, VeryLowTier } }, + { Instruction::SLT, { "SLT", 0, 2, 1, false, VeryLowTier } }, + { Instruction::SGT, { "SGT", 0, 2, 1, false, VeryLowTier } }, + { Instruction::EQ, { "EQ", 0, 2, 1, false, VeryLowTier } }, + { Instruction::ISZERO, { "ISZERO", 0, 1, 1, false, VeryLowTier } }, + { Instruction::AND, { "AND", 0, 2, 1, false, VeryLowTier } }, + { Instruction::OR, { "OR", 0, 2, 1, false, VeryLowTier } }, + { Instruction::XOR, { "XOR", 0, 2, 1, false, VeryLowTier } }, + { Instruction::BYTE, { "BYTE", 0, 2, 1, false, VeryLowTier } }, + { Instruction::ADDMOD, { "ADDMOD", 0, 3, 1, false, MidTier } }, + { Instruction::MULMOD, { "MULMOD", 0, 3, 1, false, MidTier } }, + { Instruction::SIGNEXTEND, { "SIGNEXTEND", 0, 2, 1, false, LowTier } }, + { Instruction::SHA3, { "SHA3", 0, 2, 1, false, SpecialTier } }, + { Instruction::ADDRESS, { "ADDRESS", 0, 0, 1, false, BaseTier } }, + { Instruction::BALANCE, { "BALANCE", 0, 1, 1, false, ExtTier } }, + { Instruction::ORIGIN, { "ORIGIN", 0, 0, 1, false, BaseTier } }, + { Instruction::CALLER, { "CALLER", 0, 0, 1, false, BaseTier } }, + { Instruction::CALLVALUE, { "CALLVALUE", 0, 0, 1, false, BaseTier } }, + { Instruction::CALLDATALOAD,{ "CALLDATALOAD", 0, 1, 1, false, VeryLowTier } }, + { Instruction::CALLDATASIZE,{ "CALLDATASIZE", 0, 0, 1, false, BaseTier } }, + { Instruction::CALLDATACOPY,{ "CALLDATACOPY", 0, 3, 0, true, VeryLowTier } }, + { Instruction::CODESIZE, { "CODESIZE", 0, 0, 1, false, BaseTier } }, + { Instruction::CODECOPY, { "CODECOPY", 0, 3, 0, true, VeryLowTier } }, + { Instruction::GASPRICE, { "GASPRICE", 0, 0, 1, false, BaseTier } }, + { Instruction::EXTCODESIZE, { "EXTCODESIZE", 0, 1, 1, false, ExtTier } }, + { Instruction::EXTCODECOPY, { "EXTCODECOPY", 0, 4, 0, true, ExtTier } }, + { Instruction::BLOCKHASH, { "BLOCKHASH", 0, 1, 1, false, ExtTier } }, + { Instruction::COINBASE, { "COINBASE", 0, 0, 1, false, BaseTier } }, + { Instruction::TIMESTAMP, { "TIMESTAMP", 0, 0, 1, false, BaseTier } }, + { Instruction::NUMBER, { "NUMBER", 0, 0, 1, false, BaseTier } }, + { Instruction::DIFFICULTY, { "DIFFICULTY", 0, 0, 1, false, BaseTier } }, + { Instruction::GASLIMIT, { "GASLIMIT", 0, 0, 1, false, BaseTier } }, + { Instruction::POP, { "POP", 0, 1, 0, false, BaseTier } }, + { Instruction::MLOAD, { "MLOAD", 0, 1, 1, false, VeryLowTier } }, + { Instruction::MSTORE, { "MSTORE", 0, 2, 0, true, VeryLowTier } }, + { Instruction::MSTORE8, { "MSTORE8", 0, 2, 0, true, VeryLowTier } }, + { Instruction::SLOAD, { "SLOAD", 0, 1, 1, false, SpecialTier } }, + { Instruction::SSTORE, { "SSTORE", 0, 2, 0, true, SpecialTier } }, + { Instruction::JUMP, { "JUMP", 0, 1, 0, true, MidTier } }, + { Instruction::JUMPI, { "JUMPI", 0, 2, 0, true, HighTier } }, + { Instruction::PC, { "PC", 0, 0, 1, false, BaseTier } }, + { Instruction::MSIZE, { "MSIZE", 0, 0, 1, false, BaseTier } }, + { Instruction::GAS, { "GAS", 0, 0, 1, false, BaseTier } }, + { Instruction::JUMPDEST, { "JUMPDEST", 0, 0, 0, true, SpecialTier } }, + { Instruction::PUSH1, { "PUSH1", 1, 0, 1, false, VeryLowTier } }, + { Instruction::PUSH2, { "PUSH2", 2, 0, 1, false, VeryLowTier } }, + { Instruction::PUSH3, { "PUSH3", 3, 0, 1, false, VeryLowTier } }, + { Instruction::PUSH4, { "PUSH4", 4, 0, 1, false, VeryLowTier } }, + { Instruction::PUSH5, { "PUSH5", 5, 0, 1, false, VeryLowTier } }, + { Instruction::PUSH6, { "PUSH6", 6, 0, 1, false, VeryLowTier } }, + { Instruction::PUSH7, { "PUSH7", 7, 0, 1, false, VeryLowTier } }, + { Instruction::PUSH8, { "PUSH8", 8, 0, 1, false, VeryLowTier } }, + { Instruction::PUSH9, { "PUSH9", 9, 0, 1, false, VeryLowTier } }, + { Instruction::PUSH10, { "PUSH10", 10, 0, 1, false, VeryLowTier } }, + { Instruction::PUSH11, { "PUSH11", 11, 0, 1, false, VeryLowTier } }, + { Instruction::PUSH12, { "PUSH12", 12, 0, 1, false, VeryLowTier } }, + { Instruction::PUSH13, { "PUSH13", 13, 0, 1, false, VeryLowTier } }, + { Instruction::PUSH14, { "PUSH14", 14, 0, 1, false, VeryLowTier } }, + { Instruction::PUSH15, { "PUSH15", 15, 0, 1, false, VeryLowTier } }, + { Instruction::PUSH16, { "PUSH16", 16, 0, 1, false, VeryLowTier } }, + { Instruction::PUSH17, { "PUSH17", 17, 0, 1, false, VeryLowTier } }, + { Instruction::PUSH18, { "PUSH18", 18, 0, 1, false, VeryLowTier } }, + { Instruction::PUSH19, { "PUSH19", 19, 0, 1, false, VeryLowTier } }, + { Instruction::PUSH20, { "PUSH20", 20, 0, 1, false, VeryLowTier } }, + { Instruction::PUSH21, { "PUSH21", 21, 0, 1, false, VeryLowTier } }, + { Instruction::PUSH22, { "PUSH22", 22, 0, 1, false, VeryLowTier } }, + { Instruction::PUSH23, { "PUSH23", 23, 0, 1, false, VeryLowTier } }, + { Instruction::PUSH24, { "PUSH24", 24, 0, 1, false, VeryLowTier } }, + { Instruction::PUSH25, { "PUSH25", 25, 0, 1, false, VeryLowTier } }, + { Instruction::PUSH26, { "PUSH26", 26, 0, 1, false, VeryLowTier } }, + { Instruction::PUSH27, { "PUSH27", 27, 0, 1, false, VeryLowTier } }, + { Instruction::PUSH28, { "PUSH28", 28, 0, 1, false, VeryLowTier } }, + { Instruction::PUSH29, { "PUSH29", 29, 0, 1, false, VeryLowTier } }, + { Instruction::PUSH30, { "PUSH30", 30, 0, 1, false, VeryLowTier } }, + { Instruction::PUSH31, { "PUSH31", 31, 0, 1, false, VeryLowTier } }, + { Instruction::PUSH32, { "PUSH32", 32, 0, 1, false, VeryLowTier } }, + { Instruction::DUP1, { "DUP1", 0, 1, 2, false, VeryLowTier } }, + { Instruction::DUP2, { "DUP2", 0, 2, 3, false, VeryLowTier } }, + { Instruction::DUP3, { "DUP3", 0, 3, 4, false, VeryLowTier } }, + { Instruction::DUP4, { "DUP4", 0, 4, 5, false, VeryLowTier } }, + { Instruction::DUP5, { "DUP5", 0, 5, 6, false, VeryLowTier } }, + { Instruction::DUP6, { "DUP6", 0, 6, 7, false, VeryLowTier } }, + { Instruction::DUP7, { "DUP7", 0, 7, 8, false, VeryLowTier } }, + { Instruction::DUP8, { "DUP8", 0, 8, 9, false, VeryLowTier } }, + { Instruction::DUP9, { "DUP9", 0, 9, 10, false, VeryLowTier } }, + { Instruction::DUP10, { "DUP10", 0, 10, 11, false, VeryLowTier } }, + { Instruction::DUP11, { "DUP11", 0, 11, 12, false, VeryLowTier } }, + { Instruction::DUP12, { "DUP12", 0, 12, 13, false, VeryLowTier } }, + { Instruction::DUP13, { "DUP13", 0, 13, 14, false, VeryLowTier } }, + { Instruction::DUP14, { "DUP14", 0, 14, 15, false, VeryLowTier } }, + { Instruction::DUP15, { "DUP15", 0, 15, 16, false, VeryLowTier } }, + { Instruction::DUP16, { "DUP16", 0, 16, 17, false, VeryLowTier } }, + { Instruction::SWAP1, { "SWAP1", 0, 2, 2, false, VeryLowTier } }, + { Instruction::SWAP2, { "SWAP2", 0, 3, 3, false, VeryLowTier } }, + { Instruction::SWAP3, { "SWAP3", 0, 4, 4, false, VeryLowTier } }, + { Instruction::SWAP4, { "SWAP4", 0, 5, 5, false, VeryLowTier } }, + { Instruction::SWAP5, { "SWAP5", 0, 6, 6, false, VeryLowTier } }, + { Instruction::SWAP6, { "SWAP6", 0, 7, 7, false, VeryLowTier } }, + { Instruction::SWAP7, { "SWAP7", 0, 8, 8, false, VeryLowTier } }, + { Instruction::SWAP8, { "SWAP8", 0, 9, 9, false, VeryLowTier } }, + { Instruction::SWAP9, { "SWAP9", 0, 10, 10, false, VeryLowTier } }, + { Instruction::SWAP10, { "SWAP10", 0, 11, 11, false, VeryLowTier } }, + { Instruction::SWAP11, { "SWAP11", 0, 12, 12, false, VeryLowTier } }, + { Instruction::SWAP12, { "SWAP12", 0, 13, 13, false, VeryLowTier } }, + { Instruction::SWAP13, { "SWAP13", 0, 14, 14, false, VeryLowTier } }, + { Instruction::SWAP14, { "SWAP14", 0, 15, 15, false, VeryLowTier } }, + { Instruction::SWAP15, { "SWAP15", 0, 16, 16, false, VeryLowTier } }, + { Instruction::SWAP16, { "SWAP16", 0, 17, 17, false, VeryLowTier } }, + { Instruction::LOG0, { "LOG0", 0, 2, 0, true, SpecialTier } }, + { Instruction::LOG1, { "LOG1", 0, 3, 0, true, SpecialTier } }, + { Instruction::LOG2, { "LOG2", 0, 4, 0, true, SpecialTier } }, + { Instruction::LOG3, { "LOG3", 0, 5, 0, true, SpecialTier } }, + { Instruction::LOG4, { "LOG4", 0, 6, 0, true, SpecialTier } }, + { Instruction::CREATE, { "CREATE", 0, 3, 1, true, SpecialTier } }, + { Instruction::CALL, { "CALL", 0, 7, 1, true, SpecialTier } }, + { Instruction::CALLCODE, { "CALLCODE", 0, 7, 1, true, SpecialTier } }, + { Instruction::RETURN, { "RETURN", 0, 2, 0, true, ZeroTier } }, + { Instruction::DELEGATECALL,{ "DELEGATECALL", 0, 6, 1, true, SpecialTier } }, + { Instruction::SUICIDE, { "SUICIDE", 0, 1, 0, true, ZeroTier } } +}; + +void dev::eth::eachInstruction( + bytes const& _mem, + function const& _onInstruction +) +{ + for (auto it = _mem.begin(); it < _mem.end(); ++it) + { + Instruction instr = Instruction(*it); + size_t additional = 0; + if (isValidInstruction(instr)) + additional = instructionInfo(instr).additional; + u256 data; + for (size_t i = 0; i < additional; ++i) + { + data <<= 8; + if (++it < _mem.end()) + data |= *it; + } + _onInstruction(instr, data); + } +} + +string dev::eth::disassemble(bytes const& _mem) +{ + stringstream ret; + eachInstruction(_mem, [&](Instruction _instr, u256 const& _data) { + if (!isValidInstruction(_instr)) + ret << "0x" << hex << int(_instr) << " "; + else + { + InstructionInfo info = instructionInfo(_instr); + ret << info.name << " "; + if (info.additional) + ret << "0x" << hex << _data << " "; + } + }); + return ret.str(); +} + +InstructionInfo dev::eth::instructionInfo(Instruction _inst) +{ + try + { + return c_instructionInfo.at(_inst); + } + catch (...) + { + return InstructionInfo({"", 0, 0, 0, false, InvalidTier}); + } +} + +bool dev::eth::isValidInstruction(Instruction _inst) +{ + return !!c_instructionInfo.count(_inst); +} diff --git a/libevmasm/Instruction.h b/libevmasm/Instruction.h new file mode 100644 index 00000000..2d415c08 --- /dev/null +++ b/libevmasm/Instruction.h @@ -0,0 +1,268 @@ +/* + This file is part of cpp-ethereum. + + cpp-ethereum is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + cpp-ethereum is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with cpp-ethereum. If not, see . +*/ +/** @file Instruction.h + * @author Gav Wood + * @date 2014 + */ + +#pragma once + +#include +#include +#include +#include "Exceptions.h" + +namespace dev +{ +namespace eth +{ + +DEV_SIMPLE_EXCEPTION(InvalidDeposit); +DEV_SIMPLE_EXCEPTION(InvalidOpcode); + +/// Virtual machine bytecode instruction. +enum class Instruction: uint8_t +{ + STOP = 0x00, ///< halts execution + ADD, ///< addition operation + MUL, ///< mulitplication 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 = 0x10, ///< less-than comparision + GT, ///< greater-than comparision + SLT, ///< signed less-than comparision + SGT, ///< signed greater-than comparision + EQ, ///< equality comparision + ISZERO, ///< simple not operator + AND, ///< bitwise AND operation + OR, ///< bitwise OR operation + XOR, ///< bitwise XOR operation + NOT, ///< bitwise NOT opertation + BYTE, ///< retrieve single byte from word + + SHA3 = 0x20, ///< compute SHA3-256 hash + + ADDRESS = 0x30, ///< 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) + + BLOCKHASH = 0x40, ///< 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 + GASLIMIT, ///< get the block's gas limit + + POP = 0x50, ///< 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 = 0x60, ///< 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 = 0x80, ///< 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 = 0x90, ///< 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 = 0xa0, ///< 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 = 0xf0, ///< 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 + SUICIDE = 0xff ///< halt execution and register account for later deletion +}; + +/// @returns the number of PUSH Instruction _inst +inline unsigned getPushNumber(Instruction _inst) +{ + return (byte)_inst - unsigned(Instruction::PUSH1) + 1; +} + +/// @returns the number of DUP Instruction _inst +inline unsigned getDupNumber(Instruction _inst) +{ + return (byte)_inst - unsigned(Instruction::DUP1) + 1; +} + +/// @returns the number of SWAP Instruction _inst +inline unsigned getSwapNumber(Instruction _inst) +{ + return (byte)_inst - unsigned(Instruction::SWAP1) + 1; +} + +/// @returns the PUSH<_number> instruction +inline Instruction pushInstruction(unsigned _number) +{ + assertThrow(1 <= _number && _number <= 32, InvalidOpcode, "Invalid PUSH instruction requested."); + return Instruction(unsigned(Instruction::PUSH1) + _number - 1); +} + +/// @returns the DUP<_number> instruction +inline Instruction dupInstruction(unsigned _number) +{ + assertThrow(1 <= _number && _number <= 16, InvalidOpcode, "Invalid DUP instruction requested."); + return Instruction(unsigned(Instruction::DUP1) + _number - 1); +} + +/// @returns the SWAP<_number> instruction +inline Instruction swapInstruction(unsigned _number) +{ + assertThrow(1 <= _number && _number <= 16, InvalidOpcode, "Invalid SWAP instruction requested."); + return Instruction(unsigned(Instruction::SWAP1) + _number - 1); +} + +/// @returns the LOG<_number> instruction +inline Instruction logInstruction(unsigned _number) +{ + assertThrow(_number <= 4, InvalidOpcode, "Invalid LOG instruction requested."); + return Instruction(unsigned(Instruction::LOG0) + _number); +} + +enum Tier +{ + ZeroTier = 0, // 0, Zero + BaseTier, // 2, Quick + VeryLowTier, // 3, Fastest + LowTier, // 5, Fast + MidTier, // 8, Mid + HighTier, // 10, Slow + ExtTier, // 20, Ext + SpecialTier, // multiparam or otherwise special + InvalidTier // Invalid. +}; + +/// Information structure for a particular instruction. +struct InstructionInfo +{ + std::string name; ///< The name of the instruction. + 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. + bool sideEffects; ///< false if the only effect on the execution environment (apart from gas usage) is a change to a topmost segment of the stack + int gasPriceTier; ///< Tier for gas pricing. +}; + +/// Information on all the instructions. +InstructionInfo instructionInfo(Instruction _inst); + +/// check whether instructions exists +bool isValidInstruction(Instruction _inst); + +/// Convert from string mnemonic to Instruction type. +extern const std::map c_instructions; + +/// Iterate through EVM code and call a function on each instruction. +void eachInstruction(bytes const& _mem, std::function const& _onInstruction); + +/// Convert from EVM code to simple EVM assembly language. +std::string disassemble(bytes const& _mem); + +} +} diff --git a/libevmasm/SemanticInformation.h b/libevmasm/SemanticInformation.h index 094f4591..d18cac7c 100644 --- a/libevmasm/SemanticInformation.h +++ b/libevmasm/SemanticInformation.h @@ -23,7 +23,7 @@ #pragma once -#include +#include namespace dev { diff --git a/liblll/CodeFragment.cpp b/liblll/CodeFragment.cpp index 64680d5a..7998a5b5 100644 --- a/liblll/CodeFragment.cpp +++ b/liblll/CodeFragment.cpp @@ -30,7 +30,7 @@ #pragma GCC diagnostic pop #include #include -#include +#include #include "CompilerState.h" #include "Parser.h" using namespace std; diff --git a/liblll/CodeFragment.h b/liblll/CodeFragment.h index 03f812b6..e0d48ab7 100644 --- a/liblll/CodeFragment.h +++ b/liblll/CodeFragment.h @@ -22,7 +22,7 @@ #pragma once #include -#include +#include #include #include "Exceptions.h" diff --git a/libsolidity/ast/AST.h b/libsolidity/ast/AST.h index 7bb2529a..bf275869 100644 --- a/libsolidity/ast/AST.h +++ b/libsolidity/ast/AST.h @@ -28,7 +28,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/libsolidity/codegen/ArrayUtils.cpp b/libsolidity/codegen/ArrayUtils.cpp index f1acce31..c43c48d1 100644 --- a/libsolidity/codegen/ArrayUtils.cpp +++ b/libsolidity/codegen/ArrayUtils.cpp @@ -21,7 +21,7 @@ */ #include -#include +#include #include #include #include diff --git a/libsolidity/codegen/Compiler.cpp b/libsolidity/codegen/Compiler.cpp index 69e23359..3453b6d7 100644 --- a/libsolidity/codegen/Compiler.cpp +++ b/libsolidity/codegen/Compiler.cpp @@ -23,7 +23,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/libsolidity/codegen/CompilerContext.h b/libsolidity/codegen/CompilerContext.h index bd8fc295..5e2fbbdc 100644 --- a/libsolidity/codegen/CompilerContext.h +++ b/libsolidity/codegen/CompilerContext.h @@ -25,7 +25,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/libsolidity/codegen/CompilerUtils.cpp b/libsolidity/codegen/CompilerUtils.cpp index bd0857f6..daf129e5 100644 --- a/libsolidity/codegen/CompilerUtils.cpp +++ b/libsolidity/codegen/CompilerUtils.cpp @@ -22,7 +22,7 @@ #include #include -#include +#include #include #include diff --git a/libsolidity/codegen/LValue.cpp b/libsolidity/codegen/LValue.cpp index fdef6937..be1fefcd 100644 --- a/libsolidity/codegen/LValue.cpp +++ b/libsolidity/codegen/LValue.cpp @@ -21,7 +21,7 @@ */ #include -#include +#include #include #include #include diff --git a/libsolidity/inlineasm/AsmData.h b/libsolidity/inlineasm/AsmData.h index 0361a4c2..95cfc6ee 100644 --- a/libsolidity/inlineasm/AsmData.h +++ b/libsolidity/inlineasm/AsmData.h @@ -23,7 +23,7 @@ #pragma once #include -#include +#include namespace dev { diff --git a/libsolidity/inlineasm/AsmParser.cpp b/libsolidity/inlineasm/AsmParser.cpp index 124a5d26..b1fd464e 100644 --- a/libsolidity/inlineasm/AsmParser.cpp +++ b/libsolidity/inlineasm/AsmParser.cpp @@ -23,7 +23,7 @@ #include #include #include -#include +#include #include using namespace std; diff --git a/lllc/CMakeLists.txt b/lllc/CMakeLists.txt index 989b8cbf..a897e663 100644 --- a/lllc/CMakeLists.txt +++ b/lllc/CMakeLists.txt @@ -6,7 +6,7 @@ file(GLOB HEADERS "*.h") include_directories(BEFORE ..) add_executable(${EXECUTABLE} ${SRC_LIST} ${HEADERS}) -eth_use(${EXECUTABLE} REQUIRED Solidity::lll Dev::buildinfo) +eth_use(${EXECUTABLE} REQUIRED Solidity::lll Dev::buildinfo Solidity::evmasm) install( TARGETS ${EXECUTABLE} DESTINATION bin ) diff --git a/lllc/main.cpp b/lllc/main.cpp index 5f3078c2..a2a3adc4 100644 --- a/lllc/main.cpp +++ b/lllc/main.cpp @@ -25,7 +25,7 @@ #include #include #include -#include +#include #include "ethereum/BuildInfo.h" using namespace std; using namespace dev; diff --git a/solc/CommandLineInterface.cpp b/solc/CommandLineInterface.cpp index a302e024..0583ea4f 100644 --- a/solc/CommandLineInterface.cpp +++ b/solc/CommandLineInterface.cpp @@ -34,7 +34,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/solc/jsonCompiler.cpp b/solc/jsonCompiler.cpp index c6b40fdc..bf7ebe5c 100644 --- a/solc/jsonCompiler.cpp +++ b/solc/jsonCompiler.cpp @@ -27,7 +27,7 @@ #include #include #include -#include +#include #include #include #include -- cgit