v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
local-decl-encoder.cc
Go to the documentation of this file.
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
6
9
10namespace v8 {
11namespace internal {
12namespace wasm {
13
14// This struct is just a type tag for Zone::NewArray<T>(size_t) call.
16
17void LocalDeclEncoder::Prepend(Zone* zone, const uint8_t** start,
18 const uint8_t** end) const {
19 size_t size = (*end - *start);
20 uint8_t* buffer =
21 zone->AllocateArray<uint8_t, LocalDeclEncoderBuffer>(Size() + size);
22 size_t pos = Emit(buffer);
23 if (size > 0) {
24 memcpy(buffer + pos, *start, size);
25 }
26 pos += size;
27 *start = buffer;
28 *end = buffer + pos;
29}
30
31size_t LocalDeclEncoder::Emit(uint8_t* buffer) const {
32 uint8_t* pos = buffer;
33 LEBHelper::write_u32v(&pos, static_cast<uint32_t>(local_decls.size()));
34 for (auto& local_decl : local_decls) {
35 uint32_t locals_count = local_decl.first;
36 ValueType locals_type = local_decl.second;
37 LEBHelper::write_u32v(&pos, locals_count);
38 *pos = locals_type.value_type_code();
39 ++pos;
40 if (locals_type.encoding_needs_shared()) {
42 ++pos;
43 }
44 if (locals_type.encoding_needs_heap_type()) {
45 if (locals_type.encoding_needs_exact()) {
46 *pos = kExactCode;
47 ++pos;
48 }
49 LEBHelper::write_i32v(&pos, locals_type.heap_type().code());
50 }
51 }
52 DCHECK_EQ(Size(), pos - buffer);
53 return static_cast<size_t>(pos - buffer);
54}
55
57 uint32_t result =
58 static_cast<uint32_t>(total + (sig ? sig->parameter_count() : 0));
59 total += count;
60 if (!local_decls.empty() && local_decls.back().second == type) {
61 count += local_decls.back().first;
62 local_decls.pop_back();
63 }
64 local_decls.push_back(std::pair<uint32_t, ValueType>(count, type));
65 return result;
66}
67
68// Size = (size of locals count) +
69// (for each local pair <reps, type>, (size of reps) + (size of type))
70size_t LocalDeclEncoder::Size() const {
71 size_t size = LEBHelper::sizeof_u32v(local_decls.size());
72 for (auto p : local_decls) {
73 size += LEBHelper::sizeof_u32v(p.first) + // number of locals
74 1 + // Opcode
75 (p.second.encoding_needs_shared() ? 1 : 0) +
76 (p.second.encoding_needs_exact() ? 1 : 0) +
77 (p.second.encoding_needs_heap_type()
78 ? LEBHelper::sizeof_i32v(p.second.heap_type().code())
79 : 0);
80 }
81 return size;
82}
83
84} // namespace wasm
85} // namespace internal
86} // namespace v8
SourcePosition pos
T * AllocateArray(size_t length)
Definition zone.h:127
static void write_i32v(uint8_t **dest, int32_t val)
Definition leb-helper.h:37
static size_t sizeof_u32v(size_t val)
Definition leb-helper.h:84
static size_t sizeof_i32v(int32_t val)
Definition leb-helper.h:94
static void write_u32v(uint8_t **dest, uint32_t val)
Definition leb-helper.h:27
void Prepend(Zone *zone, const uint8_t **start, const uint8_t **end) const
size_t Emit(uint8_t *buffer) const
ZoneVector< std::pair< uint32_t, ValueType > > local_decls
uint32_t AddLocals(uint32_t count, ValueType type)
constexpr bool encoding_needs_shared() const
Definition value-type.h:544
constexpr bool encoding_needs_exact() const
Definition value-type.h:548
constexpr bool encoding_needs_heap_type() const
Definition value-type.h:538
ValueTypeCode value_type_code() const
Definition value-type.h:527
constexpr HeapType heap_type() const
int start
uint32_t count
int end
ZoneVector< RpoNumber > & result
constexpr uint8_t kSharedFlagCode
Definition c-api.cc:87
#define DCHECK_EQ(v1, v2)
Definition logging.h:485