OLD | NEW |
1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "src/v8.h" | 5 #include "src/v8.h" |
6 | 6 |
7 #include "src/scopes.h" | 7 #include "src/scopes.h" |
8 | 8 |
9 #include "src/accessors.h" | 9 #include "src/accessors.h" |
10 #include "src/bootstrapper.h" | 10 #include "src/bootstrapper.h" |
(...skipping 12 matching lines...) Expand all Loading... |
23 // the handle location remains alive for the duration of that variable | 23 // the handle location remains alive for the duration of that variable |
24 // use. Because a Variable holding a handle with the same location exists | 24 // use. Because a Variable holding a handle with the same location exists |
25 // this is ensured. | 25 // this is ensured. |
26 | 26 |
27 VariableMap::VariableMap(Zone* zone) | 27 VariableMap::VariableMap(Zone* zone) |
28 : ZoneHashMap(ZoneHashMap::PointersMatch, 8, ZoneAllocationPolicy(zone)), | 28 : ZoneHashMap(ZoneHashMap::PointersMatch, 8, ZoneAllocationPolicy(zone)), |
29 zone_(zone) {} | 29 zone_(zone) {} |
30 VariableMap::~VariableMap() {} | 30 VariableMap::~VariableMap() {} |
31 | 31 |
32 | 32 |
33 Variable* VariableMap::Declare( | 33 Variable* VariableMap::Declare(Scope* scope, const AstRawString* name, |
34 Scope* scope, | 34 VariableMode mode, bool is_valid_lhs, |
35 const AstRawString* name, | 35 Variable::Kind kind, |
36 VariableMode mode, | 36 InitializationFlag initialization_flag, |
37 bool is_valid_lhs, | 37 MaybeAssignedFlag maybe_assigned_flag, |
38 Variable::Kind kind, | 38 Interface* interface) { |
39 InitializationFlag initialization_flag, | |
40 Interface* interface) { | |
41 // AstRawStrings are unambiguous, i.e., the same string is always represented | 39 // AstRawStrings are unambiguous, i.e., the same string is always represented |
42 // by the same AstRawString*. | 40 // by the same AstRawString*. |
43 // FIXME(marja): fix the type of Lookup. | 41 // FIXME(marja): fix the type of Lookup. |
44 Entry* p = ZoneHashMap::Lookup(const_cast<AstRawString*>(name), name->hash(), | 42 Entry* p = ZoneHashMap::Lookup(const_cast<AstRawString*>(name), name->hash(), |
45 true, ZoneAllocationPolicy(zone())); | 43 true, ZoneAllocationPolicy(zone())); |
46 if (p->value == NULL) { | 44 if (p->value == NULL) { |
47 // The variable has not been declared yet -> insert it. | 45 // The variable has not been declared yet -> insert it. |
48 ASSERT(p->key == name); | 46 ASSERT(p->key == name); |
49 p->value = new(zone()) Variable(scope, | 47 p->value = new (zone()) |
50 name, | 48 Variable(scope, name, mode, is_valid_lhs, kind, initialization_flag, |
51 mode, | 49 maybe_assigned_flag, interface); |
52 is_valid_lhs, | |
53 kind, | |
54 initialization_flag, | |
55 interface); | |
56 } | 50 } |
57 return reinterpret_cast<Variable*>(p->value); | 51 return reinterpret_cast<Variable*>(p->value); |
58 } | 52 } |
59 | 53 |
60 | 54 |
61 Variable* VariableMap::Lookup(const AstRawString* name) { | 55 Variable* VariableMap::Lookup(const AstRawString* name) { |
62 Entry* p = ZoneHashMap::Lookup(const_cast<AstRawString*>(name), name->hash(), | 56 Entry* p = ZoneHashMap::Lookup(const_cast<AstRawString*>(name), name->hash(), |
63 false, ZoneAllocationPolicy(NULL)); | 57 false, ZoneAllocationPolicy(NULL)); |
64 if (p != NULL) { | 58 if (p != NULL) { |
65 ASSERT(reinterpret_cast<const AstRawString*>(p->key) == name); | 59 ASSERT(reinterpret_cast<const AstRawString*>(p->key) == name); |
(...skipping 319 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
385 // it's ok to get the Handle<String> here. | 379 // it's ok to get the Handle<String> here. |
386 Handle<String> name_handle = name->string(); | 380 Handle<String> name_handle = name->string(); |
387 // If we have a serialized scope info, we might find the variable there. | 381 // If we have a serialized scope info, we might find the variable there. |
388 // There should be no local slot with the given name. | 382 // There should be no local slot with the given name. |
389 ASSERT(scope_info_->StackSlotIndex(*name_handle) < 0); | 383 ASSERT(scope_info_->StackSlotIndex(*name_handle) < 0); |
390 | 384 |
391 // Check context slot lookup. | 385 // Check context slot lookup. |
392 VariableMode mode; | 386 VariableMode mode; |
393 Variable::Location location = Variable::CONTEXT; | 387 Variable::Location location = Variable::CONTEXT; |
394 InitializationFlag init_flag; | 388 InitializationFlag init_flag; |
395 int index = | 389 MaybeAssignedFlag maybe_assigned_flag; |
396 ScopeInfo::ContextSlotIndex(scope_info_, name_handle, &mode, &init_flag); | 390 int index = ScopeInfo::ContextSlotIndex(scope_info_, name_handle, &mode, |
| 391 &init_flag, &maybe_assigned_flag); |
397 if (index < 0) { | 392 if (index < 0) { |
398 // Check parameters. | 393 // Check parameters. |
399 index = scope_info_->ParameterIndex(*name_handle); | 394 index = scope_info_->ParameterIndex(*name_handle); |
400 if (index < 0) return NULL; | 395 if (index < 0) return NULL; |
401 | 396 |
402 mode = DYNAMIC; | 397 mode = DYNAMIC; |
403 location = Variable::LOOKUP; | 398 location = Variable::LOOKUP; |
404 init_flag = kCreatedInitialized; | 399 init_flag = kCreatedInitialized; |
| 400 // Be conservative and flag parameters as maybe assigned. Better information |
| 401 // would require ScopeInfo to serialize the maybe_assigned bit also for |
| 402 // parameters. |
| 403 maybe_assigned_flag = kMaybeAssigned; |
405 } | 404 } |
406 | 405 |
407 Variable* var = variables_.Declare(this, name, mode, true, Variable::NORMAL, | 406 Variable* var = variables_.Declare(this, name, mode, true, Variable::NORMAL, |
408 init_flag); | 407 init_flag, maybe_assigned_flag); |
409 var->AllocateTo(location, index); | 408 var->AllocateTo(location, index); |
410 return var; | 409 return var; |
411 } | 410 } |
412 | 411 |
413 | 412 |
414 Variable* Scope::LookupFunctionVar(const AstRawString* name, | 413 Variable* Scope::LookupFunctionVar(const AstRawString* name, |
415 AstNodeFactory<AstNullVisitor>* factory) { | 414 AstNodeFactory<AstNullVisitor>* factory) { |
416 if (function_ != NULL && function_->proxy()->raw_name() == name) { | 415 if (function_ != NULL && function_->proxy()->raw_name() == name) { |
417 return function_->proxy()->var(); | 416 return function_->proxy()->var(); |
418 } else if (!scope_info_.is_null()) { | 417 } else if (!scope_info_.is_null()) { |
(...skipping 20 matching lines...) Expand all Loading... |
439 for (Scope* scope = this; | 438 for (Scope* scope = this; |
440 scope != NULL; | 439 scope != NULL; |
441 scope = scope->outer_scope()) { | 440 scope = scope->outer_scope()) { |
442 Variable* var = scope->LookupLocal(name); | 441 Variable* var = scope->LookupLocal(name); |
443 if (var != NULL) return var; | 442 if (var != NULL) return var; |
444 } | 443 } |
445 return NULL; | 444 return NULL; |
446 } | 445 } |
447 | 446 |
448 | 447 |
449 void Scope::DeclareParameter(const AstRawString* name, VariableMode mode) { | 448 Variable* Scope::DeclareParameter(const AstRawString* name, VariableMode mode) { |
450 ASSERT(!already_resolved()); | 449 ASSERT(!already_resolved()); |
451 ASSERT(is_function_scope()); | 450 ASSERT(is_function_scope()); |
452 Variable* var = variables_.Declare(this, name, mode, true, Variable::NORMAL, | 451 Variable* var = variables_.Declare(this, name, mode, true, Variable::NORMAL, |
453 kCreatedInitialized); | 452 kCreatedInitialized); |
454 params_.Add(var, zone()); | 453 params_.Add(var, zone()); |
| 454 return var; |
455 } | 455 } |
456 | 456 |
457 | 457 |
458 Variable* Scope::DeclareLocal(const AstRawString* name, | 458 Variable* Scope::DeclareLocal(const AstRawString* name, VariableMode mode, |
459 VariableMode mode, | |
460 InitializationFlag init_flag, | 459 InitializationFlag init_flag, |
| 460 MaybeAssignedFlag maybe_assigned_flag, |
461 Interface* interface) { | 461 Interface* interface) { |
462 ASSERT(!already_resolved()); | 462 ASSERT(!already_resolved()); |
463 // This function handles VAR, LET, and CONST modes. DYNAMIC variables are | 463 // This function handles VAR, LET, and CONST modes. DYNAMIC variables are |
464 // introduces during variable allocation, INTERNAL variables are allocated | 464 // introduces during variable allocation, INTERNAL variables are allocated |
465 // explicitly, and TEMPORARY variables are allocated via NewTemporary(). | 465 // explicitly, and TEMPORARY variables are allocated via NewTemporary(). |
466 ASSERT(IsDeclaredVariableMode(mode)); | 466 ASSERT(IsDeclaredVariableMode(mode)); |
467 ++num_var_or_const_; | 467 ++num_var_or_const_; |
468 return variables_.Declare( | 468 return variables_.Declare(this, name, mode, true, Variable::NORMAL, init_flag, |
469 this, name, mode, true, Variable::NORMAL, init_flag, interface); | 469 maybe_assigned_flag, interface); |
470 } | 470 } |
471 | 471 |
472 | 472 |
473 Variable* Scope::DeclareDynamicGlobal(const AstRawString* name) { | 473 Variable* Scope::DeclareDynamicGlobal(const AstRawString* name) { |
474 ASSERT(is_global_scope()); | 474 ASSERT(is_global_scope()); |
475 return variables_.Declare(this, | 475 return variables_.Declare(this, |
476 name, | 476 name, |
477 DYNAMIC_GLOBAL, | 477 DYNAMIC_GLOBAL, |
478 true, | 478 true, |
479 Variable::NORMAL, | 479 Variable::NORMAL, |
(...skipping 338 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
818 PrintF(" "); | 818 PrintF(" "); |
819 PrintName(var->raw_name()); | 819 PrintName(var->raw_name()); |
820 PrintF("; // "); | 820 PrintF("; // "); |
821 PrintLocation(var); | 821 PrintLocation(var); |
822 bool comma = !var->IsUnallocated(); | 822 bool comma = !var->IsUnallocated(); |
823 if (var->has_forced_context_allocation()) { | 823 if (var->has_forced_context_allocation()) { |
824 if (comma) PrintF(", "); | 824 if (comma) PrintF(", "); |
825 PrintF("forced context allocation"); | 825 PrintF("forced context allocation"); |
826 comma = true; | 826 comma = true; |
827 } | 827 } |
828 if (var->maybe_assigned()) { | 828 if (var->maybe_assigned() == kMaybeAssigned) { |
829 if (comma) PrintF(", "); | 829 if (comma) PrintF(", "); |
830 PrintF("maybe assigned"); | 830 PrintF("maybe assigned"); |
831 } | 831 } |
832 PrintF("\n"); | 832 PrintF("\n"); |
833 } | 833 } |
834 } | 834 } |
835 | 835 |
836 | 836 |
837 static void PrintMap(int indent, VariableMap* map) { | 837 static void PrintMap(int indent, VariableMap* map) { |
838 for (VariableMap::Entry* p = map->Start(); p != NULL; p = map->Next(p)) { | 838 for (VariableMap::Entry* p = map->Start(); p != NULL; p = map->Next(p)) { |
(...skipping 555 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1394 } | 1394 } |
1395 | 1395 |
1396 | 1396 |
1397 int Scope::ContextLocalCount() const { | 1397 int Scope::ContextLocalCount() const { |
1398 if (num_heap_slots() == 0) return 0; | 1398 if (num_heap_slots() == 0) return 0; |
1399 return num_heap_slots() - Context::MIN_CONTEXT_SLOTS - | 1399 return num_heap_slots() - Context::MIN_CONTEXT_SLOTS - |
1400 (function_ != NULL && function_->proxy()->var()->IsContextSlot() ? 1 : 0); | 1400 (function_ != NULL && function_->proxy()->var()->IsContextSlot() ? 1 : 0); |
1401 } | 1401 } |
1402 | 1402 |
1403 } } // namespace v8::internal | 1403 } } // namespace v8::internal |
OLD | NEW |