How Rakudo compiles a Raku program **OBSOLETE**

# How Rakudo compiles a Raku program OBSOLETE

## Parser and Action Methods

The Raku source code is transformed in various stages, of which the first two are the Parser and Action Method stages. The Parser creates a parse tree out of the Raku source code and then gives control to appropriate action methods that annotate the parse tree, incrementally turning it into an Abstract Syntax Tree (AST). When an action method is done annotating, control is handed back to the parser, which then continues parsing the Raku code and "fire off" new action methods as it goes.

The result of these two stages interacting is an "improved PAST" (Perl 6 Abstract Syntax Tree) called QAST. This tree is then passed on to the QAST compiler.

The parser and action methods are implemented in "Not Quite Perl" (NQP) and are part of Rakudo and hosted in the Rakudo repository at src/Perl6/Grammar.nqp and src/Perl6/Actions.nqp.

## The World

The World is where the parser and the action methods store any declarations they encounter during their runs, including Classes, Types, Signatures, Constants, Subs and Methods.

## QAST compiler

The QAST compiler transforms the abstract syntax tree into a PIRT (Parrot Intermediate Representation Tree). To do this, the QAST compiler does a series of translations on the AST, creating PIRT nodes that implement the operations specified by the QAST nodes.

In addition, the QAST compiler is responsible for serializing The World in such a way that later stages can get access to the declarations stored there during the parser and action methods stages.

There's also opportunity to apply some VM-specific optimizations at this point. When this is done, the resulting PIRT is passed to the PIRT serializer.

This stage is described in the different files in the nqp/src/QAST/ directory.

## PIRT serializer

The PIRT serializer "squashes" the PIR Tree into a format that can be passed to Parrot itself and it's IMCC (InterMediate Code Compiler) stage.

You can read more about this at nqp/src/QAST/PIRT.nqp.

## IMCC and Parrot runtime

The IMCC (InterMediate Code Compiler) receives the PIR code from the PIRT serializer and then transforms it into Parrot Byte Code (PBC). IMCC is parrot's PIR compiler, written in C and statically linked into parrot. The byte code can then be stored to disk or executed in memory by one of the run cores available as part of the Parrot runtime. This is in some sense the heart of Parrot - or one of the hearts; There are several different cores available, including one for just-in-time compilation (JIT), one for debugging and others.

You can find out more about the IMCC in the parrot/docs/imcc/ directory, and about the different run cores in the parrot/docs/running.pod

## PMCs and dynops

There are also some supporting custom types and operations in Rakudo called dynamic PMCs and dynamic ops (dynops) which are written in C, and helper functions written in NQP and PIR. These supporting libraries exist for adding features to Parrot that are needed to handle special features in Raku.

## Core setting library

The core settings library is the library containing the methods, classes and almost all other features that make up the Rakudo Raku implementation. This library is tightly coupled with the `raku` binary, and loaded by default every time `raku` is run.

## Glossary

NQP
  Not Quite Perl, a small subset of Raku that is used for tree transformations in compilers.
PIR
  Parrot Intermediate Representation, the most commonly used for of parrot assembly (which is still high-level enough to be written by humans).
IMCC
  InterMediate Code Compiler, the part of parrot that compiles PIR into byte code.
PBC
  Parrot Byte Code, the binary form to which all parrot programs are compiled in the end.
Core setting
  The core setting is the Raku standard library. It is part of the raku executable, and contains all the standard features available in Raku.
QAST
  The "improved" Abstract Syntax Tree used in Rakudo. It contains information about how the program is structured, and what it is supposed to do.
PIRT
  Parrot Intermediate Representation Tree.