Parrot Virtual Machine/Parrot Programming

From Wikibooks, open books for an open world
Jump to navigation Jump to search

Parrot Programming

[edit | edit source]

The Parrot Virtual Machine (PVM) can be programmed using a variety of languages, scripts, and techniques. This versatility can be confusing at first. Here are some ways that Parrot can be programmed:

  1. Parrot Assembly Language (PASM). This is the lowest human-readable way to program Parrot, and is very similar to traditional assembly languages.
  2. Parrot Intermediate Representation (PIR). This is a higher-level language which is easier to program in than PASM, and significantly more common.
  3. Not Quite Perl (NQP). This is a bare-bones partial implementation of the Perl 6 language, which is designed for bootstrapping. It is higher-level than PIR and has many of the features and the capabilities of Perl 6. At the moment NQP is not fully-featured and must be compiled separately into bytecode before it can be run on Parrot.
  4. Custom Languages. Using the Parrot Compiler tools (PCT), new dynamic languages can easily be implemented on Parrot. Once a parser and libraries have been created for a language, that language can be used to program for Parrot. Many common programming languages, including Perl 6, Python, and JavaScript (ECMAScript) are being implemented on Parrot. We will discuss more languages in a later section.

Programming Steps

[edit | edit source]

There are a number of different methods to program Parrot, as we see in the list above. However, different programming methods require different steps. Here, we will give a very brief overview of some of the ways you program Parrot.

A program written in PASM or PIR, such as Foo.pasm or Bar.pir, can be run in one of two different ways. They can be interpreted directly by typing (on most Windows and Unix/Linux systems):
./parrot Foo.pasm
./parrot Bar.pir
This will run Parrot in interpreter mode. However, we can compile these programs down to Parrot Bytecode (PBC) using the following flags:
./parrot -o Foo.pbc Foo.pir
./parrot -o Bar.pbc Bar.pir
Of course, you can name the output files anything you want. Once you have a PBC file, you can run it like this:
./parrot Foo.pbc
NQP must be compiled down to PIR using the NQP compiler. This is located in the compilers/nqp directory of the Parrot repository
High Level Languages
To program parrot in a higher-level language than NQP or PIR—such as Perl 6, Python, or Ruby—there must first be a compiler available for that language. To run file for example (".pl" is the file extension for Perl programs), you would type:
./parrot languages/perl6/perl6.pbc
This runs the Perl 6 compiler on Parrot, and passes the file name to the compiler. To output a file into PIR or PBC, you would use the --target= option to specify an output format.

Virtual Machines?

[edit | edit source]

One term that we are going to be using frequently in this book is "Virtual Machine", or VM for short. It's worth discussing now what exactly a VM is.

Before talking about virtual machines, let's consider actual computer hardware first. In an ordinary computer system, a native machine, there is a microprocessor which takes instructions and performs the necessary actions. Those instructions are written in a high level language and compiled into the binary machine code that the processor uses. The problem with this is that different types of processors use a different machine code, and to get a program to run on different platforms it needs to be recompiled for each.

A virtual machine, unlike a regular computer processor, is built using software, not hardware. The virtual machine is written in a high level language, and compiled to machine code as usual. However, programs that run on the virtual machine are compiled into bytecode instead of machine code. This bytecode runs on top of the virtual machine, and the virtual machine converts it into processor instructions.

Here is a table that summarizes some of the important differences between a virtual machine and a native machine:

Native Machine Virtual Machine
Implementation Hardware Software
Speed of Execution Fast Slow
Native Machine Code Programming Must compile every program into native machine code Must only compile the virtual machine into native machine code, everything else is converted to bytecode
Portability Every program must be recompiled on every new hardware platform Programs only need to be compiled into bytecode once, and can run anywhere a VM is installed
Extensibility Impossible Virtual machines can be improved, extended, patched, and added to over time

Previous Parrot Virtual Machine Next
Running Parrot Parrot Assembly Language