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