WasmTraits.h 3.7 KB

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