Skip to content

Commit

Permalink
Move memory expansion to function
Browse files Browse the repository at this point in the history
  • Loading branch information
ManuelBilbao committed Apr 10, 2024
1 parent cffd0cf commit db9cd58
Showing 1 changed file with 20 additions and 39 deletions.
59 changes: 20 additions & 39 deletions system-contracts/contracts/EvmInterpreter.yul
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,20 @@ object "EVMInterpreter" {
}
}

function expandMemory(end) -> gasCost {
gasCost := 3

let currentEnd := mload(MEM_OFFSET())
if gt(end, currentEnd) {
let newSize := roundUp(end, 32)
for {} lt(currentEnd, newSize) { currentEnd := add(currentEnd, 32) } {
gasCost := add(gasCost, 3)
mstore(currentEnd, 0)
}
mstore(MEM_OFFSET(), newSize)
}
}

// Essentially a NOP that will not get optimized away by the compiler
function $llvm_NoInline_llvm$_unoptimized() {
pop(1)
Expand Down Expand Up @@ -368,65 +382,32 @@ object "EVMInterpreter" {

offset, sp := popStackItem(sp)

let gasCounter := 3

let end := add(offset, 32)
let currentEnd := mload(MEM_OFFSET())
if gt(end, currentEnd) {
let newSize := roundUp(end, 32)
for {} lt(currentEnd, newSize) { currentEnd := add(currentEnd, 32) } {
gasCounter := add(gasCounter, 3)
mstore(currentEnd, 0)
}
mstore(MEM_OFFSET(), newSize)
}
let expansionGas := expandMemory(add(offset, 32))

sp := pushStackItem(sp, mload(add(MEM_OFFSET_INNER(), offset)))
evmGasLeft := chargeGas(evmGasLeft, gasCounter)
evmGasLeft := chargeGas(evmGasLeft, add(3, expansionGas))
}
case 0x52 { // OP_MSTORE
let offset, value

offset, sp := popStackItem(sp)
value, sp := popStackItem(sp)

let gasCounter := 3

let end := add(offset, 32)
let currentEnd := mload(MEM_OFFSET())
if gt(end, currentEnd) {
let newSize := roundUp(end, 32)
for {} lt(currentEnd, newSize) { currentEnd := add(currentEnd, 32) } {
gasCounter := add(gasCounter, 3)
mstore(currentEnd, 0)
}
mstore(MEM_OFFSET(), newSize)
}
let expansionGas := expandMemory(add(offset, 32))

mstore(add(MEM_OFFSET_INNER(), offset), value)
evmGasLeft := chargeGas(evmGasLeft, gasCounter)
evmGasLeft := chargeGas(evmGasLeft, add(3, expansionGas))
}
case 0x53 { // OP_MSTORE8
let offset, value

offset, sp := popStackItem(sp)
value, sp := popStackItem(sp)

let gasCounter := 3

let end := add(offset, 1)
let currentEnd := mload(MEM_OFFSET())
if gt(end, currentEnd) {
let newSize := roundUp(end, 32)
for {} lt(currentEnd, newSize) {currentEnd := add(currentEnd, 32)} {
gasCounter := add(gasCounter, 3)
mstore(currentEnd, 0)
}
mstore(MEM_OFFSET(), newSize)
}
let expansionGas := expandMemory(add(offset, 1))

mstore8(add(MEM_OFFSET_INNER(), offset), value)
evmGasLeft := chargeGas(evmGasLeft, gasCounter)
evmGasLeft := chargeGas(evmGasLeft, add(3, expansionGas))
}
case 0x55 { // OP_SSTORE
let key, value
Expand Down

0 comments on commit db9cd58

Please sign in to comment.