| OLD | NEW |
| (Empty) |
| 1 // 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 | |
| 34 namespace v8 { | |
| 35 namespace internal { | |
| 36 | |
| 37 | |
| 38 void JumpTarget::Jump(Result* arg) { | |
| 39 ASSERT(cgen()->has_valid_frame()); | |
| 40 | |
| 41 cgen()->frame()->Push(arg); | |
| 42 DoJump(); | |
| 43 } | |
| 44 | |
| 45 | |
| 46 void 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 | |
| 65 void 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 | |
| 90 void 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 | |
| 118 void 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 | |
| 127 void 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 | |
| 138 void 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 Isolate::Current()->counters()->compute_entry_frame()->Increment(); | |
| 144 #ifdef DEBUG | |
| 145 if (Isolate::Current()->jump_target_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 | |
| 332 FrameRegisterState::FrameRegisterState(VirtualFrame* frame) { | |
| 333 // Copy the register locations from the code generator's frame. | |
| 334 // These are the registers that will be spilled on entry to the | |
| 335 // deferred code and restored on exit. | |
| 336 int sp_offset = frame->fp_relative(frame->stack_pointer_); | |
| 337 for (int i = 0; i < RegisterAllocator::kNumRegisters; i++) { | |
| 338 int loc = frame->register_location(i); | |
| 339 if (loc == VirtualFrame::kIllegalIndex) { | |
| 340 registers_[i] = kIgnore; | |
| 341 } else if (frame->elements_[loc].is_synced()) { | |
| 342 // Needs to be restored on exit but not saved on entry. | |
| 343 registers_[i] = frame->fp_relative(loc) | kSyncedFlag; | |
| 344 } else { | |
| 345 int offset = frame->fp_relative(loc); | |
| 346 registers_[i] = (offset < sp_offset) ? kPush : offset; | |
| 347 } | |
| 348 } | |
| 349 } | |
| 350 | |
| 351 | |
| 352 void JumpTarget::Unuse() { | |
| 353 reaching_frames_.Clear(); | |
| 354 merge_labels_.Clear(); | |
| 355 entry_frame_ = NULL; | |
| 356 entry_label_.Unuse(); | |
| 357 } | |
| 358 | |
| 359 | |
| 360 void JumpTarget::AddReachingFrame(VirtualFrame* frame) { | |
| 361 ASSERT(reaching_frames_.length() == merge_labels_.length()); | |
| 362 ASSERT(entry_frame_ == NULL); | |
| 363 Label fresh; | |
| 364 merge_labels_.Add(fresh); | |
| 365 reaching_frames_.Add(frame); | |
| 366 } | |
| 367 | |
| 368 | |
| 369 // ------------------------------------------------------------------------- | |
| 370 // BreakTarget implementation. | |
| 371 | |
| 372 void BreakTarget::set_direction(Directionality direction) { | |
| 373 JumpTarget::set_direction(direction); | |
| 374 ASSERT(cgen()->has_valid_frame()); | |
| 375 expected_height_ = cgen()->frame()->height(); | |
| 376 } | |
| 377 | |
| 378 | |
| 379 void BreakTarget::CopyTo(BreakTarget* destination) { | |
| 380 ASSERT(destination != NULL); | |
| 381 destination->direction_ = direction_; | |
| 382 destination->reaching_frames_.Rewind(0); | |
| 383 destination->reaching_frames_.AddAll(reaching_frames_); | |
| 384 destination->merge_labels_.Rewind(0); | |
| 385 destination->merge_labels_.AddAll(merge_labels_); | |
| 386 destination->entry_frame_ = entry_frame_; | |
| 387 destination->entry_label_ = entry_label_; | |
| 388 destination->expected_height_ = expected_height_; | |
| 389 } | |
| 390 | |
| 391 | |
| 392 void BreakTarget::Branch(Condition cc, Hint hint) { | |
| 393 ASSERT(cgen()->has_valid_frame()); | |
| 394 | |
| 395 int count = cgen()->frame()->height() - expected_height_; | |
| 396 if (count > 0) { | |
| 397 // We negate and branch here rather than using DoBranch's negate | |
| 398 // and branch. This gives us a hook to remove statement state | |
| 399 // from the frame. | |
| 400 JumpTarget fall_through; | |
| 401 // Branch to fall through will not negate, because it is a | |
| 402 // forward-only target. | |
| 403 fall_through.Branch(NegateCondition(cc), NegateHint(hint)); | |
| 404 Jump(); // May emit merge code here. | |
| 405 fall_through.Bind(); | |
| 406 } else { | |
| 407 DoBranch(cc, hint); | |
| 408 } | |
| 409 } | |
| 410 | |
| 411 | |
| 412 DeferredCode::DeferredCode() | |
| 413 : masm_(CodeGeneratorScope::Current(Isolate::Current())->masm()), | |
| 414 statement_position_(masm_->positions_recorder()-> | |
| 415 current_statement_position()), | |
| 416 position_(masm_->positions_recorder()->current_position()), | |
| 417 frame_state_(CodeGeneratorScope::Current(Isolate::Current())->frame()) { | |
| 418 ASSERT(statement_position_ != RelocInfo::kNoPosition); | |
| 419 ASSERT(position_ != RelocInfo::kNoPosition); | |
| 420 | |
| 421 CodeGeneratorScope::Current(Isolate::Current())->AddDeferred(this); | |
| 422 #ifdef DEBUG | |
| 423 comment_ = ""; | |
| 424 #endif | |
| 425 } | |
| 426 | |
| 427 } } // namespace v8::internal | |
| OLD | NEW |