Search

1/19/2011

Parrot - The Parrot Primer

Parrot - The Parrot Primer

Virtual Machines

Parrot is a virtual machine. To understand what a virtual machine is, consider what happens when you write a program in a language such as Perl, then run it with the applicable interpreter (in the case of Perl, the perl executable). First, the program you have written in a high level language is turned into simple instructions, for example fetch the value of the variable named x, add 2 to this value, store this value in the variable named y, etc. A single line of code in a high level language may be converted into tens of these simple instructions. This stage is called compilation.

The second stage involves executing these simple instructions. Some languages (for example, C) are often compiled to instructions that are understood by the CPU and as such can be executed by the hardware. Other languages, such as Perl, Python and Java, are usually compiled to CPU-independent instructions. A virtual machine (sometimes known as an interpreter) is required to execute those instructions.

While the central role of a virtual machine is to efficiently execute instructions, it also performs a number of other functions. One of these is to abstract away the details of the hardware and operating system that a program is running on. Once a program has been compiled to run on a virtual machine, it will run on any platform that the VM has been implemented on. VMs may also provide security by allowing more fine-grained limitations to be placed on a program, memory management functionality and support for high level language features (such as objects, data structures, types, subroutines, etc).

Design goals
Parrot is designed with the needs of dynamically typed languages (such as Perl and Python) in mind, and should be able to run programs written in these languages more efficiently than VMs developed with static languages in mind (JVM, .NET). Parrot is also designed to provide interoperability between languages that compile to it. In theory, you will be able to write a class in Perl, subclass it in Python and then instantiate and use that subclass in a Tcl program.

Historically, Parrot started out as the runtime for Perl 6. Unlike Perl 5, the Perl 6 compiler and runtime (VM) are to be much more clearly separated. The name Parrot was chosen after the 2001 April Fool's Joke which had Perl and Python collaborating on the next version of their languages. The name reflects the intention to build a VM to run not just Perl 6, but also many other languages.

Instruction formats
Parrot can currently accept instructions to execute in four forms. PIR (Parrot Intermediate Representation) is designed to be written by people and generated by compilers. It hides away some low-level details, such as the way parameters are passed to functions. PASM (Parrot Assembly) is a level below PIR - it is still human readable/writable and can be generated by a compiler, but the author has to take care of details such as calling conventions and register allocation. PAST (Parrot Abstract Syntax Tree) enables Parrot to accept an abstract syntax tree style input - useful for those writing compilers.

All of the above forms of input are automatically converted inside Parrot to PBC (Parrot Bytecode). This is much like machine code, but understood by the Parrot interpreter. It is not intended to be human-readable or human-writable, but unlike the other forms execution can start immediately, without the need for an assembly phase. Parrot bytecode is platform independent.

http://www.programmersheaven.com/2/Parrot-a-Virtual-Machine-For-Everyone

The 'Parrot' Software-CPU instruction set includes arithmetic and logical operators, compare and ranch/jump (for implementing loops, if...then constructs, etc), finding and storing global and lexical variables, working with classes and objects, calling subroutines and methods along with their parameters, I/O, threads and more. As we mentioned earlier that the 'Parrot' VM is register based. So we should expect number of fast-access storing units called registers in the 'Parrot' Software-CPU. Our guess is very correct, there are 4 types of register, 32 each in numbers, in 'Parrot': integers (I), numbers (N), strings (S) and PMCs (P). These registers are named I0, N0, S0, P0 ....... I31, N31, S31, P31 etc. Integer registers are the same size as a word ( 32 bit on 32 bit CPUs and 64 bit on 64 bit CPUs) on the machine 'Parrot' is running on and number registers also map to a native floating point type.

Portable Programming Languages Using "Parrot"
As we have learnt that 'Virtual Machines' provide portability across the different machine architectures. So if someone wants to design a portable programming language that provides "Write Once, Run Anywhere" model then 'Parrot' fulfills this purpose very intelligently. In fact you can study about many concept languages written for 'Parrot' runtime in the 'languages' subdirectory.

Let's now discuss more about using 'Parrot' as a run time engine for custom designed portable languages. First we have to define a language with all its constructs and grammar, then we design a parser that takes source code of the language and creates a syntax tree of that source. As described above, we can convert the output of the parser in PAST format and then use 'Parrot' as runtime engine for our custom portable language. Although it's very simple layout of the total plan because we have to implement the optimization at the various stages also. This total scheme is shown in Fig-3.

沒有留言: