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 
13 #include "MCTargetDesc/X86BaseInfo.h"
14 #include "MCTargetDesc/X86FixupKinds.h"
15 #include "MCTargetDesc/X86MCTargetDesc.h"
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"
25 #include "llvm/MC/MCSubtargetInfo.h"
26 #include "llvm/MC/MCSymbol.h"
27 #include "llvm/Support/ErrorHandling.h"
28 #include "llvm/Support/raw_ostream.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   X86MCCodeEmitter(const X86MCCodeEmitter &) = delete;
47   X86MCCodeEmitter &operator=(const X86MCCodeEmitter &) = delete;
48   ~X86MCCodeEmitter() override = default;
49 
50   void emitPrefix(const MCInst &MI, raw_ostream &OS,
51                   const MCSubtargetInfo &STI) const override;
52 
53   void encodeInstruction(const MCInst &MI, raw_ostream &OS,
54                          SmallVectorImpl<MCFixup> &Fixups,
55                          const MCSubtargetInfo &STI) const override;
56 
57 private:
58   unsigned getX86RegNum(const MCOperand &MO) const;
59 
60   unsigned getX86RegEncoding(const MCInst &MI, unsigned OpNum) const;
61 
62   /// \param MI a single low-level machine instruction.
63   /// \param OpNum the operand #.
64   /// \returns true if the OpNumth operand of MI  require a bit to be set in
65   /// REX prefix.
66   bool isREXExtendedReg(const MCInst &MI, unsigned OpNum) const;
67 
68   void emitImmediate(const MCOperand &Disp, SMLoc Loc, unsigned ImmSize,
69                      MCFixupKind FixupKind, uint64_t StartByte, raw_ostream &OS,
70                      SmallVectorImpl<MCFixup> &Fixups, int ImmOffset = 0) const;
71 
72   void emitRegModRMByte(const MCOperand &ModRMReg, unsigned RegOpcodeFld,
73                         raw_ostream &OS) const;
74 
75   void emitSIBByte(unsigned SS, unsigned Index, unsigned Base,
76                    raw_ostream &OS) const;
77 
78   void emitMemModRMByte(const MCInst &MI, unsigned Op, unsigned RegOpcodeField,
79                         uint64_t TSFlags, bool HasREX, uint64_t StartByte,
80                         raw_ostream &OS, SmallVectorImpl<MCFixup> &Fixups,
81                         const MCSubtargetInfo &STI,
82                         bool ForceSIB = false) const;
83 
84   bool emitPrefixImpl(unsigned &CurOp, const MCInst &MI,
85                       const MCSubtargetInfo &STI, raw_ostream &OS) const;
86 
87   void emitVEXOpcodePrefix(int MemOperand, const MCInst &MI,
88                            raw_ostream &OS) const;
89 
90   void emitSegmentOverridePrefix(unsigned SegOperand, const MCInst &MI,
91                                  raw_ostream &OS) const;
92 
93   bool emitOpcodePrefix(int MemOperand, const MCInst &MI,
94                         const MCSubtargetInfo &STI, raw_ostream &OS) const;
95 
96   bool emitREXPrefix(int MemOperand, const MCInst &MI,
97                      const MCSubtargetInfo &STI, raw_ostream &OS) const;
98 };
99 
100 } // end anonymous namespace
101 
102 static uint8_t modRMByte(unsigned Mod, unsigned RegOpcode, unsigned RM) {
103   assert(Mod < 4 && RegOpcode < 8 && RM < 8 && "ModRM Fields out of range!");
104   return RM | (RegOpcode << 3) | (Mod << 6);
105 }
106 
107 static void emitByte(uint8_t C, raw_ostream &OS) { OS << static_cast<char>(C); }
108 
109 static void emitConstant(uint64_t Val, unsigned Size, raw_ostream &OS) {
110   // Output the constant in little endian byte order.
111   for (unsigned i = 0; i != Size; ++i) {
112     emitByte(Val & 255, OS);
113     Val >>= 8;
114   }
115 }
116 
117 /// Determine if this immediate can fit in a disp8 or a compressed disp8 for
118 /// EVEX instructions. \p will be set to the value to pass to the ImmOffset
119 /// parameter of emitImmediate.
120 static bool isDispOrCDisp8(uint64_t TSFlags, int Value, int &ImmOffset) {
121   bool HasEVEX = (TSFlags & X86II::EncodingMask) == X86II::EVEX;
122 
123   int CD8_Scale =
124       (TSFlags & X86II::CD8_Scale_Mask) >> X86II::CD8_Scale_Shift;
125   if (!HasEVEX || CD8_Scale == 0)
126     return isInt<8>(Value);
127 
128   assert(isPowerOf2_32(CD8_Scale) && "Unexpected CD8 scale!");
129   if (Value & (CD8_Scale - 1)) // Unaligned offset
130     return false;
131 
132   int CDisp8 = Value / CD8_Scale;
133   if (!isInt<8>(CDisp8))
134     return false;
135 
136   // ImmOffset will be added to Value in emitImmediate leaving just CDisp8.
137   ImmOffset = CDisp8 - Value;
138   return true;
139 }
140 
141 /// \returns the appropriate fixup kind to use for an immediate in an
142 /// instruction with the specified TSFlags.
143 static MCFixupKind getImmFixupKind(uint64_t TSFlags) {
144   unsigned Size = X86II::getSizeOfImm(TSFlags);
145   bool isPCRel = X86II::isImmPCRel(TSFlags);
146 
147   if (X86II::isImmSigned(TSFlags)) {
148     switch (Size) {
149     default:
150       llvm_unreachable("Unsupported signed fixup size!");
151     case 4:
152       return MCFixupKind(X86::reloc_signed_4byte);
153     }
154   }
155   return MCFixup::getKindForSize(Size, isPCRel);
156 }
157 
158 /// \param Op operand # of the memory operand.
159 ///
160 /// \returns true if the specified instruction has a 16-bit memory operand.
161 static bool is16BitMemOperand(const MCInst &MI, unsigned Op,
162                               const MCSubtargetInfo &STI) {
163   const MCOperand &Base = MI.getOperand(Op + X86::AddrBaseReg);
164   const MCOperand &Index = MI.getOperand(Op + X86::AddrIndexReg);
165 
166   unsigned BaseReg = Base.getReg();
167   unsigned IndexReg = Index.getReg();
168 
169   if (STI.hasFeature(X86::Mode16Bit) && BaseReg == 0 && IndexReg == 0)
170     return true;
171   if ((BaseReg != 0 &&
172        X86MCRegisterClasses[X86::GR16RegClassID].contains(BaseReg)) ||
173       (IndexReg != 0 &&
174        X86MCRegisterClasses[X86::GR16RegClassID].contains(IndexReg)))
175     return true;
176   return false;
177 }
178 
179 /// \param Op operand # of the memory operand.
180 ///
181 /// \returns true if the specified instruction has a 32-bit memory operand.
182 static bool is32BitMemOperand(const MCInst &MI, unsigned Op) {
183   const MCOperand &BaseReg = MI.getOperand(Op + X86::AddrBaseReg);
184   const MCOperand &IndexReg = MI.getOperand(Op + X86::AddrIndexReg);
185 
186   if ((BaseReg.getReg() != 0 &&
187        X86MCRegisterClasses[X86::GR32RegClassID].contains(BaseReg.getReg())) ||
188       (IndexReg.getReg() != 0 &&
189        X86MCRegisterClasses[X86::GR32RegClassID].contains(IndexReg.getReg())))
190     return true;
191   if (BaseReg.getReg() == X86::EIP) {
192     assert(IndexReg.getReg() == 0 && "Invalid eip-based address.");
193     return true;
194   }
195   if (IndexReg.getReg() == X86::EIZ)
196     return true;
197   return false;
198 }
199 
200 /// \param Op operand # of the memory operand.
201 ///
202 /// \returns true if the specified instruction has a 64-bit memory operand.
203 #ifndef NDEBUG
204 static bool is64BitMemOperand(const MCInst &MI, unsigned Op) {
205   const MCOperand &BaseReg = MI.getOperand(Op + X86::AddrBaseReg);
206   const MCOperand &IndexReg = MI.getOperand(Op + X86::AddrIndexReg);
207 
208   if ((BaseReg.getReg() != 0 &&
209        X86MCRegisterClasses[X86::GR64RegClassID].contains(BaseReg.getReg())) ||
210       (IndexReg.getReg() != 0 &&
211        X86MCRegisterClasses[X86::GR64RegClassID].contains(IndexReg.getReg())))
212     return true;
213   return false;
214 }
215 #endif
216 
217 enum GlobalOffsetTableExprKind { GOT_None, GOT_Normal, GOT_SymDiff };
218 
219 /// Check if this expression starts with  _GLOBAL_OFFSET_TABLE_ and if it is
220 /// of the form _GLOBAL_OFFSET_TABLE_-symbol. This is needed to support PIC on
221 /// ELF i386 as _GLOBAL_OFFSET_TABLE_ is magical. We check only simple case that
222 /// are know to be used: _GLOBAL_OFFSET_TABLE_ by itself or at the start of a
223 /// binary expression.
224 static GlobalOffsetTableExprKind
225 startsWithGlobalOffsetTable(const MCExpr *Expr) {
226   const MCExpr *RHS = nullptr;
227   if (Expr->getKind() == MCExpr::Binary) {
228     const MCBinaryExpr *BE = static_cast<const MCBinaryExpr *>(Expr);
229     Expr = BE->getLHS();
230     RHS = BE->getRHS();
231   }
232 
233   if (Expr->getKind() != MCExpr::SymbolRef)
234     return GOT_None;
235 
236   const MCSymbolRefExpr *Ref = static_cast<const MCSymbolRefExpr *>(Expr);
237   const MCSymbol &S = Ref->getSymbol();
238   if (S.getName() != "_GLOBAL_OFFSET_TABLE_")
239     return GOT_None;
240   if (RHS && RHS->getKind() == MCExpr::SymbolRef)
241     return GOT_SymDiff;
242   return GOT_Normal;
243 }
244 
245 static bool hasSecRelSymbolRef(const MCExpr *Expr) {
246   if (Expr->getKind() == MCExpr::SymbolRef) {
247     const MCSymbolRefExpr *Ref = static_cast<const MCSymbolRefExpr *>(Expr);
248     return Ref->getKind() == MCSymbolRefExpr::VK_SECREL;
249   }
250   return false;
251 }
252 
253 static bool isPCRel32Branch(const MCInst &MI, const MCInstrInfo &MCII) {
254   unsigned Opcode = MI.getOpcode();
255   const MCInstrDesc &Desc = MCII.get(Opcode);
256   if ((Opcode != X86::CALL64pcrel32 && Opcode != X86::JMP_4 &&
257        Opcode != X86::JCC_4) ||
258       getImmFixupKind(Desc.TSFlags) != FK_PCRel_4)
259     return false;
260 
261   unsigned CurOp = X86II::getOperandBias(Desc);
262   const MCOperand &Op = MI.getOperand(CurOp);
263   if (!Op.isExpr())
264     return false;
265 
266   const MCSymbolRefExpr *Ref = dyn_cast<MCSymbolRefExpr>(Op.getExpr());
267   return Ref && Ref->getKind() == MCSymbolRefExpr::VK_None;
268 }
269 
270 unsigned X86MCCodeEmitter::getX86RegNum(const MCOperand &MO) const {
271   return Ctx.getRegisterInfo()->getEncodingValue(MO.getReg()) & 0x7;
272 }
273 
274 unsigned X86MCCodeEmitter::getX86RegEncoding(const MCInst &MI,
275                                              unsigned OpNum) const {
276   return Ctx.getRegisterInfo()->getEncodingValue(MI.getOperand(OpNum).getReg());
277 }
278 
279 /// \param MI a single low-level machine instruction.
280 /// \param OpNum the operand #.
281 /// \returns true if the OpNumth operand of MI  require a bit to be set in
282 /// REX prefix.
283 bool X86MCCodeEmitter::isREXExtendedReg(const MCInst &MI,
284                                         unsigned OpNum) const {
285   return (getX86RegEncoding(MI, OpNum) >> 3) & 1;
286 }
287 
288 void X86MCCodeEmitter::emitImmediate(const MCOperand &DispOp, SMLoc Loc,
289                                      unsigned Size, MCFixupKind FixupKind,
290                                      uint64_t StartByte, raw_ostream &OS,
291                                      SmallVectorImpl<MCFixup> &Fixups,
292                                      int ImmOffset) const {
293   const MCExpr *Expr = nullptr;
294   if (DispOp.isImm()) {
295     // If this is a simple integer displacement that doesn't require a
296     // relocation, emit it now.
297     if (FixupKind != FK_PCRel_1 && FixupKind != FK_PCRel_2 &&
298         FixupKind != FK_PCRel_4) {
299       emitConstant(DispOp.getImm() + ImmOffset, Size, OS);
300       return;
301     }
302     Expr = MCConstantExpr::create(DispOp.getImm(), Ctx);
303   } else {
304     Expr = DispOp.getExpr();
305   }
306 
307   // If we have an immoffset, add it to the expression.
308   if ((FixupKind == FK_Data_4 || FixupKind == FK_Data_8 ||
309        FixupKind == MCFixupKind(X86::reloc_signed_4byte))) {
310     GlobalOffsetTableExprKind Kind = startsWithGlobalOffsetTable(Expr);
311     if (Kind != GOT_None) {
312       assert(ImmOffset == 0);
313 
314       if (Size == 8) {
315         FixupKind = MCFixupKind(X86::reloc_global_offset_table8);
316       } else {
317         assert(Size == 4);
318         FixupKind = MCFixupKind(X86::reloc_global_offset_table);
319       }
320 
321       if (Kind == GOT_Normal)
322         ImmOffset = static_cast<int>(OS.tell() - StartByte);
323     } else if (Expr->getKind() == MCExpr::SymbolRef) {
324       if (hasSecRelSymbolRef(Expr)) {
325         FixupKind = MCFixupKind(FK_SecRel_4);
326       }
327     } else if (Expr->getKind() == MCExpr::Binary) {
328       const MCBinaryExpr *Bin = static_cast<const MCBinaryExpr *>(Expr);
329       if (hasSecRelSymbolRef(Bin->getLHS()) ||
330           hasSecRelSymbolRef(Bin->getRHS())) {
331         FixupKind = MCFixupKind(FK_SecRel_4);
332       }
333     }
334   }
335 
336   // If the fixup is pc-relative, we need to bias the value to be relative to
337   // the start of the field, not the end of the field.
338   if (FixupKind == FK_PCRel_4 ||
339       FixupKind == MCFixupKind(X86::reloc_riprel_4byte) ||
340       FixupKind == MCFixupKind(X86::reloc_riprel_4byte_movq_load) ||
341       FixupKind == MCFixupKind(X86::reloc_riprel_4byte_relax) ||
342       FixupKind == MCFixupKind(X86::reloc_riprel_4byte_relax_rex) ||
343       FixupKind == MCFixupKind(X86::reloc_branch_4byte_pcrel)) {
344     ImmOffset -= 4;
345     // If this is a pc-relative load off _GLOBAL_OFFSET_TABLE_:
346     // leaq _GLOBAL_OFFSET_TABLE_(%rip), %r15
347     // this needs to be a GOTPC32 relocation.
348     if (startsWithGlobalOffsetTable(Expr) != GOT_None)
349       FixupKind = MCFixupKind(X86::reloc_global_offset_table);
350   }
351   if (FixupKind == FK_PCRel_2)
352     ImmOffset -= 2;
353   if (FixupKind == FK_PCRel_1)
354     ImmOffset -= 1;
355 
356   if (ImmOffset)
357     Expr = MCBinaryExpr::createAdd(Expr, MCConstantExpr::create(ImmOffset, Ctx),
358                                    Ctx);
359 
360   // Emit a symbolic constant as a fixup and 4 zeros.
361   Fixups.push_back(MCFixup::create(static_cast<uint32_t>(OS.tell() - StartByte),
362                                    Expr, FixupKind, Loc));
363   emitConstant(0, Size, OS);
364 }
365 
366 void X86MCCodeEmitter::emitRegModRMByte(const MCOperand &ModRMReg,
367                                         unsigned RegOpcodeFld,
368                                         raw_ostream &OS) const {
369   emitByte(modRMByte(3, RegOpcodeFld, getX86RegNum(ModRMReg)), OS);
370 }
371 
372 void X86MCCodeEmitter::emitSIBByte(unsigned SS, unsigned Index, unsigned Base,
373                                    raw_ostream &OS) const {
374   // SIB byte is in the same format as the modRMByte.
375   emitByte(modRMByte(SS, Index, Base), OS);
376 }
377 
378 void X86MCCodeEmitter::emitMemModRMByte(const MCInst &MI, unsigned Op,
379                                         unsigned RegOpcodeField,
380                                         uint64_t TSFlags, bool HasREX,
381                                         uint64_t StartByte, raw_ostream &OS,
382                                         SmallVectorImpl<MCFixup> &Fixups,
383                                         const MCSubtargetInfo &STI,
384                                         bool ForceSIB) const {
385   const MCOperand &Disp = MI.getOperand(Op + X86::AddrDisp);
386   const MCOperand &Base = MI.getOperand(Op + X86::AddrBaseReg);
387   const MCOperand &Scale = MI.getOperand(Op + X86::AddrScaleAmt);
388   const MCOperand &IndexReg = MI.getOperand(Op + X86::AddrIndexReg);
389   unsigned BaseReg = Base.getReg();
390 
391   // Handle %rip relative addressing.
392   if (BaseReg == X86::RIP ||
393       BaseReg == X86::EIP) { // [disp32+rIP] in X86-64 mode
394     assert(STI.hasFeature(X86::Mode64Bit) &&
395            "Rip-relative addressing requires 64-bit mode");
396     assert(IndexReg.getReg() == 0 && !ForceSIB &&
397            "Invalid rip-relative address");
398     emitByte(modRMByte(0, RegOpcodeField, 5), OS);
399 
400     unsigned Opcode = MI.getOpcode();
401     unsigned FixupKind = [&]() {
402       // Enable relaxed relocation only for a MCSymbolRefExpr.  We cannot use a
403       // relaxed relocation if an offset is present (e.g. x@GOTPCREL+4).
404       if (!(Disp.isExpr() && isa<MCSymbolRefExpr>(Disp.getExpr())))
405         return X86::reloc_riprel_4byte;
406 
407       // Certain loads for GOT references can be relocated against the symbol
408       // directly if the symbol ends up in the same linkage unit.
409       switch (Opcode) {
410       default:
411         return X86::reloc_riprel_4byte;
412       case X86::MOV64rm:
413         // movq loads is a subset of reloc_riprel_4byte_relax_rex. It is a
414         // special case because COFF and Mach-O don't support ELF's more
415         // flexible R_X86_64_REX_GOTPCRELX relaxation.
416         assert(HasREX);
417         return X86::reloc_riprel_4byte_movq_load;
418       case X86::ADC32rm:
419       case X86::ADD32rm:
420       case X86::AND32rm:
421       case X86::CMP32rm:
422       case X86::MOV32rm:
423       case X86::OR32rm:
424       case X86::SBB32rm:
425       case X86::SUB32rm:
426       case X86::TEST32mr:
427       case X86::XOR32rm:
428       case X86::CALL64m:
429       case X86::JMP64m:
430       case X86::TAILJMPm64:
431       case X86::TEST64mr:
432       case X86::ADC64rm:
433       case X86::ADD64rm:
434       case X86::AND64rm:
435       case X86::CMP64rm:
436       case X86::OR64rm:
437       case X86::SBB64rm:
438       case X86::SUB64rm:
439       case X86::XOR64rm:
440         return HasREX ? X86::reloc_riprel_4byte_relax_rex
441                       : X86::reloc_riprel_4byte_relax;
442       }
443     }();
444 
445     // rip-relative addressing is actually relative to the *next* instruction.
446     // Since an immediate can follow the mod/rm byte for an instruction, this
447     // means that we need to bias the displacement field of the instruction with
448     // the size of the immediate field. If we have this case, add it into the
449     // expression to emit.
450     // Note: rip-relative addressing using immediate displacement values should
451     // not be adjusted, assuming it was the user's intent.
452     int ImmSize = !Disp.isImm() && X86II::hasImm(TSFlags)
453                       ? X86II::getSizeOfImm(TSFlags)
454                       : 0;
455 
456     emitImmediate(Disp, MI.getLoc(), 4, MCFixupKind(FixupKind), StartByte, OS,
457                   Fixups, -ImmSize);
458     return;
459   }
460 
461   unsigned BaseRegNo = BaseReg ? getX86RegNum(Base) : -1U;
462 
463   // 16-bit addressing forms of the ModR/M byte have a different encoding for
464   // the R/M field and are far more limited in which registers can be used.
465   if (is16BitMemOperand(MI, Op, STI)) {
466     if (BaseReg) {
467       // For 32-bit addressing, the row and column values in Table 2-2 are
468       // basically the same. It's AX/CX/DX/BX/SP/BP/SI/DI in that order, with
469       // some special cases. And getX86RegNum reflects that numbering.
470       // For 16-bit addressing it's more fun, as shown in the SDM Vol 2A,
471       // Table 2-1 "16-Bit Addressing Forms with the ModR/M byte". We can only
472       // use SI/DI/BP/BX, which have "row" values 4-7 in no particular order,
473       // while values 0-3 indicate the allowed combinations (base+index) of
474       // those: 0 for BX+SI, 1 for BX+DI, 2 for BP+SI, 3 for BP+DI.
475       //
476       // R16Table[] is a lookup from the normal RegNo, to the row values from
477       // Table 2-1 for 16-bit addressing modes. Where zero means disallowed.
478       static const unsigned R16Table[] = {0, 0, 0, 7, 0, 6, 4, 5};
479       unsigned RMfield = R16Table[BaseRegNo];
480 
481       assert(RMfield && "invalid 16-bit base register");
482 
483       if (IndexReg.getReg()) {
484         unsigned IndexReg16 = R16Table[getX86RegNum(IndexReg)];
485 
486         assert(IndexReg16 && "invalid 16-bit index register");
487         // We must have one of SI/DI (4,5), and one of BP/BX (6,7).
488         assert(((IndexReg16 ^ RMfield) & 2) &&
489                "invalid 16-bit base/index register combination");
490         assert(Scale.getImm() == 1 &&
491                "invalid scale for 16-bit memory reference");
492 
493         // Allow base/index to appear in either order (although GAS doesn't).
494         if (IndexReg16 & 2)
495           RMfield = (RMfield & 1) | ((7 - IndexReg16) << 1);
496         else
497           RMfield = (IndexReg16 & 1) | ((7 - RMfield) << 1);
498       }
499 
500       if (Disp.isImm() && isInt<8>(Disp.getImm())) {
501         if (Disp.getImm() == 0 && RMfield != 6) {
502           // There is no displacement; just the register.
503           emitByte(modRMByte(0, RegOpcodeField, RMfield), OS);
504           return;
505         }
506         // Use the [REG]+disp8 form, including for [BP] which cannot be encoded.
507         emitByte(modRMByte(1, RegOpcodeField, RMfield), OS);
508         emitImmediate(Disp, MI.getLoc(), 1, FK_Data_1, StartByte, OS, Fixups);
509         return;
510       }
511       // This is the [REG]+disp16 case.
512       emitByte(modRMByte(2, RegOpcodeField, RMfield), OS);
513     } else {
514       assert(IndexReg.getReg() == 0 && "Unexpected index register!");
515       // There is no BaseReg; this is the plain [disp16] case.
516       emitByte(modRMByte(0, RegOpcodeField, 6), OS);
517     }
518 
519     // Emit 16-bit displacement for plain disp16 or [REG]+disp16 cases.
520     emitImmediate(Disp, MI.getLoc(), 2, FK_Data_2, StartByte, OS, Fixups);
521     return;
522   }
523 
524   // Check for presence of {disp8} or {disp32} pseudo prefixes.
525   bool UseDisp8 = MI.getFlags() & X86::IP_USE_DISP8;
526   bool UseDisp32 = MI.getFlags() & X86::IP_USE_DISP32;
527 
528   // We only allow no displacement if no pseudo prefix is present.
529   bool AllowNoDisp = !UseDisp8 && !UseDisp32;
530   // Disp8 is allowed unless the {disp32} prefix is present.
531   bool AllowDisp8 = !UseDisp32;
532 
533   // Determine whether a SIB byte is needed.
534   if (// The SIB byte must be used if there is an index register or the
535       // encoding requires a SIB byte.
536       !ForceSIB && IndexReg.getReg() == 0 &&
537       // The SIB byte must be used if the base is ESP/RSP/R12, all of which
538       // encode to an R/M value of 4, which indicates that a SIB byte is
539       // present.
540       BaseRegNo != N86::ESP &&
541       // If there is no base register and we're in 64-bit mode, we need a SIB
542       // byte to emit an addr that is just 'disp32' (the non-RIP relative form).
543       (!STI.hasFeature(X86::Mode64Bit) || BaseReg != 0)) {
544 
545     if (BaseReg == 0) { // [disp32]     in X86-32 mode
546       emitByte(modRMByte(0, RegOpcodeField, 5), OS);
547       emitImmediate(Disp, MI.getLoc(), 4, FK_Data_4, StartByte, OS, Fixups);
548       return;
549     }
550 
551     // If the base is not EBP/ESP/R12/R13 and there is no displacement, use
552     // simple indirect register encoding, this handles addresses like [EAX].
553     // The encoding for [EBP] or[R13] with no displacement means [disp32] so we
554     // handle it by emitting a displacement of 0 later.
555     if (BaseRegNo != N86::EBP) {
556       if (Disp.isImm() && Disp.getImm() == 0 && AllowNoDisp) {
557         emitByte(modRMByte(0, RegOpcodeField, BaseRegNo), OS);
558         return;
559       }
560 
561       // If the displacement is @tlscall, treat it as a zero.
562       if (Disp.isExpr()) {
563         auto *Sym = dyn_cast<MCSymbolRefExpr>(Disp.getExpr());
564         if (Sym && Sym->getKind() == MCSymbolRefExpr::VK_TLSCALL) {
565           // This is exclusively used by call *a@tlscall(base). The relocation
566           // (R_386_TLSCALL or R_X86_64_TLSCALL) applies to the beginning.
567           Fixups.push_back(MCFixup::create(0, Sym, FK_NONE, MI.getLoc()));
568           emitByte(modRMByte(0, RegOpcodeField, BaseRegNo), OS);
569           return;
570         }
571       }
572     }
573 
574     // Otherwise, if the displacement fits in a byte, encode as [REG+disp8].
575     // Including a compressed disp8 for EVEX instructions that support it.
576     // This also handles the 0 displacement for [EBP] or [R13]. We can't use
577     // disp8 if the {disp32} pseudo prefix is present.
578     if (Disp.isImm() && AllowDisp8) {
579       int ImmOffset = 0;
580       if (isDispOrCDisp8(TSFlags, Disp.getImm(), ImmOffset)) {
581         emitByte(modRMByte(1, RegOpcodeField, BaseRegNo), OS);
582         emitImmediate(Disp, MI.getLoc(), 1, FK_Data_1, StartByte, OS, Fixups,
583                       ImmOffset);
584         return;
585       }
586     }
587 
588     // Otherwise, emit the most general non-SIB encoding: [REG+disp32].
589     // Displacement may be 0 for [EBP] or [R13] case if {disp32} pseudo prefix
590     // prevented using disp8 above.
591     emitByte(modRMByte(2, RegOpcodeField, BaseRegNo), OS);
592     unsigned Opcode = MI.getOpcode();
593     unsigned FixupKind = Opcode == X86::MOV32rm ? X86::reloc_signed_4byte_relax
594                                                 : X86::reloc_signed_4byte;
595     emitImmediate(Disp, MI.getLoc(), 4, MCFixupKind(FixupKind), StartByte, OS,
596                   Fixups);
597     return;
598   }
599 
600   // We need a SIB byte, so start by outputting the ModR/M byte first
601   assert(IndexReg.getReg() != X86::ESP && IndexReg.getReg() != X86::RSP &&
602          "Cannot use ESP as index reg!");
603 
604   bool ForceDisp32 = false;
605   bool ForceDisp8 = false;
606   int ImmOffset = 0;
607   if (BaseReg == 0) {
608     // If there is no base register, we emit the special case SIB byte with
609     // MOD=0, BASE=5, to JUST get the index, scale, and displacement.
610     BaseRegNo = 5;
611     emitByte(modRMByte(0, RegOpcodeField, 4), OS);
612     ForceDisp32 = true;
613   } else if (Disp.isImm() && Disp.getImm() == 0 && AllowNoDisp &&
614              // Base reg can't be EBP/RBP/R13 as that would end up with '5' as
615              // the base field, but that is the magic [*] nomenclature that
616              // indicates no base when mod=0. For these cases we'll emit a 0
617              // displacement instead.
618              BaseRegNo != N86::EBP) {
619     // Emit no displacement ModR/M byte
620     emitByte(modRMByte(0, RegOpcodeField, 4), OS);
621   } else if (Disp.isImm() && AllowDisp8 &&
622              isDispOrCDisp8(TSFlags, Disp.getImm(), ImmOffset)) {
623     // Displacement fits in a byte or matches an EVEX compressed disp8, use
624     // disp8 encoding. This also handles EBP/R13 base with 0 displacement unless
625     // {disp32} pseudo prefix was used.
626     emitByte(modRMByte(1, RegOpcodeField, 4), OS);
627     ForceDisp8 = true;
628   } else {
629     // Otherwise, emit the normal disp32 encoding.
630     emitByte(modRMByte(2, RegOpcodeField, 4), OS);
631     ForceDisp32 = true;
632   }
633 
634   // Calculate what the SS field value should be...
635   static const unsigned SSTable[] = {~0U, 0, 1, ~0U, 2, ~0U, ~0U, ~0U, 3};
636   unsigned SS = SSTable[Scale.getImm()];
637 
638   unsigned IndexRegNo = IndexReg.getReg() ? getX86RegNum(IndexReg) : 4;
639 
640   emitSIBByte(SS, IndexRegNo, BaseRegNo, OS);
641 
642   // Do we need to output a displacement?
643   if (ForceDisp8)
644     emitImmediate(Disp, MI.getLoc(), 1, FK_Data_1, StartByte, OS, Fixups,
645                   ImmOffset);
646   else if (ForceDisp32)
647     emitImmediate(Disp, MI.getLoc(), 4, MCFixupKind(X86::reloc_signed_4byte),
648                   StartByte, OS, Fixups);
649 }
650 
651 /// Emit all instruction prefixes.
652 ///
653 /// \returns true if REX prefix is used, otherwise returns false.
654 bool X86MCCodeEmitter::emitPrefixImpl(unsigned &CurOp, const MCInst &MI,
655                                       const MCSubtargetInfo &STI,
656                                       raw_ostream &OS) const {
657   uint64_t TSFlags = MCII.get(MI.getOpcode()).TSFlags;
658   // Determine where the memory operand starts, if present.
659   int MemoryOperand = X86II::getMemoryOperandNo(TSFlags);
660   // Emit segment override opcode prefix as needed.
661   if (MemoryOperand != -1) {
662     MemoryOperand += CurOp;
663     emitSegmentOverridePrefix(MemoryOperand + X86::AddrSegmentReg, MI, OS);
664   }
665 
666   // Emit the repeat opcode prefix as needed.
667   unsigned Flags = MI.getFlags();
668   if (TSFlags & X86II::REP || Flags & X86::IP_HAS_REPEAT)
669     emitByte(0xF3, OS);
670   if (Flags & X86::IP_HAS_REPEAT_NE)
671     emitByte(0xF2, OS);
672 
673   // Emit the address size opcode prefix as needed.
674   bool NeedAddressOverride;
675   uint64_t AdSize = TSFlags & X86II::AdSizeMask;
676   if ((STI.hasFeature(X86::Mode16Bit) && AdSize == X86II::AdSize32) ||
677       (STI.hasFeature(X86::Mode32Bit) && AdSize == X86II::AdSize16) ||
678       (STI.hasFeature(X86::Mode64Bit) && AdSize == X86II::AdSize32)) {
679     NeedAddressOverride = true;
680   } else if (MemoryOperand < 0) {
681     NeedAddressOverride = false;
682   } else if (STI.hasFeature(X86::Mode64Bit)) {
683     assert(!is16BitMemOperand(MI, MemoryOperand, STI));
684     NeedAddressOverride = is32BitMemOperand(MI, MemoryOperand);
685   } else if (STI.hasFeature(X86::Mode32Bit)) {
686     assert(!is64BitMemOperand(MI, MemoryOperand));
687     NeedAddressOverride = is16BitMemOperand(MI, MemoryOperand, STI);
688   } else {
689     assert(STI.hasFeature(X86::Mode16Bit));
690     assert(!is64BitMemOperand(MI, MemoryOperand));
691     NeedAddressOverride = !is16BitMemOperand(MI, MemoryOperand, STI);
692   }
693 
694   if (NeedAddressOverride)
695     emitByte(0x67, OS);
696 
697   // Encoding type for this instruction.
698   uint64_t Encoding = TSFlags & X86II::EncodingMask;
699   bool HasREX = false;
700   if (Encoding)
701     emitVEXOpcodePrefix(MemoryOperand, MI, OS);
702   else
703     HasREX = emitOpcodePrefix(MemoryOperand, MI, STI, OS);
704 
705   uint64_t Form = TSFlags & X86II::FormMask;
706   switch (Form) {
707   default:
708     break;
709   case X86II::RawFrmDstSrc: {
710     unsigned siReg = MI.getOperand(1).getReg();
711     assert(((siReg == X86::SI && MI.getOperand(0).getReg() == X86::DI) ||
712             (siReg == X86::ESI && MI.getOperand(0).getReg() == X86::EDI) ||
713             (siReg == X86::RSI && MI.getOperand(0).getReg() == X86::RDI)) &&
714            "SI and DI register sizes do not match");
715     // Emit segment override opcode prefix as needed (not for %ds).
716     if (MI.getOperand(2).getReg() != X86::DS)
717       emitSegmentOverridePrefix(2, MI, OS);
718     // Emit AdSize prefix as needed.
719     if ((!STI.hasFeature(X86::Mode32Bit) && siReg == X86::ESI) ||
720         (STI.hasFeature(X86::Mode32Bit) && siReg == X86::SI))
721       emitByte(0x67, OS);
722     CurOp += 3; // Consume operands.
723     break;
724   }
725   case X86II::RawFrmSrc: {
726     unsigned siReg = MI.getOperand(0).getReg();
727     // Emit segment override opcode prefix as needed (not for %ds).
728     if (MI.getOperand(1).getReg() != X86::DS)
729       emitSegmentOverridePrefix(1, MI, OS);
730     // Emit AdSize prefix as needed.
731     if ((!STI.hasFeature(X86::Mode32Bit) && siReg == X86::ESI) ||
732         (STI.hasFeature(X86::Mode32Bit) && siReg == X86::SI))
733       emitByte(0x67, OS);
734     CurOp += 2; // Consume operands.
735     break;
736   }
737   case X86II::RawFrmDst: {
738     unsigned siReg = MI.getOperand(0).getReg();
739     // Emit AdSize prefix as needed.
740     if ((!STI.hasFeature(X86::Mode32Bit) && siReg == X86::EDI) ||
741         (STI.hasFeature(X86::Mode32Bit) && siReg == X86::DI))
742       emitByte(0x67, OS);
743     ++CurOp; // Consume operand.
744     break;
745   }
746   case X86II::RawFrmMemOffs: {
747     // Emit segment override opcode prefix as needed.
748     emitSegmentOverridePrefix(1, MI, OS);
749     break;
750   }
751   }
752 
753   return HasREX;
754 }
755 
756 /// AVX instructions are encoded using a opcode prefix called VEX.
757 void X86MCCodeEmitter::emitVEXOpcodePrefix(int MemOperand, const MCInst &MI,
758                                            raw_ostream &OS) const {
759   const MCInstrDesc &Desc = MCII.get(MI.getOpcode());
760   uint64_t TSFlags = Desc.TSFlags;
761 
762   assert(!(TSFlags & X86II::LOCK) && "Can't have LOCK VEX.");
763 
764   uint64_t Encoding = TSFlags & X86II::EncodingMask;
765   bool HasEVEX_K = TSFlags & X86II::EVEX_K;
766   bool HasVEX_4V = TSFlags & X86II::VEX_4V;
767   bool HasEVEX_RC = TSFlags & X86II::EVEX_RC;
768 
769   // VEX_R: opcode externsion equivalent to REX.R in
770   // 1's complement (inverted) form
771   //
772   //  1: Same as REX_R=0 (must be 1 in 32-bit mode)
773   //  0: Same as REX_R=1 (64 bit mode only)
774   //
775   uint8_t VEX_R = 0x1;
776   uint8_t EVEX_R2 = 0x1;
777 
778   // VEX_X: equivalent to REX.X, only used when a
779   // register is used for index in SIB Byte.
780   //
781   //  1: Same as REX.X=0 (must be 1 in 32-bit mode)
782   //  0: Same as REX.X=1 (64-bit mode only)
783   uint8_t VEX_X = 0x1;
784 
785   // VEX_B:
786   //
787   //  1: Same as REX_B=0 (ignored in 32-bit mode)
788   //  0: Same as REX_B=1 (64 bit mode only)
789   //
790   uint8_t VEX_B = 0x1;
791 
792   // VEX_W: opcode specific (use like REX.W, or used for
793   // opcode extension, or ignored, depending on the opcode byte)
794   uint8_t VEX_W = (TSFlags & X86II::VEX_W) ? 1 : 0;
795 
796   // VEX_5M (VEX m-mmmmm field):
797   //
798   //  0b00000: Reserved for future use
799   //  0b00001: implied 0F leading opcode
800   //  0b00010: implied 0F 38 leading opcode bytes
801   //  0b00011: implied 0F 3A leading opcode bytes
802   //  0b00100-0b11111: Reserved for future use
803   //  0b01000: XOP map select - 08h instructions with imm byte
804   //  0b01001: XOP map select - 09h instructions with no imm byte
805   //  0b01010: XOP map select - 0Ah instructions with imm dword
806   uint8_t VEX_5M;
807   switch (TSFlags & X86II::OpMapMask) {
808   default:
809     llvm_unreachable("Invalid prefix!");
810   case X86II::TB:
811     VEX_5M = 0x1;
812     break; // 0F
813   case X86II::T8:
814     VEX_5M = 0x2;
815     break; // 0F 38
816   case X86II::TA:
817     VEX_5M = 0x3;
818     break; // 0F 3A
819   case X86II::XOP8:
820     VEX_5M = 0x8;
821     break;
822   case X86II::XOP9:
823     VEX_5M = 0x9;
824     break;
825   case X86II::XOPA:
826     VEX_5M = 0xA;
827     break;
828   }
829 
830   // VEX_4V (VEX vvvv field): a register specifier
831   // (in 1's complement form) or 1111 if unused.
832   uint8_t VEX_4V = 0xf;
833   uint8_t EVEX_V2 = 0x1;
834 
835   // EVEX_L2/VEX_L (Vector Length):
836   //
837   // L2 L
838   //  0 0: scalar or 128-bit vector
839   //  0 1: 256-bit vector
840   //  1 0: 512-bit vector
841   //
842   uint8_t VEX_L = (TSFlags & X86II::VEX_L) ? 1 : 0;
843   uint8_t EVEX_L2 = (TSFlags & X86II::EVEX_L2) ? 1 : 0;
844 
845   // VEX_PP: opcode extension providing equivalent
846   // functionality of a SIMD prefix
847   //
848   //  0b00: None
849   //  0b01: 66
850   //  0b10: F3
851   //  0b11: F2
852   //
853   uint8_t VEX_PP = 0;
854   switch (TSFlags & X86II::OpPrefixMask) {
855   case X86II::PD:
856     VEX_PP = 0x1;
857     break; // 66
858   case X86II::XS:
859     VEX_PP = 0x2;
860     break; // F3
861   case X86II::XD:
862     VEX_PP = 0x3;
863     break; // F2
864   }
865 
866   // EVEX_U
867   uint8_t EVEX_U = 1; // Always '1' so far
868 
869   // EVEX_z
870   uint8_t EVEX_z = (HasEVEX_K && (TSFlags & X86II::EVEX_Z)) ? 1 : 0;
871 
872   // EVEX_b
873   uint8_t EVEX_b = (TSFlags & X86II::EVEX_B) ? 1 : 0;
874 
875   // EVEX_rc
876   uint8_t EVEX_rc = 0;
877 
878   // EVEX_aaa
879   uint8_t EVEX_aaa = 0;
880 
881   bool EncodeRC = false;
882 
883   // Classify VEX_B, VEX_4V, VEX_R, VEX_X
884   unsigned NumOps = Desc.getNumOperands();
885   unsigned CurOp = X86II::getOperandBias(Desc);
886 
887   switch (TSFlags & X86II::FormMask) {
888   default:
889     llvm_unreachable("Unexpected form in emitVEXOpcodePrefix!");
890   case X86II::MRM_C0:
891   case X86II::RawFrm:
892   case X86II::PrefixByte:
893     break;
894   case X86II::MRMDestMemFSIB:
895   case X86II::MRMDestMem: {
896     // MRMDestMem instructions forms:
897     //  MemAddr, src1(ModR/M)
898     //  MemAddr, src1(VEX_4V), src2(ModR/M)
899     //  MemAddr, src1(ModR/M), imm8
900     //
901     unsigned BaseRegEnc = getX86RegEncoding(MI, MemOperand + X86::AddrBaseReg);
902     VEX_B = ~(BaseRegEnc >> 3) & 1;
903     unsigned IndexRegEnc =
904         getX86RegEncoding(MI, MemOperand + X86::AddrIndexReg);
905     VEX_X = ~(IndexRegEnc >> 3) & 1;
906     if (!HasVEX_4V) // Only needed with VSIB which don't use VVVV.
907       EVEX_V2 = ~(IndexRegEnc >> 4) & 1;
908 
909     CurOp += X86::AddrNumOperands;
910 
911     if (HasEVEX_K)
912       EVEX_aaa = getX86RegEncoding(MI, CurOp++);
913 
914     if (HasVEX_4V) {
915       unsigned VRegEnc = getX86RegEncoding(MI, CurOp++);
916       VEX_4V = ~VRegEnc & 0xf;
917       EVEX_V2 = ~(VRegEnc >> 4) & 1;
918     }
919 
920     unsigned RegEnc = getX86RegEncoding(MI, CurOp++);
921     VEX_R = ~(RegEnc >> 3) & 1;
922     EVEX_R2 = ~(RegEnc >> 4) & 1;
923     break;
924   }
925   case X86II::MRMSrcMemFSIB:
926   case X86II::MRMSrcMem: {
927     // MRMSrcMem instructions forms:
928     //  src1(ModR/M), MemAddr
929     //  src1(ModR/M), src2(VEX_4V), MemAddr
930     //  src1(ModR/M), MemAddr, imm8
931     //  src1(ModR/M), MemAddr, src2(Imm[7:4])
932     //
933     //  FMA4:
934     //  dst(ModR/M.reg), src1(VEX_4V), src2(ModR/M), src3(Imm[7:4])
935     unsigned RegEnc = getX86RegEncoding(MI, CurOp++);
936     VEX_R = ~(RegEnc >> 3) & 1;
937     EVEX_R2 = ~(RegEnc >> 4) & 1;
938 
939     if (HasEVEX_K)
940       EVEX_aaa = getX86RegEncoding(MI, CurOp++);
941 
942     if (HasVEX_4V) {
943       unsigned VRegEnc = getX86RegEncoding(MI, CurOp++);
944       VEX_4V = ~VRegEnc & 0xf;
945       EVEX_V2 = ~(VRegEnc >> 4) & 1;
946     }
947 
948     unsigned BaseRegEnc = getX86RegEncoding(MI, MemOperand + X86::AddrBaseReg);
949     VEX_B = ~(BaseRegEnc >> 3) & 1;
950     unsigned IndexRegEnc =
951         getX86RegEncoding(MI, MemOperand + X86::AddrIndexReg);
952     VEX_X = ~(IndexRegEnc >> 3) & 1;
953     if (!HasVEX_4V) // Only needed with VSIB which don't use VVVV.
954       EVEX_V2 = ~(IndexRegEnc >> 4) & 1;
955 
956     break;
957   }
958   case X86II::MRMSrcMem4VOp3: {
959     // Instruction format for 4VOp3:
960     //   src1(ModR/M), MemAddr, src3(VEX_4V)
961     unsigned RegEnc = getX86RegEncoding(MI, CurOp++);
962     VEX_R = ~(RegEnc >> 3) & 1;
963 
964     unsigned BaseRegEnc = getX86RegEncoding(MI, MemOperand + X86::AddrBaseReg);
965     VEX_B = ~(BaseRegEnc >> 3) & 1;
966     unsigned IndexRegEnc =
967         getX86RegEncoding(MI, MemOperand + X86::AddrIndexReg);
968     VEX_X = ~(IndexRegEnc >> 3) & 1;
969 
970     VEX_4V = ~getX86RegEncoding(MI, CurOp + X86::AddrNumOperands) & 0xf;
971     break;
972   }
973   case X86II::MRMSrcMemOp4: {
974     //  dst(ModR/M.reg), src1(VEX_4V), src2(Imm[7:4]), src3(ModR/M),
975     unsigned RegEnc = getX86RegEncoding(MI, CurOp++);
976     VEX_R = ~(RegEnc >> 3) & 1;
977 
978     unsigned VRegEnc = getX86RegEncoding(MI, CurOp++);
979     VEX_4V = ~VRegEnc & 0xf;
980 
981     unsigned BaseRegEnc = getX86RegEncoding(MI, MemOperand + X86::AddrBaseReg);
982     VEX_B = ~(BaseRegEnc >> 3) & 1;
983     unsigned IndexRegEnc =
984         getX86RegEncoding(MI, MemOperand + X86::AddrIndexReg);
985     VEX_X = ~(IndexRegEnc >> 3) & 1;
986     break;
987   }
988   case X86II::MRM0m:
989   case X86II::MRM1m:
990   case X86II::MRM2m:
991   case X86II::MRM3m:
992   case X86II::MRM4m:
993   case X86II::MRM5m:
994   case X86II::MRM6m:
995   case X86II::MRM7m: {
996     // MRM[0-9]m instructions forms:
997     //  MemAddr
998     //  src1(VEX_4V), MemAddr
999     if (HasVEX_4V) {
1000       unsigned VRegEnc = getX86RegEncoding(MI, CurOp++);
1001       VEX_4V = ~VRegEnc & 0xf;
1002       EVEX_V2 = ~(VRegEnc >> 4) & 1;
1003     }
1004 
1005     if (HasEVEX_K)
1006       EVEX_aaa = getX86RegEncoding(MI, CurOp++);
1007 
1008     unsigned BaseRegEnc = getX86RegEncoding(MI, MemOperand + X86::AddrBaseReg);
1009     VEX_B = ~(BaseRegEnc >> 3) & 1;
1010     unsigned IndexRegEnc =
1011         getX86RegEncoding(MI, MemOperand + X86::AddrIndexReg);
1012     VEX_X = ~(IndexRegEnc >> 3) & 1;
1013     if (!HasVEX_4V) // Only needed with VSIB which don't use VVVV.
1014       EVEX_V2 = ~(IndexRegEnc >> 4) & 1;
1015 
1016     break;
1017   }
1018   case X86II::MRMSrcReg: {
1019     // MRMSrcReg instructions forms:
1020     //  dst(ModR/M), src1(VEX_4V), src2(ModR/M), src3(Imm[7:4])
1021     //  dst(ModR/M), src1(ModR/M)
1022     //  dst(ModR/M), src1(ModR/M), imm8
1023     //
1024     //  FMA4:
1025     //  dst(ModR/M.reg), src1(VEX_4V), src2(Imm[7:4]), src3(ModR/M),
1026     unsigned RegEnc = getX86RegEncoding(MI, CurOp++);
1027     VEX_R = ~(RegEnc >> 3) & 1;
1028     EVEX_R2 = ~(RegEnc >> 4) & 1;
1029 
1030     if (HasEVEX_K)
1031       EVEX_aaa = getX86RegEncoding(MI, CurOp++);
1032 
1033     if (HasVEX_4V) {
1034       unsigned VRegEnc = getX86RegEncoding(MI, CurOp++);
1035       VEX_4V = ~VRegEnc & 0xf;
1036       EVEX_V2 = ~(VRegEnc >> 4) & 1;
1037     }
1038 
1039     RegEnc = getX86RegEncoding(MI, CurOp++);
1040     VEX_B = ~(RegEnc >> 3) & 1;
1041     VEX_X = ~(RegEnc >> 4) & 1;
1042 
1043     if (EVEX_b) {
1044       if (HasEVEX_RC) {
1045         unsigned RcOperand = NumOps - 1;
1046         assert(RcOperand >= CurOp);
1047         EVEX_rc = MI.getOperand(RcOperand).getImm();
1048         assert(EVEX_rc <= 3 && "Invalid rounding control!");
1049       }
1050       EncodeRC = true;
1051     }
1052     break;
1053   }
1054   case X86II::MRMSrcReg4VOp3: {
1055     // Instruction format for 4VOp3:
1056     //   src1(ModR/M), src2(ModR/M), src3(VEX_4V)
1057     unsigned RegEnc = getX86RegEncoding(MI, CurOp++);
1058     VEX_R = ~(RegEnc >> 3) & 1;
1059 
1060     RegEnc = getX86RegEncoding(MI, CurOp++);
1061     VEX_B = ~(RegEnc >> 3) & 1;
1062 
1063     VEX_4V = ~getX86RegEncoding(MI, CurOp++) & 0xf;
1064     break;
1065   }
1066   case X86II::MRMSrcRegOp4: {
1067     //  dst(ModR/M.reg), src1(VEX_4V), src2(Imm[7:4]), src3(ModR/M),
1068     unsigned RegEnc = getX86RegEncoding(MI, CurOp++);
1069     VEX_R = ~(RegEnc >> 3) & 1;
1070 
1071     unsigned VRegEnc = getX86RegEncoding(MI, CurOp++);
1072     VEX_4V = ~VRegEnc & 0xf;
1073 
1074     // Skip second register source (encoded in Imm[7:4])
1075     ++CurOp;
1076 
1077     RegEnc = getX86RegEncoding(MI, CurOp++);
1078     VEX_B = ~(RegEnc >> 3) & 1;
1079     VEX_X = ~(RegEnc >> 4) & 1;
1080     break;
1081   }
1082   case X86II::MRMDestReg: {
1083     // MRMDestReg instructions forms:
1084     //  dst(ModR/M), src(ModR/M)
1085     //  dst(ModR/M), src(ModR/M), imm8
1086     //  dst(ModR/M), src1(VEX_4V), src2(ModR/M)
1087     unsigned RegEnc = getX86RegEncoding(MI, CurOp++);
1088     VEX_B = ~(RegEnc >> 3) & 1;
1089     VEX_X = ~(RegEnc >> 4) & 1;
1090 
1091     if (HasEVEX_K)
1092       EVEX_aaa = getX86RegEncoding(MI, CurOp++);
1093 
1094     if (HasVEX_4V) {
1095       unsigned VRegEnc = getX86RegEncoding(MI, CurOp++);
1096       VEX_4V = ~VRegEnc & 0xf;
1097       EVEX_V2 = ~(VRegEnc >> 4) & 1;
1098     }
1099 
1100     RegEnc = getX86RegEncoding(MI, CurOp++);
1101     VEX_R = ~(RegEnc >> 3) & 1;
1102     EVEX_R2 = ~(RegEnc >> 4) & 1;
1103     if (EVEX_b)
1104       EncodeRC = true;
1105     break;
1106   }
1107   case X86II::MRMr0: {
1108     // MRMr0 instructions forms:
1109     //  11:rrr:000
1110     //  dst(ModR/M)
1111     unsigned RegEnc = getX86RegEncoding(MI, CurOp++);
1112     VEX_R = ~(RegEnc >> 3) & 1;
1113     EVEX_R2 = ~(RegEnc >> 4) & 1;
1114     break;
1115   }
1116   case X86II::MRM0r:
1117   case X86II::MRM1r:
1118   case X86II::MRM2r:
1119   case X86II::MRM3r:
1120   case X86II::MRM4r:
1121   case X86II::MRM5r:
1122   case X86II::MRM6r:
1123   case X86II::MRM7r: {
1124     // MRM0r-MRM7r instructions forms:
1125     //  dst(VEX_4V), src(ModR/M), imm8
1126     if (HasVEX_4V) {
1127       unsigned VRegEnc = getX86RegEncoding(MI, CurOp++);
1128       VEX_4V = ~VRegEnc & 0xf;
1129       EVEX_V2 = ~(VRegEnc >> 4) & 1;
1130     }
1131     if (HasEVEX_K)
1132       EVEX_aaa = getX86RegEncoding(MI, CurOp++);
1133 
1134     unsigned RegEnc = getX86RegEncoding(MI, CurOp++);
1135     VEX_B = ~(RegEnc >> 3) & 1;
1136     VEX_X = ~(RegEnc >> 4) & 1;
1137     break;
1138   }
1139   }
1140 
1141   if (Encoding == X86II::VEX || Encoding == X86II::XOP) {
1142     // VEX opcode prefix can have 2 or 3 bytes
1143     //
1144     //  3 bytes:
1145     //    +-----+ +--------------+ +-------------------+
1146     //    | C4h | | RXB | m-mmmm | | W | vvvv | L | pp |
1147     //    +-----+ +--------------+ +-------------------+
1148     //  2 bytes:
1149     //    +-----+ +-------------------+
1150     //    | C5h | | R | vvvv | L | pp |
1151     //    +-----+ +-------------------+
1152     //
1153     //  XOP uses a similar prefix:
1154     //    +-----+ +--------------+ +-------------------+
1155     //    | 8Fh | | RXB | m-mmmm | | W | vvvv | L | pp |
1156     //    +-----+ +--------------+ +-------------------+
1157     uint8_t LastByte = VEX_PP | (VEX_L << 2) | (VEX_4V << 3);
1158 
1159     // Can we use the 2 byte VEX prefix?
1160     if (!(MI.getFlags() & X86::IP_USE_VEX3) && Encoding == X86II::VEX &&
1161         VEX_B && VEX_X && !VEX_W && (VEX_5M == 1)) {
1162       emitByte(0xC5, OS);
1163       emitByte(LastByte | (VEX_R << 7), OS);
1164       return;
1165     }
1166 
1167     // 3 byte VEX prefix
1168     emitByte(Encoding == X86II::XOP ? 0x8F : 0xC4, OS);
1169     emitByte(VEX_R << 7 | VEX_X << 6 | VEX_B << 5 | VEX_5M, OS);
1170     emitByte(LastByte | (VEX_W << 7), OS);
1171   } else {
1172     assert(Encoding == X86II::EVEX && "unknown encoding!");
1173     // EVEX opcode prefix can have 4 bytes
1174     //
1175     // +-----+ +--------------+ +-------------------+ +------------------------+
1176     // | 62h | | RXBR' | 00mm | | W | vvvv | U | pp | | z | L'L | b | v' | aaa |
1177     // +-----+ +--------------+ +-------------------+ +------------------------+
1178     assert((VEX_5M & 0x3) == VEX_5M &&
1179            "More than 2 significant bits in VEX.m-mmmm fields for EVEX!");
1180 
1181     emitByte(0x62, OS);
1182     emitByte((VEX_R << 7) | (VEX_X << 6) | (VEX_B << 5) | (EVEX_R2 << 4) |
1183                  VEX_5M,
1184              OS);
1185     emitByte((VEX_W << 7) | (VEX_4V << 3) | (EVEX_U << 2) | VEX_PP, OS);
1186     if (EncodeRC)
1187       emitByte((EVEX_z << 7) | (EVEX_rc << 5) | (EVEX_b << 4) | (EVEX_V2 << 3) |
1188                    EVEX_aaa,
1189                OS);
1190     else
1191       emitByte((EVEX_z << 7) | (EVEX_L2 << 6) | (VEX_L << 5) | (EVEX_b << 4) |
1192                    (EVEX_V2 << 3) | EVEX_aaa,
1193                OS);
1194   }
1195 }
1196 
1197 /// Emit REX prefix which specifies
1198 ///   1) 64-bit instructions,
1199 ///   2) non-default operand size, and
1200 ///   3) use of X86-64 extended registers.
1201 ///
1202 /// \returns true if REX prefix is used, otherwise returns false.
1203 bool X86MCCodeEmitter::emitREXPrefix(int MemOperand, const MCInst &MI,
1204                                      const MCSubtargetInfo &STI,
1205                                      raw_ostream &OS) const {
1206   uint8_t REX = [&, MemOperand]() {
1207     uint8_t REX = 0;
1208     bool UsesHighByteReg = false;
1209 
1210     const MCInstrDesc &Desc = MCII.get(MI.getOpcode());
1211     uint64_t TSFlags = Desc.TSFlags;
1212 
1213     if (TSFlags & X86II::REX_W)
1214       REX |= 1 << 3; // set REX.W
1215 
1216     if (MI.getNumOperands() == 0)
1217       return REX;
1218 
1219     unsigned NumOps = MI.getNumOperands();
1220     unsigned CurOp = X86II::getOperandBias(Desc);
1221 
1222     // If it accesses SPL, BPL, SIL, or DIL, then it requires a 0x40 REX prefix.
1223     for (unsigned i = CurOp; i != NumOps; ++i) {
1224       const MCOperand &MO = MI.getOperand(i);
1225       if (MO.isReg()) {
1226         unsigned Reg = MO.getReg();
1227         if (Reg == X86::AH || Reg == X86::BH || Reg == X86::CH ||
1228             Reg == X86::DH)
1229           UsesHighByteReg = true;
1230         if (X86II::isX86_64NonExtLowByteReg(Reg))
1231           // FIXME: The caller of determineREXPrefix slaps this prefix onto
1232           // anything that returns non-zero.
1233           REX |= 0x40; // REX fixed encoding prefix
1234       } else if (MO.isExpr() && STI.getTargetTriple().isX32()) {
1235         // GOTTPOFF and TLSDESC relocations require a REX prefix to allow
1236         // linker optimizations: even if the instructions we see may not require
1237         // any prefix, they may be replaced by instructions that do. This is
1238         // handled as a special case here so that it also works for hand-written
1239         // assembly without the user needing to write REX, as with GNU as.
1240         const auto *Ref = dyn_cast<MCSymbolRefExpr>(MO.getExpr());
1241         if (Ref && (Ref->getKind() == MCSymbolRefExpr::VK_GOTTPOFF ||
1242                     Ref->getKind() == MCSymbolRefExpr::VK_TLSDESC)) {
1243           REX |= 0x40; // REX fixed encoding prefix
1244         }
1245       }
1246     }
1247 
1248     switch (TSFlags & X86II::FormMask) {
1249     case X86II::AddRegFrm:
1250       REX |= isREXExtendedReg(MI, CurOp++) << 0; // REX.B
1251       break;
1252     case X86II::MRMSrcReg:
1253     case X86II::MRMSrcRegCC:
1254       REX |= isREXExtendedReg(MI, CurOp++) << 2; // REX.R
1255       REX |= isREXExtendedReg(MI, CurOp++) << 0; // REX.B
1256       break;
1257     case X86II::MRMSrcMem:
1258     case X86II::MRMSrcMemCC:
1259       REX |= isREXExtendedReg(MI, CurOp++) << 2;                        // REX.R
1260       REX |= isREXExtendedReg(MI, MemOperand + X86::AddrBaseReg) << 0;  // REX.B
1261       REX |= isREXExtendedReg(MI, MemOperand + X86::AddrIndexReg) << 1; // REX.X
1262       CurOp += X86::AddrNumOperands;
1263       break;
1264     case X86II::MRMDestReg:
1265       REX |= isREXExtendedReg(MI, CurOp++) << 0; // REX.B
1266       REX |= isREXExtendedReg(MI, CurOp++) << 2; // REX.R
1267       break;
1268     case X86II::MRMDestMem:
1269       REX |= isREXExtendedReg(MI, MemOperand + X86::AddrBaseReg) << 0;  // REX.B
1270       REX |= isREXExtendedReg(MI, MemOperand + X86::AddrIndexReg) << 1; // REX.X
1271       CurOp += X86::AddrNumOperands;
1272       REX |= isREXExtendedReg(MI, CurOp++) << 2; // REX.R
1273       break;
1274     case X86II::MRMXmCC:
1275     case X86II::MRMXm:
1276     case X86II::MRM0m:
1277     case X86II::MRM1m:
1278     case X86II::MRM2m:
1279     case X86II::MRM3m:
1280     case X86II::MRM4m:
1281     case X86II::MRM5m:
1282     case X86II::MRM6m:
1283     case X86II::MRM7m:
1284       REX |= isREXExtendedReg(MI, MemOperand + X86::AddrBaseReg) << 0;  // REX.B
1285       REX |= isREXExtendedReg(MI, MemOperand + X86::AddrIndexReg) << 1; // REX.X
1286       break;
1287     case X86II::MRMXrCC:
1288     case X86II::MRMXr:
1289     case X86II::MRM0r:
1290     case X86II::MRM1r:
1291     case X86II::MRM2r:
1292     case X86II::MRM3r:
1293     case X86II::MRM4r:
1294     case X86II::MRM5r:
1295     case X86II::MRM6r:
1296     case X86II::MRM7r:
1297       REX |= isREXExtendedReg(MI, CurOp++) << 0; // REX.B
1298       break;
1299     case X86II::MRMr0:
1300       REX |= isREXExtendedReg(MI, CurOp++) << 2; // REX.R
1301       break;
1302     case X86II::MRMDestMemFSIB:
1303       llvm_unreachable("FSIB format never need REX prefix!");
1304     }
1305     if (REX && UsesHighByteReg)
1306       report_fatal_error(
1307           "Cannot encode high byte register in REX-prefixed instruction");
1308     return REX;
1309   }();
1310 
1311   if (!REX)
1312     return false;
1313 
1314   emitByte(0x40 | REX, OS);
1315   return true;
1316 }
1317 
1318 /// Emit segment override opcode prefix as needed.
1319 void X86MCCodeEmitter::emitSegmentOverridePrefix(unsigned SegOperand,
1320                                                  const MCInst &MI,
1321                                                  raw_ostream &OS) const {
1322   // Check for explicit segment override on memory operand.
1323   if (unsigned Reg = MI.getOperand(SegOperand).getReg())
1324     emitByte(X86::getSegmentOverridePrefixForReg(Reg), OS);
1325 }
1326 
1327 /// Emit all instruction prefixes prior to the opcode.
1328 ///
1329 /// \param MemOperand the operand # of the start of a memory operand if present.
1330 /// If not present, it is -1.
1331 ///
1332 /// \returns true if REX prefix is used, otherwise returns false.
1333 bool X86MCCodeEmitter::emitOpcodePrefix(int MemOperand, const MCInst &MI,
1334                                         const MCSubtargetInfo &STI,
1335                                         raw_ostream &OS) const {
1336   const MCInstrDesc &Desc = MCII.get(MI.getOpcode());
1337   uint64_t TSFlags = Desc.TSFlags;
1338 
1339   // Emit the operand size opcode prefix as needed.
1340   if ((TSFlags & X86II::OpSizeMask) ==
1341       (STI.hasFeature(X86::Mode16Bit) ? X86II::OpSize32 : X86II::OpSize16))
1342     emitByte(0x66, OS);
1343 
1344   // Emit the LOCK opcode prefix.
1345   if (TSFlags & X86II::LOCK || MI.getFlags() & X86::IP_HAS_LOCK)
1346     emitByte(0xF0, OS);
1347 
1348   // Emit the NOTRACK opcode prefix.
1349   if (TSFlags & X86II::NOTRACK || MI.getFlags() & X86::IP_HAS_NOTRACK)
1350     emitByte(0x3E, OS);
1351 
1352   switch (TSFlags & X86II::OpPrefixMask) {
1353   case X86II::PD: // 66
1354     emitByte(0x66, OS);
1355     break;
1356   case X86II::XS: // F3
1357     emitByte(0xF3, OS);
1358     break;
1359   case X86II::XD: // F2
1360     emitByte(0xF2, OS);
1361     break;
1362   }
1363 
1364   // Handle REX prefix.
1365   assert((STI.hasFeature(X86::Mode64Bit) || !(TSFlags & X86II::REX_W)) &&
1366          "REX.W requires 64bit mode.");
1367   bool HasREX = STI.hasFeature(X86::Mode64Bit)
1368                     ? emitREXPrefix(MemOperand, MI, STI, OS)
1369                     : false;
1370 
1371   // 0x0F escape code must be emitted just before the opcode.
1372   switch (TSFlags & X86II::OpMapMask) {
1373   case X86II::TB:        // Two-byte opcode map
1374   case X86II::T8:        // 0F 38
1375   case X86II::TA:        // 0F 3A
1376   case X86II::ThreeDNow: // 0F 0F, second 0F emitted by caller.
1377     emitByte(0x0F, OS);
1378     break;
1379   }
1380 
1381   switch (TSFlags & X86II::OpMapMask) {
1382   case X86II::T8: // 0F 38
1383     emitByte(0x38, OS);
1384     break;
1385   case X86II::TA: // 0F 3A
1386     emitByte(0x3A, OS);
1387     break;
1388   }
1389 
1390   return HasREX;
1391 }
1392 
1393 void X86MCCodeEmitter::emitPrefix(const MCInst &MI, raw_ostream &OS,
1394                                   const MCSubtargetInfo &STI) const {
1395   unsigned Opcode = MI.getOpcode();
1396   const MCInstrDesc &Desc = MCII.get(Opcode);
1397   uint64_t TSFlags = Desc.TSFlags;
1398 
1399   // Pseudo instructions don't get encoded.
1400   if (X86II::isPseudo(TSFlags))
1401     return;
1402 
1403   unsigned CurOp = X86II::getOperandBias(Desc);
1404 
1405   emitPrefixImpl(CurOp, MI, STI, OS);
1406 }
1407 
1408 void X86MCCodeEmitter::encodeInstruction(const MCInst &MI, raw_ostream &OS,
1409                                          SmallVectorImpl<MCFixup> &Fixups,
1410                                          const MCSubtargetInfo &STI) const {
1411   unsigned Opcode = MI.getOpcode();
1412   const MCInstrDesc &Desc = MCII.get(Opcode);
1413   uint64_t TSFlags = Desc.TSFlags;
1414 
1415   // Pseudo instructions don't get encoded.
1416   if (X86II::isPseudo(TSFlags))
1417     return;
1418 
1419   unsigned NumOps = Desc.getNumOperands();
1420   unsigned CurOp = X86II::getOperandBias(Desc);
1421 
1422   uint64_t StartByte = OS.tell();
1423 
1424   bool HasREX = emitPrefixImpl(CurOp, MI, STI, OS);
1425 
1426   // It uses the VEX.VVVV field?
1427   bool HasVEX_4V = TSFlags & X86II::VEX_4V;
1428   bool HasVEX_I8Reg = (TSFlags & X86II::ImmMask) == X86II::Imm8Reg;
1429 
1430   // It uses the EVEX.aaa field?
1431   bool HasEVEX_K = TSFlags & X86II::EVEX_K;
1432   bool HasEVEX_RC = TSFlags & X86II::EVEX_RC;
1433 
1434   // Used if a register is encoded in 7:4 of immediate.
1435   unsigned I8RegNum = 0;
1436 
1437   uint8_t BaseOpcode = X86II::getBaseOpcodeFor(TSFlags);
1438 
1439   if ((TSFlags & X86II::OpMapMask) == X86II::ThreeDNow)
1440     BaseOpcode = 0x0F; // Weird 3DNow! encoding.
1441 
1442   unsigned OpcodeOffset = 0;
1443 
1444   uint64_t Form = TSFlags & X86II::FormMask;
1445   switch (Form) {
1446   default:
1447     errs() << "FORM: " << Form << "\n";
1448     llvm_unreachable("Unknown FormMask value in X86MCCodeEmitter!");
1449   case X86II::Pseudo:
1450     llvm_unreachable("Pseudo instruction shouldn't be emitted");
1451   case X86II::RawFrmDstSrc:
1452   case X86II::RawFrmSrc:
1453   case X86II::RawFrmDst:
1454   case X86II::PrefixByte:
1455     emitByte(BaseOpcode, OS);
1456     break;
1457   case X86II::AddCCFrm: {
1458     // This will be added to the opcode in the fallthrough.
1459     OpcodeOffset = MI.getOperand(NumOps - 1).getImm();
1460     assert(OpcodeOffset < 16 && "Unexpected opcode offset!");
1461     --NumOps; // Drop the operand from the end.
1462     LLVM_FALLTHROUGH;
1463   case X86II::RawFrm:
1464     emitByte(BaseOpcode + OpcodeOffset, OS);
1465 
1466     if (!STI.hasFeature(X86::Mode64Bit) || !isPCRel32Branch(MI, MCII))
1467       break;
1468 
1469     const MCOperand &Op = MI.getOperand(CurOp++);
1470     emitImmediate(Op, MI.getLoc(), X86II::getSizeOfImm(TSFlags),
1471                   MCFixupKind(X86::reloc_branch_4byte_pcrel), StartByte, OS,
1472                   Fixups);
1473     break;
1474   }
1475   case X86II::RawFrmMemOffs:
1476     emitByte(BaseOpcode, OS);
1477     emitImmediate(MI.getOperand(CurOp++), MI.getLoc(),
1478                   X86II::getSizeOfImm(TSFlags), getImmFixupKind(TSFlags),
1479                   StartByte, OS, Fixups);
1480     ++CurOp; // skip segment operand
1481     break;
1482   case X86II::RawFrmImm8:
1483     emitByte(BaseOpcode, OS);
1484     emitImmediate(MI.getOperand(CurOp++), MI.getLoc(),
1485                   X86II::getSizeOfImm(TSFlags), getImmFixupKind(TSFlags),
1486                   StartByte, OS, Fixups);
1487     emitImmediate(MI.getOperand(CurOp++), MI.getLoc(), 1, FK_Data_1, StartByte,
1488                   OS, Fixups);
1489     break;
1490   case X86II::RawFrmImm16:
1491     emitByte(BaseOpcode, OS);
1492     emitImmediate(MI.getOperand(CurOp++), MI.getLoc(),
1493                   X86II::getSizeOfImm(TSFlags), getImmFixupKind(TSFlags),
1494                   StartByte, OS, Fixups);
1495     emitImmediate(MI.getOperand(CurOp++), MI.getLoc(), 2, FK_Data_2, StartByte,
1496                   OS, Fixups);
1497     break;
1498 
1499   case X86II::AddRegFrm:
1500     emitByte(BaseOpcode + getX86RegNum(MI.getOperand(CurOp++)), OS);
1501     break;
1502 
1503   case X86II::MRMDestReg: {
1504     emitByte(BaseOpcode, OS);
1505     unsigned SrcRegNum = CurOp + 1;
1506 
1507     if (HasEVEX_K) // Skip writemask
1508       ++SrcRegNum;
1509 
1510     if (HasVEX_4V) // Skip 1st src (which is encoded in VEX_VVVV)
1511       ++SrcRegNum;
1512 
1513     emitRegModRMByte(MI.getOperand(CurOp),
1514                      getX86RegNum(MI.getOperand(SrcRegNum)), OS);
1515     CurOp = SrcRegNum + 1;
1516     break;
1517   }
1518   case X86II::MRMDestMemFSIB:
1519   case X86II::MRMDestMem: {
1520     emitByte(BaseOpcode, OS);
1521     unsigned SrcRegNum = CurOp + X86::AddrNumOperands;
1522 
1523     if (HasEVEX_K) // Skip writemask
1524       ++SrcRegNum;
1525 
1526     if (HasVEX_4V) // Skip 1st src (which is encoded in VEX_VVVV)
1527       ++SrcRegNum;
1528 
1529     bool ForceSIB = (Form == X86II::MRMDestMemFSIB);
1530     emitMemModRMByte(MI, CurOp, getX86RegNum(MI.getOperand(SrcRegNum)), TSFlags,
1531                      HasREX, StartByte, OS, Fixups, STI, ForceSIB);
1532     CurOp = SrcRegNum + 1;
1533     break;
1534   }
1535   case X86II::MRMSrcReg: {
1536     emitByte(BaseOpcode, OS);
1537     unsigned SrcRegNum = CurOp + 1;
1538 
1539     if (HasEVEX_K) // Skip writemask
1540       ++SrcRegNum;
1541 
1542     if (HasVEX_4V) // Skip 1st src (which is encoded in VEX_VVVV)
1543       ++SrcRegNum;
1544 
1545     emitRegModRMByte(MI.getOperand(SrcRegNum),
1546                      getX86RegNum(MI.getOperand(CurOp)), OS);
1547     CurOp = SrcRegNum + 1;
1548     if (HasVEX_I8Reg)
1549       I8RegNum = getX86RegEncoding(MI, CurOp++);
1550     // do not count the rounding control operand
1551     if (HasEVEX_RC)
1552       --NumOps;
1553     break;
1554   }
1555   case X86II::MRMSrcReg4VOp3: {
1556     emitByte(BaseOpcode, OS);
1557     unsigned SrcRegNum = CurOp + 1;
1558 
1559     emitRegModRMByte(MI.getOperand(SrcRegNum),
1560                      getX86RegNum(MI.getOperand(CurOp)), OS);
1561     CurOp = SrcRegNum + 1;
1562     ++CurOp; // Encoded in VEX.VVVV
1563     break;
1564   }
1565   case X86II::MRMSrcRegOp4: {
1566     emitByte(BaseOpcode, OS);
1567     unsigned SrcRegNum = CurOp + 1;
1568 
1569     // Skip 1st src (which is encoded in VEX_VVVV)
1570     ++SrcRegNum;
1571 
1572     // Capture 2nd src (which is encoded in Imm[7:4])
1573     assert(HasVEX_I8Reg && "MRMSrcRegOp4 should imply VEX_I8Reg");
1574     I8RegNum = getX86RegEncoding(MI, SrcRegNum++);
1575 
1576     emitRegModRMByte(MI.getOperand(SrcRegNum),
1577                      getX86RegNum(MI.getOperand(CurOp)), OS);
1578     CurOp = SrcRegNum + 1;
1579     break;
1580   }
1581   case X86II::MRMSrcRegCC: {
1582     unsigned FirstOp = CurOp++;
1583     unsigned SecondOp = CurOp++;
1584 
1585     unsigned CC = MI.getOperand(CurOp++).getImm();
1586     emitByte(BaseOpcode + CC, OS);
1587 
1588     emitRegModRMByte(MI.getOperand(SecondOp),
1589                      getX86RegNum(MI.getOperand(FirstOp)), OS);
1590     break;
1591   }
1592   case X86II::MRMSrcMemFSIB:
1593   case X86II::MRMSrcMem: {
1594     unsigned FirstMemOp = CurOp + 1;
1595 
1596     if (HasEVEX_K) // Skip writemask
1597       ++FirstMemOp;
1598 
1599     if (HasVEX_4V)
1600       ++FirstMemOp; // Skip the register source (which is encoded in VEX_VVVV).
1601 
1602     emitByte(BaseOpcode, OS);
1603 
1604     bool ForceSIB = (Form == X86II::MRMSrcMemFSIB);
1605     emitMemModRMByte(MI, FirstMemOp, getX86RegNum(MI.getOperand(CurOp)),
1606                      TSFlags, HasREX, StartByte, OS, Fixups, STI, ForceSIB);
1607     CurOp = FirstMemOp + X86::AddrNumOperands;
1608     if (HasVEX_I8Reg)
1609       I8RegNum = getX86RegEncoding(MI, CurOp++);
1610     break;
1611   }
1612   case X86II::MRMSrcMem4VOp3: {
1613     unsigned FirstMemOp = CurOp + 1;
1614 
1615     emitByte(BaseOpcode, OS);
1616 
1617     emitMemModRMByte(MI, FirstMemOp, getX86RegNum(MI.getOperand(CurOp)),
1618                      TSFlags, HasREX, StartByte, OS, Fixups, STI);
1619     CurOp = FirstMemOp + X86::AddrNumOperands;
1620     ++CurOp; // Encoded in VEX.VVVV.
1621     break;
1622   }
1623   case X86II::MRMSrcMemOp4: {
1624     unsigned FirstMemOp = CurOp + 1;
1625 
1626     ++FirstMemOp; // Skip the register source (which is encoded in VEX_VVVV).
1627 
1628     // Capture second register source (encoded in Imm[7:4])
1629     assert(HasVEX_I8Reg && "MRMSrcRegOp4 should imply VEX_I8Reg");
1630     I8RegNum = getX86RegEncoding(MI, FirstMemOp++);
1631 
1632     emitByte(BaseOpcode, OS);
1633 
1634     emitMemModRMByte(MI, FirstMemOp, getX86RegNum(MI.getOperand(CurOp)),
1635                      TSFlags, HasREX, StartByte, OS, Fixups, STI);
1636     CurOp = FirstMemOp + X86::AddrNumOperands;
1637     break;
1638   }
1639   case X86II::MRMSrcMemCC: {
1640     unsigned RegOp = CurOp++;
1641     unsigned FirstMemOp = CurOp;
1642     CurOp = FirstMemOp + X86::AddrNumOperands;
1643 
1644     unsigned CC = MI.getOperand(CurOp++).getImm();
1645     emitByte(BaseOpcode + CC, OS);
1646 
1647     emitMemModRMByte(MI, FirstMemOp, getX86RegNum(MI.getOperand(RegOp)),
1648                      TSFlags, HasREX, StartByte, OS, Fixups, STI);
1649     break;
1650   }
1651 
1652   case X86II::MRMXrCC: {
1653     unsigned RegOp = CurOp++;
1654 
1655     unsigned CC = MI.getOperand(CurOp++).getImm();
1656     emitByte(BaseOpcode + CC, OS);
1657     emitRegModRMByte(MI.getOperand(RegOp), 0, OS);
1658     break;
1659   }
1660 
1661   case X86II::MRMXr:
1662   case X86II::MRM0r:
1663   case X86II::MRM1r:
1664   case X86II::MRM2r:
1665   case X86II::MRM3r:
1666   case X86II::MRM4r:
1667   case X86II::MRM5r:
1668   case X86II::MRM6r:
1669   case X86II::MRM7r:
1670     if (HasVEX_4V) // Skip the register dst (which is encoded in VEX_VVVV).
1671       ++CurOp;
1672     if (HasEVEX_K) // Skip writemask
1673       ++CurOp;
1674     emitByte(BaseOpcode, OS);
1675     emitRegModRMByte(MI.getOperand(CurOp++),
1676                      (Form == X86II::MRMXr) ? 0 : Form - X86II::MRM0r, OS);
1677     break;
1678   case X86II::MRMr0:
1679     emitByte(BaseOpcode, OS);
1680     emitByte(modRMByte(3, getX86RegNum(MI.getOperand(CurOp++)),0), OS);
1681     break;
1682 
1683   case X86II::MRMXmCC: {
1684     unsigned FirstMemOp = CurOp;
1685     CurOp = FirstMemOp + X86::AddrNumOperands;
1686 
1687     unsigned CC = MI.getOperand(CurOp++).getImm();
1688     emitByte(BaseOpcode + CC, OS);
1689 
1690     emitMemModRMByte(MI, FirstMemOp, 0, TSFlags, HasREX, StartByte, OS, Fixups,
1691                      STI);
1692     break;
1693   }
1694 
1695   case X86II::MRMXm:
1696   case X86II::MRM0m:
1697   case X86II::MRM1m:
1698   case X86II::MRM2m:
1699   case X86II::MRM3m:
1700   case X86II::MRM4m:
1701   case X86II::MRM5m:
1702   case X86II::MRM6m:
1703   case X86II::MRM7m:
1704     if (HasVEX_4V) // Skip the register dst (which is encoded in VEX_VVVV).
1705       ++CurOp;
1706     if (HasEVEX_K) // Skip writemask
1707       ++CurOp;
1708     emitByte(BaseOpcode, OS);
1709     emitMemModRMByte(MI, CurOp,
1710                      (Form == X86II::MRMXm) ? 0 : Form - X86II::MRM0m, TSFlags,
1711                      HasREX, StartByte, OS, Fixups, STI);
1712     CurOp += X86::AddrNumOperands;
1713     break;
1714 
1715   case X86II::MRM0X:
1716   case X86II::MRM1X:
1717   case X86II::MRM2X:
1718   case X86II::MRM3X:
1719   case X86II::MRM4X:
1720   case X86II::MRM5X:
1721   case X86II::MRM6X:
1722   case X86II::MRM7X:
1723     emitByte(BaseOpcode, OS);
1724     emitByte(0xC0 + ((Form - X86II::MRM0X) << 3), OS);
1725     break;
1726 
1727   case X86II::MRM_C0:
1728   case X86II::MRM_C1:
1729   case X86II::MRM_C2:
1730   case X86II::MRM_C3:
1731   case X86II::MRM_C4:
1732   case X86II::MRM_C5:
1733   case X86II::MRM_C6:
1734   case X86II::MRM_C7:
1735   case X86II::MRM_C8:
1736   case X86II::MRM_C9:
1737   case X86II::MRM_CA:
1738   case X86II::MRM_CB:
1739   case X86II::MRM_CC:
1740   case X86II::MRM_CD:
1741   case X86II::MRM_CE:
1742   case X86II::MRM_CF:
1743   case X86II::MRM_D0:
1744   case X86II::MRM_D1:
1745   case X86II::MRM_D2:
1746   case X86II::MRM_D3:
1747   case X86II::MRM_D4:
1748   case X86II::MRM_D5:
1749   case X86II::MRM_D6:
1750   case X86II::MRM_D7:
1751   case X86II::MRM_D8:
1752   case X86II::MRM_D9:
1753   case X86II::MRM_DA:
1754   case X86II::MRM_DB:
1755   case X86II::MRM_DC:
1756   case X86II::MRM_DD:
1757   case X86II::MRM_DE:
1758   case X86II::MRM_DF:
1759   case X86II::MRM_E0:
1760   case X86II::MRM_E1:
1761   case X86II::MRM_E2:
1762   case X86II::MRM_E3:
1763   case X86II::MRM_E4:
1764   case X86II::MRM_E5:
1765   case X86II::MRM_E6:
1766   case X86II::MRM_E7:
1767   case X86II::MRM_E8:
1768   case X86II::MRM_E9:
1769   case X86II::MRM_EA:
1770   case X86II::MRM_EB:
1771   case X86II::MRM_EC:
1772   case X86II::MRM_ED:
1773   case X86II::MRM_EE:
1774   case X86II::MRM_EF:
1775   case X86II::MRM_F0:
1776   case X86II::MRM_F1:
1777   case X86II::MRM_F2:
1778   case X86II::MRM_F3:
1779   case X86II::MRM_F4:
1780   case X86II::MRM_F5:
1781   case X86II::MRM_F6:
1782   case X86II::MRM_F7:
1783   case X86II::MRM_F8:
1784   case X86II::MRM_F9:
1785   case X86II::MRM_FA:
1786   case X86II::MRM_FB:
1787   case X86II::MRM_FC:
1788   case X86II::MRM_FD:
1789   case X86II::MRM_FE:
1790   case X86II::MRM_FF:
1791     emitByte(BaseOpcode, OS);
1792     emitByte(0xC0 + Form - X86II::MRM_C0, OS);
1793     break;
1794   }
1795 
1796   if (HasVEX_I8Reg) {
1797     // The last source register of a 4 operand instruction in AVX is encoded
1798     // in bits[7:4] of a immediate byte.
1799     assert(I8RegNum < 16 && "Register encoding out of range");
1800     I8RegNum <<= 4;
1801     if (CurOp != NumOps) {
1802       unsigned Val = MI.getOperand(CurOp++).getImm();
1803       assert(Val < 16 && "Immediate operand value out of range");
1804       I8RegNum |= Val;
1805     }
1806     emitImmediate(MCOperand::createImm(I8RegNum), MI.getLoc(), 1, FK_Data_1,
1807                   StartByte, OS, Fixups);
1808   } else {
1809     // If there is a remaining operand, it must be a trailing immediate. Emit it
1810     // according to the right size for the instruction. Some instructions
1811     // (SSE4a extrq and insertq) have two trailing immediates.
1812     while (CurOp != NumOps && NumOps - CurOp <= 2) {
1813       emitImmediate(MI.getOperand(CurOp++), MI.getLoc(),
1814                     X86II::getSizeOfImm(TSFlags), getImmFixupKind(TSFlags),
1815                     StartByte, OS, Fixups);
1816     }
1817   }
1818 
1819   if ((TSFlags & X86II::OpMapMask) == X86II::ThreeDNow)
1820     emitByte(X86II::getBaseOpcodeFor(TSFlags), OS);
1821 
1822   assert(OS.tell() - StartByte <= 15 &&
1823          "The size of instruction must be no longer than 15.");
1824 #ifndef NDEBUG
1825   // FIXME: Verify.
1826   if (/*!Desc.isVariadic() &&*/ CurOp != NumOps) {
1827     errs() << "Cannot encode all operands of: ";
1828     MI.dump();
1829     errs() << '\n';
1830     abort();
1831   }
1832 #endif
1833 }
1834 
1835 MCCodeEmitter *llvm::createX86MCCodeEmitter(const MCInstrInfo &MCII,
1836                                             const MCRegisterInfo &MRI,
1837                                             MCContext &Ctx) {
1838   return new X86MCCodeEmitter(MCII, Ctx);
1839 }
1840