partial implementation of the ARM Addressing Mode 1
[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   struct VISIBILITY_HIDDEN ARMAsmPrinter : public AsmPrinter {
42     ARMAsmPrinter(std::ostream &O, TargetMachine &TM, const TargetAsmInfo *T)
43       : AsmPrinter(O, TM, T) {
44     }
45
46     /// We name each basic block in a Function with a unique number, so
47     /// that we can consistently refer to them later. This is cleared
48     /// at the beginning of each call to runOnMachineFunction().
49     ///
50     typedef std::map<const Value *, unsigned> ValueMapTy;
51     ValueMapTy NumberForBB;
52
53     virtual const char *getPassName() const {
54       return "ARM Assembly Printer";
55     }
56
57     void printAddrMode1(const MachineInstr *MI, int opNum);
58
59     void printMemRegImm(const MachineInstr *MI, int opNum,
60                         const char *Modifier = NULL) {
61       const MachineOperand &MO1 = MI->getOperand(opNum);
62       const MachineOperand &MO2 = MI->getOperand(opNum + 1);
63       assert(MO1.isImmediate());
64       bool arith = false;
65       if (Modifier != NULL) {
66         assert(strcmp(Modifier, "arith") == 0);
67         arith = true;
68       }
69
70       if (MO2.isConstantPoolIndex()) {
71         printOperand(MI, opNum + 1);
72       } else if (MO2.isRegister()) {
73         if(!arith)
74           O << '[';
75         printOperand(MI, opNum + 1);
76         O << ", ";
77         printOperand(MI, opNum);
78         if(!arith)
79           O << ']';
80       } else {
81         assert(0 && "Invalid Operand Type");
82       }
83     }
84
85     void printOperand(const MachineInstr *MI, int opNum);
86     void printMemOperand(const MachineInstr *MI, int opNum,
87                          const char *Modifier = 0);
88     void printCCOperand(const MachineInstr *MI, int opNum);
89
90     bool printInstruction(const MachineInstr *MI);  // autogenerated.
91     bool runOnMachineFunction(MachineFunction &F);
92     bool doInitialization(Module &M);
93     bool doFinalization(Module &M);
94   };
95 } // end of anonymous namespace
96
97 #include "ARMGenAsmWriter.inc"
98
99 /// createARMCodePrinterPass - Returns a pass that prints the ARM
100 /// assembly code for a MachineFunction to the given output stream,
101 /// using the given target machine description.  This should work
102 /// regardless of whether the function is in SSA form.
103 ///
104 FunctionPass *llvm::createARMCodePrinterPass(std::ostream &o,
105                                                TargetMachine &tm) {
106   return new ARMAsmPrinter(o, tm, tm.getTargetAsmInfo());
107 }
108
109 /// runOnMachineFunction - This uses the printMachineInstruction()
110 /// method to print assembly for each instruction.
111 ///
112 bool ARMAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
113   SetupMachineFunction(MF);
114   O << "\n\n";
115
116   // Print out constants referenced by the function
117   EmitConstantPool(MF.getConstantPool());
118
119   // Print out jump tables referenced by the function
120   EmitJumpTableInfo(MF.getJumpTableInfo());
121
122   // Print out labels for the function.
123   const Function *F = MF.getFunction();
124   switch (F->getLinkage()) {
125   default: assert(0 && "Unknown linkage type!");
126   case Function::InternalLinkage:
127     SwitchToTextSection("\t.text", F);
128     break;
129   case Function::ExternalLinkage:
130     SwitchToTextSection("\t.text", F);
131     O << "\t.globl\t" << CurrentFnName << "\n";
132     break;
133   case Function::WeakLinkage:
134   case Function::LinkOnceLinkage:
135     assert(0 && "Not implemented");
136     break;
137   }
138   EmitAlignment(2, F);
139   O << CurrentFnName << ":\n";
140
141   // Print out code for the function.
142   for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
143        I != E; ++I) {
144     // Print a label for the basic block.
145     if (I != MF.begin()) {
146       printBasicBlockLabel(I, true);
147       O << '\n';
148     }
149     for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
150          II != E; ++II) {
151       // Print the assembly for the instruction.
152       O << "\t";
153       printInstruction(II);
154     }
155   }
156
157   return false;
158 }
159
160 void ARMAsmPrinter::printAddrMode1(const MachineInstr *MI, int opNum) {
161   const MachineOperand &MO1 = MI->getOperand(opNum);
162
163   if(MO1.isImmediate()) {
164     printOperand(MI, opNum);
165   } else {
166     assert(MO1.isRegister());
167     printOperand(MI, opNum);
168   }
169 }
170
171 void ARMAsmPrinter::printOperand(const MachineInstr *MI, int opNum) {
172   const MachineOperand &MO = MI->getOperand (opNum);
173   const MRegisterInfo &RI = *TM.getRegisterInfo();
174   switch (MO.getType()) {
175   case MachineOperand::MO_Register:
176     if (MRegisterInfo::isPhysicalRegister(MO.getReg()))
177       O << LowercaseString (RI.get(MO.getReg()).Name);
178     else
179       assert(0 && "not implemented");
180     break;
181   case MachineOperand::MO_Immediate:
182     O << "#" << (int)MO.getImmedValue();
183     break;
184   case MachineOperand::MO_MachineBasicBlock:
185     printBasicBlockLabel(MO.getMachineBasicBlock());
186     return;
187   case MachineOperand::MO_GlobalAddress: {
188     GlobalValue *GV = MO.getGlobal();
189     std::string Name = Mang->getValueName(GV);
190     O << Name;
191   }
192     break;
193   case MachineOperand::MO_ExternalSymbol:
194     assert(0 && "not implemented");
195     abort();
196     break;
197   case MachineOperand::MO_ConstantPoolIndex:
198     O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber()
199       << '_' << MO.getConstantPoolIndex();
200     break;
201   default:
202     O << "<unknown operand type>"; abort (); break;
203   }
204 }
205
206 void ARMAsmPrinter::printMemOperand(const MachineInstr *MI, int opNum,
207                                       const char *Modifier) {
208   assert(0 && "not implemented");
209 }
210
211 void ARMAsmPrinter::printCCOperand(const MachineInstr *MI, int opNum) {
212   int CC = (int)MI->getOperand(opNum).getImmedValue();
213   O << ARMCondCodeToString((ARMCC::CondCodes)CC);
214 }
215
216 bool ARMAsmPrinter::doInitialization(Module &M) {
217   AsmPrinter::doInitialization(M);
218   return false; // success
219 }
220
221 bool ARMAsmPrinter::doFinalization(Module &M) {
222   const TargetData *TD = TM.getTargetData();
223
224   for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
225        I != E; ++I) {
226     if (!I->hasInitializer())   // External global require no code
227       continue;
228
229     if (EmitSpecialLLVMGlobal(I))
230       continue;
231
232     O << "\n\n";
233     std::string name = Mang->getValueName(I);
234     Constant *C = I->getInitializer();
235     unsigned Size = TD->getTypeSize(C->getType());
236     unsigned Align = TD->getTypeAlignment(C->getType());
237
238     switch (I->getLinkage()) {
239     default:
240       assert(0 && "Unknown linkage type!");
241       break;
242     case GlobalValue::ExternalLinkage:
243       O << "\t.globl " << name << "\n";
244       break;
245     case GlobalValue::InternalLinkage:
246       break;
247     }
248
249     assert (!C->isNullValue());
250     SwitchToDataSection(".data", I);
251
252     EmitAlignment(Align, I);
253     O << "\t.type " << name << ", %object\n";
254     O << "\t.size " << name << ", " << Size << "\n";
255     O << name << ":\n";
256     EmitGlobalConstant(C);
257   }
258   AsmPrinter::doFinalization(M);
259   return false; // success
260 }