OLD | NEW |
1 // Copyright 2014 the V8 project authors. All rights reserved. | 1 // Copyright 2014 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/js-typed-lowering.h" | 5 #include "src/compiler/js-typed-lowering.h" |
6 | 6 |
7 #include "src/builtins/builtins-utils.h" | 7 #include "src/builtins/builtins-utils.h" |
8 #include "src/code-factory.h" | 8 #include "src/code-factory.h" |
9 #include "src/compilation-dependencies.h" | 9 #include "src/compilation-dependencies.h" |
10 #include "src/compiler/access-builder.h" | 10 #include "src/compiler/access-builder.h" |
(...skipping 1485 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1496 node->ReplaceInput(0, rnoop); | 1496 node->ReplaceInput(0, rnoop); |
1497 node->ReplaceInput(1, rconvert); | 1497 node->ReplaceInput(1, rconvert); |
1498 node->ReplaceInput(2, rglobal); | 1498 node->ReplaceInput(2, rglobal); |
1499 node->ReplaceInput(3, control); | 1499 node->ReplaceInput(3, control); |
1500 node->TrimInputCount(4); | 1500 node->TrimInputCount(4); |
1501 NodeProperties::ChangeOp(node, | 1501 NodeProperties::ChangeOp(node, |
1502 common()->Phi(MachineRepresentation::kTagged, 3)); | 1502 common()->Phi(MachineRepresentation::kTagged, 3)); |
1503 return Changed(node); | 1503 return Changed(node); |
1504 } | 1504 } |
1505 | 1505 |
| 1506 namespace { |
| 1507 |
| 1508 void ReduceBuiltin(Isolate* isolate, JSGraph* jsgraph, Node* node, |
| 1509 int builtin_index, int arity, CallDescriptor::Flags flags) { |
| 1510 // Patch {node} to a direct CEntryStub call. |
| 1511 // |
| 1512 // ----------- A r g u m e n t s ----------- |
| 1513 // -- 0: CEntryStub |
| 1514 // --- Stack args --- |
| 1515 // -- 1: receiver |
| 1516 // -- [2, 2 + n[: the n actual arguments passed to the builtin |
| 1517 // -- 2 + n: argc, including the receiver and implicit args (Smi) |
| 1518 // -- 2 + n + 1: target |
| 1519 // -- 2 + n + 2: new_target |
| 1520 // --- Register args --- |
| 1521 // -- 2 + n + 3: the C entry point |
| 1522 // -- 2 + n + 4: argc (Int32) |
| 1523 // ----------------------------------- |
| 1524 |
| 1525 // The logic contained here is mirrored in Builtins::Generate_Adaptor. |
| 1526 // Keep these in sync. |
| 1527 |
| 1528 const bool is_construct = (node->opcode() == IrOpcode::kJSCallConstruct); |
| 1529 |
| 1530 DCHECK(Builtins::HasCppImplementation(builtin_index)); |
| 1531 |
| 1532 Node* target = NodeProperties::GetValueInput(node, 0); |
| 1533 Node* new_target = is_construct |
| 1534 ? NodeProperties::GetValueInput(node, arity + 1) |
| 1535 : jsgraph->UndefinedConstant(); |
| 1536 |
| 1537 // API and CPP builtins are implemented in C++, and we can inline both. |
| 1538 // CPP builtins create a builtin exit frame, API builtins don't. |
| 1539 const bool has_builtin_exit_frame = Builtins::IsCpp(builtin_index); |
| 1540 |
| 1541 Node* stub = jsgraph->CEntryStubConstant(1, kDontSaveFPRegs, kArgvOnStack, |
| 1542 has_builtin_exit_frame); |
| 1543 node->ReplaceInput(0, stub); |
| 1544 |
| 1545 Zone* zone = jsgraph->zone(); |
| 1546 if (is_construct) { |
| 1547 // Unify representations between construct and call nodes. |
| 1548 // Remove new target and add receiver as a stack parameter. |
| 1549 Node* receiver = jsgraph->UndefinedConstant(); |
| 1550 node->RemoveInput(arity + 1); |
| 1551 node->InsertInput(zone, 1, receiver); |
| 1552 } |
| 1553 |
| 1554 const int argc = arity + BuiltinArguments::kNumExtraArgsWithReceiver; |
| 1555 Node* argc_node = jsgraph->Int32Constant(argc); |
| 1556 |
| 1557 node->InsertInput(zone, arity + 2, argc_node); |
| 1558 node->InsertInput(zone, arity + 3, target); |
| 1559 node->InsertInput(zone, arity + 4, new_target); |
| 1560 |
| 1561 Address entry = Builtins::CppEntryOf(builtin_index); |
| 1562 ExternalReference entry_ref(ExternalReference(entry, isolate)); |
| 1563 Node* entry_node = jsgraph->ExternalConstant(entry_ref); |
| 1564 |
| 1565 node->InsertInput(zone, arity + 5, entry_node); |
| 1566 node->InsertInput(zone, arity + 6, argc_node); |
| 1567 |
| 1568 static const int kReturnCount = 1; |
| 1569 const char* debug_name = Builtins::name(builtin_index); |
| 1570 Operator::Properties properties = node->op()->properties(); |
| 1571 CallDescriptor* desc = Linkage::GetCEntryStubCallDescriptor( |
| 1572 zone, kReturnCount, argc, debug_name, properties, flags); |
| 1573 |
| 1574 NodeProperties::ChangeOp(node, jsgraph->common()->Call(desc)); |
| 1575 } |
| 1576 |
| 1577 } // namespace |
| 1578 |
1506 Reduction JSTypedLowering::ReduceJSCallConstruct(Node* node) { | 1579 Reduction JSTypedLowering::ReduceJSCallConstruct(Node* node) { |
1507 DCHECK_EQ(IrOpcode::kJSCallConstruct, node->opcode()); | 1580 DCHECK_EQ(IrOpcode::kJSCallConstruct, node->opcode()); |
1508 CallConstructParameters const& p = CallConstructParametersOf(node->op()); | 1581 CallConstructParameters const& p = CallConstructParametersOf(node->op()); |
1509 DCHECK_LE(2u, p.arity()); | 1582 DCHECK_LE(2u, p.arity()); |
1510 int const arity = static_cast<int>(p.arity() - 2); | 1583 int const arity = static_cast<int>(p.arity() - 2); |
1511 Node* target = NodeProperties::GetValueInput(node, 0); | 1584 Node* target = NodeProperties::GetValueInput(node, 0); |
1512 Type* target_type = NodeProperties::GetType(target); | 1585 Type* target_type = NodeProperties::GetType(target); |
1513 Node* new_target = NodeProperties::GetValueInput(node, arity + 1); | 1586 Node* new_target = NodeProperties::GetValueInput(node, arity + 1); |
| 1587 Node* effect = NodeProperties::GetEffectInput(node); |
| 1588 Node* control = NodeProperties::GetControlInput(node); |
1514 | 1589 |
1515 // Check if {target} is a known JSFunction. | 1590 // Check if {target} is a known JSFunction. |
1516 if (target_type->IsConstant() && | 1591 if (target_type->IsConstant() && |
1517 target_type->AsConstant()->Value()->IsJSFunction()) { | 1592 target_type->AsConstant()->Value()->IsJSFunction()) { |
1518 Handle<JSFunction> function = | 1593 Handle<JSFunction> function = |
1519 Handle<JSFunction>::cast(target_type->AsConstant()->Value()); | 1594 Handle<JSFunction>::cast(target_type->AsConstant()->Value()); |
1520 Handle<SharedFunctionInfo> shared(function->shared(), isolate()); | 1595 Handle<SharedFunctionInfo> shared(function->shared(), isolate()); |
| 1596 const int builtin_index = shared->construct_stub()->builtin_index(); |
| 1597 const bool is_builtin = (builtin_index != -1); |
1521 | 1598 |
1522 // Patch {node} to an indirect call via the {function}s construct stub. | 1599 CallDescriptor::Flags flags = CallDescriptor::kNeedsFrameState; |
1523 Callable callable(handle(shared->construct_stub(), isolate()), | 1600 |
1524 ConstructStubDescriptor(isolate())); | 1601 if (is_builtin && Builtins::HasCppImplementation(builtin_index)) { |
1525 node->RemoveInput(arity + 1); | 1602 // Patch {node} to a direct CEntryStub call. |
1526 node->InsertInput(graph()->zone(), 0, | 1603 |
1527 jsgraph()->HeapConstant(callable.code())); | 1604 // Load the context from the {target}. |
1528 node->InsertInput(graph()->zone(), 2, new_target); | 1605 Node* context = effect = graph()->NewNode( |
1529 node->InsertInput(graph()->zone(), 3, jsgraph()->Int32Constant(arity)); | 1606 simplified()->LoadField(AccessBuilder::ForJSFunctionContext()), |
1530 node->InsertInput(graph()->zone(), 4, jsgraph()->UndefinedConstant()); | 1607 target, effect, control); |
1531 node->InsertInput(graph()->zone(), 5, jsgraph()->UndefinedConstant()); | 1608 NodeProperties::ReplaceContextInput(node, context); |
1532 NodeProperties::ChangeOp( | 1609 |
1533 node, common()->Call(Linkage::GetStubCallDescriptor( | 1610 // Update the effect dependency for the {node}. |
1534 isolate(), graph()->zone(), callable.descriptor(), 1 + arity, | 1611 NodeProperties::ReplaceEffectInput(node, effect); |
1535 CallDescriptor::kNeedsFrameState))); | 1612 |
| 1613 ReduceBuiltin(isolate(), jsgraph(), node, builtin_index, arity, flags); |
| 1614 } else { |
| 1615 // Patch {node} to an indirect call via the {function}s construct stub. |
| 1616 Callable callable(handle(shared->construct_stub(), isolate()), |
| 1617 ConstructStubDescriptor(isolate())); |
| 1618 node->RemoveInput(arity + 1); |
| 1619 node->InsertInput(graph()->zone(), 0, |
| 1620 jsgraph()->HeapConstant(callable.code())); |
| 1621 node->InsertInput(graph()->zone(), 2, new_target); |
| 1622 node->InsertInput(graph()->zone(), 3, jsgraph()->Int32Constant(arity)); |
| 1623 node->InsertInput(graph()->zone(), 4, jsgraph()->UndefinedConstant()); |
| 1624 node->InsertInput(graph()->zone(), 5, jsgraph()->UndefinedConstant()); |
| 1625 NodeProperties::ChangeOp( |
| 1626 node, common()->Call(Linkage::GetStubCallDescriptor( |
| 1627 isolate(), graph()->zone(), callable.descriptor(), |
| 1628 1 + arity, flags))); |
| 1629 } |
1536 return Changed(node); | 1630 return Changed(node); |
1537 } | 1631 } |
1538 | 1632 |
1539 // Check if {target} is a JSFunction. | 1633 // Check if {target} is a JSFunction. |
1540 if (target_type->Is(Type::Function())) { | 1634 if (target_type->Is(Type::Function())) { |
1541 // Patch {node} to an indirect call via the ConstructFunction builtin. | 1635 // Patch {node} to an indirect call via the ConstructFunction builtin. |
1542 Callable callable = CodeFactory::ConstructFunction(isolate()); | 1636 Callable callable = CodeFactory::ConstructFunction(isolate()); |
1543 node->RemoveInput(arity + 1); | 1637 node->RemoveInput(arity + 1); |
1544 node->InsertInput(graph()->zone(), 0, | 1638 node->InsertInput(graph()->zone(), 0, |
1545 jsgraph()->HeapConstant(callable.code())); | 1639 jsgraph()->HeapConstant(callable.code())); |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1611 // Compute flags for the call. | 1705 // Compute flags for the call. |
1612 CallDescriptor::Flags flags = CallDescriptor::kNeedsFrameState; | 1706 CallDescriptor::Flags flags = CallDescriptor::kNeedsFrameState; |
1613 if (p.tail_call_mode() == TailCallMode::kAllow) { | 1707 if (p.tail_call_mode() == TailCallMode::kAllow) { |
1614 flags |= CallDescriptor::kSupportsTailCalls; | 1708 flags |= CallDescriptor::kSupportsTailCalls; |
1615 } | 1709 } |
1616 | 1710 |
1617 Node* new_target = jsgraph()->UndefinedConstant(); | 1711 Node* new_target = jsgraph()->UndefinedConstant(); |
1618 Node* argument_count = jsgraph()->Int32Constant(arity); | 1712 Node* argument_count = jsgraph()->Int32Constant(arity); |
1619 if (is_builtin && Builtins::HasCppImplementation(builtin_index)) { | 1713 if (is_builtin && Builtins::HasCppImplementation(builtin_index)) { |
1620 // Patch {node} to a direct CEntryStub call. | 1714 // Patch {node} to a direct CEntryStub call. |
1621 // | 1715 ReduceBuiltin(isolate(), jsgraph(), node, builtin_index, arity, flags); |
1622 // ----------- A r g u m e n t s ----------- | |
1623 // -- 0: CEntryStub | |
1624 // --- Stack args --- | |
1625 // -- 1: receiver | |
1626 // -- [2, 2 + n[: the n actual arguments passed to the builtin | |
1627 // -- 2 + n: argc, including the receiver and implicit args (Smi) | |
1628 // -- 2 + n + 1: target | |
1629 // -- 2 + n + 2: new_target | |
1630 // --- Register args --- | |
1631 // -- 2 + n + 3: the C entry point | |
1632 // -- 2 + n + 4: argc (Int32) | |
1633 // ----------------------------------- | |
1634 | |
1635 // The logic contained here is mirrored in Builtins::Generate_Adaptor. | |
1636 // Keep these in sync. | |
1637 | |
1638 // API and CPP builtins are implemented in C++, and we can inline both. | |
1639 // CPP builtins create a builtin exit frame, API builtins don't. | |
1640 const bool create_builtin_exit_frame = Builtins::IsCpp(builtin_index); | |
1641 | |
1642 Node* stub = jsgraph()->CEntryStubConstant( | |
1643 1, kDontSaveFPRegs, kArgvOnStack, create_builtin_exit_frame); | |
1644 node->ReplaceInput(0, stub); | |
1645 | |
1646 const int argc = arity + BuiltinArguments::kNumExtraArgsWithReceiver; | |
1647 Node* argc_node = jsgraph()->Int32Constant(argc); | |
1648 | |
1649 Zone* zone = graph()->zone(); | |
1650 node->InsertInput(zone, arity + 2, argc_node); | |
1651 node->InsertInput(zone, arity + 3, target); | |
1652 node->InsertInput(zone, arity + 4, new_target); | |
1653 | |
1654 Address entry = Builtins::CppEntryOf(builtin_index); | |
1655 ExternalReference entry_ref(ExternalReference(entry, isolate())); | |
1656 Node* entry_node = jsgraph()->ExternalConstant(entry_ref); | |
1657 | |
1658 node->InsertInput(zone, arity + 5, entry_node); | |
1659 node->InsertInput(zone, arity + 6, argc_node); | |
1660 | |
1661 const int return_count = 1; | |
1662 Operator::Properties properties = node->op()->properties(); | |
1663 const char* debug_name = Builtins::name(builtin_index); | |
1664 CallDescriptor* desc = Linkage::GetCEntryStubCallDescriptor( | |
1665 graph()->zone(), return_count, argc, debug_name, properties, flags); | |
1666 | |
1667 NodeProperties::ChangeOp(node, common()->Call(desc)); | |
1668 } else if (shared->internal_formal_parameter_count() == arity || | 1716 } else if (shared->internal_formal_parameter_count() == arity || |
1669 shared->internal_formal_parameter_count() == | 1717 shared->internal_formal_parameter_count() == |
1670 SharedFunctionInfo::kDontAdaptArgumentsSentinel) { | 1718 SharedFunctionInfo::kDontAdaptArgumentsSentinel) { |
1671 // Patch {node} to a direct call. | 1719 // Patch {node} to a direct call. |
1672 node->InsertInput(graph()->zone(), arity + 2, new_target); | 1720 node->InsertInput(graph()->zone(), arity + 2, new_target); |
1673 node->InsertInput(graph()->zone(), arity + 3, argument_count); | 1721 node->InsertInput(graph()->zone(), arity + 3, argument_count); |
1674 NodeProperties::ChangeOp(node, | 1722 NodeProperties::ChangeOp(node, |
1675 common()->Call(Linkage::GetJSCallDescriptor( | 1723 common()->Call(Linkage::GetJSCallDescriptor( |
1676 graph()->zone(), false, 1 + arity, flags))); | 1724 graph()->zone(), false, 1 + arity, flags))); |
1677 } else { | 1725 } else { |
(...skipping 493 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2171 } | 2219 } |
2172 | 2220 |
2173 | 2221 |
2174 CompilationDependencies* JSTypedLowering::dependencies() const { | 2222 CompilationDependencies* JSTypedLowering::dependencies() const { |
2175 return dependencies_; | 2223 return dependencies_; |
2176 } | 2224 } |
2177 | 2225 |
2178 } // namespace compiler | 2226 } // namespace compiler |
2179 } // namespace internal | 2227 } // namespace internal |
2180 } // namespace v8 | 2228 } // namespace v8 |
OLD | NEW |