Refactor X86AsmPrinter.cpp into multiple files. Patch contributed
[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 "X86.h"
20 #include "llvm/Module.h"
21 #include "llvm/Assembly/Writer.h"
22 #include "llvm/CodeGen/MachineConstantPool.h"
23 #include "llvm/Support/Mangler.h"
24 #include "llvm/Support/CommandLine.h"
25 using namespace llvm;
26 using namespace x86;
27
28 Statistic<> EmittedInsts("asm-printer", "Number of machine instrs printed");
29
30 enum AsmWriterFlavorTy { att, intel };
31 cl::opt<AsmWriterFlavorTy>
32 AsmWriterFlavor("x86-asm-syntax",
33         cl::desc("Choose style of code to emit from X86 backend:"),
34         cl::values(
35                                 clEnumVal(att,   "  Emit AT&T-style assembly"),
36                                 clEnumVal(intel, "  Emit Intel-style assembly"),
37                                 clEnumValEnd),
38         cl::init(att));
39
40 /// doInitialization
41 bool X86SharedAsmPrinter::doInitialization(Module& M) {
42   bool leadingUnderscore = false;
43   forCygwin = false;
44   const std::string& TT = M.getTargetTriple();
45   if (TT.length() > 5) {
46     forCygwin = TT.find("cygwin") != std::string::npos ||
47                 TT.find("mingw")  != std::string::npos;
48     forDarwin = TT.find("darwin") != std::string::npos;
49   } else if (TT.empty()) {
50   #if defined(__CYGWIN__) || defined(__MINGW32__)
51     forCygwin = true;
52   #elif defined(__MACOSX__)
53     forDarwin = true;
54   #elif defined(_WIN32)
55     leadingUnderscore = true;
56   #else
57     leadingUnderscore = false;
58   #endif
59   }
60   if (leadingUnderscore || forCygwin || forDarwin)
61     GlobalPrefix = "_";
62
63   if (forDarwin)
64     AlignmentIsInBytes = false;
65
66   return AsmPrinter::doInitialization(M);
67 }
68
69 /// printConstantPool - Print to the current output stream assembly
70 /// representations of the constants in the constant pool MCP. This is
71 /// used to print out constants which have been "spilled to memory" by
72 /// the code generator.
73 ///
74 void X86SharedAsmPrinter::printConstantPool(MachineConstantPool *MCP) {
75   const std::vector<Constant*> &CP = MCP->getConstants();
76   const TargetData &TD = TM.getTargetData();
77
78   if (CP.empty()) return;
79
80   for (unsigned i = 0, e = CP.size(); i != e; ++i) {
81     O << "\t.section .rodata\n";
82     emitAlignment(TD.getTypeAlignmentShift(CP[i]->getType()));
83     O << ".CPI" << CurrentFnName << "_" << i << ":\t\t\t\t\t" << CommentString
84       << *CP[i] << "\n";
85     emitGlobalConstant(CP[i]);
86   }
87 }
88
89 bool X86SharedAsmPrinter::doFinalization(Module &M) {
90   const TargetData &TD = TM.getTargetData();
91   std::string CurSection;
92
93   // Print out module-level global variables here.
94   for (Module::const_global_iterator I = M.global_begin(), E = M.global_end(); I != E; ++I)
95         if (I->hasInitializer()) {   // External global require no code
96           O << "\n\n";
97           std::string name = Mang->getValueName(I);
98           Constant *C = I->getInitializer();
99           unsigned Size = TD.getTypeSize(C->getType());
100           unsigned Align = TD.getTypeAlignmentShift(C->getType());
101
102           if (C->isNullValue() &&
103                 (I->hasLinkOnceLinkage() || I->hasInternalLinkage() ||
104                         I->hasWeakLinkage() /* FIXME: Verify correct */)) {
105                 SwitchSection(O, CurSection, ".data");
106                 if (!forCygwin && I->hasInternalLinkage())
107                   O << "\t.local " << name << "\n";
108                 O << "\t.comm " << name << "," << TD.getTypeSize(C->getType());
109                 if (!forCygwin)
110                   O << "," << (1 << Align);
111                 O << "\t\t# ";
112                 WriteAsOperand(O, I, true, true, &M);
113                 O << "\n";
114           } else {
115                 switch (I->getLinkage()) {
116                 case GlobalValue::LinkOnceLinkage:
117                 case GlobalValue::WeakLinkage:   // FIXME: Verify correct for weak.
118                   // Nonnull linkonce -> weak
119                   O << "\t.weak " << name << "\n";
120                   SwitchSection(O, CurSection, "");
121                   O << "\t.section\t.llvm.linkonce.d." << name << ",\"aw\",@progbits\n";
122                   break;
123                 case GlobalValue::AppendingLinkage:
124                   // FIXME: appending linkage variables should go into a section of
125                   // their name or something.  For now, just emit them as external.
126                 case GlobalValue::ExternalLinkage:
127                   // If external or appending, declare as a global symbol
128                   O << "\t.globl " << name << "\n";
129                   // FALL THROUGH
130                 case GlobalValue::InternalLinkage:
131                   if (C->isNullValue())
132                     SwitchSection(O, CurSection, ".bss");
133                   else
134                     SwitchSection(O, CurSection, ".data");
135                   break;
136                 case GlobalValue::GhostLinkage:
137                   std::cerr << "GhostLinkage cannot appear in X86AsmPrinter!\n";
138                   abort();
139                 }
140
141                 emitAlignment(Align);
142                 if (!forCygwin && !forDarwin) {
143                   O << "\t.type " << name << ",@object\n";
144                   O << "\t.size " << name << "," << Size << "\n";
145                 }
146                 O << name << ":\t\t\t\t# ";
147                 WriteAsOperand(O, I, true, true, &M);
148                 O << " = ";
149                 WriteAsOperand(O, C, false, false, &M);
150                 O << "\n";
151                 emitGlobalConstant(C);
152           }
153     }
154
155   AsmPrinter::doFinalization(M);
156   return false; // success
157 }
158
159 /// createX86CodePrinterPass - Returns a pass that prints the X86 assembly code
160 /// for a MachineFunction to the given output stream, using the given target
161 /// machine description.
162 ///
163 FunctionPass *llvm::createX86CodePrinterPass(std::ostream &o,TargetMachine &tm){
164   switch (AsmWriterFlavor) {
165   default:
166     assert(0 && "Unknown asm flavor!");
167   case intel:
168     return new X86IntelAsmPrinter(o, tm);
169   case att:
170     return new X86ATTAsmPrinter(o, tm);
171   }
172 }