Identifying bound/free name in a FrameDescriptor

Andreas Woess andreas.woess at oracle.com
Thu Feb 7 14:55:40 UTC 2019


Hi Adam,

the Frame implementation is (currently) not really designed to have dynamic bindings. As you already know, the FrameDescriptor is shared between multiple Frame instances (invocations), so either none or all of them have a FrameSlot. For an unset or later added FrameSlot, isObject(slot) will return true and getObject(slot) == frameDescriptor.getDefaultValue().

In order to decide if a binding is bound in a particular invocation, I'd suggest to use a marker value or null to mark a slot as unbound (you can use the FrameDescriptor's default value for that).
In your variable access node, you should then check in the Object specialization that the slot is bound (e.g. getObject(slot) != null); in primitive type specializations, the guard (e.g. isLong(slot)) already implies it is bound.
The check can be omitted for statically bound variables (known at parse time).

Minimalist example:

public abstract class ReadVarNode extends Node {
    final FrameSlot slot;
    static final UNBOUND = null;

    ReadVarNode(FrameSlot slot) {
        this.slot = slot;
    }

    public abstract Object execute(VirtualFrame frame);
    public abstract long executeLong(VirtualFrame frame) throws UnexpectedResultException;

    @Specialization(guards = "frame.isLong(slot)")
    long readLocalLong(VirtualFrame frame) {
        return FrameUtil.getLongSafe(frame, slot);
    }

    @Specialization(guards = "frame.isObject(slot)")
    Object readLocalObject(VirtualFrame frame) {
        Object value = FrameUtil.getObjectSafe(frame, slot);
        if (value == UNBOUND) {
            throw /* unbound name exception */;
        }
        return value;
    }
}

Hope this helps. Let us know if you have further questions.

- Andreas

----- Original Message -----
From: kovariadam at gmail.com
To: graal-dev at openjdk.java.net
Sent: Tuesday, February 5, 2019 9:05:15 PM GMT +01:00 Amsterdam / Berlin / Bern / Rome / Stockholm / Vienna
Subject: Identifying bound/free name in a FrameDescriptor

Hello,

I have usecase concerning recursive calls (thus, as I noticed, one
instance of FrameDescriptor which is being reused for recursive calls)
and detecting whether a variable is bound or free within that
particular invocation.

For example, this simple recursive function:

factorial 1 = 1
factorial n = n * (factorial n - 1)

then:

factorial 5

In the first invocation, this function's FrameDescriptor would not
contain "n", but in the next call to itself, it will, because it will
be written to local stack in the second line.
The situation is, when in second invocation of this recursive
function, the FrameSlot for "n" will exist, so something like:

VirtualFrame.getFrameDescriptor().findFrameSlot("n")

will return true, however, the value will be null and code such as this one:

FrameUtil.getLongSafe(frame, getSlot())

will end up throwing an IllegalStateException.


That said, I think I understand that this is because the
FrameDescriptor is being reused here, my question is, how to
effectively decide whether a name is bound or not, that would not
involve findFrameSlot or catching IllegalStateException.



I hope my example can be understood, please let me know if it is
unclear. I will appreciate any feedback.


Thanks,

--
Adam Kovari(mailto:kovariadam at gmail.com)


More information about the graal-dev mailing list