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