Actually finish array-support for lib

This commit is contained in:
Sofia 2025-07-13 20:56:24 +03:00
parent a2e52e0bd2
commit d62d6e2845
4 changed files with 72 additions and 24 deletions

View File

@ -3,6 +3,8 @@
use std::{cell::RefCell, rc::Rc}; use std::{cell::RefCell, rc::Rc};
use llvm_sys::core::LLVMBuildArrayAlloca;
use crate::{ use crate::{
BlockData, ConstValue, FunctionData, Instr, InstructionData, ModuleData, TerminatorKind, Type, BlockData, ConstValue, FunctionData, Instr, InstructionData, ModuleData, TerminatorKind, Type,
util::match_types, util::match_types,
@ -277,14 +279,28 @@ impl Builder {
Err(()) Err(())
} }
} }
Extract(list, idx) => { Extract(arr, idx) => {
let list_ty = list.get_type(&self)?; let arr_ty = arr.get_type(&self)?;
if let Type::Array(_, len) = list_ty { if let Type::Array(_, len) = arr_ty {
if len < idx { Ok(()) } else { Err(()) } if len < idx { Ok(()) } else { Err(()) }
} else { } else {
Err(()) Err(())
} }
} }
ArrayAlloca(_, _) => Ok(()),
Insert(arr, idx, val) => {
let arr_ty = arr.get_type(&self)?;
let val_ty = val.get_type(&self)?;
if let Type::Array(elem_ty, len) = arr_ty {
if val_ty == *elem_ty && len < idx {
Ok(())
} else {
Err(())
}
} else {
Err(())
}
}
} }
} }
} }
@ -351,6 +367,10 @@ impl InstructionValue {
Ok(_) => Err(()), Ok(_) => Err(()),
Err(_) => Err(()), Err(_) => Err(()),
}, },
ArrayAlloca(ty, len_value) => {
Ok(Type::Array(Box::new(ty.clone()), len_value.clone()))
}
Insert(_, _, val) => val.get_type(builder),
} }
} }
} }
@ -371,10 +391,15 @@ impl ConstValue {
ConstValue::U64(_) => U64, ConstValue::U64(_) => U64,
ConstValue::U128(_) => U128, ConstValue::U128(_) => U128,
ConstValue::Bool(_) => Bool, ConstValue::Bool(_) => Bool,
ConstValue::ConstArray(arr) => Array( // ConstValue::Array(arr) => Array(
Box::new(arr.iter().map(|a| a.get_type()).next().unwrap_or(Void)), // Box::new(
arr.len() as u32, // arr.iter()
), // .map(|a| a.get_type(builder).unwrap())
// .next()
// .unwrap_or(Void),
// ),
// arr.len() as u32,
// ),
} }
} }
} }

View File

@ -355,6 +355,22 @@ impl InstructionHolder {
*idx as u32, *idx as u32,
c"extract".as_ptr(), c"extract".as_ptr(),
), ),
ArrayAlloca(ty, len) => {
let array_len = ConstValue::U16(*len as u16).as_llvm(module.context_ref);
LLVMBuildArrayAlloca(
module.builder_ref,
ty.as_llvm(module.context_ref),
array_len,
c"array_alloca".as_ptr(),
)
}
Insert(arr, idx, val) => LLVMBuildInsertValue(
module.builder_ref,
module.values.get(arr).unwrap().value_ref,
module.values.get(val).unwrap().value_ref,
*idx,
c"insert".as_ptr(),
),
} }
}; };
LLVMValue { LLVMValue {
@ -433,24 +449,26 @@ impl ConstValue {
ConstValue::U32(val) => LLVMConstInt(t, *val as u64, 1), ConstValue::U32(val) => LLVMConstInt(t, *val as u64, 1),
ConstValue::U64(val) => LLVMConstInt(t, *val as u64, 1), ConstValue::U64(val) => LLVMConstInt(t, *val as u64, 1),
ConstValue::U128(val) => LLVMConstInt(t, *val as u64, 1), ConstValue::U128(val) => LLVMConstInt(t, *val as u64, 1),
ConstValue::ConstArray(const_values) => { // ConstValue::Array(const_values) => {
let elem_ty = const_values // let elem_ty = const_values
.iter() // .iter()
.map(|e| e.get_type()) // .map(|e| e.get_type(builder))
.next() // .next()
.unwrap_or(Type::Void); // .unwrap_or(Ok(Type::Void))
// .unwrap();
let mut elems = const_values // let mut elems = const_values
.iter() // .iter()
.map(|e| e.as_llvm(context)) // .map(|e| e.as_llvm(context))
.collect::<Vec<_>>(); // .collect::<Vec<_>>();
LLVMConstArray( // LLVMConstArray(
elem_ty.as_llvm(context), // elem_ty.as_llvm(context),
elems.as_mut_ptr(), // elems.as_mut_ptr(),
elems.len() as u32, // elems.len() as u32,
) // )
} // }
// }
} }
} }
} }

View File

@ -101,6 +101,10 @@ impl Debug for Instr {
Instr::Load(val, ty) => write!(f, "load<{:?}>({:?})", ty, val), Instr::Load(val, ty) => write!(f, "load<{:?}>({:?})", ty, val),
Instr::Store(ptr, val) => write!(f, "store({:?} = {:?})", ptr, val), Instr::Store(ptr, val) => write!(f, "store({:?} = {:?})", ptr, val),
Instr::Extract(instruction_value, idx) => fmt_index(f, instruction_value, idx), Instr::Extract(instruction_value, idx) => fmt_index(f, instruction_value, idx),
Instr::ArrayAlloca(ty, instruction_value) => {
write!(f, "array_alloca<{:?}>({:?})", ty, instruction_value)
}
Instr::Insert(arr, idx, val) => write!(f, "{:?}[{}] = {:?}", arr, idx, val),
} }
} }
} }

View File

@ -177,6 +177,8 @@ pub enum Instr {
Alloca(String, Type), Alloca(String, Type),
Load(InstructionValue, Type), Load(InstructionValue, Type),
Store(InstructionValue, InstructionValue), Store(InstructionValue, InstructionValue),
ArrayAlloca(Type, u32),
Insert(InstructionValue, u32, InstructionValue),
Extract(InstructionValue, u32), Extract(InstructionValue, u32),
/// Integer Comparison /// Integer Comparison
@ -216,7 +218,6 @@ pub enum ConstValue {
U64(u64), U64(u64),
U128(u128), U128(u128),
Bool(bool), Bool(bool),
ConstArray(Vec<ConstValue>),
} }
#[derive(Clone, Hash)] #[derive(Clone, Hash)]