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