aboutsummaryrefslogtreecommitdiff
path: root/src/lib.rs
diff options
context:
space:
mode:
authoriximeow <me@iximeow.net>2021-07-03 19:36:15 -0700
committeriximeow <me@iximeow.net>2021-07-03 19:36:15 -0700
commit041ef2605f5de35e268bd0f82d9a6240a9a58e61 (patch)
treeefb4e4d42780b46df931e55f6df7bfd0450c5c79 /src/lib.rs
parent3863d7023e1c7ae71f989e172200898d8a658f71 (diff)
add a Reader type that can read architecture-defined words
this is useful for instruction sets like arm where instructions are guaranteed to be 4 bytes, as well as instruction sets where an instruction word is not a multiple of u8 bytes.
Diffstat (limited to 'src/lib.rs')
-rw-r--r--src/lib.rs13
1 files changed, 8 insertions, 5 deletions
diff --git a/src/lib.rs b/src/lib.rs
index 89b3579..1862993 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -31,6 +31,8 @@ pub use color::{Colorize, NoColors, YaxColors};
pub use color::ColorSettings;
pub mod display;
+mod reader;
+pub use reader::{Reader, ReadError, U8Reader}; //, U16le, U16be, U32le, U32be, U64le, U64be};
pub trait DecodeError {
fn data_exhausted(&self) -> bool;
@@ -61,12 +63,13 @@ impl DecodeError for StandardDecodeError {
fn bad_operand(&self) -> bool { *self == StandardDecodeError::InvalidOperand }
}
- fn decode<T: Reader<A::Word>>(&self, bytes: &mut T) -> Result<A::Instruction, Self::Error> {
+pub trait Decoder<A: Arch + ?Sized> {
+ fn decode<T: Reader<A::Address, A::Word>>(&self, words: &mut T) -> Result<A::Instruction, A::DecodeError> {
let mut inst = A::Instruction::default();
- self.decode_into(&mut inst, bytes).map(|_: ()| inst)
+ self.decode_into(&mut inst, words).map(|_: ()| inst)
}
- fn decode_into<T: Reader<A::Word>>(&self, inst: &mut A::Instruction, bytes: &mut T) -> Result<(), Self::Error>;
+ fn decode_into<T: Reader<A::Address, A::Word>>(&self, inst: &mut A::Instruction, words: &mut T) -> Result<(), A::DecodeError>;
}
#[cfg(feature="use-serde")]
@@ -75,7 +78,7 @@ pub trait Arch {
type Address: Address + Debug + Hash + PartialEq + Eq + Serialize + for<'de> Deserialize<'de>;
type Instruction: Instruction + LengthedInstruction<Unit=AddressDiff<Self::Address>> + Debug + Default + Sized;
type DecodeError: DecodeError + Debug + Display;
- type Decoder: Decoder<Self, Error=Self::DecodeError> + Default;
+ type Decoder: Decoder<Self> + Default;
type Operand;
}
@@ -85,7 +88,7 @@ pub trait Arch {
type Address: Address + Debug + Hash + PartialEq + Eq;
type Instruction: Instruction + LengthedInstruction<Unit=AddressDiff<Self::Address>> + Debug + Default + Sized;
type DecodeError: DecodeError + Debug + Display;
- type Decoder: Decoder<Self, Error=Self::DecodeError> + Default;
+ type Decoder: Decoder<Self> + Default;
type Operand;
}