blob: 8cab24ca63a146fce8e528660768ea5abbda31ea [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"
Chris Lattner4a7d8332010-02-03 06:42:38 +000030#include "llvm/MC/MCExpr.h"
Chris Lattnerbf4b6a82009-09-20 07:41:30 +000031#include "llvm/MC/MCSectionMachO.h"
32#include "llvm/MC/MCStreamer.h"
33#include "llvm/MC/MCSymbol.h"
34#include "llvm/CodeGen/MachineJumpTableInfo.h"
35#include "llvm/CodeGen/MachineModuleInfoImpls.h"
Anton Korobeynikov84d365c2010-02-15 22:37:53 +000036#include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
Chris Lattnerbf4b6a82009-09-20 07:41:30 +000037#include "llvm/Support/ErrorHandling.h"
38#include "llvm/Support/FormattedStream.h"
Chris Lattnerbf4b6a82009-09-20 07:41:30 +000039#include "llvm/MC/MCAsmInfo.h"
Anton Korobeynikove55d1a22010-02-12 15:28:40 +000040#include "llvm/Target/Mangler.h"
Chris Lattnerbf4b6a82009-09-20 07:41:30 +000041#include "llvm/Target/TargetOptions.h"
42#include "llvm/Target/TargetRegistry.h"
43#include "llvm/ADT/SmallString.h"
Chris Lattnerbf4b6a82009-09-20 07:41:30 +000044using namespace llvm;
45
Chris Lattnerbf4b6a82009-09-20 07:41:30 +000046//===----------------------------------------------------------------------===//
47// Primitive Helper Functions.
48//===----------------------------------------------------------------------===//
49
Chris Lattnerbf4b6a82009-09-20 07:41:30 +000050void X86AsmPrinter::PrintPICBaseSymbol() const {
Chris Lattner541d8902010-01-26 06:28:43 +000051 const TargetLowering *TLI = TM.getTargetLowering();
52 O << *static_cast<const X86TargetLowering*>(TLI)->getPICBaseSymbol(MF,
53 OutContext);
Chris Lattnerbf4b6a82009-09-20 07:41:30 +000054}
55
Anton Korobeynikove55d1a22010-02-12 15:28:40 +000056MCSymbol *X86AsmPrinter::GetGlobalValueSymbol(const GlobalValue *GV) const {
57 SmallString<60> NameStr;
58 Mang->getNameWithPrefix(NameStr, GV, false);
59 MCSymbol *Symb = OutContext.GetOrCreateSymbol(NameStr.str());
60
61 if (Subtarget->isTargetCygMing()) {
62 X86COFFMachineModuleInfo &COFFMMI =
63 MMI->getObjFileInfo<X86COFFMachineModuleInfo>();
64 COFFMMI.DecorateCygMingName(Symb, OutContext, GV, *TM.getTargetData());
65
66 // Save function name for later type emission.
67 if (const Function *F = dyn_cast<Function>(GV))
68 if (F->isDeclaration())
69 COFFMMI.addExternalFunction(Symb->getName());
70
71 }
72
73 return Symb;
74}
75
Chris Lattnerd7dede22010-01-28 01:02:27 +000076/// runOnMachineFunction - Emit the function body.
Chris Lattnerbf4b6a82009-09-20 07:41:30 +000077///
78bool X86AsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Chris Lattnerbf4b6a82009-09-20 07:41:30 +000079 SetupMachineFunction(MF);
Chris Lattnerbf4b6a82009-09-20 07:41:30 +000080
Anton Korobeynikove55d1a22010-02-12 15:28:40 +000081 if (Subtarget->isTargetCOFF()) {
Chris Lattnere5980672010-01-26 23:18:44 +000082 const Function *F = MF.getFunction();
Anton Korobeynikove55d1a22010-02-12 15:28:40 +000083 O << "\t.def\t " << *CurrentFnSym << ";\t.scl\t" <<
Chris Lattnere5980672010-01-26 23:18:44 +000084 (F->hasInternalLinkage() ? COFF::C_STAT : COFF::C_EXT)
85 << ";\t.type\t" << (COFF::DT_FCN << COFF::N_BTSHFT)
86 << ";\t.endef\n";
87 }
Anton Korobeynikove55d1a22010-02-12 15:28:40 +000088
Chris Lattnere5980672010-01-26 23:18:44 +000089 // Have common code print out the function header with linkage info etc.
90 EmitFunctionHeader();
Anton Korobeynikove55d1a22010-02-12 15:28:40 +000091
Chris Lattnerd7dede22010-01-28 01:02:27 +000092 // Emit the rest of the function body.
93 EmitFunctionBody();
Chris Lattnerbf4b6a82009-09-20 07:41:30 +000094
95 // We didn't modify anything.
96 return false;
97}
98
99/// printSymbolOperand - Print a raw symbol reference operand. This handles
100/// jump tables, constant pools, global address and external symbols, all of
101/// which print to a label with various suffixes for relocation types etc.
102void X86AsmPrinter::printSymbolOperand(const MachineOperand &MO) {
103 switch (MO.getType()) {
104 default: llvm_unreachable("unknown symbol type!");
105 case MachineOperand::MO_JumpTableIndex:
Chris Lattner242edfc2010-01-23 05:26:25 +0000106 O << *GetJTISymbol(MO.getIndex());
Chris Lattnerbf4b6a82009-09-20 07:41:30 +0000107 break;
108 case MachineOperand::MO_ConstantPoolIndex:
Chris Lattner242edfc2010-01-23 05:26:25 +0000109 O << *GetCPISymbol(MO.getIndex());
Chris Lattnerbf4b6a82009-09-20 07:41:30 +0000110 printOffset(MO.getOffset());
111 break;
112 case MachineOperand::MO_GlobalAddress: {
113 const GlobalValue *GV = MO.getGlobal();
114
Chris Lattnerb6ed5862010-01-18 00:59:24 +0000115 MCSymbol *GVSym;
Chris Lattnerbf4b6a82009-09-20 07:41:30 +0000116 if (MO.getTargetFlags() == X86II::MO_DARWIN_STUB)
Chris Lattnera6f2a102010-01-16 18:37:32 +0000117 GVSym = GetSymbolWithGlobalValueBase(GV, "$stub");
Chris Lattnerbf4b6a82009-09-20 07:41:30 +0000118 else if (MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY ||
119 MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY_PIC_BASE ||
120 MO.getTargetFlags() == X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE)
Chris Lattnera6f2a102010-01-16 18:37:32 +0000121 GVSym = GetSymbolWithGlobalValueBase(GV, "$non_lazy_ptr");
Chris Lattnerc53c24c2010-01-16 00:51:39 +0000122 else
123 GVSym = GetGlobalValueSymbol(GV);
124
Chris Lattnerbf4b6a82009-09-20 07:41:30 +0000125 // Handle dllimport linkage.
126 if (MO.getTargetFlags() == X86II::MO_DLLIMPORT)
Chris Lattnerc53c24c2010-01-16 00:51:39 +0000127 GVSym = OutContext.GetOrCreateSymbol(Twine("__imp_") + GVSym->getName());
Chris Lattnerbf4b6a82009-09-20 07:41:30 +0000128
129 if (MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY ||
130 MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY_PIC_BASE) {
Chris Lattnera6f2a102010-01-16 18:37:32 +0000131 MCSymbol *Sym = GetSymbolWithGlobalValueBase(GV, "$non_lazy_ptr");
Chris Lattnerbf4b6a82009-09-20 07:41:30 +0000132
Chris Lattner77653062010-02-03 06:18:30 +0000133 MCSymbol *&StubSym =
Chris Lattnerbf4b6a82009-09-20 07:41:30 +0000134 MMI->getObjFileInfo<MachineModuleInfoMachO>().getGVStubEntry(Sym);
Chris Lattner178a9322010-01-16 01:00:27 +0000135 if (StubSym == 0)
136 StubSym = GetGlobalValueSymbol(GV);
137
Chris Lattnerbf4b6a82009-09-20 07:41:30 +0000138 } else if (MO.getTargetFlags() == X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE){
Chris Lattnera6f2a102010-01-16 18:37:32 +0000139 MCSymbol *Sym = GetSymbolWithGlobalValueBase(GV, "$non_lazy_ptr");
Chris Lattner77653062010-02-03 06:18:30 +0000140 MCSymbol *&StubSym =
Chris Lattnerbf4b6a82009-09-20 07:41:30 +0000141 MMI->getObjFileInfo<MachineModuleInfoMachO>().getHiddenGVStubEntry(Sym);
Chris Lattner178a9322010-01-16 01:00:27 +0000142 if (StubSym == 0)
143 StubSym = GetGlobalValueSymbol(GV);
Chris Lattnerbf4b6a82009-09-20 07:41:30 +0000144 } else if (MO.getTargetFlags() == X86II::MO_DARWIN_STUB) {
Chris Lattnera6f2a102010-01-16 18:37:32 +0000145 MCSymbol *Sym = GetSymbolWithGlobalValueBase(GV, "$stub");
Chris Lattner77653062010-02-03 06:18:30 +0000146 MCSymbol *&StubSym =
Chris Lattnerbf4b6a82009-09-20 07:41:30 +0000147 MMI->getObjFileInfo<MachineModuleInfoMachO>().getFnStubEntry(Sym);
Chris Lattner178a9322010-01-16 01:00:27 +0000148 if (StubSym == 0)
149 StubSym = GetGlobalValueSymbol(GV);
Chris Lattnerbf4b6a82009-09-20 07:41:30 +0000150 }
151
152 // If the name begins with a dollar-sign, enclose it in parens. We do this
153 // to avoid having it look like an integer immediate to the assembler.
Chris Lattnerc53c24c2010-01-16 00:51:39 +0000154 if (GVSym->getName()[0] != '$')
Chris Lattnerce409842010-01-17 21:43:43 +0000155 O << *GVSym;
156 else
157 O << '(' << *GVSym << ')';
Chris Lattnerbf4b6a82009-09-20 07:41:30 +0000158 printOffset(MO.getOffset());
159 break;
160 }
161 case MachineOperand::MO_ExternalSymbol: {
Chris Lattnerf80743e2010-01-13 07:56:59 +0000162 const MCSymbol *SymToPrint;
Chris Lattnerbf4b6a82009-09-20 07:41:30 +0000163 if (MO.getTargetFlags() == X86II::MO_DARWIN_STUB) {
Chris Lattner178a9322010-01-16 01:00:27 +0000164 SmallString<128> TempNameStr;
165 TempNameStr += StringRef(MO.getSymbolName());
166 TempNameStr += StringRef("$stub");
167
Chris Lattner77653062010-02-03 06:18:30 +0000168 MCSymbol *Sym = GetExternalSymbolSymbol(TempNameStr.str());
169 MCSymbol *&StubSym =
Chris Lattnerbf4b6a82009-09-20 07:41:30 +0000170 MMI->getObjFileInfo<MachineModuleInfoMachO>().getFnStubEntry(Sym);
171 if (StubSym == 0) {
Chris Lattnera38b2872010-01-13 06:38:18 +0000172 TempNameStr.erase(TempNameStr.end()-5, TempNameStr.end());
173 StubSym = OutContext.GetOrCreateSymbol(TempNameStr.str());
Chris Lattnerbf4b6a82009-09-20 07:41:30 +0000174 }
Chris Lattnerf80743e2010-01-13 07:56:59 +0000175 SymToPrint = StubSym;
176 } else {
Chris Lattner178a9322010-01-16 01:00:27 +0000177 SymToPrint = GetExternalSymbolSymbol(MO.getSymbolName());
Chris Lattnerbf4b6a82009-09-20 07:41:30 +0000178 }
179
180 // If the name begins with a dollar-sign, enclose it in parens. We do this
181 // to avoid having it look like an integer immediate to the assembler.
Chris Lattnerf80743e2010-01-13 07:56:59 +0000182 if (SymToPrint->getName()[0] != '$')
Chris Lattnerce409842010-01-17 21:43:43 +0000183 O << *SymToPrint;
184 else
185 O << '(' << *SymToPrint << '(';
Chris Lattnerbf4b6a82009-09-20 07:41:30 +0000186 break;
187 }
188 }
189
190 switch (MO.getTargetFlags()) {
191 default:
192 llvm_unreachable("Unknown target flag on GV operand");
193 case X86II::MO_NO_FLAG: // No flag.
194 break;
195 case X86II::MO_DARWIN_NONLAZY:
196 case X86II::MO_DLLIMPORT:
197 case X86II::MO_DARWIN_STUB:
198 // These affect the name of the symbol, not any suffix.
199 break;
200 case X86II::MO_GOT_ABSOLUTE_ADDRESS:
201 O << " + [.-";
202 PrintPICBaseSymbol();
203 O << ']';
204 break;
205 case X86II::MO_PIC_BASE_OFFSET:
206 case X86II::MO_DARWIN_NONLAZY_PIC_BASE:
207 case X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE:
208 O << '-';
209 PrintPICBaseSymbol();
210 break;
211 case X86II::MO_TLSGD: O << "@TLSGD"; break;
212 case X86II::MO_GOTTPOFF: O << "@GOTTPOFF"; break;
213 case X86II::MO_INDNTPOFF: O << "@INDNTPOFF"; break;
214 case X86II::MO_TPOFF: O << "@TPOFF"; break;
215 case X86II::MO_NTPOFF: O << "@NTPOFF"; break;
216 case X86II::MO_GOTPCREL: O << "@GOTPCREL"; break;
217 case X86II::MO_GOT: O << "@GOT"; break;
218 case X86II::MO_GOTOFF: O << "@GOTOFF"; break;
219 case X86II::MO_PLT: O << "@PLT"; break;
220 }
221}
222
223/// print_pcrel_imm - This is used to print an immediate value that ends up
224/// being encoded as a pc-relative value. These print slightly differently, for
225/// example, a $ is not emitted.
226void X86AsmPrinter::print_pcrel_imm(const MachineInstr *MI, unsigned OpNo) {
227 const MachineOperand &MO = MI->getOperand(OpNo);
228 switch (MO.getType()) {
229 default: llvm_unreachable("Unknown pcrel immediate operand");
230 case MachineOperand::MO_Immediate:
231 O << MO.getImm();
232 return;
233 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner84d5ca92010-01-26 04:55:51 +0000234 O << *MO.getMBB()->getSymbol(OutContext);
Chris Lattnerbf4b6a82009-09-20 07:41:30 +0000235 return;
236 case MachineOperand::MO_GlobalAddress:
237 case MachineOperand::MO_ExternalSymbol:
238 printSymbolOperand(MO);
239 return;
240 }
241}
242
243
244void X86AsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo,
Chris Lattnerb6ed5862010-01-18 00:59:24 +0000245 const char *Modifier) {
Chris Lattnerbf4b6a82009-09-20 07:41:30 +0000246 const MachineOperand &MO = MI->getOperand(OpNo);
247 switch (MO.getType()) {
248 default: llvm_unreachable("unknown operand type!");
249 case MachineOperand::MO_Register: {
250 O << '%';
251 unsigned Reg = MO.getReg();
252 if (Modifier && strncmp(Modifier, "subreg", strlen("subreg")) == 0) {
253 EVT VT = (strcmp(Modifier+6,"64") == 0) ?
254 MVT::i64 : ((strcmp(Modifier+6, "32") == 0) ? MVT::i32 :
255 ((strcmp(Modifier+6,"16") == 0) ? MVT::i16 : MVT::i8));
256 Reg = getX86SubSuperRegister(Reg, VT);
257 }
258 O << X86ATTInstPrinter::getRegisterName(Reg);
259 return;
260 }
261
262 case MachineOperand::MO_Immediate:
263 O << '$' << MO.getImm();
264 return;
265
266 case MachineOperand::MO_JumpTableIndex:
267 case MachineOperand::MO_ConstantPoolIndex:
268 case MachineOperand::MO_GlobalAddress:
269 case MachineOperand::MO_ExternalSymbol: {
270 O << '$';
271 printSymbolOperand(MO);
272 break;
273 }
274 }
275}
276
277void X86AsmPrinter::printSSECC(const MachineInstr *MI, unsigned Op) {
278 unsigned char value = MI->getOperand(Op).getImm();
279 assert(value <= 7 && "Invalid ssecc argument!");
280 switch (value) {
281 case 0: O << "eq"; break;
282 case 1: O << "lt"; break;
283 case 2: O << "le"; break;
284 case 3: O << "unord"; break;
285 case 4: O << "neq"; break;
286 case 5: O << "nlt"; break;
287 case 6: O << "nle"; break;
288 case 7: O << "ord"; break;
289 }
290}
291
292void X86AsmPrinter::printLeaMemReference(const MachineInstr *MI, unsigned Op,
293 const char *Modifier) {
294 const MachineOperand &BaseReg = MI->getOperand(Op);
295 const MachineOperand &IndexReg = MI->getOperand(Op+2);
296 const MachineOperand &DispSpec = MI->getOperand(Op+3);
297
298 // If we really don't want to print out (rip), don't.
299 bool HasBaseReg = BaseReg.getReg() != 0;
300 if (HasBaseReg && Modifier && !strcmp(Modifier, "no-rip") &&
301 BaseReg.getReg() == X86::RIP)
302 HasBaseReg = false;
303
304 // HasParenPart - True if we will print out the () part of the mem ref.
305 bool HasParenPart = IndexReg.getReg() || HasBaseReg;
306
307 if (DispSpec.isImm()) {
308 int DispVal = DispSpec.getImm();
309 if (DispVal || !HasParenPart)
310 O << DispVal;
311 } else {
312 assert(DispSpec.isGlobal() || DispSpec.isCPI() ||
313 DispSpec.isJTI() || DispSpec.isSymbol());
314 printSymbolOperand(MI->getOperand(Op+3));
315 }
316
317 if (HasParenPart) {
318 assert(IndexReg.getReg() != X86::ESP &&
319 "X86 doesn't allow scaling by ESP");
320
321 O << '(';
322 if (HasBaseReg)
323 printOperand(MI, Op, Modifier);
324
325 if (IndexReg.getReg()) {
326 O << ',';
327 printOperand(MI, Op+2, Modifier);
328 unsigned ScaleVal = MI->getOperand(Op+1).getImm();
329 if (ScaleVal != 1)
330 O << ',' << ScaleVal;
331 }
332 O << ')';
333 }
334}
335
336void X86AsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op,
337 const char *Modifier) {
338 assert(isMem(MI, Op) && "Invalid memory reference!");
339 const MachineOperand &Segment = MI->getOperand(Op+4);
340 if (Segment.getReg()) {
341 printOperand(MI, Op+4, Modifier);
342 O << ':';
343 }
344 printLeaMemReference(MI, Op, Modifier);
345}
346
Chris Lattnerbf4b6a82009-09-20 07:41:30 +0000347void X86AsmPrinter::printPICLabel(const MachineInstr *MI, unsigned Op) {
348 PrintPICBaseSymbol();
349 O << '\n';
350 PrintPICBaseSymbol();
351 O << ':';
352}
353
Chris Lattnerbf4b6a82009-09-20 07:41:30 +0000354bool X86AsmPrinter::printAsmMRegister(const MachineOperand &MO, char Mode) {
355 unsigned Reg = MO.getReg();
356 switch (Mode) {
357 default: return true; // Unknown mode.
358 case 'b': // Print QImode register
359 Reg = getX86SubSuperRegister(Reg, MVT::i8);
360 break;
361 case 'h': // Print QImode high register
362 Reg = getX86SubSuperRegister(Reg, MVT::i8, true);
363 break;
364 case 'w': // Print HImode register
365 Reg = getX86SubSuperRegister(Reg, MVT::i16);
366 break;
367 case 'k': // Print SImode register
368 Reg = getX86SubSuperRegister(Reg, MVT::i32);
369 break;
370 case 'q': // Print DImode register
371 Reg = getX86SubSuperRegister(Reg, MVT::i64);
372 break;
373 }
374
375 O << '%' << X86ATTInstPrinter::getRegisterName(Reg);
376 return false;
377}
378
379/// PrintAsmOperand - Print out an operand for an inline asm expression.
380///
381bool X86AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
382 unsigned AsmVariant,
383 const char *ExtraCode) {
384 // Does this asm operand have a single letter operand modifier?
385 if (ExtraCode && ExtraCode[0]) {
386 if (ExtraCode[1] != 0) return true; // Unknown modifier.
387
388 const MachineOperand &MO = MI->getOperand(OpNo);
389
390 switch (ExtraCode[0]) {
391 default: return true; // Unknown modifier.
392 case 'a': // This is an address. Currently only 'i' and 'r' are expected.
393 if (MO.isImm()) {
394 O << MO.getImm();
395 return false;
396 }
397 if (MO.isGlobal() || MO.isCPI() || MO.isJTI() || MO.isSymbol()) {
398 printSymbolOperand(MO);
399 return false;
400 }
401 if (MO.isReg()) {
402 O << '(';
403 printOperand(MI, OpNo);
404 O << ')';
405 return false;
406 }
407 return true;
408
409 case 'c': // Don't print "$" before a global var name or constant.
410 if (MO.isImm())
411 O << MO.getImm();
412 else if (MO.isGlobal() || MO.isCPI() || MO.isJTI() || MO.isSymbol())
413 printSymbolOperand(MO);
414 else
415 printOperand(MI, OpNo);
416 return false;
417
418 case 'A': // Print '*' before a register (it must be a register)
419 if (MO.isReg()) {
420 O << '*';
421 printOperand(MI, OpNo);
422 return false;
423 }
424 return true;
425
426 case 'b': // Print QImode register
427 case 'h': // Print QImode high register
428 case 'w': // Print HImode register
429 case 'k': // Print SImode register
430 case 'q': // Print DImode register
431 if (MO.isReg())
432 return printAsmMRegister(MO, ExtraCode[0]);
433 printOperand(MI, OpNo);
434 return false;
435
436 case 'P': // This is the operand of a call, treat specially.
437 print_pcrel_imm(MI, OpNo);
438 return false;
439
440 case 'n': // Negate the immediate or print a '-' before the operand.
441 // Note: this is a temporary solution. It should be handled target
442 // independently as part of the 'MC' work.
443 if (MO.isImm()) {
444 O << -MO.getImm();
445 return false;
446 }
447 O << '-';
448 }
449 }
450
451 printOperand(MI, OpNo);
452 return false;
453}
454
455bool X86AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
456 unsigned OpNo, unsigned AsmVariant,
457 const char *ExtraCode) {
458 if (ExtraCode && ExtraCode[0]) {
459 if (ExtraCode[1] != 0) return true; // Unknown modifier.
460
461 switch (ExtraCode[0]) {
462 default: return true; // Unknown modifier.
463 case 'b': // Print QImode register
464 case 'h': // Print QImode high register
465 case 'w': // Print HImode register
466 case 'k': // Print SImode register
467 case 'q': // Print SImode register
468 // These only apply to registers, ignore on mem.
469 break;
470 case 'P': // Don't print @PLT, but do print as memory.
471 printMemReference(MI, OpNo, "no-rip");
472 return false;
473 }
474 }
475 printMemReference(MI, OpNo);
476 return false;
477}
478
479
Chris Lattnerbf4b6a82009-09-20 07:41:30 +0000480void X86AsmPrinter::EmitEndOfAsmFile(Module &M) {
481 if (Subtarget->isTargetDarwin()) {
482 // All darwin targets use mach-o.
483 TargetLoweringObjectFileMachO &TLOFMacho =
484 static_cast<TargetLoweringObjectFileMachO &>(getObjFileLowering());
485
486 MachineModuleInfoMachO &MMIMacho =
487 MMI->getObjFileInfo<MachineModuleInfoMachO>();
488
489 // Output stubs for dynamically-linked functions.
490 MachineModuleInfoMachO::SymbolListTy Stubs;
491
492 Stubs = MMIMacho.GetFnStubList();
493 if (!Stubs.empty()) {
494 const MCSection *TheSection =
495 TLOFMacho.getMachOSection("__IMPORT", "__jump_table",
496 MCSectionMachO::S_SYMBOL_STUBS |
497 MCSectionMachO::S_ATTR_SELF_MODIFYING_CODE |
498 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
499 5, SectionKind::getMetadata());
500 OutStreamer.SwitchSection(TheSection);
501
502 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
Chris Lattner4a7d8332010-02-03 06:42:38 +0000503 // L_foo$stub:
504 OutStreamer.EmitLabel(Stubs[i].first);
505 // .indirect_symbol _foo
506 OutStreamer.EmitSymbolAttribute(Stubs[i].second, MCSA_IndirectSymbol);
507 // hlt; hlt; hlt; hlt; hlt hlt = 0xf4 = -12.
508 const char HltInsts[] = { -12, -12, -12, -12, -12 };
509 OutStreamer.EmitBytes(StringRef(HltInsts, 5), 0/*addrspace*/);
Chris Lattnerbf4b6a82009-09-20 07:41:30 +0000510 }
Chris Lattnerbf4b6a82009-09-20 07:41:30 +0000511
512 Stubs.clear();
Chris Lattner4a7d8332010-02-03 06:42:38 +0000513 OutStreamer.AddBlankLine();
Chris Lattnerbf4b6a82009-09-20 07:41:30 +0000514 }
515
516 // Output stubs for external and common global variables.
517 Stubs = MMIMacho.GetGVStubList();
518 if (!Stubs.empty()) {
519 const MCSection *TheSection =
520 TLOFMacho.getMachOSection("__IMPORT", "__pointers",
521 MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS,
522 SectionKind::getMetadata());
523 OutStreamer.SwitchSection(TheSection);
524
525 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
Chris Lattner4a7d8332010-02-03 06:42:38 +0000526 // L_foo$non_lazy_ptr:
527 OutStreamer.EmitLabel(Stubs[i].first);
528 // .indirect_symbol _foo
529 OutStreamer.EmitSymbolAttribute(Stubs[i].second, MCSA_IndirectSymbol);
530 // .long 0
531 OutStreamer.EmitIntValue(0, 4/*size*/, 0/*addrspace*/);
Chris Lattnerbf4b6a82009-09-20 07:41:30 +0000532 }
533 Stubs.clear();
Chris Lattner4a7d8332010-02-03 06:42:38 +0000534 OutStreamer.AddBlankLine();
Chris Lattnerbf4b6a82009-09-20 07:41:30 +0000535 }
536
537 Stubs = MMIMacho.GetHiddenGVStubList();
538 if (!Stubs.empty()) {
539 OutStreamer.SwitchSection(getObjFileLowering().getDataSection());
540 EmitAlignment(2);
541
542 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
Chris Lattner4a7d8332010-02-03 06:42:38 +0000543 // L_foo$non_lazy_ptr:
544 OutStreamer.EmitLabel(Stubs[i].first);
545 // .long _foo
546 OutStreamer.EmitValue(MCSymbolRefExpr::Create(Stubs[i].second,
547 OutContext),
548 4/*size*/, 0/*addrspace*/);
Chris Lattnerbf4b6a82009-09-20 07:41:30 +0000549 }
550 Stubs.clear();
Chris Lattner4a7d8332010-02-03 06:42:38 +0000551 OutStreamer.AddBlankLine();
Chris Lattnerbf4b6a82009-09-20 07:41:30 +0000552 }
553
554 // Funny Darwin hack: This flag tells the linker that no global symbols
555 // contain code that falls through to other global symbols (e.g. the obvious
556 // implementation of multiple entry points). If this doesn't occur, the
557 // linker can safely perform dead code stripping. Since LLVM never
558 // generates code that does this, it is always safe to set.
Chris Lattner2d7c8142010-01-23 06:39:22 +0000559 OutStreamer.EmitAssemblerFlag(MCAF_SubsectionsViaSymbols);
Anton Korobeynikovd5fcb1d2009-10-15 22:36:18 +0000560 }
561
Chris Lattnerbf4b6a82009-09-20 07:41:30 +0000562 if (Subtarget->isTargetCOFF()) {
Anton Korobeynikovd5fcb1d2009-10-15 22:36:18 +0000563 X86COFFMachineModuleInfo &COFFMMI =
Chris Lattnerbf4b6a82009-09-20 07:41:30 +0000564 MMI->getObjFileInfo<X86COFFMachineModuleInfo>();
Chris Lattnerbf4b6a82009-09-20 07:41:30 +0000565
Anton Korobeynikovd5fcb1d2009-10-15 22:36:18 +0000566 // Emit type information for external functions
567 for (X86COFFMachineModuleInfo::stub_iterator I = COFFMMI.stub_begin(),
Chris Lattnerbf4b6a82009-09-20 07:41:30 +0000568 E = COFFMMI.stub_end(); I != E; ++I) {
Anton Korobeynikovd5fcb1d2009-10-15 22:36:18 +0000569 O << "\t.def\t " << I->getKeyData()
Chris Lattnerbf4b6a82009-09-20 07:41:30 +0000570 << ";\t.scl\t" << COFF::C_EXT
571 << ";\t.type\t" << (COFF::DT_FCN << COFF::N_BTSHFT)
572 << ";\t.endef\n";
Chris Lattnerbf4b6a82009-09-20 07:41:30 +0000573 }
Anton Korobeynikovd5fcb1d2009-10-15 22:36:18 +0000574
575 if (Subtarget->isTargetCygMing()) {
576 // Necessary for dllexport support
Chris Lattnerc53c24c2010-01-16 00:51:39 +0000577 std::vector<const MCSymbol*> DLLExportedFns, DLLExportedGlobals;
Anton Korobeynikovd5fcb1d2009-10-15 22:36:18 +0000578
579 TargetLoweringObjectFileCOFF &TLOFCOFF =
580 static_cast<TargetLoweringObjectFileCOFF&>(getObjFileLowering());
581
582 for (Module::const_iterator I = M.begin(), E = M.end(); I != E; ++I)
583 if (I->hasDLLExportLinkage()) {
Chris Lattnerb6ed5862010-01-18 00:59:24 +0000584 MCSymbol *Sym = GetGlobalValueSymbol(I);
Chris Lattnerc53c24c2010-01-16 00:51:39 +0000585 DLLExportedFns.push_back(Sym);
Anton Korobeynikovd5fcb1d2009-10-15 22:36:18 +0000586 }
587
588 for (Module::const_global_iterator I = M.global_begin(),
589 E = M.global_end(); I != E; ++I)
Chris Lattnerc53c24c2010-01-16 00:51:39 +0000590 if (I->hasDLLExportLinkage())
591 DLLExportedGlobals.push_back(GetGlobalValueSymbol(I));
Anton Korobeynikovd5fcb1d2009-10-15 22:36:18 +0000592
593 // Output linker support code for dllexported globals on windows.
594 if (!DLLExportedGlobals.empty() || !DLLExportedFns.empty()) {
595 OutStreamer.SwitchSection(TLOFCOFF.getCOFFSection(".section .drectve",
596 true,
Chris Lattnerbf4b6a82009-09-20 07:41:30 +0000597 SectionKind::getMetadata()));
Chris Lattnerce409842010-01-17 21:43:43 +0000598 for (unsigned i = 0, e = DLLExportedGlobals.size(); i != e; ++i)
599 O << "\t.ascii \" -export:" << *DLLExportedGlobals[i] << ",data\"\n";
Anton Korobeynikovd5fcb1d2009-10-15 22:36:18 +0000600
Chris Lattnerce409842010-01-17 21:43:43 +0000601 for (unsigned i = 0, e = DLLExportedFns.size(); i != e; ++i)
602 O << "\t.ascii \" -export:" << *DLLExportedFns[i] << "\"\n";
Anton Korobeynikovd5fcb1d2009-10-15 22:36:18 +0000603 }
Chris Lattnerbf4b6a82009-09-20 07:41:30 +0000604 }
605 }
Anton Korobeynikovd779bcb2010-02-15 22:35:59 +0000606
607 if (Subtarget->isTargetELF()) {
608 TargetLoweringObjectFileELF &TLOFELF =
609 static_cast<TargetLoweringObjectFileELF &>(getObjFileLowering());
610
611 MachineModuleInfoELF &MMIELF = MMI->getObjFileInfo<MachineModuleInfoELF>();
612
613 // Output stubs for external and common global variables.
614 MachineModuleInfoELF::SymbolListTy Stubs = MMIELF.GetGVStubList();
615 if (!Stubs.empty()) {
616 OutStreamer.SwitchSection(TLOFELF.getDataRelSection());
617 const TargetData *TD = TM.getTargetData();
618
619 for (unsigned i = 0, e = Stubs.size(); i != e; ++i)
620 O << *Stubs[i].first << ":\n"
621 << (TD->getPointerSize() == 8 ?
622 MAI->getData64bitsDirective() : MAI->getData32bitsDirective())
623 << *Stubs[i].second << '\n';
624
625 Stubs.clear();
626 }
627 }
Chris Lattnerbf4b6a82009-09-20 07:41:30 +0000628}
629
630
631//===----------------------------------------------------------------------===//
632// Target Registry Stuff
633//===----------------------------------------------------------------------===//
634
635static MCInstPrinter *createX86MCInstPrinter(const Target &T,
636 unsigned SyntaxVariant,
637 const MCAsmInfo &MAI,
638 raw_ostream &O) {
639 if (SyntaxVariant == 0)
640 return new X86ATTInstPrinter(O, MAI);
641 if (SyntaxVariant == 1)
642 return new X86IntelInstPrinter(O, MAI);
643 return 0;
644}
645
646// Force static initialization.
647extern "C" void LLVMInitializeX86AsmPrinter() {
648 RegisterAsmPrinter<X86AsmPrinter> X(TheX86_32Target);
649 RegisterAsmPrinter<X86AsmPrinter> Y(TheX86_64Target);
650
651 TargetRegistry::RegisterMCInstPrinter(TheX86_32Target,createX86MCInstPrinter);
652 TargetRegistry::RegisterMCInstPrinter(TheX86_64Target,createX86MCInstPrinter);
653}