Hi Igor,<div><br><div class="gmail_quote">On Wed, May 13, 2009 at 6:18 PM, Igor Stasenko <span dir="ltr">&lt;<a href="mailto:siguctua@gmail.com">siguctua@gmail.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<br>
2009/5/14 Eliot Miranda &lt;<a href="mailto:eliot.miranda@gmail.com">eliot.miranda@gmail.com</a>&gt;:<br>
<div><div></div><div class="h5">&gt;<br>
&gt;<br>
&gt;<br>
&gt; On Wed, May 13, 2009 at 5:05 PM, Jecel Assumpcao Jr &lt;<a href="mailto:jecel@merlintec.com">jecel@merlintec.com</a>&gt; wrote:<br>
&gt;&gt;<br>
&gt;&gt; Eliot,<br>
&gt;&gt;<br>
&gt;&gt; &gt; [JIT code uses call which pushes PC first]<br>
&gt;&gt;<br>
&gt;&gt; Ok, so this can&#39;t be helped.<br>
&gt;&gt;<br>
&gt;&gt; &gt; So while one could I don&#39;t see that its worth-while.  Even if one did<br>
&gt;&gt; &gt; keep the arguments and temporaries together one would still have the<br>
&gt;&gt; &gt; stack contents separate from the arguments and temporaries and<br>
&gt;&gt; &gt; temporary access bytecodes can still access those so arguably one<br>
&gt;&gt; &gt; would still have to check the index against the temp count.<br>
&gt;&gt;<br>
&gt;&gt; Really? I wouldn&#39;t expect the compiler to ever generate such bytecodes<br>
&gt;&gt; and so wasn&#39;t too worried if the VM did the wrong thing in this<br>
&gt;&gt; situation.<br>
&gt;<br>
&gt; There&#39;s a tension between implementing what the current compiler produces and implementing what the instruction set defines.  For example should one assume arguments are never written to?  I lean on the side of implementing the instruction set.<br>

&gt;&gt;<br>
&gt;&gt; &gt; In the JIT the flag is a bit in the method reference&#39;s LSBs and is set for free<br>
&gt;&gt; &gt; on frame build.<br>
&gt;&gt;<br>
&gt;&gt; That sounds like a neat trick. Are the stack formats for the interpreted<br>
&gt;&gt; stack vm and the jit a little diffeent?<br>
&gt;<br>
&gt; Yes.  In the JIT an interpreted frame needs an extra field to hold the saved bytecode instruction pointer when an interpreted frame calls a machine code frame because the return address is the &quot;return to interpreter trampoline&quot; pc.  There is no flag word in a machine code frame.  So machine code frames save one word w.r.t. the stack vm and interpreted frames gain a word.  But most frames are machine code ones so most of the time one is saving space.<br>

&gt;&gt;<br>
&gt;&gt; Thanks for the explanations. I haven&#39;t figured out how to do this in<br>
&gt;&gt; hardware in a reasonable way and so might have to go with a different design.<br>
&gt;<br>
&gt; I guess that in hardware you can create an instruction that will load a descriptor register as part of the return sequence in parallel with restoring the frame pointer and method so one would never indirect through the frame pointer to fetch the flags word; instead it would be part of the register state.  But that&#39;s an extremely uneducated guess :)<br>

&gt;&gt;<br>
&gt;&gt;<br>
&gt;&gt; -- Jecel<br>
&gt;&gt;<br>
&gt;<br>
&gt;<br>
&gt;<br>
</div></div>The one thing you mentioned is separation of data stack &amp; code stack<br>
(use separate space to push args &amp; temps, and another space to push<br>
VM/JIT-specific state, such as context pointers &amp; return addresses).</blockquote><div><br></div><div>I think the &quot;official&quot; names used in Forth implementations are &quot;control stack&quot; and &quot;operand stack&quot;.</div>
<div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">In this way, there are no big issue with this separation, because to<br>
access the state in both variants you still have to track two pointers<br>
- namely stack pointer and frame pointer.</blockquote><div><br></div><div>I think you end up with three pointers.  There&#39;s a frame pointer into the control stack, a stack pointer to the receiver/0th temp for that frame on the operand stack and a stack pointer proper to the top of stack on the operand stack.  An operand stack page needs to be large enough to hold the maximum amount of stack space used by as many frames as can fit on the control stack and this is an issue because one ends up wasting a lot of space, something the single stack organization avoids.</div>
<div><br></div><div><br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">But such a separation could be very helpful to handle the unitialized temps.</blockquote>
<div><br></div><div>Right.  I took this approach in my BrouHaHa VMs, with the simplification that there was only one stack page.  After becoming familiar with HPS&#39;s organization I prefer it.  It fits better with conventional hardware.  But I think Jecel could use the split stack approach profitably on custom hardware where having 3 registers frame, stack base and top of stack is no problem, and where memory is still cheap.  Its fine to throw transistors at the operand stack as that memory will be made frequent use of, even if one is wasting 25% of it most of the time.</div>
<div><br></div><div>Jecel can also design the machine to avoid taking interrupts on the operand stack and provide a separate interrupt stack.</div><div><br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
To call a method you usually pushing args (sp decreasing - if stack<br>
organized in bottom-to-top order)<br>
then pushing return address, save sp &amp; other required context state<br>
(fp decreasing)<br>
And then you are ready to activate a new method.<br>
Now there are 2 variants:<br>
at the time when you entering new method, you can simply reserve the<br>
stack space for its temps, OR, leave the sp unchanged, but organize<br>
the code in such way, that each time you get a temp initialized, you<br>
decreasing sp.<br>
Then, at each point of execution, if debugger needs to determine if<br>
some of the method&#39;s temps are unitialized , it can simply check that<br>
context&#39;s saved sp should be &lt;= temp pointer.<br>
This of course makes things more complex, because you should<br>
reorganize temps in order of their initialization i.e.<br>
| a b |<br>
b := 5.<br>
a := 6.<br>
<br>
to make pointer to a will be &lt; pointer to b.<br>
As well, as you should not alter the sp when &quot;pushing&quot; arguments<br>
(otherwise debugger will assume that you have already initialized<br>
temps).<br>
<br>
But, it allows you to conserve the stack space between a method calls:<br>
<br>
someMethod<br>
| a b |<br>
<br>
&quot;1&quot; b := 5.<br>
&quot;2&quot; self foo.<br>
&quot;3&quot; a := 6.<br>
<br>
at 1) you decrement sp by pushing a initialized temp value of b<br>
at 2) you saving the sp then increasing the sp by number of arguments<br>
for method (foo) and activating a new method, on return you restoring<br>
sp<br>
at 3) you pushing a new initialized value , which leads to another<br>
decrement of sp.<br>
<br>
and, as i said, if you suspend the process in the middle of &quot;self foo&quot;<br>
call, and inspecting the context of someMethod,<br>
debugger can easily see, that it&#39;s saved sp is too small to hold all<br>
temps, and if user wanting to see a current uninitialized value of<br>
&#39;a&#39;, then answer is nil, because offset of &#39;a&#39; is greater than<br>
currently saved sp.<br>
<br>
There&#39;s another cost , of couse, - if method having 10 temps, then the<br>
generated code needs 10 pushes for each initialization, in contrast to<br>
single instruction for reserving space at the method&#39;s activation,<br>
when you simply decreasing the sp by a known constant.<br>
<br>
But i think there is no much difference: in both variants you have to<br>
write at some memory location , and there just a little difference how<br>
you calculate the address at initial store i.e. doing<br>
push reg<br>
or<br>
mov [fp + offset] &lt;- reg<br>
<br>
So the major pros is:<br>
 - conserving a stack space<br>
 - easy to determine unitialized temps for debugger<br>
<br>
and cons is:<br>
  - this could complicate the code generation<br>
<font color="#888888"><br>
--<br>
Best regards,<br>
Igor Stasenko AKA sig.<br>
</font></blockquote></div><br></div>