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