Search code examples
pharo

How is assignment implemented in Pharo?


Poking around the image and searching for ":=" produces no relevant results that quickly pop out.

Where exactly in the Pharo image is assignment implemented and how?


Solution

  • Assignment is translated to byte code instructions. Assignment to temporary variables translates to popIntoTemp:, which pops the current top off the stack and stores it in the slot assigned to the temporary variable described by the index (the argument to the instruction).

    Other assignments work in a similar manner, hence assignment to an instance variable translates to popIntoRcvr: ("pop into receiver"), where the index (the argument to the instruction) designates the index of the instance variable.

    Class variable assignment translates to popIntoLit: (here the argument to the instruction is the literal itself, i.e. the class variable in this case), class instance variable assignment to popIntoRcvr: and global assignment into popIntoLit: (the argument is the literal itself, i.e. the global).

    The names used for the instructions are taken from the byte code view in Pharo. The Blue Book defines these instructions in terms of bytes (which is what the virtual machine uses of course) and descriptive names. Here are the bytes associated with the instructions mentioned above:

    • popIntoTemp:: <68>
    • popIntoRcvr:: <60>
    • popIntoLit:: <82 C0>

    Also note, that there may be additional instructions for special cases, e.g. storing into a temporary variable at some large offset.