use crate::{Code, Mnemonic};
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash)]
pub enum OperandKind {
#[default]
InlineNone,
ShortInlineI,
InlineI,
InlineI8,
ShortInlineR,
InlineR,
ShortInlineBrTarget,
InlineBrTarget,
InlineSwitch,
InlineMethod,
InlineField,
InlineType,
InlineTok,
InlineString,
InlineSig,
ShortInlineVar,
InlineVar,
InlineBytes(u16),
InlineData,
ShortInlineData,
}
impl OperandKind {
pub const fn fixed_size(self) -> Option<usize> {
Some(match self {
Self::InlineNone => 0,
Self::ShortInlineI | Self::ShortInlineBrTarget | Self::ShortInlineVar => 1,
Self::InlineVar => 2,
Self::InlineI8 | Self::InlineR => 8,
Self::InlineSwitch | Self::InlineData | Self::ShortInlineData => return None,
Self::InlineBytes(size) => size as usize,
_ => 4,
})
}
pub const fn is_token(self) -> bool {
matches!(
self,
Self::InlineMethod
| Self::InlineField
| Self::InlineType
| Self::InlineTok
| Self::InlineString
| Self::InlineSig
)
}
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash)]
pub enum FlowControl {
#[default]
Next,
Branch,
CondBranch,
Call,
Return,
Throw,
Break,
Meta,
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash)]
pub enum OpCodeKind {
#[default]
Primitive,
Macro,
ObjModel,
Prefix,
Reserved,
Mono,
MonoLegacy,
MonoEarly,
Custom,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct OpCodeInfo {
pub code: Code,
pub mnemonic: Mnemonic,
pub name: &'static str,
pub encoding: u16,
pub size: u8,
pub operand: OperandKind,
pub pop: StackBehaviourPop,
pub push: StackBehaviourPush,
pub flow_control: FlowControl,
pub kind: OpCodeKind,
}
impl OpCodeInfo {
pub const fn is_prefix(&self) -> bool {
matches!(self.kind, OpCodeKind::Prefix)
}
pub const fn is_reserved(&self) -> bool {
matches!(self.kind, OpCodeKind::Reserved)
}
pub const fn is_extension(&self) -> bool {
matches!(
self.kind,
OpCodeKind::Mono | OpCodeKind::MonoLegacy | OpCodeKind::MonoEarly | OpCodeKind::Custom
)
}
pub const fn custom(encoding: u16, size: u8, name: &'static str, operand: OperandKind) -> Self {
Self {
code: Code::Custom,
mnemonic: Mnemonic::Custom,
name,
encoding,
size,
operand,
pop: StackBehaviourPop::VarPop,
push: StackBehaviourPush::VarPush,
flow_control: FlowControl::Next,
kind: OpCodeKind::Custom,
}
}
pub const fn encoding_bytes(&self) -> [u8; 2] {
if self.size == 1 {
[self.encoding as u8, 0]
} else {
[(self.encoding >> 8) as u8, self.encoding as u8]
}
}
}
pub use crate::generated::{StackBehaviourPop, StackBehaviourPush};