blob: 51c2e4677866aad648c8d69ceffe42574983a75c [file] [log] [blame]
Steve Block44f0eee2011-05-26 01:26:41 +01001// Copyright 2011 the V8 project authors. All rights reserved.
2// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
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
30#include "codegen.h"
31#include "deoptimizer.h"
32#include "full-codegen.h"
33#include "safepoint-table.h"
34
Steve Block44f0eee2011-05-26 01:26:41 +010035namespace v8 {
36namespace internal {
37
38
Steve Block44f0eee2011-05-26 01:26:41 +010039int Deoptimizer::patch_size() {
Ben Murdoch3ef787d2012-04-12 10:51:47 +010040 const int kCallInstructionSizeInWords = 4;
Steve Block44f0eee2011-05-26 01:26:41 +010041 return kCallInstructionSizeInWords * Assembler::kInstrSize;
42}
43
44
45void Deoptimizer::DeoptimizeFunction(JSFunction* function) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +010046 HandleScope scope;
47 AssertNoAllocation no_allocation;
48
49 if (!function->IsOptimized()) return;
50
51 // Get the optimized code.
52 Code* code = function->code();
53 Address code_start_address = code->instruction_start();
54
55 // Invalidate the relocation information, as it will become invalid by the
56 // code patching below, and is not needed any more.
57 code->InvalidateRelocation();
58
59 // For each LLazyBailout instruction insert a call to the corresponding
60 // deoptimization entry.
61 DeoptimizationInputData* deopt_data =
62 DeoptimizationInputData::cast(code->deoptimization_data());
63#ifdef DEBUG
64 Address prev_call_address = NULL;
65#endif
66 for (int i = 0; i < deopt_data->DeoptCount(); i++) {
67 if (deopt_data->Pc(i)->value() == -1) continue;
68 Address call_address = code_start_address + deopt_data->Pc(i)->value();
69 Address deopt_entry = GetDeoptimizationEntry(i, LAZY);
70 int call_size_in_bytes = MacroAssembler::CallSize(deopt_entry,
71 RelocInfo::NONE);
72 int call_size_in_words = call_size_in_bytes / Assembler::kInstrSize;
73 ASSERT(call_size_in_bytes % Assembler::kInstrSize == 0);
74 ASSERT(call_size_in_bytes <= patch_size());
75 CodePatcher patcher(call_address, call_size_in_words);
76 patcher.masm()->Call(deopt_entry, RelocInfo::NONE);
77 ASSERT(prev_call_address == NULL ||
78 call_address >= prev_call_address + patch_size());
79 ASSERT(call_address + patch_size() <= code->instruction_end());
80
81#ifdef DEBUG
82 prev_call_address = call_address;
83#endif
84 }
85
86 Isolate* isolate = code->GetIsolate();
87
88 // Add the deoptimizing code to the list.
89 DeoptimizingCodeListNode* node = new DeoptimizingCodeListNode(code);
90 DeoptimizerData* data = isolate->deoptimizer_data();
91 node->set_next(data->deoptimizing_code_list_);
92 data->deoptimizing_code_list_ = node;
93
94 // We might be in the middle of incremental marking with compaction.
95 // Tell collector to treat this code object in a special way and
96 // ignore all slots that might have been recorded on it.
97 isolate->heap()->mark_compact_collector()->InvalidateCode(code);
98
99 // Set the code for the function to non-optimized version.
100 function->ReplaceCode(function->shared()->code());
101
102 if (FLAG_trace_deopt) {
103 PrintF("[forced deoptimization: ");
104 function->PrintName();
105 PrintF(" / %x]\n", reinterpret_cast<uint32_t>(function));
106#ifdef DEBUG
107 if (FLAG_print_code) {
108 code->PrintLn();
109 }
110#endif
111 }
Steve Block44f0eee2011-05-26 01:26:41 +0100112}
113
114
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100115void Deoptimizer::PatchStackCheckCodeAt(Code* unoptimized_code,
116 Address pc_after,
Steve Block44f0eee2011-05-26 01:26:41 +0100117 Code* check_code,
118 Code* replacement_code) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100119 const int kInstrSize = Assembler::kInstrSize;
120 // This structure comes from FullCodeGenerator::EmitStackCheck.
121 // The call of the stack guard check has the following form:
122 // sltu at, sp, t0 / slt at, a3, zero_reg (in case of count based interrupts)
123 // beq at, zero_reg, ok
124 // lui t9, <stack guard address> upper
125 // ori t9, <stack guard address> lower
126 // jalr t9
127 // nop
128 // ----- pc_after points here
129
130 ASSERT(Assembler::IsBeq(Assembler::instr_at(pc_after - 5 * kInstrSize)));
131
132 // Replace the sltu instruction with load-imm 1 to at, so beq is not taken.
133 CodePatcher patcher(pc_after - 6 * kInstrSize, 1);
134 patcher.masm()->addiu(at, zero_reg, 1);
135
136 // Replace the stack check address in the load-immediate (lui/ori pair)
137 // with the entry address of the replacement code.
138 ASSERT(reinterpret_cast<uint32_t>(
139 Assembler::target_address_at(pc_after - 4 * kInstrSize)) ==
140 reinterpret_cast<uint32_t>(check_code->entry()));
141 Assembler::set_target_address_at(pc_after - 4 * kInstrSize,
142 replacement_code->entry());
143
144 // We patched the code to the following form:
145 // addiu at, zero_reg, 1
146 // beq at, zero_reg, ok ;; Not changed
147 // lui t9, <on-stack replacement address> upper
148 // ori t9, <on-stack replacement address> lower
149 // jalr t9 ;; Not changed
150 // nop ;; Not changed
151 // ----- pc_after points here
152
153 unoptimized_code->GetHeap()->incremental_marking()->RecordCodeTargetPatch(
154 unoptimized_code, pc_after - 4 * kInstrSize, replacement_code);
Steve Block44f0eee2011-05-26 01:26:41 +0100155}
156
157
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100158void Deoptimizer::RevertStackCheckCodeAt(Code* unoptimized_code,
159 Address pc_after,
Steve Block44f0eee2011-05-26 01:26:41 +0100160 Code* check_code,
161 Code* replacement_code) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100162 // Exact opposite of the function above.
163 const int kInstrSize = Assembler::kInstrSize;
164 ASSERT(Assembler::IsAddImmediate(
165 Assembler::instr_at(pc_after - 6 * kInstrSize)));
166 ASSERT(Assembler::IsBeq(Assembler::instr_at(pc_after - 5 * kInstrSize)));
167
168 // Restore the sltu instruction so beq can be taken again.
169 CodePatcher patcher(pc_after - 6 * kInstrSize, 1);
170 if (FLAG_count_based_interrupts) {
171 patcher.masm()->slt(at, a3, zero_reg);
172 } else {
173 patcher.masm()->sltu(at, sp, t0);
174 }
175
176 // Replace the on-stack replacement address in the load-immediate (lui/ori
177 // pair) with the entry address of the normal stack-check code.
178 ASSERT(reinterpret_cast<uint32_t>(
179 Assembler::target_address_at(pc_after - 4 * kInstrSize)) ==
180 reinterpret_cast<uint32_t>(replacement_code->entry()));
181 Assembler::set_target_address_at(pc_after - 4 * kInstrSize,
182 check_code->entry());
183
184 check_code->GetHeap()->incremental_marking()->RecordCodeTargetPatch(
185 unoptimized_code, pc_after - 4 * kInstrSize, check_code);
186}
187
188
189static int LookupBailoutId(DeoptimizationInputData* data, unsigned ast_id) {
190 ByteArray* translations = data->TranslationByteArray();
191 int length = data->DeoptCount();
192 for (int i = 0; i < length; i++) {
193 if (static_cast<unsigned>(data->AstId(i)->value()) == ast_id) {
194 TranslationIterator it(translations, data->TranslationIndex(i)->value());
195 int value = it.Next();
196 ASSERT(Translation::BEGIN == static_cast<Translation::Opcode>(value));
197 // Read the number of frames.
198 value = it.Next();
199 if (value == 1) return i;
200 }
201 }
202 UNREACHABLE();
203 return -1;
Steve Block44f0eee2011-05-26 01:26:41 +0100204}
205
206
207void Deoptimizer::DoComputeOsrOutputFrame() {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100208 DeoptimizationInputData* data = DeoptimizationInputData::cast(
209 optimized_code_->deoptimization_data());
210 unsigned ast_id = data->OsrAstId()->value();
211
212 int bailout_id = LookupBailoutId(data, ast_id);
213 unsigned translation_index = data->TranslationIndex(bailout_id)->value();
214 ByteArray* translations = data->TranslationByteArray();
215
216 TranslationIterator iterator(translations, translation_index);
217 Translation::Opcode opcode =
218 static_cast<Translation::Opcode>(iterator.Next());
219 ASSERT(Translation::BEGIN == opcode);
220 USE(opcode);
221 int count = iterator.Next();
222 iterator.Skip(1); // Drop JS frame count.
223 ASSERT(count == 1);
224 USE(count);
225
226 opcode = static_cast<Translation::Opcode>(iterator.Next());
227 USE(opcode);
228 ASSERT(Translation::JS_FRAME == opcode);
229 unsigned node_id = iterator.Next();
230 USE(node_id);
231 ASSERT(node_id == ast_id);
232 JSFunction* function = JSFunction::cast(ComputeLiteral(iterator.Next()));
233 USE(function);
234 ASSERT(function == function_);
235 unsigned height = iterator.Next();
236 unsigned height_in_bytes = height * kPointerSize;
237 USE(height_in_bytes);
238
239 unsigned fixed_size = ComputeFixedSize(function_);
240 unsigned input_frame_size = input_->GetFrameSize();
241 ASSERT(fixed_size + height_in_bytes == input_frame_size);
242
243 unsigned stack_slot_size = optimized_code_->stack_slots() * kPointerSize;
244 unsigned outgoing_height = data->ArgumentsStackHeight(bailout_id)->value();
245 unsigned outgoing_size = outgoing_height * kPointerSize;
246 unsigned output_frame_size = fixed_size + stack_slot_size + outgoing_size;
247 ASSERT(outgoing_size == 0); // OSR does not happen in the middle of a call.
248
249 if (FLAG_trace_osr) {
250 PrintF("[on-stack replacement: begin 0x%08" V8PRIxPTR " ",
251 reinterpret_cast<intptr_t>(function_));
252 function_->PrintName();
253 PrintF(" => node=%u, frame=%d->%d]\n",
254 ast_id,
255 input_frame_size,
256 output_frame_size);
257 }
258
259 // There's only one output frame in the OSR case.
260 output_count_ = 1;
261 output_ = new FrameDescription*[1];
262 output_[0] = new(output_frame_size) FrameDescription(
263 output_frame_size, function_);
264 output_[0]->SetFrameType(StackFrame::JAVA_SCRIPT);
265
266 // Clear the incoming parameters in the optimized frame to avoid
267 // confusing the garbage collector.
268 unsigned output_offset = output_frame_size - kPointerSize;
269 int parameter_count = function_->shared()->formal_parameter_count() + 1;
270 for (int i = 0; i < parameter_count; ++i) {
271 output_[0]->SetFrameSlot(output_offset, 0);
272 output_offset -= kPointerSize;
273 }
274
275 // Translate the incoming parameters. This may overwrite some of the
276 // incoming argument slots we've just cleared.
277 int input_offset = input_frame_size - kPointerSize;
278 bool ok = true;
279 int limit = input_offset - (parameter_count * kPointerSize);
280 while (ok && input_offset > limit) {
281 ok = DoOsrTranslateCommand(&iterator, &input_offset);
282 }
283
284 // There are no translation commands for the caller's pc and fp, the
285 // context, and the function. Set them up explicitly.
286 for (int i = StandardFrameConstants::kCallerPCOffset;
287 ok && i >= StandardFrameConstants::kMarkerOffset;
288 i -= kPointerSize) {
289 uint32_t input_value = input_->GetFrameSlot(input_offset);
290 if (FLAG_trace_osr) {
291 const char* name = "UNKNOWN";
292 switch (i) {
293 case StandardFrameConstants::kCallerPCOffset:
294 name = "caller's pc";
295 break;
296 case StandardFrameConstants::kCallerFPOffset:
297 name = "fp";
298 break;
299 case StandardFrameConstants::kContextOffset:
300 name = "context";
301 break;
302 case StandardFrameConstants::kMarkerOffset:
303 name = "function";
304 break;
305 }
306 PrintF(" [sp + %d] <- 0x%08x ; [sp + %d] (fixed part - %s)\n",
307 output_offset,
308 input_value,
309 input_offset,
310 name);
311 }
312
313 output_[0]->SetFrameSlot(output_offset, input_->GetFrameSlot(input_offset));
314 input_offset -= kPointerSize;
315 output_offset -= kPointerSize;
316 }
317
318 // Translate the rest of the frame.
319 while (ok && input_offset >= 0) {
320 ok = DoOsrTranslateCommand(&iterator, &input_offset);
321 }
322
323 // If translation of any command failed, continue using the input frame.
324 if (!ok) {
325 delete output_[0];
326 output_[0] = input_;
327 output_[0]->SetPc(reinterpret_cast<uint32_t>(from_));
328 } else {
329 // Set up the frame pointer and the context pointer.
330 output_[0]->SetRegister(fp.code(), input_->GetRegister(fp.code()));
331 output_[0]->SetRegister(cp.code(), input_->GetRegister(cp.code()));
332
333 unsigned pc_offset = data->OsrPcOffset()->value();
334 uint32_t pc = reinterpret_cast<uint32_t>(
335 optimized_code_->entry() + pc_offset);
336 output_[0]->SetPc(pc);
337 }
338 Code* continuation = isolate_->builtins()->builtin(Builtins::kNotifyOSR);
339 output_[0]->SetContinuation(
340 reinterpret_cast<uint32_t>(continuation->entry()));
341
342 if (FLAG_trace_osr) {
343 PrintF("[on-stack replacement translation %s: 0x%08" V8PRIxPTR " ",
344 ok ? "finished" : "aborted",
345 reinterpret_cast<intptr_t>(function));
346 function->PrintName();
347 PrintF(" => pc=0x%0x]\n", output_[0]->GetPc());
348 }
Steve Block44f0eee2011-05-26 01:26:41 +0100349}
350
351
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100352void Deoptimizer::DoComputeArgumentsAdaptorFrame(TranslationIterator* iterator,
353 int frame_index) {
354 JSFunction* function = JSFunction::cast(ComputeLiteral(iterator->Next()));
355 unsigned height = iterator->Next();
356 unsigned height_in_bytes = height * kPointerSize;
357 if (FLAG_trace_deopt) {
358 PrintF(" translating arguments adaptor => height=%d\n", height_in_bytes);
359 }
360
361 unsigned fixed_frame_size = ArgumentsAdaptorFrameConstants::kFrameSize;
362 unsigned output_frame_size = height_in_bytes + fixed_frame_size;
363
364 // Allocate and store the output frame description.
365 FrameDescription* output_frame =
366 new(output_frame_size) FrameDescription(output_frame_size, function);
367 output_frame->SetFrameType(StackFrame::ARGUMENTS_ADAPTOR);
368
369 // Arguments adaptor can not be topmost or bottommost.
370 ASSERT(frame_index > 0 && frame_index < output_count_ - 1);
371 ASSERT(output_[frame_index] == NULL);
372 output_[frame_index] = output_frame;
373
374 // The top address of the frame is computed from the previous
375 // frame's top and this frame's size.
376 uint32_t top_address;
377 top_address = output_[frame_index - 1]->GetTop() - output_frame_size;
378 output_frame->SetTop(top_address);
379
380 // Compute the incoming parameter translation.
381 int parameter_count = height;
382 unsigned output_offset = output_frame_size;
383 for (int i = 0; i < parameter_count; ++i) {
384 output_offset -= kPointerSize;
385 DoTranslateCommand(iterator, frame_index, output_offset);
386 }
387
388 // Read caller's PC from the previous frame.
389 output_offset -= kPointerSize;
390 intptr_t callers_pc = output_[frame_index - 1]->GetPc();
391 output_frame->SetFrameSlot(output_offset, callers_pc);
392 if (FLAG_trace_deopt) {
393 PrintF(" 0x%08x: [top + %d] <- 0x%08x ; caller's pc\n",
394 top_address + output_offset, output_offset, callers_pc);
395 }
396
397 // Read caller's FP from the previous frame, and set this frame's FP.
398 output_offset -= kPointerSize;
399 intptr_t value = output_[frame_index - 1]->GetFp();
400 output_frame->SetFrameSlot(output_offset, value);
401 intptr_t fp_value = top_address + output_offset;
402 output_frame->SetFp(fp_value);
403 if (FLAG_trace_deopt) {
404 PrintF(" 0x%08x: [top + %d] <- 0x%08x ; caller's fp\n",
405 fp_value, output_offset, value);
406 }
407
408 // A marker value is used in place of the context.
409 output_offset -= kPointerSize;
410 intptr_t context = reinterpret_cast<intptr_t>(
411 Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR));
412 output_frame->SetFrameSlot(output_offset, context);
413 if (FLAG_trace_deopt) {
414 PrintF(" 0x%08x: [top + %d] <- 0x%08x ; context (adaptor sentinel)\n",
415 top_address + output_offset, output_offset, context);
416 }
417
418 // The function was mentioned explicitly in the ARGUMENTS_ADAPTOR_FRAME.
419 output_offset -= kPointerSize;
420 value = reinterpret_cast<intptr_t>(function);
421 output_frame->SetFrameSlot(output_offset, value);
422 if (FLAG_trace_deopt) {
423 PrintF(" 0x%08x: [top + %d] <- 0x%08x ; function\n",
424 top_address + output_offset, output_offset, value);
425 }
426
427 // Number of incoming arguments.
428 output_offset -= kPointerSize;
429 value = reinterpret_cast<uint32_t>(Smi::FromInt(height - 1));
430 output_frame->SetFrameSlot(output_offset, value);
431 if (FLAG_trace_deopt) {
432 PrintF(" 0x%08x: [top + %d] <- 0x%08x ; argc (%d)\n",
433 top_address + output_offset, output_offset, value, height - 1);
434 }
435
436 ASSERT(0 == output_offset);
437
438 Builtins* builtins = isolate_->builtins();
439 Code* adaptor_trampoline =
440 builtins->builtin(Builtins::kArgumentsAdaptorTrampoline);
441 uint32_t pc = reinterpret_cast<uint32_t>(
442 adaptor_trampoline->instruction_start() +
443 isolate_->heap()->arguments_adaptor_deopt_pc_offset()->value());
444 output_frame->SetPc(pc);
Ben Murdochc7cc0282012-03-05 14:35:55 +0000445}
446
447
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100448void Deoptimizer::DoComputeConstructStubFrame(TranslationIterator* iterator,
449 int frame_index) {
450 JSFunction* function = JSFunction::cast(ComputeLiteral(iterator->Next()));
451 unsigned height = iterator->Next();
452 unsigned height_in_bytes = height * kPointerSize;
453 if (FLAG_trace_deopt) {
454 PrintF(" translating construct stub => height=%d\n", height_in_bytes);
455 }
456
457 unsigned fixed_frame_size = 7 * kPointerSize;
458 unsigned output_frame_size = height_in_bytes + fixed_frame_size;
459
460 // Allocate and store the output frame description.
461 FrameDescription* output_frame =
462 new(output_frame_size) FrameDescription(output_frame_size, function);
463 output_frame->SetFrameType(StackFrame::CONSTRUCT);
464
465 // Construct stub can not be topmost or bottommost.
466 ASSERT(frame_index > 0 && frame_index < output_count_ - 1);
467 ASSERT(output_[frame_index] == NULL);
468 output_[frame_index] = output_frame;
469
470 // The top address of the frame is computed from the previous
471 // frame's top and this frame's size.
472 uint32_t top_address;
473 top_address = output_[frame_index - 1]->GetTop() - output_frame_size;
474 output_frame->SetTop(top_address);
475
476 // Compute the incoming parameter translation.
477 int parameter_count = height;
478 unsigned output_offset = output_frame_size;
479 for (int i = 0; i < parameter_count; ++i) {
480 output_offset -= kPointerSize;
481 DoTranslateCommand(iterator, frame_index, output_offset);
482 }
483
484 // Read caller's PC from the previous frame.
485 output_offset -= kPointerSize;
486 intptr_t callers_pc = output_[frame_index - 1]->GetPc();
487 output_frame->SetFrameSlot(output_offset, callers_pc);
488 if (FLAG_trace_deopt) {
489 PrintF(" 0x%08x: [top + %d] <- 0x%08x ; caller's pc\n",
490 top_address + output_offset, output_offset, callers_pc);
491 }
492
493 // Read caller's FP from the previous frame, and set this frame's FP.
494 output_offset -= kPointerSize;
495 intptr_t value = output_[frame_index - 1]->GetFp();
496 output_frame->SetFrameSlot(output_offset, value);
497 intptr_t fp_value = top_address + output_offset;
498 output_frame->SetFp(fp_value);
499 if (FLAG_trace_deopt) {
500 PrintF(" 0x%08x: [top + %d] <- 0x%08x ; caller's fp\n",
501 fp_value, output_offset, value);
502 }
503
504 // The context can be gotten from the previous frame.
505 output_offset -= kPointerSize;
506 value = output_[frame_index - 1]->GetContext();
507 output_frame->SetFrameSlot(output_offset, value);
508 if (FLAG_trace_deopt) {
509 PrintF(" 0x%08x: [top + %d] <- 0x%08x ; context\n",
510 top_address + output_offset, output_offset, value);
511 }
512
513 // A marker value is used in place of the function.
514 output_offset -= kPointerSize;
515 value = reinterpret_cast<intptr_t>(Smi::FromInt(StackFrame::CONSTRUCT));
516 output_frame->SetFrameSlot(output_offset, value);
517 if (FLAG_trace_deopt) {
518 PrintF(" 0x%08x: [top + %d] <- 0x%08x ; function (construct sentinel)\n",
519 top_address + output_offset, output_offset, value);
520 }
521
522 // Number of incoming arguments.
523 output_offset -= kPointerSize;
524 value = reinterpret_cast<uint32_t>(Smi::FromInt(height - 1));
525 output_frame->SetFrameSlot(output_offset, value);
526 if (FLAG_trace_deopt) {
527 PrintF(" 0x%08x: [top + %d] <- 0x%08x ; argc (%d)\n",
528 top_address + output_offset, output_offset, value, height - 1);
529 }
530
531 // Constructor function being invoked by the stub.
532 output_offset -= kPointerSize;
533 value = reinterpret_cast<intptr_t>(function);
534 output_frame->SetFrameSlot(output_offset, value);
535 if (FLAG_trace_deopt) {
536 PrintF(" 0x%08x: [top + %d] <- 0x%08x ; constructor function\n",
537 top_address + output_offset, output_offset, value);
538 }
539
540 // The newly allocated object was passed as receiver in the artificial
541 // constructor stub environment created by HEnvironment::CopyForInlining().
542 output_offset -= kPointerSize;
543 value = output_frame->GetFrameSlot(output_frame_size - kPointerSize);
544 output_frame->SetFrameSlot(output_offset, value);
545 if (FLAG_trace_deopt) {
546 PrintF(" 0x%08x: [top + %d] <- 0x%08x ; allocated receiver\n",
547 top_address + output_offset, output_offset, value);
548 }
549
550 ASSERT(0 == output_offset);
551
552 Builtins* builtins = isolate_->builtins();
553 Code* construct_stub = builtins->builtin(Builtins::kJSConstructStubGeneric);
554 uint32_t pc = reinterpret_cast<uint32_t>(
555 construct_stub->instruction_start() +
556 isolate_->heap()->construct_stub_deopt_pc_offset()->value());
557 output_frame->SetPc(pc);
558}
559
560
561// This code is very similar to ia32/arm code, but relies on register names
562// (fp, sp) and how the frame is laid out.
563void Deoptimizer::DoComputeJSFrame(TranslationIterator* iterator,
564 int frame_index) {
565 // Read the ast node id, function, and frame height for this output frame.
566 int node_id = iterator->Next();
567 JSFunction* function = JSFunction::cast(ComputeLiteral(iterator->Next()));
568 unsigned height = iterator->Next();
569 unsigned height_in_bytes = height * kPointerSize;
570 if (FLAG_trace_deopt) {
571 PrintF(" translating ");
572 function->PrintName();
573 PrintF(" => node=%d, height=%d\n", node_id, height_in_bytes);
574 }
575
576 // The 'fixed' part of the frame consists of the incoming parameters and
577 // the part described by JavaScriptFrameConstants.
578 unsigned fixed_frame_size = ComputeFixedSize(function);
579 unsigned input_frame_size = input_->GetFrameSize();
580 unsigned output_frame_size = height_in_bytes + fixed_frame_size;
581
582 // Allocate and store the output frame description.
583 FrameDescription* output_frame =
584 new(output_frame_size) FrameDescription(output_frame_size, function);
585 output_frame->SetFrameType(StackFrame::JAVA_SCRIPT);
586
587 bool is_bottommost = (0 == frame_index);
588 bool is_topmost = (output_count_ - 1 == frame_index);
589 ASSERT(frame_index >= 0 && frame_index < output_count_);
590 ASSERT(output_[frame_index] == NULL);
591 output_[frame_index] = output_frame;
592
593 // The top address for the bottommost output frame can be computed from
594 // the input frame pointer and the output frame's height. For all
595 // subsequent output frames, it can be computed from the previous one's
596 // top address and the current frame's size.
597 uint32_t top_address;
598 if (is_bottommost) {
599 // 2 = context and function in the frame.
600 top_address =
601 input_->GetRegister(fp.code()) - (2 * kPointerSize) - height_in_bytes;
602 } else {
603 top_address = output_[frame_index - 1]->GetTop() - output_frame_size;
604 }
605 output_frame->SetTop(top_address);
606
607 // Compute the incoming parameter translation.
608 int parameter_count = function->shared()->formal_parameter_count() + 1;
609 unsigned output_offset = output_frame_size;
610 unsigned input_offset = input_frame_size;
611 for (int i = 0; i < parameter_count; ++i) {
612 output_offset -= kPointerSize;
613 DoTranslateCommand(iterator, frame_index, output_offset);
614 }
615 input_offset -= (parameter_count * kPointerSize);
616
617 // There are no translation commands for the caller's pc and fp, the
618 // context, and the function. Synthesize their values and set them up
619 // explicitly.
620 //
621 // The caller's pc for the bottommost output frame is the same as in the
622 // input frame. For all subsequent output frames, it can be read from the
623 // previous one. This frame's pc can be computed from the non-optimized
624 // function code and AST id of the bailout.
625 output_offset -= kPointerSize;
626 input_offset -= kPointerSize;
627 intptr_t value;
628 if (is_bottommost) {
629 value = input_->GetFrameSlot(input_offset);
630 } else {
631 value = output_[frame_index - 1]->GetPc();
632 }
633 output_frame->SetFrameSlot(output_offset, value);
634 if (FLAG_trace_deopt) {
635 PrintF(" 0x%08x: [top + %d] <- 0x%08x ; caller's pc\n",
636 top_address + output_offset, output_offset, value);
637 }
638
639 // The caller's frame pointer for the bottommost output frame is the same
640 // as in the input frame. For all subsequent output frames, it can be
641 // read from the previous one. Also compute and set this frame's frame
642 // pointer.
643 output_offset -= kPointerSize;
644 input_offset -= kPointerSize;
645 if (is_bottommost) {
646 value = input_->GetFrameSlot(input_offset);
647 } else {
648 value = output_[frame_index - 1]->GetFp();
649 }
650 output_frame->SetFrameSlot(output_offset, value);
651 intptr_t fp_value = top_address + output_offset;
652 ASSERT(!is_bottommost || input_->GetRegister(fp.code()) == fp_value);
653 output_frame->SetFp(fp_value);
654 if (is_topmost) {
655 output_frame->SetRegister(fp.code(), fp_value);
656 }
657 if (FLAG_trace_deopt) {
658 PrintF(" 0x%08x: [top + %d] <- 0x%08x ; caller's fp\n",
659 fp_value, output_offset, value);
660 }
661
662 // For the bottommost output frame the context can be gotten from the input
663 // frame. For all subsequent output frames it can be gotten from the function
664 // so long as we don't inline functions that need local contexts.
665 output_offset -= kPointerSize;
666 input_offset -= kPointerSize;
667 if (is_bottommost) {
668 value = input_->GetFrameSlot(input_offset);
669 } else {
670 value = reinterpret_cast<intptr_t>(function->context());
671 }
672 output_frame->SetFrameSlot(output_offset, value);
673 output_frame->SetContext(value);
674 if (is_topmost) output_frame->SetRegister(cp.code(), value);
675 if (FLAG_trace_deopt) {
676 PrintF(" 0x%08x: [top + %d] <- 0x%08x ; context\n",
677 top_address + output_offset, output_offset, value);
678 }
679
680 // The function was mentioned explicitly in the BEGIN_FRAME.
681 output_offset -= kPointerSize;
682 input_offset -= kPointerSize;
683 value = reinterpret_cast<uint32_t>(function);
684 // The function for the bottommost output frame should also agree with the
685 // input frame.
686 ASSERT(!is_bottommost || input_->GetFrameSlot(input_offset) == value);
687 output_frame->SetFrameSlot(output_offset, value);
688 if (FLAG_trace_deopt) {
689 PrintF(" 0x%08x: [top + %d] <- 0x%08x ; function\n",
690 top_address + output_offset, output_offset, value);
691 }
692
693 // Translate the rest of the frame.
694 for (unsigned i = 0; i < height; ++i) {
695 output_offset -= kPointerSize;
696 DoTranslateCommand(iterator, frame_index, output_offset);
697 }
698 ASSERT(0 == output_offset);
699
700 // Compute this frame's PC, state, and continuation.
701 Code* non_optimized_code = function->shared()->code();
702 FixedArray* raw_data = non_optimized_code->deoptimization_data();
703 DeoptimizationOutputData* data = DeoptimizationOutputData::cast(raw_data);
704 Address start = non_optimized_code->instruction_start();
705 unsigned pc_and_state = GetOutputInfo(data, node_id, function->shared());
706 unsigned pc_offset = FullCodeGenerator::PcField::decode(pc_and_state);
707 uint32_t pc_value = reinterpret_cast<uint32_t>(start + pc_offset);
708 output_frame->SetPc(pc_value);
709
710 FullCodeGenerator::State state =
711 FullCodeGenerator::StateField::decode(pc_and_state);
712 output_frame->SetState(Smi::FromInt(state));
713
714
715 // Set the continuation for the topmost frame.
716 if (is_topmost && bailout_type_ != DEBUGGER) {
717 Builtins* builtins = isolate_->builtins();
718 Code* continuation = (bailout_type_ == EAGER)
719 ? builtins->builtin(Builtins::kNotifyDeoptimized)
720 : builtins->builtin(Builtins::kNotifyLazyDeoptimized);
721 output_frame->SetContinuation(
722 reinterpret_cast<uint32_t>(continuation->entry()));
723 }
724}
725
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000726void Deoptimizer::FillInputFrame(Address tos, JavaScriptFrame* frame) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100727 // Set the register values. The values are not important as there are no
728 // callee saved registers in JavaScript frames, so all registers are
729 // spilled. Registers fp and sp are set to the correct values though.
730
731 for (int i = 0; i < Register::kNumRegisters; i++) {
732 input_->SetRegister(i, i * 4);
733 }
734 input_->SetRegister(sp.code(), reinterpret_cast<intptr_t>(frame->sp()));
735 input_->SetRegister(fp.code(), reinterpret_cast<intptr_t>(frame->fp()));
736 for (int i = 0; i < DoubleRegister::kNumAllocatableRegisters; i++) {
737 input_->SetDoubleRegister(i, 0.0);
738 }
739
740 // Fill the frame content from the actual data on the frame.
741 for (unsigned i = 0; i < input_->GetFrameSize(); i += kPointerSize) {
742 input_->SetFrameSlot(i, Memory::uint32_at(tos + i));
743 }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000744}
745
746
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100747#define __ masm()->
748
749
750// This code tries to be close to ia32 code so that any changes can be
751// easily ported.
Steve Block44f0eee2011-05-26 01:26:41 +0100752void Deoptimizer::EntryGenerator::Generate() {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100753 GeneratePrologue();
754
755 Isolate* isolate = masm()->isolate();
756
757 CpuFeatures::Scope scope(FPU);
758 // Unlike on ARM we don't save all the registers, just the useful ones.
759 // For the rest, there are gaps on the stack, so the offsets remain the same.
760 const int kNumberOfRegisters = Register::kNumRegisters;
761
762 RegList restored_regs = kJSCallerSaved | kCalleeSaved;
763 RegList saved_regs = restored_regs | sp.bit() | ra.bit();
764
765 const int kDoubleRegsSize =
766 kDoubleSize * FPURegister::kNumAllocatableRegisters;
767
768 // Save all FPU registers before messing with them.
769 __ Subu(sp, sp, Operand(kDoubleRegsSize));
770 for (int i = 0; i < FPURegister::kNumAllocatableRegisters; ++i) {
771 FPURegister fpu_reg = FPURegister::FromAllocationIndex(i);
772 int offset = i * kDoubleSize;
773 __ sdc1(fpu_reg, MemOperand(sp, offset));
774 }
775
776 // Push saved_regs (needed to populate FrameDescription::registers_).
777 // Leave gaps for other registers.
778 __ Subu(sp, sp, kNumberOfRegisters * kPointerSize);
779 for (int16_t i = kNumberOfRegisters - 1; i >= 0; i--) {
780 if ((saved_regs & (1 << i)) != 0) {
781 __ sw(ToRegister(i), MemOperand(sp, kPointerSize * i));
782 }
783 }
784
785 const int kSavedRegistersAreaSize =
786 (kNumberOfRegisters * kPointerSize) + kDoubleRegsSize;
787
788 // Get the bailout id from the stack.
789 __ lw(a2, MemOperand(sp, kSavedRegistersAreaSize));
790
791 // Get the address of the location in the code object if possible (a3) (return
792 // address for lazy deoptimization) and compute the fp-to-sp delta in
793 // register t0.
794 if (type() == EAGER) {
795 __ mov(a3, zero_reg);
796 // Correct one word for bailout id.
797 __ Addu(t0, sp, Operand(kSavedRegistersAreaSize + (1 * kPointerSize)));
798 } else if (type() == OSR) {
799 __ mov(a3, ra);
800 // Correct one word for bailout id.
801 __ Addu(t0, sp, Operand(kSavedRegistersAreaSize + (1 * kPointerSize)));
802 } else {
803 __ mov(a3, ra);
804 // Correct two words for bailout id and return address.
805 __ Addu(t0, sp, Operand(kSavedRegistersAreaSize + (2 * kPointerSize)));
806 }
807
808 __ Subu(t0, fp, t0);
809
810 // Allocate a new deoptimizer object.
811 // Pass four arguments in a0 to a3 and fifth & sixth arguments on stack.
812 __ PrepareCallCFunction(6, t1);
813 __ lw(a0, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
814 __ li(a1, Operand(type())); // bailout type,
815 // a2: bailout id already loaded.
816 // a3: code address or 0 already loaded.
817 __ sw(t0, CFunctionArgumentOperand(5)); // Fp-to-sp delta.
818 __ li(t1, Operand(ExternalReference::isolate_address()));
819 __ sw(t1, CFunctionArgumentOperand(6)); // Isolate.
820 // Call Deoptimizer::New().
821 {
822 AllowExternalCallThatCantCauseGC scope(masm());
823 __ CallCFunction(ExternalReference::new_deoptimizer_function(isolate), 6);
824 }
825
826 // Preserve "deoptimizer" object in register v0 and get the input
827 // frame descriptor pointer to a1 (deoptimizer->input_);
828 // Move deopt-obj to a0 for call to Deoptimizer::ComputeOutputFrames() below.
829 __ mov(a0, v0);
830 __ lw(a1, MemOperand(v0, Deoptimizer::input_offset()));
831
832 // Copy core registers into FrameDescription::registers_[kNumRegisters].
833 ASSERT(Register::kNumRegisters == kNumberOfRegisters);
834 for (int i = 0; i < kNumberOfRegisters; i++) {
835 int offset = (i * kPointerSize) + FrameDescription::registers_offset();
836 if ((saved_regs & (1 << i)) != 0) {
837 __ lw(a2, MemOperand(sp, i * kPointerSize));
838 __ sw(a2, MemOperand(a1, offset));
839 } else if (FLAG_debug_code) {
840 __ li(a2, kDebugZapValue);
841 __ sw(a2, MemOperand(a1, offset));
842 }
843 }
844
845 // Copy FPU registers to
846 // double_registers_[DoubleRegister::kNumAllocatableRegisters]
847 int double_regs_offset = FrameDescription::double_registers_offset();
848 for (int i = 0; i < FPURegister::kNumAllocatableRegisters; ++i) {
849 int dst_offset = i * kDoubleSize + double_regs_offset;
850 int src_offset = i * kDoubleSize + kNumberOfRegisters * kPointerSize;
851 __ ldc1(f0, MemOperand(sp, src_offset));
852 __ sdc1(f0, MemOperand(a1, dst_offset));
853 }
854
855 // Remove the bailout id, eventually return address, and the saved registers
856 // from the stack.
857 if (type() == EAGER || type() == OSR) {
858 __ Addu(sp, sp, Operand(kSavedRegistersAreaSize + (1 * kPointerSize)));
859 } else {
860 __ Addu(sp, sp, Operand(kSavedRegistersAreaSize + (2 * kPointerSize)));
861 }
862
863 // Compute a pointer to the unwinding limit in register a2; that is
864 // the first stack slot not part of the input frame.
865 __ lw(a2, MemOperand(a1, FrameDescription::frame_size_offset()));
866 __ Addu(a2, a2, sp);
867
868 // Unwind the stack down to - but not including - the unwinding
869 // limit and copy the contents of the activation frame to the input
870 // frame description.
871 __ Addu(a3, a1, Operand(FrameDescription::frame_content_offset()));
872 Label pop_loop;
873 __ bind(&pop_loop);
874 __ pop(t0);
875 __ sw(t0, MemOperand(a3, 0));
876 __ Branch(USE_DELAY_SLOT, &pop_loop, ne, a2, Operand(sp));
877 __ addiu(a3, a3, sizeof(uint32_t)); // In delay slot.
878
879 // Compute the output frame in the deoptimizer.
880 __ push(a0); // Preserve deoptimizer object across call.
881 // a0: deoptimizer object; a1: scratch.
882 __ PrepareCallCFunction(1, a1);
883 // Call Deoptimizer::ComputeOutputFrames().
884 {
885 AllowExternalCallThatCantCauseGC scope(masm());
886 __ CallCFunction(
887 ExternalReference::compute_output_frames_function(isolate), 1);
888 }
889 __ pop(a0); // Restore deoptimizer object (class Deoptimizer).
890
891 // Replace the current (input) frame with the output frames.
892 Label outer_push_loop, inner_push_loop;
893 // Outer loop state: a0 = current "FrameDescription** output_",
894 // a1 = one past the last FrameDescription**.
895 __ lw(a1, MemOperand(a0, Deoptimizer::output_count_offset()));
896 __ lw(a0, MemOperand(a0, Deoptimizer::output_offset())); // a0 is output_.
897 __ sll(a1, a1, kPointerSizeLog2); // Count to offset.
898 __ addu(a1, a0, a1); // a1 = one past the last FrameDescription**.
899 __ bind(&outer_push_loop);
900 // Inner loop state: a2 = current FrameDescription*, a3 = loop index.
901 __ lw(a2, MemOperand(a0, 0)); // output_[ix]
902 __ lw(a3, MemOperand(a2, FrameDescription::frame_size_offset()));
903 __ bind(&inner_push_loop);
904 __ Subu(a3, a3, Operand(sizeof(uint32_t)));
905 __ Addu(t2, a2, Operand(a3));
906 __ lw(t3, MemOperand(t2, FrameDescription::frame_content_offset()));
907 __ push(t3);
908 __ Branch(&inner_push_loop, ne, a3, Operand(zero_reg));
909
910 __ Addu(a0, a0, Operand(kPointerSize));
911 __ Branch(&outer_push_loop, lt, a0, Operand(a1));
912
913
914 // Push state, pc, and continuation from the last output frame.
915 if (type() != OSR) {
916 __ lw(t2, MemOperand(a2, FrameDescription::state_offset()));
917 __ push(t2);
918 }
919
920 __ lw(t2, MemOperand(a2, FrameDescription::pc_offset()));
921 __ push(t2);
922 __ lw(t2, MemOperand(a2, FrameDescription::continuation_offset()));
923 __ push(t2);
924
925
926 // Technically restoring 'at' should work unless zero_reg is also restored
927 // but it's safer to check for this.
928 ASSERT(!(at.bit() & restored_regs));
929 // Restore the registers from the last output frame.
930 __ mov(at, a2);
931 for (int i = kNumberOfRegisters - 1; i >= 0; i--) {
932 int offset = (i * kPointerSize) + FrameDescription::registers_offset();
933 if ((restored_regs & (1 << i)) != 0) {
934 __ lw(ToRegister(i), MemOperand(at, offset));
935 }
936 }
937
938 __ InitializeRootRegister();
939
940 __ pop(at); // Get continuation, leave pc on stack.
941 __ pop(ra);
942 __ Jump(at);
943 __ stop("Unreachable.");
Steve Block44f0eee2011-05-26 01:26:41 +0100944}
945
946
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100947// Maximum size of a table entry generated below.
948const int Deoptimizer::table_entry_size_ = 9 * Assembler::kInstrSize;
949
Steve Block44f0eee2011-05-26 01:26:41 +0100950void Deoptimizer::TableEntryGenerator::GeneratePrologue() {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100951 Assembler::BlockTrampolinePoolScope block_trampoline_pool(masm());
952
953 // Create a sequence of deoptimization entries. Note that any
954 // registers may be still live.
955 Label table_start;
956 __ bind(&table_start);
957 for (int i = 0; i < count(); i++) {
958 Label start;
959 __ bind(&start);
960 if (type() != EAGER) {
961 // Emulate ia32 like call by pushing return address to stack.
962 __ addiu(sp, sp, -2 * kPointerSize);
963 __ sw(ra, MemOperand(sp, 1 * kPointerSize));
964 } else {
965 __ addiu(sp, sp, -1 * kPointerSize);
966 }
967 // Jump over the remaining deopt entries (including this one).
968 // This code is always reached by calling Jump, which puts the target (label
969 // start) into t9.
970 const int remaining_entries = (count() - i) * table_entry_size_;
971 __ Addu(t9, t9, remaining_entries);
972 // 'at' was clobbered so we can only load the current entry value here.
973 __ li(at, i);
974 __ jr(t9); // Expose delay slot.
975 __ sw(at, MemOperand(sp, 0 * kPointerSize)); // In the delay slot.
976
977 // Pad the rest of the code.
978 while (table_entry_size_ > (masm()->SizeOfCodeGeneratedSince(&start))) {
979 __ nop();
980 }
981
982 ASSERT_EQ(table_entry_size_, masm()->SizeOfCodeGeneratedSince(&start));
983 }
984
985 ASSERT_EQ(masm()->SizeOfCodeGeneratedSince(&table_start),
986 count() * table_entry_size_);
Steve Block44f0eee2011-05-26 01:26:41 +0100987}
988
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100989#undef __
990
Steve Block44f0eee2011-05-26 01:26:41 +0100991
992} } // namespace v8::internal