Index: src/ast.cc |
=================================================================== |
--- src/ast.cc (revision 4271) |
+++ src/ast.cc (working copy) |
@@ -172,6 +172,72 @@ |
} |
+bool Expression::GuaranteedSmiResult() { |
+ BinaryOperation* node = AsBinaryOperation(); |
+ if (node == NULL) return false; |
+ Token::Value op = node->op(); |
+ switch (op) { |
+ case Token::COMMA: |
+ case Token::OR: |
+ case Token::AND: |
+ case Token::ADD: |
+ case Token::SUB: |
+ case Token::MUL: |
+ case Token::DIV: |
+ case Token::MOD: |
+ case Token::BIT_XOR: |
+ case Token::SHL: |
+ return false; |
+ break; |
+ case Token::BIT_OR: |
+ case Token::BIT_AND: { |
+ Literal* left = node->left()->AsLiteral(); |
+ Literal* right = node->right()->AsLiteral(); |
+ if (left != NULL && left->handle()->IsSmi()) { |
+ int value = Smi::cast(*left->handle())->value(); |
+ if (op == Token::BIT_OR && ((value & 0xc0000000) == 0xc0000000)) { |
+ // Result of bitwise or is always a negative Smi. |
+ return true; |
+ } |
+ if (op == Token::BIT_AND && ((value & 0xc0000000) == 0)) { |
+ // Result of bitwise and is always a positive Smi. |
+ return true; |
+ } |
+ } |
+ if (right != NULL && right->handle()->IsSmi()) { |
+ int value = Smi::cast(*right->handle())->value(); |
+ if (op == Token::BIT_OR && ((value & 0xc0000000) == 0xc0000000)) { |
+ // Result of bitwise or is always a negative Smi. |
+ return true; |
+ } |
+ if (op == Token::BIT_AND && ((value & 0xc0000000) == 0)) { |
+ // Result of bitwise and is always a positive Smi. |
+ return true; |
+ } |
+ } |
+ return false; |
+ break; |
+ } |
+ case Token::SAR: |
+ case Token::SHR: { |
+ Literal* right = node->right()->AsLiteral(); |
+ if (right != NULL && right->handle()->IsSmi()) { |
+ int value = Smi::cast(*right->handle())->value(); |
+ if ((value & 0x1F) > 1 || |
+ (op == Token::SAR && (value & 0x1F) == 1)) { |
+ return true; |
+ } |
+ } |
+ return false; |
+ break; |
+ } |
+ default: |
+ UNREACHABLE(); |
+ break; |
+ } |
+ return false; |
+} |
+ |
// ---------------------------------------------------------------------------- |
// Implementation of AstVisitor |