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