11cb0ef41Sopenharmony_ci// Copyright 2021 the V8 project authors. All rights reserved.
21cb0ef41Sopenharmony_ci// Use of this source code is governed by a BSD-style license that can be
31cb0ef41Sopenharmony_ci// found in the LICENSE file.
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ci#if !V8_ENABLE_WEBASSEMBLY
61cb0ef41Sopenharmony_ci#error This header should only be included if WebAssembly is enabled.
71cb0ef41Sopenharmony_ci#endif  // !V8_ENABLE_WEBASSEMBLY
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ci#ifndef V8_WASM_INIT_EXPR_INTERFACE_H_
101cb0ef41Sopenharmony_ci#define V8_WASM_INIT_EXPR_INTERFACE_H_
111cb0ef41Sopenharmony_ci
121cb0ef41Sopenharmony_ci#include "src/wasm/decoder.h"
131cb0ef41Sopenharmony_ci#include "src/wasm/function-body-decoder-impl.h"
141cb0ef41Sopenharmony_ci#include "src/wasm/wasm-value.h"
151cb0ef41Sopenharmony_ci
161cb0ef41Sopenharmony_cinamespace v8 {
171cb0ef41Sopenharmony_cinamespace internal {
181cb0ef41Sopenharmony_ci
191cb0ef41Sopenharmony_ciclass WasmInstanceObject;
201cb0ef41Sopenharmony_ciclass JSArrayBuffer;
211cb0ef41Sopenharmony_ci
221cb0ef41Sopenharmony_cinamespace wasm {
231cb0ef41Sopenharmony_ci
241cb0ef41Sopenharmony_ci// An interface for WasmFullDecoder used to decode initializer expressions. This
251cb0ef41Sopenharmony_ci// interface has two modes: only validation (when {isolate_ == nullptr}), which
261cb0ef41Sopenharmony_ci// is used in module-decoder, and code-generation (when {isolate_ != nullptr}),
271cb0ef41Sopenharmony_ci// which is used in module-instantiate. We merge two distinct functionalities
281cb0ef41Sopenharmony_ci// in one class to reduce the number of WasmFullDecoder instantiations, and thus
291cb0ef41Sopenharmony_ci// V8 binary code size.
301cb0ef41Sopenharmony_ciclass InitExprInterface {
311cb0ef41Sopenharmony_ci public:
321cb0ef41Sopenharmony_ci  static constexpr Decoder::ValidateFlag validate = Decoder::kFullValidation;
331cb0ef41Sopenharmony_ci  static constexpr DecodingMode decoding_mode = kInitExpression;
341cb0ef41Sopenharmony_ci
351cb0ef41Sopenharmony_ci  struct Value : public ValueBase<validate> {
361cb0ef41Sopenharmony_ci    WasmValue runtime_value;
371cb0ef41Sopenharmony_ci
381cb0ef41Sopenharmony_ci    template <typename... Args>
391cb0ef41Sopenharmony_ci    explicit Value(Args&&... args) V8_NOEXCEPT
401cb0ef41Sopenharmony_ci        : ValueBase(std::forward<Args>(args)...) {}
411cb0ef41Sopenharmony_ci  };
421cb0ef41Sopenharmony_ci
431cb0ef41Sopenharmony_ci  using Control = ControlBase<Value, validate>;
441cb0ef41Sopenharmony_ci  using FullDecoder =
451cb0ef41Sopenharmony_ci      WasmFullDecoder<validate, InitExprInterface, decoding_mode>;
461cb0ef41Sopenharmony_ci
471cb0ef41Sopenharmony_ci  InitExprInterface(const WasmModule* module, Isolate* isolate,
481cb0ef41Sopenharmony_ci                    Handle<WasmInstanceObject> instance)
491cb0ef41Sopenharmony_ci      : module_(module),
501cb0ef41Sopenharmony_ci        outer_module_(nullptr),
511cb0ef41Sopenharmony_ci        isolate_(isolate),
521cb0ef41Sopenharmony_ci        instance_(instance) {
531cb0ef41Sopenharmony_ci    DCHECK_NOT_NULL(isolate);
541cb0ef41Sopenharmony_ci  }
551cb0ef41Sopenharmony_ci
561cb0ef41Sopenharmony_ci  explicit InitExprInterface(WasmModule* outer_module)
571cb0ef41Sopenharmony_ci      : module_(nullptr), outer_module_(outer_module), isolate_(nullptr) {}
581cb0ef41Sopenharmony_ci
591cb0ef41Sopenharmony_ci#define EMPTY_INTERFACE_FUNCTION(name, ...) \
601cb0ef41Sopenharmony_ci  V8_INLINE void name(FullDecoder* decoder, ##__VA_ARGS__) {}
611cb0ef41Sopenharmony_ci  INTERFACE_META_FUNCTIONS(EMPTY_INTERFACE_FUNCTION)
621cb0ef41Sopenharmony_ci#undef EMPTY_INTERFACE_FUNCTION
631cb0ef41Sopenharmony_ci#define UNREACHABLE_INTERFACE_FUNCTION(name, ...) \
641cb0ef41Sopenharmony_ci  V8_INLINE void name(FullDecoder* decoder, ##__VA_ARGS__) { UNREACHABLE(); }
651cb0ef41Sopenharmony_ci  INTERFACE_NON_CONSTANT_FUNCTIONS(UNREACHABLE_INTERFACE_FUNCTION)
661cb0ef41Sopenharmony_ci#undef UNREACHABLE_INTERFACE_FUNCTION
671cb0ef41Sopenharmony_ci
681cb0ef41Sopenharmony_ci#define DECLARE_INTERFACE_FUNCTION(name, ...) \
691cb0ef41Sopenharmony_ci  void name(FullDecoder* decoder, ##__VA_ARGS__);
701cb0ef41Sopenharmony_ci  INTERFACE_CONSTANT_FUNCTIONS(DECLARE_INTERFACE_FUNCTION)
711cb0ef41Sopenharmony_ci#undef DECLARE_INTERFACE_FUNCTION
721cb0ef41Sopenharmony_ci
731cb0ef41Sopenharmony_ci  WasmValue result() {
741cb0ef41Sopenharmony_ci    DCHECK_NOT_NULL(isolate_);
751cb0ef41Sopenharmony_ci    return result_;
761cb0ef41Sopenharmony_ci  }
771cb0ef41Sopenharmony_ci  bool end_found() { return end_found_; }
781cb0ef41Sopenharmony_ci  bool runtime_error() { return error_ != nullptr; }
791cb0ef41Sopenharmony_ci  const char* runtime_error_msg() { return error_; }
801cb0ef41Sopenharmony_ci
811cb0ef41Sopenharmony_ci private:
821cb0ef41Sopenharmony_ci  bool generate_result() { return isolate_ != nullptr && !runtime_error(); }
831cb0ef41Sopenharmony_ci  bool end_found_ = false;
841cb0ef41Sopenharmony_ci  const char* error_ = nullptr;
851cb0ef41Sopenharmony_ci  WasmValue result_;
861cb0ef41Sopenharmony_ci  const WasmModule* module_;
871cb0ef41Sopenharmony_ci  WasmModule* outer_module_;
881cb0ef41Sopenharmony_ci  Isolate* isolate_;
891cb0ef41Sopenharmony_ci  Handle<WasmInstanceObject> instance_;
901cb0ef41Sopenharmony_ci};
911cb0ef41Sopenharmony_ci
921cb0ef41Sopenharmony_ci}  // namespace wasm
931cb0ef41Sopenharmony_ci}  // namespace internal
941cb0ef41Sopenharmony_ci}  // namespace v8
951cb0ef41Sopenharmony_ci
961cb0ef41Sopenharmony_ci#endif  // V8_WASM_INIT_EXPR_INTERFACE_H_
97