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/v8.h" 6 7#if V8_TARGET_ARCH_IA32 8 9#include "src/codegen.h" 10#include "src/deoptimizer.h" 11#include "src/full-codegen.h" 12#include "src/safepoint-table.h" 13 14namespace v8 { 15namespace internal { 16 17const int Deoptimizer::table_entry_size_ = 10; 18 19 20int Deoptimizer::patch_size() { 21 return Assembler::kCallInstructionLength; 22} 23 24 25void Deoptimizer::EnsureRelocSpaceForLazyDeoptimization(Handle<Code> code) { 26 Isolate* isolate = code->GetIsolate(); 27 HandleScope scope(isolate); 28 29 // Compute the size of relocation information needed for the code 30 // patching in Deoptimizer::DeoptimizeFunction. 31 int min_reloc_size = 0; 32 int prev_pc_offset = 0; 33 DeoptimizationInputData* deopt_data = 34 DeoptimizationInputData::cast(code->deoptimization_data()); 35 for (int i = 0; i < deopt_data->DeoptCount(); i++) { 36 int pc_offset = deopt_data->Pc(i)->value(); 37 if (pc_offset == -1) continue; 38 ASSERT_GE(pc_offset, prev_pc_offset); 39 int pc_delta = pc_offset - prev_pc_offset; 40 // We use RUNTIME_ENTRY reloc info which has a size of 2 bytes 41 // if encodable with small pc delta encoding and up to 6 bytes 42 // otherwise. 43 if (pc_delta <= RelocInfo::kMaxSmallPCDelta) { 44 min_reloc_size += 2; 45 } else { 46 min_reloc_size += 6; 47 } 48 prev_pc_offset = pc_offset; 49 } 50 51 // If the relocation information is not big enough we create a new 52 // relocation info object that is padded with comments to make it 53 // big enough for lazy doptimization. 54 int reloc_length = code->relocation_info()->length(); 55 if (min_reloc_size > reloc_length) { 56 int comment_reloc_size = RelocInfo::kMinRelocCommentSize; 57 // Padding needed. 58 int min_padding = min_reloc_size - reloc_length; 59 // Number of comments needed to take up at least that much space. 60 int additional_comments = 61 (min_padding + comment_reloc_size - 1) / comment_reloc_size; 62 // Actual padding size. 63 int padding = additional_comments * comment_reloc_size; 64 // Allocate new relocation info and copy old relocation to the end 65 // of the new relocation info array because relocation info is 66 // written and read backwards. 67 Factory* factory = isolate->factory(); 68 Handle<ByteArray> new_reloc = 69 factory->NewByteArray(reloc_length + padding, TENURED); 70 MemCopy(new_reloc->GetDataStartAddress() + padding, 71 code->relocation_info()->GetDataStartAddress(), reloc_length); 72 // Create a relocation writer to write the comments in the padding 73 // space. Use position 0 for everything to ensure short encoding. 74 RelocInfoWriter reloc_info_writer( 75 new_reloc->GetDataStartAddress() + padding, 0); 76 intptr_t comment_string 77 = reinterpret_cast<intptr_t>(RelocInfo::kFillerCommentString); 78 RelocInfo rinfo(0, RelocInfo::COMMENT, comment_string, NULL); 79 for (int i = 0; i < additional_comments; ++i) { 80#ifdef DEBUG 81 byte* pos_before = reloc_info_writer.pos(); 82#endif 83 reloc_info_writer.Write(&rinfo); 84 ASSERT(RelocInfo::kMinRelocCommentSize == 85 pos_before - reloc_info_writer.pos()); 86 } 87 // Replace relocation information on the code object. 88 code->set_relocation_info(*new_reloc); 89 } 90} 91 92 93void Deoptimizer::PatchCodeForDeoptimization(Isolate* isolate, Code* code) { 94 Address code_start_address = code->instruction_start(); 95 96 if (FLAG_zap_code_space) { 97 // Fail hard and early if we enter this code object again. 98 byte* pointer = code->FindCodeAgeSequence(); 99 if (pointer != NULL) { 100 pointer += kNoCodeAgeSequenceLength; 101 } else { 102 pointer = code->instruction_start(); 103 } 104 CodePatcher patcher(pointer, 1); 105 patcher.masm()->int3(); 106 107 DeoptimizationInputData* data = 108 DeoptimizationInputData::cast(code->deoptimization_data()); 109 int osr_offset = data->OsrPcOffset()->value(); 110 if (osr_offset > 0) { 111 CodePatcher osr_patcher(code->instruction_start() + osr_offset, 1); 112 osr_patcher.masm()->int3(); 113 } 114 } 115 116 // We will overwrite the code's relocation info in-place. Relocation info 117 // is written backward. The relocation info is the payload of a byte 118 // array. Later on we will slide this to the start of the byte array and 119 // create a filler object in the remaining space. 120 ByteArray* reloc_info = code->relocation_info(); 121 Address reloc_end_address = reloc_info->address() + reloc_info->Size(); 122 RelocInfoWriter reloc_info_writer(reloc_end_address, code_start_address); 123 124 // Since the call is a relative encoding, write new 125 // reloc info. We do not need any of the existing reloc info because the 126 // existing code will not be used again (we zap it in debug builds). 127 // 128 // Emit call to lazy deoptimization at all lazy deopt points. 129 DeoptimizationInputData* deopt_data = 130 DeoptimizationInputData::cast(code->deoptimization_data()); 131 SharedFunctionInfo* shared = 132 SharedFunctionInfo::cast(deopt_data->SharedFunctionInfo()); 133 shared->EvictFromOptimizedCodeMap(code, "deoptimized code"); 134#ifdef DEBUG 135 Address prev_call_address = NULL; 136#endif 137 // For each LLazyBailout instruction insert a call to the corresponding 138 // deoptimization entry. 139 for (int i = 0; i < deopt_data->DeoptCount(); i++) { 140 if (deopt_data->Pc(i)->value() == -1) continue; 141 // Patch lazy deoptimization entry. 142 Address call_address = code_start_address + deopt_data->Pc(i)->value(); 143 CodePatcher patcher(call_address, patch_size()); 144 Address deopt_entry = GetDeoptimizationEntry(isolate, i, LAZY); 145 patcher.masm()->call(deopt_entry, RelocInfo::NONE32); 146 // We use RUNTIME_ENTRY for deoptimization bailouts. 147 RelocInfo rinfo(call_address + 1, // 1 after the call opcode. 148 RelocInfo::RUNTIME_ENTRY, 149 reinterpret_cast<intptr_t>(deopt_entry), 150 NULL); 151 reloc_info_writer.Write(&rinfo); 152 ASSERT_GE(reloc_info_writer.pos(), 153 reloc_info->address() + ByteArray::kHeaderSize); 154 ASSERT(prev_call_address == NULL || 155 call_address >= prev_call_address + patch_size()); 156 ASSERT(call_address + patch_size() <= code->instruction_end()); 157#ifdef DEBUG 158 prev_call_address = call_address; 159#endif 160 } 161 162 // Move the relocation info to the beginning of the byte array. 163 int new_reloc_size = reloc_end_address - reloc_info_writer.pos(); 164 MemMove(code->relocation_start(), reloc_info_writer.pos(), new_reloc_size); 165 166 // The relocation info is in place, update the size. 167 reloc_info->set_length(new_reloc_size); 168 169 // Handle the junk part after the new relocation info. We will create 170 // a non-live object in the extra space at the end of the former reloc info. 171 Address junk_address = reloc_info->address() + reloc_info->Size(); 172 ASSERT(junk_address <= reloc_end_address); 173 isolate->heap()->CreateFillerObjectAt(junk_address, 174 reloc_end_address - junk_address); 175} 176 177 178void Deoptimizer::FillInputFrame(Address tos, JavaScriptFrame* frame) { 179 // Set the register values. The values are not important as there are no 180 // callee saved registers in JavaScript frames, so all registers are 181 // spilled. Registers ebp and esp are set to the correct values though. 182 183 for (int i = 0; i < Register::kNumRegisters; i++) { 184 input_->SetRegister(i, i * 4); 185 } 186 input_->SetRegister(esp.code(), reinterpret_cast<intptr_t>(frame->sp())); 187 input_->SetRegister(ebp.code(), reinterpret_cast<intptr_t>(frame->fp())); 188 for (int i = 0; i < XMMRegister::kMaxNumAllocatableRegisters; i++) { 189 input_->SetDoubleRegister(i, 0.0); 190 } 191 192 // Fill the frame content from the actual data on the frame. 193 for (unsigned i = 0; i < input_->GetFrameSize(); i += kPointerSize) { 194 input_->SetFrameSlot(i, Memory::uint32_at(tos + i)); 195 } 196} 197 198 199void Deoptimizer::SetPlatformCompiledStubRegisters( 200 FrameDescription* output_frame, CodeStubInterfaceDescriptor* descriptor) { 201 intptr_t handler = 202 reinterpret_cast<intptr_t>(descriptor->deoptimization_handler_); 203 int params = descriptor->GetHandlerParameterCount(); 204 output_frame->SetRegister(eax.code(), params); 205 output_frame->SetRegister(ebx.code(), handler); 206} 207 208 209void Deoptimizer::CopyDoubleRegisters(FrameDescription* output_frame) { 210 for (int i = 0; i < XMMRegister::kMaxNumAllocatableRegisters; ++i) { 211 double double_value = input_->GetDoubleRegister(i); 212 output_frame->SetDoubleRegister(i, double_value); 213 } 214} 215 216 217bool Deoptimizer::HasAlignmentPadding(JSFunction* function) { 218 int parameter_count = function->shared()->formal_parameter_count() + 1; 219 unsigned input_frame_size = input_->GetFrameSize(); 220 unsigned alignment_state_offset = 221 input_frame_size - parameter_count * kPointerSize - 222 StandardFrameConstants::kFixedFrameSize - 223 kPointerSize; 224 ASSERT(JavaScriptFrameConstants::kDynamicAlignmentStateOffset == 225 JavaScriptFrameConstants::kLocal0Offset); 226 int32_t alignment_state = input_->GetFrameSlot(alignment_state_offset); 227 return (alignment_state == kAlignmentPaddingPushed); 228} 229 230 231#define __ masm()-> 232 233void Deoptimizer::EntryGenerator::Generate() { 234 GeneratePrologue(); 235 236 // Save all general purpose registers before messing with them. 237 const int kNumberOfRegisters = Register::kNumRegisters; 238 239 const int kDoubleRegsSize = kDoubleSize * 240 XMMRegister::kMaxNumAllocatableRegisters; 241 __ sub(esp, Immediate(kDoubleRegsSize)); 242 for (int i = 0; i < XMMRegister::kMaxNumAllocatableRegisters; ++i) { 243 XMMRegister xmm_reg = XMMRegister::FromAllocationIndex(i); 244 int offset = i * kDoubleSize; 245 __ movsd(Operand(esp, offset), xmm_reg); 246 } 247 248 __ pushad(); 249 250 const int kSavedRegistersAreaSize = kNumberOfRegisters * kPointerSize + 251 kDoubleRegsSize; 252 253 // Get the bailout id from the stack. 254 __ mov(ebx, Operand(esp, kSavedRegistersAreaSize)); 255 256 // Get the address of the location in the code object 257 // and compute the fp-to-sp delta in register edx. 258 __ mov(ecx, Operand(esp, kSavedRegistersAreaSize + 1 * kPointerSize)); 259 __ lea(edx, Operand(esp, kSavedRegistersAreaSize + 2 * kPointerSize)); 260 261 __ sub(edx, ebp); 262 __ neg(edx); 263 264 // Allocate a new deoptimizer object. 265 __ PrepareCallCFunction(6, eax); 266 __ mov(eax, Operand(ebp, JavaScriptFrameConstants::kFunctionOffset)); 267 __ mov(Operand(esp, 0 * kPointerSize), eax); // Function. 268 __ mov(Operand(esp, 1 * kPointerSize), Immediate(type())); // Bailout type. 269 __ mov(Operand(esp, 2 * kPointerSize), ebx); // Bailout id. 270 __ mov(Operand(esp, 3 * kPointerSize), ecx); // Code address or 0. 271 __ mov(Operand(esp, 4 * kPointerSize), edx); // Fp-to-sp delta. 272 __ mov(Operand(esp, 5 * kPointerSize), 273 Immediate(ExternalReference::isolate_address(isolate()))); 274 { 275 AllowExternalCallThatCantCauseGC scope(masm()); 276 __ CallCFunction(ExternalReference::new_deoptimizer_function(isolate()), 6); 277 } 278 279 // Preserve deoptimizer object in register eax and get the input 280 // frame descriptor pointer. 281 __ mov(ebx, Operand(eax, Deoptimizer::input_offset())); 282 283 // Fill in the input registers. 284 for (int i = kNumberOfRegisters - 1; i >= 0; i--) { 285 int offset = (i * kPointerSize) + FrameDescription::registers_offset(); 286 __ pop(Operand(ebx, offset)); 287 } 288 289 int double_regs_offset = FrameDescription::double_registers_offset(); 290 // Fill in the double input registers. 291 for (int i = 0; i < XMMRegister::kMaxNumAllocatableRegisters; ++i) { 292 int dst_offset = i * kDoubleSize + double_regs_offset; 293 int src_offset = i * kDoubleSize; 294 __ movsd(xmm0, Operand(esp, src_offset)); 295 __ movsd(Operand(ebx, dst_offset), xmm0); 296 } 297 298 // Clear FPU all exceptions. 299 // TODO(ulan): Find out why the TOP register is not zero here in some cases, 300 // and check that the generated code never deoptimizes with unbalanced stack. 301 __ fnclex(); 302 303 // Remove the bailout id, return address and the double registers. 304 __ add(esp, Immediate(kDoubleRegsSize + 2 * kPointerSize)); 305 306 // Compute a pointer to the unwinding limit in register ecx; that is 307 // the first stack slot not part of the input frame. 308 __ mov(ecx, Operand(ebx, FrameDescription::frame_size_offset())); 309 __ add(ecx, esp); 310 311 // Unwind the stack down to - but not including - the unwinding 312 // limit and copy the contents of the activation frame to the input 313 // frame description. 314 __ lea(edx, Operand(ebx, FrameDescription::frame_content_offset())); 315 Label pop_loop_header; 316 __ jmp(&pop_loop_header); 317 Label pop_loop; 318 __ bind(&pop_loop); 319 __ pop(Operand(edx, 0)); 320 __ add(edx, Immediate(sizeof(uint32_t))); 321 __ bind(&pop_loop_header); 322 __ cmp(ecx, esp); 323 __ j(not_equal, &pop_loop); 324 325 // Compute the output frame in the deoptimizer. 326 __ push(eax); 327 __ PrepareCallCFunction(1, ebx); 328 __ mov(Operand(esp, 0 * kPointerSize), eax); 329 { 330 AllowExternalCallThatCantCauseGC scope(masm()); 331 __ CallCFunction( 332 ExternalReference::compute_output_frames_function(isolate()), 1); 333 } 334 __ pop(eax); 335 336 // If frame was dynamically aligned, pop padding. 337 Label no_padding; 338 __ cmp(Operand(eax, Deoptimizer::has_alignment_padding_offset()), 339 Immediate(0)); 340 __ j(equal, &no_padding); 341 __ pop(ecx); 342 if (FLAG_debug_code) { 343 __ cmp(ecx, Immediate(kAlignmentZapValue)); 344 __ Assert(equal, kAlignmentMarkerExpected); 345 } 346 __ bind(&no_padding); 347 348 // Replace the current frame with the output frames. 349 Label outer_push_loop, inner_push_loop, 350 outer_loop_header, inner_loop_header; 351 // Outer loop state: eax = current FrameDescription**, edx = one past the 352 // last FrameDescription**. 353 __ mov(edx, Operand(eax, Deoptimizer::output_count_offset())); 354 __ mov(eax, Operand(eax, Deoptimizer::output_offset())); 355 __ lea(edx, Operand(eax, edx, times_4, 0)); 356 __ jmp(&outer_loop_header); 357 __ bind(&outer_push_loop); 358 // Inner loop state: ebx = current FrameDescription*, ecx = loop index. 359 __ mov(ebx, Operand(eax, 0)); 360 __ mov(ecx, Operand(ebx, FrameDescription::frame_size_offset())); 361 __ jmp(&inner_loop_header); 362 __ bind(&inner_push_loop); 363 __ sub(ecx, Immediate(sizeof(uint32_t))); 364 __ push(Operand(ebx, ecx, times_1, FrameDescription::frame_content_offset())); 365 __ bind(&inner_loop_header); 366 __ test(ecx, ecx); 367 __ j(not_zero, &inner_push_loop); 368 __ add(eax, Immediate(kPointerSize)); 369 __ bind(&outer_loop_header); 370 __ cmp(eax, edx); 371 __ j(below, &outer_push_loop); 372 373 // In case of a failed STUB, we have to restore the XMM registers. 374 for (int i = 0; i < XMMRegister::kMaxNumAllocatableRegisters; ++i) { 375 XMMRegister xmm_reg = XMMRegister::FromAllocationIndex(i); 376 int src_offset = i * kDoubleSize + double_regs_offset; 377 __ movsd(xmm_reg, Operand(ebx, src_offset)); 378 } 379 380 // Push state, pc, and continuation from the last output frame. 381 __ push(Operand(ebx, FrameDescription::state_offset())); 382 __ push(Operand(ebx, FrameDescription::pc_offset())); 383 __ push(Operand(ebx, FrameDescription::continuation_offset())); 384 385 386 // Push the registers from the last output frame. 387 for (int i = 0; i < kNumberOfRegisters; i++) { 388 int offset = (i * kPointerSize) + FrameDescription::registers_offset(); 389 __ push(Operand(ebx, offset)); 390 } 391 392 // Restore the registers from the stack. 393 __ popad(); 394 395 // Return to the continuation point. 396 __ ret(0); 397} 398 399 400void Deoptimizer::TableEntryGenerator::GeneratePrologue() { 401 // Create a sequence of deoptimization entries. 402 Label done; 403 for (int i = 0; i < count(); i++) { 404 int start = masm()->pc_offset(); 405 USE(start); 406 __ push_imm32(i); 407 __ jmp(&done); 408 ASSERT(masm()->pc_offset() - start == table_entry_size_); 409 } 410 __ bind(&done); 411} 412 413 414void FrameDescription::SetCallerPc(unsigned offset, intptr_t value) { 415 SetFrameSlot(offset, value); 416} 417 418 419void FrameDescription::SetCallerFp(unsigned offset, intptr_t value) { 420 SetFrameSlot(offset, value); 421} 422 423 424void FrameDescription::SetCallerConstantPool(unsigned offset, intptr_t value) { 425 // No out-of-line constant pool support. 426 UNREACHABLE(); 427} 428 429 430#undef __ 431 432 433} } // namespace v8::internal 434 435#endif // V8_TARGET_ARCH_IA32 436