function-compiler.h 3.45 KB
Newer Older
1 2 3 4 5 6 7
// Copyright 2018 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.

#ifndef V8_WASM_FUNCTION_COMPILER_H_
#define V8_WASM_FUNCTION_COMPILER_H_

8
#include "src/code-desc.h"
9
#include "src/trap-handler/trap-handler.h"
10
#include "src/wasm/compilation-environment.h"
11
#include "src/wasm/function-body-decoder.h"
12 13
#include "src/wasm/wasm-limits.h"
#include "src/wasm/wasm-module.h"
14
#include "src/wasm/wasm-tier.h"
15 16 17 18

namespace v8 {
namespace internal {

19
class AssemblerBuffer;
20 21
class Counters;

22
namespace compiler {
23
class Pipeline;
24 25 26 27 28 29 30 31
class TurbofanWasmCompilationUnit;
}  // namespace compiler

namespace wasm {

class LiftoffCompilationUnit;
class NativeModule;
class WasmCode;
32
class WasmCompilationUnit;
33
class WasmEngine;
34 35
struct WasmFunction;

36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
class WasmInstructionBuffer final {
 public:
  ~WasmInstructionBuffer();
  std::unique_ptr<AssemblerBuffer> CreateView();
  std::unique_ptr<uint8_t[]> ReleaseBuffer();

  static std::unique_ptr<WasmInstructionBuffer> New();

 private:
  WasmInstructionBuffer() = delete;
  DISALLOW_COPY_AND_ASSIGN(WasmInstructionBuffer);
};

struct WasmCompilationResult {
 public:
  MOVE_ONLY_WITH_DEFAULT_CONSTRUCTORS(WasmCompilationResult);

  explicit WasmCompilationResult(WasmError error) : error(std::move(error)) {}

  bool succeeded() const {
    DCHECK_EQ(code_desc.buffer != nullptr, error.empty());
    return error.empty();
  }
  operator bool() const { return succeeded(); }

  CodeDesc code_desc;
  std::unique_ptr<uint8_t[]> instr_buffer;
  uint32_t frame_slot_count = 0;
64
  uint32_t tagged_parameter_slots = 0;
65 66 67 68 69 70
  OwnedVector<byte> source_positions;
  OwnedVector<trap_handler::ProtectedInstructionData> protected_instructions;

  WasmError error;
};

71 72
class WasmCompilationUnit final {
 public:
73
  static ExecutionTier GetDefaultExecutionTier(const WasmModule*);
74 75 76 77

  // If constructing from a background thread, pass in a Counters*, and ensure
  // that the Counters live at least as long as this compilation unit (which
  // typically means to hold a std::shared_ptr<Counters>).
78 79
  // If used exclusively from a foreground thread, Isolate::counters() may be
  // used by callers to pass Counters.
80
  WasmCompilationUnit(WasmEngine*, int index, ExecutionTier);
81 82 83

  ~WasmCompilationUnit();

84 85 86 87 88
  WasmCompilationResult ExecuteCompilation(
      CompilationEnv*, const std::shared_ptr<WireBytesStorage>&, Counters*,
      WasmFeatures* detected);

  WasmCode* Publish(WasmCompilationResult, NativeModule*);
89

90 91
  ExecutionTier requested_tier() const { return requested_tier_; }
  ExecutionTier executed_tier() const { return executed_tier_; }
92

93 94 95
  static void CompileWasmFunction(Isolate*, NativeModule*,
                                  WasmFeatures* detected, const WasmFunction*,
                                  ExecutionTier);
96 97 98 99 100

 private:
  friend class LiftoffCompilationUnit;
  friend class compiler::TurbofanWasmCompilationUnit;

101 102
  WasmEngine* const wasm_engine_;
  const int func_index_;
103 104
  ExecutionTier requested_tier_;
  ExecutionTier executed_tier_;
105

106
  // LiftoffCompilationUnit, set if {tier_ == kLiftoff}.
107
  std::unique_ptr<LiftoffCompilationUnit> liftoff_unit_;
108
  // TurbofanWasmCompilationUnit, set if {tier_ == kTurbofan}.
109 110
  std::unique_ptr<compiler::TurbofanWasmCompilationUnit> turbofan_unit_;

111
  void SwitchTier(ExecutionTier new_tier);
112 113 114 115 116 117 118 119 120

  DISALLOW_COPY_AND_ASSIGN(WasmCompilationUnit);
};

}  // namespace wasm
}  // namespace internal
}  // namespace v8

#endif  // V8_WASM_FUNCTION_COMPILER_H_