fe6b1c620444788781616a74cbfec826095381a9
[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 #pragma once
27
28 #if ENABLE(FTL_JIT)
29
30 #include "B3Value.h"
31 #include "DFGArrayMode.h"
32 #include "FTLAbstractHeap.h"
33 #include "HasOwnPropertyCache.h"
34 #include "IndexingType.h"
35 #include "JSMap.h"
36 #include "JSSet.h"
37 #include "Symbol.h"
38
39 namespace JSC { namespace FTL {
40
41 #define FOR_EACH_ABSTRACT_HEAP(macro) \
42     macro(typedArrayProperties)
43
44 #define FOR_EACH_ABSTRACT_FIELD(macro) \
45     macro(ArrayBuffer_data, ArrayBuffer::offsetOfData()) \
46     macro(Butterfly_arrayBuffer, Butterfly::offsetOfArrayBuffer()) \
47     macro(Butterfly_publicLength, Butterfly::offsetOfPublicLength()) \
48     macro(Butterfly_vectorLength, Butterfly::offsetOfVectorLength()) \
49     macro(CallFrame_callerFrame, CallFrame::callerFrameOffset()) \
50     macro(DirectArguments_callee, DirectArguments::offsetOfCallee()) \
51     macro(DirectArguments_length, DirectArguments::offsetOfLength()) \
52     macro(DirectArguments_minCapacity, DirectArguments::offsetOfMinCapacity()) \
53     macro(DirectArguments_overrides, DirectArguments::offsetOfOverrides()) \
54     macro(GetterSetter_getter, GetterSetter::offsetOfGetter()) \
55     macro(GetterSetter_setter, GetterSetter::offsetOfSetter()) \
56     macro(JSArrayBufferView_length, JSArrayBufferView::offsetOfLength()) \
57     macro(JSArrayBufferView_mode, JSArrayBufferView::offsetOfMode()) \
58     macro(JSArrayBufferView_vector, JSArrayBufferView::offsetOfVector()) \
59     macro(JSCell_cellState, JSCell::cellStateOffset()) \
60     macro(JSCell_header, 0) \
61     macro(JSCell_indexingType, JSCell::indexingTypeOffset()) \
62     macro(JSCell_structureID, JSCell::structureIDOffset()) \
63     macro(JSCell_typeInfoFlags, JSCell::typeInfoFlagsOffset()) \
64     macro(JSCell_typeInfoType, JSCell::typeInfoTypeOffset()) \
65     macro(JSCell_usefulBytes, JSCell::indexingTypeOffset()) \
66     macro(JSFunction_executable, JSFunction::offsetOfExecutable()) \
67     macro(JSFunction_scope, JSFunction::offsetOfScopeChain()) \
68     macro(JSFunction_rareData, JSFunction::offsetOfRareData()) \
69     macro(JSObject_butterfly, JSObject::butterflyOffset()) \
70     macro(JSPropertyNameEnumerator_cachedInlineCapacity, JSPropertyNameEnumerator::cachedInlineCapacityOffset()) \
71     macro(JSPropertyNameEnumerator_cachedPropertyNamesVector, JSPropertyNameEnumerator::cachedPropertyNamesVectorOffset()) \
72     macro(JSPropertyNameEnumerator_cachedStructureID, JSPropertyNameEnumerator::cachedStructureIDOffset()) \
73     macro(JSPropertyNameEnumerator_endGenericPropertyIndex, JSPropertyNameEnumerator::endGenericPropertyIndexOffset()) \
74     macro(JSPropertyNameEnumerator_endStructurePropertyIndex, JSPropertyNameEnumerator::endStructurePropertyIndexOffset()) \
75     macro(JSPropertyNameEnumerator_indexLength, JSPropertyNameEnumerator::indexedLengthOffset()) \
76     macro(JSScope_next, JSScope::offsetOfNext()) \
77     macro(JSString_flags, JSString::offsetOfFlags()) \
78     macro(JSString_length, JSString::offsetOfLength()) \
79     macro(JSString_value, JSString::offsetOfValue()) \
80     macro(JSSymbolTableObject_symbolTable, JSSymbolTableObject::offsetOfSymbolTable()) \
81     macro(JSWrapperObject_internalValue, JSWrapperObject::internalValueOffset()) \
82     macro(RegExpConstructor_cachedResult_lastRegExp, RegExpConstructor::offsetOfCachedResult() + RegExpCachedResult::offsetOfLastRegExp()) \
83     macro(RegExpConstructor_cachedResult_lastInput, RegExpConstructor::offsetOfCachedResult() + RegExpCachedResult::offsetOfLastInput()) \
84     macro(RegExpConstructor_cachedResult_result_start, RegExpConstructor::offsetOfCachedResult() + RegExpCachedResult::offsetOfResult() + OBJECT_OFFSETOF(MatchResult, start)) \
85     macro(RegExpConstructor_cachedResult_result_end, RegExpConstructor::offsetOfCachedResult() + RegExpCachedResult::offsetOfResult() + OBJECT_OFFSETOF(MatchResult, end)) \
86     macro(RegExpConstructor_cachedResult_reified, RegExpConstructor::offsetOfCachedResult() + RegExpCachedResult::offsetOfReified()) \
87     macro(RegExpObject_lastIndex, RegExpObject::offsetOfLastIndex()) \
88     macro(RegExpObject_lastIndexIsWritable, RegExpObject::offsetOfLastIndexIsWritable()) \
89     macro(ShadowChicken_Packet_callee, OBJECT_OFFSETOF(ShadowChicken::Packet, callee)) \
90     macro(ShadowChicken_Packet_frame, OBJECT_OFFSETOF(ShadowChicken::Packet, frame)) \
91     macro(ShadowChicken_Packet_callerFrame, OBJECT_OFFSETOF(ShadowChicken::Packet, callerFrame)) \
92     macro(ShadowChicken_Packet_thisValue, OBJECT_OFFSETOF(ShadowChicken::Packet, thisValue)) \
93     macro(ShadowChicken_Packet_scope, OBJECT_OFFSETOF(ShadowChicken::Packet, scope)) \
94     macro(ShadowChicken_Packet_codeBlock, OBJECT_OFFSETOF(ShadowChicken::Packet, codeBlock)) \
95     macro(ShadowChicken_Packet_callSiteIndex, OBJECT_OFFSETOF(ShadowChicken::Packet, callSiteIndex)) \
96     macro(ScopedArguments_overrodeThings, ScopedArguments::offsetOfOverrodeThings()) \
97     macro(ScopedArguments_scope, ScopedArguments::offsetOfScope()) \
98     macro(ScopedArguments_table, ScopedArguments::offsetOfTable()) \
99     macro(ScopedArguments_totalLength, ScopedArguments::offsetOfTotalLength()) \
100     macro(ScopedArgumentsTable_arguments, ScopedArgumentsTable::offsetOfArguments()) \
101     macro(ScopedArgumentsTable_length, ScopedArgumentsTable::offsetOfLength()) \
102     macro(StringImpl_data, StringImpl::dataOffset()) \
103     macro(StringImpl_hashAndFlags, StringImpl::flagsOffset()) \
104     macro(StringImpl_length, StringImpl::lengthMemoryOffset()) \
105     macro(Structure_classInfo, Structure::classInfoOffset()) \
106     macro(Structure_globalObject, Structure::globalObjectOffset()) \
107     macro(Structure_prototype, Structure::prototypeOffset()) \
108     macro(Structure_structureID, Structure::structureIDOffset()) \
109     macro(JSMap_hashMapImpl, JSMap::offsetOfHashMapImpl()) \
110     macro(JSSet_hashMapImpl, JSSet::offsetOfHashMapImpl()) \
111     macro(HashMapImpl_capacity, HashMapImpl<HashMapBucket<HashMapBucketDataKey>>::offsetOfCapacity()) \
112     macro(HashMapImpl_buffer,  HashMapImpl<HashMapBucket<HashMapBucketDataKey>>::offsetOfBuffer()) \
113     macro(HashMapBucket_value, HashMapBucket<HashMapBucketDataKeyValue>::offsetOfValue()) \
114     macro(HashMapBucket_key, HashMapBucket<HashMapBucketDataKeyValue>::offsetOfKey()) \
115     macro(Symbol_symbolImpl, Symbol::offsetOfSymbolImpl()) \
116
117 #define FOR_EACH_INDEXED_ABSTRACT_HEAP(macro) \
118     macro(DirectArguments_storage, DirectArguments::storageOffset(), sizeof(EncodedJSValue)) \
119     macro(JSEnvironmentRecord_variables, JSEnvironmentRecord::offsetOfVariables(), sizeof(EncodedJSValue)) \
120     macro(JSPropertyNameEnumerator_cachedPropertyNamesVectorContents, 0, sizeof(WriteBarrier<JSString>)) \
121     macro(JSRopeString_fibers, JSRopeString::offsetOfFibers(), sizeof(WriteBarrier<JSString>)) \
122     macro(MarkedSpace_Subspace_allocatorForSizeStep, OBJECT_OFFSETOF(MarkedSpace::Subspace, allocatorForSizeStep), sizeof(MarkedAllocator*)) \
123     macro(ScopedArguments_overflowStorage, ScopedArguments::overflowStorageOffset(), sizeof(EncodedJSValue)) \
124     macro(WriteBarrierBuffer_bufferContents, 0, sizeof(JSCell*)) \
125     macro(characters8, 0, sizeof(LChar)) \
126     macro(characters16, 0, sizeof(UChar)) \
127     macro(indexedInt32Properties, 0, sizeof(EncodedJSValue)) \
128     macro(indexedDoubleProperties, 0, sizeof(double)) \
129     macro(indexedContiguousProperties, 0, sizeof(EncodedJSValue)) \
130     macro(indexedArrayStorageProperties, 0, sizeof(EncodedJSValue)) \
131     macro(scopedArgumentsTableArguments, 0, sizeof(int32_t)) \
132     macro(singleCharacterStrings, 0, sizeof(JSString*)) \
133     macro(structureTable, 0, sizeof(Structure*)) \
134     macro(variables, 0, sizeof(Register)) \
135     macro(HasOwnPropertyCache, 0, sizeof(HasOwnPropertyCache::Entry)) \
136     
137 #define FOR_EACH_NUMBERED_ABSTRACT_HEAP(macro) \
138     macro(properties)
139     
140 // This class is meant to be cacheable between compilations, but it doesn't have to be.
141 // Doing so saves on creation of nodes. But clearing it will save memory.
142
143 class AbstractHeapRepository {
144     WTF_MAKE_NONCOPYABLE(AbstractHeapRepository);
145 public:
146     AbstractHeapRepository();
147     ~AbstractHeapRepository();
148     
149     AbstractHeap root;
150     
151 #define ABSTRACT_HEAP_DECLARATION(name) AbstractHeap name;
152     FOR_EACH_ABSTRACT_HEAP(ABSTRACT_HEAP_DECLARATION)
153 #undef ABSTRACT_HEAP_DECLARATION
154
155 #define ABSTRACT_FIELD_DECLARATION(name, offset) AbstractHeap name;
156     FOR_EACH_ABSTRACT_FIELD(ABSTRACT_FIELD_DECLARATION)
157 #undef ABSTRACT_FIELD_DECLARATION
158     
159     AbstractHeap& JSCell_freeListNext;
160     
161 #define INDEXED_ABSTRACT_HEAP_DECLARATION(name, offset, size) IndexedAbstractHeap name;
162     FOR_EACH_INDEXED_ABSTRACT_HEAP(INDEXED_ABSTRACT_HEAP_DECLARATION)
163 #undef INDEXED_ABSTRACT_HEAP_DECLARATION
164     
165 #define NUMBERED_ABSTRACT_HEAP_DECLARATION(name) NumberedAbstractHeap name;
166     FOR_EACH_NUMBERED_ABSTRACT_HEAP(NUMBERED_ABSTRACT_HEAP_DECLARATION)
167 #undef NUMBERED_ABSTRACT_HEAP_DECLARATION
168
169     AbsoluteAbstractHeap absolute;
170     
171     IndexedAbstractHeap* forIndexingType(IndexingType indexingType)
172     {
173         switch (indexingType) {
174         case ALL_BLANK_INDEXING_TYPES:
175         case ALL_UNDECIDED_INDEXING_TYPES:
176             return 0;
177             
178         case ALL_INT32_INDEXING_TYPES:
179             return &indexedInt32Properties;
180             
181         case ALL_DOUBLE_INDEXING_TYPES:
182             return &indexedDoubleProperties;
183             
184         case ALL_CONTIGUOUS_INDEXING_TYPES:
185             return &indexedContiguousProperties;
186             
187         case ALL_ARRAY_STORAGE_INDEXING_TYPES:
188             return &indexedArrayStorageProperties;
189             
190         default:
191             RELEASE_ASSERT_NOT_REACHED();
192             return 0;
193         }
194     }
195     
196     IndexedAbstractHeap& forArrayType(DFG::Array::Type type)
197     {
198         switch (type) {
199         case DFG::Array::Int32:
200             return indexedInt32Properties;
201         case DFG::Array::Double:
202             return indexedDoubleProperties;
203         case DFG::Array::Contiguous:
204             return indexedContiguousProperties;
205         case DFG::Array::ArrayStorage:
206         case DFG::Array::SlowPutArrayStorage:
207             return indexedArrayStorageProperties;
208         default:
209             RELEASE_ASSERT_NOT_REACHED();
210             return indexedInt32Properties;
211         }
212     }
213
214     void decorateMemory(const AbstractHeap*, B3::Value*);
215     void decorateCCallRead(const AbstractHeap*, B3::Value*);
216     void decorateCCallWrite(const AbstractHeap*, B3::Value*);
217     void decoratePatchpointRead(const AbstractHeap*, B3::Value*);
218     void decoratePatchpointWrite(const AbstractHeap*, B3::Value*);
219     void decorateFenceRead(const AbstractHeap*, B3::Value*);
220     void decorateFenceWrite(const AbstractHeap*, B3::Value*);
221
222     void computeRangesAndDecorateInstructions();
223
224 private:
225
226     struct HeapForValue {
227         HeapForValue()
228         {
229         }
230
231         HeapForValue(const AbstractHeap* heap, B3::Value* value)
232             : heap(heap)
233             , value(value)
234         {
235         }
236         
237         const AbstractHeap* heap { nullptr };
238         B3::Value* value { nullptr };
239     };
240
241     Vector<HeapForValue> m_heapForMemory;
242     Vector<HeapForValue> m_heapForCCallRead;
243     Vector<HeapForValue> m_heapForCCallWrite;
244     Vector<HeapForValue> m_heapForPatchpointRead;
245     Vector<HeapForValue> m_heapForPatchpointWrite;
246     Vector<HeapForValue> m_heapForFenceRead;
247     Vector<HeapForValue> m_heapForFenceWrite;
248 };
249
250 } } // namespace JSC::FTL
251
252 #endif // ENABLE(FTL_JIT)