forked from M-Labs/nac3
1
0
Fork 0

core/model: add ListModel

This commit is contained in:
lyken 2024-07-27 02:35:19 +08:00
parent cd777dcb52
commit 86ed0140cb
2 changed files with 43 additions and 0 deletions

View File

@ -0,0 +1,42 @@
use crate::codegen::{model::*, CodeGenContext};
/// Fields of [`List`]
pub struct ListFields<'ctx, T: Model<'ctx>> {
/// Length of the list
pub size: Field<SizeTModel<'ctx>>,
/// Base pointer of the list
pub data: Field<PointerModel<T>>,
}
/// nac3core's `List` definition
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct List<'ctx, T: Model<'ctx>> {
pub sizet: SizeTModel<'ctx>,
pub element: T,
}
impl<'ctx, T: Model<'ctx> + 'ctx> StructKind<'ctx> for List<'ctx, T> {
type Fields = ListFields<'ctx, T>;
fn struct_name(&self) -> &'static str {
"List"
}
fn build_fields(&self, builder: &mut FieldBuilder<'ctx>) -> Self::Fields {
Self::Fields {
size: builder.add_field("size", self.sizet),
data: builder.add_field("data", PointerModel(self.element)),
}
}
}
impl<'ctx, T: Model<'ctx> + 'ctx> Pointer<'ctx, StructModel<List<'ctx, T>>> {
pub fn as_slice(
&self,
ctx: &CodeGenContext<'ctx, '_>,
) -> ArraySlice<'ctx, SizeTModel<'ctx>, T> {
let num_elements = self.gep(ctx, |f| f.size).load(ctx, "num_elements");
let pointer = self.gep(ctx, |f| f.data).load(ctx, "base");
ArraySlice { num_elements, pointer }
}
}

View File

@ -1,3 +1,4 @@
pub mod cslice;
pub mod exception;
pub mod list;
pub mod ndarray;