Skip to content

Commit b940439

Browse files
committed
EIP-4200: EOF - Static relative jumps
1 parent bdaeedb commit b940439

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
@@ -330,6 +330,39 @@ const
330330

331331
k.cpt.memory.copy(dstPos, srcPos, len)
332332

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

547604
#[

0 commit comments

Comments
 (0)