<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
<meta name="Generator" content="Microsoft Word 15 (filtered medium)">
<style><!--
/* Font Definitions */
@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;}
/* Style Definitions */
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;}
@page WordSection1
        {size:612.0pt 792.0pt;
        margin:72.0pt 72.0pt 72.0pt 72.0pt;}
div.WordSection1
        {page:WordSection1;}
--></style>
</head>
<body lang="en-CZ" link="#0563C1" vlink="#954F72" style="word-wrap:break-word">
<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 <brian.goetz@oracle.com><br>
<b>Date: </b>Tuesday, 13 September 2022 16:28<br>
<b>To: </b>Adam Sotona <adam.sotona@oracle.com>, classfile-api-dev@openjdk.org <classfile-api-dev@openjdk.org><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>
</body>
</html>