Skip to content

Commit 5e754bb

Browse files
committed
[revm] Instructions sorted
1 parent 990d76d commit 5e754bb

File tree

16 files changed

+949
-1100
lines changed

16 files changed

+949
-1100
lines changed

Cargo.lock

Lines changed: 170 additions & 45 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bins/revm-test/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ edition = "2018"
77
[dependencies]
88
bytes = "1.1"
99
hex = "0.4"
10-
primitive-types = { version = "0.10", features = ["rlp"] }
10+
primitive-types = { version = "0.11", features = ["rlp"] }
1111
revm = { path = "../../crates/revm" }

bins/revme/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ hashbrown = "0.12"
1616
hex = "0.4"
1717
indicatif = "0.16"
1818
plain_hasher = "0.2"
19-
primitive-types = { version = "0.10", features = ["rlp", "serde"] }
19+
primitive-types = { version = "0.11", features = ["rlp", "serde"] }
2020

2121
revm = { path = "../../crates/revm", version = "1.1", default-features = false, features = ["web3db","std","k256"] }
2222
rlp = { version = "0.5", default-features = false }

crates/revm/Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@ bytes = { version = "1.1", default-features = false }
1616
futures = { version = "0.3.17", optional = true }
1717
hashbrown = { version = "0.12" }
1818
num_enum = { version = "0.5", default-features = false }#used for SpecId from u8 cast
19-
parking_lot = { version = "0.11.2", optional = true }
20-
primitive-types = { version = "0.10", default-features = false, features = ["rlp"] }
19+
parking_lot = { version = "0.12", optional = true }
20+
primitive-types = { version = "0.11", default-features = false, features = ["rlp"] }
2121
revm_precompiles = { path = "../revm_precompiles", version = "0.4", default-features = false }
2222
rlp = { version = "0.5", default-features = false }#used for create2 address calculation
2323
sha3 = { version = "0.10", default-features = false }
2424
tokio = { version = "1.14", features = ["rt-multi-thread", "macros"], optional = true }
25-
web3 = { version = "0.17", optional = true }
25+
web3 = { version = "0.18", optional = true }
2626
#crypto-bigint = "0.3"
2727
zkp-u256 = "0.2.1"
2828

crates/revm/src/instructions.rs

Lines changed: 114 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@
22
mod macros;
33
mod arithmetic;
44
mod bitwise;
5+
mod control;
6+
mod host;
7+
mod host_env;
58
mod i256;
6-
mod misc;
9+
mod memory;
710
pub mod opcode;
11+
mod stack;
812
mod system;
913

1014
pub use opcode::{OpCode, OPCODE_JUMPMAP};
@@ -120,123 +124,123 @@ pub fn eval<H: Host, S: Spec>(opcode: u8, machine: &mut Interpreter, host: &mut
120124
opcode::SHA3 => system::sha3(machine),
121125

122126
opcode::ADDRESS => system::address(machine),
123-
opcode::BALANCE => system::balance::<H, S>(machine, host),
124-
opcode::SELFBALANCE => system::selfbalance::<H, S>(machine, host),
125-
opcode::CODESIZE => misc::codesize(machine),
126-
opcode::CODECOPY => misc::codecopy(machine),
127-
opcode::CALLDATALOAD => misc::calldataload(machine),
128-
opcode::CALLDATASIZE => misc::calldatasize(machine),
129-
opcode::CALLDATACOPY => misc::calldatacopy(machine),
130-
opcode::POP => misc::pop(machine),
131-
opcode::MLOAD => misc::mload(machine),
132-
opcode::MSTORE => misc::mstore(machine),
133-
opcode::MSTORE8 => misc::mstore8(machine),
134-
opcode::JUMP => misc::jump(machine),
135-
opcode::JUMPI => misc::jumpi(machine),
136-
opcode::PC => misc::pc(machine),
137-
opcode::MSIZE => misc::msize(machine),
138-
opcode::JUMPDEST => misc::jumpdest(machine),
139-
opcode::PUSH1 => misc::push::<1>(machine),
140-
opcode::PUSH2 => misc::push::<2>(machine),
141-
opcode::PUSH3 => misc::push::<3>(machine),
142-
opcode::PUSH4 => misc::push::<4>(machine),
143-
opcode::PUSH5 => misc::push::<5>(machine),
144-
opcode::PUSH6 => misc::push::<6>(machine),
145-
opcode::PUSH7 => misc::push::<7>(machine),
146-
opcode::PUSH8 => misc::push::<8>(machine),
147-
opcode::PUSH9 => misc::push::<9>(machine),
148-
opcode::PUSH10 => misc::push::<10>(machine),
149-
opcode::PUSH11 => misc::push::<11>(machine),
150-
opcode::PUSH12 => misc::push::<12>(machine),
151-
opcode::PUSH13 => misc::push::<13>(machine),
152-
opcode::PUSH14 => misc::push::<14>(machine),
153-
opcode::PUSH15 => misc::push::<15>(machine),
154-
opcode::PUSH16 => misc::push::<16>(machine),
155-
opcode::PUSH17 => misc::push::<17>(machine),
156-
opcode::PUSH18 => misc::push::<18>(machine),
157-
opcode::PUSH19 => misc::push::<19>(machine),
158-
opcode::PUSH20 => misc::push::<20>(machine),
159-
opcode::PUSH21 => misc::push::<21>(machine),
160-
opcode::PUSH22 => misc::push::<22>(machine),
161-
opcode::PUSH23 => misc::push::<23>(machine),
162-
opcode::PUSH24 => misc::push::<24>(machine),
163-
opcode::PUSH25 => misc::push::<25>(machine),
164-
opcode::PUSH26 => misc::push::<26>(machine),
165-
opcode::PUSH27 => misc::push::<27>(machine),
166-
opcode::PUSH28 => misc::push::<28>(machine),
167-
opcode::PUSH29 => misc::push::<29>(machine),
168-
opcode::PUSH30 => misc::push::<30>(machine),
169-
opcode::PUSH31 => misc::push::<31>(machine),
170-
opcode::PUSH32 => misc::push::<32>(machine),
171-
opcode::DUP1 => misc::dup::<1>(machine),
172-
opcode::DUP2 => misc::dup::<2>(machine),
173-
opcode::DUP3 => misc::dup::<3>(machine),
174-
opcode::DUP4 => misc::dup::<4>(machine),
175-
opcode::DUP5 => misc::dup::<5>(machine),
176-
opcode::DUP6 => misc::dup::<6>(machine),
177-
opcode::DUP7 => misc::dup::<7>(machine),
178-
opcode::DUP8 => misc::dup::<8>(machine),
179-
opcode::DUP9 => misc::dup::<9>(machine),
180-
opcode::DUP10 => misc::dup::<10>(machine),
181-
opcode::DUP11 => misc::dup::<11>(machine),
182-
opcode::DUP12 => misc::dup::<12>(machine),
183-
opcode::DUP13 => misc::dup::<13>(machine),
184-
opcode::DUP14 => misc::dup::<14>(machine),
185-
opcode::DUP15 => misc::dup::<15>(machine),
186-
opcode::DUP16 => misc::dup::<16>(machine),
127+
opcode::BALANCE => host::balance::<H, S>(machine, host),
128+
opcode::SELFBALANCE => host::selfbalance::<H, S>(machine, host),
129+
opcode::CODESIZE => system::codesize(machine),
130+
opcode::CODECOPY => system::codecopy(machine),
131+
opcode::CALLDATALOAD => system::calldataload(machine),
132+
opcode::CALLDATASIZE => system::calldatasize(machine),
133+
opcode::CALLDATACOPY => system::calldatacopy(machine),
134+
opcode::POP => stack::pop(machine),
135+
opcode::MLOAD => memory::mload(machine),
136+
opcode::MSTORE => memory::mstore(machine),
137+
opcode::MSTORE8 => memory::mstore8(machine),
138+
opcode::JUMP => control::jump(machine),
139+
opcode::JUMPI => control::jumpi(machine),
140+
opcode::PC => control::pc(machine),
141+
opcode::MSIZE => memory::msize(machine),
142+
opcode::JUMPDEST => control::jumpdest(machine),
143+
opcode::PUSH1 => stack::push::<1>(machine),
144+
opcode::PUSH2 => stack::push::<2>(machine),
145+
opcode::PUSH3 => stack::push::<3>(machine),
146+
opcode::PUSH4 => stack::push::<4>(machine),
147+
opcode::PUSH5 => stack::push::<5>(machine),
148+
opcode::PUSH6 => stack::push::<6>(machine),
149+
opcode::PUSH7 => stack::push::<7>(machine),
150+
opcode::PUSH8 => stack::push::<8>(machine),
151+
opcode::PUSH9 => stack::push::<9>(machine),
152+
opcode::PUSH10 => stack::push::<10>(machine),
153+
opcode::PUSH11 => stack::push::<11>(machine),
154+
opcode::PUSH12 => stack::push::<12>(machine),
155+
opcode::PUSH13 => stack::push::<13>(machine),
156+
opcode::PUSH14 => stack::push::<14>(machine),
157+
opcode::PUSH15 => stack::push::<15>(machine),
158+
opcode::PUSH16 => stack::push::<16>(machine),
159+
opcode::PUSH17 => stack::push::<17>(machine),
160+
opcode::PUSH18 => stack::push::<18>(machine),
161+
opcode::PUSH19 => stack::push::<19>(machine),
162+
opcode::PUSH20 => stack::push::<20>(machine),
163+
opcode::PUSH21 => stack::push::<21>(machine),
164+
opcode::PUSH22 => stack::push::<22>(machine),
165+
opcode::PUSH23 => stack::push::<23>(machine),
166+
opcode::PUSH24 => stack::push::<24>(machine),
167+
opcode::PUSH25 => stack::push::<25>(machine),
168+
opcode::PUSH26 => stack::push::<26>(machine),
169+
opcode::PUSH27 => stack::push::<27>(machine),
170+
opcode::PUSH28 => stack::push::<28>(machine),
171+
opcode::PUSH29 => stack::push::<29>(machine),
172+
opcode::PUSH30 => stack::push::<30>(machine),
173+
opcode::PUSH31 => stack::push::<31>(machine),
174+
opcode::PUSH32 => stack::push::<32>(machine),
175+
opcode::DUP1 => stack::dup::<1>(machine),
176+
opcode::DUP2 => stack::dup::<2>(machine),
177+
opcode::DUP3 => stack::dup::<3>(machine),
178+
opcode::DUP4 => stack::dup::<4>(machine),
179+
opcode::DUP5 => stack::dup::<5>(machine),
180+
opcode::DUP6 => stack::dup::<6>(machine),
181+
opcode::DUP7 => stack::dup::<7>(machine),
182+
opcode::DUP8 => stack::dup::<8>(machine),
183+
opcode::DUP9 => stack::dup::<9>(machine),
184+
opcode::DUP10 => stack::dup::<10>(machine),
185+
opcode::DUP11 => stack::dup::<11>(machine),
186+
opcode::DUP12 => stack::dup::<12>(machine),
187+
opcode::DUP13 => stack::dup::<13>(machine),
188+
opcode::DUP14 => stack::dup::<14>(machine),
189+
opcode::DUP15 => stack::dup::<15>(machine),
190+
opcode::DUP16 => stack::dup::<16>(machine),
187191

188-
opcode::SWAP1 => misc::swap::<1>(machine),
189-
opcode::SWAP2 => misc::swap::<2>(machine),
190-
opcode::SWAP3 => misc::swap::<3>(machine),
191-
opcode::SWAP4 => misc::swap::<4>(machine),
192-
opcode::SWAP5 => misc::swap::<5>(machine),
193-
opcode::SWAP6 => misc::swap::<6>(machine),
194-
opcode::SWAP7 => misc::swap::<7>(machine),
195-
opcode::SWAP8 => misc::swap::<8>(machine),
196-
opcode::SWAP9 => misc::swap::<9>(machine),
197-
opcode::SWAP10 => misc::swap::<10>(machine),
198-
opcode::SWAP11 => misc::swap::<11>(machine),
199-
opcode::SWAP12 => misc::swap::<12>(machine),
200-
opcode::SWAP13 => misc::swap::<13>(machine),
201-
opcode::SWAP14 => misc::swap::<14>(machine),
202-
opcode::SWAP15 => misc::swap::<15>(machine),
203-
opcode::SWAP16 => misc::swap::<16>(machine),
192+
opcode::SWAP1 => stack::swap::<1>(machine),
193+
opcode::SWAP2 => stack::swap::<2>(machine),
194+
opcode::SWAP3 => stack::swap::<3>(machine),
195+
opcode::SWAP4 => stack::swap::<4>(machine),
196+
opcode::SWAP5 => stack::swap::<5>(machine),
197+
opcode::SWAP6 => stack::swap::<6>(machine),
198+
opcode::SWAP7 => stack::swap::<7>(machine),
199+
opcode::SWAP8 => stack::swap::<8>(machine),
200+
opcode::SWAP9 => stack::swap::<9>(machine),
201+
opcode::SWAP10 => stack::swap::<10>(machine),
202+
opcode::SWAP11 => stack::swap::<11>(machine),
203+
opcode::SWAP12 => stack::swap::<12>(machine),
204+
opcode::SWAP13 => stack::swap::<13>(machine),
205+
opcode::SWAP14 => stack::swap::<14>(machine),
206+
opcode::SWAP15 => stack::swap::<15>(machine),
207+
opcode::SWAP16 => stack::swap::<16>(machine),
204208

205-
opcode::RETURN => misc::ret(machine),
206-
opcode::REVERT => misc::revert::<S>(machine),
209+
opcode::RETURN => control::ret(machine),
210+
opcode::REVERT => control::revert::<S>(machine),
207211
opcode::INVALID => Return::InvalidOpcode,
208-
opcode::BASEFEE => system::basefee::<H, S>(machine, host),
209-
opcode::ORIGIN => system::origin(machine, host),
212+
opcode::BASEFEE => host_env::basefee::<H, S>(machine, host),
213+
opcode::ORIGIN => host_env::origin(machine, host),
210214
opcode::CALLER => system::caller(machine),
211215
opcode::CALLVALUE => system::callvalue(machine),
212-
opcode::GASPRICE => system::gasprice(machine, host),
213-
opcode::EXTCODESIZE => system::extcodesize::<H, S>(machine, host),
214-
opcode::EXTCODEHASH => system::extcodehash::<H, S>(machine, host),
215-
opcode::EXTCODECOPY => system::extcodecopy::<H, S>(machine, host),
216+
opcode::GASPRICE => host_env::gasprice(machine, host),
217+
opcode::EXTCODESIZE => host::extcodesize::<H, S>(machine, host),
218+
opcode::EXTCODEHASH => host::extcodehash::<H, S>(machine, host),
219+
opcode::EXTCODECOPY => host::extcodecopy::<H, S>(machine, host),
216220
opcode::RETURNDATASIZE => system::returndatasize::<S>(machine),
217221
opcode::RETURNDATACOPY => system::returndatacopy::<S>(machine),
218-
opcode::BLOCKHASH => system::blockhash(machine, host),
219-
opcode::COINBASE => system::coinbase(machine, host),
220-
opcode::TIMESTAMP => system::timestamp(machine, host),
221-
opcode::NUMBER => system::number(machine, host),
222-
opcode::DIFFICULTY => system::difficulty(machine, host),
223-
opcode::GASLIMIT => system::gaslimit(machine, host),
224-
opcode::SLOAD => system::sload::<H, S>(machine, host),
225-
opcode::SSTORE => system::sstore::<H, S>(machine, host),
222+
opcode::BLOCKHASH => host::blockhash(machine, host),
223+
opcode::COINBASE => host_env::coinbase(machine, host),
224+
opcode::TIMESTAMP => host_env::timestamp(machine, host),
225+
opcode::NUMBER => host_env::number(machine, host),
226+
opcode::DIFFICULTY => host_env::difficulty(machine, host),
227+
opcode::GASLIMIT => host_env::gaslimit(machine, host),
228+
opcode::SLOAD => host::sload::<H, S>(machine, host),
229+
opcode::SSTORE => host::sstore::<H, S>(machine, host),
226230
opcode::GAS => system::gas(machine),
227-
opcode::LOG0 => system::log::<H, S>(machine, 0, host),
228-
opcode::LOG1 => system::log::<H, S>(machine, 1, host),
229-
opcode::LOG2 => system::log::<H, S>(machine, 2, host),
230-
opcode::LOG3 => system::log::<H, S>(machine, 3, host),
231-
opcode::LOG4 => system::log::<H, S>(machine, 4, host),
232-
opcode::SELFDESTRUCT => system::selfdestruct::<H, S>(machine, host),
233-
opcode::CREATE => system::create::<H, S>(machine, false, host), //check
234-
opcode::CREATE2 => system::create::<H, S>(machine, true, host), //check
235-
opcode::CALL => system::call::<H, S>(machine, CallScheme::Call, host), //check
236-
opcode::CALLCODE => system::call::<H, S>(machine, CallScheme::CallCode, host), //check
237-
opcode::DELEGATECALL => system::call::<H, S>(machine, CallScheme::DelegateCall, host), //check
238-
opcode::STATICCALL => system::call::<H, S>(machine, CallScheme::StaticCall, host), //check
239-
opcode::CHAINID => system::chainid::<H, S>(machine, host),
231+
opcode::LOG0 => host::log::<H, S>(machine, 0, host),
232+
opcode::LOG1 => host::log::<H, S>(machine, 1, host),
233+
opcode::LOG2 => host::log::<H, S>(machine, 2, host),
234+
opcode::LOG3 => host::log::<H, S>(machine, 3, host),
235+
opcode::LOG4 => host::log::<H, S>(machine, 4, host),
236+
opcode::SELFDESTRUCT => host::selfdestruct::<H, S>(machine, host),
237+
opcode::CREATE => host::create::<H, S>(machine, false, host), //check
238+
opcode::CREATE2 => host::create::<H, S>(machine, true, host), //check
239+
opcode::CALL => host::call::<H, S>(machine, CallScheme::Call, host), //check
240+
opcode::CALLCODE => host::call::<H, S>(machine, CallScheme::CallCode, host), //check
241+
opcode::DELEGATECALL => host::call::<H, S>(machine, CallScheme::DelegateCall, host), //check
242+
opcode::STATICCALL => host::call::<H, S>(machine, CallScheme::StaticCall, host), //check
243+
opcode::CHAINID => host_env::chainid::<H, S>(machine, host),
240244
_ => Return::OpcodeNotFound,
241245
}
242246
}

crates/revm/src/instructions/arithmetic.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ pub fn div(op1: U256, op2: U256) -> U256 {
88
if op2.is_zero() {
99
U256::zero()
1010
} else {
11-
//op1 / op2
12-
super::i256::div_u256::div_mod(op1, op2).0
11+
op1 / op2
1312
}
1413
}
1514

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
use crate::{gas, interpreter::Interpreter, Return, Spec, SpecId::*};
2+
use primitive_types::U256;
3+
4+
pub fn jump(machine: &mut Interpreter) -> Return {
5+
//gas!(machine, gas::MID);
6+
7+
pop!(machine, dest);
8+
let dest = as_usize_or_fail!(dest, Return::InvalidJump);
9+
10+
if machine.contract.is_valid_jump(dest) {
11+
// Safety: In analazis we are checking create our jump table and we do check above to be
12+
// sure that jump is safe to execute.
13+
machine.program_counter = unsafe { machine.contract.code.as_ptr().add(dest) };
14+
Return::Continue
15+
} else {
16+
Return::InvalidJump
17+
}
18+
}
19+
20+
pub fn jumpi(machine: &mut Interpreter) -> Return {
21+
//gas!(machine, gas::HIGH);
22+
23+
pop!(machine, dest, value);
24+
25+
if !value.is_zero() {
26+
let dest = as_usize_or_fail!(dest, Return::InvalidJump);
27+
if machine.contract.is_valid_jump(dest) {
28+
// Safety: In analazis we are checking if jump is valid destination and this if.
29+
// make this unsafe block safe.
30+
machine.program_counter = unsafe { machine.contract.code.as_ptr().add(dest) };
31+
Return::Continue
32+
} else {
33+
Return::InvalidJump
34+
}
35+
} else {
36+
// if we are not doing jump, add next gas block.
37+
machine.add_next_gas_block(machine.program_counter() - 1)
38+
}
39+
}
40+
41+
pub fn jumpdest(machine: &mut Interpreter) -> Return {
42+
gas!(machine, gas::JUMPDEST);
43+
machine.add_next_gas_block(machine.program_counter() - 1)
44+
}
45+
46+
pub fn pc(machine: &mut Interpreter) -> Return {
47+
//gas!(machine, gas::BASE);
48+
push!(machine, U256::from(machine.program_counter() - 1));
49+
Return::Continue
50+
}
51+
52+
pub fn ret(machine: &mut Interpreter) -> Return {
53+
// zero gas cost gas!(machine,gas::ZERO);
54+
pop!(machine, start, len);
55+
let len = as_usize_or_fail!(len, Return::OutOfGas);
56+
if len == 0 {
57+
machine.return_range = usize::MAX..usize::MAX;
58+
} else {
59+
let offset = as_usize_or_fail!(start, Return::OutOfGas);
60+
memory_resize!(machine, offset, len);
61+
machine.return_range = offset..(offset + len);
62+
}
63+
Return::Return
64+
}
65+
66+
pub fn revert<SPEC: Spec>(machine: &mut Interpreter) -> Return {
67+
check!(SPEC::enabled(BYZANTINE)); // EIP-140: REVERT instruction
68+
// zero gas cost gas!(machine,gas::ZERO);
69+
pop!(machine, start, len);
70+
let len = as_usize_or_fail!(len, Return::OutOfGas);
71+
if len == 0 {
72+
machine.return_range = usize::MAX..usize::MAX;
73+
} else {
74+
let offset = as_usize_or_fail!(start, Return::OutOfGas);
75+
memory_resize!(machine, offset, len);
76+
machine.return_range = offset..(offset + len);
77+
}
78+
Return::Revert
79+
}

0 commit comments

Comments
 (0)