Get inner arrays working

This commit is contained in:
Sofia 2025-07-20 19:30:34 +03:00
parent a62f9db422
commit c723ba7b4a
4 changed files with 22 additions and 20 deletions

View File

@ -392,7 +392,6 @@ impl Builder {
Instr::Alloca(_, _) => Ok(()), Instr::Alloca(_, _) => Ok(()),
Instr::Load(ptr, load_ty) => { Instr::Load(ptr, load_ty) => {
let ptr_ty = ptr.get_type(&self)?; let ptr_ty = ptr.get_type(&self)?;
dbg!(&ptr_ty, &load_ty);
if let Type::Ptr(ptr_ty_inner) = ptr_ty { if let Type::Ptr(ptr_ty_inner) = ptr_ty {
if *ptr_ty_inner == load_ty { if *ptr_ty_inner == load_ty {
Ok(()) Ok(())
@ -414,11 +413,9 @@ impl Builder {
Instr::ArrayAlloca(_, _) => Ok(()), Instr::ArrayAlloca(_, _) => Ok(()),
Instr::GetElemPtr(ptr_val, _) => { Instr::GetElemPtr(ptr_val, _) => {
let ptr_ty = ptr_val.get_type(&self)?; let ptr_ty = ptr_val.get_type(&self)?;
dbg!(&ptr_ty);
match ptr_ty { match ptr_ty {
Type::Ptr(inner) => match *inner { Type::Ptr(_) => Ok(()),
Type::Array(_, _) => Ok(()),
_ => Err(()),
},
_ => Err(()), _ => Err(()),
} }
} }

View File

@ -411,10 +411,10 @@ impl InstructionValue {
let Type::Ptr(inner_ty) = instr_ty else { let Type::Ptr(inner_ty) = instr_ty else {
panic!("GetStructElemPtr on non-pointer! ({:?})", &instr_ty) panic!("GetStructElemPtr on non-pointer! ({:?})", &instr_ty)
}; };
let Type::Array(elem_ty, _) = *inner_ty else { match *inner_ty {
panic!("GetStructElemPtr on non-struct! ({:?})", &inner_ty) Type::Array(elem_ty, _) => Ok(Type::Ptr(Box::new(*elem_ty.clone()))),
}; _ => Ok(*inner_ty),
Ok(Type::Ptr(Box::new(*elem_ty.clone()))) }
} }
GetStructElemPtr(instr, idx) => { GetStructElemPtr(instr, idx) => {
let instr_ty = instr.get_type(builder)?; let instr_ty = instr.get_type(builder)?;

View File

@ -645,7 +645,7 @@ impl mir::Expression {
} }
mir::ExprKind::Indexed(expression, val_t, idx_expr) => { mir::ExprKind::Indexed(expression, val_t, idx_expr) => {
let StackValue(kind, array_ty) = expression let StackValue(kind, array_ty) = expression
.codegen(scope, &state.load(true)) .codegen(scope, &state.load(false))
.expect("array returned none!"); .expect("array returned none!");
let idx = idx_expr let idx = idx_expr
.codegen(scope, &state.load(true)) .codegen(scope, &state.load(true))
@ -657,6 +657,7 @@ impl mir::Expression {
.build(Instr::Constant(ConstValue::U32(0))) .build(Instr::Constant(ConstValue::U32(0)))
.unwrap(); .unwrap();
dbg!(&self, &val_t);
let ptr = scope let ptr = scope
.block .block
.build(Instr::GetElemPtr(kind.instr(), vec![first, idx])) .build(Instr::GetElemPtr(kind.instr(), vec![first, idx]))
@ -667,14 +668,18 @@ impl mir::Expression {
panic!(); panic!();
}; };
let elem_value = scope let elem_value = if state.should_load {
scope
.block .block
.build(Instr::Load( .build(Instr::Load(
ptr, ptr,
val_t.get_type(scope.type_values, scope.types), val_t.get_type(scope.type_values, scope.types),
)) ))
.unwrap() .unwrap()
.maybe_location(&mut scope.block, location); .maybe_location(&mut scope.block, location)
} else {
ptr
};
Some(StackValue(kind.derive(elem_value), *elem_ty)) Some(StackValue(kind.derive(elem_value), *elem_ty))
} }

View File

@ -1,7 +1,7 @@
// Arithmetic, function calls and imports! // Arithmetic, function calls and imports!
fn main() -> u16 { fn main() -> u16 {
let a = [ 5, 3, 2 ]; let a = [[ 5, 3, 2 ]];
return a[1]; return a[0][1];
} }