Bringing Martin's integer, short, byte arithmetic to default branch
authorJaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 15 Jan 2013 11:53:07 +0100
changeset 456f2f769bafeef
parent 455 02efb6bda7de
parent 454 6506c5925775
child 457 b0e82dcf51fb
Bringing Martin's integer, short, byte arithmetic to default branch
emul/src/main/resources/org/apidesign/vm4brwsr/emul/java_lang_String.js
vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeToJavaScript.java
vmtest/src/test/java/org/apidesign/bck2brwsr/tck/CompareStringsTest.java
     1.1 --- a/emul/src/main/java/java/lang/Number.java	Tue Jan 15 11:44:23 2013 +0100
     1.2 +++ b/emul/src/main/java/java/lang/Number.java	Tue Jan 15 11:53:07 2013 +0100
     1.3 @@ -25,6 +25,8 @@
     1.4  
     1.5  package java.lang;
     1.6  
     1.7 +import org.apidesign.bck2brwsr.core.ExtraJavaScript;
     1.8 +
     1.9  /**
    1.10   * The abstract class <code>Number</code> is the superclass of classes
    1.11   * <code>BigDecimal</code>, <code>BigInteger</code>,
    1.12 @@ -46,6 +48,10 @@
    1.13   * @see     java.lang.Short
    1.14   * @since   JDK1.0
    1.15   */
    1.16 +@ExtraJavaScript(
    1.17 +    resource="/org/apidesign/vm4brwsr/emul/java_lang_Number.js",
    1.18 +    processByteCode=true
    1.19 +)
    1.20  public abstract class Number implements java.io.Serializable {
    1.21      /**
    1.22       * Returns the value of the specified number as an <code>int</code>.
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/emul/src/main/resources/org/apidesign/vm4brwsr/emul/java_lang_Number.js	Tue Jan 15 11:53:07 2013 +0100
     2.3 @@ -0,0 +1,9 @@
     2.4 +// empty line needed here
     2.5 +Number.prototype.add32 = function(x) { return (this + x) | 0; };
     2.6 +Number.prototype.sub32 = function(x) { return (this - x) | 0; };
     2.7 +Number.prototype.mul32 = function(x) { 
     2.8 +    return (((this * (x >> 16)) << 16) + this * (x & 0xFFFF)) | 0;
     2.9 +};
    2.10 +
    2.11 +Number.prototype.toInt8 = function()  { return (this << 24) >> 24; };
    2.12 +Number.prototype.toInt16 = function() { return (this << 16) >> 16; };
    2.13 \ No newline at end of file
     3.1 --- a/emul/src/main/resources/org/apidesign/vm4brwsr/emul/java_lang_String.js	Tue Jan 15 11:44:23 2013 +0100
     3.2 +++ b/emul/src/main/resources/org/apidesign/vm4brwsr/emul/java_lang_String.js	Tue Jan 15 11:53:07 2013 +0100
     3.3 @@ -2,11 +2,8 @@
     3.4  vm.java_lang_String(false);
     3.5  
     3.6  // we need initialized arrays
     3.7 -Array.prototype.fillNulls = function() {
     3.8 -  for(var i = 0; i < this.length; i++) this[i] = null;
     3.9 -  return this;
    3.10 -};
    3.11 -Array.prototype.arrtype = function(sig) {
    3.12 +Array.prototype.initWith = function(sig, value) {
    3.13 +  for(var i = 0; i < this.length; i++) this[i] = value;
    3.14    this.jvmName = sig;
    3.15    return this;
    3.16  };
     4.1 --- a/vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeToJavaScript.java	Tue Jan 15 11:44:23 2013 +0100
     4.2 +++ b/vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeToJavaScript.java	Tue Jan 15 11:53:07 2013 +0100
     4.3 @@ -494,7 +494,7 @@
     4.4                      emit(out, "@1 = @2;", lmapper.setD(3), smapper.popD());
     4.5                      break;
     4.6                  case opc_iadd:
     4.7 -                    emit(out, "@1 = (@1 + @2) | 0;", smapper.getI(1), smapper.popI());
     4.8 +                    emit(out, "@1 = @1.add32(@2);", smapper.getI(1), smapper.popI());
     4.9                      break;
    4.10                  case opc_ladd:
    4.11                      emit(out, "@1 += @2;", smapper.getL(1), smapper.popL());
    4.12 @@ -506,7 +506,7 @@
    4.13                      emit(out, "@1 += @2;", smapper.getD(1), smapper.popD());
    4.14                      break;
    4.15                  case opc_isub:
    4.16 -                    emit(out, "@1 = (@1 - @2) | 0;", smapper.getI(1), smapper.popI());
    4.17 +                    emit(out, "@1 = @1.sub32(@2);", smapper.getI(1), smapper.popI());
    4.18                      break;
    4.19                  case opc_lsub:
    4.20                      emit(out, "@1 -= @2;", smapper.getL(1), smapper.popL());
    4.21 @@ -518,7 +518,7 @@
    4.22                      emit(out, "@1 -= @2;", smapper.getD(1), smapper.popD());
    4.23                      break;
    4.24                  case opc_imul:
    4.25 -                    emit(out, "@1 = (((@1 * (@2 >> 16)) << 16) + @1 * (@2 & 0xFFFF)) | 0;", smapper.getI(1), smapper.popI());
    4.26 +                    emit(out, "@1 = @1.mul32(@2);", smapper.getI(1), smapper.popI());
    4.27                      break;
    4.28                  case opc_lmul:
    4.29                      emit(out, "@1 *= @2;", smapper.getL(1), smapper.popL());
    4.30 @@ -675,9 +675,13 @@
    4.31                           smapper.popD(), smapper.pushL());
    4.32                      break;
    4.33                  case opc_i2b:
    4.34 +                    emit(out, "@1 = @1.toInt8();", smapper.getI(0));
    4.35 +                    break;
    4.36                  case opc_i2c:
    4.37 +                    out.append("{ /* number conversion */ }");
    4.38 +                    break;
    4.39                  case opc_i2s:
    4.40 -                    out.append("{ /* number conversion */ }");
    4.41 +                    emit(out, "@1 = @1.toInt16();", smapper.getI(0));
    4.42                      break;
    4.43                  case opc_aconst_null:
    4.44                      emit(out, "@1 = null;", smapper.pushA());
    4.45 @@ -921,7 +925,7 @@
    4.46                          case 11: jvmType = "[J"; break;
    4.47                          default: throw new IllegalStateException("Array type: " + atype);
    4.48                      }
    4.49 -                    emit(out, "@2 = new Array(@1).fillNulls().arrtype('@3');",
    4.50 +                    emit(out, "@2 = new Array(@1).initWith('@3', 0);",
    4.51                           smapper.popI(), smapper.pushA(), jvmType);
    4.52                      break;
    4.53                  case opc_anewarray: {
    4.54 @@ -933,7 +937,7 @@
    4.55                      } else {
    4.56                          typeName = "[L" + typeName + ";";
    4.57                      }
    4.58 -                    emit(out, "@2 = new Array(@1).fillNulls().arrtype('@3');",
    4.59 +                    emit(out, "@2 = new Array(@1).initWith('@3', null);",
    4.60                           smapper.popI(), smapper.pushA(), typeName);
    4.61                      break;
    4.62                  }
    4.63 @@ -943,7 +947,7 @@
    4.64                      String typeName = jc.getClassName(type);
    4.65                      int dim = readByte(byteCodes, ++i);
    4.66                      out.append("{ var a0 = new Array(").append(smapper.popI())
    4.67 -                       .append(").fillNulls().arrtype('").append(typeName).append("');");
    4.68 +                       .append(").initWith('").append(typeName).append("', null);");
    4.69                      for (int d = 1; d < dim; d++) {
    4.70                          typeName = typeName.substring(1);
    4.71                          out.append("\n  var l" + d).append(" = ")
    4.72 @@ -952,8 +956,8 @@
    4.73                              append(" < a" + (d - 1)).
    4.74                              append(".length; i" + d).append("++) {");
    4.75                          out.append("\n    var a" + d).
    4.76 -                            append (" = new Array(l" + d).append(").fillNulls().arrtype('")
    4.77 -                            .append(typeName).append("');");
    4.78 +                            append (" = new Array(l" + d).append(").initWith('")
    4.79 +                            .append(typeName).append("', null);");
    4.80                          out.append("\n    a" + (d - 1)).append("[i" + d).append("] = a" + d).
    4.81                              append(";");
    4.82                      }
     5.1 --- a/vm/src/test/java/org/apidesign/vm4brwsr/Array.java	Tue Jan 15 11:44:23 2013 +0100
     5.2 +++ b/vm/src/test/java/org/apidesign/vm4brwsr/Array.java	Tue Jan 15 11:53:07 2013 +0100
     5.3 @@ -98,6 +98,11 @@
     5.4          return sum;
     5.5      }
     5.6      
     5.7 +    public static int sum(int size) {
     5.8 +        int[] arr = new int[size];
     5.9 +        return arr[0] + arr[1];
    5.10 +    }
    5.11 +    
    5.12      static void arraycopy(char[] value, int srcBegin, char[] dst, int dstBegin, int count) {
    5.13          while (count-- > 0) {
    5.14              dst[dstBegin++] = value[srcBegin++];
     6.1 --- a/vm/src/test/java/org/apidesign/vm4brwsr/ArrayTest.java	Tue Jan 15 11:44:23 2013 +0100
     6.2 +++ b/vm/src/test/java/org/apidesign/vm4brwsr/ArrayTest.java	Tue Jan 15 11:53:07 2013 +0100
     6.3 @@ -27,6 +27,11 @@
     6.4   * @author Jaroslav Tulach <jtulach@netbeans.org>
     6.5   */
     6.6  public class ArrayTest {
     6.7 +    @Test public void intArrayShouldBeFilledWithZeroes() throws Exception {
     6.8 +            assertExec("0 + 0", Array.class, "sum__II", 
     6.9 +            Double.valueOf(0), 2
    6.10 +        );
    6.11 +    }
    6.12      @Test public void verifySimpleIntOperation() throws Exception {
    6.13              assertExec("CheckTheSum", Array.class, "simple__IZ", 
    6.14              Double.valueOf(15), false
     7.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     7.2 +++ b/vmtest/src/test/java/org/apidesign/bck2brwsr/tck/ByteArithmeticTest.java	Tue Jan 15 11:53:07 2013 +0100
     7.3 @@ -0,0 +1,102 @@
     7.4 +/**
     7.5 + * Back 2 Browser Bytecode Translator
     7.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     7.7 + *
     7.8 + * This program is free software: you can redistribute it and/or modify
     7.9 + * it under the terms of the GNU General Public License as published by
    7.10 + * the Free Software Foundation, version 2 of the License.
    7.11 + *
    7.12 + * This program is distributed in the hope that it will be useful,
    7.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    7.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    7.15 + * GNU General Public License for more details.
    7.16 + *
    7.17 + * You should have received a copy of the GNU General Public License
    7.18 + * along with this program. Look for COPYING file in the top folder.
    7.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
    7.20 + */
    7.21 +package org.apidesign.bck2brwsr.tck;
    7.22 +
    7.23 +import org.apidesign.bck2brwsr.vmtest.Compare;
    7.24 +import org.apidesign.bck2brwsr.vmtest.VMTest;
    7.25 +import org.testng.annotations.Factory;
    7.26 +
    7.27 +/**
    7.28 + *
    7.29 + * @author Jaroslav Tulach <jtulach@netbeans.org>
    7.30 + */
    7.31 +public class ByteArithmeticTest {
    7.32 +    
    7.33 +    private static byte add(byte x, byte y) {
    7.34 +        return (byte)(x + y);
    7.35 +    }
    7.36 +    
    7.37 +    private static byte sub(byte x, byte y) {
    7.38 +        return (byte)(x - y);
    7.39 +    }
    7.40 +    
    7.41 +    private static byte mul(byte x, byte y) {
    7.42 +        return (byte)(x * y);
    7.43 +    }
    7.44 +    
    7.45 +    private static byte div(byte x, byte y) {
    7.46 +        return (byte)(x / y);
    7.47 +    }
    7.48 +    
    7.49 +    private static byte mod(byte x, byte y) {
    7.50 +        return (byte)(x % y);
    7.51 +    }
    7.52 +    
    7.53 +    @Compare public byte conversion() {
    7.54 +        return (byte)123456;
    7.55 +    }
    7.56 +    
    7.57 +    @Compare public byte addOverflow() {
    7.58 +        return add(Byte.MAX_VALUE, (byte)1);
    7.59 +    }
    7.60 +    
    7.61 +    @Compare public byte subUnderflow() {
    7.62 +        return sub(Byte.MIN_VALUE, (byte)1);
    7.63 +    }
    7.64 +    
    7.65 +    @Compare public byte addMaxByteAndMaxByte() {
    7.66 +        return add(Byte.MAX_VALUE, Byte.MAX_VALUE);
    7.67 +    }
    7.68 +    
    7.69 +    @Compare public byte subMinByteAndMinByte() {
    7.70 +        return sub(Byte.MIN_VALUE, Byte.MIN_VALUE);
    7.71 +    }
    7.72 +    
    7.73 +    @Compare public byte multiplyMaxByte() {
    7.74 +        return mul(Byte.MAX_VALUE, (byte)2);
    7.75 +    }
    7.76 +    
    7.77 +    @Compare public byte multiplyMaxByteAndMaxByte() {
    7.78 +        return mul(Byte.MAX_VALUE, Byte.MAX_VALUE);
    7.79 +    }
    7.80 +    
    7.81 +    @Compare public byte multiplyMinByte() {
    7.82 +        return mul(Byte.MIN_VALUE, (byte)2);
    7.83 +    }
    7.84 +    
    7.85 +    @Compare public byte multiplyMinByteAndMinByte() {
    7.86 +        return mul(Byte.MIN_VALUE, Byte.MIN_VALUE);
    7.87 +    }
    7.88 +    
    7.89 +    @Compare public byte multiplyPrecision() {
    7.90 +        return mul((byte)17638, (byte)1103);
    7.91 +    }
    7.92 +    
    7.93 +    @Compare public byte division() {
    7.94 +        return div((byte)1, (byte)2);
    7.95 +    }
    7.96 +    
    7.97 +    @Compare public byte divisionReminder() {
    7.98 +        return mod((byte)1, (byte)2);
    7.99 +    }
   7.100 +    
   7.101 +    @Factory
   7.102 +    public static Object[] create() {
   7.103 +        return VMTest.create(ByteArithmeticTest.class);
   7.104 +    }
   7.105 +}
     8.1 --- a/vmtest/src/test/java/org/apidesign/bck2brwsr/tck/CompareStringsTest.java	Tue Jan 15 11:44:23 2013 +0100
     8.2 +++ b/vmtest/src/test/java/org/apidesign/bck2brwsr/tck/CompareStringsTest.java	Tue Jan 15 11:53:07 2013 +0100
     8.3 @@ -90,6 +90,11 @@
     8.4          return "Hello".matches("Hell");
     8.5      }
     8.6      
     8.7 +    @Compare public String emptyCharArray() {
     8.8 +        char[] arr = new char[10];
     8.9 +        return new String(arr);
    8.10 +    }
    8.11 +    
    8.12      @Compare public String variousCharacterTests() throws Exception {
    8.13          StringBuilder sb = new StringBuilder();
    8.14          
     9.1 --- a/vmtest/src/test/java/org/apidesign/bck2brwsr/tck/IntegerArithmeticTest.java	Tue Jan 15 11:44:23 2013 +0100
     9.2 +++ b/vmtest/src/test/java/org/apidesign/bck2brwsr/tck/IntegerArithmeticTest.java	Tue Jan 15 11:53:07 2013 +0100
     9.3 @@ -91,6 +91,25 @@
     9.4          return mod(1, 2);
     9.5      }
     9.6      
     9.7 +    @Compare public int sumTwoDimensions() {
     9.8 +        int[][] matrix = createMatrix(4, 3);
     9.9 +        int sum = 0;
    9.10 +        for (int i = 0; i < matrix.length; i++) {
    9.11 +            for (int j = 0; j < matrix[i].length; j++) {
    9.12 +                sum += matrix[i][j];
    9.13 +            }
    9.14 +        }
    9.15 +        return sum;
    9.16 +    }
    9.17 +    
    9.18 +    static int[][] createMatrix(int x, int y) {
    9.19 +        int[][] m = new int[x][y];
    9.20 +        for (int i = 0; i < Math.min(x, y); i++) {
    9.21 +            m[i][i] = i;
    9.22 +        }
    9.23 +        return m;
    9.24 +    }
    9.25 +    
    9.26      @Factory
    9.27      public static Object[] create() {
    9.28          return VMTest.create(IntegerArithmeticTest.class);
    10.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    10.2 +++ b/vmtest/src/test/java/org/apidesign/bck2brwsr/tck/ShortArithmeticTest.java	Tue Jan 15 11:53:07 2013 +0100
    10.3 @@ -0,0 +1,102 @@
    10.4 +/**
    10.5 + * Back 2 Browser Bytecode Translator
    10.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
    10.7 + *
    10.8 + * This program is free software: you can redistribute it and/or modify
    10.9 + * it under the terms of the GNU General Public License as published by
   10.10 + * the Free Software Foundation, version 2 of the License.
   10.11 + *
   10.12 + * This program is distributed in the hope that it will be useful,
   10.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
   10.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   10.15 + * GNU General Public License for more details.
   10.16 + *
   10.17 + * You should have received a copy of the GNU General Public License
   10.18 + * along with this program. Look for COPYING file in the top folder.
   10.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
   10.20 + */
   10.21 +package org.apidesign.bck2brwsr.tck;
   10.22 +
   10.23 +import org.apidesign.bck2brwsr.vmtest.Compare;
   10.24 +import org.apidesign.bck2brwsr.vmtest.VMTest;
   10.25 +import org.testng.annotations.Factory;
   10.26 +
   10.27 +/**
   10.28 + *
   10.29 + * @author Jaroslav Tulach <jtulach@netbeans.org>
   10.30 + */
   10.31 +public class ShortArithmeticTest {
   10.32 +    
   10.33 +    private static short add(short x, short y) {
   10.34 +        return (short)(x + y);
   10.35 +    }
   10.36 +    
   10.37 +    private static short sub(short x, short y) {
   10.38 +        return (short)(x - y);
   10.39 +    }
   10.40 +    
   10.41 +    private static short mul(short x, short y) {
   10.42 +        return (short)(x * y);
   10.43 +    }
   10.44 +    
   10.45 +    private static short div(short x, short y) {
   10.46 +        return (short)(x / y);
   10.47 +    }
   10.48 +    
   10.49 +    private static short mod(short x, short y) {
   10.50 +        return (short)(x % y);
   10.51 +    }
   10.52 +    
   10.53 +    @Compare public short conversion() {
   10.54 +        return (short)123456;
   10.55 +    }
   10.56 +    
   10.57 +    @Compare public short addOverflow() {
   10.58 +        return add(Short.MAX_VALUE, (short)1);
   10.59 +    }
   10.60 +    
   10.61 +    @Compare public short subUnderflow() {
   10.62 +        return sub(Short.MIN_VALUE, (short)1);
   10.63 +    }
   10.64 +    
   10.65 +    @Compare public short addMaxShortAndMaxShort() {
   10.66 +        return add(Short.MAX_VALUE, Short.MAX_VALUE);
   10.67 +    }
   10.68 +    
   10.69 +    @Compare public short subMinShortAndMinShort() {
   10.70 +        return sub(Short.MIN_VALUE, Short.MIN_VALUE);
   10.71 +    }
   10.72 +    
   10.73 +    @Compare public short multiplyMaxShort() {
   10.74 +        return mul(Short.MAX_VALUE, (short)2);
   10.75 +    }
   10.76 +    
   10.77 +    @Compare public short multiplyMaxShortAndMaxShort() {
   10.78 +        return mul(Short.MAX_VALUE, Short.MAX_VALUE);
   10.79 +    }
   10.80 +    
   10.81 +    @Compare public short multiplyMinShort() {
   10.82 +        return mul(Short.MIN_VALUE, (short)2);
   10.83 +    }
   10.84 +    
   10.85 +    @Compare public short multiplyMinShortAndMinShort() {
   10.86 +        return mul(Short.MIN_VALUE, Short.MIN_VALUE);
   10.87 +    }
   10.88 +    
   10.89 +    @Compare public short multiplyPrecision() {
   10.90 +        return mul((short)17638, (short)1103);
   10.91 +    }
   10.92 +    
   10.93 +    @Compare public short division() {
   10.94 +        return div((short)1, (short)2);
   10.95 +    }
   10.96 +    
   10.97 +    @Compare public short divisionReminder() {
   10.98 +        return mod((short)1, (short)2);
   10.99 +    }
  10.100 +    
  10.101 +    @Factory
  10.102 +    public static Object[] create() {
  10.103 +        return VMTest.create(ShortArithmeticTest.class);
  10.104 +    }
  10.105 +}