]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/internal/codegen/ssa/exits/Switch.java
Merge changes Ib64cf026,I238948da
[simantics/platform.git] / bundles / org.simantics.scl.compiler / src / org / simantics / scl / compiler / internal / codegen / ssa / exits / Switch.java
index a6674eb1b3b265233062becd15d11a19ece2a829..386199d838d3cfd0e60cb1faea0375bb45791e21 100644 (file)
@@ -1,10 +1,13 @@
 package org.simantics.scl.compiler.internal.codegen.ssa.exits;
 
 import java.util.ArrayList;
+import java.util.Arrays;
 
 import org.objectweb.asm.Label;
 import org.simantics.scl.compiler.common.exceptions.InternalCompilerError;
 import org.simantics.scl.compiler.constants.BooleanConstant;
+import org.simantics.scl.compiler.constants.IntegerConstant;
+import org.simantics.scl.compiler.constants.NoRepConstant;
 import org.simantics.scl.compiler.internal.codegen.continuations.BranchRef;
 import org.simantics.scl.compiler.internal.codegen.continuations.Cont;
 import org.simantics.scl.compiler.internal.codegen.continuations.ContRef;
@@ -19,10 +22,13 @@ import org.simantics.scl.compiler.internal.codegen.utils.MethodBuilder;
 import org.simantics.scl.compiler.internal.codegen.utils.PrintingContext;
 import org.simantics.scl.compiler.internal.codegen.utils.SSASimplificationContext;
 import org.simantics.scl.compiler.internal.codegen.utils.SSAValidationContext;
+import org.simantics.scl.compiler.internal.codegen.utils.ValRefVisitor;
 import org.simantics.scl.compiler.types.TVar;
 import org.simantics.scl.compiler.types.Type;
 import org.simantics.scl.compiler.types.Types;
 
+import gnu.trove.map.hash.TIntObjectHashMap;
+
 public class Switch extends SSAExit implements ValRefBinder {
 
     ValRef scrutinee;
@@ -43,24 +49,64 @@ public class Switch extends SSAExit implements ValRefBinder {
     public BranchRef[] getBranches() {
         return branches;
     }
+    
+    private boolean isIntegerSwitch() {
+        if(scrutinee.getType() != Types.INTEGER)
+            return false;
+        for(BranchRef branch : branches)
+            if(branch.constructor != null && !(branch.constructor instanceof IntegerConstant))
+                return false;
+        return true;
+    }
+    
+    private void generateIntegerSwitch(MethodBuilder mb) {
+        int defaultId;
+        for(defaultId=0;defaultId<branches.length-1&&branches[defaultId].constructor!=null;++defaultId);
+        int[] values = new int[defaultId];
+        Cont[] continuations = new Cont[defaultId+1];
+        TIntObjectHashMap<Label> labelMap = new TIntObjectHashMap<Label>(defaultId); 
+        for(int i=0;i<defaultId;++i) {
+            int value = ((IntegerConstant)branches[i].constructor).getValue();
+            values[i] = value;
+            Cont cont = branches[i].cont.getBinding();
+            labelMap.put(value,  mb.getLabel(cont));
+            continuations[i] = cont;
+        }
+        Arrays.sort(values);
+        Label[] labels = new Label[defaultId];
+        for(int i=0;i<defaultId;++i)
+            labels[i] = labelMap.get(values[i]);
+        Label defaultLabel;
+        {
+            Cont cont = branches[defaultId].cont.getBinding();
+            defaultLabel = mb.getLabel(cont);
+            continuations[defaultId] = cont;
+        }
+        mb.push(scrutinee, Types.INTEGER);
+        mb.switch_(values, labels, defaultLabel);
+        for(Cont cont : continuations)
+            mb.ensureExists(cont);
+    }
 
     @Override
     public void generateCode(MethodBuilder mb) {
+        if(isIntegerSwitch()) {
+            generateIntegerSwitch(mb);
+            return;
+        }
         for(int i=0;i<branches.length;++i) {
             BranchRef branch = branches[i];
-            if(branch.constructor == null) {
+            if(branch.constructor == null)
                 mb.jump(branch.cont);
-            }
             else if(i < branches.length-1) {                
                 Label failure = mb.createLabel();
                 branch.constructor.deconstruct(mb, scrutinee, 
                         branch.cont.getBinding(), failure);
                 mb.setLocation(failure);
             }
-            else {
+            else
                 branch.constructor.deconstruct(mb, scrutinee, 
                         branch.cont.getBinding(), null);
-            }
         }
     }
 
@@ -80,16 +126,16 @@ public class Switch extends SSAExit implements ValRefBinder {
                 Cont cont = branch.cont.getBinding();
                 if(cont instanceof SSABlock) {
                     SSABlock block = (SSABlock)cont;
-                    if(cont.hasMoreThanOneOccurences()) {
+                    //if(cont.hasMoreThanOneOccurences()) {
                         context.append(cont);
                         context.append('\n');
                         context.addBlock(block);
-                    }
+                    /*}
                     else {
                         block.parametersToString(context);
                         context.append('\n');
                         block.bodyToString(context);
-                    }
+                    }*/
                 }
                 else {
                     context.append(cont);
@@ -186,11 +232,15 @@ public class Switch extends SSAExit implements ValRefBinder {
             context.markModified("switch-to-if");
             newExit.simplify(context);
         }
-        else if(branches.length == 1 && branches[0].constructor == null) {
+        else if(branches.length == 1 && isConstructorParameterless(branches[0])) {
             scrutinee.remove();
             getParent().setExit(new Jump(branches[0].cont));
         }
     }
+    
+    private static boolean isConstructorParameterless(BranchRef branch) {
+        return branch.constructor == null || branch.constructor instanceof NoRepConstant;
+    }
 
     @Override
     public void collectFreeVariables(SSAFunction function,
@@ -224,4 +274,9 @@ public class Switch extends SSAExit implements ValRefBinder {
         }
         return result.toArray(new SSABlock[result.size()]);
     }
+
+    @Override
+    public void forValRefs(ValRefVisitor visitor) {
+        visitor.visit(scrutinee);
+    }
 }