OLD | NEW |
1 // Copyright 2015 the V8 project authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "src/compiler/wasm-compiler.h" | 5 #include "src/compiler/wasm-compiler.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 | 8 |
9 #include "src/isolate-inl.h" | 9 #include "src/isolate-inl.h" |
10 | 10 |
(...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
182 Node* if_false = graph()->NewNode(common()->IfFalse(), branch); | 182 Node* if_false = graph()->NewNode(common()->IfFalse(), branch); |
183 | 183 |
184 *control_ptr = iftrue ? if_true : if_false; | 184 *control_ptr = iftrue ? if_true : if_false; |
185 ConnectTrap(reason, position); | 185 ConnectTrap(reason, position); |
186 *control_ptr = iftrue ? if_false : if_true; | 186 *control_ptr = iftrue ? if_false : if_true; |
187 *effect_ptr = before; | 187 *effect_ptr = before; |
188 } | 188 } |
189 | 189 |
190 Node* GetTrapValue(wasm::FunctionSig* sig) { | 190 Node* GetTrapValue(wasm::FunctionSig* sig) { |
191 if (sig->return_count() > 0) { | 191 if (sig->return_count() > 0) { |
192 switch (sig->GetReturn()) { | 192 return GetTrapValue(sig->GetReturn()); |
193 case wasm::kAstI32: | |
194 return jsgraph()->Int32Constant(0xdeadbeef); | |
195 case wasm::kAstI64: | |
196 return jsgraph()->Int64Constant(0xdeadbeefdeadbeef); | |
197 case wasm::kAstF32: | |
198 return jsgraph()->Float32Constant(bit_cast<float>(0xdeadbeef)); | |
199 case wasm::kAstF64: | |
200 return jsgraph()->Float64Constant( | |
201 bit_cast<double>(0xdeadbeefdeadbeef)); | |
202 break; | |
203 default: | |
204 UNREACHABLE(); | |
205 return nullptr; | |
206 } | |
207 } else { | 193 } else { |
208 return jsgraph()->Int32Constant(0xdeadbeef); | 194 return jsgraph()->Int32Constant(0xdeadbeef); |
209 } | 195 } |
210 } | 196 } |
211 | 197 |
| 198 Node* GetTrapValue(wasm::LocalType type) { |
| 199 switch (type) { |
| 200 case wasm::kAstI32: |
| 201 return jsgraph()->Int32Constant(0xdeadbeef); |
| 202 case wasm::kAstI64: |
| 203 return jsgraph()->Int64Constant(0xdeadbeefdeadbeef); |
| 204 case wasm::kAstF32: |
| 205 return jsgraph()->Float32Constant(bit_cast<float>(0xdeadbeef)); |
| 206 case wasm::kAstF64: |
| 207 return jsgraph()->Float64Constant(bit_cast<double>(0xdeadbeefdeadbeef)); |
| 208 break; |
| 209 default: |
| 210 UNREACHABLE(); |
| 211 return nullptr; |
| 212 } |
| 213 } |
| 214 |
212 private: | 215 private: |
213 WasmGraphBuilder* builder_; | 216 WasmGraphBuilder* builder_; |
214 JSGraph* jsgraph_; | 217 JSGraph* jsgraph_; |
215 Graph* graph_; | 218 Graph* graph_; |
216 Node* trap_merge_ = nullptr; | 219 Node* trap_merge_ = nullptr; |
217 Node* trap_effect_; | 220 Node* trap_effect_; |
218 Node* trap_reason_; | 221 Node* trap_reason_; |
219 Node* trap_position_; | 222 Node* trap_position_; |
220 | 223 |
221 JSGraph* jsgraph() { return jsgraph_; } | 224 JSGraph* jsgraph() { return jsgraph_; } |
(...skipping 764 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
986 | 989 |
987 Node* WasmGraphBuilder::IfDefault(Node* sw) { | 990 Node* WasmGraphBuilder::IfDefault(Node* sw) { |
988 DCHECK_EQ(IrOpcode::kSwitch, sw->opcode()); | 991 DCHECK_EQ(IrOpcode::kSwitch, sw->opcode()); |
989 return graph()->NewNode(jsgraph()->common()->IfDefault(), sw); | 992 return graph()->NewNode(jsgraph()->common()->IfDefault(), sw); |
990 } | 993 } |
991 | 994 |
992 Node* WasmGraphBuilder::Return(unsigned count, Node** vals) { | 995 Node* WasmGraphBuilder::Return(unsigned count, Node** vals) { |
993 DCHECK_NOT_NULL(*control_); | 996 DCHECK_NOT_NULL(*control_); |
994 DCHECK_NOT_NULL(*effect_); | 997 DCHECK_NOT_NULL(*effect_); |
995 | 998 |
996 if (count == 0) { | |
997 // Handle a return of void. | |
998 vals[0] = jsgraph()->Int32Constant(0); | |
999 count = 1; | |
1000 } | |
1001 | |
1002 Node** buf = Realloc(vals, count, count + 2); | 999 Node** buf = Realloc(vals, count, count + 2); |
1003 buf[count] = *effect_; | 1000 buf[count] = *effect_; |
1004 buf[count + 1] = *control_; | 1001 buf[count + 1] = *control_; |
1005 Node* ret = graph()->NewNode(jsgraph()->common()->Return(), count + 2, vals); | 1002 Node* ret = |
| 1003 graph()->NewNode(jsgraph()->common()->Return(count), count + 2, vals); |
1006 | 1004 |
1007 MergeControlToEnd(jsgraph(), ret); | 1005 MergeControlToEnd(jsgraph(), ret); |
1008 return ret; | 1006 return ret; |
1009 } | 1007 } |
1010 | 1008 |
1011 Node* WasmGraphBuilder::ReturnVoid() { return Return(0, Buffer(0)); } | 1009 Node* WasmGraphBuilder::ReturnVoid() { return Return(0, Buffer(0)); } |
1012 | 1010 |
1013 Node* WasmGraphBuilder::Unreachable(wasm::WasmCodePosition position) { | 1011 Node* WasmGraphBuilder::Unreachable(wasm::WasmCodePosition position) { |
1014 trap_->Unreachable(position); | 1012 trap_->Unreachable(position); |
1015 return nullptr; | 1013 return nullptr; |
(...skipping 971 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1987 | 1985 |
1988 CallDescriptor* desc = | 1986 CallDescriptor* desc = |
1989 Linkage::GetSimplifiedCDescriptor(jsgraph()->zone(), sig); | 1987 Linkage::GetSimplifiedCDescriptor(jsgraph()->zone(), sig); |
1990 | 1988 |
1991 const Operator* op = jsgraph()->common()->Call(desc); | 1989 const Operator* op = jsgraph()->common()->Call(desc); |
1992 Node* call = graph()->NewNode(op, static_cast<int>(count), args); | 1990 Node* call = graph()->NewNode(op, static_cast<int>(count), args); |
1993 *effect_ = call; | 1991 *effect_ = call; |
1994 return call; | 1992 return call; |
1995 } | 1993 } |
1996 | 1994 |
1997 Node* WasmGraphBuilder::BuildWasmCall(wasm::FunctionSig* sig, Node** args, | 1995 Node** WasmGraphBuilder::BuildWasmCall(wasm::FunctionSig* sig, Node** args, |
1998 wasm::WasmCodePosition position) { | 1996 wasm::WasmCodePosition position) { |
1999 const size_t params = sig->parameter_count(); | 1997 const size_t params = sig->parameter_count(); |
2000 const size_t extra = 2; // effect and control inputs. | 1998 const size_t extra = 2; // effect and control inputs. |
2001 const size_t count = 1 + params + extra; | 1999 const size_t count = 1 + params + extra; |
2002 | 2000 |
2003 // Reallocate the buffer to make space for extra inputs. | 2001 // Reallocate the buffer to make space for extra inputs. |
2004 args = Realloc(args, 1 + params, count); | 2002 args = Realloc(args, 1 + params, count); |
2005 | 2003 |
2006 // Add effect and control inputs. | 2004 // Add effect and control inputs. |
2007 args[params + 1] = *effect_; | 2005 args[params + 1] = *effect_; |
2008 args[params + 2] = *control_; | 2006 args[params + 2] = *control_; |
2009 | 2007 |
2010 CallDescriptor* descriptor = | 2008 CallDescriptor* descriptor = |
2011 wasm::ModuleEnv::GetWasmCallDescriptor(jsgraph()->zone(), sig); | 2009 wasm::ModuleEnv::GetWasmCallDescriptor(jsgraph()->zone(), sig); |
2012 const Operator* op = jsgraph()->common()->Call(descriptor); | 2010 const Operator* op = jsgraph()->common()->Call(descriptor); |
2013 Node* call = graph()->NewNode(op, static_cast<int>(count), args); | 2011 Node* call = graph()->NewNode(op, static_cast<int>(count), args); |
2014 SetSourcePosition(call, position); | 2012 SetSourcePosition(call, position); |
2015 | 2013 |
2016 *effect_ = call; | 2014 *effect_ = call; |
2017 return call; | 2015 size_t ret_count = sig->return_count(); |
| 2016 if (ret_count == 0) return nullptr; // No return value. |
| 2017 |
| 2018 Node** rets = Buffer(ret_count); |
| 2019 if (ret_count == 1) { |
| 2020 // Only a single return value. |
| 2021 rets[0] = call; |
| 2022 } else { |
| 2023 // Create projections for all return values. |
| 2024 for (size_t i = 0; i < ret_count; i++) { |
| 2025 rets[i] = graph()->NewNode(jsgraph()->common()->Projection(i), call, |
| 2026 graph()->start()); |
| 2027 } |
| 2028 } |
| 2029 return rets; |
2018 } | 2030 } |
2019 | 2031 |
2020 Node* WasmGraphBuilder::CallDirect(uint32_t index, Node** args, | 2032 Node** WasmGraphBuilder::CallDirect(uint32_t index, Node** args, |
2021 wasm::WasmCodePosition position) { | 2033 wasm::WasmCodePosition position) { |
2022 DCHECK_NULL(args[0]); | 2034 DCHECK_NULL(args[0]); |
2023 | 2035 |
2024 // Add code object as constant. | 2036 // Add code object as constant. |
2025 args[0] = HeapConstant(module_->GetCodeOrPlaceholder(index)); | 2037 Handle<Code> code = module_->GetFunctionCode(index); |
| 2038 DCHECK(!code.is_null()); |
| 2039 args[0] = HeapConstant(code); |
2026 wasm::FunctionSig* sig = module_->GetFunctionSignature(index); | 2040 wasm::FunctionSig* sig = module_->GetFunctionSignature(index); |
2027 | 2041 |
2028 return BuildWasmCall(sig, args, position); | 2042 return BuildWasmCall(sig, args, position); |
2029 } | 2043 } |
2030 | 2044 |
2031 Node* WasmGraphBuilder::CallImport(uint32_t index, Node** args, | 2045 Node** WasmGraphBuilder::CallIndirect(uint32_t index, Node** args, |
2032 wasm::WasmCodePosition position) { | 2046 wasm::WasmCodePosition position) { |
2033 DCHECK_NULL(args[0]); | |
2034 | |
2035 // Add code object as constant. | |
2036 args[0] = HeapConstant(module_->GetImportCode(index)); | |
2037 wasm::FunctionSig* sig = module_->GetImportSignature(index); | |
2038 | |
2039 return BuildWasmCall(sig, args, position); | |
2040 } | |
2041 | |
2042 Node* WasmGraphBuilder::CallIndirect(uint32_t index, Node** args, | |
2043 wasm::WasmCodePosition position) { | |
2044 DCHECK_NOT_NULL(args[0]); | 2047 DCHECK_NOT_NULL(args[0]); |
2045 DCHECK(module_ && module_->instance); | 2048 DCHECK(module_ && module_->instance); |
2046 | 2049 |
2047 MachineOperatorBuilder* machine = jsgraph()->machine(); | 2050 MachineOperatorBuilder* machine = jsgraph()->machine(); |
2048 | 2051 |
2049 // Compute the code object by loading it from the function table. | 2052 // Compute the code object by loading it from the function table. |
2050 Node* key = args[0]; | 2053 Node* key = args[0]; |
2051 | 2054 |
2052 // Assume only one table for now. | 2055 // Assume only one table for now. |
2053 DCHECK_LE(module_->instance->function_tables.size(), 1u); | 2056 DCHECK_LE(module_->instance->function_tables.size(), 1u); |
2054 // Bounds check the index. | 2057 // Bounds check the index. |
2055 uint32_t table_size = | 2058 uint32_t table_size = |
2056 module_->IsValidTable(0) ? module_->GetTable(0)->max_size : 0; | 2059 module_->IsValidTable(0) ? module_->GetTable(0)->max_size : 0; |
| 2060 wasm::FunctionSig* sig = module_->GetSignature(index); |
2057 if (table_size > 0) { | 2061 if (table_size > 0) { |
2058 // Bounds check against the table size. | 2062 // Bounds check against the table size. |
2059 Node* size = Uint32Constant(table_size); | 2063 Node* size = Uint32Constant(table_size); |
2060 Node* in_bounds = graph()->NewNode(machine->Uint32LessThan(), key, size); | 2064 Node* in_bounds = graph()->NewNode(machine->Uint32LessThan(), key, size); |
2061 trap_->AddTrapIfFalse(wasm::kTrapFuncInvalid, in_bounds, position); | 2065 trap_->AddTrapIfFalse(wasm::kTrapFuncInvalid, in_bounds, position); |
2062 } else { | 2066 } else { |
2063 // No function table. Generate a trap and return a constant. | 2067 // No function table. Generate a trap and return a constant. |
2064 trap_->AddTrapIfFalse(wasm::kTrapFuncInvalid, Int32Constant(0), position); | 2068 trap_->AddTrapIfFalse(wasm::kTrapFuncInvalid, Int32Constant(0), position); |
2065 return trap_->GetTrapValue(module_->GetSignature(index)); | 2069 Node** rets = Buffer(sig->return_count()); |
| 2070 for (size_t i = 0; i < sig->return_count(); i++) { |
| 2071 rets[i] = trap_->GetTrapValue(sig->GetReturn(i)); |
| 2072 } |
| 2073 return rets; |
2066 } | 2074 } |
2067 Node* table = FunctionTable(0); | 2075 Node* table = FunctionTable(0); |
2068 | 2076 |
2069 // Load signature from the table and check. | 2077 // Load signature from the table and check. |
2070 // The table is a FixedArray; signatures are encoded as SMIs. | 2078 // The table is a FixedArray; signatures are encoded as SMIs. |
2071 // [sig1, sig2, sig3, ...., code1, code2, code3 ...] | 2079 // [sig1, sig2, sig3, ...., code1, code2, code3 ...] |
2072 ElementAccess access = AccessBuilder::ForFixedArrayElement(); | 2080 ElementAccess access = AccessBuilder::ForFixedArrayElement(); |
2073 const int fixed_offset = access.header_size - access.tag(); | 2081 const int fixed_offset = access.header_size - access.tag(); |
2074 { | 2082 { |
2075 Node* load_sig = graph()->NewNode( | 2083 Node* load_sig = graph()->NewNode( |
(...skipping 13 matching lines...) Expand all Loading... |
2089 uint32_t offset = fixed_offset + kPointerSize * table_size; | 2097 uint32_t offset = fixed_offset + kPointerSize * table_size; |
2090 Node* load_code = graph()->NewNode( | 2098 Node* load_code = graph()->NewNode( |
2091 machine->Load(MachineType::AnyTagged()), table, | 2099 machine->Load(MachineType::AnyTagged()), table, |
2092 graph()->NewNode(machine->Int32Add(), | 2100 graph()->NewNode(machine->Int32Add(), |
2093 graph()->NewNode(machine->Word32Shl(), key, | 2101 graph()->NewNode(machine->Word32Shl(), key, |
2094 Int32Constant(kPointerSizeLog2)), | 2102 Int32Constant(kPointerSizeLog2)), |
2095 Uint32Constant(offset)), | 2103 Uint32Constant(offset)), |
2096 *effect_, *control_); | 2104 *effect_, *control_); |
2097 | 2105 |
2098 args[0] = load_code; | 2106 args[0] = load_code; |
2099 wasm::FunctionSig* sig = module_->GetSignature(index); | |
2100 return BuildWasmCall(sig, args, position); | 2107 return BuildWasmCall(sig, args, position); |
2101 } | 2108 } |
2102 | 2109 |
2103 Node* WasmGraphBuilder::BuildI32Rol(Node* left, Node* right) { | 2110 Node* WasmGraphBuilder::BuildI32Rol(Node* left, Node* right) { |
2104 // Implement Rol by Ror since TurboFan does not have Rol opcode. | 2111 // Implement Rol by Ror since TurboFan does not have Rol opcode. |
2105 // TODO(weiliang): support Word32Rol opcode in TurboFan. | 2112 // TODO(weiliang): support Word32Rol opcode in TurboFan. |
2106 Int32Matcher m(right); | 2113 Int32Matcher m(right); |
2107 if (m.HasValue()) { | 2114 if (m.HasValue()) { |
2108 return Binop(wasm::kExprI32Ror, left, | 2115 return Binop(wasm::kExprI32Ror, left, |
2109 jsgraph()->Int32Constant(32 - m.Value())); | 2116 jsgraph()->Int32Constant(32 - m.Value())); |
(...skipping 576 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2686 RelocInfo::WASM_MEMORY_REFERENCE); | 2693 RelocInfo::WASM_MEMORY_REFERENCE); |
2687 } | 2694 } |
2688 return mem_buffer_; | 2695 return mem_buffer_; |
2689 } else { | 2696 } else { |
2690 return jsgraph()->RelocatableIntPtrConstant( | 2697 return jsgraph()->RelocatableIntPtrConstant( |
2691 reinterpret_cast<uintptr_t>(module_->instance->mem_start + offset), | 2698 reinterpret_cast<uintptr_t>(module_->instance->mem_start + offset), |
2692 RelocInfo::WASM_MEMORY_REFERENCE); | 2699 RelocInfo::WASM_MEMORY_REFERENCE); |
2693 } | 2700 } |
2694 } | 2701 } |
2695 | 2702 |
| 2703 Node* WasmGraphBuilder::CurrentMemoryPages() { |
| 2704 return graph()->NewNode(jsgraph()->machine()->Word32Shr(), MemSize(0), |
| 2705 jsgraph()->Int32Constant(16)); |
| 2706 } |
| 2707 |
2696 Node* WasmGraphBuilder::MemSize(uint32_t offset) { | 2708 Node* WasmGraphBuilder::MemSize(uint32_t offset) { |
2697 DCHECK(module_ && module_->instance); | 2709 DCHECK(module_ && module_->instance); |
2698 uint32_t size = static_cast<uint32_t>(module_->instance->mem_size); | 2710 uint32_t size = static_cast<uint32_t>(module_->instance->mem_size); |
2699 if (offset == 0) { | 2711 if (offset == 0) { |
2700 if (!mem_size_) | 2712 if (!mem_size_) |
2701 mem_size_ = jsgraph()->RelocatableInt32Constant( | 2713 mem_size_ = jsgraph()->RelocatableInt32Constant( |
2702 size, RelocInfo::WASM_MEMORY_SIZE_REFERENCE); | 2714 size, RelocInfo::WASM_MEMORY_SIZE_REFERENCE); |
2703 return mem_size_; | 2715 return mem_size_; |
2704 } else { | 2716 } else { |
2705 return jsgraph()->RelocatableInt32Constant( | 2717 return jsgraph()->RelocatableInt32Constant( |
(...skipping 592 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3298 function_->code_start_offset), | 3310 function_->code_start_offset), |
3299 compile_ms); | 3311 compile_ms); |
3300 } | 3312 } |
3301 | 3313 |
3302 return code; | 3314 return code; |
3303 } | 3315 } |
3304 | 3316 |
3305 } // namespace compiler | 3317 } // namespace compiler |
3306 } // namespace internal | 3318 } // namespace internal |
3307 } // namespace v8 | 3319 } // namespace v8 |
OLD | NEW |