blob: 33b87be50d23f489a27a58a64b96e950368e047b [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"
Evan Chengaf743252008-01-05 02:26:58 +000017#include "X86JITInfo.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000018#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"
Nicolas Geoffray0e757e12008-02-13 18:39:37 +000026#include "llvm/CodeGen/MachineModuleInfo.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000027#include "llvm/CodeGen/Passes.h"
28#include "llvm/Function.h"
29#include "llvm/ADT/Statistic.h"
30#include "llvm/Support/Compiler.h"
Evan Cheng872bd4b2008-03-14 07:13:42 +000031#include "llvm/Support/Debug.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000032#include "llvm/Target/TargetOptions.h"
33using namespace llvm;
34
35STATISTIC(NumEmitted, "Number of machine instructions emitted");
36
37namespace {
38 class VISIBILITY_HIDDEN Emitter : public MachineFunctionPass {
39 const X86InstrInfo *II;
40 const TargetData *TD;
41 TargetMachine &TM;
42 MachineCodeEmitter &MCE;
Evan Chengaf743252008-01-05 02:26:58 +000043 intptr_t PICBaseOffset;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000044 bool Is64BitMode;
Evan Cheng8ee6bab2007-12-22 09:40:20 +000045 bool IsPIC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000046 public:
47 static char ID;
48 explicit Emitter(TargetMachine &tm, MachineCodeEmitter &mce)
49 : MachineFunctionPass((intptr_t)&ID), II(0), TD(0), TM(tm),
Evan Chengaf743252008-01-05 02:26:58 +000050 MCE(mce), PICBaseOffset(0), Is64BitMode(false),
Evan Cheng28e7e162008-01-04 10:46:51 +000051 IsPIC(TM.getRelocationModel() == Reloc::PIC_) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +000052 Emitter(TargetMachine &tm, MachineCodeEmitter &mce,
53 const X86InstrInfo &ii, const TargetData &td, bool is64)
54 : MachineFunctionPass((intptr_t)&ID), II(&ii), TD(&td), TM(tm),
Evan Chengaf743252008-01-05 02:26:58 +000055 MCE(mce), PICBaseOffset(0), Is64BitMode(is64),
Evan Cheng28e7e162008-01-04 10:46:51 +000056 IsPIC(TM.getRelocationModel() == Reloc::PIC_) {}
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
Evan Cheng0729ccf2008-01-05 00:41:47 +000064 void emitInstruction(const MachineInstr &MI,
Chris Lattner5b930372008-01-07 07:27:27 +000065 const TargetInstrDesc *Desc);
Nicolas Geoffray0e757e12008-02-13 18:39:37 +000066
67 void getAnalysisUsage(AnalysisUsage &AU) const {
68 AU.addRequired<MachineModuleInfo>();
69 MachineFunctionPass::getAnalysisUsage(AU);
70 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +000071
72 private:
73 void emitPCRelativeBlockAddress(MachineBasicBlock *MBB);
Evan Cheng8ee6bab2007-12-22 09:40:20 +000074 void emitGlobalAddress(GlobalValue *GV, unsigned Reloc,
75 int Disp = 0, intptr_t PCAdj = 0,
Evan Cheng28e7e162008-01-04 10:46:51 +000076 bool NeedStub = false, bool IsLazy = false);
Evan Chengf0123872008-01-03 02:56:28 +000077 void emitExternalSymbolAddress(const char *ES, unsigned Reloc);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000078 void emitConstPoolAddress(unsigned CPI, unsigned Reloc, int Disp = 0,
Evan Chengf0123872008-01-03 02:56:28 +000079 intptr_t PCAdj = 0);
Evan Cheng8ee6bab2007-12-22 09:40:20 +000080 void emitJumpTableAddress(unsigned JTI, unsigned Reloc,
Evan Chengf0123872008-01-03 02:56:28 +000081 intptr_t PCAdj = 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000082
83 void emitDisplacementField(const MachineOperand *RelocOp, int DispVal,
Evan Cheng8ee6bab2007-12-22 09:40:20 +000084 intptr_t PCAdj = 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000085
86 void emitRegModRMByte(unsigned ModRMReg, unsigned RegOpcodeField);
87 void emitSIBByte(unsigned SS, unsigned Index, unsigned Base);
88 void emitConstant(uint64_t Val, unsigned Size);
89
90 void emitMemModRMByte(const MachineInstr &MI,
91 unsigned Op, unsigned RegOpcodeField,
Evan Cheng8ee6bab2007-12-22 09:40:20 +000092 intptr_t PCAdj = 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000093
Dan Gohman06844672008-02-08 03:29:40 +000094 unsigned getX86RegNum(unsigned RegNo) const;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000095 bool isX86_64ExtendedReg(const MachineOperand &MO);
96 unsigned determineREX(const MachineInstr &MI);
Evan Cheng28e7e162008-01-04 10:46:51 +000097
98 bool gvNeedsLazyPtr(const GlobalValue *GV);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000099 };
100 char Emitter::ID = 0;
101}
102
103/// createX86CodeEmitterPass - Return a pass that emits the collected X86 code
104/// to the specified MCE object.
105FunctionPass *llvm::createX86CodeEmitterPass(X86TargetMachine &TM,
106 MachineCodeEmitter &MCE) {
107 return new Emitter(TM, MCE);
108}
109
110bool Emitter::runOnMachineFunction(MachineFunction &MF) {
111 assert((MF.getTarget().getRelocationModel() != Reloc::Default ||
112 MF.getTarget().getRelocationModel() != Reloc::Static) &&
113 "JIT relocation model must be set to static or default!");
Nicolas Geoffray0e757e12008-02-13 18:39:37 +0000114
115 MCE.setModuleInfo(&getAnalysis<MachineModuleInfo>());
116
Evan Cheng28e7e162008-01-04 10:46:51 +0000117 II = ((X86TargetMachine&)TM).getInstrInfo();
118 TD = ((X86TargetMachine&)TM).getTargetData();
119 Is64BitMode = TM.getSubtarget<X86Subtarget>().is64Bit();
Nicolas Geoffray0e757e12008-02-13 18:39:37 +0000120
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000121 do {
Evan Cheng872bd4b2008-03-14 07:13:42 +0000122 DOUT << "JITTing function '" << MF.getFunction()->getName() << "'\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000123 MCE.startFunction(MF);
124 for (MachineFunction::iterator MBB = MF.begin(), E = MF.end();
125 MBB != E; ++MBB) {
126 MCE.StartMachineBasicBlock(MBB);
127 for (MachineBasicBlock::const_iterator I = MBB->begin(), E = MBB->end();
Evan Cheng0729ccf2008-01-05 00:41:47 +0000128 I != E; ++I) {
Chris Lattner5b930372008-01-07 07:27:27 +0000129 const TargetInstrDesc &Desc = I->getDesc();
130 emitInstruction(*I, &Desc);
Evan Cheng0729ccf2008-01-05 00:41:47 +0000131 // MOVPC32r is basically a call plus a pop instruction.
Chris Lattner5b930372008-01-07 07:27:27 +0000132 if (Desc.getOpcode() == X86::MOVPC32r)
Evan Cheng0729ccf2008-01-05 00:41:47 +0000133 emitInstruction(*I, &II->get(X86::POP32r));
134 NumEmitted++; // Keep track of the # of mi's emitted
135 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000136 }
137 } while (MCE.finishFunction(MF));
138
139 return false;
140}
141
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000142/// emitPCRelativeBlockAddress - This method keeps track of the information
143/// necessary to resolve the address of this block later and emits a dummy
144/// value.
145///
146void Emitter::emitPCRelativeBlockAddress(MachineBasicBlock *MBB) {
147 // Remember where this reference was and where it is to so we can
148 // deal with it later.
149 MCE.addRelocation(MachineRelocation::getBB(MCE.getCurrentPCOffset(),
150 X86::reloc_pcrel_word, MBB));
151 MCE.emitWordLE(0);
152}
153
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000154/// emitGlobalAddress - Emit the specified address to the code stream assuming
155/// this is part of a "take the address of a global" instruction.
156///
Evan Cheng8ee6bab2007-12-22 09:40:20 +0000157void Emitter::emitGlobalAddress(GlobalValue *GV, unsigned Reloc,
158 int Disp /* = 0 */, intptr_t PCAdj /* = 0 */,
Evan Cheng28e7e162008-01-04 10:46:51 +0000159 bool NeedStub /* = false */,
160 bool isLazy /* = false */) {
161 intptr_t RelocCST = 0;
Evan Chengf0123872008-01-03 02:56:28 +0000162 if (Reloc == X86::reloc_picrel_word)
Evan Chengaf743252008-01-05 02:26:58 +0000163 RelocCST = PICBaseOffset;
Evan Cheng28e7e162008-01-04 10:46:51 +0000164 else if (Reloc == X86::reloc_pcrel_word)
165 RelocCST = PCAdj;
166 MachineRelocation MR = isLazy
167 ? MachineRelocation::getGVLazyPtr(MCE.getCurrentPCOffset(), Reloc,
168 GV, RelocCST, NeedStub)
169 : MachineRelocation::getGV(MCE.getCurrentPCOffset(), Reloc,
170 GV, RelocCST, NeedStub);
171 MCE.addRelocation(MR);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000172 if (Reloc == X86::reloc_absolute_dword)
173 MCE.emitWordLE(0);
174 MCE.emitWordLE(Disp); // The relocated value will be added to the displacement
175}
176
177/// emitExternalSymbolAddress - Arrange for the address of an external symbol to
178/// be emitted to the current location in the function, and allow it to be PC
179/// relative.
Evan Chengf0123872008-01-03 02:56:28 +0000180void Emitter::emitExternalSymbolAddress(const char *ES, unsigned Reloc) {
Evan Chengaf743252008-01-05 02:26:58 +0000181 intptr_t RelocCST = (Reloc == X86::reloc_picrel_word) ? PICBaseOffset : 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000182 MCE.addRelocation(MachineRelocation::getExtSym(MCE.getCurrentPCOffset(),
Evan Cheng28e7e162008-01-04 10:46:51 +0000183 Reloc, ES, RelocCST));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000184 if (Reloc == X86::reloc_absolute_dword)
185 MCE.emitWordLE(0);
186 MCE.emitWordLE(0);
187}
188
189/// emitConstPoolAddress - Arrange for the address of an constant pool
190/// to be emitted to the current location in the function, and allow it to be PC
191/// relative.
192void Emitter::emitConstPoolAddress(unsigned CPI, unsigned Reloc,
193 int Disp /* = 0 */,
Evan Chengf0123872008-01-03 02:56:28 +0000194 intptr_t PCAdj /* = 0 */) {
Evan Cheng28e7e162008-01-04 10:46:51 +0000195 intptr_t RelocCST = 0;
Evan Chengf0123872008-01-03 02:56:28 +0000196 if (Reloc == X86::reloc_picrel_word)
Evan Chengaf743252008-01-05 02:26:58 +0000197 RelocCST = PICBaseOffset;
Evan Cheng28e7e162008-01-04 10:46:51 +0000198 else if (Reloc == X86::reloc_pcrel_word)
199 RelocCST = PCAdj;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000200 MCE.addRelocation(MachineRelocation::getConstPool(MCE.getCurrentPCOffset(),
Evan Cheng28e7e162008-01-04 10:46:51 +0000201 Reloc, CPI, RelocCST));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000202 if (Reloc == X86::reloc_absolute_dword)
203 MCE.emitWordLE(0);
204 MCE.emitWordLE(Disp); // The relocated value will be added to the displacement
205}
206
207/// emitJumpTableAddress - Arrange for the address of a jump table to
208/// be emitted to the current location in the function, and allow it to be PC
209/// relative.
210void Emitter::emitJumpTableAddress(unsigned JTI, unsigned Reloc,
Evan Chengf0123872008-01-03 02:56:28 +0000211 intptr_t PCAdj /* = 0 */) {
Evan Cheng28e7e162008-01-04 10:46:51 +0000212 intptr_t RelocCST = 0;
Evan Chengf0123872008-01-03 02:56:28 +0000213 if (Reloc == X86::reloc_picrel_word)
Evan Chengaf743252008-01-05 02:26:58 +0000214 RelocCST = PICBaseOffset;
Evan Cheng28e7e162008-01-04 10:46:51 +0000215 else if (Reloc == X86::reloc_pcrel_word)
216 RelocCST = PCAdj;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000217 MCE.addRelocation(MachineRelocation::getJumpTable(MCE.getCurrentPCOffset(),
Evan Cheng28e7e162008-01-04 10:46:51 +0000218 Reloc, JTI, RelocCST));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000219 if (Reloc == X86::reloc_absolute_dword)
220 MCE.emitWordLE(0);
221 MCE.emitWordLE(0); // The relocated value will be added to the displacement
222}
223
Dan Gohman06844672008-02-08 03:29:40 +0000224unsigned Emitter::getX86RegNum(unsigned RegNo) const {
225 return ((const X86RegisterInfo&)II->getRegisterInfo()).getX86RegNum(RegNo);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000226}
227
228inline static unsigned char ModRMByte(unsigned Mod, unsigned RegOpcode,
229 unsigned RM) {
230 assert(Mod < 4 && RegOpcode < 8 && RM < 8 && "ModRM Fields out of range!");
231 return RM | (RegOpcode << 3) | (Mod << 6);
232}
233
234void Emitter::emitRegModRMByte(unsigned ModRMReg, unsigned RegOpcodeFld){
235 MCE.emitByte(ModRMByte(3, RegOpcodeFld, getX86RegNum(ModRMReg)));
236}
237
238void Emitter::emitSIBByte(unsigned SS, unsigned Index, unsigned Base) {
239 // SIB byte is in the same format as the ModRMByte...
240 MCE.emitByte(ModRMByte(SS, Index, Base));
241}
242
243void Emitter::emitConstant(uint64_t Val, unsigned Size) {
244 // Output the constant in little endian byte order...
245 for (unsigned i = 0; i != Size; ++i) {
246 MCE.emitByte(Val & 255);
247 Val >>= 8;
248 }
249}
250
251/// isDisp8 - Return true if this signed displacement fits in a 8-bit
252/// sign-extended field.
253static bool isDisp8(int Value) {
254 return Value == (signed char)Value;
255}
256
Evan Cheng28e7e162008-01-04 10:46:51 +0000257bool Emitter::gvNeedsLazyPtr(const GlobalValue *GV) {
258 return !Is64BitMode &&
259 TM.getSubtarget<X86Subtarget>().GVRequiresExtraLoad(GV, TM, false);
260}
261
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000262void Emitter::emitDisplacementField(const MachineOperand *RelocOp,
Evan Cheng8ee6bab2007-12-22 09:40:20 +0000263 int DispVal, intptr_t PCAdj) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000264 // If this is a simple integer displacement that doesn't require a relocation,
265 // emit it now.
266 if (!RelocOp) {
267 emitConstant(DispVal, 4);
268 return;
269 }
270
271 // Otherwise, this is something that requires a relocation. Emit it as such
272 // now.
273 if (RelocOp->isGlobalAddress()) {
274 // In 64-bit static small code model, we could potentially emit absolute.
275 // But it's probably not beneficial.
Bill Wendlingf3a655f2008-02-26 10:57:23 +0000276 // 89 05 00 00 00 00 mov %eax,0(%rip) # PC-relative
277 // 89 04 25 00 00 00 00 mov %eax,0x0 # Absolute
Evan Chengf0123872008-01-03 02:56:28 +0000278 unsigned rt = Is64BitMode ? X86::reloc_pcrel_word
Evan Cheng8ee6bab2007-12-22 09:40:20 +0000279 : (IsPIC ? X86::reloc_picrel_word : X86::reloc_absolute_word);
Evan Cheng28e7e162008-01-04 10:46:51 +0000280 bool NeedStub = isa<Function>(RelocOp->getGlobal());
281 bool isLazy = gvNeedsLazyPtr(RelocOp->getGlobal());
Evan Cheng8ee6bab2007-12-22 09:40:20 +0000282 emitGlobalAddress(RelocOp->getGlobal(), rt, RelocOp->getOffset(),
Evan Cheng28e7e162008-01-04 10:46:51 +0000283 PCAdj, NeedStub, isLazy);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000284 } else if (RelocOp->isConstantPoolIndex()) {
Evan Cheng8c872652008-01-02 23:38:59 +0000285 unsigned rt = Is64BitMode ? X86::reloc_pcrel_word : X86::reloc_picrel_word;
286 emitConstPoolAddress(RelocOp->getIndex(), rt,
Evan Chengf0123872008-01-03 02:56:28 +0000287 RelocOp->getOffset(), PCAdj);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000288 } else if (RelocOp->isJumpTableIndex()) {
Evan Cheng8c872652008-01-02 23:38:59 +0000289 unsigned rt = Is64BitMode ? X86::reloc_pcrel_word : X86::reloc_picrel_word;
Evan Chengf0123872008-01-03 02:56:28 +0000290 emitJumpTableAddress(RelocOp->getIndex(), rt, PCAdj);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000291 } else {
292 assert(0 && "Unknown value to relocate!");
293 }
294}
295
296void Emitter::emitMemModRMByte(const MachineInstr &MI,
297 unsigned Op, unsigned RegOpcodeField,
Evan Cheng8ee6bab2007-12-22 09:40:20 +0000298 intptr_t PCAdj) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000299 const MachineOperand &Op3 = MI.getOperand(Op+3);
300 int DispVal = 0;
301 const MachineOperand *DispForReloc = 0;
302
303 // Figure out what sort of displacement we have to handle here.
304 if (Op3.isGlobalAddress()) {
305 DispForReloc = &Op3;
306 } else if (Op3.isConstantPoolIndex()) {
Evan Cheng8c872652008-01-02 23:38:59 +0000307 if (Is64BitMode || IsPIC) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000308 DispForReloc = &Op3;
309 } else {
Chris Lattner6017d482007-12-30 23:10:15 +0000310 DispVal += MCE.getConstantPoolEntryAddress(Op3.getIndex());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000311 DispVal += Op3.getOffset();
312 }
313 } else if (Op3.isJumpTableIndex()) {
Evan Cheng8c872652008-01-02 23:38:59 +0000314 if (Is64BitMode || IsPIC) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000315 DispForReloc = &Op3;
316 } else {
Chris Lattner6017d482007-12-30 23:10:15 +0000317 DispVal += MCE.getJumpTableEntryAddress(Op3.getIndex());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000318 }
319 } else {
320 DispVal = Op3.getImm();
321 }
322
323 const MachineOperand &Base = MI.getOperand(Op);
324 const MachineOperand &Scale = MI.getOperand(Op+1);
325 const MachineOperand &IndexReg = MI.getOperand(Op+2);
326
327 unsigned BaseReg = Base.getReg();
328
329 // Is a SIB byte needed?
330 if (IndexReg.getReg() == 0 &&
331 (BaseReg == 0 || getX86RegNum(BaseReg) != N86::ESP)) {
332 if (BaseReg == 0) { // Just a displacement?
333 // Emit special case [disp32] encoding
334 MCE.emitByte(ModRMByte(0, RegOpcodeField, 5));
335
336 emitDisplacementField(DispForReloc, DispVal, PCAdj);
337 } else {
338 unsigned BaseRegNo = getX86RegNum(BaseReg);
339 if (!DispForReloc && DispVal == 0 && BaseRegNo != N86::EBP) {
340 // Emit simple indirect register encoding... [EAX] f.e.
341 MCE.emitByte(ModRMByte(0, RegOpcodeField, BaseRegNo));
342 } else if (!DispForReloc && isDisp8(DispVal)) {
343 // Emit the disp8 encoding... [REG+disp8]
344 MCE.emitByte(ModRMByte(1, RegOpcodeField, BaseRegNo));
345 emitConstant(DispVal, 1);
346 } else {
347 // Emit the most general non-SIB encoding: [REG+disp32]
348 MCE.emitByte(ModRMByte(2, RegOpcodeField, BaseRegNo));
349 emitDisplacementField(DispForReloc, DispVal, PCAdj);
350 }
351 }
352
353 } else { // We need a SIB byte, so start by outputting the ModR/M byte first
354 assert(IndexReg.getReg() != X86::ESP &&
355 IndexReg.getReg() != X86::RSP && "Cannot use ESP as index reg!");
356
357 bool ForceDisp32 = false;
358 bool ForceDisp8 = false;
359 if (BaseReg == 0) {
360 // If there is no base register, we emit the special case SIB byte with
361 // MOD=0, BASE=5, to JUST get the index, scale, and displacement.
362 MCE.emitByte(ModRMByte(0, RegOpcodeField, 4));
363 ForceDisp32 = true;
364 } else if (DispForReloc) {
365 // Emit the normal disp32 encoding.
366 MCE.emitByte(ModRMByte(2, RegOpcodeField, 4));
367 ForceDisp32 = true;
368 } else if (DispVal == 0 && getX86RegNum(BaseReg) != N86::EBP) {
369 // Emit no displacement ModR/M byte
370 MCE.emitByte(ModRMByte(0, RegOpcodeField, 4));
371 } else if (isDisp8(DispVal)) {
372 // Emit the disp8 encoding...
373 MCE.emitByte(ModRMByte(1, RegOpcodeField, 4));
374 ForceDisp8 = true; // Make sure to force 8 bit disp if Base=EBP
375 } else {
376 // Emit the normal disp32 encoding...
377 MCE.emitByte(ModRMByte(2, RegOpcodeField, 4));
378 }
379
380 // Calculate what the SS field value should be...
381 static const unsigned SSTable[] = { ~0, 0, 1, ~0, 2, ~0, ~0, ~0, 3 };
382 unsigned SS = SSTable[Scale.getImm()];
383
384 if (BaseReg == 0) {
385 // Handle the SIB byte for the case where there is no base. The
386 // displacement has already been output.
387 assert(IndexReg.getReg() && "Index register must be specified!");
388 emitSIBByte(SS, getX86RegNum(IndexReg.getReg()), 5);
389 } else {
390 unsigned BaseRegNo = getX86RegNum(BaseReg);
391 unsigned IndexRegNo;
392 if (IndexReg.getReg())
393 IndexRegNo = getX86RegNum(IndexReg.getReg());
394 else
395 IndexRegNo = 4; // For example [ESP+1*<noreg>+4]
396 emitSIBByte(SS, IndexRegNo, BaseRegNo);
397 }
398
399 // Do we need to output a displacement?
400 if (ForceDisp8) {
401 emitConstant(DispVal, 1);
402 } else if (DispVal != 0 || ForceDisp32) {
403 emitDisplacementField(DispForReloc, DispVal, PCAdj);
404 }
405 }
406}
407
Chris Lattner5b930372008-01-07 07:27:27 +0000408static unsigned sizeOfImm(const TargetInstrDesc *Desc) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000409 switch (Desc->TSFlags & X86II::ImmMask) {
410 case X86II::Imm8: return 1;
411 case X86II::Imm16: return 2;
412 case X86II::Imm32: return 4;
413 case X86II::Imm64: return 8;
414 default: assert(0 && "Immediate size not set!");
415 return 0;
416 }
417}
418
419/// isX86_64ExtendedReg - Is the MachineOperand a x86-64 extended register?
420/// e.g. r8, xmm8, etc.
421bool Emitter::isX86_64ExtendedReg(const MachineOperand &MO) {
422 if (!MO.isRegister()) return false;
Evan Chenge21ff432007-11-13 17:54:34 +0000423 switch (MO.getReg()) {
424 default: break;
425 case X86::R8: case X86::R9: case X86::R10: case X86::R11:
426 case X86::R12: case X86::R13: case X86::R14: case X86::R15:
427 case X86::R8D: case X86::R9D: case X86::R10D: case X86::R11D:
428 case X86::R12D: case X86::R13D: case X86::R14D: case X86::R15D:
429 case X86::R8W: case X86::R9W: case X86::R10W: case X86::R11W:
430 case X86::R12W: case X86::R13W: case X86::R14W: case X86::R15W:
431 case X86::R8B: case X86::R9B: case X86::R10B: case X86::R11B:
432 case X86::R12B: case X86::R13B: case X86::R14B: case X86::R15B:
433 case X86::XMM8: case X86::XMM9: case X86::XMM10: case X86::XMM11:
434 case X86::XMM12: case X86::XMM13: case X86::XMM14: case X86::XMM15:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000435 return true;
Evan Chenge21ff432007-11-13 17:54:34 +0000436 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000437 return false;
438}
439
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000440inline static bool isX86_64NonExtLowByteReg(unsigned reg) {
441 return (reg == X86::SPL || reg == X86::BPL ||
442 reg == X86::SIL || reg == X86::DIL);
443}
444
445/// determineREX - Determine if the MachineInstr has to be encoded with a X86-64
446/// REX prefix which specifies 1) 64-bit instructions, 2) non-default operand
447/// size, and 3) use of X86-64 extended registers.
448unsigned Emitter::determineREX(const MachineInstr &MI) {
449 unsigned REX = 0;
Chris Lattner5b930372008-01-07 07:27:27 +0000450 const TargetInstrDesc &Desc = MI.getDesc();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000451
452 // Pseudo instructions do not need REX prefix byte.
Chris Lattner5b930372008-01-07 07:27:27 +0000453 if ((Desc.TSFlags & X86II::FormMask) == X86II::Pseudo)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000454 return 0;
Chris Lattner5b930372008-01-07 07:27:27 +0000455 if (Desc.TSFlags & X86II::REX_W)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000456 REX |= 1 << 3;
457
Chris Lattner5b930372008-01-07 07:27:27 +0000458 unsigned NumOps = Desc.getNumOperands();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000459 if (NumOps) {
460 bool isTwoAddr = NumOps > 1 &&
Chris Lattner5b930372008-01-07 07:27:27 +0000461 Desc.getOperandConstraint(1, TOI::TIED_TO) != -1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000462
463 // If it accesses SPL, BPL, SIL, or DIL, then it requires a 0x40 REX prefix.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000464 unsigned i = isTwoAddr ? 1 : 0;
465 for (unsigned e = NumOps; i != e; ++i) {
466 const MachineOperand& MO = MI.getOperand(i);
467 if (MO.isRegister()) {
468 unsigned Reg = MO.getReg();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000469 if (isX86_64NonExtLowByteReg(Reg))
470 REX |= 0x40;
471 }
472 }
473
Chris Lattner5b930372008-01-07 07:27:27 +0000474 switch (Desc.TSFlags & X86II::FormMask) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000475 case X86II::MRMInitReg:
476 if (isX86_64ExtendedReg(MI.getOperand(0)))
477 REX |= (1 << 0) | (1 << 2);
478 break;
479 case X86II::MRMSrcReg: {
480 if (isX86_64ExtendedReg(MI.getOperand(0)))
481 REX |= 1 << 2;
482 i = isTwoAddr ? 2 : 1;
483 for (unsigned e = NumOps; i != e; ++i) {
484 const MachineOperand& MO = MI.getOperand(i);
485 if (isX86_64ExtendedReg(MO))
486 REX |= 1 << 0;
487 }
488 break;
489 }
490 case X86II::MRMSrcMem: {
491 if (isX86_64ExtendedReg(MI.getOperand(0)))
492 REX |= 1 << 2;
493 unsigned Bit = 0;
494 i = isTwoAddr ? 2 : 1;
495 for (; i != NumOps; ++i) {
496 const MachineOperand& MO = MI.getOperand(i);
497 if (MO.isRegister()) {
498 if (isX86_64ExtendedReg(MO))
499 REX |= 1 << Bit;
500 Bit++;
501 }
502 }
503 break;
504 }
505 case X86II::MRM0m: case X86II::MRM1m:
506 case X86II::MRM2m: case X86II::MRM3m:
507 case X86II::MRM4m: case X86II::MRM5m:
508 case X86II::MRM6m: case X86II::MRM7m:
509 case X86II::MRMDestMem: {
510 unsigned e = isTwoAddr ? 5 : 4;
511 i = isTwoAddr ? 1 : 0;
512 if (NumOps > e && isX86_64ExtendedReg(MI.getOperand(e)))
513 REX |= 1 << 2;
514 unsigned Bit = 0;
515 for (; i != e; ++i) {
516 const MachineOperand& MO = MI.getOperand(i);
517 if (MO.isRegister()) {
518 if (isX86_64ExtendedReg(MO))
519 REX |= 1 << Bit;
520 Bit++;
521 }
522 }
523 break;
524 }
525 default: {
526 if (isX86_64ExtendedReg(MI.getOperand(0)))
527 REX |= 1 << 0;
528 i = isTwoAddr ? 2 : 1;
529 for (unsigned e = NumOps; i != e; ++i) {
530 const MachineOperand& MO = MI.getOperand(i);
531 if (isX86_64ExtendedReg(MO))
532 REX |= 1 << 2;
533 }
534 break;
535 }
536 }
537 }
538 return REX;
539}
540
Evan Cheng0729ccf2008-01-05 00:41:47 +0000541void Emitter::emitInstruction(const MachineInstr &MI,
Chris Lattner5b930372008-01-07 07:27:27 +0000542 const TargetInstrDesc *Desc) {
Evan Cheng872bd4b2008-03-14 07:13:42 +0000543 DOUT << MI;
544
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000545 unsigned Opcode = Desc->Opcode;
546
Andrew Lenharth7a5a4b22008-03-01 13:37:02 +0000547 // Emit the lock opcode prefix as needed.
548 if (Desc->TSFlags & X86II::LOCK) MCE.emitByte(0xF0);
549
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000550 // Emit the repeat opcode prefix as needed.
551 if ((Desc->TSFlags & X86II::Op0Mask) == X86II::REP) MCE.emitByte(0xF3);
552
553 // Emit the operand size opcode prefix as needed.
554 if (Desc->TSFlags & X86II::OpSize) MCE.emitByte(0x66);
555
556 // Emit the address size opcode prefix as needed.
557 if (Desc->TSFlags & X86II::AdSize) MCE.emitByte(0x67);
558
559 bool Need0FPrefix = false;
560 switch (Desc->TSFlags & X86II::Op0Mask) {
561 case X86II::TB:
562 Need0FPrefix = true; // Two-byte opcode prefix
563 break;
564 case X86II::T8:
565 MCE.emitByte(0x0F);
566 MCE.emitByte(0x38);
567 break;
568 case X86II::TA:
569 MCE.emitByte(0x0F);
570 MCE.emitByte(0x3A);
571 break;
572 case X86II::REP: break; // already handled.
573 case X86II::XS: // F3 0F
574 MCE.emitByte(0xF3);
575 Need0FPrefix = true;
576 break;
577 case X86II::XD: // F2 0F
578 MCE.emitByte(0xF2);
579 Need0FPrefix = true;
580 break;
581 case X86II::D8: case X86II::D9: case X86II::DA: case X86II::DB:
582 case X86II::DC: case X86II::DD: case X86II::DE: case X86II::DF:
583 MCE.emitByte(0xD8+
584 (((Desc->TSFlags & X86II::Op0Mask)-X86II::D8)
585 >> X86II::Op0Shift));
586 break; // Two-byte opcode prefix
587 default: assert(0 && "Invalid prefix!");
588 case 0: break; // No prefix!
589 }
590
591 if (Is64BitMode) {
592 // REX prefix
593 unsigned REX = determineREX(MI);
594 if (REX)
595 MCE.emitByte(0x40 | REX);
596 }
597
598 // 0x0F escape code must be emitted just before the opcode.
599 if (Need0FPrefix)
600 MCE.emitByte(0x0F);
601
602 // If this is a two-address instruction, skip one of the register operands.
Chris Lattner0c2a4f32008-01-07 03:13:06 +0000603 unsigned NumOps = Desc->getNumOperands();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000604 unsigned CurOp = 0;
605 if (NumOps > 1 && Desc->getOperandConstraint(1, TOI::TIED_TO) != -1)
606 CurOp++;
607
608 unsigned char BaseOpcode = II->getBaseOpcodeFor(Desc);
609 switch (Desc->TSFlags & X86II::FormMask) {
610 default: assert(0 && "Unknown FormMask value in X86 MachineCodeEmitter!");
611 case X86II::Pseudo:
Evan Cheng0729ccf2008-01-05 00:41:47 +0000612 // Remember the current PC offset, this is the PIC relocation
613 // base address.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000614 switch (Opcode) {
615 default:
616 assert(0 && "psuedo instructions should be removed before code emission");
Evan Cheng7c6c35e2008-03-05 02:34:36 +0000617 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000618 case TargetInstrInfo::INLINEASM:
619 assert(0 && "JIT does not support inline asm!\n");
Evan Cheng7c6c35e2008-03-05 02:34:36 +0000620 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000621 case TargetInstrInfo::LABEL:
Nicolas Geoffray0e757e12008-02-13 18:39:37 +0000622 MCE.emitLabel(MI.getOperand(0).getImm());
623 break;
Evan Cheng7c6c35e2008-03-05 02:34:36 +0000624 case TargetInstrInfo::DECLARE:
625 case X86::DWARF_LOC:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000626 case X86::IMPLICIT_DEF_GR8:
627 case X86::IMPLICIT_DEF_GR16:
628 case X86::IMPLICIT_DEF_GR32:
629 case X86::IMPLICIT_DEF_GR64:
630 case X86::IMPLICIT_DEF_FR32:
631 case X86::IMPLICIT_DEF_FR64:
632 case X86::IMPLICIT_DEF_VR64:
633 case X86::IMPLICIT_DEF_VR128:
634 case X86::FP_REG_KILL:
635 break;
Evan Chengaf743252008-01-05 02:26:58 +0000636 case X86::MOVPC32r: {
Evan Cheng0729ccf2008-01-05 00:41:47 +0000637 // This emits the "call" portion of this pseudo instruction.
638 MCE.emitByte(BaseOpcode);
639 emitConstant(0, sizeOfImm(Desc));
Evan Chengaf743252008-01-05 02:26:58 +0000640 // Remember PIC base.
641 PICBaseOffset = MCE.getCurrentPCOffset();
642 X86JITInfo *JTI = dynamic_cast<X86JITInfo*>(TM.getJITInfo());
643 JTI->setPICBase(MCE.getCurrentPCValue());
Evan Cheng0729ccf2008-01-05 00:41:47 +0000644 break;
645 }
Evan Chengaf743252008-01-05 02:26:58 +0000646 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000647 CurOp = NumOps;
648 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000649 case X86II::RawFrm:
650 MCE.emitByte(BaseOpcode);
Evan Cheng0729ccf2008-01-05 00:41:47 +0000651
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000652 if (CurOp != NumOps) {
653 const MachineOperand &MO = MI.getOperand(CurOp++);
654 if (MO.isMachineBasicBlock()) {
Chris Lattner6017d482007-12-30 23:10:15 +0000655 emitPCRelativeBlockAddress(MO.getMBB());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000656 } else if (MO.isGlobalAddress()) {
Evan Chenge5e9fe82008-01-04 10:50:28 +0000657 bool NeedStub = (Is64BitMode && TM.getCodeModel() == CodeModel::Large)
658 || Opcode == X86::TAILJMPd;
Evan Cheng8ee6bab2007-12-22 09:40:20 +0000659 emitGlobalAddress(MO.getGlobal(), X86::reloc_pcrel_word,
Evan Chengf0123872008-01-03 02:56:28 +0000660 0, 0, NeedStub);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000661 } else if (MO.isExternalSymbol()) {
Evan Chengf0123872008-01-03 02:56:28 +0000662 emitExternalSymbolAddress(MO.getSymbolName(), X86::reloc_pcrel_word);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000663 } else if (MO.isImmediate()) {
664 emitConstant(MO.getImm(), sizeOfImm(Desc));
665 } else {
666 assert(0 && "Unknown RawFrm operand!");
667 }
668 }
669 break;
670
671 case X86II::AddRegFrm:
672 MCE.emitByte(BaseOpcode + getX86RegNum(MI.getOperand(CurOp++).getReg()));
673
674 if (CurOp != NumOps) {
675 const MachineOperand &MO1 = MI.getOperand(CurOp++);
676 unsigned Size = sizeOfImm(Desc);
677 if (MO1.isImmediate())
678 emitConstant(MO1.getImm(), Size);
679 else {
Evan Cheng8ee6bab2007-12-22 09:40:20 +0000680 unsigned rt = Is64BitMode ? X86::reloc_pcrel_word
681 : (IsPIC ? X86::reloc_picrel_word : X86::reloc_absolute_word);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000682 if (Opcode == X86::MOV64ri)
683 rt = X86::reloc_absolute_dword; // FIXME: add X86II flag?
Evan Chengf0123872008-01-03 02:56:28 +0000684 if (MO1.isGlobalAddress()) {
Evan Cheng28e7e162008-01-04 10:46:51 +0000685 bool NeedStub = isa<Function>(MO1.getGlobal());
686 bool isLazy = gvNeedsLazyPtr(MO1.getGlobal());
687 emitGlobalAddress(MO1.getGlobal(), rt, MO1.getOffset(), 0,
688 NeedStub, isLazy);
Evan Chengf0123872008-01-03 02:56:28 +0000689 } else if (MO1.isExternalSymbol())
690 emitExternalSymbolAddress(MO1.getSymbolName(), rt);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000691 else if (MO1.isConstantPoolIndex())
Evan Chengf0123872008-01-03 02:56:28 +0000692 emitConstPoolAddress(MO1.getIndex(), rt);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000693 else if (MO1.isJumpTableIndex())
Evan Chengf0123872008-01-03 02:56:28 +0000694 emitJumpTableAddress(MO1.getIndex(), rt);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000695 }
696 }
697 break;
698
699 case X86II::MRMDestReg: {
700 MCE.emitByte(BaseOpcode);
701 emitRegModRMByte(MI.getOperand(CurOp).getReg(),
702 getX86RegNum(MI.getOperand(CurOp+1).getReg()));
703 CurOp += 2;
704 if (CurOp != NumOps)
705 emitConstant(MI.getOperand(CurOp++).getImm(), sizeOfImm(Desc));
706 break;
707 }
708 case X86II::MRMDestMem: {
709 MCE.emitByte(BaseOpcode);
710 emitMemModRMByte(MI, CurOp, getX86RegNum(MI.getOperand(CurOp+4).getReg()));
711 CurOp += 5;
712 if (CurOp != NumOps)
713 emitConstant(MI.getOperand(CurOp++).getImm(), sizeOfImm(Desc));
714 break;
715 }
716
717 case X86II::MRMSrcReg:
718 MCE.emitByte(BaseOpcode);
719 emitRegModRMByte(MI.getOperand(CurOp+1).getReg(),
720 getX86RegNum(MI.getOperand(CurOp).getReg()));
721 CurOp += 2;
722 if (CurOp != NumOps)
723 emitConstant(MI.getOperand(CurOp++).getImm(), sizeOfImm(Desc));
724 break;
725
726 case X86II::MRMSrcMem: {
Evan Cheng8ee6bab2007-12-22 09:40:20 +0000727 intptr_t PCAdj = (CurOp+5 != NumOps) ? sizeOfImm(Desc) : 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000728
729 MCE.emitByte(BaseOpcode);
730 emitMemModRMByte(MI, CurOp+1, getX86RegNum(MI.getOperand(CurOp).getReg()),
731 PCAdj);
732 CurOp += 5;
733 if (CurOp != NumOps)
734 emitConstant(MI.getOperand(CurOp++).getImm(), sizeOfImm(Desc));
735 break;
736 }
737
738 case X86II::MRM0r: case X86II::MRM1r:
739 case X86II::MRM2r: case X86II::MRM3r:
740 case X86II::MRM4r: case X86II::MRM5r:
741 case X86II::MRM6r: case X86II::MRM7r:
742 MCE.emitByte(BaseOpcode);
743 emitRegModRMByte(MI.getOperand(CurOp++).getReg(),
744 (Desc->TSFlags & X86II::FormMask)-X86II::MRM0r);
745
746 if (CurOp != NumOps) {
747 const MachineOperand &MO1 = MI.getOperand(CurOp++);
748 unsigned Size = sizeOfImm(Desc);
749 if (MO1.isImmediate())
750 emitConstant(MO1.getImm(), Size);
751 else {
752 unsigned rt = Is64BitMode ? X86::reloc_pcrel_word
Evan Cheng8ee6bab2007-12-22 09:40:20 +0000753 : (IsPIC ? X86::reloc_picrel_word : X86::reloc_absolute_word);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000754 if (Opcode == X86::MOV64ri32)
755 rt = X86::reloc_absolute_word; // FIXME: add X86II flag?
Evan Chengf0123872008-01-03 02:56:28 +0000756 if (MO1.isGlobalAddress()) {
Evan Cheng28e7e162008-01-04 10:46:51 +0000757 bool NeedStub = isa<Function>(MO1.getGlobal());
758 bool isLazy = gvNeedsLazyPtr(MO1.getGlobal());
759 emitGlobalAddress(MO1.getGlobal(), rt, MO1.getOffset(), 0,
760 NeedStub, isLazy);
Evan Chengf0123872008-01-03 02:56:28 +0000761 } else if (MO1.isExternalSymbol())
762 emitExternalSymbolAddress(MO1.getSymbolName(), rt);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000763 else if (MO1.isConstantPoolIndex())
Evan Chengf0123872008-01-03 02:56:28 +0000764 emitConstPoolAddress(MO1.getIndex(), rt);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000765 else if (MO1.isJumpTableIndex())
Evan Chengf0123872008-01-03 02:56:28 +0000766 emitJumpTableAddress(MO1.getIndex(), rt);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000767 }
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: {
Evan Cheng8ee6bab2007-12-22 09:40:20 +0000775 intptr_t PCAdj = (CurOp+4 != NumOps) ?
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000776 (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
Evan Cheng8ee6bab2007-12-22 09:40:20 +0000790 : (IsPIC ? X86::reloc_picrel_word : X86::reloc_absolute_word);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000791 if (Opcode == X86::MOV64mi32)
792 rt = X86::reloc_absolute_word; // FIXME: add X86II flag?
Evan Chengf0123872008-01-03 02:56:28 +0000793 if (MO.isGlobalAddress()) {
Evan Cheng28e7e162008-01-04 10:46:51 +0000794 bool NeedStub = isa<Function>(MO.getGlobal());
795 bool isLazy = gvNeedsLazyPtr(MO.getGlobal());
796 emitGlobalAddress(MO.getGlobal(), rt, MO.getOffset(), 0,
797 NeedStub, isLazy);
Evan Chengf0123872008-01-03 02:56:28 +0000798 } else if (MO.isExternalSymbol())
799 emitExternalSymbolAddress(MO.getSymbolName(), rt);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000800 else if (MO.isConstantPoolIndex())
Evan Chengf0123872008-01-03 02:56:28 +0000801 emitConstPoolAddress(MO.getIndex(), rt);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000802 else if (MO.isJumpTableIndex())
Evan Chengf0123872008-01-03 02:56:28 +0000803 emitJumpTableAddress(MO.getIndex(), rt);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000804 }
805 }
806 break;
807 }
808
809 case X86II::MRMInitReg:
810 MCE.emitByte(BaseOpcode);
811 // Duplicate register, used by things like MOV8r0 (aka xor reg,reg).
812 emitRegModRMByte(MI.getOperand(CurOp).getReg(),
813 getX86RegNum(MI.getOperand(CurOp).getReg()));
814 ++CurOp;
815 break;
816 }
817
Evan Cheng6032b652008-03-05 02:08:03 +0000818 if (!Desc->isVariadic() && CurOp != NumOps) {
819 cerr << "Cannot encode: ";
820 MI.dump();
821 cerr << '\n';
822 abort();
823 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000824}