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