Skip to content

Commit a0eaf8d

Browse files
committed
Change unused parameter check point
1 parent 9f4d898 commit a0eaf8d

File tree

6 files changed

+105
-74
lines changed

6 files changed

+105
-74
lines changed

src/codecache/CodeCacheReaderWriter.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -151,16 +151,16 @@ void CodeCacheWriter::storeInterpretedCodeBlock(InterpretedCodeBlock* codeBlock)
151151
m_buffer.put(stringIndex);
152152
}
153153

154+
#ifndef ESCARGOT_DEBUGGER
154155
// InterpretedCodeBlock::m_parameterUsed
155-
const InterpretedCodeBlock::ParameterUsedVector& parameterUsedVector = codeBlock->m_parameterUsed;
156+
const TightVector<bool, GCUtil::gc_malloc_atomic_allocator<bool>>& parameterUsedVector = codeBlock->m_parameterUsed;
156157
size = parameterUsedVector.size();
157-
m_buffer.ensureSize(sizeof(size_t) + size * (sizeof(size_t) + sizeof(bool)));
158+
m_buffer.ensureSize(sizeof(size_t) + size * sizeof(bool));
158159
m_buffer.put(size);
159160
for (size_t i = 0; i < size; i++) {
160-
const InterpretedCodeBlock::ParameterUsed& info = parameterUsedVector[i];
161-
m_buffer.put(m_stringTable->add(info.m_name));
162-
m_buffer.put(info.m_isUsed);
161+
m_buffer.put(parameterUsedVector[i]);
163162
}
163+
#endif
164164

165165
// InterpretedCodeBlock::m_identifierInfos
166166
const InterpretedCodeBlock::IdentifierInfoVector& identifierVector = codeBlock->m_identifierInfos;
@@ -734,16 +734,16 @@ InterpretedCodeBlock* CodeCacheReader::loadInterpretedCodeBlock(Context* context
734734
atomicStringVector[i] = m_stringTable->get(stringIndex);
735735
}
736736

737+
#ifndef ESCARGOT_DEBUGGER
737738
// InterpretedCodeBlock::m_parameterUsed
738-
InterpretedCodeBlock::ParameterUsedVector& parameterUsedVector = codeBlock->m_parameterUsed;
739+
TightVector<bool, GCUtil::gc_malloc_atomic_allocator<bool>>& parameterUsedVector = codeBlock->m_parameterUsed;
739740
size = m_buffer.get<size_t>();
740741
parameterUsedVector.resizeWithUninitializedValues(size);
741742
for (size_t i = 0; i < size; i++) {
742-
InterpretedCodeBlock::ParameterUsed info;
743-
info.m_name = m_stringTable->get(m_buffer.get<size_t>());
744-
info.m_isUsed = m_buffer.get<bool>();
745-
parameterUsedVector[i] = info;
743+
bool isUsed = m_buffer.get<bool>();
744+
parameterUsedVector[i] = isUsed;
746745
}
746+
#endif
747747

748748
// InterpretedCodeBlock::m_identifierInfos
749749
InterpretedCodeBlock::IdentifierInfoVector& identifierVector = codeBlock->m_identifierInfos;

src/heap/CustomAllocator.cpp

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,12 +168,19 @@ int getValidValueInInterpretedCodeBlock(void* ptr, GC_mark_custom_result* arr)
168168
arr[4].to = (GC_word*)current->m_children;
169169
arr[5].from = (GC_word*)&current->m_parameterNames;
170170
arr[5].to = (GC_word*)current->m_parameterNames.data();
171+
#ifndef ESCARGOT_DEBUGGER
171172
arr[6].from = (GC_word*)&current->m_parameterUsed;
172173
arr[6].to = (GC_word*)current->m_parameterUsed.data();
173174
arr[7].from = (GC_word*)&current->m_identifierInfos;
174175
arr[7].to = (GC_word*)current->m_identifierInfos.data();
175176
arr[8].from = (GC_word*)&current->m_blockInfos;
176177
arr[8].to = (GC_word*)current->m_blockInfos;
178+
#else
179+
arr[6].from = (GC_word*)&current->m_identifierInfos;
180+
arr[6].to = (GC_word*)current->m_identifierInfos.data();
181+
arr[7].from = (GC_word*)&current->m_blockInfos;
182+
arr[7].to = (GC_word*)current->m_blockInfos;
183+
#endif
177184
return 0;
178185
}
179186

@@ -192,6 +199,7 @@ int getValidValueInInterpretedCodeBlockWithRareData(void* ptr, GC_mark_custom_re
192199
arr[4].to = (GC_word*)current->m_children;
193200
arr[5].from = (GC_word*)&current->m_parameterNames;
194201
arr[5].to = (GC_word*)current->m_parameterNames.data();
202+
#ifndef ESCARGOT_DEBUGGER
195203
arr[6].from = (GC_word*)&current->m_parameterUsed;
196204
arr[6].to = (GC_word*)current->m_parameterUsed.data();
197205
arr[7].from = (GC_word*)&current->m_identifierInfos;
@@ -200,6 +208,14 @@ int getValidValueInInterpretedCodeBlockWithRareData(void* ptr, GC_mark_custom_re
200208
arr[8].to = (GC_word*)current->m_blockInfos;
201209
arr[9].from = (GC_word*)&current->m_rareData;
202210
arr[9].to = (GC_word*)current->m_rareData;
211+
#else
212+
arr[6].from = (GC_word*)&current->m_identifierInfos;
213+
arr[6].to = (GC_word*)current->m_identifierInfos.data();
214+
arr[7].from = (GC_word*)&current->m_blockInfos;
215+
arr[7].to = (GC_word*)current->m_blockInfos;
216+
arr[8].from = (GC_word*)&current->m_rareData;
217+
arr[8].to = (GC_word*)current->m_rareData;
218+
#endif
203219
return 0;
204220
}
205221

@@ -292,7 +308,7 @@ void initializeCustomAllocators()
292308
GC_MAKE_PROC(GC_new_proc(markAndPushCustom<getValidValueInArrayBufferObject, 5>), 0),
293309
FALSE,
294310
TRUE);
295-
311+
#ifndef ESCARGOT_DEBUGGER
296312
s_gcKinds[HeapObjectKind::InterpretedCodeBlockKind] = GC_new_kind(GC_new_free_list(),
297313
GC_MAKE_PROC(GC_new_proc(markAndPushCustom<getValidValueInInterpretedCodeBlock, 9>), 0),
298314
FALSE,
@@ -302,6 +318,17 @@ void initializeCustomAllocators()
302318
GC_MAKE_PROC(GC_new_proc(markAndPushCustom<getValidValueInInterpretedCodeBlockWithRareData, 10>), 0),
303319
FALSE,
304320
TRUE);
321+
#else
322+
s_gcKinds[HeapObjectKind::InterpretedCodeBlockKind] = GC_new_kind(GC_new_free_list(),
323+
GC_MAKE_PROC(GC_new_proc(markAndPushCustom<getValidValueInInterpretedCodeBlock, 8>), 0),
324+
FALSE,
325+
TRUE);
326+
327+
s_gcKinds[HeapObjectKind::InterpretedCodeBlockWithRareDataKind] = GC_new_kind(GC_new_free_list(),
328+
GC_MAKE_PROC(GC_new_proc(markAndPushCustom<getValidValueInInterpretedCodeBlockWithRareData, 9>), 0),
329+
FALSE,
330+
TRUE);
331+
#endif
305332

306333
s_gcKinds[HeapObjectKind::WeakRefObjectKind] = GC_new_kind(GC_new_free_list(),
307334
GC_MAKE_PROC(GC_new_proc(markAndPushCustom<getValidValueInWeakRefObject, 3>), 0),

src/parser/CodeBlock.cpp

Lines changed: 5 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -310,48 +310,6 @@ InterpretedCodeBlock::InterpretedCodeBlock(Context* ctx, Script* script)
310310
{
311311
}
312312

313-
bool InterpretedCodeBlock::innerIsParameterUsed(ASTBlockContext* block, AtomicString& name)
314-
{
315-
for (size_t i = 0; i < block->m_usingNames.size(); i++) {
316-
if (block->m_usingNames[i] == name) {
317-
return true;
318-
} else if (block->m_usingNames[i] == "arguments") {
319-
// if 'arguments' is used in body, all parameters are used
320-
return true;
321-
} else if (block->m_usingNames[i] == "eval") {
322-
// if 'eval' is used in body, all parameters are used
323-
return true;
324-
}
325-
}
326-
return false;
327-
}
328-
329-
bool InterpretedCodeBlock::isParameterUsed(ASTScopeContext* scopeCtx, AtomicString& name)
330-
{
331-
for (size_t i = 0; i < scopeCtx->m_childBlockScopes.size(); i++) {
332-
if (innerIsParameterUsed(scopeCtx->m_childBlockScopes[i], name)) {
333-
return true;
334-
}
335-
}
336-
return false;
337-
}
338-
339-
void InterpretedCodeBlock::recordParameterUsage(ASTScopeContext* scopeCtx)
340-
{
341-
for (size_t i = 0; i < m_parameterUsed.size(); i++) {
342-
if (isParameterUsed(scopeCtx, m_parameterUsed[i].m_name)) {
343-
m_parameterUsed[i].m_isUsed = true;
344-
}
345-
}
346-
347-
ASTScopeContext* curCtx = scopeCtx->firstChild();
348-
// check inner functions
349-
while (curCtx) {
350-
recordParameterUsage(curCtx);
351-
curCtx = curCtx->nextSibling();
352-
}
353-
}
354-
355313
void InterpretedCodeBlock::recordGlobalParsingInfo(ASTScopeContext* scopeCtx, bool isEvalCode, bool isEvalCodeInFunction)
356314
{
357315
m_isStrict = scopeCtx->m_isStrict;
@@ -433,17 +391,15 @@ void InterpretedCodeBlock::recordFunctionParsingInfo(ASTScopeContext* scopeCtx,
433391
if (parameterNames.size() > 0) {
434392
size_t parameterNamesCount = parameterNames.size();
435393
m_parameterNames.resizeWithUninitializedValues(parameterNamesCount);
394+
#ifndef ESCARGOT_DEBUGGER
436395
m_parameterUsed.resizeWithUninitializedValues(parameterNamesCount);
396+
#endif
437397
for (size_t i = 0; i < parameterNamesCount; i++) {
438398
m_parameterNames[i] = parameterNames[i];
439-
440-
ParameterUsed paramUsedInfo;
441-
paramUsedInfo.m_name = parameterNames[i];
442-
paramUsedInfo.m_isUsed = false;
443-
m_parameterUsed[i] = paramUsedInfo;
399+
#ifndef ESCARGOT_DEBUGGER
400+
m_parameterUsed[i] = scopeCtx->m_parameterUsed[i];
401+
#endif
444402
}
445-
446-
recordParameterUsage(scopeCtx);
447403
}
448404

449405
m_canUseIndexedVariableStorage = !m_hasEval && !m_isEvalCode && !m_hasWith;

src/parser/CodeBlock.h

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -405,13 +405,6 @@ class InterpretedCodeBlock : public CodeBlock {
405405
BlockIdentifierInfoVector m_identifiers;
406406
};
407407

408-
struct ParameterUsed {
409-
AtomicString m_name;
410-
bool m_isUsed;
411-
};
412-
413-
typedef TightVector<ParameterUsed, GCUtil::gc_malloc_atomic_allocator<ParameterUsed>> ParameterUsedVector;
414-
415408
struct IdentifierInfo {
416409
bool m_needToAllocateOnStack : 1;
417410
bool m_isMutable : 1;
@@ -946,15 +939,17 @@ class InterpretedCodeBlock : public CodeBlock {
946939
return false;
947940
}
948941

942+
#ifndef ESCARGOT_DEBUGGER
949943
bool checkParameterUsed(const AtomicString& name)
950944
{
951945
for (size_t i = 0; i < m_parameterUsed.size(); i++) {
952-
if (m_parameterUsed[i].m_name == name) {
953-
return m_parameterUsed[i].m_isUsed;
946+
if (m_parameterNames[i] == name) {
947+
return m_parameterUsed[i];
954948
}
955949
}
956950
return false;
957951
}
952+
#endif
958953

959954
void markHeapAllocatedEnvironmentFromHere(LexicalBlockIndex blockIndex = 0, InterpretedCodeBlock* to = nullptr);
960955

@@ -987,7 +982,9 @@ class InterpretedCodeBlock : public CodeBlock {
987982

988983
// all parameter names including targets of patterns and rest element
989984
AtomicStringTightVector m_parameterNames;
990-
ParameterUsedVector m_parameterUsed;
985+
#ifndef ESCARGOT_DEBUGGER
986+
TightVector<bool, GCUtil::gc_malloc_atomic_allocator<bool>> m_parameterUsed;
987+
#endif
991988
IdentifierInfoVector m_identifierInfos;
992989
BlockInfo** m_blockInfos;
993990
static constexpr size_t maxBlockInfosLength = ((1 << 24) - 1);
@@ -1070,10 +1067,6 @@ class InterpretedCodeBlock : public CodeBlock {
10701067
static InterpretedCodeBlock* createInterpretedCodeBlock(Context* ctx, Script* script, StringView src, ASTScopeContext* scopeCtx, InterpretedCodeBlock* parentBlock, bool isEvalCode, bool isEvalCodeInFunction);
10711068
static InterpretedCodeBlock* createInterpretedCodeBlock(Context* ctx, Script* script, bool needRareData = false);
10721069

1073-
bool innerIsParameterUsed(ASTBlockContext* block, AtomicString& name);
1074-
bool isParameterUsed(ASTScopeContext* scopeCtx, AtomicString& name);
1075-
void recordParameterUsage(ASTScopeContext* scopeCtx);
1076-
10771070
void recordGlobalParsingInfo(ASTScopeContext* scopeCtx, bool isEvalCode, bool isEvalCodeInFunction);
10781071
void recordFunctionParsingInfo(ASTScopeContext* scopeCtxm, bool isEvalCode, bool isEvalCodeInFunction);
10791072

src/parser/ast/ASTContext.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,11 @@ struct ASTScopeContext {
321321
AtomicStringTightVector *m_classPrivateNames; // this is needed for direct eval in class & nested class
322322
AtomicStringTightVector m_parameters;
323323
AtomicString m_functionName;
324+
#ifndef ESCARGOT_DEBUGGER
325+
TightVector<bool, GCUtil::gc_malloc_atomic_allocator<bool>> m_parameterUsed;
324326

327+
ASTScopeContext *m_parent;
328+
#endif
325329
ASTScopeContext *m_firstChild;
326330
ASTScopeContext *m_nextSibling;
327331
ASTBlockContextVector m_childBlockScopes;
@@ -703,6 +707,9 @@ struct ASTScopeContext {
703707
, m_lexicalBlockIndexFunctionLocatedIn(LEXICAL_BLOCK_INDEX_MAX)
704708
, m_varNamesMap(nullptr)
705709
, m_classPrivateNames(nullptr)
710+
#ifndef ESCARGOT_DEBUGGER
711+
, m_parent(nullptr)
712+
#endif
706713
, m_firstChild(nullptr)
707714
, m_nextSibling(nullptr)
708715
, m_functionStartLOC(1, 1, 0) // set default start location at the start of the code

src/parser/esprima_cpp/esprima.cpp

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,23 @@ class Parser {
337337
#endif /* ESCARGOT_DEBUGGER */
338338
}
339339

340+
#ifndef ESCARGOT_DEBUGGER
341+
void setParameterUsed(ASTScopeContext* scopeCtx, AtomicString name)
342+
{
343+
for (size_t i = 0; i < scopeCtx->m_parameterUsed.size(); i++) {
344+
if (scopeCtx->m_parameterUsed[i]) {
345+
continue;
346+
}
347+
348+
if (name == "eval" || name == "arguments") {
349+
scopeCtx->m_parameterUsed[i] = true;
350+
} else if (scopeCtx->m_parameters[i] == name) {
351+
scopeCtx->m_parameterUsed[i] = true;
352+
}
353+
}
354+
}
355+
#endif
356+
340357
ASTScopeContext* pushScopeContext(AtomicString functionName)
341358
{
342359
auto parentContext = this->currentScopeContext;
@@ -353,6 +370,9 @@ class Parser {
353370

354371
if (parentContext) {
355372
parentContext->appendChild(this->currentScopeContext);
373+
#ifndef ESCARGOT_DEBUGGER
374+
this->currentScopeContext->m_parent = parentContext;
375+
#endif
356376
}
357377

358378
return parentContext;
@@ -432,6 +452,15 @@ class Parser {
432452
ASSERT((VectorUtil::findInVector(this->currentBlockContext->m_usingNames, name) != VectorUtil::invalidIndex) == contains);
433453
if (!contains) {
434454
this->currentBlockContext->m_usingNames.push_back(name);
455+
#ifndef ESCARGOT_DEBUGGER
456+
ASTScopeContext* scope = this->currentScopeContext;
457+
while (scope) {
458+
if (scope->m_parameters.size()) {
459+
setParameterUsed(scope, name);
460+
}
461+
scope = scope->m_parent;
462+
}
463+
#endif
435464
}
436465
}
437466

@@ -483,11 +512,18 @@ class Parser {
483512
}
484513
#endif
485514
this->currentScopeContext->m_parameters.resizeWithUninitializedValues(paramNames.size());
515+
#ifndef ESCARGOT_DEBUGGER
516+
this->currentScopeContext->m_parameterUsed.resizeWithUninitializedValues(paramNames.size());
517+
#endif
486518
LexicalBlockIndex functionBodyBlockIndex = this->currentScopeContext->m_functionBodyBlockIndex;
487519
for (size_t i = 0; i < paramNames.size(); i++) {
488520
ASSERT(paramNames[i].length() > 0);
489521
AtomicString as(this->escargotContext, paramNames[i]);
490522
this->currentScopeContext->m_parameters[i] = as;
523+
#ifndef ESCARGOT_DEBUGGER
524+
// Set false basically, but true when parameter is not identifier.
525+
this->currentScopeContext->m_parameterUsed[i] = hasParameterOtherThanIdentifier;
526+
#endif
491527
this->currentScopeContext->insertVarName(as, functionBodyBlockIndex, true, true, true);
492528
}
493529

@@ -1710,6 +1746,10 @@ class Parser {
17101746
this->currentScopeContext->m_parameterCount = 1;
17111747
this->currentScopeContext->m_parameters.resizeWithUninitializedValues(1);
17121748
this->currentScopeContext->m_parameters[0] = className;
1749+
#ifndef ESCARGOT_DEBUGGER
1750+
this->currentScopeContext->m_parameterUsed.resizeWithUninitializedValues(1);
1751+
this->currentScopeContext->m_parameterUsed[0] = true;
1752+
#endif
17131753
this->currentScopeContext->insertVarName(className, 0, true, true, true);
17141754
}
17151755

@@ -3533,6 +3573,10 @@ class Parser {
35333573
this->currentScopeContext->m_parameterCount = 1;
35343574
this->currentScopeContext->m_parameters.resizeWithUninitializedValues(1);
35353575
this->currentScopeContext->m_parameters[0] = paramName;
3576+
#ifndef ESCARGOT_DEBUGGER
3577+
this->currentScopeContext->m_parameterUsed.resizeWithUninitializedValues(1);
3578+
this->currentScopeContext->m_parameterUsed[0] = true;
3579+
#endif
35363580
this->currentScopeContext->insertVarName(paramName, 0, true, true, true);
35373581
}
35383582

@@ -5033,11 +5077,15 @@ class Parser {
50335077

50345078
switch (param->type()) {
50355079
case Identifier: {
5080+
#ifndef ESCARGOT_DEBUGGER
50365081
if (this->codeBlock->checkParameterUsed(param->asIdentifier()->name())) {
5082+
#endif
50375083
Node* init = this->finalize(node, builder.createInitializeParameterExpressionNode(param, paramIndex));
50385084
Node* statement = this->finalize(node, builder.createExpressionStatementNode(init));
50395085
container->appendChild(statement);
5086+
#ifndef ESCARGOT_DEBUGGER
50405087
}
5088+
#endif
50415089
break;
50425090
}
50435091
case AssignmentPattern:

0 commit comments

Comments
 (0)