blob: 9d5180452bc11ae27c1d695d674b2ca8bd6e880e [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
451inline static bool isX86_64TruncToByte(unsigned oc) {
452 return (oc == X86::TRUNC_64to8 || oc == X86::TRUNC_32to8 ||
453 oc == X86::TRUNC_16to8);
454}
455
456
457inline static bool isX86_64NonExtLowByteReg(unsigned reg) {
458 return (reg == X86::SPL || reg == X86::BPL ||
459 reg == X86::SIL || reg == X86::DIL);
460}
461
462/// determineREX - Determine if the MachineInstr has to be encoded with a X86-64
463/// REX prefix which specifies 1) 64-bit instructions, 2) non-default operand
464/// size, and 3) use of X86-64 extended registers.
465unsigned Emitter::determineREX(const MachineInstr &MI) {
466 unsigned REX = 0;
467 const TargetInstrDescriptor *Desc = MI.getInstrDescriptor();
468 unsigned Opcode = Desc->Opcode;
469
470 // Pseudo instructions do not need REX prefix byte.
471 if ((Desc->TSFlags & X86II::FormMask) == X86II::Pseudo)
472 return 0;
473 if (Desc->TSFlags & X86II::REX_W)
474 REX |= 1 << 3;
475
476 unsigned NumOps = Desc->numOperands;
477 if (NumOps) {
478 bool isTwoAddr = NumOps > 1 &&
479 Desc->getOperandConstraint(1, TOI::TIED_TO) != -1;
480
481 // If it accesses SPL, BPL, SIL, or DIL, then it requires a 0x40 REX prefix.
482 bool isTrunc8 = isX86_64TruncToByte(Opcode);
483 unsigned i = isTwoAddr ? 1 : 0;
484 for (unsigned e = NumOps; i != e; ++i) {
485 const MachineOperand& MO = MI.getOperand(i);
486 if (MO.isRegister()) {
487 unsigned Reg = MO.getReg();
488 // Trunc to byte are actually movb. The real source operand is the low
489 // byte of the register.
490 if (isTrunc8 && i == 1)
491 Reg = getX86SubSuperRegister(Reg, MVT::i8);
492 if (isX86_64NonExtLowByteReg(Reg))
493 REX |= 0x40;
494 }
495 }
496
497 switch (Desc->TSFlags & X86II::FormMask) {
498 case X86II::MRMInitReg:
499 if (isX86_64ExtendedReg(MI.getOperand(0)))
500 REX |= (1 << 0) | (1 << 2);
501 break;
502 case X86II::MRMSrcReg: {
503 if (isX86_64ExtendedReg(MI.getOperand(0)))
504 REX |= 1 << 2;
505 i = isTwoAddr ? 2 : 1;
506 for (unsigned e = NumOps; i != e; ++i) {
507 const MachineOperand& MO = MI.getOperand(i);
508 if (isX86_64ExtendedReg(MO))
509 REX |= 1 << 0;
510 }
511 break;
512 }
513 case X86II::MRMSrcMem: {
514 if (isX86_64ExtendedReg(MI.getOperand(0)))
515 REX |= 1 << 2;
516 unsigned Bit = 0;
517 i = isTwoAddr ? 2 : 1;
518 for (; i != NumOps; ++i) {
519 const MachineOperand& MO = MI.getOperand(i);
520 if (MO.isRegister()) {
521 if (isX86_64ExtendedReg(MO))
522 REX |= 1 << Bit;
523 Bit++;
524 }
525 }
526 break;
527 }
528 case X86II::MRM0m: case X86II::MRM1m:
529 case X86II::MRM2m: case X86II::MRM3m:
530 case X86II::MRM4m: case X86II::MRM5m:
531 case X86II::MRM6m: case X86II::MRM7m:
532 case X86II::MRMDestMem: {
533 unsigned e = isTwoAddr ? 5 : 4;
534 i = isTwoAddr ? 1 : 0;
535 if (NumOps > e && isX86_64ExtendedReg(MI.getOperand(e)))
536 REX |= 1 << 2;
537 unsigned Bit = 0;
538 for (; i != e; ++i) {
539 const MachineOperand& MO = MI.getOperand(i);
540 if (MO.isRegister()) {
541 if (isX86_64ExtendedReg(MO))
542 REX |= 1 << Bit;
543 Bit++;
544 }
545 }
546 break;
547 }
548 default: {
549 if (isX86_64ExtendedReg(MI.getOperand(0)))
550 REX |= 1 << 0;
551 i = isTwoAddr ? 2 : 1;
552 for (unsigned e = NumOps; i != e; ++i) {
553 const MachineOperand& MO = MI.getOperand(i);
554 if (isX86_64ExtendedReg(MO))
555 REX |= 1 << 2;
556 }
557 break;
558 }
559 }
560 }
561 return REX;
562}
563
564void Emitter::emitInstruction(const MachineInstr &MI) {
565 NumEmitted++; // Keep track of the # of mi's emitted
566
567 const TargetInstrDescriptor *Desc = MI.getInstrDescriptor();
568 unsigned Opcode = Desc->Opcode;
569
570 // Emit the repeat opcode prefix as needed.
571 if ((Desc->TSFlags & X86II::Op0Mask) == X86II::REP) MCE.emitByte(0xF3);
572
573 // Emit the operand size opcode prefix as needed.
574 if (Desc->TSFlags & X86II::OpSize) MCE.emitByte(0x66);
575
576 // Emit the address size opcode prefix as needed.
577 if (Desc->TSFlags & X86II::AdSize) MCE.emitByte(0x67);
578
579 bool Need0FPrefix = false;
580 switch (Desc->TSFlags & X86II::Op0Mask) {
581 case X86II::TB:
582 Need0FPrefix = true; // Two-byte opcode prefix
583 break;
584 case X86II::T8:
585 MCE.emitByte(0x0F);
586 MCE.emitByte(0x38);
587 break;
588 case X86II::TA:
589 MCE.emitByte(0x0F);
590 MCE.emitByte(0x3A);
591 break;
592 case X86II::REP: break; // already handled.
593 case X86II::XS: // F3 0F
594 MCE.emitByte(0xF3);
595 Need0FPrefix = true;
596 break;
597 case X86II::XD: // F2 0F
598 MCE.emitByte(0xF2);
599 Need0FPrefix = true;
600 break;
601 case X86II::D8: case X86II::D9: case X86II::DA: case X86II::DB:
602 case X86II::DC: case X86II::DD: case X86II::DE: case X86II::DF:
603 MCE.emitByte(0xD8+
604 (((Desc->TSFlags & X86II::Op0Mask)-X86II::D8)
605 >> X86II::Op0Shift));
606 break; // Two-byte opcode prefix
607 default: assert(0 && "Invalid prefix!");
608 case 0: break; // No prefix!
609 }
610
611 if (Is64BitMode) {
612 // REX prefix
613 unsigned REX = determineREX(MI);
614 if (REX)
615 MCE.emitByte(0x40 | REX);
616 }
617
618 // 0x0F escape code must be emitted just before the opcode.
619 if (Need0FPrefix)
620 MCE.emitByte(0x0F);
621
622 // If this is a two-address instruction, skip one of the register operands.
623 unsigned NumOps = Desc->numOperands;
624 unsigned CurOp = 0;
625 if (NumOps > 1 && Desc->getOperandConstraint(1, TOI::TIED_TO) != -1)
626 CurOp++;
627
628 unsigned char BaseOpcode = II->getBaseOpcodeFor(Desc);
629 switch (Desc->TSFlags & X86II::FormMask) {
630 default: assert(0 && "Unknown FormMask value in X86 MachineCodeEmitter!");
631 case X86II::Pseudo:
632#ifndef NDEBUG
633 switch (Opcode) {
634 default:
635 assert(0 && "psuedo instructions should be removed before code emission");
636 case TargetInstrInfo::INLINEASM:
637 assert(0 && "JIT does not support inline asm!\n");
638 case TargetInstrInfo::LABEL:
639 assert(0 && "JIT does not support meta labels!\n");
640 case X86::IMPLICIT_USE:
641 case X86::IMPLICIT_DEF:
642 case X86::IMPLICIT_DEF_GR8:
643 case X86::IMPLICIT_DEF_GR16:
644 case X86::IMPLICIT_DEF_GR32:
645 case X86::IMPLICIT_DEF_GR64:
646 case X86::IMPLICIT_DEF_FR32:
647 case X86::IMPLICIT_DEF_FR64:
648 case X86::IMPLICIT_DEF_VR64:
649 case X86::IMPLICIT_DEF_VR128:
650 case X86::FP_REG_KILL:
651 break;
652 }
653#endif
654 CurOp = NumOps;
655 break;
656
657 case X86II::RawFrm:
658 MCE.emitByte(BaseOpcode);
659 if (CurOp != NumOps) {
660 const MachineOperand &MO = MI.getOperand(CurOp++);
661 if (MO.isMachineBasicBlock()) {
662 emitPCRelativeBlockAddress(MO.getMachineBasicBlock());
663 } else if (MO.isGlobalAddress()) {
664 bool NeedStub = Is64BitMode ||
665 Opcode == X86::TAILJMPd ||
666 Opcode == X86::TAILJMPr || Opcode == X86::TAILJMPm;
667 emitGlobalAddressForCall(MO.getGlobal(), !NeedStub);
668 } else if (MO.isExternalSymbol()) {
669 emitExternalSymbolAddress(MO.getSymbolName(), X86::reloc_pcrel_word);
670 } else if (MO.isImmediate()) {
671 emitConstant(MO.getImm(), sizeOfImm(Desc));
672 } else {
673 assert(0 && "Unknown RawFrm operand!");
674 }
675 }
676 break;
677
678 case X86II::AddRegFrm:
679 MCE.emitByte(BaseOpcode + getX86RegNum(MI.getOperand(CurOp++).getReg()));
680
681 if (CurOp != NumOps) {
682 const MachineOperand &MO1 = MI.getOperand(CurOp++);
683 unsigned Size = sizeOfImm(Desc);
684 if (MO1.isImmediate())
685 emitConstant(MO1.getImm(), Size);
686 else {
687 unsigned rt = Is64BitMode ? X86::reloc_pcrel_word : X86::reloc_absolute_word;
688 if (Opcode == X86::MOV64ri)
689 rt = X86::reloc_absolute_dword; // FIXME: add X86II flag?
690 if (MO1.isGlobalAddress())
691 emitGlobalAddressForPtr(MO1.getGlobal(), rt, MO1.getOffset());
692 else if (MO1.isExternalSymbol())
693 emitExternalSymbolAddress(MO1.getSymbolName(), rt);
694 else if (MO1.isConstantPoolIndex())
695 emitConstPoolAddress(MO1.getConstantPoolIndex(), rt);
696 else if (MO1.isJumpTableIndex())
697 emitJumpTableAddress(MO1.getJumpTableIndex(), rt);
698 }
699 }
700 break;
701
702 case X86II::MRMDestReg: {
703 MCE.emitByte(BaseOpcode);
704 emitRegModRMByte(MI.getOperand(CurOp).getReg(),
705 getX86RegNum(MI.getOperand(CurOp+1).getReg()));
706 CurOp += 2;
707 if (CurOp != NumOps)
708 emitConstant(MI.getOperand(CurOp++).getImm(), sizeOfImm(Desc));
709 break;
710 }
711 case X86II::MRMDestMem: {
712 MCE.emitByte(BaseOpcode);
713 emitMemModRMByte(MI, CurOp, getX86RegNum(MI.getOperand(CurOp+4).getReg()));
714 CurOp += 5;
715 if (CurOp != NumOps)
716 emitConstant(MI.getOperand(CurOp++).getImm(), sizeOfImm(Desc));
717 break;
718 }
719
720 case X86II::MRMSrcReg:
721 MCE.emitByte(BaseOpcode);
722 emitRegModRMByte(MI.getOperand(CurOp+1).getReg(),
723 getX86RegNum(MI.getOperand(CurOp).getReg()));
724 CurOp += 2;
725 if (CurOp != NumOps)
726 emitConstant(MI.getOperand(CurOp++).getImm(), sizeOfImm(Desc));
727 break;
728
729 case X86II::MRMSrcMem: {
730 unsigned PCAdj = (CurOp+5 != NumOps) ? sizeOfImm(Desc) : 0;
731
732 MCE.emitByte(BaseOpcode);
733 emitMemModRMByte(MI, CurOp+1, getX86RegNum(MI.getOperand(CurOp).getReg()),
734 PCAdj);
735 CurOp += 5;
736 if (CurOp != NumOps)
737 emitConstant(MI.getOperand(CurOp++).getImm(), sizeOfImm(Desc));
738 break;
739 }
740
741 case X86II::MRM0r: case X86II::MRM1r:
742 case X86II::MRM2r: case X86II::MRM3r:
743 case X86II::MRM4r: case X86II::MRM5r:
744 case X86II::MRM6r: case X86II::MRM7r:
745 MCE.emitByte(BaseOpcode);
746 emitRegModRMByte(MI.getOperand(CurOp++).getReg(),
747 (Desc->TSFlags & X86II::FormMask)-X86II::MRM0r);
748
749 if (CurOp != NumOps) {
750 const MachineOperand &MO1 = MI.getOperand(CurOp++);
751 unsigned Size = sizeOfImm(Desc);
752 if (MO1.isImmediate())
753 emitConstant(MO1.getImm(), Size);
754 else {
755 unsigned rt = Is64BitMode ? X86::reloc_pcrel_word
756 : X86::reloc_absolute_word;
757 if (Opcode == X86::MOV64ri32)
758 rt = X86::reloc_absolute_word; // FIXME: add X86II flag?
759 if (MO1.isGlobalAddress())
760 emitGlobalAddressForPtr(MO1.getGlobal(), rt, MO1.getOffset());
761 else if (MO1.isExternalSymbol())
762 emitExternalSymbolAddress(MO1.getSymbolName(), rt);
763 else if (MO1.isConstantPoolIndex())
764 emitConstPoolAddress(MO1.getConstantPoolIndex(), rt);
765 else if (MO1.isJumpTableIndex())
766 emitJumpTableAddress(MO1.getJumpTableIndex(), rt);
767 }
768 }
769 break;
770
771 case X86II::MRM0m: case X86II::MRM1m:
772 case X86II::MRM2m: case X86II::MRM3m:
773 case X86II::MRM4m: case X86II::MRM5m:
774 case X86II::MRM6m: case X86II::MRM7m: {
775 unsigned PCAdj = (CurOp+4 != NumOps) ?
776 (MI.getOperand(CurOp+4).isImmediate() ? sizeOfImm(Desc) : 4) : 0;
777
778 MCE.emitByte(BaseOpcode);
779 emitMemModRMByte(MI, CurOp, (Desc->TSFlags & X86II::FormMask)-X86II::MRM0m,
780 PCAdj);
781 CurOp += 4;
782
783 if (CurOp != NumOps) {
784 const MachineOperand &MO = MI.getOperand(CurOp++);
785 unsigned Size = sizeOfImm(Desc);
786 if (MO.isImmediate())
787 emitConstant(MO.getImm(), Size);
788 else {
789 unsigned rt = Is64BitMode ? X86::reloc_pcrel_word
790 : X86::reloc_absolute_word;
791 if (Opcode == X86::MOV64mi32)
792 rt = X86::reloc_absolute_word; // FIXME: add X86II flag?
793 if (MO.isGlobalAddress())
794 emitGlobalAddressForPtr(MO.getGlobal(), rt, MO.getOffset());
795 else if (MO.isExternalSymbol())
796 emitExternalSymbolAddress(MO.getSymbolName(), rt);
797 else if (MO.isConstantPoolIndex())
798 emitConstPoolAddress(MO.getConstantPoolIndex(), rt);
799 else if (MO.isJumpTableIndex())
800 emitJumpTableAddress(MO.getJumpTableIndex(), rt);
801 }
802 }
803 break;
804 }
805
806 case X86II::MRMInitReg:
807 MCE.emitByte(BaseOpcode);
808 // Duplicate register, used by things like MOV8r0 (aka xor reg,reg).
809 emitRegModRMByte(MI.getOperand(CurOp).getReg(),
810 getX86RegNum(MI.getOperand(CurOp).getReg()));
811 ++CurOp;
812 break;
813 }
814
815 assert((Desc->Flags & M_VARIABLE_OPS) != 0 ||
816 CurOp == NumOps && "Unknown encoding!");
817}