OLD | NEW |
---|---|
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 #include "vm/code_generator.h" | 5 #include "vm/code_generator.h" |
6 | 6 |
7 #include "vm/assembler.h" | 7 #include "vm/assembler.h" |
8 #include "vm/ast.h" | 8 #include "vm/ast.h" |
9 #include "vm/code_patcher.h" | 9 #include "vm/code_patcher.h" |
10 #include "vm/compiler.h" | 10 #include "vm/compiler.h" |
(...skipping 656 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
667 if (!target_function.HasCode()) { | 667 if (!target_function.HasCode()) { |
668 const Error& error = | 668 const Error& error = |
669 Error::Handle(Compiler::CompileFunction(thread, target_function)); | 669 Error::Handle(Compiler::CompileFunction(thread, target_function)); |
670 if (!error.IsNull()) { | 670 if (!error.IsNull()) { |
671 Exceptions::PropagateError(error); | 671 Exceptions::PropagateError(error); |
672 } | 672 } |
673 } | 673 } |
674 const Code& target_code = Code::Handle(target_function.CurrentCode()); | 674 const Code& target_code = Code::Handle(target_function.CurrentCode()); |
675 // Before patching verify that we are not repeatedly patching to the same | 675 // Before patching verify that we are not repeatedly patching to the same |
676 // target. | 676 // target. |
677 ASSERT(target_code.EntryPoint() != | 677 ASSERT(target_code.raw() != |
678 CodePatcher::GetStaticCallTargetAt(caller_frame->pc(), caller_code)); | 678 CodePatcher::GetStaticCallTargetAt(caller_frame->pc(), caller_code)); |
679 const Instructions& instrs = | 679 const Instructions& instrs = |
680 Instructions::Handle(caller_code.instructions()); | 680 Instructions::Handle(caller_code.instructions()); |
681 { | 681 { |
682 WritableInstructionsScope writable(instrs.EntryPoint(), instrs.size()); | 682 WritableInstructionsScope writable(instrs.EntryPoint(), instrs.size()); |
683 CodePatcher::PatchStaticCallAt(caller_frame->pc(), caller_code, | 683 CodePatcher::PatchStaticCallAt(caller_frame->pc(), |
684 target_code.EntryPoint()); | 684 caller_code, |
685 target_code); | |
685 caller_code.SetStaticCallTargetCodeAt(caller_frame->pc(), target_code); | 686 caller_code.SetStaticCallTargetCodeAt(caller_frame->pc(), target_code); |
686 } | 687 } |
687 if (FLAG_trace_patching) { | 688 if (FLAG_trace_patching) { |
688 OS::PrintErr("PatchStaticCall: patching caller pc %#" Px "" | 689 OS::PrintErr("PatchStaticCall: patching caller pc %#" Px "" |
689 " to '%s' new entry point %#" Px " (%s)\n", | 690 " to '%s' new entry point %#" Px " (%s)\n", |
690 caller_frame->pc(), | 691 caller_frame->pc(), |
691 target_function.ToFullyQualifiedCString(), | 692 target_function.ToFullyQualifiedCString(), |
692 target_code.EntryPoint(), | 693 target_code.EntryPoint(), |
693 target_code.is_optimized() ? "optimized" : "unoptimized"); | 694 target_code.is_optimized() ? "optimized" : "unoptimized"); |
694 } | 695 } |
695 arguments.SetReturn(target_code); | 696 arguments.SetReturn(target_code); |
696 } | 697 } |
697 | 698 |
698 | 699 |
699 // Result of an invoke may be an unhandled exception, in which case we | 700 // Result of an invoke may be an unhandled exception, in which case we |
700 // rethrow it. | 701 // rethrow it. |
701 static void CheckResultError(const Object& result) { | 702 static void CheckResultError(const Object& result) { |
702 if (result.IsError()) { | 703 if (result.IsError()) { |
703 Exceptions::PropagateError(Error::Cast(result)); | 704 Exceptions::PropagateError(Error::Cast(result)); |
704 } | 705 } |
705 } | 706 } |
706 | 707 |
707 | 708 |
708 // Gets called from debug stub when code reaches a breakpoint | 709 // Gets called from debug stub when code reaches a breakpoint |
709 // set on a runtime stub call. | 710 // set on a runtime stub call. |
710 DEFINE_RUNTIME_ENTRY(BreakpointRuntimeHandler, 0) { | 711 DEFINE_RUNTIME_ENTRY(BreakpointRuntimeHandler, 0) { |
711 DartFrameIterator iterator; | 712 DartFrameIterator iterator; |
712 StackFrame* caller_frame = iterator.NextFrame(); | 713 StackFrame* caller_frame = iterator.NextFrame(); |
713 ASSERT(caller_frame != NULL); | 714 ASSERT(caller_frame != NULL); |
714 uword orig_stub = | 715 const Code& orig_stub = Code::Handle( |
715 isolate->debugger()->GetPatchedStubAddress(caller_frame->pc()); | 716 isolate->debugger()->GetPatchedStubAddress(caller_frame->pc())); |
716 isolate->debugger()->SignalBpReached(); | 717 isolate->debugger()->SignalBpReached(); |
717 ASSERT((orig_stub & kSmiTagMask) == kSmiTag); | 718 arguments.SetReturn(orig_stub); |
718 arguments.SetReturn(Smi::Handle(reinterpret_cast<RawSmi*>(orig_stub))); | |
719 } | 719 } |
720 | 720 |
721 | 721 |
722 DEFINE_RUNTIME_ENTRY(SingleStepHandler, 0) { | 722 DEFINE_RUNTIME_ENTRY(SingleStepHandler, 0) { |
723 isolate->debugger()->DebuggerStepCallback(); | 723 isolate->debugger()->DebuggerStepCallback(); |
724 } | 724 } |
725 | 725 |
726 | 726 |
727 // An instance call of the form o.f(...) could not be resolved. Check if | 727 // An instance call of the form o.f(...) could not be resolved. Check if |
728 // there is a getter with the same name. If so, invoke it. If the value is | 728 // there is a getter with the same name. If so, invoke it. If the value is |
(...skipping 703 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1432 const Code& optimized_code = Code::Handle(function.CurrentCode()); | 1432 const Code& optimized_code = Code::Handle(function.CurrentCode()); |
1433 // The current code will not be changed in the case that the compiler | 1433 // The current code will not be changed in the case that the compiler |
1434 // bailed out during OSR compilation. | 1434 // bailed out during OSR compilation. |
1435 if (optimized_code.raw() != original_code.raw()) { | 1435 if (optimized_code.raw() != original_code.raw()) { |
1436 // The OSR code does not work for calling the function, so restore the | 1436 // The OSR code does not work for calling the function, so restore the |
1437 // unoptimized code. Patch the stack frame to return into the OSR | 1437 // unoptimized code. Patch the stack frame to return into the OSR |
1438 // code. | 1438 // code. |
1439 uword optimized_entry = | 1439 uword optimized_entry = |
1440 Instructions::Handle(optimized_code.instructions()).EntryPoint(); | 1440 Instructions::Handle(optimized_code.instructions()).EntryPoint(); |
1441 function.AttachCode(original_code); | 1441 function.AttachCode(original_code); |
1442 // TODO(vegorov) figure out if it is GC safe. | |
rmacnak
2015/09/09 01:37:48
The change to kFirstObjectSlotFromFp makes it safe
Florian Schneider
2015/09/09 13:19:49
I think so too. We do now visit the code object po
| |
1442 frame->set_pc(optimized_entry); | 1443 frame->set_pc(optimized_entry); |
1444 frame->set_pc_marker(reinterpret_cast<uword>(optimized_code.raw())); | |
1443 } | 1445 } |
1444 } | 1446 } |
1445 } | 1447 } |
1446 | 1448 |
1447 | 1449 |
1448 DEFINE_RUNTIME_ENTRY(TraceICCall, 2) { | 1450 DEFINE_RUNTIME_ENTRY(TraceICCall, 2) { |
1449 const ICData& ic_data = ICData::CheckedHandle(arguments.ArgAt(0)); | 1451 const ICData& ic_data = ICData::CheckedHandle(arguments.ArgAt(0)); |
1450 const Function& function = Function::CheckedHandle(arguments.ArgAt(1)); | 1452 const Function& function = Function::CheckedHandle(arguments.ArgAt(1)); |
1451 DartFrameIterator iterator; | 1453 DartFrameIterator iterator; |
1452 StackFrame* frame = iterator.NextFrame(); | 1454 StackFrame* frame = iterator.NextFrame(); |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1524 } | 1526 } |
1525 ASSERT(target_function.HasCode()); | 1527 ASSERT(target_function.HasCode()); |
1526 ASSERT(target_function.raw() == target_code.function()); | 1528 ASSERT(target_function.raw() == target_code.function()); |
1527 | 1529 |
1528 const Code& current_target_code = Code::Handle( | 1530 const Code& current_target_code = Code::Handle( |
1529 isolate, target_function.CurrentCode()); | 1531 isolate, target_function.CurrentCode()); |
1530 const Instructions& instrs = Instructions::Handle( | 1532 const Instructions& instrs = Instructions::Handle( |
1531 isolate, caller_code.instructions()); | 1533 isolate, caller_code.instructions()); |
1532 { | 1534 { |
1533 WritableInstructionsScope writable(instrs.EntryPoint(), instrs.size()); | 1535 WritableInstructionsScope writable(instrs.EntryPoint(), instrs.size()); |
1534 CodePatcher::PatchStaticCallAt(frame->pc(), caller_code, | 1536 CodePatcher::PatchStaticCallAt(frame->pc(), |
1535 current_target_code.EntryPoint()); | 1537 caller_code, |
1538 current_target_code); | |
1536 caller_code.SetStaticCallTargetCodeAt(frame->pc(), current_target_code); | 1539 caller_code.SetStaticCallTargetCodeAt(frame->pc(), current_target_code); |
1537 } | 1540 } |
1538 if (FLAG_trace_patching) { | 1541 if (FLAG_trace_patching) { |
1539 OS::PrintErr("FixCallersTarget: caller %#" Px " " | 1542 OS::PrintErr("FixCallersTarget: caller %#" Px " " |
1540 "target '%s' %#" Px " -> %#" Px "\n", | 1543 "target '%s' %#" Px " -> %#" Px "\n", |
1541 frame->pc(), | 1544 frame->pc(), |
1542 target_function.ToFullyQualifiedCString(), | 1545 target_function.ToFullyQualifiedCString(), |
1543 target_code.EntryPoint(), | 1546 target_code.EntryPoint(), |
1544 current_target_code.EntryPoint()); | 1547 current_target_code.EntryPoint()); |
1545 } | 1548 } |
(...skipping 11 matching lines...) Expand all Loading... | |
1557 frame = iterator.NextFrame(); | 1560 frame = iterator.NextFrame(); |
1558 ASSERT(frame != NULL); | 1561 ASSERT(frame != NULL); |
1559 } | 1562 } |
1560 if (frame->IsEntryFrame()) { | 1563 if (frame->IsEntryFrame()) { |
1561 // There must be a valid Dart frame. | 1564 // There must be a valid Dart frame. |
1562 UNREACHABLE(); | 1565 UNREACHABLE(); |
1563 } | 1566 } |
1564 ASSERT(frame->IsDartFrame()); | 1567 ASSERT(frame->IsDartFrame()); |
1565 const Code& caller_code = Code::Handle(isolate, frame->LookupDartCode()); | 1568 const Code& caller_code = Code::Handle(isolate, frame->LookupDartCode()); |
1566 ASSERT(!caller_code.IsNull()); | 1569 ASSERT(!caller_code.IsNull()); |
1567 const uword target = | 1570 const Code& stub = Code::Handle( |
1568 CodePatcher::GetStaticCallTargetAt(frame->pc(), caller_code); | 1571 CodePatcher::GetStaticCallTargetAt(frame->pc(), caller_code)); |
1569 const Code& stub = Code::Handle(isolate, Code::LookupCode(target)); | |
1570 Class& alloc_class = Class::ZoneHandle(zone); | 1572 Class& alloc_class = Class::ZoneHandle(zone); |
1571 alloc_class ^= stub.owner(); | 1573 alloc_class ^= stub.owner(); |
1572 Code& alloc_stub = Code::Handle(isolate, alloc_class.allocation_stub()); | 1574 Code& alloc_stub = Code::Handle(isolate, alloc_class.allocation_stub()); |
1573 if (alloc_stub.IsNull()) { | 1575 if (alloc_stub.IsNull()) { |
1574 alloc_stub = StubCode::GetAllocationStubForClass(alloc_class); | 1576 alloc_stub = StubCode::GetAllocationStubForClass(alloc_class); |
1575 ASSERT(!CodePatcher::IsEntryPatched(alloc_stub)); | 1577 ASSERT(!CodePatcher::IsEntryPatched(alloc_stub)); |
1576 } | 1578 } |
1577 const Instructions& instrs = | 1579 const Instructions& instrs = |
1578 Instructions::Handle(isolate, caller_code.instructions()); | 1580 Instructions::Handle(isolate, caller_code.instructions()); |
1579 { | 1581 { |
1580 WritableInstructionsScope writable(instrs.EntryPoint(), instrs.size()); | 1582 WritableInstructionsScope writable(instrs.EntryPoint(), instrs.size()); |
1581 CodePatcher::PatchStaticCallAt(frame->pc(), | 1583 CodePatcher::PatchStaticCallAt(frame->pc(), |
1582 caller_code, | 1584 caller_code, |
1583 alloc_stub.EntryPoint()); | 1585 alloc_stub); |
1584 caller_code.SetStubCallTargetCodeAt(frame->pc(), alloc_stub); | 1586 caller_code.SetStubCallTargetCodeAt(frame->pc(), alloc_stub); |
1585 } | 1587 } |
1586 if (FLAG_trace_patching) { | 1588 if (FLAG_trace_patching) { |
1587 OS::PrintErr("FixAllocationStubTarget: caller %#" Px " alloc-class %s " | 1589 OS::PrintErr("FixAllocationStubTarget: caller %#" Px " alloc-class %s " |
1588 " -> %#" Px "\n", | 1590 " -> %#" Px "\n", |
1589 frame->pc(), | 1591 frame->pc(), |
1590 alloc_class.ToCString(), | 1592 alloc_class.ToCString(), |
1591 alloc_stub.EntryPoint()); | 1593 alloc_stub.EntryPoint()); |
1592 } | 1594 } |
1593 arguments.SetReturn(alloc_stub); | 1595 arguments.SetReturn(alloc_stub); |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1629 function.SwitchToUnoptimizedCode(); | 1631 function.SwitchToUnoptimizedCode(); |
1630 } | 1632 } |
1631 // Patch call site (lazy deoptimization is quite rare, patching it twice | 1633 // Patch call site (lazy deoptimization is quite rare, patching it twice |
1632 // is not a performance issue). | 1634 // is not a performance issue). |
1633 uword lazy_deopt_jump = optimized_code.GetLazyDeoptPc(); | 1635 uword lazy_deopt_jump = optimized_code.GetLazyDeoptPc(); |
1634 ASSERT(lazy_deopt_jump != 0); | 1636 ASSERT(lazy_deopt_jump != 0); |
1635 const Instructions& instrs = | 1637 const Instructions& instrs = |
1636 Instructions::Handle(zone, optimized_code.instructions()); | 1638 Instructions::Handle(zone, optimized_code.instructions()); |
1637 { | 1639 { |
1638 WritableInstructionsScope writable(instrs.EntryPoint(), instrs.size()); | 1640 WritableInstructionsScope writable(instrs.EntryPoint(), instrs.size()); |
1639 CodePatcher::InsertCallAt(pc, lazy_deopt_jump); | 1641 CodePatcher::InsertDeoptimizationCallAt(pc, lazy_deopt_jump); |
1640 } | 1642 } |
1641 if (FLAG_trace_patching) { | 1643 if (FLAG_trace_patching) { |
1642 const String& name = String::Handle(function.name()); | 1644 const String& name = String::Handle(function.name()); |
1643 OS::PrintErr("InsertCallAt: %" Px " to %" Px " for %s\n", pc, | 1645 OS::PrintErr("InsertDeoptimizationCallAt: %" Px " to %" Px " for %s\n", pc, |
1644 lazy_deopt_jump, name.ToCString()); | 1646 lazy_deopt_jump, name.ToCString()); |
1645 } | 1647 } |
1646 // Mark code as dead (do not GC its embedded objects). | 1648 // Mark code as dead (do not GC its embedded objects). |
1647 optimized_code.set_is_alive(false); | 1649 optimized_code.set_is_alive(false); |
1648 } | 1650 } |
1649 | 1651 |
1650 | 1652 |
1651 // Currently checks only that all optimized frames have kDeoptIndex | 1653 // Currently checks only that all optimized frames have kDeoptIndex |
1652 // and unoptimized code has the kDeoptAfter. | 1654 // and unoptimized code has the kDeoptAfter. |
1653 void DeoptimizeFunctionsOnStack() { | 1655 void DeoptimizeFunctionsOnStack() { |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1686 *reinterpret_cast<intptr_t*>(saved_registers_address); | 1688 *reinterpret_cast<intptr_t*>(saved_registers_address); |
1687 saved_registers_address += kWordSize; | 1689 saved_registers_address += kWordSize; |
1688 } | 1690 } |
1689 *cpu_registers = cpu_registers_copy; | 1691 *cpu_registers = cpu_registers_copy; |
1690 } | 1692 } |
1691 | 1693 |
1692 | 1694 |
1693 // Copies saved registers and caller's frame into temporary buffers. | 1695 // Copies saved registers and caller's frame into temporary buffers. |
1694 // Returns the stack size of unoptimized frame. | 1696 // Returns the stack size of unoptimized frame. |
1695 DEFINE_LEAF_RUNTIME_ENTRY(intptr_t, DeoptimizeCopyFrame, | 1697 DEFINE_LEAF_RUNTIME_ENTRY(intptr_t, DeoptimizeCopyFrame, |
1696 1, uword saved_registers_address) { | 1698 2, |
1699 uword saved_registers_address, | |
1700 uword is_lazy_deopt) { | |
1697 Thread* thread = Thread::Current(); | 1701 Thread* thread = Thread::Current(); |
1698 Isolate* isolate = thread->isolate(); | 1702 Isolate* isolate = thread->isolate(); |
1699 StackZone zone(thread); | 1703 StackZone zone(thread); |
1700 HANDLESCOPE(thread); | 1704 HANDLESCOPE(thread); |
1701 | 1705 |
1702 // All registers have been saved below last-fp as if they were locals. | 1706 // All registers have been saved below last-fp as if they were locals. |
1703 const uword last_fp = saved_registers_address | 1707 const uword last_fp = saved_registers_address |
1704 + (kNumberOfCpuRegisters * kWordSize) | 1708 + (kNumberOfCpuRegisters * kWordSize) |
1705 + (kNumberOfFpuRegisters * kFpuRegisterSize) | 1709 + (kNumberOfFpuRegisters * kFpuRegisterSize) |
1706 - ((kFirstLocalSlotFromFp + 1) * kWordSize); | 1710 - ((kFirstLocalSlotFromFp + 1) * kWordSize); |
1707 | 1711 |
1708 // Get optimized code and frame that need to be deoptimized. | 1712 // Get optimized code and frame that need to be deoptimized. |
1709 DartFrameIterator iterator(last_fp); | 1713 DartFrameIterator iterator(last_fp); |
1710 StackFrame* caller_frame = iterator.NextFrame(); | 1714 StackFrame* caller_frame = iterator.NextFrame(); |
1711 ASSERT(caller_frame != NULL); | 1715 ASSERT(caller_frame != NULL); |
1712 const Code& optimized_code = Code::Handle(caller_frame->LookupDartCode()); | 1716 const Code& optimized_code = Code::Handle(caller_frame->LookupDartCode()); |
1713 ASSERT(optimized_code.is_optimized()); | 1717 ASSERT(optimized_code.is_optimized()); |
1714 | 1718 |
1715 // Copy the saved registers from the stack. | 1719 // Copy the saved registers from the stack. |
1716 fpu_register_t* fpu_registers; | 1720 fpu_register_t* fpu_registers; |
1717 intptr_t* cpu_registers; | 1721 intptr_t* cpu_registers; |
1718 CopySavedRegisters(saved_registers_address, &fpu_registers, &cpu_registers); | 1722 CopySavedRegisters(saved_registers_address, &fpu_registers, &cpu_registers); |
1719 | 1723 |
1720 // Create the DeoptContext. | 1724 // Create the DeoptContext. |
1721 DeoptContext* deopt_context = | 1725 DeoptContext* deopt_context = |
1722 new DeoptContext(caller_frame, optimized_code, | 1726 new DeoptContext(caller_frame, |
1727 optimized_code, | |
1723 DeoptContext::kDestIsOriginalFrame, | 1728 DeoptContext::kDestIsOriginalFrame, |
1724 fpu_registers, cpu_registers); | 1729 fpu_registers, |
1730 cpu_registers, | |
1731 is_lazy_deopt != 0); | |
1725 isolate->set_deopt_context(deopt_context); | 1732 isolate->set_deopt_context(deopt_context); |
1726 | 1733 |
1727 // Stack size (FP - SP) in bytes. | 1734 // Stack size (FP - SP) in bytes. |
1728 return deopt_context->DestStackAdjustment() * kWordSize; | 1735 return deopt_context->DestStackAdjustment() * kWordSize; |
1729 } | 1736 } |
1730 END_LEAF_RUNTIME_ENTRY | 1737 END_LEAF_RUNTIME_ENTRY |
1731 | 1738 |
1732 | 1739 |
1733 // The stack has been adjusted to fit all values for unoptimized frame. | 1740 // The stack has been adjusted to fit all values for unoptimized frame. |
1734 // Fill the unoptimized frame. | 1741 // Fill the unoptimized frame. |
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1855 const intptr_t elm_size = old_data.ElementSizeInBytes(); | 1862 const intptr_t elm_size = old_data.ElementSizeInBytes(); |
1856 const TypedData& new_data = | 1863 const TypedData& new_data = |
1857 TypedData::Handle(TypedData::New(cid, new_size, Heap::kOld)); | 1864 TypedData::Handle(TypedData::New(cid, new_size, Heap::kOld)); |
1858 TypedData::Copy(new_data, 0, old_data, 0, old_size * elm_size); | 1865 TypedData::Copy(new_data, 0, old_data, 0, old_size * elm_size); |
1859 typed_data_cell.SetAt(0, new_data); | 1866 typed_data_cell.SetAt(0, new_data); |
1860 arguments.SetReturn(new_data); | 1867 arguments.SetReturn(new_data); |
1861 } | 1868 } |
1862 | 1869 |
1863 | 1870 |
1864 } // namespace dart | 1871 } // namespace dart |
OLD | NEW |