blob: 85620a2d96fadea5c44dd2a545262126777d60e5 [file] [log] [blame]
Steve Block6ded16b2010-05-10 14:33:55 +01001// Copyright 2010 the V8 project authors. All rights reserved.
2// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28#include "v8.h"
29
30#include "codegen-inl.h"
31#include "jump-target-inl.h"
32#include "register-allocator-inl.h"
33
34namespace v8 {
35namespace internal {
36
37
38void JumpTarget::Jump(Result* arg) {
39 ASSERT(cgen()->has_valid_frame());
40
41 cgen()->frame()->Push(arg);
42 DoJump();
43}
44
45
46void JumpTarget::Branch(Condition cc, Result* arg, Hint hint) {
47 ASSERT(cgen()->has_valid_frame());
48
49 // We want to check that non-frame registers at the call site stay in
50 // the same registers on the fall-through branch.
51#ifdef DEBUG
52 Result::Type arg_type = arg->type();
53 Register arg_reg = arg->is_register() ? arg->reg() : no_reg;
54#endif
55
56 cgen()->frame()->Push(arg);
57 DoBranch(cc, hint);
58 *arg = cgen()->frame()->Pop();
59
60 ASSERT(arg->type() == arg_type);
61 ASSERT(!arg->is_register() || arg->reg().is(arg_reg));
62}
63
64
65void JumpTarget::Branch(Condition cc, Result* arg0, Result* arg1, Hint hint) {
66 ASSERT(cgen()->has_valid_frame());
67
68 // We want to check that non-frame registers at the call site stay in
69 // the same registers on the fall-through branch.
70#ifdef DEBUG
71 Result::Type arg0_type = arg0->type();
72 Register arg0_reg = arg0->is_register() ? arg0->reg() : no_reg;
73 Result::Type arg1_type = arg1->type();
74 Register arg1_reg = arg1->is_register() ? arg1->reg() : no_reg;
75#endif
76
77 cgen()->frame()->Push(arg0);
78 cgen()->frame()->Push(arg1);
79 DoBranch(cc, hint);
80 *arg1 = cgen()->frame()->Pop();
81 *arg0 = cgen()->frame()->Pop();
82
83 ASSERT(arg0->type() == arg0_type);
84 ASSERT(!arg0->is_register() || arg0->reg().is(arg0_reg));
85 ASSERT(arg1->type() == arg1_type);
86 ASSERT(!arg1->is_register() || arg1->reg().is(arg1_reg));
87}
88
89
90void BreakTarget::Branch(Condition cc, Result* arg, Hint hint) {
91 ASSERT(cgen()->has_valid_frame());
92
93 int count = cgen()->frame()->height() - expected_height_;
94 if (count > 0) {
95 // We negate and branch here rather than using DoBranch's negate
96 // and branch. This gives us a hook to remove statement state
97 // from the frame.
98 JumpTarget fall_through;
99 // Branch to fall through will not negate, because it is a
100 // forward-only target.
101 fall_through.Branch(NegateCondition(cc), NegateHint(hint));
102 Jump(arg); // May emit merge code here.
103 fall_through.Bind();
104 } else {
105#ifdef DEBUG
106 Result::Type arg_type = arg->type();
107 Register arg_reg = arg->is_register() ? arg->reg() : no_reg;
108#endif
109 cgen()->frame()->Push(arg);
110 DoBranch(cc, hint);
111 *arg = cgen()->frame()->Pop();
112 ASSERT(arg->type() == arg_type);
113 ASSERT(!arg->is_register() || arg->reg().is(arg_reg));
114 }
115}
116
117
118void JumpTarget::Bind(Result* arg) {
119 if (cgen()->has_valid_frame()) {
120 cgen()->frame()->Push(arg);
121 }
122 DoBind();
123 *arg = cgen()->frame()->Pop();
124}
125
126
127void JumpTarget::Bind(Result* arg0, Result* arg1) {
128 if (cgen()->has_valid_frame()) {
129 cgen()->frame()->Push(arg0);
130 cgen()->frame()->Push(arg1);
131 }
132 DoBind();
133 *arg1 = cgen()->frame()->Pop();
134 *arg0 = cgen()->frame()->Pop();
135}
136
137
138void JumpTarget::ComputeEntryFrame() {
139 // Given: a collection of frames reaching by forward CFG edges and
140 // the directionality of the block. Compute: an entry frame for the
141 // block.
142
143 Counters::compute_entry_frame.Increment();
144#ifdef DEBUG
145 if (compiling_deferred_code_) {
146 ASSERT(reaching_frames_.length() > 1);
147 VirtualFrame* frame = reaching_frames_[0];
148 bool all_identical = true;
149 for (int i = 1; i < reaching_frames_.length(); i++) {
150 if (!frame->Equals(reaching_frames_[i])) {
151 all_identical = false;
152 break;
153 }
154 }
155 ASSERT(!all_identical || all_identical);
156 }
157#endif
158
159 // Choose an initial frame.
160 VirtualFrame* initial_frame = reaching_frames_[0];
161
162 // A list of pointers to frame elements in the entry frame. NULL
163 // indicates that the element has not yet been determined.
164 int length = initial_frame->element_count();
165 ZoneList<FrameElement*> elements(length);
166
167 // Initially populate the list of elements based on the initial
168 // frame.
169 for (int i = 0; i < length; i++) {
170 FrameElement element = initial_frame->elements_[i];
171 // We do not allow copies or constants in bidirectional frames.
172 if (direction_ == BIDIRECTIONAL) {
173 if (element.is_constant() || element.is_copy()) {
174 elements.Add(NULL);
175 continue;
176 }
177 }
178 elements.Add(&initial_frame->elements_[i]);
179 }
180
181 // Compute elements based on the other reaching frames.
182 if (reaching_frames_.length() > 1) {
183 for (int i = 0; i < length; i++) {
184 FrameElement* element = elements[i];
185 for (int j = 1; j < reaching_frames_.length(); j++) {
186 // Element computation is monotonic: new information will not
187 // change our decision about undetermined or invalid elements.
188 if (element == NULL || !element->is_valid()) break;
189
190 FrameElement* other = &reaching_frames_[j]->elements_[i];
191 element = element->Combine(other);
192 if (element != NULL && !element->is_copy()) {
193 ASSERT(other != NULL);
194 // We overwrite the number information of one of the incoming frames.
195 // This is safe because we only use the frame for emitting merge code.
196 // The number information of incoming frames is not used anymore.
197 element->set_type_info(TypeInfo::Combine(element->type_info(),
198 other->type_info()));
199 }
200 }
201 elements[i] = element;
202 }
203 }
204
205 // Build the new frame. A freshly allocated frame has memory elements
206 // for the parameters and some platform-dependent elements (e.g.,
207 // return address). Replace those first.
208 entry_frame_ = new VirtualFrame();
209 int index = 0;
210 for (; index < entry_frame_->element_count(); index++) {
211 FrameElement* target = elements[index];
212 // If the element is determined, set it now. Count registers. Mark
213 // elements as copied exactly when they have a copy. Undetermined
214 // elements are initially recorded as if in memory.
215 if (target != NULL) {
216 entry_frame_->elements_[index] = *target;
217 InitializeEntryElement(index, target);
218 }
219 }
220 // Then fill in the rest of the frame with new elements.
221 for (; index < length; index++) {
222 FrameElement* target = elements[index];
223 if (target == NULL) {
224 entry_frame_->elements_.Add(
225 FrameElement::MemoryElement(TypeInfo::Uninitialized()));
226 } else {
227 entry_frame_->elements_.Add(*target);
228 InitializeEntryElement(index, target);
229 }
230 }
231
232 // Allocate any still-undetermined frame elements to registers or
233 // memory, from the top down.
234 for (int i = length - 1; i >= 0; i--) {
235 if (elements[i] == NULL) {
236 // Loop over all the reaching frames to check whether the element
237 // is synced on all frames and to count the registers it occupies.
238 bool is_synced = true;
239 RegisterFile candidate_registers;
240 int best_count = kMinInt;
241 int best_reg_num = RegisterAllocator::kInvalidRegister;
242 TypeInfo info = TypeInfo::Uninitialized();
243
244 for (int j = 0; j < reaching_frames_.length(); j++) {
245 FrameElement element = reaching_frames_[j]->elements_[i];
246 if (direction_ == BIDIRECTIONAL) {
247 info = TypeInfo::Unknown();
248 } else if (!element.is_copy()) {
249 info = TypeInfo::Combine(info, element.type_info());
250 } else {
251 // New elements will not be copies, so get number information from
252 // backing element in the reaching frame.
253 info = TypeInfo::Combine(info,
254 reaching_frames_[j]->elements_[element.index()].type_info());
255 }
256 is_synced = is_synced && element.is_synced();
257 if (element.is_register() && !entry_frame_->is_used(element.reg())) {
258 // Count the register occurrence and remember it if better
259 // than the previous best.
260 int num = RegisterAllocator::ToNumber(element.reg());
261 candidate_registers.Use(num);
262 if (candidate_registers.count(num) > best_count) {
263 best_count = candidate_registers.count(num);
264 best_reg_num = num;
265 }
266 }
267 }
268
269 // We must have a number type information now (not for copied elements).
270 ASSERT(entry_frame_->elements_[i].is_copy()
271 || !info.IsUninitialized());
272
273 // If the value is synced on all frames, put it in memory. This
274 // costs nothing at the merge code but will incur a
275 // memory-to-register move when the value is needed later.
276 if (is_synced) {
277 // Already recorded as a memory element.
278 // Set combined number info.
279 entry_frame_->elements_[i].set_type_info(info);
280 continue;
281 }
282
283 // Try to put it in a register. If there was no best choice
284 // consider any free register.
285 if (best_reg_num == RegisterAllocator::kInvalidRegister) {
286 for (int j = 0; j < RegisterAllocator::kNumRegisters; j++) {
287 if (!entry_frame_->is_used(j)) {
288 best_reg_num = j;
289 break;
290 }
291 }
292 }
293
294 if (best_reg_num != RegisterAllocator::kInvalidRegister) {
295 // If there was a register choice, use it. Preserve the copied
296 // flag on the element.
297 bool is_copied = entry_frame_->elements_[i].is_copied();
298 Register reg = RegisterAllocator::ToRegister(best_reg_num);
299 entry_frame_->elements_[i] =
300 FrameElement::RegisterElement(reg, FrameElement::NOT_SYNCED,
301 TypeInfo::Uninitialized());
302 if (is_copied) entry_frame_->elements_[i].set_copied();
303 entry_frame_->set_register_location(reg, i);
304 }
305 // Set combined number info.
306 entry_frame_->elements_[i].set_type_info(info);
307 }
308 }
309
310 // If we have incoming backward edges assert we forget all number information.
311#ifdef DEBUG
312 if (direction_ == BIDIRECTIONAL) {
313 for (int i = 0; i < length; ++i) {
314 if (!entry_frame_->elements_[i].is_copy()) {
315 ASSERT(entry_frame_->elements_[i].type_info().IsUnknown());
316 }
317 }
318 }
319#endif
320
321 // The stack pointer is at the highest synced element or the base of
322 // the expression stack.
323 int stack_pointer = length - 1;
324 while (stack_pointer >= entry_frame_->expression_base_index() &&
325 !entry_frame_->elements_[stack_pointer].is_synced()) {
326 stack_pointer--;
327 }
328 entry_frame_->stack_pointer_ = stack_pointer;
329}
330
331
332DeferredCode::DeferredCode()
333 : masm_(CodeGeneratorScope::Current()->masm()),
334 statement_position_(masm_->current_statement_position()),
335 position_(masm_->current_position()) {
336 ASSERT(statement_position_ != RelocInfo::kNoPosition);
337 ASSERT(position_ != RelocInfo::kNoPosition);
338
339 CodeGeneratorScope::Current()->AddDeferred(this);
340#ifdef DEBUG
341 comment_ = "";
342#endif
343
344 // Copy the register locations from the code generator's frame.
345 // These are the registers that will be spilled on entry to the
346 // deferred code and restored on exit.
347 VirtualFrame* frame = CodeGeneratorScope::Current()->frame();
348 int sp_offset = frame->fp_relative(frame->stack_pointer_);
349 for (int i = 0; i < RegisterAllocator::kNumRegisters; i++) {
350 int loc = frame->register_location(i);
351 if (loc == VirtualFrame::kIllegalIndex) {
352 registers_[i] = kIgnore;
353 } else if (frame->elements_[loc].is_synced()) {
354 // Needs to be restored on exit but not saved on entry.
355 registers_[i] = frame->fp_relative(loc) | kSyncedFlag;
356 } else {
357 int offset = frame->fp_relative(loc);
358 registers_[i] = (offset < sp_offset) ? kPush : offset;
359 }
360 }
361}
362
363} } // namespace v8::internal