Skip to content

Commit 8bab7e0

Browse files
committed
use "reduce to", add footnotes
1 parent 5f680e1 commit 8bab7e0

File tree

1 file changed

+22
-17
lines changed

1 file changed

+22
-17
lines changed

evm_vs_ewasm_opcode_table.md

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
## EVM opcodes vs ewasm methods
22

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.).
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 represent generic machine instructions (ADD, MUL, ISZERO, XOR, etc.) and the rest are Ethereum-specific operations (SLOAD, SSTORE, GASPRICE, BLOCKHASH, etc.).
44

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](https://webassembly.github.io/threads/exec/runtime.html#syntax-hostfunc) 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.
5+
When EVM bytecode is transpiled to ewasm bytecode, the Ethereum-specific opcodes translate to corresponding ewasm interface methods (Ethereum Environment Interface (EEI) methods). These interface methods are provided as [host functions](https://webassembly.github.io/threads/exec/runtime.html#syntax-hostfunc) to the wasm VM. All other EVM opcodes are not Ethereum-specific and do not access data from the Ethereum environment, so they reduce to pure wasm instructions.
66

77
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).
88

99
To reference ewasm interface methods, see the [EEI spec](https://github.com/ewasm/design/blob/master/eth_interface.md).
1010

1111

12-
### EVM opcodes that translate to ewasm methods (Ethereum Environment Interface (EEI) methods)
12+
### EVM opcodes with corresponding ewasm methods (Ethereum Environment Interface (EEI) methods)
1313

1414
EVM opcode | ewasm interface method
1515
----------------------|-----------------------------
@@ -26,8 +26,8 @@ EVM opcode | ewasm interface method
2626
0x3a: GASPRICE | getTxGasPrice
2727
0x3b: EXTCODESIZE | getExternalCodeSize
2828
0x3c: EXTCODECOPY | externalCodeCopy
29-
0x3d: RETURNDATASIZ | getReturnDataSize
30-
0x3e: RETURNDATACOP | returnDataCopy
29+
0x3d: RETURNDATASIZE | getReturnDataSize
30+
0x3e: RETURNDATACOPY | returnDataCopy
3131
0x40: BLOCKHASH | getBlockHash
3232
0x41: COINBASE | getBlockCoinbase
3333
0x42: TIMESTAMP | getBlockTimestamp
@@ -36,7 +36,7 @@ EVM opcode | ewasm interface method
3636
0x45: GASLIMIT | getBlockGasLimit
3737
0x54: SLOAD | storageLoad
3838
0x55: SSTORE | storageStore
39-
0x58: PC | -
39+
0x58: PC | -<sup>[](#f1)</sup>
4040
0x5a: GAS | getGasLeft
4141
0xa0: LOG0 | log(0,..)
4242
... |
@@ -51,6 +51,8 @@ EVM opcode | ewasm interface method
5151
0xff: SELFDESTRUCT | selfDestruct
5252

5353

54+
†. <small id="f1">In EVM, the Program Counter (PC) opcode returns the currently executing bytecode position. When EVM is transpiled to ewasm with evm2wasm, this bytecode position is calculated by the transpiler and [inserted as a wasm constant](https://github.com/ewasm/evm2wasm/blob/80136977553c9b22e385f919fff808ef8a54be95/index.js#L247-L248). For ewasm code that is not transpiled from EVM, there is no interface method corresponding to the `PC` opcode.</small>
55+
5456

5557
## EVM opcodes vs WebAssembly instructions
5658

@@ -72,9 +74,9 @@ ewasm excludes the wasm floating point instructions:
7274
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).
7375

7476

75-
### EVM opcodes that translate to wasm instructions
77+
### EVM opcodes that reduce to wasm instructions
7678

77-
EVM opcode | wasm instruction (i32/i64)
79+
EVM opcode (256-bit) | wasm instruction i32/i64 (32-bit or 64-bit)
7880
----------------------|----------------------
7981
0x00: STOP | end
8082
0x01: ADD | i32.add
@@ -84,10 +86,10 @@ EVM opcode | wasm instruction (i32/i64)
8486
0x05: SDIV | i32.div_s
8587
0x06: MOD | i32.rem_u
8688
0x07: SMOD | i32.rem_s
87-
0x08: ADDMOD | -
88-
0x09: MULMOD | -
89-
0x0a: EXP | -
90-
0x0b: SIGNEXTEND | -
89+
0x08: ADDMOD | -<sup>[](#f2)</sup>
90+
0x09: MULMOD | -<sup>[](#f2)</sup>
91+
0x0a: EXP | -<sup>[](#f2)</sup>
92+
0x0b: SIGNEXTEND | -<sup>[](#f2)</sup>
9193
0x10: LT | i32.lt_u
9294
0x11: GT | i32.gt_u
9395
0x12: SLT | i32.lt_s
@@ -97,9 +99,9 @@ EVM opcode | wasm instruction (i32/i64)
9799
0x16: AND | i32.and
98100
0x17: OR | i32.or
99101
0x18: XOR | i32.xor
100-
0x19: NOT | -
101-
0x1a: BYTE | - [i32.load8]
102-
0x20: SHA3 | -
102+
0x19: NOT | i32.sub (i32.const 0xffffffff)
103+
0x1a: BYTE | i32.load8
104+
0x20: SHA3 | -<sup>[](#f2)</sup>
103105
0x50: POP | drop
104106
0x51: MLOAD | i32.load
105107
0x52: MSTORE | i32.store
@@ -115,6 +117,9 @@ EVM opcode | wasm instruction (i32/i64)
115117
0x80: DUP1 | get_global, get_local
116118
... |
117119
0x8f: DUP16 | get_global, get_local
118-
0x90: SWAP1 | -
120+
0x90: SWAP1 | get_global, get_local
119121
... |
120-
0x9f: SWAP16 | -
122+
0x9f: SWAP16 | get_global, get_local
123+
124+
125+
‡. <small id="f2">Some EVM opcodes, such as `ADDMOD` (modular addition), `EXP` (exponentiation), or `SHA3` (Keccak-256 hash), do not have simple reductions to single wasm instructions (or to only a few wasm instructions). The functionality of these opcodes is replicated by a relatively large wasm module (for instance, `SHA3` reduces to about [900 lines of wast code](https://github.com/ewasm/evm2wasm/blob/80136977553c9b22e385f919fff808ef8a54be95/wasm/keccak.wast)).</small>

0 commit comments

Comments
 (0)