8c4b776efe2c5ea0774551fe996f0e242d909e4c
[platform/upstream/nodejs.git] / deps / v8 / src / arm64 / deoptimizer-arm64.cc
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.
4
5 #include "src/v8.h"
6
7 #include "src/codegen.h"
8 #include "src/deoptimizer.h"
9 #include "src/full-codegen.h"
10 #include "src/safepoint-table.h"
11
12
13 namespace v8 {
14 namespace internal {
15
16
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;
21 }
22
23
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.
27 }
28
29
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();
34
35   // TODO(jkummerow): if (FLAG_zap_code_space), make the code object's
36   // entry sequence unusable (see other architectures).
37
38   DeoptimizationInputData* deopt_data =
39       DeoptimizationInputData::cast(code->deoptimization_data());
40   Address code_start_address = code->instruction_start();
41 #ifdef DEBUG
42   Address prev_call_address = NULL;
43 #endif
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;
48
49     Address call_address = code_start_address + deopt_data->Pc(i)->value();
50     Address deopt_entry = GetDeoptimizationEntry(isolate, i, LAZY);
51
52     PatchingAssembler patcher(call_address, patch_size() / kInstructionSize);
53     patcher.ldr_pcrel(ip0, (2 * kInstructionSize) >> kLoadLiteralScaleLog2);
54     patcher.blr(ip0);
55     patcher.dc64(reinterpret_cast<intptr_t>(deopt_entry));
56
57     DCHECK((prev_call_address == NULL) ||
58            (call_address >= prev_call_address + patch_size()));
59     DCHECK(call_address + patch_size() <= code->instruction_end());
60 #ifdef DEBUG
61     prev_call_address = call_address;
62 #endif
63   }
64 }
65
66
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);
73   }
74
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()));
78
79   for (int i = 0; i < DoubleRegister::NumAllocatableRegisters(); i++) {
80     input_->SetDoubleRegister(i, 0.0);
81   }
82
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));
86   }
87 }
88
89
90 bool Deoptimizer::HasAlignmentPadding(JSFunction* function) {
91   // There is no dynamic alignment padding on ARM64 in the input frame.
92   return false;
93 }
94
95
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);
104 }
105
106
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);
111   }
112 }
113
114
115
116 #define __ masm()->
117
118 void Deoptimizer::EntryGenerator::Generate() {
119   GeneratePrologue();
120
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.
124
125   // Save all allocatable floating point registers.
126   CPURegList saved_fp_registers(CPURegister::kFPRegister, kDRegSizeInBits,
127                                 FPRegister::kAllocatableFPRegisters);
128   __ PushCPURegList(saved_fp_registers);
129
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);
134
135   __ Mov(x3, Operand(ExternalReference(Isolate::kCEntryFPAddress, isolate())));
136   __ Str(fp, MemOperand(x3));
137
138   const int kSavedRegistersAreaSize =
139       (saved_registers.Count() * kXRegSize) +
140       (saved_fp_registers.Count() * kDRegSize);
141
142   // Floating point registers are saved on the stack above core registers.
143   const int kFPRegistersOffset = saved_registers.Count() * kXRegSize;
144
145   // Get the bailout id from the stack.
146   Register bailout_id = x2;
147   __ Peek(bailout_id, kSavedRegistersAreaSize);
148
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);
158
159   // Allocate a new deoptimizer object.
160   __ Ldr(x0, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
161   __ Mov(x1, type());
162   // Following arguments are already loaded:
163   //  - x2: bailout id
164   //  - x3: code object address
165   //  - x4: fp-to-sp delta
166   __ Mov(x5, ExternalReference::isolate_address(isolate()));
167
168   {
169     // Call Deoptimizer::New().
170     AllowExternalCallThatCantCauseGC scope(masm());
171     __ CallCFunction(ExternalReference::new_deoptimizer_function(isolate()), 6);
172   }
173
174   // Preserve "deoptimizer" object in register x0.
175   Register deoptimizer = x0;
176
177   // Get the input frame descriptor pointer.
178   __ Ldr(x1, MemOperand(deoptimizer, Deoptimizer::input_offset()));
179
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));
188   }
189
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() +
193         (i * kDoubleSize);
194     int src_offset = kFPRegistersOffset + (i * kDoubleSize);
195     __ Peek(x2, src_offset);
196     __ Str(x2, MemOperand(x1, dst_offset));
197   }
198
199   // Remove the bailout id and the saved registers from the stack.
200   __ Drop(1 + (kSavedRegistersAreaSize / kXRegSize));
201
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());
207
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());
212   Label pop_loop;
213   Label pop_loop_header;
214   __ B(&pop_loop_header);
215   __ Bind(&pop_loop);
216   __ Pop(x4);
217   __ Str(x4, MemOperand(x3, kPointerSize, PostIndex));
218   __ Bind(&pop_loop_header);
219   __ Cmp(unwind_limit, __ StackPointer());
220   __ B(ne, &pop_loop);
221
222   // Compute the output frame in the deoptimizer.
223   __ Push(x0);  // Preserve deoptimizer object across call.
224
225   {
226     // Call Deoptimizer::ComputeOutputFrames().
227     AllowExternalCallThatCantCauseGC scope(masm());
228     __ CallCFunction(
229         ExternalReference::compute_output_frames_function(isolate()), 1);
230   }
231   __ Pop(x4);  // Restore deoptimizer object (class Deoptimizer).
232
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);
240
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);
246
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()));
251   __ Push(x7);
252   __ Bind(&inner_loop_header);
253   __ Cbnz(x3, &inner_push_loop);
254
255   __ Add(x0, x0, kPointerSize);
256   __ Bind(&outer_loop_header);
257   __ Cmp(x0, x1);
258   __ B(lt, &outer_push_loop);
259
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;
269   }
270
271   // Push state from the last output frame.
272   __ Ldr(x6, MemOperand(current_frame, FrameDescription::state_offset()));
273   __ Push(x6);
274
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
278   // ARM code.
279
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).
283
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);
291
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);
296
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));
303   }
304
305   __ Ldr(continuation, MemOperand(last_output_frame,
306                                   FrameDescription::continuation_offset()));
307   __ Ldr(lr, MemOperand(last_output_frame, FrameDescription::pc_offset()));
308   __ InitializeRootRegister();
309   __ Br(continuation);
310 }
311
312
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;
316
317
318 void Deoptimizer::TableEntryGenerator::GeneratePrologue() {
319   UseScratchRegisterScope temps(masm());
320   Register entry_id = temps.AcquireX();
321
322   // Create a sequence of deoptimization entries.
323   // Note that registers are still live when jumping to an entry.
324   Label done;
325   {
326     InstructionAccurateScope scope(masm());
327
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));
332
333     for (int i = 0; i < count(); i++) {
334       int start = masm()->pc_offset();
335       USE(start);
336       __ movz(entry_id, i);
337       __ b(&done);
338       DCHECK(masm()->pc_offset() - start == table_entry_size_);
339     }
340   }
341   __ Bind(&done);
342   __ Push(entry_id);
343 }
344
345
346 void FrameDescription::SetCallerPc(unsigned offset, intptr_t value) {
347   SetFrameSlot(offset, value);
348 }
349
350
351 void FrameDescription::SetCallerFp(unsigned offset, intptr_t value) {
352   SetFrameSlot(offset, value);
353 }
354
355
356 void FrameDescription::SetCallerConstantPool(unsigned offset, intptr_t value) {
357   // No out-of-line constant pool support.
358   UNREACHABLE();
359 }
360
361
362 #undef __
363
364 } }  // namespace v8::internal