use std::io::{Error, Write};
pub struct X86Frame;
impl X86Frame {
pub fn generate_prologue<W: Write>(writer: &mut W, stack_size: usize) -> Result<(), Error> {
writeln!(writer, " pushq %rbp")?;
writeln!(writer, " movq %rsp, %rbp")?;
let stack_size = Self::aligned_frame_size(stack_size);
if stack_size > 0 {
writeln!(writer, " subq ${stack_size}, %rsp")?;
}
Ok(())
}
pub fn aligned_frame_size(stack_size: usize) -> usize {
stack_size.div_ceil(16) * 16
}
pub fn generate_epilogue<W: Write>(writer: &mut W, stack_size: usize) -> Result<(), Error> {
let stack_size = Self::aligned_frame_size(stack_size);
if stack_size > 0 {
writeln!(writer, " addq ${stack_size}, %rsp")?;
}
writeln!(writer, " popq %rbp")?;
writeln!(writer, " ret")?;
Ok(())
}
pub fn calculate_stack_offset(slot_index: usize) -> i32 {
-((slot_index as i32 + 1) * 8)
}
}