deoptimizer.h 36.2 KB
Newer Older
1
// Copyright 2012 the V8 project authors. All rights reserved.
2 3
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
4 5 6 7

#ifndef V8_DEOPTIMIZER_H_
#define V8_DEOPTIMIZER_H_

8
#include <stack>
9 10
#include <vector>

11
#include "src/allocation.h"
12
#include "src/base/macros.h"
13
#include "src/boxed-float.h"
14
#include "src/code-tracer.h"
15
#include "src/deoptimize-reason.h"
16
#include "src/feedback-vector.h"
17
#include "src/frame-constants.h"
18
#include "src/frames.h"
19
#include "src/globals.h"
20
#include "src/isolate.h"
21
#include "src/label.h"
22
#include "src/objects/shared-function-info.h"
23
#include "src/register-arch.h"
24
#include "src/source-position.h"
25
#include "src/zone/zone-chunk-list.h"
26 27 28 29 30 31

namespace v8 {
namespace internal {

class FrameDescription;
class TranslationIterator;
32
class DeoptimizedFrameInfo;
33 34
class TranslatedState;
class RegisterValues;
35
class MacroAssembler;
36

jarin's avatar
jarin committed
37
class TranslatedValue {
38 39
 public:
  // Allocation-less getter of the value.
40 41
  // Returns ReadOnlyRoots::arguments_marker() if allocation would be necessary
  // to get the value.
42
  Object GetRawValue() const;
43 44 45

  // Getter for the value, takes care of materializing the subgraph
  // reachable from this value.
46 47 48
  Handle<Object> GetValue();

  bool IsMaterializedObject() const;
49
  bool IsMaterializableByDebugger() const;
50 51 52 53 54

 private:
  friend class TranslatedState;
  friend class TranslatedFrame;

55
  enum Kind : uint8_t {
56 57 58
    kInvalid,
    kTagged,
    kInt32,
59
    kInt64,
60 61
    kUInt32,
    kBoolBit,
62
    kFloat,
63
    kDouble,
64 65 66 67 68 69
    kCapturedObject,   // Object captured by the escape analysis.
                       // The number of nested objects can be obtained
                       // with the DeferredObjectLength() method
                       // (the values of the nested objects follow
                       // this value in the depth-first order.)
    kDuplicatedObject  // Duplicated object of a deferred object.
70 71
  };

72 73 74 75 76 77 78 79
  enum MaterializationState : uint8_t {
    kUninitialized,
    kAllocated,  // Storage for the object has been allocated (or
                 // enqueued for allocation).
    kFinished,   // The object has been initialized (or enqueued for
                 // initialization).
  };

80 81 82
  TranslatedValue(TranslatedState* container, Kind kind)
      : kind_(kind), container_(container) {}
  Kind kind() const { return kind_; }
83 84 85
  MaterializationState materialization_state() const {
    return materialization_state_;
  }
86 87 88 89 90 91
  void Handlify();
  int GetChildrenCount() const;

  static TranslatedValue NewDeferredObject(TranslatedState* container,
                                           int length, int object_index);
  static TranslatedValue NewDuplicateObject(TranslatedState* container, int id);
92 93
  static TranslatedValue NewFloat(TranslatedState* container, Float32 value);
  static TranslatedValue NewDouble(TranslatedState* container, Float64 value);
94
  static TranslatedValue NewInt32(TranslatedState* container, int32_t value);
95
  static TranslatedValue NewInt64(TranslatedState* container, int64_t value);
96 97
  static TranslatedValue NewUInt32(TranslatedState* container, uint32_t value);
  static TranslatedValue NewBool(TranslatedState* container, uint32_t value);
98
  static TranslatedValue NewTagged(TranslatedState* container, Object literal);
99
  static TranslatedValue NewInvalid(TranslatedState* container);
100 101 102 103

  Isolate* isolate() const;
  void MaterializeSimple();

104 105 106 107 108 109 110 111 112 113
  void set_storage(Handle<HeapObject> storage) { storage_ = storage; }
  void set_initialized_storage(Handle<Object> storage);
  void mark_finished() { materialization_state_ = kFinished; }
  void mark_allocated() { materialization_state_ = kAllocated; }

  Handle<Object> GetStorage() {
    DCHECK_NE(kUninitialized, materialization_state());
    return storage_;
  }

114
  Kind kind_;
115
  MaterializationState materialization_state_ = kUninitialized;
116 117 118 119
  TranslatedState* container_;  // This is only needed for materialization of
                                // objects and constructing handles (to get
                                // to the isolate).

120 121 122
  Handle<Object> storage_;  // Contains the materialized value or the
                            // byte-array that will be later morphed into
                            // the materialized object.
123 124 125

  struct MaterializedObjectInfo {
    int id_;
126
    int length_;  // Applies only to kCapturedObject kinds.
127 128 129 130
  };

  union {
    // kind kTagged. After handlification it is always nullptr.
131
    Object raw_literal_;
132 133 134 135
    // kind is kUInt32 or kBoolBit.
    uint32_t uint32_value_;
    // kind is kInt32.
    int32_t int32_value_;
136 137
    // kind is kInt64.
    int64_t int64_value_;
138
    // kind is kFloat
139
    Float32 float_value_;
140
    // kind is kDouble
141
    Float64 double_value_;
142
    // kind is kDuplicatedObject or kCapturedObject.
143 144 145 146
    MaterializedObjectInfo materialization_info_;
  };

  // Checked accessors for the union members.
147
  Object raw_literal() const;
148
  int32_t int32_value() const;
149
  int64_t int64_value() const;
150
  uint32_t uint32_value() const;
151 152
  Float32 float_value() const;
  Float64 double_value() const;
153 154 155 156 157 158 159 160
  int object_length() const;
  int object_index() const;
};


class TranslatedFrame {
 public:
  enum Kind {
161
    kInterpretedFunction,
162 163
    kArgumentsAdaptor,
    kConstructStub,
164 165
    kBuiltinContinuation,
    kJavaScriptBuiltinContinuation,
166
    kJavaScriptBuiltinContinuationWithCatch,
167 168 169 170 171 172
    kInvalid
  };

  int GetValueCount();

  Kind kind() const { return kind_; }
173
  BailoutId node_id() const { return node_id_; }
174
  Handle<SharedFunctionInfo> shared_info() const { return shared_info_; }
175
  int height() const { return height_; }
176 177
  int return_value_offset() const { return return_value_offset_; }
  int return_value_count() const { return return_value_count_; }
178

179 180
  SharedFunctionInfo raw_shared_info() const {
    CHECK(!raw_shared_info_.is_null());
181 182 183
    return raw_shared_info_;
  }

184 185 186
  class iterator {
   public:
    iterator& operator++() {
187
      ++input_index_;
188 189 190 191 192
      AdvanceIterator(&position_);
      return *this;
    }

    iterator operator++(int) {
193
      iterator original(position_, input_index_);
194
      ++input_index_;
195 196 197 198 199
      AdvanceIterator(&position_);
      return original;
    }

    bool operator==(const iterator& other) const {
200
      // Ignore {input_index_} for equality.
201 202 203 204 205 206
      return position_ == other.position_;
    }
    bool operator!=(const iterator& other) const { return !(*this == other); }

    TranslatedValue& operator*() { return (*position_); }
    TranslatedValue* operator->() { return &(*position_); }
207 208
    const TranslatedValue& operator*() const { return (*position_); }
    const TranslatedValue* operator->() const { return &(*position_); }
209

210 211
    int input_index() const { return input_index_; }

212 213 214
   private:
    friend TranslatedFrame;

215 216 217
    explicit iterator(std::deque<TranslatedValue>::iterator position,
                      int input_index = 0)
        : position_(position), input_index_(input_index) {}
218 219

    std::deque<TranslatedValue>::iterator position_;
220
    int input_index_;
221 222
  };

223 224 225
  typedef TranslatedValue& reference;
  typedef TranslatedValue const& const_reference;

226 227
  iterator begin() { return iterator(values_.begin()); }
  iterator end() { return iterator(values_.end()); }
228

229 230
  reference front() { return values_.front(); }
  const_reference front() const { return values_.front(); }
231 232 233 234 235

 private:
  friend class TranslatedState;

  // Constructor static methods.
236
  static TranslatedFrame InterpretedFrame(BailoutId bytecode_offset,
237
                                          SharedFunctionInfo shared_info,
238 239
                                          int height, int return_value_offset,
                                          int return_value_count);
240
  static TranslatedFrame AccessorFrame(Kind kind,
241 242
                                       SharedFunctionInfo shared_info);
  static TranslatedFrame ArgumentsAdaptorFrame(SharedFunctionInfo shared_info,
243
                                               int height);
244
  static TranslatedFrame ConstructStubFrame(BailoutId bailout_id,
245
                                            SharedFunctionInfo shared_info,
246
                                            int height);
247
  static TranslatedFrame BuiltinContinuationFrame(
248
      BailoutId bailout_id, SharedFunctionInfo shared_info, int height);
249
  static TranslatedFrame JavaScriptBuiltinContinuationFrame(
250
      BailoutId bailout_id, SharedFunctionInfo shared_info, int height);
251
  static TranslatedFrame JavaScriptBuiltinContinuationWithCatchFrame(
252
      BailoutId bailout_id, SharedFunctionInfo shared_info, int height);
253
  static TranslatedFrame InvalidFrame() {
254
    return TranslatedFrame(kInvalid, SharedFunctionInfo());
255 256 257 258
  }

  static void AdvanceIterator(std::deque<TranslatedValue>::iterator* iter);

259 260
  TranslatedFrame(Kind kind,
                  SharedFunctionInfo shared_info = SharedFunctionInfo(),
261 262
                  int height = 0, int return_value_offset = 0,
                  int return_value_count = 0)
263 264
      : kind_(kind),
        node_id_(BailoutId::None()),
265
        raw_shared_info_(shared_info),
266 267 268
        height_(height),
        return_value_offset_(return_value_offset),
        return_value_count_(return_value_count) {}
269 270

  void Add(const TranslatedValue& value) { values_.push_back(value); }
271
  TranslatedValue* ValueAt(int index) { return &(values_[index]); }
272
  void Handlify();
273 274 275

  Kind kind_;
  BailoutId node_id_;
276
  SharedFunctionInfo raw_shared_info_;
277
  Handle<SharedFunctionInfo> shared_info_;
278
  int height_;
279 280
  int return_value_offset_;
  int return_value_count_;
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304

  typedef std::deque<TranslatedValue> ValuesContainer;

  ValuesContainer values_;
};


// Auxiliary class for translating deoptimization values.
// Typical usage sequence:
//
// 1. Construct the instance. This will involve reading out the translations
//    and resolving them to values using the supplied frame pointer and
//    machine state (registers). This phase is guaranteed not to allocate
//    and not to use any HandleScope. Any object pointers will be stored raw.
//
// 2. Handlify pointers. This will convert all the raw pointers to handles.
//
// 3. Reading out the frame values.
//
// Note: After the instance is constructed, it is possible to iterate over
// the values eagerly.

class TranslatedState {
 public:
305
  TranslatedState() = default;
306
  explicit TranslatedState(const JavaScriptFrame* frame);
307

308
  void Prepare(Address stack_frame_pointer);
309 310

  // Store newly materialized values into the isolate.
311
  void StoreMaterializedValuesAndDeopt(JavaScriptFrame* frame);
312

313 314 315 316
  typedef std::vector<TranslatedFrame>::iterator iterator;
  iterator begin() { return frames_.begin(); }
  iterator end() { return frames_.end(); }

317 318 319 320
  typedef std::vector<TranslatedFrame>::const_iterator const_iterator;
  const_iterator begin() const { return frames_.begin(); }
  const_iterator end() const { return frames_.end(); }

321 322
  std::vector<TranslatedFrame>& frames() { return frames_; }

323
  TranslatedFrame* GetFrameFromJSFrameIndex(int jsframe_index);
324 325 326 327 328
  TranslatedFrame* GetArgumentsInfoFromJSFrameIndex(int jsframe_index,
                                                    int* arguments_count);

  Isolate* isolate() { return isolate_; }

329
  void Init(Isolate* isolate, Address input_frame_pointer,
330
            TranslationIterator* iterator, FixedArray literal_array,
331
            RegisterValues* registers, FILE* trace_file, int parameter_count);
332

333
  void VerifyMaterializedObjects();
334
  bool DoUpdateFeedback();
335

336 337 338 339
 private:
  friend TranslatedValue;

  TranslatedFrame CreateNextTranslatedFrame(TranslationIterator* iterator,
340 341
                                            FixedArray literal_array,
                                            Address fp, FILE* trace_file);
342
  int CreateNextTranslatedValue(int frame_index, TranslationIterator* iterator,
343
                                FixedArray literal_array, Address fp,
344
                                RegisterValues* registers, FILE* trace_file);
345 346
  Address ComputeArgumentsPosition(Address input_frame_pointer,
                                   CreateArgumentsType type, int* length);
347 348
  void CreateArgumentsElementsTranslatedValues(int frame_index,
                                               Address input_frame_pointer,
349 350
                                               CreateArgumentsType type,
                                               FILE* trace_file);
351 352

  void UpdateFromPreviouslyMaterializedObjects();
353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379
  void MaterializeFixedDoubleArray(TranslatedFrame* frame, int* value_index,
                                   TranslatedValue* slot, Handle<Map> map);
  void MaterializeMutableHeapNumber(TranslatedFrame* frame, int* value_index,
                                    TranslatedValue* slot);

  void EnsureObjectAllocatedAt(TranslatedValue* slot);

  void SkipSlots(int slots_to_skip, TranslatedFrame* frame, int* value_index);

  Handle<ByteArray> AllocateStorageFor(TranslatedValue* slot);
  void EnsureJSObjectAllocated(TranslatedValue* slot, Handle<Map> map);
  void EnsurePropertiesAllocatedAndMarked(TranslatedValue* properties_slot,
                                          Handle<Map> map);
  void EnsureChildrenAllocated(int count, TranslatedFrame* frame,
                               int* value_index, std::stack<int>* worklist);
  void EnsureCapturedObjectAllocatedAt(int object_index,
                                       std::stack<int>* worklist);
  Handle<Object> InitializeObjectAt(TranslatedValue* slot);
  void InitializeCapturedObjectAt(int object_index, std::stack<int>* worklist,
                                  const DisallowHeapAllocation& no_allocation);
  void InitializeJSObjectAt(TranslatedFrame* frame, int* value_index,
                            TranslatedValue* slot, Handle<Map> map,
                            const DisallowHeapAllocation& no_allocation);
  void InitializeObjectWithTaggedFieldsAt(
      TranslatedFrame* frame, int* value_index, TranslatedValue* slot,
      Handle<Map> map, const DisallowHeapAllocation& no_allocation);

380
  void ReadUpdateFeedback(TranslationIterator* iterator,
381
                          FixedArray literal_array, FILE* trace_file);
382

383 384 385
  TranslatedValue* ResolveCapturedObject(TranslatedValue* slot);
  TranslatedValue* GetValueByObjectIndex(int object_index);
  Handle<Object> GetValueAndAdvance(TranslatedFrame* frame, int* value_index);
386 387

  static uint32_t GetUInt32Slot(Address fp, int slot_index);
388
  static uint64_t GetUInt64Slot(Address fp, int slot_index);
389 390
  static Float32 GetFloatSlot(Address fp, int slot_index);
  static Float64 GetDoubleSlot(Address fp, int slot_index);
391 392

  std::vector<TranslatedFrame> frames_;
393
  Isolate* isolate_ = nullptr;
394
  Address stack_frame_pointer_ = kNullAddress;
395
  int formal_parameter_count_;
396 397 398 399 400 401

  struct ObjectPosition {
    int frame_index_;
    int value_index_;
  };
  std::deque<ObjectPosition> object_positions_;
402
  Handle<FeedbackVector> feedback_vector_handle_;
403
  FeedbackVector feedback_vector_;
404
  FeedbackSlot feedback_slot_;
405
};
406

407
class OptimizedFunctionVisitor {
408
 public:
409
  virtual ~OptimizedFunctionVisitor() = default;
410
  virtual void VisitFunction(JSFunction function) = 0;
411 412 413 414
};

class Deoptimizer : public Malloced {
 public:
415
  struct DeoptInfo {
416 417
    DeoptInfo(SourcePosition position, DeoptimizeReason deopt_reason,
              int deopt_id)
418
        : position(position), deopt_reason(deopt_reason), deopt_id(deopt_id) {}
419

420
    SourcePosition position;
421
    DeoptimizeReason deopt_reason;
422 423 424
    int deopt_id;

    static const int kNoDeoptId = -1;
425 426
  };

427
  static DeoptInfo GetDeoptInfo(Code code, Address from);
428

429
  static int ComputeSourcePositionFromBytecodeArray(SharedFunctionInfo shared,
430
                                                    BailoutId node_id);
431

432
  struct JumpTableEntry : public ZoneObject {
433
    inline JumpTableEntry(Address entry, const DeoptInfo& deopt_info,
434
                          DeoptimizeKind kind, bool frame)
435 436
        : label(),
          address(entry),
437
          deopt_info(deopt_info),
438
          deopt_kind(kind),
439
          needs_frame(frame) {}
440 441

    bool IsEquivalentTo(const JumpTableEntry& other) const {
442
      return address == other.address && deopt_kind == other.deopt_kind &&
443
             needs_frame == other.needs_frame;
444 445
    }

446 447
    Label label;
    Address address;
448
    DeoptInfo deopt_info;
449
    DeoptimizeKind deopt_kind;
450 451 452
    bool needs_frame;
  };

453
  static const char* MessageFor(DeoptimizeKind kind);
454

455 456
  int output_count() const { return output_count_; }

457 458
  Handle<JSFunction> function() const;
  Handle<Code> compiled_code() const;
459
  DeoptimizeKind deopt_kind() const { return deopt_kind_; }
460

461 462 463
  // Number of created JS frames. Not all created frames are necessarily JS.
  int jsframe_count() const { return jsframe_count_; }

464
  static Deoptimizer* New(Address raw_function, DeoptimizeKind kind,
465
                          unsigned bailout_id, Address from, int fp_to_sp_delta,
466 467
                          Isolate* isolate);
  static Deoptimizer* Grab(Isolate* isolate);
468

469 470 471
  // The returned object with information on the optimized frame needs to be
  // freed before another one can be generated.
  static DeoptimizedFrameInfo* DebuggerInspectableFrame(JavaScriptFrame* frame,
472
                                                        int jsframe_index,
473 474
                                                        Isolate* isolate);

475 476
  // Deoptimize the function now. Its current optimized code will never be run
  // again and any activations of the optimized code will get deoptimized when
477 478
  // execution returns. If {code} is specified then the given code is targeted
  // instead of the function code (e.g. OSR code not installed on function).
479
  static void DeoptimizeFunction(JSFunction function, Code code = Code());
480

481
  // Deoptimize all code in the given isolate.
482
  static void DeoptimizeAll(Isolate* isolate);
483

484 485 486 487
  // Deoptimizes all optimized code that has been previously marked
  // (via code->set_marked_for_deoptimization) and unlinks all functions that
  // refer to that code.
  static void DeoptimizeMarkedCode(Isolate* isolate);
488

489 490
  ~Deoptimizer();

491
  void MaterializeHeapObjects();
492

493
  static void ComputeOutputFrames(Deoptimizer* deoptimizer);
494

495
  static Address GetDeoptimizationEntry(Isolate* isolate, DeoptimizeKind kind);
496

497 498 499 500 501
  // Returns true if {addr} is a deoptimization entry and stores its type in
  // {type}. Returns false if {addr} is not a deoptimization entry.
  static bool IsDeoptimizationEntry(Isolate* isolate, Address addr,
                                    DeoptimizeKind* type);

502 503 504 505 506 507 508
  // Code generation support.
  static int input_offset() { return OFFSET_OF(Deoptimizer, input_); }
  static int output_count_offset() {
    return OFFSET_OF(Deoptimizer, output_count_);
  }
  static int output_offset() { return OFFSET_OF(Deoptimizer, output_); }

509 510 511 512
  static int caller_frame_top_offset() {
    return OFFSET_OF(Deoptimizer, caller_frame_top_);
  }

513
  static int GetDeoptimizedCodeCount(Isolate* isolate);
514 515 516

  static const int kNotDeoptimizationEntry = -1;

517
  static void EnsureCodeForDeoptimizationEntry(Isolate* isolate,
518
                                               DeoptimizeKind kind);
519
  static void EnsureCodeForDeoptimizationEntries(Isolate* isolate);
520

521 522
  Isolate* isolate() const { return isolate_; }

523 524
  static const int kMaxNumberOfEntries = 16384;

525
 private:
526
  friend class FrameWriter;
527
  void QueueValueForMaterialization(Address output_address, Object obj,
528
                                    const TranslatedFrame::iterator& iterator);
529

530

531
  Deoptimizer(Isolate* isolate, JSFunction function, DeoptimizeKind kind,
532
              unsigned bailout_id, Address from, int fp_to_sp_delta);
533
  Code FindOptimizedCode();
534
  void PrintFunctionName();
535 536
  void DeleteFrameDescriptions();

537 538
  static bool IsDeoptimizationEntry(Isolate* isolate, Address addr,
                                    DeoptimizeKind type);
539

540
  void DoComputeOutputFrames();
541 542 543 544 545 546
  void DoComputeInterpretedFrame(TranslatedFrame* translated_frame,
                                 int frame_index, bool goto_catch_handler);
  void DoComputeArgumentsAdaptorFrame(TranslatedFrame* translated_frame,
                                      int frame_index);
  void DoComputeConstructStubFrame(TranslatedFrame* translated_frame,
                                   int frame_index);
547 548 549 550 551 552 553 554 555 556 557 558 559 560

  enum class BuiltinContinuationMode {
    STUB,
    JAVASCRIPT,
    JAVASCRIPT_WITH_CATCH,
    JAVASCRIPT_HANDLE_EXCEPTION
  };
  static bool BuiltinContinuationModeIsWithCatch(BuiltinContinuationMode mode);
  static bool BuiltinContinuationModeIsJavaScript(BuiltinContinuationMode mode);
  static StackFrame::Type BuiltinContinuationModeToFrameType(
      BuiltinContinuationMode mode);
  static Builtins::Name TrampolineForBuiltinContinuation(
      BuiltinContinuationMode mode, bool must_handle_result);

561
  void DoComputeBuiltinContinuation(TranslatedFrame* translated_frame,
562 563
                                    int frame_index,
                                    BuiltinContinuationMode mode);
564

565
  unsigned ComputeInputFrameAboveFpFixedSize() const;
566
  unsigned ComputeInputFrameSize() const;
567
  static unsigned ComputeInterpretedFixedSize(SharedFunctionInfo shared);
568

569
  static unsigned ComputeIncomingArgumentSize(SharedFunctionInfo shared);
570
  static unsigned ComputeOutgoingArgumentSize(Code code, unsigned bailout_id);
571

572
  static void GenerateDeoptimizationEntries(MacroAssembler* masm,
573
                                            Isolate* isolate,
574
                                            DeoptimizeKind kind);
575

576
  // Marks all the code in the given context for deoptimization.
577
  static void MarkAllCodeForContext(Context native_context);
578 579

  // Deoptimizes all code marked in the given context.
580
  static void DeoptimizeMarkedCodeForContext(Context native_context);
581

582 583 584 585
  // Some architectures need to push padding together with the TOS register
  // in order to maintain stack alignment.
  static bool PadTopOfStackRegister();

586 587 588
  // Searches the list of known deoptimizing code for a Code object
  // containing the given address (which is supposedly faster than
  // searching all code objects).
589
  Code FindDeoptimizingCode(Address addr);
590

591
  Isolate* isolate_;
592
  JSFunction function_;
593
  Code compiled_code_;
594
  unsigned bailout_id_;
595
  DeoptimizeKind deopt_kind_;
596 597
  Address from_;
  int fp_to_sp_delta_;
598 599 600
  bool deoptimizing_throw_;
  int catch_handler_data_;
  int catch_handler_pc_offset_;
601 602 603 604 605

  // Input frame description.
  FrameDescription* input_;
  // Number of output frames.
  int output_count_;
606 607
  // Number of output js frames.
  int jsframe_count_;
608 609 610
  // Array of output frame descriptions.
  FrameDescription** output_;

611 612 613 614 615 616 617
  // Caller frame details computed from input frame.
  intptr_t caller_frame_top_;
  intptr_t caller_fp_;
  intptr_t caller_pc_;
  intptr_t caller_constant_pool_;
  intptr_t input_frame_context_;

jarin@chromium.org's avatar
jarin@chromium.org committed
618
  // Key for lookup of previously materialized objects
619
  intptr_t stack_fp_;
jarin@chromium.org's avatar
jarin@chromium.org committed
620

621 622 623 624 625 626
  TranslatedState translated_state_;
  struct ValueToMaterialize {
    Address output_slot_address_;
    TranslatedFrame::iterator value_;
  };
  std::vector<ValueToMaterialize> values_to_materialize_;
627

628 629 630
#ifdef DEBUG
  DisallowHeapAllocation* disallow_heap_allocation_;
#endif  // DEBUG
631

632
  CodeTracer::Scope* trace_scope_;
633

634
  static const int table_entry_size_;
635 636

  friend class FrameDescription;
637
  friend class DeoptimizedFrameInfo;
638 639 640
};


641 642 643 644 645 646 647 648 649 650 651 652 653 654 655
class RegisterValues {
 public:
  intptr_t GetRegister(unsigned n) const {
#if DEBUG
    // This convoluted DCHECK is needed to work around a gcc problem that
    // improperly detects an array bounds overflow in optimized debug builds
    // when using a plain DCHECK.
    if (n >= arraysize(registers_)) {
      DCHECK(false);
      return 0;
    }
#endif
    return registers_[n];
  }

656
  Float32 GetFloatRegister(unsigned n) const {
657 658 659 660
    DCHECK(n < arraysize(float_registers_));
    return float_registers_[n];
  }

661
  Float64 GetDoubleRegister(unsigned n) const {
662 663 664 665 666 667 668 669 670
    DCHECK(n < arraysize(double_registers_));
    return double_registers_[n];
  }

  void SetRegister(unsigned n, intptr_t value) {
    DCHECK(n < arraysize(registers_));
    registers_[n] = value;
  }

671
  void SetFloatRegister(unsigned n, Float32 value) {
672 673 674 675
    DCHECK(n < arraysize(float_registers_));
    float_registers_[n] = value;
  }

676
  void SetDoubleRegister(unsigned n, Float64 value) {
677 678 679 680
    DCHECK(n < arraysize(double_registers_));
    double_registers_[n] = value;
  }

681 682 683 684 685
  // Generated code is writing directly into the below arrays, make sure their
  // element sizes fit what the machine instructions expect.
  static_assert(sizeof(Float32) == kFloatSize, "size mismatch");
  static_assert(sizeof(Float64) == kDoubleSize, "size mismatch");

686
  intptr_t registers_[Register::kNumRegisters];
687 688
  Float32 float_registers_[FloatRegister::kNumRegisters];
  Float64 double_registers_[DoubleRegister::kNumRegisters];
689 690 691
};


692 693
class FrameDescription {
 public:
694
  explicit FrameDescription(uint32_t frame_size, int parameter_count = 0);
695 696

  void* operator new(size_t size, uint32_t frame_size) {
697 698 699
    // Subtracts kSystemPointerSize, as the member frame_content_ already
    // supplies the first element of the area to store the frame.
    return malloc(size + frame_size - kSystemPointerSize);
700 701
  }

702 703 704 705
  void operator delete(void* pointer, uint32_t frame_size) {
    free(pointer);
  }

706 707 708 709
  void operator delete(void* description) {
    free(description);
  }

710
  uint32_t GetFrameSize() const {
711
    USE(frame_content_);
712
    DCHECK(static_cast<uint32_t>(frame_size_) == frame_size_);
713 714
    return static_cast<uint32_t>(frame_size_);
  }
715

716
  intptr_t GetFrameSlot(unsigned offset) {
717 718 719
    return *GetFrameSlotPointer(offset);
  }

720 721 722
  unsigned GetLastArgumentSlotOffset() {
    int parameter_slots = parameter_count();
    if (kPadArguments) parameter_slots = RoundUp(parameter_slots, 2);
723
    return GetFrameSize() - parameter_slots * kSystemPointerSize;
724 725
  }

726
  Address GetFramePointerAddress() {
727 728
    int fp_offset =
        GetLastArgumentSlotOffset() - StandardFrameConstants::kCallerSPOffset;
729
    return reinterpret_cast<Address>(GetFrameSlotPointer(fp_offset));
730 731
  }

732 733
  RegisterValues* GetRegisterValues() { return &register_values_; }

734
  void SetFrameSlot(unsigned offset, intptr_t value) {
735 736 737
    *GetFrameSlotPointer(offset) = value;
  }

738 739 740 741
  void SetCallerPc(unsigned offset, intptr_t value);

  void SetCallerFp(unsigned offset, intptr_t value);

742 743
  void SetCallerConstantPool(unsigned offset, intptr_t value);

744
  intptr_t GetRegister(unsigned n) const {
745
    return register_values_.GetRegister(n);
746 747
  }

748
  Float64 GetDoubleRegister(unsigned n) const {
749
    return register_values_.GetDoubleRegister(n);
750 751
  }

752
  void SetRegister(unsigned n, intptr_t value) {
753
    register_values_.SetRegister(n, value);
754 755
  }

756
  void SetDoubleRegister(unsigned n, Float64 value) {
757
    register_values_.SetDoubleRegister(n, value);
758 759
  }

760 761
  intptr_t GetTop() const { return top_; }
  void SetTop(intptr_t top) { top_ = top; }
762

763 764
  intptr_t GetPc() const { return pc_; }
  void SetPc(intptr_t pc) { pc_ = pc; }
765

766 767
  intptr_t GetFp() const { return fp_; }
  void SetFp(intptr_t fp) { fp_ = fp; }
768

769 770 771
  intptr_t GetContext() const { return context_; }
  void SetContext(intptr_t context) { context_ = context; }

772 773 774 775 776
  intptr_t GetConstantPool() const { return constant_pool_; }
  void SetConstantPool(intptr_t constant_pool) {
    constant_pool_ = constant_pool;
  }

777
  void SetContinuation(intptr_t pc) { continuation_ = pc; }
778

779 780
  // Argument count, including receiver.
  int parameter_count() { return parameter_count_; }
781

782
  static int registers_offset() {
783
    return OFFSET_OF(FrameDescription, register_values_.registers_);
784 785 786
  }

  static int double_registers_offset() {
787
    return OFFSET_OF(FrameDescription, register_values_.double_registers_);
788 789
  }

790 791 792 793
  static int float_registers_offset() {
    return OFFSET_OF(FrameDescription, register_values_.float_registers_);
  }

794
  static int frame_size_offset() {
795
    return offsetof(FrameDescription, frame_size_);
796 797
  }

798
  static int pc_offset() { return offsetof(FrameDescription, pc_); }
799 800

  static int continuation_offset() {
801
    return offsetof(FrameDescription, continuation_);
802 803 804
  }

  static int frame_content_offset() {
805
    return offsetof(FrameDescription, frame_content_);
806 807 808 809 810
  }

 private:
  static const uint32_t kZapUint32 = 0xbeeddead;

811 812 813
  // Frame_size_ must hold a uint32_t value.  It is only a uintptr_t to
  // keep the variable-size array frame_content_ of type intptr_t at
  // the end of the structure aligned.
814
  uintptr_t frame_size_;  // Number of bytes.
815
  int parameter_count_;
816
  RegisterValues register_values_;
817 818 819
  intptr_t top_;
  intptr_t pc_;
  intptr_t fp_;
820
  intptr_t context_;
821
  intptr_t constant_pool_;
822 823 824

  // Continuation is the PC where the execution continues after
  // deoptimizing.
825
  intptr_t continuation_;
826

827 828 829 830
  // This must be at the end of the object as the object is allocated larger
  // than it's definition indicate to extend this array.
  intptr_t frame_content_[1];

831
  intptr_t* GetFrameSlotPointer(unsigned offset) {
832
    DCHECK(offset < frame_size_);
833
    return reinterpret_cast<intptr_t*>(
834 835 836 837 838
        reinterpret_cast<Address>(this) + frame_content_offset() + offset);
  }
};


839 840
class DeoptimizerData {
 public:
841
  explicit DeoptimizerData(Heap* heap);
842 843
  ~DeoptimizerData();

844 845 846 847 848 849 850 851 852
#ifdef DEBUG
  bool IsDeoptEntryCode(Code code) const {
    for (int i = 0; i < kLastDeoptimizeKind + 1; i++) {
      if (code == deopt_entry_code_[i]) return true;
    }
    return false;
  }
#endif  // DEBUG

853
 private:
854
  Heap* heap_;
855 856
  static const int kLastDeoptimizeKind =
      static_cast<int>(DeoptimizeKind::kLastDeoptimizeKind);
857 858 859
  Code deopt_entry_code_[kLastDeoptimizeKind + 1];
  Code deopt_entry_code(DeoptimizeKind kind);
  void set_deopt_entry_code(DeoptimizeKind kind, Code code);
860

861
  Deoptimizer* current_;
862 863 864 865 866 867

  friend class Deoptimizer;

  DISALLOW_COPY_AND_ASSIGN(DeoptimizerData);
};

868
class TranslationBuffer {
869
 public:
870
  explicit TranslationBuffer(Zone* zone) : contents_(zone) {}
871

872 873
  int CurrentIndex() const { return static_cast<int>(contents_.size()); }
  void Add(int32_t value);
874

875
  Handle<ByteArray> CreateByteArray(Factory* factory);
876 877

 private:
878
  ZoneChunkList<uint8_t> contents_;
879 880
};

881
class TranslationIterator {
882
 public:
883
  TranslationIterator(ByteArray buffer, int index);
884 885 886

  int32_t Next();

887
  bool HasNext() const;
888 889 890 891 892 893

  void Skip(int n) {
    for (int i = 0; i < n; i++) Next();
  }

 private:
894
  ByteArray buffer_;
895 896 897
  int index_;
};

898 899 900 901 902 903 904 905 906 907 908 909 910 911
#define TRANSLATION_OPCODE_LIST(V)                     \
  V(BEGIN)                                             \
  V(INTERPRETED_FRAME)                                 \
  V(BUILTIN_CONTINUATION_FRAME)                        \
  V(JAVA_SCRIPT_BUILTIN_CONTINUATION_FRAME)            \
  V(JAVA_SCRIPT_BUILTIN_CONTINUATION_WITH_CATCH_FRAME) \
  V(CONSTRUCT_STUB_FRAME)                              \
  V(ARGUMENTS_ADAPTOR_FRAME)                           \
  V(DUPLICATED_OBJECT)                                 \
  V(ARGUMENTS_ELEMENTS)                                \
  V(ARGUMENTS_LENGTH)                                  \
  V(CAPTURED_OBJECT)                                   \
  V(REGISTER)                                          \
  V(INT32_REGISTER)                                    \
912
  V(INT64_REGISTER)                                    \
913 914 915 916 917 918
  V(UINT32_REGISTER)                                   \
  V(BOOL_REGISTER)                                     \
  V(FLOAT_REGISTER)                                    \
  V(DOUBLE_REGISTER)                                   \
  V(STACK_SLOT)                                        \
  V(INT32_STACK_SLOT)                                  \
919
  V(INT64_STACK_SLOT)                                  \
920 921 922 923 924
  V(UINT32_STACK_SLOT)                                 \
  V(BOOL_STACK_SLOT)                                   \
  V(FLOAT_STACK_SLOT)                                  \
  V(DOUBLE_STACK_SLOT)                                 \
  V(LITERAL)                                           \
925
  V(UPDATE_FEEDBACK)
926

927
class Translation {
928
 public:
929
#define DECLARE_TRANSLATION_OPCODE_ENUM(item) item,
930
  enum Opcode {
931 932
    TRANSLATION_OPCODE_LIST(DECLARE_TRANSLATION_OPCODE_ENUM)
    LAST = LITERAL
933
  };
934
#undef DECLARE_TRANSLATION_OPCODE_ENUM
935

936
  Translation(TranslationBuffer* buffer, int frame_count, int jsframe_count,
937 938
              int update_feedback_count, Zone* zone)
      : buffer_(buffer), index_(buffer->CurrentIndex()), zone_(zone) {
939 940 941
    buffer_->Add(BEGIN);
    buffer_->Add(frame_count);
    buffer_->Add(jsframe_count);
942
    buffer_->Add(update_feedback_count);
943 944 945 946 947
  }

  int index() const { return index_; }

  // Commands.
948
  void BeginInterpretedFrame(BailoutId bytecode_offset, int literal_id,
949 950
                             unsigned height, int return_value_offset,
                             int return_value_count);
951
  void BeginArgumentsAdaptorFrame(int literal_id, unsigned height);
952 953
  void BeginConstructStubFrame(BailoutId bailout_id, int literal_id,
                               unsigned height);
954 955 956 957
  void BeginBuiltinContinuationFrame(BailoutId bailout_id, int literal_id,
                                     unsigned height);
  void BeginJavaScriptBuiltinContinuationFrame(BailoutId bailout_id,
                                               int literal_id, unsigned height);
958 959 960
  void BeginJavaScriptBuiltinContinuationWithCatchFrame(BailoutId bailout_id,
                                                        int literal_id,
                                                        unsigned height);
961 962
  void ArgumentsElements(CreateArgumentsType type);
  void ArgumentsLength(CreateArgumentsType type);
963
  void BeginCapturedObject(int length);
964
  void AddUpdateFeedback(int vector_literal, int slot);
965
  void DuplicateObject(int object_index);
966 967
  void StoreRegister(Register reg);
  void StoreInt32Register(Register reg);
968
  void StoreInt64Register(Register reg);
969
  void StoreUint32Register(Register reg);
970
  void StoreBoolRegister(Register reg);
971
  void StoreFloatRegister(FloatRegister reg);
972 973 974
  void StoreDoubleRegister(DoubleRegister reg);
  void StoreStackSlot(int index);
  void StoreInt32StackSlot(int index);
975
  void StoreInt64StackSlot(int index);
976
  void StoreUint32StackSlot(int index);
977
  void StoreBoolStackSlot(int index);
978
  void StoreFloatStackSlot(int index);
979 980
  void StoreDoubleStackSlot(int index);
  void StoreLiteral(int literal_id);
981
  void StoreJSFrameFunction();
982

983
  Zone* zone() const { return zone_; }
984

985 986
  static int NumberOfOperandsFor(Opcode opcode);

987
#if defined(OBJECT_PRINT) || defined(ENABLE_DISASSEMBLER)
988 989 990 991 992 993
  static const char* StringFor(Opcode opcode);
#endif

 private:
  TranslationBuffer* buffer_;
  int index_;
994
  Zone* zone_;
995 996 997
};


jarin@chromium.org's avatar
jarin@chromium.org committed
998 999 1000 1001 1002 1003 1004
class MaterializedObjectStore {
 public:
  explicit MaterializedObjectStore(Isolate* isolate) : isolate_(isolate) {
  }

  Handle<FixedArray> Get(Address fp);
  void Set(Address fp, Handle<FixedArray> materialized_objects);
1005
  bool Remove(Address fp);
jarin@chromium.org's avatar
jarin@chromium.org committed
1006 1007

 private:
1008
  Isolate* isolate() const { return isolate_; }
jarin@chromium.org's avatar
jarin@chromium.org committed
1009 1010 1011 1012 1013 1014
  Handle<FixedArray> GetStackEntries();
  Handle<FixedArray> EnsureStackEntries(int size);

  int StackIdToIndex(Address fp);

  Isolate* isolate_;
1015
  std::vector<Address> frame_fps_;
1016 1017 1018
};


1019 1020 1021 1022
// Class used to represent an unoptimized frame when the debugger
// needs to inspect a frame that is part of an optimized frame. The
// internally used FrameDescription objects are not GC safe so for use
// by the debugger frame information is copied to an object of this type.
1023 1024
// Represents parameters in unadapted form so their number might mismatch
// formal parameter count.
1025 1026
class DeoptimizedFrameInfo : public Malloced {
 public:
1027 1028 1029
  DeoptimizedFrameInfo(TranslatedState* state,
                       TranslatedState::iterator frame_it, Isolate* isolate);

1030
  // Return the number of incoming arguments.
1031
  int parameters_count() { return static_cast<int>(parameters_.size()); }
1032

1033
  // Return the height of the expression stack.
1034
  int expression_count() { return static_cast<int>(expression_stack_.size()); }
1035

1036
  // Get the frame function.
1037
  Handle<JSFunction> GetFunction() { return function_; }
1038

1039
  // Get the frame context.
1040
  Handle<Object> GetContext() { return context_; }
1041

1042
  // Get an incoming argument.
1043
  Handle<Object> GetParameter(int index) {
1044
    DCHECK(0 <= index && index < parameters_count());
1045 1046 1047
    return parameters_[index];
  }

1048
  // Get an expression from the expression stack.
1049
  Handle<Object> GetExpression(int index) {
1050
    DCHECK(0 <= index && index < expression_count());
1051 1052 1053
    return expression_stack_[index];
  }

1054 1055
  int GetSourcePosition() {
    return source_position_;
1056 1057
  }

1058
 private:
1059
  // Set an incoming argument.
1060
  void SetParameter(int index, Handle<Object> obj) {
1061
    DCHECK(0 <= index && index < parameters_count());
1062 1063 1064
    parameters_[index] = obj;
  }

1065
  // Set an expression on the expression stack.
1066
  void SetExpression(int index, Handle<Object> obj) {
1067
    DCHECK(0 <= index && index < expression_count());
1068 1069 1070
    expression_stack_[index] = obj;
  }

1071 1072 1073 1074
  Handle<JSFunction> function_;
  Handle<Object> context_;
  std::vector<Handle<Object> > parameters_;
  std::vector<Handle<Object> > expression_stack_;
1075
  int source_position_;
1076 1077 1078 1079

  friend class Deoptimizer;
};

1080 1081
}  // namespace internal
}  // namespace v8
1082 1083

#endif  // V8_DEOPTIMIZER_H_