deoptimizer.cc 120 KB
Newer Older
1
// Copyright 2013 the V8 project authors. All rights reserved.
2 3
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
4

5
#include "src/deoptimizer.h"
6 7

#include "src/accessors.h"
8
#include "src/ast/prettyprinter.h"
9 10
#include "src/codegen.h"
#include "src/disasm.h"
11
#include "src/frames-inl.h"
12
#include "src/full-codegen/full-codegen.h"
13 14
#include "src/global-handles.h"
#include "src/macro-assembler.h"
15
#include "src/profiler/cpu-profiler.h"
16
#include "src/v8.h"
17 18 19 20 21


namespace v8 {
namespace internal {

22 23
static MemoryChunk* AllocateCodeChunk(MemoryAllocator* allocator) {
  return allocator->AllocateChunk(Deoptimizer::GetMaxDeoptTableSize(),
24
                                  base::OS::CommitPageSize(),
25 26 27 28 29 30 31
#if defined(__native_client__)
  // The Native Client port of V8 uses an interpreter,
  // so code pages don't need PROT_EXEC.
                                  NOT_EXECUTABLE,
#else
                                  EXECUTABLE,
#endif
32 33 34 35 36 37 38
                                  NULL);
}


DeoptimizerData::DeoptimizerData(MemoryAllocator* allocator)
    : allocator_(allocator),
      deoptimized_frame_info_(NULL),
39
      current_(NULL) {
40 41 42 43 44
  for (int i = 0; i < Deoptimizer::kBailoutTypesWithCodeEntry; ++i) {
    deopt_entry_code_entries_[i] = -1;
    deopt_entry_code_[i] = AllocateCodeChunk(allocator);
  }
}
45 46


47
DeoptimizerData::~DeoptimizerData() {
48 49 50 51
  for (int i = 0; i < Deoptimizer::kBailoutTypesWithCodeEntry; ++i) {
    allocator_->Free(deopt_entry_code_[i]);
    deopt_entry_code_[i] = NULL;
  }
52 53
}

54 55 56 57 58 59 60 61

void DeoptimizerData::Iterate(ObjectVisitor* v) {
  if (deoptimized_frame_info_ != NULL) {
    deoptimized_frame_info_->Iterate(v);
  }
}


62 63 64 65 66 67 68
Code* Deoptimizer::FindDeoptimizingCode(Address addr) {
  if (function_->IsHeapObject()) {
    // Search all deoptimizing code in the native context of the function.
    Context* native_context = function_->context()->native_context();
    Object* element = native_context->DeoptimizedCodeListHead();
    while (!element->IsUndefined()) {
      Code* code = Code::cast(element);
69
      CHECK(code->kind() == Code::OPTIMIZED_FUNCTION);
70 71
      if (code->contains(addr)) return code;
      element = code->next_code_link();
72 73
    }
  }
74
  return NULL;
75 76 77
}


78 79
// We rely on this function not causing a GC.  It is called from generated code
// without having a real stack frame in place.
80 81 82 83
Deoptimizer* Deoptimizer::New(JSFunction* function,
                              BailoutType type,
                              unsigned bailout_id,
                              Address from,
84 85 86 87 88 89 90
                              int fp_to_sp_delta,
                              Isolate* isolate) {
  Deoptimizer* deoptimizer = new Deoptimizer(isolate,
                                             function,
                                             type,
                                             bailout_id,
                                             from,
91 92
                                             fp_to_sp_delta,
                                             NULL);
93
  CHECK(isolate->deoptimizer_data()->current_ == NULL);
94
  isolate->deoptimizer_data()->current_ = deoptimizer;
95 96 97 98
  return deoptimizer;
}


99 100 101 102 103
// No larger than 2K on all platforms
static const int kDeoptTableMaxEpilogueCodeSize = 2 * KB;


size_t Deoptimizer::GetMaxDeoptTableSize() {
104
  int entries_size =
105
      Deoptimizer::kMaxNumberOfEntries * Deoptimizer::table_entry_size_;
106
  int commit_page_size = static_cast<int>(base::OS::CommitPageSize());
107
  int page_count = ((kDeoptTableMaxEpilogueCodeSize + entries_size - 1) /
108 109
                    commit_page_size) + 1;
  return static_cast<size_t>(commit_page_size * page_count);
110 111 112
}


113 114
Deoptimizer* Deoptimizer::Grab(Isolate* isolate) {
  Deoptimizer* result = isolate->deoptimizer_data()->current_;
115
  CHECK_NOT_NULL(result);
116
  result->DeleteFrameDescriptions();
117
  isolate->deoptimizer_data()->current_ = NULL;
118 119 120
  return result;
}

121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137

int Deoptimizer::ConvertJSFrameIndexToFrameIndex(int jsframe_index) {
  if (jsframe_index == 0) return 0;

  int frame_index = 0;
  while (jsframe_index >= 0) {
    FrameDescription* frame = output_[frame_index];
    if (frame->GetFrameType() == StackFrame::JAVA_SCRIPT) {
      jsframe_index--;
    }
    frame_index++;
  }

  return frame_index - 1;
}


138 139
DeoptimizedFrameInfo* Deoptimizer::DebuggerInspectableFrame(
    JavaScriptFrame* frame,
140
    int jsframe_index,
141
    Isolate* isolate) {
142 143
  CHECK(frame->is_optimized());
  CHECK(isolate->deoptimizer_data()->deoptimized_frame_info_ == NULL);
144 145

  // Get the function and code from the frame.
146
  JSFunction* function = frame->function();
147 148 149 150
  Code* code = frame->LookupCode();

  // Locate the deoptimization point in the code. As we are at a call the
  // return address must be at a place in the code with deoptimization support.
151 152
  SafepointEntry safepoint_entry = code->GetSafepointEntry(frame->pc());
  int deoptimization_index = safepoint_entry.deoptimization_index();
153
  CHECK_NE(deoptimization_index, Safepoint::kNoDeoptimizationIndex);
154 155 156 157

  // Always use the actual stack slots when calculating the fp to sp
  // delta adding two for the function and context.
  unsigned stack_slots = code->stack_slots();
158 159
  unsigned arguments_stack_height =
      Deoptimizer::ComputeOutgoingArgumentSize(code, deoptimization_index);
160
  unsigned fp_to_sp_delta = (stack_slots * kPointerSize) +
161 162
                            StandardFrameConstants::kFixedFrameSizeFromFp +
                            arguments_stack_height;
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178

  Deoptimizer* deoptimizer = new Deoptimizer(isolate,
                                             function,
                                             Deoptimizer::DEBUGGER,
                                             deoptimization_index,
                                             frame->pc(),
                                             fp_to_sp_delta,
                                             code);
  Address tos = frame->fp() - fp_to_sp_delta;
  deoptimizer->FillInputFrame(tos, frame);

  // Calculate the output frames.
  Deoptimizer::ComputeOutputFrames(deoptimizer);

  // Create the GC safe output frame information and register it for GC
  // handling.
179
  CHECK_LT(jsframe_index, deoptimizer->jsframe_count());
180 181 182 183 184 185 186 187 188

  // Convert JS frame index into frame index.
  int frame_index = deoptimizer->ConvertJSFrameIndexToFrameIndex(jsframe_index);

  bool has_arguments_adaptor =
      frame_index > 0 &&
      deoptimizer->output_[frame_index - 1]->GetFrameType() ==
      StackFrame::ARGUMENTS_ADAPTOR;

189 190 191 192 193 194 195 196 197 198
  int construct_offset = has_arguments_adaptor ? 2 : 1;
  bool has_construct_stub =
      frame_index >= construct_offset &&
      deoptimizer->output_[frame_index - construct_offset]->GetFrameType() ==
      StackFrame::CONSTRUCT;

  DeoptimizedFrameInfo* info = new DeoptimizedFrameInfo(deoptimizer,
                                                        frame_index,
                                                        has_arguments_adaptor,
                                                        has_construct_stub);
199 200 201 202 203 204 205
  isolate->deoptimizer_data()->deoptimized_frame_info_ = info;

  // Done with the GC-unsafe frame descriptions. This re-enables allocation.
  deoptimizer->DeleteFrameDescriptions();

  // Allocate a heap number for the doubles belonging to this frame.
  deoptimizer->MaterializeHeapNumbersForDebuggerInspectableFrame(
206
      frame_index, info->parameters_count(), info->expression_count(), info);
207 208 209 210 211 212 213 214 215 216

  // Finished using the deoptimizer instance.
  delete deoptimizer;

  return info;
}


void Deoptimizer::DeleteDebuggerInspectableFrame(DeoptimizedFrameInfo* info,
                                                 Isolate* isolate) {
217
  CHECK_EQ(isolate->deoptimizer_data()->deoptimized_frame_info_, info);
218 219 220
  delete info;
  isolate->deoptimizer_data()->deoptimized_frame_info_ = NULL;
}
221

222 223 224 225 226 227 228 229 230 231 232

void Deoptimizer::GenerateDeoptimizationEntries(MacroAssembler* masm,
                                                int count,
                                                BailoutType type) {
  TableEntryGenerator generator(masm, type, count);
  generator.Generate();
}


void Deoptimizer::VisitAllOptimizedFunctionsForContext(
    Context* context, OptimizedFunctionVisitor* visitor) {
233
  DisallowHeapAllocation no_allocation;
234

235
  CHECK(context->IsNativeContext());
236 237

  visitor->EnterContext(context);
238

239 240 241
  // Visit the list of optimized functions, removing elements that
  // no longer refer to optimized code.
  JSFunction* prev = NULL;
242 243
  Object* element = context->OptimizedFunctionsListHead();
  while (!element->IsUndefined()) {
244 245 246 247 248 249 250 251 252
    JSFunction* function = JSFunction::cast(element);
    Object* next = function->next_function_link();
    if (function->code()->kind() != Code::OPTIMIZED_FUNCTION ||
        (visitor->VisitFunction(function),
         function->code()->kind() != Code::OPTIMIZED_FUNCTION)) {
      // The function no longer refers to optimized code, or the visitor
      // changed the code to which it refers to no longer be optimized code.
      // Remove the function from this list.
      if (prev != NULL) {
253
        prev->set_next_function_link(next, UPDATE_WEAK_WRITE_BARRIER);
254 255 256 257
      } else {
        context->SetOptimizedFunctionsListHead(next);
      }
      // The visitor should not alter the link directly.
258
      CHECK_EQ(function->next_function_link(), next);
259 260
      // Set the next function link to undefined to indicate it is no longer
      // in the optimized functions list.
261 262
      function->set_next_function_link(context->GetHeap()->undefined_value(),
                                       SKIP_WRITE_BARRIER);
263 264
    } else {
      // The visitor should not alter the link directly.
265
      CHECK_EQ(function->next_function_link(), next);
266 267 268 269
      // preserve this element.
      prev = function;
    }
    element = next;
270 271
  }

272 273 274 275
  visitor->LeaveContext(context);
}


276
void Deoptimizer::VisitAllOptimizedFunctions(
277
    Isolate* isolate,
278
    OptimizedFunctionVisitor* visitor) {
279
  DisallowHeapAllocation no_allocation;
280

281
  // Run through the list of all native contexts.
282
  Object* context = isolate->heap()->native_contexts_list();
283 284 285 286 287 288 289
  while (!context->IsUndefined()) {
    VisitAllOptimizedFunctionsForContext(Context::cast(context), visitor);
    context = Context::cast(context)->get(Context::NEXT_CONTEXT_LINK);
  }
}


290 291 292 293
// Unlink functions referring to code marked for deoptimization, then move
// marked code from the optimized code list to the deoptimized code list,
// and patch code for lazy deopt.
void Deoptimizer::DeoptimizeMarkedCodeForContext(Context* context) {
294
  DisallowHeapAllocation no_allocation;
295 296 297 298 299 300 301 302

  // A "closure" that unlinks optimized code that is going to be
  // deoptimized from the functions that refer to it.
  class SelectedCodeUnlinker: public OptimizedFunctionVisitor {
   public:
    virtual void EnterContext(Context* context) { }  // Don't care.
    virtual void LeaveContext(Context* context)  { }  // Don't care.
    virtual void VisitFunction(JSFunction* function) {
303
      Code* code = function->code();
304 305 306
      if (!code->marked_for_deoptimization()) return;

      // Unlink this function and evict from optimized code map.
307 308 309 310
      SharedFunctionInfo* shared = function->shared();
      function->set_code(shared->code());

      if (FLAG_trace_deopt) {
311 312 313 314 315
        CodeTracer::Scope scope(code->GetHeap()->isolate()->GetCodeTracer());
        PrintF(scope.file(), "[deoptimizer unlinked: ");
        function->PrintName(scope.file());
        PrintF(scope.file(),
               " / %" V8PRIxPTR "]\n", reinterpret_cast<intptr_t>(function));
316 317
      }
    }
318
  };
319

320 321 322
  // Unlink all functions that refer to marked code.
  SelectedCodeUnlinker unlinker;
  VisitAllOptimizedFunctionsForContext(context, &unlinker);
323

324 325 326 327 328 329 330 331 332 333 334 335
  Isolate* isolate = context->GetHeap()->isolate();
#ifdef DEBUG
  Code* topmost_optimized_code = NULL;
  bool safe_to_deopt_topmost_optimized_code = false;
  // Make sure all activations of optimized code can deopt at their current PC.
  // The topmost optimized code has special handling because it cannot be
  // deoptimized due to weak object dependency.
  for (StackFrameIterator it(isolate, isolate->thread_local_top());
       !it.done(); it.Advance()) {
    StackFrame::Type type = it.frame()->type();
    if (type == StackFrame::OPTIMIZED) {
      Code* code = it.frame()->LookupCode();
336 337
      JSFunction* function =
          static_cast<OptimizedFrame*>(it.frame())->function();
338 339 340 341 342 343 344 345 346
      if (FLAG_trace_deopt) {
        CodeTracer::Scope scope(isolate->GetCodeTracer());
        PrintF(scope.file(), "[deoptimizer found activation of function: ");
        function->PrintName(scope.file());
        PrintF(scope.file(),
               " / %" V8PRIxPTR "]\n", reinterpret_cast<intptr_t>(function));
      }
      SafepointEntry safepoint = code->GetSafepointEntry(it.frame()->pc());
      int deopt_index = safepoint.deoptimization_index();
347
      // Turbofan deopt is checked when we are patching addresses on stack.
348 349 350
      bool turbofanned = code->is_turbofanned() &&
                         function->shared()->asm_function() &&
                         !FLAG_turbo_asm_deoptimization;
351 352 353
      bool safe_to_deopt =
          deopt_index != Safepoint::kNoDeoptimizationIndex || turbofanned;
      CHECK(topmost_optimized_code == NULL || safe_to_deopt || turbofanned);
354 355 356 357 358 359 360 361
      if (topmost_optimized_code == NULL) {
        topmost_optimized_code = code;
        safe_to_deopt_topmost_optimized_code = safe_to_deopt;
      }
    }
  }
#endif

362 363
  // Move marked code from the optimized code list to the deoptimized
  // code list, collecting them into a ZoneList.
364
  Zone zone;
365
  ZoneList<Code*> codes(10, &zone);
366

367 368 369 370 371
  // Walk over all optimized code objects in this native context.
  Code* prev = NULL;
  Object* element = context->OptimizedCodeListHead();
  while (!element->IsUndefined()) {
    Code* code = Code::cast(element);
372
    CHECK_EQ(code->kind(), Code::OPTIMIZED_FUNCTION);
373
    Object* next = code->next_code_link();
374

375
    if (code->marked_for_deoptimization()) {
376 377 378 379 380 381 382 383 384 385
      // Put the code into the list for later patching.
      codes.Add(code, &zone);

      if (prev != NULL) {
        // Skip this code in the optimized code list.
        prev->set_next_code_link(next);
      } else {
        // There was no previous node, the next node is the new head.
        context->SetOptimizedCodeListHead(next);
      }
386

387 388 389 390 391 392 393 394
      // Move the code to the _deoptimized_ code list.
      code->set_next_code_link(context->DeoptimizedCodeListHead());
      context->SetDeoptimizedCodeListHead(code);
    } else {
      // Not marked; preserve this element.
      prev = code;
    }
    element = next;
395 396
  }

397 398 399
  // TODO(titzer): we need a handle scope only because of the macro assembler,
  // which is only used in EnsureCodeForDeoptimizationEntry.
  HandleScope scope(isolate);
400

401 402
  // Now patch all the codes for deoptimization.
  for (int i = 0; i < codes.length(); i++) {
403 404
#ifdef DEBUG
    if (codes[i] == topmost_optimized_code) {
405
      DCHECK(safe_to_deopt_topmost_optimized_code);
406 407
    }
#endif
408
    // It is finally time to die, code object.
409 410 411 412 413 414 415 416

    // Remove the code from optimized code map.
    DeoptimizationInputData* deopt_data =
        DeoptimizationInputData::cast(codes[i]->deoptimization_data());
    SharedFunctionInfo* shared =
        SharedFunctionInfo::cast(deopt_data->SharedFunctionInfo());
    shared->EvictFromOptimizedCodeMap(codes[i], "deoptimized code");

417
    // Do platform-specific patching to force any activations to lazy deopt.
418
    PatchCodeForDeoptimization(isolate, codes[i]);
419

420
    // We might be in the middle of incremental marking with compaction.
421 422 423
    // Tell collector to treat this code object in a special way and
    // ignore all slots that might have been recorded on it.
    isolate->heap()->mark_compact_collector()->InvalidateCode(codes[i]);
424 425 426
  }
}

427

428
void Deoptimizer::DeoptimizeAll(Isolate* isolate) {
429
  if (FLAG_trace_deopt) {
430 431
    CodeTracer::Scope scope(isolate->GetCodeTracer());
    PrintF(scope.file(), "[deoptimize all code in all contexts]\n");
432
  }
433
  DisallowHeapAllocation no_allocation;
434 435 436 437 438 439 440
  // For all contexts, mark all code, then deoptimize.
  Object* context = isolate->heap()->native_contexts_list();
  while (!context->IsUndefined()) {
    Context* native_context = Context::cast(context);
    MarkAllCodeForContext(native_context);
    DeoptimizeMarkedCodeForContext(native_context);
    context = native_context->get(Context::NEXT_CONTEXT_LINK);
441 442 443 444
  }
}


445 446
void Deoptimizer::DeoptimizeMarkedCode(Isolate* isolate) {
  if (FLAG_trace_deopt) {
447 448
    CodeTracer::Scope scope(isolate->GetCodeTracer());
    PrintF(scope.file(), "[deoptimize marked code in all contexts]\n");
449
  }
450
  DisallowHeapAllocation no_allocation;
451
  // For all contexts, deoptimize code already marked.
452
  Object* context = isolate->heap()->native_contexts_list();
453
  while (!context->IsUndefined()) {
454 455 456
    Context* native_context = Context::cast(context);
    DeoptimizeMarkedCodeForContext(native_context);
    context = native_context->get(Context::NEXT_CONTEXT_LINK);
457 458 459 460
  }
}


461 462 463 464
void Deoptimizer::MarkAllCodeForContext(Context* context) {
  Object* element = context->OptimizedCodeListHead();
  while (!element->IsUndefined()) {
    Code* code = Code::cast(element);
465
    CHECK_EQ(code->kind(), Code::OPTIMIZED_FUNCTION);
466 467 468
    code->set_marked_for_deoptimization(true);
    element = code->next_code_link();
  }
469 470 471
}


472 473 474 475 476 477 478 479
void Deoptimizer::DeoptimizeFunction(JSFunction* function) {
  Code* code = function->code();
  if (code->kind() == Code::OPTIMIZED_FUNCTION) {
    // Mark the code for deoptimization and unlink any functions that also
    // refer to that code. The code cannot be shared across native contexts,
    // so we only need to search one.
    code->set_marked_for_deoptimization(true);
    DeoptimizeMarkedCodeForContext(function->context()->native_context());
480 481 482 483
  }
}


484
void Deoptimizer::ComputeOutputFrames(Deoptimizer* deoptimizer) {
485 486 487 488
  deoptimizer->DoComputeOutputFrames();
}


489 490 491
bool Deoptimizer::TraceEnabledFor(BailoutType deopt_type,
                                  StackFrame::Type frame_type) {
  switch (deopt_type) {
492
    case EAGER:
493
    case SOFT:
494 495
    case LAZY:
    case DEBUGGER:
496 497 498
      return (frame_type == StackFrame::STUB)
          ? FLAG_trace_stub_failures
          : FLAG_trace_deopt;
499
  }
500
  FATAL("Unsupported deopt type");
501 502 503 504 505 506
  return false;
}


const char* Deoptimizer::MessageFor(BailoutType type) {
  switch (type) {
507 508 509 510
    case EAGER: return "eager";
    case SOFT: return "soft";
    case LAZY: return "lazy";
    case DEBUGGER: return "debugger";
511
  }
512
  FATAL("Unsupported deopt type");
513
  return NULL;
514 515 516
}


517 518 519
Deoptimizer::Deoptimizer(Isolate* isolate, JSFunction* function,
                         BailoutType type, unsigned bailout_id, Address from,
                         int fp_to_sp_delta, Code* optimized_code)
520 521
    : isolate_(isolate),
      function_(function),
522 523 524 525
      bailout_id_(bailout_id),
      bailout_type_(type),
      from_(from),
      fp_to_sp_delta_(fp_to_sp_delta),
526
      has_alignment_padding_(0),
527
      input_(nullptr),
528
      output_count_(0),
529
      jsframe_count_(0),
530 531
      output_(nullptr),
      trace_scope_(nullptr) {
532 533
  // For COMPILED_STUBs called from builtins, the function pointer is a SMI
  // indicating an internal frame.
534
  if (function->IsSmi()) {
535
    function = nullptr;
536
  }
537 538
  DCHECK(from != nullptr);
  if (function != nullptr && function->IsOptimized()) {
539
    function->shared()->increment_deopt_count();
540
    if (bailout_type_ == Deoptimizer::SOFT) {
541
      isolate->counters()->soft_deopts_executed()->Increment();
542 543 544 545 546 547
      // Soft deopts shouldn't count against the overall re-optimization count
      // that can eventually lead to disabling optimization for a function.
      int opt_count = function->shared()->opt_count();
      if (opt_count > 0) opt_count--;
      function->shared()->set_opt_count(opt_count);
    }
548
  }
549
  compiled_code_ = FindOptimizedCode(function, optimized_code);
550
#if DEBUG
551
  DCHECK(compiled_code_ != NULL);
552
  if (type == EAGER || type == SOFT || type == LAZY) {
553
    DCHECK(compiled_code_->kind() != Code::FUNCTION);
554 555 556
  }
#endif

557 558 559
  StackFrame::Type frame_type = function == NULL
      ? StackFrame::STUB
      : StackFrame::JAVA_SCRIPT;
560 561
  trace_scope_ = TraceEnabledFor(type, frame_type) ?
      new CodeTracer::Scope(isolate->GetCodeTracer()) : NULL;
562 563 564 565
#ifdef DEBUG
  CHECK(AllowHeapAllocation::IsAllowed());
  disallow_heap_allocation_ = new DisallowHeapAllocation();
#endif  // DEBUG
566
  if (compiled_code_->kind() == Code::OPTIMIZED_FUNCTION) {
567
    PROFILE(isolate_, CodeDeoptEvent(compiled_code_, from_, fp_to_sp_delta_));
568
  }
569 570
  unsigned size = ComputeInputFrameSize();
  input_ = new(size) FrameDescription(size, function);
571
  input_->SetFrameType(frame_type);
572 573 574
}


575 576 577
Code* Deoptimizer::FindOptimizedCode(JSFunction* function,
                                     Code* optimized_code) {
  switch (bailout_type_) {
578
    case Deoptimizer::SOFT:
579 580
    case Deoptimizer::EAGER:
    case Deoptimizer::LAZY: {
581
      Code* compiled_code = FindDeoptimizingCode(from_);
582
      return (compiled_code == NULL)
583
          ? static_cast<Code*>(isolate_->FindCodeObject(from_))
584 585 586
          : compiled_code;
    }
    case Deoptimizer::DEBUGGER:
587
      DCHECK(optimized_code->contains(from_));
588 589
      return optimized_code;
  }
590
  FATAL("Could not find code for optimized function");
591 592 593 594 595 596
  return NULL;
}


void Deoptimizer::PrintFunctionName() {
  if (function_->IsJSFunction()) {
597
    function_->ShortPrint(trace_scope_->file());
598
  } else {
599 600
    PrintF(trace_scope_->file(),
           "%s", Code::Kind2String(compiled_code_->kind()));
601 602 603 604
  }
}


605
Deoptimizer::~Deoptimizer() {
606 607
  DCHECK(input_ == NULL && output_ == NULL);
  DCHECK(disallow_heap_allocation_ == NULL);
608
  delete trace_scope_;
609 610 611 612 613 614 615 616 617 618 619
}


void Deoptimizer::DeleteFrameDescriptions() {
  delete input_;
  for (int i = 0; i < output_count_; ++i) {
    if (output_[i] != input_) delete output_[i];
  }
  delete[] output_;
  input_ = NULL;
  output_ = NULL;
620 621 622 623 624 625
#ifdef DEBUG
  CHECK(!AllowHeapAllocation::IsAllowed());
  CHECK(disallow_heap_allocation_ != NULL);
  delete disallow_heap_allocation_;
  disallow_heap_allocation_ = NULL;
#endif  // DEBUG
626 627 628
}


629 630
Address Deoptimizer::GetDeoptimizationEntry(Isolate* isolate,
                                            int id,
631 632
                                            BailoutType type,
                                            GetEntryMode mode) {
633
  CHECK_GE(id, 0);
634 635
  if (id >= kMaxNumberOfEntries) return NULL;
  if (mode == ENSURE_ENTRY_CODE) {
636
    EnsureCodeForDeoptimizationEntry(isolate, type, id);
637
  } else {
638
    CHECK_EQ(mode, CALCULATE_ENTRY_ADDRESS);
639
  }
640
  DeoptimizerData* data = isolate->deoptimizer_data();
641
  CHECK_LT(type, kBailoutTypesWithCodeEntry);
642
  MemoryChunk* base = data->deopt_entry_code_[type];
643
  return base->area_start() + (id * table_entry_size_);
644 645 646
}


647 648 649 650
int Deoptimizer::GetDeoptimizationId(Isolate* isolate,
                                     Address addr,
                                     BailoutType type) {
  DeoptimizerData* data = isolate->deoptimizer_data();
651
  MemoryChunk* base = data->deopt_entry_code_[type];
652
  Address start = base->area_start();
653
  if (addr < start ||
654
      addr >= start + (kMaxNumberOfEntries * table_entry_size_)) {
655 656
    return kNotDeoptimizationEntry;
  }
657
  DCHECK_EQ(0,
658 659
            static_cast<int>(addr - start) % table_entry_size_);
  return static_cast<int>(addr - start) / table_entry_size_;
660 661 662
}


663
int Deoptimizer::GetOutputInfo(DeoptimizationOutputData* data,
664
                               BailoutId id,
665
                               SharedFunctionInfo* shared) {
666 667 668 669 670
  // TODO(kasperl): For now, we do a simple linear search for the PC
  // offset associated with the given node id. This should probably be
  // changed to a binary search.
  int length = data->DeoptPoints();
  for (int i = 0; i < length; i++) {
671
    if (data->AstId(i) == id) {
672 673 674
      return data->PcAndState(i)->value();
    }
  }
675 676 677
  OFStream os(stderr);
  os << "[couldn't find pc offset for node=" << id.ToInt() << "]\n"
     << "[method: " << shared->DebugName()->ToCString().get() << "]\n"
678
     << "[source:\n" << SourceCodeOf(shared) << "\n]" << std::endl;
679

680 681
  shared->GetHeap()->isolate()->PushStackTraceAndDie(0xfefefefe, data, shared,
                                                     0xfefefeff);
682
  FATAL("unable to find pc offset during deoptimization");
683 684 685 686
  return -1;
}


687
int Deoptimizer::GetDeoptimizedCodeCount(Isolate* isolate) {
688
  int length = 0;
689 690 691 692 693 694 695
  // Count all entries in the deoptimizing code list of every context.
  Object* context = isolate->heap()->native_contexts_list();
  while (!context->IsUndefined()) {
    Context* native_context = Context::cast(context);
    Object* element = native_context->DeoptimizedCodeListHead();
    while (!element->IsUndefined()) {
      Code* code = Code::cast(element);
696
      DCHECK(code->kind() == Code::OPTIMIZED_FUNCTION);
697 698 699 700
      length++;
      element = code->next_code_link();
    }
    context = Context::cast(context)->get(Context::NEXT_CONTEXT_LINK);
701 702 703 704 705
  }
  return length;
}


706 707
// We rely on this function not causing a GC.  It is called from generated code
// without having a real stack frame in place.
708
void Deoptimizer::DoComputeOutputFrames() {
709
  base::ElapsedTimer timer;
710 711 712 713 714 715

  // Determine basic deoptimization information.  The optimized frame is
  // described by the input data.
  DeoptimizationInputData* input_data =
      DeoptimizationInputData::cast(compiled_code_->deoptimization_data());

716
  if (trace_scope_ != NULL) {
717
    timer.Start();
718 719
    PrintF(trace_scope_->file(), "[deoptimizing (DEOPT %s): begin ",
           MessageFor(bailout_type_));
720
    PrintFunctionName();
721
    PrintF(trace_scope_->file(),
722 723
           " (opt #%d) @%d, FP to SP delta: %d]\n",
           input_data->OptimizationId()->value(),
724 725
           bailout_id_,
           fp_to_sp_delta_);
726 727
    if (bailout_type_ == EAGER || bailout_type_ == SOFT ||
        (compiled_code_->is_hydrogen_stub())) {
728
      compiled_code_->PrintDeoptLocation(trace_scope_->file(), from_);
729
    }
730 731
  }

732
  BailoutId node_id = input_data->AstId(bailout_id_);
733 734 735 736
  ByteArray* translations = input_data->TranslationByteArray();
  unsigned translation_index =
      input_data->TranslationIndex(bailout_id_)->value();

737 738
  TranslationIterator state_iterator(translations, translation_index);
  translated_state_.Init(
739
      input_->GetFramePointerAddress(), &state_iterator,
740 741 742
      input_data->LiteralArray(), input_->GetRegisterValues(),
      trace_scope_ == nullptr ? nullptr : trace_scope_->file());

743
  // Do the input frame to output frame(s) translation.
744
  size_t count = translated_state_.frames().size();
745
  DCHECK(output_ == NULL);
746
  output_ = new FrameDescription*[count];
747
  for (size_t i = 0; i < count; ++i) {
748 749
    output_[i] = NULL;
  }
750
  output_count_ = static_cast<int>(count);
751

jarin@chromium.org's avatar
jarin@chromium.org committed
752 753 754 755 756
  Register fp_reg = JavaScriptFrame::fp_register();
  stack_fp_ = reinterpret_cast<Address>(
      input_->GetRegister(fp_reg.code()) +
          has_alignment_padding_ * kPointerSize);

757
  // Translate each output frame.
758
  for (size_t i = 0; i < count; ++i) {
759
    // Read the ast node id, function, and frame height for this output frame.
760 761 762
    int frame_index = static_cast<int>(i);
    switch (translated_state_.frames()[i].kind()) {
      case TranslatedFrame::kFunction:
763
        DoComputeJSFrame(frame_index);
764 765
        jsframe_count_++;
        break;
766
      case TranslatedFrame::kArgumentsAdaptor:
767
        DoComputeArgumentsAdaptorFrame(frame_index);
768
        break;
769
      case TranslatedFrame::kConstructStub:
770
        DoComputeConstructStubFrame(frame_index);
771
        break;
772
      case TranslatedFrame::kGetter:
773
        DoComputeAccessorStubFrame(frame_index, false);
774
        break;
775
      case TranslatedFrame::kSetter:
776
        DoComputeAccessorStubFrame(frame_index, true);
777
        break;
778
      case TranslatedFrame::kCompiledStub:
779
        DoComputeCompiledStubFrame(frame_index);
780
        break;
781 782
      case TranslatedFrame::kInvalid:
        FATAL("invalid frame");
783 784
        break;
    }
785 786 787
  }

  // Print some helpful diagnostic information.
788
  if (trace_scope_ != NULL) {
789
    double ms = timer.Elapsed().InMillisecondsF();
790
    int index = output_count_ - 1;  // Index of the topmost frame.
791 792
    PrintF(trace_scope_->file(), "[deoptimizing (%s): end ",
           MessageFor(bailout_type_));
793
    PrintFunctionName();
794 795
    PrintF(trace_scope_->file(),
           " @%d => node=%d, pc=0x%08" V8PRIxPTR ", state=%s, alignment=%s,"
796
           " took %0.3f ms]\n",
797
           bailout_id_,
798
           node_id.ToInt(),
799 800 801 802
           output_[index]->GetPc(),
           FullCodeGenerator::State2String(
               static_cast<FullCodeGenerator::State>(
                   output_[index]->GetState()->value())),
803
           has_alignment_padding_ ? "with padding" : "no padding",
804 805 806 807 808
           ms);
  }
}


809
void Deoptimizer::DoComputeJSFrame(int frame_index) {
810 811 812 813 814 815 816 817
  TranslatedFrame* translated_frame =
      &(translated_state_.frames()[frame_index]);
  TranslatedFrame::iterator value_iterator = translated_frame->begin();
  int input_index = 0;

  BailoutId node_id = translated_frame->node_id();
  unsigned height =
      translated_frame->height() - 1;  // Do not count the context.
818
  unsigned height_in_bytes = height * kPointerSize;
819 820
  JSFunction* function = JSFunction::cast(value_iterator->GetRawValue());
  value_iterator++;
821
  input_index++;
822
  if (trace_scope_ != NULL) {
823
    PrintF(trace_scope_->file(), "  translating frame ");
824 825 826
    function->PrintName(trace_scope_->file());
    PrintF(trace_scope_->file(),
           " => node=%d, height=%d\n", node_id.ToInt(), height_in_bytes);
827 828 829 830 831 832 833 834 835 836 837 838 839 840 841
  }

  // The 'fixed' part of the frame consists of the incoming parameters and
  // the part described by JavaScriptFrameConstants.
  unsigned fixed_frame_size = ComputeFixedSize(function);
  unsigned input_frame_size = input_->GetFrameSize();
  unsigned output_frame_size = height_in_bytes + fixed_frame_size;

  // Allocate and store the output frame description.
  FrameDescription* output_frame =
      new(output_frame_size) FrameDescription(output_frame_size, function);
  output_frame->SetFrameType(StackFrame::JAVA_SCRIPT);

  bool is_bottommost = (0 == frame_index);
  bool is_topmost = (output_count_ - 1 == frame_index);
842
  CHECK(frame_index >= 0 && frame_index < output_count_);
843
  CHECK_NULL(output_[frame_index]);
844 845 846 847 848 849 850 851 852 853
  output_[frame_index] = output_frame;

  // The top address for the bottommost output frame can be computed from
  // the input frame pointer and the output frame's height.  For all
  // subsequent output frames, it can be computed from the previous one's
  // top address and the current frame's size.
  Register fp_reg = JavaScriptFrame::fp_register();
  intptr_t top_address;
  if (is_bottommost) {
    // Determine whether the input frame contains alignment padding.
854 855 856 857
    has_alignment_padding_ =
        (!compiled_code_->is_turbofanned() && HasAlignmentPadding(function))
            ? 1
            : 0;
858 859 860 861
    // 2 = context and function in the frame.
    // If the optimized frame had alignment padding, adjust the frame pointer
    // to point to the new position of the old frame pointer after padding
    // is removed. Subtract 2 * kPointerSize for the context and function slots.
862 863
    top_address = input_->GetRegister(fp_reg.code()) -
        StandardFrameConstants::kFixedFrameSizeFromFp -
864 865 866 867 868 869 870
        height_in_bytes + has_alignment_padding_ * kPointerSize;
  } else {
    top_address = output_[frame_index - 1]->GetTop() - output_frame_size;
  }
  output_frame->SetTop(top_address);

  // Compute the incoming parameter translation.
871 872
  int parameter_count =
      function->shared()->internal_formal_parameter_count() + 1;
873 874 875 876
  unsigned output_offset = output_frame_size;
  unsigned input_offset = input_frame_size;
  for (int i = 0; i < parameter_count; ++i) {
    output_offset -= kPointerSize;
877 878
    WriteTranslatedValueToOutput(&value_iterator, &input_index, frame_index,
                                 output_offset);
879 880 881 882 883 884 885 886 887 888 889
  }
  input_offset -= (parameter_count * kPointerSize);

  // There are no translation commands for the caller's pc and fp, the
  // context, and the function.  Synthesize their values and set them up
  // explicitly.
  //
  // The caller's pc for the bottommost output frame is the same as in the
  // input frame.  For all subsequent output frames, it can be read from the
  // previous one.  This frame's pc can be computed from the non-optimized
  // function code and AST id of the bailout.
890 891
  output_offset -= kPCOnStackSize;
  input_offset -= kPCOnStackSize;
892 893 894 895 896 897
  intptr_t value;
  if (is_bottommost) {
    value = input_->GetFrameSlot(input_offset);
  } else {
    value = output_[frame_index - 1]->GetPc();
  }
898
  output_frame->SetCallerPc(output_offset, value);
899
  DebugPrintOutputSlot(value, frame_index, output_offset, "caller's pc\n");
900 901 902 903 904

  // The caller's frame pointer for the bottommost output frame is the same
  // as in the input frame.  For all subsequent output frames, it can be
  // read from the previous one.  Also compute and set this frame's frame
  // pointer.
905 906
  output_offset -= kFPOnStackSize;
  input_offset -= kFPOnStackSize;
907 908 909 910 911
  if (is_bottommost) {
    value = input_->GetFrameSlot(input_offset);
  } else {
    value = output_[frame_index - 1]->GetFp();
  }
912
  output_frame->SetCallerFp(output_offset, value);
913
  intptr_t fp_value = top_address + output_offset;
914
  DCHECK(!is_bottommost || (input_->GetRegister(fp_reg.code()) +
915 916 917
      has_alignment_padding_ * kPointerSize) == fp_value);
  output_frame->SetFp(fp_value);
  if (is_topmost) output_frame->SetRegister(fp_reg.code(), fp_value);
918
  DebugPrintOutputSlot(value, frame_index, output_offset, "caller's fp\n");
919
  DCHECK(!is_bottommost || !has_alignment_padding_ ||
920 921
         (fp_value & kPointerSize) != 0);

922
  if (FLAG_enable_embedded_constant_pool) {
923
    // For the bottommost output frame the constant pool pointer can be gotten
924 925
    // from the input frame. For subsequent output frames, it can be read from
    // the previous frame.
926 927 928 929 930
    output_offset -= kPointerSize;
    input_offset -= kPointerSize;
    if (is_bottommost) {
      value = input_->GetFrameSlot(input_offset);
    } else {
931
      value = output_[frame_index - 1]->GetConstantPool();
932
    }
933
    output_frame->SetCallerConstantPool(output_offset, value);
934 935
    DebugPrintOutputSlot(value, frame_index, output_offset,
                         "caller's constant_pool\n");
936 937
  }

938 939 940 941 942 943
  // For the bottommost output frame the context can be gotten from the input
  // frame. For all subsequent output frames it can be gotten from the function
  // so long as we don't inline functions that need local contexts.
  Register context_reg = JavaScriptFrame::context_register();
  output_offset -= kPointerSize;
  input_offset -= kPointerSize;
944
  // Read the context from the translations.
945
  Object* context = value_iterator->GetRawValue();
946
  // The context should not be a placeholder for a materialized object.
947 948
  CHECK(context != isolate_->heap()->arguments_marker());
  if (context == isolate_->heap()->undefined_value()) {
949 950 951
    // If the context was optimized away, just use the context from
    // the activation. This should only apply to Crankshaft code.
    CHECK(!compiled_code_->is_turbofanned());
952 953 954 955
    context =
        is_bottommost
            ? reinterpret_cast<Object*>(input_->GetFrameSlot(input_offset))
            : function->context();
956
  }
957
  value = reinterpret_cast<intptr_t>(context);
958 959
  output_frame->SetContext(value);
  if (is_topmost) output_frame->SetRegister(context_reg.code(), value);
960 961 962 963
  WriteValueToOutput(context, input_index, frame_index, output_offset,
                     "context    ");
  value_iterator++;
  input_index++;
964 965 966 967 968 969 970

  // The function was mentioned explicitly in the BEGIN_FRAME.
  output_offset -= kPointerSize;
  input_offset -= kPointerSize;
  value = reinterpret_cast<intptr_t>(function);
  // The function for the bottommost output frame should also agree with the
  // input frame.
971
  DCHECK(!is_bottommost || input_->GetFrameSlot(input_offset) == value);
972
  WriteValueToOutput(function, 0, frame_index, output_offset, "function    ");
973 974 975 976

  // Translate the rest of the frame.
  for (unsigned i = 0; i < height; ++i) {
    output_offset -= kPointerSize;
977 978
    WriteTranslatedValueToOutput(&value_iterator, &input_index, frame_index,
                                 output_offset);
979
  }
980
  CHECK_EQ(0u, output_offset);
981 982 983 984 985 986 987 988 989 990 991

  // Compute this frame's PC, state, and continuation.
  Code* non_optimized_code = function->shared()->code();
  FixedArray* raw_data = non_optimized_code->deoptimization_data();
  DeoptimizationOutputData* data = DeoptimizationOutputData::cast(raw_data);
  Address start = non_optimized_code->instruction_start();
  unsigned pc_and_state = GetOutputInfo(data, node_id, function->shared());
  unsigned pc_offset = FullCodeGenerator::PcField::decode(pc_and_state);
  intptr_t pc_value = reinterpret_cast<intptr_t>(start + pc_offset);
  output_frame->SetPc(pc_value);

992
  // Update constant pool.
993
  if (FLAG_enable_embedded_constant_pool) {
994 995 996 997 998 999 1000 1001 1002 1003
    intptr_t constant_pool_value =
        reinterpret_cast<intptr_t>(non_optimized_code->constant_pool());
    output_frame->SetConstantPool(constant_pool_value);
    if (is_topmost) {
      Register constant_pool_reg =
          JavaScriptFrame::constant_pool_pointer_register();
      output_frame->SetRegister(constant_pool_reg.code(), constant_pool_value);
    }
  }

1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016
  FullCodeGenerator::State state =
      FullCodeGenerator::StateField::decode(pc_and_state);
  output_frame->SetState(Smi::FromInt(state));

  // Set the continuation for the topmost frame.
  if (is_topmost && bailout_type_ != DEBUGGER) {
    Builtins* builtins = isolate_->builtins();
    Code* continuation = builtins->builtin(Builtins::kNotifyDeoptimized);
    if (bailout_type_ == LAZY) {
      continuation = builtins->builtin(Builtins::kNotifyLazyDeoptimized);
    } else if (bailout_type_ == SOFT) {
      continuation = builtins->builtin(Builtins::kNotifySoftDeoptimized);
    } else {
1017
      CHECK_EQ(bailout_type_, EAGER);
1018 1019 1020 1021 1022 1023 1024
    }
    output_frame->SetContinuation(
        reinterpret_cast<intptr_t>(continuation->entry()));
  }
}


1025
void Deoptimizer::DoComputeArgumentsAdaptorFrame(int frame_index) {
1026 1027 1028 1029 1030 1031
  TranslatedFrame* translated_frame =
      &(translated_state_.frames()[frame_index]);
  TranslatedFrame::iterator value_iterator = translated_frame->begin();
  int input_index = 0;

  unsigned height = translated_frame->height();
1032
  unsigned height_in_bytes = height * kPointerSize;
1033 1034
  JSFunction* function = JSFunction::cast(value_iterator->GetRawValue());
  value_iterator++;
1035
  input_index++;
1036 1037 1038
  if (trace_scope_ != NULL) {
    PrintF(trace_scope_->file(),
           "  translating arguments adaptor => height=%d\n", height_in_bytes);
1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049
  }

  unsigned fixed_frame_size = ArgumentsAdaptorFrameConstants::kFrameSize;
  unsigned output_frame_size = height_in_bytes + fixed_frame_size;

  // Allocate and store the output frame description.
  FrameDescription* output_frame =
      new(output_frame_size) FrameDescription(output_frame_size, function);
  output_frame->SetFrameType(StackFrame::ARGUMENTS_ADAPTOR);

  // Arguments adaptor can not be topmost or bottommost.
1050 1051
  CHECK(frame_index > 0 && frame_index < output_count_ - 1);
  CHECK(output_[frame_index] == NULL);
1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064
  output_[frame_index] = output_frame;

  // The top address of the frame is computed from the previous
  // frame's top and this frame's size.
  intptr_t top_address;
  top_address = output_[frame_index - 1]->GetTop() - output_frame_size;
  output_frame->SetTop(top_address);

  // Compute the incoming parameter translation.
  int parameter_count = height;
  unsigned output_offset = output_frame_size;
  for (int i = 0; i < parameter_count; ++i) {
    output_offset -= kPointerSize;
1065 1066
    WriteTranslatedValueToOutput(&value_iterator, &input_index, frame_index,
                                 output_offset);
1067 1068 1069
  }

  // Read caller's PC from the previous frame.
1070
  output_offset -= kPCOnStackSize;
1071
  intptr_t callers_pc = output_[frame_index - 1]->GetPc();
1072
  output_frame->SetCallerPc(output_offset, callers_pc);
1073
  DebugPrintOutputSlot(callers_pc, frame_index, output_offset, "caller's pc\n");
1074 1075

  // Read caller's FP from the previous frame, and set this frame's FP.
1076
  output_offset -= kFPOnStackSize;
1077
  intptr_t value = output_[frame_index - 1]->GetFp();
1078
  output_frame->SetCallerFp(output_offset, value);
1079 1080
  intptr_t fp_value = top_address + output_offset;
  output_frame->SetFp(fp_value);
1081
  DebugPrintOutputSlot(value, frame_index, output_offset, "caller's fp\n");
1082

1083
  if (FLAG_enable_embedded_constant_pool) {
1084
    // Read the caller's constant pool from the previous frame.
1085
    output_offset -= kPointerSize;
1086 1087
    value = output_[frame_index - 1]->GetConstantPool();
    output_frame->SetCallerConstantPool(output_offset, value);
1088 1089
    DebugPrintOutputSlot(value, frame_index, output_offset,
                         "caller's constant_pool\n");
1090 1091
  }

1092 1093 1094 1095 1096
  // A marker value is used in place of the context.
  output_offset -= kPointerSize;
  intptr_t context = reinterpret_cast<intptr_t>(
      Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR));
  output_frame->SetFrameSlot(output_offset, context);
1097 1098
  DebugPrintOutputSlot(context, frame_index, output_offset,
                       "context (adaptor sentinel)\n");
1099 1100 1101 1102

  // The function was mentioned explicitly in the ARGUMENTS_ADAPTOR_FRAME.
  output_offset -= kPointerSize;
  value = reinterpret_cast<intptr_t>(function);
1103
  WriteValueToOutput(function, 0, frame_index, output_offset, "function    ");
1104 1105 1106 1107 1108

  // Number of incoming arguments.
  output_offset -= kPointerSize;
  value = reinterpret_cast<intptr_t>(Smi::FromInt(height - 1));
  output_frame->SetFrameSlot(output_offset, value);
1109 1110 1111
  DebugPrintOutputSlot(value, frame_index, output_offset, "argc ");
  if (trace_scope_ != nullptr) {
    PrintF(trace_scope_->file(), "(%d)\n", height - 1);
1112 1113
  }

1114
  DCHECK(0 == output_offset);
1115 1116 1117 1118 1119 1120 1121 1122

  Builtins* builtins = isolate_->builtins();
  Code* adaptor_trampoline =
      builtins->builtin(Builtins::kArgumentsAdaptorTrampoline);
  intptr_t pc_value = reinterpret_cast<intptr_t>(
      adaptor_trampoline->instruction_start() +
      isolate_->heap()->arguments_adaptor_deopt_pc_offset()->value());
  output_frame->SetPc(pc_value);
1123
  if (FLAG_enable_embedded_constant_pool) {
1124 1125 1126 1127
    intptr_t constant_pool_value =
        reinterpret_cast<intptr_t>(adaptor_trampoline->constant_pool());
    output_frame->SetConstantPool(constant_pool_value);
  }
1128 1129 1130
}


1131
void Deoptimizer::DoComputeConstructStubFrame(int frame_index) {
1132 1133 1134 1135 1136
  TranslatedFrame* translated_frame =
      &(translated_state_.frames()[frame_index]);
  TranslatedFrame::iterator value_iterator = translated_frame->begin();
  int input_index = 0;

1137 1138
  Builtins* builtins = isolate_->builtins();
  Code* construct_stub = builtins->builtin(Builtins::kJSConstructStubGeneric);
1139
  unsigned height = translated_frame->height();
1140
  unsigned height_in_bytes = height * kPointerSize;
1141 1142
  JSFunction* function = JSFunction::cast(value_iterator->GetRawValue());
  value_iterator++;
1143
  input_index++;
1144 1145 1146
  if (trace_scope_ != NULL) {
    PrintF(trace_scope_->file(),
           "  translating construct stub => height=%d\n", height_in_bytes);
1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157
  }

  unsigned fixed_frame_size = ConstructFrameConstants::kFrameSize;
  unsigned output_frame_size = height_in_bytes + fixed_frame_size;

  // Allocate and store the output frame description.
  FrameDescription* output_frame =
      new(output_frame_size) FrameDescription(output_frame_size, function);
  output_frame->SetFrameType(StackFrame::CONSTRUCT);

  // Construct stub can not be topmost or bottommost.
1158 1159
  DCHECK(frame_index > 0 && frame_index < output_count_ - 1);
  DCHECK(output_[frame_index] == NULL);
1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172
  output_[frame_index] = output_frame;

  // The top address of the frame is computed from the previous
  // frame's top and this frame's size.
  intptr_t top_address;
  top_address = output_[frame_index - 1]->GetTop() - output_frame_size;
  output_frame->SetTop(top_address);

  // Compute the incoming parameter translation.
  int parameter_count = height;
  unsigned output_offset = output_frame_size;
  for (int i = 0; i < parameter_count; ++i) {
    output_offset -= kPointerSize;
1173 1174
    // The allocated receiver of a construct stub frame is passed as the
    // receiver parameter through the translation. It might be encoding
1175
    // a captured object, override the slot address for a captured object.
1176 1177
    WriteTranslatedValueToOutput(
        &value_iterator, &input_index, frame_index, output_offset, nullptr,
1178
        (i == 0) ? reinterpret_cast<Address>(top_address) : nullptr);
1179 1180 1181
  }

  // Read caller's PC from the previous frame.
1182
  output_offset -= kPCOnStackSize;
1183
  intptr_t callers_pc = output_[frame_index - 1]->GetPc();
1184
  output_frame->SetCallerPc(output_offset, callers_pc);
1185
  DebugPrintOutputSlot(callers_pc, frame_index, output_offset, "caller's pc\n");
1186 1187

  // Read caller's FP from the previous frame, and set this frame's FP.
1188
  output_offset -= kFPOnStackSize;
1189
  intptr_t value = output_[frame_index - 1]->GetFp();
1190
  output_frame->SetCallerFp(output_offset, value);
1191 1192
  intptr_t fp_value = top_address + output_offset;
  output_frame->SetFp(fp_value);
1193
  DebugPrintOutputSlot(value, frame_index, output_offset, "caller's fp\n");
1194

1195
  if (FLAG_enable_embedded_constant_pool) {
1196
    // Read the caller's constant pool from the previous frame.
1197 1198
    output_offset -= kPointerSize;
    value = output_[frame_index - 1]->GetConstantPool();
1199
    output_frame->SetCallerConstantPool(output_offset, value);
1200 1201
    DebugPrintOutputSlot(value, frame_index, output_offset,
                         "caller's constant_pool\n");
1202 1203
  }

1204 1205 1206 1207
  // The context can be gotten from the previous frame.
  output_offset -= kPointerSize;
  value = output_[frame_index - 1]->GetContext();
  output_frame->SetFrameSlot(output_offset, value);
1208
  DebugPrintOutputSlot(value, frame_index, output_offset, "context\n");
1209 1210 1211 1212 1213

  // A marker value is used in place of the function.
  output_offset -= kPointerSize;
  value = reinterpret_cast<intptr_t>(Smi::FromInt(StackFrame::CONSTRUCT));
  output_frame->SetFrameSlot(output_offset, value);
1214 1215
  DebugPrintOutputSlot(value, frame_index, output_offset,
                       "function (construct sentinel)\n");
1216 1217 1218 1219 1220

  // The output frame reflects a JSConstructStubGeneric frame.
  output_offset -= kPointerSize;
  value = reinterpret_cast<intptr_t>(construct_stub);
  output_frame->SetFrameSlot(output_offset, value);
1221
  DebugPrintOutputSlot(value, frame_index, output_offset, "code object\n");
1222

1223 1224 1225 1226 1227 1228
  // The allocation site.
  output_offset -= kPointerSize;
  value = reinterpret_cast<intptr_t>(isolate_->heap()->undefined_value());
  output_frame->SetFrameSlot(output_offset, value);
  DebugPrintOutputSlot(value, frame_index, output_offset, "allocation site\n");

1229 1230 1231 1232
  // Number of incoming arguments.
  output_offset -= kPointerSize;
  value = reinterpret_cast<intptr_t>(Smi::FromInt(height - 1));
  output_frame->SetFrameSlot(output_offset, value);
1233 1234 1235
  DebugPrintOutputSlot(value, frame_index, output_offset, "argc ");
  if (trace_scope_ != nullptr) {
    PrintF(trace_scope_->file(), "(%d)\n", height - 1);
1236 1237
  }

1238
  // The new target.
1239 1240 1241 1242 1243
  output_offset -= kPointerSize;
  value = reinterpret_cast<intptr_t>(isolate_->heap()->undefined_value());
  output_frame->SetFrameSlot(output_offset, value);
  DebugPrintOutputSlot(value, frame_index, output_offset, "new.target\n");

1244 1245 1246 1247 1248
  // The newly allocated object was passed as receiver in the artificial
  // constructor stub environment created by HEnvironment::CopyForInlining().
  output_offset -= kPointerSize;
  value = output_frame->GetFrameSlot(output_frame_size - kPointerSize);
  output_frame->SetFrameSlot(output_offset, value);
1249 1250
  DebugPrintOutputSlot(value, frame_index, output_offset,
                       "allocated receiver\n");
1251

1252
  CHECK_EQ(0u, output_offset);
1253 1254 1255 1256 1257

  intptr_t pc = reinterpret_cast<intptr_t>(
      construct_stub->instruction_start() +
      isolate_->heap()->construct_stub_deopt_pc_offset()->value());
  output_frame->SetPc(pc);
1258
  if (FLAG_enable_embedded_constant_pool) {
1259 1260 1261 1262
    intptr_t constant_pool_value =
        reinterpret_cast<intptr_t>(construct_stub->constant_pool());
    output_frame->SetConstantPool(constant_pool_value);
  }
1263 1264 1265
}


1266
void Deoptimizer::DoComputeAccessorStubFrame(int frame_index,
1267
                                             bool is_setter_stub_frame) {
1268 1269 1270 1271 1272
  TranslatedFrame* translated_frame =
      &(translated_state_.frames()[frame_index]);
  TranslatedFrame::iterator value_iterator = translated_frame->begin();
  int input_index = 0;

1273 1274
  JSFunction* accessor = JSFunction::cast(value_iterator->GetRawValue());
  value_iterator++;
1275
  input_index++;
1276 1277 1278 1279 1280 1281
  // The receiver (and the implicit return value, if any) are expected in
  // registers by the LoadIC/StoreIC, so they don't belong to the output stack
  // frame. This means that we have to use a height of 0.
  unsigned height = 0;
  unsigned height_in_bytes = height * kPointerSize;
  const char* kind = is_setter_stub_frame ? "setter" : "getter";
1282 1283 1284
  if (trace_scope_ != NULL) {
    PrintF(trace_scope_->file(),
           "  translating %s stub => height=%u\n", kind, height_in_bytes);
1285 1286
  }

1287
  // We need 1 stack entry for the return address and enough entries for the
1288
  // StackFrame::INTERNAL (FP, context, frame type, code object and constant
1289
  // pool (if enabled)- see MacroAssembler::EnterFrame).
1290 1291
  // For a setter stub frame we need one additional entry for the implicit
  // return value, see StoreStubCompiler::CompileStoreViaSetter.
1292 1293 1294
  unsigned fixed_frame_entries =
      (StandardFrameConstants::kFixedFrameSize / kPointerSize) + 1 +
      (is_setter_stub_frame ? 1 : 0);
1295 1296 1297 1298 1299 1300 1301 1302 1303
  unsigned fixed_frame_size = fixed_frame_entries * kPointerSize;
  unsigned output_frame_size = height_in_bytes + fixed_frame_size;

  // Allocate and store the output frame description.
  FrameDescription* output_frame =
      new(output_frame_size) FrameDescription(output_frame_size, accessor);
  output_frame->SetFrameType(StackFrame::INTERNAL);

  // A frame for an accessor stub can not be the topmost or bottommost one.
1304
  CHECK(frame_index > 0 && frame_index < output_count_ - 1);
1305
  CHECK_NULL(output_[frame_index]);
1306 1307 1308 1309 1310 1311 1312 1313 1314 1315
  output_[frame_index] = output_frame;

  // The top address of the frame is computed from the previous frame's top and
  // this frame's size.
  intptr_t top_address = output_[frame_index - 1]->GetTop() - output_frame_size;
  output_frame->SetTop(top_address);

  unsigned output_offset = output_frame_size;

  // Read caller's PC from the previous frame.
1316
  output_offset -= kPCOnStackSize;
1317
  intptr_t callers_pc = output_[frame_index - 1]->GetPc();
1318
  output_frame->SetCallerPc(output_offset, callers_pc);
1319
  DebugPrintOutputSlot(callers_pc, frame_index, output_offset, "caller's pc\n");
1320 1321

  // Read caller's FP from the previous frame, and set this frame's FP.
1322
  output_offset -= kFPOnStackSize;
1323
  intptr_t value = output_[frame_index - 1]->GetFp();
1324
  output_frame->SetCallerFp(output_offset, value);
1325 1326
  intptr_t fp_value = top_address + output_offset;
  output_frame->SetFp(fp_value);
1327
  DebugPrintOutputSlot(value, frame_index, output_offset, "caller's fp\n");
1328

1329
  if (FLAG_enable_embedded_constant_pool) {
1330
    // Read the caller's constant pool from the previous frame.
1331 1332
    output_offset -= kPointerSize;
    value = output_[frame_index - 1]->GetConstantPool();
1333
    output_frame->SetCallerConstantPool(output_offset, value);
1334 1335
    DebugPrintOutputSlot(value, frame_index, output_offset,
                         "caller's constant_pool\n");
1336 1337
  }

1338 1339 1340 1341
  // The context can be gotten from the previous frame.
  output_offset -= kPointerSize;
  value = output_[frame_index - 1]->GetContext();
  output_frame->SetFrameSlot(output_offset, value);
1342
  DebugPrintOutputSlot(value, frame_index, output_offset, "context\n");
1343 1344 1345 1346 1347

  // A marker value is used in place of the function.
  output_offset -= kPointerSize;
  value = reinterpret_cast<intptr_t>(Smi::FromInt(StackFrame::INTERNAL));
  output_frame->SetFrameSlot(output_offset, value);
1348 1349 1350
  DebugPrintOutputSlot(value, frame_index, output_offset, "function ");
  if (trace_scope_ != nullptr) {
    PrintF(trace_scope_->file(), "(%s sentinel)\n", kind);
1351 1352 1353 1354 1355 1356 1357 1358 1359 1360
  }

  // Get Code object from accessor stub.
  output_offset -= kPointerSize;
  Builtins::Name name = is_setter_stub_frame ?
      Builtins::kStoreIC_Setter_ForDeopt :
      Builtins::kLoadIC_Getter_ForDeopt;
  Code* accessor_stub = isolate_->builtins()->builtin(name);
  value = reinterpret_cast<intptr_t>(accessor_stub);
  output_frame->SetFrameSlot(output_offset, value);
1361
  DebugPrintOutputSlot(value, frame_index, output_offset, "code object\n");
1362 1363

  // Skip receiver.
1364
  value_iterator++;
1365
  input_index++;
1366 1367 1368 1369 1370

  if (is_setter_stub_frame) {
    // The implicit return value was part of the artificial setter stub
    // environment.
    output_offset -= kPointerSize;
1371 1372
    WriteTranslatedValueToOutput(&value_iterator, &input_index, frame_index,
                                 output_offset);
1373 1374
  }

1375
  CHECK_EQ(0u, output_offset);
1376 1377 1378 1379 1380 1381 1382

  Smi* offset = is_setter_stub_frame ?
      isolate_->heap()->setter_stub_deopt_pc_offset() :
      isolate_->heap()->getter_stub_deopt_pc_offset();
  intptr_t pc = reinterpret_cast<intptr_t>(
      accessor_stub->instruction_start() + offset->value());
  output_frame->SetPc(pc);
1383
  if (FLAG_enable_embedded_constant_pool) {
1384 1385 1386 1387
    intptr_t constant_pool_value =
        reinterpret_cast<intptr_t>(accessor_stub->constant_pool());
    output_frame->SetConstantPool(constant_pool_value);
  }
1388 1389 1390
}


1391
void Deoptimizer::DoComputeCompiledStubFrame(int frame_index) {
1392 1393 1394 1395 1396 1397 1398 1399
  //
  //               FROM                                  TO
  //    |          ....           |          |          ....           |
  //    +-------------------------+          +-------------------------+
  //    | JSFunction continuation |          | JSFunction continuation |
  //    +-------------------------+          +-------------------------+
  // |  |    saved frame (FP)     |          |    saved frame (FP)     |
  // |  +=========================+<-fpreg   +=========================+<-fpreg
1400 1401
  // |  |constant pool (if ool_cp)|          |constant pool (if ool_cp)|
  // |  +-------------------------+          +-------------------------|
1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422
  // |  |   JSFunction context    |          |   JSFunction context    |
  // v  +-------------------------+          +-------------------------|
  //    |   COMPILED_STUB marker  |          |   STUB_FAILURE marker   |
  //    +-------------------------+          +-------------------------+
  //    |                         |          |  caller args.arguments_ |
  //    | ...                     |          +-------------------------+
  //    |                         |          |  caller args.length_    |
  //    |-------------------------|<-spreg   +-------------------------+
  //                                         |  caller args pointer    |
  //                                         +-------------------------+
  //                                         |  caller stack param 1   |
  //      parameters in registers            +-------------------------+
  //       and spilled to stack              |           ....          |
  //                                         +-------------------------+
  //                                         |  caller stack param n   |
  //                                         +-------------------------+<-spreg
  //                                         reg = number of parameters
  //                                         reg = failure handler address
  //                                         reg = saved frame
  //                                         reg = JSFunction context
  //
1423 1424 1425
  // Caller stack params contain the register parameters to the stub first,
  // and then, if the descriptor specifies a constant number of stack
  // parameters, the stack parameters as well.
1426

1427 1428 1429 1430 1431
  TranslatedFrame* translated_frame =
      &(translated_state_.frames()[frame_index]);
  TranslatedFrame::iterator value_iterator = translated_frame->begin();
  int input_index = 0;

1432
  CHECK(compiled_code_->is_hydrogen_stub());
1433
  int major_key = CodeStub::GetMajorKey(compiled_code_);
1434
  CodeStubDescriptor descriptor(isolate_, compiled_code_->stub_key());
1435 1436 1437 1438 1439

  // The output frame must have room for all pushed register parameters
  // and the standard stack frame slots.  Include space for an argument
  // object to the callee and optionally the space to pass the argument
  // object to the stub failure handler.
1440
  int param_count = descriptor.GetRegisterParameterCount();
1441
  int stack_param_count = descriptor.GetStackParameterCount();
1442
  CHECK_EQ(translated_frame->height(), param_count);
1443 1444
  CHECK_GE(param_count, 0);

1445 1446
  int height_in_bytes = kPointerSize * (param_count + stack_param_count) +
                        sizeof(Arguments) + kPointerSize;
1447 1448 1449
  int fixed_frame_size = StandardFrameConstants::kFixedFrameSize;
  int input_frame_size = input_->GetFrameSize();
  int output_frame_size = height_in_bytes + fixed_frame_size;
1450 1451
  if (trace_scope_ != NULL) {
    PrintF(trace_scope_->file(),
verwaest@chromium.org's avatar
verwaest@chromium.org committed
1452
           "  translating %s => StubFailureTrampolineStub, height=%d\n",
1453
           CodeStub::MajorName(static_cast<CodeStub::Major>(major_key)),
1454 1455 1456 1457 1458 1459 1460
           height_in_bytes);
  }

  // The stub failure trampoline is a single frame.
  FrameDescription* output_frame =
      new(output_frame_size) FrameDescription(output_frame_size, NULL);
  output_frame->SetFrameType(StackFrame::STUB_FAILURE_TRAMPOLINE);
1461
  CHECK_EQ(frame_index, 0);
1462 1463 1464 1465 1466 1467 1468
  output_[frame_index] = output_frame;

  // The top address for the output frame can be computed from the input
  // frame pointer and the output frame's height. Subtract space for the
  // context and function slots.
  Register fp_reg = StubFailureTrampolineFrame::fp_register();
  intptr_t top_address = input_->GetRegister(fp_reg.code()) -
1469
      StandardFrameConstants::kFixedFrameSizeFromFp - height_in_bytes;
1470 1471 1472
  output_frame->SetTop(top_address);

  // Read caller's PC (JSFunction continuation) from the input frame.
1473 1474
  unsigned input_frame_offset = input_frame_size - kPCOnStackSize;
  unsigned output_frame_offset = output_frame_size - kFPOnStackSize;
1475
  intptr_t value = input_->GetFrameSlot(input_frame_offset);
1476
  output_frame->SetCallerPc(output_frame_offset, value);
1477 1478
  DebugPrintOutputSlot(value, frame_index, output_frame_offset,
                       "caller's pc\n");
1479 1480

  // Read caller's FP from the input frame, and set this frame's FP.
1481
  input_frame_offset -= kFPOnStackSize;
1482
  value = input_->GetFrameSlot(input_frame_offset);
1483 1484
  output_frame_offset -= kFPOnStackSize;
  output_frame->SetCallerFp(output_frame_offset, value);
1485 1486 1487
  intptr_t frame_ptr = input_->GetRegister(fp_reg.code());
  output_frame->SetRegister(fp_reg.code(), frame_ptr);
  output_frame->SetFp(frame_ptr);
1488 1489
  DebugPrintOutputSlot(value, frame_index, output_frame_offset,
                       "caller's fp\n");
1490

1491
  if (FLAG_enable_embedded_constant_pool) {
1492
    // Read the caller's constant pool from the input frame.
1493 1494 1495
    input_frame_offset -= kPointerSize;
    value = input_->GetFrameSlot(input_frame_offset);
    output_frame_offset -= kPointerSize;
1496
    output_frame->SetCallerConstantPool(output_frame_offset, value);
1497 1498
    DebugPrintOutputSlot(value, frame_index, output_frame_offset,
                         "caller's constant_pool\n");
1499 1500
  }

1501 1502 1503 1504 1505 1506 1507
  // The context can be gotten from the input frame.
  Register context_reg = StubFailureTrampolineFrame::context_register();
  input_frame_offset -= kPointerSize;
  value = input_->GetFrameSlot(input_frame_offset);
  output_frame->SetRegister(context_reg.code(), value);
  output_frame_offset -= kPointerSize;
  output_frame->SetFrameSlot(output_frame_offset, value);
1508
  CHECK(reinterpret_cast<Object*>(value)->IsContext());
1509
  DebugPrintOutputSlot(value, frame_index, output_frame_offset, "context\n");
1510 1511 1512 1513 1514 1515

  // A marker value is used in place of the function.
  output_frame_offset -= kPointerSize;
  value = reinterpret_cast<intptr_t>(
      Smi::FromInt(StackFrame::STUB_FAILURE_TRAMPOLINE));
  output_frame->SetFrameSlot(output_frame_offset, value);
1516 1517
  DebugPrintOutputSlot(value, frame_index, output_frame_offset,
                       "function (stub failure sentinel)\n");
1518

1519
  intptr_t caller_arg_count = stack_param_count;
1520
  bool arg_count_known = !descriptor.stack_parameter_count().is_valid();
1521 1522 1523

  // Build the Arguments object for the caller's parameters and a pointer to it.
  output_frame_offset -= kPointerSize;
1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534
  int args_arguments_offset = output_frame_offset;
  intptr_t the_hole = reinterpret_cast<intptr_t>(
      isolate_->heap()->the_hole_value());
  if (arg_count_known) {
    value = frame_ptr + StandardFrameConstants::kCallerSPOffset +
        (caller_arg_count - 1) * kPointerSize;
  } else {
    value = the_hole;
  }

  output_frame->SetFrameSlot(args_arguments_offset, value);
1535 1536 1537
  DebugPrintOutputSlot(
      value, frame_index, args_arguments_offset,
      arg_count_known ? "args.arguments\n" : "args.arguments (the hole)\n");
1538 1539

  output_frame_offset -= kPointerSize;
1540 1541 1542
  int length_frame_offset = output_frame_offset;
  value = arg_count_known ? caller_arg_count : the_hole;
  output_frame->SetFrameSlot(length_frame_offset, value);
1543 1544 1545
  DebugPrintOutputSlot(
      value, frame_index, length_frame_offset,
      arg_count_known ? "args.length\n" : "args.length (the hole)\n");
1546 1547

  output_frame_offset -= kPointerSize;
1548 1549
  value = frame_ptr + StandardFrameConstants::kCallerSPOffset -
      (output_frame_size - output_frame_offset) + kPointerSize;
1550
  output_frame->SetFrameSlot(output_frame_offset, value);
1551
  DebugPrintOutputSlot(value, frame_index, output_frame_offset, "args*\n");
1552 1553

  // Copy the register parameters to the failure frame.
1554
  int arguments_length_offset = -1;
1555
  for (int i = 0; i < param_count; ++i) {
1556
    output_frame_offset -= kPointerSize;
1557 1558
    WriteTranslatedValueToOutput(&value_iterator, &input_index, 0,
                                 output_frame_offset);
1559

1560 1561 1562
    if (!arg_count_known &&
        descriptor.GetRegisterParameter(i)
            .is(descriptor.stack_parameter_count())) {
1563 1564
      arguments_length_offset = output_frame_offset;
    }
1565 1566
  }

1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580
  // Copy constant stack parameters to the failure frame. If the number of stack
  // parameters is not known in the descriptor, the arguments object is the way
  // to access them.
  for (int i = 0; i < stack_param_count; i++) {
    output_frame_offset -= kPointerSize;
    Object** stack_parameter = reinterpret_cast<Object**>(
        frame_ptr + StandardFrameConstants::kCallerSPOffset +
        (stack_param_count - i - 1) * kPointerSize);
    value = reinterpret_cast<intptr_t>(*stack_parameter);
    output_frame->SetFrameSlot(output_frame_offset, value);
    DebugPrintOutputSlot(value, frame_index, output_frame_offset,
                         "stack parameter\n");
  }

1581
  CHECK_EQ(0u, output_frame_offset);
1582

1583
  if (!arg_count_known) {
1584
    CHECK_GE(arguments_length_offset, 0);
1585 1586 1587 1588 1589 1590 1591
    // We know it's a smi because 1) the code stub guarantees the stack
    // parameter count is in smi range, and 2) the DoTranslateCommand in the
    // parameter loop above translated that to a tagged value.
    Smi* smi_caller_arg_count = reinterpret_cast<Smi*>(
        output_frame->GetFrameSlot(arguments_length_offset));
    caller_arg_count = smi_caller_arg_count->value();
    output_frame->SetFrameSlot(length_frame_offset, caller_arg_count);
1592 1593
    DebugPrintOutputSlot(caller_arg_count, frame_index, length_frame_offset,
                         "args.length\n");
1594 1595 1596
    value = frame_ptr + StandardFrameConstants::kCallerSPOffset +
        (caller_arg_count - 1) * kPointerSize;
    output_frame->SetFrameSlot(args_arguments_offset, value);
1597 1598
    DebugPrintOutputSlot(value, frame_index, args_arguments_offset,
                         "args.arguments");
1599 1600
  }

1601 1602 1603 1604
  // Copy the double registers from the input into the output frame.
  CopyDoubleRegisters(output_frame);

  // Fill registers containing handler and number of parameters.
1605
  SetPlatformCompiledStubRegisters(output_frame, &descriptor);
1606 1607 1608

  // Compute this frame's PC, state, and continuation.
  Code* trampoline = NULL;
1609
  StubFunctionMode function_mode = descriptor.function_mode();
1610 1611
  StubFailureTrampolineStub(isolate_, function_mode)
      .FindCodeInCache(&trampoline);
1612
  DCHECK(trampoline != NULL);
1613 1614
  output_frame->SetPc(reinterpret_cast<intptr_t>(
      trampoline->instruction_start()));
1615
  if (FLAG_enable_embedded_constant_pool) {
1616 1617 1618 1619 1620 1621 1622
    Register constant_pool_reg =
        StubFailureTrampolineFrame::constant_pool_pointer_register();
    intptr_t constant_pool_value =
        reinterpret_cast<intptr_t>(trampoline->constant_pool());
    output_frame->SetConstantPool(constant_pool_value);
    output_frame->SetRegister(constant_pool_reg.code(), constant_pool_value);
  }
1623
  output_frame->SetState(Smi::FromInt(FullCodeGenerator::NO_REGISTERS));
1624 1625
  Code* notify_failure =
      isolate_->builtins()->builtin(Builtins::kNotifyStubFailureSaveDoubles);
1626 1627 1628 1629 1630
  output_frame->SetContinuation(
      reinterpret_cast<intptr_t>(notify_failure->entry()));
}


1631
void Deoptimizer::MaterializeHeapObjects(JavaScriptFrameIterator* it) {
1632
  DCHECK_NE(DEBUGGER, bailout_type_);
1633

1634 1635
  // Walk to the last JavaScript output frame to find out if it has
  // adapted arguments.
1636 1637
  for (int frame_index = 0; frame_index < jsframe_count(); ++frame_index) {
    if (frame_index != 0) it->Advance();
1638
  }
1639
  translated_state_.Prepare(it->frame()->has_adapted_arguments(), stack_fp_);
1640

1641 1642
  for (auto& materialization : values_to_materialize_) {
    Handle<Object> value = materialization.value_->GetValue();
1643

1644 1645 1646 1647 1648 1649
    if (trace_scope_ != nullptr) {
      PrintF("Materialization [0x%08" V8PRIxPTR "] <- 0x%08" V8PRIxPTR " ;  ",
             reinterpret_cast<intptr_t>(materialization.output_slot_address_),
             reinterpret_cast<intptr_t>(*value));
      value->ShortPrint(trace_scope_->file());
      PrintF(trace_scope_->file(), "\n");
1650
    }
1651

1652 1653
    *(reinterpret_cast<intptr_t*>(materialization.output_slot_address_)) =
        reinterpret_cast<intptr_t>(*value);
1654
  }
jarin@chromium.org's avatar
jarin@chromium.org committed
1655

1656
  isolate_->materialized_object_store()->Remove(stack_fp_);
1657 1658 1659
}


1660
void Deoptimizer::MaterializeHeapNumbersForDebuggerInspectableFrame(
1661
    int frame_index, int parameter_count, int expression_count,
1662
    DeoptimizedFrameInfo* info) {
1663
  CHECK_EQ(DEBUGGER, bailout_type_);
1664

1665 1666 1667 1668
  translated_state_.Prepare(false, nullptr);

  TranslatedFrame* frame = &(translated_state_.frames()[frame_index]);
  CHECK(frame->kind() == TranslatedFrame::kFunction);
1669
  int frame_arg_count = frame->shared_info()->internal_formal_parameter_count();
1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681

  // The height is #expressions + 1 for context.
  CHECK_EQ(expression_count + 1, frame->height());
  TranslatedFrame* argument_frame = frame;
  if (frame_index > 0) {
    TranslatedFrame* previous_frame =
        &(translated_state_.frames()[frame_index - 1]);
    if (previous_frame->kind() == TranslatedFrame::kArgumentsAdaptor) {
      argument_frame = previous_frame;
      CHECK_EQ(parameter_count, argument_frame->height() - 1);
    } else {
      CHECK_EQ(frame_arg_count, parameter_count);
1682
    }
1683 1684
  } else {
    CHECK_EQ(frame_arg_count, parameter_count);
1685 1686
  }

1687
  TranslatedFrame::iterator arg_iter = argument_frame->begin();
1688
  arg_iter++;  // Skip the function.
1689 1690 1691 1692 1693 1694
  arg_iter++;  // Skip the receiver.
  for (int i = 0; i < parameter_count; i++, arg_iter++) {
    if (!arg_iter->IsMaterializedObject()) {
      info->SetParameter(i, *(arg_iter->GetValue()));
    }
  }
1695

1696
  TranslatedFrame::iterator iter = frame->begin();
1697 1698
  // Skip the function, receiver, context and arguments.
  for (int i = 0; i < frame_arg_count + 3; i++, iter++) {
1699 1700
  }

1701 1702 1703 1704 1705
  for (int i = 0; i < expression_count; i++, iter++) {
    if (!iter->IsMaterializedObject()) {
      info->SetExpression(i, *(iter->GetValue()));
    }
  }
1706 1707 1708
}


1709
void Deoptimizer::WriteTranslatedValueToOutput(
1710
    TranslatedFrame::iterator* iterator, int* input_index, int frame_index,
1711 1712
    unsigned output_offset, const char* debug_hint_string,
    Address output_address_for_materialization) {
1713
  Object* value = (*iterator)->GetRawValue();
1714

1715 1716
  WriteValueToOutput(value, *input_index, frame_index, output_offset,
                     debug_hint_string);
1717

1718
  if (value == isolate_->heap()->arguments_marker()) {
1719 1720 1721
    Address output_address =
        reinterpret_cast<Address>(output_[frame_index]->GetTop()) +
        output_offset;
1722 1723
    if (output_address_for_materialization == nullptr) {
      output_address_for_materialization = output_address;
1724
    }
1725 1726 1727
    values_to_materialize_.push_back(
        {output_address_for_materialization, *iterator});
  }
1728

1729 1730 1731
  (*iterator)++;
  (*input_index)++;
}
1732 1733


1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763
void Deoptimizer::WriteValueToOutput(Object* value, int input_index,
                                     int frame_index, unsigned output_offset,
                                     const char* debug_hint_string) {
  output_[frame_index]->SetFrameSlot(output_offset,
                                     reinterpret_cast<intptr_t>(value));

  if (trace_scope_ != nullptr) {
    DebugPrintOutputSlot(reinterpret_cast<intptr_t>(value), frame_index,
                         output_offset, debug_hint_string);
    value->ShortPrint(trace_scope_->file());
    PrintF(trace_scope_->file(), "  (input #%d)\n", input_index);
  }
}


void Deoptimizer::DebugPrintOutputSlot(intptr_t value, int frame_index,
                                       unsigned output_offset,
                                       const char* debug_hint_string) {
  if (trace_scope_ != nullptr) {
    Address output_address =
        reinterpret_cast<Address>(output_[frame_index]->GetTop()) +
        output_offset;
    PrintF(trace_scope_->file(),
           "    0x%08" V8PRIxPTR ": [top + %d] <- 0x%08" V8PRIxPTR " ;  %s",
           reinterpret_cast<intptr_t>(output_address), output_offset, value,
           debug_hint_string == nullptr ? "" : debug_hint_string);
  }
}


1764 1765 1766 1767 1768 1769 1770 1771
unsigned Deoptimizer::ComputeInputFrameSize() const {
  unsigned fixed_size = ComputeFixedSize(function_);
  // The fp-to-sp delta already takes the context, constant pool pointer and the
  // function into account so we have to avoid double counting them.
  unsigned result = fixed_size + fp_to_sp_delta_ -
                    StandardFrameConstants::kFixedFrameSizeFromFp;
  if (compiled_code_->kind() == Code::OPTIMIZED_FUNCTION) {
    unsigned stack_slots = compiled_code_->stack_slots();
1772 1773
    unsigned outgoing_size =
        ComputeOutgoingArgumentSize(compiled_code_, bailout_id_);
1774
    CHECK(result == fixed_size + (stack_slots * kPointerSize) + outgoing_size);
1775
  }
1776
  return result;
1777 1778 1779
}


1780 1781 1782 1783 1784 1785
unsigned Deoptimizer::ComputeFixedSize(JSFunction* function) const {
  // The fixed part of the frame consists of the return address, frame
  // pointer, function, context, and all the incoming arguments.
  return ComputeIncomingArgumentSize(function) +
         StandardFrameConstants::kFixedFrameSize;
}
1786 1787


1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798
unsigned Deoptimizer::ComputeIncomingArgumentSize(JSFunction* function) const {
  // The incoming arguments is the values for formal parameters and
  // the receiver. Every slot contains a pointer.
  if (function->IsSmi()) {
    CHECK_EQ(Smi::cast(function), Smi::FromInt(StackFrame::STUB));
    return 0;
  }
  unsigned arguments =
      function->shared()->internal_formal_parameter_count() + 1;
  return arguments * kPointerSize;
}
1799 1800


1801 1802 1803
// static
unsigned Deoptimizer::ComputeOutgoingArgumentSize(Code* code,
                                                  unsigned bailout_id) {
1804
  DeoptimizationInputData* data =
1805 1806
      DeoptimizationInputData::cast(code->deoptimization_data());
  unsigned height = data->ArgumentsStackHeight(bailout_id)->value();
1807 1808
  return height * kPointerSize;
}
1809 1810


1811 1812 1813 1814 1815 1816
Object* Deoptimizer::ComputeLiteral(int index) const {
  DeoptimizationInputData* data =
      DeoptimizationInputData::cast(compiled_code_->deoptimization_data());
  FixedArray* literals = data->LiteralArray();
  return literals->get(index);
}
1817

1818

1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832
void Deoptimizer::EnsureCodeForDeoptimizationEntry(Isolate* isolate,
                                                   BailoutType type,
                                                   int max_entry_id) {
  // We cannot run this if the serializer is enabled because this will
  // cause us to emit relocation information for the external
  // references. This is fine because the deoptimizer's code section
  // isn't meant to be serialized at all.
  CHECK(type == EAGER || type == SOFT || type == LAZY);
  DeoptimizerData* data = isolate->deoptimizer_data();
  int entry_count = data->deopt_entry_code_entries_[type];
  if (max_entry_id < entry_count) return;
  entry_count = Max(entry_count, Deoptimizer::kMinNumberOfEntries);
  while (max_entry_id >= entry_count) entry_count *= 2;
  CHECK(entry_count <= Deoptimizer::kMaxNumberOfEntries);
1833

1834
  MacroAssembler masm(isolate, NULL, 16 * KB, CodeObjectRequired::kYes);
1835 1836 1837 1838 1839
  masm.set_emit_debug_code(false);
  GenerateDeoptimizationEntries(&masm, entry_count, type);
  CodeDesc desc;
  masm.GetCode(&desc);
  DCHECK(!RelocInfo::RequiresRelocation(desc));
1840

1841 1842 1843 1844 1845 1846 1847 1848 1849
  MemoryChunk* chunk = data->deopt_entry_code_[type];
  CHECK(static_cast<int>(Deoptimizer::GetMaxDeoptTableSize()) >=
        desc.instr_size);
  if (!chunk->CommitArea(desc.instr_size)) {
    V8::FatalProcessOutOfMemory(
        "Deoptimizer::EnsureCodeForDeoptimizationEntry");
  }
  CopyBytes(chunk->area_start(), desc.buffer,
            static_cast<size_t>(desc.instr_size));
1850
  Assembler::FlushICache(isolate, chunk->area_start(), desc.instr_size);
1851

1852 1853
  data->deopt_entry_code_entries_[type] = entry_count;
}
1854 1855 1856 1857 1858 1859 1860 1861


FrameDescription::FrameDescription(uint32_t frame_size,
                                   JSFunction* function)
    : frame_size_(frame_size),
      function_(function),
      top_(kZapUint32),
      pc_(kZapUint32),
1862
      fp_(kZapUint32),
1863 1864
      context_(kZapUint32),
      constant_pool_(kZapUint32) {
1865 1866
  // Zap all the registers.
  for (int r = 0; r < Register::kNumRegisters; r++) {
1867 1868 1869
    // TODO(jbramley): It isn't safe to use kZapUint32 here. If the register
    // isn't used before the next safepoint, the GC will try to scan it as a
    // tagged value. kZapUint32 looks like a valid tagged pointer, but it isn't.
1870 1871 1872 1873 1874 1875 1876 1877 1878 1879
    SetRegister(r, kZapUint32);
  }

  // Zap all the slots.
  for (unsigned o = 0; o < frame_size; o += kPointerSize) {
    SetFrameSlot(o, kZapUint32);
  }
}


1880 1881 1882 1883 1884 1885 1886
int FrameDescription::ComputeFixedSize() {
  return StandardFrameConstants::kFixedFrameSize +
      (ComputeParametersCount() + 1) * kPointerSize;
}


unsigned FrameDescription::GetOffsetFromSlotIndex(int slot_index) {
1887 1888 1889
  if (slot_index >= 0) {
    // Local or spill slots. Skip the fixed part of the frame
    // including all arguments.
1890
    unsigned base = GetFrameSize() - ComputeFixedSize();
1891 1892 1893
    return base - ((slot_index + 1) * kPointerSize);
  } else {
    // Incoming parameter.
1894 1895
    int arg_size = (ComputeParametersCount() + 1) * kPointerSize;
    unsigned base = GetFrameSize() - arg_size;
1896 1897 1898 1899 1900
    return base - ((slot_index + 1) * kPointerSize);
  }
}


1901
int FrameDescription::ComputeParametersCount() {
1902 1903
  switch (type_) {
    case StackFrame::JAVA_SCRIPT:
1904
      return function_->shared()->internal_formal_parameter_count();
1905 1906 1907 1908 1909
    case StackFrame::ARGUMENTS_ADAPTOR: {
      // Last slot contains number of incomming arguments as a smi.
      // Can't use GetExpression(0) because it would cause infinite recursion.
      return reinterpret_cast<Smi*>(*GetFrameSlotPointer(0))->value();
    }
1910
    case StackFrame::STUB:
1911
      return -1;  // Minus receiver.
1912
    default:
1913
      FATAL("Unexpected stack frame type");
1914 1915
      return 0;
  }
1916 1917 1918
}


1919
Object* FrameDescription::GetParameter(int index) {
1920 1921
  CHECK_GE(index, 0);
  CHECK_LT(index, ComputeParametersCount());
1922
  // The slot indexes for incoming arguments are negative.
1923
  unsigned offset = GetOffsetFromSlotIndex(index - ComputeParametersCount());
1924 1925 1926 1927
  return reinterpret_cast<Object*>(*GetFrameSlotPointer(offset));
}


1928
unsigned FrameDescription::GetExpressionCount() {
1929
  CHECK_EQ(StackFrame::JAVA_SCRIPT, type_);
1930
  unsigned size = GetFrameSize() - ComputeFixedSize();
1931
  return size / kPointerSize;
1932 1933 1934
}


1935
Object* FrameDescription::GetExpression(int index) {
1936
  DCHECK_EQ(StackFrame::JAVA_SCRIPT, type_);
1937
  unsigned offset = GetOffsetFromSlotIndex(index);
1938 1939 1940 1941
  return reinterpret_cast<Object*>(*GetFrameSlotPointer(offset));
}


1942
void TranslationBuffer::Add(int32_t value, Zone* zone) {
1943 1944
  // This wouldn't handle kMinInt correctly if it ever encountered it.
  DCHECK(value != kMinInt);
1945 1946 1947 1948 1949 1950 1951 1952
  // Encode the sign bit in the least significant bit.
  bool is_negative = (value < 0);
  uint32_t bits = ((is_negative ? -value : value) << 1) |
      static_cast<int32_t>(is_negative);
  // Encode the individual bytes using the least significant bit of
  // each byte to indicate whether or not more bytes follow.
  do {
    uint32_t next = bits >> 7;
1953
    contents_.Add(((bits << 1) & 0xFF) | (next != 0), zone);
1954 1955 1956 1957 1958 1959 1960 1961 1962 1963
    bits = next;
  } while (bits != 0);
}


int32_t TranslationIterator::Next() {
  // Run through the bytes until we reach one with a least significant
  // bit of zero (marks the end).
  uint32_t bits = 0;
  for (int i = 0; true; i += 7) {
1964
    DCHECK(HasNext());
1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975
    uint8_t next = buffer_->get(index_++);
    bits |= (next >> 1) << i;
    if ((next & 1) == 0) break;
  }
  // The bits encode the sign in the least significant bit.
  bool is_negative = (bits & 1) == 1;
  int32_t result = bits >> 1;
  return is_negative ? -result : result;
}


1976
Handle<ByteArray> TranslationBuffer::CreateByteArray(Factory* factory) {
1977
  int length = contents_.length();
1978
  Handle<ByteArray> result = factory->NewByteArray(length, TENURED);
1979
  MemCopy(result->GetDataStartAddress(), contents_.ToVector().start(), length);
1980 1981 1982 1983
  return result;
}


1984
void Translation::BeginConstructStubFrame(int literal_id, unsigned height) {
1985 1986 1987
  buffer_->Add(CONSTRUCT_STUB_FRAME, zone());
  buffer_->Add(literal_id, zone());
  buffer_->Add(height, zone());
1988 1989 1990
}


1991 1992 1993 1994 1995 1996
void Translation::BeginGetterStubFrame(int literal_id) {
  buffer_->Add(GETTER_STUB_FRAME, zone());
  buffer_->Add(literal_id, zone());
}


1997 1998 1999 2000 2001 2002
void Translation::BeginSetterStubFrame(int literal_id) {
  buffer_->Add(SETTER_STUB_FRAME, zone());
  buffer_->Add(literal_id, zone());
}


2003
void Translation::BeginArgumentsAdaptorFrame(int literal_id, unsigned height) {
2004 2005 2006
  buffer_->Add(ARGUMENTS_ADAPTOR_FRAME, zone());
  buffer_->Add(literal_id, zone());
  buffer_->Add(height, zone());
2007 2008 2009
}


2010 2011 2012
void Translation::BeginJSFrame(BailoutId node_id,
                               int literal_id,
                               unsigned height) {
2013
  buffer_->Add(JS_FRAME, zone());
2014
  buffer_->Add(node_id.ToInt(), zone());
2015 2016
  buffer_->Add(literal_id, zone());
  buffer_->Add(height, zone());
2017 2018 2019
}


2020
void Translation::BeginCompiledStubFrame(int height) {
2021
  buffer_->Add(COMPILED_STUB_FRAME, zone());
2022
  buffer_->Add(height, zone());
2023 2024 2025
}


2026 2027 2028 2029 2030 2031
void Translation::BeginArgumentsObject(int args_length) {
  buffer_->Add(ARGUMENTS_OBJECT, zone());
  buffer_->Add(args_length, zone());
}


2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043
void Translation::BeginCapturedObject(int length) {
  buffer_->Add(CAPTURED_OBJECT, zone());
  buffer_->Add(length, zone());
}


void Translation::DuplicateObject(int object_index) {
  buffer_->Add(DUPLICATED_OBJECT, zone());
  buffer_->Add(object_index, zone());
}


2044
void Translation::StoreRegister(Register reg) {
2045 2046
  buffer_->Add(REGISTER, zone());
  buffer_->Add(reg.code(), zone());
2047 2048 2049 2050
}


void Translation::StoreInt32Register(Register reg) {
2051 2052
  buffer_->Add(INT32_REGISTER, zone());
  buffer_->Add(reg.code(), zone());
2053 2054 2055
}


2056 2057 2058 2059 2060 2061
void Translation::StoreUint32Register(Register reg) {
  buffer_->Add(UINT32_REGISTER, zone());
  buffer_->Add(reg.code(), zone());
}


2062 2063 2064 2065 2066 2067
void Translation::StoreBoolRegister(Register reg) {
  buffer_->Add(BOOL_REGISTER, zone());
  buffer_->Add(reg.code(), zone());
}


2068
void Translation::StoreDoubleRegister(DoubleRegister reg) {
2069
  buffer_->Add(DOUBLE_REGISTER, zone());
2070
  buffer_->Add(reg.code(), zone());
2071 2072 2073 2074
}


void Translation::StoreStackSlot(int index) {
2075 2076
  buffer_->Add(STACK_SLOT, zone());
  buffer_->Add(index, zone());
2077 2078 2079 2080
}


void Translation::StoreInt32StackSlot(int index) {
2081 2082
  buffer_->Add(INT32_STACK_SLOT, zone());
  buffer_->Add(index, zone());
2083 2084 2085
}


2086 2087 2088 2089 2090 2091
void Translation::StoreUint32StackSlot(int index) {
  buffer_->Add(UINT32_STACK_SLOT, zone());
  buffer_->Add(index, zone());
}


2092 2093 2094 2095 2096 2097
void Translation::StoreBoolStackSlot(int index) {
  buffer_->Add(BOOL_STACK_SLOT, zone());
  buffer_->Add(index, zone());
}


2098
void Translation::StoreDoubleStackSlot(int index) {
2099 2100
  buffer_->Add(DOUBLE_STACK_SLOT, zone());
  buffer_->Add(index, zone());
2101 2102 2103 2104
}


void Translation::StoreLiteral(int literal_id) {
2105 2106
  buffer_->Add(LITERAL, zone());
  buffer_->Add(literal_id, zone());
2107 2108 2109
}


2110 2111 2112 2113 2114 2115 2116
void Translation::StoreArgumentsObject(bool args_known,
                                       int args_index,
                                       int args_length) {
  buffer_->Add(ARGUMENTS_OBJECT, zone());
  buffer_->Add(args_known, zone());
  buffer_->Add(args_index, zone());
  buffer_->Add(args_length, zone());
2117 2118 2119
}


2120 2121 2122 2123 2124
void Translation::StoreJSFrameFunction() {
  buffer_->Add(JS_FRAME_FUNCTION, zone());
}


2125 2126
int Translation::NumberOfOperandsFor(Opcode opcode) {
  switch (opcode) {
2127 2128
    case JS_FRAME_FUNCTION:
      return 0;
2129
    case GETTER_STUB_FRAME:
2130
    case SETTER_STUB_FRAME:
2131
    case DUPLICATED_OBJECT:
2132
    case ARGUMENTS_OBJECT:
2133
    case CAPTURED_OBJECT:
2134 2135
    case REGISTER:
    case INT32_REGISTER:
2136
    case UINT32_REGISTER:
2137
    case BOOL_REGISTER:
2138 2139 2140
    case DOUBLE_REGISTER:
    case STACK_SLOT:
    case INT32_STACK_SLOT:
2141
    case UINT32_STACK_SLOT:
2142
    case BOOL_STACK_SLOT:
2143 2144
    case DOUBLE_STACK_SLOT:
    case LITERAL:
2145
    case COMPILED_STUB_FRAME:
2146
      return 1;
2147 2148
    case BEGIN:
    case ARGUMENTS_ADAPTOR_FRAME:
2149
    case CONSTRUCT_STUB_FRAME:
2150 2151
      return 2;
    case JS_FRAME:
2152 2153
      return 3;
  }
2154
  FATAL("Unexpected translation type");
2155 2156 2157 2158
  return -1;
}


2159
#if defined(OBJECT_PRINT) || defined(ENABLE_DISASSEMBLER)
2160 2161

const char* Translation::StringFor(Opcode opcode) {
2162
#define TRANSLATION_OPCODE_CASE(item)   case item: return #item;
2163
  switch (opcode) {
2164
    TRANSLATION_OPCODE_LIST(TRANSLATION_OPCODE_CASE)
2165
  }
2166
#undef TRANSLATION_OPCODE_CASE
2167 2168 2169 2170 2171 2172 2173
  UNREACHABLE();
  return "";
}

#endif


jarin@chromium.org's avatar
jarin@chromium.org committed
2174 2175 2176 2177 2178 2179
Handle<FixedArray> MaterializedObjectStore::Get(Address fp) {
  int index = StackIdToIndex(fp);
  if (index == -1) {
    return Handle<FixedArray>::null();
  }
  Handle<FixedArray> array = GetStackEntries();
2180
  CHECK_GT(array->length(), index);
2181
  return Handle<FixedArray>::cast(Handle<Object>(array->get(index), isolate()));
jarin@chromium.org's avatar
jarin@chromium.org committed
2182 2183 2184 2185
}


void MaterializedObjectStore::Set(Address fp,
2186
                                  Handle<FixedArray> materialized_objects) {
jarin@chromium.org's avatar
jarin@chromium.org committed
2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197
  int index = StackIdToIndex(fp);
  if (index == -1) {
    index = frame_fps_.length();
    frame_fps_.Add(fp);
  }

  Handle<FixedArray> array = EnsureStackEntries(index + 1);
  array->set(index, *materialized_objects);
}


2198
bool MaterializedObjectStore::Remove(Address fp) {
jarin@chromium.org's avatar
jarin@chromium.org committed
2199
  int index = StackIdToIndex(fp);
2200 2201 2202
  if (index == -1) {
    return false;
  }
2203
  CHECK_GE(index, 0);
jarin@chromium.org's avatar
jarin@chromium.org committed
2204 2205

  frame_fps_.Remove(index);
2206
  FixedArray* array = isolate()->heap()->materialized_objects();
2207
  CHECK_LT(index, array->length());
jarin@chromium.org's avatar
jarin@chromium.org committed
2208 2209 2210 2211
  for (int i = index; i < frame_fps_.length(); i++) {
    array->set(i, array->get(i + 1));
  }
  array->set(frame_fps_.length(), isolate()->heap()->undefined_value());
2212
  return true;
jarin@chromium.org's avatar
jarin@chromium.org committed
2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249
}


int MaterializedObjectStore::StackIdToIndex(Address fp) {
  for (int i = 0; i < frame_fps_.length(); i++) {
    if (frame_fps_[i] == fp) {
      return i;
    }
  }
  return -1;
}


Handle<FixedArray> MaterializedObjectStore::GetStackEntries() {
  return Handle<FixedArray>(isolate()->heap()->materialized_objects());
}


Handle<FixedArray> MaterializedObjectStore::EnsureStackEntries(int length) {
  Handle<FixedArray> array = GetStackEntries();
  if (array->length() >= length) {
    return array;
  }

  int new_length = length > 10 ? length : 10;
  if (new_length < 2 * array->length()) {
    new_length = 2 * array->length();
  }

  Handle<FixedArray> new_array =
      isolate()->factory()->NewFixedArray(new_length, TENURED);
  for (int i = 0; i < array->length(); i++) {
    new_array->set(i, array->get(i));
  }
  for (int i = array->length(); i < length; i++) {
    new_array->set(i, isolate()->heap()->undefined_value());
  }
2250
  isolate()->heap()->SetRootMaterializedObjects(*new_array);
jarin@chromium.org's avatar
jarin@chromium.org committed
2251
  return new_array;
2252 2253 2254
}


2255 2256 2257 2258
DeoptimizedFrameInfo::DeoptimizedFrameInfo(Deoptimizer* deoptimizer,
                                           int frame_index,
                                           bool has_arguments_adaptor,
                                           bool has_construct_stub) {
2259
  FrameDescription* output_frame = deoptimizer->output_[frame_index];
2260
  function_ = output_frame->GetFunction();
2261
  context_ = reinterpret_cast<Object*>(output_frame->GetContext());
2262
  has_construct_stub_ = has_construct_stub;
2263
  expression_count_ = output_frame->GetExpressionCount();
2264
  expression_stack_ = new Object* [expression_count_];
2265 2266
  // Get the source position using the unoptimized code.
  Address pc = reinterpret_cast<Address>(output_frame->GetPc());
2267
  Code* code = Code::cast(deoptimizer->isolate()->FindCodeObject(pc));
2268 2269
  source_position_ = code->SourcePosition(pc);

2270
  for (int i = 0; i < expression_count_; i++) {
2271 2272 2273 2274 2275 2276
    Object* value = output_frame->GetExpression(i);
    // Replace materialization markers with the undefined value.
    if (value == deoptimizer->isolate()->heap()->arguments_marker()) {
      value = deoptimizer->isolate()->heap()->undefined_value();
    }
    SetExpression(i, value);
2277 2278 2279 2280
  }

  if (has_arguments_adaptor) {
    output_frame = deoptimizer->output_[frame_index - 1];
2281
    CHECK_EQ(output_frame->GetFrameType(), StackFrame::ARGUMENTS_ADAPTOR);
2282 2283
  }

2284
  parameters_count_ = output_frame->ComputeParametersCount();
2285
  parameters_ = new Object* [parameters_count_];
2286
  for (int i = 0; i < parameters_count_; i++) {
2287 2288 2289 2290 2291 2292
    Object* value = output_frame->GetParameter(i);
    // Replace materialization markers with the undefined value.
    if (value == deoptimizer->isolate()->heap()->arguments_marker()) {
      value = deoptimizer->isolate()->heap()->undefined_value();
    }
    SetParameter(i, value);
2293 2294 2295 2296 2297
  }
}


DeoptimizedFrameInfo::~DeoptimizedFrameInfo() {
2298 2299
  delete[] expression_stack_;
  delete[] parameters_;
2300 2301
}

2302

2303
void DeoptimizedFrameInfo::Iterate(ObjectVisitor* v) {
2304
  v->VisitPointer(bit_cast<Object**>(&function_));
2305
  v->VisitPointer(&context_);
2306
  v->VisitPointers(parameters_, parameters_ + parameters_count_);
2307 2308 2309
  v->VisitPointers(expression_stack_, expression_stack_ + expression_count_);
}

2310 2311 2312 2313 2314 2315 2316 2317 2318

const char* Deoptimizer::GetDeoptReason(DeoptReason deopt_reason) {
  DCHECK(deopt_reason < kLastDeoptReason);
#define DEOPT_MESSAGES_TEXTS(C, T) T,
  static const char* deopt_messages_[] = {
      DEOPT_MESSAGES_LIST(DEOPT_MESSAGES_TEXTS)};
#undef DEOPT_MESSAGES_TEXTS
  return deopt_messages_[deopt_reason];
}
2319 2320


2321
Deoptimizer::DeoptInfo Deoptimizer::GetDeoptInfo(Code* code, Address pc) {
2322
  SourcePosition last_position = SourcePosition::Unknown();
2323 2324
  Deoptimizer::DeoptReason last_reason = Deoptimizer::kNoReason;
  int mask = RelocInfo::ModeMask(RelocInfo::DEOPT_REASON) |
2325
             RelocInfo::ModeMask(RelocInfo::POSITION);
2326 2327
  for (RelocIterator it(code, mask); !it.done(); it.next()) {
    RelocInfo* info = it.rinfo();
2328
    if (info->pc() >= pc) return DeoptInfo(last_position, NULL, last_reason);
2329
    if (info->rmode() == RelocInfo::POSITION) {
2330 2331 2332
      int raw_position = static_cast<int>(info->data());
      last_position = raw_position ? SourcePosition::FromRaw(raw_position)
                                   : SourcePosition::Unknown();
2333 2334 2335 2336
    } else if (info->rmode() == RelocInfo::DEOPT_REASON) {
      last_reason = static_cast<Deoptimizer::DeoptReason>(info->data());
    }
  }
2337
  return DeoptInfo(SourcePosition::Unknown(), NULL, Deoptimizer::kNoReason);
2338
}
2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415


// static
TranslatedValue TranslatedValue::NewArgumentsObject(TranslatedState* container,
                                                    int length,
                                                    int object_index) {
  TranslatedValue slot(container, kArgumentsObject);
  slot.materialization_info_ = {object_index, length};
  return slot;
}


// static
TranslatedValue TranslatedValue::NewDeferredObject(TranslatedState* container,
                                                   int length,
                                                   int object_index) {
  TranslatedValue slot(container, kCapturedObject);
  slot.materialization_info_ = {object_index, length};
  return slot;
}


// static
TranslatedValue TranslatedValue::NewDuplicateObject(TranslatedState* container,
                                                    int id) {
  TranslatedValue slot(container, kDuplicatedObject);
  slot.materialization_info_ = {id, -1};
  return slot;
}


// static
TranslatedValue TranslatedValue::NewDouble(TranslatedState* container,
                                           double value) {
  TranslatedValue slot(container, kDouble);
  slot.double_value_ = value;
  return slot;
}


// static
TranslatedValue TranslatedValue::NewInt32(TranslatedState* container,
                                          int32_t value) {
  TranslatedValue slot(container, kInt32);
  slot.int32_value_ = value;
  return slot;
}


// static
TranslatedValue TranslatedValue::NewUInt32(TranslatedState* container,
                                           uint32_t value) {
  TranslatedValue slot(container, kUInt32);
  slot.uint32_value_ = value;
  return slot;
}


// static
TranslatedValue TranslatedValue::NewBool(TranslatedState* container,
                                         uint32_t value) {
  TranslatedValue slot(container, kBoolBit);
  slot.uint32_value_ = value;
  return slot;
}


// static
TranslatedValue TranslatedValue::NewTagged(TranslatedState* container,
                                           Object* literal) {
  TranslatedValue slot(container, kTagged);
  slot.raw_literal_ = literal;
  return slot;
}


// static
2416 2417
TranslatedValue TranslatedValue::NewInvalid(TranslatedState* container) {
  return TranslatedValue(container, kInvalid);
2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492
}


Isolate* TranslatedValue::isolate() const { return container_->isolate(); }


Object* TranslatedValue::raw_literal() const {
  DCHECK_EQ(kTagged, kind());
  return raw_literal_;
}


int32_t TranslatedValue::int32_value() const {
  DCHECK_EQ(kInt32, kind());
  return int32_value_;
}


uint32_t TranslatedValue::uint32_value() const {
  DCHECK(kind() == kUInt32 || kind() == kBoolBit);
  return uint32_value_;
}


double TranslatedValue::double_value() const {
  DCHECK_EQ(kDouble, kind());
  return double_value_;
}


int TranslatedValue::object_length() const {
  DCHECK(kind() == kArgumentsObject || kind() == kCapturedObject);
  return materialization_info_.length_;
}


int TranslatedValue::object_index() const {
  DCHECK(kind() == kArgumentsObject || kind() == kCapturedObject ||
         kind() == kDuplicatedObject);
  return materialization_info_.id_;
}


Object* TranslatedValue::GetRawValue() const {
  // If we have a value, return it.
  Handle<Object> result_handle;
  if (value_.ToHandle(&result_handle)) {
    return *result_handle;
  }

  // Otherwise, do a best effort to get the value without allocation.
  switch (kind()) {
    case kTagged:
      return raw_literal();

    case kInt32: {
      bool is_smi = Smi::IsValid(int32_value());
      if (is_smi) {
        return Smi::FromInt(int32_value());
      }
      break;
    }

    case kUInt32: {
      bool is_smi = (uint32_value() <= static_cast<uintptr_t>(Smi::kMaxValue));
      if (is_smi) {
        return Smi::FromInt(static_cast<int32_t>(uint32_value()));
      }
      break;
    }

    case kBoolBit: {
      if (uint32_value() == 0) {
        return isolate()->heap()->false_value();
      } else {
2493
        CHECK_EQ(1U, uint32_value());
2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615
        return isolate()->heap()->true_value();
      }
    }

    default:
      break;
  }

  // If we could not get the value without allocation, return the arguments
  // marker.
  return isolate()->heap()->arguments_marker();
}


Handle<Object> TranslatedValue::GetValue() {
  Handle<Object> result;
  // If we already have a value, then get it.
  if (value_.ToHandle(&result)) return result;

  // Otherwise we have to materialize.
  switch (kind()) {
    case TranslatedValue::kTagged:
    case TranslatedValue::kInt32:
    case TranslatedValue::kUInt32:
    case TranslatedValue::kBoolBit:
    case TranslatedValue::kDouble: {
      MaterializeSimple();
      return value_.ToHandleChecked();
    }

    case TranslatedValue::kArgumentsObject:
    case TranslatedValue::kCapturedObject:
    case TranslatedValue::kDuplicatedObject:
      return container_->MaterializeObjectAt(object_index());

    case TranslatedValue::kInvalid:
      FATAL("unexpected case");
      return Handle<Object>::null();
  }

  FATAL("internal error: value missing");
  return Handle<Object>::null();
}


void TranslatedValue::MaterializeSimple() {
  // If we already have materialized, return.
  if (!value_.is_null()) return;

  Object* raw_value = GetRawValue();
  if (raw_value != isolate()->heap()->arguments_marker()) {
    // We can get the value without allocation, just return it here.
    value_ = Handle<Object>(raw_value, isolate());
    return;
  }

  switch (kind()) {
    case kInt32: {
      value_ = Handle<Object>(isolate()->factory()->NewNumber(int32_value()));
      return;
    }

    case kUInt32:
      value_ = Handle<Object>(isolate()->factory()->NewNumber(uint32_value()));
      return;

    case kDouble:
      value_ = Handle<Object>(isolate()->factory()->NewNumber(double_value()));
      return;

    case kCapturedObject:
    case kDuplicatedObject:
    case kArgumentsObject:
    case kInvalid:
    case kTagged:
    case kBoolBit:
      FATAL("internal error: unexpected materialization.");
      break;
  }
}


bool TranslatedValue::IsMaterializedObject() const {
  switch (kind()) {
    case kCapturedObject:
    case kDuplicatedObject:
    case kArgumentsObject:
      return true;
    default:
      return false;
  }
}


int TranslatedValue::GetChildrenCount() const {
  if (kind() == kCapturedObject || kind() == kArgumentsObject) {
    return object_length();
  } else {
    return 0;
  }
}


uint32_t TranslatedState::GetUInt32Slot(Address fp, int slot_offset) {
  Address address = fp + slot_offset;
#if V8_TARGET_BIG_ENDIAN && V8_HOST_ARCH_64_BIT
  return Memory::uint32_at(address + kIntSize);
#else
  return Memory::uint32_at(address);
#endif
}


void TranslatedValue::Handlify() {
  if (kind() == kTagged) {
    value_ = Handle<Object>(raw_literal(), isolate());
    raw_literal_ = nullptr;
  }
}


TranslatedFrame TranslatedFrame::JSFrame(BailoutId node_id,
2616 2617 2618 2619
                                         SharedFunctionInfo* shared_info,
                                         int height) {
  TranslatedFrame frame(kFunction, shared_info->GetIsolate(), shared_info,
                        height);
2620 2621 2622 2623 2624
  frame.node_id_ = node_id;
  return frame;
}


2625 2626
TranslatedFrame TranslatedFrame::AccessorFrame(
    Kind kind, SharedFunctionInfo* shared_info) {
2627
  DCHECK(kind == kSetter || kind == kGetter);
2628
  return TranslatedFrame(kind, shared_info->GetIsolate(), shared_info);
2629 2630 2631
}


2632 2633 2634 2635
TranslatedFrame TranslatedFrame::ArgumentsAdaptorFrame(
    SharedFunctionInfo* shared_info, int height) {
  return TranslatedFrame(kArgumentsAdaptor, shared_info->GetIsolate(),
                         shared_info, height);
2636 2637 2638
}


2639 2640 2641
TranslatedFrame TranslatedFrame::ConstructStubFrame(
    SharedFunctionInfo* shared_info, int height) {
  return TranslatedFrame(kConstructStub, shared_info->GetIsolate(), shared_info,
2642 2643 2644 2645 2646 2647 2648 2649
                         height);
}


int TranslatedFrame::GetValueCount() {
  switch (kind()) {
    case kFunction: {
      int parameter_count =
2650 2651
          raw_shared_info_->internal_formal_parameter_count() + 1;
      return height_ + parameter_count + 1;
2652 2653 2654
    }

    case kGetter:
2655
      return 2;  // Function and receiver.
2656 2657

    case kSetter:
2658
      return 3;  // Function, receiver and the value to set.
2659 2660 2661

    case kArgumentsAdaptor:
    case kConstructStub:
2662 2663
      return 1 + height_;

2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675
    case kCompiledStub:
      return height_;

    case kInvalid:
      UNREACHABLE();
      break;
  }
  UNREACHABLE();
  return -1;
}


2676 2677 2678 2679
void TranslatedFrame::Handlify() {
  if (raw_shared_info_ != nullptr) {
    shared_info_ = Handle<SharedFunctionInfo>(raw_shared_info_);
    raw_shared_info_ = nullptr;
2680 2681 2682 2683 2684 2685 2686 2687 2688
  }
  for (auto& value : values_) {
    value.Handlify();
  }
}


TranslatedFrame TranslatedState::CreateNextTranslatedFrame(
    TranslationIterator* iterator, FixedArray* literal_array, Address fp,
2689
    FILE* trace_file) {
2690 2691 2692 2693 2694
  Translation::Opcode opcode =
      static_cast<Translation::Opcode>(iterator->Next());
  switch (opcode) {
    case Translation::JS_FRAME: {
      BailoutId node_id = BailoutId(iterator->Next());
2695 2696
      SharedFunctionInfo* shared_info =
          SharedFunctionInfo::cast(literal_array->get(iterator->Next()));
2697 2698
      int height = iterator->Next();
      if (trace_file != nullptr) {
rmcilroy's avatar
rmcilroy committed
2699 2700
        base::SmartArrayPointer<char> name =
            shared_info->DebugName()->ToCString();
2701 2702
        PrintF(trace_file, "  reading input frame %s", name.get());
        int arg_count = shared_info->internal_formal_parameter_count() + 1;
2703
        PrintF(trace_file, " => node=%d, args=%d, height=%d; inputs:\n",
2704
               node_id.ToInt(), arg_count, height);
2705
      }
2706
      return TranslatedFrame::JSFrame(node_id, shared_info, height);
2707 2708 2709
    }

    case Translation::ARGUMENTS_ADAPTOR_FRAME: {
2710 2711
      SharedFunctionInfo* shared_info =
          SharedFunctionInfo::cast(literal_array->get(iterator->Next()));
2712 2713
      int height = iterator->Next();
      if (trace_file != nullptr) {
rmcilroy's avatar
rmcilroy committed
2714 2715
        base::SmartArrayPointer<char> name =
            shared_info->DebugName()->ToCString();
2716
        PrintF(trace_file, "  reading arguments adaptor frame %s", name.get());
2717 2718
        PrintF(trace_file, " => height=%d; inputs:\n", height);
      }
2719
      return TranslatedFrame::ArgumentsAdaptorFrame(shared_info, height);
2720 2721 2722
    }

    case Translation::CONSTRUCT_STUB_FRAME: {
2723 2724
      SharedFunctionInfo* shared_info =
          SharedFunctionInfo::cast(literal_array->get(iterator->Next()));
2725 2726
      int height = iterator->Next();
      if (trace_file != nullptr) {
rmcilroy's avatar
rmcilroy committed
2727 2728
        base::SmartArrayPointer<char> name =
            shared_info->DebugName()->ToCString();
2729
        PrintF(trace_file, "  reading construct stub frame %s", name.get());
2730 2731
        PrintF(trace_file, " => height=%d; inputs:\n", height);
      }
2732
      return TranslatedFrame::ConstructStubFrame(shared_info, height);
2733 2734 2735
    }

    case Translation::GETTER_STUB_FRAME: {
2736 2737
      SharedFunctionInfo* shared_info =
          SharedFunctionInfo::cast(literal_array->get(iterator->Next()));
2738
      if (trace_file != nullptr) {
rmcilroy's avatar
rmcilroy committed
2739 2740
        base::SmartArrayPointer<char> name =
            shared_info->DebugName()->ToCString();
2741
        PrintF(trace_file, "  reading getter frame %s; inputs:\n", name.get());
2742
      }
2743 2744
      return TranslatedFrame::AccessorFrame(TranslatedFrame::kGetter,
                                            shared_info);
2745 2746 2747
    }

    case Translation::SETTER_STUB_FRAME: {
2748 2749
      SharedFunctionInfo* shared_info =
          SharedFunctionInfo::cast(literal_array->get(iterator->Next()));
2750
      if (trace_file != nullptr) {
rmcilroy's avatar
rmcilroy committed
2751 2752
        base::SmartArrayPointer<char> name =
            shared_info->DebugName()->ToCString();
2753
        PrintF(trace_file, "  reading setter frame %s; inputs:\n", name.get());
2754
      }
2755 2756
      return TranslatedFrame::AccessorFrame(TranslatedFrame::kSetter,
                                            shared_info);
2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783
    }

    case Translation::COMPILED_STUB_FRAME: {
      int height = iterator->Next();
      if (trace_file != nullptr) {
        PrintF(trace_file,
               "  reading compiler stub frame => height=%d; inputs:\n", height);
      }
      return TranslatedFrame::CompiledStubFrame(height,
                                                literal_array->GetIsolate());
    }

    case Translation::BEGIN:
    case Translation::DUPLICATED_OBJECT:
    case Translation::ARGUMENTS_OBJECT:
    case Translation::CAPTURED_OBJECT:
    case Translation::REGISTER:
    case Translation::INT32_REGISTER:
    case Translation::UINT32_REGISTER:
    case Translation::BOOL_REGISTER:
    case Translation::DOUBLE_REGISTER:
    case Translation::STACK_SLOT:
    case Translation::INT32_STACK_SLOT:
    case Translation::UINT32_STACK_SLOT:
    case Translation::BOOL_STACK_SLOT:
    case Translation::DOUBLE_STACK_SLOT:
    case Translation::LITERAL:
2784
    case Translation::JS_FRAME_FUNCTION:
2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862
      break;
  }
  FATAL("We should never get here - unexpected deopt info.");
  return TranslatedFrame::InvalidFrame();
}


// static
void TranslatedFrame::AdvanceIterator(
    std::deque<TranslatedValue>::iterator* iter) {
  int values_to_skip = 1;
  while (values_to_skip > 0) {
    // Consume the current element.
    values_to_skip--;
    // Add all the children.
    values_to_skip += (*iter)->GetChildrenCount();

    (*iter)++;
  }
}


// We can't intermix stack decoding and allocations because
// deoptimization infrastracture is not GC safe.
// Thus we build a temporary structure in malloced space.
TranslatedValue TranslatedState::CreateNextTranslatedValue(
    int frame_index, int value_index, TranslationIterator* iterator,
    FixedArray* literal_array, Address fp, RegisterValues* registers,
    FILE* trace_file) {
  disasm::NameConverter converter;

  Translation::Opcode opcode =
      static_cast<Translation::Opcode>(iterator->Next());
  switch (opcode) {
    case Translation::BEGIN:
    case Translation::JS_FRAME:
    case Translation::ARGUMENTS_ADAPTOR_FRAME:
    case Translation::CONSTRUCT_STUB_FRAME:
    case Translation::GETTER_STUB_FRAME:
    case Translation::SETTER_STUB_FRAME:
    case Translation::COMPILED_STUB_FRAME:
      // Peeled off before getting here.
      break;

    case Translation::DUPLICATED_OBJECT: {
      int object_id = iterator->Next();
      if (trace_file != nullptr) {
        PrintF(trace_file, "duplicated object #%d", object_id);
      }
      object_positions_.push_back(object_positions_[object_id]);
      return TranslatedValue::NewDuplicateObject(this, object_id);
    }

    case Translation::ARGUMENTS_OBJECT: {
      int arg_count = iterator->Next();
      int object_index = static_cast<int>(object_positions_.size());
      if (trace_file != nullptr) {
        PrintF(trace_file, "argumets object #%d (length = %d)", object_index,
               arg_count);
      }
      object_positions_.push_back({frame_index, value_index});
      return TranslatedValue::NewArgumentsObject(this, arg_count, object_index);
    }

    case Translation::CAPTURED_OBJECT: {
      int field_count = iterator->Next();
      int object_index = static_cast<int>(object_positions_.size());
      if (trace_file != nullptr) {
        PrintF(trace_file, "captured object #%d (length = %d)", object_index,
               field_count);
      }
      object_positions_.push_back({frame_index, value_index});
      return TranslatedValue::NewDeferredObject(this, field_count,
                                                object_index);
    }

    case Translation::REGISTER: {
      int input_reg = iterator->Next();
2863
      if (registers == nullptr) return TranslatedValue::NewInvalid(this);
2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874
      intptr_t value = registers->GetRegister(input_reg);
      if (trace_file != nullptr) {
        PrintF(trace_file, "0x%08" V8PRIxPTR " ; %s ", value,
               converter.NameOfCPURegister(input_reg));
        reinterpret_cast<Object*>(value)->ShortPrint(trace_file);
      }
      return TranslatedValue::NewTagged(this, reinterpret_cast<Object*>(value));
    }

    case Translation::INT32_REGISTER: {
      int input_reg = iterator->Next();
2875
      if (registers == nullptr) return TranslatedValue::NewInvalid(this);
2876 2877 2878 2879 2880 2881 2882 2883 2884 2885
      intptr_t value = registers->GetRegister(input_reg);
      if (trace_file != nullptr) {
        PrintF(trace_file, "%" V8PRIdPTR " ; %s ", value,
               converter.NameOfCPURegister(input_reg));
      }
      return TranslatedValue::NewInt32(this, static_cast<int32_t>(value));
    }

    case Translation::UINT32_REGISTER: {
      int input_reg = iterator->Next();
2886
      if (registers == nullptr) return TranslatedValue::NewInvalid(this);
2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897
      intptr_t value = registers->GetRegister(input_reg);
      if (trace_file != nullptr) {
        PrintF(trace_file, "%" V8PRIuPTR " ; %s (uint)", value,
               converter.NameOfCPURegister(input_reg));
        reinterpret_cast<Object*>(value)->ShortPrint(trace_file);
      }
      return TranslatedValue::NewUInt32(this, static_cast<uint32_t>(value));
    }

    case Translation::BOOL_REGISTER: {
      int input_reg = iterator->Next();
2898
      if (registers == nullptr) return TranslatedValue::NewInvalid(this);
2899 2900 2901 2902 2903 2904 2905 2906 2907 2908
      intptr_t value = registers->GetRegister(input_reg);
      if (trace_file != nullptr) {
        PrintF(trace_file, "%" V8PRIdPTR " ; %s (bool)", value,
               converter.NameOfCPURegister(input_reg));
      }
      return TranslatedValue::NewBool(this, static_cast<uint32_t>(value));
    }

    case Translation::DOUBLE_REGISTER: {
      int input_reg = iterator->Next();
2909
      if (registers == nullptr) return TranslatedValue::NewInvalid(this);
2910 2911 2912
      double value = registers->GetDoubleRegister(input_reg);
      if (trace_file != nullptr) {
        PrintF(trace_file, "%e ; %s (bool)", value,
2913
               DoubleRegister::from_code(input_reg).ToString());
2914 2915 2916 2917 2918
      }
      return TranslatedValue::NewDouble(this, value);
    }

    case Translation::STACK_SLOT: {
2919 2920
      int slot_offset =
          OptimizedFrame::StackSlotOffsetRelativeToFp(iterator->Next());
2921 2922 2923 2924 2925 2926 2927 2928 2929 2930
      intptr_t value = *(reinterpret_cast<intptr_t*>(fp + slot_offset));
      if (trace_file != nullptr) {
        PrintF(trace_file, "0x%08" V8PRIxPTR " ; [fp %c %d] ", value,
               slot_offset < 0 ? '-' : '+', std::abs(slot_offset));
        reinterpret_cast<Object*>(value)->ShortPrint(trace_file);
      }
      return TranslatedValue::NewTagged(this, reinterpret_cast<Object*>(value));
    }

    case Translation::INT32_STACK_SLOT: {
2931 2932
      int slot_offset =
          OptimizedFrame::StackSlotOffsetRelativeToFp(iterator->Next());
2933 2934 2935 2936 2937 2938 2939 2940 2941 2942
      uint32_t value = GetUInt32Slot(fp, slot_offset);
      if (trace_file != nullptr) {
        PrintF(trace_file, "%d ; (int) [fp %c %d] ",
               static_cast<int32_t>(value), slot_offset < 0 ? '-' : '+',
               std::abs(slot_offset));
      }
      return TranslatedValue::NewInt32(this, value);
    }

    case Translation::UINT32_STACK_SLOT: {
2943 2944
      int slot_offset =
          OptimizedFrame::StackSlotOffsetRelativeToFp(iterator->Next());
2945 2946 2947 2948 2949 2950 2951 2952 2953
      uint32_t value = GetUInt32Slot(fp, slot_offset);
      if (trace_file != nullptr) {
        PrintF(trace_file, "%u ; (uint) [fp %c %d] ", value,
               slot_offset < 0 ? '-' : '+', std::abs(slot_offset));
      }
      return TranslatedValue::NewUInt32(this, value);
    }

    case Translation::BOOL_STACK_SLOT: {
2954 2955
      int slot_offset =
          OptimizedFrame::StackSlotOffsetRelativeToFp(iterator->Next());
2956 2957 2958 2959 2960 2961 2962 2963 2964
      uint32_t value = GetUInt32Slot(fp, slot_offset);
      if (trace_file != nullptr) {
        PrintF(trace_file, "%u ; (bool) [fp %c %d] ", value,
               slot_offset < 0 ? '-' : '+', std::abs(slot_offset));
      }
      return TranslatedValue::NewBool(this, value);
    }

    case Translation::DOUBLE_STACK_SLOT: {
2965 2966
      int slot_offset =
          OptimizedFrame::StackSlotOffsetRelativeToFp(iterator->Next());
2967
      double value = ReadDoubleValue(fp + slot_offset);
2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985
      if (trace_file != nullptr) {
        PrintF(trace_file, "%e ; (double) [fp %c %d] ", value,
               slot_offset < 0 ? '-' : '+', std::abs(slot_offset));
      }
      return TranslatedValue::NewDouble(this, value);
    }

    case Translation::LITERAL: {
      int literal_index = iterator->Next();
      Object* value = literal_array->get(literal_index);
      if (trace_file != nullptr) {
        PrintF(trace_file, "0x%08" V8PRIxPTR " ; (literal %d) ",
               reinterpret_cast<intptr_t>(value), literal_index);
        reinterpret_cast<Object*>(value)->ShortPrint(trace_file);
      }

      return TranslatedValue::NewTagged(this, value);
    }
2986 2987 2988 2989 2990 2991 2992 2993 2994 2995

    case Translation::JS_FRAME_FUNCTION: {
      int slot_offset = JavaScriptFrameConstants::kFunctionOffset;
      intptr_t value = *(reinterpret_cast<intptr_t*>(fp + slot_offset));
      if (trace_file != nullptr) {
        PrintF(trace_file, "0x%08" V8PRIxPTR " ; (frame function) ", value);
        reinterpret_cast<Object*>(value)->ShortPrint(trace_file);
      }
      return TranslatedValue::NewTagged(this, reinterpret_cast<Object*>(value));
    }
2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011
  }

  FATAL("We should never get here - unexpected deopt info.");
  return TranslatedValue(nullptr, TranslatedValue::kInvalid);
}


TranslatedState::TranslatedState(JavaScriptFrame* frame)
    : isolate_(nullptr),
      stack_frame_pointer_(nullptr),
      has_adapted_arguments_(false) {
  int deopt_index = Safepoint::kNoDeoptimizationIndex;
  DeoptimizationInputData* data =
      static_cast<OptimizedFrame*>(frame)->GetDeoptimizationData(&deopt_index);
  TranslationIterator it(data->TranslationByteArray(),
                         data->TranslationIndex(deopt_index)->value());
3012 3013
  Init(frame->fp(), &it, data->LiteralArray(), nullptr /* registers */,
       nullptr /* trace file */);
3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044
}


TranslatedState::TranslatedState()
    : isolate_(nullptr),
      stack_frame_pointer_(nullptr),
      has_adapted_arguments_(false) {}


void TranslatedState::Init(Address input_frame_pointer,
                           TranslationIterator* iterator,
                           FixedArray* literal_array, RegisterValues* registers,
                           FILE* trace_file) {
  DCHECK(frames_.empty());

  isolate_ = literal_array->GetIsolate();
  // Read out the 'header' translation.
  Translation::Opcode opcode =
      static_cast<Translation::Opcode>(iterator->Next());
  CHECK(opcode == Translation::BEGIN);

  int count = iterator->Next();
  iterator->Next();  // Drop JS frames count.

  frames_.reserve(count);

  std::stack<int> nested_counts;

  // Read the frames
  for (int i = 0; i < count; i++) {
    // Read the frame descriptor.
3045 3046
    frames_.push_back(CreateNextTranslatedFrame(
        iterator, literal_array, input_frame_pointer, trace_file));
3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097
    TranslatedFrame& frame = frames_.back();

    // Read the values.
    int values_to_process = frame.GetValueCount();
    while (values_to_process > 0 || !nested_counts.empty()) {
      if (trace_file != nullptr) {
        if (nested_counts.empty()) {
          // For top level values, print the value number.
          PrintF(trace_file, "    %3i: ",
                 frame.GetValueCount() - values_to_process);
        } else {
          // Take care of indenting for nested values.
          PrintF(trace_file, "         ");
          for (size_t j = 0; j < nested_counts.size(); j++) {
            PrintF(trace_file, "  ");
          }
        }
      }

      TranslatedValue value = CreateNextTranslatedValue(
          i, static_cast<int>(frame.values_.size()), iterator, literal_array,
          input_frame_pointer, registers, trace_file);
      frame.Add(value);

      if (trace_file != nullptr) {
        PrintF(trace_file, "\n");
      }

      // Update the value count and resolve the nesting.
      values_to_process--;
      int children_count = value.GetChildrenCount();
      if (children_count > 0) {
        nested_counts.push(values_to_process);
        values_to_process = children_count;
      } else {
        while (values_to_process == 0 && !nested_counts.empty()) {
          values_to_process = nested_counts.top();
          nested_counts.pop();
        }
      }
    }
  }

  CHECK(!iterator->HasNext() ||
        static_cast<Translation::Opcode>(iterator->Next()) ==
            Translation::BEGIN);
}


void TranslatedState::Prepare(bool has_adapted_arguments,
                              Address stack_frame_pointer) {
3098
  for (auto& frame : frames_) frame.Handlify();
3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137

  stack_frame_pointer_ = stack_frame_pointer;
  has_adapted_arguments_ = has_adapted_arguments;

  UpdateFromPreviouslyMaterializedObjects();
}


Handle<Object> TranslatedState::MaterializeAt(int frame_index,
                                              int* value_index) {
  TranslatedFrame* frame = &(frames_[frame_index]);
  DCHECK(static_cast<size_t>(*value_index) < frame->values_.size());

  TranslatedValue* slot = &(frame->values_[*value_index]);
  (*value_index)++;

  switch (slot->kind()) {
    case TranslatedValue::kTagged:
    case TranslatedValue::kInt32:
    case TranslatedValue::kUInt32:
    case TranslatedValue::kBoolBit:
    case TranslatedValue::kDouble: {
      slot->MaterializeSimple();
      Handle<Object> value = slot->GetValue();
      if (value->IsMutableHeapNumber()) {
        HeapNumber::cast(*value)->set_map(isolate()->heap()->heap_number_map());
      }
      return value;
    }

    case TranslatedValue::kArgumentsObject: {
      int length = slot->GetChildrenCount();
      Handle<JSObject> arguments;
      if (GetAdaptedArguments(&arguments, frame_index)) {
        // Store the materialized object and consume the nested values.
        for (int i = 0; i < length; ++i) {
          MaterializeAt(frame_index, value_index);
        }
      } else {
3138 3139
        Handle<JSFunction> function =
            Handle<JSFunction>::cast(frame->front().GetValue());
3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189
        arguments = isolate_->factory()->NewArgumentsObject(function, length);
        Handle<FixedArray> array = isolate_->factory()->NewFixedArray(length);
        DCHECK_EQ(array->length(), length);
        arguments->set_elements(*array);
        for (int i = 0; i < length; ++i) {
          Handle<Object> value = MaterializeAt(frame_index, value_index);
          array->set(i, *value);
        }
      }
      slot->value_ = arguments;
      return arguments;
    }
    case TranslatedValue::kCapturedObject: {
      int length = slot->GetChildrenCount();

      // The map must be a tagged object.
      CHECK(frame->values_[*value_index].kind() == TranslatedValue::kTagged);

      Handle<Object> result;
      if (slot->value_.ToHandle(&result)) {
        // This has been previously materialized, return the previous value.
        // We still need to skip all the nested objects.
        for (int i = 0; i < length; i++) {
          MaterializeAt(frame_index, value_index);
        }

        return result;
      }

      Handle<Object> map_object = MaterializeAt(frame_index, value_index);
      Handle<Map> map =
          Map::GeneralizeAllFieldRepresentations(Handle<Map>::cast(map_object));
      switch (map->instance_type()) {
        case MUTABLE_HEAP_NUMBER_TYPE:
        case HEAP_NUMBER_TYPE: {
          // Reuse the HeapNumber value directly as it is already properly
          // tagged and skip materializing the HeapNumber explicitly.
          Handle<Object> object = MaterializeAt(frame_index, value_index);
          slot->value_ = object;
          // On 32-bit architectures, there is an extra slot there because
          // the escape analysis calculates the number of slots as
          // object-size/pointer-size. To account for this, we read out
          // any extra slots.
          for (int i = 0; i < length - 2; i++) {
            MaterializeAt(frame_index, value_index);
          }
          return object;
        }
        case JS_OBJECT_TYPE: {
          Handle<JSObject> object =
3190
              isolate_->factory()->NewJSObjectFromMap(map, NOT_TENURED);
3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269
          slot->value_ = object;
          Handle<Object> properties = MaterializeAt(frame_index, value_index);
          Handle<Object> elements = MaterializeAt(frame_index, value_index);
          object->set_properties(FixedArray::cast(*properties));
          object->set_elements(FixedArrayBase::cast(*elements));
          for (int i = 0; i < length - 3; ++i) {
            Handle<Object> value = MaterializeAt(frame_index, value_index);
            FieldIndex index = FieldIndex::ForPropertyIndex(object->map(), i);
            object->FastPropertyAtPut(index, *value);
          }
          return object;
        }
        case JS_ARRAY_TYPE: {
          Handle<JSArray> object =
              isolate_->factory()->NewJSArray(0, map->elements_kind());
          slot->value_ = object;
          Handle<Object> properties = MaterializeAt(frame_index, value_index);
          Handle<Object> elements = MaterializeAt(frame_index, value_index);
          Handle<Object> length = MaterializeAt(frame_index, value_index);
          object->set_properties(FixedArray::cast(*properties));
          object->set_elements(FixedArrayBase::cast(*elements));
          object->set_length(*length);
          return object;
        }
        default:
          PrintF(stderr, "[couldn't handle instance type %d]\n",
                 map->instance_type());
          FATAL("unreachable");
          return Handle<Object>::null();
      }
      UNREACHABLE();
      break;
    }

    case TranslatedValue::kDuplicatedObject: {
      int object_index = slot->object_index();
      TranslatedState::ObjectPosition pos = object_positions_[object_index];

      // Make sure the duplicate is refering to a previous object.
      DCHECK(pos.frame_index_ < frame_index ||
             (pos.frame_index_ == frame_index &&
              pos.value_index_ < *value_index - 1));

      Handle<Object> object =
          frames_[pos.frame_index_].values_[pos.value_index_].GetValue();

      // The object should have a (non-sentinel) value.
      DCHECK(!object.is_null() &&
             !object.is_identical_to(isolate_->factory()->arguments_marker()));

      slot->value_ = object;
      return object;
    }

    case TranslatedValue::kInvalid:
      UNREACHABLE();
      break;
  }

  FATAL("We should never get here - unexpected deopt slot kind.");
  return Handle<Object>::null();
}


Handle<Object> TranslatedState::MaterializeObjectAt(int object_index) {
  TranslatedState::ObjectPosition pos = object_positions_[object_index];
  return MaterializeAt(pos.frame_index_, &(pos.value_index_));
}


bool TranslatedState::GetAdaptedArguments(Handle<JSObject>* result,
                                          int frame_index) {
  if (frame_index == 0) {
    // Top level frame -> we need to go to the parent frame on the stack.
    if (!has_adapted_arguments_) return false;

    // This is top level frame, so we need to go to the stack to get
    // this function's argument. (Note that this relies on not inlining
    // recursive functions!)
3270 3271
    Handle<JSFunction> function =
        Handle<JSFunction>::cast(frames_[frame_index].front().GetValue());
3272 3273 3274 3275 3276 3277 3278 3279
    *result = Handle<JSObject>::cast(Accessors::FunctionGetArguments(function));
    return true;
  } else {
    TranslatedFrame* previous_frame = &(frames_[frame_index]);
    if (previous_frame->kind() != TranslatedFrame::kArgumentsAdaptor) {
      return false;
    }
    // We get the adapted arguments from the parent translation.
3280 3281 3282
    int length = previous_frame->height();
    Handle<JSFunction> function =
        Handle<JSFunction>::cast(previous_frame->front().GetValue());
3283 3284 3285 3286 3287
    Handle<JSObject> arguments =
        isolate_->factory()->NewArgumentsObject(function, length);
    Handle<FixedArray> array = isolate_->factory()->NewFixedArray(length);
    arguments->set_elements(*array);
    TranslatedFrame::iterator arg_iterator = previous_frame->begin();
3288
    arg_iterator++;  // Skip function.
3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314
    for (int i = 0; i < length; ++i) {
      Handle<Object> value = arg_iterator->GetValue();
      array->set(i, *value);
      arg_iterator++;
    }
    CHECK(arg_iterator == previous_frame->end());
    *result = arguments;
    return true;
  }
}


TranslatedFrame* TranslatedState::GetArgumentsInfoFromJSFrameIndex(
    int jsframe_index, int* args_count) {
  for (size_t i = 0; i < frames_.size(); i++) {
    if (frames_[i].kind() == TranslatedFrame::kFunction) {
      if (jsframe_index > 0) {
        jsframe_index--;
      } else {
        // We have the JS function frame, now check if it has arguments adaptor.
        if (i > 0 &&
            frames_[i - 1].kind() == TranslatedFrame::kArgumentsAdaptor) {
          *args_count = frames_[i - 1].height();
          return &(frames_[i - 1]);
        }
        *args_count =
3315
            frames_[i].shared_info()->internal_formal_parameter_count() + 1;
3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366
        return &(frames_[i]);
      }
    }
  }
  return nullptr;
}


void TranslatedState::StoreMaterializedValuesAndDeopt() {
  MaterializedObjectStore* materialized_store =
      isolate_->materialized_object_store();
  Handle<FixedArray> previously_materialized_objects =
      materialized_store->Get(stack_frame_pointer_);

  Handle<Object> marker = isolate_->factory()->arguments_marker();

  int length = static_cast<int>(object_positions_.size());
  bool new_store = false;
  if (previously_materialized_objects.is_null()) {
    previously_materialized_objects =
        isolate_->factory()->NewFixedArray(length);
    for (int i = 0; i < length; i++) {
      previously_materialized_objects->set(i, *marker);
    }
    new_store = true;
  }

  DCHECK_EQ(length, previously_materialized_objects->length());

  bool value_changed = false;
  for (int i = 0; i < length; i++) {
    TranslatedState::ObjectPosition pos = object_positions_[i];
    TranslatedValue* value_info =
        &(frames_[pos.frame_index_].values_[pos.value_index_]);

    DCHECK(value_info->IsMaterializedObject());

    Handle<Object> value(value_info->GetRawValue(), isolate_);

    if (!value.is_identical_to(marker)) {
      if (previously_materialized_objects->get(i) == *marker) {
        previously_materialized_objects->set(i, *value);
        value_changed = true;
      } else {
        DCHECK(previously_materialized_objects->get(i) == *value);
      }
    }
  }
  if (new_store && value_changed) {
    materialized_store->Set(stack_frame_pointer_,
                            previously_materialized_objects);
3367 3368 3369
    DCHECK_EQ(TranslatedFrame::kFunction, frames_[0].kind());
    Object* const function = frames_[0].front().GetRawValue();
    Deoptimizer::DeoptimizeFunction(JSFunction::cast(function));
3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402
  }
}


void TranslatedState::UpdateFromPreviouslyMaterializedObjects() {
  MaterializedObjectStore* materialized_store =
      isolate_->materialized_object_store();
  Handle<FixedArray> previously_materialized_objects =
      materialized_store->Get(stack_frame_pointer_);

  // If we have no previously materialized objects, there is nothing to do.
  if (previously_materialized_objects.is_null()) return;

  Handle<Object> marker = isolate_->factory()->arguments_marker();

  int length = static_cast<int>(object_positions_.size());
  DCHECK_EQ(length, previously_materialized_objects->length());

  for (int i = 0; i < length; i++) {
    // For a previously materialized objects, inject their value into the
    // translated values.
    if (previously_materialized_objects->get(i) != *marker) {
      TranslatedState::ObjectPosition pos = object_positions_[i];
      TranslatedValue* value_info =
          &(frames_[pos.frame_index_].values_[pos.value_index_]);
      DCHECK(value_info->IsMaterializedObject());

      value_info->value_ =
          Handle<Object>(previously_materialized_objects->get(i), isolate_);
    }
  }
}

3403 3404
}  // namespace internal
}  // namespace v8