LLVM API Documentation

Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

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/MachineFunctionPass.h"
00025 #include "llvm/CodeGen/MachineInstr.h"
00026 #include "llvm/CodeGen/MachineModuleInfo.h"
00027 #include "llvm/CodeGen/Passes.h"
00028 #include "llvm/Function.h"
00029 #include "llvm/ADT/Statistic.h"
00030 #include "llvm/Support/Compiler.h"
00031 #include "llvm/Support/Debug.h"
00032 #include "llvm/Target/TargetOptions.h"
00033 using namespace llvm;
00034 
00035 STATISTIC(NumEmitted, "Number of machine instructions emitted");
00036 
00037 namespace {
00038   class VISIBILITY_HIDDEN Emitter : public MachineFunctionPass {
00039     const X86InstrInfo  *II;
00040     const TargetData    *TD;
00041     X86TargetMachine    &TM;
00042     MachineCodeEmitter  &MCE;
00043     intptr_t PICBaseOffset;
00044     bool Is64BitMode;
00045     bool IsPIC;
00046   public:
00047     static char ID;
00048     explicit Emitter(X86TargetMachine &tm, MachineCodeEmitter &mce)
00049       : MachineFunctionPass((intptr_t)&ID), II(0), TD(0), TM(tm), 
00050       MCE(mce), PICBaseOffset(0), Is64BitMode(false),
00051       IsPIC(TM.getRelocationModel() == Reloc::PIC_) {}
00052     Emitter(X86TargetMachine &tm, MachineCodeEmitter &mce,
00053             const X86InstrInfo &ii, const TargetData &td, bool is64)
00054       : MachineFunctionPass((intptr_t)&ID), II(&ii), TD(&td), TM(tm), 
00055       MCE(mce), PICBaseOffset(0), Is64BitMode(is64),
00056       IsPIC(TM.getRelocationModel() == Reloc::PIC_) {}
00057 
00058     bool runOnMachineFunction(MachineFunction &MF);
00059 
00060     virtual const char *getPassName() const {
00061       return "X86 Machine Code Emitter";
00062     }
00063 
00064     void emitInstruction(const MachineInstr &MI,
00065                          const TargetInstrDesc *Desc);
00066     
00067     void getAnalysisUsage(AnalysisUsage &AU) const {
00068       AU.addRequired<MachineModuleInfo>();
00069       MachineFunctionPass::getAnalysisUsage(AU);
00070     }
00071 
00072   private:
00073     void emitPCRelativeBlockAddress(MachineBasicBlock *MBB);
00074     void emitGlobalAddress(GlobalValue *GV, unsigned Reloc,
00075                            int Disp = 0, intptr_t PCAdj = 0,
00076                            bool NeedStub = false, bool IsLazy = false);
00077     void emitExternalSymbolAddress(const char *ES, unsigned Reloc);
00078     void emitConstPoolAddress(unsigned CPI, unsigned Reloc, int Disp = 0,
00079                               intptr_t PCAdj = 0);
00080     void emitJumpTableAddress(unsigned JTI, unsigned Reloc,
00081                               intptr_t PCAdj = 0);
00082 
00083     void emitDisplacementField(const MachineOperand *RelocOp, int DispVal,
00084                                intptr_t PCAdj = 0);
00085 
00086     void emitRegModRMByte(unsigned ModRMReg, unsigned RegOpcodeField);
00087     void emitSIBByte(unsigned SS, unsigned Index, unsigned Base);
00088     void emitConstant(uint64_t Val, unsigned Size);
00089 
00090     void emitMemModRMByte(const MachineInstr &MI,
00091                           unsigned Op, unsigned RegOpcodeField,
00092                           intptr_t PCAdj = 0);
00093 
00094     unsigned getX86RegNum(unsigned RegNo) const;
00095 
00096     bool gvNeedsLazyPtr(const GlobalValue *GV);
00097   };
00098   char Emitter::ID = 0;
00099 }
00100 
00101 /// createX86CodeEmitterPass - Return a pass that emits the collected X86 code
00102 /// to the specified MCE object.
00103 FunctionPass *llvm::createX86CodeEmitterPass(X86TargetMachine &TM,
00104                                              MachineCodeEmitter &MCE) {
00105   return new Emitter(TM, MCE);
00106 }
00107 
00108 bool Emitter::runOnMachineFunction(MachineFunction &MF) {
00109  
00110   MCE.setModuleInfo(&getAnalysis<MachineModuleInfo>());
00111   
00112   II = TM.getInstrInfo();
00113   TD = TM.getTargetData();
00114   Is64BitMode = TM.getSubtarget<X86Subtarget>().is64Bit();
00115   IsPIC = TM.getRelocationModel() == Reloc::PIC_;
00116   
00117   do {
00118     DOUT << "JITTing function '" << MF.getFunction()->getName() << "'\n";
00119     MCE.startFunction(MF);
00120     for (MachineFunction::iterator MBB = MF.begin(), E = MF.end(); 
00121          MBB != E; ++MBB) {
00122       MCE.StartMachineBasicBlock(MBB);
00123       for (MachineBasicBlock::const_iterator I = MBB->begin(), E = MBB->end();
00124            I != E; ++I) {
00125         const TargetInstrDesc &Desc = I->getDesc();
00126         emitInstruction(*I, &Desc);
00127         // MOVPC32r is basically a call plus a pop instruction.
00128         if (Desc.getOpcode() == X86::MOVPC32r)
00129           emitInstruction(*I, &II->get(X86::POP32r));
00130         NumEmitted++;  // Keep track of the # of mi's emitted
00131       }
00132     }
00133   } while (MCE.finishFunction(MF));
00134 
00135   return false;
00136 }
00137 
00138 /// emitPCRelativeBlockAddress - This method keeps track of the information
00139 /// necessary to resolve the address of this block later and emits a dummy
00140 /// value.
00141 ///
00142 void Emitter::emitPCRelativeBlockAddress(MachineBasicBlock *MBB) {
00143   // Remember where this reference was and where it is to so we can
00144   // deal with it later.
00145   MCE.addRelocation(MachineRelocation::getBB(MCE.getCurrentPCOffset(),
00146                                              X86::reloc_pcrel_word, MBB));
00147   MCE.emitWordLE(0);
00148 }
00149 
00150 /// emitGlobalAddress - Emit the specified address to the code stream assuming
00151 /// this is part of a "take the address of a global" instruction.
00152 ///
00153 void Emitter::emitGlobalAddress(GlobalValue *GV, unsigned Reloc,
00154                                 int Disp /* = 0 */, intptr_t PCAdj /* = 0 */,
00155                                 bool NeedStub /* = false */,
00156                                 bool isLazy /* = false */) {
00157   intptr_t RelocCST = 0;
00158   if (Reloc == X86::reloc_picrel_word)
00159     RelocCST = PICBaseOffset;
00160   else if (Reloc == X86::reloc_pcrel_word)
00161     RelocCST = PCAdj;
00162   MachineRelocation MR = isLazy 
00163     ? MachineRelocation::getGVLazyPtr(MCE.getCurrentPCOffset(), Reloc,
00164                                       GV, RelocCST, NeedStub)
00165     : MachineRelocation::getGV(MCE.getCurrentPCOffset(), Reloc,
00166                                GV, RelocCST, NeedStub);
00167   MCE.addRelocation(MR);
00168   if (Reloc == X86::reloc_absolute_dword)
00169     MCE.emitWordLE(0);
00170   MCE.emitWordLE(Disp); // The relocated value will be added to the displacement
00171 }
00172 
00173 /// emitExternalSymbolAddress - Arrange for the address of an external symbol to
00174 /// be emitted to the current location in the function, and allow it to be PC
00175 /// relative.
00176 void Emitter::emitExternalSymbolAddress(const char *ES, unsigned Reloc) {
00177   intptr_t RelocCST = (Reloc == X86::reloc_picrel_word) ? PICBaseOffset : 0;
00178   MCE.addRelocation(MachineRelocation::getExtSym(MCE.getCurrentPCOffset(),
00179                                                  Reloc, ES, RelocCST));
00180   if (Reloc == X86::reloc_absolute_dword)
00181     MCE.emitWordLE(0);
00182   MCE.emitWordLE(0);
00183 }
00184 
00185 /// emitConstPoolAddress - Arrange for the address of an constant pool
00186 /// to be emitted to the current location in the function, and allow it to be PC
00187 /// relative.
00188 void Emitter::emitConstPoolAddress(unsigned CPI, unsigned Reloc,
00189                                    int Disp /* = 0 */,
00190                                    intptr_t PCAdj /* = 0 */) {
00191   intptr_t RelocCST = 0;
00192   if (Reloc == X86::reloc_picrel_word)
00193     RelocCST = PICBaseOffset;
00194   else if (Reloc == X86::reloc_pcrel_word)
00195     RelocCST = PCAdj;
00196   MCE.addRelocation(MachineRelocation::getConstPool(MCE.getCurrentPCOffset(),
00197                                                     Reloc, CPI, RelocCST));
00198   if (Reloc == X86::reloc_absolute_dword)
00199     MCE.emitWordLE(0);
00200   MCE.emitWordLE(Disp); // The relocated value will be added to the displacement
00201 }
00202 
00203 /// emitJumpTableAddress - Arrange for the address of a jump table to
00204 /// be emitted to the current location in the function, and allow it to be PC
00205 /// relative.
00206 void Emitter::emitJumpTableAddress(unsigned JTI, unsigned Reloc,
00207                                    intptr_t PCAdj /* = 0 */) {
00208   intptr_t RelocCST = 0;
00209   if (Reloc == X86::reloc_picrel_word)
00210     RelocCST = PICBaseOffset;
00211   else if (Reloc == X86::reloc_pcrel_word)
00212     RelocCST = PCAdj;
00213   MCE.addRelocation(MachineRelocation::getJumpTable(MCE.getCurrentPCOffset(),
00214                                                     Reloc, JTI, RelocCST));
00215   if (Reloc == X86::reloc_absolute_dword)
00216     MCE.emitWordLE(0);
00217   MCE.emitWordLE(0); // The relocated value will be added to the displacement
00218 }
00219 
00220 unsigned Emitter::getX86RegNum(unsigned RegNo) const {
00221   return II->getRegisterInfo().getX86RegNum(RegNo);
00222 }
00223 
00224 inline static unsigned char ModRMByte(unsigned Mod, unsigned RegOpcode,
00225                                       unsigned RM) {
00226   assert(Mod < 4 && RegOpcode < 8 && RM < 8 && "ModRM Fields out of range!");
00227   return RM | (RegOpcode << 3) | (Mod << 6);
00228 }
00229 
00230 void Emitter::emitRegModRMByte(unsigned ModRMReg, unsigned RegOpcodeFld){
00231   MCE.emitByte(ModRMByte(3, RegOpcodeFld, getX86RegNum(ModRMReg)));
00232 }
00233 
00234 void Emitter::emitSIBByte(unsigned SS, unsigned Index, unsigned Base) {
00235   // SIB byte is in the same format as the ModRMByte...
00236   MCE.emitByte(ModRMByte(SS, Index, Base));
00237 }
00238 
00239 void Emitter::emitConstant(uint64_t Val, unsigned Size) {
00240   // Output the constant in little endian byte order...
00241   for (unsigned i = 0; i != Size; ++i) {
00242     MCE.emitByte(Val & 255);
00243     Val >>= 8;
00244   }
00245 }
00246 
00247 /// isDisp8 - Return true if this signed displacement fits in a 8-bit 
00248 /// sign-extended field. 
00249 static bool isDisp8(int Value) {
00250   return Value == (signed char)Value;
00251 }
00252 
00253 bool Emitter::gvNeedsLazyPtr(const GlobalValue *GV) {
00254   // For Darwin, simulate the linktime GOT by using the same lazy-pointer
00255   // mechanism as 32-bit mode.
00256   return (!Is64BitMode || TM.getSubtarget<X86Subtarget>().isTargetDarwin()) &&
00257     TM.getSubtarget<X86Subtarget>().GVRequiresExtraLoad(GV, TM, false);
00258 }
00259 
00260 void Emitter::emitDisplacementField(const MachineOperand *RelocOp,
00261                                     int DispVal, intptr_t PCAdj) {
00262   // If this is a simple integer displacement that doesn't require a relocation,
00263   // emit it now.
00264   if (!RelocOp) {
00265     emitConstant(DispVal, 4);
00266     return;
00267   }
00268   
00269   // Otherwise, this is something that requires a relocation.  Emit it as such
00270   // now.
00271   if (RelocOp->isGlobalAddress()) {
00272     // In 64-bit static small code model, we could potentially emit absolute.
00273     // But it's probably not beneficial.
00274     //  89 05 00 00 00 00     mov    %eax,0(%rip)  # PC-relative
00275     //  89 04 25 00 00 00 00  mov    %eax,0x0      # Absolute
00276     unsigned rt = Is64BitMode ? X86::reloc_pcrel_word
00277       : (IsPIC ? X86::reloc_picrel_word : X86::reloc_absolute_word);
00278     bool NeedStub = isa<Function>(RelocOp->getGlobal());
00279     bool isLazy = gvNeedsLazyPtr(RelocOp->getGlobal());
00280     emitGlobalAddress(RelocOp->getGlobal(), rt, RelocOp->getOffset(),
00281                       PCAdj, NeedStub, isLazy);
00282   } else if (RelocOp->isConstantPoolIndex()) {
00283     unsigned rt = Is64BitMode ? X86::reloc_pcrel_word : X86::reloc_picrel_word;
00284     emitConstPoolAddress(RelocOp->getIndex(), rt,
00285                          RelocOp->getOffset(), PCAdj);
00286   } else if (RelocOp->isJumpTableIndex()) {
00287     unsigned rt = Is64BitMode ? X86::reloc_pcrel_word : X86::reloc_picrel_word;
00288     emitJumpTableAddress(RelocOp->getIndex(), rt, PCAdj);
00289   } else {
00290     assert(0 && "Unknown value to relocate!");
00291   }
00292 }
00293 
00294 void Emitter::emitMemModRMByte(const MachineInstr &MI,
00295                                unsigned Op, unsigned RegOpcodeField,
00296                                intptr_t PCAdj) {
00297   const MachineOperand &Op3 = MI.getOperand(Op+3);
00298   int DispVal = 0;
00299   const MachineOperand *DispForReloc = 0;
00300   
00301   // Figure out what sort of displacement we have to handle here.
00302   if (Op3.isGlobalAddress()) {
00303     DispForReloc = &Op3;
00304   } else if (Op3.isConstantPoolIndex()) {
00305     if (Is64BitMode || IsPIC) {
00306       DispForReloc = &Op3;
00307     } else {
00308       DispVal += MCE.getConstantPoolEntryAddress(Op3.getIndex());
00309       DispVal += Op3.getOffset();
00310     }
00311   } else if (Op3.isJumpTableIndex()) {
00312     if (Is64BitMode || IsPIC) {
00313       DispForReloc = &Op3;
00314     } else {
00315       DispVal += MCE.getJumpTableEntryAddress(Op3.getIndex());
00316     }
00317   } else {
00318     DispVal = Op3.getImm();
00319   }
00320 
00321   const MachineOperand &Base     = MI.getOperand(Op);
00322   const MachineOperand &Scale    = MI.getOperand(Op+1);
00323   const MachineOperand &IndexReg = MI.getOperand(Op+2);
00324 
00325   unsigned BaseReg = Base.getReg();
00326 
00327   // Is a SIB byte needed?
00328   if (IndexReg.getReg() == 0 &&
00329       (BaseReg == 0 || getX86RegNum(BaseReg) != N86::ESP)) {
00330     if (BaseReg == 0) {  // Just a displacement?
00331       // Emit special case [disp32] encoding
00332       MCE.emitByte(ModRMByte(0, RegOpcodeField, 5));
00333       
00334       emitDisplacementField(DispForReloc, DispVal, PCAdj);
00335     } else {
00336       unsigned BaseRegNo = getX86RegNum(BaseReg);
00337       if (!DispForReloc && DispVal == 0 && BaseRegNo != N86::EBP) {
00338         // Emit simple indirect register encoding... [EAX] f.e.
00339         MCE.emitByte(ModRMByte(0, RegOpcodeField, BaseRegNo));
00340       } else if (!DispForReloc && isDisp8(DispVal)) {
00341         // Emit the disp8 encoding... [REG+disp8]
00342         MCE.emitByte(ModRMByte(1, RegOpcodeField, BaseRegNo));
00343         emitConstant(DispVal, 1);
00344       } else {
00345         // Emit the most general non-SIB encoding: [REG+disp32]
00346         MCE.emitByte(ModRMByte(2, RegOpcodeField, BaseRegNo));
00347         emitDisplacementField(DispForReloc, DispVal, PCAdj);
00348       }
00349     }
00350 
00351   } else {  // We need a SIB byte, so start by outputting the ModR/M byte first
00352     assert(IndexReg.getReg() != X86::ESP &&
00353            IndexReg.getReg() != X86::RSP && "Cannot use ESP as index reg!");
00354 
00355     bool ForceDisp32 = false;
00356     bool ForceDisp8  = false;
00357     if (BaseReg == 0) {
00358       // If there is no base register, we emit the special case SIB byte with
00359       // MOD=0, BASE=5, to JUST get the index, scale, and displacement.
00360       MCE.emitByte(ModRMByte(0, RegOpcodeField, 4));
00361       ForceDisp32 = true;
00362     } else if (DispForReloc) {
00363       // Emit the normal disp32 encoding.
00364       MCE.emitByte(ModRMByte(2, RegOpcodeField, 4));
00365       ForceDisp32 = true;
00366     } else if (DispVal == 0 && getX86RegNum(BaseReg) != N86::EBP) {
00367       // Emit no displacement ModR/M byte
00368       MCE.emitByte(ModRMByte(0, RegOpcodeField, 4));
00369     } else if (isDisp8(DispVal)) {
00370       // Emit the disp8 encoding...
00371       MCE.emitByte(ModRMByte(1, RegOpcodeField, 4));
00372       ForceDisp8 = true;           // Make sure to force 8 bit disp if Base=EBP
00373     } else {
00374       // Emit the normal disp32 encoding...
00375       MCE.emitByte(ModRMByte(2, RegOpcodeField, 4));
00376     }
00377 
00378     // Calculate what the SS field value should be...
00379     static const unsigned SSTable[] = { ~0, 0, 1, ~0, 2, ~0, ~0, ~0, 3 };
00380     unsigned SS = SSTable[Scale.getImm()];
00381 
00382     if (BaseReg == 0) {
00383       // Handle the SIB byte for the case where there is no base.  The
00384       // displacement has already been output.
00385       assert(IndexReg.getReg() && "Index register must be specified!");
00386       emitSIBByte(SS, getX86RegNum(IndexReg.getReg()), 5);
00387     } else {
00388       unsigned BaseRegNo = getX86RegNum(BaseReg);
00389       unsigned IndexRegNo;
00390       if (IndexReg.getReg())
00391         IndexRegNo = getX86RegNum(IndexReg.getReg());
00392       else
00393         IndexRegNo = 4;   // For example [ESP+1*<noreg>+4]
00394       emitSIBByte(SS, IndexRegNo, BaseRegNo);
00395     }
00396 
00397     // Do we need to output a displacement?
00398     if (ForceDisp8) {
00399       emitConstant(DispVal, 1);
00400     } else if (DispVal != 0 || ForceDisp32) {
00401       emitDisplacementField(DispForReloc, DispVal, PCAdj);
00402     }
00403   }
00404 }
00405 
00406 void Emitter::emitInstruction(const MachineInstr &MI,
00407                               const TargetInstrDesc *Desc) {
00408   DOUT << MI;
00409 
00410   unsigned Opcode = Desc->Opcode;
00411 
00412   // Emit the lock opcode prefix as needed.
00413   if (Desc->TSFlags & X86II::LOCK) MCE.emitByte(0xF0);
00414 
00415   // Emit the repeat opcode prefix as needed.
00416   if ((Desc->TSFlags & X86II::Op0Mask) == X86II::REP) MCE.emitByte(0xF3);
00417 
00418   // Emit the operand size opcode prefix as needed.
00419   if (Desc->TSFlags & X86II::OpSize) MCE.emitByte(0x66);
00420 
00421   // Emit the address size opcode prefix as needed.
00422   if (Desc->TSFlags & X86II::AdSize) MCE.emitByte(0x67);
00423 
00424   bool Need0FPrefix = false;
00425   switch (Desc->TSFlags & X86II::Op0Mask) {
00426   case X86II::TB:  // Two-byte opcode prefix
00427   case X86II::T8:  // 0F 38
00428   case X86II::TA:  // 0F 3A
00429     Need0FPrefix = true;
00430     break;
00431   case X86II::REP: break; // already handled.
00432   case X86II::XS:   // F3 0F
00433     MCE.emitByte(0xF3);
00434     Need0FPrefix = true;
00435     break;
00436   case X86II::XD:   // F2 0F
00437     MCE.emitByte(0xF2);
00438     Need0FPrefix = true;
00439     break;
00440   case X86II::D8: case X86II::D9: case X86II::DA: case X86II::DB:
00441   case X86II::DC: case X86II::DD: case X86II::DE: case X86II::DF:
00442     MCE.emitByte(0xD8+
00443                  (((Desc->TSFlags & X86II::Op0Mask)-X86II::D8)
00444                                    >> X86II::Op0Shift));
00445     break; // Two-byte opcode prefix
00446   default: assert(0 && "Invalid prefix!");
00447   case 0: break;  // No prefix!
00448   }
00449 
00450   if (Is64BitMode) {
00451     // REX prefix
00452     unsigned REX = X86InstrInfo::determineREX(MI);
00453     if (REX)
00454       MCE.emitByte(0x40 | REX);
00455   }
00456 
00457   // 0x0F escape code must be emitted just before the opcode.
00458   if (Need0FPrefix)
00459     MCE.emitByte(0x0F);
00460 
00461   switch (Desc->TSFlags & X86II::Op0Mask) {
00462   case X86II::T8:  // 0F 38
00463     MCE.emitByte(0x38);
00464     break;
00465   case X86II::TA:    // 0F 3A
00466     MCE.emitByte(0x3A);
00467     break;
00468   }
00469 
00470   // If this is a two-address instruction, skip one of the register operands.
00471   unsigned NumOps = Desc->getNumOperands();
00472   unsigned CurOp = 0;
00473   if (NumOps > 1 && Desc->getOperandConstraint(1, TOI::TIED_TO) != -1)
00474     ++CurOp;
00475   else if (NumOps > 2 && Desc->getOperandConstraint(NumOps-1, TOI::TIED_TO)== 0)
00476     // Skip the last source operand that is tied_to the dest reg. e.g. LXADD32
00477     --NumOps;
00478 
00479   unsigned char BaseOpcode = II->getBaseOpcodeFor(Desc);
00480   switch (Desc->TSFlags & X86II::FormMask) {
00481   default: assert(0 && "Unknown FormMask value in X86 MachineCodeEmitter!");
00482   case X86II::Pseudo:
00483     // Remember the current PC offset, this is the PIC relocation
00484     // base address.
00485     switch (Opcode) {
00486     default: 
00487       assert(0 && "psuedo instructions should be removed before code emission");
00488       break;
00489     case TargetInstrInfo::INLINEASM: {
00490       const char* Value = MI.getOperand(0).getSymbolName();
00491       /* We allow inline assembler nodes with empty bodies - they can
00492          implicitly define registers, which is ok for JIT. */
00493       assert((Value[0] == 0) && "JIT does not support inline asm!\n");
00494       break;
00495     }
00496     case TargetInstrInfo::DBG_LABEL:
00497     case TargetInstrInfo::EH_LABEL:
00498       MCE.emitLabel(MI.getOperand(0).getImm());
00499       break;
00500     case TargetInstrInfo::IMPLICIT_DEF:
00501     case TargetInstrInfo::DECLARE:
00502     case X86::DWARF_LOC:
00503     case X86::FP_REG_KILL:
00504       break;
00505     case X86::MOVPC32r: {
00506       // This emits the "call" portion of this pseudo instruction.
00507       MCE.emitByte(BaseOpcode);
00508       emitConstant(0, X86InstrInfo::sizeOfImm(Desc));
00509       // Remember PIC base.
00510       PICBaseOffset = MCE.getCurrentPCOffset();
00511       X86JITInfo *JTI = TM.getJITInfo();
00512       JTI->setPICBase(MCE.getCurrentPCValue());
00513       break;
00514     }
00515     }
00516     CurOp = NumOps;
00517     break;
00518   case X86II::RawFrm:
00519     MCE.emitByte(BaseOpcode);
00520 
00521     if (CurOp != NumOps) {
00522       const MachineOperand &MO = MI.getOperand(CurOp++);
00523 
00524       DOUT << "RawFrm CurOp " << CurOp << "\n";
00525       DOUT << "isMachineBasicBlock " << MO.isMachineBasicBlock() << "\n";
00526       DOUT << "isGlobalAddress " << MO.isGlobalAddress() << "\n";
00527       DOUT << "isExternalSymbol " << MO.isExternalSymbol() << "\n";
00528       DOUT << "isImmediate " << MO.isImmediate() << "\n";
00529 
00530       if (MO.isMachineBasicBlock()) {
00531         emitPCRelativeBlockAddress(MO.getMBB());
00532       } else if (MO.isGlobalAddress()) {
00533         // Assume undefined functions may be outside the Small codespace.
00534         bool NeedStub = 
00535           (Is64BitMode && 
00536               (TM.getCodeModel() == CodeModel::Large ||
00537                TM.getSubtarget<X86Subtarget>().isTargetDarwin())) ||
00538           Opcode == X86::TAILJMPd;
00539         emitGlobalAddress(MO.getGlobal(), X86::reloc_pcrel_word,
00540                           0, 0, NeedStub);
00541       } else if (MO.isExternalSymbol()) {
00542         emitExternalSymbolAddress(MO.getSymbolName(), X86::reloc_pcrel_word);
00543       } else if (MO.isImmediate()) {
00544         emitConstant(MO.getImm(), X86InstrInfo::sizeOfImm(Desc));
00545       } else {
00546         assert(0 && "Unknown RawFrm operand!");
00547       }
00548     }
00549     break;
00550 
00551   case X86II::AddRegFrm:
00552     MCE.emitByte(BaseOpcode + getX86RegNum(MI.getOperand(CurOp++).getReg()));
00553     
00554     if (CurOp != NumOps) {
00555       const MachineOperand &MO1 = MI.getOperand(CurOp++);
00556       unsigned Size = X86InstrInfo::sizeOfImm(Desc);
00557       if (MO1.isImmediate())
00558         emitConstant(MO1.getImm(), Size);
00559       else {
00560         unsigned rt = Is64BitMode ? X86::reloc_pcrel_word
00561           : (IsPIC ? X86::reloc_picrel_word : X86::reloc_absolute_word);
00562         // This should not occur on Darwin for relocatable objects.
00563         if (Opcode == X86::MOV64ri)
00564           rt = X86::reloc_absolute_dword;  // FIXME: add X86II flag?
00565         if (MO1.isGlobalAddress()) {
00566           bool NeedStub = isa<Function>(MO1.getGlobal());
00567           bool isLazy = gvNeedsLazyPtr(MO1.getGlobal());
00568           emitGlobalAddress(MO1.getGlobal(), rt, MO1.getOffset(), 0,
00569                             NeedStub, isLazy);
00570         } else if (MO1.isExternalSymbol())
00571           emitExternalSymbolAddress(MO1.getSymbolName(), rt);
00572         else if (MO1.isConstantPoolIndex())
00573           emitConstPoolAddress(MO1.getIndex(), rt);
00574         else if (MO1.isJumpTableIndex())
00575           emitJumpTableAddress(MO1.getIndex(), rt);
00576       }
00577     }
00578     break;
00579 
00580   case X86II::MRMDestReg: {
00581     MCE.emitByte(BaseOpcode);
00582     emitRegModRMByte(MI.getOperand(CurOp).getReg(),
00583                      getX86RegNum(MI.getOperand(CurOp+1).getReg()));
00584     CurOp += 2;
00585     if (CurOp != NumOps)
00586       emitConstant(MI.getOperand(CurOp++).getImm(), X86InstrInfo::sizeOfImm(Desc));
00587     break;
00588   }
00589   case X86II::MRMDestMem: {
00590     MCE.emitByte(BaseOpcode);
00591     emitMemModRMByte(MI, CurOp, getX86RegNum(MI.getOperand(CurOp+4).getReg()));
00592     CurOp += 5;
00593     if (CurOp != NumOps)
00594       emitConstant(MI.getOperand(CurOp++).getImm(), X86InstrInfo::sizeOfImm(Desc));
00595     break;
00596   }
00597 
00598   case X86II::MRMSrcReg:
00599     MCE.emitByte(BaseOpcode);
00600     emitRegModRMByte(MI.getOperand(CurOp+1).getReg(),
00601                      getX86RegNum(MI.getOperand(CurOp).getReg()));
00602     CurOp += 2;
00603     if (CurOp != NumOps)
00604       emitConstant(MI.getOperand(CurOp++).getImm(), X86InstrInfo::sizeOfImm(Desc));
00605     break;
00606 
00607   case X86II::MRMSrcMem: {
00608     intptr_t PCAdj = (CurOp+5 != NumOps) ? X86InstrInfo::sizeOfImm(Desc) : 0;
00609 
00610     MCE.emitByte(BaseOpcode);
00611     emitMemModRMByte(MI, CurOp+1, getX86RegNum(MI.getOperand(CurOp).getReg()),
00612                      PCAdj);
00613     CurOp += 5;
00614     if (CurOp != NumOps)
00615       emitConstant(MI.getOperand(CurOp++).getImm(), X86InstrInfo::sizeOfImm(Desc));
00616     break;
00617   }
00618 
00619   case X86II::MRM0r: case X86II::MRM1r:
00620   case X86II::MRM2r: case X86II::MRM3r:
00621   case X86II::MRM4r: case X86II::MRM5r:
00622   case X86II::MRM6r: case X86II::MRM7r:
00623     MCE.emitByte(BaseOpcode);
00624     emitRegModRMByte(MI.getOperand(CurOp++).getReg(),
00625                      (Desc->TSFlags & X86II::FormMask)-X86II::MRM0r);
00626 
00627     if (CurOp != NumOps) {
00628       const MachineOperand &MO1 = MI.getOperand(CurOp++);
00629       unsigned Size = X86InstrInfo::sizeOfImm(Desc);
00630       if (MO1.isImmediate())
00631         emitConstant(MO1.getImm(), Size);
00632       else {
00633         unsigned rt = Is64BitMode ? X86::reloc_pcrel_word
00634           : (IsPIC ? X86::reloc_picrel_word : X86::reloc_absolute_word);
00635         if (Opcode == X86::MOV64ri32)
00636           rt = X86::reloc_absolute_word;  // FIXME: add X86II flag?
00637         if (MO1.isGlobalAddress()) {
00638           bool NeedStub = isa<Function>(MO1.getGlobal());
00639           bool isLazy = gvNeedsLazyPtr(MO1.getGlobal());
00640           emitGlobalAddress(MO1.getGlobal(), rt, MO1.getOffset(), 0,
00641                             NeedStub, isLazy);
00642         } else if (MO1.isExternalSymbol())
00643           emitExternalSymbolAddress(MO1.getSymbolName(), rt);
00644         else if (MO1.isConstantPoolIndex())
00645           emitConstPoolAddress(MO1.getIndex(), rt);
00646         else if (MO1.isJumpTableIndex())
00647           emitJumpTableAddress(MO1.getIndex(), rt);
00648       }
00649     }
00650     break;
00651 
00652   case X86II::MRM0m: case X86II::MRM1m:
00653   case X86II::MRM2m: case X86II::MRM3m:
00654   case X86II::MRM4m: case X86II::MRM5m:
00655   case X86II::MRM6m: case X86II::MRM7m: {
00656     intptr_t PCAdj = (CurOp+4 != NumOps) ?
00657       (MI.getOperand(CurOp+4).isImmediate() ? X86InstrInfo::sizeOfImm(Desc) : 4) : 0;
00658 
00659     MCE.emitByte(BaseOpcode);
00660     emitMemModRMByte(MI, CurOp, (Desc->TSFlags & X86II::FormMask)-X86II::MRM0m,
00661                      PCAdj);
00662     CurOp += 4;
00663 
00664     if (CurOp != NumOps) {
00665       const MachineOperand &MO = MI.getOperand(CurOp++);
00666       unsigned Size = X86InstrInfo::sizeOfImm(Desc);
00667       if (MO.isImmediate())
00668         emitConstant(MO.getImm(), Size);
00669       else {
00670         unsigned rt = Is64BitMode ? X86::reloc_pcrel_word
00671           : (IsPIC ? X86::reloc_picrel_word : X86::reloc_absolute_word);
00672         if (Opcode == X86::MOV64mi32)
00673           rt = X86::reloc_absolute_word;  // FIXME: add X86II flag?
00674         if (MO.isGlobalAddress()) {
00675           bool NeedStub = isa<Function>(MO.getGlobal());
00676           bool isLazy = gvNeedsLazyPtr(MO.getGlobal());
00677           emitGlobalAddress(MO.getGlobal(), rt, MO.getOffset(), 0,
00678                             NeedStub, isLazy);
00679         } else if (MO.isExternalSymbol())
00680           emitExternalSymbolAddress(MO.getSymbolName(), rt);
00681         else if (MO.isConstantPoolIndex())
00682           emitConstPoolAddress(MO.getIndex(), rt);
00683         else if (MO.isJumpTableIndex())
00684           emitJumpTableAddress(MO.getIndex(), rt);
00685       }
00686     }
00687     break;
00688   }
00689 
00690   case X86II::MRMInitReg:
00691     MCE.emitByte(BaseOpcode);
00692     // Duplicate register, used by things like MOV8r0 (aka xor reg,reg).
00693     emitRegModRMByte(MI.getOperand(CurOp).getReg(),
00694                      getX86RegNum(MI.getOperand(CurOp).getReg()));
00695     ++CurOp;
00696     break;
00697   }
00698 
00699   if (!Desc->isVariadic() && CurOp != NumOps) {
00700     cerr << "Cannot encode: ";
00701     MI.dump();
00702     cerr << '\n';
00703     abort();
00704   }
00705 }



This web site is hosted by the Computer Science Department at the University of Illinois at Urbana-Champaign.