1 // Copyright 2013 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"
17 int Deoptimizer::patch_size() {
18 // Size of the code used to patch lazy bailout points.
19 // Patching is done by Deoptimizer::DeoptimizeFunction.
20 return 4 * kInstructionSize;
24 void Deoptimizer::EnsureRelocSpaceForLazyDeoptimization(Handle<Code> code) {
25 // Empty because there is no need for relocation information for the code
26 // patching in Deoptimizer::PatchCodeForDeoptimization below.
30 void Deoptimizer::PatchCodeForDeoptimization(Isolate* isolate, Code* code) {
31 // Invalidate the relocation information, as it will become invalid by the
32 // code patching below, and is not needed any more.
33 code->InvalidateRelocation();
35 // TODO(jkummerow): if (FLAG_zap_code_space), make the code object's
36 // entry sequence unusable (see other architectures).
38 DeoptimizationInputData* deopt_data =
39 DeoptimizationInputData::cast(code->deoptimization_data());
40 Address code_start_address = code->instruction_start();
42 Address prev_call_address = NULL;
44 // For each LLazyBailout instruction insert a call to the corresponding
45 // deoptimization entry.
46 for (int i = 0; i < deopt_data->DeoptCount(); i++) {
47 if (deopt_data->Pc(i)->value() == -1) continue;
49 Address call_address = code_start_address + deopt_data->Pc(i)->value();
50 Address deopt_entry = GetDeoptimizationEntry(isolate, i, LAZY);
52 PatchingAssembler patcher(call_address, patch_size() / kInstructionSize);
53 patcher.ldr_pcrel(ip0, (2 * kInstructionSize) >> kLoadLiteralScaleLog2);
55 patcher.dc64(reinterpret_cast<intptr_t>(deopt_entry));
57 DCHECK((prev_call_address == NULL) ||
58 (call_address >= prev_call_address + patch_size()));
59 DCHECK(call_address + patch_size() <= code->instruction_end());
61 prev_call_address = call_address;
67 void Deoptimizer::FillInputFrame(Address tos, JavaScriptFrame* frame) {
68 // Set the register values. The values are not important as there are no
69 // callee saved registers in JavaScript frames, so all registers are
70 // spilled. Registers fp and sp are set to the correct values though.
71 for (int i = 0; i < Register::NumRegisters(); i++) {
72 input_->SetRegister(i, 0);
75 // TODO(all): Do we also need to set a value to csp?
76 input_->SetRegister(jssp.code(), reinterpret_cast<intptr_t>(frame->sp()));
77 input_->SetRegister(fp.code(), reinterpret_cast<intptr_t>(frame->fp()));
79 for (int i = 0; i < DoubleRegister::NumAllocatableRegisters(); i++) {
80 input_->SetDoubleRegister(i, 0.0);
83 // Fill the frame content from the actual data on the frame.
84 for (unsigned i = 0; i < input_->GetFrameSize(); i += kPointerSize) {
85 input_->SetFrameSlot(i, Memory::uint64_at(tos + i));
90 bool Deoptimizer::HasAlignmentPadding(JSFunction* function) {
91 // There is no dynamic alignment padding on ARM64 in the input frame.
96 void Deoptimizer::SetPlatformCompiledStubRegisters(
97 FrameDescription* output_frame, CodeStubDescriptor* descriptor) {
98 ApiFunction function(descriptor->deoptimization_handler());
99 ExternalReference xref(&function, ExternalReference::BUILTIN_CALL, isolate_);
100 intptr_t handler = reinterpret_cast<intptr_t>(xref.address());
101 int params = descriptor->GetHandlerParameterCount();
102 output_frame->SetRegister(x0.code(), params);
103 output_frame->SetRegister(x1.code(), handler);
107 void Deoptimizer::CopyDoubleRegisters(FrameDescription* output_frame) {
108 for (int i = 0; i < DoubleRegister::kMaxNumRegisters; ++i) {
109 double double_value = input_->GetDoubleRegister(i);
110 output_frame->SetDoubleRegister(i, double_value);
118 void Deoptimizer::TableEntryGenerator::Generate() {
121 // TODO(all): This code needs to be revisited. We probably only need to save
122 // caller-saved registers here. Callee-saved registers can be stored directly
123 // in the input frame.
125 // Save all allocatable floating point registers.
126 CPURegList saved_fp_registers(CPURegister::kFPRegister, kDRegSizeInBits,
127 FPRegister::kAllocatableFPRegisters);
128 __ PushCPURegList(saved_fp_registers);
130 // We save all the registers expcept jssp, sp and lr.
131 CPURegList saved_registers(CPURegister::kRegister, kXRegSizeInBits, 0, 27);
132 saved_registers.Combine(fp);
133 __ PushCPURegList(saved_registers);
135 __ Mov(x3, Operand(ExternalReference(Isolate::kCEntryFPAddress, isolate())));
136 __ Str(fp, MemOperand(x3));
138 const int kSavedRegistersAreaSize =
139 (saved_registers.Count() * kXRegSize) +
140 (saved_fp_registers.Count() * kDRegSize);
142 // Floating point registers are saved on the stack above core registers.
143 const int kFPRegistersOffset = saved_registers.Count() * kXRegSize;
145 // Get the bailout id from the stack.
146 Register bailout_id = x2;
147 __ Peek(bailout_id, kSavedRegistersAreaSize);
149 Register code_object = x3;
150 Register fp_to_sp = x4;
151 // Get the address of the location in the code object. This is the return
152 // address for lazy deoptimization.
153 __ Mov(code_object, lr);
154 // Compute the fp-to-sp delta, and correct one word for bailout id.
155 __ Add(fp_to_sp, masm()->StackPointer(),
156 kSavedRegistersAreaSize + (1 * kPointerSize));
157 __ Sub(fp_to_sp, fp, fp_to_sp);
159 // Allocate a new deoptimizer object.
160 __ Ldr(x0, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
162 // Following arguments are already loaded:
164 // - x3: code object address
165 // - x4: fp-to-sp delta
166 __ Mov(x5, ExternalReference::isolate_address(isolate()));
169 // Call Deoptimizer::New().
170 AllowExternalCallThatCantCauseGC scope(masm());
171 __ CallCFunction(ExternalReference::new_deoptimizer_function(isolate()), 6);
174 // Preserve "deoptimizer" object in register x0.
175 Register deoptimizer = x0;
177 // Get the input frame descriptor pointer.
178 __ Ldr(x1, MemOperand(deoptimizer, Deoptimizer::input_offset()));
180 // Copy core registers into the input frame.
181 CPURegList copy_to_input = saved_registers;
182 for (int i = 0; i < saved_registers.Count(); i++) {
183 __ Peek(x2, i * kPointerSize);
184 CPURegister current_reg = copy_to_input.PopLowestIndex();
185 int offset = (current_reg.code() * kPointerSize) +
186 FrameDescription::registers_offset();
187 __ Str(x2, MemOperand(x1, offset));
190 // Copy FP registers to the input frame.
191 for (int i = 0; i < saved_fp_registers.Count(); i++) {
192 int dst_offset = FrameDescription::double_registers_offset() +
194 int src_offset = kFPRegistersOffset + (i * kDoubleSize);
195 __ Peek(x2, src_offset);
196 __ Str(x2, MemOperand(x1, dst_offset));
199 // Remove the bailout id and the saved registers from the stack.
200 __ Drop(1 + (kSavedRegistersAreaSize / kXRegSize));
202 // Compute a pointer to the unwinding limit in register x2; that is
203 // the first stack slot not part of the input frame.
204 Register unwind_limit = x2;
205 __ Ldr(unwind_limit, MemOperand(x1, FrameDescription::frame_size_offset()));
206 __ Add(unwind_limit, unwind_limit, __ StackPointer());
208 // Unwind the stack down to - but not including - the unwinding
209 // limit and copy the contents of the activation frame to the input
210 // frame description.
211 __ Add(x3, x1, FrameDescription::frame_content_offset());
213 Label pop_loop_header;
214 __ B(&pop_loop_header);
217 __ Str(x4, MemOperand(x3, kPointerSize, PostIndex));
218 __ Bind(&pop_loop_header);
219 __ Cmp(unwind_limit, __ StackPointer());
222 // Compute the output frame in the deoptimizer.
223 __ Push(x0); // Preserve deoptimizer object across call.
226 // Call Deoptimizer::ComputeOutputFrames().
227 AllowExternalCallThatCantCauseGC scope(masm());
229 ExternalReference::compute_output_frames_function(isolate()), 1);
231 __ Pop(x4); // Restore deoptimizer object (class Deoptimizer).
233 // Replace the current (input) frame with the output frames.
234 Label outer_push_loop, inner_push_loop,
235 outer_loop_header, inner_loop_header;
236 __ Ldrsw(x1, MemOperand(x4, Deoptimizer::output_count_offset()));
237 __ Ldr(x0, MemOperand(x4, Deoptimizer::output_offset()));
238 __ Add(x1, x0, Operand(x1, LSL, kPointerSizeLog2));
239 __ B(&outer_loop_header);
241 __ Bind(&outer_push_loop);
242 Register current_frame = x2;
243 __ Ldr(current_frame, MemOperand(x0, 0));
244 __ Ldr(x3, MemOperand(current_frame, FrameDescription::frame_size_offset()));
245 __ B(&inner_loop_header);
247 __ Bind(&inner_push_loop);
248 __ Sub(x3, x3, kPointerSize);
249 __ Add(x6, current_frame, x3);
250 __ Ldr(x7, MemOperand(x6, FrameDescription::frame_content_offset()));
252 __ Bind(&inner_loop_header);
253 __ Cbnz(x3, &inner_push_loop);
255 __ Add(x0, x0, kPointerSize);
256 __ Bind(&outer_loop_header);
258 __ B(lt, &outer_push_loop);
260 __ Ldr(x1, MemOperand(x4, Deoptimizer::input_offset()));
261 DCHECK(!saved_fp_registers.IncludesAliasOf(crankshaft_fp_scratch) &&
262 !saved_fp_registers.IncludesAliasOf(fp_zero) &&
263 !saved_fp_registers.IncludesAliasOf(fp_scratch));
264 int src_offset = FrameDescription::double_registers_offset();
265 while (!saved_fp_registers.IsEmpty()) {
266 const CPURegister reg = saved_fp_registers.PopLowestIndex();
267 __ Ldr(reg, MemOperand(x1, src_offset));
268 src_offset += kDoubleSize;
271 // Push state from the last output frame.
272 __ Ldr(x6, MemOperand(current_frame, FrameDescription::state_offset()));
275 // TODO(all): ARM copies a lot (if not all) of the last output frame onto the
276 // stack, then pops it all into registers. Here, we try to load it directly
277 // into the relevant registers. Is this correct? If so, we should improve the
280 // TODO(all): This code needs to be revisited, We probably don't need to
281 // restore all the registers as fullcodegen does not keep live values in
282 // registers (note that at least fp must be restored though).
284 // Restore registers from the last output frame.
285 // Note that lr is not in the list of saved_registers and will be restored
286 // later. We can use it to hold the address of last output frame while
287 // reloading the other registers.
288 DCHECK(!saved_registers.IncludesAliasOf(lr));
289 Register last_output_frame = lr;
290 __ Mov(last_output_frame, current_frame);
292 // We don't need to restore x7 as it will be clobbered later to hold the
293 // continuation address.
294 Register continuation = x7;
295 saved_registers.Remove(continuation);
297 while (!saved_registers.IsEmpty()) {
298 // TODO(all): Look for opportunities to optimize this by using ldp.
299 CPURegister current_reg = saved_registers.PopLowestIndex();
300 int offset = (current_reg.code() * kPointerSize) +
301 FrameDescription::registers_offset();
302 __ Ldr(current_reg, MemOperand(last_output_frame, offset));
305 __ Ldr(continuation, MemOperand(last_output_frame,
306 FrameDescription::continuation_offset()));
307 __ Ldr(lr, MemOperand(last_output_frame, FrameDescription::pc_offset()));
308 __ InitializeRootRegister();
313 // Size of an entry of the second level deopt table.
314 // This is the code size generated by GeneratePrologue for one entry.
315 const int Deoptimizer::table_entry_size_ = 2 * kInstructionSize;
318 void Deoptimizer::TableEntryGenerator::GeneratePrologue() {
319 UseScratchRegisterScope temps(masm());
320 Register entry_id = temps.AcquireX();
322 // Create a sequence of deoptimization entries.
323 // Note that registers are still live when jumping to an entry.
326 InstructionAccurateScope scope(masm());
328 // The number of entry will never exceed kMaxNumberOfEntries.
329 // As long as kMaxNumberOfEntries is a valid 16 bits immediate you can use
330 // a movz instruction to load the entry id.
331 DCHECK(is_uint16(Deoptimizer::kMaxNumberOfEntries));
333 for (int i = 0; i < count(); i++) {
334 int start = masm()->pc_offset();
336 __ movz(entry_id, i);
338 DCHECK(masm()->pc_offset() - start == table_entry_size_);
346 void FrameDescription::SetCallerPc(unsigned offset, intptr_t value) {
347 SetFrameSlot(offset, value);
351 void FrameDescription::SetCallerFp(unsigned offset, intptr_t value) {
352 SetFrameSlot(offset, value);
356 void FrameDescription::SetCallerConstantPool(unsigned offset, intptr_t value) {
357 // No out-of-line constant pool support.
364 } } // namespace v8::internal