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