Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(EVM): Store bytecode in the active pointer #1190

Open
wants to merge 2 commits into
base: evm-emulator/optimize-bytecode-loading
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 24 additions & 18 deletions system-contracts/contracts/EvmEmulator.yul
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ object "EvmEmulator" {
}

mstore(BYTECODE_LEN_OFFSET(), size)

swapActivePointerWithBytecodePointer()
}

function padBytecode(offset, len) -> blobLen {
Expand Down Expand Up @@ -308,16 +306,12 @@ object "EvmEmulator" {

// It is the responsibility of the caller to ensure that ip is correct
function $llvm_AlwaysInline_llvm$_readIP(ip) -> opcode {
swapActivePointerWithBytecodePointer()
opcode := shr(248, activePointerLoad(ip))
swapActivePointerWithBytecodePointer()
}

// It is the responsibility of the caller to ensure that start and length is correct
function readBytes(start, length) -> value {
swapActivePointerWithBytecodePointer()
let rawValue := activePointerLoad(start)
swapActivePointerWithBytecodePointer()

value := shr(mul(8, sub(32, length)), rawValue)
// will be padded by zeroes if out of bounds
Expand All @@ -335,7 +329,7 @@ object "EvmEmulator" {
verbatim_2i_0o("active_ptr_swap", index0, index1)
}

function swapActivePointerWithBytecodePointer() {
function swapActivePointerWithEvmReturndataPointer() {
verbatim_2i_0o("active_ptr_swap", 0, 2)
}

Expand Down Expand Up @@ -1038,12 +1032,15 @@ object "EvmEmulator" {
}

function _saveReturndataAfterZkEVMCall() {
swapActivePointerWithEvmReturndataPointer()
loadReturndataIntoActivePtr()
swapActivePointerWithEvmReturndataPointer()
mstore(LAST_RETURNDATA_SIZE_OFFSET(), returndatasize())
}

function _saveReturndataAfterEVMCall(_outputOffset, _outputLen) -> _gasLeft {
let rtsz := returndatasize()
swapActivePointerWithEvmReturndataPointer()
loadReturndataIntoActivePtr()

// if (rtsz > 31)
Expand All @@ -1067,11 +1064,14 @@ object "EvmEmulator" {
// Skip first 32 bytes of the returnData
ptrAddIntoActive(32)
}
swapActivePointerWithEvmReturndataPointer()
}

function _eraseReturndataPointer() {
swapActivePointerWithEvmReturndataPointer()
let activePtrSize := getActivePtrDataSize()
ptrShrinkIntoActive(and(activePtrSize, 0xFFFFFFFF))// uint32(activePtrSize)
swapActivePointerWithEvmReturndataPointer()
mstore(LAST_RETURNDATA_SIZE_OFFSET(), 0)
}

Expand Down Expand Up @@ -1233,6 +1233,7 @@ object "EvmEmulator" {
}

function _saveConstructorReturnGas() -> gasLeft, addr {
swapActivePointerWithEvmReturndataPointer()
loadReturndataIntoActivePtr()

if lt(returndatasize(), 64) {
Expand All @@ -1244,6 +1245,8 @@ object "EvmEmulator" {
gasLeft := activePointerLoad(0)
addr := activePointerLoad(32)

swapActivePointerWithEvmReturndataPointer()

_eraseReturndataPointer()
}

Expand Down Expand Up @@ -1776,9 +1779,7 @@ object "EvmEmulator" {
}

if truncatedLen {
swapActivePointerWithBytecodePointer()
copyActivePtrData(dstOffset, sourceOffset, truncatedLen)
swapActivePointerWithBytecodePointer()
}

ip := add(ip, 1)
Expand Down Expand Up @@ -1885,7 +1886,9 @@ object "EvmEmulator" {
panic()
}

swapActivePointerWithEvmReturndataPointer()
copyActivePtrData(add(MEM_OFFSET(), dstOffset), sourceOffset, len)
swapActivePointerWithEvmReturndataPointer()
ip := add(ip, 1)
}
case 0x3F { // OP_EXTCODEHASH
Expand Down Expand Up @@ -2984,8 +2987,6 @@ object "EvmEmulator" {
loadReturndataIntoActivePtr()

mstore(BYTECODE_LEN_OFFSET(), codeLen)

swapActivePointerWithBytecodePointer()
}

////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -3238,16 +3239,12 @@ object "EvmEmulator" {

// It is the responsibility of the caller to ensure that ip is correct
function $llvm_AlwaysInline_llvm$_readIP(ip) -> opcode {
swapActivePointerWithBytecodePointer()
opcode := shr(248, activePointerLoad(ip))
swapActivePointerWithBytecodePointer()
}

// It is the responsibility of the caller to ensure that start and length is correct
function readBytes(start, length) -> value {
swapActivePointerWithBytecodePointer()
let rawValue := activePointerLoad(start)
swapActivePointerWithBytecodePointer()

value := shr(mul(8, sub(32, length)), rawValue)
// will be padded by zeroes if out of bounds
Expand All @@ -3265,7 +3262,7 @@ object "EvmEmulator" {
verbatim_2i_0o("active_ptr_swap", index0, index1)
}

function swapActivePointerWithBytecodePointer() {
function swapActivePointerWithEvmReturndataPointer() {
verbatim_2i_0o("active_ptr_swap", 0, 2)
}

Expand Down Expand Up @@ -3968,12 +3965,15 @@ object "EvmEmulator" {
}

function _saveReturndataAfterZkEVMCall() {
swapActivePointerWithEvmReturndataPointer()
loadReturndataIntoActivePtr()
swapActivePointerWithEvmReturndataPointer()
mstore(LAST_RETURNDATA_SIZE_OFFSET(), returndatasize())
}

function _saveReturndataAfterEVMCall(_outputOffset, _outputLen) -> _gasLeft {
let rtsz := returndatasize()
swapActivePointerWithEvmReturndataPointer()
loadReturndataIntoActivePtr()

// if (rtsz > 31)
Expand All @@ -3997,11 +3997,14 @@ object "EvmEmulator" {
// Skip first 32 bytes of the returnData
ptrAddIntoActive(32)
}
swapActivePointerWithEvmReturndataPointer()
}

function _eraseReturndataPointer() {
swapActivePointerWithEvmReturndataPointer()
let activePtrSize := getActivePtrDataSize()
ptrShrinkIntoActive(and(activePtrSize, 0xFFFFFFFF))// uint32(activePtrSize)
swapActivePointerWithEvmReturndataPointer()
mstore(LAST_RETURNDATA_SIZE_OFFSET(), 0)
}

Expand Down Expand Up @@ -4163,6 +4166,7 @@ object "EvmEmulator" {
}

function _saveConstructorReturnGas() -> gasLeft, addr {
swapActivePointerWithEvmReturndataPointer()
loadReturndataIntoActivePtr()

if lt(returndatasize(), 64) {
Expand All @@ -4174,6 +4178,8 @@ object "EvmEmulator" {
gasLeft := activePointerLoad(0)
addr := activePointerLoad(32)

swapActivePointerWithEvmReturndataPointer()

_eraseReturndataPointer()
}

Expand Down Expand Up @@ -4694,9 +4700,7 @@ object "EvmEmulator" {
}

if truncatedLen {
swapActivePointerWithBytecodePointer()
copyActivePtrData(dstOffset, sourceOffset, truncatedLen)
swapActivePointerWithBytecodePointer()
}

ip := add(ip, 1)
Expand Down Expand Up @@ -4803,7 +4807,9 @@ object "EvmEmulator" {
panic()
}

swapActivePointerWithEvmReturndataPointer()
copyActivePtrData(add(MEM_OFFSET(), dstOffset), sourceOffset, len)
swapActivePointerWithEvmReturndataPointer()
ip := add(ip, 1)
}
case 0x3F { // OP_EXTCODEHASH
Expand Down
4 changes: 0 additions & 4 deletions system-contracts/evm-emulator/EvmEmulator.template.yul
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ object "EvmEmulator" {
}

mstore(BYTECODE_LEN_OFFSET(), size)

swapActivePointerWithBytecodePointer()
}

function padBytecode(offset, len) -> blobLen {
Expand Down Expand Up @@ -116,8 +114,6 @@ object "EvmEmulator" {
loadReturndataIntoActivePtr()

mstore(BYTECODE_LEN_OFFSET(), codeLen)

swapActivePointerWithBytecodePointer()
}

<!-- @include EvmEmulatorFunctions.template.yul -->
Expand Down
15 changes: 10 additions & 5 deletions system-contracts/evm-emulator/EvmEmulatorFunctions.template.yul
Original file line number Diff line number Diff line change
Expand Up @@ -248,16 +248,12 @@ function insufficientBalance(value) -> res {

// It is the responsibility of the caller to ensure that ip is correct
function $llvm_AlwaysInline_llvm$_readIP(ip) -> opcode {
swapActivePointerWithBytecodePointer()
opcode := shr(248, activePointerLoad(ip))
swapActivePointerWithBytecodePointer()
}

// It is the responsibility of the caller to ensure that start and length is correct
function readBytes(start, length) -> value {
swapActivePointerWithBytecodePointer()
let rawValue := activePointerLoad(start)
swapActivePointerWithBytecodePointer()

value := shr(mul(8, sub(32, length)), rawValue)
// will be padded by zeroes if out of bounds
Expand All @@ -275,7 +271,7 @@ function swapActivePointer(index0, index1) {
verbatim_2i_0o("active_ptr_swap", index0, index1)
}

function swapActivePointerWithBytecodePointer() {
function swapActivePointerWithEvmReturndataPointer() {
verbatim_2i_0o("active_ptr_swap", 0, 2)
}

Expand Down Expand Up @@ -978,12 +974,15 @@ function getGasForPrecompiles(addr, argsSize) -> gasToCharge {
}

function _saveReturndataAfterZkEVMCall() {
swapActivePointerWithEvmReturndataPointer()
loadReturndataIntoActivePtr()
swapActivePointerWithEvmReturndataPointer()
mstore(LAST_RETURNDATA_SIZE_OFFSET(), returndatasize())
}

function _saveReturndataAfterEVMCall(_outputOffset, _outputLen) -> _gasLeft {
let rtsz := returndatasize()
swapActivePointerWithEvmReturndataPointer()
loadReturndataIntoActivePtr()

// if (rtsz > 31)
Expand All @@ -1007,11 +1006,14 @@ function _saveReturndataAfterEVMCall(_outputOffset, _outputLen) -> _gasLeft {
// Skip first 32 bytes of the returnData
ptrAddIntoActive(32)
}
swapActivePointerWithEvmReturndataPointer()
}

function _eraseReturndataPointer() {
swapActivePointerWithEvmReturndataPointer()
let activePtrSize := getActivePtrDataSize()
ptrShrinkIntoActive(and(activePtrSize, 0xFFFFFFFF))// uint32(activePtrSize)
swapActivePointerWithEvmReturndataPointer()
mstore(LAST_RETURNDATA_SIZE_OFFSET(), 0)
}

Expand Down Expand Up @@ -1173,6 +1175,7 @@ function performSystemCallForCreate(value, bytecodeStart, bytecodeLen) -> succes
}

function _saveConstructorReturnGas() -> gasLeft, addr {
swapActivePointerWithEvmReturndataPointer()
loadReturndataIntoActivePtr()

if lt(returndatasize(), 64) {
Expand All @@ -1184,6 +1187,8 @@ function _saveConstructorReturnGas() -> gasLeft, addr {
gasLeft := activePointerLoad(0)
addr := activePointerLoad(32)

swapActivePointerWithEvmReturndataPointer()

_eraseReturndataPointer()
}

Expand Down
4 changes: 2 additions & 2 deletions system-contracts/evm-emulator/EvmEmulatorLoop.template.yul
Original file line number Diff line number Diff line change
Expand Up @@ -429,9 +429,7 @@ for { } true { } {
}

if truncatedLen {
swapActivePointerWithBytecodePointer()
copyActivePtrData(dstOffset, sourceOffset, truncatedLen)
swapActivePointerWithBytecodePointer()
}

ip := add(ip, 1)
Expand Down Expand Up @@ -538,7 +536,9 @@ for { } true { } {
panic()
}

swapActivePointerWithEvmReturndataPointer()
copyActivePtrData(add(MEM_OFFSET(), dstOffset), sourceOffset, len)
swapActivePointerWithEvmReturndataPointer()
ip := add(ip, 1)
}
case 0x3F { // OP_EXTCODEHASH
Expand Down
Loading