Adding dllimport, dllexport and external weak linkage types.
[oota-llvm.git] / lib / Target / X86 / X86AsmPrinter.cpp
1 //===-- X86AsmPrinter.cpp - Convert X86 LLVM IR to X86 assembly -----------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
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.
7 //
8 //===----------------------------------------------------------------------===//
9 //
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
12 // assembly language.
13 // This printer is the output mechanism used by `llc'.
14 //
15 //===----------------------------------------------------------------------===//
16
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"
27 using namespace llvm;
28
29 Statistic<> llvm::EmittedInsts("asm-printer",
30                                "Number of machine instrs printed");
31
32 /// doInitialization
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;
38
39     // Emit initial debug information.
40     DW.BeginModule(&M);
41   }
42
43   return AsmPrinter::doInitialization(M);
44 }
45
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();
51
52   // Print out module-level global variables here.
53   for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
54        I != E; ++I) {
55     if (!I->hasInitializer()) continue;   // External global require no code
56     
57     // Check to see if this is a special global used by LLVM, if so, emit it.
58     if (EmitSpecialLLVMGlobal(I))
59       continue;
60     
61     std::string name = Mang->getValueName(I);
62     Constant *C = I->getInitializer();
63     unsigned Size = TD->getTypeSize(C->getType());
64     unsigned Align = getPreferredAlignmentLog(I);
65
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;
76       } else {
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);
83           } else
84             O << TAI->getCOMMDirective()  << name << "," << Size;
85         } else {
86           if (Subtarget->TargetType != X86Subtarget::isCygwin) {
87             if (I->hasInternalLinkage())
88               O << "\t.local\t" << name << "\n";
89           }
90           O << TAI->getCOMMDirective()  << name << "," << Size;
91           if (TAI->getCOMMDirectiveTakesAlignment())
92             O << "," << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
93         }
94       }
95       O << "\t\t" << TAI->getCommentString() << " " << I->getName() << "\n";
96     } else {
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";
107         } else {
108           O << "\t.section\t.llvm.linkonce.d." << name << ",\"aw\",@progbits\n"
109             << "\t.weak " << name << "\n";
110         }
111         break;
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::DLLExportLinkage:
116         DLLExportedGVs.insert(Mang->makeNameProper(I->getName(),""));
117         // FALL THROUGH
118       case GlobalValue::ExternalLinkage:
119         // If external or appending, declare as a global symbol
120         O << "\t.globl " << name << "\n";
121         // FALL THROUGH
122       case GlobalValue::InternalLinkage:
123         SwitchToDataSection(TAI->getDataSection(), I);
124         break;
125       default:
126         assert(0 && "Unknown linkage type!");
127       }
128
129       EmitAlignment(Align, I);
130       O << name << ":\t\t\t\t" << TAI->getCommentString() << " " << I->getName()
131         << "\n";
132       if (TAI->hasDotTypeDotSizeDirective())
133         O << "\t.size " << name << ", " << Size << "\n";
134
135       EmitGlobalConstant(C);
136       O << '\n';
137     }
138   }
139   
140   // Output linker support code for dllexported globals
141   if (DLLExportedGVs.begin() != DLLExportedGVs.end()) {
142     SwitchToDataSection(".section .drectve", 0);    
143   }
144
145   for (std::set<std::string>::iterator i = DLLExportedGVs.begin(),
146          e = DLLExportedGVs.end();
147          i != e; ++i) {
148     O << "\t.ascii \" -export:" << *i << ",data\"\n";
149   }    
150
151   if (DLLExportedFns.begin() != DLLExportedFns.end()) {
152     SwitchToDataSection(".section .drectve", 0);    
153   }
154
155   for (std::set<std::string>::iterator i = DLLExportedFns.begin(),
156          e = DLLExportedFns.end();
157          i != e; ++i) {
158     O << "\t.ascii \" -export:" << *i << "\"\n";
159   }    
160  
161   if (Subtarget->isTargetDarwin()) {
162     SwitchToDataSection("", 0);
163
164     // Output stubs for dynamically-linked functions
165     unsigned j = 1;
166     for (std::set<std::string>::iterator i = FnStubs.begin(), e = FnStubs.end();
167          i != e; ++i, ++j) {
168       SwitchToDataSection(".section __IMPORT,__jump_table,symbol_stubs,"
169                           "self_modifying_code+pure_instructions,5", 0);
170       O << "L" << *i << "$stub:\n";
171       O << "\t.indirect_symbol " << *i << "\n";
172       O << "\thlt ; hlt ; hlt ; hlt ; hlt\n";
173     }
174
175     O << "\n";
176
177     // Output stubs for external and common global variables.
178     if (GVStubs.begin() != GVStubs.end())
179       SwitchToDataSection(
180                     ".section __IMPORT,__pointers,non_lazy_symbol_pointers", 0);
181     for (std::set<std::string>::iterator i = GVStubs.begin(), e = GVStubs.end();
182          i != e; ++i) {
183       O << "L" << *i << "$non_lazy_ptr:\n";
184       O << "\t.indirect_symbol " << *i << "\n";
185       O << "\t.long\t0\n";
186     }
187
188     // Emit initial debug information.
189     DW.EndModule();
190
191     // Funny Darwin hack: This flag tells the linker that no global symbols
192     // contain code that falls through to other global symbols (e.g. the obvious
193     // implementation of multiple entry points).  If this doesn't occur, the
194     // linker can safely perform dead code stripping.  Since LLVM never
195     // generates code that does this, it is always safe to set.
196     O << "\t.subsections_via_symbols\n";
197   }
198
199   AsmPrinter::doFinalization(M);
200   return false; // success
201 }
202
203 /// createX86CodePrinterPass - Returns a pass that prints the X86 assembly code
204 /// for a MachineFunction to the given output stream, using the given target
205 /// machine description.
206 ///
207 FunctionPass *llvm::createX86CodePrinterPass(std::ostream &o,
208                                              X86TargetMachine &tm) {
209   const X86Subtarget *Subtarget = &tm.getSubtarget<X86Subtarget>();
210
211   if (Subtarget->isFlavorIntel()) {
212     return new X86IntelAsmPrinter(o, tm, tm.getTargetAsmInfo());
213   } else {
214     return new X86ATTAsmPrinter(o, tm, tm.getTargetAsmInfo());
215   }
216 }