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