Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1 | // 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 | |
| 13 | namespace v8 { |
| 14 | namespace internal { |
| 15 | |
| 16 | namespace compiler { |
| 17 | // Forward declarations for some compiler data structures. |
| 18 | class Node; |
| 19 | class JSGraph; |
| 20 | class Graph; |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 21 | class Operator; |
| 22 | class SourcePositionTable; |
| 23 | class WasmCompilationUnit; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 24 | } |
| 25 | |
| 26 | namespace wasm { |
| 27 | // Forward declarations for some WASM data structures. |
| 28 | struct ModuleEnv; |
| 29 | struct WasmFunction; |
| 30 | class ErrorThrower; |
| 31 | |
| 32 | // Expose {Node} and {Graph} opaquely as {wasm::TFNode} and {wasm::TFGraph}. |
| 33 | typedef compiler::Node TFNode; |
| 34 | typedef compiler::JSGraph TFGraph; |
| 35 | } |
| 36 | |
| 37 | namespace compiler { |
| 38 | // Compiles a single function, producing a code object. |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 39 | Handle<Code> CompileWasmFunction(wasm::ErrorThrower* thrower, Isolate* isolate, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 40 | wasm::ModuleEnv* module_env, |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 41 | const wasm::WasmFunction* function); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 42 | |
| 43 | // Wraps a JS function, producing a code object that can be called from WASM. |
| 44 | Handle<Code> CompileWasmToJSWrapper(Isolate* isolate, wasm::ModuleEnv* module, |
| 45 | Handle<JSFunction> function, |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 46 | wasm::FunctionSig* sig, |
| 47 | wasm::WasmName module_name, |
| 48 | wasm::WasmName function_name); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 49 | |
| 50 | // Wraps a given wasm code object, producing a JSFunction that can be called |
| 51 | // from JavaScript. |
| 52 | Handle<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 Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 56 | WasmCompilationUnit* CreateWasmCompilationUnit( |
| 57 | wasm::ErrorThrower* thrower, Isolate* isolate, wasm::ModuleEnv* module_env, |
| 58 | const wasm::WasmFunction* function, uint32_t index); |
| 59 | |
| 60 | void ExecuteCompilation(WasmCompilationUnit* unit); |
| 61 | |
| 62 | Handle<Code> FinishCompilation(WasmCompilationUnit* unit); |
| 63 | |
| 64 | uint32_t GetIndexOfWasmCompilationUnit(WasmCompilationUnit* unit); |
| 65 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 66 | // Abstracts details of building TurboFan graph nodes for WASM to separate |
| 67 | // the WASM decoder from the internal details of TurboFan. |
| 68 | class WasmTrapHelper; |
| 69 | class WasmGraphBuilder { |
| 70 | public: |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 71 | WasmGraphBuilder( |
| 72 | Zone* z, JSGraph* g, wasm::FunctionSig* function_signature, |
| 73 | compiler::SourcePositionTable* source_position_table = nullptr); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 74 | |
| 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 Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 96 | Node* NumberConstant(int32_t value); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 97 | Node* Int32Constant(int32_t value); |
| 98 | Node* Int64Constant(int64_t value); |
| 99 | Node* Float32Constant(float value); |
| 100 | Node* Float64Constant(double value); |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 101 | 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 Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 106 | unsigned InputCount(Node* node); |
| 107 | bool IsPhiWithMerge(Node* phi, Node* merge); |
| 108 | void AppendToMerge(Node* merge, Node* from); |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 109 | void AppendToPhi(Node* phi, Node* from); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 110 | |
| 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 Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 120 | Node* Unreachable(wasm::WasmCodePosition position); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 121 | |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 122 | 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 Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 128 | void BuildJSToWasmWrapper(Handle<Code> wasm_code, wasm::FunctionSig* sig); |
| 129 | void BuildWasmToJSWrapper(Handle<JSFunction> function, |
| 130 | wasm::FunctionSig* sig); |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 131 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 132 | 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 Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 144 | uint32_t offset, wasm::WasmCodePosition position); |
| 145 | Node* StoreMem(MachineType type, Node* index, uint32_t offset, Node* val, |
| 146 | wasm::WasmCodePosition position); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 147 | |
| 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 Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 161 | void Int64LoweringForTesting(); |
| 162 | |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 163 | void SetSourcePosition(Node* node, wasm::WasmCodePosition position); |
| 164 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 165 | 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 Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 183 | SetOncePointer<const Operator> allocate_heap_number_operator_; |
| 184 | |
| 185 | compiler::SourcePositionTable* source_position_table_ = nullptr; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 186 | |
| 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 Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 193 | void BoundsCheckMem(MachineType memtype, Node* index, uint32_t offset, |
| 194 | wasm::WasmCodePosition position); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 195 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 196 | Node* MaskShiftCount32(Node* node); |
| 197 | Node* MaskShiftCount64(Node* node); |
| 198 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 199 | Node* BuildCCall(MachineSignature* sig, Node** args); |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 200 | Node* BuildWasmCall(wasm::FunctionSig* sig, Node** args, |
| 201 | wasm::WasmCodePosition position); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 202 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 203 | 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 Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 211 | 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 Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 215 | Node* BuildI32Ctz(Node* input); |
| 216 | Node* BuildI32Popcnt(Node* input); |
| 217 | Node* BuildI64Ctz(Node* input); |
| 218 | Node* BuildI64Popcnt(Node* input); |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 219 | Node* BuildBitCountingCall(Node* input, ExternalReference ref, |
| 220 | MachineRepresentation input_type); |
| 221 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 222 | Node* BuildCFuncInstruction(ExternalReference ref, MachineType type, |
| 223 | Node* input0, Node* input1 = nullptr); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 224 | 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 Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 232 | Node* BuildI32Rol(Node* left, Node* right); |
| 233 | Node* BuildI64Rol(Node* left, Node* right); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 234 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 235 | 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 Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 259 | 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 Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 264 | |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 265 | 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 Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 269 | |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 270 | 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 Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 274 | Node* BuildDiv64Call(Node* left, Node* right, ExternalReference ref, |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 275 | 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 Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 305 | |
| 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 Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 309 | return buf; |
| 310 | } |
| 311 | }; |
| 312 | } // namespace compiler |
| 313 | } // namespace internal |
| 314 | } // namespace v8 |
| 315 | |
| 316 | #endif // V8_COMPILER_WASM_COMPILER_H_ |