port encoder enhancements over to the new encoder.
[oota-llvm.git] / lib / Target / X86 / X86MCCodeEmitter.cpp
1 //===-- X86/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 "x86-emitter"
15 #include "X86.h"
16 #include "X86InstrInfo.h"
17 #include "llvm/MC/MCCodeEmitter.h"
18 #include "llvm/MC/MCInst.h"
19 #include "llvm/Support/raw_ostream.h"
20 using namespace llvm;
21
22 namespace {
23 class X86MCCodeEmitter : public MCCodeEmitter {
24   X86MCCodeEmitter(const X86MCCodeEmitter &); // DO NOT IMPLEMENT
25   void operator=(const X86MCCodeEmitter &); // DO NOT IMPLEMENT
26   const TargetMachine &TM;
27   const TargetInstrInfo &TII;
28   bool Is64BitMode;
29 public:
30   X86MCCodeEmitter(TargetMachine &tm, bool is64Bit) 
31     : TM(tm), TII(*TM.getInstrInfo()) {
32     Is64BitMode = is64Bit;
33   }
34
35   ~X86MCCodeEmitter() {}
36   
37   static unsigned GetX86RegNum(const MCOperand &MO) {
38     return X86RegisterInfo::getX86RegNum(MO.getReg());
39   }
40   
41   void EmitByte(unsigned char C, raw_ostream &OS) const {
42     OS << (char)C;
43   }
44   
45   void EmitConstant(uint64_t Val, unsigned Size, raw_ostream &OS) const {
46     // Output the constant in little endian byte order.
47     for (unsigned i = 0; i != Size; ++i) {
48       EmitByte(Val & 255, OS);
49       Val >>= 8;
50     }
51   }
52
53   void EmitDisplacementField(const MCOperand *RelocOp, int DispVal,
54                              int64_t Adj, bool IsPCRel, raw_ostream &OS) const;
55   
56   inline static unsigned char ModRMByte(unsigned Mod, unsigned RegOpcode,
57                                         unsigned RM) {
58     assert(Mod < 4 && RegOpcode < 8 && RM < 8 && "ModRM Fields out of range!");
59     return RM | (RegOpcode << 3) | (Mod << 6);
60   }
61   
62   void EmitRegModRMByte(const MCOperand &ModRMReg, unsigned RegOpcodeFld,
63                         raw_ostream &OS) const {
64     EmitByte(ModRMByte(3, RegOpcodeFld, GetX86RegNum(ModRMReg)), OS);
65   }
66   
67   void EmitSIBByte(unsigned SS, unsigned Index, unsigned Base,
68                    raw_ostream &OS) const {
69     // SIB byte is in the same format as the ModRMByte...
70     EmitByte(ModRMByte(SS, Index, Base), OS);
71   }
72   
73   
74   void EmitMemModRMByte(const MCInst &MI, unsigned Op,
75                         unsigned RegOpcodeField, intptr_t PCAdj,
76                         raw_ostream &OS) const;
77   
78   void EncodeInstruction(const MCInst &MI, raw_ostream &OS) const;
79   
80 };
81
82 } // end anonymous namespace
83
84
85 MCCodeEmitter *llvm::createX86_32MCCodeEmitter(const Target &,
86                                                TargetMachine &TM) {
87   return new X86MCCodeEmitter(TM, false);
88 }
89
90 MCCodeEmitter *llvm::createX86_64MCCodeEmitter(const Target &,
91                                                TargetMachine &TM) {
92   return new X86MCCodeEmitter(TM, true);
93 }
94
95
96 /// isDisp8 - Return true if this signed displacement fits in a 8-bit 
97 /// sign-extended field. 
98 static bool isDisp8(int Value) {
99   return Value == (signed char)Value;
100 }
101
102 void X86MCCodeEmitter::
103 EmitDisplacementField(const MCOperand *RelocOp, int DispVal,
104                       int64_t Adj, bool IsPCRel, raw_ostream &OS) const {
105   // If this is a simple integer displacement that doesn't require a relocation,
106   // emit it now.
107   if (!RelocOp) {
108     EmitConstant(DispVal, 4, OS);
109     return;
110   }
111   
112   assert(0 && "Reloc not handled yet");
113 #if 0
114   // Otherwise, this is something that requires a relocation.  Emit it as such
115   // now.
116   unsigned RelocType = Is64BitMode ?
117   (IsPCRel ? X86::reloc_pcrel_word : X86::reloc_absolute_word_sext)
118   : (IsPIC ? X86::reloc_picrel_word : X86::reloc_absolute_word);
119   if (RelocOp->isGlobal()) {
120     // In 64-bit static small code model, we could potentially emit absolute.
121     // But it's probably not beneficial. If the MCE supports using RIP directly
122     // do it, otherwise fallback to absolute (this is determined by IsPCRel). 
123     //  89 05 00 00 00 00     mov    %eax,0(%rip)  # PC-relative
124     //  89 04 25 00 00 00 00  mov    %eax,0x0      # Absolute
125     bool Indirect = gvNeedsNonLazyPtr(*RelocOp, TM);
126     emitGlobalAddress(RelocOp->getGlobal(), RelocType, RelocOp->getOffset(),
127                       Adj, Indirect);
128   } else if (RelocOp->isSymbol()) {
129     emitExternalSymbolAddress(RelocOp->getSymbolName(), RelocType);
130   } else if (RelocOp->isCPI()) {
131     emitConstPoolAddress(RelocOp->getIndex(), RelocType,
132                          RelocOp->getOffset(), Adj);
133   } else {
134     assert(RelocOp->isJTI() && "Unexpected machine operand!");
135     emitJumpTableAddress(RelocOp->getIndex(), RelocType, Adj);
136   }
137 #endif
138 }
139
140
141 void X86MCCodeEmitter::EmitMemModRMByte(const MCInst &MI, unsigned Op,
142                                         unsigned RegOpcodeField,
143                                         intptr_t PCAdj,
144                                         raw_ostream &OS) const {
145   const MCOperand &Op3 = MI.getOperand(Op+3);
146   int DispVal = 0;
147   const MCOperand *DispForReloc = 0;
148   
149   // Figure out what sort of displacement we have to handle here.
150   if (Op3.isImm()) {
151     DispVal = Op3.getImm();
152   } else {
153     assert(0 && "relocatable operand");
154 #if 0
155   if (Op3.isGlobal()) {
156     DispForReloc = &Op3;
157   } else if (Op3.isSymbol()) {
158     DispForReloc = &Op3;
159   } else if (Op3.isCPI()) {
160     if (!MCE.earlyResolveAddresses() || Is64BitMode || IsPIC) {
161       DispForReloc = &Op3;
162     } else {
163       DispVal += MCE.getConstantPoolEntryAddress(Op3.getIndex());
164       DispVal += Op3.getOffset();
165     }
166   } else {
167     assert(Op3.isJTI());
168     if (!MCE.earlyResolveAddresses() || Is64BitMode || IsPIC) {
169       DispForReloc = &Op3;
170     } else {
171       DispVal += MCE.getJumpTableEntryAddress(Op3.getIndex());
172     }
173 #endif
174   }
175   
176   const MCOperand &Base     = MI.getOperand(Op);
177   const MCOperand &Scale    = MI.getOperand(Op+1);
178   const MCOperand &IndexReg = MI.getOperand(Op+2);
179   unsigned BaseReg = Base.getReg();
180
181   // FIXME: Eliminate!
182   bool IsPCRel = false;
183     
184   // Determine whether a SIB byte is needed.
185   // If no BaseReg, issue a RIP relative instruction only if the MCE can 
186   // resolve addresses on-the-fly, otherwise use SIB (Intel Manual 2A, table
187   // 2-7) and absolute references.
188   if (// The SIB byte must be used if there is an index register.
189       IndexReg.getReg() == 0 && 
190       // The SIB byte must be used if the base is ESP/RSP.
191       BaseReg != X86::ESP && BaseReg != X86::RSP &&
192       // If there is no base register and we're in 64-bit mode, we need a SIB
193       // byte to emit an addr that is just 'disp32' (the non-RIP relative form).
194       (!Is64BitMode || BaseReg != 0)) {
195
196     if (BaseReg == 0 ||          // [disp32]     in X86-32 mode
197         BaseReg == X86::RIP) {   // [disp32+RIP] in X86-64 mode
198       EmitByte(ModRMByte(0, RegOpcodeField, 5), OS);
199       EmitDisplacementField(DispForReloc, DispVal, PCAdj, true, OS);
200       return;
201     }
202     
203     unsigned BaseRegNo = GetX86RegNum(Base);
204
205     // If the base is not EBP/ESP and there is no displacement, use simple
206     // indirect register encoding, this handles addresses like [EAX].  The
207     // encoding for [EBP] with no displacement means [disp32] so we handle it
208     // by emitting a displacement of 0 below.
209     if (!DispForReloc && DispVal == 0 && BaseRegNo != N86::EBP) {
210       EmitByte(ModRMByte(0, RegOpcodeField, BaseRegNo), OS);
211       return;
212     }
213     
214     // Otherwise, if the displacement fits in a byte, encode as [REG+disp8].
215     if (!DispForReloc && isDisp8(DispVal)) {
216       EmitByte(ModRMByte(1, RegOpcodeField, BaseRegNo), OS);
217       EmitConstant(DispVal, 1, OS);
218       return;
219     }
220     
221     // Otherwise, emit the most general non-SIB encoding: [REG+disp32]
222     EmitByte(ModRMByte(2, RegOpcodeField, BaseRegNo), OS);
223     EmitDisplacementField(DispForReloc, DispVal, PCAdj, IsPCRel, OS);
224     return;
225   }
226     
227   // We need a SIB byte, so start by outputting the ModR/M byte first
228   assert(IndexReg.getReg() != X86::ESP &&
229          IndexReg.getReg() != X86::RSP && "Cannot use ESP as index reg!");
230   
231   bool ForceDisp32 = false;
232   bool ForceDisp8  = false;
233   if (BaseReg == 0) {
234     // If there is no base register, we emit the special case SIB byte with
235     // MOD=0, BASE=5, to JUST get the index, scale, and displacement.
236     EmitByte(ModRMByte(0, RegOpcodeField, 4), OS);
237     ForceDisp32 = true;
238   } else if (DispForReloc) {
239     // Emit the normal disp32 encoding.
240     EmitByte(ModRMByte(2, RegOpcodeField, 4), OS);
241     ForceDisp32 = true;
242   } else if (DispVal == 0 && BaseReg != X86::EBP) {
243     // Emit no displacement ModR/M byte
244     EmitByte(ModRMByte(0, RegOpcodeField, 4), OS);
245   } else if (isDisp8(DispVal)) {
246     // Emit the disp8 encoding.
247     EmitByte(ModRMByte(1, RegOpcodeField, 4), OS);
248     ForceDisp8 = true;           // Make sure to force 8 bit disp if Base=EBP
249   } else {
250     // Emit the normal disp32 encoding.
251     EmitByte(ModRMByte(2, RegOpcodeField, 4), OS);
252   }
253   
254   // Calculate what the SS field value should be...
255   static const unsigned SSTable[] = { ~0, 0, 1, ~0, 2, ~0, ~0, ~0, 3 };
256   unsigned SS = SSTable[Scale.getImm()];
257   
258   if (BaseReg == 0) {
259     // Handle the SIB byte for the case where there is no base, see Intel 
260     // Manual 2A, table 2-7. The displacement has already been output.
261     unsigned IndexRegNo;
262     if (IndexReg.getReg())
263       IndexRegNo = GetX86RegNum(IndexReg);
264     else // Examples: [ESP+1*<noreg>+4] or [scaled idx]+disp32 (MOD=0,BASE=5)
265       IndexRegNo = 4;
266     EmitSIBByte(SS, IndexRegNo, 5, OS);
267   } else {
268     unsigned IndexRegNo;
269     if (IndexReg.getReg())
270       IndexRegNo = GetX86RegNum(IndexReg);
271     else
272       IndexRegNo = 4;   // For example [ESP+1*<noreg>+4]
273     EmitSIBByte(SS, IndexRegNo, GetX86RegNum(Base), OS);
274   }
275   
276   // Do we need to output a displacement?
277   if (ForceDisp8)
278     EmitConstant(DispVal, 1, OS);
279   else if (DispVal != 0 || ForceDisp32)
280     EmitDisplacementField(DispForReloc, DispVal, PCAdj, IsPCRel, OS);
281 }
282
283 /// DetermineREXPrefix - Determine if the MCInst has to be encoded with a X86-64
284 /// REX prefix which specifies 1) 64-bit instructions, 2) non-default operand
285 /// size, and 3) use of X86-64 extended registers.
286 static unsigned DetermineREXPrefix(const MCInst &MI, unsigned TSFlags,
287                                    const TargetInstrDesc &Desc) {
288   unsigned REX = 0;
289   
290   // Pseudo instructions do not need REX prefix byte.
291   if ((TSFlags & X86II::FormMask) == X86II::Pseudo)
292     return 0;
293   if (TSFlags & X86II::REX_W)
294     REX |= 1 << 3;
295   
296   if (MI.getNumOperands() == 0) return REX;
297   
298   unsigned NumOps = MI.getNumOperands();
299   // FIXME: MCInst should explicitize the two-addrness.
300   bool isTwoAddr = NumOps > 1 &&
301                       Desc.getOperandConstraint(1, TOI::TIED_TO) != -1;
302   
303   // If it accesses SPL, BPL, SIL, or DIL, then it requires a 0x40 REX prefix.
304   unsigned i = isTwoAddr ? 1 : 0;
305   for (; i != NumOps; ++i) {
306     const MCOperand &MO = MI.getOperand(i);
307     if (!MO.isReg()) continue;
308     unsigned Reg = MO.getReg();
309     if (!X86InstrInfo::isX86_64NonExtLowByteReg(Reg)) continue;
310     // FIXME: The caller of DetermineREXPrefix slaps this prefix onto anything
311     // that returns non-zero.
312     REX |= 0x40;
313     break;
314   }
315   
316   switch (TSFlags & X86II::FormMask) {
317   case X86II::MRMInitReg: assert(0 && "FIXME: Remove this!");
318   case X86II::MRMSrcReg:
319     if (MI.getOperand(0).isReg() &&
320         X86InstrInfo::isX86_64ExtendedReg(MI.getOperand(0).getReg()))
321       REX |= 1 << 2;
322     i = isTwoAddr ? 2 : 1;
323     for (; i != NumOps; ++i) {
324       const MCOperand &MO = MI.getOperand(i);
325       if (MO.isReg() && X86InstrInfo::isX86_64ExtendedReg(MO.getReg()))
326         REX |= 1 << 0;
327     }
328     break;
329   case X86II::MRMSrcMem: {
330     if (MI.getOperand(0).isReg() &&
331         X86InstrInfo::isX86_64ExtendedReg(MI.getOperand(0).getReg()))
332       REX |= 1 << 2;
333     unsigned Bit = 0;
334     i = isTwoAddr ? 2 : 1;
335     for (; i != NumOps; ++i) {
336       const MCOperand &MO = MI.getOperand(i);
337       if (MO.isReg()) {
338         if (X86InstrInfo::isX86_64ExtendedReg(MO.getReg()))
339           REX |= 1 << Bit;
340         Bit++;
341       }
342     }
343     break;
344   }
345   case X86II::MRM0m: case X86II::MRM1m:
346   case X86II::MRM2m: case X86II::MRM3m:
347   case X86II::MRM4m: case X86II::MRM5m:
348   case X86II::MRM6m: case X86II::MRM7m:
349   case X86II::MRMDestMem: {
350     unsigned e = (isTwoAddr ? X86AddrNumOperands+1 : X86AddrNumOperands);
351     i = isTwoAddr ? 1 : 0;
352     if (NumOps > e && MI.getOperand(e).isReg() &&
353         X86InstrInfo::isX86_64ExtendedReg(MI.getOperand(e).getReg()))
354       REX |= 1 << 2;
355     unsigned Bit = 0;
356     for (; i != e; ++i) {
357       const MCOperand &MO = MI.getOperand(i);
358       if (MO.isReg()) {
359         if (X86InstrInfo::isX86_64ExtendedReg(MO.getReg()))
360           REX |= 1 << Bit;
361         Bit++;
362       }
363     }
364     break;
365   }
366   default:
367     if (MI.getOperand(0).isReg() &&
368         X86InstrInfo::isX86_64ExtendedReg(MI.getOperand(0).getReg()))
369       REX |= 1 << 0;
370     i = isTwoAddr ? 2 : 1;
371     for (unsigned e = NumOps; i != e; ++i) {
372       const MCOperand &MO = MI.getOperand(i);
373       if (MO.isReg() && X86InstrInfo::isX86_64ExtendedReg(MO.getReg()))
374         REX |= 1 << 2;
375     }
376     break;
377   }
378   return REX;
379 }
380
381 void X86MCCodeEmitter::
382 EncodeInstruction(const MCInst &MI, raw_ostream &OS) const {
383   unsigned Opcode = MI.getOpcode();
384   const TargetInstrDesc &Desc = TII.get(Opcode);
385   unsigned TSFlags = Desc.TSFlags;
386
387   // FIXME: We should emit the prefixes in exactly the same order as GAS does,
388   // in order to provide diffability.
389
390   // Emit the lock opcode prefix as needed.
391   if (TSFlags & X86II::LOCK)
392     EmitByte(0xF0, OS);
393   
394   // Emit segment override opcode prefix as needed.
395   switch (TSFlags & X86II::SegOvrMask) {
396   default: assert(0 && "Invalid segment!");
397   case 0: break;  // No segment override!
398   case X86II::FS:
399     EmitByte(0x64, OS);
400     break;
401   case X86II::GS:
402     EmitByte(0x65, OS);
403     break;
404   }
405   
406   // Emit the repeat opcode prefix as needed.
407   if ((TSFlags & X86II::Op0Mask) == X86II::REP)
408     EmitByte(0xF3, OS);
409   
410   // Emit the operand size opcode prefix as needed.
411   if (TSFlags & X86II::OpSize)
412     EmitByte(0x66, OS);
413   
414   // Emit the address size opcode prefix as needed.
415   if (TSFlags & X86II::AdSize)
416     EmitByte(0x67, OS);
417   
418   bool Need0FPrefix = false;
419   switch (TSFlags & X86II::Op0Mask) {
420   default: assert(0 && "Invalid prefix!");
421   case 0: break;  // No prefix!
422   case X86II::REP: break; // already handled.
423   case X86II::TB:  // Two-byte opcode prefix
424   case X86II::T8:  // 0F 38
425   case X86II::TA:  // 0F 3A
426     Need0FPrefix = true;
427     break;
428   case X86II::TF: // F2 0F 38
429     EmitByte(0xF2, OS);
430     Need0FPrefix = true;
431     break;
432   case X86II::XS:   // F3 0F
433     EmitByte(0xF3, OS);
434     Need0FPrefix = true;
435     break;
436   case X86II::XD:   // F2 0F
437     EmitByte(0xF2, OS);
438     Need0FPrefix = true;
439     break;
440   case X86II::D8: EmitByte(0xD8, OS); break;
441   case X86II::D9: EmitByte(0xD9, OS); break;
442   case X86II::DA: EmitByte(0xDA, OS); break;
443   case X86II::DB: EmitByte(0xDB, OS); break;
444   case X86II::DC: EmitByte(0xDC, OS); break;
445   case X86II::DD: EmitByte(0xDD, OS); break;
446   case X86II::DE: EmitByte(0xDE, OS); break;
447   case X86II::DF: EmitByte(0xDF, OS); break;
448   }
449   
450   // Handle REX prefix.
451   // FIXME: Can this come before F2 etc to simplify emission?
452   if (Is64BitMode) {
453     if (unsigned REX = DetermineREXPrefix(MI, TSFlags, Desc))
454       EmitByte(0x40 | REX, OS);
455   }
456   
457   // 0x0F escape code must be emitted just before the opcode.
458   if (Need0FPrefix)
459     EmitByte(0x0F, OS);
460   
461   // FIXME: Pull this up into previous switch if REX can be moved earlier.
462   switch (TSFlags & X86II::Op0Mask) {
463   case X86II::TF:    // F2 0F 38
464   case X86II::T8:    // 0F 38
465     EmitByte(0x38, OS);
466     break;
467   case X86II::TA:    // 0F 3A
468     EmitByte(0x3A, OS);
469     break;
470   }
471   
472   // If this is a two-address instruction, skip one of the register operands.
473   unsigned NumOps = Desc.getNumOperands();
474   unsigned CurOp = 0;
475   if (NumOps > 1 && Desc.getOperandConstraint(1, TOI::TIED_TO) != -1)
476     ++CurOp;
477   else if (NumOps > 2 && Desc.getOperandConstraint(NumOps-1, TOI::TIED_TO)== 0)
478     // Skip the last source operand that is tied_to the dest reg. e.g. LXADD32
479     --NumOps;
480   
481   unsigned char BaseOpcode = X86II::getBaseOpcodeFor(TSFlags);
482   switch (TSFlags & X86II::FormMask) {
483   case X86II::MRMInitReg:
484     assert(0 && "FIXME: Remove this form when the JIT moves to MCCodeEmitter!");
485   default: errs() << "FORM: " << (TSFlags & X86II::FormMask) << "\n";
486       assert(0 && "Unknown FormMask value in X86MCCodeEmitter!");
487   case X86II::RawFrm: {
488     EmitByte(BaseOpcode, OS);
489     
490     if (CurOp == NumOps)
491       break;
492     
493     assert(0 && "Unimpl RawFrm expr");
494     break;
495   }
496       
497   case X86II::AddRegFrm: {
498     EmitByte(BaseOpcode + GetX86RegNum(MI.getOperand(CurOp++)),OS);
499     if (CurOp == NumOps)
500       break;
501
502     const MCOperand &MO1 = MI.getOperand(CurOp++);
503     if (MO1.isImm()) {
504       unsigned Size = X86II::getSizeOfImm(TSFlags);
505       EmitConstant(MO1.getImm(), Size, OS);
506       break;
507     }
508
509     assert(0 && "Unimpl AddRegFrm expr");
510     break;
511   }
512       
513   case X86II::MRMDestReg:
514     EmitByte(BaseOpcode, OS);
515     EmitRegModRMByte(MI.getOperand(CurOp),
516                      GetX86RegNum(MI.getOperand(CurOp+1)), OS);
517     CurOp += 2;
518     if (CurOp != NumOps)
519       EmitConstant(MI.getOperand(CurOp++).getImm(),
520                    X86II::getSizeOfImm(TSFlags), OS);
521     break;
522   
523   case X86II::MRMDestMem:
524     EmitByte(BaseOpcode, OS);
525     EmitMemModRMByte(MI, CurOp,
526                      GetX86RegNum(MI.getOperand(CurOp + X86AddrNumOperands)),
527                      0, OS);
528     CurOp += X86AddrNumOperands + 1;
529     if (CurOp != NumOps)
530       EmitConstant(MI.getOperand(CurOp++).getImm(),
531                    X86II::getSizeOfImm(TSFlags), OS);
532     break;
533       
534   case X86II::MRMSrcReg:
535     EmitByte(BaseOpcode, OS);
536     EmitRegModRMByte(MI.getOperand(CurOp+1), GetX86RegNum(MI.getOperand(CurOp)),
537                      OS);
538     CurOp += 2;
539     if (CurOp != NumOps)
540       EmitConstant(MI.getOperand(CurOp++).getImm(),
541                    X86II::getSizeOfImm(TSFlags), OS);
542     break;
543     
544   case X86II::MRMSrcMem: {
545     EmitByte(BaseOpcode, OS);
546
547     // FIXME: Maybe lea should have its own form?  This is a horrible hack.
548     int AddrOperands;
549     if (Opcode == X86::LEA64r || Opcode == X86::LEA64_32r ||
550         Opcode == X86::LEA16r || Opcode == X86::LEA32r)
551       AddrOperands = X86AddrNumOperands - 1; // No segment register
552     else
553       AddrOperands = X86AddrNumOperands;
554     
555     // FIXME: What is this actually doing?
556     intptr_t PCAdj = (CurOp + AddrOperands + 1 != NumOps) ?
557        X86II::getSizeOfImm(TSFlags) : 0;
558     
559     EmitMemModRMByte(MI, CurOp+1, GetX86RegNum(MI.getOperand(CurOp)),
560                      PCAdj, OS);
561     CurOp += AddrOperands + 1;
562     if (CurOp != NumOps)
563       EmitConstant(MI.getOperand(CurOp++).getImm(),
564                    X86II::getSizeOfImm(TSFlags), OS);
565     break;
566   }
567
568   case X86II::MRM0r: case X86II::MRM1r:
569   case X86II::MRM2r: case X86II::MRM3r:
570   case X86II::MRM4r: case X86II::MRM5r:
571   case X86II::MRM6r: case X86II::MRM7r: {
572     EmitByte(BaseOpcode, OS);
573
574     // Special handling of lfence, mfence, monitor, and mwait.
575     // FIXME: This is terrible, they should get proper encoding bits in TSFlags.
576     if (Opcode == X86::LFENCE || Opcode == X86::MFENCE ||
577         Opcode == X86::MONITOR || Opcode == X86::MWAIT) {
578       EmitByte(ModRMByte(3, (TSFlags & X86II::FormMask)-X86II::MRM0r, 0), OS);
579
580       switch (Opcode) {
581       default: break;
582       case X86::MONITOR: EmitByte(0xC8, OS); break;
583       case X86::MWAIT:   EmitByte(0xC9, OS); break;
584       }
585     } else {
586       EmitRegModRMByte(MI.getOperand(CurOp++),
587                        (TSFlags & X86II::FormMask)-X86II::MRM0r,
588                        OS);
589     }
590
591     if (CurOp == NumOps)
592       break;
593     
594     const MCOperand &MO1 = MI.getOperand(CurOp++);
595     if (MO1.isImm()) {
596       EmitConstant(MO1.getImm(), X86II::getSizeOfImm(TSFlags), OS);
597       break;
598     }
599
600     assert(0 && "relo unimpl");
601 #if 0
602     unsigned rt = Is64BitMode ? X86::reloc_pcrel_word
603       : (IsPIC ? X86::reloc_picrel_word : X86::reloc_absolute_word);
604     if (Opcode == X86::MOV64ri32)
605       rt = X86::reloc_absolute_word_sext;  // FIXME: add X86II flag?
606     if (MO1.isGlobal()) {
607       bool Indirect = gvNeedsNonLazyPtr(MO1, TM);
608       emitGlobalAddress(MO1.getGlobal(), rt, MO1.getOffset(), 0,
609                         Indirect);
610     } else if (MO1.isSymbol())
611       emitExternalSymbolAddress(MO1.getSymbolName(), rt);
612     else if (MO1.isCPI())
613       emitConstPoolAddress(MO1.getIndex(), rt);
614     else if (MO1.isJTI())
615       emitJumpTableAddress(MO1.getIndex(), rt);
616     break;
617 #endif
618   }
619   case X86II::MRM0m: case X86II::MRM1m:
620   case X86II::MRM2m: case X86II::MRM3m:
621   case X86II::MRM4m: case X86II::MRM5m:
622   case X86II::MRM6m: case X86II::MRM7m: {
623     intptr_t PCAdj = 0;
624     if (CurOp + X86AddrNumOperands != NumOps) {
625       if (MI.getOperand(CurOp+X86AddrNumOperands).isImm())
626         PCAdj = X86II::getSizeOfImm(TSFlags);
627       else
628         PCAdj = 4;
629     }
630
631     EmitByte(BaseOpcode, OS);
632     EmitMemModRMByte(MI, CurOp, (TSFlags & X86II::FormMask)-X86II::MRM0m,
633                      PCAdj, OS);
634     CurOp += X86AddrNumOperands;
635     
636     if (CurOp == NumOps)
637       break;
638     
639     const MCOperand &MO = MI.getOperand(CurOp++);
640     if (MO.isImm()) {
641       EmitConstant(MO.getImm(), X86II::getSizeOfImm(TSFlags), OS);
642       break;
643     }
644     
645     assert(0 && "relo not handled");
646 #if 0
647     unsigned rt = Is64BitMode ? X86::reloc_pcrel_word
648     : (IsPIC ? X86::reloc_picrel_word : X86::reloc_absolute_word);
649     if (Opcode == X86::MOV64mi32)
650       rt = X86::reloc_absolute_word_sext;  // FIXME: add X86II flag?
651     if (MO.isGlobal()) {
652       bool Indirect = gvNeedsNonLazyPtr(MO, TM);
653       emitGlobalAddress(MO.getGlobal(), rt, MO.getOffset(), 0,
654                         Indirect);
655     } else if (MO.isSymbol())
656       emitExternalSymbolAddress(MO.getSymbolName(), rt);
657     else if (MO.isCPI())
658       emitConstPoolAddress(MO.getIndex(), rt);
659     else if (MO.isJTI())
660       emitJumpTableAddress(MO.getIndex(), rt);
661 #endif
662     break;
663   }
664   }
665   
666 #ifndef NDEBUG
667   // FIXME: Verify.
668   if (/*!Desc.isVariadic() &&*/ CurOp != NumOps) {
669     errs() << "Cannot encode all operands of: ";
670     MI.dump();
671     errs() << '\n';
672     abort();
673   }
674 #endif
675 }