get_type* -> llvm_type*

This commit is contained in:
lyken 2024-08-28 12:18:23 +08:00
parent 40aa27c42c
commit a2937e1742
No known key found for this signature in database
GPG Key ID: 3BD5FC6AC8325DD8
8 changed files with 39 additions and 33 deletions

View File

@ -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()
}

View File

@ -47,8 +47,8 @@ pub struct Array<Len, Item> {
}
impl<'ctx, Len: ArrayLen, Item: ModelBase<'ctx>> ModelBase<'ctx> for Array<Len, Item> {
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()
}

View File

@ -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<AnyInt>`] 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<G: CodeGenerator + ?Sized>(&self, generator: &G, ctx: &'ctx Context) -> Self::Type {
fn llvm_type<G: CodeGenerator + ?Sized>(
&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<Self>> {
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<Self>> {
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<Instance<'ctx, Ptr<Self>>, 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<Instance<'ctx, Ptr<Self>>, 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),

View File

@ -42,7 +42,7 @@ impl<'ctx> FloatKind<'ctx> for AnyFloat<'ctx> {
pub struct Float<N>(pub N);
impl<'ctx, N: FloatKind<'ctx>> ModelBase<'ctx> for Float<N> {
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()
}

View File

@ -63,7 +63,7 @@ impl<'ctx, 'a, 'b, 'c, 'd, G: CodeGenerator + ?Sized> CallFunction<'ctx, 'a, 'b,
#[must_use]
pub fn arg<M: Model<'ctx>>(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<M: Model<'ctx>>(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

View File

@ -69,7 +69,7 @@ impl<'ctx> IntKind<'ctx> for AnyInt<'ctx> {
pub struct Int<N>(pub N);
impl<'ctx, N: IntKind<'ctx>> ModelBase<'ctx> for Int<N> {
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<N> {
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<N> {
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<N> {
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<N> {
);
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<N> {
< 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<N> {
);
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<N> {
< 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<N> {
);
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<N> {
> 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) }
}

View File

@ -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<Item> {
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<Item> {
ctx: &'ctx Context,
) -> Instance<'ctx, Ptr<Item>> {
// 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<Item> {
// ```
// 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) }
}

View File

@ -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<S> {
}
impl<'ctx, S: StructKind<'ctx>> ModelBase<'ctx> for Struct<S> {
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()
}