Instruction.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. /*
  2. This file is part of cpp-ethereum.
  3. cpp-ethereum is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 3 of the License, or
  6. (at your option) any later version.
  7. cpp-ethereum is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. /** @file Instruction.h
  15. * @author Gav Wood <i@gavwood.com>
  16. * @date 2014
  17. */
  18. #pragma once
  19. #include <functional>
  20. #include <libdevcore/Common.h>
  21. #include <libdevcore/Assertions.h>
  22. #include "Exceptions.h"
  23. namespace dev
  24. {
  25. namespace eth
  26. {
  27. /// Virtual machine bytecode instruction.
  28. enum class Instruction: uint8_t
  29. {
  30. STOP = 0x00, ///< halts execution
  31. ADD, ///< addition operation
  32. MUL, ///< mulitplication operation
  33. SUB, ///< subtraction operation
  34. DIV, ///< integer division operation
  35. SDIV, ///< signed integer division operation
  36. MOD, ///< modulo remainder operation
  37. SMOD, ///< signed modulo remainder operation
  38. ADDMOD, ///< unsigned modular addition
  39. MULMOD, ///< unsigned modular multiplication
  40. EXP, ///< exponential operation
  41. SIGNEXTEND, ///< extend length of signed integer
  42. LT = 0x10, ///< less-than comparision
  43. GT, ///< greater-than comparision
  44. SLT, ///< signed less-than comparision
  45. SGT, ///< signed greater-than comparision
  46. EQ, ///< equality comparision
  47. ISZERO, ///< simple not operator
  48. AND, ///< bitwise AND operation
  49. OR, ///< bitwise OR operation
  50. XOR, ///< bitwise XOR operation
  51. NOT, ///< bitwise NOT opertation
  52. BYTE, ///< retrieve single byte from word
  53. SHA3 = 0x20, ///< compute SHA3-256 hash
  54. ADDRESS = 0x30, ///< get address of currently executing account
  55. BALANCE, ///< get balance of the given account
  56. ORIGIN, ///< get execution origination address
  57. CALLER, ///< get caller address
  58. CALLVALUE, ///< get deposited value by the instruction/transaction responsible for this execution
  59. CALLDATALOAD, ///< get input data of current environment
  60. CALLDATASIZE, ///< get size of input data in current environment
  61. CALLDATACOPY, ///< copy input data in current environment to memory
  62. CODESIZE, ///< get size of code running in current environment
  63. CODECOPY, ///< copy code running in current environment to memory
  64. GASPRICE, ///< get price of gas in current environment
  65. EXTCODESIZE, ///< get external code size (from another contract)
  66. EXTCODECOPY, ///< copy external code (from another contract)
  67. BLOCKHASH = 0x40, ///< get hash of most recent complete block
  68. COINBASE, ///< get the block's coinbase address
  69. TIMESTAMP, ///< get the block's timestamp
  70. NUMBER, ///< get the block's number
  71. DIFFICULTY, ///< get the block's difficulty
  72. GASLIMIT, ///< get the block's gas limit
  73. POP = 0x50, ///< remove item from stack
  74. MLOAD, ///< load word from memory
  75. MSTORE, ///< save word to memory
  76. MSTORE8, ///< save byte to memory
  77. SLOAD, ///< load word from storage
  78. SSTORE, ///< save word to storage
  79. JUMP, ///< alter the program counter
  80. JUMPI, ///< conditionally alter the program counter
  81. PC, ///< get the program counter
  82. MSIZE, ///< get the size of active memory
  83. GAS, ///< get the amount of available gas
  84. JUMPDEST, ///< set a potential jump destination
  85. PUSH1 = 0x60, ///< place 1 byte item on stack
  86. PUSH2, ///< place 2 byte item on stack
  87. PUSH3, ///< place 3 byte item on stack
  88. PUSH4, ///< place 4 byte item on stack
  89. PUSH5, ///< place 5 byte item on stack
  90. PUSH6, ///< place 6 byte item on stack
  91. PUSH7, ///< place 7 byte item on stack
  92. PUSH8, ///< place 8 byte item on stack
  93. PUSH9, ///< place 9 byte item on stack
  94. PUSH10, ///< place 10 byte item on stack
  95. PUSH11, ///< place 11 byte item on stack
  96. PUSH12, ///< place 12 byte item on stack
  97. PUSH13, ///< place 13 byte item on stack
  98. PUSH14, ///< place 14 byte item on stack
  99. PUSH15, ///< place 15 byte item on stack
  100. PUSH16, ///< place 16 byte item on stack
  101. PUSH17, ///< place 17 byte item on stack
  102. PUSH18, ///< place 18 byte item on stack
  103. PUSH19, ///< place 19 byte item on stack
  104. PUSH20, ///< place 20 byte item on stack
  105. PUSH21, ///< place 21 byte item on stack
  106. PUSH22, ///< place 22 byte item on stack
  107. PUSH23, ///< place 23 byte item on stack
  108. PUSH24, ///< place 24 byte item on stack
  109. PUSH25, ///< place 25 byte item on stack
  110. PUSH26, ///< place 26 byte item on stack
  111. PUSH27, ///< place 27 byte item on stack
  112. PUSH28, ///< place 28 byte item on stack
  113. PUSH29, ///< place 29 byte item on stack
  114. PUSH30, ///< place 30 byte item on stack
  115. PUSH31, ///< place 31 byte item on stack
  116. PUSH32, ///< place 32 byte item on stack
  117. DUP1 = 0x80, ///< copies the highest item in the stack to the top of the stack
  118. DUP2, ///< copies the second highest item in the stack to the top of the stack
  119. DUP3, ///< copies the third highest item in the stack to the top of the stack
  120. DUP4, ///< copies the 4th highest item in the stack to the top of the stack
  121. DUP5, ///< copies the 5th highest item in the stack to the top of the stack
  122. DUP6, ///< copies the 6th highest item in the stack to the top of the stack
  123. DUP7, ///< copies the 7th highest item in the stack to the top of the stack
  124. DUP8, ///< copies the 8th highest item in the stack to the top of the stack
  125. DUP9, ///< copies the 9th highest item in the stack to the top of the stack
  126. DUP10, ///< copies the 10th highest item in the stack to the top of the stack
  127. DUP11, ///< copies the 11th highest item in the stack to the top of the stack
  128. DUP12, ///< copies the 12th highest item in the stack to the top of the stack
  129. DUP13, ///< copies the 13th highest item in the stack to the top of the stack
  130. DUP14, ///< copies the 14th highest item in the stack to the top of the stack
  131. DUP15, ///< copies the 15th highest item in the stack to the top of the stack
  132. DUP16, ///< copies the 16th highest item in the stack to the top of the stack
  133. SWAP1 = 0x90, ///< swaps the highest and second highest value on the stack
  134. SWAP2, ///< swaps the highest and third highest value on the stack
  135. SWAP3, ///< swaps the highest and 4th highest value on the stack
  136. SWAP4, ///< swaps the highest and 5th highest value on the stack
  137. SWAP5, ///< swaps the highest and 6th highest value on the stack
  138. SWAP6, ///< swaps the highest and 7th highest value on the stack
  139. SWAP7, ///< swaps the highest and 8th highest value on the stack
  140. SWAP8, ///< swaps the highest and 9th highest value on the stack
  141. SWAP9, ///< swaps the highest and 10th highest value on the stack
  142. SWAP10, ///< swaps the highest and 11th highest value on the stack
  143. SWAP11, ///< swaps the highest and 12th highest value on the stack
  144. SWAP12, ///< swaps the highest and 13th highest value on the stack
  145. SWAP13, ///< swaps the highest and 14th highest value on the stack
  146. SWAP14, ///< swaps the highest and 15th highest value on the stack
  147. SWAP15, ///< swaps the highest and 16th highest value on the stack
  148. SWAP16, ///< swaps the highest and 17th highest value on the stack
  149. LOG0 = 0xa0, ///< Makes a log entry; no topics.
  150. LOG1, ///< Makes a log entry; 1 topic.
  151. LOG2, ///< Makes a log entry; 2 topics.
  152. LOG3, ///< Makes a log entry; 3 topics.
  153. LOG4, ///< Makes a log entry; 4 topics.
  154. // these are generated by the interpreter - should never be in user code
  155. PUSHC = 0xac, ///< push value from constant pool
  156. JUMPV, ///< alter the program counter - pre-verified
  157. JUMPVI, ///< conditionally alter the program counter - pre-verified
  158. BAD, ///< placed to force invalid instruction exception
  159. CREATE = 0xf0, ///< create a new account with associated code
  160. CALL, ///< message-call into an account
  161. CALLCODE, ///< message-call with another account's code only
  162. RETURN, ///< halt execution returning output data
  163. DELEGATECALL, ///< like CALLCODE but keeps caller's value and sender
  164. SUICIDE = 0xff ///< halt execution and register account for later deletion
  165. };
  166. /// @returns the number of PUSH Instruction _inst
  167. inline unsigned getPushNumber(Instruction _inst)
  168. {
  169. return (byte)_inst - unsigned(Instruction::PUSH1) + 1;
  170. }
  171. /// @returns the number of DUP Instruction _inst
  172. inline unsigned getDupNumber(Instruction _inst)
  173. {
  174. return (byte)_inst - unsigned(Instruction::DUP1) + 1;
  175. }
  176. /// @returns the number of SWAP Instruction _inst
  177. inline unsigned getSwapNumber(Instruction _inst)
  178. {
  179. return (byte)_inst - unsigned(Instruction::SWAP1) + 1;
  180. }
  181. /// @returns the PUSH<_number> instruction
  182. inline Instruction pushInstruction(unsigned _number)
  183. {
  184. assertThrow(1 <= _number && _number <= 32, InvalidOpcode, "Invalid PUSH instruction requested.");
  185. return Instruction(unsigned(Instruction::PUSH1) + _number - 1);
  186. }
  187. /// @returns the DUP<_number> instruction
  188. inline Instruction dupInstruction(unsigned _number)
  189. {
  190. assertThrow(1 <= _number && _number <= 16, InvalidOpcode, "Invalid DUP instruction requested.");
  191. return Instruction(unsigned(Instruction::DUP1) + _number - 1);
  192. }
  193. /// @returns the SWAP<_number> instruction
  194. inline Instruction swapInstruction(unsigned _number)
  195. {
  196. assertThrow(1 <= _number && _number <= 16, InvalidOpcode, "Invalid SWAP instruction requested.");
  197. return Instruction(unsigned(Instruction::SWAP1) + _number - 1);
  198. }
  199. /// @returns the LOG<_number> instruction
  200. inline Instruction logInstruction(unsigned _number)
  201. {
  202. assertThrow(_number <= 4, InvalidOpcode, "Invalid LOG instruction requested.");
  203. return Instruction(unsigned(Instruction::LOG0) + _number);
  204. }
  205. enum Tier
  206. {
  207. ZeroTier = 0, // 0, Zero
  208. BaseTier, // 2, Quick
  209. VeryLowTier, // 3, Fastest
  210. LowTier, // 5, Fast
  211. MidTier, // 8, Mid
  212. HighTier, // 10, Slow
  213. ExtTier, // 20, Ext
  214. SpecialTier, // multiparam or otherwise special
  215. InvalidTier // Invalid.
  216. };
  217. /// Information structure for a particular instruction.
  218. struct InstructionInfo
  219. {
  220. std::string name; ///< The name of the instruction.
  221. int additional; ///< Additional items required in memory for this instructions (only for PUSH).
  222. int args; ///< Number of items required on the stack for this instruction (and, for the purposes of ret, the number taken from the stack).
  223. int ret; ///< Number of items placed (back) on the stack by this instruction, assuming args items were removed.
  224. 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
  225. int gasPriceTier; ///< Tier for gas pricing.
  226. };
  227. /// Information on all the instructions.
  228. InstructionInfo instructionInfo(Instruction _inst);
  229. /// check whether instructions exists
  230. bool isValidInstruction(Instruction _inst);
  231. /// Convert from string mnemonic to Instruction type.
  232. extern const std::map<std::string, Instruction> c_instructions;
  233. /// Iterate through EVM code and call a function on each instruction.
  234. void eachInstruction(bytes const& _mem, std::function<void(Instruction,u256 const&)> const& _onInstruction);
  235. /// Convert from EVM code to simple EVM assembly language.
  236. std::string disassemble(bytes const& _mem);
  237. }
  238. }