Fix for ARM weak symbols, patch by Lauro Ramos Venancio!
[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 #define DEBUG_TYPE "asm-printer"
17 #include "ARM.h"
18 #include "ARMInstrInfo.h"
19 #include "llvm/Constants.h"
20 #include "llvm/DerivedTypes.h"
21 #include "llvm/Module.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 using namespace llvm;
36
37 STATISTIC(EmittedInsts, "Number of machine instrs printed");
38
39 namespace {
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     /// We name each basic block in a Function with a unique number, so
67     /// that we can consistently refer to them later. This is cleared
68     /// at the beginning of each call to runOnMachineFunction().
69     ///
70     typedef std::map<const Value *, unsigned> ValueMapTy;
71     ValueMapTy NumberForBB;
72
73     virtual const char *getPassName() const {
74       return "ARM Assembly Printer";
75     }
76
77     void printAddrMode1(const MachineInstr *MI, int opNum);
78     void printAddrMode2(const MachineInstr *MI, int opNum);
79     void printAddrMode5(const MachineInstr *MI, int opNum);
80     void printOperand(const MachineInstr *MI, int opNum);
81     void printMemOperand(const MachineInstr *MI, int opNum,
82                          const char *Modifier = 0);
83     void printCCOperand(const MachineInstr *MI, int opNum);
84
85     bool printInstruction(const MachineInstr *MI);  // autogenerated.
86     bool runOnMachineFunction(MachineFunction &F);
87     bool doInitialization(Module &M);
88     bool doFinalization(Module &M);
89   };
90 } // end of anonymous namespace
91
92 #include "ARMGenAsmWriter.inc"
93
94 /// createARMCodePrinterPass - Returns a pass that prints the ARM
95 /// assembly code for a MachineFunction to the given output stream,
96 /// using the given target machine description.  This should work
97 /// regardless of whether the function is in SSA form.
98 ///
99 FunctionPass *llvm::createARMCodePrinterPass(std::ostream &o,
100                                                TargetMachine &tm) {
101   return new ARMAsmPrinter(o, tm, tm.getTargetAsmInfo());
102 }
103
104 /// runOnMachineFunction - This uses the printMachineInstruction()
105 /// method to print assembly for each instruction.
106 ///
107 bool ARMAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
108   SetupMachineFunction(MF);
109   O << "\n\n";
110
111   // Print out constants referenced by the function
112   EmitConstantPool(MF.getConstantPool());
113
114   const std::vector<MachineConstantPoolEntry>
115     &CP = MF.getConstantPool()->getConstants();
116   for (unsigned i = 0, e = CP.size(); i != e; ++i) {
117     MachineConstantPoolEntry CPE = CP[i];
118     if (!CPE.isMachineConstantPoolEntry()){
119       Constant *CV = CPE.Val.ConstVal;
120       if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV)) {
121         if (GV->hasExternalWeakLinkage()) {
122           ExtWeakSymbols.insert(GV);
123         }
124       }
125     }
126   }
127
128   // Print out jump tables referenced by the function
129   EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
130
131   // Print out labels for the function.
132   const Function *F = MF.getFunction();
133   SwitchToTextSection(getSectionForFunction(*F).c_str(), F);
134
135   switch (F->getLinkage()) {
136   default: assert(0 && "Unknown linkage type!");
137   case Function::InternalLinkage:
138     break;
139   case Function::ExternalLinkage:
140     O << "\t.globl\t" << CurrentFnName << "\n";
141     break;
142   case Function::WeakLinkage:
143   case Function::LinkOnceLinkage:
144     O << TAI->getWeakRefDirective() << CurrentFnName << "\n";
145     break;
146   }
147   EmitAlignment(2, F);
148   O << CurrentFnName << ":\n";
149
150   // Print out code for the function.
151   for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
152        I != E; ++I) {
153     // Print a label for the basic block.
154     if (I != MF.begin()) {
155       printBasicBlockLabel(I, true);
156       O << '\n';
157     }
158     for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
159          II != E; ++II) {
160       // Print the assembly for the instruction.
161       O << "\t";
162       ++EmittedInsts;
163       printInstruction(II);
164     }
165   }
166
167   return false;
168 }
169
170 void ARMAsmPrinter::printAddrMode1(const MachineInstr *MI, int opNum) {
171   const MachineOperand &Arg       = MI->getOperand(opNum);
172   const MachineOperand &Shift     = MI->getOperand(opNum + 1);
173   const MachineOperand &ShiftType = MI->getOperand(opNum + 2);
174
175   if(Arg.isImmediate()) {
176     assert(Shift.getImmedValue() == 0);
177     printOperand(MI, opNum);
178   } else {
179     assert(Arg.isRegister());
180     printOperand(MI, opNum);
181     if(Shift.isRegister() || Shift.getImmedValue() != 0) {
182       const char *s = NULL;
183       switch(ShiftType.getImmedValue()) {
184       case ARMShift::LSL:
185         s = ", lsl ";
186         break;
187       case ARMShift::LSR:
188         s = ", lsr ";
189         break;
190       case ARMShift::ASR:
191         s = ", asr ";
192         break;
193       case ARMShift::ROR:
194         s = ", ror ";
195         break;
196       case ARMShift::RRX:
197         s = ", rrx ";
198         break;
199       }
200       O << s;
201       printOperand(MI, opNum + 1);
202     }
203   }
204 }
205
206 void ARMAsmPrinter::printAddrMode2(const MachineInstr *MI, int opNum) {
207   const MachineOperand &Arg    = MI->getOperand(opNum);
208   const MachineOperand &Offset = MI->getOperand(opNum + 1);
209   assert(Offset.isImmediate());
210
211   if (Arg.isConstantPoolIndex()) {
212     assert(Offset.getImmedValue() == 0);
213     printOperand(MI, opNum);
214   } else {
215     assert(Arg.isRegister());
216     O << '[';
217     printOperand(MI, opNum);
218     O << ", ";
219     printOperand(MI, opNum + 1);
220     O << ']';
221   }
222 }
223
224 void ARMAsmPrinter::printAddrMode5(const MachineInstr *MI, int opNum) {
225   const MachineOperand &Arg    = MI->getOperand(opNum);
226   const MachineOperand &Offset = MI->getOperand(opNum + 1);
227   assert(Offset.isImmediate());
228
229   if (Arg.isConstantPoolIndex()) {
230     assert(Offset.getImmedValue() == 0);
231     printOperand(MI, opNum);
232   } else {
233     assert(Arg.isRegister());
234     O << '[';
235     printOperand(MI, opNum);
236     O << ", ";
237     printOperand(MI, opNum + 1);
238     O << ']';
239   }
240 }
241
242 void ARMAsmPrinter::printOperand(const MachineInstr *MI, int opNum) {
243   const MachineOperand &MO = MI->getOperand (opNum);
244   const MRegisterInfo &RI = *TM.getRegisterInfo();
245   switch (MO.getType()) {
246   case MachineOperand::MO_Register:
247     if (MRegisterInfo::isPhysicalRegister(MO.getReg()))
248       O << LowercaseString (RI.get(MO.getReg()).Name);
249     else
250       assert(0 && "not implemented");
251     break;
252   case MachineOperand::MO_Immediate:
253     O << "#" << (int)MO.getImmedValue();
254     break;
255   case MachineOperand::MO_MachineBasicBlock:
256     printBasicBlockLabel(MO.getMachineBasicBlock());
257     return;
258   case MachineOperand::MO_GlobalAddress: {
259     GlobalValue *GV = MO.getGlobal();
260     std::string Name = Mang->getValueName(GV);
261     O << Name;
262     if (GV->hasExternalWeakLinkage()) {
263       ExtWeakSymbols.insert(GV);
264     }
265   }
266     break;
267   case MachineOperand::MO_ExternalSymbol:
268     O << TAI->getGlobalPrefix() << MO.getSymbolName();
269     break;
270   case MachineOperand::MO_ConstantPoolIndex:
271     O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber()
272       << '_' << MO.getConstantPoolIndex();
273     break;
274   default:
275     O << "<unknown operand type>"; abort (); break;
276   }
277 }
278
279 void ARMAsmPrinter::printMemOperand(const MachineInstr *MI, int opNum,
280                                       const char *Modifier) {
281   assert(0 && "not implemented");
282 }
283
284 void ARMAsmPrinter::printCCOperand(const MachineInstr *MI, int opNum) {
285   int CC = (int)MI->getOperand(opNum).getImmedValue();
286   O << ARMCondCodeToString((ARMCC::CondCodes)CC);
287 }
288
289 bool ARMAsmPrinter::doInitialization(Module &M) {
290   AsmPrinter::doInitialization(M);
291   return false; // success
292 }
293
294 bool ARMAsmPrinter::doFinalization(Module &M) {
295   const TargetData *TD = TM.getTargetData();
296
297   for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
298        I != E; ++I) {
299     if (!I->hasInitializer())   // External global require no code
300       continue;
301
302     if (EmitSpecialLLVMGlobal(I))
303       continue;
304
305     O << "\n\n";
306     std::string name = Mang->getValueName(I);
307     Constant *C = I->getInitializer();
308     unsigned Size = TD->getTypeSize(C->getType());
309     unsigned Align = Log2_32(TD->getTypeAlignment(C->getType()));
310
311     if (C->isNullValue() &&
312         !I->hasSection() &&
313         (I->hasLinkOnceLinkage() || I->hasInternalLinkage() ||
314          I->hasWeakLinkage())) {
315       SwitchToDataSection(".data", I);
316       if (I->hasInternalLinkage())
317         O << "\t.local " << name << "\n";
318
319       O << "\t.comm " << name << "," << Size
320         << "," << (unsigned) (1 << Align);
321       O << "\n";
322     } else {
323       switch (I->getLinkage()) {
324       default:
325         assert(0 && "Unknown linkage type!");
326         break;
327       case GlobalValue::ExternalLinkage:
328         O << "\t.globl " << name << "\n";
329         break;
330       case GlobalValue::InternalLinkage:
331         break;
332       }
333
334       if (I->hasSection() &&
335           (I->getSection() == ".ctors" ||
336            I->getSection() == ".dtors")) {
337         std::string SectionName = ".section " + I->getSection();
338
339         SectionName += ",\"aw\",%progbits";
340
341         SwitchToDataSection(SectionName.c_str());
342       } else {
343         SwitchToDataSection(TAI->getDataSection(), I);
344       }
345
346       EmitAlignment(Align, I);
347       O << "\t.type " << name << ", %object\n";
348       O << "\t.size " << name << ", " << Size << "\n";
349       O << name << ":\n";
350       EmitGlobalConstant(C);
351     }
352   }
353
354   AsmPrinter::doFinalization(M);
355   return false; // success
356 }