OLD | NEW |
1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 22 matching lines...) Expand all Loading... |
33 | 33 |
34 namespace v8 { | 34 namespace v8 { |
35 namespace internal { | 35 namespace internal { |
36 | 36 |
37 class CompilationInfo; | 37 class CompilationInfo; |
38 | 38 |
39 | 39 |
40 // A hash map to support fast variable declaration and lookup. | 40 // A hash map to support fast variable declaration and lookup. |
41 class VariableMap: public ZoneHashMap { | 41 class VariableMap: public ZoneHashMap { |
42 public: | 42 public: |
43 VariableMap(); | 43 explicit VariableMap(Zone* zone); |
44 | 44 |
45 virtual ~VariableMap(); | 45 virtual ~VariableMap(); |
46 | 46 |
47 Variable* Declare(Scope* scope, | 47 Variable* Declare(Scope* scope, |
48 Handle<String> name, | 48 Handle<String> name, |
49 VariableMode mode, | 49 VariableMode mode, |
50 bool is_valid_lhs, | 50 bool is_valid_lhs, |
51 Variable::Kind kind, | 51 Variable::Kind kind, |
52 InitializationFlag initialization_flag, | 52 InitializationFlag initialization_flag, |
53 Interface* interface = Interface::NewValue()); | 53 Interface* interface = Interface::NewValue()); |
54 | 54 |
55 Variable* Lookup(Handle<String> name); | 55 Variable* Lookup(Handle<String> name); |
| 56 |
| 57 Zone* zone() const { return zone_; } |
| 58 |
| 59 private: |
| 60 Zone* zone_; |
56 }; | 61 }; |
57 | 62 |
58 | 63 |
59 // The dynamic scope part holds hash maps for the variables that will | 64 // The dynamic scope part holds hash maps for the variables that will |
60 // be looked up dynamically from within eval and with scopes. The objects | 65 // be looked up dynamically from within eval and with scopes. The objects |
61 // are allocated on-demand from Scope::NonLocal to avoid wasting memory | 66 // are allocated on-demand from Scope::NonLocal to avoid wasting memory |
62 // and setup time for scopes that don't need them. | 67 // and setup time for scopes that don't need them. |
63 class DynamicScopePart : public ZoneObject { | 68 class DynamicScopePart : public ZoneObject { |
64 public: | 69 public: |
| 70 explicit DynamicScopePart(Zone* zone) { |
| 71 for (int i = 0; i < 3; i++) |
| 72 maps_[i] = new(zone->New(sizeof(VariableMap))) VariableMap(zone); |
| 73 } |
| 74 |
65 VariableMap* GetMap(VariableMode mode) { | 75 VariableMap* GetMap(VariableMode mode) { |
66 int index = mode - DYNAMIC; | 76 int index = mode - DYNAMIC; |
67 ASSERT(index >= 0 && index < 3); | 77 ASSERT(index >= 0 && index < 3); |
68 return &maps_[index]; | 78 return maps_[index]; |
69 } | 79 } |
70 | 80 |
71 private: | 81 private: |
72 VariableMap maps_[3]; | 82 VariableMap *maps_[3]; |
73 }; | 83 }; |
74 | 84 |
75 | 85 |
76 // Global invariants after AST construction: Each reference (i.e. identifier) | 86 // Global invariants after AST construction: Each reference (i.e. identifier) |
77 // to a JavaScript variable (including global properties) is represented by a | 87 // to a JavaScript variable (including global properties) is represented by a |
78 // VariableProxy node. Immediately after AST construction and before variable | 88 // VariableProxy node. Immediately after AST construction and before variable |
79 // allocation, most VariableProxy nodes are "unresolved", i.e. not bound to a | 89 // allocation, most VariableProxy nodes are "unresolved", i.e. not bound to a |
80 // corresponding variable (though some are bound during parse time). Variable | 90 // corresponding variable (though some are bound during parse time). Variable |
81 // allocation binds each unresolved VariableProxy to one Variable and assigns | 91 // allocation binds each unresolved VariableProxy to one Variable and assigns |
82 // a location. Note that many VariableProxy nodes may refer to the same Java- | 92 // a location. Note that many VariableProxy nodes may refer to the same Java- |
83 // Script variable. | 93 // Script variable. |
84 | 94 |
85 class Scope: public ZoneObject { | 95 class Scope: public ZoneObject { |
86 public: | 96 public: |
87 // --------------------------------------------------------------------------- | 97 // --------------------------------------------------------------------------- |
88 // Construction | 98 // Construction |
89 | 99 |
90 Scope(Scope* outer_scope, ScopeType type); | 100 Scope(Scope* outer_scope, ScopeType type, Zone* zone); |
91 | 101 |
92 // Compute top scope and allocate variables. For lazy compilation the top | 102 // Compute top scope and allocate variables. For lazy compilation the top |
93 // scope only contains the single lazily compiled function, so this | 103 // scope only contains the single lazily compiled function, so this |
94 // doesn't re-allocate variables repeatedly. | 104 // doesn't re-allocate variables repeatedly. |
95 static bool Analyze(CompilationInfo* info); | 105 static bool Analyze(CompilationInfo* info); |
96 | 106 |
97 static Scope* DeserializeScopeChain(Context* context, Scope* global_scope); | 107 static Scope* DeserializeScopeChain(Context* context, Scope* global_scope, |
| 108 Zone* zone); |
98 | 109 |
99 // The scope name is only used for printing/debugging. | 110 // The scope name is only used for printing/debugging. |
100 void SetScopeName(Handle<String> scope_name) { scope_name_ = scope_name; } | 111 void SetScopeName(Handle<String> scope_name) { scope_name_ = scope_name; } |
101 | 112 |
102 void Initialize(); | 113 void Initialize(); |
103 | 114 |
104 // Checks if the block scope is redundant, i.e. it does not contain any | 115 // Checks if the block scope is redundant, i.e. it does not contain any |
105 // block scoped declarations. In that case it is removed from the scope | 116 // block scoped declarations. In that case it is removed from the scope |
106 // tree and its children are reparented. | 117 // tree and its children are reparented. |
107 Scope* FinalizeBlockScope(); | 118 Scope* FinalizeBlockScope(); |
108 | 119 |
| 120 Zone* zone() const { return zone_; } |
| 121 |
109 // --------------------------------------------------------------------------- | 122 // --------------------------------------------------------------------------- |
110 // Declarations | 123 // Declarations |
111 | 124 |
112 // Lookup a variable in this scope. Returns the variable or NULL if not found. | 125 // Lookup a variable in this scope. Returns the variable or NULL if not found. |
113 Variable* LocalLookup(Handle<String> name); | 126 Variable* LocalLookup(Handle<String> name); |
114 | 127 |
115 // This lookup corresponds to a lookup in the "intermediate" scope sitting | 128 // This lookup corresponds to a lookup in the "intermediate" scope sitting |
116 // between this scope and the outer scope. (ECMA-262, 3rd., requires that | 129 // between this scope and the outer scope. (ECMA-262, 3rd., requires that |
117 // the name of named function literal is kept in an intermediate scope | 130 // the name of named function literal is kept in an intermediate scope |
118 // in between this scope and the next outer scope.) | 131 // in between this scope and the next outer scope.) |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
154 VariableProxy* NewUnresolved(AstNodeFactory<Visitor>* factory, | 167 VariableProxy* NewUnresolved(AstNodeFactory<Visitor>* factory, |
155 Handle<String> name, | 168 Handle<String> name, |
156 int position = RelocInfo::kNoPosition, | 169 int position = RelocInfo::kNoPosition, |
157 Interface* interface = Interface::NewValue()) { | 170 Interface* interface = Interface::NewValue()) { |
158 // Note that we must not share the unresolved variables with | 171 // Note that we must not share the unresolved variables with |
159 // the same name because they may be removed selectively via | 172 // the same name because they may be removed selectively via |
160 // RemoveUnresolved(). | 173 // RemoveUnresolved(). |
161 ASSERT(!already_resolved()); | 174 ASSERT(!already_resolved()); |
162 VariableProxy* proxy = | 175 VariableProxy* proxy = |
163 factory->NewVariableProxy(name, false, position, interface); | 176 factory->NewVariableProxy(name, false, position, interface); |
164 unresolved_.Add(proxy); | 177 unresolved_.Add(proxy, zone_); |
165 return proxy; | 178 return proxy; |
166 } | 179 } |
167 | 180 |
168 // Remove a unresolved variable. During parsing, an unresolved variable | 181 // Remove a unresolved variable. During parsing, an unresolved variable |
169 // may have been added optimistically, but then only the variable name | 182 // may have been added optimistically, but then only the variable name |
170 // was used (typically for labels). If the variable was not declared, the | 183 // was used (typically for labels). If the variable was not declared, the |
171 // addition introduced a new unresolved variable which may end up being | 184 // addition introduced a new unresolved variable which may end up being |
172 // allocated globally as a "ghost" variable. RemoveUnresolved removes | 185 // allocated globally as a "ghost" variable. RemoveUnresolved removes |
173 // such a variable again if it was added; otherwise this is a no-op. | 186 // such a variable again if it was added; otherwise this is a no-op. |
174 void RemoveUnresolved(VariableProxy* var); | 187 void RemoveUnresolved(VariableProxy* var); |
(...skipping 399 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
574 // | 587 // |
575 // In the case of code compiled and run using 'eval', the context | 588 // In the case of code compiled and run using 'eval', the context |
576 // parameter is the context in which eval was called. In all other | 589 // parameter is the context in which eval was called. In all other |
577 // cases the context parameter is an empty handle. | 590 // cases the context parameter is an empty handle. |
578 MUST_USE_RESULT | 591 MUST_USE_RESULT |
579 bool AllocateVariables(CompilationInfo* info, | 592 bool AllocateVariables(CompilationInfo* info, |
580 AstNodeFactory<AstNullVisitor>* factory); | 593 AstNodeFactory<AstNullVisitor>* factory); |
581 | 594 |
582 private: | 595 private: |
583 // Construct a scope based on the scope info. | 596 // Construct a scope based on the scope info. |
584 Scope(Scope* inner_scope, ScopeType type, Handle<ScopeInfo> scope_info); | 597 Scope(Scope* inner_scope, ScopeType type, Handle<ScopeInfo> scope_info, |
| 598 Zone* zone); |
585 | 599 |
586 // Construct a catch scope with a binding for the name. | 600 // Construct a catch scope with a binding for the name. |
587 Scope(Scope* inner_scope, Handle<String> catch_variable_name); | 601 Scope(Scope* inner_scope, Handle<String> catch_variable_name, Zone* zone); |
588 | 602 |
589 void AddInnerScope(Scope* inner_scope) { | 603 void AddInnerScope(Scope* inner_scope) { |
590 if (inner_scope != NULL) { | 604 if (inner_scope != NULL) { |
591 inner_scopes_.Add(inner_scope); | 605 inner_scopes_.Add(inner_scope, zone_); |
592 inner_scope->outer_scope_ = this; | 606 inner_scope->outer_scope_ = this; |
593 } | 607 } |
594 } | 608 } |
595 | 609 |
596 void SetDefaults(ScopeType type, | 610 void SetDefaults(ScopeType type, |
597 Scope* outer_scope, | 611 Scope* outer_scope, |
598 Handle<ScopeInfo> scope_info); | 612 Handle<ScopeInfo> scope_info); |
| 613 |
| 614 Zone* zone_; |
599 }; | 615 }; |
600 | 616 |
601 } } // namespace v8::internal | 617 } } // namespace v8::internal |
602 | 618 |
603 #endif // V8_SCOPES_H_ | 619 #endif // V8_SCOPES_H_ |
OLD | NEW |