ExceptionRegion modeling issues and proposed improvements
Paul Sandoz
paul.sandoz at oracle.com
Wed Oct 9 17:59:04 UTC 2024
You found a bug with break/continue lowering! If you add finally to both try statements I believe it should be correct, so there is some logic missing.
I have a better sense now of what you are proposing with the updated example (still working my way through the emails). The current modeling approach preserves the structure when lowering models and relies on properties of the code model for structural correctness (use and dominence). But, IIUC when lifting from bytecode that structure can ill-formed and it is hard to recover.
In your modeling approach (ignoring multi-catch for now) I think it may be possible to refer directly to the catch blocks as successors for both exit and enter, thereby avoiding a level of indirection. I am trying to work through some of the rules of region enters and exits, how they relate to each other, and operations covered by regions. On key aspect I did not realize before is being able to partially exit a region - presumably there is a required order to such exits based on the order of related enters?
It would be interesting to expand on your example to include finally blocks e.g., (the lowered model has been transformed to remove redundant blocks an which I believe to be correct)
@CodeReflection
static void nestedCatch(int i) {
PrintStream out = System.out;
while (true) {
try {
try {
if (i == 0) {
break;
} else if (i == 1) {
continue;
}
} catch (NullPointerException npe) {
out.println("CATCH NPE");
} finally {
out.println("FINALLY INNER");
}
out.println("AFTER TRY INNER");
} catch (RuntimeException re) {
out.println("CATCH RE");
} finally {
out.println("FINALLY OUTER");
}
}
}
func @"nestedCatch" @loc="126:5:file:/Users/sandoz/Projects/jdk/test/babylon-test/src/main/java/T.java" (%0 : int)void -> {
%1 : Var<int> = var %0 @"i" @loc="126:5";
%2 : java.io.PrintStream = field.load @"java.lang.System::out()java.io.PrintStream" @loc="128:27";
%3 : Var<java.io.PrintStream> = var %2 @"out" @loc="128:9";
branch ^block_1;
^block_1:
%4 : boolean = constant @"true" @loc="129:16";
cbranch %4 ^block_2 ^block_21;
^block_2:
%5 : java.lang.reflect.code.op.CoreOp$ExceptionRegion = exception.region.enter ^block_3 ^block_17 ^block_20;
^block_3:
%6 : java.lang.reflect.code.op.CoreOp$ExceptionRegion = exception.region.enter ^block_4 ^block_13 ^block_16;
^block_4:
%7 : int = var.load %1 @loc="132:25";
%8 : int = constant @"0" @loc="132:30";
%9 : boolean = eq %7 %8 @loc="132:25";
cbranch %9 ^block_5 ^block_8;
^block_5:
exception.region.exit %6 ^block_6;
^block_6:
%10 : java.io.PrintStream = var.load %3 @loc="140:21";
%11 : java.lang.String = constant @"FINALLY INNER" @loc="140:33";
invoke %10 %11 @"java.io.PrintStream::println(java.lang.String)void" @loc="140:21";
exception.region.exit %5 ^block_7;
^block_7:
%12 : java.io.PrintStream = var.load %3 @loc="146:17";
%13 : java.lang.String = constant @"FINALLY OUTER" @loc="146:29";
invoke %12 %13 @"java.io.PrintStream::println(java.lang.String)void" @loc="146:17";
branch ^block_21;
^block_8:
%14 : int = var.load %1 @loc="134:32";
%15 : int = constant @"1" @loc="134:37";
%16 : boolean = eq %14 %15 @loc="134:32";
cbranch %16 ^block_9 ^block_12;
^block_9:
exception.region.exit %6 ^block_10;
^block_10:
%17 : java.io.PrintStream = var.load %3 @loc="140:21";
%18 : java.lang.String = constant @"FINALLY INNER" @loc="140:33";
invoke %17 %18 @"java.io.PrintStream::println(java.lang.String)void" @loc="140:21";
exception.region.exit %5 ^block_11;
^block_11:
%19 : java.io.PrintStream = var.load %3 @loc="146:17";
%20 : java.lang.String = constant @"FINALLY OUTER" @loc="146:29";
invoke %19 %20 @"java.io.PrintStream::println(java.lang.String)void" @loc="146:17";
branch ^block_1;
^block_12:
exception.region.exit %6 ^block_15;
^block_13(%21 : java.lang.NullPointerException):
%22 : java.lang.reflect.code.op.CoreOp$ExceptionRegion = exception.region.enter ^block_14 ^block_16;
^block_14:
%23 : Var<java.lang.NullPointerException> = var %21 @"npe" @loc="131:17";
%24 : java.io.PrintStream = var.load %3 @loc="138:21";
%25 : java.lang.String = constant @"CATCH NPE" @loc="138:33";
invoke %24 %25 @"java.io.PrintStream::println(java.lang.String)void" @loc="138:21";
exception.region.exit %22 ^block_15;
^block_15:
%26 : java.io.PrintStream = var.load %3 @loc="140:21";
%27 : java.lang.String = constant @"FINALLY INNER" @loc="140:33";
invoke %26 %27 @"java.io.PrintStream::println(java.lang.String)void" @loc="140:21";
%28 : java.io.PrintStream = var.load %3 @loc="142:17";
%29 : java.lang.String = constant @"AFTER TRY INNER" @loc="142:29";
invoke %28 %29 @"java.io.PrintStream::println(java.lang.String)void" @loc="142:17";
exception.region.exit %5 ^block_19;
^block_16(%30 : java.lang.Throwable):
%31 : java.io.PrintStream = var.load %3 @loc="140:21";
%32 : java.lang.String = constant @"FINALLY INNER" @loc="140:33";
invoke %31 %32 @"java.io.PrintStream::println(java.lang.String)void" @loc="140:21";
throw %30;
^block_17(%33 : java.lang.RuntimeException):
%34 : java.lang.reflect.code.op.CoreOp$ExceptionRegion = exception.region.enter ^block_18 ^block_20;
^block_18:
%35 : Var<java.lang.RuntimeException> = var %33 @"re" @loc="130:13";
%36 : java.io.PrintStream = var.load %3 @loc="144:17";
%37 : java.lang.String = constant @"CATCH RE" @loc="144:29";
invoke %36 %37 @"java.io.PrintStream::println(java.lang.String)void" @loc="144:17";
exception.region.exit %34 ^block_19;
^block_19:
%38 : java.io.PrintStream = var.load %3 @loc="146:17";
%39 : java.lang.String = constant @"FINALLY OUTER" @loc="146:29";
invoke %38 %39 @"java.io.PrintStream::println(java.lang.String)void" @loc="146:17";
branch ^block_1;
^block_20(%40 : java.lang.Throwable):
%41 : java.io.PrintStream = var.load %3 @loc="146:17";
%42 : java.lang.String = constant @"FINALLY OUTER" @loc="146:29";
invoke %41 %42 @"java.io.PrintStream::println(java.lang.String)void" @loc="146:17";
throw %40;
^block_21:
return @loc="126:5";
};
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/babylon-dev/attachments/20241009/0dfe2133/attachment-0001.htm>
More information about the babylon-dev
mailing list