We should support CreateThis in the FTL
[WebKit-https.git] / Source / JavaScriptCore / ftl / FTLAbstractHeapRepository.h
1 /*
2  * Copyright (C) 2013-2018 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 "JSFixedArray.h"
36 #include "JSMap.h"
37 #include "JSSet.h"
38 #include "JSWeakMap.h"
39 #include "Symbol.h"
40
41 namespace JSC { namespace FTL {
42
43 #define FOR_EACH_ABSTRACT_HEAP(macro) \
44     macro(typedArrayProperties)
45
46 #define FOR_EACH_ABSTRACT_FIELD(macro) \
47     macro(ArrayBuffer_data, ArrayBuffer::offsetOfData()) \
48     macro(ArrayStorage_numValuesInVector, ArrayStorage::numValuesInVectorOffset()) \
49     macro(Butterfly_arrayBuffer, Butterfly::offsetOfArrayBuffer()) \
50     macro(Butterfly_publicLength, Butterfly::offsetOfPublicLength()) \
51     macro(Butterfly_vectorLength, Butterfly::offsetOfVectorLength()) \
52     macro(CallFrame_callerFrame, CallFrame::callerFrameOffset()) \
53     macro(ClassInfo_parentClass, ClassInfo::offsetOfParentClass()) \
54     macro(DirectArguments_callee, DirectArguments::offsetOfCallee()) \
55     macro(DirectArguments_length, DirectArguments::offsetOfLength()) \
56     macro(DirectArguments_minCapacity, DirectArguments::offsetOfMinCapacity()) \
57     macro(DirectArguments_mappedArguments, DirectArguments::offsetOfMappedArguments()) \
58     macro(DirectArguments_modifiedArgumentsDescriptor, DirectArguments::offsetOfModifiedArgumentsDescriptor()) \
59     macro(FunctionRareData_allocator, FunctionRareData::offsetOfObjectAllocationProfile() + ObjectAllocationProfile::offsetOfAllocator()) \
60     macro(FunctionRareData_structure, FunctionRareData::offsetOfObjectAllocationProfile() + ObjectAllocationProfile::offsetOfStructure()) \
61     macro(GetterSetter_getter, GetterSetter::offsetOfGetter()) \
62     macro(GetterSetter_setter, GetterSetter::offsetOfSetter()) \
63     macro(JSArrayBufferView_length, JSArrayBufferView::offsetOfLength()) \
64     macro(JSArrayBufferView_mode, JSArrayBufferView::offsetOfMode()) \
65     macro(JSArrayBufferView_vector, JSArrayBufferView::offsetOfVector()) \
66     macro(JSCell_cellState, JSCell::cellStateOffset()) \
67     macro(JSCell_header, 0) \
68     macro(JSCell_indexingTypeAndMisc, JSCell::indexingTypeAndMiscOffset()) \
69     macro(JSCell_structureID, JSCell::structureIDOffset()) \
70     macro(JSCell_typeInfoFlags, JSCell::typeInfoFlagsOffset()) \
71     macro(JSCell_typeInfoType, JSCell::typeInfoTypeOffset()) \
72     macro(JSCell_usefulBytes, JSCell::indexingTypeAndMiscOffset()) \
73     macro(JSDestructibleObject_classInfo, JSDestructibleObject::classInfoOffset()) \
74     macro(JSFunction_executable, JSFunction::offsetOfExecutable()) \
75     macro(JSFunction_scope, JSFunction::offsetOfScopeChain()) \
76     macro(JSFunction_rareData, JSFunction::offsetOfRareData()) \
77     macro(JSObject_butterfly, JSObject::butterflyOffset()) \
78     macro(JSPropertyNameEnumerator_cachedInlineCapacity, JSPropertyNameEnumerator::cachedInlineCapacityOffset()) \
79     macro(JSPropertyNameEnumerator_cachedPropertyNamesVector, JSPropertyNameEnumerator::cachedPropertyNamesVectorOffset()) \
80     macro(JSPropertyNameEnumerator_cachedStructureID, JSPropertyNameEnumerator::cachedStructureIDOffset()) \
81     macro(JSPropertyNameEnumerator_endGenericPropertyIndex, JSPropertyNameEnumerator::endGenericPropertyIndexOffset()) \
82     macro(JSPropertyNameEnumerator_endStructurePropertyIndex, JSPropertyNameEnumerator::endStructurePropertyIndexOffset()) \
83     macro(JSPropertyNameEnumerator_indexLength, JSPropertyNameEnumerator::indexedLengthOffset()) \
84     macro(JSScope_next, JSScope::offsetOfNext()) \
85     macro(JSString_flags, JSString::offsetOfFlags()) \
86     macro(JSString_length, JSString::offsetOfLength()) \
87     macro(JSString_value, JSString::offsetOfValue()) \
88     macro(JSSymbolTableObject_symbolTable, JSSymbolTableObject::offsetOfSymbolTable()) \
89     macro(JSWrapperObject_internalValue, JSWrapperObject::internalValueOffset()) \
90     macro(RegExpConstructor_cachedResult_lastRegExp, RegExpConstructor::offsetOfCachedResult() + RegExpCachedResult::offsetOfLastRegExp()) \
91     macro(RegExpConstructor_cachedResult_lastInput, RegExpConstructor::offsetOfCachedResult() + RegExpCachedResult::offsetOfLastInput()) \
92     macro(RegExpConstructor_cachedResult_result_start, RegExpConstructor::offsetOfCachedResult() + RegExpCachedResult::offsetOfResult() + OBJECT_OFFSETOF(MatchResult, start)) \
93     macro(RegExpConstructor_cachedResult_result_end, RegExpConstructor::offsetOfCachedResult() + RegExpCachedResult::offsetOfResult() + OBJECT_OFFSETOF(MatchResult, end)) \
94     macro(RegExpConstructor_cachedResult_reified, RegExpConstructor::offsetOfCachedResult() + RegExpCachedResult::offsetOfReified()) \
95     macro(RegExpObject_regExp, RegExpObject::offsetOfRegExp()) \
96     macro(RegExpObject_lastIndex, RegExpObject::offsetOfLastIndex()) \
97     macro(RegExpObject_lastIndexIsWritable, RegExpObject::offsetOfLastIndexIsWritable()) \
98     macro(ShadowChicken_Packet_callee, OBJECT_OFFSETOF(ShadowChicken::Packet, callee)) \
99     macro(ShadowChicken_Packet_frame, OBJECT_OFFSETOF(ShadowChicken::Packet, frame)) \
100     macro(ShadowChicken_Packet_callerFrame, OBJECT_OFFSETOF(ShadowChicken::Packet, callerFrame)) \
101     macro(ShadowChicken_Packet_thisValue, OBJECT_OFFSETOF(ShadowChicken::Packet, thisValue)) \
102     macro(ShadowChicken_Packet_scope, OBJECT_OFFSETOF(ShadowChicken::Packet, scope)) \
103     macro(ShadowChicken_Packet_codeBlock, OBJECT_OFFSETOF(ShadowChicken::Packet, codeBlock)) \
104     macro(ShadowChicken_Packet_callSiteIndex, OBJECT_OFFSETOF(ShadowChicken::Packet, callSiteIndex)) \
105     macro(ScopedArguments_Storage_overrodeThings, ScopedArguments::offsetOfOverrodeThingsInStorage()) \
106     macro(ScopedArguments_Storage_totalLength, ScopedArguments::offsetOfTotalLengthInStorage()) \
107     macro(ScopedArguments_storage, ScopedArguments::offsetOfStorage()) \
108     macro(ScopedArguments_scope, ScopedArguments::offsetOfScope()) \
109     macro(ScopedArguments_table, ScopedArguments::offsetOfTable()) \
110     macro(ScopedArgumentsTable_arguments, ScopedArgumentsTable::offsetOfArguments()) \
111     macro(ScopedArgumentsTable_length, ScopedArgumentsTable::offsetOfLength()) \
112     macro(StringImpl_data, StringImpl::dataOffset()) \
113     macro(StringImpl_hashAndFlags, StringImpl::flagsOffset()) \
114     macro(StringImpl_length, StringImpl::lengthMemoryOffset()) \
115     macro(Structure_classInfo, Structure::classInfoOffset()) \
116     macro(Structure_globalObject, Structure::globalObjectOffset()) \
117     macro(Structure_indexingModeIncludingHistory, Structure::indexingModeIncludingHistoryOffset()) \
118     macro(Structure_inlineCapacity, Structure::inlineCapacityOffset()) \
119     macro(Structure_prototype, Structure::prototypeOffset()) \
120     macro(Structure_structureID, Structure::structureIDOffset()) \
121     macro(HashMapImpl_capacity, HashMapImpl<HashMapBucket<HashMapBucketDataKey>>::offsetOfCapacity()) \
122     macro(HashMapImpl_buffer,  HashMapImpl<HashMapBucket<HashMapBucketDataKey>>::offsetOfBuffer()) \
123     macro(HashMapImpl_head,  HashMapImpl<HashMapBucket<HashMapBucketDataKey>>::offsetOfHead()) \
124     macro(HashMapBucket_value, HashMapBucket<HashMapBucketDataKeyValue>::offsetOfValue()) \
125     macro(HashMapBucket_key, HashMapBucket<HashMapBucketDataKeyValue>::offsetOfKey()) \
126     macro(HashMapBucket_next, HashMapBucket<HashMapBucketDataKeyValue>::offsetOfNext()) \
127     macro(WeakMapImpl_capacity, WeakMapImpl<WeakMapBucket<WeakMapBucketDataKey>>::offsetOfCapacity()) \
128     macro(WeakMapImpl_buffer,  WeakMapImpl<WeakMapBucket<WeakMapBucketDataKey>>::offsetOfBuffer()) \
129     macro(WeakMapBucket_value, WeakMapBucket<WeakMapBucketDataKeyValue>::offsetOfValue()) \
130     macro(WeakMapBucket_key, WeakMapBucket<WeakMapBucketDataKeyValue>::offsetOfKey()) \
131     macro(Symbol_symbolImpl, Symbol::offsetOfSymbolImpl()) \
132     macro(JSFixedArray_size, JSFixedArray::offsetOfSize()) \
133
134 #define FOR_EACH_INDEXED_ABSTRACT_HEAP(macro) \
135     macro(ArrayStorage_vector, ArrayStorage::vectorOffset(), sizeof(WriteBarrier<Unknown>)) \
136     macro(CompleteSubspace_allocatorForSizeStep, CompleteSubspace::offsetOfAllocatorForSizeStep(), sizeof(Allocator)) \
137     macro(DirectArguments_storage, DirectArguments::storageOffset(), sizeof(EncodedJSValue)) \
138     macro(JSLexicalEnvironment_variables, JSLexicalEnvironment::offsetOfVariables(), sizeof(EncodedJSValue)) \
139     macro(JSPropertyNameEnumerator_cachedPropertyNamesVectorContents, 0, sizeof(WriteBarrier<JSString>)) \
140     macro(JSRopeString_fibers, JSRopeString::offsetOfFibers(), sizeof(WriteBarrier<JSString>)) \
141     macro(ScopedArguments_Storage_storage, 0, sizeof(EncodedJSValue)) \
142     macro(WriteBarrierBuffer_bufferContents, 0, sizeof(JSCell*)) \
143     macro(characters8, 0, sizeof(LChar)) \
144     macro(characters16, 0, sizeof(UChar)) \
145     macro(indexedInt32Properties, 0, sizeof(EncodedJSValue)) \
146     macro(indexedDoubleProperties, 0, sizeof(double)) \
147     macro(indexedContiguousProperties, 0, sizeof(EncodedJSValue)) \
148     macro(scopedArgumentsTableArguments, 0, sizeof(int32_t)) \
149     macro(singleCharacterStrings, 0, sizeof(JSString*)) \
150     macro(structureTable, 0, sizeof(Structure*)) \
151     macro(variables, 0, sizeof(Register)) \
152     macro(HasOwnPropertyCache, 0, sizeof(HasOwnPropertyCache::Entry)) \
153     macro(JSFixedArray_buffer, JSFixedArray::offsetOfData(), sizeof(EncodedJSValue)) \
154     
155 #define FOR_EACH_NUMBERED_ABSTRACT_HEAP(macro) \
156     macro(properties)
157     
158 // This class is meant to be cacheable between compilations, but it doesn't have to be.
159 // Doing so saves on creation of nodes. But clearing it will save memory.
160
161 class AbstractHeapRepository {
162     WTF_MAKE_NONCOPYABLE(AbstractHeapRepository);
163 public:
164     AbstractHeapRepository();
165     ~AbstractHeapRepository();
166     
167     AbstractHeap root;
168     
169 #define ABSTRACT_HEAP_DECLARATION(name) AbstractHeap name;
170     FOR_EACH_ABSTRACT_HEAP(ABSTRACT_HEAP_DECLARATION)
171 #undef ABSTRACT_HEAP_DECLARATION
172
173 #define ABSTRACT_FIELD_DECLARATION(name, offset) AbstractHeap name;
174     FOR_EACH_ABSTRACT_FIELD(ABSTRACT_FIELD_DECLARATION)
175 #undef ABSTRACT_FIELD_DECLARATION
176     
177     AbstractHeap& JSCell_freeListNext;
178     AbstractHeap& ArrayStorage_publicLength;
179     AbstractHeap& ArrayStorage_vectorLength;
180     
181 #define INDEXED_ABSTRACT_HEAP_DECLARATION(name, offset, size) IndexedAbstractHeap name;
182     FOR_EACH_INDEXED_ABSTRACT_HEAP(INDEXED_ABSTRACT_HEAP_DECLARATION)
183 #undef INDEXED_ABSTRACT_HEAP_DECLARATION
184     
185 #define NUMBERED_ABSTRACT_HEAP_DECLARATION(name) NumberedAbstractHeap name;
186     FOR_EACH_NUMBERED_ABSTRACT_HEAP(NUMBERED_ABSTRACT_HEAP_DECLARATION)
187 #undef NUMBERED_ABSTRACT_HEAP_DECLARATION
188
189     AbsoluteAbstractHeap absolute;
190     
191     IndexedAbstractHeap* forIndexingType(IndexingType indexingType)
192     {
193         switch (indexingType) {
194         case ALL_BLANK_INDEXING_TYPES:
195         case ALL_UNDECIDED_INDEXING_TYPES:
196             return 0;
197             
198         case ALL_INT32_INDEXING_TYPES:
199             return &indexedInt32Properties;
200             
201         case ALL_DOUBLE_INDEXING_TYPES:
202             return &indexedDoubleProperties;
203             
204         case ALL_CONTIGUOUS_INDEXING_TYPES:
205             return &indexedContiguousProperties;
206             
207         case ALL_ARRAY_STORAGE_INDEXING_TYPES:
208             return &ArrayStorage_vector;
209             
210         default:
211             RELEASE_ASSERT_NOT_REACHED();
212             return 0;
213         }
214     }
215     
216     IndexedAbstractHeap& forArrayType(DFG::Array::Type type)
217     {
218         switch (type) {
219         case DFG::Array::Int32:
220             return indexedInt32Properties;
221         case DFG::Array::Double:
222             return indexedDoubleProperties;
223         case DFG::Array::Contiguous:
224             return indexedContiguousProperties;
225         case DFG::Array::ArrayStorage:
226         case DFG::Array::SlowPutArrayStorage:
227             return ArrayStorage_vector;
228         default:
229             RELEASE_ASSERT_NOT_REACHED();
230             return indexedInt32Properties;
231         }
232     }
233
234     void decorateMemory(const AbstractHeap*, B3::Value*);
235     void decorateCCallRead(const AbstractHeap*, B3::Value*);
236     void decorateCCallWrite(const AbstractHeap*, B3::Value*);
237     void decoratePatchpointRead(const AbstractHeap*, B3::Value*);
238     void decoratePatchpointWrite(const AbstractHeap*, B3::Value*);
239     void decorateFenceRead(const AbstractHeap*, B3::Value*);
240     void decorateFenceWrite(const AbstractHeap*, B3::Value*);
241     void decorateFencedAccess(const AbstractHeap*, B3::Value*);
242
243     void computeRangesAndDecorateInstructions();
244
245 private:
246
247     struct HeapForValue {
248         HeapForValue()
249         {
250         }
251
252         HeapForValue(const AbstractHeap* heap, B3::Value* value)
253             : heap(heap)
254             , value(value)
255         {
256         }
257         
258         const AbstractHeap* heap { nullptr };
259         B3::Value* value { nullptr };
260     };
261
262     Vector<HeapForValue> m_heapForMemory;
263     Vector<HeapForValue> m_heapForCCallRead;
264     Vector<HeapForValue> m_heapForCCallWrite;
265     Vector<HeapForValue> m_heapForPatchpointRead;
266     Vector<HeapForValue> m_heapForPatchpointWrite;
267     Vector<HeapForValue> m_heapForFenceRead;
268     Vector<HeapForValue> m_heapForFenceWrite;
269     Vector<HeapForValue> m_heapForFencedAccess;
270 };
271
272 } } // namespace JSC::FTL
273
274 #endif // ENABLE(FTL_JIT)