Add an entry
[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/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 using namespace x86;
29
30 Statistic<> llvm::x86::EmittedInsts("asm-printer",
31                                     "Number of machine instrs printed");
32
33 enum AsmWriterFlavorTy { att, intel };
34 cl::opt<AsmWriterFlavorTy>
35 AsmWriterFlavor("x86-asm-syntax",
36                 cl::desc("Choose style of code to emit from X86 backend:"),
37                 cl::values(
38                            clEnumVal(att,   "  Emit AT&T-style assembly"),
39                            clEnumVal(intel, "  Emit Intel-style assembly"),
40                            clEnumValEnd),
41                 cl::init(att));
42
43 /// doInitialization
44 bool X86SharedAsmPrinter::doInitialization(Module &M) {
45   const X86Subtarget *Subtarget = &TM.getSubtarget<X86Subtarget>();
46   
47   forDarwin = false;
48   PrivateGlobalPrefix = ".L";
49   
50   switch (Subtarget->TargetType) {
51   case X86Subtarget::isDarwin:
52     AlignmentIsInBytes = false;
53     GlobalPrefix = "_";
54     Data64bitsDirective = 0;       // we can't emit a 64-bit unit
55     ZeroDirective = "\t.space\t";  // ".space N" emits N zeros.
56     PrivateGlobalPrefix = "L";     // Marker for constant pool idxs
57     ConstantPoolSection = "\t.const\n";
58     LCOMMDirective = "\t.lcomm\t";
59     COMMDirectiveTakesAlignment = false;
60     HasDotTypeDotSizeDirective = false;
61     forDarwin = true;
62     StaticCtorsSection = ".mod_init_func";
63     StaticDtorsSection = ".mod_term_func";
64     InlineAsmStart = InlineAsmEnd = "";  // Don't use #APP/#NO_APP
65     break;
66   case X86Subtarget::isCygwin:
67     GlobalPrefix = "_";
68     COMMDirectiveTakesAlignment = false;
69     HasDotTypeDotSizeDirective = false;
70     break;
71   case X86Subtarget::isWindows:
72     GlobalPrefix = "_";
73     HasDotTypeDotSizeDirective = false;
74     break;
75   default: break;
76   }
77   
78   return AsmPrinter::doInitialization(M);
79 }
80
81 bool X86SharedAsmPrinter::doFinalization(Module &M) {
82   const TargetData &TD = TM.getTargetData();
83
84   // Print out module-level global variables here.
85   for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
86        I != E; ++I) {
87     if (!I->hasInitializer()) continue;   // External global require no code
88     
89     // Check to see if this is a special global used by LLVM, if so, emit it.
90     if (I->hasAppendingLinkage() && EmitSpecialLLVMGlobal(I))
91       continue;
92     
93     std::string name = Mang->getValueName(I);
94     Constant *C = I->getInitializer();
95     unsigned Size = TD.getTypeSize(C->getType());
96     unsigned Align = getPreferredAlignmentLog(I);
97
98     if (C->isNullValue() && /* FIXME: Verify correct */
99         (I->hasInternalLinkage() || I->hasWeakLinkage() ||
100          I->hasLinkOnceLinkage() ||
101          (forDarwin && I->hasExternalLinkage() && !I->hasSection()))) {
102       if (Size == 0) Size = 1;   // .comm Foo, 0 is undefined, avoid it.
103       if (I->hasExternalLinkage()) {
104           O << "\t.globl\t" << name << "\n";
105           O << "\t.zerofill __DATA__, __common, " << name << ", "
106             << Size << ", " << Align;
107       } else {
108         SwitchSection(".data", I);
109         if (LCOMMDirective != NULL) {
110           if (I->hasInternalLinkage()) {
111             O << LCOMMDirective << name << "," << Size;
112             if (forDarwin)
113               O << "," << (AlignmentIsInBytes ? (1 << Align) : Align);
114           } else
115             O << COMMDirective  << name << "," << Size;
116         } else {
117           if (I->hasInternalLinkage())
118             O << "\t.local\t" << name << "\n";
119           O << COMMDirective  << name << "," << Size;
120           if (COMMDirectiveTakesAlignment)
121             O << "," << (AlignmentIsInBytes ? (1 << Align) : Align);
122         }
123       }
124       O << "\t\t" << CommentString << " " << I->getName() << "\n";
125     } else {
126       switch (I->getLinkage()) {
127       case GlobalValue::LinkOnceLinkage:
128       case GlobalValue::WeakLinkage:
129         if (forDarwin) {
130           O << "\t.globl " << name << "\n"
131             << "\t.weak_definition " << name << "\n";
132           SwitchSection(".section __DATA,__datacoal_nt,coalesced", I);
133         } else {
134           O << "\t.section\t.llvm.linkonce.d." << name << ",\"aw\",@progbits\n";
135           O << "\t.weak " << name << "\n";
136         }
137         break;
138       case GlobalValue::AppendingLinkage:
139         // FIXME: appending linkage variables should go into a section of
140         // their name or something.  For now, just emit them as external.
141       case GlobalValue::ExternalLinkage:
142         // If external or appending, declare as a global symbol
143         O << "\t.globl " << name << "\n";
144         // FALL THROUGH
145       case GlobalValue::InternalLinkage:
146         SwitchSection(".data", I);
147         break;
148       default:
149         assert(0 && "Unknown linkage type!");
150       }
151
152       EmitAlignment(Align, I);
153       O << name << ":\t\t\t\t" << CommentString << " " << I->getName()
154         << "\n";
155       if (HasDotTypeDotSizeDirective)
156         O << "\t.size " << name << ", " << Size << "\n";
157
158       EmitGlobalConstant(C);
159       O << '\n';
160     }
161   }
162   
163   if (forDarwin) {
164     SwitchSection("", 0);
165
166     // Output stubs for dynamically-linked functions
167     unsigned j = 1;
168     for (std::set<std::string>::iterator i = FnStubs.begin(), e = FnStubs.end();
169          i != e; ++i, ++j) {
170       SwitchSection(".section __IMPORT,__jump_table,symbol_stubs,"
171                     "self_modifying_code+pure_instructions,5", 0);
172       O << "L" << *i << "$stub:\n";
173       O << "\t.indirect_symbol " << *i << "\n";
174       O << "\thlt ; hlt ; hlt ; hlt ; hlt\n";
175     }
176
177     O << "\n";
178
179     // Output stubs for external and common global variables.
180     if (GVStubs.begin() != GVStubs.end())
181       SwitchSection(".section __IMPORT,__pointers,non_lazy_symbol_pointers", 0);
182     for (std::set<std::string>::iterator i = GVStubs.begin(), e = GVStubs.end();
183          i != e; ++i) {
184       O << "L" << *i << "$non_lazy_ptr:\n";
185       O << "\t.indirect_symbol " << *i << "\n";
186       O << "\t.long\t0\n";
187     }
188   }
189
190   AsmPrinter::doFinalization(M);
191   return false; // success
192 }
193
194 /// createX86CodePrinterPass - Returns a pass that prints the X86 assembly code
195 /// for a MachineFunction to the given output stream, using the given target
196 /// machine description.
197 ///
198 FunctionPass *llvm::createX86CodePrinterPass(std::ostream &o,TargetMachine &tm){
199   switch (AsmWriterFlavor) {
200   default:
201     assert(0 && "Unknown asm flavor!");
202   case intel:
203     return new X86IntelAsmPrinter(o, tm);
204   case att:
205     return new X86ATTAsmPrinter(o, tm);
206   }
207 }