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 1066 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1077 break; | 1077 break; |
1078 case VariableLocation::CONTEXT: | 1078 case VariableLocation::CONTEXT: |
1079 PrintF("context[%d]", var->index()); | 1079 PrintF("context[%d]", var->index()); |
1080 break; | 1080 break; |
1081 case VariableLocation::GLOBAL: | 1081 case VariableLocation::GLOBAL: |
1082 PrintF("global[%d]", var->index()); | 1082 PrintF("global[%d]", var->index()); |
1083 break; | 1083 break; |
1084 case VariableLocation::LOOKUP: | 1084 case VariableLocation::LOOKUP: |
1085 PrintF("lookup"); | 1085 PrintF("lookup"); |
1086 break; | 1086 break; |
| 1087 case VariableLocation::MODULE: |
| 1088 PrintF("module"); |
| 1089 break; |
1087 } | 1090 } |
1088 } | 1091 } |
1089 | 1092 |
1090 | 1093 |
1091 static void PrintVar(int indent, Variable* var) { | 1094 static void PrintVar(int indent, Variable* var) { |
1092 if (var->is_used() || !var->IsUnallocated()) { | 1095 if (var->is_used() || !var->IsUnallocated()) { |
1093 Indent(indent, Variable::Mode2String(var->mode())); | 1096 Indent(indent, Variable::Mode2String(var->mode())); |
1094 PrintF(" "); | 1097 PrintF(" "); |
1095 if (var->raw_name()->IsEmpty()) | 1098 if (var->raw_name()->IsEmpty()) |
1096 PrintF(".%p", reinterpret_cast<void*>(var)); | 1099 PrintF(".%p", reinterpret_cast<void*>(var)); |
(...skipping 388 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1485 force_eager_compilation_ = true; | 1488 force_eager_compilation_ = true; |
1486 } | 1489 } |
1487 if (asm_module_ && inner->scope_type() == FUNCTION_SCOPE) { | 1490 if (asm_module_ && inner->scope_type() == FUNCTION_SCOPE) { |
1488 inner->asm_function_ = true; | 1491 inner->asm_function_ = true; |
1489 } | 1492 } |
1490 } | 1493 } |
1491 } | 1494 } |
1492 | 1495 |
1493 | 1496 |
1494 bool Scope::MustAllocate(Variable* var) { | 1497 bool Scope::MustAllocate(Variable* var) { |
| 1498 DCHECK(var->location() != VariableLocation::MODULE); |
1495 // Give var a read/write use if there is a chance it might be accessed | 1499 // Give var a read/write use if there is a chance it might be accessed |
1496 // via an eval() call. This is only possible if the variable has a | 1500 // via an eval() call. This is only possible if the variable has a |
1497 // visible name. | 1501 // visible name. |
1498 if ((var->is_this() || !var->raw_name()->IsEmpty()) && | 1502 if ((var->is_this() || !var->raw_name()->IsEmpty()) && |
1499 (var->has_forced_context_allocation() || scope_calls_eval_ || | 1503 (var->has_forced_context_allocation() || scope_calls_eval_ || |
1500 inner_scope_calls_eval_ || is_catch_scope() || is_block_scope() || | 1504 inner_scope_calls_eval_ || is_catch_scope() || is_block_scope() || |
1501 is_module_scope() || is_script_scope())) { | 1505 is_module_scope() || is_script_scope())) { |
1502 var->set_is_used(); | 1506 var->set_is_used(); |
1503 if (scope_calls_eval_ || inner_scope_calls_eval_) var->set_maybe_assigned(); | 1507 if (scope_calls_eval_ || inner_scope_calls_eval_) var->set_maybe_assigned(); |
1504 } | 1508 } |
(...skipping 199 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1704 | 1708 |
1705 if (new_target_ != nullptr && !MustAllocate(new_target_)) { | 1709 if (new_target_ != nullptr && !MustAllocate(new_target_)) { |
1706 new_target_ = nullptr; | 1710 new_target_ = nullptr; |
1707 } | 1711 } |
1708 | 1712 |
1709 if (this_function_ != nullptr && !MustAllocate(this_function_)) { | 1713 if (this_function_ != nullptr && !MustAllocate(this_function_)) { |
1710 this_function_ = nullptr; | 1714 this_function_ = nullptr; |
1711 } | 1715 } |
1712 } | 1716 } |
1713 | 1717 |
| 1718 void DeclarationScope::AllocateModuleVariables() { |
| 1719 for (auto entry : module()->imports()) { |
| 1720 if (entry->local_name == nullptr) continue; |
| 1721 if (entry->import_name == nullptr) continue; // Namespace import. |
| 1722 Variable* var = LookupLocal(entry->local_name); |
| 1723 // TODO(neis): Use a meaningful index. |
| 1724 var->AllocateTo(VariableLocation::MODULE, 42); |
| 1725 } |
| 1726 for (auto entry : module()->exports()) { |
| 1727 if (entry->local_name == nullptr) continue; |
| 1728 Variable* var = LookupLocal(entry->local_name); |
| 1729 var->AllocateTo(VariableLocation::MODULE, 42); |
| 1730 } |
| 1731 } |
| 1732 |
1714 void Scope::AllocateVariablesRecursively(AstValueFactory* ast_value_factory) { | 1733 void Scope::AllocateVariablesRecursively(AstValueFactory* ast_value_factory) { |
1715 if (!already_resolved()) { | 1734 if (!already_resolved()) { |
1716 num_stack_slots_ = 0; | 1735 num_stack_slots_ = 0; |
1717 } | 1736 } |
1718 // Allocate variables for inner scopes. | 1737 // Allocate variables for inner scopes. |
1719 for (Scope* scope = inner_scope_; scope != nullptr; scope = scope->sibling_) { | 1738 for (Scope* scope = inner_scope_; scope != nullptr; scope = scope->sibling_) { |
1720 scope->AllocateVariablesRecursively(ast_value_factory); | 1739 scope->AllocateVariablesRecursively(ast_value_factory); |
1721 } | 1740 } |
1722 | 1741 |
1723 // If scope is already resolved, we still need to allocate | 1742 // If scope is already resolved, we still need to allocate |
1724 // variables in inner scopes which might not have been resolved yet. | 1743 // variables in inner scopes which might not have been resolved yet. |
1725 if (already_resolved()) return; | 1744 if (already_resolved()) return; |
1726 // The number of slots required for variables. | 1745 // The number of slots required for variables. |
1727 num_heap_slots_ = Context::MIN_CONTEXT_SLOTS; | 1746 num_heap_slots_ = Context::MIN_CONTEXT_SLOTS; |
1728 | 1747 |
1729 // Allocate variables for this scope. | 1748 // Allocate variables for this scope. |
1730 // Parameters must be allocated first, if any. | 1749 // Parameters must be allocated first, if any. |
1731 if (is_declaration_scope()) { | 1750 if (is_declaration_scope()) { |
1732 if (is_function_scope()) AsDeclarationScope()->AllocateParameterLocals(); | 1751 if (is_module_scope()) { |
| 1752 AsDeclarationScope()->AllocateModuleVariables(); |
| 1753 } else if (is_function_scope()) { |
| 1754 AsDeclarationScope()->AllocateParameterLocals(); |
| 1755 } |
1733 AsDeclarationScope()->AllocateReceiver(); | 1756 AsDeclarationScope()->AllocateReceiver(); |
1734 } | 1757 } |
1735 AllocateNonParameterLocalsAndDeclaredGlobals(ast_value_factory); | 1758 AllocateNonParameterLocalsAndDeclaredGlobals(ast_value_factory); |
1736 | 1759 |
1737 // Force allocation of a context for this scope if necessary. For a 'with' | 1760 // Force allocation of a context for this scope if necessary. For a 'with' |
1738 // scope and for a function scope that makes an 'eval' call we need a context, | 1761 // scope and for a function scope that makes an 'eval' call we need a context, |
1739 // even if no local variables were statically allocated in the scope. | 1762 // even if no local variables were statically allocated in the scope. |
1740 // Likewise for modules. | 1763 // Likewise for modules. |
1741 bool must_have_context = | 1764 bool must_have_context = |
1742 is_with_scope() || is_module_scope() || | 1765 is_with_scope() || is_module_scope() || |
(...skipping 27 matching lines...) Expand all Loading... |
1770 function != NULL && function->proxy()->var()->IsContextSlot(); | 1793 function != NULL && function->proxy()->var()->IsContextSlot(); |
1771 return num_heap_slots() - Context::MIN_CONTEXT_SLOTS - num_global_slots() - | 1794 return num_heap_slots() - Context::MIN_CONTEXT_SLOTS - num_global_slots() - |
1772 (is_function_var_in_context ? 1 : 0); | 1795 (is_function_var_in_context ? 1 : 0); |
1773 } | 1796 } |
1774 | 1797 |
1775 | 1798 |
1776 int Scope::ContextGlobalCount() const { return num_global_slots(); } | 1799 int Scope::ContextGlobalCount() const { return num_global_slots(); } |
1777 | 1800 |
1778 } // namespace internal | 1801 } // namespace internal |
1779 } // namespace v8 | 1802 } // namespace v8 |
OLD | NEW |