%PDF- %PDF-
Mini Shell

Mini Shell

Direktori : /home/vacivi36/vittasync.vacivitta.com.br/vittasync/node/deps/v8/src/objects/
Upload File :
Create Path :
Current File : /home/vacivi36/vittasync.vacivitta.com.br/vittasync/node/deps/v8/src/objects/feedback-vector-inl.h

// Copyright 2012 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_OBJECTS_FEEDBACK_VECTOR_INL_H_
#define V8_OBJECTS_FEEDBACK_VECTOR_INL_H_

#include "src/common/globals.h"
#include "src/heap/heap-write-barrier-inl.h"
#include "src/objects/code-inl.h"
#include "src/objects/feedback-cell-inl.h"
#include "src/objects/feedback-vector.h"
#include "src/objects/maybe-object-inl.h"
#include "src/objects/shared-function-info.h"
#include "src/objects/smi.h"
#include "src/objects/tagged.h"
#include "src/roots/roots-inl.h"
#include "src/torque/runtime-macro-shims.h"

// Has to be the last include (doesn't have include guards):
#include "src/objects/object-macros.h"

namespace v8 {
namespace internal {

#include "torque-generated/src/objects/feedback-vector-tq-inl.inc"

TQ_OBJECT_CONSTRUCTORS_IMPL(FeedbackVector)
OBJECT_CONSTRUCTORS_IMPL(FeedbackMetadata, HeapObject)
OBJECT_CONSTRUCTORS_IMPL(ClosureFeedbackCellArray, FixedArray)

NEVER_READ_ONLY_SPACE_IMPL(FeedbackVector)
NEVER_READ_ONLY_SPACE_IMPL(ClosureFeedbackCellArray)

CAST_ACCESSOR(FeedbackMetadata)
CAST_ACCESSOR(ClosureFeedbackCellArray)

INT32_ACCESSORS(FeedbackMetadata, slot_count, kSlotCountOffset)

INT32_ACCESSORS(FeedbackMetadata, create_closure_slot_count,
                kCreateClosureSlotCountOffset)

int32_t FeedbackMetadata::slot_count(AcquireLoadTag) const {
  return ACQUIRE_READ_INT32_FIELD(*this, kSlotCountOffset);
}

int32_t FeedbackMetadata::get(int index) const {
  CHECK_LT(static_cast<unsigned>(index), static_cast<unsigned>(length()));
  int offset = kHeaderSize + index * kInt32Size;
  return ReadField<int32_t>(offset);
}

void FeedbackMetadata::set(int index, int32_t value) {
  DCHECK_LT(static_cast<unsigned>(index), static_cast<unsigned>(length()));
  int offset = kHeaderSize + index * kInt32Size;
  WriteField<int32_t>(offset, value);
}

bool FeedbackMetadata::is_empty() const { return slot_count() == 0; }

int FeedbackMetadata::length() const {
  return FeedbackMetadata::length(slot_count());
}

int FeedbackMetadata::GetSlotSize(FeedbackSlotKind kind) {
  switch (kind) {
    case FeedbackSlotKind::kForIn:
    case FeedbackSlotKind::kInstanceOf:
    case FeedbackSlotKind::kCompareOp:
    case FeedbackSlotKind::kBinaryOp:
    case FeedbackSlotKind::kLiteral:
    case FeedbackSlotKind::kJumpLoop:
      return 1;

    case FeedbackSlotKind::kCall:
    case FeedbackSlotKind::kCloneObject:
    case FeedbackSlotKind::kLoadProperty:
    case FeedbackSlotKind::kLoadGlobalInsideTypeof:
    case FeedbackSlotKind::kLoadGlobalNotInsideTypeof:
    case FeedbackSlotKind::kLoadKeyed:
    case FeedbackSlotKind::kHasKeyed:
    case FeedbackSlotKind::kSetNamedSloppy:
    case FeedbackSlotKind::kSetNamedStrict:
    case FeedbackSlotKind::kDefineNamedOwn:
    case FeedbackSlotKind::kDefineKeyedOwn:
    case FeedbackSlotKind::kStoreGlobalSloppy:
    case FeedbackSlotKind::kStoreGlobalStrict:
    case FeedbackSlotKind::kSetKeyedSloppy:
    case FeedbackSlotKind::kSetKeyedStrict:
    case FeedbackSlotKind::kStoreInArrayLiteral:
    case FeedbackSlotKind::kDefineKeyedOwnPropertyInLiteral:
      return 2;

    case FeedbackSlotKind::kInvalid:
      UNREACHABLE();
  }
}

Handle<FeedbackCell> ClosureFeedbackCellArray::GetFeedbackCell(int index) {
  return handle(FeedbackCell::cast(get(index)), GetIsolate());
}

Tagged<FeedbackCell> ClosureFeedbackCellArray::cell(int index) {
  return FeedbackCell::cast(get(index));
}

bool FeedbackVector::is_empty() const { return length() == 0; }

DEF_GETTER(FeedbackVector, metadata, Tagged<FeedbackMetadata>) {
  return shared_function_info(cage_base)->feedback_metadata(cage_base);
}

DEF_ACQUIRE_GETTER(FeedbackVector, metadata, Tagged<FeedbackMetadata>) {
  return shared_function_info(cage_base)->feedback_metadata(cage_base,
                                                            kAcquireLoad);
}

RELAXED_INT32_ACCESSORS(FeedbackVector, invocation_count,
                        kInvocationCountOffset)

void FeedbackVector::clear_invocation_count(RelaxedStoreTag tag) {
  set_invocation_count(0, tag);
}

int FeedbackVector::osr_urgency() const {
  return OsrUrgencyBits::decode(osr_state());
}

void FeedbackVector::set_osr_urgency(int urgency) {
  DCHECK(0 <= urgency && urgency <= FeedbackVector::kMaxOsrUrgency);
  static_assert(FeedbackVector::kMaxOsrUrgency <= OsrUrgencyBits::kMax);
  set_osr_state(OsrUrgencyBits::update(osr_state(), urgency));
}

void FeedbackVector::reset_osr_urgency() { set_osr_urgency(0); }

void FeedbackVector::RequestOsrAtNextOpportunity() {
  set_osr_urgency(kMaxOsrUrgency);
}

void FeedbackVector::reset_osr_state() { set_osr_state(0); }

bool FeedbackVector::maybe_has_optimized_osr_code() const {
  return maybe_has_maglev_osr_code() || maybe_has_turbofan_osr_code();
}

bool FeedbackVector::maybe_has_maglev_osr_code() const {
  return MaybeHasMaglevOsrCodeBit::decode(osr_state());
}

bool FeedbackVector::maybe_has_turbofan_osr_code() const {
  return MaybeHasTurbofanOsrCodeBit::decode(osr_state());
}

void FeedbackVector::set_maybe_has_optimized_osr_code(bool value,
                                                      CodeKind code_kind) {
  if (code_kind == CodeKind::MAGLEV) {
    CHECK(v8_flags.maglev_osr);
    set_osr_state(MaybeHasMaglevOsrCodeBit::update(osr_state(), value));
  } else {
    CHECK_EQ(code_kind, CodeKind::TURBOFAN);
    set_osr_state(MaybeHasTurbofanOsrCodeBit::update(osr_state(), value));
  }
}

Tagged<Code> FeedbackVector::optimized_code() const {
  MaybeObject slot = maybe_optimized_code();
  DCHECK(slot->IsWeakOrCleared());
  Tagged<HeapObject> heap_object;
  Tagged<Code> code;
  if (slot.GetHeapObject(&heap_object)) {
    code = Code::cast(heap_object);
  }
  // It is possible that the maybe_optimized_code slot is cleared but the flags
  // haven't been updated yet. We update them when we execute the function next
  // time / when we create new closure.
  DCHECK_IMPLIES(!code.is_null(),
                 maybe_has_maglev_code() || maybe_has_turbofan_code());
  DCHECK_IMPLIES(!code.is_null() && code->is_maglevved(),
                 maybe_has_maglev_code());
  DCHECK_IMPLIES(!code.is_null() && code->is_turbofanned(),
                 maybe_has_turbofan_code());
  return code;
}

TieringState FeedbackVector::tiering_state() const {
  return TieringStateBits::decode(flags());
}

bool FeedbackVector::has_optimized_code() const {
  DCHECK_IMPLIES(!optimized_code().is_null(),
                 maybe_has_maglev_code() || maybe_has_turbofan_code());
  return !optimized_code().is_null();
}

bool FeedbackVector::maybe_has_maglev_code() const {
  return MaybeHasMaglevCodeBit::decode(flags());
}

void FeedbackVector::set_maybe_has_maglev_code(bool value) {
  set_flags(MaybeHasMaglevCodeBit::update(flags(), value));
}

bool FeedbackVector::maybe_has_turbofan_code() const {
  return MaybeHasTurbofanCodeBit::decode(flags());
}

void FeedbackVector::set_maybe_has_turbofan_code(bool value) {
  set_flags(MaybeHasTurbofanCodeBit::update(flags(), value));
}

bool FeedbackVector::log_next_execution() const {
  return LogNextExecutionBit::decode(flags());
}

void FeedbackVector::set_log_next_execution(bool value) {
  set_flags(LogNextExecutionBit::update(flags(), value));
}

base::Optional<Tagged<Code>> FeedbackVector::GetOptimizedOsrCode(
    Isolate* isolate, FeedbackSlot slot) {
  MaybeObject maybe_code = Get(isolate, slot);
  if (maybe_code->IsCleared()) return {};

  Tagged<Code> code = Code::cast(maybe_code.GetHeapObject());
  if (code->marked_for_deoptimization()) {
    // Clear the cached Code object if deoptimized.
    // TODO(jgruber): Add tracing.
    Set(slot, HeapObjectReference::ClearedValue(isolate));
    return {};
  }

  return code;
}

// Conversion from an integer index to either a slot or an ic slot.
// static
FeedbackSlot FeedbackVector::ToSlot(intptr_t index) {
  DCHECK_LE(static_cast<uintptr_t>(index),
            static_cast<uintptr_t>(std::numeric_limits<int>::max()));
  return FeedbackSlot(static_cast<int>(index));
}

#ifdef DEBUG
// Instead of FixedArray, the Feedback and the Extra should contain
// WeakFixedArrays. The only allowed FixedArray subtype is HashTable.
bool FeedbackVector::IsOfLegacyType(MaybeObject value) {
  Tagged<HeapObject> heap_object;
  if (value.GetHeapObject(&heap_object)) {
    return IsFixedArray(heap_object) && !IsHashTable(heap_object);
  }
  return false;
}
#endif  // DEBUG

MaybeObject FeedbackVector::Get(FeedbackSlot slot) const {
  MaybeObject value = raw_feedback_slots(GetIndex(slot), kRelaxedLoad);
  DCHECK(!IsOfLegacyType(value));
  return value;
}

MaybeObject FeedbackVector::Get(PtrComprCageBase cage_base,
                                FeedbackSlot slot) const {
  MaybeObject value =
      raw_feedback_slots(cage_base, GetIndex(slot), kRelaxedLoad);
  DCHECK(!IsOfLegacyType(value));
  return value;
}

Handle<FeedbackCell> FeedbackVector::GetClosureFeedbackCell(int index) const {
  DCHECK_GE(index, 0);
  return closure_feedback_cell_array()->GetFeedbackCell(index);
}

Tagged<FeedbackCell> FeedbackVector::closure_feedback_cell(int index) const {
  DCHECK_GE(index, 0);
  return closure_feedback_cell_array()->cell(index);
}

MaybeObject FeedbackVector::SynchronizedGet(FeedbackSlot slot) const {
  const int i = slot.ToInt();
  DCHECK_LT(static_cast<unsigned>(i), static_cast<unsigned>(this->length()));
  const int offset = kRawFeedbackSlotsOffset + i * kTaggedSize;
  MaybeObject value = TaggedField<MaybeObject>::Acquire_Load(*this, offset);
  DCHECK(!IsOfLegacyType(value));
  return value;
}

void FeedbackVector::SynchronizedSet(FeedbackSlot slot, MaybeObject value,
                                     WriteBarrierMode mode) {
  DCHECK(!IsOfLegacyType(value));
  const int i = slot.ToInt();
  DCHECK_LT(static_cast<unsigned>(i), static_cast<unsigned>(this->length()));
  const int offset = kRawFeedbackSlotsOffset + i * kTaggedSize;
  TaggedField<MaybeObject>::Release_Store(*this, offset, value);
  CONDITIONAL_WEAK_WRITE_BARRIER(*this, offset, value, mode);
}

void FeedbackVector::SynchronizedSet(FeedbackSlot slot, Tagged<Object> value,
                                     WriteBarrierMode mode) {
  SynchronizedSet(slot, MaybeObject::FromObject(value), mode);
}

void FeedbackVector::Set(FeedbackSlot slot, MaybeObject value,
                         WriteBarrierMode mode) {
  DCHECK(!IsOfLegacyType(value));
  set_raw_feedback_slots(GetIndex(slot), value, mode);
}

void FeedbackVector::Set(FeedbackSlot slot, Tagged<Object> value,
                         WriteBarrierMode mode) {
  MaybeObject maybe_value = MaybeObject::FromObject(value);
  DCHECK(!IsOfLegacyType(maybe_value));
  set_raw_feedback_slots(GetIndex(slot), maybe_value, mode);
}

inline MaybeObjectSlot FeedbackVector::slots_start() {
  return RawMaybeWeakField(OffsetOfElementAt(0));
}

// Helper function to transform the feedback to BinaryOperationHint.
BinaryOperationHint BinaryOperationHintFromFeedback(int type_feedback) {
  switch (type_feedback) {
    case BinaryOperationFeedback::kNone:
      return BinaryOperationHint::kNone;
    case BinaryOperationFeedback::kSignedSmall:
      return BinaryOperationHint::kSignedSmall;
    case BinaryOperationFeedback::kSignedSmallInputs:
      return BinaryOperationHint::kSignedSmallInputs;
    case BinaryOperationFeedback::kNumber:
      return BinaryOperationHint::kNumber;
    case BinaryOperationFeedback::kNumberOrOddball:
      return BinaryOperationHint::kNumberOrOddball;
    case BinaryOperationFeedback::kString:
      return BinaryOperationHint::kString;
    case BinaryOperationFeedback::kBigInt:
      return BinaryOperationHint::kBigInt;
    case BinaryOperationFeedback::kBigInt64:
      return BinaryOperationHint::kBigInt64;
    default:
      return BinaryOperationHint::kAny;
  }
  UNREACHABLE();
}

// Helper function to transform the feedback to CompareOperationHint.
template <CompareOperationFeedback::Type Feedback>
bool Is(int type_feedback) {
  return !(type_feedback & ~Feedback);
}

CompareOperationHint CompareOperationHintFromFeedback(int type_feedback) {
  if (Is<CompareOperationFeedback::kNone>(type_feedback)) {
    return CompareOperationHint::kNone;
  }

  if (Is<CompareOperationFeedback::kSignedSmall>(type_feedback)) {
    return CompareOperationHint::kSignedSmall;
  } else if (Is<CompareOperationFeedback::kNumber>(type_feedback)) {
    return CompareOperationHint::kNumber;
  } else if (Is<CompareOperationFeedback::kNumberOrBoolean>(type_feedback)) {
    return CompareOperationHint::kNumberOrBoolean;
  }

  if (Is<CompareOperationFeedback::kInternalizedString>(type_feedback)) {
    return CompareOperationHint::kInternalizedString;
  } else if (Is<CompareOperationFeedback::kString>(type_feedback)) {
    return CompareOperationHint::kString;
  }

  if (Is<CompareOperationFeedback::kReceiver>(type_feedback)) {
    return CompareOperationHint::kReceiver;
  } else if (Is<CompareOperationFeedback::kReceiverOrNullOrUndefined>(
                 type_feedback)) {
    return CompareOperationHint::kReceiverOrNullOrUndefined;
  }

  if (Is<CompareOperationFeedback::kBigInt64>(type_feedback)) {
    return CompareOperationHint::kBigInt64;
  } else if (Is<CompareOperationFeedback::kBigInt>(type_feedback)) {
    return CompareOperationHint::kBigInt;
  }

  if (Is<CompareOperationFeedback::kSymbol>(type_feedback)) {
    return CompareOperationHint::kSymbol;
  }

  DCHECK(Is<CompareOperationFeedback::kAny>(type_feedback));
  return CompareOperationHint::kAny;
}

// Helper function to transform the feedback to ForInHint.
ForInHint ForInHintFromFeedback(ForInFeedback type_feedback) {
  switch (type_feedback) {
    case ForInFeedback::kNone:
      return ForInHint::kNone;
    case ForInFeedback::kEnumCacheKeys:
      return ForInHint::kEnumCacheKeys;
    case ForInFeedback::kEnumCacheKeysAndIndices:
      return ForInHint::kEnumCacheKeysAndIndices;
    default:
      return ForInHint::kAny;
  }
  UNREACHABLE();
}

Handle<Symbol> FeedbackVector::UninitializedSentinel(Isolate* isolate) {
  return ReadOnlyRoots(isolate).uninitialized_symbol_handle();
}

Handle<Symbol> FeedbackVector::MegamorphicSentinel(Isolate* isolate) {
  return ReadOnlyRoots(isolate).megamorphic_symbol_handle();
}

Handle<Symbol> FeedbackVector::MegaDOMSentinel(Isolate* isolate) {
  return ReadOnlyRoots(isolate).mega_dom_symbol_handle();
}

Tagged<Symbol> FeedbackVector::RawUninitializedSentinel(Isolate* isolate) {
  return ReadOnlyRoots(isolate).uninitialized_symbol();
}

bool FeedbackMetadataIterator::HasNext() const {
  return next_slot_.ToInt() < metadata()->slot_count();
}

FeedbackSlot FeedbackMetadataIterator::Next() {
  DCHECK(HasNext());
  cur_slot_ = next_slot_;
  slot_kind_ = metadata()->GetKind(cur_slot_);
  next_slot_ = FeedbackSlot(next_slot_.ToInt() + entry_size());
  return cur_slot_;
}

int FeedbackMetadataIterator::entry_size() const {
  return FeedbackMetadata::GetSlotSize(kind());
}

MaybeObject NexusConfig::GetFeedback(Tagged<FeedbackVector> vector,
                                     FeedbackSlot slot) const {
  return vector->SynchronizedGet(slot);
}

void NexusConfig::SetFeedback(Tagged<FeedbackVector> vector, FeedbackSlot slot,
                              MaybeObject feedback,
                              WriteBarrierMode mode) const {
  DCHECK(can_write());
  vector->SynchronizedSet(slot, feedback, mode);
}

MaybeObject FeedbackNexus::UninitializedSentinel() const {
  return MaybeObject::FromObject(
      *FeedbackVector::UninitializedSentinel(GetIsolate()));
}

MaybeObject FeedbackNexus::MegamorphicSentinel() const {
  return MaybeObject::FromObject(
      *FeedbackVector::MegamorphicSentinel(GetIsolate()));
}

MaybeObject FeedbackNexus::MegaDOMSentinel() const {
  return MaybeObject::FromObject(
      *FeedbackVector::MegaDOMSentinel(GetIsolate()));
}

MaybeObject FeedbackNexus::FromHandle(MaybeObjectHandle slot) const {
  return slot.is_null() ? HeapObjectReference::ClearedValue(config()->isolate())
                        : *slot;
}

MaybeObjectHandle FeedbackNexus::ToHandle(MaybeObject value) const {
  return value.IsCleared() ? MaybeObjectHandle()
                           : MaybeObjectHandle(config()->NewHandle(value));
}

MaybeObject FeedbackNexus::GetFeedback() const {
  auto pair = GetFeedbackPair();
  return pair.first;
}

MaybeObject FeedbackNexus::GetFeedbackExtra() const {
  auto pair = GetFeedbackPair();
  return pair.second;
}

std::pair<MaybeObject, MaybeObject> FeedbackNexus::GetFeedbackPair() const {
  if (config()->mode() == NexusConfig::BackgroundThread &&
      feedback_cache_.has_value()) {
    return std::make_pair(FromHandle(feedback_cache_->first),
                          FromHandle(feedback_cache_->second));
  }
  auto pair = FeedbackMetadata::GetSlotSize(kind()) == 2
                  ? config()->GetFeedbackPair(vector(), slot())
                  : std::make_pair(config()->GetFeedback(vector(), slot()),
                                   MaybeObject());
  if (config()->mode() == NexusConfig::BackgroundThread &&
      !feedback_cache_.has_value()) {
    feedback_cache_ =
        std::make_pair(ToHandle(pair.first), ToHandle(pair.second));
  }
  return pair;
}

template <typename T>
struct IsValidFeedbackType : public std::false_type {};

template <>
struct IsValidFeedbackType<MaybeObject> : public std::true_type {};
template <>
struct IsValidFeedbackType<HeapObjectReference> : public std::true_type {};
template <typename T>
struct IsValidFeedbackType<Tagged<T>> : public std::true_type {};

template <typename FeedbackType>
void FeedbackNexus::SetFeedback(FeedbackType feedback, WriteBarrierMode mode) {
  static_assert(IsValidFeedbackType<FeedbackType>(),
                "feedbacks need to be Smi, Object or MaybeObject");
  MaybeObject fmo = MaybeObject::Create(feedback);
  config()->SetFeedback(vector(), slot(), fmo, mode);
}

template <typename FeedbackType, typename FeedbackExtraType>
void FeedbackNexus::SetFeedback(FeedbackType feedback, WriteBarrierMode mode,
                                FeedbackExtraType feedback_extra,
                                WriteBarrierMode mode_extra) {
  static_assert(IsValidFeedbackType<FeedbackType>(),
                "feedbacks need to be Smi, Object or MaybeObject");
  static_assert(IsValidFeedbackType<FeedbackExtraType>(),
                "feedbacks need to be Smi, Object or MaybeObject");
  MaybeObject fmo = MaybeObject::Create(feedback);
  MaybeObject fmo_extra = MaybeObject::Create(feedback_extra);
  config()->SetFeedbackPair(vector(), slot(), fmo, mode, fmo_extra, mode_extra);
}

Isolate* FeedbackNexus::GetIsolate() const { return vector()->GetIsolate(); }
}  // namespace internal
}  // namespace v8

#include "src/objects/object-macros-undef.h"

#endif  // V8_OBJECTS_FEEDBACK_VECTOR_INL_H_

Zerion Mini Shell 1.0