<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<font size="4"><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. <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).
<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. <br>
<br>
So let's put this on the list of things that need improvement,
and meanwhile, continue to shore up basics.<br>
</font></font><br>
<div class="moz-cite-prefix">On 5/19/2023 5:12 AM, Adam Sotona
wrote:<br>
</div>
<blockquote type="cite" cite="mid:CY4PR1001MB215075E7472EA7609A60073F8C7C9@CY4PR1001MB2150.namprd10.prod.outlook.com">
<meta name="Generator" content="Microsoft Word 15 (filtered
medium)">
<style>@font-face
{font-family:Wingdings;
panose-1:5 0 0 0 0 0 0 0 0 0;}@font-face
{font-family:"Cambria Math";
panose-1:2 4 5 3 5 4 6 3 2 4;}@font-face
{font-family:Calibri;
panose-1:2 15 5 2 2 2 4 3 2 4;}@font-face
{font-family:Menlo;
panose-1:2 11 6 9 3 8 4 2 2 4;}@font-face
{font-family:"Segoe UI";
panose-1:2 11 5 2 4 2 4 2 2 3;}p.MsoNormal, li.MsoNormal, div.MsoNormal
{margin:0cm;
font-size:11.0pt;
font-family:"Calibri",sans-serif;}a:link, span.MsoHyperlink
{mso-style-priority:99;
color:#0563C1;
text-decoration:underline;}code
{mso-style-priority:99;
font-family:"Courier New";}pre
{mso-style-priority:99;
mso-style-link:"HTML Preformatted Char";
margin:0cm;
font-size:10.0pt;
font-family:"Courier New";}p.MsoListParagraph, li.MsoListParagraph, div.MsoListParagraph
{mso-style-priority:34;
margin-top:0cm;
margin-right:0cm;
margin-bottom:0cm;
margin-left:36.0pt;
font-size:11.0pt;
font-family:"Calibri",sans-serif;
mso-ligatures:standardcontextual;
mso-fareast-language:EN-US;}span.HTMLPreformattedChar
{mso-style-name:"HTML Preformatted Char";
mso-style-priority:99;
mso-style-link:"HTML Preformatted";
font-family:"Courier New";
mso-ligatures:none;
mso-fareast-language:EN-GB;}span.apple-converted-space
{mso-style-name:apple-converted-space;}span.literal
{mso-style-name:literal;}.MsoChpDefault
{mso-style-type:export-only;
font-size:10.0pt;
font-family:"Calibri",sans-serif;
mso-ligatures:none;
mso-fareast-language:EN-US;}div.WordSection1
{page:WordSection1;}ol
{margin-bottom:0cm;}ul
{margin-bottom:0cm;}</style>
<div class="WordSection1">
<p class="MsoNormal"><span lang="EN-US">Hi,<o:p></o:p></span></p>
<p style="mso-margin-top-alt:0cm;margin-right:0cm;margin-bottom:12.0pt;margin-left:0cm"><span style="font-size:10.5pt;font-family:"Segoe
UI",sans-serif;color:#1F2328">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 style="font-size:10.5pt;font-family:"Segoe
UI",sans-serif;color:#1F2328" lang="EN-US">.<o:p></o:p></span></p>
<p style="margin:0cm;box-sizing: border-box;caret-color: rgb(31,
35, 40);font-variant-caps: normal;orphans:
auto;text-align:start;widows: auto;-webkit-text-size-adjust:
auto;-webkit-text-stroke-width: 0px;word-spacing:0px">
<span style="font-size:10.5pt;font-family:"Segoe
UI",sans-serif;color:#1F2328">Here I would like to
propose new</span><span class="apple-converted-space"><span style="font-size:10.5pt;font-family:"Segoe
UI",sans-serif;color:#1F2328"> </span></span><code><span style="font-size:9.0pt;font-family:Menlo;color:#1F2328">CodeBuilder</span></code><span class="apple-converted-space"><span style="font-size:10.5pt;font-family:"Segoe
UI",sans-serif;color:#1F2328"> </span></span><span style="font-size:10.5pt;font-family:"Segoe
UI",sans-serif;color:#1F2328">conveniences:<br style="box-sizing: border-box">
<br>
</span><code><span style="font-size:9.0pt;font-family:Menlo"><o:p></o:p></span></code></p>
<ul style="margin-top:0cm" type="disc">
<li class="MsoNormal" style="color:#1F2328;mso-list:l3 level1
lfo3"><code><span style="font-size:9.0pt;font-family:Menlo">lookupswitch(Consumer<SwitchBuilder>
switchHandler)<o:p></o:p></span></code></li>
<li class="MsoNormal" style="color:#1F2328;mso-list:l3 level1
lfo3"><code><span style="font-size:9.0pt;font-family:Menlo">tableswitch(Consumer<SwitchBuilder>
switchHandler)<o:p></o:p></span></code></li>
<li class="MsoNormal" style="color:#1F2328;mso-list:l3 level1
lfo3"><code><span style="font-size:9.0pt;font-family:Menlo">tryWithFinalizer(Consumer<CodeBuilder>
tryHandler, Consumer<CodeBuilder>
finalizerHandler, Label... externalLabels)<o:p></o:p></span></code></li>
<li class="MsoNormal" style="mso-list:l3 level1 lfo3"><code><span style="font-size:9.0pt;font-family:Menlo;color:#1F2328">tryWithFinalizer(Consumer<CodeBuilder>
tryHandler, Consumer<CodeBuilder>
finalizerHandler, boolean compactForm, Label...
externalLabels)</span></code><span style="font-size:10.5pt;font-family:"Segoe
UI",sans-serif"><o:p></o:p></span></li>
</ul>
<p style="margin:0cm;box-sizing: border-box;caret-color: rgb(31,
35, 40);font-variant-caps: normal;orphans:
auto;text-align:start;widows: auto;-webkit-text-size-adjust:
auto;-webkit-text-stroke-width: 0px;word-spacing:0px">
<span style="font-size:10.5pt;font-family:"Segoe
UI",sans-serif;color:#1F2328"><o:p> </o:p></span></p>
<p style="margin:0cm"><span style="font-size:10.5pt;font-family:"Segoe
UI",sans-serif;color:#1F2328">and new</span><span class="apple-converted-space"><span style="font-size:10.5pt;font-family:"Segoe
UI",sans-serif;color:#1F2328"> </span></span><code><span style="font-size:9.0pt;font-family:Menlo;color:#1F2328">CodeBuilder.SwitchBuilder</span></code><span class="apple-converted-space"><span style="font-size:10.5pt;font-family:"Segoe
UI",sans-serif;color:#1F2328"> </span></span><span style="font-size:10.5pt;font-family:"Segoe
UI",sans-serif;color:#1F2328">interface with methods:<br style="box-sizing: border-box">
<br>
</span><code><span style="font-size:9.0pt;font-family:Menlo"><o:p></o:p></span></code></p>
<ul style="margin-top:0cm" type="disc">
<li class="MsoNormal" style="color:#1F2328;mso-list:l2 level1
lfo6"><code><span style="font-size:9.0pt;font-family:Menlo">switchCase(int
caseValue, Consumer<BlockCodeBuilder> caseHandler)<o:p></o:p></span></code></li>
<li class="MsoNormal" style="color:#1F2328;mso-list:l2 level1
lfo6"><code><span style="font-size:9.0pt;font-family:Menlo">switchCase(List<Integer>
caseValues, Consumer<BlockCodeBuilder>
caseHandler)<o:p></o:p></span></code></li>
<li class="MsoNormal" style="mso-list:l2 level1 lfo6"><code><span style="font-size:9.0pt;font-family:Menlo;color:#1F2328">defaultCase(Consumer<BlockCodeBuilder>
defaultHandler)</span></code><span style="font-size:10.5pt;font-family:"Segoe
UI",sans-serif"><o:p></o:p></span></li>
</ul>
<p class="MsoNormal"><span lang="EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">Sample use case (more
can be seen in the tests at
<a href="https://github.com/openjdk/jdk/pull/14056/files" moz-do-not-send="true" class="moz-txt-link-freetext">https://github.com/openjdk/jdk/pull/14056/files</a>):<o:p></o:p></span></p>
<p class="MsoNormal" style="background:white"><span style="font-size:10.0pt;font-family:"Courier
New";color:black" lang="EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal" style="background:white"><span style="font-size:10.0pt;font-family:"Courier
New";color:black" lang="EN-US">cob</span><span style="font-size:10.0pt;font-family:"Courier
New";color:black">.<i>lookupswitch</i>(swb -> swb<o:p></o:p></span></p>
<p class="MsoNormal" style="background:white"><span style="font-size:10.0pt;font-family:"Courier
New";color:black" lang="EN-US">
</span><span style="font-size:10.0pt;font-family:"Courier
New";color:black">.<i>switchCase</i>(1, b -> b.<i>iinc</i>(0,1).<i>goto_</i>(b.<i>breakLabel</i>()))<o:p></o:p></span></p>
<p class="MsoNormal" style="background:white"><span style="font-size:10.0pt;font-family:"Courier
New";color:black"> .<i>switchCase</i>(2, b ->
b.<i>iinc</i>(0,2).<i>goto_</i>(b.<i>breakLabel</i>()))<o:p></o:p></span></p>
<p class="MsoNormal" style="background:white"><span style="font-size:10.0pt;font-family:"Courier
New";color:black"> .<i>switchCase</i>(3, b ->
b.<i>iinc</i>(0,3).<i>goto_</i>(b.<i>breakLabel</i>()))<o:p></o:p></span></p>
<p class="MsoNormal" style="background:white"><span style="font-size:10.0pt;font-family:"Courier
New";color:black"> .<i>defaultCase</i>(b ->
b.<i>iinc</i>(0,100).<i>goto_</i>(b.<i>breakLabel</i>()))<o:p></o:p></span></p>
<p class="MsoNormal" style="background:white"><span style="font-size:10.0pt;font-family:"Courier
New";color:black"> .<i>switchCase</i>(4, b ->
b.<i>iinc</i>(0,4).<i>goto_</i>(b.<i>breakLabel</i>()))<o:p></o:p></span></p>
<p class="MsoNormal" style="background:white"><span style="font-size:10.0pt;font-family:"Courier
New";color:black"> .<i>switchCase</i>(5, b ->
b.<i>iinc</i>(0,5).<i>goto_</i>(b.<i>breakLabel</i>()))<o:p></o:p></span></p>
<p class="MsoNormal" style="background:white"><span style="font-size:10.0pt;font-family:"Courier
New";color:black"> .<i>switchCase</i>(6, b ->
b.<i>iinc</i>(0,6).<i>goto_</i>(b.<i>breakLabel</i>())))<o:p></o:p></span></p>
<pre style="background:white"><span class="literal"><span style="color:#336BDD"><o:p> </o:p></span></span></pre>
<pre style="background:white"><span style="color:black">cob.tryWithFinalizer(<o:p></o:p></span></pre>
<pre style="background:white"><span style="color:black"> tryb -> tryb.goto_(externalLabel1),<o:p></o:p></span></pre>
<pre style="background:white"><span style="color:black"> finb -> finb.constantInstruction(42).ireturn(),<o:p></o:p></span></pre>
<pre style="background:white"><span style="color:black"> externalLabel1);<o:p></o:p></span></pre>
<p class="MsoNormal" style="background:white"><span style="font-size:10.0pt;font-family:"Courier
New";color:black"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><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. <o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">The implementation of
finalizers now offers two possible modes:<o:p></o:p></span></p>
<p class="MsoListParagraph" style="text-indent:-18.0pt;mso-list:l0 level1 lfo8"><!--[if !supportLists]--><span style="font-family:Symbol" lang="EN-US"><span style="mso-list:Ignore">·<span style="font:7.0pt
"Times New Roman"">
</span></span></span><!--[endif]--><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></p>
<p class="MsoListParagraph" style="text-indent:-18.0pt;mso-list:l0 level1 lfo8"><!--[if !supportLists]--><span style="font-family:Symbol" lang="EN-US"><span style="mso-list:Ignore">·<span style="font:7.0pt
"Times New Roman"">
</span></span></span><!--[endif]--><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></p>
<p class="MsoNormal"><span lang="EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">Please let me know your
comments and suggestions.<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">Thanks,<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">Adam</span><span lang="EN-US"><o:p></o:p></span></p>
</div>
</blockquote>
<br>
</body>
</html>