Add reading file to buffer macro, only works for one global per file

This commit is contained in:
Sofia 2025-07-29 00:50:07 +03:00
parent ebe7fc8d75
commit f700c577f1
12 changed files with 48 additions and 11 deletions

View File

@ -1,4 +1,5 @@
fn main() -> u8 { fn main() -> u8 {
// let message = String::from(include_bytes!("./macro_easy_file.txt")); // 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];
} }

View File

@ -276,7 +276,7 @@ impl Instr {
Instr::ShiftRightLogical(..) => "lshr", Instr::ShiftRightLogical(..) => "lshr",
Instr::ShiftRightArithmetic(..) => "ashr", Instr::ShiftRightArithmetic(..) => "ashr",
Instr::ShiftLeft(..) => "shl", Instr::ShiftLeft(..) => "shl",
Instr::GetGlobal(global_value) => todo!(), Instr::GetGlobal(..) => "global",
} }
} }
} }

View File

@ -195,6 +195,7 @@ impl mir::Expression {
mir::ExprKind::AssociatedFunctionCall(ty, fn_call) => { mir::ExprKind::AssociatedFunctionCall(ty, fn_call) => {
allocated.extend(fn_call.allocate(&format!("{}::{}", ty, fn_call.name), scope)) allocated.extend(fn_call.allocate(&format!("{}::{}", ty, fn_call.name), scope))
} }
mir::ExprKind::GlobalRef(..) => {}
} }
allocated allocated

View File

@ -201,9 +201,10 @@ impl mir::Module {
insert_debug!(&TypeKind::CustomType(type_key.clone())); insert_debug!(&TypeKind::CustomType(type_key.clone()));
} }
let mut globals = HashMap::new();
for global in &self.globals { for global in &self.globals {
let (const_value, _) = global.kind.codegen(context, &type_values, &module)?; 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(); let mut functions = HashMap::new();
@ -379,6 +380,7 @@ impl mir::Module {
functions: &functions, functions: &functions,
types: &types, types: &types,
type_values: &type_values, type_values: &type_values,
globals: &globals,
stack_values: HashMap::new(), stack_values: HashMap::new(),
debug: Some(Debug { debug: Some(Debug {
info: &debug, info: &debug,
@ -461,6 +463,7 @@ impl mir::Module {
scope: compile_unit.clone(), scope: compile_unit.clone(),
types: &debug_types, types: &debug_types,
}), }),
globals: &globals,
binops: &binops, binops: &binops,
allocator: Rc::new(RefCell::new(allocator)), allocator: Rc::new(RefCell::new(allocator)),
}; };
@ -521,6 +524,7 @@ impl mir::Module {
scope: compile_unit.clone(), scope: compile_unit.clone(),
types: &debug_types, types: &debug_types,
}), }),
globals: &globals,
binops: &binops, binops: &binops,
allocator: Rc::new(RefCell::new(allocator)), allocator: Rc::new(RefCell::new(allocator)),
}; };
@ -724,6 +728,7 @@ impl mir::Statement {
mir::StmtKind::Let(NamedVariableRef(ty, name, meta), mutable, expression) => { mir::StmtKind::Let(NamedVariableRef(ty, name, meta), mutable, expression) => {
let value = expression.codegen(scope, &state)?.unwrap(); let value = expression.codegen(scope, &state)?.unwrap();
dbg!(&scope.allocator, &meta, &value.1);
let alloca = scope let alloca = scope
.allocate(meta, &value.1) .allocate(meta, &value.1)
.unwrap() .unwrap()
@ -1351,6 +1356,14 @@ impl mir::Expression {
} }
} }
mir::ExprKind::AssociatedFunctionCall(ty, call) => codegen_function_call(Some(ty), call, scope, state)?, 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 { if let Some(value) = &value {
value.instr().maybe_location(&mut scope.block, location.clone()); value.instr().maybe_location(&mut scope.block, location.clone());

View File

@ -1,7 +1,7 @@
use std::{cell::RefCell, collections::HashMap, mem, rc::Rc}; use std::{cell::RefCell, collections::HashMap, mem, rc::Rc};
use reid_lib::{ use reid_lib::{
builder::{InstructionValue, TypeValue}, builder::{GlobalValue, InstructionValue, TypeValue},
debug_information::{DebugInformation, DebugLocation, DebugScopeValue, DebugTypeValue}, debug_information::{DebugInformation, DebugLocation, DebugScopeValue, DebugTypeValue},
Block, Context, Function, Instr, Module, Block, Context, Function, Instr, Module,
}; };
@ -30,6 +30,7 @@ pub struct Scope<'ctx, 'scope> {
pub(super) functions: &'scope HashMap<String, ScopeFunctionKind<'ctx>>, pub(super) functions: &'scope HashMap<String, ScopeFunctionKind<'ctx>>,
pub(super) binops: &'scope HashMap<BinopKey, StackBinopDefinition<'ctx>>, pub(super) binops: &'scope HashMap<BinopKey, StackBinopDefinition<'ctx>>,
pub(super) stack_values: HashMap<String, StackValue>, pub(super) stack_values: HashMap<String, StackValue>,
pub(super) globals: &'scope HashMap<String, GlobalValue>,
pub(super) debug: Option<Debug<'ctx>>, pub(super) debug: Option<Debug<'ctx>>,
pub(super) allocator: Rc<RefCell<Allocator>>, pub(super) allocator: Rc<RefCell<Allocator>>,
} }
@ -51,6 +52,7 @@ impl<'ctx, 'a> Scope<'ctx, 'a> {
stack_values: self.stack_values.clone(), stack_values: self.stack_values.clone(),
debug: self.debug.clone(), debug: self.debug.clone(),
allocator: self.allocator.clone(), allocator: self.allocator.clone(),
globals: self.globals,
binops: self.binops, binops: self.binops,
} }
} }

View File

@ -280,6 +280,7 @@ impl Display for ExprKind {
write!(f, "::")?; write!(f, "::")?;
Display::fmt(function_call, f) Display::fmt(function_call, f)
} }
ExprKind::GlobalRef(global_value, type_kind) => write!(f, "todo globals"),
} }
} }
} }

View File

@ -429,6 +429,7 @@ impl Expression {
Err(_) => Ok((ReturnKind::Soft, type_kind.clone())), Err(_) => Ok((ReturnKind::Soft, type_kind.clone())),
}, },
AssociatedFunctionCall(_, fcall) => fcall.return_type(), 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::If(_) => None,
ExprKind::CastTo(expression, _) => expression.backing_var(), ExprKind::CastTo(expression, _) => expression.backing_var(),
ExprKind::AssociatedFunctionCall(..) => None, ExprKind::AssociatedFunctionCall(..) => None,
ExprKind::GlobalRef(..) => None,
} }
} }
@ -493,6 +495,7 @@ impl Expression {
ExprKind::Deref(_) => None, ExprKind::Deref(_) => None,
ExprKind::CastTo(expression, _) => expression.num_value()?, ExprKind::CastTo(expression, _) => expression.num_value()?,
ExprKind::AssociatedFunctionCall(..) => None, ExprKind::AssociatedFunctionCall(..) => None,
ExprKind::GlobalRef(..) => None,
}) })
} }
} }

View File

@ -173,6 +173,7 @@ impl mir::Expression {
mir::ExprKind::CastTo(expression, _) => { mir::ExprKind::CastTo(expression, _) => {
globals.extend(expression.gen_macros(data, state)); globals.extend(expression.gen_macros(data, state));
} }
mir::ExprKind::GlobalRef(..) => {}
} }
globals globals
} }
@ -211,12 +212,17 @@ impl MacroFunction for TestMacro {
.map(|c| GlobalKind::Literal(Literal::U8(*c))) .map(|c| GlobalKind::Literal(Literal::U8(*c)))
.collect::<Vec<_>>(); .collect::<Vec<_>>();
let len = literals.len();
let global_name = "sometestglobalvalue".to_owned();
let global = GlobalValue {
name: global_name.clone(),
kind: GlobalKind::Array(literals),
};
Ok(( Ok((
vec![GlobalValue { vec![global.clone()],
name: "sometestglobalvalue".to_owned(), mir::ExprKind::GlobalRef(global_name, TypeKind::U8),
kind: GlobalKind::Array(literals),
}],
mir::ExprKind::Literal(mir::Literal::Vague(mir::VagueLiteral::Number(5))),
)) ))
} }
} }

View File

@ -271,6 +271,7 @@ pub enum ExprKind {
Borrow(Box<Expression>, bool), Borrow(Box<Expression>, bool),
Deref(Box<Expression>), Deref(Box<Expression>),
CastTo(Box<Expression>, TypeKind), CastTo(Box<Expression>, TypeKind),
GlobalRef(String, TypeKind),
} }
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
@ -427,13 +428,13 @@ pub struct Module {
pub is_main: bool, pub is_main: bool,
} }
#[derive(Debug)] #[derive(Debug, Clone)]
pub struct GlobalValue { pub struct GlobalValue {
pub name: String, pub name: String,
pub kind: GlobalKind, pub kind: GlobalKind,
} }
#[derive(Debug)] #[derive(Debug, Clone)]
pub enum GlobalKind { pub enum GlobalKind {
Literal(Literal), Literal(Literal),
Array(Vec<GlobalKind>), Array(Vec<GlobalKind>),

View File

@ -615,6 +615,7 @@ impl Expression {
ExprKind::Borrow(expression, _) => expression.pass(pass, state, scope, mod_id)?, ExprKind::Borrow(expression, _) => expression.pass(pass, state, scope, mod_id)?,
ExprKind::Deref(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::CastTo(expression, _) => expression.pass(pass, state, scope, mod_id)?,
ExprKind::GlobalRef(..) => {}
} }
Ok(()) Ok(())
} }

View File

@ -767,6 +767,10 @@ impl Expression {
Ok(function_call.return_type.clone().resolve_ref(typerefs)) 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()),
} }
} }
} }

View File

@ -660,6 +660,10 @@ impl Expression {
// Provide function return type // Provide function return type
Ok(type_refs.from_type(&fn_call.ret).unwrap()) 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()),
} }
} }