]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/internal/codegen/ssa/SSAObject.java
(refs #7250) Merging master, minor CHR bugfixes
[simantics/platform.git] / bundles / org.simantics.scl.compiler / src / org / simantics / scl / compiler / internal / codegen / ssa / SSAObject.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.ValRef;
7 import org.simantics.scl.compiler.internal.codegen.ssa.binders.ClosureBinder;
8 import org.simantics.scl.compiler.internal.codegen.utils.CopyContext;
9 import org.simantics.scl.compiler.internal.codegen.utils.PrintingContext;
10 import org.simantics.scl.compiler.internal.codegen.utils.SSALambdaLiftingContext;
11 import org.simantics.scl.compiler.internal.codegen.utils.SSASimplificationContext;
12 import org.simantics.scl.compiler.internal.codegen.utils.SSAValidationContext;
13 import org.simantics.scl.compiler.internal.codegen.utils.ValRefVisitor;
14 import org.simantics.scl.compiler.internal.codegen.writer.CodeWriter;
15 import org.simantics.scl.compiler.internal.codegen.writer.ModuleWriter;
16 import org.simantics.scl.compiler.types.TVar;
17 import org.simantics.scl.compiler.types.Type;
18
19 public class SSAObject extends SSAClosure implements ClosureBinder {
20     Type type;
21     SSAClosure firstClosure;
22     
23     public SSAObject(Type type) {
24         this.type = type;
25     }
26
27     @Override
28     public void toString(PrintingContext context) {
29         for(SSAClosure closure = firstClosure; closure != null; closure = closure.getNext()) {
30             context.indentation();
31             context.append(closure.getTarget());
32             context.append("(" + closure.getTarget().occurrenceCount() + ")");
33             context.append(" :: ");
34             context.append(closure.getTarget().getType());
35             context.append(" = \n");
36             context.indent();
37             closure.toString(context);
38             context.dedent();
39         }
40     }
41
42     @Override
43     public SSAClosure getFirstClosure() {
44         return firstClosure;
45     }
46     
47     @Override
48     public void setFirstClosure(SSAClosure function) {
49         this.firstClosure = function;     
50         if(function == null)
51             detach();
52     }
53     
54     @Override
55     public void destroy() {
56         for(SSAClosure closure = firstClosure; closure != null; closure = closure.getNext())
57             closure.destroy();
58     }
59
60     @Override
61     public SSAClosure copy(CopyContext context) {
62         SSAObject result = new SSAObject(context.copyType(type));
63         for(SSAClosure closure = firstClosure; closure != null; closure = closure.getNext()) {
64             SSAClosure newFunction = closure.copy(context);
65             newFunction.setTarget(context.copy(closure.getTarget()));
66             result.addClosure(newFunction);
67         }
68         return result;    
69     }
70
71     public void addClosure(SSAClosure closure) {
72         closure.setParent(this);        
73         closure.setNext(firstClosure);
74         if(firstClosure != null)
75             firstClosure.setPrev(closure);
76         firstClosure = closure;
77     }
78     
79     @Override
80     public void markGenerateOnFly() {
81         for(SSAClosure closure = firstClosure; closure != null; closure = closure.getNext())
82             closure.markGenerateOnFly();
83     }
84
85     @Override
86     public void replace(TVar[] vars, Type[] replacements) {
87         for(SSAClosure closure = firstClosure; closure != null; closure = closure.getNext())
88             closure.replace(vars, replacements);
89     }
90
91     @Override
92     public void collectFreeVariables(ArrayList<ValRef> freeVars) {
93         for(SSAClosure closure = firstClosure; closure != null; closure = closure.getNext())
94             closure.collectFreeVariables(freeVars);
95     }
96
97     @Override
98     public void simplify(SSASimplificationContext context) {
99         for(SSAClosure closure = firstClosure; closure != null; closure = closure.getNext())
100             closure.simplify(context);
101     }
102
103     @Override
104     public void validate(SSAValidationContext context) {
105         for(SSAClosure closure = firstClosure; closure != null; closure = closure.getNext())
106             closure.validate(context);
107     }
108
109     @Override
110     public void lambdaLift(SSALambdaLiftingContext context) {
111         for(SSAClosure closure = firstClosure; closure != null; closure = closure.getNext())
112             closure.lambdaLift(context);
113     }
114
115     @Override
116     public boolean isValue() {
117         return false;
118     }
119
120     @Override
121     public void parametrize(BoundVar[] parameters) {
122         // TODO Auto-generated method stub
123         
124     }
125
126     @Override
127     public Type getType() {
128         return type;
129     }
130     
131     public CodeWriter createMethod(ModuleWriter moduleWriter, TVar[] typeParameters, Type effect, Type returnType, Type[] parameterTypes) {
132         SSAFunction function = new SSAFunction(typeParameters, effect, returnType);
133         SSABlock block = new SSABlock(parameterTypes);
134         function.addBlock(block);
135         BoundVar target = new BoundVar(function.getType());
136         function.setTarget(target);
137         addClosure(function);
138         return new CodeWriter(moduleWriter, block);
139     }
140
141     @Override
142     public void forValRefs(ValRefVisitor visitor) {
143         for(SSAClosure closure = firstClosure; closure != null; closure = closure.getNext())
144             closure.forValRefs(visitor);
145     }
146
147 }