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