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