• 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 = 4;
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 
SetPlatformCompiledStubRegisters(FrameDescription * output_frame,CodeStubDescriptor * descriptor)83 void Deoptimizer::SetPlatformCompiledStubRegisters(
84     FrameDescription* output_frame, CodeStubDescriptor* descriptor) {
85   ApiFunction function(descriptor->deoptimization_handler());
86   ExternalReference xref(&function, ExternalReference::BUILTIN_CALL, isolate_);
87   intptr_t handler = reinterpret_cast<intptr_t>(xref.address());
88   int params = descriptor->GetHandlerParameterCount();
89   output_frame->SetRegister(a0.code(), params);
90   output_frame->SetRegister(a1.code(), handler);
91 }
92 
93 
CopyDoubleRegisters(FrameDescription * output_frame)94 void Deoptimizer::CopyDoubleRegisters(FrameDescription* output_frame) {
95   for (int i = 0; i < DoubleRegister::kMaxNumRegisters; ++i) {
96     Float64 double_value = input_->GetDoubleRegister(i);
97     output_frame->SetDoubleRegister(i, double_value);
98   }
99 }
100 
101 #define __ masm()->
102 
103 
104 // This code tries to be close to ia32 code so that any changes can be
105 // easily ported.
Generate()106 void Deoptimizer::TableEntryGenerator::Generate() {
107   GeneratePrologue();
108 
109   // Unlike on ARM we don't save all the registers, just the useful ones.
110   // For the rest, there are gaps on the stack, so the offsets remain the same.
111   const int kNumberOfRegisters = Register::kNumRegisters;
112 
113   RegList restored_regs = kJSCallerSaved | kCalleeSaved;
114   RegList saved_regs = restored_regs | sp.bit() | ra.bit();
115 
116   const int kDoubleRegsSize = kDoubleSize * DoubleRegister::kMaxNumRegisters;
117 
118   // Save all FPU registers before messing with them.
119   __ Subu(sp, sp, Operand(kDoubleRegsSize));
120   const RegisterConfiguration* config = RegisterConfiguration::Crankshaft();
121   for (int i = 0; i < config->num_allocatable_double_registers(); ++i) {
122     int code = config->GetAllocatableDoubleCode(i);
123     const DoubleRegister fpu_reg = DoubleRegister::from_code(code);
124     int offset = code * kDoubleSize;
125     __ sdc1(fpu_reg, MemOperand(sp, offset));
126   }
127 
128   // Push saved_regs (needed to populate FrameDescription::registers_).
129   // Leave gaps for other registers.
130   __ Subu(sp, sp, kNumberOfRegisters * kPointerSize);
131   for (int16_t i = kNumberOfRegisters - 1; i >= 0; i--) {
132     if ((saved_regs & (1 << i)) != 0) {
133       __ sw(ToRegister(i), MemOperand(sp, kPointerSize * i));
134     }
135   }
136 
137   __ li(a2, Operand(ExternalReference(Isolate::kCEntryFPAddress, isolate())));
138   __ sw(fp, MemOperand(a2));
139 
140   const int kSavedRegistersAreaSize =
141       (kNumberOfRegisters * kPointerSize) + kDoubleRegsSize;
142 
143   // Get the bailout id from the stack.
144   __ lw(a2, MemOperand(sp, kSavedRegistersAreaSize));
145 
146   // Get the address of the location in the code object (a3) (return
147   // address for lazy deoptimization) and compute the fp-to-sp delta in
148   // register t0.
149   __ mov(a3, ra);
150   // Correct one word for bailout id.
151   __ Addu(t0, sp, Operand(kSavedRegistersAreaSize + (1 * kPointerSize)));
152 
153   __ Subu(t0, fp, t0);
154 
155   // Allocate a new deoptimizer object.
156   __ PrepareCallCFunction(6, t1);
157   // Pass four arguments in a0 to a3 and fifth & sixth arguments on stack.
158   __ mov(a0, zero_reg);
159   Label context_check;
160   __ lw(a1, MemOperand(fp, CommonFrameConstants::kContextOrFrameTypeOffset));
161   __ JumpIfSmi(a1, &context_check);
162   __ lw(a0, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
163   __ bind(&context_check);
164   __ li(a1, Operand(type()));  // Bailout type.
165   // a2: bailout id already loaded.
166   // a3: code address or 0 already loaded.
167   __ sw(t0, CFunctionArgumentOperand(5));  // Fp-to-sp delta.
168   __ li(t1, Operand(ExternalReference::isolate_address(isolate())));
169   __ sw(t1, CFunctionArgumentOperand(6));  // Isolate.
170   // Call Deoptimizer::New().
171   {
172     AllowExternalCallThatCantCauseGC scope(masm());
173     __ CallCFunction(ExternalReference::new_deoptimizer_function(isolate()), 6);
174   }
175 
176   // Preserve "deoptimizer" object in register v0 and get the input
177   // frame descriptor pointer to a1 (deoptimizer->input_);
178   // Move deopt-obj to a0 for call to Deoptimizer::ComputeOutputFrames() below.
179   __ mov(a0, v0);
180   __ lw(a1, MemOperand(v0, Deoptimizer::input_offset()));
181 
182   // Copy core registers into FrameDescription::registers_[kNumRegisters].
183   DCHECK(Register::kNumRegisters == kNumberOfRegisters);
184   for (int i = 0; i < kNumberOfRegisters; i++) {
185     int offset = (i * kPointerSize) + FrameDescription::registers_offset();
186     if ((saved_regs & (1 << i)) != 0) {
187       __ lw(a2, MemOperand(sp, i * kPointerSize));
188       __ sw(a2, MemOperand(a1, offset));
189     } else if (FLAG_debug_code) {
190       __ li(a2, kDebugZapValue);
191       __ sw(a2, MemOperand(a1, offset));
192     }
193   }
194 
195   int double_regs_offset = FrameDescription::double_registers_offset();
196   // Copy FPU registers to
197   // double_registers_[DoubleRegister::kNumAllocatableRegisters]
198   for (int i = 0; i < config->num_allocatable_double_registers(); ++i) {
199     int code = config->GetAllocatableDoubleCode(i);
200     int dst_offset = code * kDoubleSize + double_regs_offset;
201     int src_offset = code * kDoubleSize + kNumberOfRegisters * kPointerSize;
202     __ ldc1(f0, MemOperand(sp, src_offset));
203     __ sdc1(f0, MemOperand(a1, dst_offset));
204   }
205 
206   // Remove the bailout id and the saved registers from the stack.
207   __ Addu(sp, sp, Operand(kSavedRegistersAreaSize + (1 * kPointerSize)));
208 
209   // Compute a pointer to the unwinding limit in register a2; that is
210   // the first stack slot not part of the input frame.
211   __ lw(a2, MemOperand(a1, FrameDescription::frame_size_offset()));
212   __ Addu(a2, a2, sp);
213 
214   // Unwind the stack down to - but not including - the unwinding
215   // limit and copy the contents of the activation frame to the input
216   // frame description.
217   __ Addu(a3, a1, Operand(FrameDescription::frame_content_offset()));
218   Label pop_loop;
219   Label pop_loop_header;
220   __ BranchShort(&pop_loop_header);
221   __ bind(&pop_loop);
222   __ pop(t0);
223   __ sw(t0, MemOperand(a3, 0));
224   __ addiu(a3, a3, sizeof(uint32_t));
225   __ bind(&pop_loop_header);
226   __ BranchShort(&pop_loop, ne, a2, Operand(sp));
227 
228   // Compute the output frame in the deoptimizer.
229   __ push(a0);  // Preserve deoptimizer object across call.
230   // a0: deoptimizer object; a1: scratch.
231   __ PrepareCallCFunction(1, a1);
232   // Call Deoptimizer::ComputeOutputFrames().
233   {
234     AllowExternalCallThatCantCauseGC scope(masm());
235     __ CallCFunction(
236         ExternalReference::compute_output_frames_function(isolate()), 1);
237   }
238   __ pop(a0);  // Restore deoptimizer object (class Deoptimizer).
239 
240   __ lw(sp, MemOperand(a0, Deoptimizer::caller_frame_top_offset()));
241 
242   // Replace the current (input) frame with the output frames.
243   Label outer_push_loop, inner_push_loop,
244       outer_loop_header, inner_loop_header;
245   // Outer loop state: t0 = current "FrameDescription** output_",
246   // a1 = one past the last FrameDescription**.
247   __ lw(a1, MemOperand(a0, Deoptimizer::output_count_offset()));
248   __ lw(t0, MemOperand(a0, Deoptimizer::output_offset()));  // t0 is output_.
249   __ Lsa(a1, t0, a1, kPointerSizeLog2);
250   __ BranchShort(&outer_loop_header);
251   __ bind(&outer_push_loop);
252   // Inner loop state: a2 = current FrameDescription*, a3 = loop index.
253   __ lw(a2, MemOperand(t0, 0));  // output_[ix]
254   __ lw(a3, MemOperand(a2, FrameDescription::frame_size_offset()));
255   __ BranchShort(&inner_loop_header);
256   __ bind(&inner_push_loop);
257   __ Subu(a3, a3, Operand(sizeof(uint32_t)));
258   __ Addu(t2, a2, Operand(a3));
259   __ lw(t3, MemOperand(t2, FrameDescription::frame_content_offset()));
260   __ push(t3);
261   __ bind(&inner_loop_header);
262   __ BranchShort(&inner_push_loop, ne, a3, Operand(zero_reg));
263 
264   __ Addu(t0, t0, Operand(kPointerSize));
265   __ bind(&outer_loop_header);
266   __ BranchShort(&outer_push_loop, lt, t0, Operand(a1));
267 
268   __ lw(a1, MemOperand(a0, Deoptimizer::input_offset()));
269   for (int i = 0; i < config->num_allocatable_double_registers(); ++i) {
270     int code = config->GetAllocatableDoubleCode(i);
271     const DoubleRegister fpu_reg = DoubleRegister::from_code(code);
272     int src_offset = code * kDoubleSize + double_regs_offset;
273     __ ldc1(fpu_reg, MemOperand(a1, src_offset));
274   }
275 
276   // Push state, pc, and continuation from the last output frame.
277   __ lw(t2, MemOperand(a2, FrameDescription::state_offset()));
278   __ push(t2);
279 
280   __ lw(t2, MemOperand(a2, FrameDescription::pc_offset()));
281   __ push(t2);
282   __ lw(t2, MemOperand(a2, FrameDescription::continuation_offset()));
283   __ push(t2);
284 
285 
286   // Technically restoring 'at' should work unless zero_reg is also restored
287   // but it's safer to check for this.
288   DCHECK(!(at.bit() & restored_regs));
289   // Restore the registers from the last output frame.
290   __ mov(at, a2);
291   for (int i = kNumberOfRegisters - 1; i >= 0; i--) {
292     int offset = (i * kPointerSize) + FrameDescription::registers_offset();
293     if ((restored_regs & (1 << i)) != 0) {
294       __ lw(ToRegister(i), MemOperand(at, offset));
295     }
296   }
297 
298   __ InitializeRootRegister();
299 
300   __ pop(at);  // Get continuation, leave pc on stack.
301   __ pop(ra);
302   __ Jump(at);
303   __ stop("Unreachable.");
304 }
305 
306 
307 // Maximum size of a table entry generated below.
308 const int Deoptimizer::table_entry_size_ = 2 * Assembler::kInstrSize;
309 
GeneratePrologue()310 void Deoptimizer::TableEntryGenerator::GeneratePrologue() {
311   Assembler::BlockTrampolinePoolScope block_trampoline_pool(masm());
312 
313   // Create a sequence of deoptimization entries.
314   // Note that registers are still live when jumping to an entry.
315   Label table_start, done, done_special, trampoline_jump;
316   __ bind(&table_start);
317   int kMaxEntriesBranchReach = (1 << (kImm16Bits - 2))/
318      (table_entry_size_ /  Assembler::kInstrSize);
319 
320   if (count() <= kMaxEntriesBranchReach) {
321     // Common case.
322     for (int i = 0; i < count(); i++) {
323       Label start;
324       __ bind(&start);
325       DCHECK(is_int16(i));
326       __ BranchShort(USE_DELAY_SLOT, &done);  // Expose delay slot.
327       __ li(at, i);  // In the delay slot.
328 
329       DCHECK_EQ(table_entry_size_, masm()->SizeOfCodeGeneratedSince(&start));
330     }
331 
332     DCHECK_EQ(masm()->SizeOfCodeGeneratedSince(&table_start),
333         count() * table_entry_size_);
334     __ bind(&done);
335     __ Push(at);
336   } else {
337     // Uncommon case, the branch cannot reach.
338     // Create mini trampoline and adjust id constants to get proper value at
339     // the end of table.
340     for (int i = kMaxEntriesBranchReach; i > 1; i--) {
341       Label start;
342       __ bind(&start);
343       DCHECK(is_int16(i));
344       __ BranchShort(USE_DELAY_SLOT, &trampoline_jump);  // Expose delay slot.
345       __ li(at, - i);  // In the delay slot.
346       DCHECK_EQ(table_entry_size_, masm()->SizeOfCodeGeneratedSince(&start));
347     }
348     // Entry with id == kMaxEntriesBranchReach - 1.
349     __ bind(&trampoline_jump);
350     __ BranchShort(USE_DELAY_SLOT, &done_special);
351     __ li(at, -1);
352 
353     for (int i = kMaxEntriesBranchReach ; i < count(); i++) {
354       Label start;
355       __ bind(&start);
356       DCHECK(is_int16(i));
357       __ BranchShort(USE_DELAY_SLOT, &done);  // Expose delay slot.
358       __ li(at, i);  // In the delay slot.
359     }
360 
361     DCHECK_EQ(masm()->SizeOfCodeGeneratedSince(&table_start),
362         count() * table_entry_size_);
363     __ bind(&done_special);
364     __ addiu(at, at, kMaxEntriesBranchReach);
365     __ bind(&done);
366     __ Push(at);
367   }
368 }
369 
370 
SetCallerPc(unsigned offset,intptr_t value)371 void FrameDescription::SetCallerPc(unsigned offset, intptr_t value) {
372   SetFrameSlot(offset, value);
373 }
374 
375 
SetCallerFp(unsigned offset,intptr_t value)376 void FrameDescription::SetCallerFp(unsigned offset, intptr_t value) {
377   SetFrameSlot(offset, value);
378 }
379 
380 
SetCallerConstantPool(unsigned offset,intptr_t value)381 void FrameDescription::SetCallerConstantPool(unsigned offset, intptr_t value) {
382   // No embedded constant pool support.
383   UNREACHABLE();
384 }
385 
386 
387 #undef __
388 
389 
390 }  // namespace internal
391 }  // namespace v8
392