The peephole optimizer

Last updated 3 June 1996.

The peephole optimizer is now available for both C and Scheme.


The C version.

I have changed

    /course/com1355/Quirk13/Compiler2/Makefile
to define both qcc and pqcc as targets. qcc does not use the peephole optimizer, but pqcc does. The files that differ are
    peepmain.c   replaces  main.c
    peepemit.c   replaces  emit.c
    peephole.c   is new
    peepholes.c  is new (it can be compiled from your own rules)
    predicates.c is new (it contains C code mentioned in the rules)
The only differences between peepmain.c and main.c are that peepmain.c calls enable_peephole(1) and flush_peephole.

Examples of peephole optimizations can be found in

    /course/com1355/Quirk13/Compiler1/peepholes1.sch (for Scheme)
    /course/com1355/Quirk13/Compiler2/peepholes2.sch (for C)

Suppose that your peephole optimizations are described in peepholes. Then

    % scheme
    > (load "/course/com1355/Quirk13/Compiler2/ptranslator.sch")
    > (translate_peepholes "peepholes")
    > (exit)

will create peepholes.c, which you can then link in with your compiler. You will also need the driver routines in peephole.c, the modified routines in peepemit.c, and an appropriate version of predicates.c containing the procedures that your rules mention. Your compiler must call

    enable_peephole(1);
to begin peephole optimization, and must call
    enable_peephole(0);
or
    flush_peephole();
before it exits or emits the standard prologue.


The Scheme version

I have changed /course/com1355/Quirk13/Compiler1 to load the peephole optimizer and a few example peepholes. Peephole optimization will not be enabled until you call

    (enable_peephole #t)
If you enable the peephole optimizer, then you will need to call flush_peephole at the end of code generation, before you concatenate the standard epilogue.

You can add your own peephole optimizations to those in peepholes1.sch. See that file and the comments at the beginning of peephole.sch for more information.


Known bugs: