From 740aee13827242e921056ec2200477161a0d2ef1 Mon Sep 17 00:00:00 2001 From: sofia Date: Sat, 28 Jun 2025 18:56:24 +0300 Subject: [PATCH] Add SIGN to IntegerType --- reid-llvm-lib/examples/libtest.rs | 12 ++--- reid-llvm-lib/src/lib.rs | 4 +- reid-llvm-lib/src/types.rs | 80 ++++++------------------------- 3 files changed, 24 insertions(+), 72 deletions(-) diff --git a/reid-llvm-lib/examples/libtest.rs b/reid-llvm-lib/examples/libtest.rs index 7cb8ced..f6ce7e5 100644 --- a/reid-llvm-lib/examples/libtest.rs +++ b/reid-llvm-lib/examples/libtest.rs @@ -11,20 +11,20 @@ pub fn main() { let module = context.module("testmodule"); - let int_32 = context.integer_type::<32>(); + let int_32 = context.integer_type::<32, true>(); let secondary = module.add_function(int_32.function_type(&[]), "secondary"); let s_entry = secondary.block("entry"); - s_entry.ret(&int_32.from_const(54, 1)).unwrap(); + s_entry.ret(&int_32.from_const(54)).unwrap(); let function = module.add_function(int_32.function_type(&[]), "main"); let entry = function.block("entry"); - let v1 = int_32.from_const(100, 1); + let v1 = int_32.from_const(100); let v2 = entry.call(&secondary, vec![], "call").unwrap(); let lhs_cmp = entry.add(&v1, &v2, "add").unwrap(); - let rhs_cmp = int_32.from_const(200, 1); + let rhs_cmp = int_32.from_const(200); let cond_res = entry .integer_compare(&lhs_cmp, &rhs_cmp, &Comparison::LessThan, "cmp") @@ -32,8 +32,8 @@ pub fn main() { let (lhs, rhs) = entry.conditional_br(&cond_res, "lhs", "rhs").unwrap(); - lhs.ret(&int_32.from_const(123, 1)).unwrap(); - rhs.ret(&int_32.from_const(456, 1)).unwrap(); + lhs.ret(&int_32.from_const(123)).unwrap(); + rhs.ret(&int_32.from_const(456)).unwrap(); match module.print_to_string() { Ok(v) => println!("{}", v), diff --git a/reid-llvm-lib/src/lib.rs b/reid-llvm-lib/src/lib.rs index a55b630..4a8040a 100644 --- a/reid-llvm-lib/src/lib.rs +++ b/reid-llvm-lib/src/lib.rs @@ -42,7 +42,9 @@ impl Context { } } - pub fn integer_type<'a, const T: u32>(&'a self) -> IntegerType<'a, T> { + pub fn integer_type<'a, const WIDTH: u32, const SIGN: bool>( + &'a self, + ) -> IntegerType<'a, WIDTH, SIGN> { IntegerType::in_context(&self) } diff --git a/reid-llvm-lib/src/types.rs b/reid-llvm-lib/src/types.rs index c288150..64b5547 100644 --- a/reid-llvm-lib/src/types.rs +++ b/reid-llvm-lib/src/types.rs @@ -48,41 +48,48 @@ impl PartialEq for &dyn BasicType { } } -pub struct IntegerType<'ctx, const T: u32> { +pub struct IntegerType<'ctx, const WIDTH: u32, const SIGNED: bool> { context: &'ctx Context, type_ref: LLVMTypeRef, } -impl<'ctx, const T: u32> BasicType for IntegerType<'ctx, T> { +impl<'ctx, const WIDTH: u32, const SIGNED: bool> BasicType for IntegerType<'ctx, WIDTH, SIGNED> { fn llvm_type(&self) -> LLVMTypeRef { self.type_ref } } -impl<'ctx, const T: u32> IntegerType<'ctx, T> { - pub(crate) fn in_context(context: &Context) -> IntegerType { +impl<'ctx, const WIDTH: u32, const SIGNED: bool> IntegerType<'ctx, WIDTH, SIGNED> { + pub(crate) fn in_context(context: &Context) -> IntegerType { let type_ref = unsafe { - match T { + match WIDTH { 128 => LLVMInt128TypeInContext(context.context_ref), 64 => LLVMInt64TypeInContext(context.context_ref), 32 => LLVMInt32TypeInContext(context.context_ref), 16 => LLVMInt16TypeInContext(context.context_ref), 8 => LLVMInt8TypeInContext(context.context_ref), 1 => LLVMInt1TypeInContext(context.context_ref), - _ => LLVMIntTypeInContext(context.context_ref, T), + _ => LLVMIntTypeInContext(context.context_ref, WIDTH), } }; IntegerType { context, type_ref } } - pub fn from_const(&self, value: u64, sign: i32) -> OpaqueValue { + pub fn from_const(&self, value: u64) -> OpaqueValue { unsafe { OpaqueValue { basic_type: self, - value_ref: LLVMConstInt(self.type_ref, value, sign), + value_ref: LLVMConstInt(self.type_ref, value, Self::sign_to_i32()), } } } + + const fn sign_to_i32() -> i32 { + match SIGNED { + true => 1, + false => 0, + } + } } pub struct FunctionType<'ctx, ReturnType: BasicType> { @@ -131,60 +138,3 @@ impl<'ctx> OpaqueValue<'ctx> { } } } - -// pub trait IRType { -// const SIGNED: LLVMBool; -// unsafe fn llvm_type(context: &IRContext) -> LLVMTypeRef; -// } - -// impl IRType for bool { -// const SIGNED: LLVMBool = 0; -// unsafe fn llvm_type(context: &IRContext) -> LLVMTypeRef { -// unsafe { LLVMInt1TypeInContext(context.context) } -// } -// } - -// impl IRType for i32 { -// const SIGNED: LLVMBool = 1; -// unsafe fn llvm_type(context: &IRContext) -> LLVMTypeRef { -// unsafe { LLVMInt32TypeInContext(context.context) } -// } -// } - -// impl IRType for u32 { -// const SIGNED: LLVMBool = 0; -// unsafe fn llvm_type(context: &IRContext) -> LLVMTypeRef { -// unsafe { LLVMInt32TypeInContext(context.context) } -// } -// } - -// impl IRType for u16 { -// const SIGNED: LLVMBool = 0; -// unsafe fn llvm_type(context: &IRContext) -> LLVMTypeRef { -// unsafe { LLVMInt16TypeInContext(context.context) } -// } -// } - -// pub struct IRValue(PhantomData, pub(crate) OpaqueIRValue); - -// impl IRValue { -// pub(crate) unsafe fn from_runtime(t: LLVMTypeRef, value: LLVMValueRef) -> IRValue { -// IRValue(PhantomData, OpaqueIRValue(t, value)) -// } -// } - -// impl> IRValue { -// pub fn from_const(context: &IRContext, value: T) -> Self { -// unsafe { -// let t = T::llvm_type(context); -// let value = LLVMConstInt(t, value.into() as u64, T::SIGNED); -// IRValue(PhantomData, OpaqueIRValue(t, value)) -// } -// } -// } - -// impl From> for OpaqueIRValue { -// fn from(value: IRValue) -> Self { -// value.1 -// } -// }