Line data Source code
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 :
14 1 : void LocalDeclEncoder::Prepend(Zone* zone, const byte** start,
15 : const byte** end) const {
16 1 : size_t size = (*end - *start);
17 1 : byte* buffer = reinterpret_cast<byte*>(zone->New(Size() + size));
18 1 : size_t pos = Emit(buffer);
19 1 : if (size > 0) {
20 0 : memcpy(buffer + pos, *start, size);
21 : }
22 1 : pos += size;
23 1 : *start = buffer;
24 1 : *end = buffer + pos;
25 1 : }
26 :
27 1134937 : size_t LocalDeclEncoder::Emit(byte* buffer) const {
28 : byte* pos = buffer;
29 1134937 : LEBHelper::write_u32v(&pos, static_cast<uint32_t>(local_decls.size()));
30 1181954 : for (auto& local_decl : local_decls) {
31 47017 : LEBHelper::write_u32v(&pos, local_decl.first);
32 47017 : *pos = ValueTypes::ValueTypeCodeFor(local_decl.second);
33 47017 : ++pos;
34 : }
35 : DCHECK_EQ(Size(), pos - buffer);
36 1134937 : return static_cast<size_t>(pos - buffer);
37 : }
38 :
39 739122 : uint32_t LocalDeclEncoder::AddLocals(uint32_t count, ValueType type) {
40 : uint32_t result =
41 739122 : static_cast<uint32_t>(total + (sig ? sig->parameter_count() : 0));
42 739122 : total += count;
43 739122 : if (local_decls.size() > 0 && local_decls.back().second == type) {
44 692197 : count += local_decls.back().first;
45 : local_decls.pop_back();
46 : }
47 1478244 : local_decls.push_back(std::pair<uint32_t, ValueType>(count, type));
48 739122 : return result;
49 : }
50 :
51 1158680 : size_t LocalDeclEncoder::Size() const {
52 : size_t size = LEBHelper::sizeof_u32v(local_decls.size());
53 1288235 : for (auto p : local_decls) size += 1 + LEBHelper::sizeof_u32v(p.first);
54 1158680 : return size;
55 : }
56 :
57 : } // namespace wasm
58 : } // namespace internal
59 : } // namespace v8
|