Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1 | // Copyright 2012 the V8 project authors. All rights reserved. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 4 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5 | #if V8_TARGET_ARCH_IA32 |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 6 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7 | #include "src/codegen.h" |
| 8 | #include "src/deoptimizer.h" |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 9 | #include "src/full-codegen/full-codegen.h" |
| 10 | #include "src/ia32/frames-ia32.h" |
| 11 | #include "src/register-configuration.h" |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12 | #include "src/safepoint-table.h" |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 13 | |
| 14 | namespace v8 { |
| 15 | namespace internal { |
| 16 | |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 17 | const int Deoptimizer::table_entry_size_ = 10; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 18 | |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 19 | |
| 20 | int Deoptimizer::patch_size() { |
| 21 | return Assembler::kCallInstructionLength; |
| 22 | } |
| 23 | |
| 24 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 25 | void Deoptimizer::EnsureRelocSpaceForLazyDeoptimization(Handle<Code> code) { |
| 26 | Isolate* isolate = code->GetIsolate(); |
| 27 | HandleScope scope(isolate); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 28 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 29 | // Compute the size of relocation information needed for the code |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 30 | // patching in Deoptimizer::PatchCodeForDeoptimization below. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 31 | int min_reloc_size = 0; |
Ben Murdoch | 2b4ba11 | 2012-01-20 14:57:15 +0000 | [diff] [blame] | 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; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 38 | DCHECK_GE(pc_offset, prev_pc_offset); |
Ben Murdoch | 2b4ba11 | 2012-01-20 14:57:15 +0000 | [diff] [blame] | 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; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 47 | } |
Ben Murdoch | 2b4ba11 | 2012-01-20 14:57:15 +0000 | [diff] [blame] | 48 | prev_pc_offset = pc_offset; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 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); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 70 | MemCopy(new_reloc->GetDataStartAddress() + padding, |
| 71 | code->relocation_info()->GetDataStartAddress(), reloc_length); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 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); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 78 | RelocInfo rinfo(isolate, 0, RelocInfo::COMMENT, comment_string, NULL); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 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); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 84 | DCHECK(RelocInfo::kMinRelocCommentSize == |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 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 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 93 | void Deoptimizer::PatchCodeForDeoptimization(Isolate* isolate, Code* code) { |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 94 | Address code_start_address = code->instruction_start(); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 95 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 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 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 104 | CodePatcher patcher(isolate, pointer, 1); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 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) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 111 | CodePatcher osr_patcher(isolate, code->instruction_start() + osr_offset, |
| 112 | 1); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 113 | osr_patcher.masm()->int3(); |
| 114 | } |
| 115 | } |
| 116 | |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 117 | // We will overwrite the code's relocation info in-place. Relocation info |
| 118 | // is written backward. The relocation info is the payload of a byte |
| 119 | // array. Later on we will slide this to the start of the byte array and |
| 120 | // create a filler object in the remaining space. |
| 121 | ByteArray* reloc_info = code->relocation_info(); |
| 122 | Address reloc_end_address = reloc_info->address() + reloc_info->Size(); |
| 123 | RelocInfoWriter reloc_info_writer(reloc_end_address, code_start_address); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 124 | |
Ben Murdoch | 2b4ba11 | 2012-01-20 14:57:15 +0000 | [diff] [blame] | 125 | // Since the call is a relative encoding, write new |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 126 | // reloc info. We do not need any of the existing reloc info because the |
| 127 | // existing code will not be used again (we zap it in debug builds). |
Ben Murdoch | 2b4ba11 | 2012-01-20 14:57:15 +0000 | [diff] [blame] | 128 | // |
| 129 | // Emit call to lazy deoptimization at all lazy deopt points. |
| 130 | DeoptimizationInputData* deopt_data = |
| 131 | DeoptimizationInputData::cast(code->deoptimization_data()); |
| 132 | #ifdef DEBUG |
| 133 | Address prev_call_address = NULL; |
| 134 | #endif |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 135 | // For each LLazyBailout instruction insert a call to the corresponding |
| 136 | // deoptimization entry. |
Ben Murdoch | 2b4ba11 | 2012-01-20 14:57:15 +0000 | [diff] [blame] | 137 | for (int i = 0; i < deopt_data->DeoptCount(); i++) { |
| 138 | if (deopt_data->Pc(i)->value() == -1) continue; |
| 139 | // Patch lazy deoptimization entry. |
| 140 | Address call_address = code_start_address + deopt_data->Pc(i)->value(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 141 | CodePatcher patcher(isolate, call_address, patch_size()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 142 | Address deopt_entry = GetDeoptimizationEntry(isolate, i, LAZY); |
| 143 | patcher.masm()->call(deopt_entry, RelocInfo::NONE32); |
Ben Murdoch | 2b4ba11 | 2012-01-20 14:57:15 +0000 | [diff] [blame] | 144 | // We use RUNTIME_ENTRY for deoptimization bailouts. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 145 | RelocInfo rinfo(isolate, call_address + 1, // 1 after the call opcode. |
Ben Murdoch | 2b4ba11 | 2012-01-20 14:57:15 +0000 | [diff] [blame] | 146 | RelocInfo::RUNTIME_ENTRY, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 147 | reinterpret_cast<intptr_t>(deopt_entry), NULL); |
Ben Murdoch | 2b4ba11 | 2012-01-20 14:57:15 +0000 | [diff] [blame] | 148 | reloc_info_writer.Write(&rinfo); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 149 | DCHECK_GE(reloc_info_writer.pos(), |
Ben Murdoch | 2b4ba11 | 2012-01-20 14:57:15 +0000 | [diff] [blame] | 150 | reloc_info->address() + ByteArray::kHeaderSize); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 151 | DCHECK(prev_call_address == NULL || |
Ben Murdoch | 2b4ba11 | 2012-01-20 14:57:15 +0000 | [diff] [blame] | 152 | call_address >= prev_call_address + patch_size()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 153 | DCHECK(call_address + patch_size() <= code->instruction_end()); |
Ben Murdoch | 2b4ba11 | 2012-01-20 14:57:15 +0000 | [diff] [blame] | 154 | #ifdef DEBUG |
| 155 | prev_call_address = call_address; |
| 156 | #endif |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 157 | } |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 158 | |
| 159 | // Move the relocation info to the beginning of the byte array. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 160 | const int new_reloc_length = reloc_end_address - reloc_info_writer.pos(); |
| 161 | MemMove(code->relocation_start(), reloc_info_writer.pos(), new_reloc_length); |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 162 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 163 | // Right trim the relocation info to free up remaining space. |
| 164 | const int delta = reloc_info->length() - new_reloc_length; |
| 165 | if (delta > 0) { |
| 166 | isolate->heap()->RightTrimFixedArray<Heap::SEQUENTIAL_TO_SWEEPER>( |
| 167 | reloc_info, delta); |
| 168 | } |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 172 | void Deoptimizer::FillInputFrame(Address tos, JavaScriptFrame* frame) { |
| 173 | // Set the register values. The values are not important as there are no |
| 174 | // callee saved registers in JavaScript frames, so all registers are |
| 175 | // spilled. Registers ebp and esp are set to the correct values though. |
| 176 | |
| 177 | for (int i = 0; i < Register::kNumRegisters; i++) { |
| 178 | input_->SetRegister(i, i * 4); |
| 179 | } |
| 180 | input_->SetRegister(esp.code(), reinterpret_cast<intptr_t>(frame->sp())); |
| 181 | input_->SetRegister(ebp.code(), reinterpret_cast<intptr_t>(frame->fp())); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 182 | for (int i = 0; i < XMMRegister::kMaxNumRegisters; i++) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 183 | input_->SetDoubleRegister(i, 0.0); |
| 184 | } |
| 185 | |
| 186 | // Fill the frame content from the actual data on the frame. |
| 187 | for (unsigned i = 0; i < input_->GetFrameSize(); i += kPointerSize) { |
| 188 | input_->SetFrameSlot(i, Memory::uint32_at(tos + i)); |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 193 | void Deoptimizer::SetPlatformCompiledStubRegisters( |
| 194 | FrameDescription* output_frame, CodeStubDescriptor* descriptor) { |
| 195 | intptr_t handler = |
| 196 | reinterpret_cast<intptr_t>(descriptor->deoptimization_handler()); |
| 197 | int params = descriptor->GetHandlerParameterCount(); |
| 198 | output_frame->SetRegister(eax.code(), params); |
| 199 | output_frame->SetRegister(ebx.code(), handler); |
| 200 | } |
| 201 | |
| 202 | |
| 203 | void Deoptimizer::CopyDoubleRegisters(FrameDescription* output_frame) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 204 | for (int i = 0; i < XMMRegister::kMaxNumRegisters; ++i) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 205 | double double_value = input_->GetDoubleRegister(i); |
| 206 | output_frame->SetDoubleRegister(i, double_value); |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | |
| 211 | bool Deoptimizer::HasAlignmentPadding(JSFunction* function) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 212 | int parameter_count = |
| 213 | function->shared()->internal_formal_parameter_count() + 1; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 214 | unsigned input_frame_size = input_->GetFrameSize(); |
| 215 | unsigned alignment_state_offset = |
| 216 | input_frame_size - parameter_count * kPointerSize - |
| 217 | StandardFrameConstants::kFixedFrameSize - |
| 218 | kPointerSize; |
| 219 | DCHECK(JavaScriptFrameConstants::kDynamicAlignmentStateOffset == |
| 220 | JavaScriptFrameConstants::kLocal0Offset); |
| 221 | int32_t alignment_state = input_->GetFrameSlot(alignment_state_offset); |
| 222 | return (alignment_state == kAlignmentPaddingPushed); |
| 223 | } |
| 224 | |
| 225 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 226 | #define __ masm()-> |
| 227 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 228 | void Deoptimizer::TableEntryGenerator::Generate() { |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 229 | GeneratePrologue(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 230 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 231 | // Save all general purpose registers before messing with them. |
| 232 | const int kNumberOfRegisters = Register::kNumRegisters; |
| 233 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 234 | const int kDoubleRegsSize = kDoubleSize * XMMRegister::kMaxNumRegisters; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 235 | __ sub(esp, Immediate(kDoubleRegsSize)); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 236 | const RegisterConfiguration* config = |
| 237 | RegisterConfiguration::ArchDefault(RegisterConfiguration::CRANKSHAFT); |
| 238 | for (int i = 0; i < config->num_allocatable_double_registers(); ++i) { |
| 239 | int code = config->GetAllocatableDoubleCode(i); |
| 240 | XMMRegister xmm_reg = XMMRegister::from_code(code); |
| 241 | int offset = code * kDoubleSize; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 242 | __ movsd(Operand(esp, offset), xmm_reg); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | __ pushad(); |
| 246 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 247 | ExternalReference c_entry_fp_address(Isolate::kCEntryFPAddress, isolate()); |
| 248 | __ mov(Operand::StaticVariable(c_entry_fp_address), ebp); |
| 249 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 250 | const int kSavedRegistersAreaSize = kNumberOfRegisters * kPointerSize + |
| 251 | kDoubleRegsSize; |
| 252 | |
| 253 | // Get the bailout id from the stack. |
| 254 | __ mov(ebx, Operand(esp, kSavedRegistersAreaSize)); |
| 255 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 256 | // Get the address of the location in the code object |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 257 | // and compute the fp-to-sp delta in register edx. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 258 | __ mov(ecx, Operand(esp, kSavedRegistersAreaSize + 1 * kPointerSize)); |
| 259 | __ lea(edx, Operand(esp, kSavedRegistersAreaSize + 2 * kPointerSize)); |
| 260 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 261 | __ sub(edx, ebp); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 262 | __ neg(edx); |
| 263 | |
| 264 | // Allocate a new deoptimizer object. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 265 | __ PrepareCallCFunction(6, eax); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 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. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 272 | __ mov(Operand(esp, 5 * kPointerSize), |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 273 | Immediate(ExternalReference::isolate_address(isolate()))); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 274 | { |
| 275 | AllowExternalCallThatCantCauseGC scope(masm()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 276 | __ CallCFunction(ExternalReference::new_deoptimizer_function(isolate()), 6); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 277 | } |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 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. |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 284 | for (int i = kNumberOfRegisters - 1; i >= 0; i--) { |
| 285 | int offset = (i * kPointerSize) + FrameDescription::registers_offset(); |
| 286 | __ pop(Operand(ebx, offset)); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 287 | } |
| 288 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 289 | int double_regs_offset = FrameDescription::double_registers_offset(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 290 | // Fill in the double input registers. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 291 | for (int i = 0; i < config->num_allocatable_double_registers(); ++i) { |
| 292 | int code = config->GetAllocatableDoubleCode(i); |
| 293 | int dst_offset = code * kDoubleSize + double_regs_offset; |
| 294 | int src_offset = code * kDoubleSize; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 295 | __ movsd(xmm0, Operand(esp, src_offset)); |
| 296 | __ movsd(Operand(ebx, dst_offset), xmm0); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 297 | } |
| 298 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 299 | // Clear FPU all exceptions. |
| 300 | // TODO(ulan): Find out why the TOP register is not zero here in some cases, |
| 301 | // and check that the generated code never deoptimizes with unbalanced stack. |
| 302 | __ fnclex(); |
| 303 | |
| 304 | // Remove the bailout id, return address and the double registers. |
| 305 | __ add(esp, Immediate(kDoubleRegsSize + 2 * kPointerSize)); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 306 | |
| 307 | // Compute a pointer to the unwinding limit in register ecx; that is |
| 308 | // the first stack slot not part of the input frame. |
| 309 | __ mov(ecx, Operand(ebx, FrameDescription::frame_size_offset())); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 310 | __ add(ecx, esp); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 311 | |
| 312 | // Unwind the stack down to - but not including - the unwinding |
| 313 | // limit and copy the contents of the activation frame to the input |
| 314 | // frame description. |
| 315 | __ lea(edx, Operand(ebx, FrameDescription::frame_content_offset())); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 316 | Label pop_loop_header; |
| 317 | __ jmp(&pop_loop_header); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 318 | Label pop_loop; |
| 319 | __ bind(&pop_loop); |
| 320 | __ pop(Operand(edx, 0)); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 321 | __ add(edx, Immediate(sizeof(uint32_t))); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 322 | __ bind(&pop_loop_header); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 323 | __ cmp(ecx, esp); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 324 | __ j(not_equal, &pop_loop); |
| 325 | |
| 326 | // Compute the output frame in the deoptimizer. |
| 327 | __ push(eax); |
| 328 | __ PrepareCallCFunction(1, ebx); |
| 329 | __ mov(Operand(esp, 0 * kPointerSize), eax); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 330 | { |
| 331 | AllowExternalCallThatCantCauseGC scope(masm()); |
| 332 | __ CallCFunction( |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 333 | ExternalReference::compute_output_frames_function(isolate()), 1); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 334 | } |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 335 | __ pop(eax); |
| 336 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 337 | // If frame was dynamically aligned, pop padding. |
| 338 | Label no_padding; |
| 339 | __ cmp(Operand(eax, Deoptimizer::has_alignment_padding_offset()), |
| 340 | Immediate(0)); |
| 341 | __ j(equal, &no_padding); |
| 342 | __ pop(ecx); |
| 343 | if (FLAG_debug_code) { |
| 344 | __ cmp(ecx, Immediate(kAlignmentZapValue)); |
| 345 | __ Assert(equal, kAlignmentMarkerExpected); |
| 346 | } |
| 347 | __ bind(&no_padding); |
| 348 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 349 | // Replace the current frame with the output frames. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 350 | Label outer_push_loop, inner_push_loop, |
| 351 | outer_loop_header, inner_loop_header; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 352 | // Outer loop state: eax = current FrameDescription**, edx = one past the |
| 353 | // last FrameDescription**. |
| 354 | __ mov(edx, Operand(eax, Deoptimizer::output_count_offset())); |
| 355 | __ mov(eax, Operand(eax, Deoptimizer::output_offset())); |
| 356 | __ lea(edx, Operand(eax, edx, times_4, 0)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 357 | __ jmp(&outer_loop_header); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 358 | __ bind(&outer_push_loop); |
| 359 | // Inner loop state: ebx = current FrameDescription*, ecx = loop index. |
| 360 | __ mov(ebx, Operand(eax, 0)); |
| 361 | __ mov(ecx, Operand(ebx, FrameDescription::frame_size_offset())); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 362 | __ jmp(&inner_loop_header); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 363 | __ bind(&inner_push_loop); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 364 | __ sub(ecx, Immediate(sizeof(uint32_t))); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 365 | __ push(Operand(ebx, ecx, times_1, FrameDescription::frame_content_offset())); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 366 | __ bind(&inner_loop_header); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 367 | __ test(ecx, ecx); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 368 | __ j(not_zero, &inner_push_loop); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 369 | __ add(eax, Immediate(kPointerSize)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 370 | __ bind(&outer_loop_header); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 371 | __ cmp(eax, edx); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 372 | __ j(below, &outer_push_loop); |
| 373 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 374 | // In case of a failed STUB, we have to restore the XMM registers. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 375 | for (int i = 0; i < config->num_allocatable_double_registers(); ++i) { |
| 376 | int code = config->GetAllocatableDoubleCode(i); |
| 377 | XMMRegister xmm_reg = XMMRegister::from_code(code); |
| 378 | int src_offset = code * kDoubleSize + double_regs_offset; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 379 | __ movsd(xmm_reg, Operand(ebx, src_offset)); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | // Push state, pc, and continuation from the last output frame. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 383 | __ push(Operand(ebx, FrameDescription::state_offset())); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 384 | __ push(Operand(ebx, FrameDescription::pc_offset())); |
| 385 | __ push(Operand(ebx, FrameDescription::continuation_offset())); |
| 386 | |
| 387 | |
| 388 | // Push the registers from the last output frame. |
| 389 | for (int i = 0; i < kNumberOfRegisters; i++) { |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 390 | int offset = (i * kPointerSize) + FrameDescription::registers_offset(); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 391 | __ push(Operand(ebx, offset)); |
| 392 | } |
| 393 | |
| 394 | // Restore the registers from the stack. |
| 395 | __ popad(); |
| 396 | |
| 397 | // Return to the continuation point. |
| 398 | __ ret(0); |
| 399 | } |
| 400 | |
| 401 | |
| 402 | void Deoptimizer::TableEntryGenerator::GeneratePrologue() { |
| 403 | // Create a sequence of deoptimization entries. |
| 404 | Label done; |
| 405 | for (int i = 0; i < count(); i++) { |
| 406 | int start = masm()->pc_offset(); |
| 407 | USE(start); |
| 408 | __ push_imm32(i); |
| 409 | __ jmp(&done); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 410 | DCHECK(masm()->pc_offset() - start == table_entry_size_); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 411 | } |
| 412 | __ bind(&done); |
| 413 | } |
| 414 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 415 | |
| 416 | void FrameDescription::SetCallerPc(unsigned offset, intptr_t value) { |
| 417 | SetFrameSlot(offset, value); |
| 418 | } |
| 419 | |
| 420 | |
| 421 | void FrameDescription::SetCallerFp(unsigned offset, intptr_t value) { |
| 422 | SetFrameSlot(offset, value); |
| 423 | } |
| 424 | |
| 425 | |
| 426 | void FrameDescription::SetCallerConstantPool(unsigned offset, intptr_t value) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 427 | // No embedded constant pool support. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 428 | UNREACHABLE(); |
| 429 | } |
| 430 | |
| 431 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 432 | #undef __ |
| 433 | |
| 434 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 435 | } // namespace internal |
| 436 | } // namespace v8 |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 437 | |
| 438 | #endif // V8_TARGET_ARCH_IA32 |