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
18pub 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
34const OPC_LOCK: u64 = 0x2000000000;
38const SHORT: u64 = 0x4000000000;
40const 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 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 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 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 pub fn environment(&self) -> &Environment {
200 self.buffer.env()
201 }
202
203 pub fn is_32bit(&self) -> bool {
205 self.buffer.env().is_32bit()
206 }
207
208 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 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 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 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 PatchableJump::new(end - 4, LabelUse::X86JmpRel32)
391 }
392
393 pub fn patchable_jmp(&mut self, label: Label) -> PatchableJump {
395 self.patchable_rel32(|asm| asm.jmp(label))
396 }
397
398 pub fn patchable_call(&mut self, label: Label) -> PatchableJump {
400 self.patchable_rel32(|asm| asm.call(label))
401 }
402
403 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 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 let offset = self.buffer.cur_offset() - CodeOffset::from(size);
473 DataLabel::new(offset, size, DataEncoding::Raw)
474 }
475}
476
477#[derive(Debug, Clone, Copy, PartialEq, Eq)]
478pub enum CondCode {
479 O = 0x0,
480 NO = 0x1,
481 C = 0x2,
482 NC = 0x3,
483 Z = 0x4,
484 NZ = 0x5,
485 BE = 0x6,
486 A = 0x7,
487 S = 0x8,
488 NS = 0x9,
489 P = 0xa,
490
491 NP = 0xb,
492 L = 0xc,
493 GE = 0xd,
494 LE = 0xe,
495 G = 0xf,
496}
497
498impl CondCode {
499 pub const B: Self = Self::C;
500 pub const NAE: Self = Self::C;
501 pub const AE: Self = Self::NC;
502 pub const NB: Self = Self::NC;
503 pub const E: Self = Self::Z;
504 pub const NE: Self = Self::NZ;
505 pub const NA: Self = Self::BE;
506 pub const NBE: Self = Self::A;
507 pub const PO: Self = Self::NP;
508 pub const NGE: Self = Self::L;
509 pub const NL: Self = Self::GE;
510 pub const NG: Self = Self::LE;
511 pub const NLE: Self = Self::G;
512 pub const PE: Self = Self::P;
513
514 pub const fn code(self) -> u8 {
515 self as u8
516 }
517
518 pub fn invert(self) -> Self {
519 match self {
520 Self::O => Self::NO,
521 Self::NO => Self::O,
522 Self::C => Self::NC,
523 Self::NC => Self::C,
524 Self::Z => Self::NZ,
525 Self::NZ => Self::Z,
526 Self::BE => Self::A,
527 Self::A => Self::BE,
528 Self::S => Self::NS,
529 Self::NS => Self::S,
530 Self::P => Self::NP,
531 Self::NP => Self::P,
532 Self::L => Self::GE,
533 Self::GE => Self::L,
534 Self::LE => Self::G,
535 Self::G => Self::LE,
536 }
537 }
538}