Skip to content

Commit 48bf567

Browse files
committed
comparison tables for EVM opcodes to ewasm methods and wasm instructions
1 parent 79f52fe commit 48bf567

File tree

1 file changed

+120
-0
lines changed

1 file changed

+120
-0
lines changed

evm_vs_ewasm_opcode_table.md

+120
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
## EVM opcodes vs ewasm methods
2+
3+
These tables compares EVM opcodes with ewasm methods and wasm instructions. The EVM defines 134 opcodes (as of the Byzantium hard fork). Of these 134 opcodes, some are generic machine instructions (ADD, MUL, ISZERO, XOR, etc.) and the rest are Ethereum-specific opcodes (SLOAD, SSTORE, CALLVALUE, BLOCKHASH, etc.).
4+
5+
When EVM bytecode is translated into ewasm bytecode, the Ethereum-specific opcodes translate to ewasm interface methods (Ethereum Environment Interface (EEI) methods). These interface methods are provided as "host functions" to the wasm VM. All other EVM opcodes are not Ethereum-specific and do not access data from the Ethereum environment, so they can be translated to plain wasm instructions.
6+
7+
To reference EVM opcodes, try the [yellow paper](https://ethereum.github.io/yellowpaper/paper.pdf), the [readable yellow paper](https://github.com/chronaeon/beigepaper/blob/master/beigepaper.pdf), or the [pyethereum implementation](https://github.com/ethereum/pyethereum/blob/develop/ethereum/opcodes.py).
8+
9+
To reference ewasm interface methods, see the [EEI spec](https://github.com/ewasm/design/blob/master/eth_interface.md).
10+
11+
12+
### EVM opcodes that translate to ewasm methods (Ethereum Environment Interface (EEI) methods)
13+
14+
EVM opcode | ewasm interface method
15+
----------------------|-----------------------------
16+
0x30: ADDRESS | getAddress
17+
0x31: BALANCE | getBalance
18+
0x32: ORIGIN | getTxOrigin
19+
0x33: CALLER | getCaller
20+
0x34: CALLVALUE | getCallValue
21+
0x35: CALLDATALOAD | callDataCopy(resultOffset, dataOffset, 32)
22+
0x36: CALLDATASIZE | getCallDataSize
23+
0x37: CALLDATACOPY | callDataCopy(resultOffset, dataOffset, size)
24+
0x38: CODESIZE | getCodeSize
25+
0x39: CODECOPY | codeCopy
26+
0x3a: GASPRICE | getTxGasPrice
27+
0x3b: EXTCODESIZE | getExternalCodeSize
28+
0x3c: EXTCODECOPY | externalCodeCopy
29+
0x3d: RETURNDATASIZ | getReturnDataSize
30+
0x3e: RETURNDATACOP | returnDataCopy
31+
0x40: BLOCKHASH | getBlockHash
32+
0x41: COINBASE | getBlockCoinbase
33+
0x42: TIMESTAMP | getBlockTimestamp
34+
0x43: NUMBER | getBlockNumber
35+
0x44: DIFFICULTY | getBlockDifficulty
36+
0x45: GASLIMIT | getBlockGasLimit
37+
0x54: SLOAD | storageLoad
38+
0x55: SSTORE | storageStore
39+
0x58: PC | -
40+
0x5a: GAS | getGasLeft
41+
0xa0: LOG0 | log(0,..)
42+
... |
43+
0xa4: LOG4 | log(4,..)
44+
0xf0: CREATE | create
45+
0xf1: CALL | call
46+
0xf2: CALLCODE | callCode
47+
0xf3: RETURN | return
48+
0xf4: DELEGATECALL | callDelegate
49+
0xfa: STATICCALL | callStatic
50+
0xfd: REVERT | revert
51+
0xff: SELFDESTRUCT | selfDestruct
52+
53+
54+
55+
## EVM opcodes vs WebAssembly instructions
56+
57+
Whereas EVM is an untyped VM with a 256-bit word size, WebAssembly is a typed VM with 32-bit and 64-bit word sizes. The two wasm base types are integers and floats, which when combined with the two word sizes, create four types: i32, i64, f32, f64.
58+
59+
Ethereum WebAssembly, or ewasm, is a subset of wasm. Only the integer types (i32 and i64) are supported, floating point types and floating point instructions are not supported. In total, 60 wasm instructions are supported in ewasm:
60+
* 10 control flow instructions
61+
* 11 basic instructions
62+
* 19 integer arithmetic instructions
63+
* 10 integer comparison instructions
64+
* 3 integer conversion instructions
65+
* 7 memory instructions (load, store, extend)
66+
67+
ewasm excludes the wasm floating point instructions:
68+
* 14 floating point instructions
69+
* 6 floating point comparison instructions
70+
* 7 floating point conversion instructions
71+
72+
To reference wasm instructions, see the [wasm spec](https://github.com/WebAssembly/design/blob/master/Semantics.md) or [reference manual](https://github.com/sunfishcode/wasm-reference-manual/blob/master/WebAssembly.md#instructions).
73+
74+
75+
### EVM opcodes that translate to wasm instructions
76+
77+
EVM opcode | wasm instruction (i32/i64)
78+
----------------------|----------------------
79+
0x00: STOP | end
80+
0x01: ADD | i32.add
81+
0x02: MUL | i32.mul
82+
0x03: SUB | i32.sub
83+
0x04: DIV | i32.div_u
84+
0x05: SDIV | i32.div_s
85+
0x06: MOD | i32.rem_u
86+
0x07: SMOD | i32.rem_s
87+
0x08: ADDMOD | -
88+
0x09: MULMOD | -
89+
0x0a: EXP | -
90+
0x0b: SIGNEXTEND | -
91+
0x10: LT | i32.lt_u
92+
0x11: GT | i32.gt_u
93+
0x12: SLT | i32.lt_s
94+
0x13: SGT | i32.gt_s
95+
0x14: EQ | i32.eq
96+
0x15: ISZERO | i32.eqz
97+
0x16: AND | i32.and
98+
0x17: OR | i32.or
99+
0x18: XOR | i32.xor
100+
0x19: NOT | -
101+
0x1a: BYTE | - [i32.load8]
102+
0x20: SHA3 | -
103+
0x50: POP | drop
104+
0x51: MLOAD | i32.load
105+
0x52: MSTORE | i32.store
106+
0x53: MSTORE8 | i32.store
107+
0x56: JUMP | br
108+
0x57: JUMPI | br_if, br_table
109+
0x59: MSIZE | current_memory
110+
0x5b: JUMPDEST | block, loop
111+
0xfe: INVALID | unreachable
112+
0x60: PUSH1 | i32.const
113+
... |
114+
0x7f: PUSH32 | i32.const
115+
0x80: DUP1 | get_global, get_local
116+
... |
117+
0x8f: DUP16 | get_global, get_local
118+
0x90: SWAP1 | -
119+
... |
120+
0x9f: SWAP16 | -

0 commit comments

Comments
 (0)