LLVM API Documentation

X86CodeEmitter.cpp

Go to the documentation of this file.
00001 //===-- X86/X86CodeEmitter.cpp - Convert X86 code to machine code ---------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file contains the pass that transforms the X86 machine instructions into
00011 // relocatable machine code.
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #define DEBUG_TYPE "x86-emitter"
00016 #include "X86InstrInfo.h"
00017 #include "X86JITInfo.h"
00018 #include "X86Subtarget.h"
00019 #include "X86TargetMachine.h"
00020 #include "X86Relocations.h"
00021 #include "X86.h"
00022 #include "llvm/PassManager.h"
00023 #include "llvm/CodeGen/MachineCodeEmitter.h"
00024 #include "llvm/CodeGen/JITCodeEmitter.h"
00025 #include "llvm/CodeGen/ObjectCodeEmitter.h"
00026 #include "llvm/CodeGen/MachineFunctionPass.h"
00027 #include "llvm/CodeGen/MachineInstr.h"
00028 #include "llvm/CodeGen/MachineModuleInfo.h"
00029 #include "llvm/CodeGen/Passes.h"
00030 #include "llvm/Function.h"
00031 #include "llvm/ADT/Statistic.h"
00032 #include "llvm/Support/Compiler.h"
00033 #include "llvm/Support/Debug.h"
00034 #include "llvm/Support/ErrorHandling.h"
00035 #include "llvm/Support/raw_ostream.h"
00036 #include "llvm/Target/TargetOptions.h"
00037 using namespace llvm;
00038 
00039 STATISTIC(NumEmitted, "Number of machine instructions emitted");
00040 
00041 namespace {
00042   template<class CodeEmitter>
00043   class VISIBILITY_HIDDEN Emitter : public MachineFunctionPass {
00044     const X86InstrInfo  *II;
00045     const TargetData    *TD;
00046     X86TargetMachine    &TM;
00047     CodeEmitter         &MCE;
00048     intptr_t PICBaseOffset;
00049     bool Is64BitMode;
00050     bool IsPIC;
00051   public:
00052     static char ID;
00053     explicit Emitter(X86TargetMachine &tm, CodeEmitter &mce)
00054       : MachineFunctionPass(&ID), II(0), TD(0), TM(tm), 
00055       MCE(mce), PICBaseOffset(0), Is64BitMode(false),
00056       IsPIC(TM.getRelocationModel() == Reloc::PIC_) {}
00057     Emitter(X86TargetMachine &tm, CodeEmitter &mce,
00058             const X86InstrInfo &ii, const TargetData &td, bool is64)
00059       : MachineFunctionPass(&ID), II(&ii), TD(&td), TM(tm), 
00060       MCE(mce), PICBaseOffset(0), Is64BitMode(is64),
00061       IsPIC(TM.getRelocationModel() == Reloc::PIC_) {}
00062 
00063     bool runOnMachineFunction(MachineFunction &MF);
00064 
00065     virtual const char *getPassName() const {
00066       return "X86 Machine Code Emitter";
00067     }
00068 
00069     void emitInstruction(const MachineInstr &MI,
00070                          const TargetInstrDesc *Desc);
00071     
00072     void getAnalysisUsage(AnalysisUsage &AU) const {
00073       AU.setPreservesAll();
00074       AU.addRequired<MachineModuleInfo>();
00075       MachineFunctionPass::getAnalysisUsage(AU);
00076     }
00077 
00078   private:
00079     void emitPCRelativeBlockAddress(MachineBasicBlock *MBB);
00080     void emitGlobalAddress(GlobalValue *GV, unsigned Reloc,
00081                            intptr_t Disp = 0, intptr_t PCAdj = 0,
00082                            bool NeedStub = false, bool Indirect = false);
00083     void emitExternalSymbolAddress(const char *ES, unsigned Reloc);
00084     void emitConstPoolAddress(unsigned CPI, unsigned Reloc, intptr_t Disp = 0,
00085                               intptr_t PCAdj = 0);
00086     void emitJumpTableAddress(unsigned JTI, unsigned Reloc,
00087                               intptr_t PCAdj = 0);
00088 
00089     void emitDisplacementField(const MachineOperand *RelocOp, int DispVal,
00090                                intptr_t Adj = 0, bool IsPCRel = true);
00091 
00092     void emitRegModRMByte(unsigned ModRMReg, unsigned RegOpcodeField);
00093     void emitRegModRMByte(unsigned RegOpcodeField);
00094     void emitSIBByte(unsigned SS, unsigned Index, unsigned Base);
00095     void emitConstant(uint64_t Val, unsigned Size);
00096 
00097     void emitMemModRMByte(const MachineInstr &MI,
00098                           unsigned Op, unsigned RegOpcodeField,
00099                           intptr_t PCAdj = 0);
00100 
00101     unsigned getX86RegNum(unsigned RegNo) const;
00102   };
00103 
00104 template<class CodeEmitter>
00105   char Emitter<CodeEmitter>::ID = 0;
00106 } // end anonymous namespace.
00107 
00108 /// createX86CodeEmitterPass - Return a pass that emits the collected X86 code
00109 /// to the specified templated MachineCodeEmitter object.
00110 
00111 FunctionPass *llvm::createX86CodeEmitterPass(X86TargetMachine &TM,
00112                                              MachineCodeEmitter &MCE) {
00113   return new Emitter<MachineCodeEmitter>(TM, MCE);
00114 }
00115 FunctionPass *llvm::createX86JITCodeEmitterPass(X86TargetMachine &TM,
00116                                                 JITCodeEmitter &JCE) {
00117   return new Emitter<JITCodeEmitter>(TM, JCE);
00118 }
00119 FunctionPass *llvm::createX86ObjectCodeEmitterPass(X86TargetMachine &TM,
00120                                                    ObjectCodeEmitter &OCE) {
00121   return new Emitter<ObjectCodeEmitter>(TM, OCE);
00122 }
00123 
00124 template<class CodeEmitter>
00125 bool Emitter<CodeEmitter>::runOnMachineFunction(MachineFunction &MF) {
00126  
00127   MCE.setModuleInfo(&getAnalysis<MachineModuleInfo>());
00128   
00129   II = TM.getInstrInfo();
00130   TD = TM.getTargetData();
00131   Is64BitMode = TM.getSubtarget<X86Subtarget>().is64Bit();
00132   IsPIC = TM.getRelocationModel() == Reloc::PIC_;
00133   
00134   do {
00135     DEBUG(errs() << "JITTing function '" 
00136           << MF.getFunction()->getName() << "'\n");
00137     MCE.startFunction(MF);
00138     for (MachineFunction::iterator MBB = MF.begin(), E = MF.end(); 
00139          MBB != E; ++MBB) {
00140       MCE.StartMachineBasicBlock(MBB);
00141       for (MachineBasicBlock::const_iterator I = MBB->begin(), E = MBB->end();
00142            I != E; ++I) {
00143         const TargetInstrDesc &Desc = I->getDesc();
00144         emitInstruction(*I, &Desc);
00145         // MOVPC32r is basically a call plus a pop instruction.
00146         if (Desc.getOpcode() == X86::MOVPC32r)
00147           emitInstruction(*I, &II->get(X86::POP32r));
00148         NumEmitted++;  // Keep track of the # of mi's emitted
00149       }
00150     }
00151   } while (MCE.finishFunction(MF));
00152 
00153   return false;
00154 }
00155 
00156 /// emitPCRelativeBlockAddress - This method keeps track of the information
00157 /// necessary to resolve the address of this block later and emits a dummy
00158 /// value.
00159 ///
00160 template<class CodeEmitter>
00161 void Emitter<CodeEmitter>::emitPCRelativeBlockAddress(MachineBasicBlock *MBB) {
00162   // Remember where this reference was and where it is to so we can
00163   // deal with it later.
00164   MCE.addRelocation(MachineRelocation::getBB(MCE.getCurrentPCOffset(),
00165                                              X86::reloc_pcrel_word, MBB));
00166   MCE.emitWordLE(0);
00167 }
00168 
00169 /// emitGlobalAddress - Emit the specified address to the code stream assuming
00170 /// this is part of a "take the address of a global" instruction.
00171 ///
00172 template<class CodeEmitter>
00173 void Emitter<CodeEmitter>::emitGlobalAddress(GlobalValue *GV, unsigned Reloc,
00174                                 intptr_t Disp /* = 0 */,
00175                                 intptr_t PCAdj /* = 0 */,
00176                                 bool NeedStub /* = false */,
00177                                 bool Indirect /* = false */) {
00178   intptr_t RelocCST = Disp;
00179   if (Reloc == X86::reloc_picrel_word)
00180     RelocCST = PICBaseOffset;
00181   else if (Reloc == X86::reloc_pcrel_word)
00182     RelocCST = PCAdj;
00183   MachineRelocation MR = Indirect
00184     ? MachineRelocation::getIndirectSymbol(MCE.getCurrentPCOffset(), Reloc,
00185                                            GV, RelocCST, NeedStub)
00186     : MachineRelocation::getGV(MCE.getCurrentPCOffset(), Reloc,
00187                                GV, RelocCST, NeedStub);
00188   MCE.addRelocation(MR);
00189   // The relocated value will be added to the displacement
00190   if (Reloc == X86::reloc_absolute_dword)
00191     MCE.emitDWordLE(Disp);
00192   else
00193     MCE.emitWordLE((int32_t)Disp);
00194 }
00195 
00196 /// emitExternalSymbolAddress - Arrange for the address of an external symbol to
00197 /// be emitted to the current location in the function, and allow it to be PC
00198 /// relative.
00199 template<class CodeEmitter>
00200 void Emitter<CodeEmitter>::emitExternalSymbolAddress(const char *ES,
00201                                                      unsigned Reloc) {
00202   intptr_t RelocCST = (Reloc == X86::reloc_picrel_word) ? PICBaseOffset : 0;
00203   MCE.addRelocation(MachineRelocation::getExtSym(MCE.getCurrentPCOffset(),
00204                                                  Reloc, ES, RelocCST));
00205   if (Reloc == X86::reloc_absolute_dword)
00206     MCE.emitDWordLE(0);
00207   else
00208     MCE.emitWordLE(0);
00209 }
00210 
00211 /// emitConstPoolAddress - Arrange for the address of an constant pool
00212 /// to be emitted to the current location in the function, and allow it to be PC
00213 /// relative.
00214 template<class CodeEmitter>
00215 void Emitter<CodeEmitter>::emitConstPoolAddress(unsigned CPI, unsigned Reloc,
00216                                    intptr_t Disp /* = 0 */,
00217                                    intptr_t PCAdj /* = 0 */) {
00218   intptr_t RelocCST = 0;
00219   if (Reloc == X86::reloc_picrel_word)
00220     RelocCST = PICBaseOffset;
00221   else if (Reloc == X86::reloc_pcrel_word)
00222     RelocCST = PCAdj;
00223   MCE.addRelocation(MachineRelocation::getConstPool(MCE.getCurrentPCOffset(),
00224                                                     Reloc, CPI, RelocCST));
00225   // The relocated value will be added to the displacement
00226   if (Reloc == X86::reloc_absolute_dword)
00227     MCE.emitDWordLE(Disp);
00228   else
00229     MCE.emitWordLE((int32_t)Disp);
00230 }
00231 
00232 /// emitJumpTableAddress - Arrange for the address of a jump table to
00233 /// be emitted to the current location in the function, and allow it to be PC
00234 /// relative.
00235 template<class CodeEmitter>
00236 void Emitter<CodeEmitter>::emitJumpTableAddress(unsigned JTI, unsigned Reloc,
00237                                    intptr_t PCAdj /* = 0 */) {
00238   intptr_t RelocCST = 0;
00239   if (Reloc == X86::reloc_picrel_word)
00240     RelocCST = PICBaseOffset;
00241   else if (Reloc == X86::reloc_pcrel_word)
00242     RelocCST = PCAdj;
00243   MCE.addRelocation(MachineRelocation::getJumpTable(MCE.getCurrentPCOffset(),
00244                                                     Reloc, JTI, RelocCST));
00245   // The relocated value will be added to the displacement
00246   if (Reloc == X86::reloc_absolute_dword)
00247     MCE.emitDWordLE(0);
00248   else
00249     MCE.emitWordLE(0);
00250 }
00251 
00252 template<class CodeEmitter>
00253 unsigned Emitter<CodeEmitter>::getX86RegNum(unsigned RegNo) const {
00254   return II->getRegisterInfo().getX86RegNum(RegNo);
00255 }
00256 
00257 inline static unsigned char ModRMByte(unsigned Mod, unsigned RegOpcode,
00258                                       unsigned RM) {
00259   assert(Mod < 4 && RegOpcode < 8 && RM < 8 && "ModRM Fields out of range!");
00260   return RM | (RegOpcode << 3) | (Mod << 6);
00261 }
00262 
00263 template<class CodeEmitter>
00264 void Emitter<CodeEmitter>::emitRegModRMByte(unsigned ModRMReg,
00265                                             unsigned RegOpcodeFld){
00266   MCE.emitByte(ModRMByte(3, RegOpcodeFld, getX86RegNum(ModRMReg)));
00267 }
00268 
00269 template<class CodeEmitter>
00270 void Emitter<CodeEmitter>::emitRegModRMByte(unsigned RegOpcodeFld) {
00271   MCE.emitByte(ModRMByte(3, RegOpcodeFld, 0));
00272 }
00273 
00274 template<class CodeEmitter>
00275 void Emitter<CodeEmitter>::emitSIBByte(unsigned SS, 
00276                                        unsigned Index,
00277                                        unsigned Base) {
00278   // SIB byte is in the same format as the ModRMByte...
00279   MCE.emitByte(ModRMByte(SS, Index, Base));
00280 }
00281 
00282 template<class CodeEmitter>
00283 void Emitter<CodeEmitter>::emitConstant(uint64_t Val, unsigned Size) {
00284   // Output the constant in little endian byte order...
00285   for (unsigned i = 0; i != Size; ++i) {
00286     MCE.emitByte(Val & 255);
00287     Val >>= 8;
00288   }
00289 }
00290 
00291 /// isDisp8 - Return true if this signed displacement fits in a 8-bit 
00292 /// sign-extended field. 
00293 static bool isDisp8(int Value) {
00294   return Value == (signed char)Value;
00295 }
00296 
00297 static bool gvNeedsNonLazyPtr(const MachineOperand &GVOp,
00298                               const TargetMachine &TM) {
00299   // For Darwin-64, simulate the linktime GOT by using the same non-lazy-pointer
00300   // mechanism as 32-bit mode.
00301   if (TM.getSubtarget<X86Subtarget>().is64Bit() && 
00302       !TM.getSubtarget<X86Subtarget>().isTargetDarwin())
00303     return false;
00304   
00305   // Return true if this is a reference to a stub containing the address of the
00306   // global, not the global itself.
00307   return isGlobalStubReference(GVOp.getTargetFlags());
00308 }
00309 
00310 template<class CodeEmitter>
00311 void Emitter<CodeEmitter>::emitDisplacementField(const MachineOperand *RelocOp,
00312                                                  int DispVal,
00313                                                  intptr_t Adj /* = 0 */,
00314                                                  bool IsPCRel /* = true */) {
00315   // If this is a simple integer displacement that doesn't require a relocation,
00316   // emit it now.
00317   if (!RelocOp) {
00318     emitConstant(DispVal, 4);
00319     return;
00320   }
00321 
00322   // Otherwise, this is something that requires a relocation.  Emit it as such
00323   // now.
00324   if (RelocOp->isGlobal()) {
00325     // In 64-bit static small code model, we could potentially emit absolute.
00326     // But it's probably not beneficial. If the MCE supports using RIP directly
00327     // do it, otherwise fallback to absolute (this is determined by IsPCRel). 
00328     //  89 05 00 00 00 00     mov    %eax,0(%rip)  # PC-relative
00329     //  89 04 25 00 00 00 00  mov    %eax,0x0      # Absolute
00330     unsigned rt = Is64BitMode ?
00331       (IsPCRel ? X86::reloc_pcrel_word : X86::reloc_absolute_word_sext)
00332       : (IsPIC ? X86::reloc_picrel_word : X86::reloc_absolute_word);
00333     bool NeedStub = isa<Function>(RelocOp->getGlobal());
00334     bool Indirect = gvNeedsNonLazyPtr(*RelocOp, TM);
00335     emitGlobalAddress(RelocOp->getGlobal(), rt, RelocOp->getOffset(),
00336                       Adj, NeedStub, Indirect);
00337   } else if (RelocOp->isCPI()) {
00338     unsigned rt = Is64BitMode ?
00339       (IsPCRel ? X86::reloc_pcrel_word : X86::reloc_absolute_word_sext)
00340       : (IsPCRel ? X86::reloc_picrel_word : X86::reloc_absolute_word);
00341     emitConstPoolAddress(RelocOp->getIndex(), rt,
00342                          RelocOp->getOffset(), Adj);
00343   } else if (RelocOp->isJTI()) {
00344     unsigned rt = Is64BitMode ?
00345       (IsPCRel ? X86::reloc_pcrel_word : X86::reloc_absolute_word_sext)
00346       : (IsPCRel ? X86::reloc_picrel_word : X86::reloc_absolute_word);
00347     emitJumpTableAddress(RelocOp->getIndex(), rt, Adj);
00348   } else {
00349     llvm_unreachable("Unknown value to relocate!");
00350   }
00351 }
00352 
00353 template<class CodeEmitter>
00354 void Emitter<CodeEmitter>::emitMemModRMByte(const MachineInstr &MI,
00355                                             unsigned Op,unsigned RegOpcodeField,
00356                                             intptr_t PCAdj) {
00357   const MachineOperand &Op3 = MI.getOperand(Op+3);
00358   int DispVal = 0;
00359   const MachineOperand *DispForReloc = 0;
00360   
00361   // Figure out what sort of displacement we have to handle here.
00362   if (Op3.isGlobal()) {
00363     DispForReloc = &Op3;
00364   } else if (Op3.isCPI()) {
00365     if (!MCE.earlyResolveAddresses() || Is64BitMode || IsPIC) {
00366       DispForReloc = &Op3;
00367     } else {
00368       DispVal += MCE.getConstantPoolEntryAddress(Op3.getIndex());
00369       DispVal += Op3.getOffset();
00370     }
00371   } else if (Op3.isJTI()) {
00372     if (!MCE.earlyResolveAddresses() || Is64BitMode || IsPIC) {
00373       DispForReloc = &Op3;
00374     } else {
00375       DispVal += MCE.getJumpTableEntryAddress(Op3.getIndex());
00376     }
00377   } else {
00378     DispVal = Op3.getImm();
00379   }
00380 
00381   const MachineOperand &Base     = MI.getOperand(Op);
00382   const MachineOperand &Scale    = MI.getOperand(Op+1);
00383   const MachineOperand &IndexReg = MI.getOperand(Op+2);
00384 
00385   unsigned BaseReg = Base.getReg();
00386 
00387   // Indicate that the displacement will use an pcrel or absolute reference
00388   // by default. MCEs able to resolve addresses on-the-fly use pcrel by default
00389   // while others, unless explicit asked to use RIP, use absolute references.
00390   bool IsPCRel = MCE.earlyResolveAddresses() ? true : false;
00391 
00392   // Is a SIB byte needed?
00393   // If no BaseReg, issue a RIP relative instruction only if the MCE can 
00394   // resolve addresses on-the-fly, otherwise use SIB (Intel Manual 2A, table
00395   // 2-7) and absolute references.
00396   if ((!Is64BitMode || DispForReloc || BaseReg != 0) &&
00397       IndexReg.getReg() == 0 && 
00398       ((BaseReg == 0 && MCE.earlyResolveAddresses()) || BaseReg == X86::RIP || 
00399        (BaseReg != 0 && getX86RegNum(BaseReg) != N86::ESP))) {
00400     if (BaseReg == 0 || BaseReg == X86::RIP) {  // Just a displacement?
00401       // Emit special case [disp32] encoding
00402       MCE.emitByte(ModRMByte(0, RegOpcodeField, 5));
00403       emitDisplacementField(DispForReloc, DispVal, PCAdj, true);
00404     } else {
00405       unsigned BaseRegNo = getX86RegNum(BaseReg);
00406       if (!DispForReloc && DispVal == 0 && BaseRegNo != N86::EBP) {
00407         // Emit simple indirect register encoding... [EAX] f.e.
00408         MCE.emitByte(ModRMByte(0, RegOpcodeField, BaseRegNo));
00409       } else if (!DispForReloc && isDisp8(DispVal)) {
00410         // Emit the disp8 encoding... [REG+disp8]
00411         MCE.emitByte(ModRMByte(1, RegOpcodeField, BaseRegNo));
00412         emitConstant(DispVal, 1);
00413       } else {
00414         // Emit the most general non-SIB encoding: [REG+disp32]
00415         MCE.emitByte(ModRMByte(2, RegOpcodeField, BaseRegNo));
00416         emitDisplacementField(DispForReloc, DispVal, PCAdj, IsPCRel);
00417       }
00418     }
00419 
00420   } else {  // We need a SIB byte, so start by outputting the ModR/M byte first
00421     assert(IndexReg.getReg() != X86::ESP &&
00422            IndexReg.getReg() != X86::RSP && "Cannot use ESP as index reg!");
00423 
00424     bool ForceDisp32 = false;
00425     bool ForceDisp8  = false;
00426     if (BaseReg == 0) {
00427       // If there is no base register, we emit the special case SIB byte with
00428       // MOD=0, BASE=5, to JUST get the index, scale, and displacement.
00429       MCE.emitByte(ModRMByte(0, RegOpcodeField, 4));
00430       ForceDisp32 = true;
00431     } else if (DispForReloc) {
00432       // Emit the normal disp32 encoding.
00433       MCE.emitByte(ModRMByte(2, RegOpcodeField, 4));
00434       ForceDisp32 = true;
00435     } else if (DispVal == 0 && getX86RegNum(BaseReg) != N86::EBP) {
00436       // Emit no displacement ModR/M byte
00437       MCE.emitByte(ModRMByte(0, RegOpcodeField, 4));
00438     } else if (isDisp8(DispVal)) {
00439       // Emit the disp8 encoding...
00440       MCE.emitByte(ModRMByte(1, RegOpcodeField, 4));
00441       ForceDisp8 = true;           // Make sure to force 8 bit disp if Base=EBP
00442     } else {
00443       // Emit the normal disp32 encoding...
00444       MCE.emitByte(ModRMByte(2, RegOpcodeField, 4));
00445     }
00446 
00447     // Calculate what the SS field value should be...
00448     static const unsigned SSTable[] = { ~0, 0, 1, ~0, 2, ~0, ~0, ~0, 3 };
00449     unsigned SS = SSTable[Scale.getImm()];
00450 
00451     if (BaseReg == 0) {
00452       // Handle the SIB byte for the case where there is no base, see Intel 
00453       // Manual 2A, table 2-7. The displacement has already been output.
00454       unsigned IndexRegNo;
00455       if (IndexReg.getReg())
00456         IndexRegNo = getX86RegNum(IndexReg.getReg());
00457       else // Examples: [ESP+1*<noreg>+4] or [scaled idx]+disp32 (MOD=0,BASE=5)
00458         IndexRegNo = 4;
00459       emitSIBByte(SS, IndexRegNo, 5);
00460     } else {
00461       unsigned BaseRegNo = getX86RegNum(BaseReg);
00462       unsigned IndexRegNo;
00463       if (IndexReg.getReg())
00464         IndexRegNo = getX86RegNum(IndexReg.getReg());
00465       else
00466         IndexRegNo = 4;   // For example [ESP+1*<noreg>+4]
00467       emitSIBByte(SS, IndexRegNo, BaseRegNo);
00468     }
00469 
00470     // Do we need to output a displacement?
00471     if (ForceDisp8) {
00472       emitConstant(DispVal, 1);
00473     } else if (DispVal != 0 || ForceDisp32) {
00474       emitDisplacementField(DispForReloc, DispVal, PCAdj, IsPCRel);
00475     }
00476   }
00477 }
00478 
00479 template<class CodeEmitter>
00480 void Emitter<CodeEmitter>::emitInstruction(const MachineInstr &MI,
00481                                            const TargetInstrDesc *Desc) {
00482   DEBUG(errs() << MI);
00483 
00484   MCE.processDebugLoc(MI.getDebugLoc());
00485 
00486   unsigned Opcode = Desc->Opcode;
00487 
00488   // Emit the lock opcode prefix as needed.
00489   if (Desc->TSFlags & X86II::LOCK)
00490     MCE.emitByte(0xF0);
00491 
00492   // Emit segment override opcode prefix as needed.
00493   switch (Desc->TSFlags & X86II::SegOvrMask) {
00494   case X86II::FS:
00495     MCE.emitByte(0x64);
00496     break;
00497   case X86II::GS:
00498     MCE.emitByte(0x65);
00499     break;
00500   default: llvm_unreachable("Invalid segment!");
00501   case 0: break;  // No segment override!
00502   }
00503 
00504   // Emit the repeat opcode prefix as needed.
00505   if ((Desc->TSFlags & X86II::Op0Mask) == X86II::REP)
00506     MCE.emitByte(0xF3);
00507 
00508   // Emit the operand size opcode prefix as needed.
00509   if (Desc->TSFlags & X86II::OpSize)
00510     MCE.emitByte(0x66);
00511 
00512   // Emit the address size opcode prefix as needed.
00513   if (Desc->TSFlags & X86II::AdSize)
00514     MCE.emitByte(0x67);
00515 
00516   bool Need0FPrefix = false;
00517   switch (Desc->TSFlags & X86II::Op0Mask) {
00518   case X86II::TB:  // Two-byte opcode prefix
00519   case X86II::T8:  // 0F 38
00520   case X86II::TA:  // 0F 3A
00521     Need0FPrefix = true;
00522     break;
00523   case X86II::TF: // F2 0F 38
00524     MCE.emitByte(0xF2);
00525     Need0FPrefix = true;
00526     break;
00527   case X86II::REP: break; // already handled.
00528   case X86II::XS:   // F3 0F
00529     MCE.emitByte(0xF3);
00530     Need0FPrefix = true;
00531     break;
00532   case X86II::XD:   // F2 0F
00533     MCE.emitByte(0xF2);
00534     Need0FPrefix = true;
00535     break;
00536   case X86II::D8: case X86II::D9: case X86II::DA: case X86II::DB:
00537   case X86II::DC: case X86II::DD: case X86II::DE: case X86II::DF:
00538     MCE.emitByte(0xD8+
00539                  (((Desc->TSFlags & X86II::Op0Mask)-X86II::D8)
00540                                    >> X86II::Op0Shift));
00541     break; // Two-byte opcode prefix
00542   default: llvm_unreachable("Invalid prefix!");
00543   case 0: break;  // No prefix!
00544   }
00545 
00546   // Handle REX prefix.
00547   if (Is64BitMode) {
00548     if (unsigned REX = X86InstrInfo::determineREX(MI))
00549       MCE.emitByte(0x40 | REX);
00550   }
00551 
00552   // 0x0F escape code must be emitted just before the opcode.
00553   if (Need0FPrefix)
00554     MCE.emitByte(0x0F);
00555 
00556   switch (Desc->TSFlags & X86II::Op0Mask) {
00557   case X86II::TF:    // F2 0F 38
00558   case X86II::T8:    // 0F 38
00559     MCE.emitByte(0x38);
00560     break;
00561   case X86II::TA:    // 0F 3A
00562     MCE.emitByte(0x3A);
00563     break;
00564   }
00565 
00566   // If this is a two-address instruction, skip one of the register operands.
00567   unsigned NumOps = Desc->getNumOperands();
00568   unsigned CurOp = 0;
00569   if (NumOps > 1 && Desc->getOperandConstraint(1, TOI::TIED_TO) != -1)
00570     ++CurOp;
00571   else if (NumOps > 2 && Desc->getOperandConstraint(NumOps-1, TOI::TIED_TO)== 0)
00572     // Skip the last source operand that is tied_to the dest reg. e.g. LXADD32
00573     --NumOps;
00574 
00575   unsigned char BaseOpcode = II->getBaseOpcodeFor(Desc);
00576   switch (Desc->TSFlags & X86II::FormMask) {
00577   default:
00578     llvm_unreachable("Unknown FormMask value in X86 MachineCodeEmitter!");
00579   case X86II::Pseudo:
00580     // Remember the current PC offset, this is the PIC relocation
00581     // base address.
00582     switch (Opcode) {
00583     default: 
00584       llvm_unreachable("psuedo instructions should be removed before code"
00585                        " emission");
00586       break;
00587     case TargetInstrInfo::INLINEASM:
00588       // We allow inline assembler nodes with empty bodies - they can
00589       // implicitly define registers, which is ok for JIT.
00590       assert(MI.getOperand(0).getSymbolName()[0] == 0 && 
00591              "JIT does not support inline asm!");
00592       break;
00593     case TargetInstrInfo::DBG_LABEL:
00594     case TargetInstrInfo::EH_LABEL:
00595       MCE.emitLabel(MI.getOperand(0).getImm());
00596       break;
00597     case TargetInstrInfo::IMPLICIT_DEF:
00598     case TargetInstrInfo::DECLARE:
00599     case X86::DWARF_LOC:
00600     case X86::FP_REG_KILL:
00601       break;
00602     case X86::MOVPC32r: {
00603       // This emits the "call" portion of this pseudo instruction.
00604       MCE.emitByte(BaseOpcode);
00605       emitConstant(0, X86InstrInfo::sizeOfImm(Desc));
00606       // Remember PIC base.
00607       PICBaseOffset = (intptr_t) MCE.getCurrentPCOffset();
00608       X86JITInfo *JTI = TM.getJITInfo();
00609       JTI->setPICBase(MCE.getCurrentPCValue());
00610       break;
00611     }
00612     }
00613     CurOp = NumOps;
00614     break;
00615   case X86II::RawFrm: {
00616     MCE.emitByte(BaseOpcode);
00617 
00618     if (CurOp == NumOps)
00619       break;
00620       
00621     const MachineOperand &MO = MI.getOperand(CurOp++);
00622 
00623     DEBUG(errs() << "RawFrm CurOp " << CurOp << "\n");
00624     DEBUG(errs() << "isMBB " << MO.isMBB() << "\n");
00625     DEBUG(errs() << "isGlobal " << MO.isGlobal() << "\n");
00626     DEBUG(errs() << "isSymbol " << MO.isSymbol() << "\n");
00627     DEBUG(errs() << "isImm " << MO.isImm() << "\n");
00628 
00629     if (MO.isMBB()) {
00630       emitPCRelativeBlockAddress(MO.getMBB());
00631       break;
00632     }
00633     
00634     if (MO.isGlobal()) {
00635       // Assume undefined functions may be outside the Small codespace.
00636       bool NeedStub = 
00637         (Is64BitMode && 
00638             (TM.getCodeModel() == CodeModel::Large ||
00639              TM.getSubtarget<X86Subtarget>().isTargetDarwin())) ||
00640         Opcode == X86::TAILJMPd;
00641       emitGlobalAddress(MO.getGlobal(), X86::reloc_pcrel_word,
00642                         MO.getOffset(), 0, NeedStub);
00643       break;
00644     }
00645     
00646     if (MO.isSymbol()) {
00647       emitExternalSymbolAddress(MO.getSymbolName(), X86::reloc_pcrel_word);
00648       break;
00649     }
00650     
00651     assert(MO.isImm() && "Unknown RawFrm operand!");
00652     if (Opcode == X86::CALLpcrel32 || Opcode == X86::CALL64pcrel32) {
00653       // Fix up immediate operand for pc relative calls.
00654       intptr_t Imm = (intptr_t)MO.getImm();
00655       Imm = Imm - MCE.getCurrentPCValue() - 4;
00656       emitConstant(Imm, X86InstrInfo::sizeOfImm(Desc));
00657     } else
00658       emitConstant(MO.getImm(), X86InstrInfo::sizeOfImm(Desc));
00659     break;
00660   }
00661       
00662   case X86II::AddRegFrm: {
00663     MCE.emitByte(BaseOpcode + getX86RegNum(MI.getOperand(CurOp++).getReg()));
00664     
00665     if (CurOp == NumOps)
00666       break;
00667       
00668     const MachineOperand &MO1 = MI.getOperand(CurOp++);
00669     unsigned Size = X86InstrInfo::sizeOfImm(Desc);
00670     if (MO1.isImm()) {
00671       emitConstant(MO1.getImm(), Size);
00672       break;
00673     }
00674     
00675     unsigned rt = Is64BitMode ? X86::reloc_pcrel_word
00676       : (IsPIC ? X86::reloc_picrel_word : X86::reloc_absolute_word);
00677     if (Opcode == X86::MOV64ri64i32)
00678       rt = X86::reloc_absolute_word;  // FIXME: add X86II flag?
00679     // This should not occur on Darwin for relocatable objects.
00680     if (Opcode == X86::MOV64ri)
00681       rt = X86::reloc_absolute_dword;  // FIXME: add X86II flag?
00682     if (MO1.isGlobal()) {
00683       bool NeedStub = isa<Function>(MO1.getGlobal());
00684       bool Indirect = gvNeedsNonLazyPtr(MO1, TM);
00685       emitGlobalAddress(MO1.getGlobal(), rt, MO1.getOffset(), 0,
00686                         NeedStub, Indirect);
00687     } else if (MO1.isSymbol())
00688       emitExternalSymbolAddress(MO1.getSymbolName(), rt);
00689     else if (MO1.isCPI())
00690       emitConstPoolAddress(MO1.getIndex(), rt);
00691     else if (MO1.isJTI())
00692       emitJumpTableAddress(MO1.getIndex(), rt);
00693     break;
00694   }
00695 
00696   case X86II::MRMDestReg: {
00697     MCE.emitByte(BaseOpcode);
00698     emitRegModRMByte(MI.getOperand(CurOp).getReg(),
00699                      getX86RegNum(MI.getOperand(CurOp+1).getReg()));
00700     CurOp += 2;
00701     if (CurOp != NumOps)
00702       emitConstant(MI.getOperand(CurOp++).getImm(),
00703                    X86InstrInfo::sizeOfImm(Desc));
00704     break;
00705   }
00706   case X86II::MRMDestMem: {
00707     MCE.emitByte(BaseOpcode);
00708     emitMemModRMByte(MI, CurOp,
00709                      getX86RegNum(MI.getOperand(CurOp + X86AddrNumOperands)
00710                                   .getReg()));
00711     CurOp +=  X86AddrNumOperands + 1;
00712     if (CurOp != NumOps)
00713       emitConstant(MI.getOperand(CurOp++).getImm(),
00714                    X86InstrInfo::sizeOfImm(Desc));
00715     break;
00716   }
00717 
00718   case X86II::MRMSrcReg:
00719     MCE.emitByte(BaseOpcode);
00720     emitRegModRMByte(MI.getOperand(CurOp+1).getReg(),
00721                      getX86RegNum(MI.getOperand(CurOp).getReg()));
00722     CurOp += 2;
00723     if (CurOp != NumOps)
00724       emitConstant(MI.getOperand(CurOp++).getImm(),
00725                    X86InstrInfo::sizeOfImm(Desc));
00726     break;
00727 
00728   case X86II::MRMSrcMem: {
00729     // FIXME: Maybe lea should have its own form?
00730     int AddrOperands;
00731     if (Opcode == X86::LEA64r || Opcode == X86::LEA64_32r ||
00732         Opcode == X86::LEA16r || Opcode == X86::LEA32r)
00733       AddrOperands = X86AddrNumOperands - 1; // No segment register
00734     else
00735       AddrOperands = X86AddrNumOperands;
00736 
00737     intptr_t PCAdj = (CurOp + AddrOperands + 1 != NumOps) ?
00738       X86InstrInfo::sizeOfImm(Desc) : 0;
00739 
00740     MCE.emitByte(BaseOpcode);
00741     emitMemModRMByte(MI, CurOp+1, getX86RegNum(MI.getOperand(CurOp).getReg()),
00742                      PCAdj);
00743     CurOp += AddrOperands + 1;
00744     if (CurOp != NumOps)
00745       emitConstant(MI.getOperand(CurOp++).getImm(),
00746                    X86InstrInfo::sizeOfImm(Desc));
00747     break;
00748   }
00749 
00750   case X86II::MRM0r: case X86II::MRM1r:
00751   case X86II::MRM2r: case X86II::MRM3r:
00752   case X86II::MRM4r: case X86II::MRM5r:
00753   case X86II::MRM6r: case X86II::MRM7r: {
00754     MCE.emitByte(BaseOpcode);
00755 
00756     // Special handling of lfence, mfence, monitor, and mwait.
00757     if (Desc->getOpcode() == X86::LFENCE ||
00758         Desc->getOpcode() == X86::MFENCE ||
00759         Desc->getOpcode() == X86::MONITOR ||
00760         Desc->getOpcode() == X86::MWAIT) {
00761       emitRegModRMByte((Desc->TSFlags & X86II::FormMask)-X86II::MRM0r);
00762 
00763       switch (Desc->getOpcode()) {
00764       default: break;
00765       case X86::MONITOR:
00766         MCE.emitByte(0xC8);
00767         break;
00768       case X86::MWAIT:
00769         MCE.emitByte(0xC9);
00770         break;
00771       }
00772     } else {
00773       emitRegModRMByte(MI.getOperand(CurOp++).getReg(),
00774                        (Desc->TSFlags & X86II::FormMask)-X86II::MRM0r);
00775     }
00776 
00777     if (CurOp == NumOps)
00778       break;
00779     
00780     const MachineOperand &MO1 = MI.getOperand(CurOp++);
00781     unsigned Size = X86InstrInfo::sizeOfImm(Desc);
00782     if (MO1.isImm()) {
00783       emitConstant(MO1.getImm(), Size);
00784       break;
00785     }
00786     
00787     unsigned rt = Is64BitMode ? X86::reloc_pcrel_word
00788       : (IsPIC ? X86::reloc_picrel_word : X86::reloc_absolute_word);
00789     if (Opcode == X86::MOV64ri32)
00790       rt = X86::reloc_absolute_word_sext;  // FIXME: add X86II flag?
00791     if (MO1.isGlobal()) {
00792       bool NeedStub = isa<Function>(MO1.getGlobal());
00793       bool Indirect = gvNeedsNonLazyPtr(MO1, TM);
00794       emitGlobalAddress(MO1.getGlobal(), rt, MO1.getOffset(), 0,
00795                         NeedStub, Indirect);
00796     } else if (MO1.isSymbol())
00797       emitExternalSymbolAddress(MO1.getSymbolName(), rt);
00798     else if (MO1.isCPI())
00799       emitConstPoolAddress(MO1.getIndex(), rt);
00800     else if (MO1.isJTI())
00801       emitJumpTableAddress(MO1.getIndex(), rt);
00802     break;
00803   }
00804 
00805   case X86II::MRM0m: case X86II::MRM1m:
00806   case X86II::MRM2m: case X86II::MRM3m:
00807   case X86II::MRM4m: case X86II::MRM5m:
00808   case X86II::MRM6m: case X86II::MRM7m: {
00809     intptr_t PCAdj = (CurOp + X86AddrNumOperands != NumOps) ?
00810       (MI.getOperand(CurOp+X86AddrNumOperands).isImm() ? 
00811           X86InstrInfo::sizeOfImm(Desc) : 4) : 0;
00812 
00813     MCE.emitByte(BaseOpcode);
00814     emitMemModRMByte(MI, CurOp, (Desc->TSFlags & X86II::FormMask)-X86II::MRM0m,
00815                      PCAdj);
00816     CurOp += X86AddrNumOperands;
00817 
00818     if (CurOp == NumOps)
00819       break;
00820     
00821     const MachineOperand &MO = MI.getOperand(CurOp++);
00822     unsigned Size = X86InstrInfo::sizeOfImm(Desc);
00823     if (MO.isImm()) {
00824       emitConstant(MO.getImm(), Size);
00825       break;
00826     }
00827     
00828     unsigned rt = Is64BitMode ? X86::reloc_pcrel_word
00829       : (IsPIC ? X86::reloc_picrel_word : X86::reloc_absolute_word);
00830     if (Opcode == X86::MOV64mi32)
00831       rt = X86::reloc_absolute_word_sext;  // FIXME: add X86II flag?
00832     if (MO.isGlobal()) {
00833       bool NeedStub = isa<Function>(MO.getGlobal());
00834       bool Indirect = gvNeedsNonLazyPtr(MO, TM);
00835       emitGlobalAddress(MO.getGlobal(), rt, MO.getOffset(), 0,
00836                         NeedStub, Indirect);
00837     } else if (MO.isSymbol())
00838       emitExternalSymbolAddress(MO.getSymbolName(), rt);
00839     else if (MO.isCPI())
00840       emitConstPoolAddress(MO.getIndex(), rt);
00841     else if (MO.isJTI())
00842       emitJumpTableAddress(MO.getIndex(), rt);
00843     break;
00844   }
00845 
00846   case X86II::MRMInitReg:
00847     MCE.emitByte(BaseOpcode);
00848     // Duplicate register, used by things like MOV8r0 (aka xor reg,reg).
00849     emitRegModRMByte(MI.getOperand(CurOp).getReg(),
00850                      getX86RegNum(MI.getOperand(CurOp).getReg()));
00851     ++CurOp;
00852     break;
00853   }
00854 
00855   if (!Desc->isVariadic() && CurOp != NumOps) {
00856 #ifndef NDEBUG
00857     errs() << "Cannot encode all operands of: " << MI << "\n";
00858 #endif
00859     llvm_unreachable(0);
00860   }
00861 }