From b469de51bcf5fa7624c974ac94b8d652b8444aaa Mon Sep 17 00:00:00 2001 From: David Mak Date: Fri, 8 Nov 2024 15:35:45 +0800 Subject: [PATCH] [core] codegen: Add Self::llvm_type to all type abstractions --- nac3core/src/codegen/types/list.rs | 20 +++++++++----- nac3core/src/codegen/types/ndarray.rs | 38 ++++++++++++++++----------- nac3core/src/codegen/types/range.rs | 11 ++++++-- 3 files changed, 45 insertions(+), 24 deletions(-) diff --git a/nac3core/src/codegen/types/list.rs b/nac3core/src/codegen/types/list.rs index 4561b483..7b082365 100644 --- a/nac3core/src/codegen/types/list.rs +++ b/nac3core/src/codegen/types/list.rs @@ -55,6 +55,19 @@ impl<'ctx> ListType<'ctx> { Ok(()) } + /// Creates an LLVM type corresponding to the expected structure of a `List`. + #[must_use] + fn llvm_type( + ctx: &'ctx Context, + element_type: BasicTypeEnum<'ctx>, + llvm_usize: IntType<'ctx>, + ) -> PointerType<'ctx> { + // struct List { data: T*, size: size_t } + let field_tys = [element_type.ptr_type(AddressSpace::default()).into(), llvm_usize.into()]; + + ctx.struct_type(&field_tys, false).ptr_type(AddressSpace::default()) + } + /// Creates an instance of [`ListType`]. #[must_use] pub fn new( @@ -63,12 +76,7 @@ impl<'ctx> ListType<'ctx> { element_type: BasicTypeEnum<'ctx>, ) -> Self { let llvm_usize = generator.get_size_type(ctx); - let llvm_list = ctx - .struct_type( - &[element_type.ptr_type(AddressSpace::default()).into(), llvm_usize.into()], - false, - ) - .ptr_type(AddressSpace::default()); + let llvm_list = Self::llvm_type(ctx, element_type, llvm_usize); ListType::from_type(llvm_list, llvm_usize) } diff --git a/nac3core/src/codegen/types/ndarray.rs b/nac3core/src/codegen/types/ndarray.rs index ca463b6a..98bcdb62 100644 --- a/nac3core/src/codegen/types/ndarray.rs +++ b/nac3core/src/codegen/types/ndarray.rs @@ -73,6 +73,27 @@ impl<'ctx> NDArrayType<'ctx> { Ok(()) } + /// Creates an LLVM type corresponding to the expected structure of an `NDArray`. + #[must_use] + fn llvm_type( + ctx: &'ctx Context, + dtype: BasicTypeEnum<'ctx>, + llvm_usize: IntType<'ctx>, + ) -> PointerType<'ctx> { + // struct NDArray { num_dims: size_t, dims: size_t*, data: T* } + // + // * num_dims: Number of dimensions in the array + // * dims: Pointer to an array containing the size of each dimension + // * data: Pointer to an array containing the array data + let field_tys = [ + llvm_usize.into(), + llvm_usize.ptr_type(AddressSpace::default()).into(), + dtype.ptr_type(AddressSpace::default()).into(), + ]; + + ctx.struct_type(&field_tys, false).ptr_type(AddressSpace::default()) + } + /// Creates an instance of [`ListType`]. #[must_use] pub fn new( @@ -81,22 +102,7 @@ impl<'ctx> NDArrayType<'ctx> { dtype: BasicTypeEnum<'ctx>, ) -> Self { let llvm_usize = generator.get_size_type(ctx); - - // struct NDArray { num_dims: size_t, dims: size_t*, data: T* } - // - // * num_dims: Number of dimensions in the array - // * dims: Pointer to an array containing the size of each dimension - // * data: Pointer to an array containing the array data - let llvm_ndarray = ctx - .struct_type( - &[ - llvm_usize.into(), - llvm_usize.ptr_type(AddressSpace::default()).into(), - dtype.ptr_type(AddressSpace::default()).into(), - ], - false, - ) - .ptr_type(AddressSpace::default()); + let llvm_ndarray = Self::llvm_type(ctx, dtype, llvm_usize); NDArrayType::from_type(llvm_ndarray, llvm_usize) } diff --git a/nac3core/src/codegen/types/range.rs b/nac3core/src/codegen/types/range.rs index 89a1b723..49c43882 100644 --- a/nac3core/src/codegen/types/range.rs +++ b/nac3core/src/codegen/types/range.rs @@ -47,11 +47,18 @@ impl<'ctx> RangeType<'ctx> { Ok(()) } + /// Creates an LLVM type corresponding to the expected structure of a `Range`. + #[must_use] + fn llvm_type(ctx: &'ctx Context) -> PointerType<'ctx> { + // typedef int32_t Range[3]; + let llvm_i32 = ctx.i32_type(); + llvm_i32.array_type(3).ptr_type(AddressSpace::default()) + } + /// Creates an instance of [`RangeType`]. #[must_use] pub fn new(ctx: &'ctx Context) -> Self { - let llvm_i32 = ctx.i32_type(); - let llvm_range = llvm_i32.array_type(3).ptr_type(AddressSpace::default()); + let llvm_range = Self::llvm_type(ctx); RangeType::from_type(llvm_range) }