<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<font size="4"><font face="monospace">Right, that's probably a
mistake. (This is what I meant by the locals stuff is
under-tested.) <br>
<br>
Yes, CCB should probably delegate to parent. If the parent is a
block, any locals used by transforming that block should be
local to that block. <br>
</font></font><br>
<div class="moz-cite-prefix">On 9/13/2022 10:41 AM, Adam Sotona
wrote:<br>
</div>
<blockquote type="cite" cite="mid:CY4PR1001MB2150E76E58ED85DDF71220C28C479@CY4PR1001MB2150.namprd10.prod.outlook.com">
<meta name="Generator" content="Microsoft Word 15 (filtered
medium)">
<style>@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;}p.MsoNormal, li.MsoNormal, div.MsoNormal
{margin:0cm;
font-size:11.0pt;
font-family:"Calibri",sans-serif;
mso-fareast-language:EN-US;}span.EmailStyle19
{mso-style-type:personal-reply;
font-family:"Calibri",sans-serif;
color:windowtext;}.MsoChpDefault
{mso-style-type:export-only;
font-size:10.0pt;}div.WordSection1
{page:WordSection1;}</style>
<div class="WordSection1">
<p class="MsoNormal"><span lang="EN-US">OK, understood.<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">Problem is probably in
ChainedCodeBuilder, which skips BlockCodeBuilder downstream
and allocates locals in the terminal DirectCodeBuilder
directly.<o:p></o:p></span></p>
<p class="MsoNormal"><o:p> </o:p></p>
<div style="border:none;border-top:solid #B5C4DF
1.0pt;padding:3.0pt 0cm 0cm 0cm">
<p class="MsoNormal" style="margin-bottom:12.0pt"><b><span style="font-size:12.0pt;color:black">From:
</span></b><span style="font-size:12.0pt;color:black">Brian
Goetz <a class="moz-txt-link-rfc2396E" href="mailto:brian.goetz@oracle.com"><brian.goetz@oracle.com></a><br>
<b>Date: </b>Tuesday, 13 September 2022 16:28<br>
<b>To: </b>Adam Sotona <a class="moz-txt-link-rfc2396E" href="mailto:adam.sotona@oracle.com"><adam.sotona@oracle.com></a>,
<a class="moz-txt-link-abbreviated" href="mailto:classfile-api-dev@openjdk.org">classfile-api-dev@openjdk.org</a>
<a class="moz-txt-link-rfc2396E" href="mailto:classfile-api-dev@openjdk.org"><classfile-api-dev@openjdk.org></a><br>
<b>Subject: </b>Re: Question on locals allocation in
BlockCodeBuilder</span><span style="font-size:12.0pt;color:black;mso-fareast-language:EN-GB"><o:p></o:p></span></p>
</div>
<p class="MsoNormal" style="margin-bottom:12.0pt"><span style="font-size:13.5pt;font-family:"Courier New"">This
isn't intended to prohibit allocation of locals in blocks;
it is to detect when two different mechanisms are used to
allocate locals together. Are you getting this error? <br>
<br>
We define allocateLocal in CodeBuilder. In the base
(Direct) code builder, when you call allocateLocal, it bumps
up a high water mark (topLocal). There is no way to
"deallocate" a local; this is an allocation that is global
to the Code attribute being built.
<br>
<br>
In the various BlockBuilder implementations, they implement
their own allocateLocal to:<br>
<br>
- start counting initially at the high water mark of their
parent;<br>
- let the user allocate locals starting at that high water
mark;<br>
- when the block is finished, we reclaim all of the locals
allocate in the block, by dropping back down to the starting
high water mark.<br>
<br>
The problem is that these two mechanisms -- the "global"
allocation of DirectCodeBuilder and the "local" allocation
of BlockCodeBuilder don't know about each other's work.
Here's an example of what could go wrong:<br>
<br>
.withCode(db -> { <br>
int x = db.allocateLocal(TypeKind.INT); // OK, global
allocation<br>
db.aconst_1();<br>
db.ifThen(bb -> { <br>
// both the DirectCodeBuilder and BlockCodeBuilder
(db and bb) are in scope here<br>
int y = bb.allocateLocal(TypeKind.INT); // OK,
local allocation<br>
int z = db.allocateLocal(TypeKind.INT); //
conflict!<br>
});<br>
});<br>
<br>
The problem is that I used both mechanisms inside the
block. It would be ok to exclusively use db, or exclusively
use bb, to allocate locals, but not both at once. The error
you've found detects this conflict and throws, albeit with a
not so helpful error message. <br>
<br>
Overall I think using both mechanisms at once will be rare,
because its rare to mix using both the "tip" and "root"
builders at the same time -- that's usually a bug.
<br>
<br>
If a transform needs locals for its work, it should allocate
a local from the root builder in the (stateful) transform
atStart() method.
<br>
<br>
</span><o:p></o:p></p>
<div>
<p class="MsoNormal">On 9/13/2022 9:06 AM, Adam Sotona wrote:<o:p></o:p></p>
</div>
<blockquote style="margin-top:5.0pt;margin-bottom:5.0pt">
<p class="MsoNormal"><span lang="EN-US">Hi,</span><o:p></o:p></p>
<p class="MsoNormal"><span lang="EN-US">I have a question
related to an older fragment of code in
BlockCodeBuilderImpl, that actively prohibits allocation
of locals in BlockCodeBuilder:</span><o:p></o:p></p>
<p class="MsoNormal"><span lang="EN-US"> </span><o:p></o:p></p>
<p class="MsoNormal" style="background:white"><span style="font-size:10.0pt;color:black"> public void
<b>end</b>() {</span><o:p></o:p></p>
<p class="MsoNormal" style="background:white"><span style="font-size:10.0pt;color:black">
terminal.with((LabelTarget) endLabel);</span><o:p></o:p></p>
<p class="MsoNormal" style="background:white"><span style="font-size:10.0pt;color:black"> if
(terminalMaxLocals != topLocal(terminal)) {</span><o:p></o:p></p>
<p class="MsoNormal" style="background:white"><span style="font-size:10.0pt;color:black"> throw new
IllegalStateException("Interference in local variable slot
management");</span><o:p></o:p></p>
<p class="MsoNormal" style="background:white"><span style="font-size:10.0pt;color:black"> }</span><o:p></o:p></p>
<p class="MsoNormal" style="background:white"><span style="font-size:10.0pt;color:black"> }</span><o:p></o:p></p>
<p class="MsoNormal"><span lang="EN-US"> </span><o:p></o:p></p>
<p class="MsoNormal"><span lang="EN-US">Do we really want to
prevent locals allocated and valid in the context of
BlockCodeBuilder only?</span><o:p></o:p></p>
<p class="MsoNormal"><span lang="EN-US"> </span><o:p></o:p></p>
<p class="MsoNormal"><span lang="EN-US">Thanks,</span><o:p></o:p></p>
<p class="MsoNormal"><span lang="EN-US">Adam</span><o:p></o:p></p>
<p class="MsoNormal"><span lang="EN-US"> </span><o:p></o:p></p>
</blockquote>
<p class="MsoNormal"><span style="mso-fareast-language:EN-GB"><o:p> </o:p></span></p>
</div>
</blockquote>
<br>
</body>
</html>