diff --git a/objdiff-core/src/arch/superh/disasm.rs b/objdiff-core/src/arch/superh/disasm.rs index 762ada60..59427c47 100644 --- a/objdiff-core/src/arch/superh/disasm.rs +++ b/objdiff-core/src/arch/superh/disasm.rs @@ -7,6 +7,20 @@ static REG_NAMES: [&str; 16] = [ "r15", ]; +static FR_NAMES: [&str; 16] = [ + "fr0", "fr1", "fr2", "fr3", "fr4", "fr5", "fr6", "fr7", "fr8", "fr9", "fr10", "fr11", "fr12", + "fr13", "fr14", "fr15", +]; + +/// Double-precision registers, by the top three bits of a four-bit register field. +static DR_NAMES: [&str; 8] = ["dr0", "dr2", "dr4", "dr6", "dr8", "dr10", "dr12", "dr14"]; + +/// Vector registers, by a two-bit register field. +static FV_NAMES: [&str; 4] = ["fv0", "fv4", "fv8", "fv12"]; + +static BANK_REG_NAMES: [&str; 8] = + ["r0_bank", "r1_bank", "r2_bank", "r3_bank", "r4_bank", "r5_bank", "r6_bank", "r7_bank"]; + enum Ops { // match_ni_f AddImmRn, @@ -167,6 +181,141 @@ enum Ops { Rts, Sett, Sleep, + + // MOVA, an SH-1 instruction + MovaAtDispPcR0, + + // SH-3 and SH-4 additions + Ldtlb, + Clrs, + Sets, + PrefAtRn, + OcbiAtRn, + OcbpAtRn, + OcbwbAtRn, + MovcaLR0AtRn, + ShadRmRn, + ShldRmRn, + LdcRmSsr, + LdcRmSpc, + LdcRmSgr, + LdcRmDbr, + LdcRmRnBank, + LdcLAtRmIncSsr, + LdcLAtRmIncSpc, + LdcLAtRmIncSgr, + LdcLAtRmIncDbr, + LdcLAtRmIncRnBank, + StcSsrRn, + StcSpcRn, + StcSgrRn, + StcDbrRn, + StcRmBankRn, + StcLSsrAtDecrementRn, + StcLSpcAtDecrementRn, + StcLSgrAtDecrementRn, + StcLDbrAtDecrementRn, + StcLRmBankAtDecrementRn, + LdsRmFpscr, + LdsRmFpul, + LdsLAtRmIncFpscr, + LdsLAtRmIncFpul, + StsFpscrRn, + StsFpulRn, + StsLFpscrAtDecrementRn, + StsLFpulAtDecrementRn, + + // SH-4 floating-point unit + FaddFrmFrn, + FsubFrmFrn, + FmulFrmFrn, + FdivFrmFrn, + FcmpEqFrmFrn, + FcmpGtFrmFrn, + FmacFr0FrmFrn, + FmovFrmFrn, + FmovAtRmFrn, + FmovAtRmIncFrn, + FmovAtR0RmFrn, + FmovFrmAtRn, + FmovFrmAtDecrementRn, + FmovFrmAtR0Rn, + FstsFpulFrn, + FldsFrmFpul, + FloatFpulFrn, + FtrcFrmFpul, + FnegFrn, + FabsFrn, + FsqrtFrn, + FsrraFrn, + Fldi0Frn, + Fldi1Frn, + FcnvsdFpulDrn, + FcnvdsDrmFpul, + FiprFvmFvn, + FscaFpulDrn, + FtrvXmtrxFvn, + Fschg, + Frchg, +} + +/// The displacement shown for mov.l @(disp,PC),Rn and mova @(disp,PC),R0, whose longword is at +/// (PC & !3) + 4 + disp * 4. +fn pc_relative_longword_offset(v_addr: u32, op: u16) -> u32 { + let offset = (op as u32 & 0xff) * 4 + 4; + if (offset + v_addr) & 3 == 2 { offset - 2 } else { offset } +} + +/// An operand of an instruction decoded by `push_instruction`. +enum Operand { + /// A register: `rn`, `frn`, `fpul`, ... + Reg(&'static str), + /// `@rn` + AtReg(&'static str), + /// `@rn+` + AtRegInc(&'static str), + /// `@-rn` + AtDecReg(&'static str), + /// `@(r0, rn)` + AtR0Reg(&'static str), +} + +/// Pushes an instruction whose operands are registers, every one an argument for diffing. +fn push_instruction( + parts: &mut Vec, + mnemonic: &'static str, + op: Ops, + operands: &[Operand], +) { + parts.push(InstructionPart::opcode(mnemonic, op as u16)); + for (index, operand) in operands.iter().enumerate() { + if index > 0 { + parts.push(InstructionPart::separator()); + } + match *operand { + Operand::Reg(reg) => parts.push(InstructionPart::opaque(reg)), + Operand::AtReg(reg) => { + parts.push(InstructionPart::basic("@")); + parts.push(InstructionPart::opaque(reg)); + } + Operand::AtRegInc(reg) => { + parts.push(InstructionPart::basic("@")); + parts.push(InstructionPart::opaque(reg)); + parts.push(InstructionPart::basic("+")); + } + Operand::AtDecReg(reg) => { + parts.push(InstructionPart::basic("@-")); + parts.push(InstructionPart::opaque(reg)); + } + Operand::AtR0Reg(reg) => { + parts.push(InstructionPart::basic("@(")); + parts.push(InstructionPart::opaque("r0")); + parts.push(InstructionPart::separator()); + parts.push(InstructionPart::opaque(reg)); + parts.push(InstructionPart::basic(")")); + } + } + } } fn match_ni_f( _v_addr: u32, @@ -184,7 +333,7 @@ fn match_ni_f( parts.push(InstructionPart::basic("#")); parts.push(InstructionPart::unsigned(op & 0xff)); parts.push(InstructionPart::separator()); - parts.push(InstructionPart::basic(reg)); + parts.push(InstructionPart::opaque(reg)); } 0xe000 => { // MOV #imm,Rn @@ -329,12 +478,7 @@ fn match_nd8_f( parts.push(InstructionPart::opaque(format!("r{}", (op >> 8) & 0xf))); } 0xd000 => { - let mut target_a = (op as u32 & 0xff) * 4 + 4; - let test = (op as u32 & 0xff) * 4 + 4 + v_addr; - - if (test & 3) == 2 { - target_a -= 2; - } + let target_a = pc_relative_longword_offset(v_addr, op); parts.push(InstructionPart::opcode("mov.l", Ops::MovLAtDispPcRn as u16)); parts.push(InstructionPart::basic("@(")); if resolved.relocation.is_some() { @@ -557,6 +701,21 @@ fn match_d_f( parts.push(InstructionPart::unsigned(addr)); } } + 0xc700 => { + // MOVA @(disp,PC),R0 + parts.push(InstructionPart::opcode("mova", Ops::MovaAtDispPcR0 as u16)); + parts.push(InstructionPart::basic("@(")); + if resolved.relocation.is_some() { + parts.push(InstructionPart::reloc()); + } else { + parts.push(InstructionPart::unsigned(pc_relative_longword_offset(v_addr, op))); + } + parts.push(InstructionPart::separator()); + parts.push(InstructionPart::opaque("pc")); + parts.push(InstructionPart::basic(")")); + parts.push(InstructionPart::separator()); + parts.push(InstructionPart::opaque("r0")); + } _ => match_d12_f(v_addr, op, mode, parts, resolved, branch_dest), } } @@ -574,12 +733,12 @@ fn match_nmd_f( match op & 0xf000 { 0x1000 => { parts.push(InstructionPart::opcode("mov.l", Ops::MovLRmAtDispRn as u16)); - parts.push(InstructionPart::basic(reg_m)); + parts.push(InstructionPart::opaque(reg_m)); parts.push(InstructionPart::separator()); parts.push(InstructionPart::basic("@(")); parts.push(InstructionPart::unsigned((op & 0xf) * 4)); parts.push(InstructionPart::separator()); - parts.push(InstructionPart::basic(reg_n)); + parts.push(InstructionPart::opaque(reg_n)); parts.push(InstructionPart::basic(")")); } 0x5000 => { @@ -587,10 +746,10 @@ fn match_nmd_f( parts.push(InstructionPart::basic("@(")); parts.push(InstructionPart::unsigned((op & 0xf) * 4)); parts.push(InstructionPart::separator()); - parts.push(InstructionPart::basic(reg_m)); + parts.push(InstructionPart::opaque(reg_m)); parts.push(InstructionPart::basic(")")); parts.push(InstructionPart::separator()); - parts.push(InstructionPart::basic(reg_n)); + parts.push(InstructionPart::opaque(reg_n)); } _ => match_d_f(v_addr, op, mode, parts, resolved, branch_dest), } @@ -672,8 +831,11 @@ fn match_f00f( resolved: &ResolvedInstructionRef, branch_dest: &mut Option, ) { + use Operand::{AtDecReg, AtR0Reg, AtReg, AtRegInc, Reg}; let reg_m = REG_NAMES[((op >> 4) & 0xf) as usize]; let reg_n = REG_NAMES[((op >> 8) & 0xf) as usize]; + let fr_m = FR_NAMES[((op >> 4) & 0xf) as usize]; + let fr_n = FR_NAMES[((op >> 8) & 0xf) as usize]; match op & 0xf00f { 0x300c => { @@ -1047,6 +1209,30 @@ fn match_f00f( parts.push(InstructionPart::separator()); parts.push(InstructionPart::opaque(reg_n)); } + 0x400c => push_instruction(parts, "shad", Ops::ShadRmRn, &[Reg(reg_m), Reg(reg_n)]), + 0x400d => push_instruction(parts, "shld", Ops::ShldRmRn, &[Reg(reg_m), Reg(reg_n)]), + 0xf000 => push_instruction(parts, "fadd", Ops::FaddFrmFrn, &[Reg(fr_m), Reg(fr_n)]), + 0xf001 => push_instruction(parts, "fsub", Ops::FsubFrmFrn, &[Reg(fr_m), Reg(fr_n)]), + 0xf002 => push_instruction(parts, "fmul", Ops::FmulFrmFrn, &[Reg(fr_m), Reg(fr_n)]), + 0xf003 => push_instruction(parts, "fdiv", Ops::FdivFrmFrn, &[Reg(fr_m), Reg(fr_n)]), + 0xf004 => push_instruction(parts, "fcmp/eq", Ops::FcmpEqFrmFrn, &[Reg(fr_m), Reg(fr_n)]), + 0xf005 => push_instruction(parts, "fcmp/gt", Ops::FcmpGtFrmFrn, &[Reg(fr_m), Reg(fr_n)]), + // The fmov forms move a pair of registers when FPSCR.SZ is set: shown as when it is clear. + 0xf006 => push_instruction(parts, "fmov", Ops::FmovAtR0RmFrn, &[AtR0Reg(reg_m), Reg(fr_n)]), + 0xf007 => push_instruction(parts, "fmov", Ops::FmovFrmAtR0Rn, &[Reg(fr_m), AtR0Reg(reg_n)]), + 0xf008 => push_instruction(parts, "fmov", Ops::FmovAtRmFrn, &[AtReg(reg_m), Reg(fr_n)]), + 0xf009 => { + push_instruction(parts, "fmov", Ops::FmovAtRmIncFrn, &[AtRegInc(reg_m), Reg(fr_n)]) + } + 0xf00a => push_instruction(parts, "fmov", Ops::FmovFrmAtRn, &[Reg(fr_m), AtReg(reg_n)]), + 0xf00b => push_instruction(parts, "fmov", Ops::FmovFrmAtDecrementRn, &[ + Reg(fr_m), + AtDecReg(reg_n), + ]), + 0xf00c => push_instruction(parts, "fmov", Ops::FmovFrmFrn, &[Reg(fr_m), Reg(fr_n)]), + 0xf00e => { + push_instruction(parts, "fmac", Ops::FmacFr0FrmFrn, &[Reg("fr0"), Reg(fr_m), Reg(fr_n)]) + } _ => match_ff00(v_addr, op, mode, parts, resolved, branch_dest), } } @@ -1059,7 +1245,9 @@ fn match_f0ff( resolved: &ResolvedInstructionRef, branch_dest: &mut Option, ) { + use Operand::{AtDecReg, AtReg, AtRegInc, Reg}; let reg = REG_NAMES[((op >> 8) & 0xf) as usize]; + let fr = FR_NAMES[((op >> 8) & 0xf) as usize]; match op & 0xf0ff { 0x4015 => { // CMP/PL Rn @@ -1194,8 +1382,9 @@ fn match_f0ff( parts.push(InstructionPart::opaque(reg)); } 0x401b => { - // TAS.B + // TAS.B @Rn parts.push(InstructionPart::opcode("tas.b", Ops::TasB as u16)); + parts.push(InstructionPart::basic("@")); parts.push(InstructionPart::opaque(reg)); } 0x4003 => { @@ -1342,12 +1531,142 @@ fn match_f0ff( parts.push(InstructionPart::opcode("bsrf", Ops::BsrfRn as u16)); parts.push(InstructionPart::opaque(reg)); } + // SH-4 cache instructions + 0x0083 => push_instruction(parts, "pref", Ops::PrefAtRn, &[AtReg(reg)]), + 0x0093 => push_instruction(parts, "ocbi", Ops::OcbiAtRn, &[AtReg(reg)]), + 0x00a3 => push_instruction(parts, "ocbp", Ops::OcbpAtRn, &[AtReg(reg)]), + 0x00b3 => push_instruction(parts, "ocbwb", Ops::OcbwbAtRn, &[AtReg(reg)]), + 0x00c3 => push_instruction(parts, "movca.l", Ops::MovcaLR0AtRn, &[Reg("r0"), AtReg(reg)]), + // SH-3 and SH-4 control registers + 0x0032 => push_instruction(parts, "stc", Ops::StcSsrRn, &[Reg("ssr"), Reg(reg)]), + 0x0042 => push_instruction(parts, "stc", Ops::StcSpcRn, &[Reg("spc"), Reg(reg)]), + 0x003a => push_instruction(parts, "stc", Ops::StcSgrRn, &[Reg("sgr"), Reg(reg)]), + 0x00fa => push_instruction(parts, "stc", Ops::StcDbrRn, &[Reg("dbr"), Reg(reg)]), + 0x4033 => push_instruction(parts, "stc.l", Ops::StcLSsrAtDecrementRn, &[ + Reg("ssr"), + AtDecReg(reg), + ]), + 0x4043 => push_instruction(parts, "stc.l", Ops::StcLSpcAtDecrementRn, &[ + Reg("spc"), + AtDecReg(reg), + ]), + 0x4032 => push_instruction(parts, "stc.l", Ops::StcLSgrAtDecrementRn, &[ + Reg("sgr"), + AtDecReg(reg), + ]), + 0x40f2 => push_instruction(parts, "stc.l", Ops::StcLDbrAtDecrementRn, &[ + Reg("dbr"), + AtDecReg(reg), + ]), + 0x403e => push_instruction(parts, "ldc", Ops::LdcRmSsr, &[Reg(reg), Reg("ssr")]), + 0x404e => push_instruction(parts, "ldc", Ops::LdcRmSpc, &[Reg(reg), Reg("spc")]), + 0x403a => push_instruction(parts, "ldc", Ops::LdcRmSgr, &[Reg(reg), Reg("sgr")]), + 0x40fa => push_instruction(parts, "ldc", Ops::LdcRmDbr, &[Reg(reg), Reg("dbr")]), + 0x4037 => { + push_instruction(parts, "ldc.l", Ops::LdcLAtRmIncSsr, &[AtRegInc(reg), Reg("ssr")]) + } + 0x4047 => { + push_instruction(parts, "ldc.l", Ops::LdcLAtRmIncSpc, &[AtRegInc(reg), Reg("spc")]) + } + 0x4036 => { + push_instruction(parts, "ldc.l", Ops::LdcLAtRmIncSgr, &[AtRegInc(reg), Reg("sgr")]) + } + 0x40f6 => { + push_instruction(parts, "ldc.l", Ops::LdcLAtRmIncDbr, &[AtRegInc(reg), Reg("dbr")]) + } + // SH-4 floating-point system registers + 0x406a => push_instruction(parts, "lds", Ops::LdsRmFpscr, &[Reg(reg), Reg("fpscr")]), + 0x405a => push_instruction(parts, "lds", Ops::LdsRmFpul, &[Reg(reg), Reg("fpul")]), + 0x4066 => { + push_instruction(parts, "lds.l", Ops::LdsLAtRmIncFpscr, &[AtRegInc(reg), Reg("fpscr")]) + } + 0x4056 => { + push_instruction(parts, "lds.l", Ops::LdsLAtRmIncFpul, &[AtRegInc(reg), Reg("fpul")]) + } + 0x006a => push_instruction(parts, "sts", Ops::StsFpscrRn, &[Reg("fpscr"), Reg(reg)]), + 0x005a => push_instruction(parts, "sts", Ops::StsFpulRn, &[Reg("fpul"), Reg(reg)]), + 0x4062 => push_instruction(parts, "sts.l", Ops::StsLFpscrAtDecrementRn, &[ + Reg("fpscr"), + AtDecReg(reg), + ]), + 0x4052 => push_instruction(parts, "sts.l", Ops::StsLFpulAtDecrementRn, &[ + Reg("fpul"), + AtDecReg(reg), + ]), + // SH-4 floating-point instructions with one register + 0xf00d => push_instruction(parts, "fsts", Ops::FstsFpulFrn, &[Reg("fpul"), Reg(fr)]), + 0xf01d => push_instruction(parts, "flds", Ops::FldsFrmFpul, &[Reg(fr), Reg("fpul")]), + 0xf02d => push_instruction(parts, "float", Ops::FloatFpulFrn, &[Reg("fpul"), Reg(fr)]), + 0xf03d => push_instruction(parts, "ftrc", Ops::FtrcFrmFpul, &[Reg(fr), Reg("fpul")]), + 0xf04d => push_instruction(parts, "fneg", Ops::FnegFrn, &[Reg(fr)]), + 0xf05d => push_instruction(parts, "fabs", Ops::FabsFrn, &[Reg(fr)]), + 0xf06d => push_instruction(parts, "fsqrt", Ops::FsqrtFrn, &[Reg(fr)]), + 0xf07d => push_instruction(parts, "fsrra", Ops::FsrraFrn, &[Reg(fr)]), + 0xf08d => push_instruction(parts, "fldi0", Ops::Fldi0Frn, &[Reg(fr)]), + 0xf09d => push_instruction(parts, "fldi1", Ops::Fldi1Frn, &[Reg(fr)]), + 0xf0ed => { + // FIPR FVm,FVn: 1111nnmm11101101 + let fv_m = FV_NAMES[((op >> 8) & 0x3) as usize]; + let fv_n = FV_NAMES[((op >> 10) & 0x3) as usize]; + push_instruction(parts, "fipr", Ops::FiprFvmFvn, &[Reg(fv_m), Reg(fv_n)]) + } _ => { match_f00f(v_addr, op, mode, parts, resolved, branch_dest); } } } +/// SH-3 and SH-4 instructions with a banked register, 0nnn in bits 4-7. +fn match_f08f( + v_addr: u32, + op: u16, + mode: bool, + parts: &mut Vec, + resolved: &ResolvedInstructionRef, + branch_dest: &mut Option, +) { + use Operand::{AtDecReg, AtRegInc, Reg}; + let reg = REG_NAMES[((op >> 8) & 0xf) as usize]; + let bank = BANK_REG_NAMES[((op >> 4) & 0x7) as usize]; + match op & 0xf08f { + 0x0082 => push_instruction(parts, "stc", Ops::StcRmBankRn, &[Reg(bank), Reg(reg)]), + 0x4083 => push_instruction(parts, "stc.l", Ops::StcLRmBankAtDecrementRn, &[ + Reg(bank), + AtDecReg(reg), + ]), + 0x408e => push_instruction(parts, "ldc", Ops::LdcRmRnBank, &[Reg(reg), Reg(bank)]), + 0x4087 => { + push_instruction(parts, "ldc.l", Ops::LdcLAtRmIncRnBank, &[AtRegInc(reg), Reg(bank)]) + } + _ => match_f1ff(v_addr, op, mode, parts, resolved, branch_dest), + } +} + +/// SH-4 floating-point instructions with a double-precision or vector register field. +fn match_f1ff( + v_addr: u32, + op: u16, + mode: bool, + parts: &mut Vec, + resolved: &ResolvedInstructionRef, + branch_dest: &mut Option, +) { + use Operand::Reg; + // nnn0 in bits 8-11 + let dr = DR_NAMES[((op >> 9) & 0x7) as usize]; + match op & 0xf1ff { + 0xf0ad => push_instruction(parts, "fcnvsd", Ops::FcnvsdFpulDrn, &[Reg("fpul"), Reg(dr)]), + 0xf0bd => push_instruction(parts, "fcnvds", Ops::FcnvdsDrmFpul, &[Reg(dr), Reg("fpul")]), + 0xf0fd => push_instruction(parts, "fsca", Ops::FscaFpulDrn, &[Reg("fpul"), Reg(dr)]), + _ if op & 0xf3ff == 0xf1fd => { + // FTRV XMTRX,FVn: 1111nn0111111101 + let fv = FV_NAMES[((op >> 10) & 0x3) as usize]; + push_instruction(parts, "ftrv", Ops::FtrvXmtrxFvn, &[Reg("xmtrx"), Reg(fv)]) + } + _ => match_f0ff(v_addr, op, mode, parts, resolved, branch_dest), + } +} + pub fn sh2_disasm( v_addr: u32, op: u16, @@ -1365,8 +1684,13 @@ pub fn sh2_disasm( 0x000b => parts.push(InstructionPart::opcode("rts", Ops::Rts as u16)), 0x0018 => parts.push(InstructionPart::opcode("sett", Ops::Sett as u16)), 0x001b => parts.push(InstructionPart::opcode("sleep", Ops::Sleep as u16)), + 0x0038 => parts.push(InstructionPart::opcode("ldtlb", Ops::Ldtlb as u16)), + 0x0048 => parts.push(InstructionPart::opcode("clrs", Ops::Clrs as u16)), + 0x0058 => parts.push(InstructionPart::opcode("sets", Ops::Sets as u16)), + 0xf3fd => parts.push(InstructionPart::opcode("fschg", Ops::Fschg as u16)), + 0xfbfd => parts.push(InstructionPart::opcode("frchg", Ops::Frchg as u16)), _ => { - match_f0ff(v_addr, op, mode, parts, resolved, branch_dest); + match_f08f(v_addr, op, mode, parts, resolved, branch_dest); } } } diff --git a/objdiff-core/src/arch/superh/mod.rs b/objdiff-core/src/arch/superh/mod.rs index 6dc37355..97e03829 100644 --- a/objdiff-core/src/arch/superh/mod.rs +++ b/objdiff-core/src/arch/superh/mod.rs @@ -1,7 +1,7 @@ use alloc::{collections::BTreeMap, format, vec, vec::Vec}; use anyhow::Result; -use object::elf; +use object::{Endian as _, Object as _, elf}; use crate::{ arch::{Arch, superh::disasm::sh2_disasm}, @@ -12,10 +12,13 @@ use crate::{ pub mod disasm; #[derive(Debug)] -pub struct ArchSuperH {} +pub struct ArchSuperH { + /// Byte order of the object's code: big-endian on the Saturn, little-endian on the Dreamcast. + endianness: object::Endianness, +} impl ArchSuperH { - pub fn new(_file: &object::File) -> Result { Ok(Self {}) } + pub fn new(file: &object::File) -> Result { Ok(Self { endianness: file.endianness() }) } } struct DataInfo { @@ -36,7 +39,7 @@ impl Arch for ArchSuperH { let mut offset = address; for chunk in code.as_chunks::<2>().0 { - let opcode = u16::from_be_bytes(*chunk); + let opcode = self.endianness.read_u16(*chunk); let mut parts: Vec = vec![]; let resolved: ResolvedInstructionRef = Default::default(); let mut branch_dest: Option = None; @@ -66,7 +69,7 @@ impl Arch for ArchSuperH { _diff_config: &DiffObjConfig, cb: &mut dyn FnMut(InstructionPart) -> Result<()>, ) -> Result<()> { - let opcode = u16::from_be_bytes(resolved.code.try_into().unwrap()); + let opcode = self.endianness.read_u16(resolved.code.try_into().unwrap()); let mut parts: Vec = vec![]; let mut branch_dest: Option = None; @@ -81,7 +84,7 @@ impl Arch for ArchSuperH { let mut pos: u64 = 0; for chunk in symbol_data.as_chunks::<2>().0 { - let opcode = u16::from_be_bytes(*chunk); + let opcode = self.endianness.read_u16(*chunk); // mov.w if (opcode & 0xf000) == 0x9000 { let target = (opcode as u64 & 0xff) * 2 + 4 + pos; @@ -102,7 +105,7 @@ impl Arch for ArchSuperH { // add the data info if let Some(value) = data_offsets.get(&pos) { if value.size == 2 && value.address as usize + 1 < symbol_data.len() { - let data = u16::from_be_bytes( + let data = self.endianness.read_u16( symbol_data[value.address as usize..value.address as usize + 2] .try_into() .unwrap(), @@ -112,7 +115,7 @@ impl Arch for ArchSuperH { parts.push(InstructionPart::basic(format!("{data:04X}"))); parts.push(InstructionPart::basic(" */")); } else if value.size == 4 && value.address as usize + 3 < symbol_data.len() { - let data = u32::from_be_bytes( + let data = self.endianness.read_u32( symbol_data[value.address as usize..value.address as usize + 4] .try_into() .unwrap(), @@ -217,7 +220,7 @@ mod test { #[test] fn test_sh2_display_instruction_basic_ops() { - let arch = ArchSuperH {}; + let arch = ArchSuperH { endianness: object::Endianness::Big }; let ops: [(u16, &str); 8] = [ (0x0008, "clrt "), (0x0028, "clrmac "), @@ -254,7 +257,7 @@ mod test { #[test] fn test_sh2_display_instruction_f0ff_ops() { - let arch = ArchSuperH {}; + let arch = ArchSuperH { endianness: object::Endianness::Big }; let ops: [(u16, &str); 49] = [ (0x4015, "cmp/pl r0"), (0x4115, "cmp/pl r1"), @@ -283,7 +286,7 @@ mod test { (0x000a, "sts mach, r0"), (0x001a, "sts macl, r0"), (0x402a, "lds r0, pr"), - (0x401b, "tas.b r0"), + (0x401b, "tas.b @r0"), (0x4003, "stc.l sr, @-r0"), (0x4013, "stc.l gbr, @-r0"), (0x4023, "stc.l vbr, @-r0"), @@ -332,7 +335,7 @@ mod test { #[test] fn test_sh2_display_instructions_f00f() { - let arch = ArchSuperH {}; + let arch = ArchSuperH { endianness: object::Endianness::Big }; let ops: [(u16, &str); 54] = [ (0x300c, "add r0, r0"), (0x300e, "addc r0, r0"), @@ -415,7 +418,7 @@ mod test { #[test] fn test_sh2_display_instruction_mov_immediate_offset() { - let arch = ArchSuperH {}; + let arch = ArchSuperH { endianness: object::Endianness::Big }; let ops: [(u16, &str); 8] = [ (0x8000, "mov.b r0, @(0x0, r0)"), (0x8011, "mov.b r0, @(0x1, r1)"), @@ -452,7 +455,7 @@ mod test { #[test] fn test_sh2_display_instruction_gbr_and_branches() { - let arch = ArchSuperH {}; + let arch = ArchSuperH { endianness: object::Endianness::Big }; let ops: &[(u16, u32, &str)] = &[ (0xc000, 0x0000, "mov.b r0, @(0x0, gbr)"), (0xc07f, 0x0000, "mov.b r0, @(0x7f, gbr)"), @@ -506,7 +509,7 @@ mod test { #[test] fn test_sh2_display_instruction_mov_l() { - let arch = ArchSuperH {}; + let arch = ArchSuperH { endianness: object::Endianness::Big }; let ops: &[(u16, u32, &str)] = &[ // mov.l rX, @(0xXXX, rY) (0x1000, 0x0000, "mov.l r0, @(0x0, r0)"), @@ -547,7 +550,7 @@ mod test { #[test] fn test_sh2_display_instruction_bra_bsr() { - let arch: ArchSuperH = ArchSuperH {}; + let arch: ArchSuperH = ArchSuperH { endianness: object::Endianness::Big }; let ops: &[(u16, u32, &str)] = &[ // bra (0xa000, 0x0000, "bra 0x4"), @@ -591,7 +594,7 @@ mod test { #[test] fn test_sh2_display_instruction_operations() { - let arch = ArchSuperH {}; + let arch = ArchSuperH { endianness: object::Endianness::Big }; let ops: &[(u16, u32, &str)] = &[ (0xcdff, 0x0000, "and.b #0xff, @(r0, gbr)"), (0xcfff, 0x0000, "or.b #0xff, @(r0, gbr)"), @@ -635,7 +638,7 @@ mod test { #[test] fn test_sh2_add_mov_unknown_instructions() { - let arch = ArchSuperH {}; + let arch = ArchSuperH { endianness: object::Endianness::Big }; let ops: &[(u16, u32, &str)] = &[ (0x70FF, 0x0000, "add #0xff, r0"), (0xE0FF, 0x0000, "mov #0xff, r0"), @@ -672,7 +675,7 @@ mod test { #[test] fn test_sh2_mov_instructions_with_labels() { - let arch = ArchSuperH {}; + let arch = ArchSuperH { endianness: object::Endianness::Big }; let ops: &[(u16, u32, &str)] = &[(0x9000, 0x0000, "mov.w @(0x4, pc), r0"), (0xd000, 0x0000, "mov.l @(0x4, pc), r0")]; @@ -706,7 +709,7 @@ mod test { #[test] fn test_func_0606_f378_mov_w_data_labeling() { - let arch = ArchSuperH {}; + let arch = ArchSuperH { endianness: object::Endianness::Big }; let ops: &[(u16, u32, &str)] = &[(0x9000, 0x0606F378, "mov.w @(0x4, pc), r0 /* 0x00B0 */")]; let mut code = Vec::new(); @@ -754,7 +757,7 @@ mod test { #[test] fn test_func_0606_f378_mov_l_data_labeling() { - let arch = ArchSuperH {}; + let arch = ArchSuperH { endianness: object::Endianness::Big }; let ops: &[(u16, u32, &str)] = &[(0xd000, 0x0606F378, "mov.l @(0x4, pc), r0 /* 0x00B000B0 */")]; @@ -801,4 +804,282 @@ mod test { assert_eq!(joined_str, expected_str.to_string()); } } + + /// A little-endian function (Dreamcast) and its literal pool: the same code as big-endian + /// (Saturn) must scan to the same instructions and display the same. + #[test] + fn test_sh4_little_endian_matches_big_endian() { + const ADDRESS: u64 = 0x8C010000; + let ops: [(u16, &str); 6] = [ + (0xd102, "mov.l @(0xc, pc), r1 /* 0x8C0FF920 */"), + (0xa000, "bra 0x4"), // to ADDRESS + 6 + (0x0009, "nop "), + (0x000b, "rts "), + (0x0009, "nop "), + (0x0009, "nop "), // pads the literal pool to 4 bytes + ]; + let pool: u32 = 0x8C0FF920; + + for endianness in [object::Endianness::Little, object::Endianness::Big] { + let arch = ArchSuperH { endianness }; + let mut code = Vec::new(); + for (opcode, _) in ops { + code.extend_from_slice(&match endianness { + object::Endianness::Little => opcode.to_le_bytes(), + object::Endianness::Big => opcode.to_be_bytes(), + }); + } + let code_len = code.len(); + code.extend_from_slice(&match endianness { + object::Endianness::Little => pool.to_le_bytes(), + object::Endianness::Big => pool.to_be_bytes(), + }); + + let instructions = arch + .scan_instructions_internal( + ADDRESS, + &code[..code_len], + 0, + &[], + &DiffObjConfig::default(), + ) + .unwrap(); + assert_eq!(instructions.len(), ops.len()); + assert_eq!(instructions[1].branch_dest, Some(ADDRESS + 6), "{endianness:?}"); + + let symbol = Symbol { address: ADDRESS, size: code.len() as u64, ..Default::default() }; + let section = Section { + address: ADDRESS, + size: code.len() as u64, + data: SectionData(code.clone()), + ..Default::default() + }; + for (ins_ref, (_, expected_str)) in instructions.iter().zip(ops) { + let offset = (ins_ref.address - ADDRESS) as usize; + let mut parts = Vec::new(); + arch.display_instruction( + ResolvedInstructionRef { + ins_ref: *ins_ref, + code: &code[offset..offset + 2], + symbol: &symbol, + section: §ion, + ..Default::default() + }, + &DiffObjConfig::default(), + &mut |part| { + parts.push(part.into_static()); + Ok(()) + }, + ) + .unwrap(); + let joined_str: String = parts.iter().map(<_>::to_string).collect(); + assert_eq!(joined_str, expected_str, "{endianness:?}"); + } + } + } + /// Every register of an SH-2 instruction is an argument, so that a different one is a diff. + #[test] + fn test_sh2_registers_are_arguments() { + let arch = ArchSuperH { endianness: object::Endianness::Big }; + let ops: [(u16, &str, &[&str]); 4] = [ + (0x7401, "add #0x1, r4", &["0x1", "r4"]), + (0x1412, "mov.l r1, @(0x8, r4)", &["r1", "0x8", "r4"]), + (0x5412, "mov.l @(0x8, r1), r4", &["0x8", "r1", "r4"]), + (0x411b, "tas.b @r1", &["r1"]), + ]; + + for (opcode, expected_str, expected_args) in ops { + let code = opcode.to_be_bytes(); + let mut parts = Vec::new(); + + arch.display_instruction( + ResolvedInstructionRef { + ins_ref: InstructionRef { address: 0x1000, size: 2, opcode, branch_dest: None }, + code: &code, + ..Default::default() + }, + &DiffObjConfig::default(), + &mut |part| { + parts.push(part.into_static()); + Ok(()) + }, + ) + .unwrap(); + + let joined_str: String = parts.iter().map(<_>::to_string).collect(); + assert_eq!(joined_str, expected_str); + let args: Vec = parts + .iter() + .filter_map(|part| match part { + InstructionPart::Arg(arg) => Some(arg.to_string()), + _ => None, + }) + .collect(); + assert_eq!(args, expected_args, "{expected_str}"); + } + } + + /// Every SH-4 addition, each with different register numbers in its fields, and encodings + /// the SH-4 doesn't have. Every register is an argument, so that a different one is a diff. + #[test] + fn test_sh4_display_instructions() { + let arch = ArchSuperH { endianness: object::Endianness::Little }; + let ops: [(u16, &str); 77] = [ + (0x0038, "ldtlb "), + (0x0048, "clrs "), + (0x0058, "sets "), + (0x0583, "pref @r5"), + (0x0693, "ocbi @r6"), + (0x07a3, "ocbp @r7"), + (0x08b3, "ocbwb @r8"), + (0x09c3, "movca.l r0, @r9"), + (0x412c, "shad r2, r1"), + (0x423d, "shld r3, r2"), + (0x413e, "ldc r1, ssr"), + (0x424e, "ldc r2, spc"), + (0x433a, "ldc r3, sgr"), + (0x44fa, "ldc r4, dbr"), + (0x45ce, "ldc r5, r4_bank"), + (0x4637, "ldc.l @r6+, ssr"), + (0x4747, "ldc.l @r7+, spc"), + (0x4836, "ldc.l @r8+, sgr"), + (0x49f6, "ldc.l @r9+, dbr"), + (0x4ad7, "ldc.l @r10+, r5_bank"), + (0x0b32, "stc ssr, r11"), + (0x0c42, "stc spc, r12"), + (0x0d3a, "stc sgr, r13"), + (0x0efa, "stc dbr, r14"), + (0x0fe2, "stc r6_bank, r15"), + (0x4133, "stc.l ssr, @-r1"), + (0x4243, "stc.l spc, @-r2"), + (0x4332, "stc.l sgr, @-r3"), + (0x44f2, "stc.l dbr, @-r4"), + (0x45f3, "stc.l r7_bank, @-r5"), + (0x466a, "lds r6, fpscr"), + (0x475a, "lds r7, fpul"), + (0x4866, "lds.l @r8+, fpscr"), + (0x4956, "lds.l @r9+, fpul"), + (0x0a6a, "sts fpscr, r10"), + (0x0b5a, "sts fpul, r11"), + (0x4c62, "sts.l fpscr, @-r12"), + (0x4d52, "sts.l fpul, @-r13"), + (0xf120, "fadd fr2, fr1"), + (0xf231, "fsub fr3, fr2"), + (0xf342, "fmul fr4, fr3"), + (0xf453, "fdiv fr5, fr4"), + (0xf564, "fcmp/eq fr6, fr5"), + (0xf675, "fcmp/gt fr7, fr6"), + (0xf786, "fmov @(r0, r8), fr7"), + (0xf897, "fmov fr9, @(r0, r8)"), + (0xf9a8, "fmov @r10, fr9"), + (0xfab9, "fmov @r11+, fr10"), + (0xfbca, "fmov fr12, @r11"), + (0xfcdb, "fmov fr13, @-r12"), + (0xfdec, "fmov fr14, fr13"), + (0xfefe, "fmac fr0, fr15, fr14"), + (0xf10d, "fsts fpul, fr1"), + (0xf21d, "flds fr2, fpul"), + (0xf32d, "float fpul, fr3"), + (0xf43d, "ftrc fr4, fpul"), + (0xf54d, "fneg fr5"), + (0xf65d, "fabs fr6"), + (0xf76d, "fsqrt fr7"), + (0xf87d, "fsrra fr8"), + (0xf98d, "fldi0 fr9"), + (0xfa9d, "fldi1 fr10"), + (0xf2ad, "fcnvsd fpul, dr2"), + (0xf4bd, "fcnvds dr4, fpul"), + (0xf6ed, "fipr fv8, fv4"), + (0xf8fd, "fsca fpul, dr8"), + (0xf5fd, "ftrv xmtrx, fv4"), + (0xf3fd, "fschg "), + (0xfbfd, "frchg "), + (0xf00f, ".word 0xF00F /* unknown instruction */"), + (0xf1ad, ".word 0xF1AD /* unknown instruction */"), + (0xf3bd, ".word 0xF3BD /* unknown instruction */"), + (0xf0cd, ".word 0xF0CD /* unknown instruction */"), + (0xf0dd, ".word 0xF0DD /* unknown instruction */"), + (0xf7fd, ".word 0xF7FD /* unknown instruction */"), + (0x0068, ".word 0x0068 /* unknown instruction */"), + (0x4084, ".word 0x4084 /* unknown instruction */"), + ]; + + for (opcode, expected_str) in ops { + let code = opcode.to_le_bytes(); + let mut parts = Vec::new(); + + arch.display_instruction( + ResolvedInstructionRef { + ins_ref: InstructionRef { address: 0x1000, size: 2, opcode, branch_dest: None }, + code: &code, + ..Default::default() + }, + &DiffObjConfig::default(), + &mut |part| { + parts.push(part.into_static()); + Ok(()) + }, + ) + .unwrap(); + + let joined_str: String = parts.iter().map(<_>::to_string).collect(); + assert_eq!(joined_str, expected_str.to_string()); + if !expected_str.starts_with(".word") { + assert!( + parts.iter().all(|part| !matches!(part, + InstructionPart::Basic(text) if text.chars().any(char::is_alphanumeric))), + "{expected_str}: a register is not an argument" + ); + } + } + } + + /// mova shows its longword's offset like mov.l @(disp,PC) does: (PC & !3) + 4 + disp * 4, + /// from the instruction's aligned address. + #[test] + fn test_sh4_mova() { + const ADDRESS: u64 = 0x8C010000; + let arch = ArchSuperH { endianness: object::Endianness::Little }; + let ops: [(u16, &str); 4] = [ + (0xc700, "mova @(0x4, pc), r0"), + (0xc701, "mova @(0x8, pc), r0"), + (0xc7ff, "mova @(0x400, pc), r0"), + (0x0009, "nop "), + ]; + let code: Vec = ops.iter().flat_map(|(opcode, _)| opcode.to_le_bytes()).collect(); + + let instructions = arch + .scan_instructions_internal(ADDRESS, &code, 0, &[], &DiffObjConfig::default()) + .unwrap(); + assert_eq!(instructions.len(), ops.len()); + let symbol = Symbol { address: ADDRESS, size: code.len() as u64, ..Default::default() }; + let section = Section { + address: ADDRESS, + size: code.len() as u64, + data: SectionData(code.clone()), + ..Default::default() + }; + for (ins_ref, (_, expected_str)) in instructions.iter().zip(ops) { + assert_eq!(ins_ref.branch_dest, None); + let offset = (ins_ref.address - ADDRESS) as usize; + let mut parts = Vec::new(); + arch.display_instruction( + ResolvedInstructionRef { + ins_ref: *ins_ref, + code: &code[offset..offset + 2], + symbol: &symbol, + section: §ion, + ..Default::default() + }, + &DiffObjConfig::default(), + &mut |part| { + parts.push(part.into_static()); + Ok(()) + }, + ) + .unwrap(); + let joined_str: String = parts.iter().map(<_>::to_string).collect(); + assert_eq!(joined_str, expected_str); + } + } }