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