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, Variable* result, | 16 Processor(Isolate* isolate, Variable* result, |
17 AstValueFactory* ast_value_factory) | 17 AstValueFactory* ast_value_factory) |
18 : result_(result), | 18 : result_(result), |
19 replacement_(nullptr), | |
19 is_set_(false), | 20 is_set_(false), |
20 factory_(ast_value_factory) { | 21 factory_(ast_value_factory) { |
21 InitializeAstVisitor(isolate, ast_value_factory->zone()); | 22 InitializeAstVisitor(isolate, ast_value_factory->zone()); |
22 } | 23 } |
23 | 24 |
24 virtual ~Processor() { } | 25 virtual ~Processor() { } |
25 | 26 |
26 void Process(ZoneList<Statement*>* statements); | 27 void Process(ZoneList<Statement*>* statements); |
27 | 28 |
28 AstNodeFactory* factory() { return &factory_; } | 29 AstNodeFactory* factory() { return &factory_; } |
29 | 30 |
30 private: | 31 private: |
31 Variable* result_; | 32 Variable* result_; |
32 | 33 |
34 // When visiting a node, we "return" a replacement for that node in | |
35 // [replacement_]. In many cases this will just be the original node. | |
36 Statement* replacement_; | |
37 | |
33 // To avoid storing to .result all the time, we eliminate some of | 38 // To avoid storing to .result all the time, we eliminate some of |
34 // the stores by keeping track of whether or not we're sure .result | 39 // the stores by keeping track of whether or not we're sure .result |
35 // will be overwritten anyway. This is a bit more tricky than what I | 40 // will be overwritten anyway. This is a bit more tricky than what I |
36 // was hoping for. | 41 // was hoping for. |
37 bool is_set_; | 42 bool is_set_; |
38 | 43 |
39 AstNodeFactory factory_; | 44 AstNodeFactory factory_; |
40 | 45 |
41 Expression* SetResult(Expression* value) { | 46 Expression* SetResult(Expression* value) { |
42 VariableProxy* result_proxy = factory()->NewVariableProxy(result_); | 47 VariableProxy* result_proxy = factory()->NewVariableProxy(result_); |
43 return factory()->NewAssignment( | 48 return factory()->NewAssignment( |
44 Token::ASSIGN, result_proxy, value, RelocInfo::kNoPosition); | 49 Token::ASSIGN, result_proxy, value, RelocInfo::kNoPosition); |
45 } | 50 } |
46 | 51 |
47 // Node visitors. | 52 // Node visitors. |
48 #define DEF_VISIT(type) virtual void Visit##type(type* node) override; | 53 #define DEF_VISIT(type) virtual void Visit##type(type* node) override; |
49 AST_NODE_LIST(DEF_VISIT) | 54 AST_NODE_LIST(DEF_VISIT) |
50 #undef DEF_VISIT | 55 #undef DEF_VISIT |
51 | 56 |
52 void VisitIterationStatement(IterationStatement* stmt); | 57 void VisitIterationStatement(IterationStatement* stmt); |
53 | 58 |
54 DEFINE_AST_VISITOR_SUBCLASS_MEMBERS(); | 59 DEFINE_AST_VISITOR_SUBCLASS_MEMBERS(); |
55 }; | 60 }; |
56 | 61 |
57 | 62 |
58 void Processor::Process(ZoneList<Statement*>* statements) { | 63 void Processor::Process(ZoneList<Statement*>* statements) { |
59 for (int i = statements->length() - 1; i >= 0; --i) { | 64 for (int i = statements->length() - 1; i >= 0; --i) { |
60 Visit(statements->at(i)); | 65 Visit(statements->at(i)); |
66 statements->Set(i, replacement_); | |
rossberg
2015/09/28 11:42:15
How about treating replacement_ as an option and c
| |
61 } | 67 } |
62 } | 68 } |
63 | 69 |
64 | 70 |
65 void Processor::VisitBlock(Block* node) { | 71 void Processor::VisitBlock(Block* node) { |
66 // An initializer block is the rewritten form of a variable declaration | 72 // An initializer block is the rewritten form of a variable declaration |
67 // with initialization expressions. The initializer block contains the | 73 // with initialization expressions. The initializer block contains the |
68 // list of assignments corresponding to the initialization expressions. | 74 // list of assignments corresponding to the initialization expressions. |
69 // While unclear from the spec (ECMA-262, 3rd., 12.2), the value of | 75 // While unclear from the spec (ECMA-262, 3rd., 12.2), the value of |
70 // a variable declaration with initialization expression is 'undefined' | 76 // a variable declaration with initialization expression is 'undefined' |
71 // with some JS VMs: For instance, using smjs, print(eval('var x = 7')) | 77 // with some JS VMs: For instance, using smjs, print(eval('var x = 7')) |
72 // returns 'undefined'. To obtain the same behavior with v8, we need | 78 // returns 'undefined'. To obtain the same behavior with v8, we need |
73 // to prevent rewriting in that case. | 79 // to prevent rewriting in that case. |
74 if (!node->ignore_completion_value()) Process(node->statements()); | 80 if (!node->ignore_completion_value()) Process(node->statements()); |
81 replacement_ = node; | |
75 } | 82 } |
76 | 83 |
77 | 84 |
78 void Processor::VisitExpressionStatement(ExpressionStatement* node) { | 85 void Processor::VisitExpressionStatement(ExpressionStatement* node) { |
79 // Rewrite : <x>; -> .result = <x>; | 86 // Rewrite : <x>; -> .result = <x>; |
80 if (!is_set_ && !node->expression()->IsThrow()) { | 87 if (!is_set_ && !node->expression()->IsThrow()) { |
81 node->set_expression(SetResult(node->expression())); | 88 node->set_expression(SetResult(node->expression())); |
82 is_set_ = true; | 89 is_set_ = true; |
83 } | 90 } |
91 replacement_ = node; | |
84 } | 92 } |
85 | 93 |
86 | 94 |
87 void Processor::VisitIfStatement(IfStatement* node) { | 95 void Processor::VisitIfStatement(IfStatement* node) { |
88 // Rewrite both branches. | 96 // Rewrite both branches. |
89 bool set_after = is_set_; | 97 bool set_after = is_set_; |
90 Visit(node->then_statement()); | 98 Visit(node->then_statement()); |
99 node->set_then_statement(replacement_); | |
91 bool set_in_then = is_set_; | 100 bool set_in_then = is_set_; |
92 is_set_ = set_after; | 101 is_set_ = set_after; |
93 Visit(node->else_statement()); | 102 Visit(node->else_statement()); |
103 node->set_else_statement(replacement_); | |
94 is_set_ = is_set_ && set_in_then; | 104 is_set_ = is_set_ && set_in_then; |
105 replacement_ = node; | |
95 } | 106 } |
96 | 107 |
97 | 108 |
98 void Processor::VisitIterationStatement(IterationStatement* node) { | 109 void Processor::VisitIterationStatement(IterationStatement* node) { |
99 // Rewrite the body. | 110 // Rewrite the body. |
100 bool set_after = is_set_; | 111 bool set_after = is_set_; |
101 is_set_ = false; // We are in a loop, so we can't rely on [set_after]. | 112 is_set_ = false; // We are in a loop, so we can't rely on [set_after]. |
102 Visit(node->body()); | 113 Visit(node->body()); |
114 node->set_body(replacement_); | |
103 is_set_ = is_set_ && set_after; | 115 is_set_ = is_set_ && set_after; |
116 replacement_ = node; | |
104 } | 117 } |
105 | 118 |
106 | 119 |
107 void Processor::VisitDoWhileStatement(DoWhileStatement* node) { | 120 void Processor::VisitDoWhileStatement(DoWhileStatement* node) { |
108 VisitIterationStatement(node); | 121 VisitIterationStatement(node); |
109 } | 122 } |
110 | 123 |
111 | 124 |
112 void Processor::VisitWhileStatement(WhileStatement* node) { | 125 void Processor::VisitWhileStatement(WhileStatement* node) { |
113 VisitIterationStatement(node); | 126 VisitIterationStatement(node); |
(...skipping 12 matching lines...) Expand all Loading... | |
126 | 139 |
127 void Processor::VisitForOfStatement(ForOfStatement* node) { | 140 void Processor::VisitForOfStatement(ForOfStatement* node) { |
128 VisitIterationStatement(node); | 141 VisitIterationStatement(node); |
129 } | 142 } |
130 | 143 |
131 | 144 |
132 void Processor::VisitTryCatchStatement(TryCatchStatement* node) { | 145 void Processor::VisitTryCatchStatement(TryCatchStatement* node) { |
133 // Rewrite both try and catch block. | 146 // Rewrite both try and catch block. |
134 bool set_after = is_set_; | 147 bool set_after = is_set_; |
135 Visit(node->try_block()); | 148 Visit(node->try_block()); |
149 node->set_try_block(static_cast<Block*>(replacement_)); | |
136 bool set_in_try = is_set_; | 150 bool set_in_try = is_set_; |
137 is_set_ = set_after; | 151 is_set_ = set_after; |
138 Visit(node->catch_block()); | 152 Visit(node->catch_block()); |
153 node->set_catch_block(static_cast<Block*>(replacement_)); | |
139 is_set_ = is_set_ && set_in_try; | 154 is_set_ = is_set_ && set_in_try; |
155 replacement_ = node; | |
140 } | 156 } |
141 | 157 |
142 | 158 |
143 void Processor::VisitTryFinallyStatement(TryFinallyStatement* node) { | 159 void Processor::VisitTryFinallyStatement(TryFinallyStatement* node) { |
144 // Rewrite both try and finally block (in reverse order). | 160 // Rewrite both try and finally block (in reverse order). |
145 Visit(node->finally_block()); | 161 Visit(node->finally_block()); |
146 Visit(node->try_block()); | 162 node->set_finally_block(static_cast<Block*>(replacement_)); |
163 Visit(node->try_block()); // Exception will not be caught. | |
164 node->set_try_block(static_cast<Block*>(replacement_)); | |
165 replacement_ = node; | |
147 } | 166 } |
148 | 167 |
149 | 168 |
150 void Processor::VisitSwitchStatement(SwitchStatement* node) { | 169 void Processor::VisitSwitchStatement(SwitchStatement* node) { |
151 // Rewrite statements in all case clauses (in reverse order). | 170 // Rewrite statements in all case clauses (in reverse order). |
152 ZoneList<CaseClause*>* clauses = node->cases(); | 171 ZoneList<CaseClause*>* clauses = node->cases(); |
153 bool set_after = is_set_; | 172 bool set_after = is_set_; |
154 for (int i = clauses->length() - 1; i >= 0; --i) { | 173 for (int i = clauses->length() - 1; i >= 0; --i) { |
155 CaseClause* clause = clauses->at(i); | 174 CaseClause* clause = clauses->at(i); |
156 Process(clause->statements()); | 175 Process(clause->statements()); |
157 } | 176 } |
158 is_set_ = is_set_ && set_after; | 177 is_set_ = is_set_ && set_after; |
178 replacement_ = node; | |
159 } | 179 } |
160 | 180 |
161 | 181 |
162 void Processor::VisitContinueStatement(ContinueStatement* node) { | 182 void Processor::VisitContinueStatement(ContinueStatement* node) { |
163 is_set_ = false; | 183 is_set_ = false; |
184 replacement_ = node; | |
164 } | 185 } |
165 | 186 |
166 | 187 |
167 void Processor::VisitBreakStatement(BreakStatement* node) { | 188 void Processor::VisitBreakStatement(BreakStatement* node) { |
168 is_set_ = false; | 189 is_set_ = false; |
190 replacement_ = node; | |
169 } | 191 } |
170 | 192 |
171 | 193 |
172 void Processor::VisitWithStatement(WithStatement* node) { | 194 void Processor::VisitWithStatement(WithStatement* node) { |
173 Visit(node->statement()); | 195 Visit(node->statement()); |
196 node->set_statement(replacement_); | |
197 replacement_ = node; | |
174 } | 198 } |
175 | 199 |
176 | 200 |
177 void Processor::VisitSloppyBlockFunctionStatement( | 201 void Processor::VisitSloppyBlockFunctionStatement( |
178 SloppyBlockFunctionStatement* node) { | 202 SloppyBlockFunctionStatement* node) { |
179 Visit(node->statement()); | 203 Visit(node->statement()); |
204 node->set_statement(replacement_); | |
205 replacement_ = node; | |
180 } | 206 } |
181 | 207 |
182 | 208 |
183 void Processor::VisitReturnStatement(ReturnStatement* node) { is_set_ = true; } | 209 void Processor::VisitEmptyStatement(EmptyStatement* node) { |
210 replacement_ = node; | |
211 } | |
184 | 212 |
185 | 213 |
186 // Do nothing: | 214 void Processor::VisitReturnStatement(ReturnStatement* node) { |
187 void Processor::VisitEmptyStatement(EmptyStatement* node) {} | 215 is_set_ = true; |
188 void Processor::VisitDebuggerStatement(DebuggerStatement* node) {} | 216 replacement_ = node; |
217 } | |
218 | |
219 | |
220 void Processor::VisitDebuggerStatement(DebuggerStatement* node) { | |
221 replacement_ = node; | |
222 } | |
189 | 223 |
190 | 224 |
191 // Expressions are never visited. | 225 // Expressions are never visited. |
192 #define DEF_VISIT(type) \ | 226 #define DEF_VISIT(type) \ |
193 void Processor::Visit##type(type* expr) { UNREACHABLE(); } | 227 void Processor::Visit##type(type* expr) { UNREACHABLE(); } |
194 EXPRESSION_NODE_LIST(DEF_VISIT) | 228 EXPRESSION_NODE_LIST(DEF_VISIT) |
195 #undef DEF_VISIT | 229 #undef DEF_VISIT |
196 | 230 |
197 | 231 |
198 // Declarations are never visited. | 232 // Declarations are never visited. |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
235 processor.factory()->NewReturnStatement(result_proxy, pos); | 269 processor.factory()->NewReturnStatement(result_proxy, pos); |
236 body->Add(result_statement, info->zone()); | 270 body->Add(result_statement, info->zone()); |
237 } | 271 } |
238 | 272 |
239 return true; | 273 return true; |
240 } | 274 } |
241 | 275 |
242 | 276 |
243 } // namespace internal | 277 } // namespace internal |
244 } // namespace v8 | 278 } // namespace v8 |
OLD | NEW |