Skip to content

Commit e68fbc8

Browse files
committed
Merge branch 'evm-equivalence-yul' into vv-lazy-stack
2 parents be5f6eb + 36c03ec commit e68fbc8

File tree

4 files changed

+293
-419
lines changed

4 files changed

+293
-419
lines changed

system-contracts/contracts/EvmInterpreter.template.yul

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,7 @@ object "EVMInterpreter" {
2121
// This error should never be triggered
2222
// require(offset > 100, "Offset too small");
2323

24-
mstore8(sub(offset, 100), 0xd9)
25-
mstore8(sub(offset, 99), 0xeb)
26-
mstore8(sub(offset, 98), 0x76)
27-
mstore8(sub(offset, 97), 0xb2)
24+
mstore(sub(offset, 100), 0xD9EB76B200000000000000000000000000000000000000000000000000000000)
2825
mstore(sub(offset, 96), gasLeft)
2926
mstore(sub(offset, 64), 0x40)
3027
mstore(sub(offset, 32), len)

system-contracts/contracts/EvmInterpreterFunctions.template.yul

Lines changed: 54 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -155,15 +155,6 @@ function popStackCheck(sp, evmGasLeft, numInputs) {
155155
}
156156
}
157157

158-
function popPushStackCheck(sp, evmGasLeft, numInputs) {
159-
let popCheck := lt(sub(sp, mul(0x20, sub(numInputs, 1))), STACK_OFFSET())
160-
let pushOffset := sub(sp, mul(0x20, numInputs))
161-
let pushCheck := or(gt(pushOffset, BYTECODE_OFFSET()), eq(pushOffset, BYTECODE_OFFSET()))
162-
if or(popCheck, pushCheck) {
163-
revertWithGas(evmGasLeft)
164-
}
165-
}
166-
167158
function getCodeAddress() -> addr {
168159
addr := verbatim_0i_1o("code_source")
169160
}
@@ -193,11 +184,7 @@ function ptrShrinkIntoActive(_dest) {
193184
}
194185

195186
function _getRawCodeHash(account) -> hash {
196-
// TODO: Unhardcode this selector
197-
mstore8(0, 0x4d)
198-
mstore8(1, 0xe2)
199-
mstore8(2, 0xe4)
200-
mstore8(3, 0x68)
187+
mstore(0, 0x4DE2E46800000000000000000000000000000000000000000000000000000000)
201188
mstore(4, account)
202189

203190
let success := staticcall(gas(), ACCOUNT_CODE_STORAGE_SYSTEM_CONTRACT(), 0, 36, 0, 32)
@@ -212,12 +199,7 @@ function _getRawCodeHash(account) -> hash {
212199

213200
function _getCodeHash(account) -> hash {
214201
// function getCodeHash(uint256 _input) external view override returns (bytes32)
215-
// 0xe03fe177
216-
// TODO: Unhardcode this selector
217-
mstore8(0, 0xe0)
218-
mstore8(1, 0x3f)
219-
mstore8(2, 0xe1)
220-
mstore8(3, 0x77)
202+
mstore(0, 0xE03FE17700000000000000000000000000000000000000000000000000000000)
221203
mstore(4, account)
222204

223205
let success := staticcall(gas(), ACCOUNT_CODE_STORAGE_SYSTEM_CONTRACT(), 0, 36, 0, 32)
@@ -301,11 +283,7 @@ function getDeployedBytecode() {
301283

302284
function consumeEvmFrame() -> passGas, isStatic, callerEVM {
303285
// function consumeEvmFrame() external returns (uint256 passGas, bool isStatic)
304-
// TODO: Unhardcode selector
305-
mstore8(0, 0x04)
306-
mstore8(1, 0xc1)
307-
mstore8(2, 0x4e)
308-
mstore8(3, 0x9e)
286+
mstore(0, 0x04C14E9E00000000000000000000000000000000000000000000000000000000)
309287

310288
let success := call(gas(), EVM_GAS_MANAGER_CONTRACT(), 0, 0, 4, 0, 64)
311289

@@ -550,11 +528,7 @@ function printString(value) {
550528
}
551529

552530
function isSlotWarm(key) -> isWarm {
553-
// TODO: Unhardcode this selector 0x482d2e74
554-
mstore8(0, 0x48)
555-
mstore8(1, 0x2d)
556-
mstore8(2, 0x2e)
557-
mstore8(3, 0x74)
531+
mstore(0, 0x482D2E7400000000000000000000000000000000000000000000000000000000)
558532
mstore(4, key)
559533

560534
let success := call(gas(), EVM_GAS_MANAGER_CONTRACT(), 0, 0, 36, 0, 32)
@@ -568,11 +542,7 @@ function isSlotWarm(key) -> isWarm {
568542
}
569543

570544
function warmSlot(key,currentValue) -> isWarm, originalValue {
571-
// TODO: Unhardcode this selector 0xbdf78160
572-
mstore8(0, 0xbd)
573-
mstore8(1, 0xf7)
574-
mstore8(2, 0x81)
575-
mstore8(3, 0x60)
545+
mstore(0, 0xBDF7816000000000000000000000000000000000000000000000000000000000)
576546
mstore(4, key)
577547
mstore(36,currentValue)
578548

@@ -665,10 +635,7 @@ function getNewAddress(addr) -> newAddr {
665635
}
666636

667637
function incrementNonce(addr) {
668-
mstore8(0, 0x30)
669-
mstore8(1, 0x63)
670-
mstore8(2, 0x95)
671-
mstore8(3, 0xc6)
638+
mstore(0, 0x306395C600000000000000000000000000000000000000000000000000000000)
672639
mstore(4, addr)
673640

674641
let result := call(gas(), NONCE_HOLDER_SYSTEM_CONTRACT(), 0, 0, 36, 0, 0)
@@ -700,11 +667,7 @@ function addGasIfEvmRevert(isCallerEVM,offset,size,evmGasLeft) -> newOffset,newS
700667
}
701668

702669
function $llvm_AlwaysInline_llvm$_warmAddress(addr) -> isWarm {
703-
// TODO: Unhardcode this selector 0x8db2ba78
704-
mstore8(0, 0x8d)
705-
mstore8(1, 0xb2)
706-
mstore8(2, 0xba)
707-
mstore8(3, 0x78)
670+
mstore(0, 0x8DB2BA7800000000000000000000000000000000000000000000000000000000)
708671
mstore(4, addr)
709672

710673
let success := call(gas(), EVM_GAS_MANAGER_CONTRACT(), 0, 0, 36, 0, 32)
@@ -718,10 +681,7 @@ function $llvm_AlwaysInline_llvm$_warmAddress(addr) -> isWarm {
718681
}
719682

720683
function getNonce(addr) -> nonce {
721-
mstore8(0, 0xfb)
722-
mstore8(1, 0x1a)
723-
mstore8(2, 0x9a)
724-
mstore8(3, 0x57)
684+
mstore(0, 0xFB1A9A5700000000000000000000000000000000000000000000000000000000)
725685
mstore(4, addr)
726686

727687
let result := staticcall(gas(), NONCE_HOLDER_SYSTEM_CONTRACT(), 0, 36, 0, 32)
@@ -734,10 +694,7 @@ function getNonce(addr) -> nonce {
734694
}
735695

736696
function getRawNonce(addr) -> nonce {
737-
mstore8(0, 0x5a)
738-
mstore8(1, 0xa9)
739-
mstore8(2, 0xb6)
740-
mstore8(3, 0xb5)
697+
mstore(0, 0x5AA9B6B500000000000000000000000000000000000000000000000000000000)
741698
mstore(4, addr)
742699

743700
let result := staticcall(gas(), NONCE_HOLDER_SYSTEM_CONTRACT(), 0, 36, 0, 32)
@@ -756,10 +713,7 @@ function _isEVM(_addr) -> isEVM {
756713
// address(SYSTEM_CONTRACTS_OFFSET + 0x02)
757714
// );
758715

759-
mstore8(0, 0x8c)
760-
mstore8(1, 0x04)
761-
mstore8(2, 0x04)
762-
mstore8(3, 0x77)
716+
mstore(0, 0x8C04047700000000000000000000000000000000000000000000000000000000)
763717
mstore(4, _addr)
764718

765719
let success := staticcall(gas(), ACCOUNT_CODE_STORAGE_SYSTEM_CONTRACT(), 0, 36, 0, 32)
@@ -774,12 +728,8 @@ function _isEVM(_addr) -> isEVM {
774728

775729
function _pushEVMFrame(_passGas, _isStatic) {
776730
// function pushEVMFrame(uint256 _passGas, bool _isStatic) external
777-
let selector := 0xead77156
778731

779-
mstore8(0, 0xea)
780-
mstore8(1, 0xd7)
781-
mstore8(2, 0x71)
782-
mstore8(3, 0x56)
732+
mstore(0, 0xEAD7715600000000000000000000000000000000000000000000000000000000)
783733
mstore(4, _passGas)
784734
mstore(36, _isStatic)
785735

@@ -792,13 +742,8 @@ function _pushEVMFrame(_passGas, _isStatic) {
792742

793743
function _popEVMFrame() {
794744
// function popEVMFrame() external
795-
// 0xe467d2f0
796-
let selector := 0xe467d2f0
797745

798-
mstore8(0, 0xe4)
799-
mstore8(1, 0x67)
800-
mstore8(2, 0xd2)
801-
mstore8(3, 0xf0)
746+
mstore(0, 0xE467D2F000000000000000000000000000000000000000000000000000000000)
802747

803748
let success := call(gas(), EVM_GAS_MANAGER_CONTRACT(), 0, 0, 4, 0, 0)
804749
if iszero(success) {
@@ -1223,12 +1168,7 @@ function isAddrEmpty(addr) -> isEmpty {
12231168
}
12241169

12251170
function _fetchConstructorReturnGas() -> gasLeft {
1226-
//selector is 0x24e5ab4a
1227-
1228-
mstore8(0, 0x24)
1229-
mstore8(1, 0xe5)
1230-
mstore8(2, 0xab)
1231-
mstore8(3, 0x4a)
1171+
mstore(0, 0x24E5AB4A00000000000000000000000000000000000000000000000000000000)
12321172

12331173
let success := staticcall(gas(), DEPLOYER_SYSTEM_CONTRACT(), 0, 4, 0, 32)
12341174

@@ -1311,6 +1251,45 @@ function $llvm_NoInline_llvm$_genericCreate(addr, offset, size, sp, value, evmGa
13111251
mstore(sub(offset, 0x80), back)
13121252
}
13131253

1254+
function $llvm_AlwaysInline_llvm$_copyRest(dest, val, len) {
1255+
let rest_bits := shl(3, len)
1256+
let upper_bits := sub(256, rest_bits)
1257+
let val_mask := shl(upper_bits, MAX_UINT())
1258+
let val_masked := and(val, val_mask)
1259+
let dst_val := mload(dest)
1260+
let dst_mask := shr(rest_bits, MAX_UINT())
1261+
let dst_masked := and(dst_val, dst_mask)
1262+
mstore(dest, or(val_masked, dst_masked))
1263+
}
1264+
1265+
function $llvm_AlwaysInline_llvm$_memcpy(dest, src, len) {
1266+
let dest_addr := dest
1267+
let src_addr := src
1268+
let dest_end := add(dest, and(len, sub(0, 32)))
1269+
for { } lt(dest_addr, dest_end) {} {
1270+
mstore(dest_addr, mload(src_addr))
1271+
dest_addr := add(dest_addr, 32)
1272+
src_addr := add(src_addr, 32)
1273+
}
1274+
1275+
let rest_len := and(len, 31)
1276+
if rest_len {
1277+
$llvm_AlwaysInline_llvm$_copyRest(dest_addr, mload(src_addr), rest_len)
1278+
}
1279+
}
1280+
1281+
function $llvm_AlwaysInline_llvm$_memsetToZero(dest,len) {
1282+
let dest_end := add(dest, and(len, sub(0, 32)))
1283+
for {let i := dest} lt(i, dest_end) { i := add(i, 32) } {
1284+
mstore(i, 0)
1285+
}
1286+
1287+
let rest_len := and(len, 31)
1288+
if rest_len {
1289+
$llvm_AlwaysInline_llvm$_copyRest(dest_end, 0, rest_len)
1290+
}
1291+
}
1292+
13141293
function performExtCodeCopy(evmGas,oldSp, oldStackHead) -> evmGasLeft, sp, stackHead {
13151294
evmGasLeft := chargeGas(evmGas, 100)
13161295

@@ -1333,17 +1312,7 @@ function performExtCodeCopy(evmGas,oldSp, oldStackHead) -> evmGasLeft, sp, stack
13331312
}
13341313
evmGasLeft := chargeGas(evmGasLeft, dynamicGas)
13351314

1336-
1337-
let len_32 := shr(5, len)
1338-
for {let i := 0} lt(i, len_32) { i := add(i, 1) } {
1339-
mstore(add(dest,shl(5,i)),0)
1340-
}
1341-
1342-
let size_32 := shl(5,len_32)
1343-
let rest_32 := sub(len, size_32)
1344-
for {let i := 0} lt(i, rest_32) { i := add(i, 1) } {
1345-
mstore8(add(dest,add(size_32,i)),0)
1346-
}
1315+
$llvm_AlwaysInline_llvm$_memsetToZero(dest, len)
13471316

13481317
// Gets the code from the addr
13491318
if and(iszero(iszero(_getRawCodeHash(addr))),gt(len,0)) {
@@ -1435,7 +1404,7 @@ function performCreate2(evmGas, oldSp, isStatic, oldStackHead) -> evmGasLeft, sp
14351404

14361405
{
14371406
let hashedBytecode := keccak256(add(MEM_OFFSET_INNER(), offset), size)
1438-
mstore8(0, 0xFF)
1407+
mstore(0, 0xFF00000000000000000000000000000000000000000000000000000000000000)
14391408
mstore(0x01, shl(0x60, address()))
14401409
mstore(0x15, salt)
14411410
mstore(0x35, hashedBytecode)

0 commit comments

Comments
 (0)