Skip to content

Commit 75f6fe2

Browse files
committed
[MC] Remove unneeded MCSymbolRefExpr::create overload and add comments
The StringRef overload is often error-prone as users might forget to register the MCSymbol. Add comments to MCTargetExpr and MCSymbolRefExpr::VariantKind. In the distant future the VariantKind parameter might be removed.
1 parent a6ccda2 commit 75f6fe2

File tree

5 files changed

+19
-22
lines changed

5 files changed

+19
-22
lines changed

llvm/include/llvm/MC/MCExpr.h

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,11 @@ class MCConstantExpr : public MCExpr {
191191
/// of the symbol as external.
192192
class MCSymbolRefExpr : public MCExpr {
193193
public:
194+
// VariantKind isn't ideal for encoding relocation operators because:
195+
// (a) other expressions, like MCConstantExpr (e.g., 4@l) and MCBinaryExpr
196+
// (e.g., (a+1)@l), also need it; (b) semantics become unclear (e.g., folding
197+
// expressions with @). MCTargetExpr, as used by AArch64 and RISC-V, offers a
198+
// cleaner approach.
194199
enum VariantKind : uint16_t {
195200
VK_None,
196201
VK_Invalid,
@@ -384,14 +389,13 @@ class MCSymbolRefExpr : public MCExpr {
384389
/// \name Construction
385390
/// @{
386391

387-
static const MCSymbolRefExpr *create(const MCSymbol *Symbol, MCContext &Ctx) {
388-
return MCSymbolRefExpr::create(Symbol, VK_None, Ctx);
392+
static const MCSymbolRefExpr *create(const MCSymbol *Symbol, MCContext &Ctx,
393+
SMLoc Loc = SMLoc()) {
394+
return MCSymbolRefExpr::create(Symbol, VK_None, Ctx, Loc);
389395
}
390396

391397
static const MCSymbolRefExpr *create(const MCSymbol *Symbol, VariantKind Kind,
392398
MCContext &Ctx, SMLoc Loc = SMLoc());
393-
static const MCSymbolRefExpr *create(StringRef Name, VariantKind Kind,
394-
MCContext &Ctx);
395399

396400
/// @}
397401
/// \name Accessors
@@ -630,8 +634,10 @@ class MCBinaryExpr : public MCExpr {
630634
}
631635
};
632636

633-
/// This is an extension point for target-specific MCExpr subclasses to
634-
/// implement.
637+
/// Extension point for target-specific MCExpr subclasses to implement.
638+
/// This can encode a relocation operator, serving as a replacement for
639+
/// MCSymbolRefExpr::VariantKind. Ideally, limit this to
640+
/// top-level use, avoiding its inclusion as a subexpression.
635641
///
636642
/// NOTE: All subclasses are required to have trivial destructors because
637643
/// MCExprs are bump pointer allocated and not destructed.

llvm/lib/MC/MCExpr.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -246,11 +246,6 @@ const MCSymbolRefExpr *MCSymbolRefExpr::create(const MCSymbol *Sym,
246246
return new (Ctx) MCSymbolRefExpr(Sym, Kind, Ctx.getAsmInfo(), Loc);
247247
}
248248

249-
const MCSymbolRefExpr *MCSymbolRefExpr::create(StringRef Name, VariantKind Kind,
250-
MCContext &Ctx) {
251-
return create(Ctx.getOrCreateSymbol(Name), Kind, Ctx);
252-
}
253-
254249
/* *** */
255250

256251
void MCTargetExpr::anchor() {}

llvm/lib/Target/ARM/AsmParser/ARMAsmParser.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12438,9 +12438,9 @@ bool ARMAsmParser::parseDirectiveTLSDescSeq(SMLoc L) {
1243812438
if (getLexer().isNot(AsmToken::Identifier))
1243912439
return TokError("expected variable after '.tlsdescseq' directive");
1244012440

12441-
const MCSymbolRefExpr *SRE =
12442-
MCSymbolRefExpr::create(Parser.getTok().getIdentifier(),
12443-
MCSymbolRefExpr::VK_ARM_TLSDESCSEQ, getContext());
12441+
auto *Sym = getContext().getOrCreateSymbol(Parser.getTok().getIdentifier());
12442+
const auto *SRE = MCSymbolRefExpr::create(
12443+
Sym, MCSymbolRefExpr::VK_ARM_TLSDESCSEQ, getContext());
1244412444
Lex();
1244512445

1244612446
if (parseEOL())

llvm/lib/Target/Mips/MCTargetDesc/MipsTargetStreamer.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1269,9 +1269,7 @@ void MipsTargetELFStreamer::emitDirectiveCpLoad(unsigned RegNo) {
12691269
TmpInst.setOpcode(Mips::LUi);
12701270
TmpInst.addOperand(MCOperand::createReg(GPReg));
12711271
const MCExpr *HiSym = MipsMCExpr::create(
1272-
MipsMCExpr::MEK_HI,
1273-
MCSymbolRefExpr::create("_gp_disp", MCSymbolRefExpr::VK_None,
1274-
MCA.getContext()),
1272+
MipsMCExpr::MEK_HI, MCSymbolRefExpr::create(GP_Disp, MCA.getContext()),
12751273
MCA.getContext());
12761274
TmpInst.addOperand(MCOperand::createExpr(HiSym));
12771275
getStreamer().emitInstruction(TmpInst, STI);
@@ -1282,9 +1280,7 @@ void MipsTargetELFStreamer::emitDirectiveCpLoad(unsigned RegNo) {
12821280
TmpInst.addOperand(MCOperand::createReg(GPReg));
12831281
TmpInst.addOperand(MCOperand::createReg(GPReg));
12841282
const MCExpr *LoSym = MipsMCExpr::create(
1285-
MipsMCExpr::MEK_LO,
1286-
MCSymbolRefExpr::create("_gp_disp", MCSymbolRefExpr::VK_None,
1287-
MCA.getContext()),
1283+
MipsMCExpr::MEK_LO, MCSymbolRefExpr::create(GP_Disp, MCA.getContext()),
12881284
MCA.getContext());
12891285
TmpInst.addOperand(MCOperand::createExpr(LoSym));
12901286
getStreamer().emitInstruction(TmpInst, STI);

llvm/lib/Target/PowerPC/AsmParser/PPCAsmParser.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1539,8 +1539,8 @@ bool PPCAsmParser::parseOperand(OperandVector &Operands) {
15391539
if (!(parseOptionalToken(AsmToken::Identifier) &&
15401540
Tok.getString().compare_insensitive("plt") == 0))
15411541
return Error(Tok.getLoc(), "expected 'plt'");
1542-
EVal = MCSymbolRefExpr::create(TlsGetAddr, MCSymbolRefExpr::VK_PLT,
1543-
getContext());
1542+
EVal = MCSymbolRefExpr::create(getContext().getOrCreateSymbol(TlsGetAddr),
1543+
MCSymbolRefExpr::VK_PLT, getContext());
15441544
if (parseOptionalToken(AsmToken::Plus)) {
15451545
const MCExpr *Addend = nullptr;
15461546
SMLoc EndLoc;

0 commit comments

Comments
 (0)