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