Implementation of multianewarray bytecode instruction
authorJaroslav Tulach <jtulach@netbeans.org>
Tue, 30 Oct 2012 22:22:18 +0100
changeset 1288db5cf267d74
parent 127 5134b17d623c
child 129 15df78d24302
Implementation of multianewarray bytecode instruction
vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeToJavaScript.java
vm/src/test/java/org/apidesign/vm4brwsr/Array.java
vm/src/test/java/org/apidesign/vm4brwsr/ArrayTest.java
     1.1 --- a/vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeToJavaScript.java	Tue Oct 30 20:29:55 2012 +0100
     1.2 +++ b/vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeToJavaScript.java	Tue Oct 30 22:22:18 2012 +0100
     1.3 @@ -236,7 +236,7 @@
     1.4          for (int i = 0; i < byteCodes.length; i++) {
     1.5              int prev = i;
     1.6              out.append("    case " + i).append(": ");
     1.7 -            final int c = (byteCodes[i] + 256) % 256;
     1.8 +            final int c = readByte(byteCodes, i);
     1.9              switch (c) {
    1.10                  case bc_aload_0:
    1.11                  case bc_iload_0:
    1.12 @@ -271,7 +271,7 @@
    1.13                  case bc_fload:
    1.14                  case bc_dload:
    1.15                  case bc_aload: {
    1.16 -                    final int indx = (byteCodes[++i] + 256) % 256;
    1.17 +                    final int indx = readByte(byteCodes, ++i);
    1.18                      out.append("stack.push(arg").append(indx + ");");
    1.19                      break;
    1.20                  }
    1.21 @@ -280,7 +280,7 @@
    1.22                  case bc_fstore:
    1.23                  case bc_dstore:
    1.24                  case bc_astore: {
    1.25 -                    final int indx = (byteCodes[++i] + 256) % 256;
    1.26 +                    final int indx = readByte(byteCodes, ++i);
    1.27                      out.append("arg" + indx).append(" = stack.pop()");
    1.28                      break;
    1.29                  }
    1.30 @@ -369,7 +369,7 @@
    1.31                      out.append("{ var v = stack.pop(); stack.push(stack.pop() >>> v); }");
    1.32                      break;
    1.33                  case bc_iinc: {
    1.34 -                    final int varIndx = (byteCodes[++i] + 256) % 256;
    1.35 +                    final int varIndx = readByte(byteCodes, ++i);
    1.36                      final int incrBy = byteCodes[++i];
    1.37                      if (incrBy == 1) {
    1.38                          out.append("arg" + varIndx).append("++;");
    1.39 @@ -442,7 +442,7 @@
    1.40                      out.append("stack.push(5);");
    1.41                      break;
    1.42                  case bc_ldc: {
    1.43 -                    int indx = byteCodes[++i];
    1.44 +                    int indx = readByte(byteCodes, ++i);
    1.45                      CPEntry entry = jc.getConstantPool().get(indx);
    1.46                      String v = encodeConstant(entry);
    1.47                      out.append("stack.push(").append(v).append(");");
    1.48 @@ -553,7 +553,7 @@
    1.49                      break;
    1.50                  }
    1.51                  case bc_lookupswitch: {
    1.52 -                    int table = (i - 1) / 4 * 4 + 4;
    1.53 +                    int table = i / 4 * 4 + 4;
    1.54                      int dflt = i + readInt4(byteCodes, table);
    1.55                      table += 4;
    1.56                      int n = readInt4(byteCodes, table);
    1.57 @@ -571,7 +571,7 @@
    1.58                      break;
    1.59                  }
    1.60                  case bc_tableswitch: {
    1.61 -                    int table = (i - 1) / 4 * 4 + 4;
    1.62 +                    int table = i / 4 * 4 + 4;
    1.63                      int dflt = i + readInt4(byteCodes, table);
    1.64                      table += 4;
    1.65                      int low = readInt4(byteCodes, table);
    1.66 @@ -622,6 +622,26 @@
    1.67                      out.append("stack.push(new Array(stack.pop()));");
    1.68                      break;
    1.69                  }
    1.70 +                case bc_multianewarray: {
    1.71 +                    i += 2;
    1.72 +                    int dim = readByte(byteCodes, ++i);
    1.73 +                    out.append("{ var a0 = new Array(stack.pop());");
    1.74 +                    for (int d = 1; d < dim; d++) {
    1.75 +                        out.append("\n  var l" + d).append(" = stack.pop();");
    1.76 +                        out.append("\n  for (var i" + d).append (" = 0; i" + d).
    1.77 +                            append(" < a" + (d - 1)).
    1.78 +                            append(".length; i" + d).append("++) {");
    1.79 +                        out.append("\n    var a" + d).
    1.80 +                            append (" = new Array(l" + d).append(");");
    1.81 +                        out.append("\n    a" + (d - 1)).append("[i" + d).append("] = a" + d).
    1.82 +                            append(";");
    1.83 +                    }
    1.84 +                    for (int d = 1; d < dim; d++) {
    1.85 +                        out.append("\n  }");
    1.86 +                    }
    1.87 +                    out.append("\nstack.push(a0); }");
    1.88 +                    break;
    1.89 +                }
    1.90                  case bc_arraylength:
    1.91                      out.append("stack.push(stack.pop().length);");
    1.92                      break;
    1.93 @@ -731,7 +751,7 @@
    1.94              out.append(" //");
    1.95              for (int j = prev; j <= i; j++) {
    1.96                  out.append(" ");
    1.97 -                final int cc = (byteCodes[j] + 256) % 256;
    1.98 +                final int cc = readByte(byteCodes, j);
    1.99                  out.append(Integer.toString(cc));
   1.100              }
   1.101              out.append("\n");
   1.102 @@ -758,6 +778,9 @@
   1.103          final int a = byteCodes[offsetInstruction + 3];
   1.104          return (d & 0xff000000) | (c & 0xff0000) | (b & 0xff00) | (a & 0xff);
   1.105      }
   1.106 +    private int readByte(byte[] byteCodes, int offsetInstruction) {
   1.107 +        return (byteCodes[offsetInstruction] + 256) % 256;
   1.108 +    }
   1.109      
   1.110      private static int countArgs(String descriptor, boolean[] hasReturnType, StringBuilder sig) {
   1.111          int cnt = 0;
     2.1 --- a/vm/src/test/java/org/apidesign/vm4brwsr/Array.java	Tue Oct 30 20:29:55 2012 +0100
     2.2 +++ b/vm/src/test/java/org/apidesign/vm4brwsr/Array.java	Tue Oct 30 22:22:18 2012 +0100
     2.3 @@ -53,8 +53,12 @@
     2.4      
     2.5      private static final Array[] ARR = { new Array(), new Array(), new Array() };
     2.6      
     2.7 -    private static Array[] arr() {
     2.8 -        return ARR;
     2.9 +    private static Array[][] arr() {
    2.10 +        Array[][] matrix = new Array[3][3];
    2.11 +        for (int i = 0; i < ARR.length; i++) {
    2.12 +            matrix[i][i] = ARR[i];
    2.13 +        }
    2.14 +        return matrix;
    2.15      }
    2.16      private static <T> T[] filter(T[] in) {
    2.17          return in;
    2.18 @@ -62,12 +66,19 @@
    2.19      
    2.20      public static double sum() {
    2.21          double sum = 0.0;
    2.22 -        for (int i = 0; i < arr().length; i++) {
    2.23 -            sum += arr()[i].bytes();
    2.24 -            sum += arr()[i].shorts();
    2.25 -            sum += arr()[i].ints()[2];
    2.26 -            sum += arr()[i].floats();
    2.27 -            sum += filter(arr())[i].doubles();
    2.28 +        for (Array[] row : arr()) {
    2.29 +            int indx = -1;
    2.30 +            for (Array a : row) {
    2.31 +                indx++;
    2.32 +                if (a == null) {
    2.33 +                    continue;
    2.34 +                }
    2.35 +                sum += a.bytes();
    2.36 +                sum += a.shorts();
    2.37 +                sum += a.ints()[2];
    2.38 +                sum += a.floats();
    2.39 +                sum += filter(row)[indx].doubles();
    2.40 +            }
    2.41          }
    2.42          return sum;
    2.43      }
    2.44 @@ -75,8 +86,8 @@
    2.45          int[] arr = { 0, 1, 2, 3, 4, 5 };
    2.46          
    2.47          int sum = 0;
    2.48 -        for (int i = 0; i < arr.length; i++) {
    2.49 -            sum += arr[i];
    2.50 +        for (int a : arr) {
    2.51 +            sum += a;
    2.52          }
    2.53          return sum;
    2.54      }
    2.55 @@ -89,7 +100,7 @@
    2.56  
    2.57      public static char copyArray() {
    2.58          char[] arr = { '0' };
    2.59 -        arraycopy(arr()[0].chars, 0, arr, 0, 1);
    2.60 +        arraycopy(arr()[0][0].chars, 0, arr, 0, 1);
    2.61          return arr[0];
    2.62      }
    2.63  }
     3.1 --- a/vm/src/test/java/org/apidesign/vm4brwsr/ArrayTest.java	Tue Oct 30 20:29:55 2012 +0100
     3.2 +++ b/vm/src/test/java/org/apidesign/vm4brwsr/ArrayTest.java	Tue Oct 30 22:22:18 2012 +0100
     3.3 @@ -36,9 +36,9 @@
     3.4  
     3.5  import javax.script.Invocable;
     3.6  import javax.script.ScriptException;
     3.7 -import org.testng.annotations.Test;
     3.8  import static org.testng.Assert.*;
     3.9  import org.testng.annotations.BeforeClass;
    3.10 +import org.testng.annotations.Test;
    3.11  
    3.12  /**
    3.13   *
    3.14 @@ -50,6 +50,11 @@
    3.15              Double.valueOf(15)
    3.16          );
    3.17      }
    3.18 +    
    3.19 +    @Test public void realOperationOnArrays() throws Exception {
    3.20 +        assertEquals(Array.sum(), 105.0, "Computes to 105");
    3.21 +    }
    3.22 +    
    3.23      @Test public void verifyOperationsOnArrays() throws Exception {
    3.24          assertExec("The sum is 105", "org_apidesign_vm4brwsr_Array_sumD", 
    3.25              Double.valueOf(105)