next up previous
Next: Predefined Phases Up: The Basics of C-Breeze Previous: The Basics of C-Breeze

User-Defined Phases

User-defined phases can be named and invoked from the command line. To do this, define a class to be a subclass of Phase ane define a run() method that contains the code to run when the phase is invoked. For example, the trivial phase below prints Hello, World! to the standard output.

#include "c_breeze.h"

class Hello: public Phase {
        public:
        void run (void) {
                cout << "Hello, World!\n";
        }
};

To name this phase and register it as a flag that can be invoked from the command line, declare a global variable of type Phases (note the plural) constructed from the name of the phase (represented as a character string) and an instance of your subclass of Phase. In our Hello example, the following line registers a new phase that is invoked with the -hello flag.

 
Phases hello_phase ("hello", new Hello());

Here, the identifier hello_phase is a dummy variable that is never referenced; it is the invocation of the constructor that registers the phase. To define more than one phase in a file, you should obviously give them different names. It's not necessary for the names of the Phases variable, the subclass of Phase, or the identifying string to be related, but it does contribute to the readability of your code.

To create useful phases, you will want to access C-Breeze's various internal representations. These are available through the CBZ class, which is described in detail in Section 5. This class has several public and static members which can be accessed directly. Of particular interest is CBZ::Program. Each element on this STL list contains the C-Breeze representation for a translation unit (or source file). Most phases will traverse this list, applying some analyses or transformations to each unit.

For details on compiling C-Breeze, please see the release notes that accompany the source code. These notes include details on the required versions of gcc and bison.


next up previous
Next: Predefined Phases Up: The Basics of C-Breeze Previous: The Basics of C-Breeze
Adam C. Brown 2006-01-26