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, CodeStubDescriptor* 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::CopyDoubleRegisters(FrameDescription* output_frame) {
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);
128 bool Deoptimizer::HasAlignmentPadding(JSFunction* function) {
129 // There is no dynamic alignment padding on x64 in the input frame.
136 void Deoptimizer::EntryGenerator::Generate() {
139 // Save all general purpose registers before messing with them.
140 const int kNumberOfRegisters = Register::kNumRegisters;
142 const int kXMMRegsSize = kSIMD128Size *
143 XMMRegister::NumAllocatableRegisters();
144 __ subp(rsp, Immediate(kXMMRegsSize));
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);
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);
159 const int kSavedRegistersAreaSize = kNumberOfRegisters * kRegisterSize +
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.
167 // Get the bailout id from the stack.
168 __ movp(arg_reg_3, Operand(rsp, kSavedRegistersAreaSize));
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 +
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.
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.
189 __ movq(Operand(rsp, 4 * kRegisterSize), arg5);
190 __ LoadAddress(arg5, ExternalReference::isolate_address(isolate()));
191 __ movq(Operand(rsp, 5 * kRegisterSize), arg5);
194 __ LoadAddress(r9, ExternalReference::isolate_address(isolate()));
197 { AllowExternalCallThatCantCauseGC scope(masm());
198 __ CallCFunction(ExternalReference::new_deoptimizer_function(isolate()), 6);
200 // Preserve deoptimizer object in register rax and get the input
201 // frame descriptor pointer.
202 __ movp(rbx, Operand(rax, Deoptimizer::input_offset()));
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));
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));
219 // Remove the bailout id and return address from the stack.
220 __ addp(rsp, Immediate(1 * kRegisterSize + kPCOnStackSize));
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()));
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);
235 __ Pop(Operand(rdx, 0));
236 __ addp(rdx, Immediate(sizeof(intptr_t)));
237 __ bind(&pop_loop_header);
239 __ j(not_equal, &pop_loop);
241 // Compute the output frame in the deoptimizer.
243 __ PrepareCallCFunction(2);
244 __ movp(arg_reg_1, rax);
245 __ LoadAddress(arg_reg_2, ExternalReference::isolate_address(isolate()));
247 AllowExternalCallThatCantCauseGC scope(masm());
249 ExternalReference::compute_output_frames_function(isolate()), 2);
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);
272 __ j(not_zero, &inner_push_loop);
273 __ addp(rax, Immediate(kPointerSize));
274 __ bind(&outer_loop_header);
276 __ j(below, &outer_push_loop);
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));
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()));
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));
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.
302 r = Register::from_code(i - 1);
307 // Set up the roots register.
308 __ InitializeRootRegister();
309 __ InitializeSmiConstantRegister();
311 // Return to the continuation point.
316 void Deoptimizer::TableEntryGenerator::GeneratePrologue() {
317 // Create a sequence of deoptimization entries.
319 for (int i = 0; i < count(); i++) {
320 int start = masm()->pc_offset();
324 DCHECK(masm()->pc_offset() - start == table_entry_size_);
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);
335 SetFrameSlot(offset, value);
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);
344 SetFrameSlot(offset, value);
348 void FrameDescription::SetCallerConstantPool(unsigned offset, intptr_t value) {
349 // No out-of-line constant pool support.
354 double FrameDescription::GetDoubleRegister(unsigned n) const {
355 DCHECK(n < arraysize(simd128_registers_));
356 return simd128_registers_[n].d[0];
360 void FrameDescription::SetDoubleRegister(unsigned n, double value) {
361 DCHECK(n < arraysize(simd128_registers_));
362 simd128_registers_[n].d[0] = value;
366 simd128_value_t FrameDescription::GetSIMD128Register(unsigned n) const {
367 DCHECK(n < arraysize(simd128_registers_));
368 return simd128_registers_[n];
372 void FrameDescription::SetSIMD128Register(unsigned n, simd128_value_t value) {
373 DCHECK(n < arraysize(simd128_registers_));
374 simd128_registers_[n] = value;
378 int FrameDescription::double_registers_offset() {
379 return OFFSET_OF(FrameDescription, simd128_registers_);
383 int FrameDescription::simd128_registers_offset() {
384 return OFFSET_OF(FrameDescription, simd128_registers_);
391 } } // namespace v8::internal
393 #endif // V8_TARGET_ARCH_X64