use super::decode::{
Alu, Branch, FloatFormat, FloatOp, FloatTest, Instruction, Op, Rounding, Trap,
};
const NAMES: [&str; 32] = [
"zero", "at", "v0", "v1", "a0", "a1", "a2", "a3", "t0", "t1", "t2", "t3", "t4", "t5", "t6",
"t7", "s0", "s1", "s2", "s3", "s4", "s5", "s6", "s7", "t8", "t9", "k0", "k1", "gp", "sp", "fp",
"ra",
];
pub(crate) fn register_name(register: u8) -> &'static str {
NAMES[usize::from(register & 31)]
}
fn precision(double: bool) -> char {
if double { 'd' } else { 's' }
}
fn f(register: i64) -> String {
format!("$f{}", register & 31)
}
fn hex(value: i64) -> String {
if value < 0 {
format!("-0x{:x}", value.unsigned_abs())
} else {
format!("0x{value:x}")
}
}
pub fn target(instruction: &Instruction, pc: u64) -> Option<u64> {
let slot = pc.wrapping_add(4);
match instruction.op {
Op::Branch { .. } | Op::FloatBranch { .. } => {
Some(slot.wrapping_add(instruction.imm as u64) & 0xffff_ffff)
}
Op::Jump { .. } => Some(slot & 0xf000_0000 | instruction.imm as u64),
_ => None,
}
}
pub fn text(instruction: &Instruction, pc: u64) -> String {
let Instruction {
op,
rs,
rt,
rd,
imm,
word,
} = *instruction;
let r = |register: u8| format!("${}", register_name(register));
let destination = target(instruction, pc).unwrap_or(0);
let trapping = word & 1 == 0;
match op {
Op::Register(alu) => {
let name = match alu {
Alu::Add if trapping => "add",
Alu::Add => "addu",
Alu::Sub if trapping => "sub",
Alu::Sub => "subu",
Alu::And => "and",
Alu::Or => "or",
Alu::Xor => "xor",
Alu::Nor => "nor",
Alu::Less => "slt",
Alu::LessUnsigned => "sltu",
Alu::ShiftLeft => "sllv",
Alu::ShiftRight => "srlv",
Alu::ShiftRightArithmetic => "srav",
Alu::Rotate => "rotrv",
};
match alu {
Alu::ShiftLeft | Alu::ShiftRight | Alu::ShiftRightArithmetic | Alu::Rotate => {
format!("{name} {}, {}, {}", r(rd), r(rt), r(rs))
}
Alu::Or | Alu::Add if rt == 0 && !(alu == Alu::Add && trapping) => {
format!("move {}, {}", r(rd), r(rs))
}
Alu::Sub if rs == 0 => format!(
"{} {}, {}",
if trapping { "neg" } else { "negu" },
r(rd),
r(rt)
),
Alu::Nor if rt == 0 => format!("not {}, {}", r(rd), r(rs)),
_ => format!("{name} {}, {}, {}", r(rd), r(rs), r(rt)),
}
}
Op::Immediate(alu) => {
let name = match alu {
Alu::Add if word >> 26 == 8 => "addi",
Alu::Add => "addiu",
Alu::Less => "slti",
Alu::LessUnsigned => "sltiu",
Alu::And => "andi",
Alu::Or => "ori",
_ => "xori",
};
format!("{name} {}, {}, {}", r(rt), r(rs), hex(imm))
}
Op::Shift(alu) => {
let name = match alu {
Alu::ShiftLeft => "sll",
Alu::ShiftRight => "srl",
Alu::Rotate => "rotr",
_ => "sra",
};
match (alu, rd, rt, imm) {
(Alu::ShiftLeft, 0, 0, 0) => "nop".to_owned(),
(Alu::ShiftLeft, 0, 0, 1) => "ssnop".to_owned(),
(Alu::ShiftLeft, 0, 0, 3) => "ehb".to_owned(),
(Alu::ShiftLeft, 0, 0, 5) => "pause".to_owned(),
_ => format!("{name} {}, {}, {}", r(rd), r(rt), hex(imm)),
}
}
Op::Lui => format!("lui {}, {}", r(rt), hex(imm >> 16)),
Op::Multiply { signed } => format!(
"{} {}, {}",
if signed { "mult" } else { "multu" },
r(rs),
r(rt)
),
Op::MultiplyAdd { signed, subtract } => format!(
"{}{} {}, {}",
if subtract { "msub" } else { "madd" },
if signed { "" } else { "u" },
r(rs),
r(rt)
),
Op::Divide { signed } => format!(
"{} $zero, {}, {}",
if signed { "div" } else { "divu" },
r(rs),
r(rt)
),
Op::MultiplyLow => format!("mul {}, {}, {}", r(rd), r(rs), r(rt)),
Op::MoveFromHi => format!("mfhi {}", r(rd)),
Op::MoveFromLo => format!("mflo {}", r(rd)),
Op::MoveToHi => format!("mthi {}", r(rs)),
Op::MoveToLo => format!("mtlo {}", r(rs)),
Op::MoveIfZero => format!("movz {}, {}, {}", r(rd), r(rs), r(rt)),
Op::MoveIfNotZero => format!("movn {}, {}, {}", r(rd), r(rs), r(rt)),
Op::CountLeadingZeros => format!("clz {}, {}", r(rd), r(rs)),
Op::CountLeadingOnes => format!("clo {}, {}", r(rd), r(rs)),
Op::Extract => format!(
"ext {}, {}, {}, {}",
r(rt),
r(rs),
hex(imm),
hex(i64::from(rd) + 1)
),
Op::Insert => format!(
"ins {}, {}, {}, {}",
r(rt),
r(rs),
hex(imm),
hex(i64::from(rd) + 1 - imm)
),
Op::SignExtendByte => format!("seb {}, {}", r(rd), r(rt)),
Op::SignExtendHalf => format!("seh {}, {}", r(rd), r(rt)),
Op::SwapBytesInHalves => format!("wsbh {}, {}", r(rd), r(rt)),
Op::Load { bytes, signed } => {
let name = match (bytes, signed) {
(1, true) => "lb",
(1, false) => "lbu",
(2, true) => "lh",
(2, false) => "lhu",
_ => "lw",
};
format!("{name} {}, {}({})", r(rt), hex(imm), r(rs))
}
Op::Store { bytes } => {
let name = match bytes {
1 => "sb",
2 => "sh",
_ => "sw",
};
format!("{name} {}, {}({})", r(rt), hex(imm), r(rs))
}
Op::LoadLeft => format!("lwl {}, {}({})", r(rt), hex(imm), r(rs)),
Op::LoadRight => format!("lwr {}, {}({})", r(rt), hex(imm), r(rs)),
Op::StoreLeft => format!("swl {}, {}({})", r(rt), hex(imm), r(rs)),
Op::StoreRight => format!("swr {}, {}({})", r(rt), hex(imm), r(rs)),
Op::LoadLinked => format!("ll {}, {}({})", r(rt), hex(imm), r(rs)),
Op::StoreConditional => format!("sc {}, {}({})", r(rt), hex(imm), r(rs)),
Op::Jump { link } => format!("{} 0x{destination:x}", if link { "jal" } else { "j" }),
Op::JumpRegister { link } => {
let hazard = if word & 0x400 == 0 { "" } else { ".hb" };
match (link, rd) {
(false, _) => format!("jr{hazard} {}", r(rs)),
(true, 31) => format!("jalr{hazard} {}", r(rs)),
(true, _) => format!("jalr{hazard} {}, {}", r(rd), r(rs)),
}
}
Op::Branch { test, likely, link } => {
let l = if likely { "l" } else { "" };
match test {
Branch::Equal if rs == 0 && rt == 0 && !likely => format!("b 0x{destination:x}"),
Branch::Equal if rt == 0 => format!("beqz{l} {}, 0x{destination:x}", r(rs)),
Branch::NotEqual if rt == 0 => format!("bnez{l} {}, 0x{destination:x}", r(rs)),
Branch::Equal => format!("beq{l} {}, {}, 0x{destination:x}", r(rs), r(rt)),
Branch::NotEqual => format!("bne{l} {}, {}, 0x{destination:x}", r(rs), r(rt)),
Branch::GreaterEqualZero if link && rs == 0 && !likely => {
format!("bal 0x{destination:x}")
}
_ => {
let name = match test {
Branch::LessEqualZero => "blez",
Branch::GreaterZero => "bgtz",
Branch::LessZero => "bltz",
_ => "bgez",
};
format!(
"{name}{}{l} {}, 0x{destination:x}",
if link { "al" } else { "" },
r(rs)
)
}
}
}
Op::Syscall if imm == 0 => "syscall".to_owned(),
Op::Syscall => format!("syscall {}", hex(imm)),
Op::Break => match (imm >> 10, imm & 0x3ff) {
(0, 0) => "break".to_owned(),
(first, 0) => format!("break {}", hex(first)),
(first, second) => format!("break {}, {}", hex(first), hex(second)),
},
Op::Trap { test, immediate } => {
let name = match test {
Trap::GreaterEqual => "tge",
Trap::GreaterEqualUnsigned => "tgeu",
Trap::Less => "tlt",
Trap::LessUnsigned => "tltu",
Trap::Equal => "teq",
Trap::NotEqual => "tne",
};
if immediate {
let name = match test {
Trap::GreaterEqualUnsigned => "tgeiu".to_owned(),
Trap::LessUnsigned => "tltiu".to_owned(),
_ => format!("{name}i"),
};
format!("{name} {}, {}", r(rs), hex(imm))
} else if imm == 0 {
format!("{name} {}, {}", r(rs), r(rt))
} else {
format!("{name} {}, {}, {}", r(rs), r(rt), hex(imm))
}
}
Op::Sync if imm == 0 => "sync".to_owned(),
Op::Sync => format!("sync {}", hex(imm)),
Op::ReadHardware => format!("rdhwr {}, ${}", r(rt), rd),
Op::FloatArithmetic { op, double } => {
let name = match op {
FloatOp::Add => "add",
FloatOp::Sub => "sub",
FloatOp::Mul => "mul",
FloatOp::Div => "div",
FloatOp::Sqrt => "sqrt",
FloatOp::Abs => "abs",
FloatOp::Move => "mov",
FloatOp::Negate => "neg",
};
let name = format!("{name}.{}", precision(double));
match op {
FloatOp::Add | FloatOp::Sub | FloatOp::Mul | FloatOp::Div => {
format!("{name} {}, {}, {}", f(imm), f(rd.into()), f(rt.into()))
}
_ => format!("{name} {}, {}", f(imm), f(rd.into())),
}
}
Op::FloatToWord { rounding, double } => {
let name = match rounding {
Rounding::Nearest => "round",
Rounding::Zero => "trunc",
Rounding::Up => "ceil",
Rounding::Down => "floor",
Rounding::Current => "cvt",
};
format!(
"{name}.w.{} {}, {}",
precision(double),
f(imm),
f(rd.into())
)
}
Op::FloatConvert { from, double } => {
let from = match from {
FloatFormat::Single => "s",
FloatFormat::Double => "d",
FloatFormat::Word => "w",
};
format!(
"cvt.{}.{from} {}, {}",
precision(double),
f(imm),
f(rd.into())
)
}
Op::FloatCompare {
condition,
double,
flag,
} => {
const CONDITIONS: [&str; 16] = [
"f", "un", "eq", "ueq", "olt", "ult", "ole", "ule", "sf", "ngle", "seq", "ngl",
"lt", "nge", "le", "ngt",
];
let name = format!(
"c.{}.{}",
CONDITIONS[usize::from(condition & 15)],
precision(double)
);
if flag == 0 {
format!("{name} {}, {}", f(rd.into()), f(rt.into()))
} else {
format!("{name} $fcc{flag}, {}, {}", f(rd.into()), f(rt.into()))
}
}
Op::FloatSelect { test, double } => {
let (name, last) = match test {
FloatTest::Zero => ("movz", r(rt)),
FloatTest::NotZero => ("movn", r(rt)),
FloatTest::Flag { set, flag } => {
(if set { "movt" } else { "movf" }, format!("$fcc{flag}"))
}
};
format!(
"{name}.{} {}, {}, {last}",
precision(double),
f(imm),
f(rd.into())
)
}
Op::SelectOnFlag { set, flag } => format!(
"{} {}, {}, $fcc{flag}",
if set { "movt" } else { "movf" },
r(rd),
r(rs)
),
Op::FloatBranch { set, likely, flag } => {
let name = format!(
"bc1{}{}",
if set { "t" } else { "f" },
if likely { "l" } else { "" }
);
if flag == 0 {
format!("{name} 0x{destination:x}")
} else {
format!("{name} $fcc{flag}, 0x{destination:x}")
}
}
Op::FloatTransfer { to_float, high } => format!(
"m{}{}c1 {}, {}",
if to_float { "t" } else { "f" },
if high { "h" } else { "" },
r(rt),
f(rd.into())
),
Op::FloatControl { write } => {
format!("c{}c1 {}, ${rd}", if write { "t" } else { "f" }, r(rt))
}
Op::FloatLoad { double } => format!(
"l{}c1 {}, {}({})",
if double { "d" } else { "w" },
f(rt.into()),
hex(imm),
r(rs)
),
Op::FloatStore { double } => format!(
"s{}c1 {}, {}({})",
if double { "d" } else { "w" },
f(rt.into()),
hex(imm),
r(rs)
),
Op::FloatLoadIndexed { double } => format!(
"l{}xc1 {}, {}({})",
if double { "d" } else { "w" },
f(imm),
r(rt),
r(rs)
),
Op::FloatStoreIndexed { double } => format!(
"s{}xc1 {}, {}({})",
if double { "d" } else { "w" },
f(rd.into()),
r(rt),
r(rs)
),
Op::FloatMultiplyAdd {
subtract,
negate,
double,
} => format!(
"{}m{}.{} {}, {}, {}, {}",
if negate { "n" } else { "" },
if subtract { "sub" } else { "add" },
precision(double),
f(imm),
f(rs.into()),
f(rd.into()),
f(rt.into())
),
Op::Other => format!(".word 0x{word:08x}"),
}
}