V8 API Reference, 7.2.502.16 (for Deno 0.2.4)
deoptimizer-arm.cc
1 // Copyright 2012 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/assembler-inl.h"
6 #include "src/deoptimizer.h"
7 #include "src/objects-inl.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 #define __ masm()->
17 
18 // This code tries to be close to ia32 code so that any changes can be
19 // easily ported.
20 void Deoptimizer::TableEntryGenerator::Generate() {
21  GeneratePrologue();
22 
23  // Save all general purpose registers before messing with them.
24  const int kNumberOfRegisters = Register::kNumRegisters;
25 
26  // Everything but pc, lr and ip which will be saved but not restored.
27  RegList restored_regs = kJSCallerSaved | kCalleeSaved | ip.bit();
28 
29  const int kDoubleRegsSize = kDoubleSize * DwVfpRegister::kNumRegisters;
30  const int kFloatRegsSize = kFloatSize * SwVfpRegister::kNumRegisters;
31 
32  // Save all allocatable VFP registers before messing with them.
33  {
34  // We use a run-time check for VFP32DREGS.
35  CpuFeatureScope scope(masm(), VFP32DREGS,
36  CpuFeatureScope::kDontCheckSupported);
37  UseScratchRegisterScope temps(masm());
38  Register scratch = temps.Acquire();
39 
40  // Check CPU flags for number of registers, setting the Z condition flag.
41  __ CheckFor32DRegs(scratch);
42 
43  // Push registers d0-d15, and possibly d16-d31, on the stack.
44  // If d16-d31 are not pushed, decrease the stack pointer instead.
45  __ vstm(db_w, sp, d16, d31, ne);
46  __ sub(sp, sp, Operand(16 * kDoubleSize), LeaveCC, eq);
47  __ vstm(db_w, sp, d0, d15);
48 
49  // Push registers s0-s31 on the stack.
50  __ vstm(db_w, sp, s0, s31);
51  }
52 
53  // Push all 16 registers (needed to populate FrameDescription::registers_).
54  // TODO(1588) Note that using pc with stm is deprecated, so we should perhaps
55  // handle this a bit differently.
56  __ stm(db_w, sp, restored_regs | sp.bit() | lr.bit() | pc.bit());
57 
58  {
59  UseScratchRegisterScope temps(masm());
60  Register scratch = temps.Acquire();
61  __ mov(scratch, Operand(ExternalReference::Create(
62  IsolateAddressId::kCEntryFPAddress, isolate())));
63  __ str(fp, MemOperand(scratch));
64  }
65 
66  const int kSavedRegistersAreaSize =
67  (kNumberOfRegisters * kPointerSize) + kDoubleRegsSize + kFloatRegsSize;
68 
69  // Get the bailout id from the stack.
70  __ ldr(r2, MemOperand(sp, kSavedRegistersAreaSize));
71 
72  // Get the address of the location in the code object (r3) (return
73  // address for lazy deoptimization) and compute the fp-to-sp delta in
74  // register r4.
75  __ mov(r3, lr);
76  // Correct one word for bailout id.
77  __ add(r4, sp, Operand(kSavedRegistersAreaSize + (1 * kPointerSize)));
78  __ sub(r4, fp, r4);
79 
80  // Allocate a new deoptimizer object.
81  // Pass four arguments in r0 to r3 and fifth argument on stack.
82  __ PrepareCallCFunction(6);
83  __ mov(r0, Operand(0));
84  Label context_check;
85  __ ldr(r1, MemOperand(fp, CommonFrameConstants::kContextOrFrameTypeOffset));
86  __ JumpIfSmi(r1, &context_check);
87  __ ldr(r0, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
88  __ bind(&context_check);
89  __ mov(r1, Operand(static_cast<int>(deopt_kind())));
90  // r2: bailout id already loaded.
91  // r3: code address or 0 already loaded.
92  __ str(r4, MemOperand(sp, 0 * kPointerSize)); // Fp-to-sp delta.
93  __ mov(r5, Operand(ExternalReference::isolate_address(isolate())));
94  __ str(r5, MemOperand(sp, 1 * kPointerSize)); // Isolate.
95  // Call Deoptimizer::New().
96  {
97  AllowExternalCallThatCantCauseGC scope(masm());
98  __ CallCFunction(ExternalReference::new_deoptimizer_function(), 6);
99  }
100 
101  // Preserve "deoptimizer" object in register r0 and get the input
102  // frame descriptor pointer to r1 (deoptimizer->input_);
103  __ ldr(r1, MemOperand(r0, Deoptimizer::input_offset()));
104 
105  // Copy core registers into FrameDescription::registers_[kNumRegisters].
106  DCHECK_EQ(Register::kNumRegisters, kNumberOfRegisters);
107  for (int i = 0; i < kNumberOfRegisters; i++) {
108  int offset = (i * kPointerSize) + FrameDescription::registers_offset();
109  __ ldr(r2, MemOperand(sp, i * kPointerSize));
110  __ str(r2, MemOperand(r1, offset));
111  }
112 
113  // Copy VFP registers to
114  // double_registers_[DoubleRegister::kNumAllocatableRegisters]
115  int double_regs_offset = FrameDescription::double_registers_offset();
116  const RegisterConfiguration* config = RegisterConfiguration::Default();
117  for (int i = 0; i < config->num_allocatable_double_registers(); ++i) {
118  int code = config->GetAllocatableDoubleCode(i);
119  int dst_offset = code * kDoubleSize + double_regs_offset;
120  int src_offset =
121  code * kDoubleSize + kNumberOfRegisters * kPointerSize + kFloatRegsSize;
122  __ vldr(d0, sp, src_offset);
123  __ vstr(d0, r1, dst_offset);
124  }
125 
126  // Copy VFP registers to
127  // float_registers_[FloatRegister::kNumAllocatableRegisters]
128  int float_regs_offset = FrameDescription::float_registers_offset();
129  for (int i = 0; i < config->num_allocatable_float_registers(); ++i) {
130  int code = config->GetAllocatableFloatCode(i);
131  int dst_offset = code * kFloatSize + float_regs_offset;
132  int src_offset = code * kFloatSize + kNumberOfRegisters * kPointerSize;
133  __ ldr(r2, MemOperand(sp, src_offset));
134  __ str(r2, MemOperand(r1, dst_offset));
135  }
136 
137  // Remove the bailout id and the saved registers from the stack.
138  __ add(sp, sp, Operand(kSavedRegistersAreaSize + (1 * kPointerSize)));
139 
140  // Compute a pointer to the unwinding limit in register r2; that is
141  // the first stack slot not part of the input frame.
142  __ ldr(r2, MemOperand(r1, FrameDescription::frame_size_offset()));
143  __ add(r2, r2, sp);
144 
145  // Unwind the stack down to - but not including - the unwinding
146  // limit and copy the contents of the activation frame to the input
147  // frame description.
148  __ add(r3, r1, Operand(FrameDescription::frame_content_offset()));
149  Label pop_loop;
150  Label pop_loop_header;
151  __ b(&pop_loop_header);
152  __ bind(&pop_loop);
153  __ pop(r4);
154  __ str(r4, MemOperand(r3, 0));
155  __ add(r3, r3, Operand(sizeof(uint32_t)));
156  __ bind(&pop_loop_header);
157  __ cmp(r2, sp);
158  __ b(ne, &pop_loop);
159 
160  // Compute the output frame in the deoptimizer.
161  __ push(r0); // Preserve deoptimizer object across call.
162  // r0: deoptimizer object; r1: scratch.
163  __ PrepareCallCFunction(1);
164  // Call Deoptimizer::ComputeOutputFrames().
165  {
166  AllowExternalCallThatCantCauseGC scope(masm());
167  __ CallCFunction(ExternalReference::compute_output_frames_function(), 1);
168  }
169  __ pop(r0); // Restore deoptimizer object (class Deoptimizer).
170 
171  __ ldr(sp, MemOperand(r0, Deoptimizer::caller_frame_top_offset()));
172 
173  // Replace the current (input) frame with the output frames.
174  Label outer_push_loop, inner_push_loop,
175  outer_loop_header, inner_loop_header;
176  // Outer loop state: r4 = current "FrameDescription** output_",
177  // r1 = one past the last FrameDescription**.
178  __ ldr(r1, MemOperand(r0, Deoptimizer::output_count_offset()));
179  __ ldr(r4, MemOperand(r0, Deoptimizer::output_offset())); // r4 is output_.
180  __ add(r1, r4, Operand(r1, LSL, 2));
181  __ jmp(&outer_loop_header);
182  __ bind(&outer_push_loop);
183  // Inner loop state: r2 = current FrameDescription*, r3 = loop index.
184  __ ldr(r2, MemOperand(r4, 0)); // output_[ix]
185  __ ldr(r3, MemOperand(r2, FrameDescription::frame_size_offset()));
186  __ jmp(&inner_loop_header);
187  __ bind(&inner_push_loop);
188  __ sub(r3, r3, Operand(sizeof(uint32_t)));
189  __ add(r6, r2, Operand(r3));
190  __ ldr(r6, MemOperand(r6, FrameDescription::frame_content_offset()));
191  __ push(r6);
192  __ bind(&inner_loop_header);
193  __ cmp(r3, Operand::Zero());
194  __ b(ne, &inner_push_loop); // test for gt?
195  __ add(r4, r4, Operand(kPointerSize));
196  __ bind(&outer_loop_header);
197  __ cmp(r4, r1);
198  __ b(lt, &outer_push_loop);
199 
200  __ ldr(r1, MemOperand(r0, Deoptimizer::input_offset()));
201  for (int i = 0; i < config->num_allocatable_double_registers(); ++i) {
202  int code = config->GetAllocatableDoubleCode(i);
203  DwVfpRegister reg = DwVfpRegister::from_code(code);
204  int src_offset = code * kDoubleSize + double_regs_offset;
205  __ vldr(reg, r1, src_offset);
206  }
207 
208  // Push pc and continuation from the last output frame.
209  __ ldr(r6, MemOperand(r2, FrameDescription::pc_offset()));
210  __ push(r6);
211  __ ldr(r6, MemOperand(r2, FrameDescription::continuation_offset()));
212  __ push(r6);
213 
214  // Push the registers from the last output frame.
215  for (int i = kNumberOfRegisters - 1; i >= 0; i--) {
216  int offset = (i * kPointerSize) + FrameDescription::registers_offset();
217  __ ldr(r6, MemOperand(r2, offset));
218  __ push(r6);
219  }
220 
221  // Restore the registers from the stack.
222  __ ldm(ia_w, sp, restored_regs); // all but pc registers.
223 
224  // Remove sp, lr and pc.
225  __ Drop(3);
226  {
227  UseScratchRegisterScope temps(masm());
228  Register scratch = temps.Acquire();
229  __ pop(scratch); // get continuation, leave pc on stack
230  __ pop(lr);
231  __ Jump(scratch);
232  }
233  __ stop("Unreachable.");
234 }
235 
236 
237 void Deoptimizer::TableEntryGenerator::GeneratePrologue() {
238  // Create a sequence of deoptimization entries.
239  // Note that registers are still live when jumping to an entry.
240 
241  // We need to be able to generate immediates up to kMaxNumberOfEntries. On
242  // ARMv7, we can use movw (with a maximum immediate of 0xFFFF). On ARMv6, we
243  // need two instructions.
244  STATIC_ASSERT((kMaxNumberOfEntries - 1) <= 0xFFFF);
245  UseScratchRegisterScope temps(masm());
246  Register scratch = temps.Acquire();
247  if (CpuFeatures::IsSupported(ARMv7)) {
248  CpuFeatureScope scope(masm(), ARMv7);
249  Label done;
250  for (int i = 0; i < count(); i++) {
251  int start = masm()->pc_offset();
252  USE(start);
253  __ movw(scratch, i);
254  __ b(&done);
255  DCHECK_EQ(table_entry_size_, masm()->pc_offset() - start);
256  }
257  __ bind(&done);
258  } else {
259  // We want to keep table_entry_size_ == 8 (since this is the common case),
260  // but we need two instructions to load most immediates over 0xFF. To handle
261  // this, we set the low byte in the main table, and then set the high byte
262  // in a separate table if necessary.
263  Label high_fixes[256];
264  int high_fix_max = (count() - 1) >> 8;
265  DCHECK_GT(arraysize(high_fixes), static_cast<size_t>(high_fix_max));
266  for (int i = 0; i < count(); i++) {
267  int start = masm()->pc_offset();
268  USE(start);
269  __ mov(scratch, Operand(i & 0xFF)); // Set the low byte.
270  __ b(&high_fixes[i >> 8]); // Jump to the secondary table.
271  DCHECK_EQ(table_entry_size_, masm()->pc_offset() - start);
272  }
273  // Generate the secondary table, to set the high byte.
274  for (int high = 1; high <= high_fix_max; high++) {
275  __ bind(&high_fixes[high]);
276  __ orr(scratch, scratch, Operand(high << 8));
277  // If this isn't the last entry, emit a branch to the end of the table.
278  // The last entry can just fall through.
279  if (high < high_fix_max) __ b(&high_fixes[0]);
280  }
281  // Bind high_fixes[0] last, for indices like 0x00**. This case requires no
282  // fix-up, so for (common) small tables we can jump here, then just fall
283  // through with no additional branch.
284  __ bind(&high_fixes[0]);
285  }
286  __ push(scratch);
287 }
288 
289 bool Deoptimizer::PadTopOfStackRegister() { return false; }
290 
291 void FrameDescription::SetCallerPc(unsigned offset, intptr_t value) {
292  SetFrameSlot(offset, value);
293 }
294 
295 
296 void FrameDescription::SetCallerFp(unsigned offset, intptr_t value) {
297  SetFrameSlot(offset, value);
298 }
299 
300 
301 void FrameDescription::SetCallerConstantPool(unsigned offset, intptr_t value) {
302  // No embedded constant pool support.
303  UNREACHABLE();
304 }
305 
306 
307 #undef __
308 
309 } // namespace internal
310 } // namespace v8
Definition: libplatform.h:13