Disable x86 fastcc from passing args in registers
[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 "X86AsmPrinter.h"
18 #include "X86ATTAsmPrinter.h"
19 #include "X86IntelAsmPrinter.h"
20 #include "X86Subtarget.h"
21 #include "llvm/Constants.h"
22 #include "llvm/Module.h"
23 #include "llvm/Type.h"
24 #include "llvm/Assembly/Writer.h"
25 #include "llvm/Support/Mangler.h"
26 #include "llvm/Support/CommandLine.h"
27 using namespace llvm;
28
29 Statistic<> llvm::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   PrivateGlobalPrefix = ".L";
48   
49   switch (Subtarget->TargetType) {
50   case X86Subtarget::isDarwin:
51     AlignmentIsInBytes = false;
52     GlobalPrefix = "_";
53     Data64bitsDirective = 0;       // we can't emit a 64-bit unit
54     ZeroDirective = "\t.space\t";  // ".space N" emits N zeros.
55     PrivateGlobalPrefix = "L";     // Marker for constant pool idxs
56     ConstantPoolSection = "\t.const\n";
57     LCOMMDirective = "\t.lcomm\t";
58     COMMDirectiveTakesAlignment = false;
59     HasDotTypeDotSizeDirective = false;
60     forDarwin = true;
61     StaticCtorsSection = ".mod_init_func";
62     StaticDtorsSection = ".mod_term_func";
63     InlineAsmStart = InlineAsmEnd = "";  // Don't use #APP/#NO_APP
64     break;
65   case X86Subtarget::isCygwin:
66     GlobalPrefix = "_";
67     COMMDirectiveTakesAlignment = false;
68     HasDotTypeDotSizeDirective = false;
69     break;
70   case X86Subtarget::isWindows:
71     GlobalPrefix = "_";
72     HasDotTypeDotSizeDirective = false;
73     break;
74   default: break;
75   }
76   
77   if (forDarwin) {
78     // Emit initial debug information.
79     DW.BeginModule(M);
80   }
81
82   return AsmPrinter::doInitialization(M);
83 }
84
85 bool X86SharedAsmPrinter::doFinalization(Module &M) {
86   const TargetData &TD = TM.getTargetData();
87
88   // Print out module-level global variables here.
89   for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
90        I != E; ++I) {
91     if (!I->hasInitializer()) continue;   // External global require no code
92     
93     // Check to see if this is a special global used by LLVM, if so, emit it.
94     if (EmitSpecialLLVMGlobal(I))
95       continue;
96     
97     std::string name = Mang->getValueName(I);
98     Constant *C = I->getInitializer();
99     unsigned Size = TD.getTypeSize(C->getType());
100     unsigned Align = getPreferredAlignmentLog(I);
101
102     if (C->isNullValue() && /* FIXME: Verify correct */
103         (I->hasInternalLinkage() || I->hasWeakLinkage() ||
104          I->hasLinkOnceLinkage() ||
105          (forDarwin && I->hasExternalLinkage() && !I->hasSection()))) {
106       if (Size == 0) Size = 1;   // .comm Foo, 0 is undefined, avoid it.
107       if (I->hasExternalLinkage()) {
108           O << "\t.globl\t" << name << "\n";
109           O << "\t.zerofill __DATA__, __common, " << name << ", "
110             << Size << ", " << Align;
111       } else {
112         SwitchSection(".data", I);
113         if (LCOMMDirective != NULL) {
114           if (I->hasInternalLinkage()) {
115             O << LCOMMDirective << name << "," << Size;
116             if (forDarwin)
117               O << "," << (AlignmentIsInBytes ? (1 << Align) : Align);
118           } else
119             O << COMMDirective  << name << "," << Size;
120         } else {
121           if (I->hasInternalLinkage())
122             O << "\t.local\t" << name << "\n";
123           O << COMMDirective  << name << "," << Size;
124           if (COMMDirectiveTakesAlignment)
125             O << "," << (AlignmentIsInBytes ? (1 << Align) : Align);
126         }
127       }
128       O << "\t\t" << CommentString << " " << I->getName() << "\n";
129     } else {
130       switch (I->getLinkage()) {
131       case GlobalValue::LinkOnceLinkage:
132       case GlobalValue::WeakLinkage:
133         if (forDarwin) {
134           O << "\t.globl " << name << "\n"
135             << "\t.weak_definition " << name << "\n";
136           SwitchSection(".section __DATA,__datacoal_nt,coalesced", I);
137         } else {
138           O << "\t.section\t.llvm.linkonce.d." << name << ",\"aw\",@progbits\n";
139           O << "\t.weak " << name << "\n";
140         }
141         break;
142       case GlobalValue::AppendingLinkage:
143         // FIXME: appending linkage variables should go into a section of
144         // their name or something.  For now, just emit them as external.
145       case GlobalValue::ExternalLinkage:
146         // If external or appending, declare as a global symbol
147         O << "\t.globl " << name << "\n";
148         // FALL THROUGH
149       case GlobalValue::InternalLinkage:
150         SwitchSection(".data", I);
151         break;
152       default:
153         assert(0 && "Unknown linkage type!");
154       }
155
156       EmitAlignment(Align, I);
157       O << name << ":\t\t\t\t" << CommentString << " " << I->getName()
158         << "\n";
159       if (HasDotTypeDotSizeDirective)
160         O << "\t.size " << name << ", " << Size << "\n";
161
162       EmitGlobalConstant(C);
163       O << '\n';
164     }
165   }
166   
167   if (forDarwin) {
168     SwitchSection("", 0);
169
170     // Output stubs for dynamically-linked functions
171     unsigned j = 1;
172     for (std::set<std::string>::iterator i = FnStubs.begin(), e = FnStubs.end();
173          i != e; ++i, ++j) {
174       SwitchSection(".section __IMPORT,__jump_table,symbol_stubs,"
175                     "self_modifying_code+pure_instructions,5", 0);
176       O << "L" << *i << "$stub:\n";
177       O << "\t.indirect_symbol " << *i << "\n";
178       O << "\thlt ; hlt ; hlt ; hlt ; hlt\n";
179     }
180
181     O << "\n";
182
183     // Output stubs for external and common global variables.
184     if (GVStubs.begin() != GVStubs.end())
185       SwitchSection(".section __IMPORT,__pointers,non_lazy_symbol_pointers", 0);
186     for (std::set<std::string>::iterator i = GVStubs.begin(), e = GVStubs.end();
187          i != e; ++i) {
188       O << "L" << *i << "$non_lazy_ptr:\n";
189       O << "\t.indirect_symbol " << *i << "\n";
190       O << "\t.long\t0\n";
191     }
192
193     // Emit initial debug information.
194     DW.EndModule(M);
195
196     // Funny Darwin hack: This flag tells the linker that no global symbols
197     // contain code that falls through to other global symbols (e.g. the obvious
198     // implementation of multiple entry points).  If this doesn't occur, the
199     // linker can safely perform dead code stripping.  Since LLVM never generates
200     // code that does this, it is always safe to set.
201     O << "\t.subsections_via_symbols\n";
202   }
203
204   AsmPrinter::doFinalization(M);
205   return false; // success
206 }
207
208 /// createX86CodePrinterPass - Returns a pass that prints the X86 assembly code
209 /// for a MachineFunction to the given output stream, using the given target
210 /// machine description.
211 ///
212 FunctionPass *llvm::createX86CodePrinterPass(std::ostream &o,
213                                              X86TargetMachine &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 }