• Home
  • History
  • Annotate
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1  // Copyright 2016 the V8 project authors. All rights reserved.
2  // Use of this source code is governed by a BSD-style license that can be
3  // found in the LICENSE file.
4  
5  #ifndef V8_AST_CONTEXT_SLOT_CACHE_H_
6  #define V8_AST_CONTEXT_SLOT_CACHE_H_
7  
8  #include "src/allocation.h"
9  #include "src/ast/modules.h"
10  
11  namespace v8 {
12  namespace internal {
13  
14  // Cache for mapping (data, property name) into context slot index.
15  // The cache contains both positive and negative results.
16  // Slot index equals -1 means the property is absent.
17  // Cleared at startup and prior to mark sweep collection.
18  class ContextSlotCache {
19   public:
20    // Lookup context slot index for (data, name).
21    // If absent, kNotFound is returned.
22    int Lookup(Object* data, String* name, VariableMode* mode,
23               InitializationFlag* init_flag,
24               MaybeAssignedFlag* maybe_assigned_flag);
25  
26    // Update an element in the cache.
27    void Update(Handle<Object> data, Handle<String> name, VariableMode mode,
28                InitializationFlag init_flag,
29                MaybeAssignedFlag maybe_assigned_flag, int slot_index);
30  
31    // Clear the cache.
32    void Clear();
33  
34    static const int kNotFound = -2;
35  
36   private:
ContextSlotCache()37    ContextSlotCache() {
38      for (int i = 0; i < kLength; ++i) {
39        keys_[i].data = NULL;
40        keys_[i].name = NULL;
41        values_[i] = kNotFound;
42      }
43    }
44  
45    inline static int Hash(Object* data, String* name);
46  
47  #ifdef DEBUG
48    void ValidateEntry(Handle<Object> data, Handle<String> name,
49                       VariableMode mode, InitializationFlag init_flag,
50                       MaybeAssignedFlag maybe_assigned_flag, int slot_index);
51  #endif
52  
53    static const int kLength = 256;
54    struct Key {
55      Object* data;
56      String* name;
57    };
58  
59    struct Value {
ValueValue60      Value(VariableMode mode, InitializationFlag init_flag,
61            MaybeAssignedFlag maybe_assigned_flag, int index) {
62        DCHECK(ModeField::is_valid(mode));
63        DCHECK(InitField::is_valid(init_flag));
64        DCHECK(MaybeAssignedField::is_valid(maybe_assigned_flag));
65        DCHECK(IndexField::is_valid(index));
66        value_ = ModeField::encode(mode) | IndexField::encode(index) |
67                 InitField::encode(init_flag) |
68                 MaybeAssignedField::encode(maybe_assigned_flag);
69        DCHECK(mode == this->mode());
70        DCHECK(init_flag == this->initialization_flag());
71        DCHECK(maybe_assigned_flag == this->maybe_assigned_flag());
72        DCHECK(index == this->index());
73      }
74  
ValueValue75      explicit inline Value(uint32_t value) : value_(value) {}
76  
rawValue77      uint32_t raw() { return value_; }
78  
modeValue79      VariableMode mode() { return ModeField::decode(value_); }
80  
initialization_flagValue81      InitializationFlag initialization_flag() {
82        return InitField::decode(value_);
83      }
84  
maybe_assigned_flagValue85      MaybeAssignedFlag maybe_assigned_flag() {
86        return MaybeAssignedField::decode(value_);
87      }
88  
indexValue89      int index() { return IndexField::decode(value_); }
90  
91      // Bit fields in value_ (type, shift, size). Must be public so the
92      // constants can be embedded in generated code.
93      class ModeField : public BitField<VariableMode, 0, 4> {};
94      class InitField : public BitField<InitializationFlag, 4, 1> {};
95      class MaybeAssignedField : public BitField<MaybeAssignedFlag, 5, 1> {};
96      class IndexField : public BitField<int, 6, 32 - 6> {};
97  
98     private:
99      uint32_t value_;
100    };
101  
102    Key keys_[kLength];
103    uint32_t values_[kLength];
104  
105    friend class Isolate;
106    DISALLOW_COPY_AND_ASSIGN(ContextSlotCache);
107  };
108  
109  }  // namespace internal
110  }  // namespace v8
111  
112  #endif  // V8_AST_CONTEXT_SLOT_CACHE_H_
113