#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
pub struct Gpr(pub u8);
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
pub struct Fpr(pub u8);
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
pub struct Label(pub u32);
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
pub struct SymId(pub u32);
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
pub struct ExternId(pub u32);
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
pub enum Reach {
Near,
Far,
}
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
pub struct Extern {
pub id: ExternId,
pub reach: Reach,
}
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
pub enum Lbl {
Bound(Label),
Fresh(u32),
}
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
pub enum Disp {
Const(i32),
Sym(SymId),
Label(Label),
LabelFar(Label),
}
impl Disp {
pub const fn constant(self) -> Option<i32> {
match self {
Disp::Const(v) => Some(v),
Disp::Sym(_) | Disp::Label(_) | Disp::LabelFar(_) => None,
}
}
pub const fn const_or_zero(self) -> i32 {
match self {
Disp::Const(v) => v,
Disp::Sym(_) | Disp::Label(_) | Disp::LabelFar(_) => 0,
}
}
pub const fn label(self) -> Option<Label> {
match self {
Disp::Label(l) | Disp::LabelFar(l) => Some(l),
Disp::Const(_) | Disp::Sym(_) => None,
}
}
}
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
pub struct Mem {
pub base: Option<Gpr>,
pub index: Option<(Gpr, u8)>,
pub disp: Disp,
}
impl Mem {
pub fn base(base: Gpr, disp: i32) -> Self {
Self {
base: Some(base),
index: None,
disp: Disp::Const(disp),
}
}
pub fn absolute(disp: Disp) -> Self {
Self {
base: None,
index: None,
disp,
}
}
pub fn label(label: Label) -> Self {
Self::absolute(Disp::Label(label))
}
pub fn label_far(label: Label) -> Self {
Self::absolute(Disp::LabelFar(label))
}
pub fn base_reg(&self) -> Gpr {
self.base.unwrap_or(Gpr(0))
}
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum Op {
Imm(i64),
Gpr(Gpr),
Fpr(Fpr),
Mem(Mem),
Label(Lbl),
Sym(SymId),
Extern(Extern),
}
impl Op {
pub fn is_zero_imm(&self) -> bool {
matches!(self, Op::Imm(0))
}
}
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
pub enum Width {
B8,
B16,
B32,
B64,
}
impl Width {
pub fn bytes(self) -> u32 {
match self {
Width::B8 => 1,
Width::B16 => 2,
Width::B32 => 4,
Width::B64 => 8,
}
}
pub fn fits(self, v: i64) -> bool {
match self {
Width::B8 => v == (v as i8) as i64,
Width::B16 => v == (v as i16) as i64,
Width::B32 => v == (v as i32) as i64,
Width::B64 => true,
}
}
}
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
pub enum FKind {
F32,
F64,
}
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
pub enum IntCc {
Eq,
Ne,
Hi,
Hs,
Lo,
Ls,
Gt,
Ge,
Lt,
Le,
}
#[allow(dead_code)]
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
pub enum FCc {
Eq,
Ne,
Gt,
Ge,
Lt,
Le,
EqUn,
NeUn,
GtUn,
GeUn,
LtUn,
LeUn,
}
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
pub enum PredFlag {
O,
S,
Z,
Nz,
}
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
pub enum TestKind {
Sign,
Zero,
NonZero,
}
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
pub enum ShiftOp {
Shl,
ShrSar,
Shr,
Rotl,
Rotr,
}
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
pub enum RoundMode {
Nearest,
Down,
Up,
Trunc,
}
#[derive(Clone, PartialEq, Eq, Debug)]
pub enum Target {
Label(Lbl),
Reg(Gpr),
Mem(Mem),
Abs(i64),
Extern(Extern),
}
#[derive(Clone, Copy, Debug)]
pub struct Scratch {
pub gpr: Gpr,
pub gpr2: Gpr,
pub fpr: Fpr,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum ExpandError {
UnknownMnemonic(String),
BadOperand { mnemonic: String, msg: String },
NotPatchable(String),
}
impl ExpandError {
pub fn renamed(self, spelling: &str) -> Self {
match self {
ExpandError::UnknownMnemonic(_) => ExpandError::UnknownMnemonic(spelling.to_string()),
ExpandError::BadOperand { msg, .. } => ExpandError::BadOperand {
mnemonic: spelling.to_string(),
msg,
},
ExpandError::NotPatchable(_) => ExpandError::NotPatchable(spelling.to_string()),
}
}
}
impl std::fmt::Display for ExpandError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
ExpandError::UnknownMnemonic(m) => write!(f, "unknown mnemonic `{}`", m),
ExpandError::BadOperand { mnemonic, msg } => {
write!(f, "`{}`: bad operand: {}", mnemonic, msg)
}
ExpandError::NotPatchable(m) => {
write!(
f,
"`{}` cannot be a patch site (use jmp/call/branch/move-imm)",
m
)
}
}
}
}
impl std::error::Error for ExpandError {}
pub mod expand;
pub mod inst;
pub mod mnemonic;
pub mod regs;
pub mod riscv;
pub use expand::{
aligned_push_bytes, aligned_push_offset, check_callee_saved, expand, patch_kind, Expansion,
PatchKind,
};
pub use inst::*;
pub use mnemonic::{mnem, LeaForm, Mnemonic, SUPPORTED_MNEMONICS};
pub use regs::{RegInfo, RegKind, TargetRegs, VReg, TARGETS};
const _: () = {
assert!(
core::mem::size_of::<Op>() <= 16,
"Op grew past the 16-byte inline budget"
);
assert!(
core::mem::size_of::<Inst>() <= 72,
"Inst grew past the 72-byte inline budget"
);
assert!(
core::mem::size_of::<BranchArgs>() <= 56,
"BranchArgs grew past the 56-byte inline budget"
);
};