Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1)

Side by Side Diff: src/compiler/wasm-compiler.cc

Issue 2275293002: [WASM] Implements catch for the wasm low level exception mechanism. (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Addresses comments. Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/compiler/wasm-compiler.h ('k') | src/frames.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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
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;
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
1727 Node* WasmGraphBuilder::BuildI32DivS(Node* left, Node* right, 1777 Node* WasmGraphBuilder::BuildI32DivS(Node* left, Node* right,
1728 wasm::WasmCodePosition position) { 1778 wasm::WasmCodePosition position) {
1729 MachineOperatorBuilder* m = jsgraph()->machine(); 1779 MachineOperatorBuilder* m = jsgraph()->machine();
1730 trap_->ZeroCheck32(wasm::kTrapDivByZero, right, position); 1780 trap_->ZeroCheck32(wasm::kTrapDivByZero, right, position);
1731 Node* before = *control_; 1781 Node* before = *control_;
1732 Node* denom_is_m1; 1782 Node* denom_is_m1;
1733 Node* denom_is_not_m1; 1783 Node* denom_is_not_m1;
1734 Branch( 1784 Branch(
1735 graph()->NewNode(m->Word32Equal(), right, jsgraph()->Int32Constant(-1)), 1785 graph()->NewNode(m->Word32Equal(), right, jsgraph()->Int32Constant(-1)),
1736 &denom_is_m1, &denom_is_not_m1); 1786 &denom_is_m1, &denom_is_not_m1);
(...skipping 248 matching lines...) Expand 10 before | Expand all | Expand 10 after
1985 2035
1986 CallDescriptor* desc = 2036 CallDescriptor* desc =
1987 Linkage::GetSimplifiedCDescriptor(jsgraph()->zone(), sig); 2037 Linkage::GetSimplifiedCDescriptor(jsgraph()->zone(), sig);
1988 2038
1989 const Operator* op = jsgraph()->common()->Call(desc); 2039 const Operator* op = jsgraph()->common()->Call(desc);
1990 Node* call = graph()->NewNode(op, static_cast<int>(count), args); 2040 Node* call = graph()->NewNode(op, static_cast<int>(count), args);
1991 *effect_ = call; 2041 *effect_ = call;
1992 return call; 2042 return call;
1993 } 2043 }
1994 2044
1995 Node** WasmGraphBuilder::BuildWasmCall(wasm::FunctionSig* sig, Node** args, 2045 Node* WasmGraphBuilder::BuildWasmCall(wasm::FunctionSig* sig, Node** args,
1996 wasm::WasmCodePosition position) { 2046 Node*** rets,
2047 wasm::WasmCodePosition position) {
1997 const size_t params = sig->parameter_count(); 2048 const size_t params = sig->parameter_count();
1998 const size_t extra = 2; // effect and control inputs. 2049 const size_t extra = 2; // effect and control inputs.
1999 const size_t count = 1 + params + extra; 2050 const size_t count = 1 + params + extra;
2000 2051
2001 // Reallocate the buffer to make space for extra inputs. 2052 // Reallocate the buffer to make space for extra inputs.
2002 args = Realloc(args, 1 + params, count); 2053 args = Realloc(args, 1 + params, count);
2003 2054
2004 // Add effect and control inputs. 2055 // Add effect and control inputs.
2005 args[params + 1] = *effect_; 2056 args[params + 1] = *effect_;
2006 args[params + 2] = *control_; 2057 args[params + 2] = *control_;
2007 2058
2008 CallDescriptor* descriptor = 2059 CallDescriptor* descriptor =
2009 wasm::ModuleEnv::GetWasmCallDescriptor(jsgraph()->zone(), sig); 2060 wasm::ModuleEnv::GetWasmCallDescriptor(jsgraph()->zone(), sig);
2010 const Operator* op = jsgraph()->common()->Call(descriptor); 2061 const Operator* op = jsgraph()->common()->Call(descriptor);
2011 Node* call = graph()->NewNode(op, static_cast<int>(count), args); 2062 Node* call = graph()->NewNode(op, static_cast<int>(count), args);
2012 SetSourcePosition(call, position); 2063 SetSourcePosition(call, position);
2013 2064
2014 *effect_ = call; 2065 *effect_ = call;
2015 size_t ret_count = sig->return_count(); 2066 size_t ret_count = sig->return_count();
2016 if (ret_count == 0) return nullptr; // No return value. 2067 if (ret_count == 0) return call; // No return value.
2017 2068
2018 Node** rets = Buffer(ret_count); 2069 *rets = Buffer(ret_count);
2019 if (ret_count == 1) { 2070 if (ret_count == 1) {
2020 // Only a single return value. 2071 // Only a single return value.
2021 rets[0] = call; 2072 (*rets)[0] = call;
2022 } else { 2073 } else {
2023 // Create projections for all return values. 2074 // Create projections for all return values.
2024 for (size_t i = 0; i < ret_count; i++) { 2075 for (size_t i = 0; i < ret_count; i++) {
2025 rets[i] = graph()->NewNode(jsgraph()->common()->Projection(i), call, 2076 (*rets)[i] = graph()->NewNode(jsgraph()->common()->Projection(i), call,
2026 graph()->start()); 2077 graph()->start());
2027 } 2078 }
2028 } 2079 }
2029 return rets; 2080 return call;
2030 } 2081 }
2031 2082
2032 Node** WasmGraphBuilder::CallDirect(uint32_t index, Node** args, 2083 Node* WasmGraphBuilder::CallDirect(uint32_t index, Node** args, Node*** rets,
2033 wasm::WasmCodePosition position) { 2084 wasm::WasmCodePosition position) {
2034 DCHECK_NULL(args[0]); 2085 DCHECK_NULL(args[0]);
2035 2086
2036 // Add code object as constant. 2087 // Add code object as constant.
2037 Handle<Code> code = module_->GetFunctionCode(index); 2088 Handle<Code> code = module_->GetFunctionCode(index);
2038 DCHECK(!code.is_null()); 2089 DCHECK(!code.is_null());
2039 args[0] = HeapConstant(code); 2090 args[0] = HeapConstant(code);
2040 wasm::FunctionSig* sig = module_->GetFunctionSignature(index); 2091 wasm::FunctionSig* sig = module_->GetFunctionSignature(index);
2041 2092
2042 return BuildWasmCall(sig, args, position); 2093 return BuildWasmCall(sig, args, rets, position);
2043 } 2094 }
2044 2095
2045 Node** WasmGraphBuilder::CallIndirect(uint32_t index, Node** args, 2096 Node* WasmGraphBuilder::CallIndirect(uint32_t index, Node** args, Node*** rets,
2046 wasm::WasmCodePosition position) { 2097 wasm::WasmCodePosition position) {
2047 DCHECK_NOT_NULL(args[0]); 2098 DCHECK_NOT_NULL(args[0]);
2048 DCHECK(module_ && module_->instance); 2099 DCHECK(module_ && module_->instance);
2049 2100
2050 MachineOperatorBuilder* machine = jsgraph()->machine(); 2101 MachineOperatorBuilder* machine = jsgraph()->machine();
2051 2102
2052 // Compute the code object by loading it from the function table. 2103 // Compute the code object by loading it from the function table.
2053 Node* key = args[0]; 2104 Node* key = args[0];
2054 2105
2055 // Assume only one table for now. 2106 // Assume only one table for now.
2056 DCHECK_LE(module_->instance->function_tables.size(), 1u); 2107 DCHECK_LE(module_->instance->function_tables.size(), 1u);
2057 // Bounds check the index. 2108 // Bounds check the index.
2058 uint32_t table_size = 2109 uint32_t table_size =
2059 module_->IsValidTable(0) ? module_->GetTable(0)->max_size : 0; 2110 module_->IsValidTable(0) ? module_->GetTable(0)->max_size : 0;
2060 wasm::FunctionSig* sig = module_->GetSignature(index); 2111 wasm::FunctionSig* sig = module_->GetSignature(index);
2061 if (table_size > 0) { 2112 if (table_size > 0) {
2062 // Bounds check against the table size. 2113 // Bounds check against the table size.
2063 Node* size = Uint32Constant(table_size); 2114 Node* size = Uint32Constant(table_size);
2064 Node* in_bounds = graph()->NewNode(machine->Uint32LessThan(), key, size); 2115 Node* in_bounds = graph()->NewNode(machine->Uint32LessThan(), key, size);
2065 trap_->AddTrapIfFalse(wasm::kTrapFuncInvalid, in_bounds, position); 2116 trap_->AddTrapIfFalse(wasm::kTrapFuncInvalid, in_bounds, position);
2066 } else { 2117 } else {
2067 // No function table. Generate a trap and return a constant. 2118 // No function table. Generate a trap and return a constant.
2068 trap_->AddTrapIfFalse(wasm::kTrapFuncInvalid, Int32Constant(0), position); 2119 trap_->AddTrapIfFalse(wasm::kTrapFuncInvalid, Int32Constant(0), position);
2069 Node** rets = Buffer(sig->return_count()); 2120 (*rets) = Buffer(sig->return_count());
2070 for (size_t i = 0; i < sig->return_count(); i++) { 2121 for (size_t i = 0; i < sig->return_count(); i++) {
2071 rets[i] = trap_->GetTrapValue(sig->GetReturn(i)); 2122 (*rets)[i] = trap_->GetTrapValue(sig->GetReturn(i));
2072 } 2123 }
2073 return rets; 2124 return trap_->GetTrapValue(sig);
2074 } 2125 }
2075 Node* table = FunctionTable(0); 2126 Node* table = FunctionTable(0);
2076 2127
2077 // Load signature from the table and check. 2128 // Load signature from the table and check.
2078 // The table is a FixedArray; signatures are encoded as SMIs. 2129 // The table is a FixedArray; signatures are encoded as SMIs.
2079 // [sig1, sig2, sig3, ...., code1, code2, code3 ...] 2130 // [sig1, sig2, sig3, ...., code1, code2, code3 ...]
2080 ElementAccess access = AccessBuilder::ForFixedArrayElement(); 2131 ElementAccess access = AccessBuilder::ForFixedArrayElement();
2081 const int fixed_offset = access.header_size - access.tag(); 2132 const int fixed_offset = access.header_size - access.tag();
2082 { 2133 {
2083 Node* load_sig = graph()->NewNode( 2134 Node* load_sig = graph()->NewNode(
(...skipping 13 matching lines...) Expand all
2097 uint32_t offset = fixed_offset + kPointerSize * table_size; 2148 uint32_t offset = fixed_offset + kPointerSize * table_size;
2098 Node* load_code = graph()->NewNode( 2149 Node* load_code = graph()->NewNode(
2099 machine->Load(MachineType::AnyTagged()), table, 2150 machine->Load(MachineType::AnyTagged()), table,
2100 graph()->NewNode(machine->Int32Add(), 2151 graph()->NewNode(machine->Int32Add(),
2101 graph()->NewNode(machine->Word32Shl(), key, 2152 graph()->NewNode(machine->Word32Shl(), key,
2102 Int32Constant(kPointerSizeLog2)), 2153 Int32Constant(kPointerSizeLog2)),
2103 Uint32Constant(offset)), 2154 Uint32Constant(offset)),
2104 *effect_, *control_); 2155 *effect_, *control_);
2105 2156
2106 args[0] = load_code; 2157 args[0] = load_code;
2107 return BuildWasmCall(sig, args, position); 2158 return BuildWasmCall(sig, args, rets, position);
2108 } 2159 }
2109 2160
2110 Node* WasmGraphBuilder::BuildI32Rol(Node* left, Node* right) { 2161 Node* WasmGraphBuilder::BuildI32Rol(Node* left, Node* right) {
2111 // Implement Rol by Ror since TurboFan does not have Rol opcode. 2162 // Implement Rol by Ror since TurboFan does not have Rol opcode.
2112 // TODO(weiliang): support Word32Rol opcode in TurboFan. 2163 // TODO(weiliang): support Word32Rol opcode in TurboFan.
2113 Int32Matcher m(right); 2164 Int32Matcher m(right);
2114 if (m.HasValue()) { 2165 if (m.HasValue()) {
2115 return Binop(wasm::kExprI32Ror, left, 2166 return Binop(wasm::kExprI32Ror, left,
2116 jsgraph()->Int32Constant(32 - m.Value())); 2167 jsgraph()->Int32Constant(32 - m.Value()));
2117 } else { 2168 } else {
(...skipping 1207 matching lines...) Expand 10 before | Expand all | Expand 10 after
3325 function_->code_start_offset), 3376 function_->code_start_offset),
3326 compile_ms); 3377 compile_ms);
3327 } 3378 }
3328 3379
3329 return code; 3380 return code;
3330 } 3381 }
3331 3382
3332 } // namespace compiler 3383 } // namespace compiler
3333 } // namespace internal 3384 } // namespace internal
3334 } // namespace v8 3385 } // namespace v8
OLDNEW
« no previous file with comments | « src/compiler/wasm-compiler.h ('k') | src/frames.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698