Add broken support for nested arrays
This commit is contained in:
		
							parent
							
								
									d2587df4c9
								
							
						
					
					
						commit
						5d06ecb874
					
				@ -279,9 +279,9 @@ impl Builder {
 | 
			
		||||
                    }
 | 
			
		||||
                }
 | 
			
		||||
                ArrayAlloca(_, _) => Ok(()),
 | 
			
		||||
                ArrayGEP(arr, _) => {
 | 
			
		||||
                GetElemPtr(arr, _) => {
 | 
			
		||||
                    let arr_ty = arr.get_type(&self)?;
 | 
			
		||||
                    if let Type::ArrayPtr(_, _) = arr_ty {
 | 
			
		||||
                    if let Type::Ptr(_) = arr_ty {
 | 
			
		||||
                        Ok(())
 | 
			
		||||
                    } else {
 | 
			
		||||
                        Err(())
 | 
			
		||||
@ -348,11 +348,9 @@ impl InstructionValue {
 | 
			
		||||
                Alloca(_, ty) => Ok(Type::Ptr(Box::new(ty.clone()))),
 | 
			
		||||
                Load(_, ty) => Ok(ty.clone()),
 | 
			
		||||
                Store(_, value) => value.get_type(builder),
 | 
			
		||||
                ArrayAlloca(ty, len_value) => {
 | 
			
		||||
                    Ok(Type::ArrayPtr(Box::new(ty.clone()), len_value.clone()))
 | 
			
		||||
                }
 | 
			
		||||
                ArrayGEP(arr, _) => match arr.get_type(builder) {
 | 
			
		||||
                    Ok(Type::ArrayPtr(elem_t, _)) => Ok(Type::Ptr(Box::new(*elem_t))),
 | 
			
		||||
                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(()),
 | 
			
		||||
                },
 | 
			
		||||
@ -396,7 +394,6 @@ impl Type {
 | 
			
		||||
            Type::Bool => true,
 | 
			
		||||
            Type::Void => false,
 | 
			
		||||
            Type::Ptr(_) => false,
 | 
			
		||||
            Type::ArrayPtr(_, _) => false,
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@ -415,7 +412,6 @@ impl Type {
 | 
			
		||||
            Type::Bool => false,
 | 
			
		||||
            Type::Void => false,
 | 
			
		||||
            Type::Ptr(_) => false,
 | 
			
		||||
            Type::ArrayPtr(_, _) => false,
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -351,7 +351,7 @@ impl InstructionHolder {
 | 
			
		||||
                ),
 | 
			
		||||
                ArrayAlloca(ty, len) => {
 | 
			
		||||
                    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!(
 | 
			
		||||
                        &ty.as_llvm(module.context_ref),
 | 
			
		||||
                        &array_ty.as_llvm(module.context_ref)
 | 
			
		||||
@ -363,11 +363,9 @@ impl InstructionHolder {
 | 
			
		||||
                        c"array_alloca".as_ptr(),
 | 
			
		||||
                    )
 | 
			
		||||
                }
 | 
			
		||||
                ArrayGEP(arr, indices) => {
 | 
			
		||||
                GetElemPtr(arr, indices) => {
 | 
			
		||||
                    let t = arr.get_type(module.builder).unwrap();
 | 
			
		||||
                    let Type::ArrayPtr(elem_t, _) = t else {
 | 
			
		||||
                        panic!()
 | 
			
		||||
                    };
 | 
			
		||||
                    let Type::Ptr(elem_t) = t else { panic!() };
 | 
			
		||||
 | 
			
		||||
                    let mut indices: Vec<_> = indices
 | 
			
		||||
                        .iter()
 | 
			
		||||
@ -478,7 +476,6 @@ impl Type {
 | 
			
		||||
                Bool => LLVMInt1TypeInContext(context),
 | 
			
		||||
                Void => LLVMVoidType(),
 | 
			
		||||
                Ptr(ty) => LLVMPointerType(ty.as_llvm(context), 0),
 | 
			
		||||
                ArrayPtr(elem_t, _) => LLVMPointerType(elem_t.as_llvm(context), 0),
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@ -103,7 +103,7 @@ impl Debug for Instr {
 | 
			
		||||
            Instr::ArrayAlloca(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,
 | 
			
		||||
                instruction_value,
 | 
			
		||||
                &items
 | 
			
		||||
 | 
			
		||||
@ -178,7 +178,7 @@ pub enum Instr {
 | 
			
		||||
    Load(InstructionValue, Type),
 | 
			
		||||
    Store(InstructionValue, InstructionValue),
 | 
			
		||||
    ArrayAlloca(Type, u32),
 | 
			
		||||
    ArrayGEP(InstructionValue, Vec<u32>),
 | 
			
		||||
    GetElemPtr(InstructionValue, Vec<u32>),
 | 
			
		||||
 | 
			
		||||
    /// Integer Comparison
 | 
			
		||||
    ICmp(CmpPredicate, InstructionValue, InstructionValue),
 | 
			
		||||
@ -201,7 +201,6 @@ pub enum Type {
 | 
			
		||||
    Bool,
 | 
			
		||||
    Void,
 | 
			
		||||
    Ptr(Box<Type>),
 | 
			
		||||
    ArrayPtr(Box<Type>, u32),
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[derive(Debug, Clone, Hash)]
 | 
			
		||||
 | 
			
		||||
@ -1,7 +1,7 @@
 | 
			
		||||
// Arithmetic, function calls and imports!
 | 
			
		||||
 | 
			
		||||
fn array() -> [u16; 4] {
 | 
			
		||||
    return [10, 15, 7, 9];
 | 
			
		||||
fn array() -> [[u16; 4]; 1] {
 | 
			
		||||
    return [[10, 15, 7, 9]];
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
fn main() -> u16 {
 | 
			
		||||
@ -9,7 +9,7 @@ fn main() -> u16 {
 | 
			
		||||
 | 
			
		||||
    let mut list = array();
 | 
			
		||||
 | 
			
		||||
    list[3] = 5;
 | 
			
		||||
    list[0][3] = 5;
 | 
			
		||||
 | 
			
		||||
    return list[3];
 | 
			
		||||
    return list[0][3];
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -169,10 +169,10 @@ impl IndexedVariableReference {
 | 
			
		||||
                };
 | 
			
		||||
 | 
			
		||||
                match inner_val.1 {
 | 
			
		||||
                    Type::ArrayPtr(inner_ty, _) => {
 | 
			
		||||
                    Type::Ptr(inner_ty) => {
 | 
			
		||||
                        let gep_instr = scope
 | 
			
		||||
                            .block
 | 
			
		||||
                            .build(Instr::ArrayGEP(inner_instr, vec![*idx as u32]))
 | 
			
		||||
                            .build(Instr::GetElemPtr(inner_instr, vec![*idx as u32]))
 | 
			
		||||
                            .unwrap();
 | 
			
		||||
                        Some(StackValue(
 | 
			
		||||
                            match inner_val.0 {
 | 
			
		||||
@ -306,7 +306,8 @@ impl mir::Expression {
 | 
			
		||||
                Some(match v.0 {
 | 
			
		||||
                    StackValueKind::Immutable(val) => val.clone(),
 | 
			
		||||
                    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(),
 | 
			
		||||
                    },
 | 
			
		||||
                })
 | 
			
		||||
@ -377,7 +378,7 @@ impl mir::Expression {
 | 
			
		||||
                let array = expression.codegen(scope)?;
 | 
			
		||||
                let ptr = scope
 | 
			
		||||
                    .block
 | 
			
		||||
                    .build(Instr::ArrayGEP(array, vec![*idx as u32]))
 | 
			
		||||
                    .build(Instr::GetElemPtr(array, vec![*idx as u32]))
 | 
			
		||||
                    .unwrap();
 | 
			
		||||
 | 
			
		||||
                Some(
 | 
			
		||||
@ -409,7 +410,7 @@ impl mir::Expression {
 | 
			
		||||
                for (i, instr) in instr_list.iter().enumerate() {
 | 
			
		||||
                    let ptr = scope
 | 
			
		||||
                        .block
 | 
			
		||||
                        .build(Instr::ArrayGEP(array, vec![i as u32]))
 | 
			
		||||
                        .build(Instr::GetElemPtr(array, vec![i as u32]))
 | 
			
		||||
                        .unwrap();
 | 
			
		||||
                    scope.block.build(Instr::Store(ptr, *instr)).unwrap();
 | 
			
		||||
                }
 | 
			
		||||
@ -494,9 +495,7 @@ impl TypeKind {
 | 
			
		||||
            TypeKind::U64 => Type::U64,
 | 
			
		||||
            TypeKind::U128 => Type::U128,
 | 
			
		||||
            TypeKind::Bool => Type::Bool,
 | 
			
		||||
            TypeKind::Array(elem_t, len) => {
 | 
			
		||||
                Type::ArrayPtr(Box::new(elem_t.get_type()), *len as u32)
 | 
			
		||||
            }
 | 
			
		||||
            TypeKind::Array(elem_t, _) => Type::Ptr(Box::new(elem_t.get_type())),
 | 
			
		||||
            TypeKind::Void => panic!("Void not a supported type"),
 | 
			
		||||
            TypeKind::Vague(_) => panic!("Tried to compile a vague type!"),
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user