blob: 92d7cc1c2c287c74f3576e63e1fe825fc86d989d [file] [log] [blame]
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001// Copyright 2012 the V8 project authors. All rights reserved.
Ben Murdochb0fe1622011-05-05 13:52:32 +01002// 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
Ben Murdochb8e0da22011-05-16 14:20:40 +010030#if defined(V8_TARGET_ARCH_IA32)
31
Ben Murdochb0fe1622011-05-05 13:52:32 +010032#include "codegen.h"
33#include "deoptimizer.h"
34#include "full-codegen.h"
35#include "safepoint-table.h"
36
37namespace v8 {
38namespace internal {
39
Ben Murdoch69a99ed2011-11-30 16:03:39 +000040const int Deoptimizer::table_entry_size_ = 10;
Ben Murdochb0fe1622011-05-05 13:52:32 +010041
Steve Block1e0659c2011-05-24 12:43:12 +010042
43int Deoptimizer::patch_size() {
44 return Assembler::kCallInstructionLength;
45}
46
47
Steve Block44f0eee2011-05-26 01:26:41 +010048void Deoptimizer::EnsureRelocSpaceForLazyDeoptimization(Handle<Code> code) {
49 Isolate* isolate = code->GetIsolate();
50 HandleScope scope(isolate);
Ben Murdochb0fe1622011-05-05 13:52:32 +010051
Steve Block44f0eee2011-05-26 01:26:41 +010052 // Compute the size of relocation information needed for the code
53 // patching in Deoptimizer::DeoptimizeFunction.
54 int min_reloc_size = 0;
Ben Murdoch2b4ba112012-01-20 14:57:15 +000055 int prev_pc_offset = 0;
56 DeoptimizationInputData* deopt_data =
57 DeoptimizationInputData::cast(code->deoptimization_data());
58 for (int i = 0; i < deopt_data->DeoptCount(); i++) {
59 int pc_offset = deopt_data->Pc(i)->value();
60 if (pc_offset == -1) continue;
61 ASSERT_GE(pc_offset, prev_pc_offset);
62 int pc_delta = pc_offset - prev_pc_offset;
63 // We use RUNTIME_ENTRY reloc info which has a size of 2 bytes
64 // if encodable with small pc delta encoding and up to 6 bytes
65 // otherwise.
66 if (pc_delta <= RelocInfo::kMaxSmallPCDelta) {
67 min_reloc_size += 2;
68 } else {
69 min_reloc_size += 6;
Steve Block44f0eee2011-05-26 01:26:41 +010070 }
Ben Murdoch2b4ba112012-01-20 14:57:15 +000071 prev_pc_offset = pc_offset;
Steve Block44f0eee2011-05-26 01:26:41 +010072 }
73
74 // If the relocation information is not big enough we create a new
75 // relocation info object that is padded with comments to make it
76 // big enough for lazy doptimization.
77 int reloc_length = code->relocation_info()->length();
78 if (min_reloc_size > reloc_length) {
79 int comment_reloc_size = RelocInfo::kMinRelocCommentSize;
80 // Padding needed.
81 int min_padding = min_reloc_size - reloc_length;
82 // Number of comments needed to take up at least that much space.
83 int additional_comments =
84 (min_padding + comment_reloc_size - 1) / comment_reloc_size;
85 // Actual padding size.
86 int padding = additional_comments * comment_reloc_size;
87 // Allocate new relocation info and copy old relocation to the end
88 // of the new relocation info array because relocation info is
89 // written and read backwards.
90 Factory* factory = isolate->factory();
91 Handle<ByteArray> new_reloc =
92 factory->NewByteArray(reloc_length + padding, TENURED);
93 memcpy(new_reloc->GetDataStartAddress() + padding,
94 code->relocation_info()->GetDataStartAddress(),
95 reloc_length);
96 // Create a relocation writer to write the comments in the padding
97 // space. Use position 0 for everything to ensure short encoding.
98 RelocInfoWriter reloc_info_writer(
99 new_reloc->GetDataStartAddress() + padding, 0);
100 intptr_t comment_string
101 = reinterpret_cast<intptr_t>(RelocInfo::kFillerCommentString);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100102 RelocInfo rinfo(0, RelocInfo::COMMENT, comment_string, NULL);
Steve Block44f0eee2011-05-26 01:26:41 +0100103 for (int i = 0; i < additional_comments; ++i) {
104#ifdef DEBUG
105 byte* pos_before = reloc_info_writer.pos();
106#endif
107 reloc_info_writer.Write(&rinfo);
108 ASSERT(RelocInfo::kMinRelocCommentSize ==
109 pos_before - reloc_info_writer.pos());
110 }
111 // Replace relocation information on the code object.
112 code->set_relocation_info(*new_reloc);
113 }
114}
115
116
117void Deoptimizer::DeoptimizeFunction(JSFunction* function) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100118 if (!function->IsOptimized()) return;
119
Steve Block44f0eee2011-05-26 01:26:41 +0100120 Isolate* isolate = function->GetIsolate();
121 HandleScope scope(isolate);
122 AssertNoAllocation no_allocation;
123
Ben Murdochb0fe1622011-05-05 13:52:32 +0100124 // Get the optimized code.
125 Code* code = function->code();
Steve Block1e0659c2011-05-24 12:43:12 +0100126 Address code_start_address = code->instruction_start();
Ben Murdochb0fe1622011-05-05 13:52:32 +0100127
Steve Block1e0659c2011-05-24 12:43:12 +0100128 // We will overwrite the code's relocation info in-place. Relocation info
129 // is written backward. The relocation info is the payload of a byte
130 // array. Later on we will slide this to the start of the byte array and
131 // create a filler object in the remaining space.
132 ByteArray* reloc_info = code->relocation_info();
133 Address reloc_end_address = reloc_info->address() + reloc_info->Size();
134 RelocInfoWriter reloc_info_writer(reloc_end_address, code_start_address);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100135
Ben Murdoch2b4ba112012-01-20 14:57:15 +0000136 // For each LLazyBailout instruction insert a call to the corresponding
137 // deoptimization entry.
138
139 // Since the call is a relative encoding, write new
Steve Block1e0659c2011-05-24 12:43:12 +0100140 // reloc info. We do not need any of the existing reloc info because the
141 // existing code will not be used again (we zap it in debug builds).
Ben Murdoch2b4ba112012-01-20 14:57:15 +0000142 //
143 // Emit call to lazy deoptimization at all lazy deopt points.
144 DeoptimizationInputData* deopt_data =
145 DeoptimizationInputData::cast(code->deoptimization_data());
146#ifdef DEBUG
147 Address prev_call_address = NULL;
148#endif
149 for (int i = 0; i < deopt_data->DeoptCount(); i++) {
150 if (deopt_data->Pc(i)->value() == -1) continue;
151 // Patch lazy deoptimization entry.
152 Address call_address = code_start_address + deopt_data->Pc(i)->value();
153 CodePatcher patcher(call_address, patch_size());
154 Address deopt_entry = GetDeoptimizationEntry(i, LAZY);
155 patcher.masm()->call(deopt_entry, RelocInfo::NONE);
156 // We use RUNTIME_ENTRY for deoptimization bailouts.
157 RelocInfo rinfo(call_address + 1, // 1 after the call opcode.
158 RelocInfo::RUNTIME_ENTRY,
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100159 reinterpret_cast<intptr_t>(deopt_entry),
160 NULL);
Ben Murdoch2b4ba112012-01-20 14:57:15 +0000161 reloc_info_writer.Write(&rinfo);
162 ASSERT_GE(reloc_info_writer.pos(),
163 reloc_info->address() + ByteArray::kHeaderSize);
164 ASSERT(prev_call_address == NULL ||
165 call_address >= prev_call_address + patch_size());
166 ASSERT(call_address + patch_size() <= code->instruction_end());
167#ifdef DEBUG
168 prev_call_address = call_address;
169#endif
Ben Murdochb0fe1622011-05-05 13:52:32 +0100170 }
Steve Block1e0659c2011-05-24 12:43:12 +0100171
172 // Move the relocation info to the beginning of the byte array.
173 int new_reloc_size = reloc_end_address - reloc_info_writer.pos();
174 memmove(code->relocation_start(), reloc_info_writer.pos(), new_reloc_size);
175
176 // The relocation info is in place, update the size.
177 reloc_info->set_length(new_reloc_size);
178
179 // Handle the junk part after the new relocation info. We will create
180 // a non-live object in the extra space at the end of the former reloc info.
181 Address junk_address = reloc_info->address() + reloc_info->Size();
182 ASSERT(junk_address <= reloc_end_address);
Steve Block44f0eee2011-05-26 01:26:41 +0100183 isolate->heap()->CreateFillerObjectAt(junk_address,
184 reloc_end_address - junk_address);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100185
186 // Add the deoptimizing code to the list.
187 DeoptimizingCodeListNode* node = new DeoptimizingCodeListNode(code);
Steve Block44f0eee2011-05-26 01:26:41 +0100188 DeoptimizerData* data = isolate->deoptimizer_data();
189 node->set_next(data->deoptimizing_code_list_);
190 data->deoptimizing_code_list_ = node;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100191
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100192 // We might be in the middle of incremental marking with compaction.
193 // Tell collector to treat this code object in a special way and
194 // ignore all slots that might have been recorded on it.
195 isolate->heap()->mark_compact_collector()->InvalidateCode(code);
196
Ben Murdochb0fe1622011-05-05 13:52:32 +0100197 // Set the code for the function to non-optimized version.
198 function->ReplaceCode(function->shared()->code());
199
200 if (FLAG_trace_deopt) {
201 PrintF("[forced deoptimization: ");
202 function->PrintName();
203 PrintF(" / %x]\n", reinterpret_cast<uint32_t>(function));
204 }
205}
206
207
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100208static const byte kJnsInstruction = 0x79;
209static const byte kJnsOffset = 0x13;
210static const byte kJaeInstruction = 0x73;
211static const byte kJaeOffset = 0x07;
212static const byte kCallInstruction = 0xe8;
213static const byte kNopByteOne = 0x66;
214static const byte kNopByteTwo = 0x90;
215
216
217void Deoptimizer::PatchStackCheckCodeAt(Code* unoptimized_code,
218 Address pc_after,
Steve Block1e0659c2011-05-24 12:43:12 +0100219 Code* check_code,
220 Code* replacement_code) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100221 Address call_target_address = pc_after - kIntSize;
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100222 ASSERT_EQ(check_code->entry(),
223 Assembler::target_address_at(call_target_address));
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100224 // The stack check code matches the pattern:
225 //
226 // cmp esp, <limit>
227 // jae ok
228 // call <stack guard>
229 // test eax, <loop nesting depth>
230 // ok: ...
231 //
232 // We will patch away the branch so the code is:
233 //
234 // cmp esp, <limit> ;; Not changed
235 // nop
236 // nop
237 // call <on-stack replacment>
238 // test eax, <loop nesting depth>
239 // ok:
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100240
241 if (FLAG_count_based_interrupts) {
242 ASSERT_EQ(*(call_target_address - 3), kJnsInstruction);
243 ASSERT_EQ(*(call_target_address - 2), kJnsOffset);
244 } else {
245 ASSERT_EQ(*(call_target_address - 3), kJaeInstruction);
246 ASSERT_EQ(*(call_target_address - 2), kJaeOffset);
247 }
248 ASSERT_EQ(*(call_target_address - 1), kCallInstruction);
249 *(call_target_address - 3) = kNopByteOne;
250 *(call_target_address - 2) = kNopByteTwo;
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100251 Assembler::set_target_address_at(call_target_address,
252 replacement_code->entry());
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100253
254 unoptimized_code->GetHeap()->incremental_marking()->RecordCodeTargetPatch(
255 unoptimized_code, call_target_address, replacement_code);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100256}
257
258
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100259void Deoptimizer::RevertStackCheckCodeAt(Code* unoptimized_code,
260 Address pc_after,
Steve Block1e0659c2011-05-24 12:43:12 +0100261 Code* check_code,
262 Code* replacement_code) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100263 Address call_target_address = pc_after - kIntSize;
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100264 ASSERT_EQ(replacement_code->entry(),
265 Assembler::target_address_at(call_target_address));
266
Ben Murdoch086aeea2011-05-13 15:57:08 +0100267 // Replace the nops from patching (Deoptimizer::PatchStackCheckCode) to
268 // restore the conditional branch.
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100269 ASSERT_EQ(*(call_target_address - 3), kNopByteOne);
270 ASSERT_EQ(*(call_target_address - 2), kNopByteTwo);
271 ASSERT_EQ(*(call_target_address - 1), kCallInstruction);
272 if (FLAG_count_based_interrupts) {
273 *(call_target_address - 3) = kJnsInstruction;
274 *(call_target_address - 2) = kJnsOffset;
275 } else {
276 *(call_target_address - 3) = kJaeInstruction;
277 *(call_target_address - 2) = kJaeOffset;
278 }
Steve Block1e0659c2011-05-24 12:43:12 +0100279 Assembler::set_target_address_at(call_target_address,
280 check_code->entry());
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100281
282 check_code->GetHeap()->incremental_marking()->RecordCodeTargetPatch(
283 unoptimized_code, call_target_address, check_code);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100284}
285
286
287static int LookupBailoutId(DeoptimizationInputData* data, unsigned ast_id) {
288 ByteArray* translations = data->TranslationByteArray();
289 int length = data->DeoptCount();
290 for (int i = 0; i < length; i++) {
291 if (static_cast<unsigned>(data->AstId(i)->value()) == ast_id) {
292 TranslationIterator it(translations, data->TranslationIndex(i)->value());
293 int value = it.Next();
294 ASSERT(Translation::BEGIN == static_cast<Translation::Opcode>(value));
295 // Read the number of frames.
296 value = it.Next();
297 if (value == 1) return i;
298 }
299 }
300 UNREACHABLE();
301 return -1;
302}
303
304
305void Deoptimizer::DoComputeOsrOutputFrame() {
306 DeoptimizationInputData* data = DeoptimizationInputData::cast(
307 optimized_code_->deoptimization_data());
308 unsigned ast_id = data->OsrAstId()->value();
309 // TODO(kasperl): This should not be the bailout_id_. It should be
310 // the ast id. Confusing.
311 ASSERT(bailout_id_ == ast_id);
312
313 int bailout_id = LookupBailoutId(data, ast_id);
314 unsigned translation_index = data->TranslationIndex(bailout_id)->value();
315 ByteArray* translations = data->TranslationByteArray();
316
317 TranslationIterator iterator(translations, translation_index);
318 Translation::Opcode opcode =
319 static_cast<Translation::Opcode>(iterator.Next());
320 ASSERT(Translation::BEGIN == opcode);
321 USE(opcode);
322 int count = iterator.Next();
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100323 iterator.Next(); // Drop JS frames count.
Ben Murdochb0fe1622011-05-05 13:52:32 +0100324 ASSERT(count == 1);
325 USE(count);
326
327 opcode = static_cast<Translation::Opcode>(iterator.Next());
328 USE(opcode);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100329 ASSERT(Translation::JS_FRAME == opcode);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100330 unsigned node_id = iterator.Next();
331 USE(node_id);
332 ASSERT(node_id == ast_id);
333 JSFunction* function = JSFunction::cast(ComputeLiteral(iterator.Next()));
334 USE(function);
335 ASSERT(function == function_);
336 unsigned height = iterator.Next();
337 unsigned height_in_bytes = height * kPointerSize;
338 USE(height_in_bytes);
339
340 unsigned fixed_size = ComputeFixedSize(function_);
341 unsigned input_frame_size = input_->GetFrameSize();
342 ASSERT(fixed_size + height_in_bytes == input_frame_size);
343
344 unsigned stack_slot_size = optimized_code_->stack_slots() * kPointerSize;
345 unsigned outgoing_height = data->ArgumentsStackHeight(bailout_id)->value();
346 unsigned outgoing_size = outgoing_height * kPointerSize;
347 unsigned output_frame_size = fixed_size + stack_slot_size + outgoing_size;
348 ASSERT(outgoing_size == 0); // OSR does not happen in the middle of a call.
349
350 if (FLAG_trace_osr) {
351 PrintF("[on-stack replacement: begin 0x%08" V8PRIxPTR " ",
352 reinterpret_cast<intptr_t>(function_));
353 function_->PrintName();
354 PrintF(" => node=%u, frame=%d->%d]\n",
355 ast_id,
356 input_frame_size,
357 output_frame_size);
358 }
359
360 // There's only one output frame in the OSR case.
361 output_count_ = 1;
362 output_ = new FrameDescription*[1];
363 output_[0] = new(output_frame_size) FrameDescription(
364 output_frame_size, function_);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100365 output_[0]->SetFrameType(StackFrame::JAVA_SCRIPT);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100366
367 // Clear the incoming parameters in the optimized frame to avoid
368 // confusing the garbage collector.
369 unsigned output_offset = output_frame_size - kPointerSize;
370 int parameter_count = function_->shared()->formal_parameter_count() + 1;
371 for (int i = 0; i < parameter_count; ++i) {
372 output_[0]->SetFrameSlot(output_offset, 0);
373 output_offset -= kPointerSize;
374 }
375
376 // Translate the incoming parameters. This may overwrite some of the
377 // incoming argument slots we've just cleared.
378 int input_offset = input_frame_size - kPointerSize;
379 bool ok = true;
380 int limit = input_offset - (parameter_count * kPointerSize);
381 while (ok && input_offset > limit) {
382 ok = DoOsrTranslateCommand(&iterator, &input_offset);
383 }
384
385 // There are no translation commands for the caller's pc and fp, the
386 // context, and the function. Set them up explicitly.
Steve Block44f0eee2011-05-26 01:26:41 +0100387 for (int i = StandardFrameConstants::kCallerPCOffset;
388 ok && i >= StandardFrameConstants::kMarkerOffset;
389 i -= kPointerSize) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100390 uint32_t input_value = input_->GetFrameSlot(input_offset);
391 if (FLAG_trace_osr) {
Steve Block44f0eee2011-05-26 01:26:41 +0100392 const char* name = "UNKNOWN";
393 switch (i) {
394 case StandardFrameConstants::kCallerPCOffset:
395 name = "caller's pc";
396 break;
397 case StandardFrameConstants::kCallerFPOffset:
398 name = "fp";
399 break;
400 case StandardFrameConstants::kContextOffset:
401 name = "context";
402 break;
403 case StandardFrameConstants::kMarkerOffset:
404 name = "function";
405 break;
406 }
407 PrintF(" [esp + %d] <- 0x%08x ; [esp + %d] (fixed part - %s)\n",
Ben Murdochb0fe1622011-05-05 13:52:32 +0100408 output_offset,
409 input_value,
Steve Block44f0eee2011-05-26 01:26:41 +0100410 input_offset,
411 name);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100412 }
413 output_[0]->SetFrameSlot(output_offset, input_->GetFrameSlot(input_offset));
414 input_offset -= kPointerSize;
415 output_offset -= kPointerSize;
416 }
417
418 // Translate the rest of the frame.
419 while (ok && input_offset >= 0) {
420 ok = DoOsrTranslateCommand(&iterator, &input_offset);
421 }
422
423 // If translation of any command failed, continue using the input frame.
424 if (!ok) {
425 delete output_[0];
426 output_[0] = input_;
427 output_[0]->SetPc(reinterpret_cast<uint32_t>(from_));
428 } else {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100429 // Set up the frame pointer and the context pointer.
Ben Murdoch5d4cdbf2012-04-11 10:23:59 +0100430 output_[0]->SetRegister(ebp.code(), input_->GetRegister(ebp.code()));
Ben Murdochb0fe1622011-05-05 13:52:32 +0100431 output_[0]->SetRegister(esi.code(), input_->GetRegister(esi.code()));
432
433 unsigned pc_offset = data->OsrPcOffset()->value();
434 uint32_t pc = reinterpret_cast<uint32_t>(
435 optimized_code_->entry() + pc_offset);
436 output_[0]->SetPc(pc);
437 }
Steve Block44f0eee2011-05-26 01:26:41 +0100438 Code* continuation =
439 function->GetIsolate()->builtins()->builtin(Builtins::kNotifyOSR);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100440 output_[0]->SetContinuation(
441 reinterpret_cast<uint32_t>(continuation->entry()));
442
443 if (FLAG_trace_osr) {
444 PrintF("[on-stack replacement translation %s: 0x%08" V8PRIxPTR " ",
445 ok ? "finished" : "aborted",
446 reinterpret_cast<intptr_t>(function));
447 function->PrintName();
448 PrintF(" => pc=0x%0x]\n", output_[0]->GetPc());
449 }
450}
451
452
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100453void Deoptimizer::DoComputeArgumentsAdaptorFrame(TranslationIterator* iterator,
454 int frame_index) {
455 JSFunction* function = JSFunction::cast(ComputeLiteral(iterator->Next()));
456 unsigned height = iterator->Next();
457 unsigned height_in_bytes = height * kPointerSize;
458 if (FLAG_trace_deopt) {
459 PrintF(" translating arguments adaptor => height=%d\n", height_in_bytes);
460 }
461
462 unsigned fixed_frame_size = ArgumentsAdaptorFrameConstants::kFrameSize;
463 unsigned output_frame_size = height_in_bytes + fixed_frame_size;
464
465 // Allocate and store the output frame description.
466 FrameDescription* output_frame =
467 new(output_frame_size) FrameDescription(output_frame_size, function);
468 output_frame->SetFrameType(StackFrame::ARGUMENTS_ADAPTOR);
469
470 // Arguments adaptor can not be topmost or bottommost.
471 ASSERT(frame_index > 0 && frame_index < output_count_ - 1);
472 ASSERT(output_[frame_index] == NULL);
473 output_[frame_index] = output_frame;
474
475 // The top address of the frame is computed from the previous
476 // frame's top and this frame's size.
477 uint32_t top_address;
478 top_address = output_[frame_index - 1]->GetTop() - output_frame_size;
479 output_frame->SetTop(top_address);
480
481 // Compute the incoming parameter translation.
482 int parameter_count = height;
483 unsigned output_offset = output_frame_size;
484 for (int i = 0; i < parameter_count; ++i) {
485 output_offset -= kPointerSize;
486 DoTranslateCommand(iterator, frame_index, output_offset);
487 }
488
489 // Read caller's PC from the previous frame.
490 output_offset -= kPointerSize;
491 intptr_t callers_pc = output_[frame_index - 1]->GetPc();
492 output_frame->SetFrameSlot(output_offset, callers_pc);
493 if (FLAG_trace_deopt) {
494 PrintF(" 0x%08x: [top + %d] <- 0x%08x ; caller's pc\n",
495 top_address + output_offset, output_offset, callers_pc);
496 }
497
498 // Read caller's FP from the previous frame, and set this frame's FP.
499 output_offset -= kPointerSize;
500 intptr_t value = output_[frame_index - 1]->GetFp();
501 output_frame->SetFrameSlot(output_offset, value);
502 intptr_t fp_value = top_address + output_offset;
503 output_frame->SetFp(fp_value);
504 if (FLAG_trace_deopt) {
505 PrintF(" 0x%08x: [top + %d] <- 0x%08x ; caller's fp\n",
506 fp_value, output_offset, value);
507 }
508
509 // A marker value is used in place of the context.
510 output_offset -= kPointerSize;
511 intptr_t context = reinterpret_cast<intptr_t>(
512 Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR));
513 output_frame->SetFrameSlot(output_offset, context);
514 if (FLAG_trace_deopt) {
515 PrintF(" 0x%08x: [top + %d] <- 0x%08x ; context (adaptor sentinel)\n",
516 top_address + output_offset, output_offset, context);
517 }
518
519 // The function was mentioned explicitly in the ARGUMENTS_ADAPTOR_FRAME.
520 output_offset -= kPointerSize;
521 value = reinterpret_cast<intptr_t>(function);
522 output_frame->SetFrameSlot(output_offset, value);
523 if (FLAG_trace_deopt) {
524 PrintF(" 0x%08x: [top + %d] <- 0x%08x ; function\n",
525 top_address + output_offset, output_offset, value);
526 }
527
528 // Number of incoming arguments.
529 output_offset -= kPointerSize;
530 value = reinterpret_cast<uint32_t>(Smi::FromInt(height - 1));
531 output_frame->SetFrameSlot(output_offset, value);
532 if (FLAG_trace_deopt) {
533 PrintF(" 0x%08x: [top + %d] <- 0x%08x ; argc (%d)\n",
534 top_address + output_offset, output_offset, value, height - 1);
535 }
536
537 ASSERT(0 == output_offset);
538
539 Builtins* builtins = isolate_->builtins();
540 Code* adaptor_trampoline =
541 builtins->builtin(Builtins::kArgumentsAdaptorTrampoline);
542 uint32_t pc = reinterpret_cast<uint32_t>(
543 adaptor_trampoline->instruction_start() +
544 isolate_->heap()->arguments_adaptor_deopt_pc_offset()->value());
545 output_frame->SetPc(pc);
546}
547
548
549void Deoptimizer::DoComputeConstructStubFrame(TranslationIterator* iterator,
550 int frame_index) {
551 JSFunction* function = JSFunction::cast(ComputeLiteral(iterator->Next()));
552 unsigned height = iterator->Next();
553 unsigned height_in_bytes = height * kPointerSize;
554 if (FLAG_trace_deopt) {
555 PrintF(" translating construct stub => height=%d\n", height_in_bytes);
556 }
557
558 unsigned fixed_frame_size = 6 * kPointerSize;
559 unsigned output_frame_size = height_in_bytes + fixed_frame_size;
560
561 // Allocate and store the output frame description.
562 FrameDescription* output_frame =
563 new(output_frame_size) FrameDescription(output_frame_size, function);
564 output_frame->SetFrameType(StackFrame::CONSTRUCT);
565
566 // Construct stub can not be topmost or bottommost.
567 ASSERT(frame_index > 0 && frame_index < output_count_ - 1);
568 ASSERT(output_[frame_index] == NULL);
569 output_[frame_index] = output_frame;
570
571 // The top address of the frame is computed from the previous
572 // frame's top and this frame's size.
573 uint32_t top_address;
574 top_address = output_[frame_index - 1]->GetTop() - output_frame_size;
575 output_frame->SetTop(top_address);
576
577 // Compute the incoming parameter translation.
578 int parameter_count = height;
579 unsigned output_offset = output_frame_size;
580 for (int i = 0; i < parameter_count; ++i) {
581 output_offset -= kPointerSize;
582 DoTranslateCommand(iterator, frame_index, output_offset);
583 }
584
585 // Read caller's PC from the previous frame.
586 output_offset -= kPointerSize;
587 intptr_t callers_pc = output_[frame_index - 1]->GetPc();
588 output_frame->SetFrameSlot(output_offset, callers_pc);
589 if (FLAG_trace_deopt) {
590 PrintF(" 0x%08x: [top + %d] <- 0x%08x ; caller's pc\n",
591 top_address + output_offset, output_offset, callers_pc);
592 }
593
594 // Read caller's FP from the previous frame, and set this frame's FP.
595 output_offset -= kPointerSize;
596 intptr_t value = output_[frame_index - 1]->GetFp();
597 output_frame->SetFrameSlot(output_offset, value);
598 intptr_t fp_value = top_address + output_offset;
599 output_frame->SetFp(fp_value);
600 if (FLAG_trace_deopt) {
601 PrintF(" 0x%08x: [top + %d] <- 0x%08x ; caller's fp\n",
602 fp_value, output_offset, value);
603 }
604
605 // The context can be gotten from the previous frame.
606 output_offset -= kPointerSize;
607 value = output_[frame_index - 1]->GetContext();
608 output_frame->SetFrameSlot(output_offset, value);
609 if (FLAG_trace_deopt) {
610 PrintF(" 0x%08x: [top + %d] <- 0x%08x ; context\n",
611 top_address + output_offset, output_offset, value);
612 }
613
614 // A marker value is used in place of the function.
615 output_offset -= kPointerSize;
616 value = reinterpret_cast<intptr_t>(Smi::FromInt(StackFrame::CONSTRUCT));
617 output_frame->SetFrameSlot(output_offset, value);
618 if (FLAG_trace_deopt) {
619 PrintF(" 0x%08x: [top + %d] <- 0x%08x ; function (construct sentinel)\n",
620 top_address + output_offset, output_offset, value);
621 }
622
623 // Number of incoming arguments.
624 output_offset -= kPointerSize;
625 value = reinterpret_cast<uint32_t>(Smi::FromInt(height - 1));
626 output_frame->SetFrameSlot(output_offset, value);
627 if (FLAG_trace_deopt) {
628 PrintF(" 0x%08x: [top + %d] <- 0x%08x ; argc (%d)\n",
629 top_address + output_offset, output_offset, value, height - 1);
630 }
631
632 // The newly allocated object was passed as receiver in the artificial
633 // constructor stub environment created by HEnvironment::CopyForInlining().
634 output_offset -= kPointerSize;
635 value = output_frame->GetFrameSlot(output_frame_size - kPointerSize);
636 output_frame->SetFrameSlot(output_offset, value);
637 if (FLAG_trace_deopt) {
638 PrintF(" 0x%08x: [top + %d] <- 0x%08x ; allocated receiver\n",
639 top_address + output_offset, output_offset, value);
640 }
641
642 ASSERT(0 == output_offset);
643
644 Builtins* builtins = isolate_->builtins();
645 Code* construct_stub = builtins->builtin(Builtins::kJSConstructStubGeneric);
646 uint32_t pc = reinterpret_cast<uint32_t>(
647 construct_stub->instruction_start() +
648 isolate_->heap()->construct_stub_deopt_pc_offset()->value());
649 output_frame->SetPc(pc);
650}
651
652
653void Deoptimizer::DoComputeJSFrame(TranslationIterator* iterator,
654 int frame_index) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100655 int node_id = iterator->Next();
656 JSFunction* function = JSFunction::cast(ComputeLiteral(iterator->Next()));
657 unsigned height = iterator->Next();
658 unsigned height_in_bytes = height * kPointerSize;
659 if (FLAG_trace_deopt) {
660 PrintF(" translating ");
661 function->PrintName();
662 PrintF(" => node=%d, height=%d\n", node_id, height_in_bytes);
663 }
664
665 // The 'fixed' part of the frame consists of the incoming parameters and
666 // the part described by JavaScriptFrameConstants.
667 unsigned fixed_frame_size = ComputeFixedSize(function);
668 unsigned input_frame_size = input_->GetFrameSize();
669 unsigned output_frame_size = height_in_bytes + fixed_frame_size;
670
671 // Allocate and store the output frame description.
672 FrameDescription* output_frame =
673 new(output_frame_size) FrameDescription(output_frame_size, function);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100674 output_frame->SetFrameType(StackFrame::JAVA_SCRIPT);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100675
676 bool is_bottommost = (0 == frame_index);
677 bool is_topmost = (output_count_ - 1 == frame_index);
678 ASSERT(frame_index >= 0 && frame_index < output_count_);
679 ASSERT(output_[frame_index] == NULL);
680 output_[frame_index] = output_frame;
681
682 // The top address for the bottommost output frame can be computed from
683 // the input frame pointer and the output frame's height. For all
684 // subsequent output frames, it can be computed from the previous one's
685 // top address and the current frame's size.
686 uint32_t top_address;
687 if (is_bottommost) {
Ben Murdoch5d4cdbf2012-04-11 10:23:59 +0100688 // 2 = context and function in the frame.
689 top_address =
690 input_->GetRegister(ebp.code()) - (2 * kPointerSize) - height_in_bytes;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100691 } else {
692 top_address = output_[frame_index - 1]->GetTop() - output_frame_size;
693 }
694 output_frame->SetTop(top_address);
695
696 // Compute the incoming parameter translation.
697 int parameter_count = function->shared()->formal_parameter_count() + 1;
698 unsigned output_offset = output_frame_size;
699 unsigned input_offset = input_frame_size;
700 for (int i = 0; i < parameter_count; ++i) {
701 output_offset -= kPointerSize;
702 DoTranslateCommand(iterator, frame_index, output_offset);
703 }
704 input_offset -= (parameter_count * kPointerSize);
705
706 // There are no translation commands for the caller's pc and fp, the
707 // context, and the function. Synthesize their values and set them up
708 // explicitly.
709 //
710 // The caller's pc for the bottommost output frame is the same as in the
711 // input frame. For all subsequent output frames, it can be read from the
712 // previous one. This frame's pc can be computed from the non-optimized
713 // function code and AST id of the bailout.
714 output_offset -= kPointerSize;
715 input_offset -= kPointerSize;
716 intptr_t value;
717 if (is_bottommost) {
718 value = input_->GetFrameSlot(input_offset);
719 } else {
720 value = output_[frame_index - 1]->GetPc();
721 }
722 output_frame->SetFrameSlot(output_offset, value);
723 if (FLAG_trace_deopt) {
724 PrintF(" 0x%08x: [top + %d] <- 0x%08x ; caller's pc\n",
725 top_address + output_offset, output_offset, value);
726 }
727
728 // The caller's frame pointer for the bottommost output frame is the same
729 // as in the input frame. For all subsequent output frames, it can be
730 // read from the previous one. Also compute and set this frame's frame
731 // pointer.
732 output_offset -= kPointerSize;
733 input_offset -= kPointerSize;
734 if (is_bottommost) {
735 value = input_->GetFrameSlot(input_offset);
736 } else {
737 value = output_[frame_index - 1]->GetFp();
738 }
739 output_frame->SetFrameSlot(output_offset, value);
740 intptr_t fp_value = top_address + output_offset;
Ben Murdoch5d4cdbf2012-04-11 10:23:59 +0100741 ASSERT(!is_bottommost || input_->GetRegister(ebp.code()) == fp_value);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100742 output_frame->SetFp(fp_value);
743 if (is_topmost) output_frame->SetRegister(ebp.code(), fp_value);
744 if (FLAG_trace_deopt) {
745 PrintF(" 0x%08x: [top + %d] <- 0x%08x ; caller's fp\n",
746 fp_value, output_offset, value);
747 }
748
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100749 // For the bottommost output frame the context can be gotten from the input
750 // frame. For all subsequent output frames it can be gotten from the function
751 // so long as we don't inline functions that need local contexts.
Ben Murdochb0fe1622011-05-05 13:52:32 +0100752 output_offset -= kPointerSize;
753 input_offset -= kPointerSize;
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100754 if (is_bottommost) {
755 value = input_->GetFrameSlot(input_offset);
756 } else {
757 value = reinterpret_cast<uint32_t>(function->context());
758 }
Ben Murdochb0fe1622011-05-05 13:52:32 +0100759 output_frame->SetFrameSlot(output_offset, value);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100760 output_frame->SetContext(value);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100761 if (is_topmost) output_frame->SetRegister(esi.code(), value);
762 if (FLAG_trace_deopt) {
763 PrintF(" 0x%08x: [top + %d] <- 0x%08x ; context\n",
764 top_address + output_offset, output_offset, value);
765 }
766
767 // The function was mentioned explicitly in the BEGIN_FRAME.
768 output_offset -= kPointerSize;
769 input_offset -= kPointerSize;
770 value = reinterpret_cast<uint32_t>(function);
771 // The function for the bottommost output frame should also agree with the
772 // input frame.
773 ASSERT(!is_bottommost || input_->GetFrameSlot(input_offset) == value);
774 output_frame->SetFrameSlot(output_offset, value);
775 if (FLAG_trace_deopt) {
776 PrintF(" 0x%08x: [top + %d] <- 0x%08x ; function\n",
777 top_address + output_offset, output_offset, value);
778 }
779
780 // Translate the rest of the frame.
781 for (unsigned i = 0; i < height; ++i) {
782 output_offset -= kPointerSize;
783 DoTranslateCommand(iterator, frame_index, output_offset);
784 }
785 ASSERT(0 == output_offset);
786
787 // Compute this frame's PC, state, and continuation.
788 Code* non_optimized_code = function->shared()->code();
789 FixedArray* raw_data = non_optimized_code->deoptimization_data();
790 DeoptimizationOutputData* data = DeoptimizationOutputData::cast(raw_data);
791 Address start = non_optimized_code->instruction_start();
792 unsigned pc_and_state = GetOutputInfo(data, node_id, function->shared());
793 unsigned pc_offset = FullCodeGenerator::PcField::decode(pc_and_state);
794 uint32_t pc_value = reinterpret_cast<uint32_t>(start + pc_offset);
795 output_frame->SetPc(pc_value);
796
797 FullCodeGenerator::State state =
798 FullCodeGenerator::StateField::decode(pc_and_state);
799 output_frame->SetState(Smi::FromInt(state));
800
801 // Set the continuation for the topmost frame.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000802 if (is_topmost && bailout_type_ != DEBUGGER) {
Steve Block44f0eee2011-05-26 01:26:41 +0100803 Builtins* builtins = isolate_->builtins();
Ben Murdochb0fe1622011-05-05 13:52:32 +0100804 Code* continuation = (bailout_type_ == EAGER)
Steve Block44f0eee2011-05-26 01:26:41 +0100805 ? builtins->builtin(Builtins::kNotifyDeoptimized)
806 : builtins->builtin(Builtins::kNotifyLazyDeoptimized);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100807 output_frame->SetContinuation(
808 reinterpret_cast<uint32_t>(continuation->entry()));
809 }
Ben Murdochb0fe1622011-05-05 13:52:32 +0100810}
811
812
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000813void Deoptimizer::FillInputFrame(Address tos, JavaScriptFrame* frame) {
814 // Set the register values. The values are not important as there are no
815 // callee saved registers in JavaScript frames, so all registers are
816 // spilled. Registers ebp and esp are set to the correct values though.
817
818 for (int i = 0; i < Register::kNumRegisters; i++) {
819 input_->SetRegister(i, i * 4);
820 }
821 input_->SetRegister(esp.code(), reinterpret_cast<intptr_t>(frame->sp()));
822 input_->SetRegister(ebp.code(), reinterpret_cast<intptr_t>(frame->fp()));
823 for (int i = 0; i < DoubleRegister::kNumAllocatableRegisters; i++) {
824 input_->SetDoubleRegister(i, 0.0);
825 }
826
827 // Fill the frame content from the actual data on the frame.
828 for (unsigned i = 0; i < input_->GetFrameSize(); i += kPointerSize) {
829 input_->SetFrameSlot(i, Memory::uint32_at(tos + i));
830 }
831}
832
833
Ben Murdochb0fe1622011-05-05 13:52:32 +0100834#define __ masm()->
835
836void Deoptimizer::EntryGenerator::Generate() {
837 GeneratePrologue();
838 CpuFeatures::Scope scope(SSE2);
839
Steve Block44f0eee2011-05-26 01:26:41 +0100840 Isolate* isolate = masm()->isolate();
841
Ben Murdochb0fe1622011-05-05 13:52:32 +0100842 // Save all general purpose registers before messing with them.
843 const int kNumberOfRegisters = Register::kNumRegisters;
844
845 const int kDoubleRegsSize = kDoubleSize *
846 XMMRegister::kNumAllocatableRegisters;
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100847 __ sub(esp, Immediate(kDoubleRegsSize));
Ben Murdochb0fe1622011-05-05 13:52:32 +0100848 for (int i = 0; i < XMMRegister::kNumAllocatableRegisters; ++i) {
849 XMMRegister xmm_reg = XMMRegister::FromAllocationIndex(i);
850 int offset = i * kDoubleSize;
851 __ movdbl(Operand(esp, offset), xmm_reg);
852 }
853
854 __ pushad();
855
856 const int kSavedRegistersAreaSize = kNumberOfRegisters * kPointerSize +
857 kDoubleRegsSize;
858
859 // Get the bailout id from the stack.
860 __ mov(ebx, Operand(esp, kSavedRegistersAreaSize));
861
862 // Get the address of the location in the code object if possible
863 // and compute the fp-to-sp delta in register edx.
864 if (type() == EAGER) {
865 __ Set(ecx, Immediate(0));
866 __ lea(edx, Operand(esp, kSavedRegistersAreaSize + 1 * kPointerSize));
867 } else {
868 __ mov(ecx, Operand(esp, kSavedRegistersAreaSize + 1 * kPointerSize));
869 __ lea(edx, Operand(esp, kSavedRegistersAreaSize + 2 * kPointerSize));
870 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100871 __ sub(edx, ebp);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100872 __ neg(edx);
873
874 // Allocate a new deoptimizer object.
Ben Murdoch8b112d22011-06-08 16:22:53 +0100875 __ PrepareCallCFunction(6, eax);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100876 __ mov(eax, Operand(ebp, JavaScriptFrameConstants::kFunctionOffset));
877 __ mov(Operand(esp, 0 * kPointerSize), eax); // Function.
878 __ mov(Operand(esp, 1 * kPointerSize), Immediate(type())); // Bailout type.
879 __ mov(Operand(esp, 2 * kPointerSize), ebx); // Bailout id.
880 __ mov(Operand(esp, 3 * kPointerSize), ecx); // Code address or 0.
881 __ mov(Operand(esp, 4 * kPointerSize), edx); // Fp-to-sp delta.
Ben Murdoch8b112d22011-06-08 16:22:53 +0100882 __ mov(Operand(esp, 5 * kPointerSize),
883 Immediate(ExternalReference::isolate_address()));
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100884 {
885 AllowExternalCallThatCantCauseGC scope(masm());
886 __ CallCFunction(ExternalReference::new_deoptimizer_function(isolate), 6);
887 }
Ben Murdochb0fe1622011-05-05 13:52:32 +0100888
889 // Preserve deoptimizer object in register eax and get the input
890 // frame descriptor pointer.
891 __ mov(ebx, Operand(eax, Deoptimizer::input_offset()));
892
893 // Fill in the input registers.
Steve Block1e0659c2011-05-24 12:43:12 +0100894 for (int i = kNumberOfRegisters - 1; i >= 0; i--) {
895 int offset = (i * kPointerSize) + FrameDescription::registers_offset();
896 __ pop(Operand(ebx, offset));
Ben Murdochb0fe1622011-05-05 13:52:32 +0100897 }
898
899 // Fill in the double input registers.
900 int double_regs_offset = FrameDescription::double_registers_offset();
901 for (int i = 0; i < XMMRegister::kNumAllocatableRegisters; ++i) {
902 int dst_offset = i * kDoubleSize + double_regs_offset;
Steve Block1e0659c2011-05-24 12:43:12 +0100903 int src_offset = i * kDoubleSize;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100904 __ movdbl(xmm0, Operand(esp, src_offset));
905 __ movdbl(Operand(ebx, dst_offset), xmm0);
906 }
907
Steve Block1e0659c2011-05-24 12:43:12 +0100908 // Remove the bailout id and the double registers from the stack.
Ben Murdochb0fe1622011-05-05 13:52:32 +0100909 if (type() == EAGER) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100910 __ add(esp, Immediate(kDoubleRegsSize + kPointerSize));
Ben Murdochb0fe1622011-05-05 13:52:32 +0100911 } else {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100912 __ add(esp, Immediate(kDoubleRegsSize + 2 * kPointerSize));
Ben Murdochb0fe1622011-05-05 13:52:32 +0100913 }
914
915 // Compute a pointer to the unwinding limit in register ecx; that is
916 // the first stack slot not part of the input frame.
917 __ mov(ecx, Operand(ebx, FrameDescription::frame_size_offset()));
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100918 __ add(ecx, esp);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100919
920 // Unwind the stack down to - but not including - the unwinding
921 // limit and copy the contents of the activation frame to the input
922 // frame description.
923 __ lea(edx, Operand(ebx, FrameDescription::frame_content_offset()));
924 Label pop_loop;
925 __ bind(&pop_loop);
926 __ pop(Operand(edx, 0));
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100927 __ add(edx, Immediate(sizeof(uint32_t)));
928 __ cmp(ecx, esp);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100929 __ j(not_equal, &pop_loop);
930
931 // Compute the output frame in the deoptimizer.
932 __ push(eax);
933 __ PrepareCallCFunction(1, ebx);
934 __ mov(Operand(esp, 0 * kPointerSize), eax);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100935 {
936 AllowExternalCallThatCantCauseGC scope(masm());
937 __ CallCFunction(
938 ExternalReference::compute_output_frames_function(isolate), 1);
939 }
Ben Murdochb0fe1622011-05-05 13:52:32 +0100940 __ pop(eax);
941
942 // Replace the current frame with the output frames.
943 Label outer_push_loop, inner_push_loop;
944 // Outer loop state: eax = current FrameDescription**, edx = one past the
945 // last FrameDescription**.
946 __ mov(edx, Operand(eax, Deoptimizer::output_count_offset()));
947 __ mov(eax, Operand(eax, Deoptimizer::output_offset()));
948 __ lea(edx, Operand(eax, edx, times_4, 0));
949 __ bind(&outer_push_loop);
950 // Inner loop state: ebx = current FrameDescription*, ecx = loop index.
951 __ mov(ebx, Operand(eax, 0));
952 __ mov(ecx, Operand(ebx, FrameDescription::frame_size_offset()));
953 __ bind(&inner_push_loop);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100954 __ sub(ecx, Immediate(sizeof(uint32_t)));
Ben Murdochb0fe1622011-05-05 13:52:32 +0100955 __ push(Operand(ebx, ecx, times_1, FrameDescription::frame_content_offset()));
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100956 __ test(ecx, ecx);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100957 __ j(not_zero, &inner_push_loop);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100958 __ add(eax, Immediate(kPointerSize));
959 __ cmp(eax, edx);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100960 __ j(below, &outer_push_loop);
961
962 // In case of OSR, we have to restore the XMM registers.
963 if (type() == OSR) {
964 for (int i = 0; i < XMMRegister::kNumAllocatableRegisters; ++i) {
965 XMMRegister xmm_reg = XMMRegister::FromAllocationIndex(i);
966 int src_offset = i * kDoubleSize + double_regs_offset;
967 __ movdbl(xmm_reg, Operand(ebx, src_offset));
968 }
969 }
970
971 // Push state, pc, and continuation from the last output frame.
972 if (type() != OSR) {
973 __ push(Operand(ebx, FrameDescription::state_offset()));
974 }
975 __ push(Operand(ebx, FrameDescription::pc_offset()));
976 __ push(Operand(ebx, FrameDescription::continuation_offset()));
977
978
979 // Push the registers from the last output frame.
980 for (int i = 0; i < kNumberOfRegisters; i++) {
Steve Block1e0659c2011-05-24 12:43:12 +0100981 int offset = (i * kPointerSize) + FrameDescription::registers_offset();
Ben Murdochb0fe1622011-05-05 13:52:32 +0100982 __ push(Operand(ebx, offset));
983 }
984
985 // Restore the registers from the stack.
986 __ popad();
987
988 // Return to the continuation point.
989 __ ret(0);
990}
991
992
993void Deoptimizer::TableEntryGenerator::GeneratePrologue() {
994 // Create a sequence of deoptimization entries.
995 Label done;
996 for (int i = 0; i < count(); i++) {
997 int start = masm()->pc_offset();
998 USE(start);
999 __ push_imm32(i);
1000 __ jmp(&done);
1001 ASSERT(masm()->pc_offset() - start == table_entry_size_);
1002 }
1003 __ bind(&done);
1004}
1005
1006#undef __
1007
1008
1009} } // namespace v8::internal
Ben Murdochb8e0da22011-05-16 14:20:40 +01001010
1011#endif // V8_TARGET_ARCH_IA32