Upstream version 11.39.258.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, CodeStubDescriptor* 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::CopyDoubleRegisters(FrameDescription* output_frame) {
117 }
118
119
120 void Deoptimizer::CopySIMD128Registers(FrameDescription* output_frame) {
121   for (int i = 0; i < XMMRegister::NumAllocatableRegisters(); ++i) {
122     simd128_value_t xmm_value = input_->GetSIMD128Register(i);
123     output_frame->SetSIMD128Register(i, xmm_value);
124   }
125 }
126
127
128 bool Deoptimizer::HasAlignmentPadding(JSFunction* function) {
129   // There is no dynamic alignment padding on x64 in the input frame.
130   return false;
131 }
132
133
134 #define __ masm()->
135
136 void Deoptimizer::EntryGenerator::Generate() {
137   GeneratePrologue();
138
139   // Save all general purpose registers before messing with them.
140   const int kNumberOfRegisters = Register::kNumRegisters;
141
142   const int kXMMRegsSize = kSIMD128Size *
143       XMMRegister::NumAllocatableRegisters();
144   __ subp(rsp, Immediate(kXMMRegsSize));
145
146   for (int i = 0; i < XMMRegister::NumAllocatableRegisters(); ++i) {
147     XMMRegister xmm_reg = XMMRegister::FromAllocationIndex(i);
148     int offset = i * kSIMD128Size;
149     __ movups(Operand(rsp, offset), xmm_reg);
150   }
151
152   // We push all registers onto the stack, even though we do not need
153   // to restore all later.
154   for (int i = 0; i < kNumberOfRegisters; i++) {
155     Register r = Register::from_code(i);
156     __ pushq(r);
157   }
158
159   const int kSavedRegistersAreaSize = kNumberOfRegisters * kRegisterSize +
160                                       kXMMRegsSize;
161
162   // We use this to keep the value of the fifth argument temporarily.
163   // Unfortunately we can't store it directly in r8 (used for passing
164   // this on linux), since it is another parameter passing register on windows.
165   Register arg5 = r11;
166
167   // Get the bailout id from the stack.
168   __ movp(arg_reg_3, Operand(rsp, kSavedRegistersAreaSize));
169
170   // Get the address of the location in the code object
171   // and compute the fp-to-sp delta in register arg5.
172   __ movp(arg_reg_4, Operand(rsp, kSavedRegistersAreaSize + 1 * kRegisterSize));
173   __ leap(arg5, Operand(rsp, kSavedRegistersAreaSize + 1 * kRegisterSize +
174                             kPCOnStackSize));
175
176   __ subp(arg5, rbp);
177   __ negp(arg5);
178
179   // Allocate a new deoptimizer object.
180   __ PrepareCallCFunction(6);
181   __ movp(rax, Operand(rbp, JavaScriptFrameConstants::kFunctionOffset));
182   __ movp(arg_reg_1, rax);
183   __ Set(arg_reg_2, type());
184   // Args 3 and 4 are already in the right registers.
185
186   // On windows put the arguments on the stack (PrepareCallCFunction
187   // has created space for this). On linux pass the arguments in r8 and r9.
188 #ifdef _WIN64
189   __ movq(Operand(rsp, 4 * kRegisterSize), arg5);
190   __ LoadAddress(arg5, ExternalReference::isolate_address(isolate()));
191   __ movq(Operand(rsp, 5 * kRegisterSize), arg5);
192 #else
193   __ movp(r8, arg5);
194   __ LoadAddress(r9, ExternalReference::isolate_address(isolate()));
195 #endif
196
197   { AllowExternalCallThatCantCauseGC scope(masm());
198     __ CallCFunction(ExternalReference::new_deoptimizer_function(isolate()), 6);
199   }
200   // Preserve deoptimizer object in register rax and get the input
201   // frame descriptor pointer.
202   __ movp(rbx, Operand(rax, Deoptimizer::input_offset()));
203
204   // Fill in the input registers.
205   for (int i = kNumberOfRegisters -1; i >= 0; i--) {
206     int offset = (i * kPointerSize) + FrameDescription::registers_offset();
207     __ PopQuad(Operand(rbx, offset));
208   }
209
210   // Fill in the xmm input registers.
211   STATIC_ASSERT(kSIMD128Size == 2 * kDoubleSize);
212   int xmm_regs_offset = FrameDescription::simd128_registers_offset();
213   for (int i = 0; i < XMMRegister::NumAllocatableRegisters(); i++) {
214     int dst_offset = i * kSIMD128Size + xmm_regs_offset;
215     __ popq(Operand(rbx, dst_offset));
216     __ popq(Operand(rbx, dst_offset + kDoubleSize));
217   }
218
219   // Remove the bailout id and return address from the stack.
220   __ addp(rsp, Immediate(1 * kRegisterSize + kPCOnStackSize));
221
222   // Compute a pointer to the unwinding limit in register rcx; that is
223   // the first stack slot not part of the input frame.
224   __ movp(rcx, Operand(rbx, FrameDescription::frame_size_offset()));
225   __ addp(rcx, rsp);
226
227   // Unwind the stack down to - but not including - the unwinding
228   // limit and copy the contents of the activation frame to the input
229   // frame description.
230   __ leap(rdx, Operand(rbx, FrameDescription::frame_content_offset()));
231   Label pop_loop_header;
232   __ jmp(&pop_loop_header);
233   Label pop_loop;
234   __ bind(&pop_loop);
235   __ Pop(Operand(rdx, 0));
236   __ addp(rdx, Immediate(sizeof(intptr_t)));
237   __ bind(&pop_loop_header);
238   __ cmpp(rcx, rsp);
239   __ j(not_equal, &pop_loop);
240
241   // Compute the output frame in the deoptimizer.
242   __ pushq(rax);
243   __ PrepareCallCFunction(2);
244   __ movp(arg_reg_1, rax);
245   __ LoadAddress(arg_reg_2, ExternalReference::isolate_address(isolate()));
246   {
247     AllowExternalCallThatCantCauseGC scope(masm());
248     __ CallCFunction(
249         ExternalReference::compute_output_frames_function(isolate()), 2);
250   }
251   __ popq(rax);
252
253   // Replace the current frame with the output frames.
254   Label outer_push_loop, inner_push_loop,
255       outer_loop_header, inner_loop_header;
256   // Outer loop state: rax = current FrameDescription**, rdx = one past the
257   // last FrameDescription**.
258   __ movl(rdx, Operand(rax, Deoptimizer::output_count_offset()));
259   __ movp(rax, Operand(rax, Deoptimizer::output_offset()));
260   __ leap(rdx, Operand(rax, rdx, times_pointer_size, 0));
261   __ jmp(&outer_loop_header);
262   __ bind(&outer_push_loop);
263   // Inner loop state: rbx = current FrameDescription*, rcx = loop index.
264   __ movp(rbx, Operand(rax, 0));
265   __ movp(rcx, Operand(rbx, FrameDescription::frame_size_offset()));
266   __ jmp(&inner_loop_header);
267   __ bind(&inner_push_loop);
268   __ subp(rcx, Immediate(sizeof(intptr_t)));
269   __ Push(Operand(rbx, rcx, times_1, FrameDescription::frame_content_offset()));
270   __ bind(&inner_loop_header);
271   __ testp(rcx, rcx);
272   __ j(not_zero, &inner_push_loop);
273   __ addp(rax, Immediate(kPointerSize));
274   __ bind(&outer_loop_header);
275   __ cmpp(rax, rdx);
276   __ j(below, &outer_push_loop);
277
278   for (int i = 0; i < XMMRegister::NumAllocatableRegisters(); ++i) {
279     XMMRegister xmm_reg = XMMRegister::FromAllocationIndex(i);
280     int src_offset = i * kSIMD128Size + xmm_regs_offset;
281     __ movups(xmm_reg, Operand(rbx, src_offset));
282   }
283
284   // Push state, pc, and continuation from the last output frame.
285   __ Push(Operand(rbx, FrameDescription::state_offset()));
286   __ PushQuad(Operand(rbx, FrameDescription::pc_offset()));
287   __ PushQuad(Operand(rbx, FrameDescription::continuation_offset()));
288
289   // Push the registers from the last output frame.
290   for (int i = 0; i < kNumberOfRegisters; i++) {
291     int offset = (i * kPointerSize) + FrameDescription::registers_offset();
292     __ PushQuad(Operand(rbx, offset));
293   }
294
295   // Restore the registers from the stack.
296   for (int i = kNumberOfRegisters - 1; i >= 0 ; i--) {
297     Register r = Register::from_code(i);
298     // Do not restore rsp, simply pop the value into the next register
299     // and overwrite this afterwards.
300     if (r.is(rsp)) {
301       DCHECK(i > 0);
302       r = Register::from_code(i - 1);
303     }
304     __ popq(r);
305   }
306
307   // Set up the roots register.
308   __ InitializeRootRegister();
309   __ InitializeSmiConstantRegister();
310
311   // Return to the continuation point.
312   __ ret(0);
313 }
314
315
316 void Deoptimizer::TableEntryGenerator::GeneratePrologue() {
317   // Create a sequence of deoptimization entries.
318   Label done;
319   for (int i = 0; i < count(); i++) {
320     int start = masm()->pc_offset();
321     USE(start);
322     __ pushq_imm32(i);
323     __ jmp(&done);
324     DCHECK(masm()->pc_offset() - start == table_entry_size_);
325   }
326   __ bind(&done);
327 }
328
329
330 void FrameDescription::SetCallerPc(unsigned offset, intptr_t value) {
331   if (kPCOnStackSize == 2 * kPointerSize) {
332     // Zero out the high-32 bit of PC for x32 port.
333     SetFrameSlot(offset + kPointerSize, 0);
334   }
335   SetFrameSlot(offset, value);
336 }
337
338
339 void FrameDescription::SetCallerFp(unsigned offset, intptr_t value) {
340   if (kFPOnStackSize == 2 * kPointerSize) {
341     // Zero out the high-32 bit of FP for x32 port.
342     SetFrameSlot(offset + kPointerSize, 0);
343   }
344   SetFrameSlot(offset, value);
345 }
346
347
348 void FrameDescription::SetCallerConstantPool(unsigned offset, intptr_t value) {
349   // No out-of-line constant pool support.
350   UNREACHABLE();
351 }
352
353
354 double FrameDescription::GetDoubleRegister(unsigned n) const {
355   DCHECK(n < arraysize(simd128_registers_));
356   return simd128_registers_[n].d[0];
357 }
358
359
360 void FrameDescription::SetDoubleRegister(unsigned n, double value) {
361   DCHECK(n < arraysize(simd128_registers_));
362   simd128_registers_[n].d[0] = value;
363 }
364
365
366 simd128_value_t FrameDescription::GetSIMD128Register(unsigned n) const {
367   DCHECK(n < arraysize(simd128_registers_));
368   return simd128_registers_[n];
369 }
370
371
372 void FrameDescription::SetSIMD128Register(unsigned n, simd128_value_t value) {
373   DCHECK(n < arraysize(simd128_registers_));
374   simd128_registers_[n] = value;
375 }
376
377
378 int FrameDescription::double_registers_offset() {
379   return OFFSET_OF(FrameDescription, simd128_registers_);
380 }
381
382
383 int FrameDescription::simd128_registers_offset() {
384   return OFFSET_OF(FrameDescription, simd128_registers_);
385 }
386
387
388 #undef __
389
390
391 } }  // namespace v8::internal
392
393 #endif  // V8_TARGET_ARCH_X64