Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 1 | // Copyright 2010 the V8 project authors. All rights reserved. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +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 | |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 30 | #include "scopes.h" |
| 31 | |
| 32 | #include "bootstrapper.h" |
| 33 | #include "compiler.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 34 | #include "prettyprinter.h" |
| 35 | #include "scopeinfo.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 36 | |
| 37 | namespace v8 { |
| 38 | namespace internal { |
| 39 | |
| 40 | // ---------------------------------------------------------------------------- |
| 41 | // A Zone allocator for use with LocalsMap. |
| 42 | |
| 43 | class ZoneAllocator: public Allocator { |
| 44 | public: |
| 45 | /* nothing to do */ |
| 46 | virtual ~ZoneAllocator() {} |
| 47 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 48 | virtual void* New(size_t size) { return Zone::New(static_cast<int>(size)); } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 49 | |
| 50 | /* ignored - Zone is freed in one fell swoop */ |
| 51 | virtual void Delete(void* p) {} |
| 52 | }; |
| 53 | |
| 54 | |
| 55 | static ZoneAllocator LocalsMapAllocator; |
| 56 | |
| 57 | |
| 58 | // ---------------------------------------------------------------------------- |
| 59 | // Implementation of LocalsMap |
| 60 | // |
| 61 | // Note: We are storing the handle locations as key values in the hash map. |
| 62 | // When inserting a new variable via Declare(), we rely on the fact that |
| 63 | // the handle location remains alive for the duration of that variable |
| 64 | // use. Because a Variable holding a handle with the same location exists |
| 65 | // this is ensured. |
| 66 | |
| 67 | static bool Match(void* key1, void* key2) { |
| 68 | String* name1 = *reinterpret_cast<String**>(key1); |
| 69 | String* name2 = *reinterpret_cast<String**>(key2); |
| 70 | ASSERT(name1->IsSymbol()); |
| 71 | ASSERT(name2->IsSymbol()); |
| 72 | return name1 == name2; |
| 73 | } |
| 74 | |
| 75 | |
| 76 | // Dummy constructor |
| 77 | VariableMap::VariableMap(bool gotta_love_static_overloading) : HashMap() {} |
| 78 | |
| 79 | VariableMap::VariableMap() : HashMap(Match, &LocalsMapAllocator, 8) {} |
| 80 | VariableMap::~VariableMap() {} |
| 81 | |
| 82 | |
| 83 | Variable* VariableMap::Declare(Scope* scope, |
| 84 | Handle<String> name, |
| 85 | Variable::Mode mode, |
| 86 | bool is_valid_lhs, |
| 87 | Variable::Kind kind) { |
| 88 | HashMap::Entry* p = HashMap::Lookup(name.location(), name->Hash(), true); |
| 89 | if (p->value == NULL) { |
| 90 | // The variable has not been declared yet -> insert it. |
| 91 | ASSERT(p->key == name.location()); |
| 92 | p->value = new Variable(scope, name, mode, is_valid_lhs, kind); |
| 93 | } |
| 94 | return reinterpret_cast<Variable*>(p->value); |
| 95 | } |
| 96 | |
| 97 | |
| 98 | Variable* VariableMap::Lookup(Handle<String> name) { |
| 99 | HashMap::Entry* p = HashMap::Lookup(name.location(), name->Hash(), false); |
| 100 | if (p != NULL) { |
| 101 | ASSERT(*reinterpret_cast<String**>(p->key) == *name); |
| 102 | ASSERT(p->value != NULL); |
| 103 | return reinterpret_cast<Variable*>(p->value); |
| 104 | } |
| 105 | return NULL; |
| 106 | } |
| 107 | |
| 108 | |
| 109 | // ---------------------------------------------------------------------------- |
| 110 | // Implementation of Scope |
| 111 | |
| 112 | |
| 113 | // Dummy constructor |
| 114 | Scope::Scope(Type type) |
| 115 | : outer_scope_(NULL), |
| 116 | inner_scopes_(0), |
| 117 | type_(type), |
| 118 | scope_name_(Factory::empty_symbol()), |
| 119 | variables_(false), |
| 120 | temps_(0), |
| 121 | params_(0), |
| 122 | dynamics_(NULL), |
| 123 | unresolved_(0), |
| 124 | decls_(0), |
| 125 | receiver_(NULL), |
| 126 | function_(NULL), |
| 127 | arguments_(NULL), |
| 128 | arguments_shadow_(NULL), |
| 129 | illegal_redecl_(NULL), |
| 130 | scope_inside_with_(false), |
| 131 | scope_contains_with_(false), |
| 132 | scope_calls_eval_(false), |
| 133 | outer_scope_calls_eval_(false), |
| 134 | inner_scope_calls_eval_(false), |
| 135 | outer_scope_is_eval_scope_(false), |
| 136 | force_eager_compilation_(false), |
| 137 | num_stack_slots_(0), |
| 138 | num_heap_slots_(0) { |
| 139 | } |
| 140 | |
| 141 | |
| 142 | Scope::Scope(Scope* outer_scope, Type type) |
| 143 | : outer_scope_(outer_scope), |
| 144 | inner_scopes_(4), |
| 145 | type_(type), |
| 146 | scope_name_(Factory::empty_symbol()), |
| 147 | temps_(4), |
| 148 | params_(4), |
| 149 | dynamics_(NULL), |
| 150 | unresolved_(16), |
| 151 | decls_(4), |
| 152 | receiver_(NULL), |
| 153 | function_(NULL), |
| 154 | arguments_(NULL), |
| 155 | arguments_shadow_(NULL), |
| 156 | illegal_redecl_(NULL), |
| 157 | scope_inside_with_(false), |
| 158 | scope_contains_with_(false), |
| 159 | scope_calls_eval_(false), |
| 160 | outer_scope_calls_eval_(false), |
| 161 | inner_scope_calls_eval_(false), |
| 162 | outer_scope_is_eval_scope_(false), |
| 163 | force_eager_compilation_(false), |
| 164 | num_stack_slots_(0), |
| 165 | num_heap_slots_(0) { |
| 166 | // At some point we might want to provide outer scopes to |
| 167 | // eval scopes (by walking the stack and reading the scope info). |
| 168 | // In that case, the ASSERT below needs to be adjusted. |
| 169 | ASSERT((type == GLOBAL_SCOPE || type == EVAL_SCOPE) == (outer_scope == NULL)); |
| 170 | ASSERT(!HasIllegalRedeclaration()); |
| 171 | } |
| 172 | |
| 173 | |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 174 | bool Scope::Analyze(CompilationInfo* info) { |
| 175 | ASSERT(info->function() != NULL); |
| 176 | Scope* top = info->function()->scope(); |
| 177 | while (top->outer_scope() != NULL) top = top->outer_scope(); |
| 178 | top->AllocateVariables(info->calling_context()); |
| 179 | |
| 180 | #ifdef DEBUG |
| 181 | if (Bootstrapper::IsActive() |
| 182 | ? FLAG_print_builtin_scopes |
| 183 | : FLAG_print_scopes) { |
| 184 | info->function()->scope()->Print(); |
| 185 | } |
| 186 | #endif |
| 187 | |
| 188 | info->SetScope(info->function()->scope()); |
| 189 | return true; // Can not fail. |
| 190 | } |
| 191 | |
| 192 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 193 | void Scope::Initialize(bool inside_with) { |
| 194 | // Add this scope as a new inner scope of the outer scope. |
| 195 | if (outer_scope_ != NULL) { |
| 196 | outer_scope_->inner_scopes_.Add(this); |
| 197 | scope_inside_with_ = outer_scope_->scope_inside_with_ || inside_with; |
| 198 | } else { |
| 199 | scope_inside_with_ = inside_with; |
| 200 | } |
| 201 | |
| 202 | // Declare convenience variables. |
| 203 | // Declare and allocate receiver (even for the global scope, and even |
| 204 | // if naccesses_ == 0). |
| 205 | // NOTE: When loading parameters in the global scope, we must take |
| 206 | // care not to access them as properties of the global object, but |
| 207 | // instead load them directly from the stack. Currently, the only |
| 208 | // such parameter is 'this' which is passed on the stack when |
| 209 | // invoking scripts |
| 210 | Variable* var = |
| 211 | variables_.Declare(this, Factory::this_symbol(), Variable::VAR, |
| 212 | false, Variable::THIS); |
| 213 | var->rewrite_ = new Slot(var, Slot::PARAMETER, -1); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 214 | receiver_ = var; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 215 | |
| 216 | if (is_function_scope()) { |
| 217 | // Declare 'arguments' variable which exists in all functions. |
| 218 | // Note that it might never be accessed, in which case it won't be |
| 219 | // allocated during variable allocation. |
| 220 | variables_.Declare(this, Factory::arguments_symbol(), Variable::VAR, |
| 221 | true, Variable::ARGUMENTS); |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 226 | Variable* Scope::LocalLookup(Handle<String> name) { |
| 227 | return variables_.Lookup(name); |
| 228 | } |
| 229 | |
| 230 | |
| 231 | Variable* Scope::Lookup(Handle<String> name) { |
| 232 | for (Scope* scope = this; |
| 233 | scope != NULL; |
| 234 | scope = scope->outer_scope()) { |
| 235 | Variable* var = scope->LocalLookup(name); |
| 236 | if (var != NULL) return var; |
| 237 | } |
| 238 | return NULL; |
| 239 | } |
| 240 | |
| 241 | |
| 242 | Variable* Scope::DeclareFunctionVar(Handle<String> name) { |
| 243 | ASSERT(is_function_scope() && function_ == NULL); |
| 244 | function_ = new Variable(this, name, Variable::CONST, true, Variable::NORMAL); |
| 245 | return function_; |
| 246 | } |
| 247 | |
| 248 | |
| 249 | Variable* Scope::DeclareLocal(Handle<String> name, Variable::Mode mode) { |
| 250 | // DYNAMIC variables are introduces during variable allocation, |
| 251 | // INTERNAL variables are allocated explicitly, and TEMPORARY |
| 252 | // variables are allocated via NewTemporary(). |
| 253 | ASSERT(mode == Variable::VAR || mode == Variable::CONST); |
| 254 | return variables_.Declare(this, name, mode, true, Variable::NORMAL); |
| 255 | } |
| 256 | |
| 257 | |
| 258 | Variable* Scope::DeclareGlobal(Handle<String> name) { |
| 259 | ASSERT(is_global_scope()); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 260 | return variables_.Declare(this, name, Variable::DYNAMIC_GLOBAL, true, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 261 | Variable::NORMAL); |
| 262 | } |
| 263 | |
| 264 | |
| 265 | void Scope::AddParameter(Variable* var) { |
| 266 | ASSERT(is_function_scope()); |
| 267 | ASSERT(LocalLookup(var->name()) == var); |
| 268 | params_.Add(var); |
| 269 | } |
| 270 | |
| 271 | |
| 272 | VariableProxy* Scope::NewUnresolved(Handle<String> name, bool inside_with) { |
| 273 | // Note that we must not share the unresolved variables with |
| 274 | // the same name because they may be removed selectively via |
| 275 | // RemoveUnresolved(). |
| 276 | VariableProxy* proxy = new VariableProxy(name, false, inside_with); |
| 277 | unresolved_.Add(proxy); |
| 278 | return proxy; |
| 279 | } |
| 280 | |
| 281 | |
| 282 | void Scope::RemoveUnresolved(VariableProxy* var) { |
| 283 | // Most likely (always?) any variable we want to remove |
| 284 | // was just added before, so we search backwards. |
| 285 | for (int i = unresolved_.length(); i-- > 0;) { |
| 286 | if (unresolved_[i] == var) { |
| 287 | unresolved_.Remove(i); |
| 288 | return; |
| 289 | } |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame^] | 294 | Variable* Scope::NewTemporary(Handle<String> name) { |
| 295 | Variable* var = |
| 296 | new Variable(this, name, Variable::TEMPORARY, true, Variable::NORMAL); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 297 | temps_.Add(var); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame^] | 298 | return var; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | |
| 302 | void Scope::AddDeclaration(Declaration* declaration) { |
| 303 | decls_.Add(declaration); |
| 304 | } |
| 305 | |
| 306 | |
| 307 | void Scope::SetIllegalRedeclaration(Expression* expression) { |
| 308 | // Only set the illegal redeclaration expression the |
| 309 | // first time the function is called. |
| 310 | if (!HasIllegalRedeclaration()) { |
| 311 | illegal_redecl_ = expression; |
| 312 | } |
| 313 | ASSERT(HasIllegalRedeclaration()); |
| 314 | } |
| 315 | |
| 316 | |
| 317 | void Scope::VisitIllegalRedeclaration(AstVisitor* visitor) { |
| 318 | ASSERT(HasIllegalRedeclaration()); |
| 319 | illegal_redecl_->Accept(visitor); |
| 320 | } |
| 321 | |
| 322 | |
| 323 | template<class Allocator> |
| 324 | void Scope::CollectUsedVariables(List<Variable*, Allocator>* locals) { |
| 325 | // Collect variables in this scope. |
| 326 | // Note that the function_ variable - if present - is not |
| 327 | // collected here but handled separately in ScopeInfo |
| 328 | // which is the current user of this function). |
| 329 | for (int i = 0; i < temps_.length(); i++) { |
| 330 | Variable* var = temps_[i]; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 331 | if (var->is_used()) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 332 | locals->Add(var); |
| 333 | } |
| 334 | } |
| 335 | for (VariableMap::Entry* p = variables_.Start(); |
| 336 | p != NULL; |
| 337 | p = variables_.Next(p)) { |
| 338 | Variable* var = reinterpret_cast<Variable*>(p->value); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 339 | if (var->is_used()) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 340 | locals->Add(var); |
| 341 | } |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | |
| 346 | // Make sure the method gets instantiated by the template system. |
| 347 | template void Scope::CollectUsedVariables( |
| 348 | List<Variable*, FreeStoreAllocationPolicy>* locals); |
| 349 | template void Scope::CollectUsedVariables( |
| 350 | List<Variable*, PreallocatedStorage>* locals); |
| 351 | template void Scope::CollectUsedVariables( |
| 352 | List<Variable*, ZoneListAllocationPolicy>* locals); |
| 353 | |
| 354 | |
| 355 | void Scope::AllocateVariables(Handle<Context> context) { |
| 356 | ASSERT(outer_scope_ == NULL); // eval or global scopes only |
| 357 | |
| 358 | // 1) Propagate scope information. |
| 359 | // If we are in an eval scope, we may have other outer scopes about |
| 360 | // which we don't know anything at this point. Thus we must be conservative |
| 361 | // and assume they may invoke eval themselves. Eventually we could capture |
| 362 | // this information in the ScopeInfo and then use it here (by traversing |
| 363 | // the call chain stack, at compile time). |
| 364 | bool eval_scope = is_eval_scope(); |
| 365 | PropagateScopeInfo(eval_scope, eval_scope); |
| 366 | |
| 367 | // 2) Resolve variables. |
| 368 | Scope* global_scope = NULL; |
| 369 | if (is_global_scope()) global_scope = this; |
| 370 | ResolveVariablesRecursively(global_scope, context); |
| 371 | |
| 372 | // 3) Allocate variables. |
| 373 | AllocateVariablesRecursively(); |
| 374 | } |
| 375 | |
| 376 | |
| 377 | bool Scope::AllowsLazyCompilation() const { |
| 378 | return !force_eager_compilation_ && HasTrivialOuterContext(); |
| 379 | } |
| 380 | |
| 381 | |
| 382 | bool Scope::HasTrivialContext() const { |
| 383 | // A function scope has a trivial context if it always is the global |
| 384 | // context. We iteratively scan out the context chain to see if |
| 385 | // there is anything that makes this scope non-trivial; otherwise we |
| 386 | // return true. |
| 387 | for (const Scope* scope = this; scope != NULL; scope = scope->outer_scope_) { |
| 388 | if (scope->is_eval_scope()) return false; |
| 389 | if (scope->scope_inside_with_) return false; |
| 390 | if (scope->num_heap_slots_ > 0) return false; |
| 391 | } |
| 392 | return true; |
| 393 | } |
| 394 | |
| 395 | |
| 396 | bool Scope::HasTrivialOuterContext() const { |
| 397 | Scope* outer = outer_scope_; |
| 398 | if (outer == NULL) return true; |
| 399 | // Note that the outer context may be trivial in general, but the current |
| 400 | // scope may be inside a 'with' statement in which case the outer context |
| 401 | // for this scope is not trivial. |
| 402 | return !scope_inside_with_ && outer->HasTrivialContext(); |
| 403 | } |
| 404 | |
| 405 | |
| 406 | int Scope::ContextChainLength(Scope* scope) { |
| 407 | int n = 0; |
| 408 | for (Scope* s = this; s != scope; s = s->outer_scope_) { |
| 409 | ASSERT(s != NULL); // scope must be in the scope chain |
| 410 | if (s->num_heap_slots() > 0) n++; |
| 411 | } |
| 412 | return n; |
| 413 | } |
| 414 | |
| 415 | |
| 416 | #ifdef DEBUG |
| 417 | static const char* Header(Scope::Type type) { |
| 418 | switch (type) { |
| 419 | case Scope::EVAL_SCOPE: return "eval"; |
| 420 | case Scope::FUNCTION_SCOPE: return "function"; |
| 421 | case Scope::GLOBAL_SCOPE: return "global"; |
| 422 | } |
| 423 | UNREACHABLE(); |
| 424 | return NULL; |
| 425 | } |
| 426 | |
| 427 | |
| 428 | static void Indent(int n, const char* str) { |
| 429 | PrintF("%*s%s", n, "", str); |
| 430 | } |
| 431 | |
| 432 | |
| 433 | static void PrintName(Handle<String> name) { |
| 434 | SmartPointer<char> s = name->ToCString(DISALLOW_NULLS); |
| 435 | PrintF("%s", *s); |
| 436 | } |
| 437 | |
| 438 | |
| 439 | static void PrintVar(PrettyPrinter* printer, int indent, Variable* var) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 440 | if (var->is_used() || var->rewrite() != NULL) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 441 | Indent(indent, Variable::Mode2String(var->mode())); |
| 442 | PrintF(" "); |
| 443 | PrintName(var->name()); |
| 444 | PrintF("; // "); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 445 | if (var->rewrite() != NULL) { |
| 446 | PrintF("%s, ", printer->Print(var->rewrite())); |
| 447 | if (var->is_accessed_from_inner_scope()) PrintF(", "); |
| 448 | } |
| 449 | if (var->is_accessed_from_inner_scope()) PrintF("inner scope access"); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 450 | PrintF("\n"); |
| 451 | } |
| 452 | } |
| 453 | |
| 454 | |
| 455 | static void PrintMap(PrettyPrinter* printer, int indent, VariableMap* map) { |
| 456 | for (VariableMap::Entry* p = map->Start(); p != NULL; p = map->Next(p)) { |
| 457 | Variable* var = reinterpret_cast<Variable*>(p->value); |
| 458 | PrintVar(printer, indent, var); |
| 459 | } |
| 460 | } |
| 461 | |
| 462 | |
| 463 | void Scope::Print(int n) { |
| 464 | int n0 = (n > 0 ? n : 0); |
| 465 | int n1 = n0 + 2; // indentation |
| 466 | |
| 467 | // Print header. |
| 468 | Indent(n0, Header(type_)); |
| 469 | if (scope_name_->length() > 0) { |
| 470 | PrintF(" "); |
| 471 | PrintName(scope_name_); |
| 472 | } |
| 473 | |
| 474 | // Print parameters, if any. |
| 475 | if (is_function_scope()) { |
| 476 | PrintF(" ("); |
| 477 | for (int i = 0; i < params_.length(); i++) { |
| 478 | if (i > 0) PrintF(", "); |
| 479 | PrintName(params_[i]->name()); |
| 480 | } |
| 481 | PrintF(")"); |
| 482 | } |
| 483 | |
| 484 | PrintF(" {\n"); |
| 485 | |
| 486 | // Function name, if any (named function literals, only). |
| 487 | if (function_ != NULL) { |
| 488 | Indent(n1, "// (local) function name: "); |
| 489 | PrintName(function_->name()); |
| 490 | PrintF("\n"); |
| 491 | } |
| 492 | |
| 493 | // Scope info. |
| 494 | if (HasTrivialOuterContext()) { |
| 495 | Indent(n1, "// scope has trivial outer context\n"); |
| 496 | } |
| 497 | if (scope_inside_with_) Indent(n1, "// scope inside 'with'\n"); |
| 498 | if (scope_contains_with_) Indent(n1, "// scope contains 'with'\n"); |
| 499 | if (scope_calls_eval_) Indent(n1, "// scope calls 'eval'\n"); |
| 500 | if (outer_scope_calls_eval_) Indent(n1, "// outer scope calls 'eval'\n"); |
| 501 | if (inner_scope_calls_eval_) Indent(n1, "// inner scope calls 'eval'\n"); |
| 502 | if (outer_scope_is_eval_scope_) { |
| 503 | Indent(n1, "// outer scope is 'eval' scope\n"); |
| 504 | } |
| 505 | if (num_stack_slots_ > 0) { Indent(n1, "// "); |
| 506 | PrintF("%d stack slots\n", num_stack_slots_); } |
| 507 | if (num_heap_slots_ > 0) { Indent(n1, "// "); |
| 508 | PrintF("%d heap slots\n", num_heap_slots_); } |
| 509 | |
| 510 | // Print locals. |
| 511 | PrettyPrinter printer; |
| 512 | Indent(n1, "// function var\n"); |
| 513 | if (function_ != NULL) { |
| 514 | PrintVar(&printer, n1, function_); |
| 515 | } |
| 516 | |
| 517 | Indent(n1, "// temporary vars\n"); |
| 518 | for (int i = 0; i < temps_.length(); i++) { |
| 519 | PrintVar(&printer, n1, temps_[i]); |
| 520 | } |
| 521 | |
| 522 | Indent(n1, "// local vars\n"); |
| 523 | PrintMap(&printer, n1, &variables_); |
| 524 | |
| 525 | Indent(n1, "// dynamic vars\n"); |
| 526 | if (dynamics_ != NULL) { |
| 527 | PrintMap(&printer, n1, dynamics_->GetMap(Variable::DYNAMIC)); |
| 528 | PrintMap(&printer, n1, dynamics_->GetMap(Variable::DYNAMIC_LOCAL)); |
| 529 | PrintMap(&printer, n1, dynamics_->GetMap(Variable::DYNAMIC_GLOBAL)); |
| 530 | } |
| 531 | |
| 532 | // Print inner scopes (disable by providing negative n). |
| 533 | if (n >= 0) { |
| 534 | for (int i = 0; i < inner_scopes_.length(); i++) { |
| 535 | PrintF("\n"); |
| 536 | inner_scopes_[i]->Print(n1); |
| 537 | } |
| 538 | } |
| 539 | |
| 540 | Indent(n0, "}\n"); |
| 541 | } |
| 542 | #endif // DEBUG |
| 543 | |
| 544 | |
| 545 | Variable* Scope::NonLocal(Handle<String> name, Variable::Mode mode) { |
| 546 | if (dynamics_ == NULL) dynamics_ = new DynamicScopePart(); |
| 547 | VariableMap* map = dynamics_->GetMap(mode); |
| 548 | Variable* var = map->Lookup(name); |
| 549 | if (var == NULL) { |
| 550 | // Declare a new non-local. |
| 551 | var = map->Declare(NULL, name, mode, true, Variable::NORMAL); |
| 552 | // Allocate it by giving it a dynamic lookup. |
| 553 | var->rewrite_ = new Slot(var, Slot::LOOKUP, -1); |
| 554 | } |
| 555 | return var; |
| 556 | } |
| 557 | |
| 558 | |
| 559 | // Lookup a variable starting with this scope. The result is either |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 560 | // the statically resolved variable belonging to an outer scope, or |
| 561 | // NULL. It may be NULL because a) we couldn't find a variable, or b) |
| 562 | // because the variable is just a guess (and may be shadowed by |
| 563 | // another variable that is introduced dynamically via an 'eval' call |
| 564 | // or a 'with' statement). |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 565 | Variable* Scope::LookupRecursive(Handle<String> name, |
| 566 | bool inner_lookup, |
| 567 | Variable** invalidated_local) { |
| 568 | // If we find a variable, but the current scope calls 'eval', the found |
| 569 | // variable may not be the correct one (the 'eval' may introduce a |
| 570 | // property with the same name). In that case, remember that the variable |
| 571 | // found is just a guess. |
| 572 | bool guess = scope_calls_eval_; |
| 573 | |
| 574 | // Try to find the variable in this scope. |
| 575 | Variable* var = LocalLookup(name); |
| 576 | |
| 577 | if (var != NULL) { |
| 578 | // We found a variable. If this is not an inner lookup, we are done. |
| 579 | // (Even if there is an 'eval' in this scope which introduces the |
| 580 | // same variable again, the resulting variable remains the same. |
| 581 | // Note that enclosing 'with' statements are handled at the call site.) |
| 582 | if (!inner_lookup) |
| 583 | return var; |
| 584 | |
| 585 | } else { |
| 586 | // We did not find a variable locally. Check against the function variable, |
| 587 | // if any. We can do this for all scopes, since the function variable is |
| 588 | // only present - if at all - for function scopes. |
| 589 | // |
| 590 | // This lookup corresponds to a lookup in the "intermediate" scope sitting |
| 591 | // between this scope and the outer scope. (ECMA-262, 3rd., requires that |
| 592 | // the name of named function literal is kept in an intermediate scope |
| 593 | // in between this scope and the next outer scope.) |
| 594 | if (function_ != NULL && function_->name().is_identical_to(name)) { |
| 595 | var = function_; |
| 596 | |
| 597 | } else if (outer_scope_ != NULL) { |
| 598 | var = outer_scope_->LookupRecursive(name, true, invalidated_local); |
| 599 | // We may have found a variable in an outer scope. However, if |
| 600 | // the current scope is inside a 'with', the actual variable may |
| 601 | // be a property introduced via the 'with' statement. Then, the |
| 602 | // variable we may have found is just a guess. |
| 603 | if (scope_inside_with_) |
| 604 | guess = true; |
| 605 | } |
| 606 | |
| 607 | // If we did not find a variable, we are done. |
| 608 | if (var == NULL) |
| 609 | return NULL; |
| 610 | } |
| 611 | |
| 612 | ASSERT(var != NULL); |
| 613 | |
| 614 | // If this is a lookup from an inner scope, mark the variable. |
| 615 | if (inner_lookup) |
| 616 | var->is_accessed_from_inner_scope_ = true; |
| 617 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 618 | // If the variable we have found is just a guess, invalidate the |
| 619 | // result. If the found variable is local, record that fact so we |
| 620 | // can generate fast code to get it if it is not shadowed by eval. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 621 | if (guess) { |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 622 | if (!var->is_global()) *invalidated_local = var; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 623 | var = NULL; |
| 624 | } |
| 625 | |
| 626 | return var; |
| 627 | } |
| 628 | |
| 629 | |
| 630 | void Scope::ResolveVariable(Scope* global_scope, |
| 631 | Handle<Context> context, |
| 632 | VariableProxy* proxy) { |
| 633 | ASSERT(global_scope == NULL || global_scope->is_global_scope()); |
| 634 | |
| 635 | // If the proxy is already resolved there's nothing to do |
| 636 | // (functions and consts may be resolved by the parser). |
| 637 | if (proxy->var() != NULL) return; |
| 638 | |
| 639 | // Otherwise, try to resolve the variable. |
| 640 | Variable* invalidated_local = NULL; |
| 641 | Variable* var = LookupRecursive(proxy->name(), false, &invalidated_local); |
| 642 | |
| 643 | if (proxy->inside_with()) { |
| 644 | // If we are inside a local 'with' statement, all bets are off |
| 645 | // and we cannot resolve the proxy to a local variable even if |
| 646 | // we found an outer matching variable. |
| 647 | // Note that we must do a lookup anyway, because if we find one, |
| 648 | // we must mark that variable as potentially accessed from this |
| 649 | // inner scope (the property may not be in the 'with' object). |
| 650 | var = NonLocal(proxy->name(), Variable::DYNAMIC); |
| 651 | |
| 652 | } else { |
| 653 | // We are not inside a local 'with' statement. |
| 654 | |
| 655 | if (var == NULL) { |
| 656 | // We did not find the variable. We have a global variable |
| 657 | // if we are in the global scope (we know already that we |
| 658 | // are outside a 'with' statement) or if there is no way |
| 659 | // that the variable might be introduced dynamically (through |
| 660 | // a local or outer eval() call, or an outer 'with' statement), |
| 661 | // or we don't know about the outer scope (because we are |
| 662 | // in an eval scope). |
| 663 | if (is_global_scope() || |
| 664 | !(scope_inside_with_ || outer_scope_is_eval_scope_ || |
| 665 | scope_calls_eval_ || outer_scope_calls_eval_)) { |
| 666 | // We must have a global variable. |
| 667 | ASSERT(global_scope != NULL); |
| 668 | var = global_scope->DeclareGlobal(proxy->name()); |
| 669 | |
| 670 | } else if (scope_inside_with_) { |
| 671 | // If we are inside a with statement we give up and look up |
| 672 | // the variable at runtime. |
| 673 | var = NonLocal(proxy->name(), Variable::DYNAMIC); |
| 674 | |
| 675 | } else if (invalidated_local != NULL) { |
| 676 | // No with statements are involved and we found a local |
| 677 | // variable that might be shadowed by eval introduced |
| 678 | // variables. |
| 679 | var = NonLocal(proxy->name(), Variable::DYNAMIC_LOCAL); |
| 680 | var->set_local_if_not_shadowed(invalidated_local); |
| 681 | |
| 682 | } else if (outer_scope_is_eval_scope_) { |
| 683 | // No with statements and we did not find a local and the code |
| 684 | // is executed with a call to eval. The context contains |
| 685 | // scope information that we can use to determine if the |
| 686 | // variable is global if it is not shadowed by eval-introduced |
| 687 | // variables. |
| 688 | if (context->GlobalIfNotShadowedByEval(proxy->name())) { |
| 689 | var = NonLocal(proxy->name(), Variable::DYNAMIC_GLOBAL); |
| 690 | |
| 691 | } else { |
| 692 | var = NonLocal(proxy->name(), Variable::DYNAMIC); |
| 693 | } |
| 694 | |
| 695 | } else { |
| 696 | // No with statements and we did not find a local and the code |
| 697 | // is not executed with a call to eval. We know that this |
| 698 | // variable is global unless it is shadowed by eval-introduced |
| 699 | // variables. |
| 700 | var = NonLocal(proxy->name(), Variable::DYNAMIC_GLOBAL); |
| 701 | } |
| 702 | } |
| 703 | } |
| 704 | |
| 705 | proxy->BindTo(var); |
| 706 | } |
| 707 | |
| 708 | |
| 709 | void Scope::ResolveVariablesRecursively(Scope* global_scope, |
| 710 | Handle<Context> context) { |
| 711 | ASSERT(global_scope == NULL || global_scope->is_global_scope()); |
| 712 | |
| 713 | // Resolve unresolved variables for this scope. |
| 714 | for (int i = 0; i < unresolved_.length(); i++) { |
| 715 | ResolveVariable(global_scope, context, unresolved_[i]); |
| 716 | } |
| 717 | |
| 718 | // Resolve unresolved variables for inner scopes. |
| 719 | for (int i = 0; i < inner_scopes_.length(); i++) { |
| 720 | inner_scopes_[i]->ResolveVariablesRecursively(global_scope, context); |
| 721 | } |
| 722 | } |
| 723 | |
| 724 | |
| 725 | bool Scope::PropagateScopeInfo(bool outer_scope_calls_eval, |
| 726 | bool outer_scope_is_eval_scope) { |
| 727 | if (outer_scope_calls_eval) { |
| 728 | outer_scope_calls_eval_ = true; |
| 729 | } |
| 730 | |
| 731 | if (outer_scope_is_eval_scope) { |
| 732 | outer_scope_is_eval_scope_ = true; |
| 733 | } |
| 734 | |
| 735 | bool calls_eval = scope_calls_eval_ || outer_scope_calls_eval_; |
| 736 | bool is_eval = is_eval_scope() || outer_scope_is_eval_scope_; |
| 737 | for (int i = 0; i < inner_scopes_.length(); i++) { |
| 738 | Scope* inner_scope = inner_scopes_[i]; |
| 739 | if (inner_scope->PropagateScopeInfo(calls_eval, is_eval)) { |
| 740 | inner_scope_calls_eval_ = true; |
| 741 | } |
| 742 | if (inner_scope->force_eager_compilation_) { |
| 743 | force_eager_compilation_ = true; |
| 744 | } |
| 745 | } |
| 746 | |
| 747 | return scope_calls_eval_ || inner_scope_calls_eval_; |
| 748 | } |
| 749 | |
| 750 | |
| 751 | bool Scope::MustAllocate(Variable* var) { |
| 752 | // Give var a read/write use if there is a chance it might be accessed |
| 753 | // via an eval() call. This is only possible if the variable has a |
| 754 | // visible name. |
| 755 | if ((var->is_this() || var->name()->length() > 0) && |
| 756 | (var->is_accessed_from_inner_scope_ || |
| 757 | scope_calls_eval_ || inner_scope_calls_eval_ || |
| 758 | scope_contains_with_)) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 759 | var->set_is_used(true); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 760 | } |
| 761 | // Global variables do not need to be allocated. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 762 | return !var->is_global() && var->is_used(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 763 | } |
| 764 | |
| 765 | |
| 766 | bool Scope::MustAllocateInContext(Variable* var) { |
| 767 | // If var is accessed from an inner scope, or if there is a |
| 768 | // possibility that it might be accessed from the current or an inner |
| 769 | // scope (through an eval() call), it must be allocated in the |
| 770 | // context. Exception: temporary variables are not allocated in the |
| 771 | // context. |
| 772 | return |
| 773 | var->mode() != Variable::TEMPORARY && |
| 774 | (var->is_accessed_from_inner_scope_ || |
| 775 | scope_calls_eval_ || inner_scope_calls_eval_ || |
| 776 | scope_contains_with_ || var->is_global()); |
| 777 | } |
| 778 | |
| 779 | |
| 780 | bool Scope::HasArgumentsParameter() { |
| 781 | for (int i = 0; i < params_.length(); i++) { |
| 782 | if (params_[i]->name().is_identical_to(Factory::arguments_symbol())) |
| 783 | return true; |
| 784 | } |
| 785 | return false; |
| 786 | } |
| 787 | |
| 788 | |
| 789 | void Scope::AllocateStackSlot(Variable* var) { |
| 790 | var->rewrite_ = new Slot(var, Slot::LOCAL, num_stack_slots_++); |
| 791 | } |
| 792 | |
| 793 | |
| 794 | void Scope::AllocateHeapSlot(Variable* var) { |
| 795 | var->rewrite_ = new Slot(var, Slot::CONTEXT, num_heap_slots_++); |
| 796 | } |
| 797 | |
| 798 | |
| 799 | void Scope::AllocateParameterLocals() { |
| 800 | ASSERT(is_function_scope()); |
| 801 | Variable* arguments = LocalLookup(Factory::arguments_symbol()); |
| 802 | ASSERT(arguments != NULL); // functions have 'arguments' declared implicitly |
| 803 | if (MustAllocate(arguments) && !HasArgumentsParameter()) { |
| 804 | // 'arguments' is used. Unless there is also a parameter called |
| 805 | // 'arguments', we must be conservative and access all parameters via |
| 806 | // the arguments object: The i'th parameter is rewritten into |
| 807 | // '.arguments[i]' (*). If we have a parameter named 'arguments', a |
| 808 | // (new) value is always assigned to it via the function |
| 809 | // invocation. Then 'arguments' denotes that specific parameter value |
| 810 | // and cannot be used to access the parameters, which is why we don't |
| 811 | // need to rewrite in that case. |
| 812 | // |
| 813 | // (*) Instead of having a parameter called 'arguments', we may have an |
| 814 | // assignment to 'arguments' in the function body, at some arbitrary |
| 815 | // point in time (possibly through an 'eval()' call!). After that |
| 816 | // assignment any re-write of parameters would be invalid (was bug |
| 817 | // 881452). Thus, we introduce a shadow '.arguments' |
| 818 | // variable which also points to the arguments object. For rewrites we |
| 819 | // use '.arguments' which remains valid even if we assign to |
| 820 | // 'arguments'. To summarize: If we need to rewrite, we allocate an |
| 821 | // 'arguments' object dynamically upon function invocation. The compiler |
| 822 | // introduces 2 local variables 'arguments' and '.arguments', both of |
| 823 | // which originally point to the arguments object that was |
| 824 | // allocated. All parameters are rewritten into property accesses via |
| 825 | // the '.arguments' variable. Thus, any changes to properties of |
| 826 | // 'arguments' are reflected in the variables and vice versa. If the |
| 827 | // 'arguments' variable is changed, '.arguments' still points to the |
| 828 | // correct arguments object and the rewrites still work. |
| 829 | |
| 830 | // We are using 'arguments'. Tell the code generator that is needs to |
| 831 | // allocate the arguments object by setting 'arguments_'. |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 832 | arguments_ = arguments; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 833 | |
| 834 | // We also need the '.arguments' shadow variable. Declare it and create |
| 835 | // and bind the corresponding proxy. It's ok to declare it only now |
| 836 | // because it's a local variable that is allocated after the parameters |
| 837 | // have been allocated. |
| 838 | // |
| 839 | // Note: This is "almost" at temporary variable but we cannot use |
| 840 | // NewTemporary() because the mode needs to be INTERNAL since this |
| 841 | // variable may be allocated in the heap-allocated context (temporaries |
| 842 | // are never allocated in the context). |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 843 | arguments_shadow_ = new Variable(this, |
| 844 | Factory::arguments_shadow_symbol(), |
| 845 | Variable::INTERNAL, |
| 846 | true, |
| 847 | Variable::ARGUMENTS); |
| 848 | arguments_shadow_->set_is_used(true); |
| 849 | temps_.Add(arguments_shadow_); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 850 | |
| 851 | // Allocate the parameters by rewriting them into '.arguments[i]' accesses. |
| 852 | for (int i = 0; i < params_.length(); i++) { |
| 853 | Variable* var = params_[i]; |
| 854 | ASSERT(var->scope() == this); |
| 855 | if (MustAllocate(var)) { |
| 856 | if (MustAllocateInContext(var)) { |
| 857 | // It is ok to set this only now, because arguments is a local |
| 858 | // variable that is allocated after the parameters have been |
| 859 | // allocated. |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 860 | arguments_shadow_->is_accessed_from_inner_scope_ = true; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 861 | } |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame^] | 862 | Property* rewrite = |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 863 | new Property(new VariableProxy(arguments_shadow_), |
| 864 | new Literal(Handle<Object>(Smi::FromInt(i))), |
| 865 | RelocInfo::kNoPosition, |
| 866 | Property::SYNTHETIC); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame^] | 867 | rewrite->set_is_arguments_access(true); |
| 868 | var->rewrite_ = rewrite; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 869 | } |
| 870 | } |
| 871 | |
| 872 | } else { |
| 873 | // The arguments object is not used, so we can access parameters directly. |
| 874 | // The same parameter may occur multiple times in the parameters_ list. |
| 875 | // If it does, and if it is not copied into the context object, it must |
| 876 | // receive the highest parameter index for that parameter; thus iteration |
| 877 | // order is relevant! |
| 878 | for (int i = 0; i < params_.length(); i++) { |
| 879 | Variable* var = params_[i]; |
| 880 | ASSERT(var->scope() == this); |
| 881 | if (MustAllocate(var)) { |
| 882 | if (MustAllocateInContext(var)) { |
| 883 | ASSERT(var->rewrite_ == NULL || |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 884 | (var->AsSlot() != NULL && |
| 885 | var->AsSlot()->type() == Slot::CONTEXT)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 886 | if (var->rewrite_ == NULL) { |
| 887 | // Only set the heap allocation if the parameter has not |
| 888 | // been allocated yet. |
| 889 | AllocateHeapSlot(var); |
| 890 | } |
| 891 | } else { |
| 892 | ASSERT(var->rewrite_ == NULL || |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 893 | (var->AsSlot() != NULL && |
| 894 | var->AsSlot()->type() == Slot::PARAMETER)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 895 | // Set the parameter index always, even if the parameter |
| 896 | // was seen before! (We need to access the actual parameter |
| 897 | // supplied for the last occurrence of a multiply declared |
| 898 | // parameter.) |
| 899 | var->rewrite_ = new Slot(var, Slot::PARAMETER, i); |
| 900 | } |
| 901 | } |
| 902 | } |
| 903 | } |
| 904 | } |
| 905 | |
| 906 | |
| 907 | void Scope::AllocateNonParameterLocal(Variable* var) { |
| 908 | ASSERT(var->scope() == this); |
| 909 | ASSERT(var->rewrite_ == NULL || |
| 910 | (!var->IsVariable(Factory::result_symbol())) || |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 911 | (var->AsSlot() == NULL || var->AsSlot()->type() != Slot::LOCAL)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 912 | if (var->rewrite_ == NULL && MustAllocate(var)) { |
| 913 | if (MustAllocateInContext(var)) { |
| 914 | AllocateHeapSlot(var); |
| 915 | } else { |
| 916 | AllocateStackSlot(var); |
| 917 | } |
| 918 | } |
| 919 | } |
| 920 | |
| 921 | |
| 922 | void Scope::AllocateNonParameterLocals() { |
| 923 | // All variables that have no rewrite yet are non-parameter locals. |
| 924 | for (int i = 0; i < temps_.length(); i++) { |
| 925 | AllocateNonParameterLocal(temps_[i]); |
| 926 | } |
| 927 | |
| 928 | for (VariableMap::Entry* p = variables_.Start(); |
| 929 | p != NULL; |
| 930 | p = variables_.Next(p)) { |
| 931 | Variable* var = reinterpret_cast<Variable*>(p->value); |
| 932 | AllocateNonParameterLocal(var); |
| 933 | } |
| 934 | |
| 935 | // For now, function_ must be allocated at the very end. If it gets |
| 936 | // allocated in the context, it must be the last slot in the context, |
| 937 | // because of the current ScopeInfo implementation (see |
| 938 | // ScopeInfo::ScopeInfo(FunctionScope* scope) constructor). |
| 939 | if (function_ != NULL) { |
| 940 | AllocateNonParameterLocal(function_); |
| 941 | } |
| 942 | } |
| 943 | |
| 944 | |
| 945 | void Scope::AllocateVariablesRecursively() { |
| 946 | // The number of slots required for variables. |
| 947 | num_stack_slots_ = 0; |
| 948 | num_heap_slots_ = Context::MIN_CONTEXT_SLOTS; |
| 949 | |
| 950 | // Allocate variables for inner scopes. |
| 951 | for (int i = 0; i < inner_scopes_.length(); i++) { |
| 952 | inner_scopes_[i]->AllocateVariablesRecursively(); |
| 953 | } |
| 954 | |
| 955 | // Allocate variables for this scope. |
| 956 | // Parameters must be allocated first, if any. |
| 957 | if (is_function_scope()) AllocateParameterLocals(); |
| 958 | AllocateNonParameterLocals(); |
| 959 | |
| 960 | // Allocate context if necessary. |
| 961 | bool must_have_local_context = false; |
| 962 | if (scope_calls_eval_ || scope_contains_with_) { |
| 963 | // The context for the eval() call or 'with' statement in this scope. |
| 964 | // Unless we are in the global or an eval scope, we need a local |
| 965 | // context even if we didn't statically allocate any locals in it, |
| 966 | // and the compiler will access the context variable. If we are |
| 967 | // not in an inner scope, the scope is provided from the outside. |
| 968 | must_have_local_context = is_function_scope(); |
| 969 | } |
| 970 | |
| 971 | // If we didn't allocate any locals in the local context, then we only |
| 972 | // need the minimal number of slots if we must have a local context. |
| 973 | if (num_heap_slots_ == Context::MIN_CONTEXT_SLOTS && |
| 974 | !must_have_local_context) { |
| 975 | num_heap_slots_ = 0; |
| 976 | } |
| 977 | |
| 978 | // Allocation done. |
| 979 | ASSERT(num_heap_slots_ == 0 || num_heap_slots_ >= Context::MIN_CONTEXT_SLOTS); |
| 980 | } |
| 981 | |
| 982 | } } // namespace v8::internal |