120 lines
3.1 KiB
Rust
120 lines
3.1 KiB
Rust
#[derive(Clone, Copy)]
|
|
pub struct Reg(u8);
|
|
|
|
#[derive(Clone, Copy)]
|
|
pub struct RegMode {
|
|
pub reg: Reg,
|
|
pub width: BitWidth,
|
|
pub high: bool,
|
|
}
|
|
|
|
#[derive(Clone, Copy, PartialEq)]
|
|
pub enum BitWidth {
|
|
B64,
|
|
B32,
|
|
B16,
|
|
B8,
|
|
}
|
|
|
|
impl Reg {
|
|
pub fn base(&self) -> u8 {
|
|
self.0 & 0b111
|
|
}
|
|
/// checks if register is not one of the first 8 (0-7)
|
|
pub fn gt8(&self) -> bool {
|
|
self.0 >= 0b1000
|
|
}
|
|
pub fn gt4(&self) -> bool {
|
|
self.0 >= 0b0100
|
|
}
|
|
}
|
|
|
|
impl RegMode {
|
|
pub fn base(&self) -> u8 {
|
|
self.reg.base()
|
|
}
|
|
/// checks if register is not one of the first 8 (0-7)
|
|
pub fn gt8(&self) -> bool {
|
|
self.reg.gt8()
|
|
}
|
|
pub fn gt4(&self) -> bool {
|
|
self.reg.gt4()
|
|
}
|
|
}
|
|
|
|
def_regs! {
|
|
0b0000 : rax eax ax al ah=spl,
|
|
0b0001 : rcx ecx cx cl ch=bpl,
|
|
0b0010 : rdx edx dx dl dh=sil,
|
|
0b0011 : rbx ebx bx bl bh=dil,
|
|
|
|
0b0100 : rsp esp sp spl,
|
|
0b0101 : rbp ebp bp bpl,
|
|
0b0110 : rsi esi si sil,
|
|
0b0111 : rdi edi di dil,
|
|
|
|
0b1000 : r8 r8d r8w r8b,
|
|
0b1001 : r9 r9d r9w r9b,
|
|
0b1010 : r10 r10d r10w r10b,
|
|
0b1011 : r11 r11d r11w r11b,
|
|
0b1100 : r12 r12d r12w r12b,
|
|
0b1101 : r13 r13d r13w r13b,
|
|
0b1110 : r14 r14d r14w r14b,
|
|
0b1111 : r15 r15d r15w r15b,
|
|
}
|
|
|
|
impl BitWidth {
|
|
pub const fn max(&self) -> u64 {
|
|
match self {
|
|
Self::B64 => u64::MAX,
|
|
Self::B32 => u32::MAX as u64,
|
|
Self::B16 => u16::MAX as u64,
|
|
Self::B8 => u8::MAX as u64,
|
|
}
|
|
}
|
|
pub const fn bytes(&self) -> usize {
|
|
match self {
|
|
Self::B64 => 8,
|
|
Self::B32 => 4,
|
|
Self::B16 => 2,
|
|
Self::B8 => 1,
|
|
}
|
|
}
|
|
}
|
|
|
|
macro_rules! def_regs {
|
|
($($val:literal : $B64:ident $B32:ident $B16:ident $B8:ident $($B8H:ident=$hval:expr)?,)*) => {
|
|
$(
|
|
#[allow(non_upper_case_globals)]
|
|
pub const $B64: RegMode = RegMode { reg: Reg($val), width: BitWidth::B64, high: false };
|
|
#[allow(non_upper_case_globals)]
|
|
pub const $B32: RegMode = RegMode { reg: Reg($val), width: BitWidth::B32, high: false };
|
|
#[allow(non_upper_case_globals)]
|
|
pub const $B16: RegMode = RegMode { reg: Reg($val), width: BitWidth::B16, high: false };
|
|
#[allow(non_upper_case_globals)]
|
|
pub const $B8 : RegMode = RegMode { reg: Reg($val), width: BitWidth::B8, high: false };
|
|
$(
|
|
#[allow(non_upper_case_globals)]
|
|
pub const $B8H: RegMode = RegMode { reg: $hval.reg, width: BitWidth::B8, high: true };
|
|
)?
|
|
)*
|
|
impl RegMode {
|
|
pub fn parse(s: &str) -> Option<Self> {
|
|
Some(match s.to_lowercase().as_str() {
|
|
$(
|
|
stringify!($B64) => $B64,
|
|
stringify!($B32) => $B32,
|
|
stringify!($B16) => $B16,
|
|
stringify!($B8 ) => $B8,
|
|
$(
|
|
stringify!($B8H) => $B8H,
|
|
)?
|
|
)*
|
|
_ => return None,
|
|
})
|
|
}
|
|
}
|
|
};
|
|
}
|
|
use def_regs;
|