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