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