Derive synthetic type bounds for expressions

Currently synthesizes Smi as the lower bound for numeric operations (except for +, which might result in a string). That would need to change in places where we want to track constants as type feedback.

Does not do anything about variables yet.

R=jkummerow@chromium.org
BUG=

Review URL: https://codereview.chromium.org/17842004

git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@15422 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
This commit is contained in:
rossberg@chromium.org 2013-07-01 13:59:18 +00:00
parent 493d1f1c21
commit fd484c9df9
2 changed files with 210 additions and 121 deletions

View File

@ -45,13 +45,13 @@ AstTyper::AstTyper(CompilationInfo* info)
} }
#define CHECK_ALIVE(call) \ #define RECURSE(call) \
do { \ do { \
ASSERT(!visitor->HasStackOverflow()); \
call; \ call; \
if (visitor->HasStackOverflow()) return; \ if (visitor->HasStackOverflow()) return; \
} while (false) } while (false)
void AstTyper::Run(CompilationInfo* info) { void AstTyper::Run(CompilationInfo* info) {
AstTyper* visitor = new(info->zone()) AstTyper(info); AstTyper* visitor = new(info->zone()) AstTyper(info);
Scope* scope = info->scope(); Scope* scope = info->scope();
@ -59,52 +59,48 @@ void AstTyper::Run(CompilationInfo* info) {
// Handle implicit declaration of the function name in named function // Handle implicit declaration of the function name in named function
// expressions before other declarations. // expressions before other declarations.
if (scope->is_function_scope() && scope->function() != NULL) { if (scope->is_function_scope() && scope->function() != NULL) {
CHECK_ALIVE(visitor->VisitVariableDeclaration(scope->function())); RECURSE(visitor->VisitVariableDeclaration(scope->function()));
} }
CHECK_ALIVE(visitor->VisitDeclarations(scope->declarations())); RECURSE(visitor->VisitDeclarations(scope->declarations()));
CHECK_ALIVE(visitor->VisitStatements(info->function()->body())); RECURSE(visitor->VisitStatements(info->function()->body()));
} }
#undef RECURSE
#undef CHECK_ALIVE #define RECURSE(call) \
#define CHECK_ALIVE(call) \
do { \ do { \
ASSERT(!HasStackOverflow()); \
call; \ call; \
if (HasStackOverflow()) return; \ if (HasStackOverflow()) return; \
} while (false) } while (false)
void AstTyper::VisitStatements(ZoneList<Statement*>* stmts) { void AstTyper::VisitStatements(ZoneList<Statement*>* stmts) {
ASSERT(!HasStackOverflow());
for (int i = 0; i < stmts->length(); ++i) { for (int i = 0; i < stmts->length(); ++i) {
Statement* stmt = stmts->at(i); Statement* stmt = stmts->at(i);
CHECK_ALIVE(Visit(stmt)); RECURSE(Visit(stmt));
} }
} }
void AstTyper::VisitBlock(Block* stmt) { void AstTyper::VisitBlock(Block* stmt) {
ASSERT(!HasStackOverflow()); RECURSE(VisitStatements(stmt->statements()));
CHECK_ALIVE(VisitStatements(stmt->statements()));
} }
void AstTyper::VisitExpressionStatement(ExpressionStatement* stmt) { void AstTyper::VisitExpressionStatement(ExpressionStatement* stmt) {
ASSERT(!HasStackOverflow()); RECURSE(Visit(stmt->expression()));
CHECK_ALIVE(Visit(stmt->expression()));
} }
void AstTyper::VisitEmptyStatement(EmptyStatement* stmt) { void AstTyper::VisitEmptyStatement(EmptyStatement* stmt) {
ASSERT(!HasStackOverflow());
} }
void AstTyper::VisitIfStatement(IfStatement* stmt) { void AstTyper::VisitIfStatement(IfStatement* stmt) {
ASSERT(!HasStackOverflow()); RECURSE(Visit(stmt->condition()));
CHECK_ALIVE(Visit(stmt->condition())); RECURSE(Visit(stmt->then_statement()));
CHECK_ALIVE(Visit(stmt->then_statement())); RECURSE(Visit(stmt->else_statement()));
CHECK_ALIVE(Visit(stmt->else_statement()));
if (!stmt->condition()->ToBooleanIsTrue() && if (!stmt->condition()->ToBooleanIsTrue() &&
!stmt->condition()->ToBooleanIsFalse()) { !stmt->condition()->ToBooleanIsFalse()) {
@ -114,18 +110,15 @@ void AstTyper::VisitIfStatement(IfStatement* stmt) {
void AstTyper::VisitContinueStatement(ContinueStatement* stmt) { void AstTyper::VisitContinueStatement(ContinueStatement* stmt) {
ASSERT(!HasStackOverflow());
} }
void AstTyper::VisitBreakStatement(BreakStatement* stmt) { void AstTyper::VisitBreakStatement(BreakStatement* stmt) {
ASSERT(!HasStackOverflow());
} }
void AstTyper::VisitReturnStatement(ReturnStatement* stmt) { void AstTyper::VisitReturnStatement(ReturnStatement* stmt) {
ASSERT(!HasStackOverflow()); RECURSE(Visit(stmt->expression()));
CHECK_ALIVE(Visit(stmt->expression()));
// TODO(rossberg): we only need this for inlining into test contexts... // TODO(rossberg): we only need this for inlining into test contexts...
stmt->expression()->RecordToBooleanTypeFeedback(oracle()); stmt->expression()->RecordToBooleanTypeFeedback(oracle());
@ -133,22 +126,20 @@ void AstTyper::VisitReturnStatement(ReturnStatement* stmt) {
void AstTyper::VisitWithStatement(WithStatement* stmt) { void AstTyper::VisitWithStatement(WithStatement* stmt) {
ASSERT(!HasStackOverflow()); RECURSE(stmt->expression());
CHECK_ALIVE(stmt->expression()); RECURSE(stmt->statement());
CHECK_ALIVE(stmt->statement());
} }
void AstTyper::VisitSwitchStatement(SwitchStatement* stmt) { void AstTyper::VisitSwitchStatement(SwitchStatement* stmt) {
ASSERT(!HasStackOverflow()); RECURSE(Visit(stmt->tag()));
CHECK_ALIVE(Visit(stmt->tag()));
ZoneList<CaseClause*>* clauses = stmt->cases(); ZoneList<CaseClause*>* clauses = stmt->cases();
SwitchStatement::SwitchType switch_type = stmt->switch_type(); SwitchStatement::SwitchType switch_type = stmt->switch_type();
for (int i = 0; i < clauses->length(); ++i) { for (int i = 0; i < clauses->length(); ++i) {
CaseClause* clause = clauses->at(i); CaseClause* clause = clauses->at(i);
if (!clause->is_default()) { if (!clause->is_default()) {
Expression* label = clause->label(); Expression* label = clause->label();
CHECK_ALIVE(Visit(label)); RECURSE(Visit(label));
SwitchStatement::SwitchType label_switch_type = SwitchStatement::SwitchType label_switch_type =
label->IsSmiLiteral() ? SwitchStatement::SMI_SWITCH : label->IsSmiLiteral() ? SwitchStatement::SMI_SWITCH :
@ -159,7 +150,7 @@ void AstTyper::VisitSwitchStatement(SwitchStatement* stmt) {
else if (switch_type != label_switch_type) else if (switch_type != label_switch_type)
switch_type = SwitchStatement::GENERIC_SWITCH; switch_type = SwitchStatement::GENERIC_SWITCH;
} }
CHECK_ALIVE(VisitStatements(clause->statements())); RECURSE(VisitStatements(clause->statements()));
} }
if (switch_type == SwitchStatement::UNKNOWN_SWITCH) if (switch_type == SwitchStatement::UNKNOWN_SWITCH)
switch_type = SwitchStatement::GENERIC_SWITCH; switch_type = SwitchStatement::GENERIC_SWITCH;
@ -177,9 +168,8 @@ void AstTyper::VisitSwitchStatement(SwitchStatement* stmt) {
void AstTyper::VisitDoWhileStatement(DoWhileStatement* stmt) { void AstTyper::VisitDoWhileStatement(DoWhileStatement* stmt) {
ASSERT(!HasStackOverflow()); RECURSE(Visit(stmt->body()));
CHECK_ALIVE(Visit(stmt->body())); RECURSE(Visit(stmt->cond()));
CHECK_ALIVE(Visit(stmt->cond()));
if (!stmt->cond()->ToBooleanIsTrue()) { if (!stmt->cond()->ToBooleanIsTrue()) {
stmt->cond()->RecordToBooleanTypeFeedback(oracle()); stmt->cond()->RecordToBooleanTypeFeedback(oracle());
@ -188,9 +178,8 @@ void AstTyper::VisitDoWhileStatement(DoWhileStatement* stmt) {
void AstTyper::VisitWhileStatement(WhileStatement* stmt) { void AstTyper::VisitWhileStatement(WhileStatement* stmt) {
ASSERT(!HasStackOverflow()); RECURSE(Visit(stmt->cond()));
CHECK_ALIVE(Visit(stmt->cond())); RECURSE(Visit(stmt->body()));
CHECK_ALIVE(Visit(stmt->body()));
if (!stmt->cond()->ToBooleanIsTrue()) { if (!stmt->cond()->ToBooleanIsTrue()) {
stmt->cond()->RecordToBooleanTypeFeedback(oracle()); stmt->cond()->RecordToBooleanTypeFeedback(oracle());
@ -199,171 +188,185 @@ void AstTyper::VisitWhileStatement(WhileStatement* stmt) {
void AstTyper::VisitForStatement(ForStatement* stmt) { void AstTyper::VisitForStatement(ForStatement* stmt) {
ASSERT(!HasStackOverflow());
if (stmt->init() != NULL) { if (stmt->init() != NULL) {
CHECK_ALIVE(Visit(stmt->init())); RECURSE(Visit(stmt->init()));
} }
if (stmt->cond() != NULL) { if (stmt->cond() != NULL) {
CHECK_ALIVE(Visit(stmt->cond())); RECURSE(Visit(stmt->cond()));
stmt->cond()->RecordToBooleanTypeFeedback(oracle()); stmt->cond()->RecordToBooleanTypeFeedback(oracle());
} }
CHECK_ALIVE(Visit(stmt->body())); RECURSE(Visit(stmt->body()));
if (stmt->next() != NULL) { if (stmt->next() != NULL) {
CHECK_ALIVE(Visit(stmt->next())); RECURSE(Visit(stmt->next()));
} }
} }
void AstTyper::VisitForInStatement(ForInStatement* stmt) { void AstTyper::VisitForInStatement(ForInStatement* stmt) {
ASSERT(!HasStackOverflow()); RECURSE(Visit(stmt->enumerable()));
CHECK_ALIVE(Visit(stmt->enumerable())); RECURSE(Visit(stmt->body()));
CHECK_ALIVE(Visit(stmt->body()));
stmt->RecordTypeFeedback(oracle()); stmt->RecordTypeFeedback(oracle());
} }
void AstTyper::VisitForOfStatement(ForOfStatement* stmt) { void AstTyper::VisitForOfStatement(ForOfStatement* stmt) {
ASSERT(!HasStackOverflow()); RECURSE(Visit(stmt->iterable()));
CHECK_ALIVE(Visit(stmt->iterable())); RECURSE(Visit(stmt->body()));
CHECK_ALIVE(Visit(stmt->body()));
} }
void AstTyper::VisitTryCatchStatement(TryCatchStatement* stmt) { void AstTyper::VisitTryCatchStatement(TryCatchStatement* stmt) {
ASSERT(!HasStackOverflow()); RECURSE(Visit(stmt->try_block()));
CHECK_ALIVE(Visit(stmt->try_block())); RECURSE(Visit(stmt->catch_block()));
CHECK_ALIVE(Visit(stmt->catch_block()));
} }
void AstTyper::VisitTryFinallyStatement(TryFinallyStatement* stmt) { void AstTyper::VisitTryFinallyStatement(TryFinallyStatement* stmt) {
ASSERT(!HasStackOverflow()); RECURSE(Visit(stmt->try_block()));
CHECK_ALIVE(Visit(stmt->try_block())); RECURSE(Visit(stmt->finally_block()));
CHECK_ALIVE(Visit(stmt->finally_block()));
} }
void AstTyper::VisitDebuggerStatement(DebuggerStatement* stmt) { void AstTyper::VisitDebuggerStatement(DebuggerStatement* stmt) {
ASSERT(!HasStackOverflow());
} }
void AstTyper::VisitFunctionLiteral(FunctionLiteral* expr) { void AstTyper::VisitFunctionLiteral(FunctionLiteral* expr) {
ASSERT(!HasStackOverflow());
} }
void AstTyper::VisitSharedFunctionInfoLiteral(SharedFunctionInfoLiteral* expr) { void AstTyper::VisitSharedFunctionInfoLiteral(SharedFunctionInfoLiteral* expr) {
ASSERT(!HasStackOverflow());
} }
void AstTyper::VisitConditional(Conditional* expr) { void AstTyper::VisitConditional(Conditional* expr) {
ASSERT(!HasStackOverflow()); RECURSE(Visit(expr->condition()));
CHECK_ALIVE(Visit(expr->condition())); RECURSE(Visit(expr->then_expression()));
CHECK_ALIVE(Visit(expr->then_expression())); RECURSE(Visit(expr->else_expression()));
CHECK_ALIVE(Visit(expr->else_expression()));
expr->condition()->RecordToBooleanTypeFeedback(oracle()); expr->condition()->RecordToBooleanTypeFeedback(oracle());
MergeLowerType(expr, Type::Intersect(
expr->then_expression()->lower_type(),
expr->else_expression()->lower_type()));
MergeUpperType(expr, Type::Union(
expr->then_expression()->upper_type(),
expr->else_expression()->upper_type()));
} }
void AstTyper::VisitVariableProxy(VariableProxy* expr) { void AstTyper::VisitVariableProxy(VariableProxy* expr) {
ASSERT(!HasStackOverflow()); // TODO(rossberg): typing of variables
} }
void AstTyper::VisitLiteral(Literal* expr) { void AstTyper::VisitLiteral(Literal* expr) {
ASSERT(!HasStackOverflow()); Type* type = Type::Constant(expr->value(), isolate_);
MergeLowerType(expr, type);
MergeUpperType(expr, type);
} }
void AstTyper::VisitRegExpLiteral(RegExpLiteral* expr) { void AstTyper::VisitRegExpLiteral(RegExpLiteral* expr) {
ASSERT(!HasStackOverflow()); MergeLowerType(expr, Type::RegExp());
MergeUpperType(expr, Type::RegExp());
} }
void AstTyper::VisitObjectLiteral(ObjectLiteral* expr) { void AstTyper::VisitObjectLiteral(ObjectLiteral* expr) {
ASSERT(!HasStackOverflow());
ZoneList<ObjectLiteral::Property*>* properties = expr->properties(); ZoneList<ObjectLiteral::Property*>* properties = expr->properties();
for (int i = 0; i < properties->length(); ++i) { for (int i = 0; i < properties->length(); ++i) {
ObjectLiteral::Property* prop = properties->at(i); ObjectLiteral::Property* prop = properties->at(i);
CHECK_ALIVE(Visit(prop->value())); RECURSE(Visit(prop->value()));
if ((prop->kind() == ObjectLiteral::Property::MATERIALIZED_LITERAL && if ((prop->kind() == ObjectLiteral::Property::MATERIALIZED_LITERAL &&
!CompileTimeValue::IsCompileTimeValue(prop->value())) || !CompileTimeValue::IsCompileTimeValue(prop->value())) ||
prop->kind() == ObjectLiteral::Property::COMPUTED) { prop->kind() == ObjectLiteral::Property::COMPUTED) {
if (prop->key()->value()->IsInternalizedString() && prop->emit_store()) if (prop->key()->value()->IsInternalizedString() && prop->emit_store()) {
prop->RecordTypeFeedback(oracle()); prop->RecordTypeFeedback(oracle());
}
} }
} }
MergeLowerType(expr, Type::Object());
MergeUpperType(expr, Type::Object());
} }
void AstTyper::VisitArrayLiteral(ArrayLiteral* expr) { void AstTyper::VisitArrayLiteral(ArrayLiteral* expr) {
ASSERT(!HasStackOverflow());
ZoneList<Expression*>* values = expr->values(); ZoneList<Expression*>* values = expr->values();
for (int i = 0; i < values->length(); ++i) { for (int i = 0; i < values->length(); ++i) {
Expression* value = values->at(i); Expression* value = values->at(i);
CHECK_ALIVE(Visit(value)); RECURSE(Visit(value));
} }
MergeLowerType(expr, Type::Array());
MergeUpperType(expr, Type::Array());
} }
void AstTyper::VisitAssignment(Assignment* expr) { void AstTyper::VisitAssignment(Assignment* expr) {
ASSERT(!HasStackOverflow());
CHECK_ALIVE(Visit(expr->target()));
CHECK_ALIVE(Visit(expr->value()));
// TODO(rossberg): Can we clean this up? // TODO(rossberg): Can we clean this up?
if (expr->is_compound()) { if (expr->is_compound()) {
CHECK_ALIVE(Visit(expr->binary_operation())); RECURSE(Visit(expr->binary_operation()));
Expression* target = expr->target(); Expression* target = expr->target();
Property* prop = target->AsProperty(); Property* prop = target->AsProperty();
if (prop != NULL) { if (prop != NULL) {
prop->RecordTypeFeedback(oracle(), zone()); prop->RecordTypeFeedback(oracle(), zone());
if (!prop->key()->IsPropertyName()) // i.e., keyed if (!prop->key()->IsPropertyName()) { // i.e., keyed
expr->RecordTypeFeedback(oracle(), zone()); expr->RecordTypeFeedback(oracle(), zone());
}
} }
return; } else {
RECURSE(Visit(expr->target()));
RECURSE(Visit(expr->value()));
if (expr->target()->AsProperty()) {
expr->RecordTypeFeedback(oracle(), zone());
}
MergeLowerType(expr, expr->value()->lower_type());
MergeUpperType(expr, expr->value()->upper_type());
} }
if (expr->target()->AsProperty()) // TODO(rossberg): handle target variables
expr->RecordTypeFeedback(oracle(), zone());
} }
void AstTyper::VisitYield(Yield* expr) { void AstTyper::VisitYield(Yield* expr) {
ASSERT(!HasStackOverflow()); RECURSE(Visit(expr->generator_object()));
CHECK_ALIVE(Visit(expr->generator_object())); RECURSE(Visit(expr->expression()));
CHECK_ALIVE(Visit(expr->expression()));
// We don't know anything about the type.
} }
void AstTyper::VisitThrow(Throw* expr) { void AstTyper::VisitThrow(Throw* expr) {
ASSERT(!HasStackOverflow()); RECURSE(Visit(expr->exception()));
CHECK_ALIVE(Visit(expr->exception()));
// Lower type is None already.
MergeUpperType(expr, Type::None());
} }
void AstTyper::VisitProperty(Property* expr) { void AstTyper::VisitProperty(Property* expr) {
ASSERT(!HasStackOverflow()); RECURSE(Visit(expr->obj()));
CHECK_ALIVE(Visit(expr->obj())); RECURSE(Visit(expr->key()));
CHECK_ALIVE(Visit(expr->key()));
expr->RecordTypeFeedback(oracle(), zone()); expr->RecordTypeFeedback(oracle(), zone());
// We don't know anything about the type.
} }
void AstTyper::VisitCall(Call* expr) { void AstTyper::VisitCall(Call* expr) {
ASSERT(!HasStackOverflow()); RECURSE(Visit(expr->expression()));
CHECK_ALIVE(Visit(expr->expression()));
ZoneList<Expression*>* args = expr->arguments(); ZoneList<Expression*>* args = expr->arguments();
for (int i = 0; i < args->length(); ++i) { for (int i = 0; i < args->length(); ++i) {
Expression* arg = args->at(i); Expression* arg = args->at(i);
CHECK_ALIVE(Visit(arg)); RECURSE(Visit(arg));
} }
Expression* callee = expr->expression(); Expression* callee = expr->expression();
@ -374,35 +377,38 @@ void AstTyper::VisitCall(Call* expr) {
} else { } else {
expr->RecordTypeFeedback(oracle(), CALL_AS_FUNCTION); expr->RecordTypeFeedback(oracle(), CALL_AS_FUNCTION);
} }
// We don't know anything about the type.
} }
void AstTyper::VisitCallNew(CallNew* expr) { void AstTyper::VisitCallNew(CallNew* expr) {
ASSERT(!HasStackOverflow()); RECURSE(Visit(expr->expression()));
CHECK_ALIVE(Visit(expr->expression()));
ZoneList<Expression*>* args = expr->arguments(); ZoneList<Expression*>* args = expr->arguments();
for (int i = 0; i < args->length(); ++i) { for (int i = 0; i < args->length(); ++i) {
Expression* arg = args->at(i); Expression* arg = args->at(i);
CHECK_ALIVE(Visit(arg)); RECURSE(Visit(arg));
} }
expr->RecordTypeFeedback(oracle()); expr->RecordTypeFeedback(oracle());
// We don't know anything about the type.
} }
void AstTyper::VisitCallRuntime(CallRuntime* expr) { void AstTyper::VisitCallRuntime(CallRuntime* expr) {
ASSERT(!HasStackOverflow());
ZoneList<Expression*>* args = expr->arguments(); ZoneList<Expression*>* args = expr->arguments();
for (int i = 0; i < args->length(); ++i) { for (int i = 0; i < args->length(); ++i) {
Expression* arg = args->at(i); Expression* arg = args->at(i);
CHECK_ALIVE(Visit(arg)); RECURSE(Visit(arg));
} }
// We don't know anything about the type.
} }
void AstTyper::VisitUnaryOperation(UnaryOperation* expr) { void AstTyper::VisitUnaryOperation(UnaryOperation* expr) {
ASSERT(!HasStackOverflow()); RECURSE(Visit(expr->expression()));
CHECK_ALIVE(Visit(expr->expression()));
// Collect type feedback. // Collect type feedback.
Handle<Type> op_type = oracle()->UnaryType(expr->UnaryOperationFeedbackId()); Handle<Type> op_type = oracle()->UnaryType(expr->UnaryOperationFeedbackId());
@ -411,25 +417,55 @@ void AstTyper::VisitUnaryOperation(UnaryOperation* expr) {
// TODO(rossberg): only do in test or value context. // TODO(rossberg): only do in test or value context.
expr->expression()->RecordToBooleanTypeFeedback(oracle()); expr->expression()->RecordToBooleanTypeFeedback(oracle());
} }
switch (expr->op()) {
case Token::NOT:
case Token::DELETE:
MergeLowerType(expr, Type::Boolean());
MergeUpperType(expr, Type::Boolean());
break;
case Token::VOID:
MergeLowerType(expr, Type::Undefined());
MergeUpperType(expr, Type::Undefined());
break;
case Token::ADD:
case Token::SUB: {
MergeLowerType(expr, Type::Smi());
Type* upper = *expr->expression()->upper_type();
MergeUpperType(expr, upper->Is(Type::Number()) ? upper : Type::Number());
break;
}
case Token::BIT_NOT:
MergeLowerType(expr, Type::Smi());
MergeUpperType(expr, Type::Signed32());
break;
case Token::TYPEOF:
MergeLowerType(expr, Type::InternalizedString());
MergeUpperType(expr, Type::InternalizedString());
break;
default:
UNREACHABLE();
}
} }
void AstTyper::VisitCountOperation(CountOperation* expr) { void AstTyper::VisitCountOperation(CountOperation* expr) {
ASSERT(!HasStackOverflow()); RECURSE(Visit(expr->expression()));
CHECK_ALIVE(Visit(expr->expression()));
expr->RecordTypeFeedback(oracle(), zone()); expr->RecordTypeFeedback(oracle(), zone());
Property* prop = expr->expression()->AsProperty(); Property* prop = expr->expression()->AsProperty();
if (prop != NULL) { if (prop != NULL) {
prop->RecordTypeFeedback(oracle(), zone()); prop->RecordTypeFeedback(oracle(), zone());
} }
MergeLowerType(expr, Type::Smi());
MergeUpperType(expr, Type::Number());
} }
void AstTyper::VisitBinaryOperation(BinaryOperation* expr) { void AstTyper::VisitBinaryOperation(BinaryOperation* expr) {
ASSERT(!HasStackOverflow()); RECURSE(Visit(expr->left()));
CHECK_ALIVE(Visit(expr->left())); RECURSE(Visit(expr->right()));
CHECK_ALIVE(Visit(expr->right()));
// Collect type feedback. // Collect type feedback.
Handle<Type> type, left_type, right_type; Handle<Type> type, left_type, right_type;
@ -443,13 +479,69 @@ void AstTyper::VisitBinaryOperation(BinaryOperation* expr) {
if (expr->op() == Token::OR || expr->op() == Token::AND) { if (expr->op() == Token::OR || expr->op() == Token::AND) {
expr->left()->RecordToBooleanTypeFeedback(oracle()); expr->left()->RecordToBooleanTypeFeedback(oracle());
} }
switch (expr->op()) {
case Token::COMMA:
MergeLowerType(expr, expr->right()->lower_type());
MergeUpperType(expr, expr->right()->upper_type());
break;
case Token::OR:
case Token::AND:
MergeLowerType(expr, Type::Intersect(
expr->left()->lower_type(), expr->right()->lower_type()));
MergeUpperType(expr, Type::Union(
expr->left()->upper_type(), expr->right()->upper_type()));
break;
case Token::BIT_OR:
case Token::BIT_AND: {
MergeLowerType(expr, Type::Smi());
Type* upper =
Type::Union(expr->left()->upper_type(), expr->right()->upper_type());
MergeUpperType(expr,
upper->Is(Type::Signed32()) ? upper : Type::Signed32());
break;
}
case Token::BIT_XOR:
case Token::SHL:
case Token::SAR:
MergeLowerType(expr, Type::Smi());
MergeUpperType(expr, Type::Signed32());
break;
case Token::SHR:
MergeLowerType(expr, Type::Smi());
MergeUpperType(expr, Type::Unsigned32());
break;
case Token::ADD: {
Handle<Type> l = expr->left()->lower_type();
Handle<Type> r = expr->right()->lower_type();
MergeLowerType(expr,
l->Is(Type::Number()) && r->Is(Type::Number()) ? Type::Smi() :
l->Is(Type::String()) || r->Is(Type::String()) ? Type::String() :
Type::None());
l = expr->left()->upper_type();
r = expr->right()->upper_type();
MergeUpperType(expr,
l->Is(Type::Number()) && r->Is(Type::Number()) ? Type::Number() :
l->Is(Type::String()) || r->Is(Type::String()) ? Type::String() :
Type::NumberOrString());
break;
}
case Token::SUB:
case Token::MUL:
case Token::DIV:
case Token::MOD:
MergeLowerType(expr, Type::Smi());
MergeUpperType(expr, Type::Number());
break;
default:
UNREACHABLE();
}
} }
void AstTyper::VisitCompareOperation(CompareOperation* expr) { void AstTyper::VisitCompareOperation(CompareOperation* expr) {
ASSERT(!HasStackOverflow()); RECURSE(Visit(expr->left()));
CHECK_ALIVE(Visit(expr->left())); RECURSE(Visit(expr->right()));
CHECK_ALIVE(Visit(expr->right()));
// Collect type feedback. // Collect type feedback.
Handle<Type> left_type, right_type, combined_type; Handle<Type> left_type, right_type, combined_type;
@ -458,76 +550,67 @@ void AstTyper::VisitCompareOperation(CompareOperation* expr) {
MergeLowerType(expr->left(), left_type); MergeLowerType(expr->left(), left_type);
MergeLowerType(expr->right(), right_type); MergeLowerType(expr->right(), right_type);
expr->set_combined_type(combined_type); expr->set_combined_type(combined_type);
MergeLowerType(expr, Type::Boolean());
MergeUpperType(expr, Type::Boolean());
} }
void AstTyper::VisitThisFunction(ThisFunction* expr) { void AstTyper::VisitThisFunction(ThisFunction* expr) {
ASSERT(!HasStackOverflow());
} }
void AstTyper::VisitDeclarations(ZoneList<Declaration*>* decls) { void AstTyper::VisitDeclarations(ZoneList<Declaration*>* decls) {
ASSERT(!HasStackOverflow());
for (int i = 0; i < decls->length(); ++i) { for (int i = 0; i < decls->length(); ++i) {
Declaration* decl = decls->at(i); Declaration* decl = decls->at(i);
CHECK_ALIVE(Visit(decl)); RECURSE(Visit(decl));
} }
} }
void AstTyper::VisitVariableDeclaration(VariableDeclaration* declaration) { void AstTyper::VisitVariableDeclaration(VariableDeclaration* declaration) {
ASSERT(!HasStackOverflow());
} }
void AstTyper::VisitFunctionDeclaration(FunctionDeclaration* declaration) { void AstTyper::VisitFunctionDeclaration(FunctionDeclaration* declaration) {
ASSERT(!HasStackOverflow()); RECURSE(Visit(declaration->fun()));
CHECK_ALIVE(Visit(declaration->fun()));
} }
void AstTyper::VisitModuleDeclaration(ModuleDeclaration* declaration) { void AstTyper::VisitModuleDeclaration(ModuleDeclaration* declaration) {
ASSERT(!HasStackOverflow()); RECURSE(Visit(declaration->module()));
CHECK_ALIVE(Visit(declaration->module()));
} }
void AstTyper::VisitImportDeclaration(ImportDeclaration* declaration) { void AstTyper::VisitImportDeclaration(ImportDeclaration* declaration) {
ASSERT(!HasStackOverflow()); RECURSE(Visit(declaration->module()));
CHECK_ALIVE(Visit(declaration->module()));
} }
void AstTyper::VisitExportDeclaration(ExportDeclaration* declaration) { void AstTyper::VisitExportDeclaration(ExportDeclaration* declaration) {
ASSERT(!HasStackOverflow());
} }
void AstTyper::VisitModuleLiteral(ModuleLiteral* module) { void AstTyper::VisitModuleLiteral(ModuleLiteral* module) {
ASSERT(!HasStackOverflow()); RECURSE(Visit(module->body()));
CHECK_ALIVE(Visit(module->body()));
} }
void AstTyper::VisitModuleVariable(ModuleVariable* module) { void AstTyper::VisitModuleVariable(ModuleVariable* module) {
ASSERT(!HasStackOverflow());
} }
void AstTyper::VisitModulePath(ModulePath* module) { void AstTyper::VisitModulePath(ModulePath* module) {
ASSERT(!HasStackOverflow()); RECURSE(Visit(module->module()));
CHECK_ALIVE(Visit(module->module()));
} }
void AstTyper::VisitModuleUrl(ModuleUrl* module) { void AstTyper::VisitModuleUrl(ModuleUrl* module) {
ASSERT(!HasStackOverflow());
} }
void AstTyper::VisitModuleStatement(ModuleStatement* stmt) { void AstTyper::VisitModuleStatement(ModuleStatement* stmt) {
ASSERT(!HasStackOverflow()); RECURSE(Visit(stmt->body()));
CHECK_ALIVE(Visit(stmt->body()));
} }

View File

@ -68,6 +68,12 @@ class AstTyper: public AstVisitor {
void MergeUpperType(Expression* e, Handle<Type> t) { void MergeUpperType(Expression* e, Handle<Type> t) {
e->set_upper_type(handle(Type::Intersect(e->upper_type(), t), isolate_)); e->set_upper_type(handle(Type::Intersect(e->upper_type(), t), isolate_));
} }
void MergeLowerType(Expression* e, Type* t) {
MergeLowerType(e, handle(t, isolate_));
}
void MergeUpperType(Expression* e, Type* t) {
MergeUpperType(e, handle(t, isolate_));
}
void VisitDeclarations(ZoneList<Declaration*>* declarations); void VisitDeclarations(ZoneList<Declaration*>* declarations);
void VisitStatements(ZoneList<Statement*>* statements); void VisitStatements(ZoneList<Statement*>* statements);