v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
function-body-decoder.h
Go to the documentation of this file.
1// Copyright 2015 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_FUNCTION_BODY_DECODER_H_
6#define V8_WASM_FUNCTION_BODY_DECODER_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
13#include "src/base/iterator.h"
14#include "src/common/globals.h"
15#include "src/wasm/decoder.h"
18
19namespace v8::internal {
20class AccountingAllocator;
21class BitVector;
22class Zone;
23} // namespace v8::internal
24
25namespace v8::internal::wasm {
26
27class WasmDetectedFeatures;
28class WasmEnabledFeatures;
29struct WasmModule; // forward declaration of module interface.
30
31// A wrapper around the signature and bytes of a function.
33 const FunctionSig* sig; // function signature
34 uint32_t offset; // offset in the module bytes, for error reporting
35 const uint8_t* start; // start of the function body
36 const uint8_t* end; // end of the function body
37 bool is_shared; // whether this is a shared function
38
39 FunctionBody(const FunctionSig* sig, uint32_t offset, const uint8_t* start,
40 const uint8_t* end, bool is_shared)
41 : sig(sig),
43 start(start),
44 end(end),
46};
47
49
51 Zone* zone, WasmEnabledFeatures enabled, const WasmModule* module,
52 WasmDetectedFeatures* detected, const FunctionBody& body);
53
55 // The size of the encoded declarations.
56 uint32_t encoded_size = 0; // size of encoded declarations
57
58 uint32_t num_locals = 0;
60};
61
62// Decode locals; validation is not performed.
64 BodyLocalDecls* decls,
65 const uint8_t* start,
66 const uint8_t* end, Zone* zone);
67
68// Decode locals, including validation.
70 WasmEnabledFeatures enabled, BodyLocalDecls* decls,
71 const WasmModule* module, bool is_shared, const uint8_t* start,
72 const uint8_t* end, Zone* zone);
73
75 Zone* zone, uint32_t num_locals, const uint8_t* start, const uint8_t* end,
76 bool* loop_is_innermost);
77
78// Computes the length of the opcode at the given address.
79V8_EXPORT_PRIVATE unsigned OpcodeLength(const uint8_t* pc, const uint8_t* end);
80
81// Checks if the underlying hardware supports the Wasm SIMD proposal.
83
84// A simple forward iterator for bytecodes.
86 // Base class for both iterators defined below.
88 public:
92 return *this;
93 }
94 bool operator==(const iterator_base& that) const {
95 return this->ptr_ == that.ptr_;
96 }
97
98 protected:
99 const uint8_t* ptr_;
100 const uint8_t* end_;
101 iterator_base(const uint8_t* ptr, const uint8_t* end)
102 : ptr_(ptr), end_(end) {}
103 };
104
105 public:
106 // If one wants to iterate over the bytecode without looking at {pc_offset()}.
108 : public iterator_base,
109 public base::iterator<std::input_iterator_tag, WasmOpcode> {
110 public:
113 return static_cast<WasmOpcode>(*ptr_);
114 }
115
116 private:
117 friend class BytecodeIterator;
118 opcode_iterator(const uint8_t* ptr, const uint8_t* end)
119 : iterator_base(ptr, end) {}
120 };
121 // If one wants to iterate over the instruction offsets without looking at
122 // opcodes.
124 : public iterator_base,
125 public base::iterator<std::input_iterator_tag, uint32_t> {
126 public:
127 uint32_t operator*() {
129 return static_cast<uint32_t>(ptr_ - start_);
130 }
131
132 private:
133 const uint8_t* start_;
134 friend class BytecodeIterator;
135 offset_iterator(const uint8_t* start, const uint8_t* ptr,
136 const uint8_t* end)
137 : iterator_base(ptr, end), start_(start) {}
138 };
139
140 // Create a new {BytecodeIterator}, starting after the locals declarations.
141 BytecodeIterator(const uint8_t* start, const uint8_t* end);
142
143 // Create a new {BytecodeIterator}, starting with locals declarations.
144 BytecodeIterator(const uint8_t* start, const uint8_t* end,
145 BodyLocalDecls* decls, Zone* zone);
146
151
157
159 return static_cast<WasmOpcode>(
160 read_u8<Decoder::NoValidationTag>(pc_, "expected bytecode"));
161 }
162
163 void next() {
164 if (pc_ < end_) {
166 if (pc_ >= end_) pc_ = end_;
167 }
168 }
169
170 bool has_next() const { return pc_ < end_; }
171
173 auto [opcode, length] = read_prefixed_opcode<Decoder::NoValidationTag>(pc_);
174 return opcode;
175 }
176
177 const uint8_t* pc() const { return pc_; }
178};
179
180} // namespace v8::internal::wasm
181
182#endif // V8_WASM_FUNCTION_BODY_DECODER_H_
friend Zone
Definition asm-types.cc:195
iterator_base(const uint8_t *ptr, const uint8_t *end)
offset_iterator(const uint8_t *start, const uint8_t *ptr, const uint8_t *end)
opcode_iterator(const uint8_t *ptr, const uint8_t *end)
base::iterator_range< opcode_iterator > opcodes() const
base::iterator_range< offset_iterator > offsets() const
Node ** ptr_
uint8_t *const start_
Definition assembler.cc:131
const v8::base::TimeTicks end_
Definition sweeper.cc:54
int start
int end
VoidResult DecodeResult
Definition decoder.h:39
bool DecodeLocalDecls(WasmEnabledFeatures enabled, BodyLocalDecls *decls, const WasmModule *module, bool is_shared, const uint8_t *start, const uint8_t *end, Zone *zone)
bool ValidateAndDecodeLocalDeclsForTesting(WasmEnabledFeatures enabled, BodyLocalDecls *decls, const WasmModule *module, bool is_shared, const uint8_t *start, const uint8_t *end, Zone *zone)
DecodeResult ValidateFunctionBody(Zone *zone, WasmEnabledFeatures enabled, const WasmModule *module, WasmDetectedFeatures *detected, const FunctionBody &body)
unsigned OpcodeLength(const uint8_t *pc, const uint8_t *end)
BitVector * AnalyzeLoopAssignmentForTesting(Zone *zone, uint32_t num_locals, const uint8_t *start, const uint8_t *end, bool *loop_is_innermost)
wasm::WasmModule WasmModule
JSArrayBuffer::IsDetachableBit is_shared
#define NON_EXPORTED_BASE(code)
#define DCHECK_LT(v1, v2)
Definition logging.h:489
#define V8_EXPORT_PRIVATE
Definition macros.h:460
FunctionBody(const FunctionSig *sig, uint32_t offset, const uint8_t *start, const uint8_t *end, bool is_shared)