blob: 93c2ae91d10168e573ac8f0178bb261e74d5ca8e [file] [log] [blame]
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001// Copyright 2015 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_COMPILER_WASM_COMPILER_H_
6#define V8_COMPILER_WASM_COMPILER_H_
7
8// Clients of this interface shouldn't depend on lots of compiler internals.
9// Do not include anything from src/compiler here!
10#include "src/wasm/wasm-opcodes.h"
11#include "src/zone.h"
12
13namespace v8 {
14namespace internal {
15
16namespace compiler {
17// Forward declarations for some compiler data structures.
18class Node;
19class JSGraph;
20class Graph;
Ben Murdochc5610432016-08-08 18:44:38 +010021class Operator;
22class SourcePositionTable;
23class WasmCompilationUnit;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000024}
25
26namespace wasm {
27// Forward declarations for some WASM data structures.
28struct ModuleEnv;
29struct WasmFunction;
30class ErrorThrower;
31
32// Expose {Node} and {Graph} opaquely as {wasm::TFNode} and {wasm::TFGraph}.
33typedef compiler::Node TFNode;
34typedef compiler::JSGraph TFGraph;
35}
36
37namespace compiler {
38// Compiles a single function, producing a code object.
Ben Murdochc5610432016-08-08 18:44:38 +010039Handle<Code> CompileWasmFunction(wasm::ErrorThrower* thrower, Isolate* isolate,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000040 wasm::ModuleEnv* module_env,
Ben Murdochc5610432016-08-08 18:44:38 +010041 const wasm::WasmFunction* function);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000042
43// Wraps a JS function, producing a code object that can be called from WASM.
44Handle<Code> CompileWasmToJSWrapper(Isolate* isolate, wasm::ModuleEnv* module,
45 Handle<JSFunction> function,
Ben Murdochda12d292016-06-02 14:46:10 +010046 wasm::FunctionSig* sig,
47 wasm::WasmName module_name,
48 wasm::WasmName function_name);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000049
50// Wraps a given wasm code object, producing a JSFunction that can be called
51// from JavaScript.
52Handle<JSFunction> CompileJSToWasmWrapper(
53 Isolate* isolate, wasm::ModuleEnv* module, Handle<String> name,
54 Handle<Code> wasm_code, Handle<JSObject> module_object, uint32_t index);
55
Ben Murdochc5610432016-08-08 18:44:38 +010056WasmCompilationUnit* CreateWasmCompilationUnit(
57 wasm::ErrorThrower* thrower, Isolate* isolate, wasm::ModuleEnv* module_env,
58 const wasm::WasmFunction* function, uint32_t index);
59
60void ExecuteCompilation(WasmCompilationUnit* unit);
61
62Handle<Code> FinishCompilation(WasmCompilationUnit* unit);
63
64uint32_t GetIndexOfWasmCompilationUnit(WasmCompilationUnit* unit);
65
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000066// Abstracts details of building TurboFan graph nodes for WASM to separate
67// the WASM decoder from the internal details of TurboFan.
68class WasmTrapHelper;
69class WasmGraphBuilder {
70 public:
Ben Murdochc5610432016-08-08 18:44:38 +010071 WasmGraphBuilder(
72 Zone* z, JSGraph* g, wasm::FunctionSig* function_signature,
73 compiler::SourcePositionTable* source_position_table = nullptr);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000074
75 Node** Buffer(size_t count) {
76 if (count > cur_bufsize_) {
77 size_t new_size = count + cur_bufsize_ + 5;
78 cur_buffer_ =
79 reinterpret_cast<Node**>(zone_->New(new_size * sizeof(Node*)));
80 cur_bufsize_ = new_size;
81 }
82 return cur_buffer_;
83 }
84
85 //-----------------------------------------------------------------------
86 // Operations independent of {control} or {effect}.
87 //-----------------------------------------------------------------------
88 Node* Error();
89 Node* Start(unsigned params);
90 Node* Param(unsigned index, wasm::LocalType type);
91 Node* Loop(Node* entry);
92 Node* Terminate(Node* effect, Node* control);
93 Node* Merge(unsigned count, Node** controls);
94 Node* Phi(wasm::LocalType type, unsigned count, Node** vals, Node* control);
95 Node* EffectPhi(unsigned count, Node** effects, Node* control);
Ben Murdochc5610432016-08-08 18:44:38 +010096 Node* NumberConstant(int32_t value);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000097 Node* Int32Constant(int32_t value);
98 Node* Int64Constant(int64_t value);
99 Node* Float32Constant(float value);
100 Node* Float64Constant(double value);
Ben Murdochc5610432016-08-08 18:44:38 +0100101 Node* HeapConstant(Handle<HeapObject> value);
102 Node* Binop(wasm::WasmOpcode opcode, Node* left, Node* right,
103 wasm::WasmCodePosition position = wasm::kNoCodePosition);
104 Node* Unop(wasm::WasmOpcode opcode, Node* input,
105 wasm::WasmCodePosition position = wasm::kNoCodePosition);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000106 unsigned InputCount(Node* node);
107 bool IsPhiWithMerge(Node* phi, Node* merge);
108 void AppendToMerge(Node* merge, Node* from);
Ben Murdochc5610432016-08-08 18:44:38 +0100109 void AppendToPhi(Node* phi, Node* from);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000110
111 //-----------------------------------------------------------------------
112 // Operations that read and/or write {control} and {effect}.
113 //-----------------------------------------------------------------------
114 Node* Branch(Node* cond, Node** true_node, Node** false_node);
115 Node* Switch(unsigned count, Node* key);
116 Node* IfValue(int32_t value, Node* sw);
117 Node* IfDefault(Node* sw);
118 Node* Return(unsigned count, Node** vals);
119 Node* ReturnVoid();
Ben Murdochc5610432016-08-08 18:44:38 +0100120 Node* Unreachable(wasm::WasmCodePosition position);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000121
Ben Murdochc5610432016-08-08 18:44:38 +0100122 Node* CallDirect(uint32_t index, Node** args,
123 wasm::WasmCodePosition position);
124 Node* CallImport(uint32_t index, Node** args,
125 wasm::WasmCodePosition position);
126 Node* CallIndirect(uint32_t index, Node** args,
127 wasm::WasmCodePosition position);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000128 void BuildJSToWasmWrapper(Handle<Code> wasm_code, wasm::FunctionSig* sig);
129 void BuildWasmToJSWrapper(Handle<JSFunction> function,
130 wasm::FunctionSig* sig);
Ben Murdochc5610432016-08-08 18:44:38 +0100131
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000132 Node* ToJS(Node* node, Node* context, wasm::LocalType type);
133 Node* FromJS(Node* node, Node* context, wasm::LocalType type);
134 Node* Invert(Node* node);
135 Node* FunctionTable();
136
137 //-----------------------------------------------------------------------
138 // Operations that concern the linear memory.
139 //-----------------------------------------------------------------------
140 Node* MemSize(uint32_t offset);
141 Node* LoadGlobal(uint32_t index);
142 Node* StoreGlobal(uint32_t index, Node* val);
143 Node* LoadMem(wasm::LocalType type, MachineType memtype, Node* index,
Ben Murdochc5610432016-08-08 18:44:38 +0100144 uint32_t offset, wasm::WasmCodePosition position);
145 Node* StoreMem(MachineType type, Node* index, uint32_t offset, Node* val,
146 wasm::WasmCodePosition position);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000147
148 static void PrintDebugName(Node* node);
149
150 Node* Control() { return *control_; }
151 Node* Effect() { return *effect_; }
152
153 void set_module(wasm::ModuleEnv* module) { this->module_ = module; }
154
155 void set_control_ptr(Node** control) { this->control_ = control; }
156
157 void set_effect_ptr(Node** effect) { this->effect_ = effect; }
158
159 wasm::FunctionSig* GetFunctionSignature() { return function_signature_; }
160
Ben Murdoch097c5b22016-05-18 11:27:45 +0100161 void Int64LoweringForTesting();
162
Ben Murdochc5610432016-08-08 18:44:38 +0100163 void SetSourcePosition(Node* node, wasm::WasmCodePosition position);
164
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000165 private:
166 static const int kDefaultBufferSize = 16;
167 friend class WasmTrapHelper;
168
169 Zone* zone_;
170 JSGraph* jsgraph_;
171 wasm::ModuleEnv* module_;
172 Node* mem_buffer_;
173 Node* mem_size_;
174 Node* function_table_;
175 Node** control_;
176 Node** effect_;
177 Node** cur_buffer_;
178 size_t cur_bufsize_;
179 Node* def_buffer_[kDefaultBufferSize];
180
181 WasmTrapHelper* trap_;
182 wasm::FunctionSig* function_signature_;
Ben Murdochc5610432016-08-08 18:44:38 +0100183 SetOncePointer<const Operator> allocate_heap_number_operator_;
184
185 compiler::SourcePositionTable* source_position_table_ = nullptr;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000186
187 // Internal helper methods.
188 JSGraph* jsgraph() { return jsgraph_; }
189 Graph* graph();
190
191 Node* String(const char* string);
192 Node* MemBuffer(uint32_t offset);
Ben Murdochc5610432016-08-08 18:44:38 +0100193 void BoundsCheckMem(MachineType memtype, Node* index, uint32_t offset,
194 wasm::WasmCodePosition position);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000195
Ben Murdochda12d292016-06-02 14:46:10 +0100196 Node* MaskShiftCount32(Node* node);
197 Node* MaskShiftCount64(Node* node);
198
Ben Murdoch097c5b22016-05-18 11:27:45 +0100199 Node* BuildCCall(MachineSignature* sig, Node** args);
Ben Murdochc5610432016-08-08 18:44:38 +0100200 Node* BuildWasmCall(wasm::FunctionSig* sig, Node** args,
201 wasm::WasmCodePosition position);
Ben Murdochda12d292016-06-02 14:46:10 +0100202
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000203 Node* BuildF32Neg(Node* input);
204 Node* BuildF64Neg(Node* input);
205 Node* BuildF32CopySign(Node* left, Node* right);
206 Node* BuildF64CopySign(Node* left, Node* right);
207 Node* BuildF32Min(Node* left, Node* right);
208 Node* BuildF32Max(Node* left, Node* right);
209 Node* BuildF64Min(Node* left, Node* right);
210 Node* BuildF64Max(Node* left, Node* right);
Ben Murdochc5610432016-08-08 18:44:38 +0100211 Node* BuildI32SConvertF32(Node* input, wasm::WasmCodePosition position);
212 Node* BuildI32SConvertF64(Node* input, wasm::WasmCodePosition position);
213 Node* BuildI32UConvertF32(Node* input, wasm::WasmCodePosition position);
214 Node* BuildI32UConvertF64(Node* input, wasm::WasmCodePosition position);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000215 Node* BuildI32Ctz(Node* input);
216 Node* BuildI32Popcnt(Node* input);
217 Node* BuildI64Ctz(Node* input);
218 Node* BuildI64Popcnt(Node* input);
Ben Murdochc5610432016-08-08 18:44:38 +0100219 Node* BuildBitCountingCall(Node* input, ExternalReference ref,
220 MachineRepresentation input_type);
221
Ben Murdochda12d292016-06-02 14:46:10 +0100222 Node* BuildCFuncInstruction(ExternalReference ref, MachineType type,
223 Node* input0, Node* input1 = nullptr);
Ben Murdoch097c5b22016-05-18 11:27:45 +0100224 Node* BuildF32Trunc(Node* input);
225 Node* BuildF32Floor(Node* input);
226 Node* BuildF32Ceil(Node* input);
227 Node* BuildF32NearestInt(Node* input);
228 Node* BuildF64Trunc(Node* input);
229 Node* BuildF64Floor(Node* input);
230 Node* BuildF64Ceil(Node* input);
231 Node* BuildF64NearestInt(Node* input);
Ben Murdochda12d292016-06-02 14:46:10 +0100232 Node* BuildI32Rol(Node* left, Node* right);
233 Node* BuildI64Rol(Node* left, Node* right);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000234
Ben Murdochda12d292016-06-02 14:46:10 +0100235 Node* BuildF64Acos(Node* input);
236 Node* BuildF64Asin(Node* input);
237 Node* BuildF64Atan(Node* input);
238 Node* BuildF64Cos(Node* input);
239 Node* BuildF64Sin(Node* input);
240 Node* BuildF64Tan(Node* input);
241 Node* BuildF64Exp(Node* input);
242 Node* BuildF64Log(Node* input);
243 Node* BuildF64Pow(Node* left, Node* right);
244 Node* BuildF64Atan2(Node* left, Node* right);
245 Node* BuildF64Mod(Node* left, Node* right);
246
247 Node* BuildIntToFloatConversionInstruction(
248 Node* input, ExternalReference ref,
249 MachineRepresentation parameter_representation,
250 const MachineType result_type);
251 Node* BuildF32SConvertI64(Node* input);
252 Node* BuildF32UConvertI64(Node* input);
253 Node* BuildF64SConvertI64(Node* input);
254 Node* BuildF64UConvertI64(Node* input);
255
256 Node* BuildFloatToIntConversionInstruction(
257 Node* input, ExternalReference ref,
258 MachineRepresentation parameter_representation,
Ben Murdochc5610432016-08-08 18:44:38 +0100259 const MachineType result_type, wasm::WasmCodePosition position);
260 Node* BuildI64SConvertF32(Node* input, wasm::WasmCodePosition position);
261 Node* BuildI64UConvertF32(Node* input, wasm::WasmCodePosition position);
262 Node* BuildI64SConvertF64(Node* input, wasm::WasmCodePosition position);
263 Node* BuildI64UConvertF64(Node* input, wasm::WasmCodePosition position);
Ben Murdochda12d292016-06-02 14:46:10 +0100264
Ben Murdochc5610432016-08-08 18:44:38 +0100265 Node* BuildI32DivS(Node* left, Node* right, wasm::WasmCodePosition position);
266 Node* BuildI32RemS(Node* left, Node* right, wasm::WasmCodePosition position);
267 Node* BuildI32DivU(Node* left, Node* right, wasm::WasmCodePosition position);
268 Node* BuildI32RemU(Node* left, Node* right, wasm::WasmCodePosition position);
Ben Murdochda12d292016-06-02 14:46:10 +0100269
Ben Murdochc5610432016-08-08 18:44:38 +0100270 Node* BuildI64DivS(Node* left, Node* right, wasm::WasmCodePosition position);
271 Node* BuildI64RemS(Node* left, Node* right, wasm::WasmCodePosition position);
272 Node* BuildI64DivU(Node* left, Node* right, wasm::WasmCodePosition position);
273 Node* BuildI64RemU(Node* left, Node* right, wasm::WasmCodePosition position);
Ben Murdochda12d292016-06-02 14:46:10 +0100274 Node* BuildDiv64Call(Node* left, Node* right, ExternalReference ref,
Ben Murdochc5610432016-08-08 18:44:38 +0100275 MachineType result_type, int trap_zero,
276 wasm::WasmCodePosition position);
277
278 Node* BuildJavaScriptToNumber(Node* node, Node* context, Node* effect,
279 Node* control);
280 Node* BuildChangeInt32ToTagged(Node* value);
281 Node* BuildChangeFloat64ToTagged(Node* value);
282 Node* BuildChangeTaggedToFloat64(Node* value);
283
284 Node* BuildChangeInt32ToSmi(Node* value);
285 Node* BuildChangeSmiToInt32(Node* value);
286 Node* BuildChangeSmiToFloat64(Node* value);
287 Node* BuildTestNotSmi(Node* value);
288 Node* BuildSmiShiftBitsConstant();
289
290 Node* BuildAllocateHeapNumberWithValue(Node* value, Node* control);
291 Node* BuildLoadHeapNumberValue(Node* value, Node* control);
292 Node* BuildHeapNumberValueIndexConstant();
293
294 // Asm.js specific functionality.
295 Node* BuildI32AsmjsSConvertF32(Node* input);
296 Node* BuildI32AsmjsSConvertF64(Node* input);
297 Node* BuildI32AsmjsUConvertF32(Node* input);
298 Node* BuildI32AsmjsUConvertF64(Node* input);
299 Node* BuildI32AsmjsDivS(Node* left, Node* right);
300 Node* BuildI32AsmjsRemS(Node* left, Node* right);
301 Node* BuildI32AsmjsDivU(Node* left, Node* right);
302 Node* BuildI32AsmjsRemU(Node* left, Node* right);
303 Node* BuildAsmjsLoadMem(MachineType type, Node* index);
304 Node* BuildAsmjsStoreMem(MachineType type, Node* index, Node* val);
Ben Murdochda12d292016-06-02 14:46:10 +0100305
306 Node** Realloc(Node** buffer, size_t old_count, size_t new_count) {
307 Node** buf = Buffer(new_count);
308 if (buf != buffer) memcpy(buf, buffer, old_count * sizeof(Node*));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000309 return buf;
310 }
311};
312} // namespace compiler
313} // namespace internal
314} // namespace v8
315
316#endif // V8_COMPILER_WASM_COMPILER_H_