From 04d81120368173ee91fab193f8cd0837d9628dd4 Mon Sep 17 00:00:00 2001 From: neis Date: Fri, 26 Aug 2016 01:30:56 -0700 Subject: [PATCH] [modules] Minor refactorings in scopes and scopeinfos. R=adamk@chromium.org BUG=v8:1569 Review-Url: https://codereview.chromium.org/2275943005 Cr-Commit-Position: refs/heads/master@{#38931} --- src/ast/scopeinfo.cc | 55 ++++++++++++++++++--------------------- src/ast/scopes.cc | 44 ++++++++++++++++--------------- src/ast/scopes.h | 13 +++++---- src/crankshaft/typing.cc | 3 +-- src/debug/liveedit.cc | 3 +-- src/objects.h | 10 +++---- src/parsing/parser-base.h | 2 +- 7 files changed, 62 insertions(+), 68 deletions(-) diff --git a/src/ast/scopeinfo.cc b/src/ast/scopeinfo.cc index 336f96ead1..a9e824f415 100644 --- a/src/ast/scopeinfo.cc +++ b/src/ast/scopeinfo.cc @@ -14,13 +14,11 @@ namespace internal { Handle ScopeInfo::Create(Isolate* isolate, Zone* zone, Scope* scope) { - // Collect stack and context locals. + // Collect variables. ZoneList stack_locals(scope->StackLocalCount(), zone); ZoneList context_locals(scope->ContextLocalCount(), zone); ZoneList context_globals(scope->ContextGlobalCount(), zone); - - scope->CollectStackAndContextLocals(&stack_locals, &context_locals, - &context_globals); + scope->CollectVariables(&stack_locals, &context_locals, &context_globals); const int stack_local_count = stack_locals.length(); const int context_local_count = context_locals.length(); const int context_global_count = context_globals.length(); @@ -148,10 +146,9 @@ Handle ScopeInfo::Create(Isolate* isolate, Zone* zone, for (int i = 0; i < context_local_count; ++i) { Variable* var = context_locals[i]; int context_index = var->index() - Context::MIN_CONTEXT_SLOTS; - uint32_t info = - ContextLocalMode::encode(var->mode()) | - ContextLocalInitFlag::encode(var->initialization_flag()) | - ContextLocalMaybeAssignedFlag::encode(var->maybe_assigned()); + uint32_t info = VariableModeField::encode(var->mode()) | + InitFlagField::encode(var->initialization_flag()) | + MaybeAssignedFlagField::encode(var->maybe_assigned()); scope_info->set(index + context_index, *var->name()); scope_info->set(info_index + context_index, Smi::FromInt(info)); } @@ -166,10 +163,9 @@ Handle ScopeInfo::Create(Isolate* isolate, Zone* zone, Variable* var = context_globals[i]; scope_info->set(index + i, *var->name()); // TODO(ishell): do we need this kind of info for globals here? - uint32_t info = - ContextLocalMode::encode(var->mode()) | - ContextLocalInitFlag::encode(var->initialization_flag()) | - ContextLocalMaybeAssignedFlag::encode(var->maybe_assigned()); + uint32_t info = VariableModeField::encode(var->mode()) | + InitFlagField::encode(var->initialization_flag()) | + MaybeAssignedFlagField::encode(var->maybe_assigned()); scope_info->set(info_index + i, Smi::FromInt(info)); } @@ -252,9 +248,9 @@ Handle ScopeInfo::CreateGlobalThisBinding(Isolate* isolate) { DCHECK(index == scope_info->ContextLocalNameEntriesIndex()); scope_info->set(index++, *isolate->factory()->this_string()); DCHECK(index == scope_info->ContextLocalInfoEntriesIndex()); - const uint32_t value = ContextLocalMode::encode(CONST) | - ContextLocalInitFlag::encode(kCreatedInitialized) | - ContextLocalMaybeAssignedFlag::encode(kNotAssigned); + const uint32_t value = VariableModeField::encode(CONST) | + InitFlagField::encode(kCreatedInitialized) | + MaybeAssignedFlagField::encode(kNotAssigned); scope_info->set(index++, Smi::FromInt(value)); // And here we record that this scopeinfo binds a receiver. @@ -428,7 +424,7 @@ VariableMode ScopeInfo::ContextLocalMode(int var) { DCHECK(0 <= var && var < ContextLocalCount() + ContextGlobalCount()); int info_index = ContextLocalInfoEntriesIndex() + var; int value = Smi::cast(get(info_index))->value(); - return ContextLocalMode::decode(value); + return VariableModeField::decode(value); } @@ -436,7 +432,7 @@ InitializationFlag ScopeInfo::ContextLocalInitFlag(int var) { DCHECK(0 <= var && var < ContextLocalCount() + ContextGlobalCount()); int info_index = ContextLocalInfoEntriesIndex() + var; int value = Smi::cast(get(info_index))->value(); - return ContextLocalInitFlag::decode(value); + return InitFlagField::decode(value); } @@ -444,7 +440,7 @@ MaybeAssignedFlag ScopeInfo::ContextLocalMaybeAssignedFlag(int var) { DCHECK(0 <= var && var < ContextLocalCount() + ContextGlobalCount()); int info_index = ContextLocalInfoEntriesIndex() + var; int value = Smi::cast(get(info_index))->value(); - return ContextLocalMaybeAssignedFlag::decode(value); + return MaybeAssignedFlagField::decode(value); } bool ScopeInfo::VariableIsSynthetic(String* name) { @@ -462,7 +458,7 @@ int ScopeInfo::StackSlotIndex(String* name) { if (length() > 0) { int first_slot_index = Smi::cast(get(StackLocalFirstSlotIndex()))->value(); int start = StackLocalEntriesIndex(); - int end = StackLocalEntriesIndex() + StackLocalCount(); + int end = start + StackLocalCount(); for (int i = start; i < end; ++i) { if (name == get(i)) { return i - start + first_slot_index; @@ -478,8 +474,10 @@ int ScopeInfo::ContextSlotIndex(Handle scope_info, InitializationFlag* init_flag, MaybeAssignedFlag* maybe_assigned_flag) { DCHECK(name->IsInternalizedString()); - DCHECK(mode != NULL); - DCHECK(init_flag != NULL); + DCHECK_NOT_NULL(mode); + DCHECK_NOT_NULL(init_flag); + DCHECK_NOT_NULL(maybe_assigned_flag); + if (scope_info->length() > 0) { ContextSlotCache* context_slot_cache = scope_info->GetIsolate()->context_slot_cache(); @@ -491,8 +489,7 @@ int ScopeInfo::ContextSlotIndex(Handle scope_info, } int start = scope_info->ContextLocalNameEntriesIndex(); - int end = scope_info->ContextLocalNameEntriesIndex() + - scope_info->ContextLocalCount(); + int end = start + scope_info->ContextLocalCount(); for (int i = start; i < end; ++i) { if (*name == scope_info->get(i)) { int var = i - start; @@ -511,17 +508,18 @@ int ScopeInfo::ContextSlotIndex(Handle scope_info, context_slot_cache->Update(scope_info, name, TEMPORARY, kNeedsInitialization, kNotAssigned, -1); } + return -1; } - int ScopeInfo::ContextGlobalSlotIndex(Handle scope_info, Handle name, VariableMode* mode, InitializationFlag* init_flag, MaybeAssignedFlag* maybe_assigned_flag) { DCHECK(name->IsInternalizedString()); - DCHECK(mode != NULL); - DCHECK(init_flag != NULL); + DCHECK_NOT_NULL(mode); + DCHECK_NOT_NULL(init_flag); + DCHECK_NOT_NULL(maybe_assigned_flag); if (scope_info->length() > 0) { // This is to ensure that ContextLocalMode() and co. queries would work. DCHECK_EQ(scope_info->ContextGlobalNameEntriesIndex(), @@ -529,8 +527,7 @@ int ScopeInfo::ContextGlobalSlotIndex(Handle scope_info, scope_info->ContextLocalCount()); int base = scope_info->ContextLocalNameEntriesIndex(); int start = scope_info->ContextGlobalNameEntriesIndex(); - int end = scope_info->ContextGlobalNameEntriesIndex() + - scope_info->ContextGlobalCount(); + int end = start + scope_info->ContextGlobalCount(); for (int i = start; i < end; ++i) { if (*name == scope_info->get(i)) { int var = i - base; @@ -564,7 +561,7 @@ int ScopeInfo::ParameterIndex(String* name) { // inside a function (and thus we need to look // at the last index). Was bug# 1110337. int start = ParameterEntriesIndex(); - int end = ParameterEntriesIndex() + ParameterCount(); + int end = start + ParameterCount(); for (int i = end - 1; i >= start; --i) { if (name == get(i)) { return i - start; diff --git a/src/ast/scopes.cc b/src/ast/scopes.cc index ead1b4ead3..a0b2323416 100644 --- a/src/ast/scopes.cc +++ b/src/ast/scopes.cc @@ -148,9 +148,10 @@ DeclarationScope::DeclarationScope(Zone* zone, Scope* outer_scope, asm_function_ = outer_scope_->IsAsmModule(); } -ModuleScope::ModuleScope(Zone* zone, DeclarationScope* script_scope, +ModuleScope::ModuleScope(DeclarationScope* script_scope, AstValueFactory* ast_value_factory) - : DeclarationScope(zone, script_scope, MODULE_SCOPE) { + : DeclarationScope(ast_value_factory->zone(), script_scope, MODULE_SCOPE) { + Zone* zone = ast_value_factory->zone(); module_descriptor_ = new (zone) ModuleDescriptor(zone); set_language_mode(STRICT); DeclareThis(ast_value_factory); @@ -617,35 +618,36 @@ Variable* Scope::LookupInScopeInfo(const AstRawString* name) { // There should be no local slot with the given name. DCHECK(scope_info_->StackSlotIndex(*name_handle) < 0); - // Check context slot lookup. VariableMode mode; - VariableLocation location = VariableLocation::CONTEXT; InitializationFlag init_flag; MaybeAssignedFlag maybe_assigned_flag; + + VariableLocation location = VariableLocation::CONTEXT; int index = ScopeInfo::ContextSlotIndex(scope_info_, name_handle, &mode, &init_flag, &maybe_assigned_flag); if (index < 0) { location = VariableLocation::GLOBAL; index = ScopeInfo::ContextGlobalSlotIndex(scope_info_, name_handle, &mode, &init_flag, &maybe_assigned_flag); + DCHECK(index < 0 || (is_script_scope() && mode == VAR)); } if (index < 0) { - // Check parameters. - index = scope_info_->ParameterIndex(*name_handle); - if (index < 0) return NULL; - - mode = DYNAMIC; location = VariableLocation::LOOKUP; - init_flag = kCreatedInitialized; - // Be conservative and flag parameters as maybe assigned. Better information - // would require ScopeInfo to serialize the maybe_assigned bit also for - // parameters. - maybe_assigned_flag = kMaybeAssigned; - } else { - DCHECK(location != VariableLocation::GLOBAL || - (is_script_scope() && IsDeclaredVariableMode(mode) && - !IsLexicalVariableMode(mode))); + index = scope_info_->ParameterIndex(*name_handle); + if (index >= 0) { + mode = DYNAMIC; + init_flag = kCreatedInitialized; + // Be conservative and flag parameters as maybe assigned. Better + // information would require ScopeInfo to serialize the maybe_assigned bit + // also for parameters. + maybe_assigned_flag = kMaybeAssigned; + } } + if (index < 0 && scope_type() == MODULE_SCOPE) { + location = VariableLocation::MODULE; + index = -1; // TODO(neis): Find module variables in scope info. + } + if (index < 0) return nullptr; // Nowhere found. Variable::Kind kind = Variable::NORMAL; if (location == VariableLocation::CONTEXT && @@ -822,9 +824,9 @@ Declaration* Scope::CheckLexDeclarationsConflictingWith( return nullptr; } -void Scope::CollectStackAndContextLocals(ZoneList* stack_locals, - ZoneList* context_locals, - ZoneList* context_globals) { +void Scope::CollectVariables(ZoneList* stack_locals, + ZoneList* context_locals, + ZoneList* context_globals) { // TODO(verwaest): Just pass out locals_ directly and walk it? DCHECK_NOT_NULL(stack_locals); DCHECK_NOT_NULL(context_locals); diff --git a/src/ast/scopes.h b/src/ast/scopes.h index b330ed14cc..8a84664c18 100644 --- a/src/ast/scopes.h +++ b/src/ast/scopes.h @@ -340,12 +340,11 @@ class Scope: public ZoneObject { // --------------------------------------------------------------------------- // Variable allocation. - // Collect stack and context allocated local variables in this scope. Note - // that the function variable - if present - is not collected and should be - // handled separately. - void CollectStackAndContextLocals(ZoneList* stack_locals, - ZoneList* context_locals, - ZoneList* context_globals); + // Collect variables in this scope. Note that the function variable - if + // present - is not collected and should be handled separately. + void CollectVariables(ZoneList* stack_locals, + ZoneList* context_locals, + ZoneList* context_globals); // Result of variable allocation. int num_stack_slots() const { return num_stack_slots_; } @@ -836,7 +835,7 @@ class DeclarationScope : public Scope { class ModuleScope final : public DeclarationScope { public: - ModuleScope(Zone* zone, DeclarationScope* script_scope, + ModuleScope(DeclarationScope* script_scope, AstValueFactory* ast_value_factory); ModuleDescriptor* module() const { diff --git a/src/crankshaft/typing.cc b/src/crankshaft/typing.cc index 5961838711..d4854acd42 100644 --- a/src/crankshaft/typing.cc +++ b/src/crankshaft/typing.cc @@ -87,8 +87,7 @@ void AstTyper::ObserveTypesAtOsrEntry(IterationStatement* stmt) { ZoneList local_vars(locals, zone()); ZoneList context_vars(scope_->ContextLocalCount(), zone()); ZoneList global_vars(scope_->ContextGlobalCount(), zone()); - scope_->CollectStackAndContextLocals(&local_vars, &context_vars, - &global_vars); + scope_->CollectVariables(&local_vars, &context_vars, &global_vars); for (int i = 0; i < locals; i++) { PrintObserved(local_vars.at(i), frame->GetExpression(i), diff --git a/src/debug/liveedit.cc b/src/debug/liveedit.cc index f5cc58348a..6e158f3fd8 100644 --- a/src/debug/liveedit.cc +++ b/src/debug/liveedit.cc @@ -1904,8 +1904,7 @@ Handle LiveEditFunctionTracker::SerializeFunctionScope(Scope* scope) { ZoneList context_list(current_scope->ContextLocalCount(), zone_); ZoneList globals_list(current_scope->ContextGlobalCount(), zone_); - current_scope->CollectStackAndContextLocals(&stack_list, &context_list, - &globals_list); + current_scope->CollectVariables(&stack_list, &context_list, &globals_list); for (int i = 0; i < context_list.length(); i++) { int context_index = context_list[i]->index() - Context::MIN_CONTEXT_SLOTS; int location = scope_info_length + context_index * 2; diff --git a/src/objects.h b/src/objects.h index 10d6deeeb0..b4fec8ee7e 100644 --- a/src/objects.h +++ b/src/objects.h @@ -4507,12 +4507,10 @@ class ScopeInfo : public FixedArray { class FunctionKindField : public BitField {}; - // BitFields representing the encoded information for context locals in the - // ContextLocalInfoEntries part. - class ContextLocalMode: public BitField {}; - class ContextLocalInitFlag: public BitField {}; - class ContextLocalMaybeAssignedFlag - : public BitField {}; + // Properties of variables. + class VariableModeField : public BitField {}; + class InitFlagField : public BitField {}; + class MaybeAssignedFlagField : public BitField {}; friend class ScopeIterator; }; diff --git a/src/parsing/parser-base.h b/src/parsing/parser-base.h index 205c0eec49..90104e34a1 100644 --- a/src/parsing/parser-base.h +++ b/src/parsing/parser-base.h @@ -641,7 +641,7 @@ class ParserBase { } ModuleScope* NewModuleScope(DeclarationScope* parent) const { - return new (zone()) ModuleScope(zone(), parent, ast_value_factory()); + return new (zone()) ModuleScope(parent, ast_value_factory()); } DeclarationScope* NewEvalScope(Scope* parent) const {