deoptimizer.cc 149 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 8
#include <memory>

9
#include "src/accessors.h"
10
#include "src/assembler-inl.h"
11
#include "src/ast/prettyprinter.h"
12
#include "src/callable.h"
13 14
#include "src/codegen.h"
#include "src/disasm.h"
15
#include "src/frames-inl.h"
16
#include "src/global-handles.h"
17
#include "src/interpreter/interpreter.h"
18
#include "src/macro-assembler.h"
19
#include "src/objects/debug-objects-inl.h"
20
#include "src/tracing/trace-event.h"
21
#include "src/v8.h"
22 23 24 25 26


namespace v8 {
namespace internal {

27 28
static MemoryChunk* AllocateCodeChunk(MemoryAllocator* allocator) {
  return allocator->AllocateChunk(Deoptimizer::GetMaxDeoptTableSize(),
29 30
                                  MemoryAllocator::GetCommitPageSize(),
                                  EXECUTABLE, NULL);
31 32 33 34 35
}


DeoptimizerData::DeoptimizerData(MemoryAllocator* allocator)
    : allocator_(allocator),
36
      current_(NULL) {
37
  for (int i = 0; i <= Deoptimizer::kLastBailoutType; ++i) {
38 39 40 41
    deopt_entry_code_entries_[i] = -1;
    deopt_entry_code_[i] = AllocateCodeChunk(allocator);
  }
}
42 43


44
DeoptimizerData::~DeoptimizerData() {
45
  for (int i = 0; i <= Deoptimizer::kLastBailoutType; ++i) {
46
    allocator_->Free<MemoryAllocator::kFull>(deopt_entry_code_[i]);
47 48
    deopt_entry_code_[i] = NULL;
  }
49 50
}

51

52 53 54
Code* Deoptimizer::FindDeoptimizingCode(Address addr) {
  if (function_->IsHeapObject()) {
    // Search all deoptimizing code in the native context of the function.
55
    Isolate* isolate = function_->GetIsolate();
56 57
    Context* native_context = function_->context()->native_context();
    Object* element = native_context->DeoptimizedCodeListHead();
58
    while (!element->IsUndefined(isolate)) {
59
      Code* code = Code::cast(element);
60
      CHECK(code->kind() == Code::OPTIMIZED_FUNCTION);
61 62
      if (code->contains(addr)) return code;
      element = code->next_code_link();
63 64
    }
  }
65
  return NULL;
66 67 68
}


69 70
// We rely on this function not causing a GC.  It is called from generated code
// without having a real stack frame in place.
71 72 73 74
Deoptimizer* Deoptimizer::New(JSFunction* function,
                              BailoutType type,
                              unsigned bailout_id,
                              Address from,
75 76
                              int fp_to_sp_delta,
                              Isolate* isolate) {
77 78
  Deoptimizer* deoptimizer = new Deoptimizer(isolate, function, type,
                                             bailout_id, from, fp_to_sp_delta);
79
  CHECK(isolate->deoptimizer_data()->current_ == NULL);
80
  isolate->deoptimizer_data()->current_ = deoptimizer;
81 82 83 84
  return deoptimizer;
}


85 86 87 88 89
// No larger than 2K on all platforms
static const int kDeoptTableMaxEpilogueCodeSize = 2 * KB;


size_t Deoptimizer::GetMaxDeoptTableSize() {
90
  int entries_size =
91
      Deoptimizer::kMaxNumberOfEntries * Deoptimizer::table_entry_size_;
92
  int commit_page_size = static_cast<int>(MemoryAllocator::GetCommitPageSize());
93
  int page_count = ((kDeoptTableMaxEpilogueCodeSize + entries_size - 1) /
94 95
                    commit_page_size) + 1;
  return static_cast<size_t>(commit_page_size * page_count);
96 97 98
}


99 100
Deoptimizer* Deoptimizer::Grab(Isolate* isolate) {
  Deoptimizer* result = isolate->deoptimizer_data()->current_;
101
  CHECK_NOT_NULL(result);
102
  result->DeleteFrameDescriptions();
103
  isolate->deoptimizer_data()->current_ = NULL;
104 105 106
  return result;
}

107 108
DeoptimizedFrameInfo* Deoptimizer::DebuggerInspectableFrame(
    JavaScriptFrame* frame,
109
    int jsframe_index,
110
    Isolate* isolate) {
111
  CHECK(frame->is_optimized());
112

113
  TranslatedState translated_values(frame);
114
  translated_values.Prepare(frame->fp());
115 116 117 118 119

  TranslatedState::iterator frame_it = translated_values.end();
  int counter = jsframe_index;
  for (auto it = translated_values.begin(); it != translated_values.end();
       it++) {
120 121
    if (it->kind() == TranslatedFrame::kInterpretedFunction ||
        it->kind() == TranslatedFrame::kJavaScriptBuiltinContinuation) {
122 123 124 125 126 127 128 129
      if (counter == 0) {
        frame_it = it;
        break;
      }
      counter--;
    }
  }
  CHECK(frame_it != translated_values.end());
130 131 132
  // We only include kJavaScriptBuiltinContinuation frames above to get the
  // counting right.
  CHECK_EQ(frame_it->kind(), TranslatedFrame::kInterpretedFunction);
133

134 135
  DeoptimizedFrameInfo* info =
      new DeoptimizedFrameInfo(&translated_values, frame_it, isolate);
136 137 138 139

  return info;
}

140 141 142 143 144 145 146 147 148
void Deoptimizer::GenerateDeoptimizationEntries(MacroAssembler* masm,
                                                int count,
                                                BailoutType type) {
  TableEntryGenerator generator(masm, type, count);
  generator.Generate();
}

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

151
  CHECK(context->IsNativeContext());
152

153 154 155
  // Visit the list of optimized functions, removing elements that
  // no longer refer to optimized code.
  JSFunction* prev = NULL;
156
  Object* element = context->OptimizedFunctionsListHead();
157 158
  Isolate* isolate = context->GetIsolate();
  while (!element->IsUndefined(isolate)) {
159 160 161 162 163 164 165 166 167
    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) {
168
        prev->set_next_function_link(next, UPDATE_WEAK_WRITE_BARRIER);
169 170 171 172
      } else {
        context->SetOptimizedFunctionsListHead(next);
      }
      // The visitor should not alter the link directly.
173
      CHECK_EQ(function->next_function_link(), next);
174 175
      // Set the next function link to undefined to indicate it is no longer
      // in the optimized functions list.
176 177
      function->set_next_function_link(context->GetHeap()->undefined_value(),
                                       SKIP_WRITE_BARRIER);
178 179
    } else {
      // The visitor should not alter the link directly.
180
      CHECK_EQ(function->next_function_link(), next);
181 182 183 184
      // preserve this element.
      prev = function;
    }
    element = next;
185
  }
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
}

void Deoptimizer::UnlinkOptimizedCode(Code* code, Context* native_context) {
  class CodeUnlinker : public OptimizedFunctionVisitor {
   public:
    explicit CodeUnlinker(Code* code) : code_(code) {}

    virtual void VisitFunction(JSFunction* function) {
      if (function->code() == code_) {
        if (FLAG_trace_deopt) {
          PrintF("[removing optimized code for: ");
          function->ShortPrint();
          PrintF("]\n");
        }
        function->set_code(function->shared()->code());
      }
    }
203

204 205 206 207 208
   private:
    Code* code_;
  };
  CodeUnlinker unlinker(code);
  VisitAllOptimizedFunctionsForContext(native_context, &unlinker);
209 210 211
}


212
void Deoptimizer::VisitAllOptimizedFunctions(
213
    Isolate* isolate,
214
    OptimizedFunctionVisitor* visitor) {
215
  DisallowHeapAllocation no_allocation;
216

217
  // Run through the list of all native contexts.
218
  Object* context = isolate->heap()->native_contexts_list();
219
  while (!context->IsUndefined(isolate)) {
220
    VisitAllOptimizedFunctionsForContext(Context::cast(context), visitor);
221
    context = Context::cast(context)->next_context_link();
222 223 224
  }
}

225 226
// Unlink functions referring to code marked for deoptimization, then move
// marked code from the optimized code list to the deoptimized code list,
227
// and replace pc on the stack for codes marked for deoptimization.
228
void Deoptimizer::DeoptimizeMarkedCodeForContext(Context* context) {
229
  DisallowHeapAllocation no_allocation;
230 231 232 233 234 235

  // 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 VisitFunction(JSFunction* function) {
236 237 238 239 240
      // The code in the function's optimized code feedback vector slot might
      // be different from the code on the function - evict it if necessary.
      function->feedback_vector()->EvictOptimizedCodeMarkedForDeoptimization(
          function->shared(), "unlinking code marked for deopt");

241
      Code* code = function->code();
242 243
      if (!code->marked_for_deoptimization()) return;

244
      // Unlink this function.
245
      if (!code->deopt_already_counted()) {
246
        function->feedback_vector()->increment_deopt_count();
247 248
        code->set_deopt_already_counted(true);
      }
Juliana Franco's avatar
Juliana Franco committed
249

250
      function->set_code(function->shared()->code());
251 252

      if (FLAG_trace_deopt) {
253 254 255 256 257
        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));
258 259
      }
    }
260
  };
261

262 263 264
  // Unlink all functions that refer to marked code.
  SelectedCodeUnlinker unlinker;
  VisitAllOptimizedFunctionsForContext(context, &unlinker);
265

266 267 268 269 270 271 272 273 274 275 276 277
  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();
278 279
      JSFunction* function =
          static_cast<OptimizedFrame*>(it.frame())->function();
280 281 282 283 284 285 286 287 288
      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();
Juliana Franco's avatar
Juliana Franco committed
289

290
      // Turbofan deopt is checked when we are patching addresses on stack.
291 292 293 294 295 296 297 298
      bool is_non_deoptimizing_asm_code =
          code->is_turbofanned() && !function->shared()->HasBytecodeArray();
      bool safe_if_deopt_triggered =
          deopt_index != Safepoint::kNoDeoptimizationIndex ||
          is_non_deoptimizing_asm_code;
      bool is_builtin_code = code->kind() == Code::BUILTIN;
      CHECK(topmost_optimized_code == NULL || safe_if_deopt_triggered ||
            is_non_deoptimizing_asm_code || is_builtin_code);
299 300
      if (topmost_optimized_code == NULL) {
        topmost_optimized_code = code;
301
        safe_to_deopt_topmost_optimized_code = safe_if_deopt_triggered;
302 303 304 305 306
      }
    }
  }
#endif

307
  // Move marked code from the optimized code list to the deoptimized
308
  // code list.
309 310 311
  // Walk over all optimized code objects in this native context.
  Code* prev = NULL;
  Object* element = context->OptimizedCodeListHead();
312
  while (!element->IsUndefined(isolate)) {
313
    Code* code = Code::cast(element);
314
    CHECK_EQ(code->kind(), Code::OPTIMIZED_FUNCTION);
315
    Object* next = code->next_code_link();
316

317
    if (code->marked_for_deoptimization()) {
318 319
      // Make sure that this object does not point to any garbage.
      code->InvalidateEmbeddedObjects();
320 321 322 323 324 325 326
      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);
      }
327

328 329 330 331 332 333 334 335
      // 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;
336 337
  }

338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
  // Finds the with activations of codes marked for deoptimization, search for
  // the trampoline to the deoptimizer call respective to each code, and use it
  // to replace the current pc on the stack.
  for (StackFrameIterator it(isolate, isolate->thread_local_top()); !it.done();
       it.Advance()) {
    if (it.frame()->type() == StackFrame::OPTIMIZED) {
      Code* code = it.frame()->LookupCode();
      if (code->kind() == Code::OPTIMIZED_FUNCTION &&
          code->marked_for_deoptimization()) {
        // Obtain the trampoline to the deoptimizer call.
        SafepointEntry safepoint = code->GetSafepointEntry(it.frame()->pc());
        int trampoline_pc = safepoint.trampoline_pc();
        DCHECK_IMPLIES(code == topmost_optimized_code,
                       safe_to_deopt_topmost_optimized_code);
        // Replace the current pc on the stack with the trampoline.
        it.frame()->set_pc(code->instruction_start() + trampoline_pc);
      }
355
    }
356 357 358
  }
}

359

360
void Deoptimizer::DeoptimizeAll(Isolate* isolate) {
361 362
  RuntimeCallTimerScope runtimeTimer(isolate,
                                     &RuntimeCallStats::DeoptimizeCode);
363
  TimerEventScope<TimerEventDeoptimizeCode> timer(isolate);
364
  TRACE_EVENT0("v8", "V8.DeoptimizeCode");
365
  if (FLAG_trace_deopt) {
366 367
    CodeTracer::Scope scope(isolate->GetCodeTracer());
    PrintF(scope.file(), "[deoptimize all code in all contexts]\n");
368
  }
369
  DisallowHeapAllocation no_allocation;
370 371
  // For all contexts, mark all code, then deoptimize.
  Object* context = isolate->heap()->native_contexts_list();
372
  while (!context->IsUndefined(isolate)) {
373 374 375
    Context* native_context = Context::cast(context);
    MarkAllCodeForContext(native_context);
    DeoptimizeMarkedCodeForContext(native_context);
376
    context = native_context->next_context_link();
377 378 379 380
  }
}


381
void Deoptimizer::DeoptimizeMarkedCode(Isolate* isolate) {
382 383
  RuntimeCallTimerScope runtimeTimer(isolate,
                                     &RuntimeCallStats::DeoptimizeCode);
384
  TimerEventScope<TimerEventDeoptimizeCode> timer(isolate);
385
  TRACE_EVENT0("v8", "V8.DeoptimizeCode");
386
  if (FLAG_trace_deopt) {
387 388
    CodeTracer::Scope scope(isolate->GetCodeTracer());
    PrintF(scope.file(), "[deoptimize marked code in all contexts]\n");
389
  }
390
  DisallowHeapAllocation no_allocation;
391
  // For all contexts, deoptimize code already marked.
392
  Object* context = isolate->heap()->native_contexts_list();
393
  while (!context->IsUndefined(isolate)) {
394 395
    Context* native_context = Context::cast(context);
    DeoptimizeMarkedCodeForContext(native_context);
396
    context = native_context->next_context_link();
397 398 399 400
  }
}


401 402
void Deoptimizer::MarkAllCodeForContext(Context* context) {
  Object* element = context->OptimizedCodeListHead();
403 404
  Isolate* isolate = context->GetIsolate();
  while (!element->IsUndefined(isolate)) {
405
    Code* code = Code::cast(element);
406
    CHECK_EQ(code->kind(), Code::OPTIMIZED_FUNCTION);
407 408 409
    code->set_marked_for_deoptimization(true);
    element = code->next_code_link();
  }
410 411
}

412
void Deoptimizer::DeoptimizeFunction(JSFunction* function, Code* code) {
413 414 415 416
  Isolate* isolate = function->GetIsolate();
  RuntimeCallTimerScope runtimeTimer(isolate,
                                     &RuntimeCallStats::DeoptimizeCode);
  TimerEventScope<TimerEventDeoptimizeCode> timer(isolate);
417
  TRACE_EVENT0("v8", "V8.DeoptimizeCode");
418
  if (code == nullptr) code = function->code();
419

420 421 422 423 424 425
  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());
426 427 428 429
  }
}


430
void Deoptimizer::ComputeOutputFrames(Deoptimizer* deoptimizer) {
431 432 433
  deoptimizer->DoComputeOutputFrames();
}

434 435 436

const char* Deoptimizer::MessageFor(BailoutType type) {
  switch (type) {
437 438 439
    case EAGER: return "eager";
    case SOFT: return "soft";
    case LAZY: return "lazy";
440
  }
441
  FATAL("Unsupported deopt type");
442
  return NULL;
443 444
}

445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461
namespace {

CodeEventListener::DeoptKind DeoptKindOfBailoutType(
    Deoptimizer::BailoutType bailout_type) {
  switch (bailout_type) {
    case Deoptimizer::EAGER:
      return CodeEventListener::kEager;
    case Deoptimizer::SOFT:
      return CodeEventListener::kSoft;
    case Deoptimizer::LAZY:
      return CodeEventListener::kLazy;
  }
  UNREACHABLE();
}

}  // namespace

462 463
Deoptimizer::Deoptimizer(Isolate* isolate, JSFunction* function,
                         BailoutType type, unsigned bailout_id, Address from,
464
                         int fp_to_sp_delta)
465 466
    : isolate_(isolate),
      function_(function),
467 468 469 470
      bailout_id_(bailout_id),
      bailout_type_(type),
      from_(from),
      fp_to_sp_delta_(fp_to_sp_delta),
471 472 473
      deoptimizing_throw_(false),
      catch_handler_data_(-1),
      catch_handler_pc_offset_(-1),
474
      input_(nullptr),
475
      output_count_(0),
476
      jsframe_count_(0),
477
      output_(nullptr),
478 479 480 481 482 483
      caller_frame_top_(0),
      caller_fp_(0),
      caller_pc_(0),
      caller_constant_pool_(0),
      input_frame_context_(0),
      stack_fp_(0),
484
      trace_scope_(nullptr) {
485 486 487 488 489
  if (isolate->deoptimizer_lazy_throw()) {
    isolate->set_deoptimizer_lazy_throw(false);
    deoptimizing_throw_ = true;
  }

490
  DCHECK(from != nullptr);
491
  compiled_code_ = FindOptimizedCode();
492
#if DEBUG
493
  DCHECK(compiled_code_ != NULL);
494
  if (type == EAGER || type == SOFT || type == LAZY) {
495
    DCHECK(compiled_code_->kind() != Code::FUNCTION);
496 497 498
  }
#endif

499 500 501
  DCHECK(function->IsJSFunction());
  trace_scope_ =
      FLAG_trace_deopt ? new CodeTracer::Scope(isolate->GetCodeTracer()) : NULL;
502 503 504 505
#ifdef DEBUG
  CHECK(AllowHeapAllocation::IsAllowed());
  disallow_heap_allocation_ = new DisallowHeapAllocation();
#endif  // DEBUG
506 507
  if (compiled_code_->kind() != Code::OPTIMIZED_FUNCTION ||
      !compiled_code_->deopt_already_counted()) {
508 509 510 511 512 513 514 515
    // If the function is optimized, and we haven't counted that deopt yet, then
    // increment the function's deopt count so that we can avoid optimising
    // functions that deopt too often.

    if (bailout_type_ == Deoptimizer::SOFT) {
      // Soft deopts shouldn't count against the overall deoptimization count
      // that can eventually lead to disabling optimization for a function.
      isolate->counters()->soft_deopts_executed()->Increment();
516
    } else if (function != nullptr) {
517
      function->feedback_vector()->increment_deopt_count();
518 519
    }
  }
520
  if (compiled_code_->kind() == Code::OPTIMIZED_FUNCTION) {
521
    compiled_code_->set_deopt_already_counted(true);
522 523 524
    PROFILE(isolate_,
            CodeDeoptEvent(compiled_code_, DeoptKindOfBailoutType(type), from_,
                           fp_to_sp_delta_));
525
  }
526
  unsigned size = ComputeInputFrameSize();
527
  int parameter_count =
528
      function->shared()->internal_formal_parameter_count() + 1;
529
  input_ = new (size) FrameDescription(size, parameter_count);
530 531
}

532
Code* Deoptimizer::FindOptimizedCode() {
533 534 535 536
  Code* compiled_code = FindDeoptimizingCode(from_);
  return (compiled_code == NULL)
             ? static_cast<Code*>(isolate_->FindCodeObject(from_))
             : compiled_code;
537 538 539 540
}


void Deoptimizer::PrintFunctionName() {
541
  if (function_->IsHeapObject() && function_->IsJSFunction()) {
542
    function_->ShortPrint(trace_scope_->file());
543
  } else {
544 545
    PrintF(trace_scope_->file(),
           "%s", Code::Kind2String(compiled_code_->kind()));
546 547 548 549
  }
}


550
Deoptimizer::~Deoptimizer() {
551 552
  DCHECK(input_ == NULL && output_ == NULL);
  DCHECK(disallow_heap_allocation_ == NULL);
553
  delete trace_scope_;
554 555 556 557 558 559 560 561 562 563 564
}


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;
565 566 567 568 569 570
#ifdef DEBUG
  CHECK(!AllowHeapAllocation::IsAllowed());
  CHECK(disallow_heap_allocation_ != NULL);
  delete disallow_heap_allocation_;
  disallow_heap_allocation_ = NULL;
#endif  // DEBUG
571 572 573
}


574 575
Address Deoptimizer::GetDeoptimizationEntry(Isolate* isolate,
                                            int id,
576 577
                                            BailoutType type,
                                            GetEntryMode mode) {
578
  CHECK_GE(id, 0);
579 580
  if (id >= kMaxNumberOfEntries) return NULL;
  if (mode == ENSURE_ENTRY_CODE) {
581
    EnsureCodeForDeoptimizationEntry(isolate, type, id);
582
  } else {
583
    CHECK_EQ(mode, CALCULATE_ENTRY_ADDRESS);
584
  }
585
  DeoptimizerData* data = isolate->deoptimizer_data();
586
  CHECK_LE(type, kLastBailoutType);
587
  MemoryChunk* base = data->deopt_entry_code_[type];
588
  return base->area_start() + (id * table_entry_size_);
589 590 591
}


592 593 594 595
int Deoptimizer::GetDeoptimizationId(Isolate* isolate,
                                     Address addr,
                                     BailoutType type) {
  DeoptimizerData* data = isolate->deoptimizer_data();
596
  MemoryChunk* base = data->deopt_entry_code_[type];
597
  Address start = base->area_start();
598
  if (addr < start ||
599
      addr >= start + (kMaxNumberOfEntries * table_entry_size_)) {
600 601
    return kNotDeoptimizationEntry;
  }
602
  DCHECK_EQ(0,
603 604
            static_cast<int>(addr - start) % table_entry_size_);
  return static_cast<int>(addr - start) / table_entry_size_;
605 606 607
}


608
int Deoptimizer::GetDeoptimizedCodeCount(Isolate* isolate) {
609
  int length = 0;
610 611
  // Count all entries in the deoptimizing code list of every context.
  Object* context = isolate->heap()->native_contexts_list();
612
  while (!context->IsUndefined(isolate)) {
613 614
    Context* native_context = Context::cast(context);
    Object* element = native_context->DeoptimizedCodeListHead();
615
    while (!element->IsUndefined(isolate)) {
616
      Code* code = Code::cast(element);
617
      DCHECK(code->kind() == Code::OPTIMIZED_FUNCTION);
618 619 620
      length++;
      element = code->next_code_link();
    }
621
    context = Context::cast(context)->next_context_link();
622 623 624 625
  }
  return length;
}

626 627 628 629 630 631 632 633 634
namespace {

int LookupCatchHandler(TranslatedFrame* translated_frame, int* data_out) {
  switch (translated_frame->kind()) {
    case TranslatedFrame::kInterpretedFunction: {
      int bytecode_offset = translated_frame->node_id().ToInt();
      JSFunction* function =
          JSFunction::cast(translated_frame->begin()->GetRawValue());
      BytecodeArray* bytecode = function->shared()->bytecode_array();
635 636
      HandlerTable* table = HandlerTable::cast(bytecode->handler_table());
      return table->LookupRange(bytecode_offset, data_out, nullptr);
637 638 639 640 641 642 643 644
    }
    default:
      break;
  }
  return -1;
}

}  // namespace
645

646 647
// We rely on this function not causing a GC.  It is called from generated code
// without having a real stack frame in place.
648
void Deoptimizer::DoComputeOutputFrames() {
649
  base::ElapsedTimer timer;
650 651 652 653 654 655

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

656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677
  {
    // Read caller's PC, caller's FP and caller's constant pool values
    // from input frame. Compute caller's frame top address.

    Register fp_reg = JavaScriptFrame::fp_register();
    stack_fp_ = input_->GetRegister(fp_reg.code());

    caller_frame_top_ = stack_fp_ + ComputeInputFrameAboveFpFixedSize();

    Address fp_address = input_->GetFramePointerAddress();
    caller_fp_ = Memory::intptr_at(fp_address);
    caller_pc_ =
        Memory::intptr_at(fp_address + CommonFrameConstants::kCallerPCOffset);
    input_frame_context_ = Memory::intptr_at(
        fp_address + CommonFrameConstants::kContextOrFrameTypeOffset);

    if (FLAG_enable_embedded_constant_pool) {
      caller_constant_pool_ = Memory::intptr_at(
          fp_address + CommonFrameConstants::kConstantPoolOffset);
    }
  }

678
  if (trace_scope_ != NULL) {
679
    timer.Start();
680 681
    PrintF(trace_scope_->file(), "[deoptimizing (DEOPT %s): begin ",
           MessageFor(bailout_type_));
682
    PrintFunctionName();
683
    PrintF(trace_scope_->file(),
684 685 686 687
           " (opt #%d) @%d, FP to SP delta: %d, caller sp: 0x%08" V8PRIxPTR
           "]\n",
           input_data->OptimizationId()->value(), bailout_id_, fp_to_sp_delta_,
           caller_frame_top_);
688
    if (bailout_type_ == EAGER || bailout_type_ == SOFT) {
689
      compiled_code_->PrintDeoptLocation(trace_scope_->file(), from_);
690
    }
691 692
  }

693
  BailoutId node_id = input_data->BytecodeOffset(bailout_id_);
694 695 696 697
  ByteArray* translations = input_data->TranslationByteArray();
  unsigned translation_index =
      input_data->TranslationIndex(bailout_id_)->value();

698 699
  TranslationIterator state_iterator(translations, translation_index);
  translated_state_.Init(
700
      input_->GetFramePointerAddress(), &state_iterator,
701
      input_data->LiteralArray(), input_->GetRegisterValues(),
702 703 704 705
      trace_scope_ == nullptr ? nullptr : trace_scope_->file(),
      function_->IsHeapObject()
          ? function_->shared()->internal_formal_parameter_count()
          : 0);
706

707
  // Do the input frame to output frame(s) translation.
708
  size_t count = translated_state_.frames().size();
709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724
  // If we are supposed to go to the catch handler, find the catching frame
  // for the catch and make sure we only deoptimize upto that frame.
  if (deoptimizing_throw_) {
    size_t catch_handler_frame_index = count;
    for (size_t i = count; i-- > 0;) {
      catch_handler_pc_offset_ = LookupCatchHandler(
          &(translated_state_.frames()[i]), &catch_handler_data_);
      if (catch_handler_pc_offset_ >= 0) {
        catch_handler_frame_index = i;
        break;
      }
    }
    CHECK_LT(catch_handler_frame_index, count);
    count = catch_handler_frame_index + 1;
  }

725
  DCHECK(output_ == NULL);
726
  output_ = new FrameDescription*[count];
727
  for (size_t i = 0; i < count; ++i) {
728 729
    output_[i] = NULL;
  }
730
  output_count_ = static_cast<int>(count);
731 732

  // Translate each output frame.
733 734
  int frame_index = 0;  // output_frame_index
  for (size_t i = 0; i < count; ++i, ++frame_index) {
735
    // Read the ast node id, function, and frame height for this output frame.
736 737
    TranslatedFrame* translated_frame = &(translated_state_.frames()[i]);
    switch (translated_frame->kind()) {
738
      case TranslatedFrame::kInterpretedFunction:
739
        DoComputeInterpretedFrame(translated_frame, frame_index,
740
                                  deoptimizing_throw_ && i == count - 1);
741 742
        jsframe_count_++;
        break;
743
      case TranslatedFrame::kArgumentsAdaptor:
744 745
        DoComputeArgumentsAdaptorFrame(translated_frame, frame_index);
        break;
746
      case TranslatedFrame::kConstructStub:
747
        DoComputeConstructStubFrame(translated_frame, frame_index);
748
        break;
749
      case TranslatedFrame::kGetter:
750
        DoComputeAccessorStubFrame(translated_frame, frame_index, false);
751
        break;
752
      case TranslatedFrame::kSetter:
753
        DoComputeAccessorStubFrame(translated_frame, frame_index, true);
754
        break;
755 756 757 758 759 760
      case TranslatedFrame::kBuiltinContinuation:
        DoComputeBuiltinContinuation(translated_frame, frame_index, false);
        break;
      case TranslatedFrame::kJavaScriptBuiltinContinuation:
        DoComputeBuiltinContinuation(translated_frame, frame_index, true);
        break;
761 762
      case TranslatedFrame::kInvalid:
        FATAL("invalid frame");
763 764
        break;
    }
765 766 767
  }

  // Print some helpful diagnostic information.
768
  if (trace_scope_ != NULL) {
769
    double ms = timer.Elapsed().InMillisecondsF();
770
    int index = output_count_ - 1;  // Index of the topmost frame.
771 772
    PrintF(trace_scope_->file(), "[deoptimizing (%s): end ",
           MessageFor(bailout_type_));
773
    PrintFunctionName();
774 775 776 777
    PrintF(trace_scope_->file(),
           " @%d => node=%d, pc=0x%08" V8PRIxPTR ", caller sp=0x%08" V8PRIxPTR
           ", state=%s, took %0.3f ms]\n",
           bailout_id_, node_id.ToInt(), output_[index]->GetPc(),
778 779
           caller_frame_top_, BailoutStateToString(static_cast<BailoutState>(
                                  output_[index]->GetState()->value())),
780
           ms);
781 782 783
  }
}

784 785
void Deoptimizer::DoComputeInterpretedFrame(TranslatedFrame* translated_frame,
                                            int frame_index,
786
                                            bool goto_catch_handler) {
787 788
  SharedFunctionInfo* shared = translated_frame->raw_shared_info();

789
  TranslatedFrame::iterator value_iterator = translated_frame->begin();
790 791
  bool is_bottommost = (0 == frame_index);
  bool is_topmost = (output_count_ - 1 == frame_index);
792 793
  int input_index = 0;

794
  int bytecode_offset = translated_frame->node_id().ToInt();
795 796
  unsigned height = translated_frame->height();
  unsigned height_in_bytes = height * kPointerSize;
797 798 799 800 801 802 803

  // All tranlations for interpreted frames contain the accumulator and hence
  // are assumed to be in bailout state {BailoutState::TOS_REGISTER}. However
  // such a state is only supported for the topmost frame. We need to skip
  // pushing the accumulator for any non-topmost frame.
  if (!is_topmost) height_in_bytes -= kPointerSize;

804 805 806 807 808
  JSFunction* function = JSFunction::cast(value_iterator->GetRawValue());
  value_iterator++;
  input_index++;
  if (trace_scope_ != NULL) {
    PrintF(trace_scope_->file(), "  translating interpreted frame ");
809
    std::unique_ptr<char[]> name = shared->DebugName()->ToCString();
810
    PrintF(trace_scope_->file(), "%s", name.get());
811 812 813 814 815 816
    PrintF(trace_scope_->file(), " => bytecode_offset=%d, height=%d%s\n",
           bytecode_offset, height_in_bytes,
           goto_catch_handler ? " (throw)" : "");
  }
  if (goto_catch_handler) {
    bytecode_offset = catch_handler_pc_offset_;
817 818 819 820
  }

  // The 'fixed' part of the frame consists of the incoming parameters and
  // the part described by InterpreterFrameConstants.
821
  unsigned fixed_frame_size = ComputeInterpretedFixedSize(shared);
822 823 824
  unsigned output_frame_size = height_in_bytes + fixed_frame_size;

  // Allocate and store the output frame description.
825 826 827
  int parameter_count = shared->internal_formal_parameter_count() + 1;
  FrameDescription* output_frame = new (output_frame_size)
      FrameDescription(output_frame_size, parameter_count);
828 829 830 831 832

  CHECK(frame_index >= 0 && frame_index < output_count_);
  CHECK_NULL(output_[frame_index]);
  output_[frame_index] = output_frame;

833 834
  // The top address of the frame is computed from the previous frame's top and
  // this frame's size.
835 836
  intptr_t top_address;
  if (is_bottommost) {
837
    top_address = caller_frame_top_ - output_frame_size;
838 839 840 841 842 843 844 845 846 847 848 849 850
  } else {
    top_address = output_[frame_index - 1]->GetTop() - output_frame_size;
  }
  output_frame->SetTop(top_address);

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

851 852 853 854
  if (trace_scope_ != nullptr) {
    PrintF(trace_scope_->file(), "    -------------------------\n");
  }

855
  // There are no translation commands for the caller's pc and fp, the
856
  // context, the function and the bytecode offset.  Synthesize
857 858 859 860 861 862 863 864 865 866
  // 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.
  output_offset -= kPCOnStackSize;
  intptr_t value;
  if (is_bottommost) {
867
    value = caller_pc_;
868 869 870 871 872 873 874 875 876 877 878 879
  } else {
    value = output_[frame_index - 1]->GetPc();
  }
  output_frame->SetCallerPc(output_offset, value);
  DebugPrintOutputSlot(value, frame_index, output_offset, "caller's pc\n");

  // 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.
  output_offset -= kFPOnStackSize;
  if (is_bottommost) {
880
    value = caller_fp_;
881 882 883 884 885 886
  } else {
    value = output_[frame_index - 1]->GetFp();
  }
  output_frame->SetCallerFp(output_offset, value);
  intptr_t fp_value = top_address + output_offset;
  output_frame->SetFp(fp_value);
887 888 889 890
  if (is_topmost) {
    Register fp_reg = InterpretedFrame::fp_register();
    output_frame->SetRegister(fp_reg.code(), fp_value);
  }
891 892 893 894 895 896 897 898
  DebugPrintOutputSlot(value, frame_index, output_offset, "caller's fp\n");

  if (FLAG_enable_embedded_constant_pool) {
    // For the bottommost output frame the constant pool pointer can be gotten
    // from the input frame. For subsequent output frames, it can be read from
    // the previous frame.
    output_offset -= kPointerSize;
    if (is_bottommost) {
899
      value = caller_constant_pool_;
900 901 902 903 904 905 906 907 908 909 910 911
    } else {
      value = output_[frame_index - 1]->GetConstantPool();
    }
    output_frame->SetCallerConstantPool(output_offset, value);
    DebugPrintOutputSlot(value, frame_index, output_offset,
                         "caller's constant_pool\n");
  }

  // 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.
  output_offset -= kPointerSize;
912 913 914 915 916 917 918 919 920 921 922 923 924

  // When deoptimizing into a catch block, we need to take the context
  // from a register that was specified in the handler table.
  TranslatedFrame::iterator context_pos = value_iterator;
  int context_input_index = input_index;
  if (goto_catch_handler) {
    // Skip to the translated value of the register specified
    // in the handler table.
    for (int i = 0; i < catch_handler_data_ + 1; ++i) {
      context_pos++;
      context_input_index++;
    }
  }
925
  // Read the context from the translations.
926
  Object* context = context_pos->GetRawValue();
927 928
  value = reinterpret_cast<intptr_t>(context);
  output_frame->SetContext(value);
929
  WriteValueToOutput(context, context_input_index, frame_index, output_offset,
930
                     "context    ");
931 932 933 934 935 936
  if (context == isolate_->heap()->arguments_marker()) {
    Address output_address =
        reinterpret_cast<Address>(output_[frame_index]->GetTop()) +
        output_offset;
    values_to_materialize_.push_back({output_address, context_pos});
  }
937 938 939 940 941 942 943 944
  value_iterator++;
  input_index++;

  // The function was mentioned explicitly in the BEGIN_FRAME.
  output_offset -= kPointerSize;
  value = reinterpret_cast<intptr_t>(function);
  WriteValueToOutput(function, 0, frame_index, output_offset, "function    ");

945
  // Set the bytecode array pointer.
946
  output_offset -= kPointerSize;
947
  Object* bytecode_array = shared->HasBreakInfo()
948 949
                               ? shared->GetDebugInfo()->DebugBytecodeArray()
                               : shared->bytecode_array();
950 951
  WriteValueToOutput(bytecode_array, 0, frame_index, output_offset,
                     "bytecode array ");
952

953 954
  // The bytecode offset was mentioned explicitly in the BEGIN_FRAME.
  output_offset -= kPointerSize;
Juliana Franco's avatar
Juliana Franco committed
955

956
  int raw_bytecode_offset =
957
      BytecodeArray::kHeaderSize - kHeapObjectTag + bytecode_offset;
958
  Smi* smi_bytecode_offset = Smi::FromInt(raw_bytecode_offset);
Juliana Franco's avatar
Juliana Franco committed
959 960
  output_[frame_index]->SetFrameSlot(
      output_offset, reinterpret_cast<intptr_t>(smi_bytecode_offset));
961

962
  if (trace_scope_ != nullptr) {
Juliana Franco's avatar
Juliana Franco committed
963 964 965
    DebugPrintOutputSlot(reinterpret_cast<intptr_t>(smi_bytecode_offset),
                         frame_index, output_offset, "bytecode offset @ ");
    PrintF(trace_scope_->file(), "%d\n", bytecode_offset);
966
    PrintF(trace_scope_->file(), "  (input #0)\n");
967 968 969
    PrintF(trace_scope_->file(), "    -------------------------\n");
  }

970
  // Translate the rest of the interpreter registers in the frame.
971
  for (unsigned i = 0; i < height - 1; ++i) {
972 973 974 975 976
    output_offset -= kPointerSize;
    WriteTranslatedValueToOutput(&value_iterator, &input_index, frame_index,
                                 output_offset);
  }

977 978
  // Translate the accumulator register (depending on frame position).
  if (is_topmost) {
979
    // For topmost frame, put the accumulator on the stack. The bailout state
980 981 982 983 984 985 986 987
    // for interpreted frames is always set to {BailoutState::TOS_REGISTER} and
    // the {NotifyDeoptimized} builtin pops it off the topmost frame (possibly
    // after materialization).
    output_offset -= kPointerSize;
    if (goto_catch_handler) {
      // If we are lazy deopting to a catch handler, we set the accumulator to
      // the exception (which lives in the result register).
      intptr_t accumulator_value =
988
          input_->GetRegister(kInterpreterAccumulatorRegister.code());
989 990 991 992 993 994 995
      WriteValueToOutput(reinterpret_cast<Object*>(accumulator_value), 0,
                         frame_index, output_offset, "accumulator ");
      value_iterator++;
    } else {
      WriteTranslatedValueToOutput(&value_iterator, &input_index, frame_index,
                                   output_offset, "accumulator ");
    }
996
  } else {
997 998 999 1000
    // For non-topmost frames, skip the accumulator translation. For those
    // frames, the return value from the callee will become the accumulator.
    value_iterator++;
    input_index++;
1001 1002
  }
  CHECK_EQ(0u, output_offset);
1003

1004 1005 1006 1007
  // Compute this frame's PC and state. The PC will be a special builtin that
  // continues the bytecode dispatch. Note that non-topmost and lazy-style
  // bailout handlers also advance the bytecode offset before dispatch, hence
  // simulating what normal handlers do upon completion of the operation.
1008
  Builtins* builtins = isolate_->builtins();
1009
  Code* dispatch_builtin =
1010 1011 1012
      (!is_topmost || (bailout_type_ == LAZY)) && !goto_catch_handler
          ? builtins->builtin(Builtins::kInterpreterEnterBytecodeAdvance)
          : builtins->builtin(Builtins::kInterpreterEnterBytecodeDispatch);
1013
  output_frame->SetPc(reinterpret_cast<intptr_t>(dispatch_builtin->entry()));
1014 1015 1016
  // Restore accumulator (TOS) register.
  output_frame->SetState(
      Smi::FromInt(static_cast<int>(BailoutState::TOS_REGISTER)));
1017 1018 1019 1020

  // Update constant pool.
  if (FLAG_enable_embedded_constant_pool) {
    intptr_t constant_pool_value =
1021
        reinterpret_cast<intptr_t>(dispatch_builtin->constant_pool());
1022 1023 1024 1025 1026 1027 1028 1029
    output_frame->SetConstantPool(constant_pool_value);
    if (is_topmost) {
      Register constant_pool_reg =
          InterpretedFrame::constant_pool_pointer_register();
      output_frame->SetRegister(constant_pool_reg.code(), constant_pool_value);
    }
  }

1030 1031 1032 1033
  // Clear the context register. The context might be a de-materialized object
  // and will be materialized by {Runtime_NotifyDeoptimized}. For additional
  // safety we use Smi(0) instead of the potential {arguments_marker} here.
  if (is_topmost) {
1034
    intptr_t context_value = reinterpret_cast<intptr_t>(Smi::kZero);
1035 1036
    Register context_reg = JavaScriptFrame::context_register();
    output_frame->SetRegister(context_reg.code(), context_value);
1037
    // Set the continuation for the topmost frame.
1038
    Code* continuation = builtins->builtin(Builtins::kNotifyDeoptimized);
1039
    if (bailout_type_ == LAZY) {
1040
      continuation = builtins->builtin(Builtins::kNotifyLazyDeoptimized);
1041
    } else if (bailout_type_ == SOFT) {
1042
      continuation = builtins->builtin(Builtins::kNotifySoftDeoptimized);
1043 1044 1045 1046 1047 1048 1049 1050
    } else {
      CHECK_EQ(bailout_type_, EAGER);
    }
    output_frame->SetContinuation(
        reinterpret_cast<intptr_t>(continuation->entry()));
  }
}

1051 1052
void Deoptimizer::DoComputeArgumentsAdaptorFrame(
    TranslatedFrame* translated_frame, int frame_index) {
1053
  TranslatedFrame::iterator value_iterator = translated_frame->begin();
1054
  bool is_bottommost = (0 == frame_index);
1055 1056 1057
  int input_index = 0;

  unsigned height = translated_frame->height();
1058
  unsigned height_in_bytes = height * kPointerSize;
1059 1060
  JSFunction* function = JSFunction::cast(value_iterator->GetRawValue());
  value_iterator++;
1061
  input_index++;
1062 1063 1064
  if (trace_scope_ != NULL) {
    PrintF(trace_scope_->file(),
           "  translating arguments adaptor => height=%d\n", height_in_bytes);
1065 1066
  }

1067
  unsigned fixed_frame_size = ArgumentsAdaptorFrameConstants::kFixedFrameSize;
1068 1069 1070
  unsigned output_frame_size = height_in_bytes + fixed_frame_size;

  // Allocate and store the output frame description.
1071 1072 1073
  int parameter_count = height;
  FrameDescription* output_frame = new (output_frame_size)
      FrameDescription(output_frame_size, parameter_count);
1074

1075 1076
  // Arguments adaptor can not be topmost.
  CHECK(frame_index < output_count_ - 1);
1077
  CHECK(output_[frame_index] == NULL);
1078 1079
  output_[frame_index] = output_frame;

1080 1081
  // The top address of the frame is computed from the previous frame's top and
  // this frame's size.
1082
  intptr_t top_address;
1083 1084 1085 1086 1087
  if (is_bottommost) {
    top_address = caller_frame_top_ - output_frame_size;
  } else {
    top_address = output_[frame_index - 1]->GetTop() - output_frame_size;
  }
1088 1089 1090 1091 1092 1093
  output_frame->SetTop(top_address);

  // Compute the incoming parameter translation.
  unsigned output_offset = output_frame_size;
  for (int i = 0; i < parameter_count; ++i) {
    output_offset -= kPointerSize;
1094 1095
    WriteTranslatedValueToOutput(&value_iterator, &input_index, frame_index,
                                 output_offset);
1096 1097 1098
  }

  // Read caller's PC from the previous frame.
1099
  output_offset -= kPCOnStackSize;
1100 1101 1102 1103 1104 1105 1106 1107
  intptr_t value;
  if (is_bottommost) {
    value = caller_pc_;
  } else {
    value = output_[frame_index - 1]->GetPc();
  }
  output_frame->SetCallerPc(output_offset, value);
  DebugPrintOutputSlot(value, frame_index, output_offset, "caller's pc\n");
1108 1109

  // Read caller's FP from the previous frame, and set this frame's FP.
1110
  output_offset -= kFPOnStackSize;
1111 1112 1113 1114 1115
  if (is_bottommost) {
    value = caller_fp_;
  } else {
    value = output_[frame_index - 1]->GetFp();
  }
1116
  output_frame->SetCallerFp(output_offset, value);
1117 1118
  intptr_t fp_value = top_address + output_offset;
  output_frame->SetFp(fp_value);
1119
  DebugPrintOutputSlot(value, frame_index, output_offset, "caller's fp\n");
1120

1121
  if (FLAG_enable_embedded_constant_pool) {
1122
    // Read the caller's constant pool from the previous frame.
1123
    output_offset -= kPointerSize;
1124 1125 1126 1127 1128
    if (is_bottommost) {
      value = caller_constant_pool_;
    } else {
      value = output_[frame_index - 1]->GetConstantPool();
    }
1129
    output_frame->SetCallerConstantPool(output_offset, value);
1130 1131
    DebugPrintOutputSlot(value, frame_index, output_offset,
                         "caller's constant_pool\n");
1132 1133
  }

1134 1135
  // A marker value is used in place of the context.
  output_offset -= kPointerSize;
1136
  intptr_t context = StackFrame::TypeToMarker(StackFrame::ARGUMENTS_ADAPTOR);
1137
  output_frame->SetFrameSlot(output_offset, context);
1138 1139
  DebugPrintOutputSlot(context, frame_index, output_offset,
                       "context (adaptor sentinel)\n");
1140 1141 1142 1143

  // The function was mentioned explicitly in the ARGUMENTS_ADAPTOR_FRAME.
  output_offset -= kPointerSize;
  value = reinterpret_cast<intptr_t>(function);
1144
  WriteValueToOutput(function, 0, frame_index, output_offset, "function    ");
1145 1146 1147 1148 1149

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

1155
  DCHECK(0 == output_offset);
1156 1157 1158 1159 1160 1161 1162 1163

  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);
1164
  if (FLAG_enable_embedded_constant_pool) {
1165 1166 1167 1168
    intptr_t constant_pool_value =
        reinterpret_cast<intptr_t>(adaptor_trampoline->constant_pool());
    output_frame->SetConstantPool(constant_pool_value);
  }
1169 1170
}

1171 1172
void Deoptimizer::DoComputeConstructStubFrame(TranslatedFrame* translated_frame,
                                              int frame_index) {
1173
  TranslatedFrame::iterator value_iterator = translated_frame->begin();
1174 1175 1176 1177 1178
  bool is_topmost = (output_count_ - 1 == frame_index);
  // The construct frame could become topmost only if we inlined a constructor
  // call which does a tail call (otherwise the tail callee's frame would be
  // the topmost one). So it could only be the LAZY case.
  CHECK(!is_topmost || bailout_type_ == LAZY);
1179 1180
  int input_index = 0;

1181
  Builtins* builtins = isolate_->builtins();
1182 1183 1184 1185
  Code* construct_stub = builtins->builtin(
      FLAG_harmony_restrict_constructor_return
          ? Builtins::kJSConstructStubGenericRestrictedReturn
          : Builtins::kJSConstructStubGenericUnrestrictedReturn);
1186
  BailoutId bailout_id = translated_frame->node_id();
1187
  unsigned height = translated_frame->height();
1188
  unsigned height_in_bytes = height * kPointerSize;
1189 1190 1191 1192 1193

  // If the construct frame appears to be topmost we should ensure that the
  // value of result register is preserved during continuation execution.
  // We do this here by "pushing" the result of the constructor function to the
  // top of the reconstructed stack and then using the
1194
  // BailoutState::TOS_REGISTER machinery.
1195 1196 1197 1198
  if (is_topmost) {
    height_in_bytes += kPointerSize;
  }

1199
  JSFunction* function = JSFunction::cast(value_iterator->GetRawValue());
1200
  value_iterator++;
1201
  input_index++;
1202 1203
  if (trace_scope_ != NULL) {
    PrintF(trace_scope_->file(),
1204 1205 1206 1207
           "  translating construct stub => bailout_id=%d (%s), height=%d\n",
           bailout_id.ToInt(),
           bailout_id == BailoutId::ConstructStubCreate() ? "create" : "invoke",
           height_in_bytes);
1208 1209
  }

1210
  unsigned fixed_frame_size = ConstructFrameConstants::kFixedFrameSize;
1211 1212 1213 1214
  unsigned output_frame_size = height_in_bytes + fixed_frame_size;

  // Allocate and store the output frame description.
  FrameDescription* output_frame =
1215
      new (output_frame_size) FrameDescription(output_frame_size);
1216

1217 1218
  // Construct stub can not be topmost.
  DCHECK(frame_index > 0 && frame_index < output_count_);
1219
  DCHECK(output_[frame_index] == NULL);
1220 1221
  output_[frame_index] = output_frame;

1222 1223
  // The top address of the frame is computed from the previous frame's top and
  // this frame's size.
1224 1225 1226 1227 1228 1229 1230 1231 1232
  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;
1233 1234
    // The allocated receiver of a construct stub frame is passed as the
    // receiver parameter through the translation. It might be encoding
1235
    // a captured object, override the slot address for a captured object.
1236 1237
    WriteTranslatedValueToOutput(
        &value_iterator, &input_index, frame_index, output_offset, nullptr,
1238
        (i == 0) ? reinterpret_cast<Address>(top_address) : nullptr);
1239 1240 1241
  }

  // Read caller's PC from the previous frame.
1242
  output_offset -= kPCOnStackSize;
1243
  intptr_t callers_pc = output_[frame_index - 1]->GetPc();
1244
  output_frame->SetCallerPc(output_offset, callers_pc);
1245
  DebugPrintOutputSlot(callers_pc, frame_index, output_offset, "caller's pc\n");
1246 1247

  // Read caller's FP from the previous frame, and set this frame's FP.
1248
  output_offset -= kFPOnStackSize;
1249
  intptr_t value = output_[frame_index - 1]->GetFp();
1250
  output_frame->SetCallerFp(output_offset, value);
1251 1252
  intptr_t fp_value = top_address + output_offset;
  output_frame->SetFp(fp_value);
1253 1254 1255 1256
  if (is_topmost) {
    Register fp_reg = JavaScriptFrame::fp_register();
    output_frame->SetRegister(fp_reg.code(), fp_value);
  }
1257
  DebugPrintOutputSlot(value, frame_index, output_offset, "caller's fp\n");
1258

1259
  if (FLAG_enable_embedded_constant_pool) {
1260
    // Read the caller's constant pool from the previous frame.
1261 1262
    output_offset -= kPointerSize;
    value = output_[frame_index - 1]->GetConstantPool();
1263
    output_frame->SetCallerConstantPool(output_offset, value);
1264 1265
    DebugPrintOutputSlot(value, frame_index, output_offset,
                         "caller's constant_pool\n");
1266 1267
  }

1268
  // A marker value is used to mark the frame.
1269
  output_offset -= kPointerSize;
1270
  value = StackFrame::TypeToMarker(StackFrame::CONSTRUCT);
1271
  output_frame->SetFrameSlot(output_offset, value);
1272
  DebugPrintOutputSlot(value, frame_index, output_offset,
1273
                       "typed frame marker\n");
1274

1275
  // The context can be gotten from the previous frame.
1276
  output_offset -= kPointerSize;
1277
  value = output_[frame_index - 1]->GetContext();
1278
  output_frame->SetFrameSlot(output_offset, value);
1279
  DebugPrintOutputSlot(value, frame_index, output_offset, "context\n");
1280 1281 1282 1283 1284

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

1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301
  // The constructor function was mentioned explicitly in the
  // CONSTRUCT_STUB_FRAME.
  output_offset -= kPointerSize;
  value = reinterpret_cast<intptr_t>(function);
  WriteValueToOutput(function, 0, frame_index, output_offset,
                     "constructor function ");

  // The deopt info contains the implicit receiver or the new target at the
  // position of the receiver. Copy it to the top of stack.
  output_offset -= kPointerSize;
  value = output_frame->GetFrameSlot(output_frame_size - kPointerSize);
  output_frame->SetFrameSlot(output_offset, value);
1302
  if (bailout_id == BailoutId::ConstructStubCreate()) {
1303
    DebugPrintOutputSlot(value, frame_index, output_offset, "new target\n");
1304
  } else {
1305
    CHECK(bailout_id == BailoutId::ConstructStubInvoke());
1306 1307 1308
    DebugPrintOutputSlot(value, frame_index, output_offset,
                         "allocated receiver\n");
  }
1309

1310 1311 1312
  if (is_topmost) {
    // Ensure the result is restored back when we return to the stub.
    output_offset -= kPointerSize;
1313
    Register result_reg = kReturnRegister0;
1314 1315
    value = input_->GetRegister(result_reg.code());
    output_frame->SetFrameSlot(output_offset, value);
1316
    DebugPrintOutputSlot(value, frame_index, output_offset, "subcall result\n");
1317

1318 1319
    output_frame->SetState(
        Smi::FromInt(static_cast<int>(BailoutState::TOS_REGISTER)));
1320 1321
  }

1322
  CHECK_EQ(0u, output_offset);
1323

1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334
  // Compute this frame's PC.
  DCHECK(bailout_id.IsValidForConstructStub());
  Address start = construct_stub->instruction_start();
  int pc_offset =
      bailout_id == BailoutId::ConstructStubCreate()
          ? isolate_->heap()->construct_stub_create_deopt_pc_offset()->value()
          : isolate_->heap()->construct_stub_invoke_deopt_pc_offset()->value();
  intptr_t pc_value = reinterpret_cast<intptr_t>(start + pc_offset);
  output_frame->SetPc(pc_value);

  // Update constant pool.
1335
  if (FLAG_enable_embedded_constant_pool) {
1336 1337 1338
    intptr_t constant_pool_value =
        reinterpret_cast<intptr_t>(construct_stub->constant_pool());
    output_frame->SetConstantPool(constant_pool_value);
1339 1340 1341 1342 1343 1344 1345
    if (is_topmost) {
      Register constant_pool_reg =
          JavaScriptFrame::constant_pool_pointer_register();
      output_frame->SetRegister(constant_pool_reg.code(), fp_value);
    }
  }

1346 1347 1348 1349
  // Clear the context register. The context might be a de-materialized object
  // and will be materialized by {Runtime_NotifyDeoptimized}. For additional
  // safety we use Smi(0) instead of the potential {arguments_marker} here.
  if (is_topmost) {
1350
    intptr_t context_value = reinterpret_cast<intptr_t>(Smi::kZero);
1351 1352 1353 1354
    Register context_reg = JavaScriptFrame::context_register();
    output_frame->SetRegister(context_reg.code(), context_value);
  }

1355 1356 1357 1358 1359 1360 1361
  // Set the continuation for the topmost frame.
  if (is_topmost) {
    Builtins* builtins = isolate_->builtins();
    DCHECK_EQ(LAZY, bailout_type_);
    Code* continuation = builtins->builtin(Builtins::kNotifyLazyDeoptimized);
    output_frame->SetContinuation(
        reinterpret_cast<intptr_t>(continuation->entry()));
1362
  }
1363 1364
}

1365 1366
void Deoptimizer::DoComputeAccessorStubFrame(TranslatedFrame* translated_frame,
                                             int frame_index,
1367
                                             bool is_setter_stub_frame) {
1368
  TranslatedFrame::iterator value_iterator = translated_frame->begin();
1369 1370 1371 1372 1373
  bool is_topmost = (output_count_ - 1 == frame_index);
  // The accessor frame could become topmost only if we inlined an accessor
  // call which does a tail call (otherwise the tail callee's frame would be
  // the topmost one). So it could only be the LAZY case.
  CHECK(!is_topmost || bailout_type_ == LAZY);
1374 1375
  int input_index = 0;

1376
  // Skip accessor.
1377
  value_iterator++;
1378
  input_index++;
1379 1380 1381 1382 1383
  // 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;
1384 1385 1386 1387 1388

  // If the accessor frame appears to be topmost we should ensure that the
  // value of result register is preserved during continuation execution.
  // We do this here by "pushing" the result of the accessor function to the
  // top of the reconstructed stack and then using the
1389
  // BailoutState::TOS_REGISTER machinery.
1390 1391 1392 1393 1394 1395 1396
  // We don't need to restore the result in case of a setter call because we
  // have to return the stored value but not the result of the setter function.
  bool should_preserve_result = is_topmost && !is_setter_stub_frame;
  if (should_preserve_result) {
    height_in_bytes += kPointerSize;
  }

1397
  const char* kind = is_setter_stub_frame ? "setter" : "getter";
1398 1399 1400
  if (trace_scope_ != NULL) {
    PrintF(trace_scope_->file(),
           "  translating %s stub => height=%u\n", kind, height_in_bytes);
1401 1402
  }

1403
  // We need 1 stack entry for the return address and enough entries for the
1404
  // StackFrame::INTERNAL (FP, frame type, context, code object and constant
1405
  // pool (if enabled)- see MacroAssembler::EnterFrame).
1406 1407
  // For a setter stub frame we need one additional entry for the implicit
  // return value, see StoreStubCompiler::CompileStoreViaSetter.
1408 1409 1410
  unsigned fixed_frame_entries =
      (StandardFrameConstants::kFixedFrameSize / kPointerSize) + 1 +
      (is_setter_stub_frame ? 1 : 0);
1411 1412 1413 1414 1415
  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 =
1416
      new (output_frame_size) FrameDescription(output_frame_size);
1417

1418 1419
  // A frame for an accessor stub can not be bottommost.
  CHECK(frame_index > 0 && frame_index < output_count_);
1420
  CHECK_NULL(output_[frame_index]);
1421 1422 1423 1424 1425 1426 1427 1428 1429 1430
  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.
1431
  output_offset -= kPCOnStackSize;
1432
  intptr_t callers_pc = output_[frame_index - 1]->GetPc();
1433
  output_frame->SetCallerPc(output_offset, callers_pc);
1434
  DebugPrintOutputSlot(callers_pc, frame_index, output_offset, "caller's pc\n");
1435 1436

  // Read caller's FP from the previous frame, and set this frame's FP.
1437
  output_offset -= kFPOnStackSize;
1438
  intptr_t value = output_[frame_index - 1]->GetFp();
1439
  output_frame->SetCallerFp(output_offset, value);
1440 1441
  intptr_t fp_value = top_address + output_offset;
  output_frame->SetFp(fp_value);
1442 1443 1444 1445
  if (is_topmost) {
    Register fp_reg = JavaScriptFrame::fp_register();
    output_frame->SetRegister(fp_reg.code(), fp_value);
  }
1446
  DebugPrintOutputSlot(value, frame_index, output_offset, "caller's fp\n");
1447

1448
  if (FLAG_enable_embedded_constant_pool) {
1449
    // Read the caller's constant pool from the previous frame.
1450 1451
    output_offset -= kPointerSize;
    value = output_[frame_index - 1]->GetConstantPool();
1452
    output_frame->SetCallerConstantPool(output_offset, value);
1453 1454
    DebugPrintOutputSlot(value, frame_index, output_offset,
                         "caller's constant_pool\n");
1455 1456
  }

1457
  // Set the frame type.
1458
  output_offset -= kPointerSize;
1459
  value = StackFrame::TypeToMarker(StackFrame::INTERNAL);
1460
  output_frame->SetFrameSlot(output_offset, value);
1461
  DebugPrintOutputSlot(value, frame_index, output_offset, "frame type ");
1462 1463
  if (trace_scope_ != nullptr) {
    PrintF(trace_scope_->file(), "(%s sentinel)\n", kind);
1464 1465 1466 1467 1468 1469 1470 1471 1472 1473
  }

  // 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);
1474
  DebugPrintOutputSlot(value, frame_index, output_offset, "code object\n");
1475

1476 1477 1478 1479 1480 1481
  // The context can be gotten from the previous frame.
  output_offset -= kPointerSize;
  value = output_[frame_index - 1]->GetContext();
  output_frame->SetFrameSlot(output_offset, value);
  DebugPrintOutputSlot(value, frame_index, output_offset, "context\n");

1482
  // Skip receiver.
1483
  value_iterator++;
1484
  input_index++;
1485 1486 1487 1488 1489

  if (is_setter_stub_frame) {
    // The implicit return value was part of the artificial setter stub
    // environment.
    output_offset -= kPointerSize;
1490 1491
    WriteTranslatedValueToOutput(&value_iterator, &input_index, frame_index,
                                 output_offset);
1492 1493
  }

1494 1495 1496
  if (should_preserve_result) {
    // Ensure the result is restored back when we return to the stub.
    output_offset -= kPointerSize;
1497
    Register result_reg = kReturnRegister0;
1498 1499 1500 1501 1502
    value = input_->GetRegister(result_reg.code());
    output_frame->SetFrameSlot(output_offset, value);
    DebugPrintOutputSlot(value, frame_index, output_offset,
                         "accessor result\n");

1503 1504
    output_frame->SetState(
        Smi::FromInt(static_cast<int>(BailoutState::TOS_REGISTER)));
1505
  } else {
1506 1507
    output_frame->SetState(
        Smi::FromInt(static_cast<int>(BailoutState::NO_REGISTERS)));
1508 1509
  }

1510
  CHECK_EQ(0u, output_offset);
1511 1512 1513 1514 1515 1516 1517

  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);
1518 1519

  // Update constant pool.
1520
  if (FLAG_enable_embedded_constant_pool) {
1521 1522 1523
    intptr_t constant_pool_value =
        reinterpret_cast<intptr_t>(accessor_stub->constant_pool());
    output_frame->SetConstantPool(constant_pool_value);
1524 1525 1526 1527 1528 1529 1530
    if (is_topmost) {
      Register constant_pool_reg =
          JavaScriptFrame::constant_pool_pointer_register();
      output_frame->SetRegister(constant_pool_reg.code(), fp_value);
    }
  }

1531 1532 1533 1534
  // Clear the context register. The context might be a de-materialized object
  // and will be materialized by {Runtime_NotifyDeoptimized}. For additional
  // safety we use Smi(0) instead of the potential {arguments_marker} here.
  if (is_topmost) {
1535
    intptr_t context_value = reinterpret_cast<intptr_t>(Smi::kZero);
1536 1537 1538 1539
    Register context_reg = JavaScriptFrame::context_register();
    output_frame->SetRegister(context_reg.code(), context_value);
  }

1540 1541 1542 1543 1544 1545 1546
  // Set the continuation for the topmost frame.
  if (is_topmost) {
    Builtins* builtins = isolate_->builtins();
    DCHECK_EQ(LAZY, bailout_type_);
    Code* continuation = builtins->builtin(Builtins::kNotifyLazyDeoptimized);
    output_frame->SetContinuation(
        reinterpret_cast<intptr_t>(continuation->entry()));
1547
  }
1548 1549
}

1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615
// BuiltinContinuationFrames capture the machine state that is expected as input
// to a builtin, including both input register values and stack parameters. When
// the frame is reactivated (i.e. the frame below it returns), a
// ContinueToBuiltin stub restores the register state from the frame and tail
// calls to the actual target builtin, making it appear that the stub had been
// directly called by the frame above it. The input values to populate the frame
// are taken from the deopt's FrameState.
//
// Frame translation happens in two modes, EAGER and LAZY. In EAGER mode, all of
// the parameters to the Builtin are explicitly specified in the TurboFan
// FrameState node. In LAZY mode, there is always one fewer parameters specified
// in the FrameState than expected by the Builtin. In that case, construction of
// BuiltinContinuationFrame adds the final missing parameter during
// deoptimization, and that parameter is always on the stack and contains the
// value returned from the callee of the call site triggering the LAZY deopt
// (e.g. rax on x64). This requires that continuation Builtins for LAZY deopts
// must have at least one stack parameter.
//
//                TO
//    |          ....           |
//    +-------------------------+
//    |     builtin param 0     |<- FrameState input value n becomes
//    +-------------------------+
//    |           ...           |
//    +-------------------------+
//    |     builtin param m     |<- FrameState input value n+m-1, or in
//    +-------------------------+   the LAZY case, return LAZY result value
//    | ContinueToBuiltin entry |
//    +-------------------------+
// |  |    saved frame (FP)     |
// |  +=========================+<- fpreg
// |  |constant pool (if ool_cp)|
// v  +-------------------------+
//    |BUILTIN_CONTINUATION mark|
//    +-------------------------+
//    |  JS Builtin code object |
//    +-------------------------+
//    | builtin input GPR reg0  |<- populated from deopt FrameState using
//    +-------------------------+   the builtin's CallInterfaceDescriptor
//    |          ...            |   to map a FrameState's 0..n-1 inputs to
//    +-------------------------+   the builtin's n input register params.
//    | builtin input GPR regn  |
//    |-------------------------|<- spreg
//
void Deoptimizer::DoComputeBuiltinContinuation(
    TranslatedFrame* translated_frame, int frame_index,
    bool java_script_builtin) {
  TranslatedFrame::iterator value_iterator = translated_frame->begin();
  int input_index = 0;

  // The output frame must have room for all of the parameters that need to be
  // passed to the builtin continuation.
  int height_in_words = translated_frame->height();

  BailoutId bailout_id = translated_frame->node_id();
  Builtins::Name builtin_name = Builtins::GetBuiltinFromBailoutId(bailout_id);
  Code* builtin = isolate()->builtins()->builtin(builtin_name);
  Callable continuation_callable =
      Builtins::CallableFor(isolate(), builtin_name);
  CallInterfaceDescriptor continuation_descriptor =
      continuation_callable.descriptor();

  bool is_bottommost = (0 == frame_index);
  bool is_topmost = (output_count_ - 1 == frame_index);
  bool must_handle_result = !is_topmost || bailout_type_ == LAZY;

1616
  const RegisterConfiguration* config(RegisterConfiguration::Default());
1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647
  int allocatable_register_count = config->num_allocatable_general_registers();
  int register_parameter_count =
      continuation_descriptor.GetRegisterParameterCount();
  // Make sure to account for the context by removing it from the register
  // parameter count.
  int stack_param_count = height_in_words - register_parameter_count - 1;
  if (must_handle_result) stack_param_count++;
  int output_frame_size =
      kPointerSize * (stack_param_count + allocatable_register_count) +
      TYPED_FRAME_SIZE(2);  // For destination builtin code and registers

  // Validate types of parameters. They must all be tagged except for argc for
  // JS builtins.
  bool has_argc = false;
  for (int i = 0; i < register_parameter_count; ++i) {
    MachineType type = continuation_descriptor.GetParameterType(i);
    int code = continuation_descriptor.GetRegisterParameter(i).code();
    // Only tagged and int32 arguments are supported, and int32 only for the
    // arguments count on JavaScript builtins.
    if (type == MachineType::Int32()) {
      CHECK_EQ(code, kJavaScriptCallArgCountRegister.code());
      has_argc = true;
    } else {
      // Any other argument must be a tagged value.
      CHECK(IsAnyTagged(type.representation()));
    }
  }
  CHECK_EQ(java_script_builtin, has_argc);

  if (trace_scope_ != NULL) {
    PrintF(trace_scope_->file(),
1648 1649 1650 1651 1652
           "  translating BuiltinContinuation to %s,"
           " register param count %d,"
           " stack param count %d\n",
           Builtins::name(builtin_name), register_parameter_count,
           stack_param_count);
1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686
  }

  unsigned output_frame_offset = output_frame_size;
  FrameDescription* output_frame =
      new (output_frame_size) FrameDescription(output_frame_size);
  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;
  if (is_bottommost) {
    top_address = caller_frame_top_ - output_frame_size;
  } else {
    top_address = output_[frame_index - 1]->GetTop() - output_frame_size;
  }
  output_frame->SetTop(top_address);

  output_frame->SetState(
      Smi::FromInt(static_cast<int>(BailoutState::NO_REGISTERS)));

  // Get the possible JSFunction for the case that
  intptr_t maybe_function =
      reinterpret_cast<intptr_t>(value_iterator->GetRawValue());
  ++value_iterator;

  std::vector<intptr_t> register_values;
  int total_registers = config->num_general_registers();
  register_values.resize(total_registers, 0);
  for (int i = 0; i < total_registers; ++i) {
    register_values[i] = 0;
  }

  intptr_t value;

1687
  Register result_reg = kReturnRegister0;
1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 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 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838
  if (must_handle_result) {
    value = input_->GetRegister(result_reg.code());
  } else {
    value = reinterpret_cast<intptr_t>(isolate()->heap()->undefined_value());
  }
  output_frame->SetRegister(result_reg.code(), value);

  int translated_stack_parameters =
      must_handle_result ? stack_param_count - 1 : stack_param_count;

  for (int i = 0; i < translated_stack_parameters; ++i) {
    output_frame_offset -= kPointerSize;
    WriteTranslatedValueToOutput(&value_iterator, &input_index, frame_index,
                                 output_frame_offset);
  }

  if (must_handle_result) {
    output_frame_offset -= kPointerSize;
    WriteValueToOutput(isolate()->heap()->the_hole_value(), input_index,
                       frame_index, output_frame_offset,
                       "placeholder for return result on lazy deopt ");
  }

  for (int i = 0; i < register_parameter_count; ++i) {
    value = reinterpret_cast<intptr_t>(value_iterator->GetRawValue());
    int code = continuation_descriptor.GetRegisterParameter(i).code();
    register_values[code] = value;
    ++input_index;
    ++value_iterator;
  }

  // The context register is always implicit in the CallInterfaceDescriptor but
  // its register must be explicitly set when continuing to the builtin. Make
  // sure that it's harvested from the translation and copied into the register
  // set (it was automatically added at the end of the FrameState by the
  // instruction selector).
  value = reinterpret_cast<intptr_t>(value_iterator->GetRawValue());
  register_values[kContextRegister.code()] = value;
  output_frame->SetContext(value);
  output_frame->SetRegister(kContextRegister.code(), value);
  ++input_index;
  ++value_iterator;

  // Set caller's PC (JSFunction continuation).
  output_frame_offset -= kPCOnStackSize;
  if (is_bottommost) {
    value = caller_pc_;
  } else {
    value = output_[frame_index - 1]->GetPc();
  }
  output_frame->SetCallerPc(output_frame_offset, value);
  DebugPrintOutputSlot(value, frame_index, output_frame_offset,
                       "caller's pc\n");

  // Read caller's FP from the previous frame, and set this frame's FP.
  output_frame_offset -= kFPOnStackSize;
  if (is_bottommost) {
    value = caller_fp_;
  } else {
    value = output_[frame_index - 1]->GetFp();
  }
  output_frame->SetCallerFp(output_frame_offset, value);
  intptr_t fp_value = top_address + output_frame_offset;
  output_frame->SetFp(fp_value);
  DebugPrintOutputSlot(value, frame_index, output_frame_offset,
                       "caller's fp\n");

  if (FLAG_enable_embedded_constant_pool) {
    // Read the caller's constant pool from the previous frame.
    output_frame_offset -= kPointerSize;
    if (is_bottommost) {
      value = caller_constant_pool_;
    } else {
      value = output_[frame_index - 1]->GetConstantPool();
    }
    output_frame->SetCallerConstantPool(output_frame_offset, value);
    DebugPrintOutputSlot(value, frame_index, output_frame_offset,
                         "caller's constant_pool\n");
  }

  // A marker value is used in place of the context.
  output_frame_offset -= kPointerSize;
  intptr_t marker =
      java_script_builtin
          ? StackFrame::TypeToMarker(
                StackFrame::JAVA_SCRIPT_BUILTIN_CONTINUATION)
          : StackFrame::TypeToMarker(StackFrame::BUILTIN_CONTINUATION);
  output_frame->SetFrameSlot(output_frame_offset, marker);
  DebugPrintOutputSlot(marker, frame_index, output_frame_offset,
                       "context (builtin continuation sentinel)\n");

  output_frame_offset -= kPointerSize;
  value = java_script_builtin ? maybe_function : 0;
  output_frame->SetFrameSlot(output_frame_offset, value);
  DebugPrintOutputSlot(value, frame_index, output_frame_offset,
                       java_script_builtin ? "JSFunction\n" : "unused\n");

  // The builtin to continue to
  output_frame_offset -= kPointerSize;
  value = reinterpret_cast<intptr_t>(builtin);
  output_frame->SetFrameSlot(output_frame_offset, value);
  DebugPrintOutputSlot(value, frame_index, output_frame_offset,
                       "builtin address\n");

  for (int i = 0; i < allocatable_register_count; ++i) {
    output_frame_offset -= kPointerSize;
    int code = config->GetAllocatableGeneralCode(i);
    value = register_values[code];
    output_frame->SetFrameSlot(output_frame_offset, value);
    if (trace_scope_ != nullptr) {
      ScopedVector<char> str(128);
      if (java_script_builtin &&
          code == kJavaScriptCallArgCountRegister.code()) {
        SNPrintF(
            str,
            "tagged argument count %s (will be untagged by continuation)\n",
            config->GetGeneralRegisterName(code));
      } else {
        SNPrintF(str, "builtin register argument %s\n",
                 config->GetGeneralRegisterName(code));
      }
      DebugPrintOutputSlot(value, frame_index, output_frame_offset,
                           str.start());
    }
  }

  // Ensure the frame pointer register points to the callee's frame. The builtin
  // will build its own frame once we continue to it.
  Register fp_reg = JavaScriptFrame::fp_register();
  output_frame->SetRegister(fp_reg.code(), output_[frame_index - 1]->GetFp());

  Code* continue_to_builtin =
      java_script_builtin
          ? (must_handle_result
                 ? isolate()->builtins()->builtin(
                       Builtins::kContinueToJavaScriptBuiltinWithResult)
                 : isolate()->builtins()->builtin(
                       Builtins::kContinueToJavaScriptBuiltin))
          : (must_handle_result
                 ? isolate()->builtins()->builtin(
                       Builtins::kContinueToCodeStubBuiltinWithResult)
                 : isolate()->builtins()->builtin(
                       Builtins::kContinueToCodeStubBuiltin));
  output_frame->SetPc(
      reinterpret_cast<intptr_t>(continue_to_builtin->instruction_start()));

  Code* continuation =
      isolate()->builtins()->builtin(Builtins::kNotifyBuiltinContinuation);
  output_frame->SetContinuation(
      reinterpret_cast<intptr_t>(continuation->entry()));
}
1839

1840
void Deoptimizer::MaterializeHeapObjects(JavaScriptFrameIterator* it) {
1841 1842
  // Walk to the last JavaScript output frame to find out if it has
  // adapted arguments.
1843 1844
  for (int frame_index = 0; frame_index < jsframe_count(); ++frame_index) {
    if (frame_index != 0) it->Advance();
1845
  }
1846
  translated_state_.Prepare(reinterpret_cast<Address>(stack_fp_));
1847

1848 1849
  for (auto& materialization : values_to_materialize_) {
    Handle<Object> value = materialization.value_->GetValue();
1850

1851 1852 1853 1854 1855 1856
    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");
1857
    }
1858

1859 1860
    *(reinterpret_cast<intptr_t*>(materialization.output_slot_address_)) =
        reinterpret_cast<intptr_t>(*value);
1861
  }
jarin@chromium.org's avatar
jarin@chromium.org committed
1862

1863 1864
  isolate_->materialized_object_store()->Remove(
      reinterpret_cast<Address>(stack_fp_));
1865 1866 1867
}


1868
void Deoptimizer::WriteTranslatedValueToOutput(
1869
    TranslatedFrame::iterator* iterator, int* input_index, int frame_index,
1870 1871
    unsigned output_offset, const char* debug_hint_string,
    Address output_address_for_materialization) {
1872
  Object* value = (*iterator)->GetRawValue();
1873

1874 1875
  WriteValueToOutput(value, *input_index, frame_index, output_offset,
                     debug_hint_string);
1876

1877
  if (value == isolate_->heap()->arguments_marker()) {
1878 1879 1880
    Address output_address =
        reinterpret_cast<Address>(output_[frame_index]->GetTop()) +
        output_offset;
1881 1882
    if (output_address_for_materialization == nullptr) {
      output_address_for_materialization = output_address;
1883
    }
1884 1885 1886
    values_to_materialize_.push_back(
        {output_address_for_materialization, *iterator});
  }
1887

1888 1889 1890
  (*iterator)++;
  (*input_index)++;
}
1891 1892


1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921
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);
  }
}

1922
unsigned Deoptimizer::ComputeInputFrameAboveFpFixedSize() const {
1923
  unsigned fixed_size = CommonFrameConstants::kFixedFrameSizeAboveFp;
1924 1925 1926
  if (!function_->IsSmi()) {
    fixed_size += ComputeIncomingArgumentSize(function_->shared());
  }
1927 1928 1929 1930
  return fixed_size;
}

unsigned Deoptimizer::ComputeInputFrameSize() const {
1931 1932
  // 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.
1933 1934
  unsigned fixed_size_above_fp = ComputeInputFrameAboveFpFixedSize();
  unsigned result = fixed_size_above_fp + fp_to_sp_delta_;
1935 1936
  if (compiled_code_->kind() == Code::OPTIMIZED_FUNCTION) {
    unsigned stack_slots = compiled_code_->stack_slots();
1937 1938
    unsigned outgoing_size = 0;
    //        ComputeOutgoingArgumentSize(compiled_code_, bailout_id_);
1939 1940 1941
    CHECK_EQ(fixed_size_above_fp + (stack_slots * kPointerSize) -
                 CommonFrameConstants::kFixedFrameSizeAboveFp + outgoing_size,
             result);
1942
  }
1943
  return result;
1944 1945
}

1946 1947
// static
unsigned Deoptimizer::ComputeJavascriptFixedSize(SharedFunctionInfo* shared) {
1948 1949
  // The fixed part of the frame consists of the return address, frame
  // pointer, function, context, and all the incoming arguments.
1950
  return ComputeIncomingArgumentSize(shared) +
1951 1952
         StandardFrameConstants::kFixedFrameSize;
}
1953

1954 1955
// static
unsigned Deoptimizer::ComputeInterpretedFixedSize(SharedFunctionInfo* shared) {
1956
  // The fixed part of the frame consists of the return address, frame
1957
  // pointer, function, context, bytecode offset and all the incoming arguments.
1958
  return ComputeIncomingArgumentSize(shared) +
1959 1960 1961
         InterpreterFrameConstants::kFixedFrameSize;
}

1962 1963 1964
// static
unsigned Deoptimizer::ComputeIncomingArgumentSize(SharedFunctionInfo* shared) {
  return (shared->internal_formal_parameter_count() + 1) * kPointerSize;
1965
}
1966

1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980
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);
1981

1982
  MacroAssembler masm(isolate, NULL, 16 * KB, CodeObjectRequired::kYes);
1983 1984 1985
  masm.set_emit_debug_code(false);
  GenerateDeoptimizationEntries(&masm, entry_count, type);
  CodeDesc desc;
1986
  masm.GetCode(isolate, &desc);
1987
  DCHECK(!RelocInfo::RequiresRelocation(isolate, desc));
1988

1989 1990 1991 1992 1993 1994 1995 1996 1997
  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));
1998
  Assembler::FlushICache(isolate, chunk->area_start(), desc.instr_size);
1999

2000 2001
  data->deopt_entry_code_entries_[type] = entry_count;
}
2002

2003 2004 2005 2006 2007 2008
void Deoptimizer::EnsureCodeForMaxDeoptimizationEntries(Isolate* isolate) {
  EnsureCodeForDeoptimizationEntry(isolate, EAGER, kMaxNumberOfEntries - 1);
  EnsureCodeForDeoptimizationEntry(isolate, LAZY, kMaxNumberOfEntries - 1);
  EnsureCodeForDeoptimizationEntry(isolate, SOFT, kMaxNumberOfEntries - 1);
}

2009
FrameDescription::FrameDescription(uint32_t frame_size, int parameter_count)
2010
    : frame_size_(frame_size),
2011
      parameter_count_(parameter_count),
2012 2013
      top_(kZapUint32),
      pc_(kZapUint32),
2014
      fp_(kZapUint32),
2015 2016
      context_(kZapUint32),
      constant_pool_(kZapUint32) {
2017 2018
  // Zap all the registers.
  for (int r = 0; r < Register::kNumRegisters; r++) {
2019 2020 2021
    // 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.
2022 2023 2024 2025 2026 2027 2028 2029 2030
    SetRegister(r, kZapUint32);
  }

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

2031
void TranslationBuffer::Add(int32_t value) {
2032 2033
  // This wouldn't handle kMinInt correctly if it ever encountered it.
  DCHECK(value != kMinInt);
2034 2035 2036 2037 2038 2039 2040 2041
  // 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;
2042
    contents_.push_back(((bits << 1) & 0xFF) | (next != 0));
2043 2044 2045 2046 2047 2048 2049 2050 2051 2052
    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) {
2053
    DCHECK(HasNext());
2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064
    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;
}


2065
Handle<ByteArray> TranslationBuffer::CreateByteArray(Factory* factory) {
2066 2067
  Handle<ByteArray> result = factory->NewByteArray(CurrentIndex(), TENURED);
  contents_.CopyTo(result->GetDataStartAddress());
2068 2069 2070
  return result;
}

2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088
void Translation::BeginBuiltinContinuationFrame(BailoutId bailout_id,
                                                int literal_id,
                                                unsigned height) {
  buffer_->Add(BUILTIN_CONTINUATION_FRAME);
  buffer_->Add(bailout_id.ToInt());
  buffer_->Add(literal_id);
  buffer_->Add(height);
}

void Translation::BeginJavaScriptBuiltinContinuationFrame(BailoutId bailout_id,
                                                          int literal_id,
                                                          unsigned height) {
  buffer_->Add(JAVA_SCRIPT_BUILTIN_CONTINUATION_FRAME);
  buffer_->Add(bailout_id.ToInt());
  buffer_->Add(literal_id);
  buffer_->Add(height);
}

2089 2090
void Translation::BeginConstructStubFrame(BailoutId bailout_id, int literal_id,
                                          unsigned height) {
2091
  buffer_->Add(CONSTRUCT_STUB_FRAME);
2092
  buffer_->Add(bailout_id.ToInt());
2093 2094
  buffer_->Add(literal_id);
  buffer_->Add(height);
2095 2096 2097
}


2098
void Translation::BeginGetterStubFrame(int literal_id) {
2099 2100
  buffer_->Add(GETTER_STUB_FRAME);
  buffer_->Add(literal_id);
2101 2102 2103
}


2104
void Translation::BeginSetterStubFrame(int literal_id) {
2105 2106
  buffer_->Add(SETTER_STUB_FRAME);
  buffer_->Add(literal_id);
2107 2108 2109
}


2110
void Translation::BeginArgumentsAdaptorFrame(int literal_id, unsigned height) {
2111 2112 2113
  buffer_->Add(ARGUMENTS_ADAPTOR_FRAME);
  buffer_->Add(literal_id);
  buffer_->Add(height);
2114 2115
}

2116 2117
void Translation::BeginInterpretedFrame(BailoutId bytecode_offset,
                                        int literal_id, unsigned height) {
2118 2119 2120 2121
  buffer_->Add(INTERPRETED_FRAME);
  buffer_->Add(bytecode_offset.ToInt());
  buffer_->Add(literal_id);
  buffer_->Add(height);
2122 2123 2124
}


2125 2126 2127 2128
void Translation::ArgumentsElements(bool is_rest) {
  buffer_->Add(ARGUMENTS_ELEMENTS);
  buffer_->Add(is_rest);
}
2129

2130 2131 2132 2133 2134
void Translation::ArgumentsLength(bool is_rest) {
  buffer_->Add(ARGUMENTS_LENGTH);
  buffer_->Add(is_rest);
}

2135
void Translation::BeginCapturedObject(int length) {
2136 2137
  buffer_->Add(CAPTURED_OBJECT);
  buffer_->Add(length);
2138 2139 2140 2141
}


void Translation::DuplicateObject(int object_index) {
2142 2143
  buffer_->Add(DUPLICATED_OBJECT);
  buffer_->Add(object_index);
2144 2145 2146
}


2147
void Translation::StoreRegister(Register reg) {
2148 2149
  buffer_->Add(REGISTER);
  buffer_->Add(reg.code());
2150 2151 2152 2153
}


void Translation::StoreInt32Register(Register reg) {
2154 2155
  buffer_->Add(INT32_REGISTER);
  buffer_->Add(reg.code());
2156 2157 2158
}


2159
void Translation::StoreUint32Register(Register reg) {
2160 2161
  buffer_->Add(UINT32_REGISTER);
  buffer_->Add(reg.code());
2162 2163 2164
}


2165
void Translation::StoreBoolRegister(Register reg) {
2166 2167
  buffer_->Add(BOOL_REGISTER);
  buffer_->Add(reg.code());
2168 2169
}

2170
void Translation::StoreFloatRegister(FloatRegister reg) {
2171 2172
  buffer_->Add(FLOAT_REGISTER);
  buffer_->Add(reg.code());
2173
}
2174

2175
void Translation::StoreDoubleRegister(DoubleRegister reg) {
2176 2177
  buffer_->Add(DOUBLE_REGISTER);
  buffer_->Add(reg.code());
2178 2179 2180 2181
}


void Translation::StoreStackSlot(int index) {
2182 2183
  buffer_->Add(STACK_SLOT);
  buffer_->Add(index);
2184 2185 2186 2187
}


void Translation::StoreInt32StackSlot(int index) {
2188 2189
  buffer_->Add(INT32_STACK_SLOT);
  buffer_->Add(index);
2190 2191 2192
}


2193
void Translation::StoreUint32StackSlot(int index) {
2194 2195
  buffer_->Add(UINT32_STACK_SLOT);
  buffer_->Add(index);
2196 2197 2198
}


2199
void Translation::StoreBoolStackSlot(int index) {
2200 2201
  buffer_->Add(BOOL_STACK_SLOT);
  buffer_->Add(index);
2202 2203
}

2204
void Translation::StoreFloatStackSlot(int index) {
2205 2206
  buffer_->Add(FLOAT_STACK_SLOT);
  buffer_->Add(index);
2207
}
2208

2209
void Translation::StoreDoubleStackSlot(int index) {
2210 2211
  buffer_->Add(DOUBLE_STACK_SLOT);
  buffer_->Add(index);
2212 2213 2214 2215
}


void Translation::StoreLiteral(int literal_id) {
2216 2217
  buffer_->Add(LITERAL);
  buffer_->Add(literal_id);
2218 2219 2220
}


2221
void Translation::StoreJSFrameFunction() {
2222
  StoreStackSlot((StandardFrameConstants::kCallerPCOffset -
2223
                  StandardFrameConstants::kFunctionOffset) /
2224
                 kPointerSize);
2225 2226
}

2227 2228
int Translation::NumberOfOperandsFor(Opcode opcode) {
  switch (opcode) {
2229
    case GETTER_STUB_FRAME:
2230
    case SETTER_STUB_FRAME:
2231 2232
    case DUPLICATED_OBJECT:
    case CAPTURED_OBJECT:
2233 2234
    case REGISTER:
    case INT32_REGISTER:
2235
    case UINT32_REGISTER:
2236
    case BOOL_REGISTER:
2237
    case FLOAT_REGISTER:
2238 2239 2240
    case DOUBLE_REGISTER:
    case STACK_SLOT:
    case INT32_STACK_SLOT:
2241
    case UINT32_STACK_SLOT:
2242
    case BOOL_STACK_SLOT:
2243
    case FLOAT_STACK_SLOT:
2244 2245 2246
    case DOUBLE_STACK_SLOT:
    case LITERAL:
      return 1;
2247 2248 2249
    case BEGIN:
    case ARGUMENTS_ADAPTOR_FRAME:
      return 2;
2250
    case INTERPRETED_FRAME:
2251
    case CONSTRUCT_STUB_FRAME:
2252 2253
    case BUILTIN_CONTINUATION_FRAME:
    case JAVA_SCRIPT_BUILTIN_CONTINUATION_FRAME:
2254
      return 3;
2255
    case ARGUMENTS_ELEMENTS:
2256
    case ARGUMENTS_LENGTH:
2257
      return 1;
2258
  }
2259
  FATAL("Unexpected translation type");
2260 2261 2262 2263
  return -1;
}


2264
#if defined(OBJECT_PRINT) || defined(ENABLE_DISASSEMBLER)
2265 2266

const char* Translation::StringFor(Opcode opcode) {
2267
#define TRANSLATION_OPCODE_CASE(item)   case item: return #item;
2268
  switch (opcode) {
2269
    TRANSLATION_OPCODE_LIST(TRANSLATION_OPCODE_CASE)
2270
  }
2271
#undef TRANSLATION_OPCODE_CASE
2272 2273 2274 2275 2276 2277
  UNREACHABLE();
}

#endif


jarin@chromium.org's avatar
jarin@chromium.org committed
2278 2279 2280 2281 2282 2283
Handle<FixedArray> MaterializedObjectStore::Get(Address fp) {
  int index = StackIdToIndex(fp);
  if (index == -1) {
    return Handle<FixedArray>::null();
  }
  Handle<FixedArray> array = GetStackEntries();
2284
  CHECK_GT(array->length(), index);
2285
  return Handle<FixedArray>::cast(Handle<Object>(array->get(index), isolate()));
jarin@chromium.org's avatar
jarin@chromium.org committed
2286 2287 2288 2289
}


void MaterializedObjectStore::Set(Address fp,
2290
                                  Handle<FixedArray> materialized_objects) {
jarin@chromium.org's avatar
jarin@chromium.org committed
2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301
  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);
}


2302
bool MaterializedObjectStore::Remove(Address fp) {
jarin@chromium.org's avatar
jarin@chromium.org committed
2303
  int index = StackIdToIndex(fp);
2304 2305 2306
  if (index == -1) {
    return false;
  }
2307
  CHECK_GE(index, 0);
jarin@chromium.org's avatar
jarin@chromium.org committed
2308 2309

  frame_fps_.Remove(index);
2310
  FixedArray* array = isolate()->heap()->materialized_objects();
2311
  CHECK_LT(index, array->length());
jarin@chromium.org's avatar
jarin@chromium.org committed
2312 2313 2314 2315
  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());
2316
  return true;
jarin@chromium.org's avatar
jarin@chromium.org committed
2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353
}


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());
  }
2354
  isolate()->heap()->SetRootMaterializedObjects(*new_array);
jarin@chromium.org's avatar
jarin@chromium.org committed
2355
  return new_array;
2356 2357
}

2358
namespace {
2359

2360 2361 2362 2363 2364
Handle<Object> GetValueForDebugger(TranslatedFrame::iterator it,
                                   Isolate* isolate) {
  if (it->GetRawValue() == isolate->heap()->arguments_marker()) {
    if (!it->IsMaterializableByDebugger()) {
      return isolate->factory()->undefined_value();
2365
    }
2366
  }
2367 2368
  return it->GetValue();
}
2369

2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387
}  // namespace

DeoptimizedFrameInfo::DeoptimizedFrameInfo(TranslatedState* state,
                                           TranslatedState::iterator frame_it,
                                           Isolate* isolate) {
  // If the previous frame is an adaptor frame, we will take the parameters
  // from there.
  TranslatedState::iterator parameter_frame = frame_it;
  if (parameter_frame != state->begin()) {
    parameter_frame--;
  }
  int parameter_count;
  if (parameter_frame->kind() == TranslatedFrame::kArgumentsAdaptor) {
    parameter_count = parameter_frame->height() - 1;  // Ignore the receiver.
  } else {
    parameter_frame = frame_it;
    parameter_count =
        frame_it->shared_info()->internal_formal_parameter_count();
2388
  }
2389 2390 2391 2392 2393 2394 2395 2396 2397 2398
  TranslatedFrame::iterator parameter_it = parameter_frame->begin();
  parameter_it++;  // Skip the function.
  parameter_it++;  // Skip the receiver.

  // Figure out whether there is a construct stub frame on top of
  // the parameter frame.
  has_construct_stub_ =
      parameter_frame != state->begin() &&
      (parameter_frame - 1)->kind() == TranslatedFrame::kConstructStub;

2399 2400 2401
  DCHECK_EQ(TranslatedFrame::kInterpretedFunction, frame_it->kind());
  source_position_ = Deoptimizer::ComputeSourcePositionFromBytecodeArray(
      *frame_it->shared_info(), frame_it->node_id());
2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429

  TranslatedFrame::iterator value_it = frame_it->begin();
  // Get the function. Note that this might materialize the function.
  // In case the debugger mutates this value, we should deoptimize
  // the function and remember the value in the materialized value store.
  function_ = Handle<JSFunction>::cast(value_it->GetValue());

  parameters_.resize(static_cast<size_t>(parameter_count));
  for (int i = 0; i < parameter_count; i++) {
    Handle<Object> parameter = GetValueForDebugger(parameter_it, isolate);
    SetParameter(i, parameter);
    parameter_it++;
  }

  // Skip the function, the receiver and the arguments.
  int skip_count =
      frame_it->shared_info()->internal_formal_parameter_count() + 2;
  TranslatedFrame::iterator stack_it = frame_it->begin();
  for (int i = 0; i < skip_count; i++) {
    stack_it++;
  }

  // Get the context.
  context_ = GetValueForDebugger(stack_it, isolate);
  stack_it++;

  // Get the expression stack.
  int stack_height = frame_it->height();
2430
  if (frame_it->kind() == TranslatedFrame::kInterpretedFunction) {
2431
    // For interpreter frames, we should not count the accumulator.
2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442
    // TODO(jarin): Clean up the indexing in translated frames.
    stack_height--;
  }
  expression_stack_.resize(static_cast<size_t>(stack_height));
  for (int i = 0; i < stack_height; i++) {
    Handle<Object> expression = GetValueForDebugger(stack_it, isolate);
    SetExpression(i, expression);
    stack_it++;
  }

  // For interpreter frame, skip the accumulator.
2443
  if (frame_it->kind() == TranslatedFrame::kInterpretedFunction) {
2444 2445 2446
    stack_it++;
  }
  CHECK(stack_it == frame_it->end());
2447 2448 2449
}


2450
Deoptimizer::DeoptInfo Deoptimizer::GetDeoptInfo(Code* code, Address pc) {
2451
  CHECK(code->instruction_start() <= pc && pc <= code->instruction_end());
2452
  SourcePosition last_position = SourcePosition::Unknown();
2453
  DeoptimizeReason last_reason = DeoptimizeReason::kNoReason;
2454
  int last_deopt_id = kNoDeoptimizationId;
2455
  int mask = RelocInfo::ModeMask(RelocInfo::DEOPT_REASON) |
2456
             RelocInfo::ModeMask(RelocInfo::DEOPT_ID) |
2457 2458
             RelocInfo::ModeMask(RelocInfo::DEOPT_SCRIPT_OFFSET) |
             RelocInfo::ModeMask(RelocInfo::DEOPT_INLINING_ID);
2459 2460
  for (RelocIterator it(code, mask); !it.done(); it.next()) {
    RelocInfo* info = it.rinfo();
2461
    if (info->pc() >= pc) break;
2462 2463 2464 2465 2466 2467
    if (info->rmode() == RelocInfo::DEOPT_SCRIPT_OFFSET) {
      int script_offset = static_cast<int>(info->data());
      it.next();
      DCHECK(it.rinfo()->rmode() == RelocInfo::DEOPT_INLINING_ID);
      int inlining_id = static_cast<int>(it.rinfo()->data());
      last_position = SourcePosition(script_offset, inlining_id);
2468
    } else if (info->rmode() == RelocInfo::DEOPT_ID) {
2469
      last_deopt_id = static_cast<int>(info->data());
2470
    } else if (info->rmode() == RelocInfo::DEOPT_REASON) {
2471
      last_reason = static_cast<DeoptimizeReason>(info->data());
2472 2473
    }
  }
2474
  return DeoptInfo(last_position, last_reason, last_deopt_id);
2475
}
2476 2477


2478 2479 2480 2481 2482
// static
int Deoptimizer::ComputeSourcePositionFromBytecodeArray(
    SharedFunctionInfo* shared, BailoutId node_id) {
  DCHECK(shared->HasBytecodeArray());
  return AbstractCode::cast(shared->bytecode_array())
2483
      ->SourcePosition(node_id.ToInt());
2484 2485
}

2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504
// 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;
}


2505 2506
// static
TranslatedValue TranslatedValue::NewFloat(TranslatedState* container,
2507
                                          Float32 value) {
2508 2509 2510 2511 2512
  TranslatedValue slot(container, kFloat);
  slot.float_value_ = value;
  return slot;
}

2513 2514
// static
TranslatedValue TranslatedValue::NewDouble(TranslatedState* container,
2515
                                           Float64 value) {
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
  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
2559 2560
TranslatedValue TranslatedValue::NewInvalid(TranslatedState* container) {
  return TranslatedValue(container, kInvalid);
2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583
}


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_;
}

2584
Float32 TranslatedValue::float_value() const {
2585 2586 2587
  DCHECK_EQ(kFloat, kind());
  return float_value_;
}
2588

2589
Float64 TranslatedValue::double_value() const {
2590 2591 2592 2593 2594 2595
  DCHECK_EQ(kDouble, kind());
  return double_value_;
}


int TranslatedValue::object_length() const {
2596
  DCHECK(kind() == kCapturedObject);
2597 2598 2599 2600 2601
  return materialization_info_.length_;
}


int TranslatedValue::object_index() const {
2602
  DCHECK(kind() == kCapturedObject || kind() == kDuplicatedObject);
2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638
  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 {
2639
        CHECK_EQ(1U, uint32_value());
2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664
        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:
2665
    case TranslatedValue::kFloat:
2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696
    case TranslatedValue::kDouble: {
      MaterializeSimple();
      return value_.ToHandleChecked();
    }

    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()) {
2697
    case kInt32:
2698 2699 2700 2701 2702 2703 2704
      value_ = Handle<Object>(isolate()->factory()->NewNumber(int32_value()));
      return;

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

2705 2706 2707
    case kFloat: {
      double scalar_value = float_value().get_scalar();
      value_ = Handle<Object>(isolate()->factory()->NewNumber(scalar_value));
2708
      return;
2709
    }
2710

2711 2712 2713
    case kDouble: {
      double scalar_value = double_value().get_scalar();
      value_ = Handle<Object>(isolate()->factory()->NewNumber(scalar_value));
2714
      return;
2715
    }
2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737

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


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

2738 2739 2740 2741
bool TranslatedValue::IsMaterializableByDebugger() const {
  // At the moment, we only allow materialization of doubles.
  return (kind() == kDouble);
}
2742 2743

int TranslatedValue::GetChildrenCount() const {
2744
  if (kind() == kCapturedObject) {
2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760
    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
}

2761
Float32 TranslatedState::GetFloatSlot(Address fp, int slot_offset) {
2762
#if !V8_TARGET_ARCH_S390X && !V8_TARGET_ARCH_PPC64
2763
  return Float32::FromBits(GetUInt32Slot(fp, slot_offset));
2764 2765 2766
#else
  return Float32::FromBits(Memory::uint32_at(fp + slot_offset));
#endif
2767 2768 2769 2770 2771
}

Float64 TranslatedState::GetDoubleSlot(Address fp, int slot_offset) {
  return Float64::FromBits(Memory::uint64_at(fp + slot_offset));
}
2772 2773 2774 2775 2776 2777 2778 2779 2780

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


2781 2782 2783 2784 2785 2786 2787 2788 2789
TranslatedFrame TranslatedFrame::InterpretedFrame(
    BailoutId bytecode_offset, SharedFunctionInfo* shared_info, int height) {
  TranslatedFrame frame(kInterpretedFunction, shared_info->GetIsolate(),
                        shared_info, height);
  frame.node_id_ = bytecode_offset;
  return frame;
}


2790 2791
TranslatedFrame TranslatedFrame::AccessorFrame(
    Kind kind, SharedFunctionInfo* shared_info) {
2792
  DCHECK(kind == kSetter || kind == kGetter);
2793
  return TranslatedFrame(kind, shared_info->GetIsolate(), shared_info);
2794 2795 2796
}


2797 2798 2799 2800
TranslatedFrame TranslatedFrame::ArgumentsAdaptorFrame(
    SharedFunctionInfo* shared_info, int height) {
  return TranslatedFrame(kArgumentsAdaptor, shared_info->GetIsolate(),
                         shared_info, height);
2801 2802
}

2803
TranslatedFrame TranslatedFrame::ConstructStubFrame(
2804 2805 2806 2807 2808
    BailoutId bailout_id, SharedFunctionInfo* shared_info, int height) {
  TranslatedFrame frame(kConstructStub, shared_info->GetIsolate(), shared_info,
                        height);
  frame.node_id_ = bailout_id;
  return frame;
2809 2810
}

2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825
TranslatedFrame TranslatedFrame::BuiltinContinuationFrame(
    BailoutId bailout_id, SharedFunctionInfo* shared_info, int height) {
  TranslatedFrame frame(kBuiltinContinuation, shared_info->GetIsolate(),
                        shared_info, height);
  frame.node_id_ = bailout_id;
  return frame;
}

TranslatedFrame TranslatedFrame::JavaScriptBuiltinContinuationFrame(
    BailoutId bailout_id, SharedFunctionInfo* shared_info, int height) {
  TranslatedFrame frame(kJavaScriptBuiltinContinuation,
                        shared_info->GetIsolate(), shared_info, height);
  frame.node_id_ = bailout_id;
  return frame;
}
2826 2827 2828

int TranslatedFrame::GetValueCount() {
  switch (kind()) {
2829 2830 2831
    case kInterpretedFunction: {
      int parameter_count =
          raw_shared_info_->internal_formal_parameter_count() + 1;
2832 2833
      // + 2 for function and context.
      return height_ + parameter_count + 2;
2834 2835
    }

2836
    case kGetter:
2837
      return 2;  // Function and receiver.
2838 2839

    case kSetter:
2840
      return 3;  // Function, receiver and the value to set.
2841 2842 2843

    case kArgumentsAdaptor:
    case kConstructStub:
2844 2845
    case kBuiltinContinuation:
    case kJavaScriptBuiltinContinuation:
2846 2847
      return 1 + height_;

2848 2849 2850 2851 2852 2853 2854 2855
    case kInvalid:
      UNREACHABLE();
      break;
  }
  UNREACHABLE();
}


2856 2857 2858 2859
void TranslatedFrame::Handlify() {
  if (raw_shared_info_ != nullptr) {
    shared_info_ = Handle<SharedFunctionInfo>(raw_shared_info_);
    raw_shared_info_ = nullptr;
2860 2861 2862 2863 2864 2865 2866 2867 2868
  }
  for (auto& value : values_) {
    value.Handlify();
  }
}


TranslatedFrame TranslatedState::CreateNextTranslatedFrame(
    TranslationIterator* iterator, FixedArray* literal_array, Address fp,
2869
    FILE* trace_file) {
2870 2871 2872
  Translation::Opcode opcode =
      static_cast<Translation::Opcode>(iterator->Next());
  switch (opcode) {
2873 2874 2875 2876 2877 2878
    case Translation::INTERPRETED_FRAME: {
      BailoutId bytecode_offset = BailoutId(iterator->Next());
      SharedFunctionInfo* shared_info =
          SharedFunctionInfo::cast(literal_array->get(iterator->Next()));
      int height = iterator->Next();
      if (trace_file != nullptr) {
2879
        std::unique_ptr<char[]> name = shared_info->DebugName()->ToCString();
2880 2881 2882 2883 2884 2885 2886 2887 2888 2889
        PrintF(trace_file, "  reading input frame %s", name.get());
        int arg_count = shared_info->internal_formal_parameter_count() + 1;
        PrintF(trace_file,
               " => bytecode_offset=%d, args=%d, height=%d; inputs:\n",
               bytecode_offset.ToInt(), arg_count, height);
      }
      return TranslatedFrame::InterpretedFrame(bytecode_offset, shared_info,
                                               height);
    }

2890
    case Translation::ARGUMENTS_ADAPTOR_FRAME: {
2891 2892
      SharedFunctionInfo* shared_info =
          SharedFunctionInfo::cast(literal_array->get(iterator->Next()));
2893 2894
      int height = iterator->Next();
      if (trace_file != nullptr) {
2895
        std::unique_ptr<char[]> name = shared_info->DebugName()->ToCString();
2896
        PrintF(trace_file, "  reading arguments adaptor frame %s", name.get());
2897 2898
        PrintF(trace_file, " => height=%d; inputs:\n", height);
      }
2899
      return TranslatedFrame::ArgumentsAdaptorFrame(shared_info, height);
2900 2901 2902
    }

    case Translation::CONSTRUCT_STUB_FRAME: {
2903
      BailoutId bailout_id = BailoutId(iterator->Next());
2904 2905
      SharedFunctionInfo* shared_info =
          SharedFunctionInfo::cast(literal_array->get(iterator->Next()));
2906 2907
      int height = iterator->Next();
      if (trace_file != nullptr) {
2908
        std::unique_ptr<char[]> name = shared_info->DebugName()->ToCString();
2909
        PrintF(trace_file, "  reading construct stub frame %s", name.get());
2910 2911
        PrintF(trace_file, " => bailout_id=%d, height=%d; inputs:\n",
               bailout_id.ToInt(), height);
2912
      }
2913 2914
      return TranslatedFrame::ConstructStubFrame(bailout_id, shared_info,
                                                 height);
2915 2916
    }

2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928
    case Translation::BUILTIN_CONTINUATION_FRAME: {
      BailoutId bailout_id = BailoutId(iterator->Next());
      SharedFunctionInfo* shared_info =
          SharedFunctionInfo::cast(literal_array->get(iterator->Next()));
      int height = iterator->Next();
      if (trace_file != nullptr) {
        std::unique_ptr<char[]> name = shared_info->DebugName()->ToCString();
        PrintF(trace_file, "  reading builtin continuation frame %s",
               name.get());
        PrintF(trace_file, " => bailout_id=%d, height=%d; inputs:\n",
               bailout_id.ToInt(), height);
      }
2929 2930 2931
      // Add one to the height to account for the context which was implicitly
      // added to the translation during code generation.
      int height_with_context = height + 1;
2932
      return TranslatedFrame::BuiltinContinuationFrame(bailout_id, shared_info,
2933
                                                       height_with_context);
2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947
    }

    case Translation::JAVA_SCRIPT_BUILTIN_CONTINUATION_FRAME: {
      BailoutId bailout_id = BailoutId(iterator->Next());
      SharedFunctionInfo* shared_info =
          SharedFunctionInfo::cast(literal_array->get(iterator->Next()));
      int height = iterator->Next();
      if (trace_file != nullptr) {
        std::unique_ptr<char[]> name = shared_info->DebugName()->ToCString();
        PrintF(trace_file, "  reading JavaScript builtin continuation frame %s",
               name.get());
        PrintF(trace_file, " => bailout_id=%d, height=%d; inputs:\n",
               bailout_id.ToInt(), height);
      }
2948 2949 2950
      // Add one to the height to account for the context which was implicitly
      // added to the translation during code generation.
      int height_with_context = height + 1;
2951
      return TranslatedFrame::JavaScriptBuiltinContinuationFrame(
2952
          bailout_id, shared_info, height_with_context);
2953 2954
    }

2955
    case Translation::GETTER_STUB_FRAME: {
2956 2957
      SharedFunctionInfo* shared_info =
          SharedFunctionInfo::cast(literal_array->get(iterator->Next()));
2958
      if (trace_file != nullptr) {
2959
        std::unique_ptr<char[]> name = shared_info->DebugName()->ToCString();
2960
        PrintF(trace_file, "  reading getter frame %s; inputs:\n", name.get());
2961
      }
2962 2963
      return TranslatedFrame::AccessorFrame(TranslatedFrame::kGetter,
                                            shared_info);
2964 2965 2966
    }

    case Translation::SETTER_STUB_FRAME: {
2967 2968
      SharedFunctionInfo* shared_info =
          SharedFunctionInfo::cast(literal_array->get(iterator->Next()));
2969
      if (trace_file != nullptr) {
2970
        std::unique_ptr<char[]> name = shared_info->DebugName()->ToCString();
2971
        PrintF(trace_file, "  reading setter frame %s; inputs:\n", name.get());
2972
      }
2973 2974
      return TranslatedFrame::AccessorFrame(TranslatedFrame::kSetter,
                                            shared_info);
2975 2976 2977 2978
    }

    case Translation::BEGIN:
    case Translation::DUPLICATED_OBJECT:
2979
    case Translation::ARGUMENTS_ELEMENTS:
2980
    case Translation::ARGUMENTS_LENGTH:
2981 2982 2983 2984 2985
    case Translation::CAPTURED_OBJECT:
    case Translation::REGISTER:
    case Translation::INT32_REGISTER:
    case Translation::UINT32_REGISTER:
    case Translation::BOOL_REGISTER:
2986
    case Translation::FLOAT_REGISTER:
2987 2988 2989 2990 2991
    case Translation::DOUBLE_REGISTER:
    case Translation::STACK_SLOT:
    case Translation::INT32_STACK_SLOT:
    case Translation::UINT32_STACK_SLOT:
    case Translation::BOOL_STACK_SLOT:
2992
    case Translation::FLOAT_STACK_SLOT:
2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015
    case Translation::DOUBLE_STACK_SLOT:
    case Translation::LITERAL:
      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)++;
  }
}

3016 3017
Address TranslatedState::ComputeArgumentsPosition(Address input_frame_pointer,
                                                  bool is_rest, int* length) {
3018 3019 3020 3021
  Address parent_frame_pointer = *reinterpret_cast<Address*>(
      input_frame_pointer + StandardFrameConstants::kCallerFPOffset);
  intptr_t parent_frame_type = Memory::intptr_at(
      parent_frame_pointer + CommonFrameConstants::kContextOrFrameTypeOffset);
3022

3023 3024 3025
  Address arguments_frame;
  if (parent_frame_type ==
      StackFrame::TypeToMarker(StackFrame::ARGUMENTS_ADAPTOR)) {
3026 3027 3028 3029 3030
    if (length)
      *length = Smi::cast(*reinterpret_cast<Object**>(
                              parent_frame_pointer +
                              ArgumentsAdaptorFrameConstants::kLengthOffset))
                    ->value();
3031 3032
    arguments_frame = parent_frame_pointer;
  } else {
3033
    if (length) *length = formal_parameter_count_;
3034 3035 3036 3037 3038 3039
    arguments_frame = input_frame_pointer;
  }

  if (is_rest) {
    // If the actual number of arguments is less than the number of formal
    // parameters, we have zero rest parameters.
3040
    if (length) *length = std::max(0, *length - formal_parameter_count_);
3041 3042
  }

3043 3044 3045 3046 3047 3048 3049 3050
  return arguments_frame;
}

// Creates translated values for an arguments backing store, or the backing
// store for the rest parameters if {is_rest} is true. The TranslatedValue
// objects for the fields are not read from the TranslationIterator, but instead
// created on-the-fly based on dynamic information in the optimized frame.
void TranslatedState::CreateArgumentsElementsTranslatedValues(
3051 3052
    int frame_index, Address input_frame_pointer, bool is_rest,
    FILE* trace_file) {
3053 3054 3055 3056 3057 3058
  TranslatedFrame& frame = frames_[frame_index];

  int length;
  Address arguments_frame =
      ComputeArgumentsPosition(input_frame_pointer, is_rest, &length);

3059 3060
  int object_index = static_cast<int>(object_positions_.size());
  int value_index = static_cast<int>(frame.values_.size());
3061 3062 3063 3064 3065
  if (trace_file != nullptr) {
    PrintF(trace_file,
           "arguments elements object #%d (is_rest = %d, length = %d)",
           object_index, is_rest, length);
  }
3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081
  object_positions_.push_back({frame_index, value_index});
  frame.Add(TranslatedValue::NewDeferredObject(
      this, length + FixedArray::kHeaderSize / kPointerSize, object_index));

  frame.Add(
      TranslatedValue::NewTagged(this, isolate_->heap()->fixed_array_map()));
  frame.Add(TranslatedValue::NewInt32(this, length));

  for (int i = length - 1; i >= 0; --i) {
    Address argument_slot = arguments_frame +
                            CommonFrameConstants::kFixedFrameSizeAboveFp +
                            i * kPointerSize;
    frame.Add(TranslatedValue::NewTagged(
        this, *reinterpret_cast<Object**>(argument_slot)));
  }
}
3082

3083 3084
// We can't intermix stack decoding and allocations because the deoptimization
// infrastracture is not GC safe.
3085
// Thus we build a temporary structure in malloced space.
3086 3087 3088 3089 3090 3091 3092 3093 3094
// The TranslatedValue objects created correspond to the static translation
// instructions from the TranslationIterator, except for
// Translation::ARGUMENTS_ELEMENTS, where the number and values of the
// FixedArray elements depend on dynamic information from the optimized frame.
// Returns the number of expected nested translations from the
// TranslationIterator.
int TranslatedState::CreateNextTranslatedValue(
    int frame_index, TranslationIterator* iterator, FixedArray* literal_array,
    Address fp, RegisterValues* registers, FILE* trace_file) {
3095 3096
  disasm::NameConverter converter;

3097 3098 3099
  TranslatedFrame& frame = frames_[frame_index];
  int value_index = static_cast<int>(frame.values_.size());

3100 3101 3102 3103
  Translation::Opcode opcode =
      static_cast<Translation::Opcode>(iterator->Next());
  switch (opcode) {
    case Translation::BEGIN:
3104
    case Translation::INTERPRETED_FRAME:
3105 3106 3107 3108
    case Translation::ARGUMENTS_ADAPTOR_FRAME:
    case Translation::CONSTRUCT_STUB_FRAME:
    case Translation::GETTER_STUB_FRAME:
    case Translation::SETTER_STUB_FRAME:
3109 3110
    case Translation::JAVA_SCRIPT_BUILTIN_CONTINUATION_FRAME:
    case Translation::BUILTIN_CONTINUATION_FRAME:
3111 3112 3113 3114 3115 3116 3117 3118 3119
      // 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]);
3120 3121 3122 3123
      TranslatedValue translated_value =
          TranslatedValue::NewDuplicateObject(this, object_id);
      frame.Add(translated_value);
      return translated_value.GetChildrenCount();
3124 3125
    }

3126 3127
    case Translation::ARGUMENTS_ELEMENTS: {
      bool is_rest = iterator->Next();
3128 3129
      CreateArgumentsElementsTranslatedValues(frame_index, fp, is_rest,
                                              trace_file);
3130
      return 0;
3131 3132
    }

3133 3134 3135 3136
    case Translation::ARGUMENTS_LENGTH: {
      bool is_rest = iterator->Next();
      int length;
      ComputeArgumentsPosition(fp, is_rest, &length);
3137 3138 3139 3140
      if (trace_file != nullptr) {
        PrintF(trace_file, "arguments length field (is_rest = %d, length = %d)",
               is_rest, length);
      }
3141 3142 3143 3144
      frame.Add(TranslatedValue::NewInt32(this, length));
      return 0;
    }

3145 3146 3147 3148 3149 3150 3151 3152
    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});
3153 3154 3155 3156
      TranslatedValue translated_value =
          TranslatedValue::NewDeferredObject(this, field_count, object_index);
      frame.Add(translated_value);
      return translated_value.GetChildrenCount();
3157 3158 3159 3160
    }

    case Translation::REGISTER: {
      int input_reg = iterator->Next();
3161 3162 3163 3164 3165
      if (registers == nullptr) {
        TranslatedValue translated_value = TranslatedValue::NewInvalid(this);
        frame.Add(translated_value);
        return translated_value.GetChildrenCount();
      }
3166 3167 3168 3169 3170 3171
      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);
      }
3172 3173 3174 3175
      TranslatedValue translated_value =
          TranslatedValue::NewTagged(this, reinterpret_cast<Object*>(value));
      frame.Add(translated_value);
      return translated_value.GetChildrenCount();
3176 3177 3178 3179
    }

    case Translation::INT32_REGISTER: {
      int input_reg = iterator->Next();
3180 3181 3182 3183 3184
      if (registers == nullptr) {
        TranslatedValue translated_value = TranslatedValue::NewInvalid(this);
        frame.Add(translated_value);
        return translated_value.GetChildrenCount();
      }
3185 3186 3187 3188 3189
      intptr_t value = registers->GetRegister(input_reg);
      if (trace_file != nullptr) {
        PrintF(trace_file, "%" V8PRIdPTR " ; %s ", value,
               converter.NameOfCPURegister(input_reg));
      }
3190 3191 3192 3193
      TranslatedValue translated_value =
          TranslatedValue::NewInt32(this, static_cast<int32_t>(value));
      frame.Add(translated_value);
      return translated_value.GetChildrenCount();
3194 3195 3196 3197
    }

    case Translation::UINT32_REGISTER: {
      int input_reg = iterator->Next();
3198 3199 3200 3201 3202
      if (registers == nullptr) {
        TranslatedValue translated_value = TranslatedValue::NewInvalid(this);
        frame.Add(translated_value);
        return translated_value.GetChildrenCount();
      }
3203 3204 3205 3206 3207
      intptr_t value = registers->GetRegister(input_reg);
      if (trace_file != nullptr) {
        PrintF(trace_file, "%" V8PRIuPTR " ; %s (uint)", value,
               converter.NameOfCPURegister(input_reg));
      }
3208 3209 3210 3211
      TranslatedValue translated_value =
          TranslatedValue::NewUInt32(this, static_cast<uint32_t>(value));
      frame.Add(translated_value);
      return translated_value.GetChildrenCount();
3212 3213 3214 3215
    }

    case Translation::BOOL_REGISTER: {
      int input_reg = iterator->Next();
3216 3217 3218 3219 3220
      if (registers == nullptr) {
        TranslatedValue translated_value = TranslatedValue::NewInvalid(this);
        frame.Add(translated_value);
        return translated_value.GetChildrenCount();
      }
3221 3222 3223 3224 3225
      intptr_t value = registers->GetRegister(input_reg);
      if (trace_file != nullptr) {
        PrintF(trace_file, "%" V8PRIdPTR " ; %s (bool)", value,
               converter.NameOfCPURegister(input_reg));
      }
3226 3227 3228 3229
      TranslatedValue translated_value =
          TranslatedValue::NewBool(this, static_cast<uint32_t>(value));
      frame.Add(translated_value);
      return translated_value.GetChildrenCount();
3230 3231
    }

3232 3233
    case Translation::FLOAT_REGISTER: {
      int input_reg = iterator->Next();
3234 3235 3236 3237 3238
      if (registers == nullptr) {
        TranslatedValue translated_value = TranslatedValue::NewInvalid(this);
        frame.Add(translated_value);
        return translated_value.GetChildrenCount();
      }
3239
      Float32 value = registers->GetFloatRegister(input_reg);
3240
      if (trace_file != nullptr) {
3241 3242 3243
        PrintF(
            trace_file, "%e ; %s (float)", value.get_scalar(),
            RegisterConfiguration::Default()->GetFloatRegisterName(input_reg));
3244
      }
3245 3246 3247
      TranslatedValue translated_value = TranslatedValue::NewFloat(this, value);
      frame.Add(translated_value);
      return translated_value.GetChildrenCount();
3248 3249
    }

3250 3251
    case Translation::DOUBLE_REGISTER: {
      int input_reg = iterator->Next();
3252 3253 3254 3255 3256
      if (registers == nullptr) {
        TranslatedValue translated_value = TranslatedValue::NewInvalid(this);
        frame.Add(translated_value);
        return translated_value.GetChildrenCount();
      }
3257
      Float64 value = registers->GetDoubleRegister(input_reg);
3258
      if (trace_file != nullptr) {
3259 3260 3261
        PrintF(
            trace_file, "%e ; %s (double)", value.get_scalar(),
            RegisterConfiguration::Default()->GetDoubleRegisterName(input_reg));
3262
      }
3263 3264 3265 3266
      TranslatedValue translated_value =
          TranslatedValue::NewDouble(this, value);
      frame.Add(translated_value);
      return translated_value.GetChildrenCount();
3267 3268 3269
    }

    case Translation::STACK_SLOT: {
3270 3271
      int slot_offset =
          OptimizedFrame::StackSlotOffsetRelativeToFp(iterator->Next());
3272 3273 3274 3275 3276 3277
      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);
      }
3278 3279 3280 3281
      TranslatedValue translated_value =
          TranslatedValue::NewTagged(this, reinterpret_cast<Object*>(value));
      frame.Add(translated_value);
      return translated_value.GetChildrenCount();
3282 3283 3284
    }

    case Translation::INT32_STACK_SLOT: {
3285 3286
      int slot_offset =
          OptimizedFrame::StackSlotOffsetRelativeToFp(iterator->Next());
3287 3288 3289 3290 3291 3292
      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));
      }
3293 3294 3295
      TranslatedValue translated_value = TranslatedValue::NewInt32(this, value);
      frame.Add(translated_value);
      return translated_value.GetChildrenCount();
3296 3297 3298
    }

    case Translation::UINT32_STACK_SLOT: {
3299 3300
      int slot_offset =
          OptimizedFrame::StackSlotOffsetRelativeToFp(iterator->Next());
3301 3302 3303 3304 3305
      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));
      }
3306 3307 3308 3309
      TranslatedValue translated_value =
          TranslatedValue::NewUInt32(this, value);
      frame.Add(translated_value);
      return translated_value.GetChildrenCount();
3310 3311 3312
    }

    case Translation::BOOL_STACK_SLOT: {
3313 3314
      int slot_offset =
          OptimizedFrame::StackSlotOffsetRelativeToFp(iterator->Next());
3315 3316 3317 3318 3319
      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));
      }
3320 3321 3322
      TranslatedValue translated_value = TranslatedValue::NewBool(this, value);
      frame.Add(translated_value);
      return translated_value.GetChildrenCount();
3323 3324
    }

3325 3326 3327
    case Translation::FLOAT_STACK_SLOT: {
      int slot_offset =
          OptimizedFrame::StackSlotOffsetRelativeToFp(iterator->Next());
3328
      Float32 value = GetFloatSlot(fp, slot_offset);
3329
      if (trace_file != nullptr) {
3330
        PrintF(trace_file, "%e ; (float) [fp %c %d] ", value.get_scalar(),
3331 3332
               slot_offset < 0 ? '-' : '+', std::abs(slot_offset));
      }
3333 3334 3335
      TranslatedValue translated_value = TranslatedValue::NewFloat(this, value);
      frame.Add(translated_value);
      return translated_value.GetChildrenCount();
3336 3337
    }

3338
    case Translation::DOUBLE_STACK_SLOT: {
3339 3340
      int slot_offset =
          OptimizedFrame::StackSlotOffsetRelativeToFp(iterator->Next());
3341
      Float64 value = GetDoubleSlot(fp, slot_offset);
3342
      if (trace_file != nullptr) {
3343
        PrintF(trace_file, "%e ; (double) [fp %c %d] ", value.get_scalar(),
3344 3345
               slot_offset < 0 ? '-' : '+', std::abs(slot_offset));
      }
3346 3347 3348 3349
      TranslatedValue translated_value =
          TranslatedValue::NewDouble(this, value);
      frame.Add(translated_value);
      return translated_value.GetChildrenCount();
3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360
    }

    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);
      }

3361 3362 3363 3364
      TranslatedValue translated_value =
          TranslatedValue::NewTagged(this, value);
      frame.Add(translated_value);
      return translated_value.GetChildrenCount();
3365 3366 3367 3368
    }
  }

  FATAL("We should never get here - unexpected deopt info.");
3369 3370 3371 3372
  TranslatedValue translated_value =
      TranslatedValue(nullptr, TranslatedValue::kInvalid);
  frame.Add(translated_value);
  return translated_value.GetChildrenCount();
3373 3374
}

3375
TranslatedState::TranslatedState(const JavaScriptFrame* frame)
3376
    : isolate_(nullptr), stack_frame_pointer_(nullptr) {
3377 3378
  int deopt_index = Safepoint::kNoDeoptimizationIndex;
  DeoptimizationInputData* data =
3379 3380
      static_cast<const OptimizedFrame*>(frame)->GetDeoptimizationData(
          &deopt_index);
3381
  DCHECK(data != nullptr && deopt_index != Safepoint::kNoDeoptimizationIndex);
3382 3383
  TranslationIterator it(data->TranslationByteArray(),
                         data->TranslationIndex(deopt_index)->value());
3384
  Init(frame->fp(), &it, data->LiteralArray(), nullptr /* registers */,
3385 3386
       nullptr /* trace file */,
       frame->function()->shared()->internal_formal_parameter_count());
3387 3388 3389
}

TranslatedState::TranslatedState()
3390
    : isolate_(nullptr), stack_frame_pointer_(nullptr) {}
3391 3392 3393 3394

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

3398 3399
  formal_parameter_count_ = formal_parameter_count;

3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413
  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
3414
  for (int frame_index = 0; frame_index < count; frame_index++) {
3415
    // Read the frame descriptor.
3416 3417
    frames_.push_back(CreateNextTranslatedFrame(
        iterator, literal_array, input_frame_pointer, trace_file));
3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436
    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, "  ");
          }
        }
      }

3437 3438 3439
      int nested_count =
          CreateNextTranslatedValue(frame_index, iterator, literal_array,
                                    input_frame_pointer, registers, trace_file);
3440 3441 3442 3443 3444 3445 3446

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

      // Update the value count and resolve the nesting.
      values_to_process--;
3447
      if (nested_count > 0) {
3448
        nested_counts.push(values_to_process);
3449
        values_to_process = nested_count;
3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463
      } 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);
}

3464
void TranslatedState::Prepare(Address stack_frame_pointer) {
3465
  for (auto& frame : frames_) frame.Handlify();
3466 3467 3468 3469 3470 3471

  stack_frame_pointer_ = stack_frame_pointer;

  UpdateFromPreviouslyMaterializedObjects();
}

3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534
class TranslatedState::CapturedObjectMaterializer {
 public:
  CapturedObjectMaterializer(TranslatedState* state, int frame_index,
                             int field_count)
      : state_(state), frame_index_(frame_index), field_count_(field_count) {}

  Handle<Object> FieldAt(int* value_index) {
    CHECK(field_count_ > 0);
    --field_count_;
    return state_->MaterializeAt(frame_index_, value_index);
  }

  ~CapturedObjectMaterializer() { CHECK_EQ(0, field_count_); }

 private:
  TranslatedState* state_;
  int frame_index_;
  int field_count_;
};

Handle<Object> TranslatedState::MaterializeCapturedObjectAt(
    TranslatedValue* slot, int frame_index, int* value_index) {
  int length = slot->GetChildrenCount();

  CapturedObjectMaterializer materializer(this, frame_index, length);

  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++) {
      materializer.FieldAt(value_index);
    }

    return result;
  }

  Handle<Object> map_object = materializer.FieldAt(value_index);
  Handle<Map> map = Map::GeneralizeAllFields(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 = materializer.FieldAt(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++) {
        materializer.FieldAt(value_index);
      }
      return object;
    }
    case JS_OBJECT_TYPE:
    case JS_ERROR_TYPE:
    case JS_ARGUMENTS_TYPE: {
      Handle<JSObject> object =
          isolate_->factory()->NewJSObjectFromMap(map, NOT_TENURED);
      slot->value_ = object;
      Handle<Object> properties = materializer.FieldAt(value_index);
      Handle<Object> elements = materializer.FieldAt(value_index);
3535
      object->set_raw_properties_or_hash(*properties);
3536
      object->set_elements(FixedArrayBase::cast(*elements));
3537 3538
      int in_object_properties = map->GetInObjectProperties();
      for (int i = 0; i < in_object_properties; ++i) {
3539 3540 3541 3542 3543 3544
        Handle<Object> value = materializer.FieldAt(value_index);
        FieldIndex index = FieldIndex::ForPropertyIndex(object->map(), i);
        object->FastPropertyAtPut(index, *value);
      }
      return object;
    }
3545 3546 3547 3548 3549 3550 3551 3552
    case JS_SET_KEY_VALUE_ITERATOR_TYPE:
    case JS_SET_VALUE_ITERATOR_TYPE: {
      Handle<JSSetIterator> object = Handle<JSSetIterator>::cast(
          isolate_->factory()->NewJSObjectFromMap(map, NOT_TENURED));
      Handle<Object> properties = materializer.FieldAt(value_index);
      Handle<Object> elements = materializer.FieldAt(value_index);
      Handle<Object> table = materializer.FieldAt(value_index);
      Handle<Object> index = materializer.FieldAt(value_index);
3553
      object->set_raw_properties_or_hash(FixedArray::cast(*properties));
3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567
      object->set_elements(FixedArrayBase::cast(*elements));
      object->set_table(*table);
      object->set_index(*index);
      return object;
    }
    case JS_MAP_KEY_ITERATOR_TYPE:
    case JS_MAP_KEY_VALUE_ITERATOR_TYPE:
    case JS_MAP_VALUE_ITERATOR_TYPE: {
      Handle<JSMapIterator> object = Handle<JSMapIterator>::cast(
          isolate_->factory()->NewJSObjectFromMap(map, NOT_TENURED));
      Handle<Object> properties = materializer.FieldAt(value_index);
      Handle<Object> elements = materializer.FieldAt(value_index);
      Handle<Object> table = materializer.FieldAt(value_index);
      Handle<Object> index = materializer.FieldAt(value_index);
3568
      object->set_raw_properties_or_hash(FixedArray::cast(*properties));
3569 3570 3571 3572 3573
      object->set_elements(FixedArrayBase::cast(*elements));
      object->set_table(*table);
      object->set_index(*index);
      return object;
    }
3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611
    case JS_TYPED_ARRAY_KEY_ITERATOR_TYPE:
    case JS_FAST_ARRAY_KEY_ITERATOR_TYPE:
    case JS_GENERIC_ARRAY_KEY_ITERATOR_TYPE:
    case JS_UINT8_ARRAY_KEY_VALUE_ITERATOR_TYPE:
    case JS_INT8_ARRAY_KEY_VALUE_ITERATOR_TYPE:
    case JS_UINT16_ARRAY_KEY_VALUE_ITERATOR_TYPE:
    case JS_INT16_ARRAY_KEY_VALUE_ITERATOR_TYPE:
    case JS_UINT32_ARRAY_KEY_VALUE_ITERATOR_TYPE:
    case JS_INT32_ARRAY_KEY_VALUE_ITERATOR_TYPE:
    case JS_FLOAT32_ARRAY_KEY_VALUE_ITERATOR_TYPE:
    case JS_FLOAT64_ARRAY_KEY_VALUE_ITERATOR_TYPE:
    case JS_UINT8_CLAMPED_ARRAY_KEY_VALUE_ITERATOR_TYPE:
    case JS_FAST_SMI_ARRAY_KEY_VALUE_ITERATOR_TYPE:
    case JS_FAST_HOLEY_SMI_ARRAY_KEY_VALUE_ITERATOR_TYPE:
    case JS_FAST_ARRAY_KEY_VALUE_ITERATOR_TYPE:
    case JS_FAST_HOLEY_ARRAY_KEY_VALUE_ITERATOR_TYPE:
    case JS_FAST_DOUBLE_ARRAY_KEY_VALUE_ITERATOR_TYPE:
    case JS_FAST_HOLEY_DOUBLE_ARRAY_KEY_VALUE_ITERATOR_TYPE:
    case JS_GENERIC_ARRAY_KEY_VALUE_ITERATOR_TYPE:
    case JS_UINT8_ARRAY_VALUE_ITERATOR_TYPE:
    case JS_INT8_ARRAY_VALUE_ITERATOR_TYPE:
    case JS_UINT16_ARRAY_VALUE_ITERATOR_TYPE:
    case JS_INT16_ARRAY_VALUE_ITERATOR_TYPE:
    case JS_UINT32_ARRAY_VALUE_ITERATOR_TYPE:
    case JS_INT32_ARRAY_VALUE_ITERATOR_TYPE:
    case JS_FLOAT32_ARRAY_VALUE_ITERATOR_TYPE:
    case JS_FLOAT64_ARRAY_VALUE_ITERATOR_TYPE:
    case JS_UINT8_CLAMPED_ARRAY_VALUE_ITERATOR_TYPE:
    case JS_FAST_SMI_ARRAY_VALUE_ITERATOR_TYPE:
    case JS_FAST_HOLEY_SMI_ARRAY_VALUE_ITERATOR_TYPE:
    case JS_FAST_ARRAY_VALUE_ITERATOR_TYPE:
    case JS_FAST_HOLEY_ARRAY_VALUE_ITERATOR_TYPE:
    case JS_FAST_DOUBLE_ARRAY_VALUE_ITERATOR_TYPE:
    case JS_FAST_HOLEY_DOUBLE_ARRAY_VALUE_ITERATOR_TYPE:
    case JS_GENERIC_ARRAY_VALUE_ITERATOR_TYPE: {
      Handle<JSArrayIterator> object = Handle<JSArrayIterator>::cast(
          isolate_->factory()->NewJSObjectFromMap(map, NOT_TENURED));
      slot->value_ = object;
3612 3613
      // Initialize the index to zero to make the heap verifier happy.
      object->set_index(Smi::FromInt(0));
3614 3615 3616 3617 3618
      Handle<Object> properties = materializer.FieldAt(value_index);
      Handle<Object> elements = materializer.FieldAt(value_index);
      Handle<Object> iterated_object = materializer.FieldAt(value_index);
      Handle<Object> next_index = materializer.FieldAt(value_index);
      Handle<Object> iterated_object_map = materializer.FieldAt(value_index);
3619
      object->set_raw_properties_or_hash(*properties);
3620 3621 3622 3623 3624 3625
      object->set_elements(FixedArrayBase::cast(*elements));
      object->set_object(*iterated_object);
      object->set_index(*next_index);
      object->set_object_map(*iterated_object_map);
      return object;
    }
3626 3627 3628 3629 3630 3631 3632 3633 3634 3635
    case JS_STRING_ITERATOR_TYPE: {
      Handle<JSStringIterator> object = Handle<JSStringIterator>::cast(
          isolate_->factory()->NewJSObjectFromMap(map, NOT_TENURED));
      slot->value_ = object;
      // Initialize the index to zero to make the heap verifier happy.
      object->set_index(0);
      Handle<Object> properties = materializer.FieldAt(value_index);
      Handle<Object> elements = materializer.FieldAt(value_index);
      Handle<Object> iterated_string = materializer.FieldAt(value_index);
      Handle<Object> next_index = materializer.FieldAt(value_index);
3636
      object->set_raw_properties_or_hash(*properties);
3637 3638 3639 3640
      object->set_elements(FixedArrayBase::cast(*elements));
      CHECK(iterated_string->IsString());
      object->set_string(String::cast(*iterated_string));
      CHECK(next_index->IsSmi());
jgruber's avatar
jgruber committed
3641
      object->set_index(Smi::ToInt(*next_index));
3642 3643
      return object;
    }
3644 3645 3646 3647 3648 3649 3650 3651
    case JS_ASYNC_FROM_SYNC_ITERATOR_TYPE: {
      Handle<JSAsyncFromSyncIterator> object =
          Handle<JSAsyncFromSyncIterator>::cast(
              isolate_->factory()->NewJSObjectFromMap(map, NOT_TENURED));
      slot->value_ = object;
      Handle<Object> properties = materializer.FieldAt(value_index);
      Handle<Object> elements = materializer.FieldAt(value_index);
      Handle<Object> sync_iterator = materializer.FieldAt(value_index);
3652
      object->set_raw_properties_or_hash(*properties);
3653 3654 3655 3656
      object->set_elements(FixedArrayBase::cast(*elements));
      object->set_sync_iterator(JSReceiver::cast(*sync_iterator));
      return object;
    }
3657 3658 3659 3660 3661 3662
    case JS_ARRAY_TYPE: {
      Handle<JSArray> object = Handle<JSArray>::cast(
          isolate_->factory()->NewJSObjectFromMap(map, NOT_TENURED));
      slot->value_ = object;
      Handle<Object> properties = materializer.FieldAt(value_index);
      Handle<Object> elements = materializer.FieldAt(value_index);
3663
      Handle<Object> array_length = materializer.FieldAt(value_index);
3664
      object->set_raw_properties_or_hash(*properties);
3665
      object->set_elements(FixedArrayBase::cast(*elements));
3666
      object->set_length(*array_length);
3667 3668
      return object;
    }
3669 3670 3671 3672 3673 3674 3675 3676 3677
    case JS_BOUND_FUNCTION_TYPE: {
      Handle<JSBoundFunction> object = Handle<JSBoundFunction>::cast(
          isolate_->factory()->NewJSObjectFromMap(map, NOT_TENURED));
      slot->value_ = object;
      Handle<Object> properties = materializer.FieldAt(value_index);
      Handle<Object> elements = materializer.FieldAt(value_index);
      Handle<Object> bound_target_function = materializer.FieldAt(value_index);
      Handle<Object> bound_this = materializer.FieldAt(value_index);
      Handle<Object> bound_arguments = materializer.FieldAt(value_index);
3678
      object->set_raw_properties_or_hash(*properties);
3679 3680 3681 3682 3683 3684 3685
      object->set_elements(FixedArrayBase::cast(*elements));
      object->set_bound_target_function(
          JSReceiver::cast(*bound_target_function));
      object->set_bound_this(*bound_this);
      object->set_bound_arguments(FixedArray::cast(*bound_arguments));
      return object;
    }
3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698
    case JS_GENERATOR_OBJECT_TYPE: {
      Handle<JSGeneratorObject> object = Handle<JSGeneratorObject>::cast(
          isolate_->factory()->NewJSObjectFromMap(map, NOT_TENURED));
      slot->value_ = object;
      Handle<Object> properties = materializer.FieldAt(value_index);
      Handle<Object> elements = materializer.FieldAt(value_index);
      Handle<Object> function = materializer.FieldAt(value_index);
      Handle<Object> context = materializer.FieldAt(value_index);
      Handle<Object> receiver = materializer.FieldAt(value_index);
      Handle<Object> input_or_debug_pos = materializer.FieldAt(value_index);
      Handle<Object> resume_mode = materializer.FieldAt(value_index);
      Handle<Object> continuation_offset = materializer.FieldAt(value_index);
      Handle<Object> register_file = materializer.FieldAt(value_index);
3699
      object->set_raw_properties_or_hash(*properties);
3700 3701 3702 3703 3704
      object->set_elements(FixedArrayBase::cast(*elements));
      object->set_function(JSFunction::cast(*function));
      object->set_context(Context::cast(*context));
      object->set_receiver(*receiver);
      object->set_input_or_debug_pos(*input_or_debug_pos);
jgruber's avatar
jgruber committed
3705 3706
      object->set_resume_mode(Smi::ToInt(*resume_mode));
      object->set_continuation(Smi::ToInt(*continuation_offset));
3707
      object->set_register_file(FixedArray::cast(*register_file));
3708 3709 3710 3711 3712 3713
      int in_object_properties = map->GetInObjectProperties();
      for (int i = 0; i < in_object_properties; ++i) {
        Handle<Object> value = materializer.FieldAt(value_index);
        FieldIndex index = FieldIndex::ForPropertyIndex(object->map(), i);
        object->FastPropertyAtPut(index, *value);
      }
3714 3715
      return object;
    }
3716 3717 3718 3719 3720 3721 3722 3723
    case CONS_STRING_TYPE: {
      Handle<ConsString> object = Handle<ConsString>::cast(
          isolate_->factory()
              ->NewConsString(isolate_->factory()->undefined_string(),
                              isolate_->factory()->undefined_string())
              .ToHandleChecked());
      slot->value_ = object;
      Handle<Object> hash = materializer.FieldAt(value_index);
3724
      Handle<Object> string_length = materializer.FieldAt(value_index);
3725 3726 3727
      Handle<Object> first = materializer.FieldAt(value_index);
      Handle<Object> second = materializer.FieldAt(value_index);
      object->set_map(*map);
jgruber's avatar
jgruber committed
3728
      object->set_length(Smi::ToInt(*string_length));
3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745
      object->set_first(String::cast(*first));
      object->set_second(String::cast(*second));
      CHECK(hash->IsNumber());  // The {Name::kEmptyHashField} value.
      return object;
    }
    case CONTEXT_EXTENSION_TYPE: {
      Handle<ContextExtension> object =
          isolate_->factory()->NewContextExtension(
              isolate_->factory()->NewScopeInfo(1),
              isolate_->factory()->undefined_value());
      slot->value_ = object;
      Handle<Object> scope_info = materializer.FieldAt(value_index);
      Handle<Object> extension = materializer.FieldAt(value_index);
      object->set_scope_info(ScopeInfo::cast(*scope_info));
      object->set_extension(*extension);
      return object;
    }
3746
    case HASH_TABLE_TYPE:
3747 3748
    case FIXED_ARRAY_TYPE: {
      Handle<Object> lengthObject = materializer.FieldAt(value_index);
3749 3750 3751 3752
      int32_t array_length = 0;
      CHECK(lengthObject->ToInt32(&array_length));
      Handle<FixedArray> object =
          isolate_->factory()->NewFixedArray(array_length);
3753 3754 3755 3756 3757
      // We need to set the map, because the fixed array we are
      // materializing could be a context or an arguments object,
      // in which case we must retain that information.
      object->set_map(*map);
      slot->value_ = object;
3758
      for (int i = 0; i < array_length; ++i) {
3759 3760 3761 3762 3763
        Handle<Object> value = materializer.FieldAt(value_index);
        object->set(i, *value);
      }
      return object;
    }
3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777
    case PROPERTY_ARRAY_TYPE: {
      DCHECK_EQ(*map, isolate_->heap()->property_array_map());
      Handle<Object> lengthObject = materializer.FieldAt(value_index);
      int32_t array_length = 0;
      CHECK(lengthObject->ToInt32(&array_length));
      Handle<PropertyArray> object =
          isolate_->factory()->NewPropertyArray(array_length);
      slot->value_ = object;
      for (int i = 0; i < array_length; ++i) {
        Handle<Object> value = materializer.FieldAt(value_index);
        object->set(i, *value);
      }
      return object;
    }
3778 3779 3780
    case FIXED_DOUBLE_ARRAY_TYPE: {
      DCHECK_EQ(*map, isolate_->heap()->fixed_double_array_map());
      Handle<Object> lengthObject = materializer.FieldAt(value_index);
3781 3782
      int32_t array_length = 0;
      CHECK(lengthObject->ToInt32(&array_length));
3783
      Handle<FixedArrayBase> object =
3784
          isolate_->factory()->NewFixedDoubleArray(array_length);
3785
      slot->value_ = object;
3786
      if (array_length > 0) {
3787 3788
        Handle<FixedDoubleArray> double_array =
            Handle<FixedDoubleArray>::cast(object);
3789
        for (int i = 0; i < array_length; ++i) {
3790
          Handle<Object> value = materializer.FieldAt(value_index);
3791
          if (value.is_identical_to(isolate_->factory()->the_hole_value())) {
3792 3793
            double_array->set_the_hole(isolate_, i);
          } else {
3794
            CHECK(value->IsNumber());
3795 3796
            double_array->set(i, value->Number());
          }
3797 3798 3799 3800
        }
      }
      return object;
    }
3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818
    case JS_REGEXP_TYPE: {
      Handle<JSRegExp> object = Handle<JSRegExp>::cast(
          isolate_->factory()->NewJSObjectFromMap(map, NOT_TENURED));
      slot->value_ = object;
      Handle<Object> properties = materializer.FieldAt(value_index);
      Handle<Object> elements = materializer.FieldAt(value_index);
      Handle<Object> data = materializer.FieldAt(value_index);
      Handle<Object> source = materializer.FieldAt(value_index);
      Handle<Object> flags = materializer.FieldAt(value_index);
      Handle<Object> last_index = materializer.FieldAt(value_index);
      object->set_raw_properties_or_hash(*properties);
      object->set_elements(FixedArrayBase::cast(*elements));
      object->set_data(*data);
      object->set_source(*source);
      object->set_flags(*flags);
      object->set_last_index(*last_index);
      return object;
    }
3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829
    case STRING_TYPE:
    case ONE_BYTE_STRING_TYPE:
    case CONS_ONE_BYTE_STRING_TYPE:
    case SLICED_STRING_TYPE:
    case SLICED_ONE_BYTE_STRING_TYPE:
    case EXTERNAL_STRING_TYPE:
    case EXTERNAL_ONE_BYTE_STRING_TYPE:
    case EXTERNAL_STRING_WITH_ONE_BYTE_DATA_TYPE:
    case SHORT_EXTERNAL_STRING_TYPE:
    case SHORT_EXTERNAL_ONE_BYTE_STRING_TYPE:
    case SHORT_EXTERNAL_STRING_WITH_ONE_BYTE_DATA_TYPE:
3830 3831
    case THIN_STRING_TYPE:
    case THIN_ONE_BYTE_STRING_TYPE:
3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846
    case INTERNALIZED_STRING_TYPE:
    case ONE_BYTE_INTERNALIZED_STRING_TYPE:
    case EXTERNAL_INTERNALIZED_STRING_TYPE:
    case EXTERNAL_ONE_BYTE_INTERNALIZED_STRING_TYPE:
    case EXTERNAL_INTERNALIZED_STRING_WITH_ONE_BYTE_DATA_TYPE:
    case SHORT_EXTERNAL_INTERNALIZED_STRING_TYPE:
    case SHORT_EXTERNAL_ONE_BYTE_INTERNALIZED_STRING_TYPE:
    case SHORT_EXTERNAL_INTERNALIZED_STRING_WITH_ONE_BYTE_DATA_TYPE:
    case SYMBOL_TYPE:
    case ODDBALL_TYPE:
    case JS_GLOBAL_OBJECT_TYPE:
    case JS_GLOBAL_PROXY_TYPE:
    case JS_API_OBJECT_TYPE:
    case JS_SPECIAL_API_OBJECT_TYPE:
    case JS_VALUE_TYPE:
3847
    case JS_FUNCTION_TYPE:
3848 3849 3850
    case JS_MESSAGE_OBJECT_TYPE:
    case JS_DATE_TYPE:
    case JS_CONTEXT_EXTENSION_OBJECT_TYPE:
3851
    case JS_ASYNC_GENERATOR_OBJECT_TYPE:
3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871
    case JS_MODULE_NAMESPACE_TYPE:
    case JS_ARRAY_BUFFER_TYPE:
    case JS_TYPED_ARRAY_TYPE:
    case JS_DATA_VIEW_TYPE:
    case JS_SET_TYPE:
    case JS_MAP_TYPE:
    case JS_WEAK_MAP_TYPE:
    case JS_WEAK_SET_TYPE:
    case JS_PROMISE_CAPABILITY_TYPE:
    case JS_PROMISE_TYPE:
    case JS_PROXY_TYPE:
    case MAP_TYPE:
    case ALLOCATION_SITE_TYPE:
    case ACCESSOR_INFO_TYPE:
    case SHARED_FUNCTION_INFO_TYPE:
    case FUNCTION_TEMPLATE_INFO_TYPE:
    case ACCESSOR_PAIR_TYPE:
    case BYTE_ARRAY_TYPE:
    case BYTECODE_ARRAY_TYPE:
    case TRANSITION_ARRAY_TYPE:
3872
    case FEEDBACK_VECTOR_TYPE:
3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892
    case FOREIGN_TYPE:
    case SCRIPT_TYPE:
    case CODE_TYPE:
    case PROPERTY_CELL_TYPE:
    case MODULE_TYPE:
    case MODULE_INFO_ENTRY_TYPE:
    case FREE_SPACE_TYPE:
#define FIXED_TYPED_ARRAY_CASE(Type, type, TYPE, ctype, size) \
  case FIXED_##TYPE##_ARRAY_TYPE:
      TYPED_ARRAYS(FIXED_TYPED_ARRAY_CASE)
#undef FIXED_TYPED_ARRAY_CASE
    case FILLER_TYPE:
    case ACCESS_CHECK_INFO_TYPE:
    case INTERCEPTOR_INFO_TYPE:
    case OBJECT_TEMPLATE_INFO_TYPE:
    case ALLOCATION_MEMENTO_TYPE:
    case ALIASED_ARGUMENTS_ENTRY_TYPE:
    case PROMISE_RESOLVE_THENABLE_JOB_INFO_TYPE:
    case PROMISE_REACTION_JOB_INFO_TYPE:
    case DEBUG_INFO_TYPE:
3893
    case STACK_FRAME_INFO_TYPE:
3894 3895
    case CELL_TYPE:
    case WEAK_CELL_TYPE:
3896
    case SMALL_ORDERED_HASH_MAP_TYPE:
3897
    case SMALL_ORDERED_HASH_SET_TYPE:
3898 3899 3900
    case PROTOTYPE_INFO_TYPE:
    case TUPLE2_TYPE:
    case TUPLE3_TYPE:
3901
    case ASYNC_GENERATOR_REQUEST_TYPE:
3902 3903 3904 3905
    case WASM_MODULE_TYPE:
    case WASM_INSTANCE_TYPE:
    case WASM_MEMORY_TYPE:
    case WASM_TABLE_TYPE:
3906 3907 3908 3909 3910 3911 3912 3913
      OFStream os(stderr);
      os << "[couldn't handle instance type " << map->instance_type() << "]"
         << std::endl;
      UNREACHABLE();
      break;
  }
  UNREACHABLE();
}
3914 3915 3916

Handle<Object> TranslatedState::MaterializeAt(int frame_index,
                                              int* value_index) {
3917
  CHECK_LT(static_cast<size_t>(frame_index), frames().size());
3918
  TranslatedFrame* frame = &(frames_[frame_index]);
3919
  CHECK_LT(static_cast<size_t>(*value_index), frame->values_.size());
3920 3921 3922 3923 3924 3925 3926 3927 3928

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

  switch (slot->kind()) {
    case TranslatedValue::kTagged:
    case TranslatedValue::kInt32:
    case TranslatedValue::kUInt32:
    case TranslatedValue::kBoolBit:
3929
    case TranslatedValue::kFloat:
3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941
    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::kCapturedObject: {
      // The map must be a tagged object.
      CHECK(frame->values_[*value_index].kind() == TranslatedValue::kTagged);
3942 3943
      CHECK(frame->values_[*value_index].GetValue()->IsMap());
      return MaterializeCapturedObjectAt(slot, frame_index, value_index);
3944 3945 3946 3947 3948
    }
    case TranslatedValue::kDuplicatedObject: {
      int object_index = slot->object_index();
      TranslatedState::ObjectPosition pos = object_positions_[object_index];

3949
      // Make sure the duplicate is referring to a previous object.
3950 3951 3952
      CHECK(pos.frame_index_ < frame_index ||
            (pos.frame_index_ == frame_index &&
             pos.value_index_ < *value_index - 1));
3953 3954 3955 3956 3957

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

      // The object should have a (non-sentinel) value.
3958 3959
      CHECK(!object.is_null() &&
            !object.is_identical_to(isolate_->factory()->arguments_marker()));
3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974

      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) {
3975
  CHECK_LT(static_cast<size_t>(object_index), object_positions_.size());
3976 3977 3978 3979 3980 3981 3982
  TranslatedState::ObjectPosition pos = object_positions_[object_index];
  return MaterializeAt(pos.frame_index_, &(pos.value_index_));
}

TranslatedFrame* TranslatedState::GetArgumentsInfoFromJSFrameIndex(
    int jsframe_index, int* args_count) {
  for (size_t i = 0; i < frames_.size(); i++) {
3983
    if (frames_[i].kind() == TranslatedFrame::kInterpretedFunction) {
3984 3985 3986
      if (jsframe_index > 0) {
        jsframe_index--;
      } else {
3987 3988
        // We have the JS function frame, now check if it has arguments
        // adaptor.
3989 3990 3991 3992 3993 3994
        if (i > 0 &&
            frames_[i - 1].kind() == TranslatedFrame::kArgumentsAdaptor) {
          *args_count = frames_[i - 1].height();
          return &(frames_[i - 1]);
        }
        *args_count =
3995
            frames_[i].shared_info()->internal_formal_parameter_count() + 1;
3996 3997 3998 3999 4000 4001 4002
        return &(frames_[i]);
      }
    }
  }
  return nullptr;
}

4003
void TranslatedState::StoreMaterializedValuesAndDeopt(JavaScriptFrame* frame) {
4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021
  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;
  }

4022
  CHECK_EQ(length, previously_materialized_objects->length());
4023 4024 4025 4026 4027 4028 4029

  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_]);

4030
    CHECK(value_info->IsMaterializedObject());
4031 4032 4033 4034 4035 4036 4037 4038

    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 {
4039
        CHECK(previously_materialized_objects->get(i) == *value);
4040 4041 4042 4043 4044 4045
      }
    }
  }
  if (new_store && value_changed) {
    materialized_store->Set(stack_frame_pointer_,
                            previously_materialized_objects);
4046
    CHECK(frames_[0].kind() == TranslatedFrame::kInterpretedFunction);
4047 4048
    CHECK_EQ(frame->function(), frames_[0].front().GetRawValue());
    Deoptimizer::DeoptimizeFunction(frame->function(), frame->LookupCode());
4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063
  }
}

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());
4064
  CHECK_EQ(length, previously_materialized_objects->length());
4065 4066 4067 4068 4069 4070 4071 4072

  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_]);
4073
      CHECK(value_info->IsMaterializedObject());
4074 4075 4076 4077 4078 4079 4080

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

4081 4082
}  // namespace internal
}  // namespace v8