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.
9 #include "src/codegen.h"
10 #include "src/deoptimizer.h"
11 #include "src/full-codegen.h"
12 #include "src/safepoint-table.h"
18 const int Deoptimizer::table_entry_size_ = 10;
21 int Deoptimizer::patch_size() {
22 return Assembler::kCallSequenceLength;
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();
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;
37 pointer = code->instruction_start();
39 CodePatcher patcher(pointer, 1);
40 patcher.masm()->int3();
42 DeoptimizationInputData* data =
43 DeoptimizationInputData::cast(code->deoptimization_data());
44 int osr_offset = data->OsrPcOffset()->value();
46 CodePatcher osr_patcher(code->instruction_start() + osr_offset, 1);
47 osr_patcher.masm()->int3();
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).
57 Address instruction_start = code->instruction_start();
59 Address prev_call_address = NULL;
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());
79 prev_call_address = call_address;
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);
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);
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));
106 void Deoptimizer::SetPlatformCompiledStubRegisters(
107 FrameDescription* output_frame, CodeStubInterfaceDescriptor* descriptor) {
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);
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);
124 bool Deoptimizer::HasAlignmentPadding(JSFunction* function) {
125 // There is no dynamic alignment padding on x64 in the input frame.
132 void Deoptimizer::EntryGenerator::Generate() {
135 // Save all general purpose registers before messing with them.
136 const int kNumberOfRegisters = Register::kNumRegisters;
138 const int kXMMRegsSize = kSIMD128Size *
139 XMMRegister::NumAllocatableRegisters();
140 __ subp(rsp, Immediate(kXMMRegsSize));
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);
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);
155 const int kSavedRegistersAreaSize = kNumberOfRegisters * kRegisterSize +
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.
163 // Get the bailout id from the stack.
164 __ movp(arg_reg_3, Operand(rsp, kSavedRegistersAreaSize));
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 +
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.
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.
185 __ movq(Operand(rsp, 4 * kRegisterSize), arg5);
186 __ LoadAddress(arg5, ExternalReference::isolate_address(isolate()));
187 __ movq(Operand(rsp, 5 * kRegisterSize), arg5);
190 __ LoadAddress(r9, ExternalReference::isolate_address(isolate()));
193 { AllowExternalCallThatCantCauseGC scope(masm());
194 __ CallCFunction(ExternalReference::new_deoptimizer_function(isolate()), 6);
196 // Preserve deoptimizer object in register rax and get the input
197 // frame descriptor pointer.
198 __ movp(rbx, Operand(rax, Deoptimizer::input_offset()));
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));
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));
215 // Remove the bailout id and return address from the stack.
216 __ addp(rsp, Immediate(1 * kRegisterSize + kPCOnStackSize));
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()));
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);
231 __ Pop(Operand(rdx, 0));
232 __ addp(rdx, Immediate(sizeof(intptr_t)));
233 __ bind(&pop_loop_header);
235 __ j(not_equal, &pop_loop);
237 // Compute the output frame in the deoptimizer.
239 __ PrepareCallCFunction(2);
240 __ movp(arg_reg_1, rax);
241 __ LoadAddress(arg_reg_2, ExternalReference::isolate_address(isolate()));
243 AllowExternalCallThatCantCauseGC scope(masm());
245 ExternalReference::compute_output_frames_function(isolate()), 2);
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);
268 __ j(not_zero, &inner_push_loop);
269 __ addp(rax, Immediate(kPointerSize));
270 __ bind(&outer_loop_header);
272 __ j(below, &outer_push_loop);
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));
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()));
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));
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.
298 r = Register::from_code(i - 1);
303 // Set up the roots register.
304 __ InitializeRootRegister();
305 __ InitializeSmiConstantRegister();
307 // Return to the continuation point.
312 void Deoptimizer::TableEntryGenerator::GeneratePrologue() {
313 // Create a sequence of deoptimization entries.
315 for (int i = 0; i < count(); i++) {
316 int start = masm()->pc_offset();
320 DCHECK(masm()->pc_offset() - start == table_entry_size_);
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);
331 SetFrameSlot(offset, value);
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);
340 SetFrameSlot(offset, value);
344 void FrameDescription::SetCallerConstantPool(unsigned offset, intptr_t value) {
345 // No out-of-line constant pool support.
350 double FrameDescription::GetDoubleRegister(unsigned n) const {
351 DCHECK(n < ARRAY_SIZE(simd128_registers_));
352 return simd128_registers_[n].d[0];
356 void FrameDescription::SetDoubleRegister(unsigned n, double value) {
357 DCHECK(n < ARRAY_SIZE(simd128_registers_));
358 simd128_registers_[n].d[0] = value;
365 } } // namespace v8::internal
367 #endif // V8_TARGET_ARCH_X64