v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
constant-expression.h
Go to the documentation of this file.
1// Copyright 2022 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_CONSTANT_EXPRESSION_H_
6#define V8_WASM_CONSTANT_EXPRESSION_H_
7
8#if !V8_ENABLE_WEBASSEMBLY
9#error This header should only be included if WebAssembly is enabled.
10#endif // !V8_ENABLE_WEBASSEMBLY
11
12#include <stdint.h>
13
14#include <variant>
15
16#include "src/base/bit-field.h"
17#include "src/wasm/value-type.h"
18#include "src/wasm/wasm-value.h"
19
20namespace v8 {
21namespace internal {
22
23enum class MessageTemplate;
25class Zone;
26
27namespace wasm {
28
29class WireBytesRef;
30
31// A representation of a constant expression. The most common expression types
32// are hard-coded, while the rest are represented as a {WireBytesRef}.
34 public:
35 enum class Kind {
36 kEmpty,
42 };
43
44 constexpr ConstantExpression() = default;
45
46 static constexpr ConstantExpression I32Const(int32_t value) {
49 }
50 static constexpr ConstantExpression RefFunc(uint32_t index) {
53 }
54 static constexpr ConstantExpression RefNull(HeapType type) {
55 return ConstantExpression(ValueField::encode(type.raw_bit_field()) |
57 }
58 static constexpr ConstantExpression WireBytes(uint32_t offset,
59 uint32_t length) {
61 LengthField::encode(length) |
63 }
64
65 constexpr Kind kind() const { return KindField::decode(bit_field_); }
66
67 constexpr bool is_set() const { return kind() != Kind::kEmpty; }
68
69 constexpr uint32_t index() const {
72 }
73
78
79 constexpr int32_t i32_value() const {
82 }
83
85
86 private:
87 static constexpr int kValueBits = 32;
88 static constexpr int kLengthBits = 30;
89 static constexpr int kOffsetBits = 30;
90 static constexpr int kKindBits = 3;
91
92 // There are two possible combinations of fields: offset + length + kind if
93 // kind = kWireBytesRef, or value + kind for anything else.
98
99 // Make sure we reserve enough bits for a {WireBytesRef}'s length and offset.
100 static_assert(kV8MaxWasmModuleSize <= LengthField::kMax + 1);
101 static_assert(kV8MaxWasmModuleSize <= OffsetField::kMax + 1);
102 // Make sure kind fits in kKindBits.
103 static_assert(static_cast<uint64_t>(Kind::kLastKind) <= KindField::kMax + 1);
104
105 explicit constexpr ConstantExpression(uint64_t bit_field)
107
108 uint64_t bit_field_ = 0;
109};
110
111// Verify that the default constructor initializes the {kind()} to {kEmpty}.
113
114// We want to keep {ConstantExpression} small to reduce memory usage during
115// compilation/instantiation.
116static_assert(sizeof(ConstantExpression) <= 8);
117
118using ValueOrError = std::variant<WasmValue, MessageTemplate>;
119
121 return std::holds_alternative<MessageTemplate>(result);
122}
124 return std::get<MessageTemplate>(result);
125}
127 return std::get<WasmValue>(result);
128}
129
130// Evaluates a constant expression.
131// Returns a {WasmValue} if the evaluation succeeds, or an error as a
132// {MessageTemplate} if it fails.
133// Resets {zone} so make sure it contains no useful data.
135 Zone* zone, ConstantExpression expr, ValueType expected,
136 const WasmModule* module, Isolate* isolate,
137 DirectHandle<WasmTrustedInstanceData> trusted_instance_data,
138 DirectHandle<WasmTrustedInstanceData> shared_trusted_instance_data);
139
140} // namespace wasm
141} // namespace internal
142} // namespace v8
143
144#endif // V8_WASM_CONSTANT_EXPRESSION_H_
friend Zone
Definition asm-types.cc:195
static constexpr U kMax
Definition bit-field.h:44
static constexpr T decode(U value)
Definition bit-field.h:66
static constexpr U encode(T value)
Definition bit-field.h:55
constexpr ConstantExpression(uint64_t bit_field)
static constexpr ConstantExpression WireBytes(uint32_t offset, uint32_t length)
static constexpr ConstantExpression I32Const(int32_t value)
static constexpr ConstantExpression RefFunc(uint32_t index)
V8_EXPORT_PRIVATE WireBytesRef wire_bytes_ref() const
static constexpr ConstantExpression RefNull(HeapType type)
static constexpr HeapType FromBits(uint32_t bits)
Definition value-type.h:721
int32_t offset
ZoneVector< RpoNumber > & result
V8_INLINE bool is_error(ValueOrError result)
std::variant< WasmValue, MessageTemplate > ValueOrError
V8_INLINE MessageTemplate to_error(ValueOrError result)
V8_INLINE WasmValue to_value(ValueOrError result)
constexpr size_t kV8MaxWasmModuleSize
Definition wasm-limits.h:50
ValueOrError EvaluateConstantExpression(Zone *zone, ConstantExpression expr, ValueType expected, const WasmModule *module, Isolate *isolate, DirectHandle< WasmTrustedInstanceData > trusted_instance_data, DirectHandle< WasmTrustedInstanceData > shared_trusted_instance_data)
kWasmInternalFunctionIndirectPointerTag WasmTrustedInstanceData
Definition c-api.cc:87
#define DCHECK_EQ(v1, v2)
Definition logging.h:485
#define V8_EXPORT_PRIVATE
Definition macros.h:460
#define V8_INLINE
Definition v8config.h:500