1 //===-- XCoreAsmPrinter.cpp - XCore 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 the XAS-format XCore assembly language.
13 //===----------------------------------------------------------------------===//
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/MCSymbol.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"
49 #define DEBUG_TYPE "asm-printer"
52 class XCoreAsmPrinter : public AsmPrinter {
53 XCoreMCInstLower MCInstLowering;
54 XCoreTargetStreamer &getTargetStreamer();
57 explicit XCoreAsmPrinter(TargetMachine &TM,
58 std::unique_ptr<MCStreamer> Streamer)
59 : AsmPrinter(TM, std::move(Streamer)), MCInstLowering(*this) {}
61 const char *getPassName() const override {
62 return "XCore Assembly Printer";
65 void printInlineJT(const MachineInstr *MI, int opNum, raw_ostream &O,
66 const std::string &directive = ".jmptable");
67 void printInlineJT32(const MachineInstr *MI, int opNum, raw_ostream &O) {
68 printInlineJT(MI, opNum, O, ".jmptable32");
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;
78 void emitArrayBound(MCSymbol *Sym, const GlobalVariable *GV);
79 void EmitGlobalVariable(const GlobalVariable *GV) override;
81 void EmitFunctionEntryLabel() override;
82 void EmitInstruction(const MachineInstr *MI) override;
83 void EmitFunctionBodyStart() override;
84 void EmitFunctionBodyEnd() override;
86 } // end of anonymous namespace
88 XCoreTargetStreamer &XCoreAsmPrinter::getTargetStreamer() {
89 return static_cast<XCoreTargetStreamer&>(*OutStreamer.getTargetStreamer());
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>(
97 cast<PointerType>(GV->getType())->getElementType())) {
99 MCSymbol *SymGlob = OutContext.GetOrCreateSymbol(
100 Twine(Sym->getName() + StringRef(".globound")));
101 OutStreamer.EmitSymbolAttribute(SymGlob, MCSA_Global);
102 OutStreamer.EmitAssignment(SymGlob,
103 MCConstantExpr::Create(ATy->getNumElements(),
105 if (GV->hasWeakLinkage() || GV->hasLinkOnceLinkage() ||
106 GV->hasCommonLinkage()) {
107 OutStreamer.EmitSymbolAttribute(SymGlob, MCSA_Weak);
112 void XCoreAsmPrinter::EmitGlobalVariable(const GlobalVariable *GV) {
113 // Check to see if this is a special global used by LLVM, if so, emit it.
114 if (!GV->hasInitializer() ||
115 EmitSpecialLLVMGlobal(GV))
118 const DataLayout *TD = TM.getDataLayout();
119 OutStreamer.SwitchSection(
120 getObjFileLowering().SectionForGlobal(GV, *Mang, TM));
122 MCSymbol *GVSym = getSymbol(GV);
123 const Constant *C = GV->getInitializer();
124 unsigned Align = (unsigned)TD->getPreferredTypeAlignmentShift(C->getType());
126 // Mark the start of the global
127 getTargetStreamer().emitCCTopData(GVSym->getName());
129 switch (GV->getLinkage()) {
130 case GlobalValue::AppendingLinkage:
131 report_fatal_error("AppendingLinkage is not supported by this target!");
132 case GlobalValue::LinkOnceAnyLinkage:
133 case GlobalValue::LinkOnceODRLinkage:
134 case GlobalValue::WeakAnyLinkage:
135 case GlobalValue::WeakODRLinkage:
136 case GlobalValue::ExternalLinkage:
137 case GlobalValue::CommonLinkage:
138 emitArrayBound(GVSym, GV);
139 OutStreamer.EmitSymbolAttribute(GVSym, MCSA_Global);
141 if (GV->hasWeakLinkage() || GV->hasLinkOnceLinkage() ||
142 GV->hasCommonLinkage())
143 OutStreamer.EmitSymbolAttribute(GVSym, MCSA_Weak);
145 case GlobalValue::InternalLinkage:
146 case GlobalValue::PrivateLinkage:
149 llvm_unreachable("Unknown linkage type!");
152 EmitAlignment(Align > 2 ? Align : 2, GV);
154 if (GV->isThreadLocal()) {
155 report_fatal_error("TLS is not supported by this target!");
157 unsigned Size = TD->getTypeAllocSize(C->getType());
158 if (MAI->hasDotTypeDotSizeDirective()) {
159 OutStreamer.EmitSymbolAttribute(GVSym, MCSA_ELF_TypeObject);
160 OutStreamer.EmitELFSize(GVSym, MCConstantExpr::Create(Size, OutContext));
162 OutStreamer.EmitLabel(GVSym);
164 EmitGlobalConstant(C);
165 // The ABI requires that unsigned scalar types smaller than 32 bits
166 // are padded to 32 bits.
168 OutStreamer.EmitZeros(4 - Size);
170 // Mark the end of the global
171 getTargetStreamer().emitCCBottomData(GVSym->getName());
174 void XCoreAsmPrinter::EmitFunctionBodyStart() {
175 MCInstLowering.Initialize(Mang, &MF->getContext());
178 /// EmitFunctionBodyEnd - Targets can override this to emit stuff after
179 /// the last basic block in the function.
180 void XCoreAsmPrinter::EmitFunctionBodyEnd() {
181 // Emit function end directives
182 getTargetStreamer().emitCCBottomFunction(CurrentFnSym->getName());
185 void XCoreAsmPrinter::EmitFunctionEntryLabel() {
186 // Mark the start of the function
187 getTargetStreamer().emitCCTopFunction(CurrentFnSym->getName());
188 OutStreamer.EmitLabel(CurrentFnSym);
191 void XCoreAsmPrinter::
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];
204 O << *MBB->getSymbol();
208 void XCoreAsmPrinter::printOperand(const MachineInstr *MI, int opNum,
210 const DataLayout *DL = TM.getDataLayout();
211 const MachineOperand &MO = MI->getOperand(opNum);
212 switch (MO.getType()) {
213 case MachineOperand::MO_Register:
214 O << XCoreInstPrinter::getRegisterName(MO.getReg());
216 case MachineOperand::MO_Immediate:
219 case MachineOperand::MO_MachineBasicBlock:
220 O << *MO.getMBB()->getSymbol();
222 case MachineOperand::MO_GlobalAddress:
223 O << *getSymbol(MO.getGlobal());
225 case MachineOperand::MO_ConstantPoolIndex:
226 O << DL->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber()
227 << '_' << MO.getIndex();
229 case MachineOperand::MO_BlockAddress:
230 O << *GetBlockAddressSymbol(MO.getBlockAddress());
233 llvm_unreachable("not implemented");
237 /// PrintAsmOperand - Print out an operand for an inline asm expression.
239 bool XCoreAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
240 unsigned AsmVariant,const char *ExtraCode,
242 // Print the operand if there is no operand modifier.
243 if (!ExtraCode || !ExtraCode[0]) {
244 printOperand(MI, OpNo, O);
248 // Otherwise fallback on the default implementation.
249 return AsmPrinter::PrintAsmOperand(MI, OpNo, AsmVariant, ExtraCode, O);
252 bool XCoreAsmPrinter::
253 PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNum,
254 unsigned AsmVariant, const char *ExtraCode,
256 if (ExtraCode && ExtraCode[0]) {
257 return true; // Unknown modifier.
259 printOperand(MI, OpNum, O);
261 printOperand(MI, OpNum + 1, O);
266 void XCoreAsmPrinter::EmitInstruction(const MachineInstr *MI) {
267 SmallString<128> Str;
268 raw_svector_ostream O(Str);
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) {
276 << XCoreInstPrinter::getRegisterName(MI->getOperand(0).getReg()) << ", "
277 << XCoreInstPrinter::getRegisterName(MI->getOperand(1).getReg());
278 OutStreamer.EmitRawText(O.str());
285 << XCoreInstPrinter::getRegisterName(MI->getOperand(1).getReg()) << '\n';
286 if (MI->getOpcode() == XCore::BR_JT)
287 printInlineJT(MI, 0, O);
289 printInlineJT32(MI, 0, O);
291 OutStreamer.EmitRawText(O.str());
296 MCInstLowering.Lower(MI, TmpInst);
298 EmitToStreamer(OutStreamer, TmpInst);
301 // Force static initialization.
302 extern "C" void LLVMInitializeXCoreAsmPrinter() {
303 RegisterAsmPrinter<XCoreAsmPrinter> X(TheXCoreTarget);