blob: d531fd5f7b6b4f3b627b7452fa7a779663145698 [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"
17#include "X86Subtarget.h"
18#include "X86TargetMachine.h"
19#include "X86Relocations.h"
20#include "X86.h"
21#include "llvm/PassManager.h"
22#include "llvm/CodeGen/MachineCodeEmitter.h"
23#include "llvm/CodeGen/MachineFunctionPass.h"
24#include "llvm/CodeGen/MachineInstr.h"
25#include "llvm/CodeGen/Passes.h"
26#include "llvm/Function.h"
27#include "llvm/ADT/Statistic.h"
28#include "llvm/Support/Compiler.h"
29#include "llvm/Target/TargetOptions.h"
30using namespace llvm;
31
32STATISTIC(NumEmitted, "Number of machine instructions emitted");
33
34namespace {
35 class VISIBILITY_HIDDEN Emitter : public MachineFunctionPass {
36 const X86InstrInfo *II;
37 const TargetData *TD;
38 TargetMachine &TM;
39 MachineCodeEmitter &MCE;
Evan Cheng8ee6bab2007-12-22 09:40:20 +000040 intptr_t PICBase;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000041 bool Is64BitMode;
Evan Cheng8ee6bab2007-12-22 09:40:20 +000042 bool IsPIC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000043 public:
44 static char ID;
45 explicit Emitter(TargetMachine &tm, MachineCodeEmitter &mce)
46 : MachineFunctionPass((intptr_t)&ID), II(0), TD(0), TM(tm),
Evan Cheng8ee6bab2007-12-22 09:40:20 +000047 MCE(mce), PICBase(0), Is64BitMode(false),
Evan Cheng28e7e162008-01-04 10:46:51 +000048 IsPIC(TM.getRelocationModel() == Reloc::PIC_) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +000049 Emitter(TargetMachine &tm, MachineCodeEmitter &mce,
50 const X86InstrInfo &ii, const TargetData &td, bool is64)
51 : MachineFunctionPass((intptr_t)&ID), II(&ii), TD(&td), TM(tm),
Evan Cheng8ee6bab2007-12-22 09:40:20 +000052 MCE(mce), PICBase(0), Is64BitMode(is64),
Evan Cheng28e7e162008-01-04 10:46:51 +000053 IsPIC(TM.getRelocationModel() == Reloc::PIC_) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +000054
55 bool runOnMachineFunction(MachineFunction &MF);
56
57 virtual const char *getPassName() const {
58 return "X86 Machine Code Emitter";
59 }
60
Evan Cheng0729ccf2008-01-05 00:41:47 +000061 void emitInstruction(const MachineInstr &MI,
62 const TargetInstrDescriptor *Desc);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000063
64 private:
65 void emitPCRelativeBlockAddress(MachineBasicBlock *MBB);
Evan Cheng8ee6bab2007-12-22 09:40:20 +000066 void emitGlobalAddress(GlobalValue *GV, unsigned Reloc,
67 int Disp = 0, intptr_t PCAdj = 0,
Evan Cheng28e7e162008-01-04 10:46:51 +000068 bool NeedStub = false, bool IsLazy = false);
Evan Chengf0123872008-01-03 02:56:28 +000069 void emitExternalSymbolAddress(const char *ES, unsigned Reloc);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000070 void emitConstPoolAddress(unsigned CPI, unsigned Reloc, int Disp = 0,
Evan Chengf0123872008-01-03 02:56:28 +000071 intptr_t PCAdj = 0);
Evan Cheng8ee6bab2007-12-22 09:40:20 +000072 void emitJumpTableAddress(unsigned JTI, unsigned Reloc,
Evan Chengf0123872008-01-03 02:56:28 +000073 intptr_t PCAdj = 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000074
75 void emitDisplacementField(const MachineOperand *RelocOp, int DispVal,
Evan Cheng8ee6bab2007-12-22 09:40:20 +000076 intptr_t PCAdj = 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000077
78 void emitRegModRMByte(unsigned ModRMReg, unsigned RegOpcodeField);
79 void emitSIBByte(unsigned SS, unsigned Index, unsigned Base);
80 void emitConstant(uint64_t Val, unsigned Size);
81
82 void emitMemModRMByte(const MachineInstr &MI,
83 unsigned Op, unsigned RegOpcodeField,
Evan Cheng8ee6bab2007-12-22 09:40:20 +000084 intptr_t PCAdj = 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000085
86 unsigned getX86RegNum(unsigned RegNo);
87 bool isX86_64ExtendedReg(const MachineOperand &MO);
88 unsigned determineREX(const MachineInstr &MI);
Evan Cheng28e7e162008-01-04 10:46:51 +000089
90 bool gvNeedsLazyPtr(const GlobalValue *GV);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000091 };
92 char Emitter::ID = 0;
93}
94
95/// createX86CodeEmitterPass - Return a pass that emits the collected X86 code
96/// to the specified MCE object.
97FunctionPass *llvm::createX86CodeEmitterPass(X86TargetMachine &TM,
98 MachineCodeEmitter &MCE) {
99 return new Emitter(TM, MCE);
100}
101
102bool Emitter::runOnMachineFunction(MachineFunction &MF) {
103 assert((MF.getTarget().getRelocationModel() != Reloc::Default ||
104 MF.getTarget().getRelocationModel() != Reloc::Static) &&
105 "JIT relocation model must be set to static or default!");
Evan Cheng28e7e162008-01-04 10:46:51 +0000106 II = ((X86TargetMachine&)TM).getInstrInfo();
107 TD = ((X86TargetMachine&)TM).getTargetData();
108 Is64BitMode = TM.getSubtarget<X86Subtarget>().is64Bit();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000109
110 do {
111 MCE.startFunction(MF);
112 for (MachineFunction::iterator MBB = MF.begin(), E = MF.end();
113 MBB != E; ++MBB) {
114 MCE.StartMachineBasicBlock(MBB);
115 for (MachineBasicBlock::const_iterator I = MBB->begin(), E = MBB->end();
Evan Cheng0729ccf2008-01-05 00:41:47 +0000116 I != E; ++I) {
117 const TargetInstrDescriptor *Desc = I->getInstrDescriptor();
118 emitInstruction(*I, Desc);
119 // MOVPC32r is basically a call plus a pop instruction.
120 if (Desc->Opcode == X86::MOVPC32r)
121 emitInstruction(*I, &II->get(X86::POP32r));
122 NumEmitted++; // Keep track of the # of mi's emitted
123 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000124 }
125 } while (MCE.finishFunction(MF));
126
127 return false;
128}
129
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000130/// emitPCRelativeBlockAddress - This method keeps track of the information
131/// necessary to resolve the address of this block later and emits a dummy
132/// value.
133///
134void Emitter::emitPCRelativeBlockAddress(MachineBasicBlock *MBB) {
135 // Remember where this reference was and where it is to so we can
136 // deal with it later.
137 MCE.addRelocation(MachineRelocation::getBB(MCE.getCurrentPCOffset(),
138 X86::reloc_pcrel_word, MBB));
139 MCE.emitWordLE(0);
140}
141
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000142/// emitGlobalAddress - Emit the specified address to the code stream assuming
143/// this is part of a "take the address of a global" instruction.
144///
Evan Cheng8ee6bab2007-12-22 09:40:20 +0000145void Emitter::emitGlobalAddress(GlobalValue *GV, unsigned Reloc,
146 int Disp /* = 0 */, intptr_t PCAdj /* = 0 */,
Evan Cheng28e7e162008-01-04 10:46:51 +0000147 bool NeedStub /* = false */,
148 bool isLazy /* = false */) {
149 intptr_t RelocCST = 0;
Evan Chengf0123872008-01-03 02:56:28 +0000150 if (Reloc == X86::reloc_picrel_word)
Evan Cheng28e7e162008-01-04 10:46:51 +0000151 RelocCST = PICBase;
152 else if (Reloc == X86::reloc_pcrel_word)
153 RelocCST = PCAdj;
154 MachineRelocation MR = isLazy
155 ? MachineRelocation::getGVLazyPtr(MCE.getCurrentPCOffset(), Reloc,
156 GV, RelocCST, NeedStub)
157 : MachineRelocation::getGV(MCE.getCurrentPCOffset(), Reloc,
158 GV, RelocCST, NeedStub);
159 MCE.addRelocation(MR);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000160 if (Reloc == X86::reloc_absolute_dword)
161 MCE.emitWordLE(0);
162 MCE.emitWordLE(Disp); // The relocated value will be added to the displacement
163}
164
165/// emitExternalSymbolAddress - Arrange for the address of an external symbol to
166/// be emitted to the current location in the function, and allow it to be PC
167/// relative.
Evan Chengf0123872008-01-03 02:56:28 +0000168void Emitter::emitExternalSymbolAddress(const char *ES, unsigned Reloc) {
Evan Cheng28e7e162008-01-04 10:46:51 +0000169 intptr_t RelocCST = (Reloc == X86::reloc_picrel_word) ? PICBase : 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000170 MCE.addRelocation(MachineRelocation::getExtSym(MCE.getCurrentPCOffset(),
Evan Cheng28e7e162008-01-04 10:46:51 +0000171 Reloc, ES, RelocCST));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000172 if (Reloc == X86::reloc_absolute_dword)
173 MCE.emitWordLE(0);
174 MCE.emitWordLE(0);
175}
176
177/// emitConstPoolAddress - Arrange for the address of an constant pool
178/// to be emitted to the current location in the function, and allow it to be PC
179/// relative.
180void Emitter::emitConstPoolAddress(unsigned CPI, unsigned Reloc,
181 int Disp /* = 0 */,
Evan Chengf0123872008-01-03 02:56:28 +0000182 intptr_t PCAdj /* = 0 */) {
Evan Cheng28e7e162008-01-04 10:46:51 +0000183 intptr_t RelocCST = 0;
Evan Chengf0123872008-01-03 02:56:28 +0000184 if (Reloc == X86::reloc_picrel_word)
Evan Cheng28e7e162008-01-04 10:46:51 +0000185 RelocCST = PICBase;
186 else if (Reloc == X86::reloc_pcrel_word)
187 RelocCST = PCAdj;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000188 MCE.addRelocation(MachineRelocation::getConstPool(MCE.getCurrentPCOffset(),
Evan Cheng28e7e162008-01-04 10:46:51 +0000189 Reloc, CPI, RelocCST));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000190 if (Reloc == X86::reloc_absolute_dword)
191 MCE.emitWordLE(0);
192 MCE.emitWordLE(Disp); // The relocated value will be added to the displacement
193}
194
195/// emitJumpTableAddress - Arrange for the address of a jump table to
196/// be emitted to the current location in the function, and allow it to be PC
197/// relative.
198void Emitter::emitJumpTableAddress(unsigned JTI, unsigned Reloc,
Evan Chengf0123872008-01-03 02:56:28 +0000199 intptr_t PCAdj /* = 0 */) {
Evan Cheng28e7e162008-01-04 10:46:51 +0000200 intptr_t RelocCST = 0;
Evan Chengf0123872008-01-03 02:56:28 +0000201 if (Reloc == X86::reloc_picrel_word)
Evan Cheng28e7e162008-01-04 10:46:51 +0000202 RelocCST = PICBase;
203 else if (Reloc == X86::reloc_pcrel_word)
204 RelocCST = PCAdj;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000205 MCE.addRelocation(MachineRelocation::getJumpTable(MCE.getCurrentPCOffset(),
Evan Cheng28e7e162008-01-04 10:46:51 +0000206 Reloc, JTI, RelocCST));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000207 if (Reloc == X86::reloc_absolute_dword)
208 MCE.emitWordLE(0);
209 MCE.emitWordLE(0); // The relocated value will be added to the displacement
210}
211
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000212unsigned Emitter::getX86RegNum(unsigned RegNo) {
Duncan Sands466eadd2007-08-29 19:01:20 +0000213 return ((X86RegisterInfo&)II->getRegisterInfo()).getX86RegNum(RegNo);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000214}
215
216inline static unsigned char ModRMByte(unsigned Mod, unsigned RegOpcode,
217 unsigned RM) {
218 assert(Mod < 4 && RegOpcode < 8 && RM < 8 && "ModRM Fields out of range!");
219 return RM | (RegOpcode << 3) | (Mod << 6);
220}
221
222void Emitter::emitRegModRMByte(unsigned ModRMReg, unsigned RegOpcodeFld){
223 MCE.emitByte(ModRMByte(3, RegOpcodeFld, getX86RegNum(ModRMReg)));
224}
225
226void Emitter::emitSIBByte(unsigned SS, unsigned Index, unsigned Base) {
227 // SIB byte is in the same format as the ModRMByte...
228 MCE.emitByte(ModRMByte(SS, Index, Base));
229}
230
231void Emitter::emitConstant(uint64_t Val, unsigned Size) {
232 // Output the constant in little endian byte order...
233 for (unsigned i = 0; i != Size; ++i) {
234 MCE.emitByte(Val & 255);
235 Val >>= 8;
236 }
237}
238
239/// isDisp8 - Return true if this signed displacement fits in a 8-bit
240/// sign-extended field.
241static bool isDisp8(int Value) {
242 return Value == (signed char)Value;
243}
244
Evan Cheng28e7e162008-01-04 10:46:51 +0000245bool Emitter::gvNeedsLazyPtr(const GlobalValue *GV) {
246 return !Is64BitMode &&
247 TM.getSubtarget<X86Subtarget>().GVRequiresExtraLoad(GV, TM, false);
248}
249
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000250void Emitter::emitDisplacementField(const MachineOperand *RelocOp,
Evan Cheng8ee6bab2007-12-22 09:40:20 +0000251 int DispVal, intptr_t PCAdj) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000252 // If this is a simple integer displacement that doesn't require a relocation,
253 // emit it now.
254 if (!RelocOp) {
255 emitConstant(DispVal, 4);
256 return;
257 }
258
259 // Otherwise, this is something that requires a relocation. Emit it as such
260 // now.
261 if (RelocOp->isGlobalAddress()) {
262 // In 64-bit static small code model, we could potentially emit absolute.
263 // But it's probably not beneficial.
264 // 89 05 00 00 00 00 mov %eax,0(%rip) # PC-relative
265 // 89 04 25 00 00 00 00 mov %eax,0x0 # Absolute
Evan Chengf0123872008-01-03 02:56:28 +0000266 unsigned rt = Is64BitMode ? X86::reloc_pcrel_word
Evan Cheng8ee6bab2007-12-22 09:40:20 +0000267 : (IsPIC ? X86::reloc_picrel_word : X86::reloc_absolute_word);
Evan Cheng28e7e162008-01-04 10:46:51 +0000268 bool NeedStub = isa<Function>(RelocOp->getGlobal());
269 bool isLazy = gvNeedsLazyPtr(RelocOp->getGlobal());
Evan Cheng8ee6bab2007-12-22 09:40:20 +0000270 emitGlobalAddress(RelocOp->getGlobal(), rt, RelocOp->getOffset(),
Evan Cheng28e7e162008-01-04 10:46:51 +0000271 PCAdj, NeedStub, isLazy);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000272 } else if (RelocOp->isConstantPoolIndex()) {
Evan Cheng8c872652008-01-02 23:38:59 +0000273 unsigned rt = Is64BitMode ? X86::reloc_pcrel_word : X86::reloc_picrel_word;
274 emitConstPoolAddress(RelocOp->getIndex(), rt,
Evan Chengf0123872008-01-03 02:56:28 +0000275 RelocOp->getOffset(), PCAdj);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000276 } else if (RelocOp->isJumpTableIndex()) {
Evan Cheng8c872652008-01-02 23:38:59 +0000277 unsigned rt = Is64BitMode ? X86::reloc_pcrel_word : X86::reloc_picrel_word;
Evan Chengf0123872008-01-03 02:56:28 +0000278 emitJumpTableAddress(RelocOp->getIndex(), rt, PCAdj);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000279 } else {
280 assert(0 && "Unknown value to relocate!");
281 }
282}
283
284void Emitter::emitMemModRMByte(const MachineInstr &MI,
285 unsigned Op, unsigned RegOpcodeField,
Evan Cheng8ee6bab2007-12-22 09:40:20 +0000286 intptr_t PCAdj) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000287 const MachineOperand &Op3 = MI.getOperand(Op+3);
288 int DispVal = 0;
289 const MachineOperand *DispForReloc = 0;
290
291 // Figure out what sort of displacement we have to handle here.
292 if (Op3.isGlobalAddress()) {
293 DispForReloc = &Op3;
294 } else if (Op3.isConstantPoolIndex()) {
Evan Cheng8c872652008-01-02 23:38:59 +0000295 if (Is64BitMode || IsPIC) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000296 DispForReloc = &Op3;
297 } else {
Chris Lattner6017d482007-12-30 23:10:15 +0000298 DispVal += MCE.getConstantPoolEntryAddress(Op3.getIndex());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000299 DispVal += Op3.getOffset();
300 }
301 } else if (Op3.isJumpTableIndex()) {
Evan Cheng8c872652008-01-02 23:38:59 +0000302 if (Is64BitMode || IsPIC) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000303 DispForReloc = &Op3;
304 } else {
Chris Lattner6017d482007-12-30 23:10:15 +0000305 DispVal += MCE.getJumpTableEntryAddress(Op3.getIndex());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000306 }
307 } else {
308 DispVal = Op3.getImm();
309 }
310
311 const MachineOperand &Base = MI.getOperand(Op);
312 const MachineOperand &Scale = MI.getOperand(Op+1);
313 const MachineOperand &IndexReg = MI.getOperand(Op+2);
314
315 unsigned BaseReg = Base.getReg();
316
317 // Is a SIB byte needed?
318 if (IndexReg.getReg() == 0 &&
319 (BaseReg == 0 || getX86RegNum(BaseReg) != N86::ESP)) {
320 if (BaseReg == 0) { // Just a displacement?
321 // Emit special case [disp32] encoding
322 MCE.emitByte(ModRMByte(0, RegOpcodeField, 5));
323
324 emitDisplacementField(DispForReloc, DispVal, PCAdj);
325 } else {
326 unsigned BaseRegNo = getX86RegNum(BaseReg);
327 if (!DispForReloc && DispVal == 0 && BaseRegNo != N86::EBP) {
328 // Emit simple indirect register encoding... [EAX] f.e.
329 MCE.emitByte(ModRMByte(0, RegOpcodeField, BaseRegNo));
330 } else if (!DispForReloc && isDisp8(DispVal)) {
331 // Emit the disp8 encoding... [REG+disp8]
332 MCE.emitByte(ModRMByte(1, RegOpcodeField, BaseRegNo));
333 emitConstant(DispVal, 1);
334 } else {
335 // Emit the most general non-SIB encoding: [REG+disp32]
336 MCE.emitByte(ModRMByte(2, RegOpcodeField, BaseRegNo));
337 emitDisplacementField(DispForReloc, DispVal, PCAdj);
338 }
339 }
340
341 } else { // We need a SIB byte, so start by outputting the ModR/M byte first
342 assert(IndexReg.getReg() != X86::ESP &&
343 IndexReg.getReg() != X86::RSP && "Cannot use ESP as index reg!");
344
345 bool ForceDisp32 = false;
346 bool ForceDisp8 = false;
347 if (BaseReg == 0) {
348 // If there is no base register, we emit the special case SIB byte with
349 // MOD=0, BASE=5, to JUST get the index, scale, and displacement.
350 MCE.emitByte(ModRMByte(0, RegOpcodeField, 4));
351 ForceDisp32 = true;
352 } else if (DispForReloc) {
353 // Emit the normal disp32 encoding.
354 MCE.emitByte(ModRMByte(2, RegOpcodeField, 4));
355 ForceDisp32 = true;
356 } else if (DispVal == 0 && getX86RegNum(BaseReg) != N86::EBP) {
357 // Emit no displacement ModR/M byte
358 MCE.emitByte(ModRMByte(0, RegOpcodeField, 4));
359 } else if (isDisp8(DispVal)) {
360 // Emit the disp8 encoding...
361 MCE.emitByte(ModRMByte(1, RegOpcodeField, 4));
362 ForceDisp8 = true; // Make sure to force 8 bit disp if Base=EBP
363 } else {
364 // Emit the normal disp32 encoding...
365 MCE.emitByte(ModRMByte(2, RegOpcodeField, 4));
366 }
367
368 // Calculate what the SS field value should be...
369 static const unsigned SSTable[] = { ~0, 0, 1, ~0, 2, ~0, ~0, ~0, 3 };
370 unsigned SS = SSTable[Scale.getImm()];
371
372 if (BaseReg == 0) {
373 // Handle the SIB byte for the case where there is no base. The
374 // displacement has already been output.
375 assert(IndexReg.getReg() && "Index register must be specified!");
376 emitSIBByte(SS, getX86RegNum(IndexReg.getReg()), 5);
377 } else {
378 unsigned BaseRegNo = getX86RegNum(BaseReg);
379 unsigned IndexRegNo;
380 if (IndexReg.getReg())
381 IndexRegNo = getX86RegNum(IndexReg.getReg());
382 else
383 IndexRegNo = 4; // For example [ESP+1*<noreg>+4]
384 emitSIBByte(SS, IndexRegNo, BaseRegNo);
385 }
386
387 // Do we need to output a displacement?
388 if (ForceDisp8) {
389 emitConstant(DispVal, 1);
390 } else if (DispVal != 0 || ForceDisp32) {
391 emitDisplacementField(DispForReloc, DispVal, PCAdj);
392 }
393 }
394}
395
396static unsigned sizeOfImm(const TargetInstrDescriptor *Desc) {
397 switch (Desc->TSFlags & X86II::ImmMask) {
398 case X86II::Imm8: return 1;
399 case X86II::Imm16: return 2;
400 case X86II::Imm32: return 4;
401 case X86II::Imm64: return 8;
402 default: assert(0 && "Immediate size not set!");
403 return 0;
404 }
405}
406
407/// isX86_64ExtendedReg - Is the MachineOperand a x86-64 extended register?
408/// e.g. r8, xmm8, etc.
409bool Emitter::isX86_64ExtendedReg(const MachineOperand &MO) {
410 if (!MO.isRegister()) return false;
Evan Chenge21ff432007-11-13 17:54:34 +0000411 switch (MO.getReg()) {
412 default: break;
413 case X86::R8: case X86::R9: case X86::R10: case X86::R11:
414 case X86::R12: case X86::R13: case X86::R14: case X86::R15:
415 case X86::R8D: case X86::R9D: case X86::R10D: case X86::R11D:
416 case X86::R12D: case X86::R13D: case X86::R14D: case X86::R15D:
417 case X86::R8W: case X86::R9W: case X86::R10W: case X86::R11W:
418 case X86::R12W: case X86::R13W: case X86::R14W: case X86::R15W:
419 case X86::R8B: case X86::R9B: case X86::R10B: case X86::R11B:
420 case X86::R12B: case X86::R13B: case X86::R14B: case X86::R15B:
421 case X86::XMM8: case X86::XMM9: case X86::XMM10: case X86::XMM11:
422 case X86::XMM12: case X86::XMM13: case X86::XMM14: case X86::XMM15:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000423 return true;
Evan Chenge21ff432007-11-13 17:54:34 +0000424 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000425 return false;
426}
427
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000428inline static bool isX86_64NonExtLowByteReg(unsigned reg) {
429 return (reg == X86::SPL || reg == X86::BPL ||
430 reg == X86::SIL || reg == X86::DIL);
431}
432
433/// determineREX - Determine if the MachineInstr has to be encoded with a X86-64
434/// REX prefix which specifies 1) 64-bit instructions, 2) non-default operand
435/// size, and 3) use of X86-64 extended registers.
436unsigned Emitter::determineREX(const MachineInstr &MI) {
437 unsigned REX = 0;
438 const TargetInstrDescriptor *Desc = MI.getInstrDescriptor();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000439
440 // Pseudo instructions do not need REX prefix byte.
441 if ((Desc->TSFlags & X86II::FormMask) == X86II::Pseudo)
442 return 0;
443 if (Desc->TSFlags & X86II::REX_W)
444 REX |= 1 << 3;
445
446 unsigned NumOps = Desc->numOperands;
447 if (NumOps) {
448 bool isTwoAddr = NumOps > 1 &&
449 Desc->getOperandConstraint(1, TOI::TIED_TO) != -1;
450
451 // If it accesses SPL, BPL, SIL, or DIL, then it requires a 0x40 REX prefix.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000452 unsigned i = isTwoAddr ? 1 : 0;
453 for (unsigned e = NumOps; i != e; ++i) {
454 const MachineOperand& MO = MI.getOperand(i);
455 if (MO.isRegister()) {
456 unsigned Reg = MO.getReg();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000457 if (isX86_64NonExtLowByteReg(Reg))
458 REX |= 0x40;
459 }
460 }
461
462 switch (Desc->TSFlags & X86II::FormMask) {
463 case X86II::MRMInitReg:
464 if (isX86_64ExtendedReg(MI.getOperand(0)))
465 REX |= (1 << 0) | (1 << 2);
466 break;
467 case X86II::MRMSrcReg: {
468 if (isX86_64ExtendedReg(MI.getOperand(0)))
469 REX |= 1 << 2;
470 i = isTwoAddr ? 2 : 1;
471 for (unsigned e = NumOps; i != e; ++i) {
472 const MachineOperand& MO = MI.getOperand(i);
473 if (isX86_64ExtendedReg(MO))
474 REX |= 1 << 0;
475 }
476 break;
477 }
478 case X86II::MRMSrcMem: {
479 if (isX86_64ExtendedReg(MI.getOperand(0)))
480 REX |= 1 << 2;
481 unsigned Bit = 0;
482 i = isTwoAddr ? 2 : 1;
483 for (; i != NumOps; ++i) {
484 const MachineOperand& MO = MI.getOperand(i);
485 if (MO.isRegister()) {
486 if (isX86_64ExtendedReg(MO))
487 REX |= 1 << Bit;
488 Bit++;
489 }
490 }
491 break;
492 }
493 case X86II::MRM0m: case X86II::MRM1m:
494 case X86II::MRM2m: case X86II::MRM3m:
495 case X86II::MRM4m: case X86II::MRM5m:
496 case X86II::MRM6m: case X86II::MRM7m:
497 case X86II::MRMDestMem: {
498 unsigned e = isTwoAddr ? 5 : 4;
499 i = isTwoAddr ? 1 : 0;
500 if (NumOps > e && isX86_64ExtendedReg(MI.getOperand(e)))
501 REX |= 1 << 2;
502 unsigned Bit = 0;
503 for (; i != e; ++i) {
504 const MachineOperand& MO = MI.getOperand(i);
505 if (MO.isRegister()) {
506 if (isX86_64ExtendedReg(MO))
507 REX |= 1 << Bit;
508 Bit++;
509 }
510 }
511 break;
512 }
513 default: {
514 if (isX86_64ExtendedReg(MI.getOperand(0)))
515 REX |= 1 << 0;
516 i = isTwoAddr ? 2 : 1;
517 for (unsigned e = NumOps; i != e; ++i) {
518 const MachineOperand& MO = MI.getOperand(i);
519 if (isX86_64ExtendedReg(MO))
520 REX |= 1 << 2;
521 }
522 break;
523 }
524 }
525 }
526 return REX;
527}
528
Evan Cheng0729ccf2008-01-05 00:41:47 +0000529void Emitter::emitInstruction(const MachineInstr &MI,
530 const TargetInstrDescriptor *Desc) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000531 unsigned Opcode = Desc->Opcode;
532
533 // Emit the repeat opcode prefix as needed.
534 if ((Desc->TSFlags & X86II::Op0Mask) == X86II::REP) MCE.emitByte(0xF3);
535
536 // Emit the operand size opcode prefix as needed.
537 if (Desc->TSFlags & X86II::OpSize) MCE.emitByte(0x66);
538
539 // Emit the address size opcode prefix as needed.
540 if (Desc->TSFlags & X86II::AdSize) MCE.emitByte(0x67);
541
542 bool Need0FPrefix = false;
543 switch (Desc->TSFlags & X86II::Op0Mask) {
544 case X86II::TB:
545 Need0FPrefix = true; // Two-byte opcode prefix
546 break;
547 case X86II::T8:
548 MCE.emitByte(0x0F);
549 MCE.emitByte(0x38);
550 break;
551 case X86II::TA:
552 MCE.emitByte(0x0F);
553 MCE.emitByte(0x3A);
554 break;
555 case X86II::REP: break; // already handled.
556 case X86II::XS: // F3 0F
557 MCE.emitByte(0xF3);
558 Need0FPrefix = true;
559 break;
560 case X86II::XD: // F2 0F
561 MCE.emitByte(0xF2);
562 Need0FPrefix = true;
563 break;
564 case X86II::D8: case X86II::D9: case X86II::DA: case X86II::DB:
565 case X86II::DC: case X86II::DD: case X86II::DE: case X86II::DF:
566 MCE.emitByte(0xD8+
567 (((Desc->TSFlags & X86II::Op0Mask)-X86II::D8)
568 >> X86II::Op0Shift));
569 break; // Two-byte opcode prefix
570 default: assert(0 && "Invalid prefix!");
571 case 0: break; // No prefix!
572 }
573
574 if (Is64BitMode) {
575 // REX prefix
576 unsigned REX = determineREX(MI);
577 if (REX)
578 MCE.emitByte(0x40 | REX);
579 }
580
581 // 0x0F escape code must be emitted just before the opcode.
582 if (Need0FPrefix)
583 MCE.emitByte(0x0F);
584
585 // If this is a two-address instruction, skip one of the register operands.
586 unsigned NumOps = Desc->numOperands;
587 unsigned CurOp = 0;
588 if (NumOps > 1 && Desc->getOperandConstraint(1, TOI::TIED_TO) != -1)
589 CurOp++;
590
591 unsigned char BaseOpcode = II->getBaseOpcodeFor(Desc);
592 switch (Desc->TSFlags & X86II::FormMask) {
593 default: assert(0 && "Unknown FormMask value in X86 MachineCodeEmitter!");
594 case X86II::Pseudo:
Evan Cheng0729ccf2008-01-05 00:41:47 +0000595 // Remember the current PC offset, this is the PIC relocation
596 // base address.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000597 switch (Opcode) {
Evan Cheng0729ccf2008-01-05 00:41:47 +0000598#ifndef NDEBUG
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000599 default:
600 assert(0 && "psuedo instructions should be removed before code emission");
601 case TargetInstrInfo::INLINEASM:
602 assert(0 && "JIT does not support inline asm!\n");
603 case TargetInstrInfo::LABEL:
604 assert(0 && "JIT does not support meta labels!\n");
605 case X86::IMPLICIT_USE:
606 case X86::IMPLICIT_DEF:
607 case X86::IMPLICIT_DEF_GR8:
608 case X86::IMPLICIT_DEF_GR16:
609 case X86::IMPLICIT_DEF_GR32:
610 case X86::IMPLICIT_DEF_GR64:
611 case X86::IMPLICIT_DEF_FR32:
612 case X86::IMPLICIT_DEF_FR64:
613 case X86::IMPLICIT_DEF_VR64:
614 case X86::IMPLICIT_DEF_VR128:
615 case X86::FP_REG_KILL:
616 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000617#endif
Evan Cheng0729ccf2008-01-05 00:41:47 +0000618 case X86::MOVPC32r:
619 // This emits the "call" portion of this pseudo instruction.
620 MCE.emitByte(BaseOpcode);
621 emitConstant(0, sizeOfImm(Desc));
622 PICBase = MCE.getCurrentPCOffset();
623 break;
624 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000625 CurOp = NumOps;
626 break;
627
628 case X86II::RawFrm:
629 MCE.emitByte(BaseOpcode);
Evan Cheng0729ccf2008-01-05 00:41:47 +0000630
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000631 if (CurOp != NumOps) {
632 const MachineOperand &MO = MI.getOperand(CurOp++);
633 if (MO.isMachineBasicBlock()) {
Chris Lattner6017d482007-12-30 23:10:15 +0000634 emitPCRelativeBlockAddress(MO.getMBB());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000635 } else if (MO.isGlobalAddress()) {
Evan Chenge5e9fe82008-01-04 10:50:28 +0000636 bool NeedStub = (Is64BitMode && TM.getCodeModel() == CodeModel::Large)
637 || Opcode == X86::TAILJMPd;
Evan Cheng8ee6bab2007-12-22 09:40:20 +0000638 emitGlobalAddress(MO.getGlobal(), X86::reloc_pcrel_word,
Evan Chengf0123872008-01-03 02:56:28 +0000639 0, 0, NeedStub);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000640 } else if (MO.isExternalSymbol()) {
Evan Chengf0123872008-01-03 02:56:28 +0000641 emitExternalSymbolAddress(MO.getSymbolName(), X86::reloc_pcrel_word);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000642 } else if (MO.isImmediate()) {
643 emitConstant(MO.getImm(), sizeOfImm(Desc));
644 } else {
645 assert(0 && "Unknown RawFrm operand!");
646 }
647 }
648 break;
649
650 case X86II::AddRegFrm:
651 MCE.emitByte(BaseOpcode + getX86RegNum(MI.getOperand(CurOp++).getReg()));
652
653 if (CurOp != NumOps) {
654 const MachineOperand &MO1 = MI.getOperand(CurOp++);
655 unsigned Size = sizeOfImm(Desc);
656 if (MO1.isImmediate())
657 emitConstant(MO1.getImm(), Size);
658 else {
Evan Cheng8ee6bab2007-12-22 09:40:20 +0000659 unsigned rt = Is64BitMode ? X86::reloc_pcrel_word
660 : (IsPIC ? X86::reloc_picrel_word : X86::reloc_absolute_word);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000661 if (Opcode == X86::MOV64ri)
662 rt = X86::reloc_absolute_dword; // FIXME: add X86II flag?
Evan Chengf0123872008-01-03 02:56:28 +0000663 if (MO1.isGlobalAddress()) {
Evan Cheng28e7e162008-01-04 10:46:51 +0000664 bool NeedStub = isa<Function>(MO1.getGlobal());
665 bool isLazy = gvNeedsLazyPtr(MO1.getGlobal());
666 emitGlobalAddress(MO1.getGlobal(), rt, MO1.getOffset(), 0,
667 NeedStub, isLazy);
Evan Chengf0123872008-01-03 02:56:28 +0000668 } else if (MO1.isExternalSymbol())
669 emitExternalSymbolAddress(MO1.getSymbolName(), rt);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000670 else if (MO1.isConstantPoolIndex())
Evan Chengf0123872008-01-03 02:56:28 +0000671 emitConstPoolAddress(MO1.getIndex(), rt);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000672 else if (MO1.isJumpTableIndex())
Evan Chengf0123872008-01-03 02:56:28 +0000673 emitJumpTableAddress(MO1.getIndex(), rt);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000674 }
675 }
676 break;
677
678 case X86II::MRMDestReg: {
679 MCE.emitByte(BaseOpcode);
680 emitRegModRMByte(MI.getOperand(CurOp).getReg(),
681 getX86RegNum(MI.getOperand(CurOp+1).getReg()));
682 CurOp += 2;
683 if (CurOp != NumOps)
684 emitConstant(MI.getOperand(CurOp++).getImm(), sizeOfImm(Desc));
685 break;
686 }
687 case X86II::MRMDestMem: {
688 MCE.emitByte(BaseOpcode);
689 emitMemModRMByte(MI, CurOp, getX86RegNum(MI.getOperand(CurOp+4).getReg()));
690 CurOp += 5;
691 if (CurOp != NumOps)
692 emitConstant(MI.getOperand(CurOp++).getImm(), sizeOfImm(Desc));
693 break;
694 }
695
696 case X86II::MRMSrcReg:
697 MCE.emitByte(BaseOpcode);
698 emitRegModRMByte(MI.getOperand(CurOp+1).getReg(),
699 getX86RegNum(MI.getOperand(CurOp).getReg()));
700 CurOp += 2;
701 if (CurOp != NumOps)
702 emitConstant(MI.getOperand(CurOp++).getImm(), sizeOfImm(Desc));
703 break;
704
705 case X86II::MRMSrcMem: {
Evan Cheng8ee6bab2007-12-22 09:40:20 +0000706 intptr_t PCAdj = (CurOp+5 != NumOps) ? sizeOfImm(Desc) : 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000707
708 MCE.emitByte(BaseOpcode);
709 emitMemModRMByte(MI, CurOp+1, getX86RegNum(MI.getOperand(CurOp).getReg()),
710 PCAdj);
711 CurOp += 5;
712 if (CurOp != NumOps)
713 emitConstant(MI.getOperand(CurOp++).getImm(), sizeOfImm(Desc));
714 break;
715 }
716
717 case X86II::MRM0r: case X86II::MRM1r:
718 case X86II::MRM2r: case X86II::MRM3r:
719 case X86II::MRM4r: case X86II::MRM5r:
720 case X86II::MRM6r: case X86II::MRM7r:
721 MCE.emitByte(BaseOpcode);
722 emitRegModRMByte(MI.getOperand(CurOp++).getReg(),
723 (Desc->TSFlags & X86II::FormMask)-X86II::MRM0r);
724
725 if (CurOp != NumOps) {
726 const MachineOperand &MO1 = MI.getOperand(CurOp++);
727 unsigned Size = sizeOfImm(Desc);
728 if (MO1.isImmediate())
729 emitConstant(MO1.getImm(), Size);
730 else {
731 unsigned rt = Is64BitMode ? X86::reloc_pcrel_word
Evan Cheng8ee6bab2007-12-22 09:40:20 +0000732 : (IsPIC ? X86::reloc_picrel_word : X86::reloc_absolute_word);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000733 if (Opcode == X86::MOV64ri32)
734 rt = X86::reloc_absolute_word; // FIXME: add X86II flag?
Evan Chengf0123872008-01-03 02:56:28 +0000735 if (MO1.isGlobalAddress()) {
Evan Cheng28e7e162008-01-04 10:46:51 +0000736 bool NeedStub = isa<Function>(MO1.getGlobal());
737 bool isLazy = gvNeedsLazyPtr(MO1.getGlobal());
738 emitGlobalAddress(MO1.getGlobal(), rt, MO1.getOffset(), 0,
739 NeedStub, isLazy);
Evan Chengf0123872008-01-03 02:56:28 +0000740 } else if (MO1.isExternalSymbol())
741 emitExternalSymbolAddress(MO1.getSymbolName(), rt);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000742 else if (MO1.isConstantPoolIndex())
Evan Chengf0123872008-01-03 02:56:28 +0000743 emitConstPoolAddress(MO1.getIndex(), rt);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000744 else if (MO1.isJumpTableIndex())
Evan Chengf0123872008-01-03 02:56:28 +0000745 emitJumpTableAddress(MO1.getIndex(), rt);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000746 }
747 }
748 break;
749
750 case X86II::MRM0m: case X86II::MRM1m:
751 case X86II::MRM2m: case X86II::MRM3m:
752 case X86II::MRM4m: case X86II::MRM5m:
753 case X86II::MRM6m: case X86II::MRM7m: {
Evan Cheng8ee6bab2007-12-22 09:40:20 +0000754 intptr_t PCAdj = (CurOp+4 != NumOps) ?
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000755 (MI.getOperand(CurOp+4).isImmediate() ? sizeOfImm(Desc) : 4) : 0;
756
757 MCE.emitByte(BaseOpcode);
758 emitMemModRMByte(MI, CurOp, (Desc->TSFlags & X86II::FormMask)-X86II::MRM0m,
759 PCAdj);
760 CurOp += 4;
761
762 if (CurOp != NumOps) {
763 const MachineOperand &MO = MI.getOperand(CurOp++);
764 unsigned Size = sizeOfImm(Desc);
765 if (MO.isImmediate())
766 emitConstant(MO.getImm(), Size);
767 else {
768 unsigned rt = Is64BitMode ? X86::reloc_pcrel_word
Evan Cheng8ee6bab2007-12-22 09:40:20 +0000769 : (IsPIC ? X86::reloc_picrel_word : X86::reloc_absolute_word);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000770 if (Opcode == X86::MOV64mi32)
771 rt = X86::reloc_absolute_word; // FIXME: add X86II flag?
Evan Chengf0123872008-01-03 02:56:28 +0000772 if (MO.isGlobalAddress()) {
Evan Cheng28e7e162008-01-04 10:46:51 +0000773 bool NeedStub = isa<Function>(MO.getGlobal());
774 bool isLazy = gvNeedsLazyPtr(MO.getGlobal());
775 emitGlobalAddress(MO.getGlobal(), rt, MO.getOffset(), 0,
776 NeedStub, isLazy);
Evan Chengf0123872008-01-03 02:56:28 +0000777 } else if (MO.isExternalSymbol())
778 emitExternalSymbolAddress(MO.getSymbolName(), rt);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000779 else if (MO.isConstantPoolIndex())
Evan Chengf0123872008-01-03 02:56:28 +0000780 emitConstPoolAddress(MO.getIndex(), rt);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000781 else if (MO.isJumpTableIndex())
Evan Chengf0123872008-01-03 02:56:28 +0000782 emitJumpTableAddress(MO.getIndex(), rt);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000783 }
784 }
785 break;
786 }
787
788 case X86II::MRMInitReg:
789 MCE.emitByte(BaseOpcode);
790 // Duplicate register, used by things like MOV8r0 (aka xor reg,reg).
791 emitRegModRMByte(MI.getOperand(CurOp).getReg(),
792 getX86RegNum(MI.getOperand(CurOp).getReg()));
793 ++CurOp;
794 break;
795 }
796
797 assert((Desc->Flags & M_VARIABLE_OPS) != 0 ||
798 CurOp == NumOps && "Unknown encoding!");
799}