Skip to content

Commit

Permalink
fix: set local variables offsets
Browse files Browse the repository at this point in the history
closes #27
  • Loading branch information
MilkeeyCat committed Jul 26, 2024
1 parent 560280e commit 48339e7
Showing 1 changed file with 17 additions and 15 deletions.
32 changes: 17 additions & 15 deletions src/codegen/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,22 +101,9 @@ impl<Arch: Architecture> CodeGen<Arch> {
}
}

fn function(&mut self, mut func: StmtFunction) {
fn function(&mut self, func: StmtFunction) {
self.scope.enter(*func.scope);
let mut offset: usize = 0;

for stmt in &mut func.body {
if let Stmt::VarDecl(var_decl) = stmt {
//TODO: clone bad
if let Symbol::Local(local) =
self.scope.clone().find_symbol_mut(&var_decl.name).unwrap()
{
local.offset = offset;
offset += local.type_.size::<Arch>(&self.scope);
}
}
}

let offset = self.populate_offsets(&func.body);
self.arch.fn_preamble(&func.name, offset);

for stmt in func.body {
Expand Down Expand Up @@ -330,4 +317,19 @@ impl<Arch: Architecture> CodeGen<Arch> {

Ok(())
}

fn populate_offsets(&mut self, stmts: &Vec<Stmt>) -> usize {
let mut offset: usize = 0;

for stmt in stmts {
if let Stmt::VarDecl(var_decl) = stmt {
if let Symbol::Local(local) = self.scope.find_symbol_mut(&var_decl.name).unwrap() {
local.offset = offset;
offset += var_decl.type_.size::<Arch>(&self.scope);
}
}
}

offset
}
}

0 comments on commit 48339e7

Please sign in to comment.