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