properly save rsp

This commit is contained in:
nora 2023-06-03 17:09:49 +02:00
parent b7108770b9
commit a363b7c6d1

View file

@ -148,9 +148,9 @@ impl<'cx> AsmCtxt<'cx> {
} }
fn generate_func(&mut self, func: &Func<'cx>) -> Result<()> { fn generate_func(&mut self, func: &Func<'cx>) -> Result<()> {
// TODO: Prologue // Prologue: Save rbp and save rsp in rbp.
self.a.push(x::rbx).sp(func.def_span)?; self.a.push(x::rbp).sp(func.def_span)?;
self.a.push(x::rsp).sp(func.def_span)?; self.a.mov(x::rbp, x::rsp).sp(func.def_span)?;
loop { loop {
let bb = &func.bbs[self.bb_idx.as_usize()]; let bb = &func.bbs[self.bb_idx.as_usize()];
@ -204,7 +204,6 @@ impl<'cx> AsmCtxt<'cx> {
match (ptr_value, value) { match (ptr_value, value) {
(RegValue::StackRelative { offset }, Operand::Const(c)) => { (RegValue::StackRelative { offset }, Operand::Const(c)) => {
let offset_from_cur = self.current_stack_offset - offset; let offset_from_cur = self.current_stack_offset - offset;
dbg!(offset_from_cur, c);
self.a self.a
.mov(x::qword_ptr(x::rsp + offset_from_cur), c.as_i32()) .mov(x::qword_ptr(x::rsp + offset_from_cur), c.as_i32())
@ -258,11 +257,9 @@ impl<'cx> AsmCtxt<'cx> {
match bb.term { match bb.term {
Branch::Ret(_) => { Branch::Ret(_) => {
self.a // Epilogue: Restore rsp, rbp and return.
.add(x::rsp, i32::try_from(self.current_stack_offset).unwrap()) self.a.mov(x::rsp, x::rbp).sp(func.def_span)?;
.sp(func.def_span)?; self.a.pop(x::rbp).sp(func.def_span)?;
self.a.pop(x::rsp).sp(func.def_span)?;
self.a.pop(x::rbx).sp(func.def_span)?;
self.a.mov(x::rax, 0_u64).sp(func.def_span)?; self.a.mov(x::rax, 0_u64).sp(func.def_span)?;
self.a.ret().sp(func.def_span)?; self.a.ret().sp(func.def_span)?;
break; break;