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            true,
76        )
77    }
78
79    fn bind_label(&mut self, label: Label) -> Result<(), crate::AsmError> {
80        self.try_bind_label(label)
81    }
82}
83
84impl<'a> Assembler<'a> {
85    /// Collects the pending prefix flags set by the prefix setters (`rep`, `lock`,
86    /// `seg`, `k`, sae/rounding, `long`) and resets them, mapping them onto the
87    /// asmjit-style [`InstOptions`]/extra-reg model consumed by [`Assembler::emit_n`].
88    fn take_pending_prefixes(&mut self) -> PendingPrefixes {
89        let flags = self.flags;
90        self.flags = 0;
91
92        let mut prefixes = PendingPrefixes::default();
93        if flags & OPC_LOCK != 0 {
94            prefixes.options |= InstOptions::X86_LOCK;
95        }
96        if flags & 0x200000 != 0 {
97            prefixes.options |= InstOptions::X86_REP;
98        }
99        if flags & 0x100000 != 0 {
100            prefixes.options |= InstOptions::X86_REPNE;
101        }
102        if flags & LONG != 0 {
103            prefixes.options |= InstOptions::LONG_FORM;
104        }
105        if flags & SHORT != 0 {
106            prefixes.options |= InstOptions::SHORT_FORM;
107        }
108        if flags & OPC_Z != 0 {
109            prefixes.options |= InstOptions::X86_ZMASK;
110        }
111        if flags & RC_ENABLED != 0 {
112            let rc = flags & (RC_RD | RC_RU);
113            prefixes.options |= if rc == RC_RD {
114                InstOptions::X86_ER | InstOptions::X86_RD_SAE
115            } else if rc == RC_RU {
116                InstOptions::X86_ER | InstOptions::X86_RU_SAE
117            } else if rc == RC_RZ {
118                InstOptions::X86_ER | InstOptions::X86_RZ_SAE
119            } else {
120                // `sae()` and `rn_sae()` share the same flag bits (RC_RN is zero);
121                // both encode identically (EVEX.b with LL=00).
122                InstOptions::X86_SAE
123            };
124        }
125        prefixes.segment_id = ((flags & SEG_MASK) >> 29) as u32;
126        prefixes.mask_id = ((flags >> 33) & 0x7) as u32;
127        prefixes
128    }
129
130    /// Emits one instruction by id with explicit operands, using the asmjit-style
131    /// InstInfo → signature match → emit-handler pipeline (the new primary emit
132    /// path). Pending prefixes set by the prefix setters apply.
133    pub fn emit_n(&mut self, id: impl Into<u32>, ops: &[&Operand]) {
134        if let Err(error) = self.try_emit_n(id, ops) {
135            self.buffer.record_error(error);
136        }
137    }
138
139    pub fn try_emit_n(
140        &mut self,
141        id: impl Into<u32>,
142        ops: &[&Operand],
143    ) -> Result<(), crate::AsmError> {
144        if let Some(error) = self.buffer.error().cloned() {
145            return Err(error);
146        }
147        let prefixes = self.take_pending_prefixes();
148        self.try_emit_n_with_prefixes(id.into(), ops, prefixes, true)
149    }
150
151    /// Emits one instruction without validating the raw operands first.
152    ///
153    /// Use this only from a frontend that has already checked the operand
154    /// shapes. This also skips the rollback checkpoint: if an operand turns
155    /// out to be invalid, the error is recorded on the buffer (so `finish`
156    /// still fails) but partial bytes are not removed.
157    pub fn emit_n_unchecked(&mut self, id: impl Into<u32>, ops: &[&Operand]) {
158        if self.buffer.error().is_some() {
159            return;
160        }
161        let prefixes = self.take_pending_prefixes();
162        if let Err(error) = emit::emit_n(
163            self.buffer,
164            id.into(),
165            ops,
166            prefixes,
167            self.is_32bit(),
168            false,
169        ) {
170            self.buffer.record_error(error);
171        }
172    }
173
174    pub fn try_emit_n_unchecked(
175        &mut self,
176        id: impl Into<u32>,
177        ops: &[&Operand],
178    ) -> Result<(), crate::AsmError> {
179        if let Some(error) = self.buffer.error().cloned() {
180            return Err(error);
181        }
182        let prefixes = self.take_pending_prefixes();
183        self.try_emit_n_with_prefixes(id.into(), ops, prefixes, false)
184    }
185
186    fn try_emit_n_with_prefixes(
187        &mut self,
188        id: u32,
189        ops: &[&Operand],
190        prefixes: PendingPrefixes,
191        validate: bool,
192    ) -> Result<(), crate::AsmError> {
193        if let Some(error) = self.buffer.error().cloned() {
194            return Err(error);
195        }
196        let checkpoint = self.buffer.checkpoint();
197        if let Err(error) =
198            emit::emit_n(self.buffer, id, ops, prefixes, self.is_32bit(), validate)
199        {
200            self.buffer.rollback(checkpoint);
201            return Err(error);
202        }
203        Ok(())
204    }
205
206    pub fn new(buf: &'a mut CodeBuffer) -> Self {
207        if !matches!(buf.env().arch(), Arch::X86 | Arch::X64) {
208            return Self::poisoned(buf, crate::AsmError::InvalidArch);
209        }
210        Self::unchecked(buf)
211    }
212
213    pub fn try_new(buf: &'a mut CodeBuffer) -> Result<Self, crate::AsmError> {
214        if !matches!(buf.env().arch(), Arch::X86 | Arch::X64) {
215            return Err(crate::AsmError::InvalidArch);
216        }
217        Ok(Self::unchecked(buf))
218    }
219
220    fn unchecked(buf: &'a mut CodeBuffer) -> Self {
221        Self {
222            buffer: buf,
223            extra_reg: Reg::new(),
224            flags: 0,
225        }
226    }
227
228    fn poisoned(buf: &'a mut CodeBuffer, error: crate::AsmError) -> Self {
229        buf.record_error(error);
230        Self {
231            buffer: buf,
232            extra_reg: Reg::new(),
233            flags: 0,
234        }
235    }
236
237    /// Returns the environment (arch/mode) this assembler targets.
238    pub fn environment(&self) -> &Environment {
239        self.buffer.env()
240    }
241
242    /// Tests whether the assembler targets 32-bit X86 mode.
243    pub fn is_32bit(&self) -> bool {
244        self.buffer.env().is_32bit()
245    }
246
247    /// Tests whether the assembler targets 64-bit X64 mode.
248    pub fn is_64bit(&self) -> bool {
249        self.buffer.env().is_64bit()
250    }
251
252    pub fn sae(&mut self) -> &mut Self {
253        self.set_rounding(RC_RN)
254    }
255
256    pub fn rn_sae(&mut self) -> &mut Self {
257        self.set_rounding(RC_RN)
258    }
259
260    pub fn rd_sae(&mut self) -> &mut Self {
261        self.set_rounding(RC_RD)
262    }
263    pub fn ru_sae(&mut self) -> &mut Self {
264        self.set_rounding(RC_RU)
265    }
266
267    pub fn rz_sae(&mut self) -> &mut Self {
268        self.set_rounding(RC_RZ)
269    }
270
271    fn set_rounding(&mut self, rounding: u64) -> &mut Self {
272        let mask = RC_ENABLED | RC_MASK;
273        let pending = self.flags & mask;
274        let requested = RC_ENABLED | rounding;
275        if pending != 0 && pending != requested {
276            self.buffer
277                .record_error(crate::AsmError::X86(X86Error::InvalidRoundingControl {
278                    rc: requested,
279                    reason: "conflicting pending rounding modes",
280                }));
281            return self;
282        }
283        self.flags = (self.flags & !mask) | requested;
284        self
285    }
286
287    pub fn seg(&mut self, sreg: SReg) -> &mut Self {
288        let segment_id = sreg.id();
289        if !(SReg::ES..=SReg::GS).contains(&segment_id) {
290            self.buffer
291                .record_error(crate::AsmError::X86(X86Error::InvalidPrefix {
292                    prefix: segment_id as u64,
293                    reason: "invalid segment override",
294                }));
295            return self;
296        }
297        let pending = (self.flags & SEG_MASK) >> 29;
298        if pending != 0 && pending != segment_id as u64 {
299            self.buffer
300                .record_error(crate::AsmError::X86(X86Error::InvalidPrefix {
301                    prefix: segment_id as u64,
302                    reason: "conflicting pending segment overrides",
303                }));
304            return self;
305        }
306        self.flags = (self.flags & !SEG_MASK) | (segment_id as u64) << 29;
307        self
308    }
309
310    pub fn fs(&mut self) -> &mut Self {
311        self.seg(FS)
312    }
313
314    pub fn gs(&mut self) -> &mut Self {
315        self.seg(GS)
316    }
317
318    pub fn k(&mut self, k: KReg) -> &mut Self {
319        let mask_id = k.id();
320        if !(1..=7).contains(&mask_id) {
321            self.buffer
322                .record_error(crate::AsmError::X86(X86Error::InvalidMasking {
323                    mask_reg: mask_id,
324                    reason: "mask register must be k1..k7",
325                }));
326            return self;
327        }
328        let pending = (self.flags >> 33) & 0x7;
329        if pending != 0 && pending != mask_id as u64 {
330            self.buffer
331                .record_error(crate::AsmError::X86(X86Error::InvalidMasking {
332                    mask_reg: mask_id,
333                    reason: "conflicting pending mask registers",
334                }));
335            return self;
336        }
337        self.flags = (self.flags & !(0x7 << 33)) | (mask_id as u64) << 33;
338
339        self
340    }
341
342    /// AVX-512 zeroing mask `{z}` for the next instruction (requires `k()`).
343    pub fn z(&mut self) -> &mut Self {
344        self.flags |= OPC_Z;
345        self
346    }
347
348    pub fn rep(&mut self) -> &mut Self {
349        self.flags |= 0x200000;
350        self
351    }
352
353    pub fn repnz(&mut self) -> &mut Self {
354        self.flags |= 0x100000;
355        self
356    }
357
358    pub fn repz(&mut self) -> &mut Self {
359        self.rep()
360    }
361
362    pub fn lock(&mut self) -> &mut Self {
363        self.flags |= OPC_LOCK;
364        self
365    }
366
367    pub fn long(&mut self) -> &mut Self {
368        self.flags |= LONG;
369        self
370    }
371
372    pub fn short(&mut self) -> &mut Self {
373        self.flags |= SHORT;
374        self
375    }
376
377    pub fn get_label(&mut self) -> Label {
378        self.buffer.get_label()
379    }
380
381    pub fn bind_label(&mut self, label: Label) {
382        if let Err(error) = self.try_bind_label(label) {
383            self.buffer.record_error(error);
384        }
385    }
386
387    pub fn try_bind_label(&mut self, label: Label) -> Result<(), crate::AsmError> {
388        self.buffer.try_bind_label(label)
389    }
390
391    pub fn add_constant(&mut self, c: impl Into<ConstantData>) -> Label {
392        let c = self.buffer.add_constant(c);
393        self.buffer.get_label_for_constant(c)
394    }
395
396    pub fn label_offset(&self, label: Label) -> CodeOffset {
397        self.buffer.label_offset(label)
398    }
399
400    pub fn data(&self) -> &[u8] {
401        self.buffer.data()
402    }
403
404    pub fn error(&self) -> Option<&crate::AsmError> {
405        self.buffer.error()
406    }
407
408    /// Reserve a nop-filled region for later rewriting.
409    pub fn reserve_patch_region(
410        &mut self,
411        size: CodeOffset,
412        align: CodeOffset,
413    ) -> Result<PatchableRegion, crate::AsmError> {
414        self.buffer.reserve_patch_region(size, align)
415    }
416
417    /// Emits a rel32 jump through `emit`, returning its displacement as a
418    /// patchable jump.
419    fn patchable_rel32(&mut self, disp_ofs: u32, emit: impl FnOnce(&mut Self)) -> PatchableJump {
420        let previous_error = self.buffer.error().cloned();
421
422        self.buffer.align_to(4);
423        for _ in 0..(4 - disp_ofs) % 4 {
424            self.buffer.put1(0x90);
425        }
426        let start = self.buffer.cur_offset();
427        self.long();
428        emit(self);
429        let end = self.buffer.cur_offset();
430        if self.buffer.error().cloned() != previous_error || end < start + 5 {
431            return PatchableJump::invalid(LabelUse::X86JmpRel32);
432        }
433        debug_assert_eq!((end - 4) % 4, 0, "displacement must be 4-aligned");
434        // The displacement ends the instruction.
435        PatchableJump::new(end - 4, LabelUse::X86JmpRel32)
436    }
437
438    /// `jmp` to `label` with a rel32 displacement that can be retargeted.
439    pub fn patchable_jmp(&mut self, label: Label) -> PatchableJump {
440        self.patchable_rel32(1, |asm| asm.jmp(label))
441    }
442
443    /// `call` to `label` with a rel32 displacement that can be retargeted.
444    pub fn patchable_call(&mut self, label: Label) -> PatchableJump {
445        self.patchable_rel32(1, |asm| asm.call(label))
446    }
447
448    /// Conditional jump to `label` with a rel32 displacement that can be
449    /// retargeted.
450    pub fn patchable_jcc(&mut self, cc: CondCode, label: Label) -> PatchableJump {
451        const JCC: [InstId; 16] = [
452            InstId::Jo,
453            InstId::Jno,
454            InstId::Jb,
455            InstId::Jnb,
456            InstId::Jz,
457            InstId::Jnz,
458            InstId::Jbe,
459            InstId::Jnbe,
460            InstId::Js,
461            InstId::Jns,
462            InstId::Jp,
463            InstId::Jnp,
464            InstId::Jl,
465            InstId::Jnl,
466            InstId::Jle,
467            InstId::Jnle,
468        ];
469        self.patchable_rel32(2, |asm| {
470            asm.emit_n(JCC[cc.code() as usize] as u32, &[label.as_operand()])
471        })
472    }
473
474    /// `mov` of an immediate into a `Gp32`/`Gp64` register, with the
475    /// immediate at a fixed size (4 or 8 bytes) so it can be rewritten.
476    pub fn patchable_mov<A, B>(&mut self, dst: A, src: B) -> DataLabel
477    where
478        A: OperandCast + Copy,
479        B: OperandCast + Copy,
480        Self: MovEmitter<A, B>,
481    {
482        let dst_op = *dst.as_operand();
483        let src_op = *src.as_operand();
484        let size = if dst_op.is_reg_type_of(RegType::Gp64) {
485            8
486        } else if dst_op.is_reg_type_of(RegType::Gp32) {
487            4
488        } else {
489            self.buffer
490                .record_error(crate::AsmError::X86(X86Error::InvalidOperand {
491                    operand_index: 0,
492                    reason: "patchable_mov requires a Gp32 or Gp64 destination",
493                }));
494            return DataLabel::invalid(8, DataEncoding::Raw);
495        };
496
497        if !src_op.is_imm() {
498            self.buffer
499                .record_error(crate::AsmError::X86(X86Error::InvalidOperand {
500                    operand_index: 1,
501                    reason: "patchable_mov requires an immediate source",
502                }));
503            return DataLabel::invalid(size, DataEncoding::Raw);
504        }
505
506        let offset = self.buffer.cur_offset();
507        let previous_error = self.buffer.error().cloned();
508        self.long();
509        MovEmitter::mov(self, dst, src);
510        if self.buffer.error().cloned() != previous_error
511            || self.buffer.cur_offset() < offset + CodeOffset::from(size)
512        {
513            return DataLabel::invalid(size, DataEncoding::Raw);
514        }
515
516        // Long mov-imm ends with the `size`-byte immediate.
517        let offset = self.buffer.cur_offset() - CodeOffset::from(size);
518        DataLabel::new(offset, size, DataEncoding::Raw)
519    }
520
521    fn emit_string_movs(&mut self, size: u32) {
522        let (dst, src) = if self.is_32bit() {
523            (ptr(EDI, 0, size), ptr(ESI, 0, size))
524        } else {
525            (ptr(RDI, 0, size), ptr(RSI, 0, size))
526        };
527        self.emit_n(InstId::Movs as u32, &[dst.as_operand(), src.as_operand()]);
528    }
529}
530
531pub trait StringMovsEmitter {
532    /// Move byte string.
533    fn movsb(&mut self);
534    /// Move word string.
535    fn movsw(&mut self);
536    /// Move dword string.
537    fn movsd_str(&mut self);
538    /// Move qword string.
539    fn movsq(&mut self);
540}
541
542impl StringMovsEmitter for Assembler<'_> {
543    fn movsb(&mut self) {
544        self.emit_string_movs(1);
545    }
546
547    fn movsw(&mut self) {
548        self.emit_string_movs(2);
549    }
550
551    fn movsd_str(&mut self) {
552        self.emit_string_movs(4);
553    }
554
555    fn movsq(&mut self) {
556        self.emit_string_movs(8);
557    }
558}
559
560#[derive(Debug, Clone, Copy, PartialEq, Eq)]
561pub enum CondCode {
562    O = 0x0,
563    NO = 0x1,
564    C = 0x2,
565    NC = 0x3,
566    Z = 0x4,
567    NZ = 0x5,
568    BE = 0x6,
569    A = 0x7,
570    S = 0x8,
571    NS = 0x9,
572    P = 0xa,
573
574    NP = 0xb,
575    L = 0xc,
576    GE = 0xd,
577    LE = 0xe,
578    G = 0xf,
579}
580
581impl CondCode {
582    pub const B: Self = Self::C;
583    pub const NAE: Self = Self::C;
584    pub const AE: Self = Self::NC;
585    pub const NB: Self = Self::NC;
586    pub const E: Self = Self::Z;
587    pub const NE: Self = Self::NZ;
588    pub const NA: Self = Self::BE;
589    pub const NBE: Self = Self::A;
590    pub const PO: Self = Self::NP;
591    pub const NGE: Self = Self::L;
592    pub const NL: Self = Self::GE;
593    pub const NG: Self = Self::LE;
594    pub const NLE: Self = Self::G;
595    pub const PE: Self = Self::P;
596
597    pub const fn code(self) -> u8 {
598        self as u8
599    }
600
601    pub fn invert(self) -> Self {
602        match self {
603            Self::O => Self::NO,
604            Self::NO => Self::O,
605            Self::C => Self::NC,
606            Self::NC => Self::C,
607            Self::Z => Self::NZ,
608            Self::NZ => Self::Z,
609            Self::BE => Self::A,
610            Self::A => Self::BE,
611            Self::S => Self::NS,
612            Self::NS => Self::S,
613            Self::P => Self::NP,
614            Self::NP => Self::P,
615            Self::L => Self::GE,
616            Self::GE => Self::L,
617            Self::LE => Self::G,
618            Self::G => Self::LE,
619        }
620    }
621}
622
623#[cfg(test)]
624mod tests {
625    use super::*;
626    use crate::core::{arch_traits::Arch, buffer::CodeBuffer, target::Environment};
627
628    fn encode_mov_rax(value: impl Into<Imm>) -> alloc::vec::Vec<u8> {
629        let mut buf = CodeBuffer::new(Environment::new(Arch::X64));
630        let mut asm = Assembler::new(&mut buf);
631        asm.mov(RAX, value);
632        buf.finish().unwrap().data().to_vec()
633    }
634
635    #[test]
636    fn unsigned_immediates_encode_like_their_signed_bits() {
637        assert_eq!(encode_mov_rax(u64::MAX), encode_mov_rax(-1_i64));
638        assert_eq!(encode_mov_rax(usize::MAX), encode_mov_rax(-1_i64));
639        assert_eq!(
640            encode_mov_rax(0x1234_5678_9ABC_DEF0_u64),
641            encode_mov_rax(0x1234_5678_9ABC_DEF0_i64)
642        );
643    }
644}