sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1 | // Copyright 2010 the V8 project authors. All rights reserved. |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 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 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 28 | #include "v8.h" |
| 29 | |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 30 | #if defined(V8_TARGET_ARCH_X64) |
| 31 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 32 | #include "codegen-inl.h" |
| 33 | #include "register-allocator-inl.h" |
| 34 | #include "scopes.h" |
sgjesse@chromium.org | 833cdd7 | 2010-02-26 10:06:16 +0000 | [diff] [blame] | 35 | #include "virtual-frame-inl.h" |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 36 | |
| 37 | namespace v8 { |
| 38 | namespace internal { |
| 39 | |
| 40 | #define __ ACCESS_MASM(masm()) |
| 41 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 42 | void VirtualFrame::Enter() { |
| 43 | // Registers live on entry to a JS frame: |
| 44 | // rsp: stack pointer, points to return address from this function. |
| 45 | // rbp: base pointer, points to previous JS, ArgumentsAdaptor, or |
| 46 | // Trampoline frame. |
| 47 | // rsi: context of this function call. |
| 48 | // rdi: pointer to this function object. |
| 49 | Comment cmnt(masm(), "[ Enter JS frame"); |
| 50 | |
| 51 | #ifdef DEBUG |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 52 | if (FLAG_debug_code) { |
| 53 | // Verify that rdi contains a JS function. The following code |
| 54 | // relies on rax being available for use. |
| 55 | Condition not_smi = NegateCondition(masm()->CheckSmi(rdi)); |
| 56 | __ Check(not_smi, |
| 57 | "VirtualFrame::Enter - rdi is not a function (smi check)."); |
| 58 | __ CmpObjectType(rdi, JS_FUNCTION_TYPE, rax); |
| 59 | __ Check(equal, |
| 60 | "VirtualFrame::Enter - rdi is not a function (map check)."); |
| 61 | } |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 62 | #endif |
| 63 | |
| 64 | EmitPush(rbp); |
| 65 | |
| 66 | __ movq(rbp, rsp); |
| 67 | |
| 68 | // Store the context in the frame. The context is kept in rsi and a |
| 69 | // copy is stored in the frame. The external reference to rsi |
| 70 | // remains. |
| 71 | EmitPush(rsi); |
| 72 | |
| 73 | // Store the function in the frame. The frame owns the register |
| 74 | // reference now (ie, it can keep it in rdi or spill it later). |
| 75 | Push(rdi); |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 76 | SyncElementAt(element_count() - 1); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 77 | cgen()->allocator()->Unuse(rdi); |
| 78 | } |
| 79 | |
| 80 | |
| 81 | void VirtualFrame::Exit() { |
| 82 | Comment cmnt(masm(), "[ Exit JS frame"); |
| 83 | // Record the location of the JS exit code for patching when setting |
| 84 | // break point. |
| 85 | __ RecordJSReturn(); |
| 86 | |
| 87 | // Avoid using the leave instruction here, because it is too |
| 88 | // short. We need the return sequence to be a least the size of a |
| 89 | // call instruction to support patching the exit code in the |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 90 | // debugger. See GenerateReturnSequence for the full return sequence. |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 91 | // TODO(X64): A patched call will be very long now. Make sure we |
| 92 | // have enough room. |
| 93 | __ movq(rsp, rbp); |
| 94 | stack_pointer_ = frame_pointer(); |
| 95 | for (int i = element_count() - 1; i > stack_pointer_; i--) { |
| 96 | FrameElement last = elements_.RemoveLast(); |
| 97 | if (last.is_register()) { |
| 98 | Unuse(last.reg()); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | EmitPop(rbp); |
| 103 | } |
| 104 | |
| 105 | |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 106 | void VirtualFrame::AllocateStackSlots() { |
| 107 | int count = local_count(); |
| 108 | if (count > 0) { |
| 109 | Comment cmnt(masm(), "[ Allocate space for locals"); |
| 110 | // The locals are initialized to a constant (the undefined value), but |
| 111 | // we sync them with the actual frame to allocate space for spilling |
| 112 | // them later. First sync everything above the stack pointer so we can |
| 113 | // use pushes to allocate and initialize the locals. |
| 114 | SyncRange(stack_pointer_ + 1, element_count() - 1); |
| 115 | Handle<Object> undefined = Factory::undefined_value(); |
| 116 | FrameElement initial_value = |
| 117 | FrameElement::ConstantElement(undefined, FrameElement::SYNCED); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 118 | if (count == 1) { |
| 119 | __ Push(undefined); |
| 120 | } else if (count < kLocalVarBound) { |
| 121 | // For less locals the unrolled loop is more compact. |
| 122 | __ movq(kScratchRegister, undefined, RelocInfo::EMBEDDED_OBJECT); |
| 123 | for (int i = 0; i < count; i++) { |
| 124 | __ push(kScratchRegister); |
| 125 | } |
| 126 | } else { |
| 127 | // For more locals a loop in generated code is more compact. |
| 128 | Label alloc_locals_loop; |
| 129 | Result cnt = cgen()->allocator()->Allocate(); |
| 130 | ASSERT(cnt.is_valid()); |
| 131 | __ movq(cnt.reg(), Immediate(count)); |
| 132 | __ movq(kScratchRegister, undefined, RelocInfo::EMBEDDED_OBJECT); |
| 133 | __ bind(&alloc_locals_loop); |
| 134 | __ push(kScratchRegister); |
| 135 | __ decl(cnt.reg()); |
| 136 | __ j(not_zero, &alloc_locals_loop); |
| 137 | } |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 138 | for (int i = 0; i < count; i++) { |
| 139 | elements_.Add(initial_value); |
| 140 | stack_pointer_++; |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 141 | } |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | |
| 146 | void VirtualFrame::SaveContextRegister() { |
| 147 | ASSERT(elements_[context_index()].is_memory()); |
| 148 | __ movq(Operand(rbp, fp_relative(context_index())), rsi); |
| 149 | } |
| 150 | |
| 151 | |
| 152 | void VirtualFrame::RestoreContextRegister() { |
| 153 | ASSERT(elements_[context_index()].is_memory()); |
| 154 | __ movq(rsi, Operand(rbp, fp_relative(context_index()))); |
| 155 | } |
| 156 | |
| 157 | |
| 158 | void VirtualFrame::PushReceiverSlotAddress() { |
| 159 | Result temp = cgen()->allocator()->Allocate(); |
| 160 | ASSERT(temp.is_valid()); |
| 161 | __ lea(temp.reg(), ParameterAt(-1)); |
| 162 | Push(&temp); |
| 163 | } |
| 164 | |
| 165 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 166 | void VirtualFrame::EmitPop(Register reg) { |
| 167 | ASSERT(stack_pointer_ == element_count() - 1); |
| 168 | stack_pointer_--; |
| 169 | elements_.RemoveLast(); |
| 170 | __ pop(reg); |
| 171 | } |
| 172 | |
| 173 | |
| 174 | void VirtualFrame::EmitPop(const Operand& operand) { |
| 175 | ASSERT(stack_pointer_ == element_count() - 1); |
| 176 | stack_pointer_--; |
| 177 | elements_.RemoveLast(); |
| 178 | __ pop(operand); |
| 179 | } |
| 180 | |
| 181 | |
sgjesse@chromium.org | df7a284 | 2010-03-25 14:34:15 +0000 | [diff] [blame] | 182 | void VirtualFrame::EmitPush(Register reg, TypeInfo info) { |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 183 | ASSERT(stack_pointer_ == element_count() - 1); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 184 | elements_.Add(FrameElement::MemoryElement(info)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 185 | stack_pointer_++; |
| 186 | __ push(reg); |
| 187 | } |
| 188 | |
| 189 | |
sgjesse@chromium.org | df7a284 | 2010-03-25 14:34:15 +0000 | [diff] [blame] | 190 | void VirtualFrame::EmitPush(const Operand& operand, TypeInfo info) { |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 191 | ASSERT(stack_pointer_ == element_count() - 1); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 192 | elements_.Add(FrameElement::MemoryElement(info)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 193 | stack_pointer_++; |
| 194 | __ push(operand); |
| 195 | } |
| 196 | |
| 197 | |
sgjesse@chromium.org | df7a284 | 2010-03-25 14:34:15 +0000 | [diff] [blame] | 198 | void VirtualFrame::EmitPush(Immediate immediate, TypeInfo info) { |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 199 | ASSERT(stack_pointer_ == element_count() - 1); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 200 | elements_.Add(FrameElement::MemoryElement(info)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 201 | stack_pointer_++; |
| 202 | __ push(immediate); |
| 203 | } |
| 204 | |
| 205 | |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 206 | void VirtualFrame::EmitPush(Smi* smi_value) { |
| 207 | ASSERT(stack_pointer_ == element_count() - 1); |
sgjesse@chromium.org | df7a284 | 2010-03-25 14:34:15 +0000 | [diff] [blame] | 208 | elements_.Add(FrameElement::MemoryElement(TypeInfo::Smi())); |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 209 | stack_pointer_++; |
| 210 | __ Push(smi_value); |
| 211 | } |
| 212 | |
| 213 | |
ager@chromium.org | 3e87580 | 2009-06-29 08:26:34 +0000 | [diff] [blame] | 214 | void VirtualFrame::EmitPush(Handle<Object> value) { |
| 215 | ASSERT(stack_pointer_ == element_count() - 1); |
sgjesse@chromium.org | df7a284 | 2010-03-25 14:34:15 +0000 | [diff] [blame] | 216 | TypeInfo info = TypeInfo::TypeFromValue(value); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 217 | elements_.Add(FrameElement::MemoryElement(info)); |
ager@chromium.org | 3e87580 | 2009-06-29 08:26:34 +0000 | [diff] [blame] | 218 | stack_pointer_++; |
| 219 | __ Push(value); |
| 220 | } |
| 221 | |
| 222 | |
sgjesse@chromium.org | df7a284 | 2010-03-25 14:34:15 +0000 | [diff] [blame] | 223 | void VirtualFrame::EmitPush(Heap::RootListIndex index, TypeInfo info) { |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 224 | ASSERT(stack_pointer_ == element_count() - 1); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 225 | elements_.Add(FrameElement::MemoryElement(info)); |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 226 | stack_pointer_++; |
| 227 | __ PushRoot(index); |
| 228 | } |
| 229 | |
| 230 | |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 231 | void VirtualFrame::Push(Expression* expr) { |
| 232 | ASSERT(expr->IsTrivial()); |
| 233 | |
| 234 | Literal* lit = expr->AsLiteral(); |
| 235 | if (lit != NULL) { |
| 236 | Push(lit->handle()); |
| 237 | return; |
| 238 | } |
| 239 | |
| 240 | VariableProxy* proxy = expr->AsVariableProxy(); |
| 241 | if (proxy != NULL) { |
| 242 | Slot* slot = proxy->var()->slot(); |
| 243 | if (slot->type() == Slot::LOCAL) { |
| 244 | PushLocalAt(slot->index()); |
| 245 | return; |
| 246 | } |
| 247 | if (slot->type() == Slot::PARAMETER) { |
| 248 | PushParameterAt(slot->index()); |
| 249 | return; |
| 250 | } |
| 251 | } |
| 252 | UNREACHABLE(); |
| 253 | } |
| 254 | |
| 255 | |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 256 | void VirtualFrame::Drop(int count) { |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 257 | ASSERT(count >= 0); |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 258 | ASSERT(height() >= count); |
| 259 | int num_virtual_elements = (element_count() - 1) - stack_pointer_; |
| 260 | |
| 261 | // Emit code to lower the stack pointer if necessary. |
| 262 | if (num_virtual_elements < count) { |
| 263 | int num_dropped = count - num_virtual_elements; |
| 264 | stack_pointer_ -= num_dropped; |
| 265 | __ addq(rsp, Immediate(num_dropped * kPointerSize)); |
| 266 | } |
| 267 | |
| 268 | // Discard elements from the virtual frame and free any registers. |
| 269 | for (int i = 0; i < count; i++) { |
| 270 | FrameElement dropped = elements_.RemoveLast(); |
| 271 | if (dropped.is_register()) { |
| 272 | Unuse(dropped.reg()); |
| 273 | } |
| 274 | } |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 275 | } |
| 276 | |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 277 | |
| 278 | int VirtualFrame::InvalidateFrameSlotAt(int index) { |
| 279 | FrameElement original = elements_[index]; |
| 280 | |
| 281 | // Is this element the backing store of any copies? |
| 282 | int new_backing_index = kIllegalIndex; |
| 283 | if (original.is_copied()) { |
| 284 | // Verify it is copied, and find first copy. |
| 285 | for (int i = index + 1; i < element_count(); i++) { |
| 286 | if (elements_[i].is_copy() && elements_[i].index() == index) { |
| 287 | new_backing_index = i; |
| 288 | break; |
| 289 | } |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | if (new_backing_index == kIllegalIndex) { |
| 294 | // No copies found, return kIllegalIndex. |
| 295 | if (original.is_register()) { |
| 296 | Unuse(original.reg()); |
| 297 | } |
| 298 | elements_[index] = FrameElement::InvalidElement(); |
| 299 | return kIllegalIndex; |
| 300 | } |
| 301 | |
| 302 | // This is the backing store of copies. |
| 303 | Register backing_reg; |
| 304 | if (original.is_memory()) { |
| 305 | Result fresh = cgen()->allocator()->Allocate(); |
| 306 | ASSERT(fresh.is_valid()); |
| 307 | Use(fresh.reg(), new_backing_index); |
| 308 | backing_reg = fresh.reg(); |
| 309 | __ movq(backing_reg, Operand(rbp, fp_relative(index))); |
| 310 | } else { |
| 311 | // The original was in a register. |
| 312 | backing_reg = original.reg(); |
| 313 | set_register_location(backing_reg, new_backing_index); |
| 314 | } |
| 315 | // Invalidate the element at index. |
| 316 | elements_[index] = FrameElement::InvalidElement(); |
| 317 | // Set the new backing element. |
| 318 | if (elements_[new_backing_index].is_synced()) { |
| 319 | elements_[new_backing_index] = |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 320 | FrameElement::RegisterElement(backing_reg, |
| 321 | FrameElement::SYNCED, |
sgjesse@chromium.org | df7a284 | 2010-03-25 14:34:15 +0000 | [diff] [blame] | 322 | original.type_info()); |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 323 | } else { |
| 324 | elements_[new_backing_index] = |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 325 | FrameElement::RegisterElement(backing_reg, |
| 326 | FrameElement::NOT_SYNCED, |
sgjesse@chromium.org | df7a284 | 2010-03-25 14:34:15 +0000 | [diff] [blame] | 327 | original.type_info()); |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 328 | } |
| 329 | // Update the other copies. |
| 330 | for (int i = new_backing_index + 1; i < element_count(); i++) { |
| 331 | if (elements_[i].is_copy() && elements_[i].index() == index) { |
| 332 | elements_[i].set_index(new_backing_index); |
| 333 | elements_[new_backing_index].set_copied(); |
| 334 | } |
| 335 | } |
| 336 | return new_backing_index; |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 337 | } |
| 338 | |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 339 | |
ager@chromium.org | 3e87580 | 2009-06-29 08:26:34 +0000 | [diff] [blame] | 340 | void VirtualFrame::TakeFrameSlotAt(int index) { |
| 341 | ASSERT(index >= 0); |
| 342 | ASSERT(index <= element_count()); |
| 343 | FrameElement original = elements_[index]; |
| 344 | int new_backing_store_index = InvalidateFrameSlotAt(index); |
| 345 | if (new_backing_store_index != kIllegalIndex) { |
| 346 | elements_.Add(CopyElementAt(new_backing_store_index)); |
| 347 | return; |
| 348 | } |
| 349 | |
| 350 | switch (original.type()) { |
| 351 | case FrameElement::MEMORY: { |
| 352 | // Emit code to load the original element's data into a register. |
| 353 | // Push that register as a FrameElement on top of the frame. |
| 354 | Result fresh = cgen()->allocator()->Allocate(); |
| 355 | ASSERT(fresh.is_valid()); |
| 356 | FrameElement new_element = |
| 357 | FrameElement::RegisterElement(fresh.reg(), |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 358 | FrameElement::NOT_SYNCED, |
sgjesse@chromium.org | df7a284 | 2010-03-25 14:34:15 +0000 | [diff] [blame] | 359 | original.type_info()); |
ager@chromium.org | 3e87580 | 2009-06-29 08:26:34 +0000 | [diff] [blame] | 360 | Use(fresh.reg(), element_count()); |
| 361 | elements_.Add(new_element); |
| 362 | __ movq(fresh.reg(), Operand(rbp, fp_relative(index))); |
| 363 | break; |
| 364 | } |
| 365 | case FrameElement::REGISTER: |
| 366 | Use(original.reg(), element_count()); |
| 367 | // Fall through. |
| 368 | case FrameElement::CONSTANT: |
| 369 | case FrameElement::COPY: |
| 370 | original.clear_sync(); |
| 371 | elements_.Add(original); |
| 372 | break; |
| 373 | case FrameElement::INVALID: |
| 374 | UNREACHABLE(); |
| 375 | break; |
| 376 | } |
| 377 | } |
| 378 | |
| 379 | |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 380 | void VirtualFrame::StoreToFrameSlotAt(int index) { |
| 381 | // Store the value on top of the frame to the virtual frame slot at |
| 382 | // a given index. The value on top of the frame is left in place. |
| 383 | // This is a duplicating operation, so it can create copies. |
| 384 | ASSERT(index >= 0); |
| 385 | ASSERT(index < element_count()); |
| 386 | |
| 387 | int top_index = element_count() - 1; |
| 388 | FrameElement top = elements_[top_index]; |
| 389 | FrameElement original = elements_[index]; |
| 390 | if (top.is_copy() && top.index() == index) return; |
| 391 | ASSERT(top.is_valid()); |
| 392 | |
| 393 | InvalidateFrameSlotAt(index); |
| 394 | |
| 395 | // InvalidateFrameSlotAt can potentially change any frame element, due |
| 396 | // to spilling registers to allocate temporaries in order to preserve |
| 397 | // the copy-on-write semantics of aliased elements. Reload top from |
| 398 | // the frame. |
| 399 | top = elements_[top_index]; |
| 400 | |
| 401 | if (top.is_copy()) { |
| 402 | // There are two cases based on the relative positions of the |
| 403 | // stored-to slot and the backing slot of the top element. |
| 404 | int backing_index = top.index(); |
| 405 | ASSERT(backing_index != index); |
| 406 | if (backing_index < index) { |
| 407 | // 1. The top element is a copy of a slot below the stored-to |
| 408 | // slot. The stored-to slot becomes an unsynced copy of that |
| 409 | // same backing slot. |
| 410 | elements_[index] = CopyElementAt(backing_index); |
| 411 | } else { |
| 412 | // 2. The top element is a copy of a slot above the stored-to |
| 413 | // slot. The stored-to slot becomes the new (unsynced) backing |
| 414 | // slot and both the top element and the element at the former |
| 415 | // backing slot become copies of it. The sync state of the top |
| 416 | // and former backing elements is preserved. |
| 417 | FrameElement backing_element = elements_[backing_index]; |
| 418 | ASSERT(backing_element.is_memory() || backing_element.is_register()); |
| 419 | if (backing_element.is_memory()) { |
| 420 | // Because sets of copies are canonicalized to be backed by |
| 421 | // their lowest frame element, and because memory frame |
| 422 | // elements are backed by the corresponding stack address, we |
| 423 | // have to move the actual value down in the stack. |
| 424 | // |
| 425 | // TODO(209): considering allocating the stored-to slot to the |
| 426 | // temp register. Alternatively, allow copies to appear in |
| 427 | // any order in the frame and lazily move the value down to |
| 428 | // the slot. |
| 429 | __ movq(kScratchRegister, Operand(rbp, fp_relative(backing_index))); |
| 430 | __ movq(Operand(rbp, fp_relative(index)), kScratchRegister); |
| 431 | } else { |
| 432 | set_register_location(backing_element.reg(), index); |
| 433 | if (backing_element.is_synced()) { |
| 434 | // If the element is a register, we will not actually move |
| 435 | // anything on the stack but only update the virtual frame |
| 436 | // element. |
| 437 | backing_element.clear_sync(); |
| 438 | } |
| 439 | } |
| 440 | elements_[index] = backing_element; |
| 441 | |
| 442 | // The old backing element becomes a copy of the new backing |
| 443 | // element. |
| 444 | FrameElement new_element = CopyElementAt(index); |
| 445 | elements_[backing_index] = new_element; |
| 446 | if (backing_element.is_synced()) { |
| 447 | elements_[backing_index].set_sync(); |
| 448 | } |
| 449 | |
| 450 | // All the copies of the old backing element (including the top |
| 451 | // element) become copies of the new backing element. |
| 452 | for (int i = backing_index + 1; i < element_count(); i++) { |
| 453 | if (elements_[i].is_copy() && elements_[i].index() == backing_index) { |
| 454 | elements_[i].set_index(index); |
| 455 | } |
| 456 | } |
| 457 | } |
| 458 | return; |
| 459 | } |
| 460 | |
| 461 | // Move the top element to the stored-to slot and replace it (the |
| 462 | // top element) with a copy. |
| 463 | elements_[index] = top; |
| 464 | if (top.is_memory()) { |
| 465 | // TODO(209): consider allocating the stored-to slot to the temp |
| 466 | // register. Alternatively, allow copies to appear in any order |
| 467 | // in the frame and lazily move the value down to the slot. |
| 468 | FrameElement new_top = CopyElementAt(index); |
| 469 | new_top.set_sync(); |
| 470 | elements_[top_index] = new_top; |
| 471 | |
| 472 | // The sync state of the former top element is correct (synced). |
| 473 | // Emit code to move the value down in the frame. |
| 474 | __ movq(kScratchRegister, Operand(rsp, 0)); |
| 475 | __ movq(Operand(rbp, fp_relative(index)), kScratchRegister); |
| 476 | } else if (top.is_register()) { |
| 477 | set_register_location(top.reg(), index); |
| 478 | // The stored-to slot has the (unsynced) register reference and |
| 479 | // the top element becomes a copy. The sync state of the top is |
| 480 | // preserved. |
| 481 | FrameElement new_top = CopyElementAt(index); |
| 482 | if (top.is_synced()) { |
| 483 | new_top.set_sync(); |
| 484 | elements_[index].clear_sync(); |
| 485 | } |
| 486 | elements_[top_index] = new_top; |
| 487 | } else { |
| 488 | // The stored-to slot holds the same value as the top but |
| 489 | // unsynced. (We do not have copies of constants yet.) |
| 490 | ASSERT(top.is_constant()); |
| 491 | elements_[index].clear_sync(); |
| 492 | } |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 493 | } |
| 494 | |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 495 | |
| 496 | void VirtualFrame::MakeMergable() { |
| 497 | for (int i = 0; i < element_count(); i++) { |
| 498 | FrameElement element = elements_[i]; |
| 499 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 500 | // In all cases we have to reset the number type information |
| 501 | // to unknown for a mergable frame because of incoming back edges. |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 502 | if (element.is_constant() || element.is_copy()) { |
| 503 | if (element.is_synced()) { |
| 504 | // Just spill. |
sgjesse@chromium.org | df7a284 | 2010-03-25 14:34:15 +0000 | [diff] [blame] | 505 | elements_[i] = FrameElement::MemoryElement(TypeInfo::Unknown()); |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 506 | } else { |
| 507 | // Allocate to a register. |
| 508 | FrameElement backing_element; // Invalid if not a copy. |
| 509 | if (element.is_copy()) { |
| 510 | backing_element = elements_[element.index()]; |
| 511 | } |
| 512 | Result fresh = cgen()->allocator()->Allocate(); |
| 513 | ASSERT(fresh.is_valid()); // A register was spilled if all were in use. |
| 514 | elements_[i] = |
| 515 | FrameElement::RegisterElement(fresh.reg(), |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 516 | FrameElement::NOT_SYNCED, |
sgjesse@chromium.org | df7a284 | 2010-03-25 14:34:15 +0000 | [diff] [blame] | 517 | TypeInfo::Unknown()); |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 518 | Use(fresh.reg(), i); |
| 519 | |
| 520 | // Emit a move. |
| 521 | if (element.is_constant()) { |
| 522 | __ Move(fresh.reg(), element.handle()); |
| 523 | } else { |
| 524 | ASSERT(element.is_copy()); |
| 525 | // Copies are only backed by register or memory locations. |
| 526 | if (backing_element.is_register()) { |
| 527 | // The backing store may have been spilled by allocating, |
| 528 | // but that's OK. If it was, the value is right where we |
| 529 | // want it. |
| 530 | if (!fresh.reg().is(backing_element.reg())) { |
| 531 | __ movq(fresh.reg(), backing_element.reg()); |
| 532 | } |
| 533 | } else { |
| 534 | ASSERT(backing_element.is_memory()); |
| 535 | __ movq(fresh.reg(), Operand(rbp, fp_relative(element.index()))); |
| 536 | } |
| 537 | } |
| 538 | } |
ager@chromium.org | 3e87580 | 2009-06-29 08:26:34 +0000 | [diff] [blame] | 539 | // No need to set the copied flag --- there are no copies. |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 540 | } else { |
| 541 | // Clear the copy flag of non-constant, non-copy elements. |
| 542 | // They cannot be copied because copies are not allowed. |
| 543 | // The copy flag is not relied on before the end of this loop, |
| 544 | // including when registers are spilled. |
| 545 | elements_[i].clear_copied(); |
sgjesse@chromium.org | df7a284 | 2010-03-25 14:34:15 +0000 | [diff] [blame] | 546 | elements_[i].set_type_info(TypeInfo::Unknown()); |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 547 | } |
| 548 | } |
| 549 | } |
| 550 | |
| 551 | |
| 552 | void VirtualFrame::MergeTo(VirtualFrame* expected) { |
| 553 | Comment cmnt(masm(), "[ Merge frame"); |
| 554 | // We should always be merging the code generator's current frame to an |
| 555 | // expected frame. |
| 556 | ASSERT(cgen()->frame() == this); |
| 557 | |
| 558 | // Adjust the stack pointer upward (toward the top of the virtual |
| 559 | // frame) if necessary. |
| 560 | if (stack_pointer_ < expected->stack_pointer_) { |
| 561 | int difference = expected->stack_pointer_ - stack_pointer_; |
| 562 | stack_pointer_ = expected->stack_pointer_; |
| 563 | __ subq(rsp, Immediate(difference * kPointerSize)); |
| 564 | } |
| 565 | |
| 566 | MergeMoveRegistersToMemory(expected); |
| 567 | MergeMoveRegistersToRegisters(expected); |
| 568 | MergeMoveMemoryToRegisters(expected); |
| 569 | |
| 570 | // Adjust the stack pointer downward if necessary. |
| 571 | if (stack_pointer_ > expected->stack_pointer_) { |
| 572 | int difference = stack_pointer_ - expected->stack_pointer_; |
| 573 | stack_pointer_ = expected->stack_pointer_; |
| 574 | __ addq(rsp, Immediate(difference * kPointerSize)); |
| 575 | } |
| 576 | |
| 577 | // At this point, the frames should be identical. |
| 578 | ASSERT(Equals(expected)); |
| 579 | } |
| 580 | |
| 581 | |
| 582 | void VirtualFrame::MergeMoveRegistersToMemory(VirtualFrame* expected) { |
| 583 | ASSERT(stack_pointer_ >= expected->stack_pointer_); |
| 584 | |
| 585 | // Move registers, constants, and copies to memory. Perform moves |
| 586 | // from the top downward in the frame in order to leave the backing |
| 587 | // stores of copies in registers. |
| 588 | for (int i = element_count() - 1; i >= 0; i--) { |
| 589 | FrameElement target = expected->elements_[i]; |
| 590 | if (target.is_register()) continue; // Handle registers later. |
| 591 | if (target.is_memory()) { |
| 592 | FrameElement source = elements_[i]; |
| 593 | switch (source.type()) { |
| 594 | case FrameElement::INVALID: |
| 595 | // Not a legal merge move. |
| 596 | UNREACHABLE(); |
| 597 | break; |
| 598 | |
| 599 | case FrameElement::MEMORY: |
| 600 | // Already in place. |
| 601 | break; |
| 602 | |
| 603 | case FrameElement::REGISTER: |
| 604 | Unuse(source.reg()); |
| 605 | if (!source.is_synced()) { |
| 606 | __ movq(Operand(rbp, fp_relative(i)), source.reg()); |
| 607 | } |
| 608 | break; |
| 609 | |
| 610 | case FrameElement::CONSTANT: |
| 611 | if (!source.is_synced()) { |
| 612 | __ Move(Operand(rbp, fp_relative(i)), source.handle()); |
| 613 | } |
| 614 | break; |
| 615 | |
| 616 | case FrameElement::COPY: |
| 617 | if (!source.is_synced()) { |
| 618 | int backing_index = source.index(); |
| 619 | FrameElement backing_element = elements_[backing_index]; |
| 620 | if (backing_element.is_memory()) { |
| 621 | __ movq(kScratchRegister, |
| 622 | Operand(rbp, fp_relative(backing_index))); |
| 623 | __ movq(Operand(rbp, fp_relative(i)), kScratchRegister); |
| 624 | } else { |
| 625 | ASSERT(backing_element.is_register()); |
| 626 | __ movq(Operand(rbp, fp_relative(i)), backing_element.reg()); |
| 627 | } |
| 628 | } |
| 629 | break; |
| 630 | } |
| 631 | } |
| 632 | elements_[i] = target; |
| 633 | } |
| 634 | } |
| 635 | |
| 636 | |
| 637 | void VirtualFrame::MergeMoveRegistersToRegisters(VirtualFrame* expected) { |
| 638 | // We have already done X-to-memory moves. |
| 639 | ASSERT(stack_pointer_ >= expected->stack_pointer_); |
| 640 | |
| 641 | for (int i = 0; i < RegisterAllocator::kNumRegisters; i++) { |
| 642 | // Move the right value into register i if it is currently in a register. |
| 643 | int index = expected->register_location(i); |
| 644 | int use_index = register_location(i); |
| 645 | // Skip if register i is unused in the target or else if source is |
| 646 | // not a register (this is not a register-to-register move). |
| 647 | if (index == kIllegalIndex || !elements_[index].is_register()) continue; |
| 648 | |
| 649 | Register target = RegisterAllocator::ToRegister(i); |
| 650 | Register source = elements_[index].reg(); |
| 651 | if (index != use_index) { |
| 652 | if (use_index == kIllegalIndex) { // Target is currently unused. |
| 653 | // Copy contents of source from source to target. |
| 654 | // Set frame element register to target. |
| 655 | Use(target, index); |
| 656 | Unuse(source); |
| 657 | __ movq(target, source); |
| 658 | } else { |
| 659 | // Exchange contents of registers source and target. |
| 660 | // Nothing except the register backing use_index has changed. |
| 661 | elements_[use_index].set_reg(source); |
| 662 | set_register_location(target, index); |
| 663 | set_register_location(source, use_index); |
| 664 | __ xchg(source, target); |
| 665 | } |
| 666 | } |
| 667 | |
| 668 | if (!elements_[index].is_synced() && |
| 669 | expected->elements_[index].is_synced()) { |
| 670 | __ movq(Operand(rbp, fp_relative(index)), target); |
| 671 | } |
| 672 | elements_[index] = expected->elements_[index]; |
| 673 | } |
| 674 | } |
| 675 | |
| 676 | |
| 677 | void VirtualFrame::MergeMoveMemoryToRegisters(VirtualFrame* expected) { |
| 678 | // Move memory, constants, and copies to registers. This is the |
| 679 | // final step and since it is not done from the bottom up, but in |
| 680 | // register code order, we have special code to ensure that the backing |
| 681 | // elements of copies are in their correct locations when we |
| 682 | // encounter the copies. |
| 683 | for (int i = 0; i < RegisterAllocator::kNumRegisters; i++) { |
| 684 | int index = expected->register_location(i); |
| 685 | if (index != kIllegalIndex) { |
| 686 | FrameElement source = elements_[index]; |
| 687 | FrameElement target = expected->elements_[index]; |
| 688 | Register target_reg = RegisterAllocator::ToRegister(i); |
| 689 | ASSERT(target.reg().is(target_reg)); |
| 690 | switch (source.type()) { |
| 691 | case FrameElement::INVALID: // Fall through. |
| 692 | UNREACHABLE(); |
| 693 | break; |
| 694 | case FrameElement::REGISTER: |
| 695 | ASSERT(source.Equals(target)); |
| 696 | // Go to next iteration. Skips Use(target_reg) and syncing |
| 697 | // below. It is safe to skip syncing because a target |
| 698 | // register frame element would only be synced if all source |
| 699 | // elements were. |
| 700 | continue; |
| 701 | break; |
| 702 | case FrameElement::MEMORY: |
| 703 | ASSERT(index <= stack_pointer_); |
| 704 | __ movq(target_reg, Operand(rbp, fp_relative(index))); |
| 705 | break; |
| 706 | |
| 707 | case FrameElement::CONSTANT: |
| 708 | __ Move(target_reg, source.handle()); |
| 709 | break; |
| 710 | |
| 711 | case FrameElement::COPY: { |
| 712 | int backing_index = source.index(); |
| 713 | FrameElement backing = elements_[backing_index]; |
| 714 | ASSERT(backing.is_memory() || backing.is_register()); |
| 715 | if (backing.is_memory()) { |
| 716 | ASSERT(backing_index <= stack_pointer_); |
| 717 | // Code optimization if backing store should also move |
| 718 | // to a register: move backing store to its register first. |
| 719 | if (expected->elements_[backing_index].is_register()) { |
| 720 | FrameElement new_backing = expected->elements_[backing_index]; |
| 721 | Register new_backing_reg = new_backing.reg(); |
| 722 | ASSERT(!is_used(new_backing_reg)); |
| 723 | elements_[backing_index] = new_backing; |
| 724 | Use(new_backing_reg, backing_index); |
| 725 | __ movq(new_backing_reg, |
| 726 | Operand(rbp, fp_relative(backing_index))); |
| 727 | __ movq(target_reg, new_backing_reg); |
| 728 | } else { |
| 729 | __ movq(target_reg, Operand(rbp, fp_relative(backing_index))); |
| 730 | } |
| 731 | } else { |
| 732 | __ movq(target_reg, backing.reg()); |
| 733 | } |
| 734 | } |
| 735 | } |
| 736 | // Ensure the proper sync state. |
| 737 | if (target.is_synced() && !source.is_synced()) { |
| 738 | __ movq(Operand(rbp, fp_relative(index)), target_reg); |
| 739 | } |
| 740 | Use(target_reg, index); |
| 741 | elements_[index] = target; |
| 742 | } |
| 743 | } |
| 744 | } |
| 745 | |
| 746 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 747 | Result VirtualFrame::Pop() { |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 748 | FrameElement element = elements_.RemoveLast(); |
| 749 | int index = element_count(); |
| 750 | ASSERT(element.is_valid()); |
| 751 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 752 | // Get number type information of the result. |
sgjesse@chromium.org | df7a284 | 2010-03-25 14:34:15 +0000 | [diff] [blame] | 753 | TypeInfo info; |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 754 | if (!element.is_copy()) { |
sgjesse@chromium.org | df7a284 | 2010-03-25 14:34:15 +0000 | [diff] [blame] | 755 | info = element.type_info(); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 756 | } else { |
sgjesse@chromium.org | df7a284 | 2010-03-25 14:34:15 +0000 | [diff] [blame] | 757 | info = elements_[element.index()].type_info(); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 758 | } |
| 759 | |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 760 | bool pop_needed = (stack_pointer_ == index); |
| 761 | if (pop_needed) { |
| 762 | stack_pointer_--; |
| 763 | if (element.is_memory()) { |
| 764 | Result temp = cgen()->allocator()->Allocate(); |
| 765 | ASSERT(temp.is_valid()); |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 766 | __ pop(temp.reg()); |
sgjesse@chromium.org | df7a284 | 2010-03-25 14:34:15 +0000 | [diff] [blame] | 767 | temp.set_type_info(info); |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 768 | return temp; |
| 769 | } |
| 770 | |
| 771 | __ addq(rsp, Immediate(kPointerSize)); |
| 772 | } |
| 773 | ASSERT(!element.is_memory()); |
| 774 | |
| 775 | // The top element is a register, constant, or a copy. Unuse |
| 776 | // registers and follow copies to their backing store. |
| 777 | if (element.is_register()) { |
| 778 | Unuse(element.reg()); |
| 779 | } else if (element.is_copy()) { |
| 780 | ASSERT(element.index() < index); |
| 781 | index = element.index(); |
| 782 | element = elements_[index]; |
| 783 | } |
| 784 | ASSERT(!element.is_copy()); |
| 785 | |
| 786 | // The element is memory, a register, or a constant. |
| 787 | if (element.is_memory()) { |
| 788 | // Memory elements could only be the backing store of a copy. |
| 789 | // Allocate the original to a register. |
| 790 | ASSERT(index <= stack_pointer_); |
| 791 | Result temp = cgen()->allocator()->Allocate(); |
| 792 | ASSERT(temp.is_valid()); |
| 793 | Use(temp.reg(), index); |
| 794 | FrameElement new_element = |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 795 | FrameElement::RegisterElement(temp.reg(), |
| 796 | FrameElement::SYNCED, |
sgjesse@chromium.org | df7a284 | 2010-03-25 14:34:15 +0000 | [diff] [blame] | 797 | element.type_info()); |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 798 | // Preserve the copy flag on the element. |
| 799 | if (element.is_copied()) new_element.set_copied(); |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 800 | elements_[index] = new_element; |
| 801 | __ movq(temp.reg(), Operand(rbp, fp_relative(index))); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 802 | return Result(temp.reg(), info); |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 803 | } else if (element.is_register()) { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 804 | return Result(element.reg(), info); |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 805 | } else { |
| 806 | ASSERT(element.is_constant()); |
| 807 | return Result(element.handle()); |
| 808 | } |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 809 | } |
| 810 | |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 811 | |
| 812 | Result VirtualFrame::RawCallStub(CodeStub* stub) { |
| 813 | ASSERT(cgen()->HasValidEntryRegisters()); |
| 814 | __ CallStub(stub); |
| 815 | Result result = cgen()->allocator()->Allocate(rax); |
| 816 | ASSERT(result.is_valid()); |
| 817 | return result; |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 818 | } |
| 819 | |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 820 | |
ager@chromium.org | 3e87580 | 2009-06-29 08:26:34 +0000 | [diff] [blame] | 821 | Result VirtualFrame::CallStub(CodeStub* stub, Result* arg) { |
| 822 | PrepareForCall(0, 0); |
| 823 | arg->ToRegister(rax); |
| 824 | arg->Unuse(); |
| 825 | return RawCallStub(stub); |
| 826 | } |
| 827 | |
| 828 | |
| 829 | Result VirtualFrame::CallStub(CodeStub* stub, Result* arg0, Result* arg1) { |
| 830 | PrepareForCall(0, 0); |
| 831 | |
| 832 | if (arg0->is_register() && arg0->reg().is(rax)) { |
| 833 | if (arg1->is_register() && arg1->reg().is(rdx)) { |
| 834 | // Wrong registers. |
| 835 | __ xchg(rax, rdx); |
| 836 | } else { |
| 837 | // Register rdx is free for arg0, which frees rax for arg1. |
| 838 | arg0->ToRegister(rdx); |
| 839 | arg1->ToRegister(rax); |
| 840 | } |
| 841 | } else { |
| 842 | // Register rax is free for arg1, which guarantees rdx is free for |
| 843 | // arg0. |
| 844 | arg1->ToRegister(rax); |
| 845 | arg0->ToRegister(rdx); |
| 846 | } |
| 847 | |
| 848 | arg0->Unuse(); |
| 849 | arg1->Unuse(); |
| 850 | return RawCallStub(stub); |
| 851 | } |
| 852 | |
| 853 | |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 854 | Result VirtualFrame::CallJSFunction(int arg_count) { |
| 855 | Result function = Pop(); |
| 856 | |
| 857 | // InvokeFunction requires function in rdi. Move it in there. |
| 858 | function.ToRegister(rdi); |
| 859 | function.Unuse(); |
| 860 | |
| 861 | // +1 for receiver. |
| 862 | PrepareForCall(arg_count + 1, arg_count + 1); |
| 863 | ASSERT(cgen()->HasValidEntryRegisters()); |
| 864 | ParameterCount count(arg_count); |
| 865 | __ InvokeFunction(rdi, count, CALL_FUNCTION); |
| 866 | RestoreContextRegister(); |
| 867 | Result result = cgen()->allocator()->Allocate(rax); |
| 868 | ASSERT(result.is_valid()); |
| 869 | return result; |
| 870 | } |
| 871 | |
| 872 | |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 873 | void VirtualFrame::SyncElementBelowStackPointer(int index) { |
| 874 | // Emit code to write elements below the stack pointer to their |
| 875 | // (already allocated) stack address. |
| 876 | ASSERT(index <= stack_pointer_); |
| 877 | FrameElement element = elements_[index]; |
| 878 | ASSERT(!element.is_synced()); |
| 879 | switch (element.type()) { |
| 880 | case FrameElement::INVALID: |
| 881 | break; |
| 882 | |
| 883 | case FrameElement::MEMORY: |
| 884 | // This function should not be called with synced elements. |
| 885 | // (memory elements are always synced). |
| 886 | UNREACHABLE(); |
| 887 | break; |
| 888 | |
| 889 | case FrameElement::REGISTER: |
| 890 | __ movq(Operand(rbp, fp_relative(index)), element.reg()); |
| 891 | break; |
| 892 | |
| 893 | case FrameElement::CONSTANT: |
| 894 | __ Move(Operand(rbp, fp_relative(index)), element.handle()); |
| 895 | break; |
| 896 | |
| 897 | case FrameElement::COPY: { |
| 898 | int backing_index = element.index(); |
| 899 | FrameElement backing_element = elements_[backing_index]; |
| 900 | if (backing_element.is_memory()) { |
| 901 | __ movq(kScratchRegister, Operand(rbp, fp_relative(backing_index))); |
| 902 | __ movq(Operand(rbp, fp_relative(index)), kScratchRegister); |
| 903 | } else { |
| 904 | ASSERT(backing_element.is_register()); |
| 905 | __ movq(Operand(rbp, fp_relative(index)), backing_element.reg()); |
| 906 | } |
| 907 | break; |
| 908 | } |
| 909 | } |
| 910 | elements_[index].set_sync(); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 911 | } |
| 912 | |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 913 | |
| 914 | void VirtualFrame::SyncElementByPushing(int index) { |
| 915 | // Sync an element of the frame that is just above the stack pointer |
| 916 | // by pushing it. |
| 917 | ASSERT(index == stack_pointer_ + 1); |
| 918 | stack_pointer_++; |
| 919 | FrameElement element = elements_[index]; |
| 920 | |
| 921 | switch (element.type()) { |
| 922 | case FrameElement::INVALID: |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 923 | __ Push(Smi::FromInt(0)); |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 924 | break; |
| 925 | |
| 926 | case FrameElement::MEMORY: |
| 927 | // No memory elements exist above the stack pointer. |
| 928 | UNREACHABLE(); |
| 929 | break; |
| 930 | |
| 931 | case FrameElement::REGISTER: |
| 932 | __ push(element.reg()); |
| 933 | break; |
| 934 | |
| 935 | case FrameElement::CONSTANT: |
| 936 | __ Move(kScratchRegister, element.handle()); |
| 937 | __ push(kScratchRegister); |
| 938 | break; |
| 939 | |
| 940 | case FrameElement::COPY: { |
| 941 | int backing_index = element.index(); |
| 942 | FrameElement backing = elements_[backing_index]; |
| 943 | ASSERT(backing.is_memory() || backing.is_register()); |
| 944 | if (backing.is_memory()) { |
| 945 | __ push(Operand(rbp, fp_relative(backing_index))); |
| 946 | } else { |
| 947 | __ push(backing.reg()); |
| 948 | } |
| 949 | break; |
| 950 | } |
| 951 | } |
| 952 | elements_[index].set_sync(); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 953 | } |
| 954 | |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 955 | |
| 956 | // Clear the dirty bits for the range of elements in |
| 957 | // [min(stack_pointer_ + 1,begin), end]. |
| 958 | void VirtualFrame::SyncRange(int begin, int end) { |
| 959 | ASSERT(begin >= 0); |
| 960 | ASSERT(end < element_count()); |
| 961 | // Sync elements below the range if they have not been materialized |
| 962 | // on the stack. |
| 963 | int start = Min(begin, stack_pointer_ + 1); |
| 964 | |
ager@chromium.org | 7cf3b1d | 2009-11-11 09:17:04 +0000 | [diff] [blame] | 965 | // If positive we have to adjust the stack pointer. |
| 966 | int delta = end - stack_pointer_; |
| 967 | if (delta > 0) { |
| 968 | stack_pointer_ = end; |
| 969 | __ subq(rsp, Immediate(delta * kPointerSize)); |
| 970 | } |
| 971 | |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 972 | for (int i = start; i <= end; i++) { |
ager@chromium.org | 7cf3b1d | 2009-11-11 09:17:04 +0000 | [diff] [blame] | 973 | if (!elements_[i].is_synced()) SyncElementBelowStackPointer(i); |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 974 | } |
| 975 | } |
| 976 | |
ager@chromium.org | 3e87580 | 2009-06-29 08:26:34 +0000 | [diff] [blame] | 977 | |
| 978 | Result VirtualFrame::InvokeBuiltin(Builtins::JavaScript id, |
| 979 | InvokeFlag flag, |
| 980 | int arg_count) { |
| 981 | PrepareForCall(arg_count, arg_count); |
| 982 | ASSERT(cgen()->HasValidEntryRegisters()); |
| 983 | __ InvokeBuiltin(id, flag); |
| 984 | Result result = cgen()->allocator()->Allocate(rax); |
| 985 | ASSERT(result.is_valid()); |
| 986 | return result; |
| 987 | } |
| 988 | |
| 989 | |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 990 | //------------------------------------------------------------------------------ |
| 991 | // Virtual frame stub and IC calling functions. |
| 992 | |
| 993 | Result VirtualFrame::RawCallCodeObject(Handle<Code> code, |
| 994 | RelocInfo::Mode rmode) { |
| 995 | ASSERT(cgen()->HasValidEntryRegisters()); |
| 996 | __ Call(code, rmode); |
| 997 | Result result = cgen()->allocator()->Allocate(rax); |
| 998 | ASSERT(result.is_valid()); |
| 999 | return result; |
| 1000 | } |
| 1001 | |
| 1002 | |
| 1003 | Result VirtualFrame::CallRuntime(Runtime::Function* f, int arg_count) { |
| 1004 | PrepareForCall(arg_count, arg_count); |
| 1005 | ASSERT(cgen()->HasValidEntryRegisters()); |
| 1006 | __ CallRuntime(f, arg_count); |
| 1007 | Result result = cgen()->allocator()->Allocate(rax); |
| 1008 | ASSERT(result.is_valid()); |
| 1009 | return result; |
| 1010 | } |
| 1011 | |
| 1012 | |
| 1013 | Result VirtualFrame::CallRuntime(Runtime::FunctionId id, int arg_count) { |
| 1014 | PrepareForCall(arg_count, arg_count); |
| 1015 | ASSERT(cgen()->HasValidEntryRegisters()); |
| 1016 | __ CallRuntime(id, arg_count); |
| 1017 | Result result = cgen()->allocator()->Allocate(rax); |
| 1018 | ASSERT(result.is_valid()); |
| 1019 | return result; |
| 1020 | } |
| 1021 | |
| 1022 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 1023 | #ifdef ENABLE_DEBUGGER_SUPPORT |
| 1024 | void VirtualFrame::DebugBreak() { |
| 1025 | PrepareForCall(0, 0); |
| 1026 | ASSERT(cgen()->HasValidEntryRegisters()); |
| 1027 | __ DebugBreak(); |
| 1028 | Result result = cgen()->allocator()->Allocate(rax); |
| 1029 | ASSERT(result.is_valid()); |
| 1030 | } |
| 1031 | #endif |
| 1032 | |
| 1033 | |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 1034 | // This function assumes that the only results that could be in a_reg or b_reg |
| 1035 | // are a and b. Other results can be live, but must not be in a_reg or b_reg. |
| 1036 | void VirtualFrame::MoveResultsToRegisters(Result* a, |
| 1037 | Result* b, |
| 1038 | Register a_reg, |
| 1039 | Register b_reg) { |
| 1040 | ASSERT(!a_reg.is(b_reg)); |
| 1041 | // Assert that cgen()->allocator()->count(a_reg) is accounted for by a and b. |
| 1042 | ASSERT(cgen()->allocator()->count(a_reg) <= 2); |
| 1043 | ASSERT(cgen()->allocator()->count(a_reg) != 2 || a->reg().is(a_reg)); |
| 1044 | ASSERT(cgen()->allocator()->count(a_reg) != 2 || b->reg().is(a_reg)); |
| 1045 | ASSERT(cgen()->allocator()->count(a_reg) != 1 || |
| 1046 | (a->is_register() && a->reg().is(a_reg)) || |
| 1047 | (b->is_register() && b->reg().is(a_reg))); |
| 1048 | // Assert that cgen()->allocator()->count(b_reg) is accounted for by a and b. |
| 1049 | ASSERT(cgen()->allocator()->count(b_reg) <= 2); |
| 1050 | ASSERT(cgen()->allocator()->count(b_reg) != 2 || a->reg().is(b_reg)); |
| 1051 | ASSERT(cgen()->allocator()->count(b_reg) != 2 || b->reg().is(b_reg)); |
| 1052 | ASSERT(cgen()->allocator()->count(b_reg) != 1 || |
| 1053 | (a->is_register() && a->reg().is(b_reg)) || |
| 1054 | (b->is_register() && b->reg().is(b_reg))); |
| 1055 | |
| 1056 | if (a->is_register() && a->reg().is(a_reg)) { |
| 1057 | b->ToRegister(b_reg); |
| 1058 | } else if (!cgen()->allocator()->is_used(a_reg)) { |
| 1059 | a->ToRegister(a_reg); |
| 1060 | b->ToRegister(b_reg); |
| 1061 | } else if (cgen()->allocator()->is_used(b_reg)) { |
| 1062 | // a must be in b_reg, b in a_reg. |
| 1063 | __ xchg(a_reg, b_reg); |
| 1064 | // Results a and b will be invalidated, so it is ok if they are switched. |
| 1065 | } else { |
| 1066 | b->ToRegister(b_reg); |
| 1067 | a->ToRegister(a_reg); |
| 1068 | } |
| 1069 | a->Unuse(); |
| 1070 | b->Unuse(); |
| 1071 | } |
| 1072 | |
| 1073 | |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 1074 | Result VirtualFrame::CallLoadIC(RelocInfo::Mode mode) { |
sgjesse@chromium.org | 82dbbab | 2010-06-02 08:57:44 +0000 | [diff] [blame^] | 1075 | // Name and receiver are on the top of the frame. Both are dropped. |
| 1076 | // The IC expects name in rcx and receiver in rax. |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 1077 | Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize)); |
| 1078 | Result name = Pop(); |
sgjesse@chromium.org | 82dbbab | 2010-06-02 08:57:44 +0000 | [diff] [blame^] | 1079 | Result receiver = Pop(); |
| 1080 | PrepareForCall(0, 0); // One stack arg, not callee-dropped. |
| 1081 | MoveResultsToRegisters(&name, &receiver, rcx, rax); |
| 1082 | |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 1083 | return RawCallCodeObject(ic, mode); |
| 1084 | } |
| 1085 | |
| 1086 | |
| 1087 | Result VirtualFrame::CallKeyedLoadIC(RelocInfo::Mode mode) { |
| 1088 | // Key and receiver are on top of the frame. The IC expects them on |
| 1089 | // the stack. It does not drop them. |
| 1090 | Handle<Code> ic(Builtins::builtin(Builtins::KeyedLoadIC_Initialize)); |
| 1091 | PrepareForCall(2, 0); // Two stack args, neither callee-dropped. |
| 1092 | return RawCallCodeObject(ic, mode); |
| 1093 | } |
| 1094 | |
| 1095 | |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 1096 | Result VirtualFrame::CallCommonStoreIC(Handle<Code> ic, |
| 1097 | Result* value, |
| 1098 | Result* key, |
| 1099 | Result* receiver) { |
| 1100 | // The IC expects value in rax, key in rcx, and receiver in rdx. |
| 1101 | PrepareForCall(0, 0); |
| 1102 | // If one of the three registers is free, or a value is already |
| 1103 | // in the correct register, move the remaining two values using |
| 1104 | // MoveResultsToRegisters(). |
| 1105 | if (!cgen()->allocator()->is_used(rax) || |
| 1106 | (value->is_register() && value->reg().is(rax))) { |
| 1107 | if (!cgen()->allocator()->is_used(rax)) { |
| 1108 | value->ToRegister(rax); |
| 1109 | } |
| 1110 | MoveResultsToRegisters(key, receiver, rcx, rdx); |
| 1111 | value->Unuse(); |
| 1112 | } else if (!cgen()->allocator()->is_used(rcx) || |
| 1113 | (key->is_register() && key->reg().is(rcx))) { |
| 1114 | if (!cgen()->allocator()->is_used(rcx)) { |
| 1115 | key->ToRegister(rcx); |
| 1116 | } |
| 1117 | MoveResultsToRegisters(value, receiver, rax, rdx); |
| 1118 | key->Unuse(); |
| 1119 | } else if (!cgen()->allocator()->is_used(rdx) || |
| 1120 | (receiver->is_register() && receiver->reg().is(rdx))) { |
| 1121 | if (!cgen()->allocator()->is_used(rdx)) { |
| 1122 | receiver->ToRegister(rdx); |
| 1123 | } |
| 1124 | MoveResultsToRegisters(key, value, rcx, rax); |
| 1125 | receiver->Unuse(); |
| 1126 | } else { |
| 1127 | // Otherwise, no register is free, and no value is in the correct place. |
| 1128 | // We have one of the two circular permutations of eax, ecx, edx. |
| 1129 | ASSERT(value->is_register()); |
| 1130 | if (value->reg().is(rcx)) { |
| 1131 | __ xchg(rax, rdx); |
| 1132 | __ xchg(rax, rcx); |
| 1133 | } else { |
| 1134 | __ xchg(rax, rcx); |
| 1135 | __ xchg(rax, rdx); |
| 1136 | } |
| 1137 | value->Unuse(); |
| 1138 | key->Unuse(); |
| 1139 | receiver->Unuse(); |
| 1140 | } |
| 1141 | |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 1142 | return RawCallCodeObject(ic, RelocInfo::CODE_TARGET); |
| 1143 | } |
| 1144 | |
| 1145 | |
| 1146 | Result VirtualFrame::CallCallIC(RelocInfo::Mode mode, |
| 1147 | int arg_count, |
| 1148 | int loop_nesting) { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 1149 | // Function name, arguments, and receiver are found on top of the frame |
| 1150 | // and dropped by the call. The IC expects the name in rcx and the rest |
| 1151 | // on the stack, and drops them all. |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 1152 | InLoopFlag in_loop = loop_nesting > 0 ? IN_LOOP : NOT_IN_LOOP; |
| 1153 | Handle<Code> ic = cgen()->ComputeCallInitialize(arg_count, in_loop); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 1154 | Result name = Pop(); |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 1155 | // Spill args, receiver, and function. The call will drop args and |
| 1156 | // receiver. |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 1157 | PrepareForCall(arg_count + 1, arg_count + 1); |
| 1158 | name.ToRegister(rcx); |
| 1159 | name.Unuse(); |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 1160 | return RawCallCodeObject(ic, mode); |
| 1161 | } |
| 1162 | |
| 1163 | |
ager@chromium.org | 3e87580 | 2009-06-29 08:26:34 +0000 | [diff] [blame] | 1164 | Result VirtualFrame::CallConstructor(int arg_count) { |
| 1165 | // Arguments, receiver, and function are on top of the frame. The |
| 1166 | // IC expects arg count in rax, function in rdi, and the arguments |
| 1167 | // and receiver on the stack. |
| 1168 | Handle<Code> ic(Builtins::builtin(Builtins::JSConstructCall)); |
| 1169 | // Duplicate the function before preparing the frame. |
| 1170 | PushElementAt(arg_count + 1); |
| 1171 | Result function = Pop(); |
| 1172 | PrepareForCall(arg_count + 1, arg_count + 1); // Spill args and receiver. |
| 1173 | function.ToRegister(rdi); |
| 1174 | |
| 1175 | // Constructors are called with the number of arguments in register |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1176 | // rax for now. Another option would be to have separate construct |
ager@chromium.org | 3e87580 | 2009-06-29 08:26:34 +0000 | [diff] [blame] | 1177 | // call trampolines per different arguments counts encountered. |
| 1178 | Result num_args = cgen()->allocator()->Allocate(rax); |
| 1179 | ASSERT(num_args.is_valid()); |
| 1180 | __ movq(num_args.reg(), Immediate(arg_count)); |
| 1181 | |
| 1182 | function.Unuse(); |
| 1183 | num_args.Unuse(); |
| 1184 | return RawCallCodeObject(ic, RelocInfo::CONSTRUCT_CALL); |
| 1185 | } |
| 1186 | |
| 1187 | |
ager@chromium.org | 3e87580 | 2009-06-29 08:26:34 +0000 | [diff] [blame] | 1188 | void VirtualFrame::PushTryHandler(HandlerType type) { |
| 1189 | ASSERT(cgen()->HasValidEntryRegisters()); |
| 1190 | // Grow the expression stack by handler size less one (the return |
| 1191 | // address is already pushed by a call instruction). |
| 1192 | Adjust(kHandlerSize - 1); |
| 1193 | __ PushTryHandler(IN_JAVASCRIPT, type); |
| 1194 | } |
| 1195 | |
| 1196 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 1197 | #undef __ |
| 1198 | |
| 1199 | } } // namespace v8::internal |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 1200 | |
| 1201 | #endif // V8_TARGET_ARCH_X64 |