Skip to content

Commit 3b09a3d

Browse files
committed
MC,SPARC: Replace SparcMCExpr with MCSpecifierExpr
Add a hook printSpecifierExpr so that targets can implement relocation specifier printing without inheriting from MCSpecifierExpr.
1 parent 68b6f39 commit 3b09a3d

File tree

9 files changed

+38
-22
lines changed

9 files changed

+38
-22
lines changed

llvm/include/llvm/MC/MCAsmInfo.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ namespace llvm {
2828
class MCContext;
2929
class MCCFIInstruction;
3030
class MCExpr;
31+
class MCSpecifierExpr;
3132
class MCSection;
3233
class MCStreamer;
3334
class MCSubtargetInfo;
@@ -712,6 +713,7 @@ class LLVM_ABI MCAsmInfo {
712713
std::optional<uint32_t> getSpecifierForName(StringRef Name) const;
713714

714715
void printExpr(raw_ostream &, const MCExpr &) const;
716+
virtual void printSpecifierExpr(raw_ostream &, const MCSpecifierExpr &) const;
715717
};
716718

717719
} // end namespace llvm

llvm/include/llvm/MC/MCExpr.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ class MCExpr {
8282
/// \name Utility Methods
8383
/// @{
8484

85+
// TODO: Make this private. Users should call MCAsmInfo::printExpr instead.
8586
LLVM_ABI void print(raw_ostream &OS, const MCAsmInfo *MAI,
8687
int SurroundingPrec = 0) const;
8788
LLVM_ABI void dump() const;
@@ -509,7 +510,7 @@ class LLVM_ABI MCSpecifierExpr : public MCExpr {
509510
// Target-specific relocation specifier code
510511
const Spec specifier;
511512

512-
protected:
513+
public:
513514
explicit MCSpecifierExpr(const MCExpr *Expr, Spec S)
514515
: MCExpr(Specifier, SMLoc()), Expr(Expr), specifier(S) {}
515516
virtual ~MCSpecifierExpr() = default;
@@ -518,7 +519,9 @@ class LLVM_ABI MCSpecifierExpr : public MCExpr {
518519
Spec getSpecifier() const { return specifier; }
519520
const MCExpr *getSubExpr() const { return Expr; }
520521

521-
virtual void printImpl(raw_ostream &OS, const MCAsmInfo *MAI) const = 0;
522+
virtual void printImpl(raw_ostream &OS, const MCAsmInfo *MAI) const {
523+
llvm_unreachable("Replace MCExpr::print calls with MCAsmInfo::printExpr");
524+
}
522525
virtual bool evaluateAsRelocatableImpl(MCValue &Res,
523526
const MCAssembler *Asm) const;
524527

llvm/lib/MC/MCAsmInfo.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,5 +151,15 @@ std::optional<uint32_t> MCAsmInfo::getSpecifierForName(StringRef Name) const {
151151
}
152152

153153
void MCAsmInfo::printExpr(raw_ostream &OS, const MCExpr &Expr) const {
154-
Expr.print(OS, this);
154+
if (auto *SE = dyn_cast<MCSpecifierExpr>(&Expr))
155+
printSpecifierExpr(OS, *SE);
156+
else
157+
Expr.print(OS, this);
158+
}
159+
160+
void MCAsmInfo::printSpecifierExpr(raw_ostream &OS,
161+
const MCSpecifierExpr &Expr) const {
162+
// TODO: Switch to unreachable after all targets that use MCSpecifierExpr
163+
// migrate to MCAsmInfo::printSpecifierExpr.
164+
Expr.printImpl(OS, this);
155165
}

llvm/lib/MC/MCExpr.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,8 @@ void MCExpr::print(raw_ostream &OS, const MCAsmInfo *MAI,
174174
}
175175

176176
case MCExpr::Specifier:
177+
// TODO: Remove after all targets that use MCSpecifierExpr migrate to
178+
// MCAsmInfo::printSpecifierExpr.
177179
return cast<MCSpecifierExpr>(this)->printImpl(OS, MAI);
178180
}
179181

llvm/lib/Target/Sparc/MCTargetDesc/SparcInstPrinter.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
#include "SparcInstPrinter.h"
1414
#include "Sparc.h"
15+
#include "llvm/MC/MCAsmInfo.h"
1516
#include "llvm/MC/MCExpr.h"
1617
#include "llvm/MC/MCInst.h"
1718
#include "llvm/MC/MCSubtargetInfo.h"
@@ -142,7 +143,7 @@ void SparcInstPrinter::printOperand(const MCInst *MI, int opNum,
142143
}
143144

144145
assert(MO.isExpr() && "Unknown operand kind in printOperand");
145-
MO.getExpr()->print(O, &MAI);
146+
MAI.printExpr(O, *MO.getExpr());
146147
}
147148

148149
void SparcInstPrinter::printMemOperand(const MCInst *MI, int opNum,
@@ -288,5 +289,5 @@ void SparcInstPrinter::printCTILabel(const MCInst *MI, uint64_t Address,
288289
}
289290

290291
// Otherwise, just print the expression.
291-
Op.getExpr()->print(O, &MAI);
292+
MAI.printExpr(O, *Op.getExpr());
292293
}

llvm/lib/Target/Sparc/MCTargetDesc/SparcMCAsmInfo.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,13 @@ SparcELFMCAsmInfo::getExprForFDESymbol(const MCSymbol *Sym,
6666
}
6767
return MCAsmInfo::getExprForFDESymbol(Sym, Encoding, Streamer);
6868
}
69+
70+
void SparcELFMCAsmInfo::printSpecifierExpr(raw_ostream &OS,
71+
const MCSpecifierExpr &Expr) const {
72+
StringRef S = Sparc::getSpecifierName(Expr.getSpecifier());
73+
if (!S.empty())
74+
OS << '%' << S << '(';
75+
printExpr(OS, *Expr.getSubExpr());
76+
if (!S.empty())
77+
OS << ')';
78+
}

llvm/lib/Target/Sparc/MCTargetDesc/SparcMCAsmInfo.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ class SparcELFMCAsmInfo : public MCAsmInfoELF {
3232
unsigned Encoding,
3333
MCStreamer &Streamer) const override;
3434

35+
void printSpecifierExpr(raw_ostream &OS,
36+
const MCSpecifierExpr &Expr) const override;
3537
};
3638

3739
} // end namespace llvm

llvm/lib/Target/Sparc/MCTargetDesc/SparcMCExpr.cpp

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,12 @@ using namespace llvm;
2424

2525
const SparcMCExpr *Sparc::createSpecifierExpr(MCContext &Ctx,
2626
const MCExpr *Expr, uint16_t S) {
27-
return new (Ctx) SparcMCExpr(Expr, S);
27+
return new (Ctx) MCSpecifierExpr(Expr, S);
2828
}
2929

3030
const SparcMCExpr *Sparc::createSpecifierExpr(MCContext &Ctx,
3131
const MCSymbol *Sym, uint16_t S) {
32-
return new (Ctx) SparcMCExpr(MCSymbolRefExpr::create(Sym, Ctx), S);
33-
}
34-
35-
void SparcMCExpr::printImpl(raw_ostream &OS, const MCAsmInfo *MAI) const {
36-
StringRef S = Sparc::getSpecifierName(specifier);
37-
if (!S.empty())
38-
OS << '%' << S << '(';
39-
getSubExpr()->print(OS, MAI);
40-
if (!S.empty())
41-
OS << ')';
32+
return new (Ctx) MCSpecifierExpr(MCSymbolRefExpr::create(Sym, Ctx), S);
4233
}
4334

4435
StringRef Sparc::getSpecifierName(uint16_t S) {

llvm/lib/Target/Sparc/MCTargetDesc/SparcMCExpr.h

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,7 @@
2020
namespace llvm {
2121

2222
class StringRef;
23-
class SparcMCExpr : public MCSpecifierExpr {
24-
public:
25-
explicit SparcMCExpr(const MCExpr *Expr, uint16_t S)
26-
: MCSpecifierExpr(Expr, S) {}
27-
void printImpl(raw_ostream &OS, const MCAsmInfo *MAI) const override;
28-
};
23+
using SparcMCExpr = MCSpecifierExpr;
2924

3025
namespace Sparc {
3126
const SparcMCExpr *createSpecifierExpr(MCContext &Ctx, const MCExpr *Expr,

0 commit comments

Comments
 (0)