LLVM API Documentation
00001 //===-- 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 "X86.h" 00017 #include "X86InstrInfo.h" 00018 #include "X86JITInfo.h" 00019 #include "X86Relocations.h" 00020 #include "X86Subtarget.h" 00021 #include "X86TargetMachine.h" 00022 #include "llvm/ADT/Statistic.h" 00023 #include "llvm/CodeGen/JITCodeEmitter.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/IR/LLVMContext.h" 00029 #include "llvm/MC/MCCodeEmitter.h" 00030 #include "llvm/MC/MCExpr.h" 00031 #include "llvm/MC/MCInst.h" 00032 #include "llvm/PassManager.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 Emitter : public MachineFunctionPass { 00044 const X86InstrInfo *II; 00045 const DataLayout *TD; 00046 X86TargetMachine &TM; 00047 CodeEmitter &MCE; 00048 MachineModuleInfo *MMI; 00049 intptr_t PICBaseOffset; 00050 bool Is64BitMode; 00051 bool IsPIC; 00052 public: 00053 static char ID; 00054 explicit Emitter(X86TargetMachine &tm, CodeEmitter &mce) 00055 : MachineFunctionPass(ID), II(0), TD(0), TM(tm), 00056 MCE(mce), PICBaseOffset(0), Is64BitMode(false), 00057 IsPIC(TM.getRelocationModel() == Reloc::PIC_) {} 00058 00059 bool runOnMachineFunction(MachineFunction &MF); 00060 00061 virtual const char *getPassName() const { 00062 return "X86 Machine Code Emitter"; 00063 } 00064 00065 void emitOpcodePrefix(uint64_t TSFlags, int MemOperand, 00066 const MachineInstr &MI, 00067 const MCInstrDesc *Desc) const; 00068 00069 void emitVEXOpcodePrefix(uint64_t TSFlags, int MemOperand, 00070 const MachineInstr &MI, 00071 const MCInstrDesc *Desc) const; 00072 00073 void emitSegmentOverridePrefix(uint64_t TSFlags, 00074 int MemOperand, 00075 const MachineInstr &MI) const; 00076 00077 void emitInstruction(MachineInstr &MI, const MCInstrDesc *Desc); 00078 00079 void getAnalysisUsage(AnalysisUsage &AU) const { 00080 AU.setPreservesAll(); 00081 AU.addRequired<MachineModuleInfo>(); 00082 MachineFunctionPass::getAnalysisUsage(AU); 00083 } 00084 00085 private: 00086 void emitPCRelativeBlockAddress(MachineBasicBlock *MBB); 00087 void emitGlobalAddress(const GlobalValue *GV, unsigned Reloc, 00088 intptr_t Disp = 0, intptr_t PCAdj = 0, 00089 bool Indirect = false); 00090 void emitExternalSymbolAddress(const char *ES, unsigned Reloc); 00091 void emitConstPoolAddress(unsigned CPI, unsigned Reloc, intptr_t Disp = 0, 00092 intptr_t PCAdj = 0); 00093 void emitJumpTableAddress(unsigned JTI, unsigned Reloc, 00094 intptr_t PCAdj = 0); 00095 00096 void emitDisplacementField(const MachineOperand *RelocOp, int DispVal, 00097 intptr_t Adj = 0, bool IsPCRel = true); 00098 00099 void emitRegModRMByte(unsigned ModRMReg, unsigned RegOpcodeField); 00100 void emitRegModRMByte(unsigned RegOpcodeField); 00101 void emitSIBByte(unsigned SS, unsigned Index, unsigned Base); 00102 void emitConstant(uint64_t Val, unsigned Size); 00103 00104 void emitMemModRMByte(const MachineInstr &MI, 00105 unsigned Op, unsigned RegOpcodeField, 00106 intptr_t PCAdj = 0); 00107 00108 unsigned getX86RegNum(unsigned RegNo) const { 00109 const TargetRegisterInfo *TRI = TM.getRegisterInfo(); 00110 return TRI->getEncodingValue(RegNo) & 0x7; 00111 } 00112 00113 unsigned char getVEXRegisterEncoding(const MachineInstr &MI, 00114 unsigned OpNum) const; 00115 }; 00116 00117 template<class CodeEmitter> 00118 char Emitter<CodeEmitter>::ID = 0; 00119 } // end anonymous namespace. 00120 00121 /// createX86CodeEmitterPass - Return a pass that emits the collected X86 code 00122 /// to the specified JITCodeEmitter object. 00123 FunctionPass *llvm::createX86JITCodeEmitterPass(X86TargetMachine &TM, 00124 JITCodeEmitter &JCE) { 00125 return new Emitter<JITCodeEmitter>(TM, JCE); 00126 } 00127 00128 template<class CodeEmitter> 00129 bool Emitter<CodeEmitter>::runOnMachineFunction(MachineFunction &MF) { 00130 MMI = &getAnalysis<MachineModuleInfo>(); 00131 MCE.setModuleInfo(MMI); 00132 00133 II = TM.getInstrInfo(); 00134 TD = TM.getDataLayout(); 00135 Is64BitMode = TM.getSubtarget<X86Subtarget>().is64Bit(); 00136 IsPIC = TM.getRelocationModel() == Reloc::PIC_; 00137 00138 do { 00139 DEBUG(dbgs() << "JITTing function '" << MF.getName() << "'\n"); 00140 MCE.startFunction(MF); 00141 for (MachineFunction::iterator MBB = MF.begin(), E = MF.end(); 00142 MBB != E; ++MBB) { 00143 MCE.StartMachineBasicBlock(MBB); 00144 for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end(); 00145 I != E; ++I) { 00146 const MCInstrDesc &Desc = I->getDesc(); 00147 emitInstruction(*I, &Desc); 00148 // MOVPC32r is basically a call plus a pop instruction. 00149 if (Desc.getOpcode() == X86::MOVPC32r) 00150 emitInstruction(*I, &II->get(X86::POP32r)); 00151 ++NumEmitted; // Keep track of the # of mi's emitted 00152 } 00153 } 00154 } while (MCE.finishFunction(MF)); 00155 00156 return false; 00157 } 00158 00159 /// determineREX - Determine if the MachineInstr has to be encoded with a X86-64 00160 /// REX prefix which specifies 1) 64-bit instructions, 2) non-default operand 00161 /// size, and 3) use of X86-64 extended registers. 00162 static unsigned determineREX(const MachineInstr &MI) { 00163 unsigned REX = 0; 00164 const MCInstrDesc &Desc = MI.getDesc(); 00165 00166 // Pseudo instructions do not need REX prefix byte. 00167 if ((Desc.TSFlags & X86II::FormMask) == X86II::Pseudo) 00168 return 0; 00169 if (Desc.TSFlags & X86II::REX_W) 00170 REX |= 1 << 3; 00171 00172 unsigned NumOps = Desc.getNumOperands(); 00173 if (NumOps) { 00174 bool isTwoAddr = NumOps > 1 && 00175 Desc.getOperandConstraint(1, MCOI::TIED_TO) != -1; 00176 00177 // If it accesses SPL, BPL, SIL, or DIL, then it requires a 0x40 REX prefix. 00178 unsigned i = isTwoAddr ? 1 : 0; 00179 for (unsigned e = NumOps; i != e; ++i) { 00180 const MachineOperand& MO = MI.getOperand(i); 00181 if (MO.isReg()) { 00182 unsigned Reg = MO.getReg(); 00183 if (X86II::isX86_64NonExtLowByteReg(Reg)) 00184 REX |= 0x40; 00185 } 00186 } 00187 00188 switch (Desc.TSFlags & X86II::FormMask) { 00189 case X86II::MRMInitReg: 00190 if (X86InstrInfo::isX86_64ExtendedReg(MI.getOperand(0))) 00191 REX |= (1 << 0) | (1 << 2); 00192 break; 00193 case X86II::MRMSrcReg: { 00194 if (X86InstrInfo::isX86_64ExtendedReg(MI.getOperand(0))) 00195 REX |= 1 << 2; 00196 i = isTwoAddr ? 2 : 1; 00197 for (unsigned e = NumOps; i != e; ++i) { 00198 const MachineOperand& MO = MI.getOperand(i); 00199 if (X86InstrInfo::isX86_64ExtendedReg(MO)) 00200 REX |= 1 << 0; 00201 } 00202 break; 00203 } 00204 case X86II::MRMSrcMem: { 00205 if (X86InstrInfo::isX86_64ExtendedReg(MI.getOperand(0))) 00206 REX |= 1 << 2; 00207 unsigned Bit = 0; 00208 i = isTwoAddr ? 2 : 1; 00209 for (; i != NumOps; ++i) { 00210 const MachineOperand& MO = MI.getOperand(i); 00211 if (MO.isReg()) { 00212 if (X86InstrInfo::isX86_64ExtendedReg(MO)) 00213 REX |= 1 << Bit; 00214 Bit++; 00215 } 00216 } 00217 break; 00218 } 00219 case X86II::MRM0m: case X86II::MRM1m: 00220 case X86II::MRM2m: case X86II::MRM3m: 00221 case X86II::MRM4m: case X86II::MRM5m: 00222 case X86II::MRM6m: case X86II::MRM7m: 00223 case X86II::MRMDestMem: { 00224 unsigned e = (isTwoAddr ? X86::AddrNumOperands+1 : X86::AddrNumOperands); 00225 i = isTwoAddr ? 1 : 0; 00226 if (NumOps > e && X86InstrInfo::isX86_64ExtendedReg(MI.getOperand(e))) 00227 REX |= 1 << 2; 00228 unsigned Bit = 0; 00229 for (; i != e; ++i) { 00230 const MachineOperand& MO = MI.getOperand(i); 00231 if (MO.isReg()) { 00232 if (X86InstrInfo::isX86_64ExtendedReg(MO)) 00233 REX |= 1 << Bit; 00234 Bit++; 00235 } 00236 } 00237 break; 00238 } 00239 default: { 00240 if (X86InstrInfo::isX86_64ExtendedReg(MI.getOperand(0))) 00241 REX |= 1 << 0; 00242 i = isTwoAddr ? 2 : 1; 00243 for (unsigned e = NumOps; i != e; ++i) { 00244 const MachineOperand& MO = MI.getOperand(i); 00245 if (X86InstrInfo::isX86_64ExtendedReg(MO)) 00246 REX |= 1 << 2; 00247 } 00248 break; 00249 } 00250 } 00251 } 00252 return REX; 00253 } 00254 00255 00256 /// emitPCRelativeBlockAddress - This method keeps track of the information 00257 /// necessary to resolve the address of this block later and emits a dummy 00258 /// value. 00259 /// 00260 template<class CodeEmitter> 00261 void Emitter<CodeEmitter>::emitPCRelativeBlockAddress(MachineBasicBlock *MBB) { 00262 // Remember where this reference was and where it is to so we can 00263 // deal with it later. 00264 MCE.addRelocation(MachineRelocation::getBB(MCE.getCurrentPCOffset(), 00265 X86::reloc_pcrel_word, MBB)); 00266 MCE.emitWordLE(0); 00267 } 00268 00269 /// emitGlobalAddress - Emit the specified address to the code stream assuming 00270 /// this is part of a "take the address of a global" instruction. 00271 /// 00272 template<class CodeEmitter> 00273 void Emitter<CodeEmitter>::emitGlobalAddress(const GlobalValue *GV, 00274 unsigned Reloc, 00275 intptr_t Disp /* = 0 */, 00276 intptr_t PCAdj /* = 0 */, 00277 bool Indirect /* = false */) { 00278 intptr_t RelocCST = Disp; 00279 if (Reloc == X86::reloc_picrel_word) 00280 RelocCST = PICBaseOffset; 00281 else if (Reloc == X86::reloc_pcrel_word) 00282 RelocCST = PCAdj; 00283 MachineRelocation MR = Indirect 00284 ? MachineRelocation::getIndirectSymbol(MCE.getCurrentPCOffset(), Reloc, 00285 const_cast<GlobalValue *>(GV), 00286 RelocCST, false) 00287 : MachineRelocation::getGV(MCE.getCurrentPCOffset(), Reloc, 00288 const_cast<GlobalValue *>(GV), RelocCST, false); 00289 MCE.addRelocation(MR); 00290 // The relocated value will be added to the displacement 00291 if (Reloc == X86::reloc_absolute_dword) 00292 MCE.emitDWordLE(Disp); 00293 else 00294 MCE.emitWordLE((int32_t)Disp); 00295 } 00296 00297 /// emitExternalSymbolAddress - Arrange for the address of an external symbol to 00298 /// be emitted to the current location in the function, and allow it to be PC 00299 /// relative. 00300 template<class CodeEmitter> 00301 void Emitter<CodeEmitter>::emitExternalSymbolAddress(const char *ES, 00302 unsigned Reloc) { 00303 intptr_t RelocCST = (Reloc == X86::reloc_picrel_word) ? PICBaseOffset : 0; 00304 00305 // X86 never needs stubs because instruction selection will always pick 00306 // an instruction sequence that is large enough to hold any address 00307 // to a symbol. 00308 // (see X86ISelLowering.cpp, near 2039: X86TargetLowering::LowerCall) 00309 bool NeedStub = false; 00310 MCE.addRelocation(MachineRelocation::getExtSym(MCE.getCurrentPCOffset(), 00311 Reloc, ES, RelocCST, 00312 0, NeedStub)); 00313 if (Reloc == X86::reloc_absolute_dword) 00314 MCE.emitDWordLE(0); 00315 else 00316 MCE.emitWordLE(0); 00317 } 00318 00319 /// emitConstPoolAddress - Arrange for the address of an constant pool 00320 /// to be emitted to the current location in the function, and allow it to be PC 00321 /// relative. 00322 template<class CodeEmitter> 00323 void Emitter<CodeEmitter>::emitConstPoolAddress(unsigned CPI, unsigned Reloc, 00324 intptr_t Disp /* = 0 */, 00325 intptr_t PCAdj /* = 0 */) { 00326 intptr_t RelocCST = 0; 00327 if (Reloc == X86::reloc_picrel_word) 00328 RelocCST = PICBaseOffset; 00329 else if (Reloc == X86::reloc_pcrel_word) 00330 RelocCST = PCAdj; 00331 MCE.addRelocation(MachineRelocation::getConstPool(MCE.getCurrentPCOffset(), 00332 Reloc, CPI, RelocCST)); 00333 // The relocated value will be added to the displacement 00334 if (Reloc == X86::reloc_absolute_dword) 00335 MCE.emitDWordLE(Disp); 00336 else 00337 MCE.emitWordLE((int32_t)Disp); 00338 } 00339 00340 /// emitJumpTableAddress - Arrange for the address of a jump table to 00341 /// be emitted to the current location in the function, and allow it to be PC 00342 /// relative. 00343 template<class CodeEmitter> 00344 void Emitter<CodeEmitter>::emitJumpTableAddress(unsigned JTI, unsigned Reloc, 00345 intptr_t PCAdj /* = 0 */) { 00346 intptr_t RelocCST = 0; 00347 if (Reloc == X86::reloc_picrel_word) 00348 RelocCST = PICBaseOffset; 00349 else if (Reloc == X86::reloc_pcrel_word) 00350 RelocCST = PCAdj; 00351 MCE.addRelocation(MachineRelocation::getJumpTable(MCE.getCurrentPCOffset(), 00352 Reloc, JTI, RelocCST)); 00353 // The relocated value will be added to the displacement 00354 if (Reloc == X86::reloc_absolute_dword) 00355 MCE.emitDWordLE(0); 00356 else 00357 MCE.emitWordLE(0); 00358 } 00359 00360 inline static unsigned char ModRMByte(unsigned Mod, unsigned RegOpcode, 00361 unsigned RM) { 00362 assert(Mod < 4 && RegOpcode < 8 && RM < 8 && "ModRM Fields out of range!"); 00363 return RM | (RegOpcode << 3) | (Mod << 6); 00364 } 00365 00366 template<class CodeEmitter> 00367 void Emitter<CodeEmitter>::emitRegModRMByte(unsigned ModRMReg, 00368 unsigned RegOpcodeFld){ 00369 MCE.emitByte(ModRMByte(3, RegOpcodeFld, getX86RegNum(ModRMReg))); 00370 } 00371 00372 template<class CodeEmitter> 00373 void Emitter<CodeEmitter>::emitRegModRMByte(unsigned RegOpcodeFld) { 00374 MCE.emitByte(ModRMByte(3, RegOpcodeFld, 0)); 00375 } 00376 00377 template<class CodeEmitter> 00378 void Emitter<CodeEmitter>::emitSIBByte(unsigned SS, 00379 unsigned Index, 00380 unsigned Base) { 00381 // SIB byte is in the same format as the ModRMByte... 00382 MCE.emitByte(ModRMByte(SS, Index, Base)); 00383 } 00384 00385 template<class CodeEmitter> 00386 void Emitter<CodeEmitter>::emitConstant(uint64_t Val, unsigned Size) { 00387 // Output the constant in little endian byte order... 00388 for (unsigned i = 0; i != Size; ++i) { 00389 MCE.emitByte(Val & 255); 00390 Val >>= 8; 00391 } 00392 } 00393 00394 /// isDisp8 - Return true if this signed displacement fits in a 8-bit 00395 /// sign-extended field. 00396 static bool isDisp8(int Value) { 00397 return Value == (signed char)Value; 00398 } 00399 00400 static bool gvNeedsNonLazyPtr(const MachineOperand &GVOp, 00401 const TargetMachine &TM) { 00402 // For Darwin-64, simulate the linktime GOT by using the same non-lazy-pointer 00403 // mechanism as 32-bit mode. 00404 if (TM.getSubtarget<X86Subtarget>().is64Bit() && 00405 !TM.getSubtarget<X86Subtarget>().isTargetDarwin()) 00406 return false; 00407 00408 // Return true if this is a reference to a stub containing the address of the 00409 // global, not the global itself. 00410 return isGlobalStubReference(GVOp.getTargetFlags()); 00411 } 00412 00413 template<class CodeEmitter> 00414 void Emitter<CodeEmitter>::emitDisplacementField(const MachineOperand *RelocOp, 00415 int DispVal, 00416 intptr_t Adj /* = 0 */, 00417 bool IsPCRel /* = true */) { 00418 // If this is a simple integer displacement that doesn't require a relocation, 00419 // emit it now. 00420 if (!RelocOp) { 00421 emitConstant(DispVal, 4); 00422 return; 00423 } 00424 00425 // Otherwise, this is something that requires a relocation. Emit it as such 00426 // now. 00427 unsigned RelocType = Is64BitMode ? 00428 (IsPCRel ? X86::reloc_pcrel_word : X86::reloc_absolute_word_sext) 00429 : (IsPIC ? X86::reloc_picrel_word : X86::reloc_absolute_word); 00430 if (RelocOp->isGlobal()) { 00431 // In 64-bit static small code model, we could potentially emit absolute. 00432 // But it's probably not beneficial. If the MCE supports using RIP directly 00433 // do it, otherwise fallback to absolute (this is determined by IsPCRel). 00434 // 89 05 00 00 00 00 mov %eax,0(%rip) # PC-relative 00435 // 89 04 25 00 00 00 00 mov %eax,0x0 # Absolute 00436 bool Indirect = gvNeedsNonLazyPtr(*RelocOp, TM); 00437 emitGlobalAddress(RelocOp->getGlobal(), RelocType, RelocOp->getOffset(), 00438 Adj, Indirect); 00439 } else if (RelocOp->isSymbol()) { 00440 emitExternalSymbolAddress(RelocOp->getSymbolName(), RelocType); 00441 } else if (RelocOp->isCPI()) { 00442 emitConstPoolAddress(RelocOp->getIndex(), RelocType, 00443 RelocOp->getOffset(), Adj); 00444 } else { 00445 assert(RelocOp->isJTI() && "Unexpected machine operand!"); 00446 emitJumpTableAddress(RelocOp->getIndex(), RelocType, Adj); 00447 } 00448 } 00449 00450 template<class CodeEmitter> 00451 void Emitter<CodeEmitter>::emitMemModRMByte(const MachineInstr &MI, 00452 unsigned Op,unsigned RegOpcodeField, 00453 intptr_t PCAdj) { 00454 const MachineOperand &Op3 = MI.getOperand(Op+3); 00455 int DispVal = 0; 00456 const MachineOperand *DispForReloc = 0; 00457 00458 // Figure out what sort of displacement we have to handle here. 00459 if (Op3.isGlobal()) { 00460 DispForReloc = &Op3; 00461 } else if (Op3.isSymbol()) { 00462 DispForReloc = &Op3; 00463 } else if (Op3.isCPI()) { 00464 if (!MCE.earlyResolveAddresses() || Is64BitMode || IsPIC) { 00465 DispForReloc = &Op3; 00466 } else { 00467 DispVal += MCE.getConstantPoolEntryAddress(Op3.getIndex()); 00468 DispVal += Op3.getOffset(); 00469 } 00470 } else if (Op3.isJTI()) { 00471 if (!MCE.earlyResolveAddresses() || Is64BitMode || IsPIC) { 00472 DispForReloc = &Op3; 00473 } else { 00474 DispVal += MCE.getJumpTableEntryAddress(Op3.getIndex()); 00475 } 00476 } else { 00477 DispVal = Op3.getImm(); 00478 } 00479 00480 const MachineOperand &Base = MI.getOperand(Op); 00481 const MachineOperand &Scale = MI.getOperand(Op+1); 00482 const MachineOperand &IndexReg = MI.getOperand(Op+2); 00483 00484 unsigned BaseReg = Base.getReg(); 00485 00486 // Handle %rip relative addressing. 00487 if (BaseReg == X86::RIP || 00488 (Is64BitMode && DispForReloc)) { // [disp32+RIP] in X86-64 mode 00489 assert(IndexReg.getReg() == 0 && Is64BitMode && 00490 "Invalid rip-relative address"); 00491 MCE.emitByte(ModRMByte(0, RegOpcodeField, 5)); 00492 emitDisplacementField(DispForReloc, DispVal, PCAdj, true); 00493 return; 00494 } 00495 00496 // Indicate that the displacement will use an pcrel or absolute reference 00497 // by default. MCEs able to resolve addresses on-the-fly use pcrel by default 00498 // while others, unless explicit asked to use RIP, use absolute references. 00499 bool IsPCRel = MCE.earlyResolveAddresses() ? true : false; 00500 00501 // Is a SIB byte needed? 00502 // If no BaseReg, issue a RIP relative instruction only if the MCE can 00503 // resolve addresses on-the-fly, otherwise use SIB (Intel Manual 2A, table 00504 // 2-7) and absolute references. 00505 unsigned BaseRegNo = -1U; 00506 if (BaseReg != 0 && BaseReg != X86::RIP) 00507 BaseRegNo = getX86RegNum(BaseReg); 00508 00509 if (// The SIB byte must be used if there is an index register. 00510 IndexReg.getReg() == 0 && 00511 // The SIB byte must be used if the base is ESP/RSP/R12, all of which 00512 // encode to an R/M value of 4, which indicates that a SIB byte is 00513 // present. 00514 BaseRegNo != N86::ESP && 00515 // If there is no base register and we're in 64-bit mode, we need a SIB 00516 // byte to emit an addr that is just 'disp32' (the non-RIP relative form). 00517 (!Is64BitMode || BaseReg != 0)) { 00518 if (BaseReg == 0 || // [disp32] in X86-32 mode 00519 BaseReg == X86::RIP) { // [disp32+RIP] in X86-64 mode 00520 MCE.emitByte(ModRMByte(0, RegOpcodeField, 5)); 00521 emitDisplacementField(DispForReloc, DispVal, PCAdj, true); 00522 return; 00523 } 00524 00525 // If the base is not EBP/ESP and there is no displacement, use simple 00526 // indirect register encoding, this handles addresses like [EAX]. The 00527 // encoding for [EBP] with no displacement means [disp32] so we handle it 00528 // by emitting a displacement of 0 below. 00529 if (!DispForReloc && DispVal == 0 && BaseRegNo != N86::EBP) { 00530 MCE.emitByte(ModRMByte(0, RegOpcodeField, BaseRegNo)); 00531 return; 00532 } 00533 00534 // Otherwise, if the displacement fits in a byte, encode as [REG+disp8]. 00535 if (!DispForReloc && isDisp8(DispVal)) { 00536 MCE.emitByte(ModRMByte(1, RegOpcodeField, BaseRegNo)); 00537 emitConstant(DispVal, 1); 00538 return; 00539 } 00540 00541 // Otherwise, emit the most general non-SIB encoding: [REG+disp32] 00542 MCE.emitByte(ModRMByte(2, RegOpcodeField, BaseRegNo)); 00543 emitDisplacementField(DispForReloc, DispVal, PCAdj, IsPCRel); 00544 return; 00545 } 00546 00547 // Otherwise we need a SIB byte, so start by outputting the ModR/M byte first. 00548 assert(IndexReg.getReg() != X86::ESP && 00549 IndexReg.getReg() != X86::RSP && "Cannot use ESP as index reg!"); 00550 00551 bool ForceDisp32 = false; 00552 bool ForceDisp8 = false; 00553 if (BaseReg == 0) { 00554 // If there is no base register, we emit the special case SIB byte with 00555 // MOD=0, BASE=4, to JUST get the index, scale, and displacement. 00556 MCE.emitByte(ModRMByte(0, RegOpcodeField, 4)); 00557 ForceDisp32 = true; 00558 } else if (DispForReloc) { 00559 // Emit the normal disp32 encoding. 00560 MCE.emitByte(ModRMByte(2, RegOpcodeField, 4)); 00561 ForceDisp32 = true; 00562 } else if (DispVal == 0 && BaseRegNo != N86::EBP) { 00563 // Emit no displacement ModR/M byte 00564 MCE.emitByte(ModRMByte(0, RegOpcodeField, 4)); 00565 } else if (isDisp8(DispVal)) { 00566 // Emit the disp8 encoding... 00567 MCE.emitByte(ModRMByte(1, RegOpcodeField, 4)); 00568 ForceDisp8 = true; // Make sure to force 8 bit disp if Base=EBP 00569 } else { 00570 // Emit the normal disp32 encoding... 00571 MCE.emitByte(ModRMByte(2, RegOpcodeField, 4)); 00572 } 00573 00574 // Calculate what the SS field value should be... 00575 static const unsigned SSTable[] = { ~0U, 0, 1, ~0U, 2, ~0U, ~0U, ~0U, 3 }; 00576 unsigned SS = SSTable[Scale.getImm()]; 00577 00578 if (BaseReg == 0) { 00579 // Handle the SIB byte for the case where there is no base, see Intel 00580 // Manual 2A, table 2-7. The displacement has already been output. 00581 unsigned IndexRegNo; 00582 if (IndexReg.getReg()) 00583 IndexRegNo = getX86RegNum(IndexReg.getReg()); 00584 else // Examples: [ESP+1*<noreg>+4] or [scaled idx]+disp32 (MOD=0,BASE=5) 00585 IndexRegNo = 4; 00586 emitSIBByte(SS, IndexRegNo, 5); 00587 } else { 00588 unsigned BaseRegNo = getX86RegNum(BaseReg); 00589 unsigned IndexRegNo; 00590 if (IndexReg.getReg()) 00591 IndexRegNo = getX86RegNum(IndexReg.getReg()); 00592 else 00593 IndexRegNo = 4; // For example [ESP+1*<noreg>+4] 00594 emitSIBByte(SS, IndexRegNo, BaseRegNo); 00595 } 00596 00597 // Do we need to output a displacement? 00598 if (ForceDisp8) { 00599 emitConstant(DispVal, 1); 00600 } else if (DispVal != 0 || ForceDisp32) { 00601 emitDisplacementField(DispForReloc, DispVal, PCAdj, IsPCRel); 00602 } 00603 } 00604 00605 static const MCInstrDesc *UpdateOp(MachineInstr &MI, const X86InstrInfo *II, 00606 unsigned Opcode) { 00607 const MCInstrDesc *Desc = &II->get(Opcode); 00608 MI.setDesc(*Desc); 00609 return Desc; 00610 } 00611 00612 /// Is16BitMemOperand - Return true if the specified instruction has 00613 /// a 16-bit memory operand. Op specifies the operand # of the memoperand. 00614 static bool Is16BitMemOperand(const MachineInstr &MI, unsigned Op) { 00615 const MachineOperand &BaseReg = MI.getOperand(Op+X86::AddrBaseReg); 00616 const MachineOperand &IndexReg = MI.getOperand(Op+X86::AddrIndexReg); 00617 00618 if ((BaseReg.getReg() != 0 && 00619 X86MCRegisterClasses[X86::GR16RegClassID].contains(BaseReg.getReg())) || 00620 (IndexReg.getReg() != 0 && 00621 X86MCRegisterClasses[X86::GR16RegClassID].contains(IndexReg.getReg()))) 00622 return true; 00623 return false; 00624 } 00625 00626 /// Is32BitMemOperand - Return true if the specified instruction has 00627 /// a 32-bit memory operand. Op specifies the operand # of the memoperand. 00628 static bool Is32BitMemOperand(const MachineInstr &MI, unsigned Op) { 00629 const MachineOperand &BaseReg = MI.getOperand(Op+X86::AddrBaseReg); 00630 const MachineOperand &IndexReg = MI.getOperand(Op+X86::AddrIndexReg); 00631 00632 if ((BaseReg.getReg() != 0 && 00633 X86MCRegisterClasses[X86::GR32RegClassID].contains(BaseReg.getReg())) || 00634 (IndexReg.getReg() != 0 && 00635 X86MCRegisterClasses[X86::GR32RegClassID].contains(IndexReg.getReg()))) 00636 return true; 00637 return false; 00638 } 00639 00640 /// Is64BitMemOperand - Return true if the specified instruction has 00641 /// a 64-bit memory operand. Op specifies the operand # of the memoperand. 00642 #ifndef NDEBUG 00643 static bool Is64BitMemOperand(const MachineInstr &MI, unsigned Op) { 00644 const MachineOperand &BaseReg = MI.getOperand(Op+X86::AddrBaseReg); 00645 const MachineOperand &IndexReg = MI.getOperand(Op+X86::AddrIndexReg); 00646 00647 if ((BaseReg.getReg() != 0 && 00648 X86MCRegisterClasses[X86::GR64RegClassID].contains(BaseReg.getReg())) || 00649 (IndexReg.getReg() != 0 && 00650 X86MCRegisterClasses[X86::GR64RegClassID].contains(IndexReg.getReg()))) 00651 return true; 00652 return false; 00653 } 00654 #endif 00655 00656 template<class CodeEmitter> 00657 void Emitter<CodeEmitter>::emitOpcodePrefix(uint64_t TSFlags, 00658 int MemOperand, 00659 const MachineInstr &MI, 00660 const MCInstrDesc *Desc) const { 00661 // Emit the lock opcode prefix as needed. 00662 if (Desc->TSFlags & X86II::LOCK) 00663 MCE.emitByte(0xF0); 00664 00665 // Emit segment override opcode prefix as needed. 00666 emitSegmentOverridePrefix(TSFlags, MemOperand, MI); 00667 00668 // Emit the repeat opcode prefix as needed. 00669 if ((Desc->TSFlags & X86II::Op0Mask) == X86II::REP) 00670 MCE.emitByte(0xF3); 00671 00672 // Emit the address size opcode prefix as needed. 00673 bool need_address_override; 00674 if (TSFlags & X86II::AdSize) { 00675 need_address_override = true; 00676 } else if (MemOperand == -1) { 00677 need_address_override = false; 00678 } else if (Is64BitMode) { 00679 assert(!Is16BitMemOperand(MI, MemOperand)); 00680 need_address_override = Is32BitMemOperand(MI, MemOperand); 00681 } else { 00682 assert(!Is64BitMemOperand(MI, MemOperand)); 00683 need_address_override = Is16BitMemOperand(MI, MemOperand); 00684 } 00685 00686 if (need_address_override) 00687 MCE.emitByte(0x67); 00688 00689 // Emit the operand size opcode prefix as needed. 00690 if (TSFlags & X86II::OpSize) 00691 MCE.emitByte(0x66); 00692 00693 bool Need0FPrefix = false; 00694 switch (Desc->TSFlags & X86II::Op0Mask) { 00695 case X86II::TB: // Two-byte opcode prefix 00696 case X86II::T8: // 0F 38 00697 case X86II::TA: // 0F 3A 00698 case X86II::A6: // 0F A6 00699 case X86II::A7: // 0F A7 00700 Need0FPrefix = true; 00701 break; 00702 case X86II::REP: break; // already handled. 00703 case X86II::T8XS: // F3 0F 38 00704 case X86II::XS: // F3 0F 00705 MCE.emitByte(0xF3); 00706 Need0FPrefix = true; 00707 break; 00708 case X86II::T8XD: // F2 0F 38 00709 case X86II::TAXD: // F2 0F 3A 00710 case X86II::XD: // F2 0F 00711 MCE.emitByte(0xF2); 00712 Need0FPrefix = true; 00713 break; 00714 case X86II::D8: case X86II::D9: case X86II::DA: case X86II::DB: 00715 case X86II::DC: case X86II::DD: case X86II::DE: case X86II::DF: 00716 MCE.emitByte(0xD8+ 00717 (((Desc->TSFlags & X86II::Op0Mask)-X86II::D8) 00718 >> X86II::Op0Shift)); 00719 break; // Two-byte opcode prefix 00720 default: llvm_unreachable("Invalid prefix!"); 00721 case 0: break; // No prefix! 00722 } 00723 00724 // Handle REX prefix. 00725 if (Is64BitMode) { 00726 if (unsigned REX = determineREX(MI)) 00727 MCE.emitByte(0x40 | REX); 00728 } 00729 00730 // 0x0F escape code must be emitted just before the opcode. 00731 if (Need0FPrefix) 00732 MCE.emitByte(0x0F); 00733 00734 switch (Desc->TSFlags & X86II::Op0Mask) { 00735 case X86II::T8XD: // F2 0F 38 00736 case X86II::T8XS: // F3 0F 38 00737 case X86II::T8: // 0F 38 00738 MCE.emitByte(0x38); 00739 break; 00740 case X86II::TAXD: // F2 0F 38 00741 case X86II::TA: // 0F 3A 00742 MCE.emitByte(0x3A); 00743 break; 00744 case X86II::A6: // 0F A6 00745 MCE.emitByte(0xA6); 00746 break; 00747 case X86II::A7: // 0F A7 00748 MCE.emitByte(0xA7); 00749 break; 00750 } 00751 } 00752 00753 // On regular x86, both XMM0-XMM7 and XMM8-XMM15 are encoded in the range 00754 // 0-7 and the difference between the 2 groups is given by the REX prefix. 00755 // In the VEX prefix, registers are seen sequencially from 0-15 and encoded 00756 // in 1's complement form, example: 00757 // 00758 // ModRM field => XMM9 => 1 00759 // VEX.VVVV => XMM9 => ~9 00760 // 00761 // See table 4-35 of Intel AVX Programming Reference for details. 00762 template<class CodeEmitter> 00763 unsigned char 00764 Emitter<CodeEmitter>::getVEXRegisterEncoding(const MachineInstr &MI, 00765 unsigned OpNum) const { 00766 unsigned SrcReg = MI.getOperand(OpNum).getReg(); 00767 unsigned SrcRegNum = getX86RegNum(MI.getOperand(OpNum).getReg()); 00768 if (X86II::isX86_64ExtendedReg(SrcReg)) 00769 SrcRegNum |= 8; 00770 00771 // The registers represented through VEX_VVVV should 00772 // be encoded in 1's complement form. 00773 return (~SrcRegNum) & 0xf; 00774 } 00775 00776 /// EmitSegmentOverridePrefix - Emit segment override opcode prefix as needed 00777 template<class CodeEmitter> 00778 void Emitter<CodeEmitter>::emitSegmentOverridePrefix(uint64_t TSFlags, 00779 int MemOperand, 00780 const MachineInstr &MI) const { 00781 switch (TSFlags & X86II::SegOvrMask) { 00782 default: llvm_unreachable("Invalid segment!"); 00783 case 0: 00784 // No segment override, check for explicit one on memory operand. 00785 if (MemOperand != -1) { // If the instruction has a memory operand. 00786 switch (MI.getOperand(MemOperand+X86::AddrSegmentReg).getReg()) { 00787 default: llvm_unreachable("Unknown segment register!"); 00788 case 0: break; 00789 case X86::CS: MCE.emitByte(0x2E); break; 00790 case X86::SS: MCE.emitByte(0x36); break; 00791 case X86::DS: MCE.emitByte(0x3E); break; 00792 case X86::ES: MCE.emitByte(0x26); break; 00793 case X86::FS: MCE.emitByte(0x64); break; 00794 case X86::GS: MCE.emitByte(0x65); break; 00795 } 00796 } 00797 break; 00798 case X86II::FS: 00799 MCE.emitByte(0x64); 00800 break; 00801 case X86II::GS: 00802 MCE.emitByte(0x65); 00803 break; 00804 } 00805 } 00806 00807 template<class CodeEmitter> 00808 void Emitter<CodeEmitter>::emitVEXOpcodePrefix(uint64_t TSFlags, 00809 int MemOperand, 00810 const MachineInstr &MI, 00811 const MCInstrDesc *Desc) const { 00812 bool HasVEX_4V = (TSFlags >> X86II::VEXShift) & X86II::VEX_4V; 00813 bool HasVEX_4VOp3 = (TSFlags >> X86II::VEXShift) & X86II::VEX_4VOp3; 00814 bool HasMemOp4 = (TSFlags >> X86II::VEXShift) & X86II::MemOp4; 00815 00816 // VEX_R: opcode externsion equivalent to REX.R in 00817 // 1's complement (inverted) form 00818 // 00819 // 1: Same as REX_R=0 (must be 1 in 32-bit mode) 00820 // 0: Same as REX_R=1 (64 bit mode only) 00821 // 00822 unsigned char VEX_R = 0x1; 00823 00824 // VEX_X: equivalent to REX.X, only used when a 00825 // register is used for index in SIB Byte. 00826 // 00827 // 1: Same as REX.X=0 (must be 1 in 32-bit mode) 00828 // 0: Same as REX.X=1 (64-bit mode only) 00829 unsigned char VEX_X = 0x1; 00830 00831 // VEX_B: 00832 // 00833 // 1: Same as REX_B=0 (ignored in 32-bit mode) 00834 // 0: Same as REX_B=1 (64 bit mode only) 00835 // 00836 unsigned char VEX_B = 0x1; 00837 00838 // VEX_W: opcode specific (use like REX.W, or used for 00839 // opcode extension, or ignored, depending on the opcode byte) 00840 unsigned char VEX_W = 0; 00841 00842 // XOP: Use XOP prefix byte 0x8f instead of VEX. 00843 unsigned char XOP = 0; 00844 00845 // VEX_5M (VEX m-mmmmm field): 00846 // 00847 // 0b00000: Reserved for future use 00848 // 0b00001: implied 0F leading opcode 00849 // 0b00010: implied 0F 38 leading opcode bytes 00850 // 0b00011: implied 0F 3A leading opcode bytes 00851 // 0b00100-0b11111: Reserved for future use 00852 // 0b01000: XOP map select - 08h instructions with imm byte 00853 // 0b10001: XOP map select - 09h instructions with no imm byte 00854 unsigned char VEX_5M = 0x1; 00855 00856 // VEX_4V (VEX vvvv field): a register specifier 00857 // (in 1's complement form) or 1111 if unused. 00858 unsigned char VEX_4V = 0xf; 00859 00860 // VEX_L (Vector Length): 00861 // 00862 // 0: scalar or 128-bit vector 00863 // 1: 256-bit vector 00864 // 00865 unsigned char VEX_L = 0; 00866 00867 // VEX_PP: opcode extension providing equivalent 00868 // functionality of a SIMD prefix 00869 // 00870 // 0b00: None 00871 // 0b01: 66 00872 // 0b10: F3 00873 // 0b11: F2 00874 // 00875 unsigned char VEX_PP = 0; 00876 00877 // Encode the operand size opcode prefix as needed. 00878 if (TSFlags & X86II::OpSize) 00879 VEX_PP = 0x01; 00880 00881 if ((TSFlags >> X86II::VEXShift) & X86II::VEX_W) 00882 VEX_W = 1; 00883 00884 if ((TSFlags >> X86II::VEXShift) & X86II::XOP) 00885 XOP = 1; 00886 00887 if ((TSFlags >> X86II::VEXShift) & X86II::VEX_L) 00888 VEX_L = 1; 00889 00890 switch (TSFlags & X86II::Op0Mask) { 00891 default: llvm_unreachable("Invalid prefix!"); 00892 case X86II::T8: // 0F 38 00893 VEX_5M = 0x2; 00894 break; 00895 case X86II::TA: // 0F 3A 00896 VEX_5M = 0x3; 00897 break; 00898 case X86II::T8XS: // F3 0F 38 00899 VEX_PP = 0x2; 00900 VEX_5M = 0x2; 00901 break; 00902 case X86II::T8XD: // F2 0F 38 00903 VEX_PP = 0x3; 00904 VEX_5M = 0x2; 00905 break; 00906 case X86II::TAXD: // F2 0F 3A 00907 VEX_PP = 0x3; 00908 VEX_5M = 0x3; 00909 break; 00910 case X86II::XS: // F3 0F 00911 VEX_PP = 0x2; 00912 break; 00913 case X86II::XD: // F2 0F 00914 VEX_PP = 0x3; 00915 break; 00916 case X86II::XOP8: 00917 VEX_5M = 0x8; 00918 break; 00919 case X86II::XOP9: 00920 VEX_5M = 0x9; 00921 break; 00922 case X86II::A6: // Bypass: Not used by VEX 00923 case X86II::A7: // Bypass: Not used by VEX 00924 case X86II::TB: // Bypass: Not used by VEX 00925 case 0: 00926 break; // No prefix! 00927 } 00928 00929 00930 // Classify VEX_B, VEX_4V, VEX_R, VEX_X 00931 unsigned NumOps = Desc->getNumOperands(); 00932 unsigned CurOp = 0; 00933 if (NumOps > 1 && Desc->getOperandConstraint(1, MCOI::TIED_TO) == 0) 00934 ++CurOp; 00935 else if (NumOps > 3 && Desc->getOperandConstraint(2, MCOI::TIED_TO) == 0) { 00936 assert(Desc->getOperandConstraint(NumOps - 1, MCOI::TIED_TO) == 1); 00937 // Special case for GATHER with 2 TIED_TO operands 00938 // Skip the first 2 operands: dst, mask_wb 00939 CurOp += 2; 00940 } 00941 00942 switch (TSFlags & X86II::FormMask) { 00943 case X86II::MRMInitReg: 00944 // Duplicate register. 00945 if (X86II::isX86_64ExtendedReg(MI.getOperand(CurOp).getReg())) 00946 VEX_R = 0x0; 00947 00948 if (HasVEX_4V) 00949 VEX_4V = getVEXRegisterEncoding(MI, CurOp); 00950 if (X86II::isX86_64ExtendedReg(MI.getOperand(CurOp).getReg())) 00951 VEX_B = 0x0; 00952 if (HasVEX_4VOp3) 00953 VEX_4V = getVEXRegisterEncoding(MI, CurOp); 00954 break; 00955 case X86II::MRMDestMem: { 00956 // MRMDestMem instructions forms: 00957 // MemAddr, src1(ModR/M) 00958 // MemAddr, src1(VEX_4V), src2(ModR/M) 00959 // MemAddr, src1(ModR/M), imm8 00960 // 00961 if (X86II::isX86_64ExtendedReg(MI.getOperand(X86::AddrBaseReg).getReg())) 00962 VEX_B = 0x0; 00963 if (X86II::isX86_64ExtendedReg(MI.getOperand(X86::AddrIndexReg).getReg())) 00964 VEX_X = 0x0; 00965 00966 CurOp = X86::AddrNumOperands; 00967 if (HasVEX_4V) 00968 VEX_4V = getVEXRegisterEncoding(MI, CurOp++); 00969 00970 const MachineOperand &MO = MI.getOperand(CurOp); 00971 if (MO.isReg() && X86II::isX86_64ExtendedReg(MO.getReg())) 00972 VEX_R = 0x0; 00973 break; 00974 } 00975 case X86II::MRMSrcMem: 00976 // MRMSrcMem instructions forms: 00977 // src1(ModR/M), MemAddr 00978 // src1(ModR/M), src2(VEX_4V), MemAddr 00979 // src1(ModR/M), MemAddr, imm8 00980 // src1(ModR/M), MemAddr, src2(VEX_I8IMM) 00981 // 00982 // FMA4: 00983 // dst(ModR/M.reg), src1(VEX_4V), src2(ModR/M), src3(VEX_I8IMM) 00984 // dst(ModR/M.reg), src1(VEX_4V), src2(VEX_I8IMM), src3(ModR/M), 00985 if (X86II::isX86_64ExtendedReg(MI.getOperand(0).getReg())) 00986 VEX_R = 0x0; 00987 00988 if (HasVEX_4V) 00989 VEX_4V = getVEXRegisterEncoding(MI, 1); 00990 00991 if (X86II::isX86_64ExtendedReg( 00992 MI.getOperand(MemOperand+X86::AddrBaseReg).getReg())) 00993 VEX_B = 0x0; 00994 if (X86II::isX86_64ExtendedReg( 00995 MI.getOperand(MemOperand+X86::AddrIndexReg).getReg())) 00996 VEX_X = 0x0; 00997 00998 if (HasVEX_4VOp3) 00999 VEX_4V = getVEXRegisterEncoding(MI, X86::AddrNumOperands+1); 01000 break; 01001 case X86II::MRM0m: case X86II::MRM1m: 01002 case X86II::MRM2m: case X86II::MRM3m: 01003 case X86II::MRM4m: case X86II::MRM5m: 01004 case X86II::MRM6m: case X86II::MRM7m: { 01005 // MRM[0-9]m instructions forms: 01006 // MemAddr 01007 // src1(VEX_4V), MemAddr 01008 if (HasVEX_4V) 01009 VEX_4V = getVEXRegisterEncoding(MI, 0); 01010 01011 if (X86II::isX86_64ExtendedReg( 01012 MI.getOperand(MemOperand+X86::AddrBaseReg).getReg())) 01013 VEX_B = 0x0; 01014 if (X86II::isX86_64ExtendedReg( 01015 MI.getOperand(MemOperand+X86::AddrIndexReg).getReg())) 01016 VEX_X = 0x0; 01017 break; 01018 } 01019 case X86II::MRMSrcReg: 01020 // MRMSrcReg instructions forms: 01021 // dst(ModR/M), src1(VEX_4V), src2(ModR/M), src3(VEX_I8IMM) 01022 // dst(ModR/M), src1(ModR/M) 01023 // dst(ModR/M), src1(ModR/M), imm8 01024 // 01025 if (X86II::isX86_64ExtendedReg(MI.getOperand(CurOp).getReg())) 01026 VEX_R = 0x0; 01027 CurOp++; 01028 01029 if (HasVEX_4V) 01030 VEX_4V = getVEXRegisterEncoding(MI, CurOp++); 01031 01032 if (HasMemOp4) // Skip second register source (encoded in I8IMM) 01033 CurOp++; 01034 01035 if (X86II::isX86_64ExtendedReg(MI.getOperand(CurOp).getReg())) 01036 VEX_B = 0x0; 01037 CurOp++; 01038 if (HasVEX_4VOp3) 01039 VEX_4V = getVEXRegisterEncoding(MI, CurOp); 01040 break; 01041 case X86II::MRMDestReg: 01042 // MRMDestReg instructions forms: 01043 // dst(ModR/M), src(ModR/M) 01044 // dst(ModR/M), src(ModR/M), imm8 01045 // dst(ModR/M), src1(VEX_4V), src2(ModR/M) 01046 if (X86II::isX86_64ExtendedReg(MI.getOperand(CurOp).getReg())) 01047 VEX_B = 0x0; 01048 CurOp++; 01049 01050 if (HasVEX_4V) 01051 VEX_4V = getVEXRegisterEncoding(MI, CurOp++); 01052 01053 if (X86II::isX86_64ExtendedReg(MI.getOperand(CurOp).getReg())) 01054 VEX_R = 0x0; 01055 break; 01056 case X86II::MRM0r: case X86II::MRM1r: 01057 case X86II::MRM2r: case X86II::MRM3r: 01058 case X86II::MRM4r: case X86II::MRM5r: 01059 case X86II::MRM6r: case X86II::MRM7r: 01060 // MRM0r-MRM7r instructions forms: 01061 // dst(VEX_4V), src(ModR/M), imm8 01062 VEX_4V = getVEXRegisterEncoding(MI, 0); 01063 if (X86II::isX86_64ExtendedReg(MI.getOperand(1).getReg())) 01064 VEX_B = 0x0; 01065 break; 01066 default: // RawFrm 01067 break; 01068 } 01069 01070 // Emit segment override opcode prefix as needed. 01071 emitSegmentOverridePrefix(TSFlags, MemOperand, MI); 01072 01073 // VEX opcode prefix can have 2 or 3 bytes 01074 // 01075 // 3 bytes: 01076 // +-----+ +--------------+ +-------------------+ 01077 // | C4h | | RXB | m-mmmm | | W | vvvv | L | pp | 01078 // +-----+ +--------------+ +-------------------+ 01079 // 2 bytes: 01080 // +-----+ +-------------------+ 01081 // | C5h | | R | vvvv | L | pp | 01082 // +-----+ +-------------------+ 01083 // 01084 unsigned char LastByte = VEX_PP | (VEX_L << 2) | (VEX_4V << 3); 01085 01086 if (VEX_B && VEX_X && !VEX_W && !XOP && (VEX_5M == 1)) { // 2 byte VEX prefix 01087 MCE.emitByte(0xC5); 01088 MCE.emitByte(LastByte | (VEX_R << 7)); 01089 return; 01090 } 01091 01092 // 3 byte VEX prefix 01093 MCE.emitByte(XOP ? 0x8F : 0xC4); 01094 MCE.emitByte(VEX_R << 7 | VEX_X << 6 | VEX_B << 5 | VEX_5M); 01095 MCE.emitByte(LastByte | (VEX_W << 7)); 01096 } 01097 01098 template<class CodeEmitter> 01099 void Emitter<CodeEmitter>::emitInstruction(MachineInstr &MI, 01100 const MCInstrDesc *Desc) { 01101 DEBUG(dbgs() << MI); 01102 01103 // If this is a pseudo instruction, lower it. 01104 switch (Desc->getOpcode()) { 01105 case X86::ADD16rr_DB: Desc = UpdateOp(MI, II, X86::OR16rr); break; 01106 case X86::ADD32rr_DB: Desc = UpdateOp(MI, II, X86::OR32rr); break; 01107 case X86::ADD64rr_DB: Desc = UpdateOp(MI, II, X86::OR64rr); break; 01108 case X86::ADD16ri_DB: Desc = UpdateOp(MI, II, X86::OR16ri); break; 01109 case X86::ADD32ri_DB: Desc = UpdateOp(MI, II, X86::OR32ri); break; 01110 case X86::ADD64ri32_DB: Desc = UpdateOp(MI, II, X86::OR64ri32); break; 01111 case X86::ADD16ri8_DB: Desc = UpdateOp(MI, II, X86::OR16ri8); break; 01112 case X86::ADD32ri8_DB: Desc = UpdateOp(MI, II, X86::OR32ri8); break; 01113 case X86::ADD64ri8_DB: Desc = UpdateOp(MI, II, X86::OR64ri8); break; 01114 case X86::ACQUIRE_MOV8rm: Desc = UpdateOp(MI, II, X86::MOV8rm); break; 01115 case X86::ACQUIRE_MOV16rm: Desc = UpdateOp(MI, II, X86::MOV16rm); break; 01116 case X86::ACQUIRE_MOV32rm: Desc = UpdateOp(MI, II, X86::MOV32rm); break; 01117 case X86::ACQUIRE_MOV64rm: Desc = UpdateOp(MI, II, X86::MOV64rm); break; 01118 case X86::RELEASE_MOV8mr: Desc = UpdateOp(MI, II, X86::MOV8mr); break; 01119 case X86::RELEASE_MOV16mr: Desc = UpdateOp(MI, II, X86::MOV16mr); break; 01120 case X86::RELEASE_MOV32mr: Desc = UpdateOp(MI, II, X86::MOV32mr); break; 01121 case X86::RELEASE_MOV64mr: Desc = UpdateOp(MI, II, X86::MOV64mr); break; 01122 } 01123 01124 01125 MCE.processDebugLoc(MI.getDebugLoc(), true); 01126 01127 unsigned Opcode = Desc->Opcode; 01128 01129 // If this is a two-address instruction, skip one of the register operands. 01130 unsigned NumOps = Desc->getNumOperands(); 01131 unsigned CurOp = 0; 01132 if (NumOps > 1 && Desc->getOperandConstraint(1, MCOI::TIED_TO) == 0) 01133 ++CurOp; 01134 else if (NumOps > 3 && Desc->getOperandConstraint(2, MCOI::TIED_TO) == 0) { 01135 assert(Desc->getOperandConstraint(NumOps - 1, MCOI::TIED_TO) == 1); 01136 // Special case for GATHER with 2 TIED_TO operands 01137 // Skip the first 2 operands: dst, mask_wb 01138 CurOp += 2; 01139 } 01140 01141 uint64_t TSFlags = Desc->TSFlags; 01142 01143 // Is this instruction encoded using the AVX VEX prefix? 01144 bool HasVEXPrefix = (TSFlags >> X86II::VEXShift) & X86II::VEX; 01145 // It uses the VEX.VVVV field? 01146 bool HasVEX_4V = (TSFlags >> X86II::VEXShift) & X86II::VEX_4V; 01147 bool HasVEX_4VOp3 = (TSFlags >> X86II::VEXShift) & X86II::VEX_4VOp3; 01148 bool HasMemOp4 = (TSFlags >> X86II::VEXShift) & X86II::MemOp4; 01149 const unsigned MemOp4_I8IMMOperand = 2; 01150 01151 // Determine where the memory operand starts, if present. 01152 int MemoryOperand = X86II::getMemoryOperandNo(TSFlags, Opcode); 01153 if (MemoryOperand != -1) MemoryOperand += CurOp; 01154 01155 if (!HasVEXPrefix) 01156 emitOpcodePrefix(TSFlags, MemoryOperand, MI, Desc); 01157 else 01158 emitVEXOpcodePrefix(TSFlags, MemoryOperand, MI, Desc); 01159 01160 unsigned char BaseOpcode = X86II::getBaseOpcodeFor(Desc->TSFlags); 01161 switch (TSFlags & X86II::FormMask) { 01162 default: 01163 llvm_unreachable("Unknown FormMask value in X86 MachineCodeEmitter!"); 01164 case X86II::Pseudo: 01165 // Remember the current PC offset, this is the PIC relocation 01166 // base address. 01167 switch (Opcode) { 01168 default: 01169 llvm_unreachable("pseudo instructions should be removed before code" 01170 " emission"); 01171 // Do nothing for Int_MemBarrier - it's just a comment. Add a debug 01172 // to make it slightly easier to see. 01173 case X86::Int_MemBarrier: 01174 DEBUG(dbgs() << "#MEMBARRIER\n"); 01175 break; 01176 01177 case TargetOpcode::INLINEASM: 01178 // We allow inline assembler nodes with empty bodies - they can 01179 // implicitly define registers, which is ok for JIT. 01180 if (MI.getOperand(0).getSymbolName()[0]) 01181 report_fatal_error("JIT does not support inline asm!"); 01182 break; 01183 case TargetOpcode::PROLOG_LABEL: 01184 case TargetOpcode::GC_LABEL: 01185 case TargetOpcode::EH_LABEL: 01186 MCE.emitLabel(MI.getOperand(0).getMCSymbol()); 01187 break; 01188 01189 case TargetOpcode::IMPLICIT_DEF: 01190 case TargetOpcode::KILL: 01191 break; 01192 case X86::MOVPC32r: { 01193 // This emits the "call" portion of this pseudo instruction. 01194 MCE.emitByte(BaseOpcode); 01195 emitConstant(0, X86II::getSizeOfImm(Desc->TSFlags)); 01196 // Remember PIC base. 01197 PICBaseOffset = (intptr_t) MCE.getCurrentPCOffset(); 01198 X86JITInfo *JTI = TM.getJITInfo(); 01199 JTI->setPICBase(MCE.getCurrentPCValue()); 01200 break; 01201 } 01202 } 01203 CurOp = NumOps; 01204 break; 01205 case X86II::RawFrm: { 01206 MCE.emitByte(BaseOpcode); 01207 01208 if (CurOp == NumOps) 01209 break; 01210 01211 const MachineOperand &MO = MI.getOperand(CurOp++); 01212 01213 DEBUG(dbgs() << "RawFrm CurOp " << CurOp << "\n"); 01214 DEBUG(dbgs() << "isMBB " << MO.isMBB() << "\n"); 01215 DEBUG(dbgs() << "isGlobal " << MO.isGlobal() << "\n"); 01216 DEBUG(dbgs() << "isSymbol " << MO.isSymbol() << "\n"); 01217 DEBUG(dbgs() << "isImm " << MO.isImm() << "\n"); 01218 01219 if (MO.isMBB()) { 01220 emitPCRelativeBlockAddress(MO.getMBB()); 01221 break; 01222 } 01223 01224 if (MO.isGlobal()) { 01225 emitGlobalAddress(MO.getGlobal(), X86::reloc_pcrel_word, 01226 MO.getOffset(), 0); 01227 break; 01228 } 01229 01230 if (MO.isSymbol()) { 01231 emitExternalSymbolAddress(MO.getSymbolName(), X86::reloc_pcrel_word); 01232 break; 01233 } 01234 01235 // FIXME: Only used by hackish MCCodeEmitter, remove when dead. 01236 if (MO.isJTI()) { 01237 emitJumpTableAddress(MO.getIndex(), X86::reloc_pcrel_word); 01238 break; 01239 } 01240 01241 assert(MO.isImm() && "Unknown RawFrm operand!"); 01242 if (Opcode == X86::CALLpcrel32 || Opcode == X86::CALL64pcrel32) { 01243 // Fix up immediate operand for pc relative calls. 01244 intptr_t Imm = (intptr_t)MO.getImm(); 01245 Imm = Imm - MCE.getCurrentPCValue() - 4; 01246 emitConstant(Imm, X86II::getSizeOfImm(Desc->TSFlags)); 01247 } else 01248 emitConstant(MO.getImm(), X86II::getSizeOfImm(Desc->TSFlags)); 01249 break; 01250 } 01251 01252 case X86II::AddRegFrm: { 01253 MCE.emitByte(BaseOpcode + 01254 getX86RegNum(MI.getOperand(CurOp++).getReg())); 01255 01256 if (CurOp == NumOps) 01257 break; 01258 01259 const MachineOperand &MO1 = MI.getOperand(CurOp++); 01260 unsigned Size = X86II::getSizeOfImm(Desc->TSFlags); 01261 if (MO1.isImm()) { 01262 emitConstant(MO1.getImm(), Size); 01263 break; 01264 } 01265 01266 unsigned rt = Is64BitMode ? X86::reloc_pcrel_word 01267 : (IsPIC ? X86::reloc_picrel_word : X86::reloc_absolute_word); 01268 if (Opcode == X86::MOV32ri64) 01269 rt = X86::reloc_absolute_word; // FIXME: add X86II flag? 01270 // This should not occur on Darwin for relocatable objects. 01271 if (Opcode == X86::MOV64ri) 01272 rt = X86::reloc_absolute_dword; // FIXME: add X86II flag? 01273 if (MO1.isGlobal()) { 01274 bool Indirect = gvNeedsNonLazyPtr(MO1, TM); 01275 emitGlobalAddress(MO1.getGlobal(), rt, MO1.getOffset(), 0, 01276 Indirect); 01277 } else if (MO1.isSymbol()) 01278 emitExternalSymbolAddress(MO1.getSymbolName(), rt); 01279 else if (MO1.isCPI()) 01280 emitConstPoolAddress(MO1.getIndex(), rt); 01281 else if (MO1.isJTI()) 01282 emitJumpTableAddress(MO1.getIndex(), rt); 01283 break; 01284 } 01285 01286 case X86II::MRMDestReg: { 01287 MCE.emitByte(BaseOpcode); 01288 01289 unsigned SrcRegNum = CurOp+1; 01290 if (HasVEX_4V) // Skip 1st src (which is encoded in VEX_VVVV) 01291 SrcRegNum++; 01292 01293 emitRegModRMByte(MI.getOperand(CurOp).getReg(), 01294 getX86RegNum(MI.getOperand(SrcRegNum).getReg())); 01295 CurOp = SrcRegNum + 1; 01296 break; 01297 } 01298 case X86II::MRMDestMem: { 01299 MCE.emitByte(BaseOpcode); 01300 01301 unsigned SrcRegNum = CurOp + X86::AddrNumOperands; 01302 if (HasVEX_4V) // Skip 1st src (which is encoded in VEX_VVVV) 01303 SrcRegNum++; 01304 emitMemModRMByte(MI, CurOp, 01305 getX86RegNum(MI.getOperand(SrcRegNum).getReg())); 01306 CurOp = SrcRegNum + 1; 01307 break; 01308 } 01309 01310 case X86II::MRMSrcReg: { 01311 MCE.emitByte(BaseOpcode); 01312 01313 unsigned SrcRegNum = CurOp+1; 01314 if (HasVEX_4V) // Skip 1st src (which is encoded in VEX_VVVV) 01315 ++SrcRegNum; 01316 01317 if (HasMemOp4) // Skip 2nd src (which is encoded in I8IMM) 01318 ++SrcRegNum; 01319 01320 emitRegModRMByte(MI.getOperand(SrcRegNum).getReg(), 01321 getX86RegNum(MI.getOperand(CurOp).getReg())); 01322 // 2 operands skipped with HasMemOp4, compensate accordingly 01323 CurOp = HasMemOp4 ? SrcRegNum : SrcRegNum + 1; 01324 if (HasVEX_4VOp3) 01325 ++CurOp; 01326 break; 01327 } 01328 case X86II::MRMSrcMem: { 01329 int AddrOperands = X86::AddrNumOperands; 01330 unsigned FirstMemOp = CurOp+1; 01331 if (HasVEX_4V) { 01332 ++AddrOperands; 01333 ++FirstMemOp; // Skip the register source (which is encoded in VEX_VVVV). 01334 } 01335 if (HasMemOp4) // Skip second register source (encoded in I8IMM) 01336 ++FirstMemOp; 01337 01338 MCE.emitByte(BaseOpcode); 01339 01340 intptr_t PCAdj = (CurOp + AddrOperands + 1 != NumOps) ? 01341 X86II::getSizeOfImm(Desc->TSFlags) : 0; 01342 emitMemModRMByte(MI, FirstMemOp, 01343 getX86RegNum(MI.getOperand(CurOp).getReg()),PCAdj); 01344 CurOp += AddrOperands + 1; 01345 if (HasVEX_4VOp3) 01346 ++CurOp; 01347 break; 01348 } 01349 01350 case X86II::MRM0r: case X86II::MRM1r: 01351 case X86II::MRM2r: case X86II::MRM3r: 01352 case X86II::MRM4r: case X86II::MRM5r: 01353 case X86II::MRM6r: case X86II::MRM7r: { 01354 if (HasVEX_4V) // Skip the register dst (which is encoded in VEX_VVVV). 01355 ++CurOp; 01356 MCE.emitByte(BaseOpcode); 01357 emitRegModRMByte(MI.getOperand(CurOp++).getReg(), 01358 (Desc->TSFlags & X86II::FormMask)-X86II::MRM0r); 01359 01360 if (CurOp == NumOps) 01361 break; 01362 01363 const MachineOperand &MO1 = MI.getOperand(CurOp++); 01364 unsigned Size = X86II::getSizeOfImm(Desc->TSFlags); 01365 if (MO1.isImm()) { 01366 emitConstant(MO1.getImm(), Size); 01367 break; 01368 } 01369 01370 unsigned rt = Is64BitMode ? X86::reloc_pcrel_word 01371 : (IsPIC ? X86::reloc_picrel_word : X86::reloc_absolute_word); 01372 if (Opcode == X86::MOV64ri32) 01373 rt = X86::reloc_absolute_word_sext; // FIXME: add X86II flag? 01374 if (MO1.isGlobal()) { 01375 bool Indirect = gvNeedsNonLazyPtr(MO1, TM); 01376 emitGlobalAddress(MO1.getGlobal(), rt, MO1.getOffset(), 0, 01377 Indirect); 01378 } else if (MO1.isSymbol()) 01379 emitExternalSymbolAddress(MO1.getSymbolName(), rt); 01380 else if (MO1.isCPI()) 01381 emitConstPoolAddress(MO1.getIndex(), rt); 01382 else if (MO1.isJTI()) 01383 emitJumpTableAddress(MO1.getIndex(), rt); 01384 break; 01385 } 01386 01387 case X86II::MRM0m: case X86II::MRM1m: 01388 case X86II::MRM2m: case X86II::MRM3m: 01389 case X86II::MRM4m: case X86II::MRM5m: 01390 case X86II::MRM6m: case X86II::MRM7m: { 01391 if (HasVEX_4V) // Skip the register dst (which is encoded in VEX_VVVV). 01392 ++CurOp; 01393 intptr_t PCAdj = (CurOp + X86::AddrNumOperands != NumOps) ? 01394 (MI.getOperand(CurOp+X86::AddrNumOperands).isImm() ? 01395 X86II::getSizeOfImm(Desc->TSFlags) : 4) : 0; 01396 01397 MCE.emitByte(BaseOpcode); 01398 emitMemModRMByte(MI, CurOp, (Desc->TSFlags & X86II::FormMask)-X86II::MRM0m, 01399 PCAdj); 01400 CurOp += X86::AddrNumOperands; 01401 01402 if (CurOp == NumOps) 01403 break; 01404 01405 const MachineOperand &MO = MI.getOperand(CurOp++); 01406 unsigned Size = X86II::getSizeOfImm(Desc->TSFlags); 01407 if (MO.isImm()) { 01408 emitConstant(MO.getImm(), Size); 01409 break; 01410 } 01411 01412 unsigned rt = Is64BitMode ? X86::reloc_pcrel_word 01413 : (IsPIC ? X86::reloc_picrel_word : X86::reloc_absolute_word); 01414 if (Opcode == X86::MOV64mi32) 01415 rt = X86::reloc_absolute_word_sext; // FIXME: add X86II flag? 01416 if (MO.isGlobal()) { 01417 bool Indirect = gvNeedsNonLazyPtr(MO, TM); 01418 emitGlobalAddress(MO.getGlobal(), rt, MO.getOffset(), 0, 01419 Indirect); 01420 } else if (MO.isSymbol()) 01421 emitExternalSymbolAddress(MO.getSymbolName(), rt); 01422 else if (MO.isCPI()) 01423 emitConstPoolAddress(MO.getIndex(), rt); 01424 else if (MO.isJTI()) 01425 emitJumpTableAddress(MO.getIndex(), rt); 01426 break; 01427 } 01428 01429 case X86II::MRMInitReg: 01430 MCE.emitByte(BaseOpcode); 01431 // Duplicate register, used by things like MOV8r0 (aka xor reg,reg). 01432 emitRegModRMByte(MI.getOperand(CurOp).getReg(), 01433 getX86RegNum(MI.getOperand(CurOp).getReg())); 01434 ++CurOp; 01435 break; 01436 01437 case X86II::MRM_C1: 01438 MCE.emitByte(BaseOpcode); 01439 MCE.emitByte(0xC1); 01440 break; 01441 case X86II::MRM_C8: 01442 MCE.emitByte(BaseOpcode); 01443 MCE.emitByte(0xC8); 01444 break; 01445 case X86II::MRM_C9: 01446 MCE.emitByte(BaseOpcode); 01447 MCE.emitByte(0xC9); 01448 break; 01449 case X86II::MRM_CA: 01450 MCE.emitByte(BaseOpcode); 01451 MCE.emitByte(0xCA); 01452 break; 01453 case X86II::MRM_CB: 01454 MCE.emitByte(BaseOpcode); 01455 MCE.emitByte(0xCB); 01456 break; 01457 case X86II::MRM_E8: 01458 MCE.emitByte(BaseOpcode); 01459 MCE.emitByte(0xE8); 01460 break; 01461 case X86II::MRM_F0: 01462 MCE.emitByte(BaseOpcode); 01463 MCE.emitByte(0xF0); 01464 break; 01465 } 01466 01467 while (CurOp != NumOps && NumOps - CurOp <= 2) { 01468 // The last source register of a 4 operand instruction in AVX is encoded 01469 // in bits[7:4] of a immediate byte. 01470 if ((TSFlags >> X86II::VEXShift) & X86II::VEX_I8IMM) { 01471 const MachineOperand &MO = MI.getOperand(HasMemOp4 ? MemOp4_I8IMMOperand 01472 : CurOp); 01473 ++CurOp; 01474 unsigned RegNum = getX86RegNum(MO.getReg()) << 4; 01475 if (X86II::isX86_64ExtendedReg(MO.getReg())) 01476 RegNum |= 1 << 7; 01477 // If there is an additional 5th operand it must be an immediate, which 01478 // is encoded in bits[3:0] 01479 if (CurOp != NumOps) { 01480 const MachineOperand &MIMM = MI.getOperand(CurOp++); 01481 if (MIMM.isImm()) { 01482 unsigned Val = MIMM.getImm(); 01483 assert(Val < 16 && "Immediate operand value out of range"); 01484 RegNum |= Val; 01485 } 01486 } 01487 emitConstant(RegNum, 1); 01488 } else { 01489 emitConstant(MI.getOperand(CurOp++).getImm(), 01490 X86II::getSizeOfImm(Desc->TSFlags)); 01491 } 01492 } 01493 01494 if (!MI.isVariadic() && CurOp != NumOps) { 01495 #ifndef NDEBUG 01496 dbgs() << "Cannot encode all operands of: " << MI << "\n"; 01497 #endif 01498 llvm_unreachable(0); 01499 } 01500 01501 MCE.processDebugLoc(MI.getDebugLoc(), false); 01502 }