| 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 319 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 330 | 330 |
| 331 unsigned WasmGraphBuilder::InputCount(Node* node) { | 331 unsigned WasmGraphBuilder::InputCount(Node* node) { |
| 332 return static_cast<unsigned>(node->InputCount()); | 332 return static_cast<unsigned>(node->InputCount()); |
| 333 } | 333 } |
| 334 | 334 |
| 335 bool WasmGraphBuilder::IsPhiWithMerge(Node* phi, Node* merge) { | 335 bool WasmGraphBuilder::IsPhiWithMerge(Node* phi, Node* merge) { |
| 336 return phi && IrOpcode::IsPhiOpcode(phi->opcode()) && | 336 return phi && IrOpcode::IsPhiOpcode(phi->opcode()) && |
| 337 NodeProperties::GetControlInput(phi) == merge; | 337 NodeProperties::GetControlInput(phi) == merge; |
| 338 } | 338 } |
| 339 | 339 |
| 340 bool WasmGraphBuilder::DoesNotThrow(Node* node) { |
| 341 return node->op()->HasProperty(compiler::Operator::kNoThrow); |
| 342 } |
| 343 |
| 340 void WasmGraphBuilder::AppendToMerge(Node* merge, Node* from) { | 344 void WasmGraphBuilder::AppendToMerge(Node* merge, Node* from) { |
| 341 DCHECK(IrOpcode::IsMergeOpcode(merge->opcode())); | 345 DCHECK(IrOpcode::IsMergeOpcode(merge->opcode())); |
| 342 merge->AppendInput(jsgraph()->zone(), from); | 346 merge->AppendInput(jsgraph()->zone(), from); |
| 343 int new_size = merge->InputCount(); | 347 int new_size = merge->InputCount(); |
| 344 NodeProperties::ChangeOp( | 348 NodeProperties::ChangeOp( |
| 345 merge, jsgraph()->common()->ResizeMergeOrPhi(merge->op(), new_size)); | 349 merge, jsgraph()->common()->ResizeMergeOrPhi(merge->op(), new_size)); |
| 346 } | 350 } |
| 347 | 351 |
| 348 void WasmGraphBuilder::AppendToPhi(Node* phi, Node* from) { | 352 void WasmGraphBuilder::AppendToPhi(Node* phi, Node* from) { |
| 349 DCHECK(IrOpcode::IsPhiOpcode(phi->opcode())); | 353 DCHECK(IrOpcode::IsPhiOpcode(phi->opcode())); |
| (...skipping 1367 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1717 graph()->NewNode(machine->Word32Shr(), input, Int32Constant(16))); | 1721 graph()->NewNode(machine->Word32Shr(), input, Int32Constant(16))); |
| 1718 Node* lower = BuildChangeInt32ToSmi( | 1722 Node* lower = BuildChangeInt32ToSmi( |
| 1719 graph()->NewNode(machine->Word32And(), input, Int32Constant(0xFFFFu))); | 1723 graph()->NewNode(machine->Word32And(), input, Int32Constant(0xFFFFu))); |
| 1720 | 1724 |
| 1721 Node* parameters[] = {lower, upper}; // thrown value | 1725 Node* parameters[] = {lower, upper}; // thrown value |
| 1722 return BuildCallToRuntime(Runtime::kWasmThrow, jsgraph(), | 1726 return BuildCallToRuntime(Runtime::kWasmThrow, jsgraph(), |
| 1723 module_->instance->context, parameters, | 1727 module_->instance->context, parameters, |
| 1724 arraysize(parameters), effect_, *control_); | 1728 arraysize(parameters), effect_, *control_); |
| 1725 } | 1729 } |
| 1726 | 1730 |
| 1731 Node* WasmGraphBuilder::Catch(Node* input, wasm::WasmCodePosition position) { |
| 1732 CommonOperatorBuilder* common = jsgraph()->common(); |
| 1733 |
| 1734 Node* parameters[] = {input}; // caught value |
| 1735 Node* value = BuildCallToRuntime(Runtime::kWasmCatch, jsgraph(), |
| 1736 module_->instance->context, parameters, |
| 1737 arraysize(parameters), effect_, *control_); |
| 1738 |
| 1739 Node* is_smi; |
| 1740 Node* is_heap; |
| 1741 Branch(BuildTestNotSmi(value), &is_heap, &is_smi); |
| 1742 |
| 1743 // is_heap |
| 1744 Node* heap_f64 = BuildLoadHeapNumberValue(value, is_heap); |
| 1745 // *control_ needs to point to the current control dependency (is_heap) in |
| 1746 // case BuildI32SConvertF64 needs to insert nodes that depend on the "current" |
| 1747 // control node. |
| 1748 *control_ = is_heap; |
| 1749 Node* heap_i32 = BuildI32SConvertF64(heap_f64, position); |
| 1750 // *control_ contains the control node that should be used when merging the |
| 1751 // result for the catch clause. It may be different than *control_ because |
| 1752 // BuildI32SConvertF64 may introduce a new control node (used for trapping if |
| 1753 // heap_f64 cannot be converted to an i32. |
| 1754 is_heap = *control_; |
| 1755 |
| 1756 // is_smi |
| 1757 Node* smi_i32 = BuildChangeSmiToInt32(value); |
| 1758 |
| 1759 Node* merge = graph()->NewNode(common->Merge(2), is_heap, is_smi); |
| 1760 Node* value_i32 = graph()->NewNode( |
| 1761 common->Phi(MachineRepresentation::kWord32, 2), heap_i32, smi_i32, merge); |
| 1762 |
| 1763 *control_ = merge; |
| 1764 return value_i32; |
| 1765 } |
| 1766 |
| 1767 Node* WasmGraphBuilder::IfSuccess(Node* node) { |
| 1768 return graph()->NewNode(jsgraph()->common()->IfSuccess(), node); |
| 1769 } |
| 1770 |
| 1771 Node* WasmGraphBuilder::IfException(Node* node) { |
| 1772 return graph()->NewNode(jsgraph()->common()->IfException(), node, node); |
| 1773 } |
| 1774 |
| 1727 Node* WasmGraphBuilder::BuildI32DivS(Node* left, Node* right, | 1775 Node* WasmGraphBuilder::BuildI32DivS(Node* left, Node* right, |
| 1728 wasm::WasmCodePosition position) { | 1776 wasm::WasmCodePosition position) { |
| 1729 MachineOperatorBuilder* m = jsgraph()->machine(); | 1777 MachineOperatorBuilder* m = jsgraph()->machine(); |
| 1730 trap_->ZeroCheck32(wasm::kTrapDivByZero, right, position); | 1778 trap_->ZeroCheck32(wasm::kTrapDivByZero, right, position); |
| 1731 Node* before = *control_; | 1779 Node* before = *control_; |
| 1732 Node* denom_is_m1; | 1780 Node* denom_is_m1; |
| 1733 Node* denom_is_not_m1; | 1781 Node* denom_is_not_m1; |
| 1734 Branch( | 1782 Branch( |
| 1735 graph()->NewNode(m->Word32Equal(), right, jsgraph()->Int32Constant(-1)), | 1783 graph()->NewNode(m->Word32Equal(), right, jsgraph()->Int32Constant(-1)), |
| 1736 &denom_is_m1, &denom_is_not_m1); | 1784 &denom_is_m1, &denom_is_not_m1); |
| (...skipping 248 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1985 | 2033 |
| 1986 CallDescriptor* desc = | 2034 CallDescriptor* desc = |
| 1987 Linkage::GetSimplifiedCDescriptor(jsgraph()->zone(), sig); | 2035 Linkage::GetSimplifiedCDescriptor(jsgraph()->zone(), sig); |
| 1988 | 2036 |
| 1989 const Operator* op = jsgraph()->common()->Call(desc); | 2037 const Operator* op = jsgraph()->common()->Call(desc); |
| 1990 Node* call = graph()->NewNode(op, static_cast<int>(count), args); | 2038 Node* call = graph()->NewNode(op, static_cast<int>(count), args); |
| 1991 *effect_ = call; | 2039 *effect_ = call; |
| 1992 return call; | 2040 return call; |
| 1993 } | 2041 } |
| 1994 | 2042 |
| 1995 Node** WasmGraphBuilder::BuildWasmCall(wasm::FunctionSig* sig, Node** args, | 2043 Node* WasmGraphBuilder::BuildWasmCall(wasm::FunctionSig* sig, Node** args, |
| 1996 wasm::WasmCodePosition position) { | 2044 Node*** rets, |
| 2045 wasm::WasmCodePosition position) { |
| 1997 const size_t params = sig->parameter_count(); | 2046 const size_t params = sig->parameter_count(); |
| 1998 const size_t extra = 2; // effect and control inputs. | 2047 const size_t extra = 2; // effect and control inputs. |
| 1999 const size_t count = 1 + params + extra; | 2048 const size_t count = 1 + params + extra; |
| 2000 | 2049 |
| 2001 // Reallocate the buffer to make space for extra inputs. | 2050 // Reallocate the buffer to make space for extra inputs. |
| 2002 args = Realloc(args, 1 + params, count); | 2051 args = Realloc(args, 1 + params, count); |
| 2003 | 2052 |
| 2004 // Add effect and control inputs. | 2053 // Add effect and control inputs. |
| 2005 args[params + 1] = *effect_; | 2054 args[params + 1] = *effect_; |
| 2006 args[params + 2] = *control_; | 2055 args[params + 2] = *control_; |
| 2007 | 2056 |
| 2008 CallDescriptor* descriptor = | 2057 CallDescriptor* descriptor = |
| 2009 wasm::ModuleEnv::GetWasmCallDescriptor(jsgraph()->zone(), sig); | 2058 wasm::ModuleEnv::GetWasmCallDescriptor(jsgraph()->zone(), sig); |
| 2010 const Operator* op = jsgraph()->common()->Call(descriptor); | 2059 const Operator* op = jsgraph()->common()->Call(descriptor); |
| 2011 Node* call = graph()->NewNode(op, static_cast<int>(count), args); | 2060 Node* call = graph()->NewNode(op, static_cast<int>(count), args); |
| 2012 SetSourcePosition(call, position); | 2061 SetSourcePosition(call, position); |
| 2013 | 2062 |
| 2014 *effect_ = call; | 2063 *effect_ = call; |
| 2015 size_t ret_count = sig->return_count(); | 2064 size_t ret_count = sig->return_count(); |
| 2016 if (ret_count == 0) return nullptr; // No return value. | 2065 if (ret_count == 0) return call; // No return value. |
| 2017 | 2066 |
| 2018 Node** rets = Buffer(ret_count); | 2067 *rets = Buffer(ret_count); |
| 2019 if (ret_count == 1) { | 2068 if (ret_count == 1) { |
| 2020 // Only a single return value. | 2069 // Only a single return value. |
| 2021 rets[0] = call; | 2070 (*rets)[0] = call; |
| 2022 } else { | 2071 } else { |
| 2023 // Create projections for all return values. | 2072 // Create projections for all return values. |
| 2024 for (size_t i = 0; i < ret_count; i++) { | 2073 for (size_t i = 0; i < ret_count; i++) { |
| 2025 rets[i] = graph()->NewNode(jsgraph()->common()->Projection(i), call, | 2074 (*rets)[i] = graph()->NewNode(jsgraph()->common()->Projection(i), call, |
| 2026 graph()->start()); | 2075 graph()->start()); |
| 2027 } | 2076 } |
| 2028 } | 2077 } |
| 2029 return rets; | 2078 return call; |
| 2030 } | 2079 } |
| 2031 | 2080 |
| 2032 Node** WasmGraphBuilder::CallDirect(uint32_t index, Node** args, | 2081 Node* WasmGraphBuilder::CallDirect(uint32_t index, Node** args, Node*** rets, |
| 2033 wasm::WasmCodePosition position) { | 2082 wasm::WasmCodePosition position) { |
| 2034 DCHECK_NULL(args[0]); | 2083 DCHECK_NULL(args[0]); |
| 2035 | 2084 |
| 2036 // Add code object as constant. | 2085 // Add code object as constant. |
| 2037 Handle<Code> code = module_->GetFunctionCode(index); | 2086 Handle<Code> code = module_->GetFunctionCode(index); |
| 2038 DCHECK(!code.is_null()); | 2087 DCHECK(!code.is_null()); |
| 2039 args[0] = HeapConstant(code); | 2088 args[0] = HeapConstant(code); |
| 2040 wasm::FunctionSig* sig = module_->GetFunctionSignature(index); | 2089 wasm::FunctionSig* sig = module_->GetFunctionSignature(index); |
| 2041 | 2090 |
| 2042 return BuildWasmCall(sig, args, position); | 2091 return BuildWasmCall(sig, args, rets, position); |
| 2043 } | 2092 } |
| 2044 | 2093 |
| 2045 Node** WasmGraphBuilder::CallIndirect(uint32_t index, Node** args, | 2094 Node* WasmGraphBuilder::CallIndirect(uint32_t index, Node** args, Node*** rets, |
| 2046 wasm::WasmCodePosition position) { | 2095 wasm::WasmCodePosition position) { |
| 2047 DCHECK_NOT_NULL(args[0]); | 2096 DCHECK_NOT_NULL(args[0]); |
| 2048 DCHECK(module_ && module_->instance); | 2097 DCHECK(module_ && module_->instance); |
| 2049 | 2098 |
| 2050 MachineOperatorBuilder* machine = jsgraph()->machine(); | 2099 MachineOperatorBuilder* machine = jsgraph()->machine(); |
| 2051 | 2100 |
| 2052 // Compute the code object by loading it from the function table. | 2101 // Compute the code object by loading it from the function table. |
| 2053 Node* key = args[0]; | 2102 Node* key = args[0]; |
| 2054 | 2103 |
| 2055 // Assume only one table for now. | 2104 // Assume only one table for now. |
| 2056 DCHECK_LE(module_->instance->function_tables.size(), 1u); | 2105 DCHECK_LE(module_->instance->function_tables.size(), 1u); |
| 2057 // Bounds check the index. | 2106 // Bounds check the index. |
| 2058 uint32_t table_size = | 2107 uint32_t table_size = |
| 2059 module_->IsValidTable(0) ? module_->GetTable(0)->max_size : 0; | 2108 module_->IsValidTable(0) ? module_->GetTable(0)->max_size : 0; |
| 2060 wasm::FunctionSig* sig = module_->GetSignature(index); | 2109 wasm::FunctionSig* sig = module_->GetSignature(index); |
| 2061 if (table_size > 0) { | 2110 if (table_size > 0) { |
| 2062 // Bounds check against the table size. | 2111 // Bounds check against the table size. |
| 2063 Node* size = Uint32Constant(table_size); | 2112 Node* size = Uint32Constant(table_size); |
| 2064 Node* in_bounds = graph()->NewNode(machine->Uint32LessThan(), key, size); | 2113 Node* in_bounds = graph()->NewNode(machine->Uint32LessThan(), key, size); |
| 2065 trap_->AddTrapIfFalse(wasm::kTrapFuncInvalid, in_bounds, position); | 2114 trap_->AddTrapIfFalse(wasm::kTrapFuncInvalid, in_bounds, position); |
| 2066 } else { | 2115 } else { |
| 2067 // No function table. Generate a trap and return a constant. | 2116 // No function table. Generate a trap and return a constant. |
| 2068 trap_->AddTrapIfFalse(wasm::kTrapFuncInvalid, Int32Constant(0), position); | 2117 trap_->AddTrapIfFalse(wasm::kTrapFuncInvalid, Int32Constant(0), position); |
| 2069 Node** rets = Buffer(sig->return_count()); | 2118 (*rets) = Buffer(sig->return_count()); |
| 2070 for (size_t i = 0; i < sig->return_count(); i++) { | 2119 for (size_t i = 0; i < sig->return_count(); i++) { |
| 2071 rets[i] = trap_->GetTrapValue(sig->GetReturn(i)); | 2120 (*rets)[i] = trap_->GetTrapValue(sig->GetReturn(i)); |
| 2072 } | 2121 } |
| 2073 return rets; | 2122 return trap_->GetTrapValue(sig); |
| 2074 } | 2123 } |
| 2075 Node* table = FunctionTable(0); | 2124 Node* table = FunctionTable(0); |
| 2076 | 2125 |
| 2077 // Load signature from the table and check. | 2126 // Load signature from the table and check. |
| 2078 // The table is a FixedArray; signatures are encoded as SMIs. | 2127 // The table is a FixedArray; signatures are encoded as SMIs. |
| 2079 // [sig1, sig2, sig3, ...., code1, code2, code3 ...] | 2128 // [sig1, sig2, sig3, ...., code1, code2, code3 ...] |
| 2080 ElementAccess access = AccessBuilder::ForFixedArrayElement(); | 2129 ElementAccess access = AccessBuilder::ForFixedArrayElement(); |
| 2081 const int fixed_offset = access.header_size - access.tag(); | 2130 const int fixed_offset = access.header_size - access.tag(); |
| 2082 { | 2131 { |
| 2083 Node* load_sig = graph()->NewNode( | 2132 Node* load_sig = graph()->NewNode( |
| (...skipping 13 matching lines...) Expand all Loading... |
| 2097 uint32_t offset = fixed_offset + kPointerSize * table_size; | 2146 uint32_t offset = fixed_offset + kPointerSize * table_size; |
| 2098 Node* load_code = graph()->NewNode( | 2147 Node* load_code = graph()->NewNode( |
| 2099 machine->Load(MachineType::AnyTagged()), table, | 2148 machine->Load(MachineType::AnyTagged()), table, |
| 2100 graph()->NewNode(machine->Int32Add(), | 2149 graph()->NewNode(machine->Int32Add(), |
| 2101 graph()->NewNode(machine->Word32Shl(), key, | 2150 graph()->NewNode(machine->Word32Shl(), key, |
| 2102 Int32Constant(kPointerSizeLog2)), | 2151 Int32Constant(kPointerSizeLog2)), |
| 2103 Uint32Constant(offset)), | 2152 Uint32Constant(offset)), |
| 2104 *effect_, *control_); | 2153 *effect_, *control_); |
| 2105 | 2154 |
| 2106 args[0] = load_code; | 2155 args[0] = load_code; |
| 2107 return BuildWasmCall(sig, args, position); | 2156 return BuildWasmCall(sig, args, rets, position); |
| 2108 } | 2157 } |
| 2109 | 2158 |
| 2110 Node* WasmGraphBuilder::BuildI32Rol(Node* left, Node* right) { | 2159 Node* WasmGraphBuilder::BuildI32Rol(Node* left, Node* right) { |
| 2111 // Implement Rol by Ror since TurboFan does not have Rol opcode. | 2160 // Implement Rol by Ror since TurboFan does not have Rol opcode. |
| 2112 // TODO(weiliang): support Word32Rol opcode in TurboFan. | 2161 // TODO(weiliang): support Word32Rol opcode in TurboFan. |
| 2113 Int32Matcher m(right); | 2162 Int32Matcher m(right); |
| 2114 if (m.HasValue()) { | 2163 if (m.HasValue()) { |
| 2115 return Binop(wasm::kExprI32Ror, left, | 2164 return Binop(wasm::kExprI32Ror, left, |
| 2116 jsgraph()->Int32Constant(32 - m.Value())); | 2165 jsgraph()->Int32Constant(32 - m.Value())); |
| 2117 } else { | 2166 } else { |
| (...skipping 1192 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3310 function_->code_start_offset), | 3359 function_->code_start_offset), |
| 3311 compile_ms); | 3360 compile_ms); |
| 3312 } | 3361 } |
| 3313 | 3362 |
| 3314 return code; | 3363 return code; |
| 3315 } | 3364 } |
| 3316 | 3365 |
| 3317 } // namespace compiler | 3366 } // namespace compiler |
| 3318 } // namespace internal | 3367 } // namespace internal |
| 3319 } // namespace v8 | 3368 } // namespace v8 |
| OLD | NEW |