• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #if V8_TARGET_ARCH_IA32
6 
7 #include "src/codegen.h"
8 #include "src/deoptimizer.h"
9 #include "src/full-codegen/full-codegen.h"
10 #include "src/ia32/frames-ia32.h"
11 #include "src/register-configuration.h"
12 #include "src/safepoint-table.h"
13 
14 namespace v8 {
15 namespace internal {
16 
17 const int Deoptimizer::table_entry_size_ = 10;
18 
19 
patch_size()20 int Deoptimizer::patch_size() {
21   return Assembler::kCallInstructionLength;
22 }
23 
24 
EnsureRelocSpaceForLazyDeoptimization(Handle<Code> code)25 void Deoptimizer::EnsureRelocSpaceForLazyDeoptimization(Handle<Code> code) {
26   Isolate* isolate = code->GetIsolate();
27   HandleScope scope(isolate);
28 
29   // Compute the size of relocation information needed for the code
30   // patching in Deoptimizer::PatchCodeForDeoptimization below.
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     pc_offset = pc_offset + 1;  // We will encode the pc offset after the call.
39     DCHECK_GE(pc_offset, prev_pc_offset);
40     int pc_delta = pc_offset - prev_pc_offset;
41     // We use RUNTIME_ENTRY reloc info which has a size of 2 bytes
42     // if encodable with small pc delta encoding and up to 6 bytes
43     // otherwise.
44     if (pc_delta <= RelocInfo::kMaxSmallPCDelta) {
45       min_reloc_size += 2;
46     } else {
47       min_reloc_size += 6;
48     }
49     prev_pc_offset = pc_offset;
50   }
51 
52   // If the relocation information is not big enough we create a new
53   // relocation info object that is padded with comments to make it
54   // big enough for lazy doptimization.
55   int reloc_length = code->relocation_info()->length();
56   if (min_reloc_size > reloc_length) {
57     int comment_reloc_size = RelocInfo::kMinRelocCommentSize;
58     // Padding needed.
59     int min_padding = min_reloc_size - reloc_length;
60     // Number of comments needed to take up at least that much space.
61     int additional_comments =
62         (min_padding + comment_reloc_size - 1) / comment_reloc_size;
63     // Actual padding size.
64     int padding = additional_comments * comment_reloc_size;
65     // Allocate new relocation info and copy old relocation to the end
66     // of the new relocation info array because relocation info is
67     // written and read backwards.
68     Factory* factory = isolate->factory();
69     Handle<ByteArray> new_reloc =
70         factory->NewByteArray(reloc_length + padding, TENURED);
71     MemCopy(new_reloc->GetDataStartAddress() + padding,
72             code->relocation_info()->GetDataStartAddress(), reloc_length);
73     // Create a relocation writer to write the comments in the padding
74     // space. Use position 0 for everything to ensure short encoding.
75     RelocInfoWriter reloc_info_writer(
76         new_reloc->GetDataStartAddress() + padding, 0);
77     intptr_t comment_string
78         = reinterpret_cast<intptr_t>(RelocInfo::kFillerCommentString);
79     RelocInfo rinfo(isolate, 0, RelocInfo::COMMENT, comment_string, NULL);
80     for (int i = 0; i < additional_comments; ++i) {
81 #ifdef DEBUG
82       byte* pos_before = reloc_info_writer.pos();
83 #endif
84       reloc_info_writer.Write(&rinfo);
85       DCHECK(RelocInfo::kMinRelocCommentSize ==
86              pos_before - reloc_info_writer.pos());
87     }
88     // Replace relocation information on the code object.
89     code->set_relocation_info(*new_reloc);
90   }
91 }
92 
93 
PatchCodeForDeoptimization(Isolate * isolate,Code * code)94 void Deoptimizer::PatchCodeForDeoptimization(Isolate* isolate, Code* code) {
95   Address code_start_address = code->instruction_start();
96 
97   if (FLAG_zap_code_space) {
98     // Fail hard and early if we enter this code object again.
99     byte* pointer = code->FindCodeAgeSequence();
100     if (pointer != NULL) {
101       pointer += kNoCodeAgeSequenceLength;
102     } else {
103       pointer = code->instruction_start();
104     }
105     CodePatcher patcher(isolate, pointer, 1);
106     patcher.masm()->int3();
107 
108     DeoptimizationInputData* data =
109         DeoptimizationInputData::cast(code->deoptimization_data());
110     int osr_offset = data->OsrPcOffset()->value();
111     if (osr_offset > 0) {
112       CodePatcher osr_patcher(isolate, code->instruction_start() + osr_offset,
113                               1);
114       osr_patcher.masm()->int3();
115     }
116   }
117 
118   // We will overwrite the code's relocation info in-place. Relocation info
119   // is written backward. The relocation info is the payload of a byte
120   // array.  Later on we will slide this to the start of the byte array and
121   // create a filler object in the remaining space.
122   ByteArray* reloc_info = code->relocation_info();
123   Address reloc_end_address = reloc_info->address() + reloc_info->Size();
124   RelocInfoWriter reloc_info_writer(reloc_end_address, code_start_address);
125 
126   // Since the call is a relative encoding, write new
127   // reloc info.  We do not need any of the existing reloc info because the
128   // existing code will not be used again (we zap it in debug builds).
129   //
130   // Emit call to lazy deoptimization at all lazy deopt points.
131   DeoptimizationInputData* deopt_data =
132       DeoptimizationInputData::cast(code->deoptimization_data());
133 #ifdef DEBUG
134   Address prev_call_address = NULL;
135 #endif
136   // For each LLazyBailout instruction insert a call to the corresponding
137   // deoptimization entry.
138   for (int i = 0; i < deopt_data->DeoptCount(); i++) {
139     if (deopt_data->Pc(i)->value() == -1) continue;
140     // Patch lazy deoptimization entry.
141     Address call_address = code_start_address + deopt_data->Pc(i)->value();
142     CodePatcher patcher(isolate, call_address, patch_size());
143     Address deopt_entry = GetDeoptimizationEntry(isolate, i, LAZY);
144     patcher.masm()->call(deopt_entry, RelocInfo::NONE32);
145     // We use RUNTIME_ENTRY for deoptimization bailouts.
146     RelocInfo rinfo(isolate, call_address + 1,  // 1 after the call opcode.
147                     RelocInfo::RUNTIME_ENTRY,
148                     reinterpret_cast<intptr_t>(deopt_entry), NULL);
149     reloc_info_writer.Write(&rinfo);
150     DCHECK_GE(reloc_info_writer.pos(),
151               reloc_info->address() + ByteArray::kHeaderSize);
152     DCHECK(prev_call_address == NULL ||
153            call_address >= prev_call_address + patch_size());
154     DCHECK(call_address + patch_size() <= code->instruction_end());
155 #ifdef DEBUG
156     prev_call_address = call_address;
157 #endif
158   }
159 
160   // Move the relocation info to the beginning of the byte array.
161   const int new_reloc_length = reloc_end_address - reloc_info_writer.pos();
162   MemMove(code->relocation_start(), reloc_info_writer.pos(), new_reloc_length);
163 
164   // Right trim the relocation info to free up remaining space.
165   const int delta = reloc_info->length() - new_reloc_length;
166   if (delta > 0) {
167     isolate->heap()->RightTrimFixedArray<Heap::SEQUENTIAL_TO_SWEEPER>(
168         reloc_info, delta);
169   }
170 }
171 
172 
SetPlatformCompiledStubRegisters(FrameDescription * output_frame,CodeStubDescriptor * descriptor)173 void Deoptimizer::SetPlatformCompiledStubRegisters(
174     FrameDescription* output_frame, CodeStubDescriptor* descriptor) {
175   intptr_t handler =
176       reinterpret_cast<intptr_t>(descriptor->deoptimization_handler());
177   int params = descriptor->GetHandlerParameterCount();
178   output_frame->SetRegister(eax.code(), params);
179   output_frame->SetRegister(ebx.code(), handler);
180 }
181 
182 
CopyDoubleRegisters(FrameDescription * output_frame)183 void Deoptimizer::CopyDoubleRegisters(FrameDescription* output_frame) {
184   for (int i = 0; i < XMMRegister::kMaxNumRegisters; ++i) {
185     double double_value = input_->GetDoubleRegister(i);
186     output_frame->SetDoubleRegister(i, double_value);
187   }
188 }
189 
190 #define __ masm()->
191 
Generate()192 void Deoptimizer::TableEntryGenerator::Generate() {
193   GeneratePrologue();
194 
195   // Save all general purpose registers before messing with them.
196   const int kNumberOfRegisters = Register::kNumRegisters;
197 
198   const int kDoubleRegsSize = kDoubleSize * XMMRegister::kMaxNumRegisters;
199   __ sub(esp, Immediate(kDoubleRegsSize));
200   const RegisterConfiguration* config = RegisterConfiguration::Crankshaft();
201   for (int i = 0; i < config->num_allocatable_double_registers(); ++i) {
202     int code = config->GetAllocatableDoubleCode(i);
203     XMMRegister xmm_reg = XMMRegister::from_code(code);
204     int offset = code * kDoubleSize;
205     __ movsd(Operand(esp, offset), xmm_reg);
206   }
207 
208   __ pushad();
209 
210   ExternalReference c_entry_fp_address(Isolate::kCEntryFPAddress, isolate());
211   __ mov(Operand::StaticVariable(c_entry_fp_address), ebp);
212 
213   const int kSavedRegistersAreaSize = kNumberOfRegisters * kPointerSize +
214                                       kDoubleRegsSize;
215 
216   // Get the bailout id from the stack.
217   __ mov(ebx, Operand(esp, kSavedRegistersAreaSize));
218 
219   // Get the address of the location in the code object
220   // and compute the fp-to-sp delta in register edx.
221   __ mov(ecx, Operand(esp, kSavedRegistersAreaSize + 1 * kPointerSize));
222   __ lea(edx, Operand(esp, kSavedRegistersAreaSize + 2 * kPointerSize));
223 
224   __ sub(edx, ebp);
225   __ neg(edx);
226 
227   // Allocate a new deoptimizer object.
228   __ PrepareCallCFunction(6, eax);
229   __ mov(eax, Immediate(0));
230   Label context_check;
231   __ mov(edi, Operand(ebp, CommonFrameConstants::kContextOrFrameTypeOffset));
232   __ JumpIfSmi(edi, &context_check);
233   __ mov(eax, Operand(ebp, JavaScriptFrameConstants::kFunctionOffset));
234   __ bind(&context_check);
235   __ mov(Operand(esp, 0 * kPointerSize), eax);  // Function.
236   __ mov(Operand(esp, 1 * kPointerSize), Immediate(type()));  // Bailout type.
237   __ mov(Operand(esp, 2 * kPointerSize), ebx);  // Bailout id.
238   __ mov(Operand(esp, 3 * kPointerSize), ecx);  // Code address or 0.
239   __ mov(Operand(esp, 4 * kPointerSize), edx);  // Fp-to-sp delta.
240   __ mov(Operand(esp, 5 * kPointerSize),
241          Immediate(ExternalReference::isolate_address(isolate())));
242   {
243     AllowExternalCallThatCantCauseGC scope(masm());
244     __ CallCFunction(ExternalReference::new_deoptimizer_function(isolate()), 6);
245   }
246 
247   // Preserve deoptimizer object in register eax and get the input
248   // frame descriptor pointer.
249   __ mov(ebx, Operand(eax, Deoptimizer::input_offset()));
250 
251   // Fill in the input registers.
252   for (int i = kNumberOfRegisters - 1; i >= 0; i--) {
253     int offset = (i * kPointerSize) + FrameDescription::registers_offset();
254     __ pop(Operand(ebx, offset));
255   }
256 
257   int double_regs_offset = FrameDescription::double_registers_offset();
258   // Fill in the double input registers.
259   for (int i = 0; i < config->num_allocatable_double_registers(); ++i) {
260     int code = config->GetAllocatableDoubleCode(i);
261     int dst_offset = code * kDoubleSize + double_regs_offset;
262     int src_offset = code * kDoubleSize;
263     __ movsd(xmm0, Operand(esp, src_offset));
264     __ movsd(Operand(ebx, dst_offset), xmm0);
265   }
266 
267   // Clear FPU all exceptions.
268   // TODO(ulan): Find out why the TOP register is not zero here in some cases,
269   // and check that the generated code never deoptimizes with unbalanced stack.
270   __ fnclex();
271 
272   // Remove the bailout id, return address and the double registers.
273   __ add(esp, Immediate(kDoubleRegsSize + 2 * kPointerSize));
274 
275   // Compute a pointer to the unwinding limit in register ecx; that is
276   // the first stack slot not part of the input frame.
277   __ mov(ecx, Operand(ebx, FrameDescription::frame_size_offset()));
278   __ add(ecx, esp);
279 
280   // Unwind the stack down to - but not including - the unwinding
281   // limit and copy the contents of the activation frame to the input
282   // frame description.
283   __ lea(edx, Operand(ebx, FrameDescription::frame_content_offset()));
284   Label pop_loop_header;
285   __ jmp(&pop_loop_header);
286   Label pop_loop;
287   __ bind(&pop_loop);
288   __ pop(Operand(edx, 0));
289   __ add(edx, Immediate(sizeof(uint32_t)));
290   __ bind(&pop_loop_header);
291   __ cmp(ecx, esp);
292   __ j(not_equal, &pop_loop);
293 
294   // Compute the output frame in the deoptimizer.
295   __ push(eax);
296   __ PrepareCallCFunction(1, ebx);
297   __ mov(Operand(esp, 0 * kPointerSize), eax);
298   {
299     AllowExternalCallThatCantCauseGC scope(masm());
300     __ CallCFunction(
301         ExternalReference::compute_output_frames_function(isolate()), 1);
302   }
303   __ pop(eax);
304 
305   __ mov(esp, Operand(eax, Deoptimizer::caller_frame_top_offset()));
306 
307   // Replace the current (input) frame with the output frames.
308   Label outer_push_loop, inner_push_loop,
309       outer_loop_header, inner_loop_header;
310   // Outer loop state: eax = current FrameDescription**, edx = one past the
311   // last FrameDescription**.
312   __ mov(edx, Operand(eax, Deoptimizer::output_count_offset()));
313   __ mov(eax, Operand(eax, Deoptimizer::output_offset()));
314   __ lea(edx, Operand(eax, edx, times_4, 0));
315   __ jmp(&outer_loop_header);
316   __ bind(&outer_push_loop);
317   // Inner loop state: ebx = current FrameDescription*, ecx = loop index.
318   __ mov(ebx, Operand(eax, 0));
319   __ mov(ecx, Operand(ebx, FrameDescription::frame_size_offset()));
320   __ jmp(&inner_loop_header);
321   __ bind(&inner_push_loop);
322   __ sub(ecx, Immediate(sizeof(uint32_t)));
323   __ push(Operand(ebx, ecx, times_1, FrameDescription::frame_content_offset()));
324   __ bind(&inner_loop_header);
325   __ test(ecx, ecx);
326   __ j(not_zero, &inner_push_loop);
327   __ add(eax, Immediate(kPointerSize));
328   __ bind(&outer_loop_header);
329   __ cmp(eax, edx);
330   __ j(below, &outer_push_loop);
331 
332   // In case of a failed STUB, we have to restore the XMM registers.
333   for (int i = 0; i < config->num_allocatable_double_registers(); ++i) {
334     int code = config->GetAllocatableDoubleCode(i);
335     XMMRegister xmm_reg = XMMRegister::from_code(code);
336     int src_offset = code * kDoubleSize + double_regs_offset;
337     __ movsd(xmm_reg, Operand(ebx, src_offset));
338   }
339 
340   // Push state, pc, and continuation from the last output frame.
341   __ push(Operand(ebx, FrameDescription::state_offset()));
342   __ push(Operand(ebx, FrameDescription::pc_offset()));
343   __ push(Operand(ebx, FrameDescription::continuation_offset()));
344 
345 
346   // Push the registers from the last output frame.
347   for (int i = 0; i < kNumberOfRegisters; i++) {
348     int offset = (i * kPointerSize) + FrameDescription::registers_offset();
349     __ push(Operand(ebx, offset));
350   }
351 
352   // Restore the registers from the stack.
353   __ popad();
354 
355   // Return to the continuation point.
356   __ ret(0);
357 }
358 
359 
GeneratePrologue()360 void Deoptimizer::TableEntryGenerator::GeneratePrologue() {
361   // Create a sequence of deoptimization entries.
362   Label done;
363   for (int i = 0; i < count(); i++) {
364     int start = masm()->pc_offset();
365     USE(start);
366     __ push_imm32(i);
367     __ jmp(&done);
368     DCHECK(masm()->pc_offset() - start == table_entry_size_);
369   }
370   __ bind(&done);
371 }
372 
373 
SetCallerPc(unsigned offset,intptr_t value)374 void FrameDescription::SetCallerPc(unsigned offset, intptr_t value) {
375   SetFrameSlot(offset, value);
376 }
377 
378 
SetCallerFp(unsigned offset,intptr_t value)379 void FrameDescription::SetCallerFp(unsigned offset, intptr_t value) {
380   SetFrameSlot(offset, value);
381 }
382 
383 
SetCallerConstantPool(unsigned offset,intptr_t value)384 void FrameDescription::SetCallerConstantPool(unsigned offset, intptr_t value) {
385   // No embedded constant pool support.
386   UNREACHABLE();
387 }
388 
389 
390 #undef __
391 
392 
393 }  // namespace internal
394 }  // namespace v8
395 
396 #endif  // V8_TARGET_ARCH_IA32
397