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