OLD | NEW |
1 // Copyright 2011 the V8 project authors. All rights reserved. | 1 // Copyright 2011 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 14 matching lines...) Expand all Loading... |
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 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. | 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | 27 |
28 #include "v8.h" | 28 #include "v8.h" |
29 | 29 |
30 #include "codegen.h" | 30 #include "codegen.h" |
31 #include "deoptimizer.h" | 31 #include "deoptimizer.h" |
32 #include "full-codegen.h" | 32 #include "full-codegen.h" |
33 #include "safepoint-table.h" | 33 #include "safepoint-table.h" |
34 | 34 |
35 // Note: this file was taken from the X64 version. ARM has a partially working | |
36 // lithium implementation, but for now it is not ported to mips. | |
37 | |
38 namespace v8 { | 35 namespace v8 { |
39 namespace internal { | 36 namespace internal { |
40 | 37 |
41 | 38 |
42 const int Deoptimizer::table_entry_size_ = 10; | 39 const int Deoptimizer::table_entry_size_ = 32; |
43 | 40 |
44 | 41 |
45 int Deoptimizer::patch_size() { | 42 int Deoptimizer::patch_size() { |
46 const int kCallInstructionSizeInWords = 3; | 43 const int kCallInstructionSizeInWords = 4; |
47 return kCallInstructionSizeInWords * Assembler::kInstrSize; | 44 return kCallInstructionSizeInWords * Assembler::kInstrSize; |
48 } | 45 } |
49 | 46 |
50 | 47 |
51 void Deoptimizer::DeoptimizeFunction(JSFunction* function) { | 48 void Deoptimizer::EnsureRelocSpaceForLazyDeoptimization(Handle<Code> code) { |
52 UNIMPLEMENTED(); | 49 // Nothing to do. No new relocation information is written for lazy |
| 50 // deoptimization on MIPS. |
53 } | 51 } |
54 | 52 |
55 | 53 |
| 54 void Deoptimizer::DeoptimizeFunction(JSFunction* function) { |
| 55 HandleScope scope; |
| 56 AssertNoAllocation no_allocation; |
| 57 |
| 58 if (!function->IsOptimized()) return; |
| 59 |
| 60 // Get the optimized code. |
| 61 Code* code = function->code(); |
| 62 |
| 63 // Invalidate the relocation information, as it will become invalid by the |
| 64 // code patching below, and is not needed any more. |
| 65 code->InvalidateRelocation(); |
| 66 |
| 67 // For each return after a safepoint insert an absolute call to the |
| 68 // corresponding deoptimization entry. |
| 69 unsigned last_pc_offset = 0; |
| 70 SafepointTable table(function->code()); |
| 71 for (unsigned i = 0; i < table.length(); i++) { |
| 72 unsigned pc_offset = table.GetPcOffset(i); |
| 73 SafepointEntry safepoint_entry = table.GetEntry(i); |
| 74 int deoptimization_index = safepoint_entry.deoptimization_index(); |
| 75 int gap_code_size = safepoint_entry.gap_code_size(); |
| 76 // Check that we did not shoot past next safepoint. |
| 77 CHECK(pc_offset >= last_pc_offset); |
| 78 #ifdef DEBUG |
| 79 // Destroy the code which is not supposed to be run again. |
| 80 int instructions = (pc_offset - last_pc_offset) / Assembler::kInstrSize; |
| 81 CodePatcher destroyer(code->instruction_start() + last_pc_offset, |
| 82 instructions); |
| 83 for (int x = 0; x < instructions; x++) { |
| 84 destroyer.masm()->break_(0); |
| 85 } |
| 86 #endif |
| 87 last_pc_offset = pc_offset; |
| 88 if (deoptimization_index != Safepoint::kNoDeoptimizationIndex) { |
| 89 Address deoptimization_entry = Deoptimizer::GetDeoptimizationEntry( |
| 90 deoptimization_index, Deoptimizer::LAZY); |
| 91 last_pc_offset += gap_code_size; |
| 92 int call_size_in_bytes = MacroAssembler::CallSize(deoptimization_entry, |
| 93 RelocInfo::NONE); |
| 94 int call_size_in_words = call_size_in_bytes / Assembler::kInstrSize; |
| 95 ASSERT(call_size_in_bytes % Assembler::kInstrSize == 0); |
| 96 ASSERT(call_size_in_bytes <= patch_size()); |
| 97 CodePatcher patcher(code->instruction_start() + last_pc_offset, |
| 98 call_size_in_words); |
| 99 patcher.masm()->Call(deoptimization_entry, RelocInfo::NONE); |
| 100 last_pc_offset += call_size_in_bytes; |
| 101 } |
| 102 } |
| 103 |
| 104 #ifdef DEBUG |
| 105 // Destroy the code which is not supposed to be run again. |
| 106 int instructions = |
| 107 (code->safepoint_table_offset() - last_pc_offset) / Assembler::kInstrSize; |
| 108 CodePatcher destroyer(code->instruction_start() + last_pc_offset, |
| 109 instructions); |
| 110 for (int x = 0; x < instructions; x++) { |
| 111 destroyer.masm()->break_(0); |
| 112 } |
| 113 #endif |
| 114 |
| 115 Isolate* isolate = code->GetIsolate(); |
| 116 |
| 117 // Add the deoptimizing code to the list. |
| 118 DeoptimizingCodeListNode* node = new DeoptimizingCodeListNode(code); |
| 119 DeoptimizerData* data = isolate->deoptimizer_data(); |
| 120 node->set_next(data->deoptimizing_code_list_); |
| 121 data->deoptimizing_code_list_ = node; |
| 122 |
| 123 // We might be in the middle of incremental marking with compaction. |
| 124 // Tell collector to treat this code object in a special way and |
| 125 // ignore all slots that might have been recorded on it. |
| 126 isolate->heap()->mark_compact_collector()->InvalidateCode(code); |
| 127 |
| 128 // Set the code for the function to non-optimized version. |
| 129 function->ReplaceCode(function->shared()->code()); |
| 130 |
| 131 if (FLAG_trace_deopt) { |
| 132 PrintF("[forced deoptimization: "); |
| 133 function->PrintName(); |
| 134 PrintF(" / %x]\n", reinterpret_cast<uint32_t>(function)); |
| 135 #ifdef DEBUG |
| 136 if (FLAG_print_code) { |
| 137 code->PrintLn(); |
| 138 } |
| 139 #endif |
| 140 } |
| 141 } |
| 142 |
| 143 |
56 void Deoptimizer::PatchStackCheckCodeAt(Code* unoptimized_code, | 144 void Deoptimizer::PatchStackCheckCodeAt(Code* unoptimized_code, |
57 Address pc_after, | 145 Address pc_after, |
58 Code* check_code, | 146 Code* check_code, |
59 Code* replacement_code) { | 147 Code* replacement_code) { |
60 UNIMPLEMENTED(); | 148 const int kInstrSize = Assembler::kInstrSize; |
| 149 // This structure comes from FullCodeGenerator::EmitStackCheck. |
| 150 // The call of the stack guard check has the following form: |
| 151 // sltu at, sp, t0 |
| 152 // beq at, zero_reg, ok |
| 153 // lui t9, <stack guard address> upper |
| 154 // ori t9, <stack guard address> lower |
| 155 // jalr t9 |
| 156 // nop |
| 157 // ----- pc_after points here |
| 158 |
| 159 ASSERT(Assembler::IsBeq(Assembler::instr_at(pc_after - 5 * kInstrSize))); |
| 160 |
| 161 // Replace the sltu instruction with load-imm 1 to at, so beq is not taken. |
| 162 CodePatcher patcher(pc_after - 6 * kInstrSize, 1); |
| 163 patcher.masm()->addiu(at, zero_reg, 1); |
| 164 |
| 165 // Replace the stack check address in the load-immediate (lui/ori pair) |
| 166 // with the entry address of the replacement code. |
| 167 ASSERT(reinterpret_cast<uint32_t>( |
| 168 Assembler::target_address_at(pc_after - 4 * kInstrSize)) == |
| 169 reinterpret_cast<uint32_t>(check_code->entry())); |
| 170 Assembler::set_target_address_at(pc_after - 4 * kInstrSize, |
| 171 replacement_code->entry()); |
| 172 |
| 173 // We patched the code to the following form: |
| 174 // addiu at, zero_reg, 1 |
| 175 // beq at, zero_reg, ok ;; Not changed |
| 176 // lui t9, <on-stack replacement address> upper |
| 177 // ori t9, <on-stack replacement address> lower |
| 178 // jalr t9 ;; Not changed |
| 179 // nop ;; Not changed |
| 180 // ----- pc_after points here |
| 181 |
| 182 unoptimized_code->GetHeap()->incremental_marking()->RecordCodeTargetPatch( |
| 183 unoptimized_code, pc_after - 4 * kInstrSize, replacement_code); |
61 } | 184 } |
62 | 185 |
63 | 186 |
64 void Deoptimizer::RevertStackCheckCodeAt(Code* unoptimized_code, | 187 void Deoptimizer::RevertStackCheckCodeAt(Code* unoptimized_code, |
65 Address pc_after, | 188 Address pc_after, |
66 Code* check_code, | 189 Code* check_code, |
67 Code* replacement_code) { | 190 Code* replacement_code) { |
68 UNIMPLEMENTED(); | 191 // Exact opposite of the function above. |
| 192 const int kInstrSize = Assembler::kInstrSize; |
| 193 ASSERT(Assembler::IsAddImmediate( |
| 194 Assembler::instr_at(pc_after - 6 * kInstrSize))); |
| 195 ASSERT(Assembler::IsBeq(Assembler::instr_at(pc_after - 5 * kInstrSize))); |
| 196 |
| 197 // Restore the sltu instruction so beq can be taken again. |
| 198 CodePatcher patcher(pc_after - 6 * kInstrSize, 1); |
| 199 patcher.masm()->sltu(at, sp, t0); |
| 200 |
| 201 // Replace the on-stack replacement address in the load-immediate (lui/ori |
| 202 // pair) with the entry address of the normal stack-check code. |
| 203 ASSERT(reinterpret_cast<uint32_t>( |
| 204 Assembler::target_address_at(pc_after - 4 * kInstrSize)) == |
| 205 reinterpret_cast<uint32_t>(replacement_code->entry())); |
| 206 Assembler::set_target_address_at(pc_after - 4 * kInstrSize, |
| 207 check_code->entry()); |
| 208 |
| 209 check_code->GetHeap()->incremental_marking()->RecordCodeTargetPatch( |
| 210 unoptimized_code, pc_after - 4 * kInstrSize, check_code); |
69 } | 211 } |
70 | 212 |
71 | 213 |
| 214 static int LookupBailoutId(DeoptimizationInputData* data, unsigned ast_id) { |
| 215 ByteArray* translations = data->TranslationByteArray(); |
| 216 int length = data->DeoptCount(); |
| 217 for (int i = 0; i < length; i++) { |
| 218 if (static_cast<unsigned>(data->AstId(i)->value()) == ast_id) { |
| 219 TranslationIterator it(translations, data->TranslationIndex(i)->value()); |
| 220 int value = it.Next(); |
| 221 ASSERT(Translation::BEGIN == static_cast<Translation::Opcode>(value)); |
| 222 // Read the number of frames. |
| 223 value = it.Next(); |
| 224 if (value == 1) return i; |
| 225 } |
| 226 } |
| 227 UNREACHABLE(); |
| 228 return -1; |
| 229 } |
| 230 |
| 231 |
72 void Deoptimizer::DoComputeOsrOutputFrame() { | 232 void Deoptimizer::DoComputeOsrOutputFrame() { |
73 UNIMPLEMENTED(); | 233 DeoptimizationInputData* data = DeoptimizationInputData::cast( |
| 234 optimized_code_->deoptimization_data()); |
| 235 unsigned ast_id = data->OsrAstId()->value(); |
| 236 |
| 237 int bailout_id = LookupBailoutId(data, ast_id); |
| 238 unsigned translation_index = data->TranslationIndex(bailout_id)->value(); |
| 239 ByteArray* translations = data->TranslationByteArray(); |
| 240 |
| 241 TranslationIterator iterator(translations, translation_index); |
| 242 Translation::Opcode opcode = |
| 243 static_cast<Translation::Opcode>(iterator.Next()); |
| 244 ASSERT(Translation::BEGIN == opcode); |
| 245 USE(opcode); |
| 246 int count = iterator.Next(); |
| 247 ASSERT(count == 1); |
| 248 USE(count); |
| 249 |
| 250 opcode = static_cast<Translation::Opcode>(iterator.Next()); |
| 251 USE(opcode); |
| 252 ASSERT(Translation::FRAME == opcode); |
| 253 unsigned node_id = iterator.Next(); |
| 254 USE(node_id); |
| 255 ASSERT(node_id == ast_id); |
| 256 JSFunction* function = JSFunction::cast(ComputeLiteral(iterator.Next())); |
| 257 USE(function); |
| 258 ASSERT(function == function_); |
| 259 unsigned height = iterator.Next(); |
| 260 unsigned height_in_bytes = height * kPointerSize; |
| 261 USE(height_in_bytes); |
| 262 |
| 263 unsigned fixed_size = ComputeFixedSize(function_); |
| 264 unsigned input_frame_size = input_->GetFrameSize(); |
| 265 ASSERT(fixed_size + height_in_bytes == input_frame_size); |
| 266 |
| 267 unsigned stack_slot_size = optimized_code_->stack_slots() * kPointerSize; |
| 268 unsigned outgoing_height = data->ArgumentsStackHeight(bailout_id)->value(); |
| 269 unsigned outgoing_size = outgoing_height * kPointerSize; |
| 270 unsigned output_frame_size = fixed_size + stack_slot_size + outgoing_size; |
| 271 ASSERT(outgoing_size == 0); // OSR does not happen in the middle of a call. |
| 272 |
| 273 if (FLAG_trace_osr) { |
| 274 PrintF("[on-stack replacement: begin 0x%08" V8PRIxPTR " ", |
| 275 reinterpret_cast<intptr_t>(function_)); |
| 276 function_->PrintName(); |
| 277 PrintF(" => node=%u, frame=%d->%d]\n", |
| 278 ast_id, |
| 279 input_frame_size, |
| 280 output_frame_size); |
| 281 } |
| 282 |
| 283 // There's only one output frame in the OSR case. |
| 284 output_count_ = 1; |
| 285 output_ = new FrameDescription*[1]; |
| 286 output_[0] = new(output_frame_size) FrameDescription( |
| 287 output_frame_size, function_); |
| 288 #ifdef DEBUG |
| 289 output_[0]->SetKind(Code::OPTIMIZED_FUNCTION); |
| 290 #endif |
| 291 |
| 292 // Clear the incoming parameters in the optimized frame to avoid |
| 293 // confusing the garbage collector. |
| 294 unsigned output_offset = output_frame_size - kPointerSize; |
| 295 int parameter_count = function_->shared()->formal_parameter_count() + 1; |
| 296 for (int i = 0; i < parameter_count; ++i) { |
| 297 output_[0]->SetFrameSlot(output_offset, 0); |
| 298 output_offset -= kPointerSize; |
| 299 } |
| 300 |
| 301 // Translate the incoming parameters. This may overwrite some of the |
| 302 // incoming argument slots we've just cleared. |
| 303 int input_offset = input_frame_size - kPointerSize; |
| 304 bool ok = true; |
| 305 int limit = input_offset - (parameter_count * kPointerSize); |
| 306 while (ok && input_offset > limit) { |
| 307 ok = DoOsrTranslateCommand(&iterator, &input_offset); |
| 308 } |
| 309 |
| 310 // There are no translation commands for the caller's pc and fp, the |
| 311 // context, and the function. Set them up explicitly. |
| 312 for (int i = StandardFrameConstants::kCallerPCOffset; |
| 313 ok && i >= StandardFrameConstants::kMarkerOffset; |
| 314 i -= kPointerSize) { |
| 315 uint32_t input_value = input_->GetFrameSlot(input_offset); |
| 316 if (FLAG_trace_osr) { |
| 317 const char* name = "UNKNOWN"; |
| 318 switch (i) { |
| 319 case StandardFrameConstants::kCallerPCOffset: |
| 320 name = "caller's pc"; |
| 321 break; |
| 322 case StandardFrameConstants::kCallerFPOffset: |
| 323 name = "fp"; |
| 324 break; |
| 325 case StandardFrameConstants::kContextOffset: |
| 326 name = "context"; |
| 327 break; |
| 328 case StandardFrameConstants::kMarkerOffset: |
| 329 name = "function"; |
| 330 break; |
| 331 } |
| 332 PrintF(" [sp + %d] <- 0x%08x ; [sp + %d] (fixed part - %s)\n", |
| 333 output_offset, |
| 334 input_value, |
| 335 input_offset, |
| 336 name); |
| 337 } |
| 338 |
| 339 output_[0]->SetFrameSlot(output_offset, input_->GetFrameSlot(input_offset)); |
| 340 input_offset -= kPointerSize; |
| 341 output_offset -= kPointerSize; |
| 342 } |
| 343 |
| 344 // Translate the rest of the frame. |
| 345 while (ok && input_offset >= 0) { |
| 346 ok = DoOsrTranslateCommand(&iterator, &input_offset); |
| 347 } |
| 348 |
| 349 // If translation of any command failed, continue using the input frame. |
| 350 if (!ok) { |
| 351 delete output_[0]; |
| 352 output_[0] = input_; |
| 353 output_[0]->SetPc(reinterpret_cast<uint32_t>(from_)); |
| 354 } else { |
| 355 // Setup the frame pointer and the context pointer. |
| 356 output_[0]->SetRegister(fp.code(), input_->GetRegister(fp.code())); |
| 357 output_[0]->SetRegister(cp.code(), input_->GetRegister(cp.code())); |
| 358 |
| 359 unsigned pc_offset = data->OsrPcOffset()->value(); |
| 360 uint32_t pc = reinterpret_cast<uint32_t>( |
| 361 optimized_code_->entry() + pc_offset); |
| 362 output_[0]->SetPc(pc); |
| 363 } |
| 364 Code* continuation = isolate_->builtins()->builtin(Builtins::kNotifyOSR); |
| 365 output_[0]->SetContinuation( |
| 366 reinterpret_cast<uint32_t>(continuation->entry())); |
| 367 |
| 368 if (FLAG_trace_osr) { |
| 369 PrintF("[on-stack replacement translation %s: 0x%08" V8PRIxPTR " ", |
| 370 ok ? "finished" : "aborted", |
| 371 reinterpret_cast<intptr_t>(function)); |
| 372 function->PrintName(); |
| 373 PrintF(" => pc=0x%0x]\n", output_[0]->GetPc()); |
| 374 } |
74 } | 375 } |
75 | 376 |
76 | 377 |
| 378 // This code is very similar to ia32/arm code, but relies on register names |
| 379 // (fp, sp) and how the frame is laid out. |
77 void Deoptimizer::DoComputeFrame(TranslationIterator* iterator, | 380 void Deoptimizer::DoComputeFrame(TranslationIterator* iterator, |
78 int frame_index) { | 381 int frame_index) { |
79 UNIMPLEMENTED(); | 382 // Read the ast node id, function, and frame height for this output frame. |
| 383 Translation::Opcode opcode = |
| 384 static_cast<Translation::Opcode>(iterator->Next()); |
| 385 USE(opcode); |
| 386 ASSERT(Translation::FRAME == opcode); |
| 387 int node_id = iterator->Next(); |
| 388 JSFunction* function = JSFunction::cast(ComputeLiteral(iterator->Next())); |
| 389 unsigned height = iterator->Next(); |
| 390 unsigned height_in_bytes = height * kPointerSize; |
| 391 if (FLAG_trace_deopt) { |
| 392 PrintF(" translating "); |
| 393 function->PrintName(); |
| 394 PrintF(" => node=%d, height=%d\n", node_id, height_in_bytes); |
| 395 } |
| 396 |
| 397 // The 'fixed' part of the frame consists of the incoming parameters and |
| 398 // the part described by JavaScriptFrameConstants. |
| 399 unsigned fixed_frame_size = ComputeFixedSize(function); |
| 400 unsigned input_frame_size = input_->GetFrameSize(); |
| 401 unsigned output_frame_size = height_in_bytes + fixed_frame_size; |
| 402 |
| 403 // Allocate and store the output frame description. |
| 404 FrameDescription* output_frame = |
| 405 new(output_frame_size) FrameDescription(output_frame_size, function); |
| 406 #ifdef DEBUG |
| 407 output_frame->SetKind(Code::FUNCTION); |
| 408 #endif |
| 409 |
| 410 bool is_bottommost = (0 == frame_index); |
| 411 bool is_topmost = (output_count_ - 1 == frame_index); |
| 412 ASSERT(frame_index >= 0 && frame_index < output_count_); |
| 413 ASSERT(output_[frame_index] == NULL); |
| 414 output_[frame_index] = output_frame; |
| 415 |
| 416 // The top address for the bottommost output frame can be computed from |
| 417 // the input frame pointer and the output frame's height. For all |
| 418 // subsequent output frames, it can be computed from the previous one's |
| 419 // top address and the current frame's size. |
| 420 uint32_t top_address; |
| 421 if (is_bottommost) { |
| 422 // 2 = context and function in the frame. |
| 423 top_address = |
| 424 input_->GetRegister(fp.code()) - (2 * kPointerSize) - height_in_bytes; |
| 425 } else { |
| 426 top_address = output_[frame_index - 1]->GetTop() - output_frame_size; |
| 427 } |
| 428 output_frame->SetTop(top_address); |
| 429 |
| 430 // Compute the incoming parameter translation. |
| 431 int parameter_count = function->shared()->formal_parameter_count() + 1; |
| 432 unsigned output_offset = output_frame_size; |
| 433 unsigned input_offset = input_frame_size; |
| 434 for (int i = 0; i < parameter_count; ++i) { |
| 435 output_offset -= kPointerSize; |
| 436 DoTranslateCommand(iterator, frame_index, output_offset); |
| 437 } |
| 438 input_offset -= (parameter_count * kPointerSize); |
| 439 |
| 440 // There are no translation commands for the caller's pc and fp, the |
| 441 // context, and the function. Synthesize their values and set them up |
| 442 // explicitly. |
| 443 // |
| 444 // The caller's pc for the bottommost output frame is the same as in the |
| 445 // input frame. For all subsequent output frames, it can be read from the |
| 446 // previous one. This frame's pc can be computed from the non-optimized |
| 447 // function code and AST id of the bailout. |
| 448 output_offset -= kPointerSize; |
| 449 input_offset -= kPointerSize; |
| 450 intptr_t value; |
| 451 if (is_bottommost) { |
| 452 value = input_->GetFrameSlot(input_offset); |
| 453 } else { |
| 454 value = output_[frame_index - 1]->GetPc(); |
| 455 } |
| 456 output_frame->SetFrameSlot(output_offset, value); |
| 457 if (FLAG_trace_deopt) { |
| 458 PrintF(" 0x%08x: [top + %d] <- 0x%08x ; caller's pc\n", |
| 459 top_address + output_offset, output_offset, value); |
| 460 } |
| 461 |
| 462 // The caller's frame pointer for the bottommost output frame is the same |
| 463 // as in the input frame. For all subsequent output frames, it can be |
| 464 // read from the previous one. Also compute and set this frame's frame |
| 465 // pointer. |
| 466 output_offset -= kPointerSize; |
| 467 input_offset -= kPointerSize; |
| 468 if (is_bottommost) { |
| 469 value = input_->GetFrameSlot(input_offset); |
| 470 } else { |
| 471 value = output_[frame_index - 1]->GetFp(); |
| 472 } |
| 473 output_frame->SetFrameSlot(output_offset, value); |
| 474 intptr_t fp_value = top_address + output_offset; |
| 475 ASSERT(!is_bottommost || input_->GetRegister(fp.code()) == fp_value); |
| 476 output_frame->SetFp(fp_value); |
| 477 if (is_topmost) { |
| 478 output_frame->SetRegister(fp.code(), fp_value); |
| 479 } |
| 480 if (FLAG_trace_deopt) { |
| 481 PrintF(" 0x%08x: [top + %d] <- 0x%08x ; caller's fp\n", |
| 482 fp_value, output_offset, value); |
| 483 } |
| 484 |
| 485 // For the bottommost output frame the context can be gotten from the input |
| 486 // frame. For all subsequent output frames it can be gotten from the function |
| 487 // so long as we don't inline functions that need local contexts. |
| 488 output_offset -= kPointerSize; |
| 489 input_offset -= kPointerSize; |
| 490 if (is_bottommost) { |
| 491 value = input_->GetFrameSlot(input_offset); |
| 492 } else { |
| 493 value = reinterpret_cast<intptr_t>(function->context()); |
| 494 } |
| 495 output_frame->SetFrameSlot(output_offset, value); |
| 496 if (is_topmost) { |
| 497 output_frame->SetRegister(cp.code(), value); |
| 498 } |
| 499 if (FLAG_trace_deopt) { |
| 500 PrintF(" 0x%08x: [top + %d] <- 0x%08x ; context\n", |
| 501 top_address + output_offset, output_offset, value); |
| 502 } |
| 503 |
| 504 // The function was mentioned explicitly in the BEGIN_FRAME. |
| 505 output_offset -= kPointerSize; |
| 506 input_offset -= kPointerSize; |
| 507 value = reinterpret_cast<uint32_t>(function); |
| 508 // The function for the bottommost output frame should also agree with the |
| 509 // input frame. |
| 510 ASSERT(!is_bottommost || input_->GetFrameSlot(input_offset) == value); |
| 511 output_frame->SetFrameSlot(output_offset, value); |
| 512 if (FLAG_trace_deopt) { |
| 513 PrintF(" 0x%08x: [top + %d] <- 0x%08x ; function\n", |
| 514 top_address + output_offset, output_offset, value); |
| 515 } |
| 516 |
| 517 // Translate the rest of the frame. |
| 518 for (unsigned i = 0; i < height; ++i) { |
| 519 output_offset -= kPointerSize; |
| 520 DoTranslateCommand(iterator, frame_index, output_offset); |
| 521 } |
| 522 ASSERT(0 == output_offset); |
| 523 |
| 524 // Compute this frame's PC, state, and continuation. |
| 525 Code* non_optimized_code = function->shared()->code(); |
| 526 FixedArray* raw_data = non_optimized_code->deoptimization_data(); |
| 527 DeoptimizationOutputData* data = DeoptimizationOutputData::cast(raw_data); |
| 528 Address start = non_optimized_code->instruction_start(); |
| 529 unsigned pc_and_state = GetOutputInfo(data, node_id, function->shared()); |
| 530 unsigned pc_offset = FullCodeGenerator::PcField::decode(pc_and_state); |
| 531 uint32_t pc_value = reinterpret_cast<uint32_t>(start + pc_offset); |
| 532 output_frame->SetPc(pc_value); |
| 533 |
| 534 FullCodeGenerator::State state = |
| 535 FullCodeGenerator::StateField::decode(pc_and_state); |
| 536 output_frame->SetState(Smi::FromInt(state)); |
| 537 |
| 538 |
| 539 // Set the continuation for the topmost frame. |
| 540 if (is_topmost && bailout_type_ != DEBUGGER) { |
| 541 Builtins* builtins = isolate_->builtins(); |
| 542 Code* continuation = (bailout_type_ == EAGER) |
| 543 ? builtins->builtin(Builtins::kNotifyDeoptimized) |
| 544 : builtins->builtin(Builtins::kNotifyLazyDeoptimized); |
| 545 output_frame->SetContinuation( |
| 546 reinterpret_cast<uint32_t>(continuation->entry())); |
| 547 } |
80 } | 548 } |
81 | 549 |
82 | |
83 void Deoptimizer::FillInputFrame(Address tos, JavaScriptFrame* frame) { | 550 void Deoptimizer::FillInputFrame(Address tos, JavaScriptFrame* frame) { |
84 UNIMPLEMENTED(); | 551 // Set the register values. The values are not important as there are no |
| 552 // callee saved registers in JavaScript frames, so all registers are |
| 553 // spilled. Registers fp and sp are set to the correct values though. |
| 554 |
| 555 for (int i = 0; i < Register::kNumRegisters; i++) { |
| 556 input_->SetRegister(i, i * 4); |
| 557 } |
| 558 input_->SetRegister(sp.code(), reinterpret_cast<intptr_t>(frame->sp())); |
| 559 input_->SetRegister(fp.code(), reinterpret_cast<intptr_t>(frame->fp())); |
| 560 for (int i = 0; i < DoubleRegister::kNumAllocatableRegisters; i++) { |
| 561 input_->SetDoubleRegister(i, 0.0); |
| 562 } |
| 563 |
| 564 // Fill the frame content from the actual data on the frame. |
| 565 for (unsigned i = 0; i < input_->GetFrameSize(); i += kPointerSize) { |
| 566 input_->SetFrameSlot(i, Memory::uint32_at(tos + i)); |
| 567 } |
85 } | 568 } |
86 | 569 |
87 | 570 |
| 571 #define __ masm()-> |
| 572 |
| 573 |
| 574 // This code tries to be close to ia32 code so that any changes can be |
| 575 // easily ported. |
88 void Deoptimizer::EntryGenerator::Generate() { | 576 void Deoptimizer::EntryGenerator::Generate() { |
89 UNIMPLEMENTED(); | 577 GeneratePrologue(); |
| 578 |
| 579 Isolate* isolate = masm()->isolate(); |
| 580 |
| 581 CpuFeatures::Scope scope(FPU); |
| 582 // Unlike on ARM we don't save all the registers, just the useful ones. |
| 583 // For the rest, there are gaps on the stack, so the offsets remain the same. |
| 584 const int kNumberOfRegisters = Register::kNumRegisters; |
| 585 |
| 586 RegList restored_regs = kJSCallerSaved | kCalleeSaved; |
| 587 RegList saved_regs = restored_regs | sp.bit() | ra.bit(); |
| 588 |
| 589 const int kDoubleRegsSize = |
| 590 kDoubleSize * FPURegister::kNumAllocatableRegisters; |
| 591 |
| 592 // Save all FPU registers before messing with them. |
| 593 __ Subu(sp, sp, Operand(kDoubleRegsSize)); |
| 594 for (int i = 0; i < FPURegister::kNumAllocatableRegisters; ++i) { |
| 595 FPURegister fpu_reg = FPURegister::FromAllocationIndex(i); |
| 596 int offset = i * kDoubleSize; |
| 597 __ sdc1(fpu_reg, MemOperand(sp, offset)); |
| 598 } |
| 599 |
| 600 // Push saved_regs (needed to populate FrameDescription::registers_). |
| 601 // Leave gaps for other registers. |
| 602 __ Subu(sp, sp, kNumberOfRegisters * kPointerSize); |
| 603 for (int16_t i = kNumberOfRegisters - 1; i >= 0; i--) { |
| 604 if ((saved_regs & (1 << i)) != 0) { |
| 605 __ sw(ToRegister(i), MemOperand(sp, kPointerSize * i)); |
| 606 } |
| 607 } |
| 608 |
| 609 const int kSavedRegistersAreaSize = |
| 610 (kNumberOfRegisters * kPointerSize) + kDoubleRegsSize; |
| 611 |
| 612 // Get the bailout id from the stack. |
| 613 __ lw(a2, MemOperand(sp, kSavedRegistersAreaSize)); |
| 614 |
| 615 // Get the address of the location in the code object if possible (a3) (return |
| 616 // address for lazy deoptimization) and compute the fp-to-sp delta in |
| 617 // register t0. |
| 618 if (type() == EAGER) { |
| 619 __ mov(a3, zero_reg); |
| 620 // Correct one word for bailout id. |
| 621 __ Addu(t0, sp, Operand(kSavedRegistersAreaSize + (1 * kPointerSize))); |
| 622 } else if (type() == OSR) { |
| 623 __ mov(a3, ra); |
| 624 // Correct one word for bailout id. |
| 625 __ Addu(t0, sp, Operand(kSavedRegistersAreaSize + (1 * kPointerSize))); |
| 626 } else { |
| 627 __ mov(a3, ra); |
| 628 // Correct two words for bailout id and return address. |
| 629 __ Addu(t0, sp, Operand(kSavedRegistersAreaSize + (2 * kPointerSize))); |
| 630 } |
| 631 |
| 632 __ Subu(t0, fp, t0); |
| 633 |
| 634 // Allocate a new deoptimizer object. |
| 635 // Pass four arguments in a0 to a3 and fifth & sixth arguments on stack. |
| 636 __ PrepareCallCFunction(6, t1); |
| 637 __ lw(a0, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset)); |
| 638 __ li(a1, Operand(type())); // bailout type, |
| 639 // a2: bailout id already loaded. |
| 640 // a3: code address or 0 already loaded. |
| 641 __ sw(t0, CFunctionArgumentOperand(5)); // Fp-to-sp delta. |
| 642 __ li(t1, Operand(ExternalReference::isolate_address())); |
| 643 __ sw(t1, CFunctionArgumentOperand(6)); // Isolate. |
| 644 // Call Deoptimizer::New(). |
| 645 { |
| 646 AllowExternalCallThatCantCauseGC scope(masm()); |
| 647 __ CallCFunction(ExternalReference::new_deoptimizer_function(isolate), 6); |
| 648 } |
| 649 |
| 650 // Preserve "deoptimizer" object in register v0 and get the input |
| 651 // frame descriptor pointer to a1 (deoptimizer->input_); |
| 652 // Move deopt-obj to a0 for call to Deoptimizer::ComputeOutputFrames() below. |
| 653 __ mov(a0, v0); |
| 654 __ lw(a1, MemOperand(v0, Deoptimizer::input_offset())); |
| 655 |
| 656 // Copy core registers into FrameDescription::registers_[kNumRegisters]. |
| 657 ASSERT(Register::kNumRegisters == kNumberOfRegisters); |
| 658 for (int i = 0; i < kNumberOfRegisters; i++) { |
| 659 int offset = (i * kPointerSize) + FrameDescription::registers_offset(); |
| 660 if ((saved_regs & (1 << i)) != 0) { |
| 661 __ lw(a2, MemOperand(sp, i * kPointerSize)); |
| 662 __ sw(a2, MemOperand(a1, offset)); |
| 663 } else if (FLAG_debug_code) { |
| 664 __ li(a2, kDebugZapValue); |
| 665 __ sw(a2, MemOperand(a1, offset)); |
| 666 } |
| 667 } |
| 668 |
| 669 // Copy FPU registers to |
| 670 // double_registers_[DoubleRegister::kNumAllocatableRegisters] |
| 671 int double_regs_offset = FrameDescription::double_registers_offset(); |
| 672 for (int i = 0; i < FPURegister::kNumAllocatableRegisters; ++i) { |
| 673 int dst_offset = i * kDoubleSize + double_regs_offset; |
| 674 int src_offset = i * kDoubleSize + kNumberOfRegisters * kPointerSize; |
| 675 __ ldc1(f0, MemOperand(sp, src_offset)); |
| 676 __ sdc1(f0, MemOperand(a1, dst_offset)); |
| 677 } |
| 678 |
| 679 // Remove the bailout id, eventually return address, and the saved registers |
| 680 // from the stack. |
| 681 if (type() == EAGER || type() == OSR) { |
| 682 __ Addu(sp, sp, Operand(kSavedRegistersAreaSize + (1 * kPointerSize))); |
| 683 } else { |
| 684 __ Addu(sp, sp, Operand(kSavedRegistersAreaSize + (2 * kPointerSize))); |
| 685 } |
| 686 |
| 687 // Compute a pointer to the unwinding limit in register a2; that is |
| 688 // the first stack slot not part of the input frame. |
| 689 __ lw(a2, MemOperand(a1, FrameDescription::frame_size_offset())); |
| 690 __ Addu(a2, a2, sp); |
| 691 |
| 692 // Unwind the stack down to - but not including - the unwinding |
| 693 // limit and copy the contents of the activation frame to the input |
| 694 // frame description. |
| 695 __ Addu(a3, a1, Operand(FrameDescription::frame_content_offset())); |
| 696 Label pop_loop; |
| 697 __ bind(&pop_loop); |
| 698 __ pop(t0); |
| 699 __ sw(t0, MemOperand(a3, 0)); |
| 700 __ Branch(USE_DELAY_SLOT, &pop_loop, ne, a2, Operand(sp)); |
| 701 __ addiu(a3, a3, sizeof(uint32_t)); // In delay slot. |
| 702 |
| 703 // Compute the output frame in the deoptimizer. |
| 704 __ push(a0); // Preserve deoptimizer object across call. |
| 705 // a0: deoptimizer object; a1: scratch. |
| 706 __ PrepareCallCFunction(1, a1); |
| 707 // Call Deoptimizer::ComputeOutputFrames(). |
| 708 { |
| 709 AllowExternalCallThatCantCauseGC scope(masm()); |
| 710 __ CallCFunction( |
| 711 ExternalReference::compute_output_frames_function(isolate), 1); |
| 712 } |
| 713 __ pop(a0); // Restore deoptimizer object (class Deoptimizer). |
| 714 |
| 715 // Replace the current (input) frame with the output frames. |
| 716 Label outer_push_loop, inner_push_loop; |
| 717 // Outer loop state: a0 = current "FrameDescription** output_", |
| 718 // a1 = one past the last FrameDescription**. |
| 719 __ lw(a1, MemOperand(a0, Deoptimizer::output_count_offset())); |
| 720 __ lw(a0, MemOperand(a0, Deoptimizer::output_offset())); // a0 is output_. |
| 721 __ sll(a1, a1, kPointerSizeLog2); // Count to offset. |
| 722 __ addu(a1, a0, a1); // a1 = one past the last FrameDescription**. |
| 723 __ bind(&outer_push_loop); |
| 724 // Inner loop state: a2 = current FrameDescription*, a3 = loop index. |
| 725 __ lw(a2, MemOperand(a0, 0)); // output_[ix] |
| 726 __ lw(a3, MemOperand(a2, FrameDescription::frame_size_offset())); |
| 727 __ bind(&inner_push_loop); |
| 728 __ Subu(a3, a3, Operand(sizeof(uint32_t))); |
| 729 __ Addu(t2, a2, Operand(a3)); |
| 730 __ lw(t3, MemOperand(t2, FrameDescription::frame_content_offset())); |
| 731 __ push(t3); |
| 732 __ Branch(&inner_push_loop, ne, a3, Operand(zero_reg)); |
| 733 |
| 734 __ Addu(a0, a0, Operand(kPointerSize)); |
| 735 __ Branch(&outer_push_loop, lt, a0, Operand(a1)); |
| 736 |
| 737 |
| 738 // Push state, pc, and continuation from the last output frame. |
| 739 if (type() != OSR) { |
| 740 __ lw(t2, MemOperand(a2, FrameDescription::state_offset())); |
| 741 __ push(t2); |
| 742 } |
| 743 |
| 744 __ lw(t2, MemOperand(a2, FrameDescription::pc_offset())); |
| 745 __ push(t2); |
| 746 __ lw(t2, MemOperand(a2, FrameDescription::continuation_offset())); |
| 747 __ push(t2); |
| 748 |
| 749 |
| 750 // Technically restoring 'at' should work unless zero_reg is also restored |
| 751 // but it's safer to check for this. |
| 752 ASSERT(!(at.bit() & restored_regs)); |
| 753 // Restore the registers from the last output frame. |
| 754 __ mov(at, a2); |
| 755 for (int i = kNumberOfRegisters - 1; i >= 0; i--) { |
| 756 int offset = (i * kPointerSize) + FrameDescription::registers_offset(); |
| 757 if ((restored_regs & (1 << i)) != 0) { |
| 758 __ lw(ToRegister(i), MemOperand(at, offset)); |
| 759 } |
| 760 } |
| 761 |
| 762 // Set up the roots register. |
| 763 ExternalReference roots_array_start = |
| 764 ExternalReference::roots_array_start(isolate); |
| 765 __ li(roots, Operand(roots_array_start)); |
| 766 |
| 767 __ pop(at); // Get continuation, leave pc on stack. |
| 768 __ pop(ra); |
| 769 __ Jump(at); |
| 770 __ stop("Unreachable."); |
90 } | 771 } |
91 | 772 |
92 | 773 |
93 void Deoptimizer::TableEntryGenerator::GeneratePrologue() { | 774 void Deoptimizer::TableEntryGenerator::GeneratePrologue() { |
94 UNIMPLEMENTED(); | 775 Assembler::BlockTrampolinePoolScope block_trampoline_pool(masm()); |
| 776 |
| 777 // Create a sequence of deoptimization entries. Note that any |
| 778 // registers may be still live. |
| 779 |
| 780 Label done; |
| 781 for (int i = 0; i < count(); i++) { |
| 782 int start = masm()->pc_offset(); |
| 783 USE(start); |
| 784 if (type() != EAGER) { |
| 785 // Emulate ia32 like call by pushing return address to stack. |
| 786 __ push(ra); |
| 787 } |
| 788 __ li(at, Operand(i)); |
| 789 __ push(at); |
| 790 __ Branch(&done); |
| 791 |
| 792 // Pad the rest of the code. |
| 793 while (table_entry_size_ > (masm()->pc_offset() - start)) { |
| 794 __ nop(); |
| 795 } |
| 796 |
| 797 ASSERT_EQ(table_entry_size_, masm()->pc_offset() - start); |
| 798 } |
| 799 __ bind(&done); |
95 } | 800 } |
96 | 801 |
| 802 #undef __ |
| 803 |
97 | 804 |
98 } } // namespace v8::internal | 805 } } // namespace v8::internal |
OLD | NEW |