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