LLVM 20.0.0git
XCoreAsmPrinter.cpp
Go to the documentation of this file.
1//===-- XCoreAsmPrinter.cpp - XCore LLVM assembly writer ------------------===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8//
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.
11//
12//===----------------------------------------------------------------------===//
13
16#include "XCore.h"
17#include "XCoreMCInstLower.h"
18#include "XCoreSubtarget.h"
19#include "XCoreTargetMachine.h"
20#include "XCoreTargetStreamer.h"
28#include "llvm/IR/DataLayout.h"
29#include "llvm/IR/DebugInfo.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"
40#include <algorithm>
41#include <cctype>
42using namespace llvm;
43
44#define DEBUG_TYPE "asm-printer"
45
46namespace {
47 class XCoreAsmPrinter : public AsmPrinter {
48 XCoreMCInstLower MCInstLowering;
49 XCoreTargetStreamer &getTargetStreamer();
50
51 public:
52 explicit XCoreAsmPrinter(TargetMachine &TM,
53 std::unique_ptr<MCStreamer> Streamer)
54 : AsmPrinter(TM, std::move(Streamer)), MCInstLowering(*this) {}
55
56 StringRef getPassName() const override { return "XCore Assembly Printer"; }
57
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");
62 }
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;
68
69 void emitArrayBound(MCSymbol *Sym, const GlobalVariable *GV);
70 void emitGlobalVariable(const GlobalVariable *GV) override;
71
72 void emitFunctionEntryLabel() override;
73 void emitInstruction(const MachineInstr *MI) override;
74 void emitFunctionBodyStart() override;
75 void emitFunctionBodyEnd() override;
76 };
77} // end of anonymous namespace
78
79XCoreTargetStreamer &XCoreAsmPrinter::getTargetStreamer() {
80 return static_cast<XCoreTargetStreamer&>(*OutStreamer->getTargetStreamer());
81}
82
83void 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())) {
88
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(),
94 OutContext));
95 if (GV->hasWeakLinkage() || GV->hasLinkOnceLinkage() ||
96 GV->hasCommonLinkage()) {
97 OutStreamer->emitSymbolAttribute(SymGlob, MCSA_Weak);
98 }
99 }
100}
101
102void 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))
105 return;
106
107 const DataLayout &DL = getDataLayout();
108 OutStreamer->switchSection(getObjFileLowering().SectionForGlobal(GV, TM));
109
110 MCSymbol *GVSym = getSymbol(GV);
111 const Constant *C = GV->getInitializer();
112 const Align Alignment = DL.getPrefTypeAlign(C->getType());
113
114 // Mark the start of the global
115 getTargetStreamer().emitCCTopData(GVSym->getName());
116
117 switch (GV->getLinkage()) {
119 report_fatal_error("AppendingLinkage is not supported by this target!");
126 emitArrayBound(GVSym, GV);
127 OutStreamer->emitSymbolAttribute(GVSym, MCSA_Global);
128
129 if (GV->hasWeakLinkage() || GV->hasLinkOnceLinkage() ||
130 GV->hasCommonLinkage())
131 OutStreamer->emitSymbolAttribute(GVSym, MCSA_Weak);
132 [[fallthrough]];
135 break;
136 default:
137 llvm_unreachable("Unknown linkage type!");
138 }
139
140 emitAlignment(std::max(Alignment, Align(4)), GV);
141
142 if (GV->isThreadLocal()) {
143 report_fatal_error("TLS is not supported by this target!");
144 }
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));
149 }
150 OutStreamer->emitLabel(GVSym);
151
152 emitGlobalConstant(DL, C);
153 // The ABI requires that unsigned scalar types smaller than 32 bits
154 // are padded to 32 bits.
155 if (Size < 4)
156 OutStreamer->emitZeros(4 - Size);
157
158 // Mark the end of the global
159 getTargetStreamer().emitCCBottomData(GVSym->getName());
160}
161
162void XCoreAsmPrinter::emitFunctionBodyStart() {
163 MCInstLowering.Initialize(&MF->getContext());
164}
165
166/// EmitFunctionBodyEnd - Targets can override this to emit stuff after
167/// the last basic block in the function.
168void XCoreAsmPrinter::emitFunctionBodyEnd() {
169 // Emit function end directives
170 getTargetStreamer().emitCCBottomFunction(CurrentFnSym->getName());
171}
172
173void XCoreAsmPrinter::emitFunctionEntryLabel() {
174 // Mark the start of the function
175 getTargetStreamer().emitCCTopFunction(CurrentFnSym->getName());
176 OutStreamer->emitLabel(CurrentFnSym);
177}
178
179void XCoreAsmPrinter::
180printInlineJT(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];
190 if (i > 0)
191 O << ",";
192 MBB->getSymbol()->print(O, MAI);
193 }
194}
195
196void XCoreAsmPrinter::printOperand(const MachineInstr *MI, int opNum,
197 raw_ostream &O) {
198 const DataLayout &DL = getDataLayout();
199 const MachineOperand &MO = MI->getOperand(opNum);
200 switch (MO.getType()) {
203 break;
205 O << MO.getImm();
206 break;
208 MO.getMBB()->getSymbol()->print(O, MAI);
209 break;
211 PrintSymbolOperand(MO, O);
212 break;
214 O << DL.getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_'
215 << MO.getIndex();
216 break;
218 GetBlockAddressSymbol(MO.getBlockAddress())->print(O, MAI);
219 break;
220 default:
221 llvm_unreachable("not implemented");
222 }
223}
224
225/// PrintAsmOperand - Print out an operand for an inline asm expression.
226///
227bool 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);
232 return false;
233 }
234
235 // Otherwise fallback on the default implementation.
236 return AsmPrinter::PrintAsmOperand(MI, OpNo, ExtraCode, O);
237}
238
239bool XCoreAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
240 unsigned OpNum,
241 const char *ExtraCode,
242 raw_ostream &O) {
243 if (ExtraCode && ExtraCode[0]) {
244 return true; // Unknown modifier.
245 }
246 printOperand(MI, OpNum, O);
247 O << '[';
248 printOperand(MI, OpNum + 1, O);
249 O << ']';
250 return false;
251}
252
253void XCoreAsmPrinter::emitInstruction(const MachineInstr *MI) {
254 XCore_MC::verifyInstructionPredicates(MI->getOpcode(),
255 getSubtargetInfo().getFeatureBits());
256
259
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) {
265 O << "\tmov "
266 << XCoreInstPrinter::getRegisterName(MI->getOperand(0).getReg()) << ", "
267 << XCoreInstPrinter::getRegisterName(MI->getOperand(1).getReg());
268 OutStreamer->emitRawText(O.str());
269 return;
270 }
271 break;
272 case XCore::BR_JT:
273 case XCore::BR_JT32:
274 O << "\tbru "
275 << XCoreInstPrinter::getRegisterName(MI->getOperand(1).getReg()) << '\n';
276 if (MI->getOpcode() == XCore::BR_JT)
277 printInlineJT(MI, 0, O);
278 else
279 printInlineJT32(MI, 0, O);
280 O << '\n';
281 OutStreamer->emitRawText(O.str());
282 return;
283 }
284
285 MCInst TmpInst;
286 MCInstLowering.Lower(MI, TmpInst);
287
288 EmitToStreamer(*OutStreamer, TmpInst);
289}
290
291// Force static initialization.
294}
MachineBasicBlock & MBB
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
#define LLVM_EXTERNAL_VISIBILITY
Definition: Compiler.h:128
uint64_t Size
Symbol * Sym
Definition: ELF_riscv.cpp:479
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
IRTranslator LLVM IR MI
This file declares the MachineConstantPool class which is an abstract constant pool to keep track of ...
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
static bool printOperand(raw_ostream &OS, const SelectionDAG *G, const SDValue Value)
This file defines the SmallString class.
This file contains some functions that are useful when dealing with strings.
LLVM_EXTERNAL_VISIBILITY void LLVMInitializeXCoreAsmPrinter()
This file contains the declaration of the XCoreInstPrinter class, which is used to print XCore MCInst...
This class is intended to be used as a driving class for all asm writers.
Definition: AsmPrinter.h:86
virtual void emitInstruction(const MachineInstr *)
Targets should implement this to emit instructions.
Definition: AsmPrinter.h:561
virtual void emitGlobalVariable(const GlobalVariable *GV)
Emit the specified global variable to the .s file.
Definition: AsmPrinter.cpp:723
virtual void emitFunctionBodyStart()
Targets can override this to emit stuff before the first basic block in the function.
Definition: AsmPrinter.h:545
virtual bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo, const char *ExtraCode, raw_ostream &OS)
Print the specified operand of MI, an INLINEASM instruction, using the specified assembler variant as...
virtual void emitFunctionBodyEnd()
Targets can override this to emit stuff after the last basic block in the function.
Definition: AsmPrinter.h:549
virtual void emitFunctionEntryLabel()
EmitFunctionEntryLabel - Emit the label that is the entrypoint for the function.
virtual bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo, const char *ExtraCode, raw_ostream &OS)
Print the specified operand of MI, an INLINEASM instruction, using the specified assembler variant.
This is an important base class in LLVM.
Definition: Constant.h:42
A parsed version of the target data layout string in and methods for querying it.
Definition: DataLayout.h:63
bool hasLinkOnceLinkage() const
Definition: GlobalValue.h:515
bool hasExternalLinkage() const
Definition: GlobalValue.h:511
bool isThreadLocal() const
If the value is "Thread Local", its value isn't shared by the threads.
Definition: GlobalValue.h:263
LinkageTypes getLinkage() const
Definition: GlobalValue.h:546
bool hasWeakLinkage() const
Definition: GlobalValue.h:522
bool hasCommonLinkage() const
Definition: GlobalValue.h:532
@ PrivateLinkage
Like Internal, but omit from symbol table.
Definition: GlobalValue.h:60
@ CommonLinkage
Tentative definitions.
Definition: GlobalValue.h:62
@ InternalLinkage
Rename collisions when linking (static functions).
Definition: GlobalValue.h:59
@ LinkOnceAnyLinkage
Keep one copy of function when linking (inline)
Definition: GlobalValue.h:54
@ WeakODRLinkage
Same, but only replaced by something equivalent.
Definition: GlobalValue.h:57
@ ExternalLinkage
Externally visible function.
Definition: GlobalValue.h:52
@ WeakAnyLinkage
Keep one copy of named function when linking (weak)
Definition: GlobalValue.h:56
@ AppendingLinkage
Special purpose, only applies to global arrays.
Definition: GlobalValue.h:58
@ LinkOnceODRLinkage
Same, but only replaced by something equivalent.
Definition: GlobalValue.h:55
Type * getValueType() const
Definition: GlobalValue.h:296
const Constant * getInitializer() const
getInitializer - Return the initializer for this global variable.
bool hasInitializer() const
Definitions have initializers, declarations don't.
static const MCConstantExpr * create(int64_t Value, MCContext &Ctx, bool PrintInHex=false, unsigned SizeInBytes=0)
Definition: MCExpr.cpp:222
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:185
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:41
void print(raw_ostream &OS, const MCAsmInfo *MAI) const
print - Print the value to the stream OS.
Definition: MCSymbol.cpp:58
StringRef getName() const
getName - Get the symbol name.
Definition: MCSymbol.h:205
MCSymbol * getSymbol() const
Return the MCSymbol for this basic block.
const MachineJumpTableInfo * getJumpTableInfo() const
getJumpTableInfo - Return the jump table info object for the current function.
Representation of each machine instruction.
Definition: MachineInstr.h:69
const std::vector< MachineJumpTableEntry > & getJumpTables() const
MachineOperand class - Representation of each machine instruction operand.
int64_t getImm() const
MachineBasicBlock * getMBB() const
const BlockAddress * getBlockAddress() const
MachineOperandType getType() const
getType - Returns the MachineOperandType for this operand.
Register getReg() const
getReg - Returns the register number.
@ MO_Immediate
Immediate operand.
@ MO_ConstantPoolIndex
Address of indexed Constant in Constant Pool.
@ MO_GlobalAddress
Address of a global value.
@ MO_BlockAddress
Address of a basic block.
@ MO_MachineBasicBlock
MachineBasicBlock reference.
@ MO_Register
Register operand.
virtual StringRef getPassName() const
getPassName - Return a nice clean name for a pass.
Definition: Pass.cpp:81
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
Definition: SmallString.h:26
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:51
Primary interface to the complete machine description for the target machine.
Definition: TargetMachine.h:77
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
static const char * getRegisterName(MCRegister Reg)
This class is used to lower an MachineInstr into an MCInst.
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
A raw_ostream that writes to an SmallVector or SmallString.
Definition: raw_ostream.h:691
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ C
The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
Target & getTheXCoreTarget()
void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:167
@ MCSA_Weak
.weak
Definition: MCDirectives.h:45
@ MCSA_Global
.type _foo, @gnu_unique_object
Definition: MCDirectives.h:30
@ MCSA_ELF_TypeObject
.type _foo, STT_OBJECT # aka @object
Definition: MCDirectives.h:25
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition: Alignment.h:39
RegisterAsmPrinter - Helper template for registering a target specific assembly printer,...