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 // Handle %rip relative addressing.
385 if (BaseReg == X86::RIP ||
386 (Is64BitMode && DispForReloc)) { // [disp32+RIP] in X86-64 mode
387 assert(IndexReg.getReg() == 0 && Is64BitMode &&
388 "Invalid rip-relative address");
389 MCE.emitByte(ModRMByte(0, RegOpcodeField, 5));
390 emitDisplacementField(DispForReloc, DispVal, PCAdj, true);
394 // Indicate that the displacement will use an pcrel or absolute reference
395 // by default. MCEs able to resolve addresses on-the-fly use pcrel by default
396 // while others, unless explicit asked to use RIP, use absolute references.
397 bool IsPCRel = MCE.earlyResolveAddresses() ? true : false;
399 // Is a SIB byte needed?
400 // If no BaseReg, issue a RIP relative instruction only if the MCE can
401 // resolve addresses on-the-fly, otherwise use SIB (Intel Manual 2A, table
402 // 2-7) and absolute references.
403 unsigned BaseRegNo = -1U;
404 if (BaseReg != 0 && BaseReg != X86::RIP)
405 BaseRegNo = getX86RegNum(BaseReg);
407 if (// The SIB byte must be used if there is an index register.
408 IndexReg.getReg() == 0 &&
409 // The SIB byte must be used if the base is ESP/RSP/R12, all of which
410 // encode to an R/M value of 4, which indicates that a SIB byte is
412 BaseRegNo != N86::ESP &&
413 // If there is no base register and we're in 64-bit mode, we need a SIB
414 // byte to emit an addr that is just 'disp32' (the non-RIP relative form).
415 (!Is64BitMode || BaseReg != 0)) {
416 if (BaseReg == 0 || // [disp32] in X86-32 mode
417 BaseReg == X86::RIP) { // [disp32+RIP] in X86-64 mode
418 MCE.emitByte(ModRMByte(0, RegOpcodeField, 5));
419 emitDisplacementField(DispForReloc, DispVal, PCAdj, true);
423 // If the base is not EBP/ESP and there is no displacement, use simple
424 // indirect register encoding, this handles addresses like [EAX]. The
425 // encoding for [EBP] with no displacement means [disp32] so we handle it
426 // by emitting a displacement of 0 below.
427 if (!DispForReloc && DispVal == 0 && BaseRegNo != N86::EBP) {
428 MCE.emitByte(ModRMByte(0, RegOpcodeField, BaseRegNo));
432 // Otherwise, if the displacement fits in a byte, encode as [REG+disp8].
433 if (!DispForReloc && isDisp8(DispVal)) {
434 MCE.emitByte(ModRMByte(1, RegOpcodeField, BaseRegNo));
435 emitConstant(DispVal, 1);
439 // Otherwise, emit the most general non-SIB encoding: [REG+disp32]
440 MCE.emitByte(ModRMByte(2, RegOpcodeField, BaseRegNo));
441 emitDisplacementField(DispForReloc, DispVal, PCAdj, IsPCRel);
445 // Otherwise we need a SIB byte, so start by outputting the ModR/M byte first.
446 assert(IndexReg.getReg() != X86::ESP &&
447 IndexReg.getReg() != X86::RSP && "Cannot use ESP as index reg!");
449 bool ForceDisp32 = false;
450 bool ForceDisp8 = false;
452 // If there is no base register, we emit the special case SIB byte with
453 // MOD=0, BASE=4, to JUST get the index, scale, and displacement.
454 MCE.emitByte(ModRMByte(0, RegOpcodeField, 4));
456 } else if (DispForReloc) {
457 // Emit the normal disp32 encoding.
458 MCE.emitByte(ModRMByte(2, RegOpcodeField, 4));
460 } else if (DispVal == 0 && BaseRegNo != N86::EBP) {
461 // Emit no displacement ModR/M byte
462 MCE.emitByte(ModRMByte(0, RegOpcodeField, 4));
463 } else if (isDisp8(DispVal)) {
464 // Emit the disp8 encoding...
465 MCE.emitByte(ModRMByte(1, RegOpcodeField, 4));
466 ForceDisp8 = true; // Make sure to force 8 bit disp if Base=EBP
468 // Emit the normal disp32 encoding...
469 MCE.emitByte(ModRMByte(2, RegOpcodeField, 4));
472 // Calculate what the SS field value should be...
473 static const unsigned SSTable[] = { ~0, 0, 1, ~0, 2, ~0, ~0, ~0, 3 };
474 unsigned SS = SSTable[Scale.getImm()];
477 // Handle the SIB byte for the case where there is no base, see Intel
478 // Manual 2A, table 2-7. The displacement has already been output.
480 if (IndexReg.getReg())
481 IndexRegNo = getX86RegNum(IndexReg.getReg());
482 else // Examples: [ESP+1*<noreg>+4] or [scaled idx]+disp32 (MOD=0,BASE=5)
484 emitSIBByte(SS, IndexRegNo, 5);
486 unsigned BaseRegNo = getX86RegNum(BaseReg);
488 if (IndexReg.getReg())
489 IndexRegNo = getX86RegNum(IndexReg.getReg());
491 IndexRegNo = 4; // For example [ESP+1*<noreg>+4]
492 emitSIBByte(SS, IndexRegNo, BaseRegNo);
495 // Do we need to output a displacement?
497 emitConstant(DispVal, 1);
498 } else if (DispVal != 0 || ForceDisp32) {
499 emitDisplacementField(DispForReloc, DispVal, PCAdj, IsPCRel);
503 template<class CodeEmitter>
504 void Emitter<CodeEmitter>::emitInstruction(const MachineInstr &MI,
505 const TargetInstrDesc *Desc) {
508 MCE.processDebugLoc(MI.getDebugLoc(), true);
510 unsigned Opcode = Desc->Opcode;
512 // Emit the lock opcode prefix as needed.
513 if (Desc->TSFlags & X86II::LOCK)
516 // Emit segment override opcode prefix as needed.
517 switch (Desc->TSFlags & X86II::SegOvrMask) {
524 default: llvm_unreachable("Invalid segment!");
525 case 0: break; // No segment override!
528 // Emit the repeat opcode prefix as needed.
529 if ((Desc->TSFlags & X86II::Op0Mask) == X86II::REP)
532 // Emit the operand size opcode prefix as needed.
533 if (Desc->TSFlags & X86II::OpSize)
536 // Emit the address size opcode prefix as needed.
537 if (Desc->TSFlags & X86II::AdSize)
540 bool Need0FPrefix = false;
541 switch (Desc->TSFlags & X86II::Op0Mask) {
542 case X86II::TB: // Two-byte opcode prefix
543 case X86II::T8: // 0F 38
544 case X86II::TA: // 0F 3A
547 case X86II::TF: // F2 0F 38
551 case X86II::REP: break; // already handled.
552 case X86II::XS: // F3 0F
556 case X86II::XD: // F2 0F
560 case X86II::D8: case X86II::D9: case X86II::DA: case X86II::DB:
561 case X86II::DC: case X86II::DD: case X86II::DE: case X86II::DF:
563 (((Desc->TSFlags & X86II::Op0Mask)-X86II::D8)
564 >> X86II::Op0Shift));
565 break; // Two-byte opcode prefix
566 default: llvm_unreachable("Invalid prefix!");
567 case 0: break; // No prefix!
570 // Handle REX prefix.
572 if (unsigned REX = X86InstrInfo::determineREX(MI))
573 MCE.emitByte(0x40 | REX);
576 // 0x0F escape code must be emitted just before the opcode.
580 switch (Desc->TSFlags & X86II::Op0Mask) {
581 case X86II::TF: // F2 0F 38
582 case X86II::T8: // 0F 38
585 case X86II::TA: // 0F 3A
590 // If this is a two-address instruction, skip one of the register operands.
591 unsigned NumOps = Desc->getNumOperands();
593 if (NumOps > 1 && Desc->getOperandConstraint(1, TOI::TIED_TO) != -1)
595 else if (NumOps > 2 && Desc->getOperandConstraint(NumOps-1, TOI::TIED_TO)== 0)
596 // Skip the last source operand that is tied_to the dest reg. e.g. LXADD32
599 unsigned char BaseOpcode = X86II::getBaseOpcodeFor(Desc->TSFlags);
600 switch (Desc->TSFlags & X86II::FormMask) {
602 llvm_unreachable("Unknown FormMask value in X86 MachineCodeEmitter!");
604 // Remember the current PC offset, this is the PIC relocation
608 llvm_unreachable("psuedo instructions should be removed before code"
611 case TargetOpcode::INLINEASM:
612 // We allow inline assembler nodes with empty bodies - they can
613 // implicitly define registers, which is ok for JIT.
614 if (MI.getOperand(0).getSymbolName()[0])
615 report_fatal_error("JIT does not support inline asm!");
617 case TargetOpcode::DBG_LABEL:
618 case TargetOpcode::GC_LABEL:
619 case TargetOpcode::EH_LABEL:
620 MCE.emitLabel(MI.getOperand(0).getMCSymbol());
623 case TargetOpcode::IMPLICIT_DEF:
624 case TargetOpcode::KILL:
626 case X86::MOVPC32r: {
627 // This emits the "call" portion of this pseudo instruction.
628 MCE.emitByte(BaseOpcode);
629 emitConstant(0, X86II::getSizeOfImm(Desc->TSFlags));
630 // Remember PIC base.
631 PICBaseOffset = (intptr_t) MCE.getCurrentPCOffset();
632 X86JITInfo *JTI = TM.getJITInfo();
633 JTI->setPICBase(MCE.getCurrentPCValue());
639 case X86II::RawFrm: {
640 MCE.emitByte(BaseOpcode);
645 const MachineOperand &MO = MI.getOperand(CurOp++);
647 DEBUG(dbgs() << "RawFrm CurOp " << CurOp << "\n");
648 DEBUG(dbgs() << "isMBB " << MO.isMBB() << "\n");
649 DEBUG(dbgs() << "isGlobal " << MO.isGlobal() << "\n");
650 DEBUG(dbgs() << "isSymbol " << MO.isSymbol() << "\n");
651 DEBUG(dbgs() << "isImm " << MO.isImm() << "\n");
654 emitPCRelativeBlockAddress(MO.getMBB());
659 emitGlobalAddress(MO.getGlobal(), X86::reloc_pcrel_word,
665 emitExternalSymbolAddress(MO.getSymbolName(), X86::reloc_pcrel_word);
669 // FIXME: Only used by hackish MCCodeEmitter, remove when dead.
671 emitJumpTableAddress(MO.getIndex(), X86::reloc_pcrel_word);
675 assert(MO.isImm() && "Unknown RawFrm operand!");
676 if (Opcode == X86::CALLpcrel32 || Opcode == X86::CALL64pcrel32) {
677 // Fix up immediate operand for pc relative calls.
678 intptr_t Imm = (intptr_t)MO.getImm();
679 Imm = Imm - MCE.getCurrentPCValue() - 4;
680 emitConstant(Imm, X86II::getSizeOfImm(Desc->TSFlags));
682 emitConstant(MO.getImm(), X86II::getSizeOfImm(Desc->TSFlags));
686 case X86II::AddRegFrm: {
687 MCE.emitByte(BaseOpcode + getX86RegNum(MI.getOperand(CurOp++).getReg()));
692 const MachineOperand &MO1 = MI.getOperand(CurOp++);
693 unsigned Size = X86II::getSizeOfImm(Desc->TSFlags);
695 emitConstant(MO1.getImm(), Size);
699 unsigned rt = Is64BitMode ? X86::reloc_pcrel_word
700 : (IsPIC ? X86::reloc_picrel_word : X86::reloc_absolute_word);
701 if (Opcode == X86::MOV64ri64i32)
702 rt = X86::reloc_absolute_word; // FIXME: add X86II flag?
703 // This should not occur on Darwin for relocatable objects.
704 if (Opcode == X86::MOV64ri)
705 rt = X86::reloc_absolute_dword; // FIXME: add X86II flag?
706 if (MO1.isGlobal()) {
707 bool Indirect = gvNeedsNonLazyPtr(MO1, TM);
708 emitGlobalAddress(MO1.getGlobal(), rt, MO1.getOffset(), 0,
710 } else if (MO1.isSymbol())
711 emitExternalSymbolAddress(MO1.getSymbolName(), rt);
712 else if (MO1.isCPI())
713 emitConstPoolAddress(MO1.getIndex(), rt);
714 else if (MO1.isJTI())
715 emitJumpTableAddress(MO1.getIndex(), rt);
719 case X86II::MRMDestReg: {
720 MCE.emitByte(BaseOpcode);
721 emitRegModRMByte(MI.getOperand(CurOp).getReg(),
722 getX86RegNum(MI.getOperand(CurOp+1).getReg()));
725 emitConstant(MI.getOperand(CurOp++).getImm(),
726 X86II::getSizeOfImm(Desc->TSFlags));
729 case X86II::MRMDestMem: {
730 MCE.emitByte(BaseOpcode);
731 emitMemModRMByte(MI, CurOp,
732 getX86RegNum(MI.getOperand(CurOp + X86::AddrNumOperands)
734 CurOp += X86::AddrNumOperands + 1;
736 emitConstant(MI.getOperand(CurOp++).getImm(),
737 X86II::getSizeOfImm(Desc->TSFlags));
741 case X86II::MRMSrcReg:
742 MCE.emitByte(BaseOpcode);
743 emitRegModRMByte(MI.getOperand(CurOp+1).getReg(),
744 getX86RegNum(MI.getOperand(CurOp).getReg()));
747 emitConstant(MI.getOperand(CurOp++).getImm(),
748 X86II::getSizeOfImm(Desc->TSFlags));
751 case X86II::MRMSrcMem: {
752 int AddrOperands = X86::AddrNumOperands;
754 intptr_t PCAdj = (CurOp + AddrOperands + 1 != NumOps) ?
755 X86II::getSizeOfImm(Desc->TSFlags) : 0;
757 MCE.emitByte(BaseOpcode);
758 emitMemModRMByte(MI, CurOp+1, getX86RegNum(MI.getOperand(CurOp).getReg()),
760 CurOp += AddrOperands + 1;
762 emitConstant(MI.getOperand(CurOp++).getImm(),
763 X86II::getSizeOfImm(Desc->TSFlags));
767 case X86II::MRM0r: case X86II::MRM1r:
768 case X86II::MRM2r: case X86II::MRM3r:
769 case X86II::MRM4r: case X86II::MRM5r:
770 case X86II::MRM6r: case X86II::MRM7r: {
771 MCE.emitByte(BaseOpcode);
772 emitRegModRMByte(MI.getOperand(CurOp++).getReg(),
773 (Desc->TSFlags & X86II::FormMask)-X86II::MRM0r);
778 const MachineOperand &MO1 = MI.getOperand(CurOp++);
779 unsigned Size = X86II::getSizeOfImm(Desc->TSFlags);
781 emitConstant(MO1.getImm(), Size);
785 unsigned rt = Is64BitMode ? X86::reloc_pcrel_word
786 : (IsPIC ? X86::reloc_picrel_word : X86::reloc_absolute_word);
787 if (Opcode == X86::MOV64ri32)
788 rt = X86::reloc_absolute_word_sext; // FIXME: add X86II flag?
789 if (MO1.isGlobal()) {
790 bool Indirect = gvNeedsNonLazyPtr(MO1, TM);
791 emitGlobalAddress(MO1.getGlobal(), rt, MO1.getOffset(), 0,
793 } else if (MO1.isSymbol())
794 emitExternalSymbolAddress(MO1.getSymbolName(), rt);
795 else if (MO1.isCPI())
796 emitConstPoolAddress(MO1.getIndex(), rt);
797 else if (MO1.isJTI())
798 emitJumpTableAddress(MO1.getIndex(), rt);
802 case X86II::MRM0m: case X86II::MRM1m:
803 case X86II::MRM2m: case X86II::MRM3m:
804 case X86II::MRM4m: case X86II::MRM5m:
805 case X86II::MRM6m: case X86II::MRM7m: {
806 intptr_t PCAdj = (CurOp + X86::AddrNumOperands != NumOps) ?
807 (MI.getOperand(CurOp+X86::AddrNumOperands).isImm() ?
808 X86II::getSizeOfImm(Desc->TSFlags) : 4) : 0;
810 MCE.emitByte(BaseOpcode);
811 emitMemModRMByte(MI, CurOp, (Desc->TSFlags & X86II::FormMask)-X86II::MRM0m,
813 CurOp += X86::AddrNumOperands;
818 const MachineOperand &MO = MI.getOperand(CurOp++);
819 unsigned Size = X86II::getSizeOfImm(Desc->TSFlags);
821 emitConstant(MO.getImm(), Size);
825 unsigned rt = Is64BitMode ? X86::reloc_pcrel_word
826 : (IsPIC ? X86::reloc_picrel_word : X86::reloc_absolute_word);
827 if (Opcode == X86::MOV64mi32)
828 rt = X86::reloc_absolute_word_sext; // FIXME: add X86II flag?
830 bool Indirect = gvNeedsNonLazyPtr(MO, TM);
831 emitGlobalAddress(MO.getGlobal(), rt, MO.getOffset(), 0,
833 } else if (MO.isSymbol())
834 emitExternalSymbolAddress(MO.getSymbolName(), rt);
836 emitConstPoolAddress(MO.getIndex(), rt);
838 emitJumpTableAddress(MO.getIndex(), rt);
842 case X86II::MRMInitReg:
843 MCE.emitByte(BaseOpcode);
844 // Duplicate register, used by things like MOV8r0 (aka xor reg,reg).
845 emitRegModRMByte(MI.getOperand(CurOp).getReg(),
846 getX86RegNum(MI.getOperand(CurOp).getReg()));
851 MCE.emitByte(BaseOpcode);
855 MCE.emitByte(BaseOpcode);
859 MCE.emitByte(BaseOpcode);
863 MCE.emitByte(BaseOpcode);
867 MCE.emitByte(BaseOpcode);
872 if (!Desc->isVariadic() && CurOp != NumOps) {
874 dbgs() << "Cannot encode all operands of: " << MI << "\n";
879 MCE.processDebugLoc(MI.getDebugLoc(), false);