It makes no sense to have a ODR version of common
[oota-llvm.git] / lib / Target / Sparc / AsmPrinter / SparcAsmPrinter.cpp
1 //===-- SparcAsmPrinter.cpp - Sparc LLVM assembly writer ------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file contains a printer that converts from our internal representation
11 // of machine-dependent LLVM code to GAS-format SPARC assembly language.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #define DEBUG_TYPE "asm-printer"
16 #include "Sparc.h"
17 #include "SparcInstrInfo.h"
18 #include "llvm/Constants.h"
19 #include "llvm/DerivedTypes.h"
20 #include "llvm/Module.h"
21 #include "llvm/CodeGen/AsmPrinter.h"
22 #include "llvm/CodeGen/DwarfWriter.h"
23 #include "llvm/CodeGen/MachineFunctionPass.h"
24 #include "llvm/CodeGen/MachineConstantPool.h"
25 #include "llvm/CodeGen/MachineInstr.h"
26 #include "llvm/Target/TargetAsmInfo.h"
27 #include "llvm/Target/TargetData.h"
28 #include "llvm/Target/TargetMachine.h"
29 #include "llvm/Support/Mangler.h"
30 #include "llvm/Support/raw_ostream.h"
31 #include "llvm/ADT/Statistic.h"
32 #include "llvm/ADT/StringExtras.h"
33 #include "llvm/Support/CommandLine.h"
34 #include "llvm/Support/MathExtras.h"
35 #include <cctype>
36 #include <cstring>
37 #include <map>
38 using namespace llvm;
39
40 STATISTIC(EmittedInsts, "Number of machine instrs printed");
41
42 namespace {
43   class VISIBILITY_HIDDEN SparcAsmPrinter : public AsmPrinter {
44     /// We name each basic block in a Function with a unique number, so
45     /// that we can consistently refer to them later. This is cleared
46     /// at the beginning of each call to runOnMachineFunction().
47     ///
48     typedef std::map<const Value *, unsigned> ValueMapTy;
49     ValueMapTy NumberForBB;
50   public:
51     SparcAsmPrinter(raw_ostream &O, TargetMachine &TM,
52                     const TargetAsmInfo *T, bool F)
53       : AsmPrinter(O, TM, T, F) {}
54
55     virtual const char *getPassName() const {
56       return "Sparc Assembly Printer";
57     }
58
59     void printModuleLevelGV(const GlobalVariable* GVar);
60     void printOperand(const MachineInstr *MI, int opNum);
61     void printMemOperand(const MachineInstr *MI, int opNum,
62                          const char *Modifier = 0);
63     void printCCOperand(const MachineInstr *MI, int opNum);
64
65     bool printInstruction(const MachineInstr *MI);  // autogenerated.
66     bool runOnMachineFunction(MachineFunction &F);
67     bool doInitialization(Module &M);
68     bool doFinalization(Module &M);
69     bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
70                        unsigned AsmVariant, const char *ExtraCode);
71     bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
72                              unsigned AsmVariant, const char *ExtraCode);
73   };
74 } // end of anonymous namespace
75
76 #include "SparcGenAsmWriter.inc"
77
78 /// createSparcCodePrinterPass - Returns a pass that prints the SPARC
79 /// assembly code for a MachineFunction to the given output stream,
80 /// using the given target machine description.  This should work
81 /// regardless of whether the function is in SSA form.
82 ///
83 FunctionPass *llvm::createSparcCodePrinterPass(raw_ostream &o,
84                                                TargetMachine &tm,
85                                                bool fast) {
86   return new SparcAsmPrinter(o, tm, tm.getTargetAsmInfo(), fast);
87 }
88
89 /// runOnMachineFunction - This uses the printInstruction()
90 /// method to print assembly for each instruction.
91 ///
92 bool SparcAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
93   this->MF = &MF;
94
95   SetupMachineFunction(MF);
96
97   // Print out constants referenced by the function
98   EmitConstantPool(MF.getConstantPool());
99
100   // BBNumber is used here so that a given Printer will never give two
101   // BBs the same name. (If you have a better way, please let me know!)
102   static unsigned BBNumber = 0;
103
104   O << "\n\n";
105
106   // Print out the label for the function.
107   const Function *F = MF.getFunction();
108   SwitchToSection(TAI->SectionForGlobal(F));
109   EmitAlignment(4, F);
110   O << "\t.globl\t" << CurrentFnName << '\n';
111
112   printVisibility(CurrentFnName, F->getVisibility());
113
114   O << "\t.type\t" << CurrentFnName << ", #function\n";
115   O << CurrentFnName << ":\n";
116
117   // Number each basic block so that we can consistently refer to them
118   // in PC-relative references.
119   // FIXME: Why not use the MBB numbers?
120   NumberForBB.clear();
121   for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
122        I != E; ++I) {
123     NumberForBB[I->getBasicBlock()] = BBNumber++;
124   }
125
126   // Print out code for the function.
127   for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
128        I != E; ++I) {
129     // Print a label for the basic block.
130     if (I != MF.begin()) {
131       printBasicBlockLabel(I, true, true);
132       O << '\n';
133     }
134     for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
135          II != E; ++II) {
136       // Print the assembly for the instruction.
137       printInstruction(II);
138       ++EmittedInsts;
139     }
140   }
141
142   // We didn't modify anything.
143   return false;
144 }
145
146 void SparcAsmPrinter::printOperand(const MachineInstr *MI, int opNum) {
147   const MachineOperand &MO = MI->getOperand (opNum);
148   const TargetRegisterInfo &RI = *TM.getRegisterInfo();
149   bool CloseParen = false;
150   if (MI->getOpcode() == SP::SETHIi && !MO.isReg() && !MO.isImm()) {
151     O << "%hi(";
152     CloseParen = true;
153   } else if ((MI->getOpcode() == SP::ORri || MI->getOpcode() == SP::ADDri) &&
154              !MO.isReg() && !MO.isImm()) {
155     O << "%lo(";
156     CloseParen = true;
157   }
158   switch (MO.getType()) {
159   case MachineOperand::MO_Register:
160     if (TargetRegisterInfo::isPhysicalRegister(MO.getReg()))
161       O << "%" << LowercaseString (RI.get(MO.getReg()).AsmName);
162     else
163       O << "%reg" << MO.getReg();
164     break;
165
166   case MachineOperand::MO_Immediate:
167     O << (int)MO.getImm();
168     break;
169   case MachineOperand::MO_MachineBasicBlock:
170     printBasicBlockLabel(MO.getMBB());
171     return;
172   case MachineOperand::MO_GlobalAddress:
173     {
174       const GlobalValue *GV = MO.getGlobal();
175       O << Mang->getValueName(GV);
176     }
177     break;
178   case MachineOperand::MO_ExternalSymbol:
179     O << MO.getSymbolName();
180     break;
181   case MachineOperand::MO_ConstantPoolIndex:
182     O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << "_"
183       << MO.getIndex();
184     break;
185   default:
186     O << "<unknown operand type>"; abort (); break;
187   }
188   if (CloseParen) O << ")";
189 }
190
191 void SparcAsmPrinter::printMemOperand(const MachineInstr *MI, int opNum,
192                                       const char *Modifier) {
193   printOperand(MI, opNum);
194
195   // If this is an ADD operand, emit it like normal operands.
196   if (Modifier && !strcmp(Modifier, "arith")) {
197     O << ", ";
198     printOperand(MI, opNum+1);
199     return;
200   }
201
202   if (MI->getOperand(opNum+1).isReg() &&
203       MI->getOperand(opNum+1).getReg() == SP::G0)
204     return;   // don't print "+%g0"
205   if (MI->getOperand(opNum+1).isImm() &&
206       MI->getOperand(opNum+1).getImm() == 0)
207     return;   // don't print "+0"
208
209   O << "+";
210   if (MI->getOperand(opNum+1).isGlobal() ||
211       MI->getOperand(opNum+1).isCPI()) {
212     O << "%lo(";
213     printOperand(MI, opNum+1);
214     O << ")";
215   } else {
216     printOperand(MI, opNum+1);
217   }
218 }
219
220 void SparcAsmPrinter::printCCOperand(const MachineInstr *MI, int opNum) {
221   int CC = (int)MI->getOperand(opNum).getImm();
222   O << SPARCCondCodeToString((SPCC::CondCodes)CC);
223 }
224
225 bool SparcAsmPrinter::doInitialization(Module &M) {
226   Mang = new Mangler(M, "", TAI->getPrivateGlobalPrefix());
227   return false; // success
228 }
229
230 bool SparcAsmPrinter::doFinalization(Module &M) {
231   // Print out module-level global variables here.
232   for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
233        I != E; ++I)
234     printModuleLevelGV(I);
235
236   O << '\n';
237
238   return AsmPrinter::doFinalization(M);
239 }
240
241 void SparcAsmPrinter::printModuleLevelGV(const GlobalVariable* GVar) {
242   const TargetData *TD = TM.getTargetData();
243
244   if (!GVar->hasInitializer())
245     return;  // External global require no code
246
247   // Check to see if this is a special global used by LLVM, if so, emit it.
248   if (EmitSpecialLLVMGlobal(GVar))
249     return;
250
251   O << "\n\n";
252   std::string name = Mang->getValueName(GVar);
253   Constant *C = GVar->getInitializer();
254   unsigned Size = TD->getTypePaddedSize(C->getType());
255   unsigned Align = TD->getPreferredAlignment(GVar);
256
257   printVisibility(name, GVar->getVisibility());
258
259   SwitchToSection(TAI->SectionForGlobal(GVar));
260
261   if (C->isNullValue() && !GVar->hasSection()) {
262     if (!GVar->isThreadLocal() &&
263         (GVar->hasLocalLinkage() || GVar->isWeakForLinker())) {
264       if (Size == 0) Size = 1;   // .comm Foo, 0 is undefined, avoid it.
265
266       if (GVar->hasLocalLinkage())
267         O << "\t.local " << name << '\n';
268
269       O << TAI->getCOMMDirective() << name << ',' << Size;
270       if (TAI->getCOMMDirectiveTakesAlignment())
271         O << ',' << (1 << Align);
272
273       O << '\n';
274       return;
275     }
276   }
277
278   switch (GVar->getLinkage()) {
279    case GlobalValue::CommonLinkage:
280    case GlobalValue::LinkOnceAnyLinkage:
281    case GlobalValue::LinkOnceODRLinkage:
282    case GlobalValue::WeakAnyLinkage: // FIXME: Verify correct for weak.
283    case GlobalValue::WeakODRLinkage: // FIXME: Verify correct for weak.
284     // Nonnull linkonce -> weak
285     O << "\t.weak " << name << '\n';
286     break;
287    case GlobalValue::AppendingLinkage:
288     // FIXME: appending linkage variables should go into a section of
289     // their name or something.  For now, just emit them as external.
290    case GlobalValue::ExternalLinkage:
291     // If external or appending, declare as a global symbol
292     O << TAI->getGlobalDirective() << name << '\n';
293     // FALL THROUGH
294    case GlobalValue::PrivateLinkage:
295    case GlobalValue::InternalLinkage:
296     break;
297    case GlobalValue::GhostLinkage:
298     cerr << "Should not have any unmaterialized functions!\n";
299     abort();
300    case GlobalValue::DLLImportLinkage:
301     cerr << "DLLImport linkage is not supported by this target!\n";
302     abort();
303    case GlobalValue::DLLExportLinkage:
304     cerr << "DLLExport linkage is not supported by this target!\n";
305     abort();
306    default:
307     assert(0 && "Unknown linkage type!");
308   }
309
310   EmitAlignment(Align, GVar);
311
312   if (TAI->hasDotTypeDotSizeDirective()) {
313     O << "\t.type " << name << ",#object\n";
314     O << "\t.size " << name << ',' << Size << '\n';
315   }
316
317   O << name << ":\n";
318   EmitGlobalConstant(C);
319 }
320
321 /// PrintAsmOperand - Print out an operand for an inline asm expression.
322 ///
323 bool SparcAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
324                                       unsigned AsmVariant,
325                                       const char *ExtraCode) {
326   if (ExtraCode && ExtraCode[0]) {
327     if (ExtraCode[1] != 0) return true; // Unknown modifier.
328
329     switch (ExtraCode[0]) {
330     default: return true;  // Unknown modifier.
331     case 'r':
332      break;
333     }
334   }
335
336   printOperand(MI, OpNo);
337
338   return false;
339 }
340
341 bool SparcAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
342                                             unsigned OpNo,
343                                             unsigned AsmVariant,
344                                             const char *ExtraCode) {
345   if (ExtraCode && ExtraCode[0])
346     return true;  // Unknown modifier
347
348   O << '[';
349   printMemOperand(MI, OpNo);
350   O << ']';
351
352   return false;
353 }