add a new MachineBasicBlock::getSymbol method, replacing
[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
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" << *CurrentFnSym << '\n';
100     break;
101   case Function::LinkOnceAnyLinkage:
102   case Function::LinkOnceODRLinkage:
103   case Function::WeakAnyLinkage:
104   case Function::WeakODRLinkage:
105     O << "\t.weak\t" << *CurrentFnSym << '\n';
106     break;
107   }
108
109   printVisibility(CurrentFnSym, F->getVisibility());
110
111   O << "\t.type\t" << *CurrentFnSym << ",@function\n";
112   O << *CurrentFnSym << ":\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     EmitBasicBlockStart(I);
130
131     for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
132          II != E; ++II)
133       // Print the assembly for the instruction.
134       printMachineInstruction(II);
135   }
136
137   if (MAI->hasDotTypeDotSizeDirective())
138     O << "\t.size\t" << *CurrentFnSym << ", .-" << *CurrentFnSym << '\n';
139
140   // Print out jump tables referenced by the function.
141   EmitJumpTableInfo(MF);
142
143   // We didn't modify anything
144   return false;
145 }
146
147 void SystemZAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
148   ++EmittedInsts;
149
150   processDebugLoc(MI, true);
151
152   // Call the autogenerated instruction printer routines.
153   printInstruction(MI);
154   
155   if (VerboseAsm)
156     EmitComments(*MI);
157   O << '\n';
158
159   processDebugLoc(MI, false);
160 }
161
162 void SystemZAsmPrinter::printPCRelImmOperand(const MachineInstr *MI, int OpNum){
163   const MachineOperand &MO = MI->getOperand(OpNum);
164   switch (MO.getType()) {
165   case MachineOperand::MO_Immediate:
166     O << MO.getImm();
167     return;
168   case MachineOperand::MO_MachineBasicBlock:
169     O << *MO.getMBB()->getSymbol(OutContext);
170     return;
171   case MachineOperand::MO_GlobalAddress: {
172     const GlobalValue *GV = MO.getGlobal();
173     O << *GetGlobalValueSymbol(GV);
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(MAI->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 << '%' << getRegisterName(Reg);
218     return;
219   }
220   case MachineOperand::MO_Immediate:
221     O << MO.getImm();
222     return;
223   case MachineOperand::MO_MachineBasicBlock:
224     O << *MO.getMBB()->getSymbol(OutContext);
225     return;
226   case MachineOperand::MO_JumpTableIndex:
227     O << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber() << '_'
228       << MO.getIndex();
229
230     return;
231   case MachineOperand::MO_ConstantPoolIndex:
232     O << MAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_'
233       << MO.getIndex();
234
235     printOffset(MO.getOffset());
236     break;
237   case MachineOperand::MO_GlobalAddress:
238     O << *GetGlobalValueSymbol(MO.getGlobal());
239     break;
240   case MachineOperand::MO_ExternalSymbol: {
241     O << *GetExternalSymbolSymbol(MO.getSymbolName());
242     break;
243   }
244   default:
245     assert(0 && "Not implemented yet!");
246   }
247
248   switch (MO.getTargetFlags()) {
249   default:
250     llvm_unreachable("Unknown target flag on GV operand");
251   case SystemZII::MO_NO_FLAG:
252     break;
253   case SystemZII::MO_GOTENT:    O << "@GOTENT";    break;
254   case SystemZII::MO_PLT:       O << "@PLT";       break;
255   }
256
257   printOffset(MO.getOffset());
258 }
259
260 void SystemZAsmPrinter::printRIAddrOperand(const MachineInstr *MI, int OpNum,
261                                            const char* Modifier) {
262   const MachineOperand &Base = MI->getOperand(OpNum);
263
264   // Print displacement operand.
265   printOperand(MI, OpNum+1);
266
267   // Print base operand (if any)
268   if (Base.getReg()) {
269     O << '(';
270     printOperand(MI, OpNum);
271     O << ')';
272   }
273 }
274
275 void SystemZAsmPrinter::printRRIAddrOperand(const MachineInstr *MI, int OpNum,
276                                             const char* Modifier) {
277   const MachineOperand &Base = MI->getOperand(OpNum);
278   const MachineOperand &Index = MI->getOperand(OpNum+2);
279
280   // Print displacement operand.
281   printOperand(MI, OpNum+1);
282
283   // Print base operand (if any)
284   if (Base.getReg()) {
285     O << '(';
286     printOperand(MI, OpNum);
287     if (Index.getReg()) {
288       O << ',';
289       printOperand(MI, OpNum+2);
290     }
291     O << ')';
292   } else
293     assert(!Index.getReg() && "Should allocate base register first!");
294 }
295
296 // Force static initialization.
297 extern "C" void LLVMInitializeSystemZAsmPrinter() {
298   RegisterAsmPrinter<SystemZAsmPrinter> X(TheSystemZTarget);
299 }