]> gerrit.simantics Code Review - simantics/platform.git/blob - 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
1 package org.simantics.scl.compiler.internal.codegen.continuations;\r
2 \r
3 import org.simantics.scl.compiler.internal.codegen.references.BoundVar;\r
4 import org.simantics.scl.compiler.internal.codegen.references.Val;\r
5 import org.simantics.scl.compiler.internal.codegen.references.ValRef;\r
6 import org.simantics.scl.compiler.internal.codegen.ssa.SSABlock;\r
7 import org.simantics.scl.compiler.internal.codegen.ssa.SSAFunction;\r
8 import org.simantics.scl.compiler.internal.codegen.ssa.exits.Jump;\r
9 import org.simantics.scl.compiler.internal.codegen.utils.CopyContext;\r
10 import org.simantics.scl.compiler.types.TVar;\r
11 import org.simantics.scl.compiler.types.Type;\r
12 \r
13 public abstract class Cont implements ICont {\r
14     \r
15     transient ContRef occurrence;\r
16 \r
17     @Override\r
18     public ContRef createOccurrence() {\r
19         return new ContRef(this);\r
20     }\r
21     \r
22     public void replaceWith(Cont other) {\r
23         ContRef cur = occurrence;\r
24         if(cur != null) {\r
25             while(true) {\r
26                 cur.binding = other;\r
27                 if(cur.next == null)\r
28                     break;\r
29                 else\r
30                     cur = cur.next;\r
31             }\r
32             cur.next = other.occurrence;\r
33             if(other.occurrence != null)\r
34                 other.occurrence.prev = cur;\r
35             other.occurrence = occurrence;\r
36             occurrence = null;\r
37         }        \r
38     }\r
39     \r
40     public abstract int getArity();    \r
41     public abstract Type getParameterType(int parameterId);\r
42     \r
43     public int occurrenceCount() {\r
44         int count = 0;\r
45         for(ContRef ref = occurrence;ref != null;ref=ref.next)\r
46             ++count;\r
47         return count;\r
48     }\r
49 \r
50     public boolean hasMoreThanOneOccurences() {\r
51         return occurrence != null && occurrence.next != null;\r
52     }\r
53     \r
54     public boolean hasNoOccurences() {\r
55         return occurrence == null;\r
56     }\r
57 \r
58     public abstract Cont copy(CopyContext context);\r
59     public abstract void replace(TVar[] vars, Type[] replacements);\r
60     \r
61     public ContRef getOccurrence() {\r
62         return occurrence;\r
63     }\r
64     \r
65     public Cont createProxy(SSAFunction function, Val[] newParameters, Val[] oldParameters) {\r
66         BoundVar[] proxyParameters = new BoundVar[oldParameters.length];\r
67         for(int i=0;i<oldParameters.length;++i)\r
68             proxyParameters[i] = new BoundVar(oldParameters[i].getType());\r
69         SSABlock block = new SSABlock(proxyParameters);\r
70         function.addBlock(block);\r
71         block.setExit(new Jump(createOccurrence(), \r
72                 ValRef.concat(ValRef.createOccurrences(newParameters), \r
73                         ValRef.createOccurrences(proxyParameters))));\r
74         return block;\r
75     }\r
76 }\r