1 //===-- XCoreAsmPrinter.cpp - XCore 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 the XAS-format XCore assembly language.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "XCore.h"
16 #include "InstPrinter/XCoreInstPrinter.h"
17 #include "XCoreInstrInfo.h"
18 #include "XCoreMCInstLower.h"
19 #include "XCoreSubtarget.h"
20 #include "XCoreTargetMachine.h"
21 #include "XCoreTargetStreamer.h"
22 #include "llvm/ADT/SmallString.h"
23 #include "llvm/ADT/StringExtras.h"
24 #include "llvm/CodeGen/AsmPrinter.h"
25 #include "llvm/CodeGen/MachineConstantPool.h"
26 #include "llvm/CodeGen/MachineFunctionPass.h"
27 #include "llvm/CodeGen/MachineInstr.h"
28 #include "llvm/CodeGen/MachineJumpTableInfo.h"
29 #include "llvm/CodeGen/MachineModuleInfo.h"
30 #include "llvm/IR/Constants.h"
31 #include "llvm/IR/DataLayout.h"
32 #include "llvm/IR/DebugInfo.h"
33 #include "llvm/IR/DerivedTypes.h"
34 #include "llvm/IR/Mangler.h"
35 #include "llvm/IR/Module.h"
36 #include "llvm/MC/MCAsmInfo.h"
37 #include "llvm/MC/MCExpr.h"
38 #include "llvm/MC/MCInst.h"
39 #include "llvm/MC/MCStreamer.h"
40 #include "llvm/MC/MCSymbolELF.h"
41 #include "llvm/Support/ErrorHandling.h"
42 #include "llvm/Support/TargetRegistry.h"
43 #include "llvm/Support/raw_ostream.h"
44 #include "llvm/Target/TargetLoweringObjectFile.h"
45 #include <algorithm>
46 #include <cctype>
47 using namespace llvm;
48
49 #define DEBUG_TYPE "asm-printer"
50
51 namespace {
52 class XCoreAsmPrinter : public AsmPrinter {
53 XCoreMCInstLower MCInstLowering;
54 XCoreTargetStreamer &getTargetStreamer();
55
56 public:
XCoreAsmPrinter(TargetMachine & TM,std::unique_ptr<MCStreamer> Streamer)57 explicit XCoreAsmPrinter(TargetMachine &TM,
58 std::unique_ptr<MCStreamer> Streamer)
59 : AsmPrinter(TM, std::move(Streamer)), MCInstLowering(*this) {}
60
getPassName() const61 const char *getPassName() const override {
62 return "XCore Assembly Printer";
63 }
64
65 void printInlineJT(const MachineInstr *MI, int opNum, raw_ostream &O,
66 const std::string &directive = ".jmptable");
printInlineJT32(const MachineInstr * MI,int opNum,raw_ostream & O)67 void printInlineJT32(const MachineInstr *MI, int opNum, raw_ostream &O) {
68 printInlineJT(MI, opNum, O, ".jmptable32");
69 }
70 void printOperand(const MachineInstr *MI, int opNum, raw_ostream &O);
71 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
72 unsigned AsmVariant, const char *ExtraCode,
73 raw_ostream &O) override;
74 bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNum,
75 unsigned AsmVariant, const char *ExtraCode,
76 raw_ostream &O) override;
77
78 void emitArrayBound(MCSymbol *Sym, const GlobalVariable *GV);
79 void EmitGlobalVariable(const GlobalVariable *GV) override;
80
81 void EmitFunctionEntryLabel() override;
82 void EmitInstruction(const MachineInstr *MI) override;
83 void EmitFunctionBodyStart() override;
84 void EmitFunctionBodyEnd() override;
85 };
86 } // end of anonymous namespace
87
getTargetStreamer()88 XCoreTargetStreamer &XCoreAsmPrinter::getTargetStreamer() {
89 return static_cast<XCoreTargetStreamer&>(*OutStreamer->getTargetStreamer());
90 }
91
emitArrayBound(MCSymbol * Sym,const GlobalVariable * GV)92 void XCoreAsmPrinter::emitArrayBound(MCSymbol *Sym, const GlobalVariable *GV) {
93 assert( ( GV->hasExternalLinkage() || GV->hasWeakLinkage() ||
94 GV->hasLinkOnceLinkage() || GV->hasCommonLinkage() ) &&
95 "Unexpected linkage");
96 if (ArrayType *ATy = dyn_cast<ArrayType>(GV->getValueType())) {
97
98 MCSymbol *SymGlob = OutContext.getOrCreateSymbol(
99 Twine(Sym->getName() + StringRef(".globound")));
100 OutStreamer->EmitSymbolAttribute(SymGlob, MCSA_Global);
101 OutStreamer->EmitAssignment(SymGlob,
102 MCConstantExpr::create(ATy->getNumElements(),
103 OutContext));
104 if (GV->hasWeakLinkage() || GV->hasLinkOnceLinkage() ||
105 GV->hasCommonLinkage()) {
106 OutStreamer->EmitSymbolAttribute(SymGlob, MCSA_Weak);
107 }
108 }
109 }
110
EmitGlobalVariable(const GlobalVariable * GV)111 void XCoreAsmPrinter::EmitGlobalVariable(const GlobalVariable *GV) {
112 // Check to see if this is a special global used by LLVM, if so, emit it.
113 if (!GV->hasInitializer() ||
114 EmitSpecialLLVMGlobal(GV))
115 return;
116
117 const DataLayout &DL = getDataLayout();
118 OutStreamer->SwitchSection(
119 getObjFileLowering().SectionForGlobal(GV, *Mang, TM));
120
121 MCSymbol *GVSym = getSymbol(GV);
122 const Constant *C = GV->getInitializer();
123 unsigned Align = (unsigned)DL.getPreferredTypeAlignmentShift(C->getType());
124
125 // Mark the start of the global
126 getTargetStreamer().emitCCTopData(GVSym->getName());
127
128 switch (GV->getLinkage()) {
129 case GlobalValue::AppendingLinkage:
130 report_fatal_error("AppendingLinkage is not supported by this target!");
131 case GlobalValue::LinkOnceAnyLinkage:
132 case GlobalValue::LinkOnceODRLinkage:
133 case GlobalValue::WeakAnyLinkage:
134 case GlobalValue::WeakODRLinkage:
135 case GlobalValue::ExternalLinkage:
136 case GlobalValue::CommonLinkage:
137 emitArrayBound(GVSym, GV);
138 OutStreamer->EmitSymbolAttribute(GVSym, MCSA_Global);
139
140 if (GV->hasWeakLinkage() || GV->hasLinkOnceLinkage() ||
141 GV->hasCommonLinkage())
142 OutStreamer->EmitSymbolAttribute(GVSym, MCSA_Weak);
143 // FALL THROUGH
144 case GlobalValue::InternalLinkage:
145 case GlobalValue::PrivateLinkage:
146 break;
147 default:
148 llvm_unreachable("Unknown linkage type!");
149 }
150
151 EmitAlignment(Align > 2 ? Align : 2, GV);
152
153 if (GV->isThreadLocal()) {
154 report_fatal_error("TLS is not supported by this target!");
155 }
156 unsigned Size = DL.getTypeAllocSize(C->getType());
157 if (MAI->hasDotTypeDotSizeDirective()) {
158 OutStreamer->EmitSymbolAttribute(GVSym, MCSA_ELF_TypeObject);
159 OutStreamer->emitELFSize(cast<MCSymbolELF>(GVSym),
160 MCConstantExpr::create(Size, OutContext));
161 }
162 OutStreamer->EmitLabel(GVSym);
163
164 EmitGlobalConstant(DL, C);
165 // The ABI requires that unsigned scalar types smaller than 32 bits
166 // are padded to 32 bits.
167 if (Size < 4)
168 OutStreamer->EmitZeros(4 - Size);
169
170 // Mark the end of the global
171 getTargetStreamer().emitCCBottomData(GVSym->getName());
172 }
173
EmitFunctionBodyStart()174 void XCoreAsmPrinter::EmitFunctionBodyStart() {
175 MCInstLowering.Initialize(Mang, &MF->getContext());
176 }
177
178 /// EmitFunctionBodyEnd - Targets can override this to emit stuff after
179 /// the last basic block in the function.
EmitFunctionBodyEnd()180 void XCoreAsmPrinter::EmitFunctionBodyEnd() {
181 // Emit function end directives
182 getTargetStreamer().emitCCBottomFunction(CurrentFnSym->getName());
183 }
184
EmitFunctionEntryLabel()185 void XCoreAsmPrinter::EmitFunctionEntryLabel() {
186 // Mark the start of the function
187 getTargetStreamer().emitCCTopFunction(CurrentFnSym->getName());
188 OutStreamer->EmitLabel(CurrentFnSym);
189 }
190
191 void XCoreAsmPrinter::
printInlineJT(const MachineInstr * MI,int opNum,raw_ostream & O,const std::string & directive)192 printInlineJT(const MachineInstr *MI, int opNum, raw_ostream &O,
193 const std::string &directive) {
194 unsigned JTI = MI->getOperand(opNum).getIndex();
195 const MachineFunction *MF = MI->getParent()->getParent();
196 const MachineJumpTableInfo *MJTI = MF->getJumpTableInfo();
197 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
198 const std::vector<MachineBasicBlock*> &JTBBs = JT[JTI].MBBs;
199 O << "\t" << directive << " ";
200 for (unsigned i = 0, e = JTBBs.size(); i != e; ++i) {
201 MachineBasicBlock *MBB = JTBBs[i];
202 if (i > 0)
203 O << ",";
204 MBB->getSymbol()->print(O, MAI);
205 }
206 }
207
printOperand(const MachineInstr * MI,int opNum,raw_ostream & O)208 void XCoreAsmPrinter::printOperand(const MachineInstr *MI, int opNum,
209 raw_ostream &O) {
210 const DataLayout &DL = getDataLayout();
211 const MachineOperand &MO = MI->getOperand(opNum);
212 switch (MO.getType()) {
213 case MachineOperand::MO_Register:
214 O << XCoreInstPrinter::getRegisterName(MO.getReg());
215 break;
216 case MachineOperand::MO_Immediate:
217 O << MO.getImm();
218 break;
219 case MachineOperand::MO_MachineBasicBlock:
220 MO.getMBB()->getSymbol()->print(O, MAI);
221 break;
222 case MachineOperand::MO_GlobalAddress:
223 getSymbol(MO.getGlobal())->print(O, MAI);
224 break;
225 case MachineOperand::MO_ConstantPoolIndex:
226 O << DL.getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_'
227 << MO.getIndex();
228 break;
229 case MachineOperand::MO_BlockAddress:
230 GetBlockAddressSymbol(MO.getBlockAddress())->print(O, MAI);
231 break;
232 default:
233 llvm_unreachable("not implemented");
234 }
235 }
236
237 /// PrintAsmOperand - Print out an operand for an inline asm expression.
238 ///
PrintAsmOperand(const MachineInstr * MI,unsigned OpNo,unsigned AsmVariant,const char * ExtraCode,raw_ostream & O)239 bool XCoreAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
240 unsigned AsmVariant,const char *ExtraCode,
241 raw_ostream &O) {
242 // Print the operand if there is no operand modifier.
243 if (!ExtraCode || !ExtraCode[0]) {
244 printOperand(MI, OpNo, O);
245 return false;
246 }
247
248 // Otherwise fallback on the default implementation.
249 return AsmPrinter::PrintAsmOperand(MI, OpNo, AsmVariant, ExtraCode, O);
250 }
251
252 bool XCoreAsmPrinter::
PrintAsmMemoryOperand(const MachineInstr * MI,unsigned OpNum,unsigned AsmVariant,const char * ExtraCode,raw_ostream & O)253 PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNum,
254 unsigned AsmVariant, const char *ExtraCode,
255 raw_ostream &O) {
256 if (ExtraCode && ExtraCode[0]) {
257 return true; // Unknown modifier.
258 }
259 printOperand(MI, OpNum, O);
260 O << '[';
261 printOperand(MI, OpNum + 1, O);
262 O << ']';
263 return false;
264 }
265
EmitInstruction(const MachineInstr * MI)266 void XCoreAsmPrinter::EmitInstruction(const MachineInstr *MI) {
267 SmallString<128> Str;
268 raw_svector_ostream O(Str);
269
270 switch (MI->getOpcode()) {
271 case XCore::DBG_VALUE:
272 llvm_unreachable("Should be handled target independently");
273 case XCore::ADD_2rus:
274 if (MI->getOperand(2).getImm() == 0) {
275 O << "\tmov "
276 << XCoreInstPrinter::getRegisterName(MI->getOperand(0).getReg()) << ", "
277 << XCoreInstPrinter::getRegisterName(MI->getOperand(1).getReg());
278 OutStreamer->EmitRawText(O.str());
279 return;
280 }
281 break;
282 case XCore::BR_JT:
283 case XCore::BR_JT32:
284 O << "\tbru "
285 << XCoreInstPrinter::getRegisterName(MI->getOperand(1).getReg()) << '\n';
286 if (MI->getOpcode() == XCore::BR_JT)
287 printInlineJT(MI, 0, O);
288 else
289 printInlineJT32(MI, 0, O);
290 O << '\n';
291 OutStreamer->EmitRawText(O.str());
292 return;
293 }
294
295 MCInst TmpInst;
296 MCInstLowering.Lower(MI, TmpInst);
297
298 EmitToStreamer(*OutStreamer, TmpInst);
299 }
300
301 // Force static initialization.
LLVMInitializeXCoreAsmPrinter()302 extern "C" void LLVMInitializeXCoreAsmPrinter() {
303 RegisterAsmPrinter<XCoreAsmPrinter> X(TheXCoreTarget);
304 }
305