blob: 828e872cacbf58dd20c61c7cda605a3390af29ac [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"
Chris Lattner7fd81132009-10-27 17:01:03 +000022#include "llvm/LLVMContext.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000023#include "llvm/PassManager.h"
24#include "llvm/CodeGen/MachineCodeEmitter.h"
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +000025#include "llvm/CodeGen/JITCodeEmitter.h"
Bruno Cardoso Lopesaabb9a52009-07-06 05:09:34 +000026#include "llvm/CodeGen/ObjectCodeEmitter.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000027#include "llvm/CodeGen/MachineFunctionPass.h"
28#include "llvm/CodeGen/MachineInstr.h"
Nicolas Geoffray0e757e12008-02-13 18:39:37 +000029#include "llvm/CodeGen/MachineModuleInfo.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000030#include "llvm/CodeGen/Passes.h"
31#include "llvm/Function.h"
32#include "llvm/ADT/Statistic.h"
Daniel Dunbar2f379632009-08-27 08:12:55 +000033#include "llvm/MC/MCCodeEmitter.h"
Daniel Dunbar6e966212009-08-31 08:08:38 +000034#include "llvm/MC/MCExpr.h"
Daniel Dunbar2f379632009-08-27 08:12:55 +000035#include "llvm/MC/MCInst.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>
Nick Lewycky492d06e2009-10-25 06:33:48 +000046 class Emitter : public MachineFunctionPass {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000047 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,
Jeffrey Yasskine233d8a2009-11-16 22:41:33 +000085 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 {
David Greene00f64b82010-01-05 01:28:53 +0000138 DEBUG(dbgs() << "JITTing function '"
Daniel Dunbar005975c2009-07-25 00:23:56 +0000139 << 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 Cheng8af22c42008-11-10 01:08:07 +0000179 bool Indirect /* = false */) {
Bruno Cardoso Lopes670400e2009-08-05 00:11:21 +0000180 intptr_t RelocCST = Disp;
Evan Chengf0123872008-01-03 02:56:28 +0000181 if (Reloc == X86::reloc_picrel_word)
Evan Chengaf743252008-01-05 02:26:58 +0000182 RelocCST = PICBaseOffset;
Evan Cheng28e7e162008-01-04 10:46:51 +0000183 else if (Reloc == X86::reloc_pcrel_word)
184 RelocCST = PCAdj;
Evan Cheng8af22c42008-11-10 01:08:07 +0000185 MachineRelocation MR = Indirect
186 ? MachineRelocation::getIndirectSymbol(MCE.getCurrentPCOffset(), Reloc,
Jeffrey Yasskine233d8a2009-11-16 22:41:33 +0000187 GV, RelocCST, false)
Evan Cheng28e7e162008-01-04 10:46:51 +0000188 : MachineRelocation::getGV(MCE.getCurrentPCOffset(), Reloc,
Jeffrey Yasskine233d8a2009-11-16 22:41:33 +0000189 GV, RelocCST, false);
Evan Cheng28e7e162008-01-04 10:46:51 +0000190 MCE.addRelocation(MR);
Dan Gohman5ad09472008-10-24 01:57:54 +0000191 // The relocated value will be added to the displacement
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000192 if (Reloc == X86::reloc_absolute_dword)
Dan Gohman5ad09472008-10-24 01:57:54 +0000193 MCE.emitDWordLE(Disp);
194 else
195 MCE.emitWordLE((int32_t)Disp);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000196}
197
198/// emitExternalSymbolAddress - Arrange for the address of an external symbol to
199/// be emitted to the current location in the function, and allow it to be PC
200/// relative.
Bruno Cardoso Lopes8e2537b2009-06-01 19:57:37 +0000201template<class CodeEmitter>
202void Emitter<CodeEmitter>::emitExternalSymbolAddress(const char *ES,
203 unsigned Reloc) {
Evan Chengaf743252008-01-05 02:26:58 +0000204 intptr_t RelocCST = (Reloc == X86::reloc_picrel_word) ? PICBaseOffset : 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000205 MCE.addRelocation(MachineRelocation::getExtSym(MCE.getCurrentPCOffset(),
Evan Cheng28e7e162008-01-04 10:46:51 +0000206 Reloc, ES, RelocCST));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000207 if (Reloc == X86::reloc_absolute_dword)
Dan Gohman5ad09472008-10-24 01:57:54 +0000208 MCE.emitDWordLE(0);
209 else
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000210 MCE.emitWordLE(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000211}
212
213/// emitConstPoolAddress - Arrange for the address of an constant pool
214/// to be emitted to the current location in the function, and allow it to be PC
215/// relative.
Bruno Cardoso Lopes8e2537b2009-06-01 19:57:37 +0000216template<class CodeEmitter>
217void Emitter<CodeEmitter>::emitConstPoolAddress(unsigned CPI, unsigned Reloc,
Dan Gohman5ad09472008-10-24 01:57:54 +0000218 intptr_t Disp /* = 0 */,
Evan Chengf0123872008-01-03 02:56:28 +0000219 intptr_t PCAdj /* = 0 */) {
Evan Cheng28e7e162008-01-04 10:46:51 +0000220 intptr_t RelocCST = 0;
Evan Chengf0123872008-01-03 02:56:28 +0000221 if (Reloc == X86::reloc_picrel_word)
Evan Chengaf743252008-01-05 02:26:58 +0000222 RelocCST = PICBaseOffset;
Evan Cheng28e7e162008-01-04 10:46:51 +0000223 else if (Reloc == X86::reloc_pcrel_word)
224 RelocCST = PCAdj;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000225 MCE.addRelocation(MachineRelocation::getConstPool(MCE.getCurrentPCOffset(),
Evan Cheng28e7e162008-01-04 10:46:51 +0000226 Reloc, CPI, RelocCST));
Dan Gohman5ad09472008-10-24 01:57:54 +0000227 // The relocated value will be added to the displacement
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000228 if (Reloc == X86::reloc_absolute_dword)
Dan Gohman5ad09472008-10-24 01:57:54 +0000229 MCE.emitDWordLE(Disp);
230 else
231 MCE.emitWordLE((int32_t)Disp);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000232}
233
234/// emitJumpTableAddress - Arrange for the address of a jump table to
235/// be emitted to the current location in the function, and allow it to be PC
236/// relative.
Bruno Cardoso Lopes8e2537b2009-06-01 19:57:37 +0000237template<class CodeEmitter>
238void Emitter<CodeEmitter>::emitJumpTableAddress(unsigned JTI, unsigned Reloc,
Evan Chengf0123872008-01-03 02:56:28 +0000239 intptr_t PCAdj /* = 0 */) {
Evan Cheng28e7e162008-01-04 10:46:51 +0000240 intptr_t RelocCST = 0;
Evan Chengf0123872008-01-03 02:56:28 +0000241 if (Reloc == X86::reloc_picrel_word)
Evan Chengaf743252008-01-05 02:26:58 +0000242 RelocCST = PICBaseOffset;
Evan Cheng28e7e162008-01-04 10:46:51 +0000243 else if (Reloc == X86::reloc_pcrel_word)
244 RelocCST = PCAdj;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000245 MCE.addRelocation(MachineRelocation::getJumpTable(MCE.getCurrentPCOffset(),
Evan Cheng28e7e162008-01-04 10:46:51 +0000246 Reloc, JTI, RelocCST));
Dan Gohman5ad09472008-10-24 01:57:54 +0000247 // The relocated value will be added to the displacement
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000248 if (Reloc == X86::reloc_absolute_dword)
Dan Gohman5ad09472008-10-24 01:57:54 +0000249 MCE.emitDWordLE(0);
250 else
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000251 MCE.emitWordLE(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000252}
253
Bruno Cardoso Lopes8e2537b2009-06-01 19:57:37 +0000254template<class CodeEmitter>
255unsigned Emitter<CodeEmitter>::getX86RegNum(unsigned RegNo) const {
Dan Gohmanb41dfba2008-05-14 01:58:56 +0000256 return II->getRegisterInfo().getX86RegNum(RegNo);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000257}
258
259inline static unsigned char ModRMByte(unsigned Mod, unsigned RegOpcode,
260 unsigned RM) {
261 assert(Mod < 4 && RegOpcode < 8 && RM < 8 && "ModRM Fields out of range!");
262 return RM | (RegOpcode << 3) | (Mod << 6);
263}
264
Bruno Cardoso Lopes8e2537b2009-06-01 19:57:37 +0000265template<class CodeEmitter>
266void Emitter<CodeEmitter>::emitRegModRMByte(unsigned ModRMReg,
267 unsigned RegOpcodeFld){
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000268 MCE.emitByte(ModRMByte(3, RegOpcodeFld, getX86RegNum(ModRMReg)));
269}
270
Bruno Cardoso Lopes8e2537b2009-06-01 19:57:37 +0000271template<class CodeEmitter>
272void Emitter<CodeEmitter>::emitRegModRMByte(unsigned RegOpcodeFld) {
Evan Cheng5d0d34e2008-10-17 17:14:20 +0000273 MCE.emitByte(ModRMByte(3, RegOpcodeFld, 0));
274}
275
Bruno Cardoso Lopes8e2537b2009-06-01 19:57:37 +0000276template<class CodeEmitter>
277void Emitter<CodeEmitter>::emitSIBByte(unsigned SS,
278 unsigned Index,
279 unsigned Base) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000280 // SIB byte is in the same format as the ModRMByte...
281 MCE.emitByte(ModRMByte(SS, Index, Base));
282}
283
Bruno Cardoso Lopes8e2537b2009-06-01 19:57:37 +0000284template<class CodeEmitter>
285void Emitter<CodeEmitter>::emitConstant(uint64_t Val, unsigned Size) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000286 // Output the constant in little endian byte order...
287 for (unsigned i = 0; i != Size; ++i) {
288 MCE.emitByte(Val & 255);
289 Val >>= 8;
290 }
291}
292
293/// isDisp8 - Return true if this signed displacement fits in a 8-bit
294/// sign-extended field.
295static bool isDisp8(int Value) {
296 return Value == (signed char)Value;
297}
298
Chris Lattnerbdc2ea92009-07-10 05:27:43 +0000299static bool gvNeedsNonLazyPtr(const MachineOperand &GVOp,
300 const TargetMachine &TM) {
Chris Lattnerbdc2ea92009-07-10 05:27:43 +0000301 // For Darwin-64, simulate the linktime GOT by using the same non-lazy-pointer
Dale Johannesen2b65b742008-08-12 18:23:48 +0000302 // mechanism as 32-bit mode.
Chris Lattnerbdc2ea92009-07-10 05:27:43 +0000303 if (TM.getSubtarget<X86Subtarget>().is64Bit() &&
304 !TM.getSubtarget<X86Subtarget>().isTargetDarwin())
305 return false;
306
Chris Lattner8b1d2b92009-07-10 06:07:08 +0000307 // Return true if this is a reference to a stub containing the address of the
308 // global, not the global itself.
Chris Lattner6d62ab92009-07-10 06:29:59 +0000309 return isGlobalStubReference(GVOp.getTargetFlags());
Evan Cheng28e7e162008-01-04 10:46:51 +0000310}
311
Bruno Cardoso Lopes8e2537b2009-06-01 19:57:37 +0000312template<class CodeEmitter>
313void Emitter<CodeEmitter>::emitDisplacementField(const MachineOperand *RelocOp,
Bruno Cardoso Lopes670400e2009-08-05 00:11:21 +0000314 int DispVal,
315 intptr_t Adj /* = 0 */,
316 bool IsPCRel /* = true */) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000317 // If this is a simple integer displacement that doesn't require a relocation,
318 // emit it now.
319 if (!RelocOp) {
320 emitConstant(DispVal, 4);
321 return;
322 }
Bruno Cardoso Lopes670400e2009-08-05 00:11:21 +0000323
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000324 // Otherwise, this is something that requires a relocation. Emit it as such
325 // now.
Daniel Dunbar064aca12009-09-01 22:07:06 +0000326 unsigned RelocType = Is64BitMode ?
327 (IsPCRel ? X86::reloc_pcrel_word : X86::reloc_absolute_word_sext)
328 : (IsPIC ? X86::reloc_picrel_word : X86::reloc_absolute_word);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000329 if (RelocOp->isGlobal()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000330 // In 64-bit static small code model, we could potentially emit absolute.
Bruno Cardoso Lopes670400e2009-08-05 00:11:21 +0000331 // But it's probably not beneficial. If the MCE supports using RIP directly
332 // do it, otherwise fallback to absolute (this is determined by IsPCRel).
Bill Wendlingf3a655f2008-02-26 10:57:23 +0000333 // 89 05 00 00 00 00 mov %eax,0(%rip) # PC-relative
334 // 89 04 25 00 00 00 00 mov %eax,0x0 # Absolute
Chris Lattnerbdc2ea92009-07-10 05:27:43 +0000335 bool Indirect = gvNeedsNonLazyPtr(*RelocOp, TM);
Daniel Dunbar064aca12009-09-01 22:07:06 +0000336 emitGlobalAddress(RelocOp->getGlobal(), RelocType, RelocOp->getOffset(),
Jeffrey Yasskine233d8a2009-11-16 22:41:33 +0000337 Adj, Indirect);
Daniel Dunbar8ac6c042009-09-01 22:06:53 +0000338 } else if (RelocOp->isSymbol()) {
Daniel Dunbar064aca12009-09-01 22:07:06 +0000339 emitExternalSymbolAddress(RelocOp->getSymbolName(), RelocType);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000340 } else if (RelocOp->isCPI()) {
Daniel Dunbar064aca12009-09-01 22:07:06 +0000341 emitConstPoolAddress(RelocOp->getIndex(), RelocType,
Bruno Cardoso Lopes670400e2009-08-05 00:11:21 +0000342 RelocOp->getOffset(), Adj);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000343 } else {
Daniel Dunbar064aca12009-09-01 22:07:06 +0000344 assert(RelocOp->isJTI() && "Unexpected machine operand!");
345 emitJumpTableAddress(RelocOp->getIndex(), RelocType, Adj);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000346 }
347}
348
Bruno Cardoso Lopes8e2537b2009-06-01 19:57:37 +0000349template<class CodeEmitter>
350void Emitter<CodeEmitter>::emitMemModRMByte(const MachineInstr &MI,
Chris Lattner5b6b1782009-08-16 02:45:18 +0000351 unsigned Op,unsigned RegOpcodeField,
352 intptr_t PCAdj) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000353 const MachineOperand &Op3 = MI.getOperand(Op+3);
354 int DispVal = 0;
355 const MachineOperand *DispForReloc = 0;
356
357 // Figure out what sort of displacement we have to handle here.
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000358 if (Op3.isGlobal()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000359 DispForReloc = &Op3;
Daniel Dunbar8ac6c042009-09-01 22:06:53 +0000360 } else if (Op3.isSymbol()) {
361 DispForReloc = &Op3;
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000362 } else if (Op3.isCPI()) {
Bruno Cardoso Lopes670400e2009-08-05 00:11:21 +0000363 if (!MCE.earlyResolveAddresses() || Is64BitMode || IsPIC) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000364 DispForReloc = &Op3;
365 } else {
Chris Lattner6017d482007-12-30 23:10:15 +0000366 DispVal += MCE.getConstantPoolEntryAddress(Op3.getIndex());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000367 DispVal += Op3.getOffset();
368 }
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000369 } else if (Op3.isJTI()) {
Bruno Cardoso Lopes670400e2009-08-05 00:11:21 +0000370 if (!MCE.earlyResolveAddresses() || Is64BitMode || IsPIC) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000371 DispForReloc = &Op3;
372 } else {
Chris Lattner6017d482007-12-30 23:10:15 +0000373 DispVal += MCE.getJumpTableEntryAddress(Op3.getIndex());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000374 }
375 } else {
376 DispVal = Op3.getImm();
377 }
378
379 const MachineOperand &Base = MI.getOperand(Op);
380 const MachineOperand &Scale = MI.getOperand(Op+1);
381 const MachineOperand &IndexReg = MI.getOperand(Op+2);
382
383 unsigned BaseReg = Base.getReg();
384
Bruno Cardoso Lopes670400e2009-08-05 00:11:21 +0000385 // Indicate that the displacement will use an pcrel or absolute reference
386 // by default. MCEs able to resolve addresses on-the-fly use pcrel by default
387 // while others, unless explicit asked to use RIP, use absolute references.
388 bool IsPCRel = MCE.earlyResolveAddresses() ? true : false;
389
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000390 // Is a SIB byte needed?
Bruno Cardoso Lopes670400e2009-08-05 00:11:21 +0000391 // If no BaseReg, issue a RIP relative instruction only if the MCE can
392 // resolve addresses on-the-fly, otherwise use SIB (Intel Manual 2A, table
393 // 2-7) and absolute references.
Evan Cheng92569ce2009-05-12 00:07:35 +0000394 if ((!Is64BitMode || DispForReloc || BaseReg != 0) &&
Bruno Cardoso Lopes670400e2009-08-05 00:11:21 +0000395 IndexReg.getReg() == 0 &&
396 ((BaseReg == 0 && MCE.earlyResolveAddresses()) || BaseReg == X86::RIP ||
397 (BaseReg != 0 && getX86RegNum(BaseReg) != N86::ESP))) {
398 if (BaseReg == 0 || BaseReg == X86::RIP) { // Just a displacement?
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000399 // Emit special case [disp32] encoding
400 MCE.emitByte(ModRMByte(0, RegOpcodeField, 5));
Bruno Cardoso Lopes670400e2009-08-05 00:11:21 +0000401 emitDisplacementField(DispForReloc, DispVal, PCAdj, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000402 } else {
403 unsigned BaseRegNo = getX86RegNum(BaseReg);
404 if (!DispForReloc && DispVal == 0 && BaseRegNo != N86::EBP) {
405 // Emit simple indirect register encoding... [EAX] f.e.
406 MCE.emitByte(ModRMByte(0, RegOpcodeField, BaseRegNo));
407 } else if (!DispForReloc && isDisp8(DispVal)) {
408 // Emit the disp8 encoding... [REG+disp8]
409 MCE.emitByte(ModRMByte(1, RegOpcodeField, BaseRegNo));
410 emitConstant(DispVal, 1);
411 } else {
412 // Emit the most general non-SIB encoding: [REG+disp32]
413 MCE.emitByte(ModRMByte(2, RegOpcodeField, BaseRegNo));
Bruno Cardoso Lopes670400e2009-08-05 00:11:21 +0000414 emitDisplacementField(DispForReloc, DispVal, PCAdj, IsPCRel);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000415 }
416 }
417
418 } else { // We need a SIB byte, so start by outputting the ModR/M byte first
419 assert(IndexReg.getReg() != X86::ESP &&
420 IndexReg.getReg() != X86::RSP && "Cannot use ESP as index reg!");
421
422 bool ForceDisp32 = false;
423 bool ForceDisp8 = false;
424 if (BaseReg == 0) {
425 // If there is no base register, we emit the special case SIB byte with
426 // MOD=0, BASE=5, to JUST get the index, scale, and displacement.
427 MCE.emitByte(ModRMByte(0, RegOpcodeField, 4));
428 ForceDisp32 = true;
429 } else if (DispForReloc) {
430 // Emit the normal disp32 encoding.
431 MCE.emitByte(ModRMByte(2, RegOpcodeField, 4));
432 ForceDisp32 = true;
433 } else if (DispVal == 0 && getX86RegNum(BaseReg) != N86::EBP) {
434 // Emit no displacement ModR/M byte
435 MCE.emitByte(ModRMByte(0, RegOpcodeField, 4));
436 } else if (isDisp8(DispVal)) {
437 // Emit the disp8 encoding...
438 MCE.emitByte(ModRMByte(1, RegOpcodeField, 4));
439 ForceDisp8 = true; // Make sure to force 8 bit disp if Base=EBP
440 } else {
441 // Emit the normal disp32 encoding...
442 MCE.emitByte(ModRMByte(2, RegOpcodeField, 4));
443 }
444
445 // Calculate what the SS field value should be...
446 static const unsigned SSTable[] = { ~0, 0, 1, ~0, 2, ~0, ~0, ~0, 3 };
447 unsigned SS = SSTable[Scale.getImm()];
448
449 if (BaseReg == 0) {
Bruno Cardoso Lopes670400e2009-08-05 00:11:21 +0000450 // Handle the SIB byte for the case where there is no base, see Intel
451 // Manual 2A, table 2-7. The displacement has already been output.
Mon P Wang67b7fe22008-10-31 19:13:42 +0000452 unsigned IndexRegNo;
453 if (IndexReg.getReg())
454 IndexRegNo = getX86RegNum(IndexReg.getReg());
Bruno Cardoso Lopes670400e2009-08-05 00:11:21 +0000455 else // Examples: [ESP+1*<noreg>+4] or [scaled idx]+disp32 (MOD=0,BASE=5)
456 IndexRegNo = 4;
Mon P Wang67b7fe22008-10-31 19:13:42 +0000457 emitSIBByte(SS, IndexRegNo, 5);
Dan Gohman85a356f2008-11-10 22:09:58 +0000458 } else {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000459 unsigned BaseRegNo = getX86RegNum(BaseReg);
460 unsigned IndexRegNo;
461 if (IndexReg.getReg())
462 IndexRegNo = getX86RegNum(IndexReg.getReg());
463 else
464 IndexRegNo = 4; // For example [ESP+1*<noreg>+4]
465 emitSIBByte(SS, IndexRegNo, BaseRegNo);
466 }
467
468 // Do we need to output a displacement?
469 if (ForceDisp8) {
470 emitConstant(DispVal, 1);
471 } else if (DispVal != 0 || ForceDisp32) {
Bruno Cardoso Lopes670400e2009-08-05 00:11:21 +0000472 emitDisplacementField(DispForReloc, DispVal, PCAdj, IsPCRel);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000473 }
474 }
475}
476
Bruno Cardoso Lopes8e2537b2009-06-01 19:57:37 +0000477template<class CodeEmitter>
Chris Lattner5b6b1782009-08-16 02:45:18 +0000478void Emitter<CodeEmitter>::emitInstruction(const MachineInstr &MI,
479 const TargetInstrDesc *Desc) {
David Greene00f64b82010-01-05 01:28:53 +0000480 DEBUG(dbgs() << MI);
Evan Cheng872bd4b2008-03-14 07:13:42 +0000481
Devang Patel5450fc12009-10-06 02:19:11 +0000482 MCE.processDebugLoc(MI.getDebugLoc(), true);
Jeffrey Yasskin8ad296e2009-07-16 21:07:26 +0000483
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000484 unsigned Opcode = Desc->Opcode;
485
Andrew Lenharth7a5a4b22008-03-01 13:37:02 +0000486 // Emit the lock opcode prefix as needed.
Chris Lattner5b6b1782009-08-16 02:45:18 +0000487 if (Desc->TSFlags & X86II::LOCK)
488 MCE.emitByte(0xF0);
Andrew Lenharth7a5a4b22008-03-01 13:37:02 +0000489
Duncan Sandsa707cf82008-10-11 19:34:24 +0000490 // Emit segment override opcode prefix as needed.
Anton Korobeynikov975e1472008-10-11 19:09:15 +0000491 switch (Desc->TSFlags & X86II::SegOvrMask) {
492 case X86II::FS:
493 MCE.emitByte(0x64);
494 break;
495 case X86II::GS:
496 MCE.emitByte(0x65);
497 break;
Edwin Törökbd448e32009-07-14 16:55:14 +0000498 default: llvm_unreachable("Invalid segment!");
Anton Korobeynikov4b7be802008-10-12 10:30:11 +0000499 case 0: break; // No segment override!
Anton Korobeynikov975e1472008-10-11 19:09:15 +0000500 }
501
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000502 // Emit the repeat opcode prefix as needed.
Chris Lattner5b6b1782009-08-16 02:45:18 +0000503 if ((Desc->TSFlags & X86II::Op0Mask) == X86II::REP)
504 MCE.emitByte(0xF3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000505
506 // Emit the operand size opcode prefix as needed.
Chris Lattner5b6b1782009-08-16 02:45:18 +0000507 if (Desc->TSFlags & X86II::OpSize)
508 MCE.emitByte(0x66);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000509
510 // Emit the address size opcode prefix as needed.
Chris Lattner5b6b1782009-08-16 02:45:18 +0000511 if (Desc->TSFlags & X86II::AdSize)
512 MCE.emitByte(0x67);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000513
514 bool Need0FPrefix = false;
515 switch (Desc->TSFlags & X86II::Op0Mask) {
Evan Cheng0c835a82008-04-03 08:53:17 +0000516 case X86II::TB: // Two-byte opcode prefix
517 case X86II::T8: // 0F 38
518 case X86II::TA: // 0F 3A
519 Need0FPrefix = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000520 break;
Eric Christopherb5f948c2009-08-08 21:55:08 +0000521 case X86II::TF: // F2 0F 38
522 MCE.emitByte(0xF2);
523 Need0FPrefix = true;
524 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000525 case X86II::REP: break; // already handled.
526 case X86II::XS: // F3 0F
527 MCE.emitByte(0xF3);
528 Need0FPrefix = true;
529 break;
530 case X86II::XD: // F2 0F
531 MCE.emitByte(0xF2);
532 Need0FPrefix = true;
533 break;
534 case X86II::D8: case X86II::D9: case X86II::DA: case X86II::DB:
535 case X86II::DC: case X86II::DD: case X86II::DE: case X86II::DF:
536 MCE.emitByte(0xD8+
537 (((Desc->TSFlags & X86II::Op0Mask)-X86II::D8)
538 >> X86II::Op0Shift));
539 break; // Two-byte opcode prefix
Edwin Törökbd448e32009-07-14 16:55:14 +0000540 default: llvm_unreachable("Invalid prefix!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000541 case 0: break; // No prefix!
542 }
543
Chris Lattner5b6b1782009-08-16 02:45:18 +0000544 // Handle REX prefix.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000545 if (Is64BitMode) {
Chris Lattner5b6b1782009-08-16 02:45:18 +0000546 if (unsigned REX = X86InstrInfo::determineREX(MI))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000547 MCE.emitByte(0x40 | REX);
548 }
549
550 // 0x0F escape code must be emitted just before the opcode.
551 if (Need0FPrefix)
552 MCE.emitByte(0x0F);
553
Evan Cheng0c835a82008-04-03 08:53:17 +0000554 switch (Desc->TSFlags & X86II::Op0Mask) {
Chris Lattner5b6b1782009-08-16 02:45:18 +0000555 case X86II::TF: // F2 0F 38
556 case X86II::T8: // 0F 38
Evan Cheng0c835a82008-04-03 08:53:17 +0000557 MCE.emitByte(0x38);
558 break;
559 case X86II::TA: // 0F 3A
560 MCE.emitByte(0x3A);
561 break;
562 }
563
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000564 // If this is a two-address instruction, skip one of the register operands.
Chris Lattner0c2a4f32008-01-07 03:13:06 +0000565 unsigned NumOps = Desc->getNumOperands();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000566 unsigned CurOp = 0;
567 if (NumOps > 1 && Desc->getOperandConstraint(1, TOI::TIED_TO) != -1)
Evan Chengd49dbb82008-04-18 20:55:36 +0000568 ++CurOp;
569 else if (NumOps > 2 && Desc->getOperandConstraint(NumOps-1, TOI::TIED_TO)== 0)
570 // Skip the last source operand that is tied_to the dest reg. e.g. LXADD32
571 --NumOps;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000572
573 unsigned char BaseOpcode = II->getBaseOpcodeFor(Desc);
574 switch (Desc->TSFlags & X86II::FormMask) {
Chris Lattnerb4a3ef12009-08-16 02:36:40 +0000575 default:
576 llvm_unreachable("Unknown FormMask value in X86 MachineCodeEmitter!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000577 case X86II::Pseudo:
Evan Cheng0729ccf2008-01-05 00:41:47 +0000578 // Remember the current PC offset, this is the PIC relocation
579 // base address.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000580 switch (Opcode) {
581 default:
Chris Lattnerb4a3ef12009-08-16 02:36:40 +0000582 llvm_unreachable("psuedo instructions should be removed before code"
583 " emission");
Evan Cheng7c6c35e2008-03-05 02:34:36 +0000584 break;
Chris Lattner5b6b1782009-08-16 02:45:18 +0000585 case TargetInstrInfo::INLINEASM:
Evan Cheng4e1a7202008-11-19 23:21:11 +0000586 // We allow inline assembler nodes with empty bodies - they can
587 // implicitly define registers, which is ok for JIT.
Chris Lattner89357002009-10-12 04:22:44 +0000588 if (MI.getOperand(0).getSymbolName()[0])
589 llvm_report_error("JIT does not support inline asm!");
Evan Cheng7c6c35e2008-03-05 02:34:36 +0000590 break;
Dan Gohmanfa607c92008-07-01 00:05:16 +0000591 case TargetInstrInfo::DBG_LABEL:
592 case TargetInstrInfo::EH_LABEL:
Nicolas Geoffrayd326c7a2009-09-08 07:36:18 +0000593 case TargetInstrInfo::GC_LABEL:
Nicolas Geoffray0e757e12008-02-13 18:39:37 +0000594 MCE.emitLabel(MI.getOperand(0).getImm());
595 break;
Evan Chengb74b4b62008-03-17 06:56:52 +0000596 case TargetInstrInfo::IMPLICIT_DEF:
Jakob Stoklund Olesen8f12c7c2009-09-28 20:32:26 +0000597 case TargetInstrInfo::KILL:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000598 case X86::FP_REG_KILL:
599 break;
Evan Chengaf743252008-01-05 02:26:58 +0000600 case X86::MOVPC32r: {
Evan Cheng0729ccf2008-01-05 00:41:47 +0000601 // This emits the "call" portion of this pseudo instruction.
602 MCE.emitByte(BaseOpcode);
Nicolas Geoffraycb162a02008-04-16 20:10:13 +0000603 emitConstant(0, X86InstrInfo::sizeOfImm(Desc));
Evan Chengaf743252008-01-05 02:26:58 +0000604 // Remember PIC base.
Evan Cheng6e561c72008-12-10 02:32:19 +0000605 PICBaseOffset = (intptr_t) MCE.getCurrentPCOffset();
Dan Gohmanb41dfba2008-05-14 01:58:56 +0000606 X86JITInfo *JTI = TM.getJITInfo();
Evan Chengaf743252008-01-05 02:26:58 +0000607 JTI->setPICBase(MCE.getCurrentPCValue());
Evan Cheng0729ccf2008-01-05 00:41:47 +0000608 break;
609 }
Evan Chengaf743252008-01-05 02:26:58 +0000610 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000611 CurOp = NumOps;
612 break;
Chris Lattner5b6b1782009-08-16 02:45:18 +0000613 case X86II::RawFrm: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000614 MCE.emitByte(BaseOpcode);
Evan Cheng0729ccf2008-01-05 00:41:47 +0000615
Chris Lattner5b6b1782009-08-16 02:45:18 +0000616 if (CurOp == NumOps)
617 break;
618
619 const MachineOperand &MO = MI.getOperand(CurOp++);
Bill Wendling0768ef62008-08-21 08:38:54 +0000620
David Greene00f64b82010-01-05 01:28:53 +0000621 DEBUG(dbgs() << "RawFrm CurOp " << CurOp << "\n");
622 DEBUG(dbgs() << "isMBB " << MO.isMBB() << "\n");
623 DEBUG(dbgs() << "isGlobal " << MO.isGlobal() << "\n");
624 DEBUG(dbgs() << "isSymbol " << MO.isSymbol() << "\n");
625 DEBUG(dbgs() << "isImm " << MO.isImm() << "\n");
Bill Wendling0768ef62008-08-21 08:38:54 +0000626
Chris Lattner5b6b1782009-08-16 02:45:18 +0000627 if (MO.isMBB()) {
628 emitPCRelativeBlockAddress(MO.getMBB());
629 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000630 }
Chris Lattner5b6b1782009-08-16 02:45:18 +0000631
632 if (MO.isGlobal()) {
Chris Lattner5b6b1782009-08-16 02:45:18 +0000633 emitGlobalAddress(MO.getGlobal(), X86::reloc_pcrel_word,
Jeffrey Yasskine233d8a2009-11-16 22:41:33 +0000634 MO.getOffset(), 0);
Chris Lattner5b6b1782009-08-16 02:45:18 +0000635 break;
636 }
637
638 if (MO.isSymbol()) {
639 emitExternalSymbolAddress(MO.getSymbolName(), X86::reloc_pcrel_word);
640 break;
641 }
642
643 assert(MO.isImm() && "Unknown RawFrm operand!");
644 if (Opcode == X86::CALLpcrel32 || Opcode == X86::CALL64pcrel32) {
645 // Fix up immediate operand for pc relative calls.
646 intptr_t Imm = (intptr_t)MO.getImm();
647 Imm = Imm - MCE.getCurrentPCValue() - 4;
648 emitConstant(Imm, X86InstrInfo::sizeOfImm(Desc));
649 } else
650 emitConstant(MO.getImm(), X86InstrInfo::sizeOfImm(Desc));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000651 break;
Chris Lattner5b6b1782009-08-16 02:45:18 +0000652 }
653
Chris Lattnerb4a3ef12009-08-16 02:36:40 +0000654 case X86II::AddRegFrm: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000655 MCE.emitByte(BaseOpcode + getX86RegNum(MI.getOperand(CurOp++).getReg()));
656
Chris Lattnerb4a3ef12009-08-16 02:36:40 +0000657 if (CurOp == NumOps)
658 break;
659
660 const MachineOperand &MO1 = MI.getOperand(CurOp++);
661 unsigned Size = X86InstrInfo::sizeOfImm(Desc);
662 if (MO1.isImm()) {
663 emitConstant(MO1.getImm(), Size);
664 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000665 }
Chris Lattnerb4a3ef12009-08-16 02:36:40 +0000666
667 unsigned rt = Is64BitMode ? X86::reloc_pcrel_word
668 : (IsPIC ? X86::reloc_picrel_word : X86::reloc_absolute_word);
669 if (Opcode == X86::MOV64ri64i32)
670 rt = X86::reloc_absolute_word; // FIXME: add X86II flag?
671 // This should not occur on Darwin for relocatable objects.
672 if (Opcode == X86::MOV64ri)
673 rt = X86::reloc_absolute_dword; // FIXME: add X86II flag?
674 if (MO1.isGlobal()) {
Chris Lattnerb4a3ef12009-08-16 02:36:40 +0000675 bool Indirect = gvNeedsNonLazyPtr(MO1, TM);
676 emitGlobalAddress(MO1.getGlobal(), rt, MO1.getOffset(), 0,
Jeffrey Yasskine233d8a2009-11-16 22:41:33 +0000677 Indirect);
Chris Lattnerb4a3ef12009-08-16 02:36:40 +0000678 } else if (MO1.isSymbol())
679 emitExternalSymbolAddress(MO1.getSymbolName(), rt);
680 else if (MO1.isCPI())
681 emitConstPoolAddress(MO1.getIndex(), rt);
682 else if (MO1.isJTI())
683 emitJumpTableAddress(MO1.getIndex(), rt);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000684 break;
Chris Lattnerb4a3ef12009-08-16 02:36:40 +0000685 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000686
687 case X86II::MRMDestReg: {
688 MCE.emitByte(BaseOpcode);
689 emitRegModRMByte(MI.getOperand(CurOp).getReg(),
690 getX86RegNum(MI.getOperand(CurOp+1).getReg()));
691 CurOp += 2;
692 if (CurOp != NumOps)
Chris Lattnerb4a3ef12009-08-16 02:36:40 +0000693 emitConstant(MI.getOperand(CurOp++).getImm(),
694 X86InstrInfo::sizeOfImm(Desc));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000695 break;
696 }
697 case X86II::MRMDestMem: {
698 MCE.emitByte(BaseOpcode);
Rafael Espindola7f69c042009-03-28 17:03:24 +0000699 emitMemModRMByte(MI, CurOp,
700 getX86RegNum(MI.getOperand(CurOp + X86AddrNumOperands)
701 .getReg()));
702 CurOp += X86AddrNumOperands + 1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000703 if (CurOp != NumOps)
Chris Lattnerb4a3ef12009-08-16 02:36:40 +0000704 emitConstant(MI.getOperand(CurOp++).getImm(),
705 X86InstrInfo::sizeOfImm(Desc));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000706 break;
707 }
708
709 case X86II::MRMSrcReg:
710 MCE.emitByte(BaseOpcode);
711 emitRegModRMByte(MI.getOperand(CurOp+1).getReg(),
712 getX86RegNum(MI.getOperand(CurOp).getReg()));
713 CurOp += 2;
714 if (CurOp != NumOps)
Bruno Cardoso Lopes8e2537b2009-06-01 19:57:37 +0000715 emitConstant(MI.getOperand(CurOp++).getImm(),
716 X86InstrInfo::sizeOfImm(Desc));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000717 break;
718
719 case X86II::MRMSrcMem: {
Rafael Espindolabca99f72009-04-08 21:14:34 +0000720 // FIXME: Maybe lea should have its own form?
721 int AddrOperands;
722 if (Opcode == X86::LEA64r || Opcode == X86::LEA64_32r ||
723 Opcode == X86::LEA16r || Opcode == X86::LEA32r)
724 AddrOperands = X86AddrNumOperands - 1; // No segment register
725 else
726 AddrOperands = X86AddrNumOperands;
727
728 intptr_t PCAdj = (CurOp + AddrOperands + 1 != NumOps) ?
Rafael Espindola7f69c042009-03-28 17:03:24 +0000729 X86InstrInfo::sizeOfImm(Desc) : 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000730
731 MCE.emitByte(BaseOpcode);
732 emitMemModRMByte(MI, CurOp+1, getX86RegNum(MI.getOperand(CurOp).getReg()),
733 PCAdj);
Rafael Espindolabca99f72009-04-08 21:14:34 +0000734 CurOp += AddrOperands + 1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000735 if (CurOp != NumOps)
Bruno Cardoso Lopes8e2537b2009-06-01 19:57:37 +0000736 emitConstant(MI.getOperand(CurOp++).getImm(),
737 X86InstrInfo::sizeOfImm(Desc));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000738 break;
739 }
740
741 case X86II::MRM0r: case X86II::MRM1r:
742 case X86II::MRM2r: case X86II::MRM3r:
743 case X86II::MRM4r: case X86II::MRM5r:
Evan Cheng5d0d34e2008-10-17 17:14:20 +0000744 case X86II::MRM6r: case X86II::MRM7r: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000745 MCE.emitByte(BaseOpcode);
Evan Cheng5d0d34e2008-10-17 17:14:20 +0000746
Bill Wendling6ee76552009-05-28 23:40:46 +0000747 // Special handling of lfence, mfence, monitor, and mwait.
Evan Cheng5d0d34e2008-10-17 17:14:20 +0000748 if (Desc->getOpcode() == X86::LFENCE ||
Bill Wendling6ee76552009-05-28 23:40:46 +0000749 Desc->getOpcode() == X86::MFENCE ||
750 Desc->getOpcode() == X86::MONITOR ||
751 Desc->getOpcode() == X86::MWAIT) {
Evan Cheng5d0d34e2008-10-17 17:14:20 +0000752 emitRegModRMByte((Desc->TSFlags & X86II::FormMask)-X86II::MRM0r);
Bill Wendling6ee76552009-05-28 23:40:46 +0000753
754 switch (Desc->getOpcode()) {
755 default: break;
756 case X86::MONITOR:
757 MCE.emitByte(0xC8);
758 break;
759 case X86::MWAIT:
760 MCE.emitByte(0xC9);
761 break;
762 }
763 } else {
Evan Cheng5d0d34e2008-10-17 17:14:20 +0000764 emitRegModRMByte(MI.getOperand(CurOp++).getReg(),
765 (Desc->TSFlags & X86II::FormMask)-X86II::MRM0r);
Bill Wendling6ee76552009-05-28 23:40:46 +0000766 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000767
Chris Lattnerb4a3ef12009-08-16 02:36:40 +0000768 if (CurOp == NumOps)
769 break;
770
771 const MachineOperand &MO1 = MI.getOperand(CurOp++);
772 unsigned Size = X86InstrInfo::sizeOfImm(Desc);
773 if (MO1.isImm()) {
774 emitConstant(MO1.getImm(), Size);
775 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000776 }
Chris Lattnerb4a3ef12009-08-16 02:36:40 +0000777
778 unsigned rt = Is64BitMode ? X86::reloc_pcrel_word
779 : (IsPIC ? X86::reloc_picrel_word : X86::reloc_absolute_word);
780 if (Opcode == X86::MOV64ri32)
781 rt = X86::reloc_absolute_word_sext; // FIXME: add X86II flag?
782 if (MO1.isGlobal()) {
Chris Lattnerb4a3ef12009-08-16 02:36:40 +0000783 bool Indirect = gvNeedsNonLazyPtr(MO1, TM);
784 emitGlobalAddress(MO1.getGlobal(), rt, MO1.getOffset(), 0,
Jeffrey Yasskine233d8a2009-11-16 22:41:33 +0000785 Indirect);
Chris Lattnerb4a3ef12009-08-16 02:36:40 +0000786 } else if (MO1.isSymbol())
787 emitExternalSymbolAddress(MO1.getSymbolName(), rt);
788 else if (MO1.isCPI())
789 emitConstPoolAddress(MO1.getIndex(), rt);
790 else if (MO1.isJTI())
791 emitJumpTableAddress(MO1.getIndex(), rt);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000792 break;
Evan Cheng5d0d34e2008-10-17 17:14:20 +0000793 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000794
795 case X86II::MRM0m: case X86II::MRM1m:
796 case X86II::MRM2m: case X86II::MRM3m:
797 case X86II::MRM4m: case X86II::MRM5m:
798 case X86II::MRM6m: case X86II::MRM7m: {
Rafael Espindola7f69c042009-03-28 17:03:24 +0000799 intptr_t PCAdj = (CurOp + X86AddrNumOperands != NumOps) ?
Dale Johannesen1a51cff2009-05-06 19:04:30 +0000800 (MI.getOperand(CurOp+X86AddrNumOperands).isImm() ?
801 X86InstrInfo::sizeOfImm(Desc) : 4) : 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000802
803 MCE.emitByte(BaseOpcode);
804 emitMemModRMByte(MI, CurOp, (Desc->TSFlags & X86II::FormMask)-X86II::MRM0m,
805 PCAdj);
Rafael Espindola7f69c042009-03-28 17:03:24 +0000806 CurOp += X86AddrNumOperands;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000807
Chris Lattnerb4a3ef12009-08-16 02:36:40 +0000808 if (CurOp == NumOps)
809 break;
810
811 const MachineOperand &MO = MI.getOperand(CurOp++);
812 unsigned Size = X86InstrInfo::sizeOfImm(Desc);
813 if (MO.isImm()) {
814 emitConstant(MO.getImm(), Size);
815 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000816 }
Chris Lattnerb4a3ef12009-08-16 02:36:40 +0000817
818 unsigned rt = Is64BitMode ? X86::reloc_pcrel_word
819 : (IsPIC ? X86::reloc_picrel_word : X86::reloc_absolute_word);
820 if (Opcode == X86::MOV64mi32)
821 rt = X86::reloc_absolute_word_sext; // FIXME: add X86II flag?
822 if (MO.isGlobal()) {
Chris Lattnerb4a3ef12009-08-16 02:36:40 +0000823 bool Indirect = gvNeedsNonLazyPtr(MO, TM);
824 emitGlobalAddress(MO.getGlobal(), rt, MO.getOffset(), 0,
Jeffrey Yasskine233d8a2009-11-16 22:41:33 +0000825 Indirect);
Chris Lattnerb4a3ef12009-08-16 02:36:40 +0000826 } else if (MO.isSymbol())
827 emitExternalSymbolAddress(MO.getSymbolName(), rt);
828 else if (MO.isCPI())
829 emitConstPoolAddress(MO.getIndex(), rt);
830 else if (MO.isJTI())
831 emitJumpTableAddress(MO.getIndex(), rt);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000832 break;
833 }
834
835 case X86II::MRMInitReg:
836 MCE.emitByte(BaseOpcode);
837 // Duplicate register, used by things like MOV8r0 (aka xor reg,reg).
838 emitRegModRMByte(MI.getOperand(CurOp).getReg(),
839 getX86RegNum(MI.getOperand(CurOp).getReg()));
840 ++CurOp;
841 break;
842 }
843
Evan Cheng6032b652008-03-05 02:08:03 +0000844 if (!Desc->isVariadic() && CurOp != NumOps) {
Edwin Török4d9756a2009-07-08 20:53:28 +0000845#ifndef NDEBUG
David Greene00f64b82010-01-05 01:28:53 +0000846 dbgs() << "Cannot encode all operands of: " << MI << "\n";
Edwin Török4d9756a2009-07-08 20:53:28 +0000847#endif
Edwin Törökbd448e32009-07-14 16:55:14 +0000848 llvm_unreachable(0);
Evan Cheng6032b652008-03-05 02:08:03 +0000849 }
Devang Patel5450fc12009-10-06 02:19:11 +0000850
851 MCE.processDebugLoc(MI.getDebugLoc(), false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000852}
Daniel Dunbar2f379632009-08-27 08:12:55 +0000853
854// Adapt the Emitter / CodeEmitter interfaces to MCCodeEmitter.
855//
856// FIXME: This is a total hack designed to allow work on llvm-mc to proceed
857// without being blocked on various cleanups needed to support a clean interface
858// to instruction encoding.
859//
860// Look away!
861
862#include "llvm/DerivedTypes.h"
863
864namespace {
865class MCSingleInstructionCodeEmitter : public MachineCodeEmitter {
866 uint8_t Data[256];
867
868public:
869 MCSingleInstructionCodeEmitter() { reset(); }
870
871 void reset() {
872 BufferBegin = Data;
873 BufferEnd = array_endof(Data);
874 CurBufferPtr = Data;
875 }
876
877 StringRef str() {
878 return StringRef(reinterpret_cast<char*>(BufferBegin),
879 CurBufferPtr - BufferBegin);
880 }
881
882 virtual void startFunction(MachineFunction &F) {}
883 virtual bool finishFunction(MachineFunction &F) { return false; }
884 virtual void emitLabel(uint64_t LabelID) {}
885 virtual void StartMachineBasicBlock(MachineBasicBlock *MBB) {}
886 virtual bool earlyResolveAddresses() const { return false; }
887 virtual void addRelocation(const MachineRelocation &MR) { }
888 virtual uintptr_t getConstantPoolEntryAddress(unsigned Index) const {
889 return 0;
890 }
891 virtual uintptr_t getJumpTableEntryAddress(unsigned Index) const {
892 return 0;
893 }
894 virtual uintptr_t getMachineBasicBlockAddress(MachineBasicBlock *MBB) const {
895 return 0;
896 }
897 virtual uintptr_t getLabelAddress(uint64_t LabelID) const {
898 return 0;
899 }
900 virtual void setModuleInfo(MachineModuleInfo* Info) {}
901};
902
903class X86MCCodeEmitter : public MCCodeEmitter {
904 X86MCCodeEmitter(const X86MCCodeEmitter &); // DO NOT IMPLEMENT
905 void operator=(const X86MCCodeEmitter &); // DO NOT IMPLEMENT
906
907private:
908 X86TargetMachine &TM;
909 llvm::Function *DummyF;
910 TargetData *DummyTD;
911 mutable llvm::MachineFunction *DummyMF;
912 llvm::MachineBasicBlock *DummyMBB;
913
914 MCSingleInstructionCodeEmitter *InstrEmitter;
915 Emitter<MachineCodeEmitter> *Emit;
916
917public:
918 X86MCCodeEmitter(X86TargetMachine &_TM) : TM(_TM) {
919 // Verily, thou shouldst avert thine eyes.
920 const llvm::FunctionType *FTy =
921 FunctionType::get(llvm::Type::getVoidTy(getGlobalContext()), false);
922 DummyF = Function::Create(FTy, GlobalValue::InternalLinkage);
923 DummyTD = new TargetData("");
924 DummyMF = new MachineFunction(DummyF, TM);
925 DummyMBB = DummyMF->CreateMachineBasicBlock();
926
927 InstrEmitter = new MCSingleInstructionCodeEmitter();
928 Emit = new Emitter<MachineCodeEmitter>(TM, *InstrEmitter,
929 *TM.getInstrInfo(),
930 *DummyTD, false);
931 }
932 ~X86MCCodeEmitter() {
933 delete Emit;
934 delete InstrEmitter;
935 delete DummyMF;
936 delete DummyF;
937 }
938
939 bool AddRegToInstr(const MCInst &MI, MachineInstr *Instr,
940 unsigned Start) const {
941 if (Start + 1 > MI.getNumOperands())
942 return false;
943
944 const MCOperand &Op = MI.getOperand(Start);
945 if (!Op.isReg()) return false;
946
947 Instr->addOperand(MachineOperand::CreateReg(Op.getReg(), false));
948 return true;
949 }
950
951 bool AddImmToInstr(const MCInst &MI, MachineInstr *Instr,
952 unsigned Start) const {
953 if (Start + 1 > MI.getNumOperands())
954 return false;
955
956 const MCOperand &Op = MI.getOperand(Start);
957 if (Op.isImm()) {
958 Instr->addOperand(MachineOperand::CreateImm(Op.getImm()));
959 return true;
960 }
Daniel Dunbar6e966212009-08-31 08:08:38 +0000961 if (!Op.isExpr())
Daniel Dunbar2f379632009-08-27 08:12:55 +0000962 return false;
963
Daniel Dunbar6e966212009-08-31 08:08:38 +0000964 const MCExpr *Expr = Op.getExpr();
965 if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr)) {
966 Instr->addOperand(MachineOperand::CreateImm(CE->getValue()));
Daniel Dunbara8d310b2009-08-30 06:17:49 +0000967 return true;
968 }
969
Daniel Dunbar2f379632009-08-27 08:12:55 +0000970 // FIXME: Relocation / fixup.
971 Instr->addOperand(MachineOperand::CreateImm(0));
972 return true;
973 }
974
975 bool AddLMemToInstr(const MCInst &MI, MachineInstr *Instr,
976 unsigned Start) const {
977 return (AddRegToInstr(MI, Instr, Start + 0) &&
978 AddImmToInstr(MI, Instr, Start + 1) &&
979 AddRegToInstr(MI, Instr, Start + 2) &&
980 AddImmToInstr(MI, Instr, Start + 3));
981 }
982
983 bool AddMemToInstr(const MCInst &MI, MachineInstr *Instr,
984 unsigned Start) const {
985 return (AddRegToInstr(MI, Instr, Start + 0) &&
986 AddImmToInstr(MI, Instr, Start + 1) &&
987 AddRegToInstr(MI, Instr, Start + 2) &&
988 AddImmToInstr(MI, Instr, Start + 3) &&
989 AddRegToInstr(MI, Instr, Start + 4));
990 }
991
992 void EncodeInstruction(const MCInst &MI, raw_ostream &OS) const {
993 // Don't look yet!
994
995 // Convert the MCInst to a MachineInstr so we can (ab)use the regular
996 // emitter.
997 const X86InstrInfo &II = *TM.getInstrInfo();
998 const TargetInstrDesc &Desc = II.get(MI.getOpcode());
999 MachineInstr *Instr = DummyMF->CreateMachineInstr(Desc, DebugLoc());
1000 DummyMBB->push_back(Instr);
1001
1002 unsigned Opcode = MI.getOpcode();
1003 unsigned NumOps = MI.getNumOperands();
1004 unsigned CurOp = 0;
1005 if (NumOps > 1 && Desc.getOperandConstraint(1, TOI::TIED_TO) != -1) {
1006 Instr->addOperand(MachineOperand::CreateReg(0, false));
1007 ++CurOp;
1008 } else if (NumOps > 2 &&
1009 Desc.getOperandConstraint(NumOps-1, TOI::TIED_TO)== 0)
1010 // Skip the last source operand that is tied_to the dest reg. e.g. LXADD32
1011 --NumOps;
1012
1013 bool OK = true;
1014 switch (Desc.TSFlags & X86II::FormMask) {
1015 case X86II::MRMDestReg:
1016 case X86II::MRMSrcReg:
1017 // Matching doesn't fill this in completely, we have to choose operand 0
1018 // for a tied register.
1019 OK &= AddRegToInstr(MI, Instr, 0); CurOp++;
1020 OK &= AddRegToInstr(MI, Instr, CurOp++);
1021 if (CurOp < NumOps)
1022 OK &= AddImmToInstr(MI, Instr, CurOp);
1023 break;
1024
1025 case X86II::RawFrm:
1026 if (CurOp < NumOps) {
1027 // Hack to make branches work.
1028 if (!(Desc.TSFlags & X86II::ImmMask) &&
Daniel Dunbar6e966212009-08-31 08:08:38 +00001029 MI.getOperand(0).isExpr() &&
1030 isa<MCSymbolRefExpr>(MI.getOperand(0).getExpr()))
Daniel Dunbar2f379632009-08-27 08:12:55 +00001031 Instr->addOperand(MachineOperand::CreateMBB(DummyMBB));
1032 else
1033 OK &= AddImmToInstr(MI, Instr, CurOp);
1034 }
1035 break;
1036
1037 case X86II::AddRegFrm:
1038 OK &= AddRegToInstr(MI, Instr, CurOp++);
1039 if (CurOp < NumOps)
1040 OK &= AddImmToInstr(MI, Instr, CurOp);
1041 break;
1042
1043 case X86II::MRM0r: case X86II::MRM1r:
1044 case X86II::MRM2r: case X86II::MRM3r:
1045 case X86II::MRM4r: case X86II::MRM5r:
1046 case X86II::MRM6r: case X86II::MRM7r:
1047 // Matching doesn't fill this in completely, we have to choose operand 0
1048 // for a tied register.
1049 OK &= AddRegToInstr(MI, Instr, 0); CurOp++;
1050 if (CurOp < NumOps)
1051 OK &= AddImmToInstr(MI, Instr, CurOp);
1052 break;
1053
1054 case X86II::MRM0m: case X86II::MRM1m:
1055 case X86II::MRM2m: case X86II::MRM3m:
1056 case X86II::MRM4m: case X86II::MRM5m:
1057 case X86II::MRM6m: case X86II::MRM7m:
1058 OK &= AddMemToInstr(MI, Instr, CurOp); CurOp += 5;
1059 if (CurOp < NumOps)
1060 OK &= AddImmToInstr(MI, Instr, CurOp);
1061 break;
1062
1063 case X86II::MRMSrcMem:
1064 OK &= AddRegToInstr(MI, Instr, CurOp++);
1065 if (Opcode == X86::LEA64r || Opcode == X86::LEA64_32r ||
1066 Opcode == X86::LEA16r || Opcode == X86::LEA32r)
1067 OK &= AddLMemToInstr(MI, Instr, CurOp);
1068 else
1069 OK &= AddMemToInstr(MI, Instr, CurOp);
1070 break;
1071
1072 case X86II::MRMDestMem:
1073 OK &= AddMemToInstr(MI, Instr, CurOp); CurOp += 5;
1074 OK &= AddRegToInstr(MI, Instr, CurOp);
1075 break;
1076
1077 default:
1078 case X86II::MRMInitReg:
1079 case X86II::Pseudo:
1080 OK = false;
1081 break;
1082 }
1083
1084 if (!OK) {
David Greene00f64b82010-01-05 01:28:53 +00001085 dbgs() << "couldn't convert inst '";
Chris Lattnerad1950e2009-09-03 05:39:09 +00001086 MI.dump();
David Greene00f64b82010-01-05 01:28:53 +00001087 dbgs() << "' to machine instr:\n";
Daniel Dunbar2f379632009-08-27 08:12:55 +00001088 Instr->dump();
1089 }
1090
1091 InstrEmitter->reset();
1092 if (OK)
1093 Emit->emitInstruction(*Instr, &Desc);
1094 OS << InstrEmitter->str();
1095
1096 Instr->eraseFromParent();
1097 }
1098};
1099}
1100
1101// Ok, now you can look.
1102MCCodeEmitter *llvm::createX86MCCodeEmitter(const Target &,
1103 TargetMachine &TM) {
1104 return new X86MCCodeEmitter(static_cast<X86TargetMachine&>(TM));
1105}