<br><br><div class="gmail_quote">On Mon, Apr 23, 2012 at 4:59 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">
<div class="HOEnZb"><div class="h5"><br>
On 24 April 2012 02:42, Eliot Miranda &lt;<a href="mailto:eliot.miranda@gmail.com">eliot.miranda@gmail.com</a>&gt; wrote:<br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt; On Mon, Apr 23, 2012 at 4:20 PM, Igor Stasenko &lt;<a href="mailto:siguctua@gmail.com">siguctua@gmail.com</a>&gt; wrote:<br>
&gt;&gt;<br>
&gt;&gt;<br>
&gt;&gt; On 24 April 2012 01:57, Eliot Miranda &lt;<a href="mailto:eliot.miranda@gmail.com">eliot.miranda@gmail.com</a>&gt; wrote:<br>
&gt;&gt; &gt;<br>
&gt;&gt; &gt;<br>
&gt;&gt; &gt;<br>
&gt;&gt; &gt; On Mon, Apr 23, 2012 at 2:28 PM, Igor Stasenko &lt;<a href="mailto:siguctua@gmail.com">siguctua@gmail.com</a>&gt; wrote:<br>
&gt;&gt; &gt;&gt;<br>
&gt;&gt; &gt;&gt;<br>
&gt;&gt; &gt;&gt; Hi, Eliot, all :)<br>
&gt;&gt; &gt;&gt;<br>
&gt;&gt; &gt;&gt; i skimming through the cogit, and i cannot clearly identify the code<br>
&gt;&gt; &gt;&gt; which generating code for falling back to interpreter.<br>
&gt;&gt; &gt;&gt;<br>
&gt;&gt; &gt;&gt; Say,  i want to create a native code which does nothing, just<br>
&gt;&gt; &gt;&gt; switching back to interpreter, so the bytecode of given method will be<br>
&gt;&gt; &gt;&gt; interpreted<br>
&gt;&gt; &gt;&gt; where i should look for..<br>
&gt;&gt; &gt;<br>
&gt;&gt; &gt;<br>
&gt;&gt; &gt; The main transition from native code to machine code is on return, through the ceReturnToInterpreterPC, which calls ceReturnToInterpreter:, which does a longjmp via the reenterInterpreter jmp_buf.  But that will only work at return time.<br>

&gt;&gt; &gt;<br>
&gt;&gt; &gt; If you want to switch to the interpreter at primitive fail time things are going to be more complicated.  So exactly when do you want to enter the interpreter?  What state is the native boost call in?<br>
&gt;&gt; &gt;<br>
&gt;&gt;<br>
&gt;&gt; The state is following:<br>
&gt;&gt;<br>
&gt;&gt;  - a compiled method having generated code.<br>
&gt;&gt;  - vm enters this code, thinking its a JITed method<br>
&gt;&gt;  - this code fails for some reason<br>
&gt;&gt;  - on a such failure, VM should switch to interpreter and interpret<br>
&gt;&gt; the corresponding method&#39;s body.<br>
&gt;&gt;<br>
&gt;&gt;  if native code doesn&#39;t fails, it should behave, as if method were<br>
&gt;&gt; performed normally (and so no need to fall back to interpreter).<br>
&gt;&gt;<br>
&gt;&gt; so, it is more or less the same how primitives behave , i.e. if method<br>
&gt;&gt; having a primitive, you call the prim, and if everything is ok, you<br>
&gt;&gt; continue running,<br>
&gt;&gt; if prim fails, you activating the method and interpreting its bytecode.<br>
&gt;<br>
&gt;<br>
&gt; Then you should look at compilePrimitive and compileInterpreterPrimitive:.  Basically after your NB code you want to call compileInterpreterPrimitive: with some special entry-point that will call activateNewMethod.  In fact you can probably just call activateNewMethod.<br>

<br>
</div></div>thanks. this looks fairly simple :)<br>
<div class="im"><br>
&gt;But what if the method is used a lot?   Don&#39;t you want to JIT the body of the method?  I suppose its not important.<br>
&gt;<br>
<br>
</div>apparently i don&#39;t want to jit the body, because then i will have to maintain<br>
own native code + jited code in addition, and VM has only one way to<br>
identify and use CompiledMethod(s) with jited code..<br>
because the idea is to make cogit think that the code NB generated at<br>
language side is the code he jited itself :)<br>
<br>
since there is 99.9% expectation that normally primitive won&#39;t fail,<br>
it is not important to optimize/jit method body anyways,<br>
because it usually contain a lot of error checking and fallback code<br>
and native code generation.. and it don&#39;t needs to be blazingly fast .<br></blockquote><div><br></div><div>Then you need something like</div><div><br></div><div><span class="Apple-tab-span" style="white-space:pre">        </span>&quot;Save processor fp, sp and return pc in the interpreter&#39;s frame stack and instruction pointers&quot;</div>
<div><span class="Apple-tab-span" style="white-space:pre">        </span>self genExternalizePointersForPrimitiveCall.</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>&quot;Switch to the C stack.&quot;</div>
<div><span class="Apple-tab-span" style="white-space:pre">        </span>self genLoadCStackPointersForPrimCall.</div><span class="Apple-tab-span" style="white-space:pre">        </span>&quot;Call activateNewMethod.&quot;<div> <span class="Apple-tab-span" style="white-space:pre">        </span>self CallRT: #activateNewMethod asSymbol asInteger.</div>
<div><br></div><div>(the asSymbol is important for Slang)</div><div><br></div><div>The only issue here is that if a callback from NB can occur then the other state (newMethod argumentCount) either needs to be saved/restored around the callback or saved/restored around the NB callout.</div>
<div><br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div class="HOEnZb"><div class="h5"><br>
&gt;&gt;<br>
&gt;&gt;<br>
&gt;&gt; &gt;&gt;<br>
&gt;&gt; &gt;&gt; Also, what are hooks i need for that? Which global state should be changed?<br>
&gt;&gt; &gt;<br>
&gt;&gt; &gt;<br>
&gt;&gt; &gt; The glue should modify the necessary global state, which is framePointer and stackPointer.<br>
&gt;&gt; &gt;<br>
&gt;&gt; &gt;&gt;<br>
&gt;&gt; &gt;&gt; And finally, is there documentation about calling convention for jited<br>
&gt;&gt; &gt;&gt; code? I&#39;d like to know what is the state of registers when VM enters<br>
&gt;&gt; &gt;&gt; the jited method, and what their purpose.<br>
&gt;&gt; &gt;<br>
&gt;&gt; &gt;<br>
&gt;&gt; &gt; See StackToRegisterMappingCogit&gt;&gt;callingConvention, StackToRegisterMappingCogit&gt;&gt;numRegArgs and CogIA32Compiler&gt;concreteRegister:.  With the  SimpleStackBasedCogit everything is passed on the stack.  With StackToRegisterMappingCogit, with &lt;= 1 arg, args are passed in ReceiverResultReg and Arg0Reg (but with the new object represnetation I will up the number of register arguments to 2).  But to protect the CoInterpreter from these details the registers are only visible to primitives.  Primitive failure or prolog code pushes them onto the stack.  See StackToRegisterMappingCogit&gt;&gt;genPushRegisterArgsForNumArgs: and senders, especially StackToRegisterMappingCogit&gt;&gt;genPushRegisterArgs, which is called from three places, one for method frame build, one for primitive failure and one for closure activation.<br>

&gt;&gt; &gt;<br>
&gt;&gt; &gt; In addition, on calling a method&#39;s checked entry-point (see Cogit&gt;compileEntry) ClassReg holds the class when the send site was linked, and ClassReg holds the message selector when the send is unlinked (which will through glue call CoInterpreter&gt;&gt;ceSend:super:to:numArgs:.<br>

&gt;&gt; &gt;<br>
&gt;&gt; &gt; HTH<br>
&gt;&gt; &gt;<br>
&gt;&gt; &gt; and if this is still confusing we could try skyping tomorrow.<br>
&gt;&gt;<br>
&gt;&gt; ah.. i am not focused on this task.. right now i collecting necessary<br>
&gt;&gt; bits of information. But thanks for the offer and info. I will, of<br>
&gt;&gt; course, bother you more when i will start making it for real :)<br>
&gt;&gt;<br>
&gt;&gt; &gt; --<br>
&gt;&gt; &gt; Eliot<br>
&gt;&gt; &gt;<br>
&gt;&gt; &gt;<br>
&gt;&gt;<br>
&gt;&gt;<br>
&gt;&gt;<br>
&gt;&gt; --<br>
&gt;&gt; Best regards,<br>
&gt;&gt; Igor Stasenko.<br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt; --<br>
&gt; best,<br>
&gt; Eliot<br>
&gt;<br>
&gt;<br>
<br>
<br>
<br>
--<br>
Best regards,<br>
Igor Stasenko.<br>
</div></div></blockquote></div><br><br clear="all"><div><br></div>-- <br>best,<div>Eliot</div><br>