]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/internal/codegen/continuations/Cont.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.scl.compiler / src / org / simantics / scl / compiler / internal / codegen / continuations / Cont.java
diff --git a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/internal/codegen/continuations/Cont.java b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/internal/codegen/continuations/Cont.java
new file mode 100644 (file)
index 0000000..eb6d17d
--- /dev/null
@@ -0,0 +1,76 @@
+package org.simantics.scl.compiler.internal.codegen.continuations;\r
+\r
+import org.simantics.scl.compiler.internal.codegen.references.BoundVar;\r
+import org.simantics.scl.compiler.internal.codegen.references.Val;\r
+import org.simantics.scl.compiler.internal.codegen.references.ValRef;\r
+import org.simantics.scl.compiler.internal.codegen.ssa.SSABlock;\r
+import org.simantics.scl.compiler.internal.codegen.ssa.SSAFunction;\r
+import org.simantics.scl.compiler.internal.codegen.ssa.exits.Jump;\r
+import org.simantics.scl.compiler.internal.codegen.utils.CopyContext;\r
+import org.simantics.scl.compiler.types.TVar;\r
+import org.simantics.scl.compiler.types.Type;\r
+\r
+public abstract class Cont implements ICont {\r
+    \r
+    transient ContRef occurrence;\r
+\r
+    @Override\r
+    public ContRef createOccurrence() {\r
+        return new ContRef(this);\r
+    }\r
+    \r
+    public void replaceWith(Cont other) {\r
+        ContRef cur = occurrence;\r
+        if(cur != null) {\r
+            while(true) {\r
+                cur.binding = other;\r
+                if(cur.next == null)\r
+                    break;\r
+                else\r
+                    cur = cur.next;\r
+            }\r
+            cur.next = other.occurrence;\r
+            if(other.occurrence != null)\r
+                other.occurrence.prev = cur;\r
+            other.occurrence = occurrence;\r
+            occurrence = null;\r
+        }        \r
+    }\r
+    \r
+    public abstract int getArity();    \r
+    public abstract Type getParameterType(int parameterId);\r
+    \r
+    public int occurrenceCount() {\r
+        int count = 0;\r
+        for(ContRef ref = occurrence;ref != null;ref=ref.next)\r
+            ++count;\r
+        return count;\r
+    }\r
+\r
+    public boolean hasMoreThanOneOccurences() {\r
+        return occurrence != null && occurrence.next != null;\r
+    }\r
+    \r
+    public boolean hasNoOccurences() {\r
+        return occurrence == null;\r
+    }\r
+\r
+    public abstract Cont copy(CopyContext context);\r
+    public abstract void replace(TVar[] vars, Type[] replacements);\r
+    \r
+    public ContRef getOccurrence() {\r
+        return occurrence;\r
+    }\r
+    \r
+    public Cont createProxy(SSAFunction function, Val[] newParameters, Val[] oldParameters) {\r
+        BoundVar[] proxyParameters = new BoundVar[oldParameters.length];\r
+        for(int i=0;i<oldParameters.length;++i)\r
+            proxyParameters[i] = new BoundVar(oldParameters[i].getType());\r
+        SSABlock block = new SSABlock(proxyParameters);\r
+        function.addBlock(block);\r
+        block.setExit(new Jump(createOccurrence(), \r
+                ValRef.concat(ValRef.createOccurrences(newParameters), \r
+                        ValRef.createOccurrences(proxyParameters))));\r
+        return block;\r
+    }\r
+}\r