1 # Copyright (C) 2001-2005, The Perl Foundation.
6 docs/intro.pod - The Parrot Primer
8 =head1 Welcome to Parrot
10 This document provides a gentle introduction to the Parrot virtual machine for
11 anyone considering writing code for Parrot by hand, writing a compiler that
12 targets Parrot, getting involved with Parrot development or simply wondering
13 what on earth Parrot is.
15 =head1 What is Parrot?
17 =head2 Virtual Machines
19 Parrot is a virtual machine. To understand what a virtual machine is, consider
20 what happens when you write a program in a language such as Perl, then run it
21 with the applicable interpreter (in the case of Perl, the perl executable).
22 First, the program you have written in a high level language is turned into
23 simple instructions, for example I<fetch the value of the variable named x>,
24 I<add 2 to this value>, I<store this value in the variable named y>, etc. A
25 single line of code in a high level language may be converted into tens of
26 these simple instructions. This stage is called I<compilation>.
28 The second stage involves executing these simple instructions. Some languages
29 (for example, C) are often compiled to instructions that are understood by the
30 CPU and as such can be executed by the hardware. Other languages, such as Perl,
31 Python and Java, are usually compiled to CPU-independent instructions. A
32 I<virtual machine> (sometimes known as an I<interpreter>) is required to
33 execute those instructions.
35 While the central role of a virtual machine is to efficiently execute
36 instructions, it also performs a number of other functions. One of these is to
37 abstract away the details of the hardware and operating system that a program
38 is running on. Once a program has been compiled to run on a virtual machine, it
39 will run on any platform that the VM has been implemented on. VMs may also
40 provide security by allowing more fine-grained limitations to be placed on a
41 program, memory management functionality and support for high level language
42 features (such as objects, data structures, types, subroutines, etc).
46 Parrot is designed with the needs of dynamically typed languages (such as Perl
47 and Python) in mind, and should be able to run programs written in these
48 languages more efficiently than VMs developed with static languages in mind
49 (JVM, .NET). Parrot is also designed to provide interoperability between
50 languages that compile to it. In theory, you will be able to write a class in
51 Perl, subclass it in Python and then instantiate and use that subclass in a Tcl
54 Historically, Parrot started out as the runtime for Perl 6. Unlike Perl 5, the
55 Perl 6 compiler and runtime (VM) are to be much more clearly separated. The
56 name I<Parrot> was chosen after the 2001 April Fool's Joke which had Perl and
57 Python collaborating on the next version of their languages. The name reflects
58 the intention to build a VM to run not just Perl 6, but also many other
62 =head1 Parrot concepts and jargon
64 =head2 Instruction formats
66 Parrot can currently accept instructions to execute in four forms. PIR (Parrot
67 Intermediate Representation) is designed to be written by people and generated
68 by compilers. It hides away some low-level details, such as the way parameters
69 are passed to functions. PASM (Parrot Assembly) is a level below PIR - it is
70 still human readable/writable and can be generated by a compiler, but the
71 author has to take care of details such as calling conventions and register
72 allocation. PAST (Parrot Abstract Syntax Tree) enables Parrot to accept an
73 abstract syntax tree style input - useful for those writing compilers.
75 All of the above forms of input are automatically converted inside Parrot to
76 PBC (Parrot Bytecode). This is much like machine code, but understood by the
77 Parrot interpreter. It is not intended to be human-readable or human-writable,
78 but unlike the other forms execution can start immediately, without the need
79 for an assembly phase. Parrot bytecode is platform independent.
81 =head2 The instruction set
83 The Parrot instruction set includes arithmetic and logical operators, compare
84 and branch/jump (for implementing loops, if...then constructs, etc), finding
85 and storing global and lexical variables, working with classes and objects,
86 calling subroutines and methods along with their parameters, I/O, threads and
89 =head2 Registers and fundamental data types
91 The Parrot VM is register based. This means that, like a hardware CPU, it has a
92 number of fast-access units of storage called registers. There are 4 types of
93 register in Parrot: integers (I), numbers (N), strings (S) and PMCs (P). There
94 are N of each of these, named I0,I1,..N0.., etc. Integer registers are the
95 same size as a word on the machine Parrot is running on and number registers
96 also map to a native floating point type.
97 The amount of registers needed is determined per subroutine at compile-time.
101 PMC stands for Parrot Magic Cookie. PMCs represent any complex data structure
102 or type, including aggregate data types (arrays, hash tables, etc). A PMC can
103 implement its own behavior for arithmetic, logical and string operations
104 performed on it, allowing for language-specific behavior to be introduced. PMCs
105 can be built in to the Parrot executable or dynamically loaded when they are
108 =head2 Garbage Collection
110 Parrot provides garbage collection, meaning that Parrot programs to do not need
111 to free memory explicitly; it will be freed when it is no longer in use (that
112 is, no longer referenced) whenever the garbage collector runs.
115 =head1 Obtaining, building and testing Parrot
117 =head2 Where to get Parrot
119 Periodically, numbered releases will appear on CPAN. At this stage of the
120 project, an awful lot is changing between releases. You can get a copy of the
121 latest Parrot from the SVN repository. This is done as follows:
123 svn co https://svn.perl.org/parrot/trunk parrot
125 You can find more instructions at: L<http://www.parrotcode.org/source.html>
127 =head2 Building Parrot
129 The first step to building Parrot is to run the F<Configure.pl> program, which
130 looks at your platform and decides how Parrot should be built. This is done by
135 Once this is complete, run the C<make> program (sometimes called C<nmake> or
136 C<dmake>). This should complete, giving you a working Parrot executable.
138 Please report any problems that you encounter while building Parrot so the
139 developers can fix them. You can do this by sending a message to
140 C<bugs-parrot@bugs6.perl.org> containing a description of your problem. Please
141 include the F<myconfig> file that was generated as part of the build process
142 and any errors that you observed.
144 =head2 The Parrot test suite
146 Parrot has an extensive regression test suite. This can be run by typing:
150 Substituting make for the name of the make program on your platform. The output
151 will look something like this:
153 C:\Perl\bin\perl.exe t\harness --gc-debug --running-make-test
154 t\library\*.t t\op\*.t t\pmc\*.t t\run\*.t t\native_pbc\*.t
155 imcc\t\*\*.t t\dynpmc\*.t t\p6rules\*.t t\src\*.t t\perl\*.t
156 t\library\dumper...............ok
157 t\library\getopt_long..........ok
159 All tests successful, 4 test and 71 subtests skipped.
160 Files=163, Tests=2719, 192 wallclock secs ( 0.00 cusr + 0.00 csys = 0.00 CPU)
162 It is possible that a number of tests may fail. If this is a small number, then
163 it is probably little to worry about, especially if you have the latest Parrot
164 sources from the SVN repository. However, please do not let this discourage you
165 from reporting test failures, using the same method as described for reporting
169 =head1 Some simple Parrot programs
173 Create a file called F<hello.pir> that contains the following code.
176 print "Hello world!\n"
180 Then run it by typing:
184 As expected, this will display the text C<Hello world!> on the console,
185 followed by a new line (due to the C<\n>).
187 Let's take the program apart. C<.sub _main> states that the instructions that
188 follow make up a subroutine named C<_main>, until a C<.end> is encountered. The
189 second line contains the C<print> instruction. In this case, we are calling the
190 variant of the instruction that accepts a constant string. The assembler takes
191 care of deciding which variant of the instruction to use for us. The third line
192 contains the C<end> instruction, which causes the interpreter to terminate.
194 =head2 Using registers
196 We can modify hello.pir to first store the string C<Hello world!\n> in a
197 register and then use that register with the print instruction.
200 set S0, "Hello world!\n"
205 Here we have stated exactly which register to use. However, by
206 replacing C<S0> with C<$S0> we can delegate the choice of which
207 register to use to Parrot. It is also possible to use an C<=>
208 notation instead of writing the C<set> instruction.
211 $S0 = "Hello world!\n"
216 To make PIR even more readable, named registers can be used. These are later
217 mapped to real numbered registers.
221 hello = "Hello world!\n"
226 The C<.local> directive indicates that the named register is only needed inside
227 the current compilation unit (that is, between C<.sub> and C<.end>). Following
228 C<.local> is a type. This can be C<int> (for I registers), C<float> (for N
229 registers), C<string> (for S registers), C<pmc> (for P registers) or the name
234 PIR can be turned into PASM by running:
236 parrot -o hello.pasm hello.pir
238 The PASM for the final example looks like this:
241 set S30, "Hello world!\n"
245 PASM does not handle register allocation or provide support for named
246 registers. It also does not have the C<.sub> and C<.end> directives, instead
247 replacing them with a label at the start of the instructions.
249 =head2 Summing squares
251 This example introduces some more instructions and PIR syntax. Lines starting
252 with a C<#> are comments.
255 # State the number of squares to sum.
259 # Some named registers we'll use. Note how we can declare many
260 # registers of the same type on one line.
261 .local int i, total, temp
264 # Loop to do the sum.
270 if i <= maxnum goto loop
273 print "The sum of the first "
281 PIR provides a bit of syntactic sugar that makes it look more high level than
282 assembly. For example:
286 Is just another way of writing the more assembly-ish:
292 if i <= maxnum goto loop
306 As a rule, whenever a Parrot instruction modifies the contents of a register,
307 that will be the first register when writing the instruction in assembly form.
309 As is usual in assembly languages, loops and selection are implemented in terms
310 of conditional branch statements and labels, as shown above. Assembly
311 programming is one place where using goto is not bad form!
313 =head2 Recursively computing factorial
315 In this example we define a factorial function and recursively call it to
319 # Get input parameter.
322 # return (n > 1 ? n * _fact(n - 1) : 1)
325 if n > 1 goto recurse
342 # We'll do factorial 0 to 10.
347 print "Factorial of "
360 The first line, C<.param int n>, specifies that this subroutine takes one
361 integer parameter and that we'd like to refer to the register it was passed in
362 by the name C<n> for the rest of the sub.
364 Much of what follows has been seen in previous examples, apart from the line
369 This single line of PIR actually represents quite a few lines of PASM. First,
370 the value in register C<$I0> is moved into the appropriate register for it to
371 be received as an integer parameter by the C<_fact> function. Other calling
372 related registers are then set up, followed by C<_fact> being invoked. Then,
373 once C<_fact> returns, the value returned by C<_fact> is placed into the
374 register given the name C<result>.
376 Right before the C<.end> of the C<_fact> sub, a C<.return> directive is used to
377 ensure the value held in the register named C<result> is placed into the
378 correct register for it to be seen as a return value by the code calling the
381 The call to C<_fact> in main works in just the same was as the recursive call
382 to C<_fact> within the sub C<_fact> itself. The only remaining bit of new
383 syntax is the C<:main>, written after C<.sub _main>. By default, PIR assumes
384 that execution begins with the first sub in the file. This behavior can be
385 changed by marking the sub to start in with C<:main>.
387 =head2 Compiling to PBC
389 To compile PIR to bytecode, use the C<-o> flag and specify an output file with
390 the extension F<.pbc>.
392 parrot -o factorial.pbc factorial.pir
398 What documentation you read next depends upon what you are looking to do with
399 Parrot. The opcodes reference and built-in PMCs reference are useful to dip
400 into for pretty much everyone. If you intend to write or compile to PIR then
401 there are a number of documents about PIR that are worth a read. For compiler
402 writers, the Compiler FAQ is essential reading. If you want to get involved
403 with Parrot development, the PDDs (Parrot Design Documents) contain some
404 details of the internals of Parrot; a few other documents fill in the gaps. One
405 way of helping Parrot development is to write tests, and there is a document
406 entitled I<Testing Parrot> that will help with this.
408 =head2 The Parrot Mailing List
410 Much Parrot development and discussion takes place on the
411 parrot-porters mailing list. You can subscribe by sending an email to
412 C<parrot-porters-subscribe@perl.org> or read the NNTP archive at
413 L<http://www.nntp.perl.org/group/perl.perl6.internals>.
417 The Parrot IRC channel is hosted on irc.perl.org and is named C<#parrot>.
418 Alternative IRC servers are at irc.pobox.com and irc.rhizomatic.net.