1 // Copyright 2012 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_IC_STUB_CACHE_H_
6 #define V8_IC_STUB_CACHE_H_
7 
8 #include "src/objects/name.h"
9 #include "src/objects/tagged-value.h"
10 
11 namespace v8 {
12 namespace internal {
13 
14 // The stub cache is used for megamorphic property accesses.
15 // It maps (map, name, type) to property access handlers. The cache does not
16 // need explicit invalidation when a prototype chain is modified, since the
17 // handlers verify the chain.
18 
19 
20 class SCTableReference {
21  public:
address()22   Address address() const { return address_; }
23 
24  private:
SCTableReference(Address address)25   explicit SCTableReference(Address address) : address_(address) {}
26 
27   Address address_;
28 
29   friend class StubCache;
30 };
31 
32 class V8_EXPORT_PRIVATE StubCache {
33  public:
34   struct Entry {
35     // {key} is a tagged Name pointer, may be cleared by setting to empty
36     // string.
37     StrongTaggedValue key;
38     // {value} is a tagged heap object reference (weak or strong), equivalent
39     // to a MaybeObject's payload.
40     TaggedValue value;
41     // {map} is a tagged Map pointer, may be cleared by setting to Smi::zero().
42     StrongTaggedValue map;
43   };
44 
45   void Initialize();
46   // Access cache for entry hash(name, map).
47   void Set(Name name, Map map, MaybeObject handler);
48   MaybeObject Get(Name name, Map map);
49   // Clear the lookup table (@ mark compact collection).
50   void Clear();
51 
52   enum Table { kPrimary, kSecondary };
53 
key_reference(StubCache::Table table)54   SCTableReference key_reference(StubCache::Table table) {
55     return SCTableReference(
56         reinterpret_cast<Address>(&first_entry(table)->key));
57   }
58 
map_reference(StubCache::Table table)59   SCTableReference map_reference(StubCache::Table table) {
60     return SCTableReference(
61         reinterpret_cast<Address>(&first_entry(table)->map));
62   }
63 
value_reference(StubCache::Table table)64   SCTableReference value_reference(StubCache::Table table) {
65     return SCTableReference(
66         reinterpret_cast<Address>(&first_entry(table)->value));
67   }
68 
first_entry(StubCache::Table table)69   StubCache::Entry* first_entry(StubCache::Table table) {
70     switch (table) {
71       case StubCache::kPrimary:
72         return StubCache::primary_;
73       case StubCache::kSecondary:
74         return StubCache::secondary_;
75     }
76     UNREACHABLE();
77   }
78 
isolate()79   Isolate* isolate() { return isolate_; }
80 
81   // Setting kCacheIndexShift to Name::kHashShift is convenient because it
82   // causes the bit field inside the hash field to get shifted out implicitly.
83   // Note that kCacheIndexShift must not get too large, because
84   // sizeof(Entry) needs to be a multiple of 1 << kCacheIndexShift (see
85   // the STATIC_ASSERT below, in {entry(...)}).
86   static const int kCacheIndexShift = Name::kHashShift;
87 
88   static const int kPrimaryTableBits = 11;
89   static const int kPrimaryTableSize = (1 << kPrimaryTableBits);
90   static const int kSecondaryTableBits = 9;
91   static const int kSecondaryTableSize = (1 << kSecondaryTableBits);
92 
93   // We compute the hash code for a map as follows:
94   //   <code> = <address> ^ (<address> >> kMapKeyShift)
95   static const int kMapKeyShift = kPrimaryTableBits + kCacheIndexShift;
96 
97   // Some magic number used in the secondary hash computation.
98   static const int kSecondaryMagic = 0xb16ca6e5;
99 
100   static int PrimaryOffsetForTesting(Name name, Map map);
101   static int SecondaryOffsetForTesting(Name name, int seed);
102 
103   // The constructor is made public only for the purposes of testing.
104   explicit StubCache(Isolate* isolate);
105   StubCache(const StubCache&) = delete;
106   StubCache& operator=(const StubCache&) = delete;
107 
108  private:
109   // The stub cache has a primary and secondary level.  The two levels have
110   // different hashing algorithms in order to avoid simultaneous collisions
111   // in both caches.  Unlike a probing strategy (quadratic or otherwise) the
112   // update strategy on updates is fairly clear and simple:  Any existing entry
113   // in the primary cache is moved to the secondary cache, and secondary cache
114   // entries are overwritten.
115 
116   // Hash algorithm for the primary table.  This algorithm is replicated in
117   // assembler for every architecture.  Returns an index into the table that
118   // is scaled by 1 << kCacheIndexShift.
119   static int PrimaryOffset(Name name, Map map);
120 
121   // Hash algorithm for the secondary table.  This algorithm is replicated in
122   // assembler for every architecture.  Returns an index into the table that
123   // is scaled by 1 << kCacheIndexShift.
124   static int SecondaryOffset(Name name, int seed);
125 
126   // Compute the entry for a given offset in exactly the same way as
127   // we do in generated code.  We generate an hash code that already
128   // ends in Name::kHashShift 0s.  Then we multiply it so it is a multiple
129   // of sizeof(Entry).  This makes it easier to avoid making mistakes
130   // in the hashed offset computations.
entry(Entry * table,int offset)131   static Entry* entry(Entry* table, int offset) {
132     // The size of {Entry} must be a multiple of 1 << kCacheIndexShift.
133     STATIC_ASSERT((sizeof(*table) >> kCacheIndexShift) << kCacheIndexShift ==
134                   sizeof(*table));
135     const int multiplier = sizeof(*table) >> kCacheIndexShift;
136     return reinterpret_cast<Entry*>(reinterpret_cast<Address>(table) +
137                                     offset * multiplier);
138   }
139 
140  private:
141   Entry primary_[kPrimaryTableSize];
142   Entry secondary_[kSecondaryTableSize];
143   Isolate* isolate_;
144 
145   friend class Isolate;
146   friend class SCTableReference;
147 };
148 }  // namespace internal
149 }  // namespace v8
150 
151 #endif  // V8_IC_STUB_CACHE_H_
152