blob: 2785b755d5bc78fb45d826c241a8cf7058fe9422 [file] [log] [blame]
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001// Copyright 2012 the V8 project authors. All rights reserved.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
Ben Murdochb0fe1622011-05-05 13:52:32 +01004
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005#include "src/codegen.h"
6#include "src/deoptimizer.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007#include "src/full-codegen/full-codegen.h"
8#include "src/register-configuration.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009#include "src/safepoint-table.h"
Ben Murdochb0fe1622011-05-05 13:52:32 +010010
11namespace v8 {
12namespace internal {
13
Ben Murdochb8a8cc12014-11-26 15:28:44 +000014const int Deoptimizer::table_entry_size_ = 8;
Ben Murdochb0fe1622011-05-05 13:52:32 +010015
Steve Block1e0659c2011-05-24 12:43:12 +010016
17int Deoptimizer::patch_size() {
18 const int kCallInstructionSizeInWords = 3;
19 return kCallInstructionSizeInWords * Assembler::kInstrSize;
20}
21
22
Emily Bernierd0a1eb72015-03-24 16:35:39 -040023void Deoptimizer::EnsureRelocSpaceForLazyDeoptimization(Handle<Code> code) {
24 // Empty because there is no need for relocation information for the code
25 // patching in Deoptimizer::PatchCodeForDeoptimization below.
26}
27
28
Ben Murdochb8a8cc12014-11-26 15:28:44 +000029void Deoptimizer::PatchCodeForDeoptimization(Isolate* isolate, Code* code) {
Ben Murdoch2b4ba112012-01-20 14:57:15 +000030 Address code_start_address = code->instruction_start();
Ben Murdochb0fe1622011-05-05 13:52:32 +010031 // Invalidate the relocation information, as it will become invalid by the
32 // code patching below, and is not needed any more.
33 code->InvalidateRelocation();
34
Ben Murdochb8a8cc12014-11-26 15:28:44 +000035 if (FLAG_zap_code_space) {
36 // Fail hard and early if we enter this code object again.
37 byte* pointer = code->FindCodeAgeSequence();
38 if (pointer != NULL) {
39 pointer += kNoCodeAgeSequenceLength;
40 } else {
41 pointer = code->instruction_start();
42 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000043 CodePatcher patcher(isolate, pointer, 1);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000044 patcher.masm()->bkpt(0);
45
46 DeoptimizationInputData* data =
47 DeoptimizationInputData::cast(code->deoptimization_data());
48 int osr_offset = data->OsrPcOffset()->value();
49 if (osr_offset > 0) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000050 CodePatcher osr_patcher(isolate, code->instruction_start() + osr_offset,
51 1);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000052 osr_patcher.masm()->bkpt(0);
53 }
54 }
55
Ben Murdoch2b4ba112012-01-20 14:57:15 +000056 DeoptimizationInputData* deopt_data =
57 DeoptimizationInputData::cast(code->deoptimization_data());
Ben Murdochb0fe1622011-05-05 13:52:32 +010058#ifdef DEBUG
Ben Murdoch2b4ba112012-01-20 14:57:15 +000059 Address prev_call_address = NULL;
Ben Murdochb0fe1622011-05-05 13:52:32 +010060#endif
Ben Murdochb8a8cc12014-11-26 15:28:44 +000061 // For each LLazyBailout instruction insert a call to the corresponding
62 // deoptimization entry.
Ben Murdoch2b4ba112012-01-20 14:57:15 +000063 for (int i = 0; i < deopt_data->DeoptCount(); i++) {
64 if (deopt_data->Pc(i)->value() == -1) continue;
65 Address call_address = code_start_address + deopt_data->Pc(i)->value();
Ben Murdochb8a8cc12014-11-26 15:28:44 +000066 Address deopt_entry = GetDeoptimizationEntry(isolate, i, LAZY);
67 // We need calls to have a predictable size in the unoptimized code, but
68 // this is optimized code, so we don't have to have a predictable size.
69 int call_size_in_bytes =
70 MacroAssembler::CallSizeNotPredictableCodeSize(isolate,
71 deopt_entry,
72 RelocInfo::NONE32);
Ben Murdoch2b4ba112012-01-20 14:57:15 +000073 int call_size_in_words = call_size_in_bytes / Assembler::kInstrSize;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000074 DCHECK(call_size_in_bytes % Assembler::kInstrSize == 0);
75 DCHECK(call_size_in_bytes <= patch_size());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000076 CodePatcher patcher(isolate, call_address, call_size_in_words);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000077 patcher.masm()->Call(deopt_entry, RelocInfo::NONE32);
78 DCHECK(prev_call_address == NULL ||
Ben Murdoch2b4ba112012-01-20 14:57:15 +000079 call_address >= prev_call_address + patch_size());
Ben Murdochb8a8cc12014-11-26 15:28:44 +000080 DCHECK(call_address + patch_size() <= code->instruction_end());
Ben Murdochb0fe1622011-05-05 13:52:32 +010081#ifdef DEBUG
Ben Murdoch2b4ba112012-01-20 14:57:15 +000082 prev_call_address = call_address;
Ben Murdochb0fe1622011-05-05 13:52:32 +010083#endif
Ben Murdoch2b4ba112012-01-20 14:57:15 +000084 }
Ben Murdochb0fe1622011-05-05 13:52:32 +010085}
86
87
Ben Murdochb8a8cc12014-11-26 15:28:44 +000088void Deoptimizer::SetPlatformCompiledStubRegisters(
89 FrameDescription* output_frame, CodeStubDescriptor* descriptor) {
90 ApiFunction function(descriptor->deoptimization_handler());
91 ExternalReference xref(&function, ExternalReference::BUILTIN_CALL, isolate_);
92 intptr_t handler = reinterpret_cast<intptr_t>(xref.address());
93 int params = descriptor->GetHandlerParameterCount();
94 output_frame->SetRegister(r0.code(), params);
95 output_frame->SetRegister(r1.code(), handler);
96}
97
98
99void Deoptimizer::CopyDoubleRegisters(FrameDescription* output_frame) {
100 for (int i = 0; i < DwVfpRegister::kMaxNumRegisters; ++i) {
101 double double_value = input_->GetDoubleRegister(i);
102 output_frame->SetDoubleRegister(i, double_value);
103 }
104}
105
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000106#define __ masm()->
Ben Murdochb0fe1622011-05-05 13:52:32 +0100107
108// This code tries to be close to ia32 code so that any changes can be
109// easily ported.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000110void Deoptimizer::TableEntryGenerator::Generate() {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100111 GeneratePrologue();
Steve Block44f0eee2011-05-26 01:26:41 +0100112
Ben Murdochb0fe1622011-05-05 13:52:32 +0100113 // Save all general purpose registers before messing with them.
114 const int kNumberOfRegisters = Register::kNumRegisters;
115
116 // Everything but pc, lr and ip which will be saved but not restored.
117 RegList restored_regs = kJSCallerSaved | kCalleeSaved | ip.bit();
118
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000119 const int kDoubleRegsSize = kDoubleSize * DwVfpRegister::kMaxNumRegisters;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100120
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000121 // Save all allocatable VFP registers before messing with them.
122 DCHECK(kDoubleRegZero.code() == 14);
123 DCHECK(kScratchDoubleReg.code() == 15);
124
125 // Check CPU flags for number of registers, setting the Z condition flag.
126 __ CheckFor32DRegs(ip);
127
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000128 // Push registers d0-d15, and possibly d16-d31, on the stack.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000129 // If d16-d31 are not pushed, decrease the stack pointer instead.
130 __ vstm(db_w, sp, d16, d31, ne);
131 __ sub(sp, sp, Operand(16 * kDoubleSize), LeaveCC, eq);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000132 __ vstm(db_w, sp, d0, d15);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100133
134 // Push all 16 registers (needed to populate FrameDescription::registers_).
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000135 // TODO(1588) Note that using pc with stm is deprecated, so we should perhaps
136 // handle this a bit differently.
Ben Murdochb0fe1622011-05-05 13:52:32 +0100137 __ stm(db_w, sp, restored_regs | sp.bit() | lr.bit() | pc.bit());
138
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000139 __ mov(ip, Operand(ExternalReference(Isolate::kCEntryFPAddress, isolate())));
140 __ str(fp, MemOperand(ip));
141
Ben Murdochb0fe1622011-05-05 13:52:32 +0100142 const int kSavedRegistersAreaSize =
143 (kNumberOfRegisters * kPointerSize) + kDoubleRegsSize;
144
145 // Get the bailout id from the stack.
146 __ ldr(r2, MemOperand(sp, kSavedRegistersAreaSize));
147
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000148 // Get the address of the location in the code object (r3) (return
Ben Murdochb0fe1622011-05-05 13:52:32 +0100149 // address for lazy deoptimization) and compute the fp-to-sp delta in
150 // register r4.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000151 __ mov(r3, lr);
152 // Correct one word for bailout id.
153 __ add(r4, sp, Operand(kSavedRegistersAreaSize + (1 * kPointerSize)));
Ben Murdochb0fe1622011-05-05 13:52:32 +0100154 __ sub(r4, fp, r4);
155
156 // Allocate a new deoptimizer object.
157 // Pass four arguments in r0 to r3 and fifth argument on stack.
Ben Murdoch8b112d22011-06-08 16:22:53 +0100158 __ PrepareCallCFunction(6, r5);
Ben Murdochda12d292016-06-02 14:46:10 +0100159 __ mov(r0, Operand(0));
160 Label context_check;
161 __ ldr(r1, MemOperand(fp, CommonFrameConstants::kContextOrFrameTypeOffset));
162 __ JumpIfSmi(r1, &context_check);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100163 __ ldr(r0, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
Ben Murdochda12d292016-06-02 14:46:10 +0100164 __ bind(&context_check);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100165 __ mov(r1, Operand(type())); // bailout type,
166 // r2: bailout id already loaded.
167 // r3: code address or 0 already loaded.
168 __ str(r4, MemOperand(sp, 0 * kPointerSize)); // Fp-to-sp delta.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000169 __ mov(r5, Operand(ExternalReference::isolate_address(isolate())));
Ben Murdoch8b112d22011-06-08 16:22:53 +0100170 __ str(r5, MemOperand(sp, 1 * kPointerSize)); // Isolate.
Ben Murdochb0fe1622011-05-05 13:52:32 +0100171 // Call Deoptimizer::New().
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100172 {
173 AllowExternalCallThatCantCauseGC scope(masm());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000174 __ CallCFunction(ExternalReference::new_deoptimizer_function(isolate()), 6);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100175 }
Ben Murdochb0fe1622011-05-05 13:52:32 +0100176
177 // Preserve "deoptimizer" object in register r0 and get the input
178 // frame descriptor pointer to r1 (deoptimizer->input_);
179 __ ldr(r1, MemOperand(r0, Deoptimizer::input_offset()));
180
Ben Murdochb0fe1622011-05-05 13:52:32 +0100181 // Copy core registers into FrameDescription::registers_[kNumRegisters].
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000182 DCHECK(Register::kNumRegisters == kNumberOfRegisters);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100183 for (int i = 0; i < kNumberOfRegisters; i++) {
Steve Block1e0659c2011-05-24 12:43:12 +0100184 int offset = (i * kPointerSize) + FrameDescription::registers_offset();
Ben Murdochb0fe1622011-05-05 13:52:32 +0100185 __ ldr(r2, MemOperand(sp, i * kPointerSize));
186 __ str(r2, MemOperand(r1, offset));
187 }
188
189 // Copy VFP registers to
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000190 // double_registers_[DoubleRegister::kMaxNumAllocatableRegisters]
Ben Murdochb0fe1622011-05-05 13:52:32 +0100191 int double_regs_offset = FrameDescription::double_registers_offset();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000192 const RegisterConfiguration* config =
193 RegisterConfiguration::ArchDefault(RegisterConfiguration::CRANKSHAFT);
194 for (int i = 0; i < config->num_allocatable_double_registers(); ++i) {
195 int code = config->GetAllocatableDoubleCode(i);
196 int dst_offset = code * kDoubleSize + double_regs_offset;
197 int src_offset = code * kDoubleSize + kNumberOfRegisters * kPointerSize;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100198 __ vldr(d0, sp, src_offset);
199 __ vstr(d0, r1, dst_offset);
200 }
201
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000202 // Remove the bailout id and the saved registers from the stack.
203 __ add(sp, sp, Operand(kSavedRegistersAreaSize + (1 * kPointerSize)));
Ben Murdochb0fe1622011-05-05 13:52:32 +0100204
205 // Compute a pointer to the unwinding limit in register r2; that is
206 // the first stack slot not part of the input frame.
207 __ ldr(r2, MemOperand(r1, FrameDescription::frame_size_offset()));
208 __ add(r2, r2, sp);
209
210 // Unwind the stack down to - but not including - the unwinding
211 // limit and copy the contents of the activation frame to the input
212 // frame description.
213 __ add(r3, r1, Operand(FrameDescription::frame_content_offset()));
214 Label pop_loop;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000215 Label pop_loop_header;
216 __ b(&pop_loop_header);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100217 __ bind(&pop_loop);
218 __ pop(r4);
219 __ str(r4, MemOperand(r3, 0));
220 __ add(r3, r3, Operand(sizeof(uint32_t)));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000221 __ bind(&pop_loop_header);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100222 __ cmp(r2, sp);
223 __ b(ne, &pop_loop);
224
225 // Compute the output frame in the deoptimizer.
226 __ push(r0); // Preserve deoptimizer object across call.
227 // r0: deoptimizer object; r1: scratch.
228 __ PrepareCallCFunction(1, r1);
229 // Call Deoptimizer::ComputeOutputFrames().
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100230 {
231 AllowExternalCallThatCantCauseGC scope(masm());
232 __ CallCFunction(
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000233 ExternalReference::compute_output_frames_function(isolate()), 1);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100234 }
Ben Murdochb0fe1622011-05-05 13:52:32 +0100235 __ pop(r0); // Restore deoptimizer object (class Deoptimizer).
236
Ben Murdochda12d292016-06-02 14:46:10 +0100237 __ ldr(sp, MemOperand(r0, Deoptimizer::caller_frame_top_offset()));
238
Ben Murdochb0fe1622011-05-05 13:52:32 +0100239 // Replace the current (input) frame with the output frames.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000240 Label outer_push_loop, inner_push_loop,
241 outer_loop_header, inner_loop_header;
242 // Outer loop state: r4 = current "FrameDescription** output_",
Ben Murdochb0fe1622011-05-05 13:52:32 +0100243 // r1 = one past the last FrameDescription**.
244 __ ldr(r1, MemOperand(r0, Deoptimizer::output_count_offset()));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000245 __ ldr(r4, MemOperand(r0, Deoptimizer::output_offset())); // r4 is output_.
246 __ add(r1, r4, Operand(r1, LSL, 2));
247 __ jmp(&outer_loop_header);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100248 __ bind(&outer_push_loop);
249 // Inner loop state: r2 = current FrameDescription*, r3 = loop index.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000250 __ ldr(r2, MemOperand(r4, 0)); // output_[ix]
Ben Murdochb0fe1622011-05-05 13:52:32 +0100251 __ ldr(r3, MemOperand(r2, FrameDescription::frame_size_offset()));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000252 __ jmp(&inner_loop_header);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100253 __ bind(&inner_push_loop);
254 __ sub(r3, r3, Operand(sizeof(uint32_t)));
Ben Murdochb0fe1622011-05-05 13:52:32 +0100255 __ add(r6, r2, Operand(r3));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000256 __ ldr(r6, MemOperand(r6, FrameDescription::frame_content_offset()));
257 __ push(r6);
258 __ bind(&inner_loop_header);
259 __ cmp(r3, Operand::Zero());
Ben Murdochb0fe1622011-05-05 13:52:32 +0100260 __ b(ne, &inner_push_loop); // test for gt?
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000261 __ add(r4, r4, Operand(kPointerSize));
262 __ bind(&outer_loop_header);
263 __ cmp(r4, r1);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100264 __ b(lt, &outer_push_loop);
265
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000266 // Check CPU flags for number of registers, setting the Z condition flag.
267 __ CheckFor32DRegs(ip);
268
269 __ ldr(r1, MemOperand(r0, Deoptimizer::input_offset()));
Ben Murdoch097c5b22016-05-18 11:27:45 +0100270 for (int i = 0; i < config->num_allocatable_double_registers(); ++i) {
271 int code = config->GetAllocatableDoubleCode(i);
272 DwVfpRegister reg = DwVfpRegister::from_code(code);
273 int src_offset = code * kDoubleSize + double_regs_offset;
274 __ vldr(reg, r1, src_offset);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100275 }
276
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000277 // Push state, pc, and continuation from the last output frame.
278 __ ldr(r6, MemOperand(r2, FrameDescription::state_offset()));
279 __ push(r6);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100280 __ ldr(r6, MemOperand(r2, FrameDescription::pc_offset()));
281 __ push(r6);
282 __ ldr(r6, MemOperand(r2, FrameDescription::continuation_offset()));
283 __ push(r6);
284
285 // Push the registers from the last output frame.
286 for (int i = kNumberOfRegisters - 1; i >= 0; i--) {
Steve Block1e0659c2011-05-24 12:43:12 +0100287 int offset = (i * kPointerSize) + FrameDescription::registers_offset();
Ben Murdochb0fe1622011-05-05 13:52:32 +0100288 __ ldr(r6, MemOperand(r2, offset));
289 __ push(r6);
290 }
291
292 // Restore the registers from the stack.
293 __ ldm(ia_w, sp, restored_regs); // all but pc registers.
294 __ pop(ip); // remove sp
295 __ pop(ip); // remove lr
296
Ben Murdochc7cc0282012-03-05 14:35:55 +0000297 __ InitializeRootRegister();
Ben Murdochb0fe1622011-05-05 13:52:32 +0100298
299 __ pop(ip); // remove pc
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000300 __ pop(ip); // get continuation, leave pc on stack
Ben Murdochb0fe1622011-05-05 13:52:32 +0100301 __ pop(lr);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000302 __ Jump(ip);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100303 __ stop("Unreachable.");
304}
305
306
307void Deoptimizer::TableEntryGenerator::GeneratePrologue() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000308 // Create a sequence of deoptimization entries.
309 // Note that registers are still live when jumping to an entry.
Ben Murdochb0fe1622011-05-05 13:52:32 +0100310 Label done;
311 for (int i = 0; i < count(); i++) {
312 int start = masm()->pc_offset();
313 USE(start);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100314 __ mov(ip, Operand(i));
Ben Murdochb0fe1622011-05-05 13:52:32 +0100315 __ b(&done);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000316 DCHECK(masm()->pc_offset() - start == table_entry_size_);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100317 }
318 __ bind(&done);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000319 __ push(ip);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100320}
321
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000322
323void FrameDescription::SetCallerPc(unsigned offset, intptr_t value) {
324 SetFrameSlot(offset, value);
325}
326
327
328void FrameDescription::SetCallerFp(unsigned offset, intptr_t value) {
329 SetFrameSlot(offset, value);
330}
331
332
333void FrameDescription::SetCallerConstantPool(unsigned offset, intptr_t value) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000334 DCHECK(FLAG_enable_embedded_constant_pool);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000335 SetFrameSlot(offset, value);
336}
337
338
Ben Murdochb0fe1622011-05-05 13:52:32 +0100339#undef __
340
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000341} // namespace internal
342} // namespace v8