1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided
11 // with the distribution.
12 // * Neither the name of Google Inc. nor the names of its
13 // contributors may be used to endorse or promote products derived
14 // from this software without specific prior written permission.
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #include "deoptimizer.h"
33 #include "full-codegen.h"
34 #include "global-handles.h"
35 #include "macro-assembler.h"
36 #include "prettyprinter.h"
42 DeoptimizerData::DeoptimizerData() {
43 eager_deoptimization_entry_code_ = NULL;
44 lazy_deoptimization_entry_code_ = NULL;
46 deoptimizing_code_list_ = NULL;
47 #ifdef ENABLE_DEBUGGER_SUPPORT
48 deoptimized_frame_info_ = NULL;
53 DeoptimizerData::~DeoptimizerData() {
54 if (eager_deoptimization_entry_code_ != NULL) {
55 Isolate::Current()->memory_allocator()->Free(
56 eager_deoptimization_entry_code_);
57 eager_deoptimization_entry_code_ = NULL;
59 if (lazy_deoptimization_entry_code_ != NULL) {
60 Isolate::Current()->memory_allocator()->Free(
61 lazy_deoptimization_entry_code_);
62 lazy_deoptimization_entry_code_ = NULL;
67 #ifdef ENABLE_DEBUGGER_SUPPORT
68 void DeoptimizerData::Iterate(ObjectVisitor* v) {
69 if (deoptimized_frame_info_ != NULL) {
70 deoptimized_frame_info_->Iterate(v);
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,
84 ASSERT(isolate == Isolate::Current());
85 Deoptimizer* deoptimizer = new Deoptimizer(isolate,
92 ASSERT(isolate->deoptimizer_data()->current_ == NULL);
93 isolate->deoptimizer_data()->current_ = deoptimizer;
98 Deoptimizer* Deoptimizer::Grab(Isolate* isolate) {
99 ASSERT(isolate == Isolate::Current());
100 Deoptimizer* result = isolate->deoptimizer_data()->current_;
101 ASSERT(result != NULL);
102 result->DeleteFrameDescriptions();
103 isolate->deoptimizer_data()->current_ = NULL;
108 int Deoptimizer::ConvertJSFrameIndexToFrameIndex(int jsframe_index) {
109 if (jsframe_index == 0) return 0;
112 while (jsframe_index >= 0) {
113 FrameDescription* frame = output_[frame_index];
114 if (frame->GetFrameType() == StackFrame::JAVA_SCRIPT) {
120 return frame_index - 1;
124 #ifdef ENABLE_DEBUGGER_SUPPORT
125 DeoptimizedFrameInfo* Deoptimizer::DebuggerInspectableFrame(
126 JavaScriptFrame* frame,
129 ASSERT(isolate == Isolate::Current());
130 ASSERT(frame->is_optimized());
131 ASSERT(isolate->deoptimizer_data()->deoptimized_frame_info_ == NULL);
133 // Get the function and code from the frame.
134 JSFunction* function = JSFunction::cast(frame->function());
135 Code* code = frame->LookupCode();
137 // Locate the deoptimization point in the code. As we are at a call the
138 // return address must be at a place in the code with deoptimization support.
139 SafepointEntry safepoint_entry = code->GetSafepointEntry(frame->pc());
140 int deoptimization_index = safepoint_entry.deoptimization_index();
141 ASSERT(deoptimization_index != Safepoint::kNoDeoptimizationIndex);
143 // Always use the actual stack slots when calculating the fp to sp
144 // delta adding two for the function and context.
145 unsigned stack_slots = code->stack_slots();
146 unsigned fp_to_sp_delta = ((stack_slots + 2) * kPointerSize);
148 Deoptimizer* deoptimizer = new Deoptimizer(isolate,
150 Deoptimizer::DEBUGGER,
151 deoptimization_index,
155 Address tos = frame->fp() - fp_to_sp_delta;
156 deoptimizer->FillInputFrame(tos, frame);
158 // Calculate the output frames.
159 Deoptimizer::ComputeOutputFrames(deoptimizer);
161 // Create the GC safe output frame information and register it for GC
163 ASSERT_LT(jsframe_index, deoptimizer->jsframe_count());
165 // Convert JS frame index into frame index.
166 int frame_index = deoptimizer->ConvertJSFrameIndexToFrameIndex(jsframe_index);
168 bool has_arguments_adaptor =
170 deoptimizer->output_[frame_index - 1]->GetFrameType() ==
171 StackFrame::ARGUMENTS_ADAPTOR;
173 int construct_offset = has_arguments_adaptor ? 2 : 1;
174 bool has_construct_stub =
175 frame_index >= construct_offset &&
176 deoptimizer->output_[frame_index - construct_offset]->GetFrameType() ==
177 StackFrame::CONSTRUCT;
179 DeoptimizedFrameInfo* info = new DeoptimizedFrameInfo(deoptimizer,
181 has_arguments_adaptor,
183 isolate->deoptimizer_data()->deoptimized_frame_info_ = info;
185 // Get the "simulated" top and size for the requested frame.
186 FrameDescription* parameters_frame =
187 deoptimizer->output_[
188 has_arguments_adaptor ? (frame_index - 1) : frame_index];
190 uint32_t parameters_size = (info->parameters_count() + 1) * kPointerSize;
191 Address parameters_top = reinterpret_cast<Address>(
192 parameters_frame->GetTop() + (parameters_frame->GetFrameSize() -
195 uint32_t expressions_size = info->expression_count() * kPointerSize;
196 Address expressions_top = reinterpret_cast<Address>(
197 deoptimizer->output_[frame_index]->GetTop());
199 // Done with the GC-unsafe frame descriptions. This re-enables allocation.
200 deoptimizer->DeleteFrameDescriptions();
202 // Allocate a heap number for the doubles belonging to this frame.
203 deoptimizer->MaterializeHeapNumbersForDebuggerInspectableFrame(
204 parameters_top, parameters_size, expressions_top, expressions_size, info);
206 // Finished using the deoptimizer instance.
213 void Deoptimizer::DeleteDebuggerInspectableFrame(DeoptimizedFrameInfo* info,
215 ASSERT(isolate == Isolate::Current());
216 ASSERT(isolate->deoptimizer_data()->deoptimized_frame_info_ == info);
218 isolate->deoptimizer_data()->deoptimized_frame_info_ = NULL;
222 void Deoptimizer::GenerateDeoptimizationEntries(MacroAssembler* masm,
225 TableEntryGenerator generator(masm, type, count);
226 generator.Generate();
230 class DeoptimizingVisitor : public OptimizedFunctionVisitor {
232 virtual void EnterContext(Context* context) {
233 if (FLAG_trace_deopt) {
234 PrintF("[deoptimize context: %" V8PRIxPTR "]\n",
235 reinterpret_cast<intptr_t>(context));
239 virtual void VisitFunction(JSFunction* function) {
240 Deoptimizer::DeoptimizeFunction(function);
243 virtual void LeaveContext(Context* context) {
244 context->ClearOptimizedFunctions();
249 void Deoptimizer::DeoptimizeAll() {
250 AssertNoAllocation no_allocation;
252 if (FLAG_trace_deopt) {
253 PrintF("[deoptimize all contexts]\n");
256 DeoptimizingVisitor visitor;
257 VisitAllOptimizedFunctions(&visitor);
261 void Deoptimizer::DeoptimizeGlobalObject(JSObject* object) {
262 AssertNoAllocation no_allocation;
264 DeoptimizingVisitor visitor;
265 VisitAllOptimizedFunctionsForGlobalObject(object, &visitor);
269 void Deoptimizer::VisitAllOptimizedFunctionsForContext(
270 Context* context, OptimizedFunctionVisitor* visitor) {
271 AssertNoAllocation no_allocation;
273 ASSERT(context->IsGlobalContext());
275 visitor->EnterContext(context);
276 // Run through the list of optimized functions and deoptimize them.
277 Object* element = context->OptimizedFunctionsListHead();
278 while (!element->IsUndefined()) {
279 JSFunction* element_function = JSFunction::cast(element);
280 // Get the next link before deoptimizing as deoptimizing will clear the
282 element = element_function->next_function_link();
283 visitor->VisitFunction(element_function);
285 visitor->LeaveContext(context);
289 void Deoptimizer::VisitAllOptimizedFunctionsForGlobalObject(
290 JSObject* object, OptimizedFunctionVisitor* visitor) {
291 AssertNoAllocation no_allocation;
293 if (object->IsJSGlobalProxy()) {
294 Object* proto = object->GetPrototype();
295 ASSERT(proto->IsJSGlobalObject());
296 VisitAllOptimizedFunctionsForContext(
297 GlobalObject::cast(proto)->global_context(), visitor);
298 } else if (object->IsGlobalObject()) {
299 VisitAllOptimizedFunctionsForContext(
300 GlobalObject::cast(object)->global_context(), visitor);
305 void Deoptimizer::VisitAllOptimizedFunctions(
306 OptimizedFunctionVisitor* visitor) {
307 AssertNoAllocation no_allocation;
309 // Run through the list of all global contexts and deoptimize.
310 Object* context = Isolate::Current()->heap()->global_contexts_list();
311 while (!context->IsUndefined()) {
312 // GC can happen when the context is not fully initialized,
313 // so the global field of the context can be undefined.
314 Object* global = Context::cast(context)->get(Context::GLOBAL_INDEX);
315 if (!global->IsUndefined()) {
316 VisitAllOptimizedFunctionsForGlobalObject(JSObject::cast(global),
319 context = Context::cast(context)->get(Context::NEXT_CONTEXT_LINK);
324 void Deoptimizer::HandleWeakDeoptimizedCode(
325 v8::Persistent<v8::Value> obj, void* data) {
326 DeoptimizingCodeListNode* node =
327 reinterpret_cast<DeoptimizingCodeListNode*>(data);
328 RemoveDeoptimizingCode(*node->code());
330 node = Isolate::Current()->deoptimizer_data()->deoptimizing_code_list_;
331 while (node != NULL) {
332 ASSERT(node != reinterpret_cast<DeoptimizingCodeListNode*>(data));
339 void Deoptimizer::ComputeOutputFrames(Deoptimizer* deoptimizer) {
340 deoptimizer->DoComputeOutputFrames();
344 Deoptimizer::Deoptimizer(Isolate* isolate,
345 JSFunction* function,
350 Code* optimized_code)
353 bailout_id_(bailout_id),
356 fp_to_sp_delta_(fp_to_sp_delta),
361 deferred_heap_numbers_(0) {
362 if (FLAG_trace_deopt && type != OSR) {
363 if (type == DEBUGGER) {
364 PrintF("**** DEOPT FOR DEBUGGER: ");
366 PrintF("**** DEOPT: ");
368 function->PrintName();
369 PrintF(" at bailout #%u, address 0x%" V8PRIxPTR ", frame size %d\n",
371 reinterpret_cast<intptr_t>(from),
372 fp_to_sp_delta - (2 * kPointerSize));
373 } else if (FLAG_trace_osr && type == OSR) {
374 PrintF("**** OSR: ");
375 function->PrintName();
376 PrintF(" at ast id #%u, address 0x%" V8PRIxPTR ", frame size %d\n",
378 reinterpret_cast<intptr_t>(from),
379 fp_to_sp_delta - (2 * kPointerSize));
381 // Find the optimized code.
383 ASSERT(from == NULL);
384 optimized_code_ = function_->code();
385 if (FLAG_trace_deopt && FLAG_code_comments) {
386 // Print instruction associated with this bailout.
387 const char* last_comment = NULL;
388 int mask = RelocInfo::ModeMask(RelocInfo::COMMENT)
389 | RelocInfo::ModeMask(RelocInfo::RUNTIME_ENTRY);
390 for (RelocIterator it(optimized_code_, mask); !it.done(); it.next()) {
391 RelocInfo* info = it.rinfo();
392 if (info->rmode() == RelocInfo::COMMENT) {
393 last_comment = reinterpret_cast<const char*>(info->data());
395 if (info->rmode() == RelocInfo::RUNTIME_ENTRY) {
396 unsigned id = Deoptimizer::GetDeoptimizationId(
397 info->target_address(), Deoptimizer::EAGER);
398 if (id == bailout_id && last_comment != NULL) {
399 PrintF(" %s\n", last_comment);
405 } else if (type == LAZY) {
406 optimized_code_ = FindDeoptimizingCodeFromAddress(from);
407 ASSERT(optimized_code_ != NULL);
408 } else if (type == OSR) {
409 // The function has already been optimized and we're transitioning
410 // from the unoptimized shared version to the optimized one in the
411 // function. The return address (from) points to unoptimized code.
412 optimized_code_ = function_->code();
413 ASSERT(optimized_code_->kind() == Code::OPTIMIZED_FUNCTION);
414 ASSERT(!optimized_code_->contains(from));
415 } else if (type == DEBUGGER) {
416 optimized_code_ = optimized_code;
417 ASSERT(optimized_code_->contains(from));
419 ASSERT(HEAP->allow_allocation(false));
420 unsigned size = ComputeInputFrameSize();
421 input_ = new(size) FrameDescription(size, function);
422 input_->SetFrameType(StackFrame::JAVA_SCRIPT);
426 Deoptimizer::~Deoptimizer() {
427 ASSERT(input_ == NULL && output_ == NULL);
431 void Deoptimizer::DeleteFrameDescriptions() {
433 for (int i = 0; i < output_count_; ++i) {
434 if (output_[i] != input_) delete output_[i];
439 ASSERT(!HEAP->allow_allocation(true));
443 Address Deoptimizer::GetDeoptimizationEntry(int id, BailoutType type) {
445 if (id >= kNumberOfEntries) return NULL;
446 MemoryChunk* base = NULL;
447 DeoptimizerData* data = Isolate::Current()->deoptimizer_data();
449 if (data->eager_deoptimization_entry_code_ == NULL) {
450 data->eager_deoptimization_entry_code_ = CreateCode(type);
452 base = data->eager_deoptimization_entry_code_;
454 if (data->lazy_deoptimization_entry_code_ == NULL) {
455 data->lazy_deoptimization_entry_code_ = CreateCode(type);
457 base = data->lazy_deoptimization_entry_code_;
460 static_cast<Address>(base->area_start()) + (id * table_entry_size_);
464 int Deoptimizer::GetDeoptimizationId(Address addr, BailoutType type) {
465 MemoryChunk* base = NULL;
466 DeoptimizerData* data = Isolate::Current()->deoptimizer_data();
468 base = data->eager_deoptimization_entry_code_;
470 base = data->lazy_deoptimization_entry_code_;
473 addr < base->area_start() ||
474 addr >= base->area_start() +
475 (kNumberOfEntries * table_entry_size_)) {
476 return kNotDeoptimizationEntry;
479 static_cast<int>(addr - base->area_start()) % table_entry_size_);
480 return static_cast<int>(addr - base->area_start()) / table_entry_size_;
484 int Deoptimizer::GetOutputInfo(DeoptimizationOutputData* data,
486 SharedFunctionInfo* shared) {
487 // TODO(kasperl): For now, we do a simple linear search for the PC
488 // offset associated with the given node id. This should probably be
489 // changed to a binary search.
490 int length = data->DeoptPoints();
491 Smi* smi_id = Smi::FromInt(id);
492 for (int i = 0; i < length; i++) {
493 if (data->AstId(i) == smi_id) {
494 return data->PcAndState(i)->value();
497 PrintF("[couldn't find pc offset for node=%u]\n", id);
498 PrintF("[method: %s]\n", *shared->DebugName()->ToCString());
499 // Print the source code if available.
500 HeapStringAllocator string_allocator;
501 StringStream stream(&string_allocator);
502 shared->SourceCodePrint(&stream, -1);
503 PrintF("[source:\n%s\n]", *stream.ToCString());
510 int Deoptimizer::GetDeoptimizedCodeCount(Isolate* isolate) {
512 DeoptimizingCodeListNode* node =
513 isolate->deoptimizer_data()->deoptimizing_code_list_;
514 while (node != NULL) {
522 // We rely on this function not causing a GC. It is called from generated code
523 // without having a real stack frame in place.
524 void Deoptimizer::DoComputeOutputFrames() {
525 if (bailout_type_ == OSR) {
526 DoComputeOsrOutputFrame();
530 // Print some helpful diagnostic information.
531 int64_t start = OS::Ticks();
532 if (FLAG_trace_deopt) {
533 PrintF("[deoptimizing%s: begin 0x%08" V8PRIxPTR " ",
534 (bailout_type_ == LAZY ? " (lazy)" : ""),
535 reinterpret_cast<intptr_t>(function_));
536 function_->PrintName();
537 PrintF(" @%d]\n", bailout_id_);
540 // Determine basic deoptimization information. The optimized frame is
541 // described by the input data.
542 DeoptimizationInputData* input_data =
543 DeoptimizationInputData::cast(optimized_code_->deoptimization_data());
544 unsigned node_id = input_data->AstId(bailout_id_)->value();
545 ByteArray* translations = input_data->TranslationByteArray();
546 unsigned translation_index =
547 input_data->TranslationIndex(bailout_id_)->value();
549 // Do the input frame to output frame(s) translation.
550 TranslationIterator iterator(translations, translation_index);
551 Translation::Opcode opcode =
552 static_cast<Translation::Opcode>(iterator.Next());
553 ASSERT(Translation::BEGIN == opcode);
555 // Read the number of output frames and allocate an array for their
557 int count = iterator.Next();
558 iterator.Next(); // Drop JS frames count.
559 ASSERT(output_ == NULL);
560 output_ = new FrameDescription*[count];
561 for (int i = 0; i < count; ++i) {
564 output_count_ = count;
566 // Translate each output frame.
567 for (int i = 0; i < count; ++i) {
568 // Read the ast node id, function, and frame height for this output frame.
569 Translation::Opcode opcode =
570 static_cast<Translation::Opcode>(iterator.Next());
572 case Translation::JS_FRAME:
573 DoComputeJSFrame(&iterator, i);
576 case Translation::ARGUMENTS_ADAPTOR_FRAME:
577 DoComputeArgumentsAdaptorFrame(&iterator, i);
579 case Translation::CONSTRUCT_STUB_FRAME:
580 DoComputeConstructStubFrame(&iterator, i);
588 // Print some helpful diagnostic information.
589 if (FLAG_trace_deopt) {
590 double ms = static_cast<double>(OS::Ticks() - start) / 1000;
591 int index = output_count_ - 1; // Index of the topmost frame.
592 JSFunction* function = output_[index]->GetFunction();
593 PrintF("[deoptimizing: end 0x%08" V8PRIxPTR " ",
594 reinterpret_cast<intptr_t>(function));
595 function->PrintName();
596 PrintF(" => node=%u, pc=0x%08" V8PRIxPTR ", state=%s, took %0.3f ms]\n",
598 output_[index]->GetPc(),
599 FullCodeGenerator::State2String(
600 static_cast<FullCodeGenerator::State>(
601 output_[index]->GetState()->value())),
607 void Deoptimizer::MaterializeHeapNumbers() {
608 ASSERT_NE(DEBUGGER, bailout_type_);
609 for (int i = 0; i < deferred_heap_numbers_.length(); i++) {
610 HeapNumberMaterializationDescriptor d = deferred_heap_numbers_[i];
611 Handle<Object> num = isolate_->factory()->NewNumber(d.value());
612 if (FLAG_trace_deopt) {
613 PrintF("Materializing a new heap number %p [%e] in slot %p\n",
614 reinterpret_cast<void*>(*num),
619 Memory::Object_at(d.slot_address()) = *num;
624 #ifdef ENABLE_DEBUGGER_SUPPORT
625 void Deoptimizer::MaterializeHeapNumbersForDebuggerInspectableFrame(
626 Address parameters_top,
627 uint32_t parameters_size,
628 Address expressions_top,
629 uint32_t expressions_size,
630 DeoptimizedFrameInfo* info) {
631 ASSERT_EQ(DEBUGGER, bailout_type_);
632 Address parameters_bottom = parameters_top + parameters_size;
633 Address expressions_bottom = expressions_top + expressions_size;
634 for (int i = 0; i < deferred_heap_numbers_.length(); i++) {
635 HeapNumberMaterializationDescriptor d = deferred_heap_numbers_[i];
637 // Check of the heap number to materialize actually belong to the frame
639 Address slot = d.slot_address();
640 if (parameters_top <= slot && slot < parameters_bottom) {
641 Handle<Object> num = isolate_->factory()->NewNumber(d.value());
643 int index = (info->parameters_count() - 1) -
644 static_cast<int>(slot - parameters_top) / kPointerSize;
646 if (FLAG_trace_deopt) {
647 PrintF("Materializing a new heap number %p [%e] in slot %p"
648 "for parameter slot #%d\n",
649 reinterpret_cast<void*>(*num),
655 info->SetParameter(index, *num);
656 } else if (expressions_top <= slot && slot < expressions_bottom) {
657 Handle<Object> num = isolate_->factory()->NewNumber(d.value());
659 int index = info->expression_count() - 1 -
660 static_cast<int>(slot - expressions_top) / kPointerSize;
662 if (FLAG_trace_deopt) {
663 PrintF("Materializing a new heap number %p [%e] in slot %p"
664 "for expression slot #%d\n",
665 reinterpret_cast<void*>(*num),
671 info->SetExpression(index, *num);
678 void Deoptimizer::DoTranslateCommand(TranslationIterator* iterator,
680 unsigned output_offset) {
681 disasm::NameConverter converter;
682 // A GC-safe temporary placeholder that we can put in the output frame.
683 const intptr_t kPlaceholder = reinterpret_cast<intptr_t>(Smi::FromInt(0));
685 // Ignore commands marked as duplicate and act on the first non-duplicate.
686 Translation::Opcode opcode =
687 static_cast<Translation::Opcode>(iterator->Next());
688 while (opcode == Translation::DUPLICATE) {
689 opcode = static_cast<Translation::Opcode>(iterator->Next());
690 iterator->Skip(Translation::NumberOfOperandsFor(opcode));
691 opcode = static_cast<Translation::Opcode>(iterator->Next());
695 case Translation::BEGIN:
696 case Translation::JS_FRAME:
697 case Translation::ARGUMENTS_ADAPTOR_FRAME:
698 case Translation::CONSTRUCT_STUB_FRAME:
699 case Translation::DUPLICATE:
703 case Translation::REGISTER: {
704 int input_reg = iterator->Next();
705 intptr_t input_value = input_->GetRegister(input_reg);
706 if (FLAG_trace_deopt) {
708 " 0x%08" V8PRIxPTR ": [top + %d] <- 0x%08" V8PRIxPTR " ; %s ",
709 output_[frame_index]->GetTop() + output_offset,
712 converter.NameOfCPURegister(input_reg));
713 reinterpret_cast<Object*>(input_value)->ShortPrint();
716 output_[frame_index]->SetFrameSlot(output_offset, input_value);
720 case Translation::INT32_REGISTER: {
721 int input_reg = iterator->Next();
722 intptr_t value = input_->GetRegister(input_reg);
723 bool is_smi = Smi::IsValid(value);
724 if (FLAG_trace_deopt) {
726 " 0x%08" V8PRIxPTR ": [top + %d] <- %" V8PRIdPTR " ; %s (%s)\n",
727 output_[frame_index]->GetTop() + output_offset,
730 converter.NameOfCPURegister(input_reg),
731 is_smi ? "smi" : "heap number");
734 intptr_t tagged_value =
735 reinterpret_cast<intptr_t>(Smi::FromInt(static_cast<int>(value)));
736 output_[frame_index]->SetFrameSlot(output_offset, tagged_value);
738 // We save the untagged value on the side and store a GC-safe
739 // temporary placeholder in the frame.
740 AddDoubleValue(output_[frame_index]->GetTop() + output_offset,
741 static_cast<double>(static_cast<int32_t>(value)));
742 output_[frame_index]->SetFrameSlot(output_offset, kPlaceholder);
747 case Translation::DOUBLE_REGISTER: {
748 int input_reg = iterator->Next();
749 double value = input_->GetDoubleRegister(input_reg);
750 if (FLAG_trace_deopt) {
751 PrintF(" 0x%08" V8PRIxPTR ": [top + %d] <- %e ; %s\n",
752 output_[frame_index]->GetTop() + output_offset,
755 DoubleRegister::AllocationIndexToString(input_reg));
757 // We save the untagged value on the side and store a GC-safe
758 // temporary placeholder in the frame.
759 AddDoubleValue(output_[frame_index]->GetTop() + output_offset, value);
760 output_[frame_index]->SetFrameSlot(output_offset, kPlaceholder);
764 case Translation::STACK_SLOT: {
765 int input_slot_index = iterator->Next();
766 unsigned input_offset =
767 input_->GetOffsetFromSlotIndex(input_slot_index);
768 intptr_t input_value = input_->GetFrameSlot(input_offset);
769 if (FLAG_trace_deopt) {
770 PrintF(" 0x%08" V8PRIxPTR ": ",
771 output_[frame_index]->GetTop() + output_offset);
772 PrintF("[top + %d] <- 0x%08" V8PRIxPTR " ; [esp + %d] ",
776 reinterpret_cast<Object*>(input_value)->ShortPrint();
779 output_[frame_index]->SetFrameSlot(output_offset, input_value);
783 case Translation::INT32_STACK_SLOT: {
784 int input_slot_index = iterator->Next();
785 unsigned input_offset =
786 input_->GetOffsetFromSlotIndex(input_slot_index);
787 intptr_t value = input_->GetFrameSlot(input_offset);
788 bool is_smi = Smi::IsValid(value);
789 if (FLAG_trace_deopt) {
790 PrintF(" 0x%08" V8PRIxPTR ": ",
791 output_[frame_index]->GetTop() + output_offset);
792 PrintF("[top + %d] <- %" V8PRIdPTR " ; [esp + %d] (%s)\n",
796 is_smi ? "smi" : "heap number");
799 intptr_t tagged_value =
800 reinterpret_cast<intptr_t>(Smi::FromInt(static_cast<int>(value)));
801 output_[frame_index]->SetFrameSlot(output_offset, tagged_value);
803 // We save the untagged value on the side and store a GC-safe
804 // temporary placeholder in the frame.
805 AddDoubleValue(output_[frame_index]->GetTop() + output_offset,
806 static_cast<double>(static_cast<int32_t>(value)));
807 output_[frame_index]->SetFrameSlot(output_offset, kPlaceholder);
812 case Translation::DOUBLE_STACK_SLOT: {
813 int input_slot_index = iterator->Next();
814 unsigned input_offset =
815 input_->GetOffsetFromSlotIndex(input_slot_index);
816 double value = input_->GetDoubleFrameSlot(input_offset);
817 if (FLAG_trace_deopt) {
818 PrintF(" 0x%08" V8PRIxPTR ": [top + %d] <- %e ; [esp + %d]\n",
819 output_[frame_index]->GetTop() + output_offset,
824 // We save the untagged value on the side and store a GC-safe
825 // temporary placeholder in the frame.
826 AddDoubleValue(output_[frame_index]->GetTop() + output_offset, value);
827 output_[frame_index]->SetFrameSlot(output_offset, kPlaceholder);
831 case Translation::LITERAL: {
832 Object* literal = ComputeLiteral(iterator->Next());
833 if (FLAG_trace_deopt) {
834 PrintF(" 0x%08" V8PRIxPTR ": [top + %d] <- ",
835 output_[frame_index]->GetTop() + output_offset,
837 literal->ShortPrint();
838 PrintF(" ; literal\n");
840 intptr_t value = reinterpret_cast<intptr_t>(literal);
841 output_[frame_index]->SetFrameSlot(output_offset, value);
845 case Translation::ARGUMENTS_OBJECT: {
846 // Use the arguments marker value as a sentinel and fill in the arguments
847 // object after the deoptimized frame is built.
848 if (FLAG_trace_deopt) {
849 PrintF(" 0x%08" V8PRIxPTR ": [top + %d] <- ",
850 output_[frame_index]->GetTop() + output_offset,
852 isolate_->heap()->arguments_marker()->ShortPrint();
853 PrintF(" ; arguments object\n");
855 intptr_t value = reinterpret_cast<intptr_t>(
856 isolate_->heap()->arguments_marker());
857 output_[frame_index]->SetFrameSlot(output_offset, value);
864 bool Deoptimizer::DoOsrTranslateCommand(TranslationIterator* iterator,
866 disasm::NameConverter converter;
867 FrameDescription* output = output_[0];
869 // The input values are all part of the unoptimized frame so they
870 // are all tagged pointers.
871 uintptr_t input_value = input_->GetFrameSlot(*input_offset);
872 Object* input_object = reinterpret_cast<Object*>(input_value);
874 Translation::Opcode opcode =
875 static_cast<Translation::Opcode>(iterator->Next());
876 bool duplicate = (opcode == Translation::DUPLICATE);
878 opcode = static_cast<Translation::Opcode>(iterator->Next());
882 case Translation::BEGIN:
883 case Translation::JS_FRAME:
884 case Translation::ARGUMENTS_ADAPTOR_FRAME:
885 case Translation::CONSTRUCT_STUB_FRAME:
886 case Translation::DUPLICATE:
887 UNREACHABLE(); // Malformed input.
890 case Translation::REGISTER: {
891 int output_reg = iterator->Next();
892 if (FLAG_trace_osr) {
893 PrintF(" %s <- 0x%08" V8PRIxPTR " ; [sp + %d]\n",
894 converter.NameOfCPURegister(output_reg),
898 output->SetRegister(output_reg, input_value);
902 case Translation::INT32_REGISTER: {
903 // Abort OSR if we don't have a number.
904 if (!input_object->IsNumber()) return false;
906 int output_reg = iterator->Next();
907 int int32_value = input_object->IsSmi()
908 ? Smi::cast(input_object)->value()
909 : FastD2I(input_object->Number());
910 // Abort the translation if the conversion lost information.
911 if (!input_object->IsSmi() &&
912 FastI2D(int32_value) != input_object->Number()) {
913 if (FLAG_trace_osr) {
914 PrintF("**** %g could not be converted to int32 ****\n",
915 input_object->Number());
919 if (FLAG_trace_osr) {
920 PrintF(" %s <- %d (int32) ; [sp + %d]\n",
921 converter.NameOfCPURegister(output_reg),
925 output->SetRegister(output_reg, int32_value);
929 case Translation::DOUBLE_REGISTER: {
930 // Abort OSR if we don't have a number.
931 if (!input_object->IsNumber()) return false;
933 int output_reg = iterator->Next();
934 double double_value = input_object->Number();
935 if (FLAG_trace_osr) {
936 PrintF(" %s <- %g (double) ; [sp + %d]\n",
937 DoubleRegister::AllocationIndexToString(output_reg),
941 output->SetDoubleRegister(output_reg, double_value);
945 case Translation::STACK_SLOT: {
946 int output_index = iterator->Next();
947 unsigned output_offset =
948 output->GetOffsetFromSlotIndex(output_index);
949 if (FLAG_trace_osr) {
950 PrintF(" [sp + %d] <- 0x%08" V8PRIxPTR " ; [sp + %d] ",
954 reinterpret_cast<Object*>(input_value)->ShortPrint();
957 output->SetFrameSlot(output_offset, input_value);
961 case Translation::INT32_STACK_SLOT: {
962 // Abort OSR if we don't have a number.
963 if (!input_object->IsNumber()) return false;
965 int output_index = iterator->Next();
966 unsigned output_offset =
967 output->GetOffsetFromSlotIndex(output_index);
968 int int32_value = input_object->IsSmi()
969 ? Smi::cast(input_object)->value()
970 : DoubleToInt32(input_object->Number());
971 // Abort the translation if the conversion lost information.
972 if (!input_object->IsSmi() &&
973 FastI2D(int32_value) != input_object->Number()) {
974 if (FLAG_trace_osr) {
975 PrintF("**** %g could not be converted to int32 ****\n",
976 input_object->Number());
980 if (FLAG_trace_osr) {
981 PrintF(" [sp + %d] <- %d (int32) ; [sp + %d]\n",
986 output->SetFrameSlot(output_offset, int32_value);
990 case Translation::DOUBLE_STACK_SLOT: {
991 static const int kLowerOffset = 0 * kPointerSize;
992 static const int kUpperOffset = 1 * kPointerSize;
994 // Abort OSR if we don't have a number.
995 if (!input_object->IsNumber()) return false;
997 int output_index = iterator->Next();
998 unsigned output_offset =
999 output->GetOffsetFromSlotIndex(output_index);
1000 double double_value = input_object->Number();
1001 uint64_t int_value = BitCast<uint64_t, double>(double_value);
1002 int32_t lower = static_cast<int32_t>(int_value);
1003 int32_t upper = static_cast<int32_t>(int_value >> kBitsPerInt);
1004 if (FLAG_trace_osr) {
1005 PrintF(" [sp + %d] <- 0x%08x (upper bits of %g) ; [sp + %d]\n",
1006 output_offset + kUpperOffset,
1010 PrintF(" [sp + %d] <- 0x%08x (lower bits of %g) ; [sp + %d]\n",
1011 output_offset + kLowerOffset,
1016 output->SetFrameSlot(output_offset + kLowerOffset, lower);
1017 output->SetFrameSlot(output_offset + kUpperOffset, upper);
1021 case Translation::LITERAL: {
1022 // Just ignore non-materialized literals.
1027 case Translation::ARGUMENTS_OBJECT: {
1028 // Optimized code assumes that the argument object has not been
1029 // materialized and so bypasses it when doing arguments access.
1030 // We should have bailed out before starting the frame
1037 if (!duplicate) *input_offset -= kPointerSize;
1042 void Deoptimizer::PatchStackCheckCode(Code* unoptimized_code,
1044 Code* replacement_code) {
1045 // Iterate over the stack check table and patch every stack check
1046 // call to an unconditional call to the replacement code.
1047 ASSERT(unoptimized_code->kind() == Code::FUNCTION);
1048 Address stack_check_cursor = unoptimized_code->instruction_start() +
1049 unoptimized_code->stack_check_table_offset();
1050 uint32_t table_length = Memory::uint32_at(stack_check_cursor);
1051 stack_check_cursor += kIntSize;
1052 for (uint32_t i = 0; i < table_length; ++i) {
1053 uint32_t pc_offset = Memory::uint32_at(stack_check_cursor + kIntSize);
1054 Address pc_after = unoptimized_code->instruction_start() + pc_offset;
1055 PatchStackCheckCodeAt(unoptimized_code,
1059 stack_check_cursor += 2 * kIntSize;
1064 void Deoptimizer::RevertStackCheckCode(Code* unoptimized_code,
1066 Code* replacement_code) {
1067 // Iterate over the stack check table and revert the patched
1068 // stack check calls.
1069 ASSERT(unoptimized_code->kind() == Code::FUNCTION);
1070 Address stack_check_cursor = unoptimized_code->instruction_start() +
1071 unoptimized_code->stack_check_table_offset();
1072 uint32_t table_length = Memory::uint32_at(stack_check_cursor);
1073 stack_check_cursor += kIntSize;
1074 for (uint32_t i = 0; i < table_length; ++i) {
1075 uint32_t pc_offset = Memory::uint32_at(stack_check_cursor + kIntSize);
1076 Address pc_after = unoptimized_code->instruction_start() + pc_offset;
1077 RevertStackCheckCodeAt(unoptimized_code,
1081 stack_check_cursor += 2 * kIntSize;
1086 unsigned Deoptimizer::ComputeInputFrameSize() const {
1087 unsigned fixed_size = ComputeFixedSize(function_);
1088 // The fp-to-sp delta already takes the context and the function
1089 // into account so we have to avoid double counting them (-2).
1090 unsigned result = fixed_size + fp_to_sp_delta_ - (2 * kPointerSize);
1092 if (bailout_type_ == OSR) {
1093 // TODO(kasperl): It would be nice if we could verify that the
1094 // size matches with the stack height we can compute based on the
1095 // environment at the OSR entry. The code for that his built into
1096 // the DoComputeOsrOutputFrame function for now.
1098 unsigned stack_slots = optimized_code_->stack_slots();
1099 unsigned outgoing_size = ComputeOutgoingArgumentSize();
1100 ASSERT(result == fixed_size + (stack_slots * kPointerSize) + outgoing_size);
1107 unsigned Deoptimizer::ComputeFixedSize(JSFunction* function) const {
1108 // The fixed part of the frame consists of the return address, frame
1109 // pointer, function, context, and all the incoming arguments.
1110 return ComputeIncomingArgumentSize(function) +
1111 StandardFrameConstants::kFixedFrameSize;
1115 unsigned Deoptimizer::ComputeIncomingArgumentSize(JSFunction* function) const {
1116 // The incoming arguments is the values for formal parameters and
1117 // the receiver. Every slot contains a pointer.
1118 unsigned arguments = function->shared()->formal_parameter_count() + 1;
1119 return arguments * kPointerSize;
1123 unsigned Deoptimizer::ComputeOutgoingArgumentSize() const {
1124 DeoptimizationInputData* data = DeoptimizationInputData::cast(
1125 optimized_code_->deoptimization_data());
1126 unsigned height = data->ArgumentsStackHeight(bailout_id_)->value();
1127 return height * kPointerSize;
1131 Object* Deoptimizer::ComputeLiteral(int index) const {
1132 DeoptimizationInputData* data = DeoptimizationInputData::cast(
1133 optimized_code_->deoptimization_data());
1134 FixedArray* literals = data->LiteralArray();
1135 return literals->get(index);
1139 void Deoptimizer::AddDoubleValue(intptr_t slot_address,
1141 HeapNumberMaterializationDescriptor value_desc(
1142 reinterpret_cast<Address>(slot_address), value);
1143 deferred_heap_numbers_.Add(value_desc);
1147 MemoryChunk* Deoptimizer::CreateCode(BailoutType type) {
1148 // We cannot run this if the serializer is enabled because this will
1149 // cause us to emit relocation information for the external
1150 // references. This is fine because the deoptimizer's code section
1151 // isn't meant to be serialized at all.
1152 ASSERT(!Serializer::enabled());
1154 MacroAssembler masm(Isolate::Current(), NULL, 16 * KB);
1155 masm.set_emit_debug_code(false);
1156 GenerateDeoptimizationEntries(&masm, kNumberOfEntries, type);
1158 masm.GetCode(&desc);
1159 ASSERT(desc.reloc_size == 0);
1161 MemoryChunk* chunk =
1162 Isolate::Current()->memory_allocator()->AllocateChunk(desc.instr_size,
1165 ASSERT(chunk->area_size() >= desc.instr_size);
1166 if (chunk == NULL) {
1167 V8::FatalProcessOutOfMemory("Not enough memory for deoptimization table");
1169 memcpy(chunk->area_start(), desc.buffer, desc.instr_size);
1170 CPU::FlushICache(chunk->area_start(), desc.instr_size);
1175 Code* Deoptimizer::FindDeoptimizingCodeFromAddress(Address addr) {
1176 DeoptimizingCodeListNode* node =
1177 Isolate::Current()->deoptimizer_data()->deoptimizing_code_list_;
1178 while (node != NULL) {
1179 if (node->code()->contains(addr)) return *node->code();
1180 node = node->next();
1186 void Deoptimizer::RemoveDeoptimizingCode(Code* code) {
1187 DeoptimizerData* data = Isolate::Current()->deoptimizer_data();
1188 ASSERT(data->deoptimizing_code_list_ != NULL);
1189 // Run through the code objects to find this one and remove it.
1190 DeoptimizingCodeListNode* prev = NULL;
1191 DeoptimizingCodeListNode* current = data->deoptimizing_code_list_;
1192 while (current != NULL) {
1193 if (*current->code() == code) {
1194 // Unlink from list. If prev is NULL we are looking at the first element.
1196 data->deoptimizing_code_list_ = current->next();
1198 prev->set_next(current->next());
1203 // Move to next in list.
1205 current = current->next();
1207 // Deoptimizing code is removed through weak callback. Each object is expected
1208 // to be removed once and only once.
1213 FrameDescription::FrameDescription(uint32_t frame_size,
1214 JSFunction* function)
1215 : frame_size_(frame_size),
1216 function_(function),
1220 context_(kZapUint32) {
1221 // Zap all the registers.
1222 for (int r = 0; r < Register::kNumRegisters; r++) {
1223 SetRegister(r, kZapUint32);
1226 // Zap all the slots.
1227 for (unsigned o = 0; o < frame_size; o += kPointerSize) {
1228 SetFrameSlot(o, kZapUint32);
1233 int FrameDescription::ComputeFixedSize() {
1234 return StandardFrameConstants::kFixedFrameSize +
1235 (ComputeParametersCount() + 1) * kPointerSize;
1239 unsigned FrameDescription::GetOffsetFromSlotIndex(int slot_index) {
1240 if (slot_index >= 0) {
1241 // Local or spill slots. Skip the fixed part of the frame
1242 // including all arguments.
1243 unsigned base = GetFrameSize() - ComputeFixedSize();
1244 return base - ((slot_index + 1) * kPointerSize);
1246 // Incoming parameter.
1247 int arg_size = (ComputeParametersCount() + 1) * kPointerSize;
1248 unsigned base = GetFrameSize() - arg_size;
1249 return base - ((slot_index + 1) * kPointerSize);
1254 int FrameDescription::ComputeParametersCount() {
1256 case StackFrame::JAVA_SCRIPT:
1257 return function_->shared()->formal_parameter_count();
1258 case StackFrame::ARGUMENTS_ADAPTOR: {
1259 // Last slot contains number of incomming arguments as a smi.
1260 // Can't use GetExpression(0) because it would cause infinite recursion.
1261 return reinterpret_cast<Smi*>(*GetFrameSlotPointer(0))->value();
1270 Object* FrameDescription::GetParameter(int index) {
1272 ASSERT(index < ComputeParametersCount());
1273 // The slot indexes for incoming arguments are negative.
1274 unsigned offset = GetOffsetFromSlotIndex(index - ComputeParametersCount());
1275 return reinterpret_cast<Object*>(*GetFrameSlotPointer(offset));
1279 unsigned FrameDescription::GetExpressionCount() {
1280 ASSERT_EQ(StackFrame::JAVA_SCRIPT, type_);
1281 unsigned size = GetFrameSize() - ComputeFixedSize();
1282 return size / kPointerSize;
1286 Object* FrameDescription::GetExpression(int index) {
1287 ASSERT_EQ(StackFrame::JAVA_SCRIPT, type_);
1288 unsigned offset = GetOffsetFromSlotIndex(index);
1289 return reinterpret_cast<Object*>(*GetFrameSlotPointer(offset));
1293 void TranslationBuffer::Add(int32_t value) {
1294 // Encode the sign bit in the least significant bit.
1295 bool is_negative = (value < 0);
1296 uint32_t bits = ((is_negative ? -value : value) << 1) |
1297 static_cast<int32_t>(is_negative);
1298 // Encode the individual bytes using the least significant bit of
1299 // each byte to indicate whether or not more bytes follow.
1301 uint32_t next = bits >> 7;
1302 contents_.Add(((bits << 1) & 0xFF) | (next != 0));
1304 } while (bits != 0);
1308 int32_t TranslationIterator::Next() {
1309 // Run through the bytes until we reach one with a least significant
1310 // bit of zero (marks the end).
1312 for (int i = 0; true; i += 7) {
1314 uint8_t next = buffer_->get(index_++);
1315 bits |= (next >> 1) << i;
1316 if ((next & 1) == 0) break;
1318 // The bits encode the sign in the least significant bit.
1319 bool is_negative = (bits & 1) == 1;
1320 int32_t result = bits >> 1;
1321 return is_negative ? -result : result;
1325 Handle<ByteArray> TranslationBuffer::CreateByteArray() {
1326 int length = contents_.length();
1327 Handle<ByteArray> result =
1328 Isolate::Current()->factory()->NewByteArray(length, TENURED);
1329 memcpy(result->GetDataStartAddress(), contents_.ToVector().start(), length);
1334 void Translation::BeginConstructStubFrame(int literal_id, unsigned height) {
1335 buffer_->Add(CONSTRUCT_STUB_FRAME);
1336 buffer_->Add(literal_id);
1337 buffer_->Add(height);
1341 void Translation::BeginArgumentsAdaptorFrame(int literal_id, unsigned height) {
1342 buffer_->Add(ARGUMENTS_ADAPTOR_FRAME);
1343 buffer_->Add(literal_id);
1344 buffer_->Add(height);
1348 void Translation::BeginJSFrame(int node_id, int literal_id, unsigned height) {
1349 buffer_->Add(JS_FRAME);
1350 buffer_->Add(node_id);
1351 buffer_->Add(literal_id);
1352 buffer_->Add(height);
1356 void Translation::StoreRegister(Register reg) {
1357 buffer_->Add(REGISTER);
1358 buffer_->Add(reg.code());
1362 void Translation::StoreInt32Register(Register reg) {
1363 buffer_->Add(INT32_REGISTER);
1364 buffer_->Add(reg.code());
1368 void Translation::StoreDoubleRegister(DoubleRegister reg) {
1369 buffer_->Add(DOUBLE_REGISTER);
1370 buffer_->Add(DoubleRegister::ToAllocationIndex(reg));
1374 void Translation::StoreStackSlot(int index) {
1375 buffer_->Add(STACK_SLOT);
1376 buffer_->Add(index);
1380 void Translation::StoreInt32StackSlot(int index) {
1381 buffer_->Add(INT32_STACK_SLOT);
1382 buffer_->Add(index);
1386 void Translation::StoreDoubleStackSlot(int index) {
1387 buffer_->Add(DOUBLE_STACK_SLOT);
1388 buffer_->Add(index);
1392 void Translation::StoreLiteral(int literal_id) {
1393 buffer_->Add(LITERAL);
1394 buffer_->Add(literal_id);
1398 void Translation::StoreArgumentsObject() {
1399 buffer_->Add(ARGUMENTS_OBJECT);
1403 void Translation::MarkDuplicate() {
1404 buffer_->Add(DUPLICATE);
1408 int Translation::NumberOfOperandsFor(Opcode opcode) {
1410 case ARGUMENTS_OBJECT:
1414 case INT32_REGISTER:
1415 case DOUBLE_REGISTER:
1417 case INT32_STACK_SLOT:
1418 case DOUBLE_STACK_SLOT:
1422 case ARGUMENTS_ADAPTOR_FRAME:
1423 case CONSTRUCT_STUB_FRAME:
1433 #if defined(OBJECT_PRINT) || defined(ENABLE_DISASSEMBLER)
1435 const char* Translation::StringFor(Opcode opcode) {
1441 case ARGUMENTS_ADAPTOR_FRAME:
1442 return "ARGUMENTS_ADAPTOR_FRAME";
1443 case CONSTRUCT_STUB_FRAME:
1444 return "CONSTRUCT_STUB_FRAME";
1447 case INT32_REGISTER:
1448 return "INT32_REGISTER";
1449 case DOUBLE_REGISTER:
1450 return "DOUBLE_REGISTER";
1452 return "STACK_SLOT";
1453 case INT32_STACK_SLOT:
1454 return "INT32_STACK_SLOT";
1455 case DOUBLE_STACK_SLOT:
1456 return "DOUBLE_STACK_SLOT";
1459 case ARGUMENTS_OBJECT:
1460 return "ARGUMENTS_OBJECT";
1471 DeoptimizingCodeListNode::DeoptimizingCodeListNode(Code* code): next_(NULL) {
1472 GlobalHandles* global_handles = Isolate::Current()->global_handles();
1473 // Globalize the code object and make it weak.
1474 code_ = Handle<Code>::cast(global_handles->Create(code));
1475 global_handles->MakeWeak(reinterpret_cast<Object**>(code_.location()),
1477 Deoptimizer::HandleWeakDeoptimizedCode);
1481 DeoptimizingCodeListNode::~DeoptimizingCodeListNode() {
1482 GlobalHandles* global_handles = Isolate::Current()->global_handles();
1483 global_handles->Destroy(reinterpret_cast<Object**>(code_.location()));
1487 // We can't intermix stack decoding and allocations because
1488 // deoptimization infrastracture is not GC safe.
1489 // Thus we build a temporary structure in malloced space.
1490 SlotRef SlotRef::ComputeSlotForNextArgument(TranslationIterator* iterator,
1491 DeoptimizationInputData* data,
1492 JavaScriptFrame* frame) {
1493 Translation::Opcode opcode =
1494 static_cast<Translation::Opcode>(iterator->Next());
1497 case Translation::BEGIN:
1498 case Translation::JS_FRAME:
1499 case Translation::ARGUMENTS_ADAPTOR_FRAME:
1500 case Translation::CONSTRUCT_STUB_FRAME:
1501 // Peeled off before getting here.
1504 case Translation::ARGUMENTS_OBJECT:
1505 // This can be only emitted for local slots not for argument slots.
1508 case Translation::REGISTER:
1509 case Translation::INT32_REGISTER:
1510 case Translation::DOUBLE_REGISTER:
1511 case Translation::DUPLICATE:
1512 // We are at safepoint which corresponds to call. All registers are
1513 // saved by caller so there would be no live registers at this
1514 // point. Thus these translation commands should not be used.
1517 case Translation::STACK_SLOT: {
1518 int slot_index = iterator->Next();
1519 Address slot_addr = SlotAddress(frame, slot_index);
1520 return SlotRef(slot_addr, SlotRef::TAGGED);
1523 case Translation::INT32_STACK_SLOT: {
1524 int slot_index = iterator->Next();
1525 Address slot_addr = SlotAddress(frame, slot_index);
1526 return SlotRef(slot_addr, SlotRef::INT32);
1529 case Translation::DOUBLE_STACK_SLOT: {
1530 int slot_index = iterator->Next();
1531 Address slot_addr = SlotAddress(frame, slot_index);
1532 return SlotRef(slot_addr, SlotRef::DOUBLE);
1535 case Translation::LITERAL: {
1536 int literal_index = iterator->Next();
1537 return SlotRef(data->LiteralArray()->get(literal_index));
1546 void SlotRef::ComputeSlotsForArguments(Vector<SlotRef>* args_slots,
1547 TranslationIterator* it,
1548 DeoptimizationInputData* data,
1549 JavaScriptFrame* frame) {
1550 // Process the translation commands for the arguments.
1552 // Skip the translation command for the receiver.
1553 it->Skip(Translation::NumberOfOperandsFor(
1554 static_cast<Translation::Opcode>(it->Next())));
1556 // Compute slots for arguments.
1557 for (int i = 0; i < args_slots->length(); ++i) {
1558 (*args_slots)[i] = ComputeSlotForNextArgument(it, data, frame);
1563 Vector<SlotRef> SlotRef::ComputeSlotMappingForArguments(
1564 JavaScriptFrame* frame,
1565 int inlined_jsframe_index,
1566 int formal_parameter_count) {
1567 AssertNoAllocation no_gc;
1568 int deopt_index = AstNode::kNoNumber;
1569 DeoptimizationInputData* data =
1570 static_cast<OptimizedFrame*>(frame)->GetDeoptimizationData(&deopt_index);
1571 TranslationIterator it(data->TranslationByteArray(),
1572 data->TranslationIndex(deopt_index)->value());
1573 Translation::Opcode opcode = static_cast<Translation::Opcode>(it.Next());
1574 ASSERT(opcode == Translation::BEGIN);
1575 it.Next(); // Drop frame count.
1576 int jsframe_count = it.Next();
1578 ASSERT(jsframe_count > inlined_jsframe_index);
1579 int jsframes_to_skip = inlined_jsframe_index;
1581 opcode = static_cast<Translation::Opcode>(it.Next());
1582 if (opcode == Translation::ARGUMENTS_ADAPTOR_FRAME) {
1583 if (jsframes_to_skip == 0) {
1584 ASSERT(Translation::NumberOfOperandsFor(opcode) == 2);
1586 it.Skip(1); // literal id
1587 int height = it.Next();
1589 // We reached the arguments adaptor frame corresponding to the
1590 // inlined function in question. Number of arguments is height - 1.
1591 Vector<SlotRef> args_slots =
1592 Vector<SlotRef>::New(height - 1); // Minus receiver.
1593 ComputeSlotsForArguments(&args_slots, &it, data, frame);
1596 } else if (opcode == Translation::JS_FRAME) {
1597 if (jsframes_to_skip == 0) {
1598 // Skip over operands to advance to the next opcode.
1599 it.Skip(Translation::NumberOfOperandsFor(opcode));
1601 // We reached the frame corresponding to the inlined function
1602 // in question. Process the translation commands for the
1603 // arguments. Number of arguments is equal to the number of
1604 // format parameter count.
1605 Vector<SlotRef> args_slots =
1606 Vector<SlotRef>::New(formal_parameter_count);
1607 ComputeSlotsForArguments(&args_slots, &it, data, frame);
1613 // Skip over operands to advance to the next opcode.
1614 it.Skip(Translation::NumberOfOperandsFor(opcode));
1618 return Vector<SlotRef>();
1621 #ifdef ENABLE_DEBUGGER_SUPPORT
1623 DeoptimizedFrameInfo::DeoptimizedFrameInfo(Deoptimizer* deoptimizer,
1625 bool has_arguments_adaptor,
1626 bool has_construct_stub) {
1627 FrameDescription* output_frame = deoptimizer->output_[frame_index];
1628 function_ = output_frame->GetFunction();
1629 has_construct_stub_ = has_construct_stub;
1630 expression_count_ = output_frame->GetExpressionCount();
1631 expression_stack_ = new Object*[expression_count_];
1632 // Get the source position using the unoptimized code.
1633 Address pc = reinterpret_cast<Address>(output_frame->GetPc());
1634 Code* code = Code::cast(Isolate::Current()->heap()->FindCodeObject(pc));
1635 source_position_ = code->SourcePosition(pc);
1637 for (int i = 0; i < expression_count_; i++) {
1638 SetExpression(i, output_frame->GetExpression(i));
1641 if (has_arguments_adaptor) {
1642 output_frame = deoptimizer->output_[frame_index - 1];
1643 ASSERT(output_frame->GetFrameType() == StackFrame::ARGUMENTS_ADAPTOR);
1646 parameters_count_ = output_frame->ComputeParametersCount();
1647 parameters_ = new Object*[parameters_count_];
1648 for (int i = 0; i < parameters_count_; i++) {
1649 SetParameter(i, output_frame->GetParameter(i));
1654 DeoptimizedFrameInfo::~DeoptimizedFrameInfo() {
1655 delete[] expression_stack_;
1656 delete[] parameters_;
1660 void DeoptimizedFrameInfo::Iterate(ObjectVisitor* v) {
1661 v->VisitPointer(BitCast<Object**>(&function_));
1662 v->VisitPointers(parameters_, parameters_ + parameters_count_);
1663 v->VisitPointers(expression_stack_, expression_stack_ + expression_count_);
1666 #endif // ENABLE_DEBUGGER_SUPPORT
1668 } } // namespace v8::internal