Commit 04d81120 authored by neis's avatar neis Committed by Commit bot

[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}
parent a4a4e7fa
...@@ -14,13 +14,11 @@ namespace internal { ...@@ -14,13 +14,11 @@ namespace internal {
Handle<ScopeInfo> ScopeInfo::Create(Isolate* isolate, Zone* zone, Handle<ScopeInfo> ScopeInfo::Create(Isolate* isolate, Zone* zone,
Scope* scope) { Scope* scope) {
// Collect stack and context locals. // Collect variables.
ZoneList<Variable*> stack_locals(scope->StackLocalCount(), zone); ZoneList<Variable*> stack_locals(scope->StackLocalCount(), zone);
ZoneList<Variable*> context_locals(scope->ContextLocalCount(), zone); ZoneList<Variable*> context_locals(scope->ContextLocalCount(), zone);
ZoneList<Variable*> context_globals(scope->ContextGlobalCount(), zone); ZoneList<Variable*> context_globals(scope->ContextGlobalCount(), zone);
scope->CollectVariables(&stack_locals, &context_locals, &context_globals);
scope->CollectStackAndContextLocals(&stack_locals, &context_locals,
&context_globals);
const int stack_local_count = stack_locals.length(); const int stack_local_count = stack_locals.length();
const int context_local_count = context_locals.length(); const int context_local_count = context_locals.length();
const int context_global_count = context_globals.length(); const int context_global_count = context_globals.length();
...@@ -148,10 +146,9 @@ Handle<ScopeInfo> ScopeInfo::Create(Isolate* isolate, Zone* zone, ...@@ -148,10 +146,9 @@ Handle<ScopeInfo> ScopeInfo::Create(Isolate* isolate, Zone* zone,
for (int i = 0; i < context_local_count; ++i) { for (int i = 0; i < context_local_count; ++i) {
Variable* var = context_locals[i]; Variable* var = context_locals[i];
int context_index = var->index() - Context::MIN_CONTEXT_SLOTS; int context_index = var->index() - Context::MIN_CONTEXT_SLOTS;
uint32_t info = uint32_t info = VariableModeField::encode(var->mode()) |
ContextLocalMode::encode(var->mode()) | InitFlagField::encode(var->initialization_flag()) |
ContextLocalInitFlag::encode(var->initialization_flag()) | MaybeAssignedFlagField::encode(var->maybe_assigned());
ContextLocalMaybeAssignedFlag::encode(var->maybe_assigned());
scope_info->set(index + context_index, *var->name()); scope_info->set(index + context_index, *var->name());
scope_info->set(info_index + context_index, Smi::FromInt(info)); scope_info->set(info_index + context_index, Smi::FromInt(info));
} }
...@@ -166,10 +163,9 @@ Handle<ScopeInfo> ScopeInfo::Create(Isolate* isolate, Zone* zone, ...@@ -166,10 +163,9 @@ Handle<ScopeInfo> ScopeInfo::Create(Isolate* isolate, Zone* zone,
Variable* var = context_globals[i]; Variable* var = context_globals[i];
scope_info->set(index + i, *var->name()); scope_info->set(index + i, *var->name());
// TODO(ishell): do we need this kind of info for globals here? // TODO(ishell): do we need this kind of info for globals here?
uint32_t info = uint32_t info = VariableModeField::encode(var->mode()) |
ContextLocalMode::encode(var->mode()) | InitFlagField::encode(var->initialization_flag()) |
ContextLocalInitFlag::encode(var->initialization_flag()) | MaybeAssignedFlagField::encode(var->maybe_assigned());
ContextLocalMaybeAssignedFlag::encode(var->maybe_assigned());
scope_info->set(info_index + i, Smi::FromInt(info)); scope_info->set(info_index + i, Smi::FromInt(info));
} }
...@@ -252,9 +248,9 @@ Handle<ScopeInfo> ScopeInfo::CreateGlobalThisBinding(Isolate* isolate) { ...@@ -252,9 +248,9 @@ Handle<ScopeInfo> ScopeInfo::CreateGlobalThisBinding(Isolate* isolate) {
DCHECK(index == scope_info->ContextLocalNameEntriesIndex()); DCHECK(index == scope_info->ContextLocalNameEntriesIndex());
scope_info->set(index++, *isolate->factory()->this_string()); scope_info->set(index++, *isolate->factory()->this_string());
DCHECK(index == scope_info->ContextLocalInfoEntriesIndex()); DCHECK(index == scope_info->ContextLocalInfoEntriesIndex());
const uint32_t value = ContextLocalMode::encode(CONST) | const uint32_t value = VariableModeField::encode(CONST) |
ContextLocalInitFlag::encode(kCreatedInitialized) | InitFlagField::encode(kCreatedInitialized) |
ContextLocalMaybeAssignedFlag::encode(kNotAssigned); MaybeAssignedFlagField::encode(kNotAssigned);
scope_info->set(index++, Smi::FromInt(value)); scope_info->set(index++, Smi::FromInt(value));
// And here we record that this scopeinfo binds a receiver. // And here we record that this scopeinfo binds a receiver.
...@@ -428,7 +424,7 @@ VariableMode ScopeInfo::ContextLocalMode(int var) { ...@@ -428,7 +424,7 @@ VariableMode ScopeInfo::ContextLocalMode(int var) {
DCHECK(0 <= var && var < ContextLocalCount() + ContextGlobalCount()); DCHECK(0 <= var && var < ContextLocalCount() + ContextGlobalCount());
int info_index = ContextLocalInfoEntriesIndex() + var; int info_index = ContextLocalInfoEntriesIndex() + var;
int value = Smi::cast(get(info_index))->value(); 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) { ...@@ -436,7 +432,7 @@ InitializationFlag ScopeInfo::ContextLocalInitFlag(int var) {
DCHECK(0 <= var && var < ContextLocalCount() + ContextGlobalCount()); DCHECK(0 <= var && var < ContextLocalCount() + ContextGlobalCount());
int info_index = ContextLocalInfoEntriesIndex() + var; int info_index = ContextLocalInfoEntriesIndex() + var;
int value = Smi::cast(get(info_index))->value(); 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) { ...@@ -444,7 +440,7 @@ MaybeAssignedFlag ScopeInfo::ContextLocalMaybeAssignedFlag(int var) {
DCHECK(0 <= var && var < ContextLocalCount() + ContextGlobalCount()); DCHECK(0 <= var && var < ContextLocalCount() + ContextGlobalCount());
int info_index = ContextLocalInfoEntriesIndex() + var; int info_index = ContextLocalInfoEntriesIndex() + var;
int value = Smi::cast(get(info_index))->value(); int value = Smi::cast(get(info_index))->value();
return ContextLocalMaybeAssignedFlag::decode(value); return MaybeAssignedFlagField::decode(value);
} }
bool ScopeInfo::VariableIsSynthetic(String* name) { bool ScopeInfo::VariableIsSynthetic(String* name) {
...@@ -462,7 +458,7 @@ int ScopeInfo::StackSlotIndex(String* name) { ...@@ -462,7 +458,7 @@ int ScopeInfo::StackSlotIndex(String* name) {
if (length() > 0) { if (length() > 0) {
int first_slot_index = Smi::cast(get(StackLocalFirstSlotIndex()))->value(); int first_slot_index = Smi::cast(get(StackLocalFirstSlotIndex()))->value();
int start = StackLocalEntriesIndex(); int start = StackLocalEntriesIndex();
int end = StackLocalEntriesIndex() + StackLocalCount(); int end = start + StackLocalCount();
for (int i = start; i < end; ++i) { for (int i = start; i < end; ++i) {
if (name == get(i)) { if (name == get(i)) {
return i - start + first_slot_index; return i - start + first_slot_index;
...@@ -478,8 +474,10 @@ int ScopeInfo::ContextSlotIndex(Handle<ScopeInfo> scope_info, ...@@ -478,8 +474,10 @@ int ScopeInfo::ContextSlotIndex(Handle<ScopeInfo> scope_info,
InitializationFlag* init_flag, InitializationFlag* init_flag,
MaybeAssignedFlag* maybe_assigned_flag) { MaybeAssignedFlag* maybe_assigned_flag) {
DCHECK(name->IsInternalizedString()); DCHECK(name->IsInternalizedString());
DCHECK(mode != NULL); DCHECK_NOT_NULL(mode);
DCHECK(init_flag != NULL); DCHECK_NOT_NULL(init_flag);
DCHECK_NOT_NULL(maybe_assigned_flag);
if (scope_info->length() > 0) { if (scope_info->length() > 0) {
ContextSlotCache* context_slot_cache = ContextSlotCache* context_slot_cache =
scope_info->GetIsolate()->context_slot_cache(); scope_info->GetIsolate()->context_slot_cache();
...@@ -491,8 +489,7 @@ int ScopeInfo::ContextSlotIndex(Handle<ScopeInfo> scope_info, ...@@ -491,8 +489,7 @@ int ScopeInfo::ContextSlotIndex(Handle<ScopeInfo> scope_info,
} }
int start = scope_info->ContextLocalNameEntriesIndex(); int start = scope_info->ContextLocalNameEntriesIndex();
int end = scope_info->ContextLocalNameEntriesIndex() + int end = start + scope_info->ContextLocalCount();
scope_info->ContextLocalCount();
for (int i = start; i < end; ++i) { for (int i = start; i < end; ++i) {
if (*name == scope_info->get(i)) { if (*name == scope_info->get(i)) {
int var = i - start; int var = i - start;
...@@ -511,17 +508,18 @@ int ScopeInfo::ContextSlotIndex(Handle<ScopeInfo> scope_info, ...@@ -511,17 +508,18 @@ int ScopeInfo::ContextSlotIndex(Handle<ScopeInfo> scope_info,
context_slot_cache->Update(scope_info, name, TEMPORARY, context_slot_cache->Update(scope_info, name, TEMPORARY,
kNeedsInitialization, kNotAssigned, -1); kNeedsInitialization, kNotAssigned, -1);
} }
return -1; return -1;
} }
int ScopeInfo::ContextGlobalSlotIndex(Handle<ScopeInfo> scope_info, int ScopeInfo::ContextGlobalSlotIndex(Handle<ScopeInfo> scope_info,
Handle<String> name, VariableMode* mode, Handle<String> name, VariableMode* mode,
InitializationFlag* init_flag, InitializationFlag* init_flag,
MaybeAssignedFlag* maybe_assigned_flag) { MaybeAssignedFlag* maybe_assigned_flag) {
DCHECK(name->IsInternalizedString()); DCHECK(name->IsInternalizedString());
DCHECK(mode != NULL); DCHECK_NOT_NULL(mode);
DCHECK(init_flag != NULL); DCHECK_NOT_NULL(init_flag);
DCHECK_NOT_NULL(maybe_assigned_flag);
if (scope_info->length() > 0) { if (scope_info->length() > 0) {
// This is to ensure that ContextLocalMode() and co. queries would work. // This is to ensure that ContextLocalMode() and co. queries would work.
DCHECK_EQ(scope_info->ContextGlobalNameEntriesIndex(), DCHECK_EQ(scope_info->ContextGlobalNameEntriesIndex(),
...@@ -529,8 +527,7 @@ int ScopeInfo::ContextGlobalSlotIndex(Handle<ScopeInfo> scope_info, ...@@ -529,8 +527,7 @@ int ScopeInfo::ContextGlobalSlotIndex(Handle<ScopeInfo> scope_info,
scope_info->ContextLocalCount()); scope_info->ContextLocalCount());
int base = scope_info->ContextLocalNameEntriesIndex(); int base = scope_info->ContextLocalNameEntriesIndex();
int start = scope_info->ContextGlobalNameEntriesIndex(); int start = scope_info->ContextGlobalNameEntriesIndex();
int end = scope_info->ContextGlobalNameEntriesIndex() + int end = start + scope_info->ContextGlobalCount();
scope_info->ContextGlobalCount();
for (int i = start; i < end; ++i) { for (int i = start; i < end; ++i) {
if (*name == scope_info->get(i)) { if (*name == scope_info->get(i)) {
int var = i - base; int var = i - base;
...@@ -564,7 +561,7 @@ int ScopeInfo::ParameterIndex(String* name) { ...@@ -564,7 +561,7 @@ int ScopeInfo::ParameterIndex(String* name) {
// inside a function (and thus we need to look // inside a function (and thus we need to look
// at the last index). Was bug# 1110337. // at the last index). Was bug# 1110337.
int start = ParameterEntriesIndex(); int start = ParameterEntriesIndex();
int end = ParameterEntriesIndex() + ParameterCount(); int end = start + ParameterCount();
for (int i = end - 1; i >= start; --i) { for (int i = end - 1; i >= start; --i) {
if (name == get(i)) { if (name == get(i)) {
return i - start; return i - start;
......
...@@ -148,9 +148,10 @@ DeclarationScope::DeclarationScope(Zone* zone, Scope* outer_scope, ...@@ -148,9 +148,10 @@ DeclarationScope::DeclarationScope(Zone* zone, Scope* outer_scope,
asm_function_ = outer_scope_->IsAsmModule(); asm_function_ = outer_scope_->IsAsmModule();
} }
ModuleScope::ModuleScope(Zone* zone, DeclarationScope* script_scope, ModuleScope::ModuleScope(DeclarationScope* script_scope,
AstValueFactory* ast_value_factory) 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); module_descriptor_ = new (zone) ModuleDescriptor(zone);
set_language_mode(STRICT); set_language_mode(STRICT);
DeclareThis(ast_value_factory); DeclareThis(ast_value_factory);
...@@ -617,35 +618,36 @@ Variable* Scope::LookupInScopeInfo(const AstRawString* name) { ...@@ -617,35 +618,36 @@ Variable* Scope::LookupInScopeInfo(const AstRawString* name) {
// There should be no local slot with the given name. // There should be no local slot with the given name.
DCHECK(scope_info_->StackSlotIndex(*name_handle) < 0); DCHECK(scope_info_->StackSlotIndex(*name_handle) < 0);
// Check context slot lookup.
VariableMode mode; VariableMode mode;
VariableLocation location = VariableLocation::CONTEXT;
InitializationFlag init_flag; InitializationFlag init_flag;
MaybeAssignedFlag maybe_assigned_flag; MaybeAssignedFlag maybe_assigned_flag;
VariableLocation location = VariableLocation::CONTEXT;
int index = ScopeInfo::ContextSlotIndex(scope_info_, name_handle, &mode, int index = ScopeInfo::ContextSlotIndex(scope_info_, name_handle, &mode,
&init_flag, &maybe_assigned_flag); &init_flag, &maybe_assigned_flag);
if (index < 0) { if (index < 0) {
location = VariableLocation::GLOBAL; location = VariableLocation::GLOBAL;
index = ScopeInfo::ContextGlobalSlotIndex(scope_info_, name_handle, &mode, index = ScopeInfo::ContextGlobalSlotIndex(scope_info_, name_handle, &mode,
&init_flag, &maybe_assigned_flag); &init_flag, &maybe_assigned_flag);
DCHECK(index < 0 || (is_script_scope() && mode == VAR));
} }
if (index < 0) { if (index < 0) {
// Check parameters.
index = scope_info_->ParameterIndex(*name_handle);
if (index < 0) return NULL;
mode = DYNAMIC;
location = VariableLocation::LOOKUP; location = VariableLocation::LOOKUP;
init_flag = kCreatedInitialized; index = scope_info_->ParameterIndex(*name_handle);
// Be conservative and flag parameters as maybe assigned. Better information if (index >= 0) {
// would require ScopeInfo to serialize the maybe_assigned bit also for mode = DYNAMIC;
// parameters. init_flag = kCreatedInitialized;
maybe_assigned_flag = kMaybeAssigned; // Be conservative and flag parameters as maybe assigned. Better
} else { // information would require ScopeInfo to serialize the maybe_assigned bit
DCHECK(location != VariableLocation::GLOBAL || // also for parameters.
(is_script_scope() && IsDeclaredVariableMode(mode) && maybe_assigned_flag = kMaybeAssigned;
!IsLexicalVariableMode(mode))); }
}
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; Variable::Kind kind = Variable::NORMAL;
if (location == VariableLocation::CONTEXT && if (location == VariableLocation::CONTEXT &&
...@@ -822,9 +824,9 @@ Declaration* Scope::CheckLexDeclarationsConflictingWith( ...@@ -822,9 +824,9 @@ Declaration* Scope::CheckLexDeclarationsConflictingWith(
return nullptr; return nullptr;
} }
void Scope::CollectStackAndContextLocals(ZoneList<Variable*>* stack_locals, void Scope::CollectVariables(ZoneList<Variable*>* stack_locals,
ZoneList<Variable*>* context_locals, ZoneList<Variable*>* context_locals,
ZoneList<Variable*>* context_globals) { ZoneList<Variable*>* context_globals) {
// TODO(verwaest): Just pass out locals_ directly and walk it? // TODO(verwaest): Just pass out locals_ directly and walk it?
DCHECK_NOT_NULL(stack_locals); DCHECK_NOT_NULL(stack_locals);
DCHECK_NOT_NULL(context_locals); DCHECK_NOT_NULL(context_locals);
......
...@@ -340,12 +340,11 @@ class Scope: public ZoneObject { ...@@ -340,12 +340,11 @@ class Scope: public ZoneObject {
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
// Variable allocation. // Variable allocation.
// Collect stack and context allocated local variables in this scope. Note // Collect variables in this scope. Note that the function variable - if
// that the function variable - if present - is not collected and should be // present - is not collected and should be handled separately.
// handled separately. void CollectVariables(ZoneList<Variable*>* stack_locals,
void CollectStackAndContextLocals(ZoneList<Variable*>* stack_locals, ZoneList<Variable*>* context_locals,
ZoneList<Variable*>* context_locals, ZoneList<Variable*>* context_globals);
ZoneList<Variable*>* context_globals);
// Result of variable allocation. // Result of variable allocation.
int num_stack_slots() const { return num_stack_slots_; } int num_stack_slots() const { return num_stack_slots_; }
...@@ -836,7 +835,7 @@ class DeclarationScope : public Scope { ...@@ -836,7 +835,7 @@ class DeclarationScope : public Scope {
class ModuleScope final : public DeclarationScope { class ModuleScope final : public DeclarationScope {
public: public:
ModuleScope(Zone* zone, DeclarationScope* script_scope, ModuleScope(DeclarationScope* script_scope,
AstValueFactory* ast_value_factory); AstValueFactory* ast_value_factory);
ModuleDescriptor* module() const { ModuleDescriptor* module() const {
......
...@@ -87,8 +87,7 @@ void AstTyper::ObserveTypesAtOsrEntry(IterationStatement* stmt) { ...@@ -87,8 +87,7 @@ void AstTyper::ObserveTypesAtOsrEntry(IterationStatement* stmt) {
ZoneList<Variable*> local_vars(locals, zone()); ZoneList<Variable*> local_vars(locals, zone());
ZoneList<Variable*> context_vars(scope_->ContextLocalCount(), zone()); ZoneList<Variable*> context_vars(scope_->ContextLocalCount(), zone());
ZoneList<Variable*> global_vars(scope_->ContextGlobalCount(), zone()); ZoneList<Variable*> global_vars(scope_->ContextGlobalCount(), zone());
scope_->CollectStackAndContextLocals(&local_vars, &context_vars, scope_->CollectVariables(&local_vars, &context_vars, &global_vars);
&global_vars);
for (int i = 0; i < locals; i++) { for (int i = 0; i < locals; i++) {
PrintObserved(local_vars.at(i), PrintObserved(local_vars.at(i),
frame->GetExpression(i), frame->GetExpression(i),
......
...@@ -1904,8 +1904,7 @@ Handle<Object> LiveEditFunctionTracker::SerializeFunctionScope(Scope* scope) { ...@@ -1904,8 +1904,7 @@ Handle<Object> LiveEditFunctionTracker::SerializeFunctionScope(Scope* scope) {
ZoneList<Variable*> context_list(current_scope->ContextLocalCount(), zone_); ZoneList<Variable*> context_list(current_scope->ContextLocalCount(), zone_);
ZoneList<Variable*> globals_list(current_scope->ContextGlobalCount(), ZoneList<Variable*> globals_list(current_scope->ContextGlobalCount(),
zone_); zone_);
current_scope->CollectStackAndContextLocals(&stack_list, &context_list, current_scope->CollectVariables(&stack_list, &context_list, &globals_list);
&globals_list);
for (int i = 0; i < context_list.length(); i++) { for (int i = 0; i < context_list.length(); i++) {
int context_index = context_list[i]->index() - Context::MIN_CONTEXT_SLOTS; int context_index = context_list[i]->index() - Context::MIN_CONTEXT_SLOTS;
int location = scope_info_length + context_index * 2; int location = scope_info_length + context_index * 2;
......
...@@ -4507,12 +4507,10 @@ class ScopeInfo : public FixedArray { ...@@ -4507,12 +4507,10 @@ class ScopeInfo : public FixedArray {
class FunctionKindField class FunctionKindField
: public BitField<FunctionKind, HasSimpleParametersField::kNext, 9> {}; : public BitField<FunctionKind, HasSimpleParametersField::kNext, 9> {};
// BitFields representing the encoded information for context locals in the // Properties of variables.
// ContextLocalInfoEntries part. class VariableModeField : public BitField<VariableMode, 0, 3> {};
class ContextLocalMode: public BitField<VariableMode, 0, 3> {}; class InitFlagField : public BitField<InitializationFlag, 3, 1> {};
class ContextLocalInitFlag: public BitField<InitializationFlag, 3, 1> {}; class MaybeAssignedFlagField : public BitField<MaybeAssignedFlag, 4, 1> {};
class ContextLocalMaybeAssignedFlag
: public BitField<MaybeAssignedFlag, 4, 1> {};
friend class ScopeIterator; friend class ScopeIterator;
}; };
......
...@@ -641,7 +641,7 @@ class ParserBase { ...@@ -641,7 +641,7 @@ class ParserBase {
} }
ModuleScope* NewModuleScope(DeclarationScope* parent) const { 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 { DeclarationScope* NewEvalScope(Scope* parent) const {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment