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