Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / 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 "v8.h"
6
7 #include "codegen.h"
8 #include "deoptimizer.h"
9 #include "full-codegen.h"
10 #include "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
25 void Deoptimizer::PatchCodeForDeoptimization(Isolate* isolate, Code* code) {
26   // Invalidate the relocation information, as it will become invalid by the
27   // code patching below, and is not needed any more.
28   code->InvalidateRelocation();
29
30   // TODO(jkummerow): if (FLAG_zap_code_space), make the code object's
31   // entry sequence unusable (see other architectures).
32
33   DeoptimizationInputData* deopt_data =
34       DeoptimizationInputData::cast(code->deoptimization_data());
35   SharedFunctionInfo* shared =
36       SharedFunctionInfo::cast(deopt_data->SharedFunctionInfo());
37   shared->EvictFromOptimizedCodeMap(code, "deoptimized code");
38   Address code_start_address = code->instruction_start();
39 #ifdef DEBUG
40   Address prev_call_address = NULL;
41 #endif
42   // For each LLazyBailout instruction insert a call to the corresponding
43   // deoptimization entry.
44   for (int i = 0; i < deopt_data->DeoptCount(); i++) {
45     if (deopt_data->Pc(i)->value() == -1) continue;
46
47     Address call_address = code_start_address + deopt_data->Pc(i)->value();
48     Address deopt_entry = GetDeoptimizationEntry(isolate, i, LAZY);
49
50     PatchingAssembler patcher(call_address, patch_size() / kInstructionSize);
51     patcher.LoadLiteral(ip0, 2 * kInstructionSize);
52     patcher.blr(ip0);
53     patcher.dc64(reinterpret_cast<intptr_t>(deopt_entry));
54
55     ASSERT((prev_call_address == NULL) ||
56            (call_address >= prev_call_address + patch_size()));
57     ASSERT(call_address + patch_size() <= code->instruction_end());
58 #ifdef DEBUG
59     prev_call_address = call_address;
60 #endif
61   }
62 }
63
64
65 void Deoptimizer::FillInputFrame(Address tos, JavaScriptFrame* frame) {
66   // Set the register values. The values are not important as there are no
67   // callee saved registers in JavaScript frames, so all registers are
68   // spilled. Registers fp and sp are set to the correct values though.
69   for (int i = 0; i < Register::NumRegisters(); i++) {
70     input_->SetRegister(i, 0);
71   }
72
73   // TODO(all): Do we also need to set a value to csp?
74   input_->SetRegister(jssp.code(), reinterpret_cast<intptr_t>(frame->sp()));
75   input_->SetRegister(fp.code(), reinterpret_cast<intptr_t>(frame->fp()));
76
77   for (int i = 0; i < DoubleRegister::NumAllocatableRegisters(); i++) {
78     input_->SetDoubleRegister(i, 0.0);
79   }
80
81   // Fill the frame content from the actual data on the frame.
82   for (unsigned i = 0; i < input_->GetFrameSize(); i += kPointerSize) {
83     input_->SetFrameSlot(i, Memory::uint64_at(tos + i));
84   }
85 }
86
87
88 bool Deoptimizer::HasAlignmentPadding(JSFunction* function) {
89   // There is no dynamic alignment padding on ARM64 in the input frame.
90   return false;
91 }
92
93
94 void Deoptimizer::SetPlatformCompiledStubRegisters(
95     FrameDescription* output_frame, CodeStubInterfaceDescriptor* descriptor) {
96   ApiFunction function(descriptor->deoptimization_handler_);
97   ExternalReference xref(&function, ExternalReference::BUILTIN_CALL, isolate_);
98   intptr_t handler = reinterpret_cast<intptr_t>(xref.address());
99   int params = descriptor->GetHandlerParameterCount();
100   output_frame->SetRegister(x0.code(), params);
101   output_frame->SetRegister(x1.code(), handler);
102 }
103
104
105 void Deoptimizer::CopyDoubleRegisters(FrameDescription* output_frame) {
106   for (int i = 0; i < DoubleRegister::kMaxNumRegisters; ++i) {
107     double double_value = input_->GetDoubleRegister(i);
108     output_frame->SetDoubleRegister(i, double_value);
109   }
110 }
111
112
113 Code* Deoptimizer::NotifyStubFailureBuiltin() {
114   return isolate_->builtins()->builtin(Builtins::kNotifyStubFailureSaveDoubles);
115 }
116
117
118 #define __ masm->
119
120 static void CopyRegisterDumpToFrame(MacroAssembler* masm,
121                                     Register frame,
122                                     CPURegList reg_list,
123                                     Register scratch1,
124                                     Register scratch2,
125                                     int src_offset,
126                                     int dst_offset) {
127   int offset0, offset1;
128   CPURegList copy_to_input = reg_list;
129   int reg_count = reg_list.Count();
130   int reg_size = reg_list.RegisterSizeInBytes();
131   for (int i = 0; i < (reg_count / 2); i++) {
132     __ PeekPair(scratch1, scratch2, src_offset + (i * reg_size * 2));
133
134     offset0 = (copy_to_input.PopLowestIndex().code() * reg_size) + dst_offset;
135     offset1 = (copy_to_input.PopLowestIndex().code() * reg_size) + dst_offset;
136
137     if ((offset0 + reg_size) == offset1) {
138       // Registers are adjacent: store in pairs.
139       __ Stp(scratch1, scratch2, MemOperand(frame, offset0));
140     } else {
141       // Registers are not adjacent: store individually.
142       __ Str(scratch1, MemOperand(frame, offset0));
143       __ Str(scratch2, MemOperand(frame, offset1));
144     }
145   }
146   if ((reg_count & 1) != 0) {
147     __ Peek(scratch1, src_offset + (reg_count - 1) * reg_size);
148     offset0 = (copy_to_input.PopLowestIndex().code() * reg_size) + dst_offset;
149     __ Str(scratch1, MemOperand(frame, offset0));
150   }
151 }
152
153 #undef __
154
155 #define __ masm()->
156
157 void Deoptimizer::EntryGenerator::Generate() {
158   GeneratePrologue();
159
160   // TODO(all): This code needs to be revisited. We probably only need to save
161   // caller-saved registers here. Callee-saved registers can be stored directly
162   // in the input frame.
163
164   // Save all allocatable floating point registers.
165   CPURegList saved_fp_registers(CPURegister::kFPRegister, kDRegSizeInBits,
166                                 FPRegister::kAllocatableFPRegisters);
167   __ PushCPURegList(saved_fp_registers);
168
169   // We save all the registers expcept jssp, sp and lr.
170   CPURegList saved_registers(CPURegister::kRegister, kXRegSizeInBits, 0, 27);
171   saved_registers.Combine(fp);
172   __ PushCPURegList(saved_registers);
173
174   const int kSavedRegistersAreaSize =
175       (saved_registers.Count() * kXRegSize) +
176       (saved_fp_registers.Count() * kDRegSize);
177
178   // Floating point registers are saved on the stack above core registers.
179   const int kFPRegistersOffset = saved_registers.Count() * kXRegSize;
180
181   // Get the bailout id from the stack.
182   Register bailout_id = x2;
183   __ Peek(bailout_id, kSavedRegistersAreaSize);
184
185   Register code_object = x3;
186   Register fp_to_sp = x4;
187   // Get the address of the location in the code object. This is the return
188   // address for lazy deoptimization.
189   __ Mov(code_object, lr);
190   // Compute the fp-to-sp delta, and correct one word for bailout id.
191   __ Add(fp_to_sp, masm()->StackPointer(),
192          kSavedRegistersAreaSize + (1 * kPointerSize));
193   __ Sub(fp_to_sp, fp, fp_to_sp);
194
195   // Allocate a new deoptimizer object.
196   __ Ldr(x0, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
197   __ Mov(x1, type());
198   // Following arguments are already loaded:
199   //  - x2: bailout id
200   //  - x3: code object address
201   //  - x4: fp-to-sp delta
202   __ Mov(x5, ExternalReference::isolate_address(isolate()));
203
204   {
205     // Call Deoptimizer::New().
206     AllowExternalCallThatCantCauseGC scope(masm());
207     __ CallCFunction(ExternalReference::new_deoptimizer_function(isolate()), 6);
208   }
209
210   // Preserve "deoptimizer" object in register x0.
211   Register deoptimizer = x0;
212
213   // Get the input frame descriptor pointer.
214   __ Ldr(x1, MemOperand(deoptimizer, Deoptimizer::input_offset()));
215
216   // Copy core registers into the input frame.
217   CopyRegisterDumpToFrame(masm(), x1, saved_registers, x2, x4, 0,
218                           FrameDescription::registers_offset());
219
220   // Copy FP registers to the input frame.
221   CopyRegisterDumpToFrame(masm(), x1, saved_fp_registers, x2, x4,
222                           kFPRegistersOffset,
223                           FrameDescription::double_registers_offset());
224
225   // Remove the bailout id and the saved registers from the stack.
226   __ Drop(1 + (kSavedRegistersAreaSize / kXRegSize));
227
228   // Compute a pointer to the unwinding limit in register x2; that is
229   // the first stack slot not part of the input frame.
230   Register unwind_limit = x2;
231   __ Ldr(unwind_limit, MemOperand(x1, FrameDescription::frame_size_offset()));
232   __ Add(unwind_limit, unwind_limit, __ StackPointer());
233
234   // Unwind the stack down to - but not including - the unwinding
235   // limit and copy the contents of the activation frame to the input
236   // frame description.
237   __ Add(x3, x1, FrameDescription::frame_content_offset());
238   Label pop_loop;
239   Label pop_loop_header;
240   __ B(&pop_loop_header);
241   __ Bind(&pop_loop);
242   __ Pop(x4);
243   __ Str(x4, MemOperand(x3, kPointerSize, PostIndex));
244   __ Bind(&pop_loop_header);
245   __ Cmp(unwind_limit, __ StackPointer());
246   __ B(ne, &pop_loop);
247
248   // Compute the output frame in the deoptimizer.
249   __ Push(x0);  // Preserve deoptimizer object across call.
250
251   {
252     // Call Deoptimizer::ComputeOutputFrames().
253     AllowExternalCallThatCantCauseGC scope(masm());
254     __ CallCFunction(
255         ExternalReference::compute_output_frames_function(isolate()), 1);
256   }
257   __ Pop(x4);  // Restore deoptimizer object (class Deoptimizer).
258
259   // Replace the current (input) frame with the output frames.
260   Label outer_push_loop, inner_push_loop,
261       outer_loop_header, inner_loop_header;
262   __ Ldrsw(x1, MemOperand(x4, Deoptimizer::output_count_offset()));
263   __ Ldr(x0, MemOperand(x4, Deoptimizer::output_offset()));
264   __ Add(x1, x0, Operand(x1, LSL, kPointerSizeLog2));
265   __ B(&outer_loop_header);
266
267   __ Bind(&outer_push_loop);
268   Register current_frame = x2;
269   __ Ldr(current_frame, MemOperand(x0, 0));
270   __ Ldr(x3, MemOperand(current_frame, FrameDescription::frame_size_offset()));
271   __ B(&inner_loop_header);
272
273   __ Bind(&inner_push_loop);
274   __ Sub(x3, x3, kPointerSize);
275   __ Add(x6, current_frame, x3);
276   __ Ldr(x7, MemOperand(x6, FrameDescription::frame_content_offset()));
277   __ Push(x7);
278   __ Bind(&inner_loop_header);
279   __ Cbnz(x3, &inner_push_loop);
280
281   __ Add(x0, x0, kPointerSize);
282   __ Bind(&outer_loop_header);
283   __ Cmp(x0, x1);
284   __ B(lt, &outer_push_loop);
285
286   __ Ldr(x1, MemOperand(x4, Deoptimizer::input_offset()));
287   ASSERT(!saved_fp_registers.IncludesAliasOf(crankshaft_fp_scratch) &&
288          !saved_fp_registers.IncludesAliasOf(fp_zero) &&
289          !saved_fp_registers.IncludesAliasOf(fp_scratch));
290   int src_offset = FrameDescription::double_registers_offset();
291   while (!saved_fp_registers.IsEmpty()) {
292     const CPURegister reg = saved_fp_registers.PopLowestIndex();
293     __ Ldr(reg, MemOperand(x1, src_offset));
294     src_offset += kDoubleSize;
295   }
296
297   // Push state from the last output frame.
298   __ Ldr(x6, MemOperand(current_frame, FrameDescription::state_offset()));
299   __ Push(x6);
300
301   // TODO(all): ARM copies a lot (if not all) of the last output frame onto the
302   // stack, then pops it all into registers. Here, we try to load it directly
303   // into the relevant registers. Is this correct? If so, we should improve the
304   // ARM code.
305
306   // TODO(all): This code needs to be revisited, We probably don't need to
307   // restore all the registers as fullcodegen does not keep live values in
308   // registers (note that at least fp must be restored though).
309
310   // Restore registers from the last output frame.
311   // Note that lr is not in the list of saved_registers and will be restored
312   // later. We can use it to hold the address of last output frame while
313   // reloading the other registers.
314   ASSERT(!saved_registers.IncludesAliasOf(lr));
315   Register last_output_frame = lr;
316   __ Mov(last_output_frame, current_frame);
317
318   // We don't need to restore x7 as it will be clobbered later to hold the
319   // continuation address.
320   Register continuation = x7;
321   saved_registers.Remove(continuation);
322
323   while (!saved_registers.IsEmpty()) {
324     // TODO(all): Look for opportunities to optimize this by using ldp.
325     CPURegister current_reg = saved_registers.PopLowestIndex();
326     int offset = (current_reg.code() * kPointerSize) +
327         FrameDescription::registers_offset();
328     __ Ldr(current_reg, MemOperand(last_output_frame, offset));
329   }
330
331   __ Ldr(continuation, MemOperand(last_output_frame,
332                                   FrameDescription::continuation_offset()));
333   __ Ldr(lr, MemOperand(last_output_frame, FrameDescription::pc_offset()));
334   __ InitializeRootRegister();
335   __ Br(continuation);
336 }
337
338
339 // Size of an entry of the second level deopt table.
340 // This is the code size generated by GeneratePrologue for one entry.
341 const int Deoptimizer::table_entry_size_ = 2 * kInstructionSize;
342
343
344 void Deoptimizer::TableEntryGenerator::GeneratePrologue() {
345   UseScratchRegisterScope temps(masm());
346   Register entry_id = temps.AcquireX();
347
348   // Create a sequence of deoptimization entries.
349   // Note that registers are still live when jumping to an entry.
350   Label done;
351   {
352     InstructionAccurateScope scope(masm());
353
354     // The number of entry will never exceed kMaxNumberOfEntries.
355     // As long as kMaxNumberOfEntries is a valid 16 bits immediate you can use
356     // a movz instruction to load the entry id.
357     ASSERT(is_uint16(Deoptimizer::kMaxNumberOfEntries));
358
359     for (int i = 0; i < count(); i++) {
360       int start = masm()->pc_offset();
361       USE(start);
362       __ movz(entry_id, i);
363       __ b(&done);
364       ASSERT(masm()->pc_offset() - start == table_entry_size_);
365     }
366   }
367   __ Bind(&done);
368   __ Push(entry_id);
369 }
370
371
372 void FrameDescription::SetCallerPc(unsigned offset, intptr_t value) {
373   SetFrameSlot(offset, value);
374 }
375
376
377 void FrameDescription::SetCallerFp(unsigned offset, intptr_t value) {
378   SetFrameSlot(offset, value);
379 }
380
381
382 void FrameDescription::SetCallerConstantPool(unsigned offset, intptr_t value) {
383   // No out-of-line constant pool support.
384   UNREACHABLE();
385 }
386
387
388 #undef __
389
390 } }  // namespace v8::internal