Imported Upstream version 1.0.0
[platform/upstream/js.git] / js / src / jspropertycache.h
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2  * vim: set ts=8 sw=4 et tw=98:
3  *
4  * ***** BEGIN LICENSE BLOCK *****
5  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6  *
7  * The contents of this file are subject to the Mozilla Public License Version
8  * 1.1 (the "License"); you may not use this file except in compliance with
9  * the License. You may obtain a copy of the License at
10  * http://www.mozilla.org/MPL/
11  *
12  * Software distributed under the License is distributed on an "AS IS" basis,
13  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14  * for the specific language governing rights and limitations under the
15  * License.
16  *
17  * The Original Code is Mozilla Communicator client code, released
18  * March 31, 1998.
19  *
20  * The Initial Developer of the Original Code is
21  * Netscape Communications Corporation.
22  * Portions created by the Initial Developer are Copyright (C) 1998
23  * the Initial Developer. All Rights Reserved.
24  *
25  * Contributor(s):
26  *
27  * Alternatively, the contents of this file may be used under the terms of
28  * either the GNU General Public License Version 2 or later (the "GPL"), or
29  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
30  * in which case the provisions of the GPL or the LGPL are applicable instead
31  * of those above. If you wish to allow use of your version of this file only
32  * under the terms of either the GPL or the LGPL, and not to allow others to
33  * use your version of this file under the terms of the MPL, indicate your
34  * decision by deleting the provisions above and replace them with the notice
35  * and other provisions required by the GPL or the LGPL. If you do not delete
36  * the provisions above, a recipient may use your version of this file under
37  * the terms of any one of the MPL, the GPL or the LGPL.
38  *
39  * ***** END LICENSE BLOCK ***** */
40
41 #ifndef jspropertycache_h___
42 #define jspropertycache_h___
43
44 #include "jsapi.h"
45 #include "jsprvtd.h"
46 #include "jstypes.h"
47
48 namespace js {
49
50 /*
51  * Property cache with structurally typed capabilities for invalidation, for
52  * polymorphic callsite method/get/set speedups.  For details, see
53  * <https://developer.mozilla.org/en/SpiderMonkey/Internals/Property_cache>.
54  */
55
56 /* Property cache value capabilities. */
57 enum {
58     PCVCAP_PROTOBITS = 4,
59     PCVCAP_PROTOSIZE = JS_BIT(PCVCAP_PROTOBITS),
60     PCVCAP_PROTOMASK = JS_BITMASK(PCVCAP_PROTOBITS),
61
62     PCVCAP_SCOPEBITS = 4,
63     PCVCAP_SCOPESIZE = JS_BIT(PCVCAP_SCOPEBITS),
64     PCVCAP_SCOPEMASK = JS_BITMASK(PCVCAP_SCOPEBITS),
65
66     PCVCAP_TAGBITS = PCVCAP_PROTOBITS + PCVCAP_SCOPEBITS,
67     PCVCAP_TAGMASK = JS_BITMASK(PCVCAP_TAGBITS)
68 };
69
70 const uint32 SHAPE_OVERFLOW_BIT = JS_BIT(32 - PCVCAP_TAGBITS);
71
72 /*
73  * Property cache value. This is simply a tagged union:
74  *    PCVal = (JSObject * | uint32 | js::Shape *).
75  * It is the type of PropertyCacheEntry::vword and combines with the tag bits
76  * of PropertyCacheEntry::vcap to tell how to get or set the property, once a
77  * property cache hit is validated.
78  *
79  * PropertyCache::purge depends on the bit-pattern of a null PCVal being 0.
80  */
81 class PCVal
82 {
83   private:
84     enum {
85         OBJECT = 0,
86         SLOT = 1,
87         SHAPE = 2,
88         TAG = 3
89     };
90
91     jsuword v;
92
93   public:
94     bool isNull() const { return v == 0; }
95     void setNull() { v = 0; }
96
97     bool isFunObj() const { return (v & TAG) == OBJECT; }
98     JSObject &toFunObj() const {
99         JS_ASSERT(isFunObj());
100         return *reinterpret_cast<JSObject *>(v);
101     }
102     void setFunObj(JSObject &obj) {
103         v = reinterpret_cast<jsuword>(&obj);
104     }
105
106     bool isSlot() const { return v & SLOT; }
107     uint32 toSlot() const { JS_ASSERT(isSlot()); return uint32(v) >> 1; }
108     void setSlot(uint32 slot) { v = (jsuword(slot) << 1) | SLOT; }
109
110     bool isShape() const { return (v & TAG) == SHAPE; }
111     const js::Shape *toShape() const {
112         JS_ASSERT(isShape());
113         return reinterpret_cast<js::Shape *>(v & ~TAG);
114     }
115     void setShape(const js::Shape *shape) {
116         JS_ASSERT(shape);
117         v = reinterpret_cast<jsuword>(shape) | SHAPE;
118     }
119 };
120
121 struct PropertyCacheEntry
122 {
123     jsbytecode          *kpc;           /* pc of cache-testing bytecode */
124     jsuword             kshape;         /* shape of direct (key) object */
125     jsuword             vcap;           /* value capability, see above */
126     PCVal               vword;          /* value word, see PCVal above */
127
128     bool adding() const { return vcapTag() == 0 && kshape != vshape(); }
129     bool directHit() const { return vcapTag() == 0 && kshape == vshape(); }
130
131     jsuword vcapTag() const { return vcap & PCVCAP_TAGMASK; }
132     uint32 vshape() const { return uint32(vcap >> PCVCAP_TAGBITS); }
133     jsuword scopeIndex() const { return (vcap >> PCVCAP_PROTOBITS) & PCVCAP_SCOPEMASK; }
134     jsuword protoIndex() const { return vcap & PCVCAP_PROTOMASK; }
135
136     void assign(jsbytecode *kpc, jsuword kshape, jsuword vshape,
137                 uintN scopeIndex, uintN protoIndex, PCVal vword) {
138         JS_ASSERT(kshape < SHAPE_OVERFLOW_BIT);
139         JS_ASSERT(vshape < SHAPE_OVERFLOW_BIT);
140         JS_ASSERT(scopeIndex <= PCVCAP_SCOPEMASK);
141         JS_ASSERT(protoIndex <= PCVCAP_PROTOMASK);
142
143         this->kpc = kpc;
144         this->kshape = kshape;
145         this->vcap = (vshape << PCVCAP_TAGBITS) | (scopeIndex << PCVCAP_PROTOBITS) | protoIndex;
146         this->vword = vword;
147     }
148 };
149
150 /*
151  * Special value for functions returning PropertyCacheEntry * to distinguish
152  * between failure and no no-cache-fill cases.
153  */
154 #define JS_NO_PROP_CACHE_FILL ((js::PropertyCacheEntry *) NULL + 1)
155
156 #if defined DEBUG_brendan || defined DEBUG_brendaneich
157 #define JS_PROPERTY_CACHE_METERING 1
158 #endif
159
160 class PropertyCache
161 {
162   private:
163     enum {
164         SIZE_LOG2 = 12,
165         SIZE = JS_BIT(SIZE_LOG2),
166         MASK = JS_BITMASK(SIZE_LOG2)
167     };
168
169     PropertyCacheEntry  table[SIZE];
170     JSBool              empty;
171 #ifdef JS_PROPERTY_CACHE_METERING
172   public:
173     PropertyCacheEntry  *pctestentry;   /* entry of the last PC-based test */
174     uint32              fills;          /* number of cache entry fills */
175     uint32              nofills;        /* couldn't fill (e.g. default get) */
176     uint32              rofills;        /* set on read-only prop can't fill */
177     uint32              disfills;       /* fill attempts on disabled cache */
178     uint32              oddfills;       /* fill attempt after setter deleted */
179     uint32              add2dictfills;  /* fill attempt on dictionary object */
180     uint32              modfills;       /* fill that rehashed to a new entry */
181     uint32              brandfills;     /* scope brandings to type structural
182                                            method fills */
183     uint32              noprotos;       /* resolve-returned non-proto pobj */
184     uint32              longchains;     /* overlong scope and/or proto chain */
185     uint32              recycles;       /* cache entries recycled by fills */
186     uint32              tests;          /* cache probes */
187     uint32              pchits;         /* fast-path polymorphic op hits */
188     uint32              protopchits;    /* pchits hitting immediate prototype */
189     uint32              initests;       /* cache probes from JSOP_INITPROP */
190     uint32              inipchits;      /* init'ing next property pchit case */
191     uint32              inipcmisses;    /* init'ing next property pc misses */
192     uint32              settests;       /* cache probes from JOF_SET opcodes */
193     uint32              addpchits;      /* adding next property pchit case */
194     uint32              setpchits;      /* setting existing property pchit */
195     uint32              setpcmisses;    /* setting/adding property pc misses */
196     uint32              setmisses;      /* JSOP_SET{NAME,PROP} total misses */
197     uint32              kpcmisses;      /* slow-path key id == atom misses */
198     uint32              kshapemisses;   /* slow-path key object misses */
199     uint32              vcapmisses;     /* value capability misses */
200     uint32              misses;         /* cache misses */
201     uint32              flushes;        /* cache flushes */
202     uint32              pcpurges;       /* shadowing purges on proto chain */
203   private:
204 # define PCMETER(x)     x
205 #else
206 # define PCMETER(x)     ((void)0)
207 #endif
208
209     /*
210      * Add kshape rather than xor it to avoid collisions between nearby bytecode
211      * that are evolving an object by setting successive properties, incrementing
212      * the object's shape on each set.
213      */
214     static inline jsuword
215     hash(jsbytecode *pc, jsuword kshape)
216     {
217         return ((((jsuword(pc) >> SIZE_LOG2) ^ jsuword(pc)) + kshape) & MASK);
218     }
219
220     static inline bool matchShape(JSContext *cx, JSObject *obj, uint32 shape);
221
222     JS_REQUIRES_STACK JSAtom *fullTest(JSContext *cx, jsbytecode *pc, JSObject **objp,
223                                        JSObject **pobjp, PropertyCacheEntry *entry);
224
225 #ifdef DEBUG
226     void assertEmpty();
227 #else
228     inline void assertEmpty() {}
229 #endif
230
231   public:
232     JS_ALWAYS_INLINE JS_REQUIRES_STACK void test(JSContext *cx, jsbytecode *pc,
233                                                  JSObject *&obj, JSObject *&pobj,
234                                                  PropertyCacheEntry *&entry, JSAtom *&atom);
235
236     /*
237      * Test for cached information about a property set on *objp at pc.
238      *
239      * On a hit, set *entryp to the entry and return true.
240      *
241      * On a miss, set *atomp to the name of the property being set and return false.
242      */
243     JS_ALWAYS_INLINE bool testForSet(JSContext *cx, jsbytecode *pc, JSObject *obj,
244                                      PropertyCacheEntry **entryp, JSObject **obj2p,
245                                      JSAtom **atomp);
246
247     /*
248      * Test for cached information about creating a new own data property on obj at pc.
249      *
250      * On a hit, set *shapep to an shape from the property tree describing the
251      * new property as well as all existing properties on obj and return
252      * true. Otherwise return false.
253      *
254      * Hit or miss, *entryp receives a pointer to the property cache entry.
255      */
256     JS_ALWAYS_INLINE bool testForInit(JSRuntime *rt, jsbytecode *pc, JSObject *obj,
257                                       const js::Shape **shapep, PropertyCacheEntry **entryp);
258
259     /*
260      * Fill property cache entry for key cx->fp->pc, optimized value word
261      * computed from obj and shape, and entry capability forged from 24-bit
262      * obj->shape(), 4-bit scopeIndex, and 4-bit protoIndex.
263      *
264      * Return the filled cache entry or JS_NO_PROP_CACHE_FILL if caching was
265      * not possible.
266      */
267     JS_REQUIRES_STACK PropertyCacheEntry *fill(JSContext *cx, JSObject *obj, uintN scopeIndex,
268                                                uintN protoIndex, JSObject *pobj,
269                                                const js::Shape *shape, JSBool adding = false);
270
271     void purge(JSContext *cx);
272     void purgeForScript(JSContext *cx, JSScript *script);
273 };
274
275 } /* namespace js */
276
277 #endif /* jspropertycache_h___ */