blob: 118c5dfa8dcd748e1283da3f408710a59c5f2a50 [file] [log] [blame]
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001// Copyright 2013 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 Murdoch4a90d5f2016-03-22 12:00:34 +00005#include "src/arm64/frames-arm64.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006#include "src/codegen.h"
7#include "src/deoptimizer.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008#include "src/full-codegen/full-codegen.h"
9#include "src/register-configuration.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010#include "src/safepoint-table.h"
11
12
13namespace v8 {
14namespace internal {
15
16
17int Deoptimizer::patch_size() {
18 // Size of the code used to patch lazy bailout points.
19 // Patching is done by Deoptimizer::DeoptimizeFunction.
20 return 4 * kInstructionSize;
21}
22
23
Emily Bernierd0a1eb72015-03-24 16:35:39 -040024void Deoptimizer::EnsureRelocSpaceForLazyDeoptimization(Handle<Code> code) {
25 // Empty because there is no need for relocation information for the code
26 // patching in Deoptimizer::PatchCodeForDeoptimization below.
27}
28
Ben Murdochb8a8cc12014-11-26 15:28:44 +000029
30void Deoptimizer::PatchCodeForDeoptimization(Isolate* isolate, Code* code) {
31 // 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
35 // TODO(jkummerow): if (FLAG_zap_code_space), make the code object's
36 // entry sequence unusable (see other architectures).
37
38 DeoptimizationInputData* deopt_data =
39 DeoptimizationInputData::cast(code->deoptimization_data());
40 Address code_start_address = code->instruction_start();
41#ifdef DEBUG
42 Address prev_call_address = NULL;
43#endif
44 // For each LLazyBailout instruction insert a call to the corresponding
45 // deoptimization entry.
46 for (int i = 0; i < deopt_data->DeoptCount(); i++) {
47 if (deopt_data->Pc(i)->value() == -1) continue;
48
49 Address call_address = code_start_address + deopt_data->Pc(i)->value();
50 Address deopt_entry = GetDeoptimizationEntry(isolate, i, LAZY);
51
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000052 PatchingAssembler patcher(isolate, call_address,
53 patch_size() / kInstructionSize);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000054 patcher.ldr_pcrel(ip0, (2 * kInstructionSize) >> kLoadLiteralScaleLog2);
55 patcher.blr(ip0);
56 patcher.dc64(reinterpret_cast<intptr_t>(deopt_entry));
57
58 DCHECK((prev_call_address == NULL) ||
59 (call_address >= prev_call_address + patch_size()));
60 DCHECK(call_address + patch_size() <= code->instruction_end());
61#ifdef DEBUG
62 prev_call_address = call_address;
63#endif
64 }
65}
66
67
68void Deoptimizer::FillInputFrame(Address tos, JavaScriptFrame* frame) {
69 // Set the register values. The values are not important as there are no
70 // callee saved registers in JavaScript frames, so all registers are
71 // spilled. Registers fp and sp are set to the correct values though.
72 for (int i = 0; i < Register::NumRegisters(); i++) {
73 input_->SetRegister(i, 0);
74 }
75
76 // TODO(all): Do we also need to set a value to csp?
77 input_->SetRegister(jssp.code(), reinterpret_cast<intptr_t>(frame->sp()));
78 input_->SetRegister(fp.code(), reinterpret_cast<intptr_t>(frame->fp()));
79
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000080 for (int i = 0; i < DoubleRegister::kMaxNumRegisters; i++) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000081 input_->SetDoubleRegister(i, 0.0);
82 }
83
84 // Fill the frame content from the actual data on the frame.
85 for (unsigned i = 0; i < input_->GetFrameSize(); i += kPointerSize) {
86 input_->SetFrameSlot(i, Memory::uint64_at(tos + i));
87 }
88}
89
90
91bool Deoptimizer::HasAlignmentPadding(JSFunction* function) {
92 // There is no dynamic alignment padding on ARM64 in the input frame.
93 return false;
94}
95
96
97void Deoptimizer::SetPlatformCompiledStubRegisters(
98 FrameDescription* output_frame, CodeStubDescriptor* descriptor) {
99 ApiFunction function(descriptor->deoptimization_handler());
100 ExternalReference xref(&function, ExternalReference::BUILTIN_CALL, isolate_);
101 intptr_t handler = reinterpret_cast<intptr_t>(xref.address());
102 int params = descriptor->GetHandlerParameterCount();
103 output_frame->SetRegister(x0.code(), params);
104 output_frame->SetRegister(x1.code(), handler);
105}
106
107
108void Deoptimizer::CopyDoubleRegisters(FrameDescription* output_frame) {
109 for (int i = 0; i < DoubleRegister::kMaxNumRegisters; ++i) {
110 double double_value = input_->GetDoubleRegister(i);
111 output_frame->SetDoubleRegister(i, double_value);
112 }
113}
114
115
116
117#define __ masm()->
118
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000119void Deoptimizer::TableEntryGenerator::Generate() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000120 GeneratePrologue();
121
122 // TODO(all): This code needs to be revisited. We probably only need to save
123 // caller-saved registers here. Callee-saved registers can be stored directly
124 // in the input frame.
125
126 // Save all allocatable floating point registers.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000127 CPURegList saved_fp_registers(
128 CPURegister::kFPRegister, kDRegSizeInBits,
129 RegisterConfiguration::ArchDefault(RegisterConfiguration::CRANKSHAFT)
130 ->allocatable_double_codes_mask());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000131 __ PushCPURegList(saved_fp_registers);
132
133 // We save all the registers expcept jssp, sp and lr.
134 CPURegList saved_registers(CPURegister::kRegister, kXRegSizeInBits, 0, 27);
135 saved_registers.Combine(fp);
136 __ PushCPURegList(saved_registers);
137
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000138 __ Mov(x3, Operand(ExternalReference(Isolate::kCEntryFPAddress, isolate())));
139 __ Str(fp, MemOperand(x3));
140
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000141 const int kSavedRegistersAreaSize =
142 (saved_registers.Count() * kXRegSize) +
143 (saved_fp_registers.Count() * kDRegSize);
144
145 // Floating point registers are saved on the stack above core registers.
146 const int kFPRegistersOffset = saved_registers.Count() * kXRegSize;
147
148 // Get the bailout id from the stack.
149 Register bailout_id = x2;
150 __ Peek(bailout_id, kSavedRegistersAreaSize);
151
152 Register code_object = x3;
153 Register fp_to_sp = x4;
154 // Get the address of the location in the code object. This is the return
155 // address for lazy deoptimization.
156 __ Mov(code_object, lr);
157 // Compute the fp-to-sp delta, and correct one word for bailout id.
158 __ Add(fp_to_sp, masm()->StackPointer(),
159 kSavedRegistersAreaSize + (1 * kPointerSize));
160 __ Sub(fp_to_sp, fp, fp_to_sp);
161
162 // Allocate a new deoptimizer object.
163 __ Ldr(x0, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
164 __ Mov(x1, type());
165 // Following arguments are already loaded:
166 // - x2: bailout id
167 // - x3: code object address
168 // - x4: fp-to-sp delta
169 __ Mov(x5, ExternalReference::isolate_address(isolate()));
170
171 {
172 // Call Deoptimizer::New().
173 AllowExternalCallThatCantCauseGC scope(masm());
174 __ CallCFunction(ExternalReference::new_deoptimizer_function(isolate()), 6);
175 }
176
177 // Preserve "deoptimizer" object in register x0.
178 Register deoptimizer = x0;
179
180 // Get the input frame descriptor pointer.
181 __ Ldr(x1, MemOperand(deoptimizer, Deoptimizer::input_offset()));
182
183 // Copy core registers into the input frame.
184 CPURegList copy_to_input = saved_registers;
185 for (int i = 0; i < saved_registers.Count(); i++) {
186 __ Peek(x2, i * kPointerSize);
187 CPURegister current_reg = copy_to_input.PopLowestIndex();
188 int offset = (current_reg.code() * kPointerSize) +
189 FrameDescription::registers_offset();
190 __ Str(x2, MemOperand(x1, offset));
191 }
192
193 // Copy FP registers to the input frame.
194 for (int i = 0; i < saved_fp_registers.Count(); i++) {
195 int dst_offset = FrameDescription::double_registers_offset() +
196 (i * kDoubleSize);
197 int src_offset = kFPRegistersOffset + (i * kDoubleSize);
198 __ Peek(x2, src_offset);
199 __ Str(x2, MemOperand(x1, dst_offset));
200 }
201
202 // Remove the bailout id and the saved registers from the stack.
203 __ Drop(1 + (kSavedRegistersAreaSize / kXRegSize));
204
205 // Compute a pointer to the unwinding limit in register x2; that is
206 // the first stack slot not part of the input frame.
207 Register unwind_limit = x2;
208 __ Ldr(unwind_limit, MemOperand(x1, FrameDescription::frame_size_offset()));
209 __ Add(unwind_limit, unwind_limit, __ StackPointer());
210
211 // Unwind the stack down to - but not including - the unwinding
212 // limit and copy the contents of the activation frame to the input
213 // frame description.
214 __ Add(x3, x1, FrameDescription::frame_content_offset());
215 Label pop_loop;
216 Label pop_loop_header;
217 __ B(&pop_loop_header);
218 __ Bind(&pop_loop);
219 __ Pop(x4);
220 __ Str(x4, MemOperand(x3, kPointerSize, PostIndex));
221 __ Bind(&pop_loop_header);
222 __ Cmp(unwind_limit, __ StackPointer());
223 __ B(ne, &pop_loop);
224
225 // Compute the output frame in the deoptimizer.
226 __ Push(x0); // Preserve deoptimizer object across call.
227
228 {
229 // Call Deoptimizer::ComputeOutputFrames().
230 AllowExternalCallThatCantCauseGC scope(masm());
231 __ CallCFunction(
232 ExternalReference::compute_output_frames_function(isolate()), 1);
233 }
234 __ Pop(x4); // Restore deoptimizer object (class Deoptimizer).
235
236 // Replace the current (input) frame with the output frames.
237 Label outer_push_loop, inner_push_loop,
238 outer_loop_header, inner_loop_header;
239 __ Ldrsw(x1, MemOperand(x4, Deoptimizer::output_count_offset()));
240 __ Ldr(x0, MemOperand(x4, Deoptimizer::output_offset()));
241 __ Add(x1, x0, Operand(x1, LSL, kPointerSizeLog2));
242 __ B(&outer_loop_header);
243
244 __ Bind(&outer_push_loop);
245 Register current_frame = x2;
246 __ Ldr(current_frame, MemOperand(x0, 0));
247 __ Ldr(x3, MemOperand(current_frame, FrameDescription::frame_size_offset()));
248 __ B(&inner_loop_header);
249
250 __ Bind(&inner_push_loop);
251 __ Sub(x3, x3, kPointerSize);
252 __ Add(x6, current_frame, x3);
253 __ Ldr(x7, MemOperand(x6, FrameDescription::frame_content_offset()));
254 __ Push(x7);
255 __ Bind(&inner_loop_header);
256 __ Cbnz(x3, &inner_push_loop);
257
258 __ Add(x0, x0, kPointerSize);
259 __ Bind(&outer_loop_header);
260 __ Cmp(x0, x1);
261 __ B(lt, &outer_push_loop);
262
263 __ Ldr(x1, MemOperand(x4, Deoptimizer::input_offset()));
264 DCHECK(!saved_fp_registers.IncludesAliasOf(crankshaft_fp_scratch) &&
265 !saved_fp_registers.IncludesAliasOf(fp_zero) &&
266 !saved_fp_registers.IncludesAliasOf(fp_scratch));
267 int src_offset = FrameDescription::double_registers_offset();
268 while (!saved_fp_registers.IsEmpty()) {
269 const CPURegister reg = saved_fp_registers.PopLowestIndex();
270 __ Ldr(reg, MemOperand(x1, src_offset));
271 src_offset += kDoubleSize;
272 }
273
274 // Push state from the last output frame.
275 __ Ldr(x6, MemOperand(current_frame, FrameDescription::state_offset()));
276 __ Push(x6);
277
278 // TODO(all): ARM copies a lot (if not all) of the last output frame onto the
279 // stack, then pops it all into registers. Here, we try to load it directly
280 // into the relevant registers. Is this correct? If so, we should improve the
281 // ARM code.
282
283 // TODO(all): This code needs to be revisited, We probably don't need to
284 // restore all the registers as fullcodegen does not keep live values in
285 // registers (note that at least fp must be restored though).
286
287 // Restore registers from the last output frame.
288 // Note that lr is not in the list of saved_registers and will be restored
289 // later. We can use it to hold the address of last output frame while
290 // reloading the other registers.
291 DCHECK(!saved_registers.IncludesAliasOf(lr));
292 Register last_output_frame = lr;
293 __ Mov(last_output_frame, current_frame);
294
295 // We don't need to restore x7 as it will be clobbered later to hold the
296 // continuation address.
297 Register continuation = x7;
298 saved_registers.Remove(continuation);
299
300 while (!saved_registers.IsEmpty()) {
301 // TODO(all): Look for opportunities to optimize this by using ldp.
302 CPURegister current_reg = saved_registers.PopLowestIndex();
303 int offset = (current_reg.code() * kPointerSize) +
304 FrameDescription::registers_offset();
305 __ Ldr(current_reg, MemOperand(last_output_frame, offset));
306 }
307
308 __ Ldr(continuation, MemOperand(last_output_frame,
309 FrameDescription::continuation_offset()));
310 __ Ldr(lr, MemOperand(last_output_frame, FrameDescription::pc_offset()));
311 __ InitializeRootRegister();
312 __ Br(continuation);
313}
314
315
316// Size of an entry of the second level deopt table.
317// This is the code size generated by GeneratePrologue for one entry.
318const int Deoptimizer::table_entry_size_ = 2 * kInstructionSize;
319
320
321void Deoptimizer::TableEntryGenerator::GeneratePrologue() {
322 UseScratchRegisterScope temps(masm());
323 Register entry_id = temps.AcquireX();
324
325 // Create a sequence of deoptimization entries.
326 // Note that registers are still live when jumping to an entry.
327 Label done;
328 {
329 InstructionAccurateScope scope(masm());
330
331 // The number of entry will never exceed kMaxNumberOfEntries.
332 // As long as kMaxNumberOfEntries is a valid 16 bits immediate you can use
333 // a movz instruction to load the entry id.
334 DCHECK(is_uint16(Deoptimizer::kMaxNumberOfEntries));
335
336 for (int i = 0; i < count(); i++) {
337 int start = masm()->pc_offset();
338 USE(start);
339 __ movz(entry_id, i);
340 __ b(&done);
341 DCHECK(masm()->pc_offset() - start == table_entry_size_);
342 }
343 }
344 __ Bind(&done);
345 __ Push(entry_id);
346}
347
348
349void FrameDescription::SetCallerPc(unsigned offset, intptr_t value) {
350 SetFrameSlot(offset, value);
351}
352
353
354void FrameDescription::SetCallerFp(unsigned offset, intptr_t value) {
355 SetFrameSlot(offset, value);
356}
357
358
359void FrameDescription::SetCallerConstantPool(unsigned offset, intptr_t value) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000360 // No embedded constant pool support.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000361 UNREACHABLE();
362}
363
364
365#undef __
366
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000367} // namespace internal
368} // namespace v8