<br><br><div class="gmail_quote">On Sun, May 6, 2012 at 11:27 PM, Igor Stasenko <span dir="ltr">&lt;<a href="mailto:siguctua@gmail.com" target="_blank">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>
Just an idea..<br>
<br>
currently, when returning from bottom context, which has no sender, VM<br>
generates a message #cannotReturn:<br>
<br>
You can checking by invoking:<br>
<br>
(thisContext privSender: nil). ^ 5<br>
<br>
which is fine, except that i think we can extend this behavior for use<br>
in cases when we leaving the normal &quot;smalltalk&quot; execution<br>
back to some C caller, namely:<br>
<br>
- callbacks<br>
- a &quot;message send&quot; from C<br>
<br>
upon return from method (or block) , an interpreter always checks if<br>
sender of a context (a caller context) is valid MethodContext<br>
instance.<br>
i am thinking about very small change, which will provide additional<br>
functionality:<br>
 - if context&#39;s sender in non-valid MethodContext, but instance of<br>
SmallInteger, then<br>
   we just assume that given smallinteger is an address of C function<br>
which should be called (by stripping the tag bit of course).<br></blockquote><div><br></div><div>This completely breaks context-to-stack mapping in the COg VM where a SmallInteger in the sender field is used to identify a context&#39;s frame&#39;s frame pointer.  This is hidden from the image by the VM.  Whenever one accesses the sender field form the image the VM computes the actual sender object (nil or a context).</div>
<div><br></div><div>It is also unnecessary; return from callback can be performed by invoking a primitive, as happens in the Alien callbacks.  It is easy to establish an activation as the sender of the callback activation which receives the result of the callback activation and returns it to the callback via the primitive.  i.e.</div>
<div><br></div><div><div>Alien class&gt;&gt;invokeCallbackContext: vmCallbackContextAddress &quot;&lt;Integer&gt;&quot; &quot;^&lt;FFICallbackReturnValue&gt;&quot;</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>&quot;The low-level entry-point for callbacks sent from the VM/IA32ABI plugin.</div>
<div><span class="Apple-tab-span" style="white-space:pre">        </span> Return via primReturnFromContext:through:.  thisContext&#39;s sender is the</div><div><span class="Apple-tab-span" style="white-space:pre">        </span> call-out context.&quot;</div>
<div><span class="Apple-tab-span" style="white-space:pre">        </span>| callbackAlien type |</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>callbackAlien := (Smalltalk wordSize = 4</div><div><span class="Apple-tab-span" style="white-space:pre">                                                </span>ifTrue: [VMCallbackContext32]</div>
<div><span class="Apple-tab-span" style="white-space:pre">                                                </span>ifFalse: [VMCallbackContext64])</div><div><span class="Apple-tab-span" style="white-space:pre">                                                        </span>atAddress: vmCallbackContextAddress.</div>
<div><span class="Apple-tab-span" style="white-space:pre">        </span>[type := Callback evaluateCallbackForContext: callbackAlien]</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>ifCurtailed: [self error: &#39;attempt to non-local return across a callback&#39;].</div>
<div><span class="Apple-tab-span" style="white-space:pre">        </span>type ifNil:</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>[type := 1. callbackAlien wordResult: -1].</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>callbackAlien primReturnAs: type fromContext: thisContext</div>
</div><div><br></div><div><div>VMCallbackContext32/VMCallbackContext64&gt;&gt;primReturnAs: typeCode &quot;&lt;SmallInteger&gt;&quot; fromContext: context &quot;&lt;MethodContext&gt;&quot;</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>&lt;primitive: &#39;primReturnAsFromContextThrough&#39; module: &#39;IA32ABI&#39; error: ec&gt;</div>
<div><span class="Apple-tab-span" style="white-space:pre">        </span>^self primitiveFailed</div></div><div><br></div><div>So please, no.</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">

<br>
The function is of kind:<br>
<br>
   void (fn*) (sqInt returnValue).<br>
<br>
i.e. upon discovering sender is smallint, an interpreter will call<br>
this function with return value as argument.<br>
<br>
As an option for additional safety, we can avoid using pure function<br>
address in small integer instance, but instead<br>
use a registry of valid addresses (so that smallint will be an index<br>
in a table, where cool hackers can register own &quot;No-return&quot; handlers).<br>
Then VM should check if sender is small integer, and if it is a valid<br>
registered index in  &quot;No-return&quot; handlers table, and if its ok, then<br>
call that function,<br>
otherwise, do as usual and raise &quot;cannot return exception&quot;.<br>
<br>
You may argue, that same functionality can be achieved by using<br>
special primitive(s). Which is true for callbacks, since we definitely<br>
control the<br>
callback entry, and on return we can invoke special primitive to<br>
return back to C caller.<br>
But thinking about future, imagine that we have an API in VM, similar<br>
to Objective-C, which allows you to send a message to any object<br>
and get a result:<br>
<br>
/* C code */<br>
resultOop = vmSend(receiver, selector, 3 /*num args */ , arg1, arg2, arg3);<br>
<br>
here, we cannot control the execution , since potentially a C caller<br>
may pick any receiver , any selector, so the entry point, unlike<br>
from callbacks are not controllable at smalltalk side. However if we<br>
use the proposed trick, upon return from the context,<br>
we can clearly detect that it is time to return to C caller and handle<br>
it accordingly.<br>
<br>
What you think?<br>
<span class="HOEnZb"><font color="#888888"><br>
--<br>
Best regards,<br>
Igor Stasenko.<br>
</font></span></blockquote></div><br><br clear="all"><div><br></div>-- <br>best,<div>Eliot</div><br>