1// Copyright 2018 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#ifndef V8_WASM_FUNCTION_COMPILER_H_
6#define V8_WASM_FUNCTION_COMPILER_H_
7
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#include "src/wasm/wasm-limits.h"
13#include "src/wasm/wasm-module.h"
14#include "src/wasm/wasm-tier.h"
15
16namespace v8 {
17namespace internal {
18
19class AssemblerBuffer;
20class Counters;
21
22namespace compiler {
23class InterpreterCompilationUnit;
24class Pipeline;
25class TurbofanWasmCompilationUnit;
26} // namespace compiler
27
28namespace wasm {
29
30class LiftoffCompilationUnit;
31class NativeModule;
32class WasmCode;
33class WasmCompilationUnit;
34class WasmEngine;
35struct WasmFunction;
36
37class WasmInstructionBuffer final {
38 public:
39 ~WasmInstructionBuffer();
40 std::unique_ptr<AssemblerBuffer> CreateView();
41 std::unique_ptr<uint8_t[]> ReleaseBuffer();
42
43 static std::unique_ptr<WasmInstructionBuffer> New();
44
45 private:
46 WasmInstructionBuffer() = delete;
47 DISALLOW_COPY_AND_ASSIGN(WasmInstructionBuffer);
48};
49
50struct WasmCompilationResult {
51 public:
52 MOVE_ONLY_WITH_DEFAULT_CONSTRUCTORS(WasmCompilationResult);
53
54 bool succeeded() const { return code_desc.buffer != nullptr; }
55 bool failed() const { return !succeeded(); }
56 operator bool() const { return succeeded(); }
57
58 CodeDesc code_desc;
59 std::unique_ptr<uint8_t[]> instr_buffer;
60 uint32_t frame_slot_count = 0;
61 uint32_t tagged_parameter_slots = 0;
62 OwnedVector<byte> source_positions;
63 OwnedVector<trap_handler::ProtectedInstructionData> protected_instructions;
64 int func_index;
65 ExecutionTier requested_tier;
66 ExecutionTier result_tier;
67};
68
69class V8_EXPORT_PRIVATE WasmCompilationUnit final {
70 public:
71 static ExecutionTier GetDefaultExecutionTier(const WasmModule*);
72
73 WasmCompilationUnit(int index, ExecutionTier);
74
75 ~WasmCompilationUnit();
76
77 WasmCompilationResult ExecuteCompilation(
78 WasmEngine*, CompilationEnv*, const std::shared_ptr<WireBytesStorage>&,
79 Counters*, WasmFeatures* detected);
80
81 ExecutionTier tier() const { return tier_; }
82
83 static void CompileWasmFunction(Isolate*, NativeModule*,
84 WasmFeatures* detected, const WasmFunction*,
85 ExecutionTier);
86
87 private:
88 friend class LiftoffCompilationUnit;
89 friend class compiler::TurbofanWasmCompilationUnit;
90 friend class compiler::InterpreterCompilationUnit;
91
92 const int func_index_;
93 ExecutionTier tier_;
94
95 // LiftoffCompilationUnit, set if {tier_ == kLiftoff}.
96 std::unique_ptr<LiftoffCompilationUnit> liftoff_unit_;
97 // TurbofanWasmCompilationUnit, set if {tier_ == kTurbofan}.
98 std::unique_ptr<compiler::TurbofanWasmCompilationUnit> turbofan_unit_;
99 // InterpreterCompilationUnit, set if {tier_ == kInterpreter}.
100 std::unique_ptr<compiler::InterpreterCompilationUnit> interpreter_unit_;
101
102 void SwitchTier(ExecutionTier new_tier);
103
104 DISALLOW_COPY_AND_ASSIGN(WasmCompilationUnit);
105};
106
107} // namespace wasm
108} // namespace internal
109} // namespace v8
110
111#endif // V8_WASM_FUNCTION_COMPILER_H_
112