X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.scl.compiler%2Fsrc%2Forg%2Fsimantics%2Fscl%2Fcompiler%2Finternal%2Fcodegen%2Fwriter%2FCodeWriter.java;h=bb774bd24e798b10cbd3ce1d58cf4bdc8c85fb6a;hb=refs%2Fchanges%2F34%2F1534%2F3;hp=6bed9f0661fc854b86afa24738bc3eb6dc032f91;hpb=9a175feb652b2b7bba7afa540831b9076be3c10e;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/internal/codegen/writer/CodeWriter.java b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/internal/codegen/writer/CodeWriter.java index 6bed9f066..bb774bd24 100644 --- a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/internal/codegen/writer/CodeWriter.java +++ b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/internal/codegen/writer/CodeWriter.java @@ -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; @@ -10,6 +11,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,15 +30,26 @@ public class CodeWriter { ModuleWriter moduleWriter; SSABlock block; - CodeWriter(ModuleWriter moduleWriter, SSABlock block) { + public CodeWriter(ModuleWriter moduleWriter, SSABlock block) { 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(int 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); @@ -45,7 +58,7 @@ public class CodeWriter { } } - public IVal applyWithEffectChecked(int 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)) @@ -55,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) { @@ -64,11 +77,17 @@ public class CodeWriter { effect, function.createOccurrence(), ValRef.createOccurrences(parameters)); - apply.location = location; + apply.lineNumber = lineNumber(location); block.addStatement(apply); 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); @@ -108,31 +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(long location, IVal condition, ICont target) { + SSABlock newBlock = new SSABlock(Type.EMPTY_ARRAY); + block.getParent().addBlock(newBlock); + block.setExit(new If( + lineNumber(location), + condition.createOccurrence(), + target.createOccurrence(), + newBlock.createOccurrence())); + this.block = newBlock; + } + + public void branchAwayUnless(long location, IVal condition, ICont target) { + SSABlock newBlock = new SSABlock(Type.EMPTY_ARRAY); + block.getParent().addBlock(newBlock); + 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; } @@ -143,5 +187,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)); + } }