From 48339e7808ad9a86b7aeff1981986a3f9fd4ad3a Mon Sep 17 00:00:00 2001 From: MilkeeyCat Date: Fri, 26 Jul 2024 20:20:13 +0300 Subject: [PATCH] fix: set local variables offsets closes #27 --- src/codegen/codegen.rs | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/src/codegen/codegen.rs b/src/codegen/codegen.rs index f558f2e..23df230 100644 --- a/src/codegen/codegen.rs +++ b/src/codegen/codegen.rs @@ -101,22 +101,9 @@ impl CodeGen { } } - 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::(&self.scope); - } - } - } - + let offset = self.populate_offsets(&func.body); self.arch.fn_preamble(&func.name, offset); for stmt in func.body { @@ -330,4 +317,19 @@ impl CodeGen { Ok(()) } + + fn populate_offsets(&mut self, stmts: &Vec) -> 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::(&self.scope); + } + } + } + + offset + } }