1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided
11 // with the distribution.
12 // * Neither the name of Google Inc. nor the names of its
13 // contributors may be used to endorse or promote products derived
14 // from this software without specific prior written permission.
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #include "deoptimizer.h"
32 #include "full-codegen.h"
33 #include "safepoint-table.h"
38 const int Deoptimizer::table_entry_size_ = 12;
41 int Deoptimizer::patch_size() {
42 const int kCallInstructionSizeInWords = 3;
43 return kCallInstructionSizeInWords * Assembler::kInstrSize;
47 void Deoptimizer::PatchCodeForDeoptimization(Isolate* isolate, Code* code) {
48 Address code_start_address = code->instruction_start();
49 // Invalidate the relocation information, as it will become invalid by the
50 // code patching below, and is not needed any more.
51 code->InvalidateRelocation();
53 // For each LLazyBailout instruction insert a call to the corresponding
54 // deoptimization entry.
55 DeoptimizationInputData* deopt_data =
56 DeoptimizationInputData::cast(code->deoptimization_data());
58 Address prev_call_address = NULL;
60 for (int i = 0; i < deopt_data->DeoptCount(); i++) {
61 if (deopt_data->Pc(i)->value() == -1) continue;
62 Address call_address = code_start_address + deopt_data->Pc(i)->value();
63 Address deopt_entry = GetDeoptimizationEntry(isolate, i, LAZY);
64 // We need calls to have a predictable size in the unoptimized code, but
65 // this is optimized code, so we don't have to have a predictable size.
66 int call_size_in_bytes =
67 MacroAssembler::CallSizeNotPredictableCodeSize(deopt_entry,
69 int call_size_in_words = call_size_in_bytes / Assembler::kInstrSize;
70 ASSERT(call_size_in_bytes % Assembler::kInstrSize == 0);
71 ASSERT(call_size_in_bytes <= patch_size());
72 CodePatcher patcher(call_address, call_size_in_words);
73 patcher.masm()->Call(deopt_entry, RelocInfo::NONE32);
74 ASSERT(prev_call_address == NULL ||
75 call_address >= prev_call_address + patch_size());
76 ASSERT(call_address + patch_size() <= code->instruction_end());
78 prev_call_address = call_address;
84 void Deoptimizer::FillInputFrame(Address tos, JavaScriptFrame* frame) {
85 // Set the register values. The values are not important as there are no
86 // callee saved registers in JavaScript frames, so all registers are
87 // spilled. Registers fp and sp are set to the correct values though.
89 for (int i = 0; i < Register::kNumRegisters; i++) {
90 input_->SetRegister(i, i * 4);
92 input_->SetRegister(sp.code(), reinterpret_cast<intptr_t>(frame->sp()));
93 input_->SetRegister(fp.code(), reinterpret_cast<intptr_t>(frame->fp()));
94 for (int i = 0; i < DoubleRegister::NumAllocatableRegisters(); i++) {
95 input_->SetDoubleRegister(i, 0.0);
98 // Fill the frame content from the actual data on the frame.
99 for (unsigned i = 0; i < input_->GetFrameSize(); i += kPointerSize) {
100 input_->SetFrameSlot(i, Memory::uint32_at(tos + i));
105 void Deoptimizer::SetPlatformCompiledStubRegisters(
106 FrameDescription* output_frame, CodeStubInterfaceDescriptor* descriptor) {
107 ApiFunction function(descriptor->deoptimization_handler_);
108 ExternalReference xref(&function, ExternalReference::BUILTIN_CALL, isolate_);
109 intptr_t handler = reinterpret_cast<intptr_t>(xref.address());
110 int params = descriptor->GetHandlerParameterCount();
111 output_frame->SetRegister(r0.code(), params);
112 output_frame->SetRegister(r1.code(), handler);
116 void Deoptimizer::CopyDoubleRegisters(FrameDescription* output_frame) {
117 for (int i = 0; i < DwVfpRegister::kMaxNumRegisters; ++i) {
118 double double_value = input_->GetDoubleRegister(i);
119 output_frame->SetDoubleRegister(i, double_value);
124 bool Deoptimizer::HasAlignmentPadding(JSFunction* function) {
125 // There is no dynamic alignment padding on ARM in the input frame.
130 Code* Deoptimizer::NotifyStubFailureBuiltin() {
131 return isolate_->builtins()->builtin(Builtins::kNotifyStubFailureSaveDoubles);
137 // This code tries to be close to ia32 code so that any changes can be
139 void Deoptimizer::EntryGenerator::Generate() {
142 // Save all general purpose registers before messing with them.
143 const int kNumberOfRegisters = Register::kNumRegisters;
145 // Everything but pc, lr and ip which will be saved but not restored.
146 RegList restored_regs = kJSCallerSaved | kCalleeSaved | ip.bit();
148 const int kDoubleRegsSize =
149 kDoubleSize * DwVfpRegister::kMaxNumAllocatableRegisters;
151 // Save all allocatable VFP registers before messing with them.
152 ASSERT(kDoubleRegZero.code() == 14);
153 ASSERT(kScratchDoubleReg.code() == 15);
155 // Check CPU flags for number of registers, setting the Z condition flag.
156 __ CheckFor32DRegs(ip);
158 // Push registers d0-d13, and possibly d16-d31, on the stack.
159 // If d16-d31 are not pushed, decrease the stack pointer instead.
160 __ vstm(db_w, sp, d16, d31, ne);
161 __ sub(sp, sp, Operand(16 * kDoubleSize), LeaveCC, eq);
162 __ vstm(db_w, sp, d0, d13);
164 // Push all 16 registers (needed to populate FrameDescription::registers_).
165 // TODO(1588) Note that using pc with stm is deprecated, so we should perhaps
166 // handle this a bit differently.
167 __ stm(db_w, sp, restored_regs | sp.bit() | lr.bit() | pc.bit());
169 const int kSavedRegistersAreaSize =
170 (kNumberOfRegisters * kPointerSize) + kDoubleRegsSize;
172 // Get the bailout id from the stack.
173 __ ldr(r2, MemOperand(sp, kSavedRegistersAreaSize));
175 // Get the address of the location in the code object (r3) (return
176 // address for lazy deoptimization) and compute the fp-to-sp delta in
179 // Correct one word for bailout id.
180 __ add(r4, sp, Operand(kSavedRegistersAreaSize + (1 * kPointerSize)));
183 // Allocate a new deoptimizer object.
184 // Pass four arguments in r0 to r3 and fifth argument on stack.
185 __ PrepareCallCFunction(6, r5);
186 __ ldr(r0, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
187 __ mov(r1, Operand(type())); // bailout type,
188 // r2: bailout id already loaded.
189 // r3: code address or 0 already loaded.
190 __ str(r4, MemOperand(sp, 0 * kPointerSize)); // Fp-to-sp delta.
191 __ mov(r5, Operand(ExternalReference::isolate_address(isolate())));
192 __ str(r5, MemOperand(sp, 1 * kPointerSize)); // Isolate.
193 // Call Deoptimizer::New().
195 AllowExternalCallThatCantCauseGC scope(masm());
196 __ CallCFunction(ExternalReference::new_deoptimizer_function(isolate()), 6);
199 // Preserve "deoptimizer" object in register r0 and get the input
200 // frame descriptor pointer to r1 (deoptimizer->input_);
201 __ ldr(r1, MemOperand(r0, Deoptimizer::input_offset()));
203 // Copy core registers into FrameDescription::registers_[kNumRegisters].
204 ASSERT(Register::kNumRegisters == kNumberOfRegisters);
205 for (int i = 0; i < kNumberOfRegisters; i++) {
206 int offset = (i * kPointerSize) + FrameDescription::registers_offset();
207 __ ldr(r2, MemOperand(sp, i * kPointerSize));
208 __ str(r2, MemOperand(r1, offset));
211 // Copy VFP registers to
212 // double_registers_[DoubleRegister::kMaxNumAllocatableRegisters]
213 int double_regs_offset = FrameDescription::double_registers_offset();
214 for (int i = 0; i < DwVfpRegister::kMaxNumAllocatableRegisters; ++i) {
215 int dst_offset = i * kDoubleSize + double_regs_offset;
216 int src_offset = i * kDoubleSize + kNumberOfRegisters * kPointerSize;
217 __ vldr(d0, sp, src_offset);
218 __ vstr(d0, r1, dst_offset);
221 // Remove the bailout id and the saved registers from the stack.
222 __ add(sp, sp, Operand(kSavedRegistersAreaSize + (1 * kPointerSize)));
224 // Compute a pointer to the unwinding limit in register r2; that is
225 // the first stack slot not part of the input frame.
226 __ ldr(r2, MemOperand(r1, FrameDescription::frame_size_offset()));
229 // Unwind the stack down to - but not including - the unwinding
230 // limit and copy the contents of the activation frame to the input
231 // frame description.
232 __ add(r3, r1, Operand(FrameDescription::frame_content_offset()));
234 Label pop_loop_header;
235 __ b(&pop_loop_header);
238 __ str(r4, MemOperand(r3, 0));
239 __ add(r3, r3, Operand(sizeof(uint32_t)));
240 __ bind(&pop_loop_header);
244 // Compute the output frame in the deoptimizer.
245 __ push(r0); // Preserve deoptimizer object across call.
246 // r0: deoptimizer object; r1: scratch.
247 __ PrepareCallCFunction(1, r1);
248 // Call Deoptimizer::ComputeOutputFrames().
250 AllowExternalCallThatCantCauseGC scope(masm());
252 ExternalReference::compute_output_frames_function(isolate()), 1);
254 __ pop(r0); // Restore deoptimizer object (class Deoptimizer).
256 // Replace the current (input) frame with the output frames.
257 Label outer_push_loop, inner_push_loop,
258 outer_loop_header, inner_loop_header;
259 // Outer loop state: r4 = current "FrameDescription** output_",
260 // r1 = one past the last FrameDescription**.
261 __ ldr(r1, MemOperand(r0, Deoptimizer::output_count_offset()));
262 __ ldr(r4, MemOperand(r0, Deoptimizer::output_offset())); // r4 is output_.
263 __ add(r1, r4, Operand(r1, LSL, 2));
264 __ jmp(&outer_loop_header);
265 __ bind(&outer_push_loop);
266 // Inner loop state: r2 = current FrameDescription*, r3 = loop index.
267 __ ldr(r2, MemOperand(r4, 0)); // output_[ix]
268 __ ldr(r3, MemOperand(r2, FrameDescription::frame_size_offset()));
269 __ jmp(&inner_loop_header);
270 __ bind(&inner_push_loop);
271 __ sub(r3, r3, Operand(sizeof(uint32_t)));
272 __ add(r6, r2, Operand(r3));
273 __ ldr(r6, MemOperand(r6, FrameDescription::frame_content_offset()));
275 __ bind(&inner_loop_header);
276 __ cmp(r3, Operand::Zero());
277 __ b(ne, &inner_push_loop); // test for gt?
278 __ add(r4, r4, Operand(kPointerSize));
279 __ bind(&outer_loop_header);
281 __ b(lt, &outer_push_loop);
283 // Check CPU flags for number of registers, setting the Z condition flag.
284 __ CheckFor32DRegs(ip);
286 __ ldr(r1, MemOperand(r0, Deoptimizer::input_offset()));
287 int src_offset = FrameDescription::double_registers_offset();
288 for (int i = 0; i < DwVfpRegister::kMaxNumRegisters; ++i) {
289 if (i == kDoubleRegZero.code()) continue;
290 if (i == kScratchDoubleReg.code()) continue;
292 const DwVfpRegister reg = DwVfpRegister::from_code(i);
293 __ vldr(reg, r1, src_offset, i < 16 ? al : ne);
294 src_offset += kDoubleSize;
297 // Push state, pc, and continuation from the last output frame.
298 __ ldr(r6, MemOperand(r2, FrameDescription::state_offset()));
300 __ ldr(r6, MemOperand(r2, FrameDescription::pc_offset()));
302 __ ldr(r6, MemOperand(r2, FrameDescription::continuation_offset()));
305 // Push the registers from the last output frame.
306 for (int i = kNumberOfRegisters - 1; i >= 0; i--) {
307 int offset = (i * kPointerSize) + FrameDescription::registers_offset();
308 __ ldr(r6, MemOperand(r2, offset));
312 // Restore the registers from the stack.
313 __ ldm(ia_w, sp, restored_regs); // all but pc registers.
314 __ pop(ip); // remove sp
315 __ pop(ip); // remove lr
317 __ InitializeRootRegister();
319 __ pop(ip); // remove pc
320 __ pop(ip); // get continuation, leave pc on stack
323 __ stop("Unreachable.");
327 void Deoptimizer::TableEntryGenerator::GeneratePrologue() {
328 // Create a sequence of deoptimization entries.
329 // Note that registers are still live when jumping to an entry.
331 for (int i = 0; i < count(); i++) {
332 int start = masm()->pc_offset();
334 __ mov(ip, Operand(i));
337 ASSERT(masm()->pc_offset() - start == table_entry_size_);
343 void FrameDescription::SetCallerPc(unsigned offset, intptr_t value) {
344 SetFrameSlot(offset, value);
348 void FrameDescription::SetCallerFp(unsigned offset, intptr_t value) {
349 SetFrameSlot(offset, value);
355 } } // namespace v8::internal