X86MCCodeEmitter.cpp revision 8a312fb3aaec90537d434a5cc41edf566ff80dca
1//===-- X86MCCodeEmitter.cpp - Convert X86 code to machine code -----------===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the X86MCCodeEmitter class.
11//
12//===----------------------------------------------------------------------===//
13
14#define DEBUG_TYPE "mccodeemitter"
15#include "MCTargetDesc/X86MCTargetDesc.h"
16#include "MCTargetDesc/X86BaseInfo.h"
17#include "MCTargetDesc/X86FixupKinds.h"
18#include "llvm/MC/MCCodeEmitter.h"
19#include "llvm/MC/MCExpr.h"
20#include "llvm/MC/MCInst.h"
21#include "llvm/MC/MCInstrInfo.h"
22#include "llvm/MC/MCRegisterInfo.h"
23#include "llvm/MC/MCSubtargetInfo.h"
24#include "llvm/MC/MCSymbol.h"
25#include "llvm/Support/raw_ostream.h"
26
27using namespace llvm;
28
29namespace {
30class X86MCCodeEmitter : public MCCodeEmitter {
31  X86MCCodeEmitter(const X86MCCodeEmitter &) LLVM_DELETED_FUNCTION;
32  void operator=(const X86MCCodeEmitter &) LLVM_DELETED_FUNCTION;
33  const MCInstrInfo &MCII;
34  const MCSubtargetInfo &STI;
35  MCContext &Ctx;
36public:
37  X86MCCodeEmitter(const MCInstrInfo &mcii, const MCSubtargetInfo &sti,
38                   MCContext &ctx)
39    : MCII(mcii), STI(sti), Ctx(ctx) {
40  }
41
42  ~X86MCCodeEmitter() {}
43
44  bool is64BitMode() const {
45    // FIXME: Can tablegen auto-generate this?
46    return (STI.getFeatureBits() & X86::Mode64Bit) != 0;
47  }
48
49  bool is32BitMode() const {
50    // FIXME: Can tablegen auto-generate this?
51    return (STI.getFeatureBits() & X86::Mode64Bit) == 0;
52  }
53
54  static unsigned GetX86RegNum(const MCOperand &MO) {
55    return X86_MC::getX86RegNum(MO.getReg());
56  }
57
58  // On regular x86, both XMM0-XMM7 and XMM8-XMM15 are encoded in the range
59  // 0-7 and the difference between the 2 groups is given by the REX prefix.
60  // In the VEX prefix, registers are seen sequencially from 0-15 and encoded
61  // in 1's complement form, example:
62  //
63  //  ModRM field => XMM9 => 1
64  //  VEX.VVVV    => XMM9 => ~9
65  //
66  // See table 4-35 of Intel AVX Programming Reference for details.
67  static unsigned char getVEXRegisterEncoding(const MCInst &MI,
68                                              unsigned OpNum) {
69    unsigned SrcReg = MI.getOperand(OpNum).getReg();
70    unsigned SrcRegNum = GetX86RegNum(MI.getOperand(OpNum));
71    if (X86II::isX86_64ExtendedReg(SrcReg))
72      SrcRegNum |= 8;
73
74    // The registers represented through VEX_VVVV should
75    // be encoded in 1's complement form.
76    return (~SrcRegNum) & 0xf;
77  }
78
79  void EmitByte(unsigned char C, unsigned &CurByte, raw_ostream &OS) const {
80    OS << (char)C;
81    ++CurByte;
82  }
83
84  void EmitConstant(uint64_t Val, unsigned Size, unsigned &CurByte,
85                    raw_ostream &OS) const {
86    // Output the constant in little endian byte order.
87    for (unsigned i = 0; i != Size; ++i) {
88      EmitByte(Val & 255, CurByte, OS);
89      Val >>= 8;
90    }
91  }
92
93  void EmitImmediate(const MCOperand &Disp, SMLoc Loc,
94                     unsigned ImmSize, MCFixupKind FixupKind,
95                     unsigned &CurByte, raw_ostream &OS,
96                     SmallVectorImpl<MCFixup> &Fixups,
97                     int ImmOffset = 0) const;
98
99  inline static unsigned char ModRMByte(unsigned Mod, unsigned RegOpcode,
100                                        unsigned RM) {
101    assert(Mod < 4 && RegOpcode < 8 && RM < 8 && "ModRM Fields out of range!");
102    return RM | (RegOpcode << 3) | (Mod << 6);
103  }
104
105  void EmitRegModRMByte(const MCOperand &ModRMReg, unsigned RegOpcodeFld,
106                        unsigned &CurByte, raw_ostream &OS) const {
107    EmitByte(ModRMByte(3, RegOpcodeFld, GetX86RegNum(ModRMReg)), CurByte, OS);
108  }
109
110  void EmitSIBByte(unsigned SS, unsigned Index, unsigned Base,
111                   unsigned &CurByte, raw_ostream &OS) const {
112    // SIB byte is in the same format as the ModRMByte.
113    EmitByte(ModRMByte(SS, Index, Base), CurByte, OS);
114  }
115
116
117  void EmitMemModRMByte(const MCInst &MI, unsigned Op,
118                        unsigned RegOpcodeField,
119                        uint64_t TSFlags, unsigned &CurByte, raw_ostream &OS,
120                        SmallVectorImpl<MCFixup> &Fixups) const;
121
122  void EncodeInstruction(const MCInst &MI, raw_ostream &OS,
123                         SmallVectorImpl<MCFixup> &Fixups) const;
124
125  void EmitVEXOpcodePrefix(uint64_t TSFlags, unsigned &CurByte, int MemOperand,
126                           const MCInst &MI, const MCInstrDesc &Desc,
127                           raw_ostream &OS) const;
128
129  void EmitSegmentOverridePrefix(uint64_t TSFlags, unsigned &CurByte,
130                                 int MemOperand, const MCInst &MI,
131                                 raw_ostream &OS) const;
132
133  void EmitOpcodePrefix(uint64_t TSFlags, unsigned &CurByte, int MemOperand,
134                        const MCInst &MI, const MCInstrDesc &Desc,
135                        raw_ostream &OS) const;
136};
137
138} // end anonymous namespace
139
140
141MCCodeEmitter *llvm::createX86MCCodeEmitter(const MCInstrInfo &MCII,
142                                            const MCRegisterInfo &MRI,
143                                            const MCSubtargetInfo &STI,
144                                            MCContext &Ctx) {
145  return new X86MCCodeEmitter(MCII, STI, Ctx);
146}
147
148/// isDisp8 - Return true if this signed displacement fits in a 8-bit
149/// sign-extended field.
150static bool isDisp8(int Value) {
151  return Value == (signed char)Value;
152}
153
154/// getImmFixupKind - Return the appropriate fixup kind to use for an immediate
155/// in an instruction with the specified TSFlags.
156static MCFixupKind getImmFixupKind(uint64_t TSFlags) {
157  unsigned Size = X86II::getSizeOfImm(TSFlags);
158  bool isPCRel = X86II::isImmPCRel(TSFlags);
159
160  return MCFixup::getKindForSize(Size, isPCRel);
161}
162
163/// Is32BitMemOperand - Return true if the specified instruction has
164/// a 32-bit memory operand. Op specifies the operand # of the memoperand.
165static bool Is32BitMemOperand(const MCInst &MI, unsigned Op) {
166  const MCOperand &BaseReg  = MI.getOperand(Op+X86::AddrBaseReg);
167  const MCOperand &IndexReg = MI.getOperand(Op+X86::AddrIndexReg);
168
169  if ((BaseReg.getReg() != 0 &&
170       X86MCRegisterClasses[X86::GR32RegClassID].contains(BaseReg.getReg())) ||
171      (IndexReg.getReg() != 0 &&
172       X86MCRegisterClasses[X86::GR32RegClassID].contains(IndexReg.getReg())))
173    return true;
174  return false;
175}
176
177/// Is64BitMemOperand - Return true if the specified instruction has
178/// a 64-bit memory operand. Op specifies the operand # of the memoperand.
179#ifndef NDEBUG
180static bool Is64BitMemOperand(const MCInst &MI, unsigned Op) {
181  const MCOperand &BaseReg  = MI.getOperand(Op+X86::AddrBaseReg);
182  const MCOperand &IndexReg = MI.getOperand(Op+X86::AddrIndexReg);
183
184  if ((BaseReg.getReg() != 0 &&
185       X86MCRegisterClasses[X86::GR64RegClassID].contains(BaseReg.getReg())) ||
186      (IndexReg.getReg() != 0 &&
187       X86MCRegisterClasses[X86::GR64RegClassID].contains(IndexReg.getReg())))
188    return true;
189  return false;
190}
191#endif
192
193/// Is16BitMemOperand - Return true if the specified instruction has
194/// a 16-bit memory operand. Op specifies the operand # of the memoperand.
195static bool Is16BitMemOperand(const MCInst &MI, unsigned Op) {
196  const MCOperand &BaseReg  = MI.getOperand(Op+X86::AddrBaseReg);
197  const MCOperand &IndexReg = MI.getOperand(Op+X86::AddrIndexReg);
198
199  if ((BaseReg.getReg() != 0 &&
200       X86MCRegisterClasses[X86::GR16RegClassID].contains(BaseReg.getReg())) ||
201      (IndexReg.getReg() != 0 &&
202       X86MCRegisterClasses[X86::GR16RegClassID].contains(IndexReg.getReg())))
203    return true;
204  return false;
205}
206
207/// StartsWithGlobalOffsetTable - Check if this expression starts with
208///  _GLOBAL_OFFSET_TABLE_ and if it is of the form
209///  _GLOBAL_OFFSET_TABLE_-symbol. This is needed to support PIC on ELF
210/// i386 as _GLOBAL_OFFSET_TABLE_ is magical. We check only simple case that
211/// are know to be used: _GLOBAL_OFFSET_TABLE_ by itself or at the start
212/// of a binary expression.
213enum GlobalOffsetTableExprKind {
214  GOT_None,
215  GOT_Normal,
216  GOT_SymDiff
217};
218static GlobalOffsetTableExprKind
219StartsWithGlobalOffsetTable(const MCExpr *Expr) {
220  const MCExpr *RHS = 0;
221  if (Expr->getKind() == MCExpr::Binary) {
222    const MCBinaryExpr *BE = static_cast<const MCBinaryExpr *>(Expr);
223    Expr = BE->getLHS();
224    RHS = BE->getRHS();
225  }
226
227  if (Expr->getKind() != MCExpr::SymbolRef)
228    return GOT_None;
229
230  const MCSymbolRefExpr *Ref = static_cast<const MCSymbolRefExpr*>(Expr);
231  const MCSymbol &S = Ref->getSymbol();
232  if (S.getName() != "_GLOBAL_OFFSET_TABLE_")
233    return GOT_None;
234  if (RHS && RHS->getKind() == MCExpr::SymbolRef)
235    return GOT_SymDiff;
236  return GOT_Normal;
237}
238
239void X86MCCodeEmitter::
240EmitImmediate(const MCOperand &DispOp, SMLoc Loc, unsigned Size,
241              MCFixupKind FixupKind, unsigned &CurByte, raw_ostream &OS,
242              SmallVectorImpl<MCFixup> &Fixups, int ImmOffset) const {
243  const MCExpr *Expr = NULL;
244  if (DispOp.isImm()) {
245    // If this is a simple integer displacement that doesn't require a
246    // relocation, emit it now.
247    if (FixupKind != FK_PCRel_1 &&
248        FixupKind != FK_PCRel_2 &&
249        FixupKind != FK_PCRel_4) {
250      EmitConstant(DispOp.getImm()+ImmOffset, Size, CurByte, OS);
251      return;
252    }
253    Expr = MCConstantExpr::Create(DispOp.getImm(), Ctx);
254  } else {
255    Expr = DispOp.getExpr();
256  }
257
258  // If we have an immoffset, add it to the expression.
259  if ((FixupKind == FK_Data_4 ||
260       FixupKind == FK_Data_8 ||
261       FixupKind == MCFixupKind(X86::reloc_signed_4byte))) {
262    GlobalOffsetTableExprKind Kind = StartsWithGlobalOffsetTable(Expr);
263    if (Kind != GOT_None) {
264      assert(ImmOffset == 0);
265
266      FixupKind = MCFixupKind(X86::reloc_global_offset_table);
267      if (Kind == GOT_Normal)
268        ImmOffset = CurByte;
269    } else if (Expr->getKind() == MCExpr::SymbolRef) {
270      const MCSymbolRefExpr *Ref = static_cast<const MCSymbolRefExpr*>(Expr);
271      if (Ref->getKind() == MCSymbolRefExpr::VK_SECREL) {
272        FixupKind = MCFixupKind(FK_SecRel_4);
273      }
274    }
275  }
276
277  // If the fixup is pc-relative, we need to bias the value to be relative to
278  // the start of the field, not the end of the field.
279  if (FixupKind == FK_PCRel_4 ||
280      FixupKind == MCFixupKind(X86::reloc_riprel_4byte) ||
281      FixupKind == MCFixupKind(X86::reloc_riprel_4byte_movq_load))
282    ImmOffset -= 4;
283  if (FixupKind == FK_PCRel_2)
284    ImmOffset -= 2;
285  if (FixupKind == FK_PCRel_1)
286    ImmOffset -= 1;
287
288  if (ImmOffset)
289    Expr = MCBinaryExpr::CreateAdd(Expr, MCConstantExpr::Create(ImmOffset, Ctx),
290                                   Ctx);
291
292  // Emit a symbolic constant as a fixup and 4 zeros.
293  Fixups.push_back(MCFixup::Create(CurByte, Expr, FixupKind, Loc));
294  EmitConstant(0, Size, CurByte, OS);
295}
296
297void X86MCCodeEmitter::EmitMemModRMByte(const MCInst &MI, unsigned Op,
298                                        unsigned RegOpcodeField,
299                                        uint64_t TSFlags, unsigned &CurByte,
300                                        raw_ostream &OS,
301                                        SmallVectorImpl<MCFixup> &Fixups) const{
302  const MCOperand &Disp     = MI.getOperand(Op+X86::AddrDisp);
303  const MCOperand &Base     = MI.getOperand(Op+X86::AddrBaseReg);
304  const MCOperand &Scale    = MI.getOperand(Op+X86::AddrScaleAmt);
305  const MCOperand &IndexReg = MI.getOperand(Op+X86::AddrIndexReg);
306  unsigned BaseReg = Base.getReg();
307
308  // Handle %rip relative addressing.
309  if (BaseReg == X86::RIP) {    // [disp32+RIP] in X86-64 mode
310    assert(is64BitMode() && "Rip-relative addressing requires 64-bit mode");
311    assert(IndexReg.getReg() == 0 && "Invalid rip-relative address");
312    EmitByte(ModRMByte(0, RegOpcodeField, 5), CurByte, OS);
313
314    unsigned FixupKind = X86::reloc_riprel_4byte;
315
316    // movq loads are handled with a special relocation form which allows the
317    // linker to eliminate some loads for GOT references which end up in the
318    // same linkage unit.
319    if (MI.getOpcode() == X86::MOV64rm)
320      FixupKind = X86::reloc_riprel_4byte_movq_load;
321
322    // rip-relative addressing is actually relative to the *next* instruction.
323    // Since an immediate can follow the mod/rm byte for an instruction, this
324    // means that we need to bias the immediate field of the instruction with
325    // the size of the immediate field.  If we have this case, add it into the
326    // expression to emit.
327    int ImmSize = X86II::hasImm(TSFlags) ? X86II::getSizeOfImm(TSFlags) : 0;
328
329    EmitImmediate(Disp, MI.getLoc(), 4, MCFixupKind(FixupKind),
330                  CurByte, OS, Fixups, -ImmSize);
331    return;
332  }
333
334  unsigned BaseRegNo = BaseReg ? GetX86RegNum(Base) : -1U;
335
336  // Determine whether a SIB byte is needed.
337  // If no BaseReg, issue a RIP relative instruction only if the MCE can
338  // resolve addresses on-the-fly, otherwise use SIB (Intel Manual 2A, table
339  // 2-7) and absolute references.
340
341  if (// The SIB byte must be used if there is an index register.
342      IndexReg.getReg() == 0 &&
343      // The SIB byte must be used if the base is ESP/RSP/R12, all of which
344      // encode to an R/M value of 4, which indicates that a SIB byte is
345      // present.
346      BaseRegNo != N86::ESP &&
347      // If there is no base register and we're in 64-bit mode, we need a SIB
348      // byte to emit an addr that is just 'disp32' (the non-RIP relative form).
349      (!is64BitMode() || BaseReg != 0)) {
350
351    if (BaseReg == 0) {          // [disp32]     in X86-32 mode
352      EmitByte(ModRMByte(0, RegOpcodeField, 5), CurByte, OS);
353      EmitImmediate(Disp, MI.getLoc(), 4, FK_Data_4, CurByte, OS, Fixups);
354      return;
355    }
356
357    // If the base is not EBP/ESP and there is no displacement, use simple
358    // indirect register encoding, this handles addresses like [EAX].  The
359    // encoding for [EBP] with no displacement means [disp32] so we handle it
360    // by emitting a displacement of 0 below.
361    if (Disp.isImm() && Disp.getImm() == 0 && BaseRegNo != N86::EBP) {
362      EmitByte(ModRMByte(0, RegOpcodeField, BaseRegNo), CurByte, OS);
363      return;
364    }
365
366    // Otherwise, if the displacement fits in a byte, encode as [REG+disp8].
367    if (Disp.isImm() && isDisp8(Disp.getImm())) {
368      EmitByte(ModRMByte(1, RegOpcodeField, BaseRegNo), CurByte, OS);
369      EmitImmediate(Disp, MI.getLoc(), 1, FK_Data_1, CurByte, OS, Fixups);
370      return;
371    }
372
373    // Otherwise, emit the most general non-SIB encoding: [REG+disp32]
374    EmitByte(ModRMByte(2, RegOpcodeField, BaseRegNo), CurByte, OS);
375    EmitImmediate(Disp, MI.getLoc(), 4, MCFixupKind(X86::reloc_signed_4byte), CurByte, OS,
376                  Fixups);
377    return;
378  }
379
380  // We need a SIB byte, so start by outputting the ModR/M byte first
381  assert(IndexReg.getReg() != X86::ESP &&
382         IndexReg.getReg() != X86::RSP && "Cannot use ESP as index reg!");
383
384  bool ForceDisp32 = false;
385  bool ForceDisp8  = false;
386  if (BaseReg == 0) {
387    // If there is no base register, we emit the special case SIB byte with
388    // MOD=0, BASE=5, to JUST get the index, scale, and displacement.
389    EmitByte(ModRMByte(0, RegOpcodeField, 4), CurByte, OS);
390    ForceDisp32 = true;
391  } else if (!Disp.isImm()) {
392    // Emit the normal disp32 encoding.
393    EmitByte(ModRMByte(2, RegOpcodeField, 4), CurByte, OS);
394    ForceDisp32 = true;
395  } else if (Disp.getImm() == 0 &&
396             // Base reg can't be anything that ends up with '5' as the base
397             // reg, it is the magic [*] nomenclature that indicates no base.
398             BaseRegNo != N86::EBP) {
399    // Emit no displacement ModR/M byte
400    EmitByte(ModRMByte(0, RegOpcodeField, 4), CurByte, OS);
401  } else if (isDisp8(Disp.getImm())) {
402    // Emit the disp8 encoding.
403    EmitByte(ModRMByte(1, RegOpcodeField, 4), CurByte, OS);
404    ForceDisp8 = true;           // Make sure to force 8 bit disp if Base=EBP
405  } else {
406    // Emit the normal disp32 encoding.
407    EmitByte(ModRMByte(2, RegOpcodeField, 4), CurByte, OS);
408  }
409
410  // Calculate what the SS field value should be...
411  static const unsigned SSTable[] = { ~0U, 0, 1, ~0U, 2, ~0U, ~0U, ~0U, 3 };
412  unsigned SS = SSTable[Scale.getImm()];
413
414  if (BaseReg == 0) {
415    // Handle the SIB byte for the case where there is no base, see Intel
416    // Manual 2A, table 2-7. The displacement has already been output.
417    unsigned IndexRegNo;
418    if (IndexReg.getReg())
419      IndexRegNo = GetX86RegNum(IndexReg);
420    else // Examples: [ESP+1*<noreg>+4] or [scaled idx]+disp32 (MOD=0,BASE=5)
421      IndexRegNo = 4;
422    EmitSIBByte(SS, IndexRegNo, 5, CurByte, OS);
423  } else {
424    unsigned IndexRegNo;
425    if (IndexReg.getReg())
426      IndexRegNo = GetX86RegNum(IndexReg);
427    else
428      IndexRegNo = 4;   // For example [ESP+1*<noreg>+4]
429    EmitSIBByte(SS, IndexRegNo, GetX86RegNum(Base), CurByte, OS);
430  }
431
432  // Do we need to output a displacement?
433  if (ForceDisp8)
434    EmitImmediate(Disp, MI.getLoc(), 1, FK_Data_1, CurByte, OS, Fixups);
435  else if (ForceDisp32 || Disp.getImm() != 0)
436    EmitImmediate(Disp, MI.getLoc(), 4, MCFixupKind(X86::reloc_signed_4byte),
437                  CurByte, OS, Fixups);
438}
439
440/// EmitVEXOpcodePrefix - AVX instructions are encoded using a opcode prefix
441/// called VEX.
442void X86MCCodeEmitter::EmitVEXOpcodePrefix(uint64_t TSFlags, unsigned &CurByte,
443                                           int MemOperand, const MCInst &MI,
444                                           const MCInstrDesc &Desc,
445                                           raw_ostream &OS) const {
446  bool HasVEX_4V = (TSFlags >> X86II::VEXShift) & X86II::VEX_4V;
447  bool HasVEX_4VOp3 = (TSFlags >> X86II::VEXShift) & X86II::VEX_4VOp3;
448
449  // VEX_R: opcode externsion equivalent to REX.R in
450  // 1's complement (inverted) form
451  //
452  //  1: Same as REX_R=0 (must be 1 in 32-bit mode)
453  //  0: Same as REX_R=1 (64 bit mode only)
454  //
455  unsigned char VEX_R = 0x1;
456
457  // VEX_X: equivalent to REX.X, only used when a
458  // register is used for index in SIB Byte.
459  //
460  //  1: Same as REX.X=0 (must be 1 in 32-bit mode)
461  //  0: Same as REX.X=1 (64-bit mode only)
462  unsigned char VEX_X = 0x1;
463
464  // VEX_B:
465  //
466  //  1: Same as REX_B=0 (ignored in 32-bit mode)
467  //  0: Same as REX_B=1 (64 bit mode only)
468  //
469  unsigned char VEX_B = 0x1;
470
471  // VEX_W: opcode specific (use like REX.W, or used for
472  // opcode extension, or ignored, depending on the opcode byte)
473  unsigned char VEX_W = 0;
474
475  // XOP: Use XOP prefix byte 0x8f instead of VEX.
476  unsigned char XOP = 0;
477
478  // VEX_5M (VEX m-mmmmm field):
479  //
480  //  0b00000: Reserved for future use
481  //  0b00001: implied 0F leading opcode
482  //  0b00010: implied 0F 38 leading opcode bytes
483  //  0b00011: implied 0F 3A leading opcode bytes
484  //  0b00100-0b11111: Reserved for future use
485  //  0b01000: XOP map select - 08h instructions with imm byte
486  //  0b10001: XOP map select - 09h instructions with no imm byte
487  unsigned char VEX_5M = 0x1;
488
489  // VEX_4V (VEX vvvv field): a register specifier
490  // (in 1's complement form) or 1111 if unused.
491  unsigned char VEX_4V = 0xf;
492
493  // VEX_L (Vector Length):
494  //
495  //  0: scalar or 128-bit vector
496  //  1: 256-bit vector
497  //
498  unsigned char VEX_L = 0;
499
500  // VEX_PP: opcode extension providing equivalent
501  // functionality of a SIMD prefix
502  //
503  //  0b00: None
504  //  0b01: 66
505  //  0b10: F3
506  //  0b11: F2
507  //
508  unsigned char VEX_PP = 0;
509
510  // Encode the operand size opcode prefix as needed.
511  if (TSFlags & X86II::OpSize)
512    VEX_PP = 0x01;
513
514  if ((TSFlags >> X86II::VEXShift) & X86II::VEX_W)
515    VEX_W = 1;
516
517  if ((TSFlags >> X86II::VEXShift) & X86II::XOP)
518    XOP = 1;
519
520  if ((TSFlags >> X86II::VEXShift) & X86II::VEX_L)
521    VEX_L = 1;
522
523  switch (TSFlags & X86II::Op0Mask) {
524  default: llvm_unreachable("Invalid prefix!");
525  case X86II::T8:  // 0F 38
526    VEX_5M = 0x2;
527    break;
528  case X86II::TA:  // 0F 3A
529    VEX_5M = 0x3;
530    break;
531  case X86II::T8XS: // F3 0F 38
532    VEX_PP = 0x2;
533    VEX_5M = 0x2;
534    break;
535  case X86II::T8XD: // F2 0F 38
536    VEX_PP = 0x3;
537    VEX_5M = 0x2;
538    break;
539  case X86II::TAXD: // F2 0F 3A
540    VEX_PP = 0x3;
541    VEX_5M = 0x3;
542    break;
543  case X86II::XS:  // F3 0F
544    VEX_PP = 0x2;
545    break;
546  case X86II::XD:  // F2 0F
547    VEX_PP = 0x3;
548    break;
549  case X86II::XOP8:
550    VEX_5M = 0x8;
551    break;
552  case X86II::XOP9:
553    VEX_5M = 0x9;
554    break;
555  case X86II::A6:  // Bypass: Not used by VEX
556  case X86II::A7:  // Bypass: Not used by VEX
557  case X86II::TB:  // Bypass: Not used by VEX
558  case 0:
559    break;  // No prefix!
560  }
561
562
563  // Classify VEX_B, VEX_4V, VEX_R, VEX_X
564  unsigned NumOps = Desc.getNumOperands();
565  unsigned CurOp = 0;
566  if (NumOps > 1 && Desc.getOperandConstraint(1, MCOI::TIED_TO) == 0)
567    ++CurOp;
568  else if (NumOps > 3 && Desc.getOperandConstraint(2, MCOI::TIED_TO) == 0) {
569    assert(Desc.getOperandConstraint(NumOps - 1, MCOI::TIED_TO) == 1);
570    // Special case for GATHER with 2 TIED_TO operands
571    // Skip the first 2 operands: dst, mask_wb
572    CurOp += 2;
573  }
574
575  switch (TSFlags & X86II::FormMask) {
576  case X86II::MRMInitReg: llvm_unreachable("FIXME: Remove this!");
577  case X86II::MRMDestMem: {
578    // MRMDestMem instructions forms:
579    //  MemAddr, src1(ModR/M)
580    //  MemAddr, src1(VEX_4V), src2(ModR/M)
581    //  MemAddr, src1(ModR/M), imm8
582    //
583    if (X86II::isX86_64ExtendedReg(MI.getOperand(X86::AddrBaseReg).getReg()))
584      VEX_B = 0x0;
585    if (X86II::isX86_64ExtendedReg(MI.getOperand(X86::AddrIndexReg).getReg()))
586      VEX_X = 0x0;
587
588    CurOp = X86::AddrNumOperands;
589    if (HasVEX_4V)
590      VEX_4V = getVEXRegisterEncoding(MI, CurOp++);
591
592    const MCOperand &MO = MI.getOperand(CurOp);
593    if (MO.isReg() && X86II::isX86_64ExtendedReg(MO.getReg()))
594      VEX_R = 0x0;
595    break;
596  }
597  case X86II::MRMSrcMem:
598    // MRMSrcMem instructions forms:
599    //  src1(ModR/M), MemAddr
600    //  src1(ModR/M), src2(VEX_4V), MemAddr
601    //  src1(ModR/M), MemAddr, imm8
602    //  src1(ModR/M), MemAddr, src2(VEX_I8IMM)
603    //
604    //  FMA4:
605    //  dst(ModR/M.reg), src1(VEX_4V), src2(ModR/M), src3(VEX_I8IMM)
606    //  dst(ModR/M.reg), src1(VEX_4V), src2(VEX_I8IMM), src3(ModR/M),
607    if (X86II::isX86_64ExtendedReg(MI.getOperand(CurOp++).getReg()))
608      VEX_R = 0x0;
609
610    if (HasVEX_4V)
611      VEX_4V = getVEXRegisterEncoding(MI, CurOp);
612
613    if (X86II::isX86_64ExtendedReg(
614               MI.getOperand(MemOperand+X86::AddrBaseReg).getReg()))
615      VEX_B = 0x0;
616    if (X86II::isX86_64ExtendedReg(
617               MI.getOperand(MemOperand+X86::AddrIndexReg).getReg()))
618      VEX_X = 0x0;
619
620    if (HasVEX_4VOp3)
621      // Instruction format for 4VOp3:
622      //   src1(ModR/M), MemAddr, src3(VEX_4V)
623      // CurOp points to start of the MemoryOperand,
624      //   it skips TIED_TO operands if exist, then increments past src1.
625      // CurOp + X86::AddrNumOperands will point to src3.
626      VEX_4V = getVEXRegisterEncoding(MI, CurOp+X86::AddrNumOperands);
627    break;
628  case X86II::MRM0m: case X86II::MRM1m:
629  case X86II::MRM2m: case X86II::MRM3m:
630  case X86II::MRM4m: case X86II::MRM5m:
631  case X86II::MRM6m: case X86II::MRM7m: {
632    // MRM[0-9]m instructions forms:
633    //  MemAddr
634    //  src1(VEX_4V), MemAddr
635    if (HasVEX_4V)
636      VEX_4V = getVEXRegisterEncoding(MI, 0);
637
638    if (X86II::isX86_64ExtendedReg(
639               MI.getOperand(MemOperand+X86::AddrBaseReg).getReg()))
640      VEX_B = 0x0;
641    if (X86II::isX86_64ExtendedReg(
642               MI.getOperand(MemOperand+X86::AddrIndexReg).getReg()))
643      VEX_X = 0x0;
644    break;
645  }
646  case X86II::MRMSrcReg:
647    // MRMSrcReg instructions forms:
648    //  dst(ModR/M), src1(VEX_4V), src2(ModR/M), src3(VEX_I8IMM)
649    //  dst(ModR/M), src1(ModR/M)
650    //  dst(ModR/M), src1(ModR/M), imm8
651    //
652    if (X86II::isX86_64ExtendedReg(MI.getOperand(CurOp).getReg()))
653      VEX_R = 0x0;
654    CurOp++;
655
656    if (HasVEX_4V)
657      VEX_4V = getVEXRegisterEncoding(MI, CurOp++);
658    if (X86II::isX86_64ExtendedReg(MI.getOperand(CurOp).getReg()))
659      VEX_B = 0x0;
660    CurOp++;
661    if (HasVEX_4VOp3)
662      VEX_4V = getVEXRegisterEncoding(MI, CurOp);
663    break;
664  case X86II::MRMDestReg:
665    // MRMDestReg instructions forms:
666    //  dst(ModR/M), src(ModR/M)
667    //  dst(ModR/M), src(ModR/M), imm8
668    if (X86II::isX86_64ExtendedReg(MI.getOperand(0).getReg()))
669      VEX_B = 0x0;
670    if (X86II::isX86_64ExtendedReg(MI.getOperand(1).getReg()))
671      VEX_R = 0x0;
672    break;
673  case X86II::MRM0r: case X86II::MRM1r:
674  case X86II::MRM2r: case X86II::MRM3r:
675  case X86II::MRM4r: case X86II::MRM5r:
676  case X86II::MRM6r: case X86II::MRM7r:
677    // MRM0r-MRM7r instructions forms:
678    //  dst(VEX_4V), src(ModR/M), imm8
679    VEX_4V = getVEXRegisterEncoding(MI, 0);
680    if (X86II::isX86_64ExtendedReg(MI.getOperand(1).getReg()))
681      VEX_B = 0x0;
682    break;
683  default: // RawFrm
684    break;
685  }
686
687  // Emit segment override opcode prefix as needed.
688  EmitSegmentOverridePrefix(TSFlags, CurByte, MemOperand, MI, OS);
689
690  // VEX opcode prefix can have 2 or 3 bytes
691  //
692  //  3 bytes:
693  //    +-----+ +--------------+ +-------------------+
694  //    | C4h | | RXB | m-mmmm | | W | vvvv | L | pp |
695  //    +-----+ +--------------+ +-------------------+
696  //  2 bytes:
697  //    +-----+ +-------------------+
698  //    | C5h | | R | vvvv | L | pp |
699  //    +-----+ +-------------------+
700  //
701  unsigned char LastByte = VEX_PP | (VEX_L << 2) | (VEX_4V << 3);
702
703  if (VEX_B && VEX_X && !VEX_W && !XOP && (VEX_5M == 1)) { // 2 byte VEX prefix
704    EmitByte(0xC5, CurByte, OS);
705    EmitByte(LastByte | (VEX_R << 7), CurByte, OS);
706    return;
707  }
708
709  // 3 byte VEX prefix
710  EmitByte(XOP ? 0x8F : 0xC4, CurByte, OS);
711  EmitByte(VEX_R << 7 | VEX_X << 6 | VEX_B << 5 | VEX_5M, CurByte, OS);
712  EmitByte(LastByte | (VEX_W << 7), CurByte, OS);
713}
714
715/// DetermineREXPrefix - Determine if the MCInst has to be encoded with a X86-64
716/// REX prefix which specifies 1) 64-bit instructions, 2) non-default operand
717/// size, and 3) use of X86-64 extended registers.
718static unsigned DetermineREXPrefix(const MCInst &MI, uint64_t TSFlags,
719                                   const MCInstrDesc &Desc) {
720  unsigned REX = 0;
721  if (TSFlags & X86II::REX_W)
722    REX |= 1 << 3; // set REX.W
723
724  if (MI.getNumOperands() == 0) return REX;
725
726  unsigned NumOps = MI.getNumOperands();
727  // FIXME: MCInst should explicitize the two-addrness.
728  bool isTwoAddr = NumOps > 1 &&
729                      Desc.getOperandConstraint(1, MCOI::TIED_TO) != -1;
730
731  // If it accesses SPL, BPL, SIL, or DIL, then it requires a 0x40 REX prefix.
732  unsigned i = isTwoAddr ? 1 : 0;
733  for (; i != NumOps; ++i) {
734    const MCOperand &MO = MI.getOperand(i);
735    if (!MO.isReg()) continue;
736    unsigned Reg = MO.getReg();
737    if (!X86II::isX86_64NonExtLowByteReg(Reg)) continue;
738    // FIXME: The caller of DetermineREXPrefix slaps this prefix onto anything
739    // that returns non-zero.
740    REX |= 0x40; // REX fixed encoding prefix
741    break;
742  }
743
744  switch (TSFlags & X86II::FormMask) {
745  case X86II::MRMInitReg: llvm_unreachable("FIXME: Remove this!");
746  case X86II::MRMSrcReg:
747    if (MI.getOperand(0).isReg() &&
748        X86II::isX86_64ExtendedReg(MI.getOperand(0).getReg()))
749      REX |= 1 << 2; // set REX.R
750    i = isTwoAddr ? 2 : 1;
751    for (; i != NumOps; ++i) {
752      const MCOperand &MO = MI.getOperand(i);
753      if (MO.isReg() && X86II::isX86_64ExtendedReg(MO.getReg()))
754        REX |= 1 << 0; // set REX.B
755    }
756    break;
757  case X86II::MRMSrcMem: {
758    if (MI.getOperand(0).isReg() &&
759        X86II::isX86_64ExtendedReg(MI.getOperand(0).getReg()))
760      REX |= 1 << 2; // set REX.R
761    unsigned Bit = 0;
762    i = isTwoAddr ? 2 : 1;
763    for (; i != NumOps; ++i) {
764      const MCOperand &MO = MI.getOperand(i);
765      if (MO.isReg()) {
766        if (X86II::isX86_64ExtendedReg(MO.getReg()))
767          REX |= 1 << Bit; // set REX.B (Bit=0) and REX.X (Bit=1)
768        Bit++;
769      }
770    }
771    break;
772  }
773  case X86II::MRM0m: case X86II::MRM1m:
774  case X86II::MRM2m: case X86II::MRM3m:
775  case X86II::MRM4m: case X86II::MRM5m:
776  case X86II::MRM6m: case X86II::MRM7m:
777  case X86II::MRMDestMem: {
778    unsigned e = (isTwoAddr ? X86::AddrNumOperands+1 : X86::AddrNumOperands);
779    i = isTwoAddr ? 1 : 0;
780    if (NumOps > e && MI.getOperand(e).isReg() &&
781        X86II::isX86_64ExtendedReg(MI.getOperand(e).getReg()))
782      REX |= 1 << 2; // set REX.R
783    unsigned Bit = 0;
784    for (; i != e; ++i) {
785      const MCOperand &MO = MI.getOperand(i);
786      if (MO.isReg()) {
787        if (X86II::isX86_64ExtendedReg(MO.getReg()))
788          REX |= 1 << Bit; // REX.B (Bit=0) and REX.X (Bit=1)
789        Bit++;
790      }
791    }
792    break;
793  }
794  default:
795    if (MI.getOperand(0).isReg() &&
796        X86II::isX86_64ExtendedReg(MI.getOperand(0).getReg()))
797      REX |= 1 << 0; // set REX.B
798    i = isTwoAddr ? 2 : 1;
799    for (unsigned e = NumOps; i != e; ++i) {
800      const MCOperand &MO = MI.getOperand(i);
801      if (MO.isReg() && X86II::isX86_64ExtendedReg(MO.getReg()))
802        REX |= 1 << 2; // set REX.R
803    }
804    break;
805  }
806  return REX;
807}
808
809/// EmitSegmentOverridePrefix - Emit segment override opcode prefix as needed
810void X86MCCodeEmitter::EmitSegmentOverridePrefix(uint64_t TSFlags,
811                                        unsigned &CurByte, int MemOperand,
812                                        const MCInst &MI,
813                                        raw_ostream &OS) const {
814  switch (TSFlags & X86II::SegOvrMask) {
815  default: llvm_unreachable("Invalid segment!");
816  case 0:
817    // No segment override, check for explicit one on memory operand.
818    if (MemOperand != -1) {   // If the instruction has a memory operand.
819      switch (MI.getOperand(MemOperand+X86::AddrSegmentReg).getReg()) {
820      default: llvm_unreachable("Unknown segment register!");
821      case 0: break;
822      case X86::CS: EmitByte(0x2E, CurByte, OS); break;
823      case X86::SS: EmitByte(0x36, CurByte, OS); break;
824      case X86::DS: EmitByte(0x3E, CurByte, OS); break;
825      case X86::ES: EmitByte(0x26, CurByte, OS); break;
826      case X86::FS: EmitByte(0x64, CurByte, OS); break;
827      case X86::GS: EmitByte(0x65, CurByte, OS); break;
828      }
829    }
830    break;
831  case X86II::FS:
832    EmitByte(0x64, CurByte, OS);
833    break;
834  case X86II::GS:
835    EmitByte(0x65, CurByte, OS);
836    break;
837  }
838}
839
840/// EmitOpcodePrefix - Emit all instruction prefixes prior to the opcode.
841///
842/// MemOperand is the operand # of the start of a memory operand if present.  If
843/// Not present, it is -1.
844void X86MCCodeEmitter::EmitOpcodePrefix(uint64_t TSFlags, unsigned &CurByte,
845                                        int MemOperand, const MCInst &MI,
846                                        const MCInstrDesc &Desc,
847                                        raw_ostream &OS) const {
848
849  // Emit the lock opcode prefix as needed.
850  if (TSFlags & X86II::LOCK)
851    EmitByte(0xF0, CurByte, OS);
852
853  // Emit segment override opcode prefix as needed.
854  EmitSegmentOverridePrefix(TSFlags, CurByte, MemOperand, MI, OS);
855
856  // Emit the repeat opcode prefix as needed.
857  if ((TSFlags & X86II::Op0Mask) == X86II::REP)
858    EmitByte(0xF3, CurByte, OS);
859
860  // Emit the address size opcode prefix as needed.
861  bool need_address_override;
862  if (TSFlags & X86II::AdSize) {
863    need_address_override = true;
864  } else if (MemOperand == -1) {
865    need_address_override = false;
866  } else if (is64BitMode()) {
867    assert(!Is16BitMemOperand(MI, MemOperand));
868    need_address_override = Is32BitMemOperand(MI, MemOperand);
869  } else if (is32BitMode()) {
870    assert(!Is64BitMemOperand(MI, MemOperand));
871    need_address_override = Is16BitMemOperand(MI, MemOperand);
872  } else {
873    need_address_override = false;
874  }
875
876  if (need_address_override)
877    EmitByte(0x67, CurByte, OS);
878
879  // Emit the operand size opcode prefix as needed.
880  if (TSFlags & X86II::OpSize)
881    EmitByte(0x66, CurByte, OS);
882
883  bool Need0FPrefix = false;
884  switch (TSFlags & X86II::Op0Mask) {
885  default: llvm_unreachable("Invalid prefix!");
886  case 0: break;  // No prefix!
887  case X86II::REP: break; // already handled.
888  case X86II::TB:  // Two-byte opcode prefix
889  case X86II::T8:  // 0F 38
890  case X86II::TA:  // 0F 3A
891  case X86II::A6:  // 0F A6
892  case X86II::A7:  // 0F A7
893    Need0FPrefix = true;
894    break;
895  case X86II::T8XS: // F3 0F 38
896    EmitByte(0xF3, CurByte, OS);
897    Need0FPrefix = true;
898    break;
899  case X86II::T8XD: // F2 0F 38
900    EmitByte(0xF2, CurByte, OS);
901    Need0FPrefix = true;
902    break;
903  case X86II::TAXD: // F2 0F 3A
904    EmitByte(0xF2, CurByte, OS);
905    Need0FPrefix = true;
906    break;
907  case X86II::XS:   // F3 0F
908    EmitByte(0xF3, CurByte, OS);
909    Need0FPrefix = true;
910    break;
911  case X86II::XD:   // F2 0F
912    EmitByte(0xF2, CurByte, OS);
913    Need0FPrefix = true;
914    break;
915  case X86II::D8: EmitByte(0xD8, CurByte, OS); break;
916  case X86II::D9: EmitByte(0xD9, CurByte, OS); break;
917  case X86II::DA: EmitByte(0xDA, CurByte, OS); break;
918  case X86II::DB: EmitByte(0xDB, CurByte, OS); break;
919  case X86II::DC: EmitByte(0xDC, CurByte, OS); break;
920  case X86II::DD: EmitByte(0xDD, CurByte, OS); break;
921  case X86II::DE: EmitByte(0xDE, CurByte, OS); break;
922  case X86II::DF: EmitByte(0xDF, CurByte, OS); break;
923  }
924
925  // Handle REX prefix.
926  // FIXME: Can this come before F2 etc to simplify emission?
927  if (is64BitMode()) {
928    if (unsigned REX = DetermineREXPrefix(MI, TSFlags, Desc))
929      EmitByte(0x40 | REX, CurByte, OS);
930  }
931
932  // 0x0F escape code must be emitted just before the opcode.
933  if (Need0FPrefix)
934    EmitByte(0x0F, CurByte, OS);
935
936  // FIXME: Pull this up into previous switch if REX can be moved earlier.
937  switch (TSFlags & X86II::Op0Mask) {
938  case X86II::T8XS:  // F3 0F 38
939  case X86II::T8XD:  // F2 0F 38
940  case X86II::T8:    // 0F 38
941    EmitByte(0x38, CurByte, OS);
942    break;
943  case X86II::TAXD:  // F2 0F 3A
944  case X86II::TA:    // 0F 3A
945    EmitByte(0x3A, CurByte, OS);
946    break;
947  case X86II::A6:    // 0F A6
948    EmitByte(0xA6, CurByte, OS);
949    break;
950  case X86II::A7:    // 0F A7
951    EmitByte(0xA7, CurByte, OS);
952    break;
953  }
954}
955
956void X86MCCodeEmitter::
957EncodeInstruction(const MCInst &MI, raw_ostream &OS,
958                  SmallVectorImpl<MCFixup> &Fixups) const {
959  unsigned Opcode = MI.getOpcode();
960  const MCInstrDesc &Desc = MCII.get(Opcode);
961  uint64_t TSFlags = Desc.TSFlags;
962
963  // Pseudo instructions don't get encoded.
964  if ((TSFlags & X86II::FormMask) == X86II::Pseudo)
965    return;
966
967  // If this is a two-address instruction, skip one of the register operands.
968  // FIXME: This should be handled during MCInst lowering.
969  unsigned NumOps = Desc.getNumOperands();
970  unsigned CurOp = 0;
971  if (NumOps > 1 && Desc.getOperandConstraint(1, MCOI::TIED_TO) == 0)
972    ++CurOp;
973  else if (NumOps > 3 && Desc.getOperandConstraint(2, MCOI::TIED_TO) == 0) {
974    assert(Desc.getOperandConstraint(NumOps - 1, MCOI::TIED_TO) == 1);
975    // Special case for GATHER with 2 TIED_TO operands
976    // Skip the first 2 operands: dst, mask_wb
977    CurOp += 2;
978  }
979
980  // Keep track of the current byte being emitted.
981  unsigned CurByte = 0;
982
983  // Is this instruction encoded using the AVX VEX prefix?
984  bool HasVEXPrefix = (TSFlags >> X86II::VEXShift) & X86II::VEX;
985
986  // It uses the VEX.VVVV field?
987  bool HasVEX_4V = (TSFlags >> X86II::VEXShift) & X86II::VEX_4V;
988  bool HasVEX_4VOp3 = (TSFlags >> X86II::VEXShift) & X86II::VEX_4VOp3;
989  bool HasMemOp4 = (TSFlags >> X86II::VEXShift) & X86II::MemOp4;
990  const unsigned MemOp4_I8IMMOperand = 2;
991
992  // Determine where the memory operand starts, if present.
993  int MemoryOperand = X86II::getMemoryOperandNo(TSFlags, Opcode);
994  if (MemoryOperand != -1) MemoryOperand += CurOp;
995
996  if (!HasVEXPrefix)
997    EmitOpcodePrefix(TSFlags, CurByte, MemoryOperand, MI, Desc, OS);
998  else
999    EmitVEXOpcodePrefix(TSFlags, CurByte, MemoryOperand, MI, Desc, OS);
1000
1001  unsigned char BaseOpcode = X86II::getBaseOpcodeFor(TSFlags);
1002
1003  if ((TSFlags >> X86II::VEXShift) & X86II::Has3DNow0F0FOpcode)
1004    BaseOpcode = 0x0F;   // Weird 3DNow! encoding.
1005
1006  unsigned SrcRegNum = 0;
1007  switch (TSFlags & X86II::FormMask) {
1008  case X86II::MRMInitReg:
1009    llvm_unreachable("FIXME: Remove this form when the JIT moves to MCCodeEmitter!");
1010  default: errs() << "FORM: " << (TSFlags & X86II::FormMask) << "\n";
1011    llvm_unreachable("Unknown FormMask value in X86MCCodeEmitter!");
1012  case X86II::Pseudo:
1013    llvm_unreachable("Pseudo instruction shouldn't be emitted");
1014  case X86II::RawFrm:
1015    EmitByte(BaseOpcode, CurByte, OS);
1016    break;
1017  case X86II::RawFrmImm8:
1018    EmitByte(BaseOpcode, CurByte, OS);
1019    EmitImmediate(MI.getOperand(CurOp++), MI.getLoc(),
1020                  X86II::getSizeOfImm(TSFlags), getImmFixupKind(TSFlags),
1021                  CurByte, OS, Fixups);
1022    EmitImmediate(MI.getOperand(CurOp++), MI.getLoc(), 1, FK_Data_1, CurByte,
1023                  OS, Fixups);
1024    break;
1025  case X86II::RawFrmImm16:
1026    EmitByte(BaseOpcode, CurByte, OS);
1027    EmitImmediate(MI.getOperand(CurOp++), MI.getLoc(),
1028                  X86II::getSizeOfImm(TSFlags), getImmFixupKind(TSFlags),
1029                  CurByte, OS, Fixups);
1030    EmitImmediate(MI.getOperand(CurOp++), MI.getLoc(), 2, FK_Data_2, CurByte,
1031                  OS, Fixups);
1032    break;
1033
1034  case X86II::AddRegFrm:
1035    EmitByte(BaseOpcode + GetX86RegNum(MI.getOperand(CurOp++)), CurByte, OS);
1036    break;
1037
1038  case X86II::MRMDestReg:
1039    EmitByte(BaseOpcode, CurByte, OS);
1040    EmitRegModRMByte(MI.getOperand(CurOp),
1041                     GetX86RegNum(MI.getOperand(CurOp+1)), CurByte, OS);
1042    CurOp += 2;
1043    break;
1044
1045  case X86II::MRMDestMem:
1046    EmitByte(BaseOpcode, CurByte, OS);
1047    SrcRegNum = CurOp + X86::AddrNumOperands;
1048
1049    if (HasVEX_4V) // Skip 1st src (which is encoded in VEX_VVVV)
1050      ++SrcRegNum;
1051
1052    EmitMemModRMByte(MI, CurOp,
1053                     GetX86RegNum(MI.getOperand(SrcRegNum)),
1054                     TSFlags, CurByte, OS, Fixups);
1055    CurOp = SrcRegNum + 1;
1056    break;
1057
1058  case X86II::MRMSrcReg:
1059    EmitByte(BaseOpcode, CurByte, OS);
1060    SrcRegNum = CurOp + 1;
1061
1062    if (HasVEX_4V) // Skip 1st src (which is encoded in VEX_VVVV)
1063      ++SrcRegNum;
1064
1065    if (HasMemOp4) // Skip 2nd src (which is encoded in I8IMM)
1066      ++SrcRegNum;
1067
1068    EmitRegModRMByte(MI.getOperand(SrcRegNum),
1069                     GetX86RegNum(MI.getOperand(CurOp)), CurByte, OS);
1070
1071    // 2 operands skipped with HasMemOp4, compensate accordingly
1072    CurOp = HasMemOp4 ? SrcRegNum : SrcRegNum + 1;
1073    if (HasVEX_4VOp3)
1074      ++CurOp;
1075    break;
1076
1077  case X86II::MRMSrcMem: {
1078    int AddrOperands = X86::AddrNumOperands;
1079    unsigned FirstMemOp = CurOp+1;
1080    if (HasVEX_4V) {
1081      ++AddrOperands;
1082      ++FirstMemOp;  // Skip the register source (which is encoded in VEX_VVVV).
1083    }
1084    if (HasMemOp4) // Skip second register source (encoded in I8IMM)
1085      ++FirstMemOp;
1086
1087    EmitByte(BaseOpcode, CurByte, OS);
1088
1089    EmitMemModRMByte(MI, FirstMemOp, GetX86RegNum(MI.getOperand(CurOp)),
1090                     TSFlags, CurByte, OS, Fixups);
1091    CurOp += AddrOperands + 1;
1092    if (HasVEX_4VOp3)
1093      ++CurOp;
1094    break;
1095  }
1096
1097  case X86II::MRM0r: case X86II::MRM1r:
1098  case X86II::MRM2r: case X86II::MRM3r:
1099  case X86II::MRM4r: case X86II::MRM5r:
1100  case X86II::MRM6r: case X86II::MRM7r:
1101    if (HasVEX_4V) // Skip the register dst (which is encoded in VEX_VVVV).
1102      ++CurOp;
1103    EmitByte(BaseOpcode, CurByte, OS);
1104    EmitRegModRMByte(MI.getOperand(CurOp++),
1105                     (TSFlags & X86II::FormMask)-X86II::MRM0r,
1106                     CurByte, OS);
1107    break;
1108  case X86II::MRM0m: case X86II::MRM1m:
1109  case X86II::MRM2m: case X86II::MRM3m:
1110  case X86II::MRM4m: case X86II::MRM5m:
1111  case X86II::MRM6m: case X86II::MRM7m:
1112    if (HasVEX_4V) // Skip the register dst (which is encoded in VEX_VVVV).
1113      ++CurOp;
1114    EmitByte(BaseOpcode, CurByte, OS);
1115    EmitMemModRMByte(MI, CurOp, (TSFlags & X86II::FormMask)-X86II::MRM0m,
1116                     TSFlags, CurByte, OS, Fixups);
1117    CurOp += X86::AddrNumOperands;
1118    break;
1119  case X86II::MRM_C1: case X86II::MRM_C2:
1120  case X86II::MRM_C3: case X86II::MRM_C4:
1121  case X86II::MRM_C8: case X86II::MRM_C9:
1122  case X86II::MRM_D0: case X86II::MRM_D1:
1123  case X86II::MRM_D4: case X86II::MRM_D8:
1124  case X86II::MRM_D9: case X86II::MRM_DA:
1125  case X86II::MRM_DB: case X86II::MRM_DC:
1126  case X86II::MRM_DD: case X86II::MRM_DE:
1127  case X86II::MRM_DF: case X86II::MRM_E8:
1128  case X86II::MRM_F0: case X86II::MRM_F8:
1129  case X86II::MRM_F9:
1130    EmitByte(BaseOpcode, CurByte, OS);
1131
1132    unsigned char MRM;
1133    switch (TSFlags & X86II::FormMask) {
1134    default: llvm_unreachable("Invalid Form");
1135    case X86II::MRM_C1: MRM = 0xC1; break;
1136    case X86II::MRM_C2: MRM = 0xC2; break;
1137    case X86II::MRM_C3: MRM = 0xC3; break;
1138    case X86II::MRM_C4: MRM = 0xC4; break;
1139    case X86II::MRM_C8: MRM = 0xC8; break;
1140    case X86II::MRM_C9: MRM = 0xC9; break;
1141    case X86II::MRM_D0: MRM = 0xD0; break;
1142    case X86II::MRM_D1: MRM = 0xD1; break;
1143    case X86II::MRM_D4: MRM = 0xD4; break;
1144    case X86II::MRM_D8: MRM = 0xD8; break;
1145    case X86II::MRM_D9: MRM = 0xD9; break;
1146    case X86II::MRM_DA: MRM = 0xDA; break;
1147    case X86II::MRM_DB: MRM = 0xDB; break;
1148    case X86II::MRM_DC: MRM = 0xDC; break;
1149    case X86II::MRM_DD: MRM = 0xDD; break;
1150    case X86II::MRM_DE: MRM = 0xDE; break;
1151    case X86II::MRM_DF: MRM = 0xDF; break;
1152    case X86II::MRM_E8: MRM = 0xE8; break;
1153    case X86II::MRM_F0: MRM = 0xF0; break;
1154    case X86II::MRM_F8: MRM = 0xF8; break;
1155    case X86II::MRM_F9: MRM = 0xF9; break;
1156    }
1157    EmitByte(MRM, CurByte, OS);
1158    break;
1159  }
1160
1161  // If there is a remaining operand, it must be a trailing immediate.  Emit it
1162  // according to the right size for the instruction. Some instructions
1163  // (SSE4a extrq and insertq) have two trailing immediates.
1164  while (CurOp != NumOps && NumOps - CurOp <= 2) {
1165    // The last source register of a 4 operand instruction in AVX is encoded
1166    // in bits[7:4] of a immediate byte.
1167    if ((TSFlags >> X86II::VEXShift) & X86II::VEX_I8IMM) {
1168      const MCOperand &MO = MI.getOperand(HasMemOp4 ? MemOp4_I8IMMOperand
1169                                                    : CurOp);
1170      ++CurOp;
1171      unsigned RegNum = GetX86RegNum(MO) << 4;
1172      if (X86II::isX86_64ExtendedReg(MO.getReg()))
1173        RegNum |= 1 << 7;
1174      // If there is an additional 5th operand it must be an immediate, which
1175      // is encoded in bits[3:0]
1176      if (CurOp != NumOps) {
1177        const MCOperand &MIMM = MI.getOperand(CurOp++);
1178        if (MIMM.isImm()) {
1179          unsigned Val = MIMM.getImm();
1180          assert(Val < 16 && "Immediate operand value out of range");
1181          RegNum |= Val;
1182        }
1183      }
1184      EmitImmediate(MCOperand::CreateImm(RegNum), MI.getLoc(), 1, FK_Data_1,
1185                    CurByte, OS, Fixups);
1186    } else {
1187      unsigned FixupKind;
1188      // FIXME: Is there a better way to know that we need a signed relocation?
1189      if (MI.getOpcode() == X86::ADD64ri32 ||
1190          MI.getOpcode() == X86::MOV64ri32 ||
1191          MI.getOpcode() == X86::MOV64mi32 ||
1192          MI.getOpcode() == X86::PUSH64i32)
1193        FixupKind = X86::reloc_signed_4byte;
1194      else
1195        FixupKind = getImmFixupKind(TSFlags);
1196      EmitImmediate(MI.getOperand(CurOp++), MI.getLoc(),
1197                    X86II::getSizeOfImm(TSFlags), MCFixupKind(FixupKind),
1198                    CurByte, OS, Fixups);
1199    }
1200  }
1201
1202  if ((TSFlags >> X86II::VEXShift) & X86II::Has3DNow0F0FOpcode)
1203    EmitByte(X86II::getBaseOpcodeFor(TSFlags), CurByte, OS);
1204
1205#ifndef NDEBUG
1206  // FIXME: Verify.
1207  if (/*!Desc.isVariadic() &&*/ CurOp != NumOps) {
1208    errs() << "Cannot encode all operands of: ";
1209    MI.dump();
1210    errs() << '\n';
1211    abort();
1212  }
1213#endif
1214}
1215