| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file |
| 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. |
| 4 |
| 5 #include "vm/weak_code.h" |
| 6 |
| 7 #include "platform/assert.h" |
| 8 |
| 9 #include "vm/code_generator.h" |
| 10 #include "vm/code_patcher.h" |
| 11 #include "vm/object.h" |
| 12 #include "vm/stack_frame.h" |
| 13 |
| 14 namespace dart { |
| 15 |
| 16 void WeakCodeReferences::Register(const Code& value) { |
| 17 if (!array_.IsNull()) { |
| 18 // Try to find and reuse cleared WeakProperty to avoid allocating new one. |
| 19 WeakProperty& weak_property = WeakProperty::Handle(); |
| 20 for (intptr_t i = 0; i < array_.Length(); i++) { |
| 21 weak_property ^= array_.At(i); |
| 22 if (weak_property.key() == Code::null()) { |
| 23 // Empty property found. Reuse it. |
| 24 weak_property.set_key(value); |
| 25 return; |
| 26 } |
| 27 } |
| 28 } |
| 29 |
| 30 const WeakProperty& weak_property = WeakProperty::Handle( |
| 31 WeakProperty::New(Heap::kOld)); |
| 32 weak_property.set_key(value); |
| 33 |
| 34 intptr_t length = array_.IsNull() ? 0 : array_.Length(); |
| 35 const Array& new_array = Array::Handle( |
| 36 Array::Grow(array_, length + 1, Heap::kOld)); |
| 37 new_array.SetAt(length, weak_property); |
| 38 UpdateArrayTo(new_array); |
| 39 } |
| 40 |
| 41 |
| 42 bool WeakCodeReferences::IsOptimizedCode(const Array& dependent_code, |
| 43 const Code& code) { |
| 44 if (!code.is_optimized()) { |
| 45 return false; |
| 46 } |
| 47 WeakProperty& weak_property = WeakProperty::Handle(); |
| 48 for (intptr_t i = 0; i < dependent_code.Length(); i++) { |
| 49 weak_property ^= dependent_code.At(i); |
| 50 if (code.raw() == weak_property.key()) { |
| 51 return true; |
| 52 } |
| 53 } |
| 54 return false; |
| 55 } |
| 56 |
| 57 |
| 58 void WeakCodeReferences::DisableCode() { |
| 59 const Array& code_objects = Array::Handle(array_.raw()); |
| 60 if (code_objects.IsNull()) { |
| 61 return; |
| 62 } |
| 63 UpdateArrayTo(Object::null_array()); |
| 64 // Disable all code on stack. |
| 65 Code& code = Code::Handle(); |
| 66 { |
| 67 DartFrameIterator iterator; |
| 68 StackFrame* frame = iterator.NextFrame(); |
| 69 while (frame != NULL) { |
| 70 code = frame->LookupDartCode(); |
| 71 if (IsOptimizedCode(code_objects, code)) { |
| 72 ReportDeoptimization(code); |
| 73 DeoptimizeAt(code, frame->pc()); |
| 74 } |
| 75 frame = iterator.NextFrame(); |
| 76 } |
| 77 } |
| 78 |
| 79 // Switch functions that use dependent code to unoptimized code. |
| 80 WeakProperty& weak_property = WeakProperty::Handle(); |
| 81 Object& owner = Object::Handle(); |
| 82 Function& function = Function::Handle(); |
| 83 for (intptr_t i = 0; i < code_objects.Length(); i++) { |
| 84 weak_property ^= code_objects.At(i); |
| 85 code ^= weak_property.key(); |
| 86 if (code.IsNull()) { |
| 87 // Code was garbage collected already. |
| 88 continue; |
| 89 } |
| 90 owner = code.owner(); |
| 91 if (owner.IsFunction()) { |
| 92 function ^= owner.raw(); |
| 93 } else if (owner.IsClass()) { |
| 94 Class& cls = Class::Handle(); |
| 95 cls ^= owner.raw(); |
| 96 OS::Print("Skipping code owned by class %s\n", cls.ToCString()); |
| 97 cls.SwitchAllocationStub(); |
| 98 continue; |
| 99 } else if (owner.IsNull()) { |
| 100 OS::Print("Skipping code owned by null: "); |
| 101 code.Print(); |
| 102 continue; |
| 103 } |
| 104 |
| 105 // If function uses dependent code switch it to unoptimized. |
| 106 if (code.is_optimized() && (function.CurrentCode() == code.raw())) { |
| 107 ReportSwitchingCode(code); |
| 108 function.SwitchToUnoptimizedCode(); |
| 109 } else if (function.unoptimized_code() == code.raw()) { |
| 110 ReportSwitchingCode(code); |
| 111 function.ClearICData(); |
| 112 // Remove the code object from the function. The next time the |
| 113 // function is invoked, it will be compiled again. |
| 114 function.ClearCode(); |
| 115 // Invalidate the old code object so existing references to it |
| 116 // (from optimized code) will fail when invoked. |
| 117 if (!CodePatcher::IsEntryPatched(code)) { |
| 118 CodePatcher::PatchEntry(code); |
| 119 } |
| 120 } else { |
| 121 // Make non-OSR code non-entrant. |
| 122 if (code.GetEntryPatchPc() != 0) { |
| 123 if (!CodePatcher::IsEntryPatched(code)) { |
| 124 ReportSwitchingCode(code); |
| 125 CodePatcher::PatchEntry(code); |
| 126 } |
| 127 } |
| 128 } |
| 129 } |
| 130 } |
| 131 |
| 132 } // namespace dart |
| OLD | NEW |