blob: 1c81b4b6fc042755d247736b090882da949725b9 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===-- X86/X86CodeEmitter.cpp - Convert X86 code to machine code ---------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file contains the pass that transforms the X86 machine instructions into
11// relocatable machine code.
12//
13//===----------------------------------------------------------------------===//
14
15#define DEBUG_TYPE "x86-emitter"
16#include "X86InstrInfo.h"
Evan Chengaf743252008-01-05 02:26:58 +000017#include "X86JITInfo.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000018#include "X86Subtarget.h"
19#include "X86TargetMachine.h"
20#include "X86Relocations.h"
21#include "X86.h"
22#include "llvm/PassManager.h"
23#include "llvm/CodeGen/MachineCodeEmitter.h"
24#include "llvm/CodeGen/MachineFunctionPass.h"
25#include "llvm/CodeGen/MachineInstr.h"
Nicolas Geoffray0e757e12008-02-13 18:39:37 +000026#include "llvm/CodeGen/MachineModuleInfo.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000027#include "llvm/CodeGen/Passes.h"
28#include "llvm/Function.h"
29#include "llvm/ADT/Statistic.h"
30#include "llvm/Support/Compiler.h"
Evan Cheng872bd4b2008-03-14 07:13:42 +000031#include "llvm/Support/Debug.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000032#include "llvm/Target/TargetOptions.h"
33using namespace llvm;
34
35STATISTIC(NumEmitted, "Number of machine instructions emitted");
36
37namespace {
38 class VISIBILITY_HIDDEN Emitter : public MachineFunctionPass {
39 const X86InstrInfo *II;
40 const TargetData *TD;
Dan Gohmanb41dfba2008-05-14 01:58:56 +000041 X86TargetMachine &TM;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000042 MachineCodeEmitter &MCE;
Evan Chengaf743252008-01-05 02:26:58 +000043 intptr_t PICBaseOffset;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000044 bool Is64BitMode;
Evan Cheng8ee6bab2007-12-22 09:40:20 +000045 bool IsPIC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000046 public:
47 static char ID;
Dan Gohmanb41dfba2008-05-14 01:58:56 +000048 explicit Emitter(X86TargetMachine &tm, MachineCodeEmitter &mce)
Dan Gohman26f8c272008-09-04 17:05:41 +000049 : MachineFunctionPass(&ID), II(0), TD(0), TM(tm),
Evan Chengaf743252008-01-05 02:26:58 +000050 MCE(mce), PICBaseOffset(0), Is64BitMode(false),
Evan Cheng28e7e162008-01-04 10:46:51 +000051 IsPIC(TM.getRelocationModel() == Reloc::PIC_) {}
Dan Gohmanb41dfba2008-05-14 01:58:56 +000052 Emitter(X86TargetMachine &tm, MachineCodeEmitter &mce,
Dan Gohmanf17a25c2007-07-18 16:29:46 +000053 const X86InstrInfo &ii, const TargetData &td, bool is64)
Dan Gohman26f8c272008-09-04 17:05:41 +000054 : MachineFunctionPass(&ID), II(&ii), TD(&td), TM(tm),
Evan Chengaf743252008-01-05 02:26:58 +000055 MCE(mce), PICBaseOffset(0), Is64BitMode(is64),
Evan Cheng28e7e162008-01-04 10:46:51 +000056 IsPIC(TM.getRelocationModel() == Reloc::PIC_) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +000057
58 bool runOnMachineFunction(MachineFunction &MF);
59
60 virtual const char *getPassName() const {
61 return "X86 Machine Code Emitter";
62 }
63
Evan Cheng0729ccf2008-01-05 00:41:47 +000064 void emitInstruction(const MachineInstr &MI,
Chris Lattner5b930372008-01-07 07:27:27 +000065 const TargetInstrDesc *Desc);
Nicolas Geoffray0e757e12008-02-13 18:39:37 +000066
67 void getAnalysisUsage(AnalysisUsage &AU) const {
68 AU.addRequired<MachineModuleInfo>();
69 MachineFunctionPass::getAnalysisUsage(AU);
70 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +000071
72 private:
73 void emitPCRelativeBlockAddress(MachineBasicBlock *MBB);
Evan Cheng8ee6bab2007-12-22 09:40:20 +000074 void emitGlobalAddress(GlobalValue *GV, unsigned Reloc,
75 int Disp = 0, intptr_t PCAdj = 0,
Evan Cheng28e7e162008-01-04 10:46:51 +000076 bool NeedStub = false, bool IsLazy = false);
Evan Chengf0123872008-01-03 02:56:28 +000077 void emitExternalSymbolAddress(const char *ES, unsigned Reloc);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000078 void emitConstPoolAddress(unsigned CPI, unsigned Reloc, int Disp = 0,
Evan Chengf0123872008-01-03 02:56:28 +000079 intptr_t PCAdj = 0);
Evan Cheng8ee6bab2007-12-22 09:40:20 +000080 void emitJumpTableAddress(unsigned JTI, unsigned Reloc,
Evan Chengf0123872008-01-03 02:56:28 +000081 intptr_t PCAdj = 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000082
83 void emitDisplacementField(const MachineOperand *RelocOp, int DispVal,
Evan Cheng8ee6bab2007-12-22 09:40:20 +000084 intptr_t PCAdj = 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000085
86 void emitRegModRMByte(unsigned ModRMReg, unsigned RegOpcodeField);
87 void emitSIBByte(unsigned SS, unsigned Index, unsigned Base);
88 void emitConstant(uint64_t Val, unsigned Size);
89
90 void emitMemModRMByte(const MachineInstr &MI,
91 unsigned Op, unsigned RegOpcodeField,
Evan Cheng8ee6bab2007-12-22 09:40:20 +000092 intptr_t PCAdj = 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000093
Dan Gohman06844672008-02-08 03:29:40 +000094 unsigned getX86RegNum(unsigned RegNo) const;
Evan Cheng28e7e162008-01-04 10:46:51 +000095
96 bool gvNeedsLazyPtr(const GlobalValue *GV);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000097 };
98 char Emitter::ID = 0;
99}
100
101/// createX86CodeEmitterPass - Return a pass that emits the collected X86 code
102/// to the specified MCE object.
103FunctionPass *llvm::createX86CodeEmitterPass(X86TargetMachine &TM,
104 MachineCodeEmitter &MCE) {
105 return new Emitter(TM, MCE);
106}
107
108bool Emitter::runOnMachineFunction(MachineFunction &MF) {
Dale Johannesenc501c082008-08-11 23:46:25 +0000109
Nicolas Geoffray0e757e12008-02-13 18:39:37 +0000110 MCE.setModuleInfo(&getAnalysis<MachineModuleInfo>());
111
Dan Gohmanb41dfba2008-05-14 01:58:56 +0000112 II = TM.getInstrInfo();
113 TD = TM.getTargetData();
Evan Cheng28e7e162008-01-04 10:46:51 +0000114 Is64BitMode = TM.getSubtarget<X86Subtarget>().is64Bit();
Evan Chengae50ca32008-05-20 01:56:59 +0000115 IsPIC = TM.getRelocationModel() == Reloc::PIC_;
Nicolas Geoffray0e757e12008-02-13 18:39:37 +0000116
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000117 do {
Evan Cheng872bd4b2008-03-14 07:13:42 +0000118 DOUT << "JITTing function '" << MF.getFunction()->getName() << "'\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000119 MCE.startFunction(MF);
120 for (MachineFunction::iterator MBB = MF.begin(), E = MF.end();
121 MBB != E; ++MBB) {
122 MCE.StartMachineBasicBlock(MBB);
123 for (MachineBasicBlock::const_iterator I = MBB->begin(), E = MBB->end();
Evan Cheng0729ccf2008-01-05 00:41:47 +0000124 I != E; ++I) {
Chris Lattner5b930372008-01-07 07:27:27 +0000125 const TargetInstrDesc &Desc = I->getDesc();
126 emitInstruction(*I, &Desc);
Evan Cheng0729ccf2008-01-05 00:41:47 +0000127 // MOVPC32r is basically a call plus a pop instruction.
Chris Lattner5b930372008-01-07 07:27:27 +0000128 if (Desc.getOpcode() == X86::MOVPC32r)
Evan Cheng0729ccf2008-01-05 00:41:47 +0000129 emitInstruction(*I, &II->get(X86::POP32r));
130 NumEmitted++; // Keep track of the # of mi's emitted
131 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000132 }
133 } while (MCE.finishFunction(MF));
134
135 return false;
136}
137
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000138/// emitPCRelativeBlockAddress - This method keeps track of the information
139/// necessary to resolve the address of this block later and emits a dummy
140/// value.
141///
142void Emitter::emitPCRelativeBlockAddress(MachineBasicBlock *MBB) {
143 // Remember where this reference was and where it is to so we can
144 // deal with it later.
145 MCE.addRelocation(MachineRelocation::getBB(MCE.getCurrentPCOffset(),
146 X86::reloc_pcrel_word, MBB));
147 MCE.emitWordLE(0);
148}
149
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000150/// emitGlobalAddress - Emit the specified address to the code stream assuming
151/// this is part of a "take the address of a global" instruction.
152///
Evan Cheng8ee6bab2007-12-22 09:40:20 +0000153void Emitter::emitGlobalAddress(GlobalValue *GV, unsigned Reloc,
154 int Disp /* = 0 */, intptr_t PCAdj /* = 0 */,
Evan Cheng28e7e162008-01-04 10:46:51 +0000155 bool NeedStub /* = false */,
156 bool isLazy /* = false */) {
157 intptr_t RelocCST = 0;
Evan Chengf0123872008-01-03 02:56:28 +0000158 if (Reloc == X86::reloc_picrel_word)
Evan Chengaf743252008-01-05 02:26:58 +0000159 RelocCST = PICBaseOffset;
Evan Cheng28e7e162008-01-04 10:46:51 +0000160 else if (Reloc == X86::reloc_pcrel_word)
161 RelocCST = PCAdj;
162 MachineRelocation MR = isLazy
163 ? MachineRelocation::getGVLazyPtr(MCE.getCurrentPCOffset(), Reloc,
164 GV, RelocCST, NeedStub)
165 : MachineRelocation::getGV(MCE.getCurrentPCOffset(), Reloc,
166 GV, RelocCST, NeedStub);
167 MCE.addRelocation(MR);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000168 if (Reloc == X86::reloc_absolute_dword)
169 MCE.emitWordLE(0);
170 MCE.emitWordLE(Disp); // The relocated value will be added to the displacement
171}
172
173/// emitExternalSymbolAddress - Arrange for the address of an external symbol to
174/// be emitted to the current location in the function, and allow it to be PC
175/// relative.
Evan Chengf0123872008-01-03 02:56:28 +0000176void Emitter::emitExternalSymbolAddress(const char *ES, unsigned Reloc) {
Evan Chengaf743252008-01-05 02:26:58 +0000177 intptr_t RelocCST = (Reloc == X86::reloc_picrel_word) ? PICBaseOffset : 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000178 MCE.addRelocation(MachineRelocation::getExtSym(MCE.getCurrentPCOffset(),
Evan Cheng28e7e162008-01-04 10:46:51 +0000179 Reloc, ES, RelocCST));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000180 if (Reloc == X86::reloc_absolute_dword)
181 MCE.emitWordLE(0);
182 MCE.emitWordLE(0);
183}
184
185/// emitConstPoolAddress - Arrange for the address of an constant pool
186/// to be emitted to the current location in the function, and allow it to be PC
187/// relative.
188void Emitter::emitConstPoolAddress(unsigned CPI, unsigned Reloc,
189 int Disp /* = 0 */,
Evan Chengf0123872008-01-03 02:56:28 +0000190 intptr_t PCAdj /* = 0 */) {
Evan Cheng28e7e162008-01-04 10:46:51 +0000191 intptr_t RelocCST = 0;
Evan Chengf0123872008-01-03 02:56:28 +0000192 if (Reloc == X86::reloc_picrel_word)
Evan Chengaf743252008-01-05 02:26:58 +0000193 RelocCST = PICBaseOffset;
Evan Cheng28e7e162008-01-04 10:46:51 +0000194 else if (Reloc == X86::reloc_pcrel_word)
195 RelocCST = PCAdj;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000196 MCE.addRelocation(MachineRelocation::getConstPool(MCE.getCurrentPCOffset(),
Evan Cheng28e7e162008-01-04 10:46:51 +0000197 Reloc, CPI, RelocCST));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000198 if (Reloc == X86::reloc_absolute_dword)
199 MCE.emitWordLE(0);
200 MCE.emitWordLE(Disp); // The relocated value will be added to the displacement
201}
202
203/// emitJumpTableAddress - Arrange for the address of a jump table to
204/// be emitted to the current location in the function, and allow it to be PC
205/// relative.
206void Emitter::emitJumpTableAddress(unsigned JTI, unsigned Reloc,
Evan Chengf0123872008-01-03 02:56:28 +0000207 intptr_t PCAdj /* = 0 */) {
Evan Cheng28e7e162008-01-04 10:46:51 +0000208 intptr_t RelocCST = 0;
Evan Chengf0123872008-01-03 02:56:28 +0000209 if (Reloc == X86::reloc_picrel_word)
Evan Chengaf743252008-01-05 02:26:58 +0000210 RelocCST = PICBaseOffset;
Evan Cheng28e7e162008-01-04 10:46:51 +0000211 else if (Reloc == X86::reloc_pcrel_word)
212 RelocCST = PCAdj;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000213 MCE.addRelocation(MachineRelocation::getJumpTable(MCE.getCurrentPCOffset(),
Evan Cheng28e7e162008-01-04 10:46:51 +0000214 Reloc, JTI, RelocCST));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000215 if (Reloc == X86::reloc_absolute_dword)
216 MCE.emitWordLE(0);
217 MCE.emitWordLE(0); // The relocated value will be added to the displacement
218}
219
Dan Gohman06844672008-02-08 03:29:40 +0000220unsigned Emitter::getX86RegNum(unsigned RegNo) const {
Dan Gohmanb41dfba2008-05-14 01:58:56 +0000221 return II->getRegisterInfo().getX86RegNum(RegNo);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000222}
223
224inline static unsigned char ModRMByte(unsigned Mod, unsigned RegOpcode,
225 unsigned RM) {
226 assert(Mod < 4 && RegOpcode < 8 && RM < 8 && "ModRM Fields out of range!");
227 return RM | (RegOpcode << 3) | (Mod << 6);
228}
229
230void Emitter::emitRegModRMByte(unsigned ModRMReg, unsigned RegOpcodeFld){
231 MCE.emitByte(ModRMByte(3, RegOpcodeFld, getX86RegNum(ModRMReg)));
232}
233
234void Emitter::emitSIBByte(unsigned SS, unsigned Index, unsigned Base) {
235 // SIB byte is in the same format as the ModRMByte...
236 MCE.emitByte(ModRMByte(SS, Index, Base));
237}
238
239void Emitter::emitConstant(uint64_t Val, unsigned Size) {
240 // Output the constant in little endian byte order...
241 for (unsigned i = 0; i != Size; ++i) {
242 MCE.emitByte(Val & 255);
243 Val >>= 8;
244 }
245}
246
247/// isDisp8 - Return true if this signed displacement fits in a 8-bit
248/// sign-extended field.
249static bool isDisp8(int Value) {
250 return Value == (signed char)Value;
251}
252
Evan Cheng28e7e162008-01-04 10:46:51 +0000253bool Emitter::gvNeedsLazyPtr(const GlobalValue *GV) {
Dale Johannesen2b65b742008-08-12 18:23:48 +0000254 // For Darwin, simulate the linktime GOT by using the same lazy-pointer
255 // mechanism as 32-bit mode.
256 return (!Is64BitMode || TM.getSubtarget<X86Subtarget>().isTargetDarwin()) &&
Evan Cheng28e7e162008-01-04 10:46:51 +0000257 TM.getSubtarget<X86Subtarget>().GVRequiresExtraLoad(GV, TM, false);
258}
259
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000260void Emitter::emitDisplacementField(const MachineOperand *RelocOp,
Evan Cheng8ee6bab2007-12-22 09:40:20 +0000261 int DispVal, intptr_t PCAdj) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000262 // If this is a simple integer displacement that doesn't require a relocation,
263 // emit it now.
264 if (!RelocOp) {
265 emitConstant(DispVal, 4);
266 return;
267 }
268
269 // Otherwise, this is something that requires a relocation. Emit it as such
270 // now.
271 if (RelocOp->isGlobalAddress()) {
272 // In 64-bit static small code model, we could potentially emit absolute.
273 // But it's probably not beneficial.
Bill Wendlingf3a655f2008-02-26 10:57:23 +0000274 // 89 05 00 00 00 00 mov %eax,0(%rip) # PC-relative
275 // 89 04 25 00 00 00 00 mov %eax,0x0 # Absolute
Evan Chengf0123872008-01-03 02:56:28 +0000276 unsigned rt = Is64BitMode ? X86::reloc_pcrel_word
Evan Cheng8ee6bab2007-12-22 09:40:20 +0000277 : (IsPIC ? X86::reloc_picrel_word : X86::reloc_absolute_word);
Evan Cheng28e7e162008-01-04 10:46:51 +0000278 bool NeedStub = isa<Function>(RelocOp->getGlobal());
279 bool isLazy = gvNeedsLazyPtr(RelocOp->getGlobal());
Evan Cheng8ee6bab2007-12-22 09:40:20 +0000280 emitGlobalAddress(RelocOp->getGlobal(), rt, RelocOp->getOffset(),
Evan Cheng28e7e162008-01-04 10:46:51 +0000281 PCAdj, NeedStub, isLazy);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000282 } else if (RelocOp->isConstantPoolIndex()) {
Evan Cheng8c872652008-01-02 23:38:59 +0000283 unsigned rt = Is64BitMode ? X86::reloc_pcrel_word : X86::reloc_picrel_word;
284 emitConstPoolAddress(RelocOp->getIndex(), rt,
Evan Chengf0123872008-01-03 02:56:28 +0000285 RelocOp->getOffset(), PCAdj);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000286 } else if (RelocOp->isJumpTableIndex()) {
Evan Cheng8c872652008-01-02 23:38:59 +0000287 unsigned rt = Is64BitMode ? X86::reloc_pcrel_word : X86::reloc_picrel_word;
Evan Chengf0123872008-01-03 02:56:28 +0000288 emitJumpTableAddress(RelocOp->getIndex(), rt, PCAdj);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000289 } else {
290 assert(0 && "Unknown value to relocate!");
291 }
292}
293
294void Emitter::emitMemModRMByte(const MachineInstr &MI,
295 unsigned Op, unsigned RegOpcodeField,
Evan Cheng8ee6bab2007-12-22 09:40:20 +0000296 intptr_t PCAdj) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000297 const MachineOperand &Op3 = MI.getOperand(Op+3);
298 int DispVal = 0;
299 const MachineOperand *DispForReloc = 0;
300
301 // Figure out what sort of displacement we have to handle here.
302 if (Op3.isGlobalAddress()) {
303 DispForReloc = &Op3;
304 } else if (Op3.isConstantPoolIndex()) {
Evan Cheng8c872652008-01-02 23:38:59 +0000305 if (Is64BitMode || IsPIC) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000306 DispForReloc = &Op3;
307 } else {
Chris Lattner6017d482007-12-30 23:10:15 +0000308 DispVal += MCE.getConstantPoolEntryAddress(Op3.getIndex());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000309 DispVal += Op3.getOffset();
310 }
311 } else if (Op3.isJumpTableIndex()) {
Evan Cheng8c872652008-01-02 23:38:59 +0000312 if (Is64BitMode || IsPIC) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000313 DispForReloc = &Op3;
314 } else {
Chris Lattner6017d482007-12-30 23:10:15 +0000315 DispVal += MCE.getJumpTableEntryAddress(Op3.getIndex());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000316 }
317 } else {
318 DispVal = Op3.getImm();
319 }
320
321 const MachineOperand &Base = MI.getOperand(Op);
322 const MachineOperand &Scale = MI.getOperand(Op+1);
323 const MachineOperand &IndexReg = MI.getOperand(Op+2);
324
325 unsigned BaseReg = Base.getReg();
326
327 // Is a SIB byte needed?
328 if (IndexReg.getReg() == 0 &&
329 (BaseReg == 0 || getX86RegNum(BaseReg) != N86::ESP)) {
330 if (BaseReg == 0) { // Just a displacement?
331 // Emit special case [disp32] encoding
332 MCE.emitByte(ModRMByte(0, RegOpcodeField, 5));
333
334 emitDisplacementField(DispForReloc, DispVal, PCAdj);
335 } else {
336 unsigned BaseRegNo = getX86RegNum(BaseReg);
337 if (!DispForReloc && DispVal == 0 && BaseRegNo != N86::EBP) {
338 // Emit simple indirect register encoding... [EAX] f.e.
339 MCE.emitByte(ModRMByte(0, RegOpcodeField, BaseRegNo));
340 } else if (!DispForReloc && isDisp8(DispVal)) {
341 // Emit the disp8 encoding... [REG+disp8]
342 MCE.emitByte(ModRMByte(1, RegOpcodeField, BaseRegNo));
343 emitConstant(DispVal, 1);
344 } else {
345 // Emit the most general non-SIB encoding: [REG+disp32]
346 MCE.emitByte(ModRMByte(2, RegOpcodeField, BaseRegNo));
347 emitDisplacementField(DispForReloc, DispVal, PCAdj);
348 }
349 }
350
351 } else { // We need a SIB byte, so start by outputting the ModR/M byte first
352 assert(IndexReg.getReg() != X86::ESP &&
353 IndexReg.getReg() != X86::RSP && "Cannot use ESP as index reg!");
354
355 bool ForceDisp32 = false;
356 bool ForceDisp8 = false;
357 if (BaseReg == 0) {
358 // If there is no base register, we emit the special case SIB byte with
359 // MOD=0, BASE=5, to JUST get the index, scale, and displacement.
360 MCE.emitByte(ModRMByte(0, RegOpcodeField, 4));
361 ForceDisp32 = true;
362 } else if (DispForReloc) {
363 // Emit the normal disp32 encoding.
364 MCE.emitByte(ModRMByte(2, RegOpcodeField, 4));
365 ForceDisp32 = true;
366 } else if (DispVal == 0 && getX86RegNum(BaseReg) != N86::EBP) {
367 // Emit no displacement ModR/M byte
368 MCE.emitByte(ModRMByte(0, RegOpcodeField, 4));
369 } else if (isDisp8(DispVal)) {
370 // Emit the disp8 encoding...
371 MCE.emitByte(ModRMByte(1, RegOpcodeField, 4));
372 ForceDisp8 = true; // Make sure to force 8 bit disp if Base=EBP
373 } else {
374 // Emit the normal disp32 encoding...
375 MCE.emitByte(ModRMByte(2, RegOpcodeField, 4));
376 }
377
378 // Calculate what the SS field value should be...
379 static const unsigned SSTable[] = { ~0, 0, 1, ~0, 2, ~0, ~0, ~0, 3 };
380 unsigned SS = SSTable[Scale.getImm()];
381
382 if (BaseReg == 0) {
383 // Handle the SIB byte for the case where there is no base. The
384 // displacement has already been output.
385 assert(IndexReg.getReg() && "Index register must be specified!");
386 emitSIBByte(SS, getX86RegNum(IndexReg.getReg()), 5);
387 } else {
388 unsigned BaseRegNo = getX86RegNum(BaseReg);
389 unsigned IndexRegNo;
390 if (IndexReg.getReg())
391 IndexRegNo = getX86RegNum(IndexReg.getReg());
392 else
393 IndexRegNo = 4; // For example [ESP+1*<noreg>+4]
394 emitSIBByte(SS, IndexRegNo, BaseRegNo);
395 }
396
397 // Do we need to output a displacement?
398 if (ForceDisp8) {
399 emitConstant(DispVal, 1);
400 } else if (DispVal != 0 || ForceDisp32) {
401 emitDisplacementField(DispForReloc, DispVal, PCAdj);
402 }
403 }
404}
405
Evan Cheng0729ccf2008-01-05 00:41:47 +0000406void Emitter::emitInstruction(const MachineInstr &MI,
Chris Lattner5b930372008-01-07 07:27:27 +0000407 const TargetInstrDesc *Desc) {
Evan Cheng872bd4b2008-03-14 07:13:42 +0000408 DOUT << MI;
409
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000410 unsigned Opcode = Desc->Opcode;
411
Andrew Lenharth7a5a4b22008-03-01 13:37:02 +0000412 // Emit the lock opcode prefix as needed.
413 if (Desc->TSFlags & X86II::LOCK) MCE.emitByte(0xF0);
414
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000415 // Emit the repeat opcode prefix as needed.
416 if ((Desc->TSFlags & X86II::Op0Mask) == X86II::REP) MCE.emitByte(0xF3);
417
418 // Emit the operand size opcode prefix as needed.
419 if (Desc->TSFlags & X86II::OpSize) MCE.emitByte(0x66);
420
421 // Emit the address size opcode prefix as needed.
422 if (Desc->TSFlags & X86II::AdSize) MCE.emitByte(0x67);
423
424 bool Need0FPrefix = false;
425 switch (Desc->TSFlags & X86II::Op0Mask) {
Evan Cheng0c835a82008-04-03 08:53:17 +0000426 case X86II::TB: // Two-byte opcode prefix
427 case X86II::T8: // 0F 38
428 case X86II::TA: // 0F 3A
429 Need0FPrefix = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000430 break;
431 case X86II::REP: break; // already handled.
432 case X86II::XS: // F3 0F
433 MCE.emitByte(0xF3);
434 Need0FPrefix = true;
435 break;
436 case X86II::XD: // F2 0F
437 MCE.emitByte(0xF2);
438 Need0FPrefix = true;
439 break;
440 case X86II::D8: case X86II::D9: case X86II::DA: case X86II::DB:
441 case X86II::DC: case X86II::DD: case X86II::DE: case X86II::DF:
442 MCE.emitByte(0xD8+
443 (((Desc->TSFlags & X86II::Op0Mask)-X86II::D8)
444 >> X86II::Op0Shift));
445 break; // Two-byte opcode prefix
446 default: assert(0 && "Invalid prefix!");
447 case 0: break; // No prefix!
448 }
449
450 if (Is64BitMode) {
451 // REX prefix
Nicolas Geoffraycb162a02008-04-16 20:10:13 +0000452 unsigned REX = X86InstrInfo::determineREX(MI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000453 if (REX)
454 MCE.emitByte(0x40 | REX);
455 }
456
457 // 0x0F escape code must be emitted just before the opcode.
458 if (Need0FPrefix)
459 MCE.emitByte(0x0F);
460
Evan Cheng0c835a82008-04-03 08:53:17 +0000461 switch (Desc->TSFlags & X86II::Op0Mask) {
462 case X86II::T8: // 0F 38
463 MCE.emitByte(0x38);
464 break;
465 case X86II::TA: // 0F 3A
466 MCE.emitByte(0x3A);
467 break;
468 }
469
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000470 // If this is a two-address instruction, skip one of the register operands.
Chris Lattner0c2a4f32008-01-07 03:13:06 +0000471 unsigned NumOps = Desc->getNumOperands();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000472 unsigned CurOp = 0;
473 if (NumOps > 1 && Desc->getOperandConstraint(1, TOI::TIED_TO) != -1)
Evan Chengd49dbb82008-04-18 20:55:36 +0000474 ++CurOp;
475 else if (NumOps > 2 && Desc->getOperandConstraint(NumOps-1, TOI::TIED_TO)== 0)
476 // Skip the last source operand that is tied_to the dest reg. e.g. LXADD32
477 --NumOps;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000478
479 unsigned char BaseOpcode = II->getBaseOpcodeFor(Desc);
480 switch (Desc->TSFlags & X86II::FormMask) {
481 default: assert(0 && "Unknown FormMask value in X86 MachineCodeEmitter!");
482 case X86II::Pseudo:
Evan Cheng0729ccf2008-01-05 00:41:47 +0000483 // Remember the current PC offset, this is the PIC relocation
484 // base address.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000485 switch (Opcode) {
486 default:
487 assert(0 && "psuedo instructions should be removed before code emission");
Evan Cheng7c6c35e2008-03-05 02:34:36 +0000488 break;
Anton Korobeynikove3a9f872008-08-21 17:33:01 +0000489 case TargetInstrInfo::INLINEASM: {
490 const char* Value = MI.getOperand(0).getSymbolName();
491 /* We allow inline assembler nodes with empty bodies - they can
492 implicitly define registers, which is ok for JIT. */
493 assert((Value[0] == 0) && "JIT does not support inline asm!\n");
Evan Cheng7c6c35e2008-03-05 02:34:36 +0000494 break;
Anton Korobeynikove3a9f872008-08-21 17:33:01 +0000495 }
Dan Gohmanfa607c92008-07-01 00:05:16 +0000496 case TargetInstrInfo::DBG_LABEL:
497 case TargetInstrInfo::EH_LABEL:
Nicolas Geoffray0e757e12008-02-13 18:39:37 +0000498 MCE.emitLabel(MI.getOperand(0).getImm());
499 break;
Evan Chengb74b4b62008-03-17 06:56:52 +0000500 case TargetInstrInfo::IMPLICIT_DEF:
Evan Cheng7c6c35e2008-03-05 02:34:36 +0000501 case TargetInstrInfo::DECLARE:
502 case X86::DWARF_LOC:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000503 case X86::FP_REG_KILL:
504 break;
Evan Chengaf743252008-01-05 02:26:58 +0000505 case X86::MOVPC32r: {
Evan Cheng0729ccf2008-01-05 00:41:47 +0000506 // This emits the "call" portion of this pseudo instruction.
507 MCE.emitByte(BaseOpcode);
Nicolas Geoffraycb162a02008-04-16 20:10:13 +0000508 emitConstant(0, X86InstrInfo::sizeOfImm(Desc));
Evan Chengaf743252008-01-05 02:26:58 +0000509 // Remember PIC base.
510 PICBaseOffset = MCE.getCurrentPCOffset();
Dan Gohmanb41dfba2008-05-14 01:58:56 +0000511 X86JITInfo *JTI = TM.getJITInfo();
Evan Chengaf743252008-01-05 02:26:58 +0000512 JTI->setPICBase(MCE.getCurrentPCValue());
Evan Cheng0729ccf2008-01-05 00:41:47 +0000513 break;
514 }
Evan Chengaf743252008-01-05 02:26:58 +0000515 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000516 CurOp = NumOps;
517 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000518 case X86II::RawFrm:
519 MCE.emitByte(BaseOpcode);
Evan Cheng0729ccf2008-01-05 00:41:47 +0000520
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000521 if (CurOp != NumOps) {
522 const MachineOperand &MO = MI.getOperand(CurOp++);
Bill Wendling0768ef62008-08-21 08:38:54 +0000523
524 DOUT << "RawFrm CurOp " << CurOp << "\n";
525 DOUT << "isMachineBasicBlock " << MO.isMachineBasicBlock() << "\n";
526 DOUT << "isGlobalAddress " << MO.isGlobalAddress() << "\n";
527 DOUT << "isExternalSymbol " << MO.isExternalSymbol() << "\n";
528 DOUT << "isImmediate " << MO.isImmediate() << "\n";
529
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000530 if (MO.isMachineBasicBlock()) {
Chris Lattner6017d482007-12-30 23:10:15 +0000531 emitPCRelativeBlockAddress(MO.getMBB());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000532 } else if (MO.isGlobalAddress()) {
Dale Johannesenc501c082008-08-11 23:46:25 +0000533 // Assume undefined functions may be outside the Small codespace.
Dale Johannesen58c6d512008-08-12 21:02:08 +0000534 bool NeedStub =
535 (Is64BitMode &&
536 (TM.getCodeModel() == CodeModel::Large ||
537 TM.getSubtarget<X86Subtarget>().isTargetDarwin())) ||
538 Opcode == X86::TAILJMPd;
Evan Cheng8ee6bab2007-12-22 09:40:20 +0000539 emitGlobalAddress(MO.getGlobal(), X86::reloc_pcrel_word,
Evan Chengf0123872008-01-03 02:56:28 +0000540 0, 0, NeedStub);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000541 } else if (MO.isExternalSymbol()) {
Evan Chengf0123872008-01-03 02:56:28 +0000542 emitExternalSymbolAddress(MO.getSymbolName(), X86::reloc_pcrel_word);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000543 } else if (MO.isImmediate()) {
Nicolas Geoffraycb162a02008-04-16 20:10:13 +0000544 emitConstant(MO.getImm(), X86InstrInfo::sizeOfImm(Desc));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000545 } else {
546 assert(0 && "Unknown RawFrm operand!");
547 }
548 }
549 break;
550
551 case X86II::AddRegFrm:
552 MCE.emitByte(BaseOpcode + getX86RegNum(MI.getOperand(CurOp++).getReg()));
553
554 if (CurOp != NumOps) {
555 const MachineOperand &MO1 = MI.getOperand(CurOp++);
Nicolas Geoffraycb162a02008-04-16 20:10:13 +0000556 unsigned Size = X86InstrInfo::sizeOfImm(Desc);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000557 if (MO1.isImmediate())
558 emitConstant(MO1.getImm(), Size);
559 else {
Evan Cheng8ee6bab2007-12-22 09:40:20 +0000560 unsigned rt = Is64BitMode ? X86::reloc_pcrel_word
561 : (IsPIC ? X86::reloc_picrel_word : X86::reloc_absolute_word);
Dale Johannesen58c6d512008-08-12 21:02:08 +0000562 // This should not occur on Darwin for relocatable objects.
563 if (Opcode == X86::MOV64ri)
564 rt = X86::reloc_absolute_dword; // FIXME: add X86II flag?
Evan Chengf0123872008-01-03 02:56:28 +0000565 if (MO1.isGlobalAddress()) {
Evan Cheng28e7e162008-01-04 10:46:51 +0000566 bool NeedStub = isa<Function>(MO1.getGlobal());
567 bool isLazy = gvNeedsLazyPtr(MO1.getGlobal());
568 emitGlobalAddress(MO1.getGlobal(), rt, MO1.getOffset(), 0,
569 NeedStub, isLazy);
Evan Chengf0123872008-01-03 02:56:28 +0000570 } else if (MO1.isExternalSymbol())
571 emitExternalSymbolAddress(MO1.getSymbolName(), rt);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000572 else if (MO1.isConstantPoolIndex())
Evan Chengf0123872008-01-03 02:56:28 +0000573 emitConstPoolAddress(MO1.getIndex(), rt);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000574 else if (MO1.isJumpTableIndex())
Evan Chengf0123872008-01-03 02:56:28 +0000575 emitJumpTableAddress(MO1.getIndex(), rt);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000576 }
577 }
578 break;
579
580 case X86II::MRMDestReg: {
581 MCE.emitByte(BaseOpcode);
582 emitRegModRMByte(MI.getOperand(CurOp).getReg(),
583 getX86RegNum(MI.getOperand(CurOp+1).getReg()));
584 CurOp += 2;
585 if (CurOp != NumOps)
Nicolas Geoffraycb162a02008-04-16 20:10:13 +0000586 emitConstant(MI.getOperand(CurOp++).getImm(), X86InstrInfo::sizeOfImm(Desc));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000587 break;
588 }
589 case X86II::MRMDestMem: {
590 MCE.emitByte(BaseOpcode);
591 emitMemModRMByte(MI, CurOp, getX86RegNum(MI.getOperand(CurOp+4).getReg()));
592 CurOp += 5;
593 if (CurOp != NumOps)
Nicolas Geoffraycb162a02008-04-16 20:10:13 +0000594 emitConstant(MI.getOperand(CurOp++).getImm(), X86InstrInfo::sizeOfImm(Desc));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000595 break;
596 }
597
598 case X86II::MRMSrcReg:
599 MCE.emitByte(BaseOpcode);
600 emitRegModRMByte(MI.getOperand(CurOp+1).getReg(),
601 getX86RegNum(MI.getOperand(CurOp).getReg()));
602 CurOp += 2;
603 if (CurOp != NumOps)
Nicolas Geoffraycb162a02008-04-16 20:10:13 +0000604 emitConstant(MI.getOperand(CurOp++).getImm(), X86InstrInfo::sizeOfImm(Desc));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000605 break;
606
607 case X86II::MRMSrcMem: {
Nicolas Geoffraycb162a02008-04-16 20:10:13 +0000608 intptr_t PCAdj = (CurOp+5 != NumOps) ? X86InstrInfo::sizeOfImm(Desc) : 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000609
610 MCE.emitByte(BaseOpcode);
611 emitMemModRMByte(MI, CurOp+1, getX86RegNum(MI.getOperand(CurOp).getReg()),
612 PCAdj);
613 CurOp += 5;
614 if (CurOp != NumOps)
Nicolas Geoffraycb162a02008-04-16 20:10:13 +0000615 emitConstant(MI.getOperand(CurOp++).getImm(), X86InstrInfo::sizeOfImm(Desc));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000616 break;
617 }
618
619 case X86II::MRM0r: case X86II::MRM1r:
620 case X86II::MRM2r: case X86II::MRM3r:
621 case X86II::MRM4r: case X86II::MRM5r:
622 case X86II::MRM6r: case X86II::MRM7r:
623 MCE.emitByte(BaseOpcode);
624 emitRegModRMByte(MI.getOperand(CurOp++).getReg(),
625 (Desc->TSFlags & X86II::FormMask)-X86II::MRM0r);
626
627 if (CurOp != NumOps) {
628 const MachineOperand &MO1 = MI.getOperand(CurOp++);
Nicolas Geoffraycb162a02008-04-16 20:10:13 +0000629 unsigned Size = X86InstrInfo::sizeOfImm(Desc);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000630 if (MO1.isImmediate())
631 emitConstant(MO1.getImm(), Size);
632 else {
633 unsigned rt = Is64BitMode ? X86::reloc_pcrel_word
Evan Cheng8ee6bab2007-12-22 09:40:20 +0000634 : (IsPIC ? X86::reloc_picrel_word : X86::reloc_absolute_word);
Dale Johannesen58c6d512008-08-12 21:02:08 +0000635 if (Opcode == X86::MOV64ri32)
636 rt = X86::reloc_absolute_word; // FIXME: add X86II flag?
Evan Chengf0123872008-01-03 02:56:28 +0000637 if (MO1.isGlobalAddress()) {
Evan Cheng28e7e162008-01-04 10:46:51 +0000638 bool NeedStub = isa<Function>(MO1.getGlobal());
639 bool isLazy = gvNeedsLazyPtr(MO1.getGlobal());
640 emitGlobalAddress(MO1.getGlobal(), rt, MO1.getOffset(), 0,
641 NeedStub, isLazy);
Evan Chengf0123872008-01-03 02:56:28 +0000642 } else if (MO1.isExternalSymbol())
643 emitExternalSymbolAddress(MO1.getSymbolName(), rt);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000644 else if (MO1.isConstantPoolIndex())
Evan Chengf0123872008-01-03 02:56:28 +0000645 emitConstPoolAddress(MO1.getIndex(), rt);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000646 else if (MO1.isJumpTableIndex())
Evan Chengf0123872008-01-03 02:56:28 +0000647 emitJumpTableAddress(MO1.getIndex(), rt);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000648 }
649 }
650 break;
651
652 case X86II::MRM0m: case X86II::MRM1m:
653 case X86II::MRM2m: case X86II::MRM3m:
654 case X86II::MRM4m: case X86II::MRM5m:
655 case X86II::MRM6m: case X86II::MRM7m: {
Evan Cheng8ee6bab2007-12-22 09:40:20 +0000656 intptr_t PCAdj = (CurOp+4 != NumOps) ?
Nicolas Geoffraycb162a02008-04-16 20:10:13 +0000657 (MI.getOperand(CurOp+4).isImmediate() ? X86InstrInfo::sizeOfImm(Desc) : 4) : 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000658
659 MCE.emitByte(BaseOpcode);
660 emitMemModRMByte(MI, CurOp, (Desc->TSFlags & X86II::FormMask)-X86II::MRM0m,
661 PCAdj);
662 CurOp += 4;
663
664 if (CurOp != NumOps) {
665 const MachineOperand &MO = MI.getOperand(CurOp++);
Nicolas Geoffraycb162a02008-04-16 20:10:13 +0000666 unsigned Size = X86InstrInfo::sizeOfImm(Desc);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000667 if (MO.isImmediate())
668 emitConstant(MO.getImm(), Size);
669 else {
670 unsigned rt = Is64BitMode ? X86::reloc_pcrel_word
Evan Cheng8ee6bab2007-12-22 09:40:20 +0000671 : (IsPIC ? X86::reloc_picrel_word : X86::reloc_absolute_word);
Dale Johannesen58c6d512008-08-12 21:02:08 +0000672 if (Opcode == X86::MOV64mi32)
673 rt = X86::reloc_absolute_word; // FIXME: add X86II flag?
Evan Chengf0123872008-01-03 02:56:28 +0000674 if (MO.isGlobalAddress()) {
Evan Cheng28e7e162008-01-04 10:46:51 +0000675 bool NeedStub = isa<Function>(MO.getGlobal());
676 bool isLazy = gvNeedsLazyPtr(MO.getGlobal());
677 emitGlobalAddress(MO.getGlobal(), rt, MO.getOffset(), 0,
678 NeedStub, isLazy);
Evan Chengf0123872008-01-03 02:56:28 +0000679 } else if (MO.isExternalSymbol())
680 emitExternalSymbolAddress(MO.getSymbolName(), rt);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000681 else if (MO.isConstantPoolIndex())
Evan Chengf0123872008-01-03 02:56:28 +0000682 emitConstPoolAddress(MO.getIndex(), rt);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000683 else if (MO.isJumpTableIndex())
Evan Chengf0123872008-01-03 02:56:28 +0000684 emitJumpTableAddress(MO.getIndex(), rt);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000685 }
686 }
687 break;
688 }
689
690 case X86II::MRMInitReg:
691 MCE.emitByte(BaseOpcode);
692 // Duplicate register, used by things like MOV8r0 (aka xor reg,reg).
693 emitRegModRMByte(MI.getOperand(CurOp).getReg(),
694 getX86RegNum(MI.getOperand(CurOp).getReg()));
695 ++CurOp;
696 break;
697 }
698
Evan Cheng6032b652008-03-05 02:08:03 +0000699 if (!Desc->isVariadic() && CurOp != NumOps) {
700 cerr << "Cannot encode: ";
701 MI.dump();
702 cerr << '\n';
703 abort();
704 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000705}