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#if !V8_ENABLE_WEBASSEMBLY
6#error This header should only be included if WebAssembly is enabled.
7#endif  // !V8_ENABLE_WEBASSEMBLY
8
9#ifndef V8_WASM_LOCAL_DECL_ENCODER_H_
10#define V8_WASM_LOCAL_DECL_ENCODER_H_
11
12#include "src/common/globals.h"
13#include "src/wasm/wasm-opcodes.h"
14#include "src/zone/zone-containers.h"
15#include "src/zone/zone.h"
16
17namespace v8 {
18namespace internal {
19namespace wasm {
20
21// A helper for encoding local declarations prepended to the body of a function.
22class V8_EXPORT_PRIVATE LocalDeclEncoder {
23 public:
24  explicit LocalDeclEncoder(Zone* zone, const FunctionSig* s = nullptr)
25      : sig(s), local_decls(zone), total(0) {}
26
27  // Prepend local declarations by creating a new buffer and copying data
28  // over. The new buffer must be delete[]'d by the caller.
29  void Prepend(Zone* zone, const byte** start, const byte** end) const;
30
31  size_t Emit(byte* buffer) const;
32
33  // Add locals declarations to this helper. Return the index of the newly added
34  // local(s), with an optional adjustment for the parameters.
35  uint32_t AddLocals(uint32_t count, ValueType type);
36
37  size_t Size() const;
38
39  bool has_sig() const { return sig != nullptr; }
40  const FunctionSig* get_sig() const { return sig; }
41  void set_sig(const FunctionSig* s) { sig = s; }
42
43 private:
44  const FunctionSig* sig;
45  ZoneVector<std::pair<uint32_t, ValueType>> local_decls;
46  size_t total;
47};
48
49}  // namespace wasm
50}  // namespace internal
51}  // namespace v8
52
53#endif  // V8_WASM_LOCAL_DECL_ENCODER_H_
54