<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body style="overflow-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;">
There is a definite tension here. I really want to lean into the structure of exception handling semantics of the language. But, this is not enforced at bytecode, it’s just a table constructed in the required order to preserve those semantics.
<div><br>
</div>
<div><a href="https://docs.oracle.com/javase/specs/jvms/se23/html/jvms-3.html#jvms-3.12">https://docs.oracle.com/javase/specs/jvms/se23/html/jvms-3.html#jvms-3.12</a></div>
<div><br>
</div>
<div>"The nesting of catch clauses is represented only in the exception table. The Java Virtual Machine does not enforce nesting of or any ordering of the exception table entries (§2.10). However, because try-catch constructs are structured, a compiler can
always order the entries of the exception handler table such that, for any thrown exception and any program counter value in that method, the first exception handler that matches the thrown exception corresponds to the innermost matching catch clause."<br>
<div><br>
</div>
<div>(Thank goodness the jsr and ret instructions were removed!)</div>
<div><br>
</div>
<div>It is very tempting to reject lifting the bytecode of a method for which its exception table is structurally ill-formed (we get to decide what that is). </div>
<div><br>
</div>
<div>
<div>
<div><br>
</div>
<div>
<div>
<blockquote type="cite">
<div>On Oct 10, 2024, at 4:45 AM, Adam Sotona <adam.sotona@oracle.com> wrote:</div>
<br class="Apple-interchange-newline">
<div>
<div class="WordSection1" style="page: WordSection1; caret-color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;">
<div style="margin: 0in; font-size: 12pt; font-family: Aptos, sans-serif;"><span style="font-size: 11pt;">So good news, after a brief scan I haven't found a single method in the JDK which would violate the LIFO of the exception region entries and exits.<o:p></o:p></span></div>
<div style="margin: 0in; font-size: 12pt; font-family: Aptos, sans-serif;"><span style="font-size: 11pt;"></span></div>
</div>
</div>
</blockquote>
<div><br>
</div>
Good! In hindsight perhaps not so surprising given the Java language’s exception handling semantics. I don’t know what kind of exception tables Scala, Closure or Kotlin might generate.</div>
<div><br>
</div>
<div><br>
<blockquote type="cite">
<div>
<div class="WordSection1" style="page: WordSection1; caret-color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;">
<div style="margin: 0in; font-size: 12pt; font-family: Aptos, sans-serif;"><span style="font-size: 11pt;"><o:p> </o:p></span></div>
<div style="margin: 0in; font-size: 12pt; font-family: Aptos, sans-serif;"><span style="font-size: 11pt;">FYI: I've also counted the max exception stack depth and number of gaps.<o:p></o:p></span></div>
<div style="margin: 0in; font-size: 12pt; font-family: Aptos, sans-serif;"><span style="font-size: 11pt;">Here is a frequency table of the max exception stack depths:<o:p></o:p></span></div>
<div style="margin: 0in; font-size: 12pt; font-family: Aptos, sans-serif;"><span style="font-size: 11pt;">0: 205345 // methods with no exception regions<o:p></o:p></span></div>
<div style="margin: 0in; font-size: 12pt; font-family: Aptos, sans-serif;"><span style="font-size: 11pt;">1: 11392<span class="Apple-converted-space"> </span><o:p></o:p></span></div>
<div style="margin: 0in; font-size: 12pt; font-family: Aptos, sans-serif;"><span style="font-size: 11pt;">2: 1866<o:p></o:p></span></div>
<div style="margin: 0in; font-size: 12pt; font-family: Aptos, sans-serif;"><span style="font-size: 11pt;">3: 509<o:p></o:p></span></div>
<div style="margin: 0in; font-size: 12pt; font-family: Aptos, sans-serif;"><span style="font-size: 11pt;">4: 205<o:p></o:p></span></div>
<div style="margin: 0in; font-size: 12pt; font-family: Aptos, sans-serif;"><span style="font-size: 11pt;">5: 61<o:p></o:p></span></div>
<div style="margin: 0in; font-size: 12pt; font-family: Aptos, sans-serif;"><span style="font-size: 11pt;">6: 23<o:p></o:p></span></div>
<div style="margin: 0in; font-size: 12pt; font-family: Aptos, sans-serif;"><span style="font-size: 11pt;">7: 16<o:p></o:p></span></div>
<div style="margin: 0in; font-size: 12pt; font-family: Aptos, sans-serif;"><span style="font-size: 11pt;">8: 10<o:p></o:p></span></div>
<div style="margin: 0in; font-size: 12pt; font-family: Aptos, sans-serif;"><span style="font-size: 11pt;">9: 1<o:p></o:p></span></div>
<div style="margin: 0in; font-size: 12pt; font-family: Aptos, sans-serif;"><span style="font-size: 11pt;">10: 3<o:p></o:p></span></div>
<div style="margin: 0in; font-size: 12pt; font-family: Aptos, sans-serif;"><span style="font-size: 11pt;">Across the JDK I found 9792 gaps in the exception regions (split exception regions).<o:p></o:p></span></div>
<div style="margin: 0in; font-size: 12pt; font-family: Aptos, sans-serif;"><span style="font-size: 11pt;">It would be interesting to see a model of the code with 10 layers of try/catch blocks ;)<o:p></o:p></span></div>
<div style="margin: 0in; font-size: 12pt; font-family: Aptos, sans-serif;"><span style="font-size: 11pt;"></span></div>
</div>
</div>
</blockquote>
<div><br>
</div>
Ha! I also wonder what Java source looks like?</div>
<div><br>
</div>
<div>Paul.</div>
<div><br>
</div>
<div>
<blockquote type="cite">
<div>
<div class="WordSection1" style="page: WordSection1; caret-color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;">
<div style="margin: 0in; font-size: 12pt; font-family: Aptos, sans-serif;"><span style="font-size: 11pt;"><o:p> </o:p></span></div>
<div style="margin: 0in; font-size: 12pt; font-family: Aptos, sans-serif;"><span style="font-size: 11pt;">Adam<o:p></o:p></span></div>
<div style="margin: 0in; font-size: 12pt; font-family: Aptos, sans-serif;"><span style="font-size: 11pt;"><o:p> </o:p></span></div>
<div style="margin: 0in; font-size: 12pt; font-family: Aptos, sans-serif;"><span style="font-size: 11pt;"><o:p> </o:p></span></div>
<div style="margin: 0in; font-size: 12pt; font-family: Aptos, sans-serif;"><span style="font-size: 11pt;"><o:p> </o:p></span></div>
<div id="mail-editor-reference-message-container">
<div>
<div>
<div style="border-width: 1pt medium medium; border-style: solid none none; border-color: rgb(181, 196, 223) currentcolor currentcolor; border-image: none; padding: 3pt 0in 0in;">
<p class="MsoNormal" style="margin: 0in 0in 12pt; font-size: 12pt; font-family: Aptos, sans-serif;">
<b><span style="">From:<span class="Apple-converted-space"> </span></span></b><span style="">babylon-dev <<a href="mailto:babylon-dev-retn@openjdk.org">babylon-dev-retn@openjdk.org</a>> on behalf of Adam Sotona <<a href="mailto:adam.sotona@oracle.com">adam.sotona@oracle.com</a>><br>
<b>Date:<span class="Apple-converted-space"> </span></b>Thursday, 10 October 2024 at 10:04<br>
<b>To:<span class="Apple-converted-space"> </span></b>Paul Sandoz <<a href="mailto:paul.sandoz@oracle.com">paul.sandoz@oracle.com</a>><br>
<b>Cc:<span class="Apple-converted-space"> </span></b><a href="mailto:babylon-dev@openjdk.org">babylon-dev@openjdk.org</a><span class="Apple-converted-space"> </span><<a href="mailto:babylon-dev@openjdk.org">babylon-dev@openjdk.org</a>><br>
<b>Subject:<span class="Apple-converted-space"> </span></b>Re: ExceptionRegion modeling issues and proposed improvements<o:p></o:p></span></p>
</div>
<div>
<div style="margin: 0in; font-size: 12pt; font-family: Aptos, sans-serif;"><span style="font-size: 11pt;"> </span><o:p></o:p></div>
<div style="margin: 0in; font-size: 12pt; font-family: Aptos, sans-serif;"><span style="font-size: 11pt;"> </span><o:p></o:p></div>
<div id="mail-editor-reference-message-container">
<div>
<div>
<div style="border-width: 1pt medium medium; border-style: solid none none; border-color: rgb(181, 196, 223) currentcolor currentcolor; border-image: none; padding: 3pt 0in 0in;">
<p class="MsoNormal" style="margin: 0in 0in 12pt; font-size: 12pt; font-family: Aptos, sans-serif;">
<b><span style="">From:<span class="Apple-converted-space"> </span></span></b><span style="">Paul Sandoz <<a href="mailto:paul.sandoz@oracle.com">paul.sandoz@oracle.com</a>></span><o:p></o:p></p>
</div>
<div>
<div>
<div>
<div style="margin: 0in; font-size: 12pt; font-family: Aptos, sans-serif;">> Is the catch block stack order important? Can we encounter an exception region exit that refers to B when the top of the stack is C? <o:p></o:p></div>
</div>
</div>
<div>
<div style="margin: 0in; font-size: 12pt; font-family: Aptos, sans-serif;"> <o:p></o:p></div>
</div>
<div style="margin: 0in; font-size: 12pt; font-family: Aptos, sans-serif;">Intuitively I would say the order should be preserved and my brain model for that is a lifo stack, however that idea is inferred from JLS.<o:p></o:p></div>
<div style="margin: 0in; font-size: 12pt; font-family: Aptos, sans-serif;">Practically there is nothing preventing to have a bytecode with exception table entries starting and ending independently of each other.<o:p></o:p></div>
<div style="margin: 0in; font-size: 12pt; font-family: Aptos, sans-serif;">Absolute order of the exception table entries is critical, however the whole exception region (a section of bytecode handling specific exception by a specific handler) can consist of
multiple exception table entries and can (theoretically) appear in a different relative order to other regions in different situations.<o:p></o:p></div>
<div style="margin: 0in; font-size: 12pt; font-family: Aptos, sans-serif;"><span style="font-size: 11pt;">I'll scan some code to collect all real situations versus what is theoretically possible. I think I've seen gaps (instructions exclusions) of a top level
try block, while the nested was uninterrupted (however need to find it to confirm).</span><o:p></o:p></div>
<div style="margin: 0in; font-size: 12pt; font-family: Aptos, sans-serif;"><span style="font-size: 11pt;"> </span><o:p></o:p></div>
<div style="margin: 0in; font-size: 12pt; font-family: Aptos, sans-serif;"><span style="font-size: 11pt;">Adam</span></div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</blockquote>
</div>
<br>
</div>
</div>
</div>
</div>
</body>
</html>