README.txt - Readme file for PIRC compiler.
PIRC is a fresh implementation of the PIR language using Bison and Flex. Its main features are:
- thread-safety, so it is reentrant.
- strength reduction, implemented in the parser.
- constant folding, implemented in the parser.
- checks for proper use of op arguments in PIR syntax (disallowing, e.g.: $S0 = print)
- allow multiple heredocs in subroutine invocations (like: foo(<<'A', <<'B', <<'C') )
- register usage optimization
To compile PIRC on windows using MSVC:
When running PIRC, it needs the shared library
libparrot; an easy way to do this is copy
libparrot.dll in the Parrot root directory to
Running PIRC is as easy as:
See 'pirc -h' for help.
The Makefile should work fine on Linux:
cd compilers/pirc && make
When running PIRC, it needs the shared library
libparrot; in order to let PIRC find it, set the path as follows:
Running is as easy as:
The new Bison/Flex based implementation of the PIR compiler is designed as a two-stage compiler:
The heredoc preprocessor takes the input as written by the PIR programmer, and flattens out all heredoc strings. An example is shown below to illustrate this concept:
The following input:
.sub main $S0 = <<'EOS' This is a heredoc string divided over five lines. EOS .end
is transformed into:
.sub $S0 = "This is a heredoc string\n divided\n over\n five\n lines.\n" .end
In order to allow
.included file to have heredoc strings, the heredoc preprocessor also handles the
.include directive, even though logically this is a macro function. See the discussion below for how the
.include directive works.
The PIR compiler parses the output of the heredoc preprocessor. PIRC's lexer also handles macros.
The macro layer basically implements text replacements. The following directives are handled:
.include directive takes a string argument, which is the name of a file. The contents of this file are inserted at the point where the
.include directive is written. To illustrate this, consider the following example:
main.pir: ======================== .sub main print "hi\n" foo() .end .include "lib.pir" ======================== lib.pir: ======================== .sub foo print "foo\n" .end ========================
This will result in the following output:
.sub main print "hi\n" foo() .end .sub foo print "foo\n" .end
The macro directive starts a macro definition. The macro preprocessor implements the expansion of macros. For instance, given the following input:
.macro say(msg) print .msg print "\n" .endm .sub main .say("hi there!") .end
will result in this output:
.sub main print "hi there!" print "\n" .end
.macro_const directive is similar to the
.macro directive, except that a
.macro_const is just a simplified
.macro; it merely gives a name to some constant:
.macro_const PI 3.14 .sub main print "PI is approximately: " print .PI print "\n" .end
This will result in the output:
.sub main print "PI is approximately: " print 3.14 print "\n" .end
As Parrot instructions are polymorphic, the PIR compiler is responsible for selecting the right variant of the instruction. The selection is based on the types of the operands. For instance:
set $I0, 42
will select the
set_i_ic instruction: this is the
set instruction, taking an integer (i) result operand and an integer constant (ic) operand. Other examples are:
$P0 = 42 --> set_p_kic_ic # kic = key integer constant $I0 = $P0["hi"] --> set_i_p_kc # kc = key constant from constant table $P1 = new "Hash" --> new_p_sc # sc = string constant
Expressions that can be evaluated at compile-time are pre-evaluated, saving calculations during runtime. Some constant-folding is required, as Parrot depends on this. For instance:
add $I0, 1, 2
is not a valid Parrot instruction; there is no
add_i_ic_ic instruction. Instead, this will be translated to:
set $I0, 3
which, as was explained earlier, will select the
The conditional branch instructions are also pre-evaluated, if possible. For instance, consider the following statement:
if 1 < 2 goto L1
It is clear during compile time, that 1 is smaller than 2; so instead of evaluating this during runtime, we know for sure that the branch to label
L1 will be made, effectively replacing the above statement by:
Likewise, if it's clear that certain instructions don't have any effect, they can be removed altogether:
if 1 > 2 goto L1 --> nop # nop is no opcode. $I0 = $I0 + 0 --> nop
Another type of optimization is the selection of (slightly) more efficient variants of instructions. For instance, consider the following instruction:
$I0 = $I0 + $I1
which is actually syntactic sugar for:
add $I0, $I0, $I1
In C one would write (ignoring the fact that $I0 and $I0 are not a valid C identifiers):
$I0 += $I1
which is in fact valid PIR as well. When the PIR parser sees an instruction of this form, it will automatically select the variant with 2 operands instead of the 3-operand variant. So:
add $I0, $I0, $1 # $I0 is an out operand
will be optimized, as if you had written:
add $I0, $I1 # $I0 is an in/out operand
The PIR parser can do even more improvements, if it sees opportunity to do so. Consider the following statement:
$I0 = $I0 + 1
or, in Parrot assembly syntax:
add $I0, $I0, 1
Again, in C one would write (again ignoring the valid identifier issue):
$I0++, or in other words,
incrementing the given identifier. Parrot has
dec instructions built-in as well, so that the above statement
$I0 = $I0 + 1 can be optimized to:
The PIR compiler implements a vanilla register allocator. This means that each declared
.param symbol, and each PIR register ($Px, $Sx, $Ix, $Nx) is assigned a unique PASM register, that is associated with the original symbol or PIR register throughout the subroutine.
PIRC has a register optimizer, which can optimize the register usage. Run PIRC with the
-r option to activate this. The register optimizer is implemented using a Linear Scan Register allocator.
The implementation of the vanilla register allocator is done in the PIR symbol management module (
PIRC has a register optimizer, which uses a Linear Scan Register algorithm. For each symbolic register, a live-interval object is created, which has an start and end point, indicating the first and last usage of that symbolic register in the sub. The register optimizer figures out when symbolic registers don't overlap, in which case they can use the same register (assuming they're of the same type).
Bytecode generation is done, but there is the occasional bug. These are reported in trac.parrot.org.
The directory compilers/pirc has a number of subdirectories:
- doc - contains documentation.
- heredoc - contains the implementation of the heredoc preprocessor. This is now integrated with pirc/src. It now only has a driver program to build a stand-alone heredoc preprocessor.
- src - contains the Bison/Flex implementation of PIRC
- t - for tests. Tests input is fed into Parrot after compilation, which will run the code.
- macro - contains the old implementation of the macro preprocessor. This is now integrated with pirc/src. These files are kept as a reference until the macro preprocessor in pirc/src is completed.
If you want to make changes to the lexer of parser files, you will need the Flex and/or Bison programs. There are ports available for Windows, but I don't know whether they're any good. I use Cygwin's tools.
The heredoc preprocessor is implemented in
hdocprep.l, and can be regenerated using:
cd compilers/pirc/src flex hdocprep.l
PIRC's normal lexer is implemented in
pir.l, and can be regenerated using:
cd compilers/pirc/src flex pir.l
The parser is implemented in
pir.y, and can be regenerated using:
cd compilers/pirc/src bison pir.y
pir.l from which the lexer is generated is not processable by Cygwin's default version of Flex. In order to make a reentrant lexer, a newer version is needed, which can be downloaded from the link below.
$ ./configure $ make
Then make sure to overwrite the supplied flex binary.
Having a look at this implementation would be greatly appreciated, and any resulting feedback even more :-). Please post bug reports in trac.parrot.org.
languages/PIRfor a PGE based implementation.
compilers/imcc, the current standard PIR implementation.
docs/imcc/syntax.podfor a description of PIR syntax.
docs/imcc/for more documentation about the PIR language.
docs/pdds/pdd19_pir.podfor the PIR design document.