From 3111afae8f889ca62a4f71b1a0063701b1f931fa Mon Sep 17 00:00:00 2001 From: David Mak Date: Tue, 29 Oct 2024 13:41:11 +0800 Subject: [PATCH] [core] codegen/classes: Remove Underlying type This is confusing and we want a better abstraction than this. --- nac3artiq/src/symbol_resolver.rs | 11 +-- nac3core/src/codegen/classes.rs | 161 ++++++++++++++++--------------- 2 files changed, 86 insertions(+), 86 deletions(-) diff --git a/nac3artiq/src/symbol_resolver.rs b/nac3artiq/src/symbol_resolver.rs index fd8ed0db..11662b49 100644 --- a/nac3artiq/src/symbol_resolver.rs +++ b/nac3artiq/src/symbol_resolver.rs @@ -14,10 +14,7 @@ use pyo3::{ }; use nac3core::{ - codegen::{ - classes::{NDArrayType, ProxyType}, - CodeGenContext, CodeGenerator, - }, + codegen::{classes::NDArrayType, CodeGenContext, CodeGenerator}, inkwell::{ module::Linkage, types::{BasicType, BasicTypeEnum}, @@ -1096,7 +1093,7 @@ impl InnerResolver { if self.global_value_ids.read().contains_key(&id) { let global = ctx.module.get_global(&id_str).unwrap_or_else(|| { ctx.module.add_global( - ndarray_llvm_ty.as_underlying_type(), + ndarray_llvm_ty.element_type().into_struct_type(), Some(AddressSpace::default()), &id_str, ) @@ -1190,7 +1187,7 @@ impl InnerResolver { data_global.set_initializer(&data); // create a global for the ndarray object and initialize it - let value = ndarray_llvm_ty.as_underlying_type().const_named_struct(&[ + let value = ndarray_llvm_ty.element_type().into_struct_type().const_named_struct(&[ llvm_usize.const_int(ndarray_ndims, false).into(), shape_global .as_pointer_value() @@ -1203,7 +1200,7 @@ impl InnerResolver { ]); let ndarray = ctx.module.add_global( - ndarray_llvm_ty.as_underlying_type(), + ndarray_llvm_ty.element_type().into_struct_type(), Some(AddressSpace::default()), &id_str, ); diff --git a/nac3core/src/codegen/classes.rs b/nac3core/src/codegen/classes.rs index 8628aaa7..6cabcc62 100644 --- a/nac3core/src/codegen/classes.rs +++ b/nac3core/src/codegen/classes.rs @@ -1,7 +1,7 @@ use inkwell::{ context::Context, - types::{AnyTypeEnum, ArrayType, BasicType, BasicTypeEnum, IntType, PointerType, StructType}, - values::{ArrayValue, BasicValue, BasicValueEnum, IntValue, PointerValue, StructValue}, + types::{AnyTypeEnum, BasicType, BasicTypeEnum, IntType, PointerType}, + values::{BasicValue, BasicValueEnum, IntValue, PointerValue}, AddressSpace, IntPredicate, }; @@ -18,10 +18,6 @@ pub trait ProxyType<'ctx>: Into { /// [LLVM pointer type][PointerType]. type Base: BasicType<'ctx>; - /// The underlying LLVM type used to represent values. This is usually the element type of - /// [`Base`] if it is a pointer, otherwise this is the same type as `Base`. - type Underlying: BasicType<'ctx>; - /// The type of values represented by this type. type Value: ProxyValue<'ctx>; @@ -40,11 +36,7 @@ pub trait ProxyType<'ctx>: Into { ctx: &mut CodeGenContext<'ctx, '_>, size: IntValue<'ctx>, name: Option<&'ctx str>, - ) -> ArraySliceValue<'ctx> { - generator - .gen_array_var_alloc(ctx, self.as_underlying_type().as_basic_type_enum(), size, name) - .unwrap() - } + ) -> ArraySliceValue<'ctx>; /// Creates a [`value`][ProxyValue] with this as its type. fn create_value( @@ -55,9 +47,6 @@ pub trait ProxyType<'ctx>: Into { /// Returns the [base type][Self::Base] of this proxy. fn as_base_type(&self) -> Self::Base; - - /// Returns the [underlying type][Self::Underlying] of this proxy. - fn as_underlying_type(&self) -> Self::Underlying; } /// A LLVM type that is used to represent a non-primitive value in NAC3. @@ -66,10 +55,6 @@ pub trait ProxyValue<'ctx>: Into { /// [LLVM pointer type][PointerValue]. type Base: BasicValue<'ctx>; - /// The underlying type of LLVM values represented by this instance. This is usually the element - /// type of [`Base`] if it is a pointer, otherwise this is the same type as `Base`. - type Underlying: BasicValue<'ctx>; - /// The type of this value. type Type: ProxyType<'ctx>; @@ -79,13 +64,13 @@ pub trait ProxyValue<'ctx>: Into { /// Returns the [base value][Self::Base] of this proxy. fn as_base_value(&self) -> Self::Base; - /// Loads this value into its [underlying representation][Self::Underlying]. Usually involves a - /// `getelementptr` if [`Self::Base`] is a [pointer value][PointerValue]. - fn as_underlying_value( - &self, - ctx: &mut CodeGenContext<'ctx, '_>, - name: Option<&'ctx str>, - ) -> Self::Underlying; + // /// Loads this value into its [underlying representation][Self::Underlying]. Usually involves a + // /// `getelementptr` if [`Self::Base`] is a [pointer value][PointerValue]. + // fn as_underlying_value( + // &self, + // ctx: &mut CodeGenContext<'ctx, '_>, + // name: Option<&'ctx str>, + // ) -> Self::Underlying; } /// An LLVM value that is array-like, i.e. it contains a contiguous, sequenced collection of @@ -600,7 +585,6 @@ impl<'ctx> ListType<'ctx> { impl<'ctx> ProxyType<'ctx> for ListType<'ctx> { type Base = PointerType<'ctx>; - type Underlying = StructType<'ctx>; type Value = ListValue<'ctx>; fn new_value( @@ -610,11 +594,34 @@ impl<'ctx> ProxyType<'ctx> for ListType<'ctx> { name: Option<&'ctx str>, ) -> Self::Value { self.create_value( - generator.gen_var_alloc(ctx, self.as_underlying_type().into(), name).unwrap(), + generator + .gen_var_alloc( + ctx, + self.as_base_type().get_element_type().into_struct_type().into(), + name, + ) + .unwrap(), name, ) } + fn new_array_value( + &self, + generator: &mut G, + ctx: &mut CodeGenContext<'ctx, '_>, + size: IntValue<'ctx>, + name: Option<&'ctx str>, + ) -> ArraySliceValue<'ctx> { + generator + .gen_array_var_alloc( + ctx, + self.as_base_type().get_element_type().into_struct_type().into(), + size, + name, + ) + .unwrap() + } + fn create_value( &self, value: >::Base, @@ -628,10 +635,6 @@ impl<'ctx> ProxyType<'ctx> for ListType<'ctx> { fn as_base_type(&self) -> Self::Base { self.ty } - - fn as_underlying_type(&self) -> Self::Underlying { - self.as_base_type().get_element_type().into_struct_type() - } } impl<'ctx> From> for PointerType<'ctx> { @@ -770,7 +773,6 @@ impl<'ctx> ListValue<'ctx> { impl<'ctx> ProxyValue<'ctx> for ListValue<'ctx> { type Base = PointerValue<'ctx>; - type Underlying = StructValue<'ctx>; type Type = ListType<'ctx>; fn get_type(&self) -> Self::Type { @@ -780,17 +782,6 @@ impl<'ctx> ProxyValue<'ctx> for ListValue<'ctx> { fn as_base_value(&self) -> Self::Base { self.value } - - fn as_underlying_value( - &self, - ctx: &mut CodeGenContext<'ctx, '_>, - name: Option<&'ctx str>, - ) -> Self::Underlying { - ctx.builder - .build_load(self.as_base_value(), name.unwrap_or_default()) - .map(BasicValueEnum::into_struct_value) - .unwrap() - } } impl<'ctx> From> for PointerValue<'ctx> { @@ -940,7 +931,6 @@ impl<'ctx> RangeType<'ctx> { impl<'ctx> ProxyType<'ctx> for RangeType<'ctx> { type Base = PointerType<'ctx>; - type Underlying = ArrayType<'ctx>; type Value = RangeValue<'ctx>; fn new_value( @@ -950,7 +940,13 @@ impl<'ctx> ProxyType<'ctx> for RangeType<'ctx> { name: Option<&'ctx str>, ) -> Self::Value { self.create_value( - generator.gen_var_alloc(ctx, self.as_underlying_type().into(), name).unwrap(), + generator + .gen_var_alloc( + ctx, + self.as_base_type().get_element_type().into_struct_type().into(), + name, + ) + .unwrap(), name, ) } @@ -965,12 +961,25 @@ impl<'ctx> ProxyType<'ctx> for RangeType<'ctx> { RangeValue { value, name } } - fn as_base_type(&self) -> Self::Base { - self.ty + fn new_array_value( + &self, + generator: &mut G, + ctx: &mut CodeGenContext<'ctx, '_>, + size: IntValue<'ctx>, + name: Option<&'ctx str>, + ) -> ArraySliceValue<'ctx> { + generator + .gen_array_var_alloc( + ctx, + self.as_base_type().get_element_type().into_struct_type().into(), + size, + name, + ) + .unwrap() } - fn as_underlying_type(&self) -> Self::Underlying { - self.as_base_type().get_element_type().into_array_type() + fn as_base_type(&self) -> Self::Base { + self.ty } } @@ -1112,7 +1121,6 @@ impl<'ctx> RangeValue<'ctx> { impl<'ctx> ProxyValue<'ctx> for RangeValue<'ctx> { type Base = PointerValue<'ctx>; - type Underlying = ArrayValue<'ctx>; type Type = RangeType<'ctx>; fn get_type(&self) -> Self::Type { @@ -1122,17 +1130,6 @@ impl<'ctx> ProxyValue<'ctx> for RangeValue<'ctx> { fn as_base_value(&self) -> Self::Base { self.value } - - fn as_underlying_value( - &self, - ctx: &mut CodeGenContext<'ctx, '_>, - name: Option<&'ctx str>, - ) -> Self::Underlying { - ctx.builder - .build_load(self.as_base_value(), name.unwrap_or_default()) - .map(BasicValueEnum::into_array_value) - .unwrap() - } } impl<'ctx> From> for PointerValue<'ctx> { @@ -1262,7 +1259,6 @@ impl<'ctx> NDArrayType<'ctx> { impl<'ctx> ProxyType<'ctx> for NDArrayType<'ctx> { type Base = PointerType<'ctx>; - type Underlying = StructType<'ctx>; type Value = NDArrayValue<'ctx>; fn new_value( @@ -1272,11 +1268,34 @@ impl<'ctx> ProxyType<'ctx> for NDArrayType<'ctx> { name: Option<&'ctx str>, ) -> Self::Value { self.create_value( - generator.gen_var_alloc(ctx, self.as_underlying_type().into(), name).unwrap(), + generator + .gen_var_alloc( + ctx, + self.as_base_type().get_element_type().into_struct_type().into(), + name, + ) + .unwrap(), name, ) } + fn new_array_value( + &self, + generator: &mut G, + ctx: &mut CodeGenContext<'ctx, '_>, + size: IntValue<'ctx>, + name: Option<&'ctx str>, + ) -> ArraySliceValue<'ctx> { + generator + .gen_array_var_alloc( + ctx, + self.as_base_type().get_element_type().into_struct_type().into(), + size, + name, + ) + .unwrap() + } + fn create_value( &self, value: >::Base, @@ -1290,10 +1309,6 @@ impl<'ctx> ProxyType<'ctx> for NDArrayType<'ctx> { fn as_base_type(&self) -> Self::Base { self.ty } - - fn as_underlying_type(&self) -> Self::Underlying { - self.as_base_type().get_element_type().into_struct_type() - } } impl<'ctx> From> for PointerType<'ctx> { @@ -1445,7 +1460,6 @@ impl<'ctx> NDArrayValue<'ctx> { impl<'ctx> ProxyValue<'ctx> for NDArrayValue<'ctx> { type Base = PointerValue<'ctx>; - type Underlying = StructValue<'ctx>; type Type = NDArrayType<'ctx>; fn get_type(&self) -> Self::Type { @@ -1455,17 +1469,6 @@ impl<'ctx> ProxyValue<'ctx> for NDArrayValue<'ctx> { fn as_base_value(&self) -> Self::Base { self.value } - - fn as_underlying_value( - &self, - ctx: &mut CodeGenContext<'ctx, '_>, - name: Option<&'ctx str>, - ) -> Self::Underlying { - ctx.builder - .build_load(self.as_base_value(), name.unwrap_or_default()) - .map(BasicValueEnum::into_struct_value) - .unwrap() - } } impl<'ctx> From> for PointerValue<'ctx> {