]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/internal/codegen/ssa/SSAStatement.java
Added memory leak test and fixed the leak by removing references
[simantics/platform.git] / bundles / org.simantics.scl.compiler / src / org / simantics / scl / compiler / internal / codegen / ssa / SSAStatement.java
1 package org.simantics.scl.compiler.internal.codegen.ssa;
2
3 import java.util.ArrayList;
4
5 import org.simantics.scl.compiler.internal.codegen.references.BoundVar;
6 import org.simantics.scl.compiler.internal.codegen.references.Val;
7 import org.simantics.scl.compiler.internal.codegen.references.ValRef;
8 import org.simantics.scl.compiler.internal.codegen.ssa.statements.LetApply;
9 import org.simantics.scl.compiler.internal.codegen.utils.CopyContext;
10 import org.simantics.scl.compiler.internal.codegen.utils.MethodBuilder;
11 import org.simantics.scl.compiler.internal.codegen.utils.Printable;
12 import org.simantics.scl.compiler.internal.codegen.utils.PrintingContext;
13 import org.simantics.scl.compiler.internal.codegen.utils.SSALambdaLiftingContext;
14 import org.simantics.scl.compiler.internal.codegen.utils.SSASimplificationContext;
15 import org.simantics.scl.compiler.internal.codegen.utils.SSAValidationContext;
16 import org.simantics.scl.compiler.internal.codegen.utils.ValRefVisitor;
17 import org.simantics.scl.compiler.types.TVar;
18 import org.simantics.scl.compiler.types.Type;
19 import org.simantics.scl.compiler.types.Types;
20
21
22 public abstract class SSAStatement implements Printable {
23     SSABlock parent;
24     SSAStatement prev;
25     SSAStatement next;
26     public long location;
27     
28     public void detach() {
29         if(prev == null)
30             parent.firstStatement = next;
31         else
32             prev.next = next;
33         if(next == null)
34             parent.lastStatement = prev;
35         else
36             next.prev = prev;
37     }
38
39     public abstract void generateCode(MethodBuilder mb);
40
41     public abstract void validate(SSAValidationContext context);
42
43     public void simplify(SSASimplificationContext context) {
44     }
45
46     public abstract void destroy();
47     
48     public SSABlock getParent() {
49         return parent;
50     }
51     
52     public SSAStatement getPrev() {
53         return prev;
54     }
55     
56     public void setAsLastStatement() {
57         this.next = null;
58         parent.lastStatement = this;
59     }
60     
61     public SSAStatement getNext() {
62         return next;
63     }
64     
65     public void remove() {
66         detach();
67         destroy();        
68     }
69     
70     @Override
71     public String toString() {
72         PrintingContext context = new PrintingContext();
73         toString(context);
74         return context.toString();
75     }
76
77     public void markGenerateOnFly() {        
78     }
79
80     public abstract SSAStatement copy(CopyContext context);
81     public abstract void replace(TVar[] vars, Type[] replacements);
82
83     public abstract void addBoundVariablesTo(SSAValidationContext context);
84
85     public abstract void collectFreeVariables(SSAFunction function, ArrayList<ValRef> vars);
86     
87     public SSAFunction getParentFunction() {
88         return parent.parent;
89     }
90
91     public void lambdaLift(SSALambdaLiftingContext context) {
92     }
93     
94     public void insertBefore(SSAStatement statement) {
95         next = statement;
96         prev = statement.prev;
97         parent = statement.parent;
98         if(prev == null)
99             parent.firstStatement = this;
100         else
101             prev.next = this;
102         statement.prev = this;        
103     }
104     
105     public void insertAfter(SSAStatement statement) {
106         prev = statement;
107         next = statement.next;
108         parent = statement.parent;
109         if(next == null)
110             parent.lastStatement = this;
111         else
112             next.prev = this;
113         statement.next = this;        
114     }
115     
116     public void replaceByApply(ValRef valRef, Val function, Type[] typeParameters, Val[] parameters) {
117         BoundVar target = new BoundVar(valRef.getBinding().getType());
118         new LetApply(target, Types.NO_EFFECTS, function.createOccurrence(typeParameters), ValRef.createOccurrences(parameters)).insertBefore(this);
119         valRef.replaceBy(target);
120     }
121
122     public void prepare(MethodBuilder mb) {        
123     }
124
125     public abstract void forValRefs(ValRefVisitor visitor);
126
127     public abstract void cleanup();
128 }