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/codegen.h" 6#include "src/deoptimizer.h" 7#include "src/full-codegen/full-codegen.h" 8#include "src/register-configuration.h" 9#include "src/safepoint-table.h" 10 11namespace v8 { 12namespace internal { 13 14const int Deoptimizer::table_entry_size_ = 8; 15 16 17int Deoptimizer::patch_size() { 18 const int kCallInstructionSizeInWords = 3; 19 return kCallInstructionSizeInWords * Assembler::kInstrSize; 20} 21 22 23void Deoptimizer::EnsureRelocSpaceForLazyDeoptimization(Handle<Code> code) { 24 // Empty because there is no need for relocation information for the code 25 // patching in Deoptimizer::PatchCodeForDeoptimization below. 26} 27 28 29void Deoptimizer::PatchCodeForDeoptimization(Isolate* isolate, Code* code) { 30 Address code_start_address = code->instruction_start(); 31 // Invalidate the relocation information, as it will become invalid by the 32 // code patching below, and is not needed any more. 33 code->InvalidateRelocation(); 34 35 if (FLAG_zap_code_space) { 36 // Fail hard and early if we enter this code object again. 37 byte* pointer = code->FindCodeAgeSequence(); 38 if (pointer != NULL) { 39 pointer += kNoCodeAgeSequenceLength; 40 } else { 41 pointer = code->instruction_start(); 42 } 43 CodePatcher patcher(isolate, pointer, 1); 44 patcher.masm()->bkpt(0); 45 46 DeoptimizationInputData* data = 47 DeoptimizationInputData::cast(code->deoptimization_data()); 48 int osr_offset = data->OsrPcOffset()->value(); 49 if (osr_offset > 0) { 50 CodePatcher osr_patcher(isolate, code->instruction_start() + osr_offset, 51 1); 52 osr_patcher.masm()->bkpt(0); 53 } 54 } 55 56 DeoptimizationInputData* deopt_data = 57 DeoptimizationInputData::cast(code->deoptimization_data()); 58#ifdef DEBUG 59 Address prev_call_address = NULL; 60#endif 61 // For each LLazyBailout instruction insert a call to the corresponding 62 // deoptimization entry. 63 for (int i = 0; i < deopt_data->DeoptCount(); i++) { 64 if (deopt_data->Pc(i)->value() == -1) continue; 65 Address call_address = code_start_address + deopt_data->Pc(i)->value(); 66 Address deopt_entry = GetDeoptimizationEntry(isolate, i, LAZY); 67 // We need calls to have a predictable size in the unoptimized code, but 68 // this is optimized code, so we don't have to have a predictable size. 69 int call_size_in_bytes = MacroAssembler::CallDeoptimizerSize(); 70 int call_size_in_words = call_size_in_bytes / Assembler::kInstrSize; 71 DCHECK(call_size_in_bytes % Assembler::kInstrSize == 0); 72 DCHECK(call_size_in_bytes <= patch_size()); 73 CodePatcher patcher(isolate, call_address, call_size_in_words); 74 patcher.masm()->CallDeoptimizer(deopt_entry); 75 DCHECK(prev_call_address == NULL || 76 call_address >= prev_call_address + patch_size()); 77 DCHECK(call_address + patch_size() <= code->instruction_end()); 78#ifdef DEBUG 79 prev_call_address = call_address; 80#endif 81 } 82} 83 84 85void Deoptimizer::SetPlatformCompiledStubRegisters( 86 FrameDescription* output_frame, CodeStubDescriptor* descriptor) { 87 ApiFunction function(descriptor->deoptimization_handler()); 88 ExternalReference xref(&function, ExternalReference::BUILTIN_CALL, isolate_); 89 intptr_t handler = reinterpret_cast<intptr_t>(xref.address()); 90 int params = descriptor->GetHandlerParameterCount(); 91 output_frame->SetRegister(r0.code(), params); 92 output_frame->SetRegister(r1.code(), handler); 93} 94 95 96void Deoptimizer::CopyDoubleRegisters(FrameDescription* output_frame) { 97 for (int i = 0; i < DwVfpRegister::kMaxNumRegisters; ++i) { 98 Float64 double_value = input_->GetDoubleRegister(i); 99 output_frame->SetDoubleRegister(i, double_value); 100 } 101} 102 103#define __ masm()-> 104 105// This code tries to be close to ia32 code so that any changes can be 106// easily ported. 107void Deoptimizer::TableEntryGenerator::Generate() { 108 GeneratePrologue(); 109 110 // Save all general purpose registers before messing with them. 111 const int kNumberOfRegisters = Register::kNumRegisters; 112 113 // Everything but pc, lr and ip which will be saved but not restored. 114 RegList restored_regs = kJSCallerSaved | kCalleeSaved | ip.bit(); 115 116 const int kDoubleRegsSize = kDoubleSize * DwVfpRegister::kMaxNumRegisters; 117 118 // Save all allocatable VFP registers before messing with them. 119 DCHECK(kDoubleRegZero.code() == 14); 120 DCHECK(kScratchDoubleReg.code() == 15); 121 122 { 123 // We use a run-time check for VFP32DREGS. 124 CpuFeatureScope scope(masm(), VFP32DREGS, 125 CpuFeatureScope::kDontCheckSupported); 126 127 // Check CPU flags for number of registers, setting the Z condition flag. 128 __ CheckFor32DRegs(ip); 129 130 // Push registers d0-d15, and possibly d16-d31, on the stack. 131 // If d16-d31 are not pushed, decrease the stack pointer instead. 132 __ vstm(db_w, sp, d16, d31, ne); 133 __ sub(sp, sp, Operand(16 * kDoubleSize), LeaveCC, eq); 134 __ vstm(db_w, sp, d0, d15); 135 } 136 137 // Push all 16 registers (needed to populate FrameDescription::registers_). 138 // TODO(1588) Note that using pc with stm is deprecated, so we should perhaps 139 // handle this a bit differently. 140 __ stm(db_w, sp, restored_regs | sp.bit() | lr.bit() | pc.bit()); 141 142 __ mov(ip, Operand(ExternalReference(Isolate::kCEntryFPAddress, isolate()))); 143 __ str(fp, MemOperand(ip)); 144 145 const int kSavedRegistersAreaSize = 146 (kNumberOfRegisters * kPointerSize) + kDoubleRegsSize; 147 148 // Get the bailout id from the stack. 149 __ ldr(r2, MemOperand(sp, kSavedRegistersAreaSize)); 150 151 // Get the address of the location in the code object (r3) (return 152 // address for lazy deoptimization) and compute the fp-to-sp delta in 153 // register r4. 154 __ mov(r3, lr); 155 // Correct one word for bailout id. 156 __ add(r4, sp, Operand(kSavedRegistersAreaSize + (1 * kPointerSize))); 157 __ sub(r4, fp, r4); 158 159 // Allocate a new deoptimizer object. 160 // Pass four arguments in r0 to r3 and fifth argument on stack. 161 __ PrepareCallCFunction(6, r5); 162 __ mov(r0, Operand(0)); 163 Label context_check; 164 __ ldr(r1, MemOperand(fp, CommonFrameConstants::kContextOrFrameTypeOffset)); 165 __ JumpIfSmi(r1, &context_check); 166 __ ldr(r0, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset)); 167 __ bind(&context_check); 168 __ mov(r1, Operand(type())); // bailout type, 169 // r2: bailout id already loaded. 170 // r3: code address or 0 already loaded. 171 __ str(r4, MemOperand(sp, 0 * kPointerSize)); // Fp-to-sp delta. 172 __ mov(r5, Operand(ExternalReference::isolate_address(isolate()))); 173 __ str(r5, MemOperand(sp, 1 * kPointerSize)); // Isolate. 174 // Call Deoptimizer::New(). 175 { 176 AllowExternalCallThatCantCauseGC scope(masm()); 177 __ CallCFunction(ExternalReference::new_deoptimizer_function(isolate()), 6); 178 } 179 180 // Preserve "deoptimizer" object in register r0 and get the input 181 // frame descriptor pointer to r1 (deoptimizer->input_); 182 __ ldr(r1, MemOperand(r0, Deoptimizer::input_offset())); 183 184 // Copy core registers into FrameDescription::registers_[kNumRegisters]. 185 DCHECK(Register::kNumRegisters == kNumberOfRegisters); 186 for (int i = 0; i < kNumberOfRegisters; i++) { 187 int offset = (i * kPointerSize) + FrameDescription::registers_offset(); 188 __ ldr(r2, MemOperand(sp, i * kPointerSize)); 189 __ str(r2, MemOperand(r1, offset)); 190 } 191 192 // Copy VFP registers to 193 // double_registers_[DoubleRegister::kMaxNumAllocatableRegisters] 194 int double_regs_offset = FrameDescription::double_registers_offset(); 195 const RegisterConfiguration* config = RegisterConfiguration::Crankshaft(); 196 for (int i = 0; i < config->num_allocatable_double_registers(); ++i) { 197 int code = config->GetAllocatableDoubleCode(i); 198 int dst_offset = code * kDoubleSize + double_regs_offset; 199 int src_offset = code * kDoubleSize + kNumberOfRegisters * kPointerSize; 200 __ vldr(d0, sp, src_offset); 201 __ vstr(d0, r1, dst_offset); 202 } 203 204 // Remove the bailout id and the saved registers from the stack. 205 __ add(sp, sp, Operand(kSavedRegistersAreaSize + (1 * kPointerSize))); 206 207 // Compute a pointer to the unwinding limit in register r2; that is 208 // the first stack slot not part of the input frame. 209 __ ldr(r2, MemOperand(r1, FrameDescription::frame_size_offset())); 210 __ add(r2, r2, sp); 211 212 // Unwind the stack down to - but not including - the unwinding 213 // limit and copy the contents of the activation frame to the input 214 // frame description. 215 __ add(r3, r1, Operand(FrameDescription::frame_content_offset())); 216 Label pop_loop; 217 Label pop_loop_header; 218 __ b(&pop_loop_header); 219 __ bind(&pop_loop); 220 __ pop(r4); 221 __ str(r4, MemOperand(r3, 0)); 222 __ add(r3, r3, Operand(sizeof(uint32_t))); 223 __ bind(&pop_loop_header); 224 __ cmp(r2, sp); 225 __ b(ne, &pop_loop); 226 227 // Compute the output frame in the deoptimizer. 228 __ push(r0); // Preserve deoptimizer object across call. 229 // r0: deoptimizer object; r1: scratch. 230 __ PrepareCallCFunction(1, r1); 231 // Call Deoptimizer::ComputeOutputFrames(). 232 { 233 AllowExternalCallThatCantCauseGC scope(masm()); 234 __ CallCFunction( 235 ExternalReference::compute_output_frames_function(isolate()), 1); 236 } 237 __ pop(r0); // Restore deoptimizer object (class Deoptimizer). 238 239 __ ldr(sp, MemOperand(r0, Deoptimizer::caller_frame_top_offset())); 240 241 // Replace the current (input) frame with the output frames. 242 Label outer_push_loop, inner_push_loop, 243 outer_loop_header, inner_loop_header; 244 // Outer loop state: r4 = current "FrameDescription** output_", 245 // r1 = one past the last FrameDescription**. 246 __ ldr(r1, MemOperand(r0, Deoptimizer::output_count_offset())); 247 __ ldr(r4, MemOperand(r0, Deoptimizer::output_offset())); // r4 is output_. 248 __ add(r1, r4, Operand(r1, LSL, 2)); 249 __ jmp(&outer_loop_header); 250 __ bind(&outer_push_loop); 251 // Inner loop state: r2 = current FrameDescription*, r3 = loop index. 252 __ ldr(r2, MemOperand(r4, 0)); // output_[ix] 253 __ ldr(r3, MemOperand(r2, FrameDescription::frame_size_offset())); 254 __ jmp(&inner_loop_header); 255 __ bind(&inner_push_loop); 256 __ sub(r3, r3, Operand(sizeof(uint32_t))); 257 __ add(r6, r2, Operand(r3)); 258 __ ldr(r6, MemOperand(r6, FrameDescription::frame_content_offset())); 259 __ push(r6); 260 __ bind(&inner_loop_header); 261 __ cmp(r3, Operand::Zero()); 262 __ b(ne, &inner_push_loop); // test for gt? 263 __ add(r4, r4, Operand(kPointerSize)); 264 __ bind(&outer_loop_header); 265 __ cmp(r4, r1); 266 __ b(lt, &outer_push_loop); 267 268 __ ldr(r1, MemOperand(r0, Deoptimizer::input_offset())); 269 for (int i = 0; i < config->num_allocatable_double_registers(); ++i) { 270 int code = config->GetAllocatableDoubleCode(i); 271 DwVfpRegister reg = DwVfpRegister::from_code(code); 272 int src_offset = code * kDoubleSize + double_regs_offset; 273 __ vldr(reg, r1, src_offset); 274 } 275 276 // Push state, pc, and continuation from the last output frame. 277 __ ldr(r6, MemOperand(r2, FrameDescription::state_offset())); 278 __ push(r6); 279 __ ldr(r6, MemOperand(r2, FrameDescription::pc_offset())); 280 __ push(r6); 281 __ ldr(r6, MemOperand(r2, FrameDescription::continuation_offset())); 282 __ push(r6); 283 284 // Push the registers from the last output frame. 285 for (int i = kNumberOfRegisters - 1; i >= 0; i--) { 286 int offset = (i * kPointerSize) + FrameDescription::registers_offset(); 287 __ ldr(r6, MemOperand(r2, offset)); 288 __ push(r6); 289 } 290 291 // Restore the registers from the stack. 292 __ ldm(ia_w, sp, restored_regs); // all but pc registers. 293 __ pop(ip); // remove sp 294 __ pop(ip); // remove lr 295 296 __ InitializeRootRegister(); 297 298 __ pop(ip); // remove pc 299 __ pop(ip); // get continuation, leave pc on stack 300 __ pop(lr); 301 __ Jump(ip); 302 __ stop("Unreachable."); 303} 304 305 306void Deoptimizer::TableEntryGenerator::GeneratePrologue() { 307 // Create a sequence of deoptimization entries. 308 // Note that registers are still live when jumping to an entry. 309 310 // We need to be able to generate immediates up to kMaxNumberOfEntries. On 311 // ARMv7, we can use movw (with a maximum immediate of 0xffff). On ARMv6, we 312 // need two instructions. 313 STATIC_ASSERT((kMaxNumberOfEntries - 1) <= 0xffff); 314 if (CpuFeatures::IsSupported(ARMv7)) { 315 CpuFeatureScope scope(masm(), ARMv7); 316 Label done; 317 for (int i = 0; i < count(); i++) { 318 int start = masm()->pc_offset(); 319 USE(start); 320 __ movw(ip, i); 321 __ b(&done); 322 DCHECK_EQ(table_entry_size_, masm()->pc_offset() - start); 323 } 324 __ bind(&done); 325 } else { 326 // We want to keep table_entry_size_ == 8 (since this is the common case), 327 // but we need two instructions to load most immediates over 0xff. To handle 328 // this, we set the low byte in the main table, and then set the high byte 329 // in a separate table if necessary. 330 Label high_fixes[256]; 331 int high_fix_max = (count() - 1) >> 8; 332 DCHECK_GT(arraysize(high_fixes), static_cast<size_t>(high_fix_max)); 333 for (int i = 0; i < count(); i++) { 334 int start = masm()->pc_offset(); 335 USE(start); 336 __ mov(ip, Operand(i & 0xff)); // Set the low byte. 337 __ b(&high_fixes[i >> 8]); // Jump to the secondary table. 338 DCHECK_EQ(table_entry_size_, masm()->pc_offset() - start); 339 } 340 // Generate the secondary table, to set the high byte. 341 for (int high = 1; high <= high_fix_max; high++) { 342 __ bind(&high_fixes[high]); 343 __ orr(ip, ip, Operand(high << 8)); 344 // If this isn't the last entry, emit a branch to the end of the table. 345 // The last entry can just fall through. 346 if (high < high_fix_max) __ b(&high_fixes[0]); 347 } 348 // Bind high_fixes[0] last, for indices like 0x00**. This case requires no 349 // fix-up, so for (common) small tables we can jump here, then just fall 350 // through with no additional branch. 351 __ bind(&high_fixes[0]); 352 } 353 __ push(ip); 354} 355 356 357void FrameDescription::SetCallerPc(unsigned offset, intptr_t value) { 358 SetFrameSlot(offset, value); 359} 360 361 362void FrameDescription::SetCallerFp(unsigned offset, intptr_t value) { 363 SetFrameSlot(offset, value); 364} 365 366 367void FrameDescription::SetCallerConstantPool(unsigned offset, intptr_t value) { 368 DCHECK(FLAG_enable_embedded_constant_pool); 369 SetFrameSlot(offset, value); 370} 371 372 373#undef __ 374 375} // namespace internal 376} // namespace v8 377