Skip to content

Commit f294d46

Browse files
committed
EIP-4200: EOF - Static relative jumps
1 parent f02c20c commit f294d46

File tree

6 files changed

+86
-2
lines changed

6 files changed

+86
-2
lines changed

nimbus/common/evmforks.nim

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,7 @@ const
2828
FkParis* = EVMC_PARIS
2929
FkShanghai* = EVMC_SHANGHAI
3030
FkCancun* = EVMC_CANCUN
31+
32+
33+
# Meta forks related to specific EIP
34+
FkEOF* = FkCancun

nimbus/evm/code_stream.nim

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,14 @@ proc readVmWord*(c: var CodeStream, n: int): UInt256 =
6464
for i in 0 ..< toWrite : result_bytes[i] = c.bytes[last - i - 1]
6565
c.pc = last
6666

67+
proc readInt16*(c: var CodeStream): int =
68+
result = (c.bytes[c.pc].int shl 8) or c.bytes[c.pc+1].int
69+
c.pc += 2
70+
71+
proc readByte*(c: var CodeStream): byte =
72+
result = c.bytes[c.pc]
73+
inc c.pc
74+
6775
proc len*(c: CodeStream): int =
6876
len(c.bytes)
6977

nimbus/evm/interpreter/gas_costs.nim

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ type
2020
GasZero, # Nothing paid for operations of the set Wzero.
2121
GasBase, # Amount of gas to pay for operations of the set Wbase.
2222
GasVeryLow, # Amount of gas to pay for operations of the set Wverylow.
23+
GasMidLow, # Introduced in Shanghai EIP4200
2324
GasLow, # Amount of gas to pay for operations of the set Wlow.
2425
GasMid, # Amount of gas to pay for operations of the set Wmid.
2526
GasHigh, # Amount of gas to pay for operations of the set Whigh.
@@ -712,6 +713,11 @@ template gasCosts(fork: EVMFork, prefix, ResultGasCostsName: untyped) =
712713
Log3: memExpansion `prefix gasLog3`,
713714
Log4: memExpansion `prefix gasLog4`,
714715

716+
# e0s: Static jumps
717+
RJump: fixed GasBase,
718+
RJumpI: fixed GasMidLow,
719+
RJumpV: fixed GasMidLow,
720+
715721
# f0s: System operations
716722
Create: complex `prefix gasCreate`,
717723
Call: complex `prefix gasCall`,
@@ -732,6 +738,7 @@ const
732738
GasZero: 0'i64,
733739
GasBase: 2,
734740
GasVeryLow: 3,
741+
GasMidLow: 4, # Introduced in Shanghai (EIP4200)
735742
GasLow: 5,
736743
GasMid: 8,
737744
GasHigh: 10,

nimbus/evm/interpreter/op_codes.nim

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ type
127127
JumpDest = 0x5b, ## Mark a valid destination for jumps. This
128128
## operation has no effect on machine state during
129129
## execution.
130+
130131
Tload = 0x5c, ## Load word from transient storage.
131132
Tstore = 0x5d, ## Save word to transient storage.
132133

@@ -182,8 +183,13 @@ type
182183
Nop0xC9, Nop0xCA, Nop0xCB, Nop0xCC, Nop0xCD, Nop0xCE,
183184
Nop0xCF, Nop0xD0, Nop0xD1, Nop0xD2, Nop0xD3, Nop0xD4,
184185
Nop0xD5, Nop0xD6, Nop0xD7, Nop0xD8, Nop0xD9, Nop0xDA,
185-
Nop0xDB, Nop0xDC, Nop0xDD, Nop0xDE, Nop0xDF, Nop0xE0,
186-
Nop0xE1, Nop0xE2, Nop0xE3, Nop0xE4, Nop0xE5, Nop0xE6,
186+
Nop0xDB, Nop0xDC, Nop0xDD, Nop0xDE, Nop0xDF,
187+
188+
Rjump = 0xe0, ## Relative jump (EIP4200)
189+
RJumpI = 0xe1, ## Conditional relative jump (EIP4200)
190+
RJumpV = 0xe2, ## Relative jump via jump table (EIP4200)
191+
192+
Nop0xE3, Nop0xE4, Nop0xE5, Nop0xE6,
187193
Nop0xE7, Nop0xE8, Nop0xE9, Nop0xEA, Nop0xEB, Nop0xEC,
188194
Nop0xED, Nop0xEE, Nop0xEF, ## ..
189195

nimbus/evm/interpreter/op_handlers/oph_defs.nim

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ const
8989
Vm2OpCancunAndLater* =
9090
Vm2OpShanghaiAndLater - {FkShanghai}
9191

92+
Vm2OpEOFAndLater* = Vm2OpCancunAndLater
93+
9294
# ------------------------------------------------------------------------------
9395
# End
9496
# ------------------------------------------------------------------------------

nimbus/evm/interpreter/op_handlers/oph_memory.nim

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,39 @@ const
334334

335335
k.cpt.memory.copy(dstPos, srcPos, len)
336336

337+
rjumpOp: Vm2OpFn = proc (k: var Vm2Ctx) =
338+
## 0xe0, Relative jump (EIP4200)
339+
let relativeOffset = k.cpt.code.readInt16()
340+
k.cpt.code.pc += relativeOffset
341+
342+
rjumpiOp: Vm2OpFn = proc (k: var Vm2Ctx) =
343+
## 0xe1, Conditional relative jump (EIP4200)
344+
let condition = k.cpt.stack.popInt()
345+
if condition.isZero:
346+
# Not branching, just skip over immediate argument.
347+
k.cpt.code.pc += 2
348+
return
349+
350+
k.rjumpOp()
351+
352+
rjumpvOp: Vm2OpFn = proc (k: var Vm2Ctx) =
353+
## 0xe2, Relative jump via jump table (EIP4200)
354+
let count = k.cpt.code.readByte().int
355+
let savedPC = k.cpt.code.pc
356+
let idx = k.cpt.stack.popInt().truncate(int)
357+
if idx >= count:
358+
# Index out-of-bounds, don't branch, just skip over immediate
359+
# argument.
360+
k.cpt.code.pc += count * 2
361+
return
362+
363+
# get jump table entry
364+
k.cpt.code.pc += idx * 2
365+
let relativeOffset = k.cpt.code.readInt16()
366+
367+
# move pc past operand(1 + count * 2) and relativeOffset
368+
k.cpt.code.pc = savedPC + count * 2 + relativeOffset
369+
337370
#[
338371
EIP-2315: temporary disabled
339372
Reason : not included in berlin hard fork
@@ -546,6 +579,30 @@ const
546579
info: "Copy memory",
547580
exec: (prep: vm2OpIgnore,
548581
run: mCopyOp,
582+
post: vm2OpIgnore)),
583+
584+
(opCode: RJump, ## 0xe0, Relative jump (EIP4200)
585+
forks: Vm2OpEOFAndLater,
586+
name: "RJump",
587+
info: "Relative jump via jump table",
588+
exec: (prep: vm2OpIgnore,
589+
run: rjumpOp,
590+
post: vm2OpIgnore)),
591+
592+
(opCode: RJumpI, ## 0xe1, Conditional relative jump (EIP4200)
593+
forks: Vm2OpEOFAndLater,
594+
name: "RJumpI",
595+
info: "Relative jump via jump table",
596+
exec: (prep: vm2OpIgnore,
597+
run: rjumpiOp,
598+
post: vm2OpIgnore)),
599+
600+
(opCode: RJumpV, ## 0xe2, Relative jump via jump table (EIP4200)
601+
forks: Vm2OpEOFAndLater,
602+
name: "RJumpV",
603+
info: "Relative jump via jump table",
604+
exec: (prep: vm2OpIgnore,
605+
run: rjumpvOp,
549606
post: vm2OpIgnore))]
550607

551608
#[

0 commit comments

Comments
 (0)