[RISCV] Move RISCVELFStreamer::getRelocPairForSize to RISCVFixUpKinds.h and reuse...
authorCraig Topper <craig.topper@sifive.com>
Tue, 31 Jan 2023 21:09:56 +0000 (13:09 -0800)
committerCraig Topper <craig.topper@sifive.com>
Tue, 31 Jan 2023 21:10:13 +0000 (13:10 -0800)
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

llvm/lib/Target/RISCV/MCTargetDesc/RISCVAsmBackend.cpp
llvm/lib/Target/RISCV/MCTargetDesc/RISCVELFStreamer.cpp
llvm/lib/Target/RISCV/MCTargetDesc/RISCVELFStreamer.h
llvm/lib/Target/RISCV/MCTargetDesc/RISCVFixupKinds.h

index 892c406..56c2a0d 100644 (file)
@@ -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
@@ -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);
index 379aaa7..f69fc7c 100644 (file)
@@ -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);
@@ -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);
 }
index 7331894..80b8be2 100644 (file)
@@ -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;
index 67841d2..d254f3a 100644 (file)
@@ -10,6 +10,7 @@
 #define LLVM_LIB_TARGET_RISCV_MCTARGETDESC_RISCVFIXUPKINDS_H
 
 #include "llvm/MC/MCFixup.h"
+#include <utility>
 
 #undef RISCV
 
@@ -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