blob: 7ca786441cefa16bf2573800a28bc43ea475dc4d [file] [log] [blame]
Chris Lattnerbf4b6a82009-09-20 07:41:30 +00001//===-- X86AsmPrinter.cpp - Convert X86 LLVM code to AT&T assembly --------===//
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002//
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//
Chris Lattnerbf4b6a82009-09-20 07:41:30 +000010// This file contains a printer that converts from our internal representation
Chris Lattnerd7dede22010-01-28 01:02:27 +000011// of machine-dependent LLVM code to X86 machine code.
Dan Gohmanf17a25c2007-07-18 16:29:46 +000012//
13//===----------------------------------------------------------------------===//
14
Chris Lattnerbf4b6a82009-09-20 07:41:30 +000015#include "X86AsmPrinter.h"
16#include "X86ATTInstPrinter.h"
17#include "X86IntelInstPrinter.h"
18#include "X86MCInstLower.h"
19#include "X86.h"
20#include "X86COFF.h"
21#include "X86COFFMachineModuleInfo.h"
22#include "X86MachineFunctionInfo.h"
23#include "X86TargetMachine.h"
24#include "llvm/CallingConv.h"
25#include "llvm/DerivedTypes.h"
26#include "llvm/Module.h"
27#include "llvm/Type.h"
28#include "llvm/Assembly/Writer.h"
29#include "llvm/MC/MCContext.h"
30#include "llvm/MC/MCSectionMachO.h"
31#include "llvm/MC/MCStreamer.h"
32#include "llvm/MC/MCSymbol.h"
33#include "llvm/CodeGen/MachineJumpTableInfo.h"
34#include "llvm/CodeGen/MachineModuleInfoImpls.h"
35#include "llvm/Support/ErrorHandling.h"
36#include "llvm/Support/FormattedStream.h"
Chris Lattnerbf4b6a82009-09-20 07:41:30 +000037#include "llvm/MC/MCAsmInfo.h"
38#include "llvm/Target/TargetLoweringObjectFile.h"
39#include "llvm/Target/TargetOptions.h"
40#include "llvm/Target/TargetRegistry.h"
41#include "llvm/ADT/SmallString.h"
Chris Lattnerbf4b6a82009-09-20 07:41:30 +000042using namespace llvm;
43
Chris Lattnerbf4b6a82009-09-20 07:41:30 +000044//===----------------------------------------------------------------------===//
45// Primitive Helper Functions.
46//===----------------------------------------------------------------------===//
47
Chris Lattnerbf4b6a82009-09-20 07:41:30 +000048void X86AsmPrinter::PrintPICBaseSymbol() const {
Chris Lattner541d8902010-01-26 06:28:43 +000049 const TargetLowering *TLI = TM.getTargetLowering();
50 O << *static_cast<const X86TargetLowering*>(TLI)->getPICBaseSymbol(MF,
51 OutContext);
Chris Lattnerbf4b6a82009-09-20 07:41:30 +000052}
53
Chris Lattnerd7dede22010-01-28 01:02:27 +000054/// runOnMachineFunction - Emit the function body.
Chris Lattnerbf4b6a82009-09-20 07:41:30 +000055///
56bool X86AsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Chris Lattnerbf4b6a82009-09-20 07:41:30 +000057 SetupMachineFunction(MF);
Chris Lattnere5980672010-01-26 23:18:44 +000058
59 // COFF and Cygwin specific mangling stuff. This should be moved out to the
60 // mangler or handled some other way?
Chris Lattnerbf4b6a82009-09-20 07:41:30 +000061 if (Subtarget->isTargetCOFF()) {
62 X86COFFMachineModuleInfo &COFFMMI =
Chris Lattnere5980672010-01-26 23:18:44 +000063 MMI->getObjFileInfo<X86COFFMachineModuleInfo>();
Chris Lattnerbf4b6a82009-09-20 07:41:30 +000064
65 // Populate function information map. Don't want to populate
66 // non-stdcall or non-fastcall functions' information right now.
Chris Lattnere5980672010-01-26 23:18:44 +000067 const Function *F = MF.getFunction();
68 CallingConv::ID CC = F->getCallingConv();
Chris Lattnerbf4b6a82009-09-20 07:41:30 +000069 if (CC == CallingConv::X86_StdCall || CC == CallingConv::X86_FastCall)
70 COFFMMI.AddFunctionInfo(F, *MF.getInfo<X86MachineFunctionInfo>());
71 }
Chris Lattnere5980672010-01-26 23:18:44 +000072 if (Subtarget->isTargetCygMing()) {
73 const Function *F = MF.getFunction();
74 X86COFFMachineModuleInfo &COFFMMI =
75 MMI->getObjFileInfo<X86COFFMachineModuleInfo>();
76 COFFMMI.DecorateCygMingName(CurrentFnSym, OutContext,F,*TM.getTargetData());
77
78 O << "\t.def\t " << *CurrentFnSym;
79 O << ";\t.scl\t" <<
80 (F->hasInternalLinkage() ? COFF::C_STAT : COFF::C_EXT)
81 << ";\t.type\t" << (COFF::DT_FCN << COFF::N_BTSHFT)
82 << ";\t.endef\n";
83 }
84
85 // Have common code print out the function header with linkage info etc.
86 EmitFunctionHeader();
87
Chris Lattnerd7dede22010-01-28 01:02:27 +000088 // Emit the rest of the function body.
89 EmitFunctionBody();
Chris Lattnerbf4b6a82009-09-20 07:41:30 +000090
91 // We didn't modify anything.
92 return false;
93}
94
95/// printSymbolOperand - Print a raw symbol reference operand. This handles
96/// jump tables, constant pools, global address and external symbols, all of
97/// which print to a label with various suffixes for relocation types etc.
98void X86AsmPrinter::printSymbolOperand(const MachineOperand &MO) {
99 switch (MO.getType()) {
100 default: llvm_unreachable("unknown symbol type!");
101 case MachineOperand::MO_JumpTableIndex:
Chris Lattner242edfc2010-01-23 05:26:25 +0000102 O << *GetJTISymbol(MO.getIndex());
Chris Lattnerbf4b6a82009-09-20 07:41:30 +0000103 break;
104 case MachineOperand::MO_ConstantPoolIndex:
Chris Lattner242edfc2010-01-23 05:26:25 +0000105 O << *GetCPISymbol(MO.getIndex());
Chris Lattnerbf4b6a82009-09-20 07:41:30 +0000106 printOffset(MO.getOffset());
107 break;
108 case MachineOperand::MO_GlobalAddress: {
109 const GlobalValue *GV = MO.getGlobal();
110
Chris Lattnerb6ed5862010-01-18 00:59:24 +0000111 MCSymbol *GVSym;
Chris Lattnerbf4b6a82009-09-20 07:41:30 +0000112 if (MO.getTargetFlags() == X86II::MO_DARWIN_STUB)
Chris Lattnera6f2a102010-01-16 18:37:32 +0000113 GVSym = GetSymbolWithGlobalValueBase(GV, "$stub");
Chris Lattnerbf4b6a82009-09-20 07:41:30 +0000114 else if (MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY ||
115 MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY_PIC_BASE ||
116 MO.getTargetFlags() == X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE)
Chris Lattnera6f2a102010-01-16 18:37:32 +0000117 GVSym = GetSymbolWithGlobalValueBase(GV, "$non_lazy_ptr");
Chris Lattnerc53c24c2010-01-16 00:51:39 +0000118 else
119 GVSym = GetGlobalValueSymbol(GV);
120
Chris Lattnerbf4b6a82009-09-20 07:41:30 +0000121 if (Subtarget->isTargetCygMing()) {
Anton Korobeynikovd5fcb1d2009-10-15 22:36:18 +0000122 X86COFFMachineModuleInfo &COFFMMI =
Chris Lattnerbf4b6a82009-09-20 07:41:30 +0000123 MMI->getObjFileInfo<X86COFFMachineModuleInfo>();
Chris Lattnerc53c24c2010-01-16 00:51:39 +0000124 COFFMMI.DecorateCygMingName(GVSym, OutContext, GV, *TM.getTargetData());
Chris Lattnerbf4b6a82009-09-20 07:41:30 +0000125 }
126
127 // Handle dllimport linkage.
128 if (MO.getTargetFlags() == X86II::MO_DLLIMPORT)
Chris Lattnerc53c24c2010-01-16 00:51:39 +0000129 GVSym = OutContext.GetOrCreateSymbol(Twine("__imp_") + GVSym->getName());
Chris Lattnerbf4b6a82009-09-20 07:41:30 +0000130
131 if (MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY ||
132 MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY_PIC_BASE) {
Chris Lattnera6f2a102010-01-16 18:37:32 +0000133 MCSymbol *Sym = GetSymbolWithGlobalValueBase(GV, "$non_lazy_ptr");
Chris Lattnerbf4b6a82009-09-20 07:41:30 +0000134
Chris Lattner77653062010-02-03 06:18:30 +0000135 MCSymbol *&StubSym =
Chris Lattnerbf4b6a82009-09-20 07:41:30 +0000136 MMI->getObjFileInfo<MachineModuleInfoMachO>().getGVStubEntry(Sym);
Chris Lattner178a9322010-01-16 01:00:27 +0000137 if (StubSym == 0)
138 StubSym = GetGlobalValueSymbol(GV);
139
Chris Lattnerbf4b6a82009-09-20 07:41:30 +0000140 } else if (MO.getTargetFlags() == X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE){
Chris Lattnera6f2a102010-01-16 18:37:32 +0000141 MCSymbol *Sym = GetSymbolWithGlobalValueBase(GV, "$non_lazy_ptr");
Chris Lattner77653062010-02-03 06:18:30 +0000142 MCSymbol *&StubSym =
Chris Lattnerbf4b6a82009-09-20 07:41:30 +0000143 MMI->getObjFileInfo<MachineModuleInfoMachO>().getHiddenGVStubEntry(Sym);
Chris Lattner178a9322010-01-16 01:00:27 +0000144 if (StubSym == 0)
145 StubSym = GetGlobalValueSymbol(GV);
Chris Lattnerbf4b6a82009-09-20 07:41:30 +0000146 } else if (MO.getTargetFlags() == X86II::MO_DARWIN_STUB) {
Chris Lattnera6f2a102010-01-16 18:37:32 +0000147 MCSymbol *Sym = GetSymbolWithGlobalValueBase(GV, "$stub");
Chris Lattner77653062010-02-03 06:18:30 +0000148 MCSymbol *&StubSym =
Chris Lattnerbf4b6a82009-09-20 07:41:30 +0000149 MMI->getObjFileInfo<MachineModuleInfoMachO>().getFnStubEntry(Sym);
Chris Lattner178a9322010-01-16 01:00:27 +0000150 if (StubSym == 0)
151 StubSym = GetGlobalValueSymbol(GV);
Chris Lattnerbf4b6a82009-09-20 07:41:30 +0000152 }
153
154 // If the name begins with a dollar-sign, enclose it in parens. We do this
155 // to avoid having it look like an integer immediate to the assembler.
Chris Lattnerc53c24c2010-01-16 00:51:39 +0000156 if (GVSym->getName()[0] != '$')
Chris Lattnerce409842010-01-17 21:43:43 +0000157 O << *GVSym;
158 else
159 O << '(' << *GVSym << ')';
Chris Lattnerbf4b6a82009-09-20 07:41:30 +0000160 printOffset(MO.getOffset());
161 break;
162 }
163 case MachineOperand::MO_ExternalSymbol: {
Chris Lattnerf80743e2010-01-13 07:56:59 +0000164 const MCSymbol *SymToPrint;
Chris Lattnerbf4b6a82009-09-20 07:41:30 +0000165 if (MO.getTargetFlags() == X86II::MO_DARWIN_STUB) {
Chris Lattner178a9322010-01-16 01:00:27 +0000166 SmallString<128> TempNameStr;
167 TempNameStr += StringRef(MO.getSymbolName());
168 TempNameStr += StringRef("$stub");
169
Chris Lattner77653062010-02-03 06:18:30 +0000170 MCSymbol *Sym = GetExternalSymbolSymbol(TempNameStr.str());
171 MCSymbol *&StubSym =
Chris Lattnerbf4b6a82009-09-20 07:41:30 +0000172 MMI->getObjFileInfo<MachineModuleInfoMachO>().getFnStubEntry(Sym);
173 if (StubSym == 0) {
Chris Lattnera38b2872010-01-13 06:38:18 +0000174 TempNameStr.erase(TempNameStr.end()-5, TempNameStr.end());
175 StubSym = OutContext.GetOrCreateSymbol(TempNameStr.str());
Chris Lattnerbf4b6a82009-09-20 07:41:30 +0000176 }
Chris Lattnerf80743e2010-01-13 07:56:59 +0000177 SymToPrint = StubSym;
178 } else {
Chris Lattner178a9322010-01-16 01:00:27 +0000179 SymToPrint = GetExternalSymbolSymbol(MO.getSymbolName());
Chris Lattnerbf4b6a82009-09-20 07:41:30 +0000180 }
181
182 // If the name begins with a dollar-sign, enclose it in parens. We do this
183 // to avoid having it look like an integer immediate to the assembler.
Chris Lattnerf80743e2010-01-13 07:56:59 +0000184 if (SymToPrint->getName()[0] != '$')
Chris Lattnerce409842010-01-17 21:43:43 +0000185 O << *SymToPrint;
186 else
187 O << '(' << *SymToPrint << '(';
Chris Lattnerbf4b6a82009-09-20 07:41:30 +0000188 break;
189 }
190 }
191
192 switch (MO.getTargetFlags()) {
193 default:
194 llvm_unreachable("Unknown target flag on GV operand");
195 case X86II::MO_NO_FLAG: // No flag.
196 break;
197 case X86II::MO_DARWIN_NONLAZY:
198 case X86II::MO_DLLIMPORT:
199 case X86II::MO_DARWIN_STUB:
200 // These affect the name of the symbol, not any suffix.
201 break;
202 case X86II::MO_GOT_ABSOLUTE_ADDRESS:
203 O << " + [.-";
204 PrintPICBaseSymbol();
205 O << ']';
206 break;
207 case X86II::MO_PIC_BASE_OFFSET:
208 case X86II::MO_DARWIN_NONLAZY_PIC_BASE:
209 case X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE:
210 O << '-';
211 PrintPICBaseSymbol();
212 break;
213 case X86II::MO_TLSGD: O << "@TLSGD"; break;
214 case X86II::MO_GOTTPOFF: O << "@GOTTPOFF"; break;
215 case X86II::MO_INDNTPOFF: O << "@INDNTPOFF"; break;
216 case X86II::MO_TPOFF: O << "@TPOFF"; break;
217 case X86II::MO_NTPOFF: O << "@NTPOFF"; break;
218 case X86II::MO_GOTPCREL: O << "@GOTPCREL"; break;
219 case X86II::MO_GOT: O << "@GOT"; break;
220 case X86II::MO_GOTOFF: O << "@GOTOFF"; break;
221 case X86II::MO_PLT: O << "@PLT"; break;
222 }
223}
224
225/// print_pcrel_imm - This is used to print an immediate value that ends up
226/// being encoded as a pc-relative value. These print slightly differently, for
227/// example, a $ is not emitted.
228void X86AsmPrinter::print_pcrel_imm(const MachineInstr *MI, unsigned OpNo) {
229 const MachineOperand &MO = MI->getOperand(OpNo);
230 switch (MO.getType()) {
231 default: llvm_unreachable("Unknown pcrel immediate operand");
232 case MachineOperand::MO_Immediate:
233 O << MO.getImm();
234 return;
235 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner84d5ca92010-01-26 04:55:51 +0000236 O << *MO.getMBB()->getSymbol(OutContext);
Chris Lattnerbf4b6a82009-09-20 07:41:30 +0000237 return;
238 case MachineOperand::MO_GlobalAddress:
239 case MachineOperand::MO_ExternalSymbol:
240 printSymbolOperand(MO);
241 return;
242 }
243}
244
245
246void X86AsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo,
Chris Lattnerb6ed5862010-01-18 00:59:24 +0000247 const char *Modifier) {
Chris Lattnerbf4b6a82009-09-20 07:41:30 +0000248 const MachineOperand &MO = MI->getOperand(OpNo);
249 switch (MO.getType()) {
250 default: llvm_unreachable("unknown operand type!");
251 case MachineOperand::MO_Register: {
252 O << '%';
253 unsigned Reg = MO.getReg();
254 if (Modifier && strncmp(Modifier, "subreg", strlen("subreg")) == 0) {
255 EVT VT = (strcmp(Modifier+6,"64") == 0) ?
256 MVT::i64 : ((strcmp(Modifier+6, "32") == 0) ? MVT::i32 :
257 ((strcmp(Modifier+6,"16") == 0) ? MVT::i16 : MVT::i8));
258 Reg = getX86SubSuperRegister(Reg, VT);
259 }
260 O << X86ATTInstPrinter::getRegisterName(Reg);
261 return;
262 }
263
264 case MachineOperand::MO_Immediate:
265 O << '$' << MO.getImm();
266 return;
267
268 case MachineOperand::MO_JumpTableIndex:
269 case MachineOperand::MO_ConstantPoolIndex:
270 case MachineOperand::MO_GlobalAddress:
271 case MachineOperand::MO_ExternalSymbol: {
272 O << '$';
273 printSymbolOperand(MO);
274 break;
275 }
276 }
277}
278
279void X86AsmPrinter::printSSECC(const MachineInstr *MI, unsigned Op) {
280 unsigned char value = MI->getOperand(Op).getImm();
281 assert(value <= 7 && "Invalid ssecc argument!");
282 switch (value) {
283 case 0: O << "eq"; break;
284 case 1: O << "lt"; break;
285 case 2: O << "le"; break;
286 case 3: O << "unord"; break;
287 case 4: O << "neq"; break;
288 case 5: O << "nlt"; break;
289 case 6: O << "nle"; break;
290 case 7: O << "ord"; break;
291 }
292}
293
294void X86AsmPrinter::printLeaMemReference(const MachineInstr *MI, unsigned Op,
295 const char *Modifier) {
296 const MachineOperand &BaseReg = MI->getOperand(Op);
297 const MachineOperand &IndexReg = MI->getOperand(Op+2);
298 const MachineOperand &DispSpec = MI->getOperand(Op+3);
299
300 // If we really don't want to print out (rip), don't.
301 bool HasBaseReg = BaseReg.getReg() != 0;
302 if (HasBaseReg && Modifier && !strcmp(Modifier, "no-rip") &&
303 BaseReg.getReg() == X86::RIP)
304 HasBaseReg = false;
305
306 // HasParenPart - True if we will print out the () part of the mem ref.
307 bool HasParenPart = IndexReg.getReg() || HasBaseReg;
308
309 if (DispSpec.isImm()) {
310 int DispVal = DispSpec.getImm();
311 if (DispVal || !HasParenPart)
312 O << DispVal;
313 } else {
314 assert(DispSpec.isGlobal() || DispSpec.isCPI() ||
315 DispSpec.isJTI() || DispSpec.isSymbol());
316 printSymbolOperand(MI->getOperand(Op+3));
317 }
318
319 if (HasParenPart) {
320 assert(IndexReg.getReg() != X86::ESP &&
321 "X86 doesn't allow scaling by ESP");
322
323 O << '(';
324 if (HasBaseReg)
325 printOperand(MI, Op, Modifier);
326
327 if (IndexReg.getReg()) {
328 O << ',';
329 printOperand(MI, Op+2, Modifier);
330 unsigned ScaleVal = MI->getOperand(Op+1).getImm();
331 if (ScaleVal != 1)
332 O << ',' << ScaleVal;
333 }
334 O << ')';
335 }
336}
337
338void X86AsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op,
339 const char *Modifier) {
340 assert(isMem(MI, Op) && "Invalid memory reference!");
341 const MachineOperand &Segment = MI->getOperand(Op+4);
342 if (Segment.getReg()) {
343 printOperand(MI, Op+4, Modifier);
344 O << ':';
345 }
346 printLeaMemReference(MI, Op, Modifier);
347}
348
Chris Lattnerbf4b6a82009-09-20 07:41:30 +0000349void X86AsmPrinter::printPICLabel(const MachineInstr *MI, unsigned Op) {
350 PrintPICBaseSymbol();
351 O << '\n';
352 PrintPICBaseSymbol();
353 O << ':';
354}
355
Chris Lattnerbf4b6a82009-09-20 07:41:30 +0000356bool X86AsmPrinter::printAsmMRegister(const MachineOperand &MO, char Mode) {
357 unsigned Reg = MO.getReg();
358 switch (Mode) {
359 default: return true; // Unknown mode.
360 case 'b': // Print QImode register
361 Reg = getX86SubSuperRegister(Reg, MVT::i8);
362 break;
363 case 'h': // Print QImode high register
364 Reg = getX86SubSuperRegister(Reg, MVT::i8, true);
365 break;
366 case 'w': // Print HImode register
367 Reg = getX86SubSuperRegister(Reg, MVT::i16);
368 break;
369 case 'k': // Print SImode register
370 Reg = getX86SubSuperRegister(Reg, MVT::i32);
371 break;
372 case 'q': // Print DImode register
373 Reg = getX86SubSuperRegister(Reg, MVT::i64);
374 break;
375 }
376
377 O << '%' << X86ATTInstPrinter::getRegisterName(Reg);
378 return false;
379}
380
381/// PrintAsmOperand - Print out an operand for an inline asm expression.
382///
383bool X86AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
384 unsigned AsmVariant,
385 const char *ExtraCode) {
386 // Does this asm operand have a single letter operand modifier?
387 if (ExtraCode && ExtraCode[0]) {
388 if (ExtraCode[1] != 0) return true; // Unknown modifier.
389
390 const MachineOperand &MO = MI->getOperand(OpNo);
391
392 switch (ExtraCode[0]) {
393 default: return true; // Unknown modifier.
394 case 'a': // This is an address. Currently only 'i' and 'r' are expected.
395 if (MO.isImm()) {
396 O << MO.getImm();
397 return false;
398 }
399 if (MO.isGlobal() || MO.isCPI() || MO.isJTI() || MO.isSymbol()) {
400 printSymbolOperand(MO);
401 return false;
402 }
403 if (MO.isReg()) {
404 O << '(';
405 printOperand(MI, OpNo);
406 O << ')';
407 return false;
408 }
409 return true;
410
411 case 'c': // Don't print "$" before a global var name or constant.
412 if (MO.isImm())
413 O << MO.getImm();
414 else if (MO.isGlobal() || MO.isCPI() || MO.isJTI() || MO.isSymbol())
415 printSymbolOperand(MO);
416 else
417 printOperand(MI, OpNo);
418 return false;
419
420 case 'A': // Print '*' before a register (it must be a register)
421 if (MO.isReg()) {
422 O << '*';
423 printOperand(MI, OpNo);
424 return false;
425 }
426 return true;
427
428 case 'b': // Print QImode register
429 case 'h': // Print QImode high register
430 case 'w': // Print HImode register
431 case 'k': // Print SImode register
432 case 'q': // Print DImode register
433 if (MO.isReg())
434 return printAsmMRegister(MO, ExtraCode[0]);
435 printOperand(MI, OpNo);
436 return false;
437
438 case 'P': // This is the operand of a call, treat specially.
439 print_pcrel_imm(MI, OpNo);
440 return false;
441
442 case 'n': // Negate the immediate or print a '-' before the operand.
443 // Note: this is a temporary solution. It should be handled target
444 // independently as part of the 'MC' work.
445 if (MO.isImm()) {
446 O << -MO.getImm();
447 return false;
448 }
449 O << '-';
450 }
451 }
452
453 printOperand(MI, OpNo);
454 return false;
455}
456
457bool X86AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
458 unsigned OpNo, unsigned AsmVariant,
459 const char *ExtraCode) {
460 if (ExtraCode && ExtraCode[0]) {
461 if (ExtraCode[1] != 0) return true; // Unknown modifier.
462
463 switch (ExtraCode[0]) {
464 default: return true; // Unknown modifier.
465 case 'b': // Print QImode register
466 case 'h': // Print QImode high register
467 case 'w': // Print HImode register
468 case 'k': // Print SImode register
469 case 'q': // Print SImode register
470 // These only apply to registers, ignore on mem.
471 break;
472 case 'P': // Don't print @PLT, but do print as memory.
473 printMemReference(MI, OpNo, "no-rip");
474 return false;
475 }
476 }
477 printMemReference(MI, OpNo);
478 return false;
479}
480
481
Chris Lattnerbf4b6a82009-09-20 07:41:30 +0000482void X86AsmPrinter::EmitEndOfAsmFile(Module &M) {
483 if (Subtarget->isTargetDarwin()) {
484 // All darwin targets use mach-o.
485 TargetLoweringObjectFileMachO &TLOFMacho =
486 static_cast<TargetLoweringObjectFileMachO &>(getObjFileLowering());
487
488 MachineModuleInfoMachO &MMIMacho =
489 MMI->getObjFileInfo<MachineModuleInfoMachO>();
490
491 // Output stubs for dynamically-linked functions.
492 MachineModuleInfoMachO::SymbolListTy Stubs;
493
494 Stubs = MMIMacho.GetFnStubList();
495 if (!Stubs.empty()) {
496 const MCSection *TheSection =
497 TLOFMacho.getMachOSection("__IMPORT", "__jump_table",
498 MCSectionMachO::S_SYMBOL_STUBS |
499 MCSectionMachO::S_ATTR_SELF_MODIFYING_CODE |
500 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
501 5, SectionKind::getMetadata());
502 OutStreamer.SwitchSection(TheSection);
503
504 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
Chris Lattnerce409842010-01-17 21:43:43 +0000505 O << *Stubs[i].first << ":\n";
Chris Lattnerbf4b6a82009-09-20 07:41:30 +0000506 // Get the MCSymbol without the $stub suffix.
Chris Lattnerce409842010-01-17 21:43:43 +0000507 O << "\t.indirect_symbol " << *Stubs[i].second;
Chris Lattnerbf4b6a82009-09-20 07:41:30 +0000508 O << "\n\thlt ; hlt ; hlt ; hlt ; hlt\n";
509 }
510 O << '\n';
511
512 Stubs.clear();
513 }
514
515 // Output stubs for external and common global variables.
516 Stubs = MMIMacho.GetGVStubList();
517 if (!Stubs.empty()) {
518 const MCSection *TheSection =
519 TLOFMacho.getMachOSection("__IMPORT", "__pointers",
520 MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS,
521 SectionKind::getMetadata());
522 OutStreamer.SwitchSection(TheSection);
523
524 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
Chris Lattnerce409842010-01-17 21:43:43 +0000525 O << *Stubs[i].first << ":\n\t.indirect_symbol " << *Stubs[i].second;
Chris Lattnerbf4b6a82009-09-20 07:41:30 +0000526 O << "\n\t.long\t0\n";
527 }
528 Stubs.clear();
529 }
530
531 Stubs = MMIMacho.GetHiddenGVStubList();
532 if (!Stubs.empty()) {
533 OutStreamer.SwitchSection(getObjFileLowering().getDataSection());
534 EmitAlignment(2);
535
536 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
Chris Lattnerce409842010-01-17 21:43:43 +0000537 O << *Stubs[i].first << ":\n" << MAI->getData32bitsDirective();
538 O << *Stubs[i].second << '\n';
Chris Lattnerbf4b6a82009-09-20 07:41:30 +0000539 }
540 Stubs.clear();
541 }
542
543 // Funny Darwin hack: This flag tells the linker that no global symbols
544 // contain code that falls through to other global symbols (e.g. the obvious
545 // implementation of multiple entry points). If this doesn't occur, the
546 // linker can safely perform dead code stripping. Since LLVM never
547 // generates code that does this, it is always safe to set.
Chris Lattner2d7c8142010-01-23 06:39:22 +0000548 OutStreamer.EmitAssemblerFlag(MCAF_SubsectionsViaSymbols);
Anton Korobeynikovd5fcb1d2009-10-15 22:36:18 +0000549 }
550
Chris Lattnerbf4b6a82009-09-20 07:41:30 +0000551 if (Subtarget->isTargetCOFF()) {
Anton Korobeynikovd5fcb1d2009-10-15 22:36:18 +0000552 X86COFFMachineModuleInfo &COFFMMI =
Chris Lattnerbf4b6a82009-09-20 07:41:30 +0000553 MMI->getObjFileInfo<X86COFFMachineModuleInfo>();
Chris Lattnerbf4b6a82009-09-20 07:41:30 +0000554
Anton Korobeynikovd5fcb1d2009-10-15 22:36:18 +0000555 // Emit type information for external functions
556 for (X86COFFMachineModuleInfo::stub_iterator I = COFFMMI.stub_begin(),
Chris Lattnerbf4b6a82009-09-20 07:41:30 +0000557 E = COFFMMI.stub_end(); I != E; ++I) {
Anton Korobeynikovd5fcb1d2009-10-15 22:36:18 +0000558 O << "\t.def\t " << I->getKeyData()
Chris Lattnerbf4b6a82009-09-20 07:41:30 +0000559 << ";\t.scl\t" << COFF::C_EXT
560 << ";\t.type\t" << (COFF::DT_FCN << COFF::N_BTSHFT)
561 << ";\t.endef\n";
Chris Lattnerbf4b6a82009-09-20 07:41:30 +0000562 }
Anton Korobeynikovd5fcb1d2009-10-15 22:36:18 +0000563
564 if (Subtarget->isTargetCygMing()) {
565 // Necessary for dllexport support
Chris Lattnerc53c24c2010-01-16 00:51:39 +0000566 std::vector<const MCSymbol*> DLLExportedFns, DLLExportedGlobals;
Anton Korobeynikovd5fcb1d2009-10-15 22:36:18 +0000567
568 TargetLoweringObjectFileCOFF &TLOFCOFF =
569 static_cast<TargetLoweringObjectFileCOFF&>(getObjFileLowering());
570
571 for (Module::const_iterator I = M.begin(), E = M.end(); I != E; ++I)
572 if (I->hasDLLExportLinkage()) {
Chris Lattnerb6ed5862010-01-18 00:59:24 +0000573 MCSymbol *Sym = GetGlobalValueSymbol(I);
Chris Lattnerc53c24c2010-01-16 00:51:39 +0000574 COFFMMI.DecorateCygMingName(Sym, OutContext, I, *TM.getTargetData());
575 DLLExportedFns.push_back(Sym);
Anton Korobeynikovd5fcb1d2009-10-15 22:36:18 +0000576 }
577
578 for (Module::const_global_iterator I = M.global_begin(),
579 E = M.global_end(); I != E; ++I)
Chris Lattnerc53c24c2010-01-16 00:51:39 +0000580 if (I->hasDLLExportLinkage())
581 DLLExportedGlobals.push_back(GetGlobalValueSymbol(I));
Anton Korobeynikovd5fcb1d2009-10-15 22:36:18 +0000582
583 // Output linker support code for dllexported globals on windows.
584 if (!DLLExportedGlobals.empty() || !DLLExportedFns.empty()) {
585 OutStreamer.SwitchSection(TLOFCOFF.getCOFFSection(".section .drectve",
586 true,
Chris Lattnerbf4b6a82009-09-20 07:41:30 +0000587 SectionKind::getMetadata()));
Chris Lattnerce409842010-01-17 21:43:43 +0000588 for (unsigned i = 0, e = DLLExportedGlobals.size(); i != e; ++i)
589 O << "\t.ascii \" -export:" << *DLLExportedGlobals[i] << ",data\"\n";
Anton Korobeynikovd5fcb1d2009-10-15 22:36:18 +0000590
Chris Lattnerce409842010-01-17 21:43:43 +0000591 for (unsigned i = 0, e = DLLExportedFns.size(); i != e; ++i)
592 O << "\t.ascii \" -export:" << *DLLExportedFns[i] << "\"\n";
Anton Korobeynikovd5fcb1d2009-10-15 22:36:18 +0000593 }
Chris Lattnerbf4b6a82009-09-20 07:41:30 +0000594 }
595 }
596}
597
598
599//===----------------------------------------------------------------------===//
600// Target Registry Stuff
601//===----------------------------------------------------------------------===//
602
603static MCInstPrinter *createX86MCInstPrinter(const Target &T,
604 unsigned SyntaxVariant,
605 const MCAsmInfo &MAI,
606 raw_ostream &O) {
607 if (SyntaxVariant == 0)
608 return new X86ATTInstPrinter(O, MAI);
609 if (SyntaxVariant == 1)
610 return new X86IntelInstPrinter(O, MAI);
611 return 0;
612}
613
614// Force static initialization.
615extern "C" void LLVMInitializeX86AsmPrinter() {
616 RegisterAsmPrinter<X86AsmPrinter> X(TheX86_32Target);
617 RegisterAsmPrinter<X86AsmPrinter> Y(TheX86_64Target);
618
619 TargetRegistry::RegisterMCInstPrinter(TheX86_32Target,createX86MCInstPrinter);
620 TargetRegistry::RegisterMCInstPrinter(TheX86_64Target,createX86MCInstPrinter);
621}