V8 API Reference, 7.2.502.16 (for Deno 0.2.4)
deoptimizer-ppc.cc
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/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 const int Deoptimizer::table_entry_size_ = 8;
14 
15 #define __ masm()->
16 
17 // This code tries to be close to ia32 code so that any changes can be
18 // easily ported.
19 void Deoptimizer::TableEntryGenerator::Generate() {
20  GeneratePrologue();
21 
22  // Unlike on ARM we don't save all the registers, just the useful ones.
23  // For the rest, there are gaps on the stack, so the offsets remain the same.
24  const int kNumberOfRegisters = Register::kNumRegisters;
25 
26  RegList restored_regs = kJSCallerSaved | kCalleeSaved;
27  RegList saved_regs = restored_regs | sp.bit();
28 
29  const int kDoubleRegsSize = kDoubleSize * DoubleRegister::kNumRegisters;
30  const int kFloatRegsSize = kFloatSize * FloatRegister::kNumRegisters;
31 
32  // Save all double registers before messing with them.
33  __ subi(sp, sp, Operand(kDoubleRegsSize));
34  const RegisterConfiguration* config = RegisterConfiguration::Default();
35  for (int i = 0; i < config->num_allocatable_double_registers(); ++i) {
36  int code = config->GetAllocatableDoubleCode(i);
37  const DoubleRegister dreg = DoubleRegister::from_code(code);
38  int offset = code * kDoubleSize;
39  __ stfd(dreg, MemOperand(sp, offset));
40  }
41  // Save all float registers before messing with them.
42  __ subi(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 freg = FloatRegister::from_code(code);
46  int offset = code * kFloatSize;
47  __ stfs(freg, MemOperand(sp, offset));
48  }
49 
50  // Push saved_regs (needed to populate FrameDescription::registers_).
51  // Leave gaps for other registers.
52  __ subi(sp, sp, Operand(kNumberOfRegisters * kPointerSize));
53  for (int16_t i = kNumberOfRegisters - 1; i >= 0; i--) {
54  if ((saved_regs & (1 << i)) != 0) {
55  __ StoreP(ToRegister(i), MemOperand(sp, kPointerSize * i));
56  }
57  }
58 
59  __ mov(ip, Operand(ExternalReference::Create(
60  IsolateAddressId::kCEntryFPAddress, isolate())));
61  __ StoreP(fp, MemOperand(ip));
62 
63  const int kSavedRegistersAreaSize =
64  (kNumberOfRegisters * kPointerSize) + kDoubleRegsSize + kFloatRegsSize;
65 
66  // Get the bailout id from the stack.
67  __ LoadP(r5, MemOperand(sp, kSavedRegistersAreaSize));
68 
69  // Get the address of the location in the code object (r6) (return
70  // address for lazy deoptimization) and compute the fp-to-sp delta in
71  // register r7.
72  __ mflr(r6);
73  // Correct one word for bailout id.
74  __ addi(r7, sp, Operand(kSavedRegistersAreaSize + (1 * kPointerSize)));
75  __ sub(r7, fp, r7);
76 
77  // Allocate a new deoptimizer object.
78  // Pass six arguments in r3 to r8.
79  __ PrepareCallCFunction(6, r8);
80  __ li(r3, Operand::Zero());
81  Label context_check;
82  __ LoadP(r4, MemOperand(fp, CommonFrameConstants::kContextOrFrameTypeOffset));
83  __ JumpIfSmi(r4, &context_check);
84  __ LoadP(r3, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
85  __ bind(&context_check);
86  __ li(r4, Operand(static_cast<int>(deopt_kind())));
87  // r5: bailout id already loaded.
88  // r6: code address or 0 already loaded.
89  // r7: Fp-to-sp delta.
90  __ mov(r8, Operand(ExternalReference::isolate_address(isolate())));
91  // Call Deoptimizer::New().
92  {
93  AllowExternalCallThatCantCauseGC scope(masm());
94  __ CallCFunction(ExternalReference::new_deoptimizer_function(), 6);
95  }
96 
97  // Preserve "deoptimizer" object in register r3 and get the input
98  // frame descriptor pointer to r4 (deoptimizer->input_);
99  __ LoadP(r4, MemOperand(r3, Deoptimizer::input_offset()));
100 
101  // Copy core registers into FrameDescription::registers_[kNumRegisters].
102  DCHECK_EQ(Register::kNumRegisters, kNumberOfRegisters);
103  for (int i = 0; i < kNumberOfRegisters; i++) {
104  int offset = (i * kPointerSize) + FrameDescription::registers_offset();
105  __ LoadP(r5, MemOperand(sp, i * kPointerSize));
106  __ StoreP(r5, MemOperand(r4, offset));
107  }
108 
109  int double_regs_offset = FrameDescription::double_registers_offset();
110  // Copy double registers to
111  // double_registers_[DoubleRegister::kNumRegisters]
112  for (int i = 0; i < config->num_allocatable_double_registers(); ++i) {
113  int code = config->GetAllocatableDoubleCode(i);
114  int dst_offset = code * kDoubleSize + double_regs_offset;
115  int src_offset =
116  code * kDoubleSize + kNumberOfRegisters * kPointerSize + kFloatRegsSize;
117  __ lfd(d0, MemOperand(sp, src_offset));
118  __ stfd(d0, MemOperand(r4, dst_offset));
119  }
120  int float_regs_offset = FrameDescription::float_registers_offset();
121  // Copy float registers to
122  // float_registers_[FloatRegister::kNumRegisters]
123  for (int i = 0; i < config->num_allocatable_float_registers(); ++i) {
124  int code = config->GetAllocatableFloatCode(i);
125  int dst_offset = code * kFloatSize + float_regs_offset;
126  int src_offset = code * kFloatSize + kNumberOfRegisters * kPointerSize;
127  __ lfs(d0, MemOperand(sp, src_offset));
128  __ stfs(d0, MemOperand(r4, dst_offset));
129  }
130  // Remove the bailout id and the saved registers from the stack.
131  __ addi(sp, sp, Operand(kSavedRegistersAreaSize + (1 * kPointerSize)));
132 
133  // Compute a pointer to the unwinding limit in register r5; that is
134  // the first stack slot not part of the input frame.
135  __ LoadP(r5, MemOperand(r4, FrameDescription::frame_size_offset()));
136  __ add(r5, r5, sp);
137 
138  // Unwind the stack down to - but not including - the unwinding
139  // limit and copy the contents of the activation frame to the input
140  // frame description.
141  __ addi(r6, r4, Operand(FrameDescription::frame_content_offset()));
142  Label pop_loop;
143  Label pop_loop_header;
144  __ b(&pop_loop_header);
145  __ bind(&pop_loop);
146  __ pop(r7);
147  __ StoreP(r7, MemOperand(r6, 0));
148  __ addi(r6, r6, Operand(kPointerSize));
149  __ bind(&pop_loop_header);
150  __ cmp(r5, sp);
151  __ bne(&pop_loop);
152 
153  // Compute the output frame in the deoptimizer.
154  __ push(r3); // Preserve deoptimizer object across call.
155  // r3: deoptimizer object; r4: scratch.
156  __ PrepareCallCFunction(1, r4);
157  // Call Deoptimizer::ComputeOutputFrames().
158  {
159  AllowExternalCallThatCantCauseGC scope(masm());
160  __ CallCFunction(ExternalReference::compute_output_frames_function(), 1);
161  }
162  __ pop(r3); // Restore deoptimizer object (class Deoptimizer).
163 
164  __ LoadP(sp, MemOperand(r3, Deoptimizer::caller_frame_top_offset()));
165 
166  // Replace the current (input) frame with the output frames.
167  Label outer_push_loop, inner_push_loop, outer_loop_header, inner_loop_header;
168  // Outer loop state: r7 = current "FrameDescription** output_",
169  // r4 = one past the last FrameDescription**.
170  __ lwz(r4, MemOperand(r3, Deoptimizer::output_count_offset()));
171  __ LoadP(r7, MemOperand(r3, Deoptimizer::output_offset())); // r7 is output_.
172  __ ShiftLeftImm(r4, r4, Operand(kPointerSizeLog2));
173  __ add(r4, r7, r4);
174  __ b(&outer_loop_header);
175 
176  __ bind(&outer_push_loop);
177  // Inner loop state: r5 = current FrameDescription*, r6 = loop index.
178  __ LoadP(r5, MemOperand(r7, 0)); // output_[ix]
179  __ LoadP(r6, MemOperand(r5, FrameDescription::frame_size_offset()));
180  __ b(&inner_loop_header);
181 
182  __ bind(&inner_push_loop);
183  __ addi(r6, r6, Operand(-sizeof(intptr_t)));
184  __ add(r9, r5, r6);
185  __ LoadP(r9, MemOperand(r9, FrameDescription::frame_content_offset()));
186  __ push(r9);
187 
188  __ bind(&inner_loop_header);
189  __ cmpi(r6, Operand::Zero());
190  __ bne(&inner_push_loop); // test for gt?
191 
192  __ addi(r7, r7, Operand(kPointerSize));
193  __ bind(&outer_loop_header);
194  __ cmp(r7, r4);
195  __ blt(&outer_push_loop);
196 
197  __ LoadP(r4, MemOperand(r3, Deoptimizer::input_offset()));
198  for (int i = 0; i < config->num_allocatable_double_registers(); ++i) {
199  int code = config->GetAllocatableDoubleCode(i);
200  const DoubleRegister dreg = DoubleRegister::from_code(code);
201  int src_offset = code * kDoubleSize + double_regs_offset;
202  __ lfd(dreg, MemOperand(r4, src_offset));
203  }
204 
205  // Push pc, and continuation from the last output frame.
206  __ LoadP(r9, MemOperand(r5, FrameDescription::pc_offset()));
207  __ push(r9);
208  __ LoadP(r9, MemOperand(r5, FrameDescription::continuation_offset()));
209  __ push(r9);
210 
211  // Restore the registers from the last output frame.
212  DCHECK(!(ip.bit() & restored_regs));
213  __ mr(ip, r5);
214  for (int i = kNumberOfRegisters - 1; i >= 0; i--) {
215  int offset = (i * kPointerSize) + FrameDescription::registers_offset();
216  if ((restored_regs & (1 << i)) != 0) {
217  __ LoadP(ToRegister(i), MemOperand(ip, offset));
218  }
219  }
220 
221  __ pop(ip); // get continuation, leave pc on stack
222  __ pop(r0);
223  __ mtlr(r0);
224  __ Jump(ip);
225  __ stop("Unreachable.");
226 }
227 
228 
229 void Deoptimizer::TableEntryGenerator::GeneratePrologue() {
230  Assembler::BlockTrampolinePoolScope block_trampoline_pool(masm());
231 
232  // Create a sequence of deoptimization entries.
233  // Note that registers are still live when jumping to an entry.
234  Label done;
235  for (int i = 0; i < count(); i++) {
236  int start = masm()->pc_offset();
237  USE(start);
238  __ li(ip, Operand(i));
239  __ b(&done);
240  DCHECK(masm()->pc_offset() - start == table_entry_size_);
241  }
242  __ bind(&done);
243  __ push(ip);
244 }
245 
246 bool Deoptimizer::PadTopOfStackRegister() { return false; }
247 
248 void FrameDescription::SetCallerPc(unsigned offset, intptr_t value) {
249  SetFrameSlot(offset, value);
250 }
251 
252 
253 void FrameDescription::SetCallerFp(unsigned offset, intptr_t value) {
254  SetFrameSlot(offset, value);
255 }
256 
257 
258 void FrameDescription::SetCallerConstantPool(unsigned offset, intptr_t value) {
259  DCHECK(FLAG_enable_embedded_constant_pool);
260  SetFrameSlot(offset, value);
261 }
262 
263 
264 #undef __
265 } // namespace internal
266 } // namespace v8
Definition: libplatform.h:13