1 // 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
5 #include "src/codegen.h"
6 #include "src/deoptimizer.h"
7 #include "src/full-codegen/full-codegen.h"
8 #include "src/register-configuration.h"
9 #include "src/safepoint-table.h"
10
11 namespace v8 {
12 namespace internal {
13
14
patch_size()15 int Deoptimizer::patch_size() {
16 const int kCallInstructionSizeInWords = 4;
17 return kCallInstructionSizeInWords * Assembler::kInstrSize;
18 }
19
20
EnsureRelocSpaceForLazyDeoptimization(Handle<Code> code)21 void 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
PatchCodeForDeoptimization(Isolate * isolate,Code * code)27 void 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 }
41 CodePatcher patcher(isolate, pointer, 1);
42 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) {
48 CodePatcher osr_patcher(isolate, code->instruction_start() + osr_offset,
49 1);
50 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());
70 CodePatcher patcher(isolate, call_address, call_size_in_words);
71 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
FillInputFrame(Address tos,JavaScriptFrame * frame)83 void Deoptimizer::FillInputFrame(Address tos, JavaScriptFrame* frame) {
84 // Set the register values. The values are not important as there are no
85 // callee saved registers in JavaScript frames, so all registers are
86 // spilled. Registers fp and sp are set to the correct values though.
87
88 for (int i = 0; i < Register::kNumRegisters; i++) {
89 input_->SetRegister(i, i * 4);
90 }
91 input_->SetRegister(sp.code(), reinterpret_cast<intptr_t>(frame->sp()));
92 input_->SetRegister(fp.code(), reinterpret_cast<intptr_t>(frame->fp()));
93 for (int i = 0; i < DoubleRegister::kMaxNumRegisters; i++) {
94 input_->SetDoubleRegister(i, 0.0);
95 }
96
97 // Fill the frame content from the actual data on the frame.
98 for (unsigned i = 0; i < input_->GetFrameSize(); i += kPointerSize) {
99 input_->SetFrameSlot(i, Memory::uint32_at(tos + i));
100 }
101 }
102
103
SetPlatformCompiledStubRegisters(FrameDescription * output_frame,CodeStubDescriptor * descriptor)104 void Deoptimizer::SetPlatformCompiledStubRegisters(
105 FrameDescription* output_frame, CodeStubDescriptor* descriptor) {
106 ApiFunction function(descriptor->deoptimization_handler());
107 ExternalReference xref(&function, ExternalReference::BUILTIN_CALL, isolate_);
108 intptr_t handler = reinterpret_cast<intptr_t>(xref.address());
109 int params = descriptor->GetHandlerParameterCount();
110 output_frame->SetRegister(a0.code(), params);
111 output_frame->SetRegister(a1.code(), handler);
112 }
113
114
CopyDoubleRegisters(FrameDescription * output_frame)115 void Deoptimizer::CopyDoubleRegisters(FrameDescription* output_frame) {
116 for (int i = 0; i < DoubleRegister::kMaxNumRegisters; ++i) {
117 double double_value = input_->GetDoubleRegister(i);
118 output_frame->SetDoubleRegister(i, double_value);
119 }
120 }
121
122
HasAlignmentPadding(JSFunction * function)123 bool Deoptimizer::HasAlignmentPadding(JSFunction* function) {
124 // There is no dynamic alignment padding on MIPS in the input frame.
125 return false;
126 }
127
128
129 #define __ masm()->
130
131
132 // This code tries to be close to ia32 code so that any changes can be
133 // easily ported.
Generate()134 void Deoptimizer::TableEntryGenerator::Generate() {
135 GeneratePrologue();
136
137 // Unlike on ARM we don't save all the registers, just the useful ones.
138 // For the rest, there are gaps on the stack, so the offsets remain the same.
139 const int kNumberOfRegisters = Register::kNumRegisters;
140
141 RegList restored_regs = kJSCallerSaved | kCalleeSaved;
142 RegList saved_regs = restored_regs | sp.bit() | ra.bit();
143
144 const int kDoubleRegsSize = kDoubleSize * DoubleRegister::kMaxNumRegisters;
145
146 // Save all FPU registers before messing with them.
147 __ Subu(sp, sp, Operand(kDoubleRegsSize));
148 const RegisterConfiguration* config =
149 RegisterConfiguration::ArchDefault(RegisterConfiguration::CRANKSHAFT);
150 for (int i = 0; i < config->num_allocatable_double_registers(); ++i) {
151 int code = config->GetAllocatableDoubleCode(i);
152 const DoubleRegister fpu_reg = DoubleRegister::from_code(code);
153 int offset = code * kDoubleSize;
154 __ sdc1(fpu_reg, MemOperand(sp, offset));
155 }
156
157 // Push saved_regs (needed to populate FrameDescription::registers_).
158 // Leave gaps for other registers.
159 __ Subu(sp, sp, kNumberOfRegisters * kPointerSize);
160 for (int16_t i = kNumberOfRegisters - 1; i >= 0; i--) {
161 if ((saved_regs & (1 << i)) != 0) {
162 __ sw(ToRegister(i), MemOperand(sp, kPointerSize * i));
163 }
164 }
165
166 __ li(a2, Operand(ExternalReference(Isolate::kCEntryFPAddress, isolate())));
167 __ sw(fp, MemOperand(a2));
168
169 const int kSavedRegistersAreaSize =
170 (kNumberOfRegisters * kPointerSize) + kDoubleRegsSize;
171
172 // Get the bailout id from the stack.
173 __ lw(a2, MemOperand(sp, kSavedRegistersAreaSize));
174
175 // Get the address of the location in the code object (a3) (return
176 // address for lazy deoptimization) and compute the fp-to-sp delta in
177 // register t0.
178 __ mov(a3, ra);
179 // Correct one word for bailout id.
180 __ Addu(t0, sp, Operand(kSavedRegistersAreaSize + (1 * kPointerSize)));
181
182 __ Subu(t0, fp, t0);
183
184 // Allocate a new deoptimizer object.
185 // Pass four arguments in a0 to a3 and fifth & sixth arguments on stack.
186 __ PrepareCallCFunction(6, t1);
187 __ lw(a0, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
188 __ li(a1, Operand(type())); // bailout type,
189 // a2: bailout id already loaded.
190 // a3: code address or 0 already loaded.
191 __ sw(t0, CFunctionArgumentOperand(5)); // Fp-to-sp delta.
192 __ li(t1, Operand(ExternalReference::isolate_address(isolate())));
193 __ sw(t1, CFunctionArgumentOperand(6)); // Isolate.
194 // Call Deoptimizer::New().
195 {
196 AllowExternalCallThatCantCauseGC scope(masm());
197 __ CallCFunction(ExternalReference::new_deoptimizer_function(isolate()), 6);
198 }
199
200 // Preserve "deoptimizer" object in register v0 and get the input
201 // frame descriptor pointer to a1 (deoptimizer->input_);
202 // Move deopt-obj to a0 for call to Deoptimizer::ComputeOutputFrames() below.
203 __ mov(a0, v0);
204 __ lw(a1, MemOperand(v0, Deoptimizer::input_offset()));
205
206 // Copy core registers into FrameDescription::registers_[kNumRegisters].
207 DCHECK(Register::kNumRegisters == kNumberOfRegisters);
208 for (int i = 0; i < kNumberOfRegisters; i++) {
209 int offset = (i * kPointerSize) + FrameDescription::registers_offset();
210 if ((saved_regs & (1 << i)) != 0) {
211 __ lw(a2, MemOperand(sp, i * kPointerSize));
212 __ sw(a2, MemOperand(a1, offset));
213 } else if (FLAG_debug_code) {
214 __ li(a2, kDebugZapValue);
215 __ sw(a2, MemOperand(a1, offset));
216 }
217 }
218
219 int double_regs_offset = FrameDescription::double_registers_offset();
220 // Copy FPU registers to
221 // double_registers_[DoubleRegister::kNumAllocatableRegisters]
222 for (int i = 0; i < config->num_allocatable_double_registers(); ++i) {
223 int code = config->GetAllocatableDoubleCode(i);
224 int dst_offset = code * kDoubleSize + double_regs_offset;
225 int src_offset = code * kDoubleSize + kNumberOfRegisters * kPointerSize;
226 __ ldc1(f0, MemOperand(sp, src_offset));
227 __ sdc1(f0, MemOperand(a1, dst_offset));
228 }
229
230 // Remove the bailout id and the saved registers from the stack.
231 __ Addu(sp, sp, Operand(kSavedRegistersAreaSize + (1 * kPointerSize)));
232
233 // Compute a pointer to the unwinding limit in register a2; that is
234 // the first stack slot not part of the input frame.
235 __ lw(a2, MemOperand(a1, FrameDescription::frame_size_offset()));
236 __ Addu(a2, a2, sp);
237
238 // Unwind the stack down to - but not including - the unwinding
239 // limit and copy the contents of the activation frame to the input
240 // frame description.
241 __ Addu(a3, a1, Operand(FrameDescription::frame_content_offset()));
242 Label pop_loop;
243 Label pop_loop_header;
244 __ BranchShort(&pop_loop_header);
245 __ bind(&pop_loop);
246 __ pop(t0);
247 __ sw(t0, MemOperand(a3, 0));
248 __ addiu(a3, a3, sizeof(uint32_t));
249 __ bind(&pop_loop_header);
250 __ BranchShort(&pop_loop, ne, a2, Operand(sp));
251
252 // Compute the output frame in the deoptimizer.
253 __ push(a0); // Preserve deoptimizer object across call.
254 // a0: deoptimizer object; a1: scratch.
255 __ PrepareCallCFunction(1, a1);
256 // Call Deoptimizer::ComputeOutputFrames().
257 {
258 AllowExternalCallThatCantCauseGC scope(masm());
259 __ CallCFunction(
260 ExternalReference::compute_output_frames_function(isolate()), 1);
261 }
262 __ pop(a0); // Restore deoptimizer object (class Deoptimizer).
263
264 // Replace the current (input) frame with the output frames.
265 Label outer_push_loop, inner_push_loop,
266 outer_loop_header, inner_loop_header;
267 // Outer loop state: t0 = current "FrameDescription** output_",
268 // a1 = one past the last FrameDescription**.
269 __ lw(a1, MemOperand(a0, Deoptimizer::output_count_offset()));
270 __ lw(t0, MemOperand(a0, Deoptimizer::output_offset())); // t0 is output_.
271 __ sll(a1, a1, kPointerSizeLog2); // Count to offset.
272 __ addu(a1, t0, a1); // a1 = one past the last FrameDescription**.
273 __ BranchShort(&outer_loop_header);
274 __ bind(&outer_push_loop);
275 // Inner loop state: a2 = current FrameDescription*, a3 = loop index.
276 __ lw(a2, MemOperand(t0, 0)); // output_[ix]
277 __ lw(a3, MemOperand(a2, FrameDescription::frame_size_offset()));
278 __ BranchShort(&inner_loop_header);
279 __ bind(&inner_push_loop);
280 __ Subu(a3, a3, Operand(sizeof(uint32_t)));
281 __ Addu(t2, a2, Operand(a3));
282 __ lw(t3, MemOperand(t2, FrameDescription::frame_content_offset()));
283 __ push(t3);
284 __ bind(&inner_loop_header);
285 __ BranchShort(&inner_push_loop, ne, a3, Operand(zero_reg));
286
287 __ Addu(t0, t0, Operand(kPointerSize));
288 __ bind(&outer_loop_header);
289 __ BranchShort(&outer_push_loop, lt, t0, Operand(a1));
290
291 __ lw(a1, MemOperand(a0, Deoptimizer::input_offset()));
292 for (int i = 0; i < config->num_allocatable_double_registers(); ++i) {
293 int code = config->GetAllocatableDoubleCode(i);
294 const DoubleRegister fpu_reg = DoubleRegister::from_code(code);
295 int src_offset = code * kDoubleSize + double_regs_offset;
296 __ ldc1(fpu_reg, MemOperand(a1, src_offset));
297 }
298
299 // Push state, pc, and continuation from the last output frame.
300 __ lw(t2, MemOperand(a2, FrameDescription::state_offset()));
301 __ push(t2);
302
303 __ lw(t2, MemOperand(a2, FrameDescription::pc_offset()));
304 __ push(t2);
305 __ lw(t2, MemOperand(a2, FrameDescription::continuation_offset()));
306 __ push(t2);
307
308
309 // Technically restoring 'at' should work unless zero_reg is also restored
310 // but it's safer to check for this.
311 DCHECK(!(at.bit() & restored_regs));
312 // Restore the registers from the last output frame.
313 __ mov(at, a2);
314 for (int i = kNumberOfRegisters - 1; i >= 0; i--) {
315 int offset = (i * kPointerSize) + FrameDescription::registers_offset();
316 if ((restored_regs & (1 << i)) != 0) {
317 __ lw(ToRegister(i), MemOperand(at, offset));
318 }
319 }
320
321 __ InitializeRootRegister();
322
323 __ pop(at); // Get continuation, leave pc on stack.
324 __ pop(ra);
325 __ Jump(at);
326 __ stop("Unreachable.");
327 }
328
329
330 // Maximum size of a table entry generated below.
331 const int Deoptimizer::table_entry_size_ = 2 * Assembler::kInstrSize;
332
GeneratePrologue()333 void Deoptimizer::TableEntryGenerator::GeneratePrologue() {
334 Assembler::BlockTrampolinePoolScope block_trampoline_pool(masm());
335
336 // Create a sequence of deoptimization entries.
337 // Note that registers are still live when jumping to an entry.
338 Label table_start, done, done_special, trampoline_jump;
339 __ bind(&table_start);
340 int kMaxEntriesBranchReach = (1 << (kImm16Bits - 2))/
341 (table_entry_size_ / Assembler::kInstrSize);
342
343 if (count() <= kMaxEntriesBranchReach) {
344 // Common case.
345 for (int i = 0; i < count(); i++) {
346 Label start;
347 __ bind(&start);
348 DCHECK(is_int16(i));
349 __ BranchShort(USE_DELAY_SLOT, &done); // Expose delay slot.
350 __ li(at, i); // In the delay slot.
351
352 DCHECK_EQ(table_entry_size_, masm()->SizeOfCodeGeneratedSince(&start));
353 }
354
355 DCHECK_EQ(masm()->SizeOfCodeGeneratedSince(&table_start),
356 count() * table_entry_size_);
357 __ bind(&done);
358 __ Push(at);
359 } else {
360 // Uncommon case, the branch cannot reach.
361 // Create mini trampoline and adjust id constants to get proper value at
362 // the end of table.
363 for (int i = kMaxEntriesBranchReach; i > 1; i--) {
364 Label start;
365 __ bind(&start);
366 DCHECK(is_int16(i));
367 __ BranchShort(USE_DELAY_SLOT, &trampoline_jump); // Expose delay slot.
368 __ li(at, - i); // In the delay slot.
369 DCHECK_EQ(table_entry_size_, masm()->SizeOfCodeGeneratedSince(&start));
370 }
371 // Entry with id == kMaxEntriesBranchReach - 1.
372 __ bind(&trampoline_jump);
373 __ BranchShort(USE_DELAY_SLOT, &done_special);
374 __ li(at, -1);
375
376 for (int i = kMaxEntriesBranchReach ; i < count(); i++) {
377 Label start;
378 __ bind(&start);
379 DCHECK(is_int16(i));
380 __ BranchShort(USE_DELAY_SLOT, &done); // Expose delay slot.
381 __ li(at, i); // In the delay slot.
382 }
383
384 DCHECK_EQ(masm()->SizeOfCodeGeneratedSince(&table_start),
385 count() * table_entry_size_);
386 __ bind(&done_special);
387 __ addiu(at, at, kMaxEntriesBranchReach);
388 __ bind(&done);
389 __ Push(at);
390 }
391 }
392
393
SetCallerPc(unsigned offset,intptr_t value)394 void FrameDescription::SetCallerPc(unsigned offset, intptr_t value) {
395 SetFrameSlot(offset, value);
396 }
397
398
SetCallerFp(unsigned offset,intptr_t value)399 void FrameDescription::SetCallerFp(unsigned offset, intptr_t value) {
400 SetFrameSlot(offset, value);
401 }
402
403
SetCallerConstantPool(unsigned offset,intptr_t value)404 void FrameDescription::SetCallerConstantPool(unsigned offset, intptr_t value) {
405 // No embedded constant pool support.
406 UNREACHABLE();
407 }
408
409
410 #undef __
411
412
413 } // namespace internal
414 } // namespace v8
415