<html>
<head>
<style><!--
.hmmessage P
{
margin:0px;
padding:0px
}
body.hmmessage
{
font-size: 12pt;
font-family:Calibri
}
--></style></head>
<body class='hmmessage'><div dir='ltr'><div><span style="font-size: 12pt; ">I want to eliminate the knowledge of jmpbuf size from the frontend.</span></div><div>This is an old goal that I failed at previously.</div><div><br></div><div><br></div><div>I believe I need to:</div><div><br></div><div> Extend M3.CheckState to include jmpbuf_count.</div><div> The various Try*.m3.Check increment it and store</div><div> its previous value in the statement's state.</div><div> </div><div> </div><div> And then a few choices.</div><div> </div><div> 1)</div><div> </div><div> At the start of a function, call alloca with that count</div><div> times external variable jmpbuf_size, initialized in C.</div><div> </div><div><br></div><div> At each try, multiply its index by external variable jmpbuf_size</div><div> and add to the start of the buffer and use that.</div><div> </div><div> </div><div> or</div><div> </div><div> also have an array of n pointers filled in by C and index into that,</div><div> eliminating the runtime non-const multiplication</div><div> </div><div> </div><div> and/or</div><div><br></div><div> </div><div> include that array in the alloca even</div><div> </div><div> </div><div> having a "composite" not called simply "alloca" but "m3_alloc_jmpbufs",</div><div> which backends still have to special case to look kinda the same,</div><div> alloca being very special</div><div><br></div><div> </div><div> and/or</div><div><br></div><div> </div><div> add to functions themselves a notion of jmpbuf count, still the same</div><div> underlying mechanism..</div><div><br></div><div> </div><div> </div><div> and/or also add a m3cg primitive "jmpbuf_ref(n)", which gets the address</div><div> of the n'th jmpbuf of the current function, i.e. building in the multiplication</div><div> by jmpbuf size.</div><div><br></div><div> </div><div> </div><div> Note that some of these combinations of proposals imply an m3cg change.</div><div> All require at least a minimum of backend cooperation -- at least</div><div> recognizing "m3_alloca" as "alloca" or "_alloca" or "builtin_alloca" or such.</div><div><br></div><div> </div><div><br></div><div> Note that hopefully/ideally there is a much higher level way to do this,</div><div> whose design currently eludes me,</div><div> something that a backend could translate to setjmp/longjmp, or Win32 C __try,</div><div> or C++ try, or something. The "lowering" of try/finally in the frontend</div><div> is portable but very inefficient.</div><div><br></div><div> </div><div><br></div><div> Another solution is for the function to have an n-array of pointers</div><div> or merely n locals, initialized to null, and have each TRY dynamically</div><div> call alloca if the pointer isn't null.</div><div><br></div><div><br></div><div><span style="font-size: 12pt; "> This reduces stack use for the case of IF...TRY.</span></div><div><br></div><div><br></div><div> At the cost of extra initialization and the test and branch.</div><div><span style="font-size: 12pt; "><br></span></div><div><span style="font-size: 12pt; "><br></span></div><div><span style="font-size: 12pt; ">This is currently my favorite proposal so I should restate: have a local pointer for each TRY; initialized to null; at each TRY, if the pointer is null, call alloca(external jmpbuf_size). TRY is already super expensive in code size and speed. This solution adds a pointer per TRY and a test and conditional jmp. Multiplication is avoided. IF...TRY ends up using much less stack than before, if not executed, but most code will pay extra stack, a pointer per TRY. What I was missing before is that I need to extend M3.CheckState to count the jmpbufs and do some of the work at the start of the function, not merely at each TRY -- declare, and more importantly, initialize the pointers.</span></div><div><span style="font-size: 12pt; "><br></span></div><div><span style="font-size: 12pt; "> </span></div><div> </div><div>Thoughts?</div><div><br></div><div><span style="font-size: 12pt; "> </span></div><div><br></div><div><br></div><div> Thanks,</div><div> - Jay</div><br><br> </div></body>
</html>