#ifndef SymbolTable_h
#define SymbolTable_h
#include "ConcurrentJITLock.h"
#include "ConstantMode.h"
#include "InferredValue.h"
#include "JSObject.h"
#include "ScopedArgumentsTable.h"
#include "TypeLocation.h"
#include "VarOffset.h"
#include "Watchpoint.h"
#include <memory>
#include <wtf/HashTraits.h>
#include <wtf/text/UniquedStringImpl.h>
namespace JSC {
class SymbolTable;
static ALWAYS_INLINE int missingSymbolMarker() { return std::numeric_limits<int>::max(); }
struct SymbolTableEntry {
private:
static VarOffset varOffsetFromBits(intptr_t bits)
{
VarKind kind;
intptr_t kindBits = bits & KindBitsMask;
if (kindBits <= UnwatchableScopeKindBits)
kind = VarKind::Scope;
else if (kindBits == StackKindBits)
kind = VarKind::Stack;
else
kind = VarKind::DirectArgument;
return VarOffset::assemble(kind, static_cast<int>(bits >> FlagBits));
}
static ScopeOffset scopeOffsetFromBits(intptr_t bits)
{
ASSERT((bits & KindBitsMask) <= UnwatchableScopeKindBits);
return ScopeOffset(static_cast<int>(bits >> FlagBits));
}
public:
class Fast {
public:
Fast()
: m_bits(SlimFlag)
{
}
ALWAYS_INLINE Fast(const SymbolTableEntry& entry)
: m_bits(entry.bits())
{
}
bool isNull() const
{
return !(m_bits & ~SlimFlag);
}
VarOffset varOffset() const
{
return varOffsetFromBits(m_bits);
}
ScopeOffset scopeOffset() const
{
return scopeOffsetFromBits(m_bits);
}
bool isReadOnly() const
{
return m_bits & ReadOnlyFlag;
}
bool isDontEnum() const
{
return m_bits & DontEnumFlag;
}
unsigned getAttributes() const
{
unsigned attributes = 0;
if (isReadOnly())
attributes |= ReadOnly;
if (isDontEnum())
attributes |= DontEnum;
return attributes;
}
bool isFat() const
{
return !(m_bits & SlimFlag);
}
private:
friend struct SymbolTableEntry;
intptr_t m_bits;
};
SymbolTableEntry()
: m_bits(SlimFlag)
{
}
SymbolTableEntry(VarOffset offset)
: m_bits(SlimFlag)
{
ASSERT(isValidVarOffset(offset));
pack(offset, true, false, false);
}
SymbolTableEntry(VarOffset offset, unsigned attributes)
: m_bits(SlimFlag)
{
ASSERT(isValidVarOffset(offset));
pack(offset, true, attributes & ReadOnly, attributes & DontEnum);
}
~SymbolTableEntry()
{
freeFatEntry();
}
SymbolTableEntry(const SymbolTableEntry& other)
: m_bits(SlimFlag)
{
*this = other;
}
SymbolTableEntry& operator=(const SymbolTableEntry& other)
{
if (UNLIKELY(other.isFat()))
return copySlow(other);
freeFatEntry();
m_bits = other.m_bits;
return *this;
}
SymbolTableEntry(SymbolTableEntry&& other)
: m_bits(SlimFlag)
{
swap(other);
}
SymbolTableEntry& operator=(SymbolTableEntry&& other)
{
swap(other);
return *this;
}
void swap(SymbolTableEntry& other)
{
std::swap(m_bits, other.m_bits);
}
bool isNull() const
{
return !(bits() & ~SlimFlag);
}
VarOffset varOffset() const
{
return varOffsetFromBits(bits());
}
bool isWatchable() const
{
return (m_bits & KindBitsMask) == ScopeKindBits;
}
ScopeOffset scopeOffset() const
{
return scopeOffsetFromBits(bits());
}
ALWAYS_INLINE Fast getFast() const
{
return Fast(*this);
}
ALWAYS_INLINE Fast getFast(bool& wasFat) const
{
Fast result;
wasFat = isFat();
if (wasFat)
result.m_bits = fatEntry()->m_bits | SlimFlag;
else
result.m_bits = m_bits;
return result;
}
unsigned getAttributes() const
{
return getFast().getAttributes();
}
void setAttributes(unsigned attributes)
{
pack(varOffset(), isWatchable(), attributes & ReadOnly, attributes & DontEnum);
}
bool isReadOnly() const
{
return bits() & ReadOnlyFlag;
}
ConstantMode constantMode() const
{
return modeForIsConstant(isReadOnly());
}
bool isDontEnum() const
{
return bits() & DontEnumFlag;
}
void disableWatching(VM& vm)
{
if (WatchpointSet* set = watchpointSet())
set->invalidate(vm, "Disabling watching in symbol table");
if (varOffset().isScope())
pack(varOffset(), false, isReadOnly(), isDontEnum());
}
void prepareToWatch();
void addWatchpoint(Watchpoint*);
WatchpointSet* watchpointSet()
{
if (!isFat())
return 0;
return fatEntry()->m_watchpoints.get();
}
private:
static const intptr_t SlimFlag = 0x1;
static const intptr_t ReadOnlyFlag = 0x2;
static const intptr_t DontEnumFlag = 0x4;
static const intptr_t NotNullFlag = 0x8;
static const intptr_t KindBitsMask = 0x30;
static const intptr_t ScopeKindBits = 0x00;
static const intptr_t UnwatchableScopeKindBits = 0x10;
static const intptr_t StackKindBits = 0x20;
static const intptr_t DirectArgumentKindBits = 0x30;
static const intptr_t FlagBits = 6;
class FatEntry {
WTF_MAKE_FAST_ALLOCATED;
public:
FatEntry(intptr_t bits)
: m_bits(bits & ~SlimFlag)
{
}
intptr_t m_bits;
RefPtr<WatchpointSet> m_watchpoints;
};
SymbolTableEntry& copySlow(const SymbolTableEntry&);
JS_EXPORT_PRIVATE void notifyWriteSlow(VM&, JSValue, const FireDetail&);
bool isFat() const
{
return !(m_bits & SlimFlag);
}
const FatEntry* fatEntry() const
{
ASSERT(isFat());
return bitwise_cast<const FatEntry*>(m_bits);
}
FatEntry* fatEntry()
{
ASSERT(isFat());
return bitwise_cast<FatEntry*>(m_bits);
}
FatEntry* inflate()
{
if (LIKELY(isFat()))
return fatEntry();
return inflateSlow();
}
FatEntry* inflateSlow();
ALWAYS_INLINE intptr_t bits() const
{
if (isFat())
return fatEntry()->m_bits;
return m_bits;
}
ALWAYS_INLINE intptr_t& bits()
{
if (isFat())
return fatEntry()->m_bits;
return m_bits;
}
void freeFatEntry()
{
if (LIKELY(!isFat()))
return;
freeFatEntrySlow();
}
JS_EXPORT_PRIVATE void freeFatEntrySlow();
void pack(VarOffset offset, bool isWatchable, bool readOnly, bool dontEnum)
{
ASSERT(!isFat());
intptr_t& bitsRef = bits();
bitsRef =
(static_cast<intptr_t>(offset.rawOffset()) << FlagBits) | NotNullFlag | SlimFlag;
if (readOnly)
bitsRef |= ReadOnlyFlag;
if (dontEnum)
bitsRef |= DontEnumFlag;
switch (offset.kind()) {
case VarKind::Scope:
if (isWatchable)
bitsRef |= ScopeKindBits;
else
bitsRef |= UnwatchableScopeKindBits;
break;
case VarKind::Stack:
bitsRef |= StackKindBits;
break;
case VarKind::DirectArgument:
bitsRef |= DirectArgumentKindBits;
break;
default:
RELEASE_ASSERT_NOT_REACHED();
break;
}
}
static bool isValidVarOffset(VarOffset offset)
{
return ((static_cast<intptr_t>(offset.rawOffset()) << FlagBits) >> FlagBits) == static_cast<intptr_t>(offset.rawOffset());
}
intptr_t m_bits;
};
struct SymbolTableIndexHashTraits : HashTraits<SymbolTableEntry> {
static const bool needsDestruction = true;
};
class SymbolTable final : public JSCell {
public:
typedef JSCell Base;
static const unsigned StructureFlags = Base::StructureFlags | StructureIsImmortal;
typedef HashMap<RefPtr<UniquedStringImpl>, SymbolTableEntry, IdentifierRepHash, HashTraits<RefPtr<UniquedStringImpl>>, SymbolTableIndexHashTraits> Map;
typedef HashMap<RefPtr<UniquedStringImpl>, GlobalVariableID, IdentifierRepHash> UniqueIDMap;
typedef HashMap<RefPtr<UniquedStringImpl>, RefPtr<TypeSet>, IdentifierRepHash> UniqueTypeSetMap;
typedef HashMap<VarOffset, RefPtr<UniquedStringImpl>> OffsetToVariableMap;
typedef Vector<SymbolTableEntry*> LocalToEntryVec;
static SymbolTable* create(VM& vm)
{
SymbolTable* symbolTable = new (NotNull, allocateCell<SymbolTable>(vm.heap)) SymbolTable(vm);
symbolTable->finishCreation(vm);
return symbolTable;
}
static const bool needsDestruction = true;
static void destroy(JSCell*);
static Structure* createStructure(VM& vm, JSGlobalObject* globalObject, JSValue prototype)
{
return Structure::create(vm, globalObject, prototype, TypeInfo(CellType, StructureFlags), info());
}
Map::iterator find(const ConcurrentJITLocker&, UniquedStringImpl* key)
{
return m_map.find(key);
}
Map::iterator find(const GCSafeConcurrentJITLocker&, UniquedStringImpl* key)
{
return m_map.find(key);
}
SymbolTableEntry get(const ConcurrentJITLocker&, UniquedStringImpl* key)
{
return m_map.get(key);
}
SymbolTableEntry get(UniquedStringImpl* key)
{
ConcurrentJITLocker locker(m_lock);
return get(locker, key);
}
SymbolTableEntry inlineGet(const ConcurrentJITLocker&, UniquedStringImpl* key)
{
return m_map.inlineGet(key);
}
SymbolTableEntry inlineGet(UniquedStringImpl* key)
{
ConcurrentJITLocker locker(m_lock);
return inlineGet(locker, key);
}
Map::iterator begin(const ConcurrentJITLocker&)
{
return m_map.begin();
}
Map::iterator end(const ConcurrentJITLocker&)
{
return m_map.end();
}
Map::iterator end(const GCSafeConcurrentJITLocker&)
{
return m_map.end();
}
size_t size(const ConcurrentJITLocker&) const
{
return m_map.size();
}
size_t size() const
{
ConcurrentJITLocker locker(m_lock);
return size(locker);
}
ScopeOffset maxScopeOffset() const
{
return m_maxScopeOffset;
}
void didUseScopeOffset(ScopeOffset offset)
{
if (!m_maxScopeOffset || m_maxScopeOffset < offset)
m_maxScopeOffset = offset;
}
void didUseVarOffset(VarOffset offset)
{
if (offset.isScope())
didUseScopeOffset(offset.scopeOffset());
}
unsigned scopeSize() const
{
ScopeOffset maxScopeOffset = this->maxScopeOffset();
unsigned fastResult = maxScopeOffset.offsetUnchecked() + 1;
ASSERT(fastResult == (!maxScopeOffset ? 0 : maxScopeOffset.offset() + 1));
return fastResult;
}
ScopeOffset nextScopeOffset() const
{
return ScopeOffset(scopeSize());
}
ScopeOffset takeNextScopeOffset(const ConcurrentJITLocker&)
{
ScopeOffset result = nextScopeOffset();
m_maxScopeOffset = result;
return result;
}
ScopeOffset takeNextScopeOffset()
{
ConcurrentJITLocker locker(m_lock);
return takeNextScopeOffset(locker);
}
template<typename Entry>
void add(const ConcurrentJITLocker&, UniquedStringImpl* key, Entry&& entry)
{
RELEASE_ASSERT(!m_localToEntry);
didUseVarOffset(entry.varOffset());
Map::AddResult result = m_map.add(key, std::forward<Entry>(entry));
ASSERT_UNUSED(result, result.isNewEntry);
}
template<typename Entry>
void add(UniquedStringImpl* key, Entry&& entry)
{
ConcurrentJITLocker locker(m_lock);
add(locker, key, std::forward<Entry>(entry));
}
template<typename Entry>
void set(const ConcurrentJITLocker&, UniquedStringImpl* key, Entry&& entry)
{
RELEASE_ASSERT(!m_localToEntry);
didUseVarOffset(entry.varOffset());
m_map.set(key, std::forward<Entry>(entry));
}
template<typename Entry>
void set(UniquedStringImpl* key, Entry&& entry)
{
ConcurrentJITLocker locker(m_lock);
set(locker, key, std::forward<Entry>(entry));
}
bool contains(const ConcurrentJITLocker&, UniquedStringImpl* key)
{
return m_map.contains(key);
}
bool contains(UniquedStringImpl* key)
{
ConcurrentJITLocker locker(m_lock);
return contains(locker, key);
}
uint32_t argumentsLength() const
{
if (!m_arguments)
return 0;
return m_arguments->length();
}
void setArgumentsLength(VM& vm, uint32_t length)
{
if (UNLIKELY(!m_arguments))
m_arguments.set(vm, this, ScopedArgumentsTable::create(vm));
m_arguments.set(vm, this, m_arguments->setLength(vm, length));
}
ScopeOffset argumentOffset(uint32_t i) const
{
ASSERT_WITH_SECURITY_IMPLICATION(m_arguments);
return m_arguments->get(i);
}
void setArgumentOffset(VM& vm, uint32_t i, ScopeOffset offset)
{
ASSERT_WITH_SECURITY_IMPLICATION(m_arguments);
m_arguments.set(vm, this, m_arguments->set(vm, i, offset));
}
ScopedArgumentsTable* arguments() const
{
if (!m_arguments)
return nullptr;
m_arguments->lock();
return m_arguments.get();
}
const LocalToEntryVec& localToEntry(const ConcurrentJITLocker&);
SymbolTableEntry* entryFor(const ConcurrentJITLocker&, ScopeOffset);
GlobalVariableID uniqueIDForVariable(const ConcurrentJITLocker&, UniquedStringImpl* key, VM&);
GlobalVariableID uniqueIDForOffset(const ConcurrentJITLocker&, VarOffset, VM&);
RefPtr<TypeSet> globalTypeSetForOffset(const ConcurrentJITLocker&, VarOffset, VM&);
RefPtr<TypeSet> globalTypeSetForVariable(const ConcurrentJITLocker&, UniquedStringImpl* key, VM&);
bool usesNonStrictEval() const { return m_usesNonStrictEval; }
void setUsesNonStrictEval(bool usesNonStrictEval) { m_usesNonStrictEval = usesNonStrictEval; }
bool isNestedLexicalScope() const { return m_nestedLexicalScope; }
void markIsNestedLexicalScope() { ASSERT(scopeType() == LexicalScope); m_nestedLexicalScope = true; }
enum ScopeType {
VarScope,
GlobalLexicalScope,
LexicalScope,
CatchScope,
FunctionNameScope
};
void setScopeType(ScopeType type) { m_scopeType = type; }
ScopeType scopeType() const { return static_cast<ScopeType>(m_scopeType); }
SymbolTable* cloneScopePart(VM&);
void prepareForTypeProfiling(const ConcurrentJITLocker&);
CodeBlock* rareDataCodeBlock();
void setRareDataCodeBlock(CodeBlock*);
InferredValue* singletonScope() { return m_singletonScope.get(); }
static void visitChildren(JSCell*, SlotVisitor&);
DECLARE_EXPORT_INFO;
private:
JS_EXPORT_PRIVATE SymbolTable(VM&);
~SymbolTable();
JS_EXPORT_PRIVATE void finishCreation(VM&);
Map m_map;
ScopeOffset m_maxScopeOffset;
struct SymbolTableRareData {
UniqueIDMap m_uniqueIDMap;
OffsetToVariableMap m_offsetToVariableMap;
UniqueTypeSetMap m_uniqueTypeSetMap;
WriteBarrier<CodeBlock> m_codeBlock;
};
std::unique_ptr<SymbolTableRareData> m_rareData;
bool m_usesNonStrictEval : 1;
bool m_nestedLexicalScope : 1; unsigned m_scopeType : 3;
WriteBarrier<ScopedArgumentsTable> m_arguments;
WriteBarrier<InferredValue> m_singletonScope;
std::unique_ptr<LocalToEntryVec> m_localToEntry;
public:
mutable ConcurrentJITLock m_lock;
};
}
#endif // SymbolTable_h