next up previous contents
Next: Creating Objects Up: Generating SUIF Intermediate Representation Previous: Generating SUIF Intermediate Representation   Contents

From Hoof to C++

The SUIF internal representation is used by all front ends and many optimization passes. It consists of a set of classes defined using Hoof files. These are translated by a macro processor called smgn to produce the actual header files, and some code that defines the classes to the input-output system. You can extend SUIF by defining your own classes in Hoof files. This section describes how this works and how to add your own extensions. The actual predefined SUIF classes are documented in the file suifguide.ps.

Each class has a definition in the Hoof file. The most important part of each declaration is a list of fields. Methods are not usually declared. Instead, they are derived from the declarations of the fields.

Consider the following simple Hoof file:

       module my {
          include "basicnodes/basic.h";
          include "suifnodes/suif.h";
          import basic;
          import suif;
      
          concrete Example : SuifObject {
              int x;
          };
       }
The first line gives the name of this module, ``My''. It is followed by four lines that perform some book-keeping. The ``include'' statements cause corresponding ``#include'' statements to be added to the generated source. The import statements causes calls to the initialization routines for the modules basic and suif to be generated as part of the initialization routine for this module. The next few lines give the actual definition of a new class. Notice that it derives from SuifObject. All class definitions that do not derive from any other SUIF class must derive from SuifObject.

To process this Hoof definition, we need the following Makefile

       SUIF_INTERNAL = defined
       OBJ_FILES = my.o
       HOOF_MAC = $(SMGNDIR)/suif.mac
       HOOF_DEP = ../basicnodes/basic.hoof ../suifnodes/suif.hoof
       SMGN_INCLUDES = $(SMGNDIR)/searchable_list_builders.mac \
                       $(SMGNDIR)/indexed_list_builders.mac \
                       $(SMGNDIR)/list_builders.mac \
                       $(SMGNDIR)/vector_builders.mac
       HOOF = my
       HOOF_GEN = my.h my_factory.h my.cpp my_forwarders.h
       SRCS = my.cpp

Note that this assumes that the files are in the directory nci/suif/suif2b/basesuif/my. Otherwise, you will have to adjust the relative paths in lines for HOOF_DEP and the include of Makefile.std.

Typing make will generate the following files:

File Contents
my.h The declaration of the class.
my_forwarders.h Forward declarations for the class. This is all you need to declare pointers to the class.
my_factory.h The declarations for the object factory that is used to create objects of the class.
my.cpp Bodies for all of the above.

Figure 6 shows the file my.h. Notice that the methods set_x and get_x have been generated automatically. The rest of the declarations are associated with the SUIF infrastructure, for the support of input/output of SUIF data and iteration over SUIF data. Every SUIF class contains a get_class_name static function. It returns the name of the class (in this case "Example" as an LString. An LString is just a string with the property that all equal LStrings share a pointer. LString is one of the basic, predefined types of SUIF. Notice that because this is static, you cannot use this function to find the type of an actual instance. Type casting and identification are addressed in Section 3.2.

Figure 6: The File my.h
  #ifndef __MY_H__
  #define __MY_H__
  #include "suifkernel/suifkernel_forwarders.h"
  #include "suifkernel/suif_object.h"
  #include "common/suif_list.h"
  #include "common/suif_vector.h"
  #include "common/suif_indexed_list.h"
  #include <new>
  #include "my_forwarders.h"
  #include "common/formatted.h"
  #include "suifkernel/iter.h"
  #include "common/i_integer.h"
  #include "basicnodes/basic.h"
  #include "suifnodes/suif.h"
  #include "iokernel/meta_class.h"
  extern "C" void EXPORT init_mynodes(SuifEnv* suif);
  class Example : public  SuifObject {
    public:
      virtual int get_x () const;
      virtual void set_x(int the_value);
      virtual ~Example();
      virtual void print(FormattedText &x) const;
      static const LString &get_class_name();
    protected:
      //use the factory to create objects
      friend class MyObjectFactory;
      Example();
      static void constructor_function( Address address );
    private:
      int _x;
      }; 
  #endif

After the ``protected'' you will see the declaration of a friend class MyObjectFactory. This is the object factory that creates Example objects. It is declared in the my_factory.h file. Next is a default constructor for the class. As this is protected, you cannot do ``new Example'' in your code. Instead one calls a ``create_example'' routine found in MyObjectFactory. This is described below. constructorFunction is a function which is called to create an instance of the class at a given address. This is used by internally by the SUIF infrastruture implementation. Finally, you will see the private declarations of the fields of the class, in this case just ``x''.


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