1 //===-- X86AsmPrinter.cpp - Convert X86 LLVM code to AT&T assembly --------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains a printer that converts from our internal representation
11 // of machine-dependent LLVM code to AT&T format assembly
12 // language. This printer is the output mechanism used by `llc'.
14 //===----------------------------------------------------------------------===//
16 #define DEBUG_TYPE "asm-printer"
17 #include "X86AsmPrinter.h"
18 #include "X86ATTInstPrinter.h"
19 #include "X86IntelInstPrinter.h"
20 #include "X86MCInstLower.h"
23 #include "X86COFFMachineModuleInfo.h"
24 #include "X86MachineFunctionInfo.h"
25 #include "X86TargetMachine.h"
26 #include "llvm/CallingConv.h"
27 #include "llvm/DerivedTypes.h"
28 #include "llvm/Module.h"
29 #include "llvm/Type.h"
30 #include "llvm/Assembly/Writer.h"
31 #include "llvm/MC/MCContext.h"
32 #include "llvm/MC/MCSectionMachO.h"
33 #include "llvm/MC/MCStreamer.h"
34 #include "llvm/MC/MCSymbol.h"
35 #include "llvm/CodeGen/MachineJumpTableInfo.h"
36 #include "llvm/CodeGen/MachineModuleInfoImpls.h"
37 #include "llvm/Support/ErrorHandling.h"
38 #include "llvm/Support/FormattedStream.h"
39 #include "llvm/MC/MCAsmInfo.h"
40 #include "llvm/Target/TargetLoweringObjectFile.h"
41 #include "llvm/Target/TargetOptions.h"
42 #include "llvm/Target/TargetRegistry.h"
43 #include "llvm/ADT/SmallString.h"
44 #include "llvm/ADT/Statistic.h"
47 STATISTIC(EmittedInsts, "Number of machine instrs printed");
49 //===----------------------------------------------------------------------===//
50 // Primitive Helper Functions.
51 //===----------------------------------------------------------------------===//
53 void X86AsmPrinter::printMCInst(const MCInst *MI) {
54 if (MAI->getAssemblerDialect() == 0)
55 X86ATTInstPrinter(O, *MAI).printInstruction(MI);
57 X86IntelInstPrinter(O, *MAI).printInstruction(MI);
60 void X86AsmPrinter::PrintPICBaseSymbol() const {
61 // FIXME: Gross const cast hack.
62 X86AsmPrinter *AP = const_cast<X86AsmPrinter*>(this);
63 O << *X86MCInstLower(OutContext, 0, *AP).GetPICBaseSymbol();
66 void X86AsmPrinter::emitFunctionHeader(const MachineFunction &MF) {
67 unsigned FnAlign = MF.getAlignment();
68 const Function *F = MF.getFunction();
70 if (Subtarget->isTargetCygMing()) {
71 X86COFFMachineModuleInfo &COFFMMI =
72 MMI->getObjFileInfo<X86COFFMachineModuleInfo>();
73 COFFMMI.DecorateCygMingName(CurrentFnSym, OutContext, F,
77 OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(F, Mang, TM));
78 EmitAlignment(FnAlign, F);
80 switch (F->getLinkage()) {
81 default: llvm_unreachable("Unknown linkage type!");
82 case Function::InternalLinkage: // Symbols default to internal.
83 case Function::PrivateLinkage:
85 case Function::DLLExportLinkage:
86 case Function::ExternalLinkage:
87 OutStreamer.EmitSymbolAttribute(CurrentFnSym, MCStreamer::Global);
89 case Function::LinkerPrivateLinkage:
90 case Function::LinkOnceAnyLinkage:
91 case Function::LinkOnceODRLinkage:
92 case Function::WeakAnyLinkage:
93 case Function::WeakODRLinkage:
94 if (Subtarget->isTargetDarwin()) {
95 OutStreamer.EmitSymbolAttribute(CurrentFnSym, MCStreamer::Global);
96 O << MAI->getWeakDefDirective() << *CurrentFnSym << '\n';
97 } else if (Subtarget->isTargetCygMing()) {
98 OutStreamer.EmitSymbolAttribute(CurrentFnSym, MCStreamer::Global);
99 O << "\t.linkonce discard\n";
101 O << "\t.weak\t" << *CurrentFnSym << '\n';
106 printVisibility(CurrentFnSym, F->getVisibility());
108 if (Subtarget->isTargetELF()) {
109 O << "\t.type\t" << *CurrentFnSym << ",@function\n";
110 } else if (Subtarget->isTargetCygMing()) {
111 O << "\t.def\t " << *CurrentFnSym;
113 (F->hasInternalLinkage() ? COFF::C_STAT : COFF::C_EXT)
114 << ";\t.type\t" << (COFF::DT_FCN << COFF::N_BTSHFT)
118 O << *CurrentFnSym << ':';
120 O.PadToColumn(MAI->getCommentColumn());
121 O << MAI->getCommentString() << ' ';
122 WriteAsOperand(O, F, /*PrintType=*/false, F->getParent());
126 // Add some workaround for linkonce linkage on Cygwin\MinGW
127 if (Subtarget->isTargetCygMing() &&
128 (F->hasLinkOnceLinkage() || F->hasWeakLinkage()))
129 O << "Lllvm$workaround$fake$stub$" << *CurrentFnSym << ":\n";
132 /// runOnMachineFunction - This uses the printMachineInstruction()
133 /// method to print assembly for each instruction.
135 bool X86AsmPrinter::runOnMachineFunction(MachineFunction &MF) {
136 const Function *F = MF.getFunction();
138 CallingConv::ID CC = F->getCallingConv();
140 SetupMachineFunction(MF);
143 if (Subtarget->isTargetCOFF()) {
144 X86COFFMachineModuleInfo &COFFMMI =
145 MMI->getObjFileInfo<X86COFFMachineModuleInfo>();
147 // Populate function information map. Don't want to populate
148 // non-stdcall or non-fastcall functions' information right now.
149 if (CC == CallingConv::X86_StdCall || CC == CallingConv::X86_FastCall)
150 COFFMMI.AddFunctionInfo(F, *MF.getInfo<X86MachineFunctionInfo>());
153 // Print out constants referenced by the function
154 EmitConstantPool(MF.getConstantPool());
156 // Print the 'header' of function
157 emitFunctionHeader(MF);
159 // Emit pre-function debug and/or EH information.
160 if (MAI->doesSupportDebugInformation() || MAI->doesSupportExceptionHandling())
161 DW->BeginFunction(&MF);
163 // Print out code for the function.
164 bool hasAnyRealCode = false;
165 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
167 // Print a label for the basic block.
168 EmitBasicBlockStart(I);
169 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
171 // Print the assembly for the instruction.
173 hasAnyRealCode = true;
174 printMachineInstruction(II);
178 if (Subtarget->isTargetDarwin() && !hasAnyRealCode) {
179 // If the function is empty, then we need to emit *something*. Otherwise,
180 // the function's label might be associated with something that it wasn't
181 // meant to be associated with. We emit a noop in this situation.
182 // We are assuming inline asms are code.
186 if (MAI->hasDotTypeDotSizeDirective())
187 O << "\t.size\t" << *CurrentFnSym << ", .-" << *CurrentFnSym << '\n';
189 // Emit post-function debug information.
190 if (MAI->doesSupportDebugInformation() || MAI->doesSupportExceptionHandling())
191 DW->EndFunction(&MF);
193 // Print out jump tables referenced by the function.
194 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
196 // We didn't modify anything.
200 /// printSymbolOperand - Print a raw symbol reference operand. This handles
201 /// jump tables, constant pools, global address and external symbols, all of
202 /// which print to a label with various suffixes for relocation types etc.
203 void X86AsmPrinter::printSymbolOperand(const MachineOperand &MO) {
204 switch (MO.getType()) {
205 default: llvm_unreachable("unknown symbol type!");
206 case MachineOperand::MO_JumpTableIndex:
207 O << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber() << '_'
210 case MachineOperand::MO_ConstantPoolIndex:
211 O << MAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_'
213 printOffset(MO.getOffset());
215 case MachineOperand::MO_GlobalAddress: {
216 const GlobalValue *GV = MO.getGlobal();
219 if (MO.getTargetFlags() == X86II::MO_DARWIN_STUB)
220 GVSym = GetSymbolWithGlobalValueBase(GV, "$stub");
221 else if (MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY ||
222 MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY_PIC_BASE ||
223 MO.getTargetFlags() == X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE)
224 GVSym = GetSymbolWithGlobalValueBase(GV, "$non_lazy_ptr");
226 GVSym = GetGlobalValueSymbol(GV);
228 if (Subtarget->isTargetCygMing()) {
229 X86COFFMachineModuleInfo &COFFMMI =
230 MMI->getObjFileInfo<X86COFFMachineModuleInfo>();
231 COFFMMI.DecorateCygMingName(GVSym, OutContext, GV, *TM.getTargetData());
234 // Handle dllimport linkage.
235 if (MO.getTargetFlags() == X86II::MO_DLLIMPORT)
236 GVSym = OutContext.GetOrCreateSymbol(Twine("__imp_") + GVSym->getName());
238 if (MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY ||
239 MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY_PIC_BASE) {
240 MCSymbol *Sym = GetSymbolWithGlobalValueBase(GV, "$non_lazy_ptr");
242 const MCSymbol *&StubSym =
243 MMI->getObjFileInfo<MachineModuleInfoMachO>().getGVStubEntry(Sym);
245 StubSym = GetGlobalValueSymbol(GV);
247 } else if (MO.getTargetFlags() == X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE){
248 MCSymbol *Sym = GetSymbolWithGlobalValueBase(GV, "$non_lazy_ptr");
249 const MCSymbol *&StubSym =
250 MMI->getObjFileInfo<MachineModuleInfoMachO>().getHiddenGVStubEntry(Sym);
252 StubSym = GetGlobalValueSymbol(GV);
253 } else if (MO.getTargetFlags() == X86II::MO_DARWIN_STUB) {
254 MCSymbol *Sym = GetSymbolWithGlobalValueBase(GV, "$stub");
255 const MCSymbol *&StubSym =
256 MMI->getObjFileInfo<MachineModuleInfoMachO>().getFnStubEntry(Sym);
258 StubSym = GetGlobalValueSymbol(GV);
261 // If the name begins with a dollar-sign, enclose it in parens. We do this
262 // to avoid having it look like an integer immediate to the assembler.
263 if (GVSym->getName()[0] != '$')
266 O << '(' << *GVSym << ')';
267 printOffset(MO.getOffset());
270 case MachineOperand::MO_ExternalSymbol: {
271 const MCSymbol *SymToPrint;
272 if (MO.getTargetFlags() == X86II::MO_DARWIN_STUB) {
273 SmallString<128> TempNameStr;
274 TempNameStr += StringRef(MO.getSymbolName());
275 TempNameStr += StringRef("$stub");
277 const MCSymbol *Sym = GetExternalSymbolSymbol(TempNameStr.str());
278 const MCSymbol *&StubSym =
279 MMI->getObjFileInfo<MachineModuleInfoMachO>().getFnStubEntry(Sym);
281 TempNameStr.erase(TempNameStr.end()-5, TempNameStr.end());
282 StubSym = OutContext.GetOrCreateSymbol(TempNameStr.str());
284 SymToPrint = StubSym;
286 SymToPrint = GetExternalSymbolSymbol(MO.getSymbolName());
289 // If the name begins with a dollar-sign, enclose it in parens. We do this
290 // to avoid having it look like an integer immediate to the assembler.
291 if (SymToPrint->getName()[0] != '$')
294 O << '(' << *SymToPrint << '(';
299 switch (MO.getTargetFlags()) {
301 llvm_unreachable("Unknown target flag on GV operand");
302 case X86II::MO_NO_FLAG: // No flag.
304 case X86II::MO_DARWIN_NONLAZY:
305 case X86II::MO_DLLIMPORT:
306 case X86II::MO_DARWIN_STUB:
307 // These affect the name of the symbol, not any suffix.
309 case X86II::MO_GOT_ABSOLUTE_ADDRESS:
311 PrintPICBaseSymbol();
314 case X86II::MO_PIC_BASE_OFFSET:
315 case X86II::MO_DARWIN_NONLAZY_PIC_BASE:
316 case X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE:
318 PrintPICBaseSymbol();
320 case X86II::MO_TLSGD: O << "@TLSGD"; break;
321 case X86II::MO_GOTTPOFF: O << "@GOTTPOFF"; break;
322 case X86II::MO_INDNTPOFF: O << "@INDNTPOFF"; break;
323 case X86II::MO_TPOFF: O << "@TPOFF"; break;
324 case X86II::MO_NTPOFF: O << "@NTPOFF"; break;
325 case X86II::MO_GOTPCREL: O << "@GOTPCREL"; break;
326 case X86II::MO_GOT: O << "@GOT"; break;
327 case X86II::MO_GOTOFF: O << "@GOTOFF"; break;
328 case X86II::MO_PLT: O << "@PLT"; break;
332 /// print_pcrel_imm - This is used to print an immediate value that ends up
333 /// being encoded as a pc-relative value. These print slightly differently, for
334 /// example, a $ is not emitted.
335 void X86AsmPrinter::print_pcrel_imm(const MachineInstr *MI, unsigned OpNo) {
336 const MachineOperand &MO = MI->getOperand(OpNo);
337 switch (MO.getType()) {
338 default: llvm_unreachable("Unknown pcrel immediate operand");
339 case MachineOperand::MO_Immediate:
342 case MachineOperand::MO_MachineBasicBlock:
343 O << *GetMBBSymbol(MO.getMBB()->getNumber());
345 case MachineOperand::MO_GlobalAddress:
346 case MachineOperand::MO_ExternalSymbol:
347 printSymbolOperand(MO);
353 void X86AsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo,
354 const char *Modifier) {
355 const MachineOperand &MO = MI->getOperand(OpNo);
356 switch (MO.getType()) {
357 default: llvm_unreachable("unknown operand type!");
358 case MachineOperand::MO_Register: {
360 unsigned Reg = MO.getReg();
361 if (Modifier && strncmp(Modifier, "subreg", strlen("subreg")) == 0) {
362 EVT VT = (strcmp(Modifier+6,"64") == 0) ?
363 MVT::i64 : ((strcmp(Modifier+6, "32") == 0) ? MVT::i32 :
364 ((strcmp(Modifier+6,"16") == 0) ? MVT::i16 : MVT::i8));
365 Reg = getX86SubSuperRegister(Reg, VT);
367 O << X86ATTInstPrinter::getRegisterName(Reg);
371 case MachineOperand::MO_Immediate:
372 O << '$' << MO.getImm();
375 case MachineOperand::MO_JumpTableIndex:
376 case MachineOperand::MO_ConstantPoolIndex:
377 case MachineOperand::MO_GlobalAddress:
378 case MachineOperand::MO_ExternalSymbol: {
380 printSymbolOperand(MO);
386 void X86AsmPrinter::printSSECC(const MachineInstr *MI, unsigned Op) {
387 unsigned char value = MI->getOperand(Op).getImm();
388 assert(value <= 7 && "Invalid ssecc argument!");
390 case 0: O << "eq"; break;
391 case 1: O << "lt"; break;
392 case 2: O << "le"; break;
393 case 3: O << "unord"; break;
394 case 4: O << "neq"; break;
395 case 5: O << "nlt"; break;
396 case 6: O << "nle"; break;
397 case 7: O << "ord"; break;
401 void X86AsmPrinter::printLeaMemReference(const MachineInstr *MI, unsigned Op,
402 const char *Modifier) {
403 const MachineOperand &BaseReg = MI->getOperand(Op);
404 const MachineOperand &IndexReg = MI->getOperand(Op+2);
405 const MachineOperand &DispSpec = MI->getOperand(Op+3);
407 // If we really don't want to print out (rip), don't.
408 bool HasBaseReg = BaseReg.getReg() != 0;
409 if (HasBaseReg && Modifier && !strcmp(Modifier, "no-rip") &&
410 BaseReg.getReg() == X86::RIP)
413 // HasParenPart - True if we will print out the () part of the mem ref.
414 bool HasParenPart = IndexReg.getReg() || HasBaseReg;
416 if (DispSpec.isImm()) {
417 int DispVal = DispSpec.getImm();
418 if (DispVal || !HasParenPart)
421 assert(DispSpec.isGlobal() || DispSpec.isCPI() ||
422 DispSpec.isJTI() || DispSpec.isSymbol());
423 printSymbolOperand(MI->getOperand(Op+3));
427 assert(IndexReg.getReg() != X86::ESP &&
428 "X86 doesn't allow scaling by ESP");
432 printOperand(MI, Op, Modifier);
434 if (IndexReg.getReg()) {
436 printOperand(MI, Op+2, Modifier);
437 unsigned ScaleVal = MI->getOperand(Op+1).getImm();
439 O << ',' << ScaleVal;
445 void X86AsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op,
446 const char *Modifier) {
447 assert(isMem(MI, Op) && "Invalid memory reference!");
448 const MachineOperand &Segment = MI->getOperand(Op+4);
449 if (Segment.getReg()) {
450 printOperand(MI, Op+4, Modifier);
453 printLeaMemReference(MI, Op, Modifier);
456 void X86AsmPrinter::printPICJumpTableSetLabel(unsigned uid,
457 const MachineBasicBlock *MBB) const {
458 if (!MAI->getSetDirective())
461 // We don't need .set machinery if we have GOT-style relocations
462 if (Subtarget->isPICStyleGOT())
465 O << MAI->getSetDirective() << ' ' << MAI->getPrivateGlobalPrefix()
466 << getFunctionNumber() << '_' << uid << "_set_" << MBB->getNumber() << ',';
468 O << *GetMBBSymbol(MBB->getNumber());
470 if (Subtarget->isPICStyleRIPRel())
471 O << '-' << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
472 << '_' << uid << '\n';
475 PrintPICBaseSymbol();
481 void X86AsmPrinter::printPICLabel(const MachineInstr *MI, unsigned Op) {
482 PrintPICBaseSymbol();
484 PrintPICBaseSymbol();
488 void X86AsmPrinter::printPICJumpTableEntry(const MachineJumpTableInfo *MJTI,
489 const MachineBasicBlock *MBB,
490 unsigned uid) const {
491 const char *JTEntryDirective = MJTI->getEntrySize() == 4 ?
492 MAI->getData32bitsDirective() : MAI->getData64bitsDirective();
494 O << JTEntryDirective << ' ';
496 if (Subtarget->isPICStyleRIPRel() || Subtarget->isPICStyleStubPIC()) {
497 O << MAI->getPrivateGlobalPrefix() << getFunctionNumber()
498 << '_' << uid << "_set_" << MBB->getNumber();
499 } else if (Subtarget->isPICStyleGOT())
500 O << *GetMBBSymbol(MBB->getNumber()) << "@GOTOFF";
502 O << *GetMBBSymbol(MBB->getNumber());
505 bool X86AsmPrinter::printAsmMRegister(const MachineOperand &MO, char Mode) {
506 unsigned Reg = MO.getReg();
508 default: return true; // Unknown mode.
509 case 'b': // Print QImode register
510 Reg = getX86SubSuperRegister(Reg, MVT::i8);
512 case 'h': // Print QImode high register
513 Reg = getX86SubSuperRegister(Reg, MVT::i8, true);
515 case 'w': // Print HImode register
516 Reg = getX86SubSuperRegister(Reg, MVT::i16);
518 case 'k': // Print SImode register
519 Reg = getX86SubSuperRegister(Reg, MVT::i32);
521 case 'q': // Print DImode register
522 Reg = getX86SubSuperRegister(Reg, MVT::i64);
526 O << '%' << X86ATTInstPrinter::getRegisterName(Reg);
530 /// PrintAsmOperand - Print out an operand for an inline asm expression.
532 bool X86AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
534 const char *ExtraCode) {
535 // Does this asm operand have a single letter operand modifier?
536 if (ExtraCode && ExtraCode[0]) {
537 if (ExtraCode[1] != 0) return true; // Unknown modifier.
539 const MachineOperand &MO = MI->getOperand(OpNo);
541 switch (ExtraCode[0]) {
542 default: return true; // Unknown modifier.
543 case 'a': // This is an address. Currently only 'i' and 'r' are expected.
548 if (MO.isGlobal() || MO.isCPI() || MO.isJTI() || MO.isSymbol()) {
549 printSymbolOperand(MO);
554 printOperand(MI, OpNo);
560 case 'c': // Don't print "$" before a global var name or constant.
563 else if (MO.isGlobal() || MO.isCPI() || MO.isJTI() || MO.isSymbol())
564 printSymbolOperand(MO);
566 printOperand(MI, OpNo);
569 case 'A': // Print '*' before a register (it must be a register)
572 printOperand(MI, OpNo);
577 case 'b': // Print QImode register
578 case 'h': // Print QImode high register
579 case 'w': // Print HImode register
580 case 'k': // Print SImode register
581 case 'q': // Print DImode register
583 return printAsmMRegister(MO, ExtraCode[0]);
584 printOperand(MI, OpNo);
587 case 'P': // This is the operand of a call, treat specially.
588 print_pcrel_imm(MI, OpNo);
591 case 'n': // Negate the immediate or print a '-' before the operand.
592 // Note: this is a temporary solution. It should be handled target
593 // independently as part of the 'MC' work.
602 printOperand(MI, OpNo);
606 bool X86AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
607 unsigned OpNo, unsigned AsmVariant,
608 const char *ExtraCode) {
609 if (ExtraCode && ExtraCode[0]) {
610 if (ExtraCode[1] != 0) return true; // Unknown modifier.
612 switch (ExtraCode[0]) {
613 default: return true; // Unknown modifier.
614 case 'b': // Print QImode register
615 case 'h': // Print QImode high register
616 case 'w': // Print HImode register
617 case 'k': // Print SImode register
618 case 'q': // Print SImode register
619 // These only apply to registers, ignore on mem.
621 case 'P': // Don't print @PLT, but do print as memory.
622 printMemReference(MI, OpNo, "no-rip");
626 printMemReference(MI, OpNo);
632 /// printMachineInstruction -- Print out a single X86 LLVM instruction MI in
633 /// AT&T syntax to the current output stream.
635 void X86AsmPrinter::printMachineInstruction(const MachineInstr *MI) {
638 processDebugLoc(MI, true);
640 printInstructionThroughMCStreamer(MI);
646 processDebugLoc(MI, false);
649 void X86AsmPrinter::PrintGlobalVariable(const GlobalVariable* GVar) {
650 MCSymbol *GVarSym = GetGlobalValueSymbol(GVar);
651 printVisibility(GVarSym, GVar->getVisibility());
653 if (MAI->hasDotTypeDotSizeDirective()) {
654 O << "\t.type\t" << *GVarSym;
655 if (MAI->getCommentString()[0] != '@')
661 SectionKind GVKind = TargetLoweringObjectFile::getKindForGlobal(GVar, TM);
663 const TargetData *TD = TM.getTargetData();
664 unsigned Size = TD->getTypeAllocSize(GVar->getType()->getElementType());
665 unsigned AlignLog = TD->getPreferredAlignmentLog(GVar);
667 // Handle normal common symbols.
668 if (GVKind.isCommon()) {
669 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
671 O << MAI->getCOMMDirective() << *GVarSym << ',' << Size;
672 if (MAI->getCOMMDirectiveTakesAlignment())
673 O << ',' << (MAI->getAlignmentIsInBytes() ? (1 << AlignLog) : AlignLog);
676 O << "\t\t" << MAI->getCommentString() << " '";
677 WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent());
684 if (GVKind.isBSSLocal()) {
685 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
687 if (const char *LComm = MAI->getLCOMMDirective()) {
688 if (GVar->hasLocalLinkage()) {
689 O << LComm << *GVarSym << ',' << Size;
690 if (MAI->getLCOMMDirectiveTakesAlignment())
691 O << ',' << AlignLog;
694 O << "\t.local\t" << *GVarSym << '\n';
695 O << MAI->getCOMMDirective() << *GVarSym << ',' << Size;
696 if (MAI->getCOMMDirectiveTakesAlignment())
697 O << ',' << (MAI->getAlignmentIsInBytes() ? (1 << AlignLog) : AlignLog);
700 O.PadToColumn(MAI->getCommentColumn());
701 O << MAI->getCommentString() << ' ';
702 WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent());
708 const MCSection *TheSection =
709 getObjFileLowering().SectionForGlobal(GVar, GVKind, Mang, TM);
711 // Handle the zerofill directive on darwin, which is a special form of BSS
713 if (GVKind.isBSSExtern() && MAI->hasMachoZeroFillDirective()) {
715 OutStreamer.EmitSymbolAttribute(GVarSym, MCStreamer::Global);
716 // .zerofill __DATA, __common, _foo, 400, 5
717 OutStreamer.EmitZerofill(TheSection, GVarSym, Size, 1 << AlignLog);
721 OutStreamer.SwitchSection(TheSection);
723 switch (GVar->getLinkage()) {
724 case GlobalValue::CommonLinkage:
725 case GlobalValue::LinkOnceAnyLinkage:
726 case GlobalValue::LinkOnceODRLinkage:
727 case GlobalValue::WeakAnyLinkage:
728 case GlobalValue::WeakODRLinkage:
729 case GlobalValue::LinkerPrivateLinkage:
730 if (Subtarget->isTargetDarwin()) {
731 OutStreamer.EmitSymbolAttribute(GVarSym, MCStreamer::Global);
732 O << MAI->getWeakDefDirective() << *GVarSym << '\n';
733 } else if (Subtarget->isTargetCygMing()) {
734 OutStreamer.EmitSymbolAttribute(GVarSym, MCStreamer::Global);
735 O << "\t.linkonce same_size\n";
737 O << "\t.weak\t" << *GVarSym << '\n';
739 case GlobalValue::DLLExportLinkage:
740 case GlobalValue::AppendingLinkage:
741 // FIXME: appending linkage variables should go into a section of
742 // their name or something. For now, just emit them as external.
743 case GlobalValue::ExternalLinkage:
744 // If external or appending, declare as a global symbol
745 OutStreamer.EmitSymbolAttribute(GVarSym, MCStreamer::Global);
747 case GlobalValue::PrivateLinkage:
748 case GlobalValue::InternalLinkage:
751 llvm_unreachable("Unknown linkage type!");
754 EmitAlignment(AlignLog, GVar);
755 O << *GVarSym << ":";
757 O.PadToColumn(MAI->getCommentColumn());
758 O << MAI->getCommentString() << ' ';
759 WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent());
763 EmitGlobalConstant(GVar->getInitializer());
765 if (MAI->hasDotTypeDotSizeDirective())
766 O << "\t.size\t" << *GVarSym << ", " << Size << '\n';
769 void X86AsmPrinter::EmitEndOfAsmFile(Module &M) {
770 if (Subtarget->isTargetDarwin()) {
771 // All darwin targets use mach-o.
772 TargetLoweringObjectFileMachO &TLOFMacho =
773 static_cast<TargetLoweringObjectFileMachO &>(getObjFileLowering());
775 MachineModuleInfoMachO &MMIMacho =
776 MMI->getObjFileInfo<MachineModuleInfoMachO>();
778 // Output stubs for dynamically-linked functions.
779 MachineModuleInfoMachO::SymbolListTy Stubs;
781 Stubs = MMIMacho.GetFnStubList();
782 if (!Stubs.empty()) {
783 const MCSection *TheSection =
784 TLOFMacho.getMachOSection("__IMPORT", "__jump_table",
785 MCSectionMachO::S_SYMBOL_STUBS |
786 MCSectionMachO::S_ATTR_SELF_MODIFYING_CODE |
787 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
788 5, SectionKind::getMetadata());
789 OutStreamer.SwitchSection(TheSection);
791 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
792 O << *Stubs[i].first << ":\n";
793 // Get the MCSymbol without the $stub suffix.
794 O << "\t.indirect_symbol " << *Stubs[i].second;
795 O << "\n\thlt ; hlt ; hlt ; hlt ; hlt\n";
802 // Output stubs for external and common global variables.
803 Stubs = MMIMacho.GetGVStubList();
804 if (!Stubs.empty()) {
805 const MCSection *TheSection =
806 TLOFMacho.getMachOSection("__IMPORT", "__pointers",
807 MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS,
808 SectionKind::getMetadata());
809 OutStreamer.SwitchSection(TheSection);
811 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
812 O << *Stubs[i].first << ":\n\t.indirect_symbol " << *Stubs[i].second;
813 O << "\n\t.long\t0\n";
818 Stubs = MMIMacho.GetHiddenGVStubList();
819 if (!Stubs.empty()) {
820 OutStreamer.SwitchSection(getObjFileLowering().getDataSection());
823 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
824 O << *Stubs[i].first << ":\n" << MAI->getData32bitsDirective();
825 O << *Stubs[i].second << '\n';
830 // Funny Darwin hack: This flag tells the linker that no global symbols
831 // contain code that falls through to other global symbols (e.g. the obvious
832 // implementation of multiple entry points). If this doesn't occur, the
833 // linker can safely perform dead code stripping. Since LLVM never
834 // generates code that does this, it is always safe to set.
835 OutStreamer.EmitAssemblerFlag(MCStreamer::SubsectionsViaSymbols);
838 if (Subtarget->isTargetCOFF()) {
839 X86COFFMachineModuleInfo &COFFMMI =
840 MMI->getObjFileInfo<X86COFFMachineModuleInfo>();
842 // Emit type information for external functions
843 for (X86COFFMachineModuleInfo::stub_iterator I = COFFMMI.stub_begin(),
844 E = COFFMMI.stub_end(); I != E; ++I) {
845 O << "\t.def\t " << I->getKeyData()
846 << ";\t.scl\t" << COFF::C_EXT
847 << ";\t.type\t" << (COFF::DT_FCN << COFF::N_BTSHFT)
851 if (Subtarget->isTargetCygMing()) {
852 // Necessary for dllexport support
853 std::vector<const MCSymbol*> DLLExportedFns, DLLExportedGlobals;
855 TargetLoweringObjectFileCOFF &TLOFCOFF =
856 static_cast<TargetLoweringObjectFileCOFF&>(getObjFileLowering());
858 for (Module::const_iterator I = M.begin(), E = M.end(); I != E; ++I)
859 if (I->hasDLLExportLinkage()) {
860 MCSymbol *Sym = GetGlobalValueSymbol(I);
861 COFFMMI.DecorateCygMingName(Sym, OutContext, I, *TM.getTargetData());
862 DLLExportedFns.push_back(Sym);
865 for (Module::const_global_iterator I = M.global_begin(),
866 E = M.global_end(); I != E; ++I)
867 if (I->hasDLLExportLinkage())
868 DLLExportedGlobals.push_back(GetGlobalValueSymbol(I));
870 // Output linker support code for dllexported globals on windows.
871 if (!DLLExportedGlobals.empty() || !DLLExportedFns.empty()) {
872 OutStreamer.SwitchSection(TLOFCOFF.getCOFFSection(".section .drectve",
874 SectionKind::getMetadata()));
875 for (unsigned i = 0, e = DLLExportedGlobals.size(); i != e; ++i)
876 O << "\t.ascii \" -export:" << *DLLExportedGlobals[i] << ",data\"\n";
878 for (unsigned i = 0, e = DLLExportedFns.size(); i != e; ++i)
879 O << "\t.ascii \" -export:" << *DLLExportedFns[i] << "\"\n";
886 //===----------------------------------------------------------------------===//
887 // Target Registry Stuff
888 //===----------------------------------------------------------------------===//
890 static MCInstPrinter *createX86MCInstPrinter(const Target &T,
891 unsigned SyntaxVariant,
892 const MCAsmInfo &MAI,
894 if (SyntaxVariant == 0)
895 return new X86ATTInstPrinter(O, MAI);
896 if (SyntaxVariant == 1)
897 return new X86IntelInstPrinter(O, MAI);
901 // Force static initialization.
902 extern "C" void LLVMInitializeX86AsmPrinter() {
903 RegisterAsmPrinter<X86AsmPrinter> X(TheX86_32Target);
904 RegisterAsmPrinter<X86AsmPrinter> Y(TheX86_64Target);
906 TargetRegistry::RegisterMCInstPrinter(TheX86_32Target,createX86MCInstPrinter);
907 TargetRegistry::RegisterMCInstPrinter(TheX86_64Target,createX86MCInstPrinter);