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

Side by Side Diff: src/parsing/parser.cc

Issue 2193793002: Put Scopes into temporary Zone (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: ditto Created 4 years, 4 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
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/parsing/parser.h" 5 #include "src/parsing/parser.h"
6 6
7 #include <memory> 7 #include <memory>
8 8
9 #include "src/api.h" 9 #include "src/api.h"
10 #include "src/ast/ast.h" 10 #include "src/ast/ast.h"
(...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after
169 } 169 }
170 170
171 // Helper for putting parts of the parse results into a temporary zone when 171 // Helper for putting parts of the parse results into a temporary zone when
172 // parsing inner function bodies. 172 // parsing inner function bodies.
173 class DiscardableZoneScope { 173 class DiscardableZoneScope {
174 public: 174 public:
175 DiscardableZoneScope(Parser* parser, Zone* temp_zone, bool use_temp_zone) 175 DiscardableZoneScope(Parser* parser, Zone* temp_zone, bool use_temp_zone)
176 : ast_node_factory_scope_(parser->factory(), temp_zone, use_temp_zone), 176 : ast_node_factory_scope_(parser->factory(), temp_zone, use_temp_zone),
177 fni_(parser->ast_value_factory_, temp_zone), 177 fni_(parser->ast_value_factory_, temp_zone),
178 parser_(parser), 178 parser_(parser),
179 prev_fni_(parser->fni_) { 179 prev_fni_(parser->fni_),
180 prev_zone_(parser->zone_) {
180 if (use_temp_zone) { 181 if (use_temp_zone) {
181 parser_->fni_ = &fni_; 182 parser_->fni_ = &fni_;
183 parser_->zone_ = temp_zone;
182 } 184 }
183 } 185 }
184 ~DiscardableZoneScope() { parser_->fni_ = prev_fni_; } 186 ~DiscardableZoneScope() {
187 parser_->fni_ = prev_fni_;
188 parser_->zone_ = prev_zone_;
189 }
185 190
186 private: 191 private:
187 AstNodeFactory::BodyScope ast_node_factory_scope_; 192 AstNodeFactory::BodyScope ast_node_factory_scope_;
188 FuncNameInferrer fni_; 193 FuncNameInferrer fni_;
189 Parser* parser_; 194 Parser* parser_;
190 FuncNameInferrer* prev_fni_; 195 FuncNameInferrer* prev_fni_;
196 Zone* prev_zone_;
197
198 DISALLOW_COPY_AND_ASSIGN(DiscardableZoneScope);
191 }; 199 };
192 200
193 void Parser::SetCachedData(ParseInfo* info) { 201 void Parser::SetCachedData(ParseInfo* info) {
194 if (compile_options_ == ScriptCompiler::kNoCompileOptions) { 202 if (compile_options_ == ScriptCompiler::kNoCompileOptions) {
195 cached_parse_data_ = NULL; 203 cached_parse_data_ = NULL;
196 } else { 204 } else {
197 DCHECK(info->cached_data() != NULL); 205 DCHECK(info->cached_data() != NULL);
198 if (compile_options_ == ScriptCompiler::kConsumeParserCache) { 206 if (compile_options_ == ScriptCompiler::kConsumeParserCache) {
199 cached_parse_data_ = ParseData::FromCachedData(*info->cached_data()); 207 cached_parse_data_ = ParseData::FromCachedData(*info->cached_data());
200 } 208 }
(...skipping 4097 matching lines...) Expand 10 before | Expand all | Expand 10 after
4298 // Anonymous functions were passed either the empty symbol or a null 4306 // Anonymous functions were passed either the empty symbol or a null
4299 // handle as the function name. Remember if we were passed a non-empty 4307 // handle as the function name. Remember if we were passed a non-empty
4300 // handle to decide whether to invoke function name inference. 4308 // handle to decide whether to invoke function name inference.
4301 bool should_infer_name = function_name == NULL; 4309 bool should_infer_name = function_name == NULL;
4302 4310
4303 // We want a non-null handle as the function name. 4311 // We want a non-null handle as the function name.
4304 if (should_infer_name) { 4312 if (should_infer_name) {
4305 function_name = ast_value_factory()->empty_string(); 4313 function_name = ast_value_factory()->empty_string();
4306 } 4314 }
4307 4315
4308 Scope* scope = NewFunctionScope(kind); 4316 FunctionLiteral::EagerCompileHint eager_compile_hint =
4309 SetLanguageMode(scope, language_mode); 4317 function_state_->next_function_is_parenthesized()
4310 ZoneList<Statement*>* body = NULL; 4318 ? FunctionLiteral::kShouldEagerCompile
4319 : FunctionLiteral::kShouldLazyCompile;
4320
4321 // Determine if the function can be parsed lazily. Lazy parsing is
4322 // different from lazy compilation; we need to parse more eagerly than we
4323 // compile.
4324
4325 // We can only parse lazily if we also compile lazily. The heuristics for lazy
4326 // compilation are:
4327 // - It must not have been prohibited by the caller to Parse (some callers
4328 // need a full AST).
4329 // - The outer scope must allow lazy compilation of inner functions.
4330 // - The function mustn't be a function expression with an open parenthesis
4331 // before; we consider that a hint that the function will be called
4332 // immediately, and it would be a waste of time to make it lazily
4333 // compiled.
4334 // These are all things we can know at this point, without looking at the
4335 // function itself.
4336
4337 // In addition, we need to distinguish between these cases:
4338 // (function foo() {
4339 // bar = function() { return 1; }
4340 // })();
4341 // and
4342 // (function foo() {
4343 // var a = 1;
4344 // bar = function() { return a; }
4345 // })();
4346
4347 // Now foo will be parsed eagerly and compiled eagerly (optimization: assume
4348 // parenthesis before the function means that it will be called
4349 // immediately). The inner function *must* be parsed eagerly to resolve the
4350 // possible reference to the variable in foo's scope. However, it's possible
4351 // that it will be compiled lazily.
4352
4353 // To make this additional case work, both Parser and PreParser implement a
4354 // logic where only top-level functions will be parsed lazily.
4355 bool is_lazily_parsed = mode() == PARSE_LAZILY &&
4356 this->scope()->AllowsLazyParsing() &&
4357 !function_state_->next_function_is_parenthesized();
4358
4359 // Determine whether the function body can be discarded after parsing.
4360 // The preconditions are:
4361 // - Lazy compilation has to be enabled.
4362 // - Neither V8 natives nor native function declarations can be allowed,
4363 // since parsing one would retroactively force the function to be
4364 // eagerly compiled.
4365 // - The invoker of this parser can't depend on the AST being eagerly
4366 // built (either because the function is about to be compiled, or
4367 // because the AST is going to be inspected for some reason).
4368 // - Because of the above, we can't be attempting to parse a
4369 // FunctionExpression; even without enclosing parentheses it might be
4370 // immediately invoked.
4371 // - The function literal shouldn't be hinted to eagerly compile.
4372 // - For asm.js functions the body needs to be available when module
4373 // validation is active, because we examine the entire module at once.
4374 bool use_temp_zone =
4375 !is_lazily_parsed && FLAG_lazy && !allow_natives() &&
4376 extension_ == NULL && allow_lazy() &&
4377 function_type == FunctionLiteral::kDeclaration &&
4378 eager_compile_hint != FunctionLiteral::kShouldEagerCompile &&
4379 !(FLAG_validate_asm && scope()->asm_module());
4380
4381 Scope* main_scope = nullptr;
4382 if (use_temp_zone) {
4383 // This Scope lives in the main Zone; we'll migrate data into it later.
4384 main_scope = NewFunctionScope(kind);
4385 }
4386
4387 ZoneList<Statement*>* body = nullptr;
4311 int arity = -1; 4388 int arity = -1;
4312 int materialized_literal_count = -1; 4389 int materialized_literal_count = -1;
4313 int expected_property_count = -1; 4390 int expected_property_count = -1;
4314 DuplicateFinder duplicate_finder(scanner()->unicode_cache()); 4391 DuplicateFinder duplicate_finder(scanner()->unicode_cache());
4315 bool should_be_used_once_hint = false; 4392 bool should_be_used_once_hint = false;
4316 bool has_duplicate_parameters; 4393 bool has_duplicate_parameters;
4317 FunctionLiteral::EagerCompileHint eager_compile_hint;
4318 4394
4319 // Parse function.
4320 { 4395 {
4396 // Temporary zones can nest. When we migrate free variables (see below), we
4397 // need to recreate them in the previous Zone.
4398 AstNodeFactory previous_zone_ast_node_factory(ast_value_factory());
4399 previous_zone_ast_node_factory.set_zone(zone());
4400
4401 // Open a new zone scope, which sets our AstNodeFactory to allocate in the
4402 // new temporary zone if the preconditions are satisfied, and ensures that
4403 // the previous zone is always restored after parsing the body. To be able
4404 // to do scope analysis correctly after full parsing, we migrate needed
4405 // information from scope into main_scope when the function has been parsed.
4406 Zone temp_zone(zone()->allocator());
4407 DiscardableZoneScope zone_scope(this, &temp_zone, use_temp_zone);
4408
4409 Scope* scope = NewFunctionScope(kind);
4410 SetLanguageMode(scope, language_mode);
4411 if (!use_temp_zone) {
4412 main_scope = scope;
4413 } else {
4414 DCHECK(main_scope->zone() != scope->zone());
4415 }
4416
4321 FunctionState function_state(&function_state_, &scope_state_, scope, kind); 4417 FunctionState function_state(&function_state_, &scope_state_, scope, kind);
4322 this->scope()->SetScopeName(function_name); 4418 this->scope()->SetScopeName(function_name);
4323 ExpressionClassifier formals_classifier(this, &duplicate_finder); 4419 ExpressionClassifier formals_classifier(this, &duplicate_finder);
4324 4420
4325 eager_compile_hint = function_state_->this_function_is_parenthesized()
4326 ? FunctionLiteral::kShouldEagerCompile
4327 : FunctionLiteral::kShouldLazyCompile;
4328
4329 if (is_generator) { 4421 if (is_generator) {
4330 // For generators, allocating variables in contexts is currently a win 4422 // For generators, allocating variables in contexts is currently a win
4331 // because it minimizes the work needed to suspend and resume an 4423 // because it minimizes the work needed to suspend and resume an
4332 // activation. The machine code produced for generators (by full-codegen) 4424 // activation. The machine code produced for generators (by full-codegen)
4333 // relies on this forced context allocation, but not in an essential way. 4425 // relies on this forced context allocation, but not in an essential way.
4334 this->scope()->ForceContextAllocation(); 4426 this->scope()->ForceContextAllocation();
4335 4427
4336 // Calling a generator returns a generator object. That object is stored 4428 // Calling a generator returns a generator object. That object is stored
4337 // in a temporary variable, a definition that is used by "yield" 4429 // in a temporary variable, a definition that is used by "yield"
4338 // expressions. This also marks the FunctionState as a generator. 4430 // expressions. This also marks the FunctionState as a generator.
(...skipping 12 matching lines...) Expand all
4351 int formals_end_position = scanner()->location().end_pos; 4443 int formals_end_position = scanner()->location().end_pos;
4352 4444
4353 CheckArityRestrictions(arity, kind, formals.has_rest, start_position, 4445 CheckArityRestrictions(arity, kind, formals.has_rest, start_position,
4354 formals_end_position, CHECK_OK); 4446 formals_end_position, CHECK_OK);
4355 Expect(Token::LBRACE, CHECK_OK); 4447 Expect(Token::LBRACE, CHECK_OK);
4356 // Don't include the rest parameter into the function's formal parameter 4448 // Don't include the rest parameter into the function's formal parameter
4357 // count (esp. the SharedFunctionInfo::internal_formal_parameter_count, 4449 // count (esp. the SharedFunctionInfo::internal_formal_parameter_count,
4358 // which says whether we need to create an arguments adaptor frame). 4450 // which says whether we need to create an arguments adaptor frame).
4359 if (formals.has_rest) arity--; 4451 if (formals.has_rest) arity--;
4360 4452
4361 // Determine if the function can be parsed lazily. Lazy parsing is different
4362 // from lazy compilation; we need to parse more eagerly than we compile.
4363
4364 // We can only parse lazily if we also compile lazily. The heuristics for
4365 // lazy compilation are:
4366 // - It must not have been prohibited by the caller to Parse (some callers
4367 // need a full AST).
4368 // - The outer scope must allow lazy compilation of inner functions.
4369 // - The function mustn't be a function expression with an open parenthesis
4370 // before; we consider that a hint that the function will be called
4371 // immediately, and it would be a waste of time to make it lazily
4372 // compiled.
4373 // These are all things we can know at this point, without looking at the
4374 // function itself.
4375
4376 // In addition, we need to distinguish between these cases:
4377 // (function foo() {
4378 // bar = function() { return 1; }
4379 // })();
4380 // and
4381 // (function foo() {
4382 // var a = 1;
4383 // bar = function() { return a; }
4384 // })();
4385
4386 // Now foo will be parsed eagerly and compiled eagerly (optimization: assume
4387 // parenthesis before the function means that it will be called
4388 // immediately). The inner function *must* be parsed eagerly to resolve the
4389 // possible reference to the variable in foo's scope. However, it's possible
4390 // that it will be compiled lazily.
4391
4392 // To make this additional case work, both Parser and PreParser implement a
4393 // logic where only top-level functions will be parsed lazily.
4394 bool is_lazily_parsed = mode() == PARSE_LAZILY &&
4395 this->scope()->AllowsLazyParsing() &&
4396 !function_state_->this_function_is_parenthesized();
4397
4398 // Eager or lazy parse? 4453 // Eager or lazy parse?
4399 // If is_lazily_parsed, we'll parse lazy. If we can set a bookmark, we'll 4454 // If is_lazily_parsed, we'll parse lazy. If we can set a bookmark, we'll
4400 // pass it to SkipLazyFunctionBody, which may use it to abort lazy 4455 // pass it to SkipLazyFunctionBody, which may use it to abort lazy
4401 // parsing if it suspect that wasn't a good idea. If so, or if we didn't 4456 // parsing if it suspect that wasn't a good idea. If so, or if we didn't
4402 // try to lazy parse in the first place, we'll have to parse eagerly. 4457 // try to lazy parse in the first place, we'll have to parse eagerly.
4403 Scanner::BookmarkScope bookmark(scanner()); 4458 Scanner::BookmarkScope bookmark(scanner());
4404 if (is_lazily_parsed) { 4459 if (is_lazily_parsed) {
4405 Scanner::BookmarkScope* maybe_bookmark = 4460 Scanner::BookmarkScope* maybe_bookmark =
4406 bookmark.Set() ? &bookmark : nullptr; 4461 bookmark.Set() ? &bookmark : nullptr;
4407 SkipLazyFunctionBody(&materialized_literal_count, 4462 SkipLazyFunctionBody(&materialized_literal_count,
4408 &expected_property_count, /*CHECK_OK*/ ok, 4463 &expected_property_count, /*CHECK_OK*/ ok,
4409 maybe_bookmark); 4464 maybe_bookmark);
4410 4465
4411 materialized_literal_count += formals.materialized_literals_count + 4466 materialized_literal_count += formals.materialized_literals_count +
4412 function_state.materialized_literal_count(); 4467 function_state.materialized_literal_count();
4413 4468
4414 if (bookmark.HasBeenReset()) { 4469 if (bookmark.HasBeenReset()) {
4415 // Trigger eager (re-)parsing, just below this block. 4470 // Trigger eager (re-)parsing, just below this block.
4416 is_lazily_parsed = false; 4471 is_lazily_parsed = false;
4417 4472
4418 // This is probably an initialization function. Inform the compiler it 4473 // This is probably an initialization function. Inform the compiler it
4419 // should also eager-compile this function, and that we expect it to be 4474 // should also eager-compile this function, and that we expect it to be
4420 // used once. 4475 // used once.
4421 eager_compile_hint = FunctionLiteral::kShouldEagerCompile; 4476 eager_compile_hint = FunctionLiteral::kShouldEagerCompile;
4422 should_be_used_once_hint = true; 4477 should_be_used_once_hint = true;
4423 } 4478 }
4424 } 4479 }
4425 if (!is_lazily_parsed) { 4480 if (!is_lazily_parsed) {
4426 // Determine whether the function body can be discarded after parsing. 4481 body = ParseEagerFunctionBody(function_name, pos, formals, kind,
4427 // The preconditions are: 4482 function_type, CHECK_OK);
4428 // - Lazy compilation has to be enabled. 4483
4429 // - Neither V8 natives nor native function declarations can be allowed,
4430 // since parsing one would retroactively force the function to be
4431 // eagerly compiled.
4432 // - The invoker of this parser can't depend on the AST being eagerly
4433 // built (either because the function is about to be compiled, or
4434 // because the AST is going to be inspected for some reason).
4435 // - Because of the above, we can't be attempting to parse a
4436 // FunctionExpression; even without enclosing parentheses it might be
4437 // immediately invoked.
4438 // - The function literal shouldn't be hinted to eagerly compile.
4439 // - For asm.js functions the body needs to be available when module
4440 // validation is active, because we examine the entire module at once.
4441 bool use_temp_zone =
4442 FLAG_lazy && !allow_natives() && extension_ == NULL && allow_lazy() &&
4443 function_type == FunctionLiteral::kDeclaration &&
4444 eager_compile_hint != FunctionLiteral::kShouldEagerCompile &&
4445 !(FLAG_validate_asm && scope->asm_function());
4446 // Open a new zone scope, which sets our AstNodeFactory to allocate in the
4447 // new temporary zone if the preconditions are satisfied, and ensures that
4448 // the previous zone is always restored after parsing the body.
4449 // For the purpose of scope analysis, some ZoneObjects allocated by the
4450 // factory must persist after the function body is thrown away and
4451 // temp_zone is deallocated. These objects are instead allocated in a
4452 // parser-persistent zone (see parser_zone_ in AstNodeFactory).
4453 {
4454 Zone temp_zone(zone()->allocator());
4455 DiscardableZoneScope zone_scope(this, &temp_zone, use_temp_zone);
4456 body = ParseEagerFunctionBody(function_name, pos, formals, kind,
4457 function_type, CHECK_OK);
4458 }
4459 materialized_literal_count = function_state.materialized_literal_count(); 4484 materialized_literal_count = function_state.materialized_literal_count();
4460 expected_property_count = function_state.expected_property_count(); 4485 expected_property_count = function_state.expected_property_count();
4461 if (use_temp_zone) { 4486 if (use_temp_zone) {
4462 // If the preconditions are correct the function body should never be 4487 // If the preconditions are correct the function body should never be
4463 // accessed, but do this anyway for better behaviour if they're wrong. 4488 // accessed, but do this anyway for better behaviour if they're wrong.
4464 body = NULL; 4489 body = nullptr;
4465 } 4490 }
4466 } 4491 }
4467 4492
4468 // Parsing the body may change the language mode in our scope. 4493 // Parsing the body may change the language mode in our scope.
4469 language_mode = scope->language_mode(); 4494 language_mode = scope->language_mode();
4470 4495
4471 // Validate name and parameter names. We can do this only after parsing the 4496 // Validate name and parameter names. We can do this only after parsing the
4472 // function, since the function can declare itself strict. 4497 // function, since the function can declare itself strict.
4473 CheckFunctionName(language_mode, function_name, function_name_validity, 4498 CheckFunctionName(language_mode, function_name, function_name_validity,
4474 function_name_location, CHECK_OK); 4499 function_name_location, CHECK_OK);
4475 const bool allow_duplicate_parameters = 4500 const bool allow_duplicate_parameters =
4476 is_sloppy(language_mode) && formals.is_simple && !IsConciseMethod(kind); 4501 is_sloppy(language_mode) && formals.is_simple && !IsConciseMethod(kind);
4477 ValidateFormalParameters(&formals_classifier, language_mode, 4502 ValidateFormalParameters(&formals_classifier, language_mode,
4478 allow_duplicate_parameters, CHECK_OK); 4503 allow_duplicate_parameters, CHECK_OK);
4479 4504
4480 if (is_strict(language_mode)) { 4505 if (is_strict(language_mode)) {
4481 CheckStrictOctalLiteral(scope->start_position(), scope->end_position(), 4506 CheckStrictOctalLiteral(scope->start_position(), scope->end_position(),
4482 CHECK_OK); 4507 CHECK_OK);
4483 CheckDecimalLiteralWithLeadingZero(use_counts_, scope->start_position(), 4508 CheckDecimalLiteralWithLeadingZero(use_counts_, scope->start_position(),
4484 scope->end_position()); 4509 scope->end_position());
4485 } 4510 }
4486 CheckConflictingVarDeclarations(scope, CHECK_OK); 4511 CheckConflictingVarDeclarations(scope, CHECK_OK);
4487 4512
4488 if (body) { 4513 if (body) {
4489 // If body can be inspected, rewrite queued destructuring assignments 4514 // If body can be inspected, rewrite queued destructuring assignments
4490 ParserTraits::RewriteDestructuringAssignments(); 4515 ParserTraits::RewriteDestructuringAssignments();
4491 } 4516 }
4492 has_duplicate_parameters = 4517 has_duplicate_parameters =
4493 !formals_classifier.is_valid_formal_parameter_list_without_duplicates(); 4518 !formals_classifier.is_valid_formal_parameter_list_without_duplicates();
4494 } 4519
4520 if (use_temp_zone) {
4521 DCHECK(main_scope != scope);
4522 scope->AnalyzePartially(main_scope, &previous_zone_ast_node_factory);
4523 }
4524 } // DiscardableZoneScope goes out of scope.
4495 4525
4496 FunctionLiteral::ParameterFlag duplicate_parameters = 4526 FunctionLiteral::ParameterFlag duplicate_parameters =
4497 has_duplicate_parameters ? FunctionLiteral::kHasDuplicateParameters 4527 has_duplicate_parameters ? FunctionLiteral::kHasDuplicateParameters
4498 : FunctionLiteral::kNoDuplicateParameters; 4528 : FunctionLiteral::kNoDuplicateParameters;
4499 4529
4530 // Note that the FunctionLiteral needs to be created in the main Zone again.
4500 FunctionLiteral* function_literal = factory()->NewFunctionLiteral( 4531 FunctionLiteral* function_literal = factory()->NewFunctionLiteral(
4501 function_name, scope, body, materialized_literal_count, 4532 function_name, main_scope, body, materialized_literal_count,
4502 expected_property_count, arity, duplicate_parameters, function_type, 4533 expected_property_count, arity, duplicate_parameters, function_type,
4503 eager_compile_hint, kind, pos); 4534 eager_compile_hint, kind, pos);
4504 function_literal->set_function_token_position(function_token_pos); 4535 function_literal->set_function_token_position(function_token_pos);
4505 if (should_be_used_once_hint) 4536 if (should_be_used_once_hint)
4506 function_literal->set_should_be_used_once_hint(); 4537 function_literal->set_should_be_used_once_hint();
4507 4538
4508 if (fni_ != NULL && should_infer_name) fni_->AddFunction(function_literal); 4539 if (fni_ != NULL && should_infer_name) fni_->AddFunction(function_literal);
4509 return function_literal; 4540 return function_literal;
4510 } 4541 }
4511 4542
(...skipping 2588 matching lines...) Expand 10 before | Expand all | Expand 10 after
7100 node->Print(Isolate::Current()); 7131 node->Print(Isolate::Current());
7101 } 7132 }
7102 #endif // DEBUG 7133 #endif // DEBUG
7103 7134
7104 #undef CHECK_OK 7135 #undef CHECK_OK
7105 #undef CHECK_OK_VOID 7136 #undef CHECK_OK_VOID
7106 #undef CHECK_FAILED 7137 #undef CHECK_FAILED
7107 7138
7108 } // namespace internal 7139 } // namespace internal
7109 } // namespace v8 7140 } // namespace v8
OLDNEW
« src/ast/scopes.cc ('K') | « src/parsing/parser.h ('k') | src/parsing/parser-base.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698