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 "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"
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 StringRef
getPassName() const override
{ return "XCore Assembly Printer"; }
63 void printInlineJT(const MachineInstr
*MI
, int opNum
, raw_ostream
&O
,
64 const std::string
&directive
= ".jmptable");
65 void printInlineJT32(const MachineInstr
*MI
, int opNum
, raw_ostream
&O
) {
66 printInlineJT(MI
, opNum
, O
, ".jmptable32");
68 void printOperand(const MachineInstr
*MI
, int opNum
, raw_ostream
&O
);
69 bool PrintAsmOperand(const MachineInstr
*MI
, unsigned OpNo
,
70 const char *ExtraCode
, raw_ostream
&O
) override
;
71 bool PrintAsmMemoryOperand(const MachineInstr
*MI
, unsigned OpNum
,
72 const char *ExtraCode
, raw_ostream
&O
) override
;
74 void emitArrayBound(MCSymbol
*Sym
, const GlobalVariable
*GV
);
75 void EmitGlobalVariable(const GlobalVariable
*GV
) override
;
77 void EmitFunctionEntryLabel() override
;
78 void EmitInstruction(const MachineInstr
*MI
) override
;
79 void EmitFunctionBodyStart() override
;
80 void EmitFunctionBodyEnd() override
;
82 } // end of anonymous namespace
84 XCoreTargetStreamer
&XCoreAsmPrinter::getTargetStreamer() {
85 return static_cast<XCoreTargetStreamer
&>(*OutStreamer
->getTargetStreamer());
88 void XCoreAsmPrinter::emitArrayBound(MCSymbol
*Sym
, const GlobalVariable
*GV
) {
89 assert( ( GV
->hasExternalLinkage() || GV
->hasWeakLinkage() ||
90 GV
->hasLinkOnceLinkage() || GV
->hasCommonLinkage() ) &&
91 "Unexpected linkage");
92 if (ArrayType
*ATy
= dyn_cast
<ArrayType
>(GV
->getValueType())) {
94 MCSymbol
*SymGlob
= OutContext
.getOrCreateSymbol(
95 Twine(Sym
->getName() + StringRef(".globound")));
96 OutStreamer
->EmitSymbolAttribute(SymGlob
, MCSA_Global
);
97 OutStreamer
->EmitAssignment(SymGlob
,
98 MCConstantExpr::create(ATy
->getNumElements(),
100 if (GV
->hasWeakLinkage() || GV
->hasLinkOnceLinkage() ||
101 GV
->hasCommonLinkage()) {
102 OutStreamer
->EmitSymbolAttribute(SymGlob
, MCSA_Weak
);
107 void XCoreAsmPrinter::EmitGlobalVariable(const GlobalVariable
*GV
) {
108 // Check to see if this is a special global used by LLVM, if so, emit it.
109 if (!GV
->hasInitializer() ||
110 EmitSpecialLLVMGlobal(GV
))
113 const DataLayout
&DL
= getDataLayout();
114 OutStreamer
->SwitchSection(getObjFileLowering().SectionForGlobal(GV
, TM
));
116 MCSymbol
*GVSym
= getSymbol(GV
);
117 const Constant
*C
= GV
->getInitializer();
118 unsigned Align
= (unsigned)DL
.getPreferredTypeAlignmentShift(C
->getType());
120 // Mark the start of the global
121 getTargetStreamer().emitCCTopData(GVSym
->getName());
123 switch (GV
->getLinkage()) {
124 case GlobalValue::AppendingLinkage
:
125 report_fatal_error("AppendingLinkage is not supported by this target!");
126 case GlobalValue::LinkOnceAnyLinkage
:
127 case GlobalValue::LinkOnceODRLinkage
:
128 case GlobalValue::WeakAnyLinkage
:
129 case GlobalValue::WeakODRLinkage
:
130 case GlobalValue::ExternalLinkage
:
131 case GlobalValue::CommonLinkage
:
132 emitArrayBound(GVSym
, GV
);
133 OutStreamer
->EmitSymbolAttribute(GVSym
, MCSA_Global
);
135 if (GV
->hasWeakLinkage() || GV
->hasLinkOnceLinkage() ||
136 GV
->hasCommonLinkage())
137 OutStreamer
->EmitSymbolAttribute(GVSym
, MCSA_Weak
);
139 case GlobalValue::InternalLinkage
:
140 case GlobalValue::PrivateLinkage
:
143 llvm_unreachable("Unknown linkage type!");
146 EmitAlignment(Align
> 2 ? Align
: 2, GV
);
148 if (GV
->isThreadLocal()) {
149 report_fatal_error("TLS is not supported by this target!");
151 unsigned Size
= DL
.getTypeAllocSize(C
->getType());
152 if (MAI
->hasDotTypeDotSizeDirective()) {
153 OutStreamer
->EmitSymbolAttribute(GVSym
, MCSA_ELF_TypeObject
);
154 OutStreamer
->emitELFSize(GVSym
, MCConstantExpr::create(Size
, OutContext
));
156 OutStreamer
->EmitLabel(GVSym
);
158 EmitGlobalConstant(DL
, C
);
159 // The ABI requires that unsigned scalar types smaller than 32 bits
160 // are padded to 32 bits.
162 OutStreamer
->EmitZeros(4 - Size
);
164 // Mark the end of the global
165 getTargetStreamer().emitCCBottomData(GVSym
->getName());
168 void XCoreAsmPrinter::EmitFunctionBodyStart() {
169 MCInstLowering
.Initialize(&MF
->getContext());
172 /// EmitFunctionBodyEnd - Targets can override this to emit stuff after
173 /// the last basic block in the function.
174 void XCoreAsmPrinter::EmitFunctionBodyEnd() {
175 // Emit function end directives
176 getTargetStreamer().emitCCBottomFunction(CurrentFnSym
->getName());
179 void XCoreAsmPrinter::EmitFunctionEntryLabel() {
180 // Mark the start of the function
181 getTargetStreamer().emitCCTopFunction(CurrentFnSym
->getName());
182 OutStreamer
->EmitLabel(CurrentFnSym
);
185 void XCoreAsmPrinter::
186 printInlineJT(const MachineInstr
*MI
, int opNum
, raw_ostream
&O
,
187 const std::string
&directive
) {
188 unsigned JTI
= MI
->getOperand(opNum
).getIndex();
189 const MachineFunction
*MF
= MI
->getParent()->getParent();
190 const MachineJumpTableInfo
*MJTI
= MF
->getJumpTableInfo();
191 const std::vector
<MachineJumpTableEntry
> &JT
= MJTI
->getJumpTables();
192 const std::vector
<MachineBasicBlock
*> &JTBBs
= JT
[JTI
].MBBs
;
193 O
<< "\t" << directive
<< " ";
194 for (unsigned i
= 0, e
= JTBBs
.size(); i
!= e
; ++i
) {
195 MachineBasicBlock
*MBB
= JTBBs
[i
];
198 MBB
->getSymbol()->print(O
, MAI
);
202 void XCoreAsmPrinter::printOperand(const MachineInstr
*MI
, int opNum
,
204 const DataLayout
&DL
= getDataLayout();
205 const MachineOperand
&MO
= MI
->getOperand(opNum
);
206 switch (MO
.getType()) {
207 case MachineOperand::MO_Register
:
208 O
<< XCoreInstPrinter::getRegisterName(MO
.getReg());
210 case MachineOperand::MO_Immediate
:
213 case MachineOperand::MO_MachineBasicBlock
:
214 MO
.getMBB()->getSymbol()->print(O
, MAI
);
216 case MachineOperand::MO_GlobalAddress
:
217 PrintSymbolOperand(MO
, O
);
219 case MachineOperand::MO_ConstantPoolIndex
:
220 O
<< DL
.getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_'
223 case MachineOperand::MO_BlockAddress
:
224 GetBlockAddressSymbol(MO
.getBlockAddress())->print(O
, MAI
);
227 llvm_unreachable("not implemented");
231 /// PrintAsmOperand - Print out an operand for an inline asm expression.
233 bool XCoreAsmPrinter::PrintAsmOperand(const MachineInstr
*MI
, unsigned OpNo
,
234 const char *ExtraCode
, raw_ostream
&O
) {
235 // Print the operand if there is no operand modifier.
236 if (!ExtraCode
|| !ExtraCode
[0]) {
237 printOperand(MI
, OpNo
, O
);
241 // Otherwise fallback on the default implementation.
242 return AsmPrinter::PrintAsmOperand(MI
, OpNo
, ExtraCode
, O
);
245 bool XCoreAsmPrinter::PrintAsmMemoryOperand(const MachineInstr
*MI
,
247 const char *ExtraCode
,
249 if (ExtraCode
&& ExtraCode
[0]) {
250 return true; // Unknown modifier.
252 printOperand(MI
, OpNum
, O
);
254 printOperand(MI
, OpNum
+ 1, O
);
259 void XCoreAsmPrinter::EmitInstruction(const MachineInstr
*MI
) {
260 SmallString
<128> Str
;
261 raw_svector_ostream
O(Str
);
263 switch (MI
->getOpcode()) {
264 case XCore::DBG_VALUE
:
265 llvm_unreachable("Should be handled target independently");
266 case XCore::ADD_2rus
:
267 if (MI
->getOperand(2).getImm() == 0) {
269 << XCoreInstPrinter::getRegisterName(MI
->getOperand(0).getReg()) << ", "
270 << XCoreInstPrinter::getRegisterName(MI
->getOperand(1).getReg());
271 OutStreamer
->EmitRawText(O
.str());
278 << XCoreInstPrinter::getRegisterName(MI
->getOperand(1).getReg()) << '\n';
279 if (MI
->getOpcode() == XCore::BR_JT
)
280 printInlineJT(MI
, 0, O
);
282 printInlineJT32(MI
, 0, O
);
284 OutStreamer
->EmitRawText(O
.str());
289 MCInstLowering
.Lower(MI
, TmpInst
);
291 EmitToStreamer(*OutStreamer
, TmpInst
);
294 // Force static initialization.
295 extern "C" void LLVMInitializeXCoreAsmPrinter() {
296 RegisterAsmPrinter
<XCoreAsmPrinter
> X(getTheXCoreTarget());