kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1 | // Copyright 2008 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 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 30 | #include "codegen-inl.h" |
| 31 | #include "register-allocator-inl.h" |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 32 | |
| 33 | namespace v8 { namespace internal { |
| 34 | |
| 35 | // ------------------------------------------------------------------------- |
| 36 | // JumpTarget implementation. |
| 37 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 38 | #define __ ACCESS_MASM(masm_) |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 39 | |
kasperl@chromium.org | 8ccb0be | 2009-04-07 07:21:39 +0000 | [diff] [blame] | 40 | void JumpTarget::DoJump() { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 41 | ASSERT(cgen_ != NULL); |
| 42 | ASSERT(cgen_->has_valid_frame()); |
| 43 | // Live non-frame registers are not allowed at unconditional jumps |
| 44 | // because we have no way of invalidating the corresponding results |
| 45 | // which are still live in the C++ code. |
| 46 | ASSERT(cgen_->HasValidEntryRegisters()); |
| 47 | |
| 48 | if (is_bound()) { |
| 49 | // Backward jump. There is an expected frame to merge to. |
| 50 | ASSERT(direction_ == BIDIRECTIONAL); |
| 51 | cgen_->frame()->MergeTo(entry_frame_); |
| 52 | cgen_->DeleteFrame(); |
| 53 | __ jmp(&entry_label_); |
| 54 | } else { |
| 55 | // Forward jump. The current frame is added to the end of the list |
| 56 | // of frames reaching the target block and a jump to the merge code |
| 57 | // is emitted. |
| 58 | AddReachingFrame(cgen_->frame()); |
| 59 | RegisterFile empty; |
| 60 | cgen_->SetFrame(NULL, &empty); |
| 61 | __ jmp(&merge_labels_.last()); |
| 62 | } |
| 63 | |
| 64 | is_linked_ = !is_bound_; |
| 65 | } |
| 66 | |
| 67 | |
kasperl@chromium.org | 8ccb0be | 2009-04-07 07:21:39 +0000 | [diff] [blame] | 68 | void JumpTarget::DoBranch(Condition cc, Hint ignored) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 69 | ASSERT(cgen_ != NULL); |
| 70 | ASSERT(cgen_->has_valid_frame()); |
| 71 | |
| 72 | if (is_bound()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 73 | ASSERT(direction_ == BIDIRECTIONAL); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 74 | // Backward branch. We have an expected frame to merge to on the |
| 75 | // backward edge. |
| 76 | |
| 77 | // Swap the current frame for a copy (we do the swapping to get |
| 78 | // the off-frame registers off the fall through) to use for the |
| 79 | // branch. |
| 80 | VirtualFrame* fall_through_frame = cgen_->frame(); |
| 81 | VirtualFrame* branch_frame = new VirtualFrame(fall_through_frame); |
| 82 | RegisterFile non_frame_registers = RegisterAllocator::Reserved(); |
| 83 | cgen_->SetFrame(branch_frame, &non_frame_registers); |
| 84 | |
| 85 | // Check if we can avoid merge code. |
| 86 | cgen_->frame()->PrepareMergeTo(entry_frame_); |
| 87 | if (cgen_->frame()->Equals(entry_frame_)) { |
| 88 | // Branch right in to the block. |
| 89 | cgen_->DeleteFrame(); |
| 90 | __ b(cc, &entry_label_); |
| 91 | cgen_->SetFrame(fall_through_frame, &non_frame_registers); |
| 92 | return; |
| 93 | } |
| 94 | |
| 95 | // Check if we can reuse existing merge code. |
| 96 | for (int i = 0; i < reaching_frames_.length(); i++) { |
| 97 | if (reaching_frames_[i] != NULL && |
| 98 | cgen_->frame()->Equals(reaching_frames_[i])) { |
| 99 | // Branch to the merge code. |
| 100 | cgen_->DeleteFrame(); |
| 101 | __ b(cc, &merge_labels_[i]); |
| 102 | cgen_->SetFrame(fall_through_frame, &non_frame_registers); |
| 103 | return; |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | // To emit the merge code here, we negate the condition and branch |
| 108 | // around the merge code on the fall through path. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 109 | Label original_fall_through; |
| 110 | __ b(NegateCondition(cc), &original_fall_through); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 111 | cgen_->frame()->MergeTo(entry_frame_); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 112 | cgen_->DeleteFrame(); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 113 | __ b(&entry_label_); |
| 114 | cgen_->SetFrame(fall_through_frame, &non_frame_registers); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 115 | __ bind(&original_fall_through); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 116 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 117 | } else { |
| 118 | // Forward branch. A copy of the current frame is added to the end |
| 119 | // of the list of frames reaching the target block and a branch to |
| 120 | // the merge code is emitted. |
| 121 | AddReachingFrame(new VirtualFrame(cgen_->frame())); |
| 122 | __ b(cc, &merge_labels_.last()); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 123 | is_linked_ = true; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 124 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | |
| 128 | void JumpTarget::Call() { |
| 129 | // Call is used to push the address of the catch block on the stack as |
| 130 | // a return address when compiling try/catch and try/finally. We |
| 131 | // fully spill the frame before making the call. The expected frame |
| 132 | // at the label (which should be the only one) is the spilled current |
| 133 | // frame plus an in-memory return address. The "fall-through" frame |
| 134 | // at the return site is the spilled current frame. |
| 135 | ASSERT(cgen_ != NULL); |
| 136 | ASSERT(cgen_->has_valid_frame()); |
| 137 | // There are no non-frame references across the call. |
| 138 | ASSERT(cgen_->HasValidEntryRegisters()); |
| 139 | ASSERT(!is_linked()); |
| 140 | |
| 141 | cgen_->frame()->SpillAll(); |
| 142 | VirtualFrame* target_frame = new VirtualFrame(cgen_->frame()); |
| 143 | target_frame->Adjust(1); |
| 144 | AddReachingFrame(target_frame); |
| 145 | __ bl(&merge_labels_.last()); |
| 146 | |
| 147 | is_linked_ = !is_bound_; |
| 148 | } |
| 149 | |
| 150 | |
kasperl@chromium.org | 8ccb0be | 2009-04-07 07:21:39 +0000 | [diff] [blame] | 151 | void JumpTarget::DoBind(int mergable_elements) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 152 | ASSERT(cgen_ != NULL); |
| 153 | ASSERT(!is_bound()); |
| 154 | |
| 155 | // Live non-frame registers are not allowed at the start of a basic |
| 156 | // block. |
| 157 | ASSERT(!cgen_->has_valid_frame() || cgen_->HasValidEntryRegisters()); |
| 158 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 159 | if (direction_ == FORWARD_ONLY) { |
| 160 | // A simple case: no forward jumps and no possible backward jumps. |
| 161 | if (!is_linked()) { |
| 162 | // The stack pointer can be floating above the top of the |
| 163 | // virtual frame before the bind. Afterward, it should not. |
| 164 | ASSERT(cgen_->has_valid_frame()); |
| 165 | VirtualFrame* frame = cgen_->frame(); |
| 166 | int difference = |
| 167 | frame->stack_pointer_ - (frame->elements_.length() - 1); |
| 168 | if (difference > 0) { |
| 169 | frame->stack_pointer_ -= difference; |
| 170 | __ add(sp, sp, Operand(difference * kPointerSize)); |
| 171 | } |
| 172 | |
| 173 | is_bound_ = true; |
| 174 | return; |
| 175 | } |
| 176 | |
| 177 | // Another simple case: no fall through, a single forward jump, |
| 178 | // and no possible backward jumps. |
| 179 | if (!cgen_->has_valid_frame() && reaching_frames_.length() == 1) { |
| 180 | // Pick up the only reaching frame, take ownership of it, and |
| 181 | // use it for the block about to be emitted. |
| 182 | VirtualFrame* frame = reaching_frames_[0]; |
| 183 | RegisterFile reserved = RegisterAllocator::Reserved(); |
| 184 | cgen_->SetFrame(frame, &reserved); |
| 185 | reaching_frames_[0] = NULL; |
| 186 | __ bind(&merge_labels_[0]); |
| 187 | |
| 188 | // The stack pointer can be floating above the top of the |
| 189 | // virtual frame before the bind. Afterward, it should not. |
| 190 | int difference = |
| 191 | frame->stack_pointer_ - (frame->elements_.length() - 1); |
| 192 | if (difference > 0) { |
| 193 | frame->stack_pointer_ -= difference; |
| 194 | __ add(sp, sp, Operand(difference * kPointerSize)); |
| 195 | } |
| 196 | |
| 197 | is_linked_ = false; |
| 198 | is_bound_ = true; |
| 199 | return; |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | // If there is a current frame, record it as the fall-through. It |
| 204 | // is owned by the reaching frames for now. |
| 205 | bool had_fall_through = false; |
| 206 | if (cgen_->has_valid_frame()) { |
| 207 | had_fall_through = true; |
| 208 | AddReachingFrame(cgen_->frame()); |
| 209 | RegisterFile empty; |
| 210 | cgen_->SetFrame(NULL, &empty); |
| 211 | } |
| 212 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 213 | // Compute the frame to use for entry to the block. |
| 214 | ComputeEntryFrame(mergable_elements); |
| 215 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 216 | // Some moves required to merge to an expected frame require purely |
| 217 | // frame state changes, and do not require any code generation. |
| 218 | // Perform those first to increase the possibility of finding equal |
| 219 | // frames below. |
| 220 | for (int i = 0; i < reaching_frames_.length(); i++) { |
| 221 | if (reaching_frames_[i] != NULL) { |
| 222 | reaching_frames_[i]->PrepareMergeTo(entry_frame_); |
| 223 | } |
| 224 | } |
| 225 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 226 | if (is_linked()) { |
| 227 | // There were forward jumps. Handle merging the reaching frames |
| 228 | // and possible fall through to the entry frame. |
| 229 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 230 | // Loop over the (non-null) reaching frames and process any that |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 231 | // need merge code. Iterate backwards through the list to handle |
| 232 | // the fall-through frame first. Set frames that will be |
| 233 | // processed after 'i' to NULL if we want to avoid processing |
| 234 | // them. |
| 235 | for (int i = reaching_frames_.length() - 1; i >= 0; i--) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 236 | VirtualFrame* frame = reaching_frames_[i]; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 237 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 238 | if (frame != NULL) { |
| 239 | // Does the frame (probably) need merge code? |
| 240 | if (!frame->Equals(entry_frame_)) { |
| 241 | // We could have a valid frame as the fall through to the |
| 242 | // binding site or as the fall through from a previous merge |
| 243 | // code block. Jump around the code we are about to |
| 244 | // generate. |
| 245 | if (cgen_->has_valid_frame()) { |
| 246 | cgen_->DeleteFrame(); |
| 247 | __ b(&entry_label_); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 248 | } |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 249 | // Pick up the frame for this block. Assume ownership if |
| 250 | // there cannot be backward jumps. |
| 251 | RegisterFile reserved = RegisterAllocator::Reserved(); |
| 252 | if (direction_ == BIDIRECTIONAL) { |
| 253 | cgen_->SetFrame(new VirtualFrame(frame), &reserved); |
| 254 | } else { |
| 255 | cgen_->SetFrame(frame, &reserved); |
| 256 | reaching_frames_[i] = NULL; |
| 257 | } |
| 258 | __ bind(&merge_labels_[i]); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 259 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 260 | // Loop over the remaining (non-null) reaching frames, |
| 261 | // looking for any that can share merge code with this one. |
| 262 | for (int j = 0; j < i; j++) { |
| 263 | VirtualFrame* other = reaching_frames_[j]; |
| 264 | if (other != NULL && other->Equals(cgen_->frame())) { |
| 265 | // Set the reaching frame element to null to avoid |
| 266 | // processing it later, and then bind its entry label. |
| 267 | delete other; |
| 268 | reaching_frames_[j] = NULL; |
| 269 | __ bind(&merge_labels_[j]); |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | // Emit the merge code. |
| 274 | cgen_->frame()->MergeTo(entry_frame_); |
| 275 | } else if (i == reaching_frames_.length() - 1 && had_fall_through) { |
| 276 | // If this is the fall through, and it didn't need merge |
| 277 | // code, we need to pick up the frame so we can jump around |
| 278 | // subsequent merge blocks if necessary. |
| 279 | RegisterFile reserved = RegisterAllocator::Reserved(); |
| 280 | cgen_->SetFrame(frame, &reserved); |
| 281 | reaching_frames_[i] = NULL; |
| 282 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 283 | } |
| 284 | } |
| 285 | |
| 286 | // The code generator may not have a current frame if there was no |
| 287 | // fall through and none of the reaching frames needed merging. |
| 288 | // In that case, clone the entry frame as the current frame. |
| 289 | if (!cgen_->has_valid_frame()) { |
| 290 | RegisterFile reserved_registers = RegisterAllocator::Reserved(); |
| 291 | cgen_->SetFrame(new VirtualFrame(entry_frame_), &reserved_registers); |
| 292 | } |
| 293 | |
| 294 | // There is certainly a current frame equal to the entry frame. |
| 295 | // Bind the entry frame label. |
| 296 | __ bind(&entry_label_); |
| 297 | |
| 298 | // There may be unprocessed reaching frames that did not need |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 299 | // merge code. They will have unbound merge labels. Bind their |
| 300 | // merge labels to be the same as the entry label and deallocate |
| 301 | // them. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 302 | for (int i = 0; i < reaching_frames_.length(); i++) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 303 | if (!merge_labels_[i].is_bound()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 304 | delete reaching_frames_[i]; |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 305 | reaching_frames_[i] = NULL; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 306 | __ bind(&merge_labels_[i]); |
| 307 | } |
| 308 | } |
| 309 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 310 | // There are non-NULL reaching frames with bound labels for each |
| 311 | // merge block, but only on backward targets. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 312 | } else { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 313 | // There were no forward jumps. There must be a current frame and |
| 314 | // this must be a bidirectional target. |
| 315 | ASSERT(reaching_frames_.length() == 1); |
| 316 | ASSERT(reaching_frames_[0] != NULL); |
| 317 | ASSERT(direction_ == BIDIRECTIONAL); |
| 318 | |
| 319 | // Use a copy of the reaching frame so the original can be saved |
| 320 | // for possible reuse as a backward merge block. |
| 321 | RegisterFile reserved = RegisterAllocator::Reserved(); |
| 322 | cgen_->SetFrame(new VirtualFrame(reaching_frames_[0]), &reserved); |
| 323 | __ bind(&merge_labels_[0]); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 324 | cgen_->frame()->MergeTo(entry_frame_); |
| 325 | __ bind(&entry_label_); |
| 326 | } |
| 327 | |
| 328 | is_linked_ = false; |
| 329 | is_bound_ = true; |
| 330 | } |
| 331 | |
| 332 | #undef __ |
| 333 | |
| 334 | |
| 335 | } } // namespace v8::internal |