diff --git a/llvm/lib/MC/WasmObjectWriter.cpp b/llvm/lib/MC/WasmObjectWriter.cpp --- a/llvm/lib/MC/WasmObjectWriter.cpp +++ b/llvm/lib/MC/WasmObjectWriter.cpp @@ -25,6 +25,7 @@ #include "llvm/MC/MCSymbolWasm.h" #include "llvm/MC/MCValue.h" #include "llvm/MC/MCWasmObjectWriter.h" +#include "llvm/Object/WasmTraits.h" #include "llvm/Support/Casting.h" #include "llvm/Support/Debug.h" #include "llvm/Support/EndianStream.h" @@ -56,50 +57,6 @@ uint32_t Index; }; -// The signature of a wasm function or event, in a struct capable of being used -// as a DenseMap key. -// TODO: Consider using wasm::WasmSignature directly instead. -struct WasmSignature { - // Support empty and tombstone instances, needed by DenseMap. - enum { Plain, Empty, Tombstone } State = Plain; - - // The return types of the function. - SmallVector Returns; - - // The parameter types of the function. - SmallVector Params; - - bool operator==(const WasmSignature &Other) const { - return State == Other.State && Returns == Other.Returns && - Params == Other.Params; - } -}; - -// Traits for using WasmSignature in a DenseMap. -struct WasmSignatureDenseMapInfo { - static WasmSignature getEmptyKey() { - WasmSignature Sig; - Sig.State = WasmSignature::Empty; - return Sig; - } - static WasmSignature getTombstoneKey() { - WasmSignature Sig; - Sig.State = WasmSignature::Tombstone; - return Sig; - } - static unsigned getHashValue(const WasmSignature &Sig) { - uintptr_t Value = Sig.State; - for (wasm::ValType Ret : Sig.Returns) - Value += DenseMapInfo::getHashValue(uint32_t(Ret)); - for (wasm::ValType Param : Sig.Params) - Value += DenseMapInfo::getHashValue(uint32_t(Param)); - return Value; - } - static bool isEqual(const WasmSignature &LHS, const WasmSignature &RHS) { - return LHS == RHS; - } -}; - // A wasm data segment. A wasm binary contains only a single data section // but that can contain many segments, each with their own virtual location // in memory. Each MCSection data created by llvm is modeled as its own @@ -256,8 +213,8 @@ // Map from section to defining function symbol. DenseMap SectionFunctions; - DenseMap SignatureIndices; - SmallVector Signatures; + DenseMap SignatureIndices; + SmallVector Signatures; SmallVector DataSegments; unsigned NumFunctionImports = 0; unsigned NumGlobalImports = 0; @@ -347,7 +304,7 @@ void writeValueType(wasm::ValType Ty) { W->OS << static_cast(Ty); } - void writeTypeSection(ArrayRef Signatures); + void writeTypeSection(ArrayRef Signatures); void writeImportSection(ArrayRef Imports, uint64_t DataSize, uint32_t NumElements); void writeFunctionSection(ArrayRef Functions); @@ -734,7 +691,8 @@ } } -void WasmObjectWriter::writeTypeSection(ArrayRef Signatures) { +void WasmObjectWriter::writeTypeSection( + ArrayRef Signatures) { if (Signatures.empty()) return; @@ -743,7 +701,7 @@ encodeULEB128(Signatures.size(), W->OS); - for (const WasmSignature &Sig : Signatures) { + for (const wasm::WasmSignature &Sig : Signatures) { W->OS << char(wasm::WASM_TYPE_FUNC); encodeULEB128(Sig.Params.size(), W->OS); for (wasm::ValType Ty : Sig.Params) @@ -1145,7 +1103,7 @@ void WasmObjectWriter::registerFunctionType(const MCSymbolWasm &Symbol) { assert(Symbol.isFunction()); - WasmSignature S; + wasm::WasmSignature S; if (auto *Sig = Symbol.getSignature()) { S.Returns = Sig->Returns; @@ -1167,7 +1125,7 @@ // TODO Currently we don't generate imported exceptions, but if we do, we // should have a way of infering types of imported exceptions. - WasmSignature S; + wasm::WasmSignature S; if (auto *Sig = Symbol.getSignature()) { S.Returns = Sig->Returns; S.Params = Sig->Params;