Home | History | Annotate | Download | only in mips
      1 
      2 // Copyright 2011 the V8 project authors. All rights reserved.
      3 // Redistribution and use in source and binary forms, with or without
      4 // modification, are permitted provided that the following conditions are
      5 // met:
      6 //
      7 //     * Redistributions of source code must retain the above copyright
      8 //       notice, this list of conditions and the following disclaimer.
      9 //     * Redistributions in binary form must reproduce the above
     10 //       copyright notice, this list of conditions and the following
     11 //       disclaimer in the documentation and/or other materials provided
     12 //       with the distribution.
     13 //     * Neither the name of Google Inc. nor the names of its
     14 //       contributors may be used to endorse or promote products derived
     15 //       from this software without specific prior written permission.
     16 //
     17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     18 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     19 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     20 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     21 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     22 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     23 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     24 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     25 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     26 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     28 
     29 #include "v8.h"
     30 
     31 #include "codegen.h"
     32 #include "deoptimizer.h"
     33 #include "full-codegen.h"
     34 #include "safepoint-table.h"
     35 
     36 namespace v8 {
     37 namespace internal {
     38 
     39 
     40 int Deoptimizer::patch_size() {
     41   const int kCallInstructionSizeInWords = 4;
     42   return kCallInstructionSizeInWords * Assembler::kInstrSize;
     43 }
     44 
     45 
     46 void Deoptimizer::PatchCodeForDeoptimization(Isolate* isolate, Code* code) {
     47   Address code_start_address = code->instruction_start();
     48   // Invalidate the relocation information, as it will become invalid by the
     49   // code patching below, and is not needed any more.
     50   code->InvalidateRelocation();
     51 
     52   // For each LLazyBailout instruction insert a call to the corresponding
     53   // deoptimization entry.
     54   DeoptimizationInputData* deopt_data =
     55       DeoptimizationInputData::cast(code->deoptimization_data());
     56 #ifdef DEBUG
     57   Address prev_call_address = NULL;
     58 #endif
     59   for (int i = 0; i < deopt_data->DeoptCount(); i++) {
     60     if (deopt_data->Pc(i)->value() == -1) continue;
     61     Address call_address = code_start_address + deopt_data->Pc(i)->value();
     62     Address deopt_entry = GetDeoptimizationEntry(isolate, i, LAZY);
     63     int call_size_in_bytes = MacroAssembler::CallSize(deopt_entry,
     64                                                       RelocInfo::NONE32);
     65     int call_size_in_words = call_size_in_bytes / Assembler::kInstrSize;
     66     ASSERT(call_size_in_bytes % Assembler::kInstrSize == 0);
     67     ASSERT(call_size_in_bytes <= patch_size());
     68     CodePatcher patcher(call_address, call_size_in_words);
     69     patcher.masm()->Call(deopt_entry, RelocInfo::NONE32);
     70     ASSERT(prev_call_address == NULL ||
     71            call_address >= prev_call_address + patch_size());
     72     ASSERT(call_address + patch_size() <= code->instruction_end());
     73 
     74 #ifdef DEBUG
     75     prev_call_address = call_address;
     76 #endif
     77   }
     78 }
     79 
     80 
     81 // This structure comes from FullCodeGenerator::EmitBackEdgeBookkeeping.
     82 // The back edge bookkeeping code matches the pattern:
     83 //
     84 // sltu at, sp, t0 / slt at, a3, zero_reg (in case of count based interrupts)
     85 // beq at, zero_reg, ok
     86 // lui t9, <interrupt stub address> upper
     87 // ori t9, <interrupt stub address> lower
     88 // jalr t9
     89 // nop
     90 // ok-label ----- pc_after points here
     91 //
     92 // We patch the code to the following form:
     93 //
     94 // addiu at, zero_reg, 1
     95 // beq at, zero_reg, ok  ;; Not changed
     96 // lui t9, <on-stack replacement address> upper
     97 // ori t9, <on-stack replacement address> lower
     98 // jalr t9  ;; Not changed
     99 // nop  ;; Not changed
    100 // ok-label ----- pc_after points here
    101 
    102 void Deoptimizer::PatchInterruptCodeAt(Code* unoptimized_code,
    103                                         Address pc_after,
    104                                         Code* interrupt_code,
    105                                         Code* replacement_code) {
    106   ASSERT(!InterruptCodeIsPatched(unoptimized_code,
    107                                  pc_after,
    108                                  interrupt_code,
    109                                  replacement_code));
    110   static const int kInstrSize = Assembler::kInstrSize;
    111   // Replace the sltu instruction with load-imm 1 to at, so beq is not taken.
    112   CodePatcher patcher(pc_after - 6 * kInstrSize, 1);
    113   patcher.masm()->addiu(at, zero_reg, 1);
    114   // Replace the stack check address in the load-immediate (lui/ori pair)
    115   // with the entry address of the replacement code.
    116   Assembler::set_target_address_at(pc_after - 4 * kInstrSize,
    117                                    replacement_code->entry());
    118 
    119   unoptimized_code->GetHeap()->incremental_marking()->RecordCodeTargetPatch(
    120       unoptimized_code, pc_after - 4 * kInstrSize, replacement_code);
    121 }
    122 
    123 
    124 void Deoptimizer::RevertInterruptCodeAt(Code* unoptimized_code,
    125                                         Address pc_after,
    126                                         Code* interrupt_code,
    127                                         Code* replacement_code) {
    128   ASSERT(InterruptCodeIsPatched(unoptimized_code,
    129                                  pc_after,
    130                                  interrupt_code,
    131                                  replacement_code));
    132   static const int kInstrSize = Assembler::kInstrSize;
    133   // Restore the sltu instruction so beq can be taken again.
    134   CodePatcher patcher(pc_after - 6 * kInstrSize, 1);
    135   patcher.masm()->slt(at, a3, zero_reg);
    136   // Restore the original call address.
    137   Assembler::set_target_address_at(pc_after - 4 * kInstrSize,
    138                                    interrupt_code->entry());
    139 
    140   interrupt_code->GetHeap()->incremental_marking()->RecordCodeTargetPatch(
    141       unoptimized_code, pc_after - 4 * kInstrSize, interrupt_code);
    142 }
    143 
    144 
    145 #ifdef DEBUG
    146 bool Deoptimizer::InterruptCodeIsPatched(Code* unoptimized_code,
    147                                          Address pc_after,
    148                                          Code* interrupt_code,
    149                                          Code* replacement_code) {
    150   static const int kInstrSize = Assembler::kInstrSize;
    151   ASSERT(Assembler::IsBeq(Assembler::instr_at(pc_after - 5 * kInstrSize)));
    152   if (Assembler::IsAddImmediate(
    153       Assembler::instr_at(pc_after - 6 * kInstrSize))) {
    154     ASSERT(reinterpret_cast<uint32_t>(
    155         Assembler::target_address_at(pc_after - 4 * kInstrSize)) ==
    156         reinterpret_cast<uint32_t>(replacement_code->entry()));
    157     return true;
    158   } else {
    159     ASSERT(reinterpret_cast<uint32_t>(
    160         Assembler::target_address_at(pc_after - 4 * kInstrSize)) ==
    161         reinterpret_cast<uint32_t>(interrupt_code->entry()));
    162     return false;
    163   }
    164 }
    165 #endif  // DEBUG
    166 
    167 
    168 static int LookupBailoutId(DeoptimizationInputData* data, BailoutId ast_id) {
    169   ByteArray* translations = data->TranslationByteArray();
    170   int length = data->DeoptCount();
    171   for (int i = 0; i < length; i++) {
    172     if (data->AstId(i) == ast_id) {
    173       TranslationIterator it(translations,  data->TranslationIndex(i)->value());
    174       int value = it.Next();
    175       ASSERT(Translation::BEGIN == static_cast<Translation::Opcode>(value));
    176       // Read the number of frames.
    177       value = it.Next();
    178       if (value == 1) return i;
    179     }
    180   }
    181   UNREACHABLE();
    182   return -1;
    183 }
    184 
    185 
    186 void Deoptimizer::DoComputeOsrOutputFrame() {
    187   DeoptimizationInputData* data = DeoptimizationInputData::cast(
    188       compiled_code_->deoptimization_data());
    189   unsigned ast_id = data->OsrAstId()->value();
    190 
    191   int bailout_id = LookupBailoutId(data, BailoutId(ast_id));
    192   unsigned translation_index = data->TranslationIndex(bailout_id)->value();
    193   ByteArray* translations = data->TranslationByteArray();
    194 
    195   TranslationIterator iterator(translations, translation_index);
    196   Translation::Opcode opcode =
    197       static_cast<Translation::Opcode>(iterator.Next());
    198   ASSERT(Translation::BEGIN == opcode);
    199   USE(opcode);
    200   int count = iterator.Next();
    201   iterator.Skip(1);  // Drop JS frame count.
    202   ASSERT(count == 1);
    203   USE(count);
    204 
    205   opcode = static_cast<Translation::Opcode>(iterator.Next());
    206   USE(opcode);
    207   ASSERT(Translation::JS_FRAME == opcode);
    208   unsigned node_id = iterator.Next();
    209   USE(node_id);
    210   ASSERT(node_id == ast_id);
    211   int closure_id = iterator.Next();
    212   USE(closure_id);
    213   ASSERT_EQ(Translation::kSelfLiteralId, closure_id);
    214   unsigned height = iterator.Next();
    215   unsigned height_in_bytes = height * kPointerSize;
    216   USE(height_in_bytes);
    217 
    218   unsigned fixed_size = ComputeFixedSize(function_);
    219   unsigned input_frame_size = input_->GetFrameSize();
    220   ASSERT(fixed_size + height_in_bytes == input_frame_size);
    221 
    222   unsigned stack_slot_size = compiled_code_->stack_slots() * kPointerSize;
    223   unsigned outgoing_height = data->ArgumentsStackHeight(bailout_id)->value();
    224   unsigned outgoing_size = outgoing_height * kPointerSize;
    225   unsigned output_frame_size = fixed_size + stack_slot_size + outgoing_size;
    226   ASSERT(outgoing_size == 0);  // OSR does not happen in the middle of a call.
    227 
    228   if (FLAG_trace_osr) {
    229     PrintF("[on-stack replacement: begin 0x%08" V8PRIxPTR " ",
    230            reinterpret_cast<intptr_t>(function_));
    231     PrintFunctionName();
    232     PrintF(" => node=%u, frame=%d->%d]\n",
    233            ast_id,
    234            input_frame_size,
    235            output_frame_size);
    236   }
    237 
    238   // There's only one output frame in the OSR case.
    239   output_count_ = 1;
    240   output_ = new FrameDescription*[1];
    241   output_[0] = new(output_frame_size) FrameDescription(
    242       output_frame_size, function_);
    243   output_[0]->SetFrameType(StackFrame::JAVA_SCRIPT);
    244 
    245   // Clear the incoming parameters in the optimized frame to avoid
    246   // confusing the garbage collector.
    247   unsigned output_offset = output_frame_size - kPointerSize;
    248   int parameter_count = function_->shared()->formal_parameter_count() + 1;
    249   for (int i = 0; i < parameter_count; ++i) {
    250     output_[0]->SetFrameSlot(output_offset, 0);
    251     output_offset -= kPointerSize;
    252   }
    253 
    254   // Translate the incoming parameters. This may overwrite some of the
    255   // incoming argument slots we've just cleared.
    256   int input_offset = input_frame_size - kPointerSize;
    257   bool ok = true;
    258   int limit = input_offset - (parameter_count * kPointerSize);
    259   while (ok && input_offset > limit) {
    260     ok = DoOsrTranslateCommand(&iterator, &input_offset);
    261   }
    262 
    263   // There are no translation commands for the caller's pc and fp, the
    264   // context, and the function.  Set them up explicitly.
    265   for (int i =  StandardFrameConstants::kCallerPCOffset;
    266        ok && i >=  StandardFrameConstants::kMarkerOffset;
    267        i -= kPointerSize) {
    268     uint32_t input_value = input_->GetFrameSlot(input_offset);
    269     if (FLAG_trace_osr) {
    270       const char* name = "UNKNOWN";
    271       switch (i) {
    272         case StandardFrameConstants::kCallerPCOffset:
    273           name = "caller's pc";
    274           break;
    275         case StandardFrameConstants::kCallerFPOffset:
    276           name = "fp";
    277           break;
    278         case StandardFrameConstants::kContextOffset:
    279           name = "context";
    280           break;
    281         case StandardFrameConstants::kMarkerOffset:
    282           name = "function";
    283           break;
    284       }
    285       PrintF("    [sp + %d] <- 0x%08x ; [sp + %d] (fixed part - %s)\n",
    286              output_offset,
    287              input_value,
    288              input_offset,
    289              name);
    290     }
    291 
    292     output_[0]->SetFrameSlot(output_offset, input_->GetFrameSlot(input_offset));
    293     input_offset -= kPointerSize;
    294     output_offset -= kPointerSize;
    295   }
    296 
    297   // Translate the rest of the frame.
    298   while (ok && input_offset >= 0) {
    299     ok = DoOsrTranslateCommand(&iterator, &input_offset);
    300   }
    301 
    302   // If translation of any command failed, continue using the input frame.
    303   if (!ok) {
    304     delete output_[0];
    305     output_[0] = input_;
    306     output_[0]->SetPc(reinterpret_cast<uint32_t>(from_));
    307   } else {
    308     // Set up the frame pointer and the context pointer.
    309     output_[0]->SetRegister(fp.code(), input_->GetRegister(fp.code()));
    310     output_[0]->SetRegister(cp.code(), input_->GetRegister(cp.code()));
    311 
    312     unsigned pc_offset = data->OsrPcOffset()->value();
    313     uint32_t pc = reinterpret_cast<uint32_t>(
    314         compiled_code_->entry() + pc_offset);
    315     output_[0]->SetPc(pc);
    316   }
    317   Code* continuation = isolate_->builtins()->builtin(Builtins::kNotifyOSR);
    318   output_[0]->SetContinuation(
    319       reinterpret_cast<uint32_t>(continuation->entry()));
    320 
    321   if (FLAG_trace_osr) {
    322     PrintF("[on-stack replacement translation %s: 0x%08" V8PRIxPTR " ",
    323            ok ? "finished" : "aborted",
    324            reinterpret_cast<intptr_t>(function_));
    325     PrintFunctionName();
    326     PrintF(" => pc=0x%0x]\n", output_[0]->GetPc());
    327   }
    328 }
    329 
    330 
    331 void Deoptimizer::FillInputFrame(Address tos, JavaScriptFrame* frame) {
    332   // Set the register values. The values are not important as there are no
    333   // callee saved registers in JavaScript frames, so all registers are
    334   // spilled. Registers fp and sp are set to the correct values though.
    335 
    336   for (int i = 0; i < Register::kNumRegisters; i++) {
    337     input_->SetRegister(i, i * 4);
    338   }
    339   input_->SetRegister(sp.code(), reinterpret_cast<intptr_t>(frame->sp()));
    340   input_->SetRegister(fp.code(), reinterpret_cast<intptr_t>(frame->fp()));
    341   for (int i = 0; i < DoubleRegister::NumAllocatableRegisters(); i++) {
    342     input_->SetDoubleRegister(i, 0.0);
    343   }
    344 
    345   // Fill the frame content from the actual data on the frame.
    346   for (unsigned i = 0; i < input_->GetFrameSize(); i += kPointerSize) {
    347     input_->SetFrameSlot(i, Memory::uint32_at(tos + i));
    348   }
    349 }
    350 
    351 
    352 void Deoptimizer::SetPlatformCompiledStubRegisters(
    353     FrameDescription* output_frame, CodeStubInterfaceDescriptor* descriptor) {
    354   ApiFunction function(descriptor->deoptimization_handler_);
    355   ExternalReference xref(&function, ExternalReference::BUILTIN_CALL, isolate_);
    356   intptr_t handler = reinterpret_cast<intptr_t>(xref.address());
    357   int params = descriptor->register_param_count_;
    358   if (descriptor->stack_parameter_count_ != NULL) {
    359     params++;
    360   }
    361   output_frame->SetRegister(s0.code(), params);
    362   output_frame->SetRegister(s1.code(), (params - 1) * kPointerSize);
    363   output_frame->SetRegister(s2.code(), handler);
    364 }
    365 
    366 
    367 void Deoptimizer::CopyDoubleRegisters(FrameDescription* output_frame) {
    368   for (int i = 0; i < DoubleRegister::kMaxNumRegisters; ++i) {
    369     double double_value = input_->GetDoubleRegister(i);
    370     output_frame->SetDoubleRegister(i, double_value);
    371   }
    372 }
    373 
    374 
    375 bool Deoptimizer::HasAlignmentPadding(JSFunction* function) {
    376   // There is no dynamic alignment padding on MIPS in the input frame.
    377   return false;
    378 }
    379 
    380 
    381 #define __ masm()->
    382 
    383 
    384 // This code tries to be close to ia32 code so that any changes can be
    385 // easily ported.
    386 void Deoptimizer::EntryGenerator::Generate() {
    387   GeneratePrologue();
    388 
    389   // Unlike on ARM we don't save all the registers, just the useful ones.
    390   // For the rest, there are gaps on the stack, so the offsets remain the same.
    391   const int kNumberOfRegisters = Register::kNumRegisters;
    392 
    393   RegList restored_regs = kJSCallerSaved | kCalleeSaved;
    394   RegList saved_regs = restored_regs | sp.bit() | ra.bit();
    395 
    396   const int kDoubleRegsSize =
    397       kDoubleSize * FPURegister::kMaxNumAllocatableRegisters;
    398 
    399   // Save all FPU registers before messing with them.
    400   __ Subu(sp, sp, Operand(kDoubleRegsSize));
    401   for (int i = 0; i < FPURegister::kMaxNumAllocatableRegisters; ++i) {
    402     FPURegister fpu_reg = FPURegister::FromAllocationIndex(i);
    403     int offset = i * kDoubleSize;
    404     __ sdc1(fpu_reg, MemOperand(sp, offset));
    405   }
    406 
    407   // Push saved_regs (needed to populate FrameDescription::registers_).
    408   // Leave gaps for other registers.
    409   __ Subu(sp, sp, kNumberOfRegisters * kPointerSize);
    410   for (int16_t i = kNumberOfRegisters - 1; i >= 0; i--) {
    411     if ((saved_regs & (1 << i)) != 0) {
    412       __ sw(ToRegister(i), MemOperand(sp, kPointerSize * i));
    413     }
    414   }
    415 
    416   const int kSavedRegistersAreaSize =
    417       (kNumberOfRegisters * kPointerSize) + kDoubleRegsSize;
    418 
    419   // Get the bailout id from the stack.
    420   __ lw(a2, MemOperand(sp, kSavedRegistersAreaSize));
    421 
    422   // Get the address of the location in the code object (a3) (return
    423   // address for lazy deoptimization) and compute the fp-to-sp delta in
    424   // register t0.
    425   __ mov(a3, ra);
    426   // Correct one word for bailout id.
    427   __ Addu(t0, sp, Operand(kSavedRegistersAreaSize + (1 * kPointerSize)));
    428 
    429   __ Subu(t0, fp, t0);
    430 
    431   // Allocate a new deoptimizer object.
    432   // Pass four arguments in a0 to a3 and fifth & sixth arguments on stack.
    433   __ PrepareCallCFunction(6, t1);
    434   __ lw(a0, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
    435   __ li(a1, Operand(type()));  // bailout type,
    436   // a2: bailout id already loaded.
    437   // a3: code address or 0 already loaded.
    438   __ sw(t0, CFunctionArgumentOperand(5));  // Fp-to-sp delta.
    439   __ li(t1, Operand(ExternalReference::isolate_address(isolate())));
    440   __ sw(t1, CFunctionArgumentOperand(6));  // Isolate.
    441   // Call Deoptimizer::New().
    442   {
    443     AllowExternalCallThatCantCauseGC scope(masm());
    444     __ CallCFunction(ExternalReference::new_deoptimizer_function(isolate()), 6);
    445   }
    446 
    447   // Preserve "deoptimizer" object in register v0 and get the input
    448   // frame descriptor pointer to a1 (deoptimizer->input_);
    449   // Move deopt-obj to a0 for call to Deoptimizer::ComputeOutputFrames() below.
    450   __ mov(a0, v0);
    451   __ lw(a1, MemOperand(v0, Deoptimizer::input_offset()));
    452 
    453   // Copy core registers into FrameDescription::registers_[kNumRegisters].
    454   ASSERT(Register::kNumRegisters == kNumberOfRegisters);
    455   for (int i = 0; i < kNumberOfRegisters; i++) {
    456     int offset = (i * kPointerSize) + FrameDescription::registers_offset();
    457     if ((saved_regs & (1 << i)) != 0) {
    458       __ lw(a2, MemOperand(sp, i * kPointerSize));
    459       __ sw(a2, MemOperand(a1, offset));
    460     } else if (FLAG_debug_code) {
    461       __ li(a2, kDebugZapValue);
    462       __ sw(a2, MemOperand(a1, offset));
    463     }
    464   }
    465 
    466   int double_regs_offset = FrameDescription::double_registers_offset();
    467   // Copy FPU registers to
    468   // double_registers_[DoubleRegister::kNumAllocatableRegisters]
    469   for (int i = 0; i < FPURegister::NumAllocatableRegisters(); ++i) {
    470     int dst_offset = i * kDoubleSize + double_regs_offset;
    471     int src_offset = i * kDoubleSize + kNumberOfRegisters * kPointerSize;
    472     __ ldc1(f0, MemOperand(sp, src_offset));
    473     __ sdc1(f0, MemOperand(a1, dst_offset));
    474   }
    475 
    476   // Remove the bailout id and the saved registers from the stack.
    477   __ Addu(sp, sp, Operand(kSavedRegistersAreaSize + (1 * kPointerSize)));
    478 
    479   // Compute a pointer to the unwinding limit in register a2; that is
    480   // the first stack slot not part of the input frame.
    481   __ lw(a2, MemOperand(a1, FrameDescription::frame_size_offset()));
    482   __ Addu(a2, a2, sp);
    483 
    484   // Unwind the stack down to - but not including - the unwinding
    485   // limit and copy the contents of the activation frame to the input
    486   // frame description.
    487   __ Addu(a3, a1, Operand(FrameDescription::frame_content_offset()));
    488   Label pop_loop;
    489   Label pop_loop_header;
    490   __ Branch(&pop_loop_header);
    491   __ bind(&pop_loop);
    492   __ pop(t0);
    493   __ sw(t0, MemOperand(a3, 0));
    494   __ addiu(a3, a3, sizeof(uint32_t));
    495   __ bind(&pop_loop_header);
    496   __ Branch(&pop_loop, ne, a2, Operand(sp));
    497 
    498   // Compute the output frame in the deoptimizer.
    499   __ push(a0);  // Preserve deoptimizer object across call.
    500   // a0: deoptimizer object; a1: scratch.
    501   __ PrepareCallCFunction(1, a1);
    502   // Call Deoptimizer::ComputeOutputFrames().
    503   {
    504     AllowExternalCallThatCantCauseGC scope(masm());
    505     __ CallCFunction(
    506         ExternalReference::compute_output_frames_function(isolate()), 1);
    507   }
    508   __ pop(a0);  // Restore deoptimizer object (class Deoptimizer).
    509 
    510   // Replace the current (input) frame with the output frames.
    511   Label outer_push_loop, inner_push_loop,
    512       outer_loop_header, inner_loop_header;
    513   // Outer loop state: t0 = current "FrameDescription** output_",
    514   // a1 = one past the last FrameDescription**.
    515   __ lw(a1, MemOperand(a0, Deoptimizer::output_count_offset()));
    516   __ lw(t0, MemOperand(a0, Deoptimizer::output_offset()));  // t0 is output_.
    517   __ sll(a1, a1, kPointerSizeLog2);  // Count to offset.
    518   __ addu(a1, t0, a1);  // a1 = one past the last FrameDescription**.
    519   __ jmp(&outer_loop_header);
    520   __ bind(&outer_push_loop);
    521   // Inner loop state: a2 = current FrameDescription*, a3 = loop index.
    522   __ lw(a2, MemOperand(t0, 0));  // output_[ix]
    523   __ lw(a3, MemOperand(a2, FrameDescription::frame_size_offset()));
    524   __ jmp(&inner_loop_header);
    525   __ bind(&inner_push_loop);
    526   __ Subu(a3, a3, Operand(sizeof(uint32_t)));
    527   __ Addu(t2, a2, Operand(a3));
    528   __ lw(t3, MemOperand(t2, FrameDescription::frame_content_offset()));
    529   __ push(t3);
    530   __ bind(&inner_loop_header);
    531   __ Branch(&inner_push_loop, ne, a3, Operand(zero_reg));
    532 
    533   __ Addu(t0, t0, Operand(kPointerSize));
    534   __ bind(&outer_loop_header);
    535   __ Branch(&outer_push_loop, lt, t0, Operand(a1));
    536 
    537   __ lw(a1, MemOperand(a0, Deoptimizer::input_offset()));
    538   for (int i = 0; i < FPURegister::kMaxNumAllocatableRegisters; ++i) {
    539     const FPURegister fpu_reg = FPURegister::FromAllocationIndex(i);
    540     int src_offset = i * kDoubleSize + double_regs_offset;
    541     __ ldc1(fpu_reg, MemOperand(a1, src_offset));
    542   }
    543 
    544   // Push state, pc, and continuation from the last output frame.
    545   if (type() != OSR) {
    546     __ lw(t2, MemOperand(a2, FrameDescription::state_offset()));
    547     __ push(t2);
    548   }
    549 
    550   __ lw(t2, MemOperand(a2, FrameDescription::pc_offset()));
    551   __ push(t2);
    552   __ lw(t2, MemOperand(a2, FrameDescription::continuation_offset()));
    553   __ push(t2);
    554 
    555 
    556   // Technically restoring 'at' should work unless zero_reg is also restored
    557   // but it's safer to check for this.
    558   ASSERT(!(at.bit() & restored_regs));
    559   // Restore the registers from the last output frame.
    560   __ mov(at, a2);
    561   for (int i = kNumberOfRegisters - 1; i >= 0; i--) {
    562     int offset = (i * kPointerSize) + FrameDescription::registers_offset();
    563     if ((restored_regs & (1 << i)) != 0) {
    564       __ lw(ToRegister(i), MemOperand(at, offset));
    565     }
    566   }
    567 
    568   __ InitializeRootRegister();
    569 
    570   __ pop(at);  // Get continuation, leave pc on stack.
    571   __ pop(ra);
    572   __ Jump(at);
    573   __ stop("Unreachable.");
    574 }
    575 
    576 
    577 // Maximum size of a table entry generated below.
    578 const int Deoptimizer::table_entry_size_ = 7 * Assembler::kInstrSize;
    579 
    580 void Deoptimizer::TableEntryGenerator::GeneratePrologue() {
    581   Assembler::BlockTrampolinePoolScope block_trampoline_pool(masm());
    582 
    583   // Create a sequence of deoptimization entries.
    584   // Note that registers are still live when jumping to an entry.
    585   Label table_start;
    586   __ bind(&table_start);
    587   for (int i = 0; i < count(); i++) {
    588     Label start;
    589     __ bind(&start);
    590     __ addiu(sp, sp, -1 * kPointerSize);
    591     // Jump over the remaining deopt entries (including this one).
    592     // This code is always reached by calling Jump, which puts the target (label
    593     // start) into t9.
    594     const int remaining_entries = (count() - i) * table_entry_size_;
    595     __ Addu(t9, t9, remaining_entries);
    596     // 'at' was clobbered so we can only load the current entry value here.
    597     __ li(at, i);
    598     __ jr(t9);  // Expose delay slot.
    599     __ sw(at, MemOperand(sp, 0 * kPointerSize));  // In the delay slot.
    600 
    601     // Pad the rest of the code.
    602     while (table_entry_size_ > (masm()->SizeOfCodeGeneratedSince(&start))) {
    603       __ nop();
    604     }
    605 
    606     ASSERT_EQ(table_entry_size_, masm()->SizeOfCodeGeneratedSince(&start));
    607   }
    608 
    609   ASSERT_EQ(masm()->SizeOfCodeGeneratedSince(&table_start),
    610       count() * table_entry_size_);
    611 }
    612 
    613 
    614 void FrameDescription::SetCallerPc(unsigned offset, intptr_t value) {
    615   SetFrameSlot(offset, value);
    616 }
    617 
    618 
    619 void FrameDescription::SetCallerFp(unsigned offset, intptr_t value) {
    620   SetFrameSlot(offset, value);
    621 }
    622 
    623 
    624 #undef __
    625 
    626 
    627 } }  // namespace v8::internal
    628