From 89850d7b4febdbd26dfe0348e2fca44940c0ad61 Mon Sep 17 00:00:00 2001 From: sofia Date: Mon, 28 Jul 2025 21:22:15 +0300 Subject: [PATCH] Change intrinsic alloca to malloc and actually use libc malloc --- reid/lib/std.reid | 8 ++++---- reid/src/codegen/intrinsics.rs | 21 ++++++++++++++------- reid/src/codegen/mod.rs | 10 +--------- 3 files changed, 19 insertions(+), 20 deletions(-) diff --git a/reid/lib/std.reid b/reid/lib/std.reid index dbf549f..d7022ad 100644 --- a/reid/lib/std.reid +++ b/reid/lib/std.reid @@ -13,7 +13,7 @@ struct String { impl String { pub fn new() -> String { String { - inner: char::alloca(0), + inner: char::malloc(0), length: 0, max_length: 0, must_be_freed: true, @@ -41,7 +41,7 @@ impl String { pub fn add_char(&mut self, c: char) { if ((*self).length + 1) >= (*self).max_length { - let new = char::alloca((*self).max_length + 4); + let new = char::malloc((*self).max_length + 4); copy_bits((*self).inner, new, (*self).max_length); if (*self).must_be_freed == true { @@ -121,7 +121,7 @@ pub fn int_div(numerator: i32, denominator: i32) -> div_t { pub fn new_string() -> String { String { - inner: char::alloca(0), + inner: char::malloc(0), length: 0, max_length: 0, must_be_freed: true, @@ -143,7 +143,7 @@ pub fn from_str(str: *char) -> String { pub fn add_char(string: &mut String, c: char) { if ((*string).length + 1) >= (*string).max_length { - let new = char::alloca((*string).max_length + 4); + let new = char::malloc((*string).max_length + 4); copy_bits((*string).inner, new, (*string).max_length); if (*string).must_be_freed == true { diff --git a/reid/src/codegen/intrinsics.rs b/reid/src/codegen/intrinsics.rs index e639df4..cb1cac3 100644 --- a/reid/src/codegen/intrinsics.rs +++ b/reid/src/codegen/intrinsics.rs @@ -33,7 +33,8 @@ const FLOATS: [TypeKind; 7] = [ TypeKind::F128PPC, ]; -const MALLOC_IDENT: &str = "reid.malloc"; +const INTRINSIC_IDENT: &str = "reid.intrinsic"; +const MALLOC_IDENT: &str = "malloc"; pub fn form_intrinsics() -> Vec { let mut intrinsics = Vec::new(); @@ -68,8 +69,8 @@ pub fn get_intrinsic_assoc_func(ty: &TypeKind, name: &str) -> Option Some(FunctionDefinition { - name: "alloca".to_owned(), + "malloc" => Some(FunctionDefinition { + name: "malloc".to_owned(), linkage_name: None, is_pub: true, is_imported: false, @@ -363,7 +364,7 @@ impl IntrinsicFunction for IntrinsicSizeOf { fn codegen<'ctx, 'a>(&self, scope: &mut Scope<'ctx, 'a>, _: &[StackValue]) -> Result { let instr = scope .block - .build(Instr::Constant(reid_lib::ConstValue::U64(self.0.size_of()))) + .build(Instr::Constant(reid_lib::ConstValue::U64(self.0.size_of() / 8))) .unwrap(); Ok(StackValue(StackValueKind::Literal(instr), self.0.clone())) } @@ -374,11 +375,17 @@ pub struct IntrinsicMalloc(TypeKind); impl IntrinsicFunction for IntrinsicMalloc { fn codegen<'ctx, 'a>(&self, scope: &mut Scope<'ctx, 'a>, params: &[StackValue]) -> Result { let amount = params.get(0).unwrap(); - let function = scope.block.find_function(&MALLOC_IDENT.to_owned()).unwrap(); - let instr = scope + let function = scope .block - .build(Instr::FunctionCall(function, vec![amount.instr()])) + .find_function(&format!("{}.{}", INTRINSIC_IDENT, MALLOC_IDENT)) .unwrap(); + + let sizeof = scope + .block + .build(Instr::Constant(ConstValue::U64(self.0.size_of() / 8))) + .unwrap(); + let bytes = scope.block.build(Instr::Mul(sizeof, amount.instr())).unwrap(); + let instr = scope.block.build(Instr::FunctionCall(function, vec![bytes])).unwrap(); Ok(StackValue(StackValueKind::Literal(instr), self.0.clone())) } } diff --git a/reid/src/codegen/mod.rs b/reid/src/codegen/mod.rs index eb77b57..a6257a1 100644 --- a/reid/src/codegen/mod.rs +++ b/reid/src/codegen/mod.rs @@ -188,10 +188,6 @@ impl mir::Module { let is_main = self.is_main && function.name == "main"; - let is_true_extern = match function.kind { - FunctionDefinitionKind::Extern(i) => !i, - _ => false, - }; let module_prefix = if let Some(module) = function.source { if module == self.module_id { format!("reid.{}.", self.name) @@ -222,11 +218,7 @@ impl mir::Module { }, )), mir::FunctionDefinitionKind::Extern(imported) => Some(module.function( - if function.source == None { - &function.name - } else { - &full_name - }, + &full_name, if function.source == None { Some(function.linkage_name.clone().unwrap()) } else {