blob: 14b5762b9897983bb3ed77459d98f3a24c0aad92 [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;
Evan Chengf0123872008-01-03 02:56:28 +000043 bool IsStatic;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000044 public:
45 static char ID;
46 explicit Emitter(TargetMachine &tm, MachineCodeEmitter &mce)
47 : MachineFunctionPass((intptr_t)&ID), II(0), TD(0), TM(tm),
Evan Cheng8ee6bab2007-12-22 09:40:20 +000048 MCE(mce), PICBase(0), Is64BitMode(false),
Evan Chengf0123872008-01-03 02:56:28 +000049 IsPIC(TM.getRelocationModel() == Reloc::PIC_),
50 IsStatic(TM.getRelocationModel() == Reloc::Static) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +000051 Emitter(TargetMachine &tm, MachineCodeEmitter &mce,
52 const X86InstrInfo &ii, const TargetData &td, bool is64)
53 : MachineFunctionPass((intptr_t)&ID), II(&ii), TD(&td), TM(tm),
Evan Cheng8ee6bab2007-12-22 09:40:20 +000054 MCE(mce), PICBase(0), Is64BitMode(is64),
Evan Chengf0123872008-01-03 02:56:28 +000055 IsPIC(TM.getRelocationModel() == Reloc::PIC_),
56 IsStatic(TM.getRelocationModel() == Reloc::Static) {}
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
64 void emitInstruction(const MachineInstr &MI);
65
66 private:
67 void emitPCRelativeBlockAddress(MachineBasicBlock *MBB);
Evan Cheng8ee6bab2007-12-22 09:40:20 +000068 void emitGlobalAddress(GlobalValue *GV, unsigned Reloc,
69 int Disp = 0, intptr_t PCAdj = 0,
Evan Chengf0123872008-01-03 02:56:28 +000070 bool NeedStub = false);
71 void emitExternalSymbolAddress(const char *ES, unsigned Reloc);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000072 void emitConstPoolAddress(unsigned CPI, unsigned Reloc, int Disp = 0,
Evan Chengf0123872008-01-03 02:56:28 +000073 intptr_t PCAdj = 0);
Evan Cheng8ee6bab2007-12-22 09:40:20 +000074 void emitJumpTableAddress(unsigned JTI, unsigned Reloc,
Evan Chengf0123872008-01-03 02:56:28 +000075 intptr_t PCAdj = 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000076
77 void emitDisplacementField(const MachineOperand *RelocOp, int DispVal,
Evan Cheng8ee6bab2007-12-22 09:40:20 +000078 intptr_t PCAdj = 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000079
80 void emitRegModRMByte(unsigned ModRMReg, unsigned RegOpcodeField);
81 void emitSIBByte(unsigned SS, unsigned Index, unsigned Base);
82 void emitConstant(uint64_t Val, unsigned Size);
83
84 void emitMemModRMByte(const MachineInstr &MI,
85 unsigned Op, unsigned RegOpcodeField,
Evan Cheng8ee6bab2007-12-22 09:40:20 +000086 intptr_t PCAdj = 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000087
88 unsigned getX86RegNum(unsigned RegNo);
89 bool isX86_64ExtendedReg(const MachineOperand &MO);
90 unsigned determineREX(const MachineInstr &MI);
91 };
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!");
106 II = ((X86TargetMachine&)MF.getTarget()).getInstrInfo();
107 TD = ((X86TargetMachine&)MF.getTarget()).getTargetData();
108 Is64BitMode =
109 ((X86TargetMachine&)MF.getTarget()).getSubtarget<X86Subtarget>().is64Bit();
110
111 do {
112 MCE.startFunction(MF);
113 for (MachineFunction::iterator MBB = MF.begin(), E = MF.end();
114 MBB != E; ++MBB) {
115 MCE.StartMachineBasicBlock(MBB);
116 for (MachineBasicBlock::const_iterator I = MBB->begin(), E = MBB->end();
117 I != E; ++I)
118 emitInstruction(*I);
119 }
120 } while (MCE.finishFunction(MF));
121
122 return false;
123}
124
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000125/// emitPCRelativeBlockAddress - This method keeps track of the information
126/// necessary to resolve the address of this block later and emits a dummy
127/// value.
128///
129void Emitter::emitPCRelativeBlockAddress(MachineBasicBlock *MBB) {
130 // Remember where this reference was and where it is to so we can
131 // deal with it later.
132 MCE.addRelocation(MachineRelocation::getBB(MCE.getCurrentPCOffset(),
133 X86::reloc_pcrel_word, MBB));
134 MCE.emitWordLE(0);
135}
136
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000137/// emitGlobalAddress - Emit the specified address to the code stream assuming
138/// this is part of a "take the address of a global" instruction.
139///
Evan Cheng8ee6bab2007-12-22 09:40:20 +0000140void Emitter::emitGlobalAddress(GlobalValue *GV, unsigned Reloc,
141 int Disp /* = 0 */, intptr_t PCAdj /* = 0 */,
Evan Chengf0123872008-01-03 02:56:28 +0000142 bool NeedStub /* = false */) {
143 if (Reloc == X86::reloc_picrel_word)
Evan Cheng8ee6bab2007-12-22 09:40:20 +0000144 PCAdj += PICBase;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000145 MCE.addRelocation(MachineRelocation::getGV(MCE.getCurrentPCOffset(), Reloc,
Evan Chengf0123872008-01-03 02:56:28 +0000146 GV, PCAdj, NeedStub));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000147 if (Reloc == X86::reloc_absolute_dword)
148 MCE.emitWordLE(0);
149 MCE.emitWordLE(Disp); // The relocated value will be added to the displacement
150}
151
152/// emitExternalSymbolAddress - Arrange for the address of an external symbol to
153/// be emitted to the current location in the function, and allow it to be PC
154/// relative.
Evan Chengf0123872008-01-03 02:56:28 +0000155void Emitter::emitExternalSymbolAddress(const char *ES, unsigned Reloc) {
156 intptr_t PCAdj = (Reloc == X86::reloc_picrel_word) ? PICBase : 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000157 MCE.addRelocation(MachineRelocation::getExtSym(MCE.getCurrentPCOffset(),
Evan Cheng8ee6bab2007-12-22 09:40:20 +0000158 Reloc, ES, PCAdj));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000159 if (Reloc == X86::reloc_absolute_dword)
160 MCE.emitWordLE(0);
161 MCE.emitWordLE(0);
162}
163
164/// emitConstPoolAddress - Arrange for the address of an constant pool
165/// to be emitted to the current location in the function, and allow it to be PC
166/// relative.
167void Emitter::emitConstPoolAddress(unsigned CPI, unsigned Reloc,
168 int Disp /* = 0 */,
Evan Chengf0123872008-01-03 02:56:28 +0000169 intptr_t PCAdj /* = 0 */) {
170 if (Reloc == X86::reloc_picrel_word)
Evan Cheng8ee6bab2007-12-22 09:40:20 +0000171 PCAdj += PICBase;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000172 MCE.addRelocation(MachineRelocation::getConstPool(MCE.getCurrentPCOffset(),
173 Reloc, CPI, PCAdj));
174 if (Reloc == X86::reloc_absolute_dword)
175 MCE.emitWordLE(0);
176 MCE.emitWordLE(Disp); // The relocated value will be added to the displacement
177}
178
179/// emitJumpTableAddress - Arrange for the address of a jump table to
180/// be emitted to the current location in the function, and allow it to be PC
181/// relative.
182void Emitter::emitJumpTableAddress(unsigned JTI, unsigned Reloc,
Evan Chengf0123872008-01-03 02:56:28 +0000183 intptr_t PCAdj /* = 0 */) {
184 if (Reloc == X86::reloc_picrel_word)
Evan Cheng8ee6bab2007-12-22 09:40:20 +0000185 PCAdj += PICBase;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000186 MCE.addRelocation(MachineRelocation::getJumpTable(MCE.getCurrentPCOffset(),
187 Reloc, JTI, PCAdj));
188 if (Reloc == X86::reloc_absolute_dword)
189 MCE.emitWordLE(0);
190 MCE.emitWordLE(0); // The relocated value will be added to the displacement
191}
192
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000193unsigned Emitter::getX86RegNum(unsigned RegNo) {
Duncan Sands466eadd2007-08-29 19:01:20 +0000194 return ((X86RegisterInfo&)II->getRegisterInfo()).getX86RegNum(RegNo);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000195}
196
197inline static unsigned char ModRMByte(unsigned Mod, unsigned RegOpcode,
198 unsigned RM) {
199 assert(Mod < 4 && RegOpcode < 8 && RM < 8 && "ModRM Fields out of range!");
200 return RM | (RegOpcode << 3) | (Mod << 6);
201}
202
203void Emitter::emitRegModRMByte(unsigned ModRMReg, unsigned RegOpcodeFld){
204 MCE.emitByte(ModRMByte(3, RegOpcodeFld, getX86RegNum(ModRMReg)));
205}
206
207void Emitter::emitSIBByte(unsigned SS, unsigned Index, unsigned Base) {
208 // SIB byte is in the same format as the ModRMByte...
209 MCE.emitByte(ModRMByte(SS, Index, Base));
210}
211
212void Emitter::emitConstant(uint64_t Val, unsigned Size) {
213 // Output the constant in little endian byte order...
214 for (unsigned i = 0; i != Size; ++i) {
215 MCE.emitByte(Val & 255);
216 Val >>= 8;
217 }
218}
219
220/// isDisp8 - Return true if this signed displacement fits in a 8-bit
221/// sign-extended field.
222static bool isDisp8(int Value) {
223 return Value == (signed char)Value;
224}
225
226void Emitter::emitDisplacementField(const MachineOperand *RelocOp,
Evan Cheng8ee6bab2007-12-22 09:40:20 +0000227 int DispVal, intptr_t PCAdj) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000228 // If this is a simple integer displacement that doesn't require a relocation,
229 // emit it now.
230 if (!RelocOp) {
231 emitConstant(DispVal, 4);
232 return;
233 }
234
235 // Otherwise, this is something that requires a relocation. Emit it as such
236 // now.
237 if (RelocOp->isGlobalAddress()) {
238 // In 64-bit static small code model, we could potentially emit absolute.
239 // But it's probably not beneficial.
240 // 89 05 00 00 00 00 mov %eax,0(%rip) # PC-relative
241 // 89 04 25 00 00 00 00 mov %eax,0x0 # Absolute
Evan Chengf0123872008-01-03 02:56:28 +0000242 unsigned rt = Is64BitMode ? X86::reloc_pcrel_word
Evan Cheng8ee6bab2007-12-22 09:40:20 +0000243 : (IsPIC ? X86::reloc_picrel_word : X86::reloc_absolute_word);
Evan Chengf0123872008-01-03 02:56:28 +0000244 bool NeedStub = !IsStatic || isa<Function>(RelocOp->getGlobal());
Evan Cheng8ee6bab2007-12-22 09:40:20 +0000245 emitGlobalAddress(RelocOp->getGlobal(), rt, RelocOp->getOffset(),
Evan Chengf0123872008-01-03 02:56:28 +0000246 PCAdj, NeedStub);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000247 } else if (RelocOp->isConstantPoolIndex()) {
Evan Cheng8c872652008-01-02 23:38:59 +0000248 unsigned rt = Is64BitMode ? X86::reloc_pcrel_word : X86::reloc_picrel_word;
249 emitConstPoolAddress(RelocOp->getIndex(), rt,
Evan Chengf0123872008-01-03 02:56:28 +0000250 RelocOp->getOffset(), PCAdj);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000251 } else if (RelocOp->isJumpTableIndex()) {
Evan Cheng8c872652008-01-02 23:38:59 +0000252 unsigned rt = Is64BitMode ? X86::reloc_pcrel_word : X86::reloc_picrel_word;
Evan Chengf0123872008-01-03 02:56:28 +0000253 emitJumpTableAddress(RelocOp->getIndex(), rt, PCAdj);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000254 } else {
255 assert(0 && "Unknown value to relocate!");
256 }
257}
258
259void Emitter::emitMemModRMByte(const MachineInstr &MI,
260 unsigned Op, unsigned RegOpcodeField,
Evan Cheng8ee6bab2007-12-22 09:40:20 +0000261 intptr_t PCAdj) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000262 const MachineOperand &Op3 = MI.getOperand(Op+3);
263 int DispVal = 0;
264 const MachineOperand *DispForReloc = 0;
265
266 // Figure out what sort of displacement we have to handle here.
267 if (Op3.isGlobalAddress()) {
268 DispForReloc = &Op3;
269 } else if (Op3.isConstantPoolIndex()) {
Evan Cheng8c872652008-01-02 23:38:59 +0000270 if (Is64BitMode || IsPIC) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000271 DispForReloc = &Op3;
272 } else {
Chris Lattner6017d482007-12-30 23:10:15 +0000273 DispVal += MCE.getConstantPoolEntryAddress(Op3.getIndex());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000274 DispVal += Op3.getOffset();
275 }
276 } else if (Op3.isJumpTableIndex()) {
Evan Cheng8c872652008-01-02 23:38:59 +0000277 if (Is64BitMode || IsPIC) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000278 DispForReloc = &Op3;
279 } else {
Chris Lattner6017d482007-12-30 23:10:15 +0000280 DispVal += MCE.getJumpTableEntryAddress(Op3.getIndex());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000281 }
282 } else {
283 DispVal = Op3.getImm();
284 }
285
286 const MachineOperand &Base = MI.getOperand(Op);
287 const MachineOperand &Scale = MI.getOperand(Op+1);
288 const MachineOperand &IndexReg = MI.getOperand(Op+2);
289
290 unsigned BaseReg = Base.getReg();
291
292 // Is a SIB byte needed?
293 if (IndexReg.getReg() == 0 &&
294 (BaseReg == 0 || getX86RegNum(BaseReg) != N86::ESP)) {
295 if (BaseReg == 0) { // Just a displacement?
296 // Emit special case [disp32] encoding
297 MCE.emitByte(ModRMByte(0, RegOpcodeField, 5));
298
299 emitDisplacementField(DispForReloc, DispVal, PCAdj);
300 } else {
301 unsigned BaseRegNo = getX86RegNum(BaseReg);
302 if (!DispForReloc && DispVal == 0 && BaseRegNo != N86::EBP) {
303 // Emit simple indirect register encoding... [EAX] f.e.
304 MCE.emitByte(ModRMByte(0, RegOpcodeField, BaseRegNo));
305 } else if (!DispForReloc && isDisp8(DispVal)) {
306 // Emit the disp8 encoding... [REG+disp8]
307 MCE.emitByte(ModRMByte(1, RegOpcodeField, BaseRegNo));
308 emitConstant(DispVal, 1);
309 } else {
310 // Emit the most general non-SIB encoding: [REG+disp32]
311 MCE.emitByte(ModRMByte(2, RegOpcodeField, BaseRegNo));
312 emitDisplacementField(DispForReloc, DispVal, PCAdj);
313 }
314 }
315
316 } else { // We need a SIB byte, so start by outputting the ModR/M byte first
317 assert(IndexReg.getReg() != X86::ESP &&
318 IndexReg.getReg() != X86::RSP && "Cannot use ESP as index reg!");
319
320 bool ForceDisp32 = false;
321 bool ForceDisp8 = false;
322 if (BaseReg == 0) {
323 // If there is no base register, we emit the special case SIB byte with
324 // MOD=0, BASE=5, to JUST get the index, scale, and displacement.
325 MCE.emitByte(ModRMByte(0, RegOpcodeField, 4));
326 ForceDisp32 = true;
327 } else if (DispForReloc) {
328 // Emit the normal disp32 encoding.
329 MCE.emitByte(ModRMByte(2, RegOpcodeField, 4));
330 ForceDisp32 = true;
331 } else if (DispVal == 0 && getX86RegNum(BaseReg) != N86::EBP) {
332 // Emit no displacement ModR/M byte
333 MCE.emitByte(ModRMByte(0, RegOpcodeField, 4));
334 } else if (isDisp8(DispVal)) {
335 // Emit the disp8 encoding...
336 MCE.emitByte(ModRMByte(1, RegOpcodeField, 4));
337 ForceDisp8 = true; // Make sure to force 8 bit disp if Base=EBP
338 } else {
339 // Emit the normal disp32 encoding...
340 MCE.emitByte(ModRMByte(2, RegOpcodeField, 4));
341 }
342
343 // Calculate what the SS field value should be...
344 static const unsigned SSTable[] = { ~0, 0, 1, ~0, 2, ~0, ~0, ~0, 3 };
345 unsigned SS = SSTable[Scale.getImm()];
346
347 if (BaseReg == 0) {
348 // Handle the SIB byte for the case where there is no base. The
349 // displacement has already been output.
350 assert(IndexReg.getReg() && "Index register must be specified!");
351 emitSIBByte(SS, getX86RegNum(IndexReg.getReg()), 5);
352 } else {
353 unsigned BaseRegNo = getX86RegNum(BaseReg);
354 unsigned IndexRegNo;
355 if (IndexReg.getReg())
356 IndexRegNo = getX86RegNum(IndexReg.getReg());
357 else
358 IndexRegNo = 4; // For example [ESP+1*<noreg>+4]
359 emitSIBByte(SS, IndexRegNo, BaseRegNo);
360 }
361
362 // Do we need to output a displacement?
363 if (ForceDisp8) {
364 emitConstant(DispVal, 1);
365 } else if (DispVal != 0 || ForceDisp32) {
366 emitDisplacementField(DispForReloc, DispVal, PCAdj);
367 }
368 }
369}
370
371static unsigned sizeOfImm(const TargetInstrDescriptor *Desc) {
372 switch (Desc->TSFlags & X86II::ImmMask) {
373 case X86II::Imm8: return 1;
374 case X86II::Imm16: return 2;
375 case X86II::Imm32: return 4;
376 case X86II::Imm64: return 8;
377 default: assert(0 && "Immediate size not set!");
378 return 0;
379 }
380}
381
382/// isX86_64ExtendedReg - Is the MachineOperand a x86-64 extended register?
383/// e.g. r8, xmm8, etc.
384bool Emitter::isX86_64ExtendedReg(const MachineOperand &MO) {
385 if (!MO.isRegister()) return false;
Evan Chenge21ff432007-11-13 17:54:34 +0000386 switch (MO.getReg()) {
387 default: break;
388 case X86::R8: case X86::R9: case X86::R10: case X86::R11:
389 case X86::R12: case X86::R13: case X86::R14: case X86::R15:
390 case X86::R8D: case X86::R9D: case X86::R10D: case X86::R11D:
391 case X86::R12D: case X86::R13D: case X86::R14D: case X86::R15D:
392 case X86::R8W: case X86::R9W: case X86::R10W: case X86::R11W:
393 case X86::R12W: case X86::R13W: case X86::R14W: case X86::R15W:
394 case X86::R8B: case X86::R9B: case X86::R10B: case X86::R11B:
395 case X86::R12B: case X86::R13B: case X86::R14B: case X86::R15B:
396 case X86::XMM8: case X86::XMM9: case X86::XMM10: case X86::XMM11:
397 case X86::XMM12: case X86::XMM13: case X86::XMM14: case X86::XMM15:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000398 return true;
Evan Chenge21ff432007-11-13 17:54:34 +0000399 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000400 return false;
401}
402
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000403inline static bool isX86_64NonExtLowByteReg(unsigned reg) {
404 return (reg == X86::SPL || reg == X86::BPL ||
405 reg == X86::SIL || reg == X86::DIL);
406}
407
408/// determineREX - Determine if the MachineInstr has to be encoded with a X86-64
409/// REX prefix which specifies 1) 64-bit instructions, 2) non-default operand
410/// size, and 3) use of X86-64 extended registers.
411unsigned Emitter::determineREX(const MachineInstr &MI) {
412 unsigned REX = 0;
413 const TargetInstrDescriptor *Desc = MI.getInstrDescriptor();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000414
415 // Pseudo instructions do not need REX prefix byte.
416 if ((Desc->TSFlags & X86II::FormMask) == X86II::Pseudo)
417 return 0;
418 if (Desc->TSFlags & X86II::REX_W)
419 REX |= 1 << 3;
420
421 unsigned NumOps = Desc->numOperands;
422 if (NumOps) {
423 bool isTwoAddr = NumOps > 1 &&
424 Desc->getOperandConstraint(1, TOI::TIED_TO) != -1;
425
426 // If it accesses SPL, BPL, SIL, or DIL, then it requires a 0x40 REX prefix.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000427 unsigned i = isTwoAddr ? 1 : 0;
428 for (unsigned e = NumOps; i != e; ++i) {
429 const MachineOperand& MO = MI.getOperand(i);
430 if (MO.isRegister()) {
431 unsigned Reg = MO.getReg();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000432 if (isX86_64NonExtLowByteReg(Reg))
433 REX |= 0x40;
434 }
435 }
436
437 switch (Desc->TSFlags & X86II::FormMask) {
438 case X86II::MRMInitReg:
439 if (isX86_64ExtendedReg(MI.getOperand(0)))
440 REX |= (1 << 0) | (1 << 2);
441 break;
442 case X86II::MRMSrcReg: {
443 if (isX86_64ExtendedReg(MI.getOperand(0)))
444 REX |= 1 << 2;
445 i = isTwoAddr ? 2 : 1;
446 for (unsigned e = NumOps; i != e; ++i) {
447 const MachineOperand& MO = MI.getOperand(i);
448 if (isX86_64ExtendedReg(MO))
449 REX |= 1 << 0;
450 }
451 break;
452 }
453 case X86II::MRMSrcMem: {
454 if (isX86_64ExtendedReg(MI.getOperand(0)))
455 REX |= 1 << 2;
456 unsigned Bit = 0;
457 i = isTwoAddr ? 2 : 1;
458 for (; i != NumOps; ++i) {
459 const MachineOperand& MO = MI.getOperand(i);
460 if (MO.isRegister()) {
461 if (isX86_64ExtendedReg(MO))
462 REX |= 1 << Bit;
463 Bit++;
464 }
465 }
466 break;
467 }
468 case X86II::MRM0m: case X86II::MRM1m:
469 case X86II::MRM2m: case X86II::MRM3m:
470 case X86II::MRM4m: case X86II::MRM5m:
471 case X86II::MRM6m: case X86II::MRM7m:
472 case X86II::MRMDestMem: {
473 unsigned e = isTwoAddr ? 5 : 4;
474 i = isTwoAddr ? 1 : 0;
475 if (NumOps > e && isX86_64ExtendedReg(MI.getOperand(e)))
476 REX |= 1 << 2;
477 unsigned Bit = 0;
478 for (; i != e; ++i) {
479 const MachineOperand& MO = MI.getOperand(i);
480 if (MO.isRegister()) {
481 if (isX86_64ExtendedReg(MO))
482 REX |= 1 << Bit;
483 Bit++;
484 }
485 }
486 break;
487 }
488 default: {
489 if (isX86_64ExtendedReg(MI.getOperand(0)))
490 REX |= 1 << 0;
491 i = isTwoAddr ? 2 : 1;
492 for (unsigned e = NumOps; i != e; ++i) {
493 const MachineOperand& MO = MI.getOperand(i);
494 if (isX86_64ExtendedReg(MO))
495 REX |= 1 << 2;
496 }
497 break;
498 }
499 }
500 }
501 return REX;
502}
503
504void Emitter::emitInstruction(const MachineInstr &MI) {
505 NumEmitted++; // Keep track of the # of mi's emitted
506
507 const TargetInstrDescriptor *Desc = MI.getInstrDescriptor();
508 unsigned Opcode = Desc->Opcode;
509
510 // Emit the repeat opcode prefix as needed.
511 if ((Desc->TSFlags & X86II::Op0Mask) == X86II::REP) MCE.emitByte(0xF3);
512
513 // Emit the operand size opcode prefix as needed.
514 if (Desc->TSFlags & X86II::OpSize) MCE.emitByte(0x66);
515
516 // Emit the address size opcode prefix as needed.
517 if (Desc->TSFlags & X86II::AdSize) MCE.emitByte(0x67);
518
519 bool Need0FPrefix = false;
520 switch (Desc->TSFlags & X86II::Op0Mask) {
521 case X86II::TB:
522 Need0FPrefix = true; // Two-byte opcode prefix
523 break;
524 case X86II::T8:
525 MCE.emitByte(0x0F);
526 MCE.emitByte(0x38);
527 break;
528 case X86II::TA:
529 MCE.emitByte(0x0F);
530 MCE.emitByte(0x3A);
531 break;
532 case X86II::REP: break; // already handled.
533 case X86II::XS: // F3 0F
534 MCE.emitByte(0xF3);
535 Need0FPrefix = true;
536 break;
537 case X86II::XD: // F2 0F
538 MCE.emitByte(0xF2);
539 Need0FPrefix = true;
540 break;
541 case X86II::D8: case X86II::D9: case X86II::DA: case X86II::DB:
542 case X86II::DC: case X86II::DD: case X86II::DE: case X86II::DF:
543 MCE.emitByte(0xD8+
544 (((Desc->TSFlags & X86II::Op0Mask)-X86II::D8)
545 >> X86II::Op0Shift));
546 break; // Two-byte opcode prefix
547 default: assert(0 && "Invalid prefix!");
548 case 0: break; // No prefix!
549 }
550
551 if (Is64BitMode) {
552 // REX prefix
553 unsigned REX = determineREX(MI);
554 if (REX)
555 MCE.emitByte(0x40 | REX);
556 }
557
558 // 0x0F escape code must be emitted just before the opcode.
559 if (Need0FPrefix)
560 MCE.emitByte(0x0F);
561
562 // If this is a two-address instruction, skip one of the register operands.
563 unsigned NumOps = Desc->numOperands;
564 unsigned CurOp = 0;
565 if (NumOps > 1 && Desc->getOperandConstraint(1, TOI::TIED_TO) != -1)
566 CurOp++;
567
568 unsigned char BaseOpcode = II->getBaseOpcodeFor(Desc);
569 switch (Desc->TSFlags & X86II::FormMask) {
570 default: assert(0 && "Unknown FormMask value in X86 MachineCodeEmitter!");
571 case X86II::Pseudo:
572#ifndef NDEBUG
573 switch (Opcode) {
574 default:
575 assert(0 && "psuedo instructions should be removed before code emission");
576 case TargetInstrInfo::INLINEASM:
577 assert(0 && "JIT does not support inline asm!\n");
578 case TargetInstrInfo::LABEL:
579 assert(0 && "JIT does not support meta labels!\n");
580 case X86::IMPLICIT_USE:
581 case X86::IMPLICIT_DEF:
582 case X86::IMPLICIT_DEF_GR8:
583 case X86::IMPLICIT_DEF_GR16:
584 case X86::IMPLICIT_DEF_GR32:
585 case X86::IMPLICIT_DEF_GR64:
586 case X86::IMPLICIT_DEF_FR32:
587 case X86::IMPLICIT_DEF_FR64:
588 case X86::IMPLICIT_DEF_VR64:
589 case X86::IMPLICIT_DEF_VR128:
590 case X86::FP_REG_KILL:
591 break;
592 }
593#endif
594 CurOp = NumOps;
595 break;
596
597 case X86II::RawFrm:
598 MCE.emitByte(BaseOpcode);
599 if (CurOp != NumOps) {
600 const MachineOperand &MO = MI.getOperand(CurOp++);
601 if (MO.isMachineBasicBlock()) {
Chris Lattner6017d482007-12-30 23:10:15 +0000602 emitPCRelativeBlockAddress(MO.getMBB());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000603 } else if (MO.isGlobalAddress()) {
Evan Chengf0123872008-01-03 02:56:28 +0000604 bool NeedStub = !IsStatic ||
605 (Is64BitMode && TM.getCodeModel() == CodeModel::Large);
Evan Cheng8ee6bab2007-12-22 09:40:20 +0000606 emitGlobalAddress(MO.getGlobal(), X86::reloc_pcrel_word,
Evan Chengf0123872008-01-03 02:56:28 +0000607 0, 0, NeedStub);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000608 } else if (MO.isExternalSymbol()) {
Evan Chengf0123872008-01-03 02:56:28 +0000609 emitExternalSymbolAddress(MO.getSymbolName(), X86::reloc_pcrel_word);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000610 } else if (MO.isImmediate()) {
611 emitConstant(MO.getImm(), sizeOfImm(Desc));
612 } else {
613 assert(0 && "Unknown RawFrm operand!");
614 }
615 }
Evan Cheng8ee6bab2007-12-22 09:40:20 +0000616
617 // Remember the current PC offset, this is the PIC relocation
618 // base address.
619 if (Opcode == X86::MovePCtoStack)
620 PICBase = MCE.getCurrentPCOffset();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000621 break;
622
623 case X86II::AddRegFrm:
624 MCE.emitByte(BaseOpcode + getX86RegNum(MI.getOperand(CurOp++).getReg()));
625
626 if (CurOp != NumOps) {
627 const MachineOperand &MO1 = MI.getOperand(CurOp++);
628 unsigned Size = sizeOfImm(Desc);
629 if (MO1.isImmediate())
630 emitConstant(MO1.getImm(), Size);
631 else {
Evan Cheng8ee6bab2007-12-22 09:40:20 +0000632 unsigned rt = Is64BitMode ? X86::reloc_pcrel_word
633 : (IsPIC ? X86::reloc_picrel_word : X86::reloc_absolute_word);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000634 if (Opcode == X86::MOV64ri)
635 rt = X86::reloc_absolute_dword; // FIXME: add X86II flag?
Evan Chengf0123872008-01-03 02:56:28 +0000636 if (MO1.isGlobalAddress()) {
637 bool NeedStub = !IsStatic || isa<Function>(MO1.getGlobal());
638 emitGlobalAddress(MO1.getGlobal(), rt, MO1.getOffset(), 0, NeedStub);
639 } else if (MO1.isExternalSymbol())
640 emitExternalSymbolAddress(MO1.getSymbolName(), rt);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000641 else if (MO1.isConstantPoolIndex())
Evan Chengf0123872008-01-03 02:56:28 +0000642 emitConstPoolAddress(MO1.getIndex(), rt);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000643 else if (MO1.isJumpTableIndex())
Evan Chengf0123872008-01-03 02:56:28 +0000644 emitJumpTableAddress(MO1.getIndex(), rt);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000645 }
646 }
647 break;
648
649 case X86II::MRMDestReg: {
650 MCE.emitByte(BaseOpcode);
651 emitRegModRMByte(MI.getOperand(CurOp).getReg(),
652 getX86RegNum(MI.getOperand(CurOp+1).getReg()));
653 CurOp += 2;
654 if (CurOp != NumOps)
655 emitConstant(MI.getOperand(CurOp++).getImm(), sizeOfImm(Desc));
656 break;
657 }
658 case X86II::MRMDestMem: {
659 MCE.emitByte(BaseOpcode);
660 emitMemModRMByte(MI, CurOp, getX86RegNum(MI.getOperand(CurOp+4).getReg()));
661 CurOp += 5;
662 if (CurOp != NumOps)
663 emitConstant(MI.getOperand(CurOp++).getImm(), sizeOfImm(Desc));
664 break;
665 }
666
667 case X86II::MRMSrcReg:
668 MCE.emitByte(BaseOpcode);
669 emitRegModRMByte(MI.getOperand(CurOp+1).getReg(),
670 getX86RegNum(MI.getOperand(CurOp).getReg()));
671 CurOp += 2;
672 if (CurOp != NumOps)
673 emitConstant(MI.getOperand(CurOp++).getImm(), sizeOfImm(Desc));
674 break;
675
676 case X86II::MRMSrcMem: {
Evan Cheng8ee6bab2007-12-22 09:40:20 +0000677 intptr_t PCAdj = (CurOp+5 != NumOps) ? sizeOfImm(Desc) : 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000678
679 MCE.emitByte(BaseOpcode);
680 emitMemModRMByte(MI, CurOp+1, getX86RegNum(MI.getOperand(CurOp).getReg()),
681 PCAdj);
682 CurOp += 5;
683 if (CurOp != NumOps)
684 emitConstant(MI.getOperand(CurOp++).getImm(), sizeOfImm(Desc));
685 break;
686 }
687
688 case X86II::MRM0r: case X86II::MRM1r:
689 case X86II::MRM2r: case X86II::MRM3r:
690 case X86II::MRM4r: case X86II::MRM5r:
691 case X86II::MRM6r: case X86II::MRM7r:
692 MCE.emitByte(BaseOpcode);
693 emitRegModRMByte(MI.getOperand(CurOp++).getReg(),
694 (Desc->TSFlags & X86II::FormMask)-X86II::MRM0r);
695
696 if (CurOp != NumOps) {
697 const MachineOperand &MO1 = MI.getOperand(CurOp++);
698 unsigned Size = sizeOfImm(Desc);
699 if (MO1.isImmediate())
700 emitConstant(MO1.getImm(), Size);
701 else {
702 unsigned rt = Is64BitMode ? X86::reloc_pcrel_word
Evan Cheng8ee6bab2007-12-22 09:40:20 +0000703 : (IsPIC ? X86::reloc_picrel_word : X86::reloc_absolute_word);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000704 if (Opcode == X86::MOV64ri32)
705 rt = X86::reloc_absolute_word; // FIXME: add X86II flag?
Evan Chengf0123872008-01-03 02:56:28 +0000706 if (MO1.isGlobalAddress()) {
707 bool NeedStub = !IsStatic || isa<Function>(MO1.getGlobal());
708 emitGlobalAddress(MO1.getGlobal(), rt, MO1.getOffset(), 0, NeedStub);
709 } else if (MO1.isExternalSymbol())
710 emitExternalSymbolAddress(MO1.getSymbolName(), rt);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000711 else if (MO1.isConstantPoolIndex())
Evan Chengf0123872008-01-03 02:56:28 +0000712 emitConstPoolAddress(MO1.getIndex(), rt);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000713 else if (MO1.isJumpTableIndex())
Evan Chengf0123872008-01-03 02:56:28 +0000714 emitJumpTableAddress(MO1.getIndex(), rt);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000715 }
716 }
717 break;
718
719 case X86II::MRM0m: case X86II::MRM1m:
720 case X86II::MRM2m: case X86II::MRM3m:
721 case X86II::MRM4m: case X86II::MRM5m:
722 case X86II::MRM6m: case X86II::MRM7m: {
Evan Cheng8ee6bab2007-12-22 09:40:20 +0000723 intptr_t PCAdj = (CurOp+4 != NumOps) ?
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000724 (MI.getOperand(CurOp+4).isImmediate() ? sizeOfImm(Desc) : 4) : 0;
725
726 MCE.emitByte(BaseOpcode);
727 emitMemModRMByte(MI, CurOp, (Desc->TSFlags & X86II::FormMask)-X86II::MRM0m,
728 PCAdj);
729 CurOp += 4;
730
731 if (CurOp != NumOps) {
732 const MachineOperand &MO = MI.getOperand(CurOp++);
733 unsigned Size = sizeOfImm(Desc);
734 if (MO.isImmediate())
735 emitConstant(MO.getImm(), Size);
736 else {
737 unsigned rt = Is64BitMode ? X86::reloc_pcrel_word
Evan Cheng8ee6bab2007-12-22 09:40:20 +0000738 : (IsPIC ? X86::reloc_picrel_word : X86::reloc_absolute_word);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000739 if (Opcode == X86::MOV64mi32)
740 rt = X86::reloc_absolute_word; // FIXME: add X86II flag?
Evan Chengf0123872008-01-03 02:56:28 +0000741 if (MO.isGlobalAddress()) {
742 bool NeedStub = !IsStatic || isa<Function>(MO.getGlobal());
743 emitGlobalAddress(MO.getGlobal(), rt, MO.getOffset(), 0, NeedStub);
744 } else if (MO.isExternalSymbol())
745 emitExternalSymbolAddress(MO.getSymbolName(), rt);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000746 else if (MO.isConstantPoolIndex())
Evan Chengf0123872008-01-03 02:56:28 +0000747 emitConstPoolAddress(MO.getIndex(), rt);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000748 else if (MO.isJumpTableIndex())
Evan Chengf0123872008-01-03 02:56:28 +0000749 emitJumpTableAddress(MO.getIndex(), rt);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000750 }
751 }
752 break;
753 }
754
755 case X86II::MRMInitReg:
756 MCE.emitByte(BaseOpcode);
757 // Duplicate register, used by things like MOV8r0 (aka xor reg,reg).
758 emitRegModRMByte(MI.getOperand(CurOp).getReg(),
759 getX86RegNum(MI.getOperand(CurOp).getReg()));
760 ++CurOp;
761 break;
762 }
763
764 assert((Desc->Flags & M_VARIABLE_OPS) != 0 ||
765 CurOp == NumOps && "Unknown encoding!");
766}