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 switch (sig->GetReturn()) { | 192 return GetTrapValue(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 } | |
207 } else { | 193 } else { |
208 return jsgraph()->Int32Constant(0xdeadbeef); | 194 return jsgraph()->Int32Constant(0xdeadbeef); |
209 } | 195 } |
210 } | 196 } |
211 | 197 |
| 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 |
212 private: | 215 private: |
213 WasmGraphBuilder* builder_; | 216 WasmGraphBuilder* builder_; |
214 JSGraph* jsgraph_; | 217 JSGraph* jsgraph_; |
215 Graph* graph_; | 218 Graph* graph_; |
216 Node* trap_merge_ = nullptr; | 219 Node* trap_merge_ = nullptr; |
217 Node* trap_effect_; | 220 Node* trap_effect_; |
218 Node* trap_reason_; | 221 Node* trap_reason_; |
219 Node* trap_position_; | 222 Node* trap_position_; |
220 | 223 |
221 JSGraph* jsgraph() { return jsgraph_; } | 224 JSGraph* jsgraph() { return jsgraph_; } |
(...skipping 764 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
986 | 989 |
987 Node* WasmGraphBuilder::IfDefault(Node* sw) { | 990 Node* WasmGraphBuilder::IfDefault(Node* sw) { |
988 DCHECK_EQ(IrOpcode::kSwitch, sw->opcode()); | 991 DCHECK_EQ(IrOpcode::kSwitch, sw->opcode()); |
989 return graph()->NewNode(jsgraph()->common()->IfDefault(), sw); | 992 return graph()->NewNode(jsgraph()->common()->IfDefault(), sw); |
990 } | 993 } |
991 | 994 |
992 Node* WasmGraphBuilder::Return(unsigned count, Node** vals) { | 995 Node* WasmGraphBuilder::Return(unsigned count, Node** vals) { |
993 DCHECK_NOT_NULL(*control_); | 996 DCHECK_NOT_NULL(*control_); |
994 DCHECK_NOT_NULL(*effect_); | 997 DCHECK_NOT_NULL(*effect_); |
995 | 998 |
996 if (count == 0) { | |
997 // Handle a return of void. | |
998 vals[0] = jsgraph()->Int32Constant(0); | |
999 count = 1; | |
1000 } | |
1001 | |
1002 Node** buf = Realloc(vals, count, count + 2); | 999 Node** buf = Realloc(vals, count, count + 2); |
1003 buf[count] = *effect_; | 1000 buf[count] = *effect_; |
1004 buf[count + 1] = *control_; | 1001 buf[count + 1] = *control_; |
1005 Node* ret = graph()->NewNode(jsgraph()->common()->Return(), count + 2, vals); | 1002 Node* ret = |
| 1003 graph()->NewNode(jsgraph()->common()->Return(count), count + 2, vals); |
1006 | 1004 |
1007 MergeControlToEnd(jsgraph(), ret); | 1005 MergeControlToEnd(jsgraph(), ret); |
1008 return ret; | 1006 return ret; |
1009 } | 1007 } |
1010 | 1008 |
1011 Node* WasmGraphBuilder::ReturnVoid() { return Return(0, Buffer(0)); } | 1009 Node* WasmGraphBuilder::ReturnVoid() { return Return(0, Buffer(0)); } |
1012 | 1010 |
1013 Node* WasmGraphBuilder::Unreachable(wasm::WasmCodePosition position) { | 1011 Node* WasmGraphBuilder::Unreachable(wasm::WasmCodePosition position) { |
1014 trap_->Unreachable(position); | 1012 trap_->Unreachable(position); |
1015 return nullptr; | 1013 return nullptr; |
(...skipping 948 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1964 | 1962 |
1965 CallDescriptor* desc = | 1963 CallDescriptor* desc = |
1966 Linkage::GetSimplifiedCDescriptor(jsgraph()->zone(), sig); | 1964 Linkage::GetSimplifiedCDescriptor(jsgraph()->zone(), sig); |
1967 | 1965 |
1968 const Operator* op = jsgraph()->common()->Call(desc); | 1966 const Operator* op = jsgraph()->common()->Call(desc); |
1969 Node* call = graph()->NewNode(op, static_cast<int>(count), args); | 1967 Node* call = graph()->NewNode(op, static_cast<int>(count), args); |
1970 *effect_ = call; | 1968 *effect_ = call; |
1971 return call; | 1969 return call; |
1972 } | 1970 } |
1973 | 1971 |
1974 Node* WasmGraphBuilder::BuildWasmCall(wasm::FunctionSig* sig, Node** args, | 1972 Node** WasmGraphBuilder::BuildWasmCall(wasm::FunctionSig* sig, Node** args, |
1975 wasm::WasmCodePosition position) { | 1973 wasm::WasmCodePosition position) { |
1976 const size_t params = sig->parameter_count(); | 1974 const size_t params = sig->parameter_count(); |
1977 const size_t extra = 2; // effect and control inputs. | 1975 const size_t extra = 2; // effect and control inputs. |
1978 const size_t count = 1 + params + extra; | 1976 const size_t count = 1 + params + extra; |
1979 | 1977 |
1980 // Reallocate the buffer to make space for extra inputs. | 1978 // Reallocate the buffer to make space for extra inputs. |
1981 args = Realloc(args, 1 + params, count); | 1979 args = Realloc(args, 1 + params, count); |
1982 | 1980 |
1983 // Add effect and control inputs. | 1981 // Add effect and control inputs. |
1984 args[params + 1] = *effect_; | 1982 args[params + 1] = *effect_; |
1985 args[params + 2] = *control_; | 1983 args[params + 2] = *control_; |
1986 | 1984 |
1987 CallDescriptor* descriptor = | 1985 CallDescriptor* descriptor = |
1988 wasm::ModuleEnv::GetWasmCallDescriptor(jsgraph()->zone(), sig); | 1986 wasm::ModuleEnv::GetWasmCallDescriptor(jsgraph()->zone(), sig); |
1989 const Operator* op = jsgraph()->common()->Call(descriptor); | 1987 const Operator* op = jsgraph()->common()->Call(descriptor); |
1990 Node* call = graph()->NewNode(op, static_cast<int>(count), args); | 1988 Node* call = graph()->NewNode(op, static_cast<int>(count), args); |
1991 SetSourcePosition(call, position); | 1989 SetSourcePosition(call, position); |
1992 | 1990 |
1993 *effect_ = call; | 1991 *effect_ = call; |
1994 return call; | 1992 size_t ret_count = sig->return_count(); |
| 1993 if (ret_count == 0) return nullptr; // No return value. |
| 1994 |
| 1995 Node** rets = Buffer(ret_count); |
| 1996 if (ret_count == 1) { |
| 1997 // Only a single return value. |
| 1998 rets[0] = call; |
| 1999 } else { |
| 2000 // Create projections for all return values. |
| 2001 for (size_t i = 0; i < ret_count; i++) { |
| 2002 rets[i] = graph()->NewNode(jsgraph()->common()->Projection(i), call, |
| 2003 graph()->start()); |
| 2004 } |
| 2005 } |
| 2006 return rets; |
1995 } | 2007 } |
1996 | 2008 |
1997 Node* WasmGraphBuilder::CallDirect(uint32_t index, Node** args, | 2009 Node** WasmGraphBuilder::CallDirect(uint32_t index, Node** args, |
1998 wasm::WasmCodePosition position) { | 2010 wasm::WasmCodePosition position) { |
1999 DCHECK_NULL(args[0]); | 2011 DCHECK_NULL(args[0]); |
2000 | 2012 |
2001 // Add code object as constant. | 2013 // Add code object as constant. |
2002 args[0] = HeapConstant(module_->GetCodeOrPlaceholder(index)); | 2014 Handle<Code> code = module_->GetFunctionCode(index); |
| 2015 DCHECK(!code.is_null()); |
| 2016 args[0] = HeapConstant(code); |
2003 wasm::FunctionSig* sig = module_->GetFunctionSignature(index); | 2017 wasm::FunctionSig* sig = module_->GetFunctionSignature(index); |
2004 | 2018 |
2005 return BuildWasmCall(sig, args, position); | 2019 return BuildWasmCall(sig, args, position); |
2006 } | 2020 } |
2007 | 2021 |
2008 Node* WasmGraphBuilder::CallImport(uint32_t index, Node** args, | 2022 Node** WasmGraphBuilder::CallIndirect(uint32_t index, Node** args, |
2009 wasm::WasmCodePosition position) { | 2023 wasm::WasmCodePosition position) { |
2010 DCHECK_NULL(args[0]); | |
2011 | |
2012 // Add code object as constant. | |
2013 args[0] = HeapConstant(module_->GetImportCode(index)); | |
2014 wasm::FunctionSig* sig = module_->GetImportSignature(index); | |
2015 | |
2016 return BuildWasmCall(sig, args, position); | |
2017 } | |
2018 | |
2019 Node* WasmGraphBuilder::CallIndirect(uint32_t index, Node** args, | |
2020 wasm::WasmCodePosition position) { | |
2021 DCHECK_NOT_NULL(args[0]); | 2024 DCHECK_NOT_NULL(args[0]); |
2022 DCHECK(module_ && module_->instance); | 2025 DCHECK(module_ && module_->instance); |
2023 | 2026 |
2024 MachineOperatorBuilder* machine = jsgraph()->machine(); | 2027 MachineOperatorBuilder* machine = jsgraph()->machine(); |
2025 | 2028 |
2026 // Compute the code object by loading it from the function table. | 2029 // Compute the code object by loading it from the function table. |
2027 Node* key = args[0]; | 2030 Node* key = args[0]; |
2028 | 2031 |
2029 // Assume only one table for now. | 2032 // Assume only one table for now. |
2030 DCHECK_LE(module_->instance->function_tables.size(), 1u); | 2033 DCHECK_LE(module_->instance->function_tables.size(), 1u); |
2031 // Bounds check the index. | 2034 // Bounds check the index. |
2032 uint32_t table_size = | 2035 uint32_t table_size = |
2033 module_->IsValidTable(0) ? module_->GetTable(0)->max_size : 0; | 2036 module_->IsValidTable(0) ? module_->GetTable(0)->max_size : 0; |
| 2037 wasm::FunctionSig* sig = module_->GetSignature(index); |
2034 if (table_size > 0) { | 2038 if (table_size > 0) { |
2035 // Bounds check against the table size. | 2039 // Bounds check against the table size. |
2036 Node* size = Uint32Constant(table_size); | 2040 Node* size = Uint32Constant(table_size); |
2037 Node* in_bounds = graph()->NewNode(machine->Uint32LessThan(), key, size); | 2041 Node* in_bounds = graph()->NewNode(machine->Uint32LessThan(), key, size); |
2038 trap_->AddTrapIfFalse(wasm::kTrapFuncInvalid, in_bounds, position); | 2042 trap_->AddTrapIfFalse(wasm::kTrapFuncInvalid, in_bounds, position); |
2039 } else { | 2043 } else { |
2040 // No function table. Generate a trap and return a constant. | 2044 // No function table. Generate a trap and return a constant. |
2041 trap_->AddTrapIfFalse(wasm::kTrapFuncInvalid, Int32Constant(0), position); | 2045 trap_->AddTrapIfFalse(wasm::kTrapFuncInvalid, Int32Constant(0), position); |
2042 return trap_->GetTrapValue(module_->GetSignature(index)); | 2046 Node** rets = Buffer(sig->return_count()); |
| 2047 for (size_t i = 0; i < sig->return_count(); i++) { |
| 2048 rets[i] = trap_->GetTrapValue(sig->GetReturn(i)); |
| 2049 } |
| 2050 return rets; |
2043 } | 2051 } |
2044 Node* table = FunctionTable(0); | 2052 Node* table = FunctionTable(0); |
2045 | 2053 |
2046 // Load signature from the table and check. | 2054 // Load signature from the table and check. |
2047 // The table is a FixedArray; signatures are encoded as SMIs. | 2055 // The table is a FixedArray; signatures are encoded as SMIs. |
2048 // [sig1, sig2, sig3, ...., code1, code2, code3 ...] | 2056 // [sig1, sig2, sig3, ...., code1, code2, code3 ...] |
2049 ElementAccess access = AccessBuilder::ForFixedArrayElement(); | 2057 ElementAccess access = AccessBuilder::ForFixedArrayElement(); |
2050 const int fixed_offset = access.header_size - access.tag(); | 2058 const int fixed_offset = access.header_size - access.tag(); |
2051 { | 2059 { |
2052 Node* load_sig = graph()->NewNode( | 2060 Node* load_sig = graph()->NewNode( |
(...skipping 13 matching lines...) Expand all Loading... |
2066 uint32_t offset = fixed_offset + kPointerSize * table_size; | 2074 uint32_t offset = fixed_offset + kPointerSize * table_size; |
2067 Node* load_code = graph()->NewNode( | 2075 Node* load_code = graph()->NewNode( |
2068 machine->Load(MachineType::AnyTagged()), table, | 2076 machine->Load(MachineType::AnyTagged()), table, |
2069 graph()->NewNode(machine->Int32Add(), | 2077 graph()->NewNode(machine->Int32Add(), |
2070 graph()->NewNode(machine->Word32Shl(), key, | 2078 graph()->NewNode(machine->Word32Shl(), key, |
2071 Int32Constant(kPointerSizeLog2)), | 2079 Int32Constant(kPointerSizeLog2)), |
2072 Uint32Constant(offset)), | 2080 Uint32Constant(offset)), |
2073 *effect_, *control_); | 2081 *effect_, *control_); |
2074 | 2082 |
2075 args[0] = load_code; | 2083 args[0] = load_code; |
2076 wasm::FunctionSig* sig = module_->GetSignature(index); | |
2077 return BuildWasmCall(sig, args, position); | 2084 return BuildWasmCall(sig, args, position); |
2078 } | 2085 } |
2079 | 2086 |
2080 Node* WasmGraphBuilder::BuildI32Rol(Node* left, Node* right) { | 2087 Node* WasmGraphBuilder::BuildI32Rol(Node* left, Node* right) { |
2081 // Implement Rol by Ror since TurboFan does not have Rol opcode. | 2088 // Implement Rol by Ror since TurboFan does not have Rol opcode. |
2082 // TODO(weiliang): support Word32Rol opcode in TurboFan. | 2089 // TODO(weiliang): support Word32Rol opcode in TurboFan. |
2083 Int32Matcher m(right); | 2090 Int32Matcher m(right); |
2084 if (m.HasValue()) { | 2091 if (m.HasValue()) { |
2085 return Binop(wasm::kExprI32Ror, left, | 2092 return Binop(wasm::kExprI32Ror, left, |
2086 jsgraph()->Int32Constant(32 - m.Value())); | 2093 jsgraph()->Int32Constant(32 - m.Value())); |
(...skipping 1192 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3279 function_->code_start_offset), | 3286 function_->code_start_offset), |
3280 compile_ms); | 3287 compile_ms); |
3281 } | 3288 } |
3282 | 3289 |
3283 return code; | 3290 return code; |
3284 } | 3291 } |
3285 | 3292 |
3286 } // namespace compiler | 3293 } // namespace compiler |
3287 } // namespace internal | 3294 } // namespace internal |
3288 } // namespace v8 | 3295 } // namespace v8 |
OLD | NEW |