diff --git a/reid/src/allocator.rs b/reid/src/allocator.rs index 94928c4..79181c7 100644 --- a/reid/src/allocator.rs +++ b/reid/src/allocator.rs @@ -6,8 +6,8 @@ use reid_lib::{ }; use crate::mir::{ - self, CustomTypeKey, FunctionCall, FunctionDefinition, IfExpression, SourceModuleId, - TypeDefinition, TypeKind, WhileStatement, + self, CustomTypeKey, FunctionCall, FunctionDefinition, FunctionDefinitionKind, IfExpression, + SourceModuleId, TypeDefinition, TypeKind, WhileStatement, }; #[derive(Debug)] @@ -28,8 +28,12 @@ impl Allocator { } } - pub fn from(func: &FunctionDefinition, scope: &mut AllocatorScope) -> Allocator { - func.allocate(scope) + pub fn from( + func: &FunctionDefinitionKind, + params: &Vec<(String, TypeKind)>, + scope: &mut AllocatorScope, + ) -> Allocator { + func.allocate(scope, params) } pub fn allocate(&mut self, name: &String, ty: &TypeKind) -> Option { @@ -45,12 +49,16 @@ impl Allocator { #[derive(Clone, Debug)] pub struct Allocation(String, TypeKind, InstructionValue); -impl mir::FunctionDefinition { - fn allocate<'ctx, 'a>(&self, scope: &mut AllocatorScope<'ctx, 'a>) -> Allocator { +impl mir::FunctionDefinitionKind { + fn allocate<'ctx, 'a>( + &self, + scope: &mut AllocatorScope<'ctx, 'a>, + parameters: &Vec<(String, TypeKind)>, + ) -> Allocator { let mut allocated = Vec::new(); - match &self.kind { + match &self { mir::FunctionDefinitionKind::Local(block, _) => { - for param in &self.parameters { + for param in parameters { let allocation = scope .block .build_named( diff --git a/reid/src/codegen.rs b/reid/src/codegen.rs index 057209c..fef1ac2 100644 --- a/reid/src/codegen.rs +++ b/reid/src/codegen.rs @@ -355,18 +355,15 @@ impl mir::Module { let function = functions.get(&mir_function.name).unwrap(); let mut entry = function.ir.block("entry"); - let allocator = match &mir_function.kind { - FunctionDefinitionKind::Local(..) => Allocator::from( - mir_function, - &mut AllocatorScope { - block: &mut entry, - module_id: self.module_id, - type_values: &type_values, - }, - ), - FunctionDefinitionKind::Extern(_) => Allocator::empty(), - FunctionDefinitionKind::Intrinsic(_) => Allocator::empty(), - }; + let allocator = Allocator::from( + &mir_function.kind, + &mir_function.parameters, + &mut AllocatorScope { + block: &mut entry, + module_id: self.module_id, + type_values: &type_values, + }, + ); let mut scope = Scope { context,