Skip to content

Commit fedf6c6

Browse files
committed
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.
1 parent 4635b60 commit fedf6c6

File tree

11 files changed

+102
-102
lines changed

11 files changed

+102
-102
lines changed

bolt/lib/Target/RISCV/RISCVMCPlusBuilder.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13-
#include "MCTargetDesc/RISCVMCExpr.h"
13+
#include "MCTargetDesc/RISCVMCAsmInfo.h"
1414
#include "MCTargetDesc/RISCVMCTargetDesc.h"
1515
#include "bolt/Core/MCPlusBuilder.h"
1616
#include "llvm/BinaryFormat/ELF.h"

llvm/lib/Target/RISCV/AsmParser/RISCVAsmParser.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#include "MCTargetDesc/RISCVAsmBackend.h"
1010
#include "MCTargetDesc/RISCVBaseInfo.h"
1111
#include "MCTargetDesc/RISCVInstPrinter.h"
12-
#include "MCTargetDesc/RISCVMCExpr.h"
12+
#include "MCTargetDesc/RISCVMCAsmInfo.h"
1313
#include "MCTargetDesc/RISCVMCTargetDesc.h"
1414
#include "MCTargetDesc/RISCVMatInt.h"
1515
#include "MCTargetDesc/RISCVTargetStreamer.h"
@@ -2087,7 +2087,7 @@ bool RISCVAsmParser::parseExprWithSpecifier(const MCExpr *&Res, SMLoc &E) {
20872087
if (getLexer().getKind() != AsmToken::Identifier)
20882088
return Error(getLoc(), "expected '%' relocation specifier");
20892089
StringRef Identifier = getParser().getTok().getIdentifier();
2090-
auto Spec = RISCVMCExpr::getSpecifierForName(Identifier);
2090+
auto Spec = RISCV::parseSpecifierName(Identifier);
20912091
if (!Spec)
20922092
return Error(getLoc(), "invalid relocation specifier");
20932093

@@ -2099,7 +2099,7 @@ bool RISCVAsmParser::parseExprWithSpecifier(const MCExpr *&Res, SMLoc &E) {
20992099
if (getParser().parseParenExpression(SubExpr, E))
21002100
return true;
21012101

2102-
Res = RISCVMCExpr::create(SubExpr, *Spec, getContext());
2102+
Res = RISCVMCExpr::create(SubExpr, Spec, getContext());
21032103
return false;
21042104
}
21052105

llvm/lib/Target/RISCV/MCTargetDesc/RISCVAsmBackend.cpp

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
#include "RISCVAsmBackend.h"
1010
#include "RISCVFixupKinds.h"
11-
#include "RISCVMCExpr.h"
1211
#include "llvm/ADT/APInt.h"
1312
#include "llvm/MC/MCAsmInfo.h"
1413
#include "llvm/MC/MCAssembler.h"
@@ -591,6 +590,57 @@ bool RISCVAsmBackend::isPCRelFixupResolved(const MCSymbol *SymA,
591590
return !Res.getSubSym();
592591
}
593592

593+
// Get the corresponding PC-relative HI fixup that a S_PCREL_LO points to, and
594+
// optionally the fragment containing it.
595+
//
596+
// \returns nullptr if this isn't a S_PCREL_LO pointing to a known PC-relative
597+
// HI fixup.
598+
static const MCFixup *getPCRelHiFixup(const MCSpecifierExpr &Expr,
599+
const MCFragment **DFOut) {
600+
MCValue AUIPCLoc;
601+
if (!Expr.getSubExpr()->evaluateAsRelocatable(AUIPCLoc, nullptr))
602+
return nullptr;
603+
604+
const MCSymbol *AUIPCSymbol = AUIPCLoc.getAddSym();
605+
if (!AUIPCSymbol)
606+
return nullptr;
607+
const auto *DF = dyn_cast_or_null<MCDataFragment>(AUIPCSymbol->getFragment());
608+
609+
if (!DF)
610+
return nullptr;
611+
612+
uint64_t Offset = AUIPCSymbol->getOffset();
613+
if (DF->getContents().size() == Offset) {
614+
DF = dyn_cast_or_null<MCDataFragment>(DF->getNext());
615+
if (!DF)
616+
return nullptr;
617+
Offset = 0;
618+
}
619+
620+
for (const MCFixup &F : DF->getFixups()) {
621+
if (F.getOffset() != Offset)
622+
continue;
623+
auto Kind = F.getTargetKind();
624+
if (!mc::isRelocation(F.getKind())) {
625+
if (Kind == RISCV::fixup_riscv_pcrel_hi20) {
626+
*DFOut = DF;
627+
return &F;
628+
}
629+
break;
630+
}
631+
switch (Kind) {
632+
case ELF::R_RISCV_GOT_HI20:
633+
case ELF::R_RISCV_TLS_GOT_HI20:
634+
case ELF::R_RISCV_TLS_GD_HI20:
635+
case ELF::R_RISCV_TLSDESC_HI20:
636+
*DFOut = DF;
637+
return &F;
638+
}
639+
}
640+
641+
return nullptr;
642+
}
643+
594644
bool RISCVAsmBackend::evaluateTargetFixup(const MCFixup &Fixup,
595645
const MCValue &Target,
596646
uint64_t &Value) {
@@ -602,7 +652,8 @@ bool RISCVAsmBackend::evaluateTargetFixup(const MCFixup &Fixup,
602652
llvm_unreachable("Unexpected fixup kind!");
603653
case RISCV::fixup_riscv_pcrel_lo12_i:
604654
case RISCV::fixup_riscv_pcrel_lo12_s: {
605-
AUIPCFixup = cast<RISCVMCExpr>(Fixup.getValue())->getPCRelHiFixup(&AUIPCDF);
655+
AUIPCFixup =
656+
getPCRelHiFixup(cast<MCSpecifierExpr>(*Fixup.getValue()), &AUIPCDF);
606657
if (!AUIPCFixup) {
607658
getContext().reportError(Fixup.getLoc(),
608659
"could not find corresponding %pcrel_hi");

llvm/lib/Target/RISCV/MCTargetDesc/RISCVELFObjectWriter.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "MCTargetDesc/RISCVFixupKinds.h"
10-
#include "MCTargetDesc/RISCVMCExpr.h"
10+
#include "MCTargetDesc/RISCVMCAsmInfo.h"
1111
#include "MCTargetDesc/RISCVMCTargetDesc.h"
1212
#include "llvm/MC/MCContext.h"
1313
#include "llvm/MC/MCELFObjectWriter.h"
@@ -49,7 +49,7 @@ unsigned RISCVELFObjectWriter::getRelocType(const MCFixup &Fixup,
4949
const MCValue &Target,
5050
bool IsPCRel) const {
5151
unsigned Kind = Fixup.getTargetKind();
52-
auto Spec = RISCVMCExpr::Specifier(Target.getSpecifier());
52+
auto Spec = Target.getSpecifier();
5353
switch (Spec) {
5454
case ELF::R_RISCV_TPREL_HI20:
5555
case ELF::R_RISCV_TLS_GOT_HI20:
@@ -62,7 +62,7 @@ unsigned RISCVELFObjectWriter::getRelocType(const MCFixup &Fixup,
6262
case ELF::R_RISCV_GOT32_PCREL:
6363
if (Kind == FK_Data_4)
6464
break;
65-
reportError(Fixup.getLoc(), "%" + RISCVMCExpr::getSpecifierName(Spec) +
65+
reportError(Fixup.getLoc(), "%" + RISCV::getSpecifierName(Spec) +
6666
" can only be used in a .word directive");
6767
return ELF::R_RISCV_NONE;
6868
default:

llvm/lib/Target/RISCV/MCTargetDesc/RISCVInstPrinter.cpp

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

1313
#include "RISCVInstPrinter.h"
1414
#include "RISCVBaseInfo.h"
15-
#include "RISCVMCExpr.h"
1615
#include "llvm/MC/MCAsmInfo.h"
1716
#include "llvm/MC/MCExpr.h"
1817
#include "llvm/MC/MCInst.h"
@@ -102,7 +101,7 @@ void RISCVInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
102101
}
103102

104103
assert(MO.isExpr() && "Unknown operand kind in printOperand");
105-
MO.getExpr()->print(O, &MAI);
104+
MAI.printExpr(O, *MO.getExpr());
106105
}
107106

108107
void RISCVInstPrinter::printBranchOperand(const MCInst *MI, uint64_t Address,

llvm/lib/Target/RISCV/MCTargetDesc/RISCVMCAsmInfo.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
//===----------------------------------------------------------------------===//
1212

1313
#include "RISCVMCAsmInfo.h"
14-
#include "MCTargetDesc/RISCVMCExpr.h"
1514
#include "llvm/BinaryFormat/Dwarf.h"
1615
#include "llvm/BinaryFormat/ELF.h"
1716
#include "llvm/MC/MCExpr.h"
@@ -47,3 +46,14 @@ const MCExpr *RISCVMCAsmInfo::getExprForFDESymbol(const MCSymbol *Sym,
4746
assert(Encoding & dwarf::DW_EH_PE_sdata4 && "Unexpected encoding");
4847
return RISCVMCExpr::create(ME, ELF::R_RISCV_32_PCREL, Ctx);
4948
}
49+
50+
void RISCVMCAsmInfo::printSpecifierExpr(raw_ostream &OS,
51+
const MCSpecifierExpr &Expr) const {
52+
auto S = Expr.getSpecifier();
53+
bool HasSpecifier = S != 0 && S != ELF::R_RISCV_CALL_PLT;
54+
if (HasSpecifier)
55+
OS << '%' << RISCV::getSpecifierName(S) << '(';
56+
printExpr(OS, *Expr.getSubExpr());
57+
if (HasSpecifier)
58+
OS << ')';
59+
}

llvm/lib/Target/RISCV/MCTargetDesc/RISCVMCAsmInfo.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313
#ifndef LLVM_LIB_TARGET_RISCV_MCTARGETDESC_RISCVMCASMINFO_H
1414
#define LLVM_LIB_TARGET_RISCV_MCTARGETDESC_RISCVMCASMINFO_H
1515

16+
#include "RISCVMCExpr.h"
1617
#include "llvm/MC/MCAsmInfoELF.h"
18+
#include "llvm/MC/MCFixup.h"
1719

1820
namespace llvm {
1921
class Triple;
@@ -26,8 +28,29 @@ class RISCVMCAsmInfo : public MCAsmInfoELF {
2628

2729
const MCExpr *getExprForFDESymbol(const MCSymbol *Sym, unsigned Encoding,
2830
MCStreamer &Streamer) const override;
31+
void printSpecifierExpr(raw_ostream &OS,
32+
const MCSpecifierExpr &Expr) const override;
2933
};
3034

35+
namespace RISCV {
36+
using Specifier = uint16_t;
37+
// Specifiers mapping to relocation types below FirstTargetFixupKind are
38+
// encoded literally, with these exceptions:
39+
enum {
40+
S_None,
41+
// Specifiers mapping to distinct relocation types.
42+
S_LO = FirstTargetFixupKind,
43+
S_PCREL_LO,
44+
S_TPREL_LO,
45+
// Vendor-specific relocation types might conflict across vendors.
46+
// Refer to them using Specifier constants.
47+
S_QC_ABS20,
48+
};
49+
50+
Specifier parseSpecifierName(StringRef name);
51+
StringRef getSpecifierName(Specifier Kind);
52+
} // namespace RISCV
53+
3154
} // namespace llvm
3255

3356
#endif

llvm/lib/Target/RISCV/MCTargetDesc/RISCVMCCodeEmitter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
#include "MCTargetDesc/RISCVBaseInfo.h"
1414
#include "MCTargetDesc/RISCVFixupKinds.h"
15-
#include "MCTargetDesc/RISCVMCExpr.h"
15+
#include "MCTargetDesc/RISCVMCAsmInfo.h"
1616
#include "MCTargetDesc/RISCVMCTargetDesc.h"
1717
#include "llvm/ADT/Statistic.h"
1818
#include "llvm/MC/MCAsmInfo.h"

llvm/lib/Target/RISCV/MCTargetDesc/RISCVMCExpr.cpp

Lines changed: 5 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
#include "RISCVMCExpr.h"
1515
#include "MCTargetDesc/RISCVAsmBackend.h"
16+
#include "MCTargetDesc/RISCVMCAsmInfo.h"
1617
#include "RISCVFixupKinds.h"
1718
#include "llvm/BinaryFormat/ELF.h"
1819
#include "llvm/MC/MCAssembler.h"
@@ -31,65 +32,8 @@ const RISCVMCExpr *RISCVMCExpr::create(const MCExpr *Expr, Specifier S,
3132
return new (Ctx) RISCVMCExpr(Expr, S);
3233
}
3334

34-
void RISCVMCExpr::printImpl(raw_ostream &OS, const MCAsmInfo *MAI) const {
35-
Specifier S = getSpecifier();
36-
bool HasVariant = S != RISCV::S_None && S != ELF::R_RISCV_CALL_PLT;
37-
38-
if (HasVariant)
39-
OS << '%' << getSpecifierName(S) << '(';
40-
Expr->print(OS, MAI);
41-
if (HasVariant)
42-
OS << ')';
43-
}
44-
45-
const MCFixup *RISCVMCExpr::getPCRelHiFixup(const MCFragment **DFOut) const {
46-
MCValue AUIPCLoc;
47-
if (!getSubExpr()->evaluateAsRelocatable(AUIPCLoc, nullptr))
48-
return nullptr;
49-
50-
const MCSymbol *AUIPCSymbol = AUIPCLoc.getAddSym();
51-
if (!AUIPCSymbol)
52-
return nullptr;
53-
const auto *DF = dyn_cast_or_null<MCDataFragment>(AUIPCSymbol->getFragment());
54-
55-
if (!DF)
56-
return nullptr;
57-
58-
uint64_t Offset = AUIPCSymbol->getOffset();
59-
if (DF->getContents().size() == Offset) {
60-
DF = dyn_cast_or_null<MCDataFragment>(DF->getNext());
61-
if (!DF)
62-
return nullptr;
63-
Offset = 0;
64-
}
65-
66-
for (const MCFixup &F : DF->getFixups()) {
67-
if (F.getOffset() != Offset)
68-
continue;
69-
auto Kind = F.getTargetKind();
70-
if (!mc::isRelocation(F.getKind())) {
71-
if (Kind == RISCV::fixup_riscv_pcrel_hi20) {
72-
*DFOut = DF;
73-
return &F;
74-
}
75-
break;
76-
}
77-
switch (Kind) {
78-
case ELF::R_RISCV_GOT_HI20:
79-
case ELF::R_RISCV_TLS_GOT_HI20:
80-
case ELF::R_RISCV_TLS_GD_HI20:
81-
case ELF::R_RISCV_TLSDESC_HI20:
82-
*DFOut = DF;
83-
return &F;
84-
}
85-
}
86-
87-
return nullptr;
88-
}
89-
90-
std::optional<RISCVMCExpr::Specifier>
91-
RISCVMCExpr::getSpecifierForName(StringRef name) {
92-
return StringSwitch<std::optional<RISCVMCExpr::Specifier>>(name)
35+
RISCV::Specifier RISCV::parseSpecifierName(StringRef name) {
36+
return StringSwitch<RISCV::Specifier>(name)
9337
.Case("lo", RISCV::S_LO)
9438
.Case("hi", ELF::R_RISCV_HI20)
9539
.Case("pcrel_lo", RISCV::S_PCREL_LO)
@@ -108,10 +52,10 @@ RISCVMCExpr::getSpecifierForName(StringRef name) {
10852
// Used in data directives
10953
.Case("pltpcrel", ELF::R_RISCV_PLT32)
11054
.Case("gotpcrel", ELF::R_RISCV_GOT32_PCREL)
111-
.Default(std::nullopt);
55+
.Default(0);
11256
}
11357

114-
StringRef RISCVMCExpr::getSpecifierName(Specifier S) {
58+
StringRef RISCV::getSpecifierName(Specifier S) {
11559
switch (S) {
11660
case RISCV::S_None:
11761
llvm_unreachable("not used as %specifier()");

llvm/lib/Target/RISCV/MCTargetDesc/RISCVMCExpr.h

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -32,34 +32,7 @@ class RISCVMCExpr : public MCSpecifierExpr {
3232
public:
3333
static const RISCVMCExpr *create(const MCExpr *Expr, Specifier S,
3434
MCContext &Ctx);
35-
36-
/// Get the corresponding PC-relative HI fixup that a VK_PCREL_LO
37-
/// points to, and optionally the fragment containing it.
38-
///
39-
/// \returns nullptr if this isn't a VK_PCREL_LO pointing to a
40-
/// known PC-relative HI fixup.
41-
const MCFixup *getPCRelHiFixup(const MCFragment **DFOut) const;
42-
43-
void printImpl(raw_ostream &OS, const MCAsmInfo *MAI) const override;
44-
45-
static std::optional<Specifier> getSpecifierForName(StringRef name);
46-
static StringRef getSpecifierName(Specifier Kind);
47-
};
48-
49-
namespace RISCV {
50-
// Specifiers mapping to relocation types below FirstTargetFixupKind are
51-
// encoded literally, with these exceptions:
52-
enum Specifier {
53-
S_None,
54-
// Specifiers mapping to distinct relocation types.
55-
S_LO = FirstTargetFixupKind,
56-
S_PCREL_LO,
57-
S_TPREL_LO,
58-
// Vendor-specific relocation types might conflict across vendors.
59-
// Refer to them using Specifier constants.
60-
S_QC_ABS20,
6135
};
62-
} // namespace RISCV
6336
} // end namespace llvm.
6437

6538
#endif

0 commit comments

Comments
 (0)