SymbolTable.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. /*
  2. * Copyright (C) 2007, 2008, 2012 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 Computer, 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. #ifndef SymbolTable_h
  29. #define SymbolTable_h
  30. #include "JSObject.h"
  31. #include "Watchpoint.h"
  32. #include <wtf/HashTraits.h>
  33. #include <wtf/text/StringImpl.h>
  34. namespace JSC {
  35. class Watchpoint;
  36. class WatchpointSet;
  37. struct SlowArgument {
  38. enum Status {
  39. Normal = 0,
  40. Captured = 1,
  41. Deleted = 2
  42. };
  43. SlowArgument()
  44. : status(Normal)
  45. , index(0)
  46. {
  47. }
  48. Status status;
  49. int index; // If status is 'Deleted', index is bogus.
  50. };
  51. static ALWAYS_INLINE int missingSymbolMarker() { return std::numeric_limits<int>::max(); }
  52. // The bit twiddling in this class assumes that every register index is a
  53. // reasonably small positive or negative number, and therefore has its high
  54. // four bits all set or all unset.
  55. // In addition to implementing semantics-mandated variable attributes and
  56. // implementation-mandated variable indexing, this class also implements
  57. // watchpoints to be used for JIT optimizations. Because watchpoints are
  58. // meant to be relatively rare, this class optimizes heavily for the case
  59. // that they are not being used. To that end, this class uses the thin-fat
  60. // idiom: either it is thin, in which case it contains an in-place encoded
  61. // word that consists of attributes, the index, and a bit saying that it is
  62. // thin; or it is fat, in which case it contains a pointer to a malloc'd
  63. // data structure and a bit saying that it is fat. The malloc'd data
  64. // structure will be malloced a second time upon copy, to preserve the
  65. // property that in-place edits to SymbolTableEntry do not manifest in any
  66. // copies. However, the malloc'd FatEntry data structure contains a ref-
  67. // counted pointer to a shared WatchpointSet. Thus, in-place edits of the
  68. // WatchpointSet will manifest in all copies. Here's a picture:
  69. //
  70. // SymbolTableEntry --> FatEntry --> WatchpointSet
  71. //
  72. // If you make a copy of a SymbolTableEntry, you will have:
  73. //
  74. // original: SymbolTableEntry --> FatEntry --> WatchpointSet
  75. // copy: SymbolTableEntry --> FatEntry -----^
  76. struct SymbolTableEntry {
  77. // Use the SymbolTableEntry::Fast class, either via implicit cast or by calling
  78. // getFast(), when you (1) only care about isNull(), getIndex(), and isReadOnly(),
  79. // and (2) you are in a hot path where you need to minimize the number of times
  80. // that you branch on isFat() when getting the bits().
  81. class Fast {
  82. public:
  83. Fast()
  84. : m_bits(SlimFlag)
  85. {
  86. }
  87. ALWAYS_INLINE Fast(const SymbolTableEntry& entry)
  88. : m_bits(entry.bits())
  89. {
  90. }
  91. bool isNull() const
  92. {
  93. return !(m_bits & ~SlimFlag);
  94. }
  95. int getIndex() const
  96. {
  97. return static_cast<int>(m_bits >> FlagBits);
  98. }
  99. bool isReadOnly() const
  100. {
  101. return m_bits & ReadOnlyFlag;
  102. }
  103. unsigned getAttributes() const
  104. {
  105. unsigned attributes = 0;
  106. if (m_bits & ReadOnlyFlag)
  107. attributes |= ReadOnly;
  108. if (m_bits & DontEnumFlag)
  109. attributes |= DontEnum;
  110. return attributes;
  111. }
  112. bool isFat() const
  113. {
  114. return !(m_bits & SlimFlag);
  115. }
  116. private:
  117. friend struct SymbolTableEntry;
  118. intptr_t m_bits;
  119. };
  120. SymbolTableEntry()
  121. : m_bits(SlimFlag)
  122. {
  123. }
  124. SymbolTableEntry(int index)
  125. : m_bits(SlimFlag)
  126. {
  127. ASSERT(isValidIndex(index));
  128. pack(index, false, false);
  129. }
  130. SymbolTableEntry(int index, unsigned attributes)
  131. : m_bits(SlimFlag)
  132. {
  133. ASSERT(isValidIndex(index));
  134. pack(index, attributes & ReadOnly, attributes & DontEnum);
  135. }
  136. ~SymbolTableEntry()
  137. {
  138. freeFatEntry();
  139. }
  140. SymbolTableEntry(const SymbolTableEntry& other)
  141. : m_bits(SlimFlag)
  142. {
  143. *this = other;
  144. }
  145. SymbolTableEntry& operator=(const SymbolTableEntry& other)
  146. {
  147. if (UNLIKELY(other.isFat()))
  148. return copySlow(other);
  149. freeFatEntry();
  150. m_bits = other.m_bits;
  151. return *this;
  152. }
  153. bool isNull() const
  154. {
  155. return !(bits() & ~SlimFlag);
  156. }
  157. int getIndex() const
  158. {
  159. return static_cast<int>(bits() >> FlagBits);
  160. }
  161. ALWAYS_INLINE Fast getFast() const
  162. {
  163. return Fast(*this);
  164. }
  165. ALWAYS_INLINE Fast getFast(bool& wasFat) const
  166. {
  167. Fast result;
  168. wasFat = isFat();
  169. if (wasFat)
  170. result.m_bits = fatEntry()->m_bits | SlimFlag;
  171. else
  172. result.m_bits = m_bits;
  173. return result;
  174. }
  175. unsigned getAttributes() const
  176. {
  177. return getFast().getAttributes();
  178. }
  179. void setAttributes(unsigned attributes)
  180. {
  181. pack(getIndex(), attributes & ReadOnly, attributes & DontEnum);
  182. }
  183. bool isReadOnly() const
  184. {
  185. return bits() & ReadOnlyFlag;
  186. }
  187. bool couldBeWatched();
  188. // Notify an opportunity to create a watchpoint for a variable. This is
  189. // idempotent and fail-silent. It is idempotent in the sense that if
  190. // a watchpoint set had already been created, then another one will not
  191. // be created. Hence two calls to this method have the same effect as
  192. // one call. It is also fail-silent, in the sense that if a watchpoint
  193. // set had been created and had already been invalidated, then this will
  194. // just return. This means that couldBeWatched() may return false even
  195. // immediately after a call to attemptToWatch().
  196. void attemptToWatch();
  197. bool* addressOfIsWatched();
  198. void addWatchpoint(Watchpoint*);
  199. WatchpointSet* watchpointSet()
  200. {
  201. return fatEntry()->m_watchpoints.get();
  202. }
  203. ALWAYS_INLINE void notifyWrite()
  204. {
  205. if (LIKELY(!isFat()))
  206. return;
  207. notifyWriteSlow();
  208. }
  209. private:
  210. static const intptr_t SlimFlag = 0x1;
  211. static const intptr_t ReadOnlyFlag = 0x2;
  212. static const intptr_t DontEnumFlag = 0x4;
  213. static const intptr_t NotNullFlag = 0x8;
  214. static const intptr_t FlagBits = 4;
  215. class FatEntry {
  216. #if ENABLE(DETACHED_JIT)
  217. DETACHED_JIT_MAKE_SHARED_DATA_ALLOCATED;
  218. #else
  219. WTF_MAKE_FAST_ALLOCATED;
  220. #endif
  221. public:
  222. FatEntry(intptr_t bits)
  223. : m_bits(bits & ~SlimFlag)
  224. {
  225. }
  226. intptr_t m_bits; // always has FatFlag set and exactly matches what the bits would have been if this wasn't fat.
  227. RefPtr<WatchpointSet> m_watchpoints;
  228. };
  229. SymbolTableEntry& copySlow(const SymbolTableEntry&);
  230. JS_EXPORT_PRIVATE void notifyWriteSlow();
  231. bool isFat() const
  232. {
  233. return !(m_bits & SlimFlag);
  234. }
  235. const FatEntry* fatEntry() const
  236. {
  237. ASSERT(isFat());
  238. return bitwise_cast<const FatEntry*>(m_bits);
  239. }
  240. FatEntry* fatEntry()
  241. {
  242. ASSERT(isFat());
  243. return bitwise_cast<FatEntry*>(m_bits);
  244. }
  245. FatEntry* inflate()
  246. {
  247. if (LIKELY(isFat()))
  248. return fatEntry();
  249. return inflateSlow();
  250. }
  251. FatEntry* inflateSlow();
  252. ALWAYS_INLINE intptr_t bits() const
  253. {
  254. if (isFat())
  255. return fatEntry()->m_bits;
  256. return m_bits;
  257. }
  258. ALWAYS_INLINE intptr_t& bits()
  259. {
  260. if (isFat())
  261. return fatEntry()->m_bits;
  262. return m_bits;
  263. }
  264. void freeFatEntry()
  265. {
  266. if (LIKELY(!isFat()))
  267. return;
  268. freeFatEntrySlow();
  269. }
  270. JS_EXPORT_PRIVATE void freeFatEntrySlow();
  271. void pack(int index, bool readOnly, bool dontEnum)
  272. {
  273. ASSERT(!isFat());
  274. intptr_t& bitsRef = bits();
  275. bitsRef = (static_cast<intptr_t>(index) << FlagBits) | NotNullFlag | SlimFlag;
  276. if (readOnly)
  277. bitsRef |= ReadOnlyFlag;
  278. if (dontEnum)
  279. bitsRef |= DontEnumFlag;
  280. }
  281. bool isValidIndex(int index)
  282. {
  283. return ((static_cast<intptr_t>(index) << FlagBits) >> FlagBits) == static_cast<intptr_t>(index);
  284. }
  285. intptr_t m_bits;
  286. };
  287. struct SymbolTableIndexHashTraits : HashTraits<SymbolTableEntry> {
  288. static const bool needsDestruction = true;
  289. };
  290. typedef HashMap_shared<RefPtr<StringImpl>, SymbolTableEntry, IdentifierRepHash, HashTraits<RefPtr<StringImpl> >, SymbolTableIndexHashTraits> SymbolTable;
  291. class SharedSymbolTable : public JSCell, public SymbolTable {
  292. public:
  293. typedef JSCell Base;
  294. static SharedSymbolTable* create(VM& vm)
  295. {
  296. SharedSymbolTable* sharedSymbolTable = new (NotNull, allocateCell<SharedSymbolTable>(vm.heap)) SharedSymbolTable(vm);
  297. sharedSymbolTable->finishCreation(vm);
  298. return sharedSymbolTable;
  299. }
  300. static const bool needsDestruction = true;
  301. static const bool hasImmortalStructure = true;
  302. static void destroy(JSCell*);
  303. static Structure* createStructure(VM& vm, JSGlobalObject* globalObject, JSValue prototype)
  304. {
  305. return Structure::create(vm, globalObject, prototype, TypeInfo(LeafType, StructureFlags), &s_info);
  306. }
  307. bool usesNonStrictEval() { return m_usesNonStrictEval; }
  308. void setUsesNonStrictEval(bool usesNonStrictEval) { m_usesNonStrictEval = usesNonStrictEval; }
  309. int captureStart() { return m_captureStart; }
  310. void setCaptureStart(int captureStart) { m_captureStart = captureStart; }
  311. int captureEnd() { return m_captureEnd; }
  312. void setCaptureEnd(int captureEnd) { m_captureEnd = captureEnd; }
  313. int captureCount() { return m_captureEnd - m_captureStart; }
  314. int parameterCount() { return m_parameterCountIncludingThis - 1; }
  315. int parameterCountIncludingThis() { return m_parameterCountIncludingThis; }
  316. void setParameterCountIncludingThis(int parameterCountIncludingThis) { m_parameterCountIncludingThis = parameterCountIncludingThis; }
  317. // 0 if we don't capture any arguments; parameterCount() in length if we do.
  318. const SlowArgument* slowArguments() { return m_slowArguments.get(); }
  319. void setSlowArguments(PassOwnArrayPtr<SlowArgument> slowArguments) { m_slowArguments = slowArguments; }
  320. static JS_EXPORTDATA const ClassInfo s_info;
  321. private:
  322. SharedSymbolTable(VM& vm)
  323. : JSCell(vm, vm.sharedSymbolTableStructure.get())
  324. , m_parameterCountIncludingThis(0)
  325. , m_usesNonStrictEval(false)
  326. , m_captureStart(0)
  327. , m_captureEnd(0)
  328. {
  329. }
  330. int m_parameterCountIncludingThis;
  331. bool m_usesNonStrictEval;
  332. int m_captureStart;
  333. int m_captureEnd;
  334. OwnArrayPtr<SlowArgument> m_slowArguments;
  335. };
  336. } // namespace JSC
  337. #endif // SymbolTable_h