From 4f0ee72c83f1f63d541fc6668765b7ffd7061fd5 Mon Sep 17 00:00:00 2001 From: sofia Date: Thu, 31 Jul 2025 22:48:16 +0300 Subject: [PATCH] Edit example a bit, fix macro generation in function parameters --- examples/macro_easy.reid | 3 +- reid/src/codegen/mod.rs | 106 ++++++++++++++++++++++----------------- reid/src/mir/macros.rs | 5 +- 3 files changed, 67 insertions(+), 47 deletions(-) diff --git a/examples/macro_easy.reid b/examples/macro_easy.reid index c67567e..cb10a69 100644 --- a/examples/macro_easy.reid +++ b/examples/macro_easy.reid @@ -4,5 +4,6 @@ import std::print; fn main() -> u8 { let bytes = include_bytes!("./macro_easy_file.txt"); print(String::new() + bytes.length()); - return (bytes as *u8)[0]; + print(String::new() + (include_bytes!("./macro_easy_file.txt") as *u8)[1] as u64); + return (include_bytes!("./macro_easy_file.txt") as *u8)[0]; } diff --git a/reid/src/codegen/mod.rs b/reid/src/codegen/mod.rs index 4262400..70a74de 100644 --- a/reid/src/codegen/mod.rs +++ b/reid/src/codegen/mod.rs @@ -1311,45 +1311,47 @@ impl mir::Expression { if val.1 == *type_kind { Some(val) } else { - match (&val.1, type_kind) { - (TypeKind::CodegenPtr(inner), TypeKind::UserPtr(ty2)) => match *inner.clone() { - TypeKind::UserPtr(_) => Some(StackValue( - val.0.derive( - scope - .block - .build(Instr::BitCast( - val.instr(), - Type::Ptr(Box::new(type_kind.get_type(scope.type_values))), - )) - .unwrap(), - ), - TypeKind::CodegenPtr(Box::new(type_kind.clone())), - )), - TypeKind::Borrow(ty1, _) => match *ty1.clone() { - TypeKind::Array(ty1, _) => { - if ty1 == *ty2 { - Some(StackValue( - val.0.derive( - scope - .block - .build(Instr::BitCast( - val.instr(), - Type::Ptr(Box::new(type_kind.get_type(scope.type_values))), - )) - .unwrap(), - ), - TypeKind::CodegenPtr(Box::new(type_kind.clone())), - )) - } else { - return Err(ErrorKind::Null); - } + let (ty, other) = if !state.should_load { + let TypeKind::CodegenPtr(inner) = &val.1 else { + panic!(); + }; + (*inner.clone(), TypeKind::CodegenPtr(Box::new(type_kind.clone()))) + } else { + (val.1.clone(), type_kind.clone()) + }; + + dbg!(&ty, type_kind); + + match (&ty, type_kind) { + (TypeKind::UserPtr(_), TypeKind::UserPtr(_)) => Some(StackValue( + val.0.derive( + scope + .block + .build(Instr::BitCast(val.instr(), other.get_type(scope.type_values))) + .unwrap(), + ), + other.clone(), + )), + (TypeKind::Borrow(ty1, _), TypeKind::UserPtr(ty2)) => { + if let TypeKind::Array(ty1, _) = ty1.as_ref() { + if ty1 == ty2 { + Some(StackValue( + val.0.derive( + scope + .block + .build(Instr::BitCast(val.instr(), other.get_type(scope.type_values))) + .unwrap(), + ), + other, + )) + } else { + return Err(ErrorKind::Null).unwrap(); } - _ => return Err(ErrorKind::Null), - }, - _ => panic!(), - }, - (TypeKind::UserPtr(_), TypeKind::UserPtr(_)) - | (TypeKind::Char, TypeKind::U8) + } else { + return Err(ErrorKind::Null).unwrap(); + } + } + (TypeKind::Char, TypeKind::U8) | (TypeKind::U8, TypeKind::Char) | (TypeKind::U8, TypeKind::I8) => Some(StackValue( val.0.derive( @@ -1361,8 +1363,7 @@ impl mir::Expression { type_kind.clone(), )), _ => { - let cast_instr = val - .1 + let cast_instr = ty .get_type(scope.type_values) .cast_instruction(val.instr(), &type_kind.get_type(scope.type_values)) .unwrap(); @@ -1378,11 +1379,26 @@ 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()), - ty.clone(), - )); - a + + let value = scope.block.build(Instr::GetGlobal(global_value.clone())).unwrap(); + + if !state.should_load { + let allocated = scope + .block + .build(Instr::Alloca(ty.get_type(scope.type_values))) + .unwrap(); + + scope.block.build(Instr::Store(allocated, value)).unwrap(); + + let a = Some(StackValue( + StackValueKind::Literal(allocated), + TypeKind::CodegenPtr(Box::new(ty.clone())), + )); + a + } else { + let a = Some(StackValue(StackValueKind::Literal(value), ty.clone())); + a + } } }; if let Some(value) = &value { diff --git a/reid/src/mir/macros.rs b/reid/src/mir/macros.rs index c87957c..843cbf9 100644 --- a/reid/src/mir/macros.rs +++ b/reid/src/mir/macros.rs @@ -118,10 +118,13 @@ impl mir::Expression { let mut globals = Vec::new(); match &mut self.0 { mir::ExprKind::FunctionCall(function_call) => { + for param in &mut function_call.parameters { + globals.extend(param.gen_macros(data, state, map)); + } if function_call.is_macro { if let Some(existing_macro) = data.macros.get(&function_call.name) { let mut literals = Vec::new(); - for param in &function_call.parameters { + for param in &mut function_call.parameters { match ¶m.0 { super::ExprKind::Literal(literal) => literals.push(literal.clone()), _ => state.note_errors(&vec![ErrorKind::InvalidMacroArgs], param.1),