xref: /freebsd/contrib/llvm-project/llvm/include/llvm/BinaryFormat/WasmTraits.h (revision 7a6dacaca14b62ca4b74406814becb87a3fefac0)
1 //===- WasmTraits.h - DenseMap traits for the Wasm structures ---*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file provides llvm::DenseMapInfo traits for the Wasm structures.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef LLVM_BINARYFORMAT_WASMTRAITS_H
14 #define LLVM_BINARYFORMAT_WASMTRAITS_H
15 
16 #include "llvm/ADT/Hashing.h"
17 #include "llvm/BinaryFormat/Wasm.h"
18 
19 namespace llvm {
20 
21 // Traits for using WasmSignature in a DenseMap.
22 template <> struct DenseMapInfo<wasm::WasmSignature, void> {
23   static wasm::WasmSignature getEmptyKey() {
24     wasm::WasmSignature Sig;
25     Sig.State = wasm::WasmSignature::Empty;
26     return Sig;
27   }
28   static wasm::WasmSignature getTombstoneKey() {
29     wasm::WasmSignature Sig;
30     Sig.State = wasm::WasmSignature::Tombstone;
31     return Sig;
32   }
33   static unsigned getHashValue(const wasm::WasmSignature &Sig) {
34     uintptr_t H = hash_value(Sig.State);
35     for (auto Ret : Sig.Returns)
36       H = hash_combine(H, Ret);
37     for (auto Param : Sig.Params)
38       H = hash_combine(H, Param);
39     return H;
40   }
41   static bool isEqual(const wasm::WasmSignature &LHS,
42                       const wasm::WasmSignature &RHS) {
43     return LHS == RHS;
44   }
45 };
46 
47 // Traits for using WasmGlobalType in a DenseMap
48 template <> struct DenseMapInfo<wasm::WasmGlobalType, void> {
49   static wasm::WasmGlobalType getEmptyKey() {
50     return wasm::WasmGlobalType{1, true};
51   }
52   static wasm::WasmGlobalType getTombstoneKey() {
53     return wasm::WasmGlobalType{2, true};
54   }
55   static unsigned getHashValue(const wasm::WasmGlobalType &GlobalType) {
56     return hash_combine(GlobalType.Type, GlobalType.Mutable);
57   }
58   static bool isEqual(const wasm::WasmGlobalType &LHS,
59                       const wasm::WasmGlobalType &RHS) {
60     return LHS == RHS;
61   }
62 };
63 
64 // Traits for using WasmLimits in a DenseMap
65 template <> struct DenseMapInfo<wasm::WasmLimits, void> {
66   static wasm::WasmLimits getEmptyKey() {
67     return wasm::WasmLimits{0xff, 0xff, 0xff};
68   }
69   static wasm::WasmLimits getTombstoneKey() {
70     return wasm::WasmLimits{0xee, 0xee, 0xee};
71   }
72   static unsigned getHashValue(const wasm::WasmLimits &Limits) {
73     unsigned Hash = hash_value(Limits.Flags);
74     Hash = hash_combine(Hash, Limits.Minimum);
75     if (Limits.Flags & llvm::wasm::WASM_LIMITS_FLAG_HAS_MAX) {
76       Hash = hash_combine(Hash, Limits.Maximum);
77     }
78     return Hash;
79   }
80   static bool isEqual(const wasm::WasmLimits &LHS,
81                       const wasm::WasmLimits &RHS) {
82     return LHS == RHS;
83   }
84 };
85 
86 // Traits for using WasmTableType in a DenseMap
87 template <> struct DenseMapInfo<wasm::WasmTableType, void> {
88   static wasm::WasmTableType getEmptyKey() {
89     return wasm::WasmTableType{
90         wasm::ValType(0), DenseMapInfo<wasm::WasmLimits, void>::getEmptyKey()};
91   }
92   static wasm::WasmTableType getTombstoneKey() {
93     return wasm::WasmTableType{
94         wasm::ValType(1),
95         DenseMapInfo<wasm::WasmLimits, void>::getTombstoneKey()};
96   }
97   static unsigned getHashValue(const wasm::WasmTableType &TableType) {
98     return hash_combine(
99         TableType.ElemType,
100         DenseMapInfo<wasm::WasmLimits, void>::getHashValue(TableType.Limits));
101   }
102   static bool isEqual(const wasm::WasmTableType &LHS,
103                       const wasm::WasmTableType &RHS) {
104     return LHS == RHS;
105   }
106 };
107 
108 } // end namespace llvm
109 
110 #endif // LLVM_BINARYFORMAT_WASMTRAITS_H
111