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