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.
Generate()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   __ InitializeRootRegister();
225 
226   // Remove sp, lr and pc.
227   __ Drop(3);
228   {
229     UseScratchRegisterScope temps(masm());
230     Register scratch = temps.Acquire();
231     __ pop(scratch);  // get continuation, leave pc on stack
232     __ pop(lr);
233     __ Jump(scratch);
234   }
235   __ stop("Unreachable.");
236 }
237 
238 
GeneratePrologue()239 void Deoptimizer::TableEntryGenerator::GeneratePrologue() {
240   // Create a sequence of deoptimization entries.
241   // Note that registers are still live when jumping to an entry.
242 
243   // We need to be able to generate immediates up to kMaxNumberOfEntries. On
244   // ARMv7, we can use movw (with a maximum immediate of 0xFFFF). On ARMv6, we
245   // need two instructions.
246   STATIC_ASSERT((kMaxNumberOfEntries - 1) <= 0xFFFF);
247   UseScratchRegisterScope temps(masm());
248   Register scratch = temps.Acquire();
249   if (CpuFeatures::IsSupported(ARMv7)) {
250     CpuFeatureScope scope(masm(), ARMv7);
251     Label done;
252     for (int i = 0; i < count(); i++) {
253       int start = masm()->pc_offset();
254       USE(start);
255       __ movw(scratch, i);
256       __ b(&done);
257       DCHECK_EQ(table_entry_size_, masm()->pc_offset() - start);
258     }
259     __ bind(&done);
260   } else {
261     // We want to keep table_entry_size_ == 8 (since this is the common case),
262     // but we need two instructions to load most immediates over 0xFF. To handle
263     // this, we set the low byte in the main table, and then set the high byte
264     // in a separate table if necessary.
265     Label high_fixes[256];
266     int high_fix_max = (count() - 1) >> 8;
267     DCHECK_GT(arraysize(high_fixes), static_cast<size_t>(high_fix_max));
268     for (int i = 0; i < count(); i++) {
269       int start = masm()->pc_offset();
270       USE(start);
271       __ mov(scratch, Operand(i & 0xFF));  // Set the low byte.
272       __ b(&high_fixes[i >> 8]);      // Jump to the secondary table.
273       DCHECK_EQ(table_entry_size_, masm()->pc_offset() - start);
274     }
275     // Generate the secondary table, to set the high byte.
276     for (int high = 1; high <= high_fix_max; high++) {
277       __ bind(&high_fixes[high]);
278       __ orr(scratch, scratch, Operand(high << 8));
279       // If this isn't the last entry, emit a branch to the end of the table.
280       // The last entry can just fall through.
281       if (high < high_fix_max) __ b(&high_fixes[0]);
282     }
283     // Bind high_fixes[0] last, for indices like 0x00**. This case requires no
284     // fix-up, so for (common) small tables we can jump here, then just fall
285     // through with no additional branch.
286     __ bind(&high_fixes[0]);
287   }
288   __ push(scratch);
289 }
290 
PadTopOfStackRegister()291 bool Deoptimizer::PadTopOfStackRegister() { return false; }
292 
SetCallerPc(unsigned offset,intptr_t value)293 void FrameDescription::SetCallerPc(unsigned offset, intptr_t value) {
294   SetFrameSlot(offset, value);
295 }
296 
297 
SetCallerFp(unsigned offset,intptr_t value)298 void FrameDescription::SetCallerFp(unsigned offset, intptr_t value) {
299   SetFrameSlot(offset, value);
300 }
301 
302 
SetCallerConstantPool(unsigned offset,intptr_t value)303 void FrameDescription::SetCallerConstantPool(unsigned offset, intptr_t value) {
304   // No embedded constant pool support.
305   UNREACHABLE();
306 }
307 
308 
309 #undef __
310 
311 }  // namespace internal
312 }  // namespace v8
313