blob: 6367623a01a05524a94afeaac86b330274a0d338 [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"
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +000024#include "llvm/CodeGen/JITCodeEmitter.h"
Bruno Cardoso Lopesaabb9a52009-07-06 05:09:34 +000025#include "llvm/CodeGen/ObjectCodeEmitter.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000026#include "llvm/CodeGen/MachineFunctionPass.h"
27#include "llvm/CodeGen/MachineInstr.h"
Nicolas Geoffray0e757e12008-02-13 18:39:37 +000028#include "llvm/CodeGen/MachineModuleInfo.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000029#include "llvm/CodeGen/Passes.h"
30#include "llvm/Function.h"
31#include "llvm/ADT/Statistic.h"
Daniel Dunbar2f379632009-08-27 08:12:55 +000032#include "llvm/MC/MCCodeEmitter.h"
Daniel Dunbar6e966212009-08-31 08:08:38 +000033#include "llvm/MC/MCExpr.h"
Daniel Dunbar2f379632009-08-27 08:12:55 +000034#include "llvm/MC/MCInst.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000035#include "llvm/Support/Compiler.h"
Evan Cheng872bd4b2008-03-14 07:13:42 +000036#include "llvm/Support/Debug.h"
Edwin Török3cb88482009-07-08 18:01:40 +000037#include "llvm/Support/ErrorHandling.h"
Daniel Dunbar005975c2009-07-25 00:23:56 +000038#include "llvm/Support/raw_ostream.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000039#include "llvm/Target/TargetOptions.h"
40using namespace llvm;
41
42STATISTIC(NumEmitted, "Number of machine instructions emitted");
43
44namespace {
Chris Lattner5b6b1782009-08-16 02:45:18 +000045 template<class CodeEmitter>
Dan Gohmanf17a25c2007-07-18 16:29:46 +000046 class VISIBILITY_HIDDEN Emitter : public MachineFunctionPass {
47 const X86InstrInfo *II;
48 const TargetData *TD;
Dan Gohmanb41dfba2008-05-14 01:58:56 +000049 X86TargetMachine &TM;
Bruno Cardoso Lopes8e2537b2009-06-01 19:57:37 +000050 CodeEmitter &MCE;
Evan Chengaf743252008-01-05 02:26:58 +000051 intptr_t PICBaseOffset;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000052 bool Is64BitMode;
Evan Cheng8ee6bab2007-12-22 09:40:20 +000053 bool IsPIC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000054 public:
55 static char ID;
Bruno Cardoso Lopes8e2537b2009-06-01 19:57:37 +000056 explicit Emitter(X86TargetMachine &tm, CodeEmitter &mce)
Dan Gohman26f8c272008-09-04 17:05:41 +000057 : MachineFunctionPass(&ID), II(0), TD(0), TM(tm),
Evan Chengaf743252008-01-05 02:26:58 +000058 MCE(mce), PICBaseOffset(0), Is64BitMode(false),
Evan Cheng28e7e162008-01-04 10:46:51 +000059 IsPIC(TM.getRelocationModel() == Reloc::PIC_) {}
Bruno Cardoso Lopes8e2537b2009-06-01 19:57:37 +000060 Emitter(X86TargetMachine &tm, CodeEmitter &mce,
Dan Gohmanf17a25c2007-07-18 16:29:46 +000061 const X86InstrInfo &ii, const TargetData &td, bool is64)
Dan Gohman26f8c272008-09-04 17:05:41 +000062 : MachineFunctionPass(&ID), II(&ii), TD(&td), TM(tm),
Evan Chengaf743252008-01-05 02:26:58 +000063 MCE(mce), PICBaseOffset(0), Is64BitMode(is64),
Evan Cheng28e7e162008-01-04 10:46:51 +000064 IsPIC(TM.getRelocationModel() == Reloc::PIC_) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +000065
66 bool runOnMachineFunction(MachineFunction &MF);
67
68 virtual const char *getPassName() const {
69 return "X86 Machine Code Emitter";
70 }
71
Evan Cheng0729ccf2008-01-05 00:41:47 +000072 void emitInstruction(const MachineInstr &MI,
Chris Lattner5b930372008-01-07 07:27:27 +000073 const TargetInstrDesc *Desc);
Nicolas Geoffray0e757e12008-02-13 18:39:37 +000074
75 void getAnalysisUsage(AnalysisUsage &AU) const {
Dan Gohmanf5f72242009-07-31 23:44:16 +000076 AU.setPreservesAll();
Nicolas Geoffray0e757e12008-02-13 18:39:37 +000077 AU.addRequired<MachineModuleInfo>();
78 MachineFunctionPass::getAnalysisUsage(AU);
79 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +000080
81 private:
82 void emitPCRelativeBlockAddress(MachineBasicBlock *MBB);
Evan Cheng8ee6bab2007-12-22 09:40:20 +000083 void emitGlobalAddress(GlobalValue *GV, unsigned Reloc,
Dan Gohman5ad09472008-10-24 01:57:54 +000084 intptr_t Disp = 0, intptr_t PCAdj = 0,
Evan Cheng8af22c42008-11-10 01:08:07 +000085 bool NeedStub = false, bool Indirect = false);
Evan Chengf0123872008-01-03 02:56:28 +000086 void emitExternalSymbolAddress(const char *ES, unsigned Reloc);
Dan Gohman5ad09472008-10-24 01:57:54 +000087 void emitConstPoolAddress(unsigned CPI, unsigned Reloc, intptr_t Disp = 0,
Evan Chengf0123872008-01-03 02:56:28 +000088 intptr_t PCAdj = 0);
Evan Cheng8ee6bab2007-12-22 09:40:20 +000089 void emitJumpTableAddress(unsigned JTI, unsigned Reloc,
Evan Chengf0123872008-01-03 02:56:28 +000090 intptr_t PCAdj = 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000091
92 void emitDisplacementField(const MachineOperand *RelocOp, int DispVal,
Bruno Cardoso Lopes670400e2009-08-05 00:11:21 +000093 intptr_t Adj = 0, bool IsPCRel = true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000094
95 void emitRegModRMByte(unsigned ModRMReg, unsigned RegOpcodeField);
Evan Cheng5d0d34e2008-10-17 17:14:20 +000096 void emitRegModRMByte(unsigned RegOpcodeField);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000097 void emitSIBByte(unsigned SS, unsigned Index, unsigned Base);
98 void emitConstant(uint64_t Val, unsigned Size);
99
100 void emitMemModRMByte(const MachineInstr &MI,
101 unsigned Op, unsigned RegOpcodeField,
Evan Cheng8ee6bab2007-12-22 09:40:20 +0000102 intptr_t PCAdj = 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000103
Dan Gohman06844672008-02-08 03:29:40 +0000104 unsigned getX86RegNum(unsigned RegNo) const;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000105 };
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +0000106
Bruno Cardoso Lopes8e2537b2009-06-01 19:57:37 +0000107template<class CodeEmitter>
108 char Emitter<CodeEmitter>::ID = 0;
Chris Lattner5b6b1782009-08-16 02:45:18 +0000109} // end anonymous namespace.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000110
111/// createX86CodeEmitterPass - Return a pass that emits the collected X86 code
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +0000112/// to the specified templated MachineCodeEmitter object.
113
Bruno Cardoso Lopesaabb9a52009-07-06 05:09:34 +0000114FunctionPass *llvm::createX86CodeEmitterPass(X86TargetMachine &TM,
115 MachineCodeEmitter &MCE) {
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +0000116 return new Emitter<MachineCodeEmitter>(TM, MCE);
117}
Bruno Cardoso Lopesaabb9a52009-07-06 05:09:34 +0000118FunctionPass *llvm::createX86JITCodeEmitterPass(X86TargetMachine &TM,
119 JITCodeEmitter &JCE) {
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +0000120 return new Emitter<JITCodeEmitter>(TM, JCE);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000121}
Bruno Cardoso Lopesaabb9a52009-07-06 05:09:34 +0000122FunctionPass *llvm::createX86ObjectCodeEmitterPass(X86TargetMachine &TM,
123 ObjectCodeEmitter &OCE) {
124 return new Emitter<ObjectCodeEmitter>(TM, OCE);
125}
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +0000126
Bruno Cardoso Lopes8e2537b2009-06-01 19:57:37 +0000127template<class CodeEmitter>
128bool Emitter<CodeEmitter>::runOnMachineFunction(MachineFunction &MF) {
Dale Johannesenc501c082008-08-11 23:46:25 +0000129
Nicolas Geoffray0e757e12008-02-13 18:39:37 +0000130 MCE.setModuleInfo(&getAnalysis<MachineModuleInfo>());
131
Dan Gohmanb41dfba2008-05-14 01:58:56 +0000132 II = TM.getInstrInfo();
133 TD = TM.getTargetData();
Evan Cheng28e7e162008-01-04 10:46:51 +0000134 Is64BitMode = TM.getSubtarget<X86Subtarget>().is64Bit();
Evan Chengae50ca32008-05-20 01:56:59 +0000135 IsPIC = TM.getRelocationModel() == Reloc::PIC_;
Nicolas Geoffray0e757e12008-02-13 18:39:37 +0000136
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000137 do {
Daniel Dunbar005975c2009-07-25 00:23:56 +0000138 DEBUG(errs() << "JITTing function '"
139 << MF.getFunction()->getName() << "'\n");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000140 MCE.startFunction(MF);
141 for (MachineFunction::iterator MBB = MF.begin(), E = MF.end();
142 MBB != E; ++MBB) {
143 MCE.StartMachineBasicBlock(MBB);
144 for (MachineBasicBlock::const_iterator I = MBB->begin(), E = MBB->end();
Evan Cheng0729ccf2008-01-05 00:41:47 +0000145 I != E; ++I) {
Chris Lattner5b930372008-01-07 07:27:27 +0000146 const TargetInstrDesc &Desc = I->getDesc();
147 emitInstruction(*I, &Desc);
Evan Cheng0729ccf2008-01-05 00:41:47 +0000148 // MOVPC32r is basically a call plus a pop instruction.
Chris Lattner5b930372008-01-07 07:27:27 +0000149 if (Desc.getOpcode() == X86::MOVPC32r)
Evan Cheng0729ccf2008-01-05 00:41:47 +0000150 emitInstruction(*I, &II->get(X86::POP32r));
151 NumEmitted++; // Keep track of the # of mi's emitted
152 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000153 }
154 } while (MCE.finishFunction(MF));
155
156 return false;
157}
158
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000159/// emitPCRelativeBlockAddress - This method keeps track of the information
160/// necessary to resolve the address of this block later and emits a dummy
161/// value.
162///
Bruno Cardoso Lopes8e2537b2009-06-01 19:57:37 +0000163template<class CodeEmitter>
164void Emitter<CodeEmitter>::emitPCRelativeBlockAddress(MachineBasicBlock *MBB) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000165 // Remember where this reference was and where it is to so we can
166 // deal with it later.
167 MCE.addRelocation(MachineRelocation::getBB(MCE.getCurrentPCOffset(),
168 X86::reloc_pcrel_word, MBB));
169 MCE.emitWordLE(0);
170}
171
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000172/// emitGlobalAddress - Emit the specified address to the code stream assuming
173/// this is part of a "take the address of a global" instruction.
174///
Bruno Cardoso Lopes8e2537b2009-06-01 19:57:37 +0000175template<class CodeEmitter>
176void Emitter<CodeEmitter>::emitGlobalAddress(GlobalValue *GV, unsigned Reloc,
Dan Gohman5ad09472008-10-24 01:57:54 +0000177 intptr_t Disp /* = 0 */,
178 intptr_t PCAdj /* = 0 */,
Evan Cheng28e7e162008-01-04 10:46:51 +0000179 bool NeedStub /* = false */,
Evan Cheng8af22c42008-11-10 01:08:07 +0000180 bool Indirect /* = false */) {
Bruno Cardoso Lopes670400e2009-08-05 00:11:21 +0000181 intptr_t RelocCST = Disp;
Evan Chengf0123872008-01-03 02:56:28 +0000182 if (Reloc == X86::reloc_picrel_word)
Evan Chengaf743252008-01-05 02:26:58 +0000183 RelocCST = PICBaseOffset;
Evan Cheng28e7e162008-01-04 10:46:51 +0000184 else if (Reloc == X86::reloc_pcrel_word)
185 RelocCST = PCAdj;
Evan Cheng8af22c42008-11-10 01:08:07 +0000186 MachineRelocation MR = Indirect
187 ? MachineRelocation::getIndirectSymbol(MCE.getCurrentPCOffset(), Reloc,
188 GV, RelocCST, NeedStub)
Evan Cheng28e7e162008-01-04 10:46:51 +0000189 : MachineRelocation::getGV(MCE.getCurrentPCOffset(), Reloc,
190 GV, RelocCST, NeedStub);
191 MCE.addRelocation(MR);
Dan Gohman5ad09472008-10-24 01:57:54 +0000192 // The relocated value will be added to the displacement
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000193 if (Reloc == X86::reloc_absolute_dword)
Dan Gohman5ad09472008-10-24 01:57:54 +0000194 MCE.emitDWordLE(Disp);
195 else
196 MCE.emitWordLE((int32_t)Disp);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000197}
198
199/// emitExternalSymbolAddress - Arrange for the address of an external symbol to
200/// be emitted to the current location in the function, and allow it to be PC
201/// relative.
Bruno Cardoso Lopes8e2537b2009-06-01 19:57:37 +0000202template<class CodeEmitter>
203void Emitter<CodeEmitter>::emitExternalSymbolAddress(const char *ES,
204 unsigned Reloc) {
Evan Chengaf743252008-01-05 02:26:58 +0000205 intptr_t RelocCST = (Reloc == X86::reloc_picrel_word) ? PICBaseOffset : 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000206 MCE.addRelocation(MachineRelocation::getExtSym(MCE.getCurrentPCOffset(),
Evan Cheng28e7e162008-01-04 10:46:51 +0000207 Reloc, ES, RelocCST));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000208 if (Reloc == X86::reloc_absolute_dword)
Dan Gohman5ad09472008-10-24 01:57:54 +0000209 MCE.emitDWordLE(0);
210 else
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000211 MCE.emitWordLE(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000212}
213
214/// emitConstPoolAddress - Arrange for the address of an constant pool
215/// to be emitted to the current location in the function, and allow it to be PC
216/// relative.
Bruno Cardoso Lopes8e2537b2009-06-01 19:57:37 +0000217template<class CodeEmitter>
218void Emitter<CodeEmitter>::emitConstPoolAddress(unsigned CPI, unsigned Reloc,
Dan Gohman5ad09472008-10-24 01:57:54 +0000219 intptr_t Disp /* = 0 */,
Evan Chengf0123872008-01-03 02:56:28 +0000220 intptr_t PCAdj /* = 0 */) {
Evan Cheng28e7e162008-01-04 10:46:51 +0000221 intptr_t RelocCST = 0;
Evan Chengf0123872008-01-03 02:56:28 +0000222 if (Reloc == X86::reloc_picrel_word)
Evan Chengaf743252008-01-05 02:26:58 +0000223 RelocCST = PICBaseOffset;
Evan Cheng28e7e162008-01-04 10:46:51 +0000224 else if (Reloc == X86::reloc_pcrel_word)
225 RelocCST = PCAdj;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000226 MCE.addRelocation(MachineRelocation::getConstPool(MCE.getCurrentPCOffset(),
Evan Cheng28e7e162008-01-04 10:46:51 +0000227 Reloc, CPI, RelocCST));
Dan Gohman5ad09472008-10-24 01:57:54 +0000228 // The relocated value will be added to the displacement
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000229 if (Reloc == X86::reloc_absolute_dword)
Dan Gohman5ad09472008-10-24 01:57:54 +0000230 MCE.emitDWordLE(Disp);
231 else
232 MCE.emitWordLE((int32_t)Disp);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000233}
234
235/// emitJumpTableAddress - Arrange for the address of a jump table to
236/// be emitted to the current location in the function, and allow it to be PC
237/// relative.
Bruno Cardoso Lopes8e2537b2009-06-01 19:57:37 +0000238template<class CodeEmitter>
239void Emitter<CodeEmitter>::emitJumpTableAddress(unsigned JTI, unsigned Reloc,
Evan Chengf0123872008-01-03 02:56:28 +0000240 intptr_t PCAdj /* = 0 */) {
Evan Cheng28e7e162008-01-04 10:46:51 +0000241 intptr_t RelocCST = 0;
Evan Chengf0123872008-01-03 02:56:28 +0000242 if (Reloc == X86::reloc_picrel_word)
Evan Chengaf743252008-01-05 02:26:58 +0000243 RelocCST = PICBaseOffset;
Evan Cheng28e7e162008-01-04 10:46:51 +0000244 else if (Reloc == X86::reloc_pcrel_word)
245 RelocCST = PCAdj;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000246 MCE.addRelocation(MachineRelocation::getJumpTable(MCE.getCurrentPCOffset(),
Evan Cheng28e7e162008-01-04 10:46:51 +0000247 Reloc, JTI, RelocCST));
Dan Gohman5ad09472008-10-24 01:57:54 +0000248 // The relocated value will be added to the displacement
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000249 if (Reloc == X86::reloc_absolute_dword)
Dan Gohman5ad09472008-10-24 01:57:54 +0000250 MCE.emitDWordLE(0);
251 else
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000252 MCE.emitWordLE(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000253}
254
Bruno Cardoso Lopes8e2537b2009-06-01 19:57:37 +0000255template<class CodeEmitter>
256unsigned Emitter<CodeEmitter>::getX86RegNum(unsigned RegNo) const {
Dan Gohmanb41dfba2008-05-14 01:58:56 +0000257 return II->getRegisterInfo().getX86RegNum(RegNo);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000258}
259
260inline static unsigned char ModRMByte(unsigned Mod, unsigned RegOpcode,
261 unsigned RM) {
262 assert(Mod < 4 && RegOpcode < 8 && RM < 8 && "ModRM Fields out of range!");
263 return RM | (RegOpcode << 3) | (Mod << 6);
264}
265
Bruno Cardoso Lopes8e2537b2009-06-01 19:57:37 +0000266template<class CodeEmitter>
267void Emitter<CodeEmitter>::emitRegModRMByte(unsigned ModRMReg,
268 unsigned RegOpcodeFld){
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000269 MCE.emitByte(ModRMByte(3, RegOpcodeFld, getX86RegNum(ModRMReg)));
270}
271
Bruno Cardoso Lopes8e2537b2009-06-01 19:57:37 +0000272template<class CodeEmitter>
273void Emitter<CodeEmitter>::emitRegModRMByte(unsigned RegOpcodeFld) {
Evan Cheng5d0d34e2008-10-17 17:14:20 +0000274 MCE.emitByte(ModRMByte(3, RegOpcodeFld, 0));
275}
276
Bruno Cardoso Lopes8e2537b2009-06-01 19:57:37 +0000277template<class CodeEmitter>
278void Emitter<CodeEmitter>::emitSIBByte(unsigned SS,
279 unsigned Index,
280 unsigned Base) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000281 // SIB byte is in the same format as the ModRMByte...
282 MCE.emitByte(ModRMByte(SS, Index, Base));
283}
284
Bruno Cardoso Lopes8e2537b2009-06-01 19:57:37 +0000285template<class CodeEmitter>
286void Emitter<CodeEmitter>::emitConstant(uint64_t Val, unsigned Size) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000287 // Output the constant in little endian byte order...
288 for (unsigned i = 0; i != Size; ++i) {
289 MCE.emitByte(Val & 255);
290 Val >>= 8;
291 }
292}
293
294/// isDisp8 - Return true if this signed displacement fits in a 8-bit
295/// sign-extended field.
296static bool isDisp8(int Value) {
297 return Value == (signed char)Value;
298}
299
Chris Lattnerbdc2ea92009-07-10 05:27:43 +0000300static bool gvNeedsNonLazyPtr(const MachineOperand &GVOp,
301 const TargetMachine &TM) {
Chris Lattnerbdc2ea92009-07-10 05:27:43 +0000302 // For Darwin-64, simulate the linktime GOT by using the same non-lazy-pointer
Dale Johannesen2b65b742008-08-12 18:23:48 +0000303 // mechanism as 32-bit mode.
Chris Lattnerbdc2ea92009-07-10 05:27:43 +0000304 if (TM.getSubtarget<X86Subtarget>().is64Bit() &&
305 !TM.getSubtarget<X86Subtarget>().isTargetDarwin())
306 return false;
307
Chris Lattner8b1d2b92009-07-10 06:07:08 +0000308 // Return true if this is a reference to a stub containing the address of the
309 // global, not the global itself.
Chris Lattner6d62ab92009-07-10 06:29:59 +0000310 return isGlobalStubReference(GVOp.getTargetFlags());
Evan Cheng28e7e162008-01-04 10:46:51 +0000311}
312
Bruno Cardoso Lopes8e2537b2009-06-01 19:57:37 +0000313template<class CodeEmitter>
314void Emitter<CodeEmitter>::emitDisplacementField(const MachineOperand *RelocOp,
Bruno Cardoso Lopes670400e2009-08-05 00:11:21 +0000315 int DispVal,
316 intptr_t Adj /* = 0 */,
317 bool IsPCRel /* = true */) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000318 // If this is a simple integer displacement that doesn't require a relocation,
319 // emit it now.
320 if (!RelocOp) {
321 emitConstant(DispVal, 4);
322 return;
323 }
Bruno Cardoso Lopes670400e2009-08-05 00:11:21 +0000324
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000325 // Otherwise, this is something that requires a relocation. Emit it as such
326 // now.
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000327 if (RelocOp->isGlobal()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000328 // In 64-bit static small code model, we could potentially emit absolute.
Bruno Cardoso Lopes670400e2009-08-05 00:11:21 +0000329 // But it's probably not beneficial. If the MCE supports using RIP directly
330 // do it, otherwise fallback to absolute (this is determined by IsPCRel).
Bill Wendlingf3a655f2008-02-26 10:57:23 +0000331 // 89 05 00 00 00 00 mov %eax,0(%rip) # PC-relative
332 // 89 04 25 00 00 00 00 mov %eax,0x0 # Absolute
Bruno Cardoso Lopes670400e2009-08-05 00:11:21 +0000333 unsigned rt = Is64BitMode ?
334 (IsPCRel ? X86::reloc_pcrel_word : X86::reloc_absolute_word_sext)
Evan Cheng8ee6bab2007-12-22 09:40:20 +0000335 : (IsPIC ? X86::reloc_picrel_word : X86::reloc_absolute_word);
Evan Cheng28e7e162008-01-04 10:46:51 +0000336 bool NeedStub = isa<Function>(RelocOp->getGlobal());
Chris Lattnerbdc2ea92009-07-10 05:27:43 +0000337 bool Indirect = gvNeedsNonLazyPtr(*RelocOp, TM);
Evan Cheng8ee6bab2007-12-22 09:40:20 +0000338 emitGlobalAddress(RelocOp->getGlobal(), rt, RelocOp->getOffset(),
Bruno Cardoso Lopes670400e2009-08-05 00:11:21 +0000339 Adj, NeedStub, Indirect);
Daniel Dunbar8ac6c042009-09-01 22:06:53 +0000340 } else if (RelocOp->isSymbol()) {
341 unsigned rt = Is64BitMode ?
342 (IsPCRel ? X86::reloc_pcrel_word : X86::reloc_absolute_word_sext)
343 : (IsPCRel ? X86::reloc_picrel_word : X86::reloc_absolute_word);
344 emitExternalSymbolAddress(RelocOp->getSymbolName(), rt);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000345 } else if (RelocOp->isCPI()) {
Bruno Cardoso Lopes670400e2009-08-05 00:11:21 +0000346 unsigned rt = Is64BitMode ?
347 (IsPCRel ? X86::reloc_pcrel_word : X86::reloc_absolute_word_sext)
348 : (IsPCRel ? X86::reloc_picrel_word : X86::reloc_absolute_word);
Evan Cheng8c872652008-01-02 23:38:59 +0000349 emitConstPoolAddress(RelocOp->getIndex(), rt,
Bruno Cardoso Lopes670400e2009-08-05 00:11:21 +0000350 RelocOp->getOffset(), Adj);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000351 } else if (RelocOp->isJTI()) {
Bruno Cardoso Lopes670400e2009-08-05 00:11:21 +0000352 unsigned rt = Is64BitMode ?
353 (IsPCRel ? X86::reloc_pcrel_word : X86::reloc_absolute_word_sext)
354 : (IsPCRel ? X86::reloc_picrel_word : X86::reloc_absolute_word);
355 emitJumpTableAddress(RelocOp->getIndex(), rt, Adj);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000356 } else {
Edwin Törökbd448e32009-07-14 16:55:14 +0000357 llvm_unreachable("Unknown value to relocate!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000358 }
359}
360
Bruno Cardoso Lopes8e2537b2009-06-01 19:57:37 +0000361template<class CodeEmitter>
362void Emitter<CodeEmitter>::emitMemModRMByte(const MachineInstr &MI,
Chris Lattner5b6b1782009-08-16 02:45:18 +0000363 unsigned Op,unsigned RegOpcodeField,
364 intptr_t PCAdj) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000365 const MachineOperand &Op3 = MI.getOperand(Op+3);
366 int DispVal = 0;
367 const MachineOperand *DispForReloc = 0;
368
369 // Figure out what sort of displacement we have to handle here.
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000370 if (Op3.isGlobal()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000371 DispForReloc = &Op3;
Daniel Dunbar8ac6c042009-09-01 22:06:53 +0000372 } else if (Op3.isSymbol()) {
373 DispForReloc = &Op3;
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000374 } else if (Op3.isCPI()) {
Bruno Cardoso Lopes670400e2009-08-05 00:11:21 +0000375 if (!MCE.earlyResolveAddresses() || Is64BitMode || IsPIC) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000376 DispForReloc = &Op3;
377 } else {
Chris Lattner6017d482007-12-30 23:10:15 +0000378 DispVal += MCE.getConstantPoolEntryAddress(Op3.getIndex());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000379 DispVal += Op3.getOffset();
380 }
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000381 } else if (Op3.isJTI()) {
Bruno Cardoso Lopes670400e2009-08-05 00:11:21 +0000382 if (!MCE.earlyResolveAddresses() || Is64BitMode || IsPIC) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000383 DispForReloc = &Op3;
384 } else {
Chris Lattner6017d482007-12-30 23:10:15 +0000385 DispVal += MCE.getJumpTableEntryAddress(Op3.getIndex());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000386 }
387 } else {
388 DispVal = Op3.getImm();
389 }
390
391 const MachineOperand &Base = MI.getOperand(Op);
392 const MachineOperand &Scale = MI.getOperand(Op+1);
393 const MachineOperand &IndexReg = MI.getOperand(Op+2);
394
395 unsigned BaseReg = Base.getReg();
396
Bruno Cardoso Lopes670400e2009-08-05 00:11:21 +0000397 // Indicate that the displacement will use an pcrel or absolute reference
398 // by default. MCEs able to resolve addresses on-the-fly use pcrel by default
399 // while others, unless explicit asked to use RIP, use absolute references.
400 bool IsPCRel = MCE.earlyResolveAddresses() ? true : false;
401
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000402 // Is a SIB byte needed?
Bruno Cardoso Lopes670400e2009-08-05 00:11:21 +0000403 // If no BaseReg, issue a RIP relative instruction only if the MCE can
404 // resolve addresses on-the-fly, otherwise use SIB (Intel Manual 2A, table
405 // 2-7) and absolute references.
Evan Cheng92569ce2009-05-12 00:07:35 +0000406 if ((!Is64BitMode || DispForReloc || BaseReg != 0) &&
Bruno Cardoso Lopes670400e2009-08-05 00:11:21 +0000407 IndexReg.getReg() == 0 &&
408 ((BaseReg == 0 && MCE.earlyResolveAddresses()) || BaseReg == X86::RIP ||
409 (BaseReg != 0 && getX86RegNum(BaseReg) != N86::ESP))) {
410 if (BaseReg == 0 || BaseReg == X86::RIP) { // Just a displacement?
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000411 // Emit special case [disp32] encoding
412 MCE.emitByte(ModRMByte(0, RegOpcodeField, 5));
Bruno Cardoso Lopes670400e2009-08-05 00:11:21 +0000413 emitDisplacementField(DispForReloc, DispVal, PCAdj, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000414 } else {
415 unsigned BaseRegNo = getX86RegNum(BaseReg);
416 if (!DispForReloc && DispVal == 0 && BaseRegNo != N86::EBP) {
417 // Emit simple indirect register encoding... [EAX] f.e.
418 MCE.emitByte(ModRMByte(0, RegOpcodeField, BaseRegNo));
419 } else if (!DispForReloc && isDisp8(DispVal)) {
420 // Emit the disp8 encoding... [REG+disp8]
421 MCE.emitByte(ModRMByte(1, RegOpcodeField, BaseRegNo));
422 emitConstant(DispVal, 1);
423 } else {
424 // Emit the most general non-SIB encoding: [REG+disp32]
425 MCE.emitByte(ModRMByte(2, RegOpcodeField, BaseRegNo));
Bruno Cardoso Lopes670400e2009-08-05 00:11:21 +0000426 emitDisplacementField(DispForReloc, DispVal, PCAdj, IsPCRel);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000427 }
428 }
429
430 } else { // We need a SIB byte, so start by outputting the ModR/M byte first
431 assert(IndexReg.getReg() != X86::ESP &&
432 IndexReg.getReg() != X86::RSP && "Cannot use ESP as index reg!");
433
434 bool ForceDisp32 = false;
435 bool ForceDisp8 = false;
436 if (BaseReg == 0) {
437 // If there is no base register, we emit the special case SIB byte with
438 // MOD=0, BASE=5, to JUST get the index, scale, and displacement.
439 MCE.emitByte(ModRMByte(0, RegOpcodeField, 4));
440 ForceDisp32 = true;
441 } else if (DispForReloc) {
442 // Emit the normal disp32 encoding.
443 MCE.emitByte(ModRMByte(2, RegOpcodeField, 4));
444 ForceDisp32 = true;
445 } else if (DispVal == 0 && getX86RegNum(BaseReg) != N86::EBP) {
446 // Emit no displacement ModR/M byte
447 MCE.emitByte(ModRMByte(0, RegOpcodeField, 4));
448 } else if (isDisp8(DispVal)) {
449 // Emit the disp8 encoding...
450 MCE.emitByte(ModRMByte(1, RegOpcodeField, 4));
451 ForceDisp8 = true; // Make sure to force 8 bit disp if Base=EBP
452 } else {
453 // Emit the normal disp32 encoding...
454 MCE.emitByte(ModRMByte(2, RegOpcodeField, 4));
455 }
456
457 // Calculate what the SS field value should be...
458 static const unsigned SSTable[] = { ~0, 0, 1, ~0, 2, ~0, ~0, ~0, 3 };
459 unsigned SS = SSTable[Scale.getImm()];
460
461 if (BaseReg == 0) {
Bruno Cardoso Lopes670400e2009-08-05 00:11:21 +0000462 // Handle the SIB byte for the case where there is no base, see Intel
463 // Manual 2A, table 2-7. The displacement has already been output.
Mon P Wang67b7fe22008-10-31 19:13:42 +0000464 unsigned IndexRegNo;
465 if (IndexReg.getReg())
466 IndexRegNo = getX86RegNum(IndexReg.getReg());
Bruno Cardoso Lopes670400e2009-08-05 00:11:21 +0000467 else // Examples: [ESP+1*<noreg>+4] or [scaled idx]+disp32 (MOD=0,BASE=5)
468 IndexRegNo = 4;
Mon P Wang67b7fe22008-10-31 19:13:42 +0000469 emitSIBByte(SS, IndexRegNo, 5);
Dan Gohman85a356f2008-11-10 22:09:58 +0000470 } else {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000471 unsigned BaseRegNo = getX86RegNum(BaseReg);
472 unsigned IndexRegNo;
473 if (IndexReg.getReg())
474 IndexRegNo = getX86RegNum(IndexReg.getReg());
475 else
476 IndexRegNo = 4; // For example [ESP+1*<noreg>+4]
477 emitSIBByte(SS, IndexRegNo, BaseRegNo);
478 }
479
480 // Do we need to output a displacement?
481 if (ForceDisp8) {
482 emitConstant(DispVal, 1);
483 } else if (DispVal != 0 || ForceDisp32) {
Bruno Cardoso Lopes670400e2009-08-05 00:11:21 +0000484 emitDisplacementField(DispForReloc, DispVal, PCAdj, IsPCRel);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000485 }
486 }
487}
488
Bruno Cardoso Lopes8e2537b2009-06-01 19:57:37 +0000489template<class CodeEmitter>
Chris Lattner5b6b1782009-08-16 02:45:18 +0000490void Emitter<CodeEmitter>::emitInstruction(const MachineInstr &MI,
491 const TargetInstrDesc *Desc) {
Bill Wendlingbdfa3be2009-08-03 00:11:34 +0000492 DEBUG(errs() << MI);
Evan Cheng872bd4b2008-03-14 07:13:42 +0000493
Jeffrey Yasskin8ad296e2009-07-16 21:07:26 +0000494 MCE.processDebugLoc(MI.getDebugLoc());
495
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000496 unsigned Opcode = Desc->Opcode;
497
Andrew Lenharth7a5a4b22008-03-01 13:37:02 +0000498 // Emit the lock opcode prefix as needed.
Chris Lattner5b6b1782009-08-16 02:45:18 +0000499 if (Desc->TSFlags & X86II::LOCK)
500 MCE.emitByte(0xF0);
Andrew Lenharth7a5a4b22008-03-01 13:37:02 +0000501
Duncan Sandsa707cf82008-10-11 19:34:24 +0000502 // Emit segment override opcode prefix as needed.
Anton Korobeynikov975e1472008-10-11 19:09:15 +0000503 switch (Desc->TSFlags & X86II::SegOvrMask) {
504 case X86II::FS:
505 MCE.emitByte(0x64);
506 break;
507 case X86II::GS:
508 MCE.emitByte(0x65);
509 break;
Edwin Törökbd448e32009-07-14 16:55:14 +0000510 default: llvm_unreachable("Invalid segment!");
Anton Korobeynikov4b7be802008-10-12 10:30:11 +0000511 case 0: break; // No segment override!
Anton Korobeynikov975e1472008-10-11 19:09:15 +0000512 }
513
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000514 // Emit the repeat opcode prefix as needed.
Chris Lattner5b6b1782009-08-16 02:45:18 +0000515 if ((Desc->TSFlags & X86II::Op0Mask) == X86II::REP)
516 MCE.emitByte(0xF3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000517
518 // Emit the operand size opcode prefix as needed.
Chris Lattner5b6b1782009-08-16 02:45:18 +0000519 if (Desc->TSFlags & X86II::OpSize)
520 MCE.emitByte(0x66);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000521
522 // Emit the address size opcode prefix as needed.
Chris Lattner5b6b1782009-08-16 02:45:18 +0000523 if (Desc->TSFlags & X86II::AdSize)
524 MCE.emitByte(0x67);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000525
526 bool Need0FPrefix = false;
527 switch (Desc->TSFlags & X86II::Op0Mask) {
Evan Cheng0c835a82008-04-03 08:53:17 +0000528 case X86II::TB: // Two-byte opcode prefix
529 case X86II::T8: // 0F 38
530 case X86II::TA: // 0F 3A
531 Need0FPrefix = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000532 break;
Eric Christopherb5f948c2009-08-08 21:55:08 +0000533 case X86II::TF: // F2 0F 38
534 MCE.emitByte(0xF2);
535 Need0FPrefix = true;
536 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000537 case X86II::REP: break; // already handled.
538 case X86II::XS: // F3 0F
539 MCE.emitByte(0xF3);
540 Need0FPrefix = true;
541 break;
542 case X86II::XD: // F2 0F
543 MCE.emitByte(0xF2);
544 Need0FPrefix = true;
545 break;
546 case X86II::D8: case X86II::D9: case X86II::DA: case X86II::DB:
547 case X86II::DC: case X86II::DD: case X86II::DE: case X86II::DF:
548 MCE.emitByte(0xD8+
549 (((Desc->TSFlags & X86II::Op0Mask)-X86II::D8)
550 >> X86II::Op0Shift));
551 break; // Two-byte opcode prefix
Edwin Törökbd448e32009-07-14 16:55:14 +0000552 default: llvm_unreachable("Invalid prefix!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000553 case 0: break; // No prefix!
554 }
555
Chris Lattner5b6b1782009-08-16 02:45:18 +0000556 // Handle REX prefix.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000557 if (Is64BitMode) {
Chris Lattner5b6b1782009-08-16 02:45:18 +0000558 if (unsigned REX = X86InstrInfo::determineREX(MI))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000559 MCE.emitByte(0x40 | REX);
560 }
561
562 // 0x0F escape code must be emitted just before the opcode.
563 if (Need0FPrefix)
564 MCE.emitByte(0x0F);
565
Evan Cheng0c835a82008-04-03 08:53:17 +0000566 switch (Desc->TSFlags & X86II::Op0Mask) {
Chris Lattner5b6b1782009-08-16 02:45:18 +0000567 case X86II::TF: // F2 0F 38
568 case X86II::T8: // 0F 38
Evan Cheng0c835a82008-04-03 08:53:17 +0000569 MCE.emitByte(0x38);
570 break;
571 case X86II::TA: // 0F 3A
572 MCE.emitByte(0x3A);
573 break;
574 }
575
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000576 // If this is a two-address instruction, skip one of the register operands.
Chris Lattner0c2a4f32008-01-07 03:13:06 +0000577 unsigned NumOps = Desc->getNumOperands();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000578 unsigned CurOp = 0;
579 if (NumOps > 1 && Desc->getOperandConstraint(1, TOI::TIED_TO) != -1)
Evan Chengd49dbb82008-04-18 20:55:36 +0000580 ++CurOp;
581 else if (NumOps > 2 && Desc->getOperandConstraint(NumOps-1, TOI::TIED_TO)== 0)
582 // Skip the last source operand that is tied_to the dest reg. e.g. LXADD32
583 --NumOps;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000584
585 unsigned char BaseOpcode = II->getBaseOpcodeFor(Desc);
586 switch (Desc->TSFlags & X86II::FormMask) {
Chris Lattnerb4a3ef12009-08-16 02:36:40 +0000587 default:
588 llvm_unreachable("Unknown FormMask value in X86 MachineCodeEmitter!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000589 case X86II::Pseudo:
Evan Cheng0729ccf2008-01-05 00:41:47 +0000590 // Remember the current PC offset, this is the PIC relocation
591 // base address.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000592 switch (Opcode) {
593 default:
Chris Lattnerb4a3ef12009-08-16 02:36:40 +0000594 llvm_unreachable("psuedo instructions should be removed before code"
595 " emission");
Evan Cheng7c6c35e2008-03-05 02:34:36 +0000596 break;
Chris Lattner5b6b1782009-08-16 02:45:18 +0000597 case TargetInstrInfo::INLINEASM:
Evan Cheng4e1a7202008-11-19 23:21:11 +0000598 // We allow inline assembler nodes with empty bodies - they can
599 // implicitly define registers, which is ok for JIT.
Chris Lattner5b6b1782009-08-16 02:45:18 +0000600 assert(MI.getOperand(0).getSymbolName()[0] == 0 &&
601 "JIT does not support inline asm!");
Evan Cheng7c6c35e2008-03-05 02:34:36 +0000602 break;
Dan Gohmanfa607c92008-07-01 00:05:16 +0000603 case TargetInstrInfo::DBG_LABEL:
604 case TargetInstrInfo::EH_LABEL:
Nicolas Geoffray0e757e12008-02-13 18:39:37 +0000605 MCE.emitLabel(MI.getOperand(0).getImm());
606 break;
Evan Chengb74b4b62008-03-17 06:56:52 +0000607 case TargetInstrInfo::IMPLICIT_DEF:
Evan Cheng7c6c35e2008-03-05 02:34:36 +0000608 case X86::DWARF_LOC:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000609 case X86::FP_REG_KILL:
610 break;
Evan Chengaf743252008-01-05 02:26:58 +0000611 case X86::MOVPC32r: {
Evan Cheng0729ccf2008-01-05 00:41:47 +0000612 // This emits the "call" portion of this pseudo instruction.
613 MCE.emitByte(BaseOpcode);
Nicolas Geoffraycb162a02008-04-16 20:10:13 +0000614 emitConstant(0, X86InstrInfo::sizeOfImm(Desc));
Evan Chengaf743252008-01-05 02:26:58 +0000615 // Remember PIC base.
Evan Cheng6e561c72008-12-10 02:32:19 +0000616 PICBaseOffset = (intptr_t) MCE.getCurrentPCOffset();
Dan Gohmanb41dfba2008-05-14 01:58:56 +0000617 X86JITInfo *JTI = TM.getJITInfo();
Evan Chengaf743252008-01-05 02:26:58 +0000618 JTI->setPICBase(MCE.getCurrentPCValue());
Evan Cheng0729ccf2008-01-05 00:41:47 +0000619 break;
620 }
Evan Chengaf743252008-01-05 02:26:58 +0000621 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000622 CurOp = NumOps;
623 break;
Chris Lattner5b6b1782009-08-16 02:45:18 +0000624 case X86II::RawFrm: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000625 MCE.emitByte(BaseOpcode);
Evan Cheng0729ccf2008-01-05 00:41:47 +0000626
Chris Lattner5b6b1782009-08-16 02:45:18 +0000627 if (CurOp == NumOps)
628 break;
629
630 const MachineOperand &MO = MI.getOperand(CurOp++);
Bill Wendling0768ef62008-08-21 08:38:54 +0000631
Chris Lattner5b6b1782009-08-16 02:45:18 +0000632 DEBUG(errs() << "RawFrm CurOp " << CurOp << "\n");
633 DEBUG(errs() << "isMBB " << MO.isMBB() << "\n");
634 DEBUG(errs() << "isGlobal " << MO.isGlobal() << "\n");
635 DEBUG(errs() << "isSymbol " << MO.isSymbol() << "\n");
636 DEBUG(errs() << "isImm " << MO.isImm() << "\n");
Bill Wendling0768ef62008-08-21 08:38:54 +0000637
Chris Lattner5b6b1782009-08-16 02:45:18 +0000638 if (MO.isMBB()) {
639 emitPCRelativeBlockAddress(MO.getMBB());
640 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000641 }
Chris Lattner5b6b1782009-08-16 02:45:18 +0000642
643 if (MO.isGlobal()) {
644 // Assume undefined functions may be outside the Small codespace.
645 bool NeedStub =
646 (Is64BitMode &&
647 (TM.getCodeModel() == CodeModel::Large ||
648 TM.getSubtarget<X86Subtarget>().isTargetDarwin())) ||
649 Opcode == X86::TAILJMPd;
650 emitGlobalAddress(MO.getGlobal(), X86::reloc_pcrel_word,
651 MO.getOffset(), 0, NeedStub);
652 break;
653 }
654
655 if (MO.isSymbol()) {
656 emitExternalSymbolAddress(MO.getSymbolName(), X86::reloc_pcrel_word);
657 break;
658 }
659
660 assert(MO.isImm() && "Unknown RawFrm operand!");
661 if (Opcode == X86::CALLpcrel32 || Opcode == X86::CALL64pcrel32) {
662 // Fix up immediate operand for pc relative calls.
663 intptr_t Imm = (intptr_t)MO.getImm();
664 Imm = Imm - MCE.getCurrentPCValue() - 4;
665 emitConstant(Imm, X86InstrInfo::sizeOfImm(Desc));
666 } else
667 emitConstant(MO.getImm(), X86InstrInfo::sizeOfImm(Desc));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000668 break;
Chris Lattner5b6b1782009-08-16 02:45:18 +0000669 }
670
Chris Lattnerb4a3ef12009-08-16 02:36:40 +0000671 case X86II::AddRegFrm: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000672 MCE.emitByte(BaseOpcode + getX86RegNum(MI.getOperand(CurOp++).getReg()));
673
Chris Lattnerb4a3ef12009-08-16 02:36:40 +0000674 if (CurOp == NumOps)
675 break;
676
677 const MachineOperand &MO1 = MI.getOperand(CurOp++);
678 unsigned Size = X86InstrInfo::sizeOfImm(Desc);
679 if (MO1.isImm()) {
680 emitConstant(MO1.getImm(), Size);
681 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000682 }
Chris Lattnerb4a3ef12009-08-16 02:36:40 +0000683
684 unsigned rt = Is64BitMode ? X86::reloc_pcrel_word
685 : (IsPIC ? X86::reloc_picrel_word : X86::reloc_absolute_word);
686 if (Opcode == X86::MOV64ri64i32)
687 rt = X86::reloc_absolute_word; // FIXME: add X86II flag?
688 // This should not occur on Darwin for relocatable objects.
689 if (Opcode == X86::MOV64ri)
690 rt = X86::reloc_absolute_dword; // FIXME: add X86II flag?
691 if (MO1.isGlobal()) {
692 bool NeedStub = isa<Function>(MO1.getGlobal());
693 bool Indirect = gvNeedsNonLazyPtr(MO1, TM);
694 emitGlobalAddress(MO1.getGlobal(), rt, MO1.getOffset(), 0,
695 NeedStub, Indirect);
696 } else if (MO1.isSymbol())
697 emitExternalSymbolAddress(MO1.getSymbolName(), rt);
698 else if (MO1.isCPI())
699 emitConstPoolAddress(MO1.getIndex(), rt);
700 else if (MO1.isJTI())
701 emitJumpTableAddress(MO1.getIndex(), rt);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000702 break;
Chris Lattnerb4a3ef12009-08-16 02:36:40 +0000703 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000704
705 case X86II::MRMDestReg: {
706 MCE.emitByte(BaseOpcode);
707 emitRegModRMByte(MI.getOperand(CurOp).getReg(),
708 getX86RegNum(MI.getOperand(CurOp+1).getReg()));
709 CurOp += 2;
710 if (CurOp != NumOps)
Chris Lattnerb4a3ef12009-08-16 02:36:40 +0000711 emitConstant(MI.getOperand(CurOp++).getImm(),
712 X86InstrInfo::sizeOfImm(Desc));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000713 break;
714 }
715 case X86II::MRMDestMem: {
716 MCE.emitByte(BaseOpcode);
Rafael Espindola7f69c042009-03-28 17:03:24 +0000717 emitMemModRMByte(MI, CurOp,
718 getX86RegNum(MI.getOperand(CurOp + X86AddrNumOperands)
719 .getReg()));
720 CurOp += X86AddrNumOperands + 1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000721 if (CurOp != NumOps)
Chris Lattnerb4a3ef12009-08-16 02:36:40 +0000722 emitConstant(MI.getOperand(CurOp++).getImm(),
723 X86InstrInfo::sizeOfImm(Desc));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000724 break;
725 }
726
727 case X86II::MRMSrcReg:
728 MCE.emitByte(BaseOpcode);
729 emitRegModRMByte(MI.getOperand(CurOp+1).getReg(),
730 getX86RegNum(MI.getOperand(CurOp).getReg()));
731 CurOp += 2;
732 if (CurOp != NumOps)
Bruno Cardoso Lopes8e2537b2009-06-01 19:57:37 +0000733 emitConstant(MI.getOperand(CurOp++).getImm(),
734 X86InstrInfo::sizeOfImm(Desc));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000735 break;
736
737 case X86II::MRMSrcMem: {
Rafael Espindolabca99f72009-04-08 21:14:34 +0000738 // FIXME: Maybe lea should have its own form?
739 int AddrOperands;
740 if (Opcode == X86::LEA64r || Opcode == X86::LEA64_32r ||
741 Opcode == X86::LEA16r || Opcode == X86::LEA32r)
742 AddrOperands = X86AddrNumOperands - 1; // No segment register
743 else
744 AddrOperands = X86AddrNumOperands;
745
746 intptr_t PCAdj = (CurOp + AddrOperands + 1 != NumOps) ?
Rafael Espindola7f69c042009-03-28 17:03:24 +0000747 X86InstrInfo::sizeOfImm(Desc) : 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000748
749 MCE.emitByte(BaseOpcode);
750 emitMemModRMByte(MI, CurOp+1, getX86RegNum(MI.getOperand(CurOp).getReg()),
751 PCAdj);
Rafael Espindolabca99f72009-04-08 21:14:34 +0000752 CurOp += AddrOperands + 1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000753 if (CurOp != NumOps)
Bruno Cardoso Lopes8e2537b2009-06-01 19:57:37 +0000754 emitConstant(MI.getOperand(CurOp++).getImm(),
755 X86InstrInfo::sizeOfImm(Desc));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000756 break;
757 }
758
759 case X86II::MRM0r: case X86II::MRM1r:
760 case X86II::MRM2r: case X86II::MRM3r:
761 case X86II::MRM4r: case X86II::MRM5r:
Evan Cheng5d0d34e2008-10-17 17:14:20 +0000762 case X86II::MRM6r: case X86II::MRM7r: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000763 MCE.emitByte(BaseOpcode);
Evan Cheng5d0d34e2008-10-17 17:14:20 +0000764
Bill Wendling6ee76552009-05-28 23:40:46 +0000765 // Special handling of lfence, mfence, monitor, and mwait.
Evan Cheng5d0d34e2008-10-17 17:14:20 +0000766 if (Desc->getOpcode() == X86::LFENCE ||
Bill Wendling6ee76552009-05-28 23:40:46 +0000767 Desc->getOpcode() == X86::MFENCE ||
768 Desc->getOpcode() == X86::MONITOR ||
769 Desc->getOpcode() == X86::MWAIT) {
Evan Cheng5d0d34e2008-10-17 17:14:20 +0000770 emitRegModRMByte((Desc->TSFlags & X86II::FormMask)-X86II::MRM0r);
Bill Wendling6ee76552009-05-28 23:40:46 +0000771
772 switch (Desc->getOpcode()) {
773 default: break;
774 case X86::MONITOR:
775 MCE.emitByte(0xC8);
776 break;
777 case X86::MWAIT:
778 MCE.emitByte(0xC9);
779 break;
780 }
781 } else {
Evan Cheng5d0d34e2008-10-17 17:14:20 +0000782 emitRegModRMByte(MI.getOperand(CurOp++).getReg(),
783 (Desc->TSFlags & X86II::FormMask)-X86II::MRM0r);
Bill Wendling6ee76552009-05-28 23:40:46 +0000784 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000785
Chris Lattnerb4a3ef12009-08-16 02:36:40 +0000786 if (CurOp == NumOps)
787 break;
788
789 const MachineOperand &MO1 = MI.getOperand(CurOp++);
790 unsigned Size = X86InstrInfo::sizeOfImm(Desc);
791 if (MO1.isImm()) {
792 emitConstant(MO1.getImm(), Size);
793 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000794 }
Chris Lattnerb4a3ef12009-08-16 02:36:40 +0000795
796 unsigned rt = Is64BitMode ? X86::reloc_pcrel_word
797 : (IsPIC ? X86::reloc_picrel_word : X86::reloc_absolute_word);
798 if (Opcode == X86::MOV64ri32)
799 rt = X86::reloc_absolute_word_sext; // FIXME: add X86II flag?
800 if (MO1.isGlobal()) {
801 bool NeedStub = isa<Function>(MO1.getGlobal());
802 bool Indirect = gvNeedsNonLazyPtr(MO1, TM);
803 emitGlobalAddress(MO1.getGlobal(), rt, MO1.getOffset(), 0,
804 NeedStub, Indirect);
805 } else if (MO1.isSymbol())
806 emitExternalSymbolAddress(MO1.getSymbolName(), rt);
807 else if (MO1.isCPI())
808 emitConstPoolAddress(MO1.getIndex(), rt);
809 else if (MO1.isJTI())
810 emitJumpTableAddress(MO1.getIndex(), rt);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000811 break;
Evan Cheng5d0d34e2008-10-17 17:14:20 +0000812 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000813
814 case X86II::MRM0m: case X86II::MRM1m:
815 case X86II::MRM2m: case X86II::MRM3m:
816 case X86II::MRM4m: case X86II::MRM5m:
817 case X86II::MRM6m: case X86II::MRM7m: {
Rafael Espindola7f69c042009-03-28 17:03:24 +0000818 intptr_t PCAdj = (CurOp + X86AddrNumOperands != NumOps) ?
Dale Johannesen1a51cff2009-05-06 19:04:30 +0000819 (MI.getOperand(CurOp+X86AddrNumOperands).isImm() ?
820 X86InstrInfo::sizeOfImm(Desc) : 4) : 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000821
822 MCE.emitByte(BaseOpcode);
823 emitMemModRMByte(MI, CurOp, (Desc->TSFlags & X86II::FormMask)-X86II::MRM0m,
824 PCAdj);
Rafael Espindola7f69c042009-03-28 17:03:24 +0000825 CurOp += X86AddrNumOperands;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000826
Chris Lattnerb4a3ef12009-08-16 02:36:40 +0000827 if (CurOp == NumOps)
828 break;
829
830 const MachineOperand &MO = MI.getOperand(CurOp++);
831 unsigned Size = X86InstrInfo::sizeOfImm(Desc);
832 if (MO.isImm()) {
833 emitConstant(MO.getImm(), Size);
834 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000835 }
Chris Lattnerb4a3ef12009-08-16 02:36:40 +0000836
837 unsigned rt = Is64BitMode ? X86::reloc_pcrel_word
838 : (IsPIC ? X86::reloc_picrel_word : X86::reloc_absolute_word);
839 if (Opcode == X86::MOV64mi32)
840 rt = X86::reloc_absolute_word_sext; // FIXME: add X86II flag?
841 if (MO.isGlobal()) {
842 bool NeedStub = isa<Function>(MO.getGlobal());
843 bool Indirect = gvNeedsNonLazyPtr(MO, TM);
844 emitGlobalAddress(MO.getGlobal(), rt, MO.getOffset(), 0,
845 NeedStub, Indirect);
846 } else if (MO.isSymbol())
847 emitExternalSymbolAddress(MO.getSymbolName(), rt);
848 else if (MO.isCPI())
849 emitConstPoolAddress(MO.getIndex(), rt);
850 else if (MO.isJTI())
851 emitJumpTableAddress(MO.getIndex(), rt);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000852 break;
853 }
854
855 case X86II::MRMInitReg:
856 MCE.emitByte(BaseOpcode);
857 // Duplicate register, used by things like MOV8r0 (aka xor reg,reg).
858 emitRegModRMByte(MI.getOperand(CurOp).getReg(),
859 getX86RegNum(MI.getOperand(CurOp).getReg()));
860 ++CurOp;
861 break;
862 }
863
Evan Cheng6032b652008-03-05 02:08:03 +0000864 if (!Desc->isVariadic() && CurOp != NumOps) {
Edwin Török4d9756a2009-07-08 20:53:28 +0000865#ifndef NDEBUG
Chris Lattner5b6b1782009-08-16 02:45:18 +0000866 errs() << "Cannot encode all operands of: " << MI << "\n";
Edwin Török4d9756a2009-07-08 20:53:28 +0000867#endif
Edwin Törökbd448e32009-07-14 16:55:14 +0000868 llvm_unreachable(0);
Evan Cheng6032b652008-03-05 02:08:03 +0000869 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000870}
Daniel Dunbar2f379632009-08-27 08:12:55 +0000871
872// Adapt the Emitter / CodeEmitter interfaces to MCCodeEmitter.
873//
874// FIXME: This is a total hack designed to allow work on llvm-mc to proceed
875// without being blocked on various cleanups needed to support a clean interface
876// to instruction encoding.
877//
878// Look away!
879
880#include "llvm/DerivedTypes.h"
881
882namespace {
883class MCSingleInstructionCodeEmitter : public MachineCodeEmitter {
884 uint8_t Data[256];
885
886public:
887 MCSingleInstructionCodeEmitter() { reset(); }
888
889 void reset() {
890 BufferBegin = Data;
891 BufferEnd = array_endof(Data);
892 CurBufferPtr = Data;
893 }
894
895 StringRef str() {
896 return StringRef(reinterpret_cast<char*>(BufferBegin),
897 CurBufferPtr - BufferBegin);
898 }
899
900 virtual void startFunction(MachineFunction &F) {}
901 virtual bool finishFunction(MachineFunction &F) { return false; }
902 virtual void emitLabel(uint64_t LabelID) {}
903 virtual void StartMachineBasicBlock(MachineBasicBlock *MBB) {}
904 virtual bool earlyResolveAddresses() const { return false; }
905 virtual void addRelocation(const MachineRelocation &MR) { }
906 virtual uintptr_t getConstantPoolEntryAddress(unsigned Index) const {
907 return 0;
908 }
909 virtual uintptr_t getJumpTableEntryAddress(unsigned Index) const {
910 return 0;
911 }
912 virtual uintptr_t getMachineBasicBlockAddress(MachineBasicBlock *MBB) const {
913 return 0;
914 }
915 virtual uintptr_t getLabelAddress(uint64_t LabelID) const {
916 return 0;
917 }
918 virtual void setModuleInfo(MachineModuleInfo* Info) {}
919};
920
921class X86MCCodeEmitter : public MCCodeEmitter {
922 X86MCCodeEmitter(const X86MCCodeEmitter &); // DO NOT IMPLEMENT
923 void operator=(const X86MCCodeEmitter &); // DO NOT IMPLEMENT
924
925private:
926 X86TargetMachine &TM;
927 llvm::Function *DummyF;
928 TargetData *DummyTD;
929 mutable llvm::MachineFunction *DummyMF;
930 llvm::MachineBasicBlock *DummyMBB;
931
932 MCSingleInstructionCodeEmitter *InstrEmitter;
933 Emitter<MachineCodeEmitter> *Emit;
934
935public:
936 X86MCCodeEmitter(X86TargetMachine &_TM) : TM(_TM) {
937 // Verily, thou shouldst avert thine eyes.
938 const llvm::FunctionType *FTy =
939 FunctionType::get(llvm::Type::getVoidTy(getGlobalContext()), false);
940 DummyF = Function::Create(FTy, GlobalValue::InternalLinkage);
941 DummyTD = new TargetData("");
942 DummyMF = new MachineFunction(DummyF, TM);
943 DummyMBB = DummyMF->CreateMachineBasicBlock();
944
945 InstrEmitter = new MCSingleInstructionCodeEmitter();
946 Emit = new Emitter<MachineCodeEmitter>(TM, *InstrEmitter,
947 *TM.getInstrInfo(),
948 *DummyTD, false);
949 }
950 ~X86MCCodeEmitter() {
951 delete Emit;
952 delete InstrEmitter;
953 delete DummyMF;
954 delete DummyF;
955 }
956
957 bool AddRegToInstr(const MCInst &MI, MachineInstr *Instr,
958 unsigned Start) const {
959 if (Start + 1 > MI.getNumOperands())
960 return false;
961
962 const MCOperand &Op = MI.getOperand(Start);
963 if (!Op.isReg()) return false;
964
965 Instr->addOperand(MachineOperand::CreateReg(Op.getReg(), false));
966 return true;
967 }
968
969 bool AddImmToInstr(const MCInst &MI, MachineInstr *Instr,
970 unsigned Start) const {
971 if (Start + 1 > MI.getNumOperands())
972 return false;
973
974 const MCOperand &Op = MI.getOperand(Start);
975 if (Op.isImm()) {
976 Instr->addOperand(MachineOperand::CreateImm(Op.getImm()));
977 return true;
978 }
Daniel Dunbar6e966212009-08-31 08:08:38 +0000979 if (!Op.isExpr())
Daniel Dunbar2f379632009-08-27 08:12:55 +0000980 return false;
981
Daniel Dunbar6e966212009-08-31 08:08:38 +0000982 const MCExpr *Expr = Op.getExpr();
983 if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr)) {
984 Instr->addOperand(MachineOperand::CreateImm(CE->getValue()));
Daniel Dunbara8d310b2009-08-30 06:17:49 +0000985 return true;
986 }
987
Daniel Dunbar2f379632009-08-27 08:12:55 +0000988 // FIXME: Relocation / fixup.
989 Instr->addOperand(MachineOperand::CreateImm(0));
990 return true;
991 }
992
993 bool AddLMemToInstr(const MCInst &MI, MachineInstr *Instr,
994 unsigned Start) const {
995 return (AddRegToInstr(MI, Instr, Start + 0) &&
996 AddImmToInstr(MI, Instr, Start + 1) &&
997 AddRegToInstr(MI, Instr, Start + 2) &&
998 AddImmToInstr(MI, Instr, Start + 3));
999 }
1000
1001 bool AddMemToInstr(const MCInst &MI, MachineInstr *Instr,
1002 unsigned Start) const {
1003 return (AddRegToInstr(MI, Instr, Start + 0) &&
1004 AddImmToInstr(MI, Instr, Start + 1) &&
1005 AddRegToInstr(MI, Instr, Start + 2) &&
1006 AddImmToInstr(MI, Instr, Start + 3) &&
1007 AddRegToInstr(MI, Instr, Start + 4));
1008 }
1009
1010 void EncodeInstruction(const MCInst &MI, raw_ostream &OS) const {
1011 // Don't look yet!
1012
1013 // Convert the MCInst to a MachineInstr so we can (ab)use the regular
1014 // emitter.
1015 const X86InstrInfo &II = *TM.getInstrInfo();
1016 const TargetInstrDesc &Desc = II.get(MI.getOpcode());
1017 MachineInstr *Instr = DummyMF->CreateMachineInstr(Desc, DebugLoc());
1018 DummyMBB->push_back(Instr);
1019
1020 unsigned Opcode = MI.getOpcode();
1021 unsigned NumOps = MI.getNumOperands();
1022 unsigned CurOp = 0;
1023 if (NumOps > 1 && Desc.getOperandConstraint(1, TOI::TIED_TO) != -1) {
1024 Instr->addOperand(MachineOperand::CreateReg(0, false));
1025 ++CurOp;
1026 } else if (NumOps > 2 &&
1027 Desc.getOperandConstraint(NumOps-1, TOI::TIED_TO)== 0)
1028 // Skip the last source operand that is tied_to the dest reg. e.g. LXADD32
1029 --NumOps;
1030
1031 bool OK = true;
1032 switch (Desc.TSFlags & X86II::FormMask) {
1033 case X86II::MRMDestReg:
1034 case X86II::MRMSrcReg:
1035 // Matching doesn't fill this in completely, we have to choose operand 0
1036 // for a tied register.
1037 OK &= AddRegToInstr(MI, Instr, 0); CurOp++;
1038 OK &= AddRegToInstr(MI, Instr, CurOp++);
1039 if (CurOp < NumOps)
1040 OK &= AddImmToInstr(MI, Instr, CurOp);
1041 break;
1042
1043 case X86II::RawFrm:
1044 if (CurOp < NumOps) {
1045 // Hack to make branches work.
1046 if (!(Desc.TSFlags & X86II::ImmMask) &&
Daniel Dunbar6e966212009-08-31 08:08:38 +00001047 MI.getOperand(0).isExpr() &&
1048 isa<MCSymbolRefExpr>(MI.getOperand(0).getExpr()))
Daniel Dunbar2f379632009-08-27 08:12:55 +00001049 Instr->addOperand(MachineOperand::CreateMBB(DummyMBB));
1050 else
1051 OK &= AddImmToInstr(MI, Instr, CurOp);
1052 }
1053 break;
1054
1055 case X86II::AddRegFrm:
1056 OK &= AddRegToInstr(MI, Instr, CurOp++);
1057 if (CurOp < NumOps)
1058 OK &= AddImmToInstr(MI, Instr, CurOp);
1059 break;
1060
1061 case X86II::MRM0r: case X86II::MRM1r:
1062 case X86II::MRM2r: case X86II::MRM3r:
1063 case X86II::MRM4r: case X86II::MRM5r:
1064 case X86II::MRM6r: case X86II::MRM7r:
1065 // Matching doesn't fill this in completely, we have to choose operand 0
1066 // for a tied register.
1067 OK &= AddRegToInstr(MI, Instr, 0); CurOp++;
1068 if (CurOp < NumOps)
1069 OK &= AddImmToInstr(MI, Instr, CurOp);
1070 break;
1071
1072 case X86II::MRM0m: case X86II::MRM1m:
1073 case X86II::MRM2m: case X86II::MRM3m:
1074 case X86II::MRM4m: case X86II::MRM5m:
1075 case X86II::MRM6m: case X86II::MRM7m:
1076 OK &= AddMemToInstr(MI, Instr, CurOp); CurOp += 5;
1077 if (CurOp < NumOps)
1078 OK &= AddImmToInstr(MI, Instr, CurOp);
1079 break;
1080
1081 case X86II::MRMSrcMem:
1082 OK &= AddRegToInstr(MI, Instr, CurOp++);
1083 if (Opcode == X86::LEA64r || Opcode == X86::LEA64_32r ||
1084 Opcode == X86::LEA16r || Opcode == X86::LEA32r)
1085 OK &= AddLMemToInstr(MI, Instr, CurOp);
1086 else
1087 OK &= AddMemToInstr(MI, Instr, CurOp);
1088 break;
1089
1090 case X86II::MRMDestMem:
1091 OK &= AddMemToInstr(MI, Instr, CurOp); CurOp += 5;
1092 OK &= AddRegToInstr(MI, Instr, CurOp);
1093 break;
1094
1095 default:
1096 case X86II::MRMInitReg:
1097 case X86II::Pseudo:
1098 OK = false;
1099 break;
1100 }
1101
1102 if (!OK) {
1103 errs() << "couldn't convert inst '";
1104 MI.print(errs());
1105 errs() << "' to machine instr:\n";
1106 Instr->dump();
1107 }
1108
1109 InstrEmitter->reset();
1110 if (OK)
1111 Emit->emitInstruction(*Instr, &Desc);
1112 OS << InstrEmitter->str();
1113
1114 Instr->eraseFromParent();
1115 }
1116};
1117}
1118
1119// Ok, now you can look.
1120MCCodeEmitter *llvm::createX86MCCodeEmitter(const Target &,
1121 TargetMachine &TM) {
1122 return new X86MCCodeEmitter(static_cast<X86TargetMachine&>(TM));
1123}