next up previous contents
Next: Hoof Files in Detail Up: Generating SUIF Intermediate Representation Previous: Creating Objects   Contents

Implementation of IR Modules

The my.cpp file, shown in Figure 9, contains the implementation of the module. The module is implemented as a subclass of the Module class. The initialize method, which is called when the module is registered, creates an instance of the object factory for this module and adds it to the list in the Suif environment.

Next are two extern declarations for the basicnodes and suifnodes modules. These are generated from the Hoof files basic.hoof and suif.hoof and contain the base classes for the SUIF Intermediate Representation. The init_mynodes routine initializes these modules before creating and instance of MyModule and registering it. This occurs because of the import lines in the Hoof file.

Next in the my.cpp file you will see various bodies for methods from the Example class, shown in Figure 9. These are straightforward. The print routine produces a simple print of the class and is primarily for use in debugging. FormattedText is a class that pretty prints the output with suitable indentation of fields.

Figure 9: The Example Methods from my.cpp
  static const LString example_class_name("Example");

  const LString &Example::get_class_name() {
    return example_class_name;
    }
  int Example::get_x() const{ 
    return _x;

    }
  void Example::set_x(int the_value) {
    _x = the_value;
    }

  void Example::constructor_function( Address address ) {
    new (address) Example;
    }

  Example::Example() {}
  Example::~Example() {
    }
  void Example::print(FormattedText &x) const
    {
    x.start_block("{Example}");
    x.start_block("x=");
    x.set_value(_x);
    x.end_block();

    x.end_block();
    }

Figure 10: My Object Factory
  Example* MyObjectFactory::create_example(int  x)   {
    Example* obj= (Example*)
          _object_factory->create_empty_object_by_name( 
                  Example::get_class_name() );

    obj->set_x(x);

    return obj;
  }

  Example* create_example(SuifEnv *suif_env,int  x)   {
    MyObjectFactory * current = (MyObjectFactory *)
          (suif_env->get_object_factory(MyObjectFactory::get_class_name()));
    suif_assert_message(
          current != NULL,
          ("My Object Factory not registered\n"));
    return current->create_example(x);
  }

The final portion of the file is the factory methods for MyObjectFactory. It is not necessary to understand the details of how the factory is defined. Of interest are the two create routines for the Example class, which is shown in Figure 10. The first one is the method in the object factory while the second is the global function. Notice that the global function simply finds the object factory and then calls the method. This lookup is quite fast because it is being performed using LStrings so only a pointer comparison is needed.


next up previous contents
Next: Hoof Files in Detail Up: Generating SUIF Intermediate Representation Previous: Creating Objects   Contents
David Heine
1999-11-22