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

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