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