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 "vm/Runtime.h"
23 #include "wasm/WasmModule.h"
24 
25 namespace JS {
26 class OptimizedEncodingListener;
27 }
28 
29 namespace js {
30 namespace wasm {
31 
32 // Return a uint32_t which captures the observed properties of the CPU that
33 // affect compilation. If code compiled now is to be serialized and executed
34 // later, the ObservedCPUFeatures() must be ensured to be the same.
35 
36 uint32_t ObservedCPUFeatures();
37 
38 // Return the estimated compiled (machine) code size for the given bytecode size
39 // compiled at the given tier.
40 
41 double EstimateCompiledCodeSize(Tier tier, size_t bytecodeSize);
42 
43 // Compile the given WebAssembly bytecode with the given arguments into a
44 // wasm::Module. On success, the Module is returned. On failure, the returned
45 // SharedModule pointer is null and either:
46 //  - *error points to a string description of the error
47 //  - *error is null and the caller should report out-of-memory.
48 
49 SharedModule CompileBuffer(const CompileArgs& args,
50                            const ShareableBytes& bytecode, UniqueChars* error,
51                            UniqueCharsVector* warnings,
52                            JS::OptimizedEncodingListener* listener = nullptr);
53 
54 // Attempt to compile the second tier of the given wasm::Module.
55 
56 bool CompileTier2(const CompileArgs& args, const Bytes& bytecode,
57                   const Module& module, UniqueChars* error,
58                   UniqueCharsVector* warnings, Atomic<bool>* cancelled);
59 
60 // Compile the given WebAssembly module which has been broken into three
61 // partitions:
62 //  - envBytes contains a complete ModuleEnvironment that has already been
63 //    copied in from the stream.
64 //  - codeBytes is pre-sized to hold the complete code section when the stream
65 //    completes.
66 //  - The range [codeBytes.begin(), codeBytesEnd) contains the bytes currently
67 //    read from the stream and codeBytesEnd will advance until either
68 //    the stream is cancelled or codeBytesEnd == codeBytes.end().
69 //  - streamEnd contains the final information received after the code section:
70 //    the remaining module bytecodes and maybe a JS::OptimizedEncodingListener.
71 //    When the stream is successfully closed, streamEnd.reached is set.
72 // The ExclusiveWaitableData are notified when CompileStreaming() can make
73 // progress (i.e., codeBytesEnd advances or streamEnd.reached is set).
74 // If cancelled is set to true, compilation aborts and returns null. After
75 // cancellation is set, both ExclusiveWaitableData will be notified and so every
76 // wait() loop must check cancelled.
77 
78 using ExclusiveBytesPtr = ExclusiveWaitableData<const uint8_t*>;
79 
80 struct StreamEndData {
81   bool reached;
82   const Bytes* tailBytes;
83   Tier2Listener tier2Listener;
84 
StreamEndDataStreamEndData85   StreamEndData() : reached(false) {}
86 };
87 using ExclusiveStreamEndData = ExclusiveWaitableData<StreamEndData>;
88 
89 SharedModule CompileStreaming(const CompileArgs& args, const Bytes& envBytes,
90                               const Bytes& codeBytes,
91                               const ExclusiveBytesPtr& codeBytesEnd,
92                               const ExclusiveStreamEndData& streamEnd,
93                               const Atomic<bool>& cancelled, UniqueChars* error,
94                               UniqueCharsVector* warnings);
95 
96 }  // namespace wasm
97 }  // namespace js
98 
99 #endif  // namespace wasm_compile_h
100