jitcraft/BytecodeCompiler.cpp
changeset 74 80e29e23eb4e
parent 73 ef5492aa8d8b
child 75 662e81d9ed1c
     1.1 --- a/jitcraft/BytecodeCompiler.cpp	Sun Jul 15 14:40:11 2012 -0700
     1.2 +++ b/jitcraft/BytecodeCompiler.cpp	Sun Jul 15 19:48:15 2012 -0700
     1.3 @@ -95,6 +95,18 @@
     1.4          return OP_BITAND;
     1.5        case TOK_BITOR:
     1.6          return OP_BITOR;
     1.7 +      case TOK_GT:
     1.8 +          return OP_GT;
     1.9 +      case TOK_GE:
    1.10 +          return OP_GE;
    1.11 +      case TOK_LT:
    1.12 +          return OP_LT;
    1.13 +      case TOK_LE:
    1.14 +          return OP_LE;
    1.15 +      case TOK_EQUALS:
    1.16 +          return OP_EQ;
    1.17 +      case TOK_NOTEQUALS:
    1.18 +          return OP_NE;
    1.19        default:
    1.20          assert(kind == TOK_BITXOR);
    1.21          return OP_BITXOR;
    1.22 @@ -380,11 +392,23 @@
    1.23          assert(needed->primitive() == node->type()->primitive());
    1.24  }
    1.25  
    1.26 +static inline Type *
    1.27 +GetCoercionType(TokenKind tok, Type *left, Type *right)
    1.28 +{
    1.29 +    if (left->isFloat())
    1.30 +        return left;
    1.31 +    return right;
    1.32 +}
    1.33 +
    1.34  void
    1.35  BytecodeCompiler::visit(BinaryExpression *node)
    1.36  {
    1.37 -    visitForALU(node->type(), node->left());
    1.38 -    visitForALU(node->type(), node->right());
    1.39 +    Local<Type> leftType(zone_, node->left()->type());
    1.40 +    Local<Type> rightType(zone_, node->right()->type());
    1.41 +    Local<Type> coerce(zone_, GetCoercionType(node->token(), leftType, rightType));
    1.42 +
    1.43 +    visitForALU(coerce, node->left());
    1.44 +    visitForALU(coerce, node->right());
    1.45  
    1.46      Opcode op = TokenToOpcode(node->token());
    1.47      if (node->type()->isFloat())
    1.48 @@ -425,10 +449,12 @@
    1.49      TokenKind token = ToCompare(expression);
    1.50      if (token == TOK_EOF) {
    1.51          expression->accept(this);
    1.52 -        if (onTrue)
    1.53 -            __ j(OP_JT, label);
    1.54 -        else
    1.55 -            __ j(OP_JF, label);
    1.56 +
    1.57 +        Opcode op = (onTrue) ? OP_JT : OP_JF;
    1.58 +        if (expression->type()->isFloat())
    1.59 +            op = (Opcode)(int(op) + (OP_JL_F - OP_JL));
    1.60 +
    1.61 +        __ j(op, label);
    1.62          return;
    1.63      }
    1.64  
    1.65 @@ -438,8 +464,15 @@
    1.66  
    1.67      assert(expression->isBinaryExpression());
    1.68      BinaryExpression *binary = expression->toBinaryExpression();
    1.69 -    binary->left()->accept(this);
    1.70 -    binary->right()->accept(this);
    1.71 +    Local<Type> leftType(zone_, binary->left()->type());
    1.72 +    Local<Type> rightType(zone_, binary->right()->type());
    1.73 +    Local<Type> coerce(zone_, GetCoercionType(binary->token(), leftType, rightType));
    1.74 +
    1.75 +    visitForALU(coerce, binary->left());
    1.76 +    visitForALU(coerce, binary->right());
    1.77 +    if (coerce->isFloat())
    1.78 +        op = (Opcode)(int(op) + (OP_JL_F - OP_JL));
    1.79 +
    1.80      __ j(op, label);
    1.81  }
    1.82