From d12fb99ded5503d2224419a6e59de0c6c4c4d28a Mon Sep 17 00:00:00 2001 From: lyken Date: Wed, 28 Aug 2024 12:04:10 +0800 Subject: [PATCH] unsafe believe_value --- nac3core/src/codegen/model/array.rs | 2 +- nac3core/src/codegen/model/core.rs | 14 +++++++----- nac3core/src/codegen/model/int.rs | 30 ++++++++++++------------- nac3core/src/codegen/model/ptr.rs | 10 ++++----- nac3core/src/codegen/model/structure.rs | 2 +- 5 files changed, 30 insertions(+), 28 deletions(-) diff --git a/nac3core/src/codegen/model/array.rs b/nac3core/src/codegen/model/array.rs index 4d7927a1..f46c9252 100644 --- a/nac3core/src/codegen/model/array.rs +++ b/nac3core/src/codegen/model/array.rs @@ -93,7 +93,7 @@ impl<'ctx, Len: ArrayLen, Item: Model<'ctx>> Instance<'ctx, Ptr let zero = ctx.ctx.i32_type().const_zero(); let ptr = unsafe { ctx.builder.build_in_bounds_gep(self.value, &[zero, i], "").unwrap() }; - Ptr(self.model.0.item).believe_value(ptr) + unsafe { Ptr(self.model.0.item).believe_value(ptr) } } /// Like `gep` but `i` is a constant. diff --git a/nac3core/src/codegen/model/core.rs b/nac3core/src/codegen/model/core.rs index 55ad4635..a5c9fd97 100644 --- a/nac3core/src/codegen/model/core.rs +++ b/nac3core/src/codegen/model/core.rs @@ -117,9 +117,11 @@ pub trait Model<'ctx>: fmt::Debug + Clone + Copy + ModelBase<'ctx> { /// Create an instance from a value. /// + /// # Safety + /// /// Caller must make sure the type of `value` and the type of this `model` are equivalent. #[must_use] - fn believe_value(&self, value: Self::Value) -> Instance<'ctx, Self> { + unsafe fn believe_value(&self, value: Self::Value) -> Instance<'ctx, Self> { Instance { model: *self, value } } @@ -138,7 +140,7 @@ pub trait Model<'ctx>: fmt::Debug + Clone + Copy + ModelBase<'ctx> { let Ok(value) = Self::Value::try_from(value) else { unreachable!("check_type() has bad implementation") }; - Ok(self.believe_value(value)) + Ok(unsafe { self.believe_value(value) }) } // Allocate a value on the stack and return its pointer. @@ -148,7 +150,7 @@ pub trait Model<'ctx>: fmt::Debug + Clone + Copy + ModelBase<'ctx> { ctx: &CodeGenContext<'ctx, '_>, ) -> Instance<'ctx, Ptr> { let p = ctx.builder.build_alloca(self.get_type(generator, ctx.ctx), "").unwrap(); - Ptr(*self).believe_value(p) + unsafe { Ptr(*self).believe_value(p) } } // Allocate an array on the stack and return its pointer. @@ -159,7 +161,7 @@ pub trait Model<'ctx>: fmt::Debug + Clone + Copy + ModelBase<'ctx> { len: IntValue<'ctx>, ) -> Instance<'ctx, Ptr> { let p = ctx.builder.build_array_alloca(self.get_type(generator, ctx.ctx), len, "").unwrap(); - Ptr(*self).believe_value(p) + unsafe { Ptr(*self).believe_value(p) } } fn var_alloca( @@ -170,7 +172,7 @@ pub trait Model<'ctx>: fmt::Debug + Clone + Copy + ModelBase<'ctx> { ) -> Result>, String> { let ty = self.get_type(generator, ctx.ctx).as_basic_type_enum(); let p = generator.gen_var_alloc(ctx, ty, name)?; - Ok(Ptr(*self).believe_value(p)) + Ok(unsafe { Ptr(*self).believe_value(p) }) } fn array_var_alloca( @@ -183,7 +185,7 @@ pub trait Model<'ctx>: fmt::Debug + Clone + Copy + ModelBase<'ctx> { // TODO: Remove ArraySliceValue let ty = self.get_type(generator, ctx.ctx).as_basic_type_enum(); let p = generator.gen_array_var_alloc(ctx, ty, len, name)?; - Ok(Ptr(*self).believe_value(PointerValue::from(p))) + Ok(unsafe { Ptr(*self).believe_value(PointerValue::from(p)) }) } /// Allocate a constant array. diff --git a/nac3core/src/codegen/model/int.rs b/nac3core/src/codegen/model/int.rs index 13667cfd..a1e49aa6 100644 --- a/nac3core/src/codegen/model/int.rs +++ b/nac3core/src/codegen/model/int.rs @@ -109,7 +109,7 @@ impl<'ctx, N: IntKind<'ctx>> Int { value: u64, ) -> Instance<'ctx, Self> { let value = self.get_type(generator, ctx).const_int(value, false); - self.believe_value(value) + unsafe { self.believe_value(value) } } pub fn const_0( @@ -118,7 +118,7 @@ impl<'ctx, N: IntKind<'ctx>> Int { ctx: &'ctx Context, ) -> Instance<'ctx, Self> { let value = self.get_type(generator, ctx).const_zero(); - self.believe_value(value) + unsafe { self.believe_value(value) } } pub fn const_1( @@ -135,7 +135,7 @@ impl<'ctx, N: IntKind<'ctx>> Int { ctx: &'ctx Context, ) -> Instance<'ctx, Self> { let value = self.get_type(generator, ctx).const_all_ones(); - self.believe_value(value) + unsafe { self.believe_value(value) } } pub fn s_extend_or_bit_cast( @@ -152,7 +152,7 @@ impl<'ctx, N: IntKind<'ctx>> Int { .builder .build_int_s_extend_or_bit_cast(value, self.get_type(generator, ctx.ctx), "") .unwrap(); - self.believe_value(value) + unsafe { self.believe_value(value) } } pub fn s_extend( @@ -167,7 +167,7 @@ impl<'ctx, N: IntKind<'ctx>> Int { ); let value = ctx.builder.build_int_s_extend(value, self.get_type(generator, ctx.ctx), "").unwrap(); - self.believe_value(value) + unsafe { self.believe_value(value) } } pub fn z_extend_or_bit_cast( @@ -184,7 +184,7 @@ impl<'ctx, N: IntKind<'ctx>> Int { .builder .build_int_z_extend_or_bit_cast(value, self.get_type(generator, ctx.ctx), "") .unwrap(); - self.believe_value(value) + unsafe { self.believe_value(value) } } pub fn z_extend( @@ -199,7 +199,7 @@ impl<'ctx, N: IntKind<'ctx>> Int { ); let value = ctx.builder.build_int_z_extend(value, self.get_type(generator, ctx.ctx), "").unwrap(); - self.believe_value(value) + unsafe { self.believe_value(value) } } pub fn truncate_or_bit_cast( @@ -216,7 +216,7 @@ impl<'ctx, N: IntKind<'ctx>> Int { .builder .build_int_truncate_or_bit_cast(value, self.get_type(generator, ctx.ctx), "") .unwrap(); - self.believe_value(value) + unsafe { self.believe_value(value) } } pub fn truncate( @@ -231,7 +231,7 @@ impl<'ctx, N: IntKind<'ctx>> Int { ); let value = ctx.builder.build_int_truncate(value, self.get_type(generator, ctx.ctx), "").unwrap(); - self.believe_value(value) + unsafe { self.believe_value(value) } } /// `sext` or `trunc` an int to this model's int type. Does nothing if equal bit-widths. @@ -246,7 +246,7 @@ impl<'ctx, N: IntKind<'ctx>> Int { self.0.get_int_type(generator.get_size_type(ctx.ctx), ctx.ctx).get_bit_width(); match their_width.cmp(&our_width) { Ordering::Less => self.s_extend(generator, ctx, value), - Ordering::Equal => self.believe_value(value), + Ordering::Equal => unsafe { self.believe_value(value) }, Ordering::Greater => self.truncate(generator, ctx, value), } } @@ -263,7 +263,7 @@ impl<'ctx, N: IntKind<'ctx>> Int { self.0.get_int_type(generator.get_size_type(ctx.ctx), ctx.ctx).get_bit_width(); match their_width.cmp(&our_width) { Ordering::Less => self.z_extend(generator, ctx, value), - Ordering::Equal => self.believe_value(value), + Ordering::Equal => unsafe { self.believe_value(value) }, Ordering::Greater => self.truncate(generator, ctx, value), } } @@ -365,19 +365,19 @@ impl<'ctx, N: IntKind<'ctx>> Instance<'ctx, Int> { #[must_use] pub fn add(&self, ctx: &CodeGenContext<'ctx, '_>, other: Self) -> Self { let value = ctx.builder.build_int_add(self.value, other.value, "").unwrap(); - self.model.believe_value(value) + unsafe { self.model.believe_value(value) } } #[must_use] pub fn sub(&self, ctx: &CodeGenContext<'ctx, '_>, other: Self) -> Self { let value = ctx.builder.build_int_sub(self.value, other.value, "").unwrap(); - self.model.believe_value(value) + unsafe { self.model.believe_value(value) } } #[must_use] pub fn mul(&self, ctx: &CodeGenContext<'ctx, '_>, other: Self) -> Self { let value = ctx.builder.build_int_mul(self.value, other.value, "").unwrap(); - self.model.believe_value(value) + unsafe { self.model.believe_value(value) } } pub fn compare( @@ -387,6 +387,6 @@ impl<'ctx, N: IntKind<'ctx>> Instance<'ctx, Int> { other: Self, ) -> Instance<'ctx, Int> { let value = ctx.builder.build_int_compare(op, self.value, other.value, "").unwrap(); - Int(Bool).believe_value(value) + unsafe { Int(Bool).believe_value(value) } } } diff --git a/nac3core/src/codegen/model/ptr.rs b/nac3core/src/codegen/model/ptr.rs index ac9493bb..5e76c76d 100644 --- a/nac3core/src/codegen/model/ptr.rs +++ b/nac3core/src/codegen/model/ptr.rs @@ -75,7 +75,7 @@ impl<'ctx, Item: Model<'ctx>> Ptr { ) -> 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(); - self.believe_value(ptr) + unsafe { self.believe_value(ptr) } } /// Cast a pointer into this model with [`inkwell::builder::Builder::build_pointer_cast`] @@ -92,7 +92,7 @@ impl<'ctx, Item: Model<'ctx>> Ptr { // ``` let t = self.get_type(generator, ctx.ctx); let ptr = ctx.builder.build_pointer_cast(ptr, t, "").unwrap(); - self.believe_value(ptr) + unsafe { self.believe_value(ptr) } } } @@ -105,7 +105,7 @@ impl<'ctx, Item: Model<'ctx>> Instance<'ctx, Ptr> { offset: IntValue<'ctx>, ) -> Instance<'ctx, Ptr> { let p = unsafe { ctx.builder.build_in_bounds_gep(self.value, &[offset], "").unwrap() }; - self.model.believe_value(p) + unsafe { self.model.believe_value(p) } } /// Offset the pointer by [`inkwell::builder::Builder::build_in_bounds_gep`] by a constant offset. @@ -184,13 +184,13 @@ impl<'ctx, Item: Model<'ctx>> Instance<'ctx, Ptr> { /// Check if the pointer is null with [`inkwell::builder::Builder::build_is_null`]. pub fn is_null(&self, ctx: &CodeGenContext<'ctx, '_>) -> Instance<'ctx, Int> { let value = ctx.builder.build_is_null(self.value, "").unwrap(); - Int(Bool).believe_value(value) + unsafe { Int(Bool).believe_value(value) } } /// Check if the pointer is not null with [`inkwell::builder::Builder::build_is_not_null`]. pub fn is_not_null(&self, ctx: &CodeGenContext<'ctx, '_>) -> Instance<'ctx, Int> { let value = ctx.builder.build_is_not_null(self.value, "").unwrap(); - Int(Bool).believe_value(value) + unsafe { Int(Bool).believe_value(value) } } /// `memcpy` from another pointer. diff --git a/nac3core/src/codegen/model/structure.rs b/nac3core/src/codegen/model/structure.rs index f94f4603..145657d1 100644 --- a/nac3core/src/codegen/model/structure.rs +++ b/nac3core/src/codegen/model/structure.rs @@ -178,7 +178,7 @@ impl<'ctx, S: StructKind<'ctx>> Instance<'ctx, Ptr>> { .unwrap() }; - Ptr(field.model).believe_value(ptr) + unsafe { Ptr(field.model).believe_value(ptr) } } /// Convenience function equivalent to `.gep(...).load(...)`.