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