Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame^] | 1 | // Copyright 2011 the V8 project authors. All rights reserved. |
Ben Murdoch | 8c569c4 | 2011-03-10 11:43:29 +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 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 28 | #include "v8.h" |
Ben Murdoch | 8c569c4 | 2011-03-10 11:43:29 +0000 | [diff] [blame] | 29 | |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 30 | #if defined(V8_TARGET_ARCH_X64) |
| 31 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 32 | #include "codegen.h" |
| 33 | #include "deoptimizer.h" |
| 34 | #include "full-codegen.h" |
| 35 | #include "safepoint-table.h" |
Ben Murdoch | 8c569c4 | 2011-03-10 11:43:29 +0000 | [diff] [blame] | 36 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 37 | namespace v8 { |
| 38 | namespace internal { |
Ben Murdoch | 8c569c4 | 2011-03-10 11:43:29 +0000 | [diff] [blame] | 39 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 40 | |
| 41 | int Deoptimizer::table_entry_size_ = 10; |
| 42 | |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame^] | 43 | |
| 44 | int Deoptimizer::patch_size() { |
| 45 | return MacroAssembler::kCallInstructionLength; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame^] | 49 | #ifdef DEBUG |
| 50 | // Overwrites code with int3 instructions. |
| 51 | static void ZapCodeRange(Address from, Address to) { |
| 52 | CHECK(from <= to); |
| 53 | int length = static_cast<int>(to - from); |
| 54 | CodePatcher destroyer(from, length); |
| 55 | while (length-- > 0) { |
| 56 | destroyer.masm()->int3(); |
| 57 | } |
| 58 | } |
| 59 | #endif |
| 60 | |
| 61 | |
| 62 | // Iterate through the entries of a SafepointTable that corresponds to |
| 63 | // deoptimization points. |
| 64 | class SafepointTableDeoptimiztionEntryIterator { |
| 65 | public: |
| 66 | explicit SafepointTableDeoptimiztionEntryIterator(Code* code) |
| 67 | : code_(code), table_(code), index_(-1), limit_(table_.length()) { |
| 68 | FindNextIndex(); |
| 69 | } |
| 70 | |
| 71 | SafepointEntry Next(Address* pc) { |
| 72 | if (index_ >= limit_) { |
| 73 | *pc = NULL; |
| 74 | return SafepointEntry(); // Invalid entry. |
| 75 | } |
| 76 | *pc = code_->instruction_start() + table_.GetPcOffset(index_); |
| 77 | SafepointEntry entry = table_.GetEntry(index_); |
| 78 | FindNextIndex(); |
| 79 | return entry; |
| 80 | } |
| 81 | |
| 82 | private: |
| 83 | void FindNextIndex() { |
| 84 | ASSERT(index_ < limit_); |
| 85 | while (++index_ < limit_) { |
| 86 | if (table_.GetEntry(index_).deoptimization_index() != |
| 87 | Safepoint::kNoDeoptimizationIndex) { |
| 88 | return; |
| 89 | } |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | Code* code_; |
| 94 | SafepointTable table_; |
| 95 | // Index of next deoptimization entry. If negative after calling |
| 96 | // FindNextIndex, there are no more, and Next will return an invalid |
| 97 | // SafepointEntry. |
| 98 | int index_; |
| 99 | // Table length. |
| 100 | int limit_; |
| 101 | }; |
| 102 | |
| 103 | |
| 104 | void Deoptimizer::DeoptimizeFunction(JSFunction* function) { |
| 105 | AssertNoAllocation no_allocation; |
| 106 | |
| 107 | if (!function->IsOptimized()) return; |
| 108 | |
| 109 | // Get the optimized code. |
| 110 | Code* code = function->code(); |
| 111 | |
| 112 | // Invalidate the relocation information, as it will become invalid by the |
| 113 | // code patching below, and is not needed any more. |
| 114 | code->InvalidateRelocation(); |
| 115 | |
| 116 | // For each return after a safepoint insert a absolute call to the |
| 117 | // corresponding deoptimization entry, or a short call to an absolute |
| 118 | // jump if space is short. The absolute jumps are put in a table just |
| 119 | // before the safepoint table (space was allocated there when the Code |
| 120 | // object was created, if necessary). |
| 121 | |
| 122 | Address instruction_start = function->code()->instruction_start(); |
| 123 | Address jump_table_address = |
| 124 | instruction_start + function->code()->safepoint_table_offset(); |
| 125 | Address previous_pc = instruction_start; |
| 126 | |
| 127 | SafepointTableDeoptimiztionEntryIterator deoptimizations(function->code()); |
| 128 | Address entry_pc = NULL; |
| 129 | |
| 130 | SafepointEntry current_entry = deoptimizations.Next(&entry_pc); |
| 131 | while (current_entry.is_valid()) { |
| 132 | int gap_code_size = current_entry.gap_code_size(); |
| 133 | unsigned deoptimization_index = current_entry.deoptimization_index(); |
| 134 | |
| 135 | #ifdef DEBUG |
| 136 | // Destroy the code which is not supposed to run again. |
| 137 | ZapCodeRange(previous_pc, entry_pc); |
| 138 | #endif |
| 139 | // Position where Call will be patched in. |
| 140 | Address call_address = entry_pc + gap_code_size; |
| 141 | // End of call instruction, if using a direct call to a 64-bit address. |
| 142 | Address call_end_address = |
| 143 | call_address + MacroAssembler::kCallInstructionLength; |
| 144 | |
| 145 | // Find next deoptimization entry, if any. |
| 146 | Address next_pc = NULL; |
| 147 | SafepointEntry next_entry = deoptimizations.Next(&next_pc); |
| 148 | |
| 149 | if (!next_entry.is_valid() || next_pc >= call_end_address) { |
| 150 | // Room enough to write a long call instruction. |
| 151 | CodePatcher patcher(call_address, Assembler::kCallInstructionLength); |
| 152 | patcher.masm()->Call(GetDeoptimizationEntry(deoptimization_index, LAZY), |
| 153 | RelocInfo::NONE); |
| 154 | previous_pc = call_end_address; |
| 155 | } else { |
| 156 | // Not room enough for a long Call instruction. Write a short call |
| 157 | // instruction to a long jump placed elsewhere in the code. |
| 158 | Address short_call_end_address = |
| 159 | call_address + MacroAssembler::kShortCallInstructionLength; |
| 160 | ASSERT(next_pc >= short_call_end_address); |
| 161 | |
| 162 | // Write jump in jump-table. |
| 163 | jump_table_address -= MacroAssembler::kJumpInstructionLength; |
| 164 | CodePatcher jump_patcher(jump_table_address, |
| 165 | MacroAssembler::kJumpInstructionLength); |
| 166 | jump_patcher.masm()->Jump( |
| 167 | GetDeoptimizationEntry(deoptimization_index, LAZY), |
| 168 | RelocInfo::NONE); |
| 169 | |
| 170 | // Write call to jump at call_offset. |
| 171 | CodePatcher call_patcher(call_address, |
| 172 | MacroAssembler::kShortCallInstructionLength); |
| 173 | call_patcher.masm()->call(jump_table_address); |
| 174 | previous_pc = short_call_end_address; |
| 175 | } |
| 176 | |
| 177 | // Continue with next deoptimization entry. |
| 178 | current_entry = next_entry; |
| 179 | entry_pc = next_pc; |
| 180 | } |
| 181 | |
| 182 | #ifdef DEBUG |
| 183 | // Destroy the code which is not supposed to run again. |
| 184 | ZapCodeRange(previous_pc, jump_table_address); |
| 185 | #endif |
| 186 | |
| 187 | // Add the deoptimizing code to the list. |
| 188 | DeoptimizingCodeListNode* node = new DeoptimizingCodeListNode(code); |
| 189 | node->set_next(deoptimizing_code_list_); |
| 190 | deoptimizing_code_list_ = node; |
| 191 | |
| 192 | // Set the code for the function to non-optimized version. |
| 193 | function->ReplaceCode(function->shared()->code()); |
| 194 | |
| 195 | if (FLAG_trace_deopt) { |
| 196 | PrintF("[forced deoptimization: "); |
| 197 | function->PrintName(); |
| 198 | PrintF(" / %" V8PRIxPTR "]\n", reinterpret_cast<intptr_t>(function)); |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | |
| 203 | void Deoptimizer::PatchStackCheckCodeAt(Address pc_after, |
| 204 | Code* check_code, |
| 205 | Code* replacement_code) { |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 206 | UNIMPLEMENTED(); |
| 207 | } |
| 208 | |
| 209 | |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame^] | 210 | void Deoptimizer::RevertStackCheckCodeAt(Address pc_after, |
| 211 | Code* check_code, |
| 212 | Code* replacement_code) { |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 213 | UNIMPLEMENTED(); |
| 214 | } |
| 215 | |
| 216 | |
| 217 | void Deoptimizer::DoComputeOsrOutputFrame() { |
| 218 | UNIMPLEMENTED(); |
| 219 | } |
| 220 | |
| 221 | |
| 222 | void Deoptimizer::DoComputeFrame(TranslationIterator* iterator, |
| 223 | int frame_index) { |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame^] | 224 | // Read the ast node id, function, and frame height for this output frame. |
| 225 | Translation::Opcode opcode = |
| 226 | static_cast<Translation::Opcode>(iterator->Next()); |
| 227 | USE(opcode); |
| 228 | ASSERT(Translation::FRAME == opcode); |
| 229 | int node_id = iterator->Next(); |
| 230 | JSFunction* function = JSFunction::cast(ComputeLiteral(iterator->Next())); |
| 231 | unsigned height = iterator->Next(); |
| 232 | unsigned height_in_bytes = height * kPointerSize; |
| 233 | if (FLAG_trace_deopt) { |
| 234 | PrintF(" translating "); |
| 235 | function->PrintName(); |
| 236 | PrintF(" => node=%d, height=%d\n", node_id, height_in_bytes); |
| 237 | } |
| 238 | |
| 239 | // The 'fixed' part of the frame consists of the incoming parameters and |
| 240 | // the part described by JavaScriptFrameConstants. |
| 241 | unsigned fixed_frame_size = ComputeFixedSize(function); |
| 242 | unsigned input_frame_size = static_cast<unsigned>(input_->GetFrameSize()); |
| 243 | unsigned output_frame_size = height_in_bytes + fixed_frame_size; |
| 244 | |
| 245 | // Allocate and store the output frame description. |
| 246 | FrameDescription* output_frame = |
| 247 | new(output_frame_size) FrameDescription(output_frame_size, function); |
| 248 | |
| 249 | bool is_bottommost = (0 == frame_index); |
| 250 | bool is_topmost = (output_count_ - 1 == frame_index); |
| 251 | ASSERT(frame_index >= 0 && frame_index < output_count_); |
| 252 | ASSERT(output_[frame_index] == NULL); |
| 253 | output_[frame_index] = output_frame; |
| 254 | |
| 255 | // The top address for the bottommost output frame can be computed from |
| 256 | // the input frame pointer and the output frame's height. For all |
| 257 | // subsequent output frames, it can be computed from the previous one's |
| 258 | // top address and the current frame's size. |
| 259 | intptr_t top_address; |
| 260 | if (is_bottommost) { |
| 261 | // 2 = context and function in the frame. |
| 262 | top_address = |
| 263 | input_->GetRegister(rbp.code()) - (2 * kPointerSize) - height_in_bytes; |
| 264 | } else { |
| 265 | top_address = output_[frame_index - 1]->GetTop() - output_frame_size; |
| 266 | } |
| 267 | output_frame->SetTop(top_address); |
| 268 | |
| 269 | // Compute the incoming parameter translation. |
| 270 | int parameter_count = function->shared()->formal_parameter_count() + 1; |
| 271 | unsigned output_offset = output_frame_size; |
| 272 | unsigned input_offset = input_frame_size; |
| 273 | for (int i = 0; i < parameter_count; ++i) { |
| 274 | output_offset -= kPointerSize; |
| 275 | DoTranslateCommand(iterator, frame_index, output_offset); |
| 276 | } |
| 277 | input_offset -= (parameter_count * kPointerSize); |
| 278 | |
| 279 | // There are no translation commands for the caller's pc and fp, the |
| 280 | // context, and the function. Synthesize their values and set them up |
| 281 | // explicitly. |
| 282 | // |
| 283 | // The caller's pc for the bottommost output frame is the same as in the |
| 284 | // input frame. For all subsequent output frames, it can be read from the |
| 285 | // previous one. This frame's pc can be computed from the non-optimized |
| 286 | // function code and AST id of the bailout. |
| 287 | output_offset -= kPointerSize; |
| 288 | input_offset -= kPointerSize; |
| 289 | intptr_t value; |
| 290 | if (is_bottommost) { |
| 291 | value = input_->GetFrameSlot(input_offset); |
| 292 | } else { |
| 293 | value = output_[frame_index - 1]->GetPc(); |
| 294 | } |
| 295 | output_frame->SetFrameSlot(output_offset, value); |
| 296 | if (FLAG_trace_deopt) { |
| 297 | PrintF(" 0x%08" V8PRIxPTR ": [top + %d] <- 0x%08" |
| 298 | V8PRIxPTR " ; caller's pc\n", |
| 299 | top_address + output_offset, output_offset, value); |
| 300 | } |
| 301 | |
| 302 | // The caller's frame pointer for the bottommost output frame is the same |
| 303 | // as in the input frame. For all subsequent output frames, it can be |
| 304 | // read from the previous one. Also compute and set this frame's frame |
| 305 | // pointer. |
| 306 | output_offset -= kPointerSize; |
| 307 | input_offset -= kPointerSize; |
| 308 | if (is_bottommost) { |
| 309 | value = input_->GetFrameSlot(input_offset); |
| 310 | } else { |
| 311 | value = output_[frame_index - 1]->GetFp(); |
| 312 | } |
| 313 | output_frame->SetFrameSlot(output_offset, value); |
| 314 | intptr_t fp_value = top_address + output_offset; |
| 315 | ASSERT(!is_bottommost || input_->GetRegister(rbp.code()) == fp_value); |
| 316 | output_frame->SetFp(fp_value); |
| 317 | if (is_topmost) output_frame->SetRegister(rbp.code(), fp_value); |
| 318 | if (FLAG_trace_deopt) { |
| 319 | PrintF(" 0x%08" V8PRIxPTR ": [top + %d] <- 0x%08" |
| 320 | V8PRIxPTR " ; caller's fp\n", |
| 321 | fp_value, output_offset, value); |
| 322 | } |
| 323 | |
| 324 | // The context can be gotten from the function so long as we don't |
| 325 | // optimize functions that need local contexts. |
| 326 | output_offset -= kPointerSize; |
| 327 | input_offset -= kPointerSize; |
| 328 | value = reinterpret_cast<intptr_t>(function->context()); |
| 329 | // The context for the bottommost output frame should also agree with the |
| 330 | // input frame. |
| 331 | ASSERT(!is_bottommost || input_->GetFrameSlot(input_offset) == value); |
| 332 | output_frame->SetFrameSlot(output_offset, value); |
| 333 | if (is_topmost) output_frame->SetRegister(rsi.code(), value); |
| 334 | if (FLAG_trace_deopt) { |
| 335 | PrintF(" 0x%08" V8PRIxPTR ": [top + %d] <- 0x%08" |
| 336 | V8PRIxPTR "; context\n", |
| 337 | top_address + output_offset, output_offset, value); |
| 338 | } |
| 339 | |
| 340 | // The function was mentioned explicitly in the BEGIN_FRAME. |
| 341 | output_offset -= kPointerSize; |
| 342 | input_offset -= kPointerSize; |
| 343 | value = reinterpret_cast<intptr_t>(function); |
| 344 | // The function for the bottommost output frame should also agree with the |
| 345 | // input frame. |
| 346 | ASSERT(!is_bottommost || input_->GetFrameSlot(input_offset) == value); |
| 347 | output_frame->SetFrameSlot(output_offset, value); |
| 348 | if (FLAG_trace_deopt) { |
| 349 | PrintF(" 0x%08" V8PRIxPTR ": [top + %d] <- 0x%08" |
| 350 | V8PRIxPTR "; function\n", |
| 351 | top_address + output_offset, output_offset, value); |
| 352 | } |
| 353 | |
| 354 | // Translate the rest of the frame. |
| 355 | for (unsigned i = 0; i < height; ++i) { |
| 356 | output_offset -= kPointerSize; |
| 357 | DoTranslateCommand(iterator, frame_index, output_offset); |
| 358 | } |
| 359 | ASSERT(0 == output_offset); |
| 360 | |
| 361 | // Compute this frame's PC, state, and continuation. |
| 362 | Code* non_optimized_code = function->shared()->code(); |
| 363 | FixedArray* raw_data = non_optimized_code->deoptimization_data(); |
| 364 | DeoptimizationOutputData* data = DeoptimizationOutputData::cast(raw_data); |
| 365 | Address start = non_optimized_code->instruction_start(); |
| 366 | unsigned pc_and_state = GetOutputInfo(data, node_id, function->shared()); |
| 367 | unsigned pc_offset = FullCodeGenerator::PcField::decode(pc_and_state); |
| 368 | intptr_t pc_value = reinterpret_cast<intptr_t>(start + pc_offset); |
| 369 | output_frame->SetPc(pc_value); |
| 370 | |
| 371 | FullCodeGenerator::State state = |
| 372 | FullCodeGenerator::StateField::decode(pc_and_state); |
| 373 | output_frame->SetState(Smi::FromInt(state)); |
| 374 | |
| 375 | // Set the continuation for the topmost frame. |
| 376 | if (is_topmost) { |
| 377 | Code* continuation = (bailout_type_ == EAGER) |
| 378 | ? Builtins::builtin(Builtins::NotifyDeoptimized) |
| 379 | : Builtins::builtin(Builtins::NotifyLazyDeoptimized); |
| 380 | output_frame->SetContinuation( |
| 381 | reinterpret_cast<intptr_t>(continuation->entry())); |
| 382 | } |
| 383 | |
| 384 | if (output_count_ - 1 == frame_index) iterator->Done(); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 385 | } |
| 386 | |
| 387 | |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame^] | 388 | #define __ masm()-> |
| 389 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 390 | void Deoptimizer::EntryGenerator::Generate() { |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame^] | 391 | GeneratePrologue(); |
| 392 | CpuFeatures::Scope scope(SSE2); |
| 393 | |
| 394 | // Save all general purpose registers before messing with them. |
| 395 | const int kNumberOfRegisters = Register::kNumRegisters; |
| 396 | |
| 397 | const int kDoubleRegsSize = kDoubleSize * |
| 398 | XMMRegister::kNumAllocatableRegisters; |
| 399 | __ subq(rsp, Immediate(kDoubleRegsSize)); |
| 400 | |
| 401 | for (int i = 0; i < XMMRegister::kNumAllocatableRegisters; ++i) { |
| 402 | XMMRegister xmm_reg = XMMRegister::FromAllocationIndex(i); |
| 403 | int offset = i * kDoubleSize; |
| 404 | __ movsd(Operand(rsp, offset), xmm_reg); |
| 405 | } |
| 406 | |
| 407 | // We push all registers onto the stack, even though we do not need |
| 408 | // to restore all later. |
| 409 | for (int i = 0; i < kNumberOfRegisters; i++) { |
| 410 | Register r = Register::toRegister(i); |
| 411 | __ push(r); |
| 412 | } |
| 413 | |
| 414 | const int kSavedRegistersAreaSize = kNumberOfRegisters * kPointerSize + |
| 415 | kDoubleRegsSize; |
| 416 | |
| 417 | // When calling new_deoptimizer_function we need to pass the last argument |
| 418 | // on the stack on windows and in r8 on linux. The remaining arguments are |
| 419 | // all passed in registers (different ones on linux and windows though). |
| 420 | |
| 421 | #ifdef _WIN64 |
| 422 | Register arg4 = r9; |
| 423 | Register arg3 = r8; |
| 424 | Register arg2 = rdx; |
| 425 | Register arg1 = rcx; |
| 426 | #else |
| 427 | Register arg4 = rcx; |
| 428 | Register arg3 = rdx; |
| 429 | Register arg2 = rsi; |
| 430 | Register arg1 = rdi; |
| 431 | #endif |
| 432 | |
| 433 | // We use this to keep the value of the fifth argument temporarily. |
| 434 | // Unfortunately we can't store it directly in r8 (used for passing |
| 435 | // this on linux), since it is another parameter passing register on windows. |
| 436 | Register arg5 = r11; |
| 437 | |
| 438 | // Get the bailout id from the stack. |
| 439 | __ movq(arg3, Operand(rsp, kSavedRegistersAreaSize)); |
| 440 | |
| 441 | // Get the address of the location in the code object if possible |
| 442 | // and compute the fp-to-sp delta in register arg5. |
| 443 | if (type() == EAGER) { |
| 444 | __ Set(arg4, 0); |
| 445 | __ lea(arg5, Operand(rsp, kSavedRegistersAreaSize + 1 * kPointerSize)); |
| 446 | } else { |
| 447 | __ movq(arg4, Operand(rsp, kSavedRegistersAreaSize + 1 * kPointerSize)); |
| 448 | __ lea(arg5, Operand(rsp, kSavedRegistersAreaSize + 2 * kPointerSize)); |
| 449 | } |
| 450 | |
| 451 | __ subq(arg5, rbp); |
| 452 | __ neg(arg5); |
| 453 | |
| 454 | // Allocate a new deoptimizer object. |
| 455 | __ PrepareCallCFunction(5); |
| 456 | __ movq(rax, Operand(rbp, JavaScriptFrameConstants::kFunctionOffset)); |
| 457 | __ movq(arg1, rax); |
| 458 | __ movq(arg2, Immediate(type())); |
| 459 | // Args 3 and 4 are already in the right registers. |
| 460 | |
| 461 | // On windows put the argument on the stack (PrepareCallCFunction have |
| 462 | // created space for this). On linux pass the argument in r8. |
| 463 | #ifdef _WIN64 |
| 464 | __ movq(Operand(rsp, 0 * kPointerSize), arg5); |
| 465 | #else |
| 466 | __ movq(r8, arg5); |
| 467 | #endif |
| 468 | |
| 469 | __ CallCFunction(ExternalReference::new_deoptimizer_function(), 5); |
| 470 | // Preserve deoptimizer object in register rax and get the input |
| 471 | // frame descriptor pointer. |
| 472 | __ movq(rbx, Operand(rax, Deoptimizer::input_offset())); |
| 473 | |
| 474 | // Fill in the input registers. |
| 475 | for (int i = kNumberOfRegisters -1; i >= 0; i--) { |
| 476 | int offset = (i * kPointerSize) + FrameDescription::registers_offset(); |
| 477 | __ pop(Operand(rbx, offset)); |
| 478 | } |
| 479 | |
| 480 | // Fill in the double input registers. |
| 481 | int double_regs_offset = FrameDescription::double_registers_offset(); |
| 482 | for (int i = 0; i < XMMRegister::kNumAllocatableRegisters; i++) { |
| 483 | int dst_offset = i * kDoubleSize + double_regs_offset; |
| 484 | __ pop(Operand(rbx, dst_offset)); |
| 485 | } |
| 486 | |
| 487 | // Remove the bailout id from the stack. |
| 488 | if (type() == EAGER) { |
| 489 | __ addq(rsp, Immediate(kPointerSize)); |
| 490 | } else { |
| 491 | __ addq(rsp, Immediate(2 * kPointerSize)); |
| 492 | } |
| 493 | |
| 494 | // Compute a pointer to the unwinding limit in register rcx; that is |
| 495 | // the first stack slot not part of the input frame. |
| 496 | __ movq(rcx, Operand(rbx, FrameDescription::frame_size_offset())); |
| 497 | __ addq(rcx, rsp); |
| 498 | |
| 499 | // Unwind the stack down to - but not including - the unwinding |
| 500 | // limit and copy the contents of the activation frame to the input |
| 501 | // frame description. |
| 502 | __ lea(rdx, Operand(rbx, FrameDescription::frame_content_offset())); |
| 503 | Label pop_loop; |
| 504 | __ bind(&pop_loop); |
| 505 | __ pop(Operand(rdx, 0)); |
| 506 | __ addq(rdx, Immediate(sizeof(intptr_t))); |
| 507 | __ cmpq(rcx, rsp); |
| 508 | __ j(not_equal, &pop_loop); |
| 509 | |
| 510 | // Compute the output frame in the deoptimizer. |
| 511 | __ push(rax); |
| 512 | __ PrepareCallCFunction(1); |
| 513 | __ movq(arg1, rax); |
| 514 | __ CallCFunction(ExternalReference::compute_output_frames_function(), 1); |
| 515 | __ pop(rax); |
| 516 | |
| 517 | // Replace the current frame with the output frames. |
| 518 | Label outer_push_loop, inner_push_loop; |
| 519 | // Outer loop state: rax = current FrameDescription**, rdx = one past the |
| 520 | // last FrameDescription**. |
| 521 | __ movl(rdx, Operand(rax, Deoptimizer::output_count_offset())); |
| 522 | __ movq(rax, Operand(rax, Deoptimizer::output_offset())); |
| 523 | __ lea(rdx, Operand(rax, rdx, times_8, 0)); |
| 524 | __ bind(&outer_push_loop); |
| 525 | // Inner loop state: rbx = current FrameDescription*, rcx = loop index. |
| 526 | __ movq(rbx, Operand(rax, 0)); |
| 527 | __ movq(rcx, Operand(rbx, FrameDescription::frame_size_offset())); |
| 528 | __ bind(&inner_push_loop); |
| 529 | __ subq(rcx, Immediate(sizeof(intptr_t))); |
| 530 | __ push(Operand(rbx, rcx, times_1, FrameDescription::frame_content_offset())); |
| 531 | __ testq(rcx, rcx); |
| 532 | __ j(not_zero, &inner_push_loop); |
| 533 | __ addq(rax, Immediate(kPointerSize)); |
| 534 | __ cmpq(rax, rdx); |
| 535 | __ j(below, &outer_push_loop); |
| 536 | |
| 537 | // In case of OSR, we have to restore the XMM registers. |
| 538 | if (type() == OSR) { |
| 539 | for (int i = 0; i < XMMRegister::kNumAllocatableRegisters; ++i) { |
| 540 | XMMRegister xmm_reg = XMMRegister::FromAllocationIndex(i); |
| 541 | int src_offset = i * kDoubleSize + double_regs_offset; |
| 542 | __ movsd(xmm_reg, Operand(rbx, src_offset)); |
| 543 | } |
| 544 | } |
| 545 | |
| 546 | // Push state, pc, and continuation from the last output frame. |
| 547 | if (type() != OSR) { |
| 548 | __ push(Operand(rbx, FrameDescription::state_offset())); |
| 549 | } |
| 550 | __ push(Operand(rbx, FrameDescription::pc_offset())); |
| 551 | __ push(Operand(rbx, FrameDescription::continuation_offset())); |
| 552 | |
| 553 | // Push the registers from the last output frame. |
| 554 | for (int i = 0; i < kNumberOfRegisters; i++) { |
| 555 | int offset = (i * kPointerSize) + FrameDescription::registers_offset(); |
| 556 | __ push(Operand(rbx, offset)); |
| 557 | } |
| 558 | |
| 559 | // Restore the registers from the stack. |
| 560 | for (int i = kNumberOfRegisters - 1; i >= 0 ; i--) { |
| 561 | Register r = Register::toRegister(i); |
| 562 | // Do not restore rsp, simply pop the value into the next register |
| 563 | // and overwrite this afterwards. |
| 564 | if (r.is(rsp)) { |
| 565 | ASSERT(i > 0); |
| 566 | r = Register::toRegister(i - 1); |
| 567 | } |
| 568 | __ pop(r); |
| 569 | } |
| 570 | |
| 571 | // Set up the roots register. |
| 572 | ExternalReference roots_address = ExternalReference::roots_address(); |
| 573 | __ movq(r13, roots_address); |
| 574 | |
| 575 | __ movq(kSmiConstantRegister, |
| 576 | reinterpret_cast<uint64_t>(Smi::FromInt(kSmiConstantRegisterValue)), |
| 577 | RelocInfo::NONE); |
| 578 | |
| 579 | // Return to the continuation point. |
| 580 | __ ret(0); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 581 | } |
| 582 | |
| 583 | |
| 584 | void Deoptimizer::TableEntryGenerator::GeneratePrologue() { |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame^] | 585 | // Create a sequence of deoptimization entries. |
| 586 | Label done; |
| 587 | for (int i = 0; i < count(); i++) { |
| 588 | int start = masm()->pc_offset(); |
| 589 | USE(start); |
| 590 | __ push_imm32(i); |
| 591 | __ jmp(&done); |
| 592 | ASSERT(masm()->pc_offset() - start == table_entry_size_); |
| 593 | } |
| 594 | __ bind(&done); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 595 | } |
| 596 | |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame^] | 597 | #undef __ |
| 598 | |
| 599 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 600 | } } // namespace v8::internal |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 601 | |
| 602 | #endif // V8_TARGET_ARCH_X64 |