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