Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / 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_(
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
36 void 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);
59     os << std::endl;
60   }
61 #endif  // OBJECT_PRINT
62
63
64 Effect AstTyper::ObservedOnStack(Object* value) {
65   Type* lower = Type::NowOf(value, zone());
66   return Effect(Bounds(lower, Type::Any(zone())));
67 }
68
69
70 void 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
128 void 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
137 void 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
145 void AstTyper::VisitExpressionStatement(ExpressionStatement* stmt) {
146   RECURSE(Visit(stmt->expression()));
147 }
148
149
150 void AstTyper::VisitEmptyStatement(EmptyStatement* stmt) {
151 }
152
153
154 void 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
173 void AstTyper::VisitContinueStatement(ContinueStatement* stmt) {
174   // TODO(rossberg): is it worth having a non-termination effect?
175 }
176
177
178 void AstTyper::VisitBreakStatement(BreakStatement* stmt) {
179   // TODO(rossberg): is it worth having a non-termination effect?
180 }
181
182
183 void 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
193 void AstTyper::VisitWithStatement(WithStatement* stmt) {
194   RECURSE(stmt->expression());
195   RECURSE(stmt->statement());
196 }
197
198
199 void 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
245 void AstTyper::VisitCaseClause(CaseClause* clause) {
246   UNREACHABLE();
247 }
248
249
250 void 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
267 void 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
281 void 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
302 void 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
315 void 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
323 void 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
338 void 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
345 void AstTyper::VisitDebuggerStatement(DebuggerStatement* stmt) {
346   store_.Forget();  // May do whatever.
347 }
348
349
350 void AstTyper::VisitFunctionLiteral(FunctionLiteral* expr) {
351   expr->InitializeSharedInfo(Handle<Code>(info_->closure()->shared()->code()));
352 }
353
354
355 void AstTyper::VisitClassLiteral(ClassLiteral* expr) {}
356
357
358 void AstTyper::VisitNativeFunctionLiteral(NativeFunctionLiteral* expr) {
359 }
360
361
362 void 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
382 void 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
390 void AstTyper::VisitLiteral(Literal* expr) {
391   Type* type = Type::Constant(expr->value(), zone());
392   NarrowType(expr, Bounds(type));
393 }
394
395
396 void AstTyper::VisitRegExpLiteral(RegExpLiteral* expr) {
397   NarrowType(expr, Bounds(Type::RegExp(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->key()->value()->IsInternalizedString() && prop->emit_store()) {
411         prop->RecordTypeFeedback(oracle());
412       }
413     }
414
415     RECURSE(Visit(prop->value()));
416   }
417
418   NarrowType(expr, Bounds(Type::Object(zone())));
419 }
420
421
422 void AstTyper::VisitArrayLiteral(ArrayLiteral* expr) {
423   ZoneList<Expression*>* values = expr->values();
424   for (int i = 0; i < values->length(); ++i) {
425     Expression* value = values->at(i);
426     RECURSE(Visit(value));
427   }
428
429   NarrowType(expr, Bounds(Type::Array(zone())));
430 }
431
432
433 void AstTyper::VisitAssignment(Assignment* expr) {
434   // Collect type feedback.
435   Property* prop = expr->target()->AsProperty();
436   if (prop != NULL) {
437     TypeFeedbackId id = expr->AssignmentFeedbackId();
438     expr->set_is_uninitialized(oracle()->StoreIsUninitialized(id));
439     if (!expr->IsUninitialized()) {
440       if (prop->key()->IsPropertyName()) {
441         Literal* lit_key = prop->key()->AsLiteral();
442         DCHECK(lit_key != NULL && lit_key->value()->IsString());
443         Handle<String> name = Handle<String>::cast(lit_key->value());
444         oracle()->AssignmentReceiverTypes(id, name, expr->GetReceiverTypes());
445       } else {
446         KeyedAccessStoreMode store_mode;
447         IcCheckType key_type;
448         oracle()->KeyedAssignmentReceiverTypes(id, expr->GetReceiverTypes(),
449                                                &store_mode, &key_type);
450         expr->set_store_mode(store_mode);
451         expr->set_key_type(key_type);
452       }
453     }
454   }
455
456   Expression* rhs =
457       expr->is_compound() ? expr->binary_operation() : expr->value();
458   RECURSE(Visit(expr->target()));
459   RECURSE(Visit(rhs));
460   NarrowType(expr, rhs->bounds());
461
462   VariableProxy* proxy = expr->target()->AsVariableProxy();
463   if (proxy != NULL && proxy->var()->IsStackAllocated()) {
464     store_.Seq(variable_index(proxy->var()), Effect(expr->bounds()));
465   }
466 }
467
468
469 void AstTyper::VisitYield(Yield* expr) {
470   RECURSE(Visit(expr->generator_object()));
471   RECURSE(Visit(expr->expression()));
472
473   // We don't know anything about the result type.
474 }
475
476
477 void AstTyper::VisitThrow(Throw* expr) {
478   RECURSE(Visit(expr->exception()));
479   // TODO(rossberg): is it worth having a non-termination effect?
480
481   NarrowType(expr, Bounds(Type::None(zone())));
482 }
483
484
485 void AstTyper::VisitProperty(Property* expr) {
486   // Collect type feedback.
487   TypeFeedbackId id = expr->PropertyFeedbackId();
488   expr->set_is_uninitialized(oracle()->LoadIsUninitialized(id));
489   if (!expr->IsUninitialized()) {
490     if (expr->key()->IsPropertyName()) {
491       Literal* lit_key = expr->key()->AsLiteral();
492       DCHECK(lit_key != NULL && lit_key->value()->IsString());
493       Handle<String> name = Handle<String>::cast(lit_key->value());
494       oracle()->PropertyReceiverTypes(id, name, expr->GetReceiverTypes());
495     } else {
496       bool is_string;
497       oracle()->KeyedPropertyReceiverTypes(
498           id, expr->GetReceiverTypes(), &is_string);
499       expr->set_is_string_access(is_string);
500     }
501   }
502
503   RECURSE(Visit(expr->obj()));
504   RECURSE(Visit(expr->key()));
505
506   // We don't know anything about the result type.
507 }
508
509
510 void AstTyper::VisitCall(Call* expr) {
511   // Collect type feedback.
512   RECURSE(Visit(expr->expression()));
513   if (!expr->expression()->IsProperty() &&
514       expr->IsUsingCallFeedbackSlot(isolate()) &&
515       oracle()->CallIsMonomorphic(expr->CallFeedbackSlot())) {
516     expr->set_target(oracle()->GetCallTarget(expr->CallFeedbackSlot()));
517     Handle<AllocationSite> site =
518         oracle()->GetCallAllocationSite(expr->CallFeedbackSlot());
519     expr->set_allocation_site(site);
520   }
521
522   ZoneList<Expression*>* args = expr->arguments();
523   for (int i = 0; i < args->length(); ++i) {
524     Expression* arg = args->at(i);
525     RECURSE(Visit(arg));
526   }
527
528   VariableProxy* proxy = expr->expression()->AsVariableProxy();
529   if (proxy != NULL && proxy->var()->is_possibly_eval(isolate())) {
530     store_.Forget();  // Eval could do whatever to local variables.
531   }
532
533   // We don't know anything about the result type.
534 }
535
536
537 void AstTyper::VisitCallNew(CallNew* expr) {
538   // Collect type feedback.
539   expr->RecordTypeFeedback(oracle());
540
541   RECURSE(Visit(expr->expression()));
542   ZoneList<Expression*>* args = expr->arguments();
543   for (int i = 0; i < args->length(); ++i) {
544     Expression* arg = args->at(i);
545     RECURSE(Visit(arg));
546   }
547
548   NarrowType(expr, Bounds(Type::None(zone()), Type::Receiver(zone())));
549 }
550
551
552 void AstTyper::VisitCallRuntime(CallRuntime* expr) {
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   // We don't know anything about the result type.
560 }
561
562
563 void AstTyper::VisitUnaryOperation(UnaryOperation* expr) {
564   // Collect type feedback.
565   if (expr->op() == Token::NOT) {
566     // TODO(rossberg): only do in test or value context.
567     expr->expression()->RecordToBooleanTypeFeedback(oracle());
568   }
569
570   RECURSE(Visit(expr->expression()));
571
572   switch (expr->op()) {
573     case Token::NOT:
574     case Token::DELETE:
575       NarrowType(expr, Bounds(Type::Boolean(zone())));
576       break;
577     case Token::VOID:
578       NarrowType(expr, Bounds(Type::Undefined(zone())));
579       break;
580     case Token::TYPEOF:
581       NarrowType(expr, Bounds(Type::InternalizedString(zone())));
582       break;
583     default:
584       UNREACHABLE();
585   }
586 }
587
588
589 void AstTyper::VisitCountOperation(CountOperation* expr) {
590   // Collect type feedback.
591   TypeFeedbackId store_id = expr->CountStoreFeedbackId();
592   KeyedAccessStoreMode store_mode;
593   IcCheckType key_type;
594   oracle()->GetStoreModeAndKeyType(store_id, &store_mode, &key_type);
595   expr->set_store_mode(store_mode);
596   expr->set_key_type(key_type);
597   oracle()->CountReceiverTypes(store_id, expr->GetReceiverTypes());
598   expr->set_type(oracle()->CountType(expr->CountBinOpFeedbackId()));
599   // TODO(rossberg): merge the count type with the generic expression type.
600
601   RECURSE(Visit(expr->expression()));
602
603   NarrowType(expr, Bounds(Type::SignedSmall(zone()), Type::Number(zone())));
604
605   VariableProxy* proxy = expr->expression()->AsVariableProxy();
606   if (proxy != NULL && proxy->var()->IsStackAllocated()) {
607     store_.Seq(variable_index(proxy->var()), Effect(expr->bounds()));
608   }
609 }
610
611
612 void AstTyper::VisitBinaryOperation(BinaryOperation* expr) {
613   // Collect type feedback.
614   Type* type;
615   Type* left_type;
616   Type* right_type;
617   Maybe<int> fixed_right_arg;
618   Handle<AllocationSite> allocation_site;
619   oracle()->BinaryType(expr->BinaryOperationFeedbackId(),
620       &left_type, &right_type, &type, &fixed_right_arg,
621       &allocation_site, expr->op());
622   NarrowLowerType(expr, type);
623   NarrowLowerType(expr->left(), left_type);
624   NarrowLowerType(expr->right(), right_type);
625   expr->set_allocation_site(allocation_site);
626   expr->set_fixed_right_arg(fixed_right_arg);
627   if (expr->op() == Token::OR || expr->op() == Token::AND) {
628     expr->left()->RecordToBooleanTypeFeedback(oracle());
629   }
630
631   switch (expr->op()) {
632     case Token::COMMA:
633       RECURSE(Visit(expr->left()));
634       RECURSE(Visit(expr->right()));
635       NarrowType(expr, expr->right()->bounds());
636       break;
637     case Token::OR:
638     case Token::AND: {
639       Effects left_effects = EnterEffects();
640       RECURSE(Visit(expr->left()));
641       ExitEffects();
642       Effects right_effects = EnterEffects();
643       RECURSE(Visit(expr->right()));
644       ExitEffects();
645       left_effects.Alt(right_effects);
646       store_.Seq(left_effects);
647
648       NarrowType(expr, Bounds::Either(
649           expr->left()->bounds(), expr->right()->bounds(), zone()));
650       break;
651     }
652     case Token::BIT_OR:
653     case Token::BIT_AND: {
654       RECURSE(Visit(expr->left()));
655       RECURSE(Visit(expr->right()));
656       Type* upper = Type::Union(
657           expr->left()->bounds().upper, expr->right()->bounds().upper, zone());
658       if (!upper->Is(Type::Signed32())) upper = Type::Signed32(zone());
659       Type* lower = Type::Intersect(Type::SignedSmall(zone()), upper, zone());
660       NarrowType(expr, Bounds(lower, upper));
661       break;
662     }
663     case Token::BIT_XOR:
664     case Token::SHL:
665     case Token::SAR:
666       RECURSE(Visit(expr->left()));
667       RECURSE(Visit(expr->right()));
668       NarrowType(expr,
669           Bounds(Type::SignedSmall(zone()), Type::Signed32(zone())));
670       break;
671     case Token::SHR:
672       RECURSE(Visit(expr->left()));
673       RECURSE(Visit(expr->right()));
674       // TODO(rossberg): The upper bound would be Unsigned32, but since there
675       // is no 'positive Smi' type for the lower bound, we use the smallest
676       // union of Smi and Unsigned32 as upper bound instead.
677       NarrowType(expr, Bounds(Type::SignedSmall(zone()), Type::Number(zone())));
678       break;
679     case Token::ADD: {
680       RECURSE(Visit(expr->left()));
681       RECURSE(Visit(expr->right()));
682       Bounds l = expr->left()->bounds();
683       Bounds r = expr->right()->bounds();
684       Type* lower =
685           !l.lower->IsInhabited() || !r.lower->IsInhabited() ?
686               Type::None(zone()) :
687           l.lower->Is(Type::String()) || r.lower->Is(Type::String()) ?
688               Type::String(zone()) :
689           l.lower->Is(Type::Number()) && r.lower->Is(Type::Number()) ?
690               Type::SignedSmall(zone()) : Type::None(zone());
691       Type* upper =
692           l.upper->Is(Type::String()) || r.upper->Is(Type::String()) ?
693               Type::String(zone()) :
694           l.upper->Is(Type::Number()) && r.upper->Is(Type::Number()) ?
695               Type::Number(zone()) : Type::NumberOrString(zone());
696       NarrowType(expr, Bounds(lower, upper));
697       break;
698     }
699     case Token::SUB:
700     case Token::MUL:
701     case Token::DIV:
702     case Token::MOD:
703       RECURSE(Visit(expr->left()));
704       RECURSE(Visit(expr->right()));
705       NarrowType(expr, Bounds(Type::SignedSmall(zone()), Type::Number(zone())));
706       break;
707     default:
708       UNREACHABLE();
709   }
710 }
711
712
713 void AstTyper::VisitCompareOperation(CompareOperation* expr) {
714   // Collect type feedback.
715   Type* left_type;
716   Type* right_type;
717   Type* combined_type;
718   oracle()->CompareType(expr->CompareOperationFeedbackId(),
719       &left_type, &right_type, &combined_type);
720   NarrowLowerType(expr->left(), left_type);
721   NarrowLowerType(expr->right(), right_type);
722   expr->set_combined_type(combined_type);
723
724   RECURSE(Visit(expr->left()));
725   RECURSE(Visit(expr->right()));
726
727   NarrowType(expr, Bounds(Type::Boolean(zone())));
728 }
729
730
731 void AstTyper::VisitThisFunction(ThisFunction* expr) {
732 }
733
734
735 void AstTyper::VisitSuperReference(SuperReference* expr) {}
736
737
738 void AstTyper::VisitDeclarations(ZoneList<Declaration*>* decls) {
739   for (int i = 0; i < decls->length(); ++i) {
740     Declaration* decl = decls->at(i);
741     RECURSE(Visit(decl));
742   }
743 }
744
745
746 void AstTyper::VisitVariableDeclaration(VariableDeclaration* declaration) {
747 }
748
749
750 void AstTyper::VisitFunctionDeclaration(FunctionDeclaration* declaration) {
751   RECURSE(Visit(declaration->fun()));
752 }
753
754
755 void AstTyper::VisitModuleDeclaration(ModuleDeclaration* declaration) {
756   RECURSE(Visit(declaration->module()));
757 }
758
759
760 void AstTyper::VisitImportDeclaration(ImportDeclaration* declaration) {
761   RECURSE(Visit(declaration->module()));
762 }
763
764
765 void AstTyper::VisitExportDeclaration(ExportDeclaration* declaration) {
766 }
767
768
769 void AstTyper::VisitModuleLiteral(ModuleLiteral* module) {
770   RECURSE(Visit(module->body()));
771 }
772
773
774 void AstTyper::VisitModuleVariable(ModuleVariable* module) {
775 }
776
777
778 void AstTyper::VisitModulePath(ModulePath* module) {
779   RECURSE(Visit(module->module()));
780 }
781
782
783 void AstTyper::VisitModuleUrl(ModuleUrl* module) {
784 }
785
786
787 void AstTyper::VisitModuleStatement(ModuleStatement* stmt) {
788   RECURSE(Visit(stmt->body()));
789 }
790
791
792 } }  // namespace v8::internal