LLVM 20.0.0git
XtensaAsmPrinter.cpp
Go to the documentation of this file.
1//===- XtensaAsmPrinter.cpp Xtensa LLVM Assembly Printer ------------------===//
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 GAS-format Xtensa assembly language.
11//
12//===----------------------------------------------------------------------===//
13
14#include "XtensaAsmPrinter.h"
24#include "llvm/MC/MCExpr.h"
27#include "llvm/MC/MCStreamer.h"
28#include "llvm/MC/MCSymbol.h"
29#include "llvm/MC/MCSymbolELF.h"
31
32using namespace llvm;
33
36 switch (Modifier) {
39 case XtensaCP::TPOFF:
41 }
42 report_fatal_error("Invalid XtensaCPModifier!");
43}
44
46 unsigned Opc = MI->getOpcode();
47
48 switch (Opc) {
49 case Xtensa::BR_JT:
52 MCInstBuilder(Xtensa::JX).addReg(MI->getOperand(0).getReg()));
53 return;
54 default:
55 MCInst LoweredMI;
56 lowerToMCInst(MI, LoweredMI);
57 EmitToStreamer(*OutStreamer, LoweredMI);
58 return;
59 }
60}
61
64 XtensaConstantPoolValue *ACPV = static_cast<XtensaConstantPoolValue *>(MCPV);
65 MCSymbol *MCSym;
66
67 if (ACPV->isBlockAddress()) {
68 const BlockAddress *BA =
69 cast<XtensaConstantPoolConstant>(ACPV)->getBlockAddress();
70 MCSym = GetBlockAddressSymbol(BA);
71 } else if (ACPV->isJumpTable()) {
72 unsigned Idx = cast<XtensaConstantPoolJumpTable>(ACPV)->getIndex();
73 MCSym = this->GetJTISymbol(Idx, false);
74 } else {
75 assert(ACPV->isExtSymbol() && "unrecognized constant pool value");
76 XtensaConstantPoolSymbol *XtensaSym = cast<XtensaConstantPoolSymbol>(ACPV);
77 const char *SymName = XtensaSym->getSymbol();
78
79 if (XtensaSym->isPrivateLinkage()) {
80 const DataLayout &DL = getDataLayout();
81 MCSym = OutContext.getOrCreateSymbol(Twine(DL.getPrivateGlobalPrefix()) +
82 SymName);
83 } else {
84 MCSym = OutContext.getOrCreateSymbol(SymName);
85 }
86 }
87
88 MCSymbol *LblSym = GetCPISymbol(ACPV->getLabelId());
89 auto *TS =
90 static_cast<XtensaTargetStreamer *>(OutStreamer->getTargetStreamer());
92
93 if (ACPV->getModifier() != XtensaCP::no_modifier) {
94 std::string SymName(MCSym->getName());
95 StringRef Modifier = ACPV->getModifierText();
96 SymName += Modifier;
97 MCSym = OutContext.getOrCreateSymbol(SymName);
98 }
99
100 const MCExpr *Expr = MCSymbolRefExpr::create(MCSym, VK, OutContext);
101 TS->emitLiteral(LblSym, Expr, false);
102}
103
105 const MachineConstantPoolEntry &CPE, int i) {
106 if (CPE.isMachineConstantPoolEntry()) {
108 static_cast<XtensaConstantPoolValue *>(CPE.Val.MachineCPVal);
109 ACPV->setLabelId(i);
111 } else {
112 MCSymbol *LblSym = GetCPISymbol(i);
113 auto *TS =
114 static_cast<XtensaTargetStreamer *>(OutStreamer->getTargetStreamer());
115 const Constant *C = CPE.Val.ConstVal;
116 const MCExpr *Value = nullptr;
117
118 Type *Ty = C->getType();
119 if (const auto *CFP = dyn_cast<ConstantFP>(C)) {
121 CFP->getValueAPF().bitcastToAPInt().getSExtValue(), OutContext);
122 } else if (const auto *CI = dyn_cast<ConstantInt>(C)) {
123 Value = MCConstantExpr::create(CI->getValue().getSExtValue(), OutContext);
124 } else if (isa<PointerType>(Ty)) {
126 } else {
127 llvm_unreachable("unexpected constant pool entry type");
128 }
129
130 TS->emitLiteral(LblSym, Value, false);
131 }
132}
133
134// EmitConstantPool - Print to the current output stream assembly
135// representations of the constants in the constant pool MCP. This is
136// used to print out constants which have been "spilled to memory" by
137// the code generator.
139 const Function &F = MF->getFunction();
140 const MachineConstantPool *MCP = MF->getConstantPool();
141 const std::vector<MachineConstantPoolEntry> &CP = MCP->getConstants();
142 if (CP.empty())
143 return;
144
145 OutStreamer->pushSection();
146
147 auto *TS =
148 static_cast<XtensaTargetStreamer *>(OutStreamer->getTargetStreamer());
150 TS->startLiteralSection(CS);
151
152 int CPIdx = 0;
153 for (const MachineConstantPoolEntry &CPE : CP) {
154 emitMachineConstantPoolEntry(CPE, CPIdx++);
155 }
156
157 OutStreamer->popSection();
158}
159
160MCSymbol *
162 // Create a symbol for the name.
163 return GetCPISymbol(MO.getIndex());
164}
165
167 return GetJTISymbol(MO.getIndex());
168}
169
173 unsigned Offset) const {
174 const MCSymbol *Symbol;
176
177 switch (MOTy) {
179 Symbol = getSymbol(MO.getGlobal());
180 Offset += MO.getOffset();
181 break;
183 Symbol = MO.getMBB()->getSymbol();
184 break;
187 Offset += MO.getOffset();
188 break;
191 Offset += MO.getOffset();
192 break;
194 Symbol = GetJumpTableSymbol(MO);
195 break;
197 Symbol = GetConstantPoolIndexSymbol(MO);
198 Offset += MO.getOffset();
199 break;
200 default:
201 report_fatal_error("<unknown operand type>");
202 }
203
204 const MCExpr *ME =
206 ME = XtensaMCExpr::create(ME, Kind, OutContext);
207
208 if (Offset) {
209 // Assume offset is never negative.
210 assert(Offset > 0);
211
212 const MCConstantExpr *OffsetExpr =
214 ME = MCBinaryExpr::createAdd(ME, OffsetExpr, OutContext);
215 }
216
217 return MCOperand::createExpr(ME);
218}
219
221 unsigned Offset) const {
223
224 switch (MOTy) {
226 // Ignore all implicit register operands.
227 if (MO.isImplicit())
228 break;
229 return MCOperand::createReg(MO.getReg());
231 return MCOperand::createImm(MO.getImm() + Offset);
233 break;
240 return LowerSymbolOperand(MO, MOTy, Offset);
241 default:
242 report_fatal_error("unknown operand type");
243 }
244
245 return MCOperand();
246}
247
249 MCInst &OutMI) const {
250 OutMI.setOpcode(MI->getOpcode());
251
252 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
253 const MachineOperand &MO = MI->getOperand(i);
254 MCOperand MCOp = lowerOperand(MO);
255
256 if (MCOp.isValid())
257 OutMI.addOperand(MCOp);
258 }
259}
260
263}
static MCSymbolRefExpr::VariantKind getModifierVariantKind(ARMCP::ARMCPModifier Modifier)
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
#define LLVM_EXTERNAL_VISIBILITY
Definition: Compiler.h:131
Returns the sub type a function will return at a given Idx Should correspond to the result type of an ExtractValue instruction executed with just that one unsigned Idx
IRTranslator LLVM IR MI
#define F(x, y, z)
Definition: MD5.cpp:55
This file declares the MachineConstantPool class which is an abstract constant pool to keep track of ...
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file contains some functions that are useful when dealing with strings.
LLVM_EXTERNAL_VISIBILITY void LLVMInitializeXtensaAsmPrinter()
static MCSymbolRefExpr::VariantKind getModifierVariantKind(XtensaCP::XtensaCPModifier Modifier)
const TargetLoweringObjectFile & getObjFileLowering() const
Return information about object file lowering.
Definition: AsmPrinter.cpp:383
MCSymbol * getSymbol(const GlobalValue *GV) const
Definition: AsmPrinter.cpp:676
void EmitToStreamer(MCStreamer &S, const MCInst &Inst)
Definition: AsmPrinter.cpp:403
TargetMachine & TM
Target machine description.
Definition: AsmPrinter.h:89
virtual MCSymbol * GetCPISymbol(unsigned CPID) const
Return the symbol for the specified constant pool entry.
MachineFunction * MF
The current machine function.
Definition: AsmPrinter.h:104
MCSymbol * GetJTISymbol(unsigned JTID, bool isLinkerPrivate=false) const
Return the symbol for the specified jump table entry.
MCContext & OutContext
This is the context for the output file that we are streaming.
Definition: AsmPrinter.h:96
MCSymbol * GetExternalSymbolSymbol(Twine Sym) const
Return the MCSymbol for the specified ExternalSymbol.
std::unique_ptr< MCStreamer > OutStreamer
This is the MCStreamer object for the file we are generating.
Definition: AsmPrinter.h:101
virtual const MCExpr * lowerConstant(const Constant *CV)
Lower the specified LLVM Constant to an MCExpr.
MCSymbol * GetBlockAddressSymbol(const BlockAddress *BA) const
Return the MCSymbol used to satisfy BlockAddress uses of the specified basic block.
const DataLayout & getDataLayout() const
Return information about data layout.
Definition: AsmPrinter.cpp:387
The address of a basic block.
Definition: Constants.h:890
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
static const MCBinaryExpr * createAdd(const MCExpr *LHS, const MCExpr *RHS, MCContext &Ctx)
Definition: MCExpr.h:532
static const MCConstantExpr * create(int64_t Value, MCContext &Ctx, bool PrintInHex=false, unsigned SizeInBytes=0)
Definition: MCExpr.cpp:193
MCSymbol * getOrCreateSymbol(const Twine &Name)
Lookup the symbol inside with the specified Name.
Definition: MCContext.cpp:213
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:34
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:184
void addOperand(const MCOperand Op)
Definition: MCInst.h:210
void setOpcode(unsigned Op)
Definition: MCInst.h:197
Instances of this class represent operands of the MCInst class.
Definition: MCInst.h:36
static MCOperand createReg(unsigned Reg)
Definition: MCInst.h:134
static MCOperand createExpr(const MCExpr *Val)
Definition: MCInst.h:162
static MCOperand createImm(int64_t Val)
Definition: MCInst.h:141
bool isValid() const
Definition: MCInst.h:60
Instances of this class represent a uniqued identifier for a section in the current translation unit.
Definition: MCSection.h:36
static const MCSymbolRefExpr * create(const MCSymbol *Symbol, MCContext &Ctx)
Definition: MCExpr.h:393
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:41
StringRef getName() const
getName - Get the symbol name.
Definition: MCSymbol.h:205
MCSymbol * getSymbol() const
Return the MCSymbol for this basic block.
This class is a data container for one entry in a MachineConstantPool.
bool isMachineConstantPoolEntry() const
isMachineConstantPoolEntry - Return true if the MachineConstantPoolEntry is indeed a target specific ...
union llvm::MachineConstantPoolEntry::@204 Val
The constant itself.
MachineConstantPoolValue * MachineCPVal
Abstract base class for all machine specific constantpool value subclasses.
The MachineConstantPool class keeps track of constants referenced by a function which must be spilled...
const std::vector< MachineConstantPoolEntry > & getConstants() const
Function & getFunction()
Return the LLVM function that this machine code represents.
MachineConstantPool * getConstantPool()
getConstantPool - Return the constant pool object for the current function.
Representation of each machine instruction.
Definition: MachineInstr.h:69
MachineOperand class - Representation of each machine instruction operand.
const GlobalValue * getGlobal() const
int64_t getImm() const
bool isImplicit() const
MachineBasicBlock * getMBB() const
const BlockAddress * getBlockAddress() const
MachineOperandType getType() const
getType - Returns the MachineOperandType for this operand.
const char * getSymbolName() const
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_RegisterMask
Mask of preserved registers.
@ MO_BlockAddress
Address of a basic block.
@ MO_MachineBasicBlock
MachineBasicBlock reference.
@ MO_Register
Register operand.
@ MO_ExternalSymbol
Name of external global symbol.
@ MO_JumpTableIndex
Address of indexed Jump Table for switch.
int64_t getOffset() const
Return the offset from the symbol in this operand.
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
MCSection * SectionForGlobal(const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const
This method computes the appropriate section to emit the specified global variable or function defini...
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
LLVM Value Representation.
Definition: Value.h:74
MCOperand LowerSymbolOperand(const MachineOperand &MO, MachineOperand::MachineOperandType MOTy, unsigned Offset) const
void emitMachineConstantPoolEntry(const MachineConstantPoolEntry &CPE, int i)
void emitConstantPool() override
Print to the current output stream assembly representations of the constants in the constant pool MCP...
void lowerToMCInst(const MachineInstr *MI, MCInst &OutMI) const
MCSymbol * GetConstantPoolIndexSymbol(const MachineOperand &MO) const
void emitInstruction(const MachineInstr *MI) override
Targets should implement this to emit instructions.
MCSymbol * GetJumpTableSymbol(const MachineOperand &MO) const
void emitMachineConstantPoolValue(MachineConstantPoolValue *MCPV) override
MCOperand lowerOperand(const MachineOperand &MO, unsigned Offset=0) const
XtensaConstantPoolSymbol - Xtensa-specific constantpool values for external symbols.
XtensaConstantPoolValue - Xtensa specific constantpool value.
XtensaCP::XtensaCPModifier getModifier() const
static const XtensaMCExpr * create(const MCExpr *Expr, VariantKind Kind, MCContext &Ctx)
#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
@ Offset
Definition: DWP.cpp:480
void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:167
Target & getTheXtensaTarget()
RegisterAsmPrinter - Helper template for registering a target specific assembly printer,...