File: StructureSet.h

package info (click to toggle)
webkit2gtk 2.6.2%2Bdfsg1-4
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 115,572 kB
  • ctags: 216,388
  • sloc: cpp: 1,164,175; ansic: 18,422; perl: 16,884; python: 11,608; ruby: 9,409; xml: 8,376; asm: 4,765; yacc: 2,292; lex: 891; sh: 650; makefile: 79
file content (289 lines) | stat: -rw-r--r-- 7,799 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/*
 * Copyright (C) 2011, 2013, 2014 Apple Inc. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
 */

#ifndef StructureSet_h
#define StructureSet_h

#include "ArrayProfile.h"
#include "SpeculatedType.h"
#include "Structure.h"
#include "DumpContext.h"

namespace JSC {

namespace DFG {
class StructureAbstractValue;
struct AbstractValue;
}

class StructureSet {
public:
    StructureSet()
        : m_pointer(0)
    {
        setEmpty();
    }
    
    StructureSet(Structure* structure)
        : m_pointer(0)
    {
        set(structure);
    }
    
    ALWAYS_INLINE StructureSet(const StructureSet& other)
        : m_pointer(0)
    {
        copyFrom(other);
    }
    
    ALWAYS_INLINE StructureSet& operator=(const StructureSet& other)
    {
        if (this == &other)
            return *this;
        deleteStructureListIfNecessary();
        copyFrom(other);
        return *this;
    }
    
    ~StructureSet()
    {
        deleteStructureListIfNecessary();
    }
    
    void clear();
    
    Structure* onlyStructure() const
    {
        if (isThin())
            return singleStructure();
        OutOfLineList* list = structureList();
        if (list->m_length != 1)
            return nullptr;
        return list->list()[0];
    }
    
    bool isEmpty() const
    {
        bool result = isThin() && !singleStructure();
        if (result)
            ASSERT(m_pointer != reservedValue);
        return result;
    }
    
    bool add(Structure*);
    bool remove(Structure*);
    bool contains(Structure*) const;
    
    bool merge(const StructureSet&);
    void filter(const StructureSet&);
    void exclude(const StructureSet&);
    
#if ENABLE(DFG_JIT)
    void filter(const DFG::StructureAbstractValue&);
    void filter(SpeculatedType);
    void filterArrayModes(ArrayModes);
    void filter(const DFG::AbstractValue&);
#endif // ENABLE(DFG_JIT)
    
    template<typename Functor>
    void genericFilter(Functor& functor)
    {
        if (isThin()) {
            if (!singleStructure())
                return;
            if (functor(singleStructure()))
                return;
            clear();
            return;
        }
        
        OutOfLineList* list = structureList();
        for (unsigned i = 0; i < list->m_length; ++i) {
            if (functor(list->list()[i]))
                continue;
            list->list()[i--] = list->list()[--list->m_length];
        }
        if (!list->m_length)
            clear();
    }
    
    bool isSubsetOf(const StructureSet&) const;
    bool isSupersetOf(const StructureSet& other) const
    {
        return other.isSubsetOf(*this);
    }
    
    bool overlaps(const StructureSet&) const;
    
    size_t size() const
    {
        if (isThin())
            return !!singleStructure();
        return structureList()->m_length;
    }
    
    Structure* at(size_t i) const
    {
        if (isThin()) {
            ASSERT(!i);
            ASSERT(singleStructure());
            return singleStructure();
        }
        ASSERT(i < structureList()->m_length);
        return structureList()->list()[i];
    }
    
    Structure* operator[](size_t i) const { return at(i); }
    
    Structure* last() const
    {
        if (isThin()) {
            ASSERT(singleStructure());
            return singleStructure();
        }
        return structureList()->list()[structureList()->m_length - 1];
    }
    
    bool operator==(const StructureSet& other) const;
    
    SpeculatedType speculationFromStructures() const;
    ArrayModes arrayModesFromStructures() const;
    
    void dumpInContext(PrintStream&, DumpContext*) const;
    void dump(PrintStream&) const;
    
private:
    friend class DFG::StructureAbstractValue;
    
    static const uintptr_t thinFlag = 1;
    static const uintptr_t reservedFlag = 2;
    static const uintptr_t flags = thinFlag | reservedFlag;
    static const uintptr_t reservedValue = 4;

    static const unsigned defaultStartingSize = 4;
    
    bool addOutOfLine(Structure*);
    bool containsOutOfLine(Structure*) const;
    
    class ContainsOutOfLine {
    public:
        ContainsOutOfLine(const StructureSet& set)
            : m_set(set)
        {
        }
        
        bool operator()(Structure* structure)
        {
            return m_set.containsOutOfLine(structure);
        }
    private:
        const StructureSet& m_set;
    };

    ALWAYS_INLINE void copyFrom(const StructureSet& other)
    {
        if (other.isThin() || other.m_pointer == reservedValue) {
            bool value = getReservedFlag();
            m_pointer = other.m_pointer;
            setReservedFlag(value);
            return;
        }
        copyFromOutOfLine(other);
    }
    void copyFromOutOfLine(const StructureSet& other);
    
    class OutOfLineList {
    public:
        static OutOfLineList* create(unsigned capacity);
        static void destroy(OutOfLineList*);
        
        Structure** list() { return bitwise_cast<Structure**>(this + 1); }
        
        OutOfLineList(unsigned length, unsigned capacity)
            : m_length(length)
            , m_capacity(capacity)
        {
        }

        unsigned m_length;
        unsigned m_capacity;
    };
    
    ALWAYS_INLINE void deleteStructureListIfNecessary()
    {
        if (!isThin() && m_pointer != reservedValue)
            OutOfLineList::destroy(structureList());
    }
    
    bool isThin() const { return m_pointer & thinFlag; }
    
    void* pointer() const
    {
        return bitwise_cast<void*>(m_pointer & ~flags);
    }
    
    Structure* singleStructure() const
    {
        ASSERT(isThin());
        return static_cast<Structure*>(pointer());
    }
    
    OutOfLineList* structureList() const
    {
        ASSERT(!isThin());
        return static_cast<OutOfLineList*>(pointer());
    }
    
    void set(Structure* structure)
    {
        set(bitwise_cast<uintptr_t>(structure), true);
    }
    void set(OutOfLineList* structures)
    {
        set(bitwise_cast<uintptr_t>(structures), false);
    }
    void setEmpty()
    {
        set(0, true);
    }
    void set(uintptr_t pointer, bool singleStructure)
    {
        m_pointer = pointer | (singleStructure ? thinFlag : 0) | (m_pointer & reservedFlag);
    }
    bool getReservedFlag() const { return m_pointer & reservedFlag; }
    void setReservedFlag(bool value)
    {
        if (value)
            m_pointer |= reservedFlag;
        else
            m_pointer &= ~reservedFlag;
    }

    uintptr_t m_pointer;
};

} // namespace JSC

#endif // StructureSet_h