V8 API Reference, 7.2.502.16 (for Deno 0.2.4)
deoptimizer-mips64.cc
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/assembler-inl.h"
6 #include "src/deoptimizer.h"
7 #include "src/register-configuration.h"
8 #include "src/safepoint-table.h"
9 
10 namespace v8 {
11 namespace internal {
12 
13 #define __ masm()->
14 
15 
16 // This code tries to be close to ia32 code so that any changes can be
17 // easily ported.
18 void Deoptimizer::TableEntryGenerator::Generate() {
19  GeneratePrologue();
20 
21  // Unlike on ARM we don't save all the registers, just the useful ones.
22  // For the rest, there are gaps on the stack, so the offsets remain the same.
23  const int kNumberOfRegisters = Register::kNumRegisters;
24 
25  RegList restored_regs = kJSCallerSaved | kCalleeSaved;
26  RegList saved_regs = restored_regs | sp.bit() | ra.bit();
27 
28  const int kDoubleRegsSize = kDoubleSize * DoubleRegister::kNumRegisters;
29  const int kFloatRegsSize = kFloatSize * FloatRegister::kNumRegisters;
30 
31  // Save all double FPU registers before messing with them.
32  __ Dsubu(sp, sp, Operand(kDoubleRegsSize));
33  const RegisterConfiguration* config = RegisterConfiguration::Default();
34  for (int i = 0; i < config->num_allocatable_double_registers(); ++i) {
35  int code = config->GetAllocatableDoubleCode(i);
36  const DoubleRegister fpu_reg = DoubleRegister::from_code(code);
37  int offset = code * kDoubleSize;
38  __ Sdc1(fpu_reg, MemOperand(sp, offset));
39  }
40 
41  // Save all float FPU registers before messing with them.
42  __ Dsubu(sp, sp, Operand(kFloatRegsSize));
43  for (int i = 0; i < config->num_allocatable_float_registers(); ++i) {
44  int code = config->GetAllocatableFloatCode(i);
45  const FloatRegister fpu_reg = FloatRegister::from_code(code);
46  int offset = code * kFloatSize;
47  __ Swc1(fpu_reg, MemOperand(sp, offset));
48  }
49 
50  // Push saved_regs (needed to populate FrameDescription::registers_).
51  // Leave gaps for other registers.
52  __ Dsubu(sp, sp, kNumberOfRegisters * kPointerSize);
53  for (int16_t i = kNumberOfRegisters - 1; i >= 0; i--) {
54  if ((saved_regs & (1 << i)) != 0) {
55  __ Sd(ToRegister(i), MemOperand(sp, kPointerSize * i));
56  }
57  }
58 
59  __ li(a2, Operand(ExternalReference::Create(
60  IsolateAddressId::kCEntryFPAddress, isolate())));
61  __ Sd(fp, MemOperand(a2));
62 
63  const int kSavedRegistersAreaSize =
64  (kNumberOfRegisters * kPointerSize) + kDoubleRegsSize + kFloatRegsSize;
65 
66  // Get the bailout id from the stack.
67  __ Ld(a2, MemOperand(sp, kSavedRegistersAreaSize));
68 
69  // Get the address of the location in the code object (a3) (return
70  // address for lazy deoptimization) and compute the fp-to-sp delta in
71  // register a4.
72  __ mov(a3, ra);
73  // Correct one word for bailout id.
74  __ Daddu(a4, sp, Operand(kSavedRegistersAreaSize + (1 * kPointerSize)));
75 
76  __ Dsubu(a4, fp, a4);
77 
78  // Allocate a new deoptimizer object.
79  __ PrepareCallCFunction(6, a5);
80  // Pass six arguments, according to n64 ABI.
81  __ mov(a0, zero_reg);
82  Label context_check;
83  __ Ld(a1, MemOperand(fp, CommonFrameConstants::kContextOrFrameTypeOffset));
84  __ JumpIfSmi(a1, &context_check);
85  __ Ld(a0, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
86  __ bind(&context_check);
87  __ li(a1, Operand(static_cast<int>(deopt_kind())));
88  // a2: bailout id already loaded.
89  // a3: code address or 0 already loaded.
90  // a4: already has fp-to-sp delta.
91  __ li(a5, Operand(ExternalReference::isolate_address(isolate())));
92 
93  // Call Deoptimizer::New().
94  {
95  AllowExternalCallThatCantCauseGC scope(masm());
96  __ CallCFunction(ExternalReference::new_deoptimizer_function(), 6);
97  }
98 
99  // Preserve "deoptimizer" object in register v0 and get the input
100  // frame descriptor pointer to a1 (deoptimizer->input_);
101  // Move deopt-obj to a0 for call to Deoptimizer::ComputeOutputFrames() below.
102  __ mov(a0, v0);
103  __ Ld(a1, MemOperand(v0, 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  if ((saved_regs & (1 << i)) != 0) {
110  __ Ld(a2, MemOperand(sp, i * kPointerSize));
111  __ Sd(a2, MemOperand(a1, offset));
112  } else if (FLAG_debug_code) {
113  __ li(a2, kDebugZapValue);
114  __ Sd(a2, MemOperand(a1, offset));
115  }
116  }
117 
118  int double_regs_offset = FrameDescription::double_registers_offset();
119  // Copy FPU registers to
120  // double_registers_[DoubleRegister::kNumAllocatableRegisters]
121  for (int i = 0; i < config->num_allocatable_double_registers(); ++i) {
122  int code = config->GetAllocatableDoubleCode(i);
123  int dst_offset = code * kDoubleSize + double_regs_offset;
124  int src_offset =
125  code * kDoubleSize + kNumberOfRegisters * kPointerSize + kFloatRegsSize;
126  __ Ldc1(f0, MemOperand(sp, src_offset));
127  __ Sdc1(f0, MemOperand(a1, dst_offset));
128  }
129 
130  int float_regs_offset = FrameDescription::float_registers_offset();
131  // Copy FPU registers to
132  // float_registers_[FloatRegister::kNumAllocatableRegisters]
133  for (int i = 0; i < config->num_allocatable_float_registers(); ++i) {
134  int code = config->GetAllocatableFloatCode(i);
135  int dst_offset = code * kFloatSize + float_regs_offset;
136  int src_offset = code * kFloatSize + kNumberOfRegisters * kPointerSize;
137  __ Lwc1(f0, MemOperand(sp, src_offset));
138  __ Swc1(f0, MemOperand(a1, dst_offset));
139  }
140 
141  // Remove the bailout id and the saved registers from the stack.
142  __ Daddu(sp, sp, Operand(kSavedRegistersAreaSize + (1 * kPointerSize)));
143 
144  // Compute a pointer to the unwinding limit in register a2; that is
145  // the first stack slot not part of the input frame.
146  __ Ld(a2, MemOperand(a1, FrameDescription::frame_size_offset()));
147  __ Daddu(a2, a2, sp);
148 
149  // Unwind the stack down to - but not including - the unwinding
150  // limit and copy the contents of the activation frame to the input
151  // frame description.
152  __ Daddu(a3, a1, Operand(FrameDescription::frame_content_offset()));
153  Label pop_loop;
154  Label pop_loop_header;
155  __ BranchShort(&pop_loop_header);
156  __ bind(&pop_loop);
157  __ pop(a4);
158  __ Sd(a4, MemOperand(a3, 0));
159  __ daddiu(a3, a3, sizeof(uint64_t));
160  __ bind(&pop_loop_header);
161  __ BranchShort(&pop_loop, ne, a2, Operand(sp));
162  // Compute the output frame in the deoptimizer.
163  __ push(a0); // Preserve deoptimizer object across call.
164  // a0: deoptimizer object; a1: scratch.
165  __ PrepareCallCFunction(1, a1);
166  // Call Deoptimizer::ComputeOutputFrames().
167  {
168  AllowExternalCallThatCantCauseGC scope(masm());
169  __ CallCFunction(ExternalReference::compute_output_frames_function(), 1);
170  }
171  __ pop(a0); // Restore deoptimizer object (class Deoptimizer).
172 
173  __ Ld(sp, MemOperand(a0, Deoptimizer::caller_frame_top_offset()));
174 
175  // Replace the current (input) frame with the output frames.
176  Label outer_push_loop, inner_push_loop,
177  outer_loop_header, inner_loop_header;
178  // Outer loop state: a4 = current "FrameDescription** output_",
179  // a1 = one past the last FrameDescription**.
180  __ Lw(a1, MemOperand(a0, Deoptimizer::output_count_offset()));
181  __ Ld(a4, MemOperand(a0, Deoptimizer::output_offset())); // a4 is output_.
182  __ Dlsa(a1, a4, a1, kPointerSizeLog2);
183  __ BranchShort(&outer_loop_header);
184  __ bind(&outer_push_loop);
185  // Inner loop state: a2 = current FrameDescription*, a3 = loop index.
186  __ Ld(a2, MemOperand(a4, 0)); // output_[ix]
187  __ Ld(a3, MemOperand(a2, FrameDescription::frame_size_offset()));
188  __ BranchShort(&inner_loop_header);
189  __ bind(&inner_push_loop);
190  __ Dsubu(a3, a3, Operand(sizeof(uint64_t)));
191  __ Daddu(a6, a2, Operand(a3));
192  __ Ld(a7, MemOperand(a6, FrameDescription::frame_content_offset()));
193  __ push(a7);
194  __ bind(&inner_loop_header);
195  __ BranchShort(&inner_push_loop, ne, a3, Operand(zero_reg));
196 
197  __ Daddu(a4, a4, Operand(kPointerSize));
198  __ bind(&outer_loop_header);
199  __ BranchShort(&outer_push_loop, lt, a4, Operand(a1));
200 
201  __ Ld(a1, MemOperand(a0, Deoptimizer::input_offset()));
202  for (int i = 0; i < config->num_allocatable_double_registers(); ++i) {
203  int code = config->GetAllocatableDoubleCode(i);
204  const DoubleRegister fpu_reg = DoubleRegister::from_code(code);
205  int src_offset = code * kDoubleSize + double_regs_offset;
206  __ Ldc1(fpu_reg, MemOperand(a1, src_offset));
207  }
208 
209  // Push pc and continuation from the last output frame.
210  __ Ld(a6, MemOperand(a2, FrameDescription::pc_offset()));
211  __ push(a6);
212  __ Ld(a6, MemOperand(a2, FrameDescription::continuation_offset()));
213  __ push(a6);
214 
215  // Technically restoring 'at' should work unless zero_reg is also restored
216  // but it's safer to check for this.
217  DCHECK(!(at.bit() & restored_regs));
218  // Restore the registers from the last output frame.
219  __ mov(at, a2);
220  for (int i = kNumberOfRegisters - 1; i >= 0; i--) {
221  int offset = (i * kPointerSize) + FrameDescription::registers_offset();
222  if ((restored_regs & (1 << i)) != 0) {
223  __ Ld(ToRegister(i), MemOperand(at, offset));
224  }
225  }
226 
227  __ pop(at); // Get continuation, leave pc on stack.
228  __ pop(ra);
229  __ Jump(at);
230  __ stop("Unreachable.");
231 }
232 
233 
234 // Maximum size of a table entry generated below.
235 #ifdef _MIPS_ARCH_MIPS64R6
236 const int Deoptimizer::table_entry_size_ = 2 * kInstrSize;
237 #else
238 const int Deoptimizer::table_entry_size_ = 3 * kInstrSize;
239 #endif
240 
241 void Deoptimizer::TableEntryGenerator::GeneratePrologue() {
242  Assembler::BlockTrampolinePoolScope block_trampoline_pool(masm());
243 
244  // Create a sequence of deoptimization entries.
245  // Note that registers are still live when jumping to an entry.
246  Label table_start, done, trampoline_jump;
247  __ bind(&table_start);
248 #ifdef _MIPS_ARCH_MIPS64R6
249  int kMaxEntriesBranchReach =
250  (1 << (kImm26Bits - 2)) / (table_entry_size_ / kInstrSize);
251 #else
252  int kMaxEntriesBranchReach =
253  (1 << (kImm16Bits - 2)) / (table_entry_size_ / kInstrSize);
254 #endif
255 
256  if (count() <= kMaxEntriesBranchReach) {
257  // Common case.
258  for (int i = 0; i < count(); i++) {
259  Label start;
260  __ bind(&start);
261  DCHECK(is_int16(i));
262  if (kArchVariant == kMips64r6) {
263  __ li(kScratchReg, i);
264  __ BranchShort(PROTECT, &done);
265  } else {
266  __ BranchShort(USE_DELAY_SLOT, &done); // Expose delay slot.
267  __ li(kScratchReg, i); // In the delay slot.
268  __ nop();
269  }
270 
271  DCHECK_EQ(table_entry_size_, masm()->SizeOfCodeGeneratedSince(&start));
272  }
273 
274  DCHECK_EQ(masm()->SizeOfCodeGeneratedSince(&table_start),
275  count() * table_entry_size_);
276  __ bind(&done);
277  __ Push(kScratchReg);
278  } else {
279  DCHECK_NE(kArchVariant, kMips64r6);
280  // Uncommon case, the branch cannot reach.
281  // Create mini trampoline to reach the end of the table
282  for (int i = 0, j = 0; i < count(); i++, j++) {
283  Label start;
284  __ bind(&start);
285  DCHECK(is_int16(i));
286  if (j >= kMaxEntriesBranchReach) {
287  j = 0;
288  __ li(kScratchReg, i);
289  __ bind(&trampoline_jump);
290  trampoline_jump = Label();
291  __ BranchShort(USE_DELAY_SLOT, &trampoline_jump);
292  __ nop();
293  } else {
294  __ BranchShort(USE_DELAY_SLOT, &trampoline_jump); // Expose delay slot.
295  __ li(kScratchReg, i); // In the delay slot.
296  __ nop();
297  }
298  DCHECK_EQ(table_entry_size_, masm()->SizeOfCodeGeneratedSince(&start));
299  }
300 
301  DCHECK_EQ(masm()->SizeOfCodeGeneratedSince(&table_start),
302  count() * table_entry_size_);
303  __ bind(&trampoline_jump);
304  __ Push(kScratchReg);
305  }
306 }
307 
308 bool Deoptimizer::PadTopOfStackRegister() { return false; }
309 
310 void FrameDescription::SetCallerPc(unsigned offset, intptr_t value) {
311  SetFrameSlot(offset, value);
312 }
313 
314 
315 void FrameDescription::SetCallerFp(unsigned offset, intptr_t value) {
316  SetFrameSlot(offset, value);
317 }
318 
319 
320 void FrameDescription::SetCallerConstantPool(unsigned offset, intptr_t value) {
321  // No embedded constant pool support.
322  UNREACHABLE();
323 }
324 
325 
326 #undef __
327 
328 
329 } // namespace internal
330 } // namespace v8
Definition: libplatform.h:13