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