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