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