forked from chakra-core/ChakraCore
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHash.h
More file actions
482 lines (415 loc) · 14.3 KB
/
Hash.h
File metadata and controls
482 lines (415 loc) · 14.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
//-------------------------------------------------------------------------------------------------------
// Copyright (C) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
//-------------------------------------------------------------------------------------------------------
#pragma once
// StaticSym contains a string literal at the end (flexible array) and is
// meant to be initialized statically. However, flexible array initialization
// is not allowed in standard C++. We declare each StaticSym with length
// instead and cast to common StaticSymLen<0>* (StaticSym*) to access.
template <uint32 N>
struct StaticSymLen
{
uint32 luHash;
uint32 cch;
OLECHAR sz[N];
};
typedef StaticSymLen<0> StaticSym;
/***************************************************************************
Hashing functions. Definitions in core\hashfunc.cpp.
***************************************************************************/
ULONG CaseSensitiveComputeHash(LPCOLESTR prgch, LPCOLESTR end);
ULONG CaseSensitiveComputeHash(LPCUTF8 prgch, LPCUTF8 end);
ULONG CaseInsensitiveComputeHash(LPCOLESTR posz);
enum
{
fidNil = 0x0000,
fidKwdRsvd = 0x0001, // the keyword is a reserved word
fidKwdFutRsvd = 0x0002, // a future reserved word, but only in strict mode
fidModuleExport = 0x8000 // name is module export
};
struct BlockIdsStack
{
int id;
BlockIdsStack *prev;
};
class Span
{
charcount_t m_ichMin;
charcount_t m_ichLim;
public:
Span(): m_ichMin((charcount_t)-1), m_ichLim((charcount_t)-1) { }
Span(charcount_t ichMin, charcount_t ichLim): m_ichMin(ichMin), m_ichLim(ichLim) { }
charcount_t GetIchMin() { return m_ichMin; }
charcount_t GetIchLim() { Assert(m_ichMin != (charcount_t)-1); return m_ichLim; }
void Set(charcount_t ichMin, charcount_t ichLim)
{
m_ichMin = ichMin;
m_ichLim = ichLim;
}
operator bool() { return m_ichMin != -1; }
};
struct PidRefStack
{
PidRefStack() : isAsg(false), isDynamic(false), id(0), funcId(0), sym(nullptr), prev(nullptr), isEscape(false), isModuleExport(false), isFuncAssignment(false), isUsedInLdElem(false) {}
PidRefStack(int id, Js::LocalFunctionId funcId) : isAsg(false), isDynamic(false), id(id), funcId(funcId), sym(nullptr), prev(nullptr), isEscape(false), isModuleExport(false), isFuncAssignment(false), isUsedInLdElem(false) {}
int GetScopeId() const { return id; }
Js::LocalFunctionId GetFuncScopeId() const { return funcId; }
Symbol *GetSym() const { return sym; }
void SetSym(Symbol *sym) { this->sym = sym; }
bool IsAssignment() const { return isAsg; }
bool IsFuncAssignment() const { return isFuncAssignment; }
bool IsEscape() const { return isEscape; }
void SetIsEscape(bool is) { isEscape = is; }
bool IsDynamicBinding() const { return isDynamic; }
void SetDynamicBinding() { isDynamic = true; }
bool IsUsedInLdElem() const { return isUsedInLdElem; }
void SetIsUsedInLdElem(bool is) { isUsedInLdElem = is; }
Symbol **GetSymRef()
{
return &sym;
}
bool isAsg;
bool isDynamic;
bool isModuleExport;
bool isEscape;
bool isFuncAssignment;
bool isUsedInLdElem;
int id;
Js::LocalFunctionId funcId;
Symbol *sym;
PidRefStack *prev;
};
enum AssignmentState : byte {
NotAssigned,
AssignedOnce,
AssignedMultipleTimes
};
struct Ident
{
friend class HashTbl;
private:
Ident * m_pidNext; // next identifier in this hash bucket
PidRefStack *m_pidRefStack;
ushort m_tk; // token# if identifier is a keyword
ushort m_grfid; // see fidXXX above
uint32 m_luHash; // hash value
uint32 m_cch; // length of the identifier spelling
Js::PropertyId m_propertyId;
AssignmentState assignmentState;
bool isUsedInLdElem;
OLECHAR m_sz[]; // the spelling follows (null terminated)
void SetTk(tokens tk, ushort grfid);
public:
LPCOLESTR Psz(void)
{ return m_sz; }
uint32 Cch(void)
{ return m_cch; }
tokens Tk(bool isStrictMode);
uint32 Hash(void)
{ return m_luHash; }
PidRefStack *GetTopRef() const
{
return m_pidRefStack;
}
PidRefStack *GetTopRef(uint maxBlockId) const
{
PidRefStack *ref;
for (ref = m_pidRefStack; ref && (uint)ref->id > maxBlockId; ref = ref->prev)
{
; // nothing
}
return ref;
}
void SetTopRef(PidRefStack *ref)
{
m_pidRefStack = ref;
}
void PromoteAssignmentState()
{
if (assignmentState == NotAssigned)
{
assignmentState = AssignedOnce;
}
else if (assignmentState == AssignedOnce)
{
assignmentState = AssignedMultipleTimes;
}
}
bool IsUsedInLdElem() const
{
return this->isUsedInLdElem;
}
void SetIsUsedInLdElem(bool is)
{
this->isUsedInLdElem = is;
}
static void TrySetIsUsedInLdElem(ParseNode * pnode);
bool IsSingleAssignment()
{
return assignmentState == AssignedOnce;
}
PidRefStack *GetPidRefForScopeId(int scopeId)
{
PidRefStack *ref;
for (ref = m_pidRefStack; ref; ref = ref->prev)
{
int refId = ref->GetScopeId();
if (refId == scopeId)
{
return ref;
}
if (refId < scopeId)
{
break;
}
}
return nullptr;
}
void PushPidRef(int blockId, Js::LocalFunctionId funcId, PidRefStack *newRef)
{
AssertMsg(blockId >= 0, "Block Id's should be greater than 0");
newRef->id = blockId;
newRef->funcId = funcId;
newRef->prev = m_pidRefStack;
m_pidRefStack = newRef;
}
PidRefStack * RemovePrevPidRef(PidRefStack *ref)
{
PidRefStack *prevRef;
if (ref == nullptr)
{
prevRef = m_pidRefStack;
Assert(prevRef);
m_pidRefStack = prevRef->prev;
}
else
{
prevRef = ref->prev;
Assert(prevRef);
ref->prev = prevRef->prev;
}
return prevRef;
}
PidRefStack * FindOrAddPidRef(ArenaAllocator *alloc, int scopeId, Js::LocalFunctionId funcId)
{
// If the stack is empty, or we are pushing to the innermost scope already,
// we can go ahead and push a new PidRef on the stack.
if (m_pidRefStack == nullptr)
{
PidRefStack *newRef = Anew(alloc, PidRefStack, scopeId, funcId);
if (newRef == nullptr)
{
return nullptr;
}
newRef->prev = m_pidRefStack;
m_pidRefStack = newRef;
return newRef;
}
// Search for the corresponding PidRef, or the position to insert the new PidRef.
PidRefStack *ref = m_pidRefStack;
PidRefStack *prevRef = nullptr;
while (1)
{
// We may already have a ref for this scopeId.
if (ref->id == scopeId)
{
return ref;
}
if (ref->prev == nullptr || ref->id < scopeId)
{
// No existing PidRef for this scopeId, so create and insert one at this position.
PidRefStack *newRef = Anew(alloc, PidRefStack, scopeId, funcId);
if (newRef == nullptr)
{
return nullptr;
}
if (ref->id < scopeId)
{
if (prevRef != nullptr)
{
// Param scope has a reference to the same pid as the one we are inserting into the body.
// There is a another reference (prevRef), probably from an inner block in the body.
// So we should insert the new reference between them.
newRef->prev = prevRef->prev;
prevRef->prev = newRef;
}
else
{
// When we have code like below, prevRef will be null,
// function (a = x) { var x = 1; }
newRef->prev = m_pidRefStack;
m_pidRefStack = newRef;
}
}
else
{
newRef->prev = ref->prev;
ref->prev = newRef;
}
return newRef;
}
prevRef = ref;
ref = ref->prev;
}
}
Js::PropertyId GetPropertyId() const { return m_propertyId; }
void SetPropertyId(Js::PropertyId id) { m_propertyId = id; }
void SetIsModuleExport() { m_grfid |= fidModuleExport; }
BOOL GetIsModuleExport() const { return m_grfid & fidModuleExport; }
static tokens TkFromNameLen(uint32 luHash, _In_reads_(cch) LPCOLESTR prgch, uint32 cch, bool isStrictMode, ushort * pgrfid, ushort * ptk);
#if DBG
static tokens TkFromNameLen(_In_reads_(cch) LPCOLESTR prgch, uint32 cch, bool isStrictMode);
#endif
};
/*****************************************************************************/
class HashTbl
{
public:
HashTbl(uint cidHash = DEFAULT_HASH_TABLE_SIZE)
{
AssertCanHandleOutOfMemory();
m_prgpidName = nullptr;
memset(&m_rpid, 0, sizeof(m_rpid));
if (!Init(cidHash))
{
Js::Throw::OutOfMemory();
}
}
~HashTbl(void) {}
BOOL TokIsBinop(tokens tk, int *popl, OpCode *pnop)
{
const KWD *pkwd = KwdOfTok(tk);
if (nullptr == pkwd)
return FALSE;
*popl = pkwd->prec2;
*pnop = pkwd->nop2;
return TRUE;
}
BOOL TokIsUnop(tokens tk, int *popl, OpCode *pnop)
{
const KWD *pkwd = KwdOfTok(tk);
if (nullptr == pkwd)
return FALSE;
*popl = pkwd->prec1;
*pnop = pkwd->nop1;
return TRUE;
}
IdentPtr PidFromTk(tokens tk);
IdentPtr PidHashName(LPCOLESTR psz)
{
size_t csz = wcslen(psz);
Assert(csz <= ULONG_MAX);
return PidHashNameLen(psz, static_cast<uint32>(csz));
}
template <typename CharType>
IdentPtr PidHashNameLen(CharType const * psz, CharType const * end, uint32 cch);
template <typename CharType>
IdentPtr PidHashNameLen(CharType const * psz, uint32 cch);
template <typename CharType>
IdentPtr PidHashNameLenWithHash(_In_reads_(cch) CharType const * psz, CharType const * end, int32 cch, uint32 luHash);
template <typename CharType>
inline IdentPtr FindExistingPid(
CharType const * prgch,
CharType const * end,
int32 cch,
uint32 luHash,
IdentPtr **pppInsert,
int32 *pBucketCount
#if PROFILE_DICTIONARY
, int& depth
#endif
);
NoReleaseAllocator* GetAllocator() {return &m_noReleaseAllocator;}
bool Contains(_In_reads_(cch) LPCOLESTR prgch, int32 cch);
template<typename Fn>
void VisitPids(Fn fn)
{
for (uint i = 0; i <= m_luMask; i++)
{
for (IdentPtr pid = m_prgpidName[i]; pid; pid = pid->m_pidNext)
{
fn(pid);
}
}
}
private:
static const uint DEFAULT_HASH_TABLE_SIZE = 256;
NoReleaseAllocator m_noReleaseAllocator; // to allocate identifiers
Ident ** m_prgpidName; // hash table for names
uint32 m_luMask; // hash mask
uint32 m_luCount; // count of the number of entires in the hash table
IdentPtr m_rpid[tkLimKwd];
// Called to grow the number of buckets in the table to reduce the table density.
void Grow();
// Automatically grow the table if a bucket's length grows beyond BucketLengthLimit and the table is densely populated.
static const uint BucketLengthLimit = 5;
// When growing the bucket size we'll grow by GrowFactor. GrowFactor MUST be a power of 2.
static const uint GrowFactor = 4;
#if DEBUG
uint CountAndVerifyItems(IdentPtr *buckets, uint bucketCount, uint mask);
#endif
static bool CharsAreEqual(__in_z LPCOLESTR psz1, __in_ecount(psz2end - psz2) LPCOLESTR psz2, LPCOLESTR psz2end)
{
return memcmp(psz1, psz2, (psz2end - psz2) * sizeof(OLECHAR)) == 0;
}
static bool CharsAreEqual(__in_z LPCOLESTR psz1, LPCUTF8 psz2, LPCUTF8 psz2end)
{
return utf8::CharsAreEqual(psz1, psz2, psz2end, utf8::doAllowThreeByteSurrogates);
}
static bool CharsAreEqual(__in_z LPCOLESTR psz1, __in_ecount(psz2end - psz2) char const * psz2, char const * psz2end)
{
while (psz2 < psz2end)
{
if (*psz1++ != *psz2++)
{
return false;
}
}
return true;
}
static void CopyString(__in_ecount((psz2end - psz2) + 1) LPOLESTR psz1, __in_ecount(psz2end - psz2) LPCOLESTR psz2, LPCOLESTR psz2end)
{
size_t cch = psz2end - psz2;
js_memcpy_s(psz1, cch * sizeof(OLECHAR), psz2, cch * sizeof(OLECHAR));
psz1[cch] = 0;
}
static void CopyString(LPOLESTR psz1, LPCUTF8 psz2, LPCUTF8 psz2end)
{
utf8::DecodeUnitsIntoAndNullTerminate(psz1, psz2, psz2end);
}
static void CopyString(LPOLESTR psz1, char const * psz2, char const * psz2end)
{
while (psz2 < psz2end)
{
*(psz1++) = *psz2++;
}
*psz1 = 0;
}
// note: on failure this may throw or return FALSE, depending on
// where the failure occurred.
BOOL Init(uint cidHash);
/*************************************************************************/
/* The following members are related to the keyword descriptor tables */
/*************************************************************************/
struct KWD
{
OpCode nop2;
byte prec2;
OpCode nop1;
byte prec1;
};
struct ReservedWordInfo
{
StaticSym const * sym;
ushort grfid;
};
static const ReservedWordInfo s_reservedWordInfo[tkID];
static const KWD g_mptkkwd[tkLimKwd];
static const KWD * KwdOfTok(tokens tk)
{ return (unsigned int)tk < tkLimKwd ? g_mptkkwd + tk : nullptr; }
static __declspec(noreturn) void OutOfMemory();
#if PROFILE_DICTIONARY
DictionaryStats *stats;
#endif
};