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