+++ /dev/null
-package org.simantics.scl.compiler.constants;
-
-import org.cojen.classfile.TypeDesc;
-import org.simantics.scl.compiler.internal.codegen.references.Val;
-import org.simantics.scl.compiler.internal.codegen.utils.MethodBuilder;
-import org.simantics.scl.compiler.types.TVar;
-import org.simantics.scl.compiler.types.Type;
-import org.simantics.scl.compiler.types.Types;
-import org.simantics.scl.compiler.types.kinds.Kinds;
-
-public class FailFunction extends FunctionValue {
-
- private static final TVar A = Types.var(Kinds.STAR);
- public static final FailFunction INSTANCE =
- new FailFunction();
-
- private FailFunction() {
- super(new TVar[] {A}, Types.NO_EFFECTS, A, Types.STRING);
- }
-
- private static final TypeDesc RuntimeException =
- TypeDesc.forClass(RuntimeException.class);
-
- @Override
- public Type applyExact(MethodBuilder mb, Val[] parameters) {
- mb.newObject(RuntimeException);
- mb.dup();
- mb.push(parameters, parameterTypes);
- mb.invokeConstructor(RuntimeException,
- new TypeDesc[] {TypeDesc.STRING});
- mb.throwObject();
- return getReturnType();
- }
-
- @Override
- public String toString() {
- return "fail";
- }
-}
--- /dev/null
+package org.simantics.scl.compiler.constants.singletons;
+
+import org.simantics.scl.compiler.constants.FunctionValue;
+import org.simantics.scl.compiler.internal.codegen.references.Val;
+import org.simantics.scl.compiler.internal.codegen.ssa.SSABlock;
+import org.simantics.scl.compiler.internal.codegen.ssa.SSAStatement;
+import org.simantics.scl.compiler.internal.codegen.ssa.exits.Throw2;
+import org.simantics.scl.compiler.internal.codegen.ssa.statements.LetApply;
+import org.simantics.scl.compiler.internal.codegen.utils.MethodBuilder;
+import org.simantics.scl.compiler.internal.codegen.utils.SSASimplificationContext;
+import org.simantics.scl.compiler.types.TVar;
+import org.simantics.scl.compiler.types.Type;
+import org.simantics.scl.compiler.types.Types;
+import org.simantics.scl.compiler.types.kinds.Kinds;
+
+public class ThrowFunction extends FunctionValue {
+
+ private static final TVar A = Types.var(Kinds.STAR);
+ private static final TVar B = Types.var(Kinds.STAR);
+ public static final ThrowFunction INSTANCE =
+ new ThrowFunction();
+
+ private ThrowFunction() {
+ super(new TVar[] {A, B}, Types.PROC, B, A);
+ }
+
+ @Override
+ public Type applyExact(MethodBuilder mb, Val[] parameters) {
+ mb.pushBoxed(parameters[0]);
+ mb.throwObject();
+ return getReturnType();
+ }
+
+ @Override
+ public String toString() {
+ return "throw";
+ }
+
+ @Override
+ public void inline(SSASimplificationContext context, LetApply apply) {
+ if(apply.getParameters().length != 1)
+ return; // shouldn't be possible
+
+ SSABlock block = apply.getParent();
+ for(SSAStatement statement=apply.getNext();statement != null;statement=statement.getNext())
+ statement.destroy();
+ apply.getFunction().remove();
+ apply.detachThisAndSuccessors();
+ block.getExit().destroy();
+ block.setExit(new Throw2(apply.getParameters()[0]));
+ }
+}
import org.simantics.scl.compiler.constants.singletons.FailFunction;
import org.simantics.scl.compiler.constants.singletons.JustConstant;
import org.simantics.scl.compiler.constants.singletons.NothingConstant;
+import org.simantics.scl.compiler.constants.singletons.ThrowFunction;
import org.simantics.scl.compiler.constants.singletons.TypeOfConstant;
import org.simantics.scl.compiler.constants.singletons.TypeOfProxyConstant;
import org.simantics.scl.compiler.constants.singletons.TypeProxyConstant;
addValue("fail", FailFunction.INSTANCE).documentation =
"Throws a runtime exeception with the given string as a description.";
+ addValue("throw", ThrowFunction.INSTANCE).documentation =
+ "Throws a given exception.";
// *** runProc ***
public abstract void forValRefs(ValRefVisitor visitor);
public abstract void cleanup();
+
+ public void detachThisAndSuccessors() {
+ parent.lastStatement = prev;
+ if(prev == null)
+ parent.firstStatement = null;
+ else
+ prev.next = null;
+ }
}
--- /dev/null
+package org.simantics.scl.compiler.internal.codegen.ssa.exits;
+
+import java.util.ArrayList;
+
+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.references.Val;
+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.SSAExit;
+import org.simantics.scl.compiler.internal.codegen.ssa.SSAFunction;
+import org.simantics.scl.compiler.internal.codegen.utils.CopyContext;
+import org.simantics.scl.compiler.internal.codegen.utils.MethodBuilder;
+import org.simantics.scl.compiler.internal.codegen.utils.PrintingContext;
+import org.simantics.scl.compiler.internal.codegen.utils.SSAValidationContext;
+import org.simantics.scl.compiler.internal.codegen.utils.ValRefVisitor;
+import org.simantics.scl.compiler.types.TVar;
+import org.simantics.scl.compiler.types.Type;
+
+public class Throw2 extends SSAExit {
+
+ ValRef exception;
+
+ public Throw2(ValRef exception) {
+ this.exception = exception;
+ }
+
+ @Override
+ public void toString(PrintingContext context) {
+ context.append("throw ");
+ context.append(exception);
+ context.append('\n');
+ }
+
+ @Override
+ public void generateCode(MethodBuilder mb) {
+ mb.pushBoxed(exception.getBinding());
+ mb.throwObject();
+ }
+
+ @Override
+ public void validate(SSAValidationContext context) {
+ }
+
+ @Override
+ public void destroy() {
+ exception.remove();
+ }
+
+ @Override
+ public SSAExit copy(CopyContext context) {
+ return new Throw2(exception.copy());
+ }
+
+ @Override
+ public void replace(TVar[] vars, Type[] replacements) {
+ }
+
+ @Override
+ public void collectFreeVariables(SSAFunction function,
+ ArrayList<ValRef> vars) {
+ }
+
+ @Override
+ public Cont addParametersInFrontOf(ContRef contRef, Val[] newParameters, Val[] oldParameters,
+ Cont proxy) {
+ return proxy;
+ }
+
+ @Override
+ public SSABlock[] getSuccessors() {
+ return SSABlock.EMPTY_ARRAY;
+ }
+
+ @Override
+ public void forValRefs(ValRefVisitor visitor) {
+ }
+
+ @Override
+ public void cleanup() {
+ }
+}
@Override
public void addBoundVariablesTo(SSAValidationContext context) {
- context.validBoundVariables.add(target);
+ context.validBoundVariables.add(target);
}
}
@Test public void StringMatching1() { test(); }
@Test public void SumOfInverses2() { test(); }
@Test public void SwitchSimplification() { test(); }
+ @Test public void Throw1() { test(); }
@Test public void TooManyParametersToSin() { test(); }
@Test public void Transformation1() { test(); }
@Test public void Transformation2() { test(); }
--- /dev/null
+import "Prelude"
+
+importJava "java.lang.Exception" where
+ @JavaName "<init>"
+ createException :: String -> <Proc> Exception
+
+main = throw (createException "Foo") `catch` \(e :: Exception) -> print e
+--
+java.lang.Exception: Foo
+()
\ No newline at end of file