blob: 819b5e94a30a1753ee4c035cb81724e2c8d6bbee [file] [log] [blame]
mmassi@chromium.org2f0efde2013-02-06 14:12:58 +00001// Copyright 2013 the V8 project authors. All rights reserved.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28#include "v8.h"
29
ulan@chromium.org56c14af2012-09-20 12:51:09 +000030#include "accessors.h"
kasperl@chromium.orga5551262010-12-07 12:49:48 +000031#include "codegen.h"
32#include "deoptimizer.h"
33#include "disasm.h"
34#include "full-codegen.h"
35#include "global-handles.h"
36#include "macro-assembler.h"
37#include "prettyprinter.h"
38
39
40namespace v8 {
41namespace internal {
42
svenpanne@chromium.org876cca82013-03-18 14:43:20 +000043static MemoryChunk* AllocateCodeChunk(MemoryAllocator* allocator) {
44 return allocator->AllocateChunk(Deoptimizer::GetMaxDeoptTableSize(),
45 OS::CommitPageSize(),
jkummerow@chromium.orgba72ec82013-07-22 09:21:20 +000046#if defined(__native_client__)
47 // The Native Client port of V8 uses an interpreter,
48 // so code pages don't need PROT_EXEC.
49 NOT_EXECUTABLE,
50#else
svenpanne@chromium.org876cca82013-03-18 14:43:20 +000051 EXECUTABLE,
jkummerow@chromium.orgba72ec82013-07-22 09:21:20 +000052#endif
svenpanne@chromium.org876cca82013-03-18 14:43:20 +000053 NULL);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000054}
kasperl@chromium.orga5551262010-12-07 12:49:48 +000055
56
svenpanne@chromium.org876cca82013-03-18 14:43:20 +000057DeoptimizerData::DeoptimizerData(MemoryAllocator* allocator)
58 : allocator_(allocator),
svenpanne@chromium.org876cca82013-03-18 14:43:20 +000059#ifdef ENABLE_DEBUGGER_SUPPORT
60 deoptimized_frame_info_(NULL),
61#endif
jkummerow@chromium.org3d00d0a2013-09-04 13:57:32 +000062 current_(NULL) {
danno@chromium.orgaefd6072013-05-14 14:11:47 +000063 for (int i = 0; i < Deoptimizer::kBailoutTypesWithCodeEntry; ++i) {
64 deopt_entry_code_entries_[i] = -1;
65 deopt_entry_code_[i] = AllocateCodeChunk(allocator);
66 }
67}
svenpanne@chromium.org876cca82013-03-18 14:43:20 +000068
69
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000070DeoptimizerData::~DeoptimizerData() {
danno@chromium.orgaefd6072013-05-14 14:11:47 +000071 for (int i = 0; i < Deoptimizer::kBailoutTypesWithCodeEntry; ++i) {
72 allocator_->Free(deopt_entry_code_[i]);
73 deopt_entry_code_[i] = NULL;
74 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000075}
76
ricow@chromium.org4f693d62011-07-04 14:01:31 +000077
78#ifdef ENABLE_DEBUGGER_SUPPORT
79void DeoptimizerData::Iterate(ObjectVisitor* v) {
80 if (deoptimized_frame_info_ != NULL) {
81 deoptimized_frame_info_->Iterate(v);
82 }
83}
84#endif
85
86
jkummerow@chromium.org3d00d0a2013-09-04 13:57:32 +000087Code* Deoptimizer::FindDeoptimizingCode(Address addr) {
88 if (function_->IsHeapObject()) {
89 // Search all deoptimizing code in the native context of the function.
90 Context* native_context = function_->context()->native_context();
91 Object* element = native_context->DeoptimizedCodeListHead();
92 while (!element->IsUndefined()) {
93 Code* code = Code::cast(element);
94 ASSERT(code->kind() == Code::OPTIMIZED_FUNCTION);
95 if (code->contains(addr)) return code;
96 element = code->next_code_link();
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +000097 }
98 }
jkummerow@chromium.org3d00d0a2013-09-04 13:57:32 +000099 return NULL;
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +0000100}
101
102
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000103// We rely on this function not causing a GC. It is called from generated code
104// without having a real stack frame in place.
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000105Deoptimizer* Deoptimizer::New(JSFunction* function,
106 BailoutType type,
107 unsigned bailout_id,
108 Address from,
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000109 int fp_to_sp_delta,
110 Isolate* isolate) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000111 Deoptimizer* deoptimizer = new Deoptimizer(isolate,
112 function,
113 type,
114 bailout_id,
115 from,
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000116 fp_to_sp_delta,
117 NULL);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000118 ASSERT(isolate->deoptimizer_data()->current_ == NULL);
119 isolate->deoptimizer_data()->current_ = deoptimizer;
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000120 return deoptimizer;
121}
122
123
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +0000124// No larger than 2K on all platforms
125static const int kDeoptTableMaxEpilogueCodeSize = 2 * KB;
126
127
128size_t Deoptimizer::GetMaxDeoptTableSize() {
129 int entries_size =
130 Deoptimizer::kMaxNumberOfEntries * Deoptimizer::table_entry_size_;
131 int commit_page_size = static_cast<int>(OS::CommitPageSize());
132 int page_count = ((kDeoptTableMaxEpilogueCodeSize + entries_size - 1) /
133 commit_page_size) + 1;
134 return static_cast<size_t>(commit_page_size * page_count);
135}
136
137
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000138Deoptimizer* Deoptimizer::Grab(Isolate* isolate) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000139 Deoptimizer* result = isolate->deoptimizer_data()->current_;
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000140 ASSERT(result != NULL);
141 result->DeleteFrameDescriptions();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000142 isolate->deoptimizer_data()->current_ = NULL;
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000143 return result;
144}
145
yangguo@chromium.org659ceec2012-01-26 07:37:54 +0000146
147int Deoptimizer::ConvertJSFrameIndexToFrameIndex(int jsframe_index) {
148 if (jsframe_index == 0) return 0;
149
150 int frame_index = 0;
151 while (jsframe_index >= 0) {
152 FrameDescription* frame = output_[frame_index];
153 if (frame->GetFrameType() == StackFrame::JAVA_SCRIPT) {
154 jsframe_index--;
155 }
156 frame_index++;
157 }
158
159 return frame_index - 1;
160}
161
162
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000163#ifdef ENABLE_DEBUGGER_SUPPORT
164DeoptimizedFrameInfo* Deoptimizer::DebuggerInspectableFrame(
165 JavaScriptFrame* frame,
yangguo@chromium.org659ceec2012-01-26 07:37:54 +0000166 int jsframe_index,
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000167 Isolate* isolate) {
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000168 ASSERT(frame->is_optimized());
169 ASSERT(isolate->deoptimizer_data()->deoptimized_frame_info_ == NULL);
170
171 // Get the function and code from the frame.
danno@chromium.org169691d2013-07-15 08:01:13 +0000172 JSFunction* function = frame->function();
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000173 Code* code = frame->LookupCode();
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000174
175 // Locate the deoptimization point in the code. As we are at a call the
176 // return address must be at a place in the code with deoptimization support.
ricow@chromium.org27bf2882011-11-17 08:34:43 +0000177 SafepointEntry safepoint_entry = code->GetSafepointEntry(frame->pc());
178 int deoptimization_index = safepoint_entry.deoptimization_index();
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000179 ASSERT(deoptimization_index != Safepoint::kNoDeoptimizationIndex);
180
181 // Always use the actual stack slots when calculating the fp to sp
182 // delta adding two for the function and context.
183 unsigned stack_slots = code->stack_slots();
184 unsigned fp_to_sp_delta = ((stack_slots + 2) * kPointerSize);
185
186 Deoptimizer* deoptimizer = new Deoptimizer(isolate,
187 function,
188 Deoptimizer::DEBUGGER,
189 deoptimization_index,
190 frame->pc(),
191 fp_to_sp_delta,
192 code);
193 Address tos = frame->fp() - fp_to_sp_delta;
194 deoptimizer->FillInputFrame(tos, frame);
195
196 // Calculate the output frames.
197 Deoptimizer::ComputeOutputFrames(deoptimizer);
198
199 // Create the GC safe output frame information and register it for GC
200 // handling.
yangguo@chromium.org659ceec2012-01-26 07:37:54 +0000201 ASSERT_LT(jsframe_index, deoptimizer->jsframe_count());
202
203 // Convert JS frame index into frame index.
204 int frame_index = deoptimizer->ConvertJSFrameIndexToFrameIndex(jsframe_index);
205
206 bool has_arguments_adaptor =
207 frame_index > 0 &&
208 deoptimizer->output_[frame_index - 1]->GetFrameType() ==
209 StackFrame::ARGUMENTS_ADAPTOR;
210
ulan@chromium.org967e2702012-02-28 09:49:15 +0000211 int construct_offset = has_arguments_adaptor ? 2 : 1;
212 bool has_construct_stub =
213 frame_index >= construct_offset &&
214 deoptimizer->output_[frame_index - construct_offset]->GetFrameType() ==
215 StackFrame::CONSTRUCT;
216
217 DeoptimizedFrameInfo* info = new DeoptimizedFrameInfo(deoptimizer,
218 frame_index,
219 has_arguments_adaptor,
220 has_construct_stub);
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000221 isolate->deoptimizer_data()->deoptimized_frame_info_ = info;
222
223 // Get the "simulated" top and size for the requested frame.
yangguo@chromium.org659ceec2012-01-26 07:37:54 +0000224 FrameDescription* parameters_frame =
225 deoptimizer->output_[
226 has_arguments_adaptor ? (frame_index - 1) : frame_index];
227
228 uint32_t parameters_size = (info->parameters_count() + 1) * kPointerSize;
229 Address parameters_top = reinterpret_cast<Address>(
230 parameters_frame->GetTop() + (parameters_frame->GetFrameSize() -
231 parameters_size));
232
233 uint32_t expressions_size = info->expression_count() * kPointerSize;
234 Address expressions_top = reinterpret_cast<Address>(
235 deoptimizer->output_[frame_index]->GetTop());
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000236
237 // Done with the GC-unsafe frame descriptions. This re-enables allocation.
238 deoptimizer->DeleteFrameDescriptions();
239
240 // Allocate a heap number for the doubles belonging to this frame.
241 deoptimizer->MaterializeHeapNumbersForDebuggerInspectableFrame(
yangguo@chromium.org659ceec2012-01-26 07:37:54 +0000242 parameters_top, parameters_size, expressions_top, expressions_size, info);
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000243
244 // Finished using the deoptimizer instance.
245 delete deoptimizer;
246
247 return info;
248}
249
250
251void Deoptimizer::DeleteDebuggerInspectableFrame(DeoptimizedFrameInfo* info,
252 Isolate* isolate) {
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000253 ASSERT(isolate->deoptimizer_data()->deoptimized_frame_info_ == info);
254 delete info;
255 isolate->deoptimizer_data()->deoptimized_frame_info_ = NULL;
256}
257#endif
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000258
259void Deoptimizer::GenerateDeoptimizationEntries(MacroAssembler* masm,
260 int count,
261 BailoutType type) {
262 TableEntryGenerator generator(masm, type, count);
263 generator.Generate();
264}
265
266
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000267void Deoptimizer::VisitAllOptimizedFunctionsForContext(
268 Context* context, OptimizedFunctionVisitor* visitor) {
rossberg@chromium.org79e79022013-06-03 15:43:46 +0000269 DisallowHeapAllocation no_allocation;
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000270
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000271 ASSERT(context->IsNativeContext());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000272
273 visitor->EnterContext(context);
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000274
jkummerow@chromium.org3d00d0a2013-09-04 13:57:32 +0000275 // Visit the list of optimized functions, removing elements that
276 // no longer refer to optimized code.
277 JSFunction* prev = NULL;
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000278 Object* element = context->OptimizedFunctionsListHead();
279 while (!element->IsUndefined()) {
jkummerow@chromium.org3d00d0a2013-09-04 13:57:32 +0000280 JSFunction* function = JSFunction::cast(element);
281 Object* next = function->next_function_link();
282 if (function->code()->kind() != Code::OPTIMIZED_FUNCTION ||
283 (visitor->VisitFunction(function),
284 function->code()->kind() != Code::OPTIMIZED_FUNCTION)) {
285 // The function no longer refers to optimized code, or the visitor
286 // changed the code to which it refers to no longer be optimized code.
287 // Remove the function from this list.
288 if (prev != NULL) {
289 prev->set_next_function_link(next);
290 } else {
291 context->SetOptimizedFunctionsListHead(next);
292 }
293 // The visitor should not alter the link directly.
294 ASSERT(function->next_function_link() == next);
295 // Set the next function link to undefined to indicate it is no longer
296 // in the optimized functions list.
297 function->set_next_function_link(context->GetHeap()->undefined_value());
298 } else {
299 // The visitor should not alter the link directly.
300 ASSERT(function->next_function_link() == next);
301 // preserve this element.
302 prev = function;
303 }
304 element = next;
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000305 }
306
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000307 visitor->LeaveContext(context);
308}
309
310
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000311void Deoptimizer::VisitAllOptimizedFunctions(
svenpanne@chromium.org876cca82013-03-18 14:43:20 +0000312 Isolate* isolate,
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000313 OptimizedFunctionVisitor* visitor) {
rossberg@chromium.org79e79022013-06-03 15:43:46 +0000314 DisallowHeapAllocation no_allocation;
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000315
jkummerow@chromium.org3d00d0a2013-09-04 13:57:32 +0000316 // Run through the list of all native contexts.
svenpanne@chromium.org876cca82013-03-18 14:43:20 +0000317 Object* context = isolate->heap()->native_contexts_list();
ricow@chromium.org7ad65222011-12-19 12:13:11 +0000318 while (!context->IsUndefined()) {
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +0000319 VisitAllOptimizedFunctionsForContext(Context::cast(context), visitor);
ricow@chromium.org7ad65222011-12-19 12:13:11 +0000320 context = Context::cast(context)->get(Context::NEXT_CONTEXT_LINK);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000321 }
322}
323
324
jkummerow@chromium.org3d00d0a2013-09-04 13:57:32 +0000325// Unlink functions referring to code marked for deoptimization, then move
326// marked code from the optimized code list to the deoptimized code list,
327// and patch code for lazy deopt.
328void Deoptimizer::DeoptimizeMarkedCodeForContext(Context* context) {
rossberg@chromium.org79e79022013-06-03 15:43:46 +0000329 DisallowHeapAllocation no_allocation;
jkummerow@chromium.orgfb732b12013-07-26 10:27:09 +0000330
jkummerow@chromium.org3d00d0a2013-09-04 13:57:32 +0000331 // A "closure" that unlinks optimized code that is going to be
332 // deoptimized from the functions that refer to it.
333 class SelectedCodeUnlinker: public OptimizedFunctionVisitor {
334 public:
335 virtual void EnterContext(Context* context) { } // Don't care.
336 virtual void LeaveContext(Context* context) { } // Don't care.
337 virtual void VisitFunction(JSFunction* function) {
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +0000338 Code* code = function->code();
jkummerow@chromium.org3d00d0a2013-09-04 13:57:32 +0000339 if (!code->marked_for_deoptimization()) return;
340
341 // Unlink this function and evict from optimized code map.
jkummerow@chromium.orgfb732b12013-07-26 10:27:09 +0000342 SharedFunctionInfo* shared = function->shared();
jkummerow@chromium.orgfb732b12013-07-26 10:27:09 +0000343 function->set_code(shared->code());
jkummerow@chromium.orgfb732b12013-07-26 10:27:09 +0000344 shared->EvictFromOptimizedCodeMap(code, "deoptimized function");
jkummerow@chromium.orgfb732b12013-07-26 10:27:09 +0000345
346 if (FLAG_trace_deopt) {
jkummerow@chromium.org3d00d0a2013-09-04 13:57:32 +0000347 PrintF("[deoptimizer unlinked: ");
jkummerow@chromium.orgfb732b12013-07-26 10:27:09 +0000348 function->PrintName();
349 PrintF(" / %" V8PRIxPTR "]\n", reinterpret_cast<intptr_t>(function));
350 }
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +0000351 }
jkummerow@chromium.org3d00d0a2013-09-04 13:57:32 +0000352 };
353
354 // Unlink all functions that refer to marked code.
355 SelectedCodeUnlinker unlinker;
356 VisitAllOptimizedFunctionsForContext(context, &unlinker);
357
358 // Move marked code from the optimized code list to the deoptimized
359 // code list, collecting them into a ZoneList.
360 Isolate* isolate = context->GetHeap()->isolate();
361 Zone zone(isolate);
362 ZoneList<Code*> codes(10, &zone);
363
364 // Walk over all optimized code objects in this native context.
365 Code* prev = NULL;
366 Object* element = context->OptimizedCodeListHead();
367 while (!element->IsUndefined()) {
368 Code* code = Code::cast(element);
369 ASSERT(code->kind() == Code::OPTIMIZED_FUNCTION);
370 Object* next = code->next_code_link();
371 if (code->marked_for_deoptimization()) {
372 // Put the code into the list for later patching.
373 codes.Add(code, &zone);
374
375 if (prev != NULL) {
376 // Skip this code in the optimized code list.
377 prev->set_next_code_link(next);
378 } else {
379 // There was no previous node, the next node is the new head.
380 context->SetOptimizedCodeListHead(next);
381 }
382
383 // Move the code to the _deoptimized_ code list.
384 code->set_next_code_link(context->DeoptimizedCodeListHead());
385 context->SetDeoptimizedCodeListHead(code);
386 } else {
387 // Not marked; preserve this element.
388 prev = code;
389 }
390 element = next;
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +0000391 }
jkummerow@chromium.org3d00d0a2013-09-04 13:57:32 +0000392
393 // TODO(titzer): we need a handle scope only because of the macro assembler,
394 // which is only used in EnsureCodeForDeoptimizationEntry.
395 HandleScope scope(isolate);
396 // Now patch all the codes for deoptimization.
397 for (int i = 0; i < codes.length(); i++) {
398 // It is finally time to die, code object.
399 // Do platform-specific patching to force any activations to lazy deopt.
400 PatchCodeForDeoptimization(isolate, codes[i]);
401
402 // We might be in the middle of incremental marking with compaction.
403 // Tell collector to treat this code object in a special way and
404 // ignore all slots that might have been recorded on it.
405 isolate->heap()->mark_compact_collector()->InvalidateCode(codes[i]);
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +0000406 }
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +0000407}
408
409
svenpanne@chromium.org876cca82013-03-18 14:43:20 +0000410void Deoptimizer::DeoptimizeAll(Isolate* isolate) {
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +0000411 if (FLAG_trace_deopt) {
jkummerow@chromium.org3d00d0a2013-09-04 13:57:32 +0000412 PrintF("[deoptimize all code in all contexts]\n");
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +0000413 }
jkummerow@chromium.org3d00d0a2013-09-04 13:57:32 +0000414 DisallowHeapAllocation no_allocation;
415 // For all contexts, mark all code, then deoptimize.
416 Object* context = isolate->heap()->native_contexts_list();
417 while (!context->IsUndefined()) {
418 Context* native_context = Context::cast(context);
419 MarkAllCodeForContext(native_context);
420 DeoptimizeMarkedCodeForContext(native_context);
421 context = native_context->get(Context::NEXT_CONTEXT_LINK);
422 }
423}
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +0000424
jkummerow@chromium.org3d00d0a2013-09-04 13:57:32 +0000425
426void Deoptimizer::DeoptimizeMarkedCode(Isolate* isolate) {
427 if (FLAG_trace_deopt) {
428 PrintF("[deoptimize marked code in all contexts]\n");
429 }
430 DisallowHeapAllocation no_allocation;
431 // For all contexts, deoptimize code already marked.
432 Object* context = isolate->heap()->native_contexts_list();
433 while (!context->IsUndefined()) {
434 Context* native_context = Context::cast(context);
435 DeoptimizeMarkedCodeForContext(native_context);
436 context = native_context->get(Context::NEXT_CONTEXT_LINK);
437 }
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +0000438}
439
440
441void Deoptimizer::DeoptimizeGlobalObject(JSObject* object) {
jkummerow@chromium.org3d00d0a2013-09-04 13:57:32 +0000442 if (FLAG_trace_deopt) {
443 PrintF("[deoptimize global object @ 0x%08" V8PRIxPTR "]\n",
444 reinterpret_cast<intptr_t>(object));
445 }
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +0000446 if (object->IsJSGlobalProxy()) {
447 Object* proto = object->GetPrototype();
448 ASSERT(proto->IsJSGlobalObject());
jkummerow@chromium.org3d00d0a2013-09-04 13:57:32 +0000449 Context* native_context = GlobalObject::cast(proto)->native_context();
450 MarkAllCodeForContext(native_context);
451 DeoptimizeMarkedCodeForContext(native_context);
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +0000452 } else if (object->IsGlobalObject()) {
jkummerow@chromium.org3d00d0a2013-09-04 13:57:32 +0000453 Context* native_context = GlobalObject::cast(object)->native_context();
454 MarkAllCodeForContext(native_context);
455 DeoptimizeMarkedCodeForContext(native_context);
456 }
457}
458
459
460void Deoptimizer::MarkAllCodeForContext(Context* context) {
461 Object* element = context->OptimizedCodeListHead();
462 while (!element->IsUndefined()) {
463 Code* code = Code::cast(element);
464 ASSERT(code->kind() == Code::OPTIMIZED_FUNCTION);
465 code->set_marked_for_deoptimization(true);
466 element = code->next_code_link();
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +0000467 }
468}
469
470
471void Deoptimizer::DeoptimizeFunction(JSFunction* function) {
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +0000472 Code* code = function->code();
jkummerow@chromium.org3d00d0a2013-09-04 13:57:32 +0000473 if (code->kind() == Code::OPTIMIZED_FUNCTION) {
474 // Mark the code for deoptimization and unlink any functions that also
475 // refer to that code. The code cannot be shared across native contexts,
476 // so we only need to search one.
477 code->set_marked_for_deoptimization(true);
478 DeoptimizeMarkedCodeForContext(function->context()->native_context());
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +0000479 }
480}
481
482
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000483void Deoptimizer::ComputeOutputFrames(Deoptimizer* deoptimizer) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000484 deoptimizer->DoComputeOutputFrames();
485}
486
487
mmassi@chromium.org2f0efde2013-02-06 14:12:58 +0000488bool Deoptimizer::TraceEnabledFor(BailoutType deopt_type,
489 StackFrame::Type frame_type) {
490 switch (deopt_type) {
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +0000491 case EAGER:
danno@chromium.orgaefd6072013-05-14 14:11:47 +0000492 case SOFT:
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +0000493 case LAZY:
494 case DEBUGGER:
mmassi@chromium.org2f0efde2013-02-06 14:12:58 +0000495 return (frame_type == StackFrame::STUB)
496 ? FLAG_trace_stub_failures
497 : FLAG_trace_deopt;
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +0000498 }
499 UNREACHABLE();
500 return false;
501}
502
503
504const char* Deoptimizer::MessageFor(BailoutType type) {
505 switch (type) {
jkummerow@chromium.org4e308cf2013-05-17 13:39:16 +0000506 case EAGER: return "eager";
507 case SOFT: return "soft";
508 case LAZY: return "lazy";
509 case DEBUGGER: return "debugger";
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +0000510 }
511 UNREACHABLE();
ulan@chromium.org4121f232012-12-27 15:57:11 +0000512 return NULL;
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +0000513}
514
515
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000516Deoptimizer::Deoptimizer(Isolate* isolate,
517 JSFunction* function,
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000518 BailoutType type,
519 unsigned bailout_id,
520 Address from,
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000521 int fp_to_sp_delta,
522 Code* optimized_code)
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000523 : isolate_(isolate),
524 function_(function),
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000525 bailout_id_(bailout_id),
526 bailout_type_(type),
527 from_(from),
528 fp_to_sp_delta_(fp_to_sp_delta),
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000529 has_alignment_padding_(0),
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000530 input_(NULL),
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000531 output_count_(0),
yangguo@chromium.org659ceec2012-01-26 07:37:54 +0000532 jsframe_count_(0),
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000533 output_(NULL),
dslomov@chromium.orgb752d402013-06-18 11:54:54 +0000534 deferred_objects_tagged_values_(0),
535 deferred_objects_double_values_(0),
536 deferred_objects_(0),
mmassi@chromium.org2f0efde2013-02-06 14:12:58 +0000537 deferred_heap_numbers_(0),
danno@chromium.org59400602013-08-13 17:09:37 +0000538 jsframe_functions_(0),
539 jsframe_has_adapted_arguments_(0),
540 materialized_values_(NULL),
541 materialized_objects_(NULL),
542 materialization_value_index_(0),
543 materialization_object_index_(0),
mmassi@chromium.org2f0efde2013-02-06 14:12:58 +0000544 trace_(false) {
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +0000545 // For COMPILED_STUBs called from builtins, the function pointer is a SMI
546 // indicating an internal frame.
547 if (function->IsSmi()) {
548 function = NULL;
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000549 }
danno@chromium.org169691d2013-07-15 08:01:13 +0000550 ASSERT(from != NULL);
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +0000551 if (function != NULL && function->IsOptimized()) {
552 function->shared()->increment_deopt_count();
danno@chromium.orgaefd6072013-05-14 14:11:47 +0000553 if (bailout_type_ == Deoptimizer::SOFT) {
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +0000554 isolate->counters()->soft_deopts_executed()->Increment();
danno@chromium.orgaefd6072013-05-14 14:11:47 +0000555 // Soft deopts shouldn't count against the overall re-optimization count
556 // that can eventually lead to disabling optimization for a function.
557 int opt_count = function->shared()->opt_count();
558 if (opt_count > 0) opt_count--;
559 function->shared()->set_opt_count(opt_count);
560 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000561 }
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +0000562 compiled_code_ = FindOptimizedCode(function, optimized_code);
hpayer@chromium.orgc5d49712013-09-11 08:25:48 +0000563
564#if DEBUG
565 ASSERT(compiled_code_ != NULL);
566 if (type == EAGER || type == SOFT || type == LAZY) {
567 ASSERT(compiled_code_->kind() != Code::FUNCTION);
568 }
569#endif
570
mstarzinger@chromium.org068ea0a2013-01-30 09:39:44 +0000571 StackFrame::Type frame_type = function == NULL
572 ? StackFrame::STUB
573 : StackFrame::JAVA_SCRIPT;
mmassi@chromium.org2f0efde2013-02-06 14:12:58 +0000574 trace_ = TraceEnabledFor(type, frame_type);
rossberg@chromium.org79e79022013-06-03 15:43:46 +0000575#ifdef DEBUG
576 CHECK(AllowHeapAllocation::IsAllowed());
577 disallow_heap_allocation_ = new DisallowHeapAllocation();
578#endif // DEBUG
mmassi@chromium.org2f0efde2013-02-06 14:12:58 +0000579 unsigned size = ComputeInputFrameSize();
580 input_ = new(size) FrameDescription(size, function);
mstarzinger@chromium.org068ea0a2013-01-30 09:39:44 +0000581 input_->SetFrameType(frame_type);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000582}
583
584
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +0000585Code* Deoptimizer::FindOptimizedCode(JSFunction* function,
586 Code* optimized_code) {
587 switch (bailout_type_) {
danno@chromium.orgaefd6072013-05-14 14:11:47 +0000588 case Deoptimizer::SOFT:
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +0000589 case Deoptimizer::EAGER:
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +0000590 case Deoptimizer::LAZY: {
jkummerow@chromium.org3d00d0a2013-09-04 13:57:32 +0000591 Code* compiled_code = FindDeoptimizingCode(from_);
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +0000592 return (compiled_code == NULL)
jkummerow@chromium.orgba72ec82013-07-22 09:21:20 +0000593 ? static_cast<Code*>(isolate_->FindCodeObject(from_))
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +0000594 : compiled_code;
595 }
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +0000596 case Deoptimizer::DEBUGGER:
597 ASSERT(optimized_code->contains(from_));
598 return optimized_code;
599 }
600 UNREACHABLE();
601 return NULL;
602}
603
604
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +0000605void Deoptimizer::PrintFunctionName() {
606 if (function_->IsJSFunction()) {
607 function_->PrintName();
608 } else {
609 PrintF("%s", Code::Kind2String(compiled_code_->kind()));
610 }
611}
612
613
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000614Deoptimizer::~Deoptimizer() {
615 ASSERT(input_ == NULL && output_ == NULL);
rossberg@chromium.org79e79022013-06-03 15:43:46 +0000616 ASSERT(disallow_heap_allocation_ == NULL);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000617}
618
619
620void Deoptimizer::DeleteFrameDescriptions() {
621 delete input_;
622 for (int i = 0; i < output_count_; ++i) {
623 if (output_[i] != input_) delete output_[i];
624 }
625 delete[] output_;
626 input_ = NULL;
627 output_ = NULL;
rossberg@chromium.org79e79022013-06-03 15:43:46 +0000628#ifdef DEBUG
629 CHECK(!AllowHeapAllocation::IsAllowed());
630 CHECK(disallow_heap_allocation_ != NULL);
631 delete disallow_heap_allocation_;
632 disallow_heap_allocation_ = NULL;
633#endif // DEBUG
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000634}
635
636
hpayer@chromium.org8432c912013-02-28 15:55:26 +0000637Address Deoptimizer::GetDeoptimizationEntry(Isolate* isolate,
638 int id,
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +0000639 BailoutType type,
640 GetEntryMode mode) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000641 ASSERT(id >= 0);
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +0000642 if (id >= kMaxNumberOfEntries) return NULL;
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +0000643 if (mode == ENSURE_ENTRY_CODE) {
hpayer@chromium.org8432c912013-02-28 15:55:26 +0000644 EnsureCodeForDeoptimizationEntry(isolate, type, id);
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +0000645 } else {
646 ASSERT(mode == CALCULATE_ENTRY_ADDRESS);
647 }
hpayer@chromium.org8432c912013-02-28 15:55:26 +0000648 DeoptimizerData* data = isolate->deoptimizer_data();
danno@chromium.orgaefd6072013-05-14 14:11:47 +0000649 ASSERT(type < kBailoutTypesWithCodeEntry);
650 MemoryChunk* base = data->deopt_entry_code_[type];
mstarzinger@chromium.org068ea0a2013-01-30 09:39:44 +0000651 return base->area_start() + (id * table_entry_size_);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000652}
653
654
svenpanne@chromium.org876cca82013-03-18 14:43:20 +0000655int Deoptimizer::GetDeoptimizationId(Isolate* isolate,
656 Address addr,
657 BailoutType type) {
658 DeoptimizerData* data = isolate->deoptimizer_data();
danno@chromium.orgaefd6072013-05-14 14:11:47 +0000659 MemoryChunk* base = data->deopt_entry_code_[type];
mstarzinger@chromium.org068ea0a2013-01-30 09:39:44 +0000660 Address start = base->area_start();
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000661 if (base == NULL ||
mstarzinger@chromium.org068ea0a2013-01-30 09:39:44 +0000662 addr < start ||
663 addr >= start + (kMaxNumberOfEntries * table_entry_size_)) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000664 return kNotDeoptimizationEntry;
665 }
666 ASSERT_EQ(0,
mstarzinger@chromium.org068ea0a2013-01-30 09:39:44 +0000667 static_cast<int>(addr - start) % table_entry_size_);
668 return static_cast<int>(addr - start) / table_entry_size_;
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000669}
670
671
fschneider@chromium.org9e3e0b62011-01-03 10:16:46 +0000672int Deoptimizer::GetOutputInfo(DeoptimizationOutputData* data,
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +0000673 BailoutId id,
fschneider@chromium.org9e3e0b62011-01-03 10:16:46 +0000674 SharedFunctionInfo* shared) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000675 // TODO(kasperl): For now, we do a simple linear search for the PC
676 // offset associated with the given node id. This should probably be
677 // changed to a binary search.
678 int length = data->DeoptPoints();
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000679 for (int i = 0; i < length; i++) {
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +0000680 if (data->AstId(i) == id) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000681 return data->PcAndState(i)->value();
682 }
683 }
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +0000684 PrintF("[couldn't find pc offset for node=%d]\n", id.ToInt());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000685 PrintF("[method: %s]\n", *shared->DebugName()->ToCString());
686 // Print the source code if available.
687 HeapStringAllocator string_allocator;
688 StringStream stream(&string_allocator);
689 shared->SourceCodePrint(&stream, -1);
690 PrintF("[source:\n%s\n]", *stream.ToCString());
691
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000692 FATAL("unable to find pc offset during deoptimization");
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000693 return -1;
694}
695
696
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000697int Deoptimizer::GetDeoptimizedCodeCount(Isolate* isolate) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000698 int length = 0;
jkummerow@chromium.org3d00d0a2013-09-04 13:57:32 +0000699 // Count all entries in the deoptimizing code list of every context.
700 Object* context = isolate->heap()->native_contexts_list();
701 while (!context->IsUndefined()) {
702 Context* native_context = Context::cast(context);
703 Object* element = native_context->DeoptimizedCodeListHead();
704 while (!element->IsUndefined()) {
705 Code* code = Code::cast(element);
706 ASSERT(code->kind() == Code::OPTIMIZED_FUNCTION);
707 length++;
708 element = code->next_code_link();
709 }
710 context = Context::cast(context)->get(Context::NEXT_CONTEXT_LINK);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000711 }
712 return length;
713}
714
715
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000716// We rely on this function not causing a GC. It is called from generated code
717// without having a real stack frame in place.
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000718void Deoptimizer::DoComputeOutputFrames() {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000719 // Print some helpful diagnostic information.
machenbach@chromium.orgc1789ee2013-07-05 07:09:57 +0000720 if (FLAG_log_timer_events &&
721 compiled_code_->kind() == Code::OPTIMIZED_FUNCTION) {
722 LOG(isolate(), CodeDeoptEvent(compiled_code_));
723 }
jkummerow@chromium.orgdc94e192013-08-30 11:35:42 +0000724 ElapsedTimer timer;
mmassi@chromium.org2f0efde2013-02-06 14:12:58 +0000725 if (trace_) {
jkummerow@chromium.orgdc94e192013-08-30 11:35:42 +0000726 timer.Start();
jkummerow@chromium.org4e308cf2013-05-17 13:39:16 +0000727 PrintF("[deoptimizing (DEOPT %s): begin 0x%08" V8PRIxPTR " ",
728 MessageFor(bailout_type_),
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000729 reinterpret_cast<intptr_t>(function_));
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +0000730 PrintFunctionName();
jkummerow@chromium.org4e308cf2013-05-17 13:39:16 +0000731 PrintF(" @%d, FP to SP delta: %d]\n", bailout_id_, fp_to_sp_delta_);
732 if (bailout_type_ == EAGER || bailout_type_ == SOFT) {
733 compiled_code_->PrintDeoptLocation(bailout_id_);
734 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000735 }
736
737 // Determine basic deoptimization information. The optimized frame is
738 // described by the input data.
739 DeoptimizationInputData* input_data =
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +0000740 DeoptimizationInputData::cast(compiled_code_->deoptimization_data());
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +0000741 BailoutId node_id = input_data->AstId(bailout_id_);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000742 ByteArray* translations = input_data->TranslationByteArray();
743 unsigned translation_index =
744 input_data->TranslationIndex(bailout_id_)->value();
745
746 // Do the input frame to output frame(s) translation.
747 TranslationIterator iterator(translations, translation_index);
748 Translation::Opcode opcode =
749 static_cast<Translation::Opcode>(iterator.Next());
750 ASSERT(Translation::BEGIN == opcode);
751 USE(opcode);
752 // Read the number of output frames and allocate an array for their
753 // descriptions.
754 int count = iterator.Next();
yangguo@chromium.org659ceec2012-01-26 07:37:54 +0000755 iterator.Next(); // Drop JS frames count.
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000756 ASSERT(output_ == NULL);
757 output_ = new FrameDescription*[count];
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000758 for (int i = 0; i < count; ++i) {
759 output_[i] = NULL;
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000760 }
761 output_count_ = count;
762
763 // Translate each output frame.
764 for (int i = 0; i < count; ++i) {
yangguo@chromium.org659ceec2012-01-26 07:37:54 +0000765 // Read the ast node id, function, and frame height for this output frame.
766 Translation::Opcode opcode =
767 static_cast<Translation::Opcode>(iterator.Next());
768 switch (opcode) {
769 case Translation::JS_FRAME:
770 DoComputeJSFrame(&iterator, i);
771 jsframe_count_++;
772 break;
773 case Translation::ARGUMENTS_ADAPTOR_FRAME:
774 DoComputeArgumentsAdaptorFrame(&iterator, i);
775 break;
ulan@chromium.org967e2702012-02-28 09:49:15 +0000776 case Translation::CONSTRUCT_STUB_FRAME:
777 DoComputeConstructStubFrame(&iterator, i);
778 break;
mstarzinger@chromium.orgde886792012-09-11 13:22:37 +0000779 case Translation::GETTER_STUB_FRAME:
780 DoComputeAccessorStubFrame(&iterator, i, false);
781 break;
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000782 case Translation::SETTER_STUB_FRAME:
mstarzinger@chromium.orgde886792012-09-11 13:22:37 +0000783 DoComputeAccessorStubFrame(&iterator, i, true);
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000784 break;
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +0000785 case Translation::COMPILED_STUB_FRAME:
mstarzinger@chromium.org71fc3462013-02-27 09:34:27 +0000786 DoComputeCompiledStubFrame(&iterator, i);
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +0000787 break;
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000788 case Translation::BEGIN:
789 case Translation::REGISTER:
790 case Translation::INT32_REGISTER:
791 case Translation::UINT32_REGISTER:
792 case Translation::DOUBLE_REGISTER:
793 case Translation::STACK_SLOT:
794 case Translation::INT32_STACK_SLOT:
795 case Translation::UINT32_STACK_SLOT:
796 case Translation::DOUBLE_STACK_SLOT:
797 case Translation::LITERAL:
798 case Translation::ARGUMENTS_OBJECT:
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +0000799 default:
yangguo@chromium.org659ceec2012-01-26 07:37:54 +0000800 UNREACHABLE();
801 break;
802 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000803 }
804
805 // Print some helpful diagnostic information.
mmassi@chromium.org2f0efde2013-02-06 14:12:58 +0000806 if (trace_) {
jkummerow@chromium.orgdc94e192013-08-30 11:35:42 +0000807 double ms = timer.Elapsed().InMillisecondsF();
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000808 int index = output_count_ - 1; // Index of the topmost frame.
809 JSFunction* function = output_[index]->GetFunction();
jkummerow@chromium.org4e308cf2013-05-17 13:39:16 +0000810 PrintF("[deoptimizing (%s): end 0x%08" V8PRIxPTR " ",
811 MessageFor(bailout_type_),
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000812 reinterpret_cast<intptr_t>(function));
jkummerow@chromium.org4e308cf2013-05-17 13:39:16 +0000813 PrintFunctionName();
814 PrintF(" @%d => node=%d, pc=0x%08" V8PRIxPTR ", state=%s, alignment=%s,"
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000815 " took %0.3f ms]\n",
jkummerow@chromium.org4e308cf2013-05-17 13:39:16 +0000816 bailout_id_,
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +0000817 node_id.ToInt(),
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000818 output_[index]->GetPc(),
819 FullCodeGenerator::State2String(
820 static_cast<FullCodeGenerator::State>(
821 output_[index]->GetState()->value())),
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000822 has_alignment_padding_ ? "with padding" : "no padding",
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000823 ms);
824 }
825}
826
827
jkummerow@chromium.org4e308cf2013-05-17 13:39:16 +0000828void Deoptimizer::DoComputeJSFrame(TranslationIterator* iterator,
829 int frame_index) {
830 BailoutId node_id = BailoutId(iterator->Next());
831 JSFunction* function;
832 if (frame_index != 0) {
833 function = JSFunction::cast(ComputeLiteral(iterator->Next()));
834 } else {
835 int closure_id = iterator->Next();
836 USE(closure_id);
837 ASSERT_EQ(Translation::kSelfLiteralId, closure_id);
838 function = function_;
839 }
840 unsigned height = iterator->Next();
841 unsigned height_in_bytes = height * kPointerSize;
842 if (trace_) {
843 PrintF(" translating ");
844 function->PrintName();
845 PrintF(" => node=%d, height=%d\n", node_id.ToInt(), height_in_bytes);
846 }
847
848 // The 'fixed' part of the frame consists of the incoming parameters and
849 // the part described by JavaScriptFrameConstants.
850 unsigned fixed_frame_size = ComputeFixedSize(function);
851 unsigned input_frame_size = input_->GetFrameSize();
852 unsigned output_frame_size = height_in_bytes + fixed_frame_size;
853
854 // Allocate and store the output frame description.
855 FrameDescription* output_frame =
856 new(output_frame_size) FrameDescription(output_frame_size, function);
857 output_frame->SetFrameType(StackFrame::JAVA_SCRIPT);
858
859 bool is_bottommost = (0 == frame_index);
860 bool is_topmost = (output_count_ - 1 == frame_index);
861 ASSERT(frame_index >= 0 && frame_index < output_count_);
862 ASSERT(output_[frame_index] == NULL);
863 output_[frame_index] = output_frame;
864
865 // The top address for the bottommost output frame can be computed from
866 // the input frame pointer and the output frame's height. For all
867 // subsequent output frames, it can be computed from the previous one's
868 // top address and the current frame's size.
869 Register fp_reg = JavaScriptFrame::fp_register();
870 intptr_t top_address;
871 if (is_bottommost) {
872 // Determine whether the input frame contains alignment padding.
873 has_alignment_padding_ = HasAlignmentPadding(function) ? 1 : 0;
874 // 2 = context and function in the frame.
875 // If the optimized frame had alignment padding, adjust the frame pointer
876 // to point to the new position of the old frame pointer after padding
877 // is removed. Subtract 2 * kPointerSize for the context and function slots.
878 top_address = input_->GetRegister(fp_reg.code()) - (2 * kPointerSize) -
879 height_in_bytes + has_alignment_padding_ * kPointerSize;
880 } else {
881 top_address = output_[frame_index - 1]->GetTop() - output_frame_size;
882 }
883 output_frame->SetTop(top_address);
884
885 // Compute the incoming parameter translation.
886 int parameter_count = function->shared()->formal_parameter_count() + 1;
887 unsigned output_offset = output_frame_size;
888 unsigned input_offset = input_frame_size;
889 for (int i = 0; i < parameter_count; ++i) {
890 output_offset -= kPointerSize;
891 DoTranslateCommand(iterator, frame_index, output_offset);
892 }
893 input_offset -= (parameter_count * kPointerSize);
894
895 // There are no translation commands for the caller's pc and fp, the
896 // context, and the function. Synthesize their values and set them up
897 // explicitly.
898 //
899 // The caller's pc for the bottommost output frame is the same as in the
900 // input frame. For all subsequent output frames, it can be read from the
901 // previous one. This frame's pc can be computed from the non-optimized
902 // function code and AST id of the bailout.
yangguo@chromium.orgc73d55b2013-07-24 08:18:28 +0000903 output_offset -= kPCOnStackSize;
904 input_offset -= kPCOnStackSize;
jkummerow@chromium.org4e308cf2013-05-17 13:39:16 +0000905 intptr_t value;
906 if (is_bottommost) {
907 value = input_->GetFrameSlot(input_offset);
908 } else {
909 value = output_[frame_index - 1]->GetPc();
910 }
yangguo@chromium.orgc73d55b2013-07-24 08:18:28 +0000911 output_frame->SetCallerPc(output_offset, value);
jkummerow@chromium.org4e308cf2013-05-17 13:39:16 +0000912 if (trace_) {
913 PrintF(" 0x%08" V8PRIxPTR ": [top + %d] <- 0x%08"
914 V8PRIxPTR " ; caller's pc\n",
915 top_address + output_offset, output_offset, value);
916 }
917
918 // The caller's frame pointer for the bottommost output frame is the same
919 // as in the input frame. For all subsequent output frames, it can be
920 // read from the previous one. Also compute and set this frame's frame
921 // pointer.
yangguo@chromium.orgc73d55b2013-07-24 08:18:28 +0000922 output_offset -= kFPOnStackSize;
923 input_offset -= kFPOnStackSize;
jkummerow@chromium.org4e308cf2013-05-17 13:39:16 +0000924 if (is_bottommost) {
925 value = input_->GetFrameSlot(input_offset);
926 } else {
927 value = output_[frame_index - 1]->GetFp();
928 }
yangguo@chromium.orgc73d55b2013-07-24 08:18:28 +0000929 output_frame->SetCallerFp(output_offset, value);
jkummerow@chromium.org4e308cf2013-05-17 13:39:16 +0000930 intptr_t fp_value = top_address + output_offset;
931 ASSERT(!is_bottommost || (input_->GetRegister(fp_reg.code()) +
932 has_alignment_padding_ * kPointerSize) == fp_value);
933 output_frame->SetFp(fp_value);
934 if (is_topmost) output_frame->SetRegister(fp_reg.code(), fp_value);
935 if (trace_) {
936 PrintF(" 0x%08" V8PRIxPTR ": [top + %d] <- 0x%08"
937 V8PRIxPTR " ; caller's fp\n",
938 fp_value, output_offset, value);
939 }
940 ASSERT(!is_bottommost || !has_alignment_padding_ ||
941 (fp_value & kPointerSize) != 0);
942
943 // For the bottommost output frame the context can be gotten from the input
944 // frame. For all subsequent output frames it can be gotten from the function
945 // so long as we don't inline functions that need local contexts.
946 Register context_reg = JavaScriptFrame::context_register();
947 output_offset -= kPointerSize;
948 input_offset -= kPointerSize;
949 if (is_bottommost) {
950 value = input_->GetFrameSlot(input_offset);
951 } else {
952 value = reinterpret_cast<intptr_t>(function->context());
953 }
954 output_frame->SetFrameSlot(output_offset, value);
955 output_frame->SetContext(value);
956 if (is_topmost) output_frame->SetRegister(context_reg.code(), value);
957 if (trace_) {
958 PrintF(" 0x%08" V8PRIxPTR ": [top + %d] <- 0x%08"
959 V8PRIxPTR "; context\n",
960 top_address + output_offset, output_offset, value);
961 }
962
963 // The function was mentioned explicitly in the BEGIN_FRAME.
964 output_offset -= kPointerSize;
965 input_offset -= kPointerSize;
966 value = reinterpret_cast<intptr_t>(function);
967 // The function for the bottommost output frame should also agree with the
968 // input frame.
969 ASSERT(!is_bottommost || input_->GetFrameSlot(input_offset) == value);
970 output_frame->SetFrameSlot(output_offset, value);
971 if (trace_) {
972 PrintF(" 0x%08" V8PRIxPTR ": [top + %d] <- 0x%08"
973 V8PRIxPTR "; function\n",
974 top_address + output_offset, output_offset, value);
975 }
976
977 // Translate the rest of the frame.
978 for (unsigned i = 0; i < height; ++i) {
979 output_offset -= kPointerSize;
980 DoTranslateCommand(iterator, frame_index, output_offset);
981 }
982 ASSERT(0 == output_offset);
983
984 // Compute this frame's PC, state, and continuation.
985 Code* non_optimized_code = function->shared()->code();
986 FixedArray* raw_data = non_optimized_code->deoptimization_data();
987 DeoptimizationOutputData* data = DeoptimizationOutputData::cast(raw_data);
988 Address start = non_optimized_code->instruction_start();
989 unsigned pc_and_state = GetOutputInfo(data, node_id, function->shared());
990 unsigned pc_offset = FullCodeGenerator::PcField::decode(pc_and_state);
991 intptr_t pc_value = reinterpret_cast<intptr_t>(start + pc_offset);
992 output_frame->SetPc(pc_value);
993
994 FullCodeGenerator::State state =
995 FullCodeGenerator::StateField::decode(pc_and_state);
996 output_frame->SetState(Smi::FromInt(state));
997
998 // Set the continuation for the topmost frame.
999 if (is_topmost && bailout_type_ != DEBUGGER) {
1000 Builtins* builtins = isolate_->builtins();
1001 Code* continuation = builtins->builtin(Builtins::kNotifyDeoptimized);
1002 if (bailout_type_ == LAZY) {
1003 continuation = builtins->builtin(Builtins::kNotifyLazyDeoptimized);
1004 } else if (bailout_type_ == SOFT) {
1005 continuation = builtins->builtin(Builtins::kNotifySoftDeoptimized);
1006 } else {
1007 ASSERT(bailout_type_ == EAGER);
1008 }
1009 output_frame->SetContinuation(
1010 reinterpret_cast<intptr_t>(continuation->entry()));
1011 }
1012}
1013
1014
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001015void Deoptimizer::DoComputeArgumentsAdaptorFrame(TranslationIterator* iterator,
1016 int frame_index) {
1017 JSFunction* function = JSFunction::cast(ComputeLiteral(iterator->Next()));
1018 unsigned height = iterator->Next();
1019 unsigned height_in_bytes = height * kPointerSize;
1020 if (trace_) {
1021 PrintF(" translating arguments adaptor => height=%d\n", height_in_bytes);
1022 }
1023
1024 unsigned fixed_frame_size = ArgumentsAdaptorFrameConstants::kFrameSize;
1025 unsigned output_frame_size = height_in_bytes + fixed_frame_size;
1026
1027 // Allocate and store the output frame description.
1028 FrameDescription* output_frame =
1029 new(output_frame_size) FrameDescription(output_frame_size, function);
1030 output_frame->SetFrameType(StackFrame::ARGUMENTS_ADAPTOR);
1031
1032 // Arguments adaptor can not be topmost or bottommost.
1033 ASSERT(frame_index > 0 && frame_index < output_count_ - 1);
1034 ASSERT(output_[frame_index] == NULL);
1035 output_[frame_index] = output_frame;
1036
1037 // The top address of the frame is computed from the previous
1038 // frame's top and this frame's size.
1039 intptr_t top_address;
1040 top_address = output_[frame_index - 1]->GetTop() - output_frame_size;
1041 output_frame->SetTop(top_address);
1042
1043 // Compute the incoming parameter translation.
1044 int parameter_count = height;
1045 unsigned output_offset = output_frame_size;
1046 for (int i = 0; i < parameter_count; ++i) {
1047 output_offset -= kPointerSize;
1048 DoTranslateCommand(iterator, frame_index, output_offset);
1049 }
1050
1051 // Read caller's PC from the previous frame.
yangguo@chromium.orgc73d55b2013-07-24 08:18:28 +00001052 output_offset -= kPCOnStackSize;
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001053 intptr_t callers_pc = output_[frame_index - 1]->GetPc();
yangguo@chromium.orgc73d55b2013-07-24 08:18:28 +00001054 output_frame->SetCallerPc(output_offset, callers_pc);
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001055 if (trace_) {
1056 PrintF(" 0x%08" V8PRIxPTR ": [top + %d] <- 0x%08"
1057 V8PRIxPTR " ; caller's pc\n",
1058 top_address + output_offset, output_offset, callers_pc);
1059 }
1060
1061 // Read caller's FP from the previous frame, and set this frame's FP.
yangguo@chromium.orgc73d55b2013-07-24 08:18:28 +00001062 output_offset -= kFPOnStackSize;
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001063 intptr_t value = output_[frame_index - 1]->GetFp();
yangguo@chromium.orgc73d55b2013-07-24 08:18:28 +00001064 output_frame->SetCallerFp(output_offset, value);
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001065 intptr_t fp_value = top_address + output_offset;
1066 output_frame->SetFp(fp_value);
1067 if (trace_) {
1068 PrintF(" 0x%08" V8PRIxPTR ": [top + %d] <- 0x%08"
1069 V8PRIxPTR " ; caller's fp\n",
1070 fp_value, output_offset, value);
1071 }
1072
1073 // A marker value is used in place of the context.
1074 output_offset -= kPointerSize;
1075 intptr_t context = reinterpret_cast<intptr_t>(
1076 Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR));
1077 output_frame->SetFrameSlot(output_offset, context);
1078 if (trace_) {
1079 PrintF(" 0x%08" V8PRIxPTR ": [top + %d] <- 0x%08"
1080 V8PRIxPTR " ; context (adaptor sentinel)\n",
1081 top_address + output_offset, output_offset, context);
1082 }
1083
1084 // The function was mentioned explicitly in the ARGUMENTS_ADAPTOR_FRAME.
1085 output_offset -= kPointerSize;
1086 value = reinterpret_cast<intptr_t>(function);
1087 output_frame->SetFrameSlot(output_offset, value);
1088 if (trace_) {
1089 PrintF(" 0x%08" V8PRIxPTR ": [top + %d] <- 0x%08"
1090 V8PRIxPTR " ; function\n",
1091 top_address + output_offset, output_offset, value);
1092 }
1093
1094 // Number of incoming arguments.
1095 output_offset -= kPointerSize;
1096 value = reinterpret_cast<intptr_t>(Smi::FromInt(height - 1));
1097 output_frame->SetFrameSlot(output_offset, value);
1098 if (trace_) {
1099 PrintF(" 0x%08" V8PRIxPTR ": [top + %d] <- 0x%08"
1100 V8PRIxPTR " ; argc (%d)\n",
1101 top_address + output_offset, output_offset, value, height - 1);
1102 }
1103
1104 ASSERT(0 == output_offset);
1105
1106 Builtins* builtins = isolate_->builtins();
1107 Code* adaptor_trampoline =
1108 builtins->builtin(Builtins::kArgumentsAdaptorTrampoline);
1109 intptr_t pc_value = reinterpret_cast<intptr_t>(
1110 adaptor_trampoline->instruction_start() +
1111 isolate_->heap()->arguments_adaptor_deopt_pc_offset()->value());
1112 output_frame->SetPc(pc_value);
1113}
1114
1115
ulan@chromium.org750145a2013-03-07 15:14:13 +00001116void Deoptimizer::DoComputeConstructStubFrame(TranslationIterator* iterator,
1117 int frame_index) {
1118 Builtins* builtins = isolate_->builtins();
1119 Code* construct_stub = builtins->builtin(Builtins::kJSConstructStubGeneric);
1120 JSFunction* function = JSFunction::cast(ComputeLiteral(iterator->Next()));
1121 unsigned height = iterator->Next();
1122 unsigned height_in_bytes = height * kPointerSize;
1123 if (trace_) {
1124 PrintF(" translating construct stub => height=%d\n", height_in_bytes);
1125 }
1126
1127 unsigned fixed_frame_size = ConstructFrameConstants::kFrameSize;
1128 unsigned output_frame_size = height_in_bytes + fixed_frame_size;
1129
1130 // Allocate and store the output frame description.
1131 FrameDescription* output_frame =
1132 new(output_frame_size) FrameDescription(output_frame_size, function);
1133 output_frame->SetFrameType(StackFrame::CONSTRUCT);
1134
1135 // Construct stub can not be topmost or bottommost.
1136 ASSERT(frame_index > 0 && frame_index < output_count_ - 1);
1137 ASSERT(output_[frame_index] == NULL);
1138 output_[frame_index] = output_frame;
1139
1140 // The top address of the frame is computed from the previous
1141 // frame's top and this frame's size.
1142 intptr_t top_address;
1143 top_address = output_[frame_index - 1]->GetTop() - output_frame_size;
1144 output_frame->SetTop(top_address);
1145
1146 // Compute the incoming parameter translation.
1147 int parameter_count = height;
1148 unsigned output_offset = output_frame_size;
1149 for (int i = 0; i < parameter_count; ++i) {
1150 output_offset -= kPointerSize;
danno@chromium.org59400602013-08-13 17:09:37 +00001151 int deferred_object_index = deferred_objects_.length();
ulan@chromium.org750145a2013-03-07 15:14:13 +00001152 DoTranslateCommand(iterator, frame_index, output_offset);
danno@chromium.org59400602013-08-13 17:09:37 +00001153 // The allocated receiver of a construct stub frame is passed as the
1154 // receiver parameter through the translation. It might be encoding
1155 // a captured object, patch the slot address for a captured object.
1156 if (i == 0 && deferred_objects_.length() > deferred_object_index) {
1157 ASSERT(!deferred_objects_[deferred_object_index].is_arguments());
1158 deferred_objects_[deferred_object_index].patch_slot_address(top_address);
1159 }
ulan@chromium.org750145a2013-03-07 15:14:13 +00001160 }
1161
1162 // Read caller's PC from the previous frame.
yangguo@chromium.orgc73d55b2013-07-24 08:18:28 +00001163 output_offset -= kPCOnStackSize;
ulan@chromium.org750145a2013-03-07 15:14:13 +00001164 intptr_t callers_pc = output_[frame_index - 1]->GetPc();
yangguo@chromium.orgc73d55b2013-07-24 08:18:28 +00001165 output_frame->SetCallerPc(output_offset, callers_pc);
ulan@chromium.org750145a2013-03-07 15:14:13 +00001166 if (trace_) {
1167 PrintF(" 0x%08" V8PRIxPTR ": [top + %d] <- 0x%08"
1168 V8PRIxPTR " ; caller's pc\n",
1169 top_address + output_offset, output_offset, callers_pc);
1170 }
1171
1172 // Read caller's FP from the previous frame, and set this frame's FP.
yangguo@chromium.orgc73d55b2013-07-24 08:18:28 +00001173 output_offset -= kFPOnStackSize;
ulan@chromium.org750145a2013-03-07 15:14:13 +00001174 intptr_t value = output_[frame_index - 1]->GetFp();
yangguo@chromium.orgc73d55b2013-07-24 08:18:28 +00001175 output_frame->SetCallerFp(output_offset, value);
ulan@chromium.org750145a2013-03-07 15:14:13 +00001176 intptr_t fp_value = top_address + output_offset;
1177 output_frame->SetFp(fp_value);
1178 if (trace_) {
1179 PrintF(" 0x%08" V8PRIxPTR ": [top + %d] <- 0x%08"
1180 V8PRIxPTR " ; caller's fp\n",
1181 fp_value, output_offset, value);
1182 }
1183
1184 // The context can be gotten from the previous frame.
1185 output_offset -= kPointerSize;
1186 value = output_[frame_index - 1]->GetContext();
1187 output_frame->SetFrameSlot(output_offset, value);
1188 if (trace_) {
1189 PrintF(" 0x%08" V8PRIxPTR ": [top + %d] <- 0x%08"
1190 V8PRIxPTR " ; context\n",
1191 top_address + output_offset, output_offset, value);
1192 }
1193
1194 // A marker value is used in place of the function.
1195 output_offset -= kPointerSize;
1196 value = reinterpret_cast<intptr_t>(Smi::FromInt(StackFrame::CONSTRUCT));
1197 output_frame->SetFrameSlot(output_offset, value);
1198 if (trace_) {
1199 PrintF(" 0x%08" V8PRIxPTR ": [top + %d] <- 0x%08"
1200 V8PRIxPTR " ; function (construct sentinel)\n",
1201 top_address + output_offset, output_offset, value);
1202 }
1203
1204 // The output frame reflects a JSConstructStubGeneric frame.
1205 output_offset -= kPointerSize;
1206 value = reinterpret_cast<intptr_t>(construct_stub);
1207 output_frame->SetFrameSlot(output_offset, value);
1208 if (trace_) {
1209 PrintF(" 0x%08" V8PRIxPTR ": [top + %d] <- 0x%08"
1210 V8PRIxPTR " ; code object\n",
1211 top_address + output_offset, output_offset, value);
1212 }
1213
1214 // Number of incoming arguments.
1215 output_offset -= kPointerSize;
1216 value = reinterpret_cast<intptr_t>(Smi::FromInt(height - 1));
1217 output_frame->SetFrameSlot(output_offset, value);
1218 if (trace_) {
1219 PrintF(" 0x%08" V8PRIxPTR ": [top + %d] <- 0x%08"
1220 V8PRIxPTR " ; argc (%d)\n",
1221 top_address + output_offset, output_offset, value, height - 1);
1222 }
1223
1224 // Constructor function being invoked by the stub (only present on some
1225 // architectures, indicated by kConstructorOffset).
1226 if (ConstructFrameConstants::kConstructorOffset != kMinInt) {
1227 output_offset -= kPointerSize;
1228 value = reinterpret_cast<intptr_t>(function);
1229 output_frame->SetFrameSlot(output_offset, value);
1230 if (trace_) {
1231 PrintF(" 0x%08" V8PRIxPTR ": [top + %d] <- 0x%08"
1232 V8PRIxPTR " ; constructor function\n",
1233 top_address + output_offset, output_offset, value);
1234 }
1235 }
1236
1237 // The newly allocated object was passed as receiver in the artificial
1238 // constructor stub environment created by HEnvironment::CopyForInlining().
1239 output_offset -= kPointerSize;
1240 value = output_frame->GetFrameSlot(output_frame_size - kPointerSize);
1241 output_frame->SetFrameSlot(output_offset, value);
1242 if (trace_) {
1243 PrintF(" 0x%08" V8PRIxPTR ": [top + %d] <- 0x%08"
1244 V8PRIxPTR " ; allocated receiver\n",
1245 top_address + output_offset, output_offset, value);
1246 }
1247
1248 ASSERT(0 == output_offset);
1249
1250 intptr_t pc = reinterpret_cast<intptr_t>(
1251 construct_stub->instruction_start() +
1252 isolate_->heap()->construct_stub_deopt_pc_offset()->value());
1253 output_frame->SetPc(pc);
1254}
1255
1256
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001257void Deoptimizer::DoComputeAccessorStubFrame(TranslationIterator* iterator,
1258 int frame_index,
1259 bool is_setter_stub_frame) {
1260 JSFunction* accessor = JSFunction::cast(ComputeLiteral(iterator->Next()));
1261 // The receiver (and the implicit return value, if any) are expected in
1262 // registers by the LoadIC/StoreIC, so they don't belong to the output stack
1263 // frame. This means that we have to use a height of 0.
1264 unsigned height = 0;
1265 unsigned height_in_bytes = height * kPointerSize;
1266 const char* kind = is_setter_stub_frame ? "setter" : "getter";
1267 if (trace_) {
1268 PrintF(" translating %s stub => height=%u\n", kind, height_in_bytes);
1269 }
1270
1271 // We need 1 stack entry for the return address + 4 stack entries from
1272 // StackFrame::INTERNAL (FP, context, frame type, code object, see
1273 // MacroAssembler::EnterFrame). For a setter stub frame we need one additional
1274 // entry for the implicit return value, see
1275 // StoreStubCompiler::CompileStoreViaSetter.
yangguo@chromium.orgc73d55b2013-07-24 08:18:28 +00001276 unsigned fixed_frame_entries = (kPCOnStackSize / kPointerSize) +
1277 (kFPOnStackSize / kPointerSize) + 3 +
1278 (is_setter_stub_frame ? 1 : 0);
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001279 unsigned fixed_frame_size = fixed_frame_entries * kPointerSize;
1280 unsigned output_frame_size = height_in_bytes + fixed_frame_size;
1281
1282 // Allocate and store the output frame description.
1283 FrameDescription* output_frame =
1284 new(output_frame_size) FrameDescription(output_frame_size, accessor);
1285 output_frame->SetFrameType(StackFrame::INTERNAL);
1286
1287 // A frame for an accessor stub can not be the topmost or bottommost one.
1288 ASSERT(frame_index > 0 && frame_index < output_count_ - 1);
1289 ASSERT(output_[frame_index] == NULL);
1290 output_[frame_index] = output_frame;
1291
1292 // The top address of the frame is computed from the previous frame's top and
1293 // this frame's size.
1294 intptr_t top_address = output_[frame_index - 1]->GetTop() - output_frame_size;
1295 output_frame->SetTop(top_address);
1296
1297 unsigned output_offset = output_frame_size;
1298
1299 // Read caller's PC from the previous frame.
yangguo@chromium.orgc73d55b2013-07-24 08:18:28 +00001300 output_offset -= kPCOnStackSize;
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001301 intptr_t callers_pc = output_[frame_index - 1]->GetPc();
yangguo@chromium.orgc73d55b2013-07-24 08:18:28 +00001302 output_frame->SetCallerPc(output_offset, callers_pc);
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001303 if (trace_) {
1304 PrintF(" 0x%08" V8PRIxPTR ": [top + %u] <- 0x%08" V8PRIxPTR
1305 " ; caller's pc\n",
1306 top_address + output_offset, output_offset, callers_pc);
1307 }
1308
1309 // Read caller's FP from the previous frame, and set this frame's FP.
yangguo@chromium.orgc73d55b2013-07-24 08:18:28 +00001310 output_offset -= kFPOnStackSize;
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001311 intptr_t value = output_[frame_index - 1]->GetFp();
yangguo@chromium.orgc73d55b2013-07-24 08:18:28 +00001312 output_frame->SetCallerFp(output_offset, value);
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001313 intptr_t fp_value = top_address + output_offset;
1314 output_frame->SetFp(fp_value);
1315 if (trace_) {
1316 PrintF(" 0x%08" V8PRIxPTR ": [top + %u] <- 0x%08" V8PRIxPTR
1317 " ; caller's fp\n",
1318 fp_value, output_offset, value);
1319 }
1320
1321 // The context can be gotten from the previous frame.
1322 output_offset -= kPointerSize;
1323 value = output_[frame_index - 1]->GetContext();
1324 output_frame->SetFrameSlot(output_offset, value);
1325 if (trace_) {
1326 PrintF(" 0x%08" V8PRIxPTR ": [top + %u] <- 0x%08" V8PRIxPTR
1327 " ; context\n",
1328 top_address + output_offset, output_offset, value);
1329 }
1330
1331 // A marker value is used in place of the function.
1332 output_offset -= kPointerSize;
1333 value = reinterpret_cast<intptr_t>(Smi::FromInt(StackFrame::INTERNAL));
1334 output_frame->SetFrameSlot(output_offset, value);
1335 if (trace_) {
1336 PrintF(" 0x%08" V8PRIxPTR ": [top + %u] <- 0x%08" V8PRIxPTR
1337 " ; function (%s sentinel)\n",
1338 top_address + output_offset, output_offset, value, kind);
1339 }
1340
1341 // Get Code object from accessor stub.
1342 output_offset -= kPointerSize;
1343 Builtins::Name name = is_setter_stub_frame ?
1344 Builtins::kStoreIC_Setter_ForDeopt :
1345 Builtins::kLoadIC_Getter_ForDeopt;
1346 Code* accessor_stub = isolate_->builtins()->builtin(name);
1347 value = reinterpret_cast<intptr_t>(accessor_stub);
1348 output_frame->SetFrameSlot(output_offset, value);
1349 if (trace_) {
1350 PrintF(" 0x%08" V8PRIxPTR ": [top + %u] <- 0x%08" V8PRIxPTR
1351 " ; code object\n",
1352 top_address + output_offset, output_offset, value);
1353 }
1354
1355 // Skip receiver.
1356 Translation::Opcode opcode =
1357 static_cast<Translation::Opcode>(iterator->Next());
1358 iterator->Skip(Translation::NumberOfOperandsFor(opcode));
1359
1360 if (is_setter_stub_frame) {
1361 // The implicit return value was part of the artificial setter stub
1362 // environment.
1363 output_offset -= kPointerSize;
1364 DoTranslateCommand(iterator, frame_index, output_offset);
1365 }
1366
1367 ASSERT(0 == output_offset);
1368
1369 Smi* offset = is_setter_stub_frame ?
1370 isolate_->heap()->setter_stub_deopt_pc_offset() :
1371 isolate_->heap()->getter_stub_deopt_pc_offset();
1372 intptr_t pc = reinterpret_cast<intptr_t>(
1373 accessor_stub->instruction_start() + offset->value());
1374 output_frame->SetPc(pc);
1375}
1376
1377
ulan@chromium.org6e196bf2013-03-13 09:38:22 +00001378void Deoptimizer::DoComputeCompiledStubFrame(TranslationIterator* iterator,
1379 int frame_index) {
1380 //
1381 // FROM TO
1382 // | .... | | .... |
1383 // +-------------------------+ +-------------------------+
1384 // | JSFunction continuation | | JSFunction continuation |
1385 // +-------------------------+ +-------------------------+
1386 // | | saved frame (FP) | | saved frame (FP) |
1387 // | +=========================+<-fpreg +=========================+<-fpreg
1388 // | | JSFunction context | | JSFunction context |
1389 // v +-------------------------+ +-------------------------|
1390 // | COMPILED_STUB marker | | STUB_FAILURE marker |
1391 // +-------------------------+ +-------------------------+
1392 // | | | caller args.arguments_ |
1393 // | ... | +-------------------------+
1394 // | | | caller args.length_ |
1395 // |-------------------------|<-spreg +-------------------------+
1396 // | caller args pointer |
1397 // +-------------------------+
1398 // | caller stack param 1 |
1399 // parameters in registers +-------------------------+
1400 // and spilled to stack | .... |
1401 // +-------------------------+
1402 // | caller stack param n |
1403 // +-------------------------+<-spreg
1404 // reg = number of parameters
1405 // reg = failure handler address
1406 // reg = saved frame
1407 // reg = JSFunction context
1408 //
1409
mstarzinger@chromium.orgb228be02013-04-18 14:56:59 +00001410 ASSERT(compiled_code_->is_crankshafted() &&
1411 compiled_code_->kind() != Code::OPTIMIZED_FUNCTION);
ulan@chromium.org6e196bf2013-03-13 09:38:22 +00001412 int major_key = compiled_code_->major_key();
1413 CodeStubInterfaceDescriptor* descriptor =
1414 isolate_->code_stub_interface_descriptor(major_key);
1415
1416 // The output frame must have room for all pushed register parameters
1417 // and the standard stack frame slots. Include space for an argument
1418 // object to the callee and optionally the space to pass the argument
1419 // object to the stub failure handler.
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00001420 ASSERT(descriptor->register_param_count_ >= 0);
ulan@chromium.org6e196bf2013-03-13 09:38:22 +00001421 int height_in_bytes = kPointerSize * descriptor->register_param_count_ +
1422 sizeof(Arguments) + kPointerSize;
1423 int fixed_frame_size = StandardFrameConstants::kFixedFrameSize;
1424 int input_frame_size = input_->GetFrameSize();
1425 int output_frame_size = height_in_bytes + fixed_frame_size;
1426 if (trace_) {
1427 PrintF(" translating %s => StubFailureTrampolineStub, height=%d\n",
1428 CodeStub::MajorName(static_cast<CodeStub::Major>(major_key), false),
1429 height_in_bytes);
1430 }
1431
1432 // The stub failure trampoline is a single frame.
1433 FrameDescription* output_frame =
1434 new(output_frame_size) FrameDescription(output_frame_size, NULL);
1435 output_frame->SetFrameType(StackFrame::STUB_FAILURE_TRAMPOLINE);
1436 ASSERT(frame_index == 0);
1437 output_[frame_index] = output_frame;
1438
1439 // The top address for the output frame can be computed from the input
1440 // frame pointer and the output frame's height. Subtract space for the
1441 // context and function slots.
1442 Register fp_reg = StubFailureTrampolineFrame::fp_register();
1443 intptr_t top_address = input_->GetRegister(fp_reg.code()) -
1444 (2 * kPointerSize) - height_in_bytes;
1445 output_frame->SetTop(top_address);
1446
1447 // Read caller's PC (JSFunction continuation) from the input frame.
yangguo@chromium.orgc73d55b2013-07-24 08:18:28 +00001448 unsigned input_frame_offset = input_frame_size - kPCOnStackSize;
1449 unsigned output_frame_offset = output_frame_size - kFPOnStackSize;
ulan@chromium.org6e196bf2013-03-13 09:38:22 +00001450 intptr_t value = input_->GetFrameSlot(input_frame_offset);
yangguo@chromium.orgc73d55b2013-07-24 08:18:28 +00001451 output_frame->SetCallerPc(output_frame_offset, value);
ulan@chromium.org6e196bf2013-03-13 09:38:22 +00001452 if (trace_) {
1453 PrintF(" 0x%08" V8PRIxPTR ": [top + %d] <- 0x%08"
1454 V8PRIxPTR " ; caller's pc\n",
1455 top_address + output_frame_offset, output_frame_offset, value);
1456 }
1457
1458 // Read caller's FP from the input frame, and set this frame's FP.
yangguo@chromium.orgc73d55b2013-07-24 08:18:28 +00001459 input_frame_offset -= kFPOnStackSize;
ulan@chromium.org6e196bf2013-03-13 09:38:22 +00001460 value = input_->GetFrameSlot(input_frame_offset);
yangguo@chromium.orgc73d55b2013-07-24 08:18:28 +00001461 output_frame_offset -= kFPOnStackSize;
1462 output_frame->SetCallerFp(output_frame_offset, value);
ulan@chromium.org6e196bf2013-03-13 09:38:22 +00001463 intptr_t frame_ptr = input_->GetRegister(fp_reg.code());
1464 output_frame->SetRegister(fp_reg.code(), frame_ptr);
1465 output_frame->SetFp(frame_ptr);
1466 if (trace_) {
1467 PrintF(" 0x%08" V8PRIxPTR ": [top + %d] <- 0x%08"
1468 V8PRIxPTR " ; caller's fp\n",
1469 top_address + output_frame_offset, output_frame_offset, value);
1470 }
1471
1472 // The context can be gotten from the input frame.
1473 Register context_reg = StubFailureTrampolineFrame::context_register();
1474 input_frame_offset -= kPointerSize;
1475 value = input_->GetFrameSlot(input_frame_offset);
1476 output_frame->SetRegister(context_reg.code(), value);
1477 output_frame_offset -= kPointerSize;
1478 output_frame->SetFrameSlot(output_frame_offset, value);
1479 if (trace_) {
1480 PrintF(" 0x%08" V8PRIxPTR ": [top + %d] <- 0x%08"
1481 V8PRIxPTR " ; context\n",
1482 top_address + output_frame_offset, output_frame_offset, value);
1483 }
1484
1485 // A marker value is used in place of the function.
1486 output_frame_offset -= kPointerSize;
1487 value = reinterpret_cast<intptr_t>(
1488 Smi::FromInt(StackFrame::STUB_FAILURE_TRAMPOLINE));
1489 output_frame->SetFrameSlot(output_frame_offset, value);
1490 if (trace_) {
1491 PrintF(" 0x%08" V8PRIxPTR ": [top + %d] <- 0x%08"
1492 V8PRIxPTR " ; function (stub failure sentinel)\n",
1493 top_address + output_frame_offset, output_frame_offset, value);
1494 }
1495
1496 intptr_t caller_arg_count = 0;
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +00001497 bool arg_count_known = descriptor->stack_parameter_count_ == NULL;
ulan@chromium.org6e196bf2013-03-13 09:38:22 +00001498
1499 // Build the Arguments object for the caller's parameters and a pointer to it.
1500 output_frame_offset -= kPointerSize;
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +00001501 int args_arguments_offset = output_frame_offset;
1502 intptr_t the_hole = reinterpret_cast<intptr_t>(
1503 isolate_->heap()->the_hole_value());
1504 if (arg_count_known) {
1505 value = frame_ptr + StandardFrameConstants::kCallerSPOffset +
1506 (caller_arg_count - 1) * kPointerSize;
1507 } else {
1508 value = the_hole;
1509 }
1510
1511 output_frame->SetFrameSlot(args_arguments_offset, value);
ulan@chromium.org6e196bf2013-03-13 09:38:22 +00001512 if (trace_) {
1513 PrintF(" 0x%08" V8PRIxPTR ": [top + %d] <- 0x%08"
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +00001514 V8PRIxPTR " ; args.arguments %s\n",
1515 top_address + args_arguments_offset, args_arguments_offset, value,
1516 arg_count_known ? "" : "(the hole)");
ulan@chromium.org6e196bf2013-03-13 09:38:22 +00001517 }
1518
1519 output_frame_offset -= kPointerSize;
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +00001520 int length_frame_offset = output_frame_offset;
1521 value = arg_count_known ? caller_arg_count : the_hole;
1522 output_frame->SetFrameSlot(length_frame_offset, value);
ulan@chromium.org6e196bf2013-03-13 09:38:22 +00001523 if (trace_) {
1524 PrintF(" 0x%08" V8PRIxPTR ": [top + %d] <- 0x%08"
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +00001525 V8PRIxPTR " ; args.length %s\n",
1526 top_address + length_frame_offset, length_frame_offset, value,
1527 arg_count_known ? "" : "(the hole)");
ulan@chromium.org6e196bf2013-03-13 09:38:22 +00001528 }
1529
1530 output_frame_offset -= kPointerSize;
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +00001531 value = frame_ptr + StandardFrameConstants::kCallerSPOffset -
1532 (output_frame_size - output_frame_offset) + kPointerSize;
ulan@chromium.org6e196bf2013-03-13 09:38:22 +00001533 output_frame->SetFrameSlot(output_frame_offset, value);
1534 if (trace_) {
1535 PrintF(" 0x%08" V8PRIxPTR ": [top + %d] <- 0x%08"
1536 V8PRIxPTR " ; args*\n",
1537 top_address + output_frame_offset, output_frame_offset, value);
1538 }
1539
1540 // Copy the register parameters to the failure frame.
1541 for (int i = 0; i < descriptor->register_param_count_; ++i) {
1542 output_frame_offset -= kPointerSize;
1543 DoTranslateCommand(iterator, 0, output_frame_offset);
1544 }
1545
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +00001546 if (!arg_count_known) {
1547 DoTranslateCommand(iterator, 0, length_frame_offset,
1548 TRANSLATED_VALUE_IS_NATIVE);
1549 caller_arg_count = output_frame->GetFrameSlot(length_frame_offset);
1550 value = frame_ptr + StandardFrameConstants::kCallerSPOffset +
1551 (caller_arg_count - 1) * kPointerSize;
1552 output_frame->SetFrameSlot(args_arguments_offset, value);
1553 if (trace_) {
1554 PrintF(" 0x%08" V8PRIxPTR ": [top + %d] <- 0x%08"
1555 V8PRIxPTR " ; args.arguments\n",
1556 top_address + args_arguments_offset, args_arguments_offset, value);
1557 }
1558 }
1559
ulan@chromium.org6e196bf2013-03-13 09:38:22 +00001560 ASSERT(0 == output_frame_offset);
1561
1562 // Copy the double registers from the input into the output frame.
1563 CopyDoubleRegisters(output_frame);
1564
1565 // Fill registers containing handler and number of parameters.
1566 SetPlatformCompiledStubRegisters(output_frame, descriptor);
1567
1568 // Compute this frame's PC, state, and continuation.
1569 Code* trampoline = NULL;
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +00001570 StubFunctionMode function_mode = descriptor->function_mode_;
1571 StubFailureTrampolineStub(function_mode).FindCodeInCache(&trampoline,
1572 isolate_);
ulan@chromium.org6e196bf2013-03-13 09:38:22 +00001573 ASSERT(trampoline != NULL);
1574 output_frame->SetPc(reinterpret_cast<intptr_t>(
1575 trampoline->instruction_start()));
1576 output_frame->SetState(Smi::FromInt(FullCodeGenerator::NO_REGISTERS));
1577 Code* notify_failure =
1578 isolate_->builtins()->builtin(Builtins::kNotifyStubFailure);
1579 output_frame->SetContinuation(
1580 reinterpret_cast<intptr_t>(notify_failure->entry()));
1581}
1582
1583
danno@chromium.org59400602013-08-13 17:09:37 +00001584Handle<Object> Deoptimizer::MaterializeNextHeapObject() {
1585 int object_index = materialization_object_index_++;
1586 ObjectMaterializationDescriptor desc = deferred_objects_[object_index];
1587 const int length = desc.object_length();
1588
1589 if (desc.duplicate_object() >= 0) {
1590 // Found a previously materialized object by de-duplication.
1591 object_index = desc.duplicate_object();
1592 materialized_objects_->Add(Handle<Object>());
1593 } else if (desc.is_arguments() && ArgumentsObjectIsAdapted(object_index)) {
1594 // Use the arguments adapter frame we just built to materialize the
1595 // arguments object. FunctionGetArguments can't throw an exception.
1596 Handle<JSFunction> function = ArgumentsObjectFunction(object_index);
1597 Handle<JSObject> arguments = Handle<JSObject>::cast(
1598 Accessors::FunctionGetArguments(function));
1599 materialized_objects_->Add(arguments);
1600 materialization_value_index_ += length;
1601 } else if (desc.is_arguments()) {
1602 // Construct an arguments object and copy the parameters to a newly
1603 // allocated arguments object backing store.
1604 Handle<JSFunction> function = ArgumentsObjectFunction(object_index);
1605 Handle<JSObject> arguments =
1606 isolate_->factory()->NewArgumentsObject(function, length);
1607 Handle<FixedArray> array = isolate_->factory()->NewFixedArray(length);
1608 ASSERT(array->length() == length);
1609 arguments->set_elements(*array);
1610 materialized_objects_->Add(arguments);
1611 for (int i = 0; i < length; ++i) {
1612 Handle<Object> value = MaterializeNextValue();
1613 array->set(i, *value);
1614 }
1615 } else {
1616 // Dispatch on the instance type of the object to be materialized.
1617 Handle<Map> map = Handle<Map>::cast(MaterializeNextValue());
1618 switch (map->instance_type()) {
1619 case HEAP_NUMBER_TYPE: {
machenbach@chromium.org528ce022013-09-23 14:09:36 +00001620 Handle<HeapNumber> object = isolate_->factory()->NewHeapNumber(0.0);
1621 materialized_objects_->Add(object);
1622 Handle<Object> number = MaterializeNextValue();
1623 object->set_value(number->Number());
danno@chromium.org59400602013-08-13 17:09:37 +00001624 materialization_value_index_ += kDoubleSize / kPointerSize - 1;
1625 break;
1626 }
1627 case JS_OBJECT_TYPE: {
1628 Handle<JSObject> object =
1629 isolate_->factory()->NewJSObjectFromMap(map, NOT_TENURED, false);
1630 materialized_objects_->Add(object);
1631 Handle<Object> properties = MaterializeNextValue();
1632 Handle<Object> elements = MaterializeNextValue();
1633 object->set_properties(FixedArray::cast(*properties));
jkummerow@chromium.orgdc94e192013-08-30 11:35:42 +00001634 object->set_elements(FixedArrayBase::cast(*elements));
danno@chromium.org59400602013-08-13 17:09:37 +00001635 for (int i = 0; i < length - 3; ++i) {
1636 Handle<Object> value = MaterializeNextValue();
1637 object->FastPropertyAtPut(i, *value);
1638 }
1639 break;
1640 }
jkummerow@chromium.orgdc94e192013-08-30 11:35:42 +00001641 case JS_ARRAY_TYPE: {
1642 Handle<JSArray> object =
1643 isolate_->factory()->NewJSArray(0, map->elements_kind());
1644 materialized_objects_->Add(object);
1645 Handle<Object> properties = MaterializeNextValue();
1646 Handle<Object> elements = MaterializeNextValue();
1647 Handle<Object> length = MaterializeNextValue();
1648 object->set_properties(FixedArray::cast(*properties));
1649 object->set_elements(FixedArrayBase::cast(*elements));
1650 object->set_length(*length);
1651 break;
1652 }
danno@chromium.org59400602013-08-13 17:09:37 +00001653 default:
1654 PrintF("[couldn't handle instance type %d]\n", map->instance_type());
1655 UNREACHABLE();
1656 }
1657 }
1658
1659 return materialized_objects_->at(object_index);
1660}
1661
1662
1663Handle<Object> Deoptimizer::MaterializeNextValue() {
1664 int value_index = materialization_value_index_++;
1665 Handle<Object> value = materialized_values_->at(value_index);
1666 if (*value == isolate_->heap()->arguments_marker()) {
1667 value = MaterializeNextHeapObject();
1668 }
1669 return value;
1670}
1671
1672
ulan@chromium.org56c14af2012-09-20 12:51:09 +00001673void Deoptimizer::MaterializeHeapObjects(JavaScriptFrameIterator* it) {
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001674 ASSERT_NE(DEBUGGER, bailout_type_);
ulan@chromium.org56c14af2012-09-20 12:51:09 +00001675
danno@chromium.org59400602013-08-13 17:09:37 +00001676 // Walk all JavaScript output frames with the given frame iterator.
1677 for (int frame_index = 0; frame_index < jsframe_count(); ++frame_index) {
1678 if (frame_index != 0) it->Advance();
1679 JavaScriptFrame* frame = it->frame();
1680 jsframe_functions_.Add(handle(frame->function(), isolate_));
1681 jsframe_has_adapted_arguments_.Add(frame->has_adapted_arguments());
1682 }
1683
dslomov@chromium.orgb752d402013-06-18 11:54:54 +00001684 // Handlify all tagged object values before triggering any allocation.
1685 List<Handle<Object> > values(deferred_objects_tagged_values_.length());
1686 for (int i = 0; i < deferred_objects_tagged_values_.length(); ++i) {
1687 values.Add(Handle<Object>(deferred_objects_tagged_values_[i], isolate_));
ulan@chromium.org56c14af2012-09-20 12:51:09 +00001688 }
1689
1690 // Play it safe and clear all unhandlified values before we continue.
dslomov@chromium.orgb752d402013-06-18 11:54:54 +00001691 deferred_objects_tagged_values_.Clear();
ulan@chromium.org56c14af2012-09-20 12:51:09 +00001692
1693 // Materialize all heap numbers before looking at arguments because when the
1694 // output frames are used to materialize arguments objects later on they need
1695 // to already contain valid heap numbers.
karlklose@chromium.org44bc7082011-04-11 12:33:05 +00001696 for (int i = 0; i < deferred_heap_numbers_.length(); i++) {
mstarzinger@chromium.orgb4968be2013-10-16 09:00:56 +00001697 HeapNumberMaterializationDescriptor<Address> d = deferred_heap_numbers_[i];
karlklose@chromium.org44bc7082011-04-11 12:33:05 +00001698 Handle<Object> num = isolate_->factory()->NewNumber(d.value());
mmassi@chromium.org2f0efde2013-02-06 14:12:58 +00001699 if (trace_) {
danno@chromium.org59400602013-08-13 17:09:37 +00001700 PrintF("Materialized a new heap number %p [%e] in slot %p\n",
karlklose@chromium.org44bc7082011-04-11 12:33:05 +00001701 reinterpret_cast<void*>(*num),
1702 d.value(),
mstarzinger@chromium.orgb4968be2013-10-16 09:00:56 +00001703 d.destination());
karlklose@chromium.org44bc7082011-04-11 12:33:05 +00001704 }
mstarzinger@chromium.orgb4968be2013-10-16 09:00:56 +00001705 Memory::Object_at(d.destination()) = *num;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001706 }
ulan@chromium.org56c14af2012-09-20 12:51:09 +00001707
danno@chromium.org59400602013-08-13 17:09:37 +00001708 // Materialize all heap numbers required for arguments/captured objects.
mstarzinger@chromium.orgb4968be2013-10-16 09:00:56 +00001709 for (int i = 0; i < deferred_objects_double_values_.length(); i++) {
1710 HeapNumberMaterializationDescriptor<int> d =
1711 deferred_objects_double_values_[i];
1712 Handle<Object> num = isolate_->factory()->NewNumber(d.value());
dslomov@chromium.orgb752d402013-06-18 11:54:54 +00001713 if (trace_) {
mstarzinger@chromium.orgb4968be2013-10-16 09:00:56 +00001714 PrintF("Materialized a new heap number %p [%e] for object at %d\n",
1715 reinterpret_cast<void*>(*num),
1716 d.value(),
1717 d.destination());
dslomov@chromium.orgb752d402013-06-18 11:54:54 +00001718 }
mstarzinger@chromium.orgb4968be2013-10-16 09:00:56 +00001719 ASSERT(values.at(d.destination())->IsTheHole());
1720 values.Set(d.destination(), num);
dslomov@chromium.orgb752d402013-06-18 11:54:54 +00001721 }
1722
mstarzinger@chromium.orgb4968be2013-10-16 09:00:56 +00001723 // Play it safe and clear all object double values before we continue.
1724 deferred_objects_double_values_.Clear();
1725
danno@chromium.org59400602013-08-13 17:09:37 +00001726 // Materialize arguments/captured objects.
1727 if (!deferred_objects_.is_empty()) {
1728 List<Handle<Object> > materialized_objects(deferred_objects_.length());
1729 materialized_objects_ = &materialized_objects;
1730 materialized_values_ = &values;
1731
1732 while (materialization_object_index_ < deferred_objects_.length()) {
1733 int object_index = materialization_object_index_;
1734 ObjectMaterializationDescriptor descriptor =
1735 deferred_objects_.at(object_index);
1736
1737 // Find a previously materialized object by de-duplication or
1738 // materialize a new instance of the object if necessary. Store
1739 // the materialized object into the frame slot.
1740 Handle<Object> object = MaterializeNextHeapObject();
1741 Memory::Object_at(descriptor.slot_address()) = *object;
1742 if (trace_) {
1743 if (descriptor.is_arguments()) {
1744 PrintF("Materialized %sarguments object of length %d for %p: ",
1745 ArgumentsObjectIsAdapted(object_index) ? "(adapted) " : "",
1746 Handle<JSObject>::cast(object)->elements()->length(),
ulan@chromium.org56c14af2012-09-20 12:51:09 +00001747 reinterpret_cast<void*>(descriptor.slot_address()));
danno@chromium.org59400602013-08-13 17:09:37 +00001748 } else {
1749 PrintF("Materialized captured object of size %d for %p: ",
1750 Handle<HeapObject>::cast(object)->Size(),
1751 reinterpret_cast<void*>(descriptor.slot_address()));
ulan@chromium.org56c14af2012-09-20 12:51:09 +00001752 }
danno@chromium.org59400602013-08-13 17:09:37 +00001753 object->ShortPrint();
1754 PrintF("\n");
ulan@chromium.org56c14af2012-09-20 12:51:09 +00001755 }
1756 }
danno@chromium.org59400602013-08-13 17:09:37 +00001757
1758 ASSERT(materialization_object_index_ == materialized_objects_->length());
1759 ASSERT(materialization_value_index_ == materialized_values_->length());
ulan@chromium.org56c14af2012-09-20 12:51:09 +00001760 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001761}
1762
1763
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001764#ifdef ENABLE_DEBUGGER_SUPPORT
1765void Deoptimizer::MaterializeHeapNumbersForDebuggerInspectableFrame(
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001766 Address parameters_top,
1767 uint32_t parameters_size,
1768 Address expressions_top,
1769 uint32_t expressions_size,
1770 DeoptimizedFrameInfo* info) {
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001771 ASSERT_EQ(DEBUGGER, bailout_type_);
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001772 Address parameters_bottom = parameters_top + parameters_size;
1773 Address expressions_bottom = expressions_top + expressions_size;
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001774 for (int i = 0; i < deferred_heap_numbers_.length(); i++) {
mstarzinger@chromium.orgb4968be2013-10-16 09:00:56 +00001775 HeapNumberMaterializationDescriptor<Address> d = deferred_heap_numbers_[i];
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001776
1777 // Check of the heap number to materialize actually belong to the frame
1778 // being extracted.
mstarzinger@chromium.orgb4968be2013-10-16 09:00:56 +00001779 Address slot = d.destination();
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001780 if (parameters_top <= slot && slot < parameters_bottom) {
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001781 Handle<Object> num = isolate_->factory()->NewNumber(d.value());
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001782
1783 int index = (info->parameters_count() - 1) -
1784 static_cast<int>(slot - parameters_top) / kPointerSize;
1785
mmassi@chromium.org2f0efde2013-02-06 14:12:58 +00001786 if (trace_) {
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001787 PrintF("Materializing a new heap number %p [%e] in slot %p"
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001788 "for parameter slot #%d\n",
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001789 reinterpret_cast<void*>(*num),
1790 d.value(),
mstarzinger@chromium.orgb4968be2013-10-16 09:00:56 +00001791 d.destination(),
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00001792 index);
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001793 }
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001794
1795 info->SetParameter(index, *num);
1796 } else if (expressions_top <= slot && slot < expressions_bottom) {
1797 Handle<Object> num = isolate_->factory()->NewNumber(d.value());
1798
1799 int index = info->expression_count() - 1 -
1800 static_cast<int>(slot - expressions_top) / kPointerSize;
1801
mmassi@chromium.org2f0efde2013-02-06 14:12:58 +00001802 if (trace_) {
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001803 PrintF("Materializing a new heap number %p [%e] in slot %p"
1804 "for expression slot #%d\n",
1805 reinterpret_cast<void*>(*num),
1806 d.value(),
mstarzinger@chromium.orgb4968be2013-10-16 09:00:56 +00001807 d.destination(),
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001808 index);
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00001809 }
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001810
1811 info->SetExpression(index, *num);
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001812 }
1813 }
1814}
1815#endif
1816
1817
dslomov@chromium.orgb752d402013-06-18 11:54:54 +00001818static const char* TraceValueType(bool is_smi, bool is_native = false) {
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +00001819 if (is_native) {
1820 return "native";
1821 } else if (is_smi) {
1822 return "smi";
1823 }
1824
1825 return "heap number";
1826}
1827
1828
dslomov@chromium.orgb752d402013-06-18 11:54:54 +00001829void Deoptimizer::DoTranslateObject(TranslationIterator* iterator,
danno@chromium.org59400602013-08-13 17:09:37 +00001830 int object_index,
dslomov@chromium.orgb752d402013-06-18 11:54:54 +00001831 int field_index) {
1832 disasm::NameConverter converter;
danno@chromium.org59400602013-08-13 17:09:37 +00001833 Address object_slot = deferred_objects_[object_index].slot_address();
dslomov@chromium.orgb752d402013-06-18 11:54:54 +00001834
dslomov@chromium.orgb752d402013-06-18 11:54:54 +00001835 Translation::Opcode opcode =
1836 static_cast<Translation::Opcode>(iterator->Next());
dslomov@chromium.orgb752d402013-06-18 11:54:54 +00001837
1838 switch (opcode) {
1839 case Translation::BEGIN:
1840 case Translation::JS_FRAME:
1841 case Translation::ARGUMENTS_ADAPTOR_FRAME:
1842 case Translation::CONSTRUCT_STUB_FRAME:
1843 case Translation::GETTER_STUB_FRAME:
1844 case Translation::SETTER_STUB_FRAME:
1845 case Translation::COMPILED_STUB_FRAME:
dslomov@chromium.orgb752d402013-06-18 11:54:54 +00001846 UNREACHABLE();
1847 return;
1848
1849 case Translation::REGISTER: {
1850 int input_reg = iterator->Next();
1851 intptr_t input_value = input_->GetRegister(input_reg);
1852 if (trace_) {
1853 PrintF(" object @0x%08" V8PRIxPTR ": [field #%d] <- ",
1854 reinterpret_cast<intptr_t>(object_slot),
1855 field_index);
1856 PrintF("0x%08" V8PRIxPTR " ; %s ", input_value,
1857 converter.NameOfCPURegister(input_reg));
1858 reinterpret_cast<Object*>(input_value)->ShortPrint();
1859 PrintF("\n");
1860 }
1861 AddObjectTaggedValue(input_value);
1862 return;
1863 }
1864
1865 case Translation::INT32_REGISTER: {
1866 int input_reg = iterator->Next();
1867 intptr_t value = input_->GetRegister(input_reg);
1868 bool is_smi = Smi::IsValid(value);
1869 if (trace_) {
1870 PrintF(" object @0x%08" V8PRIxPTR ": [field #%d] <- ",
1871 reinterpret_cast<intptr_t>(object_slot),
1872 field_index);
1873 PrintF("%" V8PRIdPTR " ; %s (%s)\n", value,
1874 converter.NameOfCPURegister(input_reg),
1875 TraceValueType(is_smi));
1876 }
1877 if (is_smi) {
1878 intptr_t tagged_value =
1879 reinterpret_cast<intptr_t>(Smi::FromInt(static_cast<int>(value)));
1880 AddObjectTaggedValue(tagged_value);
1881 } else {
1882 double double_value = static_cast<double>(static_cast<int32_t>(value));
1883 AddObjectDoubleValue(double_value);
1884 }
1885 return;
1886 }
1887
1888 case Translation::UINT32_REGISTER: {
1889 int input_reg = iterator->Next();
1890 uintptr_t value = static_cast<uintptr_t>(input_->GetRegister(input_reg));
1891 bool is_smi = (value <= static_cast<uintptr_t>(Smi::kMaxValue));
1892 if (trace_) {
1893 PrintF(" object @0x%08" V8PRIxPTR ": [field #%d] <- ",
1894 reinterpret_cast<intptr_t>(object_slot),
1895 field_index);
1896 PrintF("%" V8PRIdPTR " ; uint %s (%s)\n", value,
1897 converter.NameOfCPURegister(input_reg),
1898 TraceValueType(is_smi));
1899 }
1900 if (is_smi) {
1901 intptr_t tagged_value =
1902 reinterpret_cast<intptr_t>(Smi::FromInt(static_cast<int>(value)));
1903 AddObjectTaggedValue(tagged_value);
1904 } else {
1905 double double_value = static_cast<double>(static_cast<uint32_t>(value));
1906 AddObjectDoubleValue(double_value);
1907 }
1908 return;
1909 }
1910
1911 case Translation::DOUBLE_REGISTER: {
1912 int input_reg = iterator->Next();
1913 double value = input_->GetDoubleRegister(input_reg);
1914 if (trace_) {
1915 PrintF(" object @0x%08" V8PRIxPTR ": [field #%d] <- ",
1916 reinterpret_cast<intptr_t>(object_slot),
1917 field_index);
1918 PrintF("%e ; %s\n", value,
1919 DoubleRegister::AllocationIndexToString(input_reg));
1920 }
1921 AddObjectDoubleValue(value);
1922 return;
1923 }
1924
1925 case Translation::STACK_SLOT: {
1926 int input_slot_index = iterator->Next();
1927 unsigned input_offset = input_->GetOffsetFromSlotIndex(input_slot_index);
1928 intptr_t input_value = input_->GetFrameSlot(input_offset);
1929 if (trace_) {
1930 PrintF(" object @0x%08" V8PRIxPTR ": [field #%d] <- ",
1931 reinterpret_cast<intptr_t>(object_slot),
1932 field_index);
1933 PrintF("0x%08" V8PRIxPTR " ; [sp + %d] ", input_value, input_offset);
1934 reinterpret_cast<Object*>(input_value)->ShortPrint();
1935 PrintF("\n");
1936 }
1937 AddObjectTaggedValue(input_value);
1938 return;
1939 }
1940
1941 case Translation::INT32_STACK_SLOT: {
1942 int input_slot_index = iterator->Next();
1943 unsigned input_offset = input_->GetOffsetFromSlotIndex(input_slot_index);
1944 intptr_t value = input_->GetFrameSlot(input_offset);
1945 bool is_smi = Smi::IsValid(value);
1946 if (trace_) {
1947 PrintF(" object @0x%08" V8PRIxPTR ": [field #%d] <- ",
1948 reinterpret_cast<intptr_t>(object_slot),
1949 field_index);
1950 PrintF("%" V8PRIdPTR " ; [sp + %d] (%s)\n",
1951 value, input_offset, TraceValueType(is_smi));
1952 }
1953 if (is_smi) {
1954 intptr_t tagged_value =
1955 reinterpret_cast<intptr_t>(Smi::FromInt(static_cast<int>(value)));
1956 AddObjectTaggedValue(tagged_value);
1957 } else {
1958 double double_value = static_cast<double>(static_cast<int32_t>(value));
1959 AddObjectDoubleValue(double_value);
1960 }
1961 return;
1962 }
1963
1964 case Translation::UINT32_STACK_SLOT: {
1965 int input_slot_index = iterator->Next();
1966 unsigned input_offset = input_->GetOffsetFromSlotIndex(input_slot_index);
1967 uintptr_t value =
1968 static_cast<uintptr_t>(input_->GetFrameSlot(input_offset));
1969 bool is_smi = (value <= static_cast<uintptr_t>(Smi::kMaxValue));
1970 if (trace_) {
1971 PrintF(" object @0x%08" V8PRIxPTR ": [field #%d] <- ",
1972 reinterpret_cast<intptr_t>(object_slot),
1973 field_index);
1974 PrintF("%" V8PRIdPTR " ; [sp + %d] (uint %s)\n",
1975 value, input_offset, TraceValueType(is_smi));
1976 }
1977 if (is_smi) {
1978 intptr_t tagged_value =
1979 reinterpret_cast<intptr_t>(Smi::FromInt(static_cast<int>(value)));
1980 AddObjectTaggedValue(tagged_value);
1981 } else {
1982 double double_value = static_cast<double>(static_cast<uint32_t>(value));
1983 AddObjectDoubleValue(double_value);
1984 }
1985 return;
1986 }
1987
1988 case Translation::DOUBLE_STACK_SLOT: {
1989 int input_slot_index = iterator->Next();
1990 unsigned input_offset = input_->GetOffsetFromSlotIndex(input_slot_index);
1991 double value = input_->GetDoubleFrameSlot(input_offset);
1992 if (trace_) {
1993 PrintF(" object @0x%08" V8PRIxPTR ": [field #%d] <- ",
1994 reinterpret_cast<intptr_t>(object_slot),
1995 field_index);
1996 PrintF("%e ; [sp + %d]\n", value, input_offset);
1997 }
1998 AddObjectDoubleValue(value);
1999 return;
2000 }
2001
2002 case Translation::LITERAL: {
2003 Object* literal = ComputeLiteral(iterator->Next());
2004 if (trace_) {
2005 PrintF(" object @0x%08" V8PRIxPTR ": [field #%d] <- ",
2006 reinterpret_cast<intptr_t>(object_slot),
2007 field_index);
2008 literal->ShortPrint();
2009 PrintF(" ; literal\n");
2010 }
2011 intptr_t value = reinterpret_cast<intptr_t>(literal);
2012 AddObjectTaggedValue(value);
2013 return;
2014 }
danno@chromium.org59400602013-08-13 17:09:37 +00002015
2016 case Translation::DUPLICATED_OBJECT: {
2017 int object_index = iterator->Next();
2018 if (trace_) {
2019 PrintF(" nested @0x%08" V8PRIxPTR ": [field #%d] <- ",
2020 reinterpret_cast<intptr_t>(object_slot),
2021 field_index);
2022 isolate_->heap()->arguments_marker()->ShortPrint();
2023 PrintF(" ; duplicate of object #%d\n", object_index);
2024 }
2025 // Use the materialization marker value as a sentinel and fill in
2026 // the object after the deoptimized frame is built.
2027 intptr_t value = reinterpret_cast<intptr_t>(
2028 isolate_->heap()->arguments_marker());
2029 AddObjectDuplication(0, object_index);
2030 AddObjectTaggedValue(value);
2031 return;
2032 }
2033
2034 case Translation::ARGUMENTS_OBJECT:
2035 case Translation::CAPTURED_OBJECT: {
2036 int length = iterator->Next();
2037 bool is_args = opcode == Translation::ARGUMENTS_OBJECT;
2038 if (trace_) {
2039 PrintF(" nested @0x%08" V8PRIxPTR ": [field #%d] <- ",
2040 reinterpret_cast<intptr_t>(object_slot),
2041 field_index);
2042 isolate_->heap()->arguments_marker()->ShortPrint();
2043 PrintF(" ; object (length = %d, is_args = %d)\n", length, is_args);
2044 }
2045 // Use the materialization marker value as a sentinel and fill in
2046 // the object after the deoptimized frame is built.
2047 intptr_t value = reinterpret_cast<intptr_t>(
2048 isolate_->heap()->arguments_marker());
2049 AddObjectStart(0, length, is_args);
2050 AddObjectTaggedValue(value);
2051 // We save the object values on the side and materialize the actual
2052 // object after the deoptimized frame is built.
2053 int object_index = deferred_objects_.length() - 1;
2054 for (int i = 0; i < length; i++) {
2055 DoTranslateObject(iterator, object_index, i);
2056 }
2057 return;
2058 }
dslomov@chromium.orgb752d402013-06-18 11:54:54 +00002059 }
2060}
2061
2062
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002063void Deoptimizer::DoTranslateCommand(TranslationIterator* iterator,
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +00002064 int frame_index,
2065 unsigned output_offset,
2066 DeoptimizerTranslatedValueType value_type) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002067 disasm::NameConverter converter;
2068 // A GC-safe temporary placeholder that we can put in the output frame.
2069 const intptr_t kPlaceholder = reinterpret_cast<intptr_t>(Smi::FromInt(0));
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +00002070 bool is_native = value_type == TRANSLATED_VALUE_IS_NATIVE;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002071
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002072 Translation::Opcode opcode =
2073 static_cast<Translation::Opcode>(iterator->Next());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002074
2075 switch (opcode) {
2076 case Translation::BEGIN:
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002077 case Translation::JS_FRAME:
2078 case Translation::ARGUMENTS_ADAPTOR_FRAME:
ulan@chromium.org967e2702012-02-28 09:49:15 +00002079 case Translation::CONSTRUCT_STUB_FRAME:
mstarzinger@chromium.orgde886792012-09-11 13:22:37 +00002080 case Translation::GETTER_STUB_FRAME:
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00002081 case Translation::SETTER_STUB_FRAME:
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00002082 case Translation::COMPILED_STUB_FRAME:
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002083 UNREACHABLE();
2084 return;
2085
2086 case Translation::REGISTER: {
2087 int input_reg = iterator->Next();
2088 intptr_t input_value = input_->GetRegister(input_reg);
mmassi@chromium.org2f0efde2013-02-06 14:12:58 +00002089 if (trace_) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002090 PrintF(
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002091 " 0x%08" V8PRIxPTR ": [top + %d] <- 0x%08" V8PRIxPTR " ; %s ",
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002092 output_[frame_index]->GetTop() + output_offset,
2093 output_offset,
2094 input_value,
2095 converter.NameOfCPURegister(input_reg));
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002096 reinterpret_cast<Object*>(input_value)->ShortPrint();
2097 PrintF("\n");
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002098 }
2099 output_[frame_index]->SetFrameSlot(output_offset, input_value);
2100 return;
2101 }
2102
2103 case Translation::INT32_REGISTER: {
2104 int input_reg = iterator->Next();
2105 intptr_t value = input_->GetRegister(input_reg);
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +00002106 bool is_smi = (value_type == TRANSLATED_VALUE_IS_TAGGED) &&
2107 Smi::IsValid(value);
mmassi@chromium.org2f0efde2013-02-06 14:12:58 +00002108 if (trace_) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002109 PrintF(
2110 " 0x%08" V8PRIxPTR ": [top + %d] <- %" V8PRIdPTR " ; %s (%s)\n",
2111 output_[frame_index]->GetTop() + output_offset,
2112 output_offset,
2113 value,
2114 converter.NameOfCPURegister(input_reg),
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +00002115 TraceValueType(is_smi, is_native));
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002116 }
2117 if (is_smi) {
2118 intptr_t tagged_value =
2119 reinterpret_cast<intptr_t>(Smi::FromInt(static_cast<int>(value)));
2120 output_[frame_index]->SetFrameSlot(output_offset, tagged_value);
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +00002121 } else if (value_type == TRANSLATED_VALUE_IS_NATIVE) {
2122 output_[frame_index]->SetFrameSlot(output_offset, value);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002123 } else {
2124 // We save the untagged value on the side and store a GC-safe
2125 // temporary placeholder in the frame.
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +00002126 ASSERT(value_type == TRANSLATED_VALUE_IS_TAGGED);
karlklose@chromium.org44bc7082011-04-11 12:33:05 +00002127 AddDoubleValue(output_[frame_index]->GetTop() + output_offset,
2128 static_cast<double>(static_cast<int32_t>(value)));
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002129 output_[frame_index]->SetFrameSlot(output_offset, kPlaceholder);
2130 }
2131 return;
2132 }
2133
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00002134 case Translation::UINT32_REGISTER: {
2135 int input_reg = iterator->Next();
2136 uintptr_t value = static_cast<uintptr_t>(input_->GetRegister(input_reg));
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +00002137 bool is_smi = (value_type == TRANSLATED_VALUE_IS_TAGGED) &&
2138 (value <= static_cast<uintptr_t>(Smi::kMaxValue));
mmassi@chromium.org2f0efde2013-02-06 14:12:58 +00002139 if (trace_) {
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00002140 PrintF(
2141 " 0x%08" V8PRIxPTR ": [top + %d] <- %" V8PRIuPTR
2142 " ; uint %s (%s)\n",
2143 output_[frame_index]->GetTop() + output_offset,
2144 output_offset,
2145 value,
2146 converter.NameOfCPURegister(input_reg),
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +00002147 TraceValueType(is_smi, is_native));
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00002148 }
2149 if (is_smi) {
2150 intptr_t tagged_value =
2151 reinterpret_cast<intptr_t>(Smi::FromInt(static_cast<int>(value)));
2152 output_[frame_index]->SetFrameSlot(output_offset, tagged_value);
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +00002153 } else if (value_type == TRANSLATED_VALUE_IS_NATIVE) {
2154 output_[frame_index]->SetFrameSlot(output_offset, value);
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00002155 } else {
2156 // We save the untagged value on the side and store a GC-safe
2157 // temporary placeholder in the frame.
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +00002158 ASSERT(value_type == TRANSLATED_VALUE_IS_TAGGED);
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00002159 AddDoubleValue(output_[frame_index]->GetTop() + output_offset,
2160 static_cast<double>(static_cast<uint32_t>(value)));
2161 output_[frame_index]->SetFrameSlot(output_offset, kPlaceholder);
2162 }
2163 return;
2164 }
2165
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002166 case Translation::DOUBLE_REGISTER: {
2167 int input_reg = iterator->Next();
2168 double value = input_->GetDoubleRegister(input_reg);
mmassi@chromium.org2f0efde2013-02-06 14:12:58 +00002169 if (trace_) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002170 PrintF(" 0x%08" V8PRIxPTR ": [top + %d] <- %e ; %s\n",
2171 output_[frame_index]->GetTop() + output_offset,
2172 output_offset,
2173 value,
2174 DoubleRegister::AllocationIndexToString(input_reg));
2175 }
2176 // We save the untagged value on the side and store a GC-safe
2177 // temporary placeholder in the frame.
karlklose@chromium.org44bc7082011-04-11 12:33:05 +00002178 AddDoubleValue(output_[frame_index]->GetTop() + output_offset, value);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002179 output_[frame_index]->SetFrameSlot(output_offset, kPlaceholder);
2180 return;
2181 }
2182
2183 case Translation::STACK_SLOT: {
2184 int input_slot_index = iterator->Next();
dslomov@chromium.orgb752d402013-06-18 11:54:54 +00002185 unsigned input_offset = input_->GetOffsetFromSlotIndex(input_slot_index);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002186 intptr_t input_value = input_->GetFrameSlot(input_offset);
mmassi@chromium.org2f0efde2013-02-06 14:12:58 +00002187 if (trace_) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002188 PrintF(" 0x%08" V8PRIxPTR ": ",
2189 output_[frame_index]->GetTop() + output_offset);
mmassi@chromium.org7028c052012-06-13 11:51:58 +00002190 PrintF("[top + %d] <- 0x%08" V8PRIxPTR " ; [sp + %d] ",
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002191 output_offset,
2192 input_value,
2193 input_offset);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002194 reinterpret_cast<Object*>(input_value)->ShortPrint();
2195 PrintF("\n");
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002196 }
2197 output_[frame_index]->SetFrameSlot(output_offset, input_value);
2198 return;
2199 }
2200
2201 case Translation::INT32_STACK_SLOT: {
2202 int input_slot_index = iterator->Next();
dslomov@chromium.orgb752d402013-06-18 11:54:54 +00002203 unsigned input_offset = input_->GetOffsetFromSlotIndex(input_slot_index);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002204 intptr_t value = input_->GetFrameSlot(input_offset);
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +00002205 bool is_smi = (value_type == TRANSLATED_VALUE_IS_TAGGED) &&
2206 Smi::IsValid(value);
mmassi@chromium.org2f0efde2013-02-06 14:12:58 +00002207 if (trace_) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002208 PrintF(" 0x%08" V8PRIxPTR ": ",
2209 output_[frame_index]->GetTop() + output_offset);
mmassi@chromium.org7028c052012-06-13 11:51:58 +00002210 PrintF("[top + %d] <- %" V8PRIdPTR " ; [sp + %d] (%s)\n",
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002211 output_offset,
2212 value,
2213 input_offset,
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +00002214 TraceValueType(is_smi, is_native));
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002215 }
2216 if (is_smi) {
2217 intptr_t tagged_value =
2218 reinterpret_cast<intptr_t>(Smi::FromInt(static_cast<int>(value)));
2219 output_[frame_index]->SetFrameSlot(output_offset, tagged_value);
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +00002220 } else if (value_type == TRANSLATED_VALUE_IS_NATIVE) {
2221 output_[frame_index]->SetFrameSlot(output_offset, value);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002222 } else {
2223 // We save the untagged value on the side and store a GC-safe
2224 // temporary placeholder in the frame.
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +00002225 ASSERT(value_type == TRANSLATED_VALUE_IS_TAGGED);
karlklose@chromium.org44bc7082011-04-11 12:33:05 +00002226 AddDoubleValue(output_[frame_index]->GetTop() + output_offset,
2227 static_cast<double>(static_cast<int32_t>(value)));
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002228 output_[frame_index]->SetFrameSlot(output_offset, kPlaceholder);
2229 }
2230 return;
2231 }
2232
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00002233 case Translation::UINT32_STACK_SLOT: {
2234 int input_slot_index = iterator->Next();
dslomov@chromium.orgb752d402013-06-18 11:54:54 +00002235 unsigned input_offset = input_->GetOffsetFromSlotIndex(input_slot_index);
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00002236 uintptr_t value =
2237 static_cast<uintptr_t>(input_->GetFrameSlot(input_offset));
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +00002238 bool is_smi = (value_type == TRANSLATED_VALUE_IS_TAGGED) &&
2239 (value <= static_cast<uintptr_t>(Smi::kMaxValue));
mmassi@chromium.org2f0efde2013-02-06 14:12:58 +00002240 if (trace_) {
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00002241 PrintF(" 0x%08" V8PRIxPTR ": ",
2242 output_[frame_index]->GetTop() + output_offset);
2243 PrintF("[top + %d] <- %" V8PRIuPTR " ; [sp + %d] (uint32 %s)\n",
2244 output_offset,
2245 value,
2246 input_offset,
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +00002247 TraceValueType(is_smi, is_native));
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00002248 }
2249 if (is_smi) {
2250 intptr_t tagged_value =
2251 reinterpret_cast<intptr_t>(Smi::FromInt(static_cast<int>(value)));
2252 output_[frame_index]->SetFrameSlot(output_offset, tagged_value);
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +00002253 } else if (value_type == TRANSLATED_VALUE_IS_NATIVE) {
2254 output_[frame_index]->SetFrameSlot(output_offset, value);
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00002255 } else {
2256 // We save the untagged value on the side and store a GC-safe
2257 // temporary placeholder in the frame.
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +00002258 ASSERT(value_type == TRANSLATED_VALUE_IS_TAGGED);
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00002259 AddDoubleValue(output_[frame_index]->GetTop() + output_offset,
2260 static_cast<double>(static_cast<uint32_t>(value)));
2261 output_[frame_index]->SetFrameSlot(output_offset, kPlaceholder);
2262 }
2263 return;
2264 }
2265
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002266 case Translation::DOUBLE_STACK_SLOT: {
2267 int input_slot_index = iterator->Next();
dslomov@chromium.orgb752d402013-06-18 11:54:54 +00002268 unsigned input_offset = input_->GetOffsetFromSlotIndex(input_slot_index);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002269 double value = input_->GetDoubleFrameSlot(input_offset);
mmassi@chromium.org2f0efde2013-02-06 14:12:58 +00002270 if (trace_) {
mmassi@chromium.org7028c052012-06-13 11:51:58 +00002271 PrintF(" 0x%08" V8PRIxPTR ": [top + %d] <- %e ; [sp + %d]\n",
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002272 output_[frame_index]->GetTop() + output_offset,
2273 output_offset,
2274 value,
2275 input_offset);
2276 }
2277 // We save the untagged value on the side and store a GC-safe
2278 // temporary placeholder in the frame.
karlklose@chromium.org44bc7082011-04-11 12:33:05 +00002279 AddDoubleValue(output_[frame_index]->GetTop() + output_offset, value);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002280 output_[frame_index]->SetFrameSlot(output_offset, kPlaceholder);
2281 return;
2282 }
2283
2284 case Translation::LITERAL: {
2285 Object* literal = ComputeLiteral(iterator->Next());
mmassi@chromium.org2f0efde2013-02-06 14:12:58 +00002286 if (trace_) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002287 PrintF(" 0x%08" V8PRIxPTR ": [top + %d] <- ",
2288 output_[frame_index]->GetTop() + output_offset,
2289 output_offset);
2290 literal->ShortPrint();
2291 PrintF(" ; literal\n");
2292 }
2293 intptr_t value = reinterpret_cast<intptr_t>(literal);
2294 output_[frame_index]->SetFrameSlot(output_offset, value);
2295 return;
2296 }
2297
danno@chromium.org59400602013-08-13 17:09:37 +00002298 case Translation::DUPLICATED_OBJECT: {
2299 int object_index = iterator->Next();
mmassi@chromium.org2f0efde2013-02-06 14:12:58 +00002300 if (trace_) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002301 PrintF(" 0x%08" V8PRIxPTR ": [top + %d] <- ",
2302 output_[frame_index]->GetTop() + output_offset,
2303 output_offset);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002304 isolate_->heap()->arguments_marker()->ShortPrint();
danno@chromium.org59400602013-08-13 17:09:37 +00002305 PrintF(" ; duplicate of object #%d\n", object_index);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002306 }
danno@chromium.org59400602013-08-13 17:09:37 +00002307 // Use the materialization marker value as a sentinel and fill in
2308 // the object after the deoptimized frame is built.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002309 intptr_t value = reinterpret_cast<intptr_t>(
2310 isolate_->heap()->arguments_marker());
danno@chromium.org59400602013-08-13 17:09:37 +00002311 AddObjectDuplication(output_[frame_index]->GetTop() + output_offset,
2312 object_index);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002313 output_[frame_index]->SetFrameSlot(output_offset, value);
danno@chromium.org59400602013-08-13 17:09:37 +00002314 return;
2315 }
2316
2317 case Translation::ARGUMENTS_OBJECT:
2318 case Translation::CAPTURED_OBJECT: {
2319 int length = iterator->Next();
2320 bool is_args = opcode == Translation::ARGUMENTS_OBJECT;
2321 if (trace_) {
2322 PrintF(" 0x%08" V8PRIxPTR ": [top + %d] <- ",
2323 output_[frame_index]->GetTop() + output_offset,
2324 output_offset);
2325 isolate_->heap()->arguments_marker()->ShortPrint();
2326 PrintF(" ; object (length = %d, is_args = %d)\n", length, is_args);
2327 }
2328 // Use the materialization marker value as a sentinel and fill in
2329 // the object after the deoptimized frame is built.
2330 intptr_t value = reinterpret_cast<intptr_t>(
2331 isolate_->heap()->arguments_marker());
2332 AddObjectStart(output_[frame_index]->GetTop() + output_offset,
2333 length, is_args);
2334 output_[frame_index]->SetFrameSlot(output_offset, value);
2335 // We save the object values on the side and materialize the actual
2336 // object after the deoptimized frame is built.
2337 int object_index = deferred_objects_.length() - 1;
dslomov@chromium.orgb752d402013-06-18 11:54:54 +00002338 for (int i = 0; i < length; i++) {
danno@chromium.org59400602013-08-13 17:09:37 +00002339 DoTranslateObject(iterator, object_index, i);
ulan@chromium.org56c14af2012-09-20 12:51:09 +00002340 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002341 return;
2342 }
2343 }
2344}
2345
2346
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002347unsigned Deoptimizer::ComputeInputFrameSize() const {
2348 unsigned fixed_size = ComputeFixedSize(function_);
2349 // The fp-to-sp delta already takes the context and the function
2350 // into account so we have to avoid double counting them (-2).
2351 unsigned result = fixed_size + fp_to_sp_delta_ - (2 * kPointerSize);
2352#ifdef DEBUG
hpayer@chromium.orgc5d49712013-09-11 08:25:48 +00002353 if (compiled_code_->kind() == Code::OPTIMIZED_FUNCTION) {
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00002354 unsigned stack_slots = compiled_code_->stack_slots();
mmassi@chromium.org2f0efde2013-02-06 14:12:58 +00002355 unsigned outgoing_size = ComputeOutgoingArgumentSize();
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002356 ASSERT(result == fixed_size + (stack_slots * kPointerSize) + outgoing_size);
2357 }
2358#endif
2359 return result;
2360}
2361
2362
2363unsigned Deoptimizer::ComputeFixedSize(JSFunction* function) const {
2364 // The fixed part of the frame consists of the return address, frame
2365 // pointer, function, context, and all the incoming arguments.
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002366 return ComputeIncomingArgumentSize(function) +
2367 StandardFrameConstants::kFixedFrameSize;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002368}
2369
2370
2371unsigned Deoptimizer::ComputeIncomingArgumentSize(JSFunction* function) const {
2372 // The incoming arguments is the values for formal parameters and
2373 // the receiver. Every slot contains a pointer.
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00002374 if (function->IsSmi()) {
2375 ASSERT(Smi::cast(function) == Smi::FromInt(StackFrame::STUB));
2376 return 0;
2377 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002378 unsigned arguments = function->shared()->formal_parameter_count() + 1;
2379 return arguments * kPointerSize;
2380}
2381
2382
2383unsigned Deoptimizer::ComputeOutgoingArgumentSize() const {
2384 DeoptimizationInputData* data = DeoptimizationInputData::cast(
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00002385 compiled_code_->deoptimization_data());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002386 unsigned height = data->ArgumentsStackHeight(bailout_id_)->value();
2387 return height * kPointerSize;
2388}
2389
2390
2391Object* Deoptimizer::ComputeLiteral(int index) const {
2392 DeoptimizationInputData* data = DeoptimizationInputData::cast(
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00002393 compiled_code_->deoptimization_data());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002394 FixedArray* literals = data->LiteralArray();
2395 return literals->get(index);
2396}
2397
2398
danno@chromium.org59400602013-08-13 17:09:37 +00002399void Deoptimizer::AddObjectStart(intptr_t slot, int length, bool is_args) {
dslomov@chromium.orgb752d402013-06-18 11:54:54 +00002400 ObjectMaterializationDescriptor object_desc(
danno@chromium.org59400602013-08-13 17:09:37 +00002401 reinterpret_cast<Address>(slot), jsframe_count_, length, -1, is_args);
2402 deferred_objects_.Add(object_desc);
2403}
2404
2405
2406void Deoptimizer::AddObjectDuplication(intptr_t slot, int object_index) {
2407 ObjectMaterializationDescriptor object_desc(
2408 reinterpret_cast<Address>(slot), jsframe_count_, -1, object_index, false);
dslomov@chromium.orgb752d402013-06-18 11:54:54 +00002409 deferred_objects_.Add(object_desc);
ulan@chromium.org56c14af2012-09-20 12:51:09 +00002410}
2411
2412
dslomov@chromium.orgb752d402013-06-18 11:54:54 +00002413void Deoptimizer::AddObjectTaggedValue(intptr_t value) {
2414 deferred_objects_tagged_values_.Add(reinterpret_cast<Object*>(value));
dslomov@chromium.orgb752d402013-06-18 11:54:54 +00002415}
2416
2417
2418void Deoptimizer::AddObjectDoubleValue(double value) {
2419 deferred_objects_tagged_values_.Add(isolate()->heap()->the_hole_value());
mstarzinger@chromium.orgb4968be2013-10-16 09:00:56 +00002420 HeapNumberMaterializationDescriptor<int> value_desc(
2421 deferred_objects_tagged_values_.length() - 1, value);
2422 deferred_objects_double_values_.Add(value_desc);
ulan@chromium.org56c14af2012-09-20 12:51:09 +00002423}
2424
2425
2426void Deoptimizer::AddDoubleValue(intptr_t slot_address, double value) {
mstarzinger@chromium.orgb4968be2013-10-16 09:00:56 +00002427 HeapNumberMaterializationDescriptor<Address> value_desc(
karlklose@chromium.org44bc7082011-04-11 12:33:05 +00002428 reinterpret_cast<Address>(slot_address), value);
2429 deferred_heap_numbers_.Add(value_desc);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002430}
2431
2432
hpayer@chromium.org8432c912013-02-28 15:55:26 +00002433void Deoptimizer::EnsureCodeForDeoptimizationEntry(Isolate* isolate,
2434 BailoutType type,
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +00002435 int max_entry_id) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002436 // We cannot run this if the serializer is enabled because this will
2437 // cause us to emit relocation information for the external
2438 // references. This is fine because the deoptimizer's code section
2439 // isn't meant to be serialized at all.
danno@chromium.orgaefd6072013-05-14 14:11:47 +00002440 ASSERT(type == EAGER || type == SOFT || type == LAZY);
hpayer@chromium.org8432c912013-02-28 15:55:26 +00002441 DeoptimizerData* data = isolate->deoptimizer_data();
danno@chromium.orgaefd6072013-05-14 14:11:47 +00002442 int entry_count = data->deopt_entry_code_entries_[type];
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +00002443 if (max_entry_id < entry_count) return;
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00002444 entry_count = Max(entry_count, Deoptimizer::kMinNumberOfEntries);
2445 while (max_entry_id >= entry_count) entry_count *= 2;
2446 ASSERT(entry_count <= Deoptimizer::kMaxNumberOfEntries);
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +00002447
hpayer@chromium.org8432c912013-02-28 15:55:26 +00002448 MacroAssembler masm(isolate, NULL, 16 * KB);
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +00002449 masm.set_emit_debug_code(false);
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +00002450 GenerateDeoptimizationEntries(&masm, entry_count, type);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002451 CodeDesc desc;
2452 masm.GetCode(&desc);
ulan@chromium.org2e04b582013-02-21 14:06:02 +00002453 ASSERT(!RelocInfo::RequiresRelocation(desc));
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002454
danno@chromium.orgaefd6072013-05-14 14:11:47 +00002455 MemoryChunk* chunk = data->deopt_entry_code_[type];
mstarzinger@chromium.org068ea0a2013-01-30 09:39:44 +00002456 ASSERT(static_cast<int>(Deoptimizer::GetMaxDeoptTableSize()) >=
2457 desc.instr_size);
2458 chunk->CommitArea(desc.instr_size);
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +00002459 CopyBytes(chunk->area_start(), desc.buffer,
2460 static_cast<size_t>(desc.instr_size));
mstarzinger@chromium.org068ea0a2013-01-30 09:39:44 +00002461 CPU::FlushICache(chunk->area_start(), desc.instr_size);
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +00002462
danno@chromium.orgaefd6072013-05-14 14:11:47 +00002463 data->deopt_entry_code_entries_[type] = entry_count;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002464}
2465
2466
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002467FrameDescription::FrameDescription(uint32_t frame_size,
2468 JSFunction* function)
2469 : frame_size_(frame_size),
2470 function_(function),
2471 top_(kZapUint32),
2472 pc_(kZapUint32),
ulan@chromium.org967e2702012-02-28 09:49:15 +00002473 fp_(kZapUint32),
2474 context_(kZapUint32) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002475 // Zap all the registers.
2476 for (int r = 0; r < Register::kNumRegisters; r++) {
2477 SetRegister(r, kZapUint32);
2478 }
2479
2480 // Zap all the slots.
2481 for (unsigned o = 0; o < frame_size; o += kPointerSize) {
2482 SetFrameSlot(o, kZapUint32);
2483 }
2484}
2485
2486
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002487int FrameDescription::ComputeFixedSize() {
2488 return StandardFrameConstants::kFixedFrameSize +
2489 (ComputeParametersCount() + 1) * kPointerSize;
2490}
2491
2492
2493unsigned FrameDescription::GetOffsetFromSlotIndex(int slot_index) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002494 if (slot_index >= 0) {
2495 // Local or spill slots. Skip the fixed part of the frame
2496 // including all arguments.
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002497 unsigned base = GetFrameSize() - ComputeFixedSize();
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002498 return base - ((slot_index + 1) * kPointerSize);
2499 } else {
2500 // Incoming parameter.
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002501 int arg_size = (ComputeParametersCount() + 1) * kPointerSize;
2502 unsigned base = GetFrameSize() - arg_size;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002503 return base - ((slot_index + 1) * kPointerSize);
2504 }
2505}
2506
2507
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00002508int FrameDescription::ComputeParametersCount() {
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002509 switch (type_) {
2510 case StackFrame::JAVA_SCRIPT:
2511 return function_->shared()->formal_parameter_count();
2512 case StackFrame::ARGUMENTS_ADAPTOR: {
2513 // Last slot contains number of incomming arguments as a smi.
2514 // Can't use GetExpression(0) because it would cause infinite recursion.
2515 return reinterpret_cast<Smi*>(*GetFrameSlotPointer(0))->value();
2516 }
mstarzinger@chromium.org068ea0a2013-01-30 09:39:44 +00002517 case StackFrame::STUB:
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00002518 return -1; // Minus receiver.
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002519 default:
2520 UNREACHABLE();
2521 return 0;
2522 }
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00002523}
2524
2525
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002526Object* FrameDescription::GetParameter(int index) {
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00002527 ASSERT(index >= 0);
2528 ASSERT(index < ComputeParametersCount());
2529 // The slot indexes for incoming arguments are negative.
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002530 unsigned offset = GetOffsetFromSlotIndex(index - ComputeParametersCount());
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00002531 return reinterpret_cast<Object*>(*GetFrameSlotPointer(offset));
2532}
2533
2534
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002535unsigned FrameDescription::GetExpressionCount() {
2536 ASSERT_EQ(StackFrame::JAVA_SCRIPT, type_);
2537 unsigned size = GetFrameSize() - ComputeFixedSize();
ricow@chromium.org4f693d62011-07-04 14:01:31 +00002538 return size / kPointerSize;
2539}
2540
2541
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002542Object* FrameDescription::GetExpression(int index) {
2543 ASSERT_EQ(StackFrame::JAVA_SCRIPT, type_);
2544 unsigned offset = GetOffsetFromSlotIndex(index);
ricow@chromium.org4f693d62011-07-04 14:01:31 +00002545 return reinterpret_cast<Object*>(*GetFrameSlotPointer(offset));
2546}
2547
2548
rossberg@chromium.org400388e2012-06-06 09:29:22 +00002549void TranslationBuffer::Add(int32_t value, Zone* zone) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002550 // Encode the sign bit in the least significant bit.
2551 bool is_negative = (value < 0);
2552 uint32_t bits = ((is_negative ? -value : value) << 1) |
2553 static_cast<int32_t>(is_negative);
2554 // Encode the individual bytes using the least significant bit of
2555 // each byte to indicate whether or not more bytes follow.
2556 do {
2557 uint32_t next = bits >> 7;
rossberg@chromium.org400388e2012-06-06 09:29:22 +00002558 contents_.Add(((bits << 1) & 0xFF) | (next != 0), zone);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002559 bits = next;
2560 } while (bits != 0);
2561}
2562
2563
2564int32_t TranslationIterator::Next() {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002565 // Run through the bytes until we reach one with a least significant
2566 // bit of zero (marks the end).
2567 uint32_t bits = 0;
2568 for (int i = 0; true; i += 7) {
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +00002569 ASSERT(HasNext());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002570 uint8_t next = buffer_->get(index_++);
2571 bits |= (next >> 1) << i;
2572 if ((next & 1) == 0) break;
2573 }
2574 // The bits encode the sign in the least significant bit.
2575 bool is_negative = (bits & 1) == 1;
2576 int32_t result = bits >> 1;
2577 return is_negative ? -result : result;
2578}
2579
2580
svenpanne@chromium.org876cca82013-03-18 14:43:20 +00002581Handle<ByteArray> TranslationBuffer::CreateByteArray(Factory* factory) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002582 int length = contents_.length();
svenpanne@chromium.org876cca82013-03-18 14:43:20 +00002583 Handle<ByteArray> result = factory->NewByteArray(length, TENURED);
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00002584 OS::MemCopy(
2585 result->GetDataStartAddress(), contents_.ToVector().start(), length);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002586 return result;
2587}
2588
2589
ulan@chromium.org967e2702012-02-28 09:49:15 +00002590void Translation::BeginConstructStubFrame(int literal_id, unsigned height) {
rossberg@chromium.org400388e2012-06-06 09:29:22 +00002591 buffer_->Add(CONSTRUCT_STUB_FRAME, zone());
2592 buffer_->Add(literal_id, zone());
2593 buffer_->Add(height, zone());
ulan@chromium.org967e2702012-02-28 09:49:15 +00002594}
2595
2596
mstarzinger@chromium.orgde886792012-09-11 13:22:37 +00002597void Translation::BeginGetterStubFrame(int literal_id) {
2598 buffer_->Add(GETTER_STUB_FRAME, zone());
2599 buffer_->Add(literal_id, zone());
2600}
2601
2602
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00002603void Translation::BeginSetterStubFrame(int literal_id) {
2604 buffer_->Add(SETTER_STUB_FRAME, zone());
2605 buffer_->Add(literal_id, zone());
2606}
2607
2608
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002609void Translation::BeginArgumentsAdaptorFrame(int literal_id, unsigned height) {
rossberg@chromium.org400388e2012-06-06 09:29:22 +00002610 buffer_->Add(ARGUMENTS_ADAPTOR_FRAME, zone());
2611 buffer_->Add(literal_id, zone());
2612 buffer_->Add(height, zone());
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002613}
2614
2615
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +00002616void Translation::BeginJSFrame(BailoutId node_id,
2617 int literal_id,
2618 unsigned height) {
rossberg@chromium.org400388e2012-06-06 09:29:22 +00002619 buffer_->Add(JS_FRAME, zone());
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +00002620 buffer_->Add(node_id.ToInt(), zone());
rossberg@chromium.org400388e2012-06-06 09:29:22 +00002621 buffer_->Add(literal_id, zone());
2622 buffer_->Add(height, zone());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002623}
2624
2625
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00002626void Translation::BeginCompiledStubFrame() {
2627 buffer_->Add(COMPILED_STUB_FRAME, zone());
2628}
2629
2630
dslomov@chromium.orgb752d402013-06-18 11:54:54 +00002631void Translation::BeginArgumentsObject(int args_length) {
2632 buffer_->Add(ARGUMENTS_OBJECT, zone());
2633 buffer_->Add(args_length, zone());
2634}
2635
2636
danno@chromium.org59400602013-08-13 17:09:37 +00002637void Translation::BeginCapturedObject(int length) {
2638 buffer_->Add(CAPTURED_OBJECT, zone());
2639 buffer_->Add(length, zone());
2640}
2641
2642
2643void Translation::DuplicateObject(int object_index) {
2644 buffer_->Add(DUPLICATED_OBJECT, zone());
2645 buffer_->Add(object_index, zone());
2646}
2647
2648
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002649void Translation::StoreRegister(Register reg) {
rossberg@chromium.org400388e2012-06-06 09:29:22 +00002650 buffer_->Add(REGISTER, zone());
2651 buffer_->Add(reg.code(), zone());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002652}
2653
2654
2655void Translation::StoreInt32Register(Register reg) {
rossberg@chromium.org400388e2012-06-06 09:29:22 +00002656 buffer_->Add(INT32_REGISTER, zone());
2657 buffer_->Add(reg.code(), zone());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002658}
2659
2660
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00002661void Translation::StoreUint32Register(Register reg) {
2662 buffer_->Add(UINT32_REGISTER, zone());
2663 buffer_->Add(reg.code(), zone());
2664}
2665
2666
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002667void Translation::StoreDoubleRegister(DoubleRegister reg) {
rossberg@chromium.org400388e2012-06-06 09:29:22 +00002668 buffer_->Add(DOUBLE_REGISTER, zone());
2669 buffer_->Add(DoubleRegister::ToAllocationIndex(reg), zone());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002670}
2671
2672
2673void Translation::StoreStackSlot(int index) {
rossberg@chromium.org400388e2012-06-06 09:29:22 +00002674 buffer_->Add(STACK_SLOT, zone());
2675 buffer_->Add(index, zone());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002676}
2677
2678
2679void Translation::StoreInt32StackSlot(int index) {
rossberg@chromium.org400388e2012-06-06 09:29:22 +00002680 buffer_->Add(INT32_STACK_SLOT, zone());
2681 buffer_->Add(index, zone());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002682}
2683
2684
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00002685void Translation::StoreUint32StackSlot(int index) {
2686 buffer_->Add(UINT32_STACK_SLOT, zone());
2687 buffer_->Add(index, zone());
2688}
2689
2690
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002691void Translation::StoreDoubleStackSlot(int index) {
rossberg@chromium.org400388e2012-06-06 09:29:22 +00002692 buffer_->Add(DOUBLE_STACK_SLOT, zone());
2693 buffer_->Add(index, zone());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002694}
2695
2696
2697void Translation::StoreLiteral(int literal_id) {
rossberg@chromium.org400388e2012-06-06 09:29:22 +00002698 buffer_->Add(LITERAL, zone());
2699 buffer_->Add(literal_id, zone());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002700}
2701
2702
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +00002703void Translation::StoreArgumentsObject(bool args_known,
2704 int args_index,
2705 int args_length) {
2706 buffer_->Add(ARGUMENTS_OBJECT, zone());
2707 buffer_->Add(args_known, zone());
2708 buffer_->Add(args_index, zone());
2709 buffer_->Add(args_length, zone());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002710}
2711
2712
2713int Translation::NumberOfOperandsFor(Opcode opcode) {
2714 switch (opcode) {
mstarzinger@chromium.orgde886792012-09-11 13:22:37 +00002715 case GETTER_STUB_FRAME:
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00002716 case SETTER_STUB_FRAME:
danno@chromium.org59400602013-08-13 17:09:37 +00002717 case DUPLICATED_OBJECT:
dslomov@chromium.orgb752d402013-06-18 11:54:54 +00002718 case ARGUMENTS_OBJECT:
danno@chromium.org59400602013-08-13 17:09:37 +00002719 case CAPTURED_OBJECT:
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002720 case REGISTER:
2721 case INT32_REGISTER:
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00002722 case UINT32_REGISTER:
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002723 case DOUBLE_REGISTER:
2724 case STACK_SLOT:
2725 case INT32_STACK_SLOT:
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00002726 case UINT32_STACK_SLOT:
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002727 case DOUBLE_STACK_SLOT:
2728 case LITERAL:
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00002729 case COMPILED_STUB_FRAME:
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002730 return 1;
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002731 case BEGIN:
2732 case ARGUMENTS_ADAPTOR_FRAME:
ulan@chromium.org967e2702012-02-28 09:49:15 +00002733 case CONSTRUCT_STUB_FRAME:
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002734 return 2;
2735 case JS_FRAME:
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002736 return 3;
2737 }
2738 UNREACHABLE();
2739 return -1;
2740}
2741
2742
whesse@chromium.org7b260152011-06-20 15:33:18 +00002743#if defined(OBJECT_PRINT) || defined(ENABLE_DISASSEMBLER)
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002744
2745const char* Translation::StringFor(Opcode opcode) {
jkummerow@chromium.orgfb7a7c42013-10-02 11:41:02 +00002746#define TRANSLATION_OPCODE_CASE(item) case item: return #item;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002747 switch (opcode) {
jkummerow@chromium.orgfb7a7c42013-10-02 11:41:02 +00002748 TRANSLATION_OPCODE_LIST(TRANSLATION_OPCODE_CASE)
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002749 }
jkummerow@chromium.orgfb7a7c42013-10-02 11:41:02 +00002750#undef TRANSLATION_OPCODE_CASE
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002751 UNREACHABLE();
2752 return "";
2753}
2754
2755#endif
2756
2757
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00002758// We can't intermix stack decoding and allocations because
2759// deoptimization infrastracture is not GC safe.
2760// Thus we build a temporary structure in malloced space.
2761SlotRef SlotRef::ComputeSlotForNextArgument(TranslationIterator* iterator,
2762 DeoptimizationInputData* data,
2763 JavaScriptFrame* frame) {
2764 Translation::Opcode opcode =
2765 static_cast<Translation::Opcode>(iterator->Next());
2766
2767 switch (opcode) {
2768 case Translation::BEGIN:
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002769 case Translation::JS_FRAME:
2770 case Translation::ARGUMENTS_ADAPTOR_FRAME:
ulan@chromium.org967e2702012-02-28 09:49:15 +00002771 case Translation::CONSTRUCT_STUB_FRAME:
mstarzinger@chromium.orgde886792012-09-11 13:22:37 +00002772 case Translation::GETTER_STUB_FRAME:
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00002773 case Translation::SETTER_STUB_FRAME:
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00002774 // Peeled off before getting here.
2775 break;
2776
danno@chromium.org59400602013-08-13 17:09:37 +00002777 case Translation::DUPLICATED_OBJECT:
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00002778 case Translation::ARGUMENTS_OBJECT:
danno@chromium.org59400602013-08-13 17:09:37 +00002779 case Translation::CAPTURED_OBJECT:
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00002780 // This can be only emitted for local slots not for argument slots.
2781 break;
2782
2783 case Translation::REGISTER:
2784 case Translation::INT32_REGISTER:
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00002785 case Translation::UINT32_REGISTER:
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00002786 case Translation::DOUBLE_REGISTER:
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00002787 // We are at safepoint which corresponds to call. All registers are
2788 // saved by caller so there would be no live registers at this
2789 // point. Thus these translation commands should not be used.
2790 break;
2791
2792 case Translation::STACK_SLOT: {
2793 int slot_index = iterator->Next();
2794 Address slot_addr = SlotAddress(frame, slot_index);
2795 return SlotRef(slot_addr, SlotRef::TAGGED);
2796 }
2797
2798 case Translation::INT32_STACK_SLOT: {
2799 int slot_index = iterator->Next();
2800 Address slot_addr = SlotAddress(frame, slot_index);
2801 return SlotRef(slot_addr, SlotRef::INT32);
2802 }
2803
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00002804 case Translation::UINT32_STACK_SLOT: {
2805 int slot_index = iterator->Next();
2806 Address slot_addr = SlotAddress(frame, slot_index);
2807 return SlotRef(slot_addr, SlotRef::UINT32);
2808 }
2809
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00002810 case Translation::DOUBLE_STACK_SLOT: {
2811 int slot_index = iterator->Next();
2812 Address slot_addr = SlotAddress(frame, slot_index);
2813 return SlotRef(slot_addr, SlotRef::DOUBLE);
2814 }
2815
2816 case Translation::LITERAL: {
2817 int literal_index = iterator->Next();
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00002818 return SlotRef(data->GetIsolate(),
2819 data->LiteralArray()->get(literal_index));
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00002820 }
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00002821
2822 case Translation::COMPILED_STUB_FRAME:
2823 UNREACHABLE();
2824 break;
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00002825 }
2826
2827 UNREACHABLE();
2828 return SlotRef();
2829}
2830
2831
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002832void SlotRef::ComputeSlotsForArguments(Vector<SlotRef>* args_slots,
2833 TranslationIterator* it,
2834 DeoptimizationInputData* data,
2835 JavaScriptFrame* frame) {
2836 // Process the translation commands for the arguments.
2837
2838 // Skip the translation command for the receiver.
2839 it->Skip(Translation::NumberOfOperandsFor(
2840 static_cast<Translation::Opcode>(it->Next())));
2841
2842 // Compute slots for arguments.
2843 for (int i = 0; i < args_slots->length(); ++i) {
2844 (*args_slots)[i] = ComputeSlotForNextArgument(it, data, frame);
2845 }
2846}
2847
2848
2849Vector<SlotRef> SlotRef::ComputeSlotMappingForArguments(
2850 JavaScriptFrame* frame,
2851 int inlined_jsframe_index,
2852 int formal_parameter_count) {
rossberg@chromium.org79e79022013-06-03 15:43:46 +00002853 DisallowHeapAllocation no_gc;
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +00002854 int deopt_index = Safepoint::kNoDeoptimizationIndex;
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00002855 DeoptimizationInputData* data =
2856 static_cast<OptimizedFrame*>(frame)->GetDeoptimizationData(&deopt_index);
2857 TranslationIterator it(data->TranslationByteArray(),
2858 data->TranslationIndex(deopt_index)->value());
2859 Translation::Opcode opcode = static_cast<Translation::Opcode>(it.Next());
2860 ASSERT(opcode == Translation::BEGIN);
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002861 it.Next(); // Drop frame count.
2862 int jsframe_count = it.Next();
2863 USE(jsframe_count);
2864 ASSERT(jsframe_count > inlined_jsframe_index);
2865 int jsframes_to_skip = inlined_jsframe_index;
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00002866 while (true) {
2867 opcode = static_cast<Translation::Opcode>(it.Next());
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002868 if (opcode == Translation::ARGUMENTS_ADAPTOR_FRAME) {
2869 if (jsframes_to_skip == 0) {
2870 ASSERT(Translation::NumberOfOperandsFor(opcode) == 2);
2871
2872 it.Skip(1); // literal id
2873 int height = it.Next();
2874
2875 // We reached the arguments adaptor frame corresponding to the
2876 // inlined function in question. Number of arguments is height - 1.
2877 Vector<SlotRef> args_slots =
2878 Vector<SlotRef>::New(height - 1); // Minus receiver.
2879 ComputeSlotsForArguments(&args_slots, &it, data, frame);
2880 return args_slots;
2881 }
2882 } else if (opcode == Translation::JS_FRAME) {
2883 if (jsframes_to_skip == 0) {
2884 // Skip over operands to advance to the next opcode.
2885 it.Skip(Translation::NumberOfOperandsFor(opcode));
2886
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00002887 // We reached the frame corresponding to the inlined function
2888 // in question. Process the translation commands for the
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002889 // arguments. Number of arguments is equal to the number of
2890 // format parameter count.
2891 Vector<SlotRef> args_slots =
2892 Vector<SlotRef>::New(formal_parameter_count);
2893 ComputeSlotsForArguments(&args_slots, &it, data, frame);
2894 return args_slots;
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00002895 }
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002896 jsframes_to_skip--;
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00002897 }
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002898
2899 // Skip over operands to advance to the next opcode.
2900 it.Skip(Translation::NumberOfOperandsFor(opcode));
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00002901 }
2902
2903 UNREACHABLE();
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002904 return Vector<SlotRef>();
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00002905}
2906
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +00002907#ifdef ENABLE_DEBUGGER_SUPPORT
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00002908
ulan@chromium.org967e2702012-02-28 09:49:15 +00002909DeoptimizedFrameInfo::DeoptimizedFrameInfo(Deoptimizer* deoptimizer,
2910 int frame_index,
2911 bool has_arguments_adaptor,
2912 bool has_construct_stub) {
ricow@chromium.org4f693d62011-07-04 14:01:31 +00002913 FrameDescription* output_frame = deoptimizer->output_[frame_index];
ulan@chromium.org967e2702012-02-28 09:49:15 +00002914 function_ = output_frame->GetFunction();
2915 has_construct_stub_ = has_construct_stub;
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002916 expression_count_ = output_frame->GetExpressionCount();
2917 expression_stack_ = new Object*[expression_count_];
danno@chromium.orgfa458e42012-02-01 10:48:36 +00002918 // Get the source position using the unoptimized code.
2919 Address pc = reinterpret_cast<Address>(output_frame->GetPc());
jkummerow@chromium.orgba72ec82013-07-22 09:21:20 +00002920 Code* code = Code::cast(deoptimizer->isolate()->FindCodeObject(pc));
danno@chromium.orgfa458e42012-02-01 10:48:36 +00002921 source_position_ = code->SourcePosition(pc);
2922
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002923 for (int i = 0; i < expression_count_; i++) {
2924 SetExpression(i, output_frame->GetExpression(i));
2925 }
2926
2927 if (has_arguments_adaptor) {
2928 output_frame = deoptimizer->output_[frame_index - 1];
2929 ASSERT(output_frame->GetFrameType() == StackFrame::ARGUMENTS_ADAPTOR);
2930 }
2931
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00002932 parameters_count_ = output_frame->ComputeParametersCount();
2933 parameters_ = new Object*[parameters_count_];
2934 for (int i = 0; i < parameters_count_; i++) {
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002935 SetParameter(i, output_frame->GetParameter(i));
ricow@chromium.org4f693d62011-07-04 14:01:31 +00002936 }
2937}
2938
2939
2940DeoptimizedFrameInfo::~DeoptimizedFrameInfo() {
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00002941 delete[] expression_stack_;
2942 delete[] parameters_;
ricow@chromium.org4f693d62011-07-04 14:01:31 +00002943}
2944
danno@chromium.orgfa458e42012-02-01 10:48:36 +00002945
ricow@chromium.org4f693d62011-07-04 14:01:31 +00002946void DeoptimizedFrameInfo::Iterate(ObjectVisitor* v) {
rossberg@chromium.org717967f2011-07-20 13:44:42 +00002947 v->VisitPointer(BitCast<Object**>(&function_));
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00002948 v->VisitPointers(parameters_, parameters_ + parameters_count_);
ricow@chromium.org4f693d62011-07-04 14:01:31 +00002949 v->VisitPointers(expression_stack_, expression_stack_ + expression_count_);
2950}
2951
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +00002952#endif // ENABLE_DEBUGGER_SUPPORT
ricow@chromium.org4f693d62011-07-04 14:01:31 +00002953
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002954} } // namespace v8::internal