[Vm-dev] questions about a couple of primitives

Fabio Niephaus lists at fniephaus.com
Tue Sep 1 07:23:18 UTC 2020


On Tue, Sep 1, 2020 at 5:32 AM Florin Mateoc <florin.mateoc at gmail.com> wrote:
>
>
>
>
> On Mon, Aug 31, 2020 at 8:25 PM Eliot Miranda <eliot.miranda at gmail.com> wrote:
>>
>>
>>
>>
>> On Aug 31, 2020, at 5:11 PM, Florin Mateoc <florin.mateoc at gmail.com> wrote:
>>
>> 
>>
>>
>> On Mon, Aug 31, 2020 at 6:29 PM Florin Mateoc <florin.mateoc at gmail.com> wrote:
>>>
>>>
>>>
>>>
>>> On Mon, Aug 31, 2020 at 6:00 PM Eliot Miranda <eliot.miranda at gmail.com> wrote:
>>>>
>>>>
>>>>
>>>>
>>>> On Mon, Aug 31, 2020 at 2:00 PM Florin Mateoc <florin.mateoc at gmail.com> wrote:
>>>>>
>>>>>
>>>>> I think this is especially confusing since the comment says that the primitive always fails, and then the expectation is that the Smalltalk code that follows is executed instead. But that code does not do what the method actually does
>>>>
>>>>
>>>> I disagree.  It does exactly what the method does (it *is* the implementation of the method) unless the stack is unwound.  Yes, the comment could point the reader to Context>>#resume:through: which runs the ensure: & ifCurtailed: blocks on unwind.  Bit otherwise ifCurtailed: is not somehow magically not executed.  It is what it is ;-)
>>>>
>>>> As I said earlier,  ifCurtailed: only evaluates its argument if a non-local return or exception return is taken and the normal return path is not taken.  See Context>>#resume:through: which runs the ensure: & ifCurtailed: blocks.
>>>>
>>>> Can I confirm that your dissatisfaction is with the comment?  Or do you really think the ifCurtailed: method does not execute verbatim in the absence of unwinds?  If the former, you're welcome to submit an improved comment.  If the latter, you're mistaken.
>>>>
>>>
>>>
>>> Of course I agree that the ifCurtailed: method does execute verbatim in the absence of unwind. But the method does not only execute in the absence of unwinds. So my "dissatisfaction" is not just with the comment. While it could be somewhat be addressed by a comment, I think this is an instance where the vm is caught cheating. The shown Smalltalk code is not what gets executed in the presence of unwinds (as opposed to the code shown in #ensure: ). The execution of the argument block is hidden inside the vm
>>>
>>
>> To be more pedantic, neither #ensure: nor #ifCurtailed: disclose what is really happening on the unwind path, but at least #ensure: shows some code that conceptually matches its semantics.
>> In both cases, there is magic happening inside #valueNoContextSwitch, which, although it does not take any arguments, it knows how to (call a method that knows how to) invoke, if necessary, its caller's argument.
>> Yes, by walking the stack and peeking inside the contexts' temps and then acting upon them anything is possible, but the resulting code is anything but readable.
>>
>> I would argue for passing the #ensure: and #ifCurtailed: arguments to the #valueNoContextSwitch method/primitive, thus making it possible to avoid #resume:through: - I think such methods are fine for simulation/debugger, but not for runtime.
>>
>>
>> -1. Putting this in the vm adds a whole level of execution suspension & resumption which isn’t there.  Since Smalltalk has first class activation records it can (and does) elegantly implement a number of very complex control structures (such as unwind protect evaluation, and exception delivery) above the vm.
>
>
> Sorry, I disagree. And I would not put exception delivery in the same boat - that one does exactly what you say, it implements something in the image, above the vm, not just elegantly, but also putting more power in the hands of the developer. But everything is out there in the open, browsable, readable, debuggable and easily understandable.
> In this case we have a message sent to a context by the vm, which is not what I would call above the vm. If one browses the code, reads the code and the comments, of both #ifCurtailed: and #valueNoContextSwitch, nothing makes sense, there is no indication whatsoever of what is happening under the hood, no mention of the message that the vm sends to the context. Nor is anything visible in the debugger. Maybe this is indeed all justified for performance reasons, and since I am not the one who implemented it or who would implement an alternative, you can of course ignore my opinion. It is but a tiny corner of the image. But I think it is an important one, and you seem to deny that there is even any readability problem with these methods. Oh, well

I must admit it took quite a while to get this right in TruffleSqueak,
and the code for supporting it in both TruffleSqueak and RSqueak is
quite messy (see [1] and [2]). aboutToReturn:through: is not mentioned
in the Blue Book, so it was probably added later on. [3] mentions it
briefly, but I do think better in-image documentation would be very
useful.

Let me rephrase Florin's question: What keeps us from using the
following ifCurtailed: implementation (inspired by Clément's version
in [4])?

==============================
ifCurtailed: curtailBlock
    "{Code comment with good documentation of the mechanism}"
    | result curtailed |
    <primitive: 198>
    curtailed := true.
    [ result := self valueNoContextSwitch.
      curtailed := false ] ensure: [ curtailed ifTrue: [ curtailBlock value ] ].
    ^ result
==============================

Cheers,
Fabio

[1] https://github.com/hpi-swa/trufflesqueak/blob/d1f5bf327d774f30d163f019006fd8df15fc2784/src/de.hpi.swa.trufflesqueak/src/de/hpi/swa/trufflesqueak/nodes/AboutToReturnNode.java#L39
[2] https://github.com/hpi-swa/RSqueak/blob/d33005c8aa7c11b7af349b0585f5faa140c4db72/rsqueakvm/interpreter_bytecodes.py#L543
[3] http://www.mirandabanda.org/cogblog/2009/01/14/under-cover-contexts-and-the-big-frame-up/
[4] http://pharobooks.gforge.inria.fr/PharoByExampleTwo-Eng/latest/Exceptions.pdf

>
> Anyway, I only now noticed that the discussion is off list - if I did that, it was unintentional, I just clicked reply without checking that it goes to list or not


More information about the Vm-dev mailing list