Detemplatize the Statistic class. The only type it is instantiated with
[oota-llvm.git] / lib / Target / ARM / ARMAsmPrinter.cpp
1 //===-- ARMAsmPrinter.cpp - ARM LLVM assembly writer ----------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the "Instituto Nokia de Tecnologia" and
6 // is distributed under the University of Illinois Open Source
7 // License. See LICENSE.TXT for details.
8 //
9 //===----------------------------------------------------------------------===//
10 //
11 // This file contains a printer that converts from our internal representation
12 // of machine-dependent LLVM code to GAS-format ARM assembly language.
13 //
14 //===----------------------------------------------------------------------===//
15
16 #include "ARM.h"
17 #include "ARMInstrInfo.h"
18 #include "llvm/Constants.h"
19 #include "llvm/DerivedTypes.h"
20 #include "llvm/Module.h"
21 #include "llvm/CodeGen/AsmPrinter.h"
22 #include "llvm/CodeGen/MachineFunctionPass.h"
23 #include "llvm/CodeGen/MachineConstantPool.h"
24 #include "llvm/CodeGen/MachineInstr.h"
25 #include "llvm/Target/TargetAsmInfo.h"
26 #include "llvm/Target/TargetData.h"
27 #include "llvm/Target/TargetMachine.h"
28 #include "llvm/Support/Mangler.h"
29 #include "llvm/ADT/Statistic.h"
30 #include "llvm/ADT/StringExtras.h"
31 #include "llvm/Support/CommandLine.h"
32 #include "llvm/Support/MathExtras.h"
33 #include <cctype>
34 #include <iostream>
35 using namespace llvm;
36
37 namespace {
38   Statistic EmittedInsts("asm-printer", "Number of machine instrs printed");
39
40   static const char *ARMCondCodeToString(ARMCC::CondCodes CC) {
41     switch (CC) {
42     default: assert(0 && "Unknown condition code");
43     case ARMCC::EQ:  return "eq";
44     case ARMCC::NE:  return "ne";
45     case ARMCC::CS:  return "cs";
46     case ARMCC::CC:  return "cc";
47     case ARMCC::MI:  return "mi";
48     case ARMCC::PL:  return "pl";
49     case ARMCC::VS:  return "vs";
50     case ARMCC::VC:  return "vc";
51     case ARMCC::HI:  return "hi";
52     case ARMCC::LS:  return "ls";
53     case ARMCC::GE:  return "ge";
54     case ARMCC::LT:  return "lt";
55     case ARMCC::GT:  return "gt";
56     case ARMCC::LE:  return "le";
57     case ARMCC::AL:  return "al";
58     }
59   }
60
61   struct VISIBILITY_HIDDEN ARMAsmPrinter : public AsmPrinter {
62     ARMAsmPrinter(std::ostream &O, TargetMachine &TM, const TargetAsmInfo *T)
63       : AsmPrinter(O, TM, T) {
64     }
65
66     std::set<std::string> ExtWeakSymbols;
67
68     /// We name each basic block in a Function with a unique number, so
69     /// that we can consistently refer to them later. This is cleared
70     /// at the beginning of each call to runOnMachineFunction().
71     ///
72     typedef std::map<const Value *, unsigned> ValueMapTy;
73     ValueMapTy NumberForBB;
74
75     virtual const char *getPassName() const {
76       return "ARM Assembly Printer";
77     }
78
79     void printAddrMode1(const MachineInstr *MI, int opNum);
80     void printAddrMode2(const MachineInstr *MI, int opNum);
81     void printAddrMode5(const MachineInstr *MI, int opNum);
82     void printOperand(const MachineInstr *MI, int opNum);
83     void printMemOperand(const MachineInstr *MI, int opNum,
84                          const char *Modifier = 0);
85     void printCCOperand(const MachineInstr *MI, int opNum);
86
87     bool printInstruction(const MachineInstr *MI);  // autogenerated.
88     bool runOnMachineFunction(MachineFunction &F);
89     bool doInitialization(Module &M);
90     bool doFinalization(Module &M);
91   };
92 } // end of anonymous namespace
93
94 #include "ARMGenAsmWriter.inc"
95
96 /// createARMCodePrinterPass - Returns a pass that prints the ARM
97 /// assembly code for a MachineFunction to the given output stream,
98 /// using the given target machine description.  This should work
99 /// regardless of whether the function is in SSA form.
100 ///
101 FunctionPass *llvm::createARMCodePrinterPass(std::ostream &o,
102                                                TargetMachine &tm) {
103   return new ARMAsmPrinter(o, tm, tm.getTargetAsmInfo());
104 }
105
106 /// runOnMachineFunction - This uses the printMachineInstruction()
107 /// method to print assembly for each instruction.
108 ///
109 bool ARMAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
110   SetupMachineFunction(MF);
111   O << "\n\n";
112
113   // Print out constants referenced by the function
114   EmitConstantPool(MF.getConstantPool());
115
116   // Print out jump tables referenced by the function
117   EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
118
119   // Print out labels for the function.
120   const Function *F = MF.getFunction();
121   SwitchToTextSection(getSectionForFunction(*F).c_str(), F);
122
123   switch (F->getLinkage()) {
124   default: assert(0 && "Unknown linkage type!");
125   case Function::InternalLinkage:
126     break;
127   case Function::ExternalLinkage:
128     O << "\t.globl\t" << CurrentFnName << "\n";
129     break;
130   case Function::WeakLinkage:
131   case Function::LinkOnceLinkage:
132     O << TAI->getWeakRefDirective() << CurrentFnName << "\n";
133     break;
134   }
135   EmitAlignment(2, F);
136   O << CurrentFnName << ":\n";
137
138   // Print out code for the function.
139   for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
140        I != E; ++I) {
141     // Print a label for the basic block.
142     if (I != MF.begin()) {
143       printBasicBlockLabel(I, true);
144       O << '\n';
145     }
146     for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
147          II != E; ++II) {
148       // Print the assembly for the instruction.
149       O << "\t";
150       printInstruction(II);
151     }
152   }
153
154   return false;
155 }
156
157 void ARMAsmPrinter::printAddrMode1(const MachineInstr *MI, int opNum) {
158   const MachineOperand &Arg       = MI->getOperand(opNum);
159   const MachineOperand &Shift     = MI->getOperand(opNum + 1);
160   const MachineOperand &ShiftType = MI->getOperand(opNum + 2);
161
162   if(Arg.isImmediate()) {
163     assert(Shift.getImmedValue() == 0);
164     printOperand(MI, opNum);
165   } else {
166     assert(Arg.isRegister());
167     printOperand(MI, opNum);
168     if(Shift.isRegister() || Shift.getImmedValue() != 0) {
169       const char *s = NULL;
170       switch(ShiftType.getImmedValue()) {
171       case ARMShift::LSL:
172         s = ", lsl ";
173         break;
174       case ARMShift::LSR:
175         s = ", lsr ";
176         break;
177       case ARMShift::ASR:
178         s = ", asr ";
179         break;
180       case ARMShift::ROR:
181         s = ", ror ";
182         break;
183       case ARMShift::RRX:
184         s = ", rrx ";
185         break;
186       }
187       O << s;
188       printOperand(MI, opNum + 1);
189     }
190   }
191 }
192
193 void ARMAsmPrinter::printAddrMode2(const MachineInstr *MI, int opNum) {
194   const MachineOperand &Arg    = MI->getOperand(opNum);
195   const MachineOperand &Offset = MI->getOperand(opNum + 1);
196   assert(Offset.isImmediate());
197
198   if (Arg.isConstantPoolIndex()) {
199     assert(Offset.getImmedValue() == 0);
200     printOperand(MI, opNum);
201   } else {
202     assert(Arg.isRegister());
203     O << '[';
204     printOperand(MI, opNum);
205     O << ", ";
206     printOperand(MI, opNum + 1);
207     O << ']';
208   }
209 }
210
211 void ARMAsmPrinter::printAddrMode5(const MachineInstr *MI, int opNum) {
212   const MachineOperand &Arg    = MI->getOperand(opNum);
213   const MachineOperand &Offset = MI->getOperand(opNum + 1);
214   assert(Offset.isImmediate());
215
216   if (Arg.isConstantPoolIndex()) {
217     assert(Offset.getImmedValue() == 0);
218     printOperand(MI, opNum);
219   } else {
220     assert(Arg.isRegister());
221     O << '[';
222     printOperand(MI, opNum);
223     O << ", ";
224     printOperand(MI, opNum + 1);
225     O << ']';
226   }
227 }
228
229 void ARMAsmPrinter::printOperand(const MachineInstr *MI, int opNum) {
230   const MachineOperand &MO = MI->getOperand (opNum);
231   const MRegisterInfo &RI = *TM.getRegisterInfo();
232   switch (MO.getType()) {
233   case MachineOperand::MO_Register:
234     if (MRegisterInfo::isPhysicalRegister(MO.getReg()))
235       O << LowercaseString (RI.get(MO.getReg()).Name);
236     else
237       assert(0 && "not implemented");
238     break;
239   case MachineOperand::MO_Immediate:
240     O << "#" << (int)MO.getImmedValue();
241     break;
242   case MachineOperand::MO_MachineBasicBlock:
243     printBasicBlockLabel(MO.getMachineBasicBlock());
244     return;
245   case MachineOperand::MO_GlobalAddress: {
246     GlobalValue *GV = MO.getGlobal();
247     std::string Name = Mang->getValueName(GV);
248     O << Name;
249     if (GV->hasExternalWeakLinkage()) {
250       ExtWeakSymbols.insert(Name);
251     }
252   }
253     break;
254   case MachineOperand::MO_ExternalSymbol:
255     O << TAI->getGlobalPrefix() << MO.getSymbolName();
256     break;
257   case MachineOperand::MO_ConstantPoolIndex:
258     O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber()
259       << '_' << MO.getConstantPoolIndex();
260     break;
261   default:
262     O << "<unknown operand type>"; abort (); break;
263   }
264 }
265
266 void ARMAsmPrinter::printMemOperand(const MachineInstr *MI, int opNum,
267                                       const char *Modifier) {
268   assert(0 && "not implemented");
269 }
270
271 void ARMAsmPrinter::printCCOperand(const MachineInstr *MI, int opNum) {
272   int CC = (int)MI->getOperand(opNum).getImmedValue();
273   O << ARMCondCodeToString((ARMCC::CondCodes)CC);
274 }
275
276 bool ARMAsmPrinter::doInitialization(Module &M) {
277   AsmPrinter::doInitialization(M);
278   return false; // success
279 }
280
281 bool ARMAsmPrinter::doFinalization(Module &M) {
282   const TargetData *TD = TM.getTargetData();
283
284   for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
285        I != E; ++I) {
286     if (!I->hasInitializer())   // External global require no code
287       continue;
288
289     if (EmitSpecialLLVMGlobal(I))
290       continue;
291
292     O << "\n\n";
293     std::string name = Mang->getValueName(I);
294     Constant *C = I->getInitializer();
295     unsigned Size = TD->getTypeSize(C->getType());
296     unsigned Align = TD->getTypeAlignment(C->getType());
297
298     if (C->isNullValue() &&
299         (I->hasLinkOnceLinkage() || I->hasInternalLinkage() ||
300          I->hasWeakLinkage())) {
301       SwitchToDataSection(".data", I);
302       if (I->hasInternalLinkage())
303         O << "\t.local " << name << "\n";
304
305       O << "\t.comm " << name << "," << TD->getTypeSize(C->getType())
306         << "," << (unsigned)TD->getTypeAlignment(C->getType());
307       O << "\n";
308     } else {
309       switch (I->getLinkage()) {
310       default:
311         assert(0 && "Unknown linkage type!");
312         break;
313       case GlobalValue::ExternalLinkage:
314         O << "\t.globl " << name << "\n";
315         break;
316       case GlobalValue::InternalLinkage:
317         break;
318       }
319
320       if (C->isNullValue())
321         SwitchToDataSection(".bss",  I);
322       else
323         SwitchToDataSection(".data", I);
324
325       EmitAlignment(Align, I);
326       O << "\t.type " << name << ", %object\n";
327       O << "\t.size " << name << ", " << Size << "\n";
328       O << name << ":\n";
329       EmitGlobalConstant(C);
330     }
331   }
332
333   if (ExtWeakSymbols.begin() != ExtWeakSymbols.end())
334     SwitchToDataSection("");
335   for (std::set<std::string>::iterator i = ExtWeakSymbols.begin(),
336          e = ExtWeakSymbols.end(); i != e; ++i) {
337     O << TAI->getWeakRefDirective() << *i << "\n";
338   }
339
340   AsmPrinter::doFinalization(M);
341   return false; // success
342 }