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