next up previous contents
Next: Command line parsing Up: Writing a Pass Previous: Writing a Pass   Contents

Writing a Module

Figure 4: Definition of a Module
  #include <suifkernel/suif_env.h>
  #include <suifkernel/module_subsystem.h>
  #include <suifkernel/module.h>

  class SampleModule : public Module {
    SampleModule (SuifEnv* suif_env, const LString &name);
    virtual ~SampleModule();
    Module *clone() const; 
    virtual void initialize();
    virtual void execute();

  SampleModule::SampleModule (SuifEnv* suif_env, const LString &name)
      : Module (suif_env, name) {


  Module *SampleModule::clone() const { 

  void SampleModule::initialize() {

  void SampleModule::execute() {

  extern "C" void init_sample(SuifEnv *suif_env) {
    ModuleSubSystem *ms = suif_env->get_module_subsystem();
       new SampleModule (suif_env, "sample"));

Figure 4 shows the code that you need to create a module. The high-level interface of a Module is documented in An Overview of the SUIF2 Compiler Infrastructure. The constructor simply passes down the pointer to the suif environment to the parent class together with the name of this module. The clone routine does nothing except return the current pointer since the module contains no state. The initialize function is executed at the time the module is registered. It usually defines the parameter profile, which will be described in more detail next, and performs the necessary initialization for the specific algorithm implemented. The last, and certainly the most important method is the execute routine, which does all the real work. It is executed whenever the module is invoked.

Each shared library has an initialization routine which is called when the shared library is loaded. This has the name init_< library_name>. In this example, we plan to put our example module into the sample library, hence we have the init_sample function. This library initialization routine is responsible for registering the modules defined in the library. It creates a new instance of each of the modules and registers it using the register_module method in the suif_env's ModuleSubSystem. It should also call all the initialization routines for all the shared libraries it depends upon. For example, if the module uses the suifnodes library, then it should include init_suifnodes(suif_env) in its init routine. Note that these libraries may already be initialized and, if so, the initialization will not be performed a second time.

next up previous contents
Next: Command line parsing Up: Writing a Pass Previous: Writing a Pass   Contents
SUIF Nightly Build