[CORE] Make Emacs coda read-only in generated files (part of #37664).
[parrot.git] / docs / intro.pod
blob38059567d9e26f1965122fbce9188fe1186dffe5
1 # Copyright (C) 2001-2005, The Perl Foundation.
2 # $Id$
4 =head1 NAME
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).
44 =head2 Design goals
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
52 program.
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
59 languages.
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
87 more.
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.
99 =head2 PMCs
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
106 needed.
108 =head2 Garbage Collection
110 Parrot provides garbage collection, meaning that Parrot programs 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
131 typing:
133   perl Configure.pl
135 Once this is complete, run the C<make> program (sometimes called C<nmake> or
136 C<dmake> or C<gmake>).  This should complete, giving you a working Parrot
137 executable.
139 Please report any problems that you encounter while building Parrot so the
140 developers can fix them. You can do this by sending a message to
141 C<parrotbug@parrotcode.org> containing a description of your problem. Please
142 include the F<myconfig> file that was generated as part of the build process
143 and any errors that you observed.
145 =head2 The Parrot test suite
147 Parrot has an extensive regression test suite. This can be run by typing:
149   make test
151 Substituting make for the name of the make program on your platform. The output
152 will look something like this:
154  C:\Perl\bin\perl.exe t\harness --gc-debug --running-make-test
155    t\library\*.t  t\op\*.t  t\pmc\*.t  t\run\*.t  t\native_pbc\*.t
156    imcc\t\*\*.t  t\dynpmc\*.t  t\p6rules\*.t t\src\*.t t\perl\*.t
157  t\library\dumper...............ok
158  t\library\getopt_long..........ok
159  ...
160  All tests successful, 4 test and 71 subtests skipped.
161  Files=163, Tests=2719, 192 wallclock secs ( 0.00 cusr +  0.00 csys =  0.00 CPU)
163 It is possible that a number of tests may fail. If this is a small number, then
164 it is probably little to worry about, especially if you have the latest Parrot
165 sources from the SVN repository. However, please do not let this discourage you
166 from reporting test failures, using the same method as described for reporting
167 build problems.
170 =head1 Some simple Parrot programs
172 =head2 Hello world!
174 Create a file called F<hello.pir> that contains the following code.
176   .sub main
177       print "Hello world!\n"
178   .end
180 Then run it by typing:
182   parrot hello.pir
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.
193 =head2 Using registers
195 We can modify hello.pir to first store the string C<Hello world!\n> in a
196 register and then use that register with the print instruction.
198   .sub main
199       set S0, "Hello world!\n"
200       print S0
201   .end
203 Here we have stated exactly which register to use. However, by
204 replacing C<S0> with C<$S0> we can delegate the choice of which
205 register to use to Parrot.  It is also possible to use an C<=>
206 notation instead of writing the C<set> instruction.
208   .sub main
209       $S0 = "Hello world!\n"
210       print $S0
211   .end
213 To make PIR even more readable, named registers can be used. These are later
214 mapped to real numbered registers.
216   .sub main
217       .local string hello
218       hello = "Hello world!\n"
219       print hello
220   .end
222 The C<.local> directive indicates that the named register is only needed inside
223 the current compilation unit (that is, between C<.sub> and C<.end>). Following
224 C<.local> is a type. This can be C<int> (for I registers), C<float> (for N
225 registers), C<string> (for S registers), C<pmc> (for P registers) or the name
226 of a PMC type.
228 =head2 PIR vs. PASM
230 PASM does not handle register allocation or provide support for named
231 registers.  It also does not have the C<.sub> and C<.end> directives, instead
232 replacing them with a label at the start of the instructions.
234 =head2 Summing squares
236 This example introduces some more instructions and PIR syntax. Lines starting
237 with a C<#> are comments.
239   .sub main
240       # State the number of squares to sum.
241       .local int maxnum
242       maxnum = 10
244       # We'll use some named registers. Note that we can declare many
245       # registers of the same type on one line.
246       .local int i, total, temp
247       total = 0
249       # Loop to do the sum.
250       i = 1
251   loop:
252       temp = i * i
253       total += temp
254       inc i
255       if i <= maxnum goto loop
257       # Output result.
258       print "The sum of the first "
259       print maxnum
260       print " squares is "
261       print total
262       print ".\n"
263   .end
265 PIR provides a bit of syntactic sugar that makes it look more high level than
266 assembly. For example:
268   temp = i * i
270 Is just another way of writing the more assembly-ish:
272   mul temp, i, i
274 And:
276   if i <= maxnum goto loop
278 Is the same as:
280   le i, maxnum, loop
282 And:
284   total += temp
286 Is the same as:
288   add total, temp
290 As a rule, whenever a Parrot instruction modifies the contents of a register,
291 that will be the first register when writing the instruction in assembly form.
293 As is usual in assembly languages, loops and selection are implemented in terms
294 of conditional branch statements and labels, as shown above. Assembly
295 programming is one place where using goto is not bad form!
297 =head2 Recursively computing factorial
299 In this example we define a factorial function and recursively call it to
300 compute factorial.
302   .sub factorial
303       # Get input parameter.
304       .param int n
306       # return (n > 1 ? n * factorial(n - 1) : 1)
307       .local int result
309       if n > 1 goto recurse
310       result = 1
311       goto return
313   recurse:
314       $I0 = n - 1
315       result = factorial($I0)
316       result *= n
318   return:
319       .return (result)
320   .end
323   .sub main :main
324       .local int f, i
326       # We'll do factorial 0 to 10.
327       i = 0
328   loop:
329       f = factorial(i)
331       print "Factorial of "
332       print i
333       print " is "
334       print f
335       print ".\n"
337       inc i
338       if i <= 10 goto loop
339   .end
341 The first line, C<.param int n>, specifies that this subroutine takes one
342 integer parameter and that we'd like to refer to the register it was passed in
343 by the name C<n> for the rest of the sub.
345 Much of what follows has been seen in previous examples, apart from the line
346 reading:
348   result = factorial($I0)
350 This single line of PIR actually represents a few lines of PASM. The assembler
351 builds a PMC that describes the signature, including which register the
352 arguments are held in. A similar process happens for providing the registers
353 that the return values should be placed in. Finally, the C<factorial> sub is 
354 invoked.
356 Right before the C<.end> of the C<factorial> sub, a C<.return> directive is
357 used to specify that the value held in the register named C<result> is to be
358 copied to the register that the caller is expecting the return value in.
360 The call to C<factorial> in main works in just the same was as the recursive
361 call to C<factorial> within the sub C<factorial> itself. The only remaining
362 bit of new syntax is the C<:main>, written after C<.sub main>. By default,
363 PIR assumes that execution begins with the first sub in the file. This 
364 behavior can be changed by marking the sub to start in with C<:main>.
366 =head2 Compiling to PBC
368 To compile PIR to bytecode, use the C<-o> flag and specify an output file with
369 the extension F<.pbc>.
371   parrot -o factorial.pbc factorial.pir
373 =head1 Where next?
375 =head2 Documentation
377 What documentation you read next depends upon what you are looking to do with
378 Parrot. The opcodes reference and built-in PMCs reference are useful to dip
379 into for pretty much everyone. If you intend to write or compile to PIR then
380 there are a number of documents about PIR that are worth a read. For compiler
381 writers, the Compiler FAQ is essential reading. If you want to get involved
382 with Parrot development, the PDDs (Parrot Design Documents) contain some
383 details of the internals of Parrot; a few other documents fill in the gaps. One
384 way of helping Parrot development is to write tests, and there is a document
385 entitled I<Testing Parrot> that will help with this.
387 =head2 The Parrot Mailing List
389 Much Parrot development and discussion takes place on the
390 parrot-porters mailing list. You can subscribe by sending an email to
391 C<parrot-porters-subscribe@perl.org> or read the NNTP archive at
392 L<http://www.nntp.perl.org/group/perl.perl6.internals>.
394 =head2 IRC
396 The Parrot IRC channel is hosted on irc.perl.org and is named C<#parrot>.
397 Alternative IRC servers are at irc.pobox.com and irc.rhizomatic.net.
399 =cut