Last active
August 4, 2017 16:14
-
-
Save arthurp/5d5a52ff2e4fb2ac532cf951391f0323 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.singingwizard.exception; | |
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary; | |
import com.oracle.truffle.api.RootCallTarget; | |
import com.oracle.truffle.api.Truffle; | |
import com.oracle.truffle.api.frame.VirtualFrame; | |
import com.oracle.truffle.api.nodes.RootNode; | |
import com.oracle.truffle.api.profiles.BranchProfile; | |
public class ExceptionOnOnlyPath { | |
static boolean shouldIThrow = false; | |
/** A simple exception which doesn't capture a stack. | |
*/ | |
final static class TestException extends RuntimeException { | |
@Override | |
public synchronized Throwable fillInStackTrace() { | |
return this; | |
} | |
} | |
///// A root node with an exception on the normal path, but throwsControlFlowException = false | |
// This case is optimized to an immediate deopt everytime it compiles, so it will repeatedly recompile. | |
// This case is shown in RootNode1_*.png. | |
@TruffleBoundary(allowInlining = false, throwsControlFlowException = false) | |
private static Object doAThing1() { | |
throw new TestException(); | |
} | |
private static RootNode createRootNode1() { | |
return new RootNode(null) { | |
@Override | |
public Object execute(VirtualFrame frame) { | |
try { | |
return doAThing1(); | |
} catch (RuntimeException e) { | |
return "Success"; | |
} | |
} | |
}; | |
} | |
///// A root node with an exception on the normal path, but throwsControlFlowException = false | |
// This case is optimized to an immediate deopt everytime it compiles, so it will repeatedly recompile. | |
// This case is shown in RootNode1.5_*.png. | |
@TruffleBoundary(allowInlining = false, throwsControlFlowException = true) | |
private static Object doAThing1_5() { | |
if (shouldIThrow) { | |
throw new TestException(); | |
} else { | |
return "Success 1"; | |
} | |
} | |
private static RootNode createRootNode1_5() { | |
return new RootNode(null) { | |
final BranchProfile profile = BranchProfile.create(); | |
@Override | |
public Object execute(VirtualFrame frame) { | |
try { | |
return doAThing1_5(); | |
} catch (RuntimeException e) { | |
profile.enter(); | |
return "Success 2"; | |
} | |
} | |
}; | |
} | |
///// A root node with an exception on the normal path, but throwsControlFlowException = true | |
// This case is optimized to as `return "Success"` since the exception handling is inlined instead of ignored. | |
// This case is shown in RootNode2_*.png. | |
@TruffleBoundary(allowInlining = false, throwsControlFlowException = true) | |
private static Object doAThing2() { | |
throw new TestException(); | |
} | |
private static RootNode createRootNode2() { | |
return new RootNode(null) { | |
@Override | |
public Object execute(VirtualFrame frame) { | |
try { | |
return doAThing2(); | |
} catch (RuntimeException e) { | |
return "Success"; | |
} | |
} | |
}; | |
} | |
///// A trivial test harness | |
private void runTest(RootNode n) { | |
RootCallTarget intTgt = Truffle.getRuntime().createCallTarget(n); | |
for (int i = 0; i < 10; i++) { | |
System.out.println(intTgt.call()); | |
} | |
} | |
public static void main(String[] args) { | |
ExceptionOnOnlyPath test = new ExceptionOnOnlyPath(); | |
System.out.println("throwsControlFlowException = false"); | |
test.runTest(createRootNode1()); | |
System.out.println("throwsControlFlowException = true, shouldIThrow = false"); | |
shouldIThrow = false; | |
test.runTest(createRootNode1_5()); | |
System.out.println("throwsControlFlowException = true, shouldIThrow = true"); | |
shouldIThrow = true; | |
test.runTest(createRootNode1_5()); | |
System.out.println("throwsControlFlowException = true"); | |
test.runTest(createRootNode2()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment