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