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