From 60957e6b7c059c26b42ee8f56180e417040dd3e4 Mon Sep 17 00:00:00 2001 From: Noa <33094578+coolreader18@users.noreply.github.com> Date: Tue, 14 Dec 2021 12:59:50 -0800 Subject: Update remaining yaxpeax impls to 0.2 --- src/main.rs | 94 +++++++++++++++++++++---------------------------------------- 1 file changed, 32 insertions(+), 62 deletions(-) (limited to 'src/main.rs') diff --git a/src/main.rs b/src/main.rs index fe531dd..02e0974 100644 --- a/src/main.rs +++ b/src/main.rs @@ -85,27 +85,27 @@ fn main() { match arch_str { "x86_64" | - "x86:64" => crate::current_arch::decode_input::(&buf, &printer), + "x86:64" => arch_02::decode_input::(&buf, &printer), "x86_32" | - "x86:32" => crate::current_arch::decode_input::(&buf, &printer), + "x86:32" => arch_02::decode_input::(&buf, &printer), "x86_16" | - "x86:16" => crate::current_arch::decode_input::(&buf, &printer), - "ia64" => crate::current_arch::decode_input::(&buf, &printer), - "avr" => crate::current_arch::decode_input::(&buf, &printer), - "armv7" => crate::current_arch::decode_input::(&buf, &printer), - "armv8" => crate::current_arch::decode_input::(&buf, &printer), - "mips" => crate::current_arch::decode_input::(&buf, &printer), - "msp430" => crate::current_arch::decode_input::(&buf, &printer), - "pic17" => crate::current_arch::decode_input::(&buf, &printer), - "pic18" => crate::current_arch::decode_input::(&buf, &printer), - "m16c" => crate::current_arch::decode_input::(&buf, &printer), - "6502" => crate::legacy_arch::decode_input::(&buf, &printer), - "lc87" => crate::current_arch::decode_input::(&buf, &printer), + "x86:16" => arch_02::decode_input::(&buf, &printer), + "ia64" => arch_02::decode_input::(&buf, &printer), + "avr" => arch_02::decode_input::(&buf, &printer), + "armv7" => arch_02::decode_input::(&buf, &printer), + "armv8" => arch_02::decode_input::(&buf, &printer), + "mips" => arch_02::decode_input::(&buf, &printer), + "msp430" => arch_02::decode_input::(&buf, &printer), + "pic17" => arch_02::decode_input::(&buf, &printer), + "pic18" => arch_02::decode_input::(&buf, &printer), + "m16c" => arch_02::decode_input::(&buf, &printer), + "6502" => arch_02::decode_input::(&buf, &printer), + "lc87" => arch_02::decode_input::(&buf, &printer), // "pic24" => decode_input::(buf), other => { let seg_idx = arch_str.find(|c| c == '+' || c == '-').unwrap_or(arch_str.len()); let wps = |base| with_parsed_superh(base, &arch_str[seg_idx..], - |decoder| crate::legacy_arch::decode_input_with_decoder::(decoder, &buf, &printer)); + |decoder| arch_02::decode_input_with_decoder::(decoder, &buf, &printer)); match &arch_str[0..seg_idx] { "sh" => wps(yaxpeax_superh::SuperHDecoder::SH1), "sh2" => wps(yaxpeax_superh::SuperHDecoder::SH2), @@ -188,67 +188,37 @@ impl Printer { } } -// yaxpeax-arch, implemented by all decoders here, is required at incompatible versions by -// different decoders. implement the actual decode-and-print behavior on both versions of -// yaxpeax-arch while older decoders are still being updated. -mod current_arch { - use yaxpeax_arch_02::{AddressBase, Arch, Decoder, Instruction, LengthedInstruction, Reader, U8Reader}; - use std::fmt; - use num_traits::identities::Zero; +// yaxpeax-arch, implemented by all decoders here, may be required at incompatible versions by +// different decoders if/when a new version releases. implement the actual decode-and-print +// behavior independent of yaxpeax-arch so decoders using different version can exist in parallel. +mod arch_02 { use super::Printer; - - pub(crate) fn decode_input(buf: &[u8], printer: &Printer) - where - A::Instruction: fmt::Display, for<'data> U8Reader<'data>: Reader, - { - decode_input_with_decoder::(A::Decoder::default(), buf, printer); - } - - pub(crate) fn decode_input_with_decoder(decoder: A::Decoder, buf: &[u8], printer: &Printer) - where - A::Instruction: fmt::Display, for<'data> U8Reader<'data>: Reader, - { - let mut addr = A::Address::zero(); - while let Some(rest) = buf.get(addr.to_linear()..).filter(|v| !v.is_empty()) { - let mut reader = U8Reader::new(rest); - let res = decoder.decode(&mut reader); - let advance_addr = match &res { - Ok(inst) => inst.len(), - Err(_) => A::Instruction::min_size(), - }; - let generic_res = res.map(|inst| { - ( - A::Address::zero().wrapping_offset(inst.len()).to_linear(), - inst.well_defined(), - inst, - ) - }); - printer.print_instr(rest, addr.to_linear(), generic_res); - addr += advance_addr; - } - } -} - -mod legacy_arch { - use yaxpeax_arch_01::{AddressBase, Arch, Decoder, Instruction, LengthedInstruction}; - use std::fmt; use num_traits::identities::Zero; - use super::Printer; + use std::fmt; + use yaxpeax_arch_02::{ + AddressBase, Arch, Decoder, Instruction, LengthedInstruction, Reader, U8Reader, + }; pub(crate) fn decode_input(buf: &[u8], printer: &Printer) where A::Instruction: fmt::Display, + for<'data> U8Reader<'data>: Reader, { decode_input_with_decoder::(A::Decoder::default(), buf, printer); } - pub(crate) fn decode_input_with_decoder(decoder: A::Decoder, buf: &[u8], printer: &Printer) - where + pub(crate) fn decode_input_with_decoder( + decoder: A::Decoder, + buf: &[u8], + printer: &Printer, + ) where A::Instruction: fmt::Display, + for<'data> U8Reader<'data>: Reader, { let mut addr = A::Address::zero(); while let Some(rest) = buf.get(addr.to_linear()..).filter(|v| !v.is_empty()) { - let res = decoder.decode(rest.iter().copied()); + let mut reader = U8Reader::new(rest); + let res = decoder.decode(&mut reader); let advance_addr = match &res { Ok(inst) => inst.len(), Err(_) => A::Instruction::min_size(), -- cgit v1.1