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(MachineInstr
&MI
, const TargetInstrDesc
*Desc
);
73 void getAnalysisUsage(AnalysisUsage
&AU
) const {
75 AU
.addRequired
<MachineModuleInfo
>();
76 MachineFunctionPass::getAnalysisUsage(AU
);
80 void emitPCRelativeBlockAddress(MachineBasicBlock
*MBB
);
81 void emitGlobalAddress(const GlobalValue
*GV
, unsigned Reloc
,
82 intptr_t Disp
= 0, intptr_t PCAdj
= 0,
83 bool Indirect
= false);
84 void emitExternalSymbolAddress(const char *ES
, unsigned Reloc
);
85 void emitConstPoolAddress(unsigned CPI
, unsigned Reloc
, intptr_t Disp
= 0,
87 void emitJumpTableAddress(unsigned JTI
, unsigned Reloc
,
90 void emitDisplacementField(const MachineOperand
*RelocOp
, int DispVal
,
91 intptr_t Adj
= 0, bool IsPCRel
= true);
93 void emitRegModRMByte(unsigned ModRMReg
, unsigned RegOpcodeField
);
94 void emitRegModRMByte(unsigned RegOpcodeField
);
95 void emitSIBByte(unsigned SS
, unsigned Index
, unsigned Base
);
96 void emitConstant(uint64_t Val
, unsigned Size
);
98 void emitMemModRMByte(const MachineInstr
&MI
,
99 unsigned Op
, unsigned RegOpcodeField
,
102 unsigned getX86RegNum(unsigned RegNo
) const;
105 template<class CodeEmitter
>
106 char Emitter
<CodeEmitter
>::ID
= 0;
107 } // end anonymous namespace.
109 /// createX86CodeEmitterPass - Return a pass that emits the collected X86 code
110 /// to the specified templated MachineCodeEmitter object.
111 FunctionPass
*llvm::createX86JITCodeEmitterPass(X86TargetMachine
&TM
,
112 JITCodeEmitter
&JCE
) {
113 return new Emitter
<JITCodeEmitter
>(TM
, JCE
);
116 template<class CodeEmitter
>
117 bool Emitter
<CodeEmitter
>::runOnMachineFunction(MachineFunction
&MF
) {
118 MMI
= &getAnalysis
<MachineModuleInfo
>();
119 MCE
.setModuleInfo(MMI
);
121 II
= TM
.getInstrInfo();
122 TD
= TM
.getTargetData();
123 Is64BitMode
= TM
.getSubtarget
<X86Subtarget
>().is64Bit();
124 IsPIC
= TM
.getRelocationModel() == Reloc::PIC_
;
127 DEBUG(dbgs() << "JITTing function '"
128 << MF
.getFunction()->getName() << "'\n");
129 MCE
.startFunction(MF
);
130 for (MachineFunction::iterator MBB
= MF
.begin(), E
= MF
.end();
132 MCE
.StartMachineBasicBlock(MBB
);
133 for (MachineBasicBlock::iterator I
= MBB
->begin(), E
= MBB
->end();
135 const TargetInstrDesc
&Desc
= I
->getDesc();
136 emitInstruction(*I
, &Desc
);
137 // MOVPC32r is basically a call plus a pop instruction.
138 if (Desc
.getOpcode() == X86::MOVPC32r
)
139 emitInstruction(*I
, &II
->get(X86::POP32r
));
140 ++NumEmitted
; // Keep track of the # of mi's emitted
143 } while (MCE
.finishFunction(MF
));
148 /// determineREX - Determine if the MachineInstr has to be encoded with a X86-64
149 /// REX prefix which specifies 1) 64-bit instructions, 2) non-default operand
150 /// size, and 3) use of X86-64 extended registers.
151 static unsigned determineREX(const MachineInstr
&MI
) {
153 const TargetInstrDesc
&Desc
= MI
.getDesc();
155 // Pseudo instructions do not need REX prefix byte.
156 if ((Desc
.TSFlags
& X86II::FormMask
) == X86II::Pseudo
)
158 if (Desc
.TSFlags
& X86II::REX_W
)
161 unsigned NumOps
= Desc
.getNumOperands();
163 bool isTwoAddr
= NumOps
> 1 &&
164 Desc
.getOperandConstraint(1, TOI::TIED_TO
) != -1;
166 // If it accesses SPL, BPL, SIL, or DIL, then it requires a 0x40 REX prefix.
167 unsigned i
= isTwoAddr
? 1 : 0;
168 for (unsigned e
= NumOps
; i
!= e
; ++i
) {
169 const MachineOperand
& MO
= MI
.getOperand(i
);
171 unsigned Reg
= MO
.getReg();
172 if (X86InstrInfo::isX86_64NonExtLowByteReg(Reg
))
177 switch (Desc
.TSFlags
& X86II::FormMask
) {
178 case X86II::MRMInitReg
:
179 if (X86InstrInfo::isX86_64ExtendedReg(MI
.getOperand(0)))
180 REX
|= (1 << 0) | (1 << 2);
182 case X86II::MRMSrcReg
: {
183 if (X86InstrInfo::isX86_64ExtendedReg(MI
.getOperand(0)))
185 i
= isTwoAddr
? 2 : 1;
186 for (unsigned e
= NumOps
; i
!= e
; ++i
) {
187 const MachineOperand
& MO
= MI
.getOperand(i
);
188 if (X86InstrInfo::isX86_64ExtendedReg(MO
))
193 case X86II::MRMSrcMem
: {
194 if (X86InstrInfo::isX86_64ExtendedReg(MI
.getOperand(0)))
197 i
= isTwoAddr
? 2 : 1;
198 for (; i
!= NumOps
; ++i
) {
199 const MachineOperand
& MO
= MI
.getOperand(i
);
201 if (X86InstrInfo::isX86_64ExtendedReg(MO
))
208 case X86II::MRM0m
: case X86II::MRM1m
:
209 case X86II::MRM2m
: case X86II::MRM3m
:
210 case X86II::MRM4m
: case X86II::MRM5m
:
211 case X86II::MRM6m
: case X86II::MRM7m
:
212 case X86II::MRMDestMem
: {
213 unsigned e
= (isTwoAddr
? X86::AddrNumOperands
+1 : X86::AddrNumOperands
);
214 i
= isTwoAddr
? 1 : 0;
215 if (NumOps
> e
&& X86InstrInfo::isX86_64ExtendedReg(MI
.getOperand(e
)))
218 for (; i
!= e
; ++i
) {
219 const MachineOperand
& MO
= MI
.getOperand(i
);
221 if (X86InstrInfo::isX86_64ExtendedReg(MO
))
229 if (X86InstrInfo::isX86_64ExtendedReg(MI
.getOperand(0)))
231 i
= isTwoAddr
? 2 : 1;
232 for (unsigned e
= NumOps
; i
!= e
; ++i
) {
233 const MachineOperand
& MO
= MI
.getOperand(i
);
234 if (X86InstrInfo::isX86_64ExtendedReg(MO
))
245 /// emitPCRelativeBlockAddress - This method keeps track of the information
246 /// necessary to resolve the address of this block later and emits a dummy
249 template<class CodeEmitter
>
250 void Emitter
<CodeEmitter
>::emitPCRelativeBlockAddress(MachineBasicBlock
*MBB
) {
251 // Remember where this reference was and where it is to so we can
252 // deal with it later.
253 MCE
.addRelocation(MachineRelocation::getBB(MCE
.getCurrentPCOffset(),
254 X86::reloc_pcrel_word
, MBB
));
258 /// emitGlobalAddress - Emit the specified address to the code stream assuming
259 /// this is part of a "take the address of a global" instruction.
261 template<class CodeEmitter
>
262 void Emitter
<CodeEmitter
>::emitGlobalAddress(const GlobalValue
*GV
,
264 intptr_t Disp
/* = 0 */,
265 intptr_t PCAdj
/* = 0 */,
266 bool Indirect
/* = false */) {
267 intptr_t RelocCST
= Disp
;
268 if (Reloc
== X86::reloc_picrel_word
)
269 RelocCST
= PICBaseOffset
;
270 else if (Reloc
== X86::reloc_pcrel_word
)
272 MachineRelocation MR
= Indirect
273 ? MachineRelocation::getIndirectSymbol(MCE
.getCurrentPCOffset(), Reloc
,
274 const_cast<GlobalValue
*>(GV
),
276 : MachineRelocation::getGV(MCE
.getCurrentPCOffset(), Reloc
,
277 const_cast<GlobalValue
*>(GV
), RelocCST
, false);
278 MCE
.addRelocation(MR
);
279 // The relocated value will be added to the displacement
280 if (Reloc
== X86::reloc_absolute_dword
)
281 MCE
.emitDWordLE(Disp
);
283 MCE
.emitWordLE((int32_t)Disp
);
286 /// emitExternalSymbolAddress - Arrange for the address of an external symbol to
287 /// be emitted to the current location in the function, and allow it to be PC
289 template<class CodeEmitter
>
290 void Emitter
<CodeEmitter
>::emitExternalSymbolAddress(const char *ES
,
292 intptr_t RelocCST
= (Reloc
== X86::reloc_picrel_word
) ? PICBaseOffset
: 0;
294 // X86 never needs stubs because instruction selection will always pick
295 // an instruction sequence that is large enough to hold any address
297 // (see X86ISelLowering.cpp, near 2039: X86TargetLowering::LowerCall)
298 bool NeedStub
= false;
299 MCE
.addRelocation(MachineRelocation::getExtSym(MCE
.getCurrentPCOffset(),
302 if (Reloc
== X86::reloc_absolute_dword
)
308 /// emitConstPoolAddress - Arrange for the address of an constant pool
309 /// to be emitted to the current location in the function, and allow it to be PC
311 template<class CodeEmitter
>
312 void Emitter
<CodeEmitter
>::emitConstPoolAddress(unsigned CPI
, unsigned Reloc
,
313 intptr_t Disp
/* = 0 */,
314 intptr_t PCAdj
/* = 0 */) {
315 intptr_t RelocCST
= 0;
316 if (Reloc
== X86::reloc_picrel_word
)
317 RelocCST
= PICBaseOffset
;
318 else if (Reloc
== X86::reloc_pcrel_word
)
320 MCE
.addRelocation(MachineRelocation::getConstPool(MCE
.getCurrentPCOffset(),
321 Reloc
, CPI
, RelocCST
));
322 // The relocated value will be added to the displacement
323 if (Reloc
== X86::reloc_absolute_dword
)
324 MCE
.emitDWordLE(Disp
);
326 MCE
.emitWordLE((int32_t)Disp
);
329 /// emitJumpTableAddress - Arrange for the address of a jump table to
330 /// be emitted to the current location in the function, and allow it to be PC
332 template<class CodeEmitter
>
333 void Emitter
<CodeEmitter
>::emitJumpTableAddress(unsigned JTI
, unsigned Reloc
,
334 intptr_t PCAdj
/* = 0 */) {
335 intptr_t RelocCST
= 0;
336 if (Reloc
== X86::reloc_picrel_word
)
337 RelocCST
= PICBaseOffset
;
338 else if (Reloc
== X86::reloc_pcrel_word
)
340 MCE
.addRelocation(MachineRelocation::getJumpTable(MCE
.getCurrentPCOffset(),
341 Reloc
, JTI
, RelocCST
));
342 // The relocated value will be added to the displacement
343 if (Reloc
== X86::reloc_absolute_dword
)
349 template<class CodeEmitter
>
350 unsigned Emitter
<CodeEmitter
>::getX86RegNum(unsigned RegNo
) const {
351 return X86RegisterInfo::getX86RegNum(RegNo
);
354 inline static unsigned char ModRMByte(unsigned Mod
, unsigned RegOpcode
,
356 assert(Mod
< 4 && RegOpcode
< 8 && RM
< 8 && "ModRM Fields out of range!");
357 return RM
| (RegOpcode
<< 3) | (Mod
<< 6);
360 template<class CodeEmitter
>
361 void Emitter
<CodeEmitter
>::emitRegModRMByte(unsigned ModRMReg
,
362 unsigned RegOpcodeFld
){
363 MCE
.emitByte(ModRMByte(3, RegOpcodeFld
, getX86RegNum(ModRMReg
)));
366 template<class CodeEmitter
>
367 void Emitter
<CodeEmitter
>::emitRegModRMByte(unsigned RegOpcodeFld
) {
368 MCE
.emitByte(ModRMByte(3, RegOpcodeFld
, 0));
371 template<class CodeEmitter
>
372 void Emitter
<CodeEmitter
>::emitSIBByte(unsigned SS
,
375 // SIB byte is in the same format as the ModRMByte...
376 MCE
.emitByte(ModRMByte(SS
, Index
, Base
));
379 template<class CodeEmitter
>
380 void Emitter
<CodeEmitter
>::emitConstant(uint64_t Val
, unsigned Size
) {
381 // Output the constant in little endian byte order...
382 for (unsigned i
= 0; i
!= Size
; ++i
) {
383 MCE
.emitByte(Val
& 255);
388 /// isDisp8 - Return true if this signed displacement fits in a 8-bit
389 /// sign-extended field.
390 static bool isDisp8(int Value
) {
391 return Value
== (signed char)Value
;
394 static bool gvNeedsNonLazyPtr(const MachineOperand
&GVOp
,
395 const TargetMachine
&TM
) {
396 // For Darwin-64, simulate the linktime GOT by using the same non-lazy-pointer
397 // mechanism as 32-bit mode.
398 if (TM
.getSubtarget
<X86Subtarget
>().is64Bit() &&
399 !TM
.getSubtarget
<X86Subtarget
>().isTargetDarwin())
402 // Return true if this is a reference to a stub containing the address of the
403 // global, not the global itself.
404 return isGlobalStubReference(GVOp
.getTargetFlags());
407 template<class CodeEmitter
>
408 void Emitter
<CodeEmitter
>::emitDisplacementField(const MachineOperand
*RelocOp
,
410 intptr_t Adj
/* = 0 */,
411 bool IsPCRel
/* = true */) {
412 // If this is a simple integer displacement that doesn't require a relocation,
415 emitConstant(DispVal
, 4);
419 // Otherwise, this is something that requires a relocation. Emit it as such
421 unsigned RelocType
= Is64BitMode
?
422 (IsPCRel
? X86::reloc_pcrel_word
: X86::reloc_absolute_word_sext
)
423 : (IsPIC
? X86::reloc_picrel_word
: X86::reloc_absolute_word
);
424 if (RelocOp
->isGlobal()) {
425 // In 64-bit static small code model, we could potentially emit absolute.
426 // But it's probably not beneficial. If the MCE supports using RIP directly
427 // do it, otherwise fallback to absolute (this is determined by IsPCRel).
428 // 89 05 00 00 00 00 mov %eax,0(%rip) # PC-relative
429 // 89 04 25 00 00 00 00 mov %eax,0x0 # Absolute
430 bool Indirect
= gvNeedsNonLazyPtr(*RelocOp
, TM
);
431 emitGlobalAddress(RelocOp
->getGlobal(), RelocType
, RelocOp
->getOffset(),
433 } else if (RelocOp
->isSymbol()) {
434 emitExternalSymbolAddress(RelocOp
->getSymbolName(), RelocType
);
435 } else if (RelocOp
->isCPI()) {
436 emitConstPoolAddress(RelocOp
->getIndex(), RelocType
,
437 RelocOp
->getOffset(), Adj
);
439 assert(RelocOp
->isJTI() && "Unexpected machine operand!");
440 emitJumpTableAddress(RelocOp
->getIndex(), RelocType
, Adj
);
444 template<class CodeEmitter
>
445 void Emitter
<CodeEmitter
>::emitMemModRMByte(const MachineInstr
&MI
,
446 unsigned Op
,unsigned RegOpcodeField
,
448 const MachineOperand
&Op3
= MI
.getOperand(Op
+3);
450 const MachineOperand
*DispForReloc
= 0;
452 // Figure out what sort of displacement we have to handle here.
453 if (Op3
.isGlobal()) {
455 } else if (Op3
.isSymbol()) {
457 } else if (Op3
.isCPI()) {
458 if (!MCE
.earlyResolveAddresses() || Is64BitMode
|| IsPIC
) {
461 DispVal
+= MCE
.getConstantPoolEntryAddress(Op3
.getIndex());
462 DispVal
+= Op3
.getOffset();
464 } else if (Op3
.isJTI()) {
465 if (!MCE
.earlyResolveAddresses() || Is64BitMode
|| IsPIC
) {
468 DispVal
+= MCE
.getJumpTableEntryAddress(Op3
.getIndex());
471 DispVal
= Op3
.getImm();
474 const MachineOperand
&Base
= MI
.getOperand(Op
);
475 const MachineOperand
&Scale
= MI
.getOperand(Op
+1);
476 const MachineOperand
&IndexReg
= MI
.getOperand(Op
+2);
478 unsigned BaseReg
= Base
.getReg();
480 // Handle %rip relative addressing.
481 if (BaseReg
== X86::RIP
||
482 (Is64BitMode
&& DispForReloc
)) { // [disp32+RIP] in X86-64 mode
483 assert(IndexReg
.getReg() == 0 && Is64BitMode
&&
484 "Invalid rip-relative address");
485 MCE
.emitByte(ModRMByte(0, RegOpcodeField
, 5));
486 emitDisplacementField(DispForReloc
, DispVal
, PCAdj
, true);
490 // Indicate that the displacement will use an pcrel or absolute reference
491 // by default. MCEs able to resolve addresses on-the-fly use pcrel by default
492 // while others, unless explicit asked to use RIP, use absolute references.
493 bool IsPCRel
= MCE
.earlyResolveAddresses() ? true : false;
495 // Is a SIB byte needed?
496 // If no BaseReg, issue a RIP relative instruction only if the MCE can
497 // resolve addresses on-the-fly, otherwise use SIB (Intel Manual 2A, table
498 // 2-7) and absolute references.
499 unsigned BaseRegNo
= -1U;
500 if (BaseReg
!= 0 && BaseReg
!= X86::RIP
)
501 BaseRegNo
= getX86RegNum(BaseReg
);
503 if (// The SIB byte must be used if there is an index register.
504 IndexReg
.getReg() == 0 &&
505 // The SIB byte must be used if the base is ESP/RSP/R12, all of which
506 // encode to an R/M value of 4, which indicates that a SIB byte is
508 BaseRegNo
!= N86::ESP
&&
509 // If there is no base register and we're in 64-bit mode, we need a SIB
510 // byte to emit an addr that is just 'disp32' (the non-RIP relative form).
511 (!Is64BitMode
|| BaseReg
!= 0)) {
512 if (BaseReg
== 0 || // [disp32] in X86-32 mode
513 BaseReg
== X86::RIP
) { // [disp32+RIP] in X86-64 mode
514 MCE
.emitByte(ModRMByte(0, RegOpcodeField
, 5));
515 emitDisplacementField(DispForReloc
, DispVal
, PCAdj
, true);
519 // If the base is not EBP/ESP and there is no displacement, use simple
520 // indirect register encoding, this handles addresses like [EAX]. The
521 // encoding for [EBP] with no displacement means [disp32] so we handle it
522 // by emitting a displacement of 0 below.
523 if (!DispForReloc
&& DispVal
== 0 && BaseRegNo
!= N86::EBP
) {
524 MCE
.emitByte(ModRMByte(0, RegOpcodeField
, BaseRegNo
));
528 // Otherwise, if the displacement fits in a byte, encode as [REG+disp8].
529 if (!DispForReloc
&& isDisp8(DispVal
)) {
530 MCE
.emitByte(ModRMByte(1, RegOpcodeField
, BaseRegNo
));
531 emitConstant(DispVal
, 1);
535 // Otherwise, emit the most general non-SIB encoding: [REG+disp32]
536 MCE
.emitByte(ModRMByte(2, RegOpcodeField
, BaseRegNo
));
537 emitDisplacementField(DispForReloc
, DispVal
, PCAdj
, IsPCRel
);
541 // Otherwise we need a SIB byte, so start by outputting the ModR/M byte first.
542 assert(IndexReg
.getReg() != X86::ESP
&&
543 IndexReg
.getReg() != X86::RSP
&& "Cannot use ESP as index reg!");
545 bool ForceDisp32
= false;
546 bool ForceDisp8
= false;
548 // If there is no base register, we emit the special case SIB byte with
549 // MOD=0, BASE=4, to JUST get the index, scale, and displacement.
550 MCE
.emitByte(ModRMByte(0, RegOpcodeField
, 4));
552 } else if (DispForReloc
) {
553 // Emit the normal disp32 encoding.
554 MCE
.emitByte(ModRMByte(2, RegOpcodeField
, 4));
556 } else if (DispVal
== 0 && BaseRegNo
!= N86::EBP
) {
557 // Emit no displacement ModR/M byte
558 MCE
.emitByte(ModRMByte(0, RegOpcodeField
, 4));
559 } else if (isDisp8(DispVal
)) {
560 // Emit the disp8 encoding...
561 MCE
.emitByte(ModRMByte(1, RegOpcodeField
, 4));
562 ForceDisp8
= true; // Make sure to force 8 bit disp if Base=EBP
564 // Emit the normal disp32 encoding...
565 MCE
.emitByte(ModRMByte(2, RegOpcodeField
, 4));
568 // Calculate what the SS field value should be...
569 static const unsigned SSTable
[] = { ~0, 0, 1, ~0, 2, ~0, ~0, ~0, 3 };
570 unsigned SS
= SSTable
[Scale
.getImm()];
573 // Handle the SIB byte for the case where there is no base, see Intel
574 // Manual 2A, table 2-7. The displacement has already been output.
576 if (IndexReg
.getReg())
577 IndexRegNo
= getX86RegNum(IndexReg
.getReg());
578 else // Examples: [ESP+1*<noreg>+4] or [scaled idx]+disp32 (MOD=0,BASE=5)
580 emitSIBByte(SS
, IndexRegNo
, 5);
582 unsigned BaseRegNo
= getX86RegNum(BaseReg
);
584 if (IndexReg
.getReg())
585 IndexRegNo
= getX86RegNum(IndexReg
.getReg());
587 IndexRegNo
= 4; // For example [ESP+1*<noreg>+4]
588 emitSIBByte(SS
, IndexRegNo
, BaseRegNo
);
591 // Do we need to output a displacement?
593 emitConstant(DispVal
, 1);
594 } else if (DispVal
!= 0 || ForceDisp32
) {
595 emitDisplacementField(DispForReloc
, DispVal
, PCAdj
, IsPCRel
);
599 template<class CodeEmitter
>
600 void Emitter
<CodeEmitter
>::emitInstruction(MachineInstr
&MI
,
601 const TargetInstrDesc
*Desc
) {
604 // If this is a pseudo instruction, lower it.
605 switch (Desc
->getOpcode()) {
606 case X86::ADD16rr_DB
: Desc
= &II
->get(X86::OR16rr
); MI
.setDesc(*Desc
);break;
607 case X86::ADD32rr_DB
: Desc
= &II
->get(X86::OR32rr
); MI
.setDesc(*Desc
);break;
608 case X86::ADD64rr_DB
: Desc
= &II
->get(X86::OR64rr
); MI
.setDesc(*Desc
);break;
609 case X86::ADD16ri_DB
: Desc
= &II
->get(X86::OR16ri
); MI
.setDesc(*Desc
);break;
610 case X86::ADD32ri_DB
: Desc
= &II
->get(X86::OR32ri
); MI
.setDesc(*Desc
);break;
611 case X86::ADD64ri32_DB
:Desc
= &II
->get(X86::OR64ri32
);MI
.setDesc(*Desc
);break;
612 case X86::ADD16ri8_DB
: Desc
= &II
->get(X86::OR16ri8
);MI
.setDesc(*Desc
);break;
613 case X86::ADD32ri8_DB
: Desc
= &II
->get(X86::OR32ri8
);MI
.setDesc(*Desc
);break;
614 case X86::ADD64ri8_DB
: Desc
= &II
->get(X86::OR64ri8
);MI
.setDesc(*Desc
);break;
618 MCE
.processDebugLoc(MI
.getDebugLoc(), true);
620 unsigned Opcode
= Desc
->Opcode
;
622 // Emit the lock opcode prefix as needed.
623 if (Desc
->TSFlags
& X86II::LOCK
)
626 // Emit segment override opcode prefix as needed.
627 switch (Desc
->TSFlags
& X86II::SegOvrMask
) {
634 default: llvm_unreachable("Invalid segment!");
635 case 0: break; // No segment override!
638 // Emit the repeat opcode prefix as needed.
639 if ((Desc
->TSFlags
& X86II::Op0Mask
) == X86II::REP
)
642 // Emit the operand size opcode prefix as needed.
643 if (Desc
->TSFlags
& X86II::OpSize
)
646 // Emit the address size opcode prefix as needed.
647 if (Desc
->TSFlags
& X86II::AdSize
)
650 bool Need0FPrefix
= false;
651 switch (Desc
->TSFlags
& X86II::Op0Mask
) {
652 case X86II::TB
: // Two-byte opcode prefix
653 case X86II::T8
: // 0F 38
654 case X86II::TA
: // 0F 3A
655 case X86II::A6
: // 0F A6
656 case X86II::A7
: // 0F A7
659 case X86II::TF
: // F2 0F 38
663 case X86II::REP
: break; // already handled.
664 case X86II::XS
: // F3 0F
668 case X86II::XD
: // F2 0F
672 case X86II::D8
: case X86II::D9
: case X86II::DA
: case X86II::DB
:
673 case X86II::DC
: case X86II::DD
: case X86II::DE
: case X86II::DF
:
675 (((Desc
->TSFlags
& X86II::Op0Mask
)-X86II::D8
)
676 >> X86II::Op0Shift
));
677 break; // Two-byte opcode prefix
678 default: llvm_unreachable("Invalid prefix!");
679 case 0: break; // No prefix!
682 // Handle REX prefix.
684 if (unsigned REX
= determineREX(MI
))
685 MCE
.emitByte(0x40 | REX
);
688 // 0x0F escape code must be emitted just before the opcode.
692 switch (Desc
->TSFlags
& X86II::Op0Mask
) {
693 case X86II::TF
: // F2 0F 38
694 case X86II::T8
: // 0F 38
697 case X86II::TA
: // 0F 3A
700 case X86II::A6
: // 0F A6
703 case X86II::A7
: // 0F A7
708 // If this is a two-address instruction, skip one of the register operands.
709 unsigned NumOps
= Desc
->getNumOperands();
711 if (NumOps
> 1 && Desc
->getOperandConstraint(1, TOI::TIED_TO
) != -1)
713 else if (NumOps
> 2 && Desc
->getOperandConstraint(NumOps
-1, TOI::TIED_TO
)== 0)
714 // Skip the last source operand that is tied_to the dest reg. e.g. LXADD32
717 unsigned char BaseOpcode
= X86II::getBaseOpcodeFor(Desc
->TSFlags
);
718 switch (Desc
->TSFlags
& X86II::FormMask
) {
720 llvm_unreachable("Unknown FormMask value in X86 MachineCodeEmitter!");
722 // Remember the current PC offset, this is the PIC relocation
726 llvm_unreachable("pseudo instructions should be removed before code"
729 // Do nothing for Int_MemBarrier - it's just a comment. Add a debug
730 // to make it slightly easier to see.
731 case X86::Int_MemBarrier
:
732 DEBUG(dbgs() << "#MEMBARRIER\n");
735 case TargetOpcode::INLINEASM
:
736 // We allow inline assembler nodes with empty bodies - they can
737 // implicitly define registers, which is ok for JIT.
738 if (MI
.getOperand(0).getSymbolName()[0])
739 report_fatal_error("JIT does not support inline asm!");
741 case TargetOpcode::PROLOG_LABEL
:
742 case TargetOpcode::GC_LABEL
:
743 case TargetOpcode::EH_LABEL
:
744 MCE
.emitLabel(MI
.getOperand(0).getMCSymbol());
747 case TargetOpcode::IMPLICIT_DEF
:
748 case TargetOpcode::KILL
:
750 case X86::MOVPC32r
: {
751 // This emits the "call" portion of this pseudo instruction.
752 MCE
.emitByte(BaseOpcode
);
753 emitConstant(0, X86II::getSizeOfImm(Desc
->TSFlags
));
754 // Remember PIC base.
755 PICBaseOffset
= (intptr_t) MCE
.getCurrentPCOffset();
756 X86JITInfo
*JTI
= TM
.getJITInfo();
757 JTI
->setPICBase(MCE
.getCurrentPCValue());
763 case X86II::RawFrm
: {
764 MCE
.emitByte(BaseOpcode
);
769 const MachineOperand
&MO
= MI
.getOperand(CurOp
++);
771 DEBUG(dbgs() << "RawFrm CurOp " << CurOp
<< "\n");
772 DEBUG(dbgs() << "isMBB " << MO
.isMBB() << "\n");
773 DEBUG(dbgs() << "isGlobal " << MO
.isGlobal() << "\n");
774 DEBUG(dbgs() << "isSymbol " << MO
.isSymbol() << "\n");
775 DEBUG(dbgs() << "isImm " << MO
.isImm() << "\n");
778 emitPCRelativeBlockAddress(MO
.getMBB());
783 emitGlobalAddress(MO
.getGlobal(), X86::reloc_pcrel_word
,
789 emitExternalSymbolAddress(MO
.getSymbolName(), X86::reloc_pcrel_word
);
793 // FIXME: Only used by hackish MCCodeEmitter, remove when dead.
795 emitJumpTableAddress(MO
.getIndex(), X86::reloc_pcrel_word
);
799 assert(MO
.isImm() && "Unknown RawFrm operand!");
800 if (Opcode
== X86::CALLpcrel32
|| Opcode
== X86::CALL64pcrel32
||
801 Opcode
== X86::WINCALL64pcrel32
) {
802 // Fix up immediate operand for pc relative calls.
803 intptr_t Imm
= (intptr_t)MO
.getImm();
804 Imm
= Imm
- MCE
.getCurrentPCValue() - 4;
805 emitConstant(Imm
, X86II::getSizeOfImm(Desc
->TSFlags
));
807 emitConstant(MO
.getImm(), X86II::getSizeOfImm(Desc
->TSFlags
));
811 case X86II::AddRegFrm
: {
812 MCE
.emitByte(BaseOpcode
+ getX86RegNum(MI
.getOperand(CurOp
++).getReg()));
817 const MachineOperand
&MO1
= MI
.getOperand(CurOp
++);
818 unsigned Size
= X86II::getSizeOfImm(Desc
->TSFlags
);
820 emitConstant(MO1
.getImm(), Size
);
824 unsigned rt
= Is64BitMode
? X86::reloc_pcrel_word
825 : (IsPIC
? X86::reloc_picrel_word
: X86::reloc_absolute_word
);
826 if (Opcode
== X86::MOV64ri64i32
)
827 rt
= X86::reloc_absolute_word
; // FIXME: add X86II flag?
828 // This should not occur on Darwin for relocatable objects.
829 if (Opcode
== X86::MOV64ri
)
830 rt
= X86::reloc_absolute_dword
; // FIXME: add X86II flag?
831 if (MO1
.isGlobal()) {
832 bool Indirect
= gvNeedsNonLazyPtr(MO1
, TM
);
833 emitGlobalAddress(MO1
.getGlobal(), rt
, MO1
.getOffset(), 0,
835 } else if (MO1
.isSymbol())
836 emitExternalSymbolAddress(MO1
.getSymbolName(), rt
);
837 else if (MO1
.isCPI())
838 emitConstPoolAddress(MO1
.getIndex(), rt
);
839 else if (MO1
.isJTI())
840 emitJumpTableAddress(MO1
.getIndex(), rt
);
844 case X86II::MRMDestReg
: {
845 MCE
.emitByte(BaseOpcode
);
846 emitRegModRMByte(MI
.getOperand(CurOp
).getReg(),
847 getX86RegNum(MI
.getOperand(CurOp
+1).getReg()));
850 emitConstant(MI
.getOperand(CurOp
++).getImm(),
851 X86II::getSizeOfImm(Desc
->TSFlags
));
854 case X86II::MRMDestMem
: {
855 MCE
.emitByte(BaseOpcode
);
856 emitMemModRMByte(MI
, CurOp
,
857 getX86RegNum(MI
.getOperand(CurOp
+ X86::AddrNumOperands
)
859 CurOp
+= X86::AddrNumOperands
+ 1;
861 emitConstant(MI
.getOperand(CurOp
++).getImm(),
862 X86II::getSizeOfImm(Desc
->TSFlags
));
866 case X86II::MRMSrcReg
:
867 MCE
.emitByte(BaseOpcode
);
868 emitRegModRMByte(MI
.getOperand(CurOp
+1).getReg(),
869 getX86RegNum(MI
.getOperand(CurOp
).getReg()));
872 emitConstant(MI
.getOperand(CurOp
++).getImm(),
873 X86II::getSizeOfImm(Desc
->TSFlags
));
876 case X86II::MRMSrcMem
: {
877 int AddrOperands
= X86::AddrNumOperands
;
879 intptr_t PCAdj
= (CurOp
+ AddrOperands
+ 1 != NumOps
) ?
880 X86II::getSizeOfImm(Desc
->TSFlags
) : 0;
882 MCE
.emitByte(BaseOpcode
);
883 emitMemModRMByte(MI
, CurOp
+1, getX86RegNum(MI
.getOperand(CurOp
).getReg()),
885 CurOp
+= AddrOperands
+ 1;
887 emitConstant(MI
.getOperand(CurOp
++).getImm(),
888 X86II::getSizeOfImm(Desc
->TSFlags
));
892 case X86II::MRM0r
: case X86II::MRM1r
:
893 case X86II::MRM2r
: case X86II::MRM3r
:
894 case X86II::MRM4r
: case X86II::MRM5r
:
895 case X86II::MRM6r
: case X86II::MRM7r
: {
896 MCE
.emitByte(BaseOpcode
);
897 emitRegModRMByte(MI
.getOperand(CurOp
++).getReg(),
898 (Desc
->TSFlags
& X86II::FormMask
)-X86II::MRM0r
);
903 const MachineOperand
&MO1
= MI
.getOperand(CurOp
++);
904 unsigned Size
= X86II::getSizeOfImm(Desc
->TSFlags
);
906 emitConstant(MO1
.getImm(), Size
);
910 unsigned rt
= Is64BitMode
? X86::reloc_pcrel_word
911 : (IsPIC
? X86::reloc_picrel_word
: X86::reloc_absolute_word
);
912 if (Opcode
== X86::MOV64ri32
)
913 rt
= X86::reloc_absolute_word_sext
; // FIXME: add X86II flag?
914 if (MO1
.isGlobal()) {
915 bool Indirect
= gvNeedsNonLazyPtr(MO1
, TM
);
916 emitGlobalAddress(MO1
.getGlobal(), rt
, MO1
.getOffset(), 0,
918 } else if (MO1
.isSymbol())
919 emitExternalSymbolAddress(MO1
.getSymbolName(), rt
);
920 else if (MO1
.isCPI())
921 emitConstPoolAddress(MO1
.getIndex(), rt
);
922 else if (MO1
.isJTI())
923 emitJumpTableAddress(MO1
.getIndex(), rt
);
927 case X86II::MRM0m
: case X86II::MRM1m
:
928 case X86II::MRM2m
: case X86II::MRM3m
:
929 case X86II::MRM4m
: case X86II::MRM5m
:
930 case X86II::MRM6m
: case X86II::MRM7m
: {
931 intptr_t PCAdj
= (CurOp
+ X86::AddrNumOperands
!= NumOps
) ?
932 (MI
.getOperand(CurOp
+X86::AddrNumOperands
).isImm() ?
933 X86II::getSizeOfImm(Desc
->TSFlags
) : 4) : 0;
935 MCE
.emitByte(BaseOpcode
);
936 emitMemModRMByte(MI
, CurOp
, (Desc
->TSFlags
& X86II::FormMask
)-X86II::MRM0m
,
938 CurOp
+= X86::AddrNumOperands
;
943 const MachineOperand
&MO
= MI
.getOperand(CurOp
++);
944 unsigned Size
= X86II::getSizeOfImm(Desc
->TSFlags
);
946 emitConstant(MO
.getImm(), Size
);
950 unsigned rt
= Is64BitMode
? X86::reloc_pcrel_word
951 : (IsPIC
? X86::reloc_picrel_word
: X86::reloc_absolute_word
);
952 if (Opcode
== X86::MOV64mi32
)
953 rt
= X86::reloc_absolute_word_sext
; // FIXME: add X86II flag?
955 bool Indirect
= gvNeedsNonLazyPtr(MO
, TM
);
956 emitGlobalAddress(MO
.getGlobal(), rt
, MO
.getOffset(), 0,
958 } else if (MO
.isSymbol())
959 emitExternalSymbolAddress(MO
.getSymbolName(), rt
);
961 emitConstPoolAddress(MO
.getIndex(), rt
);
963 emitJumpTableAddress(MO
.getIndex(), rt
);
967 case X86II::MRMInitReg
:
968 MCE
.emitByte(BaseOpcode
);
969 // Duplicate register, used by things like MOV8r0 (aka xor reg,reg).
970 emitRegModRMByte(MI
.getOperand(CurOp
).getReg(),
971 getX86RegNum(MI
.getOperand(CurOp
).getReg()));
976 MCE
.emitByte(BaseOpcode
);
980 MCE
.emitByte(BaseOpcode
);
984 MCE
.emitByte(BaseOpcode
);
988 MCE
.emitByte(BaseOpcode
);
992 MCE
.emitByte(BaseOpcode
);
997 if (!Desc
->isVariadic() && CurOp
!= NumOps
) {
999 dbgs() << "Cannot encode all operands of: " << MI
<< "\n";
1001 llvm_unreachable(0);
1004 MCE
.processDebugLoc(MI
.getDebugLoc(), false);