Skip to content
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
580 changes: 527 additions & 53 deletions crates/codegen/src/sonatina/lower_runtime.rs

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion crates/codegen/src/sonatina/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,6 @@ fn section_name_for_runtime(name: &mir::RuntimeSectionName) -> sonatina_ir::Sect
mir::RuntimeSectionName::Runtime => "runtime".into(),
mir::RuntimeSectionName::Main => "main".into(),
mir::RuntimeSectionName::Test(name) => format!("test_{name}").into(),
mir::RuntimeSectionName::CodeRegion(symbol) => format!("code_region_{symbol}").into(),
}
}

Expand Down
24 changes: 13 additions & 11 deletions crates/codegen/tests/fixtures/erc20_low_level.fe
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,16 @@ fn abi_encode_string(word: u256, len: u256) -> ! uses (ops: mut RawOps) {

// ERC20 storage ---------------------------------------------------------

struct Erc20 {
balances: StorageMap<Address, u256>,
allowances: StorageMap<(Address, Address), u256>,
struct Erc20<const BALANCES_ROOT: u256, const ALLOWANCES_ROOT: u256> {
balances: StorageMap<Address, u256, BALANCES_ROOT>,
allowances: StorageMap<(Address, Address), u256, ALLOWANCES_ROOT>,
supply: u256,
owner: Address,
}

impl Erc20 {
impl<const BALANCES_ROOT: u256, const ALLOWANCES_ROOT: u256>
Erc20<BALANCES_ROOT, ALLOWANCES_ROOT>
{

fn balance_of(self, addr: Address) -> u256 {
self.balances.get(key: addr)
Expand Down Expand Up @@ -83,34 +85,34 @@ impl Erc20 {
}
}

fn do_init() uses (erc20: mut Erc20, ctx: Ctx, ops: RawOps) {
fn do_init() uses (erc20: mut Erc20<BALANCES_SALT, ALLOWANCES_SALT>, ctx: Ctx, ops: RawOps) {
erc20.set_owner_once(owner: ctx.caller())
}

fn balance_of(addr: Address) -> u256 uses (erc20: Erc20) {
fn balance_of(addr: Address) -> u256 uses (erc20: Erc20<BALANCES_SALT, ALLOWANCES_SALT>) {
erc20.balances.get(key: addr)
}

fn allowance(owner: Address, spender: Address) -> u256 uses (erc20: Erc20) {
fn allowance(owner: Address, spender: Address) -> u256 uses (erc20: Erc20<BALANCES_SALT, ALLOWANCES_SALT>) {
erc20.allowances.get(key: (owner, spender))
}

fn transfer(to: Address, amount: u256) -> u256 uses (erc20: mut Erc20, ctx: Ctx, ops: RawOps) {
fn transfer(to: Address, amount: u256) -> u256 uses (erc20: mut Erc20<BALANCES_SALT, ALLOWANCES_SALT>, ctx: Ctx, ops: RawOps) {
erc20.transfer(from: ctx.caller(), to: to, amount: amount)
1
}

fn approve(spender: Address, amount: u256) -> u256 uses (erc20: mut Erc20, ctx: Ctx) {
fn approve(spender: Address, amount: u256) -> u256 uses (erc20: mut Erc20<BALANCES_SALT, ALLOWANCES_SALT>, ctx: Ctx) {
erc20.approve(owner: ctx.caller(), spender: spender, value: amount)
1
}

fn transfer_from(from: Address, to: Address, amount: u256) -> u256 uses (erc20: mut Erc20, ctx: Ctx, ops: RawOps) {
fn transfer_from(from: Address, to: Address, amount: u256) -> u256 uses (erc20: mut Erc20<BALANCES_SALT, ALLOWANCES_SALT>, ctx: Ctx, ops: RawOps) {
erc20.transfer_from(owner: from, to: to, amount: amount)
1
}

fn mint(to: Address, amount: u256) -> u256 uses (erc20: mut Erc20, ctx: Ctx, ops: RawOps) {
fn mint(to: Address, amount: u256) -> u256 uses (erc20: mut Erc20<BALANCES_SALT, ALLOWANCES_SALT>, ctx: Ctx, ops: RawOps) {
erc20.mint(to: to, amount: amount)
1
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ input_file: crates/codegen/tests/fixtures/borrow_storage_field_handle.fe
---
target = "evm-ethereum-osaka"

type @layout_0 = {i256};
type @layout_1 = {i256};

func private %borrow_storage_field_handle(v0.i256) -> i256 {
block0:
jump block1;
Expand All @@ -21,26 +24,30 @@ func private %borrow_storage_field_handle(v0.i256) -> i256 {

block3:
evm_sstore v0 v4;
v13.i256 = evm_sload v0;
return v13;
v14.i256 = evm_sload v0;
v15.@layout_1 = insert_value undef.@layout_1 0.i256 v14;
v16.i256 = extract_value v15 0.i256;
return v16;
}

func private %from_raw(v0.i256) -> i256 {
func private %from_raw(v0.i256) -> @layout_0 {
block0:
jump block1;

block1:
return v0;
v4.@layout_0 = insert_value undef.@layout_0 0.i256 v0;
return v4;
}

func private %main() -> i256 {
block0:
jump block1;

block1:
v1.i256 = call %stor_ptr 0.i256;
v2.i256 = call %borrow_storage_field_handle v1;
return v2;
v1.@layout_0 = call %stor_ptr 0.i256;
v2.i256 = call %raw v1;
v3.i256 = call %borrow_storage_field_handle v2;
return v3;
}

func private %main_root() {
Expand All @@ -52,12 +59,21 @@ func private %main_root() {
evm_stop;
}

func private %stor_ptr(v0.i256) -> i256 {
func private %raw(v0.@layout_0) -> i256 {
block0:
jump block1;

block1:
v3.i256 = extract_value v0 0.i256;
return v3;
}

func private %stor_ptr(v0.i256) -> @layout_0 {
block0:
jump block1;

block1:
v2.i256 = call %from_raw v0;
v2.@layout_0 = call %from_raw v0;
return v2;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ func private %cast_u8_usize_cmp(v0.constref<[i8; 8]>, v1.i256, v2.i256) -> i8 {
jump block1;

block1:
v6.i1 = lt v1 8.i256;
v7.i1 = is_zero v6;
br v7 block11 block12;
v5.i1 = lt v1 8.i256;
v6.i1 = is_zero v5;
br v6 block11 block12;

block2:
return 1.i8;
Expand Down
22 changes: 11 additions & 11 deletions crates/codegen/tests/fixtures/sonatina_ir/const_array.snap
Original file line number Diff line number Diff line change
Expand Up @@ -39,26 +39,26 @@ func private %sum_two(v0.i256, v1.i256) -> i256 {
evm_revert 0.i256 0.i256;

block3:
v8.constref<i256> = const.index v2 v0;
v9.i256 = const.load v8;
v10.constref<[i256; 3]> = const.ref $const_region_0;
v12.i1 = lt v1 3.i256;
v13.i1 = is_zero v12;
br v13 block2 block4;
v9.constref<i256> = const.index v2 v0;
v10.i256 = const.load v9;
v11.constref<[i256; 3]> = const.ref $const_region_0;
v13.i1 = lt v1 3.i256;
v14.i1 = is_zero v13;
br v14 block2 block4;

block4:
v14.constref<i256> = const.index v10 v1;
v15.i256 = const.load v14;
(v17.i256, v18.i1) = uaddo v9 v15;
br v18 block5 block6;
v16.constref<i256> = const.index v11 v1;
v17.i256 = const.load v16;
(v19.i256, v20.i1) = uaddo v10 v17;
br v20 block5 block6;

block5:
mstore 0.i256 35408467139433450592217433187231851964531694900788300625387963629091585785856.i256 i256;
mstore 4.i256 17.i256 i256;
evm_revert 0.i256 36.i256;

block6:
return v17;
return v19;
}


Expand Down
18 changes: 9 additions & 9 deletions crates/codegen/tests/fixtures/sonatina_ir/const_array_loop.snap
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ func private %sum_loop(v0.i256) -> i256 {
jump block2;

block2:
v28.i256 = phi (0.i256 block1) (v19 block11);
v2.i256 = phi (0.i256 block1) (v24 block11);
v29.i256 = phi (0.i256 block1) (v20 block11);
v2.i256 = phi (0.i256 block1) (v25 block11);
v4.i1 = lt v2 v0;
br v4 block3 block4;

Expand All @@ -43,7 +43,7 @@ func private %sum_loop(v0.i256) -> i256 {
br v7 block5 block6;

block4:
return v28;
return v29;

block5:
mstore 0.i256 35408467139433450592217433187231851964531694900788300625387963629091585785856.i256 i256;
Expand All @@ -61,19 +61,19 @@ func private %sum_loop(v0.i256) -> i256 {
evm_revert 0.i256 0.i256;

block8:
v16.constref<i256> = const.index v13 v12;
v17.i256 = const.load v16;
(v19.i256, v20.i1) = uaddo v28 v17;
br v20 block9 block10;
v17.constref<i256> = const.index v13 v12;
v18.i256 = const.load v17;
(v20.i256, v21.i1) = uaddo v29 v18;
br v21 block9 block10;

block9:
mstore 0.i256 35408467139433450592217433187231851964531694900788300625387963629091585785856.i256 i256;
mstore 4.i256 17.i256 i256;
evm_revert 0.i256 36.i256;

block10:
(v24.i256, v25.i1) = uaddo v2 1.i256;
br v25 block9 block11;
(v25.i256, v26.i1) = uaddo v2 1.i256;
br v26 block9 block11;

block11:
jump block2;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ type @layout_5 = {i256, i256};
type @layout_6 = {};
type @layout_7 = {@layout_6};
type @layout_8 = {i256, i256};
type @layout_9 = {i256, i256};
type @layout_9 = {i256};
type @layout_10 = {@layout_9, i256};
type @layout_11 = {i256};

global private const @layout_2 $const_region_0 = {0};

Expand All @@ -32,22 +34,24 @@ func private %__EffectHandleFieldDeref_recv_0_0(v0.i256, v1.i256) -> i256 {
jump block1;

block1:
v3.i256 = call %stor_ptr v0;
v5.i256 = call %write_cell v1 v3;
return v5;
v3.@layout_9 = call %stor_ptr v0;
v4.i256 = call %raw v3;
v6.i256 = call %write_cell v1 v4;
return v6;
}

func private %__EffectHandleFieldDeref_recv_0_1(v0.i256) -> i256 {
block0:
jump block1;

block1:
v2.i256 = call %stor_ptr v0;
v6.@layout_9 = insert_value undef.@layout_9 0.i256 v2;
v7.@layout_9 = insert_value v6 1.i256 1.i256;
v8.i256 = call %extract_ptr v7;
v9.i256 = call %read_cell v8;
return v9;
v2.@layout_9 = call %stor_ptr v0;
v6.@layout_10 = insert_value undef.@layout_10 0.i256 v2;
v7.@layout_10 = insert_value v6 1.i256 1.i256;
v8.@layout_9 = call %extract_ptr v7;
v9.i256 = call %raw v8;
v10.i256 = call %read_cell v9;
return v10;
}

func private %__EffectHandleFieldDeref_recv_0_2(v0.i256, v1.i256) -> i256 {
Expand Down Expand Up @@ -1094,29 +1098,22 @@ func private %encoder_new(v0.i256) -> @layout_0 {
return v2;
}

func private %extract_ptr(v0.@layout_9) -> i256 {
func private %extract_ptr(v0.@layout_10) -> @layout_9 {
block0:
v1.objref<@layout_9> = obj.alloc @layout_9;
v3.objref<i256> = obj.proj v1 0.i256;
v4.i256 = extract_value v0 0.i256;
obj.store v3 v4;
v6.objref<i256> = obj.proj v1 1.i256;
v7.i256 = extract_value v0 1.i256;
obj.store v6 v7;
jump block1;

block1:
v8.objref<i256> = obj.proj v1 0.i256;
v9.i256 = obj.load v8;
return v9;
v3.@layout_9 = extract_value v0 0.i256;
return v3;
}

func private %from_raw(v0.i256) -> i256 {
func private %from_raw(v0.i256) -> @layout_9 {
block0:
jump block1;

block1:
return v0;
v4.@layout_9 = insert_value undef.@layout_9 0.i256 v0;
return v4;
}

func private %std__lib__evm__effects__impl_trait_Evm_c913__input_0e8a() -> @layout_2 {
Expand Down Expand Up @@ -1434,13 +1431,24 @@ func private %pos(v0.objref<@layout_0>) -> i256 {
return v4;
}

func private %raw(v0.@layout_9) -> i256 {
block0:
jump block1;

block1:
v3.i256 = extract_value v0 0.i256;
return v3;
}

func private %read_cell(v0.i256) -> i256 {
block0:
jump block1;

block1:
v2.i256 = evm_sload v0;
return v2;
v3.i256 = evm_sload v0;
v5.@layout_11 = insert_value undef.@layout_11 0.i256 v3;
v6.i256 = extract_value v5 0.i256;
return v6;
}

func private %std__lib__evm__effects__impl_trait_Evm_0098__revert_27e9(v0.i256, v1.i256) {
Expand Down Expand Up @@ -1487,12 +1495,12 @@ func private %set_pos(v0.objref<@layout_0>, v1.i256) {
return;
}

func private %stor_ptr(v0.i256) -> i256 {
func private %stor_ptr(v0.i256) -> @layout_9 {
block0:
jump block1;

block1:
v2.i256 = call %from_raw v0;
v2.@layout_9 = call %from_raw v0;
return v2;
}

Expand Down Expand Up @@ -1610,8 +1618,10 @@ func private %write_cell(v0.i256, v1.i256) -> i256 {

block1:
evm_sstore v1 v0;
v4.i256 = evm_sload v1;
return v4;
v5.i256 = evm_sload v1;
v7.@layout_11 = insert_value undef.@layout_11 0.i256 v5;
v8.i256 = extract_value v7 0.i256;
return v8;
}

func private %std__lib__abi__sol__impl_trait_SolEncoder_19ce__write_word_2aa5(v0.objref<@layout_0>, v1.i256) {
Expand Down
Loading
Loading