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
10 namespace v8 {
11 namespace internal {
12
13 class AstNumberingVisitor final : public AstVisitor {
14 public:
AstNumberingVisitor(Isolate * isolate,Zone * zone)15 AstNumberingVisitor(Isolate* isolate, Zone* zone)
16 : AstVisitor(),
17 isolate_(isolate),
18 zone_(zone),
19 next_id_(BailoutId::FirstUsable().ToInt()),
20 properties_(zone),
21 slot_cache_(zone),
22 dont_optimize_reason_(kNoReason) {
23 InitializeAstVisitor(isolate);
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) override;
31 AST_NODE_LIST(DEFINE_VISIT)
32 #undef DEFINE_VISIT
33
34 bool Finish(FunctionLiteral* node);
35
36 void VisitVariableProxyReference(VariableProxy* node);
37 void VisitPropertyReference(Property* node);
38 void VisitReference(Expression* expr);
39
40 void VisitStatements(ZoneList<Statement*>* statements) override;
41 void VisitDeclarations(ZoneList<Declaration*>* declarations) override;
42 void VisitArguments(ZoneList<Expression*>* arguments);
43 void VisitObjectLiteralProperty(ObjectLiteralProperty* property);
44
ReserveIdRange(int n)45 int ReserveIdRange(int n) {
46 int tmp = next_id_;
47 next_id_ += n;
48 return tmp;
49 }
50
IncrementNodeCount()51 void IncrementNodeCount() { properties_.add_node_count(1); }
DisableSelfOptimization()52 void DisableSelfOptimization() {
53 properties_.flags() |= AstProperties::kDontSelfOptimize;
54 }
DisableOptimization(BailoutReason reason)55 void DisableOptimization(BailoutReason reason) {
56 dont_optimize_reason_ = reason;
57 DisableSelfOptimization();
58 }
DisableCrankshaft(BailoutReason reason)59 void DisableCrankshaft(BailoutReason reason) {
60 if (FLAG_turbo_shipping) {
61 properties_.flags() |= AstProperties::kDontCrankshaft;
62 } else {
63 dont_optimize_reason_ = reason;
64 DisableSelfOptimization();
65 }
66 }
67
68 template <typename Node>
ReserveFeedbackSlots(Node * node)69 void ReserveFeedbackSlots(Node* node) {
70 node->AssignFeedbackVectorSlots(isolate_, properties_.get_spec(),
71 &slot_cache_);
72 }
73
dont_optimize_reason() const74 BailoutReason dont_optimize_reason() const { return dont_optimize_reason_; }
75
76 Isolate* isolate_;
77 Zone* zone_;
78 int next_id_;
79 AstProperties properties_;
80 // The slot cache allows us to reuse certain feedback vector slots.
81 FeedbackVectorSlotCache slot_cache_;
82 BailoutReason dont_optimize_reason_;
83
84 DEFINE_AST_VISITOR_SUBCLASS_MEMBERS();
85 DISALLOW_COPY_AND_ASSIGN(AstNumberingVisitor);
86 };
87
88
VisitVariableDeclaration(VariableDeclaration * node)89 void AstNumberingVisitor::VisitVariableDeclaration(VariableDeclaration* node) {
90 IncrementNodeCount();
91 VisitVariableProxy(node->proxy());
92 }
93
94
VisitExportDeclaration(ExportDeclaration * node)95 void AstNumberingVisitor::VisitExportDeclaration(ExportDeclaration* node) {
96 IncrementNodeCount();
97 DisableOptimization(kExportDeclaration);
98 VisitVariableProxy(node->proxy());
99 }
100
101
VisitEmptyStatement(EmptyStatement * node)102 void AstNumberingVisitor::VisitEmptyStatement(EmptyStatement* node) {
103 IncrementNodeCount();
104 }
105
106
VisitSloppyBlockFunctionStatement(SloppyBlockFunctionStatement * node)107 void AstNumberingVisitor::VisitSloppyBlockFunctionStatement(
108 SloppyBlockFunctionStatement* node) {
109 IncrementNodeCount();
110 Visit(node->statement());
111 }
112
113
VisitContinueStatement(ContinueStatement * node)114 void AstNumberingVisitor::VisitContinueStatement(ContinueStatement* node) {
115 IncrementNodeCount();
116 }
117
118
VisitBreakStatement(BreakStatement * node)119 void AstNumberingVisitor::VisitBreakStatement(BreakStatement* node) {
120 IncrementNodeCount();
121 }
122
123
VisitDebuggerStatement(DebuggerStatement * node)124 void AstNumberingVisitor::VisitDebuggerStatement(DebuggerStatement* node) {
125 IncrementNodeCount();
126 DisableOptimization(kDebuggerStatement);
127 node->set_base_id(ReserveIdRange(DebuggerStatement::num_ids()));
128 }
129
130
VisitNativeFunctionLiteral(NativeFunctionLiteral * node)131 void AstNumberingVisitor::VisitNativeFunctionLiteral(
132 NativeFunctionLiteral* node) {
133 IncrementNodeCount();
134 DisableOptimization(kNativeFunctionLiteral);
135 node->set_base_id(ReserveIdRange(NativeFunctionLiteral::num_ids()));
136 }
137
138
VisitDoExpression(DoExpression * node)139 void AstNumberingVisitor::VisitDoExpression(DoExpression* node) {
140 IncrementNodeCount();
141 DisableCrankshaft(kDoExpression);
142 node->set_base_id(ReserveIdRange(DoExpression::num_ids()));
143 Visit(node->block());
144 Visit(node->result());
145 }
146
147
VisitLiteral(Literal * node)148 void AstNumberingVisitor::VisitLiteral(Literal* node) {
149 IncrementNodeCount();
150 node->set_base_id(ReserveIdRange(Literal::num_ids()));
151 }
152
153
VisitRegExpLiteral(RegExpLiteral * node)154 void AstNumberingVisitor::VisitRegExpLiteral(RegExpLiteral* node) {
155 IncrementNodeCount();
156 node->set_base_id(ReserveIdRange(RegExpLiteral::num_ids()));
157 }
158
159
VisitVariableProxyReference(VariableProxy * node)160 void AstNumberingVisitor::VisitVariableProxyReference(VariableProxy* node) {
161 IncrementNodeCount();
162 if (node->var()->IsLookupSlot()) {
163 DisableCrankshaft(kReferenceToAVariableWhichRequiresDynamicLookup);
164 }
165 node->set_base_id(ReserveIdRange(VariableProxy::num_ids()));
166 }
167
168
VisitVariableProxy(VariableProxy * node)169 void AstNumberingVisitor::VisitVariableProxy(VariableProxy* node) {
170 VisitVariableProxyReference(node);
171 ReserveFeedbackSlots(node);
172 }
173
174
VisitThisFunction(ThisFunction * node)175 void AstNumberingVisitor::VisitThisFunction(ThisFunction* node) {
176 IncrementNodeCount();
177 node->set_base_id(ReserveIdRange(ThisFunction::num_ids()));
178 }
179
180
VisitSuperPropertyReference(SuperPropertyReference * node)181 void AstNumberingVisitor::VisitSuperPropertyReference(
182 SuperPropertyReference* node) {
183 IncrementNodeCount();
184 DisableCrankshaft(kSuperReference);
185 node->set_base_id(ReserveIdRange(SuperPropertyReference::num_ids()));
186 Visit(node->this_var());
187 Visit(node->home_object());
188 }
189
190
VisitSuperCallReference(SuperCallReference * node)191 void AstNumberingVisitor::VisitSuperCallReference(SuperCallReference* node) {
192 IncrementNodeCount();
193 DisableCrankshaft(kSuperReference);
194 node->set_base_id(ReserveIdRange(SuperCallReference::num_ids()));
195 Visit(node->this_var());
196 Visit(node->new_target_var());
197 Visit(node->this_function_var());
198 }
199
200
VisitImportDeclaration(ImportDeclaration * node)201 void AstNumberingVisitor::VisitImportDeclaration(ImportDeclaration* node) {
202 IncrementNodeCount();
203 DisableOptimization(kImportDeclaration);
204 VisitVariableProxy(node->proxy());
205 }
206
207
VisitExpressionStatement(ExpressionStatement * node)208 void AstNumberingVisitor::VisitExpressionStatement(ExpressionStatement* node) {
209 IncrementNodeCount();
210 Visit(node->expression());
211 }
212
213
VisitReturnStatement(ReturnStatement * node)214 void AstNumberingVisitor::VisitReturnStatement(ReturnStatement* node) {
215 IncrementNodeCount();
216 Visit(node->expression());
217 }
218
219
VisitYield(Yield * node)220 void AstNumberingVisitor::VisitYield(Yield* node) {
221 IncrementNodeCount();
222 DisableOptimization(kYield);
223 ReserveFeedbackSlots(node);
224 node->set_base_id(ReserveIdRange(Yield::num_ids()));
225 Visit(node->generator_object());
226 Visit(node->expression());
227 }
228
229
VisitThrow(Throw * node)230 void AstNumberingVisitor::VisitThrow(Throw* node) {
231 IncrementNodeCount();
232 node->set_base_id(ReserveIdRange(Throw::num_ids()));
233 Visit(node->exception());
234 }
235
236
VisitUnaryOperation(UnaryOperation * node)237 void AstNumberingVisitor::VisitUnaryOperation(UnaryOperation* node) {
238 IncrementNodeCount();
239 node->set_base_id(ReserveIdRange(UnaryOperation::num_ids()));
240 Visit(node->expression());
241 }
242
243
VisitCountOperation(CountOperation * node)244 void AstNumberingVisitor::VisitCountOperation(CountOperation* node) {
245 IncrementNodeCount();
246 node->set_base_id(ReserveIdRange(CountOperation::num_ids()));
247 Visit(node->expression());
248 ReserveFeedbackSlots(node);
249 }
250
251
VisitBlock(Block * node)252 void AstNumberingVisitor::VisitBlock(Block* node) {
253 IncrementNodeCount();
254 node->set_base_id(ReserveIdRange(Block::num_ids()));
255 if (node->scope() != NULL) VisitDeclarations(node->scope()->declarations());
256 VisitStatements(node->statements());
257 }
258
259
VisitFunctionDeclaration(FunctionDeclaration * node)260 void AstNumberingVisitor::VisitFunctionDeclaration(FunctionDeclaration* node) {
261 IncrementNodeCount();
262 VisitVariableProxy(node->proxy());
263 VisitFunctionLiteral(node->fun());
264 }
265
266
VisitCallRuntime(CallRuntime * node)267 void AstNumberingVisitor::VisitCallRuntime(CallRuntime* node) {
268 IncrementNodeCount();
269 ReserveFeedbackSlots(node);
270 if (node->is_jsruntime()) {
271 // Don't try to optimize JS runtime calls because we bailout on them.
272 DisableOptimization(kCallToAJavaScriptRuntimeFunction);
273 }
274 node->set_base_id(ReserveIdRange(CallRuntime::num_ids()));
275 VisitArguments(node->arguments());
276 }
277
278
VisitWithStatement(WithStatement * node)279 void AstNumberingVisitor::VisitWithStatement(WithStatement* node) {
280 IncrementNodeCount();
281 DisableCrankshaft(kWithStatement);
282 node->set_base_id(ReserveIdRange(WithStatement::num_ids()));
283 Visit(node->expression());
284 Visit(node->statement());
285 }
286
287
VisitDoWhileStatement(DoWhileStatement * node)288 void AstNumberingVisitor::VisitDoWhileStatement(DoWhileStatement* node) {
289 IncrementNodeCount();
290 DisableSelfOptimization();
291 node->set_base_id(ReserveIdRange(DoWhileStatement::num_ids()));
292 Visit(node->body());
293 Visit(node->cond());
294 }
295
296
VisitWhileStatement(WhileStatement * node)297 void AstNumberingVisitor::VisitWhileStatement(WhileStatement* node) {
298 IncrementNodeCount();
299 DisableSelfOptimization();
300 node->set_base_id(ReserveIdRange(WhileStatement::num_ids()));
301 Visit(node->cond());
302 Visit(node->body());
303 }
304
305
VisitTryCatchStatement(TryCatchStatement * node)306 void AstNumberingVisitor::VisitTryCatchStatement(TryCatchStatement* node) {
307 IncrementNodeCount();
308 DisableOptimization(kTryCatchStatement);
309 node->set_base_id(ReserveIdRange(TryCatchStatement::num_ids()));
310 Visit(node->try_block());
311 Visit(node->catch_block());
312 }
313
314
VisitTryFinallyStatement(TryFinallyStatement * node)315 void AstNumberingVisitor::VisitTryFinallyStatement(TryFinallyStatement* node) {
316 IncrementNodeCount();
317 DisableOptimization(kTryFinallyStatement);
318 node->set_base_id(ReserveIdRange(TryFinallyStatement::num_ids()));
319 Visit(node->try_block());
320 Visit(node->finally_block());
321 }
322
323
VisitPropertyReference(Property * node)324 void AstNumberingVisitor::VisitPropertyReference(Property* node) {
325 IncrementNodeCount();
326 node->set_base_id(ReserveIdRange(Property::num_ids()));
327 Visit(node->key());
328 Visit(node->obj());
329 }
330
331
VisitReference(Expression * expr)332 void AstNumberingVisitor::VisitReference(Expression* expr) {
333 DCHECK(expr->IsProperty() || expr->IsVariableProxy());
334 if (expr->IsProperty()) {
335 VisitPropertyReference(expr->AsProperty());
336 } else {
337 VisitVariableProxyReference(expr->AsVariableProxy());
338 }
339 }
340
341
VisitProperty(Property * node)342 void AstNumberingVisitor::VisitProperty(Property* node) {
343 VisitPropertyReference(node);
344 ReserveFeedbackSlots(node);
345 }
346
347
VisitAssignment(Assignment * node)348 void AstNumberingVisitor::VisitAssignment(Assignment* node) {
349 IncrementNodeCount();
350 node->set_base_id(ReserveIdRange(Assignment::num_ids()));
351
352 if (node->is_compound()) VisitBinaryOperation(node->binary_operation());
353 VisitReference(node->target());
354 Visit(node->value());
355 ReserveFeedbackSlots(node);
356 }
357
358
VisitBinaryOperation(BinaryOperation * node)359 void AstNumberingVisitor::VisitBinaryOperation(BinaryOperation* node) {
360 IncrementNodeCount();
361 node->set_base_id(ReserveIdRange(BinaryOperation::num_ids()));
362 Visit(node->left());
363 Visit(node->right());
364 }
365
366
VisitCompareOperation(CompareOperation * node)367 void AstNumberingVisitor::VisitCompareOperation(CompareOperation* node) {
368 IncrementNodeCount();
369 node->set_base_id(ReserveIdRange(CompareOperation::num_ids()));
370 Visit(node->left());
371 Visit(node->right());
372 }
373
374
VisitSpread(Spread * node)375 void AstNumberingVisitor::VisitSpread(Spread* node) {
376 IncrementNodeCount();
377 DisableCrankshaft(kSpread);
378 Visit(node->expression());
379 }
380
381
VisitEmptyParentheses(EmptyParentheses * node)382 void AstNumberingVisitor::VisitEmptyParentheses(EmptyParentheses* node) {
383 UNREACHABLE();
384 }
385
386
VisitForInStatement(ForInStatement * node)387 void AstNumberingVisitor::VisitForInStatement(ForInStatement* node) {
388 IncrementNodeCount();
389 DisableSelfOptimization();
390 node->set_base_id(ReserveIdRange(ForInStatement::num_ids()));
391 Visit(node->each());
392 Visit(node->enumerable());
393 Visit(node->body());
394 ReserveFeedbackSlots(node);
395 }
396
397
VisitForOfStatement(ForOfStatement * node)398 void AstNumberingVisitor::VisitForOfStatement(ForOfStatement* node) {
399 IncrementNodeCount();
400 DisableCrankshaft(kForOfStatement);
401 node->set_base_id(ReserveIdRange(ForOfStatement::num_ids()));
402 Visit(node->assign_iterator());
403 Visit(node->next_result());
404 Visit(node->result_done());
405 Visit(node->assign_each());
406 Visit(node->body());
407 ReserveFeedbackSlots(node);
408 }
409
410
VisitConditional(Conditional * node)411 void AstNumberingVisitor::VisitConditional(Conditional* node) {
412 IncrementNodeCount();
413 node->set_base_id(ReserveIdRange(Conditional::num_ids()));
414 Visit(node->condition());
415 Visit(node->then_expression());
416 Visit(node->else_expression());
417 }
418
419
VisitIfStatement(IfStatement * node)420 void AstNumberingVisitor::VisitIfStatement(IfStatement* node) {
421 IncrementNodeCount();
422 node->set_base_id(ReserveIdRange(IfStatement::num_ids()));
423 Visit(node->condition());
424 Visit(node->then_statement());
425 if (node->HasElseStatement()) {
426 Visit(node->else_statement());
427 }
428 }
429
430
VisitSwitchStatement(SwitchStatement * node)431 void AstNumberingVisitor::VisitSwitchStatement(SwitchStatement* node) {
432 IncrementNodeCount();
433 node->set_base_id(ReserveIdRange(SwitchStatement::num_ids()));
434 Visit(node->tag());
435 ZoneList<CaseClause*>* cases = node->cases();
436 for (int i = 0; i < cases->length(); i++) {
437 VisitCaseClause(cases->at(i));
438 }
439 }
440
441
VisitCaseClause(CaseClause * node)442 void AstNumberingVisitor::VisitCaseClause(CaseClause* node) {
443 IncrementNodeCount();
444 node->set_base_id(ReserveIdRange(CaseClause::num_ids()));
445 if (!node->is_default()) Visit(node->label());
446 VisitStatements(node->statements());
447 }
448
449
VisitForStatement(ForStatement * node)450 void AstNumberingVisitor::VisitForStatement(ForStatement* node) {
451 IncrementNodeCount();
452 DisableSelfOptimization();
453 node->set_base_id(ReserveIdRange(ForStatement::num_ids()));
454 if (node->init() != NULL) Visit(node->init());
455 if (node->cond() != NULL) Visit(node->cond());
456 if (node->next() != NULL) Visit(node->next());
457 Visit(node->body());
458 }
459
460
VisitClassLiteral(ClassLiteral * node)461 void AstNumberingVisitor::VisitClassLiteral(ClassLiteral* node) {
462 IncrementNodeCount();
463 DisableCrankshaft(kClassLiteral);
464 node->set_base_id(ReserveIdRange(node->num_ids()));
465 if (node->extends()) Visit(node->extends());
466 if (node->constructor()) Visit(node->constructor());
467 if (node->class_variable_proxy()) {
468 VisitVariableProxy(node->class_variable_proxy());
469 }
470 for (int i = 0; i < node->properties()->length(); i++) {
471 VisitObjectLiteralProperty(node->properties()->at(i));
472 }
473 ReserveFeedbackSlots(node);
474 }
475
476
VisitObjectLiteral(ObjectLiteral * node)477 void AstNumberingVisitor::VisitObjectLiteral(ObjectLiteral* node) {
478 IncrementNodeCount();
479 node->set_base_id(ReserveIdRange(node->num_ids()));
480 for (int i = 0; i < node->properties()->length(); i++) {
481 VisitObjectLiteralProperty(node->properties()->at(i));
482 }
483 node->BuildConstantProperties(isolate_);
484 // Mark all computed expressions that are bound to a key that
485 // is shadowed by a later occurrence of the same key. For the
486 // marked expressions, no store code will be is emitted.
487 node->CalculateEmitStore(zone_);
488 ReserveFeedbackSlots(node);
489 }
490
491
VisitObjectLiteralProperty(ObjectLiteralProperty * node)492 void AstNumberingVisitor::VisitObjectLiteralProperty(
493 ObjectLiteralProperty* node) {
494 if (node->is_computed_name()) DisableCrankshaft(kComputedPropertyName);
495 Visit(node->key());
496 Visit(node->value());
497 }
498
499
VisitArrayLiteral(ArrayLiteral * node)500 void AstNumberingVisitor::VisitArrayLiteral(ArrayLiteral* node) {
501 IncrementNodeCount();
502 node->set_base_id(ReserveIdRange(node->num_ids()));
503 for (int i = 0; i < node->values()->length(); i++) {
504 Visit(node->values()->at(i));
505 }
506 node->BuildConstantElements(isolate_);
507 ReserveFeedbackSlots(node);
508 }
509
510
VisitCall(Call * node)511 void AstNumberingVisitor::VisitCall(Call* node) {
512 IncrementNodeCount();
513 ReserveFeedbackSlots(node);
514 node->set_base_id(ReserveIdRange(Call::num_ids()));
515 Visit(node->expression());
516 VisitArguments(node->arguments());
517 }
518
519
VisitCallNew(CallNew * node)520 void AstNumberingVisitor::VisitCallNew(CallNew* node) {
521 IncrementNodeCount();
522 ReserveFeedbackSlots(node);
523 node->set_base_id(ReserveIdRange(CallNew::num_ids()));
524 Visit(node->expression());
525 VisitArguments(node->arguments());
526 }
527
528
VisitStatements(ZoneList<Statement * > * statements)529 void AstNumberingVisitor::VisitStatements(ZoneList<Statement*>* statements) {
530 if (statements == NULL) return;
531 for (int i = 0; i < statements->length(); i++) {
532 Visit(statements->at(i));
533 }
534 }
535
536
VisitDeclarations(ZoneList<Declaration * > * declarations)537 void AstNumberingVisitor::VisitDeclarations(
538 ZoneList<Declaration*>* declarations) {
539 for (int i = 0; i < declarations->length(); i++) {
540 Visit(declarations->at(i));
541 }
542 }
543
544
VisitArguments(ZoneList<Expression * > * arguments)545 void AstNumberingVisitor::VisitArguments(ZoneList<Expression*>* arguments) {
546 for (int i = 0; i < arguments->length(); i++) {
547 Visit(arguments->at(i));
548 }
549 }
550
551
VisitFunctionLiteral(FunctionLiteral * node)552 void AstNumberingVisitor::VisitFunctionLiteral(FunctionLiteral* node) {
553 IncrementNodeCount();
554 node->set_base_id(ReserveIdRange(FunctionLiteral::num_ids()));
555 // We don't recurse into the declarations or body of the function literal:
556 // you have to separately Renumber() each FunctionLiteral that you compile.
557 }
558
559
VisitRewritableAssignmentExpression(RewritableAssignmentExpression * node)560 void AstNumberingVisitor::VisitRewritableAssignmentExpression(
561 RewritableAssignmentExpression* node) {
562 IncrementNodeCount();
563 node->set_base_id(ReserveIdRange(RewritableAssignmentExpression::num_ids()));
564 Visit(node->expression());
565 }
566
567
Finish(FunctionLiteral * node)568 bool AstNumberingVisitor::Finish(FunctionLiteral* node) {
569 node->set_ast_properties(&properties_);
570 node->set_dont_optimize_reason(dont_optimize_reason());
571 return !HasStackOverflow();
572 }
573
574
Renumber(FunctionLiteral * node)575 bool AstNumberingVisitor::Renumber(FunctionLiteral* node) {
576 Scope* scope = node->scope();
577
578 if (scope->HasIllegalRedeclaration()) {
579 Visit(scope->GetIllegalRedeclaration());
580 DisableOptimization(kFunctionWithIllegalRedeclaration);
581 return Finish(node);
582 }
583 if (scope->new_target_var()) DisableCrankshaft(kSuperReference);
584 if (scope->calls_eval()) DisableOptimization(kFunctionCallsEval);
585 if (scope->arguments() != NULL && !scope->arguments()->IsStackAllocated()) {
586 DisableCrankshaft(kContextAllocatedArguments);
587 }
588
589 int rest_index;
590 if (scope->rest_parameter(&rest_index)) {
591 DisableCrankshaft(kRestParameter);
592 }
593
594 VisitDeclarations(scope->declarations());
595 VisitStatements(node->body());
596
597 return Finish(node);
598 }
599
600
Renumber(Isolate * isolate,Zone * zone,FunctionLiteral * function)601 bool AstNumbering::Renumber(Isolate* isolate, Zone* zone,
602 FunctionLiteral* function) {
603 AstNumberingVisitor visitor(isolate, zone);
604 return visitor.Renumber(function);
605 }
606 } // namespace internal
607 } // namespace v8
608