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