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 llvm_sys::core::LLVMBuildArrayAlloca;
use crate::{
BlockData, ConstValue, FunctionData, Instr, InstructionData, ModuleData, TerminatorKind, Type,
util::match_types,
@ -277,14 +279,28 @@ impl Builder {
Err(())
}
}
Extract(list, idx) => {
let list_ty = list.get_type(&self)?;
if let Type::Array(_, len) = list_ty {
Extract(arr, idx) => {
let arr_ty = arr.get_type(&self)?;
if let Type::Array(_, len) = arr_ty {
if len < idx { Ok(()) } else { Err(()) }
} else {
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(()),
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::U128(_) => U128,
ConstValue::Bool(_) => Bool,
ConstValue::ConstArray(arr) => Array(
Box::new(arr.iter().map(|a| a.get_type()).next().unwrap_or(Void)),
arr.len() as u32,
),
// ConstValue::Array(arr) => Array(
// Box::new(
// 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,
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 {
@ -433,24 +449,26 @@ impl ConstValue {
ConstValue::U32(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::ConstArray(const_values) => {
let elem_ty = const_values
.iter()
.map(|e| e.get_type())
.next()
.unwrap_or(Type::Void);
// ConstValue::Array(const_values) => {
// let elem_ty = const_values
// .iter()
// .map(|e| e.get_type(builder))
// .next()
// .unwrap_or(Ok(Type::Void))
// .unwrap();
let mut elems = const_values
.iter()
.map(|e| e.as_llvm(context))
.collect::<Vec<_>>();
// let mut elems = const_values
// .iter()
// .map(|e| e.as_llvm(context))
// .collect::<Vec<_>>();
LLVMConstArray(
elem_ty.as_llvm(context),
elems.as_mut_ptr(),
elems.len() as u32,
)
}
// LLVMConstArray(
// elem_ty.as_llvm(context),
// elems.as_mut_ptr(),
// elems.len() as u32,
// )
// }
// }
}
}
}

View File

@ -101,6 +101,10 @@ impl Debug for Instr {
Instr::Load(val, ty) => write!(f, "load<{:?}>({:?})", ty, val),
Instr::Store(ptr, val) => write!(f, "store({:?} = {:?})", ptr, val),
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),
Load(InstructionValue, Type),
Store(InstructionValue, InstructionValue),
ArrayAlloca(Type, u32),
Insert(InstructionValue, u32, InstructionValue),
Extract(InstructionValue, u32),
/// Integer Comparison
@ -216,7 +218,6 @@ pub enum ConstValue {
U64(u64),
U128(u128),
Bool(bool),
ConstArray(Vec<ConstValue>),
}
#[derive(Clone, Hash)]