VMConfig.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  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. namespace dev
  15. {
  16. namespace eth
  17. {
  18. ///////////////////////////////////////////////////////////////////////////////
  19. //
  20. // interpreter configuration macros for optimizations and tracing
  21. //
  22. // EVM_SWITCH_DISPATCH - dispatch via loop and switch
  23. // EVM_JUMP_DISPATCH - dispatch via a jump table - available only on GCC
  24. //
  25. // EVM_USE_CONSTANT_POOL - 256 constants unpacked and ready to assign to stack
  26. //
  27. // EVM_REPLACE_CONST_JUMP - with pre-verified jumps to save runtime lookup
  28. //
  29. // EVM_TRACE - provides various levels of tracing that override ETH_VMTRACE
  30. #if true && defined(__GNUG__)
  31. #define EVM_JUMP_DISPATCH
  32. #else
  33. #define EVM_SWITCH_DISPATCH
  34. #endif
  35. #if true
  36. #define EVM_REPLACE_CONST_JUMP
  37. #endif
  38. #if true
  39. #define EVM_USE_CONSTANT_POOL
  40. #endif
  41. #if defined(EVM_USE_CONSTANT_POOL) || \
  42. defined(EVM_REPLACE_CONST_JUMP)
  43. #define EVM_DO_FIRST_PASS_OPTIMIZATION
  44. #endif
  45. // set this to 2, 1, or 0 for more, less, or no tracing to cerr
  46. #ifndef EVM_TRACE
  47. #define EVM_TRACE 0
  48. #endif
  49. #if EVM_TRACE > 0
  50. #undef ON_OP
  51. #if EVM_TRACE > 1
  52. #define ON_OP() \
  53. (onOperation(), \
  54. (cerr <<"### "<< m_nSteps <<" @"<< m_pc <<" "<< instructionInfo(m_op).name <<endl))
  55. #else
  56. #define ON_OP() onOperation()
  57. #endif
  58. #define TRACE_STR(level, str) \
  59. if ((level) <= EVM_TRACE) \
  60. cerr <<"$$$ "<< (str) <<endl;
  61. #define TRACE_VAL(level, name, val) \
  62. if ((level) <= EVM_TRACE) \
  63. cerr <<"=== "<< (name) <<" "<<hex<< (val) <<endl;
  64. #define TRACE_OP(level, pc, op) \
  65. if ((level) <= EVM_TRACE) \
  66. cerr <<"*** "<< (pc) <<" "<< instructionInfo(op).name <<endl;
  67. #define TRACE_PRE_OPT(level, pc, op) \
  68. if ((level) <= EVM_TRACE) \
  69. cerr <<"@@@ "<< (pc) <<" "<< instructionInfo(op).name <<endl;
  70. #define TRACE_POST_OPT(level, pc, op) \
  71. if ((level) <= EVM_TRACE) \
  72. cerr <<"... "<< (pc) <<" "<< instructionInfo(op).name <<endl;
  73. #else
  74. #define TRACE_STR(level, str)
  75. #define TRACE_VAL(level, name, val)
  76. #define TRACE_OP(level, pc, op)
  77. #define TRACE_PRE_OPT(level, pc, op)
  78. #define TRACE_POST_OPT(level, pc, op)
  79. #define ON_OP() onOperation()
  80. #endif
  81. // Executive swallows exceptions in some circumstances
  82. #if 0
  83. #define THROW_EXCEPTION(X) \
  84. ((cerr << "!!! EVM EXCEPTION " << (X).what() << endl), abort())
  85. #else
  86. #if EVM_TRACE > 0
  87. #define THROW_EXCEPTION(X) \
  88. ((cerr << "!!! EVM EXCEPTION " << (X).what() << endl), BOOST_THROW_EXCEPTION(X))
  89. #else
  90. #define THROW_EXCEPTION(X) BOOST_THROW_EXCEPTION(X)
  91. #endif
  92. #endif
  93. #if defined(EVM_SWITCH_DISPATCH)
  94. // build a simple loop-and-switch interpreter
  95. #define INIT_CASES if (!m_caseInit) { m_caseInit = true; return; }
  96. #define DO_CASES for(;;) { fetchInstruction(); switch(m_op) {
  97. #define CASE_BEGIN(name) case Instruction::name:
  98. #define CASE_END break;
  99. #define CASE_RETURN return;
  100. #define CASE_DEFAULT default:
  101. #define END_CASES } }
  102. #elif defined(EVM_JUMP_DISPATCH)
  103. // build an indirect-threaded interpreter using a jump table of
  104. // label addresses (a gcc extension)
  105. #define INIT_CASES \
  106. \
  107. static const void * const jumpTable[256] = \
  108. { \
  109. &&STOP, /* 00 */ \
  110. &&ADD, \
  111. &&MUL, \
  112. &&SUB, \
  113. &&DIV, \
  114. &&SDIV, \
  115. &&MOD, \
  116. &&SMOD, \
  117. &&ADDMOD, \
  118. &&MULMOD, \
  119. &&EXP, \
  120. &&SIGNEXTEND, \
  121. &&INVALID, \
  122. &&INVALID, \
  123. &&INVALID, \
  124. &&INVALID, \
  125. &&LT, /* 10, */ \
  126. &&GT, \
  127. &&SLT, \
  128. &&SGT, \
  129. &&EQ, \
  130. &&ISZERO, \
  131. &&AND, \
  132. &&OR, \
  133. &&XOR, \
  134. &&NOT, \
  135. &&BYTE, \
  136. &&INVALID, \
  137. &&INVALID, \
  138. &&INVALID, \
  139. &&INVALID, \
  140. &&INVALID, \
  141. &&SHA3, /* 20, */ \
  142. &&INVALID, \
  143. &&INVALID, \
  144. &&INVALID, \
  145. &&INVALID, \
  146. &&INVALID, \
  147. &&INVALID, \
  148. &&INVALID, \
  149. &&INVALID, \
  150. &&INVALID, \
  151. &&INVALID, \
  152. &&INVALID, \
  153. &&INVALID, \
  154. &&INVALID, \
  155. &&INVALID, \
  156. &&INVALID, \
  157. &&ADDRESS, /* 30, */ \
  158. &&BALANCE, \
  159. &&ORIGIN, \
  160. &&CALLER, \
  161. &&CALLVALUE, \
  162. &&CALLDATALOAD, \
  163. &&CALLDATASIZE, \
  164. &&CALLDATACOPY, \
  165. &&CODESIZE, \
  166. &&CODECOPY, \
  167. &&GASPRICE, \
  168. &&EXTCODESIZE, \
  169. &&EXTCODECOPY, \
  170. &&INVALID, \
  171. &&INVALID, \
  172. &&INVALID, \
  173. &&BLOCKHASH, /* 40, */ \
  174. &&COINBASE, \
  175. &&TIMESTAMP, \
  176. &&NUMBER, \
  177. &&DIFFICULTY, \
  178. &&GASLIMIT, \
  179. &&INVALID, \
  180. &&INVALID, \
  181. &&INVALID, \
  182. &&INVALID, \
  183. &&INVALID, \
  184. &&INVALID, \
  185. &&INVALID, \
  186. &&INVALID, \
  187. &&INVALID, \
  188. &&INVALID, \
  189. &&POP, /* 50, */ \
  190. &&MLOAD, \
  191. &&MSTORE, \
  192. &&MSTORE8, \
  193. &&SLOAD, \
  194. &&SSTORE, \
  195. &&JUMP, \
  196. &&JUMPI, \
  197. &&PC, \
  198. &&MSIZE, \
  199. &&GAS, \
  200. &&JUMPDEST, \
  201. &&INVALID, \
  202. &&INVALID, \
  203. &&INVALID, \
  204. &&INVALID, \
  205. &&PUSH1, /* 60, */ \
  206. &&PUSH2, \
  207. &&PUSH3, \
  208. &&PUSH4, \
  209. &&PUSH5, \
  210. &&PUSH6, \
  211. &&PUSH7, \
  212. &&PUSH8, \
  213. &&PUSH9, \
  214. &&PUSH10, \
  215. &&PUSH11, \
  216. &&PUSH12, \
  217. &&PUSH13, \
  218. &&PUSH14, \
  219. &&PUSH15, \
  220. &&PUSH16, \
  221. &&PUSH17, /* 70, */ \
  222. &&PUSH18, \
  223. &&PUSH19, \
  224. &&PUSH20, \
  225. &&PUSH21, \
  226. &&PUSH22, \
  227. &&PUSH23, \
  228. &&PUSH24, \
  229. &&PUSH25, \
  230. &&PUSH26, \
  231. &&PUSH27, \
  232. &&PUSH28, \
  233. &&PUSH29, \
  234. &&PUSH30, \
  235. &&PUSH31, \
  236. &&PUSH32, \
  237. &&DUP1, /* 80, */ \
  238. &&DUP2, \
  239. &&DUP3, \
  240. &&DUP4, \
  241. &&DUP5, \
  242. &&DUP6, \
  243. &&DUP7, \
  244. &&DUP8, \
  245. &&DUP9, \
  246. &&DUP10, \
  247. &&DUP11, \
  248. &&DUP12, \
  249. &&DUP13, \
  250. &&DUP14, \
  251. &&DUP15, \
  252. &&DUP16, \
  253. &&SWAP1, /* 90, */ \
  254. &&SWAP2, \
  255. &&SWAP3, \
  256. &&SWAP4, \
  257. &&SWAP5, \
  258. &&SWAP6, \
  259. &&SWAP7, \
  260. &&SWAP8, \
  261. &&SWAP9, \
  262. &&SWAP10, \
  263. &&SWAP11, \
  264. &&SWAP12, \
  265. &&SWAP13, \
  266. &&SWAP14, \
  267. &&SWAP15, \
  268. &&SWAP16, \
  269. &&LOG0, /* A0, */ \
  270. &&LOG1, \
  271. &&LOG2, \
  272. &&LOG3, \
  273. &&LOG4, \
  274. &&INVALID, \
  275. &&INVALID, \
  276. &&INVALID, \
  277. &&INVALID, \
  278. &&INVALID, \
  279. &&INVALID, \
  280. &&INVALID, \
  281. &&PUSHC, \
  282. &&JUMPV, \
  283. &&JUMPVI, \
  284. &&BAD, \
  285. &&INVALID, /* B0, */ \
  286. &&INVALID, \
  287. &&INVALID, \
  288. &&INVALID, \
  289. &&INVALID, \
  290. &&INVALID, \
  291. &&INVALID, \
  292. &&INVALID, \
  293. &&INVALID, \
  294. &&INVALID, \
  295. &&INVALID, \
  296. &&INVALID, \
  297. &&INVALID, \
  298. &&INVALID, \
  299. &&INVALID, \
  300. &&INVALID, \
  301. &&INVALID, /* C0, */ \
  302. &&INVALID, \
  303. &&INVALID, \
  304. &&INVALID, \
  305. &&INVALID, \
  306. &&INVALID, \
  307. &&INVALID, \
  308. &&INVALID, \
  309. &&INVALID, \
  310. &&INVALID, \
  311. &&INVALID, \
  312. &&INVALID, \
  313. &&INVALID, \
  314. &&INVALID, \
  315. &&INVALID, \
  316. &&INVALID, \
  317. &&INVALID, /* D0, */ \
  318. &&INVALID, \
  319. &&INVALID, \
  320. &&INVALID, \
  321. &&INVALID, \
  322. &&INVALID, \
  323. &&INVALID, \
  324. &&INVALID, \
  325. &&INVALID, \
  326. &&INVALID, \
  327. &&INVALID, \
  328. &&INVALID, \
  329. &&INVALID, \
  330. &&INVALID, \
  331. &&INVALID, \
  332. &&INVALID, \
  333. &&INVALID, /* E0, */ \
  334. &&INVALID, \
  335. &&INVALID, \
  336. &&INVALID, \
  337. &&INVALID, \
  338. &&INVALID, \
  339. &&INVALID, \
  340. &&INVALID, \
  341. &&INVALID, \
  342. &&INVALID, \
  343. &&INVALID, \
  344. &&INVALID, \
  345. &&INVALID, \
  346. &&INVALID, \
  347. &&INVALID, \
  348. &&INVALID, \
  349. &&CREATE, /* F0, */ \
  350. &&CALL, \
  351. &&CALLCODE, \
  352. &&RETURN, \
  353. &&DELEGATECALL, \
  354. &&INVALID, \
  355. &&INVALID, \
  356. &&INVALID, \
  357. &&INVALID, \
  358. &&INVALID, \
  359. &&INVALID, \
  360. &&INVALID, \
  361. &&INVALID, \
  362. &&INVALID, \
  363. &&INVALID, \
  364. &&SUICIDE, \
  365. }; \
  366. if (!m_caseInit) { \
  367. c_jumpTable = jumpTable; \
  368. m_caseInit = true; \
  369. return; \
  370. }
  371. #define DO_CASES fetchInstruction(); goto *jumpTable[(int)m_op];
  372. #define CASE_BEGIN(label) label:
  373. #define CASE_END fetchInstruction(); goto *jumpTable[m_code[m_pc]];
  374. #define CASE_RETURN return;
  375. #define CASE_DEFAULT INVALID:
  376. #define END_CASES
  377. #else
  378. #error No opcode dispatch configured
  379. #endif
  380. }}