forked from M-Labs/nac3
1
0
Fork 0

unsafe believe_value

This commit is contained in:
lyken 2024-08-28 12:04:10 +08:00
parent 2e4a33d6ee
commit d12fb99ded
No known key found for this signature in database
GPG Key ID: 3BD5FC6AC8325DD8
5 changed files with 30 additions and 28 deletions

View File

@ -93,7 +93,7 @@ impl<'ctx, Len: ArrayLen, Item: Model<'ctx>> Instance<'ctx, Ptr<Array<Len, Item>
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.

View File

@ -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<Self>> {
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<Self>> {
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<G: CodeGenerator + ?Sized>(
@ -170,7 +172,7 @@ pub trait Model<'ctx>: fmt::Debug + Clone + Copy + ModelBase<'ctx> {
) -> Result<Instance<'ctx, Ptr<Self>>, 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<G: CodeGenerator + ?Sized>(
@ -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.

View File

@ -109,7 +109,7 @@ impl<'ctx, N: IntKind<'ctx>> Int<N> {
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<G: CodeGenerator + ?Sized>(
@ -118,7 +118,7 @@ impl<'ctx, N: IntKind<'ctx>> Int<N> {
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<G: CodeGenerator + ?Sized>(
@ -135,7 +135,7 @@ impl<'ctx, N: IntKind<'ctx>> Int<N> {
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<G: CodeGenerator + ?Sized>(
@ -152,7 +152,7 @@ impl<'ctx, N: IntKind<'ctx>> Int<N> {
.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<G: CodeGenerator + ?Sized>(
@ -167,7 +167,7 @@ impl<'ctx, N: IntKind<'ctx>> Int<N> {
);
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<G: CodeGenerator + ?Sized>(
@ -184,7 +184,7 @@ impl<'ctx, N: IntKind<'ctx>> Int<N> {
.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<G: CodeGenerator + ?Sized>(
@ -199,7 +199,7 @@ impl<'ctx, N: IntKind<'ctx>> Int<N> {
);
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<G: CodeGenerator + ?Sized>(
@ -216,7 +216,7 @@ impl<'ctx, N: IntKind<'ctx>> Int<N> {
.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<G: CodeGenerator + ?Sized>(
@ -231,7 +231,7 @@ impl<'ctx, N: IntKind<'ctx>> Int<N> {
);
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<N> {
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<N> {
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<N>> {
#[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<N>> {
other: Self,
) -> Instance<'ctx, Int<Bool>> {
let value = ctx.builder.build_int_compare(op, self.value, other.value, "").unwrap();
Int(Bool).believe_value(value)
unsafe { Int(Bool).believe_value(value) }
}
}

View File

@ -75,7 +75,7 @@ impl<'ctx, Item: Model<'ctx>> Ptr<Item> {
) -> 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();
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<Item> {
// ```
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<Item>> {
offset: IntValue<'ctx>,
) -> Instance<'ctx, Ptr<Item>> {
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<Item>> {
/// Check if the pointer is null with [`inkwell::builder::Builder::build_is_null`].
pub fn is_null(&self, ctx: &CodeGenContext<'ctx, '_>) -> Instance<'ctx, Int<Bool>> {
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<Bool>> {
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.

View File

@ -178,7 +178,7 @@ impl<'ctx, S: StructKind<'ctx>> Instance<'ctx, Ptr<Struct<S>>> {
.unwrap()
};
Ptr(field.model).believe_value(ptr)
unsafe { Ptr(field.model).believe_value(ptr) }
}
/// Convenience function equivalent to `.gep(...).load(...)`.