1/*
2 * Copyright (C) 2012, 2014, 2015 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 *
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of Apple Inc. ("Apple") nor the names of
14 * its contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include "config.h"
30#include "SymbolTable.h"
31
32#include "CodeBlock.h"
33#include "JSDestructibleObject.h"
34#include "JSCInlines.h"
35#include "SlotVisitorInlines.h"
36#include "TypeProfiler.h"
37
38namespace JSC {
39
40const ClassInfo SymbolTable::s_info = { "SymbolTable", nullptr, nullptr, nullptr, CREATE_METHOD_TABLE(SymbolTable) };
41
42SymbolTableEntry& SymbolTableEntry::copySlow(const SymbolTableEntry& other)
43{
44 ASSERT(other.isFat());
45 FatEntry* newFatEntry = new FatEntry(*other.fatEntry());
46 freeFatEntry();
47 m_bits = bitwise_cast<intptr_t>(newFatEntry);
48 return *this;
49}
50
51void SymbolTable::destroy(JSCell* cell)
52{
53 SymbolTable* thisObject = static_cast<SymbolTable*>(cell);
54 thisObject->SymbolTable::~SymbolTable();
55}
56
57void SymbolTableEntry::freeFatEntrySlow()
58{
59 ASSERT(isFat());
60 delete fatEntry();
61}
62
63void SymbolTableEntry::prepareToWatch()
64{
65 if (!isWatchable())
66 return;
67 FatEntry* entry = inflate();
68 if (entry->m_watchpoints)
69 return;
70 entry->m_watchpoints = adoptRef(new WatchpointSet(ClearWatchpoint));
71}
72
73SymbolTableEntry::FatEntry* SymbolTableEntry::inflateSlow()
74{
75 FatEntry* entry = new FatEntry(m_bits);
76 m_bits = bitwise_cast<intptr_t>(entry);
77 return entry;
78}
79
80SymbolTable::SymbolTable(VM& vm)
81 : JSCell(vm, vm.symbolTableStructure.get())
82 , m_usesNonStrictEval(false)
83 , m_nestedLexicalScope(false)
84 , m_scopeType(VarScope)
85{
86}
87
88SymbolTable::~SymbolTable() { }
89
90void SymbolTable::finishCreation(VM& vm)
91{
92 Base::finishCreation(vm);
93 if (VM::canUseJIT())
94 m_singletonScope.set(vm, this, InferredValue::create(vm));
95}
96
97void SymbolTable::visitChildren(JSCell* thisCell, SlotVisitor& visitor)
98{
99 SymbolTable* thisSymbolTable = jsCast<SymbolTable*>(thisCell);
100 Base::visitChildren(thisSymbolTable, visitor);
101
102 visitor.append(thisSymbolTable->m_arguments);
103 visitor.append(thisSymbolTable->m_singletonScope);
104
105 if (thisSymbolTable->m_rareData)
106 visitor.append(thisSymbolTable->m_rareData->m_codeBlock);
107
108 // Save some memory. This is O(n) to rebuild and we do so on the fly.
109 ConcurrentJSLocker locker(thisSymbolTable->m_lock);
110 thisSymbolTable->m_localToEntry = nullptr;
111}
112
113const SymbolTable::LocalToEntryVec& SymbolTable::localToEntry(const ConcurrentJSLocker&)
114{
115 if (UNLIKELY(!m_localToEntry)) {
116 unsigned size = 0;
117 for (auto& entry : m_map) {
118 VarOffset offset = entry.value.varOffset();
119 if (offset.isScope())
120 size = std::max(size, offset.scopeOffset().offset() + 1);
121 }
122
123 m_localToEntry = std::make_unique<LocalToEntryVec>(size, nullptr);
124 for (auto& entry : m_map) {
125 VarOffset offset = entry.value.varOffset();
126 if (offset.isScope())
127 m_localToEntry->at(offset.scopeOffset().offset()) = &entry.value;
128 }
129 }
130
131 return *m_localToEntry;
132}
133
134SymbolTableEntry* SymbolTable::entryFor(const ConcurrentJSLocker& locker, ScopeOffset offset)
135{
136 auto& toEntryVector = localToEntry(locker);
137 if (offset.offset() >= toEntryVector.size())
138 return nullptr;
139 return toEntryVector[offset.offset()];
140}
141
142SymbolTable* SymbolTable::cloneScopePart(VM& vm)
143{
144 SymbolTable* result = SymbolTable::create(vm);
145
146 result->m_usesNonStrictEval = m_usesNonStrictEval;
147 result->m_nestedLexicalScope = m_nestedLexicalScope;
148 result->m_scopeType = m_scopeType;
149
150 for (auto iter = m_map.begin(), end = m_map.end(); iter != end; ++iter) {
151 if (!iter->value.varOffset().isScope())
152 continue;
153 result->m_map.add(
154 iter->key,
155 SymbolTableEntry(iter->value.varOffset(), iter->value.getAttributes()));
156 }
157
158 result->m_maxScopeOffset = m_maxScopeOffset;
159
160 if (ScopedArgumentsTable* arguments = this->arguments())
161 result->m_arguments.set(vm, result, arguments);
162
163 if (m_rareData) {
164 result->m_rareData = std::make_unique<SymbolTableRareData>();
165
166 {
167 auto iter = m_rareData->m_uniqueIDMap.begin();
168 auto end = m_rareData->m_uniqueIDMap.end();
169 for (; iter != end; ++iter)
170 result->m_rareData->m_uniqueIDMap.set(iter->key, iter->value);
171 }
172
173 {
174 auto iter = m_rareData->m_offsetToVariableMap.begin();
175 auto end = m_rareData->m_offsetToVariableMap.end();
176 for (; iter != end; ++iter)
177 result->m_rareData->m_offsetToVariableMap.set(iter->key, iter->value);
178 }
179
180 {
181 auto iter = m_rareData->m_uniqueTypeSetMap.begin();
182 auto end = m_rareData->m_uniqueTypeSetMap.end();
183 for (; iter != end; ++iter)
184 result->m_rareData->m_uniqueTypeSetMap.set(iter->key, iter->value);
185 }
186 }
187
188 return result;
189}
190
191void SymbolTable::prepareForTypeProfiling(const ConcurrentJSLocker&)
192{
193 if (m_rareData)
194 return;
195
196 m_rareData = std::make_unique<SymbolTableRareData>();
197
198 for (auto iter = m_map.begin(), end = m_map.end(); iter != end; ++iter) {
199 m_rareData->m_uniqueIDMap.set(iter->key, TypeProfilerNeedsUniqueIDGeneration);
200 m_rareData->m_offsetToVariableMap.set(iter->value.varOffset(), iter->key);
201 }
202}
203
204CodeBlock* SymbolTable::rareDataCodeBlock()
205{
206 if (!m_rareData)
207 return nullptr;
208
209 return m_rareData->m_codeBlock.get();
210}
211
212void SymbolTable::setRareDataCodeBlock(CodeBlock* codeBlock)
213{
214 if (!m_rareData)
215 m_rareData = std::make_unique<SymbolTableRareData>();
216
217 ASSERT(!m_rareData->m_codeBlock);
218 m_rareData->m_codeBlock.set(*codeBlock->vm(), this, codeBlock);
219}
220
221GlobalVariableID SymbolTable::uniqueIDForVariable(const ConcurrentJSLocker&, UniquedStringImpl* key, VM& vm)
222{
223 RELEASE_ASSERT(m_rareData);
224
225 auto iter = m_rareData->m_uniqueIDMap.find(key);
226 auto end = m_rareData->m_uniqueIDMap.end();
227 if (iter == end)
228 return TypeProfilerNoGlobalIDExists;
229
230 GlobalVariableID id = iter->value;
231 if (id == TypeProfilerNeedsUniqueIDGeneration) {
232 id = vm.typeProfiler()->getNextUniqueVariableID();
233 m_rareData->m_uniqueIDMap.set(key, id);
234 m_rareData->m_uniqueTypeSetMap.set(key, TypeSet::create()); // Make a new global typeset for this corresponding ID.
235 }
236
237 return id;
238}
239
240GlobalVariableID SymbolTable::uniqueIDForOffset(const ConcurrentJSLocker& locker, VarOffset offset, VM& vm)
241{
242 RELEASE_ASSERT(m_rareData);
243
244 auto iter = m_rareData->m_offsetToVariableMap.find(offset);
245 auto end = m_rareData->m_offsetToVariableMap.end();
246 if (iter == end)
247 return TypeProfilerNoGlobalIDExists;
248
249 return uniqueIDForVariable(locker, iter->value.get(), vm);
250}
251
252RefPtr<TypeSet> SymbolTable::globalTypeSetForOffset(const ConcurrentJSLocker& locker, VarOffset offset, VM& vm)
253{
254 RELEASE_ASSERT(m_rareData);
255
256 uniqueIDForOffset(locker, offset, vm); // Lazily create the TypeSet if necessary.
257
258 auto iter = m_rareData->m_offsetToVariableMap.find(offset);
259 auto end = m_rareData->m_offsetToVariableMap.end();
260 if (iter == end)
261 return nullptr;
262
263 return globalTypeSetForVariable(locker, iter->value.get(), vm);
264}
265
266RefPtr<TypeSet> SymbolTable::globalTypeSetForVariable(const ConcurrentJSLocker& locker, UniquedStringImpl* key, VM& vm)
267{
268 RELEASE_ASSERT(m_rareData);
269
270 uniqueIDForVariable(locker, key, vm); // Lazily create the TypeSet if necessary.
271
272 auto iter = m_rareData->m_uniqueTypeSetMap.find(key);
273 auto end = m_rareData->m_uniqueTypeSetMap.end();
274 if (iter == end)
275 return nullptr;
276
277 return iter->value;
278}
279
280} // namespace JSC
281
282