blob: bbcafa72960a786132ad01731ee49d7354790c57 [file] [log] [blame]
Ben Murdoch014dc512016-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;
21}
22
23namespace wasm {
24// Forward declarations for some WASM data structures.
25struct ModuleEnv;
26struct WasmFunction;
27class ErrorThrower;
28
29// Expose {Node} and {Graph} opaquely as {wasm::TFNode} and {wasm::TFGraph}.
30typedef compiler::Node TFNode;
31typedef compiler::JSGraph TFGraph;
32}
33
34namespace compiler {
35// Compiles a single function, producing a code object.
36Handle<Code> CompileWasmFunction(wasm::ErrorThrower& thrower, Isolate* isolate,
37 wasm::ModuleEnv* module_env,
Ben Murdoch109988c2016-05-18 11:27:45 +010038 const wasm::WasmFunction& function);
Ben Murdoch014dc512016-03-22 12:00:34 +000039
40// Wraps a JS function, producing a code object that can be called from WASM.
41Handle<Code> CompileWasmToJSWrapper(Isolate* isolate, wasm::ModuleEnv* module,
42 Handle<JSFunction> function,
Ben Murdoch3b9bc312016-06-02 14:46:10 +010043 wasm::FunctionSig* sig,
44 wasm::WasmName module_name,
45 wasm::WasmName function_name);
Ben Murdoch014dc512016-03-22 12:00:34 +000046
47// Wraps a given wasm code object, producing a JSFunction that can be called
48// from JavaScript.
49Handle<JSFunction> CompileJSToWasmWrapper(
50 Isolate* isolate, wasm::ModuleEnv* module, Handle<String> name,
51 Handle<Code> wasm_code, Handle<JSObject> module_object, uint32_t index);
52
53// Abstracts details of building TurboFan graph nodes for WASM to separate
54// the WASM decoder from the internal details of TurboFan.
55class WasmTrapHelper;
56class WasmGraphBuilder {
57 public:
58 WasmGraphBuilder(Zone* z, JSGraph* g, wasm::FunctionSig* function_signature);
59
60 Node** Buffer(size_t count) {
61 if (count > cur_bufsize_) {
62 size_t new_size = count + cur_bufsize_ + 5;
63 cur_buffer_ =
64 reinterpret_cast<Node**>(zone_->New(new_size * sizeof(Node*)));
65 cur_bufsize_ = new_size;
66 }
67 return cur_buffer_;
68 }
69
70 //-----------------------------------------------------------------------
71 // Operations independent of {control} or {effect}.
72 //-----------------------------------------------------------------------
73 Node* Error();
74 Node* Start(unsigned params);
75 Node* Param(unsigned index, wasm::LocalType type);
76 Node* Loop(Node* entry);
77 Node* Terminate(Node* effect, Node* control);
78 Node* Merge(unsigned count, Node** controls);
79 Node* Phi(wasm::LocalType type, unsigned count, Node** vals, Node* control);
80 Node* EffectPhi(unsigned count, Node** effects, Node* control);
81 Node* Int32Constant(int32_t value);
82 Node* Int64Constant(int64_t value);
83 Node* Float32Constant(float value);
84 Node* Float64Constant(double value);
85 Node* Constant(Handle<Object> value);
86 Node* Binop(wasm::WasmOpcode opcode, Node* left, Node* right);
87 Node* Unop(wasm::WasmOpcode opcode, Node* input);
88 unsigned InputCount(Node* node);
89 bool IsPhiWithMerge(Node* phi, Node* merge);
90 void AppendToMerge(Node* merge, Node* from);
91 void AppendToPhi(Node* merge, Node* phi, Node* from);
92
93 //-----------------------------------------------------------------------
94 // Operations that read and/or write {control} and {effect}.
95 //-----------------------------------------------------------------------
96 Node* Branch(Node* cond, Node** true_node, Node** false_node);
97 Node* Switch(unsigned count, Node* key);
98 Node* IfValue(int32_t value, Node* sw);
99 Node* IfDefault(Node* sw);
100 Node* Return(unsigned count, Node** vals);
101 Node* ReturnVoid();
102 Node* Unreachable();
103
104 Node* CallDirect(uint32_t index, Node** args);
Ben Murdoch109988c2016-05-18 11:27:45 +0100105 Node* CallImport(uint32_t index, Node** args);
Ben Murdoch014dc512016-03-22 12:00:34 +0000106 Node* CallIndirect(uint32_t index, Node** args);
107 void BuildJSToWasmWrapper(Handle<Code> wasm_code, wasm::FunctionSig* sig);
108 void BuildWasmToJSWrapper(Handle<JSFunction> function,
109 wasm::FunctionSig* sig);
110 Node* ToJS(Node* node, Node* context, wasm::LocalType type);
111 Node* FromJS(Node* node, Node* context, wasm::LocalType type);
112 Node* Invert(Node* node);
113 Node* FunctionTable();
114
115 //-----------------------------------------------------------------------
116 // Operations that concern the linear memory.
117 //-----------------------------------------------------------------------
118 Node* MemSize(uint32_t offset);
119 Node* LoadGlobal(uint32_t index);
120 Node* StoreGlobal(uint32_t index, Node* val);
121 Node* LoadMem(wasm::LocalType type, MachineType memtype, Node* index,
122 uint32_t offset);
123 Node* StoreMem(MachineType type, Node* index, uint32_t offset, Node* val);
124
125 static void PrintDebugName(Node* node);
126
127 Node* Control() { return *control_; }
128 Node* Effect() { return *effect_; }
129
130 void set_module(wasm::ModuleEnv* module) { this->module_ = module; }
131
132 void set_control_ptr(Node** control) { this->control_ = control; }
133
134 void set_effect_ptr(Node** effect) { this->effect_ = effect; }
135
136 wasm::FunctionSig* GetFunctionSignature() { return function_signature_; }
137
Ben Murdoch109988c2016-05-18 11:27:45 +0100138 void Int64LoweringForTesting();
139
Ben Murdoch014dc512016-03-22 12:00:34 +0000140 private:
141 static const int kDefaultBufferSize = 16;
142 friend class WasmTrapHelper;
143
144 Zone* zone_;
145 JSGraph* jsgraph_;
146 wasm::ModuleEnv* module_;
147 Node* mem_buffer_;
148 Node* mem_size_;
149 Node* function_table_;
150 Node** control_;
151 Node** effect_;
152 Node** cur_buffer_;
153 size_t cur_bufsize_;
154 Node* def_buffer_[kDefaultBufferSize];
155
156 WasmTrapHelper* trap_;
157 wasm::FunctionSig* function_signature_;
158
159 // Internal helper methods.
160 JSGraph* jsgraph() { return jsgraph_; }
161 Graph* graph();
162
163 Node* String(const char* string);
164 Node* MemBuffer(uint32_t offset);
165 void BoundsCheckMem(MachineType memtype, Node* index, uint32_t offset);
166
Ben Murdoch3b9bc312016-06-02 14:46:10 +0100167 Node* MaskShiftCount32(Node* node);
168 Node* MaskShiftCount64(Node* node);
169
Ben Murdoch109988c2016-05-18 11:27:45 +0100170 Node* BuildCCall(MachineSignature* sig, Node** args);
Ben Murdoch014dc512016-03-22 12:00:34 +0000171 Node* BuildWasmCall(wasm::FunctionSig* sig, Node** args);
Ben Murdoch3b9bc312016-06-02 14:46:10 +0100172
Ben Murdoch014dc512016-03-22 12:00:34 +0000173 Node* BuildF32Neg(Node* input);
174 Node* BuildF64Neg(Node* input);
175 Node* BuildF32CopySign(Node* left, Node* right);
176 Node* BuildF64CopySign(Node* left, Node* right);
177 Node* BuildF32Min(Node* left, Node* right);
178 Node* BuildF32Max(Node* left, Node* right);
179 Node* BuildF64Min(Node* left, Node* right);
180 Node* BuildF64Max(Node* left, Node* right);
181 Node* BuildI32SConvertF32(Node* input);
182 Node* BuildI32SConvertF64(Node* input);
183 Node* BuildI32UConvertF32(Node* input);
184 Node* BuildI32UConvertF64(Node* input);
185 Node* BuildI32Ctz(Node* input);
186 Node* BuildI32Popcnt(Node* input);
187 Node* BuildI64Ctz(Node* input);
188 Node* BuildI64Popcnt(Node* input);
Ben Murdoch3b9bc312016-06-02 14:46:10 +0100189 Node* BuildCFuncInstruction(ExternalReference ref, MachineType type,
190 Node* input0, Node* input1 = nullptr);
Ben Murdoch109988c2016-05-18 11:27:45 +0100191 Node* BuildF32Trunc(Node* input);
192 Node* BuildF32Floor(Node* input);
193 Node* BuildF32Ceil(Node* input);
194 Node* BuildF32NearestInt(Node* input);
195 Node* BuildF64Trunc(Node* input);
196 Node* BuildF64Floor(Node* input);
197 Node* BuildF64Ceil(Node* input);
198 Node* BuildF64NearestInt(Node* input);
Ben Murdoch3b9bc312016-06-02 14:46:10 +0100199 Node* BuildI32Rol(Node* left, Node* right);
200 Node* BuildI64Rol(Node* left, Node* right);
Ben Murdoch014dc512016-03-22 12:00:34 +0000201
Ben Murdoch3b9bc312016-06-02 14:46:10 +0100202 Node* BuildF64Acos(Node* input);
203 Node* BuildF64Asin(Node* input);
204 Node* BuildF64Atan(Node* input);
205 Node* BuildF64Cos(Node* input);
206 Node* BuildF64Sin(Node* input);
207 Node* BuildF64Tan(Node* input);
208 Node* BuildF64Exp(Node* input);
209 Node* BuildF64Log(Node* input);
210 Node* BuildF64Pow(Node* left, Node* right);
211 Node* BuildF64Atan2(Node* left, Node* right);
212 Node* BuildF64Mod(Node* left, Node* right);
213
214 Node* BuildIntToFloatConversionInstruction(
215 Node* input, ExternalReference ref,
216 MachineRepresentation parameter_representation,
217 const MachineType result_type);
218 Node* BuildF32SConvertI64(Node* input);
219 Node* BuildF32UConvertI64(Node* input);
220 Node* BuildF64SConvertI64(Node* input);
221 Node* BuildF64UConvertI64(Node* input);
222
223 Node* BuildFloatToIntConversionInstruction(
224 Node* input, ExternalReference ref,
225 MachineRepresentation parameter_representation,
226 const MachineType result_type);
227 Node* BuildI64SConvertF32(Node* input);
228 Node* BuildI64UConvertF32(Node* input);
229 Node* BuildI64SConvertF64(Node* input);
230 Node* BuildI64UConvertF64(Node* input);
231
232 Node* BuildI32DivS(Node* left, Node* right);
233 Node* BuildI32RemS(Node* left, Node* right);
234 Node* BuildI32DivU(Node* left, Node* right);
235 Node* BuildI32RemU(Node* left, Node* right);
236
237 Node* BuildI64DivS(Node* left, Node* right);
238 Node* BuildI64RemS(Node* left, Node* right);
239 Node* BuildI64DivU(Node* left, Node* right);
240 Node* BuildI64RemU(Node* left, Node* right);
241 Node* BuildDiv64Call(Node* left, Node* right, ExternalReference ref,
242 MachineType result_type, int trap_zero);
243
244 Node** Realloc(Node** buffer, size_t old_count, size_t new_count) {
245 Node** buf = Buffer(new_count);
246 if (buf != buffer) memcpy(buf, buffer, old_count * sizeof(Node*));
Ben Murdoch014dc512016-03-22 12:00:34 +0000247 return buf;
248 }
249};
250} // namespace compiler
251} // namespace internal
252} // namespace v8
253
254#endif // V8_COMPILER_WASM_COMPILER_H_