context-slot-cache.cc 3.18 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
// Copyright 2016 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.

#include "src/ast/context-slot-cache.h"

#include <stdlib.h>

#include "src/ast/scopes.h"
#include "src/bootstrapper.h"
11 12 13 14
// FIXME(mstarzinger, marja): This is weird, but required because of the missing
// (disallowed) include: src/factory.h -> src/objects-inl.h
#include "src/objects-inl.h"
// FIXME(mstarzinger, marja): This is weird, but required because of the missing
15 16 17
// (disallowed) include: src/feedback-vector.h ->
// src/feedback-vector-inl.h
#include "src/feedback-vector-inl.h"
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32

namespace v8 {
namespace internal {

int ContextSlotCache::Hash(Object* data, String* name) {
  // Uses only lower 32 bits if pointers are larger.
  uintptr_t addr_hash =
      static_cast<uint32_t>(reinterpret_cast<uintptr_t>(data)) >> 2;
  return static_cast<int>((addr_hash ^ name->Hash()) % kLength);
}

int ContextSlotCache::Lookup(Object* data, String* name, VariableMode* mode,
                             InitializationFlag* init_flag,
                             MaybeAssignedFlag* maybe_assigned_flag) {
  int index = Hash(data, name);
33
  DCHECK(name->IsInternalizedString());
34
  Key& key = keys_[index];
35
  if (key.data == data && key.name == name) {
36 37 38 39 40 41 42 43 44 45 46 47 48 49
    Value result(values_[index]);
    if (mode != nullptr) *mode = result.mode();
    if (init_flag != nullptr) *init_flag = result.initialization_flag();
    if (maybe_assigned_flag != nullptr)
      *maybe_assigned_flag = result.maybe_assigned_flag();
    return result.index() + kNotFound;
  }
  return kNotFound;
}

void ContextSlotCache::Update(Handle<Object> data, Handle<String> name,
                              VariableMode mode, InitializationFlag init_flag,
                              MaybeAssignedFlag maybe_assigned_flag,
                              int slot_index) {
50 51 52 53 54 55 56 57 58
  DCHECK(name->IsInternalizedString());
  DCHECK_LT(kNotFound, slot_index);
  int index = Hash(*data, *name);
  Key& key = keys_[index];
  key.data = *data;
  key.name = *name;
  // Please note value only takes a uint as index.
  values_[index] =
      Value(mode, init_flag, maybe_assigned_flag, slot_index - kNotFound).raw();
59
#ifdef DEBUG
60
  ValidateEntry(data, name, mode, init_flag, maybe_assigned_flag, slot_index);
61 62 63 64 65 66 67 68 69 70 71 72 73 74
#endif
}

void ContextSlotCache::Clear() {
  for (int index = 0; index < kLength; index++) keys_[index].data = nullptr;
}

#ifdef DEBUG

void ContextSlotCache::ValidateEntry(Handle<Object> data, Handle<String> name,
                                     VariableMode mode,
                                     InitializationFlag init_flag,
                                     MaybeAssignedFlag maybe_assigned_flag,
                                     int slot_index) {
75 76 77 78 79 80 81 82 83 84
  DCHECK(name->IsInternalizedString());
  int index = Hash(*data, *name);
  Key& key = keys_[index];
  DCHECK_EQ(key.data, *data);
  DCHECK_EQ(key.name, *name);
  Value result(values_[index]);
  DCHECK_EQ(result.mode(), mode);
  DCHECK_EQ(result.initialization_flag(), init_flag);
  DCHECK_EQ(result.maybe_assigned_flag(), maybe_assigned_flag);
  DCHECK_EQ(result.index() + kNotFound, slot_index);
85 86 87 88 89 90
}

#endif  // DEBUG

}  // namespace internal
}  // namespace v8