Fix Doldecomp formatter

This commit is contained in:
Richard Patel 2021-08-17 01:21:26 +02:00
parent a15c909240
commit e55398cd01
4 changed files with 46 additions and 5 deletions

View File

@ -11,6 +11,10 @@ repository = "https://github.com/terorie/ppc750cl"
name = "ppc750cl" name = "ppc750cl"
crate-type = ["cdylib"] crate-type = ["cdylib"]
[features]
extension-module = ["pyo3/extension-module"]
default = ["extension-module"]
[dependencies] [dependencies]
pyo3 = { version = "0.14", features = ["extension-module", "multiple-pymethods"] } pyo3 = { version = "0.14", features = ["multiple-pymethods"] }
ppc750cl = { version = "0.1.1", path = "../disasm" } ppc750cl = { version = "0.1.1", path = "../disasm" }

View File

@ -109,7 +109,7 @@ impl PyIterProtocol for DisasmIterator {
if (slf.bytes.len() as u32) - slf.offset < 4 { if (slf.bytes.len() as u32) - slf.offset < 4 {
return Ok(None); return Ok(None);
} }
let code = ((slf.bytes[(slf.offset + 0) as usize] as u32) << 24) let code = ((slf.bytes[(slf.offset) as usize] as u32) << 24)
| ((slf.bytes[(slf.offset + 1) as usize] as u32) << 16) | ((slf.bytes[(slf.offset + 1) as usize] as u32) << 16)
| ((slf.bytes[(slf.offset + 2) as usize] as u32) << 8) | ((slf.bytes[(slf.offset + 2) as usize] as u32) << 8)
| (slf.bytes[(slf.offset + 3) as usize] as u32); | (slf.bytes[(slf.offset + 3) as usize] as u32);
@ -121,7 +121,7 @@ impl PyIterProtocol for DisasmIterator {
} }
#[pyfunction] #[pyfunction]
fn disasm_iter<'a>(code: &[u8], addr: u32) -> PyResult<DisasmIterator> { fn disasm_iter(code: &[u8], addr: u32) -> PyResult<DisasmIterator> {
Ok(DisasmIterator { Ok(DisasmIterator {
bytes: code.to_vec(), bytes: code.to_vec(),
addr, addr,

View File

@ -186,6 +186,12 @@ pub struct DoldecompFormatter<W: Write> {
pub writer: W, pub writer: W,
} }
impl<W: Write> DoldecompFormatter<W> {
pub fn new(writer: W) -> Self {
Self { writer }
}
}
impl<W: Write> AsmFormatter<W> for DoldecompFormatter<W> { impl<W: Write> AsmFormatter<W> for DoldecompFormatter<W> {
fn writer(&mut self) -> &mut W { fn writer(&mut self) -> &mut W {
&mut self.writer &mut self.writer
@ -194,7 +200,8 @@ impl<W: Write> AsmFormatter<W> for DoldecompFormatter<W> {
fn before_instruction(&mut self, ins: &Ins) -> IOResult { fn before_instruction(&mut self, ins: &Ins) -> IOResult {
write!( write!(
&mut self.writer, &mut self.writer,
"/* TODO */ {:X} {:X} {:X} {:X}\t", "/* {:0>8X} {:0>2X} {:0>2X} {:0>2X} {:0>2X} */\t",
ins.addr,
(ins.code >> 24) as u8, (ins.code >> 24) as u8,
(ins.code >> 16) as u8, (ins.code >> 16) as u8,
(ins.code >> 8) as u8, (ins.code >> 8) as u8,
@ -223,3 +230,22 @@ impl<N: PrimInt> UpperHex for ReallySigned<N> {
f.pad_integral(num >= 0, prefix, &bare_hex) f.pad_integral(num >= 0, prefix, &bare_hex)
} }
} }
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_doldecomp_formatter() {
let buf = Vec::<u8>::new();
let mut formatter = DoldecompFormatter::new(buf);
let ins = Ins::new(0x48000007, 6);
ins.write_string(&mut formatter).unwrap();
assert_eq!(
String::from_utf8(formatter.writer).unwrap(),
"/* 00000006 48 00 00 07 */\tbla 0x4"
);
}
}

View File

@ -419,6 +419,17 @@ impl Ins {
} }
pub fn write_string<F, W>(&self, out: &mut F) -> std::io::Result<()> pub fn write_string<F, W>(&self, out: &mut F) -> std::io::Result<()>
where
F: AsmFormatter<W>,
W: Write,
{
out.before_instruction(self)?;
self.write_string_inner(out)?;
out.after_instruction(self)?;
Ok(())
}
fn write_string_inner<F, W>(&self, out: &mut F) -> std::io::Result<()>
where where
F: AsmFormatter<W>, F: AsmFormatter<W>,
W: Write, W: Write,
@ -913,7 +924,7 @@ impl Ins {
impl ToString for Ins { impl ToString for Ins {
fn to_string(&self) -> String { fn to_string(&self) -> String {
let buf = Vec::<u8>::new(); let buf = Vec::<u8>::new();
let mut formatter = SimpleFormatter { writer: buf }; let mut formatter = SimpleFormatter::new(buf);
self.write_string(&mut formatter).unwrap(); self.write_string(&mut formatter).unwrap();
unsafe { String::from_utf8_unchecked(formatter.writer) } unsafe { String::from_utf8_unchecked(formatter.writer) }
} }