#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Alu {
Add,
Sub,
And,
Or,
Xor,
Nor,
Less,
LessUnsigned,
ShiftLeft,
ShiftRight,
ShiftRightArithmetic,
Rotate,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Branch {
Equal,
NotEqual,
LessEqualZero,
GreaterZero,
LessZero,
GreaterEqualZero,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Trap {
GreaterEqual,
GreaterEqualUnsigned,
Less,
LessUnsigned,
Equal,
NotEqual,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FloatOp {
Add,
Sub,
Mul,
Div,
Sqrt,
Abs,
Move,
Negate,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Rounding {
Nearest,
Zero,
Up,
Down,
Current,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FloatFormat {
Single,
Double,
Word,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FloatTest {
Zero,
NotZero,
Flag {
set: bool,
flag: u8,
},
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Op {
Register(Alu),
Immediate(Alu),
Shift(Alu),
Lui,
Multiply {
signed: bool,
},
MultiplyAdd {
signed: bool,
subtract: bool,
},
Divide {
signed: bool,
},
MultiplyLow,
MoveFromHi,
MoveFromLo,
MoveToHi,
MoveToLo,
MoveIfZero,
MoveIfNotZero,
CountLeadingZeros,
CountLeadingOnes,
Extract,
Insert,
SignExtendByte,
SignExtendHalf,
SwapBytesInHalves,
Load {
bytes: u8,
signed: bool,
},
Store {
bytes: u8,
},
LoadLeft,
LoadRight,
StoreLeft,
StoreRight,
LoadLinked,
StoreConditional,
Jump {
link: bool,
},
JumpRegister {
link: bool,
},
Branch {
test: Branch,
likely: bool,
link: bool,
},
Syscall,
Break,
Trap {
test: Trap,
immediate: bool,
},
Sync,
ReadHardware,
FloatArithmetic {
op: FloatOp,
double: bool,
},
FloatToWord {
rounding: Rounding,
double: bool,
},
FloatConvert {
from: FloatFormat,
double: bool,
},
FloatCompare {
condition: u8,
double: bool,
flag: u8,
},
FloatSelect {
test: FloatTest,
double: bool,
},
SelectOnFlag {
set: bool,
flag: u8,
},
FloatBranch {
set: bool,
likely: bool,
flag: u8,
},
FloatTransfer {
to_float: bool,
high: bool,
},
FloatControl {
write: bool,
},
FloatLoad {
double: bool,
},
FloatStore {
double: bool,
},
FloatLoadIndexed {
double: bool,
},
FloatStoreIndexed {
double: bool,
},
FloatMultiplyAdd {
subtract: bool,
negate: bool,
double: bool,
},
Other,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Instruction {
pub op: Op,
pub rs: u8,
pub rt: u8,
pub rd: u8,
pub imm: i64,
pub word: u32,
}
impl Instruction {
pub(crate) fn has_delay_slot(&self) -> bool {
matches!(
self.op,
Op::Jump { .. } | Op::JumpRegister { .. } | Op::Branch { .. } | Op::FloatBranch { .. }
)
}
}
pub fn decode(bytes: &[u8], big: bool) -> Option<Instruction> {
let Some(&word) = bytes.first_chunk::<4>() else {
return None;
};
decode_word(if big {
u32::from_be_bytes(word)
} else {
u32::from_le_bytes(word)
})
}
pub(crate) fn decode_word(word: u32) -> Option<Instruction> {
let rs = (word >> 21 & 31) as u8;
let rt = (word >> 16 & 31) as u8;
let rd = (word >> 11 & 31) as u8;
let amount = word >> 6 & 31;
let function = word & 63;
let signed = i64::from(word as u16 as i16);
let unsigned = i64::from(word as u16);
let make = |op: Op, imm: i64| Instruction {
op,
rs,
rt,
rd,
imm,
word,
};
let branch =
|test, likely, link| make(Op::Branch { test, likely, link }, signed.wrapping_mul(4));
let load = |bytes, signed_load| {
make(
Op::Load {
bytes,
signed: signed_load,
},
signed,
)
};
Some(match word >> 26 {
0 => match function {
0 if rs == 0 => make(Op::Shift(Alu::ShiftLeft), i64::from(amount)),
1 if amount == 0 && rt & 2 == 0 => make(
Op::SelectOnFlag {
set: rt & 1 == 1,
flag: rt >> 2,
},
0,
),
2 if rs == 0 => make(Op::Shift(Alu::ShiftRight), i64::from(amount)),
2 if rs == 1 => make(Op::Shift(Alu::Rotate), i64::from(amount)),
3 if rs == 0 => make(Op::Shift(Alu::ShiftRightArithmetic), i64::from(amount)),
4 if amount == 0 => make(Op::Register(Alu::ShiftLeft), 0),
6 if amount == 0 => make(Op::Register(Alu::ShiftRight), 0),
6 if amount == 1 => make(Op::Register(Alu::Rotate), 0),
7 if amount == 0 => make(Op::Register(Alu::ShiftRightArithmetic), 0),
8 if rt == 0 && rd == 0 && matches!(amount, 0 | 16) => {
make(Op::JumpRegister { link: false }, 0)
}
9 if rt == 0 && (rd != rs || rd == 0) && matches!(amount, 0 | 16) => {
make(Op::JumpRegister { link: rd != 0 }, 0)
}
10 if amount == 0 => make(Op::MoveIfZero, 0),
11 if amount == 0 => make(Op::MoveIfNotZero, 0),
12 => make(Op::Syscall, i64::from(word >> 6 & 0xf_ffff)),
13 => make(Op::Break, i64::from(word >> 6 & 0xf_ffff)),
15 => make(Op::Sync, i64::from(amount)),
16 if rs == 0 && rt == 0 && amount == 0 => make(Op::MoveFromHi, 0),
17 if rt == 0 && rd == 0 && amount == 0 => make(Op::MoveToHi, 0),
18 if rs == 0 && rt == 0 && amount == 0 => make(Op::MoveFromLo, 0),
19 if rt == 0 && rd == 0 && amount == 0 => make(Op::MoveToLo, 0),
24 if rd == 0 && amount == 0 => make(Op::Multiply { signed: true }, 0),
25 if rd == 0 && amount == 0 => make(Op::Multiply { signed: false }, 0),
26 if rd == 0 && amount == 0 => make(Op::Divide { signed: true }, 0),
27 if rd == 0 && amount == 0 => make(Op::Divide { signed: false }, 0),
32 | 33 if amount == 0 => make(Op::Register(Alu::Add), i64::from(function & 1)),
34 | 35 if amount == 0 => make(Op::Register(Alu::Sub), i64::from(function & 1)),
36 if amount == 0 => make(Op::Register(Alu::And), 0),
37 if amount == 0 => make(Op::Register(Alu::Or), 0),
38 if amount == 0 => make(Op::Register(Alu::Xor), 0),
39 if amount == 0 => make(Op::Register(Alu::Nor), 0),
42 if amount == 0 => make(Op::Register(Alu::Less), 0),
43 if amount == 0 => make(Op::Register(Alu::LessUnsigned), 0),
48..=52 | 54 => {
let test = match function {
48 => Trap::GreaterEqual,
49 => Trap::GreaterEqualUnsigned,
50 => Trap::Less,
51 => Trap::LessUnsigned,
52 => Trap::Equal,
_ => Trap::NotEqual,
};
make(
Op::Trap {
test,
immediate: false,
},
i64::from(word >> 6 & 0x3ff),
)
}
_ => return None,
},
1 => match rt {
0 => branch(Branch::LessZero, false, false),
1 => branch(Branch::GreaterEqualZero, false, false),
2 => branch(Branch::LessZero, true, false),
3 => branch(Branch::GreaterEqualZero, true, false),
16 => branch(Branch::LessZero, false, true),
17 => branch(Branch::GreaterEqualZero, false, true),
18 => branch(Branch::LessZero, true, true),
19 => branch(Branch::GreaterEqualZero, true, true),
31 => make(Op::Other, 0),
8..=12 | 14 => {
let test = match rt {
8 => Trap::GreaterEqual,
9 => Trap::GreaterEqualUnsigned,
10 => Trap::Less,
11 => Trap::LessUnsigned,
12 => Trap::Equal,
_ => Trap::NotEqual,
};
make(
Op::Trap {
test,
immediate: true,
},
signed,
)
}
_ => return None,
},
2 | 3 => make(
Op::Jump {
link: word >> 26 == 3,
},
i64::from(word & 0x03ff_ffff) << 2,
),
4 => branch(Branch::Equal, false, false),
5 => branch(Branch::NotEqual, false, false),
6 if rt == 0 => branch(Branch::LessEqualZero, false, false),
7 if rt == 0 => branch(Branch::GreaterZero, false, false),
20 => branch(Branch::Equal, true, false),
21 => branch(Branch::NotEqual, true, false),
22 if rt == 0 => branch(Branch::LessEqualZero, true, false),
23 if rt == 0 => branch(Branch::GreaterZero, true, false),
8 | 9 => make(Op::Immediate(Alu::Add), signed),
10 => make(Op::Immediate(Alu::Less), signed),
11 => make(Op::Immediate(Alu::LessUnsigned), signed),
12 => make(Op::Immediate(Alu::And), unsigned),
13 => make(Op::Immediate(Alu::Or), unsigned),
14 => make(Op::Immediate(Alu::Xor), unsigned),
15 if rs == 0 => make(Op::Lui, unsigned << 16),
28 => match function {
0 if rd == 0 && amount == 0 => make(
Op::MultiplyAdd {
signed: true,
subtract: false,
},
0,
),
1 if rd == 0 && amount == 0 => make(
Op::MultiplyAdd {
signed: false,
subtract: false,
},
0,
),
2 if amount == 0 => make(Op::MultiplyLow, 0),
4 if rd == 0 && amount == 0 => make(
Op::MultiplyAdd {
signed: true,
subtract: true,
},
0,
),
5 if rd == 0 && amount == 0 => make(
Op::MultiplyAdd {
signed: false,
subtract: true,
},
0,
),
32 if amount == 0 && rt == rd => make(Op::CountLeadingZeros, 0),
33 if amount == 0 && rt == rd => make(Op::CountLeadingOnes, 0),
_ => return None,
},
31 => match function {
0 => make(Op::Extract, i64::from(amount)),
4 if u32::from(rd) >= amount => make(Op::Insert, i64::from(amount)),
32 if rs == 0 && amount == 2 => make(Op::SwapBytesInHalves, 0),
32 if rs == 0 && amount == 16 => make(Op::SignExtendByte, 0),
32 if rs == 0 && amount == 24 => make(Op::SignExtendHalf, 0),
59 if rs == 0 && amount == 0 && rd == 29 => make(Op::ReadHardware, 0),
_ => return None,
},
32 => load(1, true),
33 => load(2, true),
34 => make(Op::LoadLeft, signed),
35 => load(4, false),
36 => load(1, false),
37 => load(2, false),
38 => make(Op::LoadRight, signed),
40 => make(Op::Store { bytes: 1 }, signed),
41 => make(Op::Store { bytes: 2 }, signed),
42 => make(Op::StoreLeft, signed),
43 => make(Op::Store { bytes: 4 }, signed),
46 => make(Op::StoreRight, signed),
48 => make(Op::LoadLinked, signed),
56 => make(Op::StoreConditional, signed),
17 => return float(word, &make, signed),
19 => {
let double = function & 1 == 1;
let op = match function {
0 | 1 if rd == 0 => Op::FloatLoadIndexed { double },
8 | 9 if amount == 0 => Op::FloatStoreIndexed { double },
32 | 33 | 40 | 41 | 48 | 49 | 56 | 57 => Op::FloatMultiplyAdd {
subtract: function & 8 != 0,
negate: function & 16 != 0,
double,
},
_ => Op::Other,
};
make(op, i64::from(amount))
}
49 => make(Op::FloatLoad { double: false }, signed),
53 => make(Op::FloatLoad { double: true }, signed),
57 => make(Op::FloatStore { double: false }, signed),
61 => make(Op::FloatStore { double: true }, signed),
16 | 18 | 47 | 50 | 51 | 54 | 55 | 58 | 59 | 62 | 63 => make(Op::Other, 0),
_ => return None,
})
}
fn float(word: u32, make: &impl Fn(Op, i64) -> Instruction, signed: i64) -> Option<Instruction> {
let format = word >> 21 & 31;
let ft = (word >> 16 & 31) as u8;
let fd = i64::from(word >> 6 & 31);
let function = word & 63;
let low = word & 0x7ff;
let double = format == 17;
Some(match format {
0 | 3 | 4 | 7 if low == 0 => make(
Op::FloatTransfer {
to_float: format & 4 != 0,
high: format & 3 == 3,
},
0,
),
2 | 6 if low == 0 => make(Op::FloatControl { write: format == 6 }, 0),
8 => make(
Op::FloatBranch {
set: ft & 1 == 1,
likely: ft & 2 != 0,
flag: ft >> 2,
},
signed.wrapping_mul(4),
),
16 | 17 => {
let arithmetic = |op| make(Op::FloatArithmetic { op, double }, fd);
let to_word = |rounding| make(Op::FloatToWord { rounding, double }, fd);
match function {
0 => arithmetic(FloatOp::Add),
1 => arithmetic(FloatOp::Sub),
2 => arithmetic(FloatOp::Mul),
3 => arithmetic(FloatOp::Div),
4 if ft == 0 => arithmetic(FloatOp::Sqrt),
5 if ft == 0 => arithmetic(FloatOp::Abs),
6 if ft == 0 => arithmetic(FloatOp::Move),
7 if ft == 0 => arithmetic(FloatOp::Negate),
12 if ft == 0 => to_word(Rounding::Nearest),
13 if ft == 0 => to_word(Rounding::Zero),
14 if ft == 0 => to_word(Rounding::Up),
15 if ft == 0 => to_word(Rounding::Down),
36 if ft == 0 => to_word(Rounding::Current),
17 if ft & 2 == 0 => make(
Op::FloatSelect {
test: FloatTest::Flag {
set: ft & 1 == 1,
flag: ft >> 2,
},
double,
},
fd,
),
18 => make(
Op::FloatSelect {
test: FloatTest::Zero,
double,
},
fd,
),
19 => make(
Op::FloatSelect {
test: FloatTest::NotZero,
double,
},
fd,
),
32 if ft == 0 && double => make(
Op::FloatConvert {
from: FloatFormat::Double,
double: false,
},
fd,
),
33 if ft == 0 && !double => make(
Op::FloatConvert {
from: FloatFormat::Single,
double: true,
},
fd,
),
48..=63 if fd & 3 == 0 => make(
Op::FloatCompare {
condition: (function & 15) as u8,
double,
flag: (fd >> 2) as u8,
},
0,
),
_ => return None,
}
}
20 if ft == 0 && matches!(function, 32 | 33) => make(
Op::FloatConvert {
from: FloatFormat::Word,
double: function == 33,
},
fd,
),
_ => return None,
})
}