1 // Copyright 2014 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 const int Deoptimizer::table_entry_size_ = 8;
15
16
patch_size()17 int Deoptimizer::patch_size() {
18 #if V8_TARGET_ARCH_PPC64
19 const int kCallInstructionSizeInWords = 7;
20 #else
21 const int kCallInstructionSizeInWords = 4;
22 #endif
23 return kCallInstructionSizeInWords * Assembler::kInstrSize;
24 }
25
26
EnsureRelocSpaceForLazyDeoptimization(Handle<Code> code)27 void Deoptimizer::EnsureRelocSpaceForLazyDeoptimization(Handle<Code> code) {
28 // Empty because there is no need for relocation information for the code
29 // patching in Deoptimizer::PatchCodeForDeoptimization below.
30 }
31
32
PatchCodeForDeoptimization(Isolate * isolate,Code * code)33 void Deoptimizer::PatchCodeForDeoptimization(Isolate* isolate, Code* code) {
34 Address code_start_address = code->instruction_start();
35
36 // Invalidate the relocation information, as it will become invalid by the
37 // code patching below, and is not needed any more.
38 code->InvalidateRelocation();
39
40 if (FLAG_zap_code_space) {
41 // Fail hard and early if we enter this code object again.
42 byte* pointer = code->FindCodeAgeSequence();
43 if (pointer != NULL) {
44 pointer += kNoCodeAgeSequenceLength;
45 } else {
46 pointer = code->instruction_start();
47 }
48 CodePatcher patcher(isolate, pointer, 1);
49 patcher.masm()->bkpt(0);
50
51 DeoptimizationInputData* data =
52 DeoptimizationInputData::cast(code->deoptimization_data());
53 int osr_offset = data->OsrPcOffset()->value();
54 if (osr_offset > 0) {
55 CodePatcher osr_patcher(isolate, code->instruction_start() + osr_offset,
56 1);
57 osr_patcher.masm()->bkpt(0);
58 }
59 }
60
61 DeoptimizationInputData* deopt_data =
62 DeoptimizationInputData::cast(code->deoptimization_data());
63 #ifdef DEBUG
64 Address prev_call_address = NULL;
65 #endif
66 // For each LLazyBailout instruction insert a call to the corresponding
67 // deoptimization entry.
68 for (int i = 0; i < deopt_data->DeoptCount(); i++) {
69 if (deopt_data->Pc(i)->value() == -1) continue;
70 Address call_address = code_start_address + deopt_data->Pc(i)->value();
71 Address deopt_entry = GetDeoptimizationEntry(isolate, i, LAZY);
72 // We need calls to have a predictable size in the unoptimized code, but
73 // this is optimized code, so we don't have to have a predictable size.
74 int call_size_in_bytes = MacroAssembler::CallSizeNotPredictableCodeSize(
75 deopt_entry, kRelocInfo_NONEPTR);
76 int call_size_in_words = call_size_in_bytes / Assembler::kInstrSize;
77 DCHECK(call_size_in_bytes % Assembler::kInstrSize == 0);
78 DCHECK(call_size_in_bytes <= patch_size());
79 CodePatcher patcher(isolate, call_address, call_size_in_words);
80 patcher.masm()->Call(deopt_entry, kRelocInfo_NONEPTR);
81 DCHECK(prev_call_address == NULL ||
82 call_address >= prev_call_address + patch_size());
83 DCHECK(call_address + patch_size() <= code->instruction_end());
84 #ifdef DEBUG
85 prev_call_address = call_address;
86 #endif
87 }
88 }
89
90
SetPlatformCompiledStubRegisters(FrameDescription * output_frame,CodeStubDescriptor * descriptor)91 void Deoptimizer::SetPlatformCompiledStubRegisters(
92 FrameDescription* output_frame, CodeStubDescriptor* descriptor) {
93 ApiFunction function(descriptor->deoptimization_handler());
94 ExternalReference xref(&function, ExternalReference::BUILTIN_CALL, isolate_);
95 intptr_t handler = reinterpret_cast<intptr_t>(xref.address());
96 int params = descriptor->GetHandlerParameterCount();
97 output_frame->SetRegister(r3.code(), params);
98 output_frame->SetRegister(r4.code(), handler);
99 }
100
101
CopyDoubleRegisters(FrameDescription * output_frame)102 void Deoptimizer::CopyDoubleRegisters(FrameDescription* output_frame) {
103 for (int i = 0; i < DoubleRegister::kNumRegisters; ++i) {
104 Float64 double_value = input_->GetDoubleRegister(i);
105 output_frame->SetDoubleRegister(i, double_value);
106 }
107 }
108
109 #define __ masm()->
110
111 // This code tries to be close to ia32 code so that any changes can be
112 // easily ported.
Generate()113 void Deoptimizer::TableEntryGenerator::Generate() {
114 GeneratePrologue();
115
116 // Unlike on ARM we don't save all the registers, just the useful ones.
117 // For the rest, there are gaps on the stack, so the offsets remain the same.
118 const int kNumberOfRegisters = Register::kNumRegisters;
119
120 RegList restored_regs = kJSCallerSaved | kCalleeSaved;
121 RegList saved_regs = restored_regs | sp.bit();
122
123 const int kDoubleRegsSize = kDoubleSize * DoubleRegister::kNumRegisters;
124
125 // Save all double registers before messing with them.
126 __ subi(sp, sp, Operand(kDoubleRegsSize));
127 const RegisterConfiguration* config = RegisterConfiguration::Crankshaft();
128 for (int i = 0; i < config->num_allocatable_double_registers(); ++i) {
129 int code = config->GetAllocatableDoubleCode(i);
130 const DoubleRegister dreg = DoubleRegister::from_code(code);
131 int offset = code * kDoubleSize;
132 __ stfd(dreg, MemOperand(sp, offset));
133 }
134
135 // Push saved_regs (needed to populate FrameDescription::registers_).
136 // Leave gaps for other registers.
137 __ subi(sp, sp, Operand(kNumberOfRegisters * kPointerSize));
138 for (int16_t i = kNumberOfRegisters - 1; i >= 0; i--) {
139 if ((saved_regs & (1 << i)) != 0) {
140 __ StoreP(ToRegister(i), MemOperand(sp, kPointerSize * i));
141 }
142 }
143
144 __ mov(ip, Operand(ExternalReference(Isolate::kCEntryFPAddress, isolate())));
145 __ StoreP(fp, MemOperand(ip));
146
147 const int kSavedRegistersAreaSize =
148 (kNumberOfRegisters * kPointerSize) + kDoubleRegsSize;
149
150 // Get the bailout id from the stack.
151 __ LoadP(r5, MemOperand(sp, kSavedRegistersAreaSize));
152
153 // Get the address of the location in the code object (r6) (return
154 // address for lazy deoptimization) and compute the fp-to-sp delta in
155 // register r7.
156 __ mflr(r6);
157 // Correct one word for bailout id.
158 __ addi(r7, sp, Operand(kSavedRegistersAreaSize + (1 * kPointerSize)));
159 __ sub(r7, fp, r7);
160
161 // Allocate a new deoptimizer object.
162 // Pass six arguments in r3 to r8.
163 __ PrepareCallCFunction(6, r8);
164 __ li(r3, Operand::Zero());
165 Label context_check;
166 __ LoadP(r4, MemOperand(fp, CommonFrameConstants::kContextOrFrameTypeOffset));
167 __ JumpIfSmi(r4, &context_check);
168 __ LoadP(r3, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
169 __ bind(&context_check);
170 __ li(r4, Operand(type())); // bailout type,
171 // r5: bailout id already loaded.
172 // r6: code address or 0 already loaded.
173 // r7: Fp-to-sp delta.
174 __ mov(r8, Operand(ExternalReference::isolate_address(isolate())));
175 // Call Deoptimizer::New().
176 {
177 AllowExternalCallThatCantCauseGC scope(masm());
178 __ CallCFunction(ExternalReference::new_deoptimizer_function(isolate()), 6);
179 }
180
181 // Preserve "deoptimizer" object in register r3 and get the input
182 // frame descriptor pointer to r4 (deoptimizer->input_);
183 __ LoadP(r4, MemOperand(r3, Deoptimizer::input_offset()));
184
185 // Copy core registers into FrameDescription::registers_[kNumRegisters].
186 DCHECK(Register::kNumRegisters == kNumberOfRegisters);
187 for (int i = 0; i < kNumberOfRegisters; i++) {
188 int offset = (i * kPointerSize) + FrameDescription::registers_offset();
189 __ LoadP(r5, MemOperand(sp, i * kPointerSize));
190 __ StoreP(r5, MemOperand(r4, offset));
191 }
192
193 int double_regs_offset = FrameDescription::double_registers_offset();
194 // Copy double registers to
195 // double_registers_[DoubleRegister::kNumRegisters]
196 for (int i = 0; i < config->num_allocatable_double_registers(); ++i) {
197 int code = config->GetAllocatableDoubleCode(i);
198 int dst_offset = code * kDoubleSize + double_regs_offset;
199 int src_offset = code * kDoubleSize + kNumberOfRegisters * kPointerSize;
200 __ lfd(d0, MemOperand(sp, src_offset));
201 __ stfd(d0, MemOperand(r4, dst_offset));
202 }
203
204 // Remove the bailout id and the saved registers from the stack.
205 __ addi(sp, sp, Operand(kSavedRegistersAreaSize + (1 * kPointerSize)));
206
207 // Compute a pointer to the unwinding limit in register r5; that is
208 // the first stack slot not part of the input frame.
209 __ LoadP(r5, MemOperand(r4, FrameDescription::frame_size_offset()));
210 __ add(r5, r5, sp);
211
212 // Unwind the stack down to - but not including - the unwinding
213 // limit and copy the contents of the activation frame to the input
214 // frame description.
215 __ addi(r6, r4, Operand(FrameDescription::frame_content_offset()));
216 Label pop_loop;
217 Label pop_loop_header;
218 __ b(&pop_loop_header);
219 __ bind(&pop_loop);
220 __ pop(r7);
221 __ StoreP(r7, MemOperand(r6, 0));
222 __ addi(r6, r6, Operand(kPointerSize));
223 __ bind(&pop_loop_header);
224 __ cmp(r5, sp);
225 __ bne(&pop_loop);
226
227 // Compute the output frame in the deoptimizer.
228 __ push(r3); // Preserve deoptimizer object across call.
229 // r3: deoptimizer object; r4: scratch.
230 __ PrepareCallCFunction(1, r4);
231 // Call Deoptimizer::ComputeOutputFrames().
232 {
233 AllowExternalCallThatCantCauseGC scope(masm());
234 __ CallCFunction(
235 ExternalReference::compute_output_frames_function(isolate()), 1);
236 }
237 __ pop(r3); // Restore deoptimizer object (class Deoptimizer).
238
239 __ LoadP(sp, MemOperand(r3, Deoptimizer::caller_frame_top_offset()));
240
241 // Replace the current (input) frame with the output frames.
242 Label outer_push_loop, inner_push_loop, outer_loop_header, inner_loop_header;
243 // Outer loop state: r7 = current "FrameDescription** output_",
244 // r4 = one past the last FrameDescription**.
245 __ lwz(r4, MemOperand(r3, Deoptimizer::output_count_offset()));
246 __ LoadP(r7, MemOperand(r3, Deoptimizer::output_offset())); // r7 is output_.
247 __ ShiftLeftImm(r4, r4, Operand(kPointerSizeLog2));
248 __ add(r4, r7, r4);
249 __ b(&outer_loop_header);
250
251 __ bind(&outer_push_loop);
252 // Inner loop state: r5 = current FrameDescription*, r6 = loop index.
253 __ LoadP(r5, MemOperand(r7, 0)); // output_[ix]
254 __ LoadP(r6, MemOperand(r5, FrameDescription::frame_size_offset()));
255 __ b(&inner_loop_header);
256
257 __ bind(&inner_push_loop);
258 __ addi(r6, r6, Operand(-sizeof(intptr_t)));
259 __ add(r9, r5, r6);
260 __ LoadP(r9, MemOperand(r9, FrameDescription::frame_content_offset()));
261 __ push(r9);
262
263 __ bind(&inner_loop_header);
264 __ cmpi(r6, Operand::Zero());
265 __ bne(&inner_push_loop); // test for gt?
266
267 __ addi(r7, r7, Operand(kPointerSize));
268 __ bind(&outer_loop_header);
269 __ cmp(r7, r4);
270 __ blt(&outer_push_loop);
271
272 __ LoadP(r4, MemOperand(r3, Deoptimizer::input_offset()));
273 for (int i = 0; i < config->num_allocatable_double_registers(); ++i) {
274 int code = config->GetAllocatableDoubleCode(i);
275 const DoubleRegister dreg = DoubleRegister::from_code(code);
276 int src_offset = code * kDoubleSize + double_regs_offset;
277 __ lfd(dreg, MemOperand(r4, src_offset));
278 }
279
280 // Push state, pc, and continuation from the last output frame.
281 __ LoadP(r9, MemOperand(r5, FrameDescription::state_offset()));
282 __ push(r9);
283 __ LoadP(r9, MemOperand(r5, FrameDescription::pc_offset()));
284 __ push(r9);
285 __ LoadP(r9, MemOperand(r5, FrameDescription::continuation_offset()));
286 __ push(r9);
287
288 // Restore the registers from the last output frame.
289 DCHECK(!(ip.bit() & restored_regs));
290 __ mr(ip, r5);
291 for (int i = kNumberOfRegisters - 1; i >= 0; i--) {
292 int offset = (i * kPointerSize) + FrameDescription::registers_offset();
293 if ((restored_regs & (1 << i)) != 0) {
294 __ LoadP(ToRegister(i), MemOperand(ip, offset));
295 }
296 }
297
298 __ InitializeRootRegister();
299
300 __ pop(ip); // get continuation, leave pc on stack
301 __ pop(r0);
302 __ mtlr(r0);
303 __ Jump(ip);
304 __ stop("Unreachable.");
305 }
306
307
GeneratePrologue()308 void Deoptimizer::TableEntryGenerator::GeneratePrologue() {
309 Assembler::BlockTrampolinePoolScope block_trampoline_pool(masm());
310
311 // Create a sequence of deoptimization entries.
312 // Note that registers are still live when jumping to an entry.
313 Label done;
314 for (int i = 0; i < count(); i++) {
315 int start = masm()->pc_offset();
316 USE(start);
317 __ li(ip, Operand(i));
318 __ b(&done);
319 DCHECK(masm()->pc_offset() - start == table_entry_size_);
320 }
321 __ bind(&done);
322 __ push(ip);
323 }
324
325
SetCallerPc(unsigned offset,intptr_t value)326 void FrameDescription::SetCallerPc(unsigned offset, intptr_t value) {
327 SetFrameSlot(offset, value);
328 }
329
330
SetCallerFp(unsigned offset,intptr_t value)331 void FrameDescription::SetCallerFp(unsigned offset, intptr_t value) {
332 SetFrameSlot(offset, value);
333 }
334
335
SetCallerConstantPool(unsigned offset,intptr_t value)336 void FrameDescription::SetCallerConstantPool(unsigned offset, intptr_t value) {
337 DCHECK(FLAG_enable_embedded_constant_pool);
338 SetFrameSlot(offset, value);
339 }
340
341
342 #undef __
343 } // namespace internal
344 } // namespace v8
345