FTL should lower its abstract heaps to B3 heap ranges
[WebKit-https.git] / Source / JavaScriptCore / ftl / FTLAbstractHeapRepository.h
1 /*
2  * Copyright (C) 2013-2016 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 FTLAbstractHeapRepository_h
27 #define FTLAbstractHeapRepository_h
28
29 #if ENABLE(FTL_JIT)
30
31 #include "B3Value.h"
32 #include "DFGArrayMode.h"
33 #include "FTLAbstractHeap.h"
34 #include "IndexingType.h"
35
36 namespace JSC { namespace FTL {
37
38 #define FOR_EACH_ABSTRACT_HEAP(macro) \
39     macro(typedArrayProperties)
40
41 #define FOR_EACH_ABSTRACT_FIELD(macro) \
42     macro(ArrayBuffer_data, ArrayBuffer::offsetOfData()) \
43     macro(Butterfly_arrayBuffer, Butterfly::offsetOfArrayBuffer()) \
44     macro(Butterfly_publicLength, Butterfly::offsetOfPublicLength()) \
45     macro(Butterfly_vectorLength, Butterfly::offsetOfVectorLength()) \
46     macro(CallFrame_callerFrame, CallFrame::callerFrameOffset()) \
47     macro(DirectArguments_callee, DirectArguments::offsetOfCallee()) \
48     macro(DirectArguments_length, DirectArguments::offsetOfLength()) \
49     macro(DirectArguments_minCapacity, DirectArguments::offsetOfMinCapacity()) \
50     macro(DirectArguments_overrides, DirectArguments::offsetOfOverrides()) \
51     macro(GetterSetter_getter, GetterSetter::offsetOfGetter()) \
52     macro(GetterSetter_setter, GetterSetter::offsetOfSetter()) \
53     macro(JSArrayBufferView_length, JSArrayBufferView::offsetOfLength()) \
54     macro(JSArrayBufferView_mode, JSArrayBufferView::offsetOfMode()) \
55     macro(JSArrayBufferView_vector, JSArrayBufferView::offsetOfVector()) \
56     macro(JSCell_cellState, JSCell::cellStateOffset()) \
57     macro(JSCell_indexingType, JSCell::indexingTypeOffset()) \
58     macro(JSCell_structureID, JSCell::structureIDOffset()) \
59     macro(JSCell_typeInfoFlags, JSCell::typeInfoFlagsOffset()) \
60     macro(JSCell_typeInfoType, JSCell::typeInfoTypeOffset()) \
61     macro(JSCell_usefulBytes, JSCell::indexingTypeOffset()) \
62     macro(JSFunction_executable, JSFunction::offsetOfExecutable()) \
63     macro(JSFunction_scope, JSFunction::offsetOfScopeChain()) \
64     macro(JSFunction_rareData, JSFunction::offsetOfRareData()) \
65     macro(JSObject_butterfly, JSObject::butterflyOffset()) \
66     macro(JSPropertyNameEnumerator_cachedInlineCapacity, JSPropertyNameEnumerator::cachedInlineCapacityOffset()) \
67     macro(JSPropertyNameEnumerator_cachedPropertyNamesVector, JSPropertyNameEnumerator::cachedPropertyNamesVectorOffset()) \
68     macro(JSPropertyNameEnumerator_cachedStructureID, JSPropertyNameEnumerator::cachedStructureIDOffset()) \
69     macro(JSPropertyNameEnumerator_endGenericPropertyIndex, JSPropertyNameEnumerator::endGenericPropertyIndexOffset()) \
70     macro(JSPropertyNameEnumerator_endStructurePropertyIndex, JSPropertyNameEnumerator::endStructurePropertyIndexOffset()) \
71     macro(JSPropertyNameEnumerator_indexLength, JSPropertyNameEnumerator::indexedLengthOffset()) \
72     macro(JSScope_next, JSScope::offsetOfNext()) \
73     macro(JSString_flags, JSString::offsetOfFlags()) \
74     macro(JSString_length, JSString::offsetOfLength()) \
75     macro(JSString_value, JSString::offsetOfValue()) \
76     macro(JSSymbolTableObject_symbolTable, JSSymbolTableObject::offsetOfSymbolTable()) \
77     macro(JSWrapperObject_internalValue, JSWrapperObject::internalValueOffset()) \
78     macro(MarkedAllocator_freeListHead, MarkedAllocator::offsetOfFreeListHead()) \
79     macro(ScopedArguments_overrodeThings, ScopedArguments::offsetOfOverrodeThings()) \
80     macro(ScopedArguments_scope, ScopedArguments::offsetOfScope()) \
81     macro(ScopedArguments_table, ScopedArguments::offsetOfTable()) \
82     macro(ScopedArguments_totalLength, ScopedArguments::offsetOfTotalLength()) \
83     macro(ScopedArgumentsTable_arguments, ScopedArgumentsTable::offsetOfArguments()) \
84     macro(ScopedArgumentsTable_length, ScopedArgumentsTable::offsetOfLength()) \
85     macro(StringImpl_data, StringImpl::dataOffset()) \
86     macro(StringImpl_hashAndFlags, StringImpl::flagsOffset()) \
87     macro(Structure_classInfo, Structure::classInfoOffset()) \
88     macro(Structure_globalObject, Structure::globalObjectOffset()) \
89     macro(Structure_prototype, Structure::prototypeOffset()) \
90     macro(Structure_structureID, Structure::structureIDOffset()) \
91     macro(Symbol_privateName, Symbol::offsetOfPrivateName())
92
93 #define FOR_EACH_INDEXED_ABSTRACT_HEAP(macro) \
94     macro(DirectArguments_storage, DirectArguments::storageOffset(), sizeof(EncodedJSValue)) \
95     macro(JSEnvironmentRecord_variables, JSEnvironmentRecord::offsetOfVariables(), sizeof(EncodedJSValue)) \
96     macro(JSPropertyNameEnumerator_cachedPropertyNamesVectorContents, 0, sizeof(WriteBarrier<JSString>)) \
97     macro(JSRopeString_fibers, JSRopeString::offsetOfFibers(), sizeof(WriteBarrier<JSString>)) \
98     macro(MarkedSpace_Subspace_impreciseAllocators, OBJECT_OFFSETOF(MarkedSpace::Subspace, impreciseAllocators), sizeof(MarkedAllocator)) \
99     macro(MarkedSpace_Subspace_preciseAllocators, OBJECT_OFFSETOF(MarkedSpace::Subspace, preciseAllocators), sizeof(MarkedAllocator)) \
100     macro(ScopedArguments_overflowStorage, ScopedArguments::overflowStorageOffset(), sizeof(EncodedJSValue)) \
101     macro(WriteBarrierBuffer_bufferContents, 0, sizeof(JSCell*)) \
102     macro(characters8, 0, sizeof(LChar)) \
103     macro(characters16, 0, sizeof(UChar)) \
104     macro(indexedInt32Properties, 0, sizeof(EncodedJSValue)) \
105     macro(indexedDoubleProperties, 0, sizeof(double)) \
106     macro(indexedContiguousProperties, 0, sizeof(EncodedJSValue)) \
107     macro(indexedArrayStorageProperties, 0, sizeof(EncodedJSValue)) \
108     macro(scopedArgumentsTableArguments, 0, sizeof(int32_t)) \
109     macro(singleCharacterStrings, 0, sizeof(JSString*)) \
110     macro(structureTable, 0, sizeof(Structure*)) \
111     macro(variables, 0, sizeof(Register))
112     
113 #define FOR_EACH_NUMBERED_ABSTRACT_HEAP(macro) \
114     macro(properties)
115     
116 // This class is meant to be cacheable between compilations, but it doesn't have to be.
117 // Doing so saves on creation of nodes. But clearing it will save memory.
118
119 class AbstractHeapRepository {
120     WTF_MAKE_NONCOPYABLE(AbstractHeapRepository);
121 public:
122     AbstractHeapRepository();
123     ~AbstractHeapRepository();
124     
125     AbstractHeap root;
126     
127 #define ABSTRACT_HEAP_DECLARATION(name) AbstractHeap name;
128     FOR_EACH_ABSTRACT_HEAP(ABSTRACT_HEAP_DECLARATION)
129 #undef ABSTRACT_HEAP_DECLARATION
130
131 #define ABSTRACT_FIELD_DECLARATION(name, offset) AbstractHeap name;
132     FOR_EACH_ABSTRACT_FIELD(ABSTRACT_FIELD_DECLARATION)
133 #undef ABSTRACT_FIELD_DECLARATION
134     
135     AbstractHeap& JSCell_freeListNext;
136     
137 #define INDEXED_ABSTRACT_HEAP_DECLARATION(name, offset, size) IndexedAbstractHeap name;
138     FOR_EACH_INDEXED_ABSTRACT_HEAP(INDEXED_ABSTRACT_HEAP_DECLARATION)
139 #undef INDEXED_ABSTRACT_HEAP_DECLARATION
140     
141 #define NUMBERED_ABSTRACT_HEAP_DECLARATION(name) NumberedAbstractHeap name;
142     FOR_EACH_NUMBERED_ABSTRACT_HEAP(NUMBERED_ABSTRACT_HEAP_DECLARATION)
143 #undef NUMBERED_ABSTRACT_HEAP_DECLARATION
144
145     AbsoluteAbstractHeap absolute;
146     
147     IndexedAbstractHeap* forIndexingType(IndexingType indexingType)
148     {
149         switch (indexingType) {
150         case ALL_BLANK_INDEXING_TYPES:
151         case ALL_UNDECIDED_INDEXING_TYPES:
152             return 0;
153             
154         case ALL_INT32_INDEXING_TYPES:
155             return &indexedInt32Properties;
156             
157         case ALL_DOUBLE_INDEXING_TYPES:
158             return &indexedDoubleProperties;
159             
160         case ALL_CONTIGUOUS_INDEXING_TYPES:
161             return &indexedContiguousProperties;
162             
163         case ALL_ARRAY_STORAGE_INDEXING_TYPES:
164             return &indexedArrayStorageProperties;
165             
166         default:
167             RELEASE_ASSERT_NOT_REACHED();
168             return 0;
169         }
170     }
171     
172     IndexedAbstractHeap& forArrayType(DFG::Array::Type type)
173     {
174         switch (type) {
175         case DFG::Array::Int32:
176             return indexedInt32Properties;
177         case DFG::Array::Double:
178             return indexedDoubleProperties;
179         case DFG::Array::Contiguous:
180             return indexedContiguousProperties;
181         case DFG::Array::ArrayStorage:
182         case DFG::Array::SlowPutArrayStorage:
183             return indexedArrayStorageProperties;
184         default:
185             RELEASE_ASSERT_NOT_REACHED();
186             return indexedInt32Properties;
187         }
188     }
189
190     void decorateMemory(const AbstractHeap*, B3::Value*);
191     void decorateCCallRead(const AbstractHeap*, B3::Value*);
192     void decorateCCallWrite(const AbstractHeap*, B3::Value*);
193     void decoratePatchpointRead(const AbstractHeap*, B3::Value*);
194     void decoratePatchpointWrite(const AbstractHeap*, B3::Value*);
195
196     void computeRangesAndDecorateInstructions();
197
198 private:
199
200     struct HeapForValue {
201         HeapForValue()
202         {
203         }
204
205         HeapForValue(const AbstractHeap* heap, B3::Value* value)
206             : heap(heap)
207             , value(value)
208         {
209         }
210         
211         const AbstractHeap* heap { nullptr };
212         B3::Value* value { nullptr };
213     };
214
215     Vector<HeapForValue> m_heapForMemory;
216     Vector<HeapForValue> m_heapForCCallRead;
217     Vector<HeapForValue> m_heapForCCallWrite;
218     Vector<HeapForValue> m_heapForPatchpointRead;
219     Vector<HeapForValue> m_heapForPatchpointWrite;
220 };
221
222 } } // namespace JSC::FTL
223
224 #endif // ENABLE(FTL_JIT)
225
226 #endif // FTLAbstractHeapRepository_h
227