| 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/rewriter.h" | 5 #include "src/rewriter.h" |
| 6 | 6 |
| 7 #include "src/ast.h" | 7 #include "src/ast.h" |
| 8 #include "src/parser.h" | 8 #include "src/parser.h" |
| 9 #include "src/scopes.h" | 9 #include "src/scopes.h" |
| 10 | 10 |
| 11 namespace v8 { | 11 namespace v8 { |
| 12 namespace internal { | 12 namespace internal { |
| 13 | 13 |
| 14 class Processor: public AstVisitor { | 14 class Processor: public AstVisitor { |
| 15 public: | 15 public: |
| 16 Processor(Isolate* isolate, Scope* scope, Variable* result, | 16 Processor(Isolate* isolate, Scope* scope, Variable* result, |
| 17 AstValueFactory* ast_value_factory) | 17 AstValueFactory* ast_value_factory) |
| 18 : result_(result), | 18 : result_(result), |
| 19 result_assigned_(false), | 19 result_assigned_(false), |
| 20 replacement_(nullptr), | 20 replacement_(nullptr), |
| 21 is_set_(false), | 21 is_set_(false), |
| 22 scope_(scope), | 22 scope_(scope), |
| 23 factory_(ast_value_factory) { | 23 factory_(ast_value_factory) { |
| 24 InitializeAstVisitor(isolate, ast_value_factory->zone()); | 24 InitializeAstVisitor(isolate, ast_value_factory->zone()); |
| 25 } | 25 } |
| 26 | 26 |
| 27 Processor(Parser* parser, Scope* scope, Variable* result, |
| 28 AstValueFactory* ast_value_factory) |
| 29 : result_(result), |
| 30 result_assigned_(false), |
| 31 replacement_(nullptr), |
| 32 is_set_(false), |
| 33 scope_(scope), |
| 34 factory_(ast_value_factory) { |
| 35 InitializeAstVisitor(parser->stack_limit(), ast_value_factory->zone()); |
| 36 } |
| 37 |
| 27 virtual ~Processor() { } | 38 virtual ~Processor() { } |
| 28 | 39 |
| 29 void Process(ZoneList<Statement*>* statements); | 40 void Process(ZoneList<Statement*>* statements); |
| 30 bool result_assigned() const { return result_assigned_; } | 41 bool result_assigned() const { return result_assigned_; } |
| 31 | 42 |
| 32 Scope* scope() { return scope_; } | 43 Scope* scope() { return scope_; } |
| 33 AstNodeFactory* factory() { return &factory_; } | 44 AstNodeFactory* factory() { return &factory_; } |
| 34 | 45 |
| 46 // Returns ".result = value" |
| 47 Expression* SetResult(Expression* value) { |
| 48 result_assigned_ = true; |
| 49 VariableProxy* result_proxy = factory()->NewVariableProxy(result_); |
| 50 return factory()->NewAssignment(Token::ASSIGN, result_proxy, value, |
| 51 RelocInfo::kNoPosition); |
| 52 } |
| 53 |
| 54 // Inserts '.result = undefined' in front of the given statement. |
| 55 Statement* AssignUndefinedBefore(Statement* s); |
| 56 |
| 35 private: | 57 private: |
| 36 Variable* result_; | 58 Variable* result_; |
| 37 | 59 |
| 38 // We are not tracking result usage via the result_'s use | 60 // We are not tracking result usage via the result_'s use |
| 39 // counts (we leave the accurate computation to the | 61 // counts (we leave the accurate computation to the |
| 40 // usage analyzer). Instead we simple remember if | 62 // usage analyzer). Instead we simple remember if |
| 41 // there was ever an assignment to result_. | 63 // there was ever an assignment to result_. |
| 42 bool result_assigned_; | 64 bool result_assigned_; |
| 43 | 65 |
| 44 // When visiting a node, we "return" a replacement for that node in | 66 // When visiting a node, we "return" a replacement for that node in |
| 45 // [replacement_]. In many cases this will just be the original node. | 67 // [replacement_]. In many cases this will just be the original node. |
| 46 Statement* replacement_; | 68 Statement* replacement_; |
| 47 | 69 |
| 48 // To avoid storing to .result all the time, we eliminate some of | 70 // To avoid storing to .result all the time, we eliminate some of |
| 49 // the stores by keeping track of whether or not we're sure .result | 71 // the stores by keeping track of whether or not we're sure .result |
| 50 // will be overwritten anyway. This is a bit more tricky than what I | 72 // will be overwritten anyway. This is a bit more tricky than what I |
| 51 // was hoping for. | 73 // was hoping for. |
| 52 bool is_set_; | 74 bool is_set_; |
| 53 | 75 |
| 54 Scope* scope_; | 76 Scope* scope_; |
| 55 AstNodeFactory factory_; | 77 AstNodeFactory factory_; |
| 56 | 78 |
| 57 // Returns ".result = value" | |
| 58 Expression* SetResult(Expression* value) { | |
| 59 result_assigned_ = true; | |
| 60 VariableProxy* result_proxy = factory()->NewVariableProxy(result_); | |
| 61 return factory()->NewAssignment( | |
| 62 Token::ASSIGN, result_proxy, value, RelocInfo::kNoPosition); | |
| 63 } | |
| 64 | |
| 65 // Inserts '.result = undefined' in front of the given statement. | |
| 66 Statement* AssignUndefinedBefore(Statement* s); | |
| 67 | |
| 68 // Node visitors. | 79 // Node visitors. |
| 69 #define DEF_VISIT(type) virtual void Visit##type(type* node) override; | 80 #define DEF_VISIT(type) virtual void Visit##type(type* node) override; |
| 70 AST_NODE_LIST(DEF_VISIT) | 81 AST_NODE_LIST(DEF_VISIT) |
| 71 #undef DEF_VISIT | 82 #undef DEF_VISIT |
| 72 | 83 |
| 73 void VisitIterationStatement(IterationStatement* stmt); | 84 void VisitIterationStatement(IterationStatement* stmt); |
| 74 | 85 |
| 75 DEFINE_AST_VISITOR_SUBCLASS_MEMBERS(); | 86 DEFINE_AST_VISITOR_SUBCLASS_MEMBERS(); |
| 76 }; | 87 }; |
| 77 | 88 |
| (...skipping 274 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 352 Statement* result_statement = | 363 Statement* result_statement = |
| 353 processor.factory()->NewReturnStatement(result_proxy, pos); | 364 processor.factory()->NewReturnStatement(result_proxy, pos); |
| 354 body->Add(result_statement, info->zone()); | 365 body->Add(result_statement, info->zone()); |
| 355 } | 366 } |
| 356 } | 367 } |
| 357 | 368 |
| 358 return true; | 369 return true; |
| 359 } | 370 } |
| 360 | 371 |
| 361 | 372 |
| 373 bool Rewriter::Rewrite(Parser* parser, DoExpression* expr, |
| 374 AstValueFactory* factory) { |
| 375 Block* block = expr->block(); |
| 376 Scope* scope = block->scope(); |
| 377 ZoneList<Statement*>* body = block->statements(); |
| 378 VariableProxy* result = expr->result(); |
| 379 Variable* result_var = result->var(); |
| 380 |
| 381 if (!body->is_empty()) { |
| 382 Processor processor(parser, scope, result_var, factory); |
| 383 processor.Process(body); |
| 384 if (processor.HasStackOverflow()) return false; |
| 385 |
| 386 if (!processor.result_assigned()) { |
| 387 AstNodeFactory* node_factory = processor.factory(); |
| 388 Expression* undef = |
| 389 node_factory->NewUndefinedLiteral(RelocInfo::kNoPosition); |
| 390 Statement* completion = node_factory->NewExpressionStatement( |
| 391 processor.SetResult(undef), expr->position()); |
| 392 body->Add(completion, factory->zone()); |
| 393 } |
| 394 } |
| 395 return true; |
| 396 } |
| 397 |
| 398 |
| 362 } // namespace internal | 399 } // namespace internal |
| 363 } // namespace v8 | 400 } // namespace v8 |
| OLD | NEW |