RISCV: Move RISCVMCExpr functions to RISCVMCAsmInfo or RISCVMCAsmBackend

* Move getPCRelHiFixup closer to the only caller RISCVAsmBackend::evaluateTargetFixup.
* Declare getSpecifierForName in RISCVMCAsmInfo, in align with other
  targets that have migrated to the new relocation specifier representation.
This commit is contained in:
Fangrui Song
2025-06-15 15:43:41 -07:00
parent 4635b6076d
commit fedf6c68dd
11 changed files with 102 additions and 102 deletions

View File

@@ -10,7 +10,7 @@
//
//===----------------------------------------------------------------------===//
#include "MCTargetDesc/RISCVMCExpr.h"
#include "MCTargetDesc/RISCVMCAsmInfo.h"
#include "MCTargetDesc/RISCVMCTargetDesc.h"
#include "bolt/Core/MCPlusBuilder.h"
#include "llvm/BinaryFormat/ELF.h"

View File

@@ -9,7 +9,7 @@
#include "MCTargetDesc/RISCVAsmBackend.h"
#include "MCTargetDesc/RISCVBaseInfo.h"
#include "MCTargetDesc/RISCVInstPrinter.h"
#include "MCTargetDesc/RISCVMCExpr.h"
#include "MCTargetDesc/RISCVMCAsmInfo.h"
#include "MCTargetDesc/RISCVMCTargetDesc.h"
#include "MCTargetDesc/RISCVMatInt.h"
#include "MCTargetDesc/RISCVTargetStreamer.h"
@@ -2087,7 +2087,7 @@ bool RISCVAsmParser::parseExprWithSpecifier(const MCExpr *&Res, SMLoc &E) {
if (getLexer().getKind() != AsmToken::Identifier)
return Error(getLoc(), "expected '%' relocation specifier");
StringRef Identifier = getParser().getTok().getIdentifier();
auto Spec = RISCVMCExpr::getSpecifierForName(Identifier);
auto Spec = RISCV::parseSpecifierName(Identifier);
if (!Spec)
return Error(getLoc(), "invalid relocation specifier");
@@ -2099,7 +2099,7 @@ bool RISCVAsmParser::parseExprWithSpecifier(const MCExpr *&Res, SMLoc &E) {
if (getParser().parseParenExpression(SubExpr, E))
return true;
Res = RISCVMCExpr::create(SubExpr, *Spec, getContext());
Res = RISCVMCExpr::create(SubExpr, Spec, getContext());
return false;
}

View File

@@ -8,7 +8,6 @@
#include "RISCVAsmBackend.h"
#include "RISCVFixupKinds.h"
#include "RISCVMCExpr.h"
#include "llvm/ADT/APInt.h"
#include "llvm/MC/MCAsmInfo.h"
#include "llvm/MC/MCAssembler.h"
@@ -591,6 +590,57 @@ bool RISCVAsmBackend::isPCRelFixupResolved(const MCSymbol *SymA,
return !Res.getSubSym();
}
// Get the corresponding PC-relative HI fixup that a S_PCREL_LO points to, and
// optionally the fragment containing it.
//
// \returns nullptr if this isn't a S_PCREL_LO pointing to a known PC-relative
// HI fixup.
static const MCFixup *getPCRelHiFixup(const MCSpecifierExpr &Expr,
const MCFragment **DFOut) {
MCValue AUIPCLoc;
if (!Expr.getSubExpr()->evaluateAsRelocatable(AUIPCLoc, nullptr))
return nullptr;
const MCSymbol *AUIPCSymbol = AUIPCLoc.getAddSym();
if (!AUIPCSymbol)
return nullptr;
const auto *DF = dyn_cast_or_null<MCDataFragment>(AUIPCSymbol->getFragment());
if (!DF)
return nullptr;
uint64_t Offset = AUIPCSymbol->getOffset();
if (DF->getContents().size() == Offset) {
DF = dyn_cast_or_null<MCDataFragment>(DF->getNext());
if (!DF)
return nullptr;
Offset = 0;
}
for (const MCFixup &F : DF->getFixups()) {
if (F.getOffset() != Offset)
continue;
auto Kind = F.getTargetKind();
if (!mc::isRelocation(F.getKind())) {
if (Kind == RISCV::fixup_riscv_pcrel_hi20) {
*DFOut = DF;
return &F;
}
break;
}
switch (Kind) {
case ELF::R_RISCV_GOT_HI20:
case ELF::R_RISCV_TLS_GOT_HI20:
case ELF::R_RISCV_TLS_GD_HI20:
case ELF::R_RISCV_TLSDESC_HI20:
*DFOut = DF;
return &F;
}
}
return nullptr;
}
bool RISCVAsmBackend::evaluateTargetFixup(const MCFixup &Fixup,
const MCValue &Target,
uint64_t &Value) {
@@ -602,7 +652,8 @@ bool RISCVAsmBackend::evaluateTargetFixup(const MCFixup &Fixup,
llvm_unreachable("Unexpected fixup kind!");
case RISCV::fixup_riscv_pcrel_lo12_i:
case RISCV::fixup_riscv_pcrel_lo12_s: {
AUIPCFixup = cast<RISCVMCExpr>(Fixup.getValue())->getPCRelHiFixup(&AUIPCDF);
AUIPCFixup =
getPCRelHiFixup(cast<MCSpecifierExpr>(*Fixup.getValue()), &AUIPCDF);
if (!AUIPCFixup) {
getContext().reportError(Fixup.getLoc(),
"could not find corresponding %pcrel_hi");

View File

@@ -7,7 +7,7 @@
//===----------------------------------------------------------------------===//
#include "MCTargetDesc/RISCVFixupKinds.h"
#include "MCTargetDesc/RISCVMCExpr.h"
#include "MCTargetDesc/RISCVMCAsmInfo.h"
#include "MCTargetDesc/RISCVMCTargetDesc.h"
#include "llvm/MC/MCContext.h"
#include "llvm/MC/MCELFObjectWriter.h"
@@ -49,7 +49,7 @@ unsigned RISCVELFObjectWriter::getRelocType(const MCFixup &Fixup,
const MCValue &Target,
bool IsPCRel) const {
unsigned Kind = Fixup.getTargetKind();
auto Spec = RISCVMCExpr::Specifier(Target.getSpecifier());
auto Spec = Target.getSpecifier();
switch (Spec) {
case ELF::R_RISCV_TPREL_HI20:
case ELF::R_RISCV_TLS_GOT_HI20:
@@ -62,7 +62,7 @@ unsigned RISCVELFObjectWriter::getRelocType(const MCFixup &Fixup,
case ELF::R_RISCV_GOT32_PCREL:
if (Kind == FK_Data_4)
break;
reportError(Fixup.getLoc(), "%" + RISCVMCExpr::getSpecifierName(Spec) +
reportError(Fixup.getLoc(), "%" + RISCV::getSpecifierName(Spec) +
" can only be used in a .word directive");
return ELF::R_RISCV_NONE;
default:

View File

@@ -12,7 +12,6 @@
#include "RISCVInstPrinter.h"
#include "RISCVBaseInfo.h"
#include "RISCVMCExpr.h"
#include "llvm/MC/MCAsmInfo.h"
#include "llvm/MC/MCExpr.h"
#include "llvm/MC/MCInst.h"
@@ -102,7 +101,7 @@ void RISCVInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
}
assert(MO.isExpr() && "Unknown operand kind in printOperand");
MO.getExpr()->print(O, &MAI);
MAI.printExpr(O, *MO.getExpr());
}
void RISCVInstPrinter::printBranchOperand(const MCInst *MI, uint64_t Address,

View File

@@ -11,7 +11,6 @@
//===----------------------------------------------------------------------===//
#include "RISCVMCAsmInfo.h"
#include "MCTargetDesc/RISCVMCExpr.h"
#include "llvm/BinaryFormat/Dwarf.h"
#include "llvm/BinaryFormat/ELF.h"
#include "llvm/MC/MCExpr.h"
@@ -47,3 +46,14 @@ const MCExpr *RISCVMCAsmInfo::getExprForFDESymbol(const MCSymbol *Sym,
assert(Encoding & dwarf::DW_EH_PE_sdata4 && "Unexpected encoding");
return RISCVMCExpr::create(ME, ELF::R_RISCV_32_PCREL, Ctx);
}
void RISCVMCAsmInfo::printSpecifierExpr(raw_ostream &OS,
const MCSpecifierExpr &Expr) const {
auto S = Expr.getSpecifier();
bool HasSpecifier = S != 0 && S != ELF::R_RISCV_CALL_PLT;
if (HasSpecifier)
OS << '%' << RISCV::getSpecifierName(S) << '(';
printExpr(OS, *Expr.getSubExpr());
if (HasSpecifier)
OS << ')';
}

View File

@@ -13,7 +13,9 @@
#ifndef LLVM_LIB_TARGET_RISCV_MCTARGETDESC_RISCVMCASMINFO_H
#define LLVM_LIB_TARGET_RISCV_MCTARGETDESC_RISCVMCASMINFO_H
#include "RISCVMCExpr.h"
#include "llvm/MC/MCAsmInfoELF.h"
#include "llvm/MC/MCFixup.h"
namespace llvm {
class Triple;
@@ -26,8 +28,29 @@ public:
const MCExpr *getExprForFDESymbol(const MCSymbol *Sym, unsigned Encoding,
MCStreamer &Streamer) const override;
void printSpecifierExpr(raw_ostream &OS,
const MCSpecifierExpr &Expr) const override;
};
namespace RISCV {
using Specifier = uint16_t;
// Specifiers mapping to relocation types below FirstTargetFixupKind are
// encoded literally, with these exceptions:
enum {
S_None,
// Specifiers mapping to distinct relocation types.
S_LO = FirstTargetFixupKind,
S_PCREL_LO,
S_TPREL_LO,
// Vendor-specific relocation types might conflict across vendors.
// Refer to them using Specifier constants.
S_QC_ABS20,
};
Specifier parseSpecifierName(StringRef name);
StringRef getSpecifierName(Specifier Kind);
} // namespace RISCV
} // namespace llvm
#endif

View File

@@ -12,7 +12,7 @@
#include "MCTargetDesc/RISCVBaseInfo.h"
#include "MCTargetDesc/RISCVFixupKinds.h"
#include "MCTargetDesc/RISCVMCExpr.h"
#include "MCTargetDesc/RISCVMCAsmInfo.h"
#include "MCTargetDesc/RISCVMCTargetDesc.h"
#include "llvm/ADT/Statistic.h"
#include "llvm/MC/MCAsmInfo.h"

View File

@@ -13,6 +13,7 @@
#include "RISCVMCExpr.h"
#include "MCTargetDesc/RISCVAsmBackend.h"
#include "MCTargetDesc/RISCVMCAsmInfo.h"
#include "RISCVFixupKinds.h"
#include "llvm/BinaryFormat/ELF.h"
#include "llvm/MC/MCAssembler.h"
@@ -31,65 +32,8 @@ const RISCVMCExpr *RISCVMCExpr::create(const MCExpr *Expr, Specifier S,
return new (Ctx) RISCVMCExpr(Expr, S);
}
void RISCVMCExpr::printImpl(raw_ostream &OS, const MCAsmInfo *MAI) const {
Specifier S = getSpecifier();
bool HasVariant = S != RISCV::S_None && S != ELF::R_RISCV_CALL_PLT;
if (HasVariant)
OS << '%' << getSpecifierName(S) << '(';
Expr->print(OS, MAI);
if (HasVariant)
OS << ')';
}
const MCFixup *RISCVMCExpr::getPCRelHiFixup(const MCFragment **DFOut) const {
MCValue AUIPCLoc;
if (!getSubExpr()->evaluateAsRelocatable(AUIPCLoc, nullptr))
return nullptr;
const MCSymbol *AUIPCSymbol = AUIPCLoc.getAddSym();
if (!AUIPCSymbol)
return nullptr;
const auto *DF = dyn_cast_or_null<MCDataFragment>(AUIPCSymbol->getFragment());
if (!DF)
return nullptr;
uint64_t Offset = AUIPCSymbol->getOffset();
if (DF->getContents().size() == Offset) {
DF = dyn_cast_or_null<MCDataFragment>(DF->getNext());
if (!DF)
return nullptr;
Offset = 0;
}
for (const MCFixup &F : DF->getFixups()) {
if (F.getOffset() != Offset)
continue;
auto Kind = F.getTargetKind();
if (!mc::isRelocation(F.getKind())) {
if (Kind == RISCV::fixup_riscv_pcrel_hi20) {
*DFOut = DF;
return &F;
}
break;
}
switch (Kind) {
case ELF::R_RISCV_GOT_HI20:
case ELF::R_RISCV_TLS_GOT_HI20:
case ELF::R_RISCV_TLS_GD_HI20:
case ELF::R_RISCV_TLSDESC_HI20:
*DFOut = DF;
return &F;
}
}
return nullptr;
}
std::optional<RISCVMCExpr::Specifier>
RISCVMCExpr::getSpecifierForName(StringRef name) {
return StringSwitch<std::optional<RISCVMCExpr::Specifier>>(name)
RISCV::Specifier RISCV::parseSpecifierName(StringRef name) {
return StringSwitch<RISCV::Specifier>(name)
.Case("lo", RISCV::S_LO)
.Case("hi", ELF::R_RISCV_HI20)
.Case("pcrel_lo", RISCV::S_PCREL_LO)
@@ -108,10 +52,10 @@ RISCVMCExpr::getSpecifierForName(StringRef name) {
// Used in data directives
.Case("pltpcrel", ELF::R_RISCV_PLT32)
.Case("gotpcrel", ELF::R_RISCV_GOT32_PCREL)
.Default(std::nullopt);
.Default(0);
}
StringRef RISCVMCExpr::getSpecifierName(Specifier S) {
StringRef RISCV::getSpecifierName(Specifier S) {
switch (S) {
case RISCV::S_None:
llvm_unreachable("not used as %specifier()");

View File

@@ -32,34 +32,7 @@ private:
public:
static const RISCVMCExpr *create(const MCExpr *Expr, Specifier S,
MCContext &Ctx);
/// Get the corresponding PC-relative HI fixup that a VK_PCREL_LO
/// points to, and optionally the fragment containing it.
///
/// \returns nullptr if this isn't a VK_PCREL_LO pointing to a
/// known PC-relative HI fixup.
const MCFixup *getPCRelHiFixup(const MCFragment **DFOut) const;
void printImpl(raw_ostream &OS, const MCAsmInfo *MAI) const override;
static std::optional<Specifier> getSpecifierForName(StringRef name);
static StringRef getSpecifierName(Specifier Kind);
};
namespace RISCV {
// Specifiers mapping to relocation types below FirstTargetFixupKind are
// encoded literally, with these exceptions:
enum Specifier {
S_None,
// Specifiers mapping to distinct relocation types.
S_LO = FirstTargetFixupKind,
S_PCREL_LO,
S_TPREL_LO,
// Vendor-specific relocation types might conflict across vendors.
// Refer to them using Specifier constants.
S_QC_ABS20,
};
} // namespace RISCV
} // end namespace llvm.
#endif

View File

@@ -13,7 +13,7 @@
#include "MCTargetDesc/RISCVBaseInfo.h"
#include "MCTargetDesc/RISCVInstPrinter.h"
#include "MCTargetDesc/RISCVMCExpr.h"
#include "MCTargetDesc/RISCVMCAsmInfo.h"
#include "MCTargetDesc/RISCVMatInt.h"
#include "MCTargetDesc/RISCVTargetStreamer.h"
#include "RISCV.h"