91fd5dde5a23739666746564b238ec1258c534d9
[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     EmitBasicBlockStart(I);
139
140     for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
141          II != E; ++II) {
142       // Print the assembly for the instruction.
143       processDebugLoc(II, true);
144
145       printInstruction(II);
146       if (VerboseAsm && !II->getDebugLoc().isUnknown())
147         EmitComments(*II);
148       O << '\n';
149       
150       processDebugLoc(II, false);
151       ++EmittedInsts;
152     }
153   }
154
155   O << "\t.size " << CurrentFnName << ", .-" << CurrentFnName << "\n";
156
157   if (DW)
158     DW->EndFunction(&MF);
159
160   return false;
161 }
162
163 void BlackfinAsmPrinter::printOperand(const MachineInstr *MI, int opNum) {
164   const MachineOperand &MO = MI->getOperand (opNum);
165   switch (MO.getType()) {
166   case MachineOperand::MO_Register:
167     assert(TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
168            "Virtual registers should be already mapped!");
169     O << getRegisterName(MO.getReg());
170     break;
171
172   case MachineOperand::MO_Immediate:
173     O << MO.getImm();
174     break;
175   case MachineOperand::MO_MachineBasicBlock:
176     GetMBBSymbol(MO.getMBB()->getNumber())->print(O, MAI);
177     return;
178   case MachineOperand::MO_GlobalAddress:
179     O << Mang->getMangledName(MO.getGlobal());
180     printOffset(MO.getOffset());
181     break;
182   case MachineOperand::MO_ExternalSymbol:
183     O << Mang->makeNameProper(MO.getSymbolName());
184     break;
185   case MachineOperand::MO_ConstantPoolIndex:
186     O << MAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << "_"
187       << MO.getIndex();
188     break;
189   case MachineOperand::MO_JumpTableIndex:
190     O << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
191       << '_' << MO.getIndex();
192     break;
193   default:
194     llvm_unreachable("<unknown operand type>");
195     break;
196   }
197 }
198
199 void BlackfinAsmPrinter::printMemoryOperand(const MachineInstr *MI, int opNum) {
200   printOperand(MI, opNum);
201
202   if (MI->getOperand(opNum+1).isImm() && MI->getOperand(opNum+1).getImm() == 0)
203     return;
204
205   O << " + ";
206   printOperand(MI, opNum+1);
207 }
208
209 /// PrintAsmOperand - Print out an operand for an inline asm expression.
210 ///
211 bool BlackfinAsmPrinter::PrintAsmOperand(const MachineInstr *MI,
212                                          unsigned OpNo,
213                                          unsigned AsmVariant,
214                                          const char *ExtraCode) {
215   if (ExtraCode && ExtraCode[0]) {
216     if (ExtraCode[1] != 0) return true; // Unknown modifier.
217
218     switch (ExtraCode[0]) {
219     default: return true;  // Unknown modifier.
220     case 'r':
221       break;
222     }
223   }
224
225   printOperand(MI, OpNo);
226
227   return false;
228 }
229
230 bool BlackfinAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
231                                                unsigned OpNo,
232                                                unsigned AsmVariant,
233                                                const char *ExtraCode) {
234   if (ExtraCode && ExtraCode[0])
235     return true;  // Unknown modifier
236
237   O << '[';
238   printOperand(MI, OpNo);
239   O << ']';
240
241   return false;
242 }