From d06eff93474e9cffa34185f04a99e0f15c81c213 Mon Sep 17 00:00:00 2001 From: sofia Date: Mon, 28 Jul 2025 12:10:01 +0300 Subject: [PATCH] Add rest of the bitwise operations to library --- reid-llvm-lib/src/builder.rs | 31 +++++++++++++++++ reid-llvm-lib/src/compile.rs | 5 +++ reid-llvm-lib/src/fmt.rs | 66 +++++++++++++----------------------- reid-llvm-lib/src/lib.rs | 12 +++++++ 4 files changed, 71 insertions(+), 43 deletions(-) diff --git a/reid-llvm-lib/src/builder.rs b/reid-llvm-lib/src/builder.rs index 47806f3..1094fa6 100644 --- a/reid-llvm-lib/src/builder.rs +++ b/reid-llvm-lib/src/builder.rs @@ -410,6 +410,8 @@ impl Builder { } } Instr::And(lhs, rhs) => match_types(&lhs, &rhs, &self).map(|_| ()), + Instr::Or(lhs, rhs) => match_types(&lhs, &rhs, &self).map(|_| ()), + Instr::XOr(lhs, rhs) => match_types(&lhs, &rhs, &self).map(|_| ()), Instr::ICmp(_, lhs, rhs) => { let t = match_types(&lhs, &rhs, self)?; if t.category().comparable() || !t.category().integer() { @@ -528,6 +530,30 @@ impl Builder { Instr::PtrToInt(instr, ty) => instr.cast_to(self, &ty).map(|_| ()), Instr::IntToPtr(instr, ty) => instr.cast_to(self, &ty).map(|_| ()), Instr::BitCast(..) => Ok(()), + Instr::ShiftRightLogical(_, rhs) => { + let rhs_ty = rhs.get_type(&self)?; + if rhs_ty.category() == TypeCategory::UnsignedInteger { + Ok(()) + } else { + Err(ErrorKind::Null) + } + } + Instr::ShiftRightArithmetic(_, rhs) => { + let rhs_ty = rhs.get_type(&self)?; + if rhs_ty.category() == TypeCategory::UnsignedInteger { + Ok(()) + } else { + Err(ErrorKind::Null) + } + } + Instr::ShiftLeft(_, rhs) => { + let rhs_ty = rhs.get_type(&self)?; + if rhs_ty.category() == TypeCategory::UnsignedInteger { + Ok(()) + } else { + Err(ErrorKind::Null) + } + } } } } @@ -614,6 +640,8 @@ impl InstructionValue { SRem(lhs, rhs) => match_types(lhs, rhs, &builder), FRem(lhs, rhs) => match_types(lhs, rhs, &builder), And(lhs, rhs) => match_types(lhs, rhs, &builder), + Or(lhs, rhs) => match_types(lhs, rhs, &builder), + XOr(lhs, rhs) => match_types(lhs, rhs, &builder), ICmp(_, _, _) => Ok(Type::Bool), FCmp(_, _, _) => Ok(Type::Bool), FunctionCall(function_value, _) => Ok(builder.function_data(function_value).ret), @@ -674,6 +702,9 @@ impl InstructionValue { PtrToInt(instr, ty) => instr.cast_to(builder, ty).map(|_| ty.clone()), IntToPtr(instr, ty) => instr.cast_to(builder, ty).map(|_| ty.clone()), BitCast(_, ty) => Ok(ty.clone()), + ShiftRightLogical(lhs, _) => lhs.get_type(builder), + ShiftRightArithmetic(lhs, _) => lhs.get_type(builder), + ShiftLeft(lhs, _) => lhs.get_type(builder), } } } diff --git a/reid-llvm-lib/src/compile.rs b/reid-llvm-lib/src/compile.rs index 271a6cf..039e9a5 100644 --- a/reid-llvm-lib/src/compile.rs +++ b/reid-llvm-lib/src/compile.rs @@ -975,6 +975,11 @@ impl InstructionHolder { ty.as_llvm(module.context_ref, &module.types), name.as_ptr(), ), + Or(instruction_value, instruction_value1) => todo!(), + XOr(instruction_value, instruction_value1) => todo!(), + ShiftRightLogical(instruction_value, instruction_value1) => todo!(), + ShiftRightArithmetic(instruction_value, instruction_value1) => todo!(), + ShiftLeft(instruction_value, instruction_value1) => todo!(), } }; if let Some(record) = &self.record { diff --git a/reid-llvm-lib/src/fmt.rs b/reid-llvm-lib/src/fmt.rs index 112b411..b351d06 100644 --- a/reid-llvm-lib/src/fmt.rs +++ b/reid-llvm-lib/src/fmt.rs @@ -9,10 +9,10 @@ use crate::{ CmpPredicate, Context, Instr, InstructionData, TerminatorKind, builder::*, debug_information::{ - DebugArrayType, DebugBasicType, DebugFieldType, DebugInformation, DebugLocalVariable, - DebugLocation, DebugLocationValue, DebugMetadata, DebugMetadataValue, DebugParamVariable, - DebugPointerType, DebugPosition, DebugProgramValue, DebugRecordKind, DebugScopeValue, - DebugStructType, DebugSubprogramType, DebugTypeData, DebugTypeHolder, DebugTypeValue, + DebugArrayType, DebugBasicType, DebugFieldType, DebugInformation, DebugLocalVariable, DebugLocation, + DebugLocationValue, DebugMetadata, DebugMetadataValue, DebugParamVariable, DebugPointerType, DebugPosition, + DebugProgramValue, DebugRecordKind, DebugScopeValue, DebugStructType, DebugSubprogramType, DebugTypeData, + DebugTypeHolder, DebugTypeValue, }, pad_adapter::PadAdapter, }; @@ -68,11 +68,7 @@ impl FunctionHolder { .map(|p| format!("{:?}", p)) .collect::>() .join(", "); - write!( - f, - "fn {}({}) -> {:?} ", - self.data.name, params, self.data.ret - )?; + write!(f, "fn {}({}) -> {:?} ", self.data.name, params, self.data.ret)?; writeln!(f, "{{")?; let mut state = Default::default(); @@ -116,11 +112,7 @@ impl BlockHolder { terminator.builder_fmt(&mut inner, builder, debug)?; } if let Some(location) = self.data.terminator_location { - writeln!( - inner, - " ^ (At {}) ", - debug.as_ref().unwrap().get_location(location) - )?; + writeln!(inner, " ^ (At {}) ", debug.as_ref().unwrap().get_location(location))?; } Ok(()) @@ -148,11 +140,7 @@ impl InstructionHolder { writeln!(f, " (Debug {} {})", record.variable.hr(debug), kind)?; } } - writeln!( - f, - "{:?} ({}) = {:?} ", - self.value, self.name, self.data.kind - )?; + writeln!(f, "{:?} ({}) = {:?} ", self.value, self.name, self.data.kind)?; if let Some(debug) = debug { if let Some(location) = self.data.location { writeln!(f, " ^ (At {}) ", debug.get_location(location))?; @@ -188,9 +176,9 @@ impl TerminatorKind { impl DebugMetadataValue { fn hr(&self, debug: &DebugInformation) -> String { let kind = match debug.get_metadata(*self) { - DebugMetadata::ParamVar(DebugParamVariable { - name, arg_idx, ty, .. - }) => format!("param {} (idx {}) (type {:?}) ", name, arg_idx, ty), + DebugMetadata::ParamVar(DebugParamVariable { name, arg_idx, ty, .. }) => { + format!("param {} (idx {}) (type {:?}) ", name, arg_idx, ty) + } DebugMetadata::LocalVar(DebugLocalVariable { name, ty, .. }) => { format!("var {} (type {:?}) ", name, ty) } @@ -253,14 +241,11 @@ impl Debug for FunctionHolder { impl Debug for BlockHolder { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { let deleted = if self.data.deleted { " (deleted)" } else { "" }; - f.debug_tuple(&format!( - "{}[{:?}]{} ", - &self.data.name, &self.value, deleted - )) - .field(&self.instructions) - .field(&self.data.terminator) - .field(&self.data.terminator_location) - .finish() + f.debug_tuple(&format!("{}[{:?}]{} ", &self.data.name, &self.value, deleted)) + .field(&self.instructions) + .field(&self.data.terminator) + .field(&self.data.terminator_location) + .finish() } } @@ -303,11 +288,7 @@ impl Debug for BlockValue { impl Debug for InstructionValue { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!( - f, - "%{}.{}.{}.{}", - self.0.0.0.0, self.0.0.1, self.0.1, self.1 - ) + write!(f, "%{}.{}.{}.{}", self.0.0.0.0, self.0.0.1, self.0.1, self.1) } } @@ -369,9 +350,7 @@ impl Debug for Instr { fmt_index(f, instruction_value, &index.to_string())?; write!(f, ")") } - Instr::ExtractValue(instruction_value, index) => { - fmt_index(f, instruction_value, &index.to_string()) - } + Instr::ExtractValue(instruction_value, index) => fmt_index(f, instruction_value, &index.to_string()), Instr::Trunc(instr_val, ty) => { write!(f, "{:?} to {:?} ({})", instr_val, ty, self.default_name()) } @@ -408,6 +387,11 @@ impl Debug for Instr { Instr::BitCast(instr_val, ty) => { write!(f, "{:?} to {:?} ({})", instr_val, ty, self.default_name()) } + Instr::Or(lhs, rhs) => fmt_binop(f, lhs, &"||", rhs), + Instr::XOr(lhs, rhs) => fmt_binop(f, lhs, &"^", rhs), + Instr::ShiftRightLogical(lhs, rhs) => fmt_binop(f, lhs, &">>l", rhs), + Instr::ShiftRightArithmetic(lhs, rhs) => fmt_binop(f, lhs, &">>a", rhs), + Instr::ShiftLeft(lhs, rhs) => fmt_binop(f, lhs, &"<<", rhs), } } } @@ -579,11 +563,7 @@ impl Debug for DebugScopeValue { write!( f, "Scope[{}]", - self.0 - .iter() - .map(|v| v.to_string()) - .collect::>() - .join(", ") + self.0.iter().map(|v| v.to_string()).collect::>().join(", ") ) } } diff --git a/reid-llvm-lib/src/lib.rs b/reid-llvm-lib/src/lib.rs index 628c31b..8698501 100644 --- a/reid-llvm-lib/src/lib.rs +++ b/reid-llvm-lib/src/lib.rs @@ -251,6 +251,11 @@ impl Instr { Instr::PtrToInt(_, _) => "ptrtoint", Instr::IntToPtr(_, _) => "inttoptr", Instr::BitCast(_, _) => "bitcast", + Instr::Or(..) => "or", + Instr::XOr(..) => "xor", + Instr::ShiftRightLogical(..) => "lshr", + Instr::ShiftRightArithmetic(..) => "ashr", + Instr::ShiftLeft(..) => "shl", } } } @@ -369,7 +374,14 @@ pub enum Instr { SRem(InstructionValue, InstructionValue), /// Get the remainder from two floats FRem(InstructionValue, InstructionValue), + And(InstructionValue, InstructionValue), + Or(InstructionValue, InstructionValue), + XOr(InstructionValue, InstructionValue), + ShiftRightLogical(InstructionValue, InstructionValue), + ShiftRightArithmetic(InstructionValue, InstructionValue), + ShiftLeft(InstructionValue, InstructionValue), + Phi(Vec), Alloca(Type),