Remove the argument from EmitJumpTableInfo, because it doesn't need it.
[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     bool runOnMachineFunction(MachineFunction &F);
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
84
85 bool SystemZAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
86   SetupMachineFunction(MF);
87   O << "\n\n";
88
89   // Print the 'header' of function
90   EmitFunctionHeader();
91
92   // Print out code for the function.
93   for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
94        I != E; ++I) {
95     // Print a label for the basic block.
96     EmitBasicBlockStart(I);
97
98     for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
99          II != E; ++II)
100       // Print the assembly for the instruction.
101       printMachineInstruction(II);
102   }
103
104   if (MAI->hasDotTypeDotSizeDirective())
105     O << "\t.size\t" << *CurrentFnSym << ", .-" << *CurrentFnSym << '\n';
106
107   // Print out jump tables referenced by the function.
108   EmitJumpTableInfo();
109
110   // We didn't modify anything
111   return false;
112 }
113
114 void SystemZAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
115   ++EmittedInsts;
116
117   processDebugLoc(MI, true);
118
119   // Call the autogenerated instruction printer routines.
120   printInstruction(MI);
121   
122   if (VerboseAsm)
123     EmitComments(*MI);
124   O << '\n';
125
126   processDebugLoc(MI, false);
127 }
128
129 void SystemZAsmPrinter::printPCRelImmOperand(const MachineInstr *MI, int OpNum){
130   const MachineOperand &MO = MI->getOperand(OpNum);
131   switch (MO.getType()) {
132   case MachineOperand::MO_Immediate:
133     O << MO.getImm();
134     return;
135   case MachineOperand::MO_MachineBasicBlock:
136     O << *MO.getMBB()->getSymbol(OutContext);
137     return;
138   case MachineOperand::MO_GlobalAddress: {
139     const GlobalValue *GV = MO.getGlobal();
140     O << *GetGlobalValueSymbol(GV);
141
142     // Assemble calls via PLT for externally visible symbols if PIC.
143     if (TM.getRelocationModel() == Reloc::PIC_ &&
144         !GV->hasHiddenVisibility() && !GV->hasProtectedVisibility() &&
145         !GV->hasLocalLinkage())
146       O << "@PLT";
147
148     printOffset(MO.getOffset());
149     return;
150   }
151   case MachineOperand::MO_ExternalSymbol: {
152     std::string Name(MAI->getGlobalPrefix());
153     Name += MO.getSymbolName();
154     O << Name;
155
156     if (TM.getRelocationModel() == Reloc::PIC_)
157       O << "@PLT";
158
159     return;
160   }
161   default:
162     assert(0 && "Not implemented yet!");
163   }
164 }
165
166
167 void SystemZAsmPrinter::printOperand(const MachineInstr *MI, int OpNum,
168                                      const char* Modifier) {
169   const MachineOperand &MO = MI->getOperand(OpNum);
170   switch (MO.getType()) {
171   case MachineOperand::MO_Register: {
172     assert (TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
173             "Virtual registers should be already mapped!");
174     unsigned Reg = MO.getReg();
175     if (Modifier && strncmp(Modifier, "subreg", 6) == 0) {
176       if (strncmp(Modifier + 7, "even", 4) == 0)
177         Reg = TRI->getSubReg(Reg, SystemZ::SUBREG_EVEN);
178       else if (strncmp(Modifier + 7, "odd", 3) == 0)
179         Reg = TRI->getSubReg(Reg, SystemZ::SUBREG_ODD);
180       else
181         assert(0 && "Invalid subreg modifier");
182     }
183
184     O << '%' << getRegisterName(Reg);
185     return;
186   }
187   case MachineOperand::MO_Immediate:
188     O << MO.getImm();
189     return;
190   case MachineOperand::MO_MachineBasicBlock:
191     O << *MO.getMBB()->getSymbol(OutContext);
192     return;
193   case MachineOperand::MO_JumpTableIndex:
194     O << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber() << '_'
195       << MO.getIndex();
196
197     return;
198   case MachineOperand::MO_ConstantPoolIndex:
199     O << MAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_'
200       << MO.getIndex();
201
202     printOffset(MO.getOffset());
203     break;
204   case MachineOperand::MO_GlobalAddress:
205     O << *GetGlobalValueSymbol(MO.getGlobal());
206     break;
207   case MachineOperand::MO_ExternalSymbol: {
208     O << *GetExternalSymbolSymbol(MO.getSymbolName());
209     break;
210   }
211   default:
212     assert(0 && "Not implemented yet!");
213   }
214
215   switch (MO.getTargetFlags()) {
216   default:
217     llvm_unreachable("Unknown target flag on GV operand");
218   case SystemZII::MO_NO_FLAG:
219     break;
220   case SystemZII::MO_GOTENT:    O << "@GOTENT";    break;
221   case SystemZII::MO_PLT:       O << "@PLT";       break;
222   }
223
224   printOffset(MO.getOffset());
225 }
226
227 void SystemZAsmPrinter::printRIAddrOperand(const MachineInstr *MI, int OpNum,
228                                            const char* Modifier) {
229   const MachineOperand &Base = MI->getOperand(OpNum);
230
231   // Print displacement operand.
232   printOperand(MI, OpNum+1);
233
234   // Print base operand (if any)
235   if (Base.getReg()) {
236     O << '(';
237     printOperand(MI, OpNum);
238     O << ')';
239   }
240 }
241
242 void SystemZAsmPrinter::printRRIAddrOperand(const MachineInstr *MI, int OpNum,
243                                             const char* Modifier) {
244   const MachineOperand &Base = MI->getOperand(OpNum);
245   const MachineOperand &Index = MI->getOperand(OpNum+2);
246
247   // Print displacement operand.
248   printOperand(MI, OpNum+1);
249
250   // Print base operand (if any)
251   if (Base.getReg()) {
252     O << '(';
253     printOperand(MI, OpNum);
254     if (Index.getReg()) {
255       O << ',';
256       printOperand(MI, OpNum+2);
257     }
258     O << ')';
259   } else
260     assert(!Index.getReg() && "Should allocate base register first!");
261 }
262
263 // Force static initialization.
264 extern "C" void LLVMInitializeSystemZAsmPrinter() {
265   RegisterAsmPrinter<SystemZAsmPrinter> X(TheSystemZTarget);
266 }