Add rest of the bitwise operations to library

This commit is contained in:
Sofia 2025-07-28 12:10:01 +03:00
parent 49084ea0af
commit d06eff9347
4 changed files with 71 additions and 43 deletions

View File

@ -410,6 +410,8 @@ impl Builder {
} }
} }
Instr::And(lhs, rhs) => match_types(&lhs, &rhs, &self).map(|_| ()), 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) => { Instr::ICmp(_, lhs, rhs) => {
let t = match_types(&lhs, &rhs, self)?; let t = match_types(&lhs, &rhs, self)?;
if t.category().comparable() || !t.category().integer() { if t.category().comparable() || !t.category().integer() {
@ -528,6 +530,30 @@ impl Builder {
Instr::PtrToInt(instr, ty) => instr.cast_to(self, &ty).map(|_| ()), Instr::PtrToInt(instr, ty) => instr.cast_to(self, &ty).map(|_| ()),
Instr::IntToPtr(instr, ty) => instr.cast_to(self, &ty).map(|_| ()), Instr::IntToPtr(instr, ty) => instr.cast_to(self, &ty).map(|_| ()),
Instr::BitCast(..) => Ok(()), 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), SRem(lhs, rhs) => match_types(lhs, rhs, &builder),
FRem(lhs, rhs) => match_types(lhs, rhs, &builder), FRem(lhs, rhs) => match_types(lhs, rhs, &builder),
And(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), ICmp(_, _, _) => Ok(Type::Bool),
FCmp(_, _, _) => Ok(Type::Bool), FCmp(_, _, _) => Ok(Type::Bool),
FunctionCall(function_value, _) => Ok(builder.function_data(function_value).ret), 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()), PtrToInt(instr, ty) => instr.cast_to(builder, ty).map(|_| ty.clone()),
IntToPtr(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()), BitCast(_, ty) => Ok(ty.clone()),
ShiftRightLogical(lhs, _) => lhs.get_type(builder),
ShiftRightArithmetic(lhs, _) => lhs.get_type(builder),
ShiftLeft(lhs, _) => lhs.get_type(builder),
} }
} }
} }

View File

@ -975,6 +975,11 @@ impl InstructionHolder {
ty.as_llvm(module.context_ref, &module.types), ty.as_llvm(module.context_ref, &module.types),
name.as_ptr(), 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 { if let Some(record) = &self.record {

View File

@ -9,10 +9,10 @@ use crate::{
CmpPredicate, Context, Instr, InstructionData, TerminatorKind, CmpPredicate, Context, Instr, InstructionData, TerminatorKind,
builder::*, builder::*,
debug_information::{ debug_information::{
DebugArrayType, DebugBasicType, DebugFieldType, DebugInformation, DebugLocalVariable, DebugArrayType, DebugBasicType, DebugFieldType, DebugInformation, DebugLocalVariable, DebugLocation,
DebugLocation, DebugLocationValue, DebugMetadata, DebugMetadataValue, DebugParamVariable, DebugLocationValue, DebugMetadata, DebugMetadataValue, DebugParamVariable, DebugPointerType, DebugPosition,
DebugPointerType, DebugPosition, DebugProgramValue, DebugRecordKind, DebugScopeValue, DebugProgramValue, DebugRecordKind, DebugScopeValue, DebugStructType, DebugSubprogramType, DebugTypeData,
DebugStructType, DebugSubprogramType, DebugTypeData, DebugTypeHolder, DebugTypeValue, DebugTypeHolder, DebugTypeValue,
}, },
pad_adapter::PadAdapter, pad_adapter::PadAdapter,
}; };
@ -68,11 +68,7 @@ impl FunctionHolder {
.map(|p| format!("{:?}", p)) .map(|p| format!("{:?}", p))
.collect::<Vec<_>>() .collect::<Vec<_>>()
.join(", "); .join(", ");
write!( write!(f, "fn {}({}) -> {:?} ", self.data.name, params, self.data.ret)?;
f,
"fn {}({}) -> {:?} ",
self.data.name, params, self.data.ret
)?;
writeln!(f, "{{")?; writeln!(f, "{{")?;
let mut state = Default::default(); let mut state = Default::default();
@ -116,11 +112,7 @@ impl BlockHolder {
terminator.builder_fmt(&mut inner, builder, debug)?; terminator.builder_fmt(&mut inner, builder, debug)?;
} }
if let Some(location) = self.data.terminator_location { if let Some(location) = self.data.terminator_location {
writeln!( writeln!(inner, " ^ (At {}) ", debug.as_ref().unwrap().get_location(location))?;
inner,
" ^ (At {}) ",
debug.as_ref().unwrap().get_location(location)
)?;
} }
Ok(()) Ok(())
@ -148,11 +140,7 @@ impl InstructionHolder {
writeln!(f, " (Debug {} {})", record.variable.hr(debug), kind)?; writeln!(f, " (Debug {} {})", record.variable.hr(debug), kind)?;
} }
} }
writeln!( writeln!(f, "{:?} ({}) = {:?} ", self.value, self.name, self.data.kind)?;
f,
"{:?} ({}) = {:?} ",
self.value, self.name, self.data.kind
)?;
if let Some(debug) = debug { if let Some(debug) = debug {
if let Some(location) = self.data.location { if let Some(location) = self.data.location {
writeln!(f, " ^ (At {}) ", debug.get_location(location))?; writeln!(f, " ^ (At {}) ", debug.get_location(location))?;
@ -188,9 +176,9 @@ impl TerminatorKind {
impl DebugMetadataValue { impl DebugMetadataValue {
fn hr(&self, debug: &DebugInformation) -> String { fn hr(&self, debug: &DebugInformation) -> String {
let kind = match debug.get_metadata(*self) { let kind = match debug.get_metadata(*self) {
DebugMetadata::ParamVar(DebugParamVariable { DebugMetadata::ParamVar(DebugParamVariable { name, arg_idx, ty, .. }) => {
name, arg_idx, ty, .. format!("param {} (idx {}) (type {:?}) ", name, arg_idx, ty)
}) => format!("param {} (idx {}) (type {:?}) ", name, arg_idx, ty), }
DebugMetadata::LocalVar(DebugLocalVariable { name, ty, .. }) => { DebugMetadata::LocalVar(DebugLocalVariable { name, ty, .. }) => {
format!("var {} (type {:?}) ", name, ty) format!("var {} (type {:?}) ", name, ty)
} }
@ -253,14 +241,11 @@ impl Debug for FunctionHolder {
impl Debug for BlockHolder { impl Debug for BlockHolder {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let deleted = if self.data.deleted { " (deleted)" } else { "" }; let deleted = if self.data.deleted { " (deleted)" } else { "" };
f.debug_tuple(&format!( f.debug_tuple(&format!("{}[{:?}]{} ", &self.data.name, &self.value, deleted))
"{}[{:?}]{} ", .field(&self.instructions)
&self.data.name, &self.value, deleted .field(&self.data.terminator)
)) .field(&self.data.terminator_location)
.field(&self.instructions) .finish()
.field(&self.data.terminator)
.field(&self.data.terminator_location)
.finish()
} }
} }
@ -303,11 +288,7 @@ impl Debug for BlockValue {
impl Debug for InstructionValue { impl Debug for InstructionValue {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!( write!(f, "%{}.{}.{}.{}", self.0.0.0.0, self.0.0.1, self.0.1, self.1)
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())?; fmt_index(f, instruction_value, &index.to_string())?;
write!(f, ")") write!(f, ")")
} }
Instr::ExtractValue(instruction_value, index) => { Instr::ExtractValue(instruction_value, index) => fmt_index(f, instruction_value, &index.to_string()),
fmt_index(f, instruction_value, &index.to_string())
}
Instr::Trunc(instr_val, ty) => { Instr::Trunc(instr_val, ty) => {
write!(f, "{:?} to {:?} ({})", instr_val, ty, self.default_name()) write!(f, "{:?} to {:?} ({})", instr_val, ty, self.default_name())
} }
@ -408,6 +387,11 @@ impl Debug for Instr {
Instr::BitCast(instr_val, ty) => { Instr::BitCast(instr_val, ty) => {
write!(f, "{:?} to {:?} ({})", instr_val, ty, self.default_name()) 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!( write!(
f, f,
"Scope[{}]", "Scope[{}]",
self.0 self.0.iter().map(|v| v.to_string()).collect::<Vec<_>>().join(", ")
.iter()
.map(|v| v.to_string())
.collect::<Vec<_>>()
.join(", ")
) )
} }
} }

View File

@ -251,6 +251,11 @@ impl Instr {
Instr::PtrToInt(_, _) => "ptrtoint", Instr::PtrToInt(_, _) => "ptrtoint",
Instr::IntToPtr(_, _) => "inttoptr", Instr::IntToPtr(_, _) => "inttoptr",
Instr::BitCast(_, _) => "bitcast", 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), SRem(InstructionValue, InstructionValue),
/// Get the remainder from two floats /// Get the remainder from two floats
FRem(InstructionValue, InstructionValue), FRem(InstructionValue, InstructionValue),
And(InstructionValue, InstructionValue), And(InstructionValue, InstructionValue),
Or(InstructionValue, InstructionValue),
XOr(InstructionValue, InstructionValue),
ShiftRightLogical(InstructionValue, InstructionValue),
ShiftRightArithmetic(InstructionValue, InstructionValue),
ShiftLeft(InstructionValue, InstructionValue),
Phi(Vec<InstructionValue>), Phi(Vec<InstructionValue>),
Alloca(Type), Alloca(Type),