OLD | NEW |
1 // Copyright 2009 the V8 project authors. All rights reserved. | 1 // Copyright 2009 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 19 matching lines...) Expand all Loading... |
30 #include "codegen-inl.h" | 30 #include "codegen-inl.h" |
31 #include "register-allocator-inl.h" | 31 #include "register-allocator-inl.h" |
32 #include "virtual-frame-inl.h" | 32 #include "virtual-frame-inl.h" |
33 | 33 |
34 namespace v8 { | 34 namespace v8 { |
35 namespace internal { | 35 namespace internal { |
36 | 36 |
37 // ------------------------------------------------------------------------- | 37 // ------------------------------------------------------------------------- |
38 // VirtualFrame implementation. | 38 // VirtualFrame implementation. |
39 | 39 |
40 // Create a duplicate of an existing valid frame element. | |
41 // We can pass an optional number type information that will override the | |
42 // existing information about the backing element. The new information must | |
43 // not conflict with the existing type information and must be equally or | |
44 // more precise. The default parameter value kUninitialized means that there | |
45 // is no additional information. | |
46 FrameElement VirtualFrame::CopyElementAt(int index, NumberInfo info) { | |
47 ASSERT(index >= 0); | |
48 ASSERT(index < element_count()); | |
49 | |
50 FrameElement target = elements_[index]; | |
51 FrameElement result; | |
52 | |
53 switch (target.type()) { | |
54 case FrameElement::CONSTANT: | |
55 // We do not copy constants and instead return a fresh unsynced | |
56 // constant. | |
57 result = FrameElement::ConstantElement(target.handle(), | |
58 FrameElement::NOT_SYNCED); | |
59 break; | |
60 | |
61 case FrameElement::COPY: | |
62 // We do not allow copies of copies, so we follow one link to | |
63 // the actual backing store of a copy before making a copy. | |
64 index = target.index(); | |
65 ASSERT(elements_[index].is_memory() || elements_[index].is_register()); | |
66 // Fall through. | |
67 | |
68 case FrameElement::MEMORY: // Fall through. | |
69 case FrameElement::REGISTER: { | |
70 // All copies are backed by memory or register locations. | |
71 result.set_type(FrameElement::COPY); | |
72 result.clear_copied(); | |
73 result.clear_sync(); | |
74 result.set_index(index); | |
75 elements_[index].set_copied(); | |
76 // Update backing element's number information. | |
77 NumberInfo existing = elements_[index].number_info(); | |
78 ASSERT(!existing.IsUninitialized()); | |
79 // Assert that the new type information (a) does not conflict with the | |
80 // existing one and (b) is equally or more precise. | |
81 ASSERT((info.ToInt() & existing.ToInt()) == existing.ToInt()); | |
82 ASSERT((info.ToInt() | existing.ToInt()) == info.ToInt()); | |
83 | |
84 elements_[index].set_number_info(!info.IsUninitialized() | |
85 ? info | |
86 : existing); | |
87 break; | |
88 } | |
89 case FrameElement::INVALID: | |
90 // We should not try to copy invalid elements. | |
91 UNREACHABLE(); | |
92 break; | |
93 } | |
94 return result; | |
95 } | |
96 | |
97 | |
98 // Modify the state of the virtual frame to match the actual frame by adding | |
99 // extra in-memory elements to the top of the virtual frame. The extra | |
100 // elements will be externally materialized on the actual frame (eg, by | |
101 // pushing an exception handler). No code is emitted. | |
102 void VirtualFrame::Adjust(int count) { | |
103 ASSERT(count >= 0); | |
104 ASSERT(stack_pointer_ == element_count() - 1); | |
105 | |
106 for (int i = 0; i < count; i++) { | |
107 elements_.Add(FrameElement::MemoryElement(NumberInfo::Unknown())); | |
108 } | |
109 stack_pointer_ += count; | |
110 } | |
111 | |
112 | |
113 void VirtualFrame::ForgetElements(int count) { | |
114 ASSERT(count >= 0); | |
115 ASSERT(element_count() >= count); | |
116 | |
117 for (int i = 0; i < count; i++) { | |
118 FrameElement last = elements_.RemoveLast(); | |
119 if (last.is_register()) { | |
120 // A hack to properly count register references for the code | |
121 // generator's current frame and also for other frames. The | |
122 // same code appears in PrepareMergeTo. | |
123 if (cgen()->frame() == this) { | |
124 Unuse(last.reg()); | |
125 } else { | |
126 set_register_location(last.reg(), kIllegalIndex); | |
127 } | |
128 } | |
129 } | |
130 } | |
131 | |
132 | |
133 // If there are any registers referenced only by the frame, spill one. | 40 // If there are any registers referenced only by the frame, spill one. |
134 Register VirtualFrame::SpillAnyRegister() { | 41 Register VirtualFrame::SpillAnyRegister() { |
135 // Find the leftmost (ordered by register number) register whose only | 42 // Find the leftmost (ordered by register number) register whose only |
136 // reference is in the frame. | 43 // reference is in the frame. |
137 for (int i = 0; i < RegisterAllocator::kNumRegisters; i++) { | 44 for (int i = 0; i < RegisterAllocator::kNumRegisters; i++) { |
138 if (is_used(i) && cgen()->allocator()->count(i) == 1) { | 45 if (is_used(i) && cgen()->allocator()->count(i) == 1) { |
139 SpillElementAt(register_location(i)); | 46 SpillElementAt(register_location(i)); |
140 ASSERT(!cgen()->allocator()->is_used(i)); | 47 ASSERT(!cgen()->allocator()->is_used(i)); |
141 return RegisterAllocator::ToRegister(i); | 48 return RegisterAllocator::ToRegister(i); |
142 } | 49 } |
143 } | 50 } |
144 return no_reg; | 51 return no_reg; |
145 } | 52 } |
146 | 53 |
147 | 54 |
148 // Make the type of the element at a given index be MEMORY. | |
149 void VirtualFrame::SpillElementAt(int index) { | |
150 if (!elements_[index].is_valid()) return; | |
151 | |
152 SyncElementAt(index); | |
153 // Number type information is preserved. | |
154 // Copies get their number information from their backing element. | |
155 NumberInfo info; | |
156 if (!elements_[index].is_copy()) { | |
157 info = elements_[index].number_info(); | |
158 } else { | |
159 info = elements_[elements_[index].index()].number_info(); | |
160 } | |
161 // The element is now in memory. Its copied flag is preserved. | |
162 FrameElement new_element = FrameElement::MemoryElement(info); | |
163 if (elements_[index].is_copied()) { | |
164 new_element.set_copied(); | |
165 } | |
166 if (elements_[index].is_untagged_int32()) { | |
167 new_element.set_untagged_int32(true); | |
168 } | |
169 if (elements_[index].is_register()) { | |
170 Unuse(elements_[index].reg()); | |
171 } | |
172 elements_[index] = new_element; | |
173 } | |
174 | |
175 | |
176 // Clear the dirty bit for the element at a given index. | |
177 void VirtualFrame::SyncElementAt(int index) { | |
178 if (index <= stack_pointer_) { | |
179 if (!elements_[index].is_synced()) SyncElementBelowStackPointer(index); | |
180 } else if (index == stack_pointer_ + 1) { | |
181 SyncElementByPushing(index); | |
182 } else { | |
183 SyncRange(stack_pointer_ + 1, index); | |
184 } | |
185 } | |
186 | |
187 | |
188 // Make the type of all elements be MEMORY. | |
189 void VirtualFrame::SpillAll() { | |
190 for (int i = 0; i < element_count(); i++) { | |
191 SpillElementAt(i); | |
192 } | |
193 } | |
194 | |
195 | |
196 void VirtualFrame::PrepareMergeTo(VirtualFrame* expected) { | |
197 // Perform state changes on this frame that will make merge to the | |
198 // expected frame simpler or else increase the likelihood that his | |
199 // frame will match another. | |
200 for (int i = 0; i < element_count(); i++) { | |
201 FrameElement source = elements_[i]; | |
202 FrameElement target = expected->elements_[i]; | |
203 | |
204 if (!target.is_valid() || | |
205 (target.is_memory() && !source.is_memory() && source.is_synced())) { | |
206 // No code needs to be generated to invalidate valid elements. | |
207 // No code needs to be generated to move values to memory if | |
208 // they are already synced. We perform those moves here, before | |
209 // merging. | |
210 if (source.is_register()) { | |
211 // If the frame is the code generator's current frame, we have | |
212 // to decrement both the frame-internal and global register | |
213 // counts. | |
214 if (cgen()->frame() == this) { | |
215 Unuse(source.reg()); | |
216 } else { | |
217 set_register_location(source.reg(), kIllegalIndex); | |
218 } | |
219 } | |
220 elements_[i] = target; | |
221 } else if (target.is_register() && !target.is_synced() && | |
222 !source.is_memory()) { | |
223 // If an element's target is a register that doesn't need to be | |
224 // synced, and the element is not in memory, then the sync state | |
225 // of the element is irrelevant. We clear the sync bit. | |
226 ASSERT(source.is_valid()); | |
227 elements_[i].clear_sync(); | |
228 } | |
229 } | |
230 } | |
231 | |
232 | |
233 void VirtualFrame::PrepareForCall(int spilled_args, int dropped_args) { | |
234 ASSERT(height() >= dropped_args); | |
235 ASSERT(height() >= spilled_args); | |
236 ASSERT(dropped_args <= spilled_args); | |
237 | |
238 SyncRange(0, element_count() - 1); | |
239 // Spill registers. | |
240 for (int i = 0; i < RegisterAllocator::kNumRegisters; i++) { | |
241 if (is_used(i)) { | |
242 SpillElementAt(register_location(i)); | |
243 } | |
244 } | |
245 | |
246 // Spill the arguments. | |
247 for (int i = element_count() - spilled_args; i < element_count(); i++) { | |
248 if (!elements_[i].is_memory()) { | |
249 SpillElementAt(i); | |
250 } | |
251 } | |
252 | |
253 // Forget the frame elements that will be popped by the call. | |
254 Forget(dropped_args); | |
255 } | |
256 | |
257 | |
258 void VirtualFrame::PrepareForReturn() { | |
259 // Spill all locals. This is necessary to make sure all locals have | |
260 // the right value when breaking at the return site in the debugger. | |
261 for (int i = 0; i < expression_base_index(); i++) { | |
262 SpillElementAt(i); | |
263 } | |
264 } | |
265 | |
266 | |
267 void VirtualFrame::SetElementAt(int index, Result* value) { | |
268 int frame_index = element_count() - index - 1; | |
269 ASSERT(frame_index >= 0); | |
270 ASSERT(frame_index < element_count()); | |
271 ASSERT(value->is_valid()); | |
272 FrameElement original = elements_[frame_index]; | |
273 | |
274 // Early exit if the element is the same as the one being set. | |
275 bool same_register = original.is_register() | |
276 && value->is_register() | |
277 && original.reg().is(value->reg()); | |
278 bool same_constant = original.is_constant() | |
279 && value->is_constant() | |
280 && original.handle().is_identical_to(value->handle()); | |
281 if (same_register || same_constant) { | |
282 value->Unuse(); | |
283 return; | |
284 } | |
285 | |
286 InvalidateFrameSlotAt(frame_index); | |
287 | |
288 if (value->is_register()) { | |
289 if (is_used(value->reg())) { | |
290 // The register already appears on the frame. Either the existing | |
291 // register element, or the new element at frame_index, must be made | |
292 // a copy. | |
293 int i = register_location(value->reg()); | |
294 | |
295 if (i < frame_index) { | |
296 // The register FrameElement is lower in the frame than the new copy. | |
297 elements_[frame_index] = CopyElementAt(i); | |
298 } else { | |
299 // There was an early bailout for the case of setting a | |
300 // register element to itself. | |
301 ASSERT(i != frame_index); | |
302 elements_[frame_index] = elements_[i]; | |
303 elements_[i] = CopyElementAt(frame_index); | |
304 if (elements_[frame_index].is_synced()) { | |
305 elements_[i].set_sync(); | |
306 } | |
307 elements_[frame_index].clear_sync(); | |
308 set_register_location(value->reg(), frame_index); | |
309 for (int j = i + 1; j < element_count(); j++) { | |
310 if (elements_[j].is_copy() && elements_[j].index() == i) { | |
311 elements_[j].set_index(frame_index); | |
312 } | |
313 } | |
314 } | |
315 } else { | |
316 // The register value->reg() was not already used on the frame. | |
317 Use(value->reg(), frame_index); | |
318 elements_[frame_index] = | |
319 FrameElement::RegisterElement(value->reg(), | |
320 FrameElement::NOT_SYNCED, | |
321 value->number_info()); | |
322 } | |
323 } else { | |
324 ASSERT(value->is_constant()); | |
325 elements_[frame_index] = | |
326 FrameElement::ConstantElement(value->handle(), | |
327 FrameElement::NOT_SYNCED); | |
328 } | |
329 value->Unuse(); | |
330 } | |
331 | |
332 | |
333 // Specialization of List::ResizeAdd to non-inlined version for FrameElements. | 55 // Specialization of List::ResizeAdd to non-inlined version for FrameElements. |
334 // The function ResizeAdd becomes a real function, whose implementation is the | 56 // The function ResizeAdd becomes a real function, whose implementation is the |
335 // inlined ResizeAddInternal. | 57 // inlined ResizeAddInternal. |
336 template <> | 58 template <> |
337 void List<FrameElement, | 59 void List<FrameElement, |
338 FreeStoreAllocationPolicy>::ResizeAdd(const FrameElement& element) { | 60 FreeStoreAllocationPolicy>::ResizeAdd(const FrameElement& element) { |
339 ResizeAddInternal(element); | 61 ResizeAddInternal(element); |
340 } | 62 } |
341 | 63 |
342 } } // namespace v8::internal | 64 } } // namespace v8::internal |
OLD | NEW |