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