diff --git a/reid/src/allocator.rs b/reid/src/allocator.rs index 4ea09bb..94928c4 100644 --- a/reid/src/allocator.rs +++ b/reid/src/allocator.rs @@ -18,11 +18,16 @@ pub struct Allocator { pub struct AllocatorScope<'ctx, 'a> { pub(super) block: &'a mut Block<'ctx>, pub(super) module_id: SourceModuleId, - pub(super) types: &'a HashMap, pub(super) type_values: &'a HashMap, } impl Allocator { + pub fn empty() -> Allocator { + Allocator { + allocations: Vec::new(), + } + } + pub fn from(func: &FunctionDefinition, scope: &mut AllocatorScope) -> Allocator { func.allocate(scope) } @@ -44,22 +49,21 @@ impl mir::FunctionDefinition { fn allocate<'ctx, 'a>(&self, scope: &mut AllocatorScope<'ctx, 'a>) -> Allocator { let mut allocated = Vec::new(); match &self.kind { - crate::mir::FunctionDefinitionKind::Local(block, _) => { + mir::FunctionDefinitionKind::Local(block, _) => { for param in &self.parameters { let allocation = scope .block .build_named( param.0.clone(), - reid_lib::Instr::Alloca( - param.1.get_type(scope.type_values, scope.types), - ), + reid_lib::Instr::Alloca(param.1.get_type(scope.type_values)), ) .unwrap(); allocated.push(Allocation(param.0.clone(), param.1.clone(), allocation)); } allocated.extend(block.allocate(scope)); } - crate::mir::FunctionDefinitionKind::Extern(_) => {} + mir::FunctionDefinitionKind::Extern(_) => {} + mir::FunctionDefinitionKind::Intrinsic(_) => {} } Allocator { @@ -95,11 +99,7 @@ impl mir::Statement { .block .build_named( named_variable_ref.1.clone(), - reid_lib::Instr::Alloca( - named_variable_ref - .0 - .get_type(scope.type_values, scope.types), - ), + reid_lib::Instr::Alloca(named_variable_ref.0.get_type(scope.type_values)), ) .unwrap(); allocated.push(Allocation( diff --git a/reid/src/codegen.rs b/reid/src/codegen.rs index deb27da..e53b9c6 100644 --- a/reid/src/codegen.rs +++ b/reid/src/codegen.rs @@ -275,7 +275,7 @@ impl mir::Module { // TODO: Reorder custom-type definitions such that // inner types get evaluated first. Otherwise this // will cause a panic! - .map(|StructField(_, t, _)| t.get_type(&type_values, &types)) + .map(|StructField(_, t, _)| t.get_type(&type_values)) .collect(), ))) } @@ -291,14 +291,14 @@ impl mir::Module { let param_types: Vec = function .parameters .iter() - .map(|(_, p)| p.get_type(&type_values, &types)) + .map(|(_, p)| p.get_type(&type_values)) .collect(); let is_main = self.is_main && function.name == "main"; let func = match &function.kind { mir::FunctionDefinitionKind::Local(_, _) => module.function( &function.name, - function.return_type.get_type(&type_values, &types), + function.return_type.get_type(&type_values), param_types, FunctionFlags { is_pub: function.is_pub || is_main, @@ -309,7 +309,7 @@ impl mir::Module { ), mir::FunctionDefinitionKind::Extern(imported) => module.function( &function.name, - function.return_type.get_type(&type_values, &types), + function.return_type.get_type(&type_values), param_types, FunctionFlags { is_extern: true, @@ -317,6 +317,7 @@ impl mir::Module { ..FunctionFlags::default() }, ), + mir::FunctionDefinitionKind::Intrinsic(instrinsic_kind) => todo!(), }; functions.insert(function.name.clone(), StackFunction { ir: func }); @@ -333,7 +334,6 @@ impl mir::Module { &mut AllocatorScope { block: &mut entry, module_id: self.module_id, - types: &types, type_values: &type_values, }, ); @@ -470,6 +470,26 @@ impl mir::Module { } } mir::FunctionDefinitionKind::Extern(_) => {} + mir::FunctionDefinitionKind::Intrinsic(kind) => { + let mut entry = function.ir.block("entry"); + let mut scope = Scope { + context, + modules: &modules, + tokens, + module: &module, + module_id: self.module_id, + function, + block: entry, + functions: &functions, + types: &types, + type_values: &type_values, + stack_values: Default::default(), + debug: None, + allocator: Rc::new(RefCell::new(Allocator::empty())), + }; + + kind.codegen(&mut scope)? + } } } @@ -694,7 +714,7 @@ impl mir::Expression { format!("{}", varref.1), Instr::Load( v.0.instr(), - inner.get_type(scope.type_values, scope.types), + inner.get_type(scope.type_values), ), ) .unwrap(), @@ -800,7 +820,7 @@ impl mir::Expression { .known() .expect("function return type unknown"); - let ret_type = ret_type_kind.get_type(scope.type_values, scope.types); + let ret_type = ret_type_kind.get_type(scope.type_values); let params = try_all( call.parameters @@ -903,10 +923,7 @@ impl mir::Expression { .block .build_named( "load", - Instr::Load( - kind.instr(), - inner.get_type(scope.type_values, scope.types), - ), + Instr::Load(kind.instr(), inner.get_type(scope.type_values)), ) .unwrap(); ( @@ -962,10 +979,7 @@ impl mir::Expression { .block .build_named( "array.load", - Instr::Load( - ptr, - contained_ty.get_type(scope.type_values, scope.types), - ), + Instr::Load(ptr, contained_ty.get_type(scope.type_values)), ) .unwrap() .maybe_location(&mut scope.block, location), @@ -1003,7 +1017,7 @@ impl mir::Expression { .unwrap_or(TypeKind::Void); let array_ty = Type::Array( - Box::new(elem_ty_kind.get_type(scope.type_values, scope.types)), + Box::new(elem_ty_kind.get_type(scope.type_values)), instr_list.len() as u64, ); let array_name = format!("{}.{}", elem_ty_kind, instr_list.len()); @@ -1086,10 +1100,7 @@ impl mir::Expression { .block .build_named( load_n, - Instr::Load( - value, - type_kind.get_type(scope.type_values, scope.types), - ), + Instr::Load(value, type_kind.get_type(scope.type_values)), ) .unwrap(), ), @@ -1180,10 +1191,7 @@ impl mir::Expression { .block .build_named( format!("{}.deref", varref.1), - Instr::Load( - v.0.instr(), - ptr_inner.get_type(scope.type_values, scope.types), - ), + Instr::Load(v.0.instr(), ptr_inner.get_type(scope.type_values)), ) .unwrap(); @@ -1198,7 +1206,7 @@ impl mir::Expression { format!("{}.deref.inner", varref.1), Instr::Load( var_ptr_instr, - inner.get_type(scope.type_values, scope.types), + inner.get_type(scope.type_values), ), ) .unwrap(), @@ -1236,7 +1244,7 @@ impl mir::Expression { .build(Instr::BitCast( val.instr(), Type::Ptr(Box::new( - type_kind.get_type(scope.type_values, scope.types), + type_kind.get_type(scope.type_values), )), )) .unwrap(), @@ -1254,7 +1262,7 @@ impl mir::Expression { .block .build(Instr::BitCast( val.instr(), - type_kind.get_type(scope.type_values, scope.types), + type_kind.get_type(scope.type_values), )) .unwrap(), ), @@ -1263,10 +1271,10 @@ impl mir::Expression { _ => { let cast_instr = val .1 - .get_type(scope.type_values, scope.types) + .get_type(scope.type_values) .cast_instruction( val.instr(), - &type_kind.get_type(scope.type_values, scope.types), + &type_kind.get_type(scope.type_values), ) .unwrap(); @@ -1438,11 +1446,7 @@ impl mir::Literal { } impl TypeKind { - pub(super) fn get_type( - &self, - type_vals: &HashMap, - typedefs: &HashMap, - ) -> Type { + pub(super) fn get_type(&self, type_vals: &HashMap) -> Type { match &self { TypeKind::I8 => Type::I8, TypeKind::I16 => Type::I16, @@ -1463,24 +1467,16 @@ impl TypeKind { TypeKind::F80 => Type::F80, TypeKind::F128PPC => Type::F128PPC, TypeKind::Char => Type::U8, - TypeKind::Array(elem_t, len) => { - Type::Array(Box::new(elem_t.get_type(type_vals, typedefs)), *len) - } + TypeKind::Array(elem_t, len) => Type::Array(Box::new(elem_t.get_type(type_vals)), *len), TypeKind::Void => Type::Void, TypeKind::Vague(_) => panic!("Tried to compile a vague type!"), TypeKind::CustomType(n) => { let type_val = type_vals.get(n).unwrap().clone(); Type::CustomType(type_val) } - TypeKind::UserPtr(type_kind) => { - Type::Ptr(Box::new(type_kind.get_type(type_vals, typedefs))) - } - TypeKind::CodegenPtr(type_kind) => { - Type::Ptr(Box::new(type_kind.get_type(type_vals, typedefs))) - } - TypeKind::Borrow(type_kind, _) => { - Type::Ptr(Box::new(type_kind.get_type(type_vals, typedefs))) - } + TypeKind::UserPtr(type_kind) => Type::Ptr(Box::new(type_kind.get_type(type_vals))), + TypeKind::CodegenPtr(type_kind) => Type::Ptr(Box::new(type_kind.get_type(type_vals))), + TypeKind::Borrow(type_kind, _) => Type::Ptr(Box::new(type_kind.get_type(type_vals))), } } } diff --git a/reid/src/intrinsics.rs b/reid/src/intrinsics.rs new file mode 100644 index 0000000..880537b --- /dev/null +++ b/reid/src/intrinsics.rs @@ -0,0 +1,10 @@ +use crate::codegen::{ErrorKind, Scope, StackValue}; + +#[derive(Debug)] +pub enum InstrinsicKind {} + +impl InstrinsicKind { + pub fn codegen<'ctx, 'a>(&self, mut scope: &mut Scope<'ctx, 'a>) -> Result<(), ErrorKind> { + Ok(()) + } +} diff --git a/reid/src/lib.rs b/reid/src/lib.rs index 9d8ac7c..8133e24 100644 --- a/reid/src/lib.rs +++ b/reid/src/lib.rs @@ -56,6 +56,7 @@ mod allocator; mod ast; mod codegen; pub mod error_raporting; +pub mod intrinsics; pub mod ld; mod lexer; pub mod mir; diff --git a/reid/src/mir/mod.rs b/reid/src/mir/mod.rs index 6e32bdd..4482d4a 100644 --- a/reid/src/mir/mod.rs +++ b/reid/src/mir/mod.rs @@ -5,6 +5,7 @@ use std::{collections::HashMap, path::PathBuf}; use crate::{ + intrinsics::InstrinsicKind, lexer::{FullToken, Position}, token_stream::TokenRange, }; @@ -303,7 +304,7 @@ pub enum FunctionDefinitionKind { /// True = imported from other module, False = Is user defined extern Extern(bool), /// Intrinsic definition, defined within the compiler - Intrinsic(Block), + Intrinsic(InstrinsicKind), } impl FunctionDefinition { diff --git a/reid/src/mir/pass.rs b/reid/src/mir/pass.rs index f67ad74..50a3257 100644 --- a/reid/src/mir/pass.rs +++ b/reid/src/mir/pass.rs @@ -352,6 +352,7 @@ impl FunctionDefinition { block.pass(pass, state, scope, mod_id)?; } FunctionDefinitionKind::Extern(_) => {} + FunctionDefinitionKind::Intrinsic(..) => {} }; Ok(()) } diff --git a/reid/src/mir/typecheck.rs b/reid/src/mir/typecheck.rs index 2c8fe9b..ec66b2d 100644 --- a/reid/src/mir/typecheck.rs +++ b/reid/src/mir/typecheck.rs @@ -196,6 +196,9 @@ impl FunctionDefinition { FunctionDefinitionKind::Extern(_) => { Ok((ReturnKind::Soft, TypeKind::Vague(Vague::Unknown))) } + FunctionDefinitionKind::Intrinsic(..) => { + Ok((ReturnKind::Soft, TypeKind::Vague(Vague::Unknown))) + } }; match inferred { diff --git a/reid/src/mir/typeinference.rs b/reid/src/mir/typeinference.rs index cfb1bcb..e2ab97e 100644 --- a/reid/src/mir/typeinference.rs +++ b/reid/src/mir/typeinference.rs @@ -69,6 +69,7 @@ impl FunctionDefinition { } } FunctionDefinitionKind::Extern(_) => {} + FunctionDefinitionKind::Intrinsic(_) => todo!(), }; Ok(())