V8 API Reference, 7.2.502.16 (for Deno 0.2.4)
value-type.h
1 // Copyright 2018 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_TYPE_H_
6 #define V8_WASM_VALUE_TYPE_H_
7 
8 #include "src/machine-type.h"
9 #include "src/wasm/wasm-constants.h"
10 
11 namespace v8 {
12 namespace internal {
13 
14 template <typename T>
15 class Signature;
16 
17 namespace wasm {
18 
19 enum ValueType : uint8_t {
20  kWasmStmt,
21  kWasmI32,
22  kWasmI64,
23  kWasmF32,
24  kWasmF64,
25  kWasmS128,
26  kWasmAnyRef,
27  kWasmAnyFunc,
28  kWasmExceptRef,
29  kWasmVar,
30 };
31 
32 using FunctionSig = Signature<ValueType>;
33 
34 inline size_t hash_value(ValueType type) { return static_cast<size_t>(type); }
35 
36 // TODO(clemensh): Compute memtype and size from ValueType once we have c++14
37 // constexpr support.
38 #define FOREACH_LOAD_TYPE(V) \
39  V(I32, , Int32, 2) \
40  V(I32, 8S, Int8, 0) \
41  V(I32, 8U, Uint8, 0) \
42  V(I32, 16S, Int16, 1) \
43  V(I32, 16U, Uint16, 1) \
44  V(I64, , Int64, 3) \
45  V(I64, 8S, Int8, 0) \
46  V(I64, 8U, Uint8, 0) \
47  V(I64, 16S, Int16, 1) \
48  V(I64, 16U, Uint16, 1) \
49  V(I64, 32S, Int32, 2) \
50  V(I64, 32U, Uint32, 2) \
51  V(F32, , Float32, 2) \
52  V(F64, , Float64, 3) \
53  V(S128, , Simd128, 4)
54 
55 class LoadType {
56  public:
57  enum LoadTypeValue : uint8_t {
58 #define DEF_ENUM(type, suffix, ...) k##type##Load##suffix,
59  FOREACH_LOAD_TYPE(DEF_ENUM)
60 #undef DEF_ENUM
61  };
62 
63  // Allow implicit convertion of the enum value to this wrapper.
64  constexpr LoadType(LoadTypeValue val) // NOLINT(runtime/explicit)
65  : val_(val) {}
66 
67  constexpr LoadTypeValue value() const { return val_; }
68  constexpr unsigned size_log_2() const { return kLoadSizeLog2[val_]; }
69  constexpr unsigned size() const { return 1 << size_log_2(); }
70  constexpr ValueType value_type() const { return kValueType[val_]; }
71  constexpr MachineType mem_type() const { return kMemType[val_]; }
72 
73  static LoadType ForValueType(ValueType type) {
74  switch (type) {
75  case kWasmI32:
76  return kI32Load;
77  case kWasmI64:
78  return kI64Load;
79  case kWasmF32:
80  return kF32Load;
81  case kWasmF64:
82  return kF64Load;
83  default:
84  UNREACHABLE();
85  }
86  }
87 
88  private:
89  const LoadTypeValue val_;
90 
91  static constexpr uint8_t kLoadSizeLog2[] = {
92 #define LOAD_SIZE(_, __, ___, size) size,
93  FOREACH_LOAD_TYPE(LOAD_SIZE)
94 #undef LOAD_SIZE
95  };
96 
97  static constexpr ValueType kValueType[] = {
98 #define VALUE_TYPE(type, ...) kWasm##type,
99  FOREACH_LOAD_TYPE(VALUE_TYPE)
100 #undef VALUE_TYPE
101  };
102 
103  static constexpr MachineType kMemType[] = {
104 #define MEMTYPE(_, __, memtype, ___) MachineType::memtype(),
105  FOREACH_LOAD_TYPE(MEMTYPE)
106 #undef MEMTYPE
107  };
108 };
109 
110 #define FOREACH_STORE_TYPE(V) \
111  V(I32, , Word32, 2) \
112  V(I32, 8, Word8, 0) \
113  V(I32, 16, Word16, 1) \
114  V(I64, , Word64, 3) \
115  V(I64, 8, Word8, 0) \
116  V(I64, 16, Word16, 1) \
117  V(I64, 32, Word32, 2) \
118  V(F32, , Float32, 2) \
119  V(F64, , Float64, 3) \
120  V(S128, , Simd128, 4)
121 
122 class StoreType {
123  public:
124  enum StoreTypeValue : uint8_t {
125 #define DEF_ENUM(type, suffix, ...) k##type##Store##suffix,
126  FOREACH_STORE_TYPE(DEF_ENUM)
127 #undef DEF_ENUM
128  };
129 
130  // Allow implicit convertion of the enum value to this wrapper.
131  constexpr StoreType(StoreTypeValue val) // NOLINT(runtime/explicit)
132  : val_(val) {}
133 
134  constexpr StoreTypeValue value() const { return val_; }
135  constexpr unsigned size_log_2() const { return kStoreSizeLog2[val_]; }
136  constexpr unsigned size() const { return 1 << size_log_2(); }
137  constexpr ValueType value_type() const { return kValueType[val_]; }
138  constexpr MachineRepresentation mem_rep() const { return kMemRep[val_]; }
139 
140  static StoreType ForValueType(ValueType type) {
141  switch (type) {
142  case kWasmI32:
143  return kI32Store;
144  case kWasmI64:
145  return kI64Store;
146  case kWasmF32:
147  return kF32Store;
148  case kWasmF64:
149  return kF64Store;
150  default:
151  UNREACHABLE();
152  }
153  }
154 
155  private:
156  const StoreTypeValue val_;
157 
158  static constexpr uint8_t kStoreSizeLog2[] = {
159 #define STORE_SIZE(_, __, ___, size) size,
160  FOREACH_STORE_TYPE(STORE_SIZE)
161 #undef STORE_SIZE
162  };
163 
164  static constexpr ValueType kValueType[] = {
165 #define VALUE_TYPE(type, ...) kWasm##type,
166  FOREACH_STORE_TYPE(VALUE_TYPE)
167 #undef VALUE_TYPE
168  };
169 
170  static constexpr MachineRepresentation kMemRep[] = {
171 #define MEMREP(_, __, memrep, ___) MachineRepresentation::k##memrep,
172  FOREACH_STORE_TYPE(MEMREP)
173 #undef MEMREP
174  };
175 };
176 
177 // A collection of ValueType-related static methods.
178 class V8_EXPORT_PRIVATE ValueTypes {
179  public:
180  static byte MemSize(MachineType type) {
181  return 1 << i::ElementSizeLog2Of(type.representation());
182  }
183 
184  static int ElementSizeInBytes(ValueType type) {
185  switch (type) {
186  case kWasmI32:
187  case kWasmF32:
188  return 4;
189  case kWasmI64:
190  case kWasmF64:
191  return 8;
192  case kWasmS128:
193  return 16;
194  default:
195  UNREACHABLE();
196  }
197  }
198 
199  static int ElementSizeLog2Of(ValueType type) {
200  switch (type) {
201  case kWasmI32:
202  case kWasmF32:
203  return 2;
204  case kWasmI64:
205  case kWasmF64:
206  return 3;
207  case kWasmS128:
208  return 4;
209  default:
210  UNREACHABLE();
211  }
212  }
213 
214  static byte MemSize(ValueType type) { return 1 << ElementSizeLog2Of(type); }
215 
216  static ValueTypeCode ValueTypeCodeFor(ValueType type) {
217  switch (type) {
218  case kWasmI32:
219  return kLocalI32;
220  case kWasmI64:
221  return kLocalI64;
222  case kWasmF32:
223  return kLocalF32;
224  case kWasmF64:
225  return kLocalF64;
226  case kWasmS128:
227  return kLocalS128;
228  case kWasmAnyRef:
229  return kLocalAnyRef;
230  case kWasmExceptRef:
231  return kLocalExceptRef;
232  case kWasmStmt:
233  return kLocalVoid;
234  default:
235  UNREACHABLE();
236  }
237  }
238 
239  static MachineType MachineTypeFor(ValueType type) {
240  switch (type) {
241  case kWasmI32:
242  return MachineType::Int32();
243  case kWasmI64:
244  return MachineType::Int64();
245  case kWasmF32:
246  return MachineType::Float32();
247  case kWasmF64:
248  return MachineType::Float64();
249  case kWasmAnyFunc:
250  case kWasmAnyRef:
251  return MachineType::TaggedPointer();
252  case kWasmS128:
253  return MachineType::Simd128();
254  case kWasmStmt:
255  return MachineType::None();
256  default:
257  UNREACHABLE();
258  }
259  }
260 
261  static MachineRepresentation MachineRepresentationFor(ValueType type) {
262  switch (type) {
263  case kWasmI32:
264  return MachineRepresentation::kWord32;
265  case kWasmI64:
266  return MachineRepresentation::kWord64;
267  case kWasmF32:
268  return MachineRepresentation::kFloat32;
269  case kWasmF64:
270  return MachineRepresentation::kFloat64;
271  case kWasmAnyRef:
272  return MachineRepresentation::kTaggedPointer;
273  case kWasmS128:
274  return MachineRepresentation::kSimd128;
275  case kWasmStmt:
276  return MachineRepresentation::kNone;
277  default:
278  UNREACHABLE();
279  }
280  }
281 
282  static ValueType ValueTypeFor(MachineType type) {
283  switch (type.representation()) {
284  case MachineRepresentation::kWord8:
285  case MachineRepresentation::kWord16:
286  case MachineRepresentation::kWord32:
287  return kWasmI32;
288  case MachineRepresentation::kWord64:
289  return kWasmI64;
290  case MachineRepresentation::kFloat32:
291  return kWasmF32;
292  case MachineRepresentation::kFloat64:
293  return kWasmF64;
294  case MachineRepresentation::kTaggedPointer:
295  return kWasmAnyRef;
296  case MachineRepresentation::kSimd128:
297  return kWasmS128;
298  default:
299  UNREACHABLE();
300  }
301  }
302 
303  static char ShortNameOf(ValueType type) {
304  switch (type) {
305  case kWasmI32:
306  return 'i';
307  case kWasmI64:
308  return 'l';
309  case kWasmF32:
310  return 'f';
311  case kWasmF64:
312  return 'd';
313  case kWasmAnyRef:
314  return 'r';
315  case kWasmS128:
316  return 's';
317  case kWasmStmt:
318  return 'v';
319  case kWasmVar:
320  return '*';
321  default:
322  return '?';
323  }
324  }
325 
326  static const char* TypeName(ValueType type) {
327  switch (type) {
328  case kWasmI32:
329  return "i32";
330  case kWasmI64:
331  return "i64";
332  case kWasmF32:
333  return "f32";
334  case kWasmF64:
335  return "f64";
336  case kWasmAnyRef:
337  return "ref";
338  case kWasmS128:
339  return "s128";
340  case kWasmStmt:
341  return "<stmt>";
342  case kWasmVar:
343  return "<var>";
344  default:
345  return "<unknown>";
346  }
347  }
348 
349  private:
350  DISALLOW_IMPLICIT_CONSTRUCTORS(ValueTypes);
351 };
352 
353 } // namespace wasm
354 } // namespace internal
355 } // namespace v8
356 
357 #endif // V8_WASM_VALUE_TYPE_H_
Definition: libplatform.h:13