From f700c577f1a70ec2c30f9c92a58a871cfbfa522f Mon Sep 17 00:00:00 2001 From: sofia Date: Tue, 29 Jul 2025 00:50:07 +0300 Subject: [PATCH] Add reading file to buffer macro, only works for one global per file --- examples/macro_easy.reid | 3 ++- reid-llvm-lib/src/lib.rs | 2 +- reid/src/codegen/allocator.rs | 1 + reid/src/codegen/mod.rs | 15 ++++++++++++++- reid/src/codegen/scope.rs | 4 +++- reid/src/mir/fmt.rs | 1 + reid/src/mir/implement.rs | 3 +++ reid/src/mir/macros.rs | 16 +++++++++++----- reid/src/mir/mod.rs | 5 +++-- reid/src/mir/pass.rs | 1 + reid/src/mir/typecheck/typecheck.rs | 4 ++++ reid/src/mir/typecheck/typeinference.rs | 4 ++++ 12 files changed, 48 insertions(+), 11 deletions(-) diff --git a/examples/macro_easy.reid b/examples/macro_easy.reid index f82b105..7026732 100644 --- a/examples/macro_easy.reid +++ b/examples/macro_easy.reid @@ -1,4 +1,5 @@ fn main() -> u8 { // let message = String::from(include_bytes!("./macro_easy_file.txt")); - return test_macro!("./examples/macro_easy_file.txt"); + let bytes = test_macro!("./examples/macro_easy_file.txt"); + return bytes[0]; } diff --git a/reid-llvm-lib/src/lib.rs b/reid-llvm-lib/src/lib.rs index a0a7b8d..64c4696 100644 --- a/reid-llvm-lib/src/lib.rs +++ b/reid-llvm-lib/src/lib.rs @@ -276,7 +276,7 @@ impl Instr { Instr::ShiftRightLogical(..) => "lshr", Instr::ShiftRightArithmetic(..) => "ashr", Instr::ShiftLeft(..) => "shl", - Instr::GetGlobal(global_value) => todo!(), + Instr::GetGlobal(..) => "global", } } } diff --git a/reid/src/codegen/allocator.rs b/reid/src/codegen/allocator.rs index 44aa43b..df3ae1d 100644 --- a/reid/src/codegen/allocator.rs +++ b/reid/src/codegen/allocator.rs @@ -195,6 +195,7 @@ impl mir::Expression { mir::ExprKind::AssociatedFunctionCall(ty, fn_call) => { allocated.extend(fn_call.allocate(&format!("{}::{}", ty, fn_call.name), scope)) } + mir::ExprKind::GlobalRef(..) => {} } allocated diff --git a/reid/src/codegen/mod.rs b/reid/src/codegen/mod.rs index 43e8eba..41bdf76 100644 --- a/reid/src/codegen/mod.rs +++ b/reid/src/codegen/mod.rs @@ -201,9 +201,10 @@ impl mir::Module { insert_debug!(&TypeKind::CustomType(type_key.clone())); } + let mut globals = HashMap::new(); for global in &self.globals { let (const_value, _) = global.kind.codegen(context, &type_values, &module)?; - module.add_global(&global.name, const_value); + globals.insert(global.name.clone(), module.add_global(&global.name, const_value)); } let mut functions = HashMap::new(); @@ -379,6 +380,7 @@ impl mir::Module { functions: &functions, types: &types, type_values: &type_values, + globals: &globals, stack_values: HashMap::new(), debug: Some(Debug { info: &debug, @@ -461,6 +463,7 @@ impl mir::Module { scope: compile_unit.clone(), types: &debug_types, }), + globals: &globals, binops: &binops, allocator: Rc::new(RefCell::new(allocator)), }; @@ -521,6 +524,7 @@ impl mir::Module { scope: compile_unit.clone(), types: &debug_types, }), + globals: &globals, binops: &binops, allocator: Rc::new(RefCell::new(allocator)), }; @@ -724,6 +728,7 @@ impl mir::Statement { mir::StmtKind::Let(NamedVariableRef(ty, name, meta), mutable, expression) => { let value = expression.codegen(scope, &state)?.unwrap(); + dbg!(&scope.allocator, &meta, &value.1); let alloca = scope .allocate(meta, &value.1) .unwrap() @@ -1351,6 +1356,14 @@ impl mir::Expression { } } mir::ExprKind::AssociatedFunctionCall(ty, call) => codegen_function_call(Some(ty), call, scope, state)?, + mir::ExprKind::GlobalRef(global_name, ty) => { + let global_value = scope.globals.get(global_name).unwrap(); + let a = Some(StackValue( + StackValueKind::Literal(scope.block.build(Instr::GetGlobal(global_value.clone())).unwrap()), + TypeKind::UserPtr(Box::new(ty.clone())), + )); + a + } }; if let Some(value) = &value { value.instr().maybe_location(&mut scope.block, location.clone()); diff --git a/reid/src/codegen/scope.rs b/reid/src/codegen/scope.rs index 19f20b5..9b11351 100644 --- a/reid/src/codegen/scope.rs +++ b/reid/src/codegen/scope.rs @@ -1,7 +1,7 @@ use std::{cell::RefCell, collections::HashMap, mem, rc::Rc}; use reid_lib::{ - builder::{InstructionValue, TypeValue}, + builder::{GlobalValue, InstructionValue, TypeValue}, debug_information::{DebugInformation, DebugLocation, DebugScopeValue, DebugTypeValue}, Block, Context, Function, Instr, Module, }; @@ -30,6 +30,7 @@ pub struct Scope<'ctx, 'scope> { pub(super) functions: &'scope HashMap>, pub(super) binops: &'scope HashMap>, pub(super) stack_values: HashMap, + pub(super) globals: &'scope HashMap, pub(super) debug: Option>, pub(super) allocator: Rc>, } @@ -51,6 +52,7 @@ impl<'ctx, 'a> Scope<'ctx, 'a> { stack_values: self.stack_values.clone(), debug: self.debug.clone(), allocator: self.allocator.clone(), + globals: self.globals, binops: self.binops, } } diff --git a/reid/src/mir/fmt.rs b/reid/src/mir/fmt.rs index 90c0c36..2ab0e3a 100644 --- a/reid/src/mir/fmt.rs +++ b/reid/src/mir/fmt.rs @@ -280,6 +280,7 @@ impl Display for ExprKind { write!(f, "::")?; Display::fmt(function_call, f) } + ExprKind::GlobalRef(global_value, type_kind) => write!(f, "todo globals"), } } } diff --git a/reid/src/mir/implement.rs b/reid/src/mir/implement.rs index bcf2b65..fd24349 100644 --- a/reid/src/mir/implement.rs +++ b/reid/src/mir/implement.rs @@ -429,6 +429,7 @@ impl Expression { Err(_) => Ok((ReturnKind::Soft, type_kind.clone())), }, AssociatedFunctionCall(_, fcall) => fcall.return_type(), + GlobalRef(_, type_kind) => Ok((ReturnKind::Soft, TypeKind::UserPtr(Box::new(type_kind.clone())))), } } @@ -448,6 +449,7 @@ impl Expression { ExprKind::If(_) => None, ExprKind::CastTo(expression, _) => expression.backing_var(), ExprKind::AssociatedFunctionCall(..) => None, + ExprKind::GlobalRef(..) => None, } } @@ -493,6 +495,7 @@ impl Expression { ExprKind::Deref(_) => None, ExprKind::CastTo(expression, _) => expression.num_value()?, ExprKind::AssociatedFunctionCall(..) => None, + ExprKind::GlobalRef(..) => None, }) } } diff --git a/reid/src/mir/macros.rs b/reid/src/mir/macros.rs index e8a2189..869d517 100644 --- a/reid/src/mir/macros.rs +++ b/reid/src/mir/macros.rs @@ -173,6 +173,7 @@ impl mir::Expression { mir::ExprKind::CastTo(expression, _) => { globals.extend(expression.gen_macros(data, state)); } + mir::ExprKind::GlobalRef(..) => {} } globals } @@ -211,12 +212,17 @@ impl MacroFunction for TestMacro { .map(|c| GlobalKind::Literal(Literal::U8(*c))) .collect::>(); + let len = literals.len(); + + let global_name = "sometestglobalvalue".to_owned(); + let global = GlobalValue { + name: global_name.clone(), + kind: GlobalKind::Array(literals), + }; + Ok(( - vec![GlobalValue { - name: "sometestglobalvalue".to_owned(), - kind: GlobalKind::Array(literals), - }], - mir::ExprKind::Literal(mir::Literal::Vague(mir::VagueLiteral::Number(5))), + vec![global.clone()], + mir::ExprKind::GlobalRef(global_name, TypeKind::U8), )) } } diff --git a/reid/src/mir/mod.rs b/reid/src/mir/mod.rs index 4a1520f..3736d0e 100644 --- a/reid/src/mir/mod.rs +++ b/reid/src/mir/mod.rs @@ -271,6 +271,7 @@ pub enum ExprKind { Borrow(Box, bool), Deref(Box), CastTo(Box, TypeKind), + GlobalRef(String, TypeKind), } #[derive(Debug, Clone)] @@ -427,13 +428,13 @@ pub struct Module { pub is_main: bool, } -#[derive(Debug)] +#[derive(Debug, Clone)] pub struct GlobalValue { pub name: String, pub kind: GlobalKind, } -#[derive(Debug)] +#[derive(Debug, Clone)] pub enum GlobalKind { Literal(Literal), Array(Vec), diff --git a/reid/src/mir/pass.rs b/reid/src/mir/pass.rs index a3d2ff3..8d2ce5d 100644 --- a/reid/src/mir/pass.rs +++ b/reid/src/mir/pass.rs @@ -615,6 +615,7 @@ impl Expression { ExprKind::Borrow(expression, _) => expression.pass(pass, state, scope, mod_id)?, ExprKind::Deref(expression) => expression.pass(pass, state, scope, mod_id)?, ExprKind::CastTo(expression, _) => expression.pass(pass, state, scope, mod_id)?, + ExprKind::GlobalRef(..) => {} } Ok(()) } diff --git a/reid/src/mir/typecheck/typecheck.rs b/reid/src/mir/typecheck/typecheck.rs index 872bb64..a7c1c6b 100644 --- a/reid/src/mir/typecheck/typecheck.rs +++ b/reid/src/mir/typecheck/typecheck.rs @@ -767,6 +767,10 @@ impl Expression { Ok(function_call.return_type.clone().resolve_ref(typerefs)) } } + ExprKind::GlobalRef(global_value, type_kind) => Ok(self + .return_type(typerefs, state.scope.module_id.unwrap()) + .map(|r| r.1) + .unwrap()), } } } diff --git a/reid/src/mir/typecheck/typeinference.rs b/reid/src/mir/typecheck/typeinference.rs index 4bb0264..b602575 100644 --- a/reid/src/mir/typecheck/typeinference.rs +++ b/reid/src/mir/typecheck/typeinference.rs @@ -660,6 +660,10 @@ impl Expression { // Provide function return type Ok(type_refs.from_type(&fn_call.ret).unwrap()) } + ExprKind::GlobalRef(global_value, type_kind) => Ok(self + .return_type(type_refs.types, state.scope.module_id.unwrap()) + .map(|r| type_refs.from_type(&r.1).unwrap()) + .unwrap()), } }