1 /**
2  * Ethereum Virtual Machine (EVM) opcodes and related methods.
3  *
4  * Copyright © 2016, Eric Scrivner
5  *
6  * License: Subject to the terms of the MIT license, as written in the included
7  * LICENSE.txt file.
8  * Authors: Eric Scrivner
9  */
10 module phlogiston.evm.opcodes;
11 
12 import std.conv;
13 import std.traits;
14 
15 /// Enumeration of EVM opcode names and byte values.
16 enum Opcode : ubyte {
17   STOP = 0x00,
18   ADD,
19   MUL,
20   SUB,
21   DIV,
22   SDIV,
23   MOD,
24   SMOD,
25   ADDMOD,
26   MULMOD,
27   EXP,
28   SIGNEXTEND,
29   LT,
30   GT,
31   SLT,
32   SGT,
33 
34   EQ = 0x10,
35   ISZERO,
36   AND,
37   OR,
38   XOR,
39   NOT,
40   BYTE,
41   SHA3 = 0x20,
42 
43   ADDRESS = 0x30,
44   BALANCE,
45   ORIGIN,
46   CALLER,
47   CALLVALUE,
48   CALLDATALOAD,
49   CALLDATASIZE,
50   CALLDATACOPY,
51   CODESIZE,
52   CODECOPY,
53   GASPRICE,
54   EXTCODESIZE,
55   EXTCODECOPY,
56 
57   BLOCKHASH = 0x40,
58   COINBASE,
59   TIMESTAMP,
60   NUMBER,
61   DIFFICULTY,
62   GASLIMIT,
63 
64   POP = 0x50,
65   MLOAD,
66   MSTORE,
67   MSTORE8,
68   SLOAD,
69   SSTORE,
70   JUMP,
71   JUMPI,
72   PC,
73   MSIZE,
74   GAS,
75   JUMPDEST,
76 
77   PUSH1 = 0x60,
78   PUSH2,
79   PUSH3,
80   PUSH4,
81   PUSH5,
82   PUSH6,
83   PUSH7,
84   PUSH8,
85   PUSH9,
86   PUSH10,
87   PUSH11,
88   PUSH12,
89   PUSH13,
90   PUSH14,
91   PUSH15,
92   PUSH16,
93   PUSH17,
94   PUSH18,
95   PUSH19,
96   PUSH20,
97   PUSH21,
98   PUSH22,
99   PUSH23,
100   PUSH24,
101   PUSH25,
102   PUSH26,
103   PUSH27,
104   PUSH28,
105   PUSH29,
106   PUSH30,
107   PUSH31,
108   PUSH32,
109   DUP1,
110   DUP2,
111   DUP3,
112   DUP4,
113   DUP5,
114   DUP6,
115   DUP7,
116   DUP8,
117   DUP9,
118   DUP10,
119   DUP11,
120   DUP12,
121   DUP13,
122   DUP14,
123   DUP15,
124   DUP16,
125   SWAP1,
126   SWAP2,
127   SWAP3,
128   SWAP4,
129   SWAP5,
130   SWAP6,
131   SWAP7,
132   SWAP8,
133   SWAP9,
134   SWAP10,
135   SWAP11,
136   SWAP12,
137   SWAP13,
138   SWAP14,
139   SWAP15,
140   SWAP16,
141 
142   LOG0 = 0xa0,
143   LOG1,
144   LOG2,
145   LOG3,
146   LOG4,
147 
148   CREATE = 0xf0,
149   CALL,
150   CALLCODE,
151   RETURN,
152   DELEGATECALL,
153   SELFDESTRUCT = 0xff
154 }
155 
156 /**
157  * Returns: An associative array from bytecode values to opcode names.
158  */
159 pure string[ubyte] generateBytecodeToOpcodeNameMap() {
160     string[ubyte] results;
161 
162     foreach (immutable opcode; [EnumMembers!Opcode]) {
163         results[opcode] = std.conv.to!string(opcode);
164     }
165 
166     return results;
167 }
168 
169 /**
170  * Returns: An associative array from opcode names to bytecode values.
171  */
172 pure ubyte[string] generateOpcodeNameToBytecodeMap() {
173   ubyte[string] results;
174 
175   foreach (immutable opcode; [EnumMembers!Opcode]) {
176     results[std.conv.to!string(opcode)] = opcode;
177   }
178 
179   return results;
180 }