Rename TargetAsmInfo (and its subclasses) to MCAsmInfo.
[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 *TAI, bool V)
48       : AsmPrinter(O, TM, TAI, 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 (TAI->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   // Call the autogenerated instruction printer routines.
157   printInstruction(MI);
158 }
159
160 void SystemZAsmPrinter::printPCRelImmOperand(const MachineInstr *MI, int OpNum) {
161   const MachineOperand &MO = MI->getOperand(OpNum);
162   switch (MO.getType()) {
163   case MachineOperand::MO_Immediate:
164     O << MO.getImm();
165     return;
166   case MachineOperand::MO_MachineBasicBlock:
167     printBasicBlockLabel(MO.getMBB(), false, false, VerboseAsm);
168     return;
169   case MachineOperand::MO_GlobalAddress: {
170     const GlobalValue *GV = MO.getGlobal();
171     std::string Name = Mang->getMangledName(GV);
172
173     O << Name;
174
175     // Assemble calls via PLT for externally visible symbols if PIC.
176     if (TM.getRelocationModel() == Reloc::PIC_ &&
177         !GV->hasHiddenVisibility() && !GV->hasProtectedVisibility() &&
178         !GV->hasLocalLinkage())
179       O << "@PLT";
180
181     printOffset(MO.getOffset());
182     return;
183   }
184   case MachineOperand::MO_ExternalSymbol: {
185     std::string Name(TAI->getGlobalPrefix());
186     Name += MO.getSymbolName();
187     O << Name;
188
189     if (TM.getRelocationModel() == Reloc::PIC_)
190       O << "@PLT";
191
192     return;
193   }
194   default:
195     assert(0 && "Not implemented yet!");
196   }
197 }
198
199
200 void SystemZAsmPrinter::printOperand(const MachineInstr *MI, int OpNum,
201                                      const char* Modifier) {
202   const MachineOperand &MO = MI->getOperand(OpNum);
203   switch (MO.getType()) {
204   case MachineOperand::MO_Register: {
205     assert (TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
206             "Virtual registers should be already mapped!");
207     unsigned Reg = MO.getReg();
208     if (Modifier && strncmp(Modifier, "subreg", 6) == 0) {
209       if (strncmp(Modifier + 7, "even", 4) == 0)
210         Reg = TRI->getSubReg(Reg, SystemZ::SUBREG_EVEN);
211       else if (strncmp(Modifier + 7, "odd", 3) == 0)
212         Reg = TRI->getSubReg(Reg, SystemZ::SUBREG_ODD);
213       else
214         assert(0 && "Invalid subreg modifier");
215     }
216
217     O << '%' << TRI->getAsmName(Reg);
218     return;
219   }
220   case MachineOperand::MO_Immediate:
221     O << MO.getImm();
222     return;
223   case MachineOperand::MO_MachineBasicBlock:
224     printBasicBlockLabel(MO.getMBB());
225     return;
226   case MachineOperand::MO_JumpTableIndex:
227     O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber() << '_'
228       << MO.getIndex();
229
230     return;
231   case MachineOperand::MO_ConstantPoolIndex:
232     O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_'
233       << MO.getIndex();
234
235     printOffset(MO.getOffset());
236     break;
237   case MachineOperand::MO_GlobalAddress: {
238     const GlobalValue *GV = MO.getGlobal();
239     std::string Name = Mang->getMangledName(GV);
240
241     O << Name;
242     break;
243   }
244   case MachineOperand::MO_ExternalSymbol: {
245     std::string Name(TAI->getGlobalPrefix());
246     Name += MO.getSymbolName();
247     O << Name;
248     break;
249   }
250   default:
251     assert(0 && "Not implemented yet!");
252   }
253
254   switch (MO.getTargetFlags()) {
255   default:
256     llvm_unreachable("Unknown target flag on GV operand");
257   case SystemZII::MO_NO_FLAG:
258     break;
259   case SystemZII::MO_GOTENT:    O << "@GOTENT";    break;
260   case SystemZII::MO_PLT:       O << "@PLT";       break;
261   }
262
263   printOffset(MO.getOffset());
264 }
265
266 void SystemZAsmPrinter::printRIAddrOperand(const MachineInstr *MI, int OpNum,
267                                            const char* Modifier) {
268   const MachineOperand &Base = MI->getOperand(OpNum);
269
270   // Print displacement operand.
271   printOperand(MI, OpNum+1);
272
273   // Print base operand (if any)
274   if (Base.getReg()) {
275     O << '(';
276     printOperand(MI, OpNum);
277     O << ')';
278   }
279 }
280
281 void SystemZAsmPrinter::printRRIAddrOperand(const MachineInstr *MI, int OpNum,
282                                             const char* Modifier) {
283   const MachineOperand &Base = MI->getOperand(OpNum);
284   const MachineOperand &Index = MI->getOperand(OpNum+2);
285
286   // Print displacement operand.
287   printOperand(MI, OpNum+1);
288
289   // Print base operand (if any)
290   if (Base.getReg()) {
291     O << '(';
292     printOperand(MI, OpNum);
293     if (Index.getReg()) {
294       O << ',';
295       printOperand(MI, OpNum+2);
296     }
297     O << ')';
298   } else
299     assert(!Index.getReg() && "Should allocate base register first!");
300 }
301
302 void SystemZAsmPrinter::PrintGlobalVariable(const GlobalVariable* GVar) {
303   const TargetData *TD = TM.getTargetData();
304
305   if (!GVar->hasInitializer())
306     return;   // External global require no code
307
308   // Check to see if this is a special global used by LLVM, if so, emit it.
309   if (EmitSpecialLLVMGlobal(GVar))
310     return;
311
312   std::string name = Mang->getMangledName(GVar);
313   Constant *C = GVar->getInitializer();
314   unsigned Size = TD->getTypeAllocSize(C->getType());
315   unsigned Align = std::max(1U, TD->getPreferredAlignmentLog(GVar));
316
317   printVisibility(name, GVar->getVisibility());
318
319   O << "\t.type\t" << name << ",@object\n";
320
321   OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(GVar, Mang,
322                                                                   TM));
323
324   if (C->isNullValue() && !GVar->hasSection() &&
325       !GVar->isThreadLocal() &&
326       (GVar->hasLocalLinkage() || GVar->isWeakForLinker())) {
327
328     if (Size == 0) Size = 1;   // .comm Foo, 0 is undefined, avoid it.
329
330     if (GVar->hasLocalLinkage())
331       O << "\t.local\t" << name << '\n';
332
333     O << TAI->getCOMMDirective()  << name << ',' << Size;
334     if (TAI->getCOMMDirectiveTakesAlignment())
335       O << ',' << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
336
337     if (VerboseAsm) {
338       O << "\t\t" << TAI->getCommentString() << ' ';
339       WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent());
340     }
341     O << '\n';
342     return;
343   }
344
345   switch (GVar->getLinkage()) {
346   case GlobalValue::CommonLinkage:
347   case GlobalValue::LinkOnceAnyLinkage:
348   case GlobalValue::LinkOnceODRLinkage:
349   case GlobalValue::WeakAnyLinkage:
350   case GlobalValue::WeakODRLinkage:
351     O << "\t.weak\t" << name << '\n';
352     break;
353   case GlobalValue::DLLExportLinkage:
354   case GlobalValue::AppendingLinkage:
355     // FIXME: appending linkage variables should go into a section of
356     // their name or something.  For now, just emit them as external.
357   case GlobalValue::ExternalLinkage:
358     // If external or appending, declare as a global symbol
359     O << "\t.globl " << name << '\n';
360     // FALL THROUGH
361   case GlobalValue::PrivateLinkage:
362   case GlobalValue::LinkerPrivateLinkage:
363   case GlobalValue::InternalLinkage:
364      break;
365   default:
366     assert(0 && "Unknown linkage type!");
367   }
368
369   // Use 16-bit alignment by default to simplify bunch of stuff
370   EmitAlignment(Align, GVar, 1);
371   O << name << ":";
372   if (VerboseAsm) {
373     O << "\t\t\t\t" << TAI->getCommentString() << ' ';
374     WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent());
375   }
376   O << '\n';
377   if (TAI->hasDotTypeDotSizeDirective())
378     O << "\t.size\t" << name << ", " << Size << '\n';
379
380   EmitGlobalConstant(C);
381 }
382
383 // Force static initialization.
384 extern "C" void LLVMInitializeSystemZAsmPrinter() {
385   RegisterAsmPrinter<SystemZAsmPrinter> X(TheSystemZTarget);
386 }