Parrot Virtual Machine/Parrot Programming
Parrot Programming Edit
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:
- Parrot Assembly Language (PASM). This is the lowest human-readable way to program Parrot, and is very similar to traditional assembly languages.
- Parrot Intermediate Representation (PIR). This is a higher-level language which is easier to program in than PASM, and significantly more common.
- 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.
Programming Steps Edit
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.
- PASM and PIR
- A program written in PASM or PIR, such as
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):
- 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:
- 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
Foo.plfor example (".pl" is the file extension for Perl programs), you would type:
./parrot languages/perl6/perl6.pbc Foo.pl
- This runs the Perl 6 compiler on Parrot, and passes the file name
Foo.plto the compiler. To output a file into PIR or PBC, you would use the
--target=option to specify an output format.
Virtual Machines? Edit
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|
|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|