Added test for + - * operations in int32 arithmetic, updated JS generator to produce code for correct int32 arithmetic. arithmetic
authorMartin Soch <Martin.Soch@oracle.com>
Tue, 18 Dec 2012 20:19:11 +0100
brancharithmetic
changeset 351b7459b10d581
parent 345 f954edc48431
child 352 c0fd4e7919b3
Added test for + - * operations in int32 arithmetic, updated JS generator to produce code for correct int32 arithmetic.
vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeToJavaScript.java
vm/src/test/java/org/apidesign/vm4brwsr/tck/IntegerArithmeticTest.java
     1.1 --- a/vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeToJavaScript.java	Tue Dec 18 11:41:09 2012 +0100
     1.2 +++ b/vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeToJavaScript.java	Tue Dec 18 20:19:11 2012 +0100
     1.3 @@ -453,7 +453,7 @@
     1.4                      emit(out, "@1 = @2;", lmapper.setD(3), smapper.popD());
     1.5                      break;
     1.6                  case opc_iadd:
     1.7 -                    emit(out, "@1 += @2;", smapper.getI(1), smapper.popI());
     1.8 +                    emit(out, "@1 = (@1 + @2) | 0;", smapper.getI(1), smapper.popI());
     1.9                      break;
    1.10                  case opc_ladd:
    1.11                      emit(out, "@1 += @2;", smapper.getL(1), smapper.popL());
    1.12 @@ -465,7 +465,7 @@
    1.13                      emit(out, "@1 += @2;", smapper.getD(1), smapper.popD());
    1.14                      break;
    1.15                  case opc_isub:
    1.16 -                    emit(out, "@1 -= @2;", smapper.getI(1), smapper.popI());
    1.17 +                    emit(out, "@1 = (@1 - @2) | 0;", smapper.getI(1), smapper.popI());
    1.18                      break;
    1.19                  case opc_lsub:
    1.20                      emit(out, "@1 -= @2;", smapper.getL(1), smapper.popL());
    1.21 @@ -477,7 +477,7 @@
    1.22                      emit(out, "@1 -= @2;", smapper.getD(1), smapper.popD());
    1.23                      break;
    1.24                  case opc_imul:
    1.25 -                    emit(out, "@1 *= @2;", smapper.getI(1), smapper.popI());
    1.26 +                    emit(out, "@1 = (((@1 * (@2 >> 16)) << 16) + @1 * (@2 & 0xFFFF)) | 0;", smapper.getI(1), smapper.popI());
    1.27                      break;
    1.28                  case opc_lmul:
    1.29                      emit(out, "@1 *= @2;", smapper.getL(1), smapper.popL());
     2.1 --- a/vm/src/test/java/org/apidesign/vm4brwsr/tck/IntegerArithmeticTest.java	Tue Dec 18 11:41:09 2012 +0100
     2.2 +++ b/vm/src/test/java/org/apidesign/vm4brwsr/tck/IntegerArithmeticTest.java	Tue Dec 18 20:19:11 2012 +0100
     2.3 @@ -17,7 +17,6 @@
     2.4   */
     2.5  package org.apidesign.vm4brwsr.tck;
     2.6  
     2.7 -import org.apidesign.bck2brwsr.core.JavaScriptBody;
     2.8  import org.apidesign.vm4brwsr.Compare;
     2.9  import org.apidesign.vm4brwsr.CompareVMs;
    2.10  import org.testng.annotations.Factory;
    2.11 @@ -27,30 +26,53 @@
    2.12   * @author Jaroslav Tulach <jtulach@netbeans.org>
    2.13   */
    2.14  public class IntegerArithmeticTest {
    2.15 -    //@JavaScriptBody(args="msg", body="java.lang.System.out.println(msg.toString());")
    2.16 -    //private static native void log(String msg);
    2.17      
    2.18 -    @Compare public int overflow() {
    2.19 -        int v = Integer.MAX_VALUE + 1;
    2.20 -        return v;
    2.21 +    private static int add(int x, int y) {
    2.22 +        return x + y;
    2.23      }
    2.24      
    2.25 -    @Compare public int underflow() {
    2.26 -        int v = Integer.MIN_VALUE - 1;
    2.27 -        return v;
    2.28 +    private static int sub(int x, int y) {
    2.29 +        return x - y;
    2.30      }
    2.31      
    2.32 -    /* @Compare public int convertToInt() {
    2.33 -        long v = Long.MAX_VALUE / 2;
    2.34 -        log("convert: " + v);
    2.35 -        return (int) v;
    2.36 -    } */
    2.37 -    @Compare public int addAndMaxInt() {
    2.38 -        return Integer.MAX_VALUE + Integer.MAX_VALUE;
    2.39 +    private static int mul(int x, int y) {
    2.40 +        return x * y;
    2.41 +    }
    2.42 +    
    2.43 +    @Compare public int addOverflow() {
    2.44 +        return add(Integer.MAX_VALUE, 1);
    2.45 +    }
    2.46 +    
    2.47 +    @Compare public int subUnderflow() {
    2.48 +        return sub(Integer.MIN_VALUE, 1);
    2.49 +    }
    2.50 +    
    2.51 +    @Compare public int addMaxIntAndMaxInt() {
    2.52 +        return add(Integer.MAX_VALUE, Integer.MAX_VALUE);
    2.53 +    }
    2.54 +    
    2.55 +    @Compare public int subMinIntAndMinInt() {
    2.56 +        return sub(Integer.MIN_VALUE, Integer.MIN_VALUE);
    2.57      }
    2.58      
    2.59      @Compare public int multiplyMaxInt() {
    2.60 -        return Integer.MAX_VALUE * Integer.MAX_VALUE;
    2.61 +        return mul(Integer.MAX_VALUE, 2);
    2.62 +    }
    2.63 +    
    2.64 +    @Compare public int multiplyMaxIntAndMaxInt() {
    2.65 +        return mul(Integer.MAX_VALUE, Integer.MAX_VALUE);
    2.66 +    }
    2.67 +    
    2.68 +    @Compare public int multiplyMinInt() {
    2.69 +        return mul(Integer.MIN_VALUE, 2);
    2.70 +    }
    2.71 +    
    2.72 +    @Compare public int multiplyMinIntAndMinInt() {
    2.73 +        return mul(Integer.MIN_VALUE, Integer.MIN_VALUE);
    2.74 +    }
    2.75 +    
    2.76 +    @Compare public int multiplyPrecision() {
    2.77 +        return mul(119106029, 1103515245);
    2.78      }
    2.79      
    2.80      @Factory