1 //===-- BlackfinAsmPrinter.cpp - Blackfin LLVM assembly writer ------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains a printer that converts from our internal representation
11 // of machine-dependent LLVM code to GAS-format BLACKFIN assembly language.
13 //===----------------------------------------------------------------------===//
15 #define DEBUG_TYPE "asm-printer"
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"
38 STATISTIC(EmittedInsts, "Number of machine instrs printed");
41 class VISIBILITY_HIDDEN BlackfinAsmPrinter : public AsmPrinter {
43 BlackfinAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
44 const MCAsmInfo *MAI, bool V)
45 : AsmPrinter(O, TM, MAI, V) {}
47 virtual const char *getPassName() const {
48 return "Blackfin Assembly Printer";
51 void printOperand(const MachineInstr *MI, int opNum);
52 void printMemoryOperand(const MachineInstr *MI, int opNum);
53 void printInstruction(const MachineInstr *MI); // autogenerated.
54 void emitLinkage(const std::string &n, GlobalValue::LinkageTypes l);
55 bool runOnMachineFunction(MachineFunction &F);
56 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
57 unsigned AsmVariant, const char *ExtraCode);
58 bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
59 unsigned AsmVariant, const char *ExtraCode);
60 void PrintGlobalVariable(const GlobalVariable* GVar);
62 } // end of anonymous namespace
64 #include "BlackfinGenAsmWriter.inc"
66 extern "C" void LLVMInitializeBlackfinAsmPrinter() {
67 RegisterAsmPrinter<BlackfinAsmPrinter> X(TheBlackfinTarget);
70 void BlackfinAsmPrinter::emitLinkage(const std::string &name,
71 GlobalValue::LinkageTypes l) {
73 default: llvm_unreachable("Unknown linkage type!");
74 case GlobalValue::InternalLinkage: // Symbols default to internal.
75 case GlobalValue::PrivateLinkage:
76 case GlobalValue::LinkerPrivateLinkage:
78 case GlobalValue::ExternalLinkage:
79 O << MAI->getGlobalDirective() << name << "\n";
81 case GlobalValue::LinkOnceAnyLinkage:
82 case GlobalValue::LinkOnceODRLinkage:
83 case GlobalValue::WeakAnyLinkage:
84 case GlobalValue::WeakODRLinkage:
85 O << MAI->getGlobalDirective() << name << "\n";
86 O << MAI->getWeakDefDirective() << name << "\n";
91 void BlackfinAsmPrinter::PrintGlobalVariable(const GlobalVariable* GV) {
92 const TargetData *TD = TM.getTargetData();
94 if (!GV->hasInitializer() || EmitSpecialLLVMGlobal(GV))
97 std::string name = Mang->getMangledName(GV);
98 Constant *C = GV->getInitializer();
100 OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(GV, Mang,
102 emitLinkage(name, GV->getLinkage());
103 EmitAlignment(TD->getPreferredAlignmentLog(GV), GV);
104 printVisibility(name, GV->getVisibility());
106 O << "\t.type " << name << ", STT_OBJECT\n";
107 O << "\t.size " << name << ',' << TD->getTypeAllocSize(C->getType()) << '\n';
109 EmitGlobalConstant(C);
112 /// runOnMachineFunction - This uses the printInstruction()
113 /// method to print assembly for each instruction.
115 bool BlackfinAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
116 SetupMachineFunction(MF);
117 EmitConstantPool(MF.getConstantPool());
118 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
120 const Function *F = MF.getFunction();
121 OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(F, Mang, TM));
123 emitLinkage(CurrentFnName, F->getLinkage());
124 printVisibility(CurrentFnName, F->getVisibility());
126 O << "\t.type\t" << CurrentFnName << ", STT_FUNC\n"
127 << CurrentFnName << ":\n";
130 DW->BeginFunction(&MF);
132 // Print out code for the function.
133 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
135 // Print a label for the basic block.
136 if (!VerboseAsm && (I->pred_empty() || I->isOnlyReachableByFallthrough())) {
137 // This is an entry block or a block that's only reachable via a
138 // fallthrough edge. In non-VerboseAsm mode, don't print the label.
140 printBasicBlockLabel(I, true, true, VerboseAsm);
144 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
146 // Print the assembly for the instruction.
147 processDebugLoc(II->getDebugLoc());
149 printInstruction(II);
150 if (VerboseAsm && !II->getDebugLoc().isUnknown())
158 O << "\t.size " << CurrentFnName << ", .-" << CurrentFnName << "\n";
161 DW->EndFunction(&MF);
166 void BlackfinAsmPrinter::printOperand(const MachineInstr *MI, int opNum) {
167 const MachineOperand &MO = MI->getOperand (opNum);
168 const TargetRegisterInfo &RI = *TM.getRegisterInfo();
169 switch (MO.getType()) {
170 case MachineOperand::MO_Register:
171 assert(TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
172 "Virtual registers should be already mapped!");
173 O << RI.get(MO.getReg()).AsmName;
176 case MachineOperand::MO_Immediate:
179 case MachineOperand::MO_MachineBasicBlock:
180 GetMBBSymbol(MO.getMBB()->getNumber())->print(O, MAI);
182 case MachineOperand::MO_GlobalAddress:
183 O << Mang->getMangledName(MO.getGlobal());
184 printOffset(MO.getOffset());
186 case MachineOperand::MO_ExternalSymbol:
187 O << Mang->makeNameProper(MO.getSymbolName());
189 case MachineOperand::MO_ConstantPoolIndex:
190 O << MAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << "_"
193 case MachineOperand::MO_JumpTableIndex:
194 O << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
195 << '_' << MO.getIndex();
198 llvm_unreachable("<unknown operand type>");
203 void BlackfinAsmPrinter::printMemoryOperand(const MachineInstr *MI, int opNum) {
204 printOperand(MI, opNum);
206 if (MI->getOperand(opNum+1).isImm() && MI->getOperand(opNum+1).getImm() == 0)
210 printOperand(MI, opNum+1);
213 /// PrintAsmOperand - Print out an operand for an inline asm expression.
215 bool BlackfinAsmPrinter::PrintAsmOperand(const MachineInstr *MI,
218 const char *ExtraCode) {
219 if (ExtraCode && ExtraCode[0]) {
220 if (ExtraCode[1] != 0) return true; // Unknown modifier.
222 switch (ExtraCode[0]) {
223 default: return true; // Unknown modifier.
229 printOperand(MI, OpNo);
234 bool BlackfinAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
237 const char *ExtraCode) {
238 if (ExtraCode && ExtraCode[0])
239 return true; // Unknown modifier
242 printOperand(MI, OpNo);