Skip to content

Commit

Permalink
[X64] implementing #36
Browse files Browse the repository at this point in the history
  • Loading branch information
Cr0a3 committed Oct 26, 2024
1 parent 0cdeb39 commit 056c3fb
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
6 changes: 6 additions & 0 deletions src/CodeGen/compilation/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ pub enum ConstImmRules {
pub struct CompilationHelper {
pub(crate) arch: Arch,
pub(crate) lower: Option<fn(CallConv, Vec<MachineInstr>) -> Vec<Box<dyn MCInstr>>>,
pub(crate) after_alloc: Option<fn(&CompilationHelper)>,

pub(crate) call: MachineCallingConvention,

Expand All @@ -69,6 +70,7 @@ impl CompilationHelper {
alloc: alloc,
tmp_reg: tmp,
fp_imm: ConstImmRules::InstrOp,
after_alloc: None,
}
}

Expand All @@ -78,6 +80,10 @@ impl CompilationHelper {
self.vars = self.alloc.vars.to_owned();
self.var_types = self.alloc.var_types.to_owned();
self.allocated_vars = self.alloc.allocated_vars.to_owned();

if let Some(func) = self.after_alloc {
func(self);
}
}

fn get_vars_to_save_for_call(&self, node: &crate::prelude::Call<crate::prelude::FuncId, Vec<crate::prelude::Var>, crate::prelude::Var>) -> Vec<(String, VarLocation)> {
Expand Down
10 changes: 10 additions & 0 deletions src/Target/x64/compilation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,15 @@ pub(crate) fn construct_compilation_helper(call_conv: CallConv) -> CompilationHe

helper.fp_imm = ConstImmRules::CreateConst;

helper.after_alloc = Some(x64_after_alloc);

helper
}

fn x64_after_alloc(compiler: &CompilationHelper) {
if compiler.alloc.stack_off < compiler.call.shadow(compiler.arch) {
unsafe {
super::lower::USE_SP_FOR_STACK = true;
}
}
}
8 changes: 7 additions & 1 deletion src/Target/x64/lower.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,15 @@ mod fcast;
use super::optimizer::X64AsmOpt;
use super::{instr::{Mnemonic, Operand, X64MCInstr}, X64Reg};

pub(crate) static mut USE_SP_FOR_STACK: bool = false;

macro_rules! x64_stack {
($off:expr) => {
Operand::Mem(X64Reg::Rbp - $off)
if unsafe {!USE_SP_FOR_STACK} {
Operand::Mem(X64Reg::Rbp - $off)
} else {
Operand::Mem(X64Reg::Rsp - $off)
}
};
}

Expand Down

0 comments on commit 056c3fb

Please sign in to comment.