Stacks and Register Frames

Parrot provides 32 registers of each type: integer, floating-point number, string, and PMC. This is a generous number of registers, but it’s still too restrictive for the average use. You can hardly limit your code to 32 integers at a time. This is especially true when you start working with subroutines and need a way to store the caller’s values and the subroutine’s values. So, Parrot also provides stacks for storing values outside the 32 registers. Parrot has seven basic stacks, each used for a different purpose: the user stack, the control stack, the integer stack, and the four register backing stacks.[40]

User Stack

The user stack, also known as the general-purpose stack, stores individual values. The two main opcodes for working with the user stack are save , to push a value onto the stack, and restore , to pop one off the stack:

save 42         # push onto user stack
restore I1      # pop off user stack

The one argument to save can be either a constant or a register. The user stack is a typed stack, so restore will only pop a value into a register of the same type as the original value:

save 1
set I0, 4
restore I0
print I0        # prints 1
end

If that restore were restore N0 instead of an integer register, you’d get an exception, “Wrong type on top of stack!”

A handful of other instructions are useful for manipulating the user stack. rotate_up rotates a given number of elements on the user stack to put a different element on the top of the stack. The depth opcode returns ...

Get Perl 6 Essentials now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.