blob: ec610f02819aa7472e98a191b3a36de6513b4bb7 [file] [log] [blame]
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001// Copyright 2011 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#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"
10
11namespace v8 {
12namespace internal {
13
14
15int Deoptimizer::patch_size() {
16 const int kCallInstructionSizeInWords = 6;
17 return kCallInstructionSizeInWords * Assembler::kInstrSize;
18}
19
20
Emily Bernierd0a1eb72015-03-24 16:35:39 -040021void Deoptimizer::EnsureRelocSpaceForLazyDeoptimization(Handle<Code> code) {
22 // Empty because there is no need for relocation information for the code
23 // patching in Deoptimizer::PatchCodeForDeoptimization below.
24}
25
26
Ben Murdochb8a8cc12014-11-26 15:28:44 +000027void Deoptimizer::PatchCodeForDeoptimization(Isolate* isolate, Code* code) {
28 Address code_start_address = code->instruction_start();
29 // Invalidate the relocation information, as it will become invalid by the
30 // code patching below, and is not needed any more.
31 code->InvalidateRelocation();
32
33 if (FLAG_zap_code_space) {
34 // Fail hard and early if we enter this code object again.
35 byte* pointer = code->FindCodeAgeSequence();
36 if (pointer != NULL) {
37 pointer += kNoCodeAgeSequenceLength;
38 } else {
39 pointer = code->instruction_start();
40 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000041 CodePatcher patcher(isolate, pointer, 1);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000042 patcher.masm()->break_(0xCC);
43
44 DeoptimizationInputData* data =
45 DeoptimizationInputData::cast(code->deoptimization_data());
46 int osr_offset = data->OsrPcOffset()->value();
47 if (osr_offset > 0) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000048 CodePatcher osr_patcher(isolate, code->instruction_start() + osr_offset,
49 1);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000050 osr_patcher.masm()->break_(0xCC);
51 }
52 }
53
54 DeoptimizationInputData* deopt_data =
55 DeoptimizationInputData::cast(code->deoptimization_data());
56#ifdef DEBUG
57 Address prev_call_address = NULL;
58#endif
59 // For each LLazyBailout instruction insert a call to the corresponding
60 // deoptimization entry.
61 for (int i = 0; i < deopt_data->DeoptCount(); i++) {
62 if (deopt_data->Pc(i)->value() == -1) continue;
63 Address call_address = code_start_address + deopt_data->Pc(i)->value();
64 Address deopt_entry = GetDeoptimizationEntry(isolate, i, LAZY);
65 int call_size_in_bytes = MacroAssembler::CallSize(deopt_entry,
66 RelocInfo::NONE32);
67 int call_size_in_words = call_size_in_bytes / Assembler::kInstrSize;
68 DCHECK(call_size_in_bytes % Assembler::kInstrSize == 0);
69 DCHECK(call_size_in_bytes <= patch_size());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000070 CodePatcher patcher(isolate, call_address, call_size_in_words);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000071 patcher.masm()->Call(deopt_entry, RelocInfo::NONE32);
72 DCHECK(prev_call_address == NULL ||
73 call_address >= prev_call_address + patch_size());
74 DCHECK(call_address + patch_size() <= code->instruction_end());
75
76#ifdef DEBUG
77 prev_call_address = call_address;
78#endif
79 }
80}
81
82
Ben Murdochb8a8cc12014-11-26 15:28:44 +000083void Deoptimizer::SetPlatformCompiledStubRegisters(
84 FrameDescription* output_frame, CodeStubDescriptor* descriptor) {
85 ApiFunction function(descriptor->deoptimization_handler());
86 ExternalReference xref(&function, ExternalReference::BUILTIN_CALL, isolate_);
87 intptr_t handler = reinterpret_cast<intptr_t>(xref.address());
88 int params = descriptor->GetHandlerParameterCount();
Emily Bernierd0a1eb72015-03-24 16:35:39 -040089 output_frame->SetRegister(a0.code(), params);
90 output_frame->SetRegister(a1.code(), handler);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000091}
92
93
94void Deoptimizer::CopyDoubleRegisters(FrameDescription* output_frame) {
95 for (int i = 0; i < DoubleRegister::kMaxNumRegisters; ++i) {
96 double double_value = input_->GetDoubleRegister(i);
97 output_frame->SetDoubleRegister(i, double_value);
98 }
99}
100
Ben Murdoch097c5b22016-05-18 11:27:45 +0100101bool Deoptimizer::HasAlignmentPadding(SharedFunctionInfo* shared) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000102 // There is no dynamic alignment padding on MIPS in the input frame.
103 return false;
104}
105
106
107#define __ masm()->
108
109
110// This code tries to be close to ia32 code so that any changes can be
111// easily ported.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000112void Deoptimizer::TableEntryGenerator::Generate() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000113 GeneratePrologue();
114
115 // Unlike on ARM we don't save all the registers, just the useful ones.
116 // For the rest, there are gaps on the stack, so the offsets remain the same.
117 const int kNumberOfRegisters = Register::kNumRegisters;
118
119 RegList restored_regs = kJSCallerSaved | kCalleeSaved;
120 RegList saved_regs = restored_regs | sp.bit() | ra.bit();
121
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000122 const int kDoubleRegsSize = kDoubleSize * DoubleRegister::kMaxNumRegisters;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000123
124 // Save all FPU registers before messing with them.
125 __ Dsubu(sp, sp, Operand(kDoubleRegsSize));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000126 const RegisterConfiguration* config =
127 RegisterConfiguration::ArchDefault(RegisterConfiguration::CRANKSHAFT);
128 for (int i = 0; i < config->num_allocatable_double_registers(); ++i) {
129 int code = config->GetAllocatableDoubleCode(i);
130 const DoubleRegister fpu_reg = DoubleRegister::from_code(code);
131 int offset = code * kDoubleSize;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000132 __ sdc1(fpu_reg, MemOperand(sp, offset));
133 }
134
135 // Push saved_regs (needed to populate FrameDescription::registers_).
136 // Leave gaps for other registers.
137 __ Dsubu(sp, sp, kNumberOfRegisters * kPointerSize);
138 for (int16_t i = kNumberOfRegisters - 1; i >= 0; i--) {
139 if ((saved_regs & (1 << i)) != 0) {
140 __ sd(ToRegister(i), MemOperand(sp, kPointerSize * i));
141 }
142 }
143
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000144 __ li(a2, Operand(ExternalReference(Isolate::kCEntryFPAddress, isolate())));
145 __ sd(fp, MemOperand(a2));
146
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000147 const int kSavedRegistersAreaSize =
148 (kNumberOfRegisters * kPointerSize) + kDoubleRegsSize;
149
150 // Get the bailout id from the stack.
151 __ ld(a2, MemOperand(sp, kSavedRegistersAreaSize));
152
153 // Get the address of the location in the code object (a3) (return
154 // address for lazy deoptimization) and compute the fp-to-sp delta in
155 // register a4.
156 __ mov(a3, ra);
157 // Correct one word for bailout id.
158 __ Daddu(a4, sp, Operand(kSavedRegistersAreaSize + (1 * kPointerSize)));
159
160 __ Dsubu(a4, fp, a4);
161
162 // Allocate a new deoptimizer object.
163 __ PrepareCallCFunction(6, a5);
164 // Pass six arguments, according to O32 or n64 ABI. a0..a3 are same for both.
165 __ li(a1, Operand(type())); // bailout type,
166 __ ld(a0, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
167 // a2: bailout id already loaded.
168 // a3: code address or 0 already loaded.
Ben Murdoch097c5b22016-05-18 11:27:45 +0100169 // a4: already has fp-to-sp delta.
170 __ li(a5, Operand(ExternalReference::isolate_address(isolate())));
171
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000172 // Call Deoptimizer::New().
173 {
174 AllowExternalCallThatCantCauseGC scope(masm());
175 __ CallCFunction(ExternalReference::new_deoptimizer_function(isolate()), 6);
176 }
177
178 // Preserve "deoptimizer" object in register v0 and get the input
179 // frame descriptor pointer to a1 (deoptimizer->input_);
180 // Move deopt-obj to a0 for call to Deoptimizer::ComputeOutputFrames() below.
181 __ mov(a0, v0);
182 __ ld(a1, MemOperand(v0, Deoptimizer::input_offset()));
183
184 // Copy core registers into FrameDescription::registers_[kNumRegisters].
185 DCHECK(Register::kNumRegisters == kNumberOfRegisters);
186 for (int i = 0; i < kNumberOfRegisters; i++) {
187 int offset = (i * kPointerSize) + FrameDescription::registers_offset();
188 if ((saved_regs & (1 << i)) != 0) {
189 __ ld(a2, MemOperand(sp, i * kPointerSize));
190 __ sd(a2, MemOperand(a1, offset));
191 } else if (FLAG_debug_code) {
192 __ li(a2, kDebugZapValue);
193 __ sd(a2, MemOperand(a1, offset));
194 }
195 }
196
197 int double_regs_offset = FrameDescription::double_registers_offset();
198 // Copy FPU registers to
199 // double_registers_[DoubleRegister::kNumAllocatableRegisters]
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000200 for (int i = 0; i < config->num_allocatable_double_registers(); ++i) {
201 int code = config->GetAllocatableDoubleCode(i);
202 int dst_offset = code * kDoubleSize + double_regs_offset;
203 int src_offset = code * kDoubleSize + kNumberOfRegisters * kPointerSize;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000204 __ ldc1(f0, MemOperand(sp, src_offset));
205 __ sdc1(f0, MemOperand(a1, dst_offset));
206 }
207
208 // Remove the bailout id and the saved registers from the stack.
209 __ Daddu(sp, sp, Operand(kSavedRegistersAreaSize + (1 * kPointerSize)));
210
211 // Compute a pointer to the unwinding limit in register a2; that is
212 // the first stack slot not part of the input frame.
213 __ ld(a2, MemOperand(a1, FrameDescription::frame_size_offset()));
214 __ Daddu(a2, a2, sp);
215
216 // Unwind the stack down to - but not including - the unwinding
217 // limit and copy the contents of the activation frame to the input
218 // frame description.
219 __ Daddu(a3, a1, Operand(FrameDescription::frame_content_offset()));
220 Label pop_loop;
221 Label pop_loop_header;
222 __ BranchShort(&pop_loop_header);
223 __ bind(&pop_loop);
224 __ pop(a4);
225 __ sd(a4, MemOperand(a3, 0));
226 __ daddiu(a3, a3, sizeof(uint64_t));
227 __ bind(&pop_loop_header);
228 __ BranchShort(&pop_loop, ne, a2, Operand(sp));
229 // Compute the output frame in the deoptimizer.
230 __ push(a0); // Preserve deoptimizer object across call.
231 // a0: deoptimizer object; a1: scratch.
232 __ PrepareCallCFunction(1, a1);
233 // Call Deoptimizer::ComputeOutputFrames().
234 {
235 AllowExternalCallThatCantCauseGC scope(masm());
236 __ CallCFunction(
237 ExternalReference::compute_output_frames_function(isolate()), 1);
238 }
239 __ pop(a0); // Restore deoptimizer object (class Deoptimizer).
240
241 // Replace the current (input) frame with the output frames.
242 Label outer_push_loop, inner_push_loop,
243 outer_loop_header, inner_loop_header;
244 // Outer loop state: a4 = current "FrameDescription** output_",
245 // a1 = one past the last FrameDescription**.
246 __ lw(a1, MemOperand(a0, Deoptimizer::output_count_offset()));
247 __ ld(a4, MemOperand(a0, Deoptimizer::output_offset())); // a4 is output_.
Ben Murdoch097c5b22016-05-18 11:27:45 +0100248 __ Dlsa(a1, a4, a1, kPointerSizeLog2);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000249 __ BranchShort(&outer_loop_header);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000250 __ bind(&outer_push_loop);
251 // Inner loop state: a2 = current FrameDescription*, a3 = loop index.
252 __ ld(a2, MemOperand(a4, 0)); // output_[ix]
253 __ ld(a3, MemOperand(a2, FrameDescription::frame_size_offset()));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000254 __ BranchShort(&inner_loop_header);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000255 __ bind(&inner_push_loop);
256 __ Dsubu(a3, a3, Operand(sizeof(uint64_t)));
257 __ Daddu(a6, a2, Operand(a3));
258 __ ld(a7, MemOperand(a6, FrameDescription::frame_content_offset()));
259 __ push(a7);
260 __ bind(&inner_loop_header);
261 __ BranchShort(&inner_push_loop, ne, a3, Operand(zero_reg));
262
263 __ Daddu(a4, a4, Operand(kPointerSize));
264 __ bind(&outer_loop_header);
265 __ BranchShort(&outer_push_loop, lt, a4, Operand(a1));
266
267 __ ld(a1, MemOperand(a0, Deoptimizer::input_offset()));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000268 for (int i = 0; i < config->num_allocatable_double_registers(); ++i) {
269 int code = config->GetAllocatableDoubleCode(i);
270 const DoubleRegister fpu_reg = DoubleRegister::from_code(code);
271 int src_offset = code * kDoubleSize + double_regs_offset;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000272 __ ldc1(fpu_reg, MemOperand(a1, src_offset));
273 }
274
275 // Push state, pc, and continuation from the last output frame.
276 __ ld(a6, MemOperand(a2, FrameDescription::state_offset()));
277 __ push(a6);
278
279 __ ld(a6, MemOperand(a2, FrameDescription::pc_offset()));
280 __ push(a6);
281 __ ld(a6, MemOperand(a2, FrameDescription::continuation_offset()));
282 __ push(a6);
283
284
285 // Technically restoring 'at' should work unless zero_reg is also restored
286 // but it's safer to check for this.
287 DCHECK(!(at.bit() & restored_regs));
288 // Restore the registers from the last output frame.
289 __ mov(at, a2);
290 for (int i = kNumberOfRegisters - 1; i >= 0; i--) {
291 int offset = (i * kPointerSize) + FrameDescription::registers_offset();
292 if ((restored_regs & (1 << i)) != 0) {
293 __ ld(ToRegister(i), MemOperand(at, offset));
294 }
295 }
296
297 __ InitializeRootRegister();
298
299 __ pop(at); // Get continuation, leave pc on stack.
300 __ pop(ra);
301 __ Jump(at);
302 __ stop("Unreachable.");
303}
304
305
306// Maximum size of a table entry generated below.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000307const int Deoptimizer::table_entry_size_ = 2 * Assembler::kInstrSize;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000308
309void Deoptimizer::TableEntryGenerator::GeneratePrologue() {
310 Assembler::BlockTrampolinePoolScope block_trampoline_pool(masm());
311
312 // Create a sequence of deoptimization entries.
313 // Note that registers are still live when jumping to an entry.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000314 Label table_start, done, done_special, trampoline_jump;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000315 __ bind(&table_start);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000316 int kMaxEntriesBranchReach =
317 (1 << (kImm16Bits - 2)) / (table_entry_size_ / Assembler::kInstrSize);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000318
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000319 if (count() <= kMaxEntriesBranchReach) {
320 // Common case.
321 for (int i = 0; i < count(); i++) {
322 Label start;
323 __ bind(&start);
324 DCHECK(is_int16(i));
325 __ BranchShort(USE_DELAY_SLOT, &done); // Expose delay slot.
326 __ li(at, i); // In the delay slot.
327
328 DCHECK_EQ(table_entry_size_, masm()->SizeOfCodeGeneratedSince(&start));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000329 }
330
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000331 DCHECK_EQ(masm()->SizeOfCodeGeneratedSince(&table_start),
332 count() * table_entry_size_);
333 __ bind(&done);
334 __ Push(at);
335 } else {
336 // Uncommon case, the branch cannot reach.
337 // Create mini trampoline and adjust id constants to get proper value at
338 // the end of table.
339 for (int i = kMaxEntriesBranchReach; i > 1; i--) {
340 Label start;
341 __ bind(&start);
342 DCHECK(is_int16(i));
343 __ BranchShort(USE_DELAY_SLOT, &trampoline_jump); // Expose delay slot.
344 __ li(at, -i); // In the delay slot.
345 DCHECK_EQ(table_entry_size_, masm()->SizeOfCodeGeneratedSince(&start));
346 }
347 // Entry with id == kMaxEntriesBranchReach - 1.
348 __ bind(&trampoline_jump);
349 __ BranchShort(USE_DELAY_SLOT, &done_special);
350 __ li(at, -1);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000351
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000352 for (int i = kMaxEntriesBranchReach; i < count(); i++) {
353 Label start;
354 __ bind(&start);
355 DCHECK(is_int16(i));
356 __ Branch(USE_DELAY_SLOT, &done); // Expose delay slot.
357 __ li(at, i); // In the delay slot.
358 }
359
360 DCHECK_EQ(masm()->SizeOfCodeGeneratedSince(&table_start),
361 count() * table_entry_size_);
362 __ bind(&done_special);
363 __ daddiu(at, at, kMaxEntriesBranchReach);
364 __ bind(&done);
365 __ Push(at);
366 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000367}
368
369
370void FrameDescription::SetCallerPc(unsigned offset, intptr_t value) {
371 SetFrameSlot(offset, value);
372}
373
374
375void FrameDescription::SetCallerFp(unsigned offset, intptr_t value) {
376 SetFrameSlot(offset, value);
377}
378
379
380void FrameDescription::SetCallerConstantPool(unsigned offset, intptr_t value) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000381 // No embedded constant pool support.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000382 UNREACHABLE();
383}
384
385
386#undef __
387
388
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000389} // namespace internal
390} // namespace v8