blob: 10685b61931bdc8f79a3537790c49683261ab756 [file] [log] [blame]
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001// Copyright 2012 the V8 project authors. All rights reserved.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
Ben Murdochb0fe1622011-05-05 13:52:32 +01004
5#ifndef V8_DEOPTIMIZER_H_
6#define V8_DEOPTIMIZER_H_
7
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008#include "src/allocation.h"
9#include "src/macro-assembler.h"
Ben Murdochb0fe1622011-05-05 13:52:32 +010010
11
12namespace v8 {
13namespace internal {
14
15class FrameDescription;
16class TranslationIterator;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000017class DeoptimizedFrameInfo;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000018class TranslatedState;
19class RegisterValues;
Ben Murdochb0fe1622011-05-05 13:52:32 +010020
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000021class TranslatedValue {
Ben Murdochb0fe1622011-05-05 13:52:32 +010022 public:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000023 // Allocation-less getter of the value.
24 // Returns heap()->arguments_marker() if allocation would be
25 // necessary to get the value.
26 Object* GetRawValue() const;
27 Handle<Object> GetValue();
Ben Murdochb8a8cc12014-11-26 15:28:44 +000028
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000029 bool IsMaterializedObject() const;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000030
31 private:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000032 friend class TranslatedState;
33 friend class TranslatedFrame;
34
35 enum Kind {
36 kInvalid,
37 kTagged,
38 kInt32,
39 kUInt32,
40 kBoolBit,
41 kDouble,
42 kCapturedObject, // Object captured by the escape analysis.
43 // The number of nested objects can be obtained
44 // with the DeferredObjectLength() method
45 // (the values of the nested objects follow
46 // this value in the depth-first order.)
47 kDuplicatedObject, // Duplicated object of a deferred object.
48 kArgumentsObject // Arguments object - only used to keep indexing
49 // in sync, it should not be materialized.
50 };
51
52 TranslatedValue(TranslatedState* container, Kind kind)
53 : kind_(kind), container_(container) {}
54 Kind kind() const { return kind_; }
55 void Handlify();
56 int GetChildrenCount() const;
57
58 static TranslatedValue NewArgumentsObject(TranslatedState* container,
59 int length, int object_index);
60 static TranslatedValue NewDeferredObject(TranslatedState* container,
61 int length, int object_index);
62 static TranslatedValue NewDuplicateObject(TranslatedState* container, int id);
63 static TranslatedValue NewDouble(TranslatedState* container, double value);
64 static TranslatedValue NewInt32(TranslatedState* container, int32_t value);
65 static TranslatedValue NewUInt32(TranslatedState* container, uint32_t value);
66 static TranslatedValue NewBool(TranslatedState* container, uint32_t value);
67 static TranslatedValue NewTagged(TranslatedState* container, Object* literal);
68 static TranslatedValue NewInvalid(TranslatedState* container);
69
70 Isolate* isolate() const;
71 void MaterializeSimple();
72
73 Kind kind_;
74 TranslatedState* container_; // This is only needed for materialization of
75 // objects and constructing handles (to get
76 // to the isolate).
77
78 MaybeHandle<Object> value_; // Before handlification, this is always null,
79 // after materialization it is never null,
80 // in between it is only null if the value needs
81 // to be materialized.
82
83 struct MaterializedObjectInfo {
84 int id_;
85 int length_; // Applies only to kArgumentsObject or kCapturedObject kinds.
86 };
87
88 union {
89 // kind kTagged. After handlification it is always nullptr.
90 Object* raw_literal_;
91 // kind is kUInt32 or kBoolBit.
92 uint32_t uint32_value_;
93 // kind is kInt32.
94 int32_t int32_value_;
95 // kind is kDouble
96 double double_value_;
97 // kind is kDuplicatedObject or kArgumentsObject or kCapturedObject.
98 MaterializedObjectInfo materialization_info_;
99 };
100
101 // Checked accessors for the union members.
102 Object* raw_literal() const;
103 int32_t int32_value() const;
104 uint32_t uint32_value() const;
105 double double_value() const;
106 int object_length() const;
107 int object_index() const;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000108};
109
110
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000111class TranslatedFrame {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000112 public:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000113 enum Kind {
114 kFunction,
115 kInterpretedFunction,
116 kGetter,
117 kSetter,
118 kArgumentsAdaptor,
119 kConstructStub,
120 kCompiledStub,
121 kInvalid
122 };
Ben Murdoch8b112d22011-06-08 16:22:53 +0100123
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000124 int GetValueCount();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000125
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000126 Kind kind() const { return kind_; }
127 BailoutId node_id() const { return node_id_; }
128 Handle<SharedFunctionInfo> shared_info() const { return shared_info_; }
129 int height() const { return height_; }
130
131 class iterator {
132 public:
133 iterator& operator++() {
134 AdvanceIterator(&position_);
135 return *this;
136 }
137
138 iterator operator++(int) {
139 iterator original(position_);
140 AdvanceIterator(&position_);
141 return original;
142 }
143
144 bool operator==(const iterator& other) const {
145 return position_ == other.position_;
146 }
147 bool operator!=(const iterator& other) const { return !(*this == other); }
148
149 TranslatedValue& operator*() { return (*position_); }
150 TranslatedValue* operator->() { return &(*position_); }
151
152 private:
153 friend TranslatedFrame;
154
155 explicit iterator(std::deque<TranslatedValue>::iterator position)
156 : position_(position) {}
157
158 std::deque<TranslatedValue>::iterator position_;
159 };
160
161 typedef TranslatedValue& reference;
162 typedef TranslatedValue const& const_reference;
163
164 iterator begin() { return iterator(values_.begin()); }
165 iterator end() { return iterator(values_.end()); }
166
167 reference front() { return values_.front(); }
168 const_reference front() const { return values_.front(); }
Ben Murdochb0fe1622011-05-05 13:52:32 +0100169
170 private:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000171 friend class TranslatedState;
172
173 // Constructor static methods.
174 static TranslatedFrame JSFrame(BailoutId node_id,
175 SharedFunctionInfo* shared_info, int height);
176 static TranslatedFrame InterpretedFrame(BailoutId bytecode_offset,
177 SharedFunctionInfo* shared_info,
178 int height);
179 static TranslatedFrame AccessorFrame(Kind kind,
180 SharedFunctionInfo* shared_info);
181 static TranslatedFrame ArgumentsAdaptorFrame(SharedFunctionInfo* shared_info,
182 int height);
183 static TranslatedFrame ConstructStubFrame(SharedFunctionInfo* shared_info,
184 int height);
185 static TranslatedFrame CompiledStubFrame(int height, Isolate* isolate) {
186 return TranslatedFrame(kCompiledStub, isolate, nullptr, height);
187 }
188 static TranslatedFrame InvalidFrame() {
189 return TranslatedFrame(kInvalid, nullptr);
190 }
191
192 static void AdvanceIterator(std::deque<TranslatedValue>::iterator* iter);
193
194 TranslatedFrame(Kind kind, Isolate* isolate,
195 SharedFunctionInfo* shared_info = nullptr, int height = 0)
196 : kind_(kind),
197 node_id_(BailoutId::None()),
198 raw_shared_info_(shared_info),
199 height_(height),
200 isolate_(isolate) {}
201
202
203 void Add(const TranslatedValue& value) { values_.push_back(value); }
204 void Handlify();
205
206 Kind kind_;
207 BailoutId node_id_;
208 SharedFunctionInfo* raw_shared_info_;
209 Handle<SharedFunctionInfo> shared_info_;
210 int height_;
211 Isolate* isolate_;
212
213 typedef std::deque<TranslatedValue> ValuesContainer;
214
215 ValuesContainer values_;
216};
217
218
219// Auxiliary class for translating deoptimization values.
220// Typical usage sequence:
221//
222// 1. Construct the instance. This will involve reading out the translations
223// and resolving them to values using the supplied frame pointer and
224// machine state (registers). This phase is guaranteed not to allocate
225// and not to use any HandleScope. Any object pointers will be stored raw.
226//
227// 2. Handlify pointers. This will convert all the raw pointers to handles.
228//
229// 3. Reading out the frame values.
230//
231// Note: After the instance is constructed, it is possible to iterate over
232// the values eagerly.
233
234class TranslatedState {
235 public:
236 TranslatedState();
237 explicit TranslatedState(JavaScriptFrame* frame);
238
239 void Prepare(bool has_adapted_arguments, Address stack_frame_pointer);
240
241 // Store newly materialized values into the isolate.
242 void StoreMaterializedValuesAndDeopt();
243
244 typedef std::vector<TranslatedFrame>::iterator iterator;
245 iterator begin() { return frames_.begin(); }
246 iterator end() { return frames_.end(); }
247
248 typedef std::vector<TranslatedFrame>::const_iterator const_iterator;
249 const_iterator begin() const { return frames_.begin(); }
250 const_iterator end() const { return frames_.end(); }
251
252 std::vector<TranslatedFrame>& frames() { return frames_; }
253
254 TranslatedFrame* GetArgumentsInfoFromJSFrameIndex(int jsframe_index,
255 int* arguments_count);
256
257 Isolate* isolate() { return isolate_; }
258
259 void Init(Address input_frame_pointer, TranslationIterator* iterator,
260 FixedArray* literal_array, RegisterValues* registers,
261 FILE* trace_file);
262
263 private:
264 friend TranslatedValue;
265
266 TranslatedFrame CreateNextTranslatedFrame(TranslationIterator* iterator,
267 FixedArray* literal_array,
268 Address fp,
269 FILE* trace_file);
270 TranslatedValue CreateNextTranslatedValue(int frame_index, int value_index,
271 TranslationIterator* iterator,
272 FixedArray* literal_array,
273 Address fp,
274 RegisterValues* registers,
275 FILE* trace_file);
276
277 void UpdateFromPreviouslyMaterializedObjects();
278 Handle<Object> MaterializeAt(int frame_index, int* value_index);
279 Handle<Object> MaterializeObjectAt(int object_index);
280 bool GetAdaptedArguments(Handle<JSObject>* result, int frame_index);
281
282 static uint32_t GetUInt32Slot(Address fp, int slot_index);
283
284 std::vector<TranslatedFrame> frames_;
285 Isolate* isolate_;
286 Address stack_frame_pointer_;
287 bool has_adapted_arguments_;
288
289 struct ObjectPosition {
290 int frame_index_;
291 int value_index_;
292 };
293 std::deque<ObjectPosition> object_positions_;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100294};
295
296
297class OptimizedFunctionVisitor BASE_EMBEDDED {
298 public:
299 virtual ~OptimizedFunctionVisitor() {}
300
301 // Function which is called before iteration of any optimized functions
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000302 // from given native context.
Ben Murdochb0fe1622011-05-05 13:52:32 +0100303 virtual void EnterContext(Context* context) = 0;
304
305 virtual void VisitFunction(JSFunction* function) = 0;
306
307 // Function which is called after iteration of all optimized functions
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000308 // from given native context.
Ben Murdochb0fe1622011-05-05 13:52:32 +0100309 virtual void LeaveContext(Context* context) = 0;
310};
311
312
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000313#define DEOPT_MESSAGES_LIST(V) \
314 V(kAccessCheck, "Access check needed") \
315 V(kNoReason, "no reason") \
316 V(kConstantGlobalVariableAssignment, "Constant global variable assignment") \
317 V(kConversionOverflow, "conversion overflow") \
318 V(kDivisionByZero, "division by zero") \
319 V(kElementsKindUnhandledInKeyedLoadGenericStub, \
320 "ElementsKind unhandled in KeyedLoadGenericStub") \
321 V(kExpectedHeapNumber, "Expected heap number") \
322 V(kExpectedSmi, "Expected smi") \
323 V(kForcedDeoptToRuntime, "Forced deopt to runtime") \
324 V(kHole, "hole") \
325 V(kHoleyArrayDespitePackedElements_kindFeedback, \
326 "Holey array despite packed elements_kind feedback") \
327 V(kInstanceMigrationFailed, "instance migration failed") \
328 V(kInsufficientTypeFeedbackForCallWithArguments, \
329 "Insufficient type feedback for call with arguments") \
330 V(kInsufficientTypeFeedbackForCombinedTypeOfBinaryOperation, \
331 "Insufficient type feedback for combined type of binary operation") \
332 V(kInsufficientTypeFeedbackForGenericNamedAccess, \
333 "Insufficient type feedback for generic named access") \
334 V(kInsufficientTypeFeedbackForKeyedLoad, \
335 "Insufficient type feedback for keyed load") \
336 V(kInsufficientTypeFeedbackForKeyedStore, \
337 "Insufficient type feedback for keyed store") \
338 V(kInsufficientTypeFeedbackForLHSOfBinaryOperation, \
339 "Insufficient type feedback for LHS of binary operation") \
340 V(kInsufficientTypeFeedbackForRHSOfBinaryOperation, \
341 "Insufficient type feedback for RHS of binary operation") \
342 V(kKeyIsNegative, "key is negative") \
343 V(kLiteralsWereDisposed, "literals have been disposed") \
344 V(kLostPrecision, "lost precision") \
345 V(kLostPrecisionOrNaN, "lost precision or NaN") \
346 V(kMementoFound, "memento found") \
347 V(kMinusZero, "minus zero") \
348 V(kNaN, "NaN") \
349 V(kNegativeKeyEncountered, "Negative key encountered") \
350 V(kNegativeValue, "negative value") \
351 V(kNoCache, "no cache") \
352 V(kNonStrictElementsInKeyedLoadGenericStub, \
353 "non-strict elements in KeyedLoadGenericStub") \
354 V(kNotADateObject, "not a date object") \
355 V(kNotAHeapNumber, "not a heap number") \
356 V(kNotAHeapNumberUndefinedBoolean, "not a heap number/undefined/true/false") \
357 V(kNotAHeapNumberUndefined, "not a heap number/undefined") \
358 V(kNotAJavaScriptObject, "not a JavaScript object") \
359 V(kNotASmi, "not a Smi") \
360 V(kNull, "null") \
361 V(kOutOfBounds, "out of bounds") \
362 V(kOutsideOfRange, "Outside of range") \
363 V(kOverflow, "overflow") \
364 V(kProxy, "proxy") \
365 V(kReceiverWasAGlobalObject, "receiver was a global object") \
366 V(kSmi, "Smi") \
367 V(kTooManyArguments, "too many arguments") \
368 V(kTooManyUndetectableTypes, "Too many undetectable types") \
369 V(kTracingElementsTransitions, "Tracing elements transitions") \
370 V(kTypeMismatchBetweenFeedbackAndConstant, \
371 "Type mismatch between feedback and constant") \
372 V(kUndefined, "undefined") \
373 V(kUnexpectedCellContentsInConstantGlobalStore, \
374 "Unexpected cell contents in constant global store") \
375 V(kUnexpectedCellContentsInGlobalStore, \
376 "Unexpected cell contents in global store") \
377 V(kUnexpectedObject, "unexpected object") \
378 V(kUnexpectedRHSOfBinaryOperation, "Unexpected RHS of binary operation") \
379 V(kUninitializedBoilerplateInFastClone, \
380 "Uninitialized boilerplate in fast clone") \
381 V(kUninitializedBoilerplateLiterals, "Uninitialized boilerplate literals") \
382 V(kUnknownMapInPolymorphicAccess, "Unknown map in polymorphic access") \
383 V(kUnknownMapInPolymorphicCall, "Unknown map in polymorphic call") \
384 V(kUnknownMapInPolymorphicElementAccess, \
385 "Unknown map in polymorphic element access") \
386 V(kUnknownMap, "Unknown map") \
387 V(kValueMismatch, "value mismatch") \
388 V(kWrongInstanceType, "wrong instance type") \
389 V(kWrongMap, "wrong map") \
390 V(kUndefinedOrNullInForIn, "null or undefined in for-in") \
391 V(kUndefinedOrNullInToObject, "null or undefined in ToObject")
392
393
Ben Murdochb0fe1622011-05-05 13:52:32 +0100394class Deoptimizer : public Malloced {
395 public:
396 enum BailoutType {
397 EAGER,
398 LAZY,
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000399 SOFT,
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000400 // This last bailout type is not really a bailout, but used by the
401 // debugger to deoptimize stack frames to allow inspection.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000402 DEBUGGER,
403 kBailoutTypesWithCodeEntry = SOFT + 1
Ben Murdochb0fe1622011-05-05 13:52:32 +0100404 };
405
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000406#define DEOPT_MESSAGES_CONSTANTS(C, T) C,
407 enum DeoptReason {
408 DEOPT_MESSAGES_LIST(DEOPT_MESSAGES_CONSTANTS) kLastDeoptReason
409 };
410#undef DEOPT_MESSAGES_CONSTANTS
411 static const char* GetDeoptReason(DeoptReason deopt_reason);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000412
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000413 struct DeoptInfo {
414 DeoptInfo(SourcePosition position, const char* m, DeoptReason d)
415 : position(position), mnemonic(m), deopt_reason(d), inlining_id(0) {}
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000416
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000417 SourcePosition position;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000418 const char* mnemonic;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000419 DeoptReason deopt_reason;
420 int inlining_id;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000421 };
422
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000423 static DeoptInfo GetDeoptInfo(Code* code, byte* from);
424
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000425 struct JumpTableEntry : public ZoneObject {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000426 inline JumpTableEntry(Address entry, const DeoptInfo& deopt_info,
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000427 Deoptimizer::BailoutType type, bool frame)
428 : label(),
429 address(entry),
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000430 deopt_info(deopt_info),
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000431 bailout_type(type),
432 needs_frame(frame) {}
433
434 bool IsEquivalentTo(const JumpTableEntry& other) const {
435 return address == other.address && bailout_type == other.bailout_type &&
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000436 needs_frame == other.needs_frame;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000437 }
438
439 Label label;
440 Address address;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000441 DeoptInfo deopt_info;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000442 Deoptimizer::BailoutType bailout_type;
443 bool needs_frame;
444 };
445
446 static bool TraceEnabledFor(BailoutType deopt_type,
447 StackFrame::Type frame_type);
448 static const char* MessageFor(BailoutType type);
449
Ben Murdochb0fe1622011-05-05 13:52:32 +0100450 int output_count() const { return output_count_; }
451
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000452 Handle<JSFunction> function() const { return Handle<JSFunction>(function_); }
453 Handle<Code> compiled_code() const { return Handle<Code>(compiled_code_); }
454 BailoutType bailout_type() const { return bailout_type_; }
455
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100456 // Number of created JS frames. Not all created frames are necessarily JS.
457 int jsframe_count() const { return jsframe_count_; }
458
Ben Murdochb0fe1622011-05-05 13:52:32 +0100459 static Deoptimizer* New(JSFunction* function,
460 BailoutType type,
461 unsigned bailout_id,
462 Address from,
Steve Block44f0eee2011-05-26 01:26:41 +0100463 int fp_to_sp_delta,
464 Isolate* isolate);
465 static Deoptimizer* Grab(Isolate* isolate);
466
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000467 // The returned object with information on the optimized frame needs to be
468 // freed before another one can be generated.
469 static DeoptimizedFrameInfo* DebuggerInspectableFrame(JavaScriptFrame* frame,
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100470 int jsframe_index,
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000471 Isolate* isolate);
472 static void DeleteDebuggerInspectableFrame(DeoptimizedFrameInfo* info,
473 Isolate* isolate);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000474
Steve Block44f0eee2011-05-26 01:26:41 +0100475 // Makes sure that there is enough room in the relocation
476 // information of a code object to perform lazy deoptimization
477 // patching. If there is not enough room a new relocation
478 // information object is allocated and comments are added until it
479 // is big enough.
480 static void EnsureRelocSpaceForLazyDeoptimization(Handle<Code> code);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100481
482 // Deoptimize the function now. Its current optimized code will never be run
483 // again and any activations of the optimized code will get deoptimized when
484 // execution returns.
485 static void DeoptimizeFunction(JSFunction* function);
486
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000487 // Deoptimize all code in the given isolate.
488 static void DeoptimizeAll(Isolate* isolate);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100489
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000490 // Deoptimizes all optimized code that has been previously marked
491 // (via code->set_marked_for_deoptimization) and unlinks all functions that
492 // refer to that code.
493 static void DeoptimizeMarkedCode(Isolate* isolate);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100494
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000495 // Visit all the known optimized functions in a given isolate.
496 static void VisitAllOptimizedFunctions(
497 Isolate* isolate, OptimizedFunctionVisitor* visitor);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100498
Steve Block1e0659c2011-05-24 12:43:12 +0100499 // The size in bytes of the code required at a lazy deopt patch site.
500 static int patch_size();
Ben Murdochb0fe1622011-05-05 13:52:32 +0100501
Ben Murdochb0fe1622011-05-05 13:52:32 +0100502 ~Deoptimizer();
503
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000504 void MaterializeHeapObjects(JavaScriptFrameIterator* it);
505
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000506 void MaterializeHeapNumbersForDebuggerInspectableFrame(
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000507 int frame_index, int parameter_count, int expression_count,
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100508 DeoptimizedFrameInfo* info);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100509
Ben Murdoch8b112d22011-06-08 16:22:53 +0100510 static void ComputeOutputFrames(Deoptimizer* deoptimizer);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100511
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000512
513 enum GetEntryMode {
514 CALCULATE_ENTRY_ADDRESS,
515 ENSURE_ENTRY_CODE
516 };
517
518
519 static Address GetDeoptimizationEntry(
520 Isolate* isolate,
521 int id,
522 BailoutType type,
523 GetEntryMode mode = ENSURE_ENTRY_CODE);
524 static int GetDeoptimizationId(Isolate* isolate,
525 Address addr,
526 BailoutType type);
Steve Block9fac8402011-05-12 15:51:54 +0100527 static int GetOutputInfo(DeoptimizationOutputData* data,
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000528 BailoutId node_id,
Steve Block9fac8402011-05-12 15:51:54 +0100529 SharedFunctionInfo* shared);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100530
Ben Murdochb0fe1622011-05-05 13:52:32 +0100531 // Code generation support.
532 static int input_offset() { return OFFSET_OF(Deoptimizer, input_); }
533 static int output_count_offset() {
534 return OFFSET_OF(Deoptimizer, output_count_);
535 }
536 static int output_offset() { return OFFSET_OF(Deoptimizer, output_); }
537
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000538 static int has_alignment_padding_offset() {
539 return OFFSET_OF(Deoptimizer, has_alignment_padding_);
540 }
541
Steve Block44f0eee2011-05-26 01:26:41 +0100542 static int GetDeoptimizedCodeCount(Isolate* isolate);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100543
544 static const int kNotDeoptimizationEntry = -1;
545
546 // Generators for the deoptimization entry code.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000547 class TableEntryGenerator BASE_EMBEDDED {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100548 public:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000549 TableEntryGenerator(MacroAssembler* masm, BailoutType type, int count)
550 : masm_(masm), type_(type), count_(count) {}
Ben Murdochb0fe1622011-05-05 13:52:32 +0100551
552 void Generate();
553
554 protected:
555 MacroAssembler* masm() const { return masm_; }
556 BailoutType type() const { return type_; }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000557 Isolate* isolate() const { return masm_->isolate(); }
Ben Murdochb0fe1622011-05-05 13:52:32 +0100558
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000559 void GeneratePrologue();
Ben Murdochb0fe1622011-05-05 13:52:32 +0100560
561 private:
562 int count() const { return count_; }
563
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000564 MacroAssembler* masm_;
565 Deoptimizer::BailoutType type_;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100566 int count_;
567 };
568
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100569 int ConvertJSFrameIndexToFrameIndex(int jsframe_index);
570
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000571 static size_t GetMaxDeoptTableSize();
572
573 static void EnsureCodeForDeoptimizationEntry(Isolate* isolate,
574 BailoutType type,
575 int max_entry_id);
576
577 Isolate* isolate() const { return isolate_; }
578
Ben Murdochb0fe1622011-05-05 13:52:32 +0100579 private:
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000580 static const int kMinNumberOfEntries = 64;
581 static const int kMaxNumberOfEntries = 16384;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100582
Steve Block44f0eee2011-05-26 01:26:41 +0100583 Deoptimizer(Isolate* isolate,
584 JSFunction* function,
Ben Murdochb0fe1622011-05-05 13:52:32 +0100585 BailoutType type,
586 unsigned bailout_id,
587 Address from,
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000588 int fp_to_sp_delta,
589 Code* optimized_code);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000590 Code* FindOptimizedCode(JSFunction* function, Code* optimized_code);
591 void PrintFunctionName();
Ben Murdochb0fe1622011-05-05 13:52:32 +0100592 void DeleteFrameDescriptions();
593
594 void DoComputeOutputFrames();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000595 void DoComputeJSFrame(int frame_index);
596 void DoComputeInterpretedFrame(int frame_index);
597 void DoComputeArgumentsAdaptorFrame(int frame_index);
598 void DoComputeConstructStubFrame(int frame_index);
599 void DoComputeAccessorStubFrame(int frame_index, bool is_setter_stub_frame);
600 void DoComputeCompiledStubFrame(int frame_index);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000601
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000602 void WriteTranslatedValueToOutput(
603 TranslatedFrame::iterator* iterator, int* input_index, int frame_index,
604 unsigned output_offset, const char* debug_hint_string = nullptr,
605 Address output_address_for_materialization = nullptr);
606 void WriteValueToOutput(Object* value, int input_index, int frame_index,
607 unsigned output_offset,
608 const char* debug_hint_string);
609 void DebugPrintOutputSlot(intptr_t value, int frame_index,
610 unsigned output_offset,
611 const char* debug_hint_string);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100612
613 unsigned ComputeInputFrameSize() const;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000614 unsigned ComputeJavascriptFixedSize(JSFunction* function) const;
615 unsigned ComputeInterpretedFixedSize(JSFunction* function) const;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100616
617 unsigned ComputeIncomingArgumentSize(JSFunction* function) const;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000618 static unsigned ComputeOutgoingArgumentSize(Code* code, unsigned bailout_id);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100619
620 Object* ComputeLiteral(int index) const;
621
Ben Murdochb0fe1622011-05-05 13:52:32 +0100622 static void GenerateDeoptimizationEntries(
623 MacroAssembler* masm, int count, BailoutType type);
624
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000625 // Marks all the code in the given context for deoptimization.
626 static void MarkAllCodeForContext(Context* native_context);
627
628 // Visit all the known optimized functions in a given context.
629 static void VisitAllOptimizedFunctionsForContext(
630 Context* context, OptimizedFunctionVisitor* visitor);
631
632 // Deoptimizes all code marked in the given context.
633 static void DeoptimizeMarkedCodeForContext(Context* native_context);
634
635 // Patch the given code so that it will deoptimize itself.
636 static void PatchCodeForDeoptimization(Isolate* isolate, Code* code);
637
638 // Searches the list of known deoptimizing code for a Code object
639 // containing the given address (which is supposedly faster than
640 // searching all code objects).
641 Code* FindDeoptimizingCode(Address addr);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100642
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000643 // Fill the input from from a JavaScript frame. This is used when
644 // the debugger needs to inspect an optimized frame. For normal
645 // deoptimizations the input frame is filled in generated code.
646 void FillInputFrame(Address tos, JavaScriptFrame* frame);
647
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000648 // Fill the given output frame's registers to contain the failure handler
649 // address and the number of parameters for a stub failure trampoline.
650 void SetPlatformCompiledStubRegisters(FrameDescription* output_frame,
651 CodeStubDescriptor* desc);
652
653 // Fill the given output frame's double registers with the original values
654 // from the input frame's double registers.
655 void CopyDoubleRegisters(FrameDescription* output_frame);
656
657 // Determines whether the input frame contains alignment padding by looking
658 // at the dynamic alignment state slot inside the frame.
659 bool HasAlignmentPadding(JSFunction* function);
660
Steve Block44f0eee2011-05-26 01:26:41 +0100661 Isolate* isolate_;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100662 JSFunction* function_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000663 Code* compiled_code_;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100664 unsigned bailout_id_;
665 BailoutType bailout_type_;
666 Address from_;
667 int fp_to_sp_delta_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000668 int has_alignment_padding_;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100669
670 // Input frame description.
671 FrameDescription* input_;
672 // Number of output frames.
673 int output_count_;
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100674 // Number of output js frames.
675 int jsframe_count_;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100676 // Array of output frame descriptions.
677 FrameDescription** output_;
678
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000679 // Key for lookup of previously materialized objects
680 Address stack_fp_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000681
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000682 TranslatedState translated_state_;
683 struct ValueToMaterialize {
684 Address output_slot_address_;
685 TranslatedFrame::iterator value_;
686 };
687 std::vector<ValueToMaterialize> values_to_materialize_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000688
689#ifdef DEBUG
690 DisallowHeapAllocation* disallow_heap_allocation_;
691#endif // DEBUG
692
693 CodeTracer::Scope* trace_scope_;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100694
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000695 static const int table_entry_size_;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100696
697 friend class FrameDescription;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000698 friend class DeoptimizedFrameInfo;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100699};
700
701
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000702class RegisterValues {
703 public:
704 intptr_t GetRegister(unsigned n) const {
705#if DEBUG
706 // This convoluted DCHECK is needed to work around a gcc problem that
707 // improperly detects an array bounds overflow in optimized debug builds
708 // when using a plain DCHECK.
709 if (n >= arraysize(registers_)) {
710 DCHECK(false);
711 return 0;
712 }
713#endif
714 return registers_[n];
715 }
716
717 double GetDoubleRegister(unsigned n) const {
718 DCHECK(n < arraysize(double_registers_));
719 return double_registers_[n];
720 }
721
722 void SetRegister(unsigned n, intptr_t value) {
723 DCHECK(n < arraysize(registers_));
724 registers_[n] = value;
725 }
726
727 void SetDoubleRegister(unsigned n, double value) {
728 DCHECK(n < arraysize(double_registers_));
729 double_registers_[n] = value;
730 }
731
732 intptr_t registers_[Register::kNumRegisters];
733 double double_registers_[DoubleRegister::kMaxNumRegisters];
734};
735
736
Ben Murdochb0fe1622011-05-05 13:52:32 +0100737class FrameDescription {
738 public:
739 FrameDescription(uint32_t frame_size,
740 JSFunction* function);
741
742 void* operator new(size_t size, uint32_t frame_size) {
Steve Block44f0eee2011-05-26 01:26:41 +0100743 // Subtracts kPointerSize, as the member frame_content_ already supplies
744 // the first element of the area to store the frame.
745 return malloc(size + frame_size - kPointerSize);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100746 }
747
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000748 void operator delete(void* pointer, uint32_t frame_size) {
749 free(pointer);
750 }
751
Ben Murdochb0fe1622011-05-05 13:52:32 +0100752 void operator delete(void* description) {
753 free(description);
754 }
755
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000756 uint32_t GetFrameSize() const {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000757 DCHECK(static_cast<uint32_t>(frame_size_) == frame_size_);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000758 return static_cast<uint32_t>(frame_size_);
759 }
Ben Murdochb0fe1622011-05-05 13:52:32 +0100760
761 JSFunction* GetFunction() const { return function_; }
762
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100763 unsigned GetOffsetFromSlotIndex(int slot_index);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100764
765 intptr_t GetFrameSlot(unsigned offset) {
766 return *GetFrameSlotPointer(offset);
767 }
768
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000769 Address GetFramePointerAddress() {
770 int fp_offset = GetFrameSize() -
771 (ComputeParametersCount() + 1) * kPointerSize -
772 StandardFrameConstants::kCallerSPOffset;
773 return reinterpret_cast<Address>(GetFrameSlotPointer(fp_offset));
Ben Murdochb0fe1622011-05-05 13:52:32 +0100774 }
775
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000776 RegisterValues* GetRegisterValues() { return &register_values_; }
777
Ben Murdochb0fe1622011-05-05 13:52:32 +0100778 void SetFrameSlot(unsigned offset, intptr_t value) {
779 *GetFrameSlotPointer(offset) = value;
780 }
781
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000782 void SetCallerPc(unsigned offset, intptr_t value);
783
784 void SetCallerFp(unsigned offset, intptr_t value);
785
786 void SetCallerConstantPool(unsigned offset, intptr_t value);
787
Ben Murdochb0fe1622011-05-05 13:52:32 +0100788 intptr_t GetRegister(unsigned n) const {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000789 return register_values_.GetRegister(n);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100790 }
791
792 double GetDoubleRegister(unsigned n) const {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000793 return register_values_.GetDoubleRegister(n);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100794 }
795
796 void SetRegister(unsigned n, intptr_t value) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000797 register_values_.SetRegister(n, value);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100798 }
799
800 void SetDoubleRegister(unsigned n, double value) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000801 register_values_.SetDoubleRegister(n, value);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100802 }
803
804 intptr_t GetTop() const { return top_; }
805 void SetTop(intptr_t top) { top_ = top; }
806
807 intptr_t GetPc() const { return pc_; }
808 void SetPc(intptr_t pc) { pc_ = pc; }
809
810 intptr_t GetFp() const { return fp_; }
811 void SetFp(intptr_t fp) { fp_ = fp; }
812
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100813 intptr_t GetContext() const { return context_; }
814 void SetContext(intptr_t context) { context_ = context; }
815
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000816 intptr_t GetConstantPool() const { return constant_pool_; }
817 void SetConstantPool(intptr_t constant_pool) {
818 constant_pool_ = constant_pool;
819 }
820
Ben Murdochb0fe1622011-05-05 13:52:32 +0100821 Smi* GetState() const { return state_; }
822 void SetState(Smi* state) { state_ = state; }
823
824 void SetContinuation(intptr_t pc) { continuation_ = pc; }
825
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100826 StackFrame::Type GetFrameType() const { return type_; }
827 void SetFrameType(StackFrame::Type type) { type_ = type; }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000828
829 // Get the incoming arguments count.
830 int ComputeParametersCount();
831
832 // Get a parameter value for an unoptimized frame.
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100833 Object* GetParameter(int index);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000834
835 // Get the expression stack height for a unoptimized frame.
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100836 unsigned GetExpressionCount();
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000837
838 // Get the expression stack value for an unoptimized frame.
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100839 Object* GetExpression(int index);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000840
Ben Murdochb0fe1622011-05-05 13:52:32 +0100841 static int registers_offset() {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000842 return OFFSET_OF(FrameDescription, register_values_.registers_);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100843 }
844
845 static int double_registers_offset() {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000846 return OFFSET_OF(FrameDescription, register_values_.double_registers_);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100847 }
848
849 static int frame_size_offset() {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000850 return offsetof(FrameDescription, frame_size_);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100851 }
852
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000853 static int pc_offset() { return offsetof(FrameDescription, pc_); }
Ben Murdochb0fe1622011-05-05 13:52:32 +0100854
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000855 static int state_offset() { return offsetof(FrameDescription, state_); }
Ben Murdochb0fe1622011-05-05 13:52:32 +0100856
857 static int continuation_offset() {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000858 return offsetof(FrameDescription, continuation_);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100859 }
860
861 static int frame_content_offset() {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000862 return offsetof(FrameDescription, frame_content_);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100863 }
864
865 private:
866 static const uint32_t kZapUint32 = 0xbeeddead;
867
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000868 // Frame_size_ must hold a uint32_t value. It is only a uintptr_t to
869 // keep the variable-size array frame_content_ of type intptr_t at
870 // the end of the structure aligned.
Ben Murdochb0fe1622011-05-05 13:52:32 +0100871 uintptr_t frame_size_; // Number of bytes.
872 JSFunction* function_;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000873 RegisterValues register_values_;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100874 intptr_t top_;
875 intptr_t pc_;
876 intptr_t fp_;
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100877 intptr_t context_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000878 intptr_t constant_pool_;
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100879 StackFrame::Type type_;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100880 Smi* state_;
881
882 // Continuation is the PC where the execution continues after
883 // deoptimizing.
884 intptr_t continuation_;
885
Steve Block44f0eee2011-05-26 01:26:41 +0100886 // This must be at the end of the object as the object is allocated larger
887 // than it's definition indicate to extend this array.
888 intptr_t frame_content_[1];
889
Ben Murdochb0fe1622011-05-05 13:52:32 +0100890 intptr_t* GetFrameSlotPointer(unsigned offset) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000891 DCHECK(offset < frame_size_);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100892 return reinterpret_cast<intptr_t*>(
893 reinterpret_cast<Address>(this) + frame_content_offset() + offset);
894 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100895
896 int ComputeFixedSize();
Ben Murdochb0fe1622011-05-05 13:52:32 +0100897};
898
899
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000900class DeoptimizerData {
901 public:
902 explicit DeoptimizerData(MemoryAllocator* allocator);
903 ~DeoptimizerData();
904
905 void Iterate(ObjectVisitor* v);
906
907 private:
908 MemoryAllocator* allocator_;
909 int deopt_entry_code_entries_[Deoptimizer::kBailoutTypesWithCodeEntry];
910 MemoryChunk* deopt_entry_code_[Deoptimizer::kBailoutTypesWithCodeEntry];
911
912 DeoptimizedFrameInfo* deoptimized_frame_info_;
913
914 Deoptimizer* current_;
915
916 friend class Deoptimizer;
917
918 DISALLOW_COPY_AND_ASSIGN(DeoptimizerData);
919};
920
921
Ben Murdochb0fe1622011-05-05 13:52:32 +0100922class TranslationBuffer BASE_EMBEDDED {
923 public:
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000924 explicit TranslationBuffer(Zone* zone) : contents_(256, zone) { }
Ben Murdochb0fe1622011-05-05 13:52:32 +0100925
926 int CurrentIndex() const { return contents_.length(); }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000927 void Add(int32_t value, Zone* zone);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100928
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000929 Handle<ByteArray> CreateByteArray(Factory* factory);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100930
931 private:
932 ZoneList<uint8_t> contents_;
933};
934
935
936class TranslationIterator BASE_EMBEDDED {
937 public:
938 TranslationIterator(ByteArray* buffer, int index)
939 : buffer_(buffer), index_(index) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000940 DCHECK(index >= 0 && index < buffer->length());
Ben Murdochb0fe1622011-05-05 13:52:32 +0100941 }
942
943 int32_t Next();
944
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000945 bool HasNext() const { return index_ < buffer_->length(); }
Ben Murdochb0fe1622011-05-05 13:52:32 +0100946
947 void Skip(int n) {
948 for (int i = 0; i < n; i++) Next();
949 }
950
951 private:
952 ByteArray* buffer_;
953 int index_;
954};
955
956
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000957#define TRANSLATION_OPCODE_LIST(V) \
958 V(BEGIN) \
959 V(JS_FRAME) \
960 V(INTERPRETED_FRAME) \
961 V(CONSTRUCT_STUB_FRAME) \
962 V(GETTER_STUB_FRAME) \
963 V(SETTER_STUB_FRAME) \
964 V(ARGUMENTS_ADAPTOR_FRAME) \
965 V(COMPILED_STUB_FRAME) \
966 V(DUPLICATED_OBJECT) \
967 V(ARGUMENTS_OBJECT) \
968 V(CAPTURED_OBJECT) \
969 V(REGISTER) \
970 V(INT32_REGISTER) \
971 V(UINT32_REGISTER) \
972 V(BOOL_REGISTER) \
973 V(DOUBLE_REGISTER) \
974 V(STACK_SLOT) \
975 V(INT32_STACK_SLOT) \
976 V(UINT32_STACK_SLOT) \
977 V(BOOL_STACK_SLOT) \
978 V(DOUBLE_STACK_SLOT) \
979 V(LITERAL) \
980 V(JS_FRAME_FUNCTION)
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000981
982
Ben Murdochb0fe1622011-05-05 13:52:32 +0100983class Translation BASE_EMBEDDED {
984 public:
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000985#define DECLARE_TRANSLATION_OPCODE_ENUM(item) item,
Ben Murdochb0fe1622011-05-05 13:52:32 +0100986 enum Opcode {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000987 TRANSLATION_OPCODE_LIST(DECLARE_TRANSLATION_OPCODE_ENUM)
988 LAST = LITERAL
Ben Murdochb0fe1622011-05-05 13:52:32 +0100989 };
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000990#undef DECLARE_TRANSLATION_OPCODE_ENUM
Ben Murdochb0fe1622011-05-05 13:52:32 +0100991
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000992 Translation(TranslationBuffer* buffer, int frame_count, int jsframe_count,
993 Zone* zone)
Ben Murdochb0fe1622011-05-05 13:52:32 +0100994 : buffer_(buffer),
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000995 index_(buffer->CurrentIndex()),
996 zone_(zone) {
997 buffer_->Add(BEGIN, zone);
998 buffer_->Add(frame_count, zone);
999 buffer_->Add(jsframe_count, zone);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001000 }
1001
1002 int index() const { return index_; }
1003
1004 // Commands.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001005 void BeginJSFrame(BailoutId node_id, int literal_id, unsigned height);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001006 void BeginInterpretedFrame(BailoutId bytecode_offset, int literal_id,
1007 unsigned height);
1008 void BeginCompiledStubFrame(int height);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001009 void BeginArgumentsAdaptorFrame(int literal_id, unsigned height);
1010 void BeginConstructStubFrame(int literal_id, unsigned height);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001011 void BeginGetterStubFrame(int literal_id);
1012 void BeginSetterStubFrame(int literal_id);
1013 void BeginArgumentsObject(int args_length);
1014 void BeginCapturedObject(int length);
1015 void DuplicateObject(int object_index);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001016 void StoreRegister(Register reg);
1017 void StoreInt32Register(Register reg);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001018 void StoreUint32Register(Register reg);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001019 void StoreBoolRegister(Register reg);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001020 void StoreDoubleRegister(DoubleRegister reg);
1021 void StoreStackSlot(int index);
1022 void StoreInt32StackSlot(int index);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001023 void StoreUint32StackSlot(int index);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001024 void StoreBoolStackSlot(int index);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001025 void StoreDoubleStackSlot(int index);
1026 void StoreLiteral(int literal_id);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001027 void StoreArgumentsObject(bool args_known, int args_index, int args_length);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001028 void StoreJSFrameFunction();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001029
1030 Zone* zone() const { return zone_; }
Ben Murdochb0fe1622011-05-05 13:52:32 +01001031
1032 static int NumberOfOperandsFor(Opcode opcode);
1033
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001034#if defined(OBJECT_PRINT) || defined(ENABLE_DISASSEMBLER)
Ben Murdochb0fe1622011-05-05 13:52:32 +01001035 static const char* StringFor(Opcode opcode);
1036#endif
1037
1038 private:
1039 TranslationBuffer* buffer_;
1040 int index_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001041 Zone* zone_;
Ben Murdochb0fe1622011-05-05 13:52:32 +01001042};
1043
1044
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001045class MaterializedObjectStore {
1046 public:
1047 explicit MaterializedObjectStore(Isolate* isolate) : isolate_(isolate) {
1048 }
1049
1050 Handle<FixedArray> Get(Address fp);
1051 void Set(Address fp, Handle<FixedArray> materialized_objects);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001052 bool Remove(Address fp);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001053
1054 private:
1055 Isolate* isolate() { return isolate_; }
1056 Handle<FixedArray> GetStackEntries();
1057 Handle<FixedArray> EnsureStackEntries(int size);
1058
1059 int StackIdToIndex(Address fp);
1060
1061 Isolate* isolate_;
1062 List<Address> frame_fps_;
Ben Murdoch8b112d22011-06-08 16:22:53 +01001063};
1064
1065
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001066// Class used to represent an unoptimized frame when the debugger
1067// needs to inspect a frame that is part of an optimized frame. The
1068// internally used FrameDescription objects are not GC safe so for use
1069// by the debugger frame information is copied to an object of this type.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001070// Represents parameters in unadapted form so their number might mismatch
1071// formal parameter count.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001072class DeoptimizedFrameInfo : public Malloced {
1073 public:
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001074 DeoptimizedFrameInfo(Deoptimizer* deoptimizer,
1075 int frame_index,
1076 bool has_arguments_adaptor,
1077 bool has_construct_stub);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001078 virtual ~DeoptimizedFrameInfo();
1079
1080 // GC support.
1081 void Iterate(ObjectVisitor* v);
1082
1083 // Return the number of incoming arguments.
1084 int parameters_count() { return parameters_count_; }
1085
1086 // Return the height of the expression stack.
1087 int expression_count() { return expression_count_; }
1088
1089 // Get the frame function.
1090 JSFunction* GetFunction() {
1091 return function_;
1092 }
1093
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001094 // Get the frame context.
1095 Object* GetContext() { return context_; }
1096
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001097 // Check if this frame is preceded by construct stub frame. The bottom-most
1098 // inlined frame might still be called by an uninlined construct stub.
1099 bool HasConstructStub() {
1100 return has_construct_stub_;
1101 }
1102
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001103 // Get an incoming argument.
1104 Object* GetParameter(int index) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001105 DCHECK(0 <= index && index < parameters_count());
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001106 return parameters_[index];
1107 }
1108
1109 // Get an expression from the expression stack.
1110 Object* GetExpression(int index) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001111 DCHECK(0 <= index && index < expression_count());
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001112 return expression_stack_[index];
1113 }
1114
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001115 int GetSourcePosition() {
1116 return source_position_;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001117 }
1118
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001119 private:
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001120 // Set an incoming argument.
1121 void SetParameter(int index, Object* obj) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001122 DCHECK(0 <= index && index < parameters_count());
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001123 parameters_[index] = obj;
1124 }
1125
1126 // Set an expression on the expression stack.
1127 void SetExpression(int index, Object* obj) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001128 DCHECK(0 <= index && index < expression_count());
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001129 expression_stack_[index] = obj;
1130 }
1131
1132 JSFunction* function_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001133 Object* context_;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001134 bool has_construct_stub_;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001135 int parameters_count_;
1136 int expression_count_;
1137 Object** parameters_;
1138 Object** expression_stack_;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001139 int source_position_;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001140
1141 friend class Deoptimizer;
1142};
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001143
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001144} // namespace internal
1145} // namespace v8
Ben Murdochb0fe1622011-05-05 13:52:32 +01001146
1147#endif // V8_DEOPTIMIZER_H_