Skip to main content

asmkit/x86/
assembler.rs

1#![allow(dead_code)]
2use super::emit::{self, PendingPrefixes};
3use super::emitter::{CallEmitter, JmpEmitter, MovEmitter};
4use super::instdb::InstId;
5use super::operands::*;
6use crate::{
7    X86Error,
8    core::{
9        arch_traits::Arch,
10        buffer::{CodeBuffer, CodeOffset, ConstantData, LabelUse},
11        globals::InstOptions,
12        operand::*,
13        patch::{DataEncoding, DataLabel, PatchableJump, PatchableRegion},
14        target::Environment,
15    },
16};
17
18/// X86/X64 Assembler implementation.
19pub struct Assembler<'a> {
20    pub(crate) buffer: &'a mut CodeBuffer,
21    flags: u64,
22    extra_reg: Reg,
23}
24
25const RC_RN: u64 = 0x0000000;
26const RC_RD: u64 = 0x0800000;
27const RC_RU: u64 = 0x1000000;
28const RC_RZ: u64 = 0x1800000;
29const RC_MASK: u64 = RC_RD | RC_RU;
30const RC_ENABLED: u64 = 0x4000000;
31const SEG_MASK: u64 = 0xe0000000;
32const LONG: u64 = 0x100000000;
33
34/// Bit 37: LOCK prefix (bits 23/24 are the rounding-mode RC bits, bits 20/21
35/// REP/REPNE, bit 26 SAE/ER enable, bits 29..=31 segment, bit 32 long-form,
36/// bits 33..=35 mask id, bit 36 zeroing mask, bit 38 short-form).
37const OPC_LOCK: u64 = 0x2000000000;
38/// Bit 38: short (rel8) branch form, also for labels not yet bound.
39const SHORT: u64 = 0x4000000000;
40/// Bit 36: AVX-512 zeroing mask `{z}` (bits 33..=35 carry the mask register id).
41const OPC_Z: u64 = 0x1000000000;
42
43impl crate::core::builder::InstSink for Assembler<'_> {
44    fn arch(&self) -> Arch {
45        self.environment().arch()
46    }
47
48    fn emit_inst(&mut self, inst: &crate::core::inst::Inst) -> Result<(), crate::AsmError> {
49        let ops = inst.operands();
50        let mut refs: smallvec::SmallVec<[&Operand; 6]> = smallvec::SmallVec::new();
51        refs.extend(ops.iter());
52
53        let extra_reg = inst.extra_reg();
54        let mask_id = match extra_reg.signature.try_op_type() {
55            Some(OperandType::None) if extra_reg.signature.bits() == 0 => 0,
56            Some(OperandType::Reg) if extra_reg.signature.try_reg_type() == Some(RegType::Mask) => {
57                extra_reg.id()
58            }
59            _ => {
60                return Err(X86Error::InvalidMasking {
61                    mask_reg: extra_reg.id(),
62                    reason: "x86 extra register must be a mask register",
63                }
64                .into());
65            }
66        };
67        self.try_emit_n_with_prefixes(
68            inst.id(),
69            &refs,
70            PendingPrefixes {
71                options: inst.options(),
72                segment_id: 0,
73                mask_id,
74            },
75        )
76    }
77
78    fn bind_label(&mut self, label: Label) -> Result<(), crate::AsmError> {
79        self.try_bind_label(label)
80    }
81}
82
83impl<'a> Assembler<'a> {
84    /// Collects the pending prefix flags set by the prefix setters (`rep`, `lock`,
85    /// `seg`, `k`, sae/rounding, `long`) and resets them, mapping them onto the
86    /// asmjit-style [`InstOptions`]/extra-reg model consumed by [`Assembler::emit_n`].
87    fn take_pending_prefixes(&mut self) -> PendingPrefixes {
88        let flags = self.flags;
89        self.flags = 0;
90
91        let mut prefixes = PendingPrefixes::default();
92        if flags & OPC_LOCK != 0 {
93            prefixes.options |= InstOptions::X86_LOCK;
94        }
95        if flags & 0x200000 != 0 {
96            prefixes.options |= InstOptions::X86_REP;
97        }
98        if flags & 0x100000 != 0 {
99            prefixes.options |= InstOptions::X86_REPNE;
100        }
101        if flags & LONG != 0 {
102            prefixes.options |= InstOptions::LONG_FORM;
103        }
104        if flags & SHORT != 0 {
105            prefixes.options |= InstOptions::SHORT_FORM;
106        }
107        if flags & OPC_Z != 0 {
108            prefixes.options |= InstOptions::X86_ZMASK;
109        }
110        if flags & RC_ENABLED != 0 {
111            let rc = flags & (RC_RD | RC_RU);
112            prefixes.options |= if rc == RC_RD {
113                InstOptions::X86_ER | InstOptions::X86_RD_SAE
114            } else if rc == RC_RU {
115                InstOptions::X86_ER | InstOptions::X86_RU_SAE
116            } else if rc == RC_RZ {
117                InstOptions::X86_ER | InstOptions::X86_RZ_SAE
118            } else {
119                // `sae()` and `rn_sae()` share the same flag bits (RC_RN is zero);
120                // both encode identically (EVEX.b with LL=00).
121                InstOptions::X86_SAE
122            };
123        }
124        prefixes.segment_id = ((flags & SEG_MASK) >> 29) as u32;
125        prefixes.mask_id = ((flags >> 33) & 0x7) as u32;
126        prefixes
127    }
128
129    /// Emits one instruction by id with explicit operands, using the asmjit-style
130    /// InstInfo → signature match → emit-handler pipeline (the new primary emit
131    /// path). Pending prefixes set by the prefix setters apply.
132    pub fn emit_n(&mut self, id: impl Into<u32>, ops: &[&Operand]) {
133        if let Err(error) = self.try_emit_n(id, ops) {
134            self.buffer.record_error(error);
135        }
136    }
137
138    pub fn try_emit_n(
139        &mut self,
140        id: impl Into<u32>,
141        ops: &[&Operand],
142    ) -> Result<(), crate::AsmError> {
143        if let Some(error) = self.buffer.error().cloned() {
144            return Err(error);
145        }
146        let prefixes = self.take_pending_prefixes();
147        self.try_emit_n_with_prefixes(id, ops, prefixes)
148    }
149
150    fn try_emit_n_with_prefixes(
151        &mut self,
152        id: impl Into<u32>,
153        ops: &[&Operand],
154        prefixes: PendingPrefixes,
155    ) -> Result<(), crate::AsmError> {
156        if let Some(error) = self.buffer.error().cloned() {
157            return Err(error);
158        }
159        let checkpoint = self.buffer.checkpoint();
160        if let Err(error) = emit::emit_n(self.buffer, id.into(), ops, prefixes, self.is_32bit()) {
161            self.buffer.rollback(checkpoint);
162            return Err(error);
163        }
164        Ok(())
165    }
166
167    pub fn new(buf: &'a mut CodeBuffer) -> Self {
168        if !matches!(buf.env().arch(), Arch::X86 | Arch::X64) {
169            return Self::poisoned(buf, crate::AsmError::InvalidArch);
170        }
171        Self::unchecked(buf)
172    }
173
174    pub fn try_new(buf: &'a mut CodeBuffer) -> Result<Self, crate::AsmError> {
175        if !matches!(buf.env().arch(), Arch::X86 | Arch::X64) {
176            return Err(crate::AsmError::InvalidArch);
177        }
178        Ok(Self::unchecked(buf))
179    }
180
181    fn unchecked(buf: &'a mut CodeBuffer) -> Self {
182        Self {
183            buffer: buf,
184            extra_reg: Reg::new(),
185            flags: 0,
186        }
187    }
188
189    fn poisoned(buf: &'a mut CodeBuffer, error: crate::AsmError) -> Self {
190        buf.record_error(error);
191        Self {
192            buffer: buf,
193            extra_reg: Reg::new(),
194            flags: 0,
195        }
196    }
197
198    /// Returns the environment (arch/mode) this assembler targets.
199    pub fn environment(&self) -> &Environment {
200        self.buffer.env()
201    }
202
203    /// Tests whether the assembler targets 32-bit X86 mode.
204    pub fn is_32bit(&self) -> bool {
205        self.buffer.env().is_32bit()
206    }
207
208    /// Tests whether the assembler targets 64-bit X64 mode.
209    pub fn is_64bit(&self) -> bool {
210        self.buffer.env().is_64bit()
211    }
212
213    pub fn sae(&mut self) -> &mut Self {
214        self.set_rounding(RC_RN)
215    }
216
217    pub fn rn_sae(&mut self) -> &mut Self {
218        self.set_rounding(RC_RN)
219    }
220
221    pub fn rd_sae(&mut self) -> &mut Self {
222        self.set_rounding(RC_RD)
223    }
224    pub fn ru_sae(&mut self) -> &mut Self {
225        self.set_rounding(RC_RU)
226    }
227
228    pub fn rz_sae(&mut self) -> &mut Self {
229        self.set_rounding(RC_RZ)
230    }
231
232    fn set_rounding(&mut self, rounding: u64) -> &mut Self {
233        let mask = RC_ENABLED | RC_MASK;
234        let pending = self.flags & mask;
235        let requested = RC_ENABLED | rounding;
236        if pending != 0 && pending != requested {
237            self.buffer
238                .record_error(crate::AsmError::X86(X86Error::InvalidRoundingControl {
239                    rc: requested,
240                    reason: "conflicting pending rounding modes",
241                }));
242            return self;
243        }
244        self.flags = (self.flags & !mask) | requested;
245        self
246    }
247
248    pub fn seg(&mut self, sreg: SReg) -> &mut Self {
249        let segment_id = sreg.id();
250        if !(SReg::ES..=SReg::GS).contains(&segment_id) {
251            self.buffer
252                .record_error(crate::AsmError::X86(X86Error::InvalidPrefix {
253                    prefix: segment_id as u64,
254                    reason: "invalid segment override",
255                }));
256            return self;
257        }
258        let pending = (self.flags & SEG_MASK) >> 29;
259        if pending != 0 && pending != segment_id as u64 {
260            self.buffer
261                .record_error(crate::AsmError::X86(X86Error::InvalidPrefix {
262                    prefix: segment_id as u64,
263                    reason: "conflicting pending segment overrides",
264                }));
265            return self;
266        }
267        self.flags = (self.flags & !SEG_MASK) | (segment_id as u64) << 29;
268        self
269    }
270
271    pub fn fs(&mut self) -> &mut Self {
272        self.seg(FS)
273    }
274
275    pub fn gs(&mut self) -> &mut Self {
276        self.seg(GS)
277    }
278
279    pub fn k(&mut self, k: KReg) -> &mut Self {
280        let mask_id = k.id();
281        if !(1..=7).contains(&mask_id) {
282            self.buffer
283                .record_error(crate::AsmError::X86(X86Error::InvalidMasking {
284                    mask_reg: mask_id,
285                    reason: "mask register must be k1..k7",
286                }));
287            return self;
288        }
289        let pending = (self.flags >> 33) & 0x7;
290        if pending != 0 && pending != mask_id as u64 {
291            self.buffer
292                .record_error(crate::AsmError::X86(X86Error::InvalidMasking {
293                    mask_reg: mask_id,
294                    reason: "conflicting pending mask registers",
295                }));
296            return self;
297        }
298        self.flags = (self.flags & !(0x7 << 33)) | (mask_id as u64) << 33;
299
300        self
301    }
302
303    /// AVX-512 zeroing mask `{z}` for the next instruction (requires `k()`).
304    pub fn z(&mut self) -> &mut Self {
305        self.flags |= OPC_Z;
306        self
307    }
308
309    pub fn rep(&mut self) -> &mut Self {
310        self.flags |= 0x200000;
311        self
312    }
313
314    pub fn repnz(&mut self) -> &mut Self {
315        self.flags |= 0x100000;
316        self
317    }
318
319    pub fn repz(&mut self) -> &mut Self {
320        self.rep()
321    }
322
323    pub fn lock(&mut self) -> &mut Self {
324        self.flags |= OPC_LOCK;
325        self
326    }
327
328    pub fn long(&mut self) -> &mut Self {
329        self.flags |= LONG;
330        self
331    }
332
333    pub fn short(&mut self) -> &mut Self {
334        self.flags |= SHORT;
335        self
336    }
337
338    pub fn get_label(&mut self) -> Label {
339        self.buffer.get_label()
340    }
341
342    pub fn bind_label(&mut self, label: Label) {
343        if let Err(error) = self.try_bind_label(label) {
344            self.buffer.record_error(error);
345        }
346    }
347
348    pub fn try_bind_label(&mut self, label: Label) -> Result<(), crate::AsmError> {
349        self.buffer.try_bind_label(label)
350    }
351
352    pub fn add_constant(&mut self, c: impl Into<ConstantData>) -> Label {
353        let c = self.buffer.add_constant(c);
354        self.buffer.get_label_for_constant(c)
355    }
356
357    pub fn label_offset(&self, label: Label) -> CodeOffset {
358        self.buffer.label_offset(label)
359    }
360
361    pub fn data(&self) -> &[u8] {
362        self.buffer.data()
363    }
364
365    pub fn error(&self) -> Option<&crate::AsmError> {
366        self.buffer.error()
367    }
368
369    /// Reserve a nop-filled region for later rewriting.
370    pub fn reserve_patch_region(
371        &mut self,
372        size: CodeOffset,
373        align: CodeOffset,
374    ) -> Result<PatchableRegion, crate::AsmError> {
375        self.buffer.reserve_patch_region(size, align)
376    }
377
378    /// Emits a rel32 jump through `emit`, returning its displacement as a
379    /// patchable jump.
380    fn patchable_rel32(&mut self, emit: impl FnOnce(&mut Self)) -> PatchableJump {
381        let start = self.buffer.cur_offset();
382        let previous_error = self.buffer.error().cloned();
383        self.long();
384        emit(self);
385        let end = self.buffer.cur_offset();
386        if self.buffer.error().cloned() != previous_error || end < start + 5 {
387            return PatchableJump::invalid(LabelUse::X86JmpRel32);
388        }
389        // The displacement ends the instruction.
390        PatchableJump::new(end - 4, LabelUse::X86JmpRel32)
391    }
392
393    /// `jmp` to `label` with a rel32 displacement that can be retargeted.
394    pub fn patchable_jmp(&mut self, label: Label) -> PatchableJump {
395        self.patchable_rel32(|asm| asm.jmp(label))
396    }
397
398    /// `call` to `label` with a rel32 displacement that can be retargeted.
399    pub fn patchable_call(&mut self, label: Label) -> PatchableJump {
400        self.patchable_rel32(|asm| asm.call(label))
401    }
402
403    /// Conditional jump to `label` with a rel32 displacement that can be
404    /// retargeted.
405    pub fn patchable_jcc(&mut self, cc: CondCode, label: Label) -> PatchableJump {
406        const JCC: [InstId; 16] = [
407            InstId::Jo,
408            InstId::Jno,
409            InstId::Jb,
410            InstId::Jnb,
411            InstId::Jz,
412            InstId::Jnz,
413            InstId::Jbe,
414            InstId::Jnbe,
415            InstId::Js,
416            InstId::Jns,
417            InstId::Jp,
418            InstId::Jnp,
419            InstId::Jl,
420            InstId::Jnl,
421            InstId::Jle,
422            InstId::Jnle,
423        ];
424        self.patchable_rel32(|asm| {
425            asm.emit_n(JCC[cc.code() as usize] as u32, &[label.as_operand()])
426        })
427    }
428
429    /// `mov` of an immediate into a `Gp32`/`Gp64` register, with the
430    /// immediate at a fixed size (4 or 8 bytes) so it can be rewritten.
431    pub fn patchable_mov<A, B>(&mut self, dst: A, src: B) -> DataLabel
432    where
433        A: OperandCast + Copy,
434        B: OperandCast + Copy,
435        Self: MovEmitter<A, B>,
436    {
437        let dst_op = *dst.as_operand();
438        let src_op = *src.as_operand();
439        let size = if dst_op.is_reg_type_of(RegType::Gp64) {
440            8
441        } else if dst_op.is_reg_type_of(RegType::Gp32) {
442            4
443        } else {
444            self.buffer
445                .record_error(crate::AsmError::X86(X86Error::InvalidOperand {
446                    operand_index: 0,
447                    reason: "patchable_mov requires a Gp32 or Gp64 destination",
448                }));
449            return DataLabel::invalid(8, DataEncoding::Raw);
450        };
451
452        if !src_op.is_imm() {
453            self.buffer
454                .record_error(crate::AsmError::X86(X86Error::InvalidOperand {
455                    operand_index: 1,
456                    reason: "patchable_mov requires an immediate source",
457                }));
458            return DataLabel::invalid(size, DataEncoding::Raw);
459        }
460
461        let offset = self.buffer.cur_offset();
462        let previous_error = self.buffer.error().cloned();
463        self.long();
464        MovEmitter::mov(self, dst, src);
465        if self.buffer.error().cloned() != previous_error
466            || self.buffer.cur_offset() < offset + CodeOffset::from(size)
467        {
468            return DataLabel::invalid(size, DataEncoding::Raw);
469        }
470
471        // Long mov-imm ends with the `size`-byte immediate.
472        let offset = self.buffer.cur_offset() - CodeOffset::from(size);
473        DataLabel::new(offset, size, DataEncoding::Raw)
474    }
475
476    fn emit_string_movs(&mut self, size: u32) {
477        let (dst, src) = if self.is_32bit() {
478            (ptr(EDI, 0, size), ptr(ESI, 0, size))
479        } else {
480            (ptr(RDI, 0, size), ptr(RSI, 0, size))
481        };
482        self.emit_n(InstId::Movs as u32, &[dst.as_operand(), src.as_operand()]);
483    }
484}
485
486pub trait StringMovsEmitter {
487    /// Move byte string.
488    fn movsb(&mut self);
489    /// Move word string.
490    fn movsw(&mut self);
491    /// Move dword string.
492    fn movsd_str(&mut self);
493    /// Move qword string.
494    fn movsq(&mut self);
495}
496
497impl StringMovsEmitter for Assembler<'_> {
498    fn movsb(&mut self) {
499        self.emit_string_movs(1);
500    }
501
502    fn movsw(&mut self) {
503        self.emit_string_movs(2);
504    }
505
506    fn movsd_str(&mut self) {
507        self.emit_string_movs(4);
508    }
509
510    fn movsq(&mut self) {
511        self.emit_string_movs(8);
512    }
513}
514
515#[derive(Debug, Clone, Copy, PartialEq, Eq)]
516pub enum CondCode {
517    O = 0x0,
518    NO = 0x1,
519    C = 0x2,
520    NC = 0x3,
521    Z = 0x4,
522    NZ = 0x5,
523    BE = 0x6,
524    A = 0x7,
525    S = 0x8,
526    NS = 0x9,
527    P = 0xa,
528
529    NP = 0xb,
530    L = 0xc,
531    GE = 0xd,
532    LE = 0xe,
533    G = 0xf,
534}
535
536impl CondCode {
537    pub const B: Self = Self::C;
538    pub const NAE: Self = Self::C;
539    pub const AE: Self = Self::NC;
540    pub const NB: Self = Self::NC;
541    pub const E: Self = Self::Z;
542    pub const NE: Self = Self::NZ;
543    pub const NA: Self = Self::BE;
544    pub const NBE: Self = Self::A;
545    pub const PO: Self = Self::NP;
546    pub const NGE: Self = Self::L;
547    pub const NL: Self = Self::GE;
548    pub const NG: Self = Self::LE;
549    pub const NLE: Self = Self::G;
550    pub const PE: Self = Self::P;
551
552    pub const fn code(self) -> u8 {
553        self as u8
554    }
555
556    pub fn invert(self) -> Self {
557        match self {
558            Self::O => Self::NO,
559            Self::NO => Self::O,
560            Self::C => Self::NC,
561            Self::NC => Self::C,
562            Self::Z => Self::NZ,
563            Self::NZ => Self::Z,
564            Self::BE => Self::A,
565            Self::A => Self::BE,
566            Self::S => Self::NS,
567            Self::NS => Self::S,
568            Self::P => Self::NP,
569            Self::NP => Self::P,
570            Self::L => Self::GE,
571            Self::GE => Self::L,
572            Self::LE => Self::G,
573            Self::G => Self::LE,
574        }
575    }
576}