From: kmillikin@chromium.org Date: Tue, 7 Jul 2009 09:48:54 +0000 (+0000) Subject: Allow variable proxies for the same global variable to share the same X-Git-Tag: upstream/4.7.83~23722 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=8e6bf58174d4a9f497846eab1d8f7a975f161aec;p=platform%2Fupstream%2Fv8.git Allow variable proxies for the same global variable to share the same variable object. Add a map from names to variables for global scopes just like non-global scopes. Variables are added to the map by the parser when it encounters a declaration in a global scope or else at scope resolution time by a failed variable lookup from the global scope or an inner one and with no intervening with statements or possible calls to eval. Review URL: http://codereview.chromium.org/149245 git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@2369 ce2b1a6d-e550-0410-aec6-3dcde31c8c00 --- diff --git a/src/parser.cc b/src/parser.cc index e1d9b712c..e6550d959 100644 --- a/src/parser.cc +++ b/src/parser.cc @@ -1576,10 +1576,10 @@ VariableProxy* AstBuildingParser::Declare(Handle name, // to the calling function context. if (top_scope_->is_function_scope()) { // Declare the variable in the function scope. - var = top_scope_->LookupLocal(name); + var = top_scope_->LocalLookup(name); if (var == NULL) { // Declare the name. - var = top_scope_->Declare(name, mode); + var = top_scope_->DeclareLocal(name, mode); } else { // The name was declared before; check for conflicting // re-declarations. If the previous declaration was a const or the @@ -3466,8 +3466,8 @@ FunctionLiteral* Parser::ParseFunctionLiteral(Handle var_name, while (!done) { Handle param_name = ParseIdentifier(CHECK_OK); if (!is_pre_parsing_) { - top_scope_->AddParameter(top_scope_->Declare(param_name, - Variable::VAR)); + top_scope_->AddParameter(top_scope_->DeclareLocal(param_name, + Variable::VAR)); num_parameters++; } done = (peek() == Token::RPAREN); diff --git a/src/scopes.cc b/src/scopes.cc index 88b1c66f3..78ed03518 100644 --- a/src/scopes.cc +++ b/src/scopes.cc @@ -71,28 +71,28 @@ static bool Match(void* key1, void* key2) { // Dummy constructor -LocalsMap::LocalsMap(bool gotta_love_static_overloading) : HashMap() {} +VariableMap::VariableMap(bool gotta_love_static_overloading) : HashMap() {} -LocalsMap::LocalsMap() : HashMap(Match, &LocalsMapAllocator, 8) {} -LocalsMap::~LocalsMap() {} +VariableMap::VariableMap() : HashMap(Match, &LocalsMapAllocator, 8) {} +VariableMap::~VariableMap() {} -Variable* LocalsMap::Declare(Scope* scope, - Handle name, - Variable::Mode mode, - bool is_valid_LHS, - Variable::Kind kind) { +Variable* VariableMap::Declare(Scope* scope, + Handle name, + Variable::Mode mode, + bool is_valid_lhs, + Variable::Kind kind) { HashMap::Entry* p = HashMap::Lookup(name.location(), name->Hash(), true); if (p->value == NULL) { // The variable has not been declared yet -> insert it. ASSERT(p->key == name.location()); - p->value = new Variable(scope, name, mode, is_valid_LHS, kind); + p->value = new Variable(scope, name, mode, is_valid_lhs, kind); } return reinterpret_cast(p->value); } -Variable* LocalsMap::Lookup(Handle name) { +Variable* VariableMap::Lookup(Handle name) { HashMap::Entry* p = HashMap::Lookup(name.location(), name->Hash(), false); if (p != NULL) { ASSERT(*reinterpret_cast(p->key) == *name); @@ -110,7 +110,7 @@ Variable* LocalsMap::Lookup(Handle name) { // Dummy constructor Scope::Scope() : inner_scopes_(0), - locals_(false), + variables_(false), temps_(0), params_(0), dynamics_(NULL), @@ -168,27 +168,26 @@ void Scope::Initialize(bool inside_with) { // instead load them directly from the stack. Currently, the only // such parameter is 'this' which is passed on the stack when // invoking scripts - { Variable* var = - locals_.Declare(this, Factory::this_symbol(), Variable::VAR, - false, Variable::THIS); - var->rewrite_ = new Slot(var, Slot::PARAMETER, -1); - receiver_ = new VariableProxy(Factory::this_symbol(), true, false); - receiver_->BindTo(var); - } + Variable* var = + variables_.Declare(this, Factory::this_symbol(), Variable::VAR, + false, Variable::THIS); + var->rewrite_ = new Slot(var, Slot::PARAMETER, -1); + receiver_ = new VariableProxy(Factory::this_symbol(), true, false); + receiver_->BindTo(var); if (is_function_scope()) { // Declare 'arguments' variable which exists in all functions. - // Note that it may never be accessed, in which case it won't - // be allocated during variable allocation. - locals_.Declare(this, Factory::arguments_symbol(), Variable::VAR, - true, Variable::ARGUMENTS); + // Note that it might never be accessed, in which case it won't be + // allocated during variable allocation. + variables_.Declare(this, Factory::arguments_symbol(), Variable::VAR, + true, Variable::ARGUMENTS); } } -Variable* Scope::LookupLocal(Handle name) { - return locals_.Lookup(name); +Variable* Scope::LocalLookup(Handle name) { + return variables_.Lookup(name); } @@ -196,7 +195,7 @@ Variable* Scope::Lookup(Handle name) { for (Scope* scope = this; scope != NULL; scope = scope->outer_scope()) { - Variable* var = scope->LookupLocal(name); + Variable* var = scope->LocalLookup(name); if (var != NULL) return var; } return NULL; @@ -210,18 +209,25 @@ Variable* Scope::DeclareFunctionVar(Handle name) { } -Variable* Scope::Declare(Handle name, Variable::Mode mode) { +Variable* Scope::DeclareLocal(Handle name, Variable::Mode mode) { // DYNAMIC variables are introduces during variable allocation, // INTERNAL variables are allocated explicitly, and TEMPORARY // variables are allocated via NewTemporary(). ASSERT(mode == Variable::VAR || mode == Variable::CONST); - return locals_.Declare(this, name, mode, true, Variable::NORMAL); + return variables_.Declare(this, name, mode, true, Variable::NORMAL); +} + + +Variable* Scope::DeclareGlobal(Handle name) { + ASSERT(is_global_scope()); + return variables_.Declare(this, name, Variable::DYNAMIC, true, + Variable::NORMAL); } void Scope::AddParameter(Variable* var) { ASSERT(is_function_scope()); - ASSERT(LookupLocal(var->name()) == var); + ASSERT(LocalLookup(var->name()) == var); params_.Add(var); } @@ -291,7 +297,9 @@ void Scope::CollectUsedVariables(List* locals) { locals->Add(var); } } - for (LocalsMap::Entry* p = locals_.Start(); p != NULL; p = locals_.Next(p)) { + for (VariableMap::Entry* p = variables_.Start(); + p != NULL; + p = variables_.Next(p)) { Variable* var = reinterpret_cast(p->value); if (var->var_uses()->is_used()) { locals->Add(var); @@ -410,8 +418,8 @@ static void PrintVar(PrettyPrinter* printer, int indent, Variable* var) { } -static void PrintMap(PrettyPrinter* printer, int indent, LocalsMap* map) { - for (LocalsMap::Entry* p = map->Start(); p != NULL; p = map->Next(p)) { +static void PrintMap(PrettyPrinter* printer, int indent, VariableMap* map) { + for (VariableMap::Entry* p = map->Start(); p != NULL; p = map->Next(p)) { Variable* var = reinterpret_cast(p->value); PrintVar(printer, indent, var); } @@ -478,7 +486,7 @@ void Scope::Print(int n) { } Indent(n1, "// local vars\n"); - PrintMap(&printer, n1, &locals_); + PrintMap(&printer, n1, &variables_); Indent(n1, "// dynamic vars\n"); if (dynamics_ != NULL) { @@ -502,7 +510,7 @@ void Scope::Print(int n) { Variable* Scope::NonLocal(Handle name, Variable::Mode mode) { if (dynamics_ == NULL) dynamics_ = new DynamicScopePart(); - LocalsMap* map = dynamics_->GetMap(mode); + VariableMap* map = dynamics_->GetMap(mode); Variable* var = map->Lookup(name); if (var == NULL) { // Declare a new non-local. @@ -530,7 +538,7 @@ Variable* Scope::LookupRecursive(Handle name, bool guess = scope_calls_eval_; // Try to find the variable in this scope. - Variable* var = LookupLocal(name); + Variable* var = LocalLookup(name); if (var != NULL) { // We found a variable. If this is not an inner lookup, we are done. @@ -621,8 +629,7 @@ void Scope::ResolveVariable(Scope* global_scope, scope_calls_eval_ || outer_scope_calls_eval_)) { // We must have a global variable. ASSERT(global_scope != NULL); - var = new Variable(global_scope, proxy->name(), - Variable::DYNAMIC, true, Variable::NORMAL); + var = global_scope->DeclareGlobal(proxy->name()); } else if (scope_inside_with_) { // If we are inside a with statement we give up and look up @@ -706,26 +713,26 @@ bool Scope::PropagateScopeInfo(bool outer_scope_calls_eval, bool Scope::MustAllocate(Variable* var) { - // Give var a read/write use if there is a chance it might be - // accessed via an eval() call, or if it is a global variable. - // This is only possible if the variable has a visible name. + // Give var a read/write use if there is a chance it might be accessed + // via an eval() call. This is only possible if the variable has a + // visible name. if ((var->is_this() || var->name()->length() > 0) && (var->is_accessed_from_inner_scope_ || scope_calls_eval_ || inner_scope_calls_eval_ || - scope_contains_with_ || var->is_global())) { + scope_contains_with_)) { var->var_uses()->RecordAccess(1); } - return var->var_uses()->is_used(); + // Global variables do not need to be allocated. + return !var->is_global() && var->var_uses()->is_used(); } bool Scope::MustAllocateInContext(Variable* var) { // If var is accessed from an inner scope, or if there is a - // possibility that it might be accessed from the current or - // an inner scope (through an eval() call), it must be allocated - // in the context. - // Exceptions: Global variables and temporary variables must - // never be allocated in the (FixedArray part of the) context. + // possibility that it might be accessed from the current or an inner + // scope (through an eval() call), it must be allocated in the + // context. Exception: temporary variables are not allocated in the + // context. return var->mode() != Variable::TEMPORARY && (var->is_accessed_from_inner_scope_ || @@ -755,7 +762,7 @@ void Scope::AllocateHeapSlot(Variable* var) { void Scope::AllocateParameterLocals() { ASSERT(is_function_scope()); - Variable* arguments = LookupLocal(Factory::arguments_symbol()); + Variable* arguments = LocalLookup(Factory::arguments_symbol()); ASSERT(arguments != NULL); // functions have 'arguments' declared implicitly if (MustAllocate(arguments) && !HasArgumentsParameter()) { // 'arguments' is used. Unless there is also a parameter called @@ -865,7 +872,7 @@ void Scope::AllocateNonParameterLocal(Variable* var) { ASSERT(var->rewrite_ == NULL || (!var->IsVariable(Factory::result_symbol())) || (var->slot() == NULL || var->slot()->type() != Slot::LOCAL)); - if (MustAllocate(var) && var->rewrite_ == NULL) { + if (var->rewrite_ == NULL && MustAllocate(var)) { if (MustAllocateInContext(var)) { AllocateHeapSlot(var); } else { @@ -876,27 +883,21 @@ void Scope::AllocateNonParameterLocal(Variable* var) { void Scope::AllocateNonParameterLocals() { - // Each variable occurs exactly once in the locals_ list; all - // variables that have no rewrite yet are non-parameter locals. - - // Sort them according to use such that the locals with more uses - // get allocated first. - if (FLAG_usage_computation) { - // This is currently not implemented. - } - + // All variables that have no rewrite yet are non-parameter locals. for (int i = 0; i < temps_.length(); i++) { AllocateNonParameterLocal(temps_[i]); } - for (LocalsMap::Entry* p = locals_.Start(); p != NULL; p = locals_.Next(p)) { + for (VariableMap::Entry* p = variables_.Start(); + p != NULL; + p = variables_.Next(p)) { Variable* var = reinterpret_cast(p->value); AllocateNonParameterLocal(var); } - // Note: For now, function_ must be allocated at the very end. If - // it gets allocated in the context, it must be the last slot in the - // context, because of the current ScopeInfo implementation (see + // For now, function_ must be allocated at the very end. If it gets + // allocated in the context, it must be the last slot in the context, + // because of the current ScopeInfo implementation (see // ScopeInfo::ScopeInfo(FunctionScope* scope) constructor). if (function_ != NULL) { AllocateNonParameterLocal(function_); diff --git a/src/scopes.h b/src/scopes.h index ea4e0f746..5767d9f01 100644 --- a/src/scopes.h +++ b/src/scopes.h @@ -35,19 +35,22 @@ namespace v8 { namespace internal { -// A hash map to support fast local variable declaration and lookup. -class LocalsMap: public HashMap { +// A hash map to support fast variable declaration and lookup. +class VariableMap: public HashMap { public: - LocalsMap(); + VariableMap(); // Dummy constructor. This constructor doesn't set up the map // properly so don't use it unless you have a good reason. - explicit LocalsMap(bool gotta_love_static_overloading); + explicit VariableMap(bool gotta_love_static_overloading); - virtual ~LocalsMap(); + virtual ~VariableMap(); - Variable* Declare(Scope* scope, Handle name, Variable::Mode mode, - bool is_valid_LHS, Variable::Kind kind); + Variable* Declare(Scope* scope, + Handle name, + Variable::Mode mode, + bool is_valid_lhs, + Variable::Kind kind); Variable* Lookup(Handle name); }; @@ -59,14 +62,14 @@ class LocalsMap: public HashMap { // and setup time for scopes that don't need them. class DynamicScopePart : public ZoneObject { public: - LocalsMap* GetMap(Variable::Mode mode) { + VariableMap* GetMap(Variable::Mode mode) { int index = mode - Variable::DYNAMIC; ASSERT(index >= 0 && index < 3); return &maps_[index]; } private: - LocalsMap maps_[3]; + VariableMap maps_[3]; }; @@ -105,7 +108,7 @@ class Scope: public ZoneObject { // Declarations // Lookup a variable in this scope. Returns the variable or NULL if not found. - virtual Variable* LookupLocal(Handle name); + virtual Variable* LocalLookup(Handle name); // Lookup a variable in this scope or outer scopes. // Returns the variable or NULL if not found. @@ -116,9 +119,15 @@ class Scope: public ZoneObject { // outer scope. Only possible for function scopes; at most one variable. Variable* DeclareFunctionVar(Handle name); - // Declare a variable in this scope. If the variable has been + // Declare a local variable in this scope. If the variable has been // declared before, the previously declared variable is returned. - virtual Variable* Declare(Handle name, Variable::Mode mode); + virtual Variable* DeclareLocal(Handle name, Variable::Mode mode); + + // Declare an implicit global variable in this scope which must be a + // global scope. The variable was introduced (possibly from an inner + // scope) by a reference to an unresolved variable with no intervening + // with statements or eval calls. + Variable* DeclareGlobal(Handle name); // Add a parameter to the parameter list. The parameter must have been // declared via Declare. The same parameter may occur more then once in @@ -288,25 +297,28 @@ class Scope: public ZoneObject { Handle scope_name_; // The variables declared in this scope: - // all user-declared variables (incl. parameters) - LocalsMap locals_; - // compiler-allocated (user-invisible) temporaries + // + // All user-declared variables (incl. parameters). For global scopes + // variables may be implicitly 'declared' by being used (possibly in + // an inner scope) with no intervening with statements or eval calls. + VariableMap variables_; + // Compiler-allocated (user-invisible) temporaries. ZoneList temps_; - // parameter list in source order + // Parameter list in source order. ZoneList params_; - // variables that must be looked up dynamically + // Variables that must be looked up dynamically. DynamicScopePart* dynamics_; - // unresolved variables referred to from this scope + // Unresolved variables referred to from this scope. ZoneList unresolved_; - // declarations + // Declarations. ZoneList decls_; - // convenience variable + // Convenience variable. VariableProxy* receiver_; - // function variable, if any; function scopes only + // Function variable, if any; function scopes only. Variable* function_; - // convenience variable; function scopes only + // Convenience variable; function scopes only. VariableProxy* arguments_; - // convenience variable; function scopes only + // Convenience variable; function scopes only. VariableProxy* arguments_shadow_; // Illegal redeclaration. diff --git a/src/variables.h b/src/variables.h index c0d14356f..3f1f11bcd 100644 --- a/src/variables.h +++ b/src/variables.h @@ -143,6 +143,12 @@ class Variable: public ZoneObject { ARGUMENTS }; + Variable(Scope* scope, + Handle name, + Mode mode, + bool is_valid_lhs, + Kind kind); + // Printing support static const char* Mode2String(Mode mode); @@ -196,9 +202,6 @@ class Variable: public ZoneObject { SmiAnalysis* type() { return &type_; } private: - Variable(Scope* scope, Handle name, Mode mode, bool is_valid_LHS, - Kind kind); - Scope* scope_; Handle name_; Mode mode_; @@ -216,13 +219,10 @@ class Variable: public ZoneObject { SmiAnalysis type_; // Code generation. - // rewrite_ is usually a Slot or a Property, but maybe any expression. + // rewrite_ is usually a Slot or a Property, but may be any expression. Expression* rewrite_; - friend class VariableProxy; - friend class Scope; - friend class LocalsMap; - friend class AstBuildingParser; + friend class Scope; // Has explicit access to rewrite_. };