1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2  * vim: set ts=8 sts=2 et sw=2 tw=80:
3  *
4  * Copyright 2015 Mozilla Foundation
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 
19 #ifndef wasm_compile_h
20 #define wasm_compile_h
21 
22 #include "wasm/WasmModule.h"
23 
24 namespace js {
25 namespace wasm {
26 
27 // Return a uint32_t which captures the observed properties of the CPU that
28 // affect compilation. If code compiled now is to be serialized and executed
29 // later, the ObservedCPUFeatures() must be ensured to be the same.
30 
31 uint32_t ObservedCPUFeatures();
32 
33 // Describes the JS scripted caller of a request to compile a wasm module.
34 
35 struct ScriptedCaller {
36   UniqueChars filename;
37   bool filenameIsURL;
38   unsigned line;
39 
ScriptedCallerScriptedCaller40   ScriptedCaller() : filenameIsURL(false), line(0) {}
41 };
42 
43 // Describes all the parameters that control wasm compilation.
44 
45 struct CompileArgs;
46 using MutableCompileArgs = RefPtr<CompileArgs>;
47 using SharedCompileArgs = RefPtr<const CompileArgs>;
48 
49 struct CompileArgs : ShareableBase<CompileArgs> {
50   ScriptedCaller scriptedCaller;
51   UniqueChars sourceMapURL;
52 
53   bool baselineEnabled;
54   bool ionEnabled;
55   bool craneliftEnabled;
56   bool debugEnabled;
57   bool sharedMemoryEnabled;
58   bool forceTiering;
59   bool reftypesEnabled;
60   bool gcEnabled;
61   bool hugeMemory;
62   bool multiValuesEnabled;
63   bool v128Enabled;
64 
65   // CompileArgs has two constructors:
66   //
67   // - one through a factory function `build`, which checks that flags are
68   // consistent with each other.
69   // - one that gives complete access to underlying fields.
70   //
71   // You should use the first one in general, unless you have a very good
72   // reason (i.e. no JSContext around and you know which flags have been used).
73 
74   static SharedCompileArgs build(JSContext* cx,
75                                  ScriptedCaller&& scriptedCaller);
76 
CompileArgsCompileArgs77   explicit CompileArgs(ScriptedCaller&& scriptedCaller)
78       : scriptedCaller(std::move(scriptedCaller)),
79         baselineEnabled(false),
80         ionEnabled(false),
81         craneliftEnabled(false),
82         debugEnabled(false),
83         sharedMemoryEnabled(false),
84         forceTiering(false),
85         reftypesEnabled(false),
86         gcEnabled(false),
87         hugeMemory(false),
88         multiValuesEnabled(false),
89         v128Enabled(false) {}
90 };
91 
92 // Return the estimated compiled (machine) code size for the given bytecode size
93 // compiled at the given tier.
94 
95 double EstimateCompiledCodeSize(Tier tier, size_t bytecodeSize);
96 
97 // Compile the given WebAssembly bytecode with the given arguments into a
98 // wasm::Module. On success, the Module is returned. On failure, the returned
99 // SharedModule pointer is null and either:
100 //  - *error points to a string description of the error
101 //  - *error is null and the caller should report out-of-memory.
102 
103 SharedModule CompileBuffer(const CompileArgs& args,
104                            const ShareableBytes& bytecode, UniqueChars* error,
105                            UniqueCharsVector* warnings,
106                            JS::OptimizedEncodingListener* listener = nullptr);
107 
108 // Attempt to compile the second tier of the given wasm::Module.
109 
110 void CompileTier2(const CompileArgs& args, const Bytes& bytecode,
111                   const Module& module, Atomic<bool>* cancelled);
112 
113 // Compile the given WebAssembly module which has been broken into three
114 // partitions:
115 //  - envBytes contains a complete ModuleEnvironment that has already been
116 //    copied in from the stream.
117 //  - codeBytes is pre-sized to hold the complete code section when the stream
118 //    completes.
119 //  - The range [codeBytes.begin(), codeBytesEnd) contains the bytes currently
120 //    read from the stream and codeBytesEnd will advance until either
121 //    the stream is cancelled or codeBytesEnd == codeBytes.end().
122 //  - streamEnd contains the final information received after the code section:
123 //    the remaining module bytecodes and maybe a JS::OptimizedEncodingListener.
124 //    When the stream is successfully closed, streamEnd.reached is set.
125 // The ExclusiveWaitableData are notified when CompileStreaming() can make
126 // progress (i.e., codeBytesEnd advances or streamEnd.reached is set).
127 // If cancelled is set to true, compilation aborts and returns null. After
128 // cancellation is set, both ExclusiveWaitableData will be notified and so every
129 // wait() loop must check cancelled.
130 
131 using ExclusiveBytesPtr = ExclusiveWaitableData<const uint8_t*>;
132 
133 struct StreamEndData {
134   bool reached;
135   const Bytes* tailBytes;
136   Tier2Listener tier2Listener;
137 
StreamEndDataStreamEndData138   StreamEndData() : reached(false) {}
139 };
140 using ExclusiveStreamEndData = ExclusiveWaitableData<StreamEndData>;
141 
142 SharedModule CompileStreaming(const CompileArgs& args, const Bytes& envBytes,
143                               const Bytes& codeBytes,
144                               const ExclusiveBytesPtr& codeBytesEnd,
145                               const ExclusiveStreamEndData& streamEnd,
146                               const Atomic<bool>& cancelled, UniqueChars* error,
147                               UniqueCharsVector* warnings);
148 
149 }  // namespace wasm
150 }  // namespace js
151 
152 #endif  // namespace wasm_compile_h
153