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