1 package org.simantics.scl.compiler.internal.codegen.ssa;
\r
3 import java.util.ArrayList;
\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
19 public class SSAObject extends SSAClosure implements ClosureBinder {
\r
21 SSAClosure firstClosure;
\r
23 public SSAObject(Type type) {
\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
37 closure.toString(context);
\r
43 public SSAClosure getFirstClosure() {
\r
44 return firstClosure;
\r
48 public void setFirstClosure(SSAClosure function) {
\r
49 this.firstClosure = function;
\r
50 if(function == null)
\r
55 public void destroy() {
\r
56 for(SSAClosure closure = firstClosure; closure != null; closure = closure.getNext())
\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
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
80 public void markGenerateOnFly() {
\r
81 for(SSAClosure closure = firstClosure; closure != null; closure = closure.getNext())
\r
82 closure.markGenerateOnFly();
\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
92 public void collectFreeVariables(ArrayList<ValRef> freeVars) {
\r
93 for(SSAClosure closure = firstClosure; closure != null; closure = closure.getNext())
\r
94 closure.collectFreeVariables(freeVars);
\r
98 public void simplify(SSASimplificationContext context) {
\r
99 for(SSAClosure closure = firstClosure; closure != null; closure = closure.getNext())
\r
100 closure.simplify(context);
\r
104 public void validate(SSAValidationContext context) {
\r
105 for(SSAClosure closure = firstClosure; closure != null; closure = closure.getNext())
\r
106 closure.validate(context);
\r
110 public void lambdaLift(SSALambdaLiftingContext context) {
\r
111 for(SSAClosure closure = firstClosure; closure != null; closure = closure.getNext())
\r
112 closure.lambdaLift(context);
\r
116 public boolean isValue() {
\r
121 public void parametrize(BoundVar[] parameters) {
\r
122 // TODO Auto-generated method stub
\r
127 public Type getType() {
\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
142 public void forValRefs(ValRefVisitor visitor) {
\r
143 for(SSAClosure closure = firstClosure; closure != null; closure = closure.getNext())
\r
144 closure.forValRefs(visitor);
\r