Parrot is a register-based virtual machine. It has four typed register sets -- integers, floating-point numbers, strings, and objects. All variables in PIR are one of these four types. When you work with register variables or named variables, you're actually working directly with register storage locations in the virtual machine.
If you've ever worked with an assembly language before,
you may immediately jump to the conclusion that
$I0 is the zeroth integer register in the register set,
but Parrot is a bit smarter than that.
The number of a register variable does not necessarily correspond to the register used internally; Parrot's compiler maps registers as appropriate for speed and memory considerations.
The only guarantee Parrot gives you is that you'll always get the same storage location when you use
$I0 in the same subroutine.
The most basic operation on a variable is assignment using the
null opcode sets an integer or number variable to a zero value,
and undefines a string or object.
PIR has an extensive set of instructions that work with integers, floating-point numbers, and numeric PMCs. Many of these instructions have a variant that modifies the result in place:
The first form of
+ stores the sum of the two arguments in the result variable,
The second variant,
adds the single argument to
$I0 and stores the sum back in
The arguments can be Parrot literals,
If the result is an integer type,
the arguments must also be integers.
A number result,
usually requires number arguments,
but many numeric instructions also allow the final argument to be an integer.
Instructions with a PMC result may accept an integer,
or PMC final argument:
Unary opcodes have a single argument.
They either return a result or modify the argument in place.
Some of the most common unary numeric opcodes are
abs (absolute value),
Binary opcodes have two arguments and a result.
Parrot provides addition (
and exponent (
as well as
gcd (greatest common divisor) and
lcm (least common multiple).
The most common floating-point operations are
ln (natural log),
log2 (log base 2),
log10 (log base 10),
as well as a full set of trigonometric opcodes such as
sinh (hyperbolic sine),
cosh (hyperbolic cosine),
tanh (hyperbolic tangent),
sech (hyperbolic secant),
asin (arc sine),
acos (arc cosine),
atan (arc tangent),
asec (arc secant),
All angle arguments for the trigonometric opcodes are in radians:
The majority of the floating-point operations have a single argument and a single result. The arguments can generally be either an integer or number, but many of these opcodes require the result to be a number.
The logical opcodes evaluate the truth of their arguments.
They are most useful to make decisions for control flow.
Integers and numeric PMCs are false if they're 0 and true otherwise.
Strings are false if they're the empty string or a single character "0",
and true otherwise.
PMCs are true when their
get_bool vtable function returns a nonzero value.
and opcode returns the first argument if it's false and the second argument otherwise:
or opcode returns the first argument if it's true and the second argument otherwise:
or are short-circuiting ops.
If they can determine what value to return from the first argument,
they'll never evaluate the second.
This is significant only for PMCs,
as they might have side effects on evaluation.
xor opcode returns the first argument if it is the only true value,
returns the second argument if it is the only true value,
and returns false if both values are true or both are false:
not opcode returns a true value when the argument is false and a false value if the argument is true:
The bitwise opcodes operate on their values a single bit at a time.
bxor return a value that is the logical AND,
or XOR of each bit in the source arguments.
They each take two arguments.
bxor also have variants that modify the result in place.
bnot is the logical NOT of each bit in the source argument.
The logical and arithmetic shift operations shift their values by a specified number of bits:
Parrot strings are buffers of variable-sized data. The most common use of strings is to store text data. Strings can also hold binary or other non-textual data, though this is rare.In general, a custom PMC is more useful. Parrot strings are flexible and powerful, to handle the complexity of human-readable (and computer-representable) text data. String operations work with string literals, variables, and constants, and with string-like PMCs.
Strings in double-quotes allow escape sequences using backslashes. Strings in single-quotes only allow escapes for nested quotes:
$S0 = "This string is \n on two lines" $S0 = 'This is a \n one-line string with a slash in it'
Table 4.1 shows the escape sequences Parrot supports in double-quoted strings.
If you need more flexibility in defining a string, use a heredoc string literal. The
<< operator starts a heredoc. The string terminator immediately follows. All text until the terminator is part of the string. The terminator must appear on its own line, must appear at the beginning of the line, and may not have any trailing whitespace.
$S2 = <<"End_Token" This is a multi-line string literal. Notice that it doesn't use quotation marks. End_Token
. operator to concatenate strings. The following example concatenates the string "cd" onto the string "ab" and stores the result in
Concatenation has a
.= variant to modify the result in place. In the next example, the
.= operation appends "xy" onto the string "abcd" in
repeat opcode repeats a string a specified number of times:
In this example,
repeat generates a new string with "a" repeated five times and stores it in
length opcode returns the length of a string in characters. This won't be the same as the length in bytes for multibyte encoded strings:
length has no equivalent for PMC strings.
The simplest version of the
substr opcode takes three arguments: a source string, an offset position, and a length. It returns a substring of the original string, starting from the offset position (0 is the first character) and spanning the length:
This example extracts a two-character string from "abcde" at a one-character offset from the beginning of the string (starting with the second character). It generates a new string, "bc", in the destination register
When the offset position is negative, it counts backward from the end of the string. Thus an offset of -1 starts at the last character of the string.
substr no longer has a four-argument form, as in-place string operations have been removed. There is a
replace operator which will perform the replacement and return a new_string without modifying the old_string. The arguments are new_string, old_string, offset, count and replacement_string. The old_string is copied to the new_string with the replacement_string inserted from offset replacing the content for count characters.
This example replaces the substring "bc" in
$S1 with the string "XYZ", and returns "aXYZde" in
$S1 is not changed:
When the offset position in a
replace is one character beyond the original string length,
replace appends the replacement string just like the concatenation operator. If the replacement string is an empty string, the opcode removes the characters from the original string in the new string.
chr opcode takes an integer value and returns the corresponding character in the ASCII character set as a one-character string. The
ord opcode takes a single character string and returns the integer value of the character at the first position in the string. The integer value of the character will differ depending on the current encoding of the string:
ord has a two-argument variant that takes a character offset to select a single character from a multicharacter string. The offset must be within the length of the string:
A negative offset counts backward from the end of the string, so -1 is the last character.
sprintf opcode generates a formatted string from a series of values. It takes two arguments: a string specifying the format, and an array PMC containing the values to be formatted. The format string and the result can be either strings or PMCs:
The format string is similar to C's
sprintf function with extensions for Parrot data types. Each format field in the string starts with a
% and ends with a character specifying the output format. Table 4.2 lists the available output format characters.
Each format field supports several specifier options: flags, width, precision, and size. Table 4.3 lists the format flags.
The width is a number defining the minimum width of the output from a field. The precision is the maximum width for strings or integers, and the number of decimal places for floating-point fields. If either width or precision is an asterisk (
*), it takes its value from the next argument in the PMC.
The size modifier defines the type of the argument the field takes. Table 4.4 lists the size flags. The values in the aggregate PMC must have a type compatible with the specified size.
The format string of this
sprintf example has two format fields. The first,
%#Px, extracts a PMC argument (
P) from the aggregate
$P2 and formats it as a hexadecimal integer (
x) with a leading 0x (
#). The second format field,
%+2.3Pf, takes a PMC argument (
P) and formats it as a floating-point number (
f) with a minimum of two whole digits and a maximum of three decimal places (
2.3) and a leading sign (
The test files t/op/string.t and t/op/sprintf.t have many more examples of format strings.
join opcode joins the elements of an array PMC into a single string. The first argument separates the individual elements of the PMC in the final string result.
This example builds a
$P0 with the values
"parrot". It then joins those values (separated by the string
"__") into a single string stored in
Splitting a string yields a new array containing the resulting substrings of the original string.
This example splits the string "abc" into individual characters and stores them in an array in
$P0. It then prints out the first and third elements of the array.
index opcode searches for a substring within a string. If it finds the substring, it returns the position where the substring was found as a character offset from the beginning of the string. If it fails to find the substring, it returns -1:
index also has a three-argument version, where the final argument defines an offset position for starting the search.
This example finds the second "eb" in "Beeblebrox" instead of the first, because the search skips the first three characters in the string.
The numeric bitwise opcodes also have string variants for AND, OR, and XOR:
bxors. These take string or string-like PMC arguments and perform the logical operation on each byte of the strings to produce the result string. Remember that in-place string operations are no longer available.
The bitwise string opcodes produce meaningful results only when used with simple ASCII strings, because Parrot performs bitwise operations per byte.
Strings use copy-on-write (COW) optimizations. A call to
$S1 = $S0 doesn't immediately make a copy of
$S0, it only makes both variables point to the same string. Parrot doesn't make a copy of the string until one of two strings is modified.
Modifying one of the two variables causes Parrot to create a new string. This example preserves the existing value in
$S0 and assigns the new value to the new string in
$S1. The benefit of copy-on-write is avoiding the cost of copying strings until the copies are necessary.
Years ago, strings only needed to support the ASCII character set (or charset), a mapping of 128 bit patterns to symbols and English-language characters. This worked as long as everyone using a computer read and wrote English and only used a small handful of punctuation symbols. In other words, it was woefully insufficient. A modern string system must manage charsets in order to make sense out of all the string data in the world. A modern string system must also handle different encodings -- ways to represent various charsets in memory and on disk.
Every string in Parrot has an associated encoding and character set. The default format is 8-bit ASCII, which is almost universally supported. Double-quoted string constants can have an optional prefix specifying the string's format.As you might suspect, single-quoted strings do not support this. Parrot tracks information about encoding and charset internally, and automatically converts strings when necessary to preserve these characteristics. Strings constants may have prefixes of the form
Parrot supports the formats
iso-8859-1 (Latin 1),
binary format treats the string as a buffer of raw unformatted binary data. It isn't really a string per se, because binary data contains no readable characters. This exists to support libraries which manipulate binary data that doesn't easily fit into any other primitive data type.
When Parrot operates on two strings (as in concatenation or comparison), they must both use the same character set and encoding. Parrot will automatically upgrade one or both of the strings to the next highest compatible format as necessary. ASCII strings will automatically upgrade to UTF-8 strings if needed, and UTF-8 will upgrade to UTF-16. All of these conversions happen inside Parrot, so the programmer doesn't need to worry about the details.
Polymorphic Containers (PMCs) are the basis for complex data types and object-oriented behavior in Parrot. In PIR, any variable that isn't a low-level integer, number, or string is a PMC. PMC variables act much like the low-level variables, but you have to instantiate a new PMC object before you use it. The
new opcode creates a new PMC object of the specified type.
This example creates a
String object, stores it in the PMC register variable
$P0, assigns it the value "That's a bollard and not a parrot", and prints it.
Every PMC has a type that indicates what data it can store and what behavior it supports. The
typeof opcode reports the type of a PMC. When the result is a string variable,
typeof returns the name of the type:
When the result is a PMC variable,
typeof returns the
Class PMC for that object type.
In most of the examples shown so far, PMCs duplicate the behavior of integers, numbers, and strings. Parrot provides a set of PMCs for this exact purpose.
String are thin overlays on Parrot's low-level integers, numbers, and strings.
A previous example showed a string literal assigned to a PMC variable of type
String. Direct assignment of a literal to a PMC works for all the low-level types and their PMC equivalents:
You may also assign non-constant low-level integer, number, or string registers directly to a PMC. The PMC handles the conversion from the low-level type to its own internal storage.This conversion of a simpler type to a more complex type is "boxing".
box opcode is a handy shortcut to create the appropriate PMC object from an integer, number, or string literal or variable.
In the reverse situation, when assigning a PMC to an integer, number, or string variable, the PMC also has the ability to convert its value to the low-level type.The reverse of "boxing" is "unboxing".
This example creates
String PMCs, and shows the effect of assigning each one back to a low-level type.
Converting a string to an integer or number only makes sense when the contents of the string are a number. The
String PMC will attempt to extract a number from the beginning of the string, but otherwise will return a false value.
PMCs can define complex types that hold multiple values, commonly called aggregates. Two basic aggregate types are ordered arrays and associative arrays. The primary difference between these is that ordered arrays use integer keys for indexes and associative arrays use string keys.
Aggregate PMCs support the use of numeric or string keys. PIR also offers a extensive set of operations for manipulating aggregate data types.
Parrot provides several ordered array PMCs, differentiated by whether the array should store booleans, integers, numbers, strings, or other PMCs, and whether the array should maintain a fixed size or dynamically resize for the number of elements it stores.
The core array types are
ResizableBooleanArray. The array types that start with "Fixed" have a fixed size and do not allow elements to be added outside their allocated size. The "Resizable" variants automatically extend themselves as more elements are added.With some additional overhead for checking array bounds and reallocating array memory. The array types that include "String", "Integer", or "Boolean" in the name use alternate packing methods for greater memory efficiency.
Parrot's core ordered array PMCs all have zero-based integer keys. Extracting or inserting an element into the array uses PIR's standard key syntax, with the key in square brackets after the variable name. An lvalue key sets the value for that key. An rvalue key extracts the value for that key in the aggregate to use as the argument value:
Setting the array to an integer value directly (without a key) sets the number of elements of the array. Assigning an array directly to an integer retrieves the number of elements of the array.
This is equivalent to using the
elements opcode to retrieve the number of items currently in an array:
Some other useful instructions for working with ordered arrays are
unshift, to add or remove elements.
pop work on the end of the array, the highest numbered index.
unshift work on the start of the array, adding or removing the zeroth element, and renumbering all the following elements.
An associative array is an unordered aggregate that uses string keys to identify elements. You may know them as "hash tables", "hashes", "maps", or "dictionaries". Parrot provides one core associative array PMC, called
Hash. String keys work very much like integer keys. An lvalue key sets the value of an element, and an rvalue key extracts the value of an element. The string in the key must always be in single or double quotes.
Hash PMC (without a key) to an integer result fetches the number of elements in the hash.You may not set a
Hash PMC directly to an integer value.
exists opcode tests whether a keyed value exists in an aggregate. It returns 1 if it finds the key in the aggregate and 0 otherwise. It doesn't care if the value itself is true or false, only that an entry exists for that key:
delete opcode removes an element from an associative array:
An iterator extracts values from an aggregate PMC one at a time. Iterators are most useful in loops which perform an action on every element in an aggregate. The
iter opcode creates a new iterator from an aggregate PMC. It takes one argument, the PMC over which to iterate:
shift opcode extracts the next value from the iterator.
Evaluating the iterator PMC as a boolean returns whether the iterator has reached the end of the aggregate:
Parrot provides predefined constants for working with iterators.
.ITERATE_FROM_END constants select whether an ordered array iterator starts from the beginning or end of the array. These two constants have no effect on associative array iterators, as their elements are unordered.
Load the iterator constants with the
.include directive to include the file iterator.pasm. To use them, set the iterator PMC to the value of the constant:
With all of those separate pieces in one place, this example loads the iterator constants, creates an ordered array of "a", "b", "c", creates an iterator from that array, and then loops over the iterator using a conditional
goto to checks the boolean value of the iterator and another unconditional
Associative array iterators work similarly to ordered array iterators. When iterating over associative arrays, the
shift opcode extracts keys instead of values. The key looks up the value in the original hash PMC.
This example creates an associative array
$P2 that contains three keys "a", "b", and "c", assigning them the values 10, 20, and 30. It creates an iterator (
$P1) from the associative array using the
iter opcode, and then starts a loop over the iterator. At the start of each loop, the
unless instruction checks whether the iterator has any more elements. If there are no more elements,
goto jumps to the end of the loop, marked by the label
iter_end. If there are more elements, the
shift opcode extracts the next key. Keyed assignment stores the integer value of the element indexed by the key in
$I9. After printing the integer value,
goto jumps back to the start of the loop, marked by
Aggregates can hold any data type, including other aggregates. Accessing elements deep within nested data structures is a common operation, so PIR provides a way to do it in a single instruction. Complex keys specify a series of nested data structures, with each individual key separated by a semicolon.
This example builds up a data structure of an associative array containing an ordered array. The complex key
["answer"; $I1] retrieves an element of the array within the hash. You can also set a value using a complex key:
The individual keys are integer or string literals, or variables with integer or string values.
PMC registers don't directly store the data for a PMC, they only store a pointer to the structure that stores the data. As a result, the
= operator doesn't copy the entire PMC, it only copies the pointer to the PMC data. If you later modify the copy of the variable, it will also modify the original.
In this example,
$P1 are both pointers to the same internal data structure. Setting
$P1 to the string literal "Zaphod", it overwrites the previous value "Ford". Both
$P1 refer to the
String PMC "Zaphod".
clone opcode makes a deep copy of a PMC, instead of copying the pointer like
This example creates an identical, independent clone of the PMC in
$P0 and puts it in
$P1. Later changes to
$P0 have no effect on the PMC in
$P1.With low-level strings, the copies created by
clone are copy-on-write exactly the same as the copy created by
To assign the value of one PMC to another PMC that already exists, use the
This example creates two
$P2, and gives the first one the value 42. It then uses
assign to pass the same integer value on to
$P1 doesn't change. The result for
assign must have an existing object of the right type in it, because
assign neither creates a new duplicate object (as does
clone) or reuses the source object (as does
PMCs can have additional values attached to them as "properties" of the PMC. Most properties hold extra metadata about the PMC.
setprop opcode sets the value of a named property on a PMC. It takes three arguments: the PMC on which to set a property, the name of the property, and a PMC containing the value of the property.
getprop opcode returns the value of a property. It takes two arguments: the name of the property and the PMC from which to retrieve the property value.
This example creates a
String object in
$P0 and an
Integer object with the value 1 in
setprop sets a property named "eric" on the object in
$P0 and gives the property the value of
getprop retrieves the value of the property "eric" on
$P0 and stores it in
Parrot stores PMC properties in an associative array where the name of the property is the key.
delprop deletes a property from a PMC.
You can fetch a complete hash of all properties on a PMC with
Fetching the value of a non-existent property returns an
You may have noticed that a simple operation sometimes has a different effect on different PMCs. Assigning a low-level integer value to a
Integer PMC sets its integer value of the PMC, but assigning that same integer to an ordered array sets the size of the array.
Every PMC defines a standard set of low-level operations called vtable functions. When you perform an assignment like:
$P0 = 5
... Parrot calls the
set_integer_native vtable function on the PMC referred to by register
Parrot has a fixed set of vtable functions, so that any PMC can stand in for any other PMC; they're polymorphic.Hence the name "Polymorphic Container". Every PMC defines some behavior for every vtable function. The default behavior is to throw an exception reporting that the PMC doesn't implement that vtable function. The full set of vtable functions for a PMC defines the PMC's basic interface, but PMCs may also define methods to extend their behavior beyond the vtable set.
Parrot performs operations on variables stored in small register sets local to each subroutine. For more complex tasks,...and for most high-level languages that Parrot supports. it's also useful to have variables that live beyond the scope of a single subroutine. These variables may be global to the entire program or restricted to a particular library. Parrot stores long-lived variables in a hierarchy of namespaces.
get_global store and fetch a variable in a namespace:
The first two statements in this example create a
String PMC in
$P0 and assign it a value. In the third statement,
set_global stores that PMC as the named global variable
bee. At some later point in the program,
get_global retrieves the global variable by name, and stores it in
$P1 to print.
Namespaces can only store PMC variables. Parrot boxes all primitive integer, number, or string values into the corresponding PMCs before storing them in a namespace.
The name of every variable stored in a particular namespace must be unique. You can't have store both an
Integer PMC and an array PMC both named "bee", stored in the same namespace.You may wonder why anyone would want to do this. We wonder the same thing, but Perl 5 does it all the time. The Perl 6 implementation on Parrot includes type sigils in the names of the variables it stores in namespaces so each name is unique, e.g.
A single global namespace would be far too limiting for most languages or applications. The risk of accidental collisions -- where two libraries try to use the same name for some variable -- would be quite high for larger code bases. Parrot maintains a collection of namespaces arranged as a tree, with the
parrot namespace as the root. Every namespace you declare is a child of the
parrot namespace (or a child of a child....).
get_global opcodes both have alternate forms that take a key name to access a variable in a particular namespace within the tree. This code example stores a variable as
bill in the Duck namespace and retrieves it again:
The key name for the namespace can have multiple levels, which correspond to levels in the namespace hierarchy. This example stores a variable as
bill in the Electric namespace under the General namespace in the hierarchy.
get_global opcode operate on the currently selected namespace. The default top-level namespace is the "root" namespace. The
.namespace directive allows you to declare any namespace for subsequent code. If you select the General Electric namespace, then store or retrieve the
bill variable without specifying a namespace, you will work with the General Electric bill, not the Duck bill.
.namespace ["General";"Electric"] #... set_global "bill", $P0 $P1 = get_global "bill"
Passing an empty key to the
.namespace directive resets the selected namespace to the root namespace. The brackets are required even when the key is empty.
.namespace [ ]
When you need to be absolutely sure you're working with the root namespace regardless of what namespace is currently active, use the
get_root_global opcodes instead of
get_global. This example sets and retrieves the variable
bill in the Dollar namespace, which is directly under the root namespace:
To prevent further collisions, each high-level language running on Parrot operates within its own virtual namespace root. The default virtual root is
parrot, and the
.HLL directive (for High-Level Language) selects an alternate virtual root for a particular high-level language:
get_hll_global opcodes are like
get_root_global, except they always operate on the virtual root for the currently selected HLL. This example stores and retrieves a
bill variable in the Euro namespace, under the Dutch HLL namespace root:
.HLL 'Dutch' #... set_hll_global ["Euro"], "bill", $P0 $P1 = get_hll_global ["Euro"], "bill"
Namespaces are just PMCs. They implement the standard vtable functions and a few extra methods. The
get_namespace opcode retrieves the currently selected namespace as a PMC object:
$P0 = get_namespace
get_root_namespace opcode retrieves the namespace object for the root namespace. The
get_hll_namespace opcode retrieves the virtual root for the currently selected HLL.
$P0 = get_root_namespace $P0 = get_hll_namespace
Each of these three opcodes can take a key argument to retrieve a namespace under the currently selected namespace, root namespace, or HLL root namespace:
$P0 = get_namespace ["Duck"] $P0 = get_root_namespace ["General";"Electric"] $P0 = get_hll_namespace ["Euro"]
Once you have a namespace object you can use it to retrieve variables from the namespace instead of using a keyed lookup. This example first looks up the Euro namespace in the currently selected HLL, then retrieves the
bill variable from that namespace:
$P0 = get_hll_namespace ["Euro"] $P1 = get_global $P0, "bill"
Namespaces also provide a set of methods to provide more complex behavior than the standard vtable functions allow. The
get_name method returns the name of the namespace as a
$P3 = $P0.'get_name'()
get_parent method retrieves a namespace object for the parent namespace that contains this one:
$P5 = $P0.'get_parent'()
get_class method retrieves any Class PMC associated with the namespace:
$P6 = $P0.'get_class'()
find_var methods store and retrieve variables in a namespace in a language-neutral way:
$P0.'add_var'("bee", $P3) $P1 = $P0.'find_var'("bee")
find_namespace method looks up a namespace, just like the
$P1 = $P0.'find_namespace'("Duck")
add_namespace method adds a new namespace as a child of the namespace object:
make_namespace method looks up a namespace as a child of the namespace object and returns it. If the requested namespace doesn't exist,
make_namespace creates a new one and adds it under that name:
$P1 = $P0.'make_namespace'("Duck")
Just like regular assignment, the various operations to store a variable in a namespace only store a pointer to the PMC. If you modify the local PMC after storing in a namespace, those changes will also appear in the stored global. To store a true copy of the PMC,
clone it before you store it.
Leaving the global variable as an alias for a local variable has its advantages. If you retrieve a stored global into a register and modify it:
... you modify the value of the stored global, so you don't need to call