From 86eab29173ab8291f768a3bc1a9847ccea76adc9 Mon Sep 17 00:00:00 2001 From: sofia Date: Mon, 14 Jul 2025 01:20:50 +0300 Subject: [PATCH] Make nested arrays work --- reid-llvm-lib/src/builder.rs | 6 +---- reid-llvm-lib/src/compile.rs | 7 ++---- reid/src/codegen.rs | 49 ++++++++++++++++++------------------ test.ll | 38 ++++++++++++++++++++++++++++ 4 files changed, 66 insertions(+), 34 deletions(-) create mode 100644 test.ll diff --git a/reid-llvm-lib/src/builder.rs b/reid-llvm-lib/src/builder.rs index 354c630..943c437 100644 --- a/reid-llvm-lib/src/builder.rs +++ b/reid-llvm-lib/src/builder.rs @@ -349,11 +349,7 @@ impl InstructionValue { Load(_, ty) => Ok(ty.clone()), Store(_, value) => value.get_type(builder), ArrayAlloca(ty, _) => Ok(Type::Ptr(Box::new(ty.clone()))), - GetElemPtr(arr, _) => match arr.get_type(builder) { - Ok(Type::Ptr(elem_t)) => Ok(Type::Ptr(Box::new(*elem_t))), - Ok(_) => Err(()), - Err(_) => Err(()), - }, + GetElemPtr(ptr, _) => ptr.get_type(builder), } } } diff --git a/reid-llvm-lib/src/compile.rs b/reid-llvm-lib/src/compile.rs index a899552..9fba362 100644 --- a/reid-llvm-lib/src/compile.rs +++ b/reid-llvm-lib/src/compile.rs @@ -352,10 +352,6 @@ impl InstructionHolder { ArrayAlloca(ty, len) => { let array_len = ConstValue::U16(*len as u16).as_llvm(module.context_ref); let array_ty = Type::Ptr(Box::new(ty.clone())); - dbg!( - &ty.as_llvm(module.context_ref), - &array_ty.as_llvm(module.context_ref) - ); LLVMBuildArrayAlloca( module.builder_ref, ty.as_llvm(module.context_ref), @@ -371,12 +367,13 @@ impl InstructionHolder { .iter() .map(|idx| ConstValue::U32(*idx).as_llvm(module.context_ref)) .collect(); + LLVMBuildGEP2( module.builder_ref, elem_t.as_llvm(module.context_ref), module.values.get(arr).unwrap().value_ref, indices.as_mut_ptr(), - 1, + indices.len() as u32, c"array_gep".as_ptr(), ) } diff --git a/reid/src/codegen.rs b/reid/src/codegen.rs index d1b6974..33a2878 100644 --- a/reid/src/codegen.rs +++ b/reid/src/codegen.rs @@ -154,33 +154,22 @@ impl<'ctx, 'a> Scope<'ctx, 'a> { } impl IndexedVariableReference { - fn get_stack_value(&self, scope: &mut Scope) -> Option { + fn get_stack_value(&self, scope: &mut Scope) -> Option<(StackValue, Vec)> { use StackValueKind as Kind; match &self.kind { - mir::IndexedVariableReferenceKind::Named(NamedVariableRef(_, name, _)) => { - scope.stack_values.get(name).cloned() - } + mir::IndexedVariableReferenceKind::Named(NamedVariableRef(_, name, _)) => scope + .stack_values + .get(name) + .cloned() + .map(|v| (v, Vec::new())), mir::IndexedVariableReferenceKind::Index(inner, idx) => { - let inner_val = inner.get_stack_value(scope)?; - let inner_instr = match inner_val.0 { - Kind::Immutable(val) => val, - Kind::Mutable(val) => val, - }; + let (inner_val, mut indices) = inner.get_stack_value(scope)?; - match inner_val.1 { - Type::Ptr(inner_ty) => { - let gep_instr = scope - .block - .build(Instr::GetElemPtr(inner_instr, vec![*idx as u32])) - .unwrap(); - Some(StackValue( - match inner_val.0 { - Kind::Immutable(_) => Kind::Immutable(gep_instr), - Kind::Mutable(_) => Kind::Mutable(gep_instr), - }, - *inner_ty, - )) + match &inner_val.1 { + Type::Ptr(_) => { + indices.push(*idx as u32); + Some((inner_val, indices)) } _ => panic!("Tried to codegen indexing a non-indexable value!"), } @@ -217,12 +206,24 @@ impl mir::Statement { None } mir::StmtKind::Set(var, val) => { - if let Some(StackValue(kind, _)) = var.get_stack_value(scope) { + if let Some((StackValue(kind, mut ty), indices)) = var.get_stack_value(scope) { match kind { StackValueKind::Immutable(_) => { panic!("Tried to mutate an immutable variable") } - StackValueKind::Mutable(ptr) => { + StackValueKind::Mutable(mut ptr) => { + for (i, idx) in indices.iter().enumerate() { + let Type::Ptr(inner) = ty else { panic!() }; + ty = *inner; + + ptr = scope + .block + .build(Instr::GetElemPtr(ptr, vec![*idx])) + .unwrap(); + if i < (indices.len() - 1) { + ptr = scope.block.build(Instr::Load(ptr, ty.clone())).unwrap() + } + } let expression = val.codegen(scope).unwrap(); Some(scope.block.build(Instr::Store(ptr, expression)).unwrap()) } diff --git a/test.ll b/test.ll new file mode 100644 index 0000000..a6fa6ea --- /dev/null +++ b/test.ll @@ -0,0 +1,38 @@ +; ModuleID = 'test' +source_filename = "test" +target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128" +target triple = "x86_64-unknown-linux-gnu" + +define ptr @array() { +array: + %array_alloca = alloca i16, i16 4, align 2 + %array_gep = getelementptr i16, ptr %array_alloca, i32 0 + store i16 10, ptr %array_gep, align 2 + %array_gep1 = getelementptr i16, ptr %array_alloca, i32 1 + store i16 15, ptr %array_gep1, align 2 + %array_gep2 = getelementptr i16, ptr %array_alloca, i32 2 + store i16 7, ptr %array_gep2, align 2 + %array_gep3 = getelementptr i16, ptr %array_alloca, i32 3 + store i16 9, ptr %array_gep3, align 2 + + %array_alloca4 = alloca ptr, i16 1, align 8 + %array_gep5 = getelementptr ptr, ptr %array_alloca4, i32 0 + store ptr %array_alloca, ptr %array_gep5, align 8 + ret ptr %array_alloca4 +} + +define i16 @main() { +main: + %call = call ptr @array() + %array_gep = getelementptr ptr, ptr %call, i32 0 + %load5 = load ptr, ptr %array_gep2, align 8 + %array_gep1 = getelementptr i16, ptr %load5, i32 3 + store i16 5, ptr %array_gep1, align 2 + + %array_gep2 = getelementptr ptr, ptr %call, i32 0 + %load = load ptr, ptr %array_gep2, align 8 + %array_gep3 = getelementptr i16, ptr %load, i32 3 + %load4 = load i16, ptr %array_gep3, align 2 + + ret i16 %load4 +} \ No newline at end of file