Source/JavaScriptCore:
[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_header, 0) \
58     macro(JSCell_indexingType, JSCell::indexingTypeOffset()) \
59     macro(JSCell_structureID, JSCell::structureIDOffset()) \
60     macro(JSCell_typeInfoFlags, JSCell::typeInfoFlagsOffset()) \
61     macro(JSCell_typeInfoType, JSCell::typeInfoTypeOffset()) \
62     macro(JSCell_usefulBytes, JSCell::indexingTypeOffset()) \
63     macro(JSFunction_executable, JSFunction::offsetOfExecutable()) \
64     macro(JSFunction_scope, JSFunction::offsetOfScopeChain()) \
65     macro(JSFunction_rareData, JSFunction::offsetOfRareData()) \
66     macro(JSObject_butterfly, JSObject::butterflyOffset()) \
67     macro(JSPropertyNameEnumerator_cachedInlineCapacity, JSPropertyNameEnumerator::cachedInlineCapacityOffset()) \
68     macro(JSPropertyNameEnumerator_cachedPropertyNamesVector, JSPropertyNameEnumerator::cachedPropertyNamesVectorOffset()) \
69     macro(JSPropertyNameEnumerator_cachedStructureID, JSPropertyNameEnumerator::cachedStructureIDOffset()) \
70     macro(JSPropertyNameEnumerator_endGenericPropertyIndex, JSPropertyNameEnumerator::endGenericPropertyIndexOffset()) \
71     macro(JSPropertyNameEnumerator_endStructurePropertyIndex, JSPropertyNameEnumerator::endStructurePropertyIndexOffset()) \
72     macro(JSPropertyNameEnumerator_indexLength, JSPropertyNameEnumerator::indexedLengthOffset()) \
73     macro(JSScope_next, JSScope::offsetOfNext()) \
74     macro(JSString_flags, JSString::offsetOfFlags()) \
75     macro(JSString_length, JSString::offsetOfLength()) \
76     macro(JSString_value, JSString::offsetOfValue()) \
77     macro(JSSymbolTableObject_symbolTable, JSSymbolTableObject::offsetOfSymbolTable()) \
78     macro(JSWrapperObject_internalValue, JSWrapperObject::internalValueOffset()) \
79     macro(MarkedAllocator_freeListHead, MarkedAllocator::offsetOfFreeListHead()) \
80     macro(RegExpConstructor_cachedResult_lastRegExp, RegExpConstructor::offsetOfCachedResult() + RegExpCachedResult::offsetOfLastRegExp()) \
81     macro(RegExpConstructor_cachedResult_lastInput, RegExpConstructor::offsetOfCachedResult() + RegExpCachedResult::offsetOfLastInput()) \
82     macro(RegExpConstructor_cachedResult_result_start, RegExpConstructor::offsetOfCachedResult() + RegExpCachedResult::offsetOfResult() + OBJECT_OFFSETOF(MatchResult, start)) \
83     macro(RegExpConstructor_cachedResult_result_end, RegExpConstructor::offsetOfCachedResult() + RegExpCachedResult::offsetOfResult() + OBJECT_OFFSETOF(MatchResult, end)) \
84     macro(RegExpConstructor_cachedResult_reified, RegExpConstructor::offsetOfCachedResult() + RegExpCachedResult::offsetOfReified()) \
85     macro(RegExpObject_lastIndex, RegExpObject::offsetOfLastIndex()) \
86     macro(RegExpObject_lastIndexIsWritable, RegExpObject::offsetOfLastIndexIsWritable()) \
87     macro(ScopedArguments_overrodeThings, ScopedArguments::offsetOfOverrodeThings()) \
88     macro(ScopedArguments_scope, ScopedArguments::offsetOfScope()) \
89     macro(ScopedArguments_table, ScopedArguments::offsetOfTable()) \
90     macro(ScopedArguments_totalLength, ScopedArguments::offsetOfTotalLength()) \
91     macro(ScopedArgumentsTable_arguments, ScopedArgumentsTable::offsetOfArguments()) \
92     macro(ScopedArgumentsTable_length, ScopedArgumentsTable::offsetOfLength()) \
93     macro(StringImpl_data, StringImpl::dataOffset()) \
94     macro(StringImpl_hashAndFlags, StringImpl::flagsOffset()) \
95     macro(Structure_classInfo, Structure::classInfoOffset()) \
96     macro(Structure_globalObject, Structure::globalObjectOffset()) \
97     macro(Structure_prototype, Structure::prototypeOffset()) \
98     macro(Structure_structureID, Structure::structureIDOffset()) \
99     macro(Symbol_privateName, Symbol::offsetOfPrivateName())
100
101 #define FOR_EACH_INDEXED_ABSTRACT_HEAP(macro) \
102     macro(DirectArguments_storage, DirectArguments::storageOffset(), sizeof(EncodedJSValue)) \
103     macro(JSEnvironmentRecord_variables, JSEnvironmentRecord::offsetOfVariables(), sizeof(EncodedJSValue)) \
104     macro(JSPropertyNameEnumerator_cachedPropertyNamesVectorContents, 0, sizeof(WriteBarrier<JSString>)) \
105     macro(JSRopeString_fibers, JSRopeString::offsetOfFibers(), sizeof(WriteBarrier<JSString>)) \
106     macro(MarkedSpace_Subspace_impreciseAllocators, OBJECT_OFFSETOF(MarkedSpace::Subspace, impreciseAllocators), sizeof(MarkedAllocator)) \
107     macro(MarkedSpace_Subspace_preciseAllocators, OBJECT_OFFSETOF(MarkedSpace::Subspace, preciseAllocators), sizeof(MarkedAllocator)) \
108     macro(ScopedArguments_overflowStorage, ScopedArguments::overflowStorageOffset(), sizeof(EncodedJSValue)) \
109     macro(WriteBarrierBuffer_bufferContents, 0, sizeof(JSCell*)) \
110     macro(characters8, 0, sizeof(LChar)) \
111     macro(characters16, 0, sizeof(UChar)) \
112     macro(indexedInt32Properties, 0, sizeof(EncodedJSValue)) \
113     macro(indexedDoubleProperties, 0, sizeof(double)) \
114     macro(indexedContiguousProperties, 0, sizeof(EncodedJSValue)) \
115     macro(indexedArrayStorageProperties, 0, sizeof(EncodedJSValue)) \
116     macro(scopedArgumentsTableArguments, 0, sizeof(int32_t)) \
117     macro(singleCharacterStrings, 0, sizeof(JSString*)) \
118     macro(structureTable, 0, sizeof(Structure*)) \
119     macro(variables, 0, sizeof(Register))
120     
121 #define FOR_EACH_NUMBERED_ABSTRACT_HEAP(macro) \
122     macro(properties)
123     
124 // This class is meant to be cacheable between compilations, but it doesn't have to be.
125 // Doing so saves on creation of nodes. But clearing it will save memory.
126
127 class AbstractHeapRepository {
128     WTF_MAKE_NONCOPYABLE(AbstractHeapRepository);
129 public:
130     AbstractHeapRepository();
131     ~AbstractHeapRepository();
132     
133     AbstractHeap root;
134     
135 #define ABSTRACT_HEAP_DECLARATION(name) AbstractHeap name;
136     FOR_EACH_ABSTRACT_HEAP(ABSTRACT_HEAP_DECLARATION)
137 #undef ABSTRACT_HEAP_DECLARATION
138
139 #define ABSTRACT_FIELD_DECLARATION(name, offset) AbstractHeap name;
140     FOR_EACH_ABSTRACT_FIELD(ABSTRACT_FIELD_DECLARATION)
141 #undef ABSTRACT_FIELD_DECLARATION
142     
143     AbstractHeap& JSCell_freeListNext;
144     
145 #define INDEXED_ABSTRACT_HEAP_DECLARATION(name, offset, size) IndexedAbstractHeap name;
146     FOR_EACH_INDEXED_ABSTRACT_HEAP(INDEXED_ABSTRACT_HEAP_DECLARATION)
147 #undef INDEXED_ABSTRACT_HEAP_DECLARATION
148     
149 #define NUMBERED_ABSTRACT_HEAP_DECLARATION(name) NumberedAbstractHeap name;
150     FOR_EACH_NUMBERED_ABSTRACT_HEAP(NUMBERED_ABSTRACT_HEAP_DECLARATION)
151 #undef NUMBERED_ABSTRACT_HEAP_DECLARATION
152
153     AbsoluteAbstractHeap absolute;
154     
155     IndexedAbstractHeap* forIndexingType(IndexingType indexingType)
156     {
157         switch (indexingType) {
158         case ALL_BLANK_INDEXING_TYPES:
159         case ALL_UNDECIDED_INDEXING_TYPES:
160             return 0;
161             
162         case ALL_INT32_INDEXING_TYPES:
163             return &indexedInt32Properties;
164             
165         case ALL_DOUBLE_INDEXING_TYPES:
166             return &indexedDoubleProperties;
167             
168         case ALL_CONTIGUOUS_INDEXING_TYPES:
169             return &indexedContiguousProperties;
170             
171         case ALL_ARRAY_STORAGE_INDEXING_TYPES:
172             return &indexedArrayStorageProperties;
173             
174         default:
175             RELEASE_ASSERT_NOT_REACHED();
176             return 0;
177         }
178     }
179     
180     IndexedAbstractHeap& forArrayType(DFG::Array::Type type)
181     {
182         switch (type) {
183         case DFG::Array::Int32:
184             return indexedInt32Properties;
185         case DFG::Array::Double:
186             return indexedDoubleProperties;
187         case DFG::Array::Contiguous:
188             return indexedContiguousProperties;
189         case DFG::Array::ArrayStorage:
190         case DFG::Array::SlowPutArrayStorage:
191             return indexedArrayStorageProperties;
192         default:
193             RELEASE_ASSERT_NOT_REACHED();
194             return indexedInt32Properties;
195         }
196     }
197
198     void decorateMemory(const AbstractHeap*, B3::Value*);
199     void decorateCCallRead(const AbstractHeap*, B3::Value*);
200     void decorateCCallWrite(const AbstractHeap*, B3::Value*);
201     void decoratePatchpointRead(const AbstractHeap*, B3::Value*);
202     void decoratePatchpointWrite(const AbstractHeap*, B3::Value*);
203
204     void computeRangesAndDecorateInstructions();
205
206 private:
207
208     struct HeapForValue {
209         HeapForValue()
210         {
211         }
212
213         HeapForValue(const AbstractHeap* heap, B3::Value* value)
214             : heap(heap)
215             , value(value)
216         {
217         }
218         
219         const AbstractHeap* heap { nullptr };
220         B3::Value* value { nullptr };
221     };
222
223     Vector<HeapForValue> m_heapForMemory;
224     Vector<HeapForValue> m_heapForCCallRead;
225     Vector<HeapForValue> m_heapForCCallWrite;
226     Vector<HeapForValue> m_heapForPatchpointRead;
227     Vector<HeapForValue> m_heapForPatchpointWrite;
228 };
229
230 } } // namespace JSC::FTL
231
232 #endif // ENABLE(FTL_JIT)
233
234 #endif // FTLAbstractHeapRepository_h
235