Skip to content

Commit

Permalink
[RISCV] Move RISCVELFStreamer::getRelocPairForSize to RISCVFixUpKinds…
Browse files Browse the repository at this point in the history
….h and reuse it. NFC

Reuse it for RISCVAsmBackend.cpp.
While there make the function return a pair of MCFixupKind to
remove static_casts elsewhere.

Reviewed By: reames

Differential Revision: https://reviews.llvm.org/D142955
  • Loading branch information
topperc committed Jan 31, 2023
1 parent 7d7729b commit b0cb5cb
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 33 deletions.
16 changes: 6 additions & 10 deletions llvm/lib/Target/RISCV/MCTargetDesc/RISCVAsmBackend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ bool RISCVAsmBackend::relaxDwarfLineAddr(MCDwarfLineAddrFragment &DF,
}

unsigned Offset;
std::pair<unsigned, unsigned> Fixup;
std::pair<MCFixupKind, MCFixupKind> Fixup;

// According to the DWARF specification, the `DW_LNS_fixed_advance_pc` opcode
// takes a single unsigned half (unencoded) operand. The maximum encodable
Expand All @@ -223,23 +223,19 @@ bool RISCVAsmBackend::relaxDwarfLineAddr(MCDwarfLineAddrFragment &DF,

OS << uint8_t(dwarf::DW_LNE_set_address);
Offset = OS.tell();
Fixup = PtrSize == 4 ? std::make_pair(RISCV::fixup_riscv_add_32,
RISCV::fixup_riscv_sub_32)
: std::make_pair(RISCV::fixup_riscv_add_64,
RISCV::fixup_riscv_sub_64);
assert((PtrSize == 4 || PtrSize == 8) && "Unexpected pointer size");
Fixup = RISCV::getRelocPairForSize(PtrSize);
OS.write_zeros(PtrSize);
} else {
OS << uint8_t(dwarf::DW_LNS_fixed_advance_pc);
Offset = OS.tell();
Fixup = {RISCV::fixup_riscv_add_16, RISCV::fixup_riscv_sub_16};
Fixup = RISCV::getRelocPairForSize(2);
support::endian::write<uint16_t>(OS, 0, support::little);
}

const MCBinaryExpr &MBE = cast<MCBinaryExpr>(AddrDelta);
Fixups.push_back(MCFixup::create(
Offset, MBE.getLHS(), static_cast<MCFixupKind>(std::get<0>(Fixup))));
Fixups.push_back(MCFixup::create(
Offset, MBE.getRHS(), static_cast<MCFixupKind>(std::get<1>(Fixup))));
Fixups.push_back(MCFixup::create(Offset, MBE.getLHS(), std::get<0>(Fixup)));
Fixups.push_back(MCFixup::create(Offset, MBE.getRHS(), std::get<1>(Fixup)));

if (LineDelta == INT64_MAX) {
OS << uint8_t(dwarf::DW_LNS_extended_op);
Expand Down
28 changes: 6 additions & 22 deletions llvm/lib/Target/RISCV/MCTargetDesc/RISCVELFStreamer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -192,22 +192,6 @@ void RISCVTargetELFStreamer::emitDirectiveVariantCC(MCSymbol &Symbol) {
cast<MCSymbolELF>(Symbol).setOther(ELF::STO_RISCV_VARIANT_CC);
}

std::pair<unsigned, unsigned>
RISCVELFStreamer::getRelocPairForSize(unsigned Size) {
switch (Size) {
default:
llvm_unreachable("unsupported fixup size");
case 1:
return std::make_pair(RISCV::fixup_riscv_add_8, RISCV::fixup_riscv_sub_8);
case 2:
return std::make_pair(RISCV::fixup_riscv_add_16, RISCV::fixup_riscv_sub_16);
case 4:
return std::make_pair(RISCV::fixup_riscv_add_32, RISCV::fixup_riscv_sub_32);
case 8:
return std::make_pair(RISCV::fixup_riscv_add_64, RISCV::fixup_riscv_sub_64);
}
}

bool RISCVELFStreamer::requiresFixups(MCContext &C, const MCExpr *Value,
const MCExpr *&LHS, const MCExpr *&RHS) {
const auto *MBE = dyn_cast<MCBinaryExpr>(Value);
Expand Down Expand Up @@ -261,13 +245,13 @@ void RISCVELFStreamer::emitValueImpl(const MCExpr *Value, unsigned Size,
flushPendingLabels(DF, DF->getContents().size());
MCDwarfLineEntry::make(this, getCurrentSectionOnly());

unsigned Add, Sub;
std::tie(Add, Sub) = getRelocPairForSize(Size);
MCFixupKind Add, Sub;
std::tie(Add, Sub) = RISCV::getRelocPairForSize(Size);

DF->getFixups().push_back(MCFixup::create(
DF->getContents().size(), A, static_cast<MCFixupKind>(Add), Loc));
DF->getFixups().push_back(MCFixup::create(
DF->getContents().size(), B, static_cast<MCFixupKind>(Sub), Loc));
DF->getFixups().push_back(
MCFixup::create(DF->getContents().size(), A, Add, Loc));
DF->getFixups().push_back(
MCFixup::create(DF->getContents().size(), B, Sub, Loc));

DF->getContents().resize(DF->getContents().size() + Size, 0);
}
Expand Down
1 change: 0 additions & 1 deletion llvm/lib/Target/RISCV/MCTargetDesc/RISCVELFStreamer.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
using namespace llvm;

class RISCVELFStreamer : public MCELFStreamer {
static std::pair<unsigned, unsigned> getRelocPairForSize(unsigned Size);
static bool requiresFixups(MCContext &C, const MCExpr *Value,
const MCExpr *&LHS, const MCExpr *&RHS);
void reset() override;
Expand Down
22 changes: 22 additions & 0 deletions llvm/lib/Target/RISCV/MCTargetDesc/RISCVFixupKinds.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#define LLVM_LIB_TARGET_RISCV_MCTARGETDESC_RISCVFIXUPKINDS_H

#include "llvm/MC/MCFixup.h"
#include <utility>

#undef RISCV

Expand Down Expand Up @@ -108,6 +109,27 @@ enum Fixups {
fixup_riscv_invalid,
NumTargetFixupKinds = fixup_riscv_invalid - FirstTargetFixupKind
};

static inline std::pair<MCFixupKind, MCFixupKind>
getRelocPairForSize(unsigned Size) {
switch (Size) {
default:
llvm_unreachable("unsupported fixup size");
case 1:
return std::make_pair(MCFixupKind(RISCV::fixup_riscv_add_8),
MCFixupKind(RISCV::fixup_riscv_sub_8));
case 2:
return std::make_pair(MCFixupKind(RISCV::fixup_riscv_add_16),
MCFixupKind(RISCV::fixup_riscv_sub_16));
case 4:
return std::make_pair(MCFixupKind(RISCV::fixup_riscv_add_32),
MCFixupKind(RISCV::fixup_riscv_sub_32));
case 8:
return std::make_pair(MCFixupKind(RISCV::fixup_riscv_add_64),
MCFixupKind(RISCV::fixup_riscv_sub_64));
}
}

} // end namespace llvm::RISCV

#endif

0 comments on commit b0cb5cb

Please sign in to comment.