Do similar change to allocator

This commit is contained in:
Sofia 2025-07-24 14:34:40 +03:00
parent 56b8506f50
commit 4a7f27205c
2 changed files with 25 additions and 20 deletions

View File

@ -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<InstructionValue> {
@ -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(

View File

@ -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,