The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

pmc2c.pl - PMC compiler

SYNOPSIS

perl pmc2c.pl [--no-lines] foo.pmc [foo2.pmc...]

The first class.pmc should be the name of the class you wish to create. Normally, the pmc2c.pl translator uses #line pragmas to tell the C compiler where each function in the .pmc file begins (line number). This allows the compiler to issue warnings and errors based on the .pmc file instead of on the .c file which should not be edited. However, there are times when this is not desirable and therefore the --no-lines option is provided.

DESCRIPTION

The pmc2c program's job is to take .pmc files and create .c files which can be compiled for use with the Parrot interpreter.

First, the program determines the names of the .c and .h files from the basename of the .pmc file (e.g. perlint.pmc -> perlint.c and perlint.h). Next, the file is searched for /pmclass \w*/ which attempts to find the class being declared. Once the class is found, all of its superclasses are scanned and their methods added to the methods of the current PMC. PMCs default to inheriting from 'default'. Only single inheritance is supported. Once the superclass is determined, it is processed and its method names are extracted and saved. Next, each method body is processed with various directives (see below) getting replaced by their appropriate values. Finally, the .c and .h files are generated. The appropriate base class header files are included. If the noinit flag was used, then no init function is generated, otherwise one is generated which sets up the vtable and enters it into the Parrot_base_vtables array. The .c file is generated by appending the functions after the various directives have been replaced.

PMC FILE SYNTAX

The basic syntax of a PMC file is

  1. A preamble, consisting of code to be copied directly to the .c file

  2. pmclass PMCNAME [extends PMCNAME] [abstract] [extension] [noinit] {

  3. A list of vtable method implementations

  4. The final close }

METHOD BODY SUBSTITUTIONS

The vtable method bodies can use the following substitutions:

  • SELF - Converted to the current PMC object of type PMC*.

  • INTERP - Converted to the interpreter object.

  • SELF.method(a,b,c) - calls the vtable method 'method' using the static type of SELF (in other words, calls another method defined in the same file).

  • DYNSELF.method(a,b,c) - calls the vtable method 'method' using the dynamic type of SELF

  • DYNSELF(a,b,c) - same as above, but calls the current method

  • SUPER(a,b,c) - calls the overridden implementation of the current method in the nearest superclass, using the static type of SELF.

  • DYNSUPER(a,b,c) - as above, but uses the actual dynamic type of SELF.

INTERNAL METHODS

extract_balanced

This function's purpose is to extract the C code between the opening and closing brace of a function definition. For example, the function

  void f( int x ) {
    if( x == 9 ) {
      printf( "Hello!" );
    }
    else {
      printf( "Goodbye!" );
    }
  }

would generate a return value of: q{ if( x == 9 ) { printf( "Hello!" ); } else { printf( "Goodbye!" ); } }

It will actually return a triple consisting of the above, the passed in string with the above removed, and the current line number after the above has been removed.

parse_superpmc

This function looks for a superclass declaration in the current pmc class. If none was found, it assumes that default is the superclass. It then reads in the class definition for the superclass and remembers the method names. It returns an array ref to the method names and the name of the superclass that was analyzed.

superpmc_info

This function opens the file containing the superclass reads in the data and calls parse_superpmc().

scan_inheritance_tree

This function repeatedly calls superpmc_info passing in the current class name. superpmc_info will return a tuple containing all of the defined methods (not default) in that class as well as the name of the superclass that was processed. This function stops when the default superclass is processed. It returns a hash that maps the method name to the most derived class it was defined in.

count_newlines

return the number of newlines in the current string

filter

The filter function choreographs the previous functions actions on the pmcfile in question. It first scans the inheritance hierarchy to get all of the methods and their corresponding class of definition. Next, it skips over the extends clause and processes any flags (such as noinit). Afterwards, it loops through each function declared and replaces directives with the appropriate values. Finally, it generates the .c and .h files for the .pmc file being analyzed.

LICENSE

This program is free software. It is subject to the same license as the Parrot interpreter.