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.
10 #include "deoptimizer.h"
11 #include "full-codegen.h"
12 #include "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 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());
82 prev_call_address = call_address;
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);
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);
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));
109 void Deoptimizer::SetPlatformCompiledStubRegisters(
110 FrameDescription* output_frame, CodeStubInterfaceDescriptor* descriptor) {
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);
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);
127 bool Deoptimizer::HasAlignmentPadding(JSFunction* function) {
128 // There is no dynamic alignment padding on x64 in the input frame.
133 Code* Deoptimizer::NotifyStubFailureBuiltin() {
134 return isolate_->builtins()->builtin(Builtins::kNotifyStubFailureSaveDoubles);
140 void Deoptimizer::EntryGenerator::Generate() {
143 // Save all general purpose registers before messing with them.
144 const int kNumberOfRegisters = Register::kNumRegisters;
146 const int kXMMRegsSize = kSIMD128Size *
147 XMMRegister::NumAllocatableRegisters();
148 __ subp(rsp, Immediate(kXMMRegsSize));
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);
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);
163 const int kSavedRegistersAreaSize = kNumberOfRegisters * kRegisterSize +
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.
171 // Get the bailout id from the stack.
172 __ movp(arg_reg_3, Operand(rsp, kSavedRegistersAreaSize));
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 +
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.
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.
193 __ movq(Operand(rsp, 4 * kRegisterSize), arg5);
194 __ LoadAddress(arg5, ExternalReference::isolate_address(isolate()));
195 __ movq(Operand(rsp, 5 * kRegisterSize), arg5);
198 __ LoadAddress(r9, ExternalReference::isolate_address(isolate()));
201 { AllowExternalCallThatCantCauseGC scope(masm());
202 __ CallCFunction(ExternalReference::new_deoptimizer_function(isolate()), 6);
204 // Preserve deoptimizer object in register rax and get the input
205 // frame descriptor pointer.
206 __ movp(rbx, Operand(rax, Deoptimizer::input_offset()));
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));
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));
223 // Remove the bailout id and return address from the stack.
224 __ addp(rsp, Immediate(1 * kRegisterSize + kPCOnStackSize));
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()));
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);
239 __ Pop(Operand(rdx, 0));
240 __ addp(rdx, Immediate(sizeof(intptr_t)));
241 __ bind(&pop_loop_header);
243 __ j(not_equal, &pop_loop);
245 // Compute the output frame in the deoptimizer.
247 __ PrepareCallCFunction(2);
248 __ movp(arg_reg_1, rax);
249 __ LoadAddress(arg_reg_2, ExternalReference::isolate_address(isolate()));
251 AllowExternalCallThatCantCauseGC scope(masm());
253 ExternalReference::compute_output_frames_function(isolate()), 2);
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);
276 __ j(not_zero, &inner_push_loop);
277 __ addp(rax, Immediate(kPointerSize));
278 __ bind(&outer_loop_header);
280 __ j(below, &outer_push_loop);
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));
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()));
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));
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.
306 r = Register::from_code(i - 1);
311 // Set up the roots register.
312 __ InitializeRootRegister();
313 __ InitializeSmiConstantRegister();
315 // Return to the continuation point.
320 void Deoptimizer::TableEntryGenerator::GeneratePrologue() {
321 // Create a sequence of deoptimization entries.
323 for (int i = 0; i < count(); i++) {
324 int start = masm()->pc_offset();
328 ASSERT(masm()->pc_offset() - start == table_entry_size_);
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);
339 SetFrameSlot(offset, value);
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);
348 SetFrameSlot(offset, value);
352 void FrameDescription::SetCallerConstantPool(unsigned offset, intptr_t value) {
353 // No out-of-line constant pool support.
358 double FrameDescription::GetDoubleRegister(unsigned n) const {
359 ASSERT(n < ARRAY_SIZE(simd128_registers_));
360 return simd128_registers_[n].d[0];
364 void FrameDescription::SetDoubleRegister(unsigned n, double value) {
365 ASSERT(n < ARRAY_SIZE(simd128_registers_));
366 simd128_registers_[n].d[0] = value;
373 } } // namespace v8::internal
375 #endif // V8_TARGET_ARCH_X64