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.
user stack, also known as the
general-purpose stack, stores individual
values. The two main opcodes for working with the user stack are
, to push a value onto the stack, and
, 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
instead of an integer register, you’d get an
exception, “Wrong type on top of
A handful of other instructions are useful for manipulating the user
rotates a given number of elements on
the user stack to put a different element on the top of the stack.
opcode returns ...