blob: 080ad644dc4d518d36face2c4ac89da423c61d26 [file] [log] [blame]
Ben Murdoch086aeea2011-05-13 15:57:08 +01001// Copyright 2011 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);
102 RelocInfo rinfo(0, RelocInfo::COMMENT, comment_string);
103 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,
159 reinterpret_cast<intptr_t>(deopt_entry));
160 reloc_info_writer.Write(&rinfo);
161 ASSERT_GE(reloc_info_writer.pos(),
162 reloc_info->address() + ByteArray::kHeaderSize);
163 ASSERT(prev_call_address == NULL ||
164 call_address >= prev_call_address + patch_size());
165 ASSERT(call_address + patch_size() <= code->instruction_end());
166#ifdef DEBUG
167 prev_call_address = call_address;
168#endif
Ben Murdochb0fe1622011-05-05 13:52:32 +0100169 }
Steve Block1e0659c2011-05-24 12:43:12 +0100170
171 // Move the relocation info to the beginning of the byte array.
172 int new_reloc_size = reloc_end_address - reloc_info_writer.pos();
173 memmove(code->relocation_start(), reloc_info_writer.pos(), new_reloc_size);
174
175 // The relocation info is in place, update the size.
176 reloc_info->set_length(new_reloc_size);
177
178 // Handle the junk part after the new relocation info. We will create
179 // a non-live object in the extra space at the end of the former reloc info.
180 Address junk_address = reloc_info->address() + reloc_info->Size();
181 ASSERT(junk_address <= reloc_end_address);
Steve Block44f0eee2011-05-26 01:26:41 +0100182 isolate->heap()->CreateFillerObjectAt(junk_address,
183 reloc_end_address - junk_address);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100184
185 // Add the deoptimizing code to the list.
186 DeoptimizingCodeListNode* node = new DeoptimizingCodeListNode(code);
Steve Block44f0eee2011-05-26 01:26:41 +0100187 DeoptimizerData* data = isolate->deoptimizer_data();
188 node->set_next(data->deoptimizing_code_list_);
189 data->deoptimizing_code_list_ = node;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100190
191 // Set the code for the function to non-optimized version.
192 function->ReplaceCode(function->shared()->code());
193
194 if (FLAG_trace_deopt) {
195 PrintF("[forced deoptimization: ");
196 function->PrintName();
197 PrintF(" / %x]\n", reinterpret_cast<uint32_t>(function));
198 }
199}
200
201
Steve Block1e0659c2011-05-24 12:43:12 +0100202void Deoptimizer::PatchStackCheckCodeAt(Address pc_after,
203 Code* check_code,
204 Code* replacement_code) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100205 Address call_target_address = pc_after - kIntSize;
206 ASSERT(check_code->entry() ==
207 Assembler::target_address_at(call_target_address));
208 // The stack check code matches the pattern:
209 //
210 // cmp esp, <limit>
211 // jae ok
212 // call <stack guard>
213 // test eax, <loop nesting depth>
214 // ok: ...
215 //
216 // We will patch away the branch so the code is:
217 //
218 // cmp esp, <limit> ;; Not changed
219 // nop
220 // nop
221 // call <on-stack replacment>
222 // test eax, <loop nesting depth>
223 // ok:
224 ASSERT(*(call_target_address - 3) == 0x73 && // jae
225 *(call_target_address - 2) == 0x07 && // offset
226 *(call_target_address - 1) == 0xe8); // call
227 *(call_target_address - 3) = 0x90; // nop
228 *(call_target_address - 2) = 0x90; // nop
229 Assembler::set_target_address_at(call_target_address,
230 replacement_code->entry());
Ben Murdochb0fe1622011-05-05 13:52:32 +0100231}
232
233
Steve Block1e0659c2011-05-24 12:43:12 +0100234void Deoptimizer::RevertStackCheckCodeAt(Address pc_after,
235 Code* check_code,
236 Code* replacement_code) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100237 Address call_target_address = pc_after - kIntSize;
Steve Block1e0659c2011-05-24 12:43:12 +0100238 ASSERT(replacement_code->entry() ==
239 Assembler::target_address_at(call_target_address));
Ben Murdoch086aeea2011-05-13 15:57:08 +0100240 // Replace the nops from patching (Deoptimizer::PatchStackCheckCode) to
241 // restore the conditional branch.
Ben Murdochb0fe1622011-05-05 13:52:32 +0100242 ASSERT(*(call_target_address - 3) == 0x90 && // nop
243 *(call_target_address - 2) == 0x90 && // nop
244 *(call_target_address - 1) == 0xe8); // call
245 *(call_target_address - 3) = 0x73; // jae
Ben Murdoch086aeea2011-05-13 15:57:08 +0100246 *(call_target_address - 2) = 0x07; // offset
Steve Block1e0659c2011-05-24 12:43:12 +0100247 Assembler::set_target_address_at(call_target_address,
248 check_code->entry());
Ben Murdochb0fe1622011-05-05 13:52:32 +0100249}
250
251
252static int LookupBailoutId(DeoptimizationInputData* data, unsigned ast_id) {
253 ByteArray* translations = data->TranslationByteArray();
254 int length = data->DeoptCount();
255 for (int i = 0; i < length; i++) {
256 if (static_cast<unsigned>(data->AstId(i)->value()) == ast_id) {
257 TranslationIterator it(translations, data->TranslationIndex(i)->value());
258 int value = it.Next();
259 ASSERT(Translation::BEGIN == static_cast<Translation::Opcode>(value));
260 // Read the number of frames.
261 value = it.Next();
262 if (value == 1) return i;
263 }
264 }
265 UNREACHABLE();
266 return -1;
267}
268
269
270void Deoptimizer::DoComputeOsrOutputFrame() {
271 DeoptimizationInputData* data = DeoptimizationInputData::cast(
272 optimized_code_->deoptimization_data());
273 unsigned ast_id = data->OsrAstId()->value();
274 // TODO(kasperl): This should not be the bailout_id_. It should be
275 // the ast id. Confusing.
276 ASSERT(bailout_id_ == ast_id);
277
278 int bailout_id = LookupBailoutId(data, ast_id);
279 unsigned translation_index = data->TranslationIndex(bailout_id)->value();
280 ByteArray* translations = data->TranslationByteArray();
281
282 TranslationIterator iterator(translations, translation_index);
283 Translation::Opcode opcode =
284 static_cast<Translation::Opcode>(iterator.Next());
285 ASSERT(Translation::BEGIN == opcode);
286 USE(opcode);
287 int count = iterator.Next();
288 ASSERT(count == 1);
289 USE(count);
290
291 opcode = static_cast<Translation::Opcode>(iterator.Next());
292 USE(opcode);
293 ASSERT(Translation::FRAME == opcode);
294 unsigned node_id = iterator.Next();
295 USE(node_id);
296 ASSERT(node_id == ast_id);
297 JSFunction* function = JSFunction::cast(ComputeLiteral(iterator.Next()));
298 USE(function);
299 ASSERT(function == function_);
300 unsigned height = iterator.Next();
301 unsigned height_in_bytes = height * kPointerSize;
302 USE(height_in_bytes);
303
304 unsigned fixed_size = ComputeFixedSize(function_);
305 unsigned input_frame_size = input_->GetFrameSize();
306 ASSERT(fixed_size + height_in_bytes == input_frame_size);
307
308 unsigned stack_slot_size = optimized_code_->stack_slots() * kPointerSize;
309 unsigned outgoing_height = data->ArgumentsStackHeight(bailout_id)->value();
310 unsigned outgoing_size = outgoing_height * kPointerSize;
311 unsigned output_frame_size = fixed_size + stack_slot_size + outgoing_size;
312 ASSERT(outgoing_size == 0); // OSR does not happen in the middle of a call.
313
314 if (FLAG_trace_osr) {
315 PrintF("[on-stack replacement: begin 0x%08" V8PRIxPTR " ",
316 reinterpret_cast<intptr_t>(function_));
317 function_->PrintName();
318 PrintF(" => node=%u, frame=%d->%d]\n",
319 ast_id,
320 input_frame_size,
321 output_frame_size);
322 }
323
324 // There's only one output frame in the OSR case.
325 output_count_ = 1;
326 output_ = new FrameDescription*[1];
327 output_[0] = new(output_frame_size) FrameDescription(
328 output_frame_size, function_);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000329#ifdef DEBUG
330 output_[0]->SetKind(Code::OPTIMIZED_FUNCTION);
331#endif
Ben Murdochb0fe1622011-05-05 13:52:32 +0100332
333 // Clear the incoming parameters in the optimized frame to avoid
334 // confusing the garbage collector.
335 unsigned output_offset = output_frame_size - kPointerSize;
336 int parameter_count = function_->shared()->formal_parameter_count() + 1;
337 for (int i = 0; i < parameter_count; ++i) {
338 output_[0]->SetFrameSlot(output_offset, 0);
339 output_offset -= kPointerSize;
340 }
341
342 // Translate the incoming parameters. This may overwrite some of the
343 // incoming argument slots we've just cleared.
344 int input_offset = input_frame_size - kPointerSize;
345 bool ok = true;
346 int limit = input_offset - (parameter_count * kPointerSize);
347 while (ok && input_offset > limit) {
348 ok = DoOsrTranslateCommand(&iterator, &input_offset);
349 }
350
351 // There are no translation commands for the caller's pc and fp, the
352 // context, and the function. Set them up explicitly.
Steve Block44f0eee2011-05-26 01:26:41 +0100353 for (int i = StandardFrameConstants::kCallerPCOffset;
354 ok && i >= StandardFrameConstants::kMarkerOffset;
355 i -= kPointerSize) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100356 uint32_t input_value = input_->GetFrameSlot(input_offset);
357 if (FLAG_trace_osr) {
Steve Block44f0eee2011-05-26 01:26:41 +0100358 const char* name = "UNKNOWN";
359 switch (i) {
360 case StandardFrameConstants::kCallerPCOffset:
361 name = "caller's pc";
362 break;
363 case StandardFrameConstants::kCallerFPOffset:
364 name = "fp";
365 break;
366 case StandardFrameConstants::kContextOffset:
367 name = "context";
368 break;
369 case StandardFrameConstants::kMarkerOffset:
370 name = "function";
371 break;
372 }
373 PrintF(" [esp + %d] <- 0x%08x ; [esp + %d] (fixed part - %s)\n",
Ben Murdochb0fe1622011-05-05 13:52:32 +0100374 output_offset,
375 input_value,
Steve Block44f0eee2011-05-26 01:26:41 +0100376 input_offset,
377 name);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100378 }
379 output_[0]->SetFrameSlot(output_offset, input_->GetFrameSlot(input_offset));
380 input_offset -= kPointerSize;
381 output_offset -= kPointerSize;
382 }
383
384 // Translate the rest of the frame.
385 while (ok && input_offset >= 0) {
386 ok = DoOsrTranslateCommand(&iterator, &input_offset);
387 }
388
389 // If translation of any command failed, continue using the input frame.
390 if (!ok) {
391 delete output_[0];
392 output_[0] = input_;
393 output_[0]->SetPc(reinterpret_cast<uint32_t>(from_));
394 } else {
395 // Setup the frame pointer and the context pointer.
396 output_[0]->SetRegister(ebp.code(), input_->GetRegister(ebp.code()));
397 output_[0]->SetRegister(esi.code(), input_->GetRegister(esi.code()));
398
399 unsigned pc_offset = data->OsrPcOffset()->value();
400 uint32_t pc = reinterpret_cast<uint32_t>(
401 optimized_code_->entry() + pc_offset);
402 output_[0]->SetPc(pc);
403 }
Steve Block44f0eee2011-05-26 01:26:41 +0100404 Code* continuation =
405 function->GetIsolate()->builtins()->builtin(Builtins::kNotifyOSR);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100406 output_[0]->SetContinuation(
407 reinterpret_cast<uint32_t>(continuation->entry()));
408
409 if (FLAG_trace_osr) {
410 PrintF("[on-stack replacement translation %s: 0x%08" V8PRIxPTR " ",
411 ok ? "finished" : "aborted",
412 reinterpret_cast<intptr_t>(function));
413 function->PrintName();
414 PrintF(" => pc=0x%0x]\n", output_[0]->GetPc());
415 }
416}
417
418
419void Deoptimizer::DoComputeFrame(TranslationIterator* iterator,
420 int frame_index) {
421 // Read the ast node id, function, and frame height for this output frame.
422 Translation::Opcode opcode =
423 static_cast<Translation::Opcode>(iterator->Next());
424 USE(opcode);
425 ASSERT(Translation::FRAME == opcode);
426 int node_id = iterator->Next();
427 JSFunction* function = JSFunction::cast(ComputeLiteral(iterator->Next()));
428 unsigned height = iterator->Next();
429 unsigned height_in_bytes = height * kPointerSize;
430 if (FLAG_trace_deopt) {
431 PrintF(" translating ");
432 function->PrintName();
433 PrintF(" => node=%d, height=%d\n", node_id, height_in_bytes);
434 }
435
436 // The 'fixed' part of the frame consists of the incoming parameters and
437 // the part described by JavaScriptFrameConstants.
438 unsigned fixed_frame_size = ComputeFixedSize(function);
439 unsigned input_frame_size = input_->GetFrameSize();
440 unsigned output_frame_size = height_in_bytes + fixed_frame_size;
441
442 // Allocate and store the output frame description.
443 FrameDescription* output_frame =
444 new(output_frame_size) FrameDescription(output_frame_size, function);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000445#ifdef DEBUG
446 output_frame->SetKind(Code::FUNCTION);
447#endif
Ben Murdochb0fe1622011-05-05 13:52:32 +0100448
449 bool is_bottommost = (0 == frame_index);
450 bool is_topmost = (output_count_ - 1 == frame_index);
451 ASSERT(frame_index >= 0 && frame_index < output_count_);
452 ASSERT(output_[frame_index] == NULL);
453 output_[frame_index] = output_frame;
454
455 // The top address for the bottommost output frame can be computed from
456 // the input frame pointer and the output frame's height. For all
457 // subsequent output frames, it can be computed from the previous one's
458 // top address and the current frame's size.
459 uint32_t top_address;
460 if (is_bottommost) {
461 // 2 = context and function in the frame.
462 top_address =
463 input_->GetRegister(ebp.code()) - (2 * kPointerSize) - height_in_bytes;
464 } else {
465 top_address = output_[frame_index - 1]->GetTop() - output_frame_size;
466 }
467 output_frame->SetTop(top_address);
468
469 // Compute the incoming parameter translation.
470 int parameter_count = function->shared()->formal_parameter_count() + 1;
471 unsigned output_offset = output_frame_size;
472 unsigned input_offset = input_frame_size;
473 for (int i = 0; i < parameter_count; ++i) {
474 output_offset -= kPointerSize;
475 DoTranslateCommand(iterator, frame_index, output_offset);
476 }
477 input_offset -= (parameter_count * kPointerSize);
478
479 // There are no translation commands for the caller's pc and fp, the
480 // context, and the function. Synthesize their values and set them up
481 // explicitly.
482 //
483 // The caller's pc for the bottommost output frame is the same as in the
484 // input frame. For all subsequent output frames, it can be read from the
485 // previous one. This frame's pc can be computed from the non-optimized
486 // function code and AST id of the bailout.
487 output_offset -= kPointerSize;
488 input_offset -= kPointerSize;
489 intptr_t value;
490 if (is_bottommost) {
491 value = input_->GetFrameSlot(input_offset);
492 } else {
493 value = output_[frame_index - 1]->GetPc();
494 }
495 output_frame->SetFrameSlot(output_offset, value);
496 if (FLAG_trace_deopt) {
497 PrintF(" 0x%08x: [top + %d] <- 0x%08x ; caller's pc\n",
498 top_address + output_offset, output_offset, value);
499 }
500
501 // The caller's frame pointer for the bottommost output frame is the same
502 // as in the input frame. For all subsequent output frames, it can be
503 // read from the previous one. Also compute and set this frame's frame
504 // pointer.
505 output_offset -= kPointerSize;
506 input_offset -= kPointerSize;
507 if (is_bottommost) {
508 value = input_->GetFrameSlot(input_offset);
509 } else {
510 value = output_[frame_index - 1]->GetFp();
511 }
512 output_frame->SetFrameSlot(output_offset, value);
513 intptr_t fp_value = top_address + output_offset;
514 ASSERT(!is_bottommost || input_->GetRegister(ebp.code()) == fp_value);
515 output_frame->SetFp(fp_value);
516 if (is_topmost) output_frame->SetRegister(ebp.code(), fp_value);
517 if (FLAG_trace_deopt) {
518 PrintF(" 0x%08x: [top + %d] <- 0x%08x ; caller's fp\n",
519 fp_value, output_offset, value);
520 }
521
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100522 // For the bottommost output frame the context can be gotten from the input
523 // frame. For all subsequent output frames it can be gotten from the function
524 // so long as we don't inline functions that need local contexts.
Ben Murdochb0fe1622011-05-05 13:52:32 +0100525 output_offset -= kPointerSize;
526 input_offset -= kPointerSize;
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100527 if (is_bottommost) {
528 value = input_->GetFrameSlot(input_offset);
529 } else {
530 value = reinterpret_cast<uint32_t>(function->context());
531 }
Ben Murdochb0fe1622011-05-05 13:52:32 +0100532 output_frame->SetFrameSlot(output_offset, value);
533 if (is_topmost) output_frame->SetRegister(esi.code(), value);
534 if (FLAG_trace_deopt) {
535 PrintF(" 0x%08x: [top + %d] <- 0x%08x ; context\n",
536 top_address + output_offset, output_offset, value);
537 }
538
539 // The function was mentioned explicitly in the BEGIN_FRAME.
540 output_offset -= kPointerSize;
541 input_offset -= kPointerSize;
542 value = reinterpret_cast<uint32_t>(function);
543 // The function for the bottommost output frame should also agree with the
544 // input frame.
545 ASSERT(!is_bottommost || input_->GetFrameSlot(input_offset) == value);
546 output_frame->SetFrameSlot(output_offset, value);
547 if (FLAG_trace_deopt) {
548 PrintF(" 0x%08x: [top + %d] <- 0x%08x ; function\n",
549 top_address + output_offset, output_offset, value);
550 }
551
552 // Translate the rest of the frame.
553 for (unsigned i = 0; i < height; ++i) {
554 output_offset -= kPointerSize;
555 DoTranslateCommand(iterator, frame_index, output_offset);
556 }
557 ASSERT(0 == output_offset);
558
559 // Compute this frame's PC, state, and continuation.
560 Code* non_optimized_code = function->shared()->code();
561 FixedArray* raw_data = non_optimized_code->deoptimization_data();
562 DeoptimizationOutputData* data = DeoptimizationOutputData::cast(raw_data);
563 Address start = non_optimized_code->instruction_start();
564 unsigned pc_and_state = GetOutputInfo(data, node_id, function->shared());
565 unsigned pc_offset = FullCodeGenerator::PcField::decode(pc_and_state);
566 uint32_t pc_value = reinterpret_cast<uint32_t>(start + pc_offset);
567 output_frame->SetPc(pc_value);
568
569 FullCodeGenerator::State state =
570 FullCodeGenerator::StateField::decode(pc_and_state);
571 output_frame->SetState(Smi::FromInt(state));
572
573 // Set the continuation for the topmost frame.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000574 if (is_topmost && bailout_type_ != DEBUGGER) {
Steve Block44f0eee2011-05-26 01:26:41 +0100575 Builtins* builtins = isolate_->builtins();
Ben Murdochb0fe1622011-05-05 13:52:32 +0100576 Code* continuation = (bailout_type_ == EAGER)
Steve Block44f0eee2011-05-26 01:26:41 +0100577 ? builtins->builtin(Builtins::kNotifyDeoptimized)
578 : builtins->builtin(Builtins::kNotifyLazyDeoptimized);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100579 output_frame->SetContinuation(
580 reinterpret_cast<uint32_t>(continuation->entry()));
581 }
Ben Murdochb0fe1622011-05-05 13:52:32 +0100582}
583
584
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000585void Deoptimizer::FillInputFrame(Address tos, JavaScriptFrame* frame) {
586 // Set the register values. The values are not important as there are no
587 // callee saved registers in JavaScript frames, so all registers are
588 // spilled. Registers ebp and esp are set to the correct values though.
589
590 for (int i = 0; i < Register::kNumRegisters; i++) {
591 input_->SetRegister(i, i * 4);
592 }
593 input_->SetRegister(esp.code(), reinterpret_cast<intptr_t>(frame->sp()));
594 input_->SetRegister(ebp.code(), reinterpret_cast<intptr_t>(frame->fp()));
595 for (int i = 0; i < DoubleRegister::kNumAllocatableRegisters; i++) {
596 input_->SetDoubleRegister(i, 0.0);
597 }
598
599 // Fill the frame content from the actual data on the frame.
600 for (unsigned i = 0; i < input_->GetFrameSize(); i += kPointerSize) {
601 input_->SetFrameSlot(i, Memory::uint32_at(tos + i));
602 }
603}
604
605
Ben Murdochb0fe1622011-05-05 13:52:32 +0100606#define __ masm()->
607
608void Deoptimizer::EntryGenerator::Generate() {
609 GeneratePrologue();
610 CpuFeatures::Scope scope(SSE2);
611
Steve Block44f0eee2011-05-26 01:26:41 +0100612 Isolate* isolate = masm()->isolate();
613
Ben Murdochb0fe1622011-05-05 13:52:32 +0100614 // Save all general purpose registers before messing with them.
615 const int kNumberOfRegisters = Register::kNumRegisters;
616
617 const int kDoubleRegsSize = kDoubleSize *
618 XMMRegister::kNumAllocatableRegisters;
619 __ sub(Operand(esp), Immediate(kDoubleRegsSize));
620 for (int i = 0; i < XMMRegister::kNumAllocatableRegisters; ++i) {
621 XMMRegister xmm_reg = XMMRegister::FromAllocationIndex(i);
622 int offset = i * kDoubleSize;
623 __ movdbl(Operand(esp, offset), xmm_reg);
624 }
625
626 __ pushad();
627
628 const int kSavedRegistersAreaSize = kNumberOfRegisters * kPointerSize +
629 kDoubleRegsSize;
630
631 // Get the bailout id from the stack.
632 __ mov(ebx, Operand(esp, kSavedRegistersAreaSize));
633
634 // Get the address of the location in the code object if possible
635 // and compute the fp-to-sp delta in register edx.
636 if (type() == EAGER) {
637 __ Set(ecx, Immediate(0));
638 __ lea(edx, Operand(esp, kSavedRegistersAreaSize + 1 * kPointerSize));
639 } else {
640 __ mov(ecx, Operand(esp, kSavedRegistersAreaSize + 1 * kPointerSize));
641 __ lea(edx, Operand(esp, kSavedRegistersAreaSize + 2 * kPointerSize));
642 }
643 __ sub(edx, Operand(ebp));
644 __ neg(edx);
645
646 // Allocate a new deoptimizer object.
Ben Murdoch8b112d22011-06-08 16:22:53 +0100647 __ PrepareCallCFunction(6, eax);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100648 __ mov(eax, Operand(ebp, JavaScriptFrameConstants::kFunctionOffset));
649 __ mov(Operand(esp, 0 * kPointerSize), eax); // Function.
650 __ mov(Operand(esp, 1 * kPointerSize), Immediate(type())); // Bailout type.
651 __ mov(Operand(esp, 2 * kPointerSize), ebx); // Bailout id.
652 __ mov(Operand(esp, 3 * kPointerSize), ecx); // Code address or 0.
653 __ mov(Operand(esp, 4 * kPointerSize), edx); // Fp-to-sp delta.
Ben Murdoch8b112d22011-06-08 16:22:53 +0100654 __ mov(Operand(esp, 5 * kPointerSize),
655 Immediate(ExternalReference::isolate_address()));
656 __ CallCFunction(ExternalReference::new_deoptimizer_function(isolate), 6);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100657
658 // Preserve deoptimizer object in register eax and get the input
659 // frame descriptor pointer.
660 __ mov(ebx, Operand(eax, Deoptimizer::input_offset()));
661
662 // Fill in the input registers.
Steve Block1e0659c2011-05-24 12:43:12 +0100663 for (int i = kNumberOfRegisters - 1; i >= 0; i--) {
664 int offset = (i * kPointerSize) + FrameDescription::registers_offset();
665 __ pop(Operand(ebx, offset));
Ben Murdochb0fe1622011-05-05 13:52:32 +0100666 }
667
668 // Fill in the double input registers.
669 int double_regs_offset = FrameDescription::double_registers_offset();
670 for (int i = 0; i < XMMRegister::kNumAllocatableRegisters; ++i) {
671 int dst_offset = i * kDoubleSize + double_regs_offset;
Steve Block1e0659c2011-05-24 12:43:12 +0100672 int src_offset = i * kDoubleSize;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100673 __ movdbl(xmm0, Operand(esp, src_offset));
674 __ movdbl(Operand(ebx, dst_offset), xmm0);
675 }
676
Steve Block1e0659c2011-05-24 12:43:12 +0100677 // Remove the bailout id and the double registers from the stack.
Ben Murdochb0fe1622011-05-05 13:52:32 +0100678 if (type() == EAGER) {
Steve Block1e0659c2011-05-24 12:43:12 +0100679 __ add(Operand(esp), Immediate(kDoubleRegsSize + kPointerSize));
Ben Murdochb0fe1622011-05-05 13:52:32 +0100680 } else {
Steve Block1e0659c2011-05-24 12:43:12 +0100681 __ add(Operand(esp), Immediate(kDoubleRegsSize + 2 * kPointerSize));
Ben Murdochb0fe1622011-05-05 13:52:32 +0100682 }
683
684 // Compute a pointer to the unwinding limit in register ecx; that is
685 // the first stack slot not part of the input frame.
686 __ mov(ecx, Operand(ebx, FrameDescription::frame_size_offset()));
687 __ add(ecx, Operand(esp));
688
689 // Unwind the stack down to - but not including - the unwinding
690 // limit and copy the contents of the activation frame to the input
691 // frame description.
692 __ lea(edx, Operand(ebx, FrameDescription::frame_content_offset()));
693 Label pop_loop;
694 __ bind(&pop_loop);
695 __ pop(Operand(edx, 0));
696 __ add(Operand(edx), Immediate(sizeof(uint32_t)));
697 __ cmp(ecx, Operand(esp));
698 __ j(not_equal, &pop_loop);
699
700 // Compute the output frame in the deoptimizer.
701 __ push(eax);
702 __ PrepareCallCFunction(1, ebx);
703 __ mov(Operand(esp, 0 * kPointerSize), eax);
Steve Block44f0eee2011-05-26 01:26:41 +0100704 __ CallCFunction(
705 ExternalReference::compute_output_frames_function(isolate), 1);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100706 __ pop(eax);
707
708 // Replace the current frame with the output frames.
709 Label outer_push_loop, inner_push_loop;
710 // Outer loop state: eax = current FrameDescription**, edx = one past the
711 // last FrameDescription**.
712 __ mov(edx, Operand(eax, Deoptimizer::output_count_offset()));
713 __ mov(eax, Operand(eax, Deoptimizer::output_offset()));
714 __ lea(edx, Operand(eax, edx, times_4, 0));
715 __ bind(&outer_push_loop);
716 // Inner loop state: ebx = current FrameDescription*, ecx = loop index.
717 __ mov(ebx, Operand(eax, 0));
718 __ mov(ecx, Operand(ebx, FrameDescription::frame_size_offset()));
719 __ bind(&inner_push_loop);
720 __ sub(Operand(ecx), Immediate(sizeof(uint32_t)));
721 __ push(Operand(ebx, ecx, times_1, FrameDescription::frame_content_offset()));
722 __ test(ecx, Operand(ecx));
723 __ j(not_zero, &inner_push_loop);
724 __ add(Operand(eax), Immediate(kPointerSize));
725 __ cmp(eax, Operand(edx));
726 __ j(below, &outer_push_loop);
727
728 // In case of OSR, we have to restore the XMM registers.
729 if (type() == OSR) {
730 for (int i = 0; i < XMMRegister::kNumAllocatableRegisters; ++i) {
731 XMMRegister xmm_reg = XMMRegister::FromAllocationIndex(i);
732 int src_offset = i * kDoubleSize + double_regs_offset;
733 __ movdbl(xmm_reg, Operand(ebx, src_offset));
734 }
735 }
736
737 // Push state, pc, and continuation from the last output frame.
738 if (type() != OSR) {
739 __ push(Operand(ebx, FrameDescription::state_offset()));
740 }
741 __ push(Operand(ebx, FrameDescription::pc_offset()));
742 __ push(Operand(ebx, FrameDescription::continuation_offset()));
743
744
745 // Push the registers from the last output frame.
746 for (int i = 0; i < kNumberOfRegisters; i++) {
Steve Block1e0659c2011-05-24 12:43:12 +0100747 int offset = (i * kPointerSize) + FrameDescription::registers_offset();
Ben Murdochb0fe1622011-05-05 13:52:32 +0100748 __ push(Operand(ebx, offset));
749 }
750
751 // Restore the registers from the stack.
752 __ popad();
753
754 // Return to the continuation point.
755 __ ret(0);
756}
757
758
759void Deoptimizer::TableEntryGenerator::GeneratePrologue() {
760 // Create a sequence of deoptimization entries.
761 Label done;
762 for (int i = 0; i < count(); i++) {
763 int start = masm()->pc_offset();
764 USE(start);
765 __ push_imm32(i);
766 __ jmp(&done);
767 ASSERT(masm()->pc_offset() - start == table_entry_size_);
768 }
769 __ bind(&done);
770}
771
772#undef __
773
774
775} } // namespace v8::internal
Ben Murdochb8e0da22011-05-16 14:20:40 +0100776
777#endif // V8_TARGET_ARCH_IA32