Skip to content

Commit fb13be0

Browse files
committed
MC: Generalize evaluateTargetFixup
Generalize evaluateTargetFixup to be called by all targets, making FKF_IsTarget unneeded. Next: Update targets that use FKF_IsAlignedDownTo32Bits to define `evaluateFixup` and remove FKF_IsAlignedDownTo32Bits from the generic code.
1 parent cd39eae commit fb13be0

File tree

5 files changed

+18
-14
lines changed

5 files changed

+18
-14
lines changed

llvm/include/llvm/MC/MCAsmBackend.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,9 +131,12 @@ class LLVM_ABI MCAsmBackend {
131131
return false;
132132
}
133133

134-
virtual bool evaluateTargetFixup(const MCFixup &Fixup, const MCValue &Target,
135-
uint64_t &Value) {
136-
llvm_unreachable("Need to implement hook if target has custom fixups");
134+
// Evaluate a fixup, returning std::nullopt to use default handling for
135+
// `Value` and `IsResolved`. Otherwise, returns `IsResolved` with the
136+
// expectation that the hook updates `Value`.
137+
virtual std::optional<bool> evaluateFixup(MCFixup &Fixup, MCValue &Target,
138+
uint64_t &Value) {
139+
return {};
137140
}
138141

139142
void maybeAddReloc(const MCFragment &, const MCFixup &, const MCValue &,

llvm/lib/MC/MCAssembler.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,8 @@ bool MCAssembler::evaluateFixup(const MCFragment &F, MCFixup &Fixup,
163163

164164
unsigned FixupFlags = getBackend().getFixupKindInfo(Fixup.getKind()).Flags;
165165
bool IsResolved = false;
166-
if (FixupFlags & MCFixupKindInfo::FKF_IsTarget) {
167-
IsResolved = getBackend().evaluateTargetFixup(Fixup, Target, Value);
166+
if (auto State = getBackend().evaluateFixup(Fixup, Target, Value)) {
167+
IsResolved = *State;
168168
} else {
169169
const MCSymbol *Add = Target.getAddSym();
170170
const MCSymbol *Sub = Target.getSubSym();

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@ MCFixupKindInfo RISCVAsmBackend::getFixupKindInfo(MCFixupKind Kind) const {
7070
{"fixup_riscv_12_i", 20, 12, 0},
7171
{"fixup_riscv_lo12_s", 0, 32, 0},
7272
{"fixup_riscv_pcrel_hi20", 12, 20, 0},
73-
{"fixup_riscv_pcrel_lo12_i", 20, 12, MCFixupKindInfo::FKF_IsTarget},
74-
{"fixup_riscv_pcrel_lo12_s", 0, 32, MCFixupKindInfo::FKF_IsTarget},
73+
{"fixup_riscv_pcrel_lo12_i", 20, 12, 0},
74+
{"fixup_riscv_pcrel_lo12_s", 0, 32, 0},
7575
{"fixup_riscv_jal", 12, 20, 0},
7676
{"fixup_riscv_branch", 0, 32, 0},
7777
{"fixup_riscv_rvc_jump", 2, 11, 0},
@@ -655,15 +655,16 @@ static const MCFixup *getPCRelHiFixup(const MCSpecifierExpr &Expr,
655655
return nullptr;
656656
}
657657

658-
bool RISCVAsmBackend::evaluateTargetFixup(const MCFixup &Fixup,
659-
const MCValue &Target,
660-
uint64_t &Value) {
658+
std::optional<bool> RISCVAsmBackend::evaluateFixup(MCFixup &Fixup,
659+
MCValue &Target,
660+
uint64_t &Value) {
661661
const MCFixup *AUIPCFixup;
662662
const MCFragment *AUIPCDF;
663663
MCValue AUIPCTarget;
664664
switch (Fixup.getTargetKind()) {
665665
default:
666-
llvm_unreachable("Unexpected fixup kind!");
666+
// Use default handling for `Value` and `IsResolved`.
667+
return {};
667668
case RISCV::fixup_riscv_pcrel_lo12_i:
668669
case RISCV::fixup_riscv_pcrel_lo12_s: {
669670
AUIPCFixup =

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ class RISCVAsmBackend : public MCAsmBackend {
4747
bool shouldInsertFixupForCodeAlign(MCAssembler &Asm,
4848
MCAlignFragment &AF) override;
4949

50-
bool evaluateTargetFixup(const MCFixup &Fixup, const MCValue &Target,
51-
uint64_t &Value) override;
50+
std::optional<bool> evaluateFixup(MCFixup &Fixup, MCValue &Target,
51+
uint64_t &Value) override;
5252

5353
bool addReloc(const MCFragment &, const MCFixup &, const MCValue &,
5454
uint64_t &FixedValue, bool IsResolved);

llvm/test/MC/RISCV/Relocations/mc-dump.s

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
# CHECK-NEXT: Symbol @0 .text
88
# CHECK-NEXT:0 Align Align:4 Value:0 ValueSize:1 MaxBytesToEmit:4 Nops
99
# CHECK-NEXT:0 Data LinkerRelaxable Size:8 [97,00,00,00,e7,80,00,00]
10-
# CHECK-NEXT: Fixup @0 Value:specifier(19,ext) Kind:4026
10+
# CHECK-NEXT: Fixup @0 Value:specifier(19,ext) Kind:4022
1111
# CHECK-NEXT: Symbol @0 $x
1212
# CHECK-NEXT:8 Align Align:8 Value:0 ValueSize:1 MaxBytesToEmit:8 Nops
1313
# CHECK-NEXT:12 Data Size:4 [13,05,30,00]

0 commit comments

Comments
 (0)