Line data Source code
1 : // Copyright 2017 the V8 project authors. All rights reserved.
2 : // Use of this source code is governed by a BSD-style license that can be
3 : // found in the LICENSE file.
4 :
5 : #ifndef V8_WASM_VALUE_H_
6 : #define V8_WASM_VALUE_H_
7 :
8 : #include "src/wasm/wasm-opcodes.h"
9 : #include "src/zone/zone-containers.h"
10 :
11 : namespace v8 {
12 : namespace internal {
13 : namespace wasm {
14 :
15 : // Macro for defining WasmValue union members.
16 : #define FOREACH_WASMVAL_UNION_MEMBER(V) \
17 : V(i32, kWasmI32, int32_t) \
18 : V(u32, kWasmI32, uint32_t) \
19 : V(i64, kWasmI64, int64_t) \
20 : V(u64, kWasmI64, uint64_t) \
21 : V(f32, kWasmF32, float) \
22 : V(f64, kWasmF64, double)
23 :
24 : // A wasm value with type information.
25 : class WasmValue {
26 : public:
27 63592 : WasmValue() : type_(kWasmStmt) {}
28 :
29 : #define DEFINE_TYPE_SPECIFIC_METHODS(field, localtype, ctype) \
30 : explicit WasmValue(ctype v) : type_(localtype) { value_.field = v; } \
31 : ctype to_##field() const { \
32 : DCHECK_EQ(localtype, type_); \
33 : return value_.field; \
34 : }
35 2491962 : FOREACH_WASMVAL_UNION_MEMBER(DEFINE_TYPE_SPECIFIC_METHODS)
36 : #undef DEFINE_TYPE_SPECIFIC_METHODS
37 :
38 : ValueType type() const { return type_; }
39 :
40 312 : bool operator==(const WasmValue& other) const {
41 312 : if (type_ != other.type_) return false;
42 : #define CHECK_VALUE_EQ(field, localtype, ctype) \
43 : if (type_ == localtype) { \
44 : return value_.field == other.value_.field; \
45 : }
46 312 : FOREACH_WASMVAL_UNION_MEMBER(CHECK_VALUE_EQ)
47 : #undef CHECK_VALUE_EQ
48 0 : UNREACHABLE();
49 : }
50 :
51 : template <typename T>
52 : inline T to() const {
53 : static_assert(sizeof(T) == -1, "Do only use this method with valid types");
54 : }
55 :
56 : template <typename T>
57 : inline T to_unchecked() const {
58 : static_assert(sizeof(T) == -1, "Do only use this method with valid types");
59 : }
60 :
61 : private:
62 : ValueType type_;
63 : union {
64 : #define DECLARE_FIELD(field, localtype, ctype) ctype field;
65 : FOREACH_WASMVAL_UNION_MEMBER(DECLARE_FIELD)
66 : #undef DECLARE_FIELD
67 : } value_;
68 : };
69 :
70 : #define DECLARE_CAST(field, localtype, ctype) \
71 : template <> \
72 : inline ctype WasmValue::to_unchecked() const { \
73 : return value_.field; \
74 : } \
75 : template <> \
76 : inline ctype WasmValue::to() const { \
77 : return to_##field(); \
78 : }
79 2558581 : FOREACH_WASMVAL_UNION_MEMBER(DECLARE_CAST)
80 : #undef DECLARE_CAST
81 :
82 : } // namespace wasm
83 : } // namespace internal
84 : } // namespace v8
85 :
86 : #endif // V8_WASM_VALUE_H_
|