* Rename X86::IMULr16 -> X86::IMULrr16
[oota-llvm.git] / lib / Target / X86 / X86CodeEmitter.cpp
1 //===-- X86/X86CodeEmitter.cpp - Convert X86 code to machine code ---------===//
2 //
3 // This file contains the pass that transforms the X86 machine instructions into
4 // actual executable machine code.
5 //
6 //===----------------------------------------------------------------------===//
7
8 #define DEBUG_TYPE "jit"
9 #include "X86TargetMachine.h"
10 #include "X86.h"
11 #include "llvm/PassManager.h"
12 #include "llvm/CodeGen/MachineCodeEmitter.h"
13 #include "llvm/CodeGen/MachineFunctionPass.h"
14 #include "llvm/CodeGen/MachineInstr.h"
15 #include "llvm/Function.h"
16 #include "Support/Debug.h"
17 #include "Support/Statistic.h"
18 #include "Config/alloca.h"
19
20 namespace {
21   Statistic<>
22   NumEmitted("x86-emitter", "Number of machine instructions emitted");
23
24   class JITResolver {
25     MachineCodeEmitter &MCE;
26
27     // LazyCodeGenMap - Keep track of call sites for functions that are to be
28     // lazily resolved.
29     std::map<unsigned, Function*> LazyCodeGenMap;
30
31     // LazyResolverMap - Keep track of the lazy resolver created for a
32     // particular function so that we can reuse them if necessary.
33     std::map<Function*, unsigned> LazyResolverMap;
34   public:
35     JITResolver(MachineCodeEmitter &mce) : MCE(mce) {}
36     unsigned getLazyResolver(Function *F);
37     unsigned addFunctionReference(unsigned Address, Function *F);
38     
39   private:
40     unsigned emitStubForFunction(Function *F);
41     static void CompilationCallback();
42     unsigned resolveFunctionReference(unsigned RetAddr);
43   };
44
45   JITResolver *TheJITResolver;
46 }
47
48
49 /// addFunctionReference - This method is called when we need to emit the
50 /// address of a function that has not yet been emitted, so we don't know the
51 /// address.  Instead, we emit a call to the CompilationCallback method, and
52 /// keep track of where we are.
53 ///
54 unsigned JITResolver::addFunctionReference(unsigned Address, Function *F) {
55   LazyCodeGenMap[Address] = F;  
56   return (intptr_t)&JITResolver::CompilationCallback;
57 }
58
59 unsigned JITResolver::resolveFunctionReference(unsigned RetAddr) {
60   std::map<unsigned, Function*>::iterator I = LazyCodeGenMap.find(RetAddr);
61   assert(I != LazyCodeGenMap.end() && "Not in map!");
62   Function *F = I->second;
63   LazyCodeGenMap.erase(I);
64   return MCE.forceCompilationOf(F);
65 }
66
67 unsigned JITResolver::getLazyResolver(Function *F) {
68   std::map<Function*, unsigned>::iterator I = LazyResolverMap.lower_bound(F);
69   if (I != LazyResolverMap.end() && I->first == F) return I->second;
70   
71 //std::cerr << "Getting lazy resolver for : " << ((Value*)F)->getName() << "\n";
72
73   unsigned Stub = emitStubForFunction(F);
74   LazyResolverMap.insert(I, std::make_pair(F, Stub));
75   return Stub;
76 }
77
78 void JITResolver::CompilationCallback() {
79   unsigned *StackPtr = (unsigned*)__builtin_frame_address(0);
80   unsigned RetAddr = (unsigned)(intptr_t)__builtin_return_address(0);
81   assert(StackPtr[1] == RetAddr &&
82          "Could not find return address on the stack!");
83
84   // It's a stub if there is an interrupt marker after the call...
85   bool isStub = ((unsigned char*)(intptr_t)RetAddr)[0] == 0xCD;
86
87   // FIXME FIXME FIXME FIXME: __builtin_frame_address doesn't work if frame
88   // pointer elimination has been performed.  Having a variable sized alloca
89   // disables frame pointer elimination currently, even if it's dead.  This is a
90   // gross hack.
91   alloca(10+isStub);
92   // FIXME FIXME FIXME FIXME
93
94   // The call instruction should have pushed the return value onto the stack...
95   RetAddr -= 4;  // Backtrack to the reference itself...
96
97 #if 0
98   DEBUG(std::cerr << "In callback! Addr=0x" << std::hex << RetAddr
99                   << " ESP=0x" << (unsigned)StackPtr << std::dec
100                   << ": Resolving call to function: "
101                   << TheVM->getFunctionReferencedName((void*)RetAddr) << "\n");
102 #endif
103
104   // Sanity check to make sure this really is a call instruction...
105   assert(((unsigned char*)(intptr_t)RetAddr)[-1] == 0xE8 &&"Not a call instr!");
106   
107   unsigned NewVal = TheJITResolver->resolveFunctionReference(RetAddr);
108
109   // Rewrite the call target... so that we don't fault every time we execute
110   // the call.
111   *(unsigned*)(intptr_t)RetAddr = NewVal-RetAddr-4;    
112
113   if (isStub) {
114     // If this is a stub, rewrite the call into an unconditional branch
115     // instruction so that two return addresses are not pushed onto the stack
116     // when the requested function finally gets called.  This also makes the
117     // 0xCD byte (interrupt) dead, so the marker doesn't effect anything.
118     ((unsigned char*)(intptr_t)RetAddr)[-1] = 0xE9;
119   }
120
121   // Change the return address to reexecute the call instruction...
122   StackPtr[1] -= 5;
123 }
124
125 /// emitStubForFunction - This method is used by the JIT when it needs to emit
126 /// the address of a function for a function whose code has not yet been
127 /// generated.  In order to do this, it generates a stub which jumps to the lazy
128 /// function compiler, which will eventually get fixed to call the function
129 /// directly.
130 ///
131 unsigned JITResolver::emitStubForFunction(Function *F) {
132   MCE.startFunctionStub(*F, 6);
133   MCE.emitByte(0xE8);   // Call with 32 bit pc-rel destination...
134
135   unsigned Address = addFunctionReference(MCE.getCurrentPCValue(), F);
136   MCE.emitWord(Address-MCE.getCurrentPCValue()-4);
137
138   MCE.emitByte(0xCD);   // Interrupt - Just a marker identifying the stub!
139   return (intptr_t)MCE.finishFunctionStub(*F);
140 }
141
142
143
144 namespace {
145   class Emitter : public MachineFunctionPass {
146     const X86InstrInfo  *II;
147     MachineCodeEmitter  &MCE;
148     std::map<const BasicBlock*, unsigned> BasicBlockAddrs;
149     std::vector<std::pair<const BasicBlock*, unsigned> > BBRefs;
150   public:
151     Emitter(MachineCodeEmitter &mce) : II(0), MCE(mce) {}
152
153     bool runOnMachineFunction(MachineFunction &MF);
154
155     virtual const char *getPassName() const {
156       return "X86 Machine Code Emitter";
157     }
158
159   private:
160     void emitBasicBlock(MachineBasicBlock &MBB);
161     void emitInstruction(MachineInstr &MI);
162
163     void emitPCRelativeBlockAddress(BasicBlock *BB);
164     void emitMaybePCRelativeValue(unsigned Address, bool isPCRelative);
165     void emitGlobalAddressForCall(GlobalValue *GV);
166     void emitGlobalAddressForPtr(GlobalValue *GV);
167
168     void emitRegModRMByte(unsigned ModRMReg, unsigned RegOpcodeField);
169     void emitSIBByte(unsigned SS, unsigned Index, unsigned Base);
170     void emitConstant(unsigned Val, unsigned Size);
171
172     void emitMemModRMByte(const MachineInstr &MI,
173                           unsigned Op, unsigned RegOpcodeField);
174
175   };
176 }
177
178 /// addPassesToEmitMachineCode - Add passes to the specified pass manager to get
179 /// machine code emitted.  This uses a MachineCodeEmitter object to handle
180 /// actually outputting the machine code and resolving things like the address
181 /// of functions.  This method should returns true if machine code emission is
182 /// not supported.
183 ///
184 bool X86TargetMachine::addPassesToEmitMachineCode(FunctionPassManager &PM,
185                                                   MachineCodeEmitter &MCE) {
186   PM.add(new Emitter(MCE));
187   return false;
188 }
189
190 bool Emitter::runOnMachineFunction(MachineFunction &MF) {
191   II = &((X86TargetMachine&)MF.getTarget()).getInstrInfo();
192
193   MCE.startFunction(MF);
194   MCE.emitConstantPool(MF.getConstantPool());
195   for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I)
196     emitBasicBlock(*I);
197   MCE.finishFunction(MF);
198
199   // Resolve all forward branches now...
200   for (unsigned i = 0, e = BBRefs.size(); i != e; ++i) {
201     unsigned Location = BasicBlockAddrs[BBRefs[i].first];
202     unsigned Ref = BBRefs[i].second;
203     *(unsigned*)(intptr_t)Ref = Location-Ref-4;
204   }
205   BBRefs.clear();
206   BasicBlockAddrs.clear();
207   return false;
208 }
209
210 void Emitter::emitBasicBlock(MachineBasicBlock &MBB) {
211   if (uint64_t Addr = MCE.getCurrentPCValue())
212     BasicBlockAddrs[MBB.getBasicBlock()] = Addr;
213
214   for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end(); I != E; ++I)
215     emitInstruction(**I);
216 }
217
218
219 /// emitPCRelativeBlockAddress - This method emits the PC relative address of
220 /// the specified basic block, or if the basic block hasn't been emitted yet
221 /// (because this is a forward branch), it keeps track of the information
222 /// necessary to resolve this address later (and emits a dummy value).
223 ///
224 void Emitter::emitPCRelativeBlockAddress(BasicBlock *BB) {
225   // FIXME: Emit backward branches directly
226   BBRefs.push_back(std::make_pair(BB, MCE.getCurrentPCValue()));
227   MCE.emitWord(0);   // Emit a dummy value
228 }
229
230 /// emitMaybePCRelativeValue - Emit a 32-bit address which may be PC relative.
231 ///
232 void Emitter::emitMaybePCRelativeValue(unsigned Address, bool isPCRelative) {
233   if (isPCRelative)
234     MCE.emitWord(Address-MCE.getCurrentPCValue()-4);
235   else
236     MCE.emitWord(Address);
237 }
238
239 /// emitGlobalAddressForCall - Emit the specified address to the code stream
240 /// assuming this is part of a function call, which is PC relative.
241 ///
242 void Emitter::emitGlobalAddressForCall(GlobalValue *GV) {
243   // Get the address from the backend...
244   unsigned Address = MCE.getGlobalValueAddress(GV);
245   
246   if (Address == 0) {
247     // FIXME: this is JIT specific!
248     if (TheJITResolver == 0)
249       TheJITResolver = new JITResolver(MCE);
250     Address = TheJITResolver->addFunctionReference(MCE.getCurrentPCValue(),
251                                                    cast<Function>(GV));
252   }
253   emitMaybePCRelativeValue(Address, true);
254 }
255
256 /// emitGlobalAddress - Emit the specified address to the code stream assuming
257 /// this is part of a "take the address of a global" instruction, which is not
258 /// PC relative.
259 ///
260 void Emitter::emitGlobalAddressForPtr(GlobalValue *GV) {
261   // Get the address from the backend...
262   unsigned Address = MCE.getGlobalValueAddress(GV);
263
264   // If the machine code emitter doesn't know what the address IS yet, we have
265   // to take special measures.
266   //
267   if (Address == 0) {
268     // FIXME: this is JIT specific!
269     if (TheJITResolver == 0)
270       TheJITResolver = new JITResolver(MCE);
271     Address = TheJITResolver->getLazyResolver((Function*)GV);
272   }
273
274   emitMaybePCRelativeValue(Address, false);
275 }
276
277
278
279 /// N86 namespace - Native X86 Register numbers... used by X86 backend.
280 ///
281 namespace N86 {
282   enum {
283     EAX = 0, ECX = 1, EDX = 2, EBX = 3, ESP = 4, EBP = 5, ESI = 6, EDI = 7
284   };
285 }
286
287
288 // getX86RegNum - This function maps LLVM register identifiers to their X86
289 // specific numbering, which is used in various places encoding instructions.
290 //
291 static unsigned getX86RegNum(unsigned RegNo) {
292   switch(RegNo) {
293   case X86::EAX: case X86::AX: case X86::AL: return N86::EAX;
294   case X86::ECX: case X86::CX: case X86::CL: return N86::ECX;
295   case X86::EDX: case X86::DX: case X86::DL: return N86::EDX;
296   case X86::EBX: case X86::BX: case X86::BL: return N86::EBX;
297   case X86::ESP: case X86::SP: case X86::AH: return N86::ESP;
298   case X86::EBP: case X86::BP: case X86::CH: return N86::EBP;
299   case X86::ESI: case X86::SI: case X86::DH: return N86::ESI;
300   case X86::EDI: case X86::DI: case X86::BH: return N86::EDI;
301
302   case X86::ST0: case X86::ST1: case X86::ST2: case X86::ST3:
303   case X86::ST4: case X86::ST5: case X86::ST6: case X86::ST7:
304     return RegNo-X86::ST0;
305   default:
306     assert(RegNo >= MRegisterInfo::FirstVirtualRegister &&
307            "Unknown physical register!");
308     assert(0 && "Register allocator hasn't allocated reg correctly yet!");
309     return 0;
310   }
311 }
312
313 inline static unsigned char ModRMByte(unsigned Mod, unsigned RegOpcode,
314                                       unsigned RM) {
315   assert(Mod < 4 && RegOpcode < 8 && RM < 8 && "ModRM Fields out of range!");
316   return RM | (RegOpcode << 3) | (Mod << 6);
317 }
318
319 void Emitter::emitRegModRMByte(unsigned ModRMReg, unsigned RegOpcodeFld){
320   MCE.emitByte(ModRMByte(3, RegOpcodeFld, getX86RegNum(ModRMReg)));
321 }
322
323 void Emitter::emitSIBByte(unsigned SS, unsigned Index, unsigned Base) {
324   // SIB byte is in the same format as the ModRMByte...
325   MCE.emitByte(ModRMByte(SS, Index, Base));
326 }
327
328 void Emitter::emitConstant(unsigned Val, unsigned Size) {
329   // Output the constant in little endian byte order...
330   for (unsigned i = 0; i != Size; ++i) {
331     MCE.emitByte(Val & 255);
332     Val >>= 8;
333   }
334 }
335
336 static bool isDisp8(int Value) {
337   return Value == (signed char)Value;
338 }
339
340 void Emitter::emitMemModRMByte(const MachineInstr &MI,
341                                unsigned Op, unsigned RegOpcodeField) {
342   const MachineOperand &Disp     = MI.getOperand(Op+3);
343   if (MI.getOperand(Op).isConstantPoolIndex()) {
344     // Emit a direct address reference [disp32] where the displacement of the
345     // constant pool entry is controlled by the MCE.
346     MCE.emitByte(ModRMByte(0, RegOpcodeField, 5));
347     unsigned Index = MI.getOperand(Op).getConstantPoolIndex();
348     unsigned Address = MCE.getConstantPoolEntryAddress(Index);
349     MCE.emitWord(Address+Disp.getImmedValue());
350     return;
351   }
352
353   const MachineOperand &BaseReg  = MI.getOperand(Op);
354   const MachineOperand &Scale    = MI.getOperand(Op+1);
355   const MachineOperand &IndexReg = MI.getOperand(Op+2);
356
357   // Is a SIB byte needed?
358   if (IndexReg.getReg() == 0 && BaseReg.getReg() != X86::ESP) {
359     if (BaseReg.getReg() == 0) {  // Just a displacement?
360       // Emit special case [disp32] encoding
361       MCE.emitByte(ModRMByte(0, RegOpcodeField, 5));
362       emitConstant(Disp.getImmedValue(), 4);
363     } else {
364       unsigned BaseRegNo = getX86RegNum(BaseReg.getReg());
365       if (Disp.getImmedValue() == 0 && BaseRegNo != N86::EBP) {
366         // Emit simple indirect register encoding... [EAX] f.e.
367         MCE.emitByte(ModRMByte(0, RegOpcodeField, BaseRegNo));
368       } else if (isDisp8(Disp.getImmedValue())) {
369         // Emit the disp8 encoding... [REG+disp8]
370         MCE.emitByte(ModRMByte(1, RegOpcodeField, BaseRegNo));
371         emitConstant(Disp.getImmedValue(), 1);
372       } else {
373         // Emit the most general non-SIB encoding: [REG+disp32]
374         MCE.emitByte(ModRMByte(2, RegOpcodeField, BaseRegNo));
375         emitConstant(Disp.getImmedValue(), 4);
376       }
377     }
378
379   } else {  // We need a SIB byte, so start by outputting the ModR/M byte first
380     assert(IndexReg.getReg() != X86::ESP && "Cannot use ESP as index reg!");
381
382     bool ForceDisp32 = false;
383     bool ForceDisp8  = false;
384     if (BaseReg.getReg() == 0) {
385       // If there is no base register, we emit the special case SIB byte with
386       // MOD=0, BASE=5, to JUST get the index, scale, and displacement.
387       MCE.emitByte(ModRMByte(0, RegOpcodeField, 4));
388       ForceDisp32 = true;
389     } else if (Disp.getImmedValue() == 0 && BaseReg.getReg() != X86::EBP) {
390       // Emit no displacement ModR/M byte
391       MCE.emitByte(ModRMByte(0, RegOpcodeField, 4));
392     } else if (isDisp8(Disp.getImmedValue())) {
393       // Emit the disp8 encoding...
394       MCE.emitByte(ModRMByte(1, RegOpcodeField, 4));
395       ForceDisp8 = true;           // Make sure to force 8 bit disp if Base=EBP
396     } else {
397       // Emit the normal disp32 encoding...
398       MCE.emitByte(ModRMByte(2, RegOpcodeField, 4));
399     }
400
401     // Calculate what the SS field value should be...
402     static const unsigned SSTable[] = { ~0, 0, 1, ~0, 2, ~0, ~0, ~0, 3 };
403     unsigned SS = SSTable[Scale.getImmedValue()];
404
405     if (BaseReg.getReg() == 0) {
406       // Handle the SIB byte for the case where there is no base.  The
407       // displacement has already been output.
408       assert(IndexReg.getReg() && "Index register must be specified!");
409       emitSIBByte(SS, getX86RegNum(IndexReg.getReg()), 5);
410     } else {
411       unsigned BaseRegNo = getX86RegNum(BaseReg.getReg());
412       unsigned IndexRegNo;
413       if (IndexReg.getReg())
414         IndexRegNo = getX86RegNum(IndexReg.getReg());
415       else
416         IndexRegNo = 4;   // For example [ESP+1*<noreg>+4]
417       emitSIBByte(SS, IndexRegNo, BaseRegNo);
418     }
419
420     // Do we need to output a displacement?
421     if (Disp.getImmedValue() != 0 || ForceDisp32 || ForceDisp8) {
422       if (!ForceDisp32 && isDisp8(Disp.getImmedValue()))
423         emitConstant(Disp.getImmedValue(), 1);
424       else
425         emitConstant(Disp.getImmedValue(), 4);
426     }
427   }
428 }
429
430 static unsigned sizeOfPtr(const TargetInstrDescriptor &Desc) {
431   switch (Desc.TSFlags & X86II::ArgMask) {
432   case X86II::Arg8:   return 1;
433   case X86II::Arg16:  return 2;
434   case X86II::Arg32:  return 4;
435   case X86II::ArgF32: return 4;
436   case X86II::ArgF64: return 8;
437   case X86II::ArgF80: return 10;
438   default: assert(0 && "Memory size not set!");
439     return 0;
440   }
441 }
442
443 void Emitter::emitInstruction(MachineInstr &MI) {
444   NumEmitted++;  // Keep track of the # of mi's emitted
445
446   unsigned Opcode = MI.getOpcode();
447   const TargetInstrDescriptor &Desc = II->get(Opcode);
448
449   // Emit instruction prefixes if necessary
450   if (Desc.TSFlags & X86II::OpSize) MCE.emitByte(0x66);// Operand size...
451
452   switch (Desc.TSFlags & X86II::Op0Mask) {
453   case X86II::TB:
454     MCE.emitByte(0x0F);   // Two-byte opcode prefix
455     break;
456   case X86II::D8: case X86II::D9: case X86II::DA: case X86II::DB:
457   case X86II::DC: case X86II::DD: case X86II::DE: case X86II::DF:
458     MCE.emitByte(0xD8+
459                  (((Desc.TSFlags & X86II::Op0Mask)-X86II::D8)
460                                    >> X86II::Op0Shift));
461     break; // Two-byte opcode prefix
462   default: assert(0 && "Invalid prefix!");
463   case 0: break;  // No prefix!
464   }
465
466   unsigned char BaseOpcode = II->getBaseOpcodeFor(Opcode);
467   switch (Desc.TSFlags & X86II::FormMask) {
468   default: assert(0 && "Unknown FormMask value in X86 MachineCodeEmitter!");
469   case X86II::Pseudo:
470     if (Opcode != X86::IMPLICIT_USE && Opcode != X86::IMPLICIT_DEF)
471       std::cerr << "X86 Machine Code Emitter: No 'form', not emitting: " << MI;
472     break;
473
474   case X86II::RawFrm:
475     MCE.emitByte(BaseOpcode);
476     if (MI.getNumOperands() == 1) {
477       MachineOperand &MO = MI.getOperand(0);
478       if (MO.isPCRelativeDisp()) {
479         // Conditional branch... FIXME: this should use an MBB destination!
480         emitPCRelativeBlockAddress(cast<BasicBlock>(MO.getVRegValue()));
481       } else if (MO.isGlobalAddress()) {
482         assert(MO.isPCRelative() && "Call target is not PC Relative?");
483         emitGlobalAddressForCall(MO.getGlobal());
484       } else if (MO.isExternalSymbol()) {
485         unsigned Address = MCE.getGlobalValueAddress(MO.getSymbolName());
486         assert(Address && "Unknown external symbol!");
487         emitMaybePCRelativeValue(Address, MO.isPCRelative());
488       } else {
489         assert(0 && "Unknown RawFrm operand!");
490       }
491     }
492     break;
493
494   case X86II::AddRegFrm:
495     MCE.emitByte(BaseOpcode + getX86RegNum(MI.getOperand(0).getReg()));
496     if (MI.getNumOperands() == 2) {
497       MachineOperand &MO1 = MI.getOperand(1);
498       if (MO1.isImmediate() || MO1.getVRegValueOrNull() ||
499           MO1.isGlobalAddress() || MO1.isExternalSymbol()) {
500         unsigned Size = sizeOfPtr(Desc);
501         if (Value *V = MO1.getVRegValueOrNull()) {
502           assert(Size == 4 && "Don't know how to emit non-pointer values!");
503           emitGlobalAddressForPtr(cast<GlobalValue>(V));
504         } else if (MO1.isGlobalAddress()) {
505           assert(Size == 4 && "Don't know how to emit non-pointer values!");
506           assert(!MO1.isPCRelative() && "Function pointer ref is PC relative?");
507           emitGlobalAddressForPtr(MO1.getGlobal());
508         } else if (MO1.isExternalSymbol()) {
509           assert(Size == 4 && "Don't know how to emit non-pointer values!");
510
511           unsigned Address = MCE.getGlobalValueAddress(MO1.getSymbolName());
512           assert(Address && "Unknown external symbol!");
513           emitMaybePCRelativeValue(Address, MO1.isPCRelative());
514         } else {
515           emitConstant(MO1.getImmedValue(), Size);
516         }
517       }
518     }
519     break;
520
521   case X86II::MRMDestReg: {
522     MCE.emitByte(BaseOpcode);
523     MachineOperand &SrcOp = MI.getOperand(1+II->isTwoAddrInstr(Opcode));
524     emitRegModRMByte(MI.getOperand(0).getReg(), getX86RegNum(SrcOp.getReg()));
525     if (MI.getNumOperands() == 4)
526       emitConstant(MI.getOperand(3).getImmedValue(), sizeOfPtr(Desc));
527     break;
528   }
529   case X86II::MRMDestMem:
530     MCE.emitByte(BaseOpcode);
531     emitMemModRMByte(MI, 0, getX86RegNum(MI.getOperand(4).getReg()));
532     break;
533
534   case X86II::MRMSrcReg:
535     MCE.emitByte(BaseOpcode);
536
537     if (MI.getNumOperands() == 2) {
538       emitRegModRMByte(MI.getOperand(MI.getNumOperands()-1).getReg(),
539                        getX86RegNum(MI.getOperand(0).getReg()));
540     } else if (MI.getOperand(2).isImmediate()) {
541       emitRegModRMByte(MI.getOperand(1).getReg(),
542                        getX86RegNum(MI.getOperand(0).getReg()));
543
544       emitConstant(MI.getOperand(2).getImmedValue(), sizeOfPtr(Desc));
545     } else {
546       emitRegModRMByte(MI.getOperand(2).getReg(),
547                        getX86RegNum(MI.getOperand(0).getReg()));
548     }
549     break;
550
551   case X86II::MRMSrcMem:
552     MCE.emitByte(BaseOpcode);
553     emitMemModRMByte(MI, MI.getNumOperands()-4,
554                      getX86RegNum(MI.getOperand(0).getReg()));
555     break;
556
557   case X86II::MRMS0r: case X86II::MRMS1r:
558   case X86II::MRMS2r: case X86II::MRMS3r:
559   case X86II::MRMS4r: case X86II::MRMS5r:
560   case X86II::MRMS6r: case X86II::MRMS7r:
561     MCE.emitByte(BaseOpcode);
562     emitRegModRMByte(MI.getOperand(0).getReg(),
563                      (Desc.TSFlags & X86II::FormMask)-X86II::MRMS0r);
564
565     if (MI.getOperand(MI.getNumOperands()-1).isImmediate()) {
566       unsigned Size = sizeOfPtr(Desc);
567       emitConstant(MI.getOperand(MI.getNumOperands()-1).getImmedValue(), Size);
568     }
569     break;
570
571   case X86II::MRMS0m: case X86II::MRMS1m:
572   case X86II::MRMS2m: case X86II::MRMS3m:
573   case X86II::MRMS4m: case X86II::MRMS5m:
574   case X86II::MRMS6m: case X86II::MRMS7m: 
575     MCE.emitByte(BaseOpcode);
576     emitMemModRMByte(MI, 0, (Desc.TSFlags & X86II::FormMask)-X86II::MRMS0m);
577
578     if (MI.getNumOperands() == 5) {
579       unsigned Size = sizeOfPtr(Desc);
580       emitConstant(MI.getOperand(4).getImmedValue(), Size);
581     }
582     break;
583   }
584 }