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