Line data Source code
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
3 : // found in the LICENSE file.
4 :
5 : #include "src/ast/ast-numbering.h"
6 :
7 : #include "src/ast/ast.h"
8 : #include "src/ast/scopes.h"
9 : #include "src/compiler.h"
10 : #include "src/objects-inl.h"
11 :
12 : namespace v8 {
13 : namespace internal {
14 :
15 : class AstNumberingVisitor final : public AstVisitor<AstNumberingVisitor> {
16 : public:
17 : AstNumberingVisitor(uintptr_t stack_limit, Zone* zone,
18 : Compiler::EagerInnerFunctionLiterals* eager_literals)
19 : : zone_(zone),
20 : eager_literals_(eager_literals),
21 : suspend_count_(0),
22 2192803 : dont_optimize_reason_(kNoReason) {
23 : InitializeAstVisitor(stack_limit);
24 : }
25 :
26 : bool Renumber(FunctionLiteral* node);
27 :
28 : private:
29 : // AST node visitor interface.
30 : #define DEFINE_VISIT(type) void Visit##type(type* node);
31 : AST_NODE_LIST(DEFINE_VISIT)
32 : #undef DEFINE_VISIT
33 :
34 : void VisitSuspend(Suspend* node);
35 :
36 : void VisitStatementsAndDeclarations(Block* node);
37 : void VisitStatements(ZoneList<Statement*>* statements);
38 : void VisitDeclarations(Declaration::List* declarations);
39 : void VisitArguments(ZoneList<Expression*>* arguments);
40 : void VisitLiteralProperty(LiteralProperty* property);
41 :
42 : void DisableOptimization(BailoutReason reason) {
43 1678 : dont_optimize_reason_ = reason;
44 : }
45 :
46 : BailoutReason dont_optimize_reason() const { return dont_optimize_reason_; }
47 :
48 : Zone* zone() const { return zone_; }
49 :
50 : Zone* zone_;
51 : Compiler::EagerInnerFunctionLiterals* eager_literals_;
52 : int suspend_count_;
53 : FunctionKind function_kind_;
54 : BailoutReason dont_optimize_reason_;
55 :
56 375662432 : DEFINE_AST_VISITOR_SUBCLASS_MEMBERS();
57 : DISALLOW_COPY_AND_ASSIGN(AstNumberingVisitor);
58 : };
59 :
60 0 : void AstNumberingVisitor::VisitVariableDeclaration(VariableDeclaration* node) {
61 : VisitVariableProxy(node->proxy());
62 0 : }
63 :
64 0 : void AstNumberingVisitor::VisitEmptyStatement(EmptyStatement* node) {
65 0 : }
66 :
67 4331 : void AstNumberingVisitor::VisitSloppyBlockFunctionStatement(
68 4331 : SloppyBlockFunctionStatement* node) {
69 4331 : Visit(node->statement());
70 4331 : }
71 :
72 0 : void AstNumberingVisitor::VisitContinueStatement(ContinueStatement* node) {
73 0 : }
74 :
75 0 : void AstNumberingVisitor::VisitBreakStatement(BreakStatement* node) {
76 0 : }
77 :
78 0 : void AstNumberingVisitor::VisitDebuggerStatement(DebuggerStatement* node) {
79 0 : }
80 :
81 0 : void AstNumberingVisitor::VisitNativeFunctionLiteral(
82 : NativeFunctionLiteral* node) {
83 : DisableOptimization(kNativeFunctionLiteral);
84 0 : }
85 :
86 6462 : void AstNumberingVisitor::VisitDoExpression(DoExpression* node) {
87 3231 : Visit(node->block());
88 3231 : Visit(node->result());
89 3231 : }
90 :
91 0 : void AstNumberingVisitor::VisitLiteral(Literal* node) {
92 0 : }
93 :
94 0 : void AstNumberingVisitor::VisitRegExpLiteral(RegExpLiteral* node) {
95 0 : }
96 :
97 0 : void AstNumberingVisitor::VisitVariableProxy(VariableProxy* node) {
98 0 : }
99 :
100 0 : void AstNumberingVisitor::VisitThisFunction(ThisFunction* node) {
101 0 : }
102 :
103 1923 : void AstNumberingVisitor::VisitSuperPropertyReference(
104 3846 : SuperPropertyReference* node) {
105 1923 : Visit(node->this_var());
106 1923 : Visit(node->home_object());
107 1923 : }
108 :
109 11286 : void AstNumberingVisitor::VisitSuperCallReference(SuperCallReference* node) {
110 3762 : Visit(node->this_var());
111 3762 : Visit(node->new_target_var());
112 3762 : Visit(node->this_function_var());
113 3762 : }
114 :
115 9549321 : void AstNumberingVisitor::VisitExpressionStatement(ExpressionStatement* node) {
116 9549321 : Visit(node->expression());
117 9549324 : }
118 :
119 2206200 : void AstNumberingVisitor::VisitReturnStatement(ReturnStatement* node) {
120 2206200 : Visit(node->expression());
121 2206201 : }
122 :
123 23912 : void AstNumberingVisitor::VisitSuspend(Suspend* node) {
124 23912 : node->set_suspend_id(suspend_count_);
125 23912 : suspend_count_++;
126 23912 : Visit(node->expression());
127 0 : }
128 :
129 26112 : void AstNumberingVisitor::VisitYield(Yield* node) { VisitSuspend(node); }
130 :
131 243 : void AstNumberingVisitor::VisitYieldStar(YieldStar* node) {
132 486 : node->set_suspend_id(suspend_count_++);
133 486 : if (IsAsyncGeneratorFunction(function_kind_)) {
134 11 : node->set_await_iterator_close_suspend_id(suspend_count_++);
135 11 : node->set_await_delegated_iterator_output_suspend_id(suspend_count_++);
136 : }
137 243 : Visit(node->expression());
138 243 : }
139 :
140 21712 : void AstNumberingVisitor::VisitAwait(Await* node) { VisitSuspend(node); }
141 :
142 79221 : void AstNumberingVisitor::VisitThrow(Throw* node) {
143 79221 : Visit(node->exception());
144 79221 : }
145 :
146 681356 : void AstNumberingVisitor::VisitUnaryOperation(UnaryOperation* node) {
147 681356 : Visit(node->expression());
148 681356 : }
149 :
150 205850 : void AstNumberingVisitor::VisitCountOperation(CountOperation* node) {
151 205850 : Visit(node->expression());
152 205850 : }
153 :
154 5288508 : void AstNumberingVisitor::VisitBlock(Block* node) {
155 5288508 : VisitStatementsAndDeclarations(node);
156 5288508 : }
157 :
158 5288508 : void AstNumberingVisitor::VisitStatementsAndDeclarations(Block* node) {
159 : Scope* scope = node->scope();
160 : DCHECK(scope == nullptr || !scope->HasBeenRemoved());
161 5288508 : if (scope) VisitDeclarations(scope->declarations());
162 5288508 : VisitStatements(node->statements());
163 5288508 : }
164 :
165 838430 : void AstNumberingVisitor::VisitFunctionDeclaration(FunctionDeclaration* node) {
166 : VisitVariableProxy(node->proxy());
167 838430 : VisitFunctionLiteral(node->fun());
168 0 : }
169 :
170 523841 : void AstNumberingVisitor::VisitCallRuntime(CallRuntime* node) {
171 523841 : VisitArguments(node->arguments());
172 523841 : }
173 :
174 6084 : void AstNumberingVisitor::VisitWithStatement(WithStatement* node) {
175 3042 : Visit(node->expression());
176 3042 : Visit(node->statement());
177 3042 : }
178 :
179 6324 : void AstNumberingVisitor::VisitDoWhileStatement(DoWhileStatement* node) {
180 9486 : node->set_first_suspend_id(suspend_count_);
181 3162 : Visit(node->body());
182 3162 : Visit(node->cond());
183 6324 : node->set_suspend_count(suspend_count_ - node->first_suspend_id());
184 3162 : }
185 :
186 35258 : void AstNumberingVisitor::VisitWhileStatement(WhileStatement* node) {
187 52887 : node->set_first_suspend_id(suspend_count_);
188 17629 : Visit(node->cond());
189 17629 : Visit(node->body());
190 35258 : node->set_suspend_count(suspend_count_ - node->first_suspend_id());
191 17629 : }
192 :
193 224034 : void AstNumberingVisitor::VisitTryCatchStatement(TryCatchStatement* node) {
194 : DCHECK(node->scope() == nullptr || !node->scope()->HasBeenRemoved());
195 112017 : Visit(node->try_block());
196 112017 : Visit(node->catch_block());
197 112017 : }
198 :
199 81184 : void AstNumberingVisitor::VisitTryFinallyStatement(TryFinallyStatement* node) {
200 40592 : Visit(node->try_block());
201 40592 : Visit(node->finally_block());
202 40592 : }
203 :
204 10800254 : void AstNumberingVisitor::VisitProperty(Property* node) {
205 5400127 : Visit(node->key());
206 5400127 : Visit(node->obj());
207 5400127 : }
208 :
209 13662333 : void AstNumberingVisitor::VisitAssignment(Assignment* node) {
210 6831166 : Visit(node->target());
211 6831167 : Visit(node->value());
212 6831169 : }
213 :
214 101199 : void AstNumberingVisitor::VisitCompoundAssignment(CompoundAssignment* node) {
215 101199 : VisitBinaryOperation(node->binary_operation());
216 101199 : VisitAssignment(node);
217 101199 : }
218 :
219 3006786 : void AstNumberingVisitor::VisitBinaryOperation(BinaryOperation* node) {
220 1503393 : Visit(node->left());
221 1503393 : Visit(node->right());
222 1503393 : }
223 :
224 2397240 : void AstNumberingVisitor::VisitCompareOperation(CompareOperation* node) {
225 1198620 : Visit(node->left());
226 1198620 : Visit(node->right());
227 1198620 : }
228 :
229 4462 : void AstNumberingVisitor::VisitSpread(Spread* node) {
230 4462 : Visit(node->expression());
231 4462 : }
232 :
233 0 : void AstNumberingVisitor::VisitEmptyParentheses(EmptyParentheses* node) {
234 0 : UNREACHABLE();
235 : }
236 :
237 30758 : void AstNumberingVisitor::VisitGetIterator(GetIterator* node) {
238 30758 : Visit(node->iterable());
239 30758 : }
240 :
241 0 : void AstNumberingVisitor::VisitGetTemplateObject(GetTemplateObject* node) {}
242 :
243 232 : void AstNumberingVisitor::VisitImportCallExpression(
244 232 : ImportCallExpression* node) {
245 232 : Visit(node->argument());
246 232 : }
247 :
248 15322 : void AstNumberingVisitor::VisitForInStatement(ForInStatement* node) {
249 7661 : Visit(node->enumerable()); // Not part of loop.
250 22983 : node->set_first_suspend_id(suspend_count_);
251 7661 : Visit(node->each());
252 7661 : Visit(node->body());
253 15322 : node->set_suspend_count(suspend_count_ - node->first_suspend_id());
254 7661 : }
255 :
256 103760 : void AstNumberingVisitor::VisitForOfStatement(ForOfStatement* node) {
257 25940 : Visit(node->assign_iterator()); // Not part of loop.
258 77820 : node->set_first_suspend_id(suspend_count_);
259 25940 : Visit(node->next_result());
260 25940 : Visit(node->result_done());
261 25940 : Visit(node->assign_each());
262 25940 : Visit(node->body());
263 51880 : node->set_suspend_count(suspend_count_ - node->first_suspend_id());
264 25940 : }
265 :
266 163674 : void AstNumberingVisitor::VisitConditional(Conditional* node) {
267 54558 : Visit(node->condition());
268 54558 : Visit(node->then_expression());
269 54558 : Visit(node->else_expression());
270 54558 : }
271 :
272 2276076 : void AstNumberingVisitor::VisitIfStatement(IfStatement* node) {
273 1003569 : Visit(node->condition());
274 1003568 : Visit(node->then_statement());
275 1003570 : if (node->HasElseStatement()) {
276 268939 : Visit(node->else_statement());
277 : }
278 1003570 : }
279 :
280 16671 : void AstNumberingVisitor::VisitSwitchStatement(SwitchStatement* node) {
281 16671 : Visit(node->tag());
282 234558 : for (CaseClause* clause : *node->cases()) {
283 100608 : if (!clause->is_default()) Visit(clause->label());
284 100608 : VisitStatements(clause->statements());
285 : }
286 16671 : }
287 :
288 566934 : void AstNumberingVisitor::VisitForStatement(ForStatement* node) {
289 188978 : if (node->init() != nullptr) Visit(node->init()); // Not part of loop.
290 566934 : node->set_first_suspend_id(suspend_count_);
291 188978 : if (node->cond() != nullptr) Visit(node->cond());
292 188978 : if (node->next() != nullptr) Visit(node->next());
293 188978 : Visit(node->body());
294 377956 : node->set_suspend_count(suspend_count_ - node->first_suspend_id());
295 188978 : }
296 :
297 587958 : void AstNumberingVisitor::VisitClassLiteral(ClassLiteral* node) {
298 63148 : if (node->extends()) Visit(node->extends());
299 63148 : if (node->constructor()) Visit(node->constructor());
300 860176 : for (int i = 0; i < node->properties()->length(); i++) {
301 398514 : VisitLiteralProperty(node->properties()->at(i));
302 : }
303 63148 : }
304 :
305 3258717 : void AstNumberingVisitor::VisitObjectLiteral(ObjectLiteral* node) {
306 5842016 : for (int i = 0; i < node->properties()->length(); i++) {
307 2583299 : VisitLiteralProperty(node->properties()->at(i));
308 : }
309 337709 : node->InitDepthAndFlags();
310 : // Mark all computed expressions that are bound to a key that
311 : // is shadowed by a later occurrence of the same key. For the
312 : // marked expressions, no store code will be is emitted.
313 337709 : node->CalculateEmitStore(zone_);
314 337709 : }
315 :
316 5963626 : void AstNumberingVisitor::VisitLiteralProperty(LiteralProperty* node) {
317 2981813 : Visit(node->key());
318 2981813 : Visit(node->value());
319 2981813 : }
320 :
321 8512593 : void AstNumberingVisitor::VisitArrayLiteral(ArrayLiteral* node) {
322 16490108 : for (int i = 0; i < node->values()->length(); i++) {
323 7977515 : Visit(node->values()->at(i));
324 : }
325 267539 : node->InitDepthAndFlags();
326 267539 : }
327 :
328 5406492 : void AstNumberingVisitor::VisitCall(Call* node) {
329 2703246 : Visit(node->expression());
330 2703246 : VisitArguments(node->arguments());
331 2703247 : }
332 :
333 315554 : void AstNumberingVisitor::VisitCallNew(CallNew* node) {
334 157777 : Visit(node->expression());
335 157777 : VisitArguments(node->arguments());
336 157777 : }
337 :
338 29691691 : void AstNumberingVisitor::VisitStatements(ZoneList<Statement*>* statements) {
339 15163840 : if (statements == nullptr) return;
340 36637628 : for (int i = 0; i < statements->length(); i++) {
341 16592288 : Visit(statements->at(i));
342 16592294 : if (statements->at(i)->IsJump()) break;
343 : }
344 : }
345 :
346 0 : void AstNumberingVisitor::VisitDeclarations(Declaration::List* decls) {
347 14422606 : for (Declaration* decl : *decls) Visit(decl);
348 0 : }
349 :
350 14190804 : void AstNumberingVisitor::VisitArguments(ZoneList<Expression*>* arguments) {
351 21611878 : for (int i = 0; i < arguments->length(); i++) {
352 7421074 : Visit(arguments->at(i));
353 : }
354 3384865 : }
355 :
356 4778976 : void AstNumberingVisitor::VisitFunctionLiteral(FunctionLiteral* node) {
357 4231476 : if (node->ShouldEagerCompile()) {
358 562874 : if (eager_literals_) {
359 : eager_literals_->Add(new (zone())
360 547499 : ThreadedListZoneEntry<FunctionLiteral*>(node));
361 : }
362 :
363 : // If the function literal is being eagerly compiled, recurse into the
364 : // declarations and body of the function literal.
365 562873 : if (!AstNumbering::Renumber(stack_limit_, zone_, node, eager_literals_)) {
366 : SetStackOverflow();
367 4231479 : return;
368 : }
369 : }
370 : }
371 :
372 2264 : void AstNumberingVisitor::VisitRewritableExpression(
373 2264 : RewritableExpression* node) {
374 2264 : Visit(node->expression());
375 2264 : }
376 :
377 8771217 : bool AstNumberingVisitor::Renumber(FunctionLiteral* node) {
378 : DeclarationScope* scope = node->scope();
379 : DCHECK(!scope->HasBeenRemoved());
380 2192803 : function_kind_ = node->kind();
381 :
382 : VisitDeclarations(scope->declarations());
383 2192802 : VisitStatements(node->body());
384 :
385 : node->set_dont_optimize_reason(dont_optimize_reason());
386 2192806 : node->set_suspend_count(suspend_count_);
387 :
388 2192806 : return !HasStackOverflow();
389 : }
390 :
391 2192803 : bool AstNumbering::Renumber(
392 : uintptr_t stack_limit, Zone* zone, FunctionLiteral* function,
393 : Compiler::EagerInnerFunctionLiterals* eager_literals) {
394 : DisallowHeapAllocation no_allocation;
395 : DisallowHandleAllocation no_handles;
396 : DisallowHandleDereference no_deref;
397 :
398 : AstNumberingVisitor visitor(stack_limit, zone, eager_literals);
399 2192803 : return visitor.Renumber(function);
400 : }
401 : } // namespace internal
402 : } // namespace v8
|