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.
7 #if V8_TARGET_ARCH_IA32
9 #include "src/codegen.h"
10 #include "src/deoptimizer.h"
11 #include "src/full-codegen.h"
12 #include "src/safepoint-table.h"
17 const int Deoptimizer::table_entry_size_ = 10;
20 int Deoptimizer::patch_size() {
21 return Assembler::kCallInstructionLength;
25 void Deoptimizer::EnsureRelocSpaceForLazyDeoptimization(Handle<Code> code) {
26 Isolate* isolate = code->GetIsolate();
27 HandleScope scope(isolate);
29 // Compute the size of relocation information needed for the code
30 // patching in Deoptimizer::DeoptimizeFunction.
31 int min_reloc_size = 0;
32 int prev_pc_offset = 0;
33 DeoptimizationInputData* deopt_data =
34 DeoptimizationInputData::cast(code->deoptimization_data());
35 for (int i = 0; i < deopt_data->DeoptCount(); i++) {
36 int pc_offset = deopt_data->Pc(i)->value();
37 if (pc_offset == -1) continue;
38 DCHECK_GE(pc_offset, prev_pc_offset);
39 int pc_delta = pc_offset - prev_pc_offset;
40 // We use RUNTIME_ENTRY reloc info which has a size of 2 bytes
41 // if encodable with small pc delta encoding and up to 6 bytes
43 if (pc_delta <= RelocInfo::kMaxSmallPCDelta) {
48 prev_pc_offset = pc_offset;
51 // If the relocation information is not big enough we create a new
52 // relocation info object that is padded with comments to make it
53 // big enough for lazy doptimization.
54 int reloc_length = code->relocation_info()->length();
55 if (min_reloc_size > reloc_length) {
56 int comment_reloc_size = RelocInfo::kMinRelocCommentSize;
58 int min_padding = min_reloc_size - reloc_length;
59 // Number of comments needed to take up at least that much space.
60 int additional_comments =
61 (min_padding + comment_reloc_size - 1) / comment_reloc_size;
62 // Actual padding size.
63 int padding = additional_comments * comment_reloc_size;
64 // Allocate new relocation info and copy old relocation to the end
65 // of the new relocation info array because relocation info is
66 // written and read backwards.
67 Factory* factory = isolate->factory();
68 Handle<ByteArray> new_reloc =
69 factory->NewByteArray(reloc_length + padding, TENURED);
70 MemCopy(new_reloc->GetDataStartAddress() + padding,
71 code->relocation_info()->GetDataStartAddress(), reloc_length);
72 // Create a relocation writer to write the comments in the padding
73 // space. Use position 0 for everything to ensure short encoding.
74 RelocInfoWriter reloc_info_writer(
75 new_reloc->GetDataStartAddress() + padding, 0);
76 intptr_t comment_string
77 = reinterpret_cast<intptr_t>(RelocInfo::kFillerCommentString);
78 RelocInfo rinfo(0, RelocInfo::COMMENT, comment_string, NULL);
79 for (int i = 0; i < additional_comments; ++i) {
81 byte* pos_before = reloc_info_writer.pos();
83 reloc_info_writer.Write(&rinfo);
84 DCHECK(RelocInfo::kMinRelocCommentSize ==
85 pos_before - reloc_info_writer.pos());
87 // Replace relocation information on the code object.
88 code->set_relocation_info(*new_reloc);
93 void Deoptimizer::PatchCodeForDeoptimization(Isolate* isolate, Code* code) {
94 Address code_start_address = code->instruction_start();
96 if (FLAG_zap_code_space) {
97 // Fail hard and early if we enter this code object again.
98 byte* pointer = code->FindCodeAgeSequence();
99 if (pointer != NULL) {
100 pointer += kNoCodeAgeSequenceLength;
102 pointer = code->instruction_start();
104 CodePatcher patcher(pointer, 1);
105 patcher.masm()->int3();
107 DeoptimizationInputData* data =
108 DeoptimizationInputData::cast(code->deoptimization_data());
109 int osr_offset = data->OsrPcOffset()->value();
110 if (osr_offset > 0) {
111 CodePatcher osr_patcher(code->instruction_start() + osr_offset, 1);
112 osr_patcher.masm()->int3();
116 // We will overwrite the code's relocation info in-place. Relocation info
117 // is written backward. The relocation info is the payload of a byte
118 // array. Later on we will slide this to the start of the byte array and
119 // create a filler object in the remaining space.
120 ByteArray* reloc_info = code->relocation_info();
121 Address reloc_end_address = reloc_info->address() + reloc_info->Size();
122 RelocInfoWriter reloc_info_writer(reloc_end_address, code_start_address);
124 // Since the call is a relative encoding, write new
125 // reloc info. We do not need any of the existing reloc info because the
126 // existing code will not be used again (we zap it in debug builds).
128 // Emit call to lazy deoptimization at all lazy deopt points.
129 DeoptimizationInputData* deopt_data =
130 DeoptimizationInputData::cast(code->deoptimization_data());
132 Address prev_call_address = NULL;
134 // For each LLazyBailout instruction insert a call to the corresponding
135 // deoptimization entry.
136 for (int i = 0; i < deopt_data->DeoptCount(); i++) {
137 if (deopt_data->Pc(i)->value() == -1) continue;
138 // Patch lazy deoptimization entry.
139 Address call_address = code_start_address + deopt_data->Pc(i)->value();
140 CodePatcher patcher(call_address, patch_size());
141 Address deopt_entry = GetDeoptimizationEntry(isolate, i, LAZY);
142 patcher.masm()->call(deopt_entry, RelocInfo::NONE32);
143 // We use RUNTIME_ENTRY for deoptimization bailouts.
144 RelocInfo rinfo(call_address + 1, // 1 after the call opcode.
145 RelocInfo::RUNTIME_ENTRY,
146 reinterpret_cast<intptr_t>(deopt_entry),
148 reloc_info_writer.Write(&rinfo);
149 DCHECK_GE(reloc_info_writer.pos(),
150 reloc_info->address() + ByteArray::kHeaderSize);
151 DCHECK(prev_call_address == NULL ||
152 call_address >= prev_call_address + patch_size());
153 DCHECK(call_address + patch_size() <= code->instruction_end());
155 prev_call_address = call_address;
159 // Move the relocation info to the beginning of the byte array.
160 int new_reloc_size = reloc_end_address - reloc_info_writer.pos();
161 MemMove(code->relocation_start(), reloc_info_writer.pos(), new_reloc_size);
163 // The relocation info is in place, update the size.
164 reloc_info->set_length(new_reloc_size);
166 // Handle the junk part after the new relocation info. We will create
167 // a non-live object in the extra space at the end of the former reloc info.
168 Address junk_address = reloc_info->address() + reloc_info->Size();
169 DCHECK(junk_address <= reloc_end_address);
170 isolate->heap()->CreateFillerObjectAt(junk_address,
171 reloc_end_address - junk_address);
175 void Deoptimizer::FillInputFrame(Address tos, JavaScriptFrame* frame) {
176 // Set the register values. The values are not important as there are no
177 // callee saved registers in JavaScript frames, so all registers are
178 // spilled. Registers ebp and esp are set to the correct values though.
180 for (int i = 0; i < Register::kNumRegisters; i++) {
181 input_->SetRegister(i, i * 4);
183 input_->SetRegister(esp.code(), reinterpret_cast<intptr_t>(frame->sp()));
184 input_->SetRegister(ebp.code(), reinterpret_cast<intptr_t>(frame->fp()));
185 simd128_value_t zero = {{0.0, 0.0}};
186 for (int i = 0; i < XMMRegister::kMaxNumAllocatableRegisters; i++) {
187 input_->SetSIMD128Register(i, zero);
190 // Fill the frame content from the actual data on the frame.
191 for (unsigned i = 0; i < input_->GetFrameSize(); i += kPointerSize) {
192 input_->SetFrameSlot(i, Memory::uint32_at(tos + i));
197 void Deoptimizer::SetPlatformCompiledStubRegisters(
198 FrameDescription* output_frame, CodeStubDescriptor* descriptor) {
200 reinterpret_cast<intptr_t>(descriptor->deoptimization_handler());
201 int params = descriptor->GetHandlerParameterCount();
202 output_frame->SetRegister(eax.code(), params);
203 output_frame->SetRegister(ebx.code(), handler);
207 void Deoptimizer::CopyDoubleRegisters(FrameDescription* output_frame) {
211 void Deoptimizer::CopySIMD128Registers(FrameDescription* output_frame) {
212 for (int i = 0; i < XMMRegister::kMaxNumAllocatableRegisters; ++i) {
213 simd128_value_t xmm_value = input_->GetSIMD128Register(i);
214 output_frame->SetSIMD128Register(i, xmm_value);
219 bool Deoptimizer::HasAlignmentPadding(JSFunction* function) {
220 int parameter_count = function->shared()->formal_parameter_count() + 1;
221 unsigned input_frame_size = input_->GetFrameSize();
222 unsigned alignment_state_offset =
223 input_frame_size - parameter_count * kPointerSize -
224 StandardFrameConstants::kFixedFrameSize -
226 DCHECK(JavaScriptFrameConstants::kDynamicAlignmentStateOffset ==
227 JavaScriptFrameConstants::kLocal0Offset);
228 int32_t alignment_state = input_->GetFrameSlot(alignment_state_offset);
229 return (alignment_state == kAlignmentPaddingPushed);
235 void Deoptimizer::EntryGenerator::Generate() {
238 // Save all general purpose registers before messing with them.
239 const int kNumberOfRegisters = Register::kNumRegisters;
241 const int kXMMRegsSize = kSIMD128Size *
242 XMMRegister::kMaxNumAllocatableRegisters;
243 __ sub(esp, Immediate(kXMMRegsSize));
244 for (int i = 0; i < XMMRegister::kMaxNumAllocatableRegisters; ++i) {
245 XMMRegister xmm_reg = XMMRegister::FromAllocationIndex(i);
246 int offset = i * kSIMD128Size;
247 __ movups(Operand(esp, offset), xmm_reg);
252 const int kSavedRegistersAreaSize = kNumberOfRegisters * kPointerSize +
255 // Get the bailout id from the stack.
256 __ mov(ebx, Operand(esp, kSavedRegistersAreaSize));
258 // Get the address of the location in the code object
259 // and compute the fp-to-sp delta in register edx.
260 __ mov(ecx, Operand(esp, kSavedRegistersAreaSize + 1 * kPointerSize));
261 __ lea(edx, Operand(esp, kSavedRegistersAreaSize + 2 * kPointerSize));
266 // Allocate a new deoptimizer object.
267 __ PrepareCallCFunction(6, eax);
268 __ mov(eax, Operand(ebp, JavaScriptFrameConstants::kFunctionOffset));
269 __ mov(Operand(esp, 0 * kPointerSize), eax); // Function.
270 __ mov(Operand(esp, 1 * kPointerSize), Immediate(type())); // Bailout type.
271 __ mov(Operand(esp, 2 * kPointerSize), ebx); // Bailout id.
272 __ mov(Operand(esp, 3 * kPointerSize), ecx); // Code address or 0.
273 __ mov(Operand(esp, 4 * kPointerSize), edx); // Fp-to-sp delta.
274 __ mov(Operand(esp, 5 * kPointerSize),
275 Immediate(ExternalReference::isolate_address(isolate())));
277 AllowExternalCallThatCantCauseGC scope(masm());
278 __ CallCFunction(ExternalReference::new_deoptimizer_function(isolate()), 6);
281 // Preserve deoptimizer object in register eax and get the input
282 // frame descriptor pointer.
283 __ mov(ebx, Operand(eax, Deoptimizer::input_offset()));
285 // Fill in the input registers.
286 for (int i = kNumberOfRegisters - 1; i >= 0; i--) {
287 int offset = (i * kPointerSize) + FrameDescription::registers_offset();
288 __ pop(Operand(ebx, offset));
291 int xmm_regs_offset = FrameDescription::simd128_registers_offset();
292 // Fill in the double input registers.
293 for (int i = 0; i < XMMRegister::kMaxNumAllocatableRegisters; ++i) {
294 int dst_offset = i * kSIMD128Size + xmm_regs_offset;
295 int src_offset = i * kSIMD128Size;
296 __ movups(xmm0, Operand(esp, src_offset));
297 __ movups(Operand(ebx, dst_offset), xmm0);
300 // Clear FPU all exceptions.
301 // TODO(ulan): Find out why the TOP register is not zero here in some cases,
302 // and check that the generated code never deoptimizes with unbalanced stack.
305 // Remove the bailout id, return address and the double registers.
306 __ add(esp, Immediate(kXMMRegsSize + 2 * kPointerSize));
308 // Compute a pointer to the unwinding limit in register ecx; that is
309 // the first stack slot not part of the input frame.
310 __ mov(ecx, Operand(ebx, FrameDescription::frame_size_offset()));
313 // Unwind the stack down to - but not including - the unwinding
314 // limit and copy the contents of the activation frame to the input
315 // frame description.
316 __ lea(edx, Operand(ebx, FrameDescription::frame_content_offset()));
317 Label pop_loop_header;
318 __ jmp(&pop_loop_header);
321 __ pop(Operand(edx, 0));
322 __ add(edx, Immediate(sizeof(uint32_t)));
323 __ bind(&pop_loop_header);
325 __ j(not_equal, &pop_loop);
327 // Compute the output frame in the deoptimizer.
329 __ PrepareCallCFunction(1, ebx);
330 __ mov(Operand(esp, 0 * kPointerSize), eax);
332 AllowExternalCallThatCantCauseGC scope(masm());
334 ExternalReference::compute_output_frames_function(isolate()), 1);
338 // If frame was dynamically aligned, pop padding.
340 __ cmp(Operand(eax, Deoptimizer::has_alignment_padding_offset()),
342 __ j(equal, &no_padding);
344 if (FLAG_debug_code) {
345 __ cmp(ecx, Immediate(kAlignmentZapValue));
346 __ Assert(equal, kAlignmentMarkerExpected);
348 __ bind(&no_padding);
350 // Replace the current frame with the output frames.
351 Label outer_push_loop, inner_push_loop,
352 outer_loop_header, inner_loop_header;
353 // Outer loop state: eax = current FrameDescription**, edx = one past the
354 // last FrameDescription**.
355 __ mov(edx, Operand(eax, Deoptimizer::output_count_offset()));
356 __ mov(eax, Operand(eax, Deoptimizer::output_offset()));
357 __ lea(edx, Operand(eax, edx, times_4, 0));
358 __ jmp(&outer_loop_header);
359 __ bind(&outer_push_loop);
360 // Inner loop state: ebx = current FrameDescription*, ecx = loop index.
361 __ mov(ebx, Operand(eax, 0));
362 __ mov(ecx, Operand(ebx, FrameDescription::frame_size_offset()));
363 __ jmp(&inner_loop_header);
364 __ bind(&inner_push_loop);
365 __ sub(ecx, Immediate(sizeof(uint32_t)));
366 __ push(Operand(ebx, ecx, times_1, FrameDescription::frame_content_offset()));
367 __ bind(&inner_loop_header);
369 __ j(not_zero, &inner_push_loop);
370 __ add(eax, Immediate(kPointerSize));
371 __ bind(&outer_loop_header);
373 __ j(below, &outer_push_loop);
375 // In case of a failed STUB, we have to restore the XMM registers.
376 for (int i = 0; i < XMMRegister::kMaxNumAllocatableRegisters; ++i) {
377 XMMRegister xmm_reg = XMMRegister::FromAllocationIndex(i);
378 int src_offset = i * kSIMD128Size + xmm_regs_offset;
379 __ movups(xmm_reg, Operand(ebx, src_offset));
382 // Push state, pc, and continuation from the last output frame.
383 __ push(Operand(ebx, FrameDescription::state_offset()));
384 __ push(Operand(ebx, FrameDescription::pc_offset()));
385 __ push(Operand(ebx, FrameDescription::continuation_offset()));
388 // Push the registers from the last output frame.
389 for (int i = 0; i < kNumberOfRegisters; i++) {
390 int offset = (i * kPointerSize) + FrameDescription::registers_offset();
391 __ push(Operand(ebx, offset));
394 // Restore the registers from the stack.
397 // Return to the continuation point.
402 void Deoptimizer::TableEntryGenerator::GeneratePrologue() {
403 // Create a sequence of deoptimization entries.
405 for (int i = 0; i < count(); i++) {
406 int start = masm()->pc_offset();
410 DCHECK(masm()->pc_offset() - start == table_entry_size_);
416 void FrameDescription::SetCallerPc(unsigned offset, intptr_t value) {
417 SetFrameSlot(offset, value);
421 void FrameDescription::SetCallerFp(unsigned offset, intptr_t value) {
422 SetFrameSlot(offset, value);
426 void FrameDescription::SetCallerConstantPool(unsigned offset, intptr_t value) {
427 // No out-of-line constant pool support.
432 double FrameDescription::GetDoubleRegister(unsigned n) const {
433 DCHECK(n < arraysize(simd128_registers_));
434 return simd128_registers_[n].d[0];
438 void FrameDescription::SetDoubleRegister(unsigned n, double value) {
439 DCHECK(n < arraysize(simd128_registers_));
440 simd128_registers_[n].d[0] = value;
444 simd128_value_t FrameDescription::GetSIMD128Register(unsigned n) const {
445 DCHECK(n < arraysize(simd128_registers_));
446 return simd128_registers_[n];
450 void FrameDescription::SetSIMD128Register(unsigned n, simd128_value_t value) {
451 DCHECK(n < arraysize(simd128_registers_));
452 simd128_registers_[n] = value;
456 int FrameDescription::double_registers_offset() {
457 return OFFSET_OF(FrameDescription, simd128_registers_);
461 int FrameDescription::simd128_registers_offset() {
462 return OFFSET_OF(FrameDescription, simd128_registers_);
469 } } // namespace v8::internal
471 #endif // V8_TARGET_ARCH_IA32