diff --git a/reid-llvm-lib/src/builder.rs b/reid-llvm-lib/src/builder.rs index 6d7bf29..3294d5a 100644 --- a/reid-llvm-lib/src/builder.rs +++ b/reid-llvm-lib/src/builder.rs @@ -392,7 +392,6 @@ impl Builder { Instr::Alloca(_, _) => Ok(()), Instr::Load(ptr, load_ty) => { let ptr_ty = ptr.get_type(&self)?; - dbg!(&ptr_ty, &load_ty); if let Type::Ptr(ptr_ty_inner) = ptr_ty { if *ptr_ty_inner == load_ty { Ok(()) @@ -414,11 +413,9 @@ impl Builder { Instr::ArrayAlloca(_, _) => Ok(()), Instr::GetElemPtr(ptr_val, _) => { let ptr_ty = ptr_val.get_type(&self)?; + dbg!(&ptr_ty); match ptr_ty { - Type::Ptr(inner) => match *inner { - Type::Array(_, _) => Ok(()), - _ => Err(()), - }, + Type::Ptr(_) => Ok(()), _ => Err(()), } } diff --git a/reid-llvm-lib/src/lib.rs b/reid-llvm-lib/src/lib.rs index c39a6ee..67690f2 100644 --- a/reid-llvm-lib/src/lib.rs +++ b/reid-llvm-lib/src/lib.rs @@ -411,10 +411,10 @@ impl InstructionValue { let Type::Ptr(inner_ty) = instr_ty else { panic!("GetStructElemPtr on non-pointer! ({:?})", &instr_ty) }; - let Type::Array(elem_ty, _) = *inner_ty else { - panic!("GetStructElemPtr on non-struct! ({:?})", &inner_ty) - }; - Ok(Type::Ptr(Box::new(*elem_ty.clone()))) + match *inner_ty { + Type::Array(elem_ty, _) => Ok(Type::Ptr(Box::new(*elem_ty.clone()))), + _ => Ok(*inner_ty), + } } GetStructElemPtr(instr, idx) => { let instr_ty = instr.get_type(builder)?; diff --git a/reid/src/codegen.rs b/reid/src/codegen.rs index cd15f05..42ad238 100644 --- a/reid/src/codegen.rs +++ b/reid/src/codegen.rs @@ -645,7 +645,7 @@ impl mir::Expression { } mir::ExprKind::Indexed(expression, val_t, idx_expr) => { let StackValue(kind, array_ty) = expression - .codegen(scope, &state.load(true)) + .codegen(scope, &state.load(false)) .expect("array returned none!"); let idx = idx_expr .codegen(scope, &state.load(true)) @@ -657,6 +657,7 @@ impl mir::Expression { .build(Instr::Constant(ConstValue::U32(0))) .unwrap(); + dbg!(&self, &val_t); let ptr = scope .block .build(Instr::GetElemPtr(kind.instr(), vec![first, idx])) @@ -667,14 +668,18 @@ impl mir::Expression { panic!(); }; - let elem_value = scope - .block - .build(Instr::Load( - ptr, - val_t.get_type(scope.type_values, scope.types), - )) - .unwrap() - .maybe_location(&mut scope.block, location); + let elem_value = if state.should_load { + scope + .block + .build(Instr::Load( + ptr, + val_t.get_type(scope.type_values, scope.types), + )) + .unwrap() + .maybe_location(&mut scope.block, location) + } else { + ptr + }; Some(StackValue(kind.derive(elem_value), *elem_ty)) } diff --git a/reid_src/array.reid b/reid_src/array.reid index e1badac..2bdb1c2 100644 --- a/reid_src/array.reid +++ b/reid_src/array.reid @@ -1,7 +1,7 @@ // Arithmetic, function calls and imports! fn main() -> u16 { - let a = [ 5, 3, 2 ]; + let a = [[ 5, 3, 2 ]]; - return a[1]; + return a[0][1]; }