blob: 0a7beb251a7dc6b3da5993ed38c32ad4105c9445 [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//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
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"
Duncan Sandsd8455ca2007-07-27 20:02:49 +000016#include "X86CodeEmitter.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000017#include "X86InstrInfo.h"
18#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"
26#include "llvm/CodeGen/Passes.h"
27#include "llvm/Function.h"
28#include "llvm/ADT/Statistic.h"
29#include "llvm/Support/Compiler.h"
30#include "llvm/Target/TargetOptions.h"
31using namespace llvm;
32
33STATISTIC(NumEmitted, "Number of machine instructions emitted");
34
35namespace {
36 class VISIBILITY_HIDDEN Emitter : public MachineFunctionPass {
37 const X86InstrInfo *II;
38 const TargetData *TD;
39 TargetMachine &TM;
40 MachineCodeEmitter &MCE;
41 bool Is64BitMode;
42 public:
43 static char ID;
44 explicit Emitter(TargetMachine &tm, MachineCodeEmitter &mce)
45 : MachineFunctionPass((intptr_t)&ID), II(0), TD(0), TM(tm),
46 MCE(mce), Is64BitMode(false) {}
47 Emitter(TargetMachine &tm, MachineCodeEmitter &mce,
48 const X86InstrInfo &ii, const TargetData &td, bool is64)
49 : MachineFunctionPass((intptr_t)&ID), II(&ii), TD(&td), TM(tm),
50 MCE(mce), Is64BitMode(is64) {}
51
52 bool runOnMachineFunction(MachineFunction &MF);
53
54 virtual const char *getPassName() const {
55 return "X86 Machine Code Emitter";
56 }
57
58 void emitInstruction(const MachineInstr &MI);
59
60 private:
61 void emitPCRelativeBlockAddress(MachineBasicBlock *MBB);
62 void emitPCRelativeValue(intptr_t Address);
63 void emitGlobalAddressForCall(GlobalValue *GV, bool DoesntNeedStub);
64 void emitGlobalAddressForPtr(GlobalValue *GV, unsigned Reloc,
65 int Disp = 0, unsigned PCAdj = 0);
66 void emitExternalSymbolAddress(const char *ES, unsigned Reloc);
67 void emitConstPoolAddress(unsigned CPI, unsigned Reloc, int Disp = 0,
68 unsigned PCAdj = 0);
69 void emitJumpTableAddress(unsigned JTI, unsigned Reloc, unsigned PCAdj = 0);
70
71 void emitDisplacementField(const MachineOperand *RelocOp, int DispVal,
72 unsigned PCAdj = 0);
73
74 void emitRegModRMByte(unsigned ModRMReg, unsigned RegOpcodeField);
75 void emitSIBByte(unsigned SS, unsigned Index, unsigned Base);
76 void emitConstant(uint64_t Val, unsigned Size);
77
78 void emitMemModRMByte(const MachineInstr &MI,
79 unsigned Op, unsigned RegOpcodeField,
80 unsigned PCAdj = 0);
81
82 unsigned getX86RegNum(unsigned RegNo);
83 bool isX86_64ExtendedReg(const MachineOperand &MO);
84 unsigned determineREX(const MachineInstr &MI);
85 };
86 char Emitter::ID = 0;
87}
88
89/// createX86CodeEmitterPass - Return a pass that emits the collected X86 code
90/// to the specified MCE object.
91FunctionPass *llvm::createX86CodeEmitterPass(X86TargetMachine &TM,
92 MachineCodeEmitter &MCE) {
93 return new Emitter(TM, MCE);
94}
95
96bool Emitter::runOnMachineFunction(MachineFunction &MF) {
97 assert((MF.getTarget().getRelocationModel() != Reloc::Default ||
98 MF.getTarget().getRelocationModel() != Reloc::Static) &&
99 "JIT relocation model must be set to static or default!");
100 II = ((X86TargetMachine&)MF.getTarget()).getInstrInfo();
101 TD = ((X86TargetMachine&)MF.getTarget()).getTargetData();
102 Is64BitMode =
103 ((X86TargetMachine&)MF.getTarget()).getSubtarget<X86Subtarget>().is64Bit();
104
105 do {
106 MCE.startFunction(MF);
107 for (MachineFunction::iterator MBB = MF.begin(), E = MF.end();
108 MBB != E; ++MBB) {
109 MCE.StartMachineBasicBlock(MBB);
110 for (MachineBasicBlock::const_iterator I = MBB->begin(), E = MBB->end();
111 I != E; ++I)
112 emitInstruction(*I);
113 }
114 } while (MCE.finishFunction(MF));
115
116 return false;
117}
118
119/// emitPCRelativeValue - Emit a PC relative address.
120///
121void Emitter::emitPCRelativeValue(intptr_t Address) {
122 MCE.emitWordLE(Address-MCE.getCurrentPCValue()-4);
123}
124
125/// 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
137/// emitGlobalAddressForCall - Emit the specified address to the code stream
138/// assuming this is part of a function call, which is PC relative.
139///
140void Emitter::emitGlobalAddressForCall(GlobalValue *GV, bool DoesntNeedStub) {
141 MCE.addRelocation(MachineRelocation::getGV(MCE.getCurrentPCOffset(),
142 X86::reloc_pcrel_word, GV, 0,
143 DoesntNeedStub));
144 MCE.emitWordLE(0);
145}
146
147/// emitGlobalAddress - Emit the specified address to the code stream assuming
148/// this is part of a "take the address of a global" instruction.
149///
150void Emitter::emitGlobalAddressForPtr(GlobalValue *GV, unsigned Reloc,
151 int Disp /* = 0 */,
152 unsigned PCAdj /* = 0 */) {
153 MCE.addRelocation(MachineRelocation::getGV(MCE.getCurrentPCOffset(), Reloc,
154 GV, PCAdj));
155 if (Reloc == X86::reloc_absolute_dword)
156 MCE.emitWordLE(0);
157 MCE.emitWordLE(Disp); // The relocated value will be added to the displacement
158}
159
160/// emitExternalSymbolAddress - Arrange for the address of an external symbol to
161/// be emitted to the current location in the function, and allow it to be PC
162/// relative.
163void Emitter::emitExternalSymbolAddress(const char *ES, unsigned Reloc) {
164 MCE.addRelocation(MachineRelocation::getExtSym(MCE.getCurrentPCOffset(),
165 Reloc, ES));
166 if (Reloc == X86::reloc_absolute_dword)
167 MCE.emitWordLE(0);
168 MCE.emitWordLE(0);
169}
170
171/// emitConstPoolAddress - Arrange for the address of an constant pool
172/// to be emitted to the current location in the function, and allow it to be PC
173/// relative.
174void Emitter::emitConstPoolAddress(unsigned CPI, unsigned Reloc,
175 int Disp /* = 0 */,
176 unsigned PCAdj /* = 0 */) {
177 MCE.addRelocation(MachineRelocation::getConstPool(MCE.getCurrentPCOffset(),
178 Reloc, CPI, PCAdj));
179 if (Reloc == X86::reloc_absolute_dword)
180 MCE.emitWordLE(0);
181 MCE.emitWordLE(Disp); // The relocated value will be added to the displacement
182}
183
184/// emitJumpTableAddress - Arrange for the address of a jump table to
185/// be emitted to the current location in the function, and allow it to be PC
186/// relative.
187void Emitter::emitJumpTableAddress(unsigned JTI, unsigned Reloc,
188 unsigned PCAdj /* = 0 */) {
189 MCE.addRelocation(MachineRelocation::getJumpTable(MCE.getCurrentPCOffset(),
190 Reloc, JTI, PCAdj));
191 if (Reloc == X86::reloc_absolute_dword)
192 MCE.emitWordLE(0);
193 MCE.emitWordLE(0); // The relocated value will be added to the displacement
194}
195
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000196// getX86RegNum - This function maps LLVM register identifiers to their X86
197// specific numbering, which is used in various places encoding instructions.
198//
199unsigned Emitter::getX86RegNum(unsigned RegNo) {
200 switch(RegNo) {
201 case X86::RAX: case X86::EAX: case X86::AX: case X86::AL: return N86::EAX;
202 case X86::RCX: case X86::ECX: case X86::CX: case X86::CL: return N86::ECX;
203 case X86::RDX: case X86::EDX: case X86::DX: case X86::DL: return N86::EDX;
204 case X86::RBX: case X86::EBX: case X86::BX: case X86::BL: return N86::EBX;
205 case X86::RSP: case X86::ESP: case X86::SP: case X86::SPL: case X86::AH:
206 return N86::ESP;
207 case X86::RBP: case X86::EBP: case X86::BP: case X86::BPL: case X86::CH:
208 return N86::EBP;
209 case X86::RSI: case X86::ESI: case X86::SI: case X86::SIL: case X86::DH:
210 return N86::ESI;
211 case X86::RDI: case X86::EDI: case X86::DI: case X86::DIL: case X86::BH:
212 return N86::EDI;
213
214 case X86::R8: case X86::R8D: case X86::R8W: case X86::R8B:
215 return N86::EAX;
216 case X86::R9: case X86::R9D: case X86::R9W: case X86::R9B:
217 return N86::ECX;
218 case X86::R10: case X86::R10D: case X86::R10W: case X86::R10B:
219 return N86::EDX;
220 case X86::R11: case X86::R11D: case X86::R11W: case X86::R11B:
221 return N86::EBX;
222 case X86::R12: case X86::R12D: case X86::R12W: case X86::R12B:
223 return N86::ESP;
224 case X86::R13: case X86::R13D: case X86::R13W: case X86::R13B:
225 return N86::EBP;
226 case X86::R14: case X86::R14D: case X86::R14W: case X86::R14B:
227 return N86::ESI;
228 case X86::R15: case X86::R15D: case X86::R15W: case X86::R15B:
229 return N86::EDI;
230
231 case X86::ST0: case X86::ST1: case X86::ST2: case X86::ST3:
232 case X86::ST4: case X86::ST5: case X86::ST6: case X86::ST7:
233 return RegNo-X86::ST0;
234
235 case X86::XMM0: case X86::XMM1: case X86::XMM2: case X86::XMM3:
236 case X86::XMM4: case X86::XMM5: case X86::XMM6: case X86::XMM7:
237 return II->getRegisterInfo().getDwarfRegNum(RegNo) -
238 II->getRegisterInfo().getDwarfRegNum(X86::XMM0);
239 case X86::XMM8: case X86::XMM9: case X86::XMM10: case X86::XMM11:
240 case X86::XMM12: case X86::XMM13: case X86::XMM14: case X86::XMM15:
241 return II->getRegisterInfo().getDwarfRegNum(RegNo) -
242 II->getRegisterInfo().getDwarfRegNum(X86::XMM8);
243
244 default:
245 assert(MRegisterInfo::isVirtualRegister(RegNo) &&
246 "Unknown physical register!");
247 assert(0 && "Register allocator hasn't allocated reg correctly yet!");
248 return 0;
249 }
250}
251
252inline static unsigned char ModRMByte(unsigned Mod, unsigned RegOpcode,
253 unsigned RM) {
254 assert(Mod < 4 && RegOpcode < 8 && RM < 8 && "ModRM Fields out of range!");
255 return RM | (RegOpcode << 3) | (Mod << 6);
256}
257
258void Emitter::emitRegModRMByte(unsigned ModRMReg, unsigned RegOpcodeFld){
259 MCE.emitByte(ModRMByte(3, RegOpcodeFld, getX86RegNum(ModRMReg)));
260}
261
262void Emitter::emitSIBByte(unsigned SS, unsigned Index, unsigned Base) {
263 // SIB byte is in the same format as the ModRMByte...
264 MCE.emitByte(ModRMByte(SS, Index, Base));
265}
266
267void Emitter::emitConstant(uint64_t Val, unsigned Size) {
268 // Output the constant in little endian byte order...
269 for (unsigned i = 0; i != Size; ++i) {
270 MCE.emitByte(Val & 255);
271 Val >>= 8;
272 }
273}
274
275/// isDisp8 - Return true if this signed displacement fits in a 8-bit
276/// sign-extended field.
277static bool isDisp8(int Value) {
278 return Value == (signed char)Value;
279}
280
281void Emitter::emitDisplacementField(const MachineOperand *RelocOp,
282 int DispVal, unsigned PCAdj) {
283 // If this is a simple integer displacement that doesn't require a relocation,
284 // emit it now.
285 if (!RelocOp) {
286 emitConstant(DispVal, 4);
287 return;
288 }
289
290 // Otherwise, this is something that requires a relocation. Emit it as such
291 // now.
292 if (RelocOp->isGlobalAddress()) {
293 // In 64-bit static small code model, we could potentially emit absolute.
294 // But it's probably not beneficial.
295 // 89 05 00 00 00 00 mov %eax,0(%rip) # PC-relative
296 // 89 04 25 00 00 00 00 mov %eax,0x0 # Absolute
297 unsigned rt= Is64BitMode ? X86::reloc_pcrel_word : X86::reloc_absolute_word;
298 emitGlobalAddressForPtr(RelocOp->getGlobal(), rt,
299 RelocOp->getOffset(), PCAdj);
300 } else if (RelocOp->isConstantPoolIndex()) {
301 // Must be in 64-bit mode.
302 emitConstPoolAddress(RelocOp->getConstantPoolIndex(), X86::reloc_pcrel_word,
303 RelocOp->getOffset(), PCAdj);
304 } else if (RelocOp->isJumpTableIndex()) {
305 // Must be in 64-bit mode.
306 emitJumpTableAddress(RelocOp->getJumpTableIndex(), X86::reloc_pcrel_word,
307 PCAdj);
308 } else {
309 assert(0 && "Unknown value to relocate!");
310 }
311}
312
313void Emitter::emitMemModRMByte(const MachineInstr &MI,
314 unsigned Op, unsigned RegOpcodeField,
315 unsigned PCAdj) {
316 const MachineOperand &Op3 = MI.getOperand(Op+3);
317 int DispVal = 0;
318 const MachineOperand *DispForReloc = 0;
319
320 // Figure out what sort of displacement we have to handle here.
321 if (Op3.isGlobalAddress()) {
322 DispForReloc = &Op3;
323 } else if (Op3.isConstantPoolIndex()) {
324 if (Is64BitMode) {
325 DispForReloc = &Op3;
326 } else {
327 DispVal += MCE.getConstantPoolEntryAddress(Op3.getConstantPoolIndex());
328 DispVal += Op3.getOffset();
329 }
330 } else if (Op3.isJumpTableIndex()) {
331 if (Is64BitMode) {
332 DispForReloc = &Op3;
333 } else {
334 DispVal += MCE.getJumpTableEntryAddress(Op3.getJumpTableIndex());
335 }
336 } else {
337 DispVal = Op3.getImm();
338 }
339
340 const MachineOperand &Base = MI.getOperand(Op);
341 const MachineOperand &Scale = MI.getOperand(Op+1);
342 const MachineOperand &IndexReg = MI.getOperand(Op+2);
343
344 unsigned BaseReg = Base.getReg();
345
346 // Is a SIB byte needed?
347 if (IndexReg.getReg() == 0 &&
348 (BaseReg == 0 || getX86RegNum(BaseReg) != N86::ESP)) {
349 if (BaseReg == 0) { // Just a displacement?
350 // Emit special case [disp32] encoding
351 MCE.emitByte(ModRMByte(0, RegOpcodeField, 5));
352
353 emitDisplacementField(DispForReloc, DispVal, PCAdj);
354 } else {
355 unsigned BaseRegNo = getX86RegNum(BaseReg);
356 if (!DispForReloc && DispVal == 0 && BaseRegNo != N86::EBP) {
357 // Emit simple indirect register encoding... [EAX] f.e.
358 MCE.emitByte(ModRMByte(0, RegOpcodeField, BaseRegNo));
359 } else if (!DispForReloc && isDisp8(DispVal)) {
360 // Emit the disp8 encoding... [REG+disp8]
361 MCE.emitByte(ModRMByte(1, RegOpcodeField, BaseRegNo));
362 emitConstant(DispVal, 1);
363 } else {
364 // Emit the most general non-SIB encoding: [REG+disp32]
365 MCE.emitByte(ModRMByte(2, RegOpcodeField, BaseRegNo));
366 emitDisplacementField(DispForReloc, DispVal, PCAdj);
367 }
368 }
369
370 } else { // We need a SIB byte, so start by outputting the ModR/M byte first
371 assert(IndexReg.getReg() != X86::ESP &&
372 IndexReg.getReg() != X86::RSP && "Cannot use ESP as index reg!");
373
374 bool ForceDisp32 = false;
375 bool ForceDisp8 = false;
376 if (BaseReg == 0) {
377 // If there is no base register, we emit the special case SIB byte with
378 // MOD=0, BASE=5, to JUST get the index, scale, and displacement.
379 MCE.emitByte(ModRMByte(0, RegOpcodeField, 4));
380 ForceDisp32 = true;
381 } else if (DispForReloc) {
382 // Emit the normal disp32 encoding.
383 MCE.emitByte(ModRMByte(2, RegOpcodeField, 4));
384 ForceDisp32 = true;
385 } else if (DispVal == 0 && getX86RegNum(BaseReg) != N86::EBP) {
386 // Emit no displacement ModR/M byte
387 MCE.emitByte(ModRMByte(0, RegOpcodeField, 4));
388 } else if (isDisp8(DispVal)) {
389 // Emit the disp8 encoding...
390 MCE.emitByte(ModRMByte(1, RegOpcodeField, 4));
391 ForceDisp8 = true; // Make sure to force 8 bit disp if Base=EBP
392 } else {
393 // Emit the normal disp32 encoding...
394 MCE.emitByte(ModRMByte(2, RegOpcodeField, 4));
395 }
396
397 // Calculate what the SS field value should be...
398 static const unsigned SSTable[] = { ~0, 0, 1, ~0, 2, ~0, ~0, ~0, 3 };
399 unsigned SS = SSTable[Scale.getImm()];
400
401 if (BaseReg == 0) {
402 // Handle the SIB byte for the case where there is no base. The
403 // displacement has already been output.
404 assert(IndexReg.getReg() && "Index register must be specified!");
405 emitSIBByte(SS, getX86RegNum(IndexReg.getReg()), 5);
406 } else {
407 unsigned BaseRegNo = getX86RegNum(BaseReg);
408 unsigned IndexRegNo;
409 if (IndexReg.getReg())
410 IndexRegNo = getX86RegNum(IndexReg.getReg());
411 else
412 IndexRegNo = 4; // For example [ESP+1*<noreg>+4]
413 emitSIBByte(SS, IndexRegNo, BaseRegNo);
414 }
415
416 // Do we need to output a displacement?
417 if (ForceDisp8) {
418 emitConstant(DispVal, 1);
419 } else if (DispVal != 0 || ForceDisp32) {
420 emitDisplacementField(DispForReloc, DispVal, PCAdj);
421 }
422 }
423}
424
425static unsigned sizeOfImm(const TargetInstrDescriptor *Desc) {
426 switch (Desc->TSFlags & X86II::ImmMask) {
427 case X86II::Imm8: return 1;
428 case X86II::Imm16: return 2;
429 case X86II::Imm32: return 4;
430 case X86II::Imm64: return 8;
431 default: assert(0 && "Immediate size not set!");
432 return 0;
433 }
434}
435
436/// isX86_64ExtendedReg - Is the MachineOperand a x86-64 extended register?
437/// e.g. r8, xmm8, etc.
438bool Emitter::isX86_64ExtendedReg(const MachineOperand &MO) {
439 if (!MO.isRegister()) return false;
440 unsigned RegNo = MO.getReg();
441 int DWNum = II->getRegisterInfo().getDwarfRegNum(RegNo);
442 if (DWNum >= II->getRegisterInfo().getDwarfRegNum(X86::R8) &&
443 DWNum <= II->getRegisterInfo().getDwarfRegNum(X86::R15))
444 return true;
445 if (DWNum >= II->getRegisterInfo().getDwarfRegNum(X86::XMM8) &&
446 DWNum <= II->getRegisterInfo().getDwarfRegNum(X86::XMM15))
447 return true;
448 return false;
449}
450
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000451inline static bool isX86_64NonExtLowByteReg(unsigned reg) {
452 return (reg == X86::SPL || reg == X86::BPL ||
453 reg == X86::SIL || reg == X86::DIL);
454}
455
456/// determineREX - Determine if the MachineInstr has to be encoded with a X86-64
457/// REX prefix which specifies 1) 64-bit instructions, 2) non-default operand
458/// size, and 3) use of X86-64 extended registers.
459unsigned Emitter::determineREX(const MachineInstr &MI) {
460 unsigned REX = 0;
461 const TargetInstrDescriptor *Desc = MI.getInstrDescriptor();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000462
463 // Pseudo instructions do not need REX prefix byte.
464 if ((Desc->TSFlags & X86II::FormMask) == X86II::Pseudo)
465 return 0;
466 if (Desc->TSFlags & X86II::REX_W)
467 REX |= 1 << 3;
468
469 unsigned NumOps = Desc->numOperands;
470 if (NumOps) {
471 bool isTwoAddr = NumOps > 1 &&
472 Desc->getOperandConstraint(1, TOI::TIED_TO) != -1;
473
474 // If it accesses SPL, BPL, SIL, or DIL, then it requires a 0x40 REX prefix.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000475 unsigned i = isTwoAddr ? 1 : 0;
476 for (unsigned e = NumOps; i != e; ++i) {
477 const MachineOperand& MO = MI.getOperand(i);
478 if (MO.isRegister()) {
479 unsigned Reg = MO.getReg();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000480 if (isX86_64NonExtLowByteReg(Reg))
481 REX |= 0x40;
482 }
483 }
484
485 switch (Desc->TSFlags & X86II::FormMask) {
486 case X86II::MRMInitReg:
487 if (isX86_64ExtendedReg(MI.getOperand(0)))
488 REX |= (1 << 0) | (1 << 2);
489 break;
490 case X86II::MRMSrcReg: {
491 if (isX86_64ExtendedReg(MI.getOperand(0)))
492 REX |= 1 << 2;
493 i = isTwoAddr ? 2 : 1;
494 for (unsigned e = NumOps; i != e; ++i) {
495 const MachineOperand& MO = MI.getOperand(i);
496 if (isX86_64ExtendedReg(MO))
497 REX |= 1 << 0;
498 }
499 break;
500 }
501 case X86II::MRMSrcMem: {
502 if (isX86_64ExtendedReg(MI.getOperand(0)))
503 REX |= 1 << 2;
504 unsigned Bit = 0;
505 i = isTwoAddr ? 2 : 1;
506 for (; i != NumOps; ++i) {
507 const MachineOperand& MO = MI.getOperand(i);
508 if (MO.isRegister()) {
509 if (isX86_64ExtendedReg(MO))
510 REX |= 1 << Bit;
511 Bit++;
512 }
513 }
514 break;
515 }
516 case X86II::MRM0m: case X86II::MRM1m:
517 case X86II::MRM2m: case X86II::MRM3m:
518 case X86II::MRM4m: case X86II::MRM5m:
519 case X86II::MRM6m: case X86II::MRM7m:
520 case X86II::MRMDestMem: {
521 unsigned e = isTwoAddr ? 5 : 4;
522 i = isTwoAddr ? 1 : 0;
523 if (NumOps > e && isX86_64ExtendedReg(MI.getOperand(e)))
524 REX |= 1 << 2;
525 unsigned Bit = 0;
526 for (; i != e; ++i) {
527 const MachineOperand& MO = MI.getOperand(i);
528 if (MO.isRegister()) {
529 if (isX86_64ExtendedReg(MO))
530 REX |= 1 << Bit;
531 Bit++;
532 }
533 }
534 break;
535 }
536 default: {
537 if (isX86_64ExtendedReg(MI.getOperand(0)))
538 REX |= 1 << 0;
539 i = isTwoAddr ? 2 : 1;
540 for (unsigned e = NumOps; i != e; ++i) {
541 const MachineOperand& MO = MI.getOperand(i);
542 if (isX86_64ExtendedReg(MO))
543 REX |= 1 << 2;
544 }
545 break;
546 }
547 }
548 }
549 return REX;
550}
551
552void Emitter::emitInstruction(const MachineInstr &MI) {
553 NumEmitted++; // Keep track of the # of mi's emitted
554
555 const TargetInstrDescriptor *Desc = MI.getInstrDescriptor();
556 unsigned Opcode = Desc->Opcode;
557
558 // Emit the repeat opcode prefix as needed.
559 if ((Desc->TSFlags & X86II::Op0Mask) == X86II::REP) MCE.emitByte(0xF3);
560
561 // Emit the operand size opcode prefix as needed.
562 if (Desc->TSFlags & X86II::OpSize) MCE.emitByte(0x66);
563
564 // Emit the address size opcode prefix as needed.
565 if (Desc->TSFlags & X86II::AdSize) MCE.emitByte(0x67);
566
567 bool Need0FPrefix = false;
568 switch (Desc->TSFlags & X86II::Op0Mask) {
569 case X86II::TB:
570 Need0FPrefix = true; // Two-byte opcode prefix
571 break;
572 case X86II::T8:
573 MCE.emitByte(0x0F);
574 MCE.emitByte(0x38);
575 break;
576 case X86II::TA:
577 MCE.emitByte(0x0F);
578 MCE.emitByte(0x3A);
579 break;
580 case X86II::REP: break; // already handled.
581 case X86II::XS: // F3 0F
582 MCE.emitByte(0xF3);
583 Need0FPrefix = true;
584 break;
585 case X86II::XD: // F2 0F
586 MCE.emitByte(0xF2);
587 Need0FPrefix = true;
588 break;
589 case X86II::D8: case X86II::D9: case X86II::DA: case X86II::DB:
590 case X86II::DC: case X86II::DD: case X86II::DE: case X86II::DF:
591 MCE.emitByte(0xD8+
592 (((Desc->TSFlags & X86II::Op0Mask)-X86II::D8)
593 >> X86II::Op0Shift));
594 break; // Two-byte opcode prefix
595 default: assert(0 && "Invalid prefix!");
596 case 0: break; // No prefix!
597 }
598
599 if (Is64BitMode) {
600 // REX prefix
601 unsigned REX = determineREX(MI);
602 if (REX)
603 MCE.emitByte(0x40 | REX);
604 }
605
606 // 0x0F escape code must be emitted just before the opcode.
607 if (Need0FPrefix)
608 MCE.emitByte(0x0F);
609
610 // If this is a two-address instruction, skip one of the register operands.
611 unsigned NumOps = Desc->numOperands;
612 unsigned CurOp = 0;
613 if (NumOps > 1 && Desc->getOperandConstraint(1, TOI::TIED_TO) != -1)
614 CurOp++;
615
616 unsigned char BaseOpcode = II->getBaseOpcodeFor(Desc);
617 switch (Desc->TSFlags & X86II::FormMask) {
618 default: assert(0 && "Unknown FormMask value in X86 MachineCodeEmitter!");
619 case X86II::Pseudo:
620#ifndef NDEBUG
621 switch (Opcode) {
622 default:
623 assert(0 && "psuedo instructions should be removed before code emission");
624 case TargetInstrInfo::INLINEASM:
625 assert(0 && "JIT does not support inline asm!\n");
626 case TargetInstrInfo::LABEL:
627 assert(0 && "JIT does not support meta labels!\n");
628 case X86::IMPLICIT_USE:
629 case X86::IMPLICIT_DEF:
630 case X86::IMPLICIT_DEF_GR8:
631 case X86::IMPLICIT_DEF_GR16:
632 case X86::IMPLICIT_DEF_GR32:
633 case X86::IMPLICIT_DEF_GR64:
634 case X86::IMPLICIT_DEF_FR32:
635 case X86::IMPLICIT_DEF_FR64:
636 case X86::IMPLICIT_DEF_VR64:
637 case X86::IMPLICIT_DEF_VR128:
638 case X86::FP_REG_KILL:
639 break;
640 }
641#endif
642 CurOp = NumOps;
643 break;
644
645 case X86II::RawFrm:
646 MCE.emitByte(BaseOpcode);
647 if (CurOp != NumOps) {
648 const MachineOperand &MO = MI.getOperand(CurOp++);
649 if (MO.isMachineBasicBlock()) {
650 emitPCRelativeBlockAddress(MO.getMachineBasicBlock());
651 } else if (MO.isGlobalAddress()) {
652 bool NeedStub = Is64BitMode ||
653 Opcode == X86::TAILJMPd ||
654 Opcode == X86::TAILJMPr || Opcode == X86::TAILJMPm;
655 emitGlobalAddressForCall(MO.getGlobal(), !NeedStub);
656 } else if (MO.isExternalSymbol()) {
657 emitExternalSymbolAddress(MO.getSymbolName(), X86::reloc_pcrel_word);
658 } else if (MO.isImmediate()) {
659 emitConstant(MO.getImm(), sizeOfImm(Desc));
660 } else {
661 assert(0 && "Unknown RawFrm operand!");
662 }
663 }
664 break;
665
666 case X86II::AddRegFrm:
667 MCE.emitByte(BaseOpcode + getX86RegNum(MI.getOperand(CurOp++).getReg()));
668
669 if (CurOp != NumOps) {
670 const MachineOperand &MO1 = MI.getOperand(CurOp++);
671 unsigned Size = sizeOfImm(Desc);
672 if (MO1.isImmediate())
673 emitConstant(MO1.getImm(), Size);
674 else {
675 unsigned rt = Is64BitMode ? X86::reloc_pcrel_word : X86::reloc_absolute_word;
676 if (Opcode == X86::MOV64ri)
677 rt = X86::reloc_absolute_dword; // FIXME: add X86II flag?
678 if (MO1.isGlobalAddress())
679 emitGlobalAddressForPtr(MO1.getGlobal(), rt, MO1.getOffset());
680 else if (MO1.isExternalSymbol())
681 emitExternalSymbolAddress(MO1.getSymbolName(), rt);
682 else if (MO1.isConstantPoolIndex())
683 emitConstPoolAddress(MO1.getConstantPoolIndex(), rt);
684 else if (MO1.isJumpTableIndex())
685 emitJumpTableAddress(MO1.getJumpTableIndex(), rt);
686 }
687 }
688 break;
689
690 case X86II::MRMDestReg: {
691 MCE.emitByte(BaseOpcode);
692 emitRegModRMByte(MI.getOperand(CurOp).getReg(),
693 getX86RegNum(MI.getOperand(CurOp+1).getReg()));
694 CurOp += 2;
695 if (CurOp != NumOps)
696 emitConstant(MI.getOperand(CurOp++).getImm(), sizeOfImm(Desc));
697 break;
698 }
699 case X86II::MRMDestMem: {
700 MCE.emitByte(BaseOpcode);
701 emitMemModRMByte(MI, CurOp, getX86RegNum(MI.getOperand(CurOp+4).getReg()));
702 CurOp += 5;
703 if (CurOp != NumOps)
704 emitConstant(MI.getOperand(CurOp++).getImm(), sizeOfImm(Desc));
705 break;
706 }
707
708 case X86II::MRMSrcReg:
709 MCE.emitByte(BaseOpcode);
710 emitRegModRMByte(MI.getOperand(CurOp+1).getReg(),
711 getX86RegNum(MI.getOperand(CurOp).getReg()));
712 CurOp += 2;
713 if (CurOp != NumOps)
714 emitConstant(MI.getOperand(CurOp++).getImm(), sizeOfImm(Desc));
715 break;
716
717 case X86II::MRMSrcMem: {
718 unsigned PCAdj = (CurOp+5 != NumOps) ? sizeOfImm(Desc) : 0;
719
720 MCE.emitByte(BaseOpcode);
721 emitMemModRMByte(MI, CurOp+1, getX86RegNum(MI.getOperand(CurOp).getReg()),
722 PCAdj);
723 CurOp += 5;
724 if (CurOp != NumOps)
725 emitConstant(MI.getOperand(CurOp++).getImm(), sizeOfImm(Desc));
726 break;
727 }
728
729 case X86II::MRM0r: case X86II::MRM1r:
730 case X86II::MRM2r: case X86II::MRM3r:
731 case X86II::MRM4r: case X86II::MRM5r:
732 case X86II::MRM6r: case X86II::MRM7r:
733 MCE.emitByte(BaseOpcode);
734 emitRegModRMByte(MI.getOperand(CurOp++).getReg(),
735 (Desc->TSFlags & X86II::FormMask)-X86II::MRM0r);
736
737 if (CurOp != NumOps) {
738 const MachineOperand &MO1 = MI.getOperand(CurOp++);
739 unsigned Size = sizeOfImm(Desc);
740 if (MO1.isImmediate())
741 emitConstant(MO1.getImm(), Size);
742 else {
743 unsigned rt = Is64BitMode ? X86::reloc_pcrel_word
744 : X86::reloc_absolute_word;
745 if (Opcode == X86::MOV64ri32)
746 rt = X86::reloc_absolute_word; // FIXME: add X86II flag?
747 if (MO1.isGlobalAddress())
748 emitGlobalAddressForPtr(MO1.getGlobal(), rt, MO1.getOffset());
749 else if (MO1.isExternalSymbol())
750 emitExternalSymbolAddress(MO1.getSymbolName(), rt);
751 else if (MO1.isConstantPoolIndex())
752 emitConstPoolAddress(MO1.getConstantPoolIndex(), rt);
753 else if (MO1.isJumpTableIndex())
754 emitJumpTableAddress(MO1.getJumpTableIndex(), rt);
755 }
756 }
757 break;
758
759 case X86II::MRM0m: case X86II::MRM1m:
760 case X86II::MRM2m: case X86II::MRM3m:
761 case X86II::MRM4m: case X86II::MRM5m:
762 case X86II::MRM6m: case X86II::MRM7m: {
763 unsigned PCAdj = (CurOp+4 != NumOps) ?
764 (MI.getOperand(CurOp+4).isImmediate() ? sizeOfImm(Desc) : 4) : 0;
765
766 MCE.emitByte(BaseOpcode);
767 emitMemModRMByte(MI, CurOp, (Desc->TSFlags & X86II::FormMask)-X86II::MRM0m,
768 PCAdj);
769 CurOp += 4;
770
771 if (CurOp != NumOps) {
772 const MachineOperand &MO = MI.getOperand(CurOp++);
773 unsigned Size = sizeOfImm(Desc);
774 if (MO.isImmediate())
775 emitConstant(MO.getImm(), Size);
776 else {
777 unsigned rt = Is64BitMode ? X86::reloc_pcrel_word
778 : X86::reloc_absolute_word;
779 if (Opcode == X86::MOV64mi32)
780 rt = X86::reloc_absolute_word; // FIXME: add X86II flag?
781 if (MO.isGlobalAddress())
782 emitGlobalAddressForPtr(MO.getGlobal(), rt, MO.getOffset());
783 else if (MO.isExternalSymbol())
784 emitExternalSymbolAddress(MO.getSymbolName(), rt);
785 else if (MO.isConstantPoolIndex())
786 emitConstPoolAddress(MO.getConstantPoolIndex(), rt);
787 else if (MO.isJumpTableIndex())
788 emitJumpTableAddress(MO.getJumpTableIndex(), rt);
789 }
790 }
791 break;
792 }
793
794 case X86II::MRMInitReg:
795 MCE.emitByte(BaseOpcode);
796 // Duplicate register, used by things like MOV8r0 (aka xor reg,reg).
797 emitRegModRMByte(MI.getOperand(CurOp).getReg(),
798 getX86RegNum(MI.getOperand(CurOp).getReg()));
799 ++CurOp;
800 break;
801 }
802
803 assert((Desc->Flags & M_VARIABLE_OPS) != 0 ||
804 CurOp == NumOps && "Unknown encoding!");
805}