use super::*;
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct Alu3 {
pub dst: Op,
pub lhs: Op,
pub rhs: Op,
}
impl Alu3 {
pub fn from_operands(mn: Mnemonic, ops: &[Op]) -> Result<Self, ExpandError> {
match ops {
[dst, rhs] => Ok(Alu3 {
dst: *dst,
lhs: *dst,
rhs: *rhs,
}),
[dst, lhs, rhs] => Ok(Alu3 {
dst: *dst,
lhs: *lhs,
rhs: *rhs,
}),
_ => Err(bad(mn, "takes 2 or 3 operands")),
}
}
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub struct BranchArgs {
ops: [Op; 3],
len: u8,
}
impl BranchArgs {
pub fn two(rhs: Op, dst: Op) -> Self {
Self {
ops: [rhs, dst, Op::Imm(0)],
len: 2,
}
}
pub fn three(lhs: Op, rhs: Op, dst: Op) -> Self {
Self {
ops: [lhs, rhs, dst],
len: 3,
}
}
pub fn as_slice(&self) -> &[Op] {
&self.ops[..self.len as usize]
}
pub fn as_mut_slice(&mut self) -> &mut [Op] {
&mut self.ops[..self.len as usize]
}
}
#[derive(Clone, PartialEq, Eq, Debug)]
pub enum Inst {
Mov {
w: Width,
dst: Op,
src: Op,
patch: bool,
},
Bind(Lbl),
MovZero32 {
dst: Op,
src: Op,
},
Bswap {
w: Width,
dst: Op,
src: Op,
},
Load {
w: Width,
sx: Sx,
dst: Gpr,
addr: Mem,
acquire: bool,
},
Store {
w: Width,
src: Op,
addr: Mem,
},
LoadF {
k: FKind,
dst: Fpr,
addr: Mem,
},
StoreF {
k: FKind,
src: Fpr,
addr: Mem,
},
FMov {
k: FKind,
dst: Fpr,
src: Fpr,
},
LoadV {
dst: Fpr,
addr: Mem,
},
StoreV {
src: Fpr,
addr: Mem,
},
VMov {
dst: Fpr,
src: Fpr,
},
Lea {
w: Width,
dst: Gpr,
src: LeaSrc,
},
Push {
op: Op,
},
Pop {
op: Op,
},
PushV {
src: Fpr,
},
PopV {
dst: Fpr,
},
Peek {
disp: i32,
dst: Gpr,
},
Poke {
disp: i32,
src: Gpr,
},
Add {
w: Width,
op: Alu3,
},
Sub {
w: Width,
op: Alu3,
},
Mul {
w: Width,
op: Alu3,
},
And {
w: Width,
op: Alu3,
},
Or {
w: Width,
op: Alu3,
},
Xor {
w: Width,
op: Alu3,
},
Not {
w: Width,
dst: Op,
},
Neg {
w: Width,
dst: Op,
},
Shift {
w: Width,
op: ShiftOp,
dst: Gpr,
cnt: ShiftCnt,
},
MulImm {
w: Width,
dst: Gpr,
lhs: Gpr,
imm: i64,
},
AluS {
op: AluOp,
w: Width,
alu: Alu3,
},
Adc {
dst: Op,
src: Op,
},
Sbb {
dst: Op,
src: Op,
},
MulWide {
signed: bool,
dst: Gpr,
lhs: Gpr,
rhs: Gpr,
},
MulHigh {
signed: bool,
rhs: Op,
},
DivMod1 {
signed: bool,
w: Width,
divisor: Op,
},
Div3 {
signed: bool,
w: Width,
dst: Gpr,
dvd: Op,
dvs: Op,
},
Rem3 {
signed: bool,
w: Width,
dst: Gpr,
dvd: Op,
dvs: Op,
},
Extend {
from: Width,
to: Width,
sx: Sx,
dst: Gpr,
src: Op,
},
Ctz {
w: Width,
dst: Gpr,
src: Op,
},
Clz {
w: Width,
dst: Gpr,
src: Op,
},
Popcnt {
w: Width,
dst: Gpr,
src: Op,
},
Cqo {
quad: bool,
},
AddShifted {
dst: Gpr,
lhs: Gpr,
rhs: Gpr,
amount: u8,
},
Cmov {
w: Width,
zero: bool,
dst: Gpr,
src: Op,
test: Gpr,
},
VecExtract {
w: Width,
dst: Gpr,
src: Fpr,
lane: u8,
},
Branch {
cc: IntCc,
w: Width,
lhs: Op,
rhs: Op,
target: Lbl,
patch: bool,
},
TestBranch {
w: Width,
value: Op,
mask: Option<Op>,
test: TestKind,
target: Lbl,
patch: bool,
},
OpBranch {
op: FlagAlu,
w: Width,
flag: PredFlag,
args: BranchArgs,
target: Lbl,
patch: bool,
},
FlagBranch {
flag: PredFlag,
target: Lbl,
patch: bool,
},
Jump {
target: Target,
patch: bool,
},
Call {
target: Target,
patch: bool,
},
Ret,
Break,
Nop,
IntSet {
cc: IntCc,
w: Width,
dst: Op,
lhs: Op,
rhs: Op,
},
TestSet {
w: Width,
value: Op,
mask: Option<Op>,
test: TestKind,
dst: Op,
},
FArith {
op: FAlu,
k: FKind,
arith: FArith3,
},
Fma {
op: FmaOp,
k: FKind,
a: Op,
b: Op,
c: Op,
dst: Fpr,
},
FSqrt {
k: FKind,
dst: Fpr,
src: Op,
},
FRound {
k: FKind,
mode: RoundMode,
dst: Fpr,
src: Op,
},
FAbs {
k: FKind,
dst: Fpr,
src: Op,
},
FNeg {
k: FKind,
dst: Fpr,
src: Op,
},
FBit {
or: bool,
k: FKind,
dst: Fpr,
src: Op,
},
CvtFI {
k: FKind,
w: Width,
dst: Gpr,
src: Op,
},
CvtIF {
signed: bool,
from: Width,
k: FKind,
dst: Fpr,
src: Op,
},
CvtFF {
from: FKind,
to: FKind,
dst: Fpr,
src: Op,
},
Bitcast {
dir: BitcastDir,
dst: Op,
src: Op,
},
BranchF {
cc: FCc,
k: FKind,
lhs: Op,
rhs: Op,
target: Lbl,
patch: bool,
},
FSet {
cc: FCc,
k: FKind,
dst: Op,
lhs: Op,
rhs: Op,
},
Fence {
full: bool,
},
Pause,
TlsLoad {
w: Width,
dst: Gpr,
idx: i64,
},
TlsStore {
w: Width,
src: Gpr,
idx: i64,
},
TlsLea {
dst: Gpr,
idx: i64,
},
AtomicRmw {
op: AtomicOp,
w: Width,
a: Op,
b: Op,
},
AtomicCas {
w: Width,
mem: Mem,
src: Gpr,
branch: Option<Lbl>,
},
LoadLinked {
w: Width,
mem: Mem,
dst: Gpr,
},
StoreCond {
w: Width,
status: Gpr,
src: Gpr,
mem: Mem,
},
PairLoad {
w: PairW,
dst1: Op,
dst2: Op,
mem: Mem,
},
PairStore {
w: PairW,
src1: Op,
src2: Op,
mem: Mem,
},
}
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
pub enum Sx {
Zero,
Sign,
}
#[derive(Clone, PartialEq, Eq, Debug)]
pub enum ShiftCnt {
Imm(i64),
Reg(Gpr),
}
#[derive(Clone, PartialEq, Eq, Debug)]
pub enum LeaSrc {
Mem(Mem),
Label(Lbl),
Extern(Extern),
}
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
pub enum FlagAlu {
Add,
Sub,
Mul,
Or,
}
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
pub enum AluOp {
Add,
Sub,
Or,
}
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
pub enum FAlu {
Add,
Sub,
Mul,
Div,
Min,
Max,
}
impl FAlu {
pub const fn is_commutative(self) -> bool {
matches!(self, FAlu::Add | FAlu::Mul | FAlu::Min | FAlu::Max)
}
}
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
pub enum FmaOp {
Add,
Sub,
NegAdd,
NegSub,
}
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct FArith3 {
pub dst: Fpr,
pub lhs: Op,
pub rhs: Option<Op>,
}
impl FArith3 {
pub fn from_operands(mn: Mnemonic, ops: &[Op]) -> Result<Self, ExpandError> {
match ops {
[dst, lhs] => Ok(FArith3 {
dst: fpr_op(mn, dst)?,
lhs: *lhs,
rhs: None,
}),
_ => Err(bad(mn, "takes 2 operands")),
}
}
}
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
pub enum BitcastDir {
Q2D,
D2Q,
I2F,
F2I,
}
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
pub enum AtomicOp {
Add,
Sub,
Xchg,
And,
Or,
Xor,
}
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
pub enum PairW {
B32,
B64,
F64,
V128,
}
pub fn bad(mn: Mnemonic, msg: impl Into<String>) -> ExpandError {
ExpandError::BadOperand {
mnemonic: mn.as_str().to_string(),
msg: msg.into(),
}
}
pub fn expect_len(mn: Mnemonic, ops: &[Op], n: usize) -> Result<(), ExpandError> {
if ops.len() == n {
Ok(())
} else {
Err(bad(
mn,
format!("expected {} operands, got {}", n, ops.len()),
))
}
}
pub fn imm_op(mn: Mnemonic, op: &Op) -> Result<i64, ExpandError> {
match op {
Op::Imm(v) => Ok(*v),
_ => Err(bad(mn, "expected immediate")),
}
}
pub fn gpr_op(mn: Mnemonic, op: &Op) -> Result<Gpr, ExpandError> {
match op {
Op::Gpr(g) => Ok(*g),
_ => Err(bad(mn, "expected GPR")),
}
}
pub fn fpr_op(mn: Mnemonic, op: &Op) -> Result<Fpr, ExpandError> {
match op {
Op::Fpr(x) => Ok(*x),
_ => Err(bad(mn, "expected FPR")),
}
}
pub fn mem_op(mn: Mnemonic, op: &Op) -> Result<Mem, ExpandError> {
match op {
Op::Mem(a) => Ok(*a),
_ => Err(bad(mn, "expected memory")),
}
}
pub fn label_op(mn: Mnemonic, op: &Op) -> Result<Lbl, ExpandError> {
match op {
Op::Label(l) => Ok(*l),
_ => Err(bad(mn, "expected label")),
}
}
pub fn op_eq(a: &Op, b: &Op) -> bool {
match (a, b) {
(Op::Imm(x), Op::Imm(y)) => x == y,
(Op::Gpr(x), Op::Gpr(y)) => x == y,
(Op::Fpr(x), Op::Fpr(y)) => x == y,
_ => false,
}
}
impl Inst {
pub fn visit_labels(&self, f: &mut dyn FnMut(Lbl)) {
fn visit_op(f: &mut dyn FnMut(Lbl), o: &Op) {
if let Op::Label(l) = o {
f(*l);
}
}
fn visit_target(f: &mut dyn FnMut(Lbl), t: &Target) {
if let Target::Label(l) = t {
f(*l);
}
}
fn visit_lea(f: &mut dyn FnMut(Lbl), s: &LeaSrc) {
if let LeaSrc::Label(l) = s {
f(*l);
}
}
fn visit_ops(f: &mut dyn FnMut(Lbl), ops: &[Op]) {
for o in ops {
visit_op(f, o);
}
}
match self {
Inst::Mov { dst, src, .. }
| Inst::MovZero32 { dst, src }
| Inst::Bswap { dst, src, .. }
| Inst::Adc { dst, src }
| Inst::Sbb { dst, src }
| Inst::Bitcast { dst, src, .. } => {
visit_op(f, dst);
visit_op(f, src);
}
Inst::IntSet { dst, lhs, rhs, .. } | Inst::FSet { dst, lhs, rhs, .. } => {
visit_op(f, dst);
visit_op(f, lhs);
visit_op(f, rhs);
}
Inst::Bind(l) => f(*l),
Inst::Lea { src, .. } => visit_lea(f, src),
Inst::Store { src, .. } => visit_op(f, src),
Inst::Push { op } | Inst::Pop { op } => visit_op(f, op),
Inst::Add { op: a, .. }
| Inst::Sub { op: a, .. }
| Inst::Mul { op: a, .. }
| Inst::And { op: a, .. }
| Inst::Or { op: a, .. }
| Inst::Xor { op: a, .. } => {
visit_op(f, &a.dst);
visit_op(f, &a.lhs);
visit_op(f, &a.rhs);
}
Inst::AluS { alu, .. } => {
visit_op(f, &alu.dst);
visit_op(f, &alu.lhs);
visit_op(f, &alu.rhs);
}
Inst::Not { dst, .. } | Inst::Neg { dst, .. } => visit_op(f, dst),
Inst::MulHigh { rhs, .. } => visit_op(f, rhs),
Inst::DivMod1 { divisor, .. } => visit_op(f, divisor),
Inst::Div3 { dvd, dvs, .. } | Inst::Rem3 { dvd, dvs, .. } => {
visit_op(f, dvd);
visit_op(f, dvs);
}
Inst::Branch {
lhs, rhs, target, ..
} => {
visit_op(f, lhs);
visit_op(f, rhs);
f(*target);
}
Inst::TestBranch {
value,
mask,
target,
..
} => {
visit_op(f, value);
if let Some(m) = mask {
visit_op(f, m);
}
f(*target);
}
Inst::OpBranch { args, target, .. } => {
visit_ops(f, args.as_slice());
f(*target);
}
Inst::FlagBranch { target, .. } => f(*target),
Inst::Jump { target, .. } | Inst::Call { target, .. } => visit_target(f, target),
Inst::TestSet {
value, mask, dst, ..
} => {
visit_op(f, value);
if let Some(m) = mask {
visit_op(f, m);
}
visit_op(f, dst);
}
Inst::FArith { arith, .. } => {
visit_op(f, &arith.lhs);
if let Some(r) = &arith.rhs {
visit_op(f, r);
}
}
Inst::Fma { a, b, c, .. } => {
visit_op(f, a);
visit_op(f, b);
visit_op(f, c);
}
Inst::FSqrt { src, .. }
| Inst::FRound { src, .. }
| Inst::FAbs { src, .. }
| Inst::FNeg { src, .. }
| Inst::FBit { src, .. }
| Inst::CvtFI { src, .. }
| Inst::CvtIF { src, .. }
| Inst::CvtFF { src, .. } => visit_op(f, src),
Inst::BranchF {
lhs, rhs, target, ..
} => {
visit_op(f, lhs);
visit_op(f, rhs);
f(*target);
}
Inst::AtomicRmw { a, b, .. } => {
visit_op(f, a);
visit_op(f, b);
}
Inst::Load { .. }
| Inst::LoadF { .. }
| Inst::StoreF { .. }
| Inst::FMov { .. }
| Inst::LoadV { .. }
| Inst::StoreV { .. }
| Inst::VMov { .. }
| Inst::PushV { .. }
| Inst::PopV { .. }
| Inst::Peek { .. }
| Inst::Poke { .. }
| Inst::Shift { .. }
| Inst::MulImm { .. }
| Inst::Extend { .. }
| Inst::Ctz { .. }
| Inst::Clz { .. }
| Inst::Popcnt { .. }
| Inst::Cqo { .. }
| Inst::MulWide { .. }
| Inst::AddShifted { .. }
| Inst::Cmov { .. }
| Inst::VecExtract { .. }
| Inst::Fence { .. }
| Inst::Pause
| Inst::TlsLoad { .. }
| Inst::TlsStore { .. }
| Inst::TlsLea { .. }
| Inst::AtomicCas { .. }
| Inst::LoadLinked { .. }
| Inst::StoreCond { .. }
| Inst::PairLoad { .. }
| Inst::PairStore { .. }
| Inst::Ret
| Inst::Break
| Inst::Nop => {}
}
}
}