-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
0004-Xtensa-Implement-assembler-representation-of-the.patch
222 lines (214 loc) · 8.21 KB
/
0004-Xtensa-Implement-assembler-representation-of-the.patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
From a22235a1baccc4531da8ebf3a3b1b7c2ff95c073 Mon Sep 17 00:00:00 2001
From: Andrei Safronov <safronov@espressif.com>
Date: Wed, 5 Apr 2023 00:58:37 +0300
Subject: [PATCH 004/158] [Xtensa] Implement assembler representation of the
Constant Pool.
---
llvm/lib/Target/Xtensa/XtensaAsmPrinter.cpp | 170 ++++++++++++++++++++
llvm/lib/Target/Xtensa/XtensaAsmPrinter.h | 2 +
2 files changed, 172 insertions(+)
diff --git a/llvm/lib/Target/Xtensa/XtensaAsmPrinter.cpp b/llvm/lib/Target/Xtensa/XtensaAsmPrinter.cpp
index e633b77ff620..65a23506ad98 100644
--- a/llvm/lib/Target/Xtensa/XtensaAsmPrinter.cpp
+++ b/llvm/lib/Target/Xtensa/XtensaAsmPrinter.cpp
@@ -14,6 +14,7 @@
//===----------------------------------------------------------------------===//
#include "XtensaAsmPrinter.h"
+#include "XtensaConstantPoolValue.h"
#include "XtensaMCInstLower.h"
#include "TargetInfo/XtensaTargetInfo.h"
#include "llvm/BinaryFormat/ELF.h"
@@ -28,6 +29,17 @@
using namespace llvm;
+static MCSymbolRefExpr::VariantKind
+getModifierVariantKind(XtensaCP::XtensaCPModifier Modifier) {
+ switch (Modifier) {
+ case XtensaCP::no_modifier:
+ return MCSymbolRefExpr::VK_None;
+ case XtensaCP::TPOFF:
+ return MCSymbolRefExpr::VK_TPOFF;
+ }
+ llvm_unreachable("Invalid XtensaCPModifier!");
+}
+
void XtensaAsmPrinter::emitInstruction(const MachineInstr *MI) {
XtensaMCInstLower Lower(MF->getContext(), *this);
MCInst LoweredMI;
@@ -35,6 +47,164 @@ void XtensaAsmPrinter::emitInstruction(const MachineInstr *MI) {
EmitToStreamer(*OutStreamer, LoweredMI);
}
+/// EmitConstantPool - Print to the current output stream assembly
+/// representations of the constants in the constant pool MCP. This is
+/// used to print out constants which have been "spilled to memory" by
+/// the code generator.
+void XtensaAsmPrinter::emitConstantPool() {
+ const Function &F = MF->getFunction();
+ const MachineConstantPool *MCP = MF->getConstantPool();
+ const std::vector<MachineConstantPoolEntry> &CP = MCP->getConstants();
+ if (CP.empty())
+ return;
+
+ for (unsigned i = 0, e = CP.size(); i != e; ++i) {
+ const MachineConstantPoolEntry &CPE = CP[i];
+
+ if (i == 0) {
+ if (OutStreamer->hasRawTextSupport()) {
+ OutStreamer->switchSection(
+ getObjFileLowering().SectionForGlobal(&F, TM));
+ OutStreamer->emitRawText(StringRef("\t.literal_position\n"));
+ } else {
+ MCSectionELF *CS =
+ (MCSectionELF *)getObjFileLowering().SectionForGlobal(&F, TM);
+ std::string CSectionName = CS->getName().str();
+ std::size_t Pos = CSectionName.find(".text");
+ std::string SectionName;
+ if (Pos != std::string::npos) {
+ if (Pos > 0)
+ SectionName = CSectionName.substr(0, Pos + 5);
+ else
+ SectionName = "";
+ SectionName += ".literal";
+ SectionName += CSectionName.substr(Pos + 5);
+ } else {
+ SectionName = CSectionName;
+ SectionName += ".literal";
+ }
+
+ MCSectionELF *S =
+ OutContext.getELFSection(SectionName, ELF::SHT_PROGBITS,
+ ELF::SHF_EXECINSTR | ELF::SHF_ALLOC);
+ S->setAlignment(Align(4));
+ OutStreamer->switchSection(S);
+ }
+ }
+
+ if (CPE.isMachineConstantPoolEntry()) {
+ XtensaConstantPoolValue *ACPV =
+ static_cast<XtensaConstantPoolValue *>(CPE.Val.MachineCPVal);
+ ACPV->setLabelId(i);
+ emitMachineConstantPoolValue(CPE.Val.MachineCPVal);
+ } else {
+ MCSymbol *LblSym = GetCPISymbol(i);
+ // TODO find a better way to check whether we emit data to .s file
+ if (OutStreamer->hasRawTextSupport()) {
+ std::string str("\t.literal ");
+ str += LblSym->getName();
+ str += ", ";
+ const Constant *C = CPE.Val.ConstVal;
+
+ Type *Ty = C->getType();
+ if (const auto *CFP = dyn_cast<ConstantFP>(C)) {
+ str += toString(CFP->getValueAPF().bitcastToAPInt(), 10, true);
+ } else if (const auto *CI = dyn_cast<ConstantInt>(C)) {
+ str += toString(CI->getValue(), 10, true);
+ } else if (isa<PointerType>(Ty)) {
+ const MCExpr *ME = lowerConstant(C);
+ const MCSymbolRefExpr &SRE = cast<MCSymbolRefExpr>(*ME);
+ const MCSymbol &Sym = SRE.getSymbol();
+ str += Sym.getName();
+ } else {
+ unsigned NumElements;
+ if (isa<VectorType>(Ty))
+ NumElements = (cast<FixedVectorType>(Ty))->getNumElements();
+ else
+ NumElements = Ty->getArrayNumElements();
+
+ for (unsigned I = 0; I < NumElements; I++) {
+ const Constant *CAE = C->getAggregateElement(I);
+ if (I > 0)
+ str += ", ";
+ if (const auto *CFP = dyn_cast<ConstantFP>(CAE)) {
+ str += toString(CFP->getValueAPF().bitcastToAPInt(), 10, true);
+ } else if (const auto *CI = dyn_cast<ConstantInt>(CAE)) {
+ str += toString(CI->getValue(), 10, true);
+ }
+ }
+ }
+
+ OutStreamer->emitRawText(StringRef(str));
+ } else {
+ OutStreamer->emitLabel(LblSym);
+ emitGlobalConstant(getDataLayout(), CPE.Val.ConstVal);
+ }
+ }
+ }
+}
+
+void XtensaAsmPrinter::emitMachineConstantPoolValue(
+ MachineConstantPoolValue *MCPV) {
+ XtensaConstantPoolValue *ACPV = static_cast<XtensaConstantPoolValue *>(MCPV);
+
+ MCSymbol *MCSym;
+ if (ACPV->isBlockAddress()) {
+ const BlockAddress *BA =
+ cast<XtensaConstantPoolConstant>(ACPV)->getBlockAddress();
+ MCSym = GetBlockAddressSymbol(BA);
+ } else if (ACPV->isGlobalValue()) {
+ const GlobalValue *GV = cast<XtensaConstantPoolConstant>(ACPV)->getGV();
+ // TODO some modifiers
+ MCSym = getSymbol(GV);
+ } else if (ACPV->isMachineBasicBlock()) {
+ const MachineBasicBlock *MBB = cast<XtensaConstantPoolMBB>(ACPV)->getMBB();
+ MCSym = MBB->getSymbol();
+ } else if (ACPV->isJumpTable()) {
+ unsigned idx = cast<XtensaConstantPoolJumpTable>(ACPV)->getIndex();
+ MCSym = this->GetJTISymbol(idx, false);
+ } else {
+ assert(ACPV->isExtSymbol() && "unrecognized constant pool value");
+ XtensaConstantPoolSymbol *XtensaSym = cast<XtensaConstantPoolSymbol>(ACPV);
+ const char *Sym = XtensaSym->getSymbol();
+ // TODO it's a trick to distinguish static references and generated rodata
+ // references Some clear method required
+ {
+ std::string SymName(Sym);
+ if (XtensaSym->isPrivateLinkage())
+ SymName = ".L" + SymName;
+ MCSym = GetExternalSymbolSymbol(StringRef(SymName));
+ }
+ }
+
+ MCSymbol *LblSym = GetCPISymbol(ACPV->getLabelId());
+ // TODO find a better way to check whether we emit data to .s file
+ if (OutStreamer->hasRawTextSupport()) {
+ std::string SymName("\t.literal ");
+ SymName += LblSym->getName();
+ SymName += ", ";
+ SymName += MCSym->getName();
+
+ StringRef Modifier = ACPV->getModifierText();
+ SymName += Modifier;
+
+ OutStreamer->emitRawText(StringRef(SymName));
+ } else {
+ MCSymbolRefExpr::VariantKind VK =
+ getModifierVariantKind(ACPV->getModifier());
+
+ if (ACPV->getModifier() != XtensaCP::no_modifier) {
+ std::string SymName(MCSym->getName());
+ MCSym = GetExternalSymbolSymbol(StringRef(SymName));
+ }
+
+ const MCExpr *Expr = MCSymbolRefExpr::create(MCSym, VK, OutContext);
+ uint64_t Size = getDataLayout().getTypeAllocSize(ACPV->getType());
+ OutStreamer->emitLabel(LblSym);
+ OutStreamer->emitValue(Expr, Size);
+ }
+}
+
// Force static initialization.
extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeXtensaAsmPrinter() {
RegisterAsmPrinter<XtensaAsmPrinter> A(getTheXtensaTarget());
diff --git a/llvm/lib/Target/Xtensa/XtensaAsmPrinter.h b/llvm/lib/Target/Xtensa/XtensaAsmPrinter.h
index d23fa5dbeadb..0ba80bc18c1c 100644
--- a/llvm/lib/Target/Xtensa/XtensaAsmPrinter.h
+++ b/llvm/lib/Target/Xtensa/XtensaAsmPrinter.h
@@ -36,6 +36,8 @@ public:
// Override AsmPrinter.
StringRef getPassName() const override { return "Xtensa Assembly Printer"; }
void emitInstruction(const MachineInstr *MI) override;
+ void emitConstantPool() override;
+ void emitMachineConstantPoolValue(MachineConstantPoolValue *MCPV) override;
};
} // end namespace llvm
--
2.40.1