| OLD | NEW |
| 1 // Copyright 2006-2008 the V8 project authors. All rights reserved. | 1 // Copyright 2006-2008 the V8 project authors. All rights reserved. |
| 2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
| 3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
| 4 // met: | 4 // met: |
| 5 // | 5 // |
| 6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
| 7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
| 8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
| 9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
| 10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
| (...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 132 receiver_(NULL), | 132 receiver_(NULL), |
| 133 function_(NULL), | 133 function_(NULL), |
| 134 arguments_(NULL), | 134 arguments_(NULL), |
| 135 arguments_shadow_(NULL), | 135 arguments_shadow_(NULL), |
| 136 illegal_redecl_(NULL), | 136 illegal_redecl_(NULL), |
| 137 scope_inside_with_(false), | 137 scope_inside_with_(false), |
| 138 scope_contains_with_(false), | 138 scope_contains_with_(false), |
| 139 scope_calls_eval_(false), | 139 scope_calls_eval_(false), |
| 140 outer_scope_calls_eval_(false), | 140 outer_scope_calls_eval_(false), |
| 141 inner_scope_calls_eval_(false), | 141 inner_scope_calls_eval_(false), |
| 142 outer_scope_is_eval_scope_(false), |
| 142 force_eager_compilation_(false), | 143 force_eager_compilation_(false), |
| 143 num_stack_slots_(0), | 144 num_stack_slots_(0), |
| 144 num_heap_slots_(0) { | 145 num_heap_slots_(0) { |
| 145 // At some point we might want to provide outer scopes to | 146 // At some point we might want to provide outer scopes to |
| 146 // eval scopes (by walking the stack and reading the scope info). | 147 // eval scopes (by walking the stack and reading the scope info). |
| 147 // In that case, the ASSERT below needs to be adjusted. | 148 // In that case, the ASSERT below needs to be adjusted. |
| 148 ASSERT((type == GLOBAL_SCOPE || type == EVAL_SCOPE) == (outer_scope == NULL)); | 149 ASSERT((type == GLOBAL_SCOPE || type == EVAL_SCOPE) == (outer_scope == NULL)); |
| 149 ASSERT(!HasIllegalRedeclaration()); | 150 ASSERT(!HasIllegalRedeclaration()); |
| 150 } | 151 } |
| 151 | 152 |
| (...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 305 | 306 |
| 306 void Scope::AllocateVariables() { | 307 void Scope::AllocateVariables() { |
| 307 ASSERT(outer_scope_ == NULL); // eval or global scopes only | 308 ASSERT(outer_scope_ == NULL); // eval or global scopes only |
| 308 | 309 |
| 309 // 1) Propagate scope information. | 310 // 1) Propagate scope information. |
| 310 // If we are in an eval scope, we may have other outer scopes about | 311 // If we are in an eval scope, we may have other outer scopes about |
| 311 // which we don't know anything at this point. Thus we must be conservative | 312 // which we don't know anything at this point. Thus we must be conservative |
| 312 // and assume they may invoke eval themselves. Eventually we could capture | 313 // and assume they may invoke eval themselves. Eventually we could capture |
| 313 // this information in the ScopeInfo and then use it here (by traversing | 314 // this information in the ScopeInfo and then use it here (by traversing |
| 314 // the call chain stack, at compile time). | 315 // the call chain stack, at compile time). |
| 315 PropagateScopeInfo(is_eval_scope()); | 316 bool eval_scope = is_eval_scope(); |
| 317 PropagateScopeInfo(eval_scope, eval_scope); |
| 316 | 318 |
| 317 // 2) Resolve variables. | 319 // 2) Resolve variables. |
| 318 Scope* global_scope = NULL; | 320 Scope* global_scope = NULL; |
| 319 if (is_global_scope()) global_scope = this; | 321 if (is_global_scope()) global_scope = this; |
| 320 ResolveVariablesRecursively(global_scope); | 322 ResolveVariablesRecursively(global_scope); |
| 321 | 323 |
| 322 // 3) Allocate variables. | 324 // 3) Allocate variables. |
| 323 AllocateVariablesRecursively(); | 325 AllocateVariablesRecursively(); |
| 324 } | 326 } |
| 325 | 327 |
| (...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 435 | 437 |
| 436 // Scope info. | 438 // Scope info. |
| 437 if (HasTrivialOuterContext()) { | 439 if (HasTrivialOuterContext()) { |
| 438 Indent(n1, "// scope has trivial outer context\n"); | 440 Indent(n1, "// scope has trivial outer context\n"); |
| 439 } | 441 } |
| 440 if (scope_inside_with_) Indent(n1, "// scope inside 'with'\n"); | 442 if (scope_inside_with_) Indent(n1, "// scope inside 'with'\n"); |
| 441 if (scope_contains_with_) Indent(n1, "// scope contains 'with'\n"); | 443 if (scope_contains_with_) Indent(n1, "// scope contains 'with'\n"); |
| 442 if (scope_calls_eval_) Indent(n1, "// scope calls 'eval'\n"); | 444 if (scope_calls_eval_) Indent(n1, "// scope calls 'eval'\n"); |
| 443 if (outer_scope_calls_eval_) Indent(n1, "// outer scope calls 'eval'\n"); | 445 if (outer_scope_calls_eval_) Indent(n1, "// outer scope calls 'eval'\n"); |
| 444 if (inner_scope_calls_eval_) Indent(n1, "// inner scope calls 'eval'\n"); | 446 if (inner_scope_calls_eval_) Indent(n1, "// inner scope calls 'eval'\n"); |
| 447 if (outer_scope_is_eval_scope_) Indent(n1, "// outer scope is 'eval' scope\n")
; |
| 445 if (num_stack_slots_ > 0) { Indent(n1, "// "); | 448 if (num_stack_slots_ > 0) { Indent(n1, "// "); |
| 446 PrintF("%d stack slots\n", num_stack_slots_); } | 449 PrintF("%d stack slots\n", num_stack_slots_); } |
| 447 if (num_heap_slots_ > 0) { Indent(n1, "// "); | 450 if (num_heap_slots_ > 0) { Indent(n1, "// "); |
| 448 PrintF("%d heap slots\n", num_heap_slots_); } | 451 PrintF("%d heap slots\n", num_heap_slots_); } |
| 449 | 452 |
| 450 // Print locals. | 453 // Print locals. |
| 451 PrettyPrinter printer; | 454 PrettyPrinter printer; |
| 452 Indent(n1, "// function var\n"); | 455 Indent(n1, "// function var\n"); |
| 453 if (function_ != NULL) { | 456 if (function_ != NULL) { |
| 454 PrintVar(&printer, n1, function_); | 457 PrintVar(&printer, n1, function_); |
| (...skipping 20 matching lines...) Expand all Loading... |
| 475 PrintF("\n"); | 478 PrintF("\n"); |
| 476 inner_scopes_[i]->Print(n1); | 479 inner_scopes_[i]->Print(n1); |
| 477 } | 480 } |
| 478 } | 481 } |
| 479 | 482 |
| 480 Indent(n0, "}\n"); | 483 Indent(n0, "}\n"); |
| 481 } | 484 } |
| 482 #endif // DEBUG | 485 #endif // DEBUG |
| 483 | 486 |
| 484 | 487 |
| 485 Variable* Scope::NonLocal(Handle<String> name) { | 488 Variable* Scope::NonLocal(Handle<String> name, Variable::Mode mode) { |
| 486 // Space optimization: reuse existing non-local with the same name. | 489 // Space optimization: reuse existing non-local with the same name |
| 490 // and mode. |
| 487 for (int i = 0; i < nonlocals_.length(); i++) { | 491 for (int i = 0; i < nonlocals_.length(); i++) { |
| 488 Variable* var = nonlocals_[i]; | 492 Variable* var = nonlocals_[i]; |
| 489 if (var->name().is_identical_to(name)) { | 493 if (var->name().is_identical_to(name) && var->mode() == mode) { |
| 490 ASSERT(var->mode() == Variable::DYNAMIC); | |
| 491 return var; | 494 return var; |
| 492 } | 495 } |
| 493 } | 496 } |
| 494 | 497 |
| 495 // Otherwise create a new new-local and add it to the list. | 498 // Otherwise create a new non-local and add it to the list. |
| 496 Variable* var = new Variable( | 499 Variable* var = new Variable(NULL, name, mode, true, false); |
| 497 NULL /* we don't know the scope */, | |
| 498 name, Variable::DYNAMIC, true, false); | |
| 499 nonlocals_.Add(var); | 500 nonlocals_.Add(var); |
| 500 | 501 |
| 501 // Allocate it by giving it a dynamic lookup. | 502 // Allocate it by giving it a dynamic lookup. |
| 502 var->rewrite_ = new Slot(var, Slot::LOOKUP, -1); | 503 var->rewrite_ = new Slot(var, Slot::LOOKUP, -1); |
| 503 | 504 |
| 504 return var; | 505 return var; |
| 505 } | 506 } |
| 506 | 507 |
| 507 | 508 |
| 508 // Lookup a variable starting with this scope. The result is either | 509 // Lookup a variable starting with this scope. The result is either |
| 509 // the statically resolved (local!) variable belonging to an outer scope, | 510 // the statically resolved (local!) variable belonging to an outer scope, |
| 510 // or NULL. It may be NULL because a) we couldn't find a variable, or b) | 511 // or NULL. It may be NULL because a) we couldn't find a variable, or b) |
| 511 // because the variable is just a guess (and may be shadowed by another | 512 // because the variable is just a guess (and may be shadowed by another |
| 512 // variable that is introduced dynamically via an 'eval' call or a 'with' | 513 // variable that is introduced dynamically via an 'eval' call or a 'with' |
| 513 // statement). | 514 // statement). |
| 514 Variable* Scope::LookupRecursive(Handle<String> name, bool inner_lookup) { | 515 Variable* Scope::LookupRecursive(Handle<String> name, |
| 516 bool inner_lookup, |
| 517 Variable** invalidated_local) { |
| 515 // If we find a variable, but the current scope calls 'eval', the found | 518 // If we find a variable, but the current scope calls 'eval', the found |
| 516 // variable may not be the correct one (the 'eval' may introduce a | 519 // variable may not be the correct one (the 'eval' may introduce a |
| 517 // property with the same name). In that case, remember that the variable | 520 // property with the same name). In that case, remember that the variable |
| 518 // found is just a guess. | 521 // found is just a guess. |
| 519 bool guess = scope_calls_eval_; | 522 bool guess = scope_calls_eval_; |
| 520 | 523 |
| 521 // Try to find the variable in this scope. | 524 // Try to find the variable in this scope. |
| 522 Variable* var = LookupLocal(name); | 525 Variable* var = LookupLocal(name); |
| 523 | 526 |
| 524 if (var != NULL) { | 527 if (var != NULL) { |
| (...skipping 10 matching lines...) Expand all Loading... |
| 535 // only present - if at all - for function scopes. | 538 // only present - if at all - for function scopes. |
| 536 // | 539 // |
| 537 // This lookup corresponds to a lookup in the "intermediate" scope sitting | 540 // This lookup corresponds to a lookup in the "intermediate" scope sitting |
| 538 // between this scope and the outer scope. (ECMA-262, 3rd., requires that | 541 // between this scope and the outer scope. (ECMA-262, 3rd., requires that |
| 539 // the name of named function literal is kept in an intermediate scope | 542 // the name of named function literal is kept in an intermediate scope |
| 540 // in between this scope and the next outer scope.) | 543 // in between this scope and the next outer scope.) |
| 541 if (function_ != NULL && function_->name().is_identical_to(name)) { | 544 if (function_ != NULL && function_->name().is_identical_to(name)) { |
| 542 var = function_; | 545 var = function_; |
| 543 | 546 |
| 544 } else if (outer_scope_ != NULL) { | 547 } else if (outer_scope_ != NULL) { |
| 545 var = outer_scope_->LookupRecursive(name, true /* inner lookup */); | 548 var = outer_scope_->LookupRecursive(name, true, invalidated_local); |
| 546 // We may have found a variable in an outer scope. However, if | 549 // We may have found a variable in an outer scope. However, if |
| 547 // the current scope is inside a 'with', the actual variable may | 550 // the current scope is inside a 'with', the actual variable may |
| 548 // be a property introduced via the 'with' statement. Then, the | 551 // be a property introduced via the 'with' statement. Then, the |
| 549 // variable we may have found is just a guess. | 552 // variable we may have found is just a guess. |
| 550 if (scope_inside_with_) | 553 if (scope_inside_with_) |
| 551 guess = true; | 554 guess = true; |
| 552 } | 555 } |
| 553 | 556 |
| 554 // If we did not find a variable, we are done. | 557 // If we did not find a variable, we are done. |
| 555 if (var == NULL) | 558 if (var == NULL) |
| 556 return NULL; | 559 return NULL; |
| 557 } | 560 } |
| 558 | 561 |
| 559 ASSERT(var != NULL); | 562 ASSERT(var != NULL); |
| 560 | 563 |
| 561 // If this is a lookup from an inner scope, mark the variable. | 564 // If this is a lookup from an inner scope, mark the variable. |
| 562 if (inner_lookup) | 565 if (inner_lookup) |
| 563 var->is_accessed_from_inner_scope_ = true; | 566 var->is_accessed_from_inner_scope_ = true; |
| 564 | 567 |
| 565 // If the variable we have found is just a guess, invalidate the result. | 568 // If the variable we have found is just a guess, invalidate the result. |
| 566 if (guess) | 569 if (guess) { |
| 570 *invalidated_local = var; |
| 567 var = NULL; | 571 var = NULL; |
| 572 } |
| 568 | 573 |
| 569 return var; | 574 return var; |
| 570 } | 575 } |
| 571 | 576 |
| 572 | 577 |
| 573 void Scope::ResolveVariable(Scope* global_scope, VariableProxy* proxy) { | 578 void Scope::ResolveVariable(Scope* global_scope, VariableProxy* proxy) { |
| 574 ASSERT(global_scope == NULL || global_scope->is_global_scope()); | 579 ASSERT(global_scope == NULL || global_scope->is_global_scope()); |
| 575 | 580 |
| 576 // If the proxy is already resolved there's nothing to do | 581 // If the proxy is already resolved there's nothing to do |
| 577 // (functions and consts may be resolved by the parser). | 582 // (functions and consts may be resolved by the parser). |
| 578 if (proxy->var() != NULL) return; | 583 if (proxy->var() != NULL) return; |
| 579 | 584 |
| 580 // Otherwise, try to resolve the variable. | 585 // Otherwise, try to resolve the variable. |
| 581 Variable* var = LookupRecursive(proxy->name(), false); | 586 Variable* invalidated_local = NULL; |
| 587 Variable* var = LookupRecursive(proxy->name(), false, &invalidated_local); |
| 582 | 588 |
| 583 if (proxy->inside_with()) { | 589 if (proxy->inside_with()) { |
| 584 // If we are inside a local 'with' statement, all bets are off | 590 // If we are inside a local 'with' statement, all bets are off |
| 585 // and we cannot resolve the proxy to a local variable even if | 591 // and we cannot resolve the proxy to a local variable even if |
| 586 // we found an outer matching variable. | 592 // we found an outer matching variable. |
| 587 // Note that we must do a lookup anyway, because if we find one, | 593 // Note that we must do a lookup anyway, because if we find one, |
| 588 // we must mark that variable as potentially accessed from this | 594 // we must mark that variable as potentially accessed from this |
| 589 // inner scope (the property may not be in the 'with' object). | 595 // inner scope (the property may not be in the 'with' object). |
| 590 var = NonLocal(proxy->name()); | 596 var = NonLocal(proxy->name(), Variable::DYNAMIC); |
| 591 | 597 |
| 592 } else { | 598 } else { |
| 593 // We are not inside a local 'with' statement. | 599 // We are not inside a local 'with' statement. |
| 594 | 600 |
| 595 if (var == NULL) { | 601 if (var == NULL) { |
| 596 // We did not find the variable. We have a global variable | 602 // We did not find the variable. We have a global variable |
| 597 // if we are in the global scope (we know already that we | 603 // if we are in the global scope (we know already that we |
| 598 // are outside a 'with' statement) or if there is no way | 604 // are outside a 'with' statement) or if there is no way |
| 599 // that the variable might be introduced dynamically (through | 605 // that the variable might be introduced dynamically (through |
| 600 // a local or outer eval() call, or an outer 'with' statement), | 606 // a local or outer eval() call, or an outer 'with' statement), |
| 601 // or we don't know about the outer scope (because we are | 607 // or we don't know about the outer scope (because we are |
| 602 // in an eval scope). | 608 // in an eval scope). |
| 603 if (!is_global_scope() && | 609 if (!is_global_scope() && |
| 604 (is_eval_scope() || outer_scope_calls_eval_ || | 610 (scope_inside_with_ || outer_scope_is_eval_scope_)) { |
| 605 scope_calls_eval_ || scope_inside_with_)) { | 611 // If we are inside a with statement or the code is executed |
| 606 // We must look up the variable at runtime, and we don't | 612 // using eval, we give up and look up the variable at runtime. |
| 607 // know anything else. | 613 var = NonLocal(proxy->name(), Variable::DYNAMIC); |
| 608 var = NonLocal(proxy->name()); | 614 |
| 615 } else if (!is_global_scope() && |
| 616 (scope_calls_eval_ || outer_scope_calls_eval_)) { |
| 617 // If the code is not executed using eval and there are no |
| 618 // with scopes, either we have a local or a global variable |
| 619 // that might be shadowed by an eval-introduced variable. |
| 620 if (invalidated_local != NULL) { |
| 621 var = NonLocal(proxy->name(), Variable::DYNAMIC_LOCAL); |
| 622 var->set_local_if_not_shadowed(invalidated_local); |
| 623 } else { |
| 624 var = NonLocal(proxy->name(), Variable::DYNAMIC_GLOBAL); |
| 625 } |
| 609 | 626 |
| 610 } else { | 627 } else { |
| 611 // We must have a global variable. | 628 // We must have a global variable. |
| 612 ASSERT(global_scope != NULL); | 629 ASSERT(global_scope != NULL); |
| 613 var = new Variable(global_scope, proxy->name(), | 630 var = new Variable(global_scope, proxy->name(), |
| 614 Variable::DYNAMIC, true, false); | 631 Variable::DYNAMIC, true, false); |
| 615 // Ideally we simply rewrite these variables into property | 632 // Ideally we simply rewrite these variables into property |
| 616 // accesses. Unfortunately, we cannot do this here at the | 633 // accesses. Unfortunately, we cannot do this here at the |
| 617 // moment because then we can't differentiate between | 634 // moment because then we can't differentiate between |
| 618 // global variable ('x') and global property ('this.x') access. | 635 // global variable ('x') and global property ('this.x') access. |
| (...skipping 17 matching lines...) Expand all Loading... |
| 636 ResolveVariable(global_scope, unresolved_[i]); | 653 ResolveVariable(global_scope, unresolved_[i]); |
| 637 } | 654 } |
| 638 | 655 |
| 639 // Resolve unresolved variables for inner scopes. | 656 // Resolve unresolved variables for inner scopes. |
| 640 for (int i = 0; i < inner_scopes_.length(); i++) { | 657 for (int i = 0; i < inner_scopes_.length(); i++) { |
| 641 inner_scopes_[i]->ResolveVariablesRecursively(global_scope); | 658 inner_scopes_[i]->ResolveVariablesRecursively(global_scope); |
| 642 } | 659 } |
| 643 } | 660 } |
| 644 | 661 |
| 645 | 662 |
| 646 bool Scope::PropagateScopeInfo(bool outer_scope_calls_eval) { | 663 bool Scope::PropagateScopeInfo(bool outer_scope_calls_eval, |
| 664 bool outer_scope_is_eval_scope) { |
| 647 if (outer_scope_calls_eval) { | 665 if (outer_scope_calls_eval) { |
| 648 outer_scope_calls_eval_ = true; | 666 outer_scope_calls_eval_ = true; |
| 649 } | 667 } |
| 650 | 668 |
| 651 bool b = scope_calls_eval_ || outer_scope_calls_eval_; | 669 if (outer_scope_is_eval_scope) { |
| 670 outer_scope_is_eval_scope_ = true; |
| 671 } |
| 672 |
| 673 bool calls_eval = scope_calls_eval_ || outer_scope_calls_eval_; |
| 674 bool is_eval = is_eval_scope() || outer_scope_is_eval_scope_; |
| 652 for (int i = 0; i < inner_scopes_.length(); i++) { | 675 for (int i = 0; i < inner_scopes_.length(); i++) { |
| 653 Scope* inner_scope = inner_scopes_[i]; | 676 Scope* inner_scope = inner_scopes_[i]; |
| 654 if (inner_scope->PropagateScopeInfo(b)) { | 677 if (inner_scope->PropagateScopeInfo(calls_eval, is_eval)) { |
| 655 inner_scope_calls_eval_ = true; | 678 inner_scope_calls_eval_ = true; |
| 656 } | 679 } |
| 657 if (inner_scope->force_eager_compilation_) { | 680 if (inner_scope->force_eager_compilation_) { |
| 658 force_eager_compilation_ = true; | 681 force_eager_compilation_ = true; |
| 659 } | 682 } |
| 660 } | 683 } |
| 661 | 684 |
| 662 return scope_calls_eval_ || inner_scope_calls_eval_; | 685 return scope_calls_eval_ || inner_scope_calls_eval_; |
| 663 } | 686 } |
| 664 | 687 |
| (...skipping 227 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 892 if (num_heap_slots_ == Context::MIN_CONTEXT_SLOTS && | 915 if (num_heap_slots_ == Context::MIN_CONTEXT_SLOTS && |
| 893 !must_have_local_context) { | 916 !must_have_local_context) { |
| 894 num_heap_slots_ = 0; | 917 num_heap_slots_ = 0; |
| 895 } | 918 } |
| 896 | 919 |
| 897 // Allocation done. | 920 // Allocation done. |
| 898 ASSERT(num_heap_slots_ == 0 || num_heap_slots_ >= Context::MIN_CONTEXT_SLOTS); | 921 ASSERT(num_heap_slots_ == 0 || num_heap_slots_ >= Context::MIN_CONTEXT_SLOTS); |
| 899 } | 922 } |
| 900 | 923 |
| 901 } } // namespace v8::internal | 924 } } // namespace v8::internal |
| OLD | NEW |