1 //===-- X86AsmPrinter.cpp - Convert X86 LLVM IR to X86 assembly -----------===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file the shared super class printer that converts from our internal
11 // representation of machine-dependent LLVM code to Intel and AT&T format
13 // This printer is the output mechanism used by `llc'.
15 //===----------------------------------------------------------------------===//
17 #include "X86AsmPrinter.h"
18 #include "X86ATTAsmPrinter.h"
19 #include "X86IntelAsmPrinter.h"
20 #include "X86Subtarget.h"
21 #include "llvm/Constants.h"
22 #include "llvm/Module.h"
23 #include "llvm/Type.h"
24 #include "llvm/Assembly/Writer.h"
25 #include "llvm/Support/Mangler.h"
26 #include "llvm/Target/TargetAsmInfo.h"
29 Statistic<> llvm::EmittedInsts("asm-printer",
30 "Number of machine instrs printed");
33 bool X86SharedAsmPrinter::doInitialization(Module &M) {
34 if (Subtarget->isTargetDarwin()) {
35 const X86Subtarget *Subtarget = &TM.getSubtarget<X86Subtarget>();
36 if (!Subtarget->is64Bit())
37 X86PICStyle = PICStyle::Stub;
39 // Emit initial debug information.
43 return AsmPrinter::doInitialization(M);
46 bool X86SharedAsmPrinter::doFinalization(Module &M) {
47 // Note: this code is not shared by the Intel printer as it is too different
48 // from how MASM does things. When making changes here don't forget to look
49 // at X86IntelAsmPrinter::doFinalization().
50 const TargetData *TD = TM.getTargetData();
52 // Print out module-level global variables here.
53 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
55 if (!I->hasInitializer()) continue; // External global require no code
57 // Check to see if this is a special global used by LLVM, if so, emit it.
58 if (EmitSpecialLLVMGlobal(I))
61 std::string name = Mang->getValueName(I);
62 Constant *C = I->getInitializer();
63 unsigned Size = TD->getTypeSize(C->getType());
64 unsigned Align = getPreferredAlignmentLog(I);
66 if (C->isNullValue() && /* FIXME: Verify correct */
67 (I->hasInternalLinkage() || I->hasWeakLinkage() ||
68 I->hasLinkOnceLinkage() ||
69 (Subtarget->isTargetDarwin() &&
70 I->hasExternalLinkage() && !I->hasSection()))) {
71 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
72 if (I->hasExternalLinkage()) {
73 O << "\t.globl\t" << name << "\n";
74 O << "\t.zerofill __DATA__, __common, " << name << ", "
75 << Size << ", " << Align;
77 SwitchToDataSection(TAI->getDataSection(), I);
78 if (TAI->getLCOMMDirective() != NULL) {
79 if (I->hasInternalLinkage()) {
80 O << TAI->getLCOMMDirective() << name << "," << Size;
81 if (Subtarget->isTargetDarwin())
82 O << "," << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
84 O << TAI->getCOMMDirective() << name << "," << Size;
86 if (Subtarget->TargetType != X86Subtarget::isCygwin) {
87 if (I->hasInternalLinkage())
88 O << "\t.local\t" << name << "\n";
90 O << TAI->getCOMMDirective() << name << "," << Size;
91 if (TAI->getCOMMDirectiveTakesAlignment())
92 O << "," << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
95 O << "\t\t" << TAI->getCommentString() << " " << I->getName() << "\n";
97 switch (I->getLinkage()) {
98 case GlobalValue::LinkOnceLinkage:
99 case GlobalValue::WeakLinkage:
100 if (Subtarget->isTargetDarwin()) {
101 O << "\t.globl " << name << "\n"
102 << "\t.weak_definition " << name << "\n";
103 SwitchToDataSection(".section __DATA,__const_coal,coalesced", I);
104 } else if (Subtarget->TargetType == X86Subtarget::isCygwin) {
105 O << "\t.section\t.llvm.linkonce.d." << name << ",\"aw\"\n"
106 << "\t.weak " << name << "\n";
108 O << "\t.section\t.llvm.linkonce.d." << name << ",\"aw\",@progbits\n"
109 << "\t.weak " << name << "\n";
112 case GlobalValue::AppendingLinkage:
113 // FIXME: appending linkage variables should go into a section of
114 // their name or something. For now, just emit them as external.
115 case GlobalValue::ExternalLinkage:
116 // If external or appending, declare as a global symbol
117 O << "\t.globl " << name << "\n";
119 case GlobalValue::InternalLinkage:
120 SwitchToDataSection(TAI->getDataSection(), I);
123 assert(0 && "Unknown linkage type!");
126 EmitAlignment(Align, I);
127 O << name << ":\t\t\t\t" << TAI->getCommentString() << " " << I->getName()
129 if (TAI->hasDotTypeDotSizeDirective())
130 O << "\t.size " << name << ", " << Size << "\n";
132 EmitGlobalConstant(C);
137 if (Subtarget->isTargetDarwin()) {
138 SwitchToDataSection("", 0);
140 // Output stubs for dynamically-linked functions
142 for (std::set<std::string>::iterator i = FnStubs.begin(), e = FnStubs.end();
144 SwitchToDataSection(".section __IMPORT,__jump_table,symbol_stubs,"
145 "self_modifying_code+pure_instructions,5", 0);
146 O << "L" << *i << "$stub:\n";
147 O << "\t.indirect_symbol " << *i << "\n";
148 O << "\thlt ; hlt ; hlt ; hlt ; hlt\n";
153 // Output stubs for external and common global variables.
154 if (GVStubs.begin() != GVStubs.end())
156 ".section __IMPORT,__pointers,non_lazy_symbol_pointers", 0);
157 for (std::set<std::string>::iterator i = GVStubs.begin(), e = GVStubs.end();
159 O << "L" << *i << "$non_lazy_ptr:\n";
160 O << "\t.indirect_symbol " << *i << "\n";
164 // Emit initial debug information.
167 // Funny Darwin hack: This flag tells the linker that no global symbols
168 // contain code that falls through to other global symbols (e.g. the obvious
169 // implementation of multiple entry points). If this doesn't occur, the
170 // linker can safely perform dead code stripping. Since LLVM never
171 // generates code that does this, it is always safe to set.
172 O << "\t.subsections_via_symbols\n";
175 AsmPrinter::doFinalization(M);
176 return false; // success
179 /// createX86CodePrinterPass - Returns a pass that prints the X86 assembly code
180 /// for a MachineFunction to the given output stream, using the given target
181 /// machine description.
183 FunctionPass *llvm::createX86CodePrinterPass(std::ostream &o,
184 X86TargetMachine &tm) {
185 const X86Subtarget *Subtarget = &tm.getSubtarget<X86Subtarget>();
187 if (Subtarget->isFlavorIntel()) {
188 return new X86IntelAsmPrinter(o, tm, tm.getTargetAsmInfo());
190 return new X86ATTAsmPrinter(o, tm, tm.getTargetAsmInfo());