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