]> git.saurik.com Git - apple/javascriptcore.git/blob - ftl/FTLAbstractHeap.h
JavaScriptCore-7601.1.46.3.tar.gz
[apple/javascriptcore.git] / ftl / FTLAbstractHeap.h
1 /*
2 * Copyright (C) 2013, 2015 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26 #ifndef FTLAbstractHeap_h
27 #define FTLAbstractHeap_h
28
29 #if ENABLE(FTL_JIT)
30
31 #include "FTLAbbreviations.h"
32 #include "JSCJSValue.h"
33 #include <array>
34 #include <wtf/FastMalloc.h>
35 #include <wtf/HashMap.h>
36 #include <wtf/Noncopyable.h>
37 #include <wtf/Vector.h>
38 #include <wtf/text/CString.h>
39
40 namespace JSC { namespace FTL {
41
42 // The FTL JIT tries to aid LLVM's TBAA. The FTL's notion of how this
43 // happens is the AbstractHeap. AbstractHeaps are a simple type system
44 // with sub-typing.
45
46 class AbstractHeapRepository;
47 class Output;
48 class TypedPointer;
49
50 class AbstractHeap {
51 WTF_MAKE_NONCOPYABLE(AbstractHeap); WTF_MAKE_FAST_ALLOCATED;
52 public:
53 AbstractHeap()
54 : m_parent(0)
55 , m_heapName(0)
56 , m_tbaaMetadata(0)
57 {
58 }
59
60 AbstractHeap(AbstractHeap* parent, const char* heapName)
61 : m_parent(parent)
62 , m_heapName(heapName)
63 , m_tbaaMetadata(0)
64 {
65 }
66
67 bool isInitialized() const { return !!m_heapName; }
68
69 void initialize(AbstractHeap* parent, const char* heapName)
70 {
71 m_parent = parent;
72 m_heapName = heapName;
73 }
74
75 void changeParent(AbstractHeap* parent)
76 {
77 m_parent = parent;
78 }
79
80 AbstractHeap* parent() const
81 {
82 ASSERT(isInitialized());
83 return m_parent;
84 }
85
86 const char* heapName() const
87 {
88 ASSERT(isInitialized());
89 return m_heapName;
90 }
91
92 LValue tbaaMetadata(const AbstractHeapRepository& repository) const
93 {
94 ASSERT(isInitialized());
95 if (LIKELY(!!m_tbaaMetadata))
96 return m_tbaaMetadata;
97 return tbaaMetadataSlow(repository);
98 }
99
100 void decorateInstruction(LValue instruction, const AbstractHeapRepository&) const;
101
102 void dump(PrintStream&) const;
103
104 private:
105 friend class AbstractHeapRepository;
106
107 LValue tbaaMetadataSlow(const AbstractHeapRepository&) const;
108
109 AbstractHeap* m_parent;
110 const char* m_heapName;
111 mutable LValue m_tbaaMetadata;
112 };
113
114 // Think of "AbstractField" as being an "AbstractHeapWithOffset". I would have named
115 // it the latter except that I don't like typing that much.
116 class AbstractField : public AbstractHeap {
117 public:
118 AbstractField()
119 {
120 }
121
122 AbstractField(AbstractHeap* parent, const char* heapName, ptrdiff_t offset)
123 : AbstractHeap(parent, heapName)
124 , m_offset(offset)
125 {
126 }
127
128 void initialize(AbstractHeap* parent, const char* heapName, ptrdiff_t offset)
129 {
130 AbstractHeap::initialize(parent, heapName);
131 m_offset = offset;
132 }
133
134 ptrdiff_t offset() const
135 {
136 ASSERT(isInitialized());
137 return m_offset;
138 }
139
140 void dump(PrintStream&) const;
141
142 private:
143 ptrdiff_t m_offset;
144 };
145
146 class IndexedAbstractHeap {
147 public:
148 IndexedAbstractHeap(LContext, AbstractHeap* parent, const char* heapName, ptrdiff_t offset, size_t elementSize);
149 ~IndexedAbstractHeap();
150
151 const AbstractHeap& atAnyIndex() const { return m_heapForAnyIndex; }
152
153 const AbstractField& at(ptrdiff_t index)
154 {
155 if (static_cast<size_t>(index) < m_smallIndices.size())
156 return returnInitialized(m_smallIndices[index], index);
157 return atSlow(index);
158 }
159
160 const AbstractField& operator[](ptrdiff_t index) { return at(index); }
161
162 TypedPointer baseIndex(Output& out, LValue base, LValue index, JSValue indexAsConstant = JSValue(), ptrdiff_t offset = 0);
163
164 void dump(PrintStream&) const;
165
166 private:
167 const AbstractField& returnInitialized(AbstractField& field, ptrdiff_t index)
168 {
169 if (UNLIKELY(!field.isInitialized()))
170 initialize(field, index);
171 return field;
172 }
173
174 const AbstractField& atSlow(ptrdiff_t index);
175 void initialize(AbstractField& field, ptrdiff_t index);
176
177 AbstractHeap m_heapForAnyIndex;
178 size_t m_heapNameLength;
179 ptrdiff_t m_offset;
180 size_t m_elementSize;
181 LValue m_scaleTerm;
182 bool m_canShift;
183 std::array<AbstractField, 16> m_smallIndices;
184
185 struct WithoutZeroOrOneHashTraits : WTF::GenericHashTraits<ptrdiff_t> {
186 static void constructDeletedValue(ptrdiff_t& slot) { slot = 1; }
187 static bool isDeletedValue(ptrdiff_t value) { return value == 1; }
188 };
189 typedef HashMap<ptrdiff_t, std::unique_ptr<AbstractField>, WTF::IntHash<ptrdiff_t>, WithoutZeroOrOneHashTraits> MapType;
190
191 std::unique_ptr<MapType> m_largeIndices;
192 Vector<CString, 16> m_largeIndexNames;
193 };
194
195 // A numbered abstract heap is like an indexed abstract heap, except that you
196 // can't rely on there being a relationship between the number you use to
197 // retrieve the sub-heap, and the offset that this heap has. (In particular,
198 // the sub-heaps don't have indices.)
199
200 class NumberedAbstractHeap {
201 public:
202 NumberedAbstractHeap(LContext, AbstractHeap* parent, const char* heapName);
203 ~NumberedAbstractHeap();
204
205 const AbstractHeap& atAnyNumber() const { return m_indexedHeap.atAnyIndex(); }
206
207 const AbstractHeap& at(unsigned number) { return m_indexedHeap.at(number); }
208 const AbstractHeap& operator[](unsigned number) { return at(number); }
209
210 void dump(PrintStream&) const;
211
212 private:
213
214 // We use the fact that the indexed heap already has a superset of the
215 // functionality we need.
216 IndexedAbstractHeap m_indexedHeap;
217 };
218
219 class AbsoluteAbstractHeap {
220 public:
221 AbsoluteAbstractHeap(LContext, AbstractHeap* parent, const char* heapName);
222 ~AbsoluteAbstractHeap();
223
224 const AbstractHeap& atAnyAddress() const { return m_indexedHeap.atAnyIndex(); }
225
226 const AbstractHeap& at(void* address)
227 {
228 return m_indexedHeap.at(bitwise_cast<ptrdiff_t>(address));
229 }
230
231 const AbstractHeap& operator[](void* address) { return at(address); }
232
233 void dump(PrintStream&) const;
234
235 private:
236 // The trick here is that the indexed heap is "indexed" by a pointer-width
237 // integer. Pointers are themselves pointer-width integers. So we can reuse
238 // all of the functionality.
239 IndexedAbstractHeap m_indexedHeap;
240 };
241
242 } } // namespace JSC::FTL
243
244 #endif // ENABLE(FTL_JIT)
245
246 #endif // FTLAbstractHeap_h
247