Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame^] | 1 | // Copyright 2010 the V8 project authors. All rights reserved. |
| 2 | // Redistribution and use in source and binary forms, with or without |
| 3 | // modification, are permitted provided that the following conditions are |
| 4 | // met: |
| 5 | // |
| 6 | // * Redistributions of source code must retain the above copyright |
| 7 | // notice, this list of conditions and the following disclaimer. |
| 8 | // * Redistributions in binary form must reproduce the above |
| 9 | // copyright notice, this list of conditions and the following |
| 10 | // disclaimer in the documentation and/or other materials provided |
| 11 | // with the distribution. |
| 12 | // * Neither the name of Google Inc. nor the names of its |
| 13 | // contributors may be used to endorse or promote products derived |
| 14 | // from this software without specific prior written permission. |
| 15 | // |
| 16 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | |
| 28 | #include "v8.h" |
| 29 | |
| 30 | #include "codegen.h" |
| 31 | #include "deoptimizer.h" |
| 32 | #include "full-codegen.h" |
| 33 | #include "safepoint-table.h" |
| 34 | |
| 35 | namespace v8 { |
| 36 | namespace internal { |
| 37 | |
| 38 | |
| 39 | int Deoptimizer::table_entry_size_ = 10; |
| 40 | |
| 41 | void Deoptimizer::DeoptimizeFunction(JSFunction* function) { |
| 42 | AssertNoAllocation no_allocation; |
| 43 | |
| 44 | if (!function->IsOptimized()) return; |
| 45 | |
| 46 | // Get the optimized code. |
| 47 | Code* code = function->code(); |
| 48 | |
| 49 | // Invalidate the relocation information, as it will become invalid by the |
| 50 | // code patching below, and is not needed any more. |
| 51 | code->InvalidateRelocation(); |
| 52 | |
| 53 | // For each return after a safepoint insert a absolute call to the |
| 54 | // corresponding deoptimization entry. |
| 55 | unsigned last_pc_offset = 0; |
| 56 | SafepointTable table(function->code()); |
| 57 | for (unsigned i = 0; i < table.length(); i++) { |
| 58 | unsigned pc_offset = table.GetPcOffset(i); |
| 59 | int deoptimization_index = table.GetDeoptimizationIndex(i); |
| 60 | int gap_code_size = table.GetGapCodeSize(i); |
| 61 | #ifdef DEBUG |
| 62 | // Destroy the code which is not supposed to run again. |
| 63 | unsigned instructions = pc_offset - last_pc_offset; |
| 64 | CodePatcher destroyer(code->instruction_start() + last_pc_offset, |
| 65 | instructions); |
| 66 | for (unsigned i = 0; i < instructions; i++) { |
| 67 | destroyer.masm()->int3(); |
| 68 | } |
| 69 | #endif |
| 70 | last_pc_offset = pc_offset; |
| 71 | if (deoptimization_index != Safepoint::kNoDeoptimizationIndex) { |
| 72 | CodePatcher patcher( |
| 73 | code->instruction_start() + pc_offset + gap_code_size, |
| 74 | Assembler::kCallInstructionLength); |
| 75 | patcher.masm()->call(GetDeoptimizationEntry(deoptimization_index, LAZY), |
| 76 | RelocInfo::NONE); |
| 77 | last_pc_offset += gap_code_size + Assembler::kCallInstructionLength; |
| 78 | } |
| 79 | } |
| 80 | #ifdef DEBUG |
| 81 | // Destroy the code which is not supposed to run again. |
| 82 | unsigned instructions = code->safepoint_table_start() - last_pc_offset; |
| 83 | CodePatcher destroyer(code->instruction_start() + last_pc_offset, |
| 84 | instructions); |
| 85 | for (unsigned i = 0; i < instructions; i++) { |
| 86 | destroyer.masm()->int3(); |
| 87 | } |
| 88 | #endif |
| 89 | |
| 90 | // Add the deoptimizing code to the list. |
| 91 | DeoptimizingCodeListNode* node = new DeoptimizingCodeListNode(code); |
| 92 | node->set_next(deoptimizing_code_list_); |
| 93 | deoptimizing_code_list_ = node; |
| 94 | |
| 95 | // Set the code for the function to non-optimized version. |
| 96 | function->ReplaceCode(function->shared()->code()); |
| 97 | |
| 98 | if (FLAG_trace_deopt) { |
| 99 | PrintF("[forced deoptimization: "); |
| 100 | function->PrintName(); |
| 101 | PrintF(" / %x]\n", reinterpret_cast<uint32_t>(function)); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | |
| 106 | void Deoptimizer::PatchStackCheckCode(RelocInfo* rinfo, |
| 107 | Code* replacement_code) { |
| 108 | // The stack check code matches the pattern (on ia32, for example): |
| 109 | // |
| 110 | // cmp esp, <limit> |
| 111 | // jae ok |
| 112 | // call <stack guard> |
| 113 | // ok: ... |
| 114 | // |
| 115 | // We will patch the code to: |
| 116 | // |
| 117 | // cmp esp, <limit> ;; Not changed |
| 118 | // nop |
| 119 | // nop |
| 120 | // call <on-stack replacment> |
| 121 | // ok: |
| 122 | Address call_target_address = rinfo->pc(); |
| 123 | ASSERT(*(call_target_address - 3) == 0x73 && // jae |
| 124 | *(call_target_address - 2) == 0x05 && // offset |
| 125 | *(call_target_address - 1) == 0xe8); // call |
| 126 | *(call_target_address - 3) = 0x90; // nop |
| 127 | *(call_target_address - 2) = 0x90; // nop |
| 128 | rinfo->set_target_address(replacement_code->entry()); |
| 129 | } |
| 130 | |
| 131 | |
| 132 | void Deoptimizer::RevertStackCheckCode(RelocInfo* rinfo, Code* check_code) { |
| 133 | Address call_target_address = rinfo->pc(); |
| 134 | ASSERT(*(call_target_address - 3) == 0x90 && // nop |
| 135 | *(call_target_address - 2) == 0x90 && // nop |
| 136 | *(call_target_address - 1) == 0xe8); // call |
| 137 | *(call_target_address - 3) = 0x73; // jae |
| 138 | *(call_target_address - 2) = 0x05; // offset |
| 139 | rinfo->set_target_address(check_code->entry()); |
| 140 | } |
| 141 | |
| 142 | |
| 143 | static int LookupBailoutId(DeoptimizationInputData* data, unsigned ast_id) { |
| 144 | ByteArray* translations = data->TranslationByteArray(); |
| 145 | int length = data->DeoptCount(); |
| 146 | for (int i = 0; i < length; i++) { |
| 147 | if (static_cast<unsigned>(data->AstId(i)->value()) == ast_id) { |
| 148 | TranslationIterator it(translations, data->TranslationIndex(i)->value()); |
| 149 | int value = it.Next(); |
| 150 | ASSERT(Translation::BEGIN == static_cast<Translation::Opcode>(value)); |
| 151 | // Read the number of frames. |
| 152 | value = it.Next(); |
| 153 | if (value == 1) return i; |
| 154 | } |
| 155 | } |
| 156 | UNREACHABLE(); |
| 157 | return -1; |
| 158 | } |
| 159 | |
| 160 | |
| 161 | void Deoptimizer::DoComputeOsrOutputFrame() { |
| 162 | DeoptimizationInputData* data = DeoptimizationInputData::cast( |
| 163 | optimized_code_->deoptimization_data()); |
| 164 | unsigned ast_id = data->OsrAstId()->value(); |
| 165 | // TODO(kasperl): This should not be the bailout_id_. It should be |
| 166 | // the ast id. Confusing. |
| 167 | ASSERT(bailout_id_ == ast_id); |
| 168 | |
| 169 | int bailout_id = LookupBailoutId(data, ast_id); |
| 170 | unsigned translation_index = data->TranslationIndex(bailout_id)->value(); |
| 171 | ByteArray* translations = data->TranslationByteArray(); |
| 172 | |
| 173 | TranslationIterator iterator(translations, translation_index); |
| 174 | Translation::Opcode opcode = |
| 175 | static_cast<Translation::Opcode>(iterator.Next()); |
| 176 | ASSERT(Translation::BEGIN == opcode); |
| 177 | USE(opcode); |
| 178 | int count = iterator.Next(); |
| 179 | ASSERT(count == 1); |
| 180 | USE(count); |
| 181 | |
| 182 | opcode = static_cast<Translation::Opcode>(iterator.Next()); |
| 183 | USE(opcode); |
| 184 | ASSERT(Translation::FRAME == opcode); |
| 185 | unsigned node_id = iterator.Next(); |
| 186 | USE(node_id); |
| 187 | ASSERT(node_id == ast_id); |
| 188 | JSFunction* function = JSFunction::cast(ComputeLiteral(iterator.Next())); |
| 189 | USE(function); |
| 190 | ASSERT(function == function_); |
| 191 | unsigned height = iterator.Next(); |
| 192 | unsigned height_in_bytes = height * kPointerSize; |
| 193 | USE(height_in_bytes); |
| 194 | |
| 195 | unsigned fixed_size = ComputeFixedSize(function_); |
| 196 | unsigned input_frame_size = input_->GetFrameSize(); |
| 197 | ASSERT(fixed_size + height_in_bytes == input_frame_size); |
| 198 | |
| 199 | unsigned stack_slot_size = optimized_code_->stack_slots() * kPointerSize; |
| 200 | unsigned outgoing_height = data->ArgumentsStackHeight(bailout_id)->value(); |
| 201 | unsigned outgoing_size = outgoing_height * kPointerSize; |
| 202 | unsigned output_frame_size = fixed_size + stack_slot_size + outgoing_size; |
| 203 | ASSERT(outgoing_size == 0); // OSR does not happen in the middle of a call. |
| 204 | |
| 205 | if (FLAG_trace_osr) { |
| 206 | PrintF("[on-stack replacement: begin 0x%08" V8PRIxPTR " ", |
| 207 | reinterpret_cast<intptr_t>(function_)); |
| 208 | function_->PrintName(); |
| 209 | PrintF(" => node=%u, frame=%d->%d]\n", |
| 210 | ast_id, |
| 211 | input_frame_size, |
| 212 | output_frame_size); |
| 213 | } |
| 214 | |
| 215 | // There's only one output frame in the OSR case. |
| 216 | output_count_ = 1; |
| 217 | output_ = new FrameDescription*[1]; |
| 218 | output_[0] = new(output_frame_size) FrameDescription( |
| 219 | output_frame_size, function_); |
| 220 | |
| 221 | // Clear the incoming parameters in the optimized frame to avoid |
| 222 | // confusing the garbage collector. |
| 223 | unsigned output_offset = output_frame_size - kPointerSize; |
| 224 | int parameter_count = function_->shared()->formal_parameter_count() + 1; |
| 225 | for (int i = 0; i < parameter_count; ++i) { |
| 226 | output_[0]->SetFrameSlot(output_offset, 0); |
| 227 | output_offset -= kPointerSize; |
| 228 | } |
| 229 | |
| 230 | // Translate the incoming parameters. This may overwrite some of the |
| 231 | // incoming argument slots we've just cleared. |
| 232 | int input_offset = input_frame_size - kPointerSize; |
| 233 | bool ok = true; |
| 234 | int limit = input_offset - (parameter_count * kPointerSize); |
| 235 | while (ok && input_offset > limit) { |
| 236 | ok = DoOsrTranslateCommand(&iterator, &input_offset); |
| 237 | } |
| 238 | |
| 239 | // There are no translation commands for the caller's pc and fp, the |
| 240 | // context, and the function. Set them up explicitly. |
| 241 | for (int i = 0; ok && i < 4; i++) { |
| 242 | uint32_t input_value = input_->GetFrameSlot(input_offset); |
| 243 | if (FLAG_trace_osr) { |
| 244 | PrintF(" [esp + %d] <- 0x%08x ; [esp + %d] (fixed part)\n", |
| 245 | output_offset, |
| 246 | input_value, |
| 247 | input_offset); |
| 248 | } |
| 249 | output_[0]->SetFrameSlot(output_offset, input_->GetFrameSlot(input_offset)); |
| 250 | input_offset -= kPointerSize; |
| 251 | output_offset -= kPointerSize; |
| 252 | } |
| 253 | |
| 254 | // Translate the rest of the frame. |
| 255 | while (ok && input_offset >= 0) { |
| 256 | ok = DoOsrTranslateCommand(&iterator, &input_offset); |
| 257 | } |
| 258 | |
| 259 | // If translation of any command failed, continue using the input frame. |
| 260 | if (!ok) { |
| 261 | delete output_[0]; |
| 262 | output_[0] = input_; |
| 263 | output_[0]->SetPc(reinterpret_cast<uint32_t>(from_)); |
| 264 | } else { |
| 265 | // Setup the frame pointer and the context pointer. |
| 266 | output_[0]->SetRegister(ebp.code(), input_->GetRegister(ebp.code())); |
| 267 | output_[0]->SetRegister(esi.code(), input_->GetRegister(esi.code())); |
| 268 | |
| 269 | unsigned pc_offset = data->OsrPcOffset()->value(); |
| 270 | uint32_t pc = reinterpret_cast<uint32_t>( |
| 271 | optimized_code_->entry() + pc_offset); |
| 272 | output_[0]->SetPc(pc); |
| 273 | } |
| 274 | Code* continuation = Builtins::builtin(Builtins::NotifyOSR); |
| 275 | output_[0]->SetContinuation( |
| 276 | reinterpret_cast<uint32_t>(continuation->entry())); |
| 277 | |
| 278 | if (FLAG_trace_osr) { |
| 279 | PrintF("[on-stack replacement translation %s: 0x%08" V8PRIxPTR " ", |
| 280 | ok ? "finished" : "aborted", |
| 281 | reinterpret_cast<intptr_t>(function)); |
| 282 | function->PrintName(); |
| 283 | PrintF(" => pc=0x%0x]\n", output_[0]->GetPc()); |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | |
| 288 | void Deoptimizer::DoComputeFrame(TranslationIterator* iterator, |
| 289 | int frame_index) { |
| 290 | // Read the ast node id, function, and frame height for this output frame. |
| 291 | Translation::Opcode opcode = |
| 292 | static_cast<Translation::Opcode>(iterator->Next()); |
| 293 | USE(opcode); |
| 294 | ASSERT(Translation::FRAME == opcode); |
| 295 | int node_id = iterator->Next(); |
| 296 | JSFunction* function = JSFunction::cast(ComputeLiteral(iterator->Next())); |
| 297 | unsigned height = iterator->Next(); |
| 298 | unsigned height_in_bytes = height * kPointerSize; |
| 299 | if (FLAG_trace_deopt) { |
| 300 | PrintF(" translating "); |
| 301 | function->PrintName(); |
| 302 | PrintF(" => node=%d, height=%d\n", node_id, height_in_bytes); |
| 303 | } |
| 304 | |
| 305 | // The 'fixed' part of the frame consists of the incoming parameters and |
| 306 | // the part described by JavaScriptFrameConstants. |
| 307 | unsigned fixed_frame_size = ComputeFixedSize(function); |
| 308 | unsigned input_frame_size = input_->GetFrameSize(); |
| 309 | unsigned output_frame_size = height_in_bytes + fixed_frame_size; |
| 310 | |
| 311 | // Allocate and store the output frame description. |
| 312 | FrameDescription* output_frame = |
| 313 | new(output_frame_size) FrameDescription(output_frame_size, function); |
| 314 | |
| 315 | bool is_bottommost = (0 == frame_index); |
| 316 | bool is_topmost = (output_count_ - 1 == frame_index); |
| 317 | ASSERT(frame_index >= 0 && frame_index < output_count_); |
| 318 | ASSERT(output_[frame_index] == NULL); |
| 319 | output_[frame_index] = output_frame; |
| 320 | |
| 321 | // The top address for the bottommost output frame can be computed from |
| 322 | // the input frame pointer and the output frame's height. For all |
| 323 | // subsequent output frames, it can be computed from the previous one's |
| 324 | // top address and the current frame's size. |
| 325 | uint32_t top_address; |
| 326 | if (is_bottommost) { |
| 327 | // 2 = context and function in the frame. |
| 328 | top_address = |
| 329 | input_->GetRegister(ebp.code()) - (2 * kPointerSize) - height_in_bytes; |
| 330 | } else { |
| 331 | top_address = output_[frame_index - 1]->GetTop() - output_frame_size; |
| 332 | } |
| 333 | output_frame->SetTop(top_address); |
| 334 | |
| 335 | // Compute the incoming parameter translation. |
| 336 | int parameter_count = function->shared()->formal_parameter_count() + 1; |
| 337 | unsigned output_offset = output_frame_size; |
| 338 | unsigned input_offset = input_frame_size; |
| 339 | for (int i = 0; i < parameter_count; ++i) { |
| 340 | output_offset -= kPointerSize; |
| 341 | DoTranslateCommand(iterator, frame_index, output_offset); |
| 342 | } |
| 343 | input_offset -= (parameter_count * kPointerSize); |
| 344 | |
| 345 | // There are no translation commands for the caller's pc and fp, the |
| 346 | // context, and the function. Synthesize their values and set them up |
| 347 | // explicitly. |
| 348 | // |
| 349 | // The caller's pc for the bottommost output frame is the same as in the |
| 350 | // input frame. For all subsequent output frames, it can be read from the |
| 351 | // previous one. This frame's pc can be computed from the non-optimized |
| 352 | // function code and AST id of the bailout. |
| 353 | output_offset -= kPointerSize; |
| 354 | input_offset -= kPointerSize; |
| 355 | intptr_t value; |
| 356 | if (is_bottommost) { |
| 357 | value = input_->GetFrameSlot(input_offset); |
| 358 | } else { |
| 359 | value = output_[frame_index - 1]->GetPc(); |
| 360 | } |
| 361 | output_frame->SetFrameSlot(output_offset, value); |
| 362 | if (FLAG_trace_deopt) { |
| 363 | PrintF(" 0x%08x: [top + %d] <- 0x%08x ; caller's pc\n", |
| 364 | top_address + output_offset, output_offset, value); |
| 365 | } |
| 366 | |
| 367 | // The caller's frame pointer for the bottommost output frame is the same |
| 368 | // as in the input frame. For all subsequent output frames, it can be |
| 369 | // read from the previous one. Also compute and set this frame's frame |
| 370 | // pointer. |
| 371 | output_offset -= kPointerSize; |
| 372 | input_offset -= kPointerSize; |
| 373 | if (is_bottommost) { |
| 374 | value = input_->GetFrameSlot(input_offset); |
| 375 | } else { |
| 376 | value = output_[frame_index - 1]->GetFp(); |
| 377 | } |
| 378 | output_frame->SetFrameSlot(output_offset, value); |
| 379 | intptr_t fp_value = top_address + output_offset; |
| 380 | ASSERT(!is_bottommost || input_->GetRegister(ebp.code()) == fp_value); |
| 381 | output_frame->SetFp(fp_value); |
| 382 | if (is_topmost) output_frame->SetRegister(ebp.code(), fp_value); |
| 383 | if (FLAG_trace_deopt) { |
| 384 | PrintF(" 0x%08x: [top + %d] <- 0x%08x ; caller's fp\n", |
| 385 | fp_value, output_offset, value); |
| 386 | } |
| 387 | |
| 388 | // The context can be gotten from the function so long as we don't |
| 389 | // optimize functions that need local contexts. |
| 390 | output_offset -= kPointerSize; |
| 391 | input_offset -= kPointerSize; |
| 392 | value = reinterpret_cast<uint32_t>(function->context()); |
| 393 | // The context for the bottommost output frame should also agree with the |
| 394 | // input frame. |
| 395 | ASSERT(!is_bottommost || input_->GetFrameSlot(input_offset) == value); |
| 396 | output_frame->SetFrameSlot(output_offset, value); |
| 397 | if (is_topmost) output_frame->SetRegister(esi.code(), value); |
| 398 | if (FLAG_trace_deopt) { |
| 399 | PrintF(" 0x%08x: [top + %d] <- 0x%08x ; context\n", |
| 400 | top_address + output_offset, output_offset, value); |
| 401 | } |
| 402 | |
| 403 | // The function was mentioned explicitly in the BEGIN_FRAME. |
| 404 | output_offset -= kPointerSize; |
| 405 | input_offset -= kPointerSize; |
| 406 | value = reinterpret_cast<uint32_t>(function); |
| 407 | // The function for the bottommost output frame should also agree with the |
| 408 | // input frame. |
| 409 | ASSERT(!is_bottommost || input_->GetFrameSlot(input_offset) == value); |
| 410 | output_frame->SetFrameSlot(output_offset, value); |
| 411 | if (FLAG_trace_deopt) { |
| 412 | PrintF(" 0x%08x: [top + %d] <- 0x%08x ; function\n", |
| 413 | top_address + output_offset, output_offset, value); |
| 414 | } |
| 415 | |
| 416 | // Translate the rest of the frame. |
| 417 | for (unsigned i = 0; i < height; ++i) { |
| 418 | output_offset -= kPointerSize; |
| 419 | DoTranslateCommand(iterator, frame_index, output_offset); |
| 420 | } |
| 421 | ASSERT(0 == output_offset); |
| 422 | |
| 423 | // Compute this frame's PC, state, and continuation. |
| 424 | Code* non_optimized_code = function->shared()->code(); |
| 425 | FixedArray* raw_data = non_optimized_code->deoptimization_data(); |
| 426 | DeoptimizationOutputData* data = DeoptimizationOutputData::cast(raw_data); |
| 427 | Address start = non_optimized_code->instruction_start(); |
| 428 | unsigned pc_and_state = GetOutputInfo(data, node_id, function->shared()); |
| 429 | unsigned pc_offset = FullCodeGenerator::PcField::decode(pc_and_state); |
| 430 | uint32_t pc_value = reinterpret_cast<uint32_t>(start + pc_offset); |
| 431 | output_frame->SetPc(pc_value); |
| 432 | |
| 433 | FullCodeGenerator::State state = |
| 434 | FullCodeGenerator::StateField::decode(pc_and_state); |
| 435 | output_frame->SetState(Smi::FromInt(state)); |
| 436 | |
| 437 | // Set the continuation for the topmost frame. |
| 438 | if (is_topmost) { |
| 439 | Code* continuation = (bailout_type_ == EAGER) |
| 440 | ? Builtins::builtin(Builtins::NotifyDeoptimized) |
| 441 | : Builtins::builtin(Builtins::NotifyLazyDeoptimized); |
| 442 | output_frame->SetContinuation( |
| 443 | reinterpret_cast<uint32_t>(continuation->entry())); |
| 444 | } |
| 445 | |
| 446 | if (output_count_ - 1 == frame_index) iterator->Done(); |
| 447 | } |
| 448 | |
| 449 | |
| 450 | #define __ masm()-> |
| 451 | |
| 452 | void Deoptimizer::EntryGenerator::Generate() { |
| 453 | GeneratePrologue(); |
| 454 | CpuFeatures::Scope scope(SSE2); |
| 455 | |
| 456 | // Save all general purpose registers before messing with them. |
| 457 | const int kNumberOfRegisters = Register::kNumRegisters; |
| 458 | |
| 459 | const int kDoubleRegsSize = kDoubleSize * |
| 460 | XMMRegister::kNumAllocatableRegisters; |
| 461 | __ sub(Operand(esp), Immediate(kDoubleRegsSize)); |
| 462 | for (int i = 0; i < XMMRegister::kNumAllocatableRegisters; ++i) { |
| 463 | XMMRegister xmm_reg = XMMRegister::FromAllocationIndex(i); |
| 464 | int offset = i * kDoubleSize; |
| 465 | __ movdbl(Operand(esp, offset), xmm_reg); |
| 466 | } |
| 467 | |
| 468 | __ pushad(); |
| 469 | |
| 470 | const int kSavedRegistersAreaSize = kNumberOfRegisters * kPointerSize + |
| 471 | kDoubleRegsSize; |
| 472 | |
| 473 | // Get the bailout id from the stack. |
| 474 | __ mov(ebx, Operand(esp, kSavedRegistersAreaSize)); |
| 475 | |
| 476 | // Get the address of the location in the code object if possible |
| 477 | // and compute the fp-to-sp delta in register edx. |
| 478 | if (type() == EAGER) { |
| 479 | __ Set(ecx, Immediate(0)); |
| 480 | __ lea(edx, Operand(esp, kSavedRegistersAreaSize + 1 * kPointerSize)); |
| 481 | } else { |
| 482 | __ mov(ecx, Operand(esp, kSavedRegistersAreaSize + 1 * kPointerSize)); |
| 483 | __ lea(edx, Operand(esp, kSavedRegistersAreaSize + 2 * kPointerSize)); |
| 484 | } |
| 485 | __ sub(edx, Operand(ebp)); |
| 486 | __ neg(edx); |
| 487 | |
| 488 | // Allocate a new deoptimizer object. |
| 489 | __ PrepareCallCFunction(5, eax); |
| 490 | __ mov(eax, Operand(ebp, JavaScriptFrameConstants::kFunctionOffset)); |
| 491 | __ mov(Operand(esp, 0 * kPointerSize), eax); // Function. |
| 492 | __ mov(Operand(esp, 1 * kPointerSize), Immediate(type())); // Bailout type. |
| 493 | __ mov(Operand(esp, 2 * kPointerSize), ebx); // Bailout id. |
| 494 | __ mov(Operand(esp, 3 * kPointerSize), ecx); // Code address or 0. |
| 495 | __ mov(Operand(esp, 4 * kPointerSize), edx); // Fp-to-sp delta. |
| 496 | __ CallCFunction(ExternalReference::new_deoptimizer_function(), 5); |
| 497 | |
| 498 | // Preserve deoptimizer object in register eax and get the input |
| 499 | // frame descriptor pointer. |
| 500 | __ mov(ebx, Operand(eax, Deoptimizer::input_offset())); |
| 501 | |
| 502 | // Fill in the input registers. |
| 503 | for (int i = 0; i < kNumberOfRegisters; i++) { |
| 504 | int offset = (i * kIntSize) + FrameDescription::registers_offset(); |
| 505 | __ mov(ecx, Operand(esp, (kNumberOfRegisters - 1 - i) * kPointerSize)); |
| 506 | __ mov(Operand(ebx, offset), ecx); |
| 507 | } |
| 508 | |
| 509 | // Fill in the double input registers. |
| 510 | int double_regs_offset = FrameDescription::double_registers_offset(); |
| 511 | for (int i = 0; i < XMMRegister::kNumAllocatableRegisters; ++i) { |
| 512 | int dst_offset = i * kDoubleSize + double_regs_offset; |
| 513 | int src_offset = i * kDoubleSize + kNumberOfRegisters * kPointerSize; |
| 514 | __ movdbl(xmm0, Operand(esp, src_offset)); |
| 515 | __ movdbl(Operand(ebx, dst_offset), xmm0); |
| 516 | } |
| 517 | |
| 518 | // Remove the bailout id and the general purpose registers from the stack. |
| 519 | if (type() == EAGER) { |
| 520 | __ add(Operand(esp), Immediate(kSavedRegistersAreaSize + kPointerSize)); |
| 521 | } else { |
| 522 | __ add(Operand(esp), Immediate(kSavedRegistersAreaSize + 2 * kPointerSize)); |
| 523 | } |
| 524 | |
| 525 | // Compute a pointer to the unwinding limit in register ecx; that is |
| 526 | // the first stack slot not part of the input frame. |
| 527 | __ mov(ecx, Operand(ebx, FrameDescription::frame_size_offset())); |
| 528 | __ add(ecx, Operand(esp)); |
| 529 | |
| 530 | // Unwind the stack down to - but not including - the unwinding |
| 531 | // limit and copy the contents of the activation frame to the input |
| 532 | // frame description. |
| 533 | __ lea(edx, Operand(ebx, FrameDescription::frame_content_offset())); |
| 534 | Label pop_loop; |
| 535 | __ bind(&pop_loop); |
| 536 | __ pop(Operand(edx, 0)); |
| 537 | __ add(Operand(edx), Immediate(sizeof(uint32_t))); |
| 538 | __ cmp(ecx, Operand(esp)); |
| 539 | __ j(not_equal, &pop_loop); |
| 540 | |
| 541 | // Compute the output frame in the deoptimizer. |
| 542 | __ push(eax); |
| 543 | __ PrepareCallCFunction(1, ebx); |
| 544 | __ mov(Operand(esp, 0 * kPointerSize), eax); |
| 545 | __ CallCFunction(ExternalReference::compute_output_frames_function(), 1); |
| 546 | __ pop(eax); |
| 547 | |
| 548 | // Replace the current frame with the output frames. |
| 549 | Label outer_push_loop, inner_push_loop; |
| 550 | // Outer loop state: eax = current FrameDescription**, edx = one past the |
| 551 | // last FrameDescription**. |
| 552 | __ mov(edx, Operand(eax, Deoptimizer::output_count_offset())); |
| 553 | __ mov(eax, Operand(eax, Deoptimizer::output_offset())); |
| 554 | __ lea(edx, Operand(eax, edx, times_4, 0)); |
| 555 | __ bind(&outer_push_loop); |
| 556 | // Inner loop state: ebx = current FrameDescription*, ecx = loop index. |
| 557 | __ mov(ebx, Operand(eax, 0)); |
| 558 | __ mov(ecx, Operand(ebx, FrameDescription::frame_size_offset())); |
| 559 | __ bind(&inner_push_loop); |
| 560 | __ sub(Operand(ecx), Immediate(sizeof(uint32_t))); |
| 561 | __ push(Operand(ebx, ecx, times_1, FrameDescription::frame_content_offset())); |
| 562 | __ test(ecx, Operand(ecx)); |
| 563 | __ j(not_zero, &inner_push_loop); |
| 564 | __ add(Operand(eax), Immediate(kPointerSize)); |
| 565 | __ cmp(eax, Operand(edx)); |
| 566 | __ j(below, &outer_push_loop); |
| 567 | |
| 568 | // In case of OSR, we have to restore the XMM registers. |
| 569 | if (type() == OSR) { |
| 570 | for (int i = 0; i < XMMRegister::kNumAllocatableRegisters; ++i) { |
| 571 | XMMRegister xmm_reg = XMMRegister::FromAllocationIndex(i); |
| 572 | int src_offset = i * kDoubleSize + double_regs_offset; |
| 573 | __ movdbl(xmm_reg, Operand(ebx, src_offset)); |
| 574 | } |
| 575 | } |
| 576 | |
| 577 | // Push state, pc, and continuation from the last output frame. |
| 578 | if (type() != OSR) { |
| 579 | __ push(Operand(ebx, FrameDescription::state_offset())); |
| 580 | } |
| 581 | __ push(Operand(ebx, FrameDescription::pc_offset())); |
| 582 | __ push(Operand(ebx, FrameDescription::continuation_offset())); |
| 583 | |
| 584 | |
| 585 | // Push the registers from the last output frame. |
| 586 | for (int i = 0; i < kNumberOfRegisters; i++) { |
| 587 | int offset = (i * kIntSize) + FrameDescription::registers_offset(); |
| 588 | __ push(Operand(ebx, offset)); |
| 589 | } |
| 590 | |
| 591 | // Restore the registers from the stack. |
| 592 | __ popad(); |
| 593 | |
| 594 | // Return to the continuation point. |
| 595 | __ ret(0); |
| 596 | } |
| 597 | |
| 598 | |
| 599 | void Deoptimizer::TableEntryGenerator::GeneratePrologue() { |
| 600 | // Create a sequence of deoptimization entries. |
| 601 | Label done; |
| 602 | for (int i = 0; i < count(); i++) { |
| 603 | int start = masm()->pc_offset(); |
| 604 | USE(start); |
| 605 | __ push_imm32(i); |
| 606 | __ jmp(&done); |
| 607 | ASSERT(masm()->pc_offset() - start == table_entry_size_); |
| 608 | } |
| 609 | __ bind(&done); |
| 610 | } |
| 611 | |
| 612 | #undef __ |
| 613 | |
| 614 | |
| 615 | } } // namespace v8::internal |