• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/wasm/function-body-decoder.h"
9 #include "src/wasm/wasm-limits.h"
10 #include "src/wasm/wasm-module.h"
11 #include "src/wasm/wasm-tier.h"
12 
13 namespace v8 {
14 namespace internal {
15 
16 class Counters;
17 
18 namespace compiler {
19 class TurbofanWasmCompilationUnit;
20 }  // namespace compiler
21 
22 namespace wasm {
23 
24 class LiftoffCompilationUnit;
25 struct ModuleWireBytes;
26 class NativeModule;
27 class WasmCode;
28 class WasmEngine;
29 struct WasmFunction;
30 
31 enum RuntimeExceptionSupport : bool {
32   kRuntimeExceptionSupport = true,
33   kNoRuntimeExceptionSupport = false
34 };
35 
36 enum UseTrapHandler : bool { kUseTrapHandler = true, kNoTrapHandler = false };
37 
38 enum LowerSimd : bool { kLowerSimd = true, kNoLowerSimd = false };
39 
40 // The {ModuleEnv} encapsulates the module data that is used during compilation.
41 // ModuleEnvs are shareable across multiple compilations.
42 struct ModuleEnv {
43   // A pointer to the decoded module's static representation.
44   const WasmModule* const module;
45 
46   // True if trap handling should be used in compiled code, rather than
47   // compiling in bounds checks for each memory access.
48   const UseTrapHandler use_trap_handler;
49 
50   // If the runtime doesn't support exception propagation,
51   // we won't generate stack checks, and trap handling will also
52   // be generated differently.
53   const RuntimeExceptionSupport runtime_exception_support;
54 
55   // The smallest size of any memory that could be used with this module, in
56   // bytes.
57   const uint64_t min_memory_size;
58 
59   // The largest size of any memory that could be used with this module, in
60   // bytes.
61   const uint64_t max_memory_size;
62 
63   const LowerSimd lower_simd;
64 
65   constexpr ModuleEnv(const WasmModule* module, UseTrapHandler use_trap_handler,
66                       RuntimeExceptionSupport runtime_exception_support,
67                       LowerSimd lower_simd = kNoLowerSimd)
moduleModuleEnv68       : module(module),
69         use_trap_handler(use_trap_handler),
70         runtime_exception_support(runtime_exception_support),
71         min_memory_size(module ? module->initial_pages * uint64_t{kWasmPageSize}
72                                : 0),
73         max_memory_size(module && module->has_maximum_pages
74                             ? (module->maximum_pages * uint64_t{kWasmPageSize})
75                             : kSpecMaxWasmMemoryBytes),
76         lower_simd(lower_simd) {}
77 };
78 
79 class WasmCompilationUnit final {
80  public:
81   static ExecutionTier GetDefaultExecutionTier();
82 
83   // If constructing from a background thread, pass in a Counters*, and ensure
84   // that the Counters live at least as long as this compilation unit (which
85   // typically means to hold a std::shared_ptr<Counters>).
86   // If used exclusively from a foreground thread, Isolate::counters() may be
87   // used by callers to pass Counters.
88   WasmCompilationUnit(WasmEngine* wasm_engine, ModuleEnv*, NativeModule*,
89                       FunctionBody, WasmName, int index, Counters*,
90                       ExecutionTier = GetDefaultExecutionTier());
91 
92   ~WasmCompilationUnit();
93 
94   void ExecuteCompilation(WasmFeatures* detected);
95   WasmCode* FinishCompilation(ErrorThrower* thrower);
96 
97   static WasmCode* CompileWasmFunction(
98       Isolate* isolate, NativeModule* native_module, WasmFeatures* detected,
99       ErrorThrower* thrower, ModuleEnv* env, const WasmFunction* function,
100       ExecutionTier = GetDefaultExecutionTier());
101 
native_module()102   NativeModule* native_module() const { return native_module_; }
mode()103   ExecutionTier mode() const { return mode_; }
104 
105  private:
106   friend class LiftoffCompilationUnit;
107   friend class compiler::TurbofanWasmCompilationUnit;
108 
109   ModuleEnv* env_;
110   WasmEngine* wasm_engine_;
111   FunctionBody func_body_;
112   WasmName func_name_;
113   Counters* counters_;
114   int func_index_;
115   NativeModule* native_module_;
116   ExecutionTier mode_;
117   // LiftoffCompilationUnit, set if {mode_ == kLiftoff}.
118   std::unique_ptr<LiftoffCompilationUnit> liftoff_unit_;
119   // TurbofanWasmCompilationUnit, set if {mode_ == kTurbofan}.
120   std::unique_ptr<compiler::TurbofanWasmCompilationUnit> turbofan_unit_;
121 
122   void SwitchMode(ExecutionTier new_mode);
123 
124   DISALLOW_COPY_AND_ASSIGN(WasmCompilationUnit);
125 };
126 
127 }  // namespace wasm
128 }  // namespace internal
129 }  // namespace v8
130 
131 #endif  // V8_WASM_FUNCTION_COMPILER_H_
132