ft: impl flag setting closure for binary operations

This commit is contained in:
2025-06-10 14:35:00 +09:00
parent 35fefb7625
commit 5529fc0b89
3 changed files with 94 additions and 30 deletions

View File

@@ -16,15 +16,53 @@ pub type IWord = i16; // used for displacement of memory access
pub type DWord = u32;
#[derive(Debug, Clone, Ord, Eq, PartialEq, PartialOrd, Copy)]
/// Encodes either Byte- or Word-sized operands.
/// Encodes either Byte- or Word-sized immediate operands.
/// Also sometimes used to decide if an instruction is Byte- or Word-sized,
/// which is usually indicated by using a value of 0 and the disregarding
/// the value when read.
/// Can either be interpreted as signed or unsigned, depending on the context.
pub enum ImmediateOperand {
Byte(Byte),
Word(Word),
}
impl ImmediateOperand {
// Check if value is zero.
pub fn zero(&self) -> bool {
match self {
Self::Byte(byte) => return *byte == 0,
Self::Word(word) => return *word == 0,
}
}
// Check if leasy significant byte has even number of 1's.
pub fn parity(&self) -> bool {
match self {
Self::Byte(byte) => return byte.count_ones() % 2 != 0,
Self::Word(word) => {
let [low, _]: [u8; 2] = word.to_le_bytes();
return low.count_ones() % 2 != 0;
}
}
}
/// Check if least significant bit is set.
pub fn lsb(&self) -> bool {
match self {
Self::Byte(byte) => return byte & 1 == 1,
Self::Word(word) => return word & 1 == 1,
}
}
/// Check if most significant bit is set.
pub fn msb(&self) -> bool {
match self {
Self::Byte(byte) => return byte & (1 << 3) == 1,
Self::Word(word) => return word & (1 << 7) == 1,
}
}
}
impl Add for ImmediateOperand {
type Output = Self;
@@ -162,7 +200,7 @@ impl std::fmt::Display for ModRmTarget {
}
#[derive(Debug, Clone, PartialEq, Eq, Copy)]
/// Memory displacements are signed versions of Byte and Word operands.
/// Memory displacements are concrete signed versions of Byte and Word operands.
/// Encodes either Byte- or Word-sized operands.
/// Generally, a [`Displacement`] is the result of a ModRM byte parse and
/// usually really is the signed displacement of a [`MemoryIndex`] for memory