]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/internal/codegen/writer/CodeWriter.java
SCL compiler generates line numbers to bytecode
[simantics/platform.git] / bundles / org.simantics.scl.compiler / src / org / simantics / scl / compiler / internal / codegen / writer / CodeWriter.java
index 1125a4bde9f41c52f372adcd22c9c627dc6f9010..bb774bd24e798b10cbd3ce1d58cf4bdc8c85fb6a 100644 (file)
@@ -2,6 +2,7 @@ package org.simantics.scl.compiler.internal.codegen.writer;
 
 import org.cojen.classfile.TypeDesc;
 import org.simantics.scl.compiler.common.exceptions.InternalCompilerError;
+import org.simantics.scl.compiler.errors.Locations;
 import org.simantics.scl.compiler.internal.codegen.continuations.Branch;
 import org.simantics.scl.compiler.internal.codegen.continuations.BranchRef;
 import org.simantics.scl.compiler.internal.codegen.continuations.ICont;
@@ -33,11 +34,22 @@ public class CodeWriter {
         this.moduleWriter = moduleWriter;
         this.block = block;
     }
+    
+    private int lineNumber(long location) {
+        if(location == Locations.NO_LOCATION)
+            return -1;
+        else {
+            int position = Locations.beginOf(location);
+            int line = moduleWriter.lineLocator.lineNumberFromPosition(position);
+            //System.out.println("location=" + location + ", position=" + position + ", line=" + line);
+            return line + 1;
+        }
+    }
 
-    public IVal apply(long lineNumber, IVal function, IVal ... parameters) {
+    public IVal apply(long location, IVal function, IVal ... parameters) {
         try {
             MultiFunction mfun = Types.matchFunction(function.getType(), parameters.length);
-            return applyWithEffect(lineNumber,
+            return applyWithEffect(location,
                     mfun.effect,
                     mfun.returnType,
                     function, parameters);
@@ -46,7 +58,7 @@ public class CodeWriter {
         }
     }
     
-    public IVal applyWithEffectChecked(long lineNumber, Type effect, Type returnType, IVal function, IVal ... parameters) {
+    public IVal applyWithEffectChecked(long location, Type effect, Type returnType, IVal function, IVal ... parameters) {
         try {
             MultiFunction mfun = Types.matchFunction(function.getType(), parameters.length);
             if(!Types.equals(effect, mfun.effect))
@@ -56,7 +68,7 @@ public class CodeWriter {
         } catch (MatchException e) {
             throw new InternalCompilerError(e);
         }            
-        return applyWithEffect(lineNumber, effect, returnType, function, parameters);
+        return applyWithEffect(location, effect, returnType, function, parameters);
     }
     
     public IVal applyWithEffect(long location, Type effect, Type returnType, IVal function, IVal ... parameters) {
@@ -65,7 +77,7 @@ public class CodeWriter {
                 effect,
                 function.createOccurrence(), 
                 ValRef.createOccurrences(parameters));
-        apply.location = location;
+        apply.lineNumber = lineNumber(location);
         block.addStatement(apply);
         return var;
     }
@@ -115,49 +127,56 @@ public class CodeWriter {
         return block;
     }
     
-    public void jump(ICont cont, IVal ... parameters) {
-        block.setExit(new Jump(cont.createOccurrence(), 
+    public void jump(long location, ICont cont, IVal ... parameters) {
+        block.setExit(new Jump(
+                lineNumber(location),
+                cont.createOccurrence(), 
                 ValRef.createOccurrences(parameters)));
         block = null;
     }
     
-    public void if_(IVal condition, ICont thenTarget, ICont elseTarget) {
-        block.setExit(new If(condition.createOccurrence(), 
+    public void if_(long location, IVal condition, ICont thenTarget, ICont elseTarget) {
+        block.setExit(new If(
+                lineNumber(location),
+                condition.createOccurrence(), 
                 thenTarget.createOccurrence(), 
                 elseTarget.createOccurrence()));
         block = null;
     }
     
-    public void branchAwayIf(IVal condition, ICont target) {
+    public void branchAwayIf(long location, IVal condition, ICont target) {
         SSABlock newBlock = new SSABlock(Type.EMPTY_ARRAY);
         block.getParent().addBlock(newBlock);
-        block.setExit(new If(condition.createOccurrence(), 
+        block.setExit(new If(
+                lineNumber(location),
+                condition.createOccurrence(), 
                 target.createOccurrence(), 
                 newBlock.createOccurrence()));
         this.block = newBlock;
     }
     
-    public void branchAwayUnless(IVal condition, ICont target) {
+    public void branchAwayUnless(long location, IVal condition, ICont target) {
         SSABlock newBlock = new SSABlock(Type.EMPTY_ARRAY);
         block.getParent().addBlock(newBlock);
-        block.setExit(new If(condition.createOccurrence(), 
+        block.setExit(new If(
+                lineNumber(location),
+                condition.createOccurrence(), 
                 newBlock.createOccurrence(),
                 target.createOccurrence()));
         this.block = newBlock;
     }
 
-    public void return_(IVal val) {
-        jump(block.getParent().getReturnCont(), val);
+    public void return_(long location, IVal val) {
+        jump(lineNumber(location), block.getParent().getReturnCont(), val);
     }
 
-    public void switch_(IVal val, Branch[] branches) {
-        block.setExit(new Switch(val.createOccurrence(), BranchRef.toBranchRefs(branches)));
+    public void switch_(long location, IVal val, Branch[] branches) {
+        block.setExit(new Switch(lineNumber(location), val.createOccurrence(), BranchRef.toBranchRefs(branches)));
         block = null;
     }
 
     public void throw_(long location, TypeDesc exceptionClass, String description) {
-        Throw exit = new Throw(exceptionClass, description);
-        exit.location = location;
+        Throw exit = new Throw(lineNumber(location), exceptionClass, description);
         block.setExit(exit);
         block = null;
     }