blob: 9d4645e782524374e0e4339785f75f12040db1e9 [file] [log] [blame]
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001// 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
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005#if V8_TARGET_ARCH_X87
6
7#include "src/codegen.h"
8#include "src/deoptimizer.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00009#include "src/full-codegen/full-codegen.h"
10#include "src/register-configuration.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000011#include "src/safepoint-table.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000012#include "src/x87/frames-x87.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000013
14namespace v8 {
15namespace internal {
16
17const int Deoptimizer::table_entry_size_ = 10;
18
19
20int Deoptimizer::patch_size() {
21 return Assembler::kCallInstructionLength;
22}
23
24
25void 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
Emily Bernierd0a1eb72015-03-24 16:35:39 -040030 // patching in Deoptimizer::PatchCodeForDeoptimization below.
Ben Murdochb8a8cc12014-11-26 15:28:44 +000031 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
42 // otherwise.
43 if (pc_delta <= RelocInfo::kMaxSmallPCDelta) {
44 min_reloc_size += 2;
45 } else {
46 min_reloc_size += 6;
47 }
48 prev_pc_offset = pc_offset;
49 }
50
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;
57 // Padding needed.
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);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000078 RelocInfo rinfo(isolate, 0, RelocInfo::COMMENT, comment_string, NULL);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000079 for (int i = 0; i < additional_comments; ++i) {
80#ifdef DEBUG
81 byte* pos_before = reloc_info_writer.pos();
82#endif
83 reloc_info_writer.Write(&rinfo);
84 DCHECK(RelocInfo::kMinRelocCommentSize ==
85 pos_before - reloc_info_writer.pos());
86 }
87 // Replace relocation information on the code object.
88 code->set_relocation_info(*new_reloc);
89 }
90}
91
92
93void Deoptimizer::PatchCodeForDeoptimization(Isolate* isolate, Code* code) {
94 Address code_start_address = code->instruction_start();
95
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;
101 } else {
102 pointer = code->instruction_start();
103 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000104 CodePatcher patcher(isolate, pointer, 1);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000105 patcher.masm()->int3();
106
107 DeoptimizationInputData* data =
108 DeoptimizationInputData::cast(code->deoptimization_data());
109 int osr_offset = data->OsrPcOffset()->value();
110 if (osr_offset > 0) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000111 CodePatcher osr_patcher(isolate, code->instruction_start() + osr_offset,
112 1);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000113 osr_patcher.masm()->int3();
114 }
115 }
116
117 // We will overwrite the code's relocation info in-place. Relocation info
118 // is written backward. The relocation info is the payload of a byte
119 // array. Later on we will slide this to the start of the byte array and
120 // create a filler object in the remaining space.
121 ByteArray* reloc_info = code->relocation_info();
122 Address reloc_end_address = reloc_info->address() + reloc_info->Size();
123 RelocInfoWriter reloc_info_writer(reloc_end_address, code_start_address);
124
125 // Since the call is a relative encoding, write new
126 // reloc info. We do not need any of the existing reloc info because the
127 // existing code will not be used again (we zap it in debug builds).
128 //
129 // Emit call to lazy deoptimization at all lazy deopt points.
130 DeoptimizationInputData* deopt_data =
131 DeoptimizationInputData::cast(code->deoptimization_data());
132#ifdef DEBUG
133 Address prev_call_address = NULL;
134#endif
135 // For each LLazyBailout instruction insert a call to the corresponding
136 // deoptimization entry.
137 for (int i = 0; i < deopt_data->DeoptCount(); i++) {
138 if (deopt_data->Pc(i)->value() == -1) continue;
139 // Patch lazy deoptimization entry.
140 Address call_address = code_start_address + deopt_data->Pc(i)->value();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000141 CodePatcher patcher(isolate, call_address, patch_size());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000142 Address deopt_entry = GetDeoptimizationEntry(isolate, i, LAZY);
143 patcher.masm()->call(deopt_entry, RelocInfo::NONE32);
144 // We use RUNTIME_ENTRY for deoptimization bailouts.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000145 RelocInfo rinfo(isolate, call_address + 1, // 1 after the call opcode.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000146 RelocInfo::RUNTIME_ENTRY,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000147 reinterpret_cast<intptr_t>(deopt_entry), NULL);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000148 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());
154#ifdef DEBUG
155 prev_call_address = call_address;
156#endif
157 }
158
159 // Move the relocation info to the beginning of the byte array.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000160 const int new_reloc_length = reloc_end_address - reloc_info_writer.pos();
161 MemMove(code->relocation_start(), reloc_info_writer.pos(), new_reloc_length);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000162
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000163 // Right trim the relocation info to free up remaining space.
164 const int delta = reloc_info->length() - new_reloc_length;
165 if (delta > 0) {
166 isolate->heap()->RightTrimFixedArray<Heap::SEQUENTIAL_TO_SWEEPER>(
167 reloc_info, delta);
168 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000169}
170
171
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000172void Deoptimizer::SetPlatformCompiledStubRegisters(
173 FrameDescription* output_frame, CodeStubDescriptor* descriptor) {
174 intptr_t handler =
175 reinterpret_cast<intptr_t>(descriptor->deoptimization_handler());
176 int params = descriptor->GetHandlerParameterCount();
177 output_frame->SetRegister(eax.code(), params);
178 output_frame->SetRegister(ebx.code(), handler);
179}
180
181
182void Deoptimizer::CopyDoubleRegisters(FrameDescription* output_frame) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000183 for (int i = 0; i < X87Register::kMaxNumRegisters; ++i) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000184 double double_value = input_->GetDoubleRegister(i);
185 output_frame->SetDoubleRegister(i, double_value);
186 }
187}
188
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000189#define __ masm()->
190
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000191void Deoptimizer::TableEntryGenerator::Generate() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000192 GeneratePrologue();
193
194 // Save all general purpose registers before messing with them.
195 const int kNumberOfRegisters = Register::kNumRegisters;
196
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000197 const int kDoubleRegsSize = kDoubleSize * X87Register::kMaxNumRegisters;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000198
199 // Reserve space for x87 fp registers.
200 __ sub(esp, Immediate(kDoubleRegsSize));
201
202 __ pushad();
203
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000204 ExternalReference c_entry_fp_address(Isolate::kCEntryFPAddress, isolate());
205 __ mov(Operand::StaticVariable(c_entry_fp_address), ebp);
206
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000207 // GP registers are safe to use now.
208 // Save used x87 fp registers in correct position of previous reserve space.
209 Label loop, done;
210 // Get the layout of x87 stack.
211 __ sub(esp, Immediate(kPointerSize));
212 __ fistp_s(MemOperand(esp, 0));
213 __ pop(eax);
214 // Preserve stack layout in edi
215 __ mov(edi, eax);
216 // Get the x87 stack depth, the first 3 bits.
217 __ mov(ecx, eax);
218 __ and_(ecx, 0x7);
219 __ j(zero, &done, Label::kNear);
220
221 __ bind(&loop);
222 __ shr(eax, 0x3);
223 __ mov(ebx, eax);
224 __ and_(ebx, 0x7); // Extract the st_x index into ebx.
225 // Pop TOS to the correct position. The disp(0x20) is due to pushad.
226 // The st_i should be saved to (esp + ebx * kDoubleSize + 0x20).
227 __ fstp_d(Operand(esp, ebx, times_8, 0x20));
228 __ dec(ecx); // Decrease stack depth.
229 __ j(not_zero, &loop, Label::kNear);
230 __ bind(&done);
231
232 const int kSavedRegistersAreaSize =
233 kNumberOfRegisters * kPointerSize + kDoubleRegsSize;
234
235 // Get the bailout id from the stack.
236 __ mov(ebx, Operand(esp, kSavedRegistersAreaSize));
237
238 // Get the address of the location in the code object
239 // and compute the fp-to-sp delta in register edx.
240 __ mov(ecx, Operand(esp, kSavedRegistersAreaSize + 1 * kPointerSize));
241 __ lea(edx, Operand(esp, kSavedRegistersAreaSize + 2 * kPointerSize));
242
243 __ sub(edx, ebp);
244 __ neg(edx);
245
246 __ push(edi);
247 // Allocate a new deoptimizer object.
248 __ PrepareCallCFunction(6, eax);
Ben Murdochda12d292016-06-02 14:46:10 +0100249 __ mov(eax, Immediate(0));
250 Label context_check;
251 __ mov(edi, Operand(ebp, CommonFrameConstants::kContextOrFrameTypeOffset));
252 __ JumpIfSmi(edi, &context_check);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000253 __ mov(eax, Operand(ebp, JavaScriptFrameConstants::kFunctionOffset));
Ben Murdochda12d292016-06-02 14:46:10 +0100254 __ bind(&context_check);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000255 __ mov(Operand(esp, 0 * kPointerSize), eax); // Function.
256 __ mov(Operand(esp, 1 * kPointerSize), Immediate(type())); // Bailout type.
257 __ mov(Operand(esp, 2 * kPointerSize), ebx); // Bailout id.
258 __ mov(Operand(esp, 3 * kPointerSize), ecx); // Code address or 0.
259 __ mov(Operand(esp, 4 * kPointerSize), edx); // Fp-to-sp delta.
260 __ mov(Operand(esp, 5 * kPointerSize),
261 Immediate(ExternalReference::isolate_address(isolate())));
262 {
263 AllowExternalCallThatCantCauseGC scope(masm());
264 __ CallCFunction(ExternalReference::new_deoptimizer_function(isolate()), 6);
265 }
266
267 __ pop(edi);
268
269 // Preserve deoptimizer object in register eax and get the input
270 // frame descriptor pointer.
271 __ mov(ebx, Operand(eax, Deoptimizer::input_offset()));
272
273 // Fill in the input registers.
274 for (int i = kNumberOfRegisters - 1; i >= 0; i--) {
275 int offset = (i * kPointerSize) + FrameDescription::registers_offset();
276 __ pop(Operand(ebx, offset));
277 }
278
279 int double_regs_offset = FrameDescription::double_registers_offset();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000280 const RegisterConfiguration* config =
281 RegisterConfiguration::ArchDefault(RegisterConfiguration::CRANKSHAFT);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000282 // Fill in the double input registers.
283 for (int i = 0; i < X87Register::kMaxNumAllocatableRegisters; ++i) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000284 int code = config->GetAllocatableDoubleCode(i);
285 int dst_offset = code * kDoubleSize + double_regs_offset;
286 int src_offset = code * kDoubleSize;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000287 __ fld_d(Operand(esp, src_offset));
288 __ fstp_d(Operand(ebx, dst_offset));
289 }
290
291 // Clear FPU all exceptions.
292 // TODO(ulan): Find out why the TOP register is not zero here in some cases,
293 // and check that the generated code never deoptimizes with unbalanced stack.
294 __ fnclex();
295
296 // Remove the bailout id, return address and the double registers.
297 __ add(esp, Immediate(kDoubleRegsSize + 2 * kPointerSize));
298
299 // Compute a pointer to the unwinding limit in register ecx; that is
300 // the first stack slot not part of the input frame.
301 __ mov(ecx, Operand(ebx, FrameDescription::frame_size_offset()));
302 __ add(ecx, esp);
303
304 // Unwind the stack down to - but not including - the unwinding
305 // limit and copy the contents of the activation frame to the input
306 // frame description.
307 __ lea(edx, Operand(ebx, FrameDescription::frame_content_offset()));
308 Label pop_loop_header;
309 __ jmp(&pop_loop_header);
310 Label pop_loop;
311 __ bind(&pop_loop);
312 __ pop(Operand(edx, 0));
313 __ add(edx, Immediate(sizeof(uint32_t)));
314 __ bind(&pop_loop_header);
315 __ cmp(ecx, esp);
316 __ j(not_equal, &pop_loop);
317
318 // Compute the output frame in the deoptimizer.
319 __ push(edi);
320 __ push(eax);
321 __ PrepareCallCFunction(1, ebx);
322 __ mov(Operand(esp, 0 * kPointerSize), eax);
323 {
324 AllowExternalCallThatCantCauseGC scope(masm());
325 __ CallCFunction(
326 ExternalReference::compute_output_frames_function(isolate()), 1);
327 }
328 __ pop(eax);
329 __ pop(edi);
Ben Murdochda12d292016-06-02 14:46:10 +0100330 __ mov(esp, Operand(eax, Deoptimizer::caller_frame_top_offset()));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000331
Ben Murdochda12d292016-06-02 14:46:10 +0100332 // Replace the current (input) frame with the output frames.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000333 Label outer_push_loop, inner_push_loop,
334 outer_loop_header, inner_loop_header;
335 // Outer loop state: eax = current FrameDescription**, edx = one past the
336 // last FrameDescription**.
337 __ mov(edx, Operand(eax, Deoptimizer::output_count_offset()));
338 __ mov(eax, Operand(eax, Deoptimizer::output_offset()));
339 __ lea(edx, Operand(eax, edx, times_4, 0));
340 __ jmp(&outer_loop_header);
341 __ bind(&outer_push_loop);
342 // Inner loop state: ebx = current FrameDescription*, ecx = loop index.
343 __ mov(ebx, Operand(eax, 0));
344 __ mov(ecx, Operand(ebx, FrameDescription::frame_size_offset()));
345 __ jmp(&inner_loop_header);
346 __ bind(&inner_push_loop);
347 __ sub(ecx, Immediate(sizeof(uint32_t)));
348 __ push(Operand(ebx, ecx, times_1, FrameDescription::frame_content_offset()));
349 __ bind(&inner_loop_header);
350 __ test(ecx, ecx);
351 __ j(not_zero, &inner_push_loop);
352 __ add(eax, Immediate(kPointerSize));
353 __ bind(&outer_loop_header);
354 __ cmp(eax, edx);
355 __ j(below, &outer_push_loop);
356
357
358 // In case of a failed STUB, we have to restore the x87 stack.
359 // x87 stack layout is in edi.
360 Label loop2, done2;
361 // Get the x87 stack depth, the first 3 bits.
362 __ mov(ecx, edi);
363 __ and_(ecx, 0x7);
364 __ j(zero, &done2, Label::kNear);
365
366 __ lea(ecx, Operand(ecx, ecx, times_2, 0));
367 __ bind(&loop2);
368 __ mov(eax, edi);
369 __ shr_cl(eax);
370 __ and_(eax, 0x7);
371 __ fld_d(Operand(ebx, eax, times_8, double_regs_offset));
372 __ sub(ecx, Immediate(0x3));
373 __ j(not_zero, &loop2, Label::kNear);
374 __ bind(&done2);
375
376 // Push state, pc, and continuation from the last output frame.
377 __ push(Operand(ebx, FrameDescription::state_offset()));
378 __ push(Operand(ebx, FrameDescription::pc_offset()));
379 __ push(Operand(ebx, FrameDescription::continuation_offset()));
380
381
382 // Push the registers from the last output frame.
383 for (int i = 0; i < kNumberOfRegisters; i++) {
384 int offset = (i * kPointerSize) + FrameDescription::registers_offset();
385 __ push(Operand(ebx, offset));
386 }
387
388 // Restore the registers from the stack.
389 __ popad();
390
391 // Return to the continuation point.
392 __ ret(0);
393}
394
395
396void Deoptimizer::TableEntryGenerator::GeneratePrologue() {
397 // Create a sequence of deoptimization entries.
398 Label done;
399 for (int i = 0; i < count(); i++) {
400 int start = masm()->pc_offset();
401 USE(start);
402 __ push_imm32(i);
403 __ jmp(&done);
404 DCHECK(masm()->pc_offset() - start == table_entry_size_);
405 }
406 __ bind(&done);
407}
408
409
410void FrameDescription::SetCallerPc(unsigned offset, intptr_t value) {
411 SetFrameSlot(offset, value);
412}
413
414
415void FrameDescription::SetCallerFp(unsigned offset, intptr_t value) {
416 SetFrameSlot(offset, value);
417}
418
419
420void FrameDescription::SetCallerConstantPool(unsigned offset, intptr_t value) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000421 // No embedded constant pool support.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000422 UNREACHABLE();
423}
424
425
426#undef __
427
428
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000429} // namespace internal
430} // namespace v8
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000431
432#endif // V8_TARGET_ARCH_X87