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/ast/scopes.h" | 5 #include "src/ast/scopes.h" |
6 | 6 |
7 #include <set> | 7 #include <set> |
8 | 8 |
9 #include "src/accessors.h" | 9 #include "src/accessors.h" |
10 #include "src/bootstrapper.h" | 10 #include "src/bootstrapper.h" |
(...skipping 691 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
702 DeclarationScope* scope = GetClosureScope(); | 702 DeclarationScope* scope = GetClosureScope(); |
703 Variable* var = new(zone()) Variable(scope, | 703 Variable* var = new(zone()) Variable(scope, |
704 name, | 704 name, |
705 TEMPORARY, | 705 TEMPORARY, |
706 Variable::NORMAL, | 706 Variable::NORMAL, |
707 kCreatedInitialized); | 707 kCreatedInitialized); |
708 scope->AddTemporary(var); | 708 scope->AddTemporary(var); |
709 return var; | 709 return var; |
710 } | 710 } |
711 | 711 |
712 int DeclarationScope::RemoveTemporary(Variable* var) { | |
713 DCHECK(!already_resolved()); | |
714 DCHECK_NOT_NULL(var); | |
715 // Temporaries are only placed in ClosureScopes. | |
716 DCHECK_EQ(GetClosureScope(), this); | |
717 DCHECK_EQ(var->scope()->GetClosureScope(), var->scope()); | |
718 // If the temporary is not here, return quickly. | |
719 if (var->scope() != this) return -1; | |
720 // Most likely (always?) any temporary variable we want to remove | |
721 // was just added before, so we search backwards. | |
722 for (int i = temps_.length(); i-- > 0;) { | |
723 if (temps_[i] == var) { | |
724 // Don't shrink temps_, as callers of this method expect | |
725 // the returned indices to be unique per-scope. | |
726 temps_[i] = nullptr; | |
727 return i; | |
728 } | |
729 } | |
730 return -1; | |
731 } | |
732 | |
733 | |
734 void Scope::AddDeclaration(Declaration* declaration) { | 712 void Scope::AddDeclaration(Declaration* declaration) { |
735 DCHECK(!already_resolved()); | 713 DCHECK(!already_resolved()); |
736 decls_.Add(declaration, zone()); | 714 decls_.Add(declaration, zone()); |
737 } | 715 } |
738 | 716 |
739 | 717 |
740 Declaration* Scope::CheckConflictingVarDeclarations() { | 718 Declaration* Scope::CheckConflictingVarDeclarations() { |
741 int length = decls_.length(); | 719 int length = decls_.length(); |
742 for (int i = 0; i < length; i++) { | 720 for (int i = 0; i < length; i++) { |
743 Declaration* decl = decls_[i]; | 721 Declaration* decl = decls_[i]; |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
806 DCHECK(stack_locals != NULL); | 784 DCHECK(stack_locals != NULL); |
807 DCHECK(context_locals != NULL); | 785 DCHECK(context_locals != NULL); |
808 DCHECK(context_globals != NULL); | 786 DCHECK(context_globals != NULL); |
809 | 787 |
810 // Collect temporaries which are always allocated on the stack, unless the | 788 // Collect temporaries which are always allocated on the stack, unless the |
811 // context as a whole has forced context allocation. | 789 // context as a whole has forced context allocation. |
812 if (is_declaration_scope()) { | 790 if (is_declaration_scope()) { |
813 ZoneList<Variable*>* temps = AsDeclarationScope()->temps(); | 791 ZoneList<Variable*>* temps = AsDeclarationScope()->temps(); |
814 for (int i = 0; i < temps->length(); i++) { | 792 for (int i = 0; i < temps->length(); i++) { |
815 Variable* var = (*temps)[i]; | 793 Variable* var = (*temps)[i]; |
816 if (var == nullptr) continue; | |
817 if (var->is_used()) { | 794 if (var->is_used()) { |
818 if (var->IsContextSlot()) { | 795 if (var->IsContextSlot()) { |
819 DCHECK(has_forced_context_allocation()); | 796 DCHECK(has_forced_context_allocation()); |
820 context_locals->Add(var, zone()); | 797 context_locals->Add(var, zone()); |
821 } else if (var->IsStackLocal()) { | 798 } else if (var->IsStackLocal()) { |
822 stack_locals->Add(var, zone()); | 799 stack_locals->Add(var, zone()); |
823 } else { | 800 } else { |
824 DCHECK(var->IsParameter()); | 801 DCHECK(var->IsParameter()); |
825 } | 802 } |
826 } | 803 } |
(...skipping 356 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1183 // Print locals. | 1160 // Print locals. |
1184 if (function != nullptr) { | 1161 if (function != nullptr) { |
1185 Indent(n1, "// function var:\n"); | 1162 Indent(n1, "// function var:\n"); |
1186 PrintVar(n1, function); | 1163 PrintVar(n1, function); |
1187 } | 1164 } |
1188 | 1165 |
1189 if (is_declaration_scope()) { | 1166 if (is_declaration_scope()) { |
1190 bool printed_header = false; | 1167 bool printed_header = false; |
1191 ZoneList<Variable*>* temps = AsDeclarationScope()->temps(); | 1168 ZoneList<Variable*>* temps = AsDeclarationScope()->temps(); |
1192 for (int i = 0; i < temps->length(); i++) { | 1169 for (int i = 0; i < temps->length(); i++) { |
1193 if ((*temps)[i] != nullptr) { | 1170 if (!printed_header) { |
1194 if (!printed_header) { | 1171 printed_header = true; |
1195 printed_header = true; | 1172 Indent(n1, "// temporary vars:\n"); |
1196 Indent(n1, "// temporary vars:\n"); | |
1197 } | |
1198 PrintVar(n1, (*temps)[i]); | |
1199 } | 1173 } |
| 1174 PrintVar(n1, (*temps)[i]); |
1200 } | 1175 } |
1201 } | 1176 } |
1202 | 1177 |
1203 if (variables_.Start() != NULL) { | 1178 if (variables_.Start() != NULL) { |
1204 Indent(n1, "// local vars:\n"); | 1179 Indent(n1, "// local vars:\n"); |
1205 PrintMap(n1, &variables_); | 1180 PrintMap(n1, &variables_); |
1206 } | 1181 } |
1207 | 1182 |
1208 if (dynamics_ != NULL) { | 1183 if (dynamics_ != NULL) { |
1209 Indent(n1, "// dynamic vars:\n"); | 1184 Indent(n1, "// dynamic vars:\n"); |
(...skipping 444 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1654 } | 1629 } |
1655 } | 1630 } |
1656 } | 1631 } |
1657 | 1632 |
1658 void Scope::AllocateNonParameterLocalsAndDeclaredGlobals( | 1633 void Scope::AllocateNonParameterLocalsAndDeclaredGlobals( |
1659 AstValueFactory* ast_value_factory) { | 1634 AstValueFactory* ast_value_factory) { |
1660 // All variables that have no rewrite yet are non-parameter locals. | 1635 // All variables that have no rewrite yet are non-parameter locals. |
1661 if (is_declaration_scope()) { | 1636 if (is_declaration_scope()) { |
1662 ZoneList<Variable*>* temps = AsDeclarationScope()->temps(); | 1637 ZoneList<Variable*>* temps = AsDeclarationScope()->temps(); |
1663 for (int i = 0; i < temps->length(); i++) { | 1638 for (int i = 0; i < temps->length(); i++) { |
1664 if ((*temps)[i] == nullptr) continue; | |
1665 AllocateNonParameterLocal((*temps)[i], ast_value_factory); | 1639 AllocateNonParameterLocal((*temps)[i], ast_value_factory); |
1666 } | 1640 } |
1667 } | 1641 } |
1668 | 1642 |
1669 ZoneList<VarAndOrder> vars(variables_.occupancy(), zone()); | 1643 ZoneList<VarAndOrder> vars(variables_.occupancy(), zone()); |
1670 for (VariableMap::Entry* p = variables_.Start(); | 1644 for (VariableMap::Entry* p = variables_.Start(); |
1671 p != NULL; | 1645 p != NULL; |
1672 p = variables_.Next(p)) { | 1646 p = variables_.Next(p)) { |
1673 Variable* var = reinterpret_cast<Variable*>(p->value); | 1647 Variable* var = reinterpret_cast<Variable*>(p->value); |
1674 vars.Add(VarAndOrder(var, p->order), zone()); | 1648 vars.Add(VarAndOrder(var, p->order), zone()); |
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1790 function != nullptr && function->IsContextSlot(); | 1764 function != nullptr && function->IsContextSlot(); |
1791 return num_heap_slots() - Context::MIN_CONTEXT_SLOTS - num_global_slots() - | 1765 return num_heap_slots() - Context::MIN_CONTEXT_SLOTS - num_global_slots() - |
1792 (is_function_var_in_context ? 1 : 0); | 1766 (is_function_var_in_context ? 1 : 0); |
1793 } | 1767 } |
1794 | 1768 |
1795 | 1769 |
1796 int Scope::ContextGlobalCount() const { return num_global_slots(); } | 1770 int Scope::ContextGlobalCount() const { return num_global_slots(); } |
1797 | 1771 |
1798 } // namespace internal | 1772 } // namespace internal |
1799 } // namespace v8 | 1773 } // namespace v8 |
OLD | NEW |