X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=blobdiff_plain;f=bundles%2Forg.simantics.scl.compiler%2Fsrc%2Forg%2Fsimantics%2Fscl%2Fcompiler%2Finternal%2Fcodegen%2Fssa%2FSSAFunction.java;h=b2db131a41f50d6f55765d300ebd6d7cf42e30d1;hp=68dd82f15f4bb1c76d5b68c07f58cfc501e3f66c;hb=91682baa9a8252390f09b80fd724f47e5957b234;hpb=1b4d8b692f40d946deb5db8280eb4ca5b36a75a7 diff --git a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/internal/codegen/ssa/SSAFunction.java b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/internal/codegen/ssa/SSAFunction.java index 68dd82f15..b2db131a4 100644 --- a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/internal/codegen/ssa/SSAFunction.java +++ b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/internal/codegen/ssa/SSAFunction.java @@ -5,6 +5,7 @@ import java.util.Arrays; import org.cojen.classfile.TypeDesc; import org.simantics.scl.compiler.common.exceptions.InternalCompilerError; +import org.simantics.scl.compiler.constants.NoRepConstant; import org.simantics.scl.compiler.internal.codegen.continuations.Cont; import org.simantics.scl.compiler.internal.codegen.continuations.ContRef; import org.simantics.scl.compiler.internal.codegen.continuations.ReturnCont; @@ -25,8 +26,12 @@ import org.simantics.scl.compiler.internal.codegen.utils.ValRefVisitor; import org.simantics.scl.compiler.types.TVar; import org.simantics.scl.compiler.types.Type; import org.simantics.scl.compiler.types.Types; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public final class SSAFunction extends SSAClosure { + private static final Logger LOGGER = LoggerFactory.getLogger(SSAFunction.class); + TVar[] typeParameters; Type effect; SSABlock firstBlock; @@ -153,7 +158,7 @@ public final class SSAFunction extends SSAClosure { // Add valid variables and continuations context.validContinuations.add(returnCont); - for(SSABlock block = firstBlock; block != null; block = block.next) { + for(SSABlock block = firstBlock; block != null; block = block.next) { context.validContinuations.add(block); for(BoundVar parameter : block.parameters) context.validBoundVariables.add(parameter); @@ -173,12 +178,88 @@ public final class SSAFunction extends SSAClosure { public void simplify(SSASimplificationContext context) { for(SSABlock block = firstBlock; block != null; block = block.next) block.simplify(context); - if(firstBlock == lastBlock && firstBlock.firstStatement == firstBlock.lastStatement && - firstBlock.firstStatement instanceof LetFunctions) { - simplifySingleLambda(context); + if(firstBlock == lastBlock && firstBlock.firstStatement == firstBlock.lastStatement) { + if(firstBlock.firstStatement instanceof LetApply) + simplifySingleApply(context); + else if(firstBlock.firstStatement instanceof LetFunctions) + simplifySingleLambda(context); } } + + /** + * Simplifies the following kind of function definition + * \x -> f x + * to + * f + */ + private void simplifySingleApply(SSASimplificationContext context) { + if(!(parent instanceof LetFunctions) || parent.getFirstClosure().next != null) + return; + LetApply apply = (LetApply)firstBlock.firstStatement; + if(!(firstBlock.exit instanceof Jump)) + return; + Jump exit = (Jump)firstBlock.exit; + if(exit.getTarget().getBinding() != returnCont) + return; + if(exit.getParameter(0).getBinding() != apply.getTarget()) + return; + BoundVar[] functionParameters = getParameters(); + ValRef[] applyParameters = apply.getParameters(); + if(functionParameters.length > applyParameters.length) + return; + int extraApplyParameters = applyParameters.length - functionParameters.length; + for(int i=0;i 0) { + //System.out.println("-------------------------------------------------------------"); + //System.out.println(parentFunction); + //System.out.println("-------------------------------------------------------------"); + apply.setTarget((BoundVar)target); + apply.setParameters(Arrays.copyOf(applyParameters, extraApplyParameters)); + apply.insertBefore(binder); + binder.detach(); + //System.out.println(parentFunction); + //System.out.println("-------------------------------------------------------------"); + } + else { + binder.detach(); + ((BoundVar)target).replaceBy(apply.getFunction()); + } + context.markModified("SSAFunction.eta-reduce"); + } + + private boolean representSameValues(BoundVar boundVar, ValRef valRef) { + Val val = valRef.getBinding(); + if(val == boundVar && valRef.getTypeParameters().length == 0) + return true; + if(val instanceof NoRepConstant && Types.equals(valRef.getType(), boundVar.getType())) + return true; + return false; + } + + /** + * Simplifies the following kind of function definition + * \x -> \y -> e + * to + * \x y -> e + */ private void simplifySingleLambda(SSASimplificationContext context) { LetFunctions letF = (LetFunctions)firstBlock.firstStatement; if(!(letF.getFirstClosure() instanceof SSAFunction)) @@ -198,15 +279,15 @@ public final class SSAFunction extends SSAClosure { for(SSABlock block = f.firstBlock; block != null; block = block.next) block.parent = this; lastBlock.next = f.firstBlock; - f.firstBlock.prev = lastBlock; + f.firstBlock.prev = lastBlock; lastBlock = f.lastBlock; firstBlock.firstStatement = firstBlock.lastStatement = null; - setReturnCont(f.getReturnCont()); + setReturnCont(f.getReturnCont()); effect = f.effect; BoundVar[] newParameters = BoundVar.copy(f.firstBlock.parameters); firstBlock.setParameters(BoundVar.concat(getParameters(), newParameters)); - firstBlock.setExit(new Jump(f.firstBlock.createOccurrence(), ValRef.createOccurrences(newParameters))); + firstBlock.setExit(new Jump(-1, f.firstBlock.createOccurrence(), ValRef.createOccurrences(newParameters))); context.markModified("SSAFunction.simplify-simple-lambda"); } @@ -282,6 +363,8 @@ public final class SSAFunction extends SSAClosure { } public void mergeBlocks(SSAFunction function) { + if(this == function) + throw new InternalCompilerError(); SSABlock block = function.firstBlock; while(block != null) { SSABlock next = block.next; @@ -326,7 +409,7 @@ public final class SSAFunction extends SSAClosure { parameter.parent = firstBlock; } - public void apply(ValRef[] parameters) { + public void apply(int lineNumber, ValRef[] parameters) { if(parameters.length == 0) return; if(firstBlock.hasNoOccurences()) { @@ -338,7 +421,7 @@ public final class SSAFunction extends SSAClosure { else { BoundVar[] newVars = new BoundVar[getArity()-parameters.length]; SSABlock block = new SSABlock(newVars); - block.setExit(new Jump(firstBlock.createOccurrence(), + block.setExit(new Jump(lineNumber, firstBlock.createOccurrence(), ValRef.concat(ValRef.copy(parameters), ValRef.createOccurrences(newVars)))); addBlockInFront(block); } @@ -382,4 +465,9 @@ public final class SSAFunction extends SSAClosure { block.forValRefs(visitor); } + @Override + public void cleanup() { + for(SSABlock block = firstBlock; block != null; block = block.next) + block.cleanup(); + } }