<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;">
<br>
<div><br>
<blockquote type="cite">
<div>On May 19, 2023, at 7:07 AM, Brian Goetz <brian.goetz@oracle.com> wrote:</div>
<br class="Apple-interchange-newline">
<div><font size="4" style="caret-color: rgb(0, 0, 0); font-family: Helvetica; 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;"><font face="monospace">As
 I mentioned to Chen Liang, I would really like to place a moratorium on "new convenience methods" for a while, and I'd like to stop doing them one at a time.  Instead, let's gather a list of areas that potentially need improvement, and come back in a more
 organized way for a subsequent preview. <span class="Apple-converted-space"> </span><br>
<br>
I have no objection to eventually improving the API for both switches and try, but both of these areas have some risk in them, so I don't want to treat these as the same sort of convenience as `aload_0`.  Switches have messy scoping (it's all one big scope,
 which means we don't create a new locals context the way we do with if/else) and control flow (break, continue). <span class="Apple-converted-space"> </span><br>
<br>
We have an existing trying() construct but it doesn't handle finally.  Having two constructs that look like they correspond to the same language construct may be confusing. (Also, I'm not convinced that our trying() builder handles local variable slot allocation
 correctly either -- I think the try block does, but I am pretty sure the cache handler doesn't.  But we don't have tests that cover the interaction of the various convenience builders with local allocation. <span class="Apple-converted-space"> </span><br>
<br>
</font></font></div>
</blockquote>
<div><br>
</div>
Also, it can get complex for nested try with finally, where we need to keep track of the layering and know where exit points “break”/“continue” to within that layering so as to know what finalizer code to inline in the required order. It’s probably the most
 complex lowering step that the java compiler does.</div>
<div><br>
</div>
<div>Paul.</div>
<div><br>
</div>
<div><br>
<blockquote type="cite">
<div><font size="4" style="caret-color: rgb(0, 0, 0); font-family: Helvetica; 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;"><font face="monospace">So
 let's put this on the list of things that need improvement, and meanwhile, continue to shore up basics.<br>
</font></font><br style="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 class="moz-cite-prefix" style="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;">
On 5/19/2023 5:12 AM, Adam Sotona wrote:<br>
</div>
<blockquote type="cite" cite="mid:CY4PR1001MB215075E7472EA7609A60073F8C7C9@CY4PR1001MB2150.namprd10.prod.outlook.com" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;">
<div class="WordSection1" style="page: WordSection1;">
<div style="margin: 0cm; font-size: 11pt; font-family: Calibri, sans-serif;"><span lang="EN-US">Hi,<o:p></o:p></span></div>
<p style="margin-right: 0cm; margin-bottom: 12pt; margin-left: 0cm;"><span style="font-size: 10.5pt;">Classfile API provides convenient methods for if/then/else or try/catch, however similar methods to build switch or try/finally are missing</span><span lang="EN-US" style="font-size: 10.5pt;">.<o:p></o:p></span></p>
<div style="margin: 0cm; box-sizing: border-box; caret-color: rgb(31, 35, 40); font-variant-caps: normal; text-align: start; -webkit-text-stroke-width: 0px; word-spacing: 0px;">
<span style="font-size: 10.5pt;">Here I would like to propose new</span><span class="apple-converted-space"><span style="font-size: 10.5pt;"> </span></span><code style="font-family: "Courier New";"><span style="font-size: 9pt; font-family: Menlo; color: rgb(31, 35, 40);">CodeBuilder</span></code><span class="apple-converted-space"><span style="font-size: 10.5pt;"> </span></span><span style="font-size: 10.5pt;">conveniences:<br style="box-sizing: border-box;">
<br>
</span><code style="font-family: "Courier New";"><span style="font-size: 9pt; font-family: Menlo;"><o:p></o:p></span></code></div>
<ul type="disc" style="margin-bottom: 0cm; margin-top: 0cm;">
<li class="MsoNormal" style="margin: 0cm; font-size: 11pt; font-family: Calibri, sans-serif; color: rgb(31, 35, 40);">
<code style="font-family: "Courier New";"><span style="font-size: 9pt; font-family: Menlo;">lookupswitch(Consumer<SwitchBuilder> switchHandler)<o:p></o:p></span></code></li><li class="MsoNormal" style="margin: 0cm; font-size: 11pt; font-family: Calibri, sans-serif; color: rgb(31, 35, 40);">
<code style="font-family: "Courier New";"><span style="font-size: 9pt; font-family: Menlo;">tableswitch(Consumer<SwitchBuilder> switchHandler)<o:p></o:p></span></code></li><li class="MsoNormal" style="margin: 0cm; font-size: 11pt; font-family: Calibri, sans-serif; color: rgb(31, 35, 40);">
<code style="font-family: "Courier New";"><span style="font-size: 9pt; font-family: Menlo;">tryWithFinalizer(Consumer<CodeBuilder> tryHandler, Consumer<CodeBuilder> finalizerHandler, Label... externalLabels)<o:p></o:p></span></code></li><li class="MsoNormal" style="margin: 0cm; font-size: 11pt; font-family: Calibri, sans-serif;">
<code style="font-family: "Courier New";"><span style="font-size: 9pt; font-family: Menlo; color: rgb(31, 35, 40);">tryWithFinalizer(Consumer<CodeBuilder> tryHandler, Consumer<CodeBuilder> finalizerHandler, boolean compactForm, Label... externalLabels)</span></code><span style="font-size: 10.5pt;"><o:p></o:p></span></li></ul>
<div style="margin: 0cm; box-sizing: border-box; caret-color: rgb(31, 35, 40); font-variant-caps: normal; text-align: start; -webkit-text-stroke-width: 0px; word-spacing: 0px;">
<span style="font-size: 10.5pt;"><o:p> </o:p></span></div>
<div style="margin: 0cm;"><span style="font-size: 10.5pt;">and new</span><span class="apple-converted-space"><span style="font-size: 10.5pt;"> </span></span><code style="font-family: "Courier New";"><span style="font-size: 9pt; font-family: Menlo; color: rgb(31, 35, 40);">CodeBuilder.SwitchBuilder</span></code><span class="apple-converted-space"><span style="font-size: 10.5pt;"> </span></span><span style="font-size: 10.5pt;">interface
 with methods:<br style="box-sizing: border-box;">
<br>
</span><code style="font-family: "Courier New";"><span style="font-size: 9pt; font-family: Menlo;"><o:p></o:p></span></code></div>
<ul type="disc" style="margin-bottom: 0cm; margin-top: 0cm;">
<li class="MsoNormal" style="margin: 0cm; font-size: 11pt; font-family: Calibri, sans-serif; color: rgb(31, 35, 40);">
<code style="font-family: "Courier New";"><span style="font-size: 9pt; font-family: Menlo;">switchCase(int caseValue, Consumer<BlockCodeBuilder> caseHandler)<o:p></o:p></span></code></li><li class="MsoNormal" style="margin: 0cm; font-size: 11pt; font-family: Calibri, sans-serif; color: rgb(31, 35, 40);">
<code style="font-family: "Courier New";"><span style="font-size: 9pt; font-family: Menlo;">switchCase(List<Integer> caseValues, Consumer<BlockCodeBuilder> caseHandler)<o:p></o:p></span></code></li><li class="MsoNormal" style="margin: 0cm; font-size: 11pt; font-family: Calibri, sans-serif;">
<code style="font-family: "Courier New";"><span style="font-size: 9pt; font-family: Menlo; color: rgb(31, 35, 40);">defaultCase(Consumer<BlockCodeBuilder> defaultHandler)</span></code><span style="font-size: 10.5pt;"><o:p></o:p></span></li></ul>
<div style="margin: 0cm; font-size: 11pt; font-family: Calibri, sans-serif;"><span lang="EN-US"><o:p> </o:p></span></div>
<div style="margin: 0cm; font-size: 11pt; font-family: Calibri, sans-serif;"><span lang="EN-US">Sample use case (more can be seen in the tests at<span class="Apple-converted-space"> </span><a href="https://github.com/openjdk/jdk/pull/14056/files" moz-do-not-send="true" class="moz-txt-link-freetext" style="color: rgb(5, 99, 193); text-decoration: underline;">https://github.com/openjdk/jdk/pull/14056/files</a>):<o:p></o:p></span></div>
<div style="margin: 0cm; font-size: 11pt; font-family: Calibri, sans-serif; background: white;">
<span lang="EN-US" style="font-size: 10pt;"><o:p> </o:p></span></div>
<div style="margin: 0cm; font-size: 11pt; font-family: Calibri, sans-serif; background: white;">
<span lang="EN-US" style="font-size: 10pt;">cob</span><span style="font-size: 10pt;">.<i>lookupswitch</i>(swb -> swb<o:p></o:p></span></div>
<div style="margin: 0cm; font-size: 11pt; font-family: Calibri, sans-serif; background: white;">
<span lang="EN-US" style="font-size: 10pt;">       <span class="Apple-converted-space"> </span></span><span style="font-size: 10pt;">.<i>switchCase</i>(1, b -> b.<i>iinc</i>(0,1).<i>goto_</i>(b.<i>breakLabel</i>()))<o:p></o:p></span></div>
<div style="margin: 0cm; font-size: 11pt; font-family: Calibri, sans-serif; background: white;">
<span style="font-size: 10pt;">        .<i>switchCase</i>(2, b -> b.<i>iinc</i>(0,2).<i>goto_</i>(b.<i>breakLabel</i>()))<o:p></o:p></span></div>
<div style="margin: 0cm; font-size: 11pt; font-family: Calibri, sans-serif; background: white;">
<span style="font-size: 10pt;">        .<i>switchCase</i>(3, b -> b.<i>iinc</i>(0,3).<i>goto_</i>(b.<i>breakLabel</i>()))<o:p></o:p></span></div>
<div style="margin: 0cm; font-size: 11pt; font-family: Calibri, sans-serif; background: white;">
<span style="font-size: 10pt;">        .<i>defaultCase</i>(b -> b.<i>iinc</i>(0,100).<i>goto_</i>(b.<i>breakLabel</i>()))<o:p></o:p></span></div>
<div style="margin: 0cm; font-size: 11pt; font-family: Calibri, sans-serif; background: white;">
<span style="font-size: 10pt;">        .<i>switchCase</i>(4, b -> b.<i>iinc</i>(0,4).<i>goto_</i>(b.<i>breakLabel</i>()))<o:p></o:p></span></div>
<div style="margin: 0cm; font-size: 11pt; font-family: Calibri, sans-serif; background: white;">
<span style="font-size: 10pt;">        .<i>switchCase</i>(5, b -> b.<i>iinc</i>(0,5).<i>goto_</i>(b.<i>breakLabel</i>()))<o:p></o:p></span></div>
<div style="margin: 0cm; font-size: 11pt; font-family: Calibri, sans-serif; background: white;">
<span style="font-size: 10pt;">        .<i>switchCase</i>(6, b -> b.<i>iinc</i>(0,6).<i>goto_</i>(b.<i>breakLabel</i>())))<o:p></o:p></span></div>
<pre style="margin: 0cm; font-size: 10pt; font-family: "Courier New"; background: white;"><span class="literal"><span style="color: rgb(51, 107, 221);"><o:p> </o:p></span></span></pre>
<pre style="margin: 0cm; font-size: 10pt; font-family: "Courier New"; background: white;"><span style="">cob.tryWithFinalizer(<o:p></o:p></span></pre>
<pre style="margin: 0cm; font-size: 10pt; font-family: "Courier New"; background: white;"><span style="">        tryb -> tryb.goto_(externalLabel1),<o:p></o:p></span></pre>
<pre style="margin: 0cm; font-size: 10pt; font-family: "Courier New"; background: white;"><span style="">        finb -> finb.constantInstruction(42).ireturn(),<o:p></o:p></span></pre>
<pre style="margin: 0cm; font-size: 10pt; font-family: "Courier New"; background: white;"><span style="">        externalLabel1);<o:p></o:p></span></pre>
<div style="margin: 0cm; font-size: 11pt; font-family: Calibri, sans-serif; background: white;">
<span style="font-size: 10pt;"><o:p> </o:p></span></div>
<div style="margin: 0cm; font-size: 11pt; font-family: Calibri, sans-serif;"><span lang="EN-US"><o:p> </o:p></span></div>
<div style="margin: 0cm; font-size: 11pt; font-family: Calibri, sans-serif;"><span lang="EN-US"><o:p> </o:p></span></div>
<div style="margin: 0cm; font-size: 11pt; font-family: Calibri, sans-serif;"><span lang="EN-US">The implementation work is in progress. I’m now fiddling with precise placing of finalizers in all possible situations. Trying to do not miss any exit from try block,
 do not produce dead code, do not fall to double finalizer execution and all that with appropriate test coverage.<span class="Apple-converted-space"> </span><o:p></o:p></span></div>
<div style="margin: 0cm; font-size: 11pt; font-family: Calibri, sans-serif;"><span lang="EN-US">The implementation of finalizers now offers two possible modes:<o:p></o:p></span></div>
<div style="margin: 0cm 0cm 0cm 36pt; font-size: 11pt; font-family: Calibri, sans-serif; text-indent: -18pt;">
<span lang="EN-US" style="font-family: Symbol;"><span>·<span style="font-style: normal; font-variant-caps: normal; font-weight: normal; font-stretch: normal; font-size: 7pt; line-height: normal; font-family: "Times New Roman"; font-size-adjust: none; font-kerning: auto; font-variant-alternates: normal; font-variant-ligatures: normal; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-position: normal; font-feature-settings: normal; font-optical-sizing: auto; font-variation-settings: normal;">        <span class="Apple-converted-space"> </span></span></span></span><span lang="EN-US">expanded
 (as javac does now), where the code flow is unaffected and finalizer block is copied before each exit from the try block<o:p></o:p></span></div>
<div style="margin: 0cm 0cm 0cm 36pt; font-size: 11pt; font-family: Calibri, sans-serif; text-indent: -18pt;">
<span lang="EN-US" style="font-family: Symbol;"><span>·<span style="font-style: normal; font-variant-caps: normal; font-weight: normal; font-stretch: normal; font-size: 7pt; line-height: normal; font-family: "Times New Roman"; font-size-adjust: none; font-kerning: auto; font-variant-alternates: normal; font-variant-ligatures: normal; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-position: normal; font-feature-settings: normal; font-optical-sizing: auto; font-variation-settings: normal;">        <span class="Apple-converted-space"> </span></span></span></span><span lang="EN-US">compact,
 where identical exits are joined to common copy of the finalizer block (for example all returns are merged into one finalizer and return).<o:p></o:p></span></div>
<div style="margin: 0cm; font-size: 11pt; font-family: Calibri, sans-serif;"><span lang="EN-US"><o:p> </o:p></span></div>
<div style="margin: 0cm; font-size: 11pt; font-family: Calibri, sans-serif;"><span lang="EN-US"><o:p> </o:p></span></div>
<div style="margin: 0cm; font-size: 11pt; font-family: Calibri, sans-serif;"><span lang="EN-US">Please let me know your comments and suggestions.<o:p></o:p></span></div>
<div style="margin: 0cm; font-size: 11pt; font-family: Calibri, sans-serif;"><span lang="EN-US"><o:p> </o:p></span></div>
<div style="margin: 0cm; font-size: 11pt; font-family: Calibri, sans-serif;"><span lang="EN-US">Thanks,<o:p></o:p></span></div>
<div style="margin: 0cm; font-size: 11pt; font-family: Calibri, sans-serif;"><span lang="EN-US">Adam</span></div>
</div>
</blockquote>
</div>
</blockquote>
</div>
<br>
</body>
</html>