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/MachineFunctionPass.h"
23 #include "llvm/CodeGen/MachineConstantPool.h"
24 #include "llvm/CodeGen/MachineInstr.h"
25 #include "llvm/MC/MCStreamer.h"
26 #include "llvm/MC/MCAsmInfo.h"
27 #include "llvm/MC/MCContext.h"
28 #include "llvm/MC/MCSymbol.h"
29 #include "llvm/Target/Mangler.h"
30 #include "llvm/Target/TargetData.h"
31 #include "llvm/Target/TargetLoweringObjectFile.h"
32 #include "llvm/ADT/SmallString.h"
33 #include "llvm/Support/ErrorHandling.h"
34 #include "llvm/Support/TargetRegistry.h"
35 #include "llvm/Support/raw_ostream.h"
36 using namespace llvm;
37
38 namespace {
39 class BlackfinAsmPrinter : public AsmPrinter {
40 public:
BlackfinAsmPrinter(TargetMachine & TM,MCStreamer & Streamer)41 BlackfinAsmPrinter(TargetMachine &TM, MCStreamer &Streamer)
42 : AsmPrinter(TM, Streamer) {}
43
getPassName() const44 virtual const char *getPassName() const {
45 return "Blackfin Assembly Printer";
46 }
47
48 void printOperand(const MachineInstr *MI, int opNum, raw_ostream &O);
49 void printMemoryOperand(const MachineInstr *MI, int opNum, raw_ostream &O);
50 void printInstruction(const MachineInstr *MI, raw_ostream &O);// autogen'd.
51 static const char *getRegisterName(unsigned RegNo);
52
EmitInstruction(const MachineInstr * MI)53 void EmitInstruction(const MachineInstr *MI) {
54 SmallString<128> Str;
55 raw_svector_ostream OS(Str);
56 printInstruction(MI, OS);
57 OutStreamer.EmitRawText(OS.str());
58 }
59 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
60 unsigned AsmVariant, const char *ExtraCode,
61 raw_ostream &O);
62 bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
63 unsigned AsmVariant, const char *ExtraCode,
64 raw_ostream &O);
65 };
66 } // end of anonymous namespace
67
68 #include "BlackfinGenAsmWriter.inc"
69
LLVMInitializeBlackfinAsmPrinter()70 extern "C" void LLVMInitializeBlackfinAsmPrinter() {
71 RegisterAsmPrinter<BlackfinAsmPrinter> X(TheBlackfinTarget);
72 }
73
printOperand(const MachineInstr * MI,int opNum,raw_ostream & O)74 void BlackfinAsmPrinter::printOperand(const MachineInstr *MI, int opNum,
75 raw_ostream &O) {
76 const MachineOperand &MO = MI->getOperand(opNum);
77 switch (MO.getType()) {
78 case MachineOperand::MO_Register:
79 assert(TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
80 "Virtual registers should be already mapped!");
81 O << getRegisterName(MO.getReg());
82 break;
83
84 case MachineOperand::MO_Immediate:
85 O << MO.getImm();
86 break;
87 case MachineOperand::MO_MachineBasicBlock:
88 O << *MO.getMBB()->getSymbol();
89 return;
90 case MachineOperand::MO_GlobalAddress:
91 O << *Mang->getSymbol(MO.getGlobal());
92 printOffset(MO.getOffset(), O);
93 break;
94 case MachineOperand::MO_ExternalSymbol:
95 O << *GetExternalSymbolSymbol(MO.getSymbolName());
96 break;
97 case MachineOperand::MO_ConstantPoolIndex:
98 O << MAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << "_"
99 << MO.getIndex();
100 break;
101 case MachineOperand::MO_JumpTableIndex:
102 O << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
103 << '_' << MO.getIndex();
104 break;
105 default:
106 llvm_unreachable("<unknown operand type>");
107 break;
108 }
109 }
110
printMemoryOperand(const MachineInstr * MI,int opNum,raw_ostream & O)111 void BlackfinAsmPrinter::printMemoryOperand(const MachineInstr *MI, int opNum,
112 raw_ostream &O) {
113 printOperand(MI, opNum, O);
114
115 if (MI->getOperand(opNum+1).isImm() && MI->getOperand(opNum+1).getImm() == 0)
116 return;
117
118 O << " + ";
119 printOperand(MI, opNum+1, O);
120 }
121
122 /// PrintAsmOperand - Print out an operand for an inline asm expression.
123 ///
PrintAsmOperand(const MachineInstr * MI,unsigned OpNo,unsigned AsmVariant,const char * ExtraCode,raw_ostream & O)124 bool BlackfinAsmPrinter::PrintAsmOperand(const MachineInstr *MI,
125 unsigned OpNo, unsigned AsmVariant,
126 const char *ExtraCode,
127 raw_ostream &O) {
128 if (ExtraCode && ExtraCode[0]) {
129 if (ExtraCode[1] != 0) return true; // Unknown modifier.
130
131 switch (ExtraCode[0]) {
132 default: return true; // Unknown modifier.
133 case 'r':
134 break;
135 }
136 }
137
138 printOperand(MI, OpNo, O);
139
140 return false;
141 }
142
PrintAsmMemoryOperand(const MachineInstr * MI,unsigned OpNo,unsigned AsmVariant,const char * ExtraCode,raw_ostream & O)143 bool BlackfinAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
144 unsigned OpNo,
145 unsigned AsmVariant,
146 const char *ExtraCode,
147 raw_ostream &O) {
148 if (ExtraCode && ExtraCode[0])
149 return true; // Unknown modifier
150
151 O << '[';
152 printOperand(MI, OpNo, O);
153 O << ']';
154
155 return false;
156 }
157