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 "JSGlobalObject.h"
37#include "JSMap.h"
38#include "JSSet.h"
39#include "JSWeakMap.h"
40#include "Symbol.h"
41
42namespace JSC { namespace FTL {
43
44#define FOR_EACH_ABSTRACT_HEAP(macro) \
45 macro(typedArrayProperties)
46
47#define FOR_EACH_ABSTRACT_FIELD(macro) \
48 macro(ArrayBuffer_data, ArrayBuffer::offsetOfData()) \
49 macro(ArrayStorage_numValuesInVector, ArrayStorage::numValuesInVectorOffset()) \
50 macro(Butterfly_arrayBuffer, Butterfly::offsetOfArrayBuffer()) \
51 macro(Butterfly_publicLength, Butterfly::offsetOfPublicLength()) \
52 macro(Butterfly_vectorLength, Butterfly::offsetOfVectorLength()) \
53 macro(CallFrame_callerFrame, CallFrame::callerFrameOffset()) \
54 macro(ClassInfo_parentClass, ClassInfo::offsetOfParentClass()) \
55 macro(DateInstance_internalNumber, DateInstance::offsetOfInternalNumber()) \
56 macro(DateInstance_data, DateInstance::offsetOfData()) \
57 macro(DateInstanceData_gregorianDateTimeCachedForMS, DateInstanceData::offsetOfGregorianDateTimeCachedForMS()) \
58 macro(DateInstanceData_gregorianDateTimeUTCCachedForMS, DateInstanceData::offsetOfGregorianDateTimeUTCCachedForMS()) \
59 macro(DateInstanceData_cachedGregorianDateTime_year, DateInstanceData::offsetOfCachedGregorianDateTime() + GregorianDateTime::offsetOfYear()) \
60 macro(DateInstanceData_cachedGregorianDateTimeUTC_year, DateInstanceData::offsetOfCachedGregorianDateTimeUTC() + GregorianDateTime::offsetOfYear()) \
61 macro(DateInstanceData_cachedGregorianDateTime_month, DateInstanceData::offsetOfCachedGregorianDateTime() + GregorianDateTime::offsetOfMonth()) \
62 macro(DateInstanceData_cachedGregorianDateTimeUTC_month, DateInstanceData::offsetOfCachedGregorianDateTimeUTC() + GregorianDateTime::offsetOfMonth()) \
63 macro(DateInstanceData_cachedGregorianDateTime_monthDay, DateInstanceData::offsetOfCachedGregorianDateTime() + GregorianDateTime::offsetOfMonthDay()) \
64 macro(DateInstanceData_cachedGregorianDateTimeUTC_monthDay, DateInstanceData::offsetOfCachedGregorianDateTimeUTC() + GregorianDateTime::offsetOfMonthDay()) \
65 macro(DateInstanceData_cachedGregorianDateTime_weekDay, DateInstanceData::offsetOfCachedGregorianDateTime() + GregorianDateTime::offsetOfWeekDay()) \
66 macro(DateInstanceData_cachedGregorianDateTimeUTC_weekDay, DateInstanceData::offsetOfCachedGregorianDateTimeUTC() + GregorianDateTime::offsetOfWeekDay()) \
67 macro(DateInstanceData_cachedGregorianDateTime_hour, DateInstanceData::offsetOfCachedGregorianDateTime() + GregorianDateTime::offsetOfHour()) \
68 macro(DateInstanceData_cachedGregorianDateTimeUTC_hour, DateInstanceData::offsetOfCachedGregorianDateTimeUTC() + GregorianDateTime::offsetOfHour()) \
69 macro(DateInstanceData_cachedGregorianDateTime_minute, DateInstanceData::offsetOfCachedGregorianDateTime() + GregorianDateTime::offsetOfMinute()) \
70 macro(DateInstanceData_cachedGregorianDateTimeUTC_minute, DateInstanceData::offsetOfCachedGregorianDateTimeUTC() + GregorianDateTime::offsetOfMinute()) \
71 macro(DateInstanceData_cachedGregorianDateTime_second, DateInstanceData::offsetOfCachedGregorianDateTime() + GregorianDateTime::offsetOfSecond()) \
72 macro(DateInstanceData_cachedGregorianDateTimeUTC_second, DateInstanceData::offsetOfCachedGregorianDateTimeUTC() + GregorianDateTime::offsetOfSecond()) \
73 macro(DateInstanceData_cachedGregorianDateTime_utcOffsetInMinute, DateInstanceData::offsetOfCachedGregorianDateTime() + GregorianDateTime::offsetOfUTCOffsetInMinute()) \
74 macro(DateInstanceData_cachedGregorianDateTimeUTC_utcOffsetInMinute, DateInstanceData::offsetOfCachedGregorianDateTimeUTC() + GregorianDateTime::offsetOfUTCOffsetInMinute()) \
75 macro(DirectArguments_callee, DirectArguments::offsetOfCallee()) \
76 macro(DirectArguments_length, DirectArguments::offsetOfLength()) \
77 macro(DirectArguments_minCapacity, DirectArguments::offsetOfMinCapacity()) \
78 macro(DirectArguments_mappedArguments, DirectArguments::offsetOfMappedArguments()) \
79 macro(DirectArguments_modifiedArgumentsDescriptor, DirectArguments::offsetOfModifiedArgumentsDescriptor()) \
80 macro(FunctionRareData_allocator, FunctionRareData::offsetOfObjectAllocationProfile() + ObjectAllocationProfileWithPrototype::offsetOfAllocator()) \
81 macro(FunctionRareData_structure, FunctionRareData::offsetOfObjectAllocationProfile() + ObjectAllocationProfileWithPrototype::offsetOfStructure()) \
82 macro(FunctionRareData_prototype, FunctionRareData::offsetOfObjectAllocationProfile() + ObjectAllocationProfileWithPrototype::offsetOfPrototype()) \
83 macro(FunctionRareData_allocationProfileWatchpointSet, FunctionRareData::offsetOfAllocationProfileWatchpointSet()) \
84 macro(FunctionRareData_internalFunctionAllocationProfile_structure, FunctionRareData::offsetOfInternalFunctionAllocationProfile() + InternalFunctionAllocationProfile::offsetOfStructure()) \
85 macro(FunctionRareData_boundFunctionStructure, FunctionRareData::offsetOfBoundFunctionStructure()) \
86 macro(FunctionRareData_allocationProfileClearingWatchpoint, FunctionRareData::offsetOfAllocationProfileClearingWatchpoint()) \
87 macro(FunctionRareData_hasReifiedLength, FunctionRareData::offsetOfHasReifiedLength()) \
88 macro(FunctionRareData_hasReifiedName, FunctionRareData::offsetOfHasReifiedName()) \
89 macro(GetterSetter_getter, GetterSetter::offsetOfGetter()) \
90 macro(GetterSetter_setter, GetterSetter::offsetOfSetter()) \
91 macro(JSArrayBufferView_length, JSArrayBufferView::offsetOfLength()) \
92 macro(JSArrayBufferView_mode, JSArrayBufferView::offsetOfMode()) \
93 macro(JSArrayBufferView_vector, JSArrayBufferView::offsetOfVector()) \
94 macro(JSBigInt_length, JSBigInt::offsetOfLength()) \
95 macro(JSCell_cellState, JSCell::cellStateOffset()) \
96 macro(JSCell_header, 0) \
97 macro(JSCell_indexingTypeAndMisc, JSCell::indexingTypeAndMiscOffset()) \
98 macro(JSCell_structureID, JSCell::structureIDOffset()) \
99 macro(JSCell_typeInfoFlags, JSCell::typeInfoFlagsOffset()) \
100 macro(JSCell_typeInfoType, JSCell::typeInfoTypeOffset()) \
101 macro(JSCell_usefulBytes, JSCell::indexingTypeAndMiscOffset()) \
102 macro(JSDestructibleObject_classInfo, JSDestructibleObject::classInfoOffset()) \
103 macro(JSFunction_executable, JSFunction::offsetOfExecutable()) \
104 macro(JSFunction_globalObject, JSFunction::offsetOfGlobalObject()) \
105 macro(JSFunction_scope, JSFunction::offsetOfScopeChain()) \
106 macro(JSFunction_rareData, JSFunction::offsetOfRareData()) \
107 macro(JSGlobalObject_regExpGlobalData_cachedResult_lastRegExp, JSGlobalObject::regExpGlobalDataOffset() + RegExpGlobalData::offsetOfCachedResult() + RegExpCachedResult::offsetOfLastRegExp()) \
108 macro(JSGlobalObject_regExpGlobalData_cachedResult_lastInput, JSGlobalObject::regExpGlobalDataOffset() + RegExpGlobalData::offsetOfCachedResult() + RegExpCachedResult::offsetOfLastInput()) \
109 macro(JSGlobalObject_regExpGlobalData_cachedResult_result_start, JSGlobalObject::regExpGlobalDataOffset() + RegExpGlobalData::offsetOfCachedResult() + RegExpCachedResult::offsetOfResult() + OBJECT_OFFSETOF(MatchResult, start)) \
110 macro(JSGlobalObject_regExpGlobalData_cachedResult_result_end, JSGlobalObject::regExpGlobalDataOffset() + RegExpGlobalData::offsetOfCachedResult() + RegExpCachedResult::offsetOfResult() + OBJECT_OFFSETOF(MatchResult, end)) \
111 macro(JSGlobalObject_regExpGlobalData_cachedResult_reified, JSGlobalObject::regExpGlobalDataOffset() + RegExpGlobalData::offsetOfCachedResult() + RegExpCachedResult::offsetOfReified()) \
112 macro(JSObject_butterfly, JSObject::butterflyOffset()) \
113 macro(JSPropertyNameEnumerator_cachedInlineCapacity, JSPropertyNameEnumerator::cachedInlineCapacityOffset()) \
114 macro(JSPropertyNameEnumerator_cachedPropertyNamesVector, JSPropertyNameEnumerator::cachedPropertyNamesVectorOffset()) \
115 macro(JSPropertyNameEnumerator_cachedStructureID, JSPropertyNameEnumerator::cachedStructureIDOffset()) \
116 macro(JSPropertyNameEnumerator_endGenericPropertyIndex, JSPropertyNameEnumerator::endGenericPropertyIndexOffset()) \
117 macro(JSPropertyNameEnumerator_endStructurePropertyIndex, JSPropertyNameEnumerator::endStructurePropertyIndexOffset()) \
118 macro(JSPropertyNameEnumerator_indexLength, JSPropertyNameEnumerator::indexedLengthOffset()) \
119 macro(JSRopeString_flags, JSRopeString::offsetOfFlags()) \
120 macro(JSRopeString_length, JSRopeString::offsetOfLength()) \
121 macro(JSRopeString_fiber0, JSRopeString::offsetOfFiber0()) \
122 macro(JSRopeString_fiber1, JSRopeString::offsetOfFiber1()) \
123 macro(JSRopeString_fiber2, JSRopeString::offsetOfFiber2()) \
124 macro(JSScope_next, JSScope::offsetOfNext()) \
125 macro(JSSymbolTableObject_symbolTable, JSSymbolTableObject::offsetOfSymbolTable()) \
126 macro(JSWrapperObject_internalValue, JSWrapperObject::internalValueOffset()) \
127 macro(RegExpObject_regExpAndLastIndexIsNotWritableFlag, RegExpObject::offsetOfRegExpAndLastIndexIsNotWritableFlag()) \
128 macro(RegExpObject_lastIndex, RegExpObject::offsetOfLastIndex()) \
129 macro(ShadowChicken_Packet_callee, OBJECT_OFFSETOF(ShadowChicken::Packet, callee)) \
130 macro(ShadowChicken_Packet_frame, OBJECT_OFFSETOF(ShadowChicken::Packet, frame)) \
131 macro(ShadowChicken_Packet_callerFrame, OBJECT_OFFSETOF(ShadowChicken::Packet, callerFrame)) \
132 macro(ShadowChicken_Packet_thisValue, OBJECT_OFFSETOF(ShadowChicken::Packet, thisValue)) \
133 macro(ShadowChicken_Packet_scope, OBJECT_OFFSETOF(ShadowChicken::Packet, scope)) \
134 macro(ShadowChicken_Packet_codeBlock, OBJECT_OFFSETOF(ShadowChicken::Packet, codeBlock)) \
135 macro(ShadowChicken_Packet_callSiteIndex, OBJECT_OFFSETOF(ShadowChicken::Packet, callSiteIndex)) \
136 macro(ScopedArguments_overrodeThings, ScopedArguments::offsetOfOverrodeThings()) \
137 macro(ScopedArguments_scope, ScopedArguments::offsetOfScope()) \
138 macro(ScopedArguments_storage, ScopedArguments::offsetOfStorage()) \
139 macro(ScopedArguments_table, ScopedArguments::offsetOfTable()) \
140 macro(ScopedArguments_totalLength, ScopedArguments::offsetOfTotalLength()) \
141 macro(ScopedArgumentsTable_arguments, ScopedArgumentsTable::offsetOfArguments()) \
142 macro(ScopedArgumentsTable_length, ScopedArgumentsTable::offsetOfLength()) \
143 macro(StringImpl_data, StringImpl::dataOffset()) \
144 macro(StringImpl_hashAndFlags, StringImpl::flagsOffset()) \
145 macro(StringImpl_length, StringImpl::lengthMemoryOffset()) \
146 macro(Structure_classInfo, Structure::classInfoOffset()) \
147 macro(Structure_globalObject, Structure::globalObjectOffset()) \
148 macro(Structure_indexingModeIncludingHistory, Structure::indexingModeIncludingHistoryOffset()) \
149 macro(Structure_inlineCapacity, Structure::inlineCapacityOffset()) \
150 macro(Structure_previousOrRareData, Structure::previousOrRareDataOffset()) \
151 macro(Structure_prototype, Structure::prototypeOffset()) \
152 macro(Structure_structureID, Structure::structureIDOffset()) \
153 macro(StructureRareData_cachedOwnKeys, StructureRareData::offsetOfCachedOwnKeys()) \
154 macro(HashMapImpl_capacity, HashMapImpl<HashMapBucket<HashMapBucketDataKey>>::offsetOfCapacity()) \
155 macro(HashMapImpl_buffer, HashMapImpl<HashMapBucket<HashMapBucketDataKey>>::offsetOfBuffer()) \
156 macro(HashMapImpl_head, HashMapImpl<HashMapBucket<HashMapBucketDataKey>>::offsetOfHead()) \
157 macro(HashMapBucket_value, HashMapBucket<HashMapBucketDataKeyValue>::offsetOfValue()) \
158 macro(HashMapBucket_key, HashMapBucket<HashMapBucketDataKeyValue>::offsetOfKey()) \
159 macro(HashMapBucket_next, HashMapBucket<HashMapBucketDataKeyValue>::offsetOfNext()) \
160 macro(WeakMapImpl_capacity, WeakMapImpl<WeakMapBucket<WeakMapBucketDataKey>>::offsetOfCapacity()) \
161 macro(WeakMapImpl_buffer, WeakMapImpl<WeakMapBucket<WeakMapBucketDataKey>>::offsetOfBuffer()) \
162 macro(WeakMapBucket_value, WeakMapBucket<WeakMapBucketDataKeyValue>::offsetOfValue()) \
163 macro(WeakMapBucket_key, WeakMapBucket<WeakMapBucketDataKeyValue>::offsetOfKey()) \
164 macro(Symbol_symbolImpl, Symbol::offsetOfSymbolImpl()) \
165 macro(JSFixedArray_size, JSFixedArray::offsetOfSize()) \
166
167#define FOR_EACH_INDEXED_ABSTRACT_HEAP(macro) \
168 macro(ArrayStorage_vector, ArrayStorage::vectorOffset(), sizeof(WriteBarrier<Unknown>)) \
169 macro(CompleteSubspace_allocatorForSizeStep, CompleteSubspace::offsetOfAllocatorForSizeStep(), sizeof(Allocator)) \
170 macro(DirectArguments_storage, DirectArguments::storageOffset(), sizeof(EncodedJSValue)) \
171 macro(JSLexicalEnvironment_variables, JSLexicalEnvironment::offsetOfVariables(), sizeof(EncodedJSValue)) \
172 macro(JSPropertyNameEnumerator_cachedPropertyNamesVectorContents, 0, sizeof(WriteBarrier<JSString>)) \
173 macro(JSInternalFieldObjectImpl_internalFields, JSInternalFieldObjectImpl<>::offsetOfInternalFields(), sizeof(WriteBarrier<Unknown>)) \
174 macro(ScopedArguments_Storage_storage, 0, sizeof(EncodedJSValue)) \
175 macro(WriteBarrierBuffer_bufferContents, 0, sizeof(JSCell*)) \
176 macro(characters8, 0, sizeof(LChar)) \
177 macro(characters16, 0, sizeof(UChar)) \
178 macro(indexedInt32Properties, 0, sizeof(EncodedJSValue)) \
179 macro(indexedDoubleProperties, 0, sizeof(double)) \
180 macro(indexedContiguousProperties, 0, sizeof(EncodedJSValue)) \
181 macro(scopedArgumentsTableArguments, 0, sizeof(int32_t)) \
182 macro(singleCharacterStrings, 0, sizeof(JSString*)) \
183 macro(structureTable, 0, sizeof(Structure*)) \
184 macro(variables, 0, sizeof(Register)) \
185 macro(HasOwnPropertyCache, 0, sizeof(HasOwnPropertyCache::Entry)) \
186 macro(JSFixedArray_buffer, JSFixedArray::offsetOfData(), sizeof(EncodedJSValue)) \
187
188#define FOR_EACH_NUMBERED_ABSTRACT_HEAP(macro) \
189 macro(properties)
190
191// This class is meant to be cacheable between compilations, but it doesn't have to be.
192// Doing so saves on creation of nodes. But clearing it will save memory.
193
194class AbstractHeapRepository {
195 WTF_MAKE_NONCOPYABLE(AbstractHeapRepository);
196public:
197 AbstractHeapRepository();
198 ~AbstractHeapRepository();
199
200 AbstractHeap root;
201
202#define ABSTRACT_HEAP_DECLARATION(name) AbstractHeap name;
203 FOR_EACH_ABSTRACT_HEAP(ABSTRACT_HEAP_DECLARATION)
204#undef ABSTRACT_HEAP_DECLARATION
205
206#define ABSTRACT_FIELD_DECLARATION(name, offset) AbstractHeap name;
207 FOR_EACH_ABSTRACT_FIELD(ABSTRACT_FIELD_DECLARATION)
208#undef ABSTRACT_FIELD_DECLARATION
209
210 AbstractHeap& JSCell_freeListNext;
211 AbstractHeap& ArrayStorage_publicLength;
212 AbstractHeap& ArrayStorage_vectorLength;
213
214#define INDEXED_ABSTRACT_HEAP_DECLARATION(name, offset, size) IndexedAbstractHeap name;
215 FOR_EACH_INDEXED_ABSTRACT_HEAP(INDEXED_ABSTRACT_HEAP_DECLARATION)
216#undef INDEXED_ABSTRACT_HEAP_DECLARATION
217
218#define NUMBERED_ABSTRACT_HEAP_DECLARATION(name) NumberedAbstractHeap name;
219 FOR_EACH_NUMBERED_ABSTRACT_HEAP(NUMBERED_ABSTRACT_HEAP_DECLARATION)
220#undef NUMBERED_ABSTRACT_HEAP_DECLARATION
221
222 AbstractHeap& JSString_value;
223
224 AbsoluteAbstractHeap absolute;
225
226 IndexedAbstractHeap* forIndexingType(IndexingType indexingType)
227 {
228 switch (indexingType) {
229 case ALL_BLANK_INDEXING_TYPES:
230 case ALL_UNDECIDED_INDEXING_TYPES:
231 return 0;
232
233 case ALL_INT32_INDEXING_TYPES:
234 return &indexedInt32Properties;
235
236 case ALL_DOUBLE_INDEXING_TYPES:
237 return &indexedDoubleProperties;
238
239 case ALL_CONTIGUOUS_INDEXING_TYPES:
240 return &indexedContiguousProperties;
241
242 case ALL_ARRAY_STORAGE_INDEXING_TYPES:
243 return &ArrayStorage_vector;
244
245 default:
246 RELEASE_ASSERT_NOT_REACHED();
247 return 0;
248 }
249 }
250
251 IndexedAbstractHeap& forArrayType(DFG::Array::Type type)
252 {
253 switch (type) {
254 case DFG::Array::Int32:
255 return indexedInt32Properties;
256 case DFG::Array::Double:
257 return indexedDoubleProperties;
258 case DFG::Array::Contiguous:
259 return indexedContiguousProperties;
260 case DFG::Array::ArrayStorage:
261 case DFG::Array::SlowPutArrayStorage:
262 return ArrayStorage_vector;
263 default:
264 RELEASE_ASSERT_NOT_REACHED();
265 return indexedInt32Properties;
266 }
267 }
268
269 void decorateMemory(const AbstractHeap*, B3::Value*);
270 void decorateCCallRead(const AbstractHeap*, B3::Value*);
271 void decorateCCallWrite(const AbstractHeap*, B3::Value*);
272 void decoratePatchpointRead(const AbstractHeap*, B3::Value*);
273 void decoratePatchpointWrite(const AbstractHeap*, B3::Value*);
274 void decorateFenceRead(const AbstractHeap*, B3::Value*);
275 void decorateFenceWrite(const AbstractHeap*, B3::Value*);
276 void decorateFencedAccess(const AbstractHeap*, B3::Value*);
277
278 void computeRangesAndDecorateInstructions();
279
280private:
281
282 struct HeapForValue {
283 HeapForValue()
284 {
285 }
286
287 HeapForValue(const AbstractHeap* heap, B3::Value* value)
288 : heap(heap)
289 , value(value)
290 {
291 }
292
293 const AbstractHeap* heap { nullptr };
294 B3::Value* value { nullptr };
295 };
296
297 Vector<HeapForValue> m_heapForMemory;
298 Vector<HeapForValue> m_heapForCCallRead;
299 Vector<HeapForValue> m_heapForCCallWrite;
300 Vector<HeapForValue> m_heapForPatchpointRead;
301 Vector<HeapForValue> m_heapForPatchpointWrite;
302 Vector<HeapForValue> m_heapForFenceRead;
303 Vector<HeapForValue> m_heapForFenceWrite;
304 Vector<HeapForValue> m_heapForFencedAccess;
305};
306
307} } // namespace JSC::FTL
308
309#endif // ENABLE(FTL_JIT)
310