Home | History | Annotate | Download | only in ia32
      1 // Copyright 2012 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 #if defined(V8_TARGET_ARCH_IA32)
     31 
     32 #include "codegen.h"
     33 #include "deoptimizer.h"
     34 #include "full-codegen.h"
     35 #include "safepoint-table.h"
     36 
     37 namespace v8 {
     38 namespace internal {
     39 
     40 const int Deoptimizer::table_entry_size_ = 10;
     41 
     42 
     43 int Deoptimizer::patch_size() {
     44   return Assembler::kCallInstructionLength;
     45 }
     46 
     47 
     48 void Deoptimizer::EnsureRelocSpaceForLazyDeoptimization(Handle<Code> code) {
     49   Isolate* isolate = code->GetIsolate();
     50   HandleScope scope(isolate);
     51 
     52   // Compute the size of relocation information needed for the code
     53   // patching in Deoptimizer::DeoptimizeFunction.
     54   int min_reloc_size = 0;
     55   int prev_pc_offset = 0;
     56   DeoptimizationInputData* deopt_data =
     57       DeoptimizationInputData::cast(code->deoptimization_data());
     58   for (int i = 0; i < deopt_data->DeoptCount(); i++) {
     59     int pc_offset = deopt_data->Pc(i)->value();
     60     if (pc_offset == -1) continue;
     61     ASSERT_GE(pc_offset, prev_pc_offset);
     62     int pc_delta = pc_offset - prev_pc_offset;
     63     // We use RUNTIME_ENTRY reloc info which has a size of 2 bytes
     64     // if encodable with small pc delta encoding and up to 6 bytes
     65     // otherwise.
     66     if (pc_delta <= RelocInfo::kMaxSmallPCDelta) {
     67       min_reloc_size += 2;
     68     } else {
     69       min_reloc_size += 6;
     70     }
     71     prev_pc_offset = pc_offset;
     72   }
     73 
     74   // If the relocation information is not big enough we create a new
     75   // relocation info object that is padded with comments to make it
     76   // big enough for lazy doptimization.
     77   int reloc_length = code->relocation_info()->length();
     78   if (min_reloc_size > reloc_length) {
     79     int comment_reloc_size = RelocInfo::kMinRelocCommentSize;
     80     // Padding needed.
     81     int min_padding = min_reloc_size - reloc_length;
     82     // Number of comments needed to take up at least that much space.
     83     int additional_comments =
     84         (min_padding + comment_reloc_size - 1) / comment_reloc_size;
     85     // Actual padding size.
     86     int padding = additional_comments * comment_reloc_size;
     87     // Allocate new relocation info and copy old relocation to the end
     88     // of the new relocation info array because relocation info is
     89     // written and read backwards.
     90     Factory* factory = isolate->factory();
     91     Handle<ByteArray> new_reloc =
     92         factory->NewByteArray(reloc_length + padding, TENURED);
     93     memcpy(new_reloc->GetDataStartAddress() + padding,
     94            code->relocation_info()->GetDataStartAddress(),
     95            reloc_length);
     96     // Create a relocation writer to write the comments in the padding
     97     // space. Use position 0 for everything to ensure short encoding.
     98     RelocInfoWriter reloc_info_writer(
     99         new_reloc->GetDataStartAddress() + padding, 0);
    100     intptr_t comment_string
    101         = reinterpret_cast<intptr_t>(RelocInfo::kFillerCommentString);
    102     RelocInfo rinfo(0, RelocInfo::COMMENT, comment_string, NULL);
    103     for (int i = 0; i < additional_comments; ++i) {
    104 #ifdef DEBUG
    105       byte* pos_before = reloc_info_writer.pos();
    106 #endif
    107       reloc_info_writer.Write(&rinfo);
    108       ASSERT(RelocInfo::kMinRelocCommentSize ==
    109              pos_before - reloc_info_writer.pos());
    110     }
    111     // Replace relocation information on the code object.
    112     code->set_relocation_info(*new_reloc);
    113   }
    114 }
    115 
    116 
    117 void Deoptimizer::DeoptimizeFunction(JSFunction* function) {
    118   if (!function->IsOptimized()) return;
    119 
    120   Isolate* isolate = function->GetIsolate();
    121   HandleScope scope(isolate);
    122   AssertNoAllocation no_allocation;
    123 
    124   // Get the optimized code.
    125   Code* code = function->code();
    126   Address code_start_address = code->instruction_start();
    127 
    128   // We will overwrite the code's relocation info in-place. Relocation info
    129   // is written backward. The relocation info is the payload of a byte
    130   // array.  Later on we will slide this to the start of the byte array and
    131   // create a filler object in the remaining space.
    132   ByteArray* reloc_info = code->relocation_info();
    133   Address reloc_end_address = reloc_info->address() + reloc_info->Size();
    134   RelocInfoWriter reloc_info_writer(reloc_end_address, code_start_address);
    135 
    136   // For each LLazyBailout instruction insert a call to the corresponding
    137   // deoptimization entry.
    138 
    139   // Since the call is a relative encoding, write new
    140   // reloc info.  We do not need any of the existing reloc info because the
    141   // existing code will not be used again (we zap it in debug builds).
    142   //
    143   // Emit call to lazy deoptimization at all lazy deopt points.
    144   DeoptimizationInputData* deopt_data =
    145       DeoptimizationInputData::cast(code->deoptimization_data());
    146 #ifdef DEBUG
    147   Address prev_call_address = NULL;
    148 #endif
    149   for (int i = 0; i < deopt_data->DeoptCount(); i++) {
    150     if (deopt_data->Pc(i)->value() == -1) continue;
    151     // Patch lazy deoptimization entry.
    152     Address call_address = code_start_address + deopt_data->Pc(i)->value();
    153     CodePatcher patcher(call_address, patch_size());
    154     Address deopt_entry = GetDeoptimizationEntry(i, LAZY);
    155     patcher.masm()->call(deopt_entry, RelocInfo::NONE);
    156     // We use RUNTIME_ENTRY for deoptimization bailouts.
    157     RelocInfo rinfo(call_address + 1,  // 1 after the call opcode.
    158                     RelocInfo::RUNTIME_ENTRY,
    159                     reinterpret_cast<intptr_t>(deopt_entry),
    160                     NULL);
    161     reloc_info_writer.Write(&rinfo);
    162     ASSERT_GE(reloc_info_writer.pos(),
    163               reloc_info->address() + ByteArray::kHeaderSize);
    164     ASSERT(prev_call_address == NULL ||
    165            call_address >= prev_call_address + patch_size());
    166     ASSERT(call_address + patch_size() <= code->instruction_end());
    167 #ifdef DEBUG
    168     prev_call_address = call_address;
    169 #endif
    170   }
    171 
    172   // Move the relocation info to the beginning of the byte array.
    173   int new_reloc_size = reloc_end_address - reloc_info_writer.pos();
    174   memmove(code->relocation_start(), reloc_info_writer.pos(), new_reloc_size);
    175 
    176   // The relocation info is in place, update the size.
    177   reloc_info->set_length(new_reloc_size);
    178 
    179   // Handle the junk part after the new relocation info. We will create
    180   // a non-live object in the extra space at the end of the former reloc info.
    181   Address junk_address = reloc_info->address() + reloc_info->Size();
    182   ASSERT(junk_address <= reloc_end_address);
    183   isolate->heap()->CreateFillerObjectAt(junk_address,
    184                                         reloc_end_address - junk_address);
    185 
    186   // Add the deoptimizing code to the list.
    187   DeoptimizingCodeListNode* node = new DeoptimizingCodeListNode(code);
    188   DeoptimizerData* data = isolate->deoptimizer_data();
    189   node->set_next(data->deoptimizing_code_list_);
    190   data->deoptimizing_code_list_ = node;
    191 
    192   // We might be in the middle of incremental marking with compaction.
    193   // Tell collector to treat this code object in a special way and
    194   // ignore all slots that might have been recorded on it.
    195   isolate->heap()->mark_compact_collector()->InvalidateCode(code);
    196 
    197   // Set the code for the function to non-optimized version.
    198   function->ReplaceCode(function->shared()->code());
    199 
    200   if (FLAG_trace_deopt) {
    201     PrintF("[forced deoptimization: ");
    202     function->PrintName();
    203     PrintF(" / %x]\n", reinterpret_cast<uint32_t>(function));
    204   }
    205 }
    206 
    207 
    208 static const byte kJnsInstruction = 0x79;
    209 static const byte kJnsOffset = 0x13;
    210 static const byte kJaeInstruction = 0x73;
    211 static const byte kJaeOffset = 0x07;
    212 static const byte kCallInstruction = 0xe8;
    213 static const byte kNopByteOne = 0x66;
    214 static const byte kNopByteTwo = 0x90;
    215 
    216 
    217 void Deoptimizer::PatchStackCheckCodeAt(Code* unoptimized_code,
    218                                         Address pc_after,
    219                                         Code* check_code,
    220                                         Code* replacement_code) {
    221   Address call_target_address = pc_after - kIntSize;
    222   ASSERT_EQ(check_code->entry(),
    223             Assembler::target_address_at(call_target_address));
    224   // The stack check code matches the pattern:
    225   //
    226   //     cmp esp, <limit>
    227   //     jae ok
    228   //     call <stack guard>
    229   //     test eax, <loop nesting depth>
    230   // ok: ...
    231   //
    232   // We will patch away the branch so the code is:
    233   //
    234   //     cmp esp, <limit>  ;; Not changed
    235   //     nop
    236   //     nop
    237   //     call <on-stack replacment>
    238   //     test eax, <loop nesting depth>
    239   // ok:
    240 
    241   if (FLAG_count_based_interrupts) {
    242     ASSERT_EQ(*(call_target_address - 3), kJnsInstruction);
    243     ASSERT_EQ(*(call_target_address - 2), kJnsOffset);
    244   } else {
    245     ASSERT_EQ(*(call_target_address - 3), kJaeInstruction);
    246     ASSERT_EQ(*(call_target_address - 2), kJaeOffset);
    247   }
    248   ASSERT_EQ(*(call_target_address - 1), kCallInstruction);
    249   *(call_target_address - 3) = kNopByteOne;
    250   *(call_target_address - 2) = kNopByteTwo;
    251   Assembler::set_target_address_at(call_target_address,
    252                                    replacement_code->entry());
    253 
    254   unoptimized_code->GetHeap()->incremental_marking()->RecordCodeTargetPatch(
    255       unoptimized_code, call_target_address, replacement_code);
    256 }
    257 
    258 
    259 void Deoptimizer::RevertStackCheckCodeAt(Code* unoptimized_code,
    260                                          Address pc_after,
    261                                          Code* check_code,
    262                                          Code* replacement_code) {
    263   Address call_target_address = pc_after - kIntSize;
    264   ASSERT_EQ(replacement_code->entry(),
    265             Assembler::target_address_at(call_target_address));
    266 
    267   // Replace the nops from patching (Deoptimizer::PatchStackCheckCode) to
    268   // restore the conditional branch.
    269   ASSERT_EQ(*(call_target_address - 3), kNopByteOne);
    270   ASSERT_EQ(*(call_target_address - 2), kNopByteTwo);
    271   ASSERT_EQ(*(call_target_address - 1), kCallInstruction);
    272   if (FLAG_count_based_interrupts) {
    273     *(call_target_address - 3) = kJnsInstruction;
    274     *(call_target_address - 2) = kJnsOffset;
    275   } else {
    276     *(call_target_address - 3) = kJaeInstruction;
    277     *(call_target_address - 2) = kJaeOffset;
    278   }
    279   Assembler::set_target_address_at(call_target_address,
    280                                    check_code->entry());
    281 
    282   check_code->GetHeap()->incremental_marking()->RecordCodeTargetPatch(
    283       unoptimized_code, call_target_address, check_code);
    284 }
    285 
    286 
    287 static int LookupBailoutId(DeoptimizationInputData* data, unsigned ast_id) {
    288   ByteArray* translations = data->TranslationByteArray();
    289   int length = data->DeoptCount();
    290   for (int i = 0; i < length; i++) {
    291     if (static_cast<unsigned>(data->AstId(i)->value()) == ast_id) {
    292       TranslationIterator it(translations,  data->TranslationIndex(i)->value());
    293       int value = it.Next();
    294       ASSERT(Translation::BEGIN == static_cast<Translation::Opcode>(value));
    295       // Read the number of frames.
    296       value = it.Next();
    297       if (value == 1) return i;
    298     }
    299   }
    300   UNREACHABLE();
    301   return -1;
    302 }
    303 
    304 
    305 void Deoptimizer::DoComputeOsrOutputFrame() {
    306   DeoptimizationInputData* data = DeoptimizationInputData::cast(
    307       optimized_code_->deoptimization_data());
    308   unsigned ast_id = data->OsrAstId()->value();
    309   // TODO(kasperl): This should not be the bailout_id_. It should be
    310   // the ast id. Confusing.
    311   ASSERT(bailout_id_ == ast_id);
    312 
    313   int bailout_id = LookupBailoutId(data, ast_id);
    314   unsigned translation_index = data->TranslationIndex(bailout_id)->value();
    315   ByteArray* translations = data->TranslationByteArray();
    316 
    317   TranslationIterator iterator(translations, translation_index);
    318   Translation::Opcode opcode =
    319       static_cast<Translation::Opcode>(iterator.Next());
    320   ASSERT(Translation::BEGIN == opcode);
    321   USE(opcode);
    322   int count = iterator.Next();
    323   iterator.Next();  // Drop JS frames count.
    324   ASSERT(count == 1);
    325   USE(count);
    326 
    327   opcode = static_cast<Translation::Opcode>(iterator.Next());
    328   USE(opcode);
    329   ASSERT(Translation::JS_FRAME == opcode);
    330   unsigned node_id = iterator.Next();
    331   USE(node_id);
    332   ASSERT(node_id == ast_id);
    333   JSFunction* function = JSFunction::cast(ComputeLiteral(iterator.Next()));
    334   USE(function);
    335   ASSERT(function == function_);
    336   unsigned height = iterator.Next();
    337   unsigned height_in_bytes = height * kPointerSize;
    338   USE(height_in_bytes);
    339 
    340   unsigned fixed_size = ComputeFixedSize(function_);
    341   unsigned input_frame_size = input_->GetFrameSize();
    342   ASSERT(fixed_size + height_in_bytes == input_frame_size);
    343 
    344   unsigned stack_slot_size = optimized_code_->stack_slots() * kPointerSize;
    345   unsigned outgoing_height = data->ArgumentsStackHeight(bailout_id)->value();
    346   unsigned outgoing_size = outgoing_height * kPointerSize;
    347   unsigned output_frame_size = fixed_size + stack_slot_size + outgoing_size;
    348   ASSERT(outgoing_size == 0);  // OSR does not happen in the middle of a call.
    349 
    350   if (FLAG_trace_osr) {
    351     PrintF("[on-stack replacement: begin 0x%08" V8PRIxPTR " ",
    352            reinterpret_cast<intptr_t>(function_));
    353     function_->PrintName();
    354     PrintF(" => node=%u, frame=%d->%d]\n",
    355            ast_id,
    356            input_frame_size,
    357            output_frame_size);
    358   }
    359 
    360   // There's only one output frame in the OSR case.
    361   output_count_ = 1;
    362   output_ = new FrameDescription*[1];
    363   output_[0] = new(output_frame_size) FrameDescription(
    364       output_frame_size, function_);
    365   output_[0]->SetFrameType(StackFrame::JAVA_SCRIPT);
    366 
    367   // Clear the incoming parameters in the optimized frame to avoid
    368   // confusing the garbage collector.
    369   unsigned output_offset = output_frame_size - kPointerSize;
    370   int parameter_count = function_->shared()->formal_parameter_count() + 1;
    371   for (int i = 0; i < parameter_count; ++i) {
    372     output_[0]->SetFrameSlot(output_offset, 0);
    373     output_offset -= kPointerSize;
    374   }
    375 
    376   // Translate the incoming parameters. This may overwrite some of the
    377   // incoming argument slots we've just cleared.
    378   int input_offset = input_frame_size - kPointerSize;
    379   bool ok = true;
    380   int limit = input_offset - (parameter_count * kPointerSize);
    381   while (ok && input_offset > limit) {
    382     ok = DoOsrTranslateCommand(&iterator, &input_offset);
    383   }
    384 
    385   // There are no translation commands for the caller's pc and fp, the
    386   // context, and the function.  Set them up explicitly.
    387   for (int i =  StandardFrameConstants::kCallerPCOffset;
    388        ok && i >=  StandardFrameConstants::kMarkerOffset;
    389        i -= kPointerSize) {
    390     uint32_t input_value = input_->GetFrameSlot(input_offset);
    391     if (FLAG_trace_osr) {
    392       const char* name = "UNKNOWN";
    393       switch (i) {
    394         case StandardFrameConstants::kCallerPCOffset:
    395           name = "caller's pc";
    396           break;
    397         case StandardFrameConstants::kCallerFPOffset:
    398           name = "fp";
    399           break;
    400         case StandardFrameConstants::kContextOffset:
    401           name = "context";
    402           break;
    403         case StandardFrameConstants::kMarkerOffset:
    404           name = "function";
    405           break;
    406       }
    407       PrintF("    [esp + %d] <- 0x%08x ; [esp + %d] (fixed part - %s)\n",
    408              output_offset,
    409              input_value,
    410              input_offset,
    411              name);
    412     }
    413     output_[0]->SetFrameSlot(output_offset, input_->GetFrameSlot(input_offset));
    414     input_offset -= kPointerSize;
    415     output_offset -= kPointerSize;
    416   }
    417 
    418   // Translate the rest of the frame.
    419   while (ok && input_offset >= 0) {
    420     ok = DoOsrTranslateCommand(&iterator, &input_offset);
    421   }
    422 
    423   // If translation of any command failed, continue using the input frame.
    424   if (!ok) {
    425     delete output_[0];
    426     output_[0] = input_;
    427     output_[0]->SetPc(reinterpret_cast<uint32_t>(from_));
    428   } else {
    429     // Set up the frame pointer and the context pointer.
    430     output_[0]->SetRegister(ebp.code(), input_->GetRegister(ebp.code()));
    431     output_[0]->SetRegister(esi.code(), input_->GetRegister(esi.code()));
    432 
    433     unsigned pc_offset = data->OsrPcOffset()->value();
    434     uint32_t pc = reinterpret_cast<uint32_t>(
    435         optimized_code_->entry() + pc_offset);
    436     output_[0]->SetPc(pc);
    437   }
    438   Code* continuation =
    439       function->GetIsolate()->builtins()->builtin(Builtins::kNotifyOSR);
    440   output_[0]->SetContinuation(
    441       reinterpret_cast<uint32_t>(continuation->entry()));
    442 
    443   if (FLAG_trace_osr) {
    444     PrintF("[on-stack replacement translation %s: 0x%08" V8PRIxPTR " ",
    445            ok ? "finished" : "aborted",
    446            reinterpret_cast<intptr_t>(function));
    447     function->PrintName();
    448     PrintF(" => pc=0x%0x]\n", output_[0]->GetPc());
    449   }
    450 }
    451 
    452 
    453 void Deoptimizer::DoComputeArgumentsAdaptorFrame(TranslationIterator* iterator,
    454                                                  int frame_index) {
    455   JSFunction* function = JSFunction::cast(ComputeLiteral(iterator->Next()));
    456   unsigned height = iterator->Next();
    457   unsigned height_in_bytes = height * kPointerSize;
    458   if (FLAG_trace_deopt) {
    459     PrintF("  translating arguments adaptor => height=%d\n", height_in_bytes);
    460   }
    461 
    462   unsigned fixed_frame_size = ArgumentsAdaptorFrameConstants::kFrameSize;
    463   unsigned output_frame_size = height_in_bytes + fixed_frame_size;
    464 
    465   // Allocate and store the output frame description.
    466   FrameDescription* output_frame =
    467       new(output_frame_size) FrameDescription(output_frame_size, function);
    468   output_frame->SetFrameType(StackFrame::ARGUMENTS_ADAPTOR);
    469 
    470   // Arguments adaptor can not be topmost or bottommost.
    471   ASSERT(frame_index > 0 && frame_index < output_count_ - 1);
    472   ASSERT(output_[frame_index] == NULL);
    473   output_[frame_index] = output_frame;
    474 
    475   // The top address of the frame is computed from the previous
    476   // frame's top and this frame's size.
    477   uint32_t top_address;
    478   top_address = output_[frame_index - 1]->GetTop() - output_frame_size;
    479   output_frame->SetTop(top_address);
    480 
    481   // Compute the incoming parameter translation.
    482   int parameter_count = height;
    483   unsigned output_offset = output_frame_size;
    484   for (int i = 0; i < parameter_count; ++i) {
    485     output_offset -= kPointerSize;
    486     DoTranslateCommand(iterator, frame_index, output_offset);
    487   }
    488 
    489   // Read caller's PC from the previous frame.
    490   output_offset -= kPointerSize;
    491   intptr_t callers_pc = output_[frame_index - 1]->GetPc();
    492   output_frame->SetFrameSlot(output_offset, callers_pc);
    493   if (FLAG_trace_deopt) {
    494     PrintF("    0x%08x: [top + %d] <- 0x%08x ; caller's pc\n",
    495            top_address + output_offset, output_offset, callers_pc);
    496   }
    497 
    498   // Read caller's FP from the previous frame, and set this frame's FP.
    499   output_offset -= kPointerSize;
    500   intptr_t value = output_[frame_index - 1]->GetFp();
    501   output_frame->SetFrameSlot(output_offset, value);
    502   intptr_t fp_value = top_address + output_offset;
    503   output_frame->SetFp(fp_value);
    504   if (FLAG_trace_deopt) {
    505     PrintF("    0x%08x: [top + %d] <- 0x%08x ; caller's fp\n",
    506            fp_value, output_offset, value);
    507   }
    508 
    509   // A marker value is used in place of the context.
    510   output_offset -= kPointerSize;
    511   intptr_t context = reinterpret_cast<intptr_t>(
    512       Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR));
    513   output_frame->SetFrameSlot(output_offset, context);
    514   if (FLAG_trace_deopt) {
    515     PrintF("    0x%08x: [top + %d] <- 0x%08x ; context (adaptor sentinel)\n",
    516            top_address + output_offset, output_offset, context);
    517   }
    518 
    519   // The function was mentioned explicitly in the ARGUMENTS_ADAPTOR_FRAME.
    520   output_offset -= kPointerSize;
    521   value = reinterpret_cast<intptr_t>(function);
    522   output_frame->SetFrameSlot(output_offset, value);
    523   if (FLAG_trace_deopt) {
    524     PrintF("    0x%08x: [top + %d] <- 0x%08x ; function\n",
    525            top_address + output_offset, output_offset, value);
    526   }
    527 
    528   // Number of incoming arguments.
    529   output_offset -= kPointerSize;
    530   value = reinterpret_cast<uint32_t>(Smi::FromInt(height - 1));
    531   output_frame->SetFrameSlot(output_offset, value);
    532   if (FLAG_trace_deopt) {
    533     PrintF("    0x%08x: [top + %d] <- 0x%08x ; argc (%d)\n",
    534            top_address + output_offset, output_offset, value, height - 1);
    535   }
    536 
    537   ASSERT(0 == output_offset);
    538 
    539   Builtins* builtins = isolate_->builtins();
    540   Code* adaptor_trampoline =
    541       builtins->builtin(Builtins::kArgumentsAdaptorTrampoline);
    542   uint32_t pc = reinterpret_cast<uint32_t>(
    543       adaptor_trampoline->instruction_start() +
    544       isolate_->heap()->arguments_adaptor_deopt_pc_offset()->value());
    545   output_frame->SetPc(pc);
    546 }
    547 
    548 
    549 void Deoptimizer::DoComputeConstructStubFrame(TranslationIterator* iterator,
    550                                               int frame_index) {
    551   Builtins* builtins = isolate_->builtins();
    552   Code* construct_stub = builtins->builtin(Builtins::kJSConstructStubGeneric);
    553   JSFunction* function = JSFunction::cast(ComputeLiteral(iterator->Next()));
    554   unsigned height = iterator->Next();
    555   unsigned height_in_bytes = height * kPointerSize;
    556   if (FLAG_trace_deopt) {
    557     PrintF("  translating construct stub => height=%d\n", height_in_bytes);
    558   }
    559 
    560   unsigned fixed_frame_size = 7 * kPointerSize;
    561   unsigned output_frame_size = height_in_bytes + fixed_frame_size;
    562 
    563   // Allocate and store the output frame description.
    564   FrameDescription* output_frame =
    565       new(output_frame_size) FrameDescription(output_frame_size, function);
    566   output_frame->SetFrameType(StackFrame::CONSTRUCT);
    567 
    568   // Construct stub can not be topmost or bottommost.
    569   ASSERT(frame_index > 0 && frame_index < output_count_ - 1);
    570   ASSERT(output_[frame_index] == NULL);
    571   output_[frame_index] = output_frame;
    572 
    573   // The top address of the frame is computed from the previous
    574   // frame's top and this frame's size.
    575   uint32_t top_address;
    576   top_address = output_[frame_index - 1]->GetTop() - output_frame_size;
    577   output_frame->SetTop(top_address);
    578 
    579   // Compute the incoming parameter translation.
    580   int parameter_count = height;
    581   unsigned output_offset = output_frame_size;
    582   for (int i = 0; i < parameter_count; ++i) {
    583     output_offset -= kPointerSize;
    584     DoTranslateCommand(iterator, frame_index, output_offset);
    585   }
    586 
    587   // Read caller's PC from the previous frame.
    588   output_offset -= kPointerSize;
    589   intptr_t callers_pc = output_[frame_index - 1]->GetPc();
    590   output_frame->SetFrameSlot(output_offset, callers_pc);
    591   if (FLAG_trace_deopt) {
    592     PrintF("    0x%08x: [top + %d] <- 0x%08x ; caller's pc\n",
    593            top_address + output_offset, output_offset, callers_pc);
    594   }
    595 
    596   // Read caller's FP from the previous frame, and set this frame's FP.
    597   output_offset -= kPointerSize;
    598   intptr_t value = output_[frame_index - 1]->GetFp();
    599   output_frame->SetFrameSlot(output_offset, value);
    600   intptr_t fp_value = top_address + output_offset;
    601   output_frame->SetFp(fp_value);
    602   if (FLAG_trace_deopt) {
    603     PrintF("    0x%08x: [top + %d] <- 0x%08x ; caller's fp\n",
    604            fp_value, output_offset, value);
    605   }
    606 
    607   // The context can be gotten from the previous frame.
    608   output_offset -= kPointerSize;
    609   value = output_[frame_index - 1]->GetContext();
    610   output_frame->SetFrameSlot(output_offset, value);
    611   if (FLAG_trace_deopt) {
    612     PrintF("    0x%08x: [top + %d] <- 0x%08x ; context\n",
    613            top_address + output_offset, output_offset, value);
    614   }
    615 
    616   // A marker value is used in place of the function.
    617   output_offset -= kPointerSize;
    618   value = reinterpret_cast<intptr_t>(Smi::FromInt(StackFrame::CONSTRUCT));
    619   output_frame->SetFrameSlot(output_offset, value);
    620   if (FLAG_trace_deopt) {
    621     PrintF("    0x%08x: [top + %d] <- 0x%08x ; function (construct sentinel)\n",
    622            top_address + output_offset, output_offset, value);
    623   }
    624 
    625   // The output frame reflects a JSConstructStubGeneric frame.
    626   output_offset -= kPointerSize;
    627   value = reinterpret_cast<intptr_t>(construct_stub);
    628   output_frame->SetFrameSlot(output_offset, value);
    629   if (FLAG_trace_deopt) {
    630     PrintF("    0x%08x: [top + %d] <- 0x%08x ; code object\n",
    631            top_address + output_offset, output_offset, value);
    632   }
    633 
    634   // Number of incoming arguments.
    635   output_offset -= kPointerSize;
    636   value = reinterpret_cast<uint32_t>(Smi::FromInt(height - 1));
    637   output_frame->SetFrameSlot(output_offset, value);
    638   if (FLAG_trace_deopt) {
    639     PrintF("    0x%08x: [top + %d] <- 0x%08x ; argc (%d)\n",
    640            top_address + output_offset, output_offset, value, height - 1);
    641   }
    642 
    643   // The newly allocated object was passed as receiver in the artificial
    644   // constructor stub environment created by HEnvironment::CopyForInlining().
    645   output_offset -= kPointerSize;
    646   value = output_frame->GetFrameSlot(output_frame_size - kPointerSize);
    647   output_frame->SetFrameSlot(output_offset, value);
    648   if (FLAG_trace_deopt) {
    649     PrintF("    0x%08x: [top + %d] <- 0x%08x ; allocated receiver\n",
    650            top_address + output_offset, output_offset, value);
    651   }
    652 
    653   ASSERT(0 == output_offset);
    654 
    655   uint32_t pc = reinterpret_cast<uint32_t>(
    656       construct_stub->instruction_start() +
    657       isolate_->heap()->construct_stub_deopt_pc_offset()->value());
    658   output_frame->SetPc(pc);
    659 }
    660 
    661 
    662 void Deoptimizer::DoComputeJSFrame(TranslationIterator* iterator,
    663                                    int frame_index) {
    664   int node_id = iterator->Next();
    665   JSFunction* function = JSFunction::cast(ComputeLiteral(iterator->Next()));
    666   unsigned height = iterator->Next();
    667   unsigned height_in_bytes = height * kPointerSize;
    668   if (FLAG_trace_deopt) {
    669     PrintF("  translating ");
    670     function->PrintName();
    671     PrintF(" => node=%d, height=%d\n", node_id, height_in_bytes);
    672   }
    673 
    674   // The 'fixed' part of the frame consists of the incoming parameters and
    675   // the part described by JavaScriptFrameConstants.
    676   unsigned fixed_frame_size = ComputeFixedSize(function);
    677   unsigned input_frame_size = input_->GetFrameSize();
    678   unsigned output_frame_size = height_in_bytes + fixed_frame_size;
    679 
    680   // Allocate and store the output frame description.
    681   FrameDescription* output_frame =
    682       new(output_frame_size) FrameDescription(output_frame_size, function);
    683   output_frame->SetFrameType(StackFrame::JAVA_SCRIPT);
    684 
    685   bool is_bottommost = (0 == frame_index);
    686   bool is_topmost = (output_count_ - 1 == frame_index);
    687   ASSERT(frame_index >= 0 && frame_index < output_count_);
    688   ASSERT(output_[frame_index] == NULL);
    689   output_[frame_index] = output_frame;
    690 
    691   // The top address for the bottommost output frame can be computed from
    692   // the input frame pointer and the output frame's height.  For all
    693   // subsequent output frames, it can be computed from the previous one's
    694   // top address and the current frame's size.
    695   uint32_t top_address;
    696   if (is_bottommost) {
    697     // 2 = context and function in the frame.
    698     top_address =
    699         input_->GetRegister(ebp.code()) - (2 * kPointerSize) - height_in_bytes;
    700   } else {
    701     top_address = output_[frame_index - 1]->GetTop() - output_frame_size;
    702   }
    703   output_frame->SetTop(top_address);
    704 
    705   // Compute the incoming parameter translation.
    706   int parameter_count = function->shared()->formal_parameter_count() + 1;
    707   unsigned output_offset = output_frame_size;
    708   unsigned input_offset = input_frame_size;
    709   for (int i = 0; i < parameter_count; ++i) {
    710     output_offset -= kPointerSize;
    711     DoTranslateCommand(iterator, frame_index, output_offset);
    712   }
    713   input_offset -= (parameter_count * kPointerSize);
    714 
    715   // There are no translation commands for the caller's pc and fp, the
    716   // context, and the function.  Synthesize their values and set them up
    717   // explicitly.
    718   //
    719   // The caller's pc for the bottommost output frame is the same as in the
    720   // input frame.  For all subsequent output frames, it can be read from the
    721   // previous one.  This frame's pc can be computed from the non-optimized
    722   // function code and AST id of the bailout.
    723   output_offset -= kPointerSize;
    724   input_offset -= kPointerSize;
    725   intptr_t value;
    726   if (is_bottommost) {
    727     value = input_->GetFrameSlot(input_offset);
    728   } else {
    729     value = output_[frame_index - 1]->GetPc();
    730   }
    731   output_frame->SetFrameSlot(output_offset, value);
    732   if (FLAG_trace_deopt) {
    733     PrintF("    0x%08x: [top + %d] <- 0x%08x ; caller's pc\n",
    734            top_address + output_offset, output_offset, value);
    735   }
    736 
    737   // The caller's frame pointer for the bottommost output frame is the same
    738   // as in the input frame.  For all subsequent output frames, it can be
    739   // read from the previous one.  Also compute and set this frame's frame
    740   // pointer.
    741   output_offset -= kPointerSize;
    742   input_offset -= kPointerSize;
    743   if (is_bottommost) {
    744     value = input_->GetFrameSlot(input_offset);
    745   } else {
    746     value = output_[frame_index - 1]->GetFp();
    747   }
    748   output_frame->SetFrameSlot(output_offset, value);
    749   intptr_t fp_value = top_address + output_offset;
    750   ASSERT(!is_bottommost || input_->GetRegister(ebp.code()) == fp_value);
    751   output_frame->SetFp(fp_value);
    752   if (is_topmost) output_frame->SetRegister(ebp.code(), fp_value);
    753   if (FLAG_trace_deopt) {
    754     PrintF("    0x%08x: [top + %d] <- 0x%08x ; caller's fp\n",
    755            fp_value, output_offset, value);
    756   }
    757 
    758   // For the bottommost output frame the context can be gotten from the input
    759   // frame. For all subsequent output frames it can be gotten from the function
    760   // so long as we don't inline functions that need local contexts.
    761   output_offset -= kPointerSize;
    762   input_offset -= kPointerSize;
    763   if (is_bottommost) {
    764     value = input_->GetFrameSlot(input_offset);
    765   } else {
    766     value = reinterpret_cast<uint32_t>(function->context());
    767   }
    768   output_frame->SetFrameSlot(output_offset, value);
    769   output_frame->SetContext(value);
    770   if (is_topmost) output_frame->SetRegister(esi.code(), value);
    771   if (FLAG_trace_deopt) {
    772     PrintF("    0x%08x: [top + %d] <- 0x%08x ; context\n",
    773            top_address + output_offset, output_offset, value);
    774   }
    775 
    776   // The function was mentioned explicitly in the BEGIN_FRAME.
    777   output_offset -= kPointerSize;
    778   input_offset -= kPointerSize;
    779   value = reinterpret_cast<uint32_t>(function);
    780   // The function for the bottommost output frame should also agree with the
    781   // input frame.
    782   ASSERT(!is_bottommost || input_->GetFrameSlot(input_offset) == value);
    783   output_frame->SetFrameSlot(output_offset, value);
    784   if (FLAG_trace_deopt) {
    785     PrintF("    0x%08x: [top + %d] <- 0x%08x ; function\n",
    786            top_address + output_offset, output_offset, value);
    787   }
    788 
    789   // Translate the rest of the frame.
    790   for (unsigned i = 0; i < height; ++i) {
    791     output_offset -= kPointerSize;
    792     DoTranslateCommand(iterator, frame_index, output_offset);
    793   }
    794   ASSERT(0 == output_offset);
    795 
    796   // Compute this frame's PC, state, and continuation.
    797   Code* non_optimized_code = function->shared()->code();
    798   FixedArray* raw_data = non_optimized_code->deoptimization_data();
    799   DeoptimizationOutputData* data = DeoptimizationOutputData::cast(raw_data);
    800   Address start = non_optimized_code->instruction_start();
    801   unsigned pc_and_state = GetOutputInfo(data, node_id, function->shared());
    802   unsigned pc_offset = FullCodeGenerator::PcField::decode(pc_and_state);
    803   uint32_t pc_value = reinterpret_cast<uint32_t>(start + pc_offset);
    804   output_frame->SetPc(pc_value);
    805 
    806   FullCodeGenerator::State state =
    807       FullCodeGenerator::StateField::decode(pc_and_state);
    808   output_frame->SetState(Smi::FromInt(state));
    809 
    810   // Set the continuation for the topmost frame.
    811   if (is_topmost && bailout_type_ != DEBUGGER) {
    812     Builtins* builtins = isolate_->builtins();
    813     Code* continuation = (bailout_type_ == EAGER)
    814         ? builtins->builtin(Builtins::kNotifyDeoptimized)
    815         : builtins->builtin(Builtins::kNotifyLazyDeoptimized);
    816     output_frame->SetContinuation(
    817         reinterpret_cast<uint32_t>(continuation->entry()));
    818   }
    819 }
    820 
    821 
    822 void Deoptimizer::FillInputFrame(Address tos, JavaScriptFrame* frame) {
    823   // Set the register values. The values are not important as there are no
    824   // callee saved registers in JavaScript frames, so all registers are
    825   // spilled. Registers ebp and esp are set to the correct values though.
    826 
    827   for (int i = 0; i < Register::kNumRegisters; i++) {
    828     input_->SetRegister(i, i * 4);
    829   }
    830   input_->SetRegister(esp.code(), reinterpret_cast<intptr_t>(frame->sp()));
    831   input_->SetRegister(ebp.code(), reinterpret_cast<intptr_t>(frame->fp()));
    832   for (int i = 0; i < DoubleRegister::kNumAllocatableRegisters; i++) {
    833     input_->SetDoubleRegister(i, 0.0);
    834   }
    835 
    836   // Fill the frame content from the actual data on the frame.
    837   for (unsigned i = 0; i < input_->GetFrameSize(); i += kPointerSize) {
    838     input_->SetFrameSlot(i, Memory::uint32_at(tos + i));
    839   }
    840 }
    841 
    842 
    843 #define __ masm()->
    844 
    845 void Deoptimizer::EntryGenerator::Generate() {
    846   GeneratePrologue();
    847   CpuFeatures::Scope scope(SSE2);
    848 
    849   Isolate* isolate = masm()->isolate();
    850 
    851   // Save all general purpose registers before messing with them.
    852   const int kNumberOfRegisters = Register::kNumRegisters;
    853 
    854   const int kDoubleRegsSize = kDoubleSize *
    855                               XMMRegister::kNumAllocatableRegisters;
    856   __ sub(esp, Immediate(kDoubleRegsSize));
    857   for (int i = 0; i < XMMRegister::kNumAllocatableRegisters; ++i) {
    858     XMMRegister xmm_reg = XMMRegister::FromAllocationIndex(i);
    859     int offset = i * kDoubleSize;
    860     __ movdbl(Operand(esp, offset), xmm_reg);
    861   }
    862 
    863   __ pushad();
    864 
    865   const int kSavedRegistersAreaSize = kNumberOfRegisters * kPointerSize +
    866                                       kDoubleRegsSize;
    867 
    868   // Get the bailout id from the stack.
    869   __ mov(ebx, Operand(esp, kSavedRegistersAreaSize));
    870 
    871   // Get the address of the location in the code object if possible
    872   // and compute the fp-to-sp delta in register edx.
    873   if (type() == EAGER) {
    874     __ Set(ecx, Immediate(0));
    875     __ lea(edx, Operand(esp, kSavedRegistersAreaSize + 1 * kPointerSize));
    876   } else {
    877     __ mov(ecx, Operand(esp, kSavedRegistersAreaSize + 1 * kPointerSize));
    878     __ lea(edx, Operand(esp, kSavedRegistersAreaSize + 2 * kPointerSize));
    879   }
    880   __ sub(edx, ebp);
    881   __ neg(edx);
    882 
    883   // Allocate a new deoptimizer object.
    884   __ PrepareCallCFunction(6, eax);
    885   __ mov(eax, Operand(ebp, JavaScriptFrameConstants::kFunctionOffset));
    886   __ mov(Operand(esp, 0 * kPointerSize), eax);  // Function.
    887   __ mov(Operand(esp, 1 * kPointerSize), Immediate(type()));  // Bailout type.
    888   __ mov(Operand(esp, 2 * kPointerSize), ebx);  // Bailout id.
    889   __ mov(Operand(esp, 3 * kPointerSize), ecx);  // Code address or 0.
    890   __ mov(Operand(esp, 4 * kPointerSize), edx);  // Fp-to-sp delta.
    891   __ mov(Operand(esp, 5 * kPointerSize),
    892          Immediate(ExternalReference::isolate_address()));
    893   {
    894     AllowExternalCallThatCantCauseGC scope(masm());
    895     __ CallCFunction(ExternalReference::new_deoptimizer_function(isolate), 6);
    896   }
    897 
    898   // Preserve deoptimizer object in register eax and get the input
    899   // frame descriptor pointer.
    900   __ mov(ebx, Operand(eax, Deoptimizer::input_offset()));
    901 
    902   // Fill in the input registers.
    903   for (int i = kNumberOfRegisters - 1; i >= 0; i--) {
    904     int offset = (i * kPointerSize) + FrameDescription::registers_offset();
    905     __ pop(Operand(ebx, offset));
    906   }
    907 
    908   // Fill in the double input registers.
    909   int double_regs_offset = FrameDescription::double_registers_offset();
    910   for (int i = 0; i < XMMRegister::kNumAllocatableRegisters; ++i) {
    911     int dst_offset = i * kDoubleSize + double_regs_offset;
    912     int src_offset = i * kDoubleSize;
    913     __ movdbl(xmm0, Operand(esp, src_offset));
    914     __ movdbl(Operand(ebx, dst_offset), xmm0);
    915   }
    916 
    917   // Remove the bailout id and the double registers from the stack.
    918   if (type() == EAGER) {
    919     __ add(esp, Immediate(kDoubleRegsSize + kPointerSize));
    920   } else {
    921     __ add(esp, Immediate(kDoubleRegsSize + 2 * kPointerSize));
    922   }
    923 
    924   // Compute a pointer to the unwinding limit in register ecx; that is
    925   // the first stack slot not part of the input frame.
    926   __ mov(ecx, Operand(ebx, FrameDescription::frame_size_offset()));
    927   __ add(ecx, esp);
    928 
    929   // Unwind the stack down to - but not including - the unwinding
    930   // limit and copy the contents of the activation frame to the input
    931   // frame description.
    932   __ lea(edx, Operand(ebx, FrameDescription::frame_content_offset()));
    933   Label pop_loop;
    934   __ bind(&pop_loop);
    935   __ pop(Operand(edx, 0));
    936   __ add(edx, Immediate(sizeof(uint32_t)));
    937   __ cmp(ecx, esp);
    938   __ j(not_equal, &pop_loop);
    939 
    940   // Compute the output frame in the deoptimizer.
    941   __ push(eax);
    942   __ PrepareCallCFunction(1, ebx);
    943   __ mov(Operand(esp, 0 * kPointerSize), eax);
    944   {
    945     AllowExternalCallThatCantCauseGC scope(masm());
    946     __ CallCFunction(
    947         ExternalReference::compute_output_frames_function(isolate), 1);
    948   }
    949   __ pop(eax);
    950 
    951   // Replace the current frame with the output frames.
    952   Label outer_push_loop, inner_push_loop;
    953   // Outer loop state: eax = current FrameDescription**, edx = one past the
    954   // last FrameDescription**.
    955   __ mov(edx, Operand(eax, Deoptimizer::output_count_offset()));
    956   __ mov(eax, Operand(eax, Deoptimizer::output_offset()));
    957   __ lea(edx, Operand(eax, edx, times_4, 0));
    958   __ bind(&outer_push_loop);
    959   // Inner loop state: ebx = current FrameDescription*, ecx = loop index.
    960   __ mov(ebx, Operand(eax, 0));
    961   __ mov(ecx, Operand(ebx, FrameDescription::frame_size_offset()));
    962   __ bind(&inner_push_loop);
    963   __ sub(ecx, Immediate(sizeof(uint32_t)));
    964   __ push(Operand(ebx, ecx, times_1, FrameDescription::frame_content_offset()));
    965   __ test(ecx, ecx);
    966   __ j(not_zero, &inner_push_loop);
    967   __ add(eax, Immediate(kPointerSize));
    968   __ cmp(eax, edx);
    969   __ j(below, &outer_push_loop);
    970 
    971   // In case of OSR, we have to restore the XMM registers.
    972   if (type() == OSR) {
    973     for (int i = 0; i < XMMRegister::kNumAllocatableRegisters; ++i) {
    974       XMMRegister xmm_reg = XMMRegister::FromAllocationIndex(i);
    975       int src_offset = i * kDoubleSize + double_regs_offset;
    976       __ movdbl(xmm_reg, Operand(ebx, src_offset));
    977     }
    978   }
    979 
    980   // Push state, pc, and continuation from the last output frame.
    981   if (type() != OSR) {
    982     __ push(Operand(ebx, FrameDescription::state_offset()));
    983   }
    984   __ push(Operand(ebx, FrameDescription::pc_offset()));
    985   __ push(Operand(ebx, FrameDescription::continuation_offset()));
    986 
    987 
    988   // Push the registers from the last output frame.
    989   for (int i = 0; i < kNumberOfRegisters; i++) {
    990     int offset = (i * kPointerSize) + FrameDescription::registers_offset();
    991     __ push(Operand(ebx, offset));
    992   }
    993 
    994   // Restore the registers from the stack.
    995   __ popad();
    996 
    997   // Return to the continuation point.
    998   __ ret(0);
    999 }
   1000 
   1001 
   1002 void Deoptimizer::TableEntryGenerator::GeneratePrologue() {
   1003   // Create a sequence of deoptimization entries.
   1004   Label done;
   1005   for (int i = 0; i < count(); i++) {
   1006     int start = masm()->pc_offset();
   1007     USE(start);
   1008     __ push_imm32(i);
   1009     __ jmp(&done);
   1010     ASSERT(masm()->pc_offset() - start == table_entry_size_);
   1011   }
   1012   __ bind(&done);
   1013 }
   1014 
   1015 #undef __
   1016 
   1017 
   1018 } }  // namespace v8::internal
   1019 
   1020 #endif  // V8_TARGET_ARCH_IA32
   1021