Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(3)

Side by Side Diff: src/scopes.cc

Issue 981203003: Stack allocate lexical locals + hoist stack slots (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Feedback + rebased Created 5 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/scopeinfo.cc ('k') | test/mjsunit/bugs/harmony/debug-blockscopes.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/accessors.h" 7 #include "src/accessors.h"
8 #include "src/bootstrapper.h" 8 #include "src/bootstrapper.h"
9 #include "src/messages.h" 9 #include "src/messages.h"
10 #include "src/parser.h" 10 #include "src/parser.h"
(...skipping 353 matching lines...) Expand 10 before | Expand all | Expand 10 after
364 364
365 // Move unresolved variables 365 // Move unresolved variables
366 for (int i = 0; i < unresolved_.length(); i++) { 366 for (int i = 0; i < unresolved_.length(); i++) {
367 outer_scope()->unresolved_.Add(unresolved_[i], zone()); 367 outer_scope()->unresolved_.Add(unresolved_[i], zone());
368 } 368 }
369 369
370 // Propagate usage flags to outer scope. 370 // Propagate usage flags to outer scope.
371 if (uses_arguments()) outer_scope_->RecordArgumentsUsage(); 371 if (uses_arguments()) outer_scope_->RecordArgumentsUsage();
372 if (uses_super_property()) outer_scope_->RecordSuperPropertyUsage(); 372 if (uses_super_property()) outer_scope_->RecordSuperPropertyUsage();
373 if (uses_this()) outer_scope_->RecordThisUsage(); 373 if (uses_this()) outer_scope_->RecordThisUsage();
374 if (scope_calls_eval_) outer_scope_->RecordEvalCall();
374 375
375 return NULL; 376 return NULL;
376 } 377 }
377 378
378 379
379 Variable* Scope::LookupLocal(const AstRawString* name) { 380 Variable* Scope::LookupLocal(const AstRawString* name) {
380 Variable* result = variables_.Lookup(name); 381 Variable* result = variables_.Lookup(name);
381 if (result != NULL || scope_info_.is_null()) { 382 if (result != NULL || scope_info_.is_null()) {
382 return result; 383 return result;
383 } 384 }
385 Handle<String> name_handle = name->string();
384 // The Scope is backed up by ScopeInfo. This means it cannot operate in a 386 // The Scope is backed up by ScopeInfo. This means it cannot operate in a
385 // heap-independent mode, and all strings must be internalized immediately. So 387 // heap-independent mode, and all strings must be internalized immediately. So
386 // it's ok to get the Handle<String> here. 388 // it's ok to get the Handle<String> here.
387 Handle<String> name_handle = name->string();
388 // If we have a serialized scope info, we might find the variable there. 389 // If we have a serialized scope info, we might find the variable there.
389 // There should be no local slot with the given name. 390 // There should be no local slot with the given name.
390 DCHECK(scope_info_->StackSlotIndex(*name_handle) < 0); 391 DCHECK(scope_info_->StackSlotIndex(*name_handle) < 0 || is_block_scope());
391 392
392 // Check context slot lookup. 393 // Check context slot lookup.
393 VariableMode mode; 394 VariableMode mode;
394 Variable::Location location = Variable::CONTEXT; 395 Variable::Location location = Variable::CONTEXT;
395 InitializationFlag init_flag; 396 InitializationFlag init_flag;
396 MaybeAssignedFlag maybe_assigned_flag; 397 MaybeAssignedFlag maybe_assigned_flag;
397 int index = ScopeInfo::ContextSlotIndex(scope_info_, name_handle, &mode, 398 int index = ScopeInfo::ContextSlotIndex(scope_info_, name_handle, &mode,
398 &init_flag, &maybe_assigned_flag); 399 &init_flag, &maybe_assigned_flag);
399 if (index < 0) { 400 if (index < 0) {
400 // Check parameters. 401 // Check parameters.
(...skipping 298 matching lines...) Expand 10 before | Expand all | Expand 10 after
699 bool Scope::HasLazyCompilableOuterContext() const { 700 bool Scope::HasLazyCompilableOuterContext() const {
700 Scope* outer = outer_scope_; 701 Scope* outer = outer_scope_;
701 if (outer == NULL) return true; 702 if (outer == NULL) return true;
702 // We have to prevent lazy compilation if this scope is inside a with scope 703 // We have to prevent lazy compilation if this scope is inside a with scope
703 // and all declaration scopes between them have empty contexts. Such 704 // and all declaration scopes between them have empty contexts. Such
704 // declaration scopes may become invisible during scope info deserialization. 705 // declaration scopes may become invisible during scope info deserialization.
705 outer = outer->DeclarationScope(); 706 outer = outer->DeclarationScope();
706 bool found_non_trivial_declarations = false; 707 bool found_non_trivial_declarations = false;
707 for (const Scope* scope = outer; scope != NULL; scope = scope->outer_scope_) { 708 for (const Scope* scope = outer; scope != NULL; scope = scope->outer_scope_) {
708 if (scope->is_with_scope() && !found_non_trivial_declarations) return false; 709 if (scope->is_with_scope() && !found_non_trivial_declarations) return false;
710 if (scope->is_block_scope() && !scope->decls_.is_empty()) return false;
709 if (scope->is_declaration_scope() && scope->num_heap_slots() > 0) { 711 if (scope->is_declaration_scope() && scope->num_heap_slots() > 0) {
710 found_non_trivial_declarations = true; 712 found_non_trivial_declarations = true;
711 } 713 }
712 } 714 }
713 return true; 715 return true;
714 } 716 }
715 717
716 718
717 bool Scope::AllowsLazyCompilation() const { 719 bool Scope::AllowsLazyCompilation() const {
718 return !force_eager_compilation_ && HasLazyCompilableOuterContext(); 720 return !force_eager_compilation_ && HasLazyCompilableOuterContext();
(...skipping 562 matching lines...) Expand 10 before | Expand all | Expand 10 after
1281 // an eval() call or a runtime with lookup), it must be allocated in the 1283 // an eval() call or a runtime with lookup), it must be allocated in the
1282 // context. 1284 // context.
1283 // 1285 //
1284 // Exceptions: If the scope as a whole has forced context allocation, all 1286 // Exceptions: If the scope as a whole has forced context allocation, all
1285 // variables will have context allocation, even temporaries. Otherwise 1287 // variables will have context allocation, even temporaries. Otherwise
1286 // temporary variables are always stack-allocated. Catch-bound variables are 1288 // temporary variables are always stack-allocated. Catch-bound variables are
1287 // always context-allocated. 1289 // always context-allocated.
1288 if (has_forced_context_allocation()) return true; 1290 if (has_forced_context_allocation()) return true;
1289 if (var->mode() == TEMPORARY) return false; 1291 if (var->mode() == TEMPORARY) return false;
1290 if (var->mode() == INTERNAL) return true; 1292 if (var->mode() == INTERNAL) return true;
1291 if (is_catch_scope() || is_block_scope() || is_module_scope()) return true; 1293 if (is_catch_scope() || is_module_scope()) return true;
1292 if (is_script_scope() && IsLexicalVariableMode(var->mode())) return true; 1294 if (is_script_scope() && IsLexicalVariableMode(var->mode())) return true;
1293 return var->has_forced_context_allocation() || 1295 return var->has_forced_context_allocation() ||
1294 scope_calls_eval_ || 1296 scope_calls_eval_ ||
1295 inner_scope_calls_eval_ || 1297 inner_scope_calls_eval_ ||
1296 scope_contains_with_; 1298 scope_contains_with_;
1297 } 1299 }
1298 1300
1299 1301
1300 bool Scope::HasArgumentsParameter(Isolate* isolate) { 1302 bool Scope::HasArgumentsParameter(Isolate* isolate) {
1301 for (int i = 0; i < params_.length(); i++) { 1303 for (int i = 0; i < params_.length(); i++) {
1302 if (params_[i]->name().is_identical_to( 1304 if (params_[i]->name().is_identical_to(
1303 isolate->factory()->arguments_string())) { 1305 isolate->factory()->arguments_string())) {
1304 return true; 1306 return true;
1305 } 1307 }
1306 } 1308 }
1307 return false; 1309 return false;
1308 } 1310 }
1309 1311
1310 1312
1311 void Scope::AllocateStackSlot(Variable* var) { 1313 void Scope::AllocateStackSlot(Variable* var) {
1312 var->AllocateTo(Variable::LOCAL, num_stack_slots_++); 1314 if (is_block_scope()) {
1315 DeclarationScope()->AllocateStackSlot(var);
1316 } else {
1317 var->AllocateTo(Variable::LOCAL, num_stack_slots_++);
1318 }
1313 } 1319 }
1314 1320
1315 1321
1316 void Scope::AllocateHeapSlot(Variable* var) { 1322 void Scope::AllocateHeapSlot(Variable* var) {
1317 var->AllocateTo(Variable::CONTEXT, num_heap_slots_++); 1323 var->AllocateTo(Variable::CONTEXT, num_heap_slots_++);
1318 } 1324 }
1319 1325
1320 1326
1321 void Scope::AllocateParameterLocals(Isolate* isolate) { 1327 void Scope::AllocateParameterLocals(Isolate* isolate) {
1322 DCHECK(is_function_scope()); 1328 DCHECK(is_function_scope());
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after
1427 AllocateNonParameterLocal(isolate, function_->proxy()->var()); 1433 AllocateNonParameterLocal(isolate, function_->proxy()->var());
1428 } 1434 }
1429 1435
1430 if (rest_parameter_) { 1436 if (rest_parameter_) {
1431 AllocateNonParameterLocal(isolate, rest_parameter_); 1437 AllocateNonParameterLocal(isolate, rest_parameter_);
1432 } 1438 }
1433 } 1439 }
1434 1440
1435 1441
1436 void Scope::AllocateVariablesRecursively(Isolate* isolate) { 1442 void Scope::AllocateVariablesRecursively(Isolate* isolate) {
1443 if (!already_resolved()) {
1444 num_stack_slots_ = 0;
1445 }
1437 // Allocate variables for inner scopes. 1446 // Allocate variables for inner scopes.
1438 for (int i = 0; i < inner_scopes_.length(); i++) { 1447 for (int i = 0; i < inner_scopes_.length(); i++) {
1439 inner_scopes_[i]->AllocateVariablesRecursively(isolate); 1448 inner_scopes_[i]->AllocateVariablesRecursively(isolate);
1440 } 1449 }
1441 1450
1442 // If scope is already resolved, we still need to allocate 1451 // If scope is already resolved, we still need to allocate
1443 // variables in inner scopes which might not had been resolved yet. 1452 // variables in inner scopes which might not had been resolved yet.
1444 if (already_resolved()) return; 1453 if (already_resolved()) return;
1445 // The number of slots required for variables. 1454 // The number of slots required for variables.
1446 num_stack_slots_ = 0;
1447 num_heap_slots_ = Context::MIN_CONTEXT_SLOTS; 1455 num_heap_slots_ = Context::MIN_CONTEXT_SLOTS;
1448 1456
1449 // Allocate variables for this scope. 1457 // Allocate variables for this scope.
1450 // Parameters must be allocated first, if any. 1458 // Parameters must be allocated first, if any.
1451 if (is_function_scope()) AllocateParameterLocals(isolate); 1459 if (is_function_scope()) AllocateParameterLocals(isolate);
1452 AllocateNonParameterLocals(isolate); 1460 AllocateNonParameterLocals(isolate);
1453 1461
1454 // Force allocation of a context for this scope if necessary. For a 'with' 1462 // Force allocation of a context for this scope if necessary. For a 'with'
1455 // scope and for a function scope that makes an 'eval' call we need a context, 1463 // scope and for a function scope that makes an 'eval' call we need a context,
1456 // even if no local variables were statically allocated in the scope. 1464 // even if no local variables were statically allocated in the scope.
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
1490 (function_ != NULL && function_->proxy()->var()->IsStackLocal() ? 1 : 0); 1498 (function_ != NULL && function_->proxy()->var()->IsStackLocal() ? 1 : 0);
1491 } 1499 }
1492 1500
1493 1501
1494 int Scope::ContextLocalCount() const { 1502 int Scope::ContextLocalCount() const {
1495 if (num_heap_slots() == 0) return 0; 1503 if (num_heap_slots() == 0) return 0;
1496 return num_heap_slots() - Context::MIN_CONTEXT_SLOTS - 1504 return num_heap_slots() - Context::MIN_CONTEXT_SLOTS -
1497 (function_ != NULL && function_->proxy()->var()->IsContextSlot() ? 1 : 0); 1505 (function_ != NULL && function_->proxy()->var()->IsContextSlot() ? 1 : 0);
1498 } 1506 }
1499 } } // namespace v8::internal 1507 } } // namespace v8::internal
OLDNEW
« no previous file with comments | « src/scopeinfo.cc ('k') | test/mjsunit/bugs/harmony/debug-blockscopes.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698