From dbd048c21c2792599ac29f19262933e1e10d4441 Mon Sep 17 00:00:00 2001 From: sofia Date: Wed, 16 Jul 2025 20:38:21 +0300 Subject: [PATCH] Fix struct indexing when setting a field --- reid-llvm-lib/src/compile.rs | 10 +--------- reid-llvm-lib/src/lib.rs | 2 -- reid/src/codegen.rs | 31 +++++++++++++++++++++++-------- 3 files changed, 24 insertions(+), 19 deletions(-) diff --git a/reid-llvm-lib/src/compile.rs b/reid-llvm-lib/src/compile.rs index c41aee4..e6779f7 100644 --- a/reid-llvm-lib/src/compile.rs +++ b/reid-llvm-lib/src/compile.rs @@ -496,15 +496,7 @@ impl InstructionHolder { module.values.get(arr).unwrap().value_ref, llvm_indices.as_mut_ptr(), llvm_indices.len() as u32, - into_cstring(format!( - "array_gep_{:?}", - indices - .iter() - .map(|v| v.to_string()) - .collect::>() - .join("_") - )) - .as_ptr(), + into_cstring(format!("array_gep")).as_ptr(), ) } GetStructElemPtr(struct_val, idx) => { diff --git a/reid-llvm-lib/src/lib.rs b/reid-llvm-lib/src/lib.rs index d6fb8b5..9eee9cd 100644 --- a/reid-llvm-lib/src/lib.rs +++ b/reid-llvm-lib/src/lib.rs @@ -317,8 +317,6 @@ impl InstructionValue { GetElemPtr(ptr, _) => ptr.get_type(builder), GetStructElemPtr(instr, idx) => { let instr_ty = instr.get_type(builder)?; - dbg!(&builder); - dbg!(&instr, &instr_ty, idx); let Type::Ptr(inner_ty) = instr_ty else { panic!("GetStructElemPtr on non-pointer! ({:?})", &instr_ty) }; diff --git a/reid/src/codegen.rs b/reid/src/codegen.rs index b3ac0c8..9f99114 100644 --- a/reid/src/codegen.rs +++ b/reid/src/codegen.rs @@ -248,14 +248,24 @@ impl mir::Statement { panic!("Tried to mutate an immutable variable") } StackValueKind::Mutable(mut ptr) => { - for (i, idx) in indices.iter().enumerate() { + for (i, idx_kind) in indices.iter().enumerate() { let Type::Ptr(inner) = ty else { panic!() }; ty = *inner; - ptr = scope - .block - .build(Instr::GetElemPtr(ptr, vec![*idx])) - .unwrap(); + match idx_kind { + IndexKind::Array(idx) => { + ptr = scope + .block + .build(Instr::GetElemPtr(ptr, vec![*idx])) + .unwrap(); + } + IndexKind::Struct(idx) => { + ptr = scope + .block + .build(Instr::GetStructElemPtr(ptr, *idx)) + .unwrap(); + } + } if i < (indices.len() - 1) { ptr = scope.block.build(Instr::Load(ptr, ty.clone())).unwrap() } @@ -508,8 +518,13 @@ impl mir::Expression { } } +pub enum IndexKind { + Array(u32), + Struct(u32), +} + impl IndexedVariableReference { - fn get_stack_value(&self, scope: &mut Scope) -> Option<(StackValue, Vec)> { + fn get_stack_value(&self, scope: &mut Scope) -> Option<(StackValue, Vec)> { match &self.kind { mir::IndexedVariableReferenceKind::Named(NamedVariableRef(_, name, _)) => scope .stack_values @@ -521,7 +536,7 @@ impl IndexedVariableReference { match &inner_val.1 { Type::Ptr(_) => { - indices.push(*idx as u32); + indices.push(IndexKind::Array(*idx as u32)); Some((inner_val, indices)) } _ => panic!("Tried to codegen indexing a non-indexable value!"), @@ -545,7 +560,7 @@ impl IndexedVariableReference { None }?; - indices.push(idx as u32); + indices.push(IndexKind::Struct(idx as u32)); Some(( StackValue( inner_val.0,