From: marja@chromium.org Date: Thu, 13 Feb 2014 16:17:55 +0000 (+0000) Subject: (Pre)Parser: Move FunctionState, BlockState and Scope handling to ParserBase. X-Git-Tag: upstream/4.7.83~10722 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=cad32c1917046d515e761a8f53d84e03500ee5b3;p=platform%2Fupstream%2Fv8.git (Pre)Parser: Move FunctionState, BlockState and Scope handling to ParserBase. Notes: - This removes Parser::FunctionState and PreParser::FunctionState and adds ParserBase::FunctionState etc. - Also the scope stacks and function state stacks are moved to ParserBase. - PreParser::FunctionState didn't add and subtract JSFunction::kLiteralsPrefixSize (unlike Parser::FunctionState). Since the actual value of NextMaterializedLiteralIndex is not used in the Preparser, this change is valid. - Traits no longer need functions like is_classic_mode(), since now there is a unified way of getting the information from the FunctionState / Scope. R=ulan@chromium.org BUG=v8:3126 LOG=N Review URL: https://codereview.chromium.org/135213007 git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@19361 ce2b1a6d-e550-0410-aec6-3dcde31c8c00 --- diff --git a/src/parser.cc b/src/parser.cc index c67cd8b..fb1585e 100644 --- a/src/parser.cc +++ b/src/parser.cc @@ -463,58 +463,6 @@ class TargetScope BASE_EMBEDDED { // ---------------------------------------------------------------------------- -// FunctionState and BlockState together implement the parser's scope stack. -// The parser's current scope is in scope_. The BlockState and -// FunctionState constructors push on the scope stack and the destructors -// pop. They are also used to hold the parser's per-function and per-block -// state. - -class Parser::BlockState BASE_EMBEDDED { - public: - BlockState(Scope** scope_stack, Scope* scope) - : scope_stack_(scope_stack), - outer_scope_(*scope_stack) { - *scope_stack = scope; - } - - ~BlockState() { *scope_stack_ = outer_scope_; } - - private: - Scope** scope_stack_; - Scope* outer_scope_; -}; - - -Parser::FunctionState::FunctionState(FunctionState** function_state_stack, - Scope** scope_stack, Scope* scope, - Zone* zone) - : next_materialized_literal_index_(JSFunction::kLiteralsPrefixSize), - next_handler_index_(0), - expected_property_count_(0), - generator_object_variable_(NULL), - function_state_stack_(function_state_stack), - outer_function_state_(*function_state_stack), - scope_stack_(scope_stack), - outer_scope_(*scope_stack), - isolate_(zone->isolate()), - saved_ast_node_id_(isolate_->ast_node_id()), - factory_(zone) { - *scope_stack_ = scope; - *function_state_stack = this; - isolate_->set_ast_node_id(BailoutId::FirstUsable().ToInt()); -} - - -Parser::FunctionState::~FunctionState() { - *scope_stack_ = outer_scope_; - *function_state_stack_ = outer_function_state_; - if (outer_function_state_ != NULL) { - isolate_->set_ast_node_id(saved_ast_node_id_); - } -} - - -// ---------------------------------------------------------------------------- // The CHECK_OK macro is a convenient macro to enforce error // handling for functions that may fail (by returning !*ok). // @@ -537,16 +485,6 @@ Parser::FunctionState::~FunctionState() { // ---------------------------------------------------------------------------- // Implementation of Parser -bool ParserTraits::is_classic_mode() const { - return parser_->scope_->is_classic_mode(); -} - - -bool ParserTraits::is_generator() const { - return parser_->function_state_->is_generator(); -} - - bool ParserTraits::IsEvalOrArguments(Handle identifier) const { return identifier.is_identical_to( parser_->isolate()->factory()->eval_string()) || @@ -555,11 +493,6 @@ bool ParserTraits::IsEvalOrArguments(Handle identifier) const { } -int ParserTraits::NextMaterializedLiteralIndex() { - return parser_->function_state_->NextMaterializedLiteralIndex(); -} - - void ParserTraits::ReportMessageAt(Scanner::Location source_location, const char* message, Vector args) { @@ -623,14 +556,6 @@ Handle ParserTraits::NextLiteralString(PretenureFlag tenured) { } -Expression* ParserTraits::NewRegExpLiteral(Handle js_pattern, - Handle js_flags, - int literal_index, - int pos) { - return parser_->factory()->NewRegExpLiteral( - js_pattern, js_flags, literal_index, pos); -} - Parser::Parser(CompilationInfo* info) : ParserBase(&scanner_, info->isolate()->stack_guard()->real_climit(), @@ -640,9 +565,7 @@ Parser::Parser(CompilationInfo* info) script_(info->script()), scanner_(isolate_->unicode_cache()), reusable_preparser_(NULL), - scope_(NULL), original_scope_(NULL), - function_state_(NULL), target_stack_(NULL), extension_(info->extension()), pre_parse_data_(NULL), @@ -4066,8 +3989,7 @@ FunctionLiteral* Parser::ParseFunctionLiteral( // Calling a generator returns a generator object. That object is stored // in a temporary variable, a definition that is used by "yield" - // expressions. Presence of a variable for the generator object in the - // FunctionState indicates that this function is a generator. + // expressions. This also marks the FunctionState as a generator. Variable* temp = scope_->DeclarationScope()->NewTemporary( isolate()->factory()->dot_generator_object_string()); function_state.set_generator_object_variable(temp); diff --git a/src/parser.h b/src/parser.h index de99425..ddd015c 100644 --- a/src/parser.h +++ b/src/parser.h @@ -409,18 +409,41 @@ class SingletonLogger; class ParserTraits { public: - typedef Parser* ParserType; - // Return types for traversing functions. - typedef Handle IdentifierType; - typedef Expression* ExpressionType; + struct Type { + typedef v8::internal::Parser* Parser; + + // Types used by FunctionState and BlockState. + typedef v8::internal::Scope Scope; + typedef AstNodeFactory Factory; + typedef Variable GeneratorVariable; + typedef v8::internal::Zone Zone; + + // Return types for traversing functions. + typedef Handle Identifier; + typedef v8::internal::Expression* Expression; + }; explicit ParserTraits(Parser* parser) : parser_(parser) {} + // Custom operations executed when FunctionStates are created and destructed. + template + static void SetUpFunctionState(FS* function_state, Zone* zone) { + Isolate* isolate = zone->isolate(); + function_state->isolate_ = isolate; + function_state->saved_ast_node_id_ = isolate->ast_node_id(); + isolate->set_ast_node_id(BailoutId::FirstUsable().ToInt()); + } + + template + static void TearDownFunctionState(FS* function_state) { + if (function_state->outer_function_state_ != NULL) { + function_state->isolate_->set_ast_node_id( + function_state->saved_ast_node_id_); + } + } + // Helper functions for recursive descent. - bool is_classic_mode() const; - bool is_generator() const; bool IsEvalOrArguments(Handle identifier) const; - int NextMaterializedLiteralIndex(); // Reporting errors. void ReportMessageAt(Scanner::Location source_location, @@ -432,20 +455,16 @@ class ParserTraits { Vector > args); // "null" return type creators. - static IdentifierType EmptyIdentifier() { + static Handle EmptyIdentifier() { return Handle(); } - static ExpressionType EmptyExpression() { + static Expression* EmptyExpression() { return NULL; } // Producing data during the recursive descent. - IdentifierType GetSymbol(); - IdentifierType NextLiteralString(PretenureFlag tenured); - ExpressionType NewRegExpLiteral(IdentifierType js_pattern, - IdentifierType js_flags, - int literal_index, - int pos); + Handle GetSymbol(); + Handle NextLiteralString(PretenureFlag tenured); private: Parser* parser_; @@ -494,68 +513,6 @@ class Parser : public ParserBase { kHasNoInitializers }; - class BlockState; - - class FunctionState BASE_EMBEDDED { - public: - FunctionState(FunctionState** function_state_stack, - Scope** scope_stack, Scope* scope, - Zone* zone); - ~FunctionState(); - - int NextMaterializedLiteralIndex() { - return next_materialized_literal_index_++; - } - int materialized_literal_count() { - return next_materialized_literal_index_ - JSFunction::kLiteralsPrefixSize; - } - - int NextHandlerIndex() { return next_handler_index_++; } - int handler_count() { return next_handler_index_; } - - void AddProperty() { expected_property_count_++; } - int expected_property_count() { return expected_property_count_; } - - void set_generator_object_variable(Variable *variable) { - ASSERT(variable != NULL); - ASSERT(!is_generator()); - generator_object_variable_ = variable; - } - Variable* generator_object_variable() const { - return generator_object_variable_; - } - bool is_generator() const { - return generator_object_variable_ != NULL; - } - - AstNodeFactory* factory() { return &factory_; } - - private: - // Used to assign an index to each literal that needs materialization in - // the function. Includes regexp literals, and boilerplate for object and - // array literals. - int next_materialized_literal_index_; - - // Used to assign a per-function index to try and catch handlers. - int next_handler_index_; - - // Properties count estimation. - int expected_property_count_; - - // For generators, the variable that holds the generator object. This - // variable is used by yield expressions and return statements. NULL - // indicates that this function is not a generator. - Variable* generator_object_variable_; - - FunctionState** function_state_stack_; - FunctionState* outer_function_state_; - Scope** scope_stack_; - Scope* outer_scope_; - Isolate* isolate_; - int saved_ast_node_id_; - AstNodeFactory factory_; - }; - class ParsingModeScope BASE_EMBEDDED { public: ParsingModeScope(Parser* parser, Mode mode) @@ -770,19 +727,13 @@ class Parser : public ParserBase { PreParser::PreParseResult LazyParseFunctionLiteral( SingletonLogger* logger); - AstNodeFactory* factory() { - return function_state_->factory(); - } - Isolate* isolate_; ZoneList > symbol_cache_; Handle