From 954f3438d38ee9987bbaad0093128857071c27fc Mon Sep 17 00:00:00 2001 From: sofia Date: Thu, 24 Jul 2025 11:56:44 +0300 Subject: [PATCH] Codegen intrinsics --- examples/hello_world.reid | 4 +-- reid/src/ast/process.rs | 2 -- reid/src/codegen.rs | 4 +-- reid/src/intrinsics.rs | 55 ++++++++++++++++++++++++++++++++--- reid/src/lib.rs | 5 ++++ reid/src/mir/linker.rs | 1 - reid/src/mir/mod.rs | 1 - reid/src/mir/typeinference.rs | 2 +- reid/tests/e2e.rs | 2 +- 9 files changed, 62 insertions(+), 14 deletions(-) diff --git a/examples/hello_world.reid b/examples/hello_world.reid index f6792e1..bf34034 100644 --- a/examples/hello_world.reid +++ b/examples/hello_world.reid @@ -7,7 +7,7 @@ import std::new_string; import std::add_num_to_str; import std::concat_strings; -fn main() -> i32 { +fn main() -> u8 { let mut test = from_str("hello"); concat_strings(&mut test, from_str(" world")); @@ -21,5 +21,5 @@ fn main() -> i32 { free_string(&test); - return 0; + return addition(5, 3); } diff --git a/reid/src/ast/process.rs b/reid/src/ast/process.rs index 8b19356..466297f 100644 --- a/reid/src/ast/process.rs +++ b/reid/src/ast/process.rs @@ -50,7 +50,6 @@ impl ast::Module { block.into_mir(module_id), (*range).as_meta(module_id), ), - source: module_id, }; functions.push(def); } @@ -71,7 +70,6 @@ impl ast::Module { .map(|p| (p.0, p.1 .0.into_mir(module_id))) .collect(), kind: mir::FunctionDefinitionKind::Extern(false), - source: module_id, }; functions.push(def); } diff --git a/reid/src/codegen.rs b/reid/src/codegen.rs index 189f8da..c559215 100644 --- a/reid/src/codegen.rs +++ b/reid/src/codegen.rs @@ -317,7 +317,7 @@ impl mir::Module { ..FunctionFlags::default() }, ), - mir::FunctionDefinitionKind::Intrinsic(instrinsic_kind) => module.function( + mir::FunctionDefinitionKind::Intrinsic(_) => module.function( &function.name, function.return_type.get_type(&type_values), param_types, @@ -478,7 +478,7 @@ impl mir::Module { } mir::FunctionDefinitionKind::Extern(_) => {} mir::FunctionDefinitionKind::Intrinsic(kind) => { - let mut entry = function.ir.block("entry"); + let entry = function.ir.block("entry"); let mut scope = Scope { context, modules: &modules, diff --git a/reid/src/intrinsics.rs b/reid/src/intrinsics.rs index 880537b..40fc221 100644 --- a/reid/src/intrinsics.rs +++ b/reid/src/intrinsics.rs @@ -1,10 +1,57 @@ -use crate::codegen::{ErrorKind, Scope, StackValue}; +use reid_lib::Instr; -#[derive(Debug)] -pub enum InstrinsicKind {} +use crate::{ + codegen::{ErrorKind, Scope}, + mir::{FunctionDefinition, FunctionDefinitionKind, TypeKind}, +}; + +#[derive(Debug, Clone, Copy)] +pub enum InstrinsicKind { + IAdd, +} + +fn intrinsic( + name: &str, + ret_ty: TypeKind, + params: Vec<(&str, TypeKind)>, + kind: InstrinsicKind, +) -> FunctionDefinition { + FunctionDefinition { + name: name.into(), + is_pub: false, + is_imported: false, + return_type: ret_ty, + parameters: params.into_iter().map(|(n, ty)| (n.into(), ty)).collect(), + kind: FunctionDefinitionKind::Intrinsic(kind), + } +} + +pub fn form_intrinsics() -> Vec { + let mut intrinsics = Vec::new(); + + intrinsics.push(intrinsic( + "addition", + TypeKind::U8, + vec![("lhs".into(), TypeKind::U8), ("rhs".into(), TypeKind::U8)], + InstrinsicKind::IAdd, + )); + + intrinsics +} impl InstrinsicKind { - pub fn codegen<'ctx, 'a>(&self, mut scope: &mut Scope<'ctx, 'a>) -> Result<(), ErrorKind> { + pub fn codegen<'ctx, 'a>(&self, scope: &mut Scope<'ctx, 'a>) -> Result<(), ErrorKind> { + match self { + InstrinsicKind::IAdd => { + let lhs = scope.block.build(Instr::Param(0)).unwrap(); + let rhs = scope.block.build(Instr::Param(1)).unwrap(); + let add = scope.block.build(Instr::Add(lhs, rhs)).unwrap(); + scope + .block + .terminate(reid_lib::TerminatorKind::Ret(add)) + .unwrap() + } + } Ok(()) } } diff --git a/reid/src/lib.rs b/reid/src/lib.rs index 8133e24..fbca027 100644 --- a/reid/src/lib.rs +++ b/reid/src/lib.rs @@ -44,6 +44,7 @@ use std::path::PathBuf; use error_raporting::{ErrorKind as ErrorRapKind, ErrorModules, ReidError}; +use intrinsics::form_intrinsics; use lexer::FullToken; use mir::{ linker::LinkerPass, typecheck::TypeCheck, typeinference::TypeInference, typerefs::TypeRefs, @@ -127,6 +128,10 @@ pub fn perform_all_passes<'map>( #[cfg(debug_assertions)] dbg!(&context); + for module in &mut context.modules { + module.1.functions.extend(form_intrinsics()); + } + #[cfg(debug_assertions)] println!("{}", &context); diff --git a/reid/src/mir/linker.rs b/reid/src/mir/linker.rs index e404c67..b962249 100644 --- a/reid/src/mir/linker.rs +++ b/reid/src/mir/linker.rs @@ -331,7 +331,6 @@ impl<'map> Pass for LinkerPass<'map> { return_type, parameters: param_tys, kind: super::FunctionDefinitionKind::Extern(true), - source: imported_mod_id, }); } } diff --git a/reid/src/mir/mod.rs b/reid/src/mir/mod.rs index 4482d4a..f805f77 100644 --- a/reid/src/mir/mod.rs +++ b/reid/src/mir/mod.rs @@ -294,7 +294,6 @@ pub struct FunctionDefinition { pub return_type: TypeKind, pub parameters: Vec<(String, TypeKind)>, pub kind: FunctionDefinitionKind, - pub source: SourceModuleId, } #[derive(Debug)] diff --git a/reid/src/mir/typeinference.rs b/reid/src/mir/typeinference.rs index e2ab97e..0fa8f41 100644 --- a/reid/src/mir/typeinference.rs +++ b/reid/src/mir/typeinference.rs @@ -69,7 +69,7 @@ impl FunctionDefinition { } } FunctionDefinitionKind::Extern(_) => {} - FunctionDefinitionKind::Intrinsic(_) => todo!(), + FunctionDefinitionKind::Intrinsic(_) => {} }; Ok(()) diff --git a/reid/tests/e2e.rs b/reid/tests/e2e.rs index fbf738c..61e9815 100644 --- a/reid/tests/e2e.rs +++ b/reid/tests/e2e.rs @@ -98,7 +98,7 @@ fn float_compiles_well() { } #[test] fn hello_world_compiles_well() { - test(include_str!("../../examples/hello_world.reid"), "test", 0); + test(include_str!("../../examples/hello_world.reid"), "test", 8); } #[test] fn mutable_compiles_well() {