aboutsummaryrefslogtreecommitdiff
path: root/llvm/lib/MC/MCExpr.cpp
diff options
context:
space:
mode:
authorFangrui Song <i@maskray.me>2025-04-05 21:02:08 -0700
committerFangrui Song <i@maskray.me>2025-04-05 21:02:08 -0700
commitd5893fc2a7e1191afdb4940469ec9371a319b114 (patch)
treee3da9db8b29f4a28022330cc31858feddd79aa42 /llvm/lib/MC/MCExpr.cpp
parent4182d2dcb5ecbfc34d41a6cd11810cd36844eddb (diff)
downloadllvm-d5893fc2a7e1191afdb4940469ec9371a319b114.zip
llvm-d5893fc2a7e1191afdb4940469ec9371a319b114.tar.gz
llvm-d5893fc2a7e1191afdb4940469ec9371a319b114.tar.bz2
MCValue: Replace MCSymbolRefExpr members with MCSymbol
Commit 0999cbd0b9ed8aa893cce10d681dec6d54b200ad (2014) introduced `MCValue::RefKind` for AArch64 ELF as a clean approach to encode the relocation specifier. Following numerous migration commits, direct references to getSymA and getSymB have been eliminated. This allows us to seamlessly update SymA and SymB, replacing MCSymbolRefExpr with MCSymbol. Removeing reliance on a MCAssembler::evaluateFixup hack (`if (Target.SymSpecifier || SA.isUndefined()) {` (previosuly `if (A->getKind() != MCSymbolRefExpr::VK_None || SA.isUndefined()) {`)) requires 38c3ad36be1facbe6db2dede7e93c0f12fb4e1dc and 4182d2dcb5ecbfc34d41a6cd11810cd36844eddb Revert the temporary RISCV/LoongArch workaround (7e62715e0cd433ed97749549c6582c4e1aa689a3) during migration. MCAssembler::evaluateFixup needs an extra `!Add->isAbsolute()` case to support `movq abs@GOTPCREL(%rip), %rax; abs = 42` in llvm/test/MC/ELF/relocation-alias.s (ELFObjectWriter::isSymbolRefDifferenceFullyResolvedImpl asserts if called on an absolute symbol).
Diffstat (limited to 'llvm/lib/MC/MCExpr.cpp')
-rw-r--r--llvm/lib/MC/MCExpr.cpp47
1 files changed, 21 insertions, 26 deletions
diff --git a/llvm/lib/MC/MCExpr.cpp b/llvm/lib/MC/MCExpr.cpp
index e3d4c5c..c856ef5 100644
--- a/llvm/lib/MC/MCExpr.cpp
+++ b/llvm/lib/MC/MCExpr.cpp
@@ -436,22 +436,21 @@ static void attemptToFoldSymbolOffsetDifference(const MCAssembler *Asm,
// early.
bool MCExpr::evaluateSymbolicAdd(const MCAssembler *Asm,
const SectionAddrMap *Addrs, bool InSet,
- const MCValue &LHS,
- const MCSymbolRefExpr *RhsAdd,
- const MCSymbolRefExpr *RhsSub, int64_t RHS_Cst,
- uint32_t RhsSpec, MCValue &Res) {
+ const MCValue &LHS, const MCValue &RHS,
+ MCValue &Res) {
const MCSymbol *LHS_A = LHS.getAddSym();
const MCSymbol *LHS_B = LHS.getSubSym();
int64_t LHS_Cst = LHS.getConstant();
- const MCSymbol *RHS_A = RhsAdd ? &RhsAdd->getSymbol() : nullptr;
- const MCSymbol *RHS_B = RhsSub ? &RhsSub->getSymbol() : nullptr;
+ const MCSymbol *RHS_A = RHS.getAddSym();
+ const MCSymbol *RHS_B = RHS.getSubSym();
+ int64_t RHS_Cst = RHS.getConstant();
// Fold the result constant immediately.
int64_t Result_Cst = LHS_Cst + RHS_Cst;
// If we have a layout, we can fold resolved differences.
- if (Asm && !LHS.getSpecifier() && !RhsSpec) {
+ if (Asm && !LHS.getSpecifier() && !RHS.getSpecifier()) {
// While LHS_A-LHS_B and RHS_A-RHS_B from recursive calls have already been
// folded, reassociating terms in
// Result = (LHS_A - LHS_B + LHS_Cst) + (RHS_A - RHS_B + RHS_Cst).
@@ -472,16 +471,12 @@ bool MCExpr::evaluateSymbolicAdd(const MCAssembler *Asm,
// At this point, we have at most one additive symbol and one subtractive
// symbol -- find them.
- auto *A = LHS_A ? LHS.getSymA() : RHS_A ? RhsAdd : nullptr;
- auto *B = LHS_B ? LHS.getSymB() : RHS_B ? RhsSub : nullptr;
- if (B && B->getKind() != MCSymbolRefExpr::VK_None)
- return false;
+ auto *A = LHS_A ? LHS_A : RHS_A;
+ auto *B = LHS_B ? LHS_B : RHS_B;
auto Spec = LHS.getSpecifier();
if (!Spec)
- Spec = RhsSpec;
- Res = MCValue::get(A, B, Result_Cst);
- Res.Specifier = Spec;
- Res.SymSpecifier = 0;
+ Spec = RHS.getSpecifier();
+ Res = MCValue::get(A, B, Result_Cst, Spec);
return true;
}
@@ -532,18 +527,16 @@ bool MCExpr::evaluateAsRelocatableImpl(MCValue &Res, const MCAssembler *Asm,
InSet || IsMachO)) {
if (Kind != MCSymbolRefExpr::VK_None) {
if (Res.isAbsolute()) {
- Res = MCValue::get(SRE, nullptr, 0);
+ Res = MCValue::get(&Sym, nullptr, 0, Kind);
return true;
}
// If the reference has a variant kind, we can only handle expressions
// which evaluate exactly to a single unadorned symbol. Attach the
// original VariantKind to SymA of the result.
- if (Res.getRefKind() != MCSymbolRefExpr::VK_None || !Res.getSymA() ||
- Res.getSubSym() || Res.getConstant())
+ if (Res.getRefKind() != MCSymbolRefExpr::VK_None ||
+ !Res.getAddSym() || Res.getSubSym() || Res.getConstant())
return false;
- Res = MCValue::get(
- MCSymbolRefExpr::create(Res.getAddSym(), Kind, Asm->getContext()),
- Res.getSymB(), Res.getConstant(), Res.getRefKind());
+ Res.Specifier = Kind;
}
if (!IsMachO)
return true;
@@ -565,7 +558,7 @@ bool MCExpr::evaluateAsRelocatableImpl(MCValue &Res, const MCAssembler *Asm,
}
}
- Res = MCValue::get(SRE, nullptr, 0);
+ Res = MCValue::get(&Sym, nullptr, 0, Kind);
return true;
}
@@ -587,7 +580,7 @@ bool MCExpr::evaluateAsRelocatableImpl(MCValue &Res, const MCAssembler *Asm,
return false;
// The cast avoids undefined behavior if the constant is INT64_MIN.
- Res = MCValue::get(Value.getSymB(), Value.getSymA(),
+ Res = MCValue::get(Value.getSubSym(), Value.getAddSym(),
-(uint64_t)Value.getConstant());
break;
case MCUnaryExpr::Not:
@@ -653,9 +646,11 @@ bool MCExpr::evaluateAsRelocatableImpl(MCValue &Res, const MCAssembler *Asm,
Res = RHSValue;
return true;
}
- return evaluateSymbolicAdd(Asm, Addrs, InSet, LHSValue, RHSValue.SymA,
- RHSValue.SymB, RHSValue.Cst,
- RHSValue.SymSpecifier, Res);
+ if (LHSValue.SymB && LHSValue.Specifier)
+ return false;
+ if (RHSValue.SymB && RHSValue.Specifier)
+ return false;
+ return evaluateSymbolicAdd(Asm, Addrs, InSet, LHSValue, RHSValue, Res);
}
}