From 7f9114e0d308a612bb0f289b981ba5d37f59441c Mon Sep 17 00:00:00 2001 From: LiDongjin Date: Fri, 13 Jan 2023 11:48:22 +0800 Subject: [PATCH] [RISCV] Change the return type of getStreamer() to support the use of overloading and other functions in RISCVELFStreamer Move the declaration of RISCVELFStreamer from RISCVELFStreamer.cpp to RISCVELFStreamer.h. Change the return type of getStreamer() to support the use of overloading and other functions in RISCVELFStreamer. Differential Revision: https://reviews.llvm.org/D138500 --- .../Target/RISCV/MCTargetDesc/RISCVELFStreamer.cpp | 148 ++++++++++----------- .../Target/RISCV/MCTargetDesc/RISCVELFStreamer.h | 19 ++- 2 files changed, 86 insertions(+), 81 deletions(-) diff --git a/llvm/lib/Target/RISCV/MCTargetDesc/RISCVELFStreamer.cpp b/llvm/lib/Target/RISCV/MCTargetDesc/RISCVELFStreamer.cpp index f0b246b..379aaa7 100644 --- a/llvm/lib/Target/RISCV/MCTargetDesc/RISCVELFStreamer.cpp +++ b/llvm/lib/Target/RISCV/MCTargetDesc/RISCVELFStreamer.cpp @@ -39,8 +39,8 @@ RISCVTargetELFStreamer::RISCVTargetELFStreamer(MCStreamer &S, MAB.getTargetOptions().getABIName())); } -MCELFStreamer &RISCVTargetELFStreamer::getStreamer() { - return static_cast(Streamer); +RISCVELFStreamer &RISCVTargetELFStreamer::getStreamer() { + return static_cast(Streamer); } void RISCVTargetELFStreamer::emitDirectiveOptionPush() {} @@ -192,97 +192,85 @@ void RISCVTargetELFStreamer::emitDirectiveVariantCC(MCSymbol &Symbol) { cast(Symbol).setOther(ELF::STO_RISCV_VARIANT_CC); } -namespace { -class RISCVELFStreamer : public MCELFStreamer { - static std::pair 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); - } +std::pair +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); } +} - static bool requiresFixups(MCContext &C, const MCExpr *Value, - const MCExpr *&LHS, const MCExpr *&RHS) { - const auto *MBE = dyn_cast(Value); - if (MBE == nullptr) - return false; +bool RISCVELFStreamer::requiresFixups(MCContext &C, const MCExpr *Value, + const MCExpr *&LHS, const MCExpr *&RHS) { + const auto *MBE = dyn_cast(Value); + if (MBE == nullptr) + return false; - MCValue E; - if (!Value->evaluateAsRelocatable(E, nullptr, nullptr)) - return false; - if (E.getSymA() == nullptr || E.getSymB() == nullptr) - return false; + MCValue E; + if (!Value->evaluateAsRelocatable(E, nullptr, nullptr)) + return false; + if (E.getSymA() == nullptr || E.getSymB() == nullptr) + return false; - const auto &A = E.getSymA()->getSymbol(); - const auto &B = E.getSymB()->getSymbol(); + const auto &A = E.getSymA()->getSymbol(); + const auto &B = E.getSymB()->getSymbol(); - LHS = - MCBinaryExpr::create(MCBinaryExpr::Add, MCSymbolRefExpr::create(&A, C), + LHS = MCBinaryExpr::create(MCBinaryExpr::Add, MCSymbolRefExpr::create(&A, C), MCConstantExpr::create(E.getConstant(), C), C); - RHS = E.getSymB(); - - // If either symbol is in a text section, we need to delay the relocation - // evaluation as relaxation may alter the size of the symbol. - // - // Unfortunately, we cannot identify if the symbol was built with relaxation - // as we do not track the state per symbol or section. However, BFD will - // always emit the relocation and so we follow suit which avoids the need to - // track that information. - if (A.isInSection() && A.getSection().getKind().isText()) - return true; - if (B.isInSection() && B.getSection().getKind().isText()) - return true; - - // Support cross-section symbolic differences ... - return A.isInSection() && B.isInSection() && - A.getSection().getName() != B.getSection().getName(); - } - - void reset() override { - static_cast(getTargetStreamer())->reset(); - MCELFStreamer::reset(); - } + RHS = E.getSymB(); + + // If either symbol is in a text section, we need to delay the relocation + // evaluation as relaxation may alter the size of the symbol. + // + // Unfortunately, we cannot identify if the symbol was built with relaxation + // as we do not track the state per symbol or section. However, BFD will + // always emit the relocation and so we follow suit which avoids the need to + // track that information. + if (A.isInSection() && A.getSection().getKind().isText()) + return true; + if (B.isInSection() && B.getSection().getKind().isText()) + return true; + + // Support cross-section symbolic differences ... + return A.isInSection() && B.isInSection() && + A.getSection().getName() != B.getSection().getName(); +} -public: - RISCVELFStreamer(MCContext &C, std::unique_ptr MAB, - std::unique_ptr MOW, - std::unique_ptr MCE) - : MCELFStreamer(C, std::move(MAB), std::move(MOW), std::move(MCE)) {} +void RISCVELFStreamer::reset() { + static_cast(getTargetStreamer())->reset(); + MCELFStreamer::reset(); +} - void emitValueImpl(const MCExpr *Value, unsigned Size, SMLoc Loc) override { - const MCExpr *A, *B; - if (!requiresFixups(getContext(), Value, A, B)) - return MCELFStreamer::emitValueImpl(Value, Size, Loc); +void RISCVELFStreamer::emitValueImpl(const MCExpr *Value, unsigned Size, + SMLoc Loc) { + const MCExpr *A, *B; + if (!requiresFixups(getContext(), Value, A, B)) + return MCELFStreamer::emitValueImpl(Value, Size, Loc); - MCStreamer::emitValueImpl(Value, Size, Loc); + MCStreamer::emitValueImpl(Value, Size, Loc); - MCDataFragment *DF = getOrCreateDataFragment(); - flushPendingLabels(DF, DF->getContents().size()); - MCDwarfLineEntry::make(this, getCurrentSectionOnly()); + MCDataFragment *DF = getOrCreateDataFragment(); + flushPendingLabels(DF, DF->getContents().size()); + MCDwarfLineEntry::make(this, getCurrentSectionOnly()); - unsigned Add, Sub; - std::tie(Add, Sub) = getRelocPairForSize(Size); + unsigned Add, Sub; + std::tie(Add, Sub) = getRelocPairForSize(Size); - DF->getFixups().push_back(MCFixup::create( - DF->getContents().size(), A, static_cast(Add), Loc)); - DF->getFixups().push_back(MCFixup::create( - DF->getContents().size(), B, static_cast(Sub), Loc)); + DF->getFixups().push_back(MCFixup::create( + DF->getContents().size(), A, static_cast(Add), Loc)); + DF->getFixups().push_back(MCFixup::create( + DF->getContents().size(), B, static_cast(Sub), Loc)); - DF->getContents().resize(DF->getContents().size() + Size, 0); - } -}; -} // namespace + DF->getContents().resize(DF->getContents().size() + Size, 0); +} namespace llvm { MCELFStreamer *createRISCVELFStreamer(MCContext &C, diff --git a/llvm/lib/Target/RISCV/MCTargetDesc/RISCVELFStreamer.h b/llvm/lib/Target/RISCV/MCTargetDesc/RISCVELFStreamer.h index ccb20af..7331894 100644 --- a/llvm/lib/Target/RISCV/MCTargetDesc/RISCVELFStreamer.h +++ b/llvm/lib/Target/RISCV/MCTargetDesc/RISCVELFStreamer.h @@ -12,6 +12,23 @@ #include "RISCVTargetStreamer.h" #include "llvm/MC/MCELFStreamer.h" +using namespace llvm; + +class RISCVELFStreamer : public MCELFStreamer { + static std::pair getRelocPairForSize(unsigned Size); + static bool requiresFixups(MCContext &C, const MCExpr *Value, + const MCExpr *&LHS, const MCExpr *&RHS); + void reset() override; + +public: + RISCVELFStreamer(MCContext &C, std::unique_ptr MAB, + std::unique_ptr MOW, + std::unique_ptr MCE) + : MCELFStreamer(C, std::move(MAB), std::move(MOW), std::move(MCE)) {} + + void emitValueImpl(const MCExpr *Value, unsigned Size, SMLoc Loc) override; +}; + namespace llvm { class RISCVTargetELFStreamer : public RISCVTargetStreamer { @@ -95,7 +112,7 @@ private: void reset() override; public: - MCELFStreamer &getStreamer(); + RISCVELFStreamer &getStreamer(); RISCVTargetELFStreamer(MCStreamer &S, const MCSubtargetInfo &STI); void emitDirectiveOptionPush() override; -- 2.7.4