1 //===-- XCoreAsmPrinter.cpp - XCore LLVM assembly writer ------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file contains a printer that converts from our internal representation
10 // of machine-dependent LLVM code to the XAS-format XCore assembly language.
12 //===----------------------------------------------------------------------===//
14 #include "MCTargetDesc/XCoreInstPrinter.h"
15 #include "TargetInfo/XCoreTargetInfo.h"
17 #include "XCoreMCInstLower.h"
18 #include "XCoreSubtarget.h"
19 #include "XCoreTargetMachine.h"
20 #include "XCoreTargetStreamer.h"
21 #include "llvm/ADT/SmallString.h"
22 #include "llvm/ADT/StringExtras.h"
23 #include "llvm/CodeGen/AsmPrinter.h"
24 #include "llvm/CodeGen/MachineConstantPool.h"
25 #include "llvm/CodeGen/MachineInstr.h"
26 #include "llvm/CodeGen/MachineJumpTableInfo.h"
27 #include "llvm/CodeGen/MachineModuleInfo.h"
28 #include "llvm/IR/DataLayout.h"
29 #include "llvm/IR/DebugInfo.h"
30 #include "llvm/IR/DerivedTypes.h"
31 #include "llvm/IR/Mangler.h"
32 #include "llvm/MC/MCAsmInfo.h"
33 #include "llvm/MC/MCExpr.h"
34 #include "llvm/MC/MCInst.h"
35 #include "llvm/MC/MCStreamer.h"
36 #include "llvm/MC/TargetRegistry.h"
37 #include "llvm/Support/ErrorHandling.h"
38 #include "llvm/Support/raw_ostream.h"
39 #include "llvm/Target/TargetLoweringObjectFile.h"
44 #define DEBUG_TYPE "asm-printer"
47 class XCoreAsmPrinter
: public AsmPrinter
{
48 XCoreMCInstLower MCInstLowering
;
49 XCoreTargetStreamer
&getTargetStreamer();
52 explicit XCoreAsmPrinter(TargetMachine
&TM
,
53 std::unique_ptr
<MCStreamer
> Streamer
)
54 : AsmPrinter(TM
, std::move(Streamer
)), MCInstLowering(*this) {}
56 StringRef
getPassName() const override
{ return "XCore Assembly Printer"; }
58 void printInlineJT(const MachineInstr
*MI
, int opNum
, raw_ostream
&O
,
59 const std::string
&directive
= ".jmptable");
60 void printInlineJT32(const MachineInstr
*MI
, int opNum
, raw_ostream
&O
) {
61 printInlineJT(MI
, opNum
, O
, ".jmptable32");
63 void printOperand(const MachineInstr
*MI
, int opNum
, raw_ostream
&O
);
64 bool PrintAsmOperand(const MachineInstr
*MI
, unsigned OpNo
,
65 const char *ExtraCode
, raw_ostream
&O
) override
;
66 bool PrintAsmMemoryOperand(const MachineInstr
*MI
, unsigned OpNum
,
67 const char *ExtraCode
, raw_ostream
&O
) override
;
69 void emitArrayBound(MCSymbol
*Sym
, const GlobalVariable
*GV
);
70 void emitGlobalVariable(const GlobalVariable
*GV
) override
;
72 void emitFunctionEntryLabel() override
;
73 void emitInstruction(const MachineInstr
*MI
) override
;
74 void emitFunctionBodyStart() override
;
75 void emitFunctionBodyEnd() override
;
77 } // end of anonymous namespace
79 XCoreTargetStreamer
&XCoreAsmPrinter::getTargetStreamer() {
80 return static_cast<XCoreTargetStreamer
&>(*OutStreamer
->getTargetStreamer());
83 void XCoreAsmPrinter::emitArrayBound(MCSymbol
*Sym
, const GlobalVariable
*GV
) {
84 assert( ( GV
->hasExternalLinkage() || GV
->hasWeakLinkage() ||
85 GV
->hasLinkOnceLinkage() || GV
->hasCommonLinkage() ) &&
86 "Unexpected linkage");
87 if (ArrayType
*ATy
= dyn_cast
<ArrayType
>(GV
->getValueType())) {
89 MCSymbol
*SymGlob
= OutContext
.getOrCreateSymbol(
90 Twine(Sym
->getName() + StringRef(".globound")));
91 OutStreamer
->emitSymbolAttribute(SymGlob
, MCSA_Global
);
92 OutStreamer
->emitAssignment(SymGlob
,
93 MCConstantExpr::create(ATy
->getNumElements(),
95 if (GV
->hasWeakLinkage() || GV
->hasLinkOnceLinkage() ||
96 GV
->hasCommonLinkage()) {
97 OutStreamer
->emitSymbolAttribute(SymGlob
, MCSA_Weak
);
102 void XCoreAsmPrinter::emitGlobalVariable(const GlobalVariable
*GV
) {
103 // Check to see if this is a special global used by LLVM, if so, emit it.
104 if (!GV
->hasInitializer() || emitSpecialLLVMGlobal(GV
))
107 const DataLayout
&DL
= getDataLayout();
108 OutStreamer
->switchSection(getObjFileLowering().SectionForGlobal(GV
, TM
));
110 MCSymbol
*GVSym
= getSymbol(GV
);
111 const Constant
*C
= GV
->getInitializer();
112 const Align Alignment
= DL
.getPrefTypeAlign(C
->getType());
114 // Mark the start of the global
115 getTargetStreamer().emitCCTopData(GVSym
->getName());
117 switch (GV
->getLinkage()) {
118 case GlobalValue::AppendingLinkage
:
119 report_fatal_error("AppendingLinkage is not supported by this target!");
120 case GlobalValue::LinkOnceAnyLinkage
:
121 case GlobalValue::LinkOnceODRLinkage
:
122 case GlobalValue::WeakAnyLinkage
:
123 case GlobalValue::WeakODRLinkage
:
124 case GlobalValue::ExternalLinkage
:
125 case GlobalValue::CommonLinkage
:
126 emitArrayBound(GVSym
, GV
);
127 OutStreamer
->emitSymbolAttribute(GVSym
, MCSA_Global
);
129 if (GV
->hasWeakLinkage() || GV
->hasLinkOnceLinkage() ||
130 GV
->hasCommonLinkage())
131 OutStreamer
->emitSymbolAttribute(GVSym
, MCSA_Weak
);
133 case GlobalValue::InternalLinkage
:
134 case GlobalValue::PrivateLinkage
:
137 llvm_unreachable("Unknown linkage type!");
140 emitAlignment(std::max(Alignment
, Align(4)), GV
);
142 if (GV
->isThreadLocal()) {
143 report_fatal_error("TLS is not supported by this target!");
145 unsigned Size
= DL
.getTypeAllocSize(C
->getType());
146 if (MAI
->hasDotTypeDotSizeDirective()) {
147 OutStreamer
->emitSymbolAttribute(GVSym
, MCSA_ELF_TypeObject
);
148 OutStreamer
->emitELFSize(GVSym
, MCConstantExpr::create(Size
, OutContext
));
150 OutStreamer
->emitLabel(GVSym
);
152 emitGlobalConstant(DL
, C
);
153 // The ABI requires that unsigned scalar types smaller than 32 bits
154 // are padded to 32 bits.
156 OutStreamer
->emitZeros(4 - Size
);
158 // Mark the end of the global
159 getTargetStreamer().emitCCBottomData(GVSym
->getName());
162 void XCoreAsmPrinter::emitFunctionBodyStart() {
163 MCInstLowering
.Initialize(&MF
->getContext());
166 /// EmitFunctionBodyEnd - Targets can override this to emit stuff after
167 /// the last basic block in the function.
168 void XCoreAsmPrinter::emitFunctionBodyEnd() {
169 // Emit function end directives
170 getTargetStreamer().emitCCBottomFunction(CurrentFnSym
->getName());
173 void XCoreAsmPrinter::emitFunctionEntryLabel() {
174 // Mark the start of the function
175 getTargetStreamer().emitCCTopFunction(CurrentFnSym
->getName());
176 OutStreamer
->emitLabel(CurrentFnSym
);
179 void XCoreAsmPrinter::
180 printInlineJT(const MachineInstr
*MI
, int opNum
, raw_ostream
&O
,
181 const std::string
&directive
) {
182 unsigned JTI
= MI
->getOperand(opNum
).getIndex();
183 const MachineFunction
*MF
= MI
->getParent()->getParent();
184 const MachineJumpTableInfo
*MJTI
= MF
->getJumpTableInfo();
185 const std::vector
<MachineJumpTableEntry
> &JT
= MJTI
->getJumpTables();
186 const std::vector
<MachineBasicBlock
*> &JTBBs
= JT
[JTI
].MBBs
;
187 O
<< "\t" << directive
<< " ";
188 for (unsigned i
= 0, e
= JTBBs
.size(); i
!= e
; ++i
) {
189 MachineBasicBlock
*MBB
= JTBBs
[i
];
192 MBB
->getSymbol()->print(O
, MAI
);
196 void XCoreAsmPrinter::printOperand(const MachineInstr
*MI
, int opNum
,
198 const DataLayout
&DL
= getDataLayout();
199 const MachineOperand
&MO
= MI
->getOperand(opNum
);
200 switch (MO
.getType()) {
201 case MachineOperand::MO_Register
:
202 O
<< XCoreInstPrinter::getRegisterName(MO
.getReg());
204 case MachineOperand::MO_Immediate
:
207 case MachineOperand::MO_MachineBasicBlock
:
208 MO
.getMBB()->getSymbol()->print(O
, MAI
);
210 case MachineOperand::MO_GlobalAddress
:
211 PrintSymbolOperand(MO
, O
);
213 case MachineOperand::MO_ConstantPoolIndex
:
214 O
<< DL
.getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_'
217 case MachineOperand::MO_BlockAddress
:
218 GetBlockAddressSymbol(MO
.getBlockAddress())->print(O
, MAI
);
221 llvm_unreachable("not implemented");
225 /// PrintAsmOperand - Print out an operand for an inline asm expression.
227 bool XCoreAsmPrinter::PrintAsmOperand(const MachineInstr
*MI
, unsigned OpNo
,
228 const char *ExtraCode
, raw_ostream
&O
) {
229 // Print the operand if there is no operand modifier.
230 if (!ExtraCode
|| !ExtraCode
[0]) {
231 printOperand(MI
, OpNo
, O
);
235 // Otherwise fallback on the default implementation.
236 return AsmPrinter::PrintAsmOperand(MI
, OpNo
, ExtraCode
, O
);
239 bool XCoreAsmPrinter::PrintAsmMemoryOperand(const MachineInstr
*MI
,
241 const char *ExtraCode
,
243 if (ExtraCode
&& ExtraCode
[0]) {
244 return true; // Unknown modifier.
246 printOperand(MI
, OpNum
, O
);
248 printOperand(MI
, OpNum
+ 1, O
);
253 void XCoreAsmPrinter::emitInstruction(const MachineInstr
*MI
) {
254 XCore_MC::verifyInstructionPredicates(MI
->getOpcode(),
255 getSubtargetInfo().getFeatureBits());
257 SmallString
<128> Str
;
258 raw_svector_ostream
O(Str
);
260 switch (MI
->getOpcode()) {
261 case XCore::DBG_VALUE
:
262 llvm_unreachable("Should be handled target independently");
263 case XCore::ADD_2rus
:
264 if (MI
->getOperand(2).getImm() == 0) {
266 << XCoreInstPrinter::getRegisterName(MI
->getOperand(0).getReg()) << ", "
267 << XCoreInstPrinter::getRegisterName(MI
->getOperand(1).getReg());
268 OutStreamer
->emitRawText(O
.str());
275 << XCoreInstPrinter::getRegisterName(MI
->getOperand(1).getReg()) << '\n';
276 if (MI
->getOpcode() == XCore::BR_JT
)
277 printInlineJT(MI
, 0, O
);
279 printInlineJT32(MI
, 0, O
);
281 OutStreamer
->emitRawText(O
.str());
286 MCInstLowering
.Lower(MI
, TmpInst
);
288 EmitToStreamer(*OutStreamer
, TmpInst
);
291 // Force static initialization.
292 extern "C" LLVM_EXTERNAL_VISIBILITY
void LLVMInitializeXCoreAsmPrinter() {
293 RegisterAsmPrinter
<XCoreAsmPrinter
> X(getTheXCoreTarget());