the tblgen produced 'getRegisterName' method does not access
[oota-llvm.git] / lib / Target / Blackfin / AsmPrinter / BlackfinAsmPrinter.cpp
1 //===-- BlackfinAsmPrinter.cpp - Blackfin 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 GAS-format BLACKFIN assembly language.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #define DEBUG_TYPE "asm-printer"
16 #include "Blackfin.h"
17 #include "BlackfinInstrInfo.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/DwarfWriter.h"
23 #include "llvm/CodeGen/MachineFunctionPass.h"
24 #include "llvm/CodeGen/MachineConstantPool.h"
25 #include "llvm/CodeGen/MachineInstr.h"
26 #include "llvm/MC/MCStreamer.h"
27 #include "llvm/MC/MCAsmInfo.h"
28 #include "llvm/MC/MCSymbol.h"
29 #include "llvm/Target/TargetData.h"
30 #include "llvm/Target/TargetLoweringObjectFile.h"
31 #include "llvm/Target/TargetRegistry.h"
32 #include "llvm/Support/Mangler.h"
33 #include "llvm/ADT/Statistic.h"
34 #include "llvm/Support/FormattedStream.h"
35
36 using namespace llvm;
37
38 STATISTIC(EmittedInsts, "Number of machine instrs printed");
39
40 namespace {
41   class VISIBILITY_HIDDEN BlackfinAsmPrinter : public AsmPrinter {
42   public:
43     BlackfinAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
44                        const MCAsmInfo *MAI, bool V)
45       : AsmPrinter(O, TM, MAI, V) {}
46
47     virtual const char *getPassName() const {
48       return "Blackfin Assembly Printer";
49     }
50
51     void printOperand(const MachineInstr *MI, int opNum);
52     void printMemoryOperand(const MachineInstr *MI, int opNum);
53     void printInstruction(const MachineInstr *MI);  // autogenerated.
54     static const char *getRegisterName(unsigned RegNo);
55
56     void emitLinkage(const std::string &n, GlobalValue::LinkageTypes l);
57     bool runOnMachineFunction(MachineFunction &F);
58     bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
59                          unsigned AsmVariant, const char *ExtraCode);
60     bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
61                                unsigned AsmVariant, const char *ExtraCode);
62     void PrintGlobalVariable(const GlobalVariable* GVar);
63   };
64 } // end of anonymous namespace
65
66 #include "BlackfinGenAsmWriter.inc"
67
68 extern "C" void LLVMInitializeBlackfinAsmPrinter() {
69   RegisterAsmPrinter<BlackfinAsmPrinter> X(TheBlackfinTarget);
70 }
71
72 void BlackfinAsmPrinter::emitLinkage(const std::string &name,
73                                      GlobalValue::LinkageTypes l) {
74   switch (l) {
75   default: llvm_unreachable("Unknown linkage type!");
76   case GlobalValue::InternalLinkage:  // Symbols default to internal.
77   case GlobalValue::PrivateLinkage:
78   case GlobalValue::LinkerPrivateLinkage:
79     break;
80   case GlobalValue::ExternalLinkage:
81     O << MAI->getGlobalDirective() << name << "\n";
82     break;
83   case GlobalValue::LinkOnceAnyLinkage:
84   case GlobalValue::LinkOnceODRLinkage:
85   case GlobalValue::WeakAnyLinkage:
86   case GlobalValue::WeakODRLinkage:
87     O << MAI->getGlobalDirective() << name << "\n";
88     O << MAI->getWeakDefDirective() << name << "\n";
89     break;
90   }
91 }
92
93 void BlackfinAsmPrinter::PrintGlobalVariable(const GlobalVariable* GV) {
94   const TargetData *TD = TM.getTargetData();
95
96   if (!GV->hasInitializer() || EmitSpecialLLVMGlobal(GV))
97     return;
98
99   std::string name = Mang->getMangledName(GV);
100   Constant *C = GV->getInitializer();
101
102   OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(GV, Mang,
103                                                                   TM));
104   emitLinkage(name, GV->getLinkage());
105   EmitAlignment(TD->getPreferredAlignmentLog(GV), GV);
106   printVisibility(name, GV->getVisibility());
107
108   O << "\t.type " << name << ", STT_OBJECT\n";
109   O << "\t.size " << name << ',' << TD->getTypeAllocSize(C->getType()) << '\n';
110   O << name << ":\n";
111   EmitGlobalConstant(C);
112 }
113
114 /// runOnMachineFunction - This uses the printInstruction()
115 /// method to print assembly for each instruction.
116 ///
117 bool BlackfinAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
118   SetupMachineFunction(MF);
119   EmitConstantPool(MF.getConstantPool());
120   EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
121
122   const Function *F = MF.getFunction();
123   OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(F, Mang, TM));
124   EmitAlignment(2, F);
125   emitLinkage(CurrentFnName, F->getLinkage());
126   printVisibility(CurrentFnName, F->getVisibility());
127
128   O << "\t.type\t" << CurrentFnName << ", STT_FUNC\n"
129     << CurrentFnName << ":\n";
130
131   if (DW)
132     DW->BeginFunction(&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     if (!VerboseAsm && (I->pred_empty() || I->isOnlyReachableByFallthrough())) {
139       // This is an entry block or a block that's only reachable via a
140       // fallthrough edge. In non-VerboseAsm mode, don't print the label.
141     } else {
142       EmitBasicBlockStart(I);
143       O << '\n';
144     }
145
146     for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
147          II != E; ++II) {
148       // Print the assembly for the instruction.
149       processDebugLoc(II->getDebugLoc());
150
151       printInstruction(II);
152       if (VerboseAsm && !II->getDebugLoc().isUnknown())
153         EmitComments(*II);
154       O << '\n';
155       
156       ++EmittedInsts;
157     }
158   }
159
160   O << "\t.size " << CurrentFnName << ", .-" << CurrentFnName << "\n";
161
162   if (DW)
163     DW->EndFunction(&MF);
164
165   return false;
166 }
167
168 void BlackfinAsmPrinter::printOperand(const MachineInstr *MI, int opNum) {
169   const MachineOperand &MO = MI->getOperand (opNum);
170   const TargetRegisterInfo &RI = *TM.getRegisterInfo();
171   switch (MO.getType()) {
172   case MachineOperand::MO_Register:
173     assert(TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
174            "Virtual registers should be already mapped!");
175     O << RI.get(MO.getReg()).AsmName;
176     break;
177
178   case MachineOperand::MO_Immediate:
179     O << MO.getImm();
180     break;
181   case MachineOperand::MO_MachineBasicBlock:
182     GetMBBSymbol(MO.getMBB()->getNumber())->print(O, MAI);
183     return;
184   case MachineOperand::MO_GlobalAddress:
185     O << Mang->getMangledName(MO.getGlobal());
186     printOffset(MO.getOffset());
187     break;
188   case MachineOperand::MO_ExternalSymbol:
189     O << Mang->makeNameProper(MO.getSymbolName());
190     break;
191   case MachineOperand::MO_ConstantPoolIndex:
192     O << MAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << "_"
193       << MO.getIndex();
194     break;
195   case MachineOperand::MO_JumpTableIndex:
196     O << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
197       << '_' << MO.getIndex();
198     break;
199   default:
200     llvm_unreachable("<unknown operand type>");
201     break;
202   }
203 }
204
205 void BlackfinAsmPrinter::printMemoryOperand(const MachineInstr *MI, int opNum) {
206   printOperand(MI, opNum);
207
208   if (MI->getOperand(opNum+1).isImm() && MI->getOperand(opNum+1).getImm() == 0)
209     return;
210
211   O << " + ";
212   printOperand(MI, opNum+1);
213 }
214
215 /// PrintAsmOperand - Print out an operand for an inline asm expression.
216 ///
217 bool BlackfinAsmPrinter::PrintAsmOperand(const MachineInstr *MI,
218                                          unsigned OpNo,
219                                          unsigned AsmVariant,
220                                          const char *ExtraCode) {
221   if (ExtraCode && ExtraCode[0]) {
222     if (ExtraCode[1] != 0) return true; // Unknown modifier.
223
224     switch (ExtraCode[0]) {
225     default: return true;  // Unknown modifier.
226     case 'r':
227       break;
228     }
229   }
230
231   printOperand(MI, OpNo);
232
233   return false;
234 }
235
236 bool BlackfinAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
237                                                unsigned OpNo,
238                                                unsigned AsmVariant,
239                                                const char *ExtraCode) {
240   if (ExtraCode && ExtraCode[0])
241     return true;  // Unknown modifier
242
243   O << '[';
244   printOperand(MI, OpNo);
245   O << ']';
246
247   return false;
248 }