aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoriximeow <me@iximeow.net>2020-01-18 14:01:36 -0800
committeriximeow <me@iximeow.net>2020-01-18 14:01:36 -0800
commit40436975204fe296d30362e624e805ade97b4a1b (patch)
treefe0a92f7fb3da1da93ce84d53efc93c29596f68a
parentd8b5f3258d25b4ec22d1a46553518d892cc7aab7 (diff)
some of the work to get yaxpeax-arm no-std
-rw-r--r--Cargo.toml2
-rw-r--r--src/armv7.rs54
-rw-r--r--src/armv8/a64.rs27
-rw-r--r--test/armv7.rs2
-rw-r--r--test/armv8/a64.rs2
5 files changed, 42 insertions, 45 deletions
diff --git a/Cargo.toml b/Cargo.toml
index 9e299e1..20aa58f 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -8,7 +8,7 @@ repository = "http://git.iximeow.net/yaxpeax-arm/"
description = "arm decoders for the yaxpeax project"
[dependencies]
-yaxpeax-arch = { path = "../../yaxpeax-arch" }
+yaxpeax-arch = { version = "0.0.2", default-features = false, features = [] }
"serde" = "*"
"serde_derive" = "*"
diff --git a/src/armv7.rs b/src/armv7.rs
index f85038f..50cde07 100644
--- a/src/armv7.rs
+++ b/src/armv7.rs
@@ -3,19 +3,21 @@
use std::fmt::{self, Display, Formatter};
-use yaxpeax_arch::{Arch, Colorize, Colored, ColorSettings, Decoder, LengthedInstruction, ShowContextual, YaxColors};
+use yaxpeax_arch::{Arch, Colorize, Decoder, LengthedInstruction, NoColors, ShowContextual, YaxColors};
pub struct ConditionedOpcode(pub Opcode, pub ConditionCode);
impl Display for ConditionedOpcode {
- fn fmt(&self, f: &mut Formatter) -> Result<(), std::fmt::Error> {
+ fn fmt(&self, f: &mut Formatter) -> Result<(), fmt::Error> {
write!(f, "{}{}", self.0, self.1)
}
}
+pub struct NoContext;
+
#[allow(non_snake_case)]
-impl <T: std::fmt::Write> ShowContextual<u32, [Option<String>], T> for Instruction {
- fn contextualize(&self, colors: Option<&ColorSettings>, _address: u32, _context: Option<&[Option<String>]>, out: &mut T) -> std::fmt::Result {
+impl <T: fmt::Write, Color: fmt::Display, Y: YaxColors<Color>> ShowContextual<u32, NoContext, Color, T, Y> for Instruction {
+ fn contextualize(&self, colors: &Y, _address: u32, _context: Option<&NoContext>, out: &mut T) -> fmt::Result {
match self.opcode {
Opcode::LDR(true, false, false) => {
match self.operands {
@@ -169,8 +171,8 @@ impl <T: std::fmt::Write> ShowContextual<u32, [Option<String>], T> for Instructi
}
}
-impl <T: std::fmt::Write> Colorize<T> for ConditionedOpcode {
- fn colorize(&self, colors: Option<&ColorSettings>, out: &mut T) -> std::fmt::Result {
+impl <T: fmt::Write, Color: fmt::Display, Y: YaxColors<Color>> Colorize<T, Color, Y> for ConditionedOpcode {
+ fn colorize(&self, colors: &Y, out: &mut T) -> fmt::Result {
match self.0 {
Opcode::Incomplete(_) |
Opcode::Invalid => { write!(out, "{}", colors.invalid_op(self)) },
@@ -248,7 +250,7 @@ impl <T: std::fmt::Write> Colorize<T> for ConditionedOpcode {
}
impl Display for Opcode {
- fn fmt(&self, f: &mut Formatter) -> Result<(), std::fmt::Error> {
+ fn fmt(&self, f: &mut Formatter) -> Result<(), fmt::Error> {
match self {
Opcode::Incomplete(word) => { write!(f, "incomplete: {:#x}", word) },
Opcode::Invalid => { write!(f, "invalid") },
@@ -467,9 +469,8 @@ impl yaxpeax_arch::Instruction for Instruction {
fn well_defined(&self) -> bool { true }
}
-#[allow(non_snake_case)]
-impl Instruction {
- pub fn blank() -> Instruction {
+impl Default for Instruction {
+ fn default() -> Self {
Instruction {
condition: ConditionCode::AL,
opcode: Opcode::Invalid,
@@ -477,13 +478,16 @@ impl Instruction {
s: false
}
}
- pub fn set_S(&mut self, value: bool) {
+}
+
+impl Instruction {
+ fn set_s(&mut self, value: bool) {
self.s = value;
}
- pub fn S(&self) -> bool { self.s }
+ pub fn s(&self) -> bool { self.s }
}
-fn format_reg_list<T: std::fmt::Write>(f: &mut T, mut list: u16, colors: Option<&ColorSettings>) -> Result<(), std::fmt::Error> {
+fn format_reg_list<T: fmt::Write, C: fmt::Display, Y: YaxColors<C>>(f: &mut T, mut list: u16, colors: &Y) -> Result<(), fmt::Error> {
write!(f, "{{")?;
let mut i = 0;
let mut tail = false;
@@ -504,7 +508,7 @@ fn format_reg_list<T: std::fmt::Write>(f: &mut T, mut list: u16, colors: Option<
}
#[allow(non_snake_case)]
-fn format_shift<T: std::fmt::Write>(f: &mut T, Rm: u8, shift: ShiftSpec, colors: Option<&ColorSettings>) -> Result<(), std::fmt::Error> {
+fn format_shift<T: fmt::Write, C: fmt::Display, Y: YaxColors<C>>(f: &mut T, Rm: u8, shift: ShiftSpec, colors: &Y) -> Result<(), fmt::Error> {
fn shift_tpe_to_str(tpe: u8) -> &'static str {
match tpe {
0b00 => "lsl",
@@ -532,7 +536,7 @@ fn format_shift<T: std::fmt::Write>(f: &mut T, Rm: u8, shift: ShiftSpec, colors:
}
#[allow(non_snake_case)]
-fn format_reg_shift_mem<T: std::fmt::Write>(f: &mut T, Rd: u8, Rm: u8, shift: ShiftSpec, add: bool, pre: bool, wback: bool, colors: Option<&ColorSettings>) -> Result<(), std::fmt::Error> {
+fn format_reg_shift_mem<T: fmt::Write, C: fmt::Display, Y: YaxColors<C>>(f: &mut T, Rd: u8, Rm: u8, shift: ShiftSpec, add: bool, pre: bool, wback: bool, colors: &Y) -> Result<(), fmt::Error> {
let op = if add { "" } else { "-" };
match (pre, wback) {
@@ -557,7 +561,7 @@ fn format_reg_shift_mem<T: std::fmt::Write>(f: &mut T, Rd: u8, Rm: u8, shift: Sh
}
#[allow(non_snake_case)]
-fn format_reg_imm_mem<T: std::fmt::Write>(f: &mut T, Rn: u8, imm: u32, add: bool, pre: bool, wback: bool, colors: Option<&ColorSettings>) -> Result<(), std::fmt::Error> {
+fn format_reg_imm_mem<T: fmt::Write, C: fmt::Display, Y: YaxColors<C>>(f: &mut T, Rn: u8, imm: u32, add: bool, pre: bool, wback: bool, colors: &Y) -> Result<(), fmt::Error> {
if imm != 0 {
let op = if add { "" } else { "-" };
@@ -592,7 +596,7 @@ fn format_reg_imm_mem<T: std::fmt::Write>(f: &mut T, Rn: u8, imm: u32, add: bool
}
}
}
-fn reg_name_colorize(num: u8, colors: Option<&ColorSettings>) -> Colored<&'static str> {
+fn reg_name_colorize<C: fmt::Display, Y: YaxColors<C>>(num: u8, colors: &Y) -> impl fmt::Display {
match num {
0 => colors.register("r0"),
1 => colors.register("r1"),
@@ -615,8 +619,8 @@ fn reg_name_colorize(num: u8, colors: Option<&ColorSettings>) -> Colored<&'stati
}
impl Display for Instruction {
- fn fmt(&self, f: &mut Formatter) -> Result<(), std::fmt::Error> {
- self.contextualize(None, 0, None, f)
+ fn fmt(&self, f: &mut Formatter) -> Result<(), fmt::Error> {
+ self.contextualize(&NoColors, 0, Some(&NoContext), f)
}
}
@@ -650,7 +654,7 @@ pub enum ConditionCode {
}
impl Display for ConditionCode {
- fn fmt(&self, f: &mut Formatter) -> Result<(), std::fmt::Error> {
+ fn fmt(&self, f: &mut Formatter) -> Result<(), fmt::Error> {
match self {
ConditionCode::EQ => write!(f, "eq"),
ConditionCode::NE => write!(f, "ne"),
@@ -705,10 +709,6 @@ pub struct InstDecoder {}
impl Decoder<Instruction> for InstDecoder {
type Error = DecodeError;
- fn decode<T: IntoIterator<Item=u8>>(&self, bytes: T) -> Result<Instruction, Self::Error> {
- let mut blank = Instruction::blank();
- self.decode_into(&mut blank, bytes).map(|_: ()| blank)
- }
fn decode_into<T: IntoIterator<Item=u8>>(&self, inst: &mut Instruction, bytes: T) -> Result<(), Self::Error> {
fn read_word<T: IntoIterator<Item=u8>>(bytes: T) -> Result<u32, DecodeError> {
let mut iter = bytes.into_iter();
@@ -797,7 +797,7 @@ impl Decoder<Instruction> for InstDecoder {
((word >> 12) & 0x0f) as u8,
((word >> 16) & 0x0f) as u8
];
- inst.set_S(s);
+ inst.set_s(s);
match op {
0b000 => {
inst.opcode = Opcode::MUL;
@@ -1035,7 +1035,7 @@ impl Decoder<Instruction> for InstDecoder {
unreachable!();
}
inst.opcode = DATA_PROCESSING_OPCODES[opcode as usize];
- inst.set_S(s);
+ inst.set_s(s);
// at this point we know this is a data processing instruction
// either immediate shift or register shift
@@ -1115,7 +1115,7 @@ impl Decoder<Instruction> for InstDecoder {
unreachable!();
}
inst.opcode = DATA_PROCESSING_OPCODES[opcode as usize];
- inst.set_S(s);
+ inst.set_s(s);
let (Rn, imm) = {
let imm = word & 0x0000ffff;
diff --git a/src/armv8/a64.rs b/src/armv8/a64.rs
index 0f2af3a..c32c9d3 100644
--- a/src/armv8/a64.rs
+++ b/src/armv8/a64.rs
@@ -3,7 +3,7 @@
use std::fmt::{self, Display, Formatter};
-use yaxpeax_arch::{Arch, ColorSettings, Decoder, LengthedInstruction, ShowContextual};
+use yaxpeax_arch::{Arch, Decoder, LengthedInstruction, ShowContextual, YaxColors};
#[allow(non_snake_case)]
mod docs {
@@ -154,9 +154,10 @@ impl yaxpeax_arch::Instruction for Instruction {
fn well_defined(&self) -> bool { true }
}
-#[allow(non_snake_case)]
-impl <T: std::fmt::Write> ShowContextual<u64, [Option<String>], T> for Instruction {
- fn contextualize(&self, _colors: Option<&ColorSettings>, _address: u64, _context: Option<&[Option<String>]>, out: &mut T) -> std::fmt::Result {
+struct NoContext;
+
+impl <T: fmt::Write, Color: fmt::Display, Y: YaxColors<Color>> ShowContextual<u64, NoContext, Color, T, Y> for Instruction {
+ fn contextualize(&self, _colors: &Y, _address: u64, _context: Option<&NoContext>, out: &mut T) -> fmt::Result {
write!(out, "{}", self)
}
}
@@ -193,7 +194,7 @@ pub struct Instruction {
}
impl Display for Instruction {
- fn fmt(&self, fmt: &mut Formatter) -> std::fmt::Result {
+ fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
match self.opcode {
Opcode::Invalid => {
write!(fmt, "invalid")?;
@@ -646,8 +647,8 @@ impl LengthedInstruction for Instruction {
fn min_size() -> Self::Unit { 4 }
}
-impl Instruction {
- pub fn blank() -> Self {
+impl Default for Instruction {
+ fn default() -> Self {
Instruction {
opcode: Opcode::Invalid,
operands: [Operand::Nothing, Operand::Nothing, Operand::Nothing, Operand::Nothing]
@@ -792,7 +793,7 @@ pub enum ShiftStyle {
}
impl Display for ShiftStyle {
- fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
+ fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
match self {
ShiftStyle::LSL => { write!(fmt, "lsl") },
ShiftStyle::LSR => { write!(fmt, "lsr") },
@@ -832,7 +833,7 @@ pub enum Operand {
}
impl Display for Operand {
- fn fmt(&self, fmt: &mut Formatter) -> std::fmt::Result {
+ fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
match self {
Operand::Nothing => {
unreachable!();
@@ -1012,10 +1013,6 @@ pub struct InstDecoder {}
impl Decoder<Instruction> for InstDecoder {
type Error = DecodeError;
- fn decode<T: IntoIterator<Item=u8>>(&self, bytes: T) -> Result<Instruction, Self::Error> {
- let mut blank = Instruction::blank();
- self.decode_into(&mut blank, bytes).map(|_: ()| blank)
- }
fn decode_into<T: IntoIterator<Item=u8>>(&self, inst: &mut Instruction, bytes: T) -> Result<(), Self::Error> {
fn read_word<T: IntoIterator<Item=u8>>(bytes: T) -> Result<u32, DecodeError> {
let mut iter = bytes.into_iter();
@@ -1871,9 +1868,9 @@ impl Decoder<Instruction> for InstDecoder {
0b00001 => {
let Rt = (word & 0x1f) as u16;
let Rn = ((word >> 5) & 0x1f) as u16;
- let Rt2 = ((word >> 10) & 0x1f) as u16;
+ let _Rt2 = ((word >> 10) & 0x1f) as u16;
let o0 = (word >> 15) & 1;
- let Rs = (word >> 16) & 0x1f;
+ let _Rs = (word >> 16) & 0x1f;
let Lo1 = (word >> 21) & 0x3;
let size = (word >> 30) & 0x3;
// load/store exclusive
diff --git a/test/armv7.rs b/test/armv7.rs
index fbd71ee..f16a44a 100644
--- a/test/armv7.rs
+++ b/test/armv7.rs
@@ -400,7 +400,7 @@ pub fn bench_60000_instrs(b: &mut Bencher) {
for i in (0..1000) {
let mut iter = instruction_bytes.iter().map(|x| *x);
let decoder = <ARMv7 as Arch>::Decoder::default();
- let mut result = Instruction::blank();
+ let mut result = Instruction::default();
loop {
match decoder.decode_into(&mut result, &mut iter) {
Ok(result) => {
diff --git a/test/armv8/a64.rs b/test/armv8/a64.rs
index 406b867..be2c5bf 100644
--- a/test/armv8/a64.rs
+++ b/test/armv8/a64.rs
@@ -2332,7 +2332,7 @@ pub fn bench_60000_instrs(b: &mut Bencher) {
for i in (0..1000) {
let mut iter = instruction_bytes.iter().map(|x| *x);
let decoder = <ARMv8 as Arch>::Decoder::default();
- let mut result = Instruction::blank();
+ let mut result = Instruction::default();
loop {
match decoder.decode_into(&mut result, &mut iter) {
Ok(result) => {