heap-refs.h 28.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// Copyright 2019 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef V8_COMPILER_HEAP_REFS_H_
#define V8_COMPILER_HEAP_REFS_H_

#include "src/base/optional.h"
#include "src/ic/call-optimization.h"
#include "src/objects/elements-kind.h"
#include "src/objects/feedback-vector.h"
#include "src/objects/instance-type.h"

namespace v8 {
15 16
class CFunctionInfo;

17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
namespace internal {

class BytecodeArray;
class CallHandlerInfo;
class FixedDoubleArray;
class FunctionTemplateInfo;
class HeapNumber;
class InternalizedString;
class JSBoundFunction;
class JSDataView;
class JSGlobalProxy;
class JSRegExp;
class JSTypedArray;
class NativeContext;
class ScriptContextTable;

namespace compiler {
// Whether we are loading a property or storing to a property.
// For a store during literal creation, do not walk up the prototype chain.
enum class AccessMode { kLoad, kStore, kStoreInLiteral, kHas };

38 39
enum class SerializationPolicy { kAssumeSerialized, kSerializeIfNeeded };

40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
enum class OddballType : uint8_t {
  kNone,     // Not an Oddball.
  kBoolean,  // True or False.
  kUndefined,
  kNull,
  kHole,
  kUninitialized,
  kOther  // Oddball, but none of the above.
};

// This list is sorted such that subtypes appear before their supertypes.
// DO NOT VIOLATE THIS PROPERTY!
#define HEAP_BROKER_OBJECT_LIST(V) \
  /* Subtypes of JSObject */       \
  V(JSArray)                       \
  V(JSBoundFunction)               \
  V(JSDataView)                    \
  V(JSFunction)                    \
58
  V(JSGlobalObject)                \
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
  V(JSGlobalProxy)                 \
  V(JSRegExp)                      \
  V(JSTypedArray)                  \
  /* Subtypes of Context */        \
  V(NativeContext)                 \
  /* Subtypes of FixedArray */     \
  V(Context)                       \
  V(ScopeInfo)                     \
  V(ScriptContextTable)            \
  /* Subtypes of FixedArrayBase */ \
  V(BytecodeArray)                 \
  V(FixedArray)                    \
  V(FixedDoubleArray)              \
  /* Subtypes of Name */           \
  V(InternalizedString)            \
  V(String)                        \
  V(Symbol)                        \
76 77
  /* Subtypes of JSReceiver */     \
  V(JSObject)                      \
78
  /* Subtypes of HeapObject */     \
79
  V(AccessorInfo)                  \
80
  V(AllocationSite)                \
81
  V(ArrayBoilerplateDescription)   \
82
  V(BigInt)                        \
83 84 85 86 87 88 89 90 91
  V(CallHandlerInfo)               \
  V(Cell)                          \
  V(Code)                          \
  V(DescriptorArray)               \
  V(FeedbackCell)                  \
  V(FeedbackVector)                \
  V(FixedArrayBase)                \
  V(FunctionTemplateInfo)          \
  V(HeapNumber)                    \
92
  V(JSReceiver)                    \
93 94
  V(Map)                           \
  V(Name)                          \
95
  V(ObjectBoilerplateDescription)  \
96 97 98
  V(PropertyCell)                  \
  V(SharedFunctionInfo)            \
  V(SourceTextModule)              \
99
  V(TemplateObjectDescription)     \
100 101 102 103
  /* Subtypes of Object */         \
  V(HeapObject)

class CompilationDependencies;
104
struct FeedbackSource;
105 106 107 108 109 110 111 112 113 114
class JSHeapBroker;
class ObjectData;
class PerIsolateCompilerCache;
class PropertyAccessInfo;
#define FORWARD_DECL(Name) class Name##Ref;
HEAP_BROKER_OBJECT_LIST(FORWARD_DECL)
#undef FORWARD_DECL

class V8_EXPORT_PRIVATE ObjectRef {
 public:
115 116 117
  ObjectRef(JSHeapBroker* broker, Handle<Object> object,
            bool check_type = true);
  ObjectRef(JSHeapBroker* broker, ObjectData* data, bool check_type = true)
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
      : data_(data), broker_(broker) {
    CHECK_NOT_NULL(data_);
  }

  Handle<Object> object() const;

  bool equals(const ObjectRef& other) const;

  bool IsSmi() const;
  int AsSmi() const;

#define HEAP_IS_METHOD_DECL(Name) bool Is##Name() const;
  HEAP_BROKER_OBJECT_LIST(HEAP_IS_METHOD_DECL)
#undef HEAP_IS_METHOD_DECL

#define HEAP_AS_METHOD_DECL(Name) Name##Ref As##Name() const;
  HEAP_BROKER_OBJECT_LIST(HEAP_AS_METHOD_DECL)
#undef HEAP_AS_METHOD_DECL

  bool IsNullOrUndefined() const;

  bool BooleanValue() const;
  Maybe<double> OddballToNumber() const;

  // Return the element at key {index} if {index} is known to be an own data
  // property of the object that is non-writable and non-configurable.
144 145 146
  base::Optional<ObjectRef> GetOwnConstantElement(
      uint32_t index, SerializationPolicy policy =
                          SerializationPolicy::kAssumeSerialized) const;
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168

  Isolate* isolate() const;

  struct Hash {
    size_t operator()(const ObjectRef& ref) const {
      return base::hash_combine(ref.object().address());
    }
  };
  struct Equal {
    bool operator()(const ObjectRef& lhs, const ObjectRef& rhs) const {
      return lhs.equals(rhs);
    }
  };

 protected:
  JSHeapBroker* broker() const;
  ObjectData* data() const;
  ObjectData* data_;  // Should be used only by object() getters.

 private:
  friend class FunctionTemplateInfoRef;
  friend class JSArrayData;
169 170
  friend class JSGlobalObjectData;
  friend class JSGlobalObjectRef;
171
  friend class JSHeapBroker;
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
  friend class JSObjectData;
  friend class StringData;

  friend std::ostream& operator<<(std::ostream& os, const ObjectRef& ref);

  JSHeapBroker* broker_;
};

// Temporary class that carries information from a Map. We'd like to remove
// this class and use MapRef instead, but we can't as long as we support the
// kDisabled broker mode. That's because obtaining the MapRef via
// HeapObjectRef::map() requires a HandleScope when the broker is disabled.
// During OptimizeGraph we generally don't have a HandleScope, however. There
// are two places where we therefore use GetHeapObjectType() instead. Both that
// function and this class should eventually be removed.
class HeapObjectType {
 public:
  enum Flag : uint8_t { kUndetectable = 1 << 0, kCallable = 1 << 1 };

  using Flags = base::Flags<Flag>;

  HeapObjectType(InstanceType instance_type, Flags flags,
                 OddballType oddball_type)
      : instance_type_(instance_type),
        oddball_type_(oddball_type),
        flags_(flags) {
    DCHECK_EQ(instance_type == ODDBALL_TYPE,
              oddball_type != OddballType::kNone);
  }

  OddballType oddball_type() const { return oddball_type_; }
  InstanceType instance_type() const { return instance_type_; }
  Flags flags() const { return flags_; }

  bool is_callable() const { return flags_ & kCallable; }
  bool is_undetectable() const { return flags_ & kUndetectable; }

 private:
  InstanceType const instance_type_;
  OddballType const oddball_type_;
  Flags const flags_;
};

215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
// Constructors are carefully defined such that we do a type check on
// the outermost Ref class in the inheritance chain only.
#define DEFINE_REF_CONSTRUCTOR(name, base)                                  \
  name##Ref(JSHeapBroker* broker, Handle<Object> object,                    \
            bool check_type = true)                                         \
      : base(broker, object, false) {                                       \
    if (check_type) {                                                       \
      CHECK(Is##name());                                                    \
    }                                                                       \
  }                                                                         \
  name##Ref(JSHeapBroker* broker, ObjectData* data, bool check_type = true) \
      : base(broker, data, false) {                                         \
    if (check_type) {                                                       \
      CHECK(Is##name());                                                    \
    }                                                                       \
  }

232 233
class HeapObjectRef : public ObjectRef {
 public:
234 235
  DEFINE_REF_CONSTRUCTOR(HeapObject, ObjectRef)

236 237 238 239 240 241 242 243 244 245
  Handle<HeapObject> object() const;

  MapRef map() const;

  // See the comment on the HeapObjectType class.
  HeapObjectType GetHeapObjectType() const;
};

class PropertyCellRef : public HeapObjectRef {
 public:
246 247
  DEFINE_REF_CONSTRUCTOR(PropertyCell, HeapObjectRef)

248 249 250 251 252 253 254 255
  Handle<PropertyCell> object() const;

  PropertyDetails property_details() const;

  void Serialize();
  ObjectRef value() const;
};

256
class JSReceiverRef : public HeapObjectRef {
257
 public:
258 259
  DEFINE_REF_CONSTRUCTOR(JSReceiver, HeapObjectRef)

260 261 262 263 264
  Handle<JSReceiver> object() const;
};

class JSObjectRef : public JSReceiverRef {
 public:
265 266
  DEFINE_REF_CONSTRUCTOR(JSObject, JSReceiverRef)

267 268 269 270 271 272
  Handle<JSObject> object() const;

  uint64_t RawFastDoublePropertyAsBitsAt(FieldIndex index) const;
  double RawFastDoublePropertyAt(FieldIndex index) const;
  ObjectRef RawFastPropertyAt(FieldIndex index) const;

273 274
  // Return the value of the property identified by the field {index}
  // if {index} is known to be an own data property of the object.
275 276
  base::Optional<ObjectRef> GetOwnDataProperty(
      Representation field_representation, FieldIndex index,
277 278
      SerializationPolicy policy =
          SerializationPolicy::kAssumeSerialized) const;
279 280 281 282 283 284 285 286 287 288 289
  FixedArrayBaseRef elements() const;
  void SerializeElements();
  void EnsureElementsTenured();
  ElementsKind GetElementsKind() const;

  void SerializeObjectCreateMap();
  base::Optional<MapRef> GetObjectCreateMap() const;
};

class JSDataViewRef : public JSObjectRef {
 public:
290 291
  DEFINE_REF_CONSTRUCTOR(JSDataView, JSObjectRef)

292 293 294 295 296 297 298 299
  Handle<JSDataView> object() const;

  size_t byte_length() const;
  size_t byte_offset() const;
};

class JSBoundFunctionRef : public JSObjectRef {
 public:
300 301
  DEFINE_REF_CONSTRUCTOR(JSBoundFunction, JSObjectRef)

302 303 304
  Handle<JSBoundFunction> object() const;

  void Serialize();
305
  bool serialized() const;
306 307

  // The following are available only after calling Serialize().
308
  JSReceiverRef bound_target_function() const;
309 310 311 312 313 314
  ObjectRef bound_this() const;
  FixedArrayRef bound_arguments() const;
};

class V8_EXPORT_PRIVATE JSFunctionRef : public JSObjectRef {
 public:
315 316
  DEFINE_REF_CONSTRUCTOR(JSFunction, JSObjectRef)

317 318 319 320 321
  Handle<JSFunction> object() const;

  bool has_feedback_vector() const;
  bool has_initial_map() const;
  bool has_prototype() const;
322
  bool IsOptimized() const;
323 324 325 326 327 328 329 330 331 332 333 334
  bool PrototypeRequiresRuntimeLookup() const;

  void Serialize();
  bool serialized() const;

  // The following are available only after calling Serialize().
  ObjectRef prototype() const;
  MapRef initial_map() const;
  ContextRef context() const;
  NativeContextRef native_context() const;
  SharedFunctionInfoRef shared() const;
  FeedbackVectorRef feedback_vector() const;
335
  CodeRef code() const;
336 337 338 339 340
  int InitialMapInstanceSizeWithMinSlack() const;
};

class JSRegExpRef : public JSObjectRef {
 public:
341 342
  DEFINE_REF_CONSTRUCTOR(JSRegExp, JSObjectRef)

343 344 345 346 347 348 349
  Handle<JSRegExp> object() const;

  ObjectRef raw_properties_or_hash() const;
  ObjectRef data() const;
  ObjectRef source() const;
  ObjectRef flags() const;
  ObjectRef last_index() const;
350 351

  void SerializeAsRegExpBoilerplate();
352 353 354 355
};

class HeapNumberRef : public HeapObjectRef {
 public:
356 357
  DEFINE_REF_CONSTRUCTOR(HeapNumber, HeapObjectRef)

358 359 360 361 362 363 364
  Handle<HeapNumber> object() const;

  double value() const;
};

class ContextRef : public HeapObjectRef {
 public:
365 366
  DEFINE_REF_CONSTRUCTOR(Context, HeapObjectRef)

367 368
  Handle<Context> object() const;

369 370 371 372
  // {previous} decrements {depth} by 1 for each previous link successfully
  // followed. If {depth} != 0 on function return, then it only got
  // partway to the desired depth. If {serialize} is true, then
  // {previous} will cache its findings.
373 374 375
  ContextRef previous(size_t* depth,
                      SerializationPolicy policy =
                          SerializationPolicy::kAssumeSerialized) const;
376 377

  // Only returns a value if the index is valid for this ContextRef.
378 379 380
  base::Optional<ObjectRef> get(
      int index, SerializationPolicy policy =
                     SerializationPolicy::kAssumeSerialized) const;
381 382 383 384 385 386 387

  // We only serialize the ScopeInfo if certain Promise
  // builtins are called.
  void SerializeScopeInfo();
  base::Optional<ScopeInfoRef> scope_info() const;
};

388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422
#define BROKER_COMPULSORY_NATIVE_CONTEXT_FIELDS(V) \
  V(JSFunction, array_function)                    \
  V(JSFunction, boolean_function)                  \
  V(JSFunction, bigint_function)                   \
  V(JSFunction, number_function)                   \
  V(JSFunction, object_function)                   \
  V(JSFunction, promise_function)                  \
  V(JSFunction, promise_then)                      \
  V(JSFunction, regexp_function)                   \
  V(JSFunction, string_function)                   \
  V(JSFunction, symbol_function)                   \
  V(JSGlobalObject, global_object)                 \
  V(JSGlobalProxy, global_proxy_object)            \
  V(JSObject, promise_prototype)                   \
  V(Map, block_context_map)                        \
  V(Map, bound_function_with_constructor_map)      \
  V(Map, bound_function_without_constructor_map)   \
  V(Map, catch_context_map)                        \
  V(Map, eval_context_map)                         \
  V(Map, fast_aliased_arguments_map)               \
  V(Map, function_context_map)                     \
  V(Map, initial_array_iterator_map)               \
  V(Map, initial_string_iterator_map)              \
  V(Map, iterator_result_map)                      \
  V(Map, js_array_holey_double_elements_map)       \
  V(Map, js_array_holey_elements_map)              \
  V(Map, js_array_holey_smi_elements_map)          \
  V(Map, js_array_packed_double_elements_map)      \
  V(Map, js_array_packed_elements_map)             \
  V(Map, js_array_packed_smi_elements_map)         \
  V(Map, sloppy_arguments_map)                     \
  V(Map, slow_object_with_null_prototype_map)      \
  V(Map, strict_arguments_map)                     \
  V(Map, with_context_map)                         \
  V(ScriptContextTable, script_context_table)
423 424 425 426 427 428 429 430

// Those are set by Bootstrapper::ExportFromRuntime, which may not yet have
// happened when Turbofan is invoked via --always-opt.
#define BROKER_OPTIONAL_NATIVE_CONTEXT_FIELDS(V) \
  V(Map, async_function_object_map)              \
  V(Map, map_key_iterator_map)                   \
  V(Map, map_key_value_iterator_map)             \
  V(Map, map_value_iterator_map)                 \
431
  V(JSFunction, regexp_exec_function)            \
432 433 434 435 436 437 438 439 440
  V(Map, set_key_value_iterator_map)             \
  V(Map, set_value_iterator_map)

#define BROKER_NATIVE_CONTEXT_FIELDS(V)      \
  BROKER_COMPULSORY_NATIVE_CONTEXT_FIELDS(V) \
  BROKER_OPTIONAL_NATIVE_CONTEXT_FIELDS(V)

class NativeContextRef : public ContextRef {
 public:
441 442
  DEFINE_REF_CONSTRUCTOR(NativeContext, ContextRef)

443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458
  Handle<NativeContext> object() const;

  void Serialize();

#define DECL_ACCESSOR(type, name) type##Ref name() const;
  BROKER_NATIVE_CONTEXT_FIELDS(DECL_ACCESSOR)
#undef DECL_ACCESSOR

  ScopeInfoRef scope_info() const;
  MapRef GetFunctionMapFromIndex(int index) const;
  MapRef GetInitialJSArrayMap(ElementsKind kind) const;
  base::Optional<JSFunctionRef> GetConstructorFunction(const MapRef& map) const;
};

class NameRef : public HeapObjectRef {
 public:
459 460
  DEFINE_REF_CONSTRUCTOR(Name, HeapObjectRef)

461 462 463 464 465 466 467
  Handle<Name> object() const;

  bool IsUniqueName() const;
};

class ScriptContextTableRef : public HeapObjectRef {
 public:
468 469
  DEFINE_REF_CONSTRUCTOR(ScriptContextTable, HeapObjectRef)

470 471 472 473 474
  Handle<ScriptContextTable> object() const;
};

class DescriptorArrayRef : public HeapObjectRef {
 public:
475 476
  DEFINE_REF_CONSTRUCTOR(DescriptorArray, HeapObjectRef)

477 478 479 480 481
  Handle<DescriptorArray> object() const;
};

class FeedbackCellRef : public HeapObjectRef {
 public:
482 483
  DEFINE_REF_CONSTRUCTOR(FeedbackCell, HeapObjectRef)

484
  Handle<FeedbackCell> object() const;
485
  base::Optional<SharedFunctionInfoRef> shared_function_info() const;
486 487 488 489 490
  HeapObjectRef value() const;
};

class FeedbackVectorRef : public HeapObjectRef {
 public:
491 492
  DEFINE_REF_CONSTRUCTOR(FeedbackVector, HeapObjectRef)

493 494
  Handle<FeedbackVector> object() const;

495
  SharedFunctionInfoRef shared_function_info() const;
496 497 498
  double invocation_count() const;

  void Serialize();
499
  bool serialized() const;
500
  FeedbackCellRef GetClosureFeedbackCell(int index) const;
501 502 503 504
};

class CallHandlerInfoRef : public HeapObjectRef {
 public:
505 506
  DEFINE_REF_CONSTRUCTOR(CallHandlerInfo, HeapObjectRef)

507 508 509 510 511 512 513 514
  Handle<CallHandlerInfo> object() const;

  Address callback() const;

  void Serialize();
  ObjectRef data() const;
};

515 516
class AccessorInfoRef : public HeapObjectRef {
 public:
517 518
  DEFINE_REF_CONSTRUCTOR(AccessorInfo, HeapObjectRef)

519 520 521
  Handle<AccessorInfo> object() const;
};

522 523
class AllocationSiteRef : public HeapObjectRef {
 public:
524 525
  DEFINE_REF_CONSTRUCTOR(AllocationSite, HeapObjectRef)

526 527 528 529 530 531 532 533 534 535 536 537
  Handle<AllocationSite> object() const;

  bool PointsToLiteral() const;
  AllocationType GetAllocationType() const;
  ObjectRef nested_site() const;

  // {IsFastLiteral} determines whether the given array or object literal
  // boilerplate satisfies all limits to be considered for fast deep-copying
  // and computes the total size of all objects that are part of the graph.
  //
  // If PointsToLiteral() is false, then IsFastLiteral() is also false.
  bool IsFastLiteral() const;
538 539 540

  void SerializeBoilerplate();

541 542 543 544 545 546 547
  // We only serialize boilerplate if IsFastLiteral is true.
  base::Optional<JSObjectRef> boilerplate() const;

  ElementsKind GetElementsKind() const;
  bool CanInlineCall() const;
};

548 549
class BigIntRef : public HeapObjectRef {
 public:
550 551
  DEFINE_REF_CONSTRUCTOR(BigInt, HeapObjectRef)

552 553 554 555 556
  Handle<BigInt> object() const;

  uint64_t AsUint64() const;
};

557 558
class V8_EXPORT_PRIVATE MapRef : public HeapObjectRef {
 public:
559 560
  DEFINE_REF_CONSTRUCTOR(Map, HeapObjectRef)

561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590
  Handle<Map> object() const;

  int instance_size() const;
  InstanceType instance_type() const;
  int GetInObjectProperties() const;
  int GetInObjectPropertiesStartInWords() const;
  int NumberOfOwnDescriptors() const;
  int GetInObjectPropertyOffset(int index) const;
  int constructor_function_index() const;
  int NextFreePropertyIndex() const;
  int UnusedPropertyFields() const;
  ElementsKind elements_kind() const;
  bool is_stable() const;
  bool is_extensible() const;
  bool is_constructor() const;
  bool has_prototype_slot() const;
  bool is_access_check_needed() const;
  bool is_deprecated() const;
  bool CanBeDeprecated() const;
  bool CanTransition() const;
  bool IsInobjectSlackTrackingInProgress() const;
  bool is_dictionary_map() const;
  bool IsFixedCowArrayMap() const;
  bool IsPrimitiveMap() const;
  bool is_undetectable() const;
  bool is_callable() const;
  bool has_indexed_interceptor() const;
  bool is_migration_target() const;
  bool supports_fast_array_iteration() const;
  bool supports_fast_array_resize() const;
591
  bool is_abandoned_prototype_map() const;
592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613

  OddballType oddball_type() const;

#define DEF_TESTER(Type, ...) bool Is##Type##Map() const;
  INSTANCE_TYPE_CHECKERS(DEF_TESTER)
#undef DEF_TESTER

  void SerializeBackPointer();
  HeapObjectRef GetBackPointer() const;

  void SerializePrototype();
  bool serialized_prototype() const;
  HeapObjectRef prototype() const;

  void SerializeForElementLoad();

  void SerializeForElementStore();
  bool HasOnlyStablePrototypesWithFastElements(
      ZoneVector<MapRef>* prototype_maps);

  // Concerning the underlying instance_descriptors:
  void SerializeOwnDescriptors();
614 615 616 617 618 619 620 621
  void SerializeOwnDescriptor(InternalIndex descriptor_index);
  bool serialized_own_descriptor(InternalIndex descriptor_index) const;
  MapRef FindFieldOwner(InternalIndex descriptor_index) const;
  PropertyDetails GetPropertyDetails(InternalIndex descriptor_index) const;
  NameRef GetPropertyKey(InternalIndex descriptor_index) const;
  FieldIndex GetFieldIndexFor(InternalIndex descriptor_index) const;
  ObjectRef GetFieldType(InternalIndex descriptor_index) const;
  bool IsUnboxedDoubleField(InternalIndex descriptor_index) const;
622 623
  base::Optional<ObjectRef> GetStrongValue(
      InternalIndex descriptor_number) const;
624

625 626 627
  void SerializeRootMap();
  base::Optional<MapRef> FindRootMap() const;

628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644
  // Available after calling JSFunctionRef::Serialize on a function that has
  // this map as initial map.
  ObjectRef GetConstructor() const;
  base::Optional<MapRef> AsElementsKind(ElementsKind kind) const;
};

struct HolderLookupResult {
  HolderLookupResult(CallOptimization::HolderLookup lookup_ =
                         CallOptimization::kHolderNotFound,
                     base::Optional<JSObjectRef> holder_ = base::nullopt)
      : lookup(lookup_), holder(holder_) {}
  CallOptimization::HolderLookup lookup;
  base::Optional<JSObjectRef> holder;
};

class FunctionTemplateInfoRef : public HeapObjectRef {
 public:
645 646
  DEFINE_REF_CONSTRUCTOR(FunctionTemplateInfo, HeapObjectRef)

647 648 649 650 651 652 653 654 655
  Handle<FunctionTemplateInfo> object() const;

  bool is_signature_undefined() const;
  bool accept_any_receiver() const;
  // The following returns true if the CallHandlerInfo is present.
  bool has_call_code() const;

  void SerializeCallCode();
  base::Optional<CallHandlerInfoRef> call_code() const;
656 657
  Address c_function() const;
  const CFunctionInfo* c_signature() const;
658

659 660 661
  HolderLookupResult LookupHolderOfExpectedType(
      MapRef receiver_map,
      SerializationPolicy policy = SerializationPolicy::kAssumeSerialized);
662 663 664 665
};

class FixedArrayBaseRef : public HeapObjectRef {
 public:
666 667
  DEFINE_REF_CONSTRUCTOR(FixedArrayBase, HeapObjectRef)

668 669 670 671 672
  Handle<FixedArrayBase> object() const;

  int length() const;
};

673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688
class ArrayBoilerplateDescriptionRef : public HeapObjectRef {
 public:
  using HeapObjectRef::HeapObjectRef;
  Handle<ArrayBoilerplateDescription> object() const;

  int constants_elements_length() const;
};

class ObjectBoilerplateDescriptionRef : public HeapObjectRef {
 public:
  using HeapObjectRef::HeapObjectRef;
  Handle<ObjectBoilerplateDescription> object() const;

  int size() const;
};

689 690
class FixedArrayRef : public FixedArrayBaseRef {
 public:
691 692
  DEFINE_REF_CONSTRUCTOR(FixedArray, FixedArrayBaseRef)

693 694 695 696 697 698 699
  Handle<FixedArray> object() const;

  ObjectRef get(int i) const;
};

class FixedDoubleArrayRef : public FixedArrayBaseRef {
 public:
700 701
  DEFINE_REF_CONSTRUCTOR(FixedDoubleArray, FixedArrayBaseRef)

702 703 704 705 706 707 708 709
  Handle<FixedDoubleArray> object() const;

  double get_scalar(int i) const;
  bool is_the_hole(int i) const;
};

class BytecodeArrayRef : public FixedArrayBaseRef {
 public:
710 711
  DEFINE_REF_CONSTRUCTOR(BytecodeArray, FixedArrayBaseRef)

712 713 714 715 716 717 718 719 720 721
  Handle<BytecodeArray> object() const;

  int register_count() const;
  int parameter_count() const;
  interpreter::Register incoming_new_target_or_generator_register() const;

  // Bytecode access methods.
  uint8_t get(int index) const;
  Address GetFirstBytecodeAddress() const;

722 723 724 725
  // Source position table.
  const byte* source_positions_address() const;
  int source_positions_size() const;

726 727 728 729 730
  // Constant pool access.
  Handle<Object> GetConstantAtIndex(int index) const;
  bool IsConstantAtIndexSmi(int index) const;
  Smi GetConstantAtIndexAsSmi(int index) const;

731 732 733 734
  // Exception handler table.
  Address handler_table_address() const;
  int handler_table_size() const;

735 736 737 738 739
  void SerializeForCompilation();
};

class JSArrayRef : public JSObjectRef {
 public:
740 741
  DEFINE_REF_CONSTRUCTOR(JSArray, JSObjectRef)

742 743 744 745 746 747
  Handle<JSArray> object() const;

  ObjectRef length() const;

  // Return the element at key {index} if the array has a copy-on-write elements
  // storage and {index} is known to be an own data property.
748 749 750
  base::Optional<ObjectRef> GetOwnCowElement(
      uint32_t index, SerializationPolicy policy =
                          SerializationPolicy::kAssumeSerialized) const;
751 752 753 754
};

class ScopeInfoRef : public HeapObjectRef {
 public:
755 756
  DEFINE_REF_CONSTRUCTOR(ScopeInfo, HeapObjectRef)

757 758 759
  Handle<ScopeInfo> object() const;

  int ContextLength() const;
760 761 762 763 764 765 766
  bool HasOuterScopeInfo() const;
  int Flags() const;
  bool HasContextExtension() const;

  // Only serialized via SerializeScopeInfoChain.
  ScopeInfoRef OuterScopeInfo() const;
  void SerializeScopeInfoChain();
767 768
};

769 770 771 772 773 774 775 776 777 778 779 780 781 782 783
#define BROKER_SFI_FIELDS(V)                             \
  V(int, internal_formal_parameter_count)                \
  V(bool, has_duplicate_parameters)                      \
  V(int, function_map_index)                             \
  V(FunctionKind, kind)                                  \
  V(LanguageMode, language_mode)                         \
  V(bool, native)                                        \
  V(bool, HasBreakInfo)                                  \
  V(bool, HasBuiltinId)                                  \
  V(bool, construct_as_builtin)                          \
  V(bool, HasBytecodeArray)                              \
  V(bool, is_safe_to_skip_arguments_adaptor)             \
  V(SharedFunctionInfo::Inlineability, GetInlineability) \
  V(int, StartPosition)                                  \
  V(bool, is_compiled)                                   \
784
  V(bool, IsUserJavaScript)
785 786 787

class V8_EXPORT_PRIVATE SharedFunctionInfoRef : public HeapObjectRef {
 public:
788 789
  DEFINE_REF_CONSTRUCTOR(SharedFunctionInfo, HeapObjectRef)

790 791 792
  Handle<SharedFunctionInfo> object() const;

  int builtin_id() const;
793
  int context_header_size() const;
794 795 796 797 798 799
  BytecodeArrayRef GetBytecodeArray() const;

#define DECL_ACCESSOR(type, name) type name() const;
  BROKER_SFI_FIELDS(DECL_ACCESSOR)
#undef DECL_ACCESSOR

800 801 802 803
  bool IsInlineable() const {
    return GetInlineability() == SharedFunctionInfo::kIsInlineable;
  }

804 805 806
  // Template objects may not be created at compilation time. This method
  // wraps the retrieval of the template object and creates it if
  // necessary.
807
  JSArrayRef GetTemplateObject(
808
      TemplateObjectDescriptionRef description, FeedbackSource const& source,
809
      SerializationPolicy policy = SerializationPolicy::kAssumeSerialized);
810

811 812
  void SerializeFunctionTemplateInfo();
  base::Optional<FunctionTemplateInfoRef> function_template_info() const;
813 814 815

  void SerializeScopeInfoChain();
  ScopeInfoRef scope_info() const;
816 817 818 819
};

class StringRef : public NameRef {
 public:
820 821
  DEFINE_REF_CONSTRUCTOR(String, NameRef)

822 823 824 825 826 827 828 829 830 831 832
  Handle<String> object() const;

  int length() const;
  uint16_t GetFirstChar();
  base::Optional<double> ToNumber();
  bool IsSeqString() const;
  bool IsExternalString() const;
};

class SymbolRef : public NameRef {
 public:
833 834
  DEFINE_REF_CONSTRUCTOR(Symbol, NameRef)

835 836 837 838 839
  Handle<Symbol> object() const;
};

class JSTypedArrayRef : public JSObjectRef {
 public:
840 841
  DEFINE_REF_CONSTRUCTOR(JSTypedArray, JSObjectRef)

842 843 844 845
  Handle<JSTypedArray> object() const;

  bool is_on_heap() const;
  size_t length() const;
846
  void* data_ptr() const;
847 848 849 850 851 852 853 854 855

  void Serialize();
  bool serialized() const;

  HeapObjectRef buffer() const;
};

class SourceTextModuleRef : public HeapObjectRef {
 public:
856 857
  DEFINE_REF_CONSTRUCTOR(SourceTextModule, HeapObjectRef)

858 859 860 861
  Handle<SourceTextModule> object() const;

  void Serialize();

862
  base::Optional<CellRef> GetCell(int cell_index) const;
863 864
};

865 866 867 868 869 870 871
class TemplateObjectDescriptionRef : public HeapObjectRef {
 public:
  DEFINE_REF_CONSTRUCTOR(TemplateObjectDescription, HeapObjectRef)

  Handle<TemplateObjectDescription> object() const;
};

872 873
class CellRef : public HeapObjectRef {
 public:
874 875
  DEFINE_REF_CONSTRUCTOR(Cell, HeapObjectRef)

876 877 878 879 880
  Handle<Cell> object() const;

  ObjectRef value() const;
};

881 882 883 884 885
class JSGlobalObjectRef : public JSObjectRef {
 public:
  DEFINE_REF_CONSTRUCTOR(JSGlobalObject, JSObjectRef)

  Handle<JSGlobalObject> object() const;
886 887

  bool IsDetached() const;
888 889 890 891 892 893 894 895

  // If {serialize} is false:
  //   If the property is known to exist as a property cell (on the global
  //   object), return that property cell. Otherwise (not known to exist as a
  //   property cell or known not to exist as a property cell) return nothing.
  // If {serialize} is true:
  //   Like above but potentially access the heap and serialize the necessary
  //   information.
896 897 898
  base::Optional<PropertyCellRef> GetPropertyCell(
      NameRef const& name, SerializationPolicy policy =
                               SerializationPolicy::kAssumeSerialized) const;
899 900
};

901 902 903 904 905 906 907
class JSGlobalProxyRef : public JSObjectRef {
 public:
  DEFINE_REF_CONSTRUCTOR(JSGlobalProxy, JSObjectRef)

  Handle<JSGlobalProxy> object() const;
};

908 909
class CodeRef : public HeapObjectRef {
 public:
910 911
  DEFINE_REF_CONSTRUCTOR(Code, HeapObjectRef)

912
  Handle<Code> object() const;
913 914

  unsigned inlined_bytecode_size() const;
915 916 917 918
};

class InternalizedStringRef : public StringRef {
 public:
919 920
  DEFINE_REF_CONSTRUCTOR(InternalizedString, StringRef)

921 922
  Handle<InternalizedString> object() const;
};
923

924 925
#undef DEFINE_REF_CONSTRUCTOR

926 927 928 929 930
}  // namespace compiler
}  // namespace internal
}  // namespace v8

#endif  // V8_COMPILER_HEAP_REFS_H_