Update allocator to remove dynamic allocations from lists/structs
This commit is contained in:
		
							parent
							
								
									ccee457cf4
								
							
						
					
					
						commit
						e14efa2ea7
					
				| @ -83,7 +83,11 @@ Common token used throughout this document to express parts of grammar include: | |||||||
|     "i8" | "i16" | "i32" | "i64" | "i128" | |     "i8" | "i16" | "i32" | "i64" | "i128" | | ||||||
|     "f16" | "f32" | "f32b" | "f64" | "f80" | "f128" | "f128ppc" |     "f16" | "f32" | "f32b" | "f64" | "f80" | "f128" | "f128ppc" | ||||||
| 
 | 
 | ||||||
| <binop> :: "+" | "-" | "*" | "/" | "%" | "&&" | <cmp> | <binop> :: "+" | "-" | "*"  | ||||||
|  |   | "/" | "%" | "&&" | "||"  | ||||||
|  |   | "&" | "|" | "^" | ">>"  | ||||||
|  |   | "<<" | <cmp> | ||||||
|  | 
 | ||||||
| <cmp> :: "<" | "<=" | "==" | "!=" | ">=" | >" | <cmp> :: "<" | "<=" | "==" | "!=" | ">=" | >" | ||||||
| <unary> :: "+" | "-" | "!" | <unary> :: "+" | "-" | "!" | ||||||
| ``` | ``` | ||||||
|  | |||||||
| @ -2,12 +2,12 @@ use std::collections::HashMap; | |||||||
| 
 | 
 | ||||||
| use reid_lib::{ | use reid_lib::{ | ||||||
|     builder::{InstructionValue, TypeValue}, |     builder::{InstructionValue, TypeValue}, | ||||||
|     Block, |     Block, Instr, | ||||||
| }; | }; | ||||||
| 
 | 
 | ||||||
| use mir::{CustomTypeKey, FunctionCall, FunctionDefinitionKind, IfExpression, TypeKind, WhileStatement}; | use mir::{CustomTypeKey, FunctionCall, FunctionDefinitionKind, IfExpression, TypeKind, WhileStatement}; | ||||||
| 
 | 
 | ||||||
| use crate::mir::{self, FunctionParam, Metadata}; | use crate::mir::{self, FunctionParam, Metadata, SourceModuleId}; | ||||||
| 
 | 
 | ||||||
| #[derive(Debug)] | #[derive(Debug)] | ||||||
| pub struct Allocator { | pub struct Allocator { | ||||||
| @ -16,6 +16,7 @@ pub struct Allocator { | |||||||
| 
 | 
 | ||||||
| pub struct AllocatorScope<'ctx, 'a> { | pub struct AllocatorScope<'ctx, 'a> { | ||||||
|     pub(super) block: &'a mut Block<'ctx>, |     pub(super) block: &'a mut Block<'ctx>, | ||||||
|  |     pub(super) mod_id: SourceModuleId, | ||||||
|     pub(super) type_values: &'a HashMap<CustomTypeKey, TypeValue>, |     pub(super) type_values: &'a HashMap<CustomTypeKey, TypeValue>, | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| @ -136,13 +137,38 @@ impl mir::Expression { | |||||||
|                 allocated.extend(expression.allocate(scope)); |                 allocated.extend(expression.allocate(scope)); | ||||||
|             } |             } | ||||||
|             mir::ExprKind::Array(expressions) => { |             mir::ExprKind::Array(expressions) => { | ||||||
|  |                 let (_, ty) = self.return_type(&Default::default(), scope.mod_id).unwrap(); | ||||||
|  |                 let TypeKind::Array(elem_ty, _) = &ty else { panic!() }; | ||||||
|  |                 let array_name = format!("{}.{}", elem_ty, expressions.len()); | ||||||
|  | 
 | ||||||
|  |                 let allocation = scope | ||||||
|  |                     .block | ||||||
|  |                     .build_named(array_name, Instr::Alloca(ty.get_type(scope.type_values))) | ||||||
|  |                     .unwrap(); | ||||||
|  |                 allocated.push(Allocation(self.1, ty, allocation)); | ||||||
|  | 
 | ||||||
|                 for expression in expressions { |                 for expression in expressions { | ||||||
|                     allocated.extend(expression.allocate(scope)); |                     allocated.extend(expression.allocate(scope)); | ||||||
|                 } |                 } | ||||||
|             } |             } | ||||||
|             mir::ExprKind::Struct(_, items) => { |             mir::ExprKind::Struct(name, items) => { | ||||||
|                 for (_, expression) in items { |                 let (_, ty) = self.return_type(&Default::default(), scope.mod_id).unwrap(); | ||||||
|  |                 let allocation = scope | ||||||
|  |                     .block | ||||||
|  |                     .build_named(name, Instr::Alloca(ty.get_type(scope.type_values))) | ||||||
|  |                     .unwrap(); | ||||||
|  |                 allocated.push(Allocation(self.1, ty, allocation)); | ||||||
|  | 
 | ||||||
|  |                 for (field_name, expression) in items { | ||||||
|                     allocated.extend(expression.allocate(scope)); |                     allocated.extend(expression.allocate(scope)); | ||||||
|  | 
 | ||||||
|  |                     let (_, ty) = expression.return_type(&Default::default(), scope.mod_id).unwrap(); | ||||||
|  | 
 | ||||||
|  |                     let allocation = scope | ||||||
|  |                         .block | ||||||
|  |                         .build_named(field_name, Instr::Alloca(ty.get_type(scope.type_values))) | ||||||
|  |                         .unwrap(); | ||||||
|  |                     allocated.push(Allocation(expression.1, ty, allocation)); | ||||||
|                 } |                 } | ||||||
|             } |             } | ||||||
|             mir::ExprKind::Literal(_) => {} |             mir::ExprKind::Literal(_) => {} | ||||||
| @ -186,10 +212,7 @@ impl mir::FunctionCall { | |||||||
|         if self.return_type != TypeKind::Void { |         if self.return_type != TypeKind::Void { | ||||||
|             let allocation = scope |             let allocation = scope | ||||||
|                 .block |                 .block | ||||||
|                 .build_named( |                 .build_named(name, Instr::Alloca(self.return_type.get_type(scope.type_values))) | ||||||
|                     name, |  | ||||||
|                     reid_lib::Instr::Alloca(self.return_type.get_type(scope.type_values)), |  | ||||||
|                 ) |  | ||||||
|                 .unwrap(); |                 .unwrap(); | ||||||
|             allocated.push(Allocation(self.meta, self.return_type.clone(), allocation)); |             allocated.push(Allocation(self.meta, self.return_type.clone(), allocation)); | ||||||
|         } |         } | ||||||
|  | |||||||
| @ -293,6 +293,7 @@ impl mir::Module { | |||||||
|                                 &mut AllocatorScope { |                                 &mut AllocatorScope { | ||||||
|                                     block: &mut entry, |                                     block: &mut entry, | ||||||
|                                     type_values: &type_values, |                                     type_values: &type_values, | ||||||
|  |                                     mod_id: self.module_id, | ||||||
|                                 }, |                                 }, | ||||||
|                             ); |                             ); | ||||||
| 
 | 
 | ||||||
| @ -367,6 +368,7 @@ impl mir::Module { | |||||||
|                     &mut AllocatorScope { |                     &mut AllocatorScope { | ||||||
|                         block: &mut entry, |                         block: &mut entry, | ||||||
|                         type_values: &type_values, |                         type_values: &type_values, | ||||||
|  |                         mod_id: self.module_id, | ||||||
|                     }, |                     }, | ||||||
|                 ); |                 ); | ||||||
| 
 | 
 | ||||||
| @ -426,6 +428,7 @@ impl mir::Module { | |||||||
|                     &mut AllocatorScope { |                     &mut AllocatorScope { | ||||||
|                         block: &mut entry, |                         block: &mut entry, | ||||||
|                         type_values: &type_values, |                         type_values: &type_values, | ||||||
|  |                         mod_id: self.module_id, | ||||||
|                     }, |                     }, | ||||||
|                 ); |                 ); | ||||||
| 
 | 
 | ||||||
| @ -1037,8 +1040,10 @@ impl mir::Expression { | |||||||
|                 let load_n = format!("{}.load", array_name); |                 let load_n = format!("{}.load", array_name); | ||||||
| 
 | 
 | ||||||
|                 let array = scope |                 let array = scope | ||||||
|                     .block |                     .allocate( | ||||||
|                     .build_named(&array_name, Instr::Alloca(array_ty.clone())) |                         &self.1, | ||||||
|  |                         &TypeKind::Array(Box::new(elem_ty_kind.clone()), expressions.len() as u64), | ||||||
|  |                     ) | ||||||
|                     .unwrap() |                     .unwrap() | ||||||
|                     .maybe_location(&mut scope.block, location.clone()); |                     .maybe_location(&mut scope.block, location.clone()); | ||||||
| 
 | 
 | ||||||
| @ -1135,8 +1140,7 @@ impl mir::Expression { | |||||||
|                 let load_n = format!("{}.load", name); |                 let load_n = format!("{}.load", name); | ||||||
| 
 | 
 | ||||||
|                 let struct_ptr = scope |                 let struct_ptr = scope | ||||||
|                     .block |                     .allocate(&self.1, &TypeKind::CustomType(type_key.clone())) | ||||||
|                     .build_named(name, Instr::Alloca(ty.clone())) |  | ||||||
|                     .unwrap() |                     .unwrap() | ||||||
|                     .maybe_location(&mut scope.block, location.clone()); |                     .maybe_location(&mut scope.block, location.clone()); | ||||||
| 
 | 
 | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user