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