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 "InstPrinter/XCoreInstPrinter.h"
16 #include "XCoreInstrInfo.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/MachineFunctionPass.h"
26 #include "llvm/CodeGen/MachineInstr.h"
27 #include "llvm/CodeGen/MachineJumpTableInfo.h"
28 #include "llvm/CodeGen/MachineModuleInfo.h"
29 #include "llvm/IR/Constants.h"
30 #include "llvm/IR/DataLayout.h"
31 #include "llvm/IR/DebugInfo.h"
32 #include "llvm/IR/DerivedTypes.h"
33 #include "llvm/IR/Mangler.h"
34 #include "llvm/IR/Module.h"
35 #include "llvm/MC/MCAsmInfo.h"
36 #include "llvm/MC/MCExpr.h"
37 #include "llvm/MC/MCInst.h"
38 #include "llvm/MC/MCStreamer.h"
39 #include "llvm/MC/MCSymbolELF.h"
40 #include "llvm/Support/ErrorHandling.h"
41 #include "llvm/Support/TargetRegistry.h"
42 #include "llvm/Support/raw_ostream.h"
43 #include "llvm/Target/TargetLoweringObjectFile.h"
48 #define DEBUG_TYPE "asm-printer"
51 class XCoreAsmPrinter
: public AsmPrinter
{
52 XCoreMCInstLower MCInstLowering
;
53 XCoreTargetStreamer
&getTargetStreamer();
56 explicit XCoreAsmPrinter(TargetMachine
&TM
,
57 std::unique_ptr
<MCStreamer
> Streamer
)
58 : AsmPrinter(TM
, std::move(Streamer
)), MCInstLowering(*this) {}
60 StringRef
getPassName() const override
{ return "XCore Assembly Printer"; }
62 void printInlineJT(const MachineInstr
*MI
, int opNum
, raw_ostream
&O
,
63 const std::string
&directive
= ".jmptable");
64 void printInlineJT32(const MachineInstr
*MI
, int opNum
, raw_ostream
&O
) {
65 printInlineJT(MI
, opNum
, O
, ".jmptable32");
67 void printOperand(const MachineInstr
*MI
, int opNum
, raw_ostream
&O
);
68 bool PrintAsmOperand(const MachineInstr
*MI
, unsigned OpNo
,
69 unsigned AsmVariant
, const char *ExtraCode
,
70 raw_ostream
&O
) override
;
71 bool PrintAsmMemoryOperand(const MachineInstr
*MI
, unsigned OpNum
,
72 unsigned AsmVariant
, const char *ExtraCode
,
73 raw_ostream
&O
) override
;
75 void emitArrayBound(MCSymbol
*Sym
, const GlobalVariable
*GV
);
76 void EmitGlobalVariable(const GlobalVariable
*GV
) override
;
78 void EmitFunctionEntryLabel() override
;
79 void EmitInstruction(const MachineInstr
*MI
) override
;
80 void EmitFunctionBodyStart() override
;
81 void EmitFunctionBodyEnd() override
;
83 } // end of anonymous namespace
85 XCoreTargetStreamer
&XCoreAsmPrinter::getTargetStreamer() {
86 return static_cast<XCoreTargetStreamer
&>(*OutStreamer
->getTargetStreamer());
89 void XCoreAsmPrinter::emitArrayBound(MCSymbol
*Sym
, const GlobalVariable
*GV
) {
90 assert( ( GV
->hasExternalLinkage() || GV
->hasWeakLinkage() ||
91 GV
->hasLinkOnceLinkage() || GV
->hasCommonLinkage() ) &&
92 "Unexpected linkage");
93 if (ArrayType
*ATy
= dyn_cast
<ArrayType
>(GV
->getValueType())) {
95 MCSymbol
*SymGlob
= OutContext
.getOrCreateSymbol(
96 Twine(Sym
->getName() + StringRef(".globound")));
97 OutStreamer
->EmitSymbolAttribute(SymGlob
, MCSA_Global
);
98 OutStreamer
->EmitAssignment(SymGlob
,
99 MCConstantExpr::create(ATy
->getNumElements(),
101 if (GV
->hasWeakLinkage() || GV
->hasLinkOnceLinkage() ||
102 GV
->hasCommonLinkage()) {
103 OutStreamer
->EmitSymbolAttribute(SymGlob
, MCSA_Weak
);
108 void XCoreAsmPrinter::EmitGlobalVariable(const GlobalVariable
*GV
) {
109 // Check to see if this is a special global used by LLVM, if so, emit it.
110 if (!GV
->hasInitializer() ||
111 EmitSpecialLLVMGlobal(GV
))
114 const DataLayout
&DL
= getDataLayout();
115 OutStreamer
->SwitchSection(getObjFileLowering().SectionForGlobal(GV
, TM
));
117 MCSymbol
*GVSym
= getSymbol(GV
);
118 const Constant
*C
= GV
->getInitializer();
119 unsigned Align
= (unsigned)DL
.getPreferredTypeAlignmentShift(C
->getType());
121 // Mark the start of the global
122 getTargetStreamer().emitCCTopData(GVSym
->getName());
124 switch (GV
->getLinkage()) {
125 case GlobalValue::AppendingLinkage
:
126 report_fatal_error("AppendingLinkage is not supported by this target!");
127 case GlobalValue::LinkOnceAnyLinkage
:
128 case GlobalValue::LinkOnceODRLinkage
:
129 case GlobalValue::WeakAnyLinkage
:
130 case GlobalValue::WeakODRLinkage
:
131 case GlobalValue::ExternalLinkage
:
132 case GlobalValue::CommonLinkage
:
133 emitArrayBound(GVSym
, GV
);
134 OutStreamer
->EmitSymbolAttribute(GVSym
, MCSA_Global
);
136 if (GV
->hasWeakLinkage() || GV
->hasLinkOnceLinkage() ||
137 GV
->hasCommonLinkage())
138 OutStreamer
->EmitSymbolAttribute(GVSym
, MCSA_Weak
);
140 case GlobalValue::InternalLinkage
:
141 case GlobalValue::PrivateLinkage
:
144 llvm_unreachable("Unknown linkage type!");
147 EmitAlignment(Align
> 2 ? Align
: 2, GV
);
149 if (GV
->isThreadLocal()) {
150 report_fatal_error("TLS is not supported by this target!");
152 unsigned Size
= DL
.getTypeAllocSize(C
->getType());
153 if (MAI
->hasDotTypeDotSizeDirective()) {
154 OutStreamer
->EmitSymbolAttribute(GVSym
, MCSA_ELF_TypeObject
);
155 OutStreamer
->emitELFSize(GVSym
, MCConstantExpr::create(Size
, OutContext
));
157 OutStreamer
->EmitLabel(GVSym
);
159 EmitGlobalConstant(DL
, C
);
160 // The ABI requires that unsigned scalar types smaller than 32 bits
161 // are padded to 32 bits.
163 OutStreamer
->EmitZeros(4 - Size
);
165 // Mark the end of the global
166 getTargetStreamer().emitCCBottomData(GVSym
->getName());
169 void XCoreAsmPrinter::EmitFunctionBodyStart() {
170 MCInstLowering
.Initialize(&MF
->getContext());
173 /// EmitFunctionBodyEnd - Targets can override this to emit stuff after
174 /// the last basic block in the function.
175 void XCoreAsmPrinter::EmitFunctionBodyEnd() {
176 // Emit function end directives
177 getTargetStreamer().emitCCBottomFunction(CurrentFnSym
->getName());
180 void XCoreAsmPrinter::EmitFunctionEntryLabel() {
181 // Mark the start of the function
182 getTargetStreamer().emitCCTopFunction(CurrentFnSym
->getName());
183 OutStreamer
->EmitLabel(CurrentFnSym
);
186 void XCoreAsmPrinter::
187 printInlineJT(const MachineInstr
*MI
, int opNum
, raw_ostream
&O
,
188 const std::string
&directive
) {
189 unsigned JTI
= MI
->getOperand(opNum
).getIndex();
190 const MachineFunction
*MF
= MI
->getParent()->getParent();
191 const MachineJumpTableInfo
*MJTI
= MF
->getJumpTableInfo();
192 const std::vector
<MachineJumpTableEntry
> &JT
= MJTI
->getJumpTables();
193 const std::vector
<MachineBasicBlock
*> &JTBBs
= JT
[JTI
].MBBs
;
194 O
<< "\t" << directive
<< " ";
195 for (unsigned i
= 0, e
= JTBBs
.size(); i
!= e
; ++i
) {
196 MachineBasicBlock
*MBB
= JTBBs
[i
];
199 MBB
->getSymbol()->print(O
, MAI
);
203 void XCoreAsmPrinter::printOperand(const MachineInstr
*MI
, int opNum
,
205 const DataLayout
&DL
= getDataLayout();
206 const MachineOperand
&MO
= MI
->getOperand(opNum
);
207 switch (MO
.getType()) {
208 case MachineOperand::MO_Register
:
209 O
<< XCoreInstPrinter::getRegisterName(MO
.getReg());
211 case MachineOperand::MO_Immediate
:
214 case MachineOperand::MO_MachineBasicBlock
:
215 MO
.getMBB()->getSymbol()->print(O
, MAI
);
217 case MachineOperand::MO_GlobalAddress
:
218 getSymbol(MO
.getGlobal())->print(O
, MAI
);
220 case MachineOperand::MO_ConstantPoolIndex
:
221 O
<< DL
.getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_'
224 case MachineOperand::MO_BlockAddress
:
225 GetBlockAddressSymbol(MO
.getBlockAddress())->print(O
, MAI
);
228 llvm_unreachable("not implemented");
232 /// PrintAsmOperand - Print out an operand for an inline asm expression.
234 bool XCoreAsmPrinter::PrintAsmOperand(const MachineInstr
*MI
, unsigned OpNo
,
235 unsigned AsmVariant
,const char *ExtraCode
,
237 // Print the operand if there is no operand modifier.
238 if (!ExtraCode
|| !ExtraCode
[0]) {
239 printOperand(MI
, OpNo
, O
);
243 // Otherwise fallback on the default implementation.
244 return AsmPrinter::PrintAsmOperand(MI
, OpNo
, AsmVariant
, ExtraCode
, O
);
247 bool XCoreAsmPrinter::
248 PrintAsmMemoryOperand(const MachineInstr
*MI
, unsigned OpNum
,
249 unsigned AsmVariant
, const char *ExtraCode
,
251 if (ExtraCode
&& ExtraCode
[0]) {
252 return true; // Unknown modifier.
254 printOperand(MI
, OpNum
, O
);
256 printOperand(MI
, OpNum
+ 1, O
);
261 void XCoreAsmPrinter::EmitInstruction(const MachineInstr
*MI
) {
262 SmallString
<128> Str
;
263 raw_svector_ostream
O(Str
);
265 switch (MI
->getOpcode()) {
266 case XCore::DBG_VALUE
:
267 llvm_unreachable("Should be handled target independently");
268 case XCore::ADD_2rus
:
269 if (MI
->getOperand(2).getImm() == 0) {
271 << XCoreInstPrinter::getRegisterName(MI
->getOperand(0).getReg()) << ", "
272 << XCoreInstPrinter::getRegisterName(MI
->getOperand(1).getReg());
273 OutStreamer
->EmitRawText(O
.str());
280 << XCoreInstPrinter::getRegisterName(MI
->getOperand(1).getReg()) << '\n';
281 if (MI
->getOpcode() == XCore::BR_JT
)
282 printInlineJT(MI
, 0, O
);
284 printInlineJT32(MI
, 0, O
);
286 OutStreamer
->EmitRawText(O
.str());
291 MCInstLowering
.Lower(MI
, TmpInst
);
293 EmitToStreamer(*OutStreamer
, TmpInst
);
296 // Force static initialization.
297 extern "C" void LLVMInitializeXCoreAsmPrinter() {
298 RegisterAsmPrinter
<XCoreAsmPrinter
> X(getTheXCoreTarget());