remove DebugLoc from MCInst and eliminate "Comment printing" from
[oota-llvm.git] / lib / Target / SystemZ / AsmPrinter / SystemZAsmPrinter.cpp
1 //===-- SystemZAsmPrinter.cpp - SystemZ 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 the SystemZ assembly language.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #define DEBUG_TYPE "asm-printer"
16 #include "SystemZ.h"
17 #include "SystemZInstrInfo.h"
18 #include "SystemZTargetMachine.h"
19 #include "llvm/Constants.h"
20 #include "llvm/DerivedTypes.h"
21 #include "llvm/Module.h"
22 #include "llvm/Assembly/Writer.h"
23 #include "llvm/CodeGen/AsmPrinter.h"
24 #include "llvm/CodeGen/DwarfWriter.h"
25 #include "llvm/CodeGen/MachineModuleInfo.h"
26 #include "llvm/CodeGen/MachineFunctionPass.h"
27 #include "llvm/CodeGen/MachineConstantPool.h"
28 #include "llvm/CodeGen/MachineInstr.h"
29 #include "llvm/MC/MCStreamer.h"
30 #include "llvm/MC/MCAsmInfo.h"
31 #include "llvm/Target/TargetData.h"
32 #include "llvm/Target/TargetLoweringObjectFile.h"
33 #include "llvm/Target/TargetRegistry.h"
34 #include "llvm/ADT/Statistic.h"
35 #include "llvm/Support/Compiler.h"
36 #include "llvm/Support/FormattedStream.h"
37 #include "llvm/Support/Mangler.h"
38
39 using namespace llvm;
40
41 STATISTIC(EmittedInsts, "Number of machine instrs printed");
42
43 namespace {
44   class VISIBILITY_HIDDEN SystemZAsmPrinter : public AsmPrinter {
45   public:
46     SystemZAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
47                       const MCAsmInfo *MAI, bool V)
48       : AsmPrinter(O, TM, MAI, V) {}
49
50     virtual const char *getPassName() const {
51       return "SystemZ Assembly Printer";
52     }
53
54     void printOperand(const MachineInstr *MI, int OpNum,
55                       const char* Modifier = 0);
56     void printPCRelImmOperand(const MachineInstr *MI, int OpNum);
57     void printRIAddrOperand(const MachineInstr *MI, int OpNum,
58                             const char* Modifier = 0);
59     void printRRIAddrOperand(const MachineInstr *MI, int OpNum,
60                              const char* Modifier = 0);
61     void printS16ImmOperand(const MachineInstr *MI, int OpNum) {
62       O << (int16_t)MI->getOperand(OpNum).getImm();
63     }
64     void printS32ImmOperand(const MachineInstr *MI, int OpNum) {
65       O << (int32_t)MI->getOperand(OpNum).getImm();
66     }
67
68     void printInstruction(const MachineInstr *MI);  // autogenerated.
69     void printMachineInstruction(const MachineInstr * MI);
70
71     void emitFunctionHeader(const MachineFunction &MF);
72     bool runOnMachineFunction(MachineFunction &F);
73     void PrintGlobalVariable(const GlobalVariable* GVar);
74
75     void getAnalysisUsage(AnalysisUsage &AU) const {
76       AsmPrinter::getAnalysisUsage(AU);
77       AU.setPreservesAll();
78     }
79   };
80 } // end of anonymous namespace
81
82 #include "SystemZGenAsmWriter.inc"
83
84 void SystemZAsmPrinter::emitFunctionHeader(const MachineFunction &MF) {
85   unsigned FnAlign = MF.getAlignment();
86   const Function *F = MF.getFunction();
87
88   OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(F, Mang, TM));
89
90   EmitAlignment(FnAlign, F);
91
92   switch (F->getLinkage()) {
93   default: assert(0 && "Unknown linkage type!");
94   case Function::InternalLinkage:  // Symbols default to internal.
95   case Function::PrivateLinkage:
96   case Function::LinkerPrivateLinkage:
97     break;
98   case Function::ExternalLinkage:
99     O << "\t.globl\t" << CurrentFnName << '\n';
100     break;
101   case Function::LinkOnceAnyLinkage:
102   case Function::LinkOnceODRLinkage:
103   case Function::WeakAnyLinkage:
104   case Function::WeakODRLinkage:
105     O << "\t.weak\t" << CurrentFnName << '\n';
106     break;
107   }
108
109   printVisibility(CurrentFnName, F->getVisibility());
110
111   O << "\t.type\t" << CurrentFnName << ",@function\n"
112     << CurrentFnName << ":\n";
113 }
114
115 bool SystemZAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
116   SetupMachineFunction(MF);
117   O << "\n\n";
118
119   // Print out constants referenced by the function
120   EmitConstantPool(MF.getConstantPool());
121
122   // Print the 'header' of function
123   emitFunctionHeader(MF);
124
125   // Print out code for the function.
126   for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
127        I != E; ++I) {
128     // Print a label for the basic block.
129     if (!VerboseAsm && (I->pred_empty() || I->isOnlyReachableByFallthrough())) {
130       // This is an entry block or a block that's only reachable via a
131       // fallthrough edge. In non-VerboseAsm mode, don't print the label.
132     } else {
133       printBasicBlockLabel(I, true, true, VerboseAsm);
134       O << '\n';
135     }
136
137     for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
138          II != E; ++II)
139       // Print the assembly for the instruction.
140       printMachineInstruction(II);
141   }
142
143   if (MAI->hasDotTypeDotSizeDirective())
144     O << "\t.size\t" << CurrentFnName << ", .-" << CurrentFnName << '\n';
145
146   // Print out jump tables referenced by the function.
147   EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
148
149   // We didn't modify anything
150   return false;
151 }
152
153 void SystemZAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
154   ++EmittedInsts;
155
156   processDebugLoc(MI->getDebugLoc());
157
158   // Call the autogenerated instruction printer routines.
159   printInstruction(MI);
160   
161   if (VerboseAsm && !MI->getDebugLoc().isUnknown())
162     EmitComments(*MI);
163   O << '\n';
164 }
165
166 void SystemZAsmPrinter::printPCRelImmOperand(const MachineInstr *MI, int OpNum) {
167   const MachineOperand &MO = MI->getOperand(OpNum);
168   switch (MO.getType()) {
169   case MachineOperand::MO_Immediate:
170     O << MO.getImm();
171     return;
172   case MachineOperand::MO_MachineBasicBlock:
173     printBasicBlockLabel(MO.getMBB(), false, false, VerboseAsm);
174     return;
175   case MachineOperand::MO_GlobalAddress: {
176     const GlobalValue *GV = MO.getGlobal();
177     std::string Name = Mang->getMangledName(GV);
178
179     O << Name;
180
181     // Assemble calls via PLT for externally visible symbols if PIC.
182     if (TM.getRelocationModel() == Reloc::PIC_ &&
183         !GV->hasHiddenVisibility() && !GV->hasProtectedVisibility() &&
184         !GV->hasLocalLinkage())
185       O << "@PLT";
186
187     printOffset(MO.getOffset());
188     return;
189   }
190   case MachineOperand::MO_ExternalSymbol: {
191     std::string Name(MAI->getGlobalPrefix());
192     Name += MO.getSymbolName();
193     O << Name;
194
195     if (TM.getRelocationModel() == Reloc::PIC_)
196       O << "@PLT";
197
198     return;
199   }
200   default:
201     assert(0 && "Not implemented yet!");
202   }
203 }
204
205
206 void SystemZAsmPrinter::printOperand(const MachineInstr *MI, int OpNum,
207                                      const char* Modifier) {
208   const MachineOperand &MO = MI->getOperand(OpNum);
209   switch (MO.getType()) {
210   case MachineOperand::MO_Register: {
211     assert (TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
212             "Virtual registers should be already mapped!");
213     unsigned Reg = MO.getReg();
214     if (Modifier && strncmp(Modifier, "subreg", 6) == 0) {
215       if (strncmp(Modifier + 7, "even", 4) == 0)
216         Reg = TRI->getSubReg(Reg, SystemZ::SUBREG_EVEN);
217       else if (strncmp(Modifier + 7, "odd", 3) == 0)
218         Reg = TRI->getSubReg(Reg, SystemZ::SUBREG_ODD);
219       else
220         assert(0 && "Invalid subreg modifier");
221     }
222
223     O << '%' << TRI->getAsmName(Reg);
224     return;
225   }
226   case MachineOperand::MO_Immediate:
227     O << MO.getImm();
228     return;
229   case MachineOperand::MO_MachineBasicBlock:
230     printBasicBlockLabel(MO.getMBB());
231     return;
232   case MachineOperand::MO_JumpTableIndex:
233     O << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber() << '_'
234       << MO.getIndex();
235
236     return;
237   case MachineOperand::MO_ConstantPoolIndex:
238     O << MAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_'
239       << MO.getIndex();
240
241     printOffset(MO.getOffset());
242     break;
243   case MachineOperand::MO_GlobalAddress: {
244     const GlobalValue *GV = MO.getGlobal();
245     std::string Name = Mang->getMangledName(GV);
246
247     O << Name;
248     break;
249   }
250   case MachineOperand::MO_ExternalSymbol: {
251     std::string Name(MAI->getGlobalPrefix());
252     Name += MO.getSymbolName();
253     O << Name;
254     break;
255   }
256   default:
257     assert(0 && "Not implemented yet!");
258   }
259
260   switch (MO.getTargetFlags()) {
261   default:
262     llvm_unreachable("Unknown target flag on GV operand");
263   case SystemZII::MO_NO_FLAG:
264     break;
265   case SystemZII::MO_GOTENT:    O << "@GOTENT";    break;
266   case SystemZII::MO_PLT:       O << "@PLT";       break;
267   }
268
269   printOffset(MO.getOffset());
270 }
271
272 void SystemZAsmPrinter::printRIAddrOperand(const MachineInstr *MI, int OpNum,
273                                            const char* Modifier) {
274   const MachineOperand &Base = MI->getOperand(OpNum);
275
276   // Print displacement operand.
277   printOperand(MI, OpNum+1);
278
279   // Print base operand (if any)
280   if (Base.getReg()) {
281     O << '(';
282     printOperand(MI, OpNum);
283     O << ')';
284   }
285 }
286
287 void SystemZAsmPrinter::printRRIAddrOperand(const MachineInstr *MI, int OpNum,
288                                             const char* Modifier) {
289   const MachineOperand &Base = MI->getOperand(OpNum);
290   const MachineOperand &Index = MI->getOperand(OpNum+2);
291
292   // Print displacement operand.
293   printOperand(MI, OpNum+1);
294
295   // Print base operand (if any)
296   if (Base.getReg()) {
297     O << '(';
298     printOperand(MI, OpNum);
299     if (Index.getReg()) {
300       O << ',';
301       printOperand(MI, OpNum+2);
302     }
303     O << ')';
304   } else
305     assert(!Index.getReg() && "Should allocate base register first!");
306 }
307
308 void SystemZAsmPrinter::PrintGlobalVariable(const GlobalVariable* GVar) {
309   const TargetData *TD = TM.getTargetData();
310
311   if (!GVar->hasInitializer())
312     return;   // External global require no code
313
314   // Check to see if this is a special global used by LLVM, if so, emit it.
315   if (EmitSpecialLLVMGlobal(GVar))
316     return;
317
318   std::string name = Mang->getMangledName(GVar);
319   Constant *C = GVar->getInitializer();
320   unsigned Size = TD->getTypeAllocSize(C->getType());
321   unsigned Align = std::max(1U, TD->getPreferredAlignmentLog(GVar));
322
323   printVisibility(name, GVar->getVisibility());
324
325   O << "\t.type\t" << name << ",@object\n";
326
327   OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(GVar, Mang,
328                                                                   TM));
329
330   if (C->isNullValue() && !GVar->hasSection() &&
331       !GVar->isThreadLocal() &&
332       (GVar->hasLocalLinkage() || GVar->isWeakForLinker())) {
333
334     if (Size == 0) Size = 1;   // .comm Foo, 0 is undefined, avoid it.
335
336     if (GVar->hasLocalLinkage())
337       O << "\t.local\t" << name << '\n';
338
339     O << MAI->getCOMMDirective()  << name << ',' << Size;
340     if (MAI->getCOMMDirectiveTakesAlignment())
341       O << ',' << (MAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
342
343     if (VerboseAsm) {
344       O << "\t\t" << MAI->getCommentString() << ' ';
345       WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent());
346     }
347     O << '\n';
348     return;
349   }
350
351   switch (GVar->getLinkage()) {
352   case GlobalValue::CommonLinkage:
353   case GlobalValue::LinkOnceAnyLinkage:
354   case GlobalValue::LinkOnceODRLinkage:
355   case GlobalValue::WeakAnyLinkage:
356   case GlobalValue::WeakODRLinkage:
357     O << "\t.weak\t" << name << '\n';
358     break;
359   case GlobalValue::DLLExportLinkage:
360   case GlobalValue::AppendingLinkage:
361     // FIXME: appending linkage variables should go into a section of
362     // their name or something.  For now, just emit them as external.
363   case GlobalValue::ExternalLinkage:
364     // If external or appending, declare as a global symbol
365     O << "\t.globl " << name << '\n';
366     // FALL THROUGH
367   case GlobalValue::PrivateLinkage:
368   case GlobalValue::LinkerPrivateLinkage:
369   case GlobalValue::InternalLinkage:
370      break;
371   default:
372     assert(0 && "Unknown linkage type!");
373   }
374
375   // Use 16-bit alignment by default to simplify bunch of stuff
376   EmitAlignment(Align, GVar, 1);
377   O << name << ":";
378   if (VerboseAsm) {
379     O << "\t\t\t\t" << MAI->getCommentString() << ' ';
380     WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent());
381   }
382   O << '\n';
383   if (MAI->hasDotTypeDotSizeDirective())
384     O << "\t.size\t" << name << ", " << Size << '\n';
385
386   EmitGlobalConstant(C);
387 }
388
389 // Force static initialization.
390 extern "C" void LLVMInitializeSystemZAsmPrinter() {
391   RegisterAsmPrinter<SystemZAsmPrinter> X(TheSystemZTarget);
392 }