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.
7 #include "src/codegen.h"
8 #include "src/deoptimizer.h"
9 #include "src/full-codegen.h"
10 #include "src/safepoint-table.h"
16 int Deoptimizer::patch_size() {
17 const int kCallInstructionSizeInWords = 6;
18 return kCallInstructionSizeInWords * Assembler::kInstrSize;
22 void Deoptimizer::EnsureRelocSpaceForLazyDeoptimization(Handle<Code> code) {
23 // Empty because there is no need for relocation information for the code
24 // patching in Deoptimizer::PatchCodeForDeoptimization below.
28 void Deoptimizer::PatchCodeForDeoptimization(Isolate* isolate, Code* code) {
29 Address code_start_address = code->instruction_start();
30 // Invalidate the relocation information, as it will become invalid by the
31 // code patching below, and is not needed any more.
32 code->InvalidateRelocation();
34 if (FLAG_zap_code_space) {
35 // Fail hard and early if we enter this code object again.
36 byte* pointer = code->FindCodeAgeSequence();
37 if (pointer != NULL) {
38 pointer += kNoCodeAgeSequenceLength;
40 pointer = code->instruction_start();
42 CodePatcher patcher(pointer, 1);
43 patcher.masm()->break_(0xCC);
45 DeoptimizationInputData* data =
46 DeoptimizationInputData::cast(code->deoptimization_data());
47 int osr_offset = data->OsrPcOffset()->value();
49 CodePatcher osr_patcher(code->instruction_start() + osr_offset, 1);
50 osr_patcher.masm()->break_(0xCC);
54 DeoptimizationInputData* deopt_data =
55 DeoptimizationInputData::cast(code->deoptimization_data());
57 Address prev_call_address = NULL;
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,
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(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());
77 prev_call_address = call_address;
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.
88 for (int i = 0; i < Register::kNumRegisters; i++) {
89 input_->SetRegister(i, i * 4);
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::NumAllocatableRegisters(); i++) {
94 input_->SetDoubleRegister(i, 0.0);
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));
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);
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);
123 bool Deoptimizer::HasAlignmentPadding(JSFunction* function) {
124 // There is no dynamic alignment padding on MIPS in the input frame.
132 // This code tries to be close to ia32 code so that any changes can be
134 void Deoptimizer::TableEntryGenerator::Generate() {
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;
141 RegList restored_regs = kJSCallerSaved | kCalleeSaved;
142 RegList saved_regs = restored_regs | sp.bit() | ra.bit();
144 const int kDoubleRegsSize =
145 kDoubleSize * FPURegister::kMaxNumAllocatableRegisters;
147 // Save all FPU registers before messing with them.
148 __ Dsubu(sp, sp, Operand(kDoubleRegsSize));
149 for (int i = 0; i < FPURegister::kMaxNumAllocatableRegisters; ++i) {
150 FPURegister fpu_reg = FPURegister::FromAllocationIndex(i);
151 int offset = i * kDoubleSize;
152 __ sdc1(fpu_reg, MemOperand(sp, offset));
155 // Push saved_regs (needed to populate FrameDescription::registers_).
156 // Leave gaps for other registers.
157 __ Dsubu(sp, sp, kNumberOfRegisters * kPointerSize);
158 for (int16_t i = kNumberOfRegisters - 1; i >= 0; i--) {
159 if ((saved_regs & (1 << i)) != 0) {
160 __ sd(ToRegister(i), MemOperand(sp, kPointerSize * i));
164 __ li(a2, Operand(ExternalReference(Isolate::kCEntryFPAddress, isolate())));
165 __ sd(fp, MemOperand(a2));
167 const int kSavedRegistersAreaSize =
168 (kNumberOfRegisters * kPointerSize) + kDoubleRegsSize;
170 // Get the bailout id from the stack.
171 __ ld(a2, MemOperand(sp, kSavedRegistersAreaSize));
173 // Get the address of the location in the code object (a3) (return
174 // address for lazy deoptimization) and compute the fp-to-sp delta in
177 // Correct one word for bailout id.
178 __ Daddu(a4, sp, Operand(kSavedRegistersAreaSize + (1 * kPointerSize)));
180 __ Dsubu(a4, fp, a4);
182 // Allocate a new deoptimizer object.
183 __ PrepareCallCFunction(6, a5);
184 // Pass six arguments, according to O32 or n64 ABI. a0..a3 are same for both.
185 __ li(a1, Operand(type())); // bailout type,
186 __ ld(a0, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
187 // a2: bailout id already loaded.
188 // a3: code address or 0 already loaded.
189 if (kMipsAbi == kN64) {
190 // a4: already has fp-to-sp delta.
191 __ li(a5, Operand(ExternalReference::isolate_address(isolate())));
193 // Pass four arguments in a0 to a3 and fifth & sixth arguments on stack.
194 __ sd(a4, CFunctionArgumentOperand(5)); // Fp-to-sp delta.
195 __ li(a5, Operand(ExternalReference::isolate_address(isolate())));
196 __ sd(a5, CFunctionArgumentOperand(6)); // Isolate.
198 // Call Deoptimizer::New().
200 AllowExternalCallThatCantCauseGC scope(masm());
201 __ CallCFunction(ExternalReference::new_deoptimizer_function(isolate()), 6);
204 // Preserve "deoptimizer" object in register v0 and get the input
205 // frame descriptor pointer to a1 (deoptimizer->input_);
206 // Move deopt-obj to a0 for call to Deoptimizer::ComputeOutputFrames() below.
208 __ ld(a1, MemOperand(v0, Deoptimizer::input_offset()));
210 // Copy core registers into FrameDescription::registers_[kNumRegisters].
211 DCHECK(Register::kNumRegisters == kNumberOfRegisters);
212 for (int i = 0; i < kNumberOfRegisters; i++) {
213 int offset = (i * kPointerSize) + FrameDescription::registers_offset();
214 if ((saved_regs & (1 << i)) != 0) {
215 __ ld(a2, MemOperand(sp, i * kPointerSize));
216 __ sd(a2, MemOperand(a1, offset));
217 } else if (FLAG_debug_code) {
218 __ li(a2, kDebugZapValue);
219 __ sd(a2, MemOperand(a1, offset));
223 int double_regs_offset = FrameDescription::double_registers_offset();
224 // Copy FPU registers to
225 // double_registers_[DoubleRegister::kNumAllocatableRegisters]
226 for (int i = 0; i < FPURegister::NumAllocatableRegisters(); ++i) {
227 int dst_offset = i * kDoubleSize + double_regs_offset;
228 int src_offset = i * kDoubleSize + kNumberOfRegisters * kPointerSize;
229 __ ldc1(f0, MemOperand(sp, src_offset));
230 __ sdc1(f0, MemOperand(a1, dst_offset));
233 // Remove the bailout id and the saved registers from the stack.
234 __ Daddu(sp, sp, Operand(kSavedRegistersAreaSize + (1 * kPointerSize)));
236 // Compute a pointer to the unwinding limit in register a2; that is
237 // the first stack slot not part of the input frame.
238 __ ld(a2, MemOperand(a1, FrameDescription::frame_size_offset()));
239 __ Daddu(a2, a2, sp);
241 // Unwind the stack down to - but not including - the unwinding
242 // limit and copy the contents of the activation frame to the input
243 // frame description.
244 __ Daddu(a3, a1, Operand(FrameDescription::frame_content_offset()));
246 Label pop_loop_header;
247 __ BranchShort(&pop_loop_header);
250 __ sd(a4, MemOperand(a3, 0));
251 __ daddiu(a3, a3, sizeof(uint64_t));
252 __ bind(&pop_loop_header);
253 __ BranchShort(&pop_loop, ne, a2, Operand(sp));
254 // Compute the output frame in the deoptimizer.
255 __ push(a0); // Preserve deoptimizer object across call.
256 // a0: deoptimizer object; a1: scratch.
257 __ PrepareCallCFunction(1, a1);
258 // Call Deoptimizer::ComputeOutputFrames().
260 AllowExternalCallThatCantCauseGC scope(masm());
262 ExternalReference::compute_output_frames_function(isolate()), 1);
264 __ pop(a0); // Restore deoptimizer object (class Deoptimizer).
266 // Replace the current (input) frame with the output frames.
267 Label outer_push_loop, inner_push_loop,
268 outer_loop_header, inner_loop_header;
269 // Outer loop state: a4 = current "FrameDescription** output_",
270 // a1 = one past the last FrameDescription**.
271 __ lw(a1, MemOperand(a0, Deoptimizer::output_count_offset()));
272 __ ld(a4, MemOperand(a0, Deoptimizer::output_offset())); // a4 is output_.
273 __ dsll(a1, a1, kPointerSizeLog2); // Count to offset.
274 __ daddu(a1, a4, a1); // a1 = one past the last FrameDescription**.
275 __ BranchShort(&outer_loop_header);
276 __ bind(&outer_push_loop);
277 // Inner loop state: a2 = current FrameDescription*, a3 = loop index.
278 __ ld(a2, MemOperand(a4, 0)); // output_[ix]
279 __ ld(a3, MemOperand(a2, FrameDescription::frame_size_offset()));
280 __ BranchShort(&inner_loop_header);
281 __ bind(&inner_push_loop);
282 __ Dsubu(a3, a3, Operand(sizeof(uint64_t)));
283 __ Daddu(a6, a2, Operand(a3));
284 __ ld(a7, MemOperand(a6, FrameDescription::frame_content_offset()));
286 __ bind(&inner_loop_header);
287 __ BranchShort(&inner_push_loop, ne, a3, Operand(zero_reg));
289 __ Daddu(a4, a4, Operand(kPointerSize));
290 __ bind(&outer_loop_header);
291 __ BranchShort(&outer_push_loop, lt, a4, Operand(a1));
293 __ ld(a1, MemOperand(a0, Deoptimizer::input_offset()));
294 for (int i = 0; i < FPURegister::kMaxNumAllocatableRegisters; ++i) {
295 const FPURegister fpu_reg = FPURegister::FromAllocationIndex(i);
296 int src_offset = i * kDoubleSize + double_regs_offset;
297 __ ldc1(fpu_reg, MemOperand(a1, src_offset));
300 // Push state, pc, and continuation from the last output frame.
301 __ ld(a6, MemOperand(a2, FrameDescription::state_offset()));
304 __ ld(a6, MemOperand(a2, FrameDescription::pc_offset()));
306 __ ld(a6, MemOperand(a2, FrameDescription::continuation_offset()));
310 // Technically restoring 'at' should work unless zero_reg is also restored
311 // but it's safer to check for this.
312 DCHECK(!(at.bit() & restored_regs));
313 // Restore the registers from the last output frame.
315 for (int i = kNumberOfRegisters - 1; i >= 0; i--) {
316 int offset = (i * kPointerSize) + FrameDescription::registers_offset();
317 if ((restored_regs & (1 << i)) != 0) {
318 __ ld(ToRegister(i), MemOperand(at, offset));
322 __ InitializeRootRegister();
324 __ pop(at); // Get continuation, leave pc on stack.
327 __ stop("Unreachable.");
331 // Maximum size of a table entry generated below.
332 const int Deoptimizer::table_entry_size_ = 2 * Assembler::kInstrSize;
334 void Deoptimizer::TableEntryGenerator::GeneratePrologue() {
335 Assembler::BlockTrampolinePoolScope block_trampoline_pool(masm());
337 // Create a sequence of deoptimization entries.
338 // Note that registers are still live when jumping to an entry.
339 Label table_start, done, done_special, trampoline_jump;
340 __ bind(&table_start);
341 int kMaxEntriesBranchReach =
342 (1 << (kImm16Bits - 2)) / (table_entry_size_ / Assembler::kInstrSize);
344 if (count() <= kMaxEntriesBranchReach) {
346 for (int i = 0; i < count(); i++) {
350 __ BranchShort(USE_DELAY_SLOT, &done); // Expose delay slot.
351 __ li(at, i); // In the delay slot.
353 DCHECK_EQ(table_entry_size_, masm()->SizeOfCodeGeneratedSince(&start));
356 DCHECK_EQ(masm()->SizeOfCodeGeneratedSince(&table_start),
357 count() * table_entry_size_);
361 // Uncommon case, the branch cannot reach.
362 // Create mini trampoline and adjust id constants to get proper value at
364 for (int i = kMaxEntriesBranchReach; i > 1; i--) {
368 __ BranchShort(USE_DELAY_SLOT, &trampoline_jump); // Expose delay slot.
369 __ li(at, -i); // In the delay slot.
370 DCHECK_EQ(table_entry_size_, masm()->SizeOfCodeGeneratedSince(&start));
372 // Entry with id == kMaxEntriesBranchReach - 1.
373 __ bind(&trampoline_jump);
374 __ BranchShort(USE_DELAY_SLOT, &done_special);
377 for (int i = kMaxEntriesBranchReach; i < count(); i++) {
381 __ Branch(USE_DELAY_SLOT, &done); // Expose delay slot.
382 __ li(at, i); // In the delay slot.
385 DCHECK_EQ(masm()->SizeOfCodeGeneratedSince(&table_start),
386 count() * table_entry_size_);
387 __ bind(&done_special);
388 __ daddiu(at, at, kMaxEntriesBranchReach);
395 void FrameDescription::SetCallerPc(unsigned offset, intptr_t value) {
396 SetFrameSlot(offset, value);
400 void FrameDescription::SetCallerFp(unsigned offset, intptr_t value) {
401 SetFrameSlot(offset, value);
405 void FrameDescription::SetCallerConstantPool(unsigned offset, intptr_t value) {
406 // No embedded constant pool support.
414 } // namespace internal