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

Side by Side Diff: runtime/vm/code_generator.cc

Issue 1192103004: VM: New calling convention for generated code. (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: ia32 port, addressed comments Created 5 years, 3 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
OLDNEW
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
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 704 matching lines...) Expand 10 before | Expand all | Expand 10 after
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 frame->set_pc(optimized_entry); 1442 frame->set_pc(optimized_entry);
1443 frame->set_pc_marker(optimized_code.raw());
1443 } 1444 }
1444 } 1445 }
1445 } 1446 }
1446 1447
1447 1448
1448 DEFINE_RUNTIME_ENTRY(TraceICCall, 2) { 1449 DEFINE_RUNTIME_ENTRY(TraceICCall, 2) {
1449 const ICData& ic_data = ICData::CheckedHandle(arguments.ArgAt(0)); 1450 const ICData& ic_data = ICData::CheckedHandle(arguments.ArgAt(0));
1450 const Function& function = Function::CheckedHandle(arguments.ArgAt(1)); 1451 const Function& function = Function::CheckedHandle(arguments.ArgAt(1));
1451 DartFrameIterator iterator; 1452 DartFrameIterator iterator;
1452 StackFrame* frame = iterator.NextFrame(); 1453 StackFrame* frame = iterator.NextFrame();
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
1524 } 1525 }
1525 ASSERT(target_function.HasCode()); 1526 ASSERT(target_function.HasCode());
1526 ASSERT(target_function.raw() == target_code.function()); 1527 ASSERT(target_function.raw() == target_code.function());
1527 1528
1528 const Code& current_target_code = Code::Handle( 1529 const Code& current_target_code = Code::Handle(
1529 isolate, target_function.CurrentCode()); 1530 isolate, target_function.CurrentCode());
1530 const Instructions& instrs = Instructions::Handle( 1531 const Instructions& instrs = Instructions::Handle(
1531 isolate, caller_code.instructions()); 1532 isolate, caller_code.instructions());
1532 { 1533 {
1533 WritableInstructionsScope writable(instrs.EntryPoint(), instrs.size()); 1534 WritableInstructionsScope writable(instrs.EntryPoint(), instrs.size());
1534 CodePatcher::PatchStaticCallAt(frame->pc(), caller_code, 1535 CodePatcher::PatchStaticCallAt(frame->pc(),
1535 current_target_code.EntryPoint()); 1536 caller_code,
1537 current_target_code);
1536 caller_code.SetStaticCallTargetCodeAt(frame->pc(), current_target_code); 1538 caller_code.SetStaticCallTargetCodeAt(frame->pc(), current_target_code);
1537 } 1539 }
1538 if (FLAG_trace_patching) { 1540 if (FLAG_trace_patching) {
1539 OS::PrintErr("FixCallersTarget: caller %#" Px " " 1541 OS::PrintErr("FixCallersTarget: caller %#" Px " "
1540 "target '%s' %#" Px " -> %#" Px "\n", 1542 "target '%s' %#" Px " -> %#" Px "\n",
1541 frame->pc(), 1543 frame->pc(),
1542 target_function.ToFullyQualifiedCString(), 1544 target_function.ToFullyQualifiedCString(),
1543 target_code.EntryPoint(), 1545 target_code.EntryPoint(),
1544 current_target_code.EntryPoint()); 1546 current_target_code.EntryPoint());
1545 } 1547 }
(...skipping 11 matching lines...) Expand all
1557 frame = iterator.NextFrame(); 1559 frame = iterator.NextFrame();
1558 ASSERT(frame != NULL); 1560 ASSERT(frame != NULL);
1559 } 1561 }
1560 if (frame->IsEntryFrame()) { 1562 if (frame->IsEntryFrame()) {
1561 // There must be a valid Dart frame. 1563 // There must be a valid Dart frame.
1562 UNREACHABLE(); 1564 UNREACHABLE();
1563 } 1565 }
1564 ASSERT(frame->IsDartFrame()); 1566 ASSERT(frame->IsDartFrame());
1565 const Code& caller_code = Code::Handle(isolate, frame->LookupDartCode()); 1567 const Code& caller_code = Code::Handle(isolate, frame->LookupDartCode());
1566 ASSERT(!caller_code.IsNull()); 1568 ASSERT(!caller_code.IsNull());
1567 const uword target = 1569 const Code& stub = Code::Handle(
1568 CodePatcher::GetStaticCallTargetAt(frame->pc(), caller_code); 1570 CodePatcher::GetStaticCallTargetAt(frame->pc(), caller_code));
1569 const Code& stub = Code::Handle(isolate, Code::LookupCode(target));
1570 Class& alloc_class = Class::ZoneHandle(zone); 1571 Class& alloc_class = Class::ZoneHandle(zone);
1571 alloc_class ^= stub.owner(); 1572 alloc_class ^= stub.owner();
1572 Code& alloc_stub = Code::Handle(isolate, alloc_class.allocation_stub()); 1573 Code& alloc_stub = Code::Handle(isolate, alloc_class.allocation_stub());
1573 if (alloc_stub.IsNull()) { 1574 if (alloc_stub.IsNull()) {
1574 alloc_stub = StubCode::GetAllocationStubForClass(alloc_class); 1575 alloc_stub = StubCode::GetAllocationStubForClass(alloc_class);
1575 ASSERT(!CodePatcher::IsEntryPatched(alloc_stub)); 1576 ASSERT(!CodePatcher::IsEntryPatched(alloc_stub));
1576 } 1577 }
1577 const Instructions& instrs = 1578 const Instructions& instrs =
1578 Instructions::Handle(isolate, caller_code.instructions()); 1579 Instructions::Handle(isolate, caller_code.instructions());
1579 { 1580 {
1580 WritableInstructionsScope writable(instrs.EntryPoint(), instrs.size()); 1581 WritableInstructionsScope writable(instrs.EntryPoint(), instrs.size());
1581 CodePatcher::PatchStaticCallAt(frame->pc(), 1582 CodePatcher::PatchStaticCallAt(frame->pc(),
1582 caller_code, 1583 caller_code,
1583 alloc_stub.EntryPoint()); 1584 alloc_stub);
1584 caller_code.SetStubCallTargetCodeAt(frame->pc(), alloc_stub); 1585 caller_code.SetStubCallTargetCodeAt(frame->pc(), alloc_stub);
1585 } 1586 }
1586 if (FLAG_trace_patching) { 1587 if (FLAG_trace_patching) {
1587 OS::PrintErr("FixAllocationStubTarget: caller %#" Px " alloc-class %s " 1588 OS::PrintErr("FixAllocationStubTarget: caller %#" Px " alloc-class %s "
1588 " -> %#" Px "\n", 1589 " -> %#" Px "\n",
1589 frame->pc(), 1590 frame->pc(),
1590 alloc_class.ToCString(), 1591 alloc_class.ToCString(),
1591 alloc_stub.EntryPoint()); 1592 alloc_stub.EntryPoint());
1592 } 1593 }
1593 arguments.SetReturn(alloc_stub); 1594 arguments.SetReturn(alloc_stub);
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
1629 function.SwitchToUnoptimizedCode(); 1630 function.SwitchToUnoptimizedCode();
1630 } 1631 }
1631 // Patch call site (lazy deoptimization is quite rare, patching it twice 1632 // Patch call site (lazy deoptimization is quite rare, patching it twice
1632 // is not a performance issue). 1633 // is not a performance issue).
1633 uword lazy_deopt_jump = optimized_code.GetLazyDeoptPc(); 1634 uword lazy_deopt_jump = optimized_code.GetLazyDeoptPc();
1634 ASSERT(lazy_deopt_jump != 0); 1635 ASSERT(lazy_deopt_jump != 0);
1635 const Instructions& instrs = 1636 const Instructions& instrs =
1636 Instructions::Handle(zone, optimized_code.instructions()); 1637 Instructions::Handle(zone, optimized_code.instructions());
1637 { 1638 {
1638 WritableInstructionsScope writable(instrs.EntryPoint(), instrs.size()); 1639 WritableInstructionsScope writable(instrs.EntryPoint(), instrs.size());
1639 CodePatcher::InsertCallAt(pc, lazy_deopt_jump); 1640 CodePatcher::InsertDeoptimizationCallAt(pc, lazy_deopt_jump);
1640 } 1641 }
1641 if (FLAG_trace_patching) { 1642 if (FLAG_trace_patching) {
1642 const String& name = String::Handle(function.name()); 1643 const String& name = String::Handle(function.name());
1643 OS::PrintErr("InsertCallAt: %" Px " to %" Px " for %s\n", pc, 1644 OS::PrintErr("InsertDeoptimizationCallAt: %" Px " to %" Px " for %s\n", pc,
1644 lazy_deopt_jump, name.ToCString()); 1645 lazy_deopt_jump, name.ToCString());
1645 } 1646 }
1646 // Mark code as dead (do not GC its embedded objects). 1647 // Mark code as dead (do not GC its embedded objects).
1647 optimized_code.set_is_alive(false); 1648 optimized_code.set_is_alive(false);
1648 } 1649 }
1649 1650
1650 1651
1651 // Currently checks only that all optimized frames have kDeoptIndex 1652 // Currently checks only that all optimized frames have kDeoptIndex
1652 // and unoptimized code has the kDeoptAfter. 1653 // and unoptimized code has the kDeoptAfter.
1653 void DeoptimizeFunctionsOnStack() { 1654 void DeoptimizeFunctionsOnStack() {
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
1686 *reinterpret_cast<intptr_t*>(saved_registers_address); 1687 *reinterpret_cast<intptr_t*>(saved_registers_address);
1687 saved_registers_address += kWordSize; 1688 saved_registers_address += kWordSize;
1688 } 1689 }
1689 *cpu_registers = cpu_registers_copy; 1690 *cpu_registers = cpu_registers_copy;
1690 } 1691 }
1691 1692
1692 1693
1693 // Copies saved registers and caller's frame into temporary buffers. 1694 // Copies saved registers and caller's frame into temporary buffers.
1694 // Returns the stack size of unoptimized frame. 1695 // Returns the stack size of unoptimized frame.
1695 DEFINE_LEAF_RUNTIME_ENTRY(intptr_t, DeoptimizeCopyFrame, 1696 DEFINE_LEAF_RUNTIME_ENTRY(intptr_t, DeoptimizeCopyFrame,
1696 1, uword saved_registers_address) { 1697 2,
1698 uword saved_registers_address,
1699 uword is_lazy_deopt) {
1697 Thread* thread = Thread::Current(); 1700 Thread* thread = Thread::Current();
1698 Isolate* isolate = thread->isolate(); 1701 Isolate* isolate = thread->isolate();
1699 StackZone zone(thread); 1702 StackZone zone(thread);
1700 HANDLESCOPE(thread); 1703 HANDLESCOPE(thread);
1701 1704
1702 // All registers have been saved below last-fp as if they were locals. 1705 // All registers have been saved below last-fp as if they were locals.
1703 const uword last_fp = saved_registers_address 1706 const uword last_fp = saved_registers_address
1704 + (kNumberOfCpuRegisters * kWordSize) 1707 + (kNumberOfCpuRegisters * kWordSize)
1705 + (kNumberOfFpuRegisters * kFpuRegisterSize) 1708 + (kNumberOfFpuRegisters * kFpuRegisterSize)
1706 - ((kFirstLocalSlotFromFp + 1) * kWordSize); 1709 - ((kFirstLocalSlotFromFp + 1) * kWordSize);
1707 1710
1708 // Get optimized code and frame that need to be deoptimized. 1711 // Get optimized code and frame that need to be deoptimized.
1709 DartFrameIterator iterator(last_fp); 1712 DartFrameIterator iterator(last_fp);
1710 StackFrame* caller_frame = iterator.NextFrame(); 1713 StackFrame* caller_frame = iterator.NextFrame();
1711 ASSERT(caller_frame != NULL); 1714 ASSERT(caller_frame != NULL);
1712 const Code& optimized_code = Code::Handle(caller_frame->LookupDartCode()); 1715 const Code& optimized_code = Code::Handle(caller_frame->LookupDartCode());
1713 ASSERT(optimized_code.is_optimized()); 1716 ASSERT(optimized_code.is_optimized());
1714 1717
1715 // Copy the saved registers from the stack. 1718 // Copy the saved registers from the stack.
1716 fpu_register_t* fpu_registers; 1719 fpu_register_t* fpu_registers;
1717 intptr_t* cpu_registers; 1720 intptr_t* cpu_registers;
1718 CopySavedRegisters(saved_registers_address, &fpu_registers, &cpu_registers); 1721 CopySavedRegisters(saved_registers_address, &fpu_registers, &cpu_registers);
1719 1722
1720 // Create the DeoptContext. 1723 // Create the DeoptContext.
1721 DeoptContext* deopt_context = 1724 DeoptContext* deopt_context =
1722 new DeoptContext(caller_frame, optimized_code, 1725 new DeoptContext(caller_frame,
1726 optimized_code,
1723 DeoptContext::kDestIsOriginalFrame, 1727 DeoptContext::kDestIsOriginalFrame,
1724 fpu_registers, cpu_registers); 1728 fpu_registers,
1729 cpu_registers,
1730 is_lazy_deopt != 0);
1725 isolate->set_deopt_context(deopt_context); 1731 isolate->set_deopt_context(deopt_context);
1726 1732
1727 // Stack size (FP - SP) in bytes. 1733 // Stack size (FP - SP) in bytes.
1728 return deopt_context->DestStackAdjustment() * kWordSize; 1734 return deopt_context->DestStackAdjustment() * kWordSize;
1729 } 1735 }
1730 END_LEAF_RUNTIME_ENTRY 1736 END_LEAF_RUNTIME_ENTRY
1731 1737
1732 1738
1733 // The stack has been adjusted to fit all values for unoptimized frame. 1739 // The stack has been adjusted to fit all values for unoptimized frame.
1734 // Fill the unoptimized frame. 1740 // Fill the unoptimized frame.
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after
1855 const intptr_t elm_size = old_data.ElementSizeInBytes(); 1861 const intptr_t elm_size = old_data.ElementSizeInBytes();
1856 const TypedData& new_data = 1862 const TypedData& new_data =
1857 TypedData::Handle(TypedData::New(cid, new_size, Heap::kOld)); 1863 TypedData::Handle(TypedData::New(cid, new_size, Heap::kOld));
1858 TypedData::Copy(new_data, 0, old_data, 0, old_size * elm_size); 1864 TypedData::Copy(new_data, 0, old_data, 0, old_size * elm_size);
1859 typed_data_cell.SetAt(0, new_data); 1865 typed_data_cell.SetAt(0, new_data);
1860 arguments.SetReturn(new_data); 1866 arguments.SetReturn(new_data);
1861 } 1867 }
1862 1868
1863 1869
1864 } // namespace dart 1870 } // namespace dart
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698