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