use super::decode::{
Alu, Atomic, Branch, Csr, FloatBinary, FloatCompare, Fused, Instruction, Op, Precision, Width,
};
const INTEGER: [&str; 32] = [
"zero", "ra", "sp", "gp", "tp", "t0", "t1", "t2", "s0", "s1", "a0", "a1", "a2", "a3", "a4",
"a5", "a6", "a7", "s2", "s3", "s4", "s5", "s6", "s7", "s8", "s9", "s10", "s11", "t3", "t4",
"t5", "t6",
];
pub(crate) fn integer_name(register: u8) -> &'static str {
INTEGER[usize::from(register & 31)]
}
pub(crate) fn float_name(register: u8) -> &'static str {
vlk_arch::arch::riscv64::FLOAT_NAMES[usize::from(register & 31)]
}
fn hex(value: i64) -> String {
if value < 0 {
format!("-0x{:x}", value.unsigned_abs())
} else {
format!("0x{value:x}")
}
}
fn width_suffix(width: Width) -> &'static str {
match width {
Width::Byte => "b",
Width::Half => "h",
Width::Word => "w",
Width::Double => "d",
}
}
fn precision_suffix(precision: Precision) -> &'static str {
match precision {
Precision::Single => "s",
Precision::Double => "d",
}
}
fn alu_name(op: Alu) -> &'static str {
match op {
Alu::Add => "add",
Alu::Sub => "sub",
Alu::ShiftLeft => "sll",
Alu::Less => "slt",
Alu::LessUnsigned => "sltu",
Alu::Xor => "xor",
Alu::ShiftRight => "srl",
Alu::ShiftRightArithmetic => "sra",
Alu::Or => "or",
Alu::And => "and",
Alu::Multiply => "mul",
Alu::MultiplyHigh => "mulh",
Alu::MultiplyHighSignedUnsigned => "mulhsu",
Alu::MultiplyHighUnsigned => "mulhu",
Alu::Divide => "div",
Alu::DivideUnsigned => "divu",
Alu::Remainder => "rem",
Alu::RemainderUnsigned => "remu",
}
}
fn rounding(mode: u8) -> &'static str {
match mode {
0 => ", rne",
1 => ", rtz",
2 => ", rdn",
3 => ", rup",
4 => ", rmm",
_ => "",
}
}
fn exact_rounding(mode: u8) -> &'static str {
match mode {
0 => "",
7 => ", dyn",
other => rounding(other),
}
}
fn ordering(bits: u8) -> &'static str {
match bits & 3 {
0 => "",
1 => ".rl",
2 => ".aq",
_ => ".aqrl",
}
}
fn csr_name(number: i64) -> String {
match number {
1 => "fflags".to_owned(),
2 => "frm".to_owned(),
3 => "fcsr".to_owned(),
0xc00 => "cycle".to_owned(),
0xc01 => "time".to_owned(),
0xc02 => "instret".to_owned(),
other => format!("0x{other:x}"),
}
}
pub fn text(instruction: &Instruction, pc: u64) -> String {
let Instruction {
op,
rd,
rs1,
rs2,
rs3,
imm,
rounding: mode,
..
} = *instruction;
let (x, f) = (integer_name, float_name);
let destination = pc.wrapping_add(imm as u64);
match op {
Op::Lui => format!("lui {}, 0x{:x}", x(rd), (imm >> 12) & 0xf_ffff),
Op::Auipc => format!("auipc {}, 0x{:x}", x(rd), (imm >> 12) & 0xf_ffff),
Op::Jal => match rd {
0 => format!("j 0x{destination:x}"),
1 => format!("jal 0x{destination:x}"),
_ => format!("jal {}, 0x{destination:x}", x(rd)),
},
Op::Jalr => match (rd, rs1, imm) {
(0, 1, 0) => "ret".to_owned(),
(0, _, 0) => format!("jr {}", x(rs1)),
(1, _, 0) => format!("jalr {}", x(rs1)),
(0, _, _) => format!("jr {}({})", hex(imm), x(rs1)),
(1, _, _) => format!("jalr {}({})", hex(imm), x(rs1)),
(_, _, 0) => format!("jalr {}, {}", x(rd), x(rs1)),
_ => format!("jalr {}, {}({})", x(rd), hex(imm), x(rs1)),
},
Op::Branch(branch) => {
let (name, zero_second, zero_first) = match branch {
Branch::Equal => ("beq", Some("beqz"), None),
Branch::NotEqual => ("bne", Some("bnez"), None),
Branch::Less => ("blt", Some("bltz"), Some("bgtz")),
Branch::GreaterEqual => ("bge", Some("bgez"), Some("blez")),
Branch::LessUnsigned => ("bltu", None, None),
Branch::GreaterEqualUnsigned => ("bgeu", None, None),
};
match (zero_second, zero_first) {
(Some(short), _) if rs2 == 0 => format!("{short} {}, 0x{destination:x}", x(rs1)),
(_, Some(short)) if rs1 == 0 => format!("{short} {}, 0x{destination:x}", x(rs2)),
_ => format!("{name} {}, {}, 0x{destination:x}", x(rs1), x(rs2)),
}
}
Op::Load { width, signed } => {
let unsigned = if signed || width == Width::Double {
""
} else {
"u"
};
format!(
"l{}{unsigned} {}, {}({})",
width_suffix(width),
x(rd),
hex(imm),
x(rs1)
)
}
Op::Store(width) => format!(
"s{} {}, {}({})",
width_suffix(width),
x(rs2),
hex(imm),
x(rs1)
),
Op::Immediate { op, word } => match (op, word, rd, rs1, imm) {
(Alu::Add, false, 0, 0, 0) => "nop".to_owned(),
(Alu::Add, false, _, 0, _) => format!("li {}, {}", x(rd), hex(imm)),
(Alu::Add, false, _, _, 0) => format!("mv {}, {}", x(rd), x(rs1)),
(Alu::Add, true, _, _, 0) => format!("sext.w {}, {}", x(rd), x(rs1)),
(Alu::Xor, false, _, _, -1) => format!("not {}, {}", x(rd), x(rs1)),
(Alu::LessUnsigned, false, _, _, 1) => format!("seqz {}, {}", x(rd), x(rs1)),
(Alu::And, false, _, _, 0xff) => format!("zext.b {}, {}", x(rd), x(rs1)),
_ => format!(
"{}{} {}, {}, {}",
match op {
Alu::LessUnsigned => "sltiu".to_owned(),
other => format!("{}i", alu_name(other)),
},
if word { "w" } else { "" },
x(rd),
x(rs1),
hex(imm)
),
},
Op::Register { op, word } => match (op, word, rs1, rs2) {
(Alu::Sub, _, 0, _) => {
format!("neg{} {}, {}", if word { "w" } else { "" }, x(rd), x(rs2))
}
(Alu::LessUnsigned, false, 0, _) => format!("snez {}, {}", x(rd), x(rs2)),
(Alu::Less, false, _, 0) => format!("sltz {}, {}", x(rd), x(rs1)),
(Alu::Less, false, 0, _) => format!("sgtz {}, {}", x(rd), x(rs2)),
_ => format!(
"{}{} {}, {}, {}",
alu_name(op),
if word { "w" } else { "" },
x(rd),
x(rs1),
x(rs2)
),
},
Op::Fence => {
let set = |bits: i64| {
let letters: String = [(8, 'i'), (4, 'o'), (2, 'r'), (1, 'w')]
.into_iter()
.filter(|(bit, _)| bits & bit != 0)
.map(|(_, letter)| letter)
.collect();
if letters.is_empty() {
"0".to_owned()
} else {
letters
}
};
match imm {
0xff => "fence".to_owned(),
0x833 => "fence.tso".to_owned(),
0x10 => "pause".to_owned(),
_ => format!("fence {}, {}", set(imm >> 4 & 15), set(imm & 15)),
}
}
Op::FenceInstructions => "fence.i".to_owned(),
Op::Ecall => "ecall".to_owned(),
Op::Ebreak => "ebreak".to_owned(),
Op::Csr { op, immediate } => {
let name = csr_name(imm);
let base = match op {
Csr::Write => "csrrw",
Csr::Set => "csrrs",
Csr::Clear => "csrrc",
};
let short = match op {
Csr::Write => "csrw",
Csr::Set => "csrs",
Csr::Clear => "csrc",
};
let operand = if immediate {
hex(i64::from(rs1))
} else {
x(rs1).to_owned()
};
let suffix = if immediate { "i" } else { "" };
if op == Csr::Set && !immediate && rs1 == 0 {
format!("csrr {}, {name}", x(rd))
} else if rd == 0 {
format!("{short}{suffix} {name}, {operand}")
} else {
format!("{base}{suffix} {}, {name}, {operand}", x(rd))
}
}
Op::LoadReserved(width) => format!(
"lr.{}{} {}, ({})",
width_suffix(width),
ordering(mode),
x(rd),
x(rs1)
),
Op::StoreConditional(width) => format!(
"sc.{}{} {}, {}, ({})",
width_suffix(width),
ordering(mode),
x(rd),
x(rs2),
x(rs1)
),
Op::Atomic { op, width } => {
let name = match op {
Atomic::Swap => "amoswap",
Atomic::Add => "amoadd",
Atomic::Xor => "amoxor",
Atomic::And => "amoand",
Atomic::Or => "amoor",
Atomic::Min => "amomin",
Atomic::Max => "amomax",
Atomic::MinUnsigned => "amominu",
Atomic::MaxUnsigned => "amomaxu",
};
format!(
"{name}.{}{} {}, {}, ({})",
width_suffix(width),
ordering(mode),
x(rd),
x(rs2),
x(rs1)
)
}
Op::FloatLoad(precision) => format!(
"fl{} {}, {}({})",
if precision == Precision::Single {
"w"
} else {
"d"
},
f(rd),
hex(imm),
x(rs1)
),
Op::FloatStore(precision) => format!(
"fs{} {}, {}({})",
if precision == Precision::Single {
"w"
} else {
"d"
},
f(rs2),
hex(imm),
x(rs1)
),
Op::Fused { op, precision } => {
let name = match op {
Fused::MultiplyAdd => "fmadd",
Fused::MultiplySub => "fmsub",
Fused::NegatedMultiplySub => "fnmsub",
Fused::NegatedMultiplyAdd => "fnmadd",
};
format!(
"{name}.{} {}, {}, {}, {}{}",
precision_suffix(precision),
f(rd),
f(rs1),
f(rs2),
f(rs3),
rounding(mode)
)
}
Op::FloatBinary { op, precision } => {
let suffix = precision_suffix(precision);
let (name, rounded) = match op {
FloatBinary::Add => ("fadd", true),
FloatBinary::Sub => ("fsub", true),
FloatBinary::Multiply => ("fmul", true),
FloatBinary::Divide => ("fdiv", true),
FloatBinary::Min => ("fmin", false),
FloatBinary::Max => ("fmax", false),
FloatBinary::Sign => ("fsgnj", false),
FloatBinary::SignNegated => ("fsgnjn", false),
FloatBinary::SignXor => ("fsgnjx", false),
};
let short = match op {
FloatBinary::Sign => Some("fmv"),
FloatBinary::SignNegated => Some("fneg"),
FloatBinary::SignXor => Some("fabs"),
_ => None,
};
match short {
Some(short) if rs1 == rs2 => format!("{short}.{suffix} {}, {}", f(rd), f(rs1)),
_ => format!(
"{name}.{suffix} {}, {}, {}{}",
f(rd),
f(rs1),
f(rs2),
if rounded { rounding(mode) } else { "" }
),
}
}
Op::FloatSqrt(precision) => format!(
"fsqrt.{} {}, {}{}",
precision_suffix(precision),
f(rd),
f(rs1),
rounding(mode)
),
Op::FloatCompare { op, precision } => {
let name = match op {
FloatCompare::Equal => "feq",
FloatCompare::Less => "flt",
FloatCompare::LessEqual => "fle",
};
format!(
"{name}.{} {}, {}, {}",
precision_suffix(precision),
x(rd),
f(rs1),
f(rs2)
)
}
Op::FloatConvert { to } => match to {
Precision::Single => format!("fcvt.s.d {}, {}{}", f(rd), f(rs1), rounding(mode)),
Precision::Double => {
format!("fcvt.d.s {}, {}{}", f(rd), f(rs1), exact_rounding(mode))
}
},
Op::FloatToInt { from, word, signed } => format!(
"fcvt.{}{}.{} {}, {}{}",
if word { "w" } else { "l" },
if signed { "" } else { "u" },
precision_suffix(from),
x(rd),
f(rs1),
rounding(mode)
),
Op::IntToFloat { to, word, signed } => {
let exact = to == Precision::Double && word;
format!(
"fcvt.{}.{}{} {}, {}{}",
precision_suffix(to),
if word { "w" } else { "l" },
if signed { "" } else { "u" },
f(rd),
x(rs1),
if exact {
exact_rounding(mode)
} else {
rounding(mode)
}
)
}
Op::FloatBits(precision) => format!(
"fmv.x.{} {}, {}",
if precision == Precision::Single {
"w"
} else {
"d"
},
x(rd),
f(rs1)
),
Op::BitsToFloat(precision) => format!(
"fmv.{}.x {}, {}",
if precision == Precision::Single {
"w"
} else {
"d"
},
f(rd),
x(rs1)
),
Op::FloatClass(precision) => format!(
"fclass.{} {}, {}",
precision_suffix(precision),
x(rd),
f(rs1)
),
}
}