From a2937e174226cbca56add87c37bbb0c1c4753882 Mon Sep 17 00:00:00 2001 From: lyken Date: Wed, 28 Aug 2024 12:18:23 +0800 Subject: [PATCH] get_type* -> llvm_type* --- nac3core/src/codegen/model/any.rs | 2 +- nac3core/src/codegen/model/array.rs | 4 ++-- nac3core/src/codegen/model/core.rs | 27 +++++++++++++++---------- nac3core/src/codegen/model/float.rs | 2 +- nac3core/src/codegen/model/function.rs | 4 ++-- nac3core/src/codegen/model/int.rs | 20 +++++++++--------- nac3core/src/codegen/model/ptr.rs | 8 ++++---- nac3core/src/codegen/model/structure.rs | 5 +++-- 8 files changed, 39 insertions(+), 33 deletions(-) diff --git a/nac3core/src/codegen/model/any.rs b/nac3core/src/codegen/model/any.rs index b74ba324..f38da4e0 100644 --- a/nac3core/src/codegen/model/any.rs +++ b/nac3core/src/codegen/model/any.rs @@ -13,7 +13,7 @@ use super::*; pub struct Any<'ctx>(pub BasicTypeEnum<'ctx>); impl<'ctx> ModelBase<'ctx> for Any<'ctx> { - fn get_type_impl(&self, _size_t: IntType<'ctx>, _ctx: &'ctx Context) -> BasicTypeEnum<'ctx> { + fn llvm_type_impl(&self, _size_t: IntType<'ctx>, _ctx: &'ctx Context) -> BasicTypeEnum<'ctx> { self.0.as_basic_type_enum() } diff --git a/nac3core/src/codegen/model/array.rs b/nac3core/src/codegen/model/array.rs index be52cefe..2f0d6517 100644 --- a/nac3core/src/codegen/model/array.rs +++ b/nac3core/src/codegen/model/array.rs @@ -47,8 +47,8 @@ pub struct Array { } impl<'ctx, Len: ArrayLen, Item: ModelBase<'ctx>> ModelBase<'ctx> for Array { - fn get_type_impl(&self, size_t: IntType<'ctx>, ctx: &'ctx Context) -> BasicTypeEnum<'ctx> { - let item = self.item.get_type_impl(size_t, ctx); + fn llvm_type_impl(&self, size_t: IntType<'ctx>, ctx: &'ctx Context) -> BasicTypeEnum<'ctx> { + let item = self.item.llvm_type_impl(size_t, ctx); item.array_type(self.len.length()).into() } diff --git a/nac3core/src/codegen/model/core.rs b/nac3core/src/codegen/model/core.rs index a5c9fd97..7c334616 100644 --- a/nac3core/src/codegen/model/core.rs +++ b/nac3core/src/codegen/model/core.rs @@ -24,8 +24,8 @@ pub trait ModelBase<'ctx> { // NOTE: Taking `size_t` here instead of `CodeGenerator` to be object safe. // In fact, all the entire model abstraction need from the `CodeGenerator` is its `get_size_type()`. - // NOTE: Model's get_type but object-safe and returns BasicTypeEnum, instead of a known BasicType variant. - fn get_type_impl(&self, size_t: IntType<'ctx>, ctx: &'ctx Context) -> BasicTypeEnum<'ctx>; + // NOTE: Model's llvm_type but object-safe and returns BasicTypeEnum, instead of a known BasicType variant. + fn llvm_type_impl(&self, size_t: IntType<'ctx>, ctx: &'ctx Context) -> BasicTypeEnum<'ctx>; // NOTE: Model's check_type but object-safe. fn check_type_impl( @@ -45,7 +45,7 @@ pub trait ModelBase<'ctx> { /// - [`Int`] identifies an [`IntType`] with bit-width of whatever is set in the [`AnyInt`] object. /// - [`Any`] identifies a [`BasicType`] set in the [`Any`] object itself. /// -/// You can get the [`BasicType`] out of a model with [`Model::get_type`]. +/// You can get the [`BasicType`] out of a model with [`Model::llvm_type`]. /// /// Furthermore, [`Instance<'ctx, M>`] is a simple structure that carries a [`BasicValue`] with [`BasicType`] identified by model `M`. /// @@ -85,10 +85,14 @@ pub trait Model<'ctx>: fmt::Debug + Clone + Copy + ModelBase<'ctx> { /// Return the [`BasicType`] of this model. #[must_use] - fn get_type(&self, generator: &G, ctx: &'ctx Context) -> Self::Type { + fn llvm_type( + &self, + generator: &G, + ctx: &'ctx Context, + ) -> Self::Type { let size_t = generator.get_size_type(ctx); - let ty = self.get_type_impl(size_t, ctx); + let ty = self.llvm_type_impl(size_t, ctx); match Self::Type::try_from(ty) { Ok(ty) => ty, _ => panic!("Model::Type is inconsistent with what is returned from ModelBase::get_type_impl()! Got {ty:?}."), @@ -101,7 +105,7 @@ pub trait Model<'ctx>: fmt::Debug + Clone + Copy + ModelBase<'ctx> { generator: &mut G, ctx: &'ctx Context, ) -> IntValue<'ctx> { - self.get_type(generator, ctx).size_of().unwrap() + self.llvm_type(generator, ctx).size_of().unwrap() } /// Check if a [`BasicType`] matches the [`BasicType`] of this model. @@ -149,7 +153,7 @@ pub trait Model<'ctx>: fmt::Debug + Clone + Copy + ModelBase<'ctx> { generator: &mut G, ctx: &CodeGenContext<'ctx, '_>, ) -> Instance<'ctx, Ptr> { - let p = ctx.builder.build_alloca(self.get_type(generator, ctx.ctx), "").unwrap(); + let p = ctx.builder.build_alloca(self.llvm_type(generator, ctx.ctx), "").unwrap(); unsafe { Ptr(*self).believe_value(p) } } @@ -160,7 +164,8 @@ pub trait Model<'ctx>: fmt::Debug + Clone + Copy + ModelBase<'ctx> { ctx: &CodeGenContext<'ctx, '_>, len: IntValue<'ctx>, ) -> Instance<'ctx, Ptr> { - let p = ctx.builder.build_array_alloca(self.get_type(generator, ctx.ctx), len, "").unwrap(); + let p = + ctx.builder.build_array_alloca(self.llvm_type(generator, ctx.ctx), len, "").unwrap(); unsafe { Ptr(*self).believe_value(p) } } @@ -170,7 +175,7 @@ pub trait Model<'ctx>: fmt::Debug + Clone + Copy + ModelBase<'ctx> { ctx: &mut CodeGenContext<'ctx, '_>, name: Option<&str>, ) -> Result>, String> { - let ty = self.get_type(generator, ctx.ctx).as_basic_type_enum(); + let ty = self.llvm_type(generator, ctx.ctx).as_basic_type_enum(); let p = generator.gen_var_alloc(ctx, ty, name)?; Ok(unsafe { Ptr(*self).believe_value(p) }) } @@ -183,7 +188,7 @@ pub trait Model<'ctx>: fmt::Debug + Clone + Copy + ModelBase<'ctx> { name: Option<&'ctx str>, ) -> Result>, String> { // TODO: Remove ArraySliceValue - let ty = self.get_type(generator, ctx.ctx).as_basic_type_enum(); + let ty = self.llvm_type(generator, ctx.ctx).as_basic_type_enum(); let p = generator.gen_array_var_alloc(ctx, ty, len, name)?; Ok(unsafe { Ptr(*self).believe_value(PointerValue::from(p)) }) } @@ -206,7 +211,7 @@ pub trait Model<'ctx>: fmt::Debug + Clone + Copy + ModelBase<'ctx> { }; } - let value = match self.get_type(generator, ctx).as_basic_type_enum() { + let value = match self.llvm_type(generator, ctx).as_basic_type_enum() { BasicTypeEnum::ArrayType(t) => make!(t, BasicValueEnum::into_array_value), BasicTypeEnum::IntType(t) => make!(t, BasicValueEnum::into_int_value), BasicTypeEnum::FloatType(t) => make!(t, BasicValueEnum::into_float_value), diff --git a/nac3core/src/codegen/model/float.rs b/nac3core/src/codegen/model/float.rs index ef5111a8..8c6d1e3e 100644 --- a/nac3core/src/codegen/model/float.rs +++ b/nac3core/src/codegen/model/float.rs @@ -42,7 +42,7 @@ impl<'ctx> FloatKind<'ctx> for AnyFloat<'ctx> { pub struct Float(pub N); impl<'ctx, N: FloatKind<'ctx>> ModelBase<'ctx> for Float { - fn get_type_impl(&self, _size_t: IntType<'ctx>, ctx: &'ctx Context) -> BasicTypeEnum<'ctx> { + fn llvm_type_impl(&self, _size_t: IntType<'ctx>, ctx: &'ctx Context) -> BasicTypeEnum<'ctx> { self.0.get_float_type(ctx).into() } diff --git a/nac3core/src/codegen/model/function.rs b/nac3core/src/codegen/model/function.rs index 7ff2d746..0b9d4d37 100644 --- a/nac3core/src/codegen/model/function.rs +++ b/nac3core/src/codegen/model/function.rs @@ -63,7 +63,7 @@ impl<'ctx, 'a, 'b, 'c, 'd, G: CodeGenerator + ?Sized> CallFunction<'ctx, 'a, 'b, #[must_use] pub fn arg>(mut self, arg: Instance<'ctx, M>) -> Self { let arg = Arg { - ty: arg.model.get_type(self.generator, self.ctx.ctx).as_basic_type_enum().into(), + ty: arg.model.llvm_type(self.generator, self.ctx.ctx).as_basic_type_enum().into(), val: arg.value.as_basic_value_enum().into(), }; self.args.push(arg); @@ -73,7 +73,7 @@ impl<'ctx, 'a, 'b, 'c, 'd, G: CodeGenerator + ?Sized> CallFunction<'ctx, 'a, 'b, /// Call the function and expect the function to return a value of type of `return_model`. #[must_use] pub fn returning>(self, name: &str, return_model: M) -> Instance<'ctx, M> { - let ret_ty = return_model.get_type(self.generator, self.ctx.ctx); + let ret_ty = return_model.llvm_type(self.generator, self.ctx.ctx); let ret = self.call(|tys| ret_ty.fn_type(tys, false), name); let ret = BasicValueEnum::try_from(ret.as_any_value_enum()).unwrap(); // Must work diff --git a/nac3core/src/codegen/model/int.rs b/nac3core/src/codegen/model/int.rs index a1e49aa6..b6facd73 100644 --- a/nac3core/src/codegen/model/int.rs +++ b/nac3core/src/codegen/model/int.rs @@ -69,7 +69,7 @@ impl<'ctx> IntKind<'ctx> for AnyInt<'ctx> { pub struct Int(pub N); impl<'ctx, N: IntKind<'ctx>> ModelBase<'ctx> for Int { - fn get_type_impl(&self, size_t: IntType<'ctx>, ctx: &'ctx Context) -> BasicTypeEnum<'ctx> { + fn llvm_type_impl(&self, size_t: IntType<'ctx>, ctx: &'ctx Context) -> BasicTypeEnum<'ctx> { self.0.get_int_type(size_t, ctx).into() } @@ -108,7 +108,7 @@ impl<'ctx, N: IntKind<'ctx>> Int { ctx: &'ctx Context, value: u64, ) -> Instance<'ctx, Self> { - let value = self.get_type(generator, ctx).const_int(value, false); + let value = self.llvm_type(generator, ctx).const_int(value, false); unsafe { self.believe_value(value) } } @@ -117,7 +117,7 @@ impl<'ctx, N: IntKind<'ctx>> Int { generator: &mut G, ctx: &'ctx Context, ) -> Instance<'ctx, Self> { - let value = self.get_type(generator, ctx).const_zero(); + let value = self.llvm_type(generator, ctx).const_zero(); unsafe { self.believe_value(value) } } @@ -134,7 +134,7 @@ impl<'ctx, N: IntKind<'ctx>> Int { generator: &mut G, ctx: &'ctx Context, ) -> Instance<'ctx, Self> { - let value = self.get_type(generator, ctx).const_all_ones(); + let value = self.llvm_type(generator, ctx).const_all_ones(); unsafe { self.believe_value(value) } } @@ -150,7 +150,7 @@ impl<'ctx, N: IntKind<'ctx>> Int { ); let value = ctx .builder - .build_int_s_extend_or_bit_cast(value, self.get_type(generator, ctx.ctx), "") + .build_int_s_extend_or_bit_cast(value, self.llvm_type(generator, ctx.ctx), "") .unwrap(); unsafe { self.believe_value(value) } } @@ -166,7 +166,7 @@ impl<'ctx, N: IntKind<'ctx>> Int { < self.0.get_int_type(generator.get_size_type(ctx.ctx), ctx.ctx).get_bit_width() ); let value = - ctx.builder.build_int_s_extend(value, self.get_type(generator, ctx.ctx), "").unwrap(); + ctx.builder.build_int_s_extend(value, self.llvm_type(generator, ctx.ctx), "").unwrap(); unsafe { self.believe_value(value) } } @@ -182,7 +182,7 @@ impl<'ctx, N: IntKind<'ctx>> Int { ); let value = ctx .builder - .build_int_z_extend_or_bit_cast(value, self.get_type(generator, ctx.ctx), "") + .build_int_z_extend_or_bit_cast(value, self.llvm_type(generator, ctx.ctx), "") .unwrap(); unsafe { self.believe_value(value) } } @@ -198,7 +198,7 @@ impl<'ctx, N: IntKind<'ctx>> Int { < self.0.get_int_type(generator.get_size_type(ctx.ctx), ctx.ctx).get_bit_width() ); let value = - ctx.builder.build_int_z_extend(value, self.get_type(generator, ctx.ctx), "").unwrap(); + ctx.builder.build_int_z_extend(value, self.llvm_type(generator, ctx.ctx), "").unwrap(); unsafe { self.believe_value(value) } } @@ -214,7 +214,7 @@ impl<'ctx, N: IntKind<'ctx>> Int { ); let value = ctx .builder - .build_int_truncate_or_bit_cast(value, self.get_type(generator, ctx.ctx), "") + .build_int_truncate_or_bit_cast(value, self.llvm_type(generator, ctx.ctx), "") .unwrap(); unsafe { self.believe_value(value) } } @@ -230,7 +230,7 @@ impl<'ctx, N: IntKind<'ctx>> Int { > self.0.get_int_type(generator.get_size_type(ctx.ctx), ctx.ctx).get_bit_width() ); let value = - ctx.builder.build_int_truncate(value, self.get_type(generator, ctx.ctx), "").unwrap(); + ctx.builder.build_int_truncate(value, self.llvm_type(generator, ctx.ctx), "").unwrap(); unsafe { self.believe_value(value) } } diff --git a/nac3core/src/codegen/model/ptr.rs b/nac3core/src/codegen/model/ptr.rs index 5e76c76d..80e699a1 100644 --- a/nac3core/src/codegen/model/ptr.rs +++ b/nac3core/src/codegen/model/ptr.rs @@ -28,9 +28,9 @@ pub type OpaquePtr = Ptr<()>; // // See https://thedan64.github.io/inkwell/inkwell/builder/struct.Builder.html#method.build_load. impl<'ctx, Item: ModelBase<'ctx>> ModelBase<'ctx> for Ptr { - fn get_type_impl(&self, size_t: IntType<'ctx>, ctx: &'ctx Context) -> BasicTypeEnum<'ctx> { + fn llvm_type_impl(&self, size_t: IntType<'ctx>, ctx: &'ctx Context) -> BasicTypeEnum<'ctx> { // TODO: LLVM 15: ctx.ptr_type(AddressSpace::default()) - let item = self.0.get_type_impl(size_t, ctx); + let item = self.0.llvm_type_impl(size_t, ctx); item.ptr_type(AddressSpace::default()).into() } @@ -74,7 +74,7 @@ impl<'ctx, Item: Model<'ctx>> Ptr { ctx: &'ctx Context, ) -> Instance<'ctx, Ptr> { // TODO: LLVM 15: Write in an impl where `Item` does not have to be `Model<'ctx>`. - let ptr = self.get_type(generator, ctx).const_null(); + let ptr = self.llvm_type(generator, ctx).const_null(); unsafe { self.believe_value(ptr) } } @@ -90,7 +90,7 @@ impl<'ctx, Item: Model<'ctx>> Ptr { // ``` // return self.believe_value(ptr); // ``` - let t = self.get_type(generator, ctx.ctx); + let t = self.llvm_type(generator, ctx.ctx); let ptr = ctx.builder.build_pointer_cast(ptr, t, "").unwrap(); unsafe { self.believe_value(ptr) } } diff --git a/nac3core/src/codegen/model/structure.rs b/nac3core/src/codegen/model/structure.rs index 145657d1..8b17aee3 100644 --- a/nac3core/src/codegen/model/structure.rs +++ b/nac3core/src/codegen/model/structure.rs @@ -72,7 +72,8 @@ pub trait StructKind<'ctx>: fmt::Debug + Clone + Copy { /// Get the LLVM [`StructType`] of this [`StructKind`]. fn get_struct_type(&self, size_t: IntType<'ctx>, ctx: &'ctx Context) -> StructType<'ctx> { let entries = self.entries(); - let entries = entries.into_iter().map(|t| t.model.get_type_impl(size_t, ctx)).collect_vec(); + let entries = + entries.into_iter().map(|t| t.model.llvm_type_impl(size_t, ctx)).collect_vec(); ctx.struct_type(&entries, false) } } @@ -93,7 +94,7 @@ impl<'ctx, S: StructKind<'ctx>> Struct { } impl<'ctx, S: StructKind<'ctx>> ModelBase<'ctx> for Struct { - fn get_type_impl(&self, size_t: IntType<'ctx>, ctx: &'ctx Context) -> BasicTypeEnum<'ctx> { + fn llvm_type_impl(&self, size_t: IntType<'ctx>, ctx: &'ctx Context) -> BasicTypeEnum<'ctx> { self.0.get_struct_type(size_t, ctx).as_basic_type_enum() }