Upstream version 9.38.207.0
[platform/framework/web/crosswalk.git] / src / v8 / src / x64 / deoptimizer-x64.cc
1 // Copyright 2012 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 #if V8_TARGET_ARCH_X64
8
9 #include "src/codegen.h"
10 #include "src/deoptimizer.h"
11 #include "src/full-codegen.h"
12 #include "src/safepoint-table.h"
13
14 namespace v8 {
15 namespace internal {
16
17
18 const int Deoptimizer::table_entry_size_ = 10;
19
20
21 int Deoptimizer::patch_size() {
22   return Assembler::kCallSequenceLength;
23 }
24
25
26 void Deoptimizer::PatchCodeForDeoptimization(Isolate* isolate, Code* code) {
27   // Invalidate the relocation information, as it will become invalid by the
28   // code patching below, and is not needed any more.
29   code->InvalidateRelocation();
30
31   if (FLAG_zap_code_space) {
32     // Fail hard and early if we enter this code object again.
33     byte* pointer = code->FindCodeAgeSequence();
34     if (pointer != NULL) {
35       pointer += kNoCodeAgeSequenceLength;
36     } else {
37       pointer = code->instruction_start();
38     }
39     CodePatcher patcher(pointer, 1);
40     patcher.masm()->int3();
41
42     DeoptimizationInputData* data =
43         DeoptimizationInputData::cast(code->deoptimization_data());
44     int osr_offset = data->OsrPcOffset()->value();
45     if (osr_offset > 0) {
46       CodePatcher osr_patcher(code->instruction_start() + osr_offset, 1);
47       osr_patcher.masm()->int3();
48     }
49   }
50
51   // For each LLazyBailout instruction insert a absolute call to the
52   // corresponding deoptimization entry, or a short call to an absolute
53   // jump if space is short. The absolute jumps are put in a table just
54   // before the safepoint table (space was allocated there when the Code
55   // object was created, if necessary).
56
57   Address instruction_start = code->instruction_start();
58 #ifdef DEBUG
59   Address prev_call_address = NULL;
60 #endif
61   DeoptimizationInputData* deopt_data =
62       DeoptimizationInputData::cast(code->deoptimization_data());
63   deopt_data->SetSharedFunctionInfo(Smi::FromInt(0));
64   // For each LLazyBailout instruction insert a call to the corresponding
65   // deoptimization entry.
66   for (int i = 0; i < deopt_data->DeoptCount(); i++) {
67     if (deopt_data->Pc(i)->value() == -1) continue;
68     // Position where Call will be patched in.
69     Address call_address = instruction_start + deopt_data->Pc(i)->value();
70     // There is room enough to write a long call instruction because we pad
71     // LLazyBailout instructions with nops if necessary.
72     CodePatcher patcher(call_address, Assembler::kCallSequenceLength);
73     patcher.masm()->Call(GetDeoptimizationEntry(isolate, i, LAZY),
74                          Assembler::RelocInfoNone());
75     DCHECK(prev_call_address == NULL ||
76            call_address >= prev_call_address + patch_size());
77     DCHECK(call_address + patch_size() <= code->instruction_end());
78 #ifdef DEBUG
79     prev_call_address = call_address;
80 #endif
81   }
82 }
83
84
85 void Deoptimizer::FillInputFrame(Address tos, JavaScriptFrame* frame) {
86   // Set the register values. The values are not important as there are no
87   // callee saved registers in JavaScript frames, so all registers are
88   // spilled. Registers rbp and rsp are set to the correct values though.
89   for (int i = 0; i < Register::kNumRegisters; i++) {
90     input_->SetRegister(i, i * 4);
91   }
92   input_->SetRegister(rsp.code(), reinterpret_cast<intptr_t>(frame->sp()));
93   input_->SetRegister(rbp.code(), reinterpret_cast<intptr_t>(frame->fp()));
94   simd128_value_t zero = {{0.0, 0.0}};
95   for (int i = 0; i < DoubleRegister::NumAllocatableRegisters(); i++) {
96     input_->SetSIMD128Register(i, zero);
97   }
98
99   // Fill the frame content from the actual data on the frame.
100   for (unsigned i = 0; i < input_->GetFrameSize(); i += kPointerSize) {
101     input_->SetFrameSlot(i, Memory::uintptr_at(tos + i));
102   }
103 }
104
105
106 void Deoptimizer::SetPlatformCompiledStubRegisters(
107     FrameDescription* output_frame, CodeStubInterfaceDescriptor* descriptor) {
108   intptr_t handler =
109       reinterpret_cast<intptr_t>(descriptor->deoptimization_handler());
110   int params = descriptor->GetHandlerParameterCount();
111   output_frame->SetRegister(rax.code(), params);
112   output_frame->SetRegister(rbx.code(), handler);
113 }
114
115
116 void Deoptimizer::CopySIMD128Registers(FrameDescription* output_frame) {
117   for (int i = 0; i < XMMRegister::NumAllocatableRegisters(); ++i) {
118     simd128_value_t xmm_value = input_->GetSIMD128Register(i);
119     output_frame->SetSIMD128Register(i, xmm_value);
120   }
121 }
122
123
124 bool Deoptimizer::HasAlignmentPadding(JSFunction* function) {
125   // There is no dynamic alignment padding on x64 in the input frame.
126   return false;
127 }
128
129
130 #define __ masm()->
131
132 void Deoptimizer::EntryGenerator::Generate() {
133   GeneratePrologue();
134
135   // Save all general purpose registers before messing with them.
136   const int kNumberOfRegisters = Register::kNumRegisters;
137
138   const int kXMMRegsSize = kSIMD128Size *
139       XMMRegister::NumAllocatableRegisters();
140   __ subp(rsp, Immediate(kXMMRegsSize));
141
142   for (int i = 0; i < XMMRegister::NumAllocatableRegisters(); ++i) {
143     XMMRegister xmm_reg = XMMRegister::FromAllocationIndex(i);
144     int offset = i * kSIMD128Size;
145     __ movups(Operand(rsp, offset), xmm_reg);
146   }
147
148   // We push all registers onto the stack, even though we do not need
149   // to restore all later.
150   for (int i = 0; i < kNumberOfRegisters; i++) {
151     Register r = Register::from_code(i);
152     __ pushq(r);
153   }
154
155   const int kSavedRegistersAreaSize = kNumberOfRegisters * kRegisterSize +
156                                       kXMMRegsSize;
157
158   // We use this to keep the value of the fifth argument temporarily.
159   // Unfortunately we can't store it directly in r8 (used for passing
160   // this on linux), since it is another parameter passing register on windows.
161   Register arg5 = r11;
162
163   // Get the bailout id from the stack.
164   __ movp(arg_reg_3, Operand(rsp, kSavedRegistersAreaSize));
165
166   // Get the address of the location in the code object
167   // and compute the fp-to-sp delta in register arg5.
168   __ movp(arg_reg_4, Operand(rsp, kSavedRegistersAreaSize + 1 * kRegisterSize));
169   __ leap(arg5, Operand(rsp, kSavedRegistersAreaSize + 1 * kRegisterSize +
170                             kPCOnStackSize));
171
172   __ subp(arg5, rbp);
173   __ negp(arg5);
174
175   // Allocate a new deoptimizer object.
176   __ PrepareCallCFunction(6);
177   __ movp(rax, Operand(rbp, JavaScriptFrameConstants::kFunctionOffset));
178   __ movp(arg_reg_1, rax);
179   __ Set(arg_reg_2, type());
180   // Args 3 and 4 are already in the right registers.
181
182   // On windows put the arguments on the stack (PrepareCallCFunction
183   // has created space for this). On linux pass the arguments in r8 and r9.
184 #ifdef _WIN64
185   __ movq(Operand(rsp, 4 * kRegisterSize), arg5);
186   __ LoadAddress(arg5, ExternalReference::isolate_address(isolate()));
187   __ movq(Operand(rsp, 5 * kRegisterSize), arg5);
188 #else
189   __ movp(r8, arg5);
190   __ LoadAddress(r9, ExternalReference::isolate_address(isolate()));
191 #endif
192
193   { AllowExternalCallThatCantCauseGC scope(masm());
194     __ CallCFunction(ExternalReference::new_deoptimizer_function(isolate()), 6);
195   }
196   // Preserve deoptimizer object in register rax and get the input
197   // frame descriptor pointer.
198   __ movp(rbx, Operand(rax, Deoptimizer::input_offset()));
199
200   // Fill in the input registers.
201   for (int i = kNumberOfRegisters -1; i >= 0; i--) {
202     int offset = (i * kPointerSize) + FrameDescription::registers_offset();
203     __ PopQuad(Operand(rbx, offset));
204   }
205
206   // Fill in the xmm input registers.
207   STATIC_ASSERT(kSIMD128Size == 2 * kDoubleSize);
208   int xmm_regs_offset = FrameDescription::simd128_registers_offset();
209   for (int i = 0; i < XMMRegister::NumAllocatableRegisters(); i++) {
210     int dst_offset = i * kSIMD128Size + xmm_regs_offset;
211     __ popq(Operand(rbx, dst_offset));
212     __ popq(Operand(rbx, dst_offset + kDoubleSize));
213   }
214
215   // Remove the bailout id and return address from the stack.
216   __ addp(rsp, Immediate(1 * kRegisterSize + kPCOnStackSize));
217
218   // Compute a pointer to the unwinding limit in register rcx; that is
219   // the first stack slot not part of the input frame.
220   __ movp(rcx, Operand(rbx, FrameDescription::frame_size_offset()));
221   __ addp(rcx, rsp);
222
223   // Unwind the stack down to - but not including - the unwinding
224   // limit and copy the contents of the activation frame to the input
225   // frame description.
226   __ leap(rdx, Operand(rbx, FrameDescription::frame_content_offset()));
227   Label pop_loop_header;
228   __ jmp(&pop_loop_header);
229   Label pop_loop;
230   __ bind(&pop_loop);
231   __ Pop(Operand(rdx, 0));
232   __ addp(rdx, Immediate(sizeof(intptr_t)));
233   __ bind(&pop_loop_header);
234   __ cmpp(rcx, rsp);
235   __ j(not_equal, &pop_loop);
236
237   // Compute the output frame in the deoptimizer.
238   __ pushq(rax);
239   __ PrepareCallCFunction(2);
240   __ movp(arg_reg_1, rax);
241   __ LoadAddress(arg_reg_2, ExternalReference::isolate_address(isolate()));
242   {
243     AllowExternalCallThatCantCauseGC scope(masm());
244     __ CallCFunction(
245         ExternalReference::compute_output_frames_function(isolate()), 2);
246   }
247   __ popq(rax);
248
249   // Replace the current frame with the output frames.
250   Label outer_push_loop, inner_push_loop,
251       outer_loop_header, inner_loop_header;
252   // Outer loop state: rax = current FrameDescription**, rdx = one past the
253   // last FrameDescription**.
254   __ movl(rdx, Operand(rax, Deoptimizer::output_count_offset()));
255   __ movp(rax, Operand(rax, Deoptimizer::output_offset()));
256   __ leap(rdx, Operand(rax, rdx, times_pointer_size, 0));
257   __ jmp(&outer_loop_header);
258   __ bind(&outer_push_loop);
259   // Inner loop state: rbx = current FrameDescription*, rcx = loop index.
260   __ movp(rbx, Operand(rax, 0));
261   __ movp(rcx, Operand(rbx, FrameDescription::frame_size_offset()));
262   __ jmp(&inner_loop_header);
263   __ bind(&inner_push_loop);
264   __ subp(rcx, Immediate(sizeof(intptr_t)));
265   __ Push(Operand(rbx, rcx, times_1, FrameDescription::frame_content_offset()));
266   __ bind(&inner_loop_header);
267   __ testp(rcx, rcx);
268   __ j(not_zero, &inner_push_loop);
269   __ addp(rax, Immediate(kPointerSize));
270   __ bind(&outer_loop_header);
271   __ cmpp(rax, rdx);
272   __ j(below, &outer_push_loop);
273
274   for (int i = 0; i < XMMRegister::NumAllocatableRegisters(); ++i) {
275     XMMRegister xmm_reg = XMMRegister::FromAllocationIndex(i);
276     int src_offset = i * kSIMD128Size + xmm_regs_offset;
277     __ movups(xmm_reg, Operand(rbx, src_offset));
278   }
279
280   // Push state, pc, and continuation from the last output frame.
281   __ Push(Operand(rbx, FrameDescription::state_offset()));
282   __ PushQuad(Operand(rbx, FrameDescription::pc_offset()));
283   __ PushQuad(Operand(rbx, FrameDescription::continuation_offset()));
284
285   // Push the registers from the last output frame.
286   for (int i = 0; i < kNumberOfRegisters; i++) {
287     int offset = (i * kPointerSize) + FrameDescription::registers_offset();
288     __ PushQuad(Operand(rbx, offset));
289   }
290
291   // Restore the registers from the stack.
292   for (int i = kNumberOfRegisters - 1; i >= 0 ; i--) {
293     Register r = Register::from_code(i);
294     // Do not restore rsp, simply pop the value into the next register
295     // and overwrite this afterwards.
296     if (r.is(rsp)) {
297       DCHECK(i > 0);
298       r = Register::from_code(i - 1);
299     }
300     __ popq(r);
301   }
302
303   // Set up the roots register.
304   __ InitializeRootRegister();
305   __ InitializeSmiConstantRegister();
306
307   // Return to the continuation point.
308   __ ret(0);
309 }
310
311
312 void Deoptimizer::TableEntryGenerator::GeneratePrologue() {
313   // Create a sequence of deoptimization entries.
314   Label done;
315   for (int i = 0; i < count(); i++) {
316     int start = masm()->pc_offset();
317     USE(start);
318     __ pushq_imm32(i);
319     __ jmp(&done);
320     DCHECK(masm()->pc_offset() - start == table_entry_size_);
321   }
322   __ bind(&done);
323 }
324
325
326 void FrameDescription::SetCallerPc(unsigned offset, intptr_t value) {
327   if (kPCOnStackSize == 2 * kPointerSize) {
328     // Zero out the high-32 bit of PC for x32 port.
329     SetFrameSlot(offset + kPointerSize, 0);
330   }
331   SetFrameSlot(offset, value);
332 }
333
334
335 void FrameDescription::SetCallerFp(unsigned offset, intptr_t value) {
336   if (kFPOnStackSize == 2 * kPointerSize) {
337     // Zero out the high-32 bit of FP for x32 port.
338     SetFrameSlot(offset + kPointerSize, 0);
339   }
340   SetFrameSlot(offset, value);
341 }
342
343
344 void FrameDescription::SetCallerConstantPool(unsigned offset, intptr_t value) {
345   // No out-of-line constant pool support.
346   UNREACHABLE();
347 }
348
349
350 double FrameDescription::GetDoubleRegister(unsigned n) const {
351   DCHECK(n < ARRAY_SIZE(simd128_registers_));
352   return simd128_registers_[n].d[0];
353 }
354
355
356 void FrameDescription::SetDoubleRegister(unsigned n, double value) {
357   DCHECK(n < ARRAY_SIZE(simd128_registers_));
358   simd128_registers_[n].d[0] = value;
359 }
360
361
362 #undef __
363
364
365 } }  // namespace v8::internal
366
367 #endif  // V8_TARGET_ARCH_X64