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