Add a forELF flag, allowing the removal of forCygwin and simplification of
[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 "X86ATTAsmPrinter.h"
18 #include "X86IntelAsmPrinter.h"
19 #include "X86.h"
20 #include "llvm/Module.h"
21 #include "llvm/Type.h"
22 #include "llvm/Assembly/Writer.h"
23 #include "llvm/Support/Mangler.h"
24 #include "llvm/Support/CommandLine.h"
25 using namespace llvm;
26 using namespace x86;
27
28 Statistic<> llvm::x86::EmittedInsts("asm-printer",
29                                     "Number of machine instrs printed");
30
31 enum AsmWriterFlavorTy { att, intel };
32 cl::opt<AsmWriterFlavorTy>
33 AsmWriterFlavor("x86-asm-syntax",
34                 cl::desc("Choose style of code to emit from X86 backend:"),
35                 cl::values(
36                            clEnumVal(att,   "  Emit AT&T-style assembly"),
37                            clEnumVal(intel, "  Emit Intel-style assembly"),
38                            clEnumValEnd),
39                 cl::init(att));
40
41 /// doInitialization
42 bool X86SharedAsmPrinter::doInitialization(Module &M) {
43   bool forCygwin = false;
44   forDarwin = false;
45   forELF    = false;
46   bool forWin32 = false;
47   const std::string& TT = M.getTargetTriple();
48   if (TT.length() > 5) {
49     forCygwin = TT.find("cygwin") != std::string::npos ||
50                 TT.find("mingw")  != std::string::npos;
51     forDarwin = TT.find("darwin") != std::string::npos;
52     if (!forDarwin && !forCygwin)
53       forELF = true;
54   } else if (TT.empty()) {
55 #if defined(__CYGWIN__) || defined(__MINGW32__)
56     forCygwin = true;
57 #elif defined(__APPLE__)
58     forDarwin = true;
59 #elif defined(_WIN32)
60     forWin32 = true;
61 #else
62     forELF = true;
63 #endif
64   }
65
66   if (forDarwin) {
67     AlignmentIsInBytes = false;
68     GlobalPrefix = "_";
69     Data64bitsDirective = 0;       // we can't emit a 64-bit unit
70     ZeroDirective = "\t.space\t";  // ".space N" emits N zeros.
71     PrivateGlobalPrefix = "L";     // Marker for constant pool idxs
72     ConstantPoolSection = "\t.const\n";
73     LCOMMDirective = "\t.lcomm\t";
74     COMMDirectiveTakesAlignment = false;
75   } else if (forCygwin) {
76     GlobalPrefix = "_";
77     COMMDirectiveTakesAlignment = false;
78   } else if (forWin32) {
79     GlobalPrefix = "_";
80   }  
81   
82   return AsmPrinter::doInitialization(M);
83 }
84
85 bool X86SharedAsmPrinter::doFinalization(Module &M) {
86   const TargetData &TD = TM.getTargetData();
87
88   // Print out module-level global variables here.
89   for (Module::const_global_iterator I = M.global_begin(),
90          E = M.global_end(); I != E; ++I)
91     if (I->hasInitializer()) {   // External global require no code
92       O << "\n\n";
93       std::string name = Mang->getValueName(I);
94       Constant *C = I->getInitializer();
95       unsigned Size = TD.getTypeSize(C->getType());
96       unsigned Align = TD.getTypeAlignmentShift(C->getType());
97
98       switch (I->getLinkage()) {
99       default: assert(0 && "Unknown linkage type!");
100       case GlobalValue::LinkOnceLinkage:
101       case GlobalValue::WeakLinkage:   // FIXME: Verify correct for weak.
102         if (C->isNullValue()) {
103           O << COMMDirective << name << "," << Size;
104           if (COMMDirectiveTakesAlignment)
105             O << "," << (1 << Align);
106           O << "\t\t# ";
107           WriteAsOperand(O, I, true, true, &M);
108           O << "\n";
109           continue;
110         }
111         
112         // Nonnull linkonce -> weak
113         O << "\t.weak " << name << "\n";
114         O << "\t.section\t.llvm.linkonce.d." << name << ",\"aw\",@progbits\n";
115         SwitchSection("", I);
116         break;
117       case GlobalValue::InternalLinkage:
118         if (C->isNullValue()) {
119           if (LCOMMDirective) {
120             O << LCOMMDirective << name << "," << Size << "," << Align;
121             continue;
122           } else {
123             SwitchSection(".bss", I);
124             O << "\t.local " << name << "\n";
125             O << COMMDirective << name << "," << Size;
126             if (COMMDirectiveTakesAlignment)
127               O << "," << (1 << Align);
128             O << "\t\t# ";
129             WriteAsOperand(O, I, true, true, &M);
130             O << "\n";
131             continue;
132           }
133         }
134         SwitchSection(C->isNullValue() ? ".bss" : ".data", I);
135         break;
136       case GlobalValue::AppendingLinkage:
137         // FIXME: appending linkage variables should go into a section of
138         // their name or something.  For now, just emit them as external.
139       case GlobalValue::ExternalLinkage:
140         SwitchSection(C->isNullValue() ? ".bss" : ".data", I);
141         // If external or appending, declare as a global symbol
142         O << "\t.globl " << name << "\n";
143         break;
144       }
145
146       EmitAlignment(Align);
147       if (forELF) {
148         O << "\t.type " << name << ",@object\n";
149         O << "\t.size " << name << "," << Size << "\n";
150       }
151       O << name << ":\t\t\t\t# ";
152       WriteAsOperand(O, I, true, true, &M);
153       O << " = ";
154       WriteAsOperand(O, C, false, false, &M);
155       O << "\n";
156       EmitGlobalConstant(C);
157     }
158
159   if (forDarwin) {
160     SwitchSection("", 0);
161     // Output stubs for external global variables
162     if (GVStubs.begin() != GVStubs.end())
163       O << "\t.non_lazy_symbol_pointer\n";
164     for (std::set<std::string>::iterator i = GVStubs.begin(), e = GVStubs.end();
165          i != e; ++i) {
166       O << "L" << *i << "$non_lazy_ptr:\n";
167       O << "\t.indirect_symbol " << *i << "\n";
168       O << "\t.long\t0\n";
169     }
170
171     // Output stubs for dynamically-linked functions
172     unsigned j = 1;
173     for (std::set<std::string>::iterator i = FnStubs.begin(), e = FnStubs.end();
174          i != e; ++i, ++j) {
175       O << "\t.symbol_stub\n";
176       O << "L" << *i << "$stub:\n";
177       O << "\t.indirect_symbol " << *i << "\n";
178       O << "\tjmp\t*L" << j << "$lz\n";
179       O << "L" << *i << "$stub_binder:\n";
180       O << "\tpushl\t$L" << j << "$lz\n";
181       O << "\tjmp\tdyld_stub_binding_helper\n";
182       O << "\t.section __DATA, __la_sym_ptr3,lazy_symbol_pointers\n";
183       O << "L" << j << "$lz:\n";
184       O << "\t.indirect_symbol " << *i << "\n";
185       O << "\t.long\tL" << *i << "$stub_binder\n";
186     }
187
188     O << "\n";
189
190     // Output stubs for link-once variables
191     if (LinkOnceStubs.begin() != LinkOnceStubs.end())
192       O << ".data\n.align 2\n";
193     for (std::set<std::string>::iterator i = LinkOnceStubs.begin(),
194          e = LinkOnceStubs.end(); i != e; ++i) {
195       O << "L" << *i << "$non_lazy_ptr:\n"
196         << "\t.long\t" << *i << '\n';
197     }
198   }
199
200   AsmPrinter::doFinalization(M);
201   return false; // success
202 }
203
204 /// createX86CodePrinterPass - Returns a pass that prints the X86 assembly code
205 /// for a MachineFunction to the given output stream, using the given target
206 /// machine description.
207 ///
208 FunctionPass *llvm::createX86CodePrinterPass(std::ostream &o,TargetMachine &tm){
209   switch (AsmWriterFlavor) {
210   default:
211     assert(0 && "Unknown asm flavor!");
212   case intel:
213     return new X86IntelAsmPrinter(o, tm);
214   case att:
215     return new X86ATTAsmPrinter(o, tm);
216   }
217 }