Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1 | // Copyright 2013 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. |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 4 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5 | #include "src/deoptimizer.h" |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 6 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7 | #include "src/accessors.h" |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 8 | #include "src/ast/prettyprinter.h" |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9 | #include "src/codegen.h" |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10 | #include "src/disasm.h" |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 11 | #include "src/frames-inl.h" |
| 12 | #include "src/full-codegen/full-codegen.h" |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 13 | #include "src/global-handles.h" |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 14 | #include "src/interpreter/interpreter.h" |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 15 | #include "src/macro-assembler.h" |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 16 | #include "src/profiler/cpu-profiler.h" |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 17 | #include "src/tracing/trace-event.h" |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 18 | #include "src/v8.h" |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 19 | |
| 20 | |
| 21 | namespace v8 { |
| 22 | namespace internal { |
| 23 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 24 | static MemoryChunk* AllocateCodeChunk(MemoryAllocator* allocator) { |
| 25 | return allocator->AllocateChunk(Deoptimizer::GetMaxDeoptTableSize(), |
| 26 | base::OS::CommitPageSize(), |
| 27 | #if defined(__native_client__) |
| 28 | // The Native Client port of V8 uses an interpreter, |
| 29 | // so code pages don't need PROT_EXEC. |
| 30 | NOT_EXECUTABLE, |
| 31 | #else |
| 32 | EXECUTABLE, |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 33 | #endif |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 34 | NULL); |
| 35 | } |
| 36 | |
| 37 | |
| 38 | DeoptimizerData::DeoptimizerData(MemoryAllocator* allocator) |
| 39 | : allocator_(allocator), |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 40 | current_(NULL) { |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 41 | for (int i = 0; i <= Deoptimizer::kLastBailoutType; ++i) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 42 | deopt_entry_code_entries_[i] = -1; |
| 43 | deopt_entry_code_[i] = AllocateCodeChunk(allocator); |
| 44 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 45 | } |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 46 | |
| 47 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 48 | DeoptimizerData::~DeoptimizerData() { |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 49 | for (int i = 0; i <= Deoptimizer::kLastBailoutType; ++i) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 50 | allocator_->Free(deopt_entry_code_[i]); |
| 51 | deopt_entry_code_[i] = NULL; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 52 | } |
| 53 | } |
| 54 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 55 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 56 | Code* Deoptimizer::FindDeoptimizingCode(Address addr) { |
| 57 | if (function_->IsHeapObject()) { |
| 58 | // Search all deoptimizing code in the native context of the function. |
| 59 | Context* native_context = function_->context()->native_context(); |
| 60 | Object* element = native_context->DeoptimizedCodeListHead(); |
| 61 | while (!element->IsUndefined()) { |
| 62 | Code* code = Code::cast(element); |
| 63 | CHECK(code->kind() == Code::OPTIMIZED_FUNCTION); |
| 64 | if (code->contains(addr)) return code; |
| 65 | element = code->next_code_link(); |
| 66 | } |
| 67 | } |
| 68 | return NULL; |
| 69 | } |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 70 | |
| 71 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 72 | // We rely on this function not causing a GC. It is called from generated code |
| 73 | // without having a real stack frame in place. |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 74 | Deoptimizer* Deoptimizer::New(JSFunction* function, |
| 75 | BailoutType type, |
| 76 | unsigned bailout_id, |
| 77 | Address from, |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 78 | int fp_to_sp_delta, |
| 79 | Isolate* isolate) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 80 | Deoptimizer* deoptimizer = new Deoptimizer(isolate, |
| 81 | function, |
| 82 | type, |
| 83 | bailout_id, |
| 84 | from, |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 85 | fp_to_sp_delta, |
| 86 | NULL); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 87 | CHECK(isolate->deoptimizer_data()->current_ == NULL); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 88 | isolate->deoptimizer_data()->current_ = deoptimizer; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 89 | return deoptimizer; |
| 90 | } |
| 91 | |
| 92 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 93 | // No larger than 2K on all platforms |
| 94 | static const int kDeoptTableMaxEpilogueCodeSize = 2 * KB; |
| 95 | |
| 96 | |
| 97 | size_t Deoptimizer::GetMaxDeoptTableSize() { |
| 98 | int entries_size = |
| 99 | Deoptimizer::kMaxNumberOfEntries * Deoptimizer::table_entry_size_; |
| 100 | int commit_page_size = static_cast<int>(base::OS::CommitPageSize()); |
| 101 | int page_count = ((kDeoptTableMaxEpilogueCodeSize + entries_size - 1) / |
| 102 | commit_page_size) + 1; |
| 103 | return static_cast<size_t>(commit_page_size * page_count); |
| 104 | } |
| 105 | |
| 106 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 107 | Deoptimizer* Deoptimizer::Grab(Isolate* isolate) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 108 | Deoptimizer* result = isolate->deoptimizer_data()->current_; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 109 | CHECK_NOT_NULL(result); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 110 | result->DeleteFrameDescriptions(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 111 | isolate->deoptimizer_data()->current_ = NULL; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 112 | return result; |
| 113 | } |
| 114 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 115 | |
| 116 | int Deoptimizer::ConvertJSFrameIndexToFrameIndex(int jsframe_index) { |
| 117 | if (jsframe_index == 0) return 0; |
| 118 | |
| 119 | int frame_index = 0; |
| 120 | while (jsframe_index >= 0) { |
| 121 | FrameDescription* frame = output_[frame_index]; |
| 122 | if (frame->GetFrameType() == StackFrame::JAVA_SCRIPT) { |
| 123 | jsframe_index--; |
| 124 | } |
| 125 | frame_index++; |
| 126 | } |
| 127 | |
| 128 | return frame_index - 1; |
| 129 | } |
| 130 | |
| 131 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 132 | DeoptimizedFrameInfo* Deoptimizer::DebuggerInspectableFrame( |
| 133 | JavaScriptFrame* frame, |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 134 | int jsframe_index, |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 135 | Isolate* isolate) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 136 | CHECK(frame->is_optimized()); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 137 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 138 | TranslatedState translated_values(frame); |
| 139 | translated_values.Prepare(false, frame->fp()); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 140 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 141 | TranslatedState::iterator frame_it = translated_values.end(); |
| 142 | int counter = jsframe_index; |
| 143 | for (auto it = translated_values.begin(); it != translated_values.end(); |
| 144 | it++) { |
| 145 | if (it->kind() == TranslatedFrame::kFunction || |
| 146 | it->kind() == TranslatedFrame::kInterpretedFunction) { |
| 147 | if (counter == 0) { |
| 148 | frame_it = it; |
| 149 | break; |
| 150 | } |
| 151 | counter--; |
| 152 | } |
| 153 | } |
| 154 | CHECK(frame_it != translated_values.end()); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 155 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 156 | DeoptimizedFrameInfo* info = |
| 157 | new DeoptimizedFrameInfo(&translated_values, frame_it, isolate); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 158 | |
| 159 | return info; |
| 160 | } |
| 161 | |
| 162 | |
| 163 | void Deoptimizer::DeleteDebuggerInspectableFrame(DeoptimizedFrameInfo* info, |
| 164 | Isolate* isolate) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 165 | delete info; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 166 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 167 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 168 | |
| 169 | void Deoptimizer::GenerateDeoptimizationEntries(MacroAssembler* masm, |
| 170 | int count, |
| 171 | BailoutType type) { |
| 172 | TableEntryGenerator generator(masm, type, count); |
| 173 | generator.Generate(); |
| 174 | } |
| 175 | |
| 176 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 177 | void Deoptimizer::VisitAllOptimizedFunctionsForContext( |
| 178 | Context* context, OptimizedFunctionVisitor* visitor) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 179 | DisallowHeapAllocation no_allocation; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 180 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 181 | CHECK(context->IsNativeContext()); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 182 | |
| 183 | visitor->EnterContext(context); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 184 | |
| 185 | // Visit the list of optimized functions, removing elements that |
| 186 | // no longer refer to optimized code. |
| 187 | JSFunction* prev = NULL; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 188 | Object* element = context->OptimizedFunctionsListHead(); |
| 189 | while (!element->IsUndefined()) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 190 | JSFunction* function = JSFunction::cast(element); |
| 191 | Object* next = function->next_function_link(); |
| 192 | if (function->code()->kind() != Code::OPTIMIZED_FUNCTION || |
| 193 | (visitor->VisitFunction(function), |
| 194 | function->code()->kind() != Code::OPTIMIZED_FUNCTION)) { |
| 195 | // The function no longer refers to optimized code, or the visitor |
| 196 | // changed the code to which it refers to no longer be optimized code. |
| 197 | // Remove the function from this list. |
| 198 | if (prev != NULL) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 199 | prev->set_next_function_link(next, UPDATE_WEAK_WRITE_BARRIER); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 200 | } else { |
| 201 | context->SetOptimizedFunctionsListHead(next); |
| 202 | } |
| 203 | // The visitor should not alter the link directly. |
| 204 | CHECK_EQ(function->next_function_link(), next); |
| 205 | // Set the next function link to undefined to indicate it is no longer |
| 206 | // in the optimized functions list. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 207 | function->set_next_function_link(context->GetHeap()->undefined_value(), |
| 208 | SKIP_WRITE_BARRIER); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 209 | } else { |
| 210 | // The visitor should not alter the link directly. |
| 211 | CHECK_EQ(function->next_function_link(), next); |
| 212 | // preserve this element. |
| 213 | prev = function; |
| 214 | } |
| 215 | element = next; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 216 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 217 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 218 | visitor->LeaveContext(context); |
| 219 | } |
| 220 | |
| 221 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 222 | void Deoptimizer::VisitAllOptimizedFunctions( |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 223 | Isolate* isolate, |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 224 | OptimizedFunctionVisitor* visitor) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 225 | DisallowHeapAllocation no_allocation; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 226 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 227 | // Run through the list of all native contexts. |
| 228 | Object* context = isolate->heap()->native_contexts_list(); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 229 | while (!context->IsUndefined()) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 230 | VisitAllOptimizedFunctionsForContext(Context::cast(context), visitor); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 231 | context = Context::cast(context)->get(Context::NEXT_CONTEXT_LINK); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 232 | } |
| 233 | } |
| 234 | |
| 235 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 236 | // Unlink functions referring to code marked for deoptimization, then move |
| 237 | // marked code from the optimized code list to the deoptimized code list, |
| 238 | // and patch code for lazy deopt. |
| 239 | void Deoptimizer::DeoptimizeMarkedCodeForContext(Context* context) { |
| 240 | DisallowHeapAllocation no_allocation; |
| 241 | |
| 242 | // A "closure" that unlinks optimized code that is going to be |
| 243 | // deoptimized from the functions that refer to it. |
| 244 | class SelectedCodeUnlinker: public OptimizedFunctionVisitor { |
| 245 | public: |
| 246 | virtual void EnterContext(Context* context) { } // Don't care. |
| 247 | virtual void LeaveContext(Context* context) { } // Don't care. |
| 248 | virtual void VisitFunction(JSFunction* function) { |
| 249 | Code* code = function->code(); |
| 250 | if (!code->marked_for_deoptimization()) return; |
| 251 | |
| 252 | // Unlink this function and evict from optimized code map. |
| 253 | SharedFunctionInfo* shared = function->shared(); |
| 254 | function->set_code(shared->code()); |
| 255 | |
| 256 | if (FLAG_trace_deopt) { |
| 257 | CodeTracer::Scope scope(code->GetHeap()->isolate()->GetCodeTracer()); |
| 258 | PrintF(scope.file(), "[deoptimizer unlinked: "); |
| 259 | function->PrintName(scope.file()); |
| 260 | PrintF(scope.file(), |
| 261 | " / %" V8PRIxPTR "]\n", reinterpret_cast<intptr_t>(function)); |
| 262 | } |
| 263 | } |
| 264 | }; |
| 265 | |
| 266 | // Unlink all functions that refer to marked code. |
| 267 | SelectedCodeUnlinker unlinker; |
| 268 | VisitAllOptimizedFunctionsForContext(context, &unlinker); |
| 269 | |
| 270 | Isolate* isolate = context->GetHeap()->isolate(); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 271 | #ifdef DEBUG |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 272 | Code* topmost_optimized_code = NULL; |
| 273 | bool safe_to_deopt_topmost_optimized_code = false; |
| 274 | // Make sure all activations of optimized code can deopt at their current PC. |
| 275 | // The topmost optimized code has special handling because it cannot be |
| 276 | // deoptimized due to weak object dependency. |
| 277 | for (StackFrameIterator it(isolate, isolate->thread_local_top()); |
| 278 | !it.done(); it.Advance()) { |
| 279 | StackFrame::Type type = it.frame()->type(); |
| 280 | if (type == StackFrame::OPTIMIZED) { |
| 281 | Code* code = it.frame()->LookupCode(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 282 | JSFunction* function = |
| 283 | static_cast<OptimizedFrame*>(it.frame())->function(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 284 | if (FLAG_trace_deopt) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 285 | CodeTracer::Scope scope(isolate->GetCodeTracer()); |
| 286 | PrintF(scope.file(), "[deoptimizer found activation of function: "); |
| 287 | function->PrintName(scope.file()); |
| 288 | PrintF(scope.file(), |
| 289 | " / %" V8PRIxPTR "]\n", reinterpret_cast<intptr_t>(function)); |
| 290 | } |
| 291 | SafepointEntry safepoint = code->GetSafepointEntry(it.frame()->pc()); |
| 292 | int deopt_index = safepoint.deoptimization_index(); |
| 293 | // Turbofan deopt is checked when we are patching addresses on stack. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 294 | bool turbofanned = code->is_turbofanned() && |
| 295 | function->shared()->asm_function() && |
| 296 | !FLAG_turbo_asm_deoptimization; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 297 | bool safe_to_deopt = |
| 298 | deopt_index != Safepoint::kNoDeoptimizationIndex || turbofanned; |
| 299 | CHECK(topmost_optimized_code == NULL || safe_to_deopt || turbofanned); |
| 300 | if (topmost_optimized_code == NULL) { |
| 301 | topmost_optimized_code = code; |
| 302 | safe_to_deopt_topmost_optimized_code = safe_to_deopt; |
| 303 | } |
| 304 | } |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 305 | } |
| 306 | #endif |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 307 | |
| 308 | // Move marked code from the optimized code list to the deoptimized |
| 309 | // code list, collecting them into a ZoneList. |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 310 | Zone zone(isolate->allocator()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 311 | ZoneList<Code*> codes(10, &zone); |
| 312 | |
| 313 | // Walk over all optimized code objects in this native context. |
| 314 | Code* prev = NULL; |
| 315 | Object* element = context->OptimizedCodeListHead(); |
| 316 | while (!element->IsUndefined()) { |
| 317 | Code* code = Code::cast(element); |
| 318 | CHECK_EQ(code->kind(), Code::OPTIMIZED_FUNCTION); |
| 319 | Object* next = code->next_code_link(); |
| 320 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 321 | if (code->marked_for_deoptimization()) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 322 | // Put the code into the list for later patching. |
| 323 | codes.Add(code, &zone); |
| 324 | |
| 325 | if (prev != NULL) { |
| 326 | // Skip this code in the optimized code list. |
| 327 | prev->set_next_code_link(next); |
| 328 | } else { |
| 329 | // There was no previous node, the next node is the new head. |
| 330 | context->SetOptimizedCodeListHead(next); |
| 331 | } |
| 332 | |
| 333 | // Move the code to the _deoptimized_ code list. |
| 334 | code->set_next_code_link(context->DeoptimizedCodeListHead()); |
| 335 | context->SetDeoptimizedCodeListHead(code); |
| 336 | } else { |
| 337 | // Not marked; preserve this element. |
| 338 | prev = code; |
| 339 | } |
| 340 | element = next; |
| 341 | } |
| 342 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 343 | // We need a handle scope only because of the macro assembler, |
| 344 | // which is used in code patching in EnsureCodeForDeoptimizationEntry. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 345 | HandleScope scope(isolate); |
| 346 | |
| 347 | // Now patch all the codes for deoptimization. |
| 348 | for (int i = 0; i < codes.length(); i++) { |
| 349 | #ifdef DEBUG |
| 350 | if (codes[i] == topmost_optimized_code) { |
| 351 | DCHECK(safe_to_deopt_topmost_optimized_code); |
| 352 | } |
| 353 | #endif |
| 354 | // It is finally time to die, code object. |
| 355 | |
| 356 | // Remove the code from optimized code map. |
| 357 | DeoptimizationInputData* deopt_data = |
| 358 | DeoptimizationInputData::cast(codes[i]->deoptimization_data()); |
| 359 | SharedFunctionInfo* shared = |
| 360 | SharedFunctionInfo::cast(deopt_data->SharedFunctionInfo()); |
| 361 | shared->EvictFromOptimizedCodeMap(codes[i], "deoptimized code"); |
| 362 | |
| 363 | // Do platform-specific patching to force any activations to lazy deopt. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 364 | PatchCodeForDeoptimization(isolate, codes[i]); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 365 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 366 | // We might be in the middle of incremental marking with compaction. |
| 367 | // Tell collector to treat this code object in a special way and |
| 368 | // ignore all slots that might have been recorded on it. |
| 369 | isolate->heap()->mark_compact_collector()->InvalidateCode(codes[i]); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 370 | } |
| 371 | } |
| 372 | |
| 373 | |
| 374 | void Deoptimizer::DeoptimizeAll(Isolate* isolate) { |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 375 | TimerEventScope<TimerEventDeoptimizeCode> timer(isolate); |
| 376 | TRACE_EVENT0("v8", "V8.DeoptimizeCode"); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 377 | if (FLAG_trace_deopt) { |
| 378 | CodeTracer::Scope scope(isolate->GetCodeTracer()); |
| 379 | PrintF(scope.file(), "[deoptimize all code in all contexts]\n"); |
| 380 | } |
| 381 | DisallowHeapAllocation no_allocation; |
| 382 | // For all contexts, mark all code, then deoptimize. |
| 383 | Object* context = isolate->heap()->native_contexts_list(); |
| 384 | while (!context->IsUndefined()) { |
| 385 | Context* native_context = Context::cast(context); |
| 386 | MarkAllCodeForContext(native_context); |
| 387 | DeoptimizeMarkedCodeForContext(native_context); |
| 388 | context = native_context->get(Context::NEXT_CONTEXT_LINK); |
| 389 | } |
| 390 | } |
| 391 | |
| 392 | |
| 393 | void Deoptimizer::DeoptimizeMarkedCode(Isolate* isolate) { |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 394 | TimerEventScope<TimerEventDeoptimizeCode> timer(isolate); |
| 395 | TRACE_EVENT0("v8", "V8.DeoptimizeCode"); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 396 | if (FLAG_trace_deopt) { |
| 397 | CodeTracer::Scope scope(isolate->GetCodeTracer()); |
| 398 | PrintF(scope.file(), "[deoptimize marked code in all contexts]\n"); |
| 399 | } |
| 400 | DisallowHeapAllocation no_allocation; |
| 401 | // For all contexts, deoptimize code already marked. |
| 402 | Object* context = isolate->heap()->native_contexts_list(); |
| 403 | while (!context->IsUndefined()) { |
| 404 | Context* native_context = Context::cast(context); |
| 405 | DeoptimizeMarkedCodeForContext(native_context); |
| 406 | context = native_context->get(Context::NEXT_CONTEXT_LINK); |
| 407 | } |
| 408 | } |
| 409 | |
| 410 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 411 | void Deoptimizer::MarkAllCodeForContext(Context* context) { |
| 412 | Object* element = context->OptimizedCodeListHead(); |
| 413 | while (!element->IsUndefined()) { |
| 414 | Code* code = Code::cast(element); |
| 415 | CHECK_EQ(code->kind(), Code::OPTIMIZED_FUNCTION); |
| 416 | code->set_marked_for_deoptimization(true); |
| 417 | element = code->next_code_link(); |
| 418 | } |
| 419 | } |
| 420 | |
| 421 | |
| 422 | void Deoptimizer::DeoptimizeFunction(JSFunction* function) { |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 423 | TimerEventScope<TimerEventDeoptimizeCode> timer(function->GetIsolate()); |
| 424 | TRACE_EVENT0("v8", "V8.DeoptimizeCode"); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 425 | Code* code = function->code(); |
| 426 | if (code->kind() == Code::OPTIMIZED_FUNCTION) { |
| 427 | // Mark the code for deoptimization and unlink any functions that also |
| 428 | // refer to that code. The code cannot be shared across native contexts, |
| 429 | // so we only need to search one. |
| 430 | code->set_marked_for_deoptimization(true); |
| 431 | DeoptimizeMarkedCodeForContext(function->context()->native_context()); |
| 432 | } |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 436 | void Deoptimizer::ComputeOutputFrames(Deoptimizer* deoptimizer) { |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 437 | deoptimizer->DoComputeOutputFrames(); |
| 438 | } |
| 439 | |
| 440 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 441 | bool Deoptimizer::TraceEnabledFor(BailoutType deopt_type, |
| 442 | StackFrame::Type frame_type) { |
| 443 | switch (deopt_type) { |
| 444 | case EAGER: |
| 445 | case SOFT: |
| 446 | case LAZY: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 447 | return (frame_type == StackFrame::STUB) |
| 448 | ? FLAG_trace_stub_failures |
| 449 | : FLAG_trace_deopt; |
| 450 | } |
| 451 | FATAL("Unsupported deopt type"); |
| 452 | return false; |
| 453 | } |
| 454 | |
| 455 | |
| 456 | const char* Deoptimizer::MessageFor(BailoutType type) { |
| 457 | switch (type) { |
| 458 | case EAGER: return "eager"; |
| 459 | case SOFT: return "soft"; |
| 460 | case LAZY: return "lazy"; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 461 | } |
| 462 | FATAL("Unsupported deopt type"); |
| 463 | return NULL; |
| 464 | } |
| 465 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 466 | Deoptimizer::Deoptimizer(Isolate* isolate, JSFunction* function, |
| 467 | BailoutType type, unsigned bailout_id, Address from, |
| 468 | int fp_to_sp_delta, Code* optimized_code) |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 469 | : isolate_(isolate), |
| 470 | function_(function), |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 471 | bailout_id_(bailout_id), |
| 472 | bailout_type_(type), |
| 473 | from_(from), |
| 474 | fp_to_sp_delta_(fp_to_sp_delta), |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 475 | deoptimizing_throw_(false), |
| 476 | catch_handler_data_(-1), |
| 477 | catch_handler_pc_offset_(-1), |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 478 | input_(nullptr), |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 479 | output_count_(0), |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 480 | jsframe_count_(0), |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 481 | output_(nullptr), |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 482 | caller_frame_top_(0), |
| 483 | caller_fp_(0), |
| 484 | caller_pc_(0), |
| 485 | caller_constant_pool_(0), |
| 486 | input_frame_context_(0), |
| 487 | stack_fp_(0), |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 488 | trace_scope_(nullptr) { |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 489 | if (isolate->deoptimizer_lazy_throw()) { |
| 490 | isolate->set_deoptimizer_lazy_throw(false); |
| 491 | deoptimizing_throw_ = true; |
| 492 | } |
| 493 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 494 | // For COMPILED_STUBs called from builtins, the function pointer is a SMI |
| 495 | // indicating an internal frame. |
| 496 | if (function->IsSmi()) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 497 | function = nullptr; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 498 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 499 | DCHECK(from != nullptr); |
| 500 | if (function != nullptr && function->IsOptimized()) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 501 | function->shared()->increment_deopt_count(); |
| 502 | if (bailout_type_ == Deoptimizer::SOFT) { |
| 503 | isolate->counters()->soft_deopts_executed()->Increment(); |
| 504 | // Soft deopts shouldn't count against the overall re-optimization count |
| 505 | // that can eventually lead to disabling optimization for a function. |
| 506 | int opt_count = function->shared()->opt_count(); |
| 507 | if (opt_count > 0) opt_count--; |
| 508 | function->shared()->set_opt_count(opt_count); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 509 | } |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 510 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 511 | compiled_code_ = FindOptimizedCode(function, optimized_code); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 512 | #if DEBUG |
| 513 | DCHECK(compiled_code_ != NULL); |
| 514 | if (type == EAGER || type == SOFT || type == LAZY) { |
| 515 | DCHECK(compiled_code_->kind() != Code::FUNCTION); |
| 516 | } |
| 517 | #endif |
| 518 | |
| 519 | StackFrame::Type frame_type = function == NULL |
| 520 | ? StackFrame::STUB |
| 521 | : StackFrame::JAVA_SCRIPT; |
| 522 | trace_scope_ = TraceEnabledFor(type, frame_type) ? |
| 523 | new CodeTracer::Scope(isolate->GetCodeTracer()) : NULL; |
| 524 | #ifdef DEBUG |
| 525 | CHECK(AllowHeapAllocation::IsAllowed()); |
| 526 | disallow_heap_allocation_ = new DisallowHeapAllocation(); |
| 527 | #endif // DEBUG |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 528 | if (compiled_code_->kind() == Code::OPTIMIZED_FUNCTION) { |
| 529 | PROFILE(isolate_, CodeDeoptEvent(compiled_code_, from_, fp_to_sp_delta_)); |
| 530 | } |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 531 | unsigned size = ComputeInputFrameSize(); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 532 | int parameter_count = |
| 533 | function == nullptr |
| 534 | ? 0 |
| 535 | : (function->shared()->internal_formal_parameter_count() + 1); |
| 536 | input_ = new (size) FrameDescription(size, parameter_count); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 537 | input_->SetFrameType(frame_type); |
| 538 | } |
| 539 | |
| 540 | |
| 541 | Code* Deoptimizer::FindOptimizedCode(JSFunction* function, |
| 542 | Code* optimized_code) { |
| 543 | switch (bailout_type_) { |
| 544 | case Deoptimizer::SOFT: |
| 545 | case Deoptimizer::EAGER: |
| 546 | case Deoptimizer::LAZY: { |
| 547 | Code* compiled_code = FindDeoptimizingCode(from_); |
| 548 | return (compiled_code == NULL) |
| 549 | ? static_cast<Code*>(isolate_->FindCodeObject(from_)) |
| 550 | : compiled_code; |
| 551 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 552 | } |
| 553 | FATAL("Could not find code for optimized function"); |
| 554 | return NULL; |
| 555 | } |
| 556 | |
| 557 | |
| 558 | void Deoptimizer::PrintFunctionName() { |
| 559 | if (function_->IsJSFunction()) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 560 | function_->ShortPrint(trace_scope_->file()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 561 | } else { |
| 562 | PrintF(trace_scope_->file(), |
| 563 | "%s", Code::Kind2String(compiled_code_->kind())); |
| 564 | } |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 565 | } |
| 566 | |
| 567 | |
| 568 | Deoptimizer::~Deoptimizer() { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 569 | DCHECK(input_ == NULL && output_ == NULL); |
| 570 | DCHECK(disallow_heap_allocation_ == NULL); |
| 571 | delete trace_scope_; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 572 | } |
| 573 | |
| 574 | |
| 575 | void Deoptimizer::DeleteFrameDescriptions() { |
| 576 | delete input_; |
| 577 | for (int i = 0; i < output_count_; ++i) { |
| 578 | if (output_[i] != input_) delete output_[i]; |
| 579 | } |
| 580 | delete[] output_; |
| 581 | input_ = NULL; |
| 582 | output_ = NULL; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 583 | #ifdef DEBUG |
| 584 | CHECK(!AllowHeapAllocation::IsAllowed()); |
| 585 | CHECK(disallow_heap_allocation_ != NULL); |
| 586 | delete disallow_heap_allocation_; |
| 587 | disallow_heap_allocation_ = NULL; |
| 588 | #endif // DEBUG |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 589 | } |
| 590 | |
| 591 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 592 | Address Deoptimizer::GetDeoptimizationEntry(Isolate* isolate, |
| 593 | int id, |
| 594 | BailoutType type, |
| 595 | GetEntryMode mode) { |
| 596 | CHECK_GE(id, 0); |
| 597 | if (id >= kMaxNumberOfEntries) return NULL; |
| 598 | if (mode == ENSURE_ENTRY_CODE) { |
| 599 | EnsureCodeForDeoptimizationEntry(isolate, type, id); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 600 | } else { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 601 | CHECK_EQ(mode, CALCULATE_ENTRY_ADDRESS); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 602 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 603 | DeoptimizerData* data = isolate->deoptimizer_data(); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 604 | CHECK_LE(type, kLastBailoutType); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 605 | MemoryChunk* base = data->deopt_entry_code_[type]; |
| 606 | return base->area_start() + (id * table_entry_size_); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 607 | } |
| 608 | |
| 609 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 610 | int Deoptimizer::GetDeoptimizationId(Isolate* isolate, |
| 611 | Address addr, |
| 612 | BailoutType type) { |
| 613 | DeoptimizerData* data = isolate->deoptimizer_data(); |
| 614 | MemoryChunk* base = data->deopt_entry_code_[type]; |
| 615 | Address start = base->area_start(); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 616 | if (addr < start || |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 617 | addr >= start + (kMaxNumberOfEntries * table_entry_size_)) { |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 618 | return kNotDeoptimizationEntry; |
| 619 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 620 | DCHECK_EQ(0, |
| 621 | static_cast<int>(addr - start) % table_entry_size_); |
| 622 | return static_cast<int>(addr - start) / table_entry_size_; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 623 | } |
| 624 | |
| 625 | |
Steve Block | 9fac840 | 2011-05-12 15:51:54 +0100 | [diff] [blame] | 626 | int Deoptimizer::GetOutputInfo(DeoptimizationOutputData* data, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 627 | BailoutId id, |
Steve Block | 9fac840 | 2011-05-12 15:51:54 +0100 | [diff] [blame] | 628 | SharedFunctionInfo* shared) { |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 629 | // TODO(kasperl): For now, we do a simple linear search for the PC |
| 630 | // offset associated with the given node id. This should probably be |
| 631 | // changed to a binary search. |
| 632 | int length = data->DeoptPoints(); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 633 | for (int i = 0; i < length; i++) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 634 | if (data->AstId(i) == id) { |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 635 | return data->PcAndState(i)->value(); |
| 636 | } |
| 637 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 638 | OFStream os(stderr); |
| 639 | os << "[couldn't find pc offset for node=" << id.ToInt() << "]\n" |
| 640 | << "[method: " << shared->DebugName()->ToCString().get() << "]\n" |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 641 | << "[source:\n" << SourceCodeOf(shared) << "\n]" << std::endl; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 642 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 643 | shared->GetHeap()->isolate()->PushStackTraceAndDie(0xfefefefe, data, shared, |
| 644 | 0xfefefeff); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 645 | FATAL("unable to find pc offset during deoptimization"); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 646 | return -1; |
| 647 | } |
| 648 | |
| 649 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 650 | int Deoptimizer::GetDeoptimizedCodeCount(Isolate* isolate) { |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 651 | int length = 0; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 652 | // Count all entries in the deoptimizing code list of every context. |
| 653 | Object* context = isolate->heap()->native_contexts_list(); |
| 654 | while (!context->IsUndefined()) { |
| 655 | Context* native_context = Context::cast(context); |
| 656 | Object* element = native_context->DeoptimizedCodeListHead(); |
| 657 | while (!element->IsUndefined()) { |
| 658 | Code* code = Code::cast(element); |
| 659 | DCHECK(code->kind() == Code::OPTIMIZED_FUNCTION); |
| 660 | length++; |
| 661 | element = code->next_code_link(); |
| 662 | } |
| 663 | context = Context::cast(context)->get(Context::NEXT_CONTEXT_LINK); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 664 | } |
| 665 | return length; |
| 666 | } |
| 667 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 668 | namespace { |
| 669 | |
| 670 | int LookupCatchHandler(TranslatedFrame* translated_frame, int* data_out) { |
| 671 | switch (translated_frame->kind()) { |
| 672 | case TranslatedFrame::kFunction: { |
| 673 | BailoutId node_id = translated_frame->node_id(); |
| 674 | JSFunction* function = |
| 675 | JSFunction::cast(translated_frame->begin()->GetRawValue()); |
| 676 | Code* non_optimized_code = function->shared()->code(); |
| 677 | FixedArray* raw_data = non_optimized_code->deoptimization_data(); |
| 678 | DeoptimizationOutputData* data = DeoptimizationOutputData::cast(raw_data); |
| 679 | unsigned pc_and_state = |
| 680 | Deoptimizer::GetOutputInfo(data, node_id, function->shared()); |
| 681 | unsigned pc_offset = FullCodeGenerator::PcField::decode(pc_and_state); |
| 682 | HandlerTable* table = |
| 683 | HandlerTable::cast(non_optimized_code->handler_table()); |
| 684 | HandlerTable::CatchPrediction prediction; |
| 685 | return table->LookupRange(pc_offset, data_out, &prediction); |
| 686 | } |
| 687 | case TranslatedFrame::kInterpretedFunction: { |
| 688 | int bytecode_offset = translated_frame->node_id().ToInt(); |
| 689 | JSFunction* function = |
| 690 | JSFunction::cast(translated_frame->begin()->GetRawValue()); |
| 691 | BytecodeArray* bytecode = function->shared()->bytecode_array(); |
| 692 | HandlerTable* table = HandlerTable::cast(bytecode->handler_table()); |
| 693 | HandlerTable::CatchPrediction prediction; |
| 694 | return table->LookupRange(bytecode_offset, data_out, &prediction); |
| 695 | } |
| 696 | default: |
| 697 | break; |
| 698 | } |
| 699 | return -1; |
| 700 | } |
| 701 | |
| 702 | } // namespace |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 703 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 704 | // We rely on this function not causing a GC. It is called from generated code |
| 705 | // without having a real stack frame in place. |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 706 | void Deoptimizer::DoComputeOutputFrames() { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 707 | base::ElapsedTimer timer; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 708 | |
| 709 | // Determine basic deoptimization information. The optimized frame is |
| 710 | // described by the input data. |
| 711 | DeoptimizationInputData* input_data = |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 712 | DeoptimizationInputData::cast(compiled_code_->deoptimization_data()); |
| 713 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 714 | { |
| 715 | // Read caller's PC, caller's FP and caller's constant pool values |
| 716 | // from input frame. Compute caller's frame top address. |
| 717 | |
| 718 | Register fp_reg = JavaScriptFrame::fp_register(); |
| 719 | stack_fp_ = input_->GetRegister(fp_reg.code()); |
| 720 | |
| 721 | caller_frame_top_ = stack_fp_ + ComputeInputFrameAboveFpFixedSize(); |
| 722 | |
| 723 | Address fp_address = input_->GetFramePointerAddress(); |
| 724 | caller_fp_ = Memory::intptr_at(fp_address); |
| 725 | caller_pc_ = |
| 726 | Memory::intptr_at(fp_address + CommonFrameConstants::kCallerPCOffset); |
| 727 | input_frame_context_ = Memory::intptr_at( |
| 728 | fp_address + CommonFrameConstants::kContextOrFrameTypeOffset); |
| 729 | |
| 730 | if (FLAG_enable_embedded_constant_pool) { |
| 731 | caller_constant_pool_ = Memory::intptr_at( |
| 732 | fp_address + CommonFrameConstants::kConstantPoolOffset); |
| 733 | } |
| 734 | } |
| 735 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 736 | if (trace_scope_ != NULL) { |
| 737 | timer.Start(); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 738 | PrintF(trace_scope_->file(), "[deoptimizing (DEOPT %s): begin ", |
| 739 | MessageFor(bailout_type_)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 740 | PrintFunctionName(); |
| 741 | PrintF(trace_scope_->file(), |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 742 | " (opt #%d) @%d, FP to SP delta: %d, caller sp: 0x%08" V8PRIxPTR |
| 743 | "]\n", |
| 744 | input_data->OptimizationId()->value(), bailout_id_, fp_to_sp_delta_, |
| 745 | caller_frame_top_); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 746 | if (bailout_type_ == EAGER || bailout_type_ == SOFT || |
| 747 | (compiled_code_->is_hydrogen_stub())) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 748 | compiled_code_->PrintDeoptLocation(trace_scope_->file(), from_); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 749 | } |
| 750 | } |
| 751 | |
| 752 | BailoutId node_id = input_data->AstId(bailout_id_); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 753 | ByteArray* translations = input_data->TranslationByteArray(); |
| 754 | unsigned translation_index = |
| 755 | input_data->TranslationIndex(bailout_id_)->value(); |
| 756 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 757 | TranslationIterator state_iterator(translations, translation_index); |
| 758 | translated_state_.Init( |
| 759 | input_->GetFramePointerAddress(), &state_iterator, |
| 760 | input_data->LiteralArray(), input_->GetRegisterValues(), |
| 761 | trace_scope_ == nullptr ? nullptr : trace_scope_->file()); |
| 762 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 763 | // Do the input frame to output frame(s) translation. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 764 | size_t count = translated_state_.frames().size(); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 765 | // If we are supposed to go to the catch handler, find the catching frame |
| 766 | // for the catch and make sure we only deoptimize upto that frame. |
| 767 | if (deoptimizing_throw_) { |
| 768 | size_t catch_handler_frame_index = count; |
| 769 | for (size_t i = count; i-- > 0;) { |
| 770 | catch_handler_pc_offset_ = LookupCatchHandler( |
| 771 | &(translated_state_.frames()[i]), &catch_handler_data_); |
| 772 | if (catch_handler_pc_offset_ >= 0) { |
| 773 | catch_handler_frame_index = i; |
| 774 | break; |
| 775 | } |
| 776 | } |
| 777 | CHECK_LT(catch_handler_frame_index, count); |
| 778 | count = catch_handler_frame_index + 1; |
| 779 | } |
| 780 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 781 | DCHECK(output_ == NULL); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 782 | output_ = new FrameDescription*[count]; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 783 | for (size_t i = 0; i < count; ++i) { |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 784 | output_[i] = NULL; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 785 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 786 | output_count_ = static_cast<int>(count); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 787 | |
| 788 | // Translate each output frame. |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 789 | int frame_index = 0; // output_frame_index |
| 790 | for (size_t i = 0; i < count; ++i, ++frame_index) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 791 | // Read the ast node id, function, and frame height for this output frame. |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 792 | TranslatedFrame* translated_frame = &(translated_state_.frames()[i]); |
| 793 | switch (translated_frame->kind()) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 794 | case TranslatedFrame::kFunction: |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 795 | DoComputeJSFrame(translated_frame, frame_index, |
| 796 | deoptimizing_throw_ && i == count - 1); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 797 | jsframe_count_++; |
| 798 | break; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 799 | case TranslatedFrame::kInterpretedFunction: |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 800 | DoComputeInterpretedFrame(translated_frame, frame_index, |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 801 | deoptimizing_throw_ && i == count - 1); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 802 | jsframe_count_++; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 803 | break; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 804 | case TranslatedFrame::kArgumentsAdaptor: |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 805 | DoComputeArgumentsAdaptorFrame(translated_frame, frame_index); |
| 806 | break; |
| 807 | case TranslatedFrame::kTailCallerFunction: |
| 808 | DoComputeTailCallerFrame(translated_frame, frame_index); |
| 809 | // Tail caller frame translations do not produce output frames. |
| 810 | frame_index--; |
| 811 | output_count_--; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 812 | break; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 813 | case TranslatedFrame::kConstructStub: |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 814 | DoComputeConstructStubFrame(translated_frame, frame_index); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 815 | break; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 816 | case TranslatedFrame::kGetter: |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 817 | DoComputeAccessorStubFrame(translated_frame, frame_index, false); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 818 | break; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 819 | case TranslatedFrame::kSetter: |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 820 | DoComputeAccessorStubFrame(translated_frame, frame_index, true); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 821 | break; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 822 | case TranslatedFrame::kCompiledStub: |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 823 | DoComputeCompiledStubFrame(translated_frame, frame_index); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 824 | break; |
| 825 | case TranslatedFrame::kInvalid: |
| 826 | FATAL("invalid frame"); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 827 | break; |
| 828 | } |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 829 | } |
| 830 | |
| 831 | // Print some helpful diagnostic information. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 832 | if (trace_scope_ != NULL) { |
| 833 | double ms = timer.Elapsed().InMillisecondsF(); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 834 | int index = output_count_ - 1; // Index of the topmost frame. |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 835 | PrintF(trace_scope_->file(), "[deoptimizing (%s): end ", |
| 836 | MessageFor(bailout_type_)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 837 | PrintFunctionName(); |
| 838 | PrintF(trace_scope_->file(), |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 839 | " @%d => node=%d, pc=0x%08" V8PRIxPTR ", caller sp=0x%08" V8PRIxPTR |
| 840 | ", state=%s, took %0.3f ms]\n", |
| 841 | bailout_id_, node_id.ToInt(), output_[index]->GetPc(), |
| 842 | caller_frame_top_, FullCodeGenerator::State2String( |
| 843 | static_cast<FullCodeGenerator::State>( |
| 844 | output_[index]->GetState()->value())), |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 845 | ms); |
| 846 | } |
| 847 | } |
| 848 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 849 | void Deoptimizer::DoComputeJSFrame(TranslatedFrame* translated_frame, |
| 850 | int frame_index, bool goto_catch_handler) { |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 851 | SharedFunctionInfo* shared = translated_frame->raw_shared_info(); |
| 852 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 853 | TranslatedFrame::iterator value_iterator = translated_frame->begin(); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 854 | bool is_bottommost = (0 == frame_index); |
| 855 | bool is_topmost = (output_count_ - 1 == frame_index); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 856 | int input_index = 0; |
| 857 | |
| 858 | BailoutId node_id = translated_frame->node_id(); |
| 859 | unsigned height = |
| 860 | translated_frame->height() - 1; // Do not count the context. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 861 | unsigned height_in_bytes = height * kPointerSize; |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 862 | if (goto_catch_handler) { |
| 863 | // Take the stack height from the handler table. |
| 864 | height = catch_handler_data_; |
| 865 | // We also make space for the exception itself. |
| 866 | height_in_bytes = (height + 1) * kPointerSize; |
| 867 | CHECK(is_topmost); |
| 868 | } |
| 869 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 870 | JSFunction* function = JSFunction::cast(value_iterator->GetRawValue()); |
| 871 | value_iterator++; |
| 872 | input_index++; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 873 | if (trace_scope_ != NULL) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 874 | PrintF(trace_scope_->file(), " translating frame "); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 875 | base::SmartArrayPointer<char> name = shared->DebugName()->ToCString(); |
| 876 | PrintF(trace_scope_->file(), "%s", name.get()); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 877 | PrintF(trace_scope_->file(), " => node=%d, height=%d%s\n", node_id.ToInt(), |
| 878 | height_in_bytes, goto_catch_handler ? " (throw)" : ""); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 879 | } |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 880 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 881 | // The 'fixed' part of the frame consists of the incoming parameters and |
| 882 | // the part described by JavaScriptFrameConstants. |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 883 | unsigned fixed_frame_size = ComputeJavascriptFixedSize(shared); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 884 | unsigned output_frame_size = height_in_bytes + fixed_frame_size; |
| 885 | |
| 886 | // Allocate and store the output frame description. |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 887 | int parameter_count = shared->internal_formal_parameter_count() + 1; |
| 888 | FrameDescription* output_frame = new (output_frame_size) |
| 889 | FrameDescription(output_frame_size, parameter_count); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 890 | output_frame->SetFrameType(StackFrame::JAVA_SCRIPT); |
| 891 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 892 | CHECK(frame_index >= 0 && frame_index < output_count_); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 893 | CHECK_NULL(output_[frame_index]); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 894 | output_[frame_index] = output_frame; |
| 895 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 896 | // The top address of the frame is computed from the previous frame's top and |
| 897 | // this frame's size. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 898 | intptr_t top_address; |
| 899 | if (is_bottommost) { |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 900 | top_address = caller_frame_top_ - output_frame_size; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 901 | } else { |
| 902 | top_address = output_[frame_index - 1]->GetTop() - output_frame_size; |
| 903 | } |
| 904 | output_frame->SetTop(top_address); |
| 905 | |
| 906 | // Compute the incoming parameter translation. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 907 | unsigned output_offset = output_frame_size; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 908 | for (int i = 0; i < parameter_count; ++i) { |
| 909 | output_offset -= kPointerSize; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 910 | WriteTranslatedValueToOutput(&value_iterator, &input_index, frame_index, |
| 911 | output_offset); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 912 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 913 | |
| 914 | // There are no translation commands for the caller's pc and fp, the |
| 915 | // context, and the function. Synthesize their values and set them up |
| 916 | // explicitly. |
| 917 | // |
| 918 | // The caller's pc for the bottommost output frame is the same as in the |
| 919 | // input frame. For all subsequent output frames, it can be read from the |
| 920 | // previous one. This frame's pc can be computed from the non-optimized |
| 921 | // function code and AST id of the bailout. |
| 922 | output_offset -= kPCOnStackSize; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 923 | intptr_t value; |
| 924 | if (is_bottommost) { |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 925 | value = caller_pc_; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 926 | } else { |
| 927 | value = output_[frame_index - 1]->GetPc(); |
| 928 | } |
| 929 | output_frame->SetCallerPc(output_offset, value); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 930 | DebugPrintOutputSlot(value, frame_index, output_offset, "caller's pc\n"); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 931 | |
| 932 | // The caller's frame pointer for the bottommost output frame is the same |
| 933 | // as in the input frame. For all subsequent output frames, it can be |
| 934 | // read from the previous one. Also compute and set this frame's frame |
| 935 | // pointer. |
| 936 | output_offset -= kFPOnStackSize; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 937 | if (is_bottommost) { |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 938 | value = caller_fp_; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 939 | } else { |
| 940 | value = output_[frame_index - 1]->GetFp(); |
| 941 | } |
| 942 | output_frame->SetCallerFp(output_offset, value); |
| 943 | intptr_t fp_value = top_address + output_offset; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 944 | output_frame->SetFp(fp_value); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 945 | if (is_topmost) { |
| 946 | Register fp_reg = JavaScriptFrame::fp_register(); |
| 947 | output_frame->SetRegister(fp_reg.code(), fp_value); |
| 948 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 949 | DebugPrintOutputSlot(value, frame_index, output_offset, "caller's fp\n"); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 950 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 951 | if (FLAG_enable_embedded_constant_pool) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 952 | // For the bottommost output frame the constant pool pointer can be gotten |
| 953 | // from the input frame. For subsequent output frames, it can be read from |
| 954 | // the previous frame. |
| 955 | output_offset -= kPointerSize; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 956 | if (is_bottommost) { |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 957 | value = caller_constant_pool_; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 958 | } else { |
| 959 | value = output_[frame_index - 1]->GetConstantPool(); |
| 960 | } |
| 961 | output_frame->SetCallerConstantPool(output_offset, value); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 962 | DebugPrintOutputSlot(value, frame_index, output_offset, |
| 963 | "caller's constant_pool\n"); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 964 | } |
| 965 | |
| 966 | // For the bottommost output frame the context can be gotten from the input |
| 967 | // frame. For all subsequent output frames it can be gotten from the function |
| 968 | // so long as we don't inline functions that need local contexts. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 969 | output_offset -= kPointerSize; |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 970 | |
| 971 | TranslatedFrame::iterator context_pos = value_iterator; |
| 972 | int context_input_index = input_index; |
| 973 | // When deoptimizing into a catch block, we need to take the context |
| 974 | // from just above the top of the operand stack (we push the context |
| 975 | // at the entry of the try block). |
| 976 | if (goto_catch_handler) { |
| 977 | for (unsigned i = 0; i < height + 1; ++i) { |
| 978 | context_pos++; |
| 979 | context_input_index++; |
| 980 | } |
| 981 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 982 | // Read the context from the translations. |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 983 | Object* context = context_pos->GetRawValue(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 984 | if (context == isolate_->heap()->undefined_value()) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 985 | // If the context was optimized away, just use the context from |
| 986 | // the activation. This should only apply to Crankshaft code. |
| 987 | CHECK(!compiled_code_->is_turbofanned()); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 988 | context = is_bottommost ? reinterpret_cast<Object*>(input_frame_context_) |
| 989 | : function->context(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 990 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 991 | value = reinterpret_cast<intptr_t>(context); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 992 | output_frame->SetContext(value); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 993 | if (is_topmost) { |
| 994 | Register context_reg = JavaScriptFrame::context_register(); |
| 995 | output_frame->SetRegister(context_reg.code(), value); |
| 996 | } |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 997 | WriteValueToOutput(context, context_input_index, frame_index, output_offset, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 998 | "context "); |
| 999 | if (context == isolate_->heap()->arguments_marker()) { |
| 1000 | Address output_address = |
| 1001 | reinterpret_cast<Address>(output_[frame_index]->GetTop()) + |
| 1002 | output_offset; |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 1003 | values_to_materialize_.push_back({output_address, context_pos}); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1004 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1005 | value_iterator++; |
| 1006 | input_index++; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1007 | |
| 1008 | // The function was mentioned explicitly in the BEGIN_FRAME. |
| 1009 | output_offset -= kPointerSize; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1010 | value = reinterpret_cast<intptr_t>(function); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1011 | WriteValueToOutput(function, 0, frame_index, output_offset, "function "); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1012 | |
| 1013 | // Translate the rest of the frame. |
| 1014 | for (unsigned i = 0; i < height; ++i) { |
| 1015 | output_offset -= kPointerSize; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1016 | WriteTranslatedValueToOutput(&value_iterator, &input_index, frame_index, |
| 1017 | output_offset); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1018 | } |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 1019 | if (goto_catch_handler) { |
| 1020 | // Write out the exception for the catch handler. |
| 1021 | output_offset -= kPointerSize; |
| 1022 | Object* exception_obj = reinterpret_cast<Object*>( |
| 1023 | input_->GetRegister(FullCodeGenerator::result_register().code())); |
| 1024 | WriteValueToOutput(exception_obj, input_index, frame_index, output_offset, |
| 1025 | "exception "); |
| 1026 | input_index++; |
| 1027 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1028 | CHECK_EQ(0u, output_offset); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1029 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1030 | // Update constant pool. |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 1031 | Code* non_optimized_code = shared->code(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1032 | if (FLAG_enable_embedded_constant_pool) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1033 | intptr_t constant_pool_value = |
| 1034 | reinterpret_cast<intptr_t>(non_optimized_code->constant_pool()); |
| 1035 | output_frame->SetConstantPool(constant_pool_value); |
| 1036 | if (is_topmost) { |
| 1037 | Register constant_pool_reg = |
| 1038 | JavaScriptFrame::constant_pool_pointer_register(); |
| 1039 | output_frame->SetRegister(constant_pool_reg.code(), constant_pool_value); |
| 1040 | } |
| 1041 | } |
| 1042 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 1043 | // Compute this frame's PC, state, and continuation. |
| 1044 | FixedArray* raw_data = non_optimized_code->deoptimization_data(); |
| 1045 | DeoptimizationOutputData* data = DeoptimizationOutputData::cast(raw_data); |
| 1046 | Address start = non_optimized_code->instruction_start(); |
| 1047 | unsigned pc_and_state = GetOutputInfo(data, node_id, function->shared()); |
| 1048 | unsigned pc_offset = goto_catch_handler |
| 1049 | ? catch_handler_pc_offset_ |
| 1050 | : FullCodeGenerator::PcField::decode(pc_and_state); |
| 1051 | intptr_t pc_value = reinterpret_cast<intptr_t>(start + pc_offset); |
| 1052 | output_frame->SetPc(pc_value); |
| 1053 | |
| 1054 | // If we are going to the catch handler, then the exception lives in |
| 1055 | // the accumulator. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1056 | FullCodeGenerator::State state = |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 1057 | goto_catch_handler ? FullCodeGenerator::TOS_REG |
| 1058 | : FullCodeGenerator::StateField::decode(pc_and_state); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1059 | output_frame->SetState(Smi::FromInt(state)); |
| 1060 | |
| 1061 | // Set the continuation for the topmost frame. |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 1062 | if (is_topmost) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1063 | Builtins* builtins = isolate_->builtins(); |
| 1064 | Code* continuation = builtins->builtin(Builtins::kNotifyDeoptimized); |
| 1065 | if (bailout_type_ == LAZY) { |
| 1066 | continuation = builtins->builtin(Builtins::kNotifyLazyDeoptimized); |
| 1067 | } else if (bailout_type_ == SOFT) { |
| 1068 | continuation = builtins->builtin(Builtins::kNotifySoftDeoptimized); |
| 1069 | } else { |
| 1070 | CHECK_EQ(bailout_type_, EAGER); |
| 1071 | } |
| 1072 | output_frame->SetContinuation( |
| 1073 | reinterpret_cast<intptr_t>(continuation->entry())); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 1074 | } |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 1075 | } |
| 1076 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 1077 | void Deoptimizer::DoComputeInterpretedFrame(TranslatedFrame* translated_frame, |
| 1078 | int frame_index, |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 1079 | bool goto_catch_handler) { |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 1080 | SharedFunctionInfo* shared = translated_frame->raw_shared_info(); |
| 1081 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1082 | TranslatedFrame::iterator value_iterator = translated_frame->begin(); |
| 1083 | int input_index = 0; |
| 1084 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 1085 | int bytecode_offset = translated_frame->node_id().ToInt(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1086 | unsigned height = translated_frame->height(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1087 | unsigned height_in_bytes = height * kPointerSize; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1088 | JSFunction* function = JSFunction::cast(value_iterator->GetRawValue()); |
| 1089 | value_iterator++; |
| 1090 | input_index++; |
| 1091 | if (trace_scope_ != NULL) { |
| 1092 | PrintF(trace_scope_->file(), " translating interpreted frame "); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 1093 | base::SmartArrayPointer<char> name = shared->DebugName()->ToCString(); |
| 1094 | PrintF(trace_scope_->file(), "%s", name.get()); |
| 1095 | PrintF(trace_scope_->file(), " => bytecode_offset=%d, height=%d%s\n", |
| 1096 | bytecode_offset, height_in_bytes, |
| 1097 | goto_catch_handler ? " (throw)" : ""); |
| 1098 | } |
| 1099 | if (goto_catch_handler) { |
| 1100 | bytecode_offset = catch_handler_pc_offset_; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1101 | } |
| 1102 | |
| 1103 | // The 'fixed' part of the frame consists of the incoming parameters and |
| 1104 | // the part described by InterpreterFrameConstants. |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 1105 | unsigned fixed_frame_size = ComputeInterpretedFixedSize(shared); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1106 | unsigned output_frame_size = height_in_bytes + fixed_frame_size; |
| 1107 | |
| 1108 | // Allocate and store the output frame description. |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 1109 | int parameter_count = shared->internal_formal_parameter_count() + 1; |
| 1110 | FrameDescription* output_frame = new (output_frame_size) |
| 1111 | FrameDescription(output_frame_size, parameter_count); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1112 | output_frame->SetFrameType(StackFrame::INTERPRETED); |
| 1113 | |
| 1114 | bool is_bottommost = (0 == frame_index); |
| 1115 | bool is_topmost = (output_count_ - 1 == frame_index); |
| 1116 | CHECK(frame_index >= 0 && frame_index < output_count_); |
| 1117 | CHECK_NULL(output_[frame_index]); |
| 1118 | output_[frame_index] = output_frame; |
| 1119 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 1120 | // The top address of the frame is computed from the previous frame's top and |
| 1121 | // this frame's size. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1122 | intptr_t top_address; |
| 1123 | if (is_bottommost) { |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 1124 | top_address = caller_frame_top_ - output_frame_size; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1125 | } else { |
| 1126 | top_address = output_[frame_index - 1]->GetTop() - output_frame_size; |
| 1127 | } |
| 1128 | output_frame->SetTop(top_address); |
| 1129 | |
| 1130 | // Compute the incoming parameter translation. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1131 | unsigned output_offset = output_frame_size; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1132 | for (int i = 0; i < parameter_count; ++i) { |
| 1133 | output_offset -= kPointerSize; |
| 1134 | WriteTranslatedValueToOutput(&value_iterator, &input_index, frame_index, |
| 1135 | output_offset); |
| 1136 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1137 | |
| 1138 | // There are no translation commands for the caller's pc and fp, the |
| 1139 | // context, the function, new.target and the bytecode offset. Synthesize |
| 1140 | // their values and set them up |
| 1141 | // explicitly. |
| 1142 | // |
| 1143 | // The caller's pc for the bottommost output frame is the same as in the |
| 1144 | // input frame. For all subsequent output frames, it can be read from the |
| 1145 | // previous one. This frame's pc can be computed from the non-optimized |
| 1146 | // function code and AST id of the bailout. |
| 1147 | output_offset -= kPCOnStackSize; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1148 | intptr_t value; |
| 1149 | if (is_bottommost) { |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 1150 | value = caller_pc_; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1151 | } else { |
| 1152 | value = output_[frame_index - 1]->GetPc(); |
| 1153 | } |
| 1154 | output_frame->SetCallerPc(output_offset, value); |
| 1155 | DebugPrintOutputSlot(value, frame_index, output_offset, "caller's pc\n"); |
| 1156 | |
| 1157 | // The caller's frame pointer for the bottommost output frame is the same |
| 1158 | // as in the input frame. For all subsequent output frames, it can be |
| 1159 | // read from the previous one. Also compute and set this frame's frame |
| 1160 | // pointer. |
| 1161 | output_offset -= kFPOnStackSize; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1162 | if (is_bottommost) { |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 1163 | value = caller_fp_; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1164 | } else { |
| 1165 | value = output_[frame_index - 1]->GetFp(); |
| 1166 | } |
| 1167 | output_frame->SetCallerFp(output_offset, value); |
| 1168 | intptr_t fp_value = top_address + output_offset; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1169 | output_frame->SetFp(fp_value); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 1170 | if (is_topmost) { |
| 1171 | Register fp_reg = InterpretedFrame::fp_register(); |
| 1172 | output_frame->SetRegister(fp_reg.code(), fp_value); |
| 1173 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1174 | DebugPrintOutputSlot(value, frame_index, output_offset, "caller's fp\n"); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1175 | |
| 1176 | if (FLAG_enable_embedded_constant_pool) { |
| 1177 | // For the bottommost output frame the constant pool pointer can be gotten |
| 1178 | // from the input frame. For subsequent output frames, it can be read from |
| 1179 | // the previous frame. |
| 1180 | output_offset -= kPointerSize; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1181 | if (is_bottommost) { |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 1182 | value = caller_constant_pool_; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1183 | } else { |
| 1184 | value = output_[frame_index - 1]->GetConstantPool(); |
| 1185 | } |
| 1186 | output_frame->SetCallerConstantPool(output_offset, value); |
| 1187 | DebugPrintOutputSlot(value, frame_index, output_offset, |
| 1188 | "caller's constant_pool\n"); |
| 1189 | } |
| 1190 | |
| 1191 | // For the bottommost output frame the context can be gotten from the input |
| 1192 | // frame. For all subsequent output frames it can be gotten from the function |
| 1193 | // so long as we don't inline functions that need local contexts. |
| 1194 | Register context_reg = InterpretedFrame::context_register(); |
| 1195 | output_offset -= kPointerSize; |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 1196 | |
| 1197 | // When deoptimizing into a catch block, we need to take the context |
| 1198 | // from a register that was specified in the handler table. |
| 1199 | TranslatedFrame::iterator context_pos = value_iterator; |
| 1200 | int context_input_index = input_index; |
| 1201 | if (goto_catch_handler) { |
| 1202 | // Skip to the translated value of the register specified |
| 1203 | // in the handler table. |
| 1204 | for (int i = 0; i < catch_handler_data_ + 1; ++i) { |
| 1205 | context_pos++; |
| 1206 | context_input_index++; |
| 1207 | } |
| 1208 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1209 | // Read the context from the translations. |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 1210 | Object* context = context_pos->GetRawValue(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1211 | // The context should not be a placeholder for a materialized object. |
| 1212 | CHECK(context != isolate_->heap()->arguments_marker()); |
| 1213 | value = reinterpret_cast<intptr_t>(context); |
| 1214 | output_frame->SetContext(value); |
| 1215 | if (is_topmost) output_frame->SetRegister(context_reg.code(), value); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 1216 | WriteValueToOutput(context, context_input_index, frame_index, output_offset, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1217 | "context "); |
| 1218 | value_iterator++; |
| 1219 | input_index++; |
| 1220 | |
| 1221 | // The function was mentioned explicitly in the BEGIN_FRAME. |
| 1222 | output_offset -= kPointerSize; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1223 | value = reinterpret_cast<intptr_t>(function); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1224 | WriteValueToOutput(function, 0, frame_index, output_offset, "function "); |
| 1225 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 1226 | // The new.target slot is only used during function activiation which is |
| 1227 | // before the first deopt point, so should never be needed. Just set it to |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1228 | // undefined. |
| 1229 | output_offset -= kPointerSize; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1230 | Object* new_target = isolate_->heap()->undefined_value(); |
| 1231 | WriteValueToOutput(new_target, 0, frame_index, output_offset, "new_target "); |
| 1232 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 1233 | // Set the bytecode array pointer. |
| 1234 | output_offset -= kPointerSize; |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 1235 | Object* bytecode_array = shared->bytecode_array(); |
| 1236 | WriteValueToOutput(bytecode_array, 0, frame_index, output_offset, |
| 1237 | "bytecode array "); |
| 1238 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1239 | // The bytecode offset was mentioned explicitly in the BEGIN_FRAME. |
| 1240 | output_offset -= kPointerSize; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1241 | int raw_bytecode_offset = |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 1242 | BytecodeArray::kHeaderSize - kHeapObjectTag + bytecode_offset; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1243 | Smi* smi_bytecode_offset = Smi::FromInt(raw_bytecode_offset); |
| 1244 | WriteValueToOutput(smi_bytecode_offset, 0, frame_index, output_offset, |
| 1245 | "bytecode offset "); |
| 1246 | |
| 1247 | // Translate the rest of the interpreter registers in the frame. |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 1248 | for (unsigned i = 0; i < height - 1; ++i) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1249 | output_offset -= kPointerSize; |
| 1250 | WriteTranslatedValueToOutput(&value_iterator, &input_index, frame_index, |
| 1251 | output_offset); |
| 1252 | } |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 1253 | |
| 1254 | // Put the accumulator on the stack. It will be popped by the |
| 1255 | // InterpreterNotifyDeopt builtin (possibly after materialization). |
| 1256 | output_offset -= kPointerSize; |
| 1257 | if (goto_catch_handler) { |
| 1258 | // If we are lazy deopting to a catch handler, we set the accumulator to |
| 1259 | // the exception (which lives in the result register). |
| 1260 | intptr_t accumulator_value = |
| 1261 | input_->GetRegister(FullCodeGenerator::result_register().code()); |
| 1262 | WriteValueToOutput(reinterpret_cast<Object*>(accumulator_value), 0, |
| 1263 | frame_index, output_offset, "accumulator "); |
| 1264 | value_iterator++; |
| 1265 | } else { |
| 1266 | WriteTranslatedValueToOutput(&value_iterator, &input_index, frame_index, |
| 1267 | output_offset); |
| 1268 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1269 | CHECK_EQ(0u, output_offset); |
| 1270 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1271 | Builtins* builtins = isolate_->builtins(); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 1272 | Code* dispatch_builtin = |
| 1273 | builtins->builtin(Builtins::kInterpreterEnterBytecodeDispatch); |
| 1274 | output_frame->SetPc(reinterpret_cast<intptr_t>(dispatch_builtin->entry())); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1275 | output_frame->SetState(0); |
| 1276 | |
| 1277 | // Update constant pool. |
| 1278 | if (FLAG_enable_embedded_constant_pool) { |
| 1279 | intptr_t constant_pool_value = |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 1280 | reinterpret_cast<intptr_t>(dispatch_builtin->constant_pool()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1281 | output_frame->SetConstantPool(constant_pool_value); |
| 1282 | if (is_topmost) { |
| 1283 | Register constant_pool_reg = |
| 1284 | InterpretedFrame::constant_pool_pointer_register(); |
| 1285 | output_frame->SetRegister(constant_pool_reg.code(), constant_pool_value); |
| 1286 | } |
| 1287 | } |
| 1288 | |
| 1289 | // Set the continuation for the topmost frame. |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 1290 | if (is_topmost) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1291 | Code* continuation = |
| 1292 | builtins->builtin(Builtins::kInterpreterNotifyDeoptimized); |
| 1293 | if (bailout_type_ == LAZY) { |
| 1294 | continuation = |
| 1295 | builtins->builtin(Builtins::kInterpreterNotifyLazyDeoptimized); |
| 1296 | } else if (bailout_type_ == SOFT) { |
| 1297 | continuation = |
| 1298 | builtins->builtin(Builtins::kInterpreterNotifySoftDeoptimized); |
| 1299 | } else { |
| 1300 | CHECK_EQ(bailout_type_, EAGER); |
| 1301 | } |
| 1302 | output_frame->SetContinuation( |
| 1303 | reinterpret_cast<intptr_t>(continuation->entry())); |
| 1304 | } |
| 1305 | } |
| 1306 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 1307 | void Deoptimizer::DoComputeArgumentsAdaptorFrame( |
| 1308 | TranslatedFrame* translated_frame, int frame_index) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1309 | TranslatedFrame::iterator value_iterator = translated_frame->begin(); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 1310 | bool is_bottommost = (0 == frame_index); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1311 | int input_index = 0; |
| 1312 | |
| 1313 | unsigned height = translated_frame->height(); |
| 1314 | unsigned height_in_bytes = height * kPointerSize; |
| 1315 | JSFunction* function = JSFunction::cast(value_iterator->GetRawValue()); |
| 1316 | value_iterator++; |
| 1317 | input_index++; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1318 | if (trace_scope_ != NULL) { |
| 1319 | PrintF(trace_scope_->file(), |
| 1320 | " translating arguments adaptor => height=%d\n", height_in_bytes); |
| 1321 | } |
| 1322 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 1323 | unsigned fixed_frame_size = ArgumentsAdaptorFrameConstants::kFixedFrameSize; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1324 | unsigned output_frame_size = height_in_bytes + fixed_frame_size; |
| 1325 | |
| 1326 | // Allocate and store the output frame description. |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 1327 | int parameter_count = height; |
| 1328 | FrameDescription* output_frame = new (output_frame_size) |
| 1329 | FrameDescription(output_frame_size, parameter_count); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1330 | output_frame->SetFrameType(StackFrame::ARGUMENTS_ADAPTOR); |
| 1331 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 1332 | // Arguments adaptor can not be topmost. |
| 1333 | CHECK(frame_index < output_count_ - 1); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1334 | CHECK(output_[frame_index] == NULL); |
| 1335 | output_[frame_index] = output_frame; |
| 1336 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 1337 | // The top address of the frame is computed from the previous frame's top and |
| 1338 | // this frame's size. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1339 | intptr_t top_address; |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 1340 | if (is_bottommost) { |
| 1341 | top_address = caller_frame_top_ - output_frame_size; |
| 1342 | } else { |
| 1343 | top_address = output_[frame_index - 1]->GetTop() - output_frame_size; |
| 1344 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1345 | output_frame->SetTop(top_address); |
| 1346 | |
| 1347 | // Compute the incoming parameter translation. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1348 | unsigned output_offset = output_frame_size; |
| 1349 | for (int i = 0; i < parameter_count; ++i) { |
| 1350 | output_offset -= kPointerSize; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1351 | WriteTranslatedValueToOutput(&value_iterator, &input_index, frame_index, |
| 1352 | output_offset); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1353 | } |
| 1354 | |
| 1355 | // Read caller's PC from the previous frame. |
| 1356 | output_offset -= kPCOnStackSize; |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 1357 | intptr_t value; |
| 1358 | if (is_bottommost) { |
| 1359 | value = caller_pc_; |
| 1360 | } else { |
| 1361 | value = output_[frame_index - 1]->GetPc(); |
| 1362 | } |
| 1363 | output_frame->SetCallerPc(output_offset, value); |
| 1364 | DebugPrintOutputSlot(value, frame_index, output_offset, "caller's pc\n"); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1365 | |
| 1366 | // Read caller's FP from the previous frame, and set this frame's FP. |
| 1367 | output_offset -= kFPOnStackSize; |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 1368 | if (is_bottommost) { |
| 1369 | value = caller_fp_; |
| 1370 | } else { |
| 1371 | value = output_[frame_index - 1]->GetFp(); |
| 1372 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1373 | output_frame->SetCallerFp(output_offset, value); |
| 1374 | intptr_t fp_value = top_address + output_offset; |
| 1375 | output_frame->SetFp(fp_value); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1376 | DebugPrintOutputSlot(value, frame_index, output_offset, "caller's fp\n"); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1377 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1378 | if (FLAG_enable_embedded_constant_pool) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1379 | // Read the caller's constant pool from the previous frame. |
| 1380 | output_offset -= kPointerSize; |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 1381 | if (is_bottommost) { |
| 1382 | value = caller_constant_pool_; |
| 1383 | } else { |
| 1384 | value = output_[frame_index - 1]->GetConstantPool(); |
| 1385 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1386 | output_frame->SetCallerConstantPool(output_offset, value); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1387 | DebugPrintOutputSlot(value, frame_index, output_offset, |
| 1388 | "caller's constant_pool\n"); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1389 | } |
| 1390 | |
| 1391 | // A marker value is used in place of the context. |
| 1392 | output_offset -= kPointerSize; |
| 1393 | intptr_t context = reinterpret_cast<intptr_t>( |
| 1394 | Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)); |
| 1395 | output_frame->SetFrameSlot(output_offset, context); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1396 | DebugPrintOutputSlot(context, frame_index, output_offset, |
| 1397 | "context (adaptor sentinel)\n"); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1398 | |
| 1399 | // The function was mentioned explicitly in the ARGUMENTS_ADAPTOR_FRAME. |
| 1400 | output_offset -= kPointerSize; |
| 1401 | value = reinterpret_cast<intptr_t>(function); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1402 | WriteValueToOutput(function, 0, frame_index, output_offset, "function "); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1403 | |
| 1404 | // Number of incoming arguments. |
| 1405 | output_offset -= kPointerSize; |
| 1406 | value = reinterpret_cast<intptr_t>(Smi::FromInt(height - 1)); |
| 1407 | output_frame->SetFrameSlot(output_offset, value); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1408 | DebugPrintOutputSlot(value, frame_index, output_offset, "argc "); |
| 1409 | if (trace_scope_ != nullptr) { |
| 1410 | PrintF(trace_scope_->file(), "(%d)\n", height - 1); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1411 | } |
| 1412 | |
| 1413 | DCHECK(0 == output_offset); |
| 1414 | |
| 1415 | Builtins* builtins = isolate_->builtins(); |
| 1416 | Code* adaptor_trampoline = |
| 1417 | builtins->builtin(Builtins::kArgumentsAdaptorTrampoline); |
| 1418 | intptr_t pc_value = reinterpret_cast<intptr_t>( |
| 1419 | adaptor_trampoline->instruction_start() + |
| 1420 | isolate_->heap()->arguments_adaptor_deopt_pc_offset()->value()); |
| 1421 | output_frame->SetPc(pc_value); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1422 | if (FLAG_enable_embedded_constant_pool) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1423 | intptr_t constant_pool_value = |
| 1424 | reinterpret_cast<intptr_t>(adaptor_trampoline->constant_pool()); |
| 1425 | output_frame->SetConstantPool(constant_pool_value); |
| 1426 | } |
| 1427 | } |
| 1428 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 1429 | void Deoptimizer::DoComputeTailCallerFrame(TranslatedFrame* translated_frame, |
| 1430 | int frame_index) { |
| 1431 | SharedFunctionInfo* shared = translated_frame->raw_shared_info(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1432 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 1433 | bool is_bottommost = (0 == frame_index); |
| 1434 | // Tail caller frame can't be topmost. |
| 1435 | CHECK_NE(output_count_ - 1, frame_index); |
| 1436 | |
| 1437 | if (trace_scope_ != NULL) { |
| 1438 | PrintF(trace_scope_->file(), " translating tail caller frame "); |
| 1439 | base::SmartArrayPointer<char> name = shared->DebugName()->ToCString(); |
| 1440 | PrintF(trace_scope_->file(), "%s\n", name.get()); |
| 1441 | } |
| 1442 | |
| 1443 | if (!is_bottommost) return; |
| 1444 | |
| 1445 | // Drop arguments adaptor frame below current frame if it exsits. |
| 1446 | Address fp_address = input_->GetFramePointerAddress(); |
| 1447 | Address adaptor_fp_address = |
| 1448 | Memory::Address_at(fp_address + CommonFrameConstants::kCallerFPOffset); |
| 1449 | |
| 1450 | if (Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR) != |
| 1451 | Memory::Object_at(adaptor_fp_address + |
| 1452 | CommonFrameConstants::kContextOrFrameTypeOffset)) { |
| 1453 | return; |
| 1454 | } |
| 1455 | |
| 1456 | int caller_params_count = |
| 1457 | Smi::cast( |
| 1458 | Memory::Object_at(adaptor_fp_address + |
| 1459 | ArgumentsAdaptorFrameConstants::kLengthOffset)) |
| 1460 | ->value(); |
| 1461 | |
| 1462 | int callee_params_count = |
| 1463 | function_->shared()->internal_formal_parameter_count(); |
| 1464 | |
| 1465 | // Both caller and callee parameters count do not include receiver. |
| 1466 | int offset = (caller_params_count - callee_params_count) * kPointerSize; |
| 1467 | intptr_t new_stack_fp = |
| 1468 | reinterpret_cast<intptr_t>(adaptor_fp_address) + offset; |
| 1469 | |
| 1470 | intptr_t new_caller_frame_top = new_stack_fp + |
| 1471 | (callee_params_count + 1) * kPointerSize + |
| 1472 | CommonFrameConstants::kFixedFrameSizeAboveFp; |
| 1473 | |
| 1474 | intptr_t adaptor_caller_pc = Memory::intptr_at( |
| 1475 | adaptor_fp_address + CommonFrameConstants::kCallerPCOffset); |
| 1476 | intptr_t adaptor_caller_fp = Memory::intptr_at( |
| 1477 | adaptor_fp_address + CommonFrameConstants::kCallerFPOffset); |
| 1478 | |
| 1479 | if (trace_scope_ != NULL) { |
| 1480 | PrintF(trace_scope_->file(), |
| 1481 | " dropping caller arguments adaptor frame: offset=%d, " |
| 1482 | "fp: 0x%08" V8PRIxPTR " -> 0x%08" V8PRIxPTR |
| 1483 | ", " |
| 1484 | "caller sp: 0x%08" V8PRIxPTR " -> 0x%08" V8PRIxPTR "\n", |
| 1485 | offset, stack_fp_, new_stack_fp, caller_frame_top_, |
| 1486 | new_caller_frame_top); |
| 1487 | } |
| 1488 | caller_frame_top_ = new_caller_frame_top; |
| 1489 | caller_fp_ = adaptor_caller_fp; |
| 1490 | caller_pc_ = adaptor_caller_pc; |
| 1491 | } |
| 1492 | |
| 1493 | void Deoptimizer::DoComputeConstructStubFrame(TranslatedFrame* translated_frame, |
| 1494 | int frame_index) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1495 | TranslatedFrame::iterator value_iterator = translated_frame->begin(); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 1496 | bool is_topmost = (output_count_ - 1 == frame_index); |
| 1497 | // The construct frame could become topmost only if we inlined a constructor |
| 1498 | // call which does a tail call (otherwise the tail callee's frame would be |
| 1499 | // the topmost one). So it could only be the LAZY case. |
| 1500 | CHECK(!is_topmost || bailout_type_ == LAZY); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1501 | int input_index = 0; |
| 1502 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1503 | Builtins* builtins = isolate_->builtins(); |
| 1504 | Code* construct_stub = builtins->builtin(Builtins::kJSConstructStubGeneric); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1505 | unsigned height = translated_frame->height(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1506 | unsigned height_in_bytes = height * kPointerSize; |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 1507 | |
| 1508 | // If the construct frame appears to be topmost we should ensure that the |
| 1509 | // value of result register is preserved during continuation execution. |
| 1510 | // We do this here by "pushing" the result of the constructor function to the |
| 1511 | // top of the reconstructed stack and then using the |
| 1512 | // FullCodeGenerator::TOS_REG machinery. |
| 1513 | if (is_topmost) { |
| 1514 | height_in_bytes += kPointerSize; |
| 1515 | } |
| 1516 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 1517 | // Skip function. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1518 | value_iterator++; |
| 1519 | input_index++; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1520 | if (trace_scope_ != NULL) { |
| 1521 | PrintF(trace_scope_->file(), |
| 1522 | " translating construct stub => height=%d\n", height_in_bytes); |
| 1523 | } |
| 1524 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 1525 | unsigned fixed_frame_size = ConstructFrameConstants::kFixedFrameSize; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1526 | unsigned output_frame_size = height_in_bytes + fixed_frame_size; |
| 1527 | |
| 1528 | // Allocate and store the output frame description. |
| 1529 | FrameDescription* output_frame = |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 1530 | new (output_frame_size) FrameDescription(output_frame_size); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1531 | output_frame->SetFrameType(StackFrame::CONSTRUCT); |
| 1532 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 1533 | // Construct stub can not be topmost. |
| 1534 | DCHECK(frame_index > 0 && frame_index < output_count_); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1535 | DCHECK(output_[frame_index] == NULL); |
| 1536 | output_[frame_index] = output_frame; |
| 1537 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 1538 | // The top address of the frame is computed from the previous frame's top and |
| 1539 | // this frame's size. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1540 | intptr_t top_address; |
| 1541 | top_address = output_[frame_index - 1]->GetTop() - output_frame_size; |
| 1542 | output_frame->SetTop(top_address); |
| 1543 | |
| 1544 | // Compute the incoming parameter translation. |
| 1545 | int parameter_count = height; |
| 1546 | unsigned output_offset = output_frame_size; |
| 1547 | for (int i = 0; i < parameter_count; ++i) { |
| 1548 | output_offset -= kPointerSize; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1549 | // The allocated receiver of a construct stub frame is passed as the |
| 1550 | // receiver parameter through the translation. It might be encoding |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1551 | // a captured object, override the slot address for a captured object. |
| 1552 | WriteTranslatedValueToOutput( |
| 1553 | &value_iterator, &input_index, frame_index, output_offset, nullptr, |
| 1554 | (i == 0) ? reinterpret_cast<Address>(top_address) : nullptr); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1555 | } |
| 1556 | |
| 1557 | // Read caller's PC from the previous frame. |
| 1558 | output_offset -= kPCOnStackSize; |
| 1559 | intptr_t callers_pc = output_[frame_index - 1]->GetPc(); |
| 1560 | output_frame->SetCallerPc(output_offset, callers_pc); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1561 | DebugPrintOutputSlot(callers_pc, frame_index, output_offset, "caller's pc\n"); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1562 | |
| 1563 | // Read caller's FP from the previous frame, and set this frame's FP. |
| 1564 | output_offset -= kFPOnStackSize; |
| 1565 | intptr_t value = output_[frame_index - 1]->GetFp(); |
| 1566 | output_frame->SetCallerFp(output_offset, value); |
| 1567 | intptr_t fp_value = top_address + output_offset; |
| 1568 | output_frame->SetFp(fp_value); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 1569 | if (is_topmost) { |
| 1570 | Register fp_reg = JavaScriptFrame::fp_register(); |
| 1571 | output_frame->SetRegister(fp_reg.code(), fp_value); |
| 1572 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1573 | DebugPrintOutputSlot(value, frame_index, output_offset, "caller's fp\n"); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1574 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1575 | if (FLAG_enable_embedded_constant_pool) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1576 | // Read the caller's constant pool from the previous frame. |
| 1577 | output_offset -= kPointerSize; |
| 1578 | value = output_[frame_index - 1]->GetConstantPool(); |
| 1579 | output_frame->SetCallerConstantPool(output_offset, value); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1580 | DebugPrintOutputSlot(value, frame_index, output_offset, |
| 1581 | "caller's constant_pool\n"); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1582 | } |
| 1583 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 1584 | // A marker value is used to mark the frame. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1585 | output_offset -= kPointerSize; |
| 1586 | value = reinterpret_cast<intptr_t>(Smi::FromInt(StackFrame::CONSTRUCT)); |
| 1587 | output_frame->SetFrameSlot(output_offset, value); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1588 | DebugPrintOutputSlot(value, frame_index, output_offset, |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 1589 | "typed frame marker\n"); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1590 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 1591 | // The context can be gotten from the previous frame. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1592 | output_offset -= kPointerSize; |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 1593 | value = output_[frame_index - 1]->GetContext(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1594 | output_frame->SetFrameSlot(output_offset, value); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 1595 | if (is_topmost) { |
| 1596 | Register context_reg = JavaScriptFrame::context_register(); |
| 1597 | output_frame->SetRegister(context_reg.code(), value); |
| 1598 | } |
| 1599 | DebugPrintOutputSlot(value, frame_index, output_offset, "context\n"); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1600 | |
| 1601 | // The allocation site. |
| 1602 | output_offset -= kPointerSize; |
| 1603 | value = reinterpret_cast<intptr_t>(isolate_->heap()->undefined_value()); |
| 1604 | output_frame->SetFrameSlot(output_offset, value); |
| 1605 | DebugPrintOutputSlot(value, frame_index, output_offset, "allocation site\n"); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1606 | |
| 1607 | // Number of incoming arguments. |
| 1608 | output_offset -= kPointerSize; |
| 1609 | value = reinterpret_cast<intptr_t>(Smi::FromInt(height - 1)); |
| 1610 | output_frame->SetFrameSlot(output_offset, value); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1611 | DebugPrintOutputSlot(value, frame_index, output_offset, "argc "); |
| 1612 | if (trace_scope_ != nullptr) { |
| 1613 | PrintF(trace_scope_->file(), "(%d)\n", height - 1); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1614 | } |
| 1615 | |
| 1616 | // The newly allocated object was passed as receiver in the artificial |
| 1617 | // constructor stub environment created by HEnvironment::CopyForInlining(). |
| 1618 | output_offset -= kPointerSize; |
| 1619 | value = output_frame->GetFrameSlot(output_frame_size - kPointerSize); |
| 1620 | output_frame->SetFrameSlot(output_offset, value); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1621 | DebugPrintOutputSlot(value, frame_index, output_offset, |
| 1622 | "allocated receiver\n"); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1623 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 1624 | if (is_topmost) { |
| 1625 | // Ensure the result is restored back when we return to the stub. |
| 1626 | output_offset -= kPointerSize; |
| 1627 | Register result_reg = FullCodeGenerator::result_register(); |
| 1628 | value = input_->GetRegister(result_reg.code()); |
| 1629 | output_frame->SetFrameSlot(output_offset, value); |
| 1630 | DebugPrintOutputSlot(value, frame_index, output_offset, |
| 1631 | "constructor result\n"); |
| 1632 | |
| 1633 | output_frame->SetState(Smi::FromInt(FullCodeGenerator::TOS_REG)); |
| 1634 | } |
| 1635 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1636 | CHECK_EQ(0u, output_offset); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1637 | |
| 1638 | intptr_t pc = reinterpret_cast<intptr_t>( |
| 1639 | construct_stub->instruction_start() + |
| 1640 | isolate_->heap()->construct_stub_deopt_pc_offset()->value()); |
| 1641 | output_frame->SetPc(pc); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1642 | if (FLAG_enable_embedded_constant_pool) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1643 | intptr_t constant_pool_value = |
| 1644 | reinterpret_cast<intptr_t>(construct_stub->constant_pool()); |
| 1645 | output_frame->SetConstantPool(constant_pool_value); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 1646 | if (is_topmost) { |
| 1647 | Register constant_pool_reg = |
| 1648 | JavaScriptFrame::constant_pool_pointer_register(); |
| 1649 | output_frame->SetRegister(constant_pool_reg.code(), fp_value); |
| 1650 | } |
| 1651 | } |
| 1652 | |
| 1653 | // Set the continuation for the topmost frame. |
| 1654 | if (is_topmost) { |
| 1655 | Builtins* builtins = isolate_->builtins(); |
| 1656 | DCHECK_EQ(LAZY, bailout_type_); |
| 1657 | Code* continuation = builtins->builtin(Builtins::kNotifyLazyDeoptimized); |
| 1658 | output_frame->SetContinuation( |
| 1659 | reinterpret_cast<intptr_t>(continuation->entry())); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1660 | } |
| 1661 | } |
| 1662 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 1663 | void Deoptimizer::DoComputeAccessorStubFrame(TranslatedFrame* translated_frame, |
| 1664 | int frame_index, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1665 | bool is_setter_stub_frame) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1666 | TranslatedFrame::iterator value_iterator = translated_frame->begin(); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 1667 | bool is_topmost = (output_count_ - 1 == frame_index); |
| 1668 | // The accessor frame could become topmost only if we inlined an accessor |
| 1669 | // call which does a tail call (otherwise the tail callee's frame would be |
| 1670 | // the topmost one). So it could only be the LAZY case. |
| 1671 | CHECK(!is_topmost || bailout_type_ == LAZY); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1672 | int input_index = 0; |
| 1673 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 1674 | // Skip accessor. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1675 | value_iterator++; |
| 1676 | input_index++; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1677 | // The receiver (and the implicit return value, if any) are expected in |
| 1678 | // registers by the LoadIC/StoreIC, so they don't belong to the output stack |
| 1679 | // frame. This means that we have to use a height of 0. |
| 1680 | unsigned height = 0; |
| 1681 | unsigned height_in_bytes = height * kPointerSize; |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 1682 | |
| 1683 | // If the accessor frame appears to be topmost we should ensure that the |
| 1684 | // value of result register is preserved during continuation execution. |
| 1685 | // We do this here by "pushing" the result of the accessor function to the |
| 1686 | // top of the reconstructed stack and then using the |
| 1687 | // FullCodeGenerator::TOS_REG machinery. |
| 1688 | // We don't need to restore the result in case of a setter call because we |
| 1689 | // have to return the stored value but not the result of the setter function. |
| 1690 | bool should_preserve_result = is_topmost && !is_setter_stub_frame; |
| 1691 | if (should_preserve_result) { |
| 1692 | height_in_bytes += kPointerSize; |
| 1693 | } |
| 1694 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1695 | const char* kind = is_setter_stub_frame ? "setter" : "getter"; |
| 1696 | if (trace_scope_ != NULL) { |
| 1697 | PrintF(trace_scope_->file(), |
| 1698 | " translating %s stub => height=%u\n", kind, height_in_bytes); |
| 1699 | } |
| 1700 | |
| 1701 | // We need 1 stack entry for the return address and enough entries for the |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 1702 | // StackFrame::INTERNAL (FP, frame type, context, code object and constant |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1703 | // pool (if enabled)- see MacroAssembler::EnterFrame). |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1704 | // For a setter stub frame we need one additional entry for the implicit |
| 1705 | // return value, see StoreStubCompiler::CompileStoreViaSetter. |
| 1706 | unsigned fixed_frame_entries = |
| 1707 | (StandardFrameConstants::kFixedFrameSize / kPointerSize) + 1 + |
| 1708 | (is_setter_stub_frame ? 1 : 0); |
| 1709 | unsigned fixed_frame_size = fixed_frame_entries * kPointerSize; |
| 1710 | unsigned output_frame_size = height_in_bytes + fixed_frame_size; |
| 1711 | |
| 1712 | // Allocate and store the output frame description. |
| 1713 | FrameDescription* output_frame = |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 1714 | new (output_frame_size) FrameDescription(output_frame_size); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1715 | output_frame->SetFrameType(StackFrame::INTERNAL); |
| 1716 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 1717 | // A frame for an accessor stub can not be bottommost. |
| 1718 | CHECK(frame_index > 0 && frame_index < output_count_); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1719 | CHECK_NULL(output_[frame_index]); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1720 | output_[frame_index] = output_frame; |
| 1721 | |
| 1722 | // The top address of the frame is computed from the previous frame's top and |
| 1723 | // this frame's size. |
| 1724 | intptr_t top_address = output_[frame_index - 1]->GetTop() - output_frame_size; |
| 1725 | output_frame->SetTop(top_address); |
| 1726 | |
| 1727 | unsigned output_offset = output_frame_size; |
| 1728 | |
| 1729 | // Read caller's PC from the previous frame. |
| 1730 | output_offset -= kPCOnStackSize; |
| 1731 | intptr_t callers_pc = output_[frame_index - 1]->GetPc(); |
| 1732 | output_frame->SetCallerPc(output_offset, callers_pc); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1733 | DebugPrintOutputSlot(callers_pc, frame_index, output_offset, "caller's pc\n"); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1734 | |
| 1735 | // Read caller's FP from the previous frame, and set this frame's FP. |
| 1736 | output_offset -= kFPOnStackSize; |
| 1737 | intptr_t value = output_[frame_index - 1]->GetFp(); |
| 1738 | output_frame->SetCallerFp(output_offset, value); |
| 1739 | intptr_t fp_value = top_address + output_offset; |
| 1740 | output_frame->SetFp(fp_value); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 1741 | if (is_topmost) { |
| 1742 | Register fp_reg = JavaScriptFrame::fp_register(); |
| 1743 | output_frame->SetRegister(fp_reg.code(), fp_value); |
| 1744 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1745 | DebugPrintOutputSlot(value, frame_index, output_offset, "caller's fp\n"); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1746 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1747 | if (FLAG_enable_embedded_constant_pool) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1748 | // Read the caller's constant pool from the previous frame. |
| 1749 | output_offset -= kPointerSize; |
| 1750 | value = output_[frame_index - 1]->GetConstantPool(); |
| 1751 | output_frame->SetCallerConstantPool(output_offset, value); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1752 | DebugPrintOutputSlot(value, frame_index, output_offset, |
| 1753 | "caller's constant_pool\n"); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1754 | } |
| 1755 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 1756 | // Set the frame type. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1757 | output_offset -= kPointerSize; |
| 1758 | value = reinterpret_cast<intptr_t>(Smi::FromInt(StackFrame::INTERNAL)); |
| 1759 | output_frame->SetFrameSlot(output_offset, value); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 1760 | DebugPrintOutputSlot(value, frame_index, output_offset, "frame type "); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1761 | if (trace_scope_ != nullptr) { |
| 1762 | PrintF(trace_scope_->file(), "(%s sentinel)\n", kind); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1763 | } |
| 1764 | |
| 1765 | // Get Code object from accessor stub. |
| 1766 | output_offset -= kPointerSize; |
| 1767 | Builtins::Name name = is_setter_stub_frame ? |
| 1768 | Builtins::kStoreIC_Setter_ForDeopt : |
| 1769 | Builtins::kLoadIC_Getter_ForDeopt; |
| 1770 | Code* accessor_stub = isolate_->builtins()->builtin(name); |
| 1771 | value = reinterpret_cast<intptr_t>(accessor_stub); |
| 1772 | output_frame->SetFrameSlot(output_offset, value); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1773 | DebugPrintOutputSlot(value, frame_index, output_offset, "code object\n"); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1774 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 1775 | // The context can be gotten from the previous frame. |
| 1776 | output_offset -= kPointerSize; |
| 1777 | value = output_[frame_index - 1]->GetContext(); |
| 1778 | output_frame->SetFrameSlot(output_offset, value); |
| 1779 | if (is_topmost) { |
| 1780 | Register context_reg = JavaScriptFrame::context_register(); |
| 1781 | output_frame->SetRegister(context_reg.code(), value); |
| 1782 | } |
| 1783 | DebugPrintOutputSlot(value, frame_index, output_offset, "context\n"); |
| 1784 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1785 | // Skip receiver. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1786 | value_iterator++; |
| 1787 | input_index++; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1788 | |
| 1789 | if (is_setter_stub_frame) { |
| 1790 | // The implicit return value was part of the artificial setter stub |
| 1791 | // environment. |
| 1792 | output_offset -= kPointerSize; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1793 | WriteTranslatedValueToOutput(&value_iterator, &input_index, frame_index, |
| 1794 | output_offset); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1795 | } |
| 1796 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 1797 | if (should_preserve_result) { |
| 1798 | // Ensure the result is restored back when we return to the stub. |
| 1799 | output_offset -= kPointerSize; |
| 1800 | Register result_reg = FullCodeGenerator::result_register(); |
| 1801 | value = input_->GetRegister(result_reg.code()); |
| 1802 | output_frame->SetFrameSlot(output_offset, value); |
| 1803 | DebugPrintOutputSlot(value, frame_index, output_offset, |
| 1804 | "accessor result\n"); |
| 1805 | |
| 1806 | output_frame->SetState(Smi::FromInt(FullCodeGenerator::TOS_REG)); |
| 1807 | } else { |
| 1808 | output_frame->SetState(Smi::FromInt(FullCodeGenerator::NO_REGISTERS)); |
| 1809 | } |
| 1810 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1811 | CHECK_EQ(0u, output_offset); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1812 | |
| 1813 | Smi* offset = is_setter_stub_frame ? |
| 1814 | isolate_->heap()->setter_stub_deopt_pc_offset() : |
| 1815 | isolate_->heap()->getter_stub_deopt_pc_offset(); |
| 1816 | intptr_t pc = reinterpret_cast<intptr_t>( |
| 1817 | accessor_stub->instruction_start() + offset->value()); |
| 1818 | output_frame->SetPc(pc); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1819 | if (FLAG_enable_embedded_constant_pool) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1820 | intptr_t constant_pool_value = |
| 1821 | reinterpret_cast<intptr_t>(accessor_stub->constant_pool()); |
| 1822 | output_frame->SetConstantPool(constant_pool_value); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 1823 | if (is_topmost) { |
| 1824 | Register constant_pool_reg = |
| 1825 | JavaScriptFrame::constant_pool_pointer_register(); |
| 1826 | output_frame->SetRegister(constant_pool_reg.code(), fp_value); |
| 1827 | } |
| 1828 | } |
| 1829 | |
| 1830 | // Set the continuation for the topmost frame. |
| 1831 | if (is_topmost) { |
| 1832 | Builtins* builtins = isolate_->builtins(); |
| 1833 | DCHECK_EQ(LAZY, bailout_type_); |
| 1834 | Code* continuation = builtins->builtin(Builtins::kNotifyLazyDeoptimized); |
| 1835 | output_frame->SetContinuation( |
| 1836 | reinterpret_cast<intptr_t>(continuation->entry())); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1837 | } |
| 1838 | } |
| 1839 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 1840 | void Deoptimizer::DoComputeCompiledStubFrame(TranslatedFrame* translated_frame, |
| 1841 | int frame_index) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1842 | // |
| 1843 | // FROM TO |
| 1844 | // | .... | | .... | |
| 1845 | // +-------------------------+ +-------------------------+ |
| 1846 | // | JSFunction continuation | | JSFunction continuation | |
| 1847 | // +-------------------------+ +-------------------------+ |
| 1848 | // | | saved frame (FP) | | saved frame (FP) | |
| 1849 | // | +=========================+<-fpreg +=========================+<-fpreg |
| 1850 | // | |constant pool (if ool_cp)| |constant pool (if ool_cp)| |
| 1851 | // | +-------------------------+ +-------------------------| |
| 1852 | // | | JSFunction context | | JSFunction context | |
| 1853 | // v +-------------------------+ +-------------------------| |
| 1854 | // | COMPILED_STUB marker | | STUB_FAILURE marker | |
| 1855 | // +-------------------------+ +-------------------------+ |
| 1856 | // | | | caller args.arguments_ | |
| 1857 | // | ... | +-------------------------+ |
| 1858 | // | | | caller args.length_ | |
| 1859 | // |-------------------------|<-spreg +-------------------------+ |
| 1860 | // | caller args pointer | |
| 1861 | // +-------------------------+ |
| 1862 | // | caller stack param 1 | |
| 1863 | // parameters in registers +-------------------------+ |
| 1864 | // and spilled to stack | .... | |
| 1865 | // +-------------------------+ |
| 1866 | // | caller stack param n | |
| 1867 | // +-------------------------+<-spreg |
| 1868 | // reg = number of parameters |
| 1869 | // reg = failure handler address |
| 1870 | // reg = saved frame |
| 1871 | // reg = JSFunction context |
| 1872 | // |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1873 | // Caller stack params contain the register parameters to the stub first, |
| 1874 | // and then, if the descriptor specifies a constant number of stack |
| 1875 | // parameters, the stack parameters as well. |
| 1876 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1877 | TranslatedFrame::iterator value_iterator = translated_frame->begin(); |
| 1878 | int input_index = 0; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1879 | |
| 1880 | CHECK(compiled_code_->is_hydrogen_stub()); |
| 1881 | int major_key = CodeStub::GetMajorKey(compiled_code_); |
| 1882 | CodeStubDescriptor descriptor(isolate_, compiled_code_->stub_key()); |
| 1883 | |
| 1884 | // The output frame must have room for all pushed register parameters |
| 1885 | // and the standard stack frame slots. Include space for an argument |
| 1886 | // object to the callee and optionally the space to pass the argument |
| 1887 | // object to the stub failure handler. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1888 | int param_count = descriptor.GetRegisterParameterCount(); |
| 1889 | int stack_param_count = descriptor.GetStackParameterCount(); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 1890 | // The translated frame contains all of the register parameters |
| 1891 | // plus the context. |
| 1892 | CHECK_EQ(translated_frame->height(), param_count + 1); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1893 | CHECK_GE(param_count, 0); |
| 1894 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 1895 | int height_in_bytes = kPointerSize * (param_count + stack_param_count); |
| 1896 | int fixed_frame_size = StubFailureTrampolineFrameConstants::kFixedFrameSize; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1897 | int output_frame_size = height_in_bytes + fixed_frame_size; |
| 1898 | if (trace_scope_ != NULL) { |
| 1899 | PrintF(trace_scope_->file(), |
| 1900 | " translating %s => StubFailureTrampolineStub, height=%d\n", |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1901 | CodeStub::MajorName(static_cast<CodeStub::Major>(major_key)), |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1902 | height_in_bytes); |
| 1903 | } |
| 1904 | |
| 1905 | // The stub failure trampoline is a single frame. |
| 1906 | FrameDescription* output_frame = |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 1907 | new (output_frame_size) FrameDescription(output_frame_size); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1908 | output_frame->SetFrameType(StackFrame::STUB_FAILURE_TRAMPOLINE); |
| 1909 | CHECK_EQ(frame_index, 0); |
| 1910 | output_[frame_index] = output_frame; |
| 1911 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 1912 | // The top address of the frame is computed from the previous frame's top and |
| 1913 | // this frame's size. |
| 1914 | intptr_t top_address = caller_frame_top_ - output_frame_size; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1915 | output_frame->SetTop(top_address); |
| 1916 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 1917 | // Set caller's PC (JSFunction continuation). |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1918 | unsigned output_frame_offset = output_frame_size - kFPOnStackSize; |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 1919 | intptr_t value = caller_pc_; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1920 | output_frame->SetCallerPc(output_frame_offset, value); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1921 | DebugPrintOutputSlot(value, frame_index, output_frame_offset, |
| 1922 | "caller's pc\n"); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1923 | |
| 1924 | // Read caller's FP from the input frame, and set this frame's FP. |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 1925 | value = caller_fp_; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1926 | output_frame_offset -= kFPOnStackSize; |
| 1927 | output_frame->SetCallerFp(output_frame_offset, value); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 1928 | intptr_t frame_ptr = top_address + output_frame_offset; |
| 1929 | Register fp_reg = StubFailureTrampolineFrame::fp_register(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1930 | output_frame->SetRegister(fp_reg.code(), frame_ptr); |
| 1931 | output_frame->SetFp(frame_ptr); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1932 | DebugPrintOutputSlot(value, frame_index, output_frame_offset, |
| 1933 | "caller's fp\n"); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1934 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1935 | if (FLAG_enable_embedded_constant_pool) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1936 | // Read the caller's constant pool from the input frame. |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 1937 | value = caller_constant_pool_; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1938 | output_frame_offset -= kPointerSize; |
| 1939 | output_frame->SetCallerConstantPool(output_frame_offset, value); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1940 | DebugPrintOutputSlot(value, frame_index, output_frame_offset, |
| 1941 | "caller's constant_pool\n"); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1942 | } |
| 1943 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 1944 | // The marker for the typed stack frame |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1945 | output_frame_offset -= kPointerSize; |
| 1946 | value = reinterpret_cast<intptr_t>( |
| 1947 | Smi::FromInt(StackFrame::STUB_FAILURE_TRAMPOLINE)); |
| 1948 | output_frame->SetFrameSlot(output_frame_offset, value); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1949 | DebugPrintOutputSlot(value, frame_index, output_frame_offset, |
| 1950 | "function (stub failure sentinel)\n"); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1951 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1952 | intptr_t caller_arg_count = stack_param_count; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1953 | bool arg_count_known = !descriptor.stack_parameter_count().is_valid(); |
| 1954 | |
| 1955 | // Build the Arguments object for the caller's parameters and a pointer to it. |
| 1956 | output_frame_offset -= kPointerSize; |
| 1957 | int args_arguments_offset = output_frame_offset; |
| 1958 | intptr_t the_hole = reinterpret_cast<intptr_t>( |
| 1959 | isolate_->heap()->the_hole_value()); |
| 1960 | if (arg_count_known) { |
| 1961 | value = frame_ptr + StandardFrameConstants::kCallerSPOffset + |
| 1962 | (caller_arg_count - 1) * kPointerSize; |
| 1963 | } else { |
| 1964 | value = the_hole; |
| 1965 | } |
| 1966 | |
| 1967 | output_frame->SetFrameSlot(args_arguments_offset, value); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1968 | DebugPrintOutputSlot( |
| 1969 | value, frame_index, args_arguments_offset, |
| 1970 | arg_count_known ? "args.arguments\n" : "args.arguments (the hole)\n"); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1971 | |
| 1972 | output_frame_offset -= kPointerSize; |
| 1973 | int length_frame_offset = output_frame_offset; |
| 1974 | value = arg_count_known ? caller_arg_count : the_hole; |
| 1975 | output_frame->SetFrameSlot(length_frame_offset, value); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1976 | DebugPrintOutputSlot( |
| 1977 | value, frame_index, length_frame_offset, |
| 1978 | arg_count_known ? "args.length\n" : "args.length (the hole)\n"); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1979 | |
| 1980 | output_frame_offset -= kPointerSize; |
| 1981 | value = frame_ptr + StandardFrameConstants::kCallerSPOffset - |
| 1982 | (output_frame_size - output_frame_offset) + kPointerSize; |
| 1983 | output_frame->SetFrameSlot(output_frame_offset, value); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1984 | DebugPrintOutputSlot(value, frame_index, output_frame_offset, "args*\n"); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1985 | |
| 1986 | // Copy the register parameters to the failure frame. |
| 1987 | int arguments_length_offset = -1; |
| 1988 | for (int i = 0; i < param_count; ++i) { |
| 1989 | output_frame_offset -= kPointerSize; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1990 | WriteTranslatedValueToOutput(&value_iterator, &input_index, 0, |
| 1991 | output_frame_offset); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1992 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1993 | if (!arg_count_known && |
| 1994 | descriptor.GetRegisterParameter(i) |
| 1995 | .is(descriptor.stack_parameter_count())) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1996 | arguments_length_offset = output_frame_offset; |
| 1997 | } |
| 1998 | } |
| 1999 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 2000 | Object* maybe_context = value_iterator->GetRawValue(); |
| 2001 | CHECK(maybe_context->IsContext()); |
| 2002 | Register context_reg = StubFailureTrampolineFrame::context_register(); |
| 2003 | value = reinterpret_cast<intptr_t>(maybe_context); |
| 2004 | output_frame->SetRegister(context_reg.code(), value); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 2005 | ++value_iterator; |
| 2006 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2007 | // Copy constant stack parameters to the failure frame. If the number of stack |
| 2008 | // parameters is not known in the descriptor, the arguments object is the way |
| 2009 | // to access them. |
| 2010 | for (int i = 0; i < stack_param_count; i++) { |
| 2011 | output_frame_offset -= kPointerSize; |
| 2012 | Object** stack_parameter = reinterpret_cast<Object**>( |
| 2013 | frame_ptr + StandardFrameConstants::kCallerSPOffset + |
| 2014 | (stack_param_count - i - 1) * kPointerSize); |
| 2015 | value = reinterpret_cast<intptr_t>(*stack_parameter); |
| 2016 | output_frame->SetFrameSlot(output_frame_offset, value); |
| 2017 | DebugPrintOutputSlot(value, frame_index, output_frame_offset, |
| 2018 | "stack parameter\n"); |
| 2019 | } |
| 2020 | |
| 2021 | CHECK_EQ(0u, output_frame_offset); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2022 | |
| 2023 | if (!arg_count_known) { |
| 2024 | CHECK_GE(arguments_length_offset, 0); |
| 2025 | // We know it's a smi because 1) the code stub guarantees the stack |
| 2026 | // parameter count is in smi range, and 2) the DoTranslateCommand in the |
| 2027 | // parameter loop above translated that to a tagged value. |
| 2028 | Smi* smi_caller_arg_count = reinterpret_cast<Smi*>( |
| 2029 | output_frame->GetFrameSlot(arguments_length_offset)); |
| 2030 | caller_arg_count = smi_caller_arg_count->value(); |
| 2031 | output_frame->SetFrameSlot(length_frame_offset, caller_arg_count); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2032 | DebugPrintOutputSlot(caller_arg_count, frame_index, length_frame_offset, |
| 2033 | "args.length\n"); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2034 | value = frame_ptr + StandardFrameConstants::kCallerSPOffset + |
| 2035 | (caller_arg_count - 1) * kPointerSize; |
| 2036 | output_frame->SetFrameSlot(args_arguments_offset, value); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2037 | DebugPrintOutputSlot(value, frame_index, args_arguments_offset, |
| 2038 | "args.arguments"); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2039 | } |
| 2040 | |
| 2041 | // Copy the double registers from the input into the output frame. |
| 2042 | CopyDoubleRegisters(output_frame); |
| 2043 | |
| 2044 | // Fill registers containing handler and number of parameters. |
| 2045 | SetPlatformCompiledStubRegisters(output_frame, &descriptor); |
| 2046 | |
| 2047 | // Compute this frame's PC, state, and continuation. |
| 2048 | Code* trampoline = NULL; |
| 2049 | StubFunctionMode function_mode = descriptor.function_mode(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2050 | StubFailureTrampolineStub(isolate_, function_mode) |
| 2051 | .FindCodeInCache(&trampoline); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2052 | DCHECK(trampoline != NULL); |
| 2053 | output_frame->SetPc(reinterpret_cast<intptr_t>( |
| 2054 | trampoline->instruction_start())); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2055 | if (FLAG_enable_embedded_constant_pool) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2056 | Register constant_pool_reg = |
| 2057 | StubFailureTrampolineFrame::constant_pool_pointer_register(); |
| 2058 | intptr_t constant_pool_value = |
| 2059 | reinterpret_cast<intptr_t>(trampoline->constant_pool()); |
| 2060 | output_frame->SetConstantPool(constant_pool_value); |
| 2061 | output_frame->SetRegister(constant_pool_reg.code(), constant_pool_value); |
| 2062 | } |
| 2063 | output_frame->SetState(Smi::FromInt(FullCodeGenerator::NO_REGISTERS)); |
| 2064 | Code* notify_failure = |
| 2065 | isolate_->builtins()->builtin(Builtins::kNotifyStubFailureSaveDoubles); |
| 2066 | output_frame->SetContinuation( |
| 2067 | reinterpret_cast<intptr_t>(notify_failure->entry())); |
| 2068 | } |
| 2069 | |
| 2070 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2071 | void Deoptimizer::MaterializeHeapObjects(JavaScriptFrameIterator* it) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2072 | // Walk to the last JavaScript output frame to find out if it has |
| 2073 | // adapted arguments. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2074 | for (int frame_index = 0; frame_index < jsframe_count(); ++frame_index) { |
| 2075 | if (frame_index != 0) it->Advance(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2076 | } |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 2077 | translated_state_.Prepare(it->frame()->has_adapted_arguments(), |
| 2078 | reinterpret_cast<Address>(stack_fp_)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2079 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2080 | for (auto& materialization : values_to_materialize_) { |
| 2081 | Handle<Object> value = materialization.value_->GetValue(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2082 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2083 | if (trace_scope_ != nullptr) { |
| 2084 | PrintF("Materialization [0x%08" V8PRIxPTR "] <- 0x%08" V8PRIxPTR " ; ", |
| 2085 | reinterpret_cast<intptr_t>(materialization.output_slot_address_), |
| 2086 | reinterpret_cast<intptr_t>(*value)); |
| 2087 | value->ShortPrint(trace_scope_->file()); |
| 2088 | PrintF(trace_scope_->file(), "\n"); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2089 | } |
| 2090 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2091 | *(reinterpret_cast<intptr_t*>(materialization.output_slot_address_)) = |
| 2092 | reinterpret_cast<intptr_t>(*value); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2093 | } |
| 2094 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 2095 | isolate_->materialized_object_store()->Remove( |
| 2096 | reinterpret_cast<Address>(stack_fp_)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2097 | } |
| 2098 | |
| 2099 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2100 | void Deoptimizer::WriteTranslatedValueToOutput( |
| 2101 | TranslatedFrame::iterator* iterator, int* input_index, int frame_index, |
| 2102 | unsigned output_offset, const char* debug_hint_string, |
| 2103 | Address output_address_for_materialization) { |
| 2104 | Object* value = (*iterator)->GetRawValue(); |
| 2105 | |
| 2106 | WriteValueToOutput(value, *input_index, frame_index, output_offset, |
| 2107 | debug_hint_string); |
| 2108 | |
| 2109 | if (value == isolate_->heap()->arguments_marker()) { |
| 2110 | Address output_address = |
| 2111 | reinterpret_cast<Address>(output_[frame_index]->GetTop()) + |
| 2112 | output_offset; |
| 2113 | if (output_address_for_materialization == nullptr) { |
| 2114 | output_address_for_materialization = output_address; |
| 2115 | } |
| 2116 | values_to_materialize_.push_back( |
| 2117 | {output_address_for_materialization, *iterator}); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2118 | } |
| 2119 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2120 | (*iterator)++; |
| 2121 | (*input_index)++; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2122 | } |
| 2123 | |
| 2124 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2125 | void Deoptimizer::WriteValueToOutput(Object* value, int input_index, |
| 2126 | int frame_index, unsigned output_offset, |
| 2127 | const char* debug_hint_string) { |
| 2128 | output_[frame_index]->SetFrameSlot(output_offset, |
| 2129 | reinterpret_cast<intptr_t>(value)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2130 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2131 | if (trace_scope_ != nullptr) { |
| 2132 | DebugPrintOutputSlot(reinterpret_cast<intptr_t>(value), frame_index, |
| 2133 | output_offset, debug_hint_string); |
| 2134 | value->ShortPrint(trace_scope_->file()); |
| 2135 | PrintF(trace_scope_->file(), " (input #%d)\n", input_index); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2136 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2137 | } |
| 2138 | |
| 2139 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2140 | void Deoptimizer::DebugPrintOutputSlot(intptr_t value, int frame_index, |
| 2141 | unsigned output_offset, |
| 2142 | const char* debug_hint_string) { |
| 2143 | if (trace_scope_ != nullptr) { |
| 2144 | Address output_address = |
| 2145 | reinterpret_cast<Address>(output_[frame_index]->GetTop()) + |
| 2146 | output_offset; |
| 2147 | PrintF(trace_scope_->file(), |
| 2148 | " 0x%08" V8PRIxPTR ": [top + %d] <- 0x%08" V8PRIxPTR " ; %s", |
| 2149 | reinterpret_cast<intptr_t>(output_address), output_offset, value, |
| 2150 | debug_hint_string == nullptr ? "" : debug_hint_string); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 2151 | } |
| 2152 | } |
| 2153 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 2154 | unsigned Deoptimizer::ComputeInputFrameAboveFpFixedSize() const { |
| 2155 | unsigned fixed_size = CommonFrameConstants::kFixedFrameSizeAboveFp; |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 2156 | if (!function_->IsSmi()) { |
| 2157 | fixed_size += ComputeIncomingArgumentSize(function_->shared()); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 2158 | } |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 2159 | return fixed_size; |
| 2160 | } |
| 2161 | |
| 2162 | unsigned Deoptimizer::ComputeInputFrameSize() const { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2163 | // The fp-to-sp delta already takes the context, constant pool pointer and the |
| 2164 | // function into account so we have to avoid double counting them. |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 2165 | unsigned fixed_size_above_fp = ComputeInputFrameAboveFpFixedSize(); |
| 2166 | unsigned result = fixed_size_above_fp + fp_to_sp_delta_; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2167 | if (compiled_code_->kind() == Code::OPTIMIZED_FUNCTION) { |
| 2168 | unsigned stack_slots = compiled_code_->stack_slots(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2169 | unsigned outgoing_size = |
| 2170 | ComputeOutgoingArgumentSize(compiled_code_, bailout_id_); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 2171 | CHECK_EQ(fixed_size_above_fp + (stack_slots * kPointerSize) - |
| 2172 | CommonFrameConstants::kFixedFrameSizeAboveFp + outgoing_size, |
| 2173 | result); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 2174 | } |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 2175 | return result; |
| 2176 | } |
| 2177 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 2178 | // static |
| 2179 | unsigned Deoptimizer::ComputeJavascriptFixedSize(SharedFunctionInfo* shared) { |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 2180 | // The fixed part of the frame consists of the return address, frame |
| 2181 | // pointer, function, context, and all the incoming arguments. |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 2182 | return ComputeIncomingArgumentSize(shared) + |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2183 | StandardFrameConstants::kFixedFrameSize; |
| 2184 | } |
| 2185 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 2186 | // static |
| 2187 | unsigned Deoptimizer::ComputeInterpretedFixedSize(SharedFunctionInfo* shared) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2188 | // The fixed part of the frame consists of the return address, frame |
| 2189 | // pointer, function, context, new.target, bytecode offset and all the |
| 2190 | // incoming arguments. |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 2191 | return ComputeIncomingArgumentSize(shared) + |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2192 | InterpreterFrameConstants::kFixedFrameSize; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 2193 | } |
| 2194 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 2195 | // static |
| 2196 | unsigned Deoptimizer::ComputeIncomingArgumentSize(SharedFunctionInfo* shared) { |
| 2197 | return (shared->internal_formal_parameter_count() + 1) * kPointerSize; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 2198 | } |
| 2199 | |
| 2200 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2201 | // static |
| 2202 | unsigned Deoptimizer::ComputeOutgoingArgumentSize(Code* code, |
| 2203 | unsigned bailout_id) { |
| 2204 | DeoptimizationInputData* data = |
| 2205 | DeoptimizationInputData::cast(code->deoptimization_data()); |
| 2206 | unsigned height = data->ArgumentsStackHeight(bailout_id)->value(); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 2207 | return height * kPointerSize; |
| 2208 | } |
| 2209 | |
| 2210 | |
| 2211 | Object* Deoptimizer::ComputeLiteral(int index) const { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2212 | DeoptimizationInputData* data = |
| 2213 | DeoptimizationInputData::cast(compiled_code_->deoptimization_data()); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 2214 | FixedArray* literals = data->LiteralArray(); |
| 2215 | return literals->get(index); |
| 2216 | } |
| 2217 | |
| 2218 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2219 | void Deoptimizer::EnsureCodeForDeoptimizationEntry(Isolate* isolate, |
| 2220 | BailoutType type, |
| 2221 | int max_entry_id) { |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 2222 | // We cannot run this if the serializer is enabled because this will |
| 2223 | // cause us to emit relocation information for the external |
| 2224 | // references. This is fine because the deoptimizer's code section |
| 2225 | // isn't meant to be serialized at all. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2226 | CHECK(type == EAGER || type == SOFT || type == LAZY); |
| 2227 | DeoptimizerData* data = isolate->deoptimizer_data(); |
| 2228 | int entry_count = data->deopt_entry_code_entries_[type]; |
| 2229 | if (max_entry_id < entry_count) return; |
| 2230 | entry_count = Max(entry_count, Deoptimizer::kMinNumberOfEntries); |
| 2231 | while (max_entry_id >= entry_count) entry_count *= 2; |
| 2232 | CHECK(entry_count <= Deoptimizer::kMaxNumberOfEntries); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 2233 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2234 | MacroAssembler masm(isolate, NULL, 16 * KB, CodeObjectRequired::kYes); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2235 | masm.set_emit_debug_code(false); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2236 | GenerateDeoptimizationEntries(&masm, entry_count, type); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 2237 | CodeDesc desc; |
| 2238 | masm.GetCode(&desc); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2239 | DCHECK(!RelocInfo::RequiresRelocation(desc)); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 2240 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2241 | MemoryChunk* chunk = data->deopt_entry_code_[type]; |
| 2242 | CHECK(static_cast<int>(Deoptimizer::GetMaxDeoptTableSize()) >= |
| 2243 | desc.instr_size); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2244 | if (!chunk->CommitArea(desc.instr_size)) { |
| 2245 | V8::FatalProcessOutOfMemory( |
| 2246 | "Deoptimizer::EnsureCodeForDeoptimizationEntry"); |
| 2247 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2248 | CopyBytes(chunk->area_start(), desc.buffer, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2249 | static_cast<size_t>(desc.instr_size)); |
| 2250 | Assembler::FlushICache(isolate, chunk->area_start(), desc.instr_size); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 2251 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2252 | data->deopt_entry_code_entries_[type] = entry_count; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 2253 | } |
| 2254 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 2255 | FrameDescription::FrameDescription(uint32_t frame_size, int parameter_count) |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 2256 | : frame_size_(frame_size), |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 2257 | parameter_count_(parameter_count), |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 2258 | top_(kZapUint32), |
| 2259 | pc_(kZapUint32), |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 2260 | fp_(kZapUint32), |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2261 | context_(kZapUint32), |
| 2262 | constant_pool_(kZapUint32) { |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 2263 | // Zap all the registers. |
| 2264 | for (int r = 0; r < Register::kNumRegisters; r++) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2265 | // TODO(jbramley): It isn't safe to use kZapUint32 here. If the register |
| 2266 | // isn't used before the next safepoint, the GC will try to scan it as a |
| 2267 | // tagged value. kZapUint32 looks like a valid tagged pointer, but it isn't. |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 2268 | SetRegister(r, kZapUint32); |
| 2269 | } |
| 2270 | |
| 2271 | // Zap all the slots. |
| 2272 | for (unsigned o = 0; o < frame_size; o += kPointerSize) { |
| 2273 | SetFrameSlot(o, kZapUint32); |
| 2274 | } |
| 2275 | } |
| 2276 | |
| 2277 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 2278 | int FrameDescription::ComputeFixedSize() { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2279 | if (type_ == StackFrame::INTERPRETED) { |
| 2280 | return InterpreterFrameConstants::kFixedFrameSize + |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 2281 | parameter_count() * kPointerSize; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2282 | } else { |
| 2283 | return StandardFrameConstants::kFixedFrameSize + |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 2284 | parameter_count() * kPointerSize; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2285 | } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 2286 | } |
| 2287 | |
| 2288 | |
| 2289 | unsigned FrameDescription::GetOffsetFromSlotIndex(int slot_index) { |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 2290 | if (slot_index >= 0) { |
| 2291 | // Local or spill slots. Skip the fixed part of the frame |
| 2292 | // including all arguments. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 2293 | unsigned base = GetFrameSize() - ComputeFixedSize(); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 2294 | return base - ((slot_index + 1) * kPointerSize); |
| 2295 | } else { |
| 2296 | // Incoming parameter. |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 2297 | int arg_size = parameter_count() * kPointerSize; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 2298 | unsigned base = GetFrameSize() - arg_size; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 2299 | return base - ((slot_index + 1) * kPointerSize); |
| 2300 | } |
| 2301 | } |
| 2302 | |
| 2303 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2304 | void TranslationBuffer::Add(int32_t value, Zone* zone) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2305 | // This wouldn't handle kMinInt correctly if it ever encountered it. |
| 2306 | DCHECK(value != kMinInt); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 2307 | // Encode the sign bit in the least significant bit. |
| 2308 | bool is_negative = (value < 0); |
| 2309 | uint32_t bits = ((is_negative ? -value : value) << 1) | |
| 2310 | static_cast<int32_t>(is_negative); |
| 2311 | // Encode the individual bytes using the least significant bit of |
| 2312 | // each byte to indicate whether or not more bytes follow. |
| 2313 | do { |
| 2314 | uint32_t next = bits >> 7; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2315 | contents_.Add(((bits << 1) & 0xFF) | (next != 0), zone); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 2316 | bits = next; |
| 2317 | } while (bits != 0); |
| 2318 | } |
| 2319 | |
| 2320 | |
| 2321 | int32_t TranslationIterator::Next() { |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 2322 | // Run through the bytes until we reach one with a least significant |
| 2323 | // bit of zero (marks the end). |
| 2324 | uint32_t bits = 0; |
| 2325 | for (int i = 0; true; i += 7) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2326 | DCHECK(HasNext()); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 2327 | uint8_t next = buffer_->get(index_++); |
| 2328 | bits |= (next >> 1) << i; |
| 2329 | if ((next & 1) == 0) break; |
| 2330 | } |
| 2331 | // The bits encode the sign in the least significant bit. |
| 2332 | bool is_negative = (bits & 1) == 1; |
| 2333 | int32_t result = bits >> 1; |
| 2334 | return is_negative ? -result : result; |
| 2335 | } |
| 2336 | |
| 2337 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2338 | Handle<ByteArray> TranslationBuffer::CreateByteArray(Factory* factory) { |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 2339 | int length = contents_.length(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2340 | Handle<ByteArray> result = factory->NewByteArray(length, TENURED); |
| 2341 | MemCopy(result->GetDataStartAddress(), contents_.ToVector().start(), length); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 2342 | return result; |
| 2343 | } |
| 2344 | |
| 2345 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 2346 | void Translation::BeginConstructStubFrame(int literal_id, unsigned height) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2347 | buffer_->Add(CONSTRUCT_STUB_FRAME, zone()); |
| 2348 | buffer_->Add(literal_id, zone()); |
| 2349 | buffer_->Add(height, zone()); |
| 2350 | } |
| 2351 | |
| 2352 | |
| 2353 | void Translation::BeginGetterStubFrame(int literal_id) { |
| 2354 | buffer_->Add(GETTER_STUB_FRAME, zone()); |
| 2355 | buffer_->Add(literal_id, zone()); |
| 2356 | } |
| 2357 | |
| 2358 | |
| 2359 | void Translation::BeginSetterStubFrame(int literal_id) { |
| 2360 | buffer_->Add(SETTER_STUB_FRAME, zone()); |
| 2361 | buffer_->Add(literal_id, zone()); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 2362 | } |
| 2363 | |
| 2364 | |
| 2365 | void Translation::BeginArgumentsAdaptorFrame(int literal_id, unsigned height) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2366 | buffer_->Add(ARGUMENTS_ADAPTOR_FRAME, zone()); |
| 2367 | buffer_->Add(literal_id, zone()); |
| 2368 | buffer_->Add(height, zone()); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 2369 | } |
| 2370 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 2371 | void Translation::BeginTailCallerFrame(int literal_id) { |
| 2372 | buffer_->Add(TAIL_CALLER_FRAME, zone()); |
| 2373 | buffer_->Add(literal_id, zone()); |
| 2374 | } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 2375 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2376 | void Translation::BeginJSFrame(BailoutId node_id, |
| 2377 | int literal_id, |
| 2378 | unsigned height) { |
| 2379 | buffer_->Add(JS_FRAME, zone()); |
| 2380 | buffer_->Add(node_id.ToInt(), zone()); |
| 2381 | buffer_->Add(literal_id, zone()); |
| 2382 | buffer_->Add(height, zone()); |
| 2383 | } |
| 2384 | |
| 2385 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2386 | void Translation::BeginInterpretedFrame(BailoutId bytecode_offset, |
| 2387 | int literal_id, unsigned height) { |
| 2388 | buffer_->Add(INTERPRETED_FRAME, zone()); |
| 2389 | buffer_->Add(bytecode_offset.ToInt(), zone()); |
| 2390 | buffer_->Add(literal_id, zone()); |
| 2391 | buffer_->Add(height, zone()); |
| 2392 | } |
| 2393 | |
| 2394 | |
| 2395 | void Translation::BeginCompiledStubFrame(int height) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2396 | buffer_->Add(COMPILED_STUB_FRAME, zone()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2397 | buffer_->Add(height, zone()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2398 | } |
| 2399 | |
| 2400 | |
| 2401 | void Translation::BeginArgumentsObject(int args_length) { |
| 2402 | buffer_->Add(ARGUMENTS_OBJECT, zone()); |
| 2403 | buffer_->Add(args_length, zone()); |
| 2404 | } |
| 2405 | |
| 2406 | |
| 2407 | void Translation::BeginCapturedObject(int length) { |
| 2408 | buffer_->Add(CAPTURED_OBJECT, zone()); |
| 2409 | buffer_->Add(length, zone()); |
| 2410 | } |
| 2411 | |
| 2412 | |
| 2413 | void Translation::DuplicateObject(int object_index) { |
| 2414 | buffer_->Add(DUPLICATED_OBJECT, zone()); |
| 2415 | buffer_->Add(object_index, zone()); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 2416 | } |
| 2417 | |
| 2418 | |
| 2419 | void Translation::StoreRegister(Register reg) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2420 | buffer_->Add(REGISTER, zone()); |
| 2421 | buffer_->Add(reg.code(), zone()); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 2422 | } |
| 2423 | |
| 2424 | |
| 2425 | void Translation::StoreInt32Register(Register reg) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2426 | buffer_->Add(INT32_REGISTER, zone()); |
| 2427 | buffer_->Add(reg.code(), zone()); |
| 2428 | } |
| 2429 | |
| 2430 | |
| 2431 | void Translation::StoreUint32Register(Register reg) { |
| 2432 | buffer_->Add(UINT32_REGISTER, zone()); |
| 2433 | buffer_->Add(reg.code(), zone()); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 2434 | } |
| 2435 | |
| 2436 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2437 | void Translation::StoreBoolRegister(Register reg) { |
| 2438 | buffer_->Add(BOOL_REGISTER, zone()); |
| 2439 | buffer_->Add(reg.code(), zone()); |
| 2440 | } |
| 2441 | |
| 2442 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 2443 | void Translation::StoreDoubleRegister(DoubleRegister reg) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2444 | buffer_->Add(DOUBLE_REGISTER, zone()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2445 | buffer_->Add(reg.code(), zone()); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 2446 | } |
| 2447 | |
| 2448 | |
| 2449 | void Translation::StoreStackSlot(int index) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2450 | buffer_->Add(STACK_SLOT, zone()); |
| 2451 | buffer_->Add(index, zone()); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 2452 | } |
| 2453 | |
| 2454 | |
| 2455 | void Translation::StoreInt32StackSlot(int index) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2456 | buffer_->Add(INT32_STACK_SLOT, zone()); |
| 2457 | buffer_->Add(index, zone()); |
| 2458 | } |
| 2459 | |
| 2460 | |
| 2461 | void Translation::StoreUint32StackSlot(int index) { |
| 2462 | buffer_->Add(UINT32_STACK_SLOT, zone()); |
| 2463 | buffer_->Add(index, zone()); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 2464 | } |
| 2465 | |
| 2466 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2467 | void Translation::StoreBoolStackSlot(int index) { |
| 2468 | buffer_->Add(BOOL_STACK_SLOT, zone()); |
| 2469 | buffer_->Add(index, zone()); |
| 2470 | } |
| 2471 | |
| 2472 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 2473 | void Translation::StoreDoubleStackSlot(int index) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2474 | buffer_->Add(DOUBLE_STACK_SLOT, zone()); |
| 2475 | buffer_->Add(index, zone()); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 2476 | } |
| 2477 | |
| 2478 | |
| 2479 | void Translation::StoreLiteral(int literal_id) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2480 | buffer_->Add(LITERAL, zone()); |
| 2481 | buffer_->Add(literal_id, zone()); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 2482 | } |
| 2483 | |
| 2484 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2485 | void Translation::StoreArgumentsObject(bool args_known, |
| 2486 | int args_index, |
| 2487 | int args_length) { |
| 2488 | buffer_->Add(ARGUMENTS_OBJECT, zone()); |
| 2489 | buffer_->Add(args_known, zone()); |
| 2490 | buffer_->Add(args_index, zone()); |
| 2491 | buffer_->Add(args_length, zone()); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 2492 | } |
| 2493 | |
| 2494 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2495 | void Translation::StoreJSFrameFunction() { |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 2496 | StoreStackSlot((StandardFrameConstants::kCallerPCOffset - |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 2497 | StandardFrameConstants::kFunctionOffset) / |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 2498 | kPointerSize); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2499 | } |
| 2500 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 2501 | int Translation::NumberOfOperandsFor(Opcode opcode) { |
| 2502 | switch (opcode) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2503 | case GETTER_STUB_FRAME: |
| 2504 | case SETTER_STUB_FRAME: |
| 2505 | case DUPLICATED_OBJECT: |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 2506 | case ARGUMENTS_OBJECT: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2507 | case CAPTURED_OBJECT: |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 2508 | case REGISTER: |
| 2509 | case INT32_REGISTER: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2510 | case UINT32_REGISTER: |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2511 | case BOOL_REGISTER: |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 2512 | case DOUBLE_REGISTER: |
| 2513 | case STACK_SLOT: |
| 2514 | case INT32_STACK_SLOT: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2515 | case UINT32_STACK_SLOT: |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2516 | case BOOL_STACK_SLOT: |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 2517 | case DOUBLE_STACK_SLOT: |
| 2518 | case LITERAL: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2519 | case COMPILED_STUB_FRAME: |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 2520 | case TAIL_CALLER_FRAME: |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 2521 | return 1; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 2522 | case BEGIN: |
| 2523 | case ARGUMENTS_ADAPTOR_FRAME: |
| 2524 | case CONSTRUCT_STUB_FRAME: |
| 2525 | return 2; |
| 2526 | case JS_FRAME: |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2527 | case INTERPRETED_FRAME: |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 2528 | return 3; |
| 2529 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2530 | FATAL("Unexpected translation type"); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 2531 | return -1; |
| 2532 | } |
| 2533 | |
| 2534 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 2535 | #if defined(OBJECT_PRINT) || defined(ENABLE_DISASSEMBLER) |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 2536 | |
| 2537 | const char* Translation::StringFor(Opcode opcode) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2538 | #define TRANSLATION_OPCODE_CASE(item) case item: return #item; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 2539 | switch (opcode) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2540 | TRANSLATION_OPCODE_LIST(TRANSLATION_OPCODE_CASE) |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 2541 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2542 | #undef TRANSLATION_OPCODE_CASE |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 2543 | UNREACHABLE(); |
| 2544 | return ""; |
| 2545 | } |
| 2546 | |
| 2547 | #endif |
| 2548 | |
| 2549 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2550 | Handle<FixedArray> MaterializedObjectStore::Get(Address fp) { |
| 2551 | int index = StackIdToIndex(fp); |
| 2552 | if (index == -1) { |
| 2553 | return Handle<FixedArray>::null(); |
| 2554 | } |
| 2555 | Handle<FixedArray> array = GetStackEntries(); |
| 2556 | CHECK_GT(array->length(), index); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2557 | return Handle<FixedArray>::cast(Handle<Object>(array->get(index), isolate())); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2558 | } |
| 2559 | |
| 2560 | |
| 2561 | void MaterializedObjectStore::Set(Address fp, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2562 | Handle<FixedArray> materialized_objects) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2563 | int index = StackIdToIndex(fp); |
| 2564 | if (index == -1) { |
| 2565 | index = frame_fps_.length(); |
| 2566 | frame_fps_.Add(fp); |
| 2567 | } |
| 2568 | |
| 2569 | Handle<FixedArray> array = EnsureStackEntries(index + 1); |
| 2570 | array->set(index, *materialized_objects); |
| 2571 | } |
| 2572 | |
| 2573 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2574 | bool MaterializedObjectStore::Remove(Address fp) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2575 | int index = StackIdToIndex(fp); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2576 | if (index == -1) { |
| 2577 | return false; |
| 2578 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2579 | CHECK_GE(index, 0); |
| 2580 | |
| 2581 | frame_fps_.Remove(index); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2582 | FixedArray* array = isolate()->heap()->materialized_objects(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2583 | CHECK_LT(index, array->length()); |
| 2584 | for (int i = index; i < frame_fps_.length(); i++) { |
| 2585 | array->set(i, array->get(i + 1)); |
| 2586 | } |
| 2587 | array->set(frame_fps_.length(), isolate()->heap()->undefined_value()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2588 | return true; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2589 | } |
| 2590 | |
| 2591 | |
| 2592 | int MaterializedObjectStore::StackIdToIndex(Address fp) { |
| 2593 | for (int i = 0; i < frame_fps_.length(); i++) { |
| 2594 | if (frame_fps_[i] == fp) { |
| 2595 | return i; |
| 2596 | } |
| 2597 | } |
| 2598 | return -1; |
| 2599 | } |
| 2600 | |
| 2601 | |
| 2602 | Handle<FixedArray> MaterializedObjectStore::GetStackEntries() { |
| 2603 | return Handle<FixedArray>(isolate()->heap()->materialized_objects()); |
| 2604 | } |
| 2605 | |
| 2606 | |
| 2607 | Handle<FixedArray> MaterializedObjectStore::EnsureStackEntries(int length) { |
| 2608 | Handle<FixedArray> array = GetStackEntries(); |
| 2609 | if (array->length() >= length) { |
| 2610 | return array; |
| 2611 | } |
| 2612 | |
| 2613 | int new_length = length > 10 ? length : 10; |
| 2614 | if (new_length < 2 * array->length()) { |
| 2615 | new_length = 2 * array->length(); |
| 2616 | } |
| 2617 | |
| 2618 | Handle<FixedArray> new_array = |
| 2619 | isolate()->factory()->NewFixedArray(new_length, TENURED); |
| 2620 | for (int i = 0; i < array->length(); i++) { |
| 2621 | new_array->set(i, array->get(i)); |
| 2622 | } |
| 2623 | for (int i = array->length(); i < length; i++) { |
| 2624 | new_array->set(i, isolate()->heap()->undefined_value()); |
| 2625 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2626 | isolate()->heap()->SetRootMaterializedObjects(*new_array); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2627 | return new_array; |
| 2628 | } |
| 2629 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 2630 | namespace { |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 2631 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 2632 | Handle<Object> GetValueForDebugger(TranslatedFrame::iterator it, |
| 2633 | Isolate* isolate) { |
| 2634 | if (it->GetRawValue() == isolate->heap()->arguments_marker()) { |
| 2635 | if (!it->IsMaterializableByDebugger()) { |
| 2636 | return isolate->factory()->undefined_value(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2637 | } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 2638 | } |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 2639 | return it->GetValue(); |
| 2640 | } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 2641 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 2642 | int ComputeSourcePosition(Handle<SharedFunctionInfo> shared, |
| 2643 | BailoutId node_id) { |
| 2644 | if (shared->HasBytecodeArray()) { |
| 2645 | BytecodeArray* bytecodes = shared->bytecode_array(); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 2646 | // BailoutId points to the next bytecode in the bytecode aray. Subtract |
| 2647 | // 1 to get the end of current bytecode. |
| 2648 | return bytecodes->SourcePosition(node_id.ToInt() - 1); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 2649 | } else { |
| 2650 | Code* non_optimized_code = shared->code(); |
| 2651 | FixedArray* raw_data = non_optimized_code->deoptimization_data(); |
| 2652 | DeoptimizationOutputData* data = DeoptimizationOutputData::cast(raw_data); |
| 2653 | unsigned pc_and_state = Deoptimizer::GetOutputInfo(data, node_id, *shared); |
| 2654 | unsigned pc_offset = FullCodeGenerator::PcField::decode(pc_and_state); |
| 2655 | return non_optimized_code->SourcePosition(pc_offset); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 2656 | } |
| 2657 | } |
| 2658 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 2659 | } // namespace |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 2660 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 2661 | DeoptimizedFrameInfo::DeoptimizedFrameInfo(TranslatedState* state, |
| 2662 | TranslatedState::iterator frame_it, |
| 2663 | Isolate* isolate) { |
| 2664 | // If the previous frame is an adaptor frame, we will take the parameters |
| 2665 | // from there. |
| 2666 | TranslatedState::iterator parameter_frame = frame_it; |
| 2667 | if (parameter_frame != state->begin()) { |
| 2668 | parameter_frame--; |
| 2669 | } |
| 2670 | int parameter_count; |
| 2671 | if (parameter_frame->kind() == TranslatedFrame::kArgumentsAdaptor) { |
| 2672 | parameter_count = parameter_frame->height() - 1; // Ignore the receiver. |
| 2673 | } else { |
| 2674 | parameter_frame = frame_it; |
| 2675 | parameter_count = |
| 2676 | frame_it->shared_info()->internal_formal_parameter_count(); |
| 2677 | } |
| 2678 | TranslatedFrame::iterator parameter_it = parameter_frame->begin(); |
| 2679 | parameter_it++; // Skip the function. |
| 2680 | parameter_it++; // Skip the receiver. |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 2681 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 2682 | // Figure out whether there is a construct stub frame on top of |
| 2683 | // the parameter frame. |
| 2684 | has_construct_stub_ = |
| 2685 | parameter_frame != state->begin() && |
| 2686 | (parameter_frame - 1)->kind() == TranslatedFrame::kConstructStub; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 2687 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 2688 | source_position_ = |
| 2689 | ComputeSourcePosition(frame_it->shared_info(), frame_it->node_id()); |
| 2690 | |
| 2691 | TranslatedFrame::iterator value_it = frame_it->begin(); |
| 2692 | // Get the function. Note that this might materialize the function. |
| 2693 | // In case the debugger mutates this value, we should deoptimize |
| 2694 | // the function and remember the value in the materialized value store. |
| 2695 | function_ = Handle<JSFunction>::cast(value_it->GetValue()); |
| 2696 | |
| 2697 | parameters_.resize(static_cast<size_t>(parameter_count)); |
| 2698 | for (int i = 0; i < parameter_count; i++) { |
| 2699 | Handle<Object> parameter = GetValueForDebugger(parameter_it, isolate); |
| 2700 | SetParameter(i, parameter); |
| 2701 | parameter_it++; |
| 2702 | } |
| 2703 | |
| 2704 | // Skip the function, the receiver and the arguments. |
| 2705 | int skip_count = |
| 2706 | frame_it->shared_info()->internal_formal_parameter_count() + 2; |
| 2707 | TranslatedFrame::iterator stack_it = frame_it->begin(); |
| 2708 | for (int i = 0; i < skip_count; i++) { |
| 2709 | stack_it++; |
| 2710 | } |
| 2711 | |
| 2712 | // Get the context. |
| 2713 | context_ = GetValueForDebugger(stack_it, isolate); |
| 2714 | stack_it++; |
| 2715 | |
| 2716 | // Get the expression stack. |
| 2717 | int stack_height = frame_it->height(); |
| 2718 | if (frame_it->kind() == TranslatedFrame::kFunction || |
| 2719 | frame_it->kind() == TranslatedFrame::kInterpretedFunction) { |
| 2720 | // For full-code frames, we should not count the context. |
| 2721 | // For interpreter frames, we should not count the accumulator. |
| 2722 | // TODO(jarin): Clean up the indexing in translated frames. |
| 2723 | stack_height--; |
| 2724 | } |
| 2725 | expression_stack_.resize(static_cast<size_t>(stack_height)); |
| 2726 | for (int i = 0; i < stack_height; i++) { |
| 2727 | Handle<Object> expression = GetValueForDebugger(stack_it, isolate); |
| 2728 | SetExpression(i, expression); |
| 2729 | stack_it++; |
| 2730 | } |
| 2731 | |
| 2732 | // For interpreter frame, skip the accumulator. |
| 2733 | if (frame_it->kind() == TranslatedFrame::kInterpretedFunction) { |
| 2734 | stack_it++; |
| 2735 | } |
| 2736 | CHECK(stack_it == frame_it->end()); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 2737 | } |
| 2738 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2739 | |
| 2740 | const char* Deoptimizer::GetDeoptReason(DeoptReason deopt_reason) { |
| 2741 | DCHECK(deopt_reason < kLastDeoptReason); |
| 2742 | #define DEOPT_MESSAGES_TEXTS(C, T) T, |
| 2743 | static const char* deopt_messages_[] = { |
| 2744 | DEOPT_MESSAGES_LIST(DEOPT_MESSAGES_TEXTS)}; |
| 2745 | #undef DEOPT_MESSAGES_TEXTS |
| 2746 | return deopt_messages_[deopt_reason]; |
| 2747 | } |
| 2748 | |
| 2749 | |
| 2750 | Deoptimizer::DeoptInfo Deoptimizer::GetDeoptInfo(Code* code, Address pc) { |
| 2751 | SourcePosition last_position = SourcePosition::Unknown(); |
| 2752 | Deoptimizer::DeoptReason last_reason = Deoptimizer::kNoReason; |
| 2753 | int mask = RelocInfo::ModeMask(RelocInfo::DEOPT_REASON) | |
| 2754 | RelocInfo::ModeMask(RelocInfo::POSITION); |
| 2755 | for (RelocIterator it(code, mask); !it.done(); it.next()) { |
| 2756 | RelocInfo* info = it.rinfo(); |
| 2757 | if (info->pc() >= pc) return DeoptInfo(last_position, NULL, last_reason); |
| 2758 | if (info->rmode() == RelocInfo::POSITION) { |
| 2759 | int raw_position = static_cast<int>(info->data()); |
| 2760 | last_position = raw_position ? SourcePosition::FromRaw(raw_position) |
| 2761 | : SourcePosition::Unknown(); |
| 2762 | } else if (info->rmode() == RelocInfo::DEOPT_REASON) { |
| 2763 | last_reason = static_cast<Deoptimizer::DeoptReason>(info->data()); |
| 2764 | } |
| 2765 | } |
| 2766 | return DeoptInfo(SourcePosition::Unknown(), NULL, Deoptimizer::kNoReason); |
| 2767 | } |
| 2768 | |
| 2769 | |
| 2770 | // static |
| 2771 | TranslatedValue TranslatedValue::NewArgumentsObject(TranslatedState* container, |
| 2772 | int length, |
| 2773 | int object_index) { |
| 2774 | TranslatedValue slot(container, kArgumentsObject); |
| 2775 | slot.materialization_info_ = {object_index, length}; |
| 2776 | return slot; |
| 2777 | } |
| 2778 | |
| 2779 | |
| 2780 | // static |
| 2781 | TranslatedValue TranslatedValue::NewDeferredObject(TranslatedState* container, |
| 2782 | int length, |
| 2783 | int object_index) { |
| 2784 | TranslatedValue slot(container, kCapturedObject); |
| 2785 | slot.materialization_info_ = {object_index, length}; |
| 2786 | return slot; |
| 2787 | } |
| 2788 | |
| 2789 | |
| 2790 | // static |
| 2791 | TranslatedValue TranslatedValue::NewDuplicateObject(TranslatedState* container, |
| 2792 | int id) { |
| 2793 | TranslatedValue slot(container, kDuplicatedObject); |
| 2794 | slot.materialization_info_ = {id, -1}; |
| 2795 | return slot; |
| 2796 | } |
| 2797 | |
| 2798 | |
| 2799 | // static |
| 2800 | TranslatedValue TranslatedValue::NewDouble(TranslatedState* container, |
| 2801 | double value) { |
| 2802 | TranslatedValue slot(container, kDouble); |
| 2803 | slot.double_value_ = value; |
| 2804 | return slot; |
| 2805 | } |
| 2806 | |
| 2807 | |
| 2808 | // static |
| 2809 | TranslatedValue TranslatedValue::NewInt32(TranslatedState* container, |
| 2810 | int32_t value) { |
| 2811 | TranslatedValue slot(container, kInt32); |
| 2812 | slot.int32_value_ = value; |
| 2813 | return slot; |
| 2814 | } |
| 2815 | |
| 2816 | |
| 2817 | // static |
| 2818 | TranslatedValue TranslatedValue::NewUInt32(TranslatedState* container, |
| 2819 | uint32_t value) { |
| 2820 | TranslatedValue slot(container, kUInt32); |
| 2821 | slot.uint32_value_ = value; |
| 2822 | return slot; |
| 2823 | } |
| 2824 | |
| 2825 | |
| 2826 | // static |
| 2827 | TranslatedValue TranslatedValue::NewBool(TranslatedState* container, |
| 2828 | uint32_t value) { |
| 2829 | TranslatedValue slot(container, kBoolBit); |
| 2830 | slot.uint32_value_ = value; |
| 2831 | return slot; |
| 2832 | } |
| 2833 | |
| 2834 | |
| 2835 | // static |
| 2836 | TranslatedValue TranslatedValue::NewTagged(TranslatedState* container, |
| 2837 | Object* literal) { |
| 2838 | TranslatedValue slot(container, kTagged); |
| 2839 | slot.raw_literal_ = literal; |
| 2840 | return slot; |
| 2841 | } |
| 2842 | |
| 2843 | |
| 2844 | // static |
| 2845 | TranslatedValue TranslatedValue::NewInvalid(TranslatedState* container) { |
| 2846 | return TranslatedValue(container, kInvalid); |
| 2847 | } |
| 2848 | |
| 2849 | |
| 2850 | Isolate* TranslatedValue::isolate() const { return container_->isolate(); } |
| 2851 | |
| 2852 | |
| 2853 | Object* TranslatedValue::raw_literal() const { |
| 2854 | DCHECK_EQ(kTagged, kind()); |
| 2855 | return raw_literal_; |
| 2856 | } |
| 2857 | |
| 2858 | |
| 2859 | int32_t TranslatedValue::int32_value() const { |
| 2860 | DCHECK_EQ(kInt32, kind()); |
| 2861 | return int32_value_; |
| 2862 | } |
| 2863 | |
| 2864 | |
| 2865 | uint32_t TranslatedValue::uint32_value() const { |
| 2866 | DCHECK(kind() == kUInt32 || kind() == kBoolBit); |
| 2867 | return uint32_value_; |
| 2868 | } |
| 2869 | |
| 2870 | |
| 2871 | double TranslatedValue::double_value() const { |
| 2872 | DCHECK_EQ(kDouble, kind()); |
| 2873 | return double_value_; |
| 2874 | } |
| 2875 | |
| 2876 | |
| 2877 | int TranslatedValue::object_length() const { |
| 2878 | DCHECK(kind() == kArgumentsObject || kind() == kCapturedObject); |
| 2879 | return materialization_info_.length_; |
| 2880 | } |
| 2881 | |
| 2882 | |
| 2883 | int TranslatedValue::object_index() const { |
| 2884 | DCHECK(kind() == kArgumentsObject || kind() == kCapturedObject || |
| 2885 | kind() == kDuplicatedObject); |
| 2886 | return materialization_info_.id_; |
| 2887 | } |
| 2888 | |
| 2889 | |
| 2890 | Object* TranslatedValue::GetRawValue() const { |
| 2891 | // If we have a value, return it. |
| 2892 | Handle<Object> result_handle; |
| 2893 | if (value_.ToHandle(&result_handle)) { |
| 2894 | return *result_handle; |
| 2895 | } |
| 2896 | |
| 2897 | // Otherwise, do a best effort to get the value without allocation. |
| 2898 | switch (kind()) { |
| 2899 | case kTagged: |
| 2900 | return raw_literal(); |
| 2901 | |
| 2902 | case kInt32: { |
| 2903 | bool is_smi = Smi::IsValid(int32_value()); |
| 2904 | if (is_smi) { |
| 2905 | return Smi::FromInt(int32_value()); |
| 2906 | } |
| 2907 | break; |
| 2908 | } |
| 2909 | |
| 2910 | case kUInt32: { |
| 2911 | bool is_smi = (uint32_value() <= static_cast<uintptr_t>(Smi::kMaxValue)); |
| 2912 | if (is_smi) { |
| 2913 | return Smi::FromInt(static_cast<int32_t>(uint32_value())); |
| 2914 | } |
| 2915 | break; |
| 2916 | } |
| 2917 | |
| 2918 | case kBoolBit: { |
| 2919 | if (uint32_value() == 0) { |
| 2920 | return isolate()->heap()->false_value(); |
| 2921 | } else { |
| 2922 | CHECK_EQ(1U, uint32_value()); |
| 2923 | return isolate()->heap()->true_value(); |
| 2924 | } |
| 2925 | } |
| 2926 | |
| 2927 | default: |
| 2928 | break; |
| 2929 | } |
| 2930 | |
| 2931 | // If we could not get the value without allocation, return the arguments |
| 2932 | // marker. |
| 2933 | return isolate()->heap()->arguments_marker(); |
| 2934 | } |
| 2935 | |
| 2936 | |
| 2937 | Handle<Object> TranslatedValue::GetValue() { |
| 2938 | Handle<Object> result; |
| 2939 | // If we already have a value, then get it. |
| 2940 | if (value_.ToHandle(&result)) return result; |
| 2941 | |
| 2942 | // Otherwise we have to materialize. |
| 2943 | switch (kind()) { |
| 2944 | case TranslatedValue::kTagged: |
| 2945 | case TranslatedValue::kInt32: |
| 2946 | case TranslatedValue::kUInt32: |
| 2947 | case TranslatedValue::kBoolBit: |
| 2948 | case TranslatedValue::kDouble: { |
| 2949 | MaterializeSimple(); |
| 2950 | return value_.ToHandleChecked(); |
| 2951 | } |
| 2952 | |
| 2953 | case TranslatedValue::kArgumentsObject: |
| 2954 | case TranslatedValue::kCapturedObject: |
| 2955 | case TranslatedValue::kDuplicatedObject: |
| 2956 | return container_->MaterializeObjectAt(object_index()); |
| 2957 | |
| 2958 | case TranslatedValue::kInvalid: |
| 2959 | FATAL("unexpected case"); |
| 2960 | return Handle<Object>::null(); |
| 2961 | } |
| 2962 | |
| 2963 | FATAL("internal error: value missing"); |
| 2964 | return Handle<Object>::null(); |
| 2965 | } |
| 2966 | |
| 2967 | |
| 2968 | void TranslatedValue::MaterializeSimple() { |
| 2969 | // If we already have materialized, return. |
| 2970 | if (!value_.is_null()) return; |
| 2971 | |
| 2972 | Object* raw_value = GetRawValue(); |
| 2973 | if (raw_value != isolate()->heap()->arguments_marker()) { |
| 2974 | // We can get the value without allocation, just return it here. |
| 2975 | value_ = Handle<Object>(raw_value, isolate()); |
| 2976 | return; |
| 2977 | } |
| 2978 | |
| 2979 | switch (kind()) { |
| 2980 | case kInt32: { |
| 2981 | value_ = Handle<Object>(isolate()->factory()->NewNumber(int32_value())); |
| 2982 | return; |
| 2983 | } |
| 2984 | |
| 2985 | case kUInt32: |
| 2986 | value_ = Handle<Object>(isolate()->factory()->NewNumber(uint32_value())); |
| 2987 | return; |
| 2988 | |
| 2989 | case kDouble: |
| 2990 | value_ = Handle<Object>(isolate()->factory()->NewNumber(double_value())); |
| 2991 | return; |
| 2992 | |
| 2993 | case kCapturedObject: |
| 2994 | case kDuplicatedObject: |
| 2995 | case kArgumentsObject: |
| 2996 | case kInvalid: |
| 2997 | case kTagged: |
| 2998 | case kBoolBit: |
| 2999 | FATAL("internal error: unexpected materialization."); |
| 3000 | break; |
| 3001 | } |
| 3002 | } |
| 3003 | |
| 3004 | |
| 3005 | bool TranslatedValue::IsMaterializedObject() const { |
| 3006 | switch (kind()) { |
| 3007 | case kCapturedObject: |
| 3008 | case kDuplicatedObject: |
| 3009 | case kArgumentsObject: |
| 3010 | return true; |
| 3011 | default: |
| 3012 | return false; |
| 3013 | } |
| 3014 | } |
| 3015 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 3016 | bool TranslatedValue::IsMaterializableByDebugger() const { |
| 3017 | // At the moment, we only allow materialization of doubles. |
| 3018 | return (kind() == kDouble); |
| 3019 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3020 | |
| 3021 | int TranslatedValue::GetChildrenCount() const { |
| 3022 | if (kind() == kCapturedObject || kind() == kArgumentsObject) { |
| 3023 | return object_length(); |
| 3024 | } else { |
| 3025 | return 0; |
| 3026 | } |
| 3027 | } |
| 3028 | |
| 3029 | |
| 3030 | uint32_t TranslatedState::GetUInt32Slot(Address fp, int slot_offset) { |
| 3031 | Address address = fp + slot_offset; |
| 3032 | #if V8_TARGET_BIG_ENDIAN && V8_HOST_ARCH_64_BIT |
| 3033 | return Memory::uint32_at(address + kIntSize); |
| 3034 | #else |
| 3035 | return Memory::uint32_at(address); |
| 3036 | #endif |
| 3037 | } |
| 3038 | |
| 3039 | |
| 3040 | void TranslatedValue::Handlify() { |
| 3041 | if (kind() == kTagged) { |
| 3042 | value_ = Handle<Object>(raw_literal(), isolate()); |
| 3043 | raw_literal_ = nullptr; |
| 3044 | } |
| 3045 | } |
| 3046 | |
| 3047 | |
| 3048 | TranslatedFrame TranslatedFrame::JSFrame(BailoutId node_id, |
| 3049 | SharedFunctionInfo* shared_info, |
| 3050 | int height) { |
| 3051 | TranslatedFrame frame(kFunction, shared_info->GetIsolate(), shared_info, |
| 3052 | height); |
| 3053 | frame.node_id_ = node_id; |
| 3054 | return frame; |
| 3055 | } |
| 3056 | |
| 3057 | |
| 3058 | TranslatedFrame TranslatedFrame::InterpretedFrame( |
| 3059 | BailoutId bytecode_offset, SharedFunctionInfo* shared_info, int height) { |
| 3060 | TranslatedFrame frame(kInterpretedFunction, shared_info->GetIsolate(), |
| 3061 | shared_info, height); |
| 3062 | frame.node_id_ = bytecode_offset; |
| 3063 | return frame; |
| 3064 | } |
| 3065 | |
| 3066 | |
| 3067 | TranslatedFrame TranslatedFrame::AccessorFrame( |
| 3068 | Kind kind, SharedFunctionInfo* shared_info) { |
| 3069 | DCHECK(kind == kSetter || kind == kGetter); |
| 3070 | return TranslatedFrame(kind, shared_info->GetIsolate(), shared_info); |
| 3071 | } |
| 3072 | |
| 3073 | |
| 3074 | TranslatedFrame TranslatedFrame::ArgumentsAdaptorFrame( |
| 3075 | SharedFunctionInfo* shared_info, int height) { |
| 3076 | return TranslatedFrame(kArgumentsAdaptor, shared_info->GetIsolate(), |
| 3077 | shared_info, height); |
| 3078 | } |
| 3079 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 3080 | TranslatedFrame TranslatedFrame::TailCallerFrame( |
| 3081 | SharedFunctionInfo* shared_info) { |
| 3082 | return TranslatedFrame(kTailCallerFunction, shared_info->GetIsolate(), |
| 3083 | shared_info, 0); |
| 3084 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3085 | |
| 3086 | TranslatedFrame TranslatedFrame::ConstructStubFrame( |
| 3087 | SharedFunctionInfo* shared_info, int height) { |
| 3088 | return TranslatedFrame(kConstructStub, shared_info->GetIsolate(), shared_info, |
| 3089 | height); |
| 3090 | } |
| 3091 | |
| 3092 | |
| 3093 | int TranslatedFrame::GetValueCount() { |
| 3094 | switch (kind()) { |
| 3095 | case kFunction: { |
| 3096 | int parameter_count = |
| 3097 | raw_shared_info_->internal_formal_parameter_count() + 1; |
| 3098 | // + 1 for function. |
| 3099 | return height_ + parameter_count + 1; |
| 3100 | } |
| 3101 | |
| 3102 | case kInterpretedFunction: { |
| 3103 | int parameter_count = |
| 3104 | raw_shared_info_->internal_formal_parameter_count() + 1; |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 3105 | // + 2 for function and context. |
| 3106 | return height_ + parameter_count + 2; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3107 | } |
| 3108 | |
| 3109 | case kGetter: |
| 3110 | return 2; // Function and receiver. |
| 3111 | |
| 3112 | case kSetter: |
| 3113 | return 3; // Function, receiver and the value to set. |
| 3114 | |
| 3115 | case kArgumentsAdaptor: |
| 3116 | case kConstructStub: |
| 3117 | return 1 + height_; |
| 3118 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 3119 | case kTailCallerFunction: |
| 3120 | return 1; // Function. |
| 3121 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3122 | case kCompiledStub: |
| 3123 | return height_; |
| 3124 | |
| 3125 | case kInvalid: |
| 3126 | UNREACHABLE(); |
| 3127 | break; |
| 3128 | } |
| 3129 | UNREACHABLE(); |
| 3130 | return -1; |
| 3131 | } |
| 3132 | |
| 3133 | |
| 3134 | void TranslatedFrame::Handlify() { |
| 3135 | if (raw_shared_info_ != nullptr) { |
| 3136 | shared_info_ = Handle<SharedFunctionInfo>(raw_shared_info_); |
| 3137 | raw_shared_info_ = nullptr; |
| 3138 | } |
| 3139 | for (auto& value : values_) { |
| 3140 | value.Handlify(); |
| 3141 | } |
| 3142 | } |
| 3143 | |
| 3144 | |
| 3145 | TranslatedFrame TranslatedState::CreateNextTranslatedFrame( |
| 3146 | TranslationIterator* iterator, FixedArray* literal_array, Address fp, |
| 3147 | FILE* trace_file) { |
| 3148 | Translation::Opcode opcode = |
| 3149 | static_cast<Translation::Opcode>(iterator->Next()); |
| 3150 | switch (opcode) { |
| 3151 | case Translation::JS_FRAME: { |
| 3152 | BailoutId node_id = BailoutId(iterator->Next()); |
| 3153 | SharedFunctionInfo* shared_info = |
| 3154 | SharedFunctionInfo::cast(literal_array->get(iterator->Next())); |
| 3155 | int height = iterator->Next(); |
| 3156 | if (trace_file != nullptr) { |
| 3157 | base::SmartArrayPointer<char> name = |
| 3158 | shared_info->DebugName()->ToCString(); |
| 3159 | PrintF(trace_file, " reading input frame %s", name.get()); |
| 3160 | int arg_count = shared_info->internal_formal_parameter_count() + 1; |
| 3161 | PrintF(trace_file, " => node=%d, args=%d, height=%d; inputs:\n", |
| 3162 | node_id.ToInt(), arg_count, height); |
| 3163 | } |
| 3164 | return TranslatedFrame::JSFrame(node_id, shared_info, height); |
| 3165 | } |
| 3166 | |
| 3167 | case Translation::INTERPRETED_FRAME: { |
| 3168 | BailoutId bytecode_offset = BailoutId(iterator->Next()); |
| 3169 | SharedFunctionInfo* shared_info = |
| 3170 | SharedFunctionInfo::cast(literal_array->get(iterator->Next())); |
| 3171 | int height = iterator->Next(); |
| 3172 | if (trace_file != nullptr) { |
| 3173 | base::SmartArrayPointer<char> name = |
| 3174 | shared_info->DebugName()->ToCString(); |
| 3175 | PrintF(trace_file, " reading input frame %s", name.get()); |
| 3176 | int arg_count = shared_info->internal_formal_parameter_count() + 1; |
| 3177 | PrintF(trace_file, |
| 3178 | " => bytecode_offset=%d, args=%d, height=%d; inputs:\n", |
| 3179 | bytecode_offset.ToInt(), arg_count, height); |
| 3180 | } |
| 3181 | return TranslatedFrame::InterpretedFrame(bytecode_offset, shared_info, |
| 3182 | height); |
| 3183 | } |
| 3184 | |
| 3185 | case Translation::ARGUMENTS_ADAPTOR_FRAME: { |
| 3186 | SharedFunctionInfo* shared_info = |
| 3187 | SharedFunctionInfo::cast(literal_array->get(iterator->Next())); |
| 3188 | int height = iterator->Next(); |
| 3189 | if (trace_file != nullptr) { |
| 3190 | base::SmartArrayPointer<char> name = |
| 3191 | shared_info->DebugName()->ToCString(); |
| 3192 | PrintF(trace_file, " reading arguments adaptor frame %s", name.get()); |
| 3193 | PrintF(trace_file, " => height=%d; inputs:\n", height); |
| 3194 | } |
| 3195 | return TranslatedFrame::ArgumentsAdaptorFrame(shared_info, height); |
| 3196 | } |
| 3197 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 3198 | case Translation::TAIL_CALLER_FRAME: { |
| 3199 | SharedFunctionInfo* shared_info = |
| 3200 | SharedFunctionInfo::cast(literal_array->get(iterator->Next())); |
| 3201 | if (trace_file != nullptr) { |
| 3202 | base::SmartArrayPointer<char> name = |
| 3203 | shared_info->DebugName()->ToCString(); |
| 3204 | PrintF(trace_file, " reading tail caller frame marker %s\n", |
| 3205 | name.get()); |
| 3206 | } |
| 3207 | return TranslatedFrame::TailCallerFrame(shared_info); |
| 3208 | } |
| 3209 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3210 | case Translation::CONSTRUCT_STUB_FRAME: { |
| 3211 | SharedFunctionInfo* shared_info = |
| 3212 | SharedFunctionInfo::cast(literal_array->get(iterator->Next())); |
| 3213 | int height = iterator->Next(); |
| 3214 | if (trace_file != nullptr) { |
| 3215 | base::SmartArrayPointer<char> name = |
| 3216 | shared_info->DebugName()->ToCString(); |
| 3217 | PrintF(trace_file, " reading construct stub frame %s", name.get()); |
| 3218 | PrintF(trace_file, " => height=%d; inputs:\n", height); |
| 3219 | } |
| 3220 | return TranslatedFrame::ConstructStubFrame(shared_info, height); |
| 3221 | } |
| 3222 | |
| 3223 | case Translation::GETTER_STUB_FRAME: { |
| 3224 | SharedFunctionInfo* shared_info = |
| 3225 | SharedFunctionInfo::cast(literal_array->get(iterator->Next())); |
| 3226 | if (trace_file != nullptr) { |
| 3227 | base::SmartArrayPointer<char> name = |
| 3228 | shared_info->DebugName()->ToCString(); |
| 3229 | PrintF(trace_file, " reading getter frame %s; inputs:\n", name.get()); |
| 3230 | } |
| 3231 | return TranslatedFrame::AccessorFrame(TranslatedFrame::kGetter, |
| 3232 | shared_info); |
| 3233 | } |
| 3234 | |
| 3235 | case Translation::SETTER_STUB_FRAME: { |
| 3236 | SharedFunctionInfo* shared_info = |
| 3237 | SharedFunctionInfo::cast(literal_array->get(iterator->Next())); |
| 3238 | if (trace_file != nullptr) { |
| 3239 | base::SmartArrayPointer<char> name = |
| 3240 | shared_info->DebugName()->ToCString(); |
| 3241 | PrintF(trace_file, " reading setter frame %s; inputs:\n", name.get()); |
| 3242 | } |
| 3243 | return TranslatedFrame::AccessorFrame(TranslatedFrame::kSetter, |
| 3244 | shared_info); |
| 3245 | } |
| 3246 | |
| 3247 | case Translation::COMPILED_STUB_FRAME: { |
| 3248 | int height = iterator->Next(); |
| 3249 | if (trace_file != nullptr) { |
| 3250 | PrintF(trace_file, |
| 3251 | " reading compiler stub frame => height=%d; inputs:\n", height); |
| 3252 | } |
| 3253 | return TranslatedFrame::CompiledStubFrame(height, |
| 3254 | literal_array->GetIsolate()); |
| 3255 | } |
| 3256 | |
| 3257 | case Translation::BEGIN: |
| 3258 | case Translation::DUPLICATED_OBJECT: |
| 3259 | case Translation::ARGUMENTS_OBJECT: |
| 3260 | case Translation::CAPTURED_OBJECT: |
| 3261 | case Translation::REGISTER: |
| 3262 | case Translation::INT32_REGISTER: |
| 3263 | case Translation::UINT32_REGISTER: |
| 3264 | case Translation::BOOL_REGISTER: |
| 3265 | case Translation::DOUBLE_REGISTER: |
| 3266 | case Translation::STACK_SLOT: |
| 3267 | case Translation::INT32_STACK_SLOT: |
| 3268 | case Translation::UINT32_STACK_SLOT: |
| 3269 | case Translation::BOOL_STACK_SLOT: |
| 3270 | case Translation::DOUBLE_STACK_SLOT: |
| 3271 | case Translation::LITERAL: |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3272 | break; |
| 3273 | } |
| 3274 | FATAL("We should never get here - unexpected deopt info."); |
| 3275 | return TranslatedFrame::InvalidFrame(); |
| 3276 | } |
| 3277 | |
| 3278 | |
| 3279 | // static |
| 3280 | void TranslatedFrame::AdvanceIterator( |
| 3281 | std::deque<TranslatedValue>::iterator* iter) { |
| 3282 | int values_to_skip = 1; |
| 3283 | while (values_to_skip > 0) { |
| 3284 | // Consume the current element. |
| 3285 | values_to_skip--; |
| 3286 | // Add all the children. |
| 3287 | values_to_skip += (*iter)->GetChildrenCount(); |
| 3288 | |
| 3289 | (*iter)++; |
| 3290 | } |
| 3291 | } |
| 3292 | |
| 3293 | |
| 3294 | // We can't intermix stack decoding and allocations because |
| 3295 | // deoptimization infrastracture is not GC safe. |
| 3296 | // Thus we build a temporary structure in malloced space. |
| 3297 | TranslatedValue TranslatedState::CreateNextTranslatedValue( |
| 3298 | int frame_index, int value_index, TranslationIterator* iterator, |
| 3299 | FixedArray* literal_array, Address fp, RegisterValues* registers, |
| 3300 | FILE* trace_file) { |
| 3301 | disasm::NameConverter converter; |
| 3302 | |
| 3303 | Translation::Opcode opcode = |
| 3304 | static_cast<Translation::Opcode>(iterator->Next()); |
| 3305 | switch (opcode) { |
| 3306 | case Translation::BEGIN: |
| 3307 | case Translation::JS_FRAME: |
| 3308 | case Translation::INTERPRETED_FRAME: |
| 3309 | case Translation::ARGUMENTS_ADAPTOR_FRAME: |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 3310 | case Translation::TAIL_CALLER_FRAME: |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3311 | case Translation::CONSTRUCT_STUB_FRAME: |
| 3312 | case Translation::GETTER_STUB_FRAME: |
| 3313 | case Translation::SETTER_STUB_FRAME: |
| 3314 | case Translation::COMPILED_STUB_FRAME: |
| 3315 | // Peeled off before getting here. |
| 3316 | break; |
| 3317 | |
| 3318 | case Translation::DUPLICATED_OBJECT: { |
| 3319 | int object_id = iterator->Next(); |
| 3320 | if (trace_file != nullptr) { |
| 3321 | PrintF(trace_file, "duplicated object #%d", object_id); |
| 3322 | } |
| 3323 | object_positions_.push_back(object_positions_[object_id]); |
| 3324 | return TranslatedValue::NewDuplicateObject(this, object_id); |
| 3325 | } |
| 3326 | |
| 3327 | case Translation::ARGUMENTS_OBJECT: { |
| 3328 | int arg_count = iterator->Next(); |
| 3329 | int object_index = static_cast<int>(object_positions_.size()); |
| 3330 | if (trace_file != nullptr) { |
| 3331 | PrintF(trace_file, "argumets object #%d (length = %d)", object_index, |
| 3332 | arg_count); |
| 3333 | } |
| 3334 | object_positions_.push_back({frame_index, value_index}); |
| 3335 | return TranslatedValue::NewArgumentsObject(this, arg_count, object_index); |
| 3336 | } |
| 3337 | |
| 3338 | case Translation::CAPTURED_OBJECT: { |
| 3339 | int field_count = iterator->Next(); |
| 3340 | int object_index = static_cast<int>(object_positions_.size()); |
| 3341 | if (trace_file != nullptr) { |
| 3342 | PrintF(trace_file, "captured object #%d (length = %d)", object_index, |
| 3343 | field_count); |
| 3344 | } |
| 3345 | object_positions_.push_back({frame_index, value_index}); |
| 3346 | return TranslatedValue::NewDeferredObject(this, field_count, |
| 3347 | object_index); |
| 3348 | } |
| 3349 | |
| 3350 | case Translation::REGISTER: { |
| 3351 | int input_reg = iterator->Next(); |
| 3352 | if (registers == nullptr) return TranslatedValue::NewInvalid(this); |
| 3353 | intptr_t value = registers->GetRegister(input_reg); |
| 3354 | if (trace_file != nullptr) { |
| 3355 | PrintF(trace_file, "0x%08" V8PRIxPTR " ; %s ", value, |
| 3356 | converter.NameOfCPURegister(input_reg)); |
| 3357 | reinterpret_cast<Object*>(value)->ShortPrint(trace_file); |
| 3358 | } |
| 3359 | return TranslatedValue::NewTagged(this, reinterpret_cast<Object*>(value)); |
| 3360 | } |
| 3361 | |
| 3362 | case Translation::INT32_REGISTER: { |
| 3363 | int input_reg = iterator->Next(); |
| 3364 | if (registers == nullptr) return TranslatedValue::NewInvalid(this); |
| 3365 | intptr_t value = registers->GetRegister(input_reg); |
| 3366 | if (trace_file != nullptr) { |
| 3367 | PrintF(trace_file, "%" V8PRIdPTR " ; %s ", value, |
| 3368 | converter.NameOfCPURegister(input_reg)); |
| 3369 | } |
| 3370 | return TranslatedValue::NewInt32(this, static_cast<int32_t>(value)); |
| 3371 | } |
| 3372 | |
| 3373 | case Translation::UINT32_REGISTER: { |
| 3374 | int input_reg = iterator->Next(); |
| 3375 | if (registers == nullptr) return TranslatedValue::NewInvalid(this); |
| 3376 | intptr_t value = registers->GetRegister(input_reg); |
| 3377 | if (trace_file != nullptr) { |
| 3378 | PrintF(trace_file, "%" V8PRIuPTR " ; %s (uint)", value, |
| 3379 | converter.NameOfCPURegister(input_reg)); |
| 3380 | reinterpret_cast<Object*>(value)->ShortPrint(trace_file); |
| 3381 | } |
| 3382 | return TranslatedValue::NewUInt32(this, static_cast<uint32_t>(value)); |
| 3383 | } |
| 3384 | |
| 3385 | case Translation::BOOL_REGISTER: { |
| 3386 | int input_reg = iterator->Next(); |
| 3387 | if (registers == nullptr) return TranslatedValue::NewInvalid(this); |
| 3388 | intptr_t value = registers->GetRegister(input_reg); |
| 3389 | if (trace_file != nullptr) { |
| 3390 | PrintF(trace_file, "%" V8PRIdPTR " ; %s (bool)", value, |
| 3391 | converter.NameOfCPURegister(input_reg)); |
| 3392 | } |
| 3393 | return TranslatedValue::NewBool(this, static_cast<uint32_t>(value)); |
| 3394 | } |
| 3395 | |
| 3396 | case Translation::DOUBLE_REGISTER: { |
| 3397 | int input_reg = iterator->Next(); |
| 3398 | if (registers == nullptr) return TranslatedValue::NewInvalid(this); |
| 3399 | double value = registers->GetDoubleRegister(input_reg); |
| 3400 | if (trace_file != nullptr) { |
| 3401 | PrintF(trace_file, "%e ; %s (bool)", value, |
| 3402 | DoubleRegister::from_code(input_reg).ToString()); |
| 3403 | } |
| 3404 | return TranslatedValue::NewDouble(this, value); |
| 3405 | } |
| 3406 | |
| 3407 | case Translation::STACK_SLOT: { |
| 3408 | int slot_offset = |
| 3409 | OptimizedFrame::StackSlotOffsetRelativeToFp(iterator->Next()); |
| 3410 | intptr_t value = *(reinterpret_cast<intptr_t*>(fp + slot_offset)); |
| 3411 | if (trace_file != nullptr) { |
| 3412 | PrintF(trace_file, "0x%08" V8PRIxPTR " ; [fp %c %d] ", value, |
| 3413 | slot_offset < 0 ? '-' : '+', std::abs(slot_offset)); |
| 3414 | reinterpret_cast<Object*>(value)->ShortPrint(trace_file); |
| 3415 | } |
| 3416 | return TranslatedValue::NewTagged(this, reinterpret_cast<Object*>(value)); |
| 3417 | } |
| 3418 | |
| 3419 | case Translation::INT32_STACK_SLOT: { |
| 3420 | int slot_offset = |
| 3421 | OptimizedFrame::StackSlotOffsetRelativeToFp(iterator->Next()); |
| 3422 | uint32_t value = GetUInt32Slot(fp, slot_offset); |
| 3423 | if (trace_file != nullptr) { |
| 3424 | PrintF(trace_file, "%d ; (int) [fp %c %d] ", |
| 3425 | static_cast<int32_t>(value), slot_offset < 0 ? '-' : '+', |
| 3426 | std::abs(slot_offset)); |
| 3427 | } |
| 3428 | return TranslatedValue::NewInt32(this, value); |
| 3429 | } |
| 3430 | |
| 3431 | case Translation::UINT32_STACK_SLOT: { |
| 3432 | int slot_offset = |
| 3433 | OptimizedFrame::StackSlotOffsetRelativeToFp(iterator->Next()); |
| 3434 | uint32_t value = GetUInt32Slot(fp, slot_offset); |
| 3435 | if (trace_file != nullptr) { |
| 3436 | PrintF(trace_file, "%u ; (uint) [fp %c %d] ", value, |
| 3437 | slot_offset < 0 ? '-' : '+', std::abs(slot_offset)); |
| 3438 | } |
| 3439 | return TranslatedValue::NewUInt32(this, value); |
| 3440 | } |
| 3441 | |
| 3442 | case Translation::BOOL_STACK_SLOT: { |
| 3443 | int slot_offset = |
| 3444 | OptimizedFrame::StackSlotOffsetRelativeToFp(iterator->Next()); |
| 3445 | uint32_t value = GetUInt32Slot(fp, slot_offset); |
| 3446 | if (trace_file != nullptr) { |
| 3447 | PrintF(trace_file, "%u ; (bool) [fp %c %d] ", value, |
| 3448 | slot_offset < 0 ? '-' : '+', std::abs(slot_offset)); |
| 3449 | } |
| 3450 | return TranslatedValue::NewBool(this, value); |
| 3451 | } |
| 3452 | |
| 3453 | case Translation::DOUBLE_STACK_SLOT: { |
| 3454 | int slot_offset = |
| 3455 | OptimizedFrame::StackSlotOffsetRelativeToFp(iterator->Next()); |
| 3456 | double value = ReadDoubleValue(fp + slot_offset); |
| 3457 | if (trace_file != nullptr) { |
| 3458 | PrintF(trace_file, "%e ; (double) [fp %c %d] ", value, |
| 3459 | slot_offset < 0 ? '-' : '+', std::abs(slot_offset)); |
| 3460 | } |
| 3461 | return TranslatedValue::NewDouble(this, value); |
| 3462 | } |
| 3463 | |
| 3464 | case Translation::LITERAL: { |
| 3465 | int literal_index = iterator->Next(); |
| 3466 | Object* value = literal_array->get(literal_index); |
| 3467 | if (trace_file != nullptr) { |
| 3468 | PrintF(trace_file, "0x%08" V8PRIxPTR " ; (literal %d) ", |
| 3469 | reinterpret_cast<intptr_t>(value), literal_index); |
| 3470 | reinterpret_cast<Object*>(value)->ShortPrint(trace_file); |
| 3471 | } |
| 3472 | |
| 3473 | return TranslatedValue::NewTagged(this, value); |
| 3474 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3475 | } |
| 3476 | |
| 3477 | FATAL("We should never get here - unexpected deopt info."); |
| 3478 | return TranslatedValue(nullptr, TranslatedValue::kInvalid); |
| 3479 | } |
| 3480 | |
| 3481 | |
| 3482 | TranslatedState::TranslatedState(JavaScriptFrame* frame) |
| 3483 | : isolate_(nullptr), |
| 3484 | stack_frame_pointer_(nullptr), |
| 3485 | has_adapted_arguments_(false) { |
| 3486 | int deopt_index = Safepoint::kNoDeoptimizationIndex; |
| 3487 | DeoptimizationInputData* data = |
| 3488 | static_cast<OptimizedFrame*>(frame)->GetDeoptimizationData(&deopt_index); |
| 3489 | TranslationIterator it(data->TranslationByteArray(), |
| 3490 | data->TranslationIndex(deopt_index)->value()); |
| 3491 | Init(frame->fp(), &it, data->LiteralArray(), nullptr /* registers */, |
| 3492 | nullptr /* trace file */); |
| 3493 | } |
| 3494 | |
| 3495 | |
| 3496 | TranslatedState::TranslatedState() |
| 3497 | : isolate_(nullptr), |
| 3498 | stack_frame_pointer_(nullptr), |
| 3499 | has_adapted_arguments_(false) {} |
| 3500 | |
| 3501 | |
| 3502 | void TranslatedState::Init(Address input_frame_pointer, |
| 3503 | TranslationIterator* iterator, |
| 3504 | FixedArray* literal_array, RegisterValues* registers, |
| 3505 | FILE* trace_file) { |
| 3506 | DCHECK(frames_.empty()); |
| 3507 | |
| 3508 | isolate_ = literal_array->GetIsolate(); |
| 3509 | // Read out the 'header' translation. |
| 3510 | Translation::Opcode opcode = |
| 3511 | static_cast<Translation::Opcode>(iterator->Next()); |
| 3512 | CHECK(opcode == Translation::BEGIN); |
| 3513 | |
| 3514 | int count = iterator->Next(); |
| 3515 | iterator->Next(); // Drop JS frames count. |
| 3516 | |
| 3517 | frames_.reserve(count); |
| 3518 | |
| 3519 | std::stack<int> nested_counts; |
| 3520 | |
| 3521 | // Read the frames |
| 3522 | for (int i = 0; i < count; i++) { |
| 3523 | // Read the frame descriptor. |
| 3524 | frames_.push_back(CreateNextTranslatedFrame( |
| 3525 | iterator, literal_array, input_frame_pointer, trace_file)); |
| 3526 | TranslatedFrame& frame = frames_.back(); |
| 3527 | |
| 3528 | // Read the values. |
| 3529 | int values_to_process = frame.GetValueCount(); |
| 3530 | while (values_to_process > 0 || !nested_counts.empty()) { |
| 3531 | if (trace_file != nullptr) { |
| 3532 | if (nested_counts.empty()) { |
| 3533 | // For top level values, print the value number. |
| 3534 | PrintF(trace_file, " %3i: ", |
| 3535 | frame.GetValueCount() - values_to_process); |
| 3536 | } else { |
| 3537 | // Take care of indenting for nested values. |
| 3538 | PrintF(trace_file, " "); |
| 3539 | for (size_t j = 0; j < nested_counts.size(); j++) { |
| 3540 | PrintF(trace_file, " "); |
| 3541 | } |
| 3542 | } |
| 3543 | } |
| 3544 | |
| 3545 | TranslatedValue value = CreateNextTranslatedValue( |
| 3546 | i, static_cast<int>(frame.values_.size()), iterator, literal_array, |
| 3547 | input_frame_pointer, registers, trace_file); |
| 3548 | frame.Add(value); |
| 3549 | |
| 3550 | if (trace_file != nullptr) { |
| 3551 | PrintF(trace_file, "\n"); |
| 3552 | } |
| 3553 | |
| 3554 | // Update the value count and resolve the nesting. |
| 3555 | values_to_process--; |
| 3556 | int children_count = value.GetChildrenCount(); |
| 3557 | if (children_count > 0) { |
| 3558 | nested_counts.push(values_to_process); |
| 3559 | values_to_process = children_count; |
| 3560 | } else { |
| 3561 | while (values_to_process == 0 && !nested_counts.empty()) { |
| 3562 | values_to_process = nested_counts.top(); |
| 3563 | nested_counts.pop(); |
| 3564 | } |
| 3565 | } |
| 3566 | } |
| 3567 | } |
| 3568 | |
| 3569 | CHECK(!iterator->HasNext() || |
| 3570 | static_cast<Translation::Opcode>(iterator->Next()) == |
| 3571 | Translation::BEGIN); |
| 3572 | } |
| 3573 | |
| 3574 | |
| 3575 | void TranslatedState::Prepare(bool has_adapted_arguments, |
| 3576 | Address stack_frame_pointer) { |
| 3577 | for (auto& frame : frames_) frame.Handlify(); |
| 3578 | |
| 3579 | stack_frame_pointer_ = stack_frame_pointer; |
| 3580 | has_adapted_arguments_ = has_adapted_arguments; |
| 3581 | |
| 3582 | UpdateFromPreviouslyMaterializedObjects(); |
| 3583 | } |
| 3584 | |
| 3585 | |
| 3586 | Handle<Object> TranslatedState::MaterializeAt(int frame_index, |
| 3587 | int* value_index) { |
| 3588 | TranslatedFrame* frame = &(frames_[frame_index]); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 3589 | CHECK(static_cast<size_t>(*value_index) < frame->values_.size()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3590 | |
| 3591 | TranslatedValue* slot = &(frame->values_[*value_index]); |
| 3592 | (*value_index)++; |
| 3593 | |
| 3594 | switch (slot->kind()) { |
| 3595 | case TranslatedValue::kTagged: |
| 3596 | case TranslatedValue::kInt32: |
| 3597 | case TranslatedValue::kUInt32: |
| 3598 | case TranslatedValue::kBoolBit: |
| 3599 | case TranslatedValue::kDouble: { |
| 3600 | slot->MaterializeSimple(); |
| 3601 | Handle<Object> value = slot->GetValue(); |
| 3602 | if (value->IsMutableHeapNumber()) { |
| 3603 | HeapNumber::cast(*value)->set_map(isolate()->heap()->heap_number_map()); |
| 3604 | } |
| 3605 | return value; |
| 3606 | } |
| 3607 | |
| 3608 | case TranslatedValue::kArgumentsObject: { |
| 3609 | int length = slot->GetChildrenCount(); |
| 3610 | Handle<JSObject> arguments; |
| 3611 | if (GetAdaptedArguments(&arguments, frame_index)) { |
| 3612 | // Store the materialized object and consume the nested values. |
| 3613 | for (int i = 0; i < length; ++i) { |
| 3614 | MaterializeAt(frame_index, value_index); |
| 3615 | } |
| 3616 | } else { |
| 3617 | Handle<JSFunction> function = |
| 3618 | Handle<JSFunction>::cast(frame->front().GetValue()); |
| 3619 | arguments = isolate_->factory()->NewArgumentsObject(function, length); |
| 3620 | Handle<FixedArray> array = isolate_->factory()->NewFixedArray(length); |
| 3621 | DCHECK_EQ(array->length(), length); |
| 3622 | arguments->set_elements(*array); |
| 3623 | for (int i = 0; i < length; ++i) { |
| 3624 | Handle<Object> value = MaterializeAt(frame_index, value_index); |
| 3625 | array->set(i, *value); |
| 3626 | } |
| 3627 | } |
| 3628 | slot->value_ = arguments; |
| 3629 | return arguments; |
| 3630 | } |
| 3631 | case TranslatedValue::kCapturedObject: { |
| 3632 | int length = slot->GetChildrenCount(); |
| 3633 | |
| 3634 | // The map must be a tagged object. |
| 3635 | CHECK(frame->values_[*value_index].kind() == TranslatedValue::kTagged); |
| 3636 | |
| 3637 | Handle<Object> result; |
| 3638 | if (slot->value_.ToHandle(&result)) { |
| 3639 | // This has been previously materialized, return the previous value. |
| 3640 | // We still need to skip all the nested objects. |
| 3641 | for (int i = 0; i < length; i++) { |
| 3642 | MaterializeAt(frame_index, value_index); |
| 3643 | } |
| 3644 | |
| 3645 | return result; |
| 3646 | } |
| 3647 | |
| 3648 | Handle<Object> map_object = MaterializeAt(frame_index, value_index); |
| 3649 | Handle<Map> map = |
| 3650 | Map::GeneralizeAllFieldRepresentations(Handle<Map>::cast(map_object)); |
| 3651 | switch (map->instance_type()) { |
| 3652 | case MUTABLE_HEAP_NUMBER_TYPE: |
| 3653 | case HEAP_NUMBER_TYPE: { |
| 3654 | // Reuse the HeapNumber value directly as it is already properly |
| 3655 | // tagged and skip materializing the HeapNumber explicitly. |
| 3656 | Handle<Object> object = MaterializeAt(frame_index, value_index); |
| 3657 | slot->value_ = object; |
| 3658 | // On 32-bit architectures, there is an extra slot there because |
| 3659 | // the escape analysis calculates the number of slots as |
| 3660 | // object-size/pointer-size. To account for this, we read out |
| 3661 | // any extra slots. |
| 3662 | for (int i = 0; i < length - 2; i++) { |
| 3663 | MaterializeAt(frame_index, value_index); |
| 3664 | } |
| 3665 | return object; |
| 3666 | } |
| 3667 | case JS_OBJECT_TYPE: { |
| 3668 | Handle<JSObject> object = |
| 3669 | isolate_->factory()->NewJSObjectFromMap(map, NOT_TENURED); |
| 3670 | slot->value_ = object; |
| 3671 | Handle<Object> properties = MaterializeAt(frame_index, value_index); |
| 3672 | Handle<Object> elements = MaterializeAt(frame_index, value_index); |
| 3673 | object->set_properties(FixedArray::cast(*properties)); |
| 3674 | object->set_elements(FixedArrayBase::cast(*elements)); |
| 3675 | for (int i = 0; i < length - 3; ++i) { |
| 3676 | Handle<Object> value = MaterializeAt(frame_index, value_index); |
| 3677 | FieldIndex index = FieldIndex::ForPropertyIndex(object->map(), i); |
| 3678 | object->FastPropertyAtPut(index, *value); |
| 3679 | } |
| 3680 | return object; |
| 3681 | } |
| 3682 | case JS_ARRAY_TYPE: { |
| 3683 | Handle<JSArray> object = |
| 3684 | isolate_->factory()->NewJSArray(0, map->elements_kind()); |
| 3685 | slot->value_ = object; |
| 3686 | Handle<Object> properties = MaterializeAt(frame_index, value_index); |
| 3687 | Handle<Object> elements = MaterializeAt(frame_index, value_index); |
| 3688 | Handle<Object> length = MaterializeAt(frame_index, value_index); |
| 3689 | object->set_properties(FixedArray::cast(*properties)); |
| 3690 | object->set_elements(FixedArrayBase::cast(*elements)); |
| 3691 | object->set_length(*length); |
| 3692 | return object; |
| 3693 | } |
| 3694 | case FIXED_ARRAY_TYPE: { |
| 3695 | Handle<Object> lengthObject = MaterializeAt(frame_index, value_index); |
| 3696 | int32_t length = 0; |
| 3697 | CHECK(lengthObject->ToInt32(&length)); |
| 3698 | Handle<FixedArray> object = |
| 3699 | isolate_->factory()->NewFixedArray(length); |
| 3700 | // We need to set the map, because the fixed array we are |
| 3701 | // materializing could be a context or an arguments object, |
| 3702 | // in which case we must retain that information. |
| 3703 | object->set_map(*map); |
| 3704 | slot->value_ = object; |
| 3705 | for (int i = 0; i < length; ++i) { |
| 3706 | Handle<Object> value = MaterializeAt(frame_index, value_index); |
| 3707 | object->set(i, *value); |
| 3708 | } |
| 3709 | return object; |
| 3710 | } |
| 3711 | case FIXED_DOUBLE_ARRAY_TYPE: { |
| 3712 | DCHECK_EQ(*map, isolate_->heap()->fixed_double_array_map()); |
| 3713 | Handle<Object> lengthObject = MaterializeAt(frame_index, value_index); |
| 3714 | int32_t length = 0; |
| 3715 | CHECK(lengthObject->ToInt32(&length)); |
| 3716 | Handle<FixedArrayBase> object = |
| 3717 | isolate_->factory()->NewFixedDoubleArray(length); |
| 3718 | slot->value_ = object; |
| 3719 | if (length > 0) { |
| 3720 | Handle<FixedDoubleArray> double_array = |
| 3721 | Handle<FixedDoubleArray>::cast(object); |
| 3722 | for (int i = 0; i < length; ++i) { |
| 3723 | Handle<Object> value = MaterializeAt(frame_index, value_index); |
| 3724 | CHECK(value->IsNumber()); |
| 3725 | double_array->set(i, value->Number()); |
| 3726 | } |
| 3727 | } |
| 3728 | return object; |
| 3729 | } |
| 3730 | default: |
| 3731 | PrintF(stderr, "[couldn't handle instance type %d]\n", |
| 3732 | map->instance_type()); |
| 3733 | FATAL("unreachable"); |
| 3734 | return Handle<Object>::null(); |
| 3735 | } |
| 3736 | UNREACHABLE(); |
| 3737 | break; |
| 3738 | } |
| 3739 | |
| 3740 | case TranslatedValue::kDuplicatedObject: { |
| 3741 | int object_index = slot->object_index(); |
| 3742 | TranslatedState::ObjectPosition pos = object_positions_[object_index]; |
| 3743 | |
| 3744 | // Make sure the duplicate is refering to a previous object. |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 3745 | CHECK(pos.frame_index_ < frame_index || |
| 3746 | (pos.frame_index_ == frame_index && |
| 3747 | pos.value_index_ < *value_index - 1)); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3748 | |
| 3749 | Handle<Object> object = |
| 3750 | frames_[pos.frame_index_].values_[pos.value_index_].GetValue(); |
| 3751 | |
| 3752 | // The object should have a (non-sentinel) value. |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 3753 | CHECK(!object.is_null() && |
| 3754 | !object.is_identical_to(isolate_->factory()->arguments_marker())); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3755 | |
| 3756 | slot->value_ = object; |
| 3757 | return object; |
| 3758 | } |
| 3759 | |
| 3760 | case TranslatedValue::kInvalid: |
| 3761 | UNREACHABLE(); |
| 3762 | break; |
| 3763 | } |
| 3764 | |
| 3765 | FATAL("We should never get here - unexpected deopt slot kind."); |
| 3766 | return Handle<Object>::null(); |
| 3767 | } |
| 3768 | |
| 3769 | |
| 3770 | Handle<Object> TranslatedState::MaterializeObjectAt(int object_index) { |
| 3771 | TranslatedState::ObjectPosition pos = object_positions_[object_index]; |
| 3772 | return MaterializeAt(pos.frame_index_, &(pos.value_index_)); |
| 3773 | } |
| 3774 | |
| 3775 | |
| 3776 | bool TranslatedState::GetAdaptedArguments(Handle<JSObject>* result, |
| 3777 | int frame_index) { |
| 3778 | if (frame_index == 0) { |
| 3779 | // Top level frame -> we need to go to the parent frame on the stack. |
| 3780 | if (!has_adapted_arguments_) return false; |
| 3781 | |
| 3782 | // This is top level frame, so we need to go to the stack to get |
| 3783 | // this function's argument. (Note that this relies on not inlining |
| 3784 | // recursive functions!) |
| 3785 | Handle<JSFunction> function = |
| 3786 | Handle<JSFunction>::cast(frames_[frame_index].front().GetValue()); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 3787 | *result = Accessors::FunctionGetArguments(function); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3788 | return true; |
| 3789 | } else { |
| 3790 | TranslatedFrame* previous_frame = &(frames_[frame_index]); |
| 3791 | if (previous_frame->kind() != TranslatedFrame::kArgumentsAdaptor) { |
| 3792 | return false; |
| 3793 | } |
| 3794 | // We get the adapted arguments from the parent translation. |
| 3795 | int length = previous_frame->height(); |
| 3796 | Handle<JSFunction> function = |
| 3797 | Handle<JSFunction>::cast(previous_frame->front().GetValue()); |
| 3798 | Handle<JSObject> arguments = |
| 3799 | isolate_->factory()->NewArgumentsObject(function, length); |
| 3800 | Handle<FixedArray> array = isolate_->factory()->NewFixedArray(length); |
| 3801 | arguments->set_elements(*array); |
| 3802 | TranslatedFrame::iterator arg_iterator = previous_frame->begin(); |
| 3803 | arg_iterator++; // Skip function. |
| 3804 | for (int i = 0; i < length; ++i) { |
| 3805 | Handle<Object> value = arg_iterator->GetValue(); |
| 3806 | array->set(i, *value); |
| 3807 | arg_iterator++; |
| 3808 | } |
| 3809 | CHECK(arg_iterator == previous_frame->end()); |
| 3810 | *result = arguments; |
| 3811 | return true; |
| 3812 | } |
| 3813 | } |
| 3814 | |
| 3815 | |
| 3816 | TranslatedFrame* TranslatedState::GetArgumentsInfoFromJSFrameIndex( |
| 3817 | int jsframe_index, int* args_count) { |
| 3818 | for (size_t i = 0; i < frames_.size(); i++) { |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 3819 | if (frames_[i].kind() == TranslatedFrame::kFunction || |
| 3820 | frames_[i].kind() == TranslatedFrame::kInterpretedFunction) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3821 | if (jsframe_index > 0) { |
| 3822 | jsframe_index--; |
| 3823 | } else { |
| 3824 | // We have the JS function frame, now check if it has arguments adaptor. |
| 3825 | if (i > 0 && |
| 3826 | frames_[i - 1].kind() == TranslatedFrame::kArgumentsAdaptor) { |
| 3827 | *args_count = frames_[i - 1].height(); |
| 3828 | return &(frames_[i - 1]); |
| 3829 | } |
| 3830 | *args_count = |
| 3831 | frames_[i].shared_info()->internal_formal_parameter_count() + 1; |
| 3832 | return &(frames_[i]); |
| 3833 | } |
| 3834 | } |
| 3835 | } |
| 3836 | return nullptr; |
| 3837 | } |
| 3838 | |
| 3839 | |
| 3840 | void TranslatedState::StoreMaterializedValuesAndDeopt() { |
| 3841 | MaterializedObjectStore* materialized_store = |
| 3842 | isolate_->materialized_object_store(); |
| 3843 | Handle<FixedArray> previously_materialized_objects = |
| 3844 | materialized_store->Get(stack_frame_pointer_); |
| 3845 | |
| 3846 | Handle<Object> marker = isolate_->factory()->arguments_marker(); |
| 3847 | |
| 3848 | int length = static_cast<int>(object_positions_.size()); |
| 3849 | bool new_store = false; |
| 3850 | if (previously_materialized_objects.is_null()) { |
| 3851 | previously_materialized_objects = |
| 3852 | isolate_->factory()->NewFixedArray(length); |
| 3853 | for (int i = 0; i < length; i++) { |
| 3854 | previously_materialized_objects->set(i, *marker); |
| 3855 | } |
| 3856 | new_store = true; |
| 3857 | } |
| 3858 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 3859 | CHECK_EQ(length, previously_materialized_objects->length()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3860 | |
| 3861 | bool value_changed = false; |
| 3862 | for (int i = 0; i < length; i++) { |
| 3863 | TranslatedState::ObjectPosition pos = object_positions_[i]; |
| 3864 | TranslatedValue* value_info = |
| 3865 | &(frames_[pos.frame_index_].values_[pos.value_index_]); |
| 3866 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 3867 | CHECK(value_info->IsMaterializedObject()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3868 | |
| 3869 | Handle<Object> value(value_info->GetRawValue(), isolate_); |
| 3870 | |
| 3871 | if (!value.is_identical_to(marker)) { |
| 3872 | if (previously_materialized_objects->get(i) == *marker) { |
| 3873 | previously_materialized_objects->set(i, *value); |
| 3874 | value_changed = true; |
| 3875 | } else { |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 3876 | CHECK(previously_materialized_objects->get(i) == *value); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3877 | } |
| 3878 | } |
| 3879 | } |
| 3880 | if (new_store && value_changed) { |
| 3881 | materialized_store->Set(stack_frame_pointer_, |
| 3882 | previously_materialized_objects); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 3883 | CHECK(frames_[0].kind() == TranslatedFrame::kFunction || |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 3884 | frames_[0].kind() == TranslatedFrame::kInterpretedFunction || |
| 3885 | frames_[0].kind() == TranslatedFrame::kTailCallerFunction); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3886 | Object* const function = frames_[0].front().GetRawValue(); |
| 3887 | Deoptimizer::DeoptimizeFunction(JSFunction::cast(function)); |
| 3888 | } |
| 3889 | } |
| 3890 | |
| 3891 | |
| 3892 | void TranslatedState::UpdateFromPreviouslyMaterializedObjects() { |
| 3893 | MaterializedObjectStore* materialized_store = |
| 3894 | isolate_->materialized_object_store(); |
| 3895 | Handle<FixedArray> previously_materialized_objects = |
| 3896 | materialized_store->Get(stack_frame_pointer_); |
| 3897 | |
| 3898 | // If we have no previously materialized objects, there is nothing to do. |
| 3899 | if (previously_materialized_objects.is_null()) return; |
| 3900 | |
| 3901 | Handle<Object> marker = isolate_->factory()->arguments_marker(); |
| 3902 | |
| 3903 | int length = static_cast<int>(object_positions_.size()); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 3904 | CHECK_EQ(length, previously_materialized_objects->length()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3905 | |
| 3906 | for (int i = 0; i < length; i++) { |
| 3907 | // For a previously materialized objects, inject their value into the |
| 3908 | // translated values. |
| 3909 | if (previously_materialized_objects->get(i) != *marker) { |
| 3910 | TranslatedState::ObjectPosition pos = object_positions_[i]; |
| 3911 | TranslatedValue* value_info = |
| 3912 | &(frames_[pos.frame_index_].values_[pos.value_index_]); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 3913 | CHECK(value_info->IsMaterializedObject()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3914 | |
| 3915 | value_info->value_ = |
| 3916 | Handle<Object>(previously_materialized_objects->get(i), isolate_); |
| 3917 | } |
| 3918 | } |
| 3919 | } |
| 3920 | |
| 3921 | } // namespace internal |
| 3922 | } // namespace v8 |