chore: Rename Operand -> ImmediateOperand

This commit is contained in:
2025-06-05 10:07:57 +09:00
parent ef4663a245
commit 232b73aad8
8 changed files with 141 additions and 130 deletions

View File

@@ -20,70 +20,75 @@ pub type DWord = u32;
/// 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.
pub enum Operand {
pub enum ImmediateOperand {
Byte(Byte),
Word(Word),
}
impl Add for Operand {
impl Add for ImmediateOperand {
type Output = Self;
fn add(self, other: Self) -> Self {
match self {
Operand::Byte(lhsb) => match other {
Operand::Byte(rhsb) => Operand::Byte(lhsb.wrapping_add(rhsb)),
ImmediateOperand::Byte(lhsb) => match other {
ImmediateOperand::Byte(rhsb) => ImmediateOperand::Byte(lhsb.wrapping_add(rhsb)),
_ => panic!("Cannot add Word to Byte"),
},
Operand::Word(lhsw) => match other {
Operand::Word(rhsw) => Operand::Word(lhsw.wrapping_add(rhsw)),
Operand::Byte(rhsb) => Operand::Word(lhsw.wrapping_add(rhsb as Word)),
ImmediateOperand::Word(lhsw) => match other {
ImmediateOperand::Word(rhsw) => ImmediateOperand::Word(lhsw.wrapping_add(rhsw)),
ImmediateOperand::Byte(rhsb) => {
ImmediateOperand::Word(lhsw.wrapping_add(rhsb as Word))
}
},
}
}
}
impl Add<Displacement> for Operand {
type Output = Operand;
impl Add<Displacement> for ImmediateOperand {
type Output = ImmediateOperand;
fn add(self, disp: Displacement) -> Self::Output {
// XXX: ugly hack to add Operand with Displacement
// Warning: this gets rid of the sign, which is fine as long as it is
// used for a memory index, which can never be negative.
match disp {
Displacement::IByte(byte) => {
if byte < 0 {
return self - Operand::Byte((byte * -1) as Byte);
return self - ImmediateOperand::Byte((byte * -1) as Byte);
} else {
return self + Operand::Byte(byte as Byte);
return self + ImmediateOperand::Byte(byte as Byte);
}
}
Displacement::IWord(word) => {
if word < 0 {
return self - Operand::Word((word * -1) as Word);
return self - ImmediateOperand::Word((word * -1) as Word);
} else {
return self + Operand::Word(word as Word);
return self + ImmediateOperand::Word(word as Word);
}
}
}
}
}
impl Sub for Operand {
impl Sub for ImmediateOperand {
type Output = Self;
fn sub(self, other: Self) -> Self {
match self {
Operand::Byte(lhsb) => match other {
Operand::Byte(rhsb) => Operand::Byte(lhsb.wrapping_sub(rhsb)),
ImmediateOperand::Byte(lhsb) => match other {
ImmediateOperand::Byte(rhsb) => ImmediateOperand::Byte(lhsb.wrapping_sub(rhsb)),
_ => panic!("Cannot substract Word from Byte"),
},
Operand::Word(lhsw) => match other {
Operand::Word(rhsw) => Operand::Word(lhsw.wrapping_sub(rhsw)),
Operand::Byte(rhsb) => Operand::Word(lhsw.wrapping_sub(rhsb as Word)),
ImmediateOperand::Word(lhsw) => match other {
ImmediateOperand::Word(rhsw) => ImmediateOperand::Word(lhsw.wrapping_sub(rhsw)),
ImmediateOperand::Byte(rhsb) => {
ImmediateOperand::Word(lhsw.wrapping_sub(rhsb as Word))
}
},
}
}
}
impl fmt::Display for Operand {
impl fmt::Display for ImmediateOperand {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Self::Byte(byte) => write!(f, "{}", byte),
@@ -92,7 +97,7 @@ impl fmt::Display for Operand {
}
}
impl fmt::LowerHex for Operand {
impl fmt::LowerHex for ImmediateOperand {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Byte(b) => fmt::LowerHex::fmt(b, f),