665279a0ad3beece2b60851d35f93be1aea78c5e
[platform/upstream/nodejs.git] / deps / v8 / src / deoptimizer.cc
1 // Copyright 2013 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "src/v8.h"
6
7 #include "src/accessors.h"
8 #include "src/codegen.h"
9 #include "src/deoptimizer.h"
10 #include "src/disasm.h"
11 #include "src/full-codegen.h"
12 #include "src/global-handles.h"
13 #include "src/macro-assembler.h"
14 #include "src/prettyprinter.h"
15
16
17 namespace v8 {
18 namespace internal {
19
20 static MemoryChunk* AllocateCodeChunk(MemoryAllocator* allocator) {
21   return allocator->AllocateChunk(Deoptimizer::GetMaxDeoptTableSize(),
22                                   base::OS::CommitPageSize(),
23 #if defined(__native_client__)
24   // The Native Client port of V8 uses an interpreter,
25   // so code pages don't need PROT_EXEC.
26                                   NOT_EXECUTABLE,
27 #else
28                                   EXECUTABLE,
29 #endif
30                                   NULL);
31 }
32
33
34 DeoptimizerData::DeoptimizerData(MemoryAllocator* allocator)
35     : allocator_(allocator),
36       deoptimized_frame_info_(NULL),
37       current_(NULL) {
38   for (int i = 0; i < Deoptimizer::kBailoutTypesWithCodeEntry; ++i) {
39     deopt_entry_code_entries_[i] = -1;
40     deopt_entry_code_[i] = AllocateCodeChunk(allocator);
41   }
42 }
43
44
45 DeoptimizerData::~DeoptimizerData() {
46   for (int i = 0; i < Deoptimizer::kBailoutTypesWithCodeEntry; ++i) {
47     allocator_->Free(deopt_entry_code_[i]);
48     deopt_entry_code_[i] = NULL;
49   }
50 }
51
52
53 void DeoptimizerData::Iterate(ObjectVisitor* v) {
54   if (deoptimized_frame_info_ != NULL) {
55     deoptimized_frame_info_->Iterate(v);
56   }
57 }
58
59
60 Code* Deoptimizer::FindDeoptimizingCode(Address addr) {
61   if (function_->IsHeapObject()) {
62     // Search all deoptimizing code in the native context of the function.
63     Context* native_context = function_->context()->native_context();
64     Object* element = native_context->DeoptimizedCodeListHead();
65     while (!element->IsUndefined()) {
66       Code* code = Code::cast(element);
67       CHECK(code->kind() == Code::OPTIMIZED_FUNCTION);
68       if (code->contains(addr)) return code;
69       element = code->next_code_link();
70     }
71   }
72   return NULL;
73 }
74
75
76 // We rely on this function not causing a GC.  It is called from generated code
77 // without having a real stack frame in place.
78 Deoptimizer* Deoptimizer::New(JSFunction* function,
79                               BailoutType type,
80                               unsigned bailout_id,
81                               Address from,
82                               int fp_to_sp_delta,
83                               Isolate* isolate) {
84   Deoptimizer* deoptimizer = new Deoptimizer(isolate,
85                                              function,
86                                              type,
87                                              bailout_id,
88                                              from,
89                                              fp_to_sp_delta,
90                                              NULL);
91   CHECK(isolate->deoptimizer_data()->current_ == NULL);
92   isolate->deoptimizer_data()->current_ = deoptimizer;
93   return deoptimizer;
94 }
95
96
97 // No larger than 2K on all platforms
98 static const int kDeoptTableMaxEpilogueCodeSize = 2 * KB;
99
100
101 size_t Deoptimizer::GetMaxDeoptTableSize() {
102   int entries_size =
103       Deoptimizer::kMaxNumberOfEntries * Deoptimizer::table_entry_size_;
104   int commit_page_size = static_cast<int>(base::OS::CommitPageSize());
105   int page_count = ((kDeoptTableMaxEpilogueCodeSize + entries_size - 1) /
106                     commit_page_size) + 1;
107   return static_cast<size_t>(commit_page_size * page_count);
108 }
109
110
111 Deoptimizer* Deoptimizer::Grab(Isolate* isolate) {
112   Deoptimizer* result = isolate->deoptimizer_data()->current_;
113   CHECK_NOT_NULL(result);
114   result->DeleteFrameDescriptions();
115   isolate->deoptimizer_data()->current_ = NULL;
116   return result;
117 }
118
119
120 int Deoptimizer::ConvertJSFrameIndexToFrameIndex(int jsframe_index) {
121   if (jsframe_index == 0) return 0;
122
123   int frame_index = 0;
124   while (jsframe_index >= 0) {
125     FrameDescription* frame = output_[frame_index];
126     if (frame->GetFrameType() == StackFrame::JAVA_SCRIPT) {
127       jsframe_index--;
128     }
129     frame_index++;
130   }
131
132   return frame_index - 1;
133 }
134
135
136 DeoptimizedFrameInfo* Deoptimizer::DebuggerInspectableFrame(
137     JavaScriptFrame* frame,
138     int jsframe_index,
139     Isolate* isolate) {
140   CHECK(frame->is_optimized());
141   CHECK(isolate->deoptimizer_data()->deoptimized_frame_info_ == NULL);
142
143   // Get the function and code from the frame.
144   JSFunction* function = frame->function();
145   Code* code = frame->LookupCode();
146
147   // Locate the deoptimization point in the code. As we are at a call the
148   // return address must be at a place in the code with deoptimization support.
149   SafepointEntry safepoint_entry = code->GetSafepointEntry(frame->pc());
150   int deoptimization_index = safepoint_entry.deoptimization_index();
151   CHECK_NE(deoptimization_index, Safepoint::kNoDeoptimizationIndex);
152
153   // Always use the actual stack slots when calculating the fp to sp
154   // delta adding two for the function and context.
155   unsigned stack_slots = code->stack_slots();
156   unsigned fp_to_sp_delta = (stack_slots * kPointerSize) +
157       StandardFrameConstants::kFixedFrameSizeFromFp;
158
159   Deoptimizer* deoptimizer = new Deoptimizer(isolate,
160                                              function,
161                                              Deoptimizer::DEBUGGER,
162                                              deoptimization_index,
163                                              frame->pc(),
164                                              fp_to_sp_delta,
165                                              code);
166   Address tos = frame->fp() - fp_to_sp_delta;
167   deoptimizer->FillInputFrame(tos, frame);
168
169   // Calculate the output frames.
170   Deoptimizer::ComputeOutputFrames(deoptimizer);
171
172   // Create the GC safe output frame information and register it for GC
173   // handling.
174   CHECK_LT(jsframe_index, deoptimizer->jsframe_count());
175
176   // Convert JS frame index into frame index.
177   int frame_index = deoptimizer->ConvertJSFrameIndexToFrameIndex(jsframe_index);
178
179   bool has_arguments_adaptor =
180       frame_index > 0 &&
181       deoptimizer->output_[frame_index - 1]->GetFrameType() ==
182       StackFrame::ARGUMENTS_ADAPTOR;
183
184   int construct_offset = has_arguments_adaptor ? 2 : 1;
185   bool has_construct_stub =
186       frame_index >= construct_offset &&
187       deoptimizer->output_[frame_index - construct_offset]->GetFrameType() ==
188       StackFrame::CONSTRUCT;
189
190   DeoptimizedFrameInfo* info = new DeoptimizedFrameInfo(deoptimizer,
191                                                         frame_index,
192                                                         has_arguments_adaptor,
193                                                         has_construct_stub);
194   isolate->deoptimizer_data()->deoptimized_frame_info_ = info;
195
196   // Get the "simulated" top and size for the requested frame.
197   FrameDescription* parameters_frame =
198       deoptimizer->output_[
199           has_arguments_adaptor ? (frame_index - 1) : frame_index];
200
201   uint32_t parameters_size = (info->parameters_count() + 1) * kPointerSize;
202   Address parameters_top = reinterpret_cast<Address>(
203       parameters_frame->GetTop() + (parameters_frame->GetFrameSize() -
204                                     parameters_size));
205
206   uint32_t expressions_size = info->expression_count() * kPointerSize;
207   Address expressions_top = reinterpret_cast<Address>(
208       deoptimizer->output_[frame_index]->GetTop());
209
210   // Done with the GC-unsafe frame descriptions. This re-enables allocation.
211   deoptimizer->DeleteFrameDescriptions();
212
213   // Allocate a heap number for the doubles belonging to this frame.
214   deoptimizer->MaterializeHeapNumbersForDebuggerInspectableFrame(
215       parameters_top, parameters_size, expressions_top, expressions_size, info);
216
217   // Finished using the deoptimizer instance.
218   delete deoptimizer;
219
220   return info;
221 }
222
223
224 void Deoptimizer::DeleteDebuggerInspectableFrame(DeoptimizedFrameInfo* info,
225                                                  Isolate* isolate) {
226   CHECK_EQ(isolate->deoptimizer_data()->deoptimized_frame_info_, info);
227   delete info;
228   isolate->deoptimizer_data()->deoptimized_frame_info_ = NULL;
229 }
230
231
232 void Deoptimizer::GenerateDeoptimizationEntries(MacroAssembler* masm,
233                                                 int count,
234                                                 BailoutType type) {
235   TableEntryGenerator generator(masm, type, count);
236   generator.Generate();
237 }
238
239
240 void Deoptimizer::VisitAllOptimizedFunctionsForContext(
241     Context* context, OptimizedFunctionVisitor* visitor) {
242   DisallowHeapAllocation no_allocation;
243
244   CHECK(context->IsNativeContext());
245
246   visitor->EnterContext(context);
247
248   // Visit the list of optimized functions, removing elements that
249   // no longer refer to optimized code.
250   JSFunction* prev = NULL;
251   Object* element = context->OptimizedFunctionsListHead();
252   while (!element->IsUndefined()) {
253     JSFunction* function = JSFunction::cast(element);
254     Object* next = function->next_function_link();
255     if (function->code()->kind() != Code::OPTIMIZED_FUNCTION ||
256         (visitor->VisitFunction(function),
257          function->code()->kind() != Code::OPTIMIZED_FUNCTION)) {
258       // The function no longer refers to optimized code, or the visitor
259       // changed the code to which it refers to no longer be optimized code.
260       // Remove the function from this list.
261       if (prev != NULL) {
262         prev->set_next_function_link(next);
263       } else {
264         context->SetOptimizedFunctionsListHead(next);
265       }
266       // The visitor should not alter the link directly.
267       CHECK_EQ(function->next_function_link(), next);
268       // Set the next function link to undefined to indicate it is no longer
269       // in the optimized functions list.
270       function->set_next_function_link(context->GetHeap()->undefined_value());
271     } else {
272       // The visitor should not alter the link directly.
273       CHECK_EQ(function->next_function_link(), next);
274       // preserve this element.
275       prev = function;
276     }
277     element = next;
278   }
279
280   visitor->LeaveContext(context);
281 }
282
283
284 void Deoptimizer::VisitAllOptimizedFunctions(
285     Isolate* isolate,
286     OptimizedFunctionVisitor* visitor) {
287   DisallowHeapAllocation no_allocation;
288
289   // Run through the list of all native contexts.
290   Object* context = isolate->heap()->native_contexts_list();
291   while (!context->IsUndefined()) {
292     VisitAllOptimizedFunctionsForContext(Context::cast(context), visitor);
293     context = Context::cast(context)->get(Context::NEXT_CONTEXT_LINK);
294   }
295 }
296
297
298 // Unlink functions referring to code marked for deoptimization, then move
299 // marked code from the optimized code list to the deoptimized code list,
300 // and patch code for lazy deopt.
301 void Deoptimizer::DeoptimizeMarkedCodeForContext(Context* context) {
302   DisallowHeapAllocation no_allocation;
303
304   // A "closure" that unlinks optimized code that is going to be
305   // deoptimized from the functions that refer to it.
306   class SelectedCodeUnlinker: public OptimizedFunctionVisitor {
307    public:
308     virtual void EnterContext(Context* context) { }  // Don't care.
309     virtual void LeaveContext(Context* context)  { }  // Don't care.
310     virtual void VisitFunction(JSFunction* function) {
311       Code* code = function->code();
312       if (!code->marked_for_deoptimization()) return;
313
314       // Unlink this function and evict from optimized code map.
315       SharedFunctionInfo* shared = function->shared();
316       function->set_code(shared->code());
317
318       if (FLAG_trace_deopt) {
319         CodeTracer::Scope scope(code->GetHeap()->isolate()->GetCodeTracer());
320         PrintF(scope.file(), "[deoptimizer unlinked: ");
321         function->PrintName(scope.file());
322         PrintF(scope.file(),
323                " / %" V8PRIxPTR "]\n", reinterpret_cast<intptr_t>(function));
324       }
325     }
326   };
327
328   // Unlink all functions that refer to marked code.
329   SelectedCodeUnlinker unlinker;
330   VisitAllOptimizedFunctionsForContext(context, &unlinker);
331
332   Isolate* isolate = context->GetHeap()->isolate();
333 #ifdef DEBUG
334   Code* topmost_optimized_code = NULL;
335   bool safe_to_deopt_topmost_optimized_code = false;
336   // Make sure all activations of optimized code can deopt at their current PC.
337   // The topmost optimized code has special handling because it cannot be
338   // deoptimized due to weak object dependency.
339   for (StackFrameIterator it(isolate, isolate->thread_local_top());
340        !it.done(); it.Advance()) {
341     StackFrame::Type type = it.frame()->type();
342     if (type == StackFrame::OPTIMIZED) {
343       Code* code = it.frame()->LookupCode();
344       if (FLAG_trace_deopt) {
345         JSFunction* function =
346             static_cast<OptimizedFrame*>(it.frame())->function();
347         CodeTracer::Scope scope(isolate->GetCodeTracer());
348         PrintF(scope.file(), "[deoptimizer found activation of function: ");
349         function->PrintName(scope.file());
350         PrintF(scope.file(),
351                " / %" V8PRIxPTR "]\n", reinterpret_cast<intptr_t>(function));
352       }
353       SafepointEntry safepoint = code->GetSafepointEntry(it.frame()->pc());
354       int deopt_index = safepoint.deoptimization_index();
355       // Turbofan deopt is checked when we are patching addresses on stack.
356       bool turbofanned = code->is_turbofanned() && !FLAG_turbo_deoptimization;
357       bool safe_to_deopt =
358           deopt_index != Safepoint::kNoDeoptimizationIndex || turbofanned;
359       CHECK(topmost_optimized_code == NULL || safe_to_deopt || turbofanned);
360       if (topmost_optimized_code == NULL) {
361         topmost_optimized_code = code;
362         safe_to_deopt_topmost_optimized_code = safe_to_deopt;
363       }
364     }
365   }
366 #endif
367
368   // Move marked code from the optimized code list to the deoptimized
369   // code list, collecting them into a ZoneList.
370   Zone zone;
371   ZoneList<Code*> codes(10, &zone);
372
373   // Walk over all optimized code objects in this native context.
374   Code* prev = NULL;
375   Object* element = context->OptimizedCodeListHead();
376   while (!element->IsUndefined()) {
377     Code* code = Code::cast(element);
378     CHECK_EQ(code->kind(), Code::OPTIMIZED_FUNCTION);
379     Object* next = code->next_code_link();
380
381     if (code->marked_for_deoptimization() &&
382         (!code->is_turbofanned() || FLAG_turbo_deoptimization)) {
383       // Put the code into the list for later patching.
384       codes.Add(code, &zone);
385
386       if (prev != NULL) {
387         // Skip this code in the optimized code list.
388         prev->set_next_code_link(next);
389       } else {
390         // There was no previous node, the next node is the new head.
391         context->SetOptimizedCodeListHead(next);
392       }
393
394       // Move the code to the _deoptimized_ code list.
395       code->set_next_code_link(context->DeoptimizedCodeListHead());
396       context->SetDeoptimizedCodeListHead(code);
397     } else {
398       // Not marked; preserve this element.
399       prev = code;
400     }
401     element = next;
402   }
403
404   // TODO(titzer): we need a handle scope only because of the macro assembler,
405   // which is only used in EnsureCodeForDeoptimizationEntry.
406   HandleScope scope(isolate);
407
408   // Now patch all the codes for deoptimization.
409   for (int i = 0; i < codes.length(); i++) {
410 #ifdef DEBUG
411     if (codes[i] == topmost_optimized_code) {
412       DCHECK(safe_to_deopt_topmost_optimized_code);
413     }
414 #endif
415     // It is finally time to die, code object.
416
417     // Remove the code from optimized code map.
418     DeoptimizationInputData* deopt_data =
419         DeoptimizationInputData::cast(codes[i]->deoptimization_data());
420     SharedFunctionInfo* shared =
421         SharedFunctionInfo::cast(deopt_data->SharedFunctionInfo());
422     shared->EvictFromOptimizedCodeMap(codes[i], "deoptimized code");
423
424     // Do platform-specific patching to force any activations to lazy deopt.
425     if (!codes[i]->is_turbofanned() || FLAG_turbo_deoptimization) {
426       PatchCodeForDeoptimization(isolate, codes[i]);
427
428       // We might be in the middle of incremental marking with compaction.
429       // Tell collector to treat this code object in a special way and
430       // ignore all slots that might have been recorded on it.
431       isolate->heap()->mark_compact_collector()->InvalidateCode(codes[i]);
432     }
433   }
434 }
435
436
437 void Deoptimizer::DeoptimizeAll(Isolate* isolate) {
438   if (FLAG_trace_deopt) {
439     CodeTracer::Scope scope(isolate->GetCodeTracer());
440     PrintF(scope.file(), "[deoptimize all code in all contexts]\n");
441   }
442   DisallowHeapAllocation no_allocation;
443   // For all contexts, mark all code, then deoptimize.
444   Object* context = isolate->heap()->native_contexts_list();
445   while (!context->IsUndefined()) {
446     Context* native_context = Context::cast(context);
447     MarkAllCodeForContext(native_context);
448     DeoptimizeMarkedCodeForContext(native_context);
449     context = native_context->get(Context::NEXT_CONTEXT_LINK);
450   }
451 }
452
453
454 void Deoptimizer::DeoptimizeMarkedCode(Isolate* isolate) {
455   if (FLAG_trace_deopt) {
456     CodeTracer::Scope scope(isolate->GetCodeTracer());
457     PrintF(scope.file(), "[deoptimize marked code in all contexts]\n");
458   }
459   DisallowHeapAllocation no_allocation;
460   // For all contexts, deoptimize code already marked.
461   Object* context = isolate->heap()->native_contexts_list();
462   while (!context->IsUndefined()) {
463     Context* native_context = Context::cast(context);
464     DeoptimizeMarkedCodeForContext(native_context);
465     context = native_context->get(Context::NEXT_CONTEXT_LINK);
466   }
467 }
468
469
470 void Deoptimizer::DeoptimizeGlobalObject(JSObject* object) {
471   if (FLAG_trace_deopt) {
472     CodeTracer::Scope scope(object->GetHeap()->isolate()->GetCodeTracer());
473     PrintF(scope.file(), "[deoptimize global object @ 0x%08" V8PRIxPTR "]\n",
474         reinterpret_cast<intptr_t>(object));
475   }
476   if (object->IsJSGlobalProxy()) {
477     PrototypeIterator iter(object->GetIsolate(), object);
478     // TODO(verwaest): This CHECK will be hit if the global proxy is detached.
479     CHECK(iter.GetCurrent()->IsJSGlobalObject());
480     Context* native_context =
481         GlobalObject::cast(iter.GetCurrent())->native_context();
482     MarkAllCodeForContext(native_context);
483     DeoptimizeMarkedCodeForContext(native_context);
484   } else if (object->IsGlobalObject()) {
485     Context* native_context = GlobalObject::cast(object)->native_context();
486     MarkAllCodeForContext(native_context);
487     DeoptimizeMarkedCodeForContext(native_context);
488   }
489 }
490
491
492 void Deoptimizer::MarkAllCodeForContext(Context* context) {
493   Object* element = context->OptimizedCodeListHead();
494   while (!element->IsUndefined()) {
495     Code* code = Code::cast(element);
496     CHECK_EQ(code->kind(), Code::OPTIMIZED_FUNCTION);
497     code->set_marked_for_deoptimization(true);
498     element = code->next_code_link();
499   }
500 }
501
502
503 void Deoptimizer::DeoptimizeFunction(JSFunction* function) {
504   Code* code = function->code();
505   if (code->kind() == Code::OPTIMIZED_FUNCTION) {
506     // Mark the code for deoptimization and unlink any functions that also
507     // refer to that code. The code cannot be shared across native contexts,
508     // so we only need to search one.
509     code->set_marked_for_deoptimization(true);
510     DeoptimizeMarkedCodeForContext(function->context()->native_context());
511   }
512 }
513
514
515 void Deoptimizer::ComputeOutputFrames(Deoptimizer* deoptimizer) {
516   deoptimizer->DoComputeOutputFrames();
517 }
518
519
520 bool Deoptimizer::TraceEnabledFor(BailoutType deopt_type,
521                                   StackFrame::Type frame_type) {
522   switch (deopt_type) {
523     case EAGER:
524     case SOFT:
525     case LAZY:
526     case DEBUGGER:
527       return (frame_type == StackFrame::STUB)
528           ? FLAG_trace_stub_failures
529           : FLAG_trace_deopt;
530   }
531   FATAL("Unsupported deopt type");
532   return false;
533 }
534
535
536 const char* Deoptimizer::MessageFor(BailoutType type) {
537   switch (type) {
538     case EAGER: return "eager";
539     case SOFT: return "soft";
540     case LAZY: return "lazy";
541     case DEBUGGER: return "debugger";
542   }
543   FATAL("Unsupported deopt type");
544   return NULL;
545 }
546
547
548 Deoptimizer::Deoptimizer(Isolate* isolate,
549                          JSFunction* function,
550                          BailoutType type,
551                          unsigned bailout_id,
552                          Address from,
553                          int fp_to_sp_delta,
554                          Code* optimized_code)
555     : isolate_(isolate),
556       function_(function),
557       bailout_id_(bailout_id),
558       bailout_type_(type),
559       from_(from),
560       fp_to_sp_delta_(fp_to_sp_delta),
561       has_alignment_padding_(0),
562       input_(NULL),
563       output_count_(0),
564       jsframe_count_(0),
565       output_(NULL),
566       deferred_objects_tagged_values_(0),
567       deferred_objects_double_values_(0),
568       deferred_objects_(0),
569       deferred_heap_numbers_(0),
570       jsframe_functions_(0),
571       jsframe_has_adapted_arguments_(0),
572       materialized_values_(NULL),
573       materialized_objects_(NULL),
574       materialization_value_index_(0),
575       materialization_object_index_(0),
576       trace_scope_(NULL) {
577   // For COMPILED_STUBs called from builtins, the function pointer is a SMI
578   // indicating an internal frame.
579   if (function->IsSmi()) {
580     function = NULL;
581   }
582   DCHECK(from != NULL);
583   if (function != NULL && function->IsOptimized()) {
584     function->shared()->increment_deopt_count();
585     if (bailout_type_ == Deoptimizer::SOFT) {
586       isolate->counters()->soft_deopts_executed()->Increment();
587       // Soft deopts shouldn't count against the overall re-optimization count
588       // that can eventually lead to disabling optimization for a function.
589       int opt_count = function->shared()->opt_count();
590       if (opt_count > 0) opt_count--;
591       function->shared()->set_opt_count(opt_count);
592     }
593   }
594   compiled_code_ = FindOptimizedCode(function, optimized_code);
595 #if DEBUG
596   DCHECK(compiled_code_ != NULL);
597   if (type == EAGER || type == SOFT || type == LAZY) {
598     DCHECK(compiled_code_->kind() != Code::FUNCTION);
599   }
600 #endif
601
602   StackFrame::Type frame_type = function == NULL
603       ? StackFrame::STUB
604       : StackFrame::JAVA_SCRIPT;
605   trace_scope_ = TraceEnabledFor(type, frame_type) ?
606       new CodeTracer::Scope(isolate->GetCodeTracer()) : NULL;
607 #ifdef DEBUG
608   CHECK(AllowHeapAllocation::IsAllowed());
609   disallow_heap_allocation_ = new DisallowHeapAllocation();
610 #endif  // DEBUG
611   if (compiled_code_->kind() == Code::OPTIMIZED_FUNCTION) {
612     PROFILE(isolate_, CodeDeoptEvent(compiled_code_, bailout_id_, from_,
613                                      fp_to_sp_delta_));
614   }
615   unsigned size = ComputeInputFrameSize();
616   input_ = new(size) FrameDescription(size, function);
617   input_->SetFrameType(frame_type);
618 }
619
620
621 Code* Deoptimizer::FindOptimizedCode(JSFunction* function,
622                                      Code* optimized_code) {
623   switch (bailout_type_) {
624     case Deoptimizer::SOFT:
625     case Deoptimizer::EAGER:
626     case Deoptimizer::LAZY: {
627       Code* compiled_code = FindDeoptimizingCode(from_);
628       return (compiled_code == NULL)
629           ? static_cast<Code*>(isolate_->FindCodeObject(from_))
630           : compiled_code;
631     }
632     case Deoptimizer::DEBUGGER:
633       DCHECK(optimized_code->contains(from_));
634       return optimized_code;
635   }
636   FATAL("Could not find code for optimized function");
637   return NULL;
638 }
639
640
641 void Deoptimizer::PrintFunctionName() {
642   if (function_->IsJSFunction()) {
643     function_->ShortPrint(trace_scope_->file());
644   } else {
645     PrintF(trace_scope_->file(),
646            "%s", Code::Kind2String(compiled_code_->kind()));
647   }
648 }
649
650
651 Deoptimizer::~Deoptimizer() {
652   DCHECK(input_ == NULL && output_ == NULL);
653   DCHECK(disallow_heap_allocation_ == NULL);
654   delete trace_scope_;
655 }
656
657
658 void Deoptimizer::DeleteFrameDescriptions() {
659   delete input_;
660   for (int i = 0; i < output_count_; ++i) {
661     if (output_[i] != input_) delete output_[i];
662   }
663   delete[] output_;
664   input_ = NULL;
665   output_ = NULL;
666 #ifdef DEBUG
667   CHECK(!AllowHeapAllocation::IsAllowed());
668   CHECK(disallow_heap_allocation_ != NULL);
669   delete disallow_heap_allocation_;
670   disallow_heap_allocation_ = NULL;
671 #endif  // DEBUG
672 }
673
674
675 Address Deoptimizer::GetDeoptimizationEntry(Isolate* isolate,
676                                             int id,
677                                             BailoutType type,
678                                             GetEntryMode mode) {
679   CHECK_GE(id, 0);
680   if (id >= kMaxNumberOfEntries) return NULL;
681   if (mode == ENSURE_ENTRY_CODE) {
682     EnsureCodeForDeoptimizationEntry(isolate, type, id);
683   } else {
684     CHECK_EQ(mode, CALCULATE_ENTRY_ADDRESS);
685   }
686   DeoptimizerData* data = isolate->deoptimizer_data();
687   CHECK_LT(type, kBailoutTypesWithCodeEntry);
688   MemoryChunk* base = data->deopt_entry_code_[type];
689   return base->area_start() + (id * table_entry_size_);
690 }
691
692
693 int Deoptimizer::GetDeoptimizationId(Isolate* isolate,
694                                      Address addr,
695                                      BailoutType type) {
696   DeoptimizerData* data = isolate->deoptimizer_data();
697   MemoryChunk* base = data->deopt_entry_code_[type];
698   Address start = base->area_start();
699   if (addr < start ||
700       addr >= start + (kMaxNumberOfEntries * table_entry_size_)) {
701     return kNotDeoptimizationEntry;
702   }
703   DCHECK_EQ(0,
704             static_cast<int>(addr - start) % table_entry_size_);
705   return static_cast<int>(addr - start) / table_entry_size_;
706 }
707
708
709 int Deoptimizer::GetOutputInfo(DeoptimizationOutputData* data,
710                                BailoutId id,
711                                SharedFunctionInfo* shared) {
712   // TODO(kasperl): For now, we do a simple linear search for the PC
713   // offset associated with the given node id. This should probably be
714   // changed to a binary search.
715   int length = data->DeoptPoints();
716   for (int i = 0; i < length; i++) {
717     if (data->AstId(i) == id) {
718       return data->PcAndState(i)->value();
719     }
720   }
721   OFStream os(stderr);
722   os << "[couldn't find pc offset for node=" << id.ToInt() << "]\n"
723      << "[method: " << shared->DebugName()->ToCString().get() << "]\n"
724      << "[source:\n" << SourceCodeOf(shared) << "\n]" << std::endl;
725
726   FATAL("unable to find pc offset during deoptimization");
727   return -1;
728 }
729
730
731 int Deoptimizer::GetDeoptimizedCodeCount(Isolate* isolate) {
732   int length = 0;
733   // Count all entries in the deoptimizing code list of every context.
734   Object* context = isolate->heap()->native_contexts_list();
735   while (!context->IsUndefined()) {
736     Context* native_context = Context::cast(context);
737     Object* element = native_context->DeoptimizedCodeListHead();
738     while (!element->IsUndefined()) {
739       Code* code = Code::cast(element);
740       DCHECK(code->kind() == Code::OPTIMIZED_FUNCTION);
741       length++;
742       element = code->next_code_link();
743     }
744     context = Context::cast(context)->get(Context::NEXT_CONTEXT_LINK);
745   }
746   return length;
747 }
748
749
750 // We rely on this function not causing a GC.  It is called from generated code
751 // without having a real stack frame in place.
752 void Deoptimizer::DoComputeOutputFrames() {
753   base::ElapsedTimer timer;
754
755   // Determine basic deoptimization information.  The optimized frame is
756   // described by the input data.
757   DeoptimizationInputData* input_data =
758       DeoptimizationInputData::cast(compiled_code_->deoptimization_data());
759
760   if (trace_scope_ != NULL) {
761     timer.Start();
762     PrintF(trace_scope_->file(), "[deoptimizing (DEOPT %s): begin ",
763            MessageFor(bailout_type_));
764     PrintFunctionName();
765     PrintF(trace_scope_->file(),
766            " (opt #%d) @%d, FP to SP delta: %d]\n",
767            input_data->OptimizationId()->value(),
768            bailout_id_,
769            fp_to_sp_delta_);
770     if (bailout_type_ == EAGER || bailout_type_ == SOFT ||
771         (compiled_code_->is_hydrogen_stub())) {
772       compiled_code_->PrintDeoptLocation(trace_scope_->file(), bailout_id_);
773     }
774   }
775
776   BailoutId node_id = input_data->AstId(bailout_id_);
777   ByteArray* translations = input_data->TranslationByteArray();
778   unsigned translation_index =
779       input_data->TranslationIndex(bailout_id_)->value();
780
781   // Do the input frame to output frame(s) translation.
782   TranslationIterator iterator(translations, translation_index);
783   Translation::Opcode opcode =
784       static_cast<Translation::Opcode>(iterator.Next());
785   DCHECK(Translation::BEGIN == opcode);
786   USE(opcode);
787   // Read the number of output frames and allocate an array for their
788   // descriptions.
789   int count = iterator.Next();
790   iterator.Next();  // Drop JS frames count.
791   DCHECK(output_ == NULL);
792   output_ = new FrameDescription*[count];
793   for (int i = 0; i < count; ++i) {
794     output_[i] = NULL;
795   }
796   output_count_ = count;
797
798   Register fp_reg = JavaScriptFrame::fp_register();
799   stack_fp_ = reinterpret_cast<Address>(
800       input_->GetRegister(fp_reg.code()) +
801           has_alignment_padding_ * kPointerSize);
802
803   // Translate each output frame.
804   for (int i = 0; i < count; ++i) {
805     // Read the ast node id, function, and frame height for this output frame.
806     Translation::Opcode opcode =
807         static_cast<Translation::Opcode>(iterator.Next());
808     switch (opcode) {
809       case Translation::JS_FRAME:
810         DoComputeJSFrame(&iterator, i);
811         jsframe_count_++;
812         break;
813       case Translation::ARGUMENTS_ADAPTOR_FRAME:
814         DoComputeArgumentsAdaptorFrame(&iterator, i);
815         break;
816       case Translation::CONSTRUCT_STUB_FRAME:
817         DoComputeConstructStubFrame(&iterator, i);
818         break;
819       case Translation::GETTER_STUB_FRAME:
820         DoComputeAccessorStubFrame(&iterator, i, false);
821         break;
822       case Translation::SETTER_STUB_FRAME:
823         DoComputeAccessorStubFrame(&iterator, i, true);
824         break;
825       case Translation::COMPILED_STUB_FRAME:
826         DoComputeCompiledStubFrame(&iterator, i);
827         break;
828       case Translation::BEGIN:
829       case Translation::REGISTER:
830       case Translation::INT32_REGISTER:
831       case Translation::UINT32_REGISTER:
832       case Translation::DOUBLE_REGISTER:
833       case Translation::STACK_SLOT:
834       case Translation::INT32_STACK_SLOT:
835       case Translation::UINT32_STACK_SLOT:
836       case Translation::DOUBLE_STACK_SLOT:
837       case Translation::LITERAL:
838       case Translation::ARGUMENTS_OBJECT:
839       default:
840         FATAL("Unsupported translation");
841         break;
842     }
843   }
844
845   // Print some helpful diagnostic information.
846   if (trace_scope_ != NULL) {
847     double ms = timer.Elapsed().InMillisecondsF();
848     int index = output_count_ - 1;  // Index of the topmost frame.
849     PrintF(trace_scope_->file(), "[deoptimizing (%s): end ",
850            MessageFor(bailout_type_));
851     PrintFunctionName();
852     PrintF(trace_scope_->file(),
853            " @%d => node=%d, pc=0x%08" V8PRIxPTR ", state=%s, alignment=%s,"
854            " took %0.3f ms]\n",
855            bailout_id_,
856            node_id.ToInt(),
857            output_[index]->GetPc(),
858            FullCodeGenerator::State2String(
859                static_cast<FullCodeGenerator::State>(
860                    output_[index]->GetState()->value())),
861            has_alignment_padding_ ? "with padding" : "no padding",
862            ms);
863   }
864 }
865
866
867 void Deoptimizer::DoComputeJSFrame(TranslationIterator* iterator,
868                                    int frame_index) {
869   BailoutId node_id = BailoutId(iterator->Next());
870   JSFunction* function;
871   if (frame_index != 0) {
872     function = JSFunction::cast(ComputeLiteral(iterator->Next()));
873   } else {
874     int closure_id = iterator->Next();
875     USE(closure_id);
876     CHECK_EQ(Translation::kSelfLiteralId, closure_id);
877     function = function_;
878   }
879   unsigned height = iterator->Next() - 1;  // Do not count the context.
880   unsigned height_in_bytes = height * kPointerSize;
881   if (trace_scope_ != NULL) {
882     PrintF(trace_scope_->file(), "  translating ");
883     function->PrintName(trace_scope_->file());
884     PrintF(trace_scope_->file(),
885            " => node=%d, height=%d\n", node_id.ToInt(), height_in_bytes);
886   }
887
888   // The 'fixed' part of the frame consists of the incoming parameters and
889   // the part described by JavaScriptFrameConstants.
890   unsigned fixed_frame_size = ComputeFixedSize(function);
891   unsigned input_frame_size = input_->GetFrameSize();
892   unsigned output_frame_size = height_in_bytes + fixed_frame_size;
893
894   // Allocate and store the output frame description.
895   FrameDescription* output_frame =
896       new(output_frame_size) FrameDescription(output_frame_size, function);
897   output_frame->SetFrameType(StackFrame::JAVA_SCRIPT);
898
899   bool is_bottommost = (0 == frame_index);
900   bool is_topmost = (output_count_ - 1 == frame_index);
901   CHECK(frame_index >= 0 && frame_index < output_count_);
902   CHECK_NULL(output_[frame_index]);
903   output_[frame_index] = output_frame;
904
905   // The top address for the bottommost output frame can be computed from
906   // the input frame pointer and the output frame's height.  For all
907   // subsequent output frames, it can be computed from the previous one's
908   // top address and the current frame's size.
909   Register fp_reg = JavaScriptFrame::fp_register();
910   intptr_t top_address;
911   if (is_bottommost) {
912     // Determine whether the input frame contains alignment padding.
913     has_alignment_padding_ =
914         (!compiled_code_->is_turbofanned() && HasAlignmentPadding(function))
915             ? 1
916             : 0;
917     // 2 = context and function in the frame.
918     // If the optimized frame had alignment padding, adjust the frame pointer
919     // to point to the new position of the old frame pointer after padding
920     // is removed. Subtract 2 * kPointerSize for the context and function slots.
921     top_address = input_->GetRegister(fp_reg.code()) -
922         StandardFrameConstants::kFixedFrameSizeFromFp -
923         height_in_bytes + has_alignment_padding_ * kPointerSize;
924   } else {
925     top_address = output_[frame_index - 1]->GetTop() - output_frame_size;
926   }
927   output_frame->SetTop(top_address);
928
929   // Compute the incoming parameter translation.
930   int parameter_count =
931       function->shared()->internal_formal_parameter_count() + 1;
932   unsigned output_offset = output_frame_size;
933   unsigned input_offset = input_frame_size;
934   for (int i = 0; i < parameter_count; ++i) {
935     output_offset -= kPointerSize;
936     DoTranslateCommand(iterator, frame_index, output_offset);
937   }
938   input_offset -= (parameter_count * kPointerSize);
939
940   // There are no translation commands for the caller's pc and fp, the
941   // context, and the function.  Synthesize their values and set them up
942   // explicitly.
943   //
944   // The caller's pc for the bottommost output frame is the same as in the
945   // input frame.  For all subsequent output frames, it can be read from the
946   // previous one.  This frame's pc can be computed from the non-optimized
947   // function code and AST id of the bailout.
948   output_offset -= kPCOnStackSize;
949   input_offset -= kPCOnStackSize;
950   intptr_t value;
951   if (is_bottommost) {
952     value = input_->GetFrameSlot(input_offset);
953   } else {
954     value = output_[frame_index - 1]->GetPc();
955   }
956   output_frame->SetCallerPc(output_offset, value);
957   if (trace_scope_ != NULL) {
958     PrintF(trace_scope_->file(),
959            "    0x%08" V8PRIxPTR ": [top + %d] <- 0x%08"
960            V8PRIxPTR  " ; caller's pc\n",
961            top_address + output_offset, output_offset, value);
962   }
963
964   // The caller's frame pointer for the bottommost output frame is the same
965   // as in the input frame.  For all subsequent output frames, it can be
966   // read from the previous one.  Also compute and set this frame's frame
967   // pointer.
968   output_offset -= kFPOnStackSize;
969   input_offset -= kFPOnStackSize;
970   if (is_bottommost) {
971     value = input_->GetFrameSlot(input_offset);
972   } else {
973     value = output_[frame_index - 1]->GetFp();
974   }
975   output_frame->SetCallerFp(output_offset, value);
976   intptr_t fp_value = top_address + output_offset;
977   DCHECK(!is_bottommost || (input_->GetRegister(fp_reg.code()) +
978       has_alignment_padding_ * kPointerSize) == fp_value);
979   output_frame->SetFp(fp_value);
980   if (is_topmost) output_frame->SetRegister(fp_reg.code(), fp_value);
981   if (trace_scope_ != NULL) {
982     PrintF(trace_scope_->file(),
983            "    0x%08" V8PRIxPTR ": [top + %d] <- 0x%08"
984            V8PRIxPTR " ; caller's fp\n",
985            fp_value, output_offset, value);
986   }
987   DCHECK(!is_bottommost || !has_alignment_padding_ ||
988          (fp_value & kPointerSize) != 0);
989
990   if (FLAG_enable_ool_constant_pool) {
991     // For the bottommost output frame the constant pool pointer can be gotten
992     // from the input frame. For subsequent output frames, it can be read from
993     // the previous frame.
994     output_offset -= kPointerSize;
995     input_offset -= kPointerSize;
996     if (is_bottommost) {
997       value = input_->GetFrameSlot(input_offset);
998     } else {
999       value = output_[frame_index - 1]->GetConstantPool();
1000     }
1001     output_frame->SetCallerConstantPool(output_offset, value);
1002     if (trace_scope_) {
1003       PrintF("    0x%08" V8PRIxPTR ": [top + %d] <- 0x%08"
1004              V8PRIxPTR "; caller's constant_pool\n",
1005              top_address + output_offset, output_offset, value);
1006     }
1007   }
1008
1009   // For the bottommost output frame the context can be gotten from the input
1010   // frame. For all subsequent output frames it can be gotten from the function
1011   // so long as we don't inline functions that need local contexts.
1012   Register context_reg = JavaScriptFrame::context_register();
1013   output_offset -= kPointerSize;
1014   input_offset -= kPointerSize;
1015   // Read the context from the translations.
1016   DoTranslateCommand(iterator, frame_index, output_offset);
1017   value = output_frame->GetFrameSlot(output_offset);
1018   // The context should not be a placeholder for a materialized object.
1019   CHECK(value !=
1020         reinterpret_cast<intptr_t>(isolate_->heap()->arguments_marker()));
1021   if (value ==
1022       reinterpret_cast<intptr_t>(isolate_->heap()->undefined_value())) {
1023     // If the context was optimized away, just use the context from
1024     // the activation. This should only apply to Crankshaft code.
1025     CHECK(!compiled_code_->is_turbofanned());
1026     if (is_bottommost) {
1027       value = input_->GetFrameSlot(input_offset);
1028     } else {
1029       value = reinterpret_cast<intptr_t>(function->context());
1030     }
1031     output_frame->SetFrameSlot(output_offset, value);
1032   }
1033   output_frame->SetContext(value);
1034   if (is_topmost) output_frame->SetRegister(context_reg.code(), value);
1035   if (trace_scope_ != NULL) {
1036     PrintF(trace_scope_->file(),
1037            "    0x%08" V8PRIxPTR ": [top + %d] <- 0x%08"
1038            V8PRIxPTR "; context\n",
1039            top_address + output_offset, output_offset, value);
1040   }
1041
1042   // The function was mentioned explicitly in the BEGIN_FRAME.
1043   output_offset -= kPointerSize;
1044   input_offset -= kPointerSize;
1045   value = reinterpret_cast<intptr_t>(function);
1046   // The function for the bottommost output frame should also agree with the
1047   // input frame.
1048   DCHECK(!is_bottommost || input_->GetFrameSlot(input_offset) == value);
1049   output_frame->SetFrameSlot(output_offset, value);
1050   if (trace_scope_ != NULL) {
1051     PrintF(trace_scope_->file(),
1052            "    0x%08" V8PRIxPTR ": [top + %d] <- 0x%08"
1053            V8PRIxPTR "; function\n",
1054            top_address + output_offset, output_offset, value);
1055   }
1056
1057   // Translate the rest of the frame.
1058   for (unsigned i = 0; i < height; ++i) {
1059     output_offset -= kPointerSize;
1060     DoTranslateCommand(iterator, frame_index, output_offset);
1061   }
1062   CHECK_EQ(0u, output_offset);
1063
1064   // Compute this frame's PC, state, and continuation.
1065   Code* non_optimized_code = function->shared()->code();
1066   FixedArray* raw_data = non_optimized_code->deoptimization_data();
1067   DeoptimizationOutputData* data = DeoptimizationOutputData::cast(raw_data);
1068   Address start = non_optimized_code->instruction_start();
1069   unsigned pc_and_state = GetOutputInfo(data, node_id, function->shared());
1070   unsigned pc_offset = FullCodeGenerator::PcField::decode(pc_and_state);
1071   intptr_t pc_value = reinterpret_cast<intptr_t>(start + pc_offset);
1072   output_frame->SetPc(pc_value);
1073
1074   // Update constant pool.
1075   if (FLAG_enable_ool_constant_pool) {
1076     intptr_t constant_pool_value =
1077         reinterpret_cast<intptr_t>(non_optimized_code->constant_pool());
1078     output_frame->SetConstantPool(constant_pool_value);
1079     if (is_topmost) {
1080       Register constant_pool_reg =
1081           JavaScriptFrame::constant_pool_pointer_register();
1082       output_frame->SetRegister(constant_pool_reg.code(), constant_pool_value);
1083     }
1084   }
1085
1086   FullCodeGenerator::State state =
1087       FullCodeGenerator::StateField::decode(pc_and_state);
1088   output_frame->SetState(Smi::FromInt(state));
1089
1090   // Set the continuation for the topmost frame.
1091   if (is_topmost && bailout_type_ != DEBUGGER) {
1092     Builtins* builtins = isolate_->builtins();
1093     Code* continuation = builtins->builtin(Builtins::kNotifyDeoptimized);
1094     if (bailout_type_ == LAZY) {
1095       continuation = builtins->builtin(Builtins::kNotifyLazyDeoptimized);
1096     } else if (bailout_type_ == SOFT) {
1097       continuation = builtins->builtin(Builtins::kNotifySoftDeoptimized);
1098     } else {
1099       CHECK_EQ(bailout_type_, EAGER);
1100     }
1101     output_frame->SetContinuation(
1102         reinterpret_cast<intptr_t>(continuation->entry()));
1103   }
1104 }
1105
1106
1107 void Deoptimizer::DoComputeArgumentsAdaptorFrame(TranslationIterator* iterator,
1108                                                  int frame_index) {
1109   JSFunction* function = JSFunction::cast(ComputeLiteral(iterator->Next()));
1110   unsigned height = iterator->Next();
1111   unsigned height_in_bytes = height * kPointerSize;
1112   if (trace_scope_ != NULL) {
1113     PrintF(trace_scope_->file(),
1114            "  translating arguments adaptor => height=%d\n", height_in_bytes);
1115   }
1116
1117   unsigned fixed_frame_size = ArgumentsAdaptorFrameConstants::kFrameSize;
1118   unsigned output_frame_size = height_in_bytes + fixed_frame_size;
1119
1120   // Allocate and store the output frame description.
1121   FrameDescription* output_frame =
1122       new(output_frame_size) FrameDescription(output_frame_size, function);
1123   output_frame->SetFrameType(StackFrame::ARGUMENTS_ADAPTOR);
1124
1125   // Arguments adaptor can not be topmost or bottommost.
1126   CHECK(frame_index > 0 && frame_index < output_count_ - 1);
1127   CHECK(output_[frame_index] == NULL);
1128   output_[frame_index] = output_frame;
1129
1130   // The top address of the frame is computed from the previous
1131   // frame's top and this frame's size.
1132   intptr_t top_address;
1133   top_address = output_[frame_index - 1]->GetTop() - output_frame_size;
1134   output_frame->SetTop(top_address);
1135
1136   // Compute the incoming parameter translation.
1137   int parameter_count = height;
1138   unsigned output_offset = output_frame_size;
1139   for (int i = 0; i < parameter_count; ++i) {
1140     output_offset -= kPointerSize;
1141     DoTranslateCommand(iterator, frame_index, output_offset);
1142   }
1143
1144   // Read caller's PC from the previous frame.
1145   output_offset -= kPCOnStackSize;
1146   intptr_t callers_pc = output_[frame_index - 1]->GetPc();
1147   output_frame->SetCallerPc(output_offset, callers_pc);
1148   if (trace_scope_ != NULL) {
1149     PrintF(trace_scope_->file(),
1150            "    0x%08" V8PRIxPTR ": [top + %d] <- 0x%08"
1151            V8PRIxPTR " ; caller's pc\n",
1152            top_address + output_offset, output_offset, callers_pc);
1153   }
1154
1155   // Read caller's FP from the previous frame, and set this frame's FP.
1156   output_offset -= kFPOnStackSize;
1157   intptr_t value = output_[frame_index - 1]->GetFp();
1158   output_frame->SetCallerFp(output_offset, value);
1159   intptr_t fp_value = top_address + output_offset;
1160   output_frame->SetFp(fp_value);
1161   if (trace_scope_ != NULL) {
1162     PrintF(trace_scope_->file(),
1163            "    0x%08" V8PRIxPTR ": [top + %d] <- 0x%08"
1164            V8PRIxPTR " ; caller's fp\n",
1165            fp_value, output_offset, value);
1166   }
1167
1168   if (FLAG_enable_ool_constant_pool) {
1169     // Read the caller's constant pool from the previous frame.
1170     output_offset -= kPointerSize;
1171     value = output_[frame_index - 1]->GetConstantPool();
1172     output_frame->SetCallerConstantPool(output_offset, value);
1173     if (trace_scope_) {
1174       PrintF("    0x%08" V8PRIxPTR ": [top + %d] <- 0x%08"
1175              V8PRIxPTR "; caller's constant_pool\n",
1176              top_address + output_offset, output_offset, value);
1177     }
1178   }
1179
1180   // A marker value is used in place of the context.
1181   output_offset -= kPointerSize;
1182   intptr_t context = reinterpret_cast<intptr_t>(
1183       Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR));
1184   output_frame->SetFrameSlot(output_offset, context);
1185   if (trace_scope_ != NULL) {
1186     PrintF(trace_scope_->file(),
1187            "    0x%08" V8PRIxPTR ": [top + %d] <- 0x%08"
1188            V8PRIxPTR " ; context (adaptor sentinel)\n",
1189            top_address + output_offset, output_offset, context);
1190   }
1191
1192   // The function was mentioned explicitly in the ARGUMENTS_ADAPTOR_FRAME.
1193   output_offset -= kPointerSize;
1194   value = reinterpret_cast<intptr_t>(function);
1195   output_frame->SetFrameSlot(output_offset, value);
1196   if (trace_scope_ != NULL) {
1197     PrintF(trace_scope_->file(),
1198            "    0x%08" V8PRIxPTR ": [top + %d] <- 0x%08"
1199            V8PRIxPTR " ; function\n",
1200            top_address + output_offset, output_offset, value);
1201   }
1202
1203   // Number of incoming arguments.
1204   output_offset -= kPointerSize;
1205   value = reinterpret_cast<intptr_t>(Smi::FromInt(height - 1));
1206   output_frame->SetFrameSlot(output_offset, value);
1207   if (trace_scope_ != NULL) {
1208     PrintF(trace_scope_->file(),
1209            "    0x%08" V8PRIxPTR ": [top + %d] <- 0x%08"
1210            V8PRIxPTR " ; argc (%d)\n",
1211            top_address + output_offset, output_offset, value, height - 1);
1212   }
1213
1214   DCHECK(0 == output_offset);
1215
1216   Builtins* builtins = isolate_->builtins();
1217   Code* adaptor_trampoline =
1218       builtins->builtin(Builtins::kArgumentsAdaptorTrampoline);
1219   intptr_t pc_value = reinterpret_cast<intptr_t>(
1220       adaptor_trampoline->instruction_start() +
1221       isolate_->heap()->arguments_adaptor_deopt_pc_offset()->value());
1222   output_frame->SetPc(pc_value);
1223   if (FLAG_enable_ool_constant_pool) {
1224     intptr_t constant_pool_value =
1225         reinterpret_cast<intptr_t>(adaptor_trampoline->constant_pool());
1226     output_frame->SetConstantPool(constant_pool_value);
1227   }
1228 }
1229
1230
1231 void Deoptimizer::DoComputeConstructStubFrame(TranslationIterator* iterator,
1232                                               int frame_index) {
1233   Builtins* builtins = isolate_->builtins();
1234   Code* construct_stub = builtins->builtin(Builtins::kJSConstructStubGeneric);
1235   JSFunction* function = JSFunction::cast(ComputeLiteral(iterator->Next()));
1236   unsigned height = iterator->Next();
1237   unsigned height_in_bytes = height * kPointerSize;
1238   if (trace_scope_ != NULL) {
1239     PrintF(trace_scope_->file(),
1240            "  translating construct stub => height=%d\n", height_in_bytes);
1241   }
1242
1243   unsigned fixed_frame_size = ConstructFrameConstants::kFrameSize;
1244   unsigned output_frame_size = height_in_bytes + fixed_frame_size;
1245
1246   // Allocate and store the output frame description.
1247   FrameDescription* output_frame =
1248       new(output_frame_size) FrameDescription(output_frame_size, function);
1249   output_frame->SetFrameType(StackFrame::CONSTRUCT);
1250
1251   // Construct stub can not be topmost or bottommost.
1252   DCHECK(frame_index > 0 && frame_index < output_count_ - 1);
1253   DCHECK(output_[frame_index] == NULL);
1254   output_[frame_index] = output_frame;
1255
1256   // The top address of the frame is computed from the previous
1257   // frame's top and this frame's size.
1258   intptr_t top_address;
1259   top_address = output_[frame_index - 1]->GetTop() - output_frame_size;
1260   output_frame->SetTop(top_address);
1261
1262   // Compute the incoming parameter translation.
1263   int parameter_count = height;
1264   unsigned output_offset = output_frame_size;
1265   for (int i = 0; i < parameter_count; ++i) {
1266     output_offset -= kPointerSize;
1267     int deferred_object_index = deferred_objects_.length();
1268     DoTranslateCommand(iterator, frame_index, output_offset);
1269     // The allocated receiver of a construct stub frame is passed as the
1270     // receiver parameter through the translation. It might be encoding
1271     // a captured object, patch the slot address for a captured object.
1272     if (i == 0 && deferred_objects_.length() > deferred_object_index) {
1273       CHECK(!deferred_objects_[deferred_object_index].is_arguments());
1274       deferred_objects_[deferred_object_index].patch_slot_address(top_address);
1275     }
1276   }
1277
1278   // Read caller's PC from the previous frame.
1279   output_offset -= kPCOnStackSize;
1280   intptr_t callers_pc = output_[frame_index - 1]->GetPc();
1281   output_frame->SetCallerPc(output_offset, callers_pc);
1282   if (trace_scope_ != NULL) {
1283     PrintF(trace_scope_->file(),
1284            "    0x%08" V8PRIxPTR ": [top + %d] <- 0x%08"
1285            V8PRIxPTR " ; caller's pc\n",
1286            top_address + output_offset, output_offset, callers_pc);
1287   }
1288
1289   // Read caller's FP from the previous frame, and set this frame's FP.
1290   output_offset -= kFPOnStackSize;
1291   intptr_t value = output_[frame_index - 1]->GetFp();
1292   output_frame->SetCallerFp(output_offset, value);
1293   intptr_t fp_value = top_address + output_offset;
1294   output_frame->SetFp(fp_value);
1295   if (trace_scope_ != NULL) {
1296     PrintF(trace_scope_->file(),
1297            "    0x%08" V8PRIxPTR ": [top + %d] <- 0x%08"
1298            V8PRIxPTR " ; caller's fp\n",
1299            fp_value, output_offset, value);
1300   }
1301
1302   if (FLAG_enable_ool_constant_pool) {
1303     // Read the caller's constant pool from the previous frame.
1304     output_offset -= kPointerSize;
1305     value = output_[frame_index - 1]->GetConstantPool();
1306     output_frame->SetCallerConstantPool(output_offset, value);
1307     if (trace_scope_) {
1308       PrintF("    0x%08" V8PRIxPTR ": [top + %d] <- 0x%08"
1309              V8PRIxPTR " ; caller's constant pool\n",
1310              top_address + output_offset, output_offset, value);
1311     }
1312   }
1313
1314   // The context can be gotten from the previous frame.
1315   output_offset -= kPointerSize;
1316   value = output_[frame_index - 1]->GetContext();
1317   output_frame->SetFrameSlot(output_offset, value);
1318   if (trace_scope_ != NULL) {
1319     PrintF(trace_scope_->file(),
1320            "    0x%08" V8PRIxPTR ": [top + %d] <- 0x%08"
1321            V8PRIxPTR " ; context\n",
1322            top_address + output_offset, output_offset, value);
1323   }
1324
1325   // A marker value is used in place of the function.
1326   output_offset -= kPointerSize;
1327   value = reinterpret_cast<intptr_t>(Smi::FromInt(StackFrame::CONSTRUCT));
1328   output_frame->SetFrameSlot(output_offset, value);
1329   if (trace_scope_ != NULL) {
1330     PrintF(trace_scope_->file(),
1331            "    0x%08" V8PRIxPTR ": [top + %d] <- 0x%08"
1332            V8PRIxPTR " ; function (construct sentinel)\n",
1333            top_address + output_offset, output_offset, value);
1334   }
1335
1336   // The output frame reflects a JSConstructStubGeneric frame.
1337   output_offset -= kPointerSize;
1338   value = reinterpret_cast<intptr_t>(construct_stub);
1339   output_frame->SetFrameSlot(output_offset, value);
1340   if (trace_scope_ != NULL) {
1341     PrintF(trace_scope_->file(),
1342            "    0x%08" V8PRIxPTR ": [top + %d] <- 0x%08"
1343            V8PRIxPTR " ; code object\n",
1344            top_address + output_offset, output_offset, value);
1345   }
1346
1347   // Number of incoming arguments.
1348   output_offset -= kPointerSize;
1349   value = reinterpret_cast<intptr_t>(Smi::FromInt(height - 1));
1350   output_frame->SetFrameSlot(output_offset, value);
1351   if (trace_scope_ != NULL) {
1352     PrintF(trace_scope_->file(),
1353            "    0x%08" V8PRIxPTR ": [top + %d] <- 0x%08"
1354            V8PRIxPTR " ; argc (%d)\n",
1355            top_address + output_offset, output_offset, value, height - 1);
1356   }
1357
1358   // Constructor function being invoked by the stub (only present on some
1359   // architectures, indicated by kConstructorOffset).
1360   if (ConstructFrameConstants::kConstructorOffset != kMinInt) {
1361     output_offset -= kPointerSize;
1362     value = reinterpret_cast<intptr_t>(function);
1363     output_frame->SetFrameSlot(output_offset, value);
1364     if (trace_scope_ != NULL) {
1365       PrintF(trace_scope_->file(),
1366              "    0x%08" V8PRIxPTR ": [top + %d] <- 0x%08"
1367              V8PRIxPTR " ; constructor function\n",
1368              top_address + output_offset, output_offset, value);
1369     }
1370   }
1371
1372   // The newly allocated object was passed as receiver in the artificial
1373   // constructor stub environment created by HEnvironment::CopyForInlining().
1374   output_offset -= kPointerSize;
1375   value = output_frame->GetFrameSlot(output_frame_size - kPointerSize);
1376   output_frame->SetFrameSlot(output_offset, value);
1377   if (trace_scope_ != NULL) {
1378     PrintF(trace_scope_->file(),
1379            "    0x%08" V8PRIxPTR ": [top + %d] <- 0x%08"
1380            V8PRIxPTR " ; allocated receiver\n",
1381            top_address + output_offset, output_offset, value);
1382   }
1383
1384   CHECK_EQ(0u, output_offset);
1385
1386   intptr_t pc = reinterpret_cast<intptr_t>(
1387       construct_stub->instruction_start() +
1388       isolate_->heap()->construct_stub_deopt_pc_offset()->value());
1389   output_frame->SetPc(pc);
1390   if (FLAG_enable_ool_constant_pool) {
1391     intptr_t constant_pool_value =
1392         reinterpret_cast<intptr_t>(construct_stub->constant_pool());
1393     output_frame->SetConstantPool(constant_pool_value);
1394   }
1395 }
1396
1397
1398 void Deoptimizer::DoComputeAccessorStubFrame(TranslationIterator* iterator,
1399                                              int frame_index,
1400                                              bool is_setter_stub_frame) {
1401   JSFunction* accessor = JSFunction::cast(ComputeLiteral(iterator->Next()));
1402   // The receiver (and the implicit return value, if any) are expected in
1403   // registers by the LoadIC/StoreIC, so they don't belong to the output stack
1404   // frame. This means that we have to use a height of 0.
1405   unsigned height = 0;
1406   unsigned height_in_bytes = height * kPointerSize;
1407   const char* kind = is_setter_stub_frame ? "setter" : "getter";
1408   if (trace_scope_ != NULL) {
1409     PrintF(trace_scope_->file(),
1410            "  translating %s stub => height=%u\n", kind, height_in_bytes);
1411   }
1412
1413   // We need 1 stack entry for the return address and enough entries for the
1414   // StackFrame::INTERNAL (FP, context, frame type, code object and constant
1415   // pool (if FLAG_enable_ool_constant_pool)- see MacroAssembler::EnterFrame).
1416   // For a setter stub frame we need one additional entry for the implicit
1417   // return value, see StoreStubCompiler::CompileStoreViaSetter.
1418   unsigned fixed_frame_entries =
1419       (StandardFrameConstants::kFixedFrameSize / kPointerSize) + 1 +
1420       (is_setter_stub_frame ? 1 : 0);
1421   unsigned fixed_frame_size = fixed_frame_entries * kPointerSize;
1422   unsigned output_frame_size = height_in_bytes + fixed_frame_size;
1423
1424   // Allocate and store the output frame description.
1425   FrameDescription* output_frame =
1426       new(output_frame_size) FrameDescription(output_frame_size, accessor);
1427   output_frame->SetFrameType(StackFrame::INTERNAL);
1428
1429   // A frame for an accessor stub can not be the topmost or bottommost one.
1430   CHECK(frame_index > 0 && frame_index < output_count_ - 1);
1431   CHECK_NULL(output_[frame_index]);
1432   output_[frame_index] = output_frame;
1433
1434   // The top address of the frame is computed from the previous frame's top and
1435   // this frame's size.
1436   intptr_t top_address = output_[frame_index - 1]->GetTop() - output_frame_size;
1437   output_frame->SetTop(top_address);
1438
1439   unsigned output_offset = output_frame_size;
1440
1441   // Read caller's PC from the previous frame.
1442   output_offset -= kPCOnStackSize;
1443   intptr_t callers_pc = output_[frame_index - 1]->GetPc();
1444   output_frame->SetCallerPc(output_offset, callers_pc);
1445   if (trace_scope_ != NULL) {
1446     PrintF(trace_scope_->file(),
1447            "    0x%08" V8PRIxPTR ": [top + %u] <- 0x%08" V8PRIxPTR
1448            " ; caller's pc\n",
1449            top_address + output_offset, output_offset, callers_pc);
1450   }
1451
1452   // Read caller's FP from the previous frame, and set this frame's FP.
1453   output_offset -= kFPOnStackSize;
1454   intptr_t value = output_[frame_index - 1]->GetFp();
1455   output_frame->SetCallerFp(output_offset, value);
1456   intptr_t fp_value = top_address + output_offset;
1457   output_frame->SetFp(fp_value);
1458   if (trace_scope_ != NULL) {
1459     PrintF(trace_scope_->file(),
1460            "    0x%08" V8PRIxPTR ": [top + %u] <- 0x%08" V8PRIxPTR
1461            " ; caller's fp\n",
1462            fp_value, output_offset, value);
1463   }
1464
1465   if (FLAG_enable_ool_constant_pool) {
1466     // Read the caller's constant pool from the previous frame.
1467     output_offset -= kPointerSize;
1468     value = output_[frame_index - 1]->GetConstantPool();
1469     output_frame->SetCallerConstantPool(output_offset, value);
1470     if (trace_scope_) {
1471       PrintF("    0x%08" V8PRIxPTR ": [top + %d] <- 0x%08"
1472              V8PRIxPTR " ; caller's constant pool\n",
1473              top_address + output_offset, output_offset, value);
1474     }
1475   }
1476
1477   // The context can be gotten from the previous frame.
1478   output_offset -= kPointerSize;
1479   value = output_[frame_index - 1]->GetContext();
1480   output_frame->SetFrameSlot(output_offset, value);
1481   if (trace_scope_ != NULL) {
1482     PrintF(trace_scope_->file(),
1483            "    0x%08" V8PRIxPTR ": [top + %u] <- 0x%08" V8PRIxPTR
1484            " ; context\n",
1485            top_address + output_offset, output_offset, value);
1486   }
1487
1488   // A marker value is used in place of the function.
1489   output_offset -= kPointerSize;
1490   value = reinterpret_cast<intptr_t>(Smi::FromInt(StackFrame::INTERNAL));
1491   output_frame->SetFrameSlot(output_offset, value);
1492   if (trace_scope_ != NULL) {
1493     PrintF(trace_scope_->file(),
1494            "    0x%08" V8PRIxPTR ": [top + %u] <- 0x%08" V8PRIxPTR
1495            " ; function (%s sentinel)\n",
1496            top_address + output_offset, output_offset, value, kind);
1497   }
1498
1499   // Get Code object from accessor stub.
1500   output_offset -= kPointerSize;
1501   Builtins::Name name = is_setter_stub_frame ?
1502       Builtins::kStoreIC_Setter_ForDeopt :
1503       Builtins::kLoadIC_Getter_ForDeopt;
1504   Code* accessor_stub = isolate_->builtins()->builtin(name);
1505   value = reinterpret_cast<intptr_t>(accessor_stub);
1506   output_frame->SetFrameSlot(output_offset, value);
1507   if (trace_scope_ != NULL) {
1508     PrintF(trace_scope_->file(),
1509            "    0x%08" V8PRIxPTR ": [top + %u] <- 0x%08" V8PRIxPTR
1510            " ; code object\n",
1511            top_address + output_offset, output_offset, value);
1512   }
1513
1514   // Skip receiver.
1515   DoTranslateObjectAndSkip(iterator);
1516
1517   if (is_setter_stub_frame) {
1518     // The implicit return value was part of the artificial setter stub
1519     // environment.
1520     output_offset -= kPointerSize;
1521     DoTranslateCommand(iterator, frame_index, output_offset);
1522   }
1523
1524   CHECK_EQ(0u, output_offset);
1525
1526   Smi* offset = is_setter_stub_frame ?
1527       isolate_->heap()->setter_stub_deopt_pc_offset() :
1528       isolate_->heap()->getter_stub_deopt_pc_offset();
1529   intptr_t pc = reinterpret_cast<intptr_t>(
1530       accessor_stub->instruction_start() + offset->value());
1531   output_frame->SetPc(pc);
1532   if (FLAG_enable_ool_constant_pool) {
1533     intptr_t constant_pool_value =
1534         reinterpret_cast<intptr_t>(accessor_stub->constant_pool());
1535     output_frame->SetConstantPool(constant_pool_value);
1536   }
1537 }
1538
1539
1540 void Deoptimizer::DoComputeCompiledStubFrame(TranslationIterator* iterator,
1541                                              int frame_index) {
1542   //
1543   //               FROM                                  TO
1544   //    |          ....           |          |          ....           |
1545   //    +-------------------------+          +-------------------------+
1546   //    | JSFunction continuation |          | JSFunction continuation |
1547   //    +-------------------------+          +-------------------------+
1548   // |  |    saved frame (FP)     |          |    saved frame (FP)     |
1549   // |  +=========================+<-fpreg   +=========================+<-fpreg
1550   // |  |constant pool (if ool_cp)|          |constant pool (if ool_cp)|
1551   // |  +-------------------------+          +-------------------------|
1552   // |  |   JSFunction context    |          |   JSFunction context    |
1553   // v  +-------------------------+          +-------------------------|
1554   //    |   COMPILED_STUB marker  |          |   STUB_FAILURE marker   |
1555   //    +-------------------------+          +-------------------------+
1556   //    |                         |          |  caller args.arguments_ |
1557   //    | ...                     |          +-------------------------+
1558   //    |                         |          |  caller args.length_    |
1559   //    |-------------------------|<-spreg   +-------------------------+
1560   //                                         |  caller args pointer    |
1561   //                                         +-------------------------+
1562   //                                         |  caller stack param 1   |
1563   //      parameters in registers            +-------------------------+
1564   //       and spilled to stack              |           ....          |
1565   //                                         +-------------------------+
1566   //                                         |  caller stack param n   |
1567   //                                         +-------------------------+<-spreg
1568   //                                         reg = number of parameters
1569   //                                         reg = failure handler address
1570   //                                         reg = saved frame
1571   //                                         reg = JSFunction context
1572   //
1573
1574   CHECK(compiled_code_->is_hydrogen_stub());
1575   int major_key = CodeStub::GetMajorKey(compiled_code_);
1576   CodeStubDescriptor descriptor(isolate_, compiled_code_->stub_key());
1577
1578   // The output frame must have room for all pushed register parameters
1579   // and the standard stack frame slots.  Include space for an argument
1580   // object to the callee and optionally the space to pass the argument
1581   // object to the stub failure handler.
1582   int param_count = descriptor.GetEnvironmentParameterCount();
1583   CHECK_GE(param_count, 0);
1584
1585   int height_in_bytes = kPointerSize * param_count + sizeof(Arguments) +
1586       kPointerSize;
1587   int fixed_frame_size = StandardFrameConstants::kFixedFrameSize;
1588   int input_frame_size = input_->GetFrameSize();
1589   int output_frame_size = height_in_bytes + fixed_frame_size;
1590   if (trace_scope_ != NULL) {
1591     PrintF(trace_scope_->file(),
1592            "  translating %s => StubFailureTrampolineStub, height=%d\n",
1593            CodeStub::MajorName(static_cast<CodeStub::Major>(major_key), false),
1594            height_in_bytes);
1595   }
1596
1597   // The stub failure trampoline is a single frame.
1598   FrameDescription* output_frame =
1599       new(output_frame_size) FrameDescription(output_frame_size, NULL);
1600   output_frame->SetFrameType(StackFrame::STUB_FAILURE_TRAMPOLINE);
1601   CHECK_EQ(frame_index, 0);
1602   output_[frame_index] = output_frame;
1603
1604   // The top address for the output frame can be computed from the input
1605   // frame pointer and the output frame's height. Subtract space for the
1606   // context and function slots.
1607   Register fp_reg = StubFailureTrampolineFrame::fp_register();
1608   intptr_t top_address = input_->GetRegister(fp_reg.code()) -
1609       StandardFrameConstants::kFixedFrameSizeFromFp - height_in_bytes;
1610   output_frame->SetTop(top_address);
1611
1612   // Read caller's PC (JSFunction continuation) from the input frame.
1613   unsigned input_frame_offset = input_frame_size - kPCOnStackSize;
1614   unsigned output_frame_offset = output_frame_size - kFPOnStackSize;
1615   intptr_t value = input_->GetFrameSlot(input_frame_offset);
1616   output_frame->SetCallerPc(output_frame_offset, value);
1617   if (trace_scope_ != NULL) {
1618     PrintF(trace_scope_->file(),
1619            "    0x%08" V8PRIxPTR ": [top + %d] <- 0x%08"
1620            V8PRIxPTR " ; caller's pc\n",
1621            top_address + output_frame_offset, output_frame_offset, value);
1622   }
1623
1624   // Read caller's FP from the input frame, and set this frame's FP.
1625   input_frame_offset -= kFPOnStackSize;
1626   value = input_->GetFrameSlot(input_frame_offset);
1627   output_frame_offset -= kFPOnStackSize;
1628   output_frame->SetCallerFp(output_frame_offset, value);
1629   intptr_t frame_ptr = input_->GetRegister(fp_reg.code());
1630   output_frame->SetRegister(fp_reg.code(), frame_ptr);
1631   output_frame->SetFp(frame_ptr);
1632   if (trace_scope_ != NULL) {
1633     PrintF(trace_scope_->file(),
1634            "    0x%08" V8PRIxPTR ": [top + %d] <- 0x%08"
1635            V8PRIxPTR " ; caller's fp\n",
1636            top_address + output_frame_offset, output_frame_offset, value);
1637   }
1638
1639   if (FLAG_enable_ool_constant_pool) {
1640     // Read the caller's constant pool from the input frame.
1641     input_frame_offset -= kPointerSize;
1642     value = input_->GetFrameSlot(input_frame_offset);
1643     output_frame_offset -= kPointerSize;
1644     output_frame->SetCallerConstantPool(output_frame_offset, value);
1645     if (trace_scope_) {
1646       PrintF("    0x%08" V8PRIxPTR ": [top + %d] <- 0x%08"
1647              V8PRIxPTR " ; caller's constant_pool\n",
1648              top_address + output_frame_offset, output_frame_offset, value);
1649     }
1650   }
1651
1652   // The context can be gotten from the input frame.
1653   Register context_reg = StubFailureTrampolineFrame::context_register();
1654   input_frame_offset -= kPointerSize;
1655   value = input_->GetFrameSlot(input_frame_offset);
1656   output_frame->SetRegister(context_reg.code(), value);
1657   output_frame_offset -= kPointerSize;
1658   output_frame->SetFrameSlot(output_frame_offset, value);
1659   CHECK(reinterpret_cast<Object*>(value)->IsContext());
1660   if (trace_scope_ != NULL) {
1661     PrintF(trace_scope_->file(),
1662            "    0x%08" V8PRIxPTR ": [top + %d] <- 0x%08"
1663            V8PRIxPTR " ; context\n",
1664            top_address + output_frame_offset, output_frame_offset, value);
1665   }
1666
1667   // A marker value is used in place of the function.
1668   output_frame_offset -= kPointerSize;
1669   value = reinterpret_cast<intptr_t>(
1670       Smi::FromInt(StackFrame::STUB_FAILURE_TRAMPOLINE));
1671   output_frame->SetFrameSlot(output_frame_offset, value);
1672   if (trace_scope_ != NULL) {
1673     PrintF(trace_scope_->file(),
1674            "    0x%08" V8PRIxPTR ": [top + %d] <- 0x%08"
1675            V8PRIxPTR " ; function (stub failure sentinel)\n",
1676            top_address + output_frame_offset, output_frame_offset, value);
1677   }
1678
1679   intptr_t caller_arg_count = 0;
1680   bool arg_count_known = !descriptor.stack_parameter_count().is_valid();
1681
1682   // Build the Arguments object for the caller's parameters and a pointer to it.
1683   output_frame_offset -= kPointerSize;
1684   int args_arguments_offset = output_frame_offset;
1685   intptr_t the_hole = reinterpret_cast<intptr_t>(
1686       isolate_->heap()->the_hole_value());
1687   if (arg_count_known) {
1688     value = frame_ptr + StandardFrameConstants::kCallerSPOffset +
1689         (caller_arg_count - 1) * kPointerSize;
1690   } else {
1691     value = the_hole;
1692   }
1693
1694   output_frame->SetFrameSlot(args_arguments_offset, value);
1695   if (trace_scope_ != NULL) {
1696     PrintF(trace_scope_->file(),
1697            "    0x%08" V8PRIxPTR ": [top + %d] <- 0x%08"
1698            V8PRIxPTR " ; args.arguments %s\n",
1699            top_address + args_arguments_offset, args_arguments_offset, value,
1700            arg_count_known ? "" : "(the hole)");
1701   }
1702
1703   output_frame_offset -= kPointerSize;
1704   int length_frame_offset = output_frame_offset;
1705   value = arg_count_known ? caller_arg_count : the_hole;
1706   output_frame->SetFrameSlot(length_frame_offset, value);
1707   if (trace_scope_ != NULL) {
1708     PrintF(trace_scope_->file(),
1709            "    0x%08" V8PRIxPTR ": [top + %d] <- 0x%08"
1710            V8PRIxPTR " ; args.length %s\n",
1711            top_address + length_frame_offset, length_frame_offset, value,
1712            arg_count_known ? "" : "(the hole)");
1713   }
1714
1715   output_frame_offset -= kPointerSize;
1716   value = frame_ptr + StandardFrameConstants::kCallerSPOffset -
1717       (output_frame_size - output_frame_offset) + kPointerSize;
1718   output_frame->SetFrameSlot(output_frame_offset, value);
1719   if (trace_scope_ != NULL) {
1720     PrintF(trace_scope_->file(),
1721            "    0x%08" V8PRIxPTR ": [top + %d] <- 0x%08"
1722            V8PRIxPTR " ; args*\n",
1723            top_address + output_frame_offset, output_frame_offset, value);
1724   }
1725
1726   // Copy the register parameters to the failure frame.
1727   int arguments_length_offset = -1;
1728   for (int i = 0; i < param_count; ++i) {
1729     output_frame_offset -= kPointerSize;
1730     DoTranslateCommand(iterator, 0, output_frame_offset);
1731
1732     if (!arg_count_known && descriptor.IsEnvironmentParameterCountRegister(i)) {
1733       arguments_length_offset = output_frame_offset;
1734     }
1735   }
1736
1737   CHECK_EQ(0u, output_frame_offset);
1738
1739   if (!arg_count_known) {
1740     CHECK_GE(arguments_length_offset, 0);
1741     // We know it's a smi because 1) the code stub guarantees the stack
1742     // parameter count is in smi range, and 2) the DoTranslateCommand in the
1743     // parameter loop above translated that to a tagged value.
1744     Smi* smi_caller_arg_count = reinterpret_cast<Smi*>(
1745         output_frame->GetFrameSlot(arguments_length_offset));
1746     caller_arg_count = smi_caller_arg_count->value();
1747     output_frame->SetFrameSlot(length_frame_offset, caller_arg_count);
1748     if (trace_scope_ != NULL) {
1749       PrintF(trace_scope_->file(),
1750              "    0x%08" V8PRIxPTR ": [top + %d] <- 0x%08"
1751              V8PRIxPTR " ; args.length\n",
1752              top_address + length_frame_offset, length_frame_offset,
1753              caller_arg_count);
1754     }
1755     value = frame_ptr + StandardFrameConstants::kCallerSPOffset +
1756         (caller_arg_count - 1) * kPointerSize;
1757     output_frame->SetFrameSlot(args_arguments_offset, value);
1758     if (trace_scope_ != NULL) {
1759       PrintF(trace_scope_->file(),
1760              "    0x%08" V8PRIxPTR ": [top + %d] <- 0x%08"
1761              V8PRIxPTR " ; args.arguments\n",
1762              top_address + args_arguments_offset, args_arguments_offset,
1763              value);
1764     }
1765   }
1766
1767   // Copy the double registers from the input into the output frame.
1768   CopyDoubleRegisters(output_frame);
1769
1770   // Fill registers containing handler and number of parameters.
1771   SetPlatformCompiledStubRegisters(output_frame, &descriptor);
1772
1773   // Compute this frame's PC, state, and continuation.
1774   Code* trampoline = NULL;
1775   StubFunctionMode function_mode = descriptor.function_mode();
1776   StubFailureTrampolineStub(isolate_,
1777                             function_mode).FindCodeInCache(&trampoline);
1778   DCHECK(trampoline != NULL);
1779   output_frame->SetPc(reinterpret_cast<intptr_t>(
1780       trampoline->instruction_start()));
1781   if (FLAG_enable_ool_constant_pool) {
1782     Register constant_pool_reg =
1783         StubFailureTrampolineFrame::constant_pool_pointer_register();
1784     intptr_t constant_pool_value =
1785         reinterpret_cast<intptr_t>(trampoline->constant_pool());
1786     output_frame->SetConstantPool(constant_pool_value);
1787     output_frame->SetRegister(constant_pool_reg.code(), constant_pool_value);
1788   }
1789   output_frame->SetState(Smi::FromInt(FullCodeGenerator::NO_REGISTERS));
1790   Code* notify_failure =
1791       isolate_->builtins()->builtin(Builtins::kNotifyStubFailureSaveDoubles);
1792   output_frame->SetContinuation(
1793       reinterpret_cast<intptr_t>(notify_failure->entry()));
1794 }
1795
1796
1797 Handle<Object> Deoptimizer::MaterializeNextHeapObject() {
1798   int object_index = materialization_object_index_++;
1799   ObjectMaterializationDescriptor desc = deferred_objects_[object_index];
1800   const int length = desc.object_length();
1801
1802   if (desc.duplicate_object() >= 0) {
1803     // Found a previously materialized object by de-duplication.
1804     object_index = desc.duplicate_object();
1805     materialized_objects_->Add(Handle<Object>());
1806   } else if (desc.is_arguments() && ArgumentsObjectIsAdapted(object_index)) {
1807     // Use the arguments adapter frame we just built to materialize the
1808     // arguments object. FunctionGetArguments can't throw an exception.
1809     Handle<JSFunction> function = ArgumentsObjectFunction(object_index);
1810     Handle<JSObject> arguments = Handle<JSObject>::cast(
1811         Accessors::FunctionGetArguments(function));
1812     materialized_objects_->Add(arguments);
1813     // To keep consistent object counters, we still materialize the
1814     // nested values (but we throw them away).
1815     for (int i = 0; i < length; ++i) {
1816       MaterializeNextValue();
1817     }
1818   } else if (desc.is_arguments()) {
1819     // Construct an arguments object and copy the parameters to a newly
1820     // allocated arguments object backing store.
1821     Handle<JSFunction> function = ArgumentsObjectFunction(object_index);
1822     Handle<JSObject> arguments =
1823         isolate_->factory()->NewArgumentsObject(function, length);
1824     Handle<FixedArray> array = isolate_->factory()->NewFixedArray(length);
1825     DCHECK_EQ(array->length(), length);
1826     arguments->set_elements(*array);
1827     materialized_objects_->Add(arguments);
1828     for (int i = 0; i < length; ++i) {
1829       Handle<Object> value = MaterializeNextValue();
1830       array->set(i, *value);
1831     }
1832   } else {
1833     // Dispatch on the instance type of the object to be materialized.
1834     // We also need to make sure that the representation of all fields
1835     // in the given object are general enough to hold a tagged value.
1836     Handle<Map> map = Map::GeneralizeAllFieldRepresentations(
1837         Handle<Map>::cast(MaterializeNextValue()));
1838     switch (map->instance_type()) {
1839       case MUTABLE_HEAP_NUMBER_TYPE:
1840       case HEAP_NUMBER_TYPE: {
1841         // Reuse the HeapNumber value directly as it is already properly
1842         // tagged and skip materializing the HeapNumber explicitly. Turn mutable
1843         // heap numbers immutable.
1844         Handle<Object> object = MaterializeNextValue();
1845         if (object_index < prev_materialized_count_) {
1846           materialized_objects_->Add(Handle<Object>(
1847               previously_materialized_objects_->get(object_index), isolate_));
1848         } else {
1849           materialized_objects_->Add(object);
1850         }
1851         materialization_value_index_ += kDoubleSize / kPointerSize - 1;
1852         break;
1853       }
1854       case JS_OBJECT_TYPE: {
1855         Handle<JSObject> object =
1856             isolate_->factory()->NewJSObjectFromMap(map, NOT_TENURED, false);
1857         if (object_index < prev_materialized_count_) {
1858           materialized_objects_->Add(Handle<Object>(
1859               previously_materialized_objects_->get(object_index), isolate_));
1860         } else {
1861           materialized_objects_->Add(object);
1862         }
1863         Handle<Object> properties = MaterializeNextValue();
1864         Handle<Object> elements = MaterializeNextValue();
1865         object->set_properties(FixedArray::cast(*properties));
1866         object->set_elements(FixedArrayBase::cast(*elements));
1867         for (int i = 0; i < length - 3; ++i) {
1868           Handle<Object> value = MaterializeNextValue();
1869           FieldIndex index = FieldIndex::ForPropertyIndex(object->map(), i);
1870           object->FastPropertyAtPut(index, *value);
1871         }
1872         break;
1873       }
1874       case JS_ARRAY_TYPE: {
1875         Handle<JSArray> object =
1876             isolate_->factory()->NewJSArray(0, map->elements_kind());
1877         if (object_index < prev_materialized_count_) {
1878           materialized_objects_->Add(Handle<Object>(
1879               previously_materialized_objects_->get(object_index), isolate_));
1880         } else {
1881           materialized_objects_->Add(object);
1882         }
1883         Handle<Object> properties = MaterializeNextValue();
1884         Handle<Object> elements = MaterializeNextValue();
1885         Handle<Object> length = MaterializeNextValue();
1886         object->set_properties(FixedArray::cast(*properties));
1887         object->set_elements(FixedArrayBase::cast(*elements));
1888         object->set_length(*length);
1889         break;
1890       }
1891       default:
1892         PrintF(stderr,
1893                "[couldn't handle instance type %d]\n", map->instance_type());
1894         FATAL("Unsupported instance type");
1895     }
1896   }
1897
1898   return materialized_objects_->at(object_index);
1899 }
1900
1901
1902 Handle<Object> Deoptimizer::MaterializeNextValue() {
1903   int value_index = materialization_value_index_++;
1904   Handle<Object> value = materialized_values_->at(value_index);
1905   if (value->IsMutableHeapNumber()) {
1906     HeapNumber::cast(*value)->set_map(isolate_->heap()->heap_number_map());
1907   }
1908   if (*value == isolate_->heap()->arguments_marker()) {
1909     value = MaterializeNextHeapObject();
1910   }
1911   return value;
1912 }
1913
1914
1915 void Deoptimizer::MaterializeHeapObjects(JavaScriptFrameIterator* it) {
1916   DCHECK_NE(DEBUGGER, bailout_type_);
1917
1918   MaterializedObjectStore* materialized_store =
1919       isolate_->materialized_object_store();
1920   previously_materialized_objects_ = materialized_store->Get(stack_fp_);
1921   prev_materialized_count_ = previously_materialized_objects_.is_null() ?
1922       0 : previously_materialized_objects_->length();
1923
1924   // Walk all JavaScript output frames with the given frame iterator.
1925   for (int frame_index = 0; frame_index < jsframe_count(); ++frame_index) {
1926     if (frame_index != 0) it->Advance();
1927     JavaScriptFrame* frame = it->frame();
1928     jsframe_functions_.Add(handle(frame->function(), isolate_));
1929     jsframe_has_adapted_arguments_.Add(frame->has_adapted_arguments());
1930   }
1931
1932   // Handlify all tagged object values before triggering any allocation.
1933   List<Handle<Object> > values(deferred_objects_tagged_values_.length());
1934   for (int i = 0; i < deferred_objects_tagged_values_.length(); ++i) {
1935     values.Add(Handle<Object>(deferred_objects_tagged_values_[i], isolate_));
1936   }
1937
1938   // Play it safe and clear all unhandlified values before we continue.
1939   deferred_objects_tagged_values_.Clear();
1940
1941   // Materialize all heap numbers before looking at arguments because when the
1942   // output frames are used to materialize arguments objects later on they need
1943   // to already contain valid heap numbers.
1944   for (int i = 0; i < deferred_heap_numbers_.length(); i++) {
1945     HeapNumberMaterializationDescriptor<Address> d = deferred_heap_numbers_[i];
1946     Handle<Object> num = isolate_->factory()->NewNumber(d.value());
1947     if (trace_scope_ != NULL) {
1948       PrintF(trace_scope_->file(),
1949              "Materialized a new heap number %p [%e] in slot %p\n",
1950              reinterpret_cast<void*>(*num),
1951              d.value(),
1952              d.destination());
1953     }
1954     Memory::Object_at(d.destination()) = *num;
1955   }
1956
1957   // Materialize all heap numbers required for arguments/captured objects.
1958   for (int i = 0; i < deferred_objects_double_values_.length(); i++) {
1959     HeapNumberMaterializationDescriptor<int> d =
1960         deferred_objects_double_values_[i];
1961     Handle<Object> num = isolate_->factory()->NewNumber(d.value());
1962     if (trace_scope_ != NULL) {
1963       PrintF(trace_scope_->file(),
1964              "Materialized a new heap number %p [%e] for object at %d\n",
1965              reinterpret_cast<void*>(*num),
1966              d.value(),
1967              d.destination());
1968     }
1969     DCHECK(values.at(d.destination())->IsTheHole());
1970     values.Set(d.destination(), num);
1971   }
1972
1973   // Play it safe and clear all object double values before we continue.
1974   deferred_objects_double_values_.Clear();
1975
1976   // Materialize arguments/captured objects.
1977   if (!deferred_objects_.is_empty()) {
1978     List<Handle<Object> > materialized_objects(deferred_objects_.length());
1979     materialized_objects_ = &materialized_objects;
1980     materialized_values_ = &values;
1981
1982     while (materialization_object_index_ < deferred_objects_.length()) {
1983       int object_index = materialization_object_index_;
1984       ObjectMaterializationDescriptor descriptor =
1985           deferred_objects_.at(object_index);
1986
1987       // Find a previously materialized object by de-duplication or
1988       // materialize a new instance of the object if necessary. Store
1989       // the materialized object into the frame slot.
1990       Handle<Object> object = MaterializeNextHeapObject();
1991       if (descriptor.slot_address() != NULL) {
1992         Memory::Object_at(descriptor.slot_address()) = *object;
1993       }
1994       if (trace_scope_ != NULL) {
1995         if (descriptor.is_arguments()) {
1996           PrintF(trace_scope_->file(),
1997                  "Materialized %sarguments object of length %d for %p: ",
1998                  ArgumentsObjectIsAdapted(object_index) ? "(adapted) " : "",
1999                  Handle<JSObject>::cast(object)->elements()->length(),
2000                  reinterpret_cast<void*>(descriptor.slot_address()));
2001         } else {
2002           PrintF(trace_scope_->file(),
2003                  "Materialized captured object of size %d for %p: ",
2004                  Handle<HeapObject>::cast(object)->Size(),
2005                  reinterpret_cast<void*>(descriptor.slot_address()));
2006         }
2007         object->ShortPrint(trace_scope_->file());
2008         PrintF(trace_scope_->file(), "\n");
2009       }
2010     }
2011
2012     CHECK_EQ(materialization_object_index_, materialized_objects_->length());
2013     CHECK_EQ(materialization_value_index_, materialized_values_->length());
2014   }
2015
2016   if (prev_materialized_count_ > 0) {
2017     materialized_store->Remove(stack_fp_);
2018   }
2019 }
2020
2021
2022 void Deoptimizer::MaterializeHeapNumbersForDebuggerInspectableFrame(
2023     Address parameters_top,
2024     uint32_t parameters_size,
2025     Address expressions_top,
2026     uint32_t expressions_size,
2027     DeoptimizedFrameInfo* info) {
2028   CHECK_EQ(DEBUGGER, bailout_type_);
2029   Address parameters_bottom = parameters_top + parameters_size;
2030   Address expressions_bottom = expressions_top + expressions_size;
2031   for (int i = 0; i < deferred_heap_numbers_.length(); i++) {
2032     HeapNumberMaterializationDescriptor<Address> d = deferred_heap_numbers_[i];
2033
2034     // Check of the heap number to materialize actually belong to the frame
2035     // being extracted.
2036     Address slot = d.destination();
2037     if (parameters_top <= slot && slot < parameters_bottom) {
2038       Handle<Object> num = isolate_->factory()->NewNumber(d.value());
2039
2040       int index = (info->parameters_count() - 1) -
2041           static_cast<int>(slot - parameters_top) / kPointerSize;
2042
2043       if (trace_scope_ != NULL) {
2044         PrintF(trace_scope_->file(),
2045                "Materializing a new heap number %p [%e] in slot %p"
2046                "for parameter slot #%d\n",
2047                reinterpret_cast<void*>(*num),
2048                d.value(),
2049                d.destination(),
2050                index);
2051       }
2052
2053       info->SetParameter(index, *num);
2054     } else if (expressions_top <= slot && slot < expressions_bottom) {
2055       Handle<Object> num = isolate_->factory()->NewNumber(d.value());
2056
2057       int index = info->expression_count() - 1 -
2058           static_cast<int>(slot - expressions_top) / kPointerSize;
2059
2060       if (trace_scope_ != NULL) {
2061         PrintF(trace_scope_->file(),
2062                "Materializing a new heap number %p [%e] in slot %p"
2063                "for expression slot #%d\n",
2064                reinterpret_cast<void*>(*num),
2065                d.value(),
2066                d.destination(),
2067                index);
2068       }
2069
2070       info->SetExpression(index, *num);
2071     }
2072   }
2073 }
2074
2075
2076 static const char* TraceValueType(bool is_smi) {
2077   if (is_smi) {
2078     return "smi";
2079   }
2080
2081   return "heap number";
2082 }
2083
2084
2085 void Deoptimizer::DoTranslateObjectAndSkip(TranslationIterator* iterator) {
2086   Translation::Opcode opcode =
2087       static_cast<Translation::Opcode>(iterator->Next());
2088
2089   switch (opcode) {
2090     case Translation::BEGIN:
2091     case Translation::JS_FRAME:
2092     case Translation::ARGUMENTS_ADAPTOR_FRAME:
2093     case Translation::CONSTRUCT_STUB_FRAME:
2094     case Translation::GETTER_STUB_FRAME:
2095     case Translation::SETTER_STUB_FRAME:
2096     case Translation::COMPILED_STUB_FRAME: {
2097       FATAL("Unexpected frame start translation opcode");
2098       return;
2099     }
2100
2101     case Translation::REGISTER:
2102     case Translation::INT32_REGISTER:
2103     case Translation::UINT32_REGISTER:
2104     case Translation::DOUBLE_REGISTER:
2105     case Translation::STACK_SLOT:
2106     case Translation::INT32_STACK_SLOT:
2107     case Translation::UINT32_STACK_SLOT:
2108     case Translation::DOUBLE_STACK_SLOT:
2109     case Translation::LITERAL: {
2110       // The value is not part of any materialized object, so we can ignore it.
2111       iterator->Skip(Translation::NumberOfOperandsFor(opcode));
2112       return;
2113     }
2114
2115     case Translation::DUPLICATED_OBJECT: {
2116       int object_index = iterator->Next();
2117       if (trace_scope_ != NULL) {
2118         PrintF(trace_scope_->file(), "      skipping object ");
2119         PrintF(trace_scope_->file(),
2120                " ; duplicate of object #%d\n", object_index);
2121       }
2122       AddObjectDuplication(0, object_index);
2123       return;
2124     }
2125
2126     case Translation::ARGUMENTS_OBJECT:
2127     case Translation::CAPTURED_OBJECT: {
2128       int length = iterator->Next();
2129       bool is_args = opcode == Translation::ARGUMENTS_OBJECT;
2130       if (trace_scope_ != NULL) {
2131         PrintF(trace_scope_->file(), "    skipping object ");
2132         PrintF(trace_scope_->file(),
2133                " ; object (length = %d, is_args = %d)\n", length, is_args);
2134       }
2135
2136       AddObjectStart(0, length, is_args);
2137
2138       // We save the object values on the side and materialize the actual
2139       // object after the deoptimized frame is built.
2140       int object_index = deferred_objects_.length() - 1;
2141       for (int i = 0; i < length; i++) {
2142         DoTranslateObject(iterator, object_index, i);
2143       }
2144       return;
2145     }
2146   }
2147
2148   FATAL("Unexpected translation opcode");
2149 }
2150
2151
2152 void Deoptimizer::DoTranslateObject(TranslationIterator* iterator,
2153                                     int object_index,
2154                                     int field_index) {
2155   disasm::NameConverter converter;
2156   Address object_slot = deferred_objects_[object_index].slot_address();
2157
2158   Translation::Opcode opcode =
2159       static_cast<Translation::Opcode>(iterator->Next());
2160
2161   switch (opcode) {
2162     case Translation::BEGIN:
2163     case Translation::JS_FRAME:
2164     case Translation::ARGUMENTS_ADAPTOR_FRAME:
2165     case Translation::CONSTRUCT_STUB_FRAME:
2166     case Translation::GETTER_STUB_FRAME:
2167     case Translation::SETTER_STUB_FRAME:
2168     case Translation::COMPILED_STUB_FRAME:
2169       FATAL("Unexpected frame start translation opcode");
2170       return;
2171
2172     case Translation::REGISTER: {
2173       int input_reg = iterator->Next();
2174       intptr_t input_value = input_->GetRegister(input_reg);
2175       if (trace_scope_ != NULL) {
2176         PrintF(trace_scope_->file(),
2177                "      object @0x%08" V8PRIxPTR ": [field #%d] <- ",
2178                reinterpret_cast<intptr_t>(object_slot),
2179                field_index);
2180         PrintF(trace_scope_->file(),
2181                "0x%08" V8PRIxPTR " ; %s ", input_value,
2182                converter.NameOfCPURegister(input_reg));
2183         reinterpret_cast<Object*>(input_value)->ShortPrint(
2184             trace_scope_->file());
2185         PrintF(trace_scope_->file(),
2186                "\n");
2187       }
2188       AddObjectTaggedValue(input_value);
2189       return;
2190     }
2191
2192     case Translation::INT32_REGISTER: {
2193       int input_reg = iterator->Next();
2194       intptr_t value = input_->GetRegister(input_reg);
2195       bool is_smi = Smi::IsValid(value);
2196       if (trace_scope_ != NULL) {
2197         PrintF(trace_scope_->file(),
2198                "      object @0x%08" V8PRIxPTR ": [field #%d] <- ",
2199                reinterpret_cast<intptr_t>(object_slot),
2200                field_index);
2201         PrintF(trace_scope_->file(),
2202                "%" V8PRIdPTR " ; %s (%s)\n", value,
2203                converter.NameOfCPURegister(input_reg),
2204                TraceValueType(is_smi));
2205       }
2206       if (is_smi) {
2207         intptr_t tagged_value =
2208             reinterpret_cast<intptr_t>(Smi::FromInt(static_cast<int>(value)));
2209         AddObjectTaggedValue(tagged_value);
2210       } else {
2211         double double_value = static_cast<double>(static_cast<int32_t>(value));
2212         AddObjectDoubleValue(double_value);
2213       }
2214       return;
2215     }
2216
2217     case Translation::UINT32_REGISTER: {
2218       int input_reg = iterator->Next();
2219       uintptr_t value = static_cast<uintptr_t>(input_->GetRegister(input_reg));
2220       bool is_smi = (value <= static_cast<uintptr_t>(Smi::kMaxValue));
2221       if (trace_scope_ != NULL) {
2222         PrintF(trace_scope_->file(),
2223                "      object @0x%08" V8PRIxPTR ": [field #%d] <- ",
2224                reinterpret_cast<intptr_t>(object_slot),
2225                field_index);
2226         PrintF(trace_scope_->file(),
2227                "%" V8PRIdPTR " ; uint %s (%s)\n", value,
2228                converter.NameOfCPURegister(input_reg),
2229                TraceValueType(is_smi));
2230       }
2231       if (is_smi) {
2232         intptr_t tagged_value =
2233             reinterpret_cast<intptr_t>(Smi::FromInt(static_cast<int>(value)));
2234         AddObjectTaggedValue(tagged_value);
2235       } else {
2236         double double_value = static_cast<double>(static_cast<uint32_t>(value));
2237         AddObjectDoubleValue(double_value);
2238       }
2239       return;
2240     }
2241
2242     case Translation::DOUBLE_REGISTER: {
2243       int input_reg = iterator->Next();
2244       double value = input_->GetDoubleRegister(input_reg);
2245       if (trace_scope_ != NULL) {
2246         PrintF(trace_scope_->file(),
2247                "      object @0x%08" V8PRIxPTR ": [field #%d] <- ",
2248                reinterpret_cast<intptr_t>(object_slot),
2249                field_index);
2250         PrintF(trace_scope_->file(),
2251                "%e ; %s\n", value,
2252                DoubleRegister::AllocationIndexToString(input_reg));
2253       }
2254       AddObjectDoubleValue(value);
2255       return;
2256     }
2257
2258     case Translation::STACK_SLOT: {
2259       int input_slot_index = iterator->Next();
2260       unsigned input_offset = input_->GetOffsetFromSlotIndex(input_slot_index);
2261       intptr_t input_value = input_->GetFrameSlot(input_offset);
2262       if (trace_scope_ != NULL) {
2263         PrintF(trace_scope_->file(),
2264                "      object @0x%08" V8PRIxPTR ": [field #%d] <- ",
2265                reinterpret_cast<intptr_t>(object_slot),
2266                field_index);
2267         PrintF(trace_scope_->file(),
2268                "0x%08" V8PRIxPTR " ; [sp + %d] ", input_value, input_offset);
2269         reinterpret_cast<Object*>(input_value)->ShortPrint(
2270             trace_scope_->file());
2271         PrintF(trace_scope_->file(),
2272                "\n");
2273       }
2274       AddObjectTaggedValue(input_value);
2275       return;
2276     }
2277
2278     case Translation::INT32_STACK_SLOT: {
2279       int input_slot_index = iterator->Next();
2280       unsigned input_offset = input_->GetOffsetFromSlotIndex(input_slot_index);
2281       intptr_t value = input_->GetFrameSlot(input_offset);
2282       bool is_smi = Smi::IsValid(value);
2283       if (trace_scope_ != NULL) {
2284         PrintF(trace_scope_->file(),
2285                "      object @0x%08" V8PRIxPTR ": [field #%d] <- ",
2286                reinterpret_cast<intptr_t>(object_slot),
2287                field_index);
2288         PrintF(trace_scope_->file(),
2289                "%" V8PRIdPTR " ; [sp + %d] (%s)\n",
2290                value, input_offset, TraceValueType(is_smi));
2291       }
2292       if (is_smi) {
2293         intptr_t tagged_value =
2294             reinterpret_cast<intptr_t>(Smi::FromInt(static_cast<int>(value)));
2295         AddObjectTaggedValue(tagged_value);
2296       } else {
2297         double double_value = static_cast<double>(static_cast<int32_t>(value));
2298         AddObjectDoubleValue(double_value);
2299       }
2300       return;
2301     }
2302
2303     case Translation::UINT32_STACK_SLOT: {
2304       int input_slot_index = iterator->Next();
2305       unsigned input_offset = input_->GetOffsetFromSlotIndex(input_slot_index);
2306       uintptr_t value =
2307           static_cast<uintptr_t>(input_->GetFrameSlot(input_offset));
2308       bool is_smi = (value <= static_cast<uintptr_t>(Smi::kMaxValue));
2309       if (trace_scope_ != NULL) {
2310         PrintF(trace_scope_->file(),
2311                "      object @0x%08" V8PRIxPTR ": [field #%d] <- ",
2312                reinterpret_cast<intptr_t>(object_slot),
2313                field_index);
2314         PrintF(trace_scope_->file(),
2315                "%" V8PRIdPTR " ; [sp + %d] (uint %s)\n",
2316                value, input_offset, TraceValueType(is_smi));
2317       }
2318       if (is_smi) {
2319         intptr_t tagged_value =
2320             reinterpret_cast<intptr_t>(Smi::FromInt(static_cast<int>(value)));
2321         AddObjectTaggedValue(tagged_value);
2322       } else {
2323         double double_value = static_cast<double>(static_cast<uint32_t>(value));
2324         AddObjectDoubleValue(double_value);
2325       }
2326       return;
2327     }
2328
2329     case Translation::DOUBLE_STACK_SLOT: {
2330       int input_slot_index = iterator->Next();
2331       unsigned input_offset = input_->GetOffsetFromSlotIndex(input_slot_index);
2332       double value = input_->GetDoubleFrameSlot(input_offset);
2333       if (trace_scope_ != NULL) {
2334         PrintF(trace_scope_->file(),
2335                "      object @0x%08" V8PRIxPTR ": [field #%d] <- ",
2336                reinterpret_cast<intptr_t>(object_slot),
2337                field_index);
2338         PrintF(trace_scope_->file(),
2339                "%e ; [sp + %d]\n", value, input_offset);
2340       }
2341       AddObjectDoubleValue(value);
2342       return;
2343     }
2344
2345     case Translation::LITERAL: {
2346       Object* literal = ComputeLiteral(iterator->Next());
2347       if (trace_scope_ != NULL) {
2348         PrintF(trace_scope_->file(),
2349                "      object @0x%08" V8PRIxPTR ": [field #%d] <- ",
2350                reinterpret_cast<intptr_t>(object_slot),
2351                field_index);
2352         literal->ShortPrint(trace_scope_->file());
2353         PrintF(trace_scope_->file(),
2354                " ; literal\n");
2355       }
2356       intptr_t value = reinterpret_cast<intptr_t>(literal);
2357       AddObjectTaggedValue(value);
2358       return;
2359     }
2360
2361     case Translation::DUPLICATED_OBJECT: {
2362       int object_index = iterator->Next();
2363       if (trace_scope_ != NULL) {
2364         PrintF(trace_scope_->file(),
2365                "      nested @0x%08" V8PRIxPTR ": [field #%d] <- ",
2366                reinterpret_cast<intptr_t>(object_slot),
2367                field_index);
2368         isolate_->heap()->arguments_marker()->ShortPrint(trace_scope_->file());
2369         PrintF(trace_scope_->file(),
2370                " ; duplicate of object #%d\n", object_index);
2371       }
2372       // Use the materialization marker value as a sentinel and fill in
2373       // the object after the deoptimized frame is built.
2374       intptr_t value = reinterpret_cast<intptr_t>(
2375           isolate_->heap()->arguments_marker());
2376       AddObjectDuplication(0, object_index);
2377       AddObjectTaggedValue(value);
2378       return;
2379     }
2380
2381     case Translation::ARGUMENTS_OBJECT:
2382     case Translation::CAPTURED_OBJECT: {
2383       int length = iterator->Next();
2384       bool is_args = opcode == Translation::ARGUMENTS_OBJECT;
2385       if (trace_scope_ != NULL) {
2386         PrintF(trace_scope_->file(),
2387                "      nested @0x%08" V8PRIxPTR ": [field #%d] <- ",
2388                reinterpret_cast<intptr_t>(object_slot),
2389                field_index);
2390         isolate_->heap()->arguments_marker()->ShortPrint(trace_scope_->file());
2391         PrintF(trace_scope_->file(),
2392                " ; object (length = %d, is_args = %d)\n", length, is_args);
2393       }
2394       // Use the materialization marker value as a sentinel and fill in
2395       // the object after the deoptimized frame is built.
2396       intptr_t value = reinterpret_cast<intptr_t>(
2397           isolate_->heap()->arguments_marker());
2398       AddObjectStart(0, length, is_args);
2399       AddObjectTaggedValue(value);
2400       // We save the object values on the side and materialize the actual
2401       // object after the deoptimized frame is built.
2402       int object_index = deferred_objects_.length() - 1;
2403       for (int i = 0; i < length; i++) {
2404         DoTranslateObject(iterator, object_index, i);
2405       }
2406       return;
2407     }
2408   }
2409
2410   FATAL("Unexpected translation opcode");
2411 }
2412
2413
2414 void Deoptimizer::DoTranslateCommand(TranslationIterator* iterator,
2415                                      int frame_index,
2416                                      unsigned output_offset) {
2417   disasm::NameConverter converter;
2418   // A GC-safe temporary placeholder that we can put in the output frame.
2419   const intptr_t kPlaceholder = reinterpret_cast<intptr_t>(Smi::FromInt(0));
2420
2421   Translation::Opcode opcode =
2422       static_cast<Translation::Opcode>(iterator->Next());
2423
2424   switch (opcode) {
2425     case Translation::BEGIN:
2426     case Translation::JS_FRAME:
2427     case Translation::ARGUMENTS_ADAPTOR_FRAME:
2428     case Translation::CONSTRUCT_STUB_FRAME:
2429     case Translation::GETTER_STUB_FRAME:
2430     case Translation::SETTER_STUB_FRAME:
2431     case Translation::COMPILED_STUB_FRAME:
2432       FATAL("Unexpected translation opcode");
2433       return;
2434
2435     case Translation::REGISTER: {
2436       int input_reg = iterator->Next();
2437       intptr_t input_value = input_->GetRegister(input_reg);
2438       if (trace_scope_ != NULL) {
2439         PrintF(
2440             trace_scope_->file(),
2441             "    0x%08" V8PRIxPTR ": [top + %d] <- 0x%08" V8PRIxPTR " ; %s ",
2442             output_[frame_index]->GetTop() + output_offset,
2443             output_offset,
2444             input_value,
2445             converter.NameOfCPURegister(input_reg));
2446         reinterpret_cast<Object*>(input_value)->ShortPrint(
2447             trace_scope_->file());
2448         PrintF(trace_scope_->file(), "\n");
2449       }
2450       output_[frame_index]->SetFrameSlot(output_offset, input_value);
2451       return;
2452     }
2453
2454     case Translation::INT32_REGISTER: {
2455       int input_reg = iterator->Next();
2456       intptr_t value = input_->GetRegister(input_reg);
2457       bool is_smi = Smi::IsValid(value);
2458       if (trace_scope_ != NULL) {
2459         PrintF(
2460             trace_scope_->file(),
2461             "    0x%08" V8PRIxPTR ": [top + %d] <- %" V8PRIdPTR " ; %s (%s)\n",
2462             output_[frame_index]->GetTop() + output_offset,
2463             output_offset,
2464             value,
2465             converter.NameOfCPURegister(input_reg),
2466             TraceValueType(is_smi));
2467       }
2468       if (is_smi) {
2469         intptr_t tagged_value =
2470             reinterpret_cast<intptr_t>(Smi::FromInt(static_cast<int>(value)));
2471         output_[frame_index]->SetFrameSlot(output_offset, tagged_value);
2472       } else {
2473         // We save the untagged value on the side and store a GC-safe
2474         // temporary placeholder in the frame.
2475         AddDoubleValue(output_[frame_index]->GetTop() + output_offset,
2476                        static_cast<double>(static_cast<int32_t>(value)));
2477         output_[frame_index]->SetFrameSlot(output_offset, kPlaceholder);
2478       }
2479       return;
2480     }
2481
2482     case Translation::UINT32_REGISTER: {
2483       int input_reg = iterator->Next();
2484       uintptr_t value = static_cast<uintptr_t>(input_->GetRegister(input_reg));
2485       bool is_smi = value <= static_cast<uintptr_t>(Smi::kMaxValue);
2486       if (trace_scope_ != NULL) {
2487         PrintF(
2488             trace_scope_->file(),
2489             "    0x%08" V8PRIxPTR ": [top + %d] <- %" V8PRIuPTR
2490             " ; uint %s (%s)\n",
2491             output_[frame_index]->GetTop() + output_offset,
2492             output_offset,
2493             value,
2494             converter.NameOfCPURegister(input_reg),
2495             TraceValueType(is_smi));
2496       }
2497       if (is_smi) {
2498         intptr_t tagged_value =
2499             reinterpret_cast<intptr_t>(Smi::FromInt(static_cast<int>(value)));
2500         output_[frame_index]->SetFrameSlot(output_offset, tagged_value);
2501       } else {
2502         // We save the untagged value on the side and store a GC-safe
2503         // temporary placeholder in the frame.
2504         AddDoubleValue(output_[frame_index]->GetTop() + output_offset,
2505                        static_cast<double>(static_cast<uint32_t>(value)));
2506         output_[frame_index]->SetFrameSlot(output_offset, kPlaceholder);
2507       }
2508       return;
2509     }
2510
2511     case Translation::DOUBLE_REGISTER: {
2512       int input_reg = iterator->Next();
2513       double value = input_->GetDoubleRegister(input_reg);
2514       if (trace_scope_ != NULL) {
2515         PrintF(trace_scope_->file(),
2516                "    0x%08" V8PRIxPTR ": [top + %d] <- %e ; %s\n",
2517                output_[frame_index]->GetTop() + output_offset,
2518                output_offset,
2519                value,
2520                DoubleRegister::AllocationIndexToString(input_reg));
2521       }
2522       // We save the untagged value on the side and store a GC-safe
2523       // temporary placeholder in the frame.
2524       AddDoubleValue(output_[frame_index]->GetTop() + output_offset, value);
2525       output_[frame_index]->SetFrameSlot(output_offset, kPlaceholder);
2526       return;
2527     }
2528
2529     case Translation::STACK_SLOT: {
2530       int input_slot_index = iterator->Next();
2531       unsigned input_offset = input_->GetOffsetFromSlotIndex(input_slot_index);
2532       intptr_t input_value = input_->GetFrameSlot(input_offset);
2533       if (trace_scope_ != NULL) {
2534         PrintF(trace_scope_->file(),
2535                "    0x%08" V8PRIxPTR ": ",
2536                output_[frame_index]->GetTop() + output_offset);
2537         PrintF(trace_scope_->file(),
2538                "[top + %d] <- 0x%08" V8PRIxPTR " ; [sp + %d] ",
2539                output_offset,
2540                input_value,
2541                input_offset);
2542         reinterpret_cast<Object*>(input_value)->ShortPrint(
2543             trace_scope_->file());
2544         PrintF(trace_scope_->file(), "\n");
2545       }
2546       output_[frame_index]->SetFrameSlot(output_offset, input_value);
2547       return;
2548     }
2549
2550     case Translation::INT32_STACK_SLOT: {
2551       int input_slot_index = iterator->Next();
2552       unsigned input_offset = input_->GetOffsetFromSlotIndex(input_slot_index);
2553       intptr_t value = input_->GetFrameSlot(input_offset);
2554       bool is_smi = Smi::IsValid(value);
2555       if (trace_scope_ != NULL) {
2556         PrintF(trace_scope_->file(),
2557                "    0x%08" V8PRIxPTR ": ",
2558                output_[frame_index]->GetTop() + output_offset);
2559         PrintF(trace_scope_->file(),
2560                "[top + %d] <- %" V8PRIdPTR " ; [sp + %d] (%s)\n",
2561                output_offset,
2562                value,
2563                input_offset,
2564                TraceValueType(is_smi));
2565       }
2566       if (is_smi) {
2567         intptr_t tagged_value =
2568             reinterpret_cast<intptr_t>(Smi::FromInt(static_cast<int>(value)));
2569         output_[frame_index]->SetFrameSlot(output_offset, tagged_value);
2570       } else {
2571         // We save the untagged value on the side and store a GC-safe
2572         // temporary placeholder in the frame.
2573         AddDoubleValue(output_[frame_index]->GetTop() + output_offset,
2574                        static_cast<double>(static_cast<int32_t>(value)));
2575         output_[frame_index]->SetFrameSlot(output_offset, kPlaceholder);
2576       }
2577       return;
2578     }
2579
2580     case Translation::UINT32_STACK_SLOT: {
2581       int input_slot_index = iterator->Next();
2582       unsigned input_offset = input_->GetOffsetFromSlotIndex(input_slot_index);
2583       uintptr_t value =
2584           static_cast<uintptr_t>(input_->GetFrameSlot(input_offset));
2585       bool is_smi = value <= static_cast<uintptr_t>(Smi::kMaxValue);
2586       if (trace_scope_ != NULL) {
2587         PrintF(trace_scope_->file(),
2588                "    0x%08" V8PRIxPTR ": ",
2589                output_[frame_index]->GetTop() + output_offset);
2590         PrintF(trace_scope_->file(),
2591                "[top + %d] <- %" V8PRIuPTR " ; [sp + %d] (uint32 %s)\n",
2592                output_offset,
2593                value,
2594                input_offset,
2595                TraceValueType(is_smi));
2596       }
2597       if (is_smi) {
2598         intptr_t tagged_value =
2599             reinterpret_cast<intptr_t>(Smi::FromInt(static_cast<int>(value)));
2600         output_[frame_index]->SetFrameSlot(output_offset, tagged_value);
2601       } else {
2602         // We save the untagged value on the side and store a GC-safe
2603         // temporary placeholder in the frame.
2604         AddDoubleValue(output_[frame_index]->GetTop() + output_offset,
2605                        static_cast<double>(static_cast<uint32_t>(value)));
2606         output_[frame_index]->SetFrameSlot(output_offset, kPlaceholder);
2607       }
2608       return;
2609     }
2610
2611     case Translation::DOUBLE_STACK_SLOT: {
2612       int input_slot_index = iterator->Next();
2613       unsigned input_offset = input_->GetOffsetFromSlotIndex(input_slot_index);
2614       double value = input_->GetDoubleFrameSlot(input_offset);
2615       if (trace_scope_ != NULL) {
2616         PrintF(trace_scope_->file(),
2617                "    0x%08" V8PRIxPTR ": [top + %d] <- %e ; [sp + %d]\n",
2618                output_[frame_index]->GetTop() + output_offset,
2619                output_offset,
2620                value,
2621                input_offset);
2622       }
2623       // We save the untagged value on the side and store a GC-safe
2624       // temporary placeholder in the frame.
2625       AddDoubleValue(output_[frame_index]->GetTop() + output_offset, value);
2626       output_[frame_index]->SetFrameSlot(output_offset, kPlaceholder);
2627       return;
2628     }
2629
2630     case Translation::LITERAL: {
2631       Object* literal = ComputeLiteral(iterator->Next());
2632       if (trace_scope_ != NULL) {
2633         PrintF(trace_scope_->file(),
2634                "    0x%08" V8PRIxPTR ": [top + %d] <- ",
2635                output_[frame_index]->GetTop() + output_offset,
2636                output_offset);
2637         literal->ShortPrint(trace_scope_->file());
2638         PrintF(trace_scope_->file(), " ; literal\n");
2639       }
2640       intptr_t value = reinterpret_cast<intptr_t>(literal);
2641       output_[frame_index]->SetFrameSlot(output_offset, value);
2642       return;
2643     }
2644
2645     case Translation::DUPLICATED_OBJECT: {
2646       int object_index = iterator->Next();
2647       if (trace_scope_ != NULL) {
2648         PrintF(trace_scope_->file(),
2649                "    0x%08" V8PRIxPTR ": [top + %d] <- ",
2650                output_[frame_index]->GetTop() + output_offset,
2651                output_offset);
2652         isolate_->heap()->arguments_marker()->ShortPrint(trace_scope_->file());
2653         PrintF(trace_scope_->file(),
2654                " ; duplicate of object #%d\n", object_index);
2655       }
2656       // Use the materialization marker value as a sentinel and fill in
2657       // the object after the deoptimized frame is built.
2658       intptr_t value = reinterpret_cast<intptr_t>(
2659           isolate_->heap()->arguments_marker());
2660       AddObjectDuplication(output_[frame_index]->GetTop() + output_offset,
2661                            object_index);
2662       output_[frame_index]->SetFrameSlot(output_offset, value);
2663       return;
2664     }
2665
2666     case Translation::ARGUMENTS_OBJECT:
2667     case Translation::CAPTURED_OBJECT: {
2668       int length = iterator->Next();
2669       bool is_args = opcode == Translation::ARGUMENTS_OBJECT;
2670       if (trace_scope_ != NULL) {
2671         PrintF(trace_scope_->file(),
2672                "    0x%08" V8PRIxPTR ": [top + %d] <- ",
2673                output_[frame_index]->GetTop() + output_offset,
2674                output_offset);
2675         isolate_->heap()->arguments_marker()->ShortPrint(trace_scope_->file());
2676         PrintF(trace_scope_->file(),
2677                " ; object (length = %d, is_args = %d)\n", length, is_args);
2678       }
2679       // Use the materialization marker value as a sentinel and fill in
2680       // the object after the deoptimized frame is built.
2681       intptr_t value = reinterpret_cast<intptr_t>(
2682           isolate_->heap()->arguments_marker());
2683       AddObjectStart(output_[frame_index]->GetTop() + output_offset,
2684                      length, is_args);
2685       output_[frame_index]->SetFrameSlot(output_offset, value);
2686       // We save the object values on the side and materialize the actual
2687       // object after the deoptimized frame is built.
2688       int object_index = deferred_objects_.length() - 1;
2689       for (int i = 0; i < length; i++) {
2690         DoTranslateObject(iterator, object_index, i);
2691       }
2692       return;
2693     }
2694   }
2695 }
2696
2697
2698 unsigned Deoptimizer::ComputeInputFrameSize() const {
2699   unsigned fixed_size = ComputeFixedSize(function_);
2700   // The fp-to-sp delta already takes the context, constant pool pointer and the
2701   // function into account so we have to avoid double counting them.
2702   unsigned result = fixed_size + fp_to_sp_delta_ -
2703       StandardFrameConstants::kFixedFrameSizeFromFp;
2704   if (compiled_code_->kind() == Code::OPTIMIZED_FUNCTION) {
2705     unsigned stack_slots = compiled_code_->stack_slots();
2706     unsigned outgoing_size = ComputeOutgoingArgumentSize();
2707     CHECK(result == fixed_size + (stack_slots * kPointerSize) + outgoing_size);
2708   }
2709   return result;
2710 }
2711
2712
2713 unsigned Deoptimizer::ComputeFixedSize(JSFunction* function) const {
2714   // The fixed part of the frame consists of the return address, frame
2715   // pointer, function, context, and all the incoming arguments.
2716   return ComputeIncomingArgumentSize(function) +
2717       StandardFrameConstants::kFixedFrameSize;
2718 }
2719
2720
2721 unsigned Deoptimizer::ComputeIncomingArgumentSize(JSFunction* function) const {
2722   // The incoming arguments is the values for formal parameters and
2723   // the receiver. Every slot contains a pointer.
2724   if (function->IsSmi()) {
2725     CHECK_EQ(Smi::cast(function), Smi::FromInt(StackFrame::STUB));
2726     return 0;
2727   }
2728   unsigned arguments =
2729       function->shared()->internal_formal_parameter_count() + 1;
2730   return arguments * kPointerSize;
2731 }
2732
2733
2734 unsigned Deoptimizer::ComputeOutgoingArgumentSize() const {
2735   DeoptimizationInputData* data = DeoptimizationInputData::cast(
2736       compiled_code_->deoptimization_data());
2737   unsigned height = data->ArgumentsStackHeight(bailout_id_)->value();
2738   return height * kPointerSize;
2739 }
2740
2741
2742 Object* Deoptimizer::ComputeLiteral(int index) const {
2743   DeoptimizationInputData* data = DeoptimizationInputData::cast(
2744       compiled_code_->deoptimization_data());
2745   FixedArray* literals = data->LiteralArray();
2746   return literals->get(index);
2747 }
2748
2749
2750 void Deoptimizer::AddObjectStart(intptr_t slot, int length, bool is_args) {
2751   ObjectMaterializationDescriptor object_desc(
2752       reinterpret_cast<Address>(slot), jsframe_count_, length, -1, is_args);
2753   deferred_objects_.Add(object_desc);
2754 }
2755
2756
2757 void Deoptimizer::AddObjectDuplication(intptr_t slot, int object_index) {
2758   ObjectMaterializationDescriptor object_desc(
2759       reinterpret_cast<Address>(slot), jsframe_count_, -1, object_index, false);
2760   deferred_objects_.Add(object_desc);
2761 }
2762
2763
2764 void Deoptimizer::AddObjectTaggedValue(intptr_t value) {
2765   deferred_objects_tagged_values_.Add(reinterpret_cast<Object*>(value));
2766 }
2767
2768
2769 void Deoptimizer::AddObjectDoubleValue(double value) {
2770   deferred_objects_tagged_values_.Add(isolate()->heap()->the_hole_value());
2771   HeapNumberMaterializationDescriptor<int> value_desc(
2772       deferred_objects_tagged_values_.length() - 1, value);
2773   deferred_objects_double_values_.Add(value_desc);
2774 }
2775
2776
2777 void Deoptimizer::AddDoubleValue(intptr_t slot_address, double value) {
2778   HeapNumberMaterializationDescriptor<Address> value_desc(
2779       reinterpret_cast<Address>(slot_address), value);
2780   deferred_heap_numbers_.Add(value_desc);
2781 }
2782
2783
2784 void Deoptimizer::EnsureCodeForDeoptimizationEntry(Isolate* isolate,
2785                                                    BailoutType type,
2786                                                    int max_entry_id) {
2787   // We cannot run this if the serializer is enabled because this will
2788   // cause us to emit relocation information for the external
2789   // references. This is fine because the deoptimizer's code section
2790   // isn't meant to be serialized at all.
2791   CHECK(type == EAGER || type == SOFT || type == LAZY);
2792   DeoptimizerData* data = isolate->deoptimizer_data();
2793   int entry_count = data->deopt_entry_code_entries_[type];
2794   if (max_entry_id < entry_count) return;
2795   entry_count = Max(entry_count, Deoptimizer::kMinNumberOfEntries);
2796   while (max_entry_id >= entry_count) entry_count *= 2;
2797   CHECK(entry_count <= Deoptimizer::kMaxNumberOfEntries);
2798
2799   MacroAssembler masm(isolate, NULL, 16 * KB);
2800   masm.set_emit_debug_code(false);
2801   GenerateDeoptimizationEntries(&masm, entry_count, type);
2802   CodeDesc desc;
2803   masm.GetCode(&desc);
2804   DCHECK(!RelocInfo::RequiresRelocation(desc));
2805
2806   MemoryChunk* chunk = data->deopt_entry_code_[type];
2807   CHECK(static_cast<int>(Deoptimizer::GetMaxDeoptTableSize()) >=
2808         desc.instr_size);
2809   chunk->CommitArea(desc.instr_size);
2810   CopyBytes(chunk->area_start(), desc.buffer,
2811       static_cast<size_t>(desc.instr_size));
2812   CpuFeatures::FlushICache(chunk->area_start(), desc.instr_size);
2813
2814   data->deopt_entry_code_entries_[type] = entry_count;
2815 }
2816
2817
2818 FrameDescription::FrameDescription(uint32_t frame_size,
2819                                    JSFunction* function)
2820     : frame_size_(frame_size),
2821       function_(function),
2822       top_(kZapUint32),
2823       pc_(kZapUint32),
2824       fp_(kZapUint32),
2825       context_(kZapUint32),
2826       constant_pool_(kZapUint32) {
2827   // Zap all the registers.
2828   for (int r = 0; r < Register::kNumRegisters; r++) {
2829     // TODO(jbramley): It isn't safe to use kZapUint32 here. If the register
2830     // isn't used before the next safepoint, the GC will try to scan it as a
2831     // tagged value. kZapUint32 looks like a valid tagged pointer, but it isn't.
2832     SetRegister(r, kZapUint32);
2833   }
2834
2835   // Zap all the slots.
2836   for (unsigned o = 0; o < frame_size; o += kPointerSize) {
2837     SetFrameSlot(o, kZapUint32);
2838   }
2839 }
2840
2841
2842 int FrameDescription::ComputeFixedSize() {
2843   return StandardFrameConstants::kFixedFrameSize +
2844       (ComputeParametersCount() + 1) * kPointerSize;
2845 }
2846
2847
2848 unsigned FrameDescription::GetOffsetFromSlotIndex(int slot_index) {
2849   if (slot_index >= 0) {
2850     // Local or spill slots. Skip the fixed part of the frame
2851     // including all arguments.
2852     unsigned base = GetFrameSize() - ComputeFixedSize();
2853     return base - ((slot_index + 1) * kPointerSize);
2854   } else {
2855     // Incoming parameter.
2856     int arg_size = (ComputeParametersCount() + 1) * kPointerSize;
2857     unsigned base = GetFrameSize() - arg_size;
2858     return base - ((slot_index + 1) * kPointerSize);
2859   }
2860 }
2861
2862
2863 int FrameDescription::ComputeParametersCount() {
2864   switch (type_) {
2865     case StackFrame::JAVA_SCRIPT:
2866       return function_->shared()->internal_formal_parameter_count();
2867     case StackFrame::ARGUMENTS_ADAPTOR: {
2868       // Last slot contains number of incomming arguments as a smi.
2869       // Can't use GetExpression(0) because it would cause infinite recursion.
2870       return reinterpret_cast<Smi*>(*GetFrameSlotPointer(0))->value();
2871     }
2872     case StackFrame::STUB:
2873       return -1;  // Minus receiver.
2874     default:
2875       FATAL("Unexpected stack frame type");
2876       return 0;
2877   }
2878 }
2879
2880
2881 Object* FrameDescription::GetParameter(int index) {
2882   CHECK_GE(index, 0);
2883   CHECK_LT(index, ComputeParametersCount());
2884   // The slot indexes for incoming arguments are negative.
2885   unsigned offset = GetOffsetFromSlotIndex(index - ComputeParametersCount());
2886   return reinterpret_cast<Object*>(*GetFrameSlotPointer(offset));
2887 }
2888
2889
2890 unsigned FrameDescription::GetExpressionCount() {
2891   CHECK_EQ(StackFrame::JAVA_SCRIPT, type_);
2892   unsigned size = GetFrameSize() - ComputeFixedSize();
2893   return size / kPointerSize;
2894 }
2895
2896
2897 Object* FrameDescription::GetExpression(int index) {
2898   DCHECK_EQ(StackFrame::JAVA_SCRIPT, type_);
2899   unsigned offset = GetOffsetFromSlotIndex(index);
2900   return reinterpret_cast<Object*>(*GetFrameSlotPointer(offset));
2901 }
2902
2903
2904 void TranslationBuffer::Add(int32_t value, Zone* zone) {
2905   // Encode the sign bit in the least significant bit.
2906   bool is_negative = (value < 0);
2907   uint32_t bits = ((is_negative ? -value : value) << 1) |
2908       static_cast<int32_t>(is_negative);
2909   // Encode the individual bytes using the least significant bit of
2910   // each byte to indicate whether or not more bytes follow.
2911   do {
2912     uint32_t next = bits >> 7;
2913     contents_.Add(((bits << 1) & 0xFF) | (next != 0), zone);
2914     bits = next;
2915   } while (bits != 0);
2916 }
2917
2918
2919 int32_t TranslationIterator::Next() {
2920   // Run through the bytes until we reach one with a least significant
2921   // bit of zero (marks the end).
2922   uint32_t bits = 0;
2923   for (int i = 0; true; i += 7) {
2924     DCHECK(HasNext());
2925     uint8_t next = buffer_->get(index_++);
2926     bits |= (next >> 1) << i;
2927     if ((next & 1) == 0) break;
2928   }
2929   // The bits encode the sign in the least significant bit.
2930   bool is_negative = (bits & 1) == 1;
2931   int32_t result = bits >> 1;
2932   return is_negative ? -result : result;
2933 }
2934
2935
2936 Handle<ByteArray> TranslationBuffer::CreateByteArray(Factory* factory) {
2937   int length = contents_.length();
2938   Handle<ByteArray> result = factory->NewByteArray(length, TENURED);
2939   MemCopy(result->GetDataStartAddress(), contents_.ToVector().start(), length);
2940   return result;
2941 }
2942
2943
2944 void Translation::BeginConstructStubFrame(int literal_id, unsigned height) {
2945   buffer_->Add(CONSTRUCT_STUB_FRAME, zone());
2946   buffer_->Add(literal_id, zone());
2947   buffer_->Add(height, zone());
2948 }
2949
2950
2951 void Translation::BeginGetterStubFrame(int literal_id) {
2952   buffer_->Add(GETTER_STUB_FRAME, zone());
2953   buffer_->Add(literal_id, zone());
2954 }
2955
2956
2957 void Translation::BeginSetterStubFrame(int literal_id) {
2958   buffer_->Add(SETTER_STUB_FRAME, zone());
2959   buffer_->Add(literal_id, zone());
2960 }
2961
2962
2963 void Translation::BeginArgumentsAdaptorFrame(int literal_id, unsigned height) {
2964   buffer_->Add(ARGUMENTS_ADAPTOR_FRAME, zone());
2965   buffer_->Add(literal_id, zone());
2966   buffer_->Add(height, zone());
2967 }
2968
2969
2970 void Translation::BeginJSFrame(BailoutId node_id,
2971                                int literal_id,
2972                                unsigned height) {
2973   buffer_->Add(JS_FRAME, zone());
2974   buffer_->Add(node_id.ToInt(), zone());
2975   buffer_->Add(literal_id, zone());
2976   buffer_->Add(height, zone());
2977 }
2978
2979
2980 void Translation::BeginCompiledStubFrame() {
2981   buffer_->Add(COMPILED_STUB_FRAME, zone());
2982 }
2983
2984
2985 void Translation::BeginArgumentsObject(int args_length) {
2986   buffer_->Add(ARGUMENTS_OBJECT, zone());
2987   buffer_->Add(args_length, zone());
2988 }
2989
2990
2991 void Translation::BeginCapturedObject(int length) {
2992   buffer_->Add(CAPTURED_OBJECT, zone());
2993   buffer_->Add(length, zone());
2994 }
2995
2996
2997 void Translation::DuplicateObject(int object_index) {
2998   buffer_->Add(DUPLICATED_OBJECT, zone());
2999   buffer_->Add(object_index, zone());
3000 }
3001
3002
3003 void Translation::StoreRegister(Register reg) {
3004   buffer_->Add(REGISTER, zone());
3005   buffer_->Add(reg.code(), zone());
3006 }
3007
3008
3009 void Translation::StoreInt32Register(Register reg) {
3010   buffer_->Add(INT32_REGISTER, zone());
3011   buffer_->Add(reg.code(), zone());
3012 }
3013
3014
3015 void Translation::StoreUint32Register(Register reg) {
3016   buffer_->Add(UINT32_REGISTER, zone());
3017   buffer_->Add(reg.code(), zone());
3018 }
3019
3020
3021 void Translation::StoreDoubleRegister(DoubleRegister reg) {
3022   buffer_->Add(DOUBLE_REGISTER, zone());
3023   buffer_->Add(DoubleRegister::ToAllocationIndex(reg), zone());
3024 }
3025
3026
3027 void Translation::StoreStackSlot(int index) {
3028   buffer_->Add(STACK_SLOT, zone());
3029   buffer_->Add(index, zone());
3030 }
3031
3032
3033 void Translation::StoreInt32StackSlot(int index) {
3034   buffer_->Add(INT32_STACK_SLOT, zone());
3035   buffer_->Add(index, zone());
3036 }
3037
3038
3039 void Translation::StoreUint32StackSlot(int index) {
3040   buffer_->Add(UINT32_STACK_SLOT, zone());
3041   buffer_->Add(index, zone());
3042 }
3043
3044
3045 void Translation::StoreDoubleStackSlot(int index) {
3046   buffer_->Add(DOUBLE_STACK_SLOT, zone());
3047   buffer_->Add(index, zone());
3048 }
3049
3050
3051 void Translation::StoreLiteral(int literal_id) {
3052   buffer_->Add(LITERAL, zone());
3053   buffer_->Add(literal_id, zone());
3054 }
3055
3056
3057 void Translation::StoreArgumentsObject(bool args_known,
3058                                        int args_index,
3059                                        int args_length) {
3060   buffer_->Add(ARGUMENTS_OBJECT, zone());
3061   buffer_->Add(args_known, zone());
3062   buffer_->Add(args_index, zone());
3063   buffer_->Add(args_length, zone());
3064 }
3065
3066
3067 int Translation::NumberOfOperandsFor(Opcode opcode) {
3068   switch (opcode) {
3069     case GETTER_STUB_FRAME:
3070     case SETTER_STUB_FRAME:
3071     case DUPLICATED_OBJECT:
3072     case ARGUMENTS_OBJECT:
3073     case CAPTURED_OBJECT:
3074     case REGISTER:
3075     case INT32_REGISTER:
3076     case UINT32_REGISTER:
3077     case DOUBLE_REGISTER:
3078     case STACK_SLOT:
3079     case INT32_STACK_SLOT:
3080     case UINT32_STACK_SLOT:
3081     case DOUBLE_STACK_SLOT:
3082     case LITERAL:
3083     case COMPILED_STUB_FRAME:
3084       return 1;
3085     case BEGIN:
3086     case ARGUMENTS_ADAPTOR_FRAME:
3087     case CONSTRUCT_STUB_FRAME:
3088       return 2;
3089     case JS_FRAME:
3090       return 3;
3091   }
3092   FATAL("Unexpected translation type");
3093   return -1;
3094 }
3095
3096
3097 #if defined(OBJECT_PRINT) || defined(ENABLE_DISASSEMBLER)
3098
3099 const char* Translation::StringFor(Opcode opcode) {
3100 #define TRANSLATION_OPCODE_CASE(item)   case item: return #item;
3101   switch (opcode) {
3102     TRANSLATION_OPCODE_LIST(TRANSLATION_OPCODE_CASE)
3103   }
3104 #undef TRANSLATION_OPCODE_CASE
3105   UNREACHABLE();
3106   return "";
3107 }
3108
3109 #endif
3110
3111
3112 // We can't intermix stack decoding and allocations because
3113 // deoptimization infrastracture is not GC safe.
3114 // Thus we build a temporary structure in malloced space.
3115 SlotRef SlotRefValueBuilder::ComputeSlotForNextArgument(
3116     Translation::Opcode opcode,
3117     TranslationIterator* iterator,
3118     DeoptimizationInputData* data,
3119     JavaScriptFrame* frame) {
3120   switch (opcode) {
3121     case Translation::BEGIN:
3122     case Translation::JS_FRAME:
3123     case Translation::ARGUMENTS_ADAPTOR_FRAME:
3124     case Translation::CONSTRUCT_STUB_FRAME:
3125     case Translation::GETTER_STUB_FRAME:
3126     case Translation::SETTER_STUB_FRAME:
3127       // Peeled off before getting here.
3128       break;
3129
3130     case Translation::DUPLICATED_OBJECT: {
3131       return SlotRef::NewDuplicateObject(iterator->Next());
3132     }
3133
3134     case Translation::ARGUMENTS_OBJECT:
3135       return SlotRef::NewArgumentsObject(iterator->Next());
3136
3137     case Translation::CAPTURED_OBJECT: {
3138       return SlotRef::NewDeferredObject(iterator->Next());
3139     }
3140
3141     case Translation::REGISTER:
3142     case Translation::INT32_REGISTER:
3143     case Translation::UINT32_REGISTER:
3144     case Translation::DOUBLE_REGISTER:
3145       // We are at safepoint which corresponds to call.  All registers are
3146       // saved by caller so there would be no live registers at this
3147       // point. Thus these translation commands should not be used.
3148       break;
3149
3150     case Translation::STACK_SLOT: {
3151       int slot_index = iterator->Next();
3152       Address slot_addr = SlotAddress(frame, slot_index);
3153       return SlotRef(slot_addr, SlotRef::TAGGED);
3154     }
3155
3156     case Translation::INT32_STACK_SLOT: {
3157       int slot_index = iterator->Next();
3158       Address slot_addr = SlotAddress(frame, slot_index);
3159       return SlotRef(slot_addr, SlotRef::INT32);
3160     }
3161
3162     case Translation::UINT32_STACK_SLOT: {
3163       int slot_index = iterator->Next();
3164       Address slot_addr = SlotAddress(frame, slot_index);
3165       return SlotRef(slot_addr, SlotRef::UINT32);
3166     }
3167
3168     case Translation::DOUBLE_STACK_SLOT: {
3169       int slot_index = iterator->Next();
3170       Address slot_addr = SlotAddress(frame, slot_index);
3171       return SlotRef(slot_addr, SlotRef::DOUBLE);
3172     }
3173
3174     case Translation::LITERAL: {
3175       int literal_index = iterator->Next();
3176       return SlotRef(data->GetIsolate(),
3177                      data->LiteralArray()->get(literal_index));
3178     }
3179
3180     case Translation::COMPILED_STUB_FRAME:
3181       UNREACHABLE();
3182       break;
3183   }
3184
3185   FATAL("We should never get here - unexpected deopt info.");
3186   return SlotRef();
3187 }
3188
3189
3190 SlotRefValueBuilder::SlotRefValueBuilder(JavaScriptFrame* frame,
3191                                          int inlined_jsframe_index,
3192                                          int formal_parameter_count)
3193     : current_slot_(0),
3194       args_length_(-1),
3195       first_slot_index_(-1),
3196       should_deoptimize_(false) {
3197   DisallowHeapAllocation no_gc;
3198
3199   int deopt_index = Safepoint::kNoDeoptimizationIndex;
3200   DeoptimizationInputData* data =
3201       static_cast<OptimizedFrame*>(frame)->GetDeoptimizationData(&deopt_index);
3202   TranslationIterator it(data->TranslationByteArray(),
3203                          data->TranslationIndex(deopt_index)->value());
3204   Translation::Opcode opcode = static_cast<Translation::Opcode>(it.Next());
3205   CHECK_EQ(opcode, Translation::BEGIN);
3206   it.Next();  // Drop frame count.
3207
3208   stack_frame_id_ = frame->fp();
3209
3210   int jsframe_count = it.Next();
3211   CHECK_GT(jsframe_count, inlined_jsframe_index);
3212   int jsframes_to_skip = inlined_jsframe_index;
3213   int number_of_slots = -1;  // Number of slots inside our frame (yet unknown)
3214   while (number_of_slots != 0) {
3215     opcode = static_cast<Translation::Opcode>(it.Next());
3216     bool processed = false;
3217     if (opcode == Translation::ARGUMENTS_ADAPTOR_FRAME) {
3218       if (jsframes_to_skip == 0) {
3219         CHECK_EQ(Translation::NumberOfOperandsFor(opcode), 2);
3220
3221         it.Skip(1);  // literal id
3222         int height = it.Next();
3223
3224         // Skip the translation command for the receiver.
3225         it.Skip(Translation::NumberOfOperandsFor(
3226             static_cast<Translation::Opcode>(it.Next())));
3227
3228         // We reached the arguments adaptor frame corresponding to the
3229         // inlined function in question.  Number of arguments is height - 1.
3230         first_slot_index_ = slot_refs_.length();
3231         args_length_ = height - 1;
3232         number_of_slots = height - 1;
3233         processed = true;
3234       }
3235     } else if (opcode == Translation::JS_FRAME) {
3236       if (jsframes_to_skip == 0) {
3237         // Skip over operands to advance to the next opcode.
3238         it.Skip(Translation::NumberOfOperandsFor(opcode));
3239
3240         // Skip the translation command for the receiver.
3241         it.Skip(Translation::NumberOfOperandsFor(
3242             static_cast<Translation::Opcode>(it.Next())));
3243
3244         // We reached the frame corresponding to the inlined function
3245         // in question.  Process the translation commands for the
3246         // arguments.  Number of arguments is equal to the number of
3247         // format parameter count.
3248         first_slot_index_ = slot_refs_.length();
3249         args_length_ = formal_parameter_count;
3250         number_of_slots = formal_parameter_count;
3251         processed = true;
3252       }
3253       jsframes_to_skip--;
3254     } else if (opcode != Translation::BEGIN &&
3255                opcode != Translation::CONSTRUCT_STUB_FRAME &&
3256                opcode != Translation::GETTER_STUB_FRAME &&
3257                opcode != Translation::SETTER_STUB_FRAME &&
3258                opcode != Translation::COMPILED_STUB_FRAME) {
3259       slot_refs_.Add(ComputeSlotForNextArgument(opcode, &it, data, frame));
3260
3261       if (first_slot_index_ >= 0) {
3262         // We have found the beginning of our frame -> make sure we count
3263         // the nested slots of captured objects
3264         number_of_slots--;
3265         SlotRef& slot = slot_refs_.last();
3266         CHECK_NE(slot.Representation(), SlotRef::ARGUMENTS_OBJECT);
3267         number_of_slots += slot.GetChildrenCount();
3268         if (slot.Representation() == SlotRef::DEFERRED_OBJECT ||
3269             slot.Representation() == SlotRef::DUPLICATE_OBJECT) {
3270           should_deoptimize_ = true;
3271         }
3272       }
3273
3274       processed = true;
3275     }
3276     if (!processed) {
3277       // Skip over operands to advance to the next opcode.
3278       it.Skip(Translation::NumberOfOperandsFor(opcode));
3279     }
3280   }
3281   if (should_deoptimize_) {
3282     List<JSFunction*> functions(2);
3283     frame->GetFunctions(&functions);
3284     Deoptimizer::DeoptimizeFunction(functions[0]);
3285   }
3286 }
3287
3288
3289 Handle<Object> SlotRef::GetValue(Isolate* isolate) {
3290   switch (representation_) {
3291     case TAGGED: {
3292       Handle<Object> value(Memory::Object_at(addr_), isolate);
3293       if (value->IsMutableHeapNumber()) {
3294         HeapNumber::cast(*value)->set_map(isolate->heap()->heap_number_map());
3295       }
3296       return value;
3297     }
3298
3299     case INT32: {
3300 #if V8_TARGET_BIG_ENDIAN && V8_HOST_ARCH_64_BIT
3301       int value = Memory::int32_at(addr_ + kIntSize);
3302 #else
3303       int value = Memory::int32_at(addr_);
3304 #endif
3305       if (Smi::IsValid(value)) {
3306         return Handle<Object>(Smi::FromInt(value), isolate);
3307       } else {
3308         return isolate->factory()->NewNumberFromInt(value);
3309       }
3310     }
3311
3312     case UINT32: {
3313 #if V8_TARGET_BIG_ENDIAN && V8_HOST_ARCH_64_BIT
3314       uint32_t value = Memory::uint32_at(addr_ + kIntSize);
3315 #else
3316       uint32_t value = Memory::uint32_at(addr_);
3317 #endif
3318       if (value <= static_cast<uint32_t>(Smi::kMaxValue)) {
3319         return Handle<Object>(Smi::FromInt(static_cast<int>(value)), isolate);
3320       } else {
3321         return isolate->factory()->NewNumber(static_cast<double>(value));
3322       }
3323     }
3324
3325     case DOUBLE: {
3326       double value = read_double_value(addr_);
3327       return isolate->factory()->NewNumber(value);
3328     }
3329
3330     case LITERAL:
3331       return literal_;
3332
3333     default:
3334       FATAL("We should never get here - unexpected deopt info.");
3335       return Handle<Object>::null();
3336   }
3337 }
3338
3339
3340 void SlotRefValueBuilder::Prepare(Isolate* isolate) {
3341   MaterializedObjectStore* materialized_store =
3342       isolate->materialized_object_store();
3343   previously_materialized_objects_ = materialized_store->Get(stack_frame_id_);
3344   prev_materialized_count_ = previously_materialized_objects_.is_null()
3345       ? 0 : previously_materialized_objects_->length();
3346
3347   // Skip any materialized objects of the inlined "parent" frames.
3348   // (Note that we still need to materialize them because they might be
3349   // referred to as duplicated objects.)
3350   while (current_slot_ < first_slot_index_) {
3351     GetNext(isolate, 0);
3352   }
3353   CHECK_EQ(current_slot_, first_slot_index_);
3354 }
3355
3356
3357 Handle<Object> SlotRefValueBuilder::GetPreviouslyMaterialized(
3358     Isolate* isolate, int length) {
3359   int object_index = materialized_objects_.length();
3360   Handle<Object> return_value = Handle<Object>(
3361       previously_materialized_objects_->get(object_index), isolate);
3362   materialized_objects_.Add(return_value);
3363
3364   // Now need to skip all the nested objects (and possibly read them from
3365   // the materialization store, too).
3366   for (int i = 0; i < length; i++) {
3367     SlotRef& slot = slot_refs_[current_slot_];
3368     current_slot_++;
3369
3370     // We need to read all the nested objects - add them to the
3371     // number of objects we need to process.
3372     length += slot.GetChildrenCount();
3373
3374     // Put the nested deferred/duplicate objects into our materialization
3375     // array.
3376     if (slot.Representation() == SlotRef::DEFERRED_OBJECT ||
3377         slot.Representation() == SlotRef::DUPLICATE_OBJECT) {
3378       int nested_object_index = materialized_objects_.length();
3379       Handle<Object> nested_object = Handle<Object>(
3380           previously_materialized_objects_->get(nested_object_index),
3381           isolate);
3382       materialized_objects_.Add(nested_object);
3383     }
3384   }
3385
3386   return return_value;
3387 }
3388
3389
3390 Handle<Object> SlotRefValueBuilder::GetNext(Isolate* isolate, int lvl) {
3391   SlotRef& slot = slot_refs_[current_slot_];
3392   current_slot_++;
3393   switch (slot.Representation()) {
3394     case SlotRef::TAGGED:
3395     case SlotRef::INT32:
3396     case SlotRef::UINT32:
3397     case SlotRef::DOUBLE:
3398     case SlotRef::LITERAL:
3399       return slot.GetValue(isolate);
3400
3401     case SlotRef::ARGUMENTS_OBJECT: {
3402       // We should never need to materialize an arguments object,
3403       // but we still need to put something into the array
3404       // so that the indexing is consistent.
3405       materialized_objects_.Add(isolate->factory()->undefined_value());
3406       int length = slot.GetChildrenCount();
3407       for (int i = 0; i < length; ++i) {
3408         // We don't need the argument, just ignore it
3409         GetNext(isolate, lvl + 1);
3410       }
3411       return isolate->factory()->undefined_value();
3412     }
3413     case SlotRef::DEFERRED_OBJECT: {
3414       int length = slot.GetChildrenCount();
3415       CHECK(slot_refs_[current_slot_].Representation() == SlotRef::LITERAL ||
3416             slot_refs_[current_slot_].Representation() == SlotRef::TAGGED);
3417
3418       int object_index = materialized_objects_.length();
3419       if (object_index <  prev_materialized_count_) {
3420         return GetPreviouslyMaterialized(isolate, length);
3421       }
3422
3423       Handle<Object> map_object = slot_refs_[current_slot_].GetValue(isolate);
3424       Handle<Map> map = Map::GeneralizeAllFieldRepresentations(
3425           Handle<Map>::cast(map_object));
3426       current_slot_++;
3427       // TODO(jarin) this should be unified with the code in
3428       // Deoptimizer::MaterializeNextHeapObject()
3429       switch (map->instance_type()) {
3430         case MUTABLE_HEAP_NUMBER_TYPE:
3431         case HEAP_NUMBER_TYPE: {
3432           // Reuse the HeapNumber value directly as it is already properly
3433           // tagged and skip materializing the HeapNumber explicitly.
3434           Handle<Object> object = GetNext(isolate, lvl + 1);
3435           materialized_objects_.Add(object);
3436           // On 32-bit architectures, there is an extra slot there because
3437           // the escape analysis calculates the number of slots as
3438           // object-size/pointer-size. To account for this, we read out
3439           // any extra slots.
3440           for (int i = 0; i < length - 2; i++) {
3441             GetNext(isolate, lvl + 1);
3442           }
3443           return object;
3444         }
3445         case JS_OBJECT_TYPE: {
3446           Handle<JSObject> object =
3447               isolate->factory()->NewJSObjectFromMap(map, NOT_TENURED, false);
3448           materialized_objects_.Add(object);
3449           Handle<Object> properties = GetNext(isolate, lvl + 1);
3450           Handle<Object> elements = GetNext(isolate, lvl + 1);
3451           object->set_properties(FixedArray::cast(*properties));
3452           object->set_elements(FixedArrayBase::cast(*elements));
3453           for (int i = 0; i < length - 3; ++i) {
3454             Handle<Object> value = GetNext(isolate, lvl + 1);
3455             FieldIndex index = FieldIndex::ForPropertyIndex(object->map(), i);
3456             object->FastPropertyAtPut(index, *value);
3457           }
3458           return object;
3459         }
3460         case JS_ARRAY_TYPE: {
3461           Handle<JSArray> object =
3462               isolate->factory()->NewJSArray(0, map->elements_kind());
3463           materialized_objects_.Add(object);
3464           Handle<Object> properties = GetNext(isolate, lvl + 1);
3465           Handle<Object> elements = GetNext(isolate, lvl + 1);
3466           Handle<Object> length = GetNext(isolate, lvl + 1);
3467           object->set_properties(FixedArray::cast(*properties));
3468           object->set_elements(FixedArrayBase::cast(*elements));
3469           object->set_length(*length);
3470           return object;
3471         }
3472         default:
3473           PrintF(stderr,
3474                  "[couldn't handle instance type %d]\n", map->instance_type());
3475           UNREACHABLE();
3476           break;
3477       }
3478       UNREACHABLE();
3479       break;
3480     }
3481
3482     case SlotRef::DUPLICATE_OBJECT: {
3483       int object_index = slot.DuplicateObjectId();
3484       Handle<Object> object = materialized_objects_[object_index];
3485       materialized_objects_.Add(object);
3486       return object;
3487     }
3488     default:
3489       UNREACHABLE();
3490       break;
3491   }
3492
3493   FATAL("We should never get here - unexpected deopt slot kind.");
3494   return Handle<Object>::null();
3495 }
3496
3497
3498 void SlotRefValueBuilder::Finish(Isolate* isolate) {
3499   // We should have processed all the slots
3500   CHECK_EQ(slot_refs_.length(), current_slot_);
3501
3502   if (should_deoptimize_ &&
3503       materialized_objects_.length() > prev_materialized_count_) {
3504     // We have materialized some new objects and they might be accessible
3505     // from the arguments object, so we have to store them
3506     // to prevent duplicate materialization.
3507     Handle<FixedArray> array = isolate->factory()->NewFixedArray(
3508         materialized_objects_.length());
3509     for (int i = 0; i < materialized_objects_.length(); i++) {
3510       array->set(i, *(materialized_objects_.at(i)));
3511     }
3512     isolate->materialized_object_store()->Set(stack_frame_id_, array);
3513   }
3514 }
3515
3516
3517 Handle<FixedArray> MaterializedObjectStore::Get(Address fp) {
3518   int index = StackIdToIndex(fp);
3519   if (index == -1) {
3520     return Handle<FixedArray>::null();
3521   }
3522   Handle<FixedArray> array = GetStackEntries();
3523   CHECK_GT(array->length(), index);
3524   return Handle<FixedArray>::cast(Handle<Object>(array->get(index),
3525                                                  isolate()));
3526 }
3527
3528
3529 void MaterializedObjectStore::Set(Address fp,
3530     Handle<FixedArray> materialized_objects) {
3531   int index = StackIdToIndex(fp);
3532   if (index == -1) {
3533     index = frame_fps_.length();
3534     frame_fps_.Add(fp);
3535   }
3536
3537   Handle<FixedArray> array = EnsureStackEntries(index + 1);
3538   array->set(index, *materialized_objects);
3539 }
3540
3541
3542 void MaterializedObjectStore::Remove(Address fp) {
3543   int index = StackIdToIndex(fp);
3544   CHECK_GE(index, 0);
3545
3546   frame_fps_.Remove(index);
3547   Handle<FixedArray> array = GetStackEntries();
3548   CHECK_LT(index, array->length());
3549   for (int i = index; i < frame_fps_.length(); i++) {
3550     array->set(i, array->get(i + 1));
3551   }
3552   array->set(frame_fps_.length(), isolate()->heap()->undefined_value());
3553 }
3554
3555
3556 int MaterializedObjectStore::StackIdToIndex(Address fp) {
3557   for (int i = 0; i < frame_fps_.length(); i++) {
3558     if (frame_fps_[i] == fp) {
3559       return i;
3560     }
3561   }
3562   return -1;
3563 }
3564
3565
3566 Handle<FixedArray> MaterializedObjectStore::GetStackEntries() {
3567   return Handle<FixedArray>(isolate()->heap()->materialized_objects());
3568 }
3569
3570
3571 Handle<FixedArray> MaterializedObjectStore::EnsureStackEntries(int length) {
3572   Handle<FixedArray> array = GetStackEntries();
3573   if (array->length() >= length) {
3574     return array;
3575   }
3576
3577   int new_length = length > 10 ? length : 10;
3578   if (new_length < 2 * array->length()) {
3579     new_length = 2 * array->length();
3580   }
3581
3582   Handle<FixedArray> new_array =
3583       isolate()->factory()->NewFixedArray(new_length, TENURED);
3584   for (int i = 0; i < array->length(); i++) {
3585     new_array->set(i, array->get(i));
3586   }
3587   for (int i = array->length(); i < length; i++) {
3588     new_array->set(i, isolate()->heap()->undefined_value());
3589   }
3590   isolate()->heap()->public_set_materialized_objects(*new_array);
3591   return new_array;
3592 }
3593
3594
3595 DeoptimizedFrameInfo::DeoptimizedFrameInfo(Deoptimizer* deoptimizer,
3596                                            int frame_index,
3597                                            bool has_arguments_adaptor,
3598                                            bool has_construct_stub) {
3599   FrameDescription* output_frame = deoptimizer->output_[frame_index];
3600   function_ = output_frame->GetFunction();
3601   context_ = reinterpret_cast<Object*>(output_frame->GetContext());
3602   has_construct_stub_ = has_construct_stub;
3603   expression_count_ = output_frame->GetExpressionCount();
3604   expression_stack_ = new Object*[expression_count_];
3605   // Get the source position using the unoptimized code.
3606   Address pc = reinterpret_cast<Address>(output_frame->GetPc());
3607   Code* code = Code::cast(deoptimizer->isolate()->FindCodeObject(pc));
3608   source_position_ = code->SourcePosition(pc);
3609
3610   for (int i = 0; i < expression_count_; i++) {
3611     SetExpression(i, output_frame->GetExpression(i));
3612   }
3613
3614   if (has_arguments_adaptor) {
3615     output_frame = deoptimizer->output_[frame_index - 1];
3616     CHECK_EQ(output_frame->GetFrameType(), StackFrame::ARGUMENTS_ADAPTOR);
3617   }
3618
3619   parameters_count_ = output_frame->ComputeParametersCount();
3620   parameters_ = new Object*[parameters_count_];
3621   for (int i = 0; i < parameters_count_; i++) {
3622     SetParameter(i, output_frame->GetParameter(i));
3623   }
3624 }
3625
3626
3627 DeoptimizedFrameInfo::~DeoptimizedFrameInfo() {
3628   delete[] expression_stack_;
3629   delete[] parameters_;
3630 }
3631
3632
3633 void DeoptimizedFrameInfo::Iterate(ObjectVisitor* v) {
3634   v->VisitPointer(bit_cast<Object**>(&function_));
3635   v->VisitPointer(&context_);
3636   v->VisitPointers(parameters_, parameters_ + parameters_count_);
3637   v->VisitPointers(expression_stack_, expression_stack_ + expression_count_);
3638 }
3639
3640
3641 const char* Deoptimizer::GetDeoptReason(DeoptReason deopt_reason) {
3642   DCHECK(deopt_reason < kLastDeoptReason);
3643 #define DEOPT_MESSAGES_TEXTS(C, T) T,
3644   static const char* deopt_messages_[] = {
3645       DEOPT_MESSAGES_LIST(DEOPT_MESSAGES_TEXTS)};
3646 #undef DEOPT_MESSAGES_TEXTS
3647   return deopt_messages_[deopt_reason];
3648 }
3649
3650
3651 Deoptimizer::DeoptInfo Deoptimizer::GetDeoptInfo(Code* code, int bailout_id) {
3652   int last_position = 0;
3653   Isolate* isolate = code->GetIsolate();
3654   Deoptimizer::DeoptReason last_reason = Deoptimizer::kNoReason;
3655   int mask = RelocInfo::ModeMask(RelocInfo::DEOPT_REASON) |
3656              RelocInfo::ModeMask(RelocInfo::POSITION) |
3657              RelocInfo::ModeMask(RelocInfo::RUNTIME_ENTRY);
3658   for (RelocIterator it(code, mask); !it.done(); it.next()) {
3659     RelocInfo* info = it.rinfo();
3660     if (info->rmode() == RelocInfo::POSITION) {
3661       last_position = static_cast<int>(info->data());
3662     } else if (info->rmode() == RelocInfo::DEOPT_REASON) {
3663       last_reason = static_cast<Deoptimizer::DeoptReason>(info->data());
3664     } else if (last_reason != Deoptimizer::kNoReason) {
3665       if ((bailout_id ==
3666            Deoptimizer::GetDeoptimizationId(isolate, info->target_address(),
3667                                             Deoptimizer::EAGER)) ||
3668           (bailout_id ==
3669            Deoptimizer::GetDeoptimizationId(isolate, info->target_address(),
3670                                             Deoptimizer::SOFT)) ||
3671           (bailout_id ==
3672            Deoptimizer::GetDeoptimizationId(isolate, info->target_address(),
3673                                             Deoptimizer::LAZY))) {
3674         CHECK(RelocInfo::IsRuntimeEntry(info->rmode()));
3675         return DeoptInfo(last_position, NULL, last_reason);
3676       }
3677     }
3678   }
3679   return DeoptInfo(0, NULL, Deoptimizer::kNoReason);
3680 }
3681 } }  // namespace v8::internal