Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(186)

Side by Side Diff: src/mips/deoptimizer-mips.cc

Issue 7934002: MIPS: crankshaft implementation (Closed)
Patch Set: Rebased to r9640, including new-gc. Created 9 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2011 the V8 project authors. All rights reserved. 1 // Copyright 2011 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without 2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are 3 // modification, are permitted provided that the following conditions are
4 // met: 4 // met:
5 // 5 //
6 // * Redistributions of source code must retain the above copyright 6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer. 7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above 8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following 9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided 10 // disclaimer in the documentation and/or other materials provided
(...skipping 14 matching lines...) Expand all
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 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. 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 27
28 #include "v8.h" 28 #include "v8.h"
29 29
30 #include "codegen.h" 30 #include "codegen.h"
31 #include "deoptimizer.h" 31 #include "deoptimizer.h"
32 #include "full-codegen.h" 32 #include "full-codegen.h"
33 #include "safepoint-table.h" 33 #include "safepoint-table.h"
34 34
35 // Note: this file was taken from the X64 version. ARM has a partially working
36 // lithium implementation, but for now it is not ported to mips.
37
38 namespace v8 { 35 namespace v8 {
39 namespace internal { 36 namespace internal {
40 37
41 38
42 const int Deoptimizer::table_entry_size_ = 10; 39 const int Deoptimizer::table_entry_size_ = 32;
43 40
44 41
45 int Deoptimizer::patch_size() { 42 int Deoptimizer::patch_size() {
46 const int kCallInstructionSizeInWords = 3; 43 const int kCallInstructionSizeInWords = 4;
47 return kCallInstructionSizeInWords * Assembler::kInstrSize; 44 return kCallInstructionSizeInWords * Assembler::kInstrSize;
48 } 45 }
49 46
50 47
51 void Deoptimizer::DeoptimizeFunction(JSFunction* function) { 48 void Deoptimizer::EnsureRelocSpaceForLazyDeoptimization(Handle<Code> code) {
52 UNIMPLEMENTED(); 49 // Nothing to do. No new relocation information is written for lazy
50 // deoptimization on MIPS.
53 } 51 }
54 52
55 53
54 void Deoptimizer::DeoptimizeFunction(JSFunction* function) {
55 HandleScope scope;
56 AssertNoAllocation no_allocation;
57
58 if (!function->IsOptimized()) return;
59
60 // Get the optimized code.
61 Code* code = function->code();
62
63 // Invalidate the relocation information, as it will become invalid by the
64 // code patching below, and is not needed any more.
65 code->InvalidateRelocation();
66
67 // For each return after a safepoint insert an absolute call to the
68 // corresponding deoptimization entry.
69 unsigned last_pc_offset = 0;
70 SafepointTable table(function->code());
71 for (unsigned i = 0; i < table.length(); i++) {
72 unsigned pc_offset = table.GetPcOffset(i);
73 SafepointEntry safepoint_entry = table.GetEntry(i);
74 int deoptimization_index = safepoint_entry.deoptimization_index();
75 int gap_code_size = safepoint_entry.gap_code_size();
76 // Check that we did not shoot past next safepoint.
77 CHECK(pc_offset >= last_pc_offset);
78 #ifdef DEBUG
79 // Destroy the code which is not supposed to be run again.
80 int instructions = (pc_offset - last_pc_offset) / Assembler::kInstrSize;
81 CodePatcher destroyer(code->instruction_start() + last_pc_offset,
82 instructions);
83 for (int x = 0; x < instructions; x++) {
84 destroyer.masm()->break_(0);
85 }
86 #endif
87 last_pc_offset = pc_offset;
88 if (deoptimization_index != Safepoint::kNoDeoptimizationIndex) {
89 Address deoptimization_entry = Deoptimizer::GetDeoptimizationEntry(
90 deoptimization_index, Deoptimizer::LAZY);
91 last_pc_offset += gap_code_size;
92 int call_size_in_bytes = MacroAssembler::CallSize(deoptimization_entry,
93 RelocInfo::NONE);
94 int call_size_in_words = call_size_in_bytes / Assembler::kInstrSize;
95 ASSERT(call_size_in_bytes % Assembler::kInstrSize == 0);
96 ASSERT(call_size_in_bytes <= patch_size());
97 CodePatcher patcher(code->instruction_start() + last_pc_offset,
98 call_size_in_words);
99 patcher.masm()->Call(deoptimization_entry, RelocInfo::NONE);
100 last_pc_offset += call_size_in_bytes;
101 }
102 }
103
104 #ifdef DEBUG
105 // Destroy the code which is not supposed to be run again.
106 int instructions =
107 (code->safepoint_table_offset() - last_pc_offset) / Assembler::kInstrSize;
108 CodePatcher destroyer(code->instruction_start() + last_pc_offset,
109 instructions);
110 for (int x = 0; x < instructions; x++) {
111 destroyer.masm()->break_(0);
112 }
113 #endif
114
115 Isolate* isolate = code->GetIsolate();
116
fschneider 2011/10/18 14:27:58 Remove extra new line.
Paul Lind 2011/10/19 02:18:27 Done.
117
118 // Add the deoptimizing code to the list.
119 DeoptimizingCodeListNode* node = new DeoptimizingCodeListNode(code);
120 DeoptimizerData* data = isolate->deoptimizer_data();
121 node->set_next(data->deoptimizing_code_list_);
122 data->deoptimizing_code_list_ = node;
123
124 // We might be in the middle of incremental marking with compaction.
125 // Tell collector to treat this code object in a special way and
126 // ignore all slots that might have been recorded on it.
127 isolate->heap()->mark_compact_collector()->InvalidateCode(code);
128
129 // Set the code for the function to non-optimized version.
130 function->ReplaceCode(function->shared()->code());
131
132 if (FLAG_trace_deopt) {
133 PrintF("[forced deoptimization: ");
134 function->PrintName();
135 PrintF(" / %x]\n", reinterpret_cast<uint32_t>(function));
136 #ifdef DEBUG
137 if (FLAG_print_code) {
138 code->PrintLn();
139 }
140 #endif
141 }
142 }
143
144
56 void Deoptimizer::PatchStackCheckCodeAt(Code* unoptimized_code, 145 void Deoptimizer::PatchStackCheckCodeAt(Code* unoptimized_code,
57 Address pc_after, 146 Address pc_after,
58 Code* check_code, 147 Code* check_code,
59 Code* replacement_code) { 148 Code* replacement_code) {
60 UNIMPLEMENTED(); 149 const int kInstrSize = Assembler::kInstrSize;
150 // This structure comes from FullCodeGenerator::EmitStackCheck.
151 // The call of the stack guard check has the following form:
152 // sltu at, sp, t0
153 // beq at, zero_reg, ok
154 // lui t9, <stack guard address> upper
155 // ori t9, <stack guard address> lower
156 // jalr t9
157 // nop
158 // ----- pc_after points here
159
160 ASSERT(Assembler::IsBeq(Assembler::instr_at(pc_after - 5 * kInstrSize)));
161
162 // Replace the sltu instruction so beq is not executed.
163 CodePatcher patcher(pc_after - 6 * kInstrSize, 1);
164 patcher.masm()->addiu(at, zero_reg, 1);
165
166 Assembler::set_target_address_at(pc_after - 4 * kInstrSize,
167 replacement_code->entry());
168
169 // We patched the code to the following form:
170 // addiu at, zero_reg, 1
171 // beq at, zero_reg, ok ;; Not changed
172 // lui t9, <on-stack replacement address> upper
173 // ori t9, <on-stack replacement address> lower
174 // jalr t9 ;; Not changed
175 // nop ;; Not changed
176 // ----- pc_after points here
fschneider 2011/10/18 14:27:58 With new GC I think you also need a write barrier
Paul Lind 2011/10/19 02:18:27 Oops, we missed this. Thanks. Added it, and ported
61 } 177 }
62 178
63 179
64 void Deoptimizer::RevertStackCheckCodeAt(Address pc_after, 180 void Deoptimizer::RevertStackCheckCodeAt(Address pc_after,
65 Code* check_code, 181 Code* check_code,
66 Code* replacement_code) { 182 Code* replacement_code) {
67 UNIMPLEMENTED(); 183 // Exact opposite of the function above.
184 const int kInstrSize = Assembler::kInstrSize;
185 ASSERT(Assembler::IsAddImmediate(
186 Assembler::instr_at(pc_after - 6 * kInstrSize)));
187 ASSERT(Assembler::IsBeq(Assembler::instr_at(pc_after - 5 * kInstrSize)));
188
189 // Restore the sltu instruction so beq can possibly be executed again.
190 CodePatcher patcher(pc_after - 6 * kInstrSize, 1);
191 patcher.masm()->sltu(at, sp, t0);
192
193 Assembler::set_target_address_at(pc_after - 4 * kInstrSize,
194 check_code->entry());
fschneider 2011/10/18 14:27:58 With new GC also insert call to write barrier here
Paul Lind 2011/10/19 02:18:27 Updated, as above.
195 }
196
197
198 static int LookupBailoutId(DeoptimizationInputData* data, unsigned ast_id) {
199 ByteArray* translations = data->TranslationByteArray();
200 int length = data->DeoptCount();
201 for (int i = 0; i < length; i++) {
202 if (static_cast<unsigned>(data->AstId(i)->value()) == ast_id) {
203 TranslationIterator it(translations, data->TranslationIndex(i)->value());
204 int value = it.Next();
205 ASSERT(Translation::BEGIN == static_cast<Translation::Opcode>(value));
206 // Read the number of frames.
207 value = it.Next();
208 if (value == 1) return i;
209 }
210 }
211 UNREACHABLE();
212 return -1;
68 } 213 }
69 214
70 215
71 void Deoptimizer::DoComputeOsrOutputFrame() { 216 void Deoptimizer::DoComputeOsrOutputFrame() {
72 UNIMPLEMENTED(); 217 DeoptimizationInputData* data = DeoptimizationInputData::cast(
73 } 218 optimized_code_->deoptimization_data());
74 219 unsigned ast_id = data->OsrAstId()->value();
75 220
221 int bailout_id = LookupBailoutId(data, ast_id);
222 unsigned translation_index = data->TranslationIndex(bailout_id)->value();
223 ByteArray* translations = data->TranslationByteArray();
224
225 TranslationIterator iterator(translations, translation_index);
226 Translation::Opcode opcode =
227 static_cast<Translation::Opcode>(iterator.Next());
228 ASSERT(Translation::BEGIN == opcode);
229 USE(opcode);
230 int count = iterator.Next();
231 ASSERT(count == 1);
232 USE(count);
233
234 opcode = static_cast<Translation::Opcode>(iterator.Next());
235 USE(opcode);
236 ASSERT(Translation::FRAME == opcode);
237 unsigned node_id = iterator.Next();
238 USE(node_id);
239 ASSERT(node_id == ast_id);
240 JSFunction* function = JSFunction::cast(ComputeLiteral(iterator.Next()));
241 USE(function);
242 ASSERT(function == function_);
243 unsigned height = iterator.Next();
244 unsigned height_in_bytes = height * kPointerSize;
245 USE(height_in_bytes);
246
247 unsigned fixed_size = ComputeFixedSize(function_);
248 unsigned input_frame_size = input_->GetFrameSize();
249 ASSERT(fixed_size + height_in_bytes == input_frame_size);
250
251 unsigned stack_slot_size = optimized_code_->stack_slots() * kPointerSize;
252 unsigned outgoing_height = data->ArgumentsStackHeight(bailout_id)->value();
253 unsigned outgoing_size = outgoing_height * kPointerSize;
254 unsigned output_frame_size = fixed_size + stack_slot_size + outgoing_size;
255 ASSERT(outgoing_size == 0); // OSR does not happen in the middle of a call.
256
257 if (FLAG_trace_osr) {
258 PrintF("[on-stack replacement: begin 0x%08" V8PRIxPTR " ",
259 reinterpret_cast<intptr_t>(function_));
260 function_->PrintName();
261 PrintF(" => node=%u, frame=%d->%d]\n",
262 ast_id,
263 input_frame_size,
264 output_frame_size);
265 }
266
267 // There's only one output frame in the OSR case.
268 output_count_ = 1;
269 output_ = new FrameDescription*[1];
270 output_[0] = new(output_frame_size) FrameDescription(
271 output_frame_size, function_);
272 #ifdef DEBUG
273 output_[0]->SetKind(Code::OPTIMIZED_FUNCTION);
274 #endif
275
276 // Clear the incoming parameters in the optimized frame to avoid
277 // confusing the garbage collector.
278 unsigned output_offset = output_frame_size - kPointerSize;
279 int parameter_count = function_->shared()->formal_parameter_count() + 1;
280 for (int i = 0; i < parameter_count; ++i) {
281 output_[0]->SetFrameSlot(output_offset, 0);
282 output_offset -= kPointerSize;
283 }
284
285 // Translate the incoming parameters. This may overwrite some of the
286 // incoming argument slots we've just cleared.
287 int input_offset = input_frame_size - kPointerSize;
288 bool ok = true;
289 int limit = input_offset - (parameter_count * kPointerSize);
290 while (ok && input_offset > limit) {
291 ok = DoOsrTranslateCommand(&iterator, &input_offset);
292 }
293
294 // There are no translation commands for the caller's pc and fp, the
295 // context, and the function. Set them up explicitly.
296 for (int i = StandardFrameConstants::kCallerPCOffset;
297 ok && i >= StandardFrameConstants::kMarkerOffset;
298 i -= kPointerSize) {
299 uint32_t input_value = input_->GetFrameSlot(input_offset);
300 if (FLAG_trace_osr) {
301 const char* name = "UNKNOWN";
302 switch (i) {
303 case StandardFrameConstants::kCallerPCOffset:
304 name = "caller's pc";
305 break;
306 case StandardFrameConstants::kCallerFPOffset:
307 name = "fp";
308 break;
309 case StandardFrameConstants::kContextOffset:
310 name = "context";
311 break;
312 case StandardFrameConstants::kMarkerOffset:
313 name = "function";
314 break;
315 }
316 PrintF(" [sp + %d] <- 0x%08x ; [sp + %d] (fixed part - %s)\n",
317 output_offset,
318 input_value,
319 input_offset,
320 name);
321 }
322
323 output_[0]->SetFrameSlot(output_offset, input_->GetFrameSlot(input_offset));
324 input_offset -= kPointerSize;
325 output_offset -= kPointerSize;
326 }
327
328 // Translate the rest of the frame.
329 while (ok && input_offset >= 0) {
330 ok = DoOsrTranslateCommand(&iterator, &input_offset);
331 }
332
333 // If translation of any command failed, continue using the input frame.
334 if (!ok) {
335 delete output_[0];
336 output_[0] = input_;
337 output_[0]->SetPc(reinterpret_cast<uint32_t>(from_));
338 } else {
339 // Setup the frame pointer and the context pointer.
340 output_[0]->SetRegister(fp.code(), input_->GetRegister(fp.code()));
341 output_[0]->SetRegister(cp.code(), input_->GetRegister(cp.code()));
342
343 unsigned pc_offset = data->OsrPcOffset()->value();
344 uint32_t pc = reinterpret_cast<uint32_t>(
345 optimized_code_->entry() + pc_offset);
346 output_[0]->SetPc(pc);
347 }
348 Code* continuation = isolate_->builtins()->builtin(Builtins::kNotifyOSR);
349 output_[0]->SetContinuation(
350 reinterpret_cast<uint32_t>(continuation->entry()));
351
352 if (FLAG_trace_osr) {
353 PrintF("[on-stack replacement translation %s: 0x%08" V8PRIxPTR " ",
354 ok ? "finished" : "aborted",
355 reinterpret_cast<intptr_t>(function));
356 function->PrintName();
357 PrintF(" => pc=0x%0x]\n", output_[0]->GetPc());
358 }
359 }
360
361
362 // This code is very similar to ia32/arm code, but relies on register names
363 // (fp, sp) and how the frame is laid out.
76 void Deoptimizer::DoComputeFrame(TranslationIterator* iterator, 364 void Deoptimizer::DoComputeFrame(TranslationIterator* iterator,
77 int frame_index) { 365 int frame_index) {
78 UNIMPLEMENTED(); 366 // Read the ast node id, function, and frame height for this output frame.
79 } 367 Translation::Opcode opcode =
80 368 static_cast<Translation::Opcode>(iterator->Next());
369 USE(opcode);
370 ASSERT(Translation::FRAME == opcode);
371 int node_id = iterator->Next();
372 JSFunction* function = JSFunction::cast(ComputeLiteral(iterator->Next()));
373 unsigned height = iterator->Next();
374 unsigned height_in_bytes = height * kPointerSize;
375 if (FLAG_trace_deopt) {
376 PrintF(" translating ");
377 function->PrintName();
378 PrintF(" => node=%d, height=%d\n", node_id, height_in_bytes);
379 }
380
381 // The 'fixed' part of the frame consists of the incoming parameters and
382 // the part described by JavaScriptFrameConstants.
383 unsigned fixed_frame_size = ComputeFixedSize(function);
384 unsigned input_frame_size = input_->GetFrameSize();
385 unsigned output_frame_size = height_in_bytes + fixed_frame_size;
386
387 // Allocate and store the output frame description.
388 FrameDescription* output_frame =
389 new(output_frame_size) FrameDescription(output_frame_size, function);
390 #ifdef DEBUG
391 output_frame->SetKind(Code::FUNCTION);
392 #endif
393
394 bool is_bottommost = (0 == frame_index);
395 bool is_topmost = (output_count_ - 1 == frame_index);
396 ASSERT(frame_index >= 0 && frame_index < output_count_);
397 ASSERT(output_[frame_index] == NULL);
398 output_[frame_index] = output_frame;
399
400 // The top address for the bottommost output frame can be computed from
401 // the input frame pointer and the output frame's height. For all
402 // subsequent output frames, it can be computed from the previous one's
403 // top address and the current frame's size.
404 uint32_t top_address;
405 if (is_bottommost) {
406 // 2 = context and function in the frame.
407 top_address =
408 input_->GetRegister(fp.code()) - (2 * kPointerSize) - height_in_bytes;
409 } else {
410 top_address = output_[frame_index - 1]->GetTop() - output_frame_size;
411 }
412 output_frame->SetTop(top_address);
413
414 // Compute the incoming parameter translation.
415 int parameter_count = function->shared()->formal_parameter_count() + 1;
416 unsigned output_offset = output_frame_size;
417 unsigned input_offset = input_frame_size;
418 for (int i = 0; i < parameter_count; ++i) {
419 output_offset -= kPointerSize;
420 DoTranslateCommand(iterator, frame_index, output_offset);
421 }
422 input_offset -= (parameter_count * kPointerSize);
423
424 // There are no translation commands for the caller's pc and fp, the
425 // context, and the function. Synthesize their values and set them up
426 // explicitly.
427 //
428 // The caller's pc for the bottommost output frame is the same as in the
429 // input frame. For all subsequent output frames, it can be read from the
430 // previous one. This frame's pc can be computed from the non-optimized
431 // function code and AST id of the bailout.
432 output_offset -= kPointerSize;
433 input_offset -= kPointerSize;
434 intptr_t value;
435 if (is_bottommost) {
436 value = input_->GetFrameSlot(input_offset);
437 } else {
438 value = output_[frame_index - 1]->GetPc();
439 }
440 output_frame->SetFrameSlot(output_offset, value);
441 if (FLAG_trace_deopt) {
442 PrintF(" 0x%08x: [top + %d] <- 0x%08x ; caller's pc\n",
443 top_address + output_offset, output_offset, value);
444 }
445
446 // The caller's frame pointer for the bottommost output frame is the same
447 // as in the input frame. For all subsequent output frames, it can be
448 // read from the previous one. Also compute and set this frame's frame
449 // pointer.
450 output_offset -= kPointerSize;
451 input_offset -= kPointerSize;
452 if (is_bottommost) {
453 value = input_->GetFrameSlot(input_offset);
454 } else {
455 value = output_[frame_index - 1]->GetFp();
456 }
457 output_frame->SetFrameSlot(output_offset, value);
458 intptr_t fp_value = top_address + output_offset;
459 ASSERT(!is_bottommost || input_->GetRegister(fp.code()) == fp_value);
460 output_frame->SetFp(fp_value);
461 if (is_topmost) {
462 output_frame->SetRegister(fp.code(), fp_value);
463 }
464 if (FLAG_trace_deopt) {
465 PrintF(" 0x%08x: [top + %d] <- 0x%08x ; caller's fp\n",
466 fp_value, output_offset, value);
467 }
468
469 // For the bottommost output frame the context can be gotten from the input
470 // frame. For all subsequent output frames it can be gotten from the function
471 // so long as we don't inline functions that need local contexts.
472 output_offset -= kPointerSize;
473 input_offset -= kPointerSize;
474 if (is_bottommost) {
475 value = input_->GetFrameSlot(input_offset);
476 } else {
477 value = reinterpret_cast<intptr_t>(function->context());
478 }
479 output_frame->SetFrameSlot(output_offset, value);
480 if (is_topmost) {
481 output_frame->SetRegister(cp.code(), value);
482 }
483 if (FLAG_trace_deopt) {
484 PrintF(" 0x%08x: [top + %d] <- 0x%08x ; context\n",
485 top_address + output_offset, output_offset, value);
486 }
487
488 // The function was mentioned explicitly in the BEGIN_FRAME.
489 output_offset -= kPointerSize;
490 input_offset -= kPointerSize;
491 value = reinterpret_cast<uint32_t>(function);
492 // The function for the bottommost output frame should also agree with the
493 // input frame.
494 ASSERT(!is_bottommost || input_->GetFrameSlot(input_offset) == value);
495 output_frame->SetFrameSlot(output_offset, value);
496 if (FLAG_trace_deopt) {
497 PrintF(" 0x%08x: [top + %d] <- 0x%08x ; function\n",
498 top_address + output_offset, output_offset, value);
499 }
500
501 // Translate the rest of the frame.
502 for (unsigned i = 0; i < height; ++i) {
503 output_offset -= kPointerSize;
504 DoTranslateCommand(iterator, frame_index, output_offset);
505 }
506 ASSERT(0 == output_offset);
507
508 // Compute this frame's PC, state, and continuation.
509 Code* non_optimized_code = function->shared()->code();
510 FixedArray* raw_data = non_optimized_code->deoptimization_data();
511 DeoptimizationOutputData* data = DeoptimizationOutputData::cast(raw_data);
512 Address start = non_optimized_code->instruction_start();
513 unsigned pc_and_state = GetOutputInfo(data, node_id, function->shared());
514 unsigned pc_offset = FullCodeGenerator::PcField::decode(pc_and_state);
515 uint32_t pc_value = reinterpret_cast<uint32_t>(start + pc_offset);
516 output_frame->SetPc(pc_value);
517
518 FullCodeGenerator::State state =
519 FullCodeGenerator::StateField::decode(pc_and_state);
520 output_frame->SetState(Smi::FromInt(state));
521
522
523 // Set the continuation for the topmost frame.
524 if (is_topmost && bailout_type_ != DEBUGGER) {
525 Builtins* builtins = isolate_->builtins();
526 Code* continuation = (bailout_type_ == EAGER)
527 ? builtins->builtin(Builtins::kNotifyDeoptimized)
528 : builtins->builtin(Builtins::kNotifyLazyDeoptimized);
529 output_frame->SetContinuation(
530 reinterpret_cast<uint32_t>(continuation->entry()));
531 }
532 }
81 533
82 void Deoptimizer::FillInputFrame(Address tos, JavaScriptFrame* frame) { 534 void Deoptimizer::FillInputFrame(Address tos, JavaScriptFrame* frame) {
83 UNIMPLEMENTED(); 535 // Set the register values. The values are not important as there are no
84 } 536 // callee saved registers in JavaScript frames, so all registers are
85 537 // spilled. Registers fp and sp are set to the correct values though.
86 538
539 for (int i = 0; i < Register::kNumRegisters; i++) {
540 input_->SetRegister(i, i * 4);
541 }
542 input_->SetRegister(sp.code(), reinterpret_cast<intptr_t>(frame->sp()));
543 input_->SetRegister(fp.code(), reinterpret_cast<intptr_t>(frame->fp()));
544 for (int i = 0; i < DoubleRegister::kNumAllocatableRegisters; i++) {
545 input_->SetDoubleRegister(i, 0.0);
546 }
547
548 // Fill the frame content from the actual data on the frame.
549 for (unsigned i = 0; i < input_->GetFrameSize(); i += kPointerSize) {
550 input_->SetFrameSlot(i, Memory::uint32_at(tos + i));
551 }
552 }
553
554
555 #define __ masm()->
556
557
558 // This code tries to be close to ia32 code so that any changes can be
559 // easily ported.
87 void Deoptimizer::EntryGenerator::Generate() { 560 void Deoptimizer::EntryGenerator::Generate() {
88 UNIMPLEMENTED(); 561 GeneratePrologue();
562
563 Isolate* isolate = masm()->isolate();
564
565 CpuFeatures::Scope scope(FPU);
566 // Unlike on ARM we don't save all the registers, just the useful ones.
567 // For the rest, there are gaps on the stack, so the offsets remain the same.
568 const int kNumberOfRegisters = Register::kNumRegisters;
569
570 RegList restored_regs = kJSCallerSaved | kCalleeSaved;
571 RegList saved_regs = restored_regs | sp.bit() | ra.bit();
572
573 const int kDoubleRegsSize =
574 kDoubleSize * FPURegister::kNumAllocatableRegisters;
575
576 // Save all FPU registers before messing with them.
577 __ Subu(sp, sp, Operand(kDoubleRegsSize));
578 for (int i = 0; i < FPURegister::kNumAllocatableRegisters; ++i) {
579 FPURegister fpu_reg = FPURegister::FromAllocationIndex(i);
580 int offset = i * kDoubleSize;
581 __ sdc1(fpu_reg, MemOperand(sp, offset));
582 }
583
584 // Push saved_regs (needed to populate FrameDescription::registers_).
585 // Leave gaps for other registers.
586 __ Subu(sp, sp, kNumberOfRegisters * kPointerSize);
587 for (int16_t i = kNumberOfRegisters - 1; i >= 0; i--) {
588 if ((saved_regs & (1 << i)) != 0) {
589 __ sw(ToRegister(i), MemOperand(sp, 4 * i));
fschneider 2011/10/18 14:27:58 Maybe use kPointerSize instead of 4.
Paul Lind 2011/10/19 02:18:27 Done.
590 }
591 }
592
593 const int kSavedRegistersAreaSize =
594 (kNumberOfRegisters * kPointerSize) + kDoubleRegsSize;
595
596 // Get the bailout id from the stack.
597 __ lw(a2, MemOperand(sp, kSavedRegistersAreaSize));
598
599 // Get the address of the location in the code object if possible (a3) (return
600 // address for lazy deoptimization) and compute the fp-to-sp delta in
601 // register t0.
602 if (type() == EAGER) {
603 __ mov(a3, zero_reg);
604 // Correct one word for bailout id.
605 __ Addu(t0, sp, Operand(kSavedRegistersAreaSize + (1 * kPointerSize)));
606 } else if (type() == OSR) {
607 __ mov(a3, ra);
608 // Correct one word for bailout id.
609 __ Addu(t0, sp, Operand(kSavedRegistersAreaSize + (1 * kPointerSize)));
610 } else {
611 __ mov(a3, ra);
612 // Correct two words for bailout id and return address.
613 __ Addu(t0, sp, Operand(kSavedRegistersAreaSize + (2 * kPointerSize)));
614 }
615
616 __ Subu(t0, fp, t0);
617
618 // Allocate a new deoptimizer object.
619 // Pass four arguments in a0 to a3 and fifth & sixth arguments on stack.
620 __ PrepareCallCFunction(6, t1);
621 __ lw(a0, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
622 __ li(a1, Operand(type())); // bailout type,
623 // a2: bailout id already loaded.
624 // a3: code address or 0 already loaded.
625 __ sw(t0, CFunctionArgumentOperand(5)); // Fp-to-sp delta.
626 __ li(t1, Operand(ExternalReference::isolate_address()));
627 __ sw(t1, CFunctionArgumentOperand(6)); // Isolate.
628 // Call Deoptimizer::New().
629 {
630 AllowExternalCallThatCantCauseGC scope(masm());
631 __ CallCFunction(ExternalReference::new_deoptimizer_function(isolate), 6);
632 }
633
634 // Preserve "deoptimizer" object in register v0 and get the input
635 // frame descriptor pointer to a1 (deoptimizer->input_);
636 // Move deopt-obj to a0 for call to Deoptimizer::ComputeOutputFrames() below.
637 __ mov(a0, v0);
638 __ lw(a1, MemOperand(v0, Deoptimizer::input_offset()));
639
640 // Copy core registers into FrameDescription::registers_[kNumRegisters].
641 ASSERT(Register::kNumRegisters == kNumberOfRegisters);
642 for (int i = 0; i < kNumberOfRegisters; i++) {
643 int offset = (i * kPointerSize) + FrameDescription::registers_offset();
644 if ((saved_regs & (1 << i)) != 0) {
645 __ lw(a2, MemOperand(sp, i * kPointerSize));
646 __ sw(a2, MemOperand(a1, offset));
647 } else if (FLAG_debug_code) {
648 __ li(a2, kDebugZapValue);
649 __ sw(a2, MemOperand(a1, offset));
650 }
651 }
652
653 // Copy FPU registers to
654 // double_registers_[DoubleRegister::kNumAllocatableRegisters]
655 int double_regs_offset = FrameDescription::double_registers_offset();
656 for (int i = 0; i < FPURegister::kNumAllocatableRegisters; ++i) {
657 int dst_offset = i * kDoubleSize + double_regs_offset;
658 int src_offset = i * kDoubleSize + kNumberOfRegisters * kPointerSize;
659 __ ldc1(f0, MemOperand(sp, src_offset));
660 __ sdc1(f0, MemOperand(a1, dst_offset));
661 }
662
663 // Remove the bailout id, eventually return address, and the saved registers
664 // from the stack.
665 if (type() == EAGER || type() == OSR) {
666 __ Addu(sp, sp, Operand(kSavedRegistersAreaSize + (1 * kPointerSize)));
667 } else {
668 __ Addu(sp, sp, Operand(kSavedRegistersAreaSize + (2 * kPointerSize)));
669 }
670
671 // Compute a pointer to the unwinding limit in register a2; that is
672 // the first stack slot not part of the input frame.
673 __ lw(a2, MemOperand(a1, FrameDescription::frame_size_offset()));
674 __ Addu(a2, a2, sp);
675
676 // Unwind the stack down to - but not including - the unwinding
677 // limit and copy the contents of the activation frame to the input
678 // frame description.
679 __ Addu(a3, a1, Operand(FrameDescription::frame_content_offset()));
680 Label pop_loop;
681 __ bind(&pop_loop);
682 __ pop(t0);
683 __ sw(t0, MemOperand(a3, 0));
684 __ Branch(USE_DELAY_SLOT, &pop_loop, ne, a2, Operand(sp));
685 __ addiu(a3, a3, sizeof(uint32_t)); // In delay slot.
686
687 // Compute the output frame in the deoptimizer.
688 __ push(a0); // Preserve deoptimizer object across call.
689 // a0: deoptimizer object; a1: scratch.
690 __ PrepareCallCFunction(1, a1);
691 // Call Deoptimizer::ComputeOutputFrames().
692 {
693 AllowExternalCallThatCantCauseGC scope(masm());
694 __ CallCFunction(
695 ExternalReference::compute_output_frames_function(isolate), 1);
696 }
697 __ pop(a0); // Restore deoptimizer object (class Deoptimizer).
698
699 // Replace the current (input) frame with the output frames.
700 Label outer_push_loop, inner_push_loop;
701 // Outer loop state: a0 = current "FrameDescription** output_",
702 // a1 = one past the last FrameDescription**.
703 __ lw(a1, MemOperand(a0, Deoptimizer::output_count_offset()));
704 __ lw(a0, MemOperand(a0, Deoptimizer::output_offset())); // a0 is output_.
705 // __ add(r1, r0, Operand(r1, LSL, 2));
fschneider 2011/10/18 14:27:58 Remove commented code. Looks as if this left over
Paul Lind 2011/10/19 02:18:27 Yep. A porting artifact, now removed.
706 __ sll(a1, a1, kPointerSizeLog2); // Count to offset.
707 __ addu(a1, a0, a1); // a1 = one past the last FrameDescription**.
708 __ bind(&outer_push_loop);
709 // Inner loop state: a2 = current FrameDescription*, a3 = loop index.
710 __ lw(a2, MemOperand(a0, 0)); // output_[ix]
711 __ lw(a3, MemOperand(a2, FrameDescription::frame_size_offset()));
712 __ bind(&inner_push_loop);
713 __ Subu(a3, a3, Operand(sizeof(uint32_t)));
714 __ Addu(t2, a2, Operand(a3));
715 __ lw(t3, MemOperand(t2, FrameDescription::frame_content_offset()));
716 __ push(t3);
717 __ Branch(&inner_push_loop, ne, a3, Operand(zero_reg));
718
719 __ Addu(a0, a0, Operand(kPointerSize));
720 __ Branch(&outer_push_loop, lt, a0, Operand(a1));
721
722
723 // Push state, pc, and continuation from the last output frame.
724 if (type() != OSR) {
725 __ lw(t2, MemOperand(a2, FrameDescription::state_offset()));
726 __ push(t2);
727 }
728
729 __ lw(t2, MemOperand(a2, FrameDescription::pc_offset()));
730 __ push(t2);
731 __ lw(t2, MemOperand(a2, FrameDescription::continuation_offset()));
732 __ push(t2);
733
734
735 // Technically restoring 'at' should work unless zero_reg is also restored
736 // but it's safer to check for this.
737 ASSERT(!(at.bit() & restored_regs));
738 // Restore the registers from the last output frame.
739 __ mov(at, a2);
740 for (int i = kNumberOfRegisters - 1; i >= 0; i--) {
741 int offset = (i * kPointerSize) + FrameDescription::registers_offset();
742 if ((restored_regs & (1 << i)) != 0) {
743 __ lw(ToRegister(i), MemOperand(at, offset));
744 }
745 }
746
747 // Set up the roots register.
748 ExternalReference roots_address = ExternalReference::roots_address(isolate);
749 __ li(roots, Operand(roots_address));
750
751 __ pop(at); // Get continuation, leave pc on stack.
752 __ pop(ra);
753 __ Jump(at);
754 __ stop("Unreachable.");
89 } 755 }
90 756
91 757
92 void Deoptimizer::TableEntryGenerator::GeneratePrologue() { 758 void Deoptimizer::TableEntryGenerator::GeneratePrologue() {
93 UNIMPLEMENTED(); 759 Assembler::BlockTrampolinePoolScope block_trampoline_pool(masm());
94 } 760
761 // Create a sequence of deoptimization entries. Note that any
762 // registers may be still live.
763
764 Label done;
765 for (int i = 0; i < count(); i++) {
766 int start = masm()->pc_offset();
767 USE(start);
768 if (type() != EAGER) {
769 // Emulate ia32 like call by pushing return address to stack.
770 __ push(ra);
771 }
772 __ li(at, Operand(i));
773 __ push(at);
774 __ Branch(&done);
775
776 // Pad the rest of the code.
777 while (table_entry_size_ > (masm()->pc_offset() - start)) {
778 __ nop();
779 }
780
781 ASSERT_EQ(table_entry_size_, masm()->pc_offset() - start);
782 }
783 __ bind(&done);
784 }
785
786 #undef __
95 787
96 788
97 } } // namespace v8::internal 789 } } // namespace v8::internal
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698