[core] codegen/classes: Remove Underlying type

This is confusing and we want a better abstraction than this.
This commit is contained in:
David Mak 2024-10-29 13:41:11 +08:00
parent 2822074b2d
commit 26a1b85206
2 changed files with 86 additions and 86 deletions

View File

@ -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,
);

View File

@ -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<Self::Base> {
/// [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<Self::Base> {
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<Self::Base> {
/// 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<Self::Base> {
/// [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<Self::Base> {
/// 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<G: CodeGenerator + ?Sized>(
@ -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<G: CodeGenerator + ?Sized>(
&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: <Self::Value as ProxyValue<'ctx>>::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<ListType<'ctx>> 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<ListValue<'ctx>> 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<G: CodeGenerator + ?Sized>(
@ -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<G: CodeGenerator + ?Sized>(
&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<RangeValue<'ctx>> 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<G: CodeGenerator + ?Sized>(
@ -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<G: CodeGenerator + ?Sized>(
&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: <Self::Value as ProxyValue<'ctx>>::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<NDArrayType<'ctx>> 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<NDArrayValue<'ctx>> for PointerValue<'ctx> {