wasm-subtyping.h 4.4 KB
Newer Older
1 2 3 4
// Copyright 2020 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5 6 7 8
#if !V8_ENABLE_WEBASSEMBLY
#error This header should only be included if WebAssembly is enabled.
#endif  // !V8_ENABLE_WEBASSEMBLY

9 10 11 12 13 14 15 16 17 18
#ifndef V8_WASM_WASM_SUBTYPING_H_
#define V8_WASM_WASM_SUBTYPING_H_

#include "src/wasm/value-type.h"

namespace v8 {
namespace internal {
namespace wasm {

struct WasmModule;
19 20 21 22 23

V8_NOINLINE V8_EXPORT_PRIVATE bool IsSubtypeOfImpl(
    ValueType subtype, ValueType supertype, const WasmModule* sub_module,
    const WasmModule* super_module);

24 25 26 27
// Checks if type1, defined in module1, is equivalent with type2, defined in
// module2.
// Type equivalence (~) is described by the following rules (structural
// equivalence):
28
// - Two numeric types are equivalent iff they are equal.
29 30 31 32 33 34
// - optref(ht1) ~ optref(ht2) iff ht1 ~ ht2.
// - ref(ht1) ~ ref(ht2) iff ht1 ~ ht2.
// - rtt(d1, ht1) ~ rtt(d2, ht2) iff (d1 = d2 and ht1 ~ ht2).
// For heap types, the following rules hold:
// - Two generic heap types are equivalent iff they are equal.
// - Two structs are equivalent iff they contain the same number of fields and
35
//   these are pairwise equivalent.
36
// - Two functions are equivalent iff they contain the same number of parameters
37
//   and returns and these are pairwise equivalent.
38
// - Two arrays are equivalent iff their underlying types are equivalent.
39 40 41
V8_NOINLINE bool EquivalentTypes(ValueType type1, ValueType type2,
                                 const WasmModule* module1,
                                 const WasmModule* module2);
42

43 44 45 46 47
// Checks if subtype, defined in module1, is a subtype of supertype, defined in
// module2.
// Subtyping between value types is described by the following rules
// (structural subtyping):
// - numeric types are subtype-related iff they are equal.
48 49
// - optref(ht1) <: optref(ht2) iff ht1 <: ht2.
// - ref(ht1) <: ref/optref(ht2) iff ht1 <: ht2.
50 51
// - rtt1 <: rtt2 iff rtt1 ~ rtt2.
// For heap types, the following subtyping rules hold:
52 53 54 55 56 57 58
// - The abstract heap types form the following type hierarchy:
//           any
//         /  |  \
//       eq func  extern
//      / \
//   i31   data
// - All structs and arrays are subtypes of data.
59 60 61 62 63 64
// - All functions are subtypes of func.
// - Struct subtyping: Subtype must have at least as many fields as supertype,
//   covariance for immutable fields, equivalence for mutable fields.
// - Array subtyping (mutable only) is the equivalence relation.
// - Function subtyping is the equivalence relation (note: this rule might
//   change in the future to include type variance).
65 66 67 68 69 70 71
V8_INLINE bool IsSubtypeOf(ValueType subtype, ValueType supertype,
                           const WasmModule* sub_module,
                           const WasmModule* super_module) {
  if (subtype == supertype && sub_module == super_module) return true;
  return IsSubtypeOfImpl(subtype, supertype, sub_module, super_module);
}

72
// Checks if 'subtype' is a subtype of 'supertype' (both defined in module).
73 74
V8_INLINE bool IsSubtypeOf(ValueType subtype, ValueType supertype,
                           const WasmModule* module) {
75
  // If the types are trivially identical, exit early.
76
  if (V8_LIKELY(subtype == supertype)) return true;
77
  return IsSubtypeOfImpl(subtype, supertype, module, module);
78 79
}

80
// We have this function call IsSubtypeOf instead of the opposite because type
81 82 83
// checks are much more common than heap type checks.}
V8_INLINE bool IsHeapSubtypeOf(uint32_t subtype_index,
                               HeapType::Representation supertype,
84
                               const WasmModule* module) {
85
  return IsSubtypeOf(ValueType::Ref(subtype_index, kNonNullable),
86 87
                     ValueType::Ref(supertype, kNonNullable), module);
}
88 89 90 91 92
V8_INLINE bool IsHeapSubtypeOf(uint32_t subtype_index, uint32_t supertype_index,
                               const WasmModule* module) {
  return IsSubtypeOf(ValueType::Ref(subtype_index, kNonNullable),
                     ValueType::Ref(supertype_index, kNonNullable), module);
}
93

94 95 96 97
// Returns the weakest type that is a subtype of both a and b
// (which is currently always one of a, b, or kWasmBottom).
// TODO(manoskouk): Update this once we have settled on a type system for
// reference types.
98 99 100 101 102 103 104
ValueType CommonSubtype(ValueType a, ValueType b, const WasmModule* module);

}  // namespace wasm
}  // namespace internal
}  // namespace v8

#endif  // V8_WASM_WASM_SUBTYPING_H_