remove DebugLoc from MCInst and eliminate "Comment printing" from
[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/CodeGen/AsmPrinter.h"
23 #include "llvm/CodeGen/DwarfWriter.h"
24 #include "llvm/CodeGen/MachineFunctionPass.h"
25 #include "llvm/CodeGen/MachineConstantPool.h"
26 #include "llvm/CodeGen/MachineInstr.h"
27 #include "llvm/MC/MCStreamer.h"
28 #include "llvm/MC/MCAsmInfo.h"
29 #include "llvm/Target/TargetLoweringObjectFile.h"
30 #include "llvm/Target/TargetRegistry.h"
31 #include "llvm/ADT/Statistic.h"
32 #include "llvm/ADT/StringExtras.h"
33 #include "llvm/Support/CommandLine.h"
34 #include "llvm/Support/ErrorHandling.h"
35 #include "llvm/Support/FormattedStream.h"
36 #include "llvm/Support/Mangler.h"
37 #include "llvm/Support/MathExtras.h"
38 #include <cctype>
39 #include <cstring>
40 #include <map>
41 using namespace llvm;
42
43 STATISTIC(EmittedInsts, "Number of machine instrs printed");
44
45 namespace {
46   class VISIBILITY_HIDDEN SparcAsmPrinter : public AsmPrinter {
47     /// We name each basic block in a Function with a unique number, so
48     /// that we can consistently refer to them later. This is cleared
49     /// at the beginning of each call to runOnMachineFunction().
50     ///
51     typedef std::map<const Value *, unsigned> ValueMapTy;
52     ValueMapTy NumberForBB;
53     unsigned BBNumber;
54   public:
55     explicit SparcAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
56                              const MCAsmInfo *T, bool V)
57       : AsmPrinter(O, TM, T, V), BBNumber(0) {}
58
59     virtual const char *getPassName() const {
60       return "Sparc Assembly Printer";
61     }
62
63     void PrintGlobalVariable(const GlobalVariable *GVar);
64     void printOperand(const MachineInstr *MI, int opNum);
65     void printMemOperand(const MachineInstr *MI, int opNum,
66                          const char *Modifier = 0);
67     void printCCOperand(const MachineInstr *MI, int opNum);
68
69     void printInstruction(const MachineInstr *MI);  // autogenerated.
70     bool runOnMachineFunction(MachineFunction &F);
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
81 /// runOnMachineFunction - This uses the printInstruction()
82 /// method to print assembly for each instruction.
83 ///
84 bool SparcAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
85   this->MF = &MF;
86
87   SetupMachineFunction(MF);
88
89   // Print out constants referenced by the function
90   EmitConstantPool(MF.getConstantPool());
91
92   // BBNumber is used here so that a given Printer will never give two
93   // BBs the same name. (If you have a better way, please let me know!)
94
95   O << "\n\n";
96
97   // Print out the label for the function.
98   const Function *F = MF.getFunction();
99   OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(F, Mang, TM));
100   EmitAlignment(MF.getAlignment(), F);
101   O << "\t.globl\t" << CurrentFnName << '\n';
102
103   printVisibility(CurrentFnName, F->getVisibility());
104
105   O << "\t.type\t" << CurrentFnName << ", #function\n";
106   O << CurrentFnName << ":\n";
107   // Emit pre-function debug information.
108   DW->BeginFunction(&MF);
109
110   // Number each basic block so that we can consistently refer to them
111   // in PC-relative references.
112   // FIXME: Why not use the MBB numbers?
113   NumberForBB.clear();
114   for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
115        I != E; ++I) {
116     NumberForBB[I->getBasicBlock()] = BBNumber++;
117   }
118
119   // Print out code for the function.
120   for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
121        I != E; ++I) {
122     // Print a label for the basic block.
123     if (I != MF.begin()) {
124       printBasicBlockLabel(I, true, true);
125       O << '\n';
126     }
127     for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
128          II != E; ++II) {
129       // Print the assembly for the instruction.
130       processDebugLoc(II->getDebugLoc());
131       printInstruction(II);
132       
133       if (VerboseAsm && !II->getDebugLoc().isUnknown())
134         EmitComments(*II);
135       O << '\n';
136       
137       ++EmittedInsts;
138     }
139   }
140
141   // Emit post-function debug information.
142   DW->EndFunction(&MF);
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     O << Mang->getMangledName(MO.getGlobal());
176     break;
177   case MachineOperand::MO_ExternalSymbol:
178     O << MO.getSymbolName();
179     break;
180   case MachineOperand::MO_ConstantPoolIndex:
181     O << MAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << "_"
182       << MO.getIndex();
183     break;
184   default:
185     llvm_unreachable("<unknown operand type>");
186   }
187   if (CloseParen) O << ")";
188 }
189
190 void SparcAsmPrinter::printMemOperand(const MachineInstr *MI, int opNum,
191                                       const char *Modifier) {
192   printOperand(MI, opNum);
193
194   // If this is an ADD operand, emit it like normal operands.
195   if (Modifier && !strcmp(Modifier, "arith")) {
196     O << ", ";
197     printOperand(MI, opNum+1);
198     return;
199   }
200
201   if (MI->getOperand(opNum+1).isReg() &&
202       MI->getOperand(opNum+1).getReg() == SP::G0)
203     return;   // don't print "+%g0"
204   if (MI->getOperand(opNum+1).isImm() &&
205       MI->getOperand(opNum+1).getImm() == 0)
206     return;   // don't print "+0"
207
208   O << "+";
209   if (MI->getOperand(opNum+1).isGlobal() ||
210       MI->getOperand(opNum+1).isCPI()) {
211     O << "%lo(";
212     printOperand(MI, opNum+1);
213     O << ")";
214   } else {
215     printOperand(MI, opNum+1);
216   }
217 }
218
219 void SparcAsmPrinter::printCCOperand(const MachineInstr *MI, int opNum) {
220   int CC = (int)MI->getOperand(opNum).getImm();
221   O << SPARCCondCodeToString((SPCC::CondCodes)CC);
222 }
223
224 void SparcAsmPrinter::PrintGlobalVariable(const GlobalVariable* GVar) {
225   const TargetData *TD = TM.getTargetData();
226
227   if (!GVar->hasInitializer())
228     return;  // External global require no code
229
230   // Check to see if this is a special global used by LLVM, if so, emit it.
231   if (EmitSpecialLLVMGlobal(GVar))
232     return;
233
234   O << "\n\n";
235   std::string name = Mang->getMangledName(GVar);
236   Constant *C = GVar->getInitializer();
237   unsigned Size = TD->getTypeAllocSize(C->getType());
238   unsigned Align = TD->getPreferredAlignment(GVar);
239
240   printVisibility(name, GVar->getVisibility());
241
242   OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(GVar, Mang,
243                                                                   TM));
244
245   if (C->isNullValue() && !GVar->hasSection()) {
246     if (!GVar->isThreadLocal() &&
247         (GVar->hasLocalLinkage() || GVar->isWeakForLinker())) {
248       if (Size == 0) Size = 1;   // .comm Foo, 0 is undefined, avoid it.
249
250       if (GVar->hasLocalLinkage())
251         O << "\t.local " << name << '\n';
252
253       O << MAI->getCOMMDirective() << name << ',' << Size;
254       if (MAI->getCOMMDirectiveTakesAlignment())
255         O << ',' << (1 << Align);
256
257       O << '\n';
258       return;
259     }
260   }
261
262   switch (GVar->getLinkage()) {
263    case GlobalValue::CommonLinkage:
264    case GlobalValue::LinkOnceAnyLinkage:
265    case GlobalValue::LinkOnceODRLinkage:
266    case GlobalValue::WeakAnyLinkage: // FIXME: Verify correct for weak.
267    case GlobalValue::WeakODRLinkage: // FIXME: Verify correct for weak.
268     // Nonnull linkonce -> weak
269     O << "\t.weak " << name << '\n';
270     break;
271    case GlobalValue::AppendingLinkage:
272     // FIXME: appending linkage variables should go into a section of
273     // their name or something.  For now, just emit them as external.
274    case GlobalValue::ExternalLinkage:
275     // If external or appending, declare as a global symbol
276     O << MAI->getGlobalDirective() << name << '\n';
277     // FALL THROUGH
278    case GlobalValue::PrivateLinkage:
279    case GlobalValue::LinkerPrivateLinkage:
280    case GlobalValue::InternalLinkage:
281     break;
282    case GlobalValue::GhostLinkage:
283     llvm_unreachable("Should not have any unmaterialized functions!");
284    case GlobalValue::DLLImportLinkage:
285     llvm_unreachable("DLLImport linkage is not supported by this target!");
286    case GlobalValue::DLLExportLinkage:
287     llvm_unreachable("DLLExport linkage is not supported by this target!");
288    default:
289     llvm_unreachable("Unknown linkage type!");
290   }
291
292   EmitAlignment(Align, GVar);
293
294   if (MAI->hasDotTypeDotSizeDirective()) {
295     O << "\t.type " << name << ",#object\n";
296     O << "\t.size " << name << ',' << Size << '\n';
297   }
298
299   O << name << ":\n";
300   EmitGlobalConstant(C);
301 }
302
303 /// PrintAsmOperand - Print out an operand for an inline asm expression.
304 ///
305 bool SparcAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
306                                       unsigned AsmVariant,
307                                       const char *ExtraCode) {
308   if (ExtraCode && ExtraCode[0]) {
309     if (ExtraCode[1] != 0) return true; // Unknown modifier.
310
311     switch (ExtraCode[0]) {
312     default: return true;  // Unknown modifier.
313     case 'r':
314      break;
315     }
316   }
317
318   printOperand(MI, OpNo);
319
320   return false;
321 }
322
323 bool SparcAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
324                                             unsigned OpNo,
325                                             unsigned AsmVariant,
326                                             const char *ExtraCode) {
327   if (ExtraCode && ExtraCode[0])
328     return true;  // Unknown modifier
329
330   O << '[';
331   printMemOperand(MI, OpNo);
332   O << ']';
333
334   return false;
335 }
336
337 // Force static initialization.
338 extern "C" void LLVMInitializeSparcAsmPrinter() { 
339   RegisterAsmPrinter<SparcAsmPrinter> X(TheSparcTarget);
340 }