blob: 16b0cdcd1ba434abcdcc7d70302179cbbe97f616 [file] [log] [blame]
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001// Copyright 2012 the V8 project authors. All rights reserved.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
Ben Murdoch8c569c42011-03-10 11:43:29 +00004
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005#include "src/v8.h"
Ben Murdoch8c569c42011-03-10 11:43:29 +00006
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007#if V8_TARGET_ARCH_X64
Ben Murdochb8e0da22011-05-16 14:20:40 +01008
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009#include "src/codegen.h"
10#include "src/deoptimizer.h"
11#include "src/full-codegen.h"
12#include "src/safepoint-table.h"
Ben Murdoch8c569c42011-03-10 11:43:29 +000013
Ben Murdochb0fe1622011-05-05 13:52:32 +010014namespace v8 {
15namespace internal {
Ben Murdoch8c569c42011-03-10 11:43:29 +000016
Ben Murdochb0fe1622011-05-05 13:52:32 +010017
Ben Murdoch69a99ed2011-11-30 16:03:39 +000018const int Deoptimizer::table_entry_size_ = 10;
Ben Murdochb0fe1622011-05-05 13:52:32 +010019
Steve Block1e0659c2011-05-24 12:43:12 +010020
21int Deoptimizer::patch_size() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000022 return Assembler::kCallSequenceLength;
Steve Block44f0eee2011-05-26 01:26:41 +010023}
24
25
Ben Murdochb8a8cc12014-11-26 15:28:44 +000026void Deoptimizer::PatchCodeForDeoptimization(Isolate* isolate, Code* code) {
Steve Block1e0659c2011-05-24 12:43:12 +010027 // 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
Ben Murdochb8a8cc12014-11-26 15:28:44 +000031 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
Ben Murdoch2b4ba112012-01-20 14:57:15 +000051 // For each LLazyBailout instruction insert a absolute call to the
Steve Block1e0659c2011-05-24 12:43:12 +010052 // 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
Ben Murdochb8a8cc12014-11-26 15:28:44 +000057 Address instruction_start = code->instruction_start();
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000058#ifdef DEBUG
Ben Murdoch2b4ba112012-01-20 14:57:15 +000059 Address prev_call_address = NULL;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000060#endif
Ben Murdoch2b4ba112012-01-20 14:57:15 +000061 DeoptimizationInputData* deopt_data =
62 DeoptimizationInputData::cast(code->deoptimization_data());
Ben Murdochb8a8cc12014-11-26 15:28:44 +000063 deopt_data->SetSharedFunctionInfo(Smi::FromInt(0));
64 // For each LLazyBailout instruction insert a call to the corresponding
65 // deoptimization entry.
Ben Murdoch2b4ba112012-01-20 14:57:15 +000066 for (int i = 0; i < deopt_data->DeoptCount(); i++) {
67 if (deopt_data->Pc(i)->value() == -1) continue;
Steve Block1e0659c2011-05-24 12:43:12 +010068 // Position where Call will be patched in.
Ben Murdoch2b4ba112012-01-20 14:57:15 +000069 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.
Ben Murdochb8a8cc12014-11-26 15:28:44 +000072 CodePatcher patcher(call_address, Assembler::kCallSequenceLength);
73 patcher.masm()->Call(GetDeoptimizationEntry(isolate, i, LAZY),
74 Assembler::RelocInfoNone());
75 DCHECK(prev_call_address == NULL ||
Ben Murdoch2b4ba112012-01-20 14:57:15 +000076 call_address >= prev_call_address + patch_size());
Ben Murdochb8a8cc12014-11-26 15:28:44 +000077 DCHECK(call_address + patch_size() <= code->instruction_end());
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000078#ifdef DEBUG
Ben Murdoch2b4ba112012-01-20 14:57:15 +000079 prev_call_address = call_address;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000080#endif
Steve Block1e0659c2011-05-24 12:43:12 +010081 }
Ben Murdochb0fe1622011-05-05 13:52:32 +010082}
83
84
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000085void 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()));
Ben Murdochb8a8cc12014-11-26 15:28:44 +000094 for (int i = 0; i < DoubleRegister::NumAllocatableRegisters(); i++) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000095 input_->SetDoubleRegister(i, 0.0);
96 }
97
98 // Fill the frame content from the actual data on the frame.
99 for (unsigned i = 0; i < input_->GetFrameSize(); i += kPointerSize) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000100 input_->SetFrameSlot(i, Memory::uintptr_at(tos + i));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000101 }
102}
103
104
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000105void Deoptimizer::SetPlatformCompiledStubRegisters(
106 FrameDescription* output_frame, CodeStubDescriptor* descriptor) {
107 intptr_t handler =
108 reinterpret_cast<intptr_t>(descriptor->deoptimization_handler());
109 int params = descriptor->GetHandlerParameterCount();
110 output_frame->SetRegister(rax.code(), params);
111 output_frame->SetRegister(rbx.code(), handler);
112}
113
114
115void Deoptimizer::CopyDoubleRegisters(FrameDescription* output_frame) {
116 for (int i = 0; i < XMMRegister::NumAllocatableRegisters(); ++i) {
117 double double_value = input_->GetDoubleRegister(i);
118 output_frame->SetDoubleRegister(i, double_value);
119 }
120}
121
122
123bool Deoptimizer::HasAlignmentPadding(JSFunction* function) {
124 // There is no dynamic alignment padding on x64 in the input frame.
125 return false;
126}
127
128
Steve Block1e0659c2011-05-24 12:43:12 +0100129#define __ masm()->
130
Ben Murdochb0fe1622011-05-05 13:52:32 +0100131void Deoptimizer::EntryGenerator::Generate() {
Steve Block1e0659c2011-05-24 12:43:12 +0100132 GeneratePrologue();
Steve Block1e0659c2011-05-24 12:43:12 +0100133
134 // Save all general purpose registers before messing with them.
135 const int kNumberOfRegisters = Register::kNumRegisters;
136
137 const int kDoubleRegsSize = kDoubleSize *
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000138 XMMRegister::NumAllocatableRegisters();
139 __ subp(rsp, Immediate(kDoubleRegsSize));
Steve Block1e0659c2011-05-24 12:43:12 +0100140
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000141 for (int i = 0; i < XMMRegister::NumAllocatableRegisters(); ++i) {
Steve Block1e0659c2011-05-24 12:43:12 +0100142 XMMRegister xmm_reg = XMMRegister::FromAllocationIndex(i);
143 int offset = i * kDoubleSize;
144 __ movsd(Operand(rsp, offset), xmm_reg);
145 }
146
147 // We push all registers onto the stack, even though we do not need
148 // to restore all later.
149 for (int i = 0; i < kNumberOfRegisters; i++) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000150 Register r = Register::from_code(i);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000151 __ pushq(r);
Steve Block1e0659c2011-05-24 12:43:12 +0100152 }
153
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000154 const int kSavedRegistersAreaSize = kNumberOfRegisters * kRegisterSize +
Steve Block1e0659c2011-05-24 12:43:12 +0100155 kDoubleRegsSize;
156
Steve Block1e0659c2011-05-24 12:43:12 +0100157 // We use this to keep the value of the fifth argument temporarily.
158 // Unfortunately we can't store it directly in r8 (used for passing
159 // this on linux), since it is another parameter passing register on windows.
160 Register arg5 = r11;
161
162 // Get the bailout id from the stack.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000163 __ movp(arg_reg_3, Operand(rsp, kSavedRegistersAreaSize));
Steve Block1e0659c2011-05-24 12:43:12 +0100164
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000165 // Get the address of the location in the code object
Steve Block1e0659c2011-05-24 12:43:12 +0100166 // and compute the fp-to-sp delta in register arg5.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000167 __ movp(arg_reg_4, Operand(rsp, kSavedRegistersAreaSize + 1 * kRegisterSize));
168 __ leap(arg5, Operand(rsp, kSavedRegistersAreaSize + 1 * kRegisterSize +
169 kPCOnStackSize));
Steve Block1e0659c2011-05-24 12:43:12 +0100170
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000171 __ subp(arg5, rbp);
172 __ negp(arg5);
Steve Block1e0659c2011-05-24 12:43:12 +0100173
174 // Allocate a new deoptimizer object.
Ben Murdoch8b112d22011-06-08 16:22:53 +0100175 __ PrepareCallCFunction(6);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000176 __ movp(rax, Operand(rbp, JavaScriptFrameConstants::kFunctionOffset));
177 __ movp(arg_reg_1, rax);
178 __ Set(arg_reg_2, type());
Steve Block1e0659c2011-05-24 12:43:12 +0100179 // Args 3 and 4 are already in the right registers.
180
Ben Murdoch8b112d22011-06-08 16:22:53 +0100181 // On windows put the arguments on the stack (PrepareCallCFunction
182 // has created space for this). On linux pass the arguments in r8 and r9.
Steve Block1e0659c2011-05-24 12:43:12 +0100183#ifdef _WIN64
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000184 __ movq(Operand(rsp, 4 * kRegisterSize), arg5);
185 __ LoadAddress(arg5, ExternalReference::isolate_address(isolate()));
186 __ movq(Operand(rsp, 5 * kRegisterSize), arg5);
Steve Block1e0659c2011-05-24 12:43:12 +0100187#else
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000188 __ movp(r8, arg5);
189 __ LoadAddress(r9, ExternalReference::isolate_address(isolate()));
Steve Block1e0659c2011-05-24 12:43:12 +0100190#endif
191
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000192 { AllowExternalCallThatCantCauseGC scope(masm());
193 __ CallCFunction(ExternalReference::new_deoptimizer_function(isolate()), 6);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100194 }
Steve Block1e0659c2011-05-24 12:43:12 +0100195 // Preserve deoptimizer object in register rax and get the input
196 // frame descriptor pointer.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000197 __ movp(rbx, Operand(rax, Deoptimizer::input_offset()));
Steve Block1e0659c2011-05-24 12:43:12 +0100198
199 // Fill in the input registers.
200 for (int i = kNumberOfRegisters -1; i >= 0; i--) {
201 int offset = (i * kPointerSize) + FrameDescription::registers_offset();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000202 __ PopQuad(Operand(rbx, offset));
Steve Block1e0659c2011-05-24 12:43:12 +0100203 }
204
205 // Fill in the double input registers.
206 int double_regs_offset = FrameDescription::double_registers_offset();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000207 for (int i = 0; i < XMMRegister::NumAllocatableRegisters(); i++) {
Steve Block1e0659c2011-05-24 12:43:12 +0100208 int dst_offset = i * kDoubleSize + double_regs_offset;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000209 __ popq(Operand(rbx, dst_offset));
Steve Block1e0659c2011-05-24 12:43:12 +0100210 }
211
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000212 // Remove the bailout id and return address from the stack.
213 __ addp(rsp, Immediate(1 * kRegisterSize + kPCOnStackSize));
Steve Block1e0659c2011-05-24 12:43:12 +0100214
215 // Compute a pointer to the unwinding limit in register rcx; that is
216 // the first stack slot not part of the input frame.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000217 __ movp(rcx, Operand(rbx, FrameDescription::frame_size_offset()));
218 __ addp(rcx, rsp);
Steve Block1e0659c2011-05-24 12:43:12 +0100219
220 // Unwind the stack down to - but not including - the unwinding
221 // limit and copy the contents of the activation frame to the input
222 // frame description.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000223 __ leap(rdx, Operand(rbx, FrameDescription::frame_content_offset()));
224 Label pop_loop_header;
225 __ jmp(&pop_loop_header);
Steve Block1e0659c2011-05-24 12:43:12 +0100226 Label pop_loop;
227 __ bind(&pop_loop);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000228 __ Pop(Operand(rdx, 0));
229 __ addp(rdx, Immediate(sizeof(intptr_t)));
230 __ bind(&pop_loop_header);
231 __ cmpp(rcx, rsp);
Steve Block1e0659c2011-05-24 12:43:12 +0100232 __ j(not_equal, &pop_loop);
233
234 // Compute the output frame in the deoptimizer.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000235 __ pushq(rax);
Ben Murdoch8b112d22011-06-08 16:22:53 +0100236 __ PrepareCallCFunction(2);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000237 __ movp(arg_reg_1, rax);
238 __ LoadAddress(arg_reg_2, ExternalReference::isolate_address(isolate()));
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100239 {
240 AllowExternalCallThatCantCauseGC scope(masm());
241 __ CallCFunction(
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000242 ExternalReference::compute_output_frames_function(isolate()), 2);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100243 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000244 __ popq(rax);
Steve Block1e0659c2011-05-24 12:43:12 +0100245
246 // Replace the current frame with the output frames.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000247 Label outer_push_loop, inner_push_loop,
248 outer_loop_header, inner_loop_header;
Steve Block1e0659c2011-05-24 12:43:12 +0100249 // Outer loop state: rax = current FrameDescription**, rdx = one past the
250 // last FrameDescription**.
251 __ movl(rdx, Operand(rax, Deoptimizer::output_count_offset()));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000252 __ movp(rax, Operand(rax, Deoptimizer::output_offset()));
253 __ leap(rdx, Operand(rax, rdx, times_pointer_size, 0));
254 __ jmp(&outer_loop_header);
Steve Block1e0659c2011-05-24 12:43:12 +0100255 __ bind(&outer_push_loop);
256 // Inner loop state: rbx = current FrameDescription*, rcx = loop index.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000257 __ movp(rbx, Operand(rax, 0));
258 __ movp(rcx, Operand(rbx, FrameDescription::frame_size_offset()));
259 __ jmp(&inner_loop_header);
Steve Block1e0659c2011-05-24 12:43:12 +0100260 __ bind(&inner_push_loop);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000261 __ subp(rcx, Immediate(sizeof(intptr_t)));
262 __ Push(Operand(rbx, rcx, times_1, FrameDescription::frame_content_offset()));
263 __ bind(&inner_loop_header);
264 __ testp(rcx, rcx);
Steve Block1e0659c2011-05-24 12:43:12 +0100265 __ j(not_zero, &inner_push_loop);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000266 __ addp(rax, Immediate(kPointerSize));
267 __ bind(&outer_loop_header);
268 __ cmpp(rax, rdx);
Steve Block1e0659c2011-05-24 12:43:12 +0100269 __ j(below, &outer_push_loop);
270
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000271 for (int i = 0; i < XMMRegister::NumAllocatableRegisters(); ++i) {
272 XMMRegister xmm_reg = XMMRegister::FromAllocationIndex(i);
273 int src_offset = i * kDoubleSize + double_regs_offset;
274 __ movsd(xmm_reg, Operand(rbx, src_offset));
Steve Block1e0659c2011-05-24 12:43:12 +0100275 }
276
277 // Push state, pc, and continuation from the last output frame.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000278 __ Push(Operand(rbx, FrameDescription::state_offset()));
279 __ PushQuad(Operand(rbx, FrameDescription::pc_offset()));
280 __ PushQuad(Operand(rbx, FrameDescription::continuation_offset()));
Steve Block1e0659c2011-05-24 12:43:12 +0100281
282 // Push the registers from the last output frame.
283 for (int i = 0; i < kNumberOfRegisters; i++) {
284 int offset = (i * kPointerSize) + FrameDescription::registers_offset();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000285 __ PushQuad(Operand(rbx, offset));
Steve Block1e0659c2011-05-24 12:43:12 +0100286 }
287
288 // Restore the registers from the stack.
289 for (int i = kNumberOfRegisters - 1; i >= 0 ; i--) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000290 Register r = Register::from_code(i);
Steve Block1e0659c2011-05-24 12:43:12 +0100291 // Do not restore rsp, simply pop the value into the next register
292 // and overwrite this afterwards.
293 if (r.is(rsp)) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000294 DCHECK(i > 0);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000295 r = Register::from_code(i - 1);
Steve Block1e0659c2011-05-24 12:43:12 +0100296 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000297 __ popq(r);
Steve Block1e0659c2011-05-24 12:43:12 +0100298 }
299
300 // Set up the roots register.
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100301 __ InitializeRootRegister();
302 __ InitializeSmiConstantRegister();
Steve Block1e0659c2011-05-24 12:43:12 +0100303
304 // Return to the continuation point.
305 __ ret(0);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100306}
307
308
309void Deoptimizer::TableEntryGenerator::GeneratePrologue() {
Steve Block1e0659c2011-05-24 12:43:12 +0100310 // Create a sequence of deoptimization entries.
311 Label done;
312 for (int i = 0; i < count(); i++) {
313 int start = masm()->pc_offset();
314 USE(start);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000315 __ pushq_imm32(i);
Steve Block1e0659c2011-05-24 12:43:12 +0100316 __ jmp(&done);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000317 DCHECK(masm()->pc_offset() - start == table_entry_size_);
Steve Block1e0659c2011-05-24 12:43:12 +0100318 }
319 __ bind(&done);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100320}
321
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000322
323void FrameDescription::SetCallerPc(unsigned offset, intptr_t value) {
324 if (kPCOnStackSize == 2 * kPointerSize) {
325 // Zero out the high-32 bit of PC for x32 port.
326 SetFrameSlot(offset + kPointerSize, 0);
327 }
328 SetFrameSlot(offset, value);
329}
330
331
332void FrameDescription::SetCallerFp(unsigned offset, intptr_t value) {
333 if (kFPOnStackSize == 2 * kPointerSize) {
334 // Zero out the high-32 bit of FP for x32 port.
335 SetFrameSlot(offset + kPointerSize, 0);
336 }
337 SetFrameSlot(offset, value);
338}
339
340
341void FrameDescription::SetCallerConstantPool(unsigned offset, intptr_t value) {
342 // No out-of-line constant pool support.
343 UNREACHABLE();
344}
345
346
Steve Block1e0659c2011-05-24 12:43:12 +0100347#undef __
348
349
Ben Murdochb0fe1622011-05-05 13:52:32 +0100350} } // namespace v8::internal
Ben Murdochb8e0da22011-05-16 14:20:40 +0100351
352#endif // V8_TARGET_ARCH_X64