| OLD | NEW |
| 1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 the V8 project authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "src/v8.h" | 5 #include "src/v8.h" |
| 6 | 6 |
| 7 #include "src/accessors.h" | 7 #include "src/accessors.h" |
| 8 #include "src/bootstrapper.h" | 8 #include "src/bootstrapper.h" |
| 9 #include "src/messages.h" | 9 #include "src/messages.h" |
| 10 #include "src/parser.h" | 10 #include "src/parser.h" |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 67 } | 67 } |
| 68 | 68 |
| 69 | 69 |
| 70 // ---------------------------------------------------------------------------- | 70 // ---------------------------------------------------------------------------- |
| 71 // Implementation of Scope | 71 // Implementation of Scope |
| 72 | 72 |
| 73 Scope::Scope(Zone* zone, Scope* outer_scope, ScopeType scope_type, | 73 Scope::Scope(Zone* zone, Scope* outer_scope, ScopeType scope_type, |
| 74 AstValueFactory* ast_value_factory, FunctionKind function_kind) | 74 AstValueFactory* ast_value_factory, FunctionKind function_kind) |
| 75 : inner_scopes_(4, zone), | 75 : inner_scopes_(4, zone), |
| 76 variables_(zone), | 76 variables_(zone), |
| 77 internals_(4, zone), | |
| 78 temps_(4, zone), | 77 temps_(4, zone), |
| 79 params_(4, zone), | 78 params_(4, zone), |
| 80 unresolved_(16, zone), | 79 unresolved_(16, zone), |
| 81 decls_(4, zone), | 80 decls_(4, zone), |
| 82 module_descriptor_( | 81 module_descriptor_( |
| 83 scope_type == MODULE_SCOPE ? ModuleDescriptor::New(zone) : NULL), | 82 scope_type == MODULE_SCOPE ? ModuleDescriptor::New(zone) : NULL), |
| 84 already_resolved_(false), | 83 already_resolved_(false), |
| 85 ast_value_factory_(ast_value_factory), | 84 ast_value_factory_(ast_value_factory), |
| 86 zone_(zone), | 85 zone_(zone), |
| 87 class_declaration_group_start_(-1) { | 86 class_declaration_group_start_(-1) { |
| 88 SetDefaults(scope_type, outer_scope, Handle<ScopeInfo>::null(), | 87 SetDefaults(scope_type, outer_scope, Handle<ScopeInfo>::null(), |
| 89 function_kind); | 88 function_kind); |
| 90 // The outermost scope must be a script scope. | 89 // The outermost scope must be a script scope. |
| 91 DCHECK(scope_type == SCRIPT_SCOPE || outer_scope != NULL); | 90 DCHECK(scope_type == SCRIPT_SCOPE || outer_scope != NULL); |
| 92 DCHECK(!HasIllegalRedeclaration()); | 91 DCHECK(!HasIllegalRedeclaration()); |
| 93 } | 92 } |
| 94 | 93 |
| 95 | 94 |
| 96 Scope::Scope(Zone* zone, Scope* inner_scope, ScopeType scope_type, | 95 Scope::Scope(Zone* zone, Scope* inner_scope, ScopeType scope_type, |
| 97 Handle<ScopeInfo> scope_info, AstValueFactory* value_factory) | 96 Handle<ScopeInfo> scope_info, AstValueFactory* value_factory) |
| 98 : inner_scopes_(4, zone), | 97 : inner_scopes_(4, zone), |
| 99 variables_(zone), | 98 variables_(zone), |
| 100 internals_(4, zone), | |
| 101 temps_(4, zone), | 99 temps_(4, zone), |
| 102 params_(4, zone), | 100 params_(4, zone), |
| 103 unresolved_(16, zone), | 101 unresolved_(16, zone), |
| 104 decls_(4, zone), | 102 decls_(4, zone), |
| 105 module_descriptor_(NULL), | 103 module_descriptor_(NULL), |
| 106 already_resolved_(true), | 104 already_resolved_(true), |
| 107 ast_value_factory_(value_factory), | 105 ast_value_factory_(value_factory), |
| 108 zone_(zone), | 106 zone_(zone), |
| 109 class_declaration_group_start_(-1) { | 107 class_declaration_group_start_(-1) { |
| 110 SetDefaults(scope_type, NULL, scope_info); | 108 SetDefaults(scope_type, NULL, scope_info); |
| 111 if (!scope_info.is_null()) { | 109 if (!scope_info.is_null()) { |
| 112 num_heap_slots_ = scope_info_->ContextLength(); | 110 num_heap_slots_ = scope_info_->ContextLength(); |
| 113 } | 111 } |
| 114 // Ensure at least MIN_CONTEXT_SLOTS to indicate a materialized context. | 112 // Ensure at least MIN_CONTEXT_SLOTS to indicate a materialized context. |
| 115 num_heap_slots_ = Max(num_heap_slots_, | 113 num_heap_slots_ = Max(num_heap_slots_, |
| 116 static_cast<int>(Context::MIN_CONTEXT_SLOTS)); | 114 static_cast<int>(Context::MIN_CONTEXT_SLOTS)); |
| 117 AddInnerScope(inner_scope); | 115 AddInnerScope(inner_scope); |
| 118 } | 116 } |
| 119 | 117 |
| 120 | 118 |
| 121 Scope::Scope(Zone* zone, Scope* inner_scope, | 119 Scope::Scope(Zone* zone, Scope* inner_scope, |
| 122 const AstRawString* catch_variable_name, | 120 const AstRawString* catch_variable_name, |
| 123 AstValueFactory* value_factory) | 121 AstValueFactory* value_factory) |
| 124 : inner_scopes_(1, zone), | 122 : inner_scopes_(1, zone), |
| 125 variables_(zone), | 123 variables_(zone), |
| 126 internals_(0, zone), | |
| 127 temps_(0, zone), | 124 temps_(0, zone), |
| 128 params_(0, zone), | 125 params_(0, zone), |
| 129 unresolved_(0, zone), | 126 unresolved_(0, zone), |
| 130 decls_(0, zone), | 127 decls_(0, zone), |
| 131 module_descriptor_(NULL), | 128 module_descriptor_(NULL), |
| 132 already_resolved_(true), | 129 already_resolved_(true), |
| 133 ast_value_factory_(value_factory), | 130 ast_value_factory_(value_factory), |
| 134 zone_(zone), | 131 zone_(zone), |
| 135 class_declaration_group_start_(-1) { | 132 class_declaration_group_start_(-1) { |
| 136 SetDefaults(CATCH_SCOPE, NULL, Handle<ScopeInfo>::null()); | 133 SetDefaults(CATCH_SCOPE, NULL, Handle<ScopeInfo>::null()); |
| (...skipping 200 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 337 IsAccessorFunction(function_kind_)) { | 334 IsAccessorFunction(function_kind_)) { |
| 338 variables_.Declare(this, ast_value_factory_->this_function_string(), | 335 variables_.Declare(this, ast_value_factory_->this_function_string(), |
| 339 CONST, Variable::NORMAL, kCreatedInitialized); | 336 CONST, Variable::NORMAL, kCreatedInitialized); |
| 340 } | 337 } |
| 341 } | 338 } |
| 342 } | 339 } |
| 343 | 340 |
| 344 | 341 |
| 345 Scope* Scope::FinalizeBlockScope() { | 342 Scope* Scope::FinalizeBlockScope() { |
| 346 DCHECK(is_block_scope()); | 343 DCHECK(is_block_scope()); |
| 347 DCHECK(internals_.is_empty()); | |
| 348 DCHECK(temps_.is_empty()); | 344 DCHECK(temps_.is_empty()); |
| 349 DCHECK(params_.is_empty()); | 345 DCHECK(params_.is_empty()); |
| 350 | 346 |
| 351 if (num_var_or_const() > 0) return this; | 347 if (num_var_or_const() > 0) return this; |
| 352 | 348 |
| 353 // Remove this scope from outer scope. | 349 // Remove this scope from outer scope. |
| 354 for (int i = 0; i < outer_scope_->inner_scopes_.length(); i++) { | 350 for (int i = 0; i < outer_scope_->inner_scopes_.length(); i++) { |
| 355 if (outer_scope_->inner_scopes_[i] == this) { | 351 if (outer_scope_->inner_scopes_[i] == this) { |
| 356 outer_scope_->inner_scopes_.Remove(i); | 352 outer_scope_->inner_scopes_.Remove(i); |
| 357 break; | 353 break; |
| (...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 489 return var; | 485 return var; |
| 490 } | 486 } |
| 491 | 487 |
| 492 | 488 |
| 493 Variable* Scope::DeclareLocal(const AstRawString* name, VariableMode mode, | 489 Variable* Scope::DeclareLocal(const AstRawString* name, VariableMode mode, |
| 494 InitializationFlag init_flag, Variable::Kind kind, | 490 InitializationFlag init_flag, Variable::Kind kind, |
| 495 MaybeAssignedFlag maybe_assigned_flag, | 491 MaybeAssignedFlag maybe_assigned_flag, |
| 496 int declaration_group_start) { | 492 int declaration_group_start) { |
| 497 DCHECK(!already_resolved()); | 493 DCHECK(!already_resolved()); |
| 498 // This function handles VAR, LET, and CONST modes. DYNAMIC variables are | 494 // This function handles VAR, LET, and CONST modes. DYNAMIC variables are |
| 499 // introduces during variable allocation, INTERNAL variables are allocated | 495 // introduces during variable allocation, and TEMPORARY variables are |
| 500 // explicitly, and TEMPORARY variables are allocated via NewTemporary(). | 496 // allocated via NewTemporary(). |
| 501 DCHECK(IsDeclaredVariableMode(mode)); | 497 DCHECK(IsDeclaredVariableMode(mode)); |
| 502 ++num_var_or_const_; | 498 ++num_var_or_const_; |
| 503 return variables_.Declare(this, name, mode, kind, init_flag, | 499 return variables_.Declare(this, name, mode, kind, init_flag, |
| 504 maybe_assigned_flag, declaration_group_start); | 500 maybe_assigned_flag, declaration_group_start); |
| 505 } | 501 } |
| 506 | 502 |
| 507 | 503 |
| 508 Variable* Scope::DeclareDynamicGlobal(const AstRawString* name) { | 504 Variable* Scope::DeclareDynamicGlobal(const AstRawString* name) { |
| 509 DCHECK(is_script_scope()); | 505 DCHECK(is_script_scope()); |
| 510 return variables_.Declare(this, | 506 return variables_.Declare(this, |
| (...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 609 | 605 |
| 610 | 606 |
| 611 void Scope::CollectStackAndContextLocals( | 607 void Scope::CollectStackAndContextLocals( |
| 612 ZoneList<Variable*>* stack_locals, ZoneList<Variable*>* context_locals, | 608 ZoneList<Variable*>* stack_locals, ZoneList<Variable*>* context_locals, |
| 613 ZoneList<Variable*>* context_globals, | 609 ZoneList<Variable*>* context_globals, |
| 614 ZoneList<Variable*>* strong_mode_free_variables) { | 610 ZoneList<Variable*>* strong_mode_free_variables) { |
| 615 DCHECK(stack_locals != NULL); | 611 DCHECK(stack_locals != NULL); |
| 616 DCHECK(context_locals != NULL); | 612 DCHECK(context_locals != NULL); |
| 617 DCHECK(context_globals != NULL); | 613 DCHECK(context_globals != NULL); |
| 618 | 614 |
| 619 // Collect internals which are always allocated on the heap. | |
| 620 for (int i = 0; i < internals_.length(); i++) { | |
| 621 Variable* var = internals_[i]; | |
| 622 if (var->is_used()) { | |
| 623 DCHECK(var->IsContextSlot()); | |
| 624 context_locals->Add(var, zone()); | |
| 625 } | |
| 626 } | |
| 627 | |
| 628 // Collect temporaries which are always allocated on the stack, unless the | 615 // Collect temporaries which are always allocated on the stack, unless the |
| 629 // context as a whole has forced context allocation. | 616 // context as a whole has forced context allocation. |
| 630 for (int i = 0; i < temps_.length(); i++) { | 617 for (int i = 0; i < temps_.length(); i++) { |
| 631 Variable* var = temps_[i]; | 618 Variable* var = temps_[i]; |
| 632 if (var->is_used()) { | 619 if (var->is_used()) { |
| 633 if (var->IsContextSlot()) { | 620 if (var->IsContextSlot()) { |
| 634 DCHECK(has_forced_context_allocation()); | 621 DCHECK(has_forced_context_allocation()); |
| 635 context_locals->Add(var, zone()); | 622 context_locals->Add(var, zone()); |
| 636 } else { | 623 } else { |
| 637 DCHECK(var->IsStackLocal()); | 624 DCHECK(var->IsStackLocal()); |
| (...skipping 335 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 973 PrintVar(n1, function_->proxy()->var()); | 960 PrintVar(n1, function_->proxy()->var()); |
| 974 } | 961 } |
| 975 | 962 |
| 976 if (temps_.length() > 0) { | 963 if (temps_.length() > 0) { |
| 977 Indent(n1, "// temporary vars:\n"); | 964 Indent(n1, "// temporary vars:\n"); |
| 978 for (int i = 0; i < temps_.length(); i++) { | 965 for (int i = 0; i < temps_.length(); i++) { |
| 979 PrintVar(n1, temps_[i]); | 966 PrintVar(n1, temps_[i]); |
| 980 } | 967 } |
| 981 } | 968 } |
| 982 | 969 |
| 983 if (internals_.length() > 0) { | |
| 984 Indent(n1, "// internal vars:\n"); | |
| 985 for (int i = 0; i < internals_.length(); i++) { | |
| 986 PrintVar(n1, internals_[i]); | |
| 987 } | |
| 988 } | |
| 989 | |
| 990 if (variables_.Start() != NULL) { | 970 if (variables_.Start() != NULL) { |
| 991 Indent(n1, "// local vars:\n"); | 971 Indent(n1, "// local vars:\n"); |
| 992 PrintMap(n1, &variables_); | 972 PrintMap(n1, &variables_); |
| 993 } | 973 } |
| 994 | 974 |
| 995 if (dynamics_ != NULL) { | 975 if (dynamics_ != NULL) { |
| 996 Indent(n1, "// dynamic vars:\n"); | 976 Indent(n1, "// dynamic vars:\n"); |
| 997 PrintMap(n1, dynamics_->GetMap(DYNAMIC)); | 977 PrintMap(n1, dynamics_->GetMap(DYNAMIC)); |
| 998 PrintMap(n1, dynamics_->GetMap(DYNAMIC_LOCAL)); | 978 PrintMap(n1, dynamics_->GetMap(DYNAMIC_LOCAL)); |
| 999 PrintMap(n1, dynamics_->GetMap(DYNAMIC_GLOBAL)); | 979 PrintMap(n1, dynamics_->GetMap(DYNAMIC_GLOBAL)); |
| (...skipping 502 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1502 } | 1482 } |
| 1503 } | 1483 } |
| 1504 | 1484 |
| 1505 | 1485 |
| 1506 void Scope::AllocateNonParameterLocalsAndDeclaredGlobals(Isolate* isolate) { | 1486 void Scope::AllocateNonParameterLocalsAndDeclaredGlobals(Isolate* isolate) { |
| 1507 // All variables that have no rewrite yet are non-parameter locals. | 1487 // All variables that have no rewrite yet are non-parameter locals. |
| 1508 for (int i = 0; i < temps_.length(); i++) { | 1488 for (int i = 0; i < temps_.length(); i++) { |
| 1509 AllocateNonParameterLocal(isolate, temps_[i]); | 1489 AllocateNonParameterLocal(isolate, temps_[i]); |
| 1510 } | 1490 } |
| 1511 | 1491 |
| 1512 for (int i = 0; i < internals_.length(); i++) { | |
| 1513 AllocateNonParameterLocal(isolate, internals_[i]); | |
| 1514 } | |
| 1515 | |
| 1516 ZoneList<VarAndOrder> vars(variables_.occupancy(), zone()); | 1492 ZoneList<VarAndOrder> vars(variables_.occupancy(), zone()); |
| 1517 for (VariableMap::Entry* p = variables_.Start(); | 1493 for (VariableMap::Entry* p = variables_.Start(); |
| 1518 p != NULL; | 1494 p != NULL; |
| 1519 p = variables_.Next(p)) { | 1495 p = variables_.Next(p)) { |
| 1520 Variable* var = reinterpret_cast<Variable*>(p->value); | 1496 Variable* var = reinterpret_cast<Variable*>(p->value); |
| 1521 vars.Add(VarAndOrder(var, p->order), zone()); | 1497 vars.Add(VarAndOrder(var, p->order), zone()); |
| 1522 } | 1498 } |
| 1523 vars.Sort(VarAndOrder::Compare); | 1499 vars.Sort(VarAndOrder::Compare); |
| 1524 int var_count = vars.length(); | 1500 int var_count = vars.length(); |
| 1525 for (int i = 0; i < var_count; i++) { | 1501 for (int i = 0; i < var_count; i++) { |
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1625 bool is_function_var_in_context = | 1601 bool is_function_var_in_context = |
| 1626 function_ != NULL && function_->proxy()->var()->IsContextSlot(); | 1602 function_ != NULL && function_->proxy()->var()->IsContextSlot(); |
| 1627 return num_heap_slots() - Context::MIN_CONTEXT_SLOTS - | 1603 return num_heap_slots() - Context::MIN_CONTEXT_SLOTS - |
| 1628 2 * num_global_slots() - (is_function_var_in_context ? 1 : 0); | 1604 2 * num_global_slots() - (is_function_var_in_context ? 1 : 0); |
| 1629 } | 1605 } |
| 1630 | 1606 |
| 1631 | 1607 |
| 1632 int Scope::ContextGlobalCount() const { return num_global_slots(); } | 1608 int Scope::ContextGlobalCount() const { return num_global_slots(); } |
| 1633 } // namespace internal | 1609 } // namespace internal |
| 1634 } // namespace v8 | 1610 } // namespace v8 |
| OLD | NEW |