Skip to content

Commit

Permalink
Account for virtual sp adjustments
Browse files Browse the repository at this point in the history
When we use NominalSPOffset, we need to adjust it using `virtual_sp_offset`.
  • Loading branch information
aborg-dev committed Jan 23, 2024
1 parent f1231ff commit c7663a1
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 23 deletions.
42 changes: 21 additions & 21 deletions cranelift/codegen/src/isa/zkasm/inst/emit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -589,34 +589,34 @@ impl MachInstEmit for Inst {
let offset = from.get_offset_with_state(state);
let rd = allocs.next_writable(rd);
match from {
AMode::RegOffset(r, off, _) => {
AMode::RegOffset(r, ..) => {
debug_assert_eq!(r, e0());
put_string(
&format!(
"$ => {} :MLOAD(MEM:{})\n",
reg_name(rd.to_reg()),
access_reg_with_offset(r, off)
access_reg_with_offset(r, offset)
),
sink,
);
}
AMode::SPOffset(off, _) | AMode::NominalSPOffset(off, _) => {
assert_eq!(off % 8, 0);
AMode::SPOffset(..) | AMode::NominalSPOffset(..) => {
assert_eq!(offset % 8, 0);
put_string(
&format!(
"$ => {} :MLOAD({})\n",
reg_name(rd.to_reg()),
access_reg_with_offset(stack_reg(), off / 8),
access_reg_with_offset(stack_reg(), offset / 8),
),
sink,
);
}
AMode::FPOffset(off, _) => {
AMode::FPOffset(..) => {
put_string(
&format!(
"$ => {} :MLOAD({})\n",
reg_name(rd.to_reg()),
access_reg_with_offset(fp_reg(), off),
access_reg_with_offset(fp_reg(), offset),
),
sink,
);
Expand All @@ -634,36 +634,37 @@ impl MachInstEmit for Inst {
&Inst::Store { op, src, flags, to } => {
let to = to.clone().with_allocs(&mut allocs);
let src = allocs.next(src);
let offset = to.get_offset_with_state(state);

match to {
AMode::RegOffset(r, off, _) => {
AMode::RegOffset(r, ..) => {
debug_assert_eq!(r, e0());
put_string(
&format!(
"{} :MSTORE(MEM:{})\n",
reg_name(src),
access_reg_with_offset(r, off)
access_reg_with_offset(r, offset)
),
sink,
);
}
AMode::SPOffset(off, _) | AMode::NominalSPOffset(off, _) => {
assert_eq!(off % 8, 0);
AMode::SPOffset(..) | AMode::NominalSPOffset(..) => {
assert_eq!(offset % 8, 0);
put_string(
&format!(
"{} :MSTORE({})\n",
reg_name(src),
access_reg_with_offset(stack_reg(), off / 8),
access_reg_with_offset(stack_reg(), offset / 8),
),
sink,
);
}
AMode::FPOffset(off, _) => {
AMode::FPOffset(..) => {
put_string(
&format!(
"{} :MSTORE({})\n",
reg_name(src),
access_reg_with_offset(fp_reg(), off),
access_reg_with_offset(fp_reg(), offset),
),
sink,
);
Expand Down Expand Up @@ -1182,13 +1183,12 @@ impl MachInstEmit for Inst {
}

&Inst::VirtualSPOffsetAdj { amount } => {
println!("virtual_sp_offset_adj {amount}");
// crate::trace!(
// "virtual sp offset adjusted by {} -> {}",
// amount,
// state.virtual_sp_offset + amount
// );
// state.virtual_sp_offset += amount;
crate::trace!(
"virtual sp offset adjusted by {} -> {}",
amount,
state.virtual_sp_offset + amount
);
state.virtual_sp_offset += amount;
}

&Inst::LoadAddr { rd, mem } => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -557,7 +557,7 @@ label_1_3:
1n => B ;; LoadConst32
SP - 1 => SP
B :MSTORE(SP)
$ => B :MLOAD(SP)
$ => B :MLOAD(SP + 1)
zkPC + 2 => RR
:JMP(function_2)
SP + 1 => SP
Expand All @@ -572,7 +572,7 @@ label_1_5:
1n => C ;; LoadConst32
SP - 1 => SP
C :MSTORE(SP)
$ => B :MLOAD(SP)
$ => B :MLOAD(SP + 1)
zkPC + 2 => RR
:JMP(function_2)
SP + 1 => SP
Expand Down

0 comments on commit c7663a1

Please sign in to comment.