[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
This commit is contained in:
Derek Schuff 2022-05-27 08:23:37 -07:00
parent 042ae89556
commit a205f2904d
4 changed files with 33 additions and 60 deletions

View file

@ -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);
}

View file

@ -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

View file

@ -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:

View file

@ -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; }