Source code

Revision control

Other Tools

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
*
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
40
ScriptedCaller() : filenameIsURL(false), line(0) {}
41
};
42
43
// Describes all the parameters that control wasm compilation.
44
45
struct CompileArgs;
46
typedef RefPtr<CompileArgs> MutableCompileArgs;
47
typedef RefPtr<const CompileArgs> SharedCompileArgs;
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 gcEnabled;
60
bool hugeMemory;
61
bool bigIntEnabled;
62
63
// CompileArgs has two constructors:
64
//
65
// - one through a factory function `build`, which checks that flags are
66
// consistent with each other.
67
// - one that gives complete access to underlying fields.
68
//
69
// You should use the first one in general, unless you have a very good
70
// reason (i.e. no JSContext around and you know which flags have been used).
71
72
static SharedCompileArgs build(JSContext* cx,
73
ScriptedCaller&& scriptedCaller);
74
75
explicit CompileArgs(ScriptedCaller&& scriptedCaller)
76
: scriptedCaller(std::move(scriptedCaller)),
77
baselineEnabled(false),
78
ionEnabled(false),
79
craneliftEnabled(false),
80
debugEnabled(false),
81
sharedMemoryEnabled(false),
82
forceTiering(false),
83
gcEnabled(false),
84
hugeMemory(false),
85
bigIntEnabled(false) {}
86
};
87
88
// Return the estimated compiled (machine) code size for the given bytecode size
89
// compiled at the given tier.
90
91
double EstimateCompiledCodeSize(Tier tier, size_t bytecodeSize);
92
93
// Compile the given WebAssembly bytecode with the given arguments into a
94
// wasm::Module. On success, the Module is returned. On failure, the returned
95
// SharedModule pointer is null and either:
96
// - *error points to a string description of the error
97
// - *error is null and the caller should report out-of-memory.
98
99
SharedModule CompileBuffer(const CompileArgs& args,
100
const ShareableBytes& bytecode, UniqueChars* error,
101
UniqueCharsVector* warnings,
102
JS::OptimizedEncodingListener* listener = nullptr);
103
104
// Attempt to compile the second tier of the given wasm::Module.
105
106
void CompileTier2(const CompileArgs& args, const Bytes& bytecode,
107
const Module& module, Atomic<bool>* cancelled);
108
109
// Compile the given WebAssembly module which has been broken into three
110
// partitions:
111
// - envBytes contains a complete ModuleEnvironment that has already been
112
// copied in from the stream.
113
// - codeBytes is pre-sized to hold the complete code section when the stream
114
// completes.
115
// - The range [codeBytes.begin(), codeBytesEnd) contains the bytes currently
116
// read from the stream and codeBytesEnd will advance until either
117
// the stream is cancelled or codeBytesEnd == codeBytes.end().
118
// - streamEnd contains the final information received after the code section:
119
// the remaining module bytecodes and maybe a JS::OptimizedEncodingListener.
120
// When the stream is successfully closed, streamEnd.reached is set.
121
// The ExclusiveWaitableData are notified when CompileStreaming() can make
122
// progress (i.e., codeBytesEnd advances or streamEnd.reached is set).
123
// If cancelled is set to true, compilation aborts and returns null. After
124
// cancellation is set, both ExclusiveWaitableData will be notified and so every
125
// wait() loop must check cancelled.
126
127
typedef ExclusiveWaitableData<const uint8_t*> ExclusiveBytesPtr;
128
129
struct StreamEndData {
130
bool reached;
131
const Bytes* tailBytes;
132
Tier2Listener tier2Listener;
133
134
StreamEndData() : reached(false) {}
135
};
136
typedef ExclusiveWaitableData<StreamEndData> ExclusiveStreamEndData;
137
138
SharedModule CompileStreaming(const CompileArgs& args, const Bytes& envBytes,
139
const Bytes& codeBytes,
140
const ExclusiveBytesPtr& codeBytesEnd,
141
const ExclusiveStreamEndData& streamEnd,
142
const Atomic<bool>& cancelled, UniqueChars* error,
143
UniqueCharsVector* warnings);
144
145
} // namespace wasm
146
} // namespace js
147
148
#endif // namespace wasm_compile_h