blob: dd9832d56c4ced7b971cba8357a3db9b902ffc4c [file] [log] [blame]
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001
Steve Block44f0eee2011-05-26 01:26:41 +01002// Copyright 2011 the V8 project authors. All rights reserved.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
Steve Block44f0eee2011-05-26 01:26:41 +01005
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006#include "src/v8.h"
Steve Block44f0eee2011-05-26 01:26:41 +01007
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008#include "src/codegen.h"
9#include "src/deoptimizer.h"
10#include "src/full-codegen.h"
11#include "src/safepoint-table.h"
Steve Block44f0eee2011-05-26 01:26:41 +010012
Steve Block44f0eee2011-05-26 01:26:41 +010013namespace v8 {
14namespace internal {
15
16
Steve Block44f0eee2011-05-26 01:26:41 +010017int Deoptimizer::patch_size() {
Ben Murdoch3ef787d2012-04-12 10:51:47 +010018 const int kCallInstructionSizeInWords = 4;
Steve Block44f0eee2011-05-26 01:26:41 +010019 return kCallInstructionSizeInWords * Assembler::kInstrSize;
20}
21
22
Ben Murdochb8a8cc12014-11-26 15:28:44 +000023void Deoptimizer::PatchCodeForDeoptimization(Isolate* isolate, Code* code) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +010024 Address code_start_address = code->instruction_start();
Ben Murdoch3ef787d2012-04-12 10:51:47 +010025 // Invalidate the relocation information, as it will become invalid by the
26 // code patching below, and is not needed any more.
27 code->InvalidateRelocation();
28
Ben Murdochb8a8cc12014-11-26 15:28:44 +000029 if (FLAG_zap_code_space) {
30 // Fail hard and early if we enter this code object again.
31 byte* pointer = code->FindCodeAgeSequence();
32 if (pointer != NULL) {
33 pointer += kNoCodeAgeSequenceLength;
34 } else {
35 pointer = code->instruction_start();
36 }
37 CodePatcher patcher(pointer, 1);
38 patcher.masm()->break_(0xCC);
39
40 DeoptimizationInputData* data =
41 DeoptimizationInputData::cast(code->deoptimization_data());
42 int osr_offset = data->OsrPcOffset()->value();
43 if (osr_offset > 0) {
44 CodePatcher osr_patcher(code->instruction_start() + osr_offset, 1);
45 osr_patcher.masm()->break_(0xCC);
46 }
47 }
48
Ben Murdoch3ef787d2012-04-12 10:51:47 +010049 DeoptimizationInputData* deopt_data =
50 DeoptimizationInputData::cast(code->deoptimization_data());
51#ifdef DEBUG
52 Address prev_call_address = NULL;
53#endif
Ben Murdochb8a8cc12014-11-26 15:28:44 +000054 // For each LLazyBailout instruction insert a call to the corresponding
55 // deoptimization entry.
Ben Murdoch3ef787d2012-04-12 10:51:47 +010056 for (int i = 0; i < deopt_data->DeoptCount(); i++) {
57 if (deopt_data->Pc(i)->value() == -1) continue;
58 Address call_address = code_start_address + deopt_data->Pc(i)->value();
Ben Murdochb8a8cc12014-11-26 15:28:44 +000059 Address deopt_entry = GetDeoptimizationEntry(isolate, i, LAZY);
Ben Murdoch3ef787d2012-04-12 10:51:47 +010060 int call_size_in_bytes = MacroAssembler::CallSize(deopt_entry,
Ben Murdochb8a8cc12014-11-26 15:28:44 +000061 RelocInfo::NONE32);
Ben Murdoch3ef787d2012-04-12 10:51:47 +010062 int call_size_in_words = call_size_in_bytes / Assembler::kInstrSize;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000063 DCHECK(call_size_in_bytes % Assembler::kInstrSize == 0);
64 DCHECK(call_size_in_bytes <= patch_size());
Ben Murdoch3ef787d2012-04-12 10:51:47 +010065 CodePatcher patcher(call_address, call_size_in_words);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000066 patcher.masm()->Call(deopt_entry, RelocInfo::NONE32);
67 DCHECK(prev_call_address == NULL ||
Ben Murdoch3ef787d2012-04-12 10:51:47 +010068 call_address >= prev_call_address + patch_size());
Ben Murdochb8a8cc12014-11-26 15:28:44 +000069 DCHECK(call_address + patch_size() <= code->instruction_end());
Ben Murdoch3ef787d2012-04-12 10:51:47 +010070
71#ifdef DEBUG
72 prev_call_address = call_address;
73#endif
74 }
Steve Block44f0eee2011-05-26 01:26:41 +010075}
76
77
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000078void Deoptimizer::FillInputFrame(Address tos, JavaScriptFrame* frame) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +010079 // Set the register values. The values are not important as there are no
80 // callee saved registers in JavaScript frames, so all registers are
81 // spilled. Registers fp and sp are set to the correct values though.
82
83 for (int i = 0; i < Register::kNumRegisters; i++) {
84 input_->SetRegister(i, i * 4);
85 }
86 input_->SetRegister(sp.code(), reinterpret_cast<intptr_t>(frame->sp()));
87 input_->SetRegister(fp.code(), reinterpret_cast<intptr_t>(frame->fp()));
Ben Murdochb8a8cc12014-11-26 15:28:44 +000088 for (int i = 0; i < DoubleRegister::NumAllocatableRegisters(); i++) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +010089 input_->SetDoubleRegister(i, 0.0);
90 }
91
92 // Fill the frame content from the actual data on the frame.
93 for (unsigned i = 0; i < input_->GetFrameSize(); i += kPointerSize) {
94 input_->SetFrameSlot(i, Memory::uint32_at(tos + i));
95 }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000096}
97
98
Ben Murdochb8a8cc12014-11-26 15:28:44 +000099void Deoptimizer::SetPlatformCompiledStubRegisters(
100 FrameDescription* output_frame, CodeStubDescriptor* descriptor) {
101 ApiFunction function(descriptor->deoptimization_handler());
102 ExternalReference xref(&function, ExternalReference::BUILTIN_CALL, isolate_);
103 intptr_t handler = reinterpret_cast<intptr_t>(xref.address());
104 int params = descriptor->GetHandlerParameterCount();
105 output_frame->SetRegister(s0.code(), params);
106 output_frame->SetRegister(s1.code(), (params - 1) * kPointerSize);
107 output_frame->SetRegister(s2.code(), handler);
108}
109
110
111void Deoptimizer::CopyDoubleRegisters(FrameDescription* output_frame) {
112 for (int i = 0; i < DoubleRegister::kMaxNumRegisters; ++i) {
113 double double_value = input_->GetDoubleRegister(i);
114 output_frame->SetDoubleRegister(i, double_value);
115 }
116}
117
118
119bool Deoptimizer::HasAlignmentPadding(JSFunction* function) {
120 // There is no dynamic alignment padding on MIPS in the input frame.
121 return false;
122}
123
124
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100125#define __ masm()->
126
127
128// This code tries to be close to ia32 code so that any changes can be
129// easily ported.
Steve Block44f0eee2011-05-26 01:26:41 +0100130void Deoptimizer::EntryGenerator::Generate() {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100131 GeneratePrologue();
132
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100133 // Unlike on ARM we don't save all the registers, just the useful ones.
134 // For the rest, there are gaps on the stack, so the offsets remain the same.
135 const int kNumberOfRegisters = Register::kNumRegisters;
136
137 RegList restored_regs = kJSCallerSaved | kCalleeSaved;
138 RegList saved_regs = restored_regs | sp.bit() | ra.bit();
139
140 const int kDoubleRegsSize =
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000141 kDoubleSize * FPURegister::kMaxNumAllocatableRegisters;
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100142
143 // Save all FPU registers before messing with them.
144 __ Subu(sp, sp, Operand(kDoubleRegsSize));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000145 for (int i = 0; i < FPURegister::kMaxNumAllocatableRegisters; ++i) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100146 FPURegister fpu_reg = FPURegister::FromAllocationIndex(i);
147 int offset = i * kDoubleSize;
148 __ sdc1(fpu_reg, MemOperand(sp, offset));
149 }
150
151 // Push saved_regs (needed to populate FrameDescription::registers_).
152 // Leave gaps for other registers.
153 __ Subu(sp, sp, kNumberOfRegisters * kPointerSize);
154 for (int16_t i = kNumberOfRegisters - 1; i >= 0; i--) {
155 if ((saved_regs & (1 << i)) != 0) {
156 __ sw(ToRegister(i), MemOperand(sp, kPointerSize * i));
157 }
158 }
159
160 const int kSavedRegistersAreaSize =
161 (kNumberOfRegisters * kPointerSize) + kDoubleRegsSize;
162
163 // Get the bailout id from the stack.
164 __ lw(a2, MemOperand(sp, kSavedRegistersAreaSize));
165
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000166 // Get the address of the location in the code object (a3) (return
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100167 // address for lazy deoptimization) and compute the fp-to-sp delta in
168 // register t0.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000169 __ mov(a3, ra);
170 // Correct one word for bailout id.
171 __ Addu(t0, sp, Operand(kSavedRegistersAreaSize + (1 * kPointerSize)));
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100172
173 __ Subu(t0, fp, t0);
174
175 // Allocate a new deoptimizer object.
176 // Pass four arguments in a0 to a3 and fifth & sixth arguments on stack.
177 __ PrepareCallCFunction(6, t1);
178 __ lw(a0, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
179 __ li(a1, Operand(type())); // bailout type,
180 // a2: bailout id already loaded.
181 // a3: code address or 0 already loaded.
182 __ sw(t0, CFunctionArgumentOperand(5)); // Fp-to-sp delta.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000183 __ li(t1, Operand(ExternalReference::isolate_address(isolate())));
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100184 __ sw(t1, CFunctionArgumentOperand(6)); // Isolate.
185 // Call Deoptimizer::New().
186 {
187 AllowExternalCallThatCantCauseGC scope(masm());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000188 __ CallCFunction(ExternalReference::new_deoptimizer_function(isolate()), 6);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100189 }
190
191 // Preserve "deoptimizer" object in register v0 and get the input
192 // frame descriptor pointer to a1 (deoptimizer->input_);
193 // Move deopt-obj to a0 for call to Deoptimizer::ComputeOutputFrames() below.
194 __ mov(a0, v0);
195 __ lw(a1, MemOperand(v0, Deoptimizer::input_offset()));
196
197 // Copy core registers into FrameDescription::registers_[kNumRegisters].
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000198 DCHECK(Register::kNumRegisters == kNumberOfRegisters);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100199 for (int i = 0; i < kNumberOfRegisters; i++) {
200 int offset = (i * kPointerSize) + FrameDescription::registers_offset();
201 if ((saved_regs & (1 << i)) != 0) {
202 __ lw(a2, MemOperand(sp, i * kPointerSize));
203 __ sw(a2, MemOperand(a1, offset));
204 } else if (FLAG_debug_code) {
205 __ li(a2, kDebugZapValue);
206 __ sw(a2, MemOperand(a1, offset));
207 }
208 }
209
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000210 int double_regs_offset = FrameDescription::double_registers_offset();
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100211 // Copy FPU registers to
212 // double_registers_[DoubleRegister::kNumAllocatableRegisters]
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000213 for (int i = 0; i < FPURegister::NumAllocatableRegisters(); ++i) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100214 int dst_offset = i * kDoubleSize + double_regs_offset;
215 int src_offset = i * kDoubleSize + kNumberOfRegisters * kPointerSize;
216 __ ldc1(f0, MemOperand(sp, src_offset));
217 __ sdc1(f0, MemOperand(a1, dst_offset));
218 }
219
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000220 // Remove the bailout id and the saved registers from the stack.
221 __ Addu(sp, sp, Operand(kSavedRegistersAreaSize + (1 * kPointerSize)));
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100222
223 // Compute a pointer to the unwinding limit in register a2; that is
224 // the first stack slot not part of the input frame.
225 __ lw(a2, MemOperand(a1, FrameDescription::frame_size_offset()));
226 __ Addu(a2, a2, sp);
227
228 // Unwind the stack down to - but not including - the unwinding
229 // limit and copy the contents of the activation frame to the input
230 // frame description.
231 __ Addu(a3, a1, Operand(FrameDescription::frame_content_offset()));
232 Label pop_loop;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000233 Label pop_loop_header;
234 __ BranchShort(&pop_loop_header);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100235 __ bind(&pop_loop);
236 __ pop(t0);
237 __ sw(t0, MemOperand(a3, 0));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000238 __ addiu(a3, a3, sizeof(uint32_t));
239 __ bind(&pop_loop_header);
240 __ BranchShort(&pop_loop, ne, a2, Operand(sp));
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100241
242 // Compute the output frame in the deoptimizer.
243 __ push(a0); // Preserve deoptimizer object across call.
244 // a0: deoptimizer object; a1: scratch.
245 __ PrepareCallCFunction(1, a1);
246 // Call Deoptimizer::ComputeOutputFrames().
247 {
248 AllowExternalCallThatCantCauseGC scope(masm());
249 __ CallCFunction(
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000250 ExternalReference::compute_output_frames_function(isolate()), 1);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100251 }
252 __ pop(a0); // Restore deoptimizer object (class Deoptimizer).
253
254 // Replace the current (input) frame with the output frames.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000255 Label outer_push_loop, inner_push_loop,
256 outer_loop_header, inner_loop_header;
257 // Outer loop state: t0 = current "FrameDescription** output_",
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100258 // a1 = one past the last FrameDescription**.
259 __ lw(a1, MemOperand(a0, Deoptimizer::output_count_offset()));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000260 __ lw(t0, MemOperand(a0, Deoptimizer::output_offset())); // t0 is output_.
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100261 __ sll(a1, a1, kPointerSizeLog2); // Count to offset.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000262 __ addu(a1, t0, a1); // a1 = one past the last FrameDescription**.
263 __ jmp(&outer_loop_header);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100264 __ bind(&outer_push_loop);
265 // Inner loop state: a2 = current FrameDescription*, a3 = loop index.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000266 __ lw(a2, MemOperand(t0, 0)); // output_[ix]
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100267 __ lw(a3, MemOperand(a2, FrameDescription::frame_size_offset()));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000268 __ jmp(&inner_loop_header);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100269 __ bind(&inner_push_loop);
270 __ Subu(a3, a3, Operand(sizeof(uint32_t)));
271 __ Addu(t2, a2, Operand(a3));
272 __ lw(t3, MemOperand(t2, FrameDescription::frame_content_offset()));
273 __ push(t3);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000274 __ bind(&inner_loop_header);
275 __ BranchShort(&inner_push_loop, ne, a3, Operand(zero_reg));
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100276
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000277 __ Addu(t0, t0, Operand(kPointerSize));
278 __ bind(&outer_loop_header);
279 __ BranchShort(&outer_push_loop, lt, t0, Operand(a1));
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100280
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000281 __ lw(a1, MemOperand(a0, Deoptimizer::input_offset()));
282 for (int i = 0; i < FPURegister::kMaxNumAllocatableRegisters; ++i) {
283 const FPURegister fpu_reg = FPURegister::FromAllocationIndex(i);
284 int src_offset = i * kDoubleSize + double_regs_offset;
285 __ ldc1(fpu_reg, MemOperand(a1, src_offset));
286 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100287
288 // Push state, pc, and continuation from the last output frame.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000289 __ lw(t2, MemOperand(a2, FrameDescription::state_offset()));
290 __ push(t2);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100291
292 __ lw(t2, MemOperand(a2, FrameDescription::pc_offset()));
293 __ push(t2);
294 __ lw(t2, MemOperand(a2, FrameDescription::continuation_offset()));
295 __ push(t2);
296
297
298 // Technically restoring 'at' should work unless zero_reg is also restored
299 // but it's safer to check for this.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000300 DCHECK(!(at.bit() & restored_regs));
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100301 // Restore the registers from the last output frame.
302 __ mov(at, a2);
303 for (int i = kNumberOfRegisters - 1; i >= 0; i--) {
304 int offset = (i * kPointerSize) + FrameDescription::registers_offset();
305 if ((restored_regs & (1 << i)) != 0) {
306 __ lw(ToRegister(i), MemOperand(at, offset));
307 }
308 }
309
310 __ InitializeRootRegister();
311
312 __ pop(at); // Get continuation, leave pc on stack.
313 __ pop(ra);
314 __ Jump(at);
315 __ stop("Unreachable.");
Steve Block44f0eee2011-05-26 01:26:41 +0100316}
317
318
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100319// Maximum size of a table entry generated below.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000320const int Deoptimizer::table_entry_size_ = 2 * Assembler::kInstrSize;
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100321
Steve Block44f0eee2011-05-26 01:26:41 +0100322void Deoptimizer::TableEntryGenerator::GeneratePrologue() {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100323 Assembler::BlockTrampolinePoolScope block_trampoline_pool(masm());
324
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000325 // Create a sequence of deoptimization entries.
326 // Note that registers are still live when jumping to an entry.
327 Label table_start, done, done_special, trampoline_jump;
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100328 __ bind(&table_start);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000329 int kMaxEntriesBranchReach = (1 << (kImm16Bits - 2))/
330 (table_entry_size_ / Assembler::kInstrSize);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100331
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000332 if (count() <= kMaxEntriesBranchReach) {
333 // Common case.
334 for (int i = 0; i < count(); i++) {
335 Label start;
336 __ bind(&start);
337 DCHECK(is_int16(i));
338 __ Branch(USE_DELAY_SLOT, &done); // Expose delay slot.
339 __ li(at, i); // In the delay slot.
340
341 DCHECK_EQ(table_entry_size_, masm()->SizeOfCodeGeneratedSince(&start));
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100342 }
343
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000344 DCHECK_EQ(masm()->SizeOfCodeGeneratedSince(&table_start),
345 count() * table_entry_size_);
346 __ bind(&done);
347 __ Push(at);
348 } else {
349 // Uncommon case, the branch cannot reach.
350 // Create mini trampoline and adjust id constants to get proper value at
351 // the end of table.
352 for (int i = kMaxEntriesBranchReach; i > 1; i--) {
353 Label start;
354 __ bind(&start);
355 DCHECK(is_int16(i));
356 __ Branch(USE_DELAY_SLOT, &trampoline_jump); // Expose delay slot.
357 __ li(at, - i); // In the delay slot.
358 DCHECK_EQ(table_entry_size_, masm()->SizeOfCodeGeneratedSince(&start));
359 }
360 // Entry with id == kMaxEntriesBranchReach - 1.
361 __ bind(&trampoline_jump);
362 __ Branch(USE_DELAY_SLOT, &done_special);
363 __ li(at, -1);
364
365 for (int i = kMaxEntriesBranchReach ; i < count(); i++) {
366 Label start;
367 __ bind(&start);
368 DCHECK(is_int16(i));
369 __ Branch(USE_DELAY_SLOT, &done); // Expose delay slot.
370 __ li(at, i); // In the delay slot.
371 }
372
373 DCHECK_EQ(masm()->SizeOfCodeGeneratedSince(&table_start),
374 count() * table_entry_size_);
375 __ bind(&done_special);
376 __ addiu(at, at, kMaxEntriesBranchReach);
377 __ bind(&done);
378 __ Push(at);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100379 }
Steve Block44f0eee2011-05-26 01:26:41 +0100380}
381
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000382
383void FrameDescription::SetCallerPc(unsigned offset, intptr_t value) {
384 SetFrameSlot(offset, value);
385}
386
387
388void FrameDescription::SetCallerFp(unsigned offset, intptr_t value) {
389 SetFrameSlot(offset, value);
390}
391
392
393void FrameDescription::SetCallerConstantPool(unsigned offset, intptr_t value) {
394 // No out-of-line constant pool support.
395 UNREACHABLE();
396}
397
398
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100399#undef __
400
Steve Block44f0eee2011-05-26 01:26:41 +0100401
402} } // namespace v8::internal