Couple of fixes to mention bunzip2 and make instructions more clear.
[llvm-complete.git] / lib / Target / X86 / X86CodeEmitter.cpp
blob90d361bc2f31b21d50180cd7d63d5c9e148e2c5c
1 //===-- 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.
13 //===----------------------------------------------------------------------===//
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"
30 using namespace llvm;
32 STATISTIC(NumEmitted, "Number of machine instructions emitted");
34 namespace {
35 class VISIBILITY_HIDDEN Emitter : public MachineFunctionPass {
36 const X86InstrInfo *II;
37 const TargetData *TD;
38 TargetMachine &TM;
39 MachineCodeEmitter &MCE;
40 bool Is64BitMode;
41 public:
42 static char ID;
43 explicit Emitter(TargetMachine &tm, MachineCodeEmitter &mce)
44 : MachineFunctionPass((intptr_t)&ID), II(0), TD(0), TM(tm),
45 MCE(mce), Is64BitMode(false) {}
46 Emitter(TargetMachine &tm, MachineCodeEmitter &mce,
47 const X86InstrInfo &ii, const TargetData &td, bool is64)
48 : MachineFunctionPass((intptr_t)&ID), II(&ii), TD(&td), TM(tm),
49 MCE(mce), Is64BitMode(is64) {}
51 bool runOnMachineFunction(MachineFunction &MF);
53 virtual const char *getPassName() const {
54 return "X86 Machine Code Emitter";
57 void emitInstruction(const MachineInstr &MI);
59 private:
60 void emitPCRelativeBlockAddress(MachineBasicBlock *MBB);
61 void emitPCRelativeValue(intptr_t Address);
62 void emitGlobalAddressForCall(GlobalValue *GV, bool DoesntNeedStub);
63 void emitGlobalAddressForPtr(GlobalValue *GV, unsigned Reloc,
64 int Disp = 0, unsigned PCAdj = 0);
65 void emitExternalSymbolAddress(const char *ES, unsigned Reloc);
66 void emitConstPoolAddress(unsigned CPI, unsigned Reloc, int Disp = 0,
67 unsigned PCAdj = 0);
68 void emitJumpTableAddress(unsigned JTI, unsigned Reloc, unsigned PCAdj = 0);
70 void emitDisplacementField(const MachineOperand *RelocOp, int DispVal,
71 unsigned PCAdj = 0);
73 void emitRegModRMByte(unsigned ModRMReg, unsigned RegOpcodeField);
74 void emitSIBByte(unsigned SS, unsigned Index, unsigned Base);
75 void emitConstant(uint64_t Val, unsigned Size);
77 void emitMemModRMByte(const MachineInstr &MI,
78 unsigned Op, unsigned RegOpcodeField,
79 unsigned PCAdj = 0);
81 unsigned getX86RegNum(unsigned RegNo);
82 bool isX86_64ExtendedReg(const MachineOperand &MO);
83 unsigned determineREX(const MachineInstr &MI);
85 char Emitter::ID = 0;
88 /// createX86CodeEmitterPass - Return a pass that emits the collected X86 code
89 /// to the specified MCE object.
90 FunctionPass *llvm::createX86CodeEmitterPass(X86TargetMachine &TM,
91 MachineCodeEmitter &MCE) {
92 return new Emitter(TM, MCE);
95 bool Emitter::runOnMachineFunction(MachineFunction &MF) {
96 assert((MF.getTarget().getRelocationModel() != Reloc::Default ||
97 MF.getTarget().getRelocationModel() != Reloc::Static) &&
98 "JIT relocation model must be set to static or default!");
99 II = ((X86TargetMachine&)MF.getTarget()).getInstrInfo();
100 TD = ((X86TargetMachine&)MF.getTarget()).getTargetData();
101 Is64BitMode =
102 ((X86TargetMachine&)MF.getTarget()).getSubtarget<X86Subtarget>().is64Bit();
104 do {
105 MCE.startFunction(MF);
106 for (MachineFunction::iterator MBB = MF.begin(), E = MF.end();
107 MBB != E; ++MBB) {
108 MCE.StartMachineBasicBlock(MBB);
109 for (MachineBasicBlock::const_iterator I = MBB->begin(), E = MBB->end();
110 I != E; ++I)
111 emitInstruction(*I);
113 } while (MCE.finishFunction(MF));
115 return false;
118 /// emitPCRelativeValue - Emit a PC relative address.
120 void Emitter::emitPCRelativeValue(intptr_t Address) {
121 MCE.emitWordLE(Address-MCE.getCurrentPCValue()-4);
124 /// emitPCRelativeBlockAddress - This method keeps track of the information
125 /// necessary to resolve the address of this block later and emits a dummy
126 /// value.
128 void Emitter::emitPCRelativeBlockAddress(MachineBasicBlock *MBB) {
129 // Remember where this reference was and where it is to so we can
130 // deal with it later.
131 MCE.addRelocation(MachineRelocation::getBB(MCE.getCurrentPCOffset(),
132 X86::reloc_pcrel_word, MBB));
133 MCE.emitWordLE(0);
136 /// emitGlobalAddressForCall - Emit the specified address to the code stream
137 /// assuming this is part of a function call, which is PC relative.
139 void Emitter::emitGlobalAddressForCall(GlobalValue *GV, bool DoesntNeedStub) {
140 MCE.addRelocation(MachineRelocation::getGV(MCE.getCurrentPCOffset(),
141 X86::reloc_pcrel_word, GV, 0,
142 DoesntNeedStub));
143 MCE.emitWordLE(0);
146 /// emitGlobalAddress - Emit the specified address to the code stream assuming
147 /// this is part of a "take the address of a global" instruction.
149 void Emitter::emitGlobalAddressForPtr(GlobalValue *GV, unsigned Reloc,
150 int Disp /* = 0 */,
151 unsigned PCAdj /* = 0 */) {
152 MCE.addRelocation(MachineRelocation::getGV(MCE.getCurrentPCOffset(), Reloc,
153 GV, PCAdj));
154 if (Reloc == X86::reloc_absolute_dword)
155 MCE.emitWordLE(0);
156 MCE.emitWordLE(Disp); // The relocated value will be added to the displacement
159 /// emitExternalSymbolAddress - Arrange for the address of an external symbol to
160 /// be emitted to the current location in the function, and allow it to be PC
161 /// relative.
162 void Emitter::emitExternalSymbolAddress(const char *ES, unsigned Reloc) {
163 MCE.addRelocation(MachineRelocation::getExtSym(MCE.getCurrentPCOffset(),
164 Reloc, ES));
165 if (Reloc == X86::reloc_absolute_dword)
166 MCE.emitWordLE(0);
167 MCE.emitWordLE(0);
170 /// emitConstPoolAddress - Arrange for the address of an constant pool
171 /// to be emitted to the current location in the function, and allow it to be PC
172 /// relative.
173 void Emitter::emitConstPoolAddress(unsigned CPI, unsigned Reloc,
174 int Disp /* = 0 */,
175 unsigned PCAdj /* = 0 */) {
176 MCE.addRelocation(MachineRelocation::getConstPool(MCE.getCurrentPCOffset(),
177 Reloc, CPI, PCAdj));
178 if (Reloc == X86::reloc_absolute_dword)
179 MCE.emitWordLE(0);
180 MCE.emitWordLE(Disp); // The relocated value will be added to the displacement
183 /// emitJumpTableAddress - Arrange for the address of a jump table to
184 /// be emitted to the current location in the function, and allow it to be PC
185 /// relative.
186 void Emitter::emitJumpTableAddress(unsigned JTI, unsigned Reloc,
187 unsigned PCAdj /* = 0 */) {
188 MCE.addRelocation(MachineRelocation::getJumpTable(MCE.getCurrentPCOffset(),
189 Reloc, JTI, PCAdj));
190 if (Reloc == X86::reloc_absolute_dword)
191 MCE.emitWordLE(0);
192 MCE.emitWordLE(0); // The relocated value will be added to the displacement
195 unsigned Emitter::getX86RegNum(unsigned RegNo) {
196 return ((X86RegisterInfo&)II->getRegisterInfo()).getX86RegNum(RegNo);
199 inline static unsigned char ModRMByte(unsigned Mod, unsigned RegOpcode,
200 unsigned RM) {
201 assert(Mod < 4 && RegOpcode < 8 && RM < 8 && "ModRM Fields out of range!");
202 return RM | (RegOpcode << 3) | (Mod << 6);
205 void Emitter::emitRegModRMByte(unsigned ModRMReg, unsigned RegOpcodeFld){
206 MCE.emitByte(ModRMByte(3, RegOpcodeFld, getX86RegNum(ModRMReg)));
209 void Emitter::emitSIBByte(unsigned SS, unsigned Index, unsigned Base) {
210 // SIB byte is in the same format as the ModRMByte...
211 MCE.emitByte(ModRMByte(SS, Index, Base));
214 void Emitter::emitConstant(uint64_t Val, unsigned Size) {
215 // Output the constant in little endian byte order...
216 for (unsigned i = 0; i != Size; ++i) {
217 MCE.emitByte(Val & 255);
218 Val >>= 8;
222 /// isDisp8 - Return true if this signed displacement fits in a 8-bit
223 /// sign-extended field.
224 static bool isDisp8(int Value) {
225 return Value == (signed char)Value;
228 void Emitter::emitDisplacementField(const MachineOperand *RelocOp,
229 int DispVal, unsigned PCAdj) {
230 // If this is a simple integer displacement that doesn't require a relocation,
231 // emit it now.
232 if (!RelocOp) {
233 emitConstant(DispVal, 4);
234 return;
237 // Otherwise, this is something that requires a relocation. Emit it as such
238 // now.
239 if (RelocOp->isGlobalAddress()) {
240 // In 64-bit static small code model, we could potentially emit absolute.
241 // But it's probably not beneficial.
242 // 89 05 00 00 00 00 mov %eax,0(%rip) # PC-relative
243 // 89 04 25 00 00 00 00 mov %eax,0x0 # Absolute
244 unsigned rt= Is64BitMode ? X86::reloc_pcrel_word : X86::reloc_absolute_word;
245 emitGlobalAddressForPtr(RelocOp->getGlobal(), rt,
246 RelocOp->getOffset(), PCAdj);
247 } else if (RelocOp->isConstantPoolIndex()) {
248 // Must be in 64-bit mode.
249 emitConstPoolAddress(RelocOp->getConstantPoolIndex(), X86::reloc_pcrel_word,
250 RelocOp->getOffset(), PCAdj);
251 } else if (RelocOp->isJumpTableIndex()) {
252 // Must be in 64-bit mode.
253 emitJumpTableAddress(RelocOp->getJumpTableIndex(), X86::reloc_pcrel_word,
254 PCAdj);
255 } else {
256 assert(0 && "Unknown value to relocate!");
260 void Emitter::emitMemModRMByte(const MachineInstr &MI,
261 unsigned Op, unsigned RegOpcodeField,
262 unsigned PCAdj) {
263 const MachineOperand &Op3 = MI.getOperand(Op+3);
264 int DispVal = 0;
265 const MachineOperand *DispForReloc = 0;
267 // Figure out what sort of displacement we have to handle here.
268 if (Op3.isGlobalAddress()) {
269 DispForReloc = &Op3;
270 } else if (Op3.isConstantPoolIndex()) {
271 if (Is64BitMode) {
272 DispForReloc = &Op3;
273 } else {
274 DispVal += MCE.getConstantPoolEntryAddress(Op3.getConstantPoolIndex());
275 DispVal += Op3.getOffset();
277 } else if (Op3.isJumpTableIndex()) {
278 if (Is64BitMode) {
279 DispForReloc = &Op3;
280 } else {
281 DispVal += MCE.getJumpTableEntryAddress(Op3.getJumpTableIndex());
283 } else {
284 DispVal = Op3.getImm();
287 const MachineOperand &Base = MI.getOperand(Op);
288 const MachineOperand &Scale = MI.getOperand(Op+1);
289 const MachineOperand &IndexReg = MI.getOperand(Op+2);
291 unsigned BaseReg = Base.getReg();
293 // Is a SIB byte needed?
294 if (IndexReg.getReg() == 0 &&
295 (BaseReg == 0 || getX86RegNum(BaseReg) != N86::ESP)) {
296 if (BaseReg == 0) { // Just a displacement?
297 // Emit special case [disp32] encoding
298 MCE.emitByte(ModRMByte(0, RegOpcodeField, 5));
300 emitDisplacementField(DispForReloc, DispVal, PCAdj);
301 } else {
302 unsigned BaseRegNo = getX86RegNum(BaseReg);
303 if (!DispForReloc && DispVal == 0 && BaseRegNo != N86::EBP) {
304 // Emit simple indirect register encoding... [EAX] f.e.
305 MCE.emitByte(ModRMByte(0, RegOpcodeField, BaseRegNo));
306 } else if (!DispForReloc && isDisp8(DispVal)) {
307 // Emit the disp8 encoding... [REG+disp8]
308 MCE.emitByte(ModRMByte(1, RegOpcodeField, BaseRegNo));
309 emitConstant(DispVal, 1);
310 } else {
311 // Emit the most general non-SIB encoding: [REG+disp32]
312 MCE.emitByte(ModRMByte(2, RegOpcodeField, BaseRegNo));
313 emitDisplacementField(DispForReloc, DispVal, PCAdj);
317 } else { // We need a SIB byte, so start by outputting the ModR/M byte first
318 assert(IndexReg.getReg() != X86::ESP &&
319 IndexReg.getReg() != X86::RSP && "Cannot use ESP as index reg!");
321 bool ForceDisp32 = false;
322 bool ForceDisp8 = false;
323 if (BaseReg == 0) {
324 // If there is no base register, we emit the special case SIB byte with
325 // MOD=0, BASE=5, to JUST get the index, scale, and displacement.
326 MCE.emitByte(ModRMByte(0, RegOpcodeField, 4));
327 ForceDisp32 = true;
328 } else if (DispForReloc) {
329 // Emit the normal disp32 encoding.
330 MCE.emitByte(ModRMByte(2, RegOpcodeField, 4));
331 ForceDisp32 = true;
332 } else if (DispVal == 0 && getX86RegNum(BaseReg) != N86::EBP) {
333 // Emit no displacement ModR/M byte
334 MCE.emitByte(ModRMByte(0, RegOpcodeField, 4));
335 } else if (isDisp8(DispVal)) {
336 // Emit the disp8 encoding...
337 MCE.emitByte(ModRMByte(1, RegOpcodeField, 4));
338 ForceDisp8 = true; // Make sure to force 8 bit disp if Base=EBP
339 } else {
340 // Emit the normal disp32 encoding...
341 MCE.emitByte(ModRMByte(2, RegOpcodeField, 4));
344 // Calculate what the SS field value should be...
345 static const unsigned SSTable[] = { ~0, 0, 1, ~0, 2, ~0, ~0, ~0, 3 };
346 unsigned SS = SSTable[Scale.getImm()];
348 if (BaseReg == 0) {
349 // Handle the SIB byte for the case where there is no base. The
350 // displacement has already been output.
351 assert(IndexReg.getReg() && "Index register must be specified!");
352 emitSIBByte(SS, getX86RegNum(IndexReg.getReg()), 5);
353 } else {
354 unsigned BaseRegNo = getX86RegNum(BaseReg);
355 unsigned IndexRegNo;
356 if (IndexReg.getReg())
357 IndexRegNo = getX86RegNum(IndexReg.getReg());
358 else
359 IndexRegNo = 4; // For example [ESP+1*<noreg>+4]
360 emitSIBByte(SS, IndexRegNo, BaseRegNo);
363 // Do we need to output a displacement?
364 if (ForceDisp8) {
365 emitConstant(DispVal, 1);
366 } else if (DispVal != 0 || ForceDisp32) {
367 emitDisplacementField(DispForReloc, DispVal, PCAdj);
372 static unsigned sizeOfImm(const TargetInstrDescriptor *Desc) {
373 switch (Desc->TSFlags & X86II::ImmMask) {
374 case X86II::Imm8: return 1;
375 case X86II::Imm16: return 2;
376 case X86II::Imm32: return 4;
377 case X86II::Imm64: return 8;
378 default: assert(0 && "Immediate size not set!");
379 return 0;
383 /// isX86_64ExtendedReg - Is the MachineOperand a x86-64 extended register?
384 /// e.g. r8, xmm8, etc.
385 bool Emitter::isX86_64ExtendedReg(const MachineOperand &MO) {
386 if (!MO.isRegister()) return false;
387 unsigned RegNo = MO.getReg();
388 int DWNum = II->getRegisterInfo().getDwarfRegNum(RegNo);
389 if (DWNum >= II->getRegisterInfo().getDwarfRegNum(X86::R8) &&
390 DWNum <= II->getRegisterInfo().getDwarfRegNum(X86::R15))
391 return true;
392 if (DWNum >= II->getRegisterInfo().getDwarfRegNum(X86::XMM8) &&
393 DWNum <= II->getRegisterInfo().getDwarfRegNum(X86::XMM15))
394 return true;
395 return false;
398 inline static bool isX86_64NonExtLowByteReg(unsigned reg) {
399 return (reg == X86::SPL || reg == X86::BPL ||
400 reg == X86::SIL || reg == X86::DIL);
403 /// determineREX - Determine if the MachineInstr has to be encoded with a X86-64
404 /// REX prefix which specifies 1) 64-bit instructions, 2) non-default operand
405 /// size, and 3) use of X86-64 extended registers.
406 unsigned Emitter::determineREX(const MachineInstr &MI) {
407 unsigned REX = 0;
408 const TargetInstrDescriptor *Desc = MI.getInstrDescriptor();
410 // Pseudo instructions do not need REX prefix byte.
411 if ((Desc->TSFlags & X86II::FormMask) == X86II::Pseudo)
412 return 0;
413 if (Desc->TSFlags & X86II::REX_W)
414 REX |= 1 << 3;
416 unsigned NumOps = Desc->numOperands;
417 if (NumOps) {
418 bool isTwoAddr = NumOps > 1 &&
419 Desc->getOperandConstraint(1, TOI::TIED_TO) != -1;
421 // If it accesses SPL, BPL, SIL, or DIL, then it requires a 0x40 REX prefix.
422 unsigned i = isTwoAddr ? 1 : 0;
423 for (unsigned e = NumOps; i != e; ++i) {
424 const MachineOperand& MO = MI.getOperand(i);
425 if (MO.isRegister()) {
426 unsigned Reg = MO.getReg();
427 if (isX86_64NonExtLowByteReg(Reg))
428 REX |= 0x40;
432 switch (Desc->TSFlags & X86II::FormMask) {
433 case X86II::MRMInitReg:
434 if (isX86_64ExtendedReg(MI.getOperand(0)))
435 REX |= (1 << 0) | (1 << 2);
436 break;
437 case X86II::MRMSrcReg: {
438 if (isX86_64ExtendedReg(MI.getOperand(0)))
439 REX |= 1 << 2;
440 i = isTwoAddr ? 2 : 1;
441 for (unsigned e = NumOps; i != e; ++i) {
442 const MachineOperand& MO = MI.getOperand(i);
443 if (isX86_64ExtendedReg(MO))
444 REX |= 1 << 0;
446 break;
448 case X86II::MRMSrcMem: {
449 if (isX86_64ExtendedReg(MI.getOperand(0)))
450 REX |= 1 << 2;
451 unsigned Bit = 0;
452 i = isTwoAddr ? 2 : 1;
453 for (; i != NumOps; ++i) {
454 const MachineOperand& MO = MI.getOperand(i);
455 if (MO.isRegister()) {
456 if (isX86_64ExtendedReg(MO))
457 REX |= 1 << Bit;
458 Bit++;
461 break;
463 case X86II::MRM0m: case X86II::MRM1m:
464 case X86II::MRM2m: case X86II::MRM3m:
465 case X86II::MRM4m: case X86II::MRM5m:
466 case X86II::MRM6m: case X86II::MRM7m:
467 case X86II::MRMDestMem: {
468 unsigned e = isTwoAddr ? 5 : 4;
469 i = isTwoAddr ? 1 : 0;
470 if (NumOps > e && isX86_64ExtendedReg(MI.getOperand(e)))
471 REX |= 1 << 2;
472 unsigned Bit = 0;
473 for (; i != e; ++i) {
474 const MachineOperand& MO = MI.getOperand(i);
475 if (MO.isRegister()) {
476 if (isX86_64ExtendedReg(MO))
477 REX |= 1 << Bit;
478 Bit++;
481 break;
483 default: {
484 if (isX86_64ExtendedReg(MI.getOperand(0)))
485 REX |= 1 << 0;
486 i = isTwoAddr ? 2 : 1;
487 for (unsigned e = NumOps; i != e; ++i) {
488 const MachineOperand& MO = MI.getOperand(i);
489 if (isX86_64ExtendedReg(MO))
490 REX |= 1 << 2;
492 break;
496 return REX;
499 void Emitter::emitInstruction(const MachineInstr &MI) {
500 NumEmitted++; // Keep track of the # of mi's emitted
502 const TargetInstrDescriptor *Desc = MI.getInstrDescriptor();
503 unsigned Opcode = Desc->Opcode;
505 // Emit the repeat opcode prefix as needed.
506 if ((Desc->TSFlags & X86II::Op0Mask) == X86II::REP) MCE.emitByte(0xF3);
508 // Emit the operand size opcode prefix as needed.
509 if (Desc->TSFlags & X86II::OpSize) MCE.emitByte(0x66);
511 // Emit the address size opcode prefix as needed.
512 if (Desc->TSFlags & X86II::AdSize) MCE.emitByte(0x67);
514 bool Need0FPrefix = false;
515 switch (Desc->TSFlags & X86II::Op0Mask) {
516 case X86II::TB:
517 Need0FPrefix = true; // Two-byte opcode prefix
518 break;
519 case X86II::T8:
520 MCE.emitByte(0x0F);
521 MCE.emitByte(0x38);
522 break;
523 case X86II::TA:
524 MCE.emitByte(0x0F);
525 MCE.emitByte(0x3A);
526 break;
527 case X86II::REP: break; // already handled.
528 case X86II::XS: // F3 0F
529 MCE.emitByte(0xF3);
530 Need0FPrefix = true;
531 break;
532 case X86II::XD: // F2 0F
533 MCE.emitByte(0xF2);
534 Need0FPrefix = true;
535 break;
536 case X86II::D8: case X86II::D9: case X86II::DA: case X86II::DB:
537 case X86II::DC: case X86II::DD: case X86II::DE: case X86II::DF:
538 MCE.emitByte(0xD8+
539 (((Desc->TSFlags & X86II::Op0Mask)-X86II::D8)
540 >> X86II::Op0Shift));
541 break; // Two-byte opcode prefix
542 default: assert(0 && "Invalid prefix!");
543 case 0: break; // No prefix!
546 if (Is64BitMode) {
547 // REX prefix
548 unsigned REX = determineREX(MI);
549 if (REX)
550 MCE.emitByte(0x40 | REX);
553 // 0x0F escape code must be emitted just before the opcode.
554 if (Need0FPrefix)
555 MCE.emitByte(0x0F);
557 // If this is a two-address instruction, skip one of the register operands.
558 unsigned NumOps = Desc->numOperands;
559 unsigned CurOp = 0;
560 if (NumOps > 1 && Desc->getOperandConstraint(1, TOI::TIED_TO) != -1)
561 CurOp++;
563 unsigned char BaseOpcode = II->getBaseOpcodeFor(Desc);
564 switch (Desc->TSFlags & X86II::FormMask) {
565 default: assert(0 && "Unknown FormMask value in X86 MachineCodeEmitter!");
566 case X86II::Pseudo:
567 #ifndef NDEBUG
568 switch (Opcode) {
569 default:
570 assert(0 && "psuedo instructions should be removed before code emission");
571 case TargetInstrInfo::INLINEASM:
572 assert(0 && "JIT does not support inline asm!\n");
573 case TargetInstrInfo::LABEL:
574 assert(0 && "JIT does not support meta labels!\n");
575 case X86::IMPLICIT_USE:
576 case X86::IMPLICIT_DEF:
577 case X86::IMPLICIT_DEF_GR8:
578 case X86::IMPLICIT_DEF_GR16:
579 case X86::IMPLICIT_DEF_GR32:
580 case X86::IMPLICIT_DEF_GR64:
581 case X86::IMPLICIT_DEF_FR32:
582 case X86::IMPLICIT_DEF_FR64:
583 case X86::IMPLICIT_DEF_VR64:
584 case X86::IMPLICIT_DEF_VR128:
585 case X86::FP_REG_KILL:
586 break;
588 #endif
589 CurOp = NumOps;
590 break;
592 case X86II::RawFrm:
593 MCE.emitByte(BaseOpcode);
594 if (CurOp != NumOps) {
595 const MachineOperand &MO = MI.getOperand(CurOp++);
596 if (MO.isMachineBasicBlock()) {
597 emitPCRelativeBlockAddress(MO.getMachineBasicBlock());
598 } else if (MO.isGlobalAddress()) {
599 bool NeedStub = Is64BitMode ||
600 Opcode == X86::TAILJMPd ||
601 Opcode == X86::TAILJMPr || Opcode == X86::TAILJMPm;
602 emitGlobalAddressForCall(MO.getGlobal(), !NeedStub);
603 } else if (MO.isExternalSymbol()) {
604 emitExternalSymbolAddress(MO.getSymbolName(), X86::reloc_pcrel_word);
605 } else if (MO.isImmediate()) {
606 emitConstant(MO.getImm(), sizeOfImm(Desc));
607 } else {
608 assert(0 && "Unknown RawFrm operand!");
611 break;
613 case X86II::AddRegFrm:
614 MCE.emitByte(BaseOpcode + getX86RegNum(MI.getOperand(CurOp++).getReg()));
616 if (CurOp != NumOps) {
617 const MachineOperand &MO1 = MI.getOperand(CurOp++);
618 unsigned Size = sizeOfImm(Desc);
619 if (MO1.isImmediate())
620 emitConstant(MO1.getImm(), Size);
621 else {
622 unsigned rt = Is64BitMode ? X86::reloc_pcrel_word : X86::reloc_absolute_word;
623 if (Opcode == X86::MOV64ri)
624 rt = X86::reloc_absolute_dword; // FIXME: add X86II flag?
625 if (MO1.isGlobalAddress())
626 emitGlobalAddressForPtr(MO1.getGlobal(), rt, MO1.getOffset());
627 else if (MO1.isExternalSymbol())
628 emitExternalSymbolAddress(MO1.getSymbolName(), rt);
629 else if (MO1.isConstantPoolIndex())
630 emitConstPoolAddress(MO1.getConstantPoolIndex(), rt);
631 else if (MO1.isJumpTableIndex())
632 emitJumpTableAddress(MO1.getJumpTableIndex(), rt);
635 break;
637 case X86II::MRMDestReg: {
638 MCE.emitByte(BaseOpcode);
639 emitRegModRMByte(MI.getOperand(CurOp).getReg(),
640 getX86RegNum(MI.getOperand(CurOp+1).getReg()));
641 CurOp += 2;
642 if (CurOp != NumOps)
643 emitConstant(MI.getOperand(CurOp++).getImm(), sizeOfImm(Desc));
644 break;
646 case X86II::MRMDestMem: {
647 MCE.emitByte(BaseOpcode);
648 emitMemModRMByte(MI, CurOp, getX86RegNum(MI.getOperand(CurOp+4).getReg()));
649 CurOp += 5;
650 if (CurOp != NumOps)
651 emitConstant(MI.getOperand(CurOp++).getImm(), sizeOfImm(Desc));
652 break;
655 case X86II::MRMSrcReg:
656 MCE.emitByte(BaseOpcode);
657 emitRegModRMByte(MI.getOperand(CurOp+1).getReg(),
658 getX86RegNum(MI.getOperand(CurOp).getReg()));
659 CurOp += 2;
660 if (CurOp != NumOps)
661 emitConstant(MI.getOperand(CurOp++).getImm(), sizeOfImm(Desc));
662 break;
664 case X86II::MRMSrcMem: {
665 unsigned PCAdj = (CurOp+5 != NumOps) ? sizeOfImm(Desc) : 0;
667 MCE.emitByte(BaseOpcode);
668 emitMemModRMByte(MI, CurOp+1, getX86RegNum(MI.getOperand(CurOp).getReg()),
669 PCAdj);
670 CurOp += 5;
671 if (CurOp != NumOps)
672 emitConstant(MI.getOperand(CurOp++).getImm(), sizeOfImm(Desc));
673 break;
676 case X86II::MRM0r: case X86II::MRM1r:
677 case X86II::MRM2r: case X86II::MRM3r:
678 case X86II::MRM4r: case X86II::MRM5r:
679 case X86II::MRM6r: case X86II::MRM7r:
680 MCE.emitByte(BaseOpcode);
681 emitRegModRMByte(MI.getOperand(CurOp++).getReg(),
682 (Desc->TSFlags & X86II::FormMask)-X86II::MRM0r);
684 if (CurOp != NumOps) {
685 const MachineOperand &MO1 = MI.getOperand(CurOp++);
686 unsigned Size = sizeOfImm(Desc);
687 if (MO1.isImmediate())
688 emitConstant(MO1.getImm(), Size);
689 else {
690 unsigned rt = Is64BitMode ? X86::reloc_pcrel_word
691 : X86::reloc_absolute_word;
692 if (Opcode == X86::MOV64ri32)
693 rt = X86::reloc_absolute_word; // FIXME: add X86II flag?
694 if (MO1.isGlobalAddress())
695 emitGlobalAddressForPtr(MO1.getGlobal(), rt, MO1.getOffset());
696 else if (MO1.isExternalSymbol())
697 emitExternalSymbolAddress(MO1.getSymbolName(), rt);
698 else if (MO1.isConstantPoolIndex())
699 emitConstPoolAddress(MO1.getConstantPoolIndex(), rt);
700 else if (MO1.isJumpTableIndex())
701 emitJumpTableAddress(MO1.getJumpTableIndex(), rt);
704 break;
706 case X86II::MRM0m: case X86II::MRM1m:
707 case X86II::MRM2m: case X86II::MRM3m:
708 case X86II::MRM4m: case X86II::MRM5m:
709 case X86II::MRM6m: case X86II::MRM7m: {
710 unsigned PCAdj = (CurOp+4 != NumOps) ?
711 (MI.getOperand(CurOp+4).isImmediate() ? sizeOfImm(Desc) : 4) : 0;
713 MCE.emitByte(BaseOpcode);
714 emitMemModRMByte(MI, CurOp, (Desc->TSFlags & X86II::FormMask)-X86II::MRM0m,
715 PCAdj);
716 CurOp += 4;
718 if (CurOp != NumOps) {
719 const MachineOperand &MO = MI.getOperand(CurOp++);
720 unsigned Size = sizeOfImm(Desc);
721 if (MO.isImmediate())
722 emitConstant(MO.getImm(), Size);
723 else {
724 unsigned rt = Is64BitMode ? X86::reloc_pcrel_word
725 : X86::reloc_absolute_word;
726 if (Opcode == X86::MOV64mi32)
727 rt = X86::reloc_absolute_word; // FIXME: add X86II flag?
728 if (MO.isGlobalAddress())
729 emitGlobalAddressForPtr(MO.getGlobal(), rt, MO.getOffset());
730 else if (MO.isExternalSymbol())
731 emitExternalSymbolAddress(MO.getSymbolName(), rt);
732 else if (MO.isConstantPoolIndex())
733 emitConstPoolAddress(MO.getConstantPoolIndex(), rt);
734 else if (MO.isJumpTableIndex())
735 emitJumpTableAddress(MO.getJumpTableIndex(), rt);
738 break;
741 case X86II::MRMInitReg:
742 MCE.emitByte(BaseOpcode);
743 // Duplicate register, used by things like MOV8r0 (aka xor reg,reg).
744 emitRegModRMByte(MI.getOperand(CurOp).getReg(),
745 getX86RegNum(MI.getOperand(CurOp).getReg()));
746 ++CurOp;
747 break;
750 assert((Desc->Flags & M_VARIABLE_OPS) != 0 ||
751 CurOp == NumOps && "Unknown encoding!");