File: FTLAbstractHeap.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 (233 lines) | stat: -rw-r--r-- 7,222 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
/*
 * Copyright (C) 2013 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 FTLAbstractHeap_h
#define FTLAbstractHeap_h

#if ENABLE(FTL_JIT)

#include "FTLAbbreviations.h"
#include "JSCJSValue.h"
#include <array>
#include <wtf/FastMalloc.h>
#include <wtf/HashMap.h>
#include <wtf/Noncopyable.h>
#include <wtf/OwnPtr.h>
#include <wtf/Vector.h>
#include <wtf/text/CString.h>

namespace JSC { namespace FTL {

// The FTL JIT tries to aid LLVM's TBAA. The FTL's notion of how this
// happens is the AbstractHeap. AbstractHeaps are a simple type system
// with sub-typing.

class AbstractHeapRepository;
class Output;
class TypedPointer;

class AbstractHeap {
    WTF_MAKE_NONCOPYABLE(AbstractHeap); WTF_MAKE_FAST_ALLOCATED;
public:
    AbstractHeap()
        : m_parent(0)
        , m_heapName(0)
        , m_tbaaMetadata(0)
    {
    }
    
    AbstractHeap(AbstractHeap* parent, const char* heapName)
        : m_parent(parent)
        , m_heapName(heapName)
        , m_tbaaMetadata(0)
    {
    }
    
    bool isInitialized() const { return !!m_heapName; }
    
    void initialize(AbstractHeap* parent, const char* heapName)
    {
        m_parent = parent;
        m_heapName = heapName;
    }

    AbstractHeap* parent() const
    {
        ASSERT(isInitialized());
        return m_parent;
    }
    
    const char* heapName() const
    {
        ASSERT(isInitialized());
        return m_heapName;
    }
    
    LValue tbaaMetadata(const AbstractHeapRepository& repository) const
    {
        ASSERT(isInitialized());
        if (LIKELY(!!m_tbaaMetadata))
            return m_tbaaMetadata;
        return tbaaMetadataSlow(repository);
    }
    
    void decorateInstruction(LValue instruction, const AbstractHeapRepository&) const;

private:
    friend class AbstractHeapRepository;
    
    LValue tbaaMetadataSlow(const AbstractHeapRepository&) const;
    
    AbstractHeap* m_parent;
    const char* m_heapName;
    mutable LValue m_tbaaMetadata;
};

// Think of "AbstractField" as being an "AbstractHeapWithOffset". I would have named
// it the latter except that I don't like typing that much.
class AbstractField : public AbstractHeap {
public:
    AbstractField()
    {
    }
    
    AbstractField(AbstractHeap* parent, const char* heapName, ptrdiff_t offset)
        : AbstractHeap(parent, heapName)
        , m_offset(offset)
    {
    }
    
    void initialize(AbstractHeap* parent, const char* heapName, ptrdiff_t offset)
    {
        AbstractHeap::initialize(parent, heapName);
        m_offset = offset;
    }
    
    ptrdiff_t offset() const
    {
        ASSERT(isInitialized());
        return m_offset;
    }
    
private:
    ptrdiff_t m_offset;
};

class IndexedAbstractHeap {
public:
    IndexedAbstractHeap(LContext, AbstractHeap* parent, const char* heapName, ptrdiff_t offset, size_t elementSize);
    ~IndexedAbstractHeap();
    
    const AbstractHeap& atAnyIndex() const { return m_heapForAnyIndex; }
    
    const AbstractField& at(ptrdiff_t index)
    {
        if (static_cast<size_t>(index) < m_smallIndices.size())
            return returnInitialized(m_smallIndices[index], index);
        return atSlow(index);
    }
    
    const AbstractField& operator[](ptrdiff_t index) { return at(index); }
    
    TypedPointer baseIndex(Output& out, LValue base, LValue index, JSValue indexAsConstant = JSValue(), ptrdiff_t offset = 0);
    
private:
    const AbstractField& returnInitialized(AbstractField& field, ptrdiff_t index)
    {
        if (UNLIKELY(!field.isInitialized()))
            initialize(field, index);
        return field;
    }

    const AbstractField& atSlow(ptrdiff_t index);
    void initialize(AbstractField& field, ptrdiff_t index);

    AbstractHeap m_heapForAnyIndex;
    size_t m_heapNameLength;
    ptrdiff_t m_offset;
    size_t m_elementSize;
    LValue m_scaleTerm;
    bool m_canShift;
    std::array<AbstractField, 16> m_smallIndices;
    
    struct WithoutZeroOrOneHashTraits : WTF::GenericHashTraits<ptrdiff_t> {
        static void constructDeletedValue(ptrdiff_t& slot) { slot = 1; }
        static bool isDeletedValue(ptrdiff_t value) { return value == 1; }
    };
    typedef HashMap<ptrdiff_t, std::unique_ptr<AbstractField>, WTF::IntHash<ptrdiff_t>, WithoutZeroOrOneHashTraits> MapType;
    
    OwnPtr<MapType> m_largeIndices;
    Vector<CString, 16> m_largeIndexNames;
};

// A numbered abstract heap is like an indexed abstract heap, except that you
// can't rely on there being a relationship between the number you use to
// retrieve the sub-heap, and the offset that this heap has. (In particular,
// the sub-heaps don't have indices.)

class NumberedAbstractHeap {
public:
    NumberedAbstractHeap(LContext, AbstractHeap* parent, const char* heapName);
    ~NumberedAbstractHeap();
    
    const AbstractHeap& atAnyNumber() const { return m_indexedHeap.atAnyIndex(); }
    
    const AbstractHeap& at(unsigned number) { return m_indexedHeap.at(number); }
    const AbstractHeap& operator[](unsigned number) { return at(number); }

private:
    
    // We use the fact that the indexed heap already has a superset of the
    // functionality we need.
    IndexedAbstractHeap m_indexedHeap;
};

class AbsoluteAbstractHeap {
public:
    AbsoluteAbstractHeap(LContext, AbstractHeap* parent, const char* heapName);
    ~AbsoluteAbstractHeap();
    
    const AbstractHeap& atAnyAddress() const { return m_indexedHeap.atAnyIndex(); }
    
    const AbstractHeap& at(void* address)
    {
        return m_indexedHeap.at(bitwise_cast<ptrdiff_t>(address));
    }
    
    const AbstractHeap& operator[](void* address) { return at(address); }

private:
    // The trick here is that the indexed heap is "indexed" by a pointer-width
    // integer. Pointers are themselves pointer-width integers. So we can reuse
    // all of the functionality.
    IndexedAbstractHeap m_indexedHeap;
};

} } // namespace JSC::FTL

#endif // ENABLE(FTL_JIT)

#endif // FTLAbstractHeap_h