Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- |
michael@0 | 2 | * vim: set ts=8 sts=4 et sw=4 tw=99: |
michael@0 | 3 | * This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 6 | |
michael@0 | 7 | #ifndef vm_PIC_h |
michael@0 | 8 | #define vm_PIC_h |
michael@0 | 9 | |
michael@0 | 10 | #include "jsapi.h" |
michael@0 | 11 | #include "jscntxt.h" |
michael@0 | 12 | #include "jsfriendapi.h" |
michael@0 | 13 | #include "jsobj.h" |
michael@0 | 14 | |
michael@0 | 15 | #include "gc/Barrier.h" |
michael@0 | 16 | #include "gc/Heap.h" |
michael@0 | 17 | #include "gc/Marking.h" |
michael@0 | 18 | |
michael@0 | 19 | #include "js/Value.h" |
michael@0 | 20 | #include "vm/GlobalObject.h" |
michael@0 | 21 | |
michael@0 | 22 | namespace js { |
michael@0 | 23 | |
michael@0 | 24 | class Shape; |
michael@0 | 25 | |
michael@0 | 26 | template <typename Category> class PICChain; |
michael@0 | 27 | |
michael@0 | 28 | /* |
michael@0 | 29 | * The basic PICStub just has a pointer to the next stub. |
michael@0 | 30 | */ |
michael@0 | 31 | template <typename Category> |
michael@0 | 32 | class PICStub |
michael@0 | 33 | { |
michael@0 | 34 | friend class PICChain<Category>; |
michael@0 | 35 | private: |
michael@0 | 36 | typedef typename Category::Stub CatStub; |
michael@0 | 37 | typedef typename Category::Chain CatChain; |
michael@0 | 38 | |
michael@0 | 39 | protected: |
michael@0 | 40 | CatStub *next_; |
michael@0 | 41 | |
michael@0 | 42 | PICStub() : next_(nullptr) {} |
michael@0 | 43 | PICStub(const CatStub *next) : next_(next) { |
michael@0 | 44 | JS_ASSERT(next_); |
michael@0 | 45 | } |
michael@0 | 46 | PICStub(const CatStub &other) : next_(other.next_) {} |
michael@0 | 47 | |
michael@0 | 48 | public: |
michael@0 | 49 | CatStub *next() const { |
michael@0 | 50 | return next_; |
michael@0 | 51 | } |
michael@0 | 52 | |
michael@0 | 53 | protected: |
michael@0 | 54 | void append(CatStub *stub) { |
michael@0 | 55 | JS_ASSERT(!next_); |
michael@0 | 56 | JS_ASSERT(!stub->next_); |
michael@0 | 57 | next_ = stub; |
michael@0 | 58 | } |
michael@0 | 59 | }; |
michael@0 | 60 | |
michael@0 | 61 | /* |
michael@0 | 62 | * The basic PIC just has a pointer to the list of stubs. |
michael@0 | 63 | */ |
michael@0 | 64 | template <typename Category> |
michael@0 | 65 | class PICChain |
michael@0 | 66 | { |
michael@0 | 67 | private: |
michael@0 | 68 | typedef typename Category::Stub CatStub; |
michael@0 | 69 | typedef typename Category::Chain CatChain; |
michael@0 | 70 | |
michael@0 | 71 | protected: |
michael@0 | 72 | CatStub *stubs_; |
michael@0 | 73 | |
michael@0 | 74 | PICChain() : stubs_(nullptr) {} |
michael@0 | 75 | // PICs should never be copy constructed. |
michael@0 | 76 | PICChain(const PICChain<Category> &other) MOZ_DELETE; |
michael@0 | 77 | |
michael@0 | 78 | public: |
michael@0 | 79 | CatStub *stubs() const { |
michael@0 | 80 | return stubs_; |
michael@0 | 81 | } |
michael@0 | 82 | |
michael@0 | 83 | void addStub(CatStub *stub) { |
michael@0 | 84 | JS_ASSERT(stub); |
michael@0 | 85 | JS_ASSERT(!stub->next()); |
michael@0 | 86 | if (!stubs_) { |
michael@0 | 87 | stubs_ = stub; |
michael@0 | 88 | return; |
michael@0 | 89 | } |
michael@0 | 90 | |
michael@0 | 91 | CatStub *cur = stubs_; |
michael@0 | 92 | while (cur->next()) |
michael@0 | 93 | cur = cur->next(); |
michael@0 | 94 | cur->append(stub); |
michael@0 | 95 | } |
michael@0 | 96 | |
michael@0 | 97 | unsigned numStubs() const { |
michael@0 | 98 | unsigned count = 0; |
michael@0 | 99 | for (CatStub *stub = stubs_; stub; stub = stub->next()) |
michael@0 | 100 | count++; |
michael@0 | 101 | return count; |
michael@0 | 102 | } |
michael@0 | 103 | |
michael@0 | 104 | void removeStub(CatStub *stub, CatStub *previous) { |
michael@0 | 105 | if (previous) { |
michael@0 | 106 | JS_ASSERT(previous->next() == stub); |
michael@0 | 107 | previous->next_ = stub->next(); |
michael@0 | 108 | } else { |
michael@0 | 109 | JS_ASSERT(stub == stubs_); |
michael@0 | 110 | stubs_ = stub->next(); |
michael@0 | 111 | } |
michael@0 | 112 | js_delete(stub); |
michael@0 | 113 | } |
michael@0 | 114 | }; |
michael@0 | 115 | |
michael@0 | 116 | /* |
michael@0 | 117 | * ForOfPIC defines a PIC category for optimizing for-of operations. |
michael@0 | 118 | */ |
michael@0 | 119 | struct ForOfPIC |
michael@0 | 120 | { |
michael@0 | 121 | /* Forward declarations so template-substitution works. */ |
michael@0 | 122 | class Stub; |
michael@0 | 123 | class Chain; |
michael@0 | 124 | |
michael@0 | 125 | ForOfPIC() MOZ_DELETE; |
michael@0 | 126 | ForOfPIC(const ForOfPIC &other) MOZ_DELETE; |
michael@0 | 127 | |
michael@0 | 128 | typedef PICStub<ForOfPIC> BaseStub; |
michael@0 | 129 | typedef PICChain<ForOfPIC> BaseChain; |
michael@0 | 130 | |
michael@0 | 131 | /* |
michael@0 | 132 | * A ForOfPIC has only one kind of stub for now: one that holds the shape |
michael@0 | 133 | * of an array object that does not override its '@@iterator' property. |
michael@0 | 134 | */ |
michael@0 | 135 | class Stub : public BaseStub |
michael@0 | 136 | { |
michael@0 | 137 | private: |
michael@0 | 138 | // Shape of matching array object. |
michael@0 | 139 | Shape *shape_; |
michael@0 | 140 | |
michael@0 | 141 | public: |
michael@0 | 142 | Stub(Shape *shape) |
michael@0 | 143 | : BaseStub(), |
michael@0 | 144 | shape_(shape) |
michael@0 | 145 | { |
michael@0 | 146 | JS_ASSERT(shape_); |
michael@0 | 147 | } |
michael@0 | 148 | |
michael@0 | 149 | Shape *shape() { |
michael@0 | 150 | return shape_; |
michael@0 | 151 | } |
michael@0 | 152 | }; |
michael@0 | 153 | |
michael@0 | 154 | /* |
michael@0 | 155 | * A ForOfPIC chain holds the following: |
michael@0 | 156 | * |
michael@0 | 157 | * Array.prototype (arrayProto_) |
michael@0 | 158 | * To ensure that the incoming array has the standard proto. |
michael@0 | 159 | * |
michael@0 | 160 | * Array.prototype's shape (arrayProtoShape_) |
michael@0 | 161 | * To ensure that Array.prototype has not been modified. |
michael@0 | 162 | * |
michael@0 | 163 | * ArrayIterator.prototype (arrayIteratorProto_) |
michael@0 | 164 | * ArrayIterator.prototype's shape (arrayIteratorProtoShape_) |
michael@0 | 165 | * To ensure that an ArrayIterator.prototype has not been modified. |
michael@0 | 166 | * |
michael@0 | 167 | * Array.prototype's slot number for '@@iterator' (arrayProtoIteratorSlot_) |
michael@0 | 168 | * Array.prototype's canonical value for '@@iterator' (canonicalIteratorFunc_) |
michael@0 | 169 | * To quickly retreive and ensure that the iterator constructor |
michael@0 | 170 | * stored in the slot has not changed. |
michael@0 | 171 | * |
michael@0 | 172 | * ArrayIterator.prototype's slot number for 'next' (arrayIteratorProtoNextSlot_) |
michael@0 | 173 | * ArrayIterator.prototype's canonical value for 'next' (canonicalNextFunc_) |
michael@0 | 174 | * To quickly retreive and ensure that the 'next' method for ArrayIterator |
michael@0 | 175 | * objects has not changed. |
michael@0 | 176 | */ |
michael@0 | 177 | class Chain : public BaseChain |
michael@0 | 178 | { |
michael@0 | 179 | private: |
michael@0 | 180 | // Pointer to canonical Array.prototype and ArrayIterator.prototype |
michael@0 | 181 | HeapPtrObject arrayProto_; |
michael@0 | 182 | HeapPtrObject arrayIteratorProto_; |
michael@0 | 183 | |
michael@0 | 184 | // Shape of matching Array.prototype object, and slot containing |
michael@0 | 185 | // the '@@iterator' for it, and the canonical value. |
michael@0 | 186 | HeapPtrShape arrayProtoShape_; |
michael@0 | 187 | uint32_t arrayProtoIteratorSlot_; |
michael@0 | 188 | HeapValue canonicalIteratorFunc_; |
michael@0 | 189 | |
michael@0 | 190 | // Shape of matching ArrayIteratorProto, and slot containing |
michael@0 | 191 | // the 'next' property, and the canonical value. |
michael@0 | 192 | HeapPtrShape arrayIteratorProtoShape_; |
michael@0 | 193 | uint32_t arrayIteratorProtoNextSlot_; |
michael@0 | 194 | HeapValue canonicalNextFunc_; |
michael@0 | 195 | |
michael@0 | 196 | // Initialization flag marking lazy initialization of above fields. |
michael@0 | 197 | bool initialized_; |
michael@0 | 198 | |
michael@0 | 199 | // Disabled flag is set when we don't want to try optimizing anymore |
michael@0 | 200 | // because core objects were changed. |
michael@0 | 201 | bool disabled_; |
michael@0 | 202 | |
michael@0 | 203 | static const unsigned MAX_STUBS = 10; |
michael@0 | 204 | |
michael@0 | 205 | public: |
michael@0 | 206 | Chain() |
michael@0 | 207 | : BaseChain(), |
michael@0 | 208 | arrayProto_(nullptr), |
michael@0 | 209 | arrayIteratorProto_(nullptr), |
michael@0 | 210 | arrayProtoShape_(nullptr), |
michael@0 | 211 | arrayProtoIteratorSlot_(-1), |
michael@0 | 212 | canonicalIteratorFunc_(UndefinedValue()), |
michael@0 | 213 | arrayIteratorProtoShape_(nullptr), |
michael@0 | 214 | arrayIteratorProtoNextSlot_(-1), |
michael@0 | 215 | initialized_(false), |
michael@0 | 216 | disabled_(false) |
michael@0 | 217 | {} |
michael@0 | 218 | |
michael@0 | 219 | // Initialize the canonical iterator function. |
michael@0 | 220 | bool initialize(JSContext *cx); |
michael@0 | 221 | |
michael@0 | 222 | // Check if a given array object is optimized by this PIC. |
michael@0 | 223 | Stub *isArrayOptimized(ArrayObject *obj); |
michael@0 | 224 | |
michael@0 | 225 | // Try to optimize this chain for an object. |
michael@0 | 226 | bool tryOptimizeArray(JSContext *cx, HandleObject array, bool *optimized); |
michael@0 | 227 | |
michael@0 | 228 | // Check if the global array-related objects have not been messed with |
michael@0 | 229 | // in a way that would disable this PIC. |
michael@0 | 230 | bool isArrayStateStillSane(); |
michael@0 | 231 | |
michael@0 | 232 | // Check if ArrayIterator.next is still optimizable. |
michael@0 | 233 | inline bool isArrayNextStillSane() { |
michael@0 | 234 | return (arrayIteratorProto_->lastProperty() == arrayIteratorProtoShape_) && |
michael@0 | 235 | (arrayIteratorProto_->getSlot(arrayIteratorProtoNextSlot_) == canonicalNextFunc_); |
michael@0 | 236 | } |
michael@0 | 237 | |
michael@0 | 238 | void mark(JSTracer *trc); |
michael@0 | 239 | void sweep(FreeOp *fop); |
michael@0 | 240 | |
michael@0 | 241 | private: |
michael@0 | 242 | // Get a matching optimized stub for the given object. |
michael@0 | 243 | Stub *getMatchingStub(JSObject *obj); |
michael@0 | 244 | |
michael@0 | 245 | // Check if the given object is for-of optimizable with this PIC. |
michael@0 | 246 | bool isOptimizableArray(JSObject *obj); |
michael@0 | 247 | |
michael@0 | 248 | // Reset the PIC and all info associated with it. |
michael@0 | 249 | void reset(JSContext *cx); |
michael@0 | 250 | |
michael@0 | 251 | // Erase the stub chain. |
michael@0 | 252 | void eraseChain(); |
michael@0 | 253 | }; |
michael@0 | 254 | |
michael@0 | 255 | // Class for object that holds ForOfPIC chain. |
michael@0 | 256 | static const Class jsclass; |
michael@0 | 257 | |
michael@0 | 258 | static JSObject *createForOfPICObject(JSContext *cx, Handle<GlobalObject *> global); |
michael@0 | 259 | |
michael@0 | 260 | static inline Chain *fromJSObject(JSObject *obj) { |
michael@0 | 261 | JS_ASSERT(js::GetObjectClass(obj) == &ForOfPIC::jsclass); |
michael@0 | 262 | return (ForOfPIC::Chain *) obj->getPrivate(); |
michael@0 | 263 | } |
michael@0 | 264 | static inline Chain *getOrCreate(JSContext *cx) { |
michael@0 | 265 | JSObject *obj = cx->global()->getForOfPICObject(); |
michael@0 | 266 | if (obj) |
michael@0 | 267 | return fromJSObject(obj); |
michael@0 | 268 | return create(cx); |
michael@0 | 269 | } |
michael@0 | 270 | static Chain *create(JSContext *cx); |
michael@0 | 271 | }; |
michael@0 | 272 | |
michael@0 | 273 | |
michael@0 | 274 | } // namespace js |
michael@0 | 275 | |
michael@0 | 276 | #endif /* vm_PIC_h */ |