Tuesday, December 9, 2014

Table – New Model

One of the major goals for the new table design (missed in the December 3 post) is to eliminate all the standalone code work functions (translate, encode, recreate, etc.), which require their definitions so that pointers to them can be put into the table entries.  Many of the codes do not have several of these work functions (for example, only commands have translate functions), and many codes share work functions (for example, all binary operator have the same recreate function).  Once all the codes are implemented, there would have been an explosion of these work functions, especially considering that each code will need a unique run function.

Therefore, a class hierarchy will be the basis for the new table model where the base class holds the information members and virtual functions used for these functions.  Using virtual functions allow defining common work functions in an upper class.  Unfortunately, there will be an explosion of classes since all codes need a unique run virtual function.  Fortunately, there is a way to define these classes without requiring every source file to know about them so only the base table class needs to be known globally, which will contain the interface for all of these derived classes.

There will be a table instance for each code containing the information for that code only.  Instead of identifying a code by an enumerator (essentially an index), a code will be identified by a base table class pointer.  Code information will be accessed by inline access functions, and the work functions accessed using virtual functions.  The index of the code will only be accessed when a token is finally encoded into the program.  Each code will be assigned a unique index during initialization (more on this later).  The token will therefore contain a code table instance pointer instead of a code enumerator (index).

Though there will no longer be a monolithic table instance, there will be some static table class members and functions.  For example, the search functions will be static since these will not require a table instance (they will return an instance for a code).  The search functions will use a static map member for looking up a name to get a code table instance.   Each of these static members will be described as they are created.

Table – Current Model

The model of the current table design is a monolithic single instance where the information for each code is obtained with a code enumerator that is used within the table as an index into an array of plain structures where each element contains the information for a single code.  There is a series of access functions taking a code enumerator as an argument.  There is a similar series of access functions taking a token as an argument, where the code enumerator stored in the token is used.  Finally there are a couple of functions for searching the table for a code by a name.

A singleton pattern was used for the table instance so that a global table instance would not be used.  However, the table entry array was global (though only within the table source file) along with a static pointer to the instance (within the table class and defined in the table source file).  Is this really a singleton?  In any case, this singleton pattern will be temporarily replaced with a single global table instance (until the new table model starts to get implemented).

One problem with the current table is when it comes time to add a new variable to a table entry structure, like the just added expected data type member.  If a value for one of the entries is missed, the compiler reports the error, but error is reported against the end of the array giving no clue which entry has the problem.  Finding the problem entry is very time consuming.  (This actually occurred).

Another problem is with the auto-generation of the code enumeration.  The actual code enumerators were defined as comments in the table source file, which a awk script combed through an generated a header file with the code enumeration definition.  This design was an attempt to eliminate the problem of matching the code enumeration with the entries.

This issue was that this auto-generated header file is dependent on the table source file, the main header file includes this header file, and all source files are dependent on the main header file.  So every time the table source file is modified, the entire project needs to be rebuilt, and this is becoming a nuisance.  Therefore, the first goal will be to eliminate this.