LLVM  9.0.0svn
X86MCCodeEmitter.cpp
Go to the documentation of this file.
1 //===-- X86MCCodeEmitter.cpp - Convert X86 code to machine code -----------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements the X86MCCodeEmitter class.
10 //
11 //===----------------------------------------------------------------------===//
12 
16 #include "llvm/ADT/SmallVector.h"
17 #include "llvm/MC/MCCodeEmitter.h"
18 #include "llvm/MC/MCContext.h"
19 #include "llvm/MC/MCExpr.h"
20 #include "llvm/MC/MCFixup.h"
21 #include "llvm/MC/MCInst.h"
22 #include "llvm/MC/MCInstrDesc.h"
23 #include "llvm/MC/MCInstrInfo.h"
24 #include "llvm/MC/MCRegisterInfo.h"
26 #include "llvm/MC/MCSymbol.h"
29 #include <cassert>
30 #include <cstdint>
31 #include <cstdlib>
32 
33 using namespace llvm;
34 
35 #define DEBUG_TYPE "mccodeemitter"
36 
37 namespace {
38 
39 class X86MCCodeEmitter : public MCCodeEmitter {
40  const MCInstrInfo &MCII;
41  MCContext &Ctx;
42 
43 public:
44  X86MCCodeEmitter(const MCInstrInfo &mcii, MCContext &ctx)
45  : MCII(mcii), Ctx(ctx) {
46  }
47  X86MCCodeEmitter(const X86MCCodeEmitter &) = delete;
48  X86MCCodeEmitter &operator=(const X86MCCodeEmitter &) = delete;
49  ~X86MCCodeEmitter() override = default;
50 
51  bool is64BitMode(const MCSubtargetInfo &STI) const {
52  return STI.getFeatureBits()[X86::Mode64Bit];
53  }
54 
55  bool is32BitMode(const MCSubtargetInfo &STI) const {
56  return STI.getFeatureBits()[X86::Mode32Bit];
57  }
58 
59  bool is16BitMode(const MCSubtargetInfo &STI) const {
60  return STI.getFeatureBits()[X86::Mode16Bit];
61  }
62 
63  /// Is16BitMemOperand - Return true if the specified instruction has
64  /// a 16-bit memory operand. Op specifies the operand # of the memoperand.
65  bool Is16BitMemOperand(const MCInst &MI, unsigned Op,
66  const MCSubtargetInfo &STI) const {
67  const MCOperand &BaseReg = MI.getOperand(Op+X86::AddrBaseReg);
68  const MCOperand &IndexReg = MI.getOperand(Op+X86::AddrIndexReg);
69  const MCOperand &Disp = MI.getOperand(Op+X86::AddrDisp);
70 
71  if (is16BitMode(STI) && BaseReg.getReg() == 0 &&
72  Disp.isImm() && Disp.getImm() < 0x10000)
73  return true;
74  if ((BaseReg.getReg() != 0 &&
75  X86MCRegisterClasses[X86::GR16RegClassID].contains(BaseReg.getReg())) ||
76  (IndexReg.getReg() != 0 &&
77  X86MCRegisterClasses[X86::GR16RegClassID].contains(IndexReg.getReg())))
78  return true;
79  return false;
80  }
81 
82  unsigned GetX86RegNum(const MCOperand &MO) const {
83  return Ctx.getRegisterInfo()->getEncodingValue(MO.getReg()) & 0x7;
84  }
85 
86  unsigned getX86RegEncoding(const MCInst &MI, unsigned OpNum) const {
87  return Ctx.getRegisterInfo()->getEncodingValue(
88  MI.getOperand(OpNum).getReg());
89  }
90 
91  // Does this register require a bit to be set in REX prefix.
92  bool isREXExtendedReg(const MCInst &MI, unsigned OpNum) const {
93  return (getX86RegEncoding(MI, OpNum) >> 3) & 1;
94  }
95 
96  void EmitByte(uint8_t C, unsigned &CurByte, raw_ostream &OS) const {
97  OS << (char)C;
98  ++CurByte;
99  }
100 
101  void EmitConstant(uint64_t Val, unsigned Size, unsigned &CurByte,
102  raw_ostream &OS) const {
103  // Output the constant in little endian byte order.
104  for (unsigned i = 0; i != Size; ++i) {
105  EmitByte(Val & 255, CurByte, OS);
106  Val >>= 8;
107  }
108  }
109 
110  void EmitImmediate(const MCOperand &Disp, SMLoc Loc,
111  unsigned ImmSize, MCFixupKind FixupKind,
112  unsigned &CurByte, raw_ostream &OS,
114  int ImmOffset = 0) const;
115 
116  static uint8_t ModRMByte(unsigned Mod, unsigned RegOpcode, unsigned RM) {
117  assert(Mod < 4 && RegOpcode < 8 && RM < 8 && "ModRM Fields out of range!");
118  return RM | (RegOpcode << 3) | (Mod << 6);
119  }
120 
121  void EmitRegModRMByte(const MCOperand &ModRMReg, unsigned RegOpcodeFld,
122  unsigned &CurByte, raw_ostream &OS) const {
123  EmitByte(ModRMByte(3, RegOpcodeFld, GetX86RegNum(ModRMReg)), CurByte, OS);
124  }
125 
126  void EmitSIBByte(unsigned SS, unsigned Index, unsigned Base,
127  unsigned &CurByte, raw_ostream &OS) const {
128  // SIB byte is in the same format as the ModRMByte.
129  EmitByte(ModRMByte(SS, Index, Base), CurByte, OS);
130  }
131 
132  void emitMemModRMByte(const MCInst &MI, unsigned Op, unsigned RegOpcodeField,
133  uint64_t TSFlags, bool Rex, unsigned &CurByte,
135  const MCSubtargetInfo &STI) const;
136 
137  void encodeInstruction(const MCInst &MI, raw_ostream &OS,
138  SmallVectorImpl<MCFixup> &Fixups,
139  const MCSubtargetInfo &STI) const override;
140 
141  void EmitVEXOpcodePrefix(uint64_t TSFlags, unsigned &CurByte, int MemOperand,
142  const MCInst &MI, const MCInstrDesc &Desc,
143  raw_ostream &OS) const;
144 
145  void EmitSegmentOverridePrefix(unsigned &CurByte, unsigned SegOperand,
146  const MCInst &MI, raw_ostream &OS) const;
147 
148  bool emitOpcodePrefix(uint64_t TSFlags, unsigned &CurByte, int MemOperand,
149  const MCInst &MI, const MCInstrDesc &Desc,
150  const MCSubtargetInfo &STI, raw_ostream &OS) const;
151 
152  uint8_t DetermineREXPrefix(const MCInst &MI, uint64_t TSFlags,
153  int MemOperand, const MCInstrDesc &Desc) const;
154 
155  bool isPCRel32Branch(const MCInst &MI) const;
156 };
157 
158 } // end anonymous namespace
159 
160 /// isDisp8 - Return true if this signed displacement fits in a 8-bit
161 /// sign-extended field.
162 static bool isDisp8(int Value) {
163  return Value == (int8_t)Value;
164 }
165 
166 /// isCDisp8 - Return true if this signed displacement fits in a 8-bit
167 /// compressed dispacement field.
168 static bool isCDisp8(uint64_t TSFlags, int Value, int& CValue) {
169  assert(((TSFlags & X86II::EncodingMask) == X86II::EVEX) &&
170  "Compressed 8-bit displacement is only valid for EVEX inst.");
171 
172  unsigned CD8_Scale =
174  if (CD8_Scale == 0) {
175  CValue = Value;
176  return isDisp8(Value);
177  }
178 
179  unsigned Mask = CD8_Scale - 1;
180  assert((CD8_Scale & Mask) == 0 && "Invalid memory object size.");
181  if (Value & Mask) // Unaligned offset
182  return false;
183  Value /= (int)CD8_Scale;
184  bool Ret = (Value == (int8_t)Value);
185 
186  if (Ret)
187  CValue = Value;
188  return Ret;
189 }
190 
191 /// getImmFixupKind - Return the appropriate fixup kind to use for an immediate
192 /// in an instruction with the specified TSFlags.
193 static MCFixupKind getImmFixupKind(uint64_t TSFlags) {
194  unsigned Size = X86II::getSizeOfImm(TSFlags);
195  bool isPCRel = X86II::isImmPCRel(TSFlags);
196 
197  if (X86II::isImmSigned(TSFlags)) {
198  switch (Size) {
199  default: llvm_unreachable("Unsupported signed fixup size!");
200  case 4: return MCFixupKind(X86::reloc_signed_4byte);
201  }
202  }
203  return MCFixup::getKindForSize(Size, isPCRel);
204 }
205 
206 /// Is32BitMemOperand - Return true if the specified instruction has
207 /// a 32-bit memory operand. Op specifies the operand # of the memoperand.
208 static bool Is32BitMemOperand(const MCInst &MI, unsigned Op) {
209  const MCOperand &BaseReg = MI.getOperand(Op+X86::AddrBaseReg);
210  const MCOperand &IndexReg = MI.getOperand(Op+X86::AddrIndexReg);
211 
212  if ((BaseReg.getReg() != 0 &&
213  X86MCRegisterClasses[X86::GR32RegClassID].contains(BaseReg.getReg())) ||
214  (IndexReg.getReg() != 0 &&
215  X86MCRegisterClasses[X86::GR32RegClassID].contains(IndexReg.getReg())))
216  return true;
217  if (BaseReg.getReg() == X86::EIP) {
218  assert(IndexReg.getReg() == 0 && "Invalid eip-based address.");
219  return true;
220  }
221  if (IndexReg.getReg() == X86::EIZ)
222  return true;
223  return false;
224 }
225 
226 /// Is64BitMemOperand - Return true if the specified instruction has
227 /// a 64-bit memory operand. Op specifies the operand # of the memoperand.
228 #ifndef NDEBUG
229 static bool Is64BitMemOperand(const MCInst &MI, unsigned Op) {
230  const MCOperand &BaseReg = MI.getOperand(Op+X86::AddrBaseReg);
231  const MCOperand &IndexReg = MI.getOperand(Op+X86::AddrIndexReg);
232 
233  if ((BaseReg.getReg() != 0 &&
234  X86MCRegisterClasses[X86::GR64RegClassID].contains(BaseReg.getReg())) ||
235  (IndexReg.getReg() != 0 &&
236  X86MCRegisterClasses[X86::GR64RegClassID].contains(IndexReg.getReg())))
237  return true;
238  return false;
239 }
240 #endif
241 
242 /// StartsWithGlobalOffsetTable - Check if this expression starts with
243 /// _GLOBAL_OFFSET_TABLE_ and if it is of the form
244 /// _GLOBAL_OFFSET_TABLE_-symbol. This is needed to support PIC on ELF
245 /// i386 as _GLOBAL_OFFSET_TABLE_ is magical. We check only simple case that
246 /// are know to be used: _GLOBAL_OFFSET_TABLE_ by itself or at the start
247 /// of a binary expression.
252 };
255  const MCExpr *RHS = nullptr;
256  if (Expr->getKind() == MCExpr::Binary) {
257  const MCBinaryExpr *BE = static_cast<const MCBinaryExpr *>(Expr);
258  Expr = BE->getLHS();
259  RHS = BE->getRHS();
260  }
261 
262  if (Expr->getKind() != MCExpr::SymbolRef)
263  return GOT_None;
264 
265  const MCSymbolRefExpr *Ref = static_cast<const MCSymbolRefExpr*>(Expr);
266  const MCSymbol &S = Ref->getSymbol();
267  if (S.getName() != "_GLOBAL_OFFSET_TABLE_")
268  return GOT_None;
269  if (RHS && RHS->getKind() == MCExpr::SymbolRef)
270  return GOT_SymDiff;
271  return GOT_Normal;
272 }
273 
274 static bool HasSecRelSymbolRef(const MCExpr *Expr) {
275  if (Expr->getKind() == MCExpr::SymbolRef) {
276  const MCSymbolRefExpr *Ref = static_cast<const MCSymbolRefExpr*>(Expr);
277  return Ref->getKind() == MCSymbolRefExpr::VK_SECREL;
278  }
279  return false;
280 }
281 
282 bool X86MCCodeEmitter::isPCRel32Branch(const MCInst &MI) const {
283  unsigned Opcode = MI.getOpcode();
284  const MCInstrDesc &Desc = MCII.get(Opcode);
285  if ((Opcode != X86::CALL64pcrel32 && Opcode != X86::JMP_4) ||
287  return false;
288 
289  unsigned CurOp = X86II::getOperandBias(Desc);
290  const MCOperand &Op = MI.getOperand(CurOp);
291  if (!Op.isExpr())
292  return false;
293 
295  return Ref && Ref->getKind() == MCSymbolRefExpr::VK_None;
296 }
297 
298 void X86MCCodeEmitter::
299 EmitImmediate(const MCOperand &DispOp, SMLoc Loc, unsigned Size,
300  MCFixupKind FixupKind, unsigned &CurByte, raw_ostream &OS,
301  SmallVectorImpl<MCFixup> &Fixups, int ImmOffset) const {
302  const MCExpr *Expr = nullptr;
303  if (DispOp.isImm()) {
304  // If this is a simple integer displacement that doesn't require a
305  // relocation, emit it now.
306  if (FixupKind != FK_PCRel_1 &&
307  FixupKind != FK_PCRel_2 &&
308  FixupKind != FK_PCRel_4) {
309  EmitConstant(DispOp.getImm()+ImmOffset, Size, CurByte, OS);
310  return;
311  }
312  Expr = MCConstantExpr::create(DispOp.getImm(), Ctx);
313  } else {
314  Expr = DispOp.getExpr();
315  }
316 
317  // If we have an immoffset, add it to the expression.
318  if ((FixupKind == FK_Data_4 ||
319  FixupKind == FK_Data_8 ||
322  if (Kind != GOT_None) {
323  assert(ImmOffset == 0);
324 
325  if (Size == 8) {
327  } else {
328  assert(Size == 4);
330  }
331 
332  if (Kind == GOT_Normal)
333  ImmOffset = CurByte;
334  } else if (Expr->getKind() == MCExpr::SymbolRef) {
335  if (HasSecRelSymbolRef(Expr)) {
337  }
338  } else if (Expr->getKind() == MCExpr::Binary) {
339  const MCBinaryExpr *Bin = static_cast<const MCBinaryExpr*>(Expr);
340  if (HasSecRelSymbolRef(Bin->getLHS())
341  || HasSecRelSymbolRef(Bin->getRHS())) {
343  }
344  }
345  }
346 
347  // If the fixup is pc-relative, we need to bias the value to be relative to
348  // the start of the field, not the end of the field.
349  if (FixupKind == FK_PCRel_4 ||
355  ImmOffset -= 4;
356  // If this is a pc-relative load off _GLOBAL_OFFSET_TABLE_:
357  // leaq _GLOBAL_OFFSET_TABLE_(%rip), %r15
358  // this needs to be a GOTPC32 relocation.
361  }
362  if (FixupKind == FK_PCRel_2)
363  ImmOffset -= 2;
364  if (FixupKind == FK_PCRel_1)
365  ImmOffset -= 1;
366 
367  if (ImmOffset)
368  Expr = MCBinaryExpr::createAdd(Expr, MCConstantExpr::create(ImmOffset, Ctx),
369  Ctx);
370 
371  // Emit a symbolic constant as a fixup and 4 zeros.
372  Fixups.push_back(MCFixup::create(CurByte, Expr, FixupKind, Loc));
373  EmitConstant(0, Size, CurByte, OS);
374 }
375 
376 void X86MCCodeEmitter::emitMemModRMByte(const MCInst &MI, unsigned Op,
377  unsigned RegOpcodeField,
378  uint64_t TSFlags, bool Rex,
379  unsigned &CurByte, raw_ostream &OS,
380  SmallVectorImpl<MCFixup> &Fixups,
381  const MCSubtargetInfo &STI) const {
382  const MCOperand &Disp = MI.getOperand(Op+X86::AddrDisp);
383  const MCOperand &Base = MI.getOperand(Op+X86::AddrBaseReg);
384  const MCOperand &Scale = MI.getOperand(Op+X86::AddrScaleAmt);
385  const MCOperand &IndexReg = MI.getOperand(Op+X86::AddrIndexReg);
386  unsigned BaseReg = Base.getReg();
387  bool HasEVEX = (TSFlags & X86II::EncodingMask) == X86II::EVEX;
388 
389  // Handle %rip relative addressing.
390  if (BaseReg == X86::RIP ||
391  BaseReg == X86::EIP) { // [disp32+rIP] in X86-64 mode
392  assert(is64BitMode(STI) && "Rip-relative addressing requires 64-bit mode");
393  assert(IndexReg.getReg() == 0 && "Invalid rip-relative address");
394  EmitByte(ModRMByte(0, RegOpcodeField, 5), CurByte, OS);
395 
396  unsigned Opcode = MI.getOpcode();
397  // movq loads are handled with a special relocation form which allows the
398  // linker to eliminate some loads for GOT references which end up in the
399  // same linkage unit.
400  unsigned FixupKind = [=]() {
401  switch (Opcode) {
402  default:
404  case X86::MOV64rm:
405  assert(Rex);
407  case X86::CALL64m:
408  case X86::JMP64m:
409  case X86::TAILJMPm64:
410  case X86::TEST64mr:
411  case X86::ADC64rm:
412  case X86::ADD64rm:
413  case X86::AND64rm:
414  case X86::CMP64rm:
415  case X86::OR64rm:
416  case X86::SBB64rm:
417  case X86::SUB64rm:
418  case X86::XOR64rm:
421  }
422  }();
423 
424  // rip-relative addressing is actually relative to the *next* instruction.
425  // Since an immediate can follow the mod/rm byte for an instruction, this
426  // means that we need to bias the displacement field of the instruction with
427  // the size of the immediate field. If we have this case, add it into the
428  // expression to emit.
429  // Note: rip-relative addressing using immediate displacement values should
430  // not be adjusted, assuming it was the user's intent.
431  int ImmSize = !Disp.isImm() && X86II::hasImm(TSFlags)
432  ? X86II::getSizeOfImm(TSFlags)
433  : 0;
434 
435  EmitImmediate(Disp, MI.getLoc(), 4, MCFixupKind(FixupKind),
436  CurByte, OS, Fixups, -ImmSize);
437  return;
438  }
439 
440  unsigned BaseRegNo = BaseReg ? GetX86RegNum(Base) : -1U;
441 
442  // 16-bit addressing forms of the ModR/M byte have a different encoding for
443  // the R/M field and are far more limited in which registers can be used.
444  if (Is16BitMemOperand(MI, Op, STI)) {
445  if (BaseReg) {
446  // For 32-bit addressing, the row and column values in Table 2-2 are
447  // basically the same. It's AX/CX/DX/BX/SP/BP/SI/DI in that order, with
448  // some special cases. And GetX86RegNum reflects that numbering.
449  // For 16-bit addressing it's more fun, as shown in the SDM Vol 2A,
450  // Table 2-1 "16-Bit Addressing Forms with the ModR/M byte". We can only
451  // use SI/DI/BP/BX, which have "row" values 4-7 in no particular order,
452  // while values 0-3 indicate the allowed combinations (base+index) of
453  // those: 0 for BX+SI, 1 for BX+DI, 2 for BP+SI, 3 for BP+DI.
454  //
455  // R16Table[] is a lookup from the normal RegNo, to the row values from
456  // Table 2-1 for 16-bit addressing modes. Where zero means disallowed.
457  static const unsigned R16Table[] = { 0, 0, 0, 7, 0, 6, 4, 5 };
458  unsigned RMfield = R16Table[BaseRegNo];
459 
460  assert(RMfield && "invalid 16-bit base register");
461 
462  if (IndexReg.getReg()) {
463  unsigned IndexReg16 = R16Table[GetX86RegNum(IndexReg)];
464 
465  assert(IndexReg16 && "invalid 16-bit index register");
466  // We must have one of SI/DI (4,5), and one of BP/BX (6,7).
467  assert(((IndexReg16 ^ RMfield) & 2) &&
468  "invalid 16-bit base/index register combination");
469  assert(Scale.getImm() == 1 &&
470  "invalid scale for 16-bit memory reference");
471 
472  // Allow base/index to appear in either order (although GAS doesn't).
473  if (IndexReg16 & 2)
474  RMfield = (RMfield & 1) | ((7 - IndexReg16) << 1);
475  else
476  RMfield = (IndexReg16 & 1) | ((7 - RMfield) << 1);
477  }
478 
479  if (Disp.isImm() && isDisp8(Disp.getImm())) {
480  if (Disp.getImm() == 0 && RMfield != 6) {
481  // There is no displacement; just the register.
482  EmitByte(ModRMByte(0, RegOpcodeField, RMfield), CurByte, OS);
483  return;
484  }
485  // Use the [REG]+disp8 form, including for [BP] which cannot be encoded.
486  EmitByte(ModRMByte(1, RegOpcodeField, RMfield), CurByte, OS);
487  EmitImmediate(Disp, MI.getLoc(), 1, FK_Data_1, CurByte, OS, Fixups);
488  return;
489  }
490  // This is the [REG]+disp16 case.
491  EmitByte(ModRMByte(2, RegOpcodeField, RMfield), CurByte, OS);
492  } else {
493  // There is no BaseReg; this is the plain [disp16] case.
494  EmitByte(ModRMByte(0, RegOpcodeField, 6), CurByte, OS);
495  }
496 
497  // Emit 16-bit displacement for plain disp16 or [REG]+disp16 cases.
498  EmitImmediate(Disp, MI.getLoc(), 2, FK_Data_2, CurByte, OS, Fixups);
499  return;
500  }
501 
502  // Determine whether a SIB byte is needed.
503  // If no BaseReg, issue a RIP relative instruction only if the MCE can
504  // resolve addresses on-the-fly, otherwise use SIB (Intel Manual 2A, table
505  // 2-7) and absolute references.
506 
507  if (// The SIB byte must be used if there is an index register.
508  IndexReg.getReg() == 0 &&
509  // The SIB byte must be used if the base is ESP/RSP/R12, all of which
510  // encode to an R/M value of 4, which indicates that a SIB byte is
511  // present.
512  BaseRegNo != N86::ESP &&
513  // If there is no base register and we're in 64-bit mode, we need a SIB
514  // byte to emit an addr that is just 'disp32' (the non-RIP relative form).
515  (!is64BitMode(STI) || BaseReg != 0)) {
516 
517  if (BaseReg == 0) { // [disp32] in X86-32 mode
518  EmitByte(ModRMByte(0, RegOpcodeField, 5), CurByte, OS);
519  EmitImmediate(Disp, MI.getLoc(), 4, FK_Data_4, CurByte, OS, Fixups);
520  return;
521  }
522 
523  // If the base is not EBP/ESP and there is no displacement, use simple
524  // indirect register encoding, this handles addresses like [EAX]. The
525  // encoding for [EBP] with no displacement means [disp32] so we handle it
526  // by emitting a displacement of 0 below.
527  if (Disp.isImm() && Disp.getImm() == 0 && BaseRegNo != N86::EBP) {
528  EmitByte(ModRMByte(0, RegOpcodeField, BaseRegNo), CurByte, OS);
529  return;
530  }
531 
532  // Otherwise, if the displacement fits in a byte, encode as [REG+disp8].
533  if (Disp.isImm()) {
534  if (!HasEVEX && isDisp8(Disp.getImm())) {
535  EmitByte(ModRMByte(1, RegOpcodeField, BaseRegNo), CurByte, OS);
536  EmitImmediate(Disp, MI.getLoc(), 1, FK_Data_1, CurByte, OS, Fixups);
537  return;
538  }
539  // Try EVEX compressed 8-bit displacement first; if failed, fall back to
540  // 32-bit displacement.
541  int CDisp8 = 0;
542  if (HasEVEX && isCDisp8(TSFlags, Disp.getImm(), CDisp8)) {
543  EmitByte(ModRMByte(1, RegOpcodeField, BaseRegNo), CurByte, OS);
544  EmitImmediate(Disp, MI.getLoc(), 1, FK_Data_1, CurByte, OS, Fixups,
545  CDisp8 - Disp.getImm());
546  return;
547  }
548  }
549 
550  // Otherwise, emit the most general non-SIB encoding: [REG+disp32]
551  EmitByte(ModRMByte(2, RegOpcodeField, BaseRegNo), CurByte, OS);
552  unsigned Opcode = MI.getOpcode();
553  unsigned FixupKind = Opcode == X86::MOV32rm ? X86::reloc_signed_4byte_relax
555  EmitImmediate(Disp, MI.getLoc(), 4, MCFixupKind(FixupKind), CurByte, OS,
556  Fixups);
557  return;
558  }
559 
560  // We need a SIB byte, so start by outputting the ModR/M byte first
561  assert(IndexReg.getReg() != X86::ESP &&
562  IndexReg.getReg() != X86::RSP && "Cannot use ESP as index reg!");
563 
564  bool ForceDisp32 = false;
565  bool ForceDisp8 = false;
566  int CDisp8 = 0;
567  int ImmOffset = 0;
568  if (BaseReg == 0) {
569  // If there is no base register, we emit the special case SIB byte with
570  // MOD=0, BASE=5, to JUST get the index, scale, and displacement.
571  EmitByte(ModRMByte(0, RegOpcodeField, 4), CurByte, OS);
572  ForceDisp32 = true;
573  } else if (!Disp.isImm()) {
574  // Emit the normal disp32 encoding.
575  EmitByte(ModRMByte(2, RegOpcodeField, 4), CurByte, OS);
576  ForceDisp32 = true;
577  } else if (Disp.getImm() == 0 &&
578  // Base reg can't be anything that ends up with '5' as the base
579  // reg, it is the magic [*] nomenclature that indicates no base.
580  BaseRegNo != N86::EBP) {
581  // Emit no displacement ModR/M byte
582  EmitByte(ModRMByte(0, RegOpcodeField, 4), CurByte, OS);
583  } else if (!HasEVEX && isDisp8(Disp.getImm())) {
584  // Emit the disp8 encoding.
585  EmitByte(ModRMByte(1, RegOpcodeField, 4), CurByte, OS);
586  ForceDisp8 = true; // Make sure to force 8 bit disp if Base=EBP
587  } else if (HasEVEX && isCDisp8(TSFlags, Disp.getImm(), CDisp8)) {
588  // Emit the disp8 encoding.
589  EmitByte(ModRMByte(1, RegOpcodeField, 4), CurByte, OS);
590  ForceDisp8 = true; // Make sure to force 8 bit disp if Base=EBP
591  ImmOffset = CDisp8 - Disp.getImm();
592  } else {
593  // Emit the normal disp32 encoding.
594  EmitByte(ModRMByte(2, RegOpcodeField, 4), CurByte, OS);
595  }
596 
597  // Calculate what the SS field value should be...
598  static const unsigned SSTable[] = { ~0U, 0, 1, ~0U, 2, ~0U, ~0U, ~0U, 3 };
599  unsigned SS = SSTable[Scale.getImm()];
600 
601  if (BaseReg == 0) {
602  // Handle the SIB byte for the case where there is no base, see Intel
603  // Manual 2A, table 2-7. The displacement has already been output.
604  unsigned IndexRegNo;
605  if (IndexReg.getReg())
606  IndexRegNo = GetX86RegNum(IndexReg);
607  else // Examples: [ESP+1*<noreg>+4] or [scaled idx]+disp32 (MOD=0,BASE=5)
608  IndexRegNo = 4;
609  EmitSIBByte(SS, IndexRegNo, 5, CurByte, OS);
610  } else {
611  unsigned IndexRegNo;
612  if (IndexReg.getReg())
613  IndexRegNo = GetX86RegNum(IndexReg);
614  else
615  IndexRegNo = 4; // For example [ESP+1*<noreg>+4]
616  EmitSIBByte(SS, IndexRegNo, GetX86RegNum(Base), CurByte, OS);
617  }
618 
619  // Do we need to output a displacement?
620  if (ForceDisp8)
621  EmitImmediate(Disp, MI.getLoc(), 1, FK_Data_1, CurByte, OS, Fixups, ImmOffset);
622  else if (ForceDisp32 || Disp.getImm() != 0)
623  EmitImmediate(Disp, MI.getLoc(), 4, MCFixupKind(X86::reloc_signed_4byte),
624  CurByte, OS, Fixups);
625 }
626 
627 /// EmitVEXOpcodePrefix - AVX instructions are encoded using a opcode prefix
628 /// called VEX.
629 void X86MCCodeEmitter::EmitVEXOpcodePrefix(uint64_t TSFlags, unsigned &CurByte,
630  int MemOperand, const MCInst &MI,
631  const MCInstrDesc &Desc,
632  raw_ostream &OS) const {
633  assert(!(TSFlags & X86II::LOCK) && "Can't have LOCK VEX.");
634 
635  uint64_t Encoding = TSFlags & X86II::EncodingMask;
636  bool HasEVEX_K = TSFlags & X86II::EVEX_K;
637  bool HasVEX_4V = TSFlags & X86II::VEX_4V;
638  bool HasEVEX_RC = TSFlags & X86II::EVEX_RC;
639 
640  // VEX_R: opcode externsion equivalent to REX.R in
641  // 1's complement (inverted) form
642  //
643  // 1: Same as REX_R=0 (must be 1 in 32-bit mode)
644  // 0: Same as REX_R=1 (64 bit mode only)
645  //
646  uint8_t VEX_R = 0x1;
647  uint8_t EVEX_R2 = 0x1;
648 
649  // VEX_X: equivalent to REX.X, only used when a
650  // register is used for index in SIB Byte.
651  //
652  // 1: Same as REX.X=0 (must be 1 in 32-bit mode)
653  // 0: Same as REX.X=1 (64-bit mode only)
654  uint8_t VEX_X = 0x1;
655 
656  // VEX_B:
657  //
658  // 1: Same as REX_B=0 (ignored in 32-bit mode)
659  // 0: Same as REX_B=1 (64 bit mode only)
660  //
661  uint8_t VEX_B = 0x1;
662 
663  // VEX_W: opcode specific (use like REX.W, or used for
664  // opcode extension, or ignored, depending on the opcode byte)
665  uint8_t VEX_W = (TSFlags & X86II::VEX_W) ? 1 : 0;
666 
667  // VEX_5M (VEX m-mmmmm field):
668  //
669  // 0b00000: Reserved for future use
670  // 0b00001: implied 0F leading opcode
671  // 0b00010: implied 0F 38 leading opcode bytes
672  // 0b00011: implied 0F 3A leading opcode bytes
673  // 0b00100-0b11111: Reserved for future use
674  // 0b01000: XOP map select - 08h instructions with imm byte
675  // 0b01001: XOP map select - 09h instructions with no imm byte
676  // 0b01010: XOP map select - 0Ah instructions with imm dword
677  uint8_t VEX_5M;
678  switch (TSFlags & X86II::OpMapMask) {
679  default: llvm_unreachable("Invalid prefix!");
680  case X86II::TB: VEX_5M = 0x1; break; // 0F
681  case X86II::T8: VEX_5M = 0x2; break; // 0F 38
682  case X86II::TA: VEX_5M = 0x3; break; // 0F 3A
683  case X86II::XOP8: VEX_5M = 0x8; break;
684  case X86II::XOP9: VEX_5M = 0x9; break;
685  case X86II::XOPA: VEX_5M = 0xA; break;
686  }
687 
688  // VEX_4V (VEX vvvv field): a register specifier
689  // (in 1's complement form) or 1111 if unused.
690  uint8_t VEX_4V = 0xf;
691  uint8_t EVEX_V2 = 0x1;
692 
693  // EVEX_L2/VEX_L (Vector Length):
694  //
695  // L2 L
696  // 0 0: scalar or 128-bit vector
697  // 0 1: 256-bit vector
698  // 1 0: 512-bit vector
699  //
700  uint8_t VEX_L = (TSFlags & X86II::VEX_L) ? 1 : 0;
701  uint8_t EVEX_L2 = (TSFlags & X86II::EVEX_L2) ? 1 : 0;
702 
703  // VEX_PP: opcode extension providing equivalent
704  // functionality of a SIMD prefix
705  //
706  // 0b00: None
707  // 0b01: 66
708  // 0b10: F3
709  // 0b11: F2
710  //
711  uint8_t VEX_PP = 0;
712  switch (TSFlags & X86II::OpPrefixMask) {
713  case X86II::PD: VEX_PP = 0x1; break; // 66
714  case X86II::XS: VEX_PP = 0x2; break; // F3
715  case X86II::XD: VEX_PP = 0x3; break; // F2
716  }
717 
718  // EVEX_U
719  uint8_t EVEX_U = 1; // Always '1' so far
720 
721  // EVEX_z
722  uint8_t EVEX_z = (HasEVEX_K && (TSFlags & X86II::EVEX_Z)) ? 1 : 0;
723 
724  // EVEX_b
725  uint8_t EVEX_b = (TSFlags & X86II::EVEX_B) ? 1 : 0;
726 
727  // EVEX_rc
728  uint8_t EVEX_rc = 0;
729 
730  // EVEX_aaa
731  uint8_t EVEX_aaa = 0;
732 
733  bool EncodeRC = false;
734 
735  // Classify VEX_B, VEX_4V, VEX_R, VEX_X
736  unsigned NumOps = Desc.getNumOperands();
737  unsigned CurOp = X86II::getOperandBias(Desc);
738 
739  switch (TSFlags & X86II::FormMask) {
740  default: llvm_unreachable("Unexpected form in EmitVEXOpcodePrefix!");
741  case X86II::RawFrm:
742  break;
743  case X86II::MRMDestMem: {
744  // MRMDestMem instructions forms:
745  // MemAddr, src1(ModR/M)
746  // MemAddr, src1(VEX_4V), src2(ModR/M)
747  // MemAddr, src1(ModR/M), imm8
748  //
749  unsigned BaseRegEnc = getX86RegEncoding(MI, MemOperand + X86::AddrBaseReg);
750  VEX_B = ~(BaseRegEnc >> 3) & 1;
751  unsigned IndexRegEnc = getX86RegEncoding(MI, MemOperand+X86::AddrIndexReg);
752  VEX_X = ~(IndexRegEnc >> 3) & 1;
753  if (!HasVEX_4V) // Only needed with VSIB which don't use VVVV.
754  EVEX_V2 = ~(IndexRegEnc >> 4) & 1;
755 
756  CurOp += X86::AddrNumOperands;
757 
758  if (HasEVEX_K)
759  EVEX_aaa = getX86RegEncoding(MI, CurOp++);
760 
761  if (HasVEX_4V) {
762  unsigned VRegEnc = getX86RegEncoding(MI, CurOp++);
763  VEX_4V = ~VRegEnc & 0xf;
764  EVEX_V2 = ~(VRegEnc >> 4) & 1;
765  }
766 
767  unsigned RegEnc = getX86RegEncoding(MI, CurOp++);
768  VEX_R = ~(RegEnc >> 3) & 1;
769  EVEX_R2 = ~(RegEnc >> 4) & 1;
770  break;
771  }
772  case X86II::MRMSrcMem: {
773  // MRMSrcMem instructions forms:
774  // src1(ModR/M), MemAddr
775  // src1(ModR/M), src2(VEX_4V), MemAddr
776  // src1(ModR/M), MemAddr, imm8
777  // src1(ModR/M), MemAddr, src2(Imm[7:4])
778  //
779  // FMA4:
780  // dst(ModR/M.reg), src1(VEX_4V), src2(ModR/M), src3(Imm[7:4])
781  unsigned RegEnc = getX86RegEncoding(MI, CurOp++);
782  VEX_R = ~(RegEnc >> 3) & 1;
783  EVEX_R2 = ~(RegEnc >> 4) & 1;
784 
785  if (HasEVEX_K)
786  EVEX_aaa = getX86RegEncoding(MI, CurOp++);
787 
788  if (HasVEX_4V) {
789  unsigned VRegEnc = getX86RegEncoding(MI, CurOp++);
790  VEX_4V = ~VRegEnc & 0xf;
791  EVEX_V2 = ~(VRegEnc >> 4) & 1;
792  }
793 
794  unsigned BaseRegEnc = getX86RegEncoding(MI, MemOperand + X86::AddrBaseReg);
795  VEX_B = ~(BaseRegEnc >> 3) & 1;
796  unsigned IndexRegEnc = getX86RegEncoding(MI, MemOperand+X86::AddrIndexReg);
797  VEX_X = ~(IndexRegEnc >> 3) & 1;
798  if (!HasVEX_4V) // Only needed with VSIB which don't use VVVV.
799  EVEX_V2 = ~(IndexRegEnc >> 4) & 1;
800 
801  break;
802  }
803  case X86II::MRMSrcMem4VOp3: {
804  // Instruction format for 4VOp3:
805  // src1(ModR/M), MemAddr, src3(VEX_4V)
806  unsigned RegEnc = getX86RegEncoding(MI, CurOp++);
807  VEX_R = ~(RegEnc >> 3) & 1;
808 
809  unsigned BaseRegEnc = getX86RegEncoding(MI, MemOperand + X86::AddrBaseReg);
810  VEX_B = ~(BaseRegEnc >> 3) & 1;
811  unsigned IndexRegEnc = getX86RegEncoding(MI, MemOperand+X86::AddrIndexReg);
812  VEX_X = ~(IndexRegEnc >> 3) & 1;
813 
814  VEX_4V = ~getX86RegEncoding(MI, CurOp + X86::AddrNumOperands) & 0xf;
815  break;
816  }
817  case X86II::MRMSrcMemOp4: {
818  // dst(ModR/M.reg), src1(VEX_4V), src2(Imm[7:4]), src3(ModR/M),
819  unsigned RegEnc = getX86RegEncoding(MI, CurOp++);
820  VEX_R = ~(RegEnc >> 3) & 1;
821 
822  unsigned VRegEnc = getX86RegEncoding(MI, CurOp++);
823  VEX_4V = ~VRegEnc & 0xf;
824 
825  unsigned BaseRegEnc = getX86RegEncoding(MI, MemOperand + X86::AddrBaseReg);
826  VEX_B = ~(BaseRegEnc >> 3) & 1;
827  unsigned IndexRegEnc = getX86RegEncoding(MI, MemOperand+X86::AddrIndexReg);
828  VEX_X = ~(IndexRegEnc >> 3) & 1;
829  break;
830  }
831  case X86II::MRM0m: case X86II::MRM1m:
832  case X86II::MRM2m: case X86II::MRM3m:
833  case X86II::MRM4m: case X86II::MRM5m:
834  case X86II::MRM6m: case X86II::MRM7m: {
835  // MRM[0-9]m instructions forms:
836  // MemAddr
837  // src1(VEX_4V), MemAddr
838  if (HasVEX_4V) {
839  unsigned VRegEnc = getX86RegEncoding(MI, CurOp++);
840  VEX_4V = ~VRegEnc & 0xf;
841  EVEX_V2 = ~(VRegEnc >> 4) & 1;
842  }
843 
844  if (HasEVEX_K)
845  EVEX_aaa = getX86RegEncoding(MI, CurOp++);
846 
847  unsigned BaseRegEnc = getX86RegEncoding(MI, MemOperand + X86::AddrBaseReg);
848  VEX_B = ~(BaseRegEnc >> 3) & 1;
849  unsigned IndexRegEnc = getX86RegEncoding(MI, MemOperand+X86::AddrIndexReg);
850  VEX_X = ~(IndexRegEnc >> 3) & 1;
851  break;
852  }
853  case X86II::MRMSrcReg: {
854  // MRMSrcReg instructions forms:
855  // dst(ModR/M), src1(VEX_4V), src2(ModR/M), src3(Imm[7:4])
856  // dst(ModR/M), src1(ModR/M)
857  // dst(ModR/M), src1(ModR/M), imm8
858  //
859  // FMA4:
860  // dst(ModR/M.reg), src1(VEX_4V), src2(Imm[7:4]), src3(ModR/M),
861  unsigned RegEnc = getX86RegEncoding(MI, CurOp++);
862  VEX_R = ~(RegEnc >> 3) & 1;
863  EVEX_R2 = ~(RegEnc >> 4) & 1;
864 
865  if (HasEVEX_K)
866  EVEX_aaa = getX86RegEncoding(MI, CurOp++);
867 
868  if (HasVEX_4V) {
869  unsigned VRegEnc = getX86RegEncoding(MI, CurOp++);
870  VEX_4V = ~VRegEnc & 0xf;
871  EVEX_V2 = ~(VRegEnc >> 4) & 1;
872  }
873 
874  RegEnc = getX86RegEncoding(MI, CurOp++);
875  VEX_B = ~(RegEnc >> 3) & 1;
876  VEX_X = ~(RegEnc >> 4) & 1;
877 
878  if (EVEX_b) {
879  if (HasEVEX_RC) {
880  unsigned RcOperand = NumOps-1;
881  assert(RcOperand >= CurOp);
882  EVEX_rc = MI.getOperand(RcOperand).getImm();
883  assert(EVEX_rc <= 3 && "Invalid rounding control!");
884  }
885  EncodeRC = true;
886  }
887  break;
888  }
889  case X86II::MRMSrcReg4VOp3: {
890  // Instruction format for 4VOp3:
891  // src1(ModR/M), src2(ModR/M), src3(VEX_4V)
892  unsigned RegEnc = getX86RegEncoding(MI, CurOp++);
893  VEX_R = ~(RegEnc >> 3) & 1;
894 
895  RegEnc = getX86RegEncoding(MI, CurOp++);
896  VEX_B = ~(RegEnc >> 3) & 1;
897 
898  VEX_4V = ~getX86RegEncoding(MI, CurOp++) & 0xf;
899  break;
900  }
901  case X86II::MRMSrcRegOp4: {
902  // dst(ModR/M.reg), src1(VEX_4V), src2(Imm[7:4]), src3(ModR/M),
903  unsigned RegEnc = getX86RegEncoding(MI, CurOp++);
904  VEX_R = ~(RegEnc >> 3) & 1;
905 
906  unsigned VRegEnc = getX86RegEncoding(MI, CurOp++);
907  VEX_4V = ~VRegEnc & 0xf;
908 
909  // Skip second register source (encoded in Imm[7:4])
910  ++CurOp;
911 
912  RegEnc = getX86RegEncoding(MI, CurOp++);
913  VEX_B = ~(RegEnc >> 3) & 1;
914  VEX_X = ~(RegEnc >> 4) & 1;
915  break;
916  }
917  case X86II::MRMDestReg: {
918  // MRMDestReg instructions forms:
919  // dst(ModR/M), src(ModR/M)
920  // dst(ModR/M), src(ModR/M), imm8
921  // dst(ModR/M), src1(VEX_4V), src2(ModR/M)
922  unsigned RegEnc = getX86RegEncoding(MI, CurOp++);
923  VEX_B = ~(RegEnc >> 3) & 1;
924  VEX_X = ~(RegEnc >> 4) & 1;
925 
926  if (HasEVEX_K)
927  EVEX_aaa = getX86RegEncoding(MI, CurOp++);
928 
929  if (HasVEX_4V) {
930  unsigned VRegEnc = getX86RegEncoding(MI, CurOp++);
931  VEX_4V = ~VRegEnc & 0xf;
932  EVEX_V2 = ~(VRegEnc >> 4) & 1;
933  }
934 
935  RegEnc = getX86RegEncoding(MI, CurOp++);
936  VEX_R = ~(RegEnc >> 3) & 1;
937  EVEX_R2 = ~(RegEnc >> 4) & 1;
938  if (EVEX_b)
939  EncodeRC = true;
940  break;
941  }
942  case X86II::MRM0r: case X86II::MRM1r:
943  case X86II::MRM2r: case X86II::MRM3r:
944  case X86II::MRM4r: case X86II::MRM5r:
945  case X86II::MRM6r: case X86II::MRM7r: {
946  // MRM0r-MRM7r instructions forms:
947  // dst(VEX_4V), src(ModR/M), imm8
948  if (HasVEX_4V) {
949  unsigned VRegEnc = getX86RegEncoding(MI, CurOp++);
950  VEX_4V = ~VRegEnc & 0xf;
951  EVEX_V2 = ~(VRegEnc >> 4) & 1;
952  }
953  if (HasEVEX_K)
954  EVEX_aaa = getX86RegEncoding(MI, CurOp++);
955 
956  unsigned RegEnc = getX86RegEncoding(MI, CurOp++);
957  VEX_B = ~(RegEnc >> 3) & 1;
958  VEX_X = ~(RegEnc >> 4) & 1;
959  break;
960  }
961  }
962 
963  if (Encoding == X86II::VEX || Encoding == X86II::XOP) {
964  // VEX opcode prefix can have 2 or 3 bytes
965  //
966  // 3 bytes:
967  // +-----+ +--------------+ +-------------------+
968  // | C4h | | RXB | m-mmmm | | W | vvvv | L | pp |
969  // +-----+ +--------------+ +-------------------+
970  // 2 bytes:
971  // +-----+ +-------------------+
972  // | C5h | | R | vvvv | L | pp |
973  // +-----+ +-------------------+
974  //
975  // XOP uses a similar prefix:
976  // +-----+ +--------------+ +-------------------+
977  // | 8Fh | | RXB | m-mmmm | | W | vvvv | L | pp |
978  // +-----+ +--------------+ +-------------------+
979  uint8_t LastByte = VEX_PP | (VEX_L << 2) | (VEX_4V << 3);
980 
981  // Can we use the 2 byte VEX prefix?
982  if (!(MI.getFlags() & X86::IP_USE_VEX3) &&
983  Encoding == X86II::VEX && VEX_B && VEX_X && !VEX_W && (VEX_5M == 1)) {
984  EmitByte(0xC5, CurByte, OS);
985  EmitByte(LastByte | (VEX_R << 7), CurByte, OS);
986  return;
987  }
988 
989  // 3 byte VEX prefix
990  EmitByte(Encoding == X86II::XOP ? 0x8F : 0xC4, CurByte, OS);
991  EmitByte(VEX_R << 7 | VEX_X << 6 | VEX_B << 5 | VEX_5M, CurByte, OS);
992  EmitByte(LastByte | (VEX_W << 7), CurByte, OS);
993  } else {
994  assert(Encoding == X86II::EVEX && "unknown encoding!");
995  // EVEX opcode prefix can have 4 bytes
996  //
997  // +-----+ +--------------+ +-------------------+ +------------------------+
998  // | 62h | | RXBR' | 00mm | | W | vvvv | U | pp | | z | L'L | b | v' | aaa |
999  // +-----+ +--------------+ +-------------------+ +------------------------+
1000  assert((VEX_5M & 0x3) == VEX_5M
1001  && "More than 2 significant bits in VEX.m-mmmm fields for EVEX!");
1002 
1003  EmitByte(0x62, CurByte, OS);
1004  EmitByte((VEX_R << 7) |
1005  (VEX_X << 6) |
1006  (VEX_B << 5) |
1007  (EVEX_R2 << 4) |
1008  VEX_5M, CurByte, OS);
1009  EmitByte((VEX_W << 7) |
1010  (VEX_4V << 3) |
1011  (EVEX_U << 2) |
1012  VEX_PP, CurByte, OS);
1013  if (EncodeRC)
1014  EmitByte((EVEX_z << 7) |
1015  (EVEX_rc << 5) |
1016  (EVEX_b << 4) |
1017  (EVEX_V2 << 3) |
1018  EVEX_aaa, CurByte, OS);
1019  else
1020  EmitByte((EVEX_z << 7) |
1021  (EVEX_L2 << 6) |
1022  (VEX_L << 5) |
1023  (EVEX_b << 4) |
1024  (EVEX_V2 << 3) |
1025  EVEX_aaa, CurByte, OS);
1026  }
1027 }
1028 
1029 /// DetermineREXPrefix - Determine if the MCInst has to be encoded with a X86-64
1030 /// REX prefix which specifies 1) 64-bit instructions, 2) non-default operand
1031 /// size, and 3) use of X86-64 extended registers.
1032 uint8_t X86MCCodeEmitter::DetermineREXPrefix(const MCInst &MI, uint64_t TSFlags,
1033  int MemOperand,
1034  const MCInstrDesc &Desc) const {
1035  uint8_t REX = 0;
1036  bool UsesHighByteReg = false;
1037 
1038  if (TSFlags & X86II::REX_W)
1039  REX |= 1 << 3; // set REX.W
1040 
1041  if (MI.getNumOperands() == 0) return REX;
1042 
1043  unsigned NumOps = MI.getNumOperands();
1044  unsigned CurOp = X86II::getOperandBias(Desc);
1045 
1046  // If it accesses SPL, BPL, SIL, or DIL, then it requires a 0x40 REX prefix.
1047  for (unsigned i = CurOp; i != NumOps; ++i) {
1048  const MCOperand &MO = MI.getOperand(i);
1049  if (!MO.isReg()) continue;
1050  unsigned Reg = MO.getReg();
1051  if (Reg == X86::AH || Reg == X86::BH || Reg == X86::CH || Reg == X86::DH)
1052  UsesHighByteReg = true;
1054  // FIXME: The caller of DetermineREXPrefix slaps this prefix onto anything
1055  // that returns non-zero.
1056  REX |= 0x40; // REX fixed encoding prefix
1057  }
1058 
1059  switch (TSFlags & X86II::FormMask) {
1060  case X86II::AddRegFrm:
1061  REX |= isREXExtendedReg(MI, CurOp++) << 0; // REX.B
1062  break;
1063  case X86II::MRMSrcReg:
1064  case X86II::MRMSrcRegCC:
1065  REX |= isREXExtendedReg(MI, CurOp++) << 2; // REX.R
1066  REX |= isREXExtendedReg(MI, CurOp++) << 0; // REX.B
1067  break;
1068  case X86II::MRMSrcMem:
1069  case X86II::MRMSrcMemCC:
1070  REX |= isREXExtendedReg(MI, CurOp++) << 2; // REX.R
1071  REX |= isREXExtendedReg(MI, MemOperand+X86::AddrBaseReg) << 0; // REX.B
1072  REX |= isREXExtendedReg(MI, MemOperand+X86::AddrIndexReg) << 1; // REX.X
1073  CurOp += X86::AddrNumOperands;
1074  break;
1075  case X86II::MRMDestReg:
1076  REX |= isREXExtendedReg(MI, CurOp++) << 0; // REX.B
1077  REX |= isREXExtendedReg(MI, CurOp++) << 2; // REX.R
1078  break;
1079  case X86II::MRMDestMem:
1080  REX |= isREXExtendedReg(MI, MemOperand+X86::AddrBaseReg) << 0; // REX.B
1081  REX |= isREXExtendedReg(MI, MemOperand+X86::AddrIndexReg) << 1; // REX.X
1082  CurOp += X86::AddrNumOperands;
1083  REX |= isREXExtendedReg(MI, CurOp++) << 2; // REX.R
1084  break;
1085  case X86II::MRMXmCC: case X86II::MRMXm:
1086  case X86II::MRM0m: case X86II::MRM1m:
1087  case X86II::MRM2m: case X86II::MRM3m:
1088  case X86II::MRM4m: case X86II::MRM5m:
1089  case X86II::MRM6m: case X86II::MRM7m:
1090  REX |= isREXExtendedReg(MI, MemOperand+X86::AddrBaseReg) << 0; // REX.B
1091  REX |= isREXExtendedReg(MI, MemOperand+X86::AddrIndexReg) << 1; // REX.X
1092  break;
1093  case X86II::MRMXrCC: case X86II::MRMXr:
1094  case X86II::MRM0r: case X86II::MRM1r:
1095  case X86II::MRM2r: case X86II::MRM3r:
1096  case X86II::MRM4r: case X86II::MRM5r:
1097  case X86II::MRM6r: case X86II::MRM7r:
1098  REX |= isREXExtendedReg(MI, CurOp++) << 0; // REX.B
1099  break;
1100  }
1101  if (REX && UsesHighByteReg)
1102  report_fatal_error("Cannot encode high byte register in REX-prefixed instruction");
1103 
1104  return REX;
1105 }
1106 
1107 /// EmitSegmentOverridePrefix - Emit segment override opcode prefix as needed
1108 void X86MCCodeEmitter::EmitSegmentOverridePrefix(unsigned &CurByte,
1109  unsigned SegOperand,
1110  const MCInst &MI,
1111  raw_ostream &OS) const {
1112  // Check for explicit segment override on memory operand.
1113  switch (MI.getOperand(SegOperand).getReg()) {
1114  default: llvm_unreachable("Unknown segment register!");
1115  case 0: break;
1116  case X86::CS: EmitByte(0x2E, CurByte, OS); break;
1117  case X86::SS: EmitByte(0x36, CurByte, OS); break;
1118  case X86::DS: EmitByte(0x3E, CurByte, OS); break;
1119  case X86::ES: EmitByte(0x26, CurByte, OS); break;
1120  case X86::FS: EmitByte(0x64, CurByte, OS); break;
1121  case X86::GS: EmitByte(0x65, CurByte, OS); break;
1122  }
1123 }
1124 
1125 /// Emit all instruction prefixes prior to the opcode.
1126 ///
1127 /// MemOperand is the operand # of the start of a memory operand if present. If
1128 /// Not present, it is -1.
1129 ///
1130 /// Returns true if a REX prefix was used.
1131 bool X86MCCodeEmitter::emitOpcodePrefix(uint64_t TSFlags, unsigned &CurByte,
1132  int MemOperand, const MCInst &MI,
1133  const MCInstrDesc &Desc,
1134  const MCSubtargetInfo &STI,
1135  raw_ostream &OS) const {
1136  bool Ret = false;
1137  // Emit the operand size opcode prefix as needed.
1138  if ((TSFlags & X86II::OpSizeMask) == (is16BitMode(STI) ? X86II::OpSize32
1139  : X86II::OpSize16))
1140  EmitByte(0x66, CurByte, OS);
1141 
1142  // Emit the LOCK opcode prefix.
1143  if (TSFlags & X86II::LOCK || MI.getFlags() & X86::IP_HAS_LOCK)
1144  EmitByte(0xF0, CurByte, OS);
1145 
1146  // Emit the NOTRACK opcode prefix.
1147  if (TSFlags & X86II::NOTRACK || MI.getFlags() & X86::IP_HAS_NOTRACK)
1148  EmitByte(0x3E, CurByte, OS);
1149 
1150  switch (TSFlags & X86II::OpPrefixMask) {
1151  case X86II::PD: // 66
1152  EmitByte(0x66, CurByte, OS);
1153  break;
1154  case X86II::XS: // F3
1155  EmitByte(0xF3, CurByte, OS);
1156  break;
1157  case X86II::XD: // F2
1158  EmitByte(0xF2, CurByte, OS);
1159  break;
1160  }
1161 
1162  // Handle REX prefix.
1163  // FIXME: Can this come before F2 etc to simplify emission?
1164  if (is64BitMode(STI)) {
1165  if (uint8_t REX = DetermineREXPrefix(MI, TSFlags, MemOperand, Desc)) {
1166  EmitByte(0x40 | REX, CurByte, OS);
1167  Ret = true;
1168  }
1169  } else {
1170  assert(!(TSFlags & X86II::REX_W) && "REX.W requires 64bit mode.");
1171  }
1172 
1173  // 0x0F escape code must be emitted just before the opcode.
1174  switch (TSFlags & X86II::OpMapMask) {
1175  case X86II::TB: // Two-byte opcode map
1176  case X86II::T8: // 0F 38
1177  case X86II::TA: // 0F 3A
1178  case X86II::ThreeDNow: // 0F 0F, second 0F emitted by caller.
1179  EmitByte(0x0F, CurByte, OS);
1180  break;
1181  }
1182 
1183  switch (TSFlags & X86II::OpMapMask) {
1184  case X86II::T8: // 0F 38
1185  EmitByte(0x38, CurByte, OS);
1186  break;
1187  case X86II::TA: // 0F 3A
1188  EmitByte(0x3A, CurByte, OS);
1189  break;
1190  }
1191  return Ret;
1192 }
1193 
1194 void X86MCCodeEmitter::
1195 encodeInstruction(const MCInst &MI, raw_ostream &OS,
1196  SmallVectorImpl<MCFixup> &Fixups,
1197  const MCSubtargetInfo &STI) const {
1198  unsigned Opcode = MI.getOpcode();
1199  const MCInstrDesc &Desc = MCII.get(Opcode);
1200  uint64_t TSFlags = Desc.TSFlags;
1201  unsigned Flags = MI.getFlags();
1202 
1203  // Pseudo instructions don't get encoded.
1204  if ((TSFlags & X86II::FormMask) == X86II::Pseudo)
1205  return;
1206 
1207  unsigned NumOps = Desc.getNumOperands();
1208  unsigned CurOp = X86II::getOperandBias(Desc);
1209 
1210  // Keep track of the current byte being emitted.
1211  unsigned CurByte = 0;
1212 
1213  // Encoding type for this instruction.
1214  uint64_t Encoding = TSFlags & X86II::EncodingMask;
1215 
1216  // It uses the VEX.VVVV field?
1217  bool HasVEX_4V = TSFlags & X86II::VEX_4V;
1218  bool HasVEX_I8Reg = (TSFlags & X86II::ImmMask) == X86II::Imm8Reg;
1219 
1220  // It uses the EVEX.aaa field?
1221  bool HasEVEX_K = TSFlags & X86II::EVEX_K;
1222  bool HasEVEX_RC = TSFlags & X86II::EVEX_RC;
1223 
1224  // Used if a register is encoded in 7:4 of immediate.
1225  unsigned I8RegNum = 0;
1226 
1227  // Determine where the memory operand starts, if present.
1228  int MemoryOperand = X86II::getMemoryOperandNo(TSFlags);
1229  if (MemoryOperand != -1) MemoryOperand += CurOp;
1230 
1231  // Emit segment override opcode prefix as needed.
1232  if (MemoryOperand >= 0)
1233  EmitSegmentOverridePrefix(CurByte, MemoryOperand+X86::AddrSegmentReg,
1234  MI, OS);
1235 
1236  // Emit the repeat opcode prefix as needed.
1237  if (TSFlags & X86II::REP || Flags & X86::IP_HAS_REPEAT)
1238  EmitByte(0xF3, CurByte, OS);
1239  if (Flags & X86::IP_HAS_REPEAT_NE)
1240  EmitByte(0xF2, CurByte, OS);
1241 
1242  // Emit the address size opcode prefix as needed.
1243  bool need_address_override;
1244  uint64_t AdSize = TSFlags & X86II::AdSizeMask;
1245  if ((is16BitMode(STI) && AdSize == X86II::AdSize32) ||
1246  (is32BitMode(STI) && AdSize == X86II::AdSize16) ||
1247  (is64BitMode(STI) && AdSize == X86II::AdSize32)) {
1248  need_address_override = true;
1249  } else if (MemoryOperand < 0) {
1250  need_address_override = false;
1251  } else if (is64BitMode(STI)) {
1252  assert(!Is16BitMemOperand(MI, MemoryOperand, STI));
1253  need_address_override = Is32BitMemOperand(MI, MemoryOperand);
1254  } else if (is32BitMode(STI)) {
1255  assert(!Is64BitMemOperand(MI, MemoryOperand));
1256  need_address_override = Is16BitMemOperand(MI, MemoryOperand, STI);
1257  } else {
1258  assert(is16BitMode(STI));
1259  assert(!Is64BitMemOperand(MI, MemoryOperand));
1260  need_address_override = !Is16BitMemOperand(MI, MemoryOperand, STI);
1261  }
1262 
1263  if (need_address_override)
1264  EmitByte(0x67, CurByte, OS);
1265 
1266  bool Rex = false;
1267  if (Encoding == 0)
1268  Rex = emitOpcodePrefix(TSFlags, CurByte, MemoryOperand, MI, Desc, STI, OS);
1269  else
1270  EmitVEXOpcodePrefix(TSFlags, CurByte, MemoryOperand, MI, Desc, OS);
1271 
1272  uint8_t BaseOpcode = X86II::getBaseOpcodeFor(TSFlags);
1273 
1274  if ((TSFlags & X86II::OpMapMask) == X86II::ThreeDNow)
1275  BaseOpcode = 0x0F; // Weird 3DNow! encoding.
1276 
1277  unsigned OpcodeOffset = 0;
1278 
1279  uint64_t Form = TSFlags & X86II::FormMask;
1280  switch (Form) {
1281  default: errs() << "FORM: " << Form << "\n";
1282  llvm_unreachable("Unknown FormMask value in X86MCCodeEmitter!");
1283  case X86II::Pseudo:
1284  llvm_unreachable("Pseudo instruction shouldn't be emitted");
1285  case X86II::RawFrmDstSrc: {
1286  unsigned siReg = MI.getOperand(1).getReg();
1287  assert(((siReg == X86::SI && MI.getOperand(0).getReg() == X86::DI) ||
1288  (siReg == X86::ESI && MI.getOperand(0).getReg() == X86::EDI) ||
1289  (siReg == X86::RSI && MI.getOperand(0).getReg() == X86::RDI)) &&
1290  "SI and DI register sizes do not match");
1291  // Emit segment override opcode prefix as needed (not for %ds).
1292  if (MI.getOperand(2).getReg() != X86::DS)
1293  EmitSegmentOverridePrefix(CurByte, 2, MI, OS);
1294  // Emit AdSize prefix as needed.
1295  if ((!is32BitMode(STI) && siReg == X86::ESI) ||
1296  (is32BitMode(STI) && siReg == X86::SI))
1297  EmitByte(0x67, CurByte, OS);
1298  CurOp += 3; // Consume operands.
1299  EmitByte(BaseOpcode, CurByte, OS);
1300  break;
1301  }
1302  case X86II::RawFrmSrc: {
1303  unsigned siReg = MI.getOperand(0).getReg();
1304  // Emit segment override opcode prefix as needed (not for %ds).
1305  if (MI.getOperand(1).getReg() != X86::DS)
1306  EmitSegmentOverridePrefix(CurByte, 1, MI, OS);
1307  // Emit AdSize prefix as needed.
1308  if ((!is32BitMode(STI) && siReg == X86::ESI) ||
1309  (is32BitMode(STI) && siReg == X86::SI))
1310  EmitByte(0x67, CurByte, OS);
1311  CurOp += 2; // Consume operands.
1312  EmitByte(BaseOpcode, CurByte, OS);
1313  break;
1314  }
1315  case X86II::RawFrmDst: {
1316  unsigned siReg = MI.getOperand(0).getReg();
1317  // Emit AdSize prefix as needed.
1318  if ((!is32BitMode(STI) && siReg == X86::EDI) ||
1319  (is32BitMode(STI) && siReg == X86::DI))
1320  EmitByte(0x67, CurByte, OS);
1321  ++CurOp; // Consume operand.
1322  EmitByte(BaseOpcode, CurByte, OS);
1323  break;
1324  }
1325  case X86II::AddCCFrm: {
1326  // This will be added to the opcode in the fallthrough.
1327  OpcodeOffset = MI.getOperand(NumOps - 1).getImm();
1328  assert(OpcodeOffset < 16 && "Unexpected opcode offset!");
1329  --NumOps; // Drop the operand from the end.
1331  case X86II::RawFrm:
1332  EmitByte(BaseOpcode + OpcodeOffset, CurByte, OS);
1333 
1334  if (!is64BitMode(STI) || !isPCRel32Branch(MI))
1335  break;
1336 
1337  const MCOperand &Op = MI.getOperand(CurOp++);
1338  EmitImmediate(Op, MI.getLoc(), X86II::getSizeOfImm(TSFlags),
1340  Fixups);
1341  break;
1342  }
1343  case X86II::RawFrmMemOffs:
1344  // Emit segment override opcode prefix as needed.
1345  EmitSegmentOverridePrefix(CurByte, 1, MI, OS);
1346  EmitByte(BaseOpcode, CurByte, OS);
1347  EmitImmediate(MI.getOperand(CurOp++), MI.getLoc(),
1348  X86II::getSizeOfImm(TSFlags), getImmFixupKind(TSFlags),
1349  CurByte, OS, Fixups);
1350  ++CurOp; // skip segment operand
1351  break;
1352  case X86II::RawFrmImm8:
1353  EmitByte(BaseOpcode, CurByte, OS);
1354  EmitImmediate(MI.getOperand(CurOp++), MI.getLoc(),
1355  X86II::getSizeOfImm(TSFlags), getImmFixupKind(TSFlags),
1356  CurByte, OS, Fixups);
1357  EmitImmediate(MI.getOperand(CurOp++), MI.getLoc(), 1, FK_Data_1, CurByte,
1358  OS, Fixups);
1359  break;
1360  case X86II::RawFrmImm16:
1361  EmitByte(BaseOpcode, CurByte, OS);
1362  EmitImmediate(MI.getOperand(CurOp++), MI.getLoc(),
1363  X86II::getSizeOfImm(TSFlags), getImmFixupKind(TSFlags),
1364  CurByte, OS, Fixups);
1365  EmitImmediate(MI.getOperand(CurOp++), MI.getLoc(), 2, FK_Data_2, CurByte,
1366  OS, Fixups);
1367  break;
1368 
1369  case X86II::AddRegFrm:
1370  EmitByte(BaseOpcode + GetX86RegNum(MI.getOperand(CurOp++)), CurByte, OS);
1371  break;
1372 
1373  case X86II::MRMDestReg: {
1374  EmitByte(BaseOpcode, CurByte, OS);
1375  unsigned SrcRegNum = CurOp + 1;
1376 
1377  if (HasEVEX_K) // Skip writemask
1378  ++SrcRegNum;
1379 
1380  if (HasVEX_4V) // Skip 1st src (which is encoded in VEX_VVVV)
1381  ++SrcRegNum;
1382 
1383  EmitRegModRMByte(MI.getOperand(CurOp),
1384  GetX86RegNum(MI.getOperand(SrcRegNum)), CurByte, OS);
1385  CurOp = SrcRegNum + 1;
1386  break;
1387  }
1388  case X86II::MRMDestMem: {
1389  EmitByte(BaseOpcode, CurByte, OS);
1390  unsigned SrcRegNum = CurOp + X86::AddrNumOperands;
1391 
1392  if (HasEVEX_K) // Skip writemask
1393  ++SrcRegNum;
1394 
1395  if (HasVEX_4V) // Skip 1st src (which is encoded in VEX_VVVV)
1396  ++SrcRegNum;
1397 
1398  emitMemModRMByte(MI, CurOp, GetX86RegNum(MI.getOperand(SrcRegNum)), TSFlags,
1399  Rex, CurByte, OS, Fixups, STI);
1400  CurOp = SrcRegNum + 1;
1401  break;
1402  }
1403  case X86II::MRMSrcReg: {
1404  EmitByte(BaseOpcode, CurByte, OS);
1405  unsigned SrcRegNum = CurOp + 1;
1406 
1407  if (HasEVEX_K) // Skip writemask
1408  ++SrcRegNum;
1409 
1410  if (HasVEX_4V) // Skip 1st src (which is encoded in VEX_VVVV)
1411  ++SrcRegNum;
1412 
1413  EmitRegModRMByte(MI.getOperand(SrcRegNum),
1414  GetX86RegNum(MI.getOperand(CurOp)), CurByte, OS);
1415  CurOp = SrcRegNum + 1;
1416  if (HasVEX_I8Reg)
1417  I8RegNum = getX86RegEncoding(MI, CurOp++);
1418  // do not count the rounding control operand
1419  if (HasEVEX_RC)
1420  --NumOps;
1421  break;
1422  }
1423  case X86II::MRMSrcReg4VOp3: {
1424  EmitByte(BaseOpcode, CurByte, OS);
1425  unsigned SrcRegNum = CurOp + 1;
1426 
1427  EmitRegModRMByte(MI.getOperand(SrcRegNum),
1428  GetX86RegNum(MI.getOperand(CurOp)), CurByte, OS);
1429  CurOp = SrcRegNum + 1;
1430  ++CurOp; // Encoded in VEX.VVVV
1431  break;
1432  }
1433  case X86II::MRMSrcRegOp4: {
1434  EmitByte(BaseOpcode, CurByte, OS);
1435  unsigned SrcRegNum = CurOp + 1;
1436 
1437  // Skip 1st src (which is encoded in VEX_VVVV)
1438  ++SrcRegNum;
1439 
1440  // Capture 2nd src (which is encoded in Imm[7:4])
1441  assert(HasVEX_I8Reg && "MRMSrcRegOp4 should imply VEX_I8Reg");
1442  I8RegNum = getX86RegEncoding(MI, SrcRegNum++);
1443 
1444  EmitRegModRMByte(MI.getOperand(SrcRegNum),
1445  GetX86RegNum(MI.getOperand(CurOp)), CurByte, OS);
1446  CurOp = SrcRegNum + 1;
1447  break;
1448  }
1449  case X86II::MRMSrcRegCC: {
1450  unsigned FirstOp = CurOp++;
1451  unsigned SecondOp = CurOp++;
1452 
1453  unsigned CC = MI.getOperand(CurOp++).getImm();
1454  EmitByte(BaseOpcode + CC, CurByte, OS);
1455 
1456  EmitRegModRMByte(MI.getOperand(SecondOp),
1457  GetX86RegNum(MI.getOperand(FirstOp)), CurByte, OS);
1458  break;
1459  }
1460  case X86II::MRMSrcMem: {
1461  unsigned FirstMemOp = CurOp+1;
1462 
1463  if (HasEVEX_K) // Skip writemask
1464  ++FirstMemOp;
1465 
1466  if (HasVEX_4V)
1467  ++FirstMemOp; // Skip the register source (which is encoded in VEX_VVVV).
1468 
1469  EmitByte(BaseOpcode, CurByte, OS);
1470 
1471  emitMemModRMByte(MI, FirstMemOp, GetX86RegNum(MI.getOperand(CurOp)),
1472  TSFlags, Rex, CurByte, OS, Fixups, STI);
1473  CurOp = FirstMemOp + X86::AddrNumOperands;
1474  if (HasVEX_I8Reg)
1475  I8RegNum = getX86RegEncoding(MI, CurOp++);
1476  break;
1477  }
1478  case X86II::MRMSrcMem4VOp3: {
1479  unsigned FirstMemOp = CurOp+1;
1480 
1481  EmitByte(BaseOpcode, CurByte, OS);
1482 
1483  emitMemModRMByte(MI, FirstMemOp, GetX86RegNum(MI.getOperand(CurOp)),
1484  TSFlags, Rex, CurByte, OS, Fixups, STI);
1485  CurOp = FirstMemOp + X86::AddrNumOperands;
1486  ++CurOp; // Encoded in VEX.VVVV.
1487  break;
1488  }
1489  case X86II::MRMSrcMemOp4: {
1490  unsigned FirstMemOp = CurOp+1;
1491 
1492  ++FirstMemOp; // Skip the register source (which is encoded in VEX_VVVV).
1493 
1494  // Capture second register source (encoded in Imm[7:4])
1495  assert(HasVEX_I8Reg && "MRMSrcRegOp4 should imply VEX_I8Reg");
1496  I8RegNum = getX86RegEncoding(MI, FirstMemOp++);
1497 
1498  EmitByte(BaseOpcode, CurByte, OS);
1499 
1500  emitMemModRMByte(MI, FirstMemOp, GetX86RegNum(MI.getOperand(CurOp)),
1501  TSFlags, Rex, CurByte, OS, Fixups, STI);
1502  CurOp = FirstMemOp + X86::AddrNumOperands;
1503  break;
1504  }
1505  case X86II::MRMSrcMemCC: {
1506  unsigned RegOp = CurOp++;
1507  unsigned FirstMemOp = CurOp;
1508  CurOp = FirstMemOp + X86::AddrNumOperands;
1509 
1510  unsigned CC = MI.getOperand(CurOp++).getImm();
1511  EmitByte(BaseOpcode + CC, CurByte, OS);
1512 
1513  emitMemModRMByte(MI, FirstMemOp, GetX86RegNum(MI.getOperand(RegOp)),
1514  TSFlags, Rex, CurByte, OS, Fixups, STI);
1515  break;
1516  }
1517 
1518  case X86II::MRMXrCC: {
1519  unsigned RegOp = CurOp++;
1520 
1521  unsigned CC = MI.getOperand(CurOp++).getImm();
1522  EmitByte(BaseOpcode + CC, CurByte, OS);
1523  EmitRegModRMByte(MI.getOperand(RegOp), 0, CurByte, OS);
1524  break;
1525  }
1526 
1527  case X86II::MRMXr:
1528  case X86II::MRM0r: case X86II::MRM1r:
1529  case X86II::MRM2r: case X86II::MRM3r:
1530  case X86II::MRM4r: case X86II::MRM5r:
1531  case X86II::MRM6r: case X86II::MRM7r:
1532  if (HasVEX_4V) // Skip the register dst (which is encoded in VEX_VVVV).
1533  ++CurOp;
1534  if (HasEVEX_K) // Skip writemask
1535  ++CurOp;
1536  EmitByte(BaseOpcode, CurByte, OS);
1537  EmitRegModRMByte(MI.getOperand(CurOp++),
1538  (Form == X86II::MRMXr) ? 0 : Form-X86II::MRM0r,
1539  CurByte, OS);
1540  break;
1541 
1542  case X86II::MRMXmCC: {
1543  unsigned FirstMemOp = CurOp;
1544  CurOp = FirstMemOp + X86::AddrNumOperands;
1545 
1546  unsigned CC = MI.getOperand(CurOp++).getImm();
1547  EmitByte(BaseOpcode + CC, CurByte, OS);
1548 
1549  emitMemModRMByte(MI, FirstMemOp, 0, TSFlags, Rex, CurByte, OS, Fixups, STI);
1550  break;
1551  }
1552 
1553  case X86II::MRMXm:
1554  case X86II::MRM0m: case X86II::MRM1m:
1555  case X86II::MRM2m: case X86II::MRM3m:
1556  case X86II::MRM4m: case X86II::MRM5m:
1557  case X86II::MRM6m: case X86II::MRM7m:
1558  if (HasVEX_4V) // Skip the register dst (which is encoded in VEX_VVVV).
1559  ++CurOp;
1560  if (HasEVEX_K) // Skip writemask
1561  ++CurOp;
1562  EmitByte(BaseOpcode, CurByte, OS);
1563  emitMemModRMByte(MI, CurOp,
1564  (Form == X86II::MRMXm) ? 0 : Form - X86II::MRM0m, TSFlags,
1565  Rex, CurByte, OS, Fixups, STI);
1566  CurOp += X86::AddrNumOperands;
1567  break;
1568 
1569  case X86II::MRM_C0: case X86II::MRM_C1: case X86II::MRM_C2:
1570  case X86II::MRM_C3: case X86II::MRM_C4: case X86II::MRM_C5:
1571  case X86II::MRM_C6: case X86II::MRM_C7: case X86II::MRM_C8:
1572  case X86II::MRM_C9: case X86II::MRM_CA: case X86II::MRM_CB:
1573  case X86II::MRM_CC: case X86II::MRM_CD: case X86II::MRM_CE:
1574  case X86II::MRM_CF: case X86II::MRM_D0: case X86II::MRM_D1:
1575  case X86II::MRM_D2: case X86II::MRM_D3: case X86II::MRM_D4:
1576  case X86II::MRM_D5: case X86II::MRM_D6: case X86II::MRM_D7:
1577  case X86II::MRM_D8: case X86II::MRM_D9: case X86II::MRM_DA:
1578  case X86II::MRM_DB: case X86II::MRM_DC: case X86II::MRM_DD:
1579  case X86II::MRM_DE: case X86II::MRM_DF: case X86II::MRM_E0:
1580  case X86II::MRM_E1: case X86II::MRM_E2: case X86II::MRM_E3:
1581  case X86II::MRM_E4: case X86II::MRM_E5: case X86II::MRM_E6:
1582  case X86II::MRM_E7: case X86II::MRM_E8: case X86II::MRM_E9:
1583  case X86II::MRM_EA: case X86II::MRM_EB: case X86II::MRM_EC:
1584  case X86II::MRM_ED: case X86II::MRM_EE: case X86II::MRM_EF:
1585  case X86II::MRM_F0: case X86II::MRM_F1: case X86II::MRM_F2:
1586  case X86II::MRM_F3: case X86II::MRM_F4: case X86II::MRM_F5:
1587  case X86II::MRM_F6: case X86II::MRM_F7: case X86II::MRM_F8:
1588  case X86II::MRM_F9: case X86II::MRM_FA: case X86II::MRM_FB:
1589  case X86II::MRM_FC: case X86II::MRM_FD: case X86II::MRM_FE:
1590  case X86II::MRM_FF:
1591  EmitByte(BaseOpcode, CurByte, OS);
1592  EmitByte(0xC0 + Form - X86II::MRM_C0, CurByte, OS);
1593  break;
1594  }
1595 
1596  if (HasVEX_I8Reg) {
1597  // The last source register of a 4 operand instruction in AVX is encoded
1598  // in bits[7:4] of a immediate byte.
1599  assert(I8RegNum < 16 && "Register encoding out of range");
1600  I8RegNum <<= 4;
1601  if (CurOp != NumOps) {
1602  unsigned Val = MI.getOperand(CurOp++).getImm();
1603  assert(Val < 16 && "Immediate operand value out of range");
1604  I8RegNum |= Val;
1605  }
1606  EmitImmediate(MCOperand::createImm(I8RegNum), MI.getLoc(), 1, FK_Data_1,
1607  CurByte, OS, Fixups);
1608  } else {
1609  // If there is a remaining operand, it must be a trailing immediate. Emit it
1610  // according to the right size for the instruction. Some instructions
1611  // (SSE4a extrq and insertq) have two trailing immediates.
1612  while (CurOp != NumOps && NumOps - CurOp <= 2) {
1613  EmitImmediate(MI.getOperand(CurOp++), MI.getLoc(),
1614  X86II::getSizeOfImm(TSFlags), getImmFixupKind(TSFlags),
1615  CurByte, OS, Fixups);
1616  }
1617  }
1618 
1619  if ((TSFlags & X86II::OpMapMask) == X86II::ThreeDNow)
1620  EmitByte(X86II::getBaseOpcodeFor(TSFlags), CurByte, OS);
1621 
1622 #ifndef NDEBUG
1623  // FIXME: Verify.
1624  if (/*!Desc.isVariadic() &&*/ CurOp != NumOps) {
1625  errs() << "Cannot encode all operands of: ";
1626  MI.dump();
1627  errs() << '\n';
1628  abort();
1629  }
1630 #endif
1631 }
1632 
1634  const MCRegisterInfo &MRI,
1635  MCContext &Ctx) {
1636  return new X86MCCodeEmitter(MCII, Ctx);
1637 }
uint64_t CallInst * C
static bool HasSecRelSymbolRef(const MCExpr *Expr)
bool isImm() const
Definition: MCInst.h:58
Raw - This form is for instructions that don&#39;t have any operands, so they are just a fixed opcode val...
Definition: X86BaseInfo.h:291
static bool Is64BitMemOperand(const MCInst &MI, unsigned Op)
Is64BitMemOperand - Return true if the specified instruction has a 64-bit memory operand.
raw_ostream & errs()
This returns a reference to a raw_ostream for standard error.
bool isX86_64NonExtLowByteReg(unsigned reg)
Definition: X86BaseInfo.h:876
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:139
This class represents lattice values for constants.
Definition: AllocatorList.h:23
AddrNumOperands - Total number of operands in a memory reference.
Definition: X86BaseInfo.h:41
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:41
VariantKind getKind() const
Definition: MCExpr.h:336
static bool isDisp8(int Value)
isDisp8 - Return true if this signed displacement fits in a 8-bit sign-extended field.
Describe properties that are true of each instruction in the target description file.
Definition: MCInstrDesc.h:163
unsigned Reg
static GlobalOffsetTableExprKind StartsWithGlobalOffsetTable(const MCExpr *Expr)
bool isReg() const
Definition: MCInst.h:57
const MCExpr * getLHS() const
Get the left-hand side expression of the binary operator.
Definition: MCExpr.h:562
static MCFixupKind getKindForSize(unsigned Size, bool isPCRel)
Return the generic fixup kind for a value with the given size.
Definition: MCFixup.h:132
static Lanai::Fixups FixupKind(const MCExpr *Expr)
unsigned isImmPCRel(uint64_t TSFlags)
isImmPCRel - Return true if the immediate of the specified instruction&#39;s TSFlags indicates that it is...
Definition: X86BaseInfo.h:678
A one-byte pc relative fixup.
Definition: MCFixup.h:28
ThreeDNow - This indicates that the instruction uses the wacky 0x0F 0x0F prefix for 3DNow! instructio...
Definition: X86BaseInfo.h:508
MRMSrcReg4VOp3 - This form is used for instructions that encode operand 3 with VEX.VVVV and do not load from memory.
Definition: X86BaseInfo.h:387
unsigned getNumOperands() const
Return the number of declared MachineOperands for this MachineInstruction.
Definition: MCInstrDesc.h:210
const FeatureBitset & getFeatureBits() const
MCCodeEmitter * createX86MCCodeEmitter(const MCInstrInfo &MCII, const MCRegisterInfo &MRI, MCContext &Ctx)
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: APFloat.h:41
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:35
The access may reference the value stored in memory.
MRMXr - This form is used for instructions that use the Mod/RM byte to specify a register source...
Definition: X86BaseInfo.h:408
Represent a reference to a symbol from inside an expression.
Definition: MCExpr.h:165
A four-byte section relative fixup.
Definition: MCFixup.h:42
MRMSrcMem4VOp3 - This form is used for instructions that encode operand 3 with VEX.VVVV and load from memory.
Definition: X86BaseInfo.h:347
MRMSrcMem - This form is used for instructions that use the Mod/RM byte to specify a source...
Definition: X86BaseInfo.h:342
unsigned getReg() const
Returns the register number.
Definition: MCInst.h:64
A four-byte fixup.
Definition: MCFixup.h:26
Context object for machine code objects.
Definition: MCContext.h:62
MRMSrcMemCC - This form is used for instructions that use the Mod/RM byte to specify the operands and...
Definition: X86BaseInfo.h:357
MRMSrcRegCC - This form is used for instructions that use the Mod/RM byte to specify the operands and...
Definition: X86BaseInfo.h:397
uint8_t getBaseOpcodeFor(uint64_t TSFlags)
Definition: X86BaseInfo.h:651
const MCExpr * getRHS() const
Get the right-hand side expression of the binary operator.
Definition: MCExpr.h:565
const MCExpr * getExpr() const
Definition: MCInst.h:95
static const MCBinaryExpr * createAdd(const MCExpr *LHS, const MCExpr *RHS, MCContext &Ctx)
Definition: MCExpr.h:459
bool hasImm(uint64_t TSFlags)
Definition: X86BaseInfo.h:655
static bool isCDisp8(uint64_t TSFlags, int Value, int &CValue)
isCDisp8 - Return true if this signed displacement fits in a 8-bit compressed dispacement field...
MRMSrcReg - This form is used for instructions that use the Mod/RM byte to specify a source...
Definition: X86BaseInfo.h:382
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:158
MCRegisterInfo base class - We assume that the target defines a static array of MCRegisterDesc object...
int64_t getImm() const
Definition: MCInst.h:75
static MCFixupKind getImmFixupKind(uint64_t TSFlags)
getImmFixupKind - Return the appropriate fixup kind to use for an immediate in an instruction with th...
MRM_XX - A mod/rm byte of exactly 0xXX.
Definition: X86BaseInfo.h:415
unsigned const MachineRegisterInfo * MRI
XOP - Opcode prefix used by XOP instructions.
Definition: X86BaseInfo.h:590
unsigned getFlags() const
Definition: MCInst.h:174
AddRegFrm - This form is used for instructions like &#39;push r32&#39; that have their one register operand a...
Definition: X86BaseInfo.h:295
MCCodeEmitter - Generic instruction encoding interface.
Definition: MCCodeEmitter.h:21
Interface to description of machine instruction set.
Definition: MCInstrInfo.h:23
MCFixupKind
Extensible enumeration to represent the type of a fixup.
Definition: MCFixup.h:22
RawFrmDst - This form is for instructions that use the destination index register DI/EDI/RDI...
Definition: X86BaseInfo.h:307
bool isExpr() const
Definition: MCInst.h:60
unsigned getOperandBias(const MCInstrDesc &Desc)
getOperandBias - compute whether all of the def operands are repeated in the uses and therefore shoul...
Definition: X86BaseInfo.h:721
unsigned getNumOperands() const
Definition: MCInst.h:181
RawFrmMemOffs - This form is for instructions that store an absolute memory offset as an immediate wi...
Definition: X86BaseInfo.h:299
MRMXm - This form is used for instructions that use the Mod/RM byte to specify a memory source...
Definition: X86BaseInfo.h:363
unsigned isImmSigned(uint64_t TSFlags)
isImmSigned - Return true if the immediate of the specified instruction&#39;s TSFlags indicates that it i...
Definition: X86BaseInfo.h:697
Binary assembler expressions.
Definition: MCExpr.h:415
static MCFixup create(uint32_t Offset, const MCExpr *Value, MCFixupKind Kind, SMLoc Loc=SMLoc())
Definition: MCFixup.h:90
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
A one-byte fixup.
Definition: MCFixup.h:24
static bool Is32BitMemOperand(const MCInst &MI, unsigned Op)
Is32BitMemOperand - Return true if the specified instruction has a 32-bit memory operand.
A two-byte pc relative fixup.
Definition: MCFixup.h:29
RawFrmDstSrc - This form is for instructions that use the source index register SI/ESI/RSI with a pos...
Definition: X86BaseInfo.h:312
void dump() const
Definition: MCInst.cpp:94
A four-byte pc relative fixup.
Definition: MCFixup.h:30
const MCSymbol & getSymbol() const
Definition: MCExpr.h:334
ExprKind getKind() const
Definition: MCExpr.h:72
MRMXCCr - This form is used for instructions that use the Mod/RM byte to specify a register source...
Definition: X86BaseInfo.h:403
const MCOperand & getOperand(unsigned i) const
Definition: MCInst.h:179
The access may modify the value stored in memory.
SMLoc getLoc() const
Definition: MCInst.h:177
AddCCFrm - This form is used for Jcc that encode the condition code in the lower 4 bits of the opcode...
Definition: X86BaseInfo.h:327
MRMSrcRegOp4 - This form is used for instructions that use the Mod/RM byte to specify the fourth sour...
Definition: X86BaseInfo.h:392
uint16_t getEncodingValue(unsigned RegNo) const
Returns the encoding for RegNo.
RawFrmImm8 - This is used for the ENTER instruction, which has two immediates, the first of which is ...
Definition: X86BaseInfo.h:317
unsigned getSizeOfImm(uint64_t TSFlags)
getSizeOfImm - Decode the "size of immediate" field from the TSFlags field of the specified instructi...
Definition: X86BaseInfo.h:661
AddrSegmentReg - The operand # of the segment in the memory operand.
Definition: X86BaseInfo.h:38
Generic base class for all target subtargets.
A eight-byte fixup.
Definition: MCFixup.h:27
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:332
References to labels and assigned expressions.
Definition: MCExpr.h:40
uint32_t Size
Definition: Profile.cpp:46
MRMDestReg - This form is used for instructions that use the Mod/RM byte to specify a destination...
Definition: X86BaseInfo.h:377
RawFrmSrc - This form is for instructions that use the source index register SI/ESI/RSI with a possib...
Definition: X86BaseInfo.h:303
StringRef getName() const
getName - Get the symbol name.
Definition: MCSymbol.h:202
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
const MCRegisterInfo * getRegisterInfo() const
Definition: MCContext.h:294
LLVM Value Representation.
Definition: Value.h:72
#define LLVM_FALLTHROUGH
LLVM_FALLTHROUGH - Mark fallthrough cases in switch statements.
Definition: Compiler.h:250
Binary expressions.
Definition: MCExpr.h:38
std::underlying_type< E >::type Mask()
Get a bitmask with 1s in all places up to the high-order bit of E&#39;s largest value.
Definition: BitmaskEnum.h:80
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:45
GlobalOffsetTableExprKind
StartsWithGlobalOffsetTable - Check if this expression starts with GLOBAL_OFFSET_TABLE and if it is o...
IRTranslator LLVM IR MI
MRMXm - This form is used for instructions that use the Mod/RM byte to specify a memory source...
Definition: X86BaseInfo.h:368
static bool isPCRel(unsigned Kind)
Represents a location in source code.
Definition: SMLoc.h:23
unsigned getOpcode() const
Definition: MCInst.h:171
Instances of this class represent operands of the MCInst class.
Definition: MCInst.h:34
MRM[0-7][rm] - These forms are used to represent instructions that use a Mod/RM byte, and use the middle field to hold extended opcode information.
Definition: X86BaseInfo.h:337
RawFrmImm16 - This is used for CALL FAR instructions, which have two immediates, the first of which i...
Definition: X86BaseInfo.h:323
MRMSrcMemOp4 - This form is used for instructions that use the Mod/RM byte to specify the fourth sour...
Definition: X86BaseInfo.h:352
static MCOperand createImm(int64_t Val)
Definition: MCInst.h:122
A two-byte fixup.
Definition: MCFixup.h:25
static const MCConstantExpr * create(int64_t Value, MCContext &Ctx)
Definition: MCExpr.cpp:163
int getMemoryOperandNo(uint64_t TSFlags)
getMemoryOperandNo - The function returns the MCInst operand # for the first field of the memory oper...
Definition: X86BaseInfo.h:761