]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/internal/codegen/ssa/SSAObject.java
Merged changes from feature/scl to master.
[simantics/platform.git] / bundles / org.simantics.scl.compiler / src / org / simantics / scl / compiler / internal / codegen / ssa / SSAObject.java
diff --git a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/internal/codegen/ssa/SSAObject.java b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/internal/codegen/ssa/SSAObject.java
new file mode 100644 (file)
index 0000000..8735b76
--- /dev/null
@@ -0,0 +1,147 @@
+package org.simantics.scl.compiler.internal.codegen.ssa;\r
+\r
+import java.util.ArrayList;\r
+\r
+import org.simantics.scl.compiler.internal.codegen.references.BoundVar;\r
+import org.simantics.scl.compiler.internal.codegen.references.ValRef;\r
+import org.simantics.scl.compiler.internal.codegen.ssa.binders.ClosureBinder;\r
+import org.simantics.scl.compiler.internal.codegen.utils.CopyContext;\r
+import org.simantics.scl.compiler.internal.codegen.utils.PrintingContext;\r
+import org.simantics.scl.compiler.internal.codegen.utils.SSALambdaLiftingContext;\r
+import org.simantics.scl.compiler.internal.codegen.utils.SSASimplificationContext;\r
+import org.simantics.scl.compiler.internal.codegen.utils.SSAValidationContext;\r
+import org.simantics.scl.compiler.internal.codegen.utils.ValRefVisitor;\r
+import org.simantics.scl.compiler.internal.codegen.writer.CodeWriter;\r
+import org.simantics.scl.compiler.internal.codegen.writer.ModuleWriter;\r
+import org.simantics.scl.compiler.types.TVar;\r
+import org.simantics.scl.compiler.types.Type;\r
+\r
+public class SSAObject extends SSAClosure implements ClosureBinder {\r
+    Type type;\r
+    SSAClosure firstClosure;\r
+    \r
+    public SSAObject(Type type) {\r
+        this.type = type;\r
+    }\r
+\r
+    @Override\r
+    public void toString(PrintingContext context) {\r
+        for(SSAClosure closure = firstClosure; closure != null; closure = closure.getNext()) {\r
+            context.indentation();\r
+            context.append(closure.getTarget());\r
+            context.append("(" + closure.getTarget().occurrenceCount() + ")");\r
+            context.append(" :: ");\r
+            context.append(closure.getTarget().getType());\r
+            context.append(" = \n");\r
+            context.indent();\r
+            closure.toString(context);\r
+            context.dedent();\r
+        }\r
+    }\r
+\r
+    @Override\r
+    public SSAClosure getFirstClosure() {\r
+        return firstClosure;\r
+    }\r
+    \r
+    @Override\r
+    public void setFirstClosure(SSAClosure function) {\r
+        this.firstClosure = function;     \r
+        if(function == null)\r
+            detach();\r
+    }\r
+    \r
+    @Override\r
+    public void destroy() {\r
+        for(SSAClosure closure = firstClosure; closure != null; closure = closure.getNext())\r
+            closure.destroy();\r
+    }\r
+\r
+    @Override\r
+    public SSAClosure copy(CopyContext context) {\r
+        SSAObject result = new SSAObject(context.copyType(type));\r
+        for(SSAClosure closure = firstClosure; closure != null; closure = closure.getNext()) {\r
+            SSAClosure newFunction = closure.copy(context);\r
+            newFunction.setTarget(context.copy(closure.getTarget()));\r
+            result.addClosure(newFunction);\r
+        }\r
+        return result;    \r
+    }\r
+\r
+    public void addClosure(SSAClosure closure) {\r
+        closure.setParent(this);        \r
+        closure.setNext(firstClosure);\r
+        if(firstClosure != null)\r
+            firstClosure.setPrev(closure);\r
+        firstClosure = closure;\r
+    }\r
+    \r
+    @Override\r
+    public void markGenerateOnFly() {\r
+        for(SSAClosure closure = firstClosure; closure != null; closure = closure.getNext())\r
+            closure.markGenerateOnFly();\r
+    }\r
+\r
+    @Override\r
+    public void replace(TVar[] vars, Type[] replacements) {\r
+        for(SSAClosure closure = firstClosure; closure != null; closure = closure.getNext())\r
+            closure.replace(vars, replacements);\r
+    }\r
+\r
+    @Override\r
+    public void collectFreeVariables(ArrayList<ValRef> freeVars) {\r
+        for(SSAClosure closure = firstClosure; closure != null; closure = closure.getNext())\r
+            closure.collectFreeVariables(freeVars);\r
+    }\r
+\r
+    @Override\r
+    public void simplify(SSASimplificationContext context) {\r
+        for(SSAClosure closure = firstClosure; closure != null; closure = closure.getNext())\r
+            closure.simplify(context);\r
+    }\r
+\r
+    @Override\r
+    public void validate(SSAValidationContext context) {\r
+        for(SSAClosure closure = firstClosure; closure != null; closure = closure.getNext())\r
+            closure.validate(context);\r
+    }\r
+\r
+    @Override\r
+    public void lambdaLift(SSALambdaLiftingContext context) {\r
+        for(SSAClosure closure = firstClosure; closure != null; closure = closure.getNext())\r
+            closure.lambdaLift(context);\r
+    }\r
+\r
+    @Override\r
+    public boolean isValue() {\r
+        return false;\r
+    }\r
+\r
+    @Override\r
+    public void parametrize(BoundVar[] parameters) {\r
+        // TODO Auto-generated method stub\r
+        \r
+    }\r
+\r
+    @Override\r
+    public Type getType() {\r
+        return type;\r
+    }\r
+    \r
+    public CodeWriter createMethod(ModuleWriter moduleWriter, TVar[] typeParameters, Type effect, Type returnType, Type[] parameterTypes) {\r
+        SSAFunction function = new SSAFunction(typeParameters, effect, returnType);\r
+        SSABlock block = new SSABlock(parameterTypes);\r
+        function.addBlock(block);\r
+        BoundVar target = new BoundVar(function.getType());\r
+        function.setTarget(target);\r
+        addClosure(function);\r
+        return new CodeWriter(moduleWriter, block);\r
+    }\r
+\r
+    @Override\r
+    public void forValRefs(ValRefVisitor visitor) {\r
+        for(SSAClosure closure = firstClosure; closure != null; closure = closure.getNext())\r
+            closure.forValRefs(visitor);\r
+    }\r
+\r
+}\r