Skip to content

Commit

Permalink
Merge pull request #194 from near/fp_offset
Browse files Browse the repository at this point in the history
Support loads and stores at FPOffset
  • Loading branch information
aborg-dev authored Jan 23, 2024
2 parents e2ceaea + 135bd64 commit a318127
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 29 deletions.
3 changes: 1 addition & 2 deletions cranelift/codegen/src/isa/zkasm/abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,7 @@ impl ABIMachineSpec for ZkAsmMachineDeps {
}

fn fp_to_arg_offset(_call_conv: isa::CallConv, _flags: &settings::Flags) -> i64 {
// lr fp.
16
8
}

fn gen_load_stack(mem: StackAMode, into_reg: Writable<Reg>, ty: Type) -> Inst {
Expand Down
5 changes: 4 additions & 1 deletion cranelift/codegen/src/isa/zkasm/inst/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,14 +143,17 @@ impl AMode {
match self {
&AMode::RegOffset(reg, ..) => Some(reg),
&AMode::SPOffset(..) => Some(stack_reg()),
&AMode::FPOffset(..) => Some(fp_reg()),
&AMode::FPOffset(..) => Some(stack_reg()),
&AMode::NominalSPOffset(..) => Some(stack_reg()),
&AMode::Const(..) | AMode::Label(..) | AMode::Global(..) => None,
}
}

pub(crate) fn get_offset_with_state(&self, state: &EmitState) -> i64 {
match self {
// We don't have a frame pointer on ZKASM, but it can be easily computed
// based on the nominal sp.
&AMode::FPOffset(offset, _) => offset + state.nominal_sp_to_fp,
&AMode::NominalSPOffset(offset, _) => offset + state.virtual_sp_offset,
_ => self.get_offset(),
}
Expand Down
24 changes: 2 additions & 22 deletions cranelift/codegen/src/isa/zkasm/inst/emit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -600,7 +600,7 @@ impl MachInstEmit for Inst {
sink,
);
}
AMode::SPOffset(..) | AMode::NominalSPOffset(..) => {
AMode::SPOffset(..) | AMode::NominalSPOffset(..) | AMode::FPOffset(..) => {
assert_eq!(offset % 8, 0);
put_string(
&format!(
Expand All @@ -611,16 +611,6 @@ impl MachInstEmit for Inst {
sink,
);
}
AMode::FPOffset(..) => {
put_string(
&format!(
"$ => {} :MLOAD({})\n",
reg_name(rd.to_reg()),
access_reg_with_offset(fp_reg(), offset),
),
sink,
);
}
AMode::Const(_) => unimplemented!("Load, AMode::Const"),
AMode::Label(_) => unimplemented!("Load, AMode::Label"),
AMode::Global(index, _) => {
Expand Down Expand Up @@ -648,7 +638,7 @@ impl MachInstEmit for Inst {
sink,
);
}
AMode::SPOffset(..) | AMode::NominalSPOffset(..) => {
AMode::SPOffset(..) | AMode::NominalSPOffset(..) | AMode::FPOffset(..) => {
assert_eq!(offset % 8, 0);
put_string(
&format!(
Expand All @@ -659,16 +649,6 @@ impl MachInstEmit for Inst {
sink,
);
}
AMode::FPOffset(..) => {
put_string(
&format!(
"{} :MSTORE({})\n",
reg_name(src),
access_reg_with_offset(fp_reg(), offset),
),
sink,
);
}
AMode::Const(_) => unimplemented!("Store, AMode::Const"),
AMode::Label(_) => unimplemented!("Store, AMode::Label"),
AMode::Global(index, _) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1664,7 +1664,7 @@ function_2:
SP - 1 => SP
C :MSTORE(SP - 1)
SP - 2 => SP
$ => C :MLOAD(fp + 16)
$ => C :MLOAD(SP + 3)
SP - 1 => SP
C :MSTORE(SP)
zkPC + 2 => RR
Expand All @@ -1685,7 +1685,7 @@ function_3:
SP - 1182 => SP
A :MSTORE(SP)
B :MSTORE(SP + 1)
$ => A :MLOAD(fp + 16)
$ => A :MLOAD(SP + 1183)
A :MSTORE(SP + 2)
0 => A ;; LoadExtName(User(userextname0))
$ => B :MLOAD(SP)
Expand Down Expand Up @@ -28172,7 +28172,7 @@ function_4:
SP - 1 => SP
C :MSTORE(SP - 1)
SP - 2 => SP
$ => C :MLOAD(fp + 16)
$ => C :MLOAD(SP + 3)
SP - 1 => SP
C :MSTORE(SP)
zkPC + 2 => RR
Expand All @@ -28193,7 +28193,7 @@ function_5:
SP - 10 => SP
A :MSTORE(SP)
B :MSTORE(SP + 1)
$ => A :MLOAD(fp + 16)
$ => A :MLOAD(SP + 11)
A :MSTORE(SP + 2)
15n => B ;; LoadConst32
A => C
Expand Down

0 comments on commit a318127

Please sign in to comment.