[llvm] a205f29 - [WebAssembly] Consolidate sectionTypeToString in BinaryFormat [NFC]

Derek Schuff via llvm-commits llvm-commits at lists.llvm.org
Fri May 27 09:28:37 PDT 2022


Author: Derek Schuff
Date: 2022-05-27T09:26:36-07:00
New Revision: a205f2904d0ae4e3c6ce73b6e0a0aff29e46bd96

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

LOG: [WebAssembly] Consolidate sectionTypeToString in BinaryFormat [NFC]

Currently there are 2 duplicate implementation, and I want to add
a use in a 3rd place. Combine them in lib/BinaryFormat so they can
be shared.

Also update toString for symbol and reloc types to use StringRef

Differential Revision: https://reviews.llvm.org/D126553

Added: 
    

Modified: 
    lld/wasm/OutputSections.cpp
    llvm/include/llvm/BinaryFormat/Wasm.h
    llvm/lib/BinaryFormat/Wasm.cpp
    llvm/lib/Object/WasmObjectFile.cpp

Removed: 
    


################################################################################
diff  --git a/lld/wasm/OutputSections.cpp b/lld/wasm/OutputSections.cpp
index d23fcc35883c..f7e4df8900a2 100644
--- a/lld/wasm/OutputSections.cpp
+++ b/lld/wasm/OutputSections.cpp
@@ -33,41 +33,6 @@ std::string toString(const wasm::OutputSection &sec) {
 }
 
 namespace wasm {
-static StringRef sectionTypeToString(uint32_t sectionType) {
-  switch (sectionType) {
-  case WASM_SEC_CUSTOM:
-    return "CUSTOM";
-  case WASM_SEC_TYPE:
-    return "TYPE";
-  case WASM_SEC_IMPORT:
-    return "IMPORT";
-  case WASM_SEC_FUNCTION:
-    return "FUNCTION";
-  case WASM_SEC_TABLE:
-    return "TABLE";
-  case WASM_SEC_MEMORY:
-    return "MEMORY";
-  case WASM_SEC_GLOBAL:
-    return "GLOBAL";
-  case WASM_SEC_TAG:
-    return "TAG";
-  case WASM_SEC_EXPORT:
-    return "EXPORT";
-  case WASM_SEC_START:
-    return "START";
-  case WASM_SEC_ELEM:
-    return "ELEM";
-  case WASM_SEC_CODE:
-    return "CODE";
-  case WASM_SEC_DATA:
-    return "DATA";
-  case WASM_SEC_DATACOUNT:
-    return "DATACOUNT";
-  default:
-    fatal("invalid section type");
-  }
-}
-
 StringRef OutputSection::getSectionName() const {
   return sectionTypeToString(type);
 }

diff  --git a/llvm/include/llvm/BinaryFormat/Wasm.h b/llvm/include/llvm/BinaryFormat/Wasm.h
index 2419a06fbe96..4b6a1d584077 100644
--- a/llvm/include/llvm/BinaryFormat/Wasm.h
+++ b/llvm/include/llvm/BinaryFormat/Wasm.h
@@ -470,8 +470,9 @@ inline bool operator==(const WasmTableType &LHS, const WasmTableType &RHS) {
   return LHS.ElemType == RHS.ElemType && LHS.Limits == RHS.Limits;
 }
 
-std::string toString(WasmSymbolType type);
-std::string relocTypetoString(uint32_t type);
+llvm::StringRef toString(WasmSymbolType type);
+llvm::StringRef relocTypetoString(uint32_t type);
+llvm::StringRef sectionTypeToString(uint32_t type);
 bool relocTypeHasAddend(uint32_t type);
 
 } // end namespace wasm

diff  --git a/llvm/lib/BinaryFormat/Wasm.cpp b/llvm/lib/BinaryFormat/Wasm.cpp
index 55efe31f2669..babeb12e49ef 100644
--- a/llvm/lib/BinaryFormat/Wasm.cpp
+++ b/llvm/lib/BinaryFormat/Wasm.cpp
@@ -8,7 +8,7 @@
 
 #include "llvm/BinaryFormat/Wasm.h"
 
-std::string llvm::wasm::toString(wasm::WasmSymbolType Type) {
+llvm::StringRef llvm::wasm::toString(wasm::WasmSymbolType Type) {
   switch (Type) {
   case wasm::WASM_SYMBOL_TYPE_FUNCTION:
     return "WASM_SYMBOL_TYPE_FUNCTION";
@@ -26,7 +26,7 @@ std::string llvm::wasm::toString(wasm::WasmSymbolType Type) {
   llvm_unreachable("unknown symbol type");
 }
 
-std::string llvm::wasm::relocTypetoString(uint32_t Type) {
+llvm::StringRef llvm::wasm::relocTypetoString(uint32_t Type) {
   switch (Type) {
 #define WASM_RELOC(NAME, VALUE)                                                \
   case VALUE:                                                                  \
@@ -38,6 +38,31 @@ std::string llvm::wasm::relocTypetoString(uint32_t Type) {
   }
 }
 
+llvm::StringRef llvm::wasm::sectionTypeToString(uint32_t Type) {
+#define ECase(X)                                                               \
+  case wasm::WASM_SEC_##X:                                                     \
+    return #X;
+  switch (Type) {
+    ECase(CUSTOM);
+    ECase(TYPE);
+    ECase(IMPORT);
+    ECase(FUNCTION);
+    ECase(TABLE);
+    ECase(MEMORY);
+    ECase(GLOBAL);
+    ECase(EXPORT);
+    ECase(START);
+    ECase(ELEM);
+    ECase(CODE);
+    ECase(DATA);
+    ECase(DATACOUNT);
+    ECase(TAG);
+  default:
+    llvm_unreachable("unknown section type");
+  }
+#undef ECase
+}
+
 bool llvm::wasm::relocTypeHasAddend(uint32_t Type) {
   switch (Type) {
   case R_WASM_MEMORY_ADDR_LEB:

diff  --git a/llvm/lib/Object/WasmObjectFile.cpp b/llvm/lib/Object/WasmObjectFile.cpp
index dfb9e2b3eab2..73c2f868492e 100644
--- a/llvm/lib/Object/WasmObjectFile.cpp
+++ b/llvm/lib/Object/WasmObjectFile.cpp
@@ -1727,29 +1727,11 @@ void WasmObjectFile::moveSectionNext(DataRefImpl &Sec) const { Sec.d.a++; }
 
 Expected<StringRef> WasmObjectFile::getSectionName(DataRefImpl Sec) const {
   const WasmSection &S = Sections[Sec.d.a];
-#define ECase(X)                                                               \
-  case wasm::WASM_SEC_##X:                                                     \
-    return #X;
-  switch (S.Type) {
-    ECase(TYPE);
-    ECase(IMPORT);
-    ECase(FUNCTION);
-    ECase(TABLE);
-    ECase(MEMORY);
-    ECase(GLOBAL);
-    ECase(TAG);
-    ECase(EXPORT);
-    ECase(START);
-    ECase(ELEM);
-    ECase(CODE);
-    ECase(DATA);
-    ECase(DATACOUNT);
-  case wasm::WASM_SEC_CUSTOM:
+  if (S.Type == wasm::WASM_SEC_CUSTOM)
     return S.Name;
-  default:
+  if (S.Type > wasm::WASM_SEC_TAG)
     return createStringError(object_error::invalid_section_index, "");
-  }
-#undef ECase
+  return wasm::sectionTypeToString(S.Type);
 }
 
 uint64_t WasmObjectFile::getSectionAddress(DataRefImpl Sec) const { return 0; }


        


More information about the llvm-commits mailing list