Skip to content

Commit

Permalink
[FIX] finally fixing #8 after years and a lot of tears!! (that was ki…
Browse files Browse the repository at this point in the history
…nda poetic)
  • Loading branch information
toni5757 committed Aug 28, 2024
1 parent 957afd1 commit 1799706
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 17 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@
*.o
*.c
*.exe
*.out
*.out
.idx/*
.vscode/*
7 changes: 1 addition & 6 deletions src/IR/nodes/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,8 @@ impl Ir for Call<Function, Vec<Var>, Var> {
fn uses(&self, var: &Var) -> bool {
let mut uses = false;

if self.inner3 == *var {
uses = true;
}


for arg in &self.inner2 {
if *arg == *var {
if arg.name == var.name {
uses = true;
}
}
Expand Down
5 changes: 4 additions & 1 deletion src/Target/target_descr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,15 @@ impl BackendInfos {
}
}

/// Delets the variable of the varsStorage (giving out it's resources)
/// Delets the variable of the varsStorage (freeing it's resources)
pub(crate) fn drop(&mut self, var: &Var) {
if let Some(loc) = &self.varsStorage.get(var) {
if let VarStorage::Register(reg) = &loc {
self.dropReg(reg.clone());
} // don't decrease the stack offset because if it isn't at the bottom other variables will may be overriden
self.varsStorage.remove(var);
} else {
panic!("wanted to drop reg but it has no location");
}
}

Expand Down
8 changes: 3 additions & 5 deletions src/Target/x64/asm/optimizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,10 @@ impl Optimize<Instr> for Vec<Instr> {

if instr.mnemonic == Mnemonic::StartOptimization {
optimize = true;
}

if instr.mnemonic == Mnemonic::EndOptimization {
} else if instr.mnemonic == Mnemonic::EndOptimization {
optimize = false;
}

}
if !optimize {
out.push(instr.to_owned());
continue;
Expand Down
20 changes: 17 additions & 3 deletions src/Target/x64/compilation/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,13 @@ pub(crate) fn CompileCall(call: &Call<Function, Vec<Var>, Var>, registry: &mut T
let var = registry.backend.getVarByReg(reg.clone());

if let Some(var) = var { // get the var
asm.push( Instr::with2(Mnemonic::Mov, Operand::Mem(x64Reg::Rbp - (offset as u32)), Operand::Reg(reg.clone())) );
saved_var_memory_locations.insert(var.clone(), offset);
offset += 8;
if block.isVarUsedAfterNode(&boxed, &var) || call.inner2.contains(var) && registry.call.args64().contains(reg.as_any().downcast_ref::<x64Reg>().unwrap()) {
asm.push( Instr::with2(Mnemonic::Mov, Operand::Mem(x64Reg::Rbp - (offset as u32)), Operand::Reg(reg.clone())) );
saved_var_memory_locations.insert(var.clone(), offset);
offset += 8;
} /*else {
eprintln!("don't save {} which has location {:?}", var, reg);
}*/
}
}
}
Expand Down Expand Up @@ -51,9 +55,15 @@ pub(crate) fn CompileCall(call: &Call<Function, Vec<Var>, Var>, registry: &mut T
TypeMetadata::Void => vec![],
};

// don't change else some stuff will brake in some random linux vm idk why but it is so
//if args[reg_args] == x64Reg::Rsi { /*eprintln!("abc");*/ }

if args.contains(reg.as_any().downcast_ref::<x64Reg>().unwrap()) {
if let Some(offset) = saved_var_memory_locations.get(arg) {
asm.push( Instr::with2(Mnemonic::Mov, Operand::Reg(args[reg_args].boxed()), Operand::Mem(x64Reg::Rbp - (*offset as u32))));
} else {
eprintln!("arg {:?} wasn't saved but it needs to be put into {:?}", reg, args[reg_args]);
asm.push(Instr::with2(Mnemonic::Mov, Operand::Reg(args[reg_args].boxed()), Operand::Reg(reg.clone())));
}
} else {
asm.push(Instr::with2(Mnemonic::Mov, Operand::Reg(args[reg_args].boxed()), Operand::Reg(reg.clone())));
Expand Down Expand Up @@ -81,6 +91,10 @@ pub(crate) fn CompileCall(call: &Call<Function, Vec<Var>, Var>, registry: &mut T
}
}

if registry.call.reset_eax() {
asm.push( Instr::with2(Mnemonic::Xor, Operand::Reg(x64Reg::Eax.boxed()), Operand::Reg(x64Reg::Eax.boxed())));
}

asm.push( Instr::with1(Mnemonic::Call, Operand::Imm(0)) );
asm.push( Instr::with1(Mnemonic::Link, Operand::LinkDestination(call.inner1.name.to_string(), -4)) );

Expand Down
1 change: 1 addition & 0 deletions src/Target/x64/compilation/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ use super::{x64Reg, instr::*};

pub(crate) fn buildAsmX86<'a>(block: &'a Block, func: &Function, call: &CallConv, registry: &mut TargetBackendDescr<'a>) -> Vec<Instr> {
registry.block = Some(&block);
registry.backend.stackSafe = true;

let info = &mut registry.backend;

Expand Down
2 changes: 1 addition & 1 deletion src/Target/x64/compilation/prolog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ pub(crate) fn x64BuildProlog(_: &Block, registry: &mut TargetBackendDescr) -> Ve
let mut res = vec![];

if registry.backend.currStackOffsetForLocalVars != 0 || registry.backend.stackSafe {
res.push( Instr::with0(Mnemonic::Endbr64) );
//res.push( Instr::with0(Mnemonic::Endbr64) );
res.push( Instr::with1(Mnemonic::Push, Operand::Reg(x64Reg::Rbp.boxed())) );
res.push( Instr::with2(Mnemonic::Mov, Operand::Reg(x64Reg::Rbp.boxed()), Operand::Reg(x64Reg::Rsp.boxed())) );
res.push( Instr::with2(Mnemonic::Sub, Operand::Reg(x64Reg::Rsp.boxed()), Operand::Imm(registry.backend.shadow + 8)) );
Expand Down

0 comments on commit 1799706

Please sign in to comment.