Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 1 | // Copyright 2011 the V8 project authors. All rights reserved. |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 2 | // Redistribution and use in source and binary forms, with or without |
| 3 | // modification, are permitted provided that the following conditions are |
| 4 | // met: |
| 5 | // |
| 6 | // * Redistributions of source code must retain the above copyright |
| 7 | // notice, this list of conditions and the following disclaimer. |
| 8 | // * Redistributions in binary form must reproduce the above |
| 9 | // copyright notice, this list of conditions and the following |
| 10 | // disclaimer in the documentation and/or other materials provided |
| 11 | // with the distribution. |
| 12 | // * Neither the name of Google Inc. nor the names of its |
| 13 | // contributors may be used to endorse or promote products derived |
| 14 | // from this software without specific prior written permission. |
| 15 | // |
| 16 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | |
| 28 | #include "v8.h" |
| 29 | |
| 30 | #include "codegen.h" |
| 31 | #include "deoptimizer.h" |
| 32 | #include "full-codegen.h" |
| 33 | #include "safepoint-table.h" |
| 34 | |
| 35 | namespace v8 { |
| 36 | namespace internal { |
| 37 | |
| 38 | int Deoptimizer::table_entry_size_ = 16; |
| 39 | |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 40 | |
| 41 | int Deoptimizer::patch_size() { |
| 42 | const int kCallInstructionSizeInWords = 3; |
| 43 | return kCallInstructionSizeInWords * Assembler::kInstrSize; |
| 44 | } |
| 45 | |
| 46 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 47 | void Deoptimizer::EnsureRelocSpaceForLazyDeoptimization(Handle<Code> code) { |
| 48 | // Nothing to do. No new relocation information is written for lazy |
| 49 | // deoptimization on ARM. |
| 50 | } |
| 51 | |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 52 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 53 | void Deoptimizer::DeoptimizeFunction(JSFunction* function) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 54 | HandleScope scope; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 55 | AssertNoAllocation no_allocation; |
| 56 | |
| 57 | if (!function->IsOptimized()) return; |
| 58 | |
| 59 | // Get the optimized code. |
| 60 | Code* code = function->code(); |
| 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 | |
| 66 | // For each return after a safepoint insert an absolute call to the |
| 67 | // corresponding deoptimization entry. |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 68 | ASSERT(patch_size() % Assembler::kInstrSize == 0); |
| 69 | int call_size_in_words = patch_size() / Assembler::kInstrSize; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 70 | unsigned last_pc_offset = 0; |
| 71 | SafepointTable table(function->code()); |
| 72 | for (unsigned i = 0; i < table.length(); i++) { |
| 73 | unsigned pc_offset = table.GetPcOffset(i); |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 74 | SafepointEntry safepoint_entry = table.GetEntry(i); |
| 75 | int deoptimization_index = safepoint_entry.deoptimization_index(); |
| 76 | int gap_code_size = safepoint_entry.gap_code_size(); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 77 | // Check that we did not shoot past next safepoint. |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 78 | CHECK(pc_offset >= last_pc_offset); |
| 79 | #ifdef DEBUG |
| 80 | // Destroy the code which is not supposed to be run again. |
| 81 | int instructions = (pc_offset - last_pc_offset) / Assembler::kInstrSize; |
| 82 | CodePatcher destroyer(code->instruction_start() + last_pc_offset, |
| 83 | instructions); |
| 84 | for (int x = 0; x < instructions; x++) { |
| 85 | destroyer.masm()->bkpt(0); |
| 86 | } |
| 87 | #endif |
| 88 | last_pc_offset = pc_offset; |
| 89 | if (deoptimization_index != Safepoint::kNoDeoptimizationIndex) { |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 90 | last_pc_offset += gap_code_size; |
| 91 | CodePatcher patcher(code->instruction_start() + last_pc_offset, |
| 92 | call_size_in_words); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 93 | Address deoptimization_entry = Deoptimizer::GetDeoptimizationEntry( |
| 94 | deoptimization_index, Deoptimizer::LAZY); |
| 95 | patcher.masm()->Call(deoptimization_entry, RelocInfo::NONE); |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 96 | last_pc_offset += patch_size(); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 97 | } |
| 98 | } |
| 99 | |
| 100 | |
| 101 | #ifdef DEBUG |
| 102 | // Destroy the code which is not supposed to be run again. |
| 103 | int instructions = |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 104 | (code->safepoint_table_offset() - last_pc_offset) / Assembler::kInstrSize; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 105 | CodePatcher destroyer(code->instruction_start() + last_pc_offset, |
| 106 | instructions); |
| 107 | for (int x = 0; x < instructions; x++) { |
| 108 | destroyer.masm()->bkpt(0); |
| 109 | } |
| 110 | #endif |
| 111 | |
| 112 | // Add the deoptimizing code to the list. |
| 113 | DeoptimizingCodeListNode* node = new DeoptimizingCodeListNode(code); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 114 | DeoptimizerData* data = code->GetIsolate()->deoptimizer_data(); |
| 115 | node->set_next(data->deoptimizing_code_list_); |
| 116 | data->deoptimizing_code_list_ = node; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 117 | |
| 118 | // Set the code for the function to non-optimized version. |
| 119 | function->ReplaceCode(function->shared()->code()); |
| 120 | |
| 121 | if (FLAG_trace_deopt) { |
| 122 | PrintF("[forced deoptimization: "); |
| 123 | function->PrintName(); |
| 124 | PrintF(" / %x]\n", reinterpret_cast<uint32_t>(function)); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 125 | #ifdef DEBUG |
| 126 | if (FLAG_print_code) { |
| 127 | code->PrintLn(); |
| 128 | } |
| 129 | #endif |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 130 | } |
| 131 | } |
| 132 | |
| 133 | |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 134 | void Deoptimizer::PatchStackCheckCodeAt(Address pc_after, |
| 135 | Code* check_code, |
| 136 | Code* replacement_code) { |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 137 | const int kInstrSize = Assembler::kInstrSize; |
| 138 | // The call of the stack guard check has the following form: |
| 139 | // e1 5d 00 0c cmp sp, <limit> |
| 140 | // 2a 00 00 01 bcs ok |
| 141 | // e5 9f c? ?? ldr ip, [pc, <stack guard address>] |
| 142 | // e1 2f ff 3c blx ip |
| 143 | ASSERT(Memory::int32_at(pc_after - kInstrSize) == |
| 144 | (al | B24 | B21 | 15*B16 | 15*B12 | 15*B8 | BLX | ip.code())); |
| 145 | ASSERT(Assembler::IsLdrPcImmediateOffset( |
| 146 | Assembler::instr_at(pc_after - 2 * kInstrSize))); |
| 147 | |
| 148 | // We patch the code to the following form: |
| 149 | // e1 5d 00 0c cmp sp, <limit> |
| 150 | // e1 a0 00 00 mov r0, r0 (NOP) |
| 151 | // e5 9f c? ?? ldr ip, [pc, <on-stack replacement address>] |
| 152 | // e1 2f ff 3c blx ip |
| 153 | // and overwrite the constant containing the |
| 154 | // address of the stack check stub. |
| 155 | |
| 156 | // Replace conditional jump with NOP. |
| 157 | CodePatcher patcher(pc_after - 3 * kInstrSize, 1); |
| 158 | patcher.masm()->nop(); |
| 159 | |
| 160 | // Replace the stack check address in the constant pool |
| 161 | // with the entry address of the replacement code. |
| 162 | uint32_t stack_check_address_offset = Memory::uint16_at(pc_after - |
| 163 | 2 * kInstrSize) & 0xfff; |
| 164 | Address stack_check_address_pointer = pc_after + stack_check_address_offset; |
| 165 | ASSERT(Memory::uint32_at(stack_check_address_pointer) == |
| 166 | reinterpret_cast<uint32_t>(check_code->entry())); |
| 167 | Memory::uint32_at(stack_check_address_pointer) = |
| 168 | reinterpret_cast<uint32_t>(replacement_code->entry()); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 172 | void Deoptimizer::RevertStackCheckCodeAt(Address pc_after, |
| 173 | Code* check_code, |
| 174 | Code* replacement_code) { |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 175 | const int kInstrSize = Assembler::kInstrSize; |
| 176 | ASSERT(Memory::uint32_at(pc_after - kInstrSize) == 0xe12fff3c); |
| 177 | ASSERT(Memory::uint8_at(pc_after - kInstrSize - 1) == 0xe5); |
| 178 | ASSERT(Memory::uint8_at(pc_after - kInstrSize - 2) == 0x9f); |
| 179 | |
| 180 | // Replace NOP with conditional jump. |
| 181 | CodePatcher patcher(pc_after - 3 * kInstrSize, 1); |
| 182 | patcher.masm()->b(+4, cs); |
| 183 | |
| 184 | // Replace the stack check address in the constant pool |
| 185 | // with the entry address of the replacement code. |
| 186 | uint32_t stack_check_address_offset = Memory::uint16_at(pc_after - |
| 187 | 2 * kInstrSize) & 0xfff; |
| 188 | Address stack_check_address_pointer = pc_after + stack_check_address_offset; |
| 189 | ASSERT(Memory::uint32_at(stack_check_address_pointer) == |
| 190 | reinterpret_cast<uint32_t>(replacement_code->entry())); |
| 191 | Memory::uint32_at(stack_check_address_pointer) = |
| 192 | reinterpret_cast<uint32_t>(check_code->entry()); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 196 | static int LookupBailoutId(DeoptimizationInputData* data, unsigned ast_id) { |
| 197 | ByteArray* translations = data->TranslationByteArray(); |
| 198 | int length = data->DeoptCount(); |
| 199 | for (int i = 0; i < length; i++) { |
| 200 | if (static_cast<unsigned>(data->AstId(i)->value()) == ast_id) { |
| 201 | TranslationIterator it(translations, data->TranslationIndex(i)->value()); |
| 202 | int value = it.Next(); |
| 203 | ASSERT(Translation::BEGIN == static_cast<Translation::Opcode>(value)); |
| 204 | // Read the number of frames. |
| 205 | value = it.Next(); |
| 206 | if (value == 1) return i; |
| 207 | } |
| 208 | } |
| 209 | UNREACHABLE(); |
| 210 | return -1; |
| 211 | } |
| 212 | |
| 213 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 214 | void Deoptimizer::DoComputeOsrOutputFrame() { |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 215 | DeoptimizationInputData* data = DeoptimizationInputData::cast( |
| 216 | optimized_code_->deoptimization_data()); |
| 217 | unsigned ast_id = data->OsrAstId()->value(); |
| 218 | |
| 219 | int bailout_id = LookupBailoutId(data, ast_id); |
| 220 | unsigned translation_index = data->TranslationIndex(bailout_id)->value(); |
| 221 | ByteArray* translations = data->TranslationByteArray(); |
| 222 | |
| 223 | TranslationIterator iterator(translations, translation_index); |
| 224 | Translation::Opcode opcode = |
| 225 | static_cast<Translation::Opcode>(iterator.Next()); |
| 226 | ASSERT(Translation::BEGIN == opcode); |
| 227 | USE(opcode); |
| 228 | int count = iterator.Next(); |
| 229 | ASSERT(count == 1); |
| 230 | USE(count); |
| 231 | |
| 232 | opcode = static_cast<Translation::Opcode>(iterator.Next()); |
| 233 | USE(opcode); |
| 234 | ASSERT(Translation::FRAME == opcode); |
| 235 | unsigned node_id = iterator.Next(); |
| 236 | USE(node_id); |
| 237 | ASSERT(node_id == ast_id); |
| 238 | JSFunction* function = JSFunction::cast(ComputeLiteral(iterator.Next())); |
| 239 | USE(function); |
| 240 | ASSERT(function == function_); |
| 241 | unsigned height = iterator.Next(); |
| 242 | unsigned height_in_bytes = height * kPointerSize; |
| 243 | USE(height_in_bytes); |
| 244 | |
| 245 | unsigned fixed_size = ComputeFixedSize(function_); |
| 246 | unsigned input_frame_size = input_->GetFrameSize(); |
| 247 | ASSERT(fixed_size + height_in_bytes == input_frame_size); |
| 248 | |
| 249 | unsigned stack_slot_size = optimized_code_->stack_slots() * kPointerSize; |
| 250 | unsigned outgoing_height = data->ArgumentsStackHeight(bailout_id)->value(); |
| 251 | unsigned outgoing_size = outgoing_height * kPointerSize; |
| 252 | unsigned output_frame_size = fixed_size + stack_slot_size + outgoing_size; |
| 253 | ASSERT(outgoing_size == 0); // OSR does not happen in the middle of a call. |
| 254 | |
| 255 | if (FLAG_trace_osr) { |
| 256 | PrintF("[on-stack replacement: begin 0x%08" V8PRIxPTR " ", |
| 257 | reinterpret_cast<intptr_t>(function_)); |
| 258 | function_->PrintName(); |
| 259 | PrintF(" => node=%u, frame=%d->%d]\n", |
| 260 | ast_id, |
| 261 | input_frame_size, |
| 262 | output_frame_size); |
| 263 | } |
| 264 | |
| 265 | // There's only one output frame in the OSR case. |
| 266 | output_count_ = 1; |
| 267 | output_ = new FrameDescription*[1]; |
| 268 | output_[0] = new(output_frame_size) FrameDescription( |
| 269 | output_frame_size, function_); |
| 270 | |
| 271 | // Clear the incoming parameters in the optimized frame to avoid |
| 272 | // confusing the garbage collector. |
| 273 | unsigned output_offset = output_frame_size - kPointerSize; |
| 274 | int parameter_count = function_->shared()->formal_parameter_count() + 1; |
| 275 | for (int i = 0; i < parameter_count; ++i) { |
| 276 | output_[0]->SetFrameSlot(output_offset, 0); |
| 277 | output_offset -= kPointerSize; |
| 278 | } |
| 279 | |
| 280 | // Translate the incoming parameters. This may overwrite some of the |
| 281 | // incoming argument slots we've just cleared. |
| 282 | int input_offset = input_frame_size - kPointerSize; |
| 283 | bool ok = true; |
| 284 | int limit = input_offset - (parameter_count * kPointerSize); |
| 285 | while (ok && input_offset > limit) { |
| 286 | ok = DoOsrTranslateCommand(&iterator, &input_offset); |
| 287 | } |
| 288 | |
| 289 | // There are no translation commands for the caller's pc and fp, the |
| 290 | // context, and the function. Set them up explicitly. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 291 | for (int i = StandardFrameConstants::kCallerPCOffset; |
| 292 | ok && i >= StandardFrameConstants::kMarkerOffset; |
| 293 | i -= kPointerSize) { |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 294 | uint32_t input_value = input_->GetFrameSlot(input_offset); |
| 295 | if (FLAG_trace_osr) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 296 | const char* name = "UNKNOWN"; |
| 297 | switch (i) { |
| 298 | case StandardFrameConstants::kCallerPCOffset: |
| 299 | name = "caller's pc"; |
| 300 | break; |
| 301 | case StandardFrameConstants::kCallerFPOffset: |
| 302 | name = "fp"; |
| 303 | break; |
| 304 | case StandardFrameConstants::kContextOffset: |
| 305 | name = "context"; |
| 306 | break; |
| 307 | case StandardFrameConstants::kMarkerOffset: |
| 308 | name = "function"; |
| 309 | break; |
| 310 | } |
| 311 | PrintF(" [sp + %d] <- 0x%08x ; [sp + %d] (fixed part - %s)\n", |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 312 | output_offset, |
| 313 | input_value, |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 314 | input_offset, |
| 315 | name); |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 316 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 317 | |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 318 | output_[0]->SetFrameSlot(output_offset, input_->GetFrameSlot(input_offset)); |
| 319 | input_offset -= kPointerSize; |
| 320 | output_offset -= kPointerSize; |
| 321 | } |
| 322 | |
| 323 | // Translate the rest of the frame. |
| 324 | while (ok && input_offset >= 0) { |
| 325 | ok = DoOsrTranslateCommand(&iterator, &input_offset); |
| 326 | } |
| 327 | |
| 328 | // If translation of any command failed, continue using the input frame. |
| 329 | if (!ok) { |
| 330 | delete output_[0]; |
| 331 | output_[0] = input_; |
| 332 | output_[0]->SetPc(reinterpret_cast<uint32_t>(from_)); |
| 333 | } else { |
| 334 | // Setup the frame pointer and the context pointer. |
| 335 | output_[0]->SetRegister(fp.code(), input_->GetRegister(fp.code())); |
| 336 | output_[0]->SetRegister(cp.code(), input_->GetRegister(cp.code())); |
| 337 | |
| 338 | unsigned pc_offset = data->OsrPcOffset()->value(); |
| 339 | uint32_t pc = reinterpret_cast<uint32_t>( |
| 340 | optimized_code_->entry() + pc_offset); |
| 341 | output_[0]->SetPc(pc); |
| 342 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 343 | Code* continuation = isolate_->builtins()->builtin(Builtins::kNotifyOSR); |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 344 | output_[0]->SetContinuation( |
| 345 | reinterpret_cast<uint32_t>(continuation->entry())); |
| 346 | |
| 347 | if (FLAG_trace_osr) { |
| 348 | PrintF("[on-stack replacement translation %s: 0x%08" V8PRIxPTR " ", |
| 349 | ok ? "finished" : "aborted", |
| 350 | reinterpret_cast<intptr_t>(function)); |
| 351 | function->PrintName(); |
| 352 | PrintF(" => pc=0x%0x]\n", output_[0]->GetPc()); |
| 353 | } |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 354 | } |
| 355 | |
| 356 | |
| 357 | // This code is very similar to ia32 code, but relies on register names (fp, sp) |
| 358 | // and how the frame is laid out. |
| 359 | void Deoptimizer::DoComputeFrame(TranslationIterator* iterator, |
| 360 | int frame_index) { |
| 361 | // Read the ast node id, function, and frame height for this output frame. |
| 362 | Translation::Opcode opcode = |
| 363 | static_cast<Translation::Opcode>(iterator->Next()); |
| 364 | USE(opcode); |
| 365 | ASSERT(Translation::FRAME == opcode); |
| 366 | int node_id = iterator->Next(); |
| 367 | JSFunction* function = JSFunction::cast(ComputeLiteral(iterator->Next())); |
| 368 | unsigned height = iterator->Next(); |
| 369 | unsigned height_in_bytes = height * kPointerSize; |
| 370 | if (FLAG_trace_deopt) { |
| 371 | PrintF(" translating "); |
| 372 | function->PrintName(); |
| 373 | PrintF(" => node=%d, height=%d\n", node_id, height_in_bytes); |
| 374 | } |
| 375 | |
| 376 | // The 'fixed' part of the frame consists of the incoming parameters and |
| 377 | // the part described by JavaScriptFrameConstants. |
| 378 | unsigned fixed_frame_size = ComputeFixedSize(function); |
| 379 | unsigned input_frame_size = input_->GetFrameSize(); |
| 380 | unsigned output_frame_size = height_in_bytes + fixed_frame_size; |
| 381 | |
| 382 | // Allocate and store the output frame description. |
| 383 | FrameDescription* output_frame = |
| 384 | new(output_frame_size) FrameDescription(output_frame_size, function); |
| 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); |
| 433 | if (FLAG_trace_deopt) { |
| 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 | } |
| 456 | if (FLAG_trace_deopt) { |
| 457 | PrintF(" 0x%08x: [top + %d] <- 0x%08x ; caller's fp\n", |
| 458 | fp_value, output_offset, value); |
| 459 | } |
| 460 | |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 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. |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 464 | output_offset -= kPointerSize; |
| 465 | input_offset -= kPointerSize; |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 466 | if (is_bottommost) { |
| 467 | value = input_->GetFrameSlot(input_offset); |
| 468 | } else { |
| 469 | value = reinterpret_cast<intptr_t>(function->context()); |
| 470 | } |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 471 | output_frame->SetFrameSlot(output_offset, value); |
| 472 | if (is_topmost) { |
| 473 | output_frame->SetRegister(cp.code(), value); |
| 474 | } |
| 475 | if (FLAG_trace_deopt) { |
| 476 | PrintF(" 0x%08x: [top + %d] <- 0x%08x ; context\n", |
| 477 | top_address + output_offset, output_offset, value); |
| 478 | } |
| 479 | |
| 480 | // The function was mentioned explicitly in the BEGIN_FRAME. |
| 481 | output_offset -= kPointerSize; |
| 482 | input_offset -= kPointerSize; |
| 483 | value = reinterpret_cast<uint32_t>(function); |
| 484 | // The function for the bottommost output frame should also agree with the |
| 485 | // input frame. |
| 486 | ASSERT(!is_bottommost || input_->GetFrameSlot(input_offset) == value); |
| 487 | output_frame->SetFrameSlot(output_offset, value); |
| 488 | if (FLAG_trace_deopt) { |
| 489 | PrintF(" 0x%08x: [top + %d] <- 0x%08x ; function\n", |
| 490 | top_address + output_offset, output_offset, value); |
| 491 | } |
| 492 | |
| 493 | // Translate the rest of the frame. |
| 494 | for (unsigned i = 0; i < height; ++i) { |
| 495 | output_offset -= kPointerSize; |
| 496 | DoTranslateCommand(iterator, frame_index, output_offset); |
| 497 | } |
| 498 | ASSERT(0 == output_offset); |
| 499 | |
| 500 | // Compute this frame's PC, state, and continuation. |
| 501 | Code* non_optimized_code = function->shared()->code(); |
| 502 | FixedArray* raw_data = non_optimized_code->deoptimization_data(); |
| 503 | DeoptimizationOutputData* data = DeoptimizationOutputData::cast(raw_data); |
| 504 | Address start = non_optimized_code->instruction_start(); |
| 505 | unsigned pc_and_state = GetOutputInfo(data, node_id, function->shared()); |
| 506 | unsigned pc_offset = FullCodeGenerator::PcField::decode(pc_and_state); |
| 507 | uint32_t pc_value = reinterpret_cast<uint32_t>(start + pc_offset); |
| 508 | output_frame->SetPc(pc_value); |
| 509 | if (is_topmost) { |
| 510 | output_frame->SetRegister(pc.code(), pc_value); |
| 511 | } |
| 512 | |
| 513 | FullCodeGenerator::State state = |
| 514 | FullCodeGenerator::StateField::decode(pc_and_state); |
| 515 | output_frame->SetState(Smi::FromInt(state)); |
| 516 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 517 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 518 | // Set the continuation for the topmost frame. |
| 519 | if (is_topmost) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 520 | Builtins* builtins = isolate_->builtins(); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 521 | Code* continuation = (bailout_type_ == EAGER) |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 522 | ? builtins->builtin(Builtins::kNotifyDeoptimized) |
| 523 | : builtins->builtin(Builtins::kNotifyLazyDeoptimized); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 524 | output_frame->SetContinuation( |
| 525 | reinterpret_cast<uint32_t>(continuation->entry())); |
| 526 | } |
| 527 | |
| 528 | if (output_count_ - 1 == frame_index) iterator->Done(); |
| 529 | } |
| 530 | |
| 531 | |
| 532 | #define __ masm()-> |
| 533 | |
| 534 | |
| 535 | // This code tries to be close to ia32 code so that any changes can be |
| 536 | // easily ported. |
| 537 | void Deoptimizer::EntryGenerator::Generate() { |
| 538 | GeneratePrologue(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 539 | |
| 540 | Isolate* isolate = masm()->isolate(); |
| 541 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 542 | CpuFeatures::Scope scope(VFP3); |
| 543 | // Save all general purpose registers before messing with them. |
| 544 | const int kNumberOfRegisters = Register::kNumRegisters; |
| 545 | |
| 546 | // Everything but pc, lr and ip which will be saved but not restored. |
| 547 | RegList restored_regs = kJSCallerSaved | kCalleeSaved | ip.bit(); |
| 548 | |
| 549 | const int kDoubleRegsSize = |
| 550 | kDoubleSize * DwVfpRegister::kNumAllocatableRegisters; |
| 551 | |
| 552 | // Save all general purpose registers before messing with them. |
| 553 | __ sub(sp, sp, Operand(kDoubleRegsSize)); |
| 554 | for (int i = 0; i < DwVfpRegister::kNumAllocatableRegisters; ++i) { |
| 555 | DwVfpRegister vfp_reg = DwVfpRegister::FromAllocationIndex(i); |
| 556 | int offset = i * kDoubleSize; |
| 557 | __ vstr(vfp_reg, sp, offset); |
| 558 | } |
| 559 | |
| 560 | // Push all 16 registers (needed to populate FrameDescription::registers_). |
| 561 | __ stm(db_w, sp, restored_regs | sp.bit() | lr.bit() | pc.bit()); |
| 562 | |
| 563 | const int kSavedRegistersAreaSize = |
| 564 | (kNumberOfRegisters * kPointerSize) + kDoubleRegsSize; |
| 565 | |
| 566 | // Get the bailout id from the stack. |
| 567 | __ ldr(r2, MemOperand(sp, kSavedRegistersAreaSize)); |
| 568 | |
| 569 | // Get the address of the location in the code object if possible (r3) (return |
| 570 | // address for lazy deoptimization) and compute the fp-to-sp delta in |
| 571 | // register r4. |
| 572 | if (type() == EAGER) { |
| 573 | __ mov(r3, Operand(0)); |
| 574 | // Correct one word for bailout id. |
| 575 | __ add(r4, sp, Operand(kSavedRegistersAreaSize + (1 * kPointerSize))); |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 576 | } else if (type() == OSR) { |
| 577 | __ mov(r3, lr); |
| 578 | // Correct one word for bailout id. |
| 579 | __ add(r4, sp, Operand(kSavedRegistersAreaSize + (1 * kPointerSize))); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 580 | } else { |
| 581 | __ mov(r3, lr); |
| 582 | // Correct two words for bailout id and return address. |
| 583 | __ add(r4, sp, Operand(kSavedRegistersAreaSize + (2 * kPointerSize))); |
| 584 | } |
| 585 | __ sub(r4, fp, r4); |
| 586 | |
| 587 | // Allocate a new deoptimizer object. |
| 588 | // Pass four arguments in r0 to r3 and fifth argument on stack. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame^] | 589 | __ PrepareCallCFunction(6, r5); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 590 | __ ldr(r0, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset)); |
| 591 | __ mov(r1, Operand(type())); // bailout type, |
| 592 | // r2: bailout id already loaded. |
| 593 | // r3: code address or 0 already loaded. |
| 594 | __ str(r4, MemOperand(sp, 0 * kPointerSize)); // Fp-to-sp delta. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame^] | 595 | __ mov(r5, Operand(ExternalReference::isolate_address())); |
| 596 | __ str(r5, MemOperand(sp, 1 * kPointerSize)); // Isolate. |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 597 | // Call Deoptimizer::New(). |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame^] | 598 | __ CallCFunction(ExternalReference::new_deoptimizer_function(isolate), 6); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 599 | |
| 600 | // Preserve "deoptimizer" object in register r0 and get the input |
| 601 | // frame descriptor pointer to r1 (deoptimizer->input_); |
| 602 | __ ldr(r1, MemOperand(r0, Deoptimizer::input_offset())); |
| 603 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 604 | // Copy core registers into FrameDescription::registers_[kNumRegisters]. |
| 605 | ASSERT(Register::kNumRegisters == kNumberOfRegisters); |
| 606 | for (int i = 0; i < kNumberOfRegisters; i++) { |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 607 | int offset = (i * kPointerSize) + FrameDescription::registers_offset(); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 608 | __ ldr(r2, MemOperand(sp, i * kPointerSize)); |
| 609 | __ str(r2, MemOperand(r1, offset)); |
| 610 | } |
| 611 | |
| 612 | // Copy VFP registers to |
| 613 | // double_registers_[DoubleRegister::kNumAllocatableRegisters] |
| 614 | int double_regs_offset = FrameDescription::double_registers_offset(); |
| 615 | for (int i = 0; i < DwVfpRegister::kNumAllocatableRegisters; ++i) { |
| 616 | int dst_offset = i * kDoubleSize + double_regs_offset; |
| 617 | int src_offset = i * kDoubleSize + kNumberOfRegisters * kPointerSize; |
| 618 | __ vldr(d0, sp, src_offset); |
| 619 | __ vstr(d0, r1, dst_offset); |
| 620 | } |
| 621 | |
| 622 | // Remove the bailout id, eventually return address, and the saved registers |
| 623 | // from the stack. |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 624 | if (type() == EAGER || type() == OSR) { |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 625 | __ add(sp, sp, Operand(kSavedRegistersAreaSize + (1 * kPointerSize))); |
| 626 | } else { |
| 627 | __ add(sp, sp, Operand(kSavedRegistersAreaSize + (2 * kPointerSize))); |
| 628 | } |
| 629 | |
| 630 | // Compute a pointer to the unwinding limit in register r2; that is |
| 631 | // the first stack slot not part of the input frame. |
| 632 | __ ldr(r2, MemOperand(r1, FrameDescription::frame_size_offset())); |
| 633 | __ add(r2, r2, sp); |
| 634 | |
| 635 | // Unwind the stack down to - but not including - the unwinding |
| 636 | // limit and copy the contents of the activation frame to the input |
| 637 | // frame description. |
| 638 | __ add(r3, r1, Operand(FrameDescription::frame_content_offset())); |
| 639 | Label pop_loop; |
| 640 | __ bind(&pop_loop); |
| 641 | __ pop(r4); |
| 642 | __ str(r4, MemOperand(r3, 0)); |
| 643 | __ add(r3, r3, Operand(sizeof(uint32_t))); |
| 644 | __ cmp(r2, sp); |
| 645 | __ b(ne, &pop_loop); |
| 646 | |
| 647 | // Compute the output frame in the deoptimizer. |
| 648 | __ push(r0); // Preserve deoptimizer object across call. |
| 649 | // r0: deoptimizer object; r1: scratch. |
| 650 | __ PrepareCallCFunction(1, r1); |
| 651 | // Call Deoptimizer::ComputeOutputFrames(). |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 652 | __ CallCFunction( |
| 653 | ExternalReference::compute_output_frames_function(isolate), 1); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 654 | __ pop(r0); // Restore deoptimizer object (class Deoptimizer). |
| 655 | |
| 656 | // Replace the current (input) frame with the output frames. |
| 657 | Label outer_push_loop, inner_push_loop; |
| 658 | // Outer loop state: r0 = current "FrameDescription** output_", |
| 659 | // r1 = one past the last FrameDescription**. |
| 660 | __ ldr(r1, MemOperand(r0, Deoptimizer::output_count_offset())); |
| 661 | __ ldr(r0, MemOperand(r0, Deoptimizer::output_offset())); // r0 is output_. |
| 662 | __ add(r1, r0, Operand(r1, LSL, 2)); |
| 663 | __ bind(&outer_push_loop); |
| 664 | // Inner loop state: r2 = current FrameDescription*, r3 = loop index. |
| 665 | __ ldr(r2, MemOperand(r0, 0)); // output_[ix] |
| 666 | __ ldr(r3, MemOperand(r2, FrameDescription::frame_size_offset())); |
| 667 | __ bind(&inner_push_loop); |
| 668 | __ sub(r3, r3, Operand(sizeof(uint32_t))); |
| 669 | // __ add(r6, r2, Operand(r3, LSL, 1)); |
| 670 | __ add(r6, r2, Operand(r3)); |
| 671 | __ ldr(r7, MemOperand(r6, FrameDescription::frame_content_offset())); |
| 672 | __ push(r7); |
| 673 | __ cmp(r3, Operand(0)); |
| 674 | __ b(ne, &inner_push_loop); // test for gt? |
| 675 | __ add(r0, r0, Operand(kPointerSize)); |
| 676 | __ cmp(r0, r1); |
| 677 | __ b(lt, &outer_push_loop); |
| 678 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 679 | // Push state, pc, and continuation from the last output frame. |
| 680 | if (type() != OSR) { |
| 681 | __ ldr(r6, MemOperand(r2, FrameDescription::state_offset())); |
| 682 | __ push(r6); |
| 683 | } |
| 684 | |
| 685 | __ ldr(r6, MemOperand(r2, FrameDescription::pc_offset())); |
| 686 | __ push(r6); |
| 687 | __ ldr(r6, MemOperand(r2, FrameDescription::continuation_offset())); |
| 688 | __ push(r6); |
| 689 | |
| 690 | // Push the registers from the last output frame. |
| 691 | for (int i = kNumberOfRegisters - 1; i >= 0; i--) { |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 692 | int offset = (i * kPointerSize) + FrameDescription::registers_offset(); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 693 | __ ldr(r6, MemOperand(r2, offset)); |
| 694 | __ push(r6); |
| 695 | } |
| 696 | |
| 697 | // Restore the registers from the stack. |
| 698 | __ ldm(ia_w, sp, restored_regs); // all but pc registers. |
| 699 | __ pop(ip); // remove sp |
| 700 | __ pop(ip); // remove lr |
| 701 | |
| 702 | // Set up the roots register. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 703 | ExternalReference roots_address = ExternalReference::roots_address(isolate); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 704 | __ mov(r10, Operand(roots_address)); |
| 705 | |
| 706 | __ pop(ip); // remove pc |
| 707 | __ pop(r7); // get continuation, leave pc on stack |
| 708 | __ pop(lr); |
| 709 | __ Jump(r7); |
| 710 | __ stop("Unreachable."); |
| 711 | } |
| 712 | |
| 713 | |
| 714 | void Deoptimizer::TableEntryGenerator::GeneratePrologue() { |
| 715 | // Create a sequence of deoptimization entries. Note that any |
| 716 | // registers may be still live. |
| 717 | Label done; |
| 718 | for (int i = 0; i < count(); i++) { |
| 719 | int start = masm()->pc_offset(); |
| 720 | USE(start); |
| 721 | if (type() == EAGER) { |
| 722 | __ nop(); |
| 723 | } else { |
| 724 | // Emulate ia32 like call by pushing return address to stack. |
| 725 | __ push(lr); |
| 726 | } |
| 727 | __ mov(ip, Operand(i)); |
| 728 | __ push(ip); |
| 729 | __ b(&done); |
| 730 | ASSERT(masm()->pc_offset() - start == table_entry_size_); |
| 731 | } |
| 732 | __ bind(&done); |
| 733 | } |
| 734 | |
| 735 | #undef __ |
| 736 | |
| 737 | } } // namespace v8::internal |