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#include "src/wasm/local-decl-encoder.h"
6
7#include "src/signature.h"
8#include "src/wasm/leb-helper.h"
9
10namespace v8 {
11namespace internal {
12namespace wasm {
13
14void LocalDeclEncoder::Prepend(Zone* zone, const byte** start,
15 const byte** end) const {
16 size_t size = (*end - *start);
17 byte* buffer = reinterpret_cast<byte*>(zone->New(Size() + size));
18 size_t pos = Emit(buffer);
19 if (size > 0) {
20 memcpy(buffer + pos, *start, size);
21 }
22 pos += size;
23 *start = buffer;
24 *end = buffer + pos;
25}
26
27size_t LocalDeclEncoder::Emit(byte* buffer) const {
28 byte* pos = buffer;
29 LEBHelper::write_u32v(&pos, static_cast<uint32_t>(local_decls.size()));
30 for (auto& local_decl : local_decls) {
31 LEBHelper::write_u32v(&pos, local_decl.first);
32 *pos = ValueTypes::ValueTypeCodeFor(local_decl.second);
33 ++pos;
34 }
35 DCHECK_EQ(Size(), pos - buffer);
36 return static_cast<size_t>(pos - buffer);
37}
38
39uint32_t LocalDeclEncoder::AddLocals(uint32_t count, ValueType type) {
40 uint32_t result =
41 static_cast<uint32_t>(total + (sig ? sig->parameter_count() : 0));
42 total += count;
43 if (local_decls.size() > 0 && local_decls.back().second == type) {
44 count += local_decls.back().first;
45 local_decls.pop_back();
46 }
47 local_decls.push_back(std::pair<uint32_t, ValueType>(count, type));
48 return result;
49}
50
51size_t LocalDeclEncoder::Size() const {
52 size_t size = LEBHelper::sizeof_u32v(local_decls.size());
53 for (auto p : local_decls) size += 1 + LEBHelper::sizeof_u32v(p.first);
54 return size;
55}
56
57} // namespace wasm
58} // namespace internal
59} // namespace v8
60