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, disp_ofs: u32, emit: impl FnOnce(&mut Self)) -> PatchableJump {
381        let previous_error = self.buffer.error().cloned();
382
383        self.buffer.align_to(4);
384        for _ in 0..(4 - disp_ofs) % 4 {
385            self.buffer.put1(0x90);
386        }
387        let start = self.buffer.cur_offset();
388        self.long();
389        emit(self);
390        let end = self.buffer.cur_offset();
391        if self.buffer.error().cloned() != previous_error || end < start + 5 {
392            return PatchableJump::invalid(LabelUse::X86JmpRel32);
393        }
394        debug_assert_eq!((end - 4) % 4, 0, "displacement must be 4-aligned");
395        // The displacement ends the instruction.
396        PatchableJump::new(end - 4, LabelUse::X86JmpRel32)
397    }
398
399    /// `jmp` to `label` with a rel32 displacement that can be retargeted.
400    pub fn patchable_jmp(&mut self, label: Label) -> PatchableJump {
401        self.patchable_rel32(1, |asm| asm.jmp(label))
402    }
403
404    /// `call` to `label` with a rel32 displacement that can be retargeted.
405    pub fn patchable_call(&mut self, label: Label) -> PatchableJump {
406        self.patchable_rel32(1, |asm| asm.call(label))
407    }
408
409    /// Conditional jump to `label` with a rel32 displacement that can be
410    /// retargeted.
411    pub fn patchable_jcc(&mut self, cc: CondCode, label: Label) -> PatchableJump {
412        const JCC: [InstId; 16] = [
413            InstId::Jo,
414            InstId::Jno,
415            InstId::Jb,
416            InstId::Jnb,
417            InstId::Jz,
418            InstId::Jnz,
419            InstId::Jbe,
420            InstId::Jnbe,
421            InstId::Js,
422            InstId::Jns,
423            InstId::Jp,
424            InstId::Jnp,
425            InstId::Jl,
426            InstId::Jnl,
427            InstId::Jle,
428            InstId::Jnle,
429        ];
430        self.patchable_rel32(2, |asm| {
431            asm.emit_n(JCC[cc.code() as usize] as u32, &[label.as_operand()])
432        })
433    }
434
435    /// `mov` of an immediate into a `Gp32`/`Gp64` register, with the
436    /// immediate at a fixed size (4 or 8 bytes) so it can be rewritten.
437    pub fn patchable_mov<A, B>(&mut self, dst: A, src: B) -> DataLabel
438    where
439        A: OperandCast + Copy,
440        B: OperandCast + Copy,
441        Self: MovEmitter<A, B>,
442    {
443        let dst_op = *dst.as_operand();
444        let src_op = *src.as_operand();
445        let size = if dst_op.is_reg_type_of(RegType::Gp64) {
446            8
447        } else if dst_op.is_reg_type_of(RegType::Gp32) {
448            4
449        } else {
450            self.buffer
451                .record_error(crate::AsmError::X86(X86Error::InvalidOperand {
452                    operand_index: 0,
453                    reason: "patchable_mov requires a Gp32 or Gp64 destination",
454                }));
455            return DataLabel::invalid(8, DataEncoding::Raw);
456        };
457
458        if !src_op.is_imm() {
459            self.buffer
460                .record_error(crate::AsmError::X86(X86Error::InvalidOperand {
461                    operand_index: 1,
462                    reason: "patchable_mov requires an immediate source",
463                }));
464            return DataLabel::invalid(size, DataEncoding::Raw);
465        }
466
467        let offset = self.buffer.cur_offset();
468        let previous_error = self.buffer.error().cloned();
469        self.long();
470        MovEmitter::mov(self, dst, src);
471        if self.buffer.error().cloned() != previous_error
472            || self.buffer.cur_offset() < offset + CodeOffset::from(size)
473        {
474            return DataLabel::invalid(size, DataEncoding::Raw);
475        }
476
477        // Long mov-imm ends with the `size`-byte immediate.
478        let offset = self.buffer.cur_offset() - CodeOffset::from(size);
479        DataLabel::new(offset, size, DataEncoding::Raw)
480    }
481
482    fn emit_string_movs(&mut self, size: u32) {
483        let (dst, src) = if self.is_32bit() {
484            (ptr(EDI, 0, size), ptr(ESI, 0, size))
485        } else {
486            (ptr(RDI, 0, size), ptr(RSI, 0, size))
487        };
488        self.emit_n(InstId::Movs as u32, &[dst.as_operand(), src.as_operand()]);
489    }
490}
491
492pub trait StringMovsEmitter {
493    /// Move byte string.
494    fn movsb(&mut self);
495    /// Move word string.
496    fn movsw(&mut self);
497    /// Move dword string.
498    fn movsd_str(&mut self);
499    /// Move qword string.
500    fn movsq(&mut self);
501}
502
503impl StringMovsEmitter for Assembler<'_> {
504    fn movsb(&mut self) {
505        self.emit_string_movs(1);
506    }
507
508    fn movsw(&mut self) {
509        self.emit_string_movs(2);
510    }
511
512    fn movsd_str(&mut self) {
513        self.emit_string_movs(4);
514    }
515
516    fn movsq(&mut self) {
517        self.emit_string_movs(8);
518    }
519}
520
521#[derive(Debug, Clone, Copy, PartialEq, Eq)]
522pub enum CondCode {
523    O = 0x0,
524    NO = 0x1,
525    C = 0x2,
526    NC = 0x3,
527    Z = 0x4,
528    NZ = 0x5,
529    BE = 0x6,
530    A = 0x7,
531    S = 0x8,
532    NS = 0x9,
533    P = 0xa,
534
535    NP = 0xb,
536    L = 0xc,
537    GE = 0xd,
538    LE = 0xe,
539    G = 0xf,
540}
541
542impl CondCode {
543    pub const B: Self = Self::C;
544    pub const NAE: Self = Self::C;
545    pub const AE: Self = Self::NC;
546    pub const NB: Self = Self::NC;
547    pub const E: Self = Self::Z;
548    pub const NE: Self = Self::NZ;
549    pub const NA: Self = Self::BE;
550    pub const NBE: Self = Self::A;
551    pub const PO: Self = Self::NP;
552    pub const NGE: Self = Self::L;
553    pub const NL: Self = Self::GE;
554    pub const NG: Self = Self::LE;
555    pub const NLE: Self = Self::G;
556    pub const PE: Self = Self::P;
557
558    pub const fn code(self) -> u8 {
559        self as u8
560    }
561
562    pub fn invert(self) -> Self {
563        match self {
564            Self::O => Self::NO,
565            Self::NO => Self::O,
566            Self::C => Self::NC,
567            Self::NC => Self::C,
568            Self::Z => Self::NZ,
569            Self::NZ => Self::Z,
570            Self::BE => Self::A,
571            Self::A => Self::BE,
572            Self::S => Self::NS,
573            Self::NS => Self::S,
574            Self::P => Self::NP,
575            Self::NP => Self::P,
576            Self::L => Self::GE,
577            Self::GE => Self::L,
578            Self::LE => Self::G,
579            Self::G => Self::LE,
580        }
581    }
582}