blob: 9a8cfd0cfdb9acd1106babb2d83cc91507ce223e [file] [log] [blame]
lrn@chromium.org7516f052011-03-30 08:52:27 +00001// 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
lrn@chromium.org7516f052011-03-30 08:52:27 +000035namespace v8 {
36namespace internal {
37
38
lrn@chromium.org7516f052011-03-30 08:52:27 +000039int Deoptimizer::patch_size() {
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +000040 const int kCallInstructionSizeInWords = 4;
lrn@chromium.org7516f052011-03-30 08:52:27 +000041 return kCallInstructionSizeInWords * Assembler::kInstrSize;
42}
43
44
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +000045void Deoptimizer::DeoptimizeFunctionWithPreparedFunctionList(
46 JSFunction* function) {
47 Isolate* isolate = function->GetIsolate();
48 HandleScope scope(isolate);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +000049 AssertNoAllocation no_allocation;
50
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +000051 ASSERT(function->IsOptimized());
52 ASSERT(function->FunctionsInFunctionListShareSameCode());
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +000053
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +000054 // The optimized code is going to be patched, so we cannot use it
55 // any more. Play safe and reset the whole cache.
56 function->shared()->ClearOptimizedCodeMap();
57
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +000058 // Get the optimized code.
59 Code* code = function->code();
erikcorry0ad885c2011-11-21 13:51:57 +000060 Address code_start_address = code->instruction_start();
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +000061
62 // Invalidate the relocation information, as it will become invalid by the
63 // code patching below, and is not needed any more.
64 code->InvalidateRelocation();
65
erikcorry0ad885c2011-11-21 13:51:57 +000066 // For each LLazyBailout instruction insert a call to the corresponding
67 // deoptimization entry.
68 DeoptimizationInputData* deopt_data =
69 DeoptimizationInputData::cast(code->deoptimization_data());
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +000070#ifdef DEBUG
erikcorry0ad885c2011-11-21 13:51:57 +000071 Address prev_call_address = NULL;
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +000072#endif
erikcorry0ad885c2011-11-21 13:51:57 +000073 for (int i = 0; i < deopt_data->DeoptCount(); i++) {
74 if (deopt_data->Pc(i)->value() == -1) continue;
75 Address call_address = code_start_address + deopt_data->Pc(i)->value();
76 Address deopt_entry = GetDeoptimizationEntry(i, LAZY);
77 int call_size_in_bytes = MacroAssembler::CallSize(deopt_entry,
jkummerow@chromium.org59297c72013-01-09 16:32:23 +000078 RelocInfo::NONE32);
erikcorry0ad885c2011-11-21 13:51:57 +000079 int call_size_in_words = call_size_in_bytes / Assembler::kInstrSize;
80 ASSERT(call_size_in_bytes % Assembler::kInstrSize == 0);
81 ASSERT(call_size_in_bytes <= patch_size());
82 CodePatcher patcher(call_address, call_size_in_words);
jkummerow@chromium.org59297c72013-01-09 16:32:23 +000083 patcher.masm()->Call(deopt_entry, RelocInfo::NONE32);
erikcorry0ad885c2011-11-21 13:51:57 +000084 ASSERT(prev_call_address == NULL ||
85 call_address >= prev_call_address + patch_size());
86 ASSERT(call_address + patch_size() <= code->instruction_end());
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +000087
88#ifdef DEBUG
erikcorry0ad885c2011-11-21 13:51:57 +000089 prev_call_address = call_address;
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +000090#endif
erikcorry0ad885c2011-11-21 13:51:57 +000091 }
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +000092
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +000093 // Add the deoptimizing code to the list.
94 DeoptimizingCodeListNode* node = new DeoptimizingCodeListNode(code);
95 DeoptimizerData* data = isolate->deoptimizer_data();
96 node->set_next(data->deoptimizing_code_list_);
97 data->deoptimizing_code_list_ = node;
98
99 // We might be in the middle of incremental marking with compaction.
100 // Tell collector to treat this code object in a special way and
101 // ignore all slots that might have been recorded on it.
102 isolate->heap()->mark_compact_collector()->InvalidateCode(code);
103
rossberg@chromium.org89e18f52012-10-22 13:09:53 +0000104 ReplaceCodeForRelatedFunctions(function, code);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000105
106 if (FLAG_trace_deopt) {
107 PrintF("[forced deoptimization: ");
108 function->PrintName();
109 PrintF(" / %x]\n", reinterpret_cast<uint32_t>(function));
110#ifdef DEBUG
111 if (FLAG_print_code) {
112 code->PrintLn();
113 }
114#endif
115 }
lrn@chromium.org7516f052011-03-30 08:52:27 +0000116}
117
118
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +0000119void Deoptimizer::PatchStackCheckCodeAt(Code* unoptimized_code,
120 Address pc_after,
lrn@chromium.org7516f052011-03-30 08:52:27 +0000121 Code* check_code,
122 Code* replacement_code) {
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000123 const int kInstrSize = Assembler::kInstrSize;
rossberg@chromium.orgcddc71f2012-12-07 12:40:13 +0000124 // This structure comes from FullCodeGenerator::EmitBackEdgeBookkeeping.
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000125 // The call of the stack guard check has the following form:
jkummerow@chromium.org1456e702012-03-30 08:38:13 +0000126 // sltu at, sp, t0 / slt at, a3, zero_reg (in case of count based interrupts)
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000127 // beq at, zero_reg, ok
128 // lui t9, <stack guard address> upper
129 // ori t9, <stack guard address> lower
130 // jalr t9
131 // nop
132 // ----- pc_after points here
133
134 ASSERT(Assembler::IsBeq(Assembler::instr_at(pc_after - 5 * kInstrSize)));
135
136 // Replace the sltu instruction with load-imm 1 to at, so beq is not taken.
137 CodePatcher patcher(pc_after - 6 * kInstrSize, 1);
138 patcher.masm()->addiu(at, zero_reg, 1);
139
140 // Replace the stack check address in the load-immediate (lui/ori pair)
141 // with the entry address of the replacement code.
142 ASSERT(reinterpret_cast<uint32_t>(
143 Assembler::target_address_at(pc_after - 4 * kInstrSize)) ==
144 reinterpret_cast<uint32_t>(check_code->entry()));
145 Assembler::set_target_address_at(pc_after - 4 * kInstrSize,
146 replacement_code->entry());
147
148 // We patched the code to the following form:
149 // addiu at, zero_reg, 1
150 // beq at, zero_reg, ok ;; Not changed
151 // lui t9, <on-stack replacement address> upper
152 // ori t9, <on-stack replacement address> lower
153 // jalr t9 ;; Not changed
154 // nop ;; Not changed
155 // ----- pc_after points here
156
157 unoptimized_code->GetHeap()->incremental_marking()->RecordCodeTargetPatch(
158 unoptimized_code, pc_after - 4 * kInstrSize, replacement_code);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000159}
160
161
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000162void Deoptimizer::RevertStackCheckCodeAt(Code* unoptimized_code,
163 Address pc_after,
lrn@chromium.org7516f052011-03-30 08:52:27 +0000164 Code* check_code,
165 Code* replacement_code) {
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000166 // Exact opposite of the function above.
167 const int kInstrSize = Assembler::kInstrSize;
168 ASSERT(Assembler::IsAddImmediate(
169 Assembler::instr_at(pc_after - 6 * kInstrSize)));
170 ASSERT(Assembler::IsBeq(Assembler::instr_at(pc_after - 5 * kInstrSize)));
171
172 // Restore the sltu instruction so beq can be taken again.
173 CodePatcher patcher(pc_after - 6 * kInstrSize, 1);
rossberg@chromium.orgcddc71f2012-12-07 12:40:13 +0000174 patcher.masm()->slt(at, a3, zero_reg);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000175
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
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +0000189static int LookupBailoutId(DeoptimizationInputData* data, BailoutId ast_id) {
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000190 ByteArray* translations = data->TranslationByteArray();
191 int length = data->DeoptCount();
192 for (int i = 0; i < length; i++) {
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +0000193 if (data->AstId(i) == ast_id) {
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000194 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;
lrn@chromium.org7516f052011-03-30 08:52:27 +0000204}
205
206
207void Deoptimizer::DoComputeOsrOutputFrame() {
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000208 DeoptimizationInputData* data = DeoptimizationInputData::cast(
jkummerow@chromium.org59297c72013-01-09 16:32:23 +0000209 compiled_code_->deoptimization_data());
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000210 unsigned ast_id = data->OsrAstId()->value();
211
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +0000212 int bailout_id = LookupBailoutId(data, BailoutId(ast_id));
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000213 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();
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000222 iterator.Skip(1); // Drop JS frame count.
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000223 ASSERT(count == 1);
224 USE(count);
225
226 opcode = static_cast<Translation::Opcode>(iterator.Next());
227 USE(opcode);
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000228 ASSERT(Translation::JS_FRAME == opcode);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000229 unsigned node_id = iterator.Next();
230 USE(node_id);
231 ASSERT(node_id == ast_id);
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000232 int closure_id = iterator.Next();
233 USE(closure_id);
234 ASSERT_EQ(Translation::kSelfLiteralId, closure_id);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000235 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
jkummerow@chromium.org59297c72013-01-09 16:32:23 +0000243 unsigned stack_slot_size = compiled_code_->stack_slots() * kPointerSize;
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000244 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_);
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000264 output_[0]->SetFrameType(StackFrame::JAVA_SCRIPT);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000265
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 {
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000329 // Set up the frame pointer and the context pointer.
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000330 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>(
jkummerow@chromium.org59297c72013-01-09 16:32:23 +0000335 compiled_code_->entry() + pc_offset);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000336 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",
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000345 reinterpret_cast<intptr_t>(function_));
346 function_->PrintName();
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000347 PrintF(" => pc=0x%0x]\n", output_[0]->GetPc());
348 }
lrn@chromium.org7516f052011-03-30 08:52:27 +0000349}
350
351
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000352void 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;
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000362 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;
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000383 for (int i = 0; i < parameter_count; ++i) {
384 output_offset -= kPointerSize;
385 DoTranslateCommand(iterator, frame_index, output_offset);
386 }
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000387
388 // Read caller's PC from the previous frame.
389 output_offset -= kPointerSize;
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000390 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;
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000399 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;
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000410 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;
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000420 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;
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000429 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);
445}
446
447
jkummerow@chromium.org59297c72013-01-09 16:32:23 +0000448void Deoptimizer::DoCompiledStubFrame(TranslationIterator* iterator,
449 int frame_index) {
450 //
mstarzinger@chromium.orge3b8d0f2013-02-01 09:06:41 +0000451 // FROM TO
jkummerow@chromium.org59297c72013-01-09 16:32:23 +0000452 // | .... | | .... |
453 // +-------------------------+ +-------------------------+
mstarzinger@chromium.orge3b8d0f2013-02-01 09:06:41 +0000454 // | JSFunction continuation | | JSFunction continuation |
jkummerow@chromium.org59297c72013-01-09 16:32:23 +0000455 // +-------------------------+ +-------------------------+
mstarzinger@chromium.orge3b8d0f2013-02-01 09:06:41 +0000456 // | | saved frame (fp) | | saved frame (fp) |
457 // | +=========================+<-fp +=========================+<-fp
458 // | | JSFunction context | | JSFunction context |
jkummerow@chromium.org59297c72013-01-09 16:32:23 +0000459 // v +-------------------------+ +-------------------------|
mstarzinger@chromium.orge3b8d0f2013-02-01 09:06:41 +0000460 // | COMPILED_STUB marker | | STUB_FAILURE marker |
461 // +-------------------------+ +-------------------------+
462 // | | | stub parameter 1 |
463 // | ... | +-------------------------+
464 // | | | ... |
465 // |-------------------------|<-sp +-------------------------+
466 // | stub parameter n |
467 // parameters in registers +-------------------------+<-sp
468 // and spilled to stack s0-s1 = number of parameters
469 // s2 = failure handler address
470 // fp = saved frame
471 // cp = JSFunction context
jkummerow@chromium.org59297c72013-01-09 16:32:23 +0000472 //
473
474 ASSERT(compiled_code_->kind() == Code::COMPILED_STUB);
475 int major_key = compiled_code_->major_key();
476 CodeStubInterfaceDescriptor* descriptor =
477 isolate_->code_stub_interface_descriptor(major_key);
478
mstarzinger@chromium.orge3b8d0f2013-02-01 09:06:41 +0000479 int output_frame_size = StandardFrameConstants::kFixedFrameSize +
480 kPointerSize * descriptor->register_param_count_;
481
jkummerow@chromium.org59297c72013-01-09 16:32:23 +0000482 FrameDescription* output_frame =
483 new(output_frame_size) FrameDescription(output_frame_size, 0);
mstarzinger@chromium.orge3b8d0f2013-02-01 09:06:41 +0000484 ASSERT(frame_index == 0);
485 output_[frame_index] = output_frame;
jkummerow@chromium.org59297c72013-01-09 16:32:23 +0000486 Code* notify_failure =
487 isolate_->builtins()->builtin(Builtins::kNotifyStubFailure);
488 output_frame->SetState(Smi::FromInt(FullCodeGenerator::NO_REGISTERS));
489 output_frame->SetContinuation(
mstarzinger@chromium.orge3b8d0f2013-02-01 09:06:41 +0000490 reinterpret_cast<intptr_t>(notify_failure->entry()));
jkummerow@chromium.org59297c72013-01-09 16:32:23 +0000491
mstarzinger@chromium.orge3b8d0f2013-02-01 09:06:41 +0000492 Code* trampoline = NULL;
493 StubFailureTrampolineStub().FindCodeInCache(&trampoline, isolate_);
494 ASSERT(trampoline != NULL);
495 output_frame->SetPc(reinterpret_cast<intptr_t>(
496 trampoline->instruction_start()));
jkummerow@chromium.org59297c72013-01-09 16:32:23 +0000497 unsigned input_frame_size = input_->GetFrameSize();
mstarzinger@chromium.orge3b8d0f2013-02-01 09:06:41 +0000498
499 // JSFunction continuation
500 intptr_t input_frame_offset = input_frame_size - kPointerSize;
501 intptr_t output_frame_offset = output_frame_size - kPointerSize;
502 intptr_t value = input_->GetFrameSlot(input_frame_offset);
503 output_frame->SetFrameSlot(output_frame_offset, value);
504
505 // saved frame ptr
506 input_frame_offset -= kPointerSize;
507 value = input_->GetFrameSlot(input_frame_offset);
508 output_frame_offset -= kPointerSize;
509 output_frame->SetFrameSlot(output_frame_offset, value);
510
511 // Restore context
512 input_frame_offset -= kPointerSize;
513 value = input_->GetFrameSlot(input_frame_offset);
514 output_frame->SetRegister(cp.code(), value);
515 output_frame_offset -= kPointerSize;
516 output_frame->SetFrameSlot(output_frame_offset, value);
517
518 // Internal frame markers
519 output_frame_offset -= kPointerSize;
520 value = reinterpret_cast<intptr_t>(
521 Smi::FromInt(StackFrame::STUB_FAILURE_TRAMPOLINE));
522 output_frame->SetFrameSlot(output_frame_offset, value);
523
524 for (int i = 0; i < descriptor->register_param_count_; ++i) {
525 output_frame_offset -= kPointerSize;
526 DoTranslateCommand(iterator, 0, output_frame_offset);
527 }
528
529 value = input_->GetRegister(fp.code());
jkummerow@chromium.org59297c72013-01-09 16:32:23 +0000530 output_frame->SetRegister(fp.code(), value);
531 output_frame->SetFp(value);
jkummerow@chromium.org59297c72013-01-09 16:32:23 +0000532
533 ApiFunction function(descriptor->deoptimization_handler_);
534 ExternalReference xref(&function, ExternalReference::BUILTIN_CALL, isolate_);
535 intptr_t handler = reinterpret_cast<intptr_t>(xref.address());
536 output_frame->SetRegister(s0.code(), descriptor->register_param_count_);
537 output_frame->SetRegister(s1.code(),
538 (descriptor->register_param_count_ - 1) * kPointerSize);
539 output_frame->SetRegister(s2.code(), handler);
jkummerow@chromium.org59297c72013-01-09 16:32:23 +0000540}
541
542
ulan@chromium.org812308e2012-02-29 15:58:45 +0000543void Deoptimizer::DoComputeConstructStubFrame(TranslationIterator* iterator,
544 int frame_index) {
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +0000545 Builtins* builtins = isolate_->builtins();
546 Code* construct_stub = builtins->builtin(Builtins::kJSConstructStubGeneric);
ulan@chromium.org812308e2012-02-29 15:58:45 +0000547 JSFunction* function = JSFunction::cast(ComputeLiteral(iterator->Next()));
548 unsigned height = iterator->Next();
549 unsigned height_in_bytes = height * kPointerSize;
550 if (FLAG_trace_deopt) {
551 PrintF(" translating construct stub => height=%d\n", height_in_bytes);
552 }
553
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +0000554 unsigned fixed_frame_size = 8 * kPointerSize;
ulan@chromium.org812308e2012-02-29 15:58:45 +0000555 unsigned output_frame_size = height_in_bytes + fixed_frame_size;
556
557 // Allocate and store the output frame description.
558 FrameDescription* output_frame =
559 new(output_frame_size) FrameDescription(output_frame_size, function);
560 output_frame->SetFrameType(StackFrame::CONSTRUCT);
561
562 // Construct stub can not be topmost or bottommost.
563 ASSERT(frame_index > 0 && frame_index < output_count_ - 1);
564 ASSERT(output_[frame_index] == NULL);
565 output_[frame_index] = output_frame;
566
567 // The top address of the frame is computed from the previous
568 // frame's top and this frame's size.
569 uint32_t top_address;
570 top_address = output_[frame_index - 1]->GetTop() - output_frame_size;
571 output_frame->SetTop(top_address);
572
573 // Compute the incoming parameter translation.
574 int parameter_count = height;
575 unsigned output_offset = output_frame_size;
576 for (int i = 0; i < parameter_count; ++i) {
577 output_offset -= kPointerSize;
578 DoTranslateCommand(iterator, frame_index, output_offset);
579 }
580
581 // Read caller's PC from the previous frame.
582 output_offset -= kPointerSize;
583 intptr_t callers_pc = output_[frame_index - 1]->GetPc();
584 output_frame->SetFrameSlot(output_offset, callers_pc);
585 if (FLAG_trace_deopt) {
586 PrintF(" 0x%08x: [top + %d] <- 0x%08x ; caller's pc\n",
587 top_address + output_offset, output_offset, callers_pc);
588 }
589
590 // Read caller's FP from the previous frame, and set this frame's FP.
591 output_offset -= kPointerSize;
592 intptr_t value = output_[frame_index - 1]->GetFp();
593 output_frame->SetFrameSlot(output_offset, value);
594 intptr_t fp_value = top_address + output_offset;
595 output_frame->SetFp(fp_value);
596 if (FLAG_trace_deopt) {
597 PrintF(" 0x%08x: [top + %d] <- 0x%08x ; caller's fp\n",
598 fp_value, output_offset, value);
599 }
600
601 // The context can be gotten from the previous frame.
602 output_offset -= kPointerSize;
603 value = output_[frame_index - 1]->GetContext();
604 output_frame->SetFrameSlot(output_offset, value);
605 if (FLAG_trace_deopt) {
606 PrintF(" 0x%08x: [top + %d] <- 0x%08x ; context\n",
607 top_address + output_offset, output_offset, value);
608 }
609
610 // A marker value is used in place of the function.
611 output_offset -= kPointerSize;
612 value = reinterpret_cast<intptr_t>(Smi::FromInt(StackFrame::CONSTRUCT));
613 output_frame->SetFrameSlot(output_offset, value);
614 if (FLAG_trace_deopt) {
615 PrintF(" 0x%08x: [top + %d] <- 0x%08x ; function (construct sentinel)\n",
616 top_address + output_offset, output_offset, value);
617 }
618
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +0000619 // The output frame reflects a JSConstructStubGeneric frame.
620 output_offset -= kPointerSize;
621 value = reinterpret_cast<intptr_t>(construct_stub);
622 output_frame->SetFrameSlot(output_offset, value);
623 if (FLAG_trace_deopt) {
624 PrintF(" 0x%08x: [top + %d] <- 0x%08x ; code object\n",
625 top_address + output_offset, output_offset, value);
626 }
627
ulan@chromium.org812308e2012-02-29 15:58:45 +0000628 // Number of incoming arguments.
629 output_offset -= kPointerSize;
630 value = reinterpret_cast<uint32_t>(Smi::FromInt(height - 1));
631 output_frame->SetFrameSlot(output_offset, value);
632 if (FLAG_trace_deopt) {
633 PrintF(" 0x%08x: [top + %d] <- 0x%08x ; argc (%d)\n",
634 top_address + output_offset, output_offset, value, height - 1);
635 }
636
637 // Constructor function being invoked by the stub.
638 output_offset -= kPointerSize;
639 value = reinterpret_cast<intptr_t>(function);
640 output_frame->SetFrameSlot(output_offset, value);
641 if (FLAG_trace_deopt) {
642 PrintF(" 0x%08x: [top + %d] <- 0x%08x ; constructor function\n",
643 top_address + output_offset, output_offset, value);
644 }
645
646 // The newly allocated object was passed as receiver in the artificial
647 // constructor stub environment created by HEnvironment::CopyForInlining().
648 output_offset -= kPointerSize;
649 value = output_frame->GetFrameSlot(output_frame_size - kPointerSize);
650 output_frame->SetFrameSlot(output_offset, value);
651 if (FLAG_trace_deopt) {
652 PrintF(" 0x%08x: [top + %d] <- 0x%08x ; allocated receiver\n",
653 top_address + output_offset, output_offset, value);
654 }
655
656 ASSERT(0 == output_offset);
657
ulan@chromium.org812308e2012-02-29 15:58:45 +0000658 uint32_t pc = reinterpret_cast<uint32_t>(
659 construct_stub->instruction_start() +
660 isolate_->heap()->construct_stub_deopt_pc_offset()->value());
661 output_frame->SetPc(pc);
662}
663
664
mstarzinger@chromium.orgde886792012-09-11 13:22:37 +0000665void Deoptimizer::DoComputeAccessorStubFrame(TranslationIterator* iterator,
666 int frame_index,
667 bool is_setter_stub_frame) {
668 JSFunction* accessor = JSFunction::cast(ComputeLiteral(iterator->Next()));
669 // The receiver (and the implicit return value, if any) are expected in
670 // registers by the LoadIC/StoreIC, so they don't belong to the output stack
671 // frame. This means that we have to use a height of 0.
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000672 unsigned height = 0;
673 unsigned height_in_bytes = height * kPointerSize;
mstarzinger@chromium.orgde886792012-09-11 13:22:37 +0000674 const char* kind = is_setter_stub_frame ? "setter" : "getter";
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000675 if (FLAG_trace_deopt) {
mstarzinger@chromium.orgde886792012-09-11 13:22:37 +0000676 PrintF(" translating %s stub => height=%u\n", kind, height_in_bytes);
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000677 }
678
679 // We need 5 stack entries from StackFrame::INTERNAL (ra, fp, cp, frame type,
mstarzinger@chromium.orgde886792012-09-11 13:22:37 +0000680 // code object, see MacroAssembler::EnterFrame). For a setter stub frame we
681 // need one additional entry for the implicit return value, see
682 // StoreStubCompiler::CompileStoreViaSetter.
683 unsigned fixed_frame_entries = 5 + (is_setter_stub_frame ? 1 : 0);
684 unsigned fixed_frame_size = fixed_frame_entries * kPointerSize;
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000685 unsigned output_frame_size = height_in_bytes + fixed_frame_size;
686
687 // Allocate and store the output frame description.
688 FrameDescription* output_frame =
mstarzinger@chromium.orgde886792012-09-11 13:22:37 +0000689 new(output_frame_size) FrameDescription(output_frame_size, accessor);
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000690 output_frame->SetFrameType(StackFrame::INTERNAL);
691
mstarzinger@chromium.orgde886792012-09-11 13:22:37 +0000692 // A frame for an accessor stub can not be the topmost or bottommost one.
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000693 ASSERT(frame_index > 0 && frame_index < output_count_ - 1);
694 ASSERT(output_[frame_index] == NULL);
695 output_[frame_index] = output_frame;
696
697 // The top address of the frame is computed from the previous frame's top and
698 // this frame's size.
699 uint32_t top_address = output_[frame_index - 1]->GetTop() - output_frame_size;
700 output_frame->SetTop(top_address);
701
702 unsigned output_offset = output_frame_size;
703
704 // Read caller's PC from the previous frame.
705 output_offset -= kPointerSize;
706 intptr_t value = output_[frame_index - 1]->GetPc();
707 output_frame->SetFrameSlot(output_offset, value);
708 if (FLAG_trace_deopt) {
709 PrintF(" 0x%08" V8PRIxPTR ": [top + %u] <- 0x%08" V8PRIxPTR
710 " ; caller's pc\n",
711 top_address + output_offset, output_offset, value);
712 }
713
714 // Read caller's FP from the previous frame, and set this frame's FP.
715 output_offset -= kPointerSize;
716 value = output_[frame_index - 1]->GetFp();
717 output_frame->SetFrameSlot(output_offset, value);
718 intptr_t fp_value = top_address + output_offset;
719 output_frame->SetFp(fp_value);
720 if (FLAG_trace_deopt) {
721 PrintF(" 0x%08" V8PRIxPTR ": [top + %u] <- 0x%08" V8PRIxPTR
722 " ; caller's fp\n",
723 fp_value, output_offset, value);
724 }
725
726 // The context can be gotten from the previous frame.
727 output_offset -= kPointerSize;
728 value = output_[frame_index - 1]->GetContext();
729 output_frame->SetFrameSlot(output_offset, value);
730 if (FLAG_trace_deopt) {
731 PrintF(" 0x%08" V8PRIxPTR ": [top + %u] <- 0x%08" V8PRIxPTR
732 " ; context\n",
733 top_address + output_offset, output_offset, value);
734 }
735
736 // A marker value is used in place of the function.
737 output_offset -= kPointerSize;
738 value = reinterpret_cast<intptr_t>(Smi::FromInt(StackFrame::INTERNAL));
739 output_frame->SetFrameSlot(output_offset, value);
740 if (FLAG_trace_deopt) {
741 PrintF(" 0x%08" V8PRIxPTR ": [top + %u] <- 0x%08" V8PRIxPTR
mstarzinger@chromium.orgde886792012-09-11 13:22:37 +0000742 " ; function (%s sentinel)\n",
743 top_address + output_offset, output_offset, value, kind);
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000744 }
745
mstarzinger@chromium.orgde886792012-09-11 13:22:37 +0000746 // Get Code object from accessor stub.
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000747 output_offset -= kPointerSize;
mstarzinger@chromium.orgde886792012-09-11 13:22:37 +0000748 Builtins::Name name = is_setter_stub_frame ?
749 Builtins::kStoreIC_Setter_ForDeopt :
750 Builtins::kLoadIC_Getter_ForDeopt;
751 Code* accessor_stub = isolate_->builtins()->builtin(name);
752 value = reinterpret_cast<intptr_t>(accessor_stub);
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000753 output_frame->SetFrameSlot(output_offset, value);
754 if (FLAG_trace_deopt) {
755 PrintF(" 0x%08" V8PRIxPTR ": [top + %u] <- 0x%08" V8PRIxPTR
756 " ; code object\n",
757 top_address + output_offset, output_offset, value);
758 }
759
760 // Skip receiver.
761 Translation::Opcode opcode =
762 static_cast<Translation::Opcode>(iterator->Next());
763 iterator->Skip(Translation::NumberOfOperandsFor(opcode));
764
mstarzinger@chromium.orgde886792012-09-11 13:22:37 +0000765 if (is_setter_stub_frame) {
766 // The implicit return value was part of the artificial setter stub
767 // environment.
768 output_offset -= kPointerSize;
769 DoTranslateCommand(iterator, frame_index, output_offset);
770 }
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000771
772 ASSERT(0 == output_offset);
773
mstarzinger@chromium.orgde886792012-09-11 13:22:37 +0000774 Smi* offset = is_setter_stub_frame ?
775 isolate_->heap()->setter_stub_deopt_pc_offset() :
776 isolate_->heap()->getter_stub_deopt_pc_offset();
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000777 intptr_t pc = reinterpret_cast<intptr_t>(
mstarzinger@chromium.orgde886792012-09-11 13:22:37 +0000778 accessor_stub->instruction_start() + offset->value());
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000779 output_frame->SetPc(pc);
780}
781
782
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000783// This code is very similar to ia32/arm code, but relies on register names
784// (fp, sp) and how the frame is laid out.
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000785void Deoptimizer::DoComputeJSFrame(TranslationIterator* iterator,
786 int frame_index) {
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000787 // Read the ast node id, function, and frame height for this output frame.
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +0000788 BailoutId node_id = BailoutId(iterator->Next());
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000789 JSFunction* function;
790 if (frame_index != 0) {
791 function = JSFunction::cast(ComputeLiteral(iterator->Next()));
792 } else {
793 int closure_id = iterator->Next();
794 USE(closure_id);
795 ASSERT_EQ(Translation::kSelfLiteralId, closure_id);
796 function = function_;
797 }
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000798 unsigned height = iterator->Next();
799 unsigned height_in_bytes = height * kPointerSize;
800 if (FLAG_trace_deopt) {
801 PrintF(" translating ");
802 function->PrintName();
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +0000803 PrintF(" => node=%d, height=%d\n", node_id.ToInt(), height_in_bytes);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000804 }
lrn@chromium.org7516f052011-03-30 08:52:27 +0000805
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000806 // The 'fixed' part of the frame consists of the incoming parameters and
807 // the part described by JavaScriptFrameConstants.
808 unsigned fixed_frame_size = ComputeFixedSize(function);
809 unsigned input_frame_size = input_->GetFrameSize();
810 unsigned output_frame_size = height_in_bytes + fixed_frame_size;
811
812 // Allocate and store the output frame description.
813 FrameDescription* output_frame =
814 new(output_frame_size) FrameDescription(output_frame_size, function);
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000815 output_frame->SetFrameType(StackFrame::JAVA_SCRIPT);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000816
817 bool is_bottommost = (0 == frame_index);
818 bool is_topmost = (output_count_ - 1 == frame_index);
819 ASSERT(frame_index >= 0 && frame_index < output_count_);
820 ASSERT(output_[frame_index] == NULL);
821 output_[frame_index] = output_frame;
822
823 // The top address for the bottommost output frame can be computed from
824 // the input frame pointer and the output frame's height. For all
825 // subsequent output frames, it can be computed from the previous one's
826 // top address and the current frame's size.
827 uint32_t top_address;
828 if (is_bottommost) {
829 // 2 = context and function in the frame.
830 top_address =
831 input_->GetRegister(fp.code()) - (2 * kPointerSize) - height_in_bytes;
832 } else {
833 top_address = output_[frame_index - 1]->GetTop() - output_frame_size;
834 }
835 output_frame->SetTop(top_address);
836
837 // Compute the incoming parameter translation.
838 int parameter_count = function->shared()->formal_parameter_count() + 1;
839 unsigned output_offset = output_frame_size;
840 unsigned input_offset = input_frame_size;
841 for (int i = 0; i < parameter_count; ++i) {
842 output_offset -= kPointerSize;
843 DoTranslateCommand(iterator, frame_index, output_offset);
844 }
845 input_offset -= (parameter_count * kPointerSize);
846
847 // There are no translation commands for the caller's pc and fp, the
848 // context, and the function. Synthesize their values and set them up
849 // explicitly.
850 //
851 // The caller's pc for the bottommost output frame is the same as in the
852 // input frame. For all subsequent output frames, it can be read from the
853 // previous one. This frame's pc can be computed from the non-optimized
854 // function code and AST id of the bailout.
855 output_offset -= kPointerSize;
856 input_offset -= kPointerSize;
857 intptr_t value;
858 if (is_bottommost) {
859 value = input_->GetFrameSlot(input_offset);
860 } else {
861 value = output_[frame_index - 1]->GetPc();
862 }
863 output_frame->SetFrameSlot(output_offset, value);
864 if (FLAG_trace_deopt) {
865 PrintF(" 0x%08x: [top + %d] <- 0x%08x ; caller's pc\n",
866 top_address + output_offset, output_offset, value);
867 }
868
869 // The caller's frame pointer for the bottommost output frame is the same
870 // as in the input frame. For all subsequent output frames, it can be
871 // read from the previous one. Also compute and set this frame's frame
872 // pointer.
873 output_offset -= kPointerSize;
874 input_offset -= kPointerSize;
875 if (is_bottommost) {
876 value = input_->GetFrameSlot(input_offset);
877 } else {
878 value = output_[frame_index - 1]->GetFp();
879 }
880 output_frame->SetFrameSlot(output_offset, value);
881 intptr_t fp_value = top_address + output_offset;
882 ASSERT(!is_bottommost || input_->GetRegister(fp.code()) == fp_value);
883 output_frame->SetFp(fp_value);
884 if (is_topmost) {
885 output_frame->SetRegister(fp.code(), fp_value);
886 }
887 if (FLAG_trace_deopt) {
888 PrintF(" 0x%08x: [top + %d] <- 0x%08x ; caller's fp\n",
889 fp_value, output_offset, value);
890 }
891
892 // For the bottommost output frame the context can be gotten from the input
893 // frame. For all subsequent output frames it can be gotten from the function
894 // so long as we don't inline functions that need local contexts.
895 output_offset -= kPointerSize;
896 input_offset -= kPointerSize;
897 if (is_bottommost) {
898 value = input_->GetFrameSlot(input_offset);
899 } else {
900 value = reinterpret_cast<intptr_t>(function->context());
901 }
902 output_frame->SetFrameSlot(output_offset, value);
ulan@chromium.org812308e2012-02-29 15:58:45 +0000903 output_frame->SetContext(value);
904 if (is_topmost) output_frame->SetRegister(cp.code(), value);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000905 if (FLAG_trace_deopt) {
906 PrintF(" 0x%08x: [top + %d] <- 0x%08x ; context\n",
907 top_address + output_offset, output_offset, value);
908 }
909
910 // The function was mentioned explicitly in the BEGIN_FRAME.
911 output_offset -= kPointerSize;
912 input_offset -= kPointerSize;
913 value = reinterpret_cast<uint32_t>(function);
914 // The function for the bottommost output frame should also agree with the
915 // input frame.
916 ASSERT(!is_bottommost || input_->GetFrameSlot(input_offset) == value);
917 output_frame->SetFrameSlot(output_offset, value);
918 if (FLAG_trace_deopt) {
919 PrintF(" 0x%08x: [top + %d] <- 0x%08x ; function\n",
920 top_address + output_offset, output_offset, value);
921 }
922
923 // Translate the rest of the frame.
924 for (unsigned i = 0; i < height; ++i) {
925 output_offset -= kPointerSize;
926 DoTranslateCommand(iterator, frame_index, output_offset);
927 }
928 ASSERT(0 == output_offset);
929
930 // Compute this frame's PC, state, and continuation.
931 Code* non_optimized_code = function->shared()->code();
932 FixedArray* raw_data = non_optimized_code->deoptimization_data();
933 DeoptimizationOutputData* data = DeoptimizationOutputData::cast(raw_data);
934 Address start = non_optimized_code->instruction_start();
935 unsigned pc_and_state = GetOutputInfo(data, node_id, function->shared());
936 unsigned pc_offset = FullCodeGenerator::PcField::decode(pc_and_state);
937 uint32_t pc_value = reinterpret_cast<uint32_t>(start + pc_offset);
938 output_frame->SetPc(pc_value);
939
940 FullCodeGenerator::State state =
941 FullCodeGenerator::StateField::decode(pc_and_state);
942 output_frame->SetState(Smi::FromInt(state));
943
944
945 // Set the continuation for the topmost frame.
946 if (is_topmost && bailout_type_ != DEBUGGER) {
947 Builtins* builtins = isolate_->builtins();
948 Code* continuation = (bailout_type_ == EAGER)
949 ? builtins->builtin(Builtins::kNotifyDeoptimized)
950 : builtins->builtin(Builtins::kNotifyLazyDeoptimized);
951 output_frame->SetContinuation(
952 reinterpret_cast<uint32_t>(continuation->entry()));
953 }
954}
lrn@chromium.org7516f052011-03-30 08:52:27 +0000955
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000956void Deoptimizer::FillInputFrame(Address tos, JavaScriptFrame* frame) {
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000957 // Set the register values. The values are not important as there are no
958 // callee saved registers in JavaScript frames, so all registers are
959 // spilled. Registers fp and sp are set to the correct values though.
960
961 for (int i = 0; i < Register::kNumRegisters; i++) {
962 input_->SetRegister(i, i * 4);
963 }
964 input_->SetRegister(sp.code(), reinterpret_cast<intptr_t>(frame->sp()));
965 input_->SetRegister(fp.code(), reinterpret_cast<intptr_t>(frame->fp()));
jkummerow@chromium.org59297c72013-01-09 16:32:23 +0000966 for (int i = 0; i < DoubleRegister::NumAllocatableRegisters(); i++) {
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000967 input_->SetDoubleRegister(i, 0.0);
968 }
969
970 // Fill the frame content from the actual data on the frame.
971 for (unsigned i = 0; i < input_->GetFrameSize(); i += kPointerSize) {
972 input_->SetFrameSlot(i, Memory::uint32_at(tos + i));
973 }
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000974}
975
976
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000977#define __ masm()->
978
979
980// This code tries to be close to ia32 code so that any changes can be
981// easily ported.
lrn@chromium.org7516f052011-03-30 08:52:27 +0000982void Deoptimizer::EntryGenerator::Generate() {
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000983 GeneratePrologue();
984
985 Isolate* isolate = masm()->isolate();
986
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000987 // Unlike on ARM we don't save all the registers, just the useful ones.
988 // For the rest, there are gaps on the stack, so the offsets remain the same.
989 const int kNumberOfRegisters = Register::kNumRegisters;
990
991 RegList restored_regs = kJSCallerSaved | kCalleeSaved;
992 RegList saved_regs = restored_regs | sp.bit() | ra.bit();
993
994 const int kDoubleRegsSize =
jkummerow@chromium.org59297c72013-01-09 16:32:23 +0000995 kDoubleSize * FPURegister::kMaxNumAllocatableRegisters;
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000996
jkummerow@chromium.org59297c72013-01-09 16:32:23 +0000997 if (CpuFeatures::IsSupported(FPU)) {
998 CpuFeatures::Scope scope(FPU);
999 // Save all FPU registers before messing with them.
1000 __ Subu(sp, sp, Operand(kDoubleRegsSize));
1001 for (int i = 0; i < FPURegister::kMaxNumAllocatableRegisters; ++i) {
1002 FPURegister fpu_reg = FPURegister::FromAllocationIndex(i);
1003 int offset = i * kDoubleSize;
1004 __ sdc1(fpu_reg, MemOperand(sp, offset));
1005 }
1006 } else {
1007 __ Subu(sp, sp, Operand(kDoubleRegsSize));
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001008 }
1009
1010 // Push saved_regs (needed to populate FrameDescription::registers_).
1011 // Leave gaps for other registers.
1012 __ Subu(sp, sp, kNumberOfRegisters * kPointerSize);
1013 for (int16_t i = kNumberOfRegisters - 1; i >= 0; i--) {
1014 if ((saved_regs & (1 << i)) != 0) {
1015 __ sw(ToRegister(i), MemOperand(sp, kPointerSize * i));
1016 }
1017 }
1018
1019 const int kSavedRegistersAreaSize =
1020 (kNumberOfRegisters * kPointerSize) + kDoubleRegsSize;
1021
1022 // Get the bailout id from the stack.
1023 __ lw(a2, MemOperand(sp, kSavedRegistersAreaSize));
1024
1025 // Get the address of the location in the code object if possible (a3) (return
1026 // address for lazy deoptimization) and compute the fp-to-sp delta in
1027 // register t0.
1028 if (type() == EAGER) {
1029 __ mov(a3, zero_reg);
1030 // Correct one word for bailout id.
1031 __ Addu(t0, sp, Operand(kSavedRegistersAreaSize + (1 * kPointerSize)));
1032 } else if (type() == OSR) {
1033 __ mov(a3, ra);
1034 // Correct one word for bailout id.
1035 __ Addu(t0, sp, Operand(kSavedRegistersAreaSize + (1 * kPointerSize)));
1036 } else {
1037 __ mov(a3, ra);
1038 // Correct two words for bailout id and return address.
1039 __ Addu(t0, sp, Operand(kSavedRegistersAreaSize + (2 * kPointerSize)));
1040 }
1041
1042 __ Subu(t0, fp, t0);
1043
1044 // Allocate a new deoptimizer object.
1045 // Pass four arguments in a0 to a3 and fifth & sixth arguments on stack.
1046 __ PrepareCallCFunction(6, t1);
1047 __ lw(a0, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
1048 __ li(a1, Operand(type())); // bailout type,
1049 // a2: bailout id already loaded.
1050 // a3: code address or 0 already loaded.
1051 __ sw(t0, CFunctionArgumentOperand(5)); // Fp-to-sp delta.
1052 __ li(t1, Operand(ExternalReference::isolate_address()));
1053 __ sw(t1, CFunctionArgumentOperand(6)); // Isolate.
1054 // Call Deoptimizer::New().
1055 {
1056 AllowExternalCallThatCantCauseGC scope(masm());
1057 __ CallCFunction(ExternalReference::new_deoptimizer_function(isolate), 6);
1058 }
1059
1060 // Preserve "deoptimizer" object in register v0 and get the input
1061 // frame descriptor pointer to a1 (deoptimizer->input_);
1062 // Move deopt-obj to a0 for call to Deoptimizer::ComputeOutputFrames() below.
1063 __ mov(a0, v0);
1064 __ lw(a1, MemOperand(v0, Deoptimizer::input_offset()));
1065
1066 // Copy core registers into FrameDescription::registers_[kNumRegisters].
1067 ASSERT(Register::kNumRegisters == kNumberOfRegisters);
1068 for (int i = 0; i < kNumberOfRegisters; i++) {
1069 int offset = (i * kPointerSize) + FrameDescription::registers_offset();
1070 if ((saved_regs & (1 << i)) != 0) {
1071 __ lw(a2, MemOperand(sp, i * kPointerSize));
1072 __ sw(a2, MemOperand(a1, offset));
1073 } else if (FLAG_debug_code) {
1074 __ li(a2, kDebugZapValue);
1075 __ sw(a2, MemOperand(a1, offset));
1076 }
1077 }
1078
jkummerow@chromium.org59297c72013-01-09 16:32:23 +00001079 if (CpuFeatures::IsSupported(FPU)) {
1080 CpuFeatures::Scope scope(FPU);
1081 // Copy FPU registers to
1082 // double_registers_[DoubleRegister::kNumAllocatableRegisters]
1083 int double_regs_offset = FrameDescription::double_registers_offset();
1084 for (int i = 0; i < FPURegister::NumAllocatableRegisters(); ++i) {
1085 int dst_offset = i * kDoubleSize + double_regs_offset;
1086 int src_offset = i * kDoubleSize + kNumberOfRegisters * kPointerSize;
1087 __ ldc1(f0, MemOperand(sp, src_offset));
1088 __ sdc1(f0, MemOperand(a1, dst_offset));
1089 }
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001090 }
1091
1092 // Remove the bailout id, eventually return address, and the saved registers
1093 // from the stack.
1094 if (type() == EAGER || type() == OSR) {
1095 __ Addu(sp, sp, Operand(kSavedRegistersAreaSize + (1 * kPointerSize)));
1096 } else {
1097 __ Addu(sp, sp, Operand(kSavedRegistersAreaSize + (2 * kPointerSize)));
1098 }
1099
1100 // Compute a pointer to the unwinding limit in register a2; that is
1101 // the first stack slot not part of the input frame.
1102 __ lw(a2, MemOperand(a1, FrameDescription::frame_size_offset()));
1103 __ Addu(a2, a2, sp);
1104
1105 // Unwind the stack down to - but not including - the unwinding
1106 // limit and copy the contents of the activation frame to the input
1107 // frame description.
1108 __ Addu(a3, a1, Operand(FrameDescription::frame_content_offset()));
1109 Label pop_loop;
jkummerow@chromium.org59297c72013-01-09 16:32:23 +00001110 Label pop_loop_header;
1111 __ Branch(&pop_loop_header);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001112 __ bind(&pop_loop);
1113 __ pop(t0);
1114 __ sw(t0, MemOperand(a3, 0));
jkummerow@chromium.org59297c72013-01-09 16:32:23 +00001115 __ addiu(a3, a3, sizeof(uint32_t));
1116 __ bind(&pop_loop_header);
1117 __ Branch(&pop_loop, ne, a2, Operand(sp));
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001118
1119 // Compute the output frame in the deoptimizer.
1120 __ push(a0); // Preserve deoptimizer object across call.
1121 // a0: deoptimizer object; a1: scratch.
1122 __ PrepareCallCFunction(1, a1);
1123 // Call Deoptimizer::ComputeOutputFrames().
1124 {
1125 AllowExternalCallThatCantCauseGC scope(masm());
1126 __ CallCFunction(
1127 ExternalReference::compute_output_frames_function(isolate), 1);
1128 }
1129 __ pop(a0); // Restore deoptimizer object (class Deoptimizer).
1130
1131 // Replace the current (input) frame with the output frames.
jkummerow@chromium.org59297c72013-01-09 16:32:23 +00001132 Label outer_push_loop, inner_push_loop,
1133 outer_loop_header, inner_loop_header;
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001134 // Outer loop state: a0 = current "FrameDescription** output_",
1135 // a1 = one past the last FrameDescription**.
1136 __ lw(a1, MemOperand(a0, Deoptimizer::output_count_offset()));
1137 __ lw(a0, MemOperand(a0, Deoptimizer::output_offset())); // a0 is output_.
1138 __ sll(a1, a1, kPointerSizeLog2); // Count to offset.
1139 __ addu(a1, a0, a1); // a1 = one past the last FrameDescription**.
jkummerow@chromium.org59297c72013-01-09 16:32:23 +00001140 __ jmp(&outer_loop_header);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001141 __ bind(&outer_push_loop);
1142 // Inner loop state: a2 = current FrameDescription*, a3 = loop index.
1143 __ lw(a2, MemOperand(a0, 0)); // output_[ix]
1144 __ lw(a3, MemOperand(a2, FrameDescription::frame_size_offset()));
jkummerow@chromium.org59297c72013-01-09 16:32:23 +00001145 __ jmp(&inner_loop_header);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001146 __ bind(&inner_push_loop);
1147 __ Subu(a3, a3, Operand(sizeof(uint32_t)));
1148 __ Addu(t2, a2, Operand(a3));
1149 __ lw(t3, MemOperand(t2, FrameDescription::frame_content_offset()));
1150 __ push(t3);
jkummerow@chromium.org59297c72013-01-09 16:32:23 +00001151 __ bind(&inner_loop_header);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001152 __ Branch(&inner_push_loop, ne, a3, Operand(zero_reg));
1153
1154 __ Addu(a0, a0, Operand(kPointerSize));
jkummerow@chromium.org59297c72013-01-09 16:32:23 +00001155 __ bind(&outer_loop_header);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001156 __ Branch(&outer_push_loop, lt, a0, Operand(a1));
1157
1158
1159 // Push state, pc, and continuation from the last output frame.
1160 if (type() != OSR) {
1161 __ lw(t2, MemOperand(a2, FrameDescription::state_offset()));
1162 __ push(t2);
1163 }
1164
1165 __ lw(t2, MemOperand(a2, FrameDescription::pc_offset()));
1166 __ push(t2);
1167 __ lw(t2, MemOperand(a2, FrameDescription::continuation_offset()));
1168 __ push(t2);
1169
1170
1171 // Technically restoring 'at' should work unless zero_reg is also restored
1172 // but it's safer to check for this.
1173 ASSERT(!(at.bit() & restored_regs));
1174 // Restore the registers from the last output frame.
1175 __ mov(at, a2);
1176 for (int i = kNumberOfRegisters - 1; i >= 0; i--) {
1177 int offset = (i * kPointerSize) + FrameDescription::registers_offset();
1178 if ((restored_regs & (1 << i)) != 0) {
1179 __ lw(ToRegister(i), MemOperand(at, offset));
1180 }
1181 }
1182
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001183 __ InitializeRootRegister();
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001184
1185 __ pop(at); // Get continuation, leave pc on stack.
1186 __ pop(ra);
1187 __ Jump(at);
1188 __ stop("Unreachable.");
lrn@chromium.org7516f052011-03-30 08:52:27 +00001189}
1190
1191
yangguo@chromium.org56454712012-02-16 15:33:53 +00001192// Maximum size of a table entry generated below.
erik.corry@gmail.combbceb572012-03-09 10:52:05 +00001193const int Deoptimizer::table_entry_size_ = 9 * Assembler::kInstrSize;
yangguo@chromium.org56454712012-02-16 15:33:53 +00001194
lrn@chromium.org7516f052011-03-30 08:52:27 +00001195void Deoptimizer::TableEntryGenerator::GeneratePrologue() {
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001196 Assembler::BlockTrampolinePoolScope block_trampoline_pool(masm());
1197
1198 // Create a sequence of deoptimization entries. Note that any
1199 // registers may be still live.
yangguo@chromium.org56454712012-02-16 15:33:53 +00001200 Label table_start;
1201 __ bind(&table_start);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001202 for (int i = 0; i < count(); i++) {
yangguo@chromium.org56454712012-02-16 15:33:53 +00001203 Label start;
1204 __ bind(&start);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001205 if (type() != EAGER) {
1206 // Emulate ia32 like call by pushing return address to stack.
yangguo@chromium.org56454712012-02-16 15:33:53 +00001207 __ addiu(sp, sp, -2 * kPointerSize);
erik.corry@gmail.combbceb572012-03-09 10:52:05 +00001208 __ sw(ra, MemOperand(sp, 1 * kPointerSize));
1209 } else {
1210 __ addiu(sp, sp, -1 * kPointerSize);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001211 }
yangguo@chromium.org56454712012-02-16 15:33:53 +00001212 // Jump over the remaining deopt entries (including this one).
erik.corry@gmail.combbceb572012-03-09 10:52:05 +00001213 // This code is always reached by calling Jump, which puts the target (label
1214 // start) into t9.
yangguo@chromium.org56454712012-02-16 15:33:53 +00001215 const int remaining_entries = (count() - i) * table_entry_size_;
erik.corry@gmail.combbceb572012-03-09 10:52:05 +00001216 __ Addu(t9, t9, remaining_entries);
1217 // 'at' was clobbered so we can only load the current entry value here.
1218 __ li(at, i);
1219 __ jr(t9); // Expose delay slot.
1220 __ sw(at, MemOperand(sp, 0 * kPointerSize)); // In the delay slot.
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001221
1222 // Pad the rest of the code.
yangguo@chromium.org56454712012-02-16 15:33:53 +00001223 while (table_entry_size_ > (masm()->SizeOfCodeGeneratedSince(&start))) {
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001224 __ nop();
1225 }
1226
yangguo@chromium.org56454712012-02-16 15:33:53 +00001227 ASSERT_EQ(table_entry_size_, masm()->SizeOfCodeGeneratedSince(&start));
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001228 }
yangguo@chromium.org56454712012-02-16 15:33:53 +00001229
1230 ASSERT_EQ(masm()->SizeOfCodeGeneratedSince(&table_start),
1231 count() * table_entry_size_);
lrn@chromium.org7516f052011-03-30 08:52:27 +00001232}
1233
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001234#undef __
1235
lrn@chromium.org7516f052011-03-30 08:52:27 +00001236
1237} } // namespace v8::internal