1 //===-- X86/X86CodeEmitter.cpp - Convert X86 code to machine code ---------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains the pass that transforms the X86 machine instructions into
11 // relocatable machine code.
13 //===----------------------------------------------------------------------===//
15 #define DEBUG_TYPE "x86-emitter"
16 #include "X86InstrInfo.h"
17 #include "X86JITInfo.h"
18 #include "X86Subtarget.h"
19 #include "X86TargetMachine.h"
20 #include "X86Relocations.h"
22 #include "llvm/LLVMContext.h"
23 #include "llvm/PassManager.h"
24 #include "llvm/CodeGen/JITCodeEmitter.h"
25 #include "llvm/CodeGen/MachineFunctionPass.h"
26 #include "llvm/CodeGen/MachineInstr.h"
27 #include "llvm/CodeGen/MachineModuleInfo.h"
28 #include "llvm/CodeGen/Passes.h"
29 #include "llvm/Function.h"
30 #include "llvm/ADT/Statistic.h"
31 #include "llvm/MC/MCCodeEmitter.h"
32 #include "llvm/MC/MCExpr.h"
33 #include "llvm/MC/MCInst.h"
34 #include "llvm/Support/Debug.h"
35 #include "llvm/Support/ErrorHandling.h"
36 #include "llvm/Support/raw_ostream.h"
37 #include "llvm/Target/TargetOptions.h"
40 STATISTIC(NumEmitted, "Number of machine instructions emitted");
43 template<class CodeEmitter>
44 class Emitter : public MachineFunctionPass {
45 const X86InstrInfo *II;
49 MachineModuleInfo *MMI;
50 intptr_t PICBaseOffset;
55 explicit Emitter(X86TargetMachine &tm, CodeEmitter &mce)
56 : MachineFunctionPass(&ID), II(0), TD(0), TM(tm),
57 MCE(mce), PICBaseOffset(0), Is64BitMode(false),
58 IsPIC(TM.getRelocationModel() == Reloc::PIC_) {}
59 Emitter(X86TargetMachine &tm, CodeEmitter &mce,
60 const X86InstrInfo &ii, const TargetData &td, bool is64)
61 : MachineFunctionPass(&ID), II(&ii), TD(&td), TM(tm),
62 MCE(mce), PICBaseOffset(0), Is64BitMode(is64),
63 IsPIC(TM.getRelocationModel() == Reloc::PIC_) {}
65 bool runOnMachineFunction(MachineFunction &MF);
67 virtual const char *getPassName() const {
68 return "X86 Machine Code Emitter";
71 void emitInstruction(const MachineInstr &MI,
72 const TargetInstrDesc *Desc);
74 void getAnalysisUsage(AnalysisUsage &AU) const {
76 AU.addRequired<MachineModuleInfo>();
77 MachineFunctionPass::getAnalysisUsage(AU);
81 void emitPCRelativeBlockAddress(MachineBasicBlock *MBB);
82 void emitGlobalAddress(const GlobalValue *GV, unsigned Reloc,
83 intptr_t Disp = 0, intptr_t PCAdj = 0,
84 bool Indirect = false);
85 void emitExternalSymbolAddress(const char *ES, unsigned Reloc);
86 void emitConstPoolAddress(unsigned CPI, unsigned Reloc, intptr_t Disp = 0,
88 void emitJumpTableAddress(unsigned JTI, unsigned Reloc,
91 void emitDisplacementField(const MachineOperand *RelocOp, int DispVal,
92 intptr_t Adj = 0, bool IsPCRel = true);
94 void emitRegModRMByte(unsigned ModRMReg, unsigned RegOpcodeField);
95 void emitRegModRMByte(unsigned RegOpcodeField);
96 void emitSIBByte(unsigned SS, unsigned Index, unsigned Base);
97 void emitConstant(uint64_t Val, unsigned Size);
99 void emitMemModRMByte(const MachineInstr &MI,
100 unsigned Op, unsigned RegOpcodeField,
103 unsigned getX86RegNum(unsigned RegNo) const;
106 template<class CodeEmitter>
107 char Emitter<CodeEmitter>::ID = 0;
108 } // end anonymous namespace.
110 /// createX86CodeEmitterPass - Return a pass that emits the collected X86 code
111 /// to the specified templated MachineCodeEmitter object.
112 FunctionPass *llvm::createX86JITCodeEmitterPass(X86TargetMachine &TM,
113 JITCodeEmitter &JCE) {
114 return new Emitter<JITCodeEmitter>(TM, JCE);
117 template<class CodeEmitter>
118 bool Emitter<CodeEmitter>::runOnMachineFunction(MachineFunction &MF) {
119 MMI = &getAnalysis<MachineModuleInfo>();
120 MCE.setModuleInfo(MMI);
122 II = TM.getInstrInfo();
123 TD = TM.getTargetData();
124 Is64BitMode = TM.getSubtarget<X86Subtarget>().is64Bit();
125 IsPIC = TM.getRelocationModel() == Reloc::PIC_;
128 DEBUG(dbgs() << "JITTing function '"
129 << MF.getFunction()->getName() << "'\n");
130 MCE.startFunction(MF);
131 for (MachineFunction::iterator MBB = MF.begin(), E = MF.end();
133 MCE.StartMachineBasicBlock(MBB);
134 for (MachineBasicBlock::const_iterator I = MBB->begin(), E = MBB->end();
136 const TargetInstrDesc &Desc = I->getDesc();
137 emitInstruction(*I, &Desc);
138 // MOVPC32r is basically a call plus a pop instruction.
139 if (Desc.getOpcode() == X86::MOVPC32r)
140 emitInstruction(*I, &II->get(X86::POP32r));
141 NumEmitted++; // Keep track of the # of mi's emitted
144 } while (MCE.finishFunction(MF));
149 /// emitPCRelativeBlockAddress - This method keeps track of the information
150 /// necessary to resolve the address of this block later and emits a dummy
153 template<class CodeEmitter>
154 void Emitter<CodeEmitter>::emitPCRelativeBlockAddress(MachineBasicBlock *MBB) {
155 // Remember where this reference was and where it is to so we can
156 // deal with it later.
157 MCE.addRelocation(MachineRelocation::getBB(MCE.getCurrentPCOffset(),
158 X86::reloc_pcrel_word, MBB));
162 /// emitGlobalAddress - Emit the specified address to the code stream assuming
163 /// this is part of a "take the address of a global" instruction.
165 template<class CodeEmitter>
166 void Emitter<CodeEmitter>::emitGlobalAddress(const GlobalValue *GV,
168 intptr_t Disp /* = 0 */,
169 intptr_t PCAdj /* = 0 */,
170 bool Indirect /* = false */) {
171 intptr_t RelocCST = Disp;
172 if (Reloc == X86::reloc_picrel_word)
173 RelocCST = PICBaseOffset;
174 else if (Reloc == X86::reloc_pcrel_word)
176 MachineRelocation MR = Indirect
177 ? MachineRelocation::getIndirectSymbol(MCE.getCurrentPCOffset(), Reloc,
178 const_cast<GlobalValue *>(GV),
180 : MachineRelocation::getGV(MCE.getCurrentPCOffset(), Reloc,
181 const_cast<GlobalValue *>(GV), RelocCST, false);
182 MCE.addRelocation(MR);
183 // The relocated value will be added to the displacement
184 if (Reloc == X86::reloc_absolute_dword)
185 MCE.emitDWordLE(Disp);
187 MCE.emitWordLE((int32_t)Disp);
190 /// emitExternalSymbolAddress - Arrange for the address of an external symbol to
191 /// be emitted to the current location in the function, and allow it to be PC
193 template<class CodeEmitter>
194 void Emitter<CodeEmitter>::emitExternalSymbolAddress(const char *ES,
196 intptr_t RelocCST = (Reloc == X86::reloc_picrel_word) ? PICBaseOffset : 0;
198 // X86 never needs stubs because instruction selection will always pick
199 // an instruction sequence that is large enough to hold any address
201 // (see X86ISelLowering.cpp, near 2039: X86TargetLowering::LowerCall)
202 bool NeedStub = false;
203 MCE.addRelocation(MachineRelocation::getExtSym(MCE.getCurrentPCOffset(),
206 if (Reloc == X86::reloc_absolute_dword)
212 /// emitConstPoolAddress - Arrange for the address of an constant pool
213 /// to be emitted to the current location in the function, and allow it to be PC
215 template<class CodeEmitter>
216 void Emitter<CodeEmitter>::emitConstPoolAddress(unsigned CPI, unsigned Reloc,
217 intptr_t Disp /* = 0 */,
218 intptr_t PCAdj /* = 0 */) {
219 intptr_t RelocCST = 0;
220 if (Reloc == X86::reloc_picrel_word)
221 RelocCST = PICBaseOffset;
222 else if (Reloc == X86::reloc_pcrel_word)
224 MCE.addRelocation(MachineRelocation::getConstPool(MCE.getCurrentPCOffset(),
225 Reloc, CPI, RelocCST));
226 // The relocated value will be added to the displacement
227 if (Reloc == X86::reloc_absolute_dword)
228 MCE.emitDWordLE(Disp);
230 MCE.emitWordLE((int32_t)Disp);
233 /// emitJumpTableAddress - Arrange for the address of a jump table to
234 /// be emitted to the current location in the function, and allow it to be PC
236 template<class CodeEmitter>
237 void Emitter<CodeEmitter>::emitJumpTableAddress(unsigned JTI, unsigned Reloc,
238 intptr_t PCAdj /* = 0 */) {
239 intptr_t RelocCST = 0;
240 if (Reloc == X86::reloc_picrel_word)
241 RelocCST = PICBaseOffset;
242 else if (Reloc == X86::reloc_pcrel_word)
244 MCE.addRelocation(MachineRelocation::getJumpTable(MCE.getCurrentPCOffset(),
245 Reloc, JTI, RelocCST));
246 // The relocated value will be added to the displacement
247 if (Reloc == X86::reloc_absolute_dword)
253 template<class CodeEmitter>
254 unsigned Emitter<CodeEmitter>::getX86RegNum(unsigned RegNo) const {
255 return X86RegisterInfo::getX86RegNum(RegNo);
258 inline static unsigned char ModRMByte(unsigned Mod, unsigned RegOpcode,
260 assert(Mod < 4 && RegOpcode < 8 && RM < 8 && "ModRM Fields out of range!");
261 return RM | (RegOpcode << 3) | (Mod << 6);
264 template<class CodeEmitter>
265 void Emitter<CodeEmitter>::emitRegModRMByte(unsigned ModRMReg,
266 unsigned RegOpcodeFld){
267 MCE.emitByte(ModRMByte(3, RegOpcodeFld, getX86RegNum(ModRMReg)));
270 template<class CodeEmitter>
271 void Emitter<CodeEmitter>::emitRegModRMByte(unsigned RegOpcodeFld) {
272 MCE.emitByte(ModRMByte(3, RegOpcodeFld, 0));
275 template<class CodeEmitter>
276 void Emitter<CodeEmitter>::emitSIBByte(unsigned SS,
279 // SIB byte is in the same format as the ModRMByte...
280 MCE.emitByte(ModRMByte(SS, Index, Base));
283 template<class CodeEmitter>
284 void Emitter<CodeEmitter>::emitConstant(uint64_t Val, unsigned Size) {
285 // Output the constant in little endian byte order...
286 for (unsigned i = 0; i != Size; ++i) {
287 MCE.emitByte(Val & 255);
292 /// isDisp8 - Return true if this signed displacement fits in a 8-bit
293 /// sign-extended field.
294 static bool isDisp8(int Value) {
295 return Value == (signed char)Value;
298 static bool gvNeedsNonLazyPtr(const MachineOperand &GVOp,
299 const TargetMachine &TM) {
300 // For Darwin-64, simulate the linktime GOT by using the same non-lazy-pointer
301 // mechanism as 32-bit mode.
302 if (TM.getSubtarget<X86Subtarget>().is64Bit() &&
303 !TM.getSubtarget<X86Subtarget>().isTargetDarwin())
306 // Return true if this is a reference to a stub containing the address of the
307 // global, not the global itself.
308 return isGlobalStubReference(GVOp.getTargetFlags());
311 template<class CodeEmitter>
312 void Emitter<CodeEmitter>::emitDisplacementField(const MachineOperand *RelocOp,
314 intptr_t Adj /* = 0 */,
315 bool IsPCRel /* = true */) {
316 // If this is a simple integer displacement that doesn't require a relocation,
319 emitConstant(DispVal, 4);
323 // Otherwise, this is something that requires a relocation. Emit it as such
325 unsigned RelocType = Is64BitMode ?
326 (IsPCRel ? X86::reloc_pcrel_word : X86::reloc_absolute_word_sext)
327 : (IsPIC ? X86::reloc_picrel_word : X86::reloc_absolute_word);
328 if (RelocOp->isGlobal()) {
329 // In 64-bit static small code model, we could potentially emit absolute.
330 // But it's probably not beneficial. If the MCE supports using RIP directly
331 // do it, otherwise fallback to absolute (this is determined by IsPCRel).
332 // 89 05 00 00 00 00 mov %eax,0(%rip) # PC-relative
333 // 89 04 25 00 00 00 00 mov %eax,0x0 # Absolute
334 bool Indirect = gvNeedsNonLazyPtr(*RelocOp, TM);
335 emitGlobalAddress(RelocOp->getGlobal(), RelocType, RelocOp->getOffset(),
337 } else if (RelocOp->isSymbol()) {
338 emitExternalSymbolAddress(RelocOp->getSymbolName(), RelocType);
339 } else if (RelocOp->isCPI()) {
340 emitConstPoolAddress(RelocOp->getIndex(), RelocType,
341 RelocOp->getOffset(), Adj);
343 assert(RelocOp->isJTI() && "Unexpected machine operand!");
344 emitJumpTableAddress(RelocOp->getIndex(), RelocType, Adj);
348 template<class CodeEmitter>
349 void Emitter<CodeEmitter>::emitMemModRMByte(const MachineInstr &MI,
350 unsigned Op,unsigned RegOpcodeField,
352 const MachineOperand &Op3 = MI.getOperand(Op+3);
354 const MachineOperand *DispForReloc = 0;
356 // Figure out what sort of displacement we have to handle here.
357 if (Op3.isGlobal()) {
359 } else if (Op3.isSymbol()) {
361 } else if (Op3.isCPI()) {
362 if (!MCE.earlyResolveAddresses() || Is64BitMode || IsPIC) {
365 DispVal += MCE.getConstantPoolEntryAddress(Op3.getIndex());
366 DispVal += Op3.getOffset();
368 } else if (Op3.isJTI()) {
369 if (!MCE.earlyResolveAddresses() || Is64BitMode || IsPIC) {
372 DispVal += MCE.getJumpTableEntryAddress(Op3.getIndex());
375 DispVal = Op3.getImm();
378 const MachineOperand &Base = MI.getOperand(Op);
379 const MachineOperand &Scale = MI.getOperand(Op+1);
380 const MachineOperand &IndexReg = MI.getOperand(Op+2);
382 unsigned BaseReg = Base.getReg();
384 // Indicate that the displacement will use an pcrel or absolute reference
385 // by default. MCEs able to resolve addresses on-the-fly use pcrel by default
386 // while others, unless explicit asked to use RIP, use absolute references.
387 bool IsPCRel = MCE.earlyResolveAddresses() ? true : false;
389 // Is a SIB byte needed?
390 // If no BaseReg, issue a RIP relative instruction only if the MCE can
391 // resolve addresses on-the-fly, otherwise use SIB (Intel Manual 2A, table
392 // 2-7) and absolute references.
393 unsigned BaseRegNo = -1U;
394 if (BaseReg != 0 && BaseReg != X86::RIP)
395 BaseRegNo = getX86RegNum(BaseReg);
397 if (// The SIB byte must be used if there is an index register.
398 IndexReg.getReg() == 0 &&
399 // The SIB byte must be used if the base is ESP/RSP/R12, all of which
400 // encode to an R/M value of 4, which indicates that a SIB byte is
402 BaseRegNo != N86::ESP &&
403 // If there is no base register and we're in 64-bit mode, we need a SIB
404 // byte to emit an addr that is just 'disp32' (the non-RIP relative form).
405 (!Is64BitMode || BaseReg != 0)) {
406 if (BaseReg == 0 || // [disp32] in X86-32 mode
407 BaseReg == X86::RIP) { // [disp32+RIP] in X86-64 mode
408 MCE.emitByte(ModRMByte(0, RegOpcodeField, 5));
409 emitDisplacementField(DispForReloc, DispVal, PCAdj, true);
413 // If the base is not EBP/ESP and there is no displacement, use simple
414 // indirect register encoding, this handles addresses like [EAX]. The
415 // encoding for [EBP] with no displacement means [disp32] so we handle it
416 // by emitting a displacement of 0 below.
417 if (!DispForReloc && DispVal == 0 && BaseRegNo != N86::EBP) {
418 MCE.emitByte(ModRMByte(0, RegOpcodeField, BaseRegNo));
422 // Otherwise, if the displacement fits in a byte, encode as [REG+disp8].
423 if (!DispForReloc && isDisp8(DispVal)) {
424 MCE.emitByte(ModRMByte(1, RegOpcodeField, BaseRegNo));
425 emitConstant(DispVal, 1);
429 // Otherwise, emit the most general non-SIB encoding: [REG+disp32]
430 MCE.emitByte(ModRMByte(2, RegOpcodeField, BaseRegNo));
431 emitDisplacementField(DispForReloc, DispVal, PCAdj, IsPCRel);
435 // Otherwise we need a SIB byte, so start by outputting the ModR/M byte first.
436 assert(IndexReg.getReg() != X86::ESP &&
437 IndexReg.getReg() != X86::RSP && "Cannot use ESP as index reg!");
439 bool ForceDisp32 = false;
440 bool ForceDisp8 = false;
442 // If there is no base register, we emit the special case SIB byte with
443 // MOD=0, BASE=4, to JUST get the index, scale, and displacement.
444 MCE.emitByte(ModRMByte(0, RegOpcodeField, 4));
446 } else if (DispForReloc) {
447 // Emit the normal disp32 encoding.
448 MCE.emitByte(ModRMByte(2, RegOpcodeField, 4));
450 } else if (DispVal == 0 && getX86RegNum(BaseReg) != N86::EBP) {
451 // Emit no displacement ModR/M byte
452 MCE.emitByte(ModRMByte(0, RegOpcodeField, 4));
453 } else if (isDisp8(DispVal)) {
454 // Emit the disp8 encoding...
455 MCE.emitByte(ModRMByte(1, RegOpcodeField, 4));
456 ForceDisp8 = true; // Make sure to force 8 bit disp if Base=EBP
458 // Emit the normal disp32 encoding...
459 MCE.emitByte(ModRMByte(2, RegOpcodeField, 4));
462 // Calculate what the SS field value should be...
463 static const unsigned SSTable[] = { ~0, 0, 1, ~0, 2, ~0, ~0, ~0, 3 };
464 unsigned SS = SSTable[Scale.getImm()];
467 // Handle the SIB byte for the case where there is no base, see Intel
468 // Manual 2A, table 2-7. The displacement has already been output.
470 if (IndexReg.getReg())
471 IndexRegNo = getX86RegNum(IndexReg.getReg());
472 else // Examples: [ESP+1*<noreg>+4] or [scaled idx]+disp32 (MOD=0,BASE=5)
474 emitSIBByte(SS, IndexRegNo, 5);
476 unsigned BaseRegNo = getX86RegNum(BaseReg);
478 if (IndexReg.getReg())
479 IndexRegNo = getX86RegNum(IndexReg.getReg());
481 IndexRegNo = 4; // For example [ESP+1*<noreg>+4]
482 emitSIBByte(SS, IndexRegNo, BaseRegNo);
485 // Do we need to output a displacement?
487 emitConstant(DispVal, 1);
488 } else if (DispVal != 0 || ForceDisp32) {
489 emitDisplacementField(DispForReloc, DispVal, PCAdj, IsPCRel);
493 template<class CodeEmitter>
494 void Emitter<CodeEmitter>::emitInstruction(const MachineInstr &MI,
495 const TargetInstrDesc *Desc) {
498 MCE.processDebugLoc(MI.getDebugLoc(), true);
500 unsigned Opcode = Desc->Opcode;
502 // Emit the lock opcode prefix as needed.
503 if (Desc->TSFlags & X86II::LOCK)
506 // Emit segment override opcode prefix as needed.
507 switch (Desc->TSFlags & X86II::SegOvrMask) {
514 default: llvm_unreachable("Invalid segment!");
515 case 0: break; // No segment override!
518 // Emit the repeat opcode prefix as needed.
519 if ((Desc->TSFlags & X86II::Op0Mask) == X86II::REP)
522 // Emit the operand size opcode prefix as needed.
523 if (Desc->TSFlags & X86II::OpSize)
526 // Emit the address size opcode prefix as needed.
527 if (Desc->TSFlags & X86II::AdSize)
530 bool Need0FPrefix = false;
531 switch (Desc->TSFlags & X86II::Op0Mask) {
532 case X86II::TB: // Two-byte opcode prefix
533 case X86II::T8: // 0F 38
534 case X86II::TA: // 0F 3A
537 case X86II::TF: // F2 0F 38
541 case X86II::REP: break; // already handled.
542 case X86II::XS: // F3 0F
546 case X86II::XD: // F2 0F
550 case X86II::D8: case X86II::D9: case X86II::DA: case X86II::DB:
551 case X86II::DC: case X86II::DD: case X86II::DE: case X86II::DF:
553 (((Desc->TSFlags & X86II::Op0Mask)-X86II::D8)
554 >> X86II::Op0Shift));
555 break; // Two-byte opcode prefix
556 default: llvm_unreachable("Invalid prefix!");
557 case 0: break; // No prefix!
560 // Handle REX prefix.
562 if (unsigned REX = X86InstrInfo::determineREX(MI))
563 MCE.emitByte(0x40 | REX);
566 // 0x0F escape code must be emitted just before the opcode.
570 switch (Desc->TSFlags & X86II::Op0Mask) {
571 case X86II::TF: // F2 0F 38
572 case X86II::T8: // 0F 38
575 case X86II::TA: // 0F 3A
580 // If this is a two-address instruction, skip one of the register operands.
581 unsigned NumOps = Desc->getNumOperands();
583 if (NumOps > 1 && Desc->getOperandConstraint(1, TOI::TIED_TO) != -1)
585 else if (NumOps > 2 && Desc->getOperandConstraint(NumOps-1, TOI::TIED_TO)== 0)
586 // Skip the last source operand that is tied_to the dest reg. e.g. LXADD32
589 unsigned char BaseOpcode = X86II::getBaseOpcodeFor(Desc->TSFlags);
590 switch (Desc->TSFlags & X86II::FormMask) {
592 llvm_unreachable("Unknown FormMask value in X86 MachineCodeEmitter!");
594 // Remember the current PC offset, this is the PIC relocation
598 llvm_unreachable("psuedo instructions should be removed before code"
601 case TargetOpcode::INLINEASM:
602 // We allow inline assembler nodes with empty bodies - they can
603 // implicitly define registers, which is ok for JIT.
604 if (MI.getOperand(0).getSymbolName()[0])
605 report_fatal_error("JIT does not support inline asm!");
607 case TargetOpcode::DBG_LABEL:
608 case TargetOpcode::GC_LABEL:
609 case TargetOpcode::EH_LABEL:
610 MCE.emitLabel(MI.getOperand(0).getMCSymbol());
613 case TargetOpcode::IMPLICIT_DEF:
614 case TargetOpcode::KILL:
615 case X86::FP_REG_KILL:
617 case X86::MOVPC32r: {
618 // This emits the "call" portion of this pseudo instruction.
619 MCE.emitByte(BaseOpcode);
620 emitConstant(0, X86II::getSizeOfImm(Desc->TSFlags));
621 // Remember PIC base.
622 PICBaseOffset = (intptr_t) MCE.getCurrentPCOffset();
623 X86JITInfo *JTI = TM.getJITInfo();
624 JTI->setPICBase(MCE.getCurrentPCValue());
630 case X86II::RawFrm: {
631 MCE.emitByte(BaseOpcode);
636 const MachineOperand &MO = MI.getOperand(CurOp++);
638 DEBUG(dbgs() << "RawFrm CurOp " << CurOp << "\n");
639 DEBUG(dbgs() << "isMBB " << MO.isMBB() << "\n");
640 DEBUG(dbgs() << "isGlobal " << MO.isGlobal() << "\n");
641 DEBUG(dbgs() << "isSymbol " << MO.isSymbol() << "\n");
642 DEBUG(dbgs() << "isImm " << MO.isImm() << "\n");
645 emitPCRelativeBlockAddress(MO.getMBB());
650 emitGlobalAddress(MO.getGlobal(), X86::reloc_pcrel_word,
656 emitExternalSymbolAddress(MO.getSymbolName(), X86::reloc_pcrel_word);
660 // FIXME: Only used by hackish MCCodeEmitter, remove when dead.
662 emitJumpTableAddress(MO.getIndex(), X86::reloc_pcrel_word);
666 assert(MO.isImm() && "Unknown RawFrm operand!");
667 if (Opcode == X86::CALLpcrel32 || Opcode == X86::CALL64pcrel32) {
668 // Fix up immediate operand for pc relative calls.
669 intptr_t Imm = (intptr_t)MO.getImm();
670 Imm = Imm - MCE.getCurrentPCValue() - 4;
671 emitConstant(Imm, X86II::getSizeOfImm(Desc->TSFlags));
673 emitConstant(MO.getImm(), X86II::getSizeOfImm(Desc->TSFlags));
677 case X86II::AddRegFrm: {
678 MCE.emitByte(BaseOpcode + getX86RegNum(MI.getOperand(CurOp++).getReg()));
683 const MachineOperand &MO1 = MI.getOperand(CurOp++);
684 unsigned Size = X86II::getSizeOfImm(Desc->TSFlags);
686 emitConstant(MO1.getImm(), Size);
690 unsigned rt = Is64BitMode ? X86::reloc_pcrel_word
691 : (IsPIC ? X86::reloc_picrel_word : X86::reloc_absolute_word);
692 if (Opcode == X86::MOV64ri64i32)
693 rt = X86::reloc_absolute_word; // FIXME: add X86II flag?
694 // This should not occur on Darwin for relocatable objects.
695 if (Opcode == X86::MOV64ri)
696 rt = X86::reloc_absolute_dword; // FIXME: add X86II flag?
697 if (MO1.isGlobal()) {
698 bool Indirect = gvNeedsNonLazyPtr(MO1, TM);
699 emitGlobalAddress(MO1.getGlobal(), rt, MO1.getOffset(), 0,
701 } else if (MO1.isSymbol())
702 emitExternalSymbolAddress(MO1.getSymbolName(), rt);
703 else if (MO1.isCPI())
704 emitConstPoolAddress(MO1.getIndex(), rt);
705 else if (MO1.isJTI())
706 emitJumpTableAddress(MO1.getIndex(), rt);
710 case X86II::MRMDestReg: {
711 MCE.emitByte(BaseOpcode);
712 emitRegModRMByte(MI.getOperand(CurOp).getReg(),
713 getX86RegNum(MI.getOperand(CurOp+1).getReg()));
716 emitConstant(MI.getOperand(CurOp++).getImm(),
717 X86II::getSizeOfImm(Desc->TSFlags));
720 case X86II::MRMDestMem: {
721 MCE.emitByte(BaseOpcode);
722 emitMemModRMByte(MI, CurOp,
723 getX86RegNum(MI.getOperand(CurOp + X86AddrNumOperands)
725 CurOp += X86AddrNumOperands + 1;
727 emitConstant(MI.getOperand(CurOp++).getImm(),
728 X86II::getSizeOfImm(Desc->TSFlags));
732 case X86II::MRMSrcReg:
733 MCE.emitByte(BaseOpcode);
734 emitRegModRMByte(MI.getOperand(CurOp+1).getReg(),
735 getX86RegNum(MI.getOperand(CurOp).getReg()));
738 emitConstant(MI.getOperand(CurOp++).getImm(),
739 X86II::getSizeOfImm(Desc->TSFlags));
742 case X86II::MRMSrcMem: {
743 // FIXME: Maybe lea should have its own form?
745 if (Opcode == X86::LEA64r || Opcode == X86::LEA64_32r ||
746 Opcode == X86::LEA16r || Opcode == X86::LEA32r)
747 AddrOperands = X86AddrNumOperands - 1; // No segment register
749 AddrOperands = X86AddrNumOperands;
751 intptr_t PCAdj = (CurOp + AddrOperands + 1 != NumOps) ?
752 X86II::getSizeOfImm(Desc->TSFlags) : 0;
754 MCE.emitByte(BaseOpcode);
755 emitMemModRMByte(MI, CurOp+1, getX86RegNum(MI.getOperand(CurOp).getReg()),
757 CurOp += AddrOperands + 1;
759 emitConstant(MI.getOperand(CurOp++).getImm(),
760 X86II::getSizeOfImm(Desc->TSFlags));
764 case X86II::MRM0r: case X86II::MRM1r:
765 case X86II::MRM2r: case X86II::MRM3r:
766 case X86II::MRM4r: case X86II::MRM5r:
767 case X86II::MRM6r: case X86II::MRM7r: {
768 MCE.emitByte(BaseOpcode);
769 emitRegModRMByte(MI.getOperand(CurOp++).getReg(),
770 (Desc->TSFlags & X86II::FormMask)-X86II::MRM0r);
775 const MachineOperand &MO1 = MI.getOperand(CurOp++);
776 unsigned Size = X86II::getSizeOfImm(Desc->TSFlags);
778 emitConstant(MO1.getImm(), Size);
782 unsigned rt = Is64BitMode ? X86::reloc_pcrel_word
783 : (IsPIC ? X86::reloc_picrel_word : X86::reloc_absolute_word);
784 if (Opcode == X86::MOV64ri32)
785 rt = X86::reloc_absolute_word_sext; // FIXME: add X86II flag?
786 if (MO1.isGlobal()) {
787 bool Indirect = gvNeedsNonLazyPtr(MO1, TM);
788 emitGlobalAddress(MO1.getGlobal(), rt, MO1.getOffset(), 0,
790 } else if (MO1.isSymbol())
791 emitExternalSymbolAddress(MO1.getSymbolName(), rt);
792 else if (MO1.isCPI())
793 emitConstPoolAddress(MO1.getIndex(), rt);
794 else if (MO1.isJTI())
795 emitJumpTableAddress(MO1.getIndex(), rt);
799 case X86II::MRM0m: case X86II::MRM1m:
800 case X86II::MRM2m: case X86II::MRM3m:
801 case X86II::MRM4m: case X86II::MRM5m:
802 case X86II::MRM6m: case X86II::MRM7m: {
803 intptr_t PCAdj = (CurOp + X86AddrNumOperands != NumOps) ?
804 (MI.getOperand(CurOp+X86AddrNumOperands).isImm() ?
805 X86II::getSizeOfImm(Desc->TSFlags) : 4) : 0;
807 MCE.emitByte(BaseOpcode);
808 emitMemModRMByte(MI, CurOp, (Desc->TSFlags & X86II::FormMask)-X86II::MRM0m,
810 CurOp += X86AddrNumOperands;
815 const MachineOperand &MO = MI.getOperand(CurOp++);
816 unsigned Size = X86II::getSizeOfImm(Desc->TSFlags);
818 emitConstant(MO.getImm(), Size);
822 unsigned rt = Is64BitMode ? X86::reloc_pcrel_word
823 : (IsPIC ? X86::reloc_picrel_word : X86::reloc_absolute_word);
824 if (Opcode == X86::MOV64mi32)
825 rt = X86::reloc_absolute_word_sext; // FIXME: add X86II flag?
827 bool Indirect = gvNeedsNonLazyPtr(MO, TM);
828 emitGlobalAddress(MO.getGlobal(), rt, MO.getOffset(), 0,
830 } else if (MO.isSymbol())
831 emitExternalSymbolAddress(MO.getSymbolName(), rt);
833 emitConstPoolAddress(MO.getIndex(), rt);
835 emitJumpTableAddress(MO.getIndex(), rt);
839 case X86II::MRMInitReg:
840 MCE.emitByte(BaseOpcode);
841 // Duplicate register, used by things like MOV8r0 (aka xor reg,reg).
842 emitRegModRMByte(MI.getOperand(CurOp).getReg(),
843 getX86RegNum(MI.getOperand(CurOp).getReg()));
848 MCE.emitByte(BaseOpcode);
852 MCE.emitByte(BaseOpcode);
856 MCE.emitByte(BaseOpcode);
860 MCE.emitByte(BaseOpcode);
864 MCE.emitByte(BaseOpcode);
869 if (!Desc->isVariadic() && CurOp != NumOps) {
871 dbgs() << "Cannot encode all operands of: " << MI << "\n";
876 MCE.processDebugLoc(MI.getDebugLoc(), false);