karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 1 | // Copyright 2011 the V8 project authors. All rights reserved. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2 | // Redistribution and use in source and binary forms, with or without |
| 3 | // modification, are permitted provided that the following conditions are |
| 4 | // met: |
| 5 | // |
| 6 | // * Redistributions of source code must retain the above copyright |
| 7 | // notice, this list of conditions and the following disclaimer. |
| 8 | // * Redistributions in binary form must reproduce the above |
| 9 | // copyright notice, this list of conditions and the following |
| 10 | // disclaimer in the documentation and/or other materials provided |
| 11 | // with the distribution. |
| 12 | // * Neither the name of Google Inc. nor the names of its |
| 13 | // contributors may be used to endorse or promote products derived |
| 14 | // from this software without specific prior written permission. |
| 15 | // |
| 16 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | |
| 28 | #include "v8.h" |
| 29 | |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 30 | #include "scopes.h" |
| 31 | |
| 32 | #include "bootstrapper.h" |
| 33 | #include "compiler.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 34 | #include "prettyprinter.h" |
| 35 | #include "scopeinfo.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 36 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 37 | namespace v8 { |
| 38 | namespace internal { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 39 | |
| 40 | // ---------------------------------------------------------------------------- |
| 41 | // A Zone allocator for use with LocalsMap. |
| 42 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 43 | // TODO(isolates): It is probably worth it to change the Allocator class to |
| 44 | // take a pointer to an isolate. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 45 | class ZoneAllocator: public Allocator { |
| 46 | public: |
| 47 | /* nothing to do */ |
| 48 | virtual ~ZoneAllocator() {} |
| 49 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 50 | virtual void* New(size_t size) { return ZONE->New(static_cast<int>(size)); } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 51 | |
| 52 | /* ignored - Zone is freed in one fell swoop */ |
| 53 | virtual void Delete(void* p) {} |
| 54 | }; |
| 55 | |
| 56 | |
| 57 | static ZoneAllocator LocalsMapAllocator; |
| 58 | |
| 59 | |
| 60 | // ---------------------------------------------------------------------------- |
| 61 | // Implementation of LocalsMap |
| 62 | // |
| 63 | // Note: We are storing the handle locations as key values in the hash map. |
| 64 | // When inserting a new variable via Declare(), we rely on the fact that |
| 65 | // the handle location remains alive for the duration of that variable |
| 66 | // use. Because a Variable holding a handle with the same location exists |
| 67 | // this is ensured. |
| 68 | |
| 69 | static bool Match(void* key1, void* key2) { |
| 70 | String* name1 = *reinterpret_cast<String**>(key1); |
| 71 | String* name2 = *reinterpret_cast<String**>(key2); |
| 72 | ASSERT(name1->IsSymbol()); |
| 73 | ASSERT(name2->IsSymbol()); |
| 74 | return name1 == name2; |
| 75 | } |
| 76 | |
| 77 | |
| 78 | // Dummy constructor |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 79 | VariableMap::VariableMap(bool gotta_love_static_overloading) : HashMap() {} |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 80 | |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 81 | VariableMap::VariableMap() : HashMap(Match, &LocalsMapAllocator, 8) {} |
| 82 | VariableMap::~VariableMap() {} |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 83 | |
| 84 | |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 85 | Variable* VariableMap::Declare(Scope* scope, |
| 86 | Handle<String> name, |
| 87 | Variable::Mode mode, |
| 88 | bool is_valid_lhs, |
| 89 | Variable::Kind kind) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 90 | HashMap::Entry* p = HashMap::Lookup(name.location(), name->Hash(), true); |
| 91 | if (p->value == NULL) { |
| 92 | // The variable has not been declared yet -> insert it. |
| 93 | ASSERT(p->key == name.location()); |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 94 | p->value = new Variable(scope, name, mode, is_valid_lhs, kind); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 95 | } |
| 96 | return reinterpret_cast<Variable*>(p->value); |
| 97 | } |
| 98 | |
| 99 | |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 100 | Variable* VariableMap::Lookup(Handle<String> name) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 101 | HashMap::Entry* p = HashMap::Lookup(name.location(), name->Hash(), false); |
| 102 | if (p != NULL) { |
| 103 | ASSERT(*reinterpret_cast<String**>(p->key) == *name); |
| 104 | ASSERT(p->value != NULL); |
| 105 | return reinterpret_cast<Variable*>(p->value); |
| 106 | } |
| 107 | return NULL; |
| 108 | } |
| 109 | |
| 110 | |
| 111 | // ---------------------------------------------------------------------------- |
| 112 | // Implementation of Scope |
| 113 | |
| 114 | |
| 115 | // Dummy constructor |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 116 | Scope::Scope(Type type) |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 117 | : inner_scopes_(0), |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 118 | variables_(false), |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 119 | temps_(0), |
| 120 | params_(0), |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 121 | unresolved_(0), |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame^] | 122 | decls_(0), |
| 123 | already_resolved_(false) { |
sgjesse@chromium.org | 3475509 | 2011-04-07 08:41:03 +0000 | [diff] [blame] | 124 | SetDefaults(type, NULL, Handle<SerializedScopeInfo>::null()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | |
| 128 | Scope::Scope(Scope* outer_scope, Type type) |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 129 | : inner_scopes_(4), |
| 130 | variables_(), |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 131 | temps_(4), |
| 132 | params_(4), |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 133 | unresolved_(16), |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame^] | 134 | decls_(4), |
| 135 | already_resolved_(false) { |
sgjesse@chromium.org | 3475509 | 2011-04-07 08:41:03 +0000 | [diff] [blame] | 136 | SetDefaults(type, outer_scope, Handle<SerializedScopeInfo>::null()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 137 | // At some point we might want to provide outer scopes to |
| 138 | // eval scopes (by walking the stack and reading the scope info). |
| 139 | // In that case, the ASSERT below needs to be adjusted. |
| 140 | ASSERT((type == GLOBAL_SCOPE || type == EVAL_SCOPE) == (outer_scope == NULL)); |
| 141 | ASSERT(!HasIllegalRedeclaration()); |
| 142 | } |
| 143 | |
| 144 | |
sgjesse@chromium.org | 3475509 | 2011-04-07 08:41:03 +0000 | [diff] [blame] | 145 | Scope::Scope(Scope* inner_scope, Handle<SerializedScopeInfo> scope_info) |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 146 | : inner_scopes_(4), |
| 147 | variables_(), |
| 148 | temps_(4), |
| 149 | params_(4), |
| 150 | unresolved_(16), |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame^] | 151 | decls_(4), |
| 152 | already_resolved_(true) { |
sgjesse@chromium.org | 3475509 | 2011-04-07 08:41:03 +0000 | [diff] [blame] | 153 | ASSERT(!scope_info.is_null()); |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 154 | SetDefaults(FUNCTION_SCOPE, NULL, scope_info); |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 155 | if (scope_info->HasHeapAllocatedLocals()) { |
| 156 | num_heap_slots_ = scope_info_->NumberOfContextSlots(); |
| 157 | } |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 158 | AddInnerScope(inner_scope); |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame^] | 162 | Scope::Scope(Scope* inner_scope, Handle<String> catch_variable_name) |
| 163 | : inner_scopes_(1), |
| 164 | variables_(), |
| 165 | temps_(0), |
| 166 | params_(0), |
| 167 | unresolved_(0), |
| 168 | decls_(0), |
| 169 | already_resolved_(true) { |
| 170 | SetDefaults(CATCH_SCOPE, NULL, Handle<SerializedScopeInfo>::null()); |
| 171 | AddInnerScope(inner_scope); |
| 172 | ++num_var_or_const_; |
| 173 | Variable* variable = variables_.Declare(this, |
| 174 | catch_variable_name, |
| 175 | Variable::VAR, |
| 176 | true, // Valid left-hand side. |
| 177 | Variable::NORMAL); |
| 178 | AllocateHeapSlot(variable); |
| 179 | } |
| 180 | |
| 181 | |
sgjesse@chromium.org | 3475509 | 2011-04-07 08:41:03 +0000 | [diff] [blame] | 182 | void Scope::SetDefaults(Type type, |
| 183 | Scope* outer_scope, |
| 184 | Handle<SerializedScopeInfo> scope_info) { |
| 185 | outer_scope_ = outer_scope; |
| 186 | type_ = type; |
| 187 | scope_name_ = FACTORY->empty_symbol(); |
| 188 | dynamics_ = NULL; |
| 189 | receiver_ = NULL; |
| 190 | function_ = NULL; |
| 191 | arguments_ = NULL; |
sgjesse@chromium.org | 3475509 | 2011-04-07 08:41:03 +0000 | [diff] [blame] | 192 | illegal_redecl_ = NULL; |
| 193 | scope_inside_with_ = false; |
| 194 | scope_contains_with_ = false; |
| 195 | scope_calls_eval_ = false; |
| 196 | // Inherit the strict mode from the parent scope. |
| 197 | strict_mode_ = (outer_scope != NULL) && outer_scope->strict_mode_; |
| 198 | outer_scope_calls_eval_ = false; |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 199 | outer_scope_calls_non_strict_eval_ = false; |
sgjesse@chromium.org | 3475509 | 2011-04-07 08:41:03 +0000 | [diff] [blame] | 200 | inner_scope_calls_eval_ = false; |
| 201 | outer_scope_is_eval_scope_ = false; |
| 202 | force_eager_compilation_ = false; |
vegorov@chromium.org | 7304bca | 2011-05-16 12:14:13 +0000 | [diff] [blame] | 203 | num_var_or_const_ = 0; |
sgjesse@chromium.org | 3475509 | 2011-04-07 08:41:03 +0000 | [diff] [blame] | 204 | num_stack_slots_ = 0; |
| 205 | num_heap_slots_ = 0; |
| 206 | scope_info_ = scope_info; |
| 207 | } |
| 208 | |
| 209 | |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 210 | Scope* Scope::DeserializeScopeChain(CompilationInfo* info, |
| 211 | Scope* global_scope) { |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame^] | 212 | // Reconstruct the outer scope chain from a closure's context chain. |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 213 | ASSERT(!info->closure().is_null()); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame^] | 214 | Context* context = info->closure()->context(); |
| 215 | Scope* current_scope = NULL; |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 216 | Scope* innermost_scope = NULL; |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame^] | 217 | bool contains_with = false; |
| 218 | while (!context->IsGlobalContext()) { |
| 219 | if (context->IsWithContext()) { |
| 220 | // All the inner scopes are inside a with. |
| 221 | contains_with = true; |
| 222 | for (Scope* s = innermost_scope; s != NULL; s = s->outer_scope()) { |
| 223 | s->scope_inside_with_ = true; |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 224 | } |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame^] | 225 | } else { |
| 226 | if (context->IsFunctionContext()) { |
| 227 | SerializedScopeInfo* scope_info = |
| 228 | context->closure()->shared()->scope_info(); |
| 229 | current_scope = |
| 230 | new Scope(current_scope, Handle<SerializedScopeInfo>(scope_info)); |
| 231 | } else { |
| 232 | ASSERT(context->IsCatchContext()); |
| 233 | String* name = String::cast(context->extension()); |
| 234 | current_scope = new Scope(current_scope, Handle<String>(name)); |
| 235 | } |
| 236 | if (contains_with) current_scope->RecordWithStatement(); |
| 237 | if (innermost_scope == NULL) innermost_scope = current_scope; |
| 238 | } |
| 239 | |
| 240 | // Forget about a with when we move to a context for a different function. |
| 241 | if (context->previous()->closure() != context->closure()) { |
| 242 | contains_with = false; |
| 243 | } |
| 244 | context = context->previous(); |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 245 | } |
| 246 | |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame^] | 247 | global_scope->AddInnerScope(current_scope); |
| 248 | return (innermost_scope == NULL) ? global_scope : innermost_scope; |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 249 | } |
| 250 | |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 251 | |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 252 | bool Scope::Analyze(CompilationInfo* info) { |
| 253 | ASSERT(info->function() != NULL); |
| 254 | Scope* top = info->function()->scope(); |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 255 | |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 256 | while (top->outer_scope() != NULL) top = top->outer_scope(); |
| 257 | top->AllocateVariables(info->calling_context()); |
| 258 | |
| 259 | #ifdef DEBUG |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 260 | if (info->isolate()->bootstrapper()->IsActive() |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 261 | ? FLAG_print_builtin_scopes |
| 262 | : FLAG_print_scopes) { |
| 263 | info->function()->scope()->Print(); |
| 264 | } |
| 265 | #endif |
| 266 | |
| 267 | info->SetScope(info->function()->scope()); |
| 268 | return true; // Can not fail. |
| 269 | } |
| 270 | |
| 271 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 272 | void Scope::Initialize(bool inside_with) { |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame^] | 273 | ASSERT(!already_resolved()); |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 274 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 275 | // Add this scope as a new inner scope of the outer scope. |
| 276 | if (outer_scope_ != NULL) { |
| 277 | outer_scope_->inner_scopes_.Add(this); |
| 278 | scope_inside_with_ = outer_scope_->scope_inside_with_ || inside_with; |
| 279 | } else { |
| 280 | scope_inside_with_ = inside_with; |
| 281 | } |
| 282 | |
| 283 | // Declare convenience variables. |
| 284 | // Declare and allocate receiver (even for the global scope, and even |
| 285 | // if naccesses_ == 0). |
| 286 | // NOTE: When loading parameters in the global scope, we must take |
| 287 | // care not to access them as properties of the global object, but |
| 288 | // instead load them directly from the stack. Currently, the only |
| 289 | // such parameter is 'this' which is passed on the stack when |
| 290 | // invoking scripts |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame^] | 291 | if (is_catch_scope()) { |
| 292 | ASSERT(outer_scope() != NULL); |
| 293 | receiver_ = outer_scope()->receiver(); |
| 294 | } else { |
| 295 | Variable* var = |
| 296 | variables_.Declare(this, FACTORY->this_symbol(), Variable::VAR, |
| 297 | false, Variable::THIS); |
| 298 | var->set_rewrite(new Slot(var, Slot::PARAMETER, -1)); |
| 299 | receiver_ = var; |
| 300 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 301 | |
| 302 | if (is_function_scope()) { |
| 303 | // Declare 'arguments' variable which exists in all functions. |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 304 | // Note that it might never be accessed, in which case it won't be |
| 305 | // allocated during variable allocation. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 306 | variables_.Declare(this, FACTORY->arguments_symbol(), Variable::VAR, |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 307 | true, Variable::ARGUMENTS); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 308 | } |
| 309 | } |
| 310 | |
| 311 | |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 312 | Variable* Scope::LocalLookup(Handle<String> name) { |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 313 | Variable* result = variables_.Lookup(name); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame^] | 314 | if (result != NULL || scope_info_.is_null()) { |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 315 | return result; |
| 316 | } |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame^] | 317 | // If we have a serialized scope info, we might find the variable there. |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 318 | // |
| 319 | // We should never lookup 'arguments' in this scope as it is implicitly |
| 320 | // present in every scope. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 321 | ASSERT(*name != *FACTORY->arguments_symbol()); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 322 | // There should be no local slot with the given name. |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 323 | ASSERT(scope_info_->StackSlotIndex(*name) < 0); |
| 324 | |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 325 | // Check context slot lookup. |
| 326 | Variable::Mode mode; |
| 327 | int index = scope_info_->ContextSlotIndex(*name, &mode); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 328 | if (index < 0) { |
| 329 | // Check parameters. |
| 330 | mode = Variable::VAR; |
| 331 | index = scope_info_->ParameterIndex(*name); |
| 332 | if (index < 0) { |
| 333 | // Check the function name. |
| 334 | index = scope_info_->FunctionContextSlotIndex(*name); |
| 335 | if (index < 0) return NULL; |
| 336 | } |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 337 | } |
| 338 | |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 339 | Variable* var = |
| 340 | variables_.Declare(this, name, mode, true, Variable::NORMAL); |
| 341 | var->set_rewrite(new Slot(var, Slot::CONTEXT, index)); |
| 342 | return var; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 343 | } |
| 344 | |
| 345 | |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 346 | Variable* Scope::Lookup(Handle<String> name) { |
| 347 | for (Scope* scope = this; |
| 348 | scope != NULL; |
| 349 | scope = scope->outer_scope()) { |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 350 | Variable* var = scope->LocalLookup(name); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 351 | if (var != NULL) return var; |
| 352 | } |
| 353 | return NULL; |
| 354 | } |
| 355 | |
| 356 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 357 | Variable* Scope::DeclareFunctionVar(Handle<String> name) { |
| 358 | ASSERT(is_function_scope() && function_ == NULL); |
ager@chromium.org | 3e87580 | 2009-06-29 08:26:34 +0000 | [diff] [blame] | 359 | function_ = new Variable(this, name, Variable::CONST, true, Variable::NORMAL); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 360 | return function_; |
| 361 | } |
| 362 | |
| 363 | |
ricow@chromium.org | c54d365 | 2011-05-30 09:20:16 +0000 | [diff] [blame] | 364 | void Scope::DeclareParameter(Handle<String> name) { |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame^] | 365 | ASSERT(!already_resolved()); |
ricow@chromium.org | c54d365 | 2011-05-30 09:20:16 +0000 | [diff] [blame] | 366 | ASSERT(is_function_scope()); |
| 367 | Variable* var = |
| 368 | variables_.Declare(this, name, Variable::VAR, true, Variable::NORMAL); |
| 369 | params_.Add(var); |
| 370 | } |
| 371 | |
| 372 | |
| 373 | Variable* Scope::DeclareLocal(Handle<String> name, Variable::Mode mode) { |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame^] | 374 | ASSERT(!already_resolved()); |
ricow@chromium.org | c54d365 | 2011-05-30 09:20:16 +0000 | [diff] [blame] | 375 | // This function handles VAR and CONST modes. DYNAMIC variables are |
| 376 | // introduces during variable allocation, INTERNAL variables are allocated |
| 377 | // explicitly, and TEMPORARY variables are allocated via NewTemporary(). |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 378 | ASSERT(mode == Variable::VAR || mode == Variable::CONST); |
ricow@chromium.org | c54d365 | 2011-05-30 09:20:16 +0000 | [diff] [blame] | 379 | ++num_var_or_const_; |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 380 | return variables_.Declare(this, name, mode, true, Variable::NORMAL); |
| 381 | } |
| 382 | |
| 383 | |
| 384 | Variable* Scope::DeclareGlobal(Handle<String> name) { |
| 385 | ASSERT(is_global_scope()); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 386 | return variables_.Declare(this, name, Variable::DYNAMIC_GLOBAL, true, |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 387 | Variable::NORMAL); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 391 | VariableProxy* Scope::NewUnresolved(Handle<String> name, |
| 392 | bool inside_with, |
| 393 | int position) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 394 | // Note that we must not share the unresolved variables with |
| 395 | // the same name because they may be removed selectively via |
| 396 | // RemoveUnresolved(). |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame^] | 397 | ASSERT(!already_resolved()); |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 398 | VariableProxy* proxy = new VariableProxy(name, false, inside_with, position); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 399 | unresolved_.Add(proxy); |
| 400 | return proxy; |
| 401 | } |
| 402 | |
| 403 | |
| 404 | void Scope::RemoveUnresolved(VariableProxy* var) { |
| 405 | // Most likely (always?) any variable we want to remove |
| 406 | // was just added before, so we search backwards. |
| 407 | for (int i = unresolved_.length(); i-- > 0;) { |
| 408 | if (unresolved_[i] == var) { |
| 409 | unresolved_.Remove(i); |
| 410 | return; |
| 411 | } |
| 412 | } |
| 413 | } |
| 414 | |
| 415 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 416 | Variable* Scope::NewTemporary(Handle<String> name) { |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame^] | 417 | ASSERT(!already_resolved()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 418 | Variable* var = |
| 419 | new Variable(this, name, Variable::TEMPORARY, true, Variable::NORMAL); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 420 | temps_.Add(var); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 421 | return var; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | |
| 425 | void Scope::AddDeclaration(Declaration* declaration) { |
| 426 | decls_.Add(declaration); |
| 427 | } |
| 428 | |
| 429 | |
| 430 | void Scope::SetIllegalRedeclaration(Expression* expression) { |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 431 | // Record only the first illegal redeclaration. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 432 | if (!HasIllegalRedeclaration()) { |
| 433 | illegal_redecl_ = expression; |
| 434 | } |
| 435 | ASSERT(HasIllegalRedeclaration()); |
| 436 | } |
| 437 | |
| 438 | |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 439 | void Scope::VisitIllegalRedeclaration(AstVisitor* visitor) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 440 | ASSERT(HasIllegalRedeclaration()); |
| 441 | illegal_redecl_->Accept(visitor); |
| 442 | } |
| 443 | |
| 444 | |
| 445 | template<class Allocator> |
| 446 | void Scope::CollectUsedVariables(List<Variable*, Allocator>* locals) { |
| 447 | // Collect variables in this scope. |
| 448 | // Note that the function_ variable - if present - is not |
| 449 | // collected here but handled separately in ScopeInfo |
| 450 | // which is the current user of this function). |
| 451 | for (int i = 0; i < temps_.length(); i++) { |
| 452 | Variable* var = temps_[i]; |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 453 | if (var->is_used()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 454 | locals->Add(var); |
| 455 | } |
| 456 | } |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 457 | for (VariableMap::Entry* p = variables_.Start(); |
| 458 | p != NULL; |
| 459 | p = variables_.Next(p)) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 460 | Variable* var = reinterpret_cast<Variable*>(p->value); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 461 | if (var->is_used()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 462 | locals->Add(var); |
| 463 | } |
| 464 | } |
| 465 | } |
| 466 | |
| 467 | |
| 468 | // Make sure the method gets instantiated by the template system. |
| 469 | template void Scope::CollectUsedVariables( |
| 470 | List<Variable*, FreeStoreAllocationPolicy>* locals); |
| 471 | template void Scope::CollectUsedVariables( |
| 472 | List<Variable*, PreallocatedStorage>* locals); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 473 | template void Scope::CollectUsedVariables( |
| 474 | List<Variable*, ZoneListAllocationPolicy>* locals); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 475 | |
| 476 | |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 477 | void Scope::AllocateVariables(Handle<Context> context) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 478 | ASSERT(outer_scope_ == NULL); // eval or global scopes only |
| 479 | |
| 480 | // 1) Propagate scope information. |
| 481 | // If we are in an eval scope, we may have other outer scopes about |
| 482 | // which we don't know anything at this point. Thus we must be conservative |
| 483 | // and assume they may invoke eval themselves. Eventually we could capture |
| 484 | // this information in the ScopeInfo and then use it here (by traversing |
| 485 | // the call chain stack, at compile time). |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 486 | |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 487 | bool eval_scope = is_eval_scope(); |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 488 | bool outer_scope_calls_eval = false; |
| 489 | bool outer_scope_calls_non_strict_eval = false; |
| 490 | if (!is_global_scope()) { |
| 491 | context->ComputeEvalScopeInfo(&outer_scope_calls_eval, |
| 492 | &outer_scope_calls_non_strict_eval); |
| 493 | } |
| 494 | PropagateScopeInfo(outer_scope_calls_eval, |
| 495 | outer_scope_calls_non_strict_eval, |
| 496 | eval_scope); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 497 | |
| 498 | // 2) Resolve variables. |
| 499 | Scope* global_scope = NULL; |
| 500 | if (is_global_scope()) global_scope = this; |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 501 | ResolveVariablesRecursively(global_scope, context); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 502 | |
| 503 | // 3) Allocate variables. |
| 504 | AllocateVariablesRecursively(); |
| 505 | } |
| 506 | |
| 507 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 508 | bool Scope::AllowsLazyCompilation() const { |
| 509 | return !force_eager_compilation_ && HasTrivialOuterContext(); |
| 510 | } |
| 511 | |
| 512 | |
| 513 | bool Scope::HasTrivialContext() const { |
| 514 | // A function scope has a trivial context if it always is the global |
| 515 | // context. We iteratively scan out the context chain to see if |
| 516 | // there is anything that makes this scope non-trivial; otherwise we |
| 517 | // return true. |
| 518 | for (const Scope* scope = this; scope != NULL; scope = scope->outer_scope_) { |
| 519 | if (scope->is_eval_scope()) return false; |
| 520 | if (scope->scope_inside_with_) return false; |
| 521 | if (scope->num_heap_slots_ > 0) return false; |
| 522 | } |
| 523 | return true; |
| 524 | } |
| 525 | |
| 526 | |
| 527 | bool Scope::HasTrivialOuterContext() const { |
| 528 | Scope* outer = outer_scope_; |
| 529 | if (outer == NULL) return true; |
| 530 | // Note that the outer context may be trivial in general, but the current |
| 531 | // scope may be inside a 'with' statement in which case the outer context |
| 532 | // for this scope is not trivial. |
| 533 | return !scope_inside_with_ && outer->HasTrivialContext(); |
| 534 | } |
| 535 | |
| 536 | |
| 537 | int Scope::ContextChainLength(Scope* scope) { |
| 538 | int n = 0; |
| 539 | for (Scope* s = this; s != scope; s = s->outer_scope_) { |
| 540 | ASSERT(s != NULL); // scope must be in the scope chain |
| 541 | if (s->num_heap_slots() > 0) n++; |
| 542 | } |
| 543 | return n; |
| 544 | } |
| 545 | |
| 546 | |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame^] | 547 | Scope* Scope::DeclarationScope() { |
| 548 | Scope* scope = this; |
| 549 | while (scope->is_catch_scope()) { |
| 550 | scope = scope->outer_scope(); |
| 551 | } |
| 552 | return scope; |
| 553 | } |
| 554 | |
| 555 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 556 | #ifdef DEBUG |
| 557 | static const char* Header(Scope::Type type) { |
| 558 | switch (type) { |
| 559 | case Scope::EVAL_SCOPE: return "eval"; |
| 560 | case Scope::FUNCTION_SCOPE: return "function"; |
| 561 | case Scope::GLOBAL_SCOPE: return "global"; |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame^] | 562 | case Scope::CATCH_SCOPE: return "catch"; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 563 | } |
| 564 | UNREACHABLE(); |
| 565 | return NULL; |
| 566 | } |
| 567 | |
| 568 | |
| 569 | static void Indent(int n, const char* str) { |
| 570 | PrintF("%*s%s", n, "", str); |
| 571 | } |
| 572 | |
| 573 | |
| 574 | static void PrintName(Handle<String> name) { |
| 575 | SmartPointer<char> s = name->ToCString(DISALLOW_NULLS); |
| 576 | PrintF("%s", *s); |
| 577 | } |
| 578 | |
| 579 | |
| 580 | static void PrintVar(PrettyPrinter* printer, int indent, Variable* var) { |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 581 | if (var->is_used() || var->rewrite() != NULL) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 582 | Indent(indent, Variable::Mode2String(var->mode())); |
| 583 | PrintF(" "); |
| 584 | PrintName(var->name()); |
| 585 | PrintF("; // "); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 586 | if (var->rewrite() != NULL) { |
| 587 | PrintF("%s, ", printer->Print(var->rewrite())); |
| 588 | if (var->is_accessed_from_inner_scope()) PrintF(", "); |
| 589 | } |
| 590 | if (var->is_accessed_from_inner_scope()) PrintF("inner scope access"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 591 | PrintF("\n"); |
| 592 | } |
| 593 | } |
| 594 | |
| 595 | |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 596 | static void PrintMap(PrettyPrinter* printer, int indent, VariableMap* map) { |
| 597 | for (VariableMap::Entry* p = map->Start(); p != NULL; p = map->Next(p)) { |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 598 | Variable* var = reinterpret_cast<Variable*>(p->value); |
| 599 | PrintVar(printer, indent, var); |
| 600 | } |
| 601 | } |
| 602 | |
| 603 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 604 | void Scope::Print(int n) { |
| 605 | int n0 = (n > 0 ? n : 0); |
| 606 | int n1 = n0 + 2; // indentation |
| 607 | |
| 608 | // Print header. |
| 609 | Indent(n0, Header(type_)); |
| 610 | if (scope_name_->length() > 0) { |
| 611 | PrintF(" "); |
| 612 | PrintName(scope_name_); |
| 613 | } |
| 614 | |
| 615 | // Print parameters, if any. |
| 616 | if (is_function_scope()) { |
| 617 | PrintF(" ("); |
| 618 | for (int i = 0; i < params_.length(); i++) { |
| 619 | if (i > 0) PrintF(", "); |
| 620 | PrintName(params_[i]->name()); |
| 621 | } |
| 622 | PrintF(")"); |
| 623 | } |
| 624 | |
| 625 | PrintF(" {\n"); |
| 626 | |
| 627 | // Function name, if any (named function literals, only). |
| 628 | if (function_ != NULL) { |
| 629 | Indent(n1, "// (local) function name: "); |
| 630 | PrintName(function_->name()); |
| 631 | PrintF("\n"); |
| 632 | } |
| 633 | |
| 634 | // Scope info. |
| 635 | if (HasTrivialOuterContext()) { |
| 636 | Indent(n1, "// scope has trivial outer context\n"); |
| 637 | } |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 638 | if (is_strict_mode()) Indent(n1, "// strict mode scope\n"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 639 | if (scope_inside_with_) Indent(n1, "// scope inside 'with'\n"); |
| 640 | if (scope_contains_with_) Indent(n1, "// scope contains 'with'\n"); |
| 641 | if (scope_calls_eval_) Indent(n1, "// scope calls 'eval'\n"); |
| 642 | if (outer_scope_calls_eval_) Indent(n1, "// outer scope calls 'eval'\n"); |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 643 | if (outer_scope_calls_non_strict_eval_) { |
| 644 | Indent(n1, "// outer scope calls 'eval' in non-strict context\n"); |
| 645 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 646 | if (inner_scope_calls_eval_) Indent(n1, "// inner scope calls 'eval'\n"); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 647 | if (outer_scope_is_eval_scope_) { |
| 648 | Indent(n1, "// outer scope is 'eval' scope\n"); |
| 649 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 650 | if (num_stack_slots_ > 0) { Indent(n1, "// "); |
| 651 | PrintF("%d stack slots\n", num_stack_slots_); } |
| 652 | if (num_heap_slots_ > 0) { Indent(n1, "// "); |
| 653 | PrintF("%d heap slots\n", num_heap_slots_); } |
| 654 | |
| 655 | // Print locals. |
| 656 | PrettyPrinter printer; |
| 657 | Indent(n1, "// function var\n"); |
| 658 | if (function_ != NULL) { |
| 659 | PrintVar(&printer, n1, function_); |
| 660 | } |
| 661 | |
| 662 | Indent(n1, "// temporary vars\n"); |
| 663 | for (int i = 0; i < temps_.length(); i++) { |
| 664 | PrintVar(&printer, n1, temps_[i]); |
| 665 | } |
| 666 | |
| 667 | Indent(n1, "// local vars\n"); |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 668 | PrintMap(&printer, n1, &variables_); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 669 | |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 670 | Indent(n1, "// dynamic vars\n"); |
| 671 | if (dynamics_ != NULL) { |
| 672 | PrintMap(&printer, n1, dynamics_->GetMap(Variable::DYNAMIC)); |
| 673 | PrintMap(&printer, n1, dynamics_->GetMap(Variable::DYNAMIC_LOCAL)); |
| 674 | PrintMap(&printer, n1, dynamics_->GetMap(Variable::DYNAMIC_GLOBAL)); |
| 675 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 676 | |
| 677 | // Print inner scopes (disable by providing negative n). |
| 678 | if (n >= 0) { |
| 679 | for (int i = 0; i < inner_scopes_.length(); i++) { |
| 680 | PrintF("\n"); |
| 681 | inner_scopes_[i]->Print(n1); |
| 682 | } |
| 683 | } |
| 684 | |
| 685 | Indent(n0, "}\n"); |
| 686 | } |
| 687 | #endif // DEBUG |
| 688 | |
| 689 | |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 690 | Variable* Scope::NonLocal(Handle<String> name, Variable::Mode mode) { |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 691 | if (dynamics_ == NULL) dynamics_ = new DynamicScopePart(); |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 692 | VariableMap* map = dynamics_->GetMap(mode); |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 693 | Variable* var = map->Lookup(name); |
| 694 | if (var == NULL) { |
| 695 | // Declare a new non-local. |
ager@chromium.org | 3e87580 | 2009-06-29 08:26:34 +0000 | [diff] [blame] | 696 | var = map->Declare(NULL, name, mode, true, Variable::NORMAL); |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 697 | // Allocate it by giving it a dynamic lookup. |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 698 | var->set_rewrite(new Slot(var, Slot::LOOKUP, -1)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 699 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 700 | return var; |
| 701 | } |
| 702 | |
| 703 | |
| 704 | // Lookup a variable starting with this scope. The result is either |
ager@chromium.org | fe22fc4 | 2009-11-05 11:55:54 +0000 | [diff] [blame] | 705 | // the statically resolved variable belonging to an outer scope, or |
| 706 | // NULL. It may be NULL because a) we couldn't find a variable, or b) |
| 707 | // because the variable is just a guess (and may be shadowed by |
| 708 | // another variable that is introduced dynamically via an 'eval' call |
| 709 | // or a 'with' statement). |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 710 | Variable* Scope::LookupRecursive(Handle<String> name, |
| 711 | bool inner_lookup, |
| 712 | Variable** invalidated_local) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 713 | // If we find a variable, but the current scope calls 'eval', the found |
| 714 | // variable may not be the correct one (the 'eval' may introduce a |
| 715 | // property with the same name). In that case, remember that the variable |
| 716 | // found is just a guess. |
| 717 | bool guess = scope_calls_eval_; |
| 718 | |
| 719 | // Try to find the variable in this scope. |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 720 | Variable* var = LocalLookup(name); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 721 | |
| 722 | if (var != NULL) { |
| 723 | // We found a variable. If this is not an inner lookup, we are done. |
| 724 | // (Even if there is an 'eval' in this scope which introduces the |
| 725 | // same variable again, the resulting variable remains the same. |
| 726 | // Note that enclosing 'with' statements are handled at the call site.) |
| 727 | if (!inner_lookup) |
| 728 | return var; |
| 729 | |
| 730 | } else { |
| 731 | // We did not find a variable locally. Check against the function variable, |
| 732 | // if any. We can do this for all scopes, since the function variable is |
| 733 | // only present - if at all - for function scopes. |
| 734 | // |
| 735 | // This lookup corresponds to a lookup in the "intermediate" scope sitting |
| 736 | // between this scope and the outer scope. (ECMA-262, 3rd., requires that |
| 737 | // the name of named function literal is kept in an intermediate scope |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 738 | // in between this scope and the next outer scope.) |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 739 | if (function_ != NULL && function_->name().is_identical_to(name)) { |
| 740 | var = function_; |
| 741 | |
| 742 | } else if (outer_scope_ != NULL) { |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 743 | var = outer_scope_->LookupRecursive(name, true, invalidated_local); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 744 | // We may have found a variable in an outer scope. However, if |
| 745 | // the current scope is inside a 'with', the actual variable may |
| 746 | // be a property introduced via the 'with' statement. Then, the |
| 747 | // variable we may have found is just a guess. |
| 748 | if (scope_inside_with_) |
| 749 | guess = true; |
| 750 | } |
| 751 | |
| 752 | // If we did not find a variable, we are done. |
| 753 | if (var == NULL) |
| 754 | return NULL; |
| 755 | } |
| 756 | |
| 757 | ASSERT(var != NULL); |
| 758 | |
| 759 | // If this is a lookup from an inner scope, mark the variable. |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 760 | if (inner_lookup) { |
| 761 | var->MarkAsAccessedFromInnerScope(); |
| 762 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 763 | |
ager@chromium.org | fe22fc4 | 2009-11-05 11:55:54 +0000 | [diff] [blame] | 764 | // If the variable we have found is just a guess, invalidate the |
| 765 | // result. If the found variable is local, record that fact so we |
| 766 | // can generate fast code to get it if it is not shadowed by eval. |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 767 | if (guess) { |
ager@chromium.org | fe22fc4 | 2009-11-05 11:55:54 +0000 | [diff] [blame] | 768 | if (!var->is_global()) *invalidated_local = var; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 769 | var = NULL; |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 770 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 771 | |
| 772 | return var; |
| 773 | } |
| 774 | |
| 775 | |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 776 | void Scope::ResolveVariable(Scope* global_scope, |
| 777 | Handle<Context> context, |
| 778 | VariableProxy* proxy) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 779 | ASSERT(global_scope == NULL || global_scope->is_global_scope()); |
| 780 | |
| 781 | // If the proxy is already resolved there's nothing to do |
| 782 | // (functions and consts may be resolved by the parser). |
| 783 | if (proxy->var() != NULL) return; |
| 784 | |
| 785 | // Otherwise, try to resolve the variable. |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 786 | Variable* invalidated_local = NULL; |
| 787 | Variable* var = LookupRecursive(proxy->name(), false, &invalidated_local); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 788 | |
| 789 | if (proxy->inside_with()) { |
| 790 | // If we are inside a local 'with' statement, all bets are off |
| 791 | // and we cannot resolve the proxy to a local variable even if |
| 792 | // we found an outer matching variable. |
| 793 | // Note that we must do a lookup anyway, because if we find one, |
| 794 | // we must mark that variable as potentially accessed from this |
| 795 | // inner scope (the property may not be in the 'with' object). |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 796 | var = NonLocal(proxy->name(), Variable::DYNAMIC); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 797 | |
| 798 | } else { |
| 799 | // We are not inside a local 'with' statement. |
| 800 | |
| 801 | if (var == NULL) { |
| 802 | // We did not find the variable. We have a global variable |
| 803 | // if we are in the global scope (we know already that we |
| 804 | // are outside a 'with' statement) or if there is no way |
| 805 | // that the variable might be introduced dynamically (through |
| 806 | // a local or outer eval() call, or an outer 'with' statement), |
| 807 | // or we don't know about the outer scope (because we are |
| 808 | // in an eval scope). |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 809 | if (is_global_scope() || |
| 810 | !(scope_inside_with_ || outer_scope_is_eval_scope_ || |
| 811 | scope_calls_eval_ || outer_scope_calls_eval_)) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 812 | // We must have a global variable. |
| 813 | ASSERT(global_scope != NULL); |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 814 | var = global_scope->DeclareGlobal(proxy->name()); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 815 | |
| 816 | } else if (scope_inside_with_) { |
| 817 | // If we are inside a with statement we give up and look up |
| 818 | // the variable at runtime. |
| 819 | var = NonLocal(proxy->name(), Variable::DYNAMIC); |
| 820 | |
| 821 | } else if (invalidated_local != NULL) { |
| 822 | // No with statements are involved and we found a local |
| 823 | // variable that might be shadowed by eval introduced |
| 824 | // variables. |
| 825 | var = NonLocal(proxy->name(), Variable::DYNAMIC_LOCAL); |
| 826 | var->set_local_if_not_shadowed(invalidated_local); |
| 827 | |
| 828 | } else if (outer_scope_is_eval_scope_) { |
| 829 | // No with statements and we did not find a local and the code |
| 830 | // is executed with a call to eval. The context contains |
| 831 | // scope information that we can use to determine if the |
| 832 | // variable is global if it is not shadowed by eval-introduced |
| 833 | // variables. |
| 834 | if (context->GlobalIfNotShadowedByEval(proxy->name())) { |
| 835 | var = NonLocal(proxy->name(), Variable::DYNAMIC_GLOBAL); |
| 836 | |
| 837 | } else { |
| 838 | var = NonLocal(proxy->name(), Variable::DYNAMIC); |
| 839 | } |
| 840 | |
| 841 | } else { |
| 842 | // No with statements and we did not find a local and the code |
| 843 | // is not executed with a call to eval. We know that this |
| 844 | // variable is global unless it is shadowed by eval-introduced |
| 845 | // variables. |
| 846 | var = NonLocal(proxy->name(), Variable::DYNAMIC_GLOBAL); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 847 | } |
| 848 | } |
| 849 | } |
| 850 | |
| 851 | proxy->BindTo(var); |
| 852 | } |
| 853 | |
| 854 | |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 855 | void Scope::ResolveVariablesRecursively(Scope* global_scope, |
| 856 | Handle<Context> context) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 857 | ASSERT(global_scope == NULL || global_scope->is_global_scope()); |
| 858 | |
| 859 | // Resolve unresolved variables for this scope. |
| 860 | for (int i = 0; i < unresolved_.length(); i++) { |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 861 | ResolveVariable(global_scope, context, unresolved_[i]); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 862 | } |
| 863 | |
| 864 | // Resolve unresolved variables for inner scopes. |
| 865 | for (int i = 0; i < inner_scopes_.length(); i++) { |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 866 | inner_scopes_[i]->ResolveVariablesRecursively(global_scope, context); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 867 | } |
| 868 | } |
| 869 | |
| 870 | |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 871 | bool Scope::PropagateScopeInfo(bool outer_scope_calls_eval, |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 872 | bool outer_scope_calls_non_strict_eval, |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 873 | bool outer_scope_is_eval_scope) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 874 | if (outer_scope_calls_eval) { |
| 875 | outer_scope_calls_eval_ = true; |
| 876 | } |
| 877 | |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 878 | if (outer_scope_calls_non_strict_eval) { |
| 879 | outer_scope_calls_non_strict_eval_ = true; |
| 880 | } |
| 881 | |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 882 | if (outer_scope_is_eval_scope) { |
| 883 | outer_scope_is_eval_scope_ = true; |
| 884 | } |
| 885 | |
| 886 | bool calls_eval = scope_calls_eval_ || outer_scope_calls_eval_; |
| 887 | bool is_eval = is_eval_scope() || outer_scope_is_eval_scope_; |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 888 | bool calls_non_strict_eval = |
| 889 | (scope_calls_eval_ && !is_strict_mode()) || |
| 890 | outer_scope_calls_non_strict_eval_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 891 | for (int i = 0; i < inner_scopes_.length(); i++) { |
| 892 | Scope* inner_scope = inner_scopes_[i]; |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 893 | if (inner_scope->PropagateScopeInfo(calls_eval, |
| 894 | calls_non_strict_eval, |
| 895 | is_eval)) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 896 | inner_scope_calls_eval_ = true; |
| 897 | } |
| 898 | if (inner_scope->force_eager_compilation_) { |
| 899 | force_eager_compilation_ = true; |
| 900 | } |
| 901 | } |
| 902 | |
| 903 | return scope_calls_eval_ || inner_scope_calls_eval_; |
| 904 | } |
| 905 | |
| 906 | |
| 907 | bool Scope::MustAllocate(Variable* var) { |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 908 | // Give var a read/write use if there is a chance it might be accessed |
| 909 | // via an eval() call. This is only possible if the variable has a |
| 910 | // visible name. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 911 | if ((var->is_this() || var->name()->length() > 0) && |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 912 | (var->is_accessed_from_inner_scope() || |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame^] | 913 | scope_calls_eval_ || |
| 914 | inner_scope_calls_eval_ || |
| 915 | scope_contains_with_ || |
| 916 | is_catch_scope())) { |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 917 | var->set_is_used(true); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 918 | } |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 919 | // Global variables do not need to be allocated. |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 920 | return !var->is_global() && var->is_used(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 921 | } |
| 922 | |
| 923 | |
| 924 | bool Scope::MustAllocateInContext(Variable* var) { |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame^] | 925 | // If var is accessed from an inner scope, or if there is a possibility |
| 926 | // that it might be accessed from the current or an inner scope (through |
| 927 | // an eval() call or a runtime with lookup), it must be allocated in the |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 928 | // context. |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame^] | 929 | // |
| 930 | // Exceptions: temporary variables are never allocated in a context; |
| 931 | // catch-bound variables are always allocated in a context. |
| 932 | if (var->mode() == Variable::TEMPORARY) return false; |
| 933 | if (is_catch_scope()) return true; |
| 934 | return var->is_accessed_from_inner_scope() || |
| 935 | scope_calls_eval_ || |
| 936 | inner_scope_calls_eval_ || |
| 937 | scope_contains_with_ || |
| 938 | var->is_global(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 939 | } |
| 940 | |
| 941 | |
| 942 | bool Scope::HasArgumentsParameter() { |
| 943 | for (int i = 0; i < params_.length(); i++) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 944 | if (params_[i]->name().is_identical_to(FACTORY->arguments_symbol())) |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 945 | return true; |
| 946 | } |
| 947 | return false; |
| 948 | } |
| 949 | |
| 950 | |
| 951 | void Scope::AllocateStackSlot(Variable* var) { |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 952 | var->set_rewrite(new Slot(var, Slot::LOCAL, num_stack_slots_++)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 953 | } |
| 954 | |
| 955 | |
| 956 | void Scope::AllocateHeapSlot(Variable* var) { |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 957 | var->set_rewrite(new Slot(var, Slot::CONTEXT, num_heap_slots_++)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 958 | } |
| 959 | |
| 960 | |
| 961 | void Scope::AllocateParameterLocals() { |
| 962 | ASSERT(is_function_scope()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 963 | Variable* arguments = LocalLookup(FACTORY->arguments_symbol()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 964 | ASSERT(arguments != NULL); // functions have 'arguments' declared implicitly |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 965 | |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 966 | bool uses_nonstrict_arguments = false; |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 967 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 968 | if (MustAllocate(arguments) && !HasArgumentsParameter()) { |
| 969 | // 'arguments' is used. Unless there is also a parameter called |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 970 | // 'arguments', we must be conservative and allocate all parameters to |
| 971 | // the context assuming they will be captured by the arguments object. |
| 972 | // If we have a parameter named 'arguments', a (new) value is always |
| 973 | // assigned to it via the function invocation. Then 'arguments' denotes |
| 974 | // that specific parameter value and cannot be used to access the |
| 975 | // parameters, which is why we don't need to allocate an arguments |
| 976 | // object in that case. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 977 | |
| 978 | // We are using 'arguments'. Tell the code generator that is needs to |
| 979 | // allocate the arguments object by setting 'arguments_'. |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 980 | arguments_ = arguments; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 981 | |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 982 | // In strict mode 'arguments' does not alias formal parameters. |
| 983 | // Therefore in strict mode we allocate parameters as if 'arguments' |
| 984 | // were not used. |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 985 | uses_nonstrict_arguments = !is_strict_mode(); |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 986 | } |
| 987 | |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 988 | // The same parameter may occur multiple times in the parameters_ list. |
| 989 | // If it does, and if it is not copied into the context object, it must |
| 990 | // receive the highest parameter index for that parameter; thus iteration |
| 991 | // order is relevant! |
| 992 | for (int i = params_.length() - 1; i >= 0; --i) { |
| 993 | Variable* var = params_[i]; |
| 994 | ASSERT(var->scope() == this); |
| 995 | if (uses_nonstrict_arguments) { |
| 996 | // Give the parameter a use from an inner scope, to force allocation |
| 997 | // to the context. |
| 998 | var->MarkAsAccessedFromInnerScope(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 999 | } |
| 1000 | |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 1001 | if (MustAllocate(var)) { |
| 1002 | if (MustAllocateInContext(var)) { |
| 1003 | ASSERT(var->rewrite() == NULL || var->IsContextSlot()); |
| 1004 | if (var->rewrite() == NULL) { |
| 1005 | AllocateHeapSlot(var); |
| 1006 | } |
| 1007 | } else { |
| 1008 | ASSERT(var->rewrite() == NULL || var->IsParameter()); |
| 1009 | if (var->rewrite() == NULL) { |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 1010 | var->set_rewrite(new Slot(var, Slot::PARAMETER, i)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1011 | } |
| 1012 | } |
| 1013 | } |
| 1014 | } |
| 1015 | } |
| 1016 | |
| 1017 | |
| 1018 | void Scope::AllocateNonParameterLocal(Variable* var) { |
| 1019 | ASSERT(var->scope() == this); |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 1020 | ASSERT(var->rewrite() == NULL || |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 1021 | !var->IsVariable(FACTORY->result_symbol()) || |
| 1022 | var->AsSlot() == NULL || |
| 1023 | var->AsSlot()->type() != Slot::LOCAL); |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 1024 | if (var->rewrite() == NULL && MustAllocate(var)) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1025 | if (MustAllocateInContext(var)) { |
| 1026 | AllocateHeapSlot(var); |
| 1027 | } else { |
| 1028 | AllocateStackSlot(var); |
| 1029 | } |
| 1030 | } |
| 1031 | } |
| 1032 | |
| 1033 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1034 | void Scope::AllocateNonParameterLocals() { |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 1035 | // All variables that have no rewrite yet are non-parameter locals. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1036 | for (int i = 0; i < temps_.length(); i++) { |
| 1037 | AllocateNonParameterLocal(temps_[i]); |
| 1038 | } |
| 1039 | |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 1040 | for (VariableMap::Entry* p = variables_.Start(); |
| 1041 | p != NULL; |
| 1042 | p = variables_.Next(p)) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1043 | Variable* var = reinterpret_cast<Variable*>(p->value); |
| 1044 | AllocateNonParameterLocal(var); |
| 1045 | } |
| 1046 | |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 1047 | // For now, function_ must be allocated at the very end. If it gets |
| 1048 | // allocated in the context, it must be the last slot in the context, |
| 1049 | // because of the current ScopeInfo implementation (see |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1050 | // ScopeInfo::ScopeInfo(FunctionScope* scope) constructor). |
| 1051 | if (function_ != NULL) { |
| 1052 | AllocateNonParameterLocal(function_); |
| 1053 | } |
| 1054 | } |
| 1055 | |
| 1056 | |
| 1057 | void Scope::AllocateVariablesRecursively() { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1058 | // Allocate variables for inner scopes. |
| 1059 | for (int i = 0; i < inner_scopes_.length(); i++) { |
| 1060 | inner_scopes_[i]->AllocateVariablesRecursively(); |
| 1061 | } |
| 1062 | |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 1063 | // If scope is already resolved, we still need to allocate |
| 1064 | // variables in inner scopes which might not had been resolved yet. |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame^] | 1065 | if (already_resolved()) return; |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 1066 | // The number of slots required for variables. |
| 1067 | num_stack_slots_ = 0; |
| 1068 | num_heap_slots_ = Context::MIN_CONTEXT_SLOTS; |
| 1069 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1070 | // Allocate variables for this scope. |
| 1071 | // Parameters must be allocated first, if any. |
| 1072 | if (is_function_scope()) AllocateParameterLocals(); |
| 1073 | AllocateNonParameterLocals(); |
| 1074 | |
| 1075 | // Allocate context if necessary. |
| 1076 | bool must_have_local_context = false; |
| 1077 | if (scope_calls_eval_ || scope_contains_with_) { |
| 1078 | // The context for the eval() call or 'with' statement in this scope. |
| 1079 | // Unless we are in the global or an eval scope, we need a local |
| 1080 | // context even if we didn't statically allocate any locals in it, |
| 1081 | // and the compiler will access the context variable. If we are |
| 1082 | // not in an inner scope, the scope is provided from the outside. |
| 1083 | must_have_local_context = is_function_scope(); |
| 1084 | } |
| 1085 | |
| 1086 | // If we didn't allocate any locals in the local context, then we only |
| 1087 | // need the minimal number of slots if we must have a local context. |
| 1088 | if (num_heap_slots_ == Context::MIN_CONTEXT_SLOTS && |
| 1089 | !must_have_local_context) { |
| 1090 | num_heap_slots_ = 0; |
| 1091 | } |
| 1092 | |
| 1093 | // Allocation done. |
| 1094 | ASSERT(num_heap_slots_ == 0 || num_heap_slots_ >= Context::MIN_CONTEXT_SLOTS); |
| 1095 | } |
| 1096 | |
| 1097 | } } // namespace v8::internal |