Add broken support for nested arrays

This commit is contained in:
Sofia 2025-07-14 00:05:05 +03:00
parent d2587df4c9
commit 5d06ecb874
6 changed files with 21 additions and 30 deletions

View File

@ -279,9 +279,9 @@ impl Builder {
} }
} }
ArrayAlloca(_, _) => Ok(()), ArrayAlloca(_, _) => Ok(()),
ArrayGEP(arr, _) => { GetElemPtr(arr, _) => {
let arr_ty = arr.get_type(&self)?; let arr_ty = arr.get_type(&self)?;
if let Type::ArrayPtr(_, _) = arr_ty { if let Type::Ptr(_) = arr_ty {
Ok(()) Ok(())
} else { } else {
Err(()) Err(())
@ -348,11 +348,9 @@ impl InstructionValue {
Alloca(_, ty) => Ok(Type::Ptr(Box::new(ty.clone()))), Alloca(_, ty) => Ok(Type::Ptr(Box::new(ty.clone()))),
Load(_, ty) => Ok(ty.clone()), Load(_, ty) => Ok(ty.clone()),
Store(_, value) => value.get_type(builder), Store(_, value) => value.get_type(builder),
ArrayAlloca(ty, len_value) => { ArrayAlloca(ty, _) => Ok(Type::Ptr(Box::new(ty.clone()))),
Ok(Type::ArrayPtr(Box::new(ty.clone()), len_value.clone())) GetElemPtr(arr, _) => match arr.get_type(builder) {
} Ok(Type::Ptr(elem_t)) => Ok(Type::Ptr(Box::new(*elem_t))),
ArrayGEP(arr, _) => match arr.get_type(builder) {
Ok(Type::ArrayPtr(elem_t, _)) => Ok(Type::Ptr(Box::new(*elem_t))),
Ok(_) => Err(()), Ok(_) => Err(()),
Err(_) => Err(()), Err(_) => Err(()),
}, },
@ -396,7 +394,6 @@ impl Type {
Type::Bool => true, Type::Bool => true,
Type::Void => false, Type::Void => false,
Type::Ptr(_) => false, Type::Ptr(_) => false,
Type::ArrayPtr(_, _) => false,
} }
} }
@ -415,7 +412,6 @@ impl Type {
Type::Bool => false, Type::Bool => false,
Type::Void => false, Type::Void => false,
Type::Ptr(_) => false, Type::Ptr(_) => false,
Type::ArrayPtr(_, _) => false,
} }
} }
} }

View File

@ -351,7 +351,7 @@ impl InstructionHolder {
), ),
ArrayAlloca(ty, len) => { ArrayAlloca(ty, len) => {
let array_len = ConstValue::U16(*len as u16).as_llvm(module.context_ref); let array_len = ConstValue::U16(*len as u16).as_llvm(module.context_ref);
let array_ty = Type::ArrayPtr(Box::new(ty.clone()), *len); let array_ty = Type::Ptr(Box::new(ty.clone()));
dbg!( dbg!(
&ty.as_llvm(module.context_ref), &ty.as_llvm(module.context_ref),
&array_ty.as_llvm(module.context_ref) &array_ty.as_llvm(module.context_ref)
@ -363,11 +363,9 @@ impl InstructionHolder {
c"array_alloca".as_ptr(), c"array_alloca".as_ptr(),
) )
} }
ArrayGEP(arr, indices) => { GetElemPtr(arr, indices) => {
let t = arr.get_type(module.builder).unwrap(); let t = arr.get_type(module.builder).unwrap();
let Type::ArrayPtr(elem_t, _) = t else { let Type::Ptr(elem_t) = t else { panic!() };
panic!()
};
let mut indices: Vec<_> = indices let mut indices: Vec<_> = indices
.iter() .iter()
@ -478,7 +476,6 @@ impl Type {
Bool => LLVMInt1TypeInContext(context), Bool => LLVMInt1TypeInContext(context),
Void => LLVMVoidType(), Void => LLVMVoidType(),
Ptr(ty) => LLVMPointerType(ty.as_llvm(context), 0), Ptr(ty) => LLVMPointerType(ty.as_llvm(context), 0),
ArrayPtr(elem_t, _) => LLVMPointerType(elem_t.as_llvm(context), 0),
} }
} }
} }

View File

@ -103,7 +103,7 @@ impl Debug for Instr {
Instr::ArrayAlloca(ty, instruction_value) => { Instr::ArrayAlloca(ty, instruction_value) => {
write!(f, "array_alloca<{:?}>({:?})", ty, instruction_value) write!(f, "array_alloca<{:?}>({:?})", ty, instruction_value)
} }
Instr::ArrayGEP(instruction_value, items) => fmt_index( Instr::GetElemPtr(instruction_value, items) => fmt_index(
f, f,
instruction_value, instruction_value,
&items &items

View File

@ -178,7 +178,7 @@ pub enum Instr {
Load(InstructionValue, Type), Load(InstructionValue, Type),
Store(InstructionValue, InstructionValue), Store(InstructionValue, InstructionValue),
ArrayAlloca(Type, u32), ArrayAlloca(Type, u32),
ArrayGEP(InstructionValue, Vec<u32>), GetElemPtr(InstructionValue, Vec<u32>),
/// Integer Comparison /// Integer Comparison
ICmp(CmpPredicate, InstructionValue, InstructionValue), ICmp(CmpPredicate, InstructionValue, InstructionValue),
@ -201,7 +201,6 @@ pub enum Type {
Bool, Bool,
Void, Void,
Ptr(Box<Type>), Ptr(Box<Type>),
ArrayPtr(Box<Type>, u32),
} }
#[derive(Debug, Clone, Hash)] #[derive(Debug, Clone, Hash)]

View File

@ -1,7 +1,7 @@
// Arithmetic, function calls and imports! // Arithmetic, function calls and imports!
fn array() -> [u16; 4] { fn array() -> [[u16; 4]; 1] {
return [10, 15, 7, 9]; return [[10, 15, 7, 9]];
} }
fn main() -> u16 { fn main() -> u16 {
@ -9,7 +9,7 @@ fn main() -> u16 {
let mut list = array(); let mut list = array();
list[3] = 5; list[0][3] = 5;
return list[3]; return list[0][3];
} }

View File

@ -169,10 +169,10 @@ impl IndexedVariableReference {
}; };
match inner_val.1 { match inner_val.1 {
Type::ArrayPtr(inner_ty, _) => { Type::Ptr(inner_ty) => {
let gep_instr = scope let gep_instr = scope
.block .block
.build(Instr::ArrayGEP(inner_instr, vec![*idx as u32])) .build(Instr::GetElemPtr(inner_instr, vec![*idx as u32]))
.unwrap(); .unwrap();
Some(StackValue( Some(StackValue(
match inner_val.0 { match inner_val.0 {
@ -306,7 +306,8 @@ impl mir::Expression {
Some(match v.0 { Some(match v.0 {
StackValueKind::Immutable(val) => val.clone(), StackValueKind::Immutable(val) => val.clone(),
StackValueKind::Mutable(val) => match v.1 { StackValueKind::Mutable(val) => match v.1 {
Type::ArrayPtr(_, _) => val, // TODO probably wrong ..?
Type::Ptr(_) => val,
_ => scope.block.build(Instr::Load(val, v.1.clone())).unwrap(), _ => scope.block.build(Instr::Load(val, v.1.clone())).unwrap(),
}, },
}) })
@ -377,7 +378,7 @@ impl mir::Expression {
let array = expression.codegen(scope)?; let array = expression.codegen(scope)?;
let ptr = scope let ptr = scope
.block .block
.build(Instr::ArrayGEP(array, vec![*idx as u32])) .build(Instr::GetElemPtr(array, vec![*idx as u32]))
.unwrap(); .unwrap();
Some( Some(
@ -409,7 +410,7 @@ impl mir::Expression {
for (i, instr) in instr_list.iter().enumerate() { for (i, instr) in instr_list.iter().enumerate() {
let ptr = scope let ptr = scope
.block .block
.build(Instr::ArrayGEP(array, vec![i as u32])) .build(Instr::GetElemPtr(array, vec![i as u32]))
.unwrap(); .unwrap();
scope.block.build(Instr::Store(ptr, *instr)).unwrap(); scope.block.build(Instr::Store(ptr, *instr)).unwrap();
} }
@ -494,9 +495,7 @@ impl TypeKind {
TypeKind::U64 => Type::U64, TypeKind::U64 => Type::U64,
TypeKind::U128 => Type::U128, TypeKind::U128 => Type::U128,
TypeKind::Bool => Type::Bool, TypeKind::Bool => Type::Bool,
TypeKind::Array(elem_t, len) => { TypeKind::Array(elem_t, _) => Type::Ptr(Box::new(elem_t.get_type())),
Type::ArrayPtr(Box::new(elem_t.get_type()), *len as u32)
}
TypeKind::Void => panic!("Void not a supported type"), TypeKind::Void => panic!("Void not a supported type"),
TypeKind::Vague(_) => panic!("Tried to compile a vague type!"), TypeKind::Vague(_) => panic!("Tried to compile a vague type!"),
} }