[llvm] 7f9114e - [RISCV] Change the return type of getStreamer() to support the use of overloading and other functions in RISCVELFStreamer

via llvm-commits llvm-commits at lists.llvm.org
Thu Jan 12 19:50:42 PST 2023


Author: LiDongjin
Date: 2023-01-13T11:49:23+08:00
New Revision: 7f9114e0d308a612bb0f289b981ba5d37f59441c

URL: https://github.com/llvm/llvm-project/commit/7f9114e0d308a612bb0f289b981ba5d37f59441c
DIFF: https://github.com/llvm/llvm-project/commit/7f9114e0d308a612bb0f289b981ba5d37f59441c.diff

LOG: [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

Added: 
    

Modified: 
    llvm/lib/Target/RISCV/MCTargetDesc/RISCVELFStreamer.cpp
    llvm/lib/Target/RISCV/MCTargetDesc/RISCVELFStreamer.h

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Target/RISCV/MCTargetDesc/RISCVELFStreamer.cpp b/llvm/lib/Target/RISCV/MCTargetDesc/RISCVELFStreamer.cpp
index f0b246b9fb09..379aaa713a00 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<MCELFStreamer &>(Streamer);
+RISCVELFStreamer &RISCVTargetELFStreamer::getStreamer() {
+  return static_cast<RISCVELFStreamer &>(Streamer);
 }
 
 void RISCVTargetELFStreamer::emitDirectiveOptionPush() {}
@@ -192,97 +192,85 @@ void RISCVTargetELFStreamer::emitDirectiveVariantCC(MCSymbol &Symbol) {
   cast<MCSymbolELF>(Symbol).setOther(ELF::STO_RISCV_VARIANT_CC);
 }
 
-namespace {
-class RISCVELFStreamer : public MCELFStreamer {
-  static std::pair<unsigned, unsigned> 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<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);
   }
+}
 
-  static bool requiresFixups(MCContext &C, const MCExpr *Value,
-                             const MCExpr *&LHS, const MCExpr *&RHS) {
-    const auto *MBE = dyn_cast<MCBinaryExpr>(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<MCBinaryExpr>(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 
diff erences ...
-    return A.isInSection() && B.isInSection() &&
-           A.getSection().getName() != B.getSection().getName();
-  }
-
-  void reset() override {
-    static_cast<RISCVTargetStreamer *>(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 
diff erences ...
+  return A.isInSection() && B.isInSection() &&
+         A.getSection().getName() != B.getSection().getName();
+}
 
-public:
-  RISCVELFStreamer(MCContext &C, std::unique_ptr<MCAsmBackend> MAB,
-                   std::unique_ptr<MCObjectWriter> MOW,
-                   std::unique_ptr<MCCodeEmitter> MCE)
-      : MCELFStreamer(C, std::move(MAB), std::move(MOW), std::move(MCE)) {}
+void RISCVELFStreamer::reset() {
+  static_cast<RISCVTargetStreamer *>(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<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, static_cast<MCFixupKind>(Add), Loc));
+  DF->getFixups().push_back(MCFixup::create(
+      DF->getContents().size(), B, static_cast<MCFixupKind>(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 ccb20af0caa5..7331894f637d 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<unsigned, unsigned> 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<MCAsmBackend> MAB,
+                   std::unique_ptr<MCObjectWriter> MOW,
+                   std::unique_ptr<MCCodeEmitter> 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 @@ class RISCVTargetELFStreamer : public RISCVTargetStreamer {
   void reset() override;
 
 public:
-  MCELFStreamer &getStreamer();
+  RISCVELFStreamer &getStreamer();
   RISCVTargetELFStreamer(MCStreamer &S, const MCSubtargetInfo &STI);
 
   void emitDirectiveOptionPush() override;


        


More information about the llvm-commits mailing list