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
10 namespace v8 {
11 namespace internal {
12 namespace wasm {
13
Prepend(Zone * zone,const byte ** start,const byte ** end) const14 void 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 memcpy(buffer + pos, *start, size);
20 pos += size;
21 *start = buffer;
22 *end = buffer + pos;
23 }
24
Emit(byte * buffer) const25 size_t LocalDeclEncoder::Emit(byte* buffer) const {
26 byte* pos = buffer;
27 LEBHelper::write_u32v(&pos, static_cast<uint32_t>(local_decls.size()));
28 for (auto& local_decl : local_decls) {
29 LEBHelper::write_u32v(&pos, local_decl.first);
30 *pos = ValueTypes::ValueTypeCodeFor(local_decl.second);
31 ++pos;
32 }
33 DCHECK_EQ(Size(), pos - buffer);
34 return static_cast<size_t>(pos - buffer);
35 }
36
AddLocals(uint32_t count,ValueType type)37 uint32_t LocalDeclEncoder::AddLocals(uint32_t count, ValueType type) {
38 uint32_t result =
39 static_cast<uint32_t>(total + (sig ? sig->parameter_count() : 0));
40 total += count;
41 if (local_decls.size() > 0 && local_decls.back().second == type) {
42 count += local_decls.back().first;
43 local_decls.pop_back();
44 }
45 local_decls.push_back(std::pair<uint32_t, ValueType>(count, type));
46 return result;
47 }
48
Size() const49 size_t LocalDeclEncoder::Size() const {
50 size_t size = LEBHelper::sizeof_u32v(local_decls.size());
51 for (auto p : local_decls) size += 1 + LEBHelper::sizeof_u32v(p.first);
52 return size;
53 }
54
55 } // namespace wasm
56 } // namespace internal
57 } // namespace v8
58