XFPT, an XML/XSLT Extension to the FPT compiler -- by Yijun Yu

After dumping the intermediate representation (IR) of a compiler into XML, one can not only exchange the IR with external tools, but also perform compiler transformations that was originally only possible internal to the compiler. To illustrate, I explain the process through a simple transformation -- Loop Unrolling.

Usage

Preparation

  1. Here is the original program (e.g. matrix multiplication ) in FORTRAN.

XML Generation

  1. Adding a directive
  2. Generating the XML representation for the FORTRAN IR
  3. fpt -X mxm.f 
    
    Note 1. Sorry the FPT compiler is not open-sourced yet. For those at Ghent university, you can check out the source code through the following CVS location:
    :extssh:epicmp.elis.rug.ac.be:/sfroot/cvs/fpt
    
    TODO. I may put a binary program here soon.
    Note 2. This FORTRAN IR is designed for AST used internal to the FPT compiler, thus it is different from the parsing tree generated from the YAXX project, although the same YACC rules were used. (I donated the FORTRAN yacc rules to the YAXX project.)
    Here is the output of the XML representation (Abstract Syntax Tree) .

Verify the XML IR through a stylesheet

  1. The IR can be translated back into the original language using the following AST stylesheet
  2. xsltproc ast.xsl mxm.xml > mxm-ast.f
    
    Here are example FORTRAN output.

Unrolling the loop using the XSLT translation

  1. The first xsl stylesheet unrolls the loop body by N (specified by the directive C$unroll < N >) times:
  2. xsltproc unroll0.xsl mxm.xml > mxm1.xml
    xsltproc ast.xsl mxm1.xml > mxm1.f
    
    Here is the example XML output (if your browser can not show it because of the default stylesheet, try to look at the textual version of the XML output)and here is the corresponding FORTRAN output.
  3. The second xsl stylesheet computes the loop indices inside each loop body
  4. xsltproc [unroll1.xsl] mxm1.xml > mxm2.xml
    xsltproc [ast.xsl] mxm2.xml > mxm2.f
    
    Note 3. The 1st argument to the above commands is optional, because the XML files have already a proper processing instruction generated to refer to these stylesheets. So the default stylesheet for XSLTPROC does not need to be specified. Here is the example XML output (if your browser can not show it because of the default stylesheet, try to look at the textual version of the XML output) and here is the corresponding FORTRAN output.

Development

Download

References


Design decisions

Why create two stylesheets for the "single" unrolling transformation?

In XSLT processing, the node-set of the generated XML document can not be changed, because XSL stylesheet is FUNCTIONAL PROGRAMMING, that is, one can not change the value of named variables! Therefore, a workaround is invented (I am not aware who else do the same) to create two stylesheets, one of them generating an XML file with additional processing instruction to refer to another stylesheet. Then the 2nd stylesheet could work on the output of the first one to "modify" it. In software engineering terminology, this design is called "Pipe-and-Filter" architecture, following the example of the design of Unix (made of many simple utilities such as cat, sed, sort, uniq, etc, and combine them through pipes into a more complex utility) .

ChangeLog

Date: Aug 16, 2006