]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/internal/codegen/utils/MethodBuilderBase.java
Merged changes from feature/scl to master.
[simantics/platform.git] / bundles / org.simantics.scl.compiler / src / org / simantics / scl / compiler / internal / codegen / utils / MethodBuilderBase.java
index 0f56aba8e2fbeae1c22c8b8893d3091a8921326c..7fb83fb49e3fd210dddd0ed7228a9febdaa792da 100644 (file)
@@ -429,6 +429,10 @@ public class MethodBuilderBase {
     public void newObject(TypeDesc type) {
         methodVisitor.visitTypeInsn(Opcodes.NEW, getClassName(type));
     }
+    
+    public void newObject(String type) {
+        methodVisitor.visitTypeInsn(Opcodes.NEW, type);
+    }
 
     public void newObject(TypeDesc type, int dimensions) {
         methodVisitor.visitMultiANewArrayInsn(type.getDescriptor(), dimensions);
@@ -576,4 +580,30 @@ public class MethodBuilderBase {
     public static String getClassName(Class<?> clazz) {
         return clazz.getName().replace('.', '/');
     }
+    
+    public void switch_(int[] values, Label[] labels, Label defaultLabel) {
+        int lo = values[0];
+        int hi = values[values.length-1];
+        long table_space_cost = 4 + ((long) hi - lo + 1); // words
+        long table_time_cost = 3; // comparisons
+        long lookup_space_cost = 3 + 2 * (long) values.length;
+        long lookup_time_cost = values.length;
+        if(values.length > 0 &&
+            table_space_cost + 3 * table_time_cost <=
+            lookup_space_cost + 3 * lookup_time_cost) {
+            Label[] table = new Label[hi - lo + 1];
+            for(int i=0,j=0;i<table.length;++i) {
+                int id = lo+i;
+                if(values[j] == id) {
+                    table[i] = labels[j];
+                    ++j;
+                }
+                else
+                    table[i] = defaultLabel;
+            }
+            methodVisitor.visitTableSwitchInsn(lo, hi, defaultLabel, table);
+        }
+        else
+            methodVisitor.visitLookupSwitchInsn(defaultLabel, values, labels);
+    }
 }