]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/internal/codegen/writer/CodeWriter.java
Merged changes from feature/scl to master.
[simantics/platform.git] / bundles / org.simantics.scl.compiler / src / org / simantics / scl / compiler / internal / codegen / writer / CodeWriter.java
index 6bed9f0661fc854b86afa24738bc3eb6dc032f91..1125a4bde9f41c52f372adcd22c9c627dc6f9010 100644 (file)
@@ -10,6 +10,7 @@ import org.simantics.scl.compiler.internal.codegen.references.IVal;
 import org.simantics.scl.compiler.internal.codegen.references.ValRef;
 import org.simantics.scl.compiler.internal.codegen.ssa.SSABlock;
 import org.simantics.scl.compiler.internal.codegen.ssa.SSAFunction;
+import org.simantics.scl.compiler.internal.codegen.ssa.SSAObject;
 import org.simantics.scl.compiler.internal.codegen.ssa.exits.If;
 import org.simantics.scl.compiler.internal.codegen.ssa.exits.Jump;
 import org.simantics.scl.compiler.internal.codegen.ssa.exits.Switch;
@@ -28,12 +29,12 @@ public class CodeWriter {
     ModuleWriter moduleWriter;
     SSABlock block;
     
-    CodeWriter(ModuleWriter moduleWriter, SSABlock block) {
+    public CodeWriter(ModuleWriter moduleWriter, SSABlock block) {
         this.moduleWriter = moduleWriter;
         this.block = block;
     }
 
-    public IVal apply(int lineNumber, IVal function, IVal ... parameters) {
+    public IVal apply(long lineNumber, IVal function, IVal ... parameters) {
         try {
             MultiFunction mfun = Types.matchFunction(function.getType(), parameters.length);
             return applyWithEffect(lineNumber,
@@ -45,7 +46,7 @@ public class CodeWriter {
         }
     }
     
-    public IVal applyWithEffectChecked(int lineNumber, Type effect, Type returnType, IVal function, IVal ... parameters) {
+    public IVal applyWithEffectChecked(long lineNumber, Type effect, Type returnType, IVal function, IVal ... parameters) {
         try {
             MultiFunction mfun = Types.matchFunction(function.getType(), parameters.length);
             if(!Types.equals(effect, mfun.effect))
@@ -69,6 +70,12 @@ public class CodeWriter {
         return var;
     }
     
+    public CodeWriter createBlock() {
+        SSABlock newBlock = new SSABlock(Type.EMPTY_ARRAY);
+        block.getParent().addBlock(newBlock);
+        return new CodeWriter(moduleWriter, newBlock);
+    }
+    
     public CodeWriter createBlock(Type ... parameterTypes) {
         SSABlock newBlock = new SSABlock(parameterTypes);
         block.getParent().addBlock(newBlock);
@@ -120,6 +127,24 @@ public class CodeWriter {
                 elseTarget.createOccurrence()));
         block = null;
     }
+    
+    public void branchAwayIf(IVal condition, ICont target) {
+        SSABlock newBlock = new SSABlock(Type.EMPTY_ARRAY);
+        block.getParent().addBlock(newBlock);
+        block.setExit(new If(condition.createOccurrence(), 
+                target.createOccurrence(), 
+                newBlock.createOccurrence()));
+        this.block = newBlock;
+    }
+    
+    public void branchAwayUnless(IVal condition, ICont target) {
+        SSABlock newBlock = new SSABlock(Type.EMPTY_ARRAY);
+        block.getParent().addBlock(newBlock);
+        block.setExit(new If(condition.createOccurrence(), 
+                newBlock.createOccurrence(),
+                target.createOccurrence()));
+        this.block = newBlock;
+    }
 
     public void return_(IVal val) {
         jump(block.getParent().getReturnCont(), val);
@@ -143,5 +168,13 @@ public class CodeWriter {
 
     public SSAFunction getFunction() {
         return block.getParent();
-    }        
+    }
+
+    public boolean isUnfinished() {
+        return block != null;
+    }
+
+    public void defineObject(SSAObject object) {
+        this.block.addStatement(new LetFunctions(object));
+    }
 }