• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2011 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "src/codegen.h"
6 #include "src/deoptimizer.h"
7 #include "src/full-codegen/full-codegen.h"
8 #include "src/register-configuration.h"
9 #include "src/safepoint-table.h"
10 
11 namespace v8 {
12 namespace internal {
13 
14 
patch_size()15 int Deoptimizer::patch_size() {
16   const int kCallInstructionSizeInWords = 6;
17   return kCallInstructionSizeInWords * Assembler::kInstrSize;
18 }
19 
20 
EnsureRelocSpaceForLazyDeoptimization(Handle<Code> code)21 void Deoptimizer::EnsureRelocSpaceForLazyDeoptimization(Handle<Code> code) {
22   // Empty because there is no need for relocation information for the code
23   // patching in Deoptimizer::PatchCodeForDeoptimization below.
24 }
25 
26 
PatchCodeForDeoptimization(Isolate * isolate,Code * code)27 void Deoptimizer::PatchCodeForDeoptimization(Isolate* isolate, Code* code) {
28   Address code_start_address = code->instruction_start();
29   // Invalidate the relocation information, as it will become invalid by the
30   // code patching below, and is not needed any more.
31   code->InvalidateRelocation();
32 
33   if (FLAG_zap_code_space) {
34     // Fail hard and early if we enter this code object again.
35     byte* pointer = code->FindCodeAgeSequence();
36     if (pointer != NULL) {
37       pointer += kNoCodeAgeSequenceLength;
38     } else {
39       pointer = code->instruction_start();
40     }
41     CodePatcher patcher(isolate, pointer, 1);
42     patcher.masm()->break_(0xCC);
43 
44     DeoptimizationInputData* data =
45         DeoptimizationInputData::cast(code->deoptimization_data());
46     int osr_offset = data->OsrPcOffset()->value();
47     if (osr_offset > 0) {
48       CodePatcher osr_patcher(isolate, code->instruction_start() + osr_offset,
49                               1);
50       osr_patcher.masm()->break_(0xCC);
51     }
52   }
53 
54   DeoptimizationInputData* deopt_data =
55       DeoptimizationInputData::cast(code->deoptimization_data());
56 #ifdef DEBUG
57   Address prev_call_address = NULL;
58 #endif
59   // For each LLazyBailout instruction insert a call to the corresponding
60   // deoptimization entry.
61   for (int i = 0; i < deopt_data->DeoptCount(); i++) {
62     if (deopt_data->Pc(i)->value() == -1) continue;
63     Address call_address = code_start_address + deopt_data->Pc(i)->value();
64     Address deopt_entry = GetDeoptimizationEntry(isolate, i, LAZY);
65     int call_size_in_bytes = MacroAssembler::CallSize(deopt_entry,
66                                                       RelocInfo::NONE32);
67     int call_size_in_words = call_size_in_bytes / Assembler::kInstrSize;
68     DCHECK(call_size_in_bytes % Assembler::kInstrSize == 0);
69     DCHECK(call_size_in_bytes <= patch_size());
70     CodePatcher patcher(isolate, call_address, call_size_in_words);
71     patcher.masm()->Call(deopt_entry, RelocInfo::NONE32);
72     DCHECK(prev_call_address == NULL ||
73            call_address >= prev_call_address + patch_size());
74     DCHECK(call_address + patch_size() <= code->instruction_end());
75 
76 #ifdef DEBUG
77     prev_call_address = call_address;
78 #endif
79   }
80 }
81 
82 
FillInputFrame(Address tos,JavaScriptFrame * frame)83 void Deoptimizer::FillInputFrame(Address tos, JavaScriptFrame* frame) {
84   // Set the register values. The values are not important as there are no
85   // callee saved registers in JavaScript frames, so all registers are
86   // spilled. Registers fp and sp are set to the correct values though.
87 
88   for (int i = 0; i < Register::kNumRegisters; i++) {
89     input_->SetRegister(i, i * 4);
90   }
91   input_->SetRegister(sp.code(), reinterpret_cast<intptr_t>(frame->sp()));
92   input_->SetRegister(fp.code(), reinterpret_cast<intptr_t>(frame->fp()));
93   for (int i = 0; i < DoubleRegister::kMaxNumRegisters; i++) {
94     input_->SetDoubleRegister(i, 0.0);
95   }
96 
97   // Fill the frame content from the actual data on the frame.
98   for (unsigned i = 0; i < input_->GetFrameSize(); i += kPointerSize) {
99     input_->SetFrameSlot(i, Memory::uint64_at(tos + i));
100   }
101 }
102 
103 
SetPlatformCompiledStubRegisters(FrameDescription * output_frame,CodeStubDescriptor * descriptor)104 void Deoptimizer::SetPlatformCompiledStubRegisters(
105     FrameDescription* output_frame, CodeStubDescriptor* descriptor) {
106   ApiFunction function(descriptor->deoptimization_handler());
107   ExternalReference xref(&function, ExternalReference::BUILTIN_CALL, isolate_);
108   intptr_t handler = reinterpret_cast<intptr_t>(xref.address());
109   int params = descriptor->GetHandlerParameterCount();
110   output_frame->SetRegister(a0.code(), params);
111   output_frame->SetRegister(a1.code(), handler);
112 }
113 
114 
CopyDoubleRegisters(FrameDescription * output_frame)115 void Deoptimizer::CopyDoubleRegisters(FrameDescription* output_frame) {
116   for (int i = 0; i < DoubleRegister::kMaxNumRegisters; ++i) {
117     double double_value = input_->GetDoubleRegister(i);
118     output_frame->SetDoubleRegister(i, double_value);
119   }
120 }
121 
122 
HasAlignmentPadding(JSFunction * function)123 bool Deoptimizer::HasAlignmentPadding(JSFunction* function) {
124   // There is no dynamic alignment padding on MIPS in the input frame.
125   return false;
126 }
127 
128 
129 #define __ masm()->
130 
131 
132 // This code tries to be close to ia32 code so that any changes can be
133 // easily ported.
Generate()134 void Deoptimizer::TableEntryGenerator::Generate() {
135   GeneratePrologue();
136 
137   // Unlike on ARM we don't save all the registers, just the useful ones.
138   // For the rest, there are gaps on the stack, so the offsets remain the same.
139   const int kNumberOfRegisters = Register::kNumRegisters;
140 
141   RegList restored_regs = kJSCallerSaved | kCalleeSaved;
142   RegList saved_regs = restored_regs | sp.bit() | ra.bit();
143 
144   const int kDoubleRegsSize = kDoubleSize * DoubleRegister::kMaxNumRegisters;
145 
146   // Save all FPU registers before messing with them.
147   __ Dsubu(sp, sp, Operand(kDoubleRegsSize));
148   const RegisterConfiguration* config =
149       RegisterConfiguration::ArchDefault(RegisterConfiguration::CRANKSHAFT);
150   for (int i = 0; i < config->num_allocatable_double_registers(); ++i) {
151     int code = config->GetAllocatableDoubleCode(i);
152     const DoubleRegister fpu_reg = DoubleRegister::from_code(code);
153     int offset = code * kDoubleSize;
154     __ sdc1(fpu_reg, MemOperand(sp, offset));
155   }
156 
157   // Push saved_regs (needed to populate FrameDescription::registers_).
158   // Leave gaps for other registers.
159   __ Dsubu(sp, sp, kNumberOfRegisters * kPointerSize);
160   for (int16_t i = kNumberOfRegisters - 1; i >= 0; i--) {
161     if ((saved_regs & (1 << i)) != 0) {
162       __ sd(ToRegister(i), MemOperand(sp, kPointerSize * i));
163     }
164   }
165 
166   __ li(a2, Operand(ExternalReference(Isolate::kCEntryFPAddress, isolate())));
167   __ sd(fp, MemOperand(a2));
168 
169   const int kSavedRegistersAreaSize =
170       (kNumberOfRegisters * kPointerSize) + kDoubleRegsSize;
171 
172   // Get the bailout id from the stack.
173   __ ld(a2, MemOperand(sp, kSavedRegistersAreaSize));
174 
175   // Get the address of the location in the code object (a3) (return
176   // address for lazy deoptimization) and compute the fp-to-sp delta in
177   // register a4.
178   __ mov(a3, ra);
179   // Correct one word for bailout id.
180   __ Daddu(a4, sp, Operand(kSavedRegistersAreaSize + (1 * kPointerSize)));
181 
182   __ Dsubu(a4, fp, a4);
183 
184   // Allocate a new deoptimizer object.
185   __ PrepareCallCFunction(6, a5);
186   // Pass six arguments, according to O32 or n64 ABI. a0..a3 are same for both.
187   __ li(a1, Operand(type()));  // bailout type,
188   __ ld(a0, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
189   // a2: bailout id already loaded.
190   // a3: code address or 0 already loaded.
191   if (kMipsAbi == kN64) {
192     // a4: already has fp-to-sp delta.
193     __ li(a5, Operand(ExternalReference::isolate_address(isolate())));
194   } else {  // O32 abi.
195     // Pass four arguments in a0 to a3 and fifth & sixth arguments on stack.
196     __ sd(a4, CFunctionArgumentOperand(5));  // Fp-to-sp delta.
197     __ li(a5, Operand(ExternalReference::isolate_address(isolate())));
198     __ sd(a5, CFunctionArgumentOperand(6));  // Isolate.
199   }
200   // Call Deoptimizer::New().
201   {
202     AllowExternalCallThatCantCauseGC scope(masm());
203     __ CallCFunction(ExternalReference::new_deoptimizer_function(isolate()), 6);
204   }
205 
206   // Preserve "deoptimizer" object in register v0 and get the input
207   // frame descriptor pointer to a1 (deoptimizer->input_);
208   // Move deopt-obj to a0 for call to Deoptimizer::ComputeOutputFrames() below.
209   __ mov(a0, v0);
210   __ ld(a1, MemOperand(v0, Deoptimizer::input_offset()));
211 
212   // Copy core registers into FrameDescription::registers_[kNumRegisters].
213   DCHECK(Register::kNumRegisters == kNumberOfRegisters);
214   for (int i = 0; i < kNumberOfRegisters; i++) {
215     int offset = (i * kPointerSize) + FrameDescription::registers_offset();
216     if ((saved_regs & (1 << i)) != 0) {
217       __ ld(a2, MemOperand(sp, i * kPointerSize));
218       __ sd(a2, MemOperand(a1, offset));
219     } else if (FLAG_debug_code) {
220       __ li(a2, kDebugZapValue);
221       __ sd(a2, MemOperand(a1, offset));
222     }
223   }
224 
225   int double_regs_offset = FrameDescription::double_registers_offset();
226   // Copy FPU registers to
227   // double_registers_[DoubleRegister::kNumAllocatableRegisters]
228   for (int i = 0; i < config->num_allocatable_double_registers(); ++i) {
229     int code = config->GetAllocatableDoubleCode(i);
230     int dst_offset = code * kDoubleSize + double_regs_offset;
231     int src_offset = code * kDoubleSize + kNumberOfRegisters * kPointerSize;
232     __ ldc1(f0, MemOperand(sp, src_offset));
233     __ sdc1(f0, MemOperand(a1, dst_offset));
234   }
235 
236   // Remove the bailout id and the saved registers from the stack.
237   __ Daddu(sp, sp, Operand(kSavedRegistersAreaSize + (1 * kPointerSize)));
238 
239   // Compute a pointer to the unwinding limit in register a2; that is
240   // the first stack slot not part of the input frame.
241   __ ld(a2, MemOperand(a1, FrameDescription::frame_size_offset()));
242   __ Daddu(a2, a2, sp);
243 
244   // Unwind the stack down to - but not including - the unwinding
245   // limit and copy the contents of the activation frame to the input
246   // frame description.
247   __ Daddu(a3, a1, Operand(FrameDescription::frame_content_offset()));
248   Label pop_loop;
249   Label pop_loop_header;
250   __ BranchShort(&pop_loop_header);
251   __ bind(&pop_loop);
252   __ pop(a4);
253   __ sd(a4, MemOperand(a3, 0));
254   __ daddiu(a3, a3, sizeof(uint64_t));
255   __ bind(&pop_loop_header);
256   __ BranchShort(&pop_loop, ne, a2, Operand(sp));
257   // Compute the output frame in the deoptimizer.
258   __ push(a0);  // Preserve deoptimizer object across call.
259   // a0: deoptimizer object; a1: scratch.
260   __ PrepareCallCFunction(1, a1);
261   // Call Deoptimizer::ComputeOutputFrames().
262   {
263     AllowExternalCallThatCantCauseGC scope(masm());
264     __ CallCFunction(
265         ExternalReference::compute_output_frames_function(isolate()), 1);
266   }
267   __ pop(a0);  // Restore deoptimizer object (class Deoptimizer).
268 
269   // Replace the current (input) frame with the output frames.
270   Label outer_push_loop, inner_push_loop,
271       outer_loop_header, inner_loop_header;
272   // Outer loop state: a4 = current "FrameDescription** output_",
273   // a1 = one past the last FrameDescription**.
274   __ lw(a1, MemOperand(a0, Deoptimizer::output_count_offset()));
275   __ ld(a4, MemOperand(a0, Deoptimizer::output_offset()));  // a4 is output_.
276   __ dsll(a1, a1, kPointerSizeLog2);  // Count to offset.
277   __ daddu(a1, a4, a1);  // a1 = one past the last FrameDescription**.
278   __ BranchShort(&outer_loop_header);
279   __ bind(&outer_push_loop);
280   // Inner loop state: a2 = current FrameDescription*, a3 = loop index.
281   __ ld(a2, MemOperand(a4, 0));  // output_[ix]
282   __ ld(a3, MemOperand(a2, FrameDescription::frame_size_offset()));
283   __ BranchShort(&inner_loop_header);
284   __ bind(&inner_push_loop);
285   __ Dsubu(a3, a3, Operand(sizeof(uint64_t)));
286   __ Daddu(a6, a2, Operand(a3));
287   __ ld(a7, MemOperand(a6, FrameDescription::frame_content_offset()));
288   __ push(a7);
289   __ bind(&inner_loop_header);
290   __ BranchShort(&inner_push_loop, ne, a3, Operand(zero_reg));
291 
292   __ Daddu(a4, a4, Operand(kPointerSize));
293   __ bind(&outer_loop_header);
294   __ BranchShort(&outer_push_loop, lt, a4, Operand(a1));
295 
296   __ ld(a1, MemOperand(a0, Deoptimizer::input_offset()));
297   for (int i = 0; i < config->num_allocatable_double_registers(); ++i) {
298     int code = config->GetAllocatableDoubleCode(i);
299     const DoubleRegister fpu_reg = DoubleRegister::from_code(code);
300     int src_offset = code * kDoubleSize + double_regs_offset;
301     __ ldc1(fpu_reg, MemOperand(a1, src_offset));
302   }
303 
304   // Push state, pc, and continuation from the last output frame.
305   __ ld(a6, MemOperand(a2, FrameDescription::state_offset()));
306   __ push(a6);
307 
308   __ ld(a6, MemOperand(a2, FrameDescription::pc_offset()));
309   __ push(a6);
310   __ ld(a6, MemOperand(a2, FrameDescription::continuation_offset()));
311   __ push(a6);
312 
313 
314   // Technically restoring 'at' should work unless zero_reg is also restored
315   // but it's safer to check for this.
316   DCHECK(!(at.bit() & restored_regs));
317   // Restore the registers from the last output frame.
318   __ mov(at, a2);
319   for (int i = kNumberOfRegisters - 1; i >= 0; i--) {
320     int offset = (i * kPointerSize) + FrameDescription::registers_offset();
321     if ((restored_regs & (1 << i)) != 0) {
322       __ ld(ToRegister(i), MemOperand(at, offset));
323     }
324   }
325 
326   __ InitializeRootRegister();
327 
328   __ pop(at);  // Get continuation, leave pc on stack.
329   __ pop(ra);
330   __ Jump(at);
331   __ stop("Unreachable.");
332 }
333 
334 
335 // Maximum size of a table entry generated below.
336 const int Deoptimizer::table_entry_size_ = 2 * Assembler::kInstrSize;
337 
GeneratePrologue()338 void Deoptimizer::TableEntryGenerator::GeneratePrologue() {
339   Assembler::BlockTrampolinePoolScope block_trampoline_pool(masm());
340 
341   // Create a sequence of deoptimization entries.
342   // Note that registers are still live when jumping to an entry.
343   Label table_start, done, done_special, trampoline_jump;
344   __ bind(&table_start);
345   int kMaxEntriesBranchReach =
346       (1 << (kImm16Bits - 2)) / (table_entry_size_ / Assembler::kInstrSize);
347 
348   if (count() <= kMaxEntriesBranchReach) {
349     // Common case.
350     for (int i = 0; i < count(); i++) {
351       Label start;
352       __ bind(&start);
353       DCHECK(is_int16(i));
354       __ BranchShort(USE_DELAY_SLOT, &done);  // Expose delay slot.
355       __ li(at, i);                      // In the delay slot.
356 
357       DCHECK_EQ(table_entry_size_, masm()->SizeOfCodeGeneratedSince(&start));
358     }
359 
360     DCHECK_EQ(masm()->SizeOfCodeGeneratedSince(&table_start),
361               count() * table_entry_size_);
362     __ bind(&done);
363     __ Push(at);
364   } else {
365     // Uncommon case, the branch cannot reach.
366     // Create mini trampoline and adjust id constants to get proper value at
367     // the end of table.
368     for (int i = kMaxEntriesBranchReach; i > 1; i--) {
369       Label start;
370       __ bind(&start);
371       DCHECK(is_int16(i));
372       __ BranchShort(USE_DELAY_SLOT, &trampoline_jump);  // Expose delay slot.
373       __ li(at, -i);                                // In the delay slot.
374       DCHECK_EQ(table_entry_size_, masm()->SizeOfCodeGeneratedSince(&start));
375     }
376     // Entry with id == kMaxEntriesBranchReach - 1.
377     __ bind(&trampoline_jump);
378     __ BranchShort(USE_DELAY_SLOT, &done_special);
379     __ li(at, -1);
380 
381     for (int i = kMaxEntriesBranchReach; i < count(); i++) {
382       Label start;
383       __ bind(&start);
384       DCHECK(is_int16(i));
385       __ Branch(USE_DELAY_SLOT, &done);  // Expose delay slot.
386       __ li(at, i);                      // In the delay slot.
387     }
388 
389     DCHECK_EQ(masm()->SizeOfCodeGeneratedSince(&table_start),
390               count() * table_entry_size_);
391     __ bind(&done_special);
392     __ daddiu(at, at, kMaxEntriesBranchReach);
393     __ bind(&done);
394     __ Push(at);
395   }
396 }
397 
398 
SetCallerPc(unsigned offset,intptr_t value)399 void FrameDescription::SetCallerPc(unsigned offset, intptr_t value) {
400   SetFrameSlot(offset, value);
401 }
402 
403 
SetCallerFp(unsigned offset,intptr_t value)404 void FrameDescription::SetCallerFp(unsigned offset, intptr_t value) {
405   SetFrameSlot(offset, value);
406 }
407 
408 
SetCallerConstantPool(unsigned offset,intptr_t value)409 void FrameDescription::SetCallerConstantPool(unsigned offset, intptr_t value) {
410   // No embedded constant pool support.
411   UNREACHABLE();
412 }
413 
414 
415 #undef __
416 
417 
418 }  // namespace internal
419 }  // namespace v8
420