blob: 1b2d5ffc3e6621c1edcd053b57bb55d6800c0af0 [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 Gohmanf17a25c2007-07-18 16:29:46 +000049 : MachineFunctionPass((intptr_t)&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)
54 : MachineFunctionPass((intptr_t)&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) {
109 assert((MF.getTarget().getRelocationModel() != Reloc::Default ||
110 MF.getTarget().getRelocationModel() != Reloc::Static) &&
111 "JIT relocation model must be set to static or default!");
Nicolas Geoffray0e757e12008-02-13 18:39:37 +0000112
113 MCE.setModuleInfo(&getAnalysis<MachineModuleInfo>());
114
Dan Gohmanb41dfba2008-05-14 01:58:56 +0000115 II = TM.getInstrInfo();
116 TD = TM.getTargetData();
Evan Cheng28e7e162008-01-04 10:46:51 +0000117 Is64BitMode = TM.getSubtarget<X86Subtarget>().is64Bit();
Evan Chengae50ca32008-05-20 01:56:59 +0000118 IsPIC = TM.getRelocationModel() == Reloc::PIC_;
Nicolas Geoffray0e757e12008-02-13 18:39:37 +0000119
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000120 do {
Evan Cheng872bd4b2008-03-14 07:13:42 +0000121 DOUT << "JITTing function '" << MF.getFunction()->getName() << "'\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000122 MCE.startFunction(MF);
123 for (MachineFunction::iterator MBB = MF.begin(), E = MF.end();
124 MBB != E; ++MBB) {
125 MCE.StartMachineBasicBlock(MBB);
126 for (MachineBasicBlock::const_iterator I = MBB->begin(), E = MBB->end();
Evan Cheng0729ccf2008-01-05 00:41:47 +0000127 I != E; ++I) {
Chris Lattner5b930372008-01-07 07:27:27 +0000128 const TargetInstrDesc &Desc = I->getDesc();
129 emitInstruction(*I, &Desc);
Evan Cheng0729ccf2008-01-05 00:41:47 +0000130 // MOVPC32r is basically a call plus a pop instruction.
Chris Lattner5b930372008-01-07 07:27:27 +0000131 if (Desc.getOpcode() == X86::MOVPC32r)
Evan Cheng0729ccf2008-01-05 00:41:47 +0000132 emitInstruction(*I, &II->get(X86::POP32r));
133 NumEmitted++; // Keep track of the # of mi's emitted
134 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000135 }
136 } while (MCE.finishFunction(MF));
137
138 return false;
139}
140
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000141/// emitPCRelativeBlockAddress - This method keeps track of the information
142/// necessary to resolve the address of this block later and emits a dummy
143/// value.
144///
145void Emitter::emitPCRelativeBlockAddress(MachineBasicBlock *MBB) {
146 // Remember where this reference was and where it is to so we can
147 // deal with it later.
148 MCE.addRelocation(MachineRelocation::getBB(MCE.getCurrentPCOffset(),
149 X86::reloc_pcrel_word, MBB));
150 MCE.emitWordLE(0);
151}
152
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000153/// emitGlobalAddress - Emit the specified address to the code stream assuming
154/// this is part of a "take the address of a global" instruction.
155///
Evan Cheng8ee6bab2007-12-22 09:40:20 +0000156void Emitter::emitGlobalAddress(GlobalValue *GV, unsigned Reloc,
157 int Disp /* = 0 */, intptr_t PCAdj /* = 0 */,
Evan Cheng28e7e162008-01-04 10:46:51 +0000158 bool NeedStub /* = false */,
159 bool isLazy /* = false */) {
160 intptr_t RelocCST = 0;
Evan Chengf0123872008-01-03 02:56:28 +0000161 if (Reloc == X86::reloc_picrel_word)
Evan Chengaf743252008-01-05 02:26:58 +0000162 RelocCST = PICBaseOffset;
Evan Cheng28e7e162008-01-04 10:46:51 +0000163 else if (Reloc == X86::reloc_pcrel_word)
164 RelocCST = PCAdj;
165 MachineRelocation MR = isLazy
166 ? MachineRelocation::getGVLazyPtr(MCE.getCurrentPCOffset(), Reloc,
167 GV, RelocCST, NeedStub)
168 : MachineRelocation::getGV(MCE.getCurrentPCOffset(), Reloc,
169 GV, RelocCST, NeedStub);
170 MCE.addRelocation(MR);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000171 if (Reloc == X86::reloc_absolute_dword)
172 MCE.emitWordLE(0);
173 MCE.emitWordLE(Disp); // The relocated value will be added to the displacement
174}
175
176/// emitExternalSymbolAddress - Arrange for the address of an external symbol to
177/// be emitted to the current location in the function, and allow it to be PC
178/// relative.
Evan Chengf0123872008-01-03 02:56:28 +0000179void Emitter::emitExternalSymbolAddress(const char *ES, unsigned Reloc) {
Evan Chengaf743252008-01-05 02:26:58 +0000180 intptr_t RelocCST = (Reloc == X86::reloc_picrel_word) ? PICBaseOffset : 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000181 MCE.addRelocation(MachineRelocation::getExtSym(MCE.getCurrentPCOffset(),
Evan Cheng28e7e162008-01-04 10:46:51 +0000182 Reloc, ES, RelocCST));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000183 if (Reloc == X86::reloc_absolute_dword)
184 MCE.emitWordLE(0);
185 MCE.emitWordLE(0);
186}
187
188/// emitConstPoolAddress - Arrange for the address of an constant pool
189/// to be emitted to the current location in the function, and allow it to be PC
190/// relative.
191void Emitter::emitConstPoolAddress(unsigned CPI, unsigned Reloc,
192 int Disp /* = 0 */,
Evan Chengf0123872008-01-03 02:56:28 +0000193 intptr_t PCAdj /* = 0 */) {
Evan Cheng28e7e162008-01-04 10:46:51 +0000194 intptr_t RelocCST = 0;
Evan Chengf0123872008-01-03 02:56:28 +0000195 if (Reloc == X86::reloc_picrel_word)
Evan Chengaf743252008-01-05 02:26:58 +0000196 RelocCST = PICBaseOffset;
Evan Cheng28e7e162008-01-04 10:46:51 +0000197 else if (Reloc == X86::reloc_pcrel_word)
198 RelocCST = PCAdj;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000199 MCE.addRelocation(MachineRelocation::getConstPool(MCE.getCurrentPCOffset(),
Evan Cheng28e7e162008-01-04 10:46:51 +0000200 Reloc, CPI, RelocCST));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000201 if (Reloc == X86::reloc_absolute_dword)
202 MCE.emitWordLE(0);
203 MCE.emitWordLE(Disp); // The relocated value will be added to the displacement
204}
205
206/// emitJumpTableAddress - Arrange for the address of a jump table to
207/// be emitted to the current location in the function, and allow it to be PC
208/// relative.
209void Emitter::emitJumpTableAddress(unsigned JTI, unsigned Reloc,
Evan Chengf0123872008-01-03 02:56:28 +0000210 intptr_t PCAdj /* = 0 */) {
Evan Cheng28e7e162008-01-04 10:46:51 +0000211 intptr_t RelocCST = 0;
Evan Chengf0123872008-01-03 02:56:28 +0000212 if (Reloc == X86::reloc_picrel_word)
Evan Chengaf743252008-01-05 02:26:58 +0000213 RelocCST = PICBaseOffset;
Evan Cheng28e7e162008-01-04 10:46:51 +0000214 else if (Reloc == X86::reloc_pcrel_word)
215 RelocCST = PCAdj;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000216 MCE.addRelocation(MachineRelocation::getJumpTable(MCE.getCurrentPCOffset(),
Evan Cheng28e7e162008-01-04 10:46:51 +0000217 Reloc, JTI, RelocCST));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000218 if (Reloc == X86::reloc_absolute_dword)
219 MCE.emitWordLE(0);
220 MCE.emitWordLE(0); // The relocated value will be added to the displacement
221}
222
Dan Gohman06844672008-02-08 03:29:40 +0000223unsigned Emitter::getX86RegNum(unsigned RegNo) const {
Dan Gohmanb41dfba2008-05-14 01:58:56 +0000224 return II->getRegisterInfo().getX86RegNum(RegNo);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000225}
226
227inline static unsigned char ModRMByte(unsigned Mod, unsigned RegOpcode,
228 unsigned RM) {
229 assert(Mod < 4 && RegOpcode < 8 && RM < 8 && "ModRM Fields out of range!");
230 return RM | (RegOpcode << 3) | (Mod << 6);
231}
232
233void Emitter::emitRegModRMByte(unsigned ModRMReg, unsigned RegOpcodeFld){
234 MCE.emitByte(ModRMByte(3, RegOpcodeFld, getX86RegNum(ModRMReg)));
235}
236
237void Emitter::emitSIBByte(unsigned SS, unsigned Index, unsigned Base) {
238 // SIB byte is in the same format as the ModRMByte...
239 MCE.emitByte(ModRMByte(SS, Index, Base));
240}
241
242void Emitter::emitConstant(uint64_t Val, unsigned Size) {
243 // Output the constant in little endian byte order...
244 for (unsigned i = 0; i != Size; ++i) {
245 MCE.emitByte(Val & 255);
246 Val >>= 8;
247 }
248}
249
250/// isDisp8 - Return true if this signed displacement fits in a 8-bit
251/// sign-extended field.
252static bool isDisp8(int Value) {
253 return Value == (signed char)Value;
254}
255
Evan Cheng28e7e162008-01-04 10:46:51 +0000256bool Emitter::gvNeedsLazyPtr(const GlobalValue *GV) {
257 return !Is64BitMode &&
258 TM.getSubtarget<X86Subtarget>().GVRequiresExtraLoad(GV, TM, false);
259}
260
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000261void Emitter::emitDisplacementField(const MachineOperand *RelocOp,
Evan Cheng8ee6bab2007-12-22 09:40:20 +0000262 int DispVal, intptr_t PCAdj) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000263 // If this is a simple integer displacement that doesn't require a relocation,
264 // emit it now.
265 if (!RelocOp) {
266 emitConstant(DispVal, 4);
267 return;
268 }
269
270 // Otherwise, this is something that requires a relocation. Emit it as such
271 // now.
272 if (RelocOp->isGlobalAddress()) {
273 // In 64-bit static small code model, we could potentially emit absolute.
274 // But it's probably not beneficial.
Bill Wendlingf3a655f2008-02-26 10:57:23 +0000275 // 89 05 00 00 00 00 mov %eax,0(%rip) # PC-relative
276 // 89 04 25 00 00 00 00 mov %eax,0x0 # Absolute
Evan Chengf0123872008-01-03 02:56:28 +0000277 unsigned rt = Is64BitMode ? X86::reloc_pcrel_word
Evan Cheng8ee6bab2007-12-22 09:40:20 +0000278 : (IsPIC ? X86::reloc_picrel_word : X86::reloc_absolute_word);
Evan Cheng28e7e162008-01-04 10:46:51 +0000279 bool NeedStub = isa<Function>(RelocOp->getGlobal());
280 bool isLazy = gvNeedsLazyPtr(RelocOp->getGlobal());
Evan Cheng8ee6bab2007-12-22 09:40:20 +0000281 emitGlobalAddress(RelocOp->getGlobal(), rt, RelocOp->getOffset(),
Evan Cheng28e7e162008-01-04 10:46:51 +0000282 PCAdj, NeedStub, isLazy);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000283 } else if (RelocOp->isConstantPoolIndex()) {
Evan Cheng8c872652008-01-02 23:38:59 +0000284 unsigned rt = Is64BitMode ? X86::reloc_pcrel_word : X86::reloc_picrel_word;
285 emitConstPoolAddress(RelocOp->getIndex(), rt,
Evan Chengf0123872008-01-03 02:56:28 +0000286 RelocOp->getOffset(), PCAdj);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000287 } else if (RelocOp->isJumpTableIndex()) {
Evan Cheng8c872652008-01-02 23:38:59 +0000288 unsigned rt = Is64BitMode ? X86::reloc_pcrel_word : X86::reloc_picrel_word;
Evan Chengf0123872008-01-03 02:56:28 +0000289 emitJumpTableAddress(RelocOp->getIndex(), rt, PCAdj);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000290 } else {
291 assert(0 && "Unknown value to relocate!");
292 }
293}
294
295void Emitter::emitMemModRMByte(const MachineInstr &MI,
296 unsigned Op, unsigned RegOpcodeField,
Evan Cheng8ee6bab2007-12-22 09:40:20 +0000297 intptr_t PCAdj) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000298 const MachineOperand &Op3 = MI.getOperand(Op+3);
299 int DispVal = 0;
300 const MachineOperand *DispForReloc = 0;
301
302 // Figure out what sort of displacement we have to handle here.
303 if (Op3.isGlobalAddress()) {
304 DispForReloc = &Op3;
305 } else if (Op3.isConstantPoolIndex()) {
Evan Cheng8c872652008-01-02 23:38:59 +0000306 if (Is64BitMode || IsPIC) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000307 DispForReloc = &Op3;
308 } else {
Chris Lattner6017d482007-12-30 23:10:15 +0000309 DispVal += MCE.getConstantPoolEntryAddress(Op3.getIndex());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000310 DispVal += Op3.getOffset();
311 }
312 } else if (Op3.isJumpTableIndex()) {
Evan Cheng8c872652008-01-02 23:38:59 +0000313 if (Is64BitMode || IsPIC) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000314 DispForReloc = &Op3;
315 } else {
Chris Lattner6017d482007-12-30 23:10:15 +0000316 DispVal += MCE.getJumpTableEntryAddress(Op3.getIndex());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000317 }
318 } else {
319 DispVal = Op3.getImm();
320 }
321
322 const MachineOperand &Base = MI.getOperand(Op);
323 const MachineOperand &Scale = MI.getOperand(Op+1);
324 const MachineOperand &IndexReg = MI.getOperand(Op+2);
325
326 unsigned BaseReg = Base.getReg();
327
328 // Is a SIB byte needed?
329 if (IndexReg.getReg() == 0 &&
330 (BaseReg == 0 || getX86RegNum(BaseReg) != N86::ESP)) {
331 if (BaseReg == 0) { // Just a displacement?
332 // Emit special case [disp32] encoding
333 MCE.emitByte(ModRMByte(0, RegOpcodeField, 5));
334
335 emitDisplacementField(DispForReloc, DispVal, PCAdj);
336 } else {
337 unsigned BaseRegNo = getX86RegNum(BaseReg);
338 if (!DispForReloc && DispVal == 0 && BaseRegNo != N86::EBP) {
339 // Emit simple indirect register encoding... [EAX] f.e.
340 MCE.emitByte(ModRMByte(0, RegOpcodeField, BaseRegNo));
341 } else if (!DispForReloc && isDisp8(DispVal)) {
342 // Emit the disp8 encoding... [REG+disp8]
343 MCE.emitByte(ModRMByte(1, RegOpcodeField, BaseRegNo));
344 emitConstant(DispVal, 1);
345 } else {
346 // Emit the most general non-SIB encoding: [REG+disp32]
347 MCE.emitByte(ModRMByte(2, RegOpcodeField, BaseRegNo));
348 emitDisplacementField(DispForReloc, DispVal, PCAdj);
349 }
350 }
351
352 } else { // We need a SIB byte, so start by outputting the ModR/M byte first
353 assert(IndexReg.getReg() != X86::ESP &&
354 IndexReg.getReg() != X86::RSP && "Cannot use ESP as index reg!");
355
356 bool ForceDisp32 = false;
357 bool ForceDisp8 = false;
358 if (BaseReg == 0) {
359 // If there is no base register, we emit the special case SIB byte with
360 // MOD=0, BASE=5, to JUST get the index, scale, and displacement.
361 MCE.emitByte(ModRMByte(0, RegOpcodeField, 4));
362 ForceDisp32 = true;
363 } else if (DispForReloc) {
364 // Emit the normal disp32 encoding.
365 MCE.emitByte(ModRMByte(2, RegOpcodeField, 4));
366 ForceDisp32 = true;
367 } else if (DispVal == 0 && getX86RegNum(BaseReg) != N86::EBP) {
368 // Emit no displacement ModR/M byte
369 MCE.emitByte(ModRMByte(0, RegOpcodeField, 4));
370 } else if (isDisp8(DispVal)) {
371 // Emit the disp8 encoding...
372 MCE.emitByte(ModRMByte(1, RegOpcodeField, 4));
373 ForceDisp8 = true; // Make sure to force 8 bit disp if Base=EBP
374 } else {
375 // Emit the normal disp32 encoding...
376 MCE.emitByte(ModRMByte(2, RegOpcodeField, 4));
377 }
378
379 // Calculate what the SS field value should be...
380 static const unsigned SSTable[] = { ~0, 0, 1, ~0, 2, ~0, ~0, ~0, 3 };
381 unsigned SS = SSTable[Scale.getImm()];
382
383 if (BaseReg == 0) {
384 // Handle the SIB byte for the case where there is no base. The
385 // displacement has already been output.
386 assert(IndexReg.getReg() && "Index register must be specified!");
387 emitSIBByte(SS, getX86RegNum(IndexReg.getReg()), 5);
388 } else {
389 unsigned BaseRegNo = getX86RegNum(BaseReg);
390 unsigned IndexRegNo;
391 if (IndexReg.getReg())
392 IndexRegNo = getX86RegNum(IndexReg.getReg());
393 else
394 IndexRegNo = 4; // For example [ESP+1*<noreg>+4]
395 emitSIBByte(SS, IndexRegNo, BaseRegNo);
396 }
397
398 // Do we need to output a displacement?
399 if (ForceDisp8) {
400 emitConstant(DispVal, 1);
401 } else if (DispVal != 0 || ForceDisp32) {
402 emitDisplacementField(DispForReloc, DispVal, PCAdj);
403 }
404 }
405}
406
Evan Cheng0729ccf2008-01-05 00:41:47 +0000407void Emitter::emitInstruction(const MachineInstr &MI,
Chris Lattner5b930372008-01-07 07:27:27 +0000408 const TargetInstrDesc *Desc) {
Evan Cheng872bd4b2008-03-14 07:13:42 +0000409 DOUT << MI;
410
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000411 unsigned Opcode = Desc->Opcode;
412
Andrew Lenharth7a5a4b22008-03-01 13:37:02 +0000413 // Emit the lock opcode prefix as needed.
414 if (Desc->TSFlags & X86II::LOCK) MCE.emitByte(0xF0);
415
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000416 // Emit the repeat opcode prefix as needed.
417 if ((Desc->TSFlags & X86II::Op0Mask) == X86II::REP) MCE.emitByte(0xF3);
418
419 // Emit the operand size opcode prefix as needed.
420 if (Desc->TSFlags & X86II::OpSize) MCE.emitByte(0x66);
421
422 // Emit the address size opcode prefix as needed.
423 if (Desc->TSFlags & X86II::AdSize) MCE.emitByte(0x67);
424
425 bool Need0FPrefix = false;
426 switch (Desc->TSFlags & X86II::Op0Mask) {
Evan Cheng0c835a82008-04-03 08:53:17 +0000427 case X86II::TB: // Two-byte opcode prefix
428 case X86II::T8: // 0F 38
429 case X86II::TA: // 0F 3A
430 Need0FPrefix = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000431 break;
432 case X86II::REP: break; // already handled.
433 case X86II::XS: // F3 0F
434 MCE.emitByte(0xF3);
435 Need0FPrefix = true;
436 break;
437 case X86II::XD: // F2 0F
438 MCE.emitByte(0xF2);
439 Need0FPrefix = true;
440 break;
441 case X86II::D8: case X86II::D9: case X86II::DA: case X86II::DB:
442 case X86II::DC: case X86II::DD: case X86II::DE: case X86II::DF:
443 MCE.emitByte(0xD8+
444 (((Desc->TSFlags & X86II::Op0Mask)-X86II::D8)
445 >> X86II::Op0Shift));
446 break; // Two-byte opcode prefix
447 default: assert(0 && "Invalid prefix!");
448 case 0: break; // No prefix!
449 }
450
451 if (Is64BitMode) {
452 // REX prefix
Nicolas Geoffraycb162a02008-04-16 20:10:13 +0000453 unsigned REX = X86InstrInfo::determineREX(MI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000454 if (REX)
455 MCE.emitByte(0x40 | REX);
456 }
457
458 // 0x0F escape code must be emitted just before the opcode.
459 if (Need0FPrefix)
460 MCE.emitByte(0x0F);
461
Evan Cheng0c835a82008-04-03 08:53:17 +0000462 switch (Desc->TSFlags & X86II::Op0Mask) {
463 case X86II::T8: // 0F 38
464 MCE.emitByte(0x38);
465 break;
466 case X86II::TA: // 0F 3A
467 MCE.emitByte(0x3A);
468 break;
469 }
470
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000471 // If this is a two-address instruction, skip one of the register operands.
Chris Lattner0c2a4f32008-01-07 03:13:06 +0000472 unsigned NumOps = Desc->getNumOperands();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000473 unsigned CurOp = 0;
474 if (NumOps > 1 && Desc->getOperandConstraint(1, TOI::TIED_TO) != -1)
Evan Chengd49dbb82008-04-18 20:55:36 +0000475 ++CurOp;
476 else if (NumOps > 2 && Desc->getOperandConstraint(NumOps-1, TOI::TIED_TO)== 0)
477 // Skip the last source operand that is tied_to the dest reg. e.g. LXADD32
478 --NumOps;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000479
480 unsigned char BaseOpcode = II->getBaseOpcodeFor(Desc);
481 switch (Desc->TSFlags & X86II::FormMask) {
482 default: assert(0 && "Unknown FormMask value in X86 MachineCodeEmitter!");
483 case X86II::Pseudo:
Evan Cheng0729ccf2008-01-05 00:41:47 +0000484 // Remember the current PC offset, this is the PIC relocation
485 // base address.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000486 switch (Opcode) {
487 default:
488 assert(0 && "psuedo instructions should be removed before code emission");
Evan Cheng7c6c35e2008-03-05 02:34:36 +0000489 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000490 case TargetInstrInfo::INLINEASM:
491 assert(0 && "JIT does not support inline asm!\n");
Evan Cheng7c6c35e2008-03-05 02:34:36 +0000492 break;
Dan Gohmanfa607c92008-07-01 00:05:16 +0000493 case TargetInstrInfo::DBG_LABEL:
494 case TargetInstrInfo::EH_LABEL:
Nicolas Geoffray0e757e12008-02-13 18:39:37 +0000495 MCE.emitLabel(MI.getOperand(0).getImm());
496 break;
Evan Chengb74b4b62008-03-17 06:56:52 +0000497 case TargetInstrInfo::IMPLICIT_DEF:
Evan Cheng7c6c35e2008-03-05 02:34:36 +0000498 case TargetInstrInfo::DECLARE:
499 case X86::DWARF_LOC:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000500 case X86::FP_REG_KILL:
501 break;
Evan Chengaf743252008-01-05 02:26:58 +0000502 case X86::MOVPC32r: {
Evan Cheng0729ccf2008-01-05 00:41:47 +0000503 // This emits the "call" portion of this pseudo instruction.
504 MCE.emitByte(BaseOpcode);
Nicolas Geoffraycb162a02008-04-16 20:10:13 +0000505 emitConstant(0, X86InstrInfo::sizeOfImm(Desc));
Evan Chengaf743252008-01-05 02:26:58 +0000506 // Remember PIC base.
507 PICBaseOffset = MCE.getCurrentPCOffset();
Dan Gohmanb41dfba2008-05-14 01:58:56 +0000508 X86JITInfo *JTI = TM.getJITInfo();
Evan Chengaf743252008-01-05 02:26:58 +0000509 JTI->setPICBase(MCE.getCurrentPCValue());
Evan Cheng0729ccf2008-01-05 00:41:47 +0000510 break;
511 }
Evan Chengaf743252008-01-05 02:26:58 +0000512 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000513 CurOp = NumOps;
514 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000515 case X86II::RawFrm:
516 MCE.emitByte(BaseOpcode);
Evan Cheng0729ccf2008-01-05 00:41:47 +0000517
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000518 if (CurOp != NumOps) {
519 const MachineOperand &MO = MI.getOperand(CurOp++);
520 if (MO.isMachineBasicBlock()) {
Chris Lattner6017d482007-12-30 23:10:15 +0000521 emitPCRelativeBlockAddress(MO.getMBB());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000522 } else if (MO.isGlobalAddress()) {
Evan Chenge5e9fe82008-01-04 10:50:28 +0000523 bool NeedStub = (Is64BitMode && TM.getCodeModel() == CodeModel::Large)
524 || Opcode == X86::TAILJMPd;
Evan Cheng8ee6bab2007-12-22 09:40:20 +0000525 emitGlobalAddress(MO.getGlobal(), X86::reloc_pcrel_word,
Evan Chengf0123872008-01-03 02:56:28 +0000526 0, 0, NeedStub);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000527 } else if (MO.isExternalSymbol()) {
Evan Chengf0123872008-01-03 02:56:28 +0000528 emitExternalSymbolAddress(MO.getSymbolName(), X86::reloc_pcrel_word);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000529 } else if (MO.isImmediate()) {
Nicolas Geoffraycb162a02008-04-16 20:10:13 +0000530 emitConstant(MO.getImm(), X86InstrInfo::sizeOfImm(Desc));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000531 } else {
532 assert(0 && "Unknown RawFrm operand!");
533 }
534 }
535 break;
536
537 case X86II::AddRegFrm:
538 MCE.emitByte(BaseOpcode + getX86RegNum(MI.getOperand(CurOp++).getReg()));
539
540 if (CurOp != NumOps) {
541 const MachineOperand &MO1 = MI.getOperand(CurOp++);
Nicolas Geoffraycb162a02008-04-16 20:10:13 +0000542 unsigned Size = X86InstrInfo::sizeOfImm(Desc);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000543 if (MO1.isImmediate())
544 emitConstant(MO1.getImm(), Size);
545 else {
Evan Cheng8ee6bab2007-12-22 09:40:20 +0000546 unsigned rt = Is64BitMode ? X86::reloc_pcrel_word
547 : (IsPIC ? X86::reloc_picrel_word : X86::reloc_absolute_word);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000548 if (Opcode == X86::MOV64ri)
549 rt = X86::reloc_absolute_dword; // FIXME: add X86II flag?
Evan Chengf0123872008-01-03 02:56:28 +0000550 if (MO1.isGlobalAddress()) {
Evan Cheng28e7e162008-01-04 10:46:51 +0000551 bool NeedStub = isa<Function>(MO1.getGlobal());
552 bool isLazy = gvNeedsLazyPtr(MO1.getGlobal());
553 emitGlobalAddress(MO1.getGlobal(), rt, MO1.getOffset(), 0,
554 NeedStub, isLazy);
Evan Chengf0123872008-01-03 02:56:28 +0000555 } else if (MO1.isExternalSymbol())
556 emitExternalSymbolAddress(MO1.getSymbolName(), rt);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000557 else if (MO1.isConstantPoolIndex())
Evan Chengf0123872008-01-03 02:56:28 +0000558 emitConstPoolAddress(MO1.getIndex(), rt);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000559 else if (MO1.isJumpTableIndex())
Evan Chengf0123872008-01-03 02:56:28 +0000560 emitJumpTableAddress(MO1.getIndex(), rt);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000561 }
562 }
563 break;
564
565 case X86II::MRMDestReg: {
566 MCE.emitByte(BaseOpcode);
567 emitRegModRMByte(MI.getOperand(CurOp).getReg(),
568 getX86RegNum(MI.getOperand(CurOp+1).getReg()));
569 CurOp += 2;
570 if (CurOp != NumOps)
Nicolas Geoffraycb162a02008-04-16 20:10:13 +0000571 emitConstant(MI.getOperand(CurOp++).getImm(), X86InstrInfo::sizeOfImm(Desc));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000572 break;
573 }
574 case X86II::MRMDestMem: {
575 MCE.emitByte(BaseOpcode);
576 emitMemModRMByte(MI, CurOp, getX86RegNum(MI.getOperand(CurOp+4).getReg()));
577 CurOp += 5;
578 if (CurOp != NumOps)
Nicolas Geoffraycb162a02008-04-16 20:10:13 +0000579 emitConstant(MI.getOperand(CurOp++).getImm(), X86InstrInfo::sizeOfImm(Desc));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000580 break;
581 }
582
583 case X86II::MRMSrcReg:
584 MCE.emitByte(BaseOpcode);
585 emitRegModRMByte(MI.getOperand(CurOp+1).getReg(),
586 getX86RegNum(MI.getOperand(CurOp).getReg()));
587 CurOp += 2;
588 if (CurOp != NumOps)
Nicolas Geoffraycb162a02008-04-16 20:10:13 +0000589 emitConstant(MI.getOperand(CurOp++).getImm(), X86InstrInfo::sizeOfImm(Desc));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000590 break;
591
592 case X86II::MRMSrcMem: {
Nicolas Geoffraycb162a02008-04-16 20:10:13 +0000593 intptr_t PCAdj = (CurOp+5 != NumOps) ? X86InstrInfo::sizeOfImm(Desc) : 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000594
595 MCE.emitByte(BaseOpcode);
596 emitMemModRMByte(MI, CurOp+1, getX86RegNum(MI.getOperand(CurOp).getReg()),
597 PCAdj);
598 CurOp += 5;
599 if (CurOp != NumOps)
Nicolas Geoffraycb162a02008-04-16 20:10:13 +0000600 emitConstant(MI.getOperand(CurOp++).getImm(), X86InstrInfo::sizeOfImm(Desc));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000601 break;
602 }
603
604 case X86II::MRM0r: case X86II::MRM1r:
605 case X86II::MRM2r: case X86II::MRM3r:
606 case X86II::MRM4r: case X86II::MRM5r:
607 case X86II::MRM6r: case X86II::MRM7r:
608 MCE.emitByte(BaseOpcode);
609 emitRegModRMByte(MI.getOperand(CurOp++).getReg(),
610 (Desc->TSFlags & X86II::FormMask)-X86II::MRM0r);
611
612 if (CurOp != NumOps) {
613 const MachineOperand &MO1 = MI.getOperand(CurOp++);
Nicolas Geoffraycb162a02008-04-16 20:10:13 +0000614 unsigned Size = X86InstrInfo::sizeOfImm(Desc);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000615 if (MO1.isImmediate())
616 emitConstant(MO1.getImm(), Size);
617 else {
618 unsigned rt = Is64BitMode ? X86::reloc_pcrel_word
Evan Cheng8ee6bab2007-12-22 09:40:20 +0000619 : (IsPIC ? X86::reloc_picrel_word : X86::reloc_absolute_word);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000620 if (Opcode == X86::MOV64ri32)
621 rt = X86::reloc_absolute_word; // FIXME: add X86II flag?
Evan Chengf0123872008-01-03 02:56:28 +0000622 if (MO1.isGlobalAddress()) {
Evan Cheng28e7e162008-01-04 10:46:51 +0000623 bool NeedStub = isa<Function>(MO1.getGlobal());
624 bool isLazy = gvNeedsLazyPtr(MO1.getGlobal());
625 emitGlobalAddress(MO1.getGlobal(), rt, MO1.getOffset(), 0,
626 NeedStub, isLazy);
Evan Chengf0123872008-01-03 02:56:28 +0000627 } else if (MO1.isExternalSymbol())
628 emitExternalSymbolAddress(MO1.getSymbolName(), rt);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000629 else if (MO1.isConstantPoolIndex())
Evan Chengf0123872008-01-03 02:56:28 +0000630 emitConstPoolAddress(MO1.getIndex(), rt);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000631 else if (MO1.isJumpTableIndex())
Evan Chengf0123872008-01-03 02:56:28 +0000632 emitJumpTableAddress(MO1.getIndex(), rt);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000633 }
634 }
635 break;
636
637 case X86II::MRM0m: case X86II::MRM1m:
638 case X86II::MRM2m: case X86II::MRM3m:
639 case X86II::MRM4m: case X86II::MRM5m:
640 case X86II::MRM6m: case X86II::MRM7m: {
Evan Cheng8ee6bab2007-12-22 09:40:20 +0000641 intptr_t PCAdj = (CurOp+4 != NumOps) ?
Nicolas Geoffraycb162a02008-04-16 20:10:13 +0000642 (MI.getOperand(CurOp+4).isImmediate() ? X86InstrInfo::sizeOfImm(Desc) : 4) : 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000643
644 MCE.emitByte(BaseOpcode);
645 emitMemModRMByte(MI, CurOp, (Desc->TSFlags & X86II::FormMask)-X86II::MRM0m,
646 PCAdj);
647 CurOp += 4;
648
649 if (CurOp != NumOps) {
650 const MachineOperand &MO = MI.getOperand(CurOp++);
Nicolas Geoffraycb162a02008-04-16 20:10:13 +0000651 unsigned Size = X86InstrInfo::sizeOfImm(Desc);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000652 if (MO.isImmediate())
653 emitConstant(MO.getImm(), Size);
654 else {
655 unsigned rt = Is64BitMode ? X86::reloc_pcrel_word
Evan Cheng8ee6bab2007-12-22 09:40:20 +0000656 : (IsPIC ? X86::reloc_picrel_word : X86::reloc_absolute_word);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000657 if (Opcode == X86::MOV64mi32)
658 rt = X86::reloc_absolute_word; // FIXME: add X86II flag?
Evan Chengf0123872008-01-03 02:56:28 +0000659 if (MO.isGlobalAddress()) {
Evan Cheng28e7e162008-01-04 10:46:51 +0000660 bool NeedStub = isa<Function>(MO.getGlobal());
661 bool isLazy = gvNeedsLazyPtr(MO.getGlobal());
662 emitGlobalAddress(MO.getGlobal(), rt, MO.getOffset(), 0,
663 NeedStub, isLazy);
Evan Chengf0123872008-01-03 02:56:28 +0000664 } else if (MO.isExternalSymbol())
665 emitExternalSymbolAddress(MO.getSymbolName(), rt);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000666 else if (MO.isConstantPoolIndex())
Evan Chengf0123872008-01-03 02:56:28 +0000667 emitConstPoolAddress(MO.getIndex(), rt);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000668 else if (MO.isJumpTableIndex())
Evan Chengf0123872008-01-03 02:56:28 +0000669 emitJumpTableAddress(MO.getIndex(), rt);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000670 }
671 }
672 break;
673 }
674
675 case X86II::MRMInitReg:
676 MCE.emitByte(BaseOpcode);
677 // Duplicate register, used by things like MOV8r0 (aka xor reg,reg).
678 emitRegModRMByte(MI.getOperand(CurOp).getReg(),
679 getX86RegNum(MI.getOperand(CurOp).getReg()));
680 ++CurOp;
681 break;
682 }
683
Evan Cheng6032b652008-03-05 02:08:03 +0000684 if (!Desc->isVariadic() && CurOp != NumOps) {
685 cerr << "Cannot encode: ";
686 MI.dump();
687 cerr << '\n';
688 abort();
689 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000690}