chromium/v8/src/wasm/function-body-decoder-impl.h

// Copyright 2017 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#if !V8_ENABLE_WEBASSEMBLY
#error This header should only be included if WebAssembly is enabled.
#endif  // !V8_ENABLE_WEBASSEMBLY

#ifndef V8_WASM_FUNCTION_BODY_DECODER_IMPL_H_
#define V8_WASM_FUNCTION_BODY_DECODER_IMPL_H_

// Do only include this header for implementing new Interface of the
// WasmFullDecoder.

#include <inttypes.h>

#include <optional>

#include "src/base/bounds.h"
#include "src/base/small-vector.h"
#include "src/base/strings.h"
#include "src/base/vector.h"
#include "src/strings/unicode.h"
#include "src/utils/bit-vector.h"
#include "src/wasm/decoder.h"
#include "src/wasm/function-body-decoder.h"
#include "src/wasm/value-type.h"
#include "src/wasm/wasm-features.h"
#include "src/wasm/wasm-limits.h"
#include "src/wasm/wasm-module.h"
#include "src/wasm/wasm-opcodes.h"
#include "src/wasm/wasm-subtyping.h"

namespace v8::internal::wasm {

struct WasmGlobal;
struct WasmTag;

#define TRACE

#define TRACE_INST_FORMAT

// Return the evaluation of {condition} if {ValidationTag::validate} is true,
// DCHECK that it is true and always return true otherwise.
// Note that this needs to be a macro, because the "likely" annotation does not
// survive inlining.
#ifdef DEBUG
#define VALIDATE

V8_INLINE bool ValidateAssumeTrue(bool condition, const char* message) {}
#else
#define VALIDATE
#endif

#define CHECK_PROTOTYPE_OPCODE

static constexpr LoadType GetLoadType(WasmOpcode opcode) {}

static constexpr StoreType GetStoreType(WasmOpcode opcode) {}

#define ATOMIC_OP_LIST(V)

#define ATOMIC_STORE_OP_LIST(V)

// Decoder error with explicit PC and optional format arguments.
// Depending on the validation tag and the number of arguments, this forwards to
// a V8_NOINLINE and V8_PRESERVE_MOST method of the decoder.
template <typename ValidationTag, typename... Args>
V8_INLINE void DecodeError(Decoder* decoder, const uint8_t* pc, const char* str,
                           Args&&... args) {}

// Decoder error without explicit PC and with optional format arguments.
// Depending on the validation tag and the number of arguments, this forwards to
// a V8_NOINLINE and V8_PRESERVE_MOST method of the decoder.
template <typename ValidationTag, typename... Args>
V8_INLINE void DecodeError(Decoder* decoder, const char* str, Args&&... args) {}

namespace value_type_reader {

template <typename ValidationTag>
std::pair<HeapType, uint32_t> read_heap_type(Decoder* decoder,
                                             const uint8_t* pc,
                                             WasmEnabledFeatures enabled) {}

// Read a value type starting at address {pc} using {decoder}.
// No bytes are consumed.
// Returns the read value type and the number of bytes read (a.k.a. length).
template <typename ValidationTag>
std::pair<ValueType, uint32_t> read_value_type(Decoder* decoder,
                                               const uint8_t* pc,
                                               WasmEnabledFeatures enabled) {}

template <typename ValidationTag>
bool ValidateHeapType(Decoder* decoder, const uint8_t* pc,
                      const WasmModule* module, HeapType type) {}

template <typename ValidationTag>
bool ValidateValueType(Decoder* decoder, const uint8_t* pc,
                       const WasmModule* module, ValueType type) {}

}  // namespace value_type_reader

enum DecodingMode {};

// Helpers for decoding different kinds of immediates which follow bytecodes.
struct ImmI32Immediate {};

struct ImmI64Immediate {};

struct ImmF32Immediate {};

struct ImmF64Immediate {};

struct BrOnCastFlags {};

struct BrOnCastImmediate {};

// Parent class for all Immediates which read a u32v index value in their
// constructor.
struct IndexImmediate {};

struct MemoryIndexImmediate : public IndexImmediate {};

struct TableIndexImmediate : public IndexImmediate {};

struct TagIndexImmediate : public IndexImmediate {};

struct GlobalIndexImmediate : public IndexImmediate {};

struct SigIndexImmediate : public IndexImmediate {};

struct StructIndexImmediate : public IndexImmediate {};

struct ArrayIndexImmediate : public IndexImmediate {};

struct CallFunctionImmediate : public IndexImmediate {};

struct SelectTypeImmediate {};

static constexpr uint32_t kInlineSignatureSentinel =;

struct BlockTypeImmediate {};

struct BranchDepthImmediate {};

struct FieldImmediate {};

struct CallIndirectImmediate {};

struct BranchTableImmediate {};

TryTableImmediate;

// A helper to iterate over a branch table.
template <typename ValidationTag>
class BranchTableIterator {};

struct CatchCase {};

// A helper to iterate over a try table.
template <typename ValidationTag>
class TryTableIterator {};

struct MemoryAccessImmediate {};

// Immediate for SIMD lane operations.
struct SimdLaneImmediate {};

// Immediate for SIMD S8x16 shuffle operations.
struct Simd128Immediate {};

struct MemoryInitImmediate {};

struct MemoryCopyImmediate {};

struct TableInitImmediate {};

struct TableCopyImmediate {};

struct HeapTypeImmediate {};

struct StringConstImmediate {};

template <bool validate>
struct PcForErrors {};

template <>
struct PcForErrors<true> {};

// An entry on the value stack.
template <typename ValidationTag>
struct ValueBase : public PcForErrors<ValidationTag::validate> {};

template <typename Value>
struct Merge {};

enum ControlKind : uint8_t {};

enum Reachability : uint8_t {};

// An entry on the control stack (i.e. if, block, loop, or try).
template <typename Value, typename ValidationTag>
struct ControlBase : public PcForErrors<ValidationTag::validate> {};

// This is the list of callback functions that an interface for the
// WasmFullDecoder should implement.
// F(Name, args...)
#define INTERFACE_FUNCTIONS(F)

#define INTERFACE_META_FUNCTIONS(F)

#define INTERFACE_CONSTANT_FUNCTIONS(F)

#define INTERFACE_NON_CONSTANT_FUNCTIONS(F)

// This is a global constant invalid instruction trace, to be pointed at by
// the current instruction trace pointer in the default case
const std::pair<uint32_t, uint32_t> invalid_instruction_trace = {0, 0};

// A fast vector implementation, without implicit bounds checks (see
// https://crbug.com/1358853).
template <typename T>
class FastZoneVector {};

// Generic Wasm bytecode decoder with utilities for decoding immediates,
// lengths, etc.
template <typename ValidationTag, DecodingMode decoding_mode = kFunctionBody>
class WasmDecoder : public Decoder {};

// Only call this in contexts where {current_code_reachable_and_ok_} is known to
// hold.
#define CALL_INTERFACE(name, ...)
#define CALL_INTERFACE_IF_OK_AND_REACHABLE
#define CALL_INTERFACE_IF_OK_AND_PARENT_REACHABLE

// An empty class used in place of a {base::SmallVector} for cases where the
// content is not needed afterwards.
// This is used for implementations which set {kUsesPoppedArgs} to {false}.
class NoVector {};

template <typename ValidationTag, typename Interface,
          DecodingMode decoding_mode = kFunctionBody>
class WasmFullDecoder : public WasmDecoder<ValidationTag, decoding_mode> {};

class EmptyInterface {};

#undef CALL_INTERFACE_IF_OK_AND_REACHABLE
#undef CALL_INTERFACE_IF_OK_AND_PARENT_REACHABLE
#undef TRACE
#undef TRACE_INST_FORMAT
#undef VALIDATE
#undef CHECK_PROTOTYPE_OPCODE

}  // namespace v8::internal::wasm

#endif  // V8_WASM_FUNCTION_BODY_DECODER_IMPL_H_