use core::num::NonZeroUsize;
use gdbstub::arch::RegId;
#[derive(Debug, Clone, Copy)]
pub enum X87FpuInternalRegId {
Fctrl,
Fstat,
Ftag,
Fiseg,
Fioff,
Foseg,
Fooff,
Fop,
}
impl X87FpuInternalRegId {
fn from_u8(val: u8) -> Option<Self> {
use self::X87FpuInternalRegId::*;
let r = match val {
0 => Fctrl,
1 => Fstat,
2 => Ftag,
3 => Fiseg,
4 => Fioff,
5 => Foseg,
6 => Fooff,
7 => Fop,
_ => return None,
};
Some(r)
}
}
#[derive(Debug, Clone, Copy)]
#[allow(clippy::upper_case_acronyms)]
pub enum X86SegmentRegId {
CS,
SS,
DS,
ES,
FS,
GS,
}
impl X86SegmentRegId {
fn from_u8(val: u8) -> Option<Self> {
use self::X86SegmentRegId::*;
let r = match val {
0 => CS,
1 => SS,
2 => DS,
3 => ES,
4 => FS,
5 => GS,
_ => return None,
};
Some(r)
}
}
#[derive(Debug, Clone, Copy)]
#[non_exhaustive]
pub enum X86CoreRegId {
Eax,
Ecx,
Edx,
Ebx,
Esp,
Ebp,
Esi,
Edi,
Eip,
Eflags,
Segment(X86SegmentRegId),
St(u8),
Fpu(X87FpuInternalRegId),
Xmm(u8),
Mxcsr,
}
impl RegId for X86CoreRegId {
fn from_raw_id(id: usize) -> Option<(Self, Option<NonZeroUsize>)> {
use self::X86CoreRegId::*;
let (r, sz): (X86CoreRegId, usize) = match id {
0 => (Eax, 4),
1 => (Ecx, 4),
2 => (Edx, 4),
3 => (Ebx, 4),
4 => (Esp, 4),
5 => (Ebp, 4),
6 => (Esi, 4),
7 => (Edi, 4),
8 => (Eip, 4),
9 => (Eflags, 4),
10..=15 => (Segment(X86SegmentRegId::from_u8(id as u8 - 10)?), 4),
16..=23 => (St(id as u8 - 16), 10),
24..=31 => (Fpu(X87FpuInternalRegId::from_u8(id as u8 - 24)?), 4),
32..=39 => (Xmm(id as u8 - 32), 16),
40 => (Mxcsr, 4),
_ => return None,
};
Some((r, Some(NonZeroUsize::new(sz)?)))
}
}
#[derive(Debug, Clone, Copy)]
#[non_exhaustive]
pub enum X86_64CoreRegId {
Gpr(u8),
Rip,
Eflags,
Segment(X86SegmentRegId),
St(u8),
Fpu(X87FpuInternalRegId),
Xmm(u8),
Mxcsr,
}
impl RegId for X86_64CoreRegId {
fn from_raw_id(id: usize) -> Option<(Self, Option<NonZeroUsize>)> {
use self::X86_64CoreRegId::*;
let (r, sz): (X86_64CoreRegId, usize) = match id {
0..=15 => (Gpr(id as u8), 8),
16 => (Rip, 8),
17 => (Eflags, 4),
18..=23 => (Segment(X86SegmentRegId::from_u8(id as u8 - 18)?), 4),
24..=31 => (St(id as u8 - 24), 10),
32..=39 => (Fpu(X87FpuInternalRegId::from_u8(id as u8 - 32)?), 4),
40..=55 => (Xmm(id as u8 - 40), 16),
56 => (Mxcsr, 4),
_ => return None,
};
Some((r, Some(NonZeroUsize::new(sz)?)))
}
}
#[cfg(test)]
mod tests {
use gdbstub::arch::RegId;
use gdbstub::arch::Registers;
fn test<Rs: Registers, RId: RegId>() {
let mut serialized_data_len = 0;
let counter = |b: Option<u8>| {
if b.is_some() {
serialized_data_len += 1;
}
};
Rs::default().gdb_serialize(counter);
let mut i = 0;
let mut sum_reg_sizes = 0;
while let Some((_, size)) = RId::from_raw_id(i) {
sum_reg_sizes += size.unwrap().get();
i += 1;
}
assert_eq!(serialized_data_len, sum_reg_sizes);
}
#[test]
fn test_x86() {
test::<crate::x86::reg::X86CoreRegs, crate::x86::reg::id::X86CoreRegId>()
}
#[test]
fn test_x86_64() {
test::<crate::x86::reg::X86_64CoreRegs, crate::x86::reg::id::X86_64CoreRegId>()
}
}