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"
25#include "llvm/MC/MCExpr.h"
28#include "llvm/MC/MCStreamer.h"
29#include "llvm/MC/MCSymbol.h"
30#include "llvm/MC/MCSymbolELF.h"
32
33using namespace llvm;
34
37 switch (Modifier) {
40 case XtensaCP::TPOFF:
42 }
43 report_fatal_error("Invalid XtensaCPModifier!");
44}
45
47 unsigned Opc = MI->getOpcode();
48
49 switch (Opc) {
50 case Xtensa::BR_JT:
53 MCInstBuilder(Xtensa::JX).addReg(MI->getOperand(0).getReg()));
54 return;
55 default:
56 MCInst LoweredMI;
57 lowerToMCInst(MI, LoweredMI);
58 EmitToStreamer(*OutStreamer, LoweredMI);
59 return;
60 }
61}
62
65 XtensaConstantPoolValue *ACPV = static_cast<XtensaConstantPoolValue *>(MCPV);
66 MCSymbol *MCSym;
67
68 if (ACPV->isBlockAddress()) {
69 const BlockAddress *BA =
70 cast<XtensaConstantPoolConstant>(ACPV)->getBlockAddress();
71 MCSym = GetBlockAddressSymbol(BA);
72 } else if (ACPV->isMachineBasicBlock()) {
73 const MachineBasicBlock *MBB = cast<XtensaConstantPoolMBB>(ACPV)->getMBB();
74 MCSym = MBB->getSymbol();
75 } else if (ACPV->isJumpTable()) {
76 unsigned Idx = cast<XtensaConstantPoolJumpTable>(ACPV)->getIndex();
77 MCSym = this->GetJTISymbol(Idx, false);
78 } else {
79 assert(ACPV->isExtSymbol() && "unrecognized constant pool value");
80 XtensaConstantPoolSymbol *XtensaSym = cast<XtensaConstantPoolSymbol>(ACPV);
81 const char *SymName = XtensaSym->getSymbol();
82
83 if (XtensaSym->isPrivateLinkage()) {
84 const DataLayout &DL = getDataLayout();
85 MCSym = OutContext.getOrCreateSymbol(Twine(DL.getPrivateGlobalPrefix()) +
86 SymName);
87 } else {
88 MCSym = OutContext.getOrCreateSymbol(SymName);
89 }
90 }
91
92 MCSymbol *LblSym = GetCPISymbol(ACPV->getLabelId());
93 auto *TS =
94 static_cast<XtensaTargetStreamer *>(OutStreamer->getTargetStreamer());
96
97 if (ACPV->getModifier() != XtensaCP::no_modifier) {
98 std::string SymName(MCSym->getName());
99 StringRef Modifier = ACPV->getModifierText();
100 SymName += Modifier;
101 MCSym = OutContext.getOrCreateSymbol(SymName);
102 }
103
104 const MCExpr *Expr = MCSymbolRefExpr::create(MCSym, VK, OutContext);
105 TS->emitLiteral(LblSym, Expr, false);
106}
107
109 const MachineConstantPoolEntry &CPE, int i) {
110 if (CPE.isMachineConstantPoolEntry()) {
112 static_cast<XtensaConstantPoolValue *>(CPE.Val.MachineCPVal);
113 ACPV->setLabelId(i);
115 } else {
116 MCSymbol *LblSym = GetCPISymbol(i);
117 auto *TS =
118 static_cast<XtensaTargetStreamer *>(OutStreamer->getTargetStreamer());
119 const Constant *C = CPE.Val.ConstVal;
120 const MCExpr *Value = nullptr;
121
122 Type *Ty = C->getType();
123 if (const auto *CFP = dyn_cast<ConstantFP>(C)) {
125 CFP->getValueAPF().bitcastToAPInt().getSExtValue(), OutContext);
126 } else if (const auto *CI = dyn_cast<ConstantInt>(C)) {
127 Value = MCConstantExpr::create(CI->getValue().getSExtValue(), OutContext);
128 } else if (isa<PointerType>(Ty)) {
130 } else {
131 llvm_unreachable("unexpected constant pool entry type");
132 }
133
134 TS->emitLiteral(LblSym, Value, false);
135 }
136}
137
138// EmitConstantPool - Print to the current output stream assembly
139// representations of the constants in the constant pool MCP. This is
140// used to print out constants which have been "spilled to memory" by
141// the code generator.
143 const Function &F = MF->getFunction();
144 const MachineConstantPool *MCP = MF->getConstantPool();
145 const std::vector<MachineConstantPoolEntry> &CP = MCP->getConstants();
146 if (CP.empty())
147 return;
148
149 OutStreamer->pushSection();
150
151 auto *TS =
152 static_cast<XtensaTargetStreamer *>(OutStreamer->getTargetStreamer());
154 TS->startLiteralSection(CS);
155
156 int CPIdx = 0;
157 for (const MachineConstantPoolEntry &CPE : CP) {
158 emitMachineConstantPoolEntry(CPE, CPIdx++);
159 }
160
161 OutStreamer->popSection();
162}
163
165 raw_ostream &O) {
166 const MachineOperand &MO = MI->getOperand(OpNo);
167
168 switch (MO.getType()) {
171 MCOperand MC = lowerOperand(MI->getOperand(OpNo));
173 break;
174 }
175 default:
176 llvm_unreachable("unknown operand type");
177 }
178}
179
181 const char *ExtraCode, raw_ostream &O) {
182 // Print the operand if there is no operand modifier.
183 if (!ExtraCode || !ExtraCode[0]) {
184 printOperand(MI, OpNo, O);
185 return false;
186 }
187
188 // Fallback to the default implementation.
189 return AsmPrinter::PrintAsmOperand(MI, OpNo, ExtraCode, O);
190}
191
193 unsigned OpNo,
194 const char *ExtraCode,
195 raw_ostream &OS) {
196 if (ExtraCode && ExtraCode[0])
197 return true; // Unknown modifier.
198
199 assert(OpNo + 1 < MI->getNumOperands() && "Insufficient operands");
200
201 const MachineOperand &Base = MI->getOperand(OpNo);
202 const MachineOperand &Offset = MI->getOperand(OpNo + 1);
203
204 assert(Base.isReg() &&
205 "Unexpected base pointer for inline asm memory operand.");
206 assert(Offset.isImm() && "Unexpected offset for inline asm memory operand.");
207
209 OS << ", ";
210 OS << Offset.getImm();
211
212 return false;
213}
214
215MCSymbol *
217 // Create a symbol for the name.
218 return GetCPISymbol(MO.getIndex());
219}
220
222 return GetJTISymbol(MO.getIndex());
223}
224
228 unsigned Offset) const {
229 const MCSymbol *Symbol;
231
232 switch (MOTy) {
234 Symbol = getSymbol(MO.getGlobal());
235 Offset += MO.getOffset();
236 break;
238 Symbol = MO.getMBB()->getSymbol();
239 break;
242 Offset += MO.getOffset();
243 break;
246 Offset += MO.getOffset();
247 break;
249 Symbol = GetJumpTableSymbol(MO);
250 break;
252 Symbol = GetConstantPoolIndexSymbol(MO);
253 Offset += MO.getOffset();
254 break;
255 default:
256 report_fatal_error("<unknown operand type>");
257 }
258
259 const MCExpr *ME =
261 ME = XtensaMCExpr::create(ME, Kind, OutContext);
262
263 if (Offset) {
264 // Assume offset is never negative.
265 assert(Offset > 0);
266
267 const MCConstantExpr *OffsetExpr =
269 ME = MCBinaryExpr::createAdd(ME, OffsetExpr, OutContext);
270 }
271
272 return MCOperand::createExpr(ME);
273}
274
276 unsigned Offset) const {
278
279 switch (MOTy) {
281 // Ignore all implicit register operands.
282 if (MO.isImplicit())
283 break;
284 return MCOperand::createReg(MO.getReg());
286 return MCOperand::createImm(MO.getImm() + Offset);
288 break;
295 return LowerSymbolOperand(MO, MOTy, Offset);
296 default:
297 report_fatal_error("unknown operand type");
298 }
299
300 return MCOperand();
301}
302
304 MCInst &OutMI) const {
305 OutMI.setOpcode(MI->getOpcode());
306
307 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
308 const MachineOperand &MO = MI->getOperand(i);
309 MCOperand MCOp = lowerOperand(MO);
310
311 if (MCOp.isValid())
312 OutMI.addOperand(MCOp);
313 }
314}
315
318}
static MCSymbolRefExpr::VariantKind getModifierVariantKind(ARMCP::ARMCPModifier Modifier)
MachineBasicBlock & MBB
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
#define LLVM_EXTERNAL_VISIBILITY
Definition: Compiler.h:128
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())
raw_pwrite_stream & OS
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:408
MCSymbol * getSymbol(const GlobalValue *GV) const
Definition: AsmPrinter.cpp:701
void EmitToStreamer(MCStreamer &S, const MCInst &Inst)
Definition: AsmPrinter.cpp:428
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:412
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.
The address of a basic block.
Definition: Constants.h:893
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:537
static const MCConstantExpr * create(int64_t Value, MCContext &Ctx, bool PrintInHex=false, unsigned SizeInBytes=0)
Definition: MCExpr.cpp:222
MCSymbol * getOrCreateSymbol(const Twine &Name)
Lookup the symbol inside with the specified Name.
Definition: MCContext.cpp:212
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:185
void addOperand(const MCOperand Op)
Definition: MCInst.h:211
void setOpcode(unsigned Op)
Definition: MCInst.h:198
Instances of this class represent operands of the MCInst class.
Definition: MCInst.h:37
static MCOperand createExpr(const MCExpr *Val)
Definition: MCInst.h:163
static MCOperand createReg(MCRegister Reg)
Definition: MCInst.h:135
static MCOperand createImm(int64_t Val)
Definition: MCInst.h:142
bool isValid() const
Definition: MCInst.h:61
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:398
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:51
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
bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo, const char *ExtraCode, raw_ostream &O) override
Print the specified operand of MI, an INLINEASM instruction, using the specified assembler variant.
MCOperand LowerSymbolOperand(const MachineOperand &MO, MachineOperand::MachineOperandType MOTy, unsigned Offset) const
bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo, const char *ExtraCode, raw_ostream &OS) override
Print the specified operand of MI, an INLINEASM instruction, using the specified assembler variant as...
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
void printOperand(const MachineInstr *MI, int opNum, raw_ostream &O)
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 char * getRegisterName(MCRegister Reg)
static void printOperand(const MCOperand &MO, raw_ostream &O)
static const XtensaMCExpr * create(const MCExpr *Expr, VariantKind Kind, MCContext &Ctx)
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
#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,...