LLVM 19.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"
26#include "llvm/MC/MCStreamer.h"
27#include "llvm/MC/MCSymbol.h"
28#include "llvm/MC/MCSymbolELF.h"
30
31using namespace llvm;
32
35 switch (Modifier) {
38 case XtensaCP::TPOFF:
40 }
41 report_fatal_error("Invalid XtensaCPModifier!");
42}
43
45 MCInst LoweredMI;
46 lowerToMCInst(MI, LoweredMI);
47 EmitToStreamer(*OutStreamer, LoweredMI);
48}
49
52 XtensaConstantPoolValue *ACPV = static_cast<XtensaConstantPoolValue *>(MCPV);
53 MCSymbol *MCSym;
54
55 assert(ACPV->isExtSymbol() && "unrecognized constant pool value");
56
57 XtensaConstantPoolSymbol *XtensaSym = cast<XtensaConstantPoolSymbol>(ACPV);
58 const char *Sym = XtensaSym->getSymbol();
59 std::string SymName(Sym);
60
61 if (XtensaSym->isPrivateLinkage())
62 SymName = ".L" + SymName;
63
64 MCSym = GetExternalSymbolSymbol(StringRef(SymName));
65 MCSymbol *LblSym = GetCPISymbol(ACPV->getLabelId());
66 auto *TS =
67 static_cast<XtensaTargetStreamer *>(OutStreamer->getTargetStreamer());
69
70 if (ACPV->getModifier() != XtensaCP::no_modifier) {
71 std::string SymName(MCSym->getName());
72 StringRef Modifier = ACPV->getModifierText();
73 SymName += Modifier;
74 MCSym = GetExternalSymbolSymbol(StringRef(SymName));
75 }
76
77 const MCExpr *Expr = MCSymbolRefExpr::create(MCSym, VK, OutContext);
78 TS->emitLiteral(LblSym, Expr, false);
79}
80
82 const MachineConstantPoolEntry &CPE, int i) {
85 static_cast<XtensaConstantPoolValue *>(CPE.Val.MachineCPVal);
86 ACPV->setLabelId(i);
88 } else {
89 MCSymbol *LblSym = GetCPISymbol(i);
90 auto *TS =
91 static_cast<XtensaTargetStreamer *>(OutStreamer->getTargetStreamer());
92 const Constant *C = CPE.Val.ConstVal;
93 const MCExpr *Value = nullptr;
94
95 Type *Ty = C->getType();
96 if (const auto *CFP = dyn_cast<ConstantFP>(C)) {
98 CFP->getValueAPF().bitcastToAPInt().getSExtValue(), OutContext);
99 } else if (const auto *CI = dyn_cast<ConstantInt>(C)) {
100 Value = MCConstantExpr::create(CI->getValue().getSExtValue(), OutContext);
101 } else if (isa<PointerType>(Ty)) {
103 } else {
104 llvm_unreachable("unexpected constant pool entry type");
105 }
106
107 TS->emitLiteral(LblSym, Value, false);
108 }
109}
110
111// EmitConstantPool - Print to the current output stream assembly
112// representations of the constants in the constant pool MCP. This is
113// used to print out constants which have been "spilled to memory" by
114// the code generator.
116 const Function &F = MF->getFunction();
117 const MachineConstantPool *MCP = MF->getConstantPool();
118 const std::vector<MachineConstantPoolEntry> &CP = MCP->getConstants();
119 if (CP.empty())
120 return;
121
122 OutStreamer->pushSection();
123
124 auto *TS =
125 static_cast<XtensaTargetStreamer *>(OutStreamer->getTargetStreamer());
127 TS->startLiteralSection(CS);
128
129 int CPIdx = 0;
130 for (const MachineConstantPoolEntry &CPE : CP) {
131 emitMachineConstantPoolEntry(CPE, CPIdx++);
132 }
133
134 OutStreamer->popSection();
135}
136
137MCSymbol *
139 // Create a symbol for the name.
140 return GetCPISymbol(MO.getIndex());
141}
142
146 unsigned Offset) const {
147 const MCSymbol *Symbol;
149
150 switch (MOTy) {
152 Symbol = getSymbol(MO.getGlobal());
153 Offset += MO.getOffset();
154 break;
156 Symbol = GetConstantPoolIndexSymbol(MO);
157 Offset += MO.getOffset();
158 break;
159 default:
160 report_fatal_error("<unknown operand type>");
161 }
162
163 const MCExpr *ME =
165 ME = XtensaMCExpr::create(ME, Kind, OutContext);
166
167 if (Offset) {
168 // Assume offset is never negative.
169 assert(Offset > 0);
170
171 const MCConstantExpr *OffsetExpr =
173 ME = MCBinaryExpr::createAdd(ME, OffsetExpr, OutContext);
174 }
175
176 return MCOperand::createExpr(ME);
177}
178
180 unsigned Offset) const {
182
183 switch (MOTy) {
185 // Ignore all implicit register operands.
186 if (MO.isImplicit())
187 break;
188 return MCOperand::createReg(MO.getReg());
190 return MCOperand::createImm(MO.getImm() + Offset);
192 break;
195 return LowerSymbolOperand(MO, MOTy, Offset);
196 default:
197 report_fatal_error("unknown operand type");
198 }
199
200 return MCOperand();
201}
202
204 MCInst &OutMI) const {
205 OutMI.setOpcode(MI->getOpcode());
206
207 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
208 const MachineOperand &MO = MI->getOperand(i);
209 MCOperand MCOp = lowerOperand(MO);
210
211 if (MCOp.isValid())
212 OutMI.addOperand(MCOp);
213 }
214}
215
218}
static MCSymbolRefExpr::VariantKind getModifierVariantKind(ARMCP::ARMCPModifier Modifier)
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
#define LLVM_EXTERNAL_VISIBILITY
Definition: Compiler.h:135
Symbol * Sym
Definition: ELF_riscv.cpp:479
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:398
MCSymbol * getSymbol(const GlobalValue *GV) const
Definition: AsmPrinter.cpp:700
void EmitToStreamer(MCStreamer &S, const MCInst &Inst)
Definition: AsmPrinter.cpp:418
TargetMachine & TM
Target machine description.
Definition: AsmPrinter.h:87
virtual MCSymbol * GetCPISymbol(unsigned CPID) const
Return the symbol for the specified constant pool entry.
MachineFunction * MF
The current machine function.
Definition: AsmPrinter.h:102
MCContext & OutContext
This is the context for the output file that we are streaming.
Definition: AsmPrinter.h:94
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:99
virtual const MCExpr * lowerConstant(const Constant *CV)
Lower the specified LLVM Constant to an MCExpr.
This is an important base class in LLVM.
Definition: Constant.h:41
static const MCBinaryExpr * createAdd(const MCExpr *LHS, const MCExpr *RHS, MCContext &Ctx)
Definition: MCExpr.h:536
static const MCConstantExpr * create(int64_t Value, MCContext &Ctx, bool PrintInHex=false, unsigned SizeInBytes=0)
Definition: MCExpr.cpp:194
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:35
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:39
static const MCSymbolRefExpr * create(const MCSymbol *Symbol, MCContext &Ctx)
Definition: MCExpr.h:397
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:40
StringRef getName() const
getName - Get the symbol name.
Definition: MCSymbol.h:205
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::@196 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
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_RegisterMask
Mask of preserved registers.
@ MO_Register
Register operand.
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...
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.
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:456
void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:156
Target & getTheXtensaTarget()
RegisterAsmPrinter - Helper template for registering a target specific assembly printer,...