blob: 17deb6db42d1ca085cf121cca55c0e6e0e257996 [file] [log] [blame]
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001// Copyright 2013 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/typing.h"
6
7#include "src/frames.h"
8#include "src/frames-inl.h"
9#include "src/ostreams.h"
10#include "src/parser.h" // for CompileTimeValue; TODO(rossberg): should move
11#include "src/scopes.h"
12
13namespace v8 {
14namespace internal {
15
16
17AstTyper::AstTyper(CompilationInfo* info)
18 : info_(info),
19 oracle_(
20 handle(info->closure()->shared()->code()),
21 handle(info->closure()->shared()->feedback_vector()),
22 handle(info->closure()->context()->native_context()),
23 info->zone()),
24 store_(info->zone()) {
25 InitializeAstVisitor(info->zone());
26}
27
28
29#define RECURSE(call) \
30 do { \
31 DCHECK(!visitor->HasStackOverflow()); \
32 call; \
33 if (visitor->HasStackOverflow()) return; \
34 } while (false)
35
36void AstTyper::Run(CompilationInfo* info) {
37 AstTyper* visitor = new(info->zone()) AstTyper(info);
38 Scope* scope = info->scope();
39
40 // Handle implicit declaration of the function name in named function
41 // expressions before other declarations.
42 if (scope->is_function_scope() && scope->function() != NULL) {
43 RECURSE(visitor->VisitVariableDeclaration(scope->function()));
44 }
45 RECURSE(visitor->VisitDeclarations(scope->declarations()));
46 RECURSE(visitor->VisitStatements(info->function()->body()));
47}
48
49#undef RECURSE
50
51
52#ifdef OBJECT_PRINT
53 static void PrintObserved(Variable* var, Object* value, Type* type) {
54 OFStream os(stdout);
55 os << " observed " << (var->IsParameter() ? "param" : "local") << " ";
56 var->name()->Print(os);
57 os << " : " << Brief(value) << " -> ";
58 type->PrintTo(os);
Emily Bernierd0a1eb72015-03-24 16:35:39 -040059 os << std::endl;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000060 }
61#endif // OBJECT_PRINT
62
63
64Effect AstTyper::ObservedOnStack(Object* value) {
65 Type* lower = Type::NowOf(value, zone());
66 return Effect(Bounds(lower, Type::Any(zone())));
67}
68
69
70void AstTyper::ObserveTypesAtOsrEntry(IterationStatement* stmt) {
71 if (stmt->OsrEntryId() != info_->osr_ast_id()) return;
72
73 DisallowHeapAllocation no_gc;
74 JavaScriptFrameIterator it(isolate());
75 JavaScriptFrame* frame = it.frame();
76 Scope* scope = info_->scope();
77
78 // Assert that the frame on the stack belongs to the function we want to OSR.
79 DCHECK_EQ(*info_->closure(), frame->function());
80
81 int params = scope->num_parameters();
82 int locals = scope->StackLocalCount();
83
84 // Use sequential composition to achieve desired narrowing.
85 // The receiver is a parameter with index -1.
86 store_.Seq(parameter_index(-1), ObservedOnStack(frame->receiver()));
87 for (int i = 0; i < params; i++) {
88 store_.Seq(parameter_index(i), ObservedOnStack(frame->GetParameter(i)));
89 }
90
91 for (int i = 0; i < locals; i++) {
92 store_.Seq(stack_local_index(i), ObservedOnStack(frame->GetExpression(i)));
93 }
94
95#ifdef OBJECT_PRINT
96 if (FLAG_trace_osr && FLAG_print_scopes) {
97 PrintObserved(scope->receiver(),
98 frame->receiver(),
99 store_.LookupBounds(parameter_index(-1)).lower);
100
101 for (int i = 0; i < params; i++) {
102 PrintObserved(scope->parameter(i),
103 frame->GetParameter(i),
104 store_.LookupBounds(parameter_index(i)).lower);
105 }
106
107 ZoneList<Variable*> local_vars(locals, zone());
108 ZoneList<Variable*> context_vars(scope->ContextLocalCount(), zone());
109 scope->CollectStackAndContextLocals(&local_vars, &context_vars);
110 for (int i = 0; i < locals; i++) {
111 PrintObserved(local_vars.at(i),
112 frame->GetExpression(i),
113 store_.LookupBounds(stack_local_index(i)).lower);
114 }
115 }
116#endif // OBJECT_PRINT
117}
118
119
120#define RECURSE(call) \
121 do { \
122 DCHECK(!HasStackOverflow()); \
123 call; \
124 if (HasStackOverflow()) return; \
125 } while (false)
126
127
128void AstTyper::VisitStatements(ZoneList<Statement*>* stmts) {
129 for (int i = 0; i < stmts->length(); ++i) {
130 Statement* stmt = stmts->at(i);
131 RECURSE(Visit(stmt));
132 if (stmt->IsJump()) break;
133 }
134}
135
136
137void AstTyper::VisitBlock(Block* stmt) {
138 RECURSE(VisitStatements(stmt->statements()));
139 if (stmt->labels() != NULL) {
140 store_.Forget(); // Control may transfer here via 'break l'.
141 }
142}
143
144
145void AstTyper::VisitExpressionStatement(ExpressionStatement* stmt) {
146 RECURSE(Visit(stmt->expression()));
147}
148
149
150void AstTyper::VisitEmptyStatement(EmptyStatement* stmt) {
151}
152
153
154void AstTyper::VisitIfStatement(IfStatement* stmt) {
155 // Collect type feedback.
156 if (!stmt->condition()->ToBooleanIsTrue() &&
157 !stmt->condition()->ToBooleanIsFalse()) {
158 stmt->condition()->RecordToBooleanTypeFeedback(oracle());
159 }
160
161 RECURSE(Visit(stmt->condition()));
162 Effects then_effects = EnterEffects();
163 RECURSE(Visit(stmt->then_statement()));
164 ExitEffects();
165 Effects else_effects = EnterEffects();
166 RECURSE(Visit(stmt->else_statement()));
167 ExitEffects();
168 then_effects.Alt(else_effects);
169 store_.Seq(then_effects);
170}
171
172
173void AstTyper::VisitContinueStatement(ContinueStatement* stmt) {
174 // TODO(rossberg): is it worth having a non-termination effect?
175}
176
177
178void AstTyper::VisitBreakStatement(BreakStatement* stmt) {
179 // TODO(rossberg): is it worth having a non-termination effect?
180}
181
182
183void AstTyper::VisitReturnStatement(ReturnStatement* stmt) {
184 // Collect type feedback.
185 // TODO(rossberg): we only need this for inlining into test contexts...
186 stmt->expression()->RecordToBooleanTypeFeedback(oracle());
187
188 RECURSE(Visit(stmt->expression()));
189 // TODO(rossberg): is it worth having a non-termination effect?
190}
191
192
193void AstTyper::VisitWithStatement(WithStatement* stmt) {
194 RECURSE(stmt->expression());
195 RECURSE(stmt->statement());
196}
197
198
199void AstTyper::VisitSwitchStatement(SwitchStatement* stmt) {
200 RECURSE(Visit(stmt->tag()));
201
202 ZoneList<CaseClause*>* clauses = stmt->cases();
203 Effects local_effects(zone());
204 bool complex_effects = false; // True for label effects or fall-through.
205
206 for (int i = 0; i < clauses->length(); ++i) {
207 CaseClause* clause = clauses->at(i);
208
209 Effects clause_effects = EnterEffects();
210
211 if (!clause->is_default()) {
212 Expression* label = clause->label();
213 // Collect type feedback.
214 Type* tag_type;
215 Type* label_type;
216 Type* combined_type;
217 oracle()->CompareType(clause->CompareId(),
218 &tag_type, &label_type, &combined_type);
219 NarrowLowerType(stmt->tag(), tag_type);
220 NarrowLowerType(label, label_type);
221 clause->set_compare_type(combined_type);
222
223 RECURSE(Visit(label));
224 if (!clause_effects.IsEmpty()) complex_effects = true;
225 }
226
227 ZoneList<Statement*>* stmts = clause->statements();
228 RECURSE(VisitStatements(stmts));
229 ExitEffects();
230 if (stmts->is_empty() || stmts->last()->IsJump()) {
231 local_effects.Alt(clause_effects);
232 } else {
233 complex_effects = true;
234 }
235 }
236
237 if (complex_effects) {
238 store_.Forget(); // Reached this in unknown state.
239 } else {
240 store_.Seq(local_effects);
241 }
242}
243
244
245void AstTyper::VisitCaseClause(CaseClause* clause) {
246 UNREACHABLE();
247}
248
249
250void AstTyper::VisitDoWhileStatement(DoWhileStatement* stmt) {
251 // Collect type feedback.
252 if (!stmt->cond()->ToBooleanIsTrue()) {
253 stmt->cond()->RecordToBooleanTypeFeedback(oracle());
254 }
255
256 // TODO(rossberg): refine the unconditional Forget (here and elsewhere) by
257 // computing the set of variables assigned in only some of the origins of the
258 // control transfer (such as the loop body here).
259 store_.Forget(); // Control may transfer here via looping or 'continue'.
260 ObserveTypesAtOsrEntry(stmt);
261 RECURSE(Visit(stmt->body()));
262 RECURSE(Visit(stmt->cond()));
263 store_.Forget(); // Control may transfer here via 'break'.
264}
265
266
267void AstTyper::VisitWhileStatement(WhileStatement* stmt) {
268 // Collect type feedback.
269 if (!stmt->cond()->ToBooleanIsTrue()) {
270 stmt->cond()->RecordToBooleanTypeFeedback(oracle());
271 }
272
273 store_.Forget(); // Control may transfer here via looping or 'continue'.
274 RECURSE(Visit(stmt->cond()));
275 ObserveTypesAtOsrEntry(stmt);
276 RECURSE(Visit(stmt->body()));
277 store_.Forget(); // Control may transfer here via termination or 'break'.
278}
279
280
281void AstTyper::VisitForStatement(ForStatement* stmt) {
282 if (stmt->init() != NULL) {
283 RECURSE(Visit(stmt->init()));
284 }
285 store_.Forget(); // Control may transfer here via looping.
286 if (stmt->cond() != NULL) {
287 // Collect type feedback.
288 stmt->cond()->RecordToBooleanTypeFeedback(oracle());
289
290 RECURSE(Visit(stmt->cond()));
291 }
292 ObserveTypesAtOsrEntry(stmt);
293 RECURSE(Visit(stmt->body()));
294 if (stmt->next() != NULL) {
295 store_.Forget(); // Control may transfer here via 'continue'.
296 RECURSE(Visit(stmt->next()));
297 }
298 store_.Forget(); // Control may transfer here via termination or 'break'.
299}
300
301
302void AstTyper::VisitForInStatement(ForInStatement* stmt) {
303 // Collect type feedback.
304 stmt->set_for_in_type(static_cast<ForInStatement::ForInType>(
305 oracle()->ForInType(stmt->ForInFeedbackSlot())));
306
307 RECURSE(Visit(stmt->enumerable()));
308 store_.Forget(); // Control may transfer here via looping or 'continue'.
309 ObserveTypesAtOsrEntry(stmt);
310 RECURSE(Visit(stmt->body()));
311 store_.Forget(); // Control may transfer here via 'break'.
312}
313
314
315void AstTyper::VisitForOfStatement(ForOfStatement* stmt) {
316 RECURSE(Visit(stmt->iterable()));
317 store_.Forget(); // Control may transfer here via looping or 'continue'.
318 RECURSE(Visit(stmt->body()));
319 store_.Forget(); // Control may transfer here via 'break'.
320}
321
322
323void AstTyper::VisitTryCatchStatement(TryCatchStatement* stmt) {
324 Effects try_effects = EnterEffects();
325 RECURSE(Visit(stmt->try_block()));
326 ExitEffects();
327 Effects catch_effects = EnterEffects();
328 store_.Forget(); // Control may transfer here via 'throw'.
329 RECURSE(Visit(stmt->catch_block()));
330 ExitEffects();
331 try_effects.Alt(catch_effects);
332 store_.Seq(try_effects);
333 // At this point, only variables that were reassigned in the catch block are
334 // still remembered.
335}
336
337
338void AstTyper::VisitTryFinallyStatement(TryFinallyStatement* stmt) {
339 RECURSE(Visit(stmt->try_block()));
340 store_.Forget(); // Control may transfer here via 'throw'.
341 RECURSE(Visit(stmt->finally_block()));
342}
343
344
345void AstTyper::VisitDebuggerStatement(DebuggerStatement* stmt) {
346 store_.Forget(); // May do whatever.
347}
348
349
350void AstTyper::VisitFunctionLiteral(FunctionLiteral* expr) {
351 expr->InitializeSharedInfo(Handle<Code>(info_->closure()->shared()->code()));
352}
353
354
355void AstTyper::VisitClassLiteral(ClassLiteral* expr) {}
356
357
358void AstTyper::VisitNativeFunctionLiteral(NativeFunctionLiteral* expr) {
359}
360
361
362void AstTyper::VisitConditional(Conditional* expr) {
363 // Collect type feedback.
364 expr->condition()->RecordToBooleanTypeFeedback(oracle());
365
366 RECURSE(Visit(expr->condition()));
367 Effects then_effects = EnterEffects();
368 RECURSE(Visit(expr->then_expression()));
369 ExitEffects();
370 Effects else_effects = EnterEffects();
371 RECURSE(Visit(expr->else_expression()));
372 ExitEffects();
373 then_effects.Alt(else_effects);
374 store_.Seq(then_effects);
375
376 NarrowType(expr, Bounds::Either(
377 expr->then_expression()->bounds(),
378 expr->else_expression()->bounds(), zone()));
379}
380
381
382void AstTyper::VisitVariableProxy(VariableProxy* expr) {
383 Variable* var = expr->var();
384 if (var->IsStackAllocated()) {
385 NarrowType(expr, store_.LookupBounds(variable_index(var)));
386 }
387}
388
389
390void AstTyper::VisitLiteral(Literal* expr) {
391 Type* type = Type::Constant(expr->value(), zone());
392 NarrowType(expr, Bounds(type));
393}
394
395
396void AstTyper::VisitRegExpLiteral(RegExpLiteral* expr) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400397 // TODO(rossberg): Reintroduce RegExp type.
398 NarrowType(expr, Bounds(Type::Object(zone())));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000399}
400
401
402void AstTyper::VisitObjectLiteral(ObjectLiteral* expr) {
403 ZoneList<ObjectLiteral::Property*>* properties = expr->properties();
404 for (int i = 0; i < properties->length(); ++i) {
405 ObjectLiteral::Property* prop = properties->at(i);
406
407 // Collect type feedback.
408 if ((prop->kind() == ObjectLiteral::Property::MATERIALIZED_LITERAL &&
409 !CompileTimeValue::IsCompileTimeValue(prop->value())) ||
410 prop->kind() == ObjectLiteral::Property::COMPUTED) {
411 if (prop->key()->value()->IsInternalizedString() && prop->emit_store()) {
412 prop->RecordTypeFeedback(oracle());
413 }
414 }
415
416 RECURSE(Visit(prop->value()));
417 }
418
419 NarrowType(expr, Bounds(Type::Object(zone())));
420}
421
422
423void AstTyper::VisitArrayLiteral(ArrayLiteral* expr) {
424 ZoneList<Expression*>* values = expr->values();
425 for (int i = 0; i < values->length(); ++i) {
426 Expression* value = values->at(i);
427 RECURSE(Visit(value));
428 }
429
430 NarrowType(expr, Bounds(Type::Array(zone())));
431}
432
433
434void AstTyper::VisitAssignment(Assignment* expr) {
435 // Collect type feedback.
436 Property* prop = expr->target()->AsProperty();
437 if (prop != NULL) {
438 TypeFeedbackId id = expr->AssignmentFeedbackId();
439 expr->set_is_uninitialized(oracle()->StoreIsUninitialized(id));
440 if (!expr->IsUninitialized()) {
441 if (prop->key()->IsPropertyName()) {
442 Literal* lit_key = prop->key()->AsLiteral();
443 DCHECK(lit_key != NULL && lit_key->value()->IsString());
444 Handle<String> name = Handle<String>::cast(lit_key->value());
445 oracle()->AssignmentReceiverTypes(id, name, expr->GetReceiverTypes());
446 } else {
447 KeyedAccessStoreMode store_mode;
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400448 IcCheckType key_type;
449 oracle()->KeyedAssignmentReceiverTypes(id, expr->GetReceiverTypes(),
450 &store_mode, &key_type);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000451 expr->set_store_mode(store_mode);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400452 expr->set_key_type(key_type);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000453 }
454 }
455 }
456
457 Expression* rhs =
458 expr->is_compound() ? expr->binary_operation() : expr->value();
459 RECURSE(Visit(expr->target()));
460 RECURSE(Visit(rhs));
461 NarrowType(expr, rhs->bounds());
462
463 VariableProxy* proxy = expr->target()->AsVariableProxy();
464 if (proxy != NULL && proxy->var()->IsStackAllocated()) {
465 store_.Seq(variable_index(proxy->var()), Effect(expr->bounds()));
466 }
467}
468
469
470void AstTyper::VisitYield(Yield* expr) {
471 RECURSE(Visit(expr->generator_object()));
472 RECURSE(Visit(expr->expression()));
473
474 // We don't know anything about the result type.
475}
476
477
478void AstTyper::VisitThrow(Throw* expr) {
479 RECURSE(Visit(expr->exception()));
480 // TODO(rossberg): is it worth having a non-termination effect?
481
482 NarrowType(expr, Bounds(Type::None(zone())));
483}
484
485
486void AstTyper::VisitProperty(Property* expr) {
487 // Collect type feedback.
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400488 FeedbackVectorICSlot slot(FeedbackVectorICSlot::Invalid());
489 TypeFeedbackId id(TypeFeedbackId::None());
490 if (FLAG_vector_ics) {
491 slot = expr->PropertyFeedbackSlot();
492 expr->set_is_uninitialized(oracle()->LoadIsUninitialized(slot));
493 } else {
494 id = expr->PropertyFeedbackId();
495 expr->set_is_uninitialized(oracle()->LoadIsUninitialized(id));
496 }
497
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000498 if (!expr->IsUninitialized()) {
499 if (expr->key()->IsPropertyName()) {
500 Literal* lit_key = expr->key()->AsLiteral();
501 DCHECK(lit_key != NULL && lit_key->value()->IsString());
502 Handle<String> name = Handle<String>::cast(lit_key->value());
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400503 if (FLAG_vector_ics) {
504 oracle()->PropertyReceiverTypes(slot, name, expr->GetReceiverTypes());
505 } else {
506 oracle()->PropertyReceiverTypes(id, name, expr->GetReceiverTypes());
507 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000508 } else {
509 bool is_string;
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400510 IcCheckType key_type;
511 if (FLAG_vector_ics) {
512 oracle()->KeyedPropertyReceiverTypes(slot, expr->GetReceiverTypes(),
513 &is_string, &key_type);
514 } else {
515 oracle()->KeyedPropertyReceiverTypes(id, expr->GetReceiverTypes(),
516 &is_string, &key_type);
517 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000518 expr->set_is_string_access(is_string);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400519 expr->set_key_type(key_type);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000520 }
521 }
522
523 RECURSE(Visit(expr->obj()));
524 RECURSE(Visit(expr->key()));
525
526 // We don't know anything about the result type.
527}
528
529
530void AstTyper::VisitCall(Call* expr) {
531 // Collect type feedback.
532 RECURSE(Visit(expr->expression()));
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400533 bool is_uninitialized = true;
534 if (expr->IsUsingCallFeedbackSlot(isolate())) {
535 FeedbackVectorICSlot slot = expr->CallFeedbackSlot();
536 is_uninitialized = oracle()->CallIsUninitialized(slot);
537 if (!expr->expression()->IsProperty() &&
538 oracle()->CallIsMonomorphic(slot)) {
539 expr->set_target(oracle()->GetCallTarget(slot));
540 Handle<AllocationSite> site = oracle()->GetCallAllocationSite(slot);
541 expr->set_allocation_site(site);
542 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000543 }
544
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400545 expr->set_is_uninitialized(is_uninitialized);
546
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000547 ZoneList<Expression*>* args = expr->arguments();
548 for (int i = 0; i < args->length(); ++i) {
549 Expression* arg = args->at(i);
550 RECURSE(Visit(arg));
551 }
552
553 VariableProxy* proxy = expr->expression()->AsVariableProxy();
554 if (proxy != NULL && proxy->var()->is_possibly_eval(isolate())) {
555 store_.Forget(); // Eval could do whatever to local variables.
556 }
557
558 // We don't know anything about the result type.
559}
560
561
562void AstTyper::VisitCallNew(CallNew* expr) {
563 // Collect type feedback.
564 expr->RecordTypeFeedback(oracle());
565
566 RECURSE(Visit(expr->expression()));
567 ZoneList<Expression*>* args = expr->arguments();
568 for (int i = 0; i < args->length(); ++i) {
569 Expression* arg = args->at(i);
570 RECURSE(Visit(arg));
571 }
572
573 NarrowType(expr, Bounds(Type::None(zone()), Type::Receiver(zone())));
574}
575
576
577void AstTyper::VisitCallRuntime(CallRuntime* expr) {
578 ZoneList<Expression*>* args = expr->arguments();
579 for (int i = 0; i < args->length(); ++i) {
580 Expression* arg = args->at(i);
581 RECURSE(Visit(arg));
582 }
583
584 // We don't know anything about the result type.
585}
586
587
588void AstTyper::VisitUnaryOperation(UnaryOperation* expr) {
589 // Collect type feedback.
590 if (expr->op() == Token::NOT) {
591 // TODO(rossberg): only do in test or value context.
592 expr->expression()->RecordToBooleanTypeFeedback(oracle());
593 }
594
595 RECURSE(Visit(expr->expression()));
596
597 switch (expr->op()) {
598 case Token::NOT:
599 case Token::DELETE:
600 NarrowType(expr, Bounds(Type::Boolean(zone())));
601 break;
602 case Token::VOID:
603 NarrowType(expr, Bounds(Type::Undefined(zone())));
604 break;
605 case Token::TYPEOF:
606 NarrowType(expr, Bounds(Type::InternalizedString(zone())));
607 break;
608 default:
609 UNREACHABLE();
610 }
611}
612
613
614void AstTyper::VisitCountOperation(CountOperation* expr) {
615 // Collect type feedback.
616 TypeFeedbackId store_id = expr->CountStoreFeedbackId();
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400617 KeyedAccessStoreMode store_mode;
618 IcCheckType key_type;
619 oracle()->GetStoreModeAndKeyType(store_id, &store_mode, &key_type);
620 expr->set_store_mode(store_mode);
621 expr->set_key_type(key_type);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000622 oracle()->CountReceiverTypes(store_id, expr->GetReceiverTypes());
623 expr->set_type(oracle()->CountType(expr->CountBinOpFeedbackId()));
624 // TODO(rossberg): merge the count type with the generic expression type.
625
626 RECURSE(Visit(expr->expression()));
627
628 NarrowType(expr, Bounds(Type::SignedSmall(zone()), Type::Number(zone())));
629
630 VariableProxy* proxy = expr->expression()->AsVariableProxy();
631 if (proxy != NULL && proxy->var()->IsStackAllocated()) {
632 store_.Seq(variable_index(proxy->var()), Effect(expr->bounds()));
633 }
634}
635
636
637void AstTyper::VisitBinaryOperation(BinaryOperation* expr) {
638 // Collect type feedback.
639 Type* type;
640 Type* left_type;
641 Type* right_type;
642 Maybe<int> fixed_right_arg;
643 Handle<AllocationSite> allocation_site;
644 oracle()->BinaryType(expr->BinaryOperationFeedbackId(),
645 &left_type, &right_type, &type, &fixed_right_arg,
646 &allocation_site, expr->op());
647 NarrowLowerType(expr, type);
648 NarrowLowerType(expr->left(), left_type);
649 NarrowLowerType(expr->right(), right_type);
650 expr->set_allocation_site(allocation_site);
651 expr->set_fixed_right_arg(fixed_right_arg);
652 if (expr->op() == Token::OR || expr->op() == Token::AND) {
653 expr->left()->RecordToBooleanTypeFeedback(oracle());
654 }
655
656 switch (expr->op()) {
657 case Token::COMMA:
658 RECURSE(Visit(expr->left()));
659 RECURSE(Visit(expr->right()));
660 NarrowType(expr, expr->right()->bounds());
661 break;
662 case Token::OR:
663 case Token::AND: {
664 Effects left_effects = EnterEffects();
665 RECURSE(Visit(expr->left()));
666 ExitEffects();
667 Effects right_effects = EnterEffects();
668 RECURSE(Visit(expr->right()));
669 ExitEffects();
670 left_effects.Alt(right_effects);
671 store_.Seq(left_effects);
672
673 NarrowType(expr, Bounds::Either(
674 expr->left()->bounds(), expr->right()->bounds(), zone()));
675 break;
676 }
677 case Token::BIT_OR:
678 case Token::BIT_AND: {
679 RECURSE(Visit(expr->left()));
680 RECURSE(Visit(expr->right()));
681 Type* upper = Type::Union(
682 expr->left()->bounds().upper, expr->right()->bounds().upper, zone());
683 if (!upper->Is(Type::Signed32())) upper = Type::Signed32(zone());
684 Type* lower = Type::Intersect(Type::SignedSmall(zone()), upper, zone());
685 NarrowType(expr, Bounds(lower, upper));
686 break;
687 }
688 case Token::BIT_XOR:
689 case Token::SHL:
690 case Token::SAR:
691 RECURSE(Visit(expr->left()));
692 RECURSE(Visit(expr->right()));
693 NarrowType(expr,
694 Bounds(Type::SignedSmall(zone()), Type::Signed32(zone())));
695 break;
696 case Token::SHR:
697 RECURSE(Visit(expr->left()));
698 RECURSE(Visit(expr->right()));
699 // TODO(rossberg): The upper bound would be Unsigned32, but since there
700 // is no 'positive Smi' type for the lower bound, we use the smallest
701 // union of Smi and Unsigned32 as upper bound instead.
702 NarrowType(expr, Bounds(Type::SignedSmall(zone()), Type::Number(zone())));
703 break;
704 case Token::ADD: {
705 RECURSE(Visit(expr->left()));
706 RECURSE(Visit(expr->right()));
707 Bounds l = expr->left()->bounds();
708 Bounds r = expr->right()->bounds();
709 Type* lower =
710 !l.lower->IsInhabited() || !r.lower->IsInhabited() ?
711 Type::None(zone()) :
712 l.lower->Is(Type::String()) || r.lower->Is(Type::String()) ?
713 Type::String(zone()) :
714 l.lower->Is(Type::Number()) && r.lower->Is(Type::Number()) ?
715 Type::SignedSmall(zone()) : Type::None(zone());
716 Type* upper =
717 l.upper->Is(Type::String()) || r.upper->Is(Type::String()) ?
718 Type::String(zone()) :
719 l.upper->Is(Type::Number()) && r.upper->Is(Type::Number()) ?
720 Type::Number(zone()) : Type::NumberOrString(zone());
721 NarrowType(expr, Bounds(lower, upper));
722 break;
723 }
724 case Token::SUB:
725 case Token::MUL:
726 case Token::DIV:
727 case Token::MOD:
728 RECURSE(Visit(expr->left()));
729 RECURSE(Visit(expr->right()));
730 NarrowType(expr, Bounds(Type::SignedSmall(zone()), Type::Number(zone())));
731 break;
732 default:
733 UNREACHABLE();
734 }
735}
736
737
738void AstTyper::VisitCompareOperation(CompareOperation* expr) {
739 // Collect type feedback.
740 Type* left_type;
741 Type* right_type;
742 Type* combined_type;
743 oracle()->CompareType(expr->CompareOperationFeedbackId(),
744 &left_type, &right_type, &combined_type);
745 NarrowLowerType(expr->left(), left_type);
746 NarrowLowerType(expr->right(), right_type);
747 expr->set_combined_type(combined_type);
748
749 RECURSE(Visit(expr->left()));
750 RECURSE(Visit(expr->right()));
751
752 NarrowType(expr, Bounds(Type::Boolean(zone())));
753}
754
755
756void AstTyper::VisitThisFunction(ThisFunction* expr) {
757}
758
759
760void AstTyper::VisitSuperReference(SuperReference* expr) {}
761
762
763void AstTyper::VisitDeclarations(ZoneList<Declaration*>* decls) {
764 for (int i = 0; i < decls->length(); ++i) {
765 Declaration* decl = decls->at(i);
766 RECURSE(Visit(decl));
767 }
768}
769
770
771void AstTyper::VisitVariableDeclaration(VariableDeclaration* declaration) {
772}
773
774
775void AstTyper::VisitFunctionDeclaration(FunctionDeclaration* declaration) {
776 RECURSE(Visit(declaration->fun()));
777}
778
779
780void AstTyper::VisitModuleDeclaration(ModuleDeclaration* declaration) {
781 RECURSE(Visit(declaration->module()));
782}
783
784
785void AstTyper::VisitImportDeclaration(ImportDeclaration* declaration) {
786 RECURSE(Visit(declaration->module()));
787}
788
789
790void AstTyper::VisitExportDeclaration(ExportDeclaration* declaration) {
791}
792
793
794void AstTyper::VisitModuleLiteral(ModuleLiteral* module) {
795 RECURSE(Visit(module->body()));
796}
797
798
799void AstTyper::VisitModuleVariable(ModuleVariable* module) {
800}
801
802
803void AstTyper::VisitModulePath(ModulePath* module) {
804 RECURSE(Visit(module->module()));
805}
806
807
808void AstTyper::VisitModuleUrl(ModuleUrl* module) {
809}
810
811
812void AstTyper::VisitModuleStatement(ModuleStatement* stmt) {
813 RECURSE(Visit(stmt->body()));
814}
815
816
817} } // namespace v8::internal