A couple more darwinisms
[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/Assembly/Writer.h"
22 #include "llvm/CodeGen/MachineConstantPool.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 leadingUnderscore = false;
44   forCygwin = false;
45   const std::string& TT = M.getTargetTriple();
46   if (TT.length() > 5) {
47     forCygwin = TT.find("cygwin") != std::string::npos ||
48                 TT.find("mingw")  != std::string::npos;
49     forDarwin = TT.find("darwin") != std::string::npos;
50   } else if (TT.empty()) {
51 #if defined(__CYGWIN__) || defined(__MINGW32__)
52     forCygwin = true;
53 #elif defined(__APPLE__)
54     forDarwin = true;
55 #elif defined(_WIN32)
56     leadingUnderscore = true;
57 #else
58     leadingUnderscore = false;
59 #endif
60   }
61   
62   if (leadingUnderscore || forCygwin || forDarwin)
63     GlobalPrefix = "_";
64
65   if (forDarwin) {
66     AlignmentIsInBytes = false;
67     Data64bitsDirective = 0;       // we can't emit a 64-bit unit
68     ZeroDirective = "\t.space\t";  // ".space N" emits N zeros.
69   }
70   
71   return AsmPrinter::doInitialization(M);
72 }
73
74 /// printConstantPool - Print to the current output stream assembly
75 /// representations of the constants in the constant pool MCP. This is
76 /// used to print out constants which have been "spilled to memory" by
77 /// the code generator.
78 ///
79 void X86SharedAsmPrinter::printConstantPool(MachineConstantPool *MCP) {
80   const std::vector<Constant*> &CP = MCP->getConstants();
81   const TargetData &TD = TM.getTargetData();
82
83   if (CP.empty()) return;
84
85   for (unsigned i = 0, e = CP.size(); i != e; ++i) {
86     if (forDarwin)
87       O << "\t.data\n";
88     else
89       O << "\t.section .rodata\n";
90     emitAlignment(TD.getTypeAlignmentShift(CP[i]->getType()));
91     O << ".CPI" << CurrentFnName << "_" << i << ":\t\t\t\t\t" << CommentString
92       << *CP[i] << "\n";
93     emitGlobalConstant(CP[i]);
94   }
95 }
96
97 bool X86SharedAsmPrinter::doFinalization(Module &M) {
98   const TargetData &TD = TM.getTargetData();
99   std::string CurSection;
100
101   // Print out module-level global variables here.
102   for (Module::const_global_iterator I = M.global_begin(),
103          E = M.global_end(); I != E; ++I)
104     if (I->hasInitializer()) {   // External global require no code
105       O << "\n\n";
106       std::string name = Mang->getValueName(I);
107       Constant *C = I->getInitializer();
108       unsigned Size = TD.getTypeSize(C->getType());
109       unsigned Align = TD.getTypeAlignmentShift(C->getType());
110       
111       if (C->isNullValue() &&
112           (I->hasLinkOnceLinkage() || I->hasInternalLinkage() ||
113            I->hasWeakLinkage() /* FIXME: Verify correct */)) {
114         SwitchSection(O, CurSection, ".data");
115         if (!forCygwin && !forDarwin && I->hasInternalLinkage())
116           O << "\t.local " << name << "\n";
117         if (forDarwin && I->hasInternalLinkage())
118           O << "\t.lcomm " << name << "," << Size << "," << Align;
119         else 
120           O << "\t.comm " << name << "," << Size;
121         if (!forCygwin && !forDarwin)
122           O << "," << (1 << Align);
123         O << "\t\t# ";
124         WriteAsOperand(O, I, true, true, &M);
125         O << "\n";
126       } else {
127         switch (I->getLinkage()) {
128         default: assert(0 && "Unknown linkage type!");
129         case GlobalValue::LinkOnceLinkage:
130         case GlobalValue::WeakLinkage:   // FIXME: Verify correct for weak.
131           // Nonnull linkonce -> weak
132           O << "\t.weak " << name << "\n";
133           SwitchSection(O, CurSection, "");
134           O << "\t.section\t.llvm.linkonce.d." << name << ",\"aw\",@progbits\n";
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           // If external or appending, declare as a global symbol
141           O << "\t.globl " << name << "\n";
142           // FALL THROUGH
143         case GlobalValue::InternalLinkage:
144           if (C->isNullValue())
145             SwitchSection(O, CurSection, ".bss");
146           else
147             SwitchSection(O, CurSection, ".data");
148           break;
149         }
150         
151         emitAlignment(Align);
152         if (!forCygwin && !forDarwin) {
153           O << "\t.type " << name << ",@object\n";
154           O << "\t.size " << name << "," << Size << "\n";
155         }
156         O << name << ":\t\t\t\t# ";
157         WriteAsOperand(O, I, true, true, &M);
158         O << " = ";
159         WriteAsOperand(O, C, false, false, &M);
160         O << "\n";
161         emitGlobalConstant(C);
162       }
163     }
164   
165   if (forDarwin) {
166     // Output stubs for external global variables
167     if (GVStubs.begin() != GVStubs.end())
168       O << "\t.non_lazy_symbol_pointer\n";
169     for (std::set<std::string>::iterator i = GVStubs.begin(), e = GVStubs.end();
170          i != e; ++i) {
171       O << "L" << *i << "$non_lazy_ptr:\n";
172       O << "\t.indirect_symbol " << *i << "\n";
173       O << "\t.long\t0\n";
174     }
175
176     // Output stubs for dynamically-linked functions
177     unsigned j = 1;
178     for (std::set<std::string>::iterator i = FnStubs.begin(), e = FnStubs.end();
179          i != e; ++i, ++j) {
180       O << "\t.symbol_stub\n";
181       O << "L" << *i << "$stub:\n";
182       O << "\t.indirect_symbol " << *i << "\n";
183       O << "\tjmp\t*L" << j << "$lz\n";
184       O << "L" << *i << "$stub_binder:\n";
185       O << "\tpushl\t$L" << j << "$lz\n";
186       O << "\tjmp\tdyld_stub_binding_helper\n";
187       O << "\t.section __DATA, __la_sym_ptr3,lazy_symbol_pointers\n";
188       O << "L" << j << "$lz:\n";
189       O << "\t.indirect_symbol " << *i << "\n";
190       O << "\t.long\tL" << *i << "$stub_binder\n";
191     }
192
193     O << "\n";
194   
195     // Output stubs for link-once variables
196     if (LinkOnceStubs.begin() != LinkOnceStubs.end())
197       O << ".data\n.align 2\n";
198     for (std::set<std::string>::iterator i = LinkOnceStubs.begin(),
199          e = LinkOnceStubs.end(); i != e; ++i) {
200       O << "L" << *i << "$non_lazy_ptr:\n"
201         << "\t.long\t" << *i << '\n';
202     }
203   }
204
205   AsmPrinter::doFinalization(M);
206   return false; // success
207 }
208
209 /// createX86CodePrinterPass - Returns a pass that prints the X86 assembly code
210 /// for a MachineFunction to the given output stream, using the given target
211 /// machine description.
212 ///
213 FunctionPass *llvm::createX86CodePrinterPass(std::ostream &o,TargetMachine &tm){
214   switch (AsmWriterFlavor) {
215   default:
216     assert(0 && "Unknown asm flavor!");
217   case intel:
218     return new X86IntelAsmPrinter(o, tm);
219   case att:
220     return new X86ATTAsmPrinter(o, tm);
221   }
222 }