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 24 matching lines...) Expand all Loading... |
35 #include "safepoint-table.h" | 35 #include "safepoint-table.h" |
36 | 36 |
37 namespace v8 { | 37 namespace v8 { |
38 namespace internal { | 38 namespace internal { |
39 | 39 |
40 | 40 |
41 int Deoptimizer::table_entry_size_ = 10; | 41 int Deoptimizer::table_entry_size_ = 10; |
42 | 42 |
43 | 43 |
44 int Deoptimizer::patch_size() { | 44 int Deoptimizer::patch_size() { |
45 return Assembler::kCallInstructionLength; | 45 return MacroAssembler::kCallInstructionLength; |
46 } | 46 } |
47 | 47 |
48 | 48 |
| 49 #ifdef DEBUG |
| 50 // Overwrites code with int3 instructions. |
| 51 static void ZapCodeRange(Address from, Address to) { |
| 52 CHECK(from <= to); |
| 53 int length = static_cast<int>(to - from); |
| 54 CodePatcher destroyer(from, length); |
| 55 while (length-- > 0) { |
| 56 destroyer.masm()->int3(); |
| 57 } |
| 58 } |
| 59 #endif |
| 60 |
| 61 |
| 62 // Iterate through the entries of a SafepointTable that corresponds to |
| 63 // deoptimization points. |
| 64 class SafepointTableDeoptimiztionEntryIterator { |
| 65 public: |
| 66 explicit SafepointTableDeoptimiztionEntryIterator(Code* code) |
| 67 : code_(code), table_(code), index_(-1), limit_(table_.length()) { |
| 68 FindNextIndex(); |
| 69 } |
| 70 |
| 71 SafepointEntry Next(Address* pc) { |
| 72 if (index_ >= limit_) { |
| 73 *pc = NULL; |
| 74 return SafepointEntry(); // Invalid entry. |
| 75 } |
| 76 *pc = code_->instruction_start() + table_.GetPcOffset(index_); |
| 77 SafepointEntry entry = table_.GetEntry(index_); |
| 78 FindNextIndex(); |
| 79 return entry; |
| 80 } |
| 81 |
| 82 private: |
| 83 void FindNextIndex() { |
| 84 ASSERT(index_ < limit_); |
| 85 while (++index_ < limit_) { |
| 86 if (table_.GetEntry(index_).deoptimization_index() != |
| 87 Safepoint::kNoDeoptimizationIndex) { |
| 88 return; |
| 89 } |
| 90 } |
| 91 } |
| 92 |
| 93 Code* code_; |
| 94 SafepointTable table_; |
| 95 // Index of next deoptimization entry. If negative after calling |
| 96 // FindNextIndex, there are no more, and Next will return an invalid |
| 97 // SafepointEntry. |
| 98 int index_; |
| 99 // Table length. |
| 100 int limit_; |
| 101 }; |
| 102 |
| 103 |
49 void Deoptimizer::DeoptimizeFunction(JSFunction* function) { | 104 void Deoptimizer::DeoptimizeFunction(JSFunction* function) { |
50 AssertNoAllocation no_allocation; | 105 AssertNoAllocation no_allocation; |
51 | 106 |
52 if (!function->IsOptimized()) return; | 107 if (!function->IsOptimized()) return; |
53 | 108 |
54 // Get the optimized code. | 109 // Get the optimized code. |
55 Code* code = function->code(); | 110 Code* code = function->code(); |
56 | 111 |
57 // Invalidate the relocation information, as it will become invalid by the | 112 // Invalidate the relocation information, as it will become invalid by the |
58 // code patching below, and is not needed any more. | 113 // code patching below, and is not needed any more. |
59 code->InvalidateRelocation(); | 114 code->InvalidateRelocation(); |
60 | 115 |
61 // For each return after a safepoint insert a absolute call to the | 116 // For each return after a safepoint insert a absolute call to the |
62 // corresponding deoptimization entry. | 117 // corresponding deoptimization entry, or a short call to an absolute |
63 unsigned last_pc_offset = 0; | 118 // jump if space is short. The absolute jumps are put in a table just |
64 SafepointTable table(function->code()); | 119 // before the safepoint table (space was allocated there when the Code |
65 for (unsigned i = 0; i < table.length(); i++) { | 120 // object was created, if necessary). |
66 unsigned pc_offset = table.GetPcOffset(i); | 121 |
67 SafepointEntry safepoint_entry = table.GetEntry(i); | 122 Address instruction_start = function->code()->instruction_start(); |
68 int deoptimization_index = safepoint_entry.deoptimization_index(); | 123 Address jump_table_address = |
69 int gap_code_size = safepoint_entry.gap_code_size(); | 124 instruction_start + function->code()->safepoint_table_offset(); |
| 125 Address previous_pc = instruction_start; |
| 126 |
| 127 SafepointTableDeoptimiztionEntryIterator deoptimizations(function->code()); |
| 128 Address entry_pc = NULL; |
| 129 |
| 130 SafepointEntry current_entry = deoptimizations.Next(&entry_pc); |
| 131 while (current_entry.is_valid()) { |
| 132 int gap_code_size = current_entry.gap_code_size(); |
| 133 unsigned deoptimization_index = current_entry.deoptimization_index(); |
| 134 |
70 #ifdef DEBUG | 135 #ifdef DEBUG |
71 // Destroy the code which is not supposed to run again. | 136 // Destroy the code which is not supposed to run again. |
72 unsigned instructions = pc_offset - last_pc_offset; | 137 ZapCodeRange(previous_pc, entry_pc); |
73 CodePatcher destroyer(code->instruction_start() + last_pc_offset, | |
74 instructions); | |
75 for (unsigned i = 0; i < instructions; i++) { | |
76 destroyer.masm()->int3(); | |
77 } | |
78 #endif | 138 #endif |
79 last_pc_offset = pc_offset; | 139 // Position where Call will be patched in. |
80 if (deoptimization_index != Safepoint::kNoDeoptimizationIndex) { | 140 Address call_address = entry_pc + gap_code_size; |
81 last_pc_offset += gap_code_size; | 141 // End of call instruction, if using a direct call to a 64-bit address. |
82 CodePatcher patcher(code->instruction_start() + last_pc_offset, | 142 Address call_end_address = |
83 patch_size()); | 143 call_address + MacroAssembler::kCallInstructionLength; |
| 144 |
| 145 // Find next deoptimization entry, if any. |
| 146 Address next_pc = NULL; |
| 147 SafepointEntry next_entry = deoptimizations.Next(&next_pc); |
| 148 |
| 149 if (!next_entry.is_valid() || next_pc >= call_end_address) { |
| 150 // Room enough to write a long call instruction. |
| 151 CodePatcher patcher(call_address, Assembler::kCallInstructionLength); |
84 patcher.masm()->Call(GetDeoptimizationEntry(deoptimization_index, LAZY), | 152 patcher.masm()->Call(GetDeoptimizationEntry(deoptimization_index, LAZY), |
85 RelocInfo::NONE); | 153 RelocInfo::NONE); |
86 last_pc_offset += patch_size(); | 154 previous_pc = call_end_address; |
| 155 } else { |
| 156 // Not room enough for a long Call instruction. Write a short call |
| 157 // instruction to a long jump placed elsewhere in the code. |
| 158 Address short_call_end_address = |
| 159 call_address + MacroAssembler::kShortCallInstructionLength; |
| 160 ASSERT(next_pc >= short_call_end_address); |
| 161 |
| 162 // Write jump in jump-table. |
| 163 jump_table_address -= MacroAssembler::kJumpInstructionLength; |
| 164 CodePatcher jump_patcher(jump_table_address, |
| 165 MacroAssembler::kJumpInstructionLength); |
| 166 jump_patcher.masm()->Jump( |
| 167 GetDeoptimizationEntry(deoptimization_index, LAZY), |
| 168 RelocInfo::NONE); |
| 169 |
| 170 // Write call to jump at call_offset. |
| 171 CodePatcher call_patcher(call_address, |
| 172 MacroAssembler::kShortCallInstructionLength); |
| 173 call_patcher.masm()->call(jump_table_address); |
| 174 previous_pc = short_call_end_address; |
87 } | 175 } |
| 176 |
| 177 // Continue with next deoptimization entry. |
| 178 current_entry = next_entry; |
| 179 entry_pc = next_pc; |
88 } | 180 } |
| 181 |
89 #ifdef DEBUG | 182 #ifdef DEBUG |
90 // Destroy the code which is not supposed to run again. | 183 // Destroy the code which is not supposed to run again. |
91 CHECK(code->safepoint_table_offset() >= last_pc_offset); | 184 ZapCodeRange(previous_pc, jump_table_address); |
92 unsigned instructions = code->safepoint_table_offset() - last_pc_offset; | |
93 CodePatcher destroyer(code->instruction_start() + last_pc_offset, | |
94 instructions); | |
95 for (unsigned i = 0; i < instructions; i++) { | |
96 destroyer.masm()->int3(); | |
97 } | |
98 #endif | 185 #endif |
99 | 186 |
100 // Add the deoptimizing code to the list. | 187 // Add the deoptimizing code to the list. |
101 DeoptimizingCodeListNode* node = new DeoptimizingCodeListNode(code); | 188 DeoptimizingCodeListNode* node = new DeoptimizingCodeListNode(code); |
102 node->set_next(deoptimizing_code_list_); | 189 node->set_next(deoptimizing_code_list_); |
103 deoptimizing_code_list_ = node; | 190 deoptimizing_code_list_ = node; |
104 | 191 |
105 // Set the code for the function to non-optimized version. | 192 // Set the code for the function to non-optimized version. |
106 function->ReplaceCode(function->shared()->code()); | 193 function->ReplaceCode(function->shared()->code()); |
107 | 194 |
(...skipping 275 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
383 // Preserve deoptimizer object in register rax and get the input | 470 // Preserve deoptimizer object in register rax and get the input |
384 // frame descriptor pointer. | 471 // frame descriptor pointer. |
385 __ movq(rbx, Operand(rax, Deoptimizer::input_offset())); | 472 __ movq(rbx, Operand(rax, Deoptimizer::input_offset())); |
386 | 473 |
387 // Fill in the input registers. | 474 // Fill in the input registers. |
388 for (int i = kNumberOfRegisters -1; i >= 0; i--) { | 475 for (int i = kNumberOfRegisters -1; i >= 0; i--) { |
389 int offset = (i * kPointerSize) + FrameDescription::registers_offset(); | 476 int offset = (i * kPointerSize) + FrameDescription::registers_offset(); |
390 __ pop(Operand(rbx, offset)); | 477 __ pop(Operand(rbx, offset)); |
391 } | 478 } |
392 | 479 |
393 // Fill in the double input registers. | 480 // Fill in the double input registers. |
394 int double_regs_offset = FrameDescription::double_registers_offset(); | 481 int double_regs_offset = FrameDescription::double_registers_offset(); |
395 for (int i = 0; i < XMMRegister::kNumAllocatableRegisters; i++) { | 482 for (int i = 0; i < XMMRegister::kNumAllocatableRegisters; i++) { |
396 int dst_offset = i * kDoubleSize + double_regs_offset; | 483 int dst_offset = i * kDoubleSize + double_regs_offset; |
397 __ pop(Operand(rbx, dst_offset)); | 484 __ pop(Operand(rbx, dst_offset)); |
398 } | 485 } |
399 | 486 |
400 // Remove the bailout id from the stack. | 487 // Remove the bailout id from the stack. |
401 if (type() == EAGER) { | 488 if (type() == EAGER) { |
402 __ addq(rsp, Immediate(kPointerSize)); | 489 __ addq(rsp, Immediate(kPointerSize)); |
403 } else { | 490 } else { |
404 __ addq(rsp, Immediate(2 * kPointerSize)); | 491 __ addq(rsp, Immediate(2 * kPointerSize)); |
405 } | 492 } |
406 | 493 |
407 // Compute a pointer to the unwinding limit in register ecx; that is | 494 // Compute a pointer to the unwinding limit in register rcx; that is |
408 // the first stack slot not part of the input frame. | 495 // the first stack slot not part of the input frame. |
409 __ movq(rcx, Operand(rbx, FrameDescription::frame_size_offset())); | 496 __ movq(rcx, Operand(rbx, FrameDescription::frame_size_offset())); |
410 __ addq(rcx, rsp); | 497 __ addq(rcx, rsp); |
411 | 498 |
412 // Unwind the stack down to - but not including - the unwinding | 499 // Unwind the stack down to - but not including - the unwinding |
413 // limit and copy the contents of the activation frame to the input | 500 // limit and copy the contents of the activation frame to the input |
414 // frame description. | 501 // frame description. |
415 __ lea(rdx, Operand(rbx, FrameDescription::frame_content_offset())); | 502 __ lea(rdx, Operand(rbx, FrameDescription::frame_content_offset())); |
416 Label pop_loop; | 503 Label pop_loop; |
417 __ bind(&pop_loop); | 504 __ bind(&pop_loop); |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
506 } | 593 } |
507 __ bind(&done); | 594 __ bind(&done); |
508 } | 595 } |
509 | 596 |
510 #undef __ | 597 #undef __ |
511 | 598 |
512 | 599 |
513 } } // namespace v8::internal | 600 } } // namespace v8::internal |
514 | 601 |
515 #endif // V8_TARGET_ARCH_X64 | 602 #endif // V8_TARGET_ARCH_X64 |
OLD | NEW |