Main Page | Class Hierarchy | Alphabetical List | Compound List | File List | Compound Members | File Members

lSymTab.H

Go to the documentation of this file.
00001 /*-----------------------------------------------------------------
00002  * Name:         lSymTab.H
00003  * Designer:     Willie Glover
00004  * Description:  In memory symbol table for LEGO IR variables
00005  *
00006  * 2/5/97 WAH:  Fixing elementSize assumption, AllocVar guessing.
00007  * 2/6/97 WAH:  Changed nameTable structure radically to avoid
00008  *              guessing and AllocVar altogether.
00009  * 2/26/97 WAH: Made datum::FindOffset, datum::append, and
00010  *              ~datum nonrecursive.
00011  * 2/27/97 WAH: Made default varType LIR_BYTE.
00012  * 2/28/97 WAH: Verified capability to have (long ... (s ""));
00013  *              (with tinker.y) takes in / prints out text data
00014  * 4/7/97 WAH:  Improved copy constructor, destructor. Default type
00015  *              changed to LIR_LONG.
00016  * 7/18/97 WAH: Added LIR_LABELASINT varType.
00017  * 7/29/97 WAH: Changed initType() to InitType(), append() to Append().
00018  * 12/8/97 WAH: Lots of cleanup; changed some int fields to enums;
00019  *              removed sVarType field from nameTable; added couple
00020  *              of checks for LIR_LABELASINT where missing; changed
00021  *              procName field of symbolTable to regionName; added
00022  *              symbolTable::GetSDataType; moved
00023  *              some functions to lSymTab.C.
00024  * 1/9/98 WAH:  Fixed several string-setting functions to handle NULL.
00025  *----------------------------------------------------------------*/
00026 #ifndef LSYMBOLTABLE_H
00027 #define LSYMBOLTABLE_H
00028 
00029 #include <stdio.h>
00030 #include <string.h>
00031 #include "legoErr.H"
00032 
00033 enum importTypes {
00034 LIR_GLOBAL,
00035 LIR_GNU_WEAK,
00036 LIR_LOCAL
00037 };
00038 
00039 enum tableTypes {
00040 LIR_PROC,
00041 LIR_MODULE
00042 };
00043 //HZ: 12/17/01 added the segments of SDATA (short data), SBSS (short
00044 // uninitialized data), COMMENT (COMMENT segment) for IA-64
00045 enum sDataTypes {
00046 LIR_DATA,
00047 LIR_DATA1,
00048 LIR_BSS,
00049 LIR_TEXT,
00050 LIR_SDATA,
00051 LIR_SBSS,
00052 LIR_COMMENT,
00053 LIR_CTORS,
00054 LIR_DTORS
00055 };
00056 
00057 enum varTypes {
00058 LIR_VOID,
00059 LIR_BYTE,
00060 LIR_USBYTE,
00061 LIR_WORD,
00062 LIR_USWORD,
00063 LIR_LONG,
00064 LIR_USLONG,
00065 LIR_LONGLONG,
00066 LIR_USLONGLONG,
00067 LIR_FLOAT,
00068 LIR_DOUBLE,
00069 LIR_STRING,       // used for ascii / asciz and ws data
00070 LIR_STRINGASINT,  // for wi and long storing a string
00071 LIR_LABEL,        // used for function pointers stored with wi
00072 LIR_LABELASINT,   // for wi and long storing a label
00073 };
00074 
00075 enum initTypes {
00076   LIR_WRITTEN,    // using wi, ww, et al.
00077   LIR_PLACED      // using byte, word, et al.
00078 };
00079 
00080 /*
00081  * class datum
00082  *
00083  * A single piece of data, specifying its type, relative
00084  * location, and actual value.
00085  */
00086 class datum
00087 {
00088   enum varTypes varType;  // see varTypes
00089   int offset;             // byte offset
00090   int isDummy;            // marks data as empty (uninitialized data)
00091   class datum *next;      // next datum
00092   union                   // anon. union of values
00093   {
00094     long Byte;
00095     long usByte;
00096     long Word;
00097     long usWord;
00098     long Long;
00099     unsigned long usLong;
00100     long long Longlong;
00101     unsigned long long usLonglong;
00102     float Float;
00103     double Double;
00104     char *String;
00105     char *Label;
00106   };
00107 
00108 public:
00109 
00110   datum()
00111   {
00112     //    fprintf(stderr,"Creating DATUM %p\n", this);
00113     varType = LIR_LONG;
00114     offset = -1;
00115     isDummy = 0;
00116     next = NULL;
00117     usLonglong = 0;
00118   } // end constructor
00119   datum (const datum &oldD)  // copy constructor
00120   {
00121     //    fprintf(stderr,"Creating DATUM %p\n", this);
00122     varType = oldD.varType;
00123     offset = oldD.offset;
00124     isDummy = oldD.isDummy;
00125     if (varType != LIR_STRING && varType != LIR_STRINGASINT &&
00126         varType != LIR_LABEL && varType != LIR_LABELASINT)
00127       usLonglong = oldD.usLonglong;
00128     else
00129       {
00130         String = new char [strlen (oldD.String) + 1]; // works for label too!
00131         strcpy (String, oldD.String);
00132       } // end else
00133     next = new datum (*(oldD.next));
00134   } // end copy constructor
00135 
00136   ~datum()
00137   {
00138     datum *temp;
00139 
00140     if (varType == LIR_STRING || varType == LIR_STRINGASINT ||
00141         varType == LIR_LABEL || varType == LIR_LABELASINT)
00142       delete[] String;
00143     while (next != NULL)
00144       {
00145         temp = next;
00146         next = temp->next;
00147         temp->next = NULL;
00148         delete temp;
00149       } // end while
00150     //    fprintf(stderr,"Deleting DATUM %p\n", this);
00151     
00152   } // end destructor
00153 
00154   enum varTypes GetVarType (void) {return varType;}
00155   void SetVarType (enum varTypes vt) {varType = vt;}
00156 
00157   int GetOffset (void) {return offset;}
00158   void SetOffset (int o) {offset = o;}
00159 
00160   int IsDummy (void) {return isDummy;}
00161   void SetDummy (int d) {isDummy = d;}
00162 
00163   datum *GetNext (void) {return next;}
00164   void SetNext (datum *n) {next = n;}
00165 
00166   // Data retrieval methods
00167 
00168   // Because there seems to be no specification as to whether byte and
00169   // word data in LCODE is signed or unsigned, these data sizes are
00170   // stored internally as longs, so that either case will be covered!
00171 
00172   long GetByte (void) {return Byte;}
00173   void SetByte (long b) {Byte = b;}
00174 
00175   long GetUsByte (void) {return usByte;}
00176   void SetUsByte (long b) {usByte = b;}
00177 
00178   long GetWord (void) {return Word;}
00179   void SetWord (long w) {Word = w;}
00180 
00181   long GetUsWord (void) {return usWord;}
00182   void SetUsWord (long w) {usWord = w;}
00183 
00184   long GetLong (void) {return Long;}
00185   void SetLong (long l) {Long = l;}
00186 
00187   unsigned long GetUsLong (void) {return usLong;}
00188   void SetUsLong (unsigned long l) {usLong = l;}
00189 
00190   long long GetLongLong (void) {return Longlong;}
00191   void SetLongLong (long long l) {Longlong = l;}
00192 
00193   unsigned long long GetUsLongLong (void) {return usLonglong;}
00194   void SetUsLongLong (unsigned long long l) {usLonglong = l;}
00195 
00196   float GetFloat (void) {return Float;}
00197   void SetFloat (float f) {Float = f;}
00198 
00199   double GetDouble (void) {return Double;}
00200   void SetDouble (double d) {Double = d;}
00201 
00202   char *GetString (void) {return String;}
00203   void SetString (char *s)
00204   {
00205     if (varType == LIR_STRING || varType == LIR_STRINGASINT)
00206       delete[] String;
00207     if (varType == LIR_LABEL || varType == LIR_LABELASINT)
00208       delete[] Label;
00209     if (s != NULL)
00210       {
00211         String = new char [strlen (s) + 1];
00212         strcpy (String, s);
00213       } // end if
00214     else String = NULL;
00215   } // end SetString
00216 
00217   char *GetLabel (void) {return Label;}
00218   void SetLabel (char *s)
00219   {
00220     if (varType == LIR_STRING || varType == LIR_STRINGASINT)
00221       delete[] String;
00222     if (varType == LIR_LABEL || varType == LIR_LABELASINT)
00223       delete[] Label;
00224     if (s != NULL)
00225       {
00226         Label = new char [strlen (s) + 1];
00227         strcpy (Label, s);
00228       } // end if
00229     else Label = NULL;
00230   } // end SetString
00231 
00232   // Find the datum with a given offset.
00233 
00234   datum *FindOffset (int o)
00235   {
00236     datum *candidate;
00237 
00238     for (candidate = this; candidate != NULL;
00239          candidate = candidate->next)
00240       {
00241         if ((candidate->offset == o) && !(candidate->isDummy))
00242           return candidate;
00243       } // end for
00244   } // end FindOffset
00245 
00246   // Find the datum with a given index, assuming an element size.
00247 
00248   datum *FindIndex (int idx, int elsize)
00249   {
00250     return FindOffset (idx * elsize);
00251   } // end FindIndex
00252 
00253   // Append a new datum on the end of this list.
00254 
00255   void Append (datum *newd)
00256   {
00257     datum *tail;
00258 
00259     for (tail = this; tail->next != NULL; tail = tail->next) ;
00260     tail->next = newd;
00261     return;
00262   } // end Append
00263 
00264   // Write out the datum in Rebel.
00265   void WriteRebel (FILE *, char *, int);
00266 };
00267 
00268 class symbolTable;
00269 
00270 /*
00271  * class nameTable
00272  *
00273  * A single variable, possibly an array or struct.
00274  */
00275 class nameTable {
00276 
00277     char *varName;      /* variable name                               */
00278     int elementSize;    /* size of the data type, in bytes             */
00279     int reserve;        /* total space requirements, in # bytes        */
00280     int numElements;    /* # of elements                               */
00281     int align;          /* addresee allignment (used for REBEL format) */
00282     //void *info;       /* ptr to additional info (code, entry, etc)   */
00283     //HZ:
00284     char *info;
00285     enum importTypes importType;        /* global or local             */
00286     symbolTable* STPtr; /* points to parent symbolTable                */
00287 
00288   int numData;  // true number of data elements
00289   int totalSize;  // true total space occupied
00290   datum *data;  // first piece of data
00291   datum *datatail;  // last piece of data (for speed in appending)
00292 
00293 public:
00294 
00295    nameTable()
00296    {
00297         varName         = NULL;
00298         //      sVarType        = LIR_DATA; WAH 12/8/97
00299         //      elementSize     = 4; WAH 2/5/97
00300         elementSize     = 0;
00301         reserve         = 0;
00302         numElements     = 0; 
00303         //      align           = 4; WAH 2/5/97
00304         align           = 0;
00305         info            = NULL;
00306         importType      = LIR_LOCAL;
00307         STPtr           = NULL;
00308         //      dataValue.longPtr = NULL;
00309         numData         = 0;
00310         totalSize       = 0;
00311         data            = NULL;
00312         datatail        = NULL;
00313    } // end no-arg constructor
00314   ~nameTable()
00315    {
00316         delete[] varName;
00317         delete data;
00318         //
00319         if(info != NULL) delete [] info;
00320         // NOT DELETING INFO - don't know how, its a void * !
00321    } // end destructor
00322 
00323 /*
00324  *  Below is the copy constructor for class nameTable.
00325  */
00326    nameTable(const nameTable &oldVar)
00327    {
00328      if (oldVar.varName != NULL)
00329        {
00330          varName                = new char[strlen(oldVar.varName)+1];
00331          strcpy(varName,oldVar.varName);
00332        } // end if
00333      else
00334        varName = NULL;
00335      elementSize        = oldVar.elementSize;
00336      reserve            = oldVar.reserve;
00337      numElements        = oldVar.numElements;
00338      align              = oldVar.align;
00339      info               = oldVar.info;
00340      importType         = oldVar.importType;
00341      STPtr              = oldVar.STPtr;
00342      numData            = oldVar.numData;
00343      totalSize          = oldVar.totalSize;
00344      if (oldVar.data != NULL)
00345        {
00346          data               = new datum (*(oldVar.data));
00347          for (datatail = data; datatail->GetNext() != NULL;
00348               datatail = datatail->GetNext()) /* do nothing */ ;
00349        } // end if
00350      else
00351        data = datatail = NULL;
00352 
00353    } // end copy constructor
00354 
00355 /*
00356  *  Below is the operator = copy constructor for nameTable.
00357  */
00358    nameTable& operator=(const nameTable& oldVar)
00359    {
00360      if ( varName != oldVar.varName ) {
00361         delete[] varName;
00362         if (oldVar.varName != NULL)
00363           {
00364             varName     = new char[strlen(oldVar.varName)+1];
00365             strcpy(varName,oldVar.varName);
00366           } // end if
00367         else
00368           varName = NULL;
00369         elementSize     = oldVar.elementSize;
00370         reserve         = oldVar.reserve;
00371         numElements     = oldVar.numElements; 
00372         align           = oldVar.align;
00373         info            = oldVar.info;
00374         importType      = oldVar.importType;
00375         STPtr           = oldVar.STPtr;
00376         numData         = oldVar.numData;
00377         totalSize       = oldVar.totalSize;
00378         delete data;
00379         if (oldVar.data != NULL)
00380           {
00381             data            = new datum (*(oldVar.data));
00382             for (datatail = data; datatail->GetNext() != NULL;
00383                  datatail = datatail->GetNext()) /* do nothing */ ;
00384           } // end if
00385         else
00386           data = datatail = NULL;
00387      }  // end if
00388      return *this;
00389    } // end operator=
00390 
00391   // --- Get methods ------------------------------------------
00392 
00393    void SetVarName(char *newVarName) {
00394      delete[] varName;
00395      if (newVarName != NULL)
00396        {
00397          varName = new char[strlen(newVarName)+1];
00398          strcpy(varName,newVarName);
00399        } // end if
00400      else varName = NULL;
00401    }
00402 
00403   //   void SetSVarType(int newSVarType) { sVarType = newSVarType; }
00404 
00405    void SetElementSize(int newElementSize)
00406    { 
00407      elementSize = newElementSize; 
00408    }
00409 
00410    void SetReserve(int newReserve) 
00411    { 
00412      reserve = newReserve; 
00413    } 
00414 
00415    void SetNumElements(int newNumElements)
00416      { numElements = newNumElements; }
00417 
00418    void SetAlign(int newAlign) { align = newAlign; }
00419 
00420    //void SetInfo(void *newInfo) { info = newInfo; }
00421    //HZ:
00422    void SetInfo(char *newInfo) 
00423    { 
00424        if(info != NULL)
00425        {
00426            delete [] info;
00427            info = NULL;
00428        }
00429        if(newInfo != NULL)
00430        {
00431            info = new char[strlen(newInfo) + 1];
00432            strcpy(info, newInfo);
00433        }
00434    }
00435    
00436    void AppendInfo(char *newInfo)
00437    {
00438        char *a;
00439        
00440        if(info != NULL)
00441        {
00442            a = new char[strlen(info) + strlen(newInfo) + 1];
00443            strcpy(a, info);
00444            strcat(a, newInfo);
00445            delete [] info;
00446            info = a;
00447        }
00448        else
00449        {
00450            info = new char[strlen(newInfo) + 1];
00451            strcpy(info, newInfo);
00452        }
00453    }
00454 
00455    void SetImportType(enum importTypes newImportType)
00456      { importType = newImportType; }
00457 
00458    void SetSTPtr(symbolTable *newTablePtr) { STPtr = newTablePtr; }
00459 
00460    void SetNumData(int newNumData) { numData = newNumData; }
00461 
00462    void SetTotalSize(int newTotalSize) { totalSize = newTotalSize; }
00463 
00464   // These next methods are generic ways to set and get values
00465   // from the data.
00466 
00467   void SetOffset (enum varTypes, int, void *, int = 1);
00468   void SetArray (enum varTypes, int, void *, int = 1);
00469 
00470   datum *GetOffset (int offset)  // returns a datum, NOT the value
00471   {
00472     if (data != NULL)
00473       return data->FindOffset (offset);
00474     else
00475       return NULL;
00476   } // end GetOffset
00477 
00478   datum *GetArray (enum varTypes, int);  // returns a datum, NOT value
00479 
00480   // --- Get methods ------------------------------------------
00481 
00482    char *GetVarName(void) { return varName; }
00483 
00484   //   int       GetSVarType(void) { return sVarType; }
00485 
00486    int   GetElementSize(void) {return elementSize; }
00487 
00488    int   GetReserve(void) { return reserve; }
00489 
00490    int   GetNumElements(void) { return numElements; }
00491 
00492    int  GetAlign(void) { return align; }
00493 
00494    //void *GetInfo(void) { return info; }
00495    //HZ:
00496    char *GetInfo(void) { return info; }
00497 
00498    int   GetImportType(void) { return importType; }
00499 
00500    symbolTable *GetSTPtr(void) { return STPtr; }
00501 
00502   int GetNumData(void) { return numData; }
00503 
00504   int GetTotalSize(void) { return totalSize; }
00505 
00506   datum *GetData(void) { return data; }
00507 
00508   datum *GetDataTail(void) { return datatail; }
00509 
00510 /*---------------------------------------------------------------
00511  * There are two sets of Get- methods for the variable data.
00512  *
00513  * One returns an element from the variable array and requires
00514  * the element index, arrIdx, as input.
00515  *
00516  * The other returns the pointer to the array and does not require
00517  * any inputs.
00518  *
00519  * The methods which return the pointers have "Ptr" as the last
00520  * three characters of the method name.
00521  *
00522  * 2/6/97 WAH: Not any more.  To get the variable array use
00523  *             GetData.  The first set of methods will work the
00524  *             same, however.
00525  * 12/8/97 WAH: Commented out the first set of methods. Will remove
00526  *              completely soon.
00527  *---------------------------------------------------------------*/
00528   /*
00529   char GetByte (int arrIdx=0)
00530   {
00531     datum *target;
00532 
00533     target = GetArray (LIR_BYTE, arrIdx);
00534     if (target == NULL)
00535       {
00536         LegoFatal ("nameTable::GetByte", "No byte at index %d.", arrIdx);
00537         exit;
00538       } // end if
00539     return (char) target->GetByte();
00540   } // end GetByte
00541   unsigned char GetUsByte (int arrIdx=0)
00542   {
00543     datum *target;
00544 
00545     target = GetArray (LIR_USBYTE, arrIdx);
00546     if (target == NULL)
00547       {
00548         LegoFatal ("nameTable::GetUsByte",
00549                    "No unsigned byte at index %d.", arrIdx);
00550         exit;
00551       } // end if
00552     return (unsigned char) target->GetUsByte();
00553   } // end GetUsByte
00554   short int GetWord (int arrIdx=0)
00555   {
00556     datum *target;
00557 
00558     target = GetArray (LIR_WORD, arrIdx);
00559     if (target == NULL)
00560       {
00561         LegoFatal ("nameTable::GetWord", "No word at index %d.", arrIdx);
00562         exit;
00563       } // end if
00564     return (short int) target->GetWord();
00565   } // end GetWord
00566   unsigned short int GetUsWord (int arrIdx=0)
00567   {
00568     datum *target;
00569 
00570     target = GetArray (LIR_USWORD, arrIdx);
00571     if (target == NULL)
00572       {
00573         LegoFatal ("nameTable::GetUsWord", "No unsigned word at index %d.",
00574                    arrIdx);
00575         exit;
00576       } // end if
00577     return (unsigned short int) target->GetUsWord();
00578   } // end GetUsWord
00579   long GetLong (int arrIdx=0)
00580   {
00581     datum *target;
00582 
00583     target = GetArray (LIR_LONG, arrIdx);
00584     if (target == NULL)
00585       {
00586         LegoFatal ("nameTable::GetLong()", "No long at index %d.", arrIdx);
00587         exit;
00588       } // end if
00589     return target->GetLong();
00590   } // end GetLong
00591   unsigned long GetUsLong (int arrIdx=0)
00592   {
00593     datum *target;
00594 
00595     target = GetArray (LIR_USLONG, arrIdx);
00596     if (target == NULL)
00597       {
00598         LegoFatal ("nameTable::GetUsLong", "No unsigned long at index %d.",
00599                    arrIdx);
00600         exit;
00601       } // end if
00602     return target->GetUsLong();
00603   } // end GetUsLong
00604   long long GetLongLong (int arrIdx=0)
00605   {
00606     datum *target;
00607 
00608     target = GetArray (LIR_LONGLONG, arrIdx);
00609     if (target == NULL)
00610       {
00611         LegoFatal ("nameTable::GetLongLong", "No long long at index %d.",
00612                    arrIdx);
00613         exit;
00614       } // end if
00615     return target->GetLongLong();
00616   } // end GetLongLong
00617   unsigned long long GetUsLongLong (int arrIdx=0)
00618   {
00619     datum *target;
00620 
00621     target = GetArray (LIR_USLONGLONG, arrIdx);
00622     if (target == NULL)
00623       {
00624         LegoFatal ("nameTable::GetUsLongLong",
00625                    " No unsigned long long at index %d.", arrIdx);
00626         exit;
00627       } // end if
00628     return target->GetUsLongLong();
00629   } // end GetUsLongLong
00630   float GetFloat (int arrIdx=0)
00631   {
00632     datum *target;
00633 
00634     target = GetArray (LIR_FLOAT, arrIdx);
00635     if (target == NULL)
00636       {
00637         LegoFatal ("nameTable::GetFloat", "No float at index %d.", arrIdx);
00638         exit;
00639       } // end if
00640     return target->GetFloat();
00641   } // end GetFloat
00642   double GetDouble (int arrIdx=0)
00643   {
00644     datum *target;
00645 
00646     target = GetArray (LIR_DOUBLE, arrIdx);
00647     if (target == NULL)
00648       {
00649         LegoFatal ("nameTable::GetDouble", "No double at index %d.", arrIdx);
00650         exit;
00651       } // end if
00652     return target->GetDouble();
00653   } // end GetDouble
00654   char *GetString (int arrIdx=0)
00655   {
00656     datum *target;
00657 
00658     target = GetArray (LIR_STRING, arrIdx);
00659     if (target == NULL)
00660       {
00661         LegoFatal ("nameTable::GetString", "No string at index %d.", arrIdx);
00662         exit;
00663       } // end if
00664     return target->GetString();
00665   } // end GetString
00666   */
00667 
00668   // Determine whether the data was initialized by placement (byte, word)
00669   // or by writing (wi, ww).
00670   int InitType(void)
00671   {
00672     if (numElements != 0) return LIR_PLACED;
00673     else return LIR_WRITTEN;
00674   } // end InitType
00675 
00676   void ClearData (void)
00677   {
00678     delete data;
00679     data = datatail = NULL;
00680     numData = 0;
00681     totalSize = 0;
00682   } // end ClearData
00683 
00684   // Write out the name table in Rebel.
00685   void WriteRebel (FILE *);
00686 }; // end class nameTable
00687 
00688 // ===================================================================
00689 
00690 class symbolTable {
00691 
00692    char *regionName;        /* name of table owner                  */
00693    enum tableTypes tableType;       /* type of table                */
00694    enum sDataTypes sDataType;       /* data segment type            */
00695    int   varCount;          /* number of variables in nameTable     */
00696    int   tableSize;         /* size of nametable initialize to 10   */
00697    int   parmCount;         /* number of formal parameters (unused) */
00698    nameTable **varTablePtr; /* pointer to nameTable                 */
00699 
00700 public:
00701 
00702 /*---------------------------------------------------------------------
00703  * Default constructor initializes the nameTable size to 10 entries.
00704  *--------------------------------------------------------------------*/
00705   symbolTable()
00706   {
00707     regionName  = NULL;
00708     tableType   = LIR_PROC;
00709     sDataType   = LIR_DATA;
00710     varCount    = 0;
00711     tableSize   = 10;
00712     parmCount   = 0;
00713     varTablePtr         = new nameTable*[tableSize];
00714   }
00715 
00716 /*---------------------------------------------------------------------
00717  * Constructor initializes the nameTable size  to initSize entries.
00718  *--------------------------------------------------------------------*/
00719   symbolTable(int initSize)
00720   {
00721     regionName  = NULL;
00722     tableType   = LIR_PROC;
00723     sDataType   = LIR_DATA;
00724     varCount    = 0;
00725     tableSize   = initSize;
00726     parmCount   = 0;
00727     varTablePtr         = new nameTable*[tableSize];
00728   }
00729 
00730   ~symbolTable()
00731   {
00732     delete[] regionName;
00733     for (int i = 0; i < varCount; i++)
00734       delete varTablePtr[i];
00735     delete[] varTablePtr;
00736   }
00737 
00738 /*---------------------------------------------------------------------
00739  * Copy a variable to the table.  Input is a pointer to class nameTable.
00740  * A new entry is created and copied into the table at the current
00741  * variable index.  This method _does_not_ delete the storage pointed
00742  * to by the input variable.
00743  *--------------------------------------------------------------------*/
00744   void AddVar(nameTable *inputVar)
00745   {
00746     if ( varCount >= (tableSize - 1) ) { 
00747       //                fprintf(stderr,"   Want to resize table\n");
00748       ResizeNameTable(); 
00749     }
00750     varTablePtr[varCount] = inputVar;
00751     varCount++;
00752 /* 
00753         fprintf(stderr,"   At the end of AddVar, the whole table look like this:\n"); 
00754         for(int i=0;i<varCount;i++){
00755                 fprintf(stderr,"\t___Name Table name %s,type %d,numElements %d,importType %d,NumBytes %d.\n",
00756                 varTablePtr[i]->GetVarName(),
00757                 varTablePtr[i]->GetVarType(),
00758                 varTablePtr[i]->GetNumElements(),
00759                 varTablePtr[i]->GetImportType(),
00760                 varTablePtr[i]->GetNumBytes()); 
00761         } 
00762 */ 
00763   }
00764 /*---------------------------------------------------------------------
00765  * Update the region name.
00766  *--------------------------------------------------------------------*/
00767   void SetRegionName (char *newRegionName) {
00768     delete[] regionName;
00769     if (newRegionName != NULL)
00770       {
00771         regionName = new char[strlen(newRegionName)+1];
00772         strcpy(regionName,newRegionName);
00773       } // end if
00774     else regionName = NULL;
00775   }
00776 
00777 /*---------------------------------------------------------------------
00778  * Update the table type.
00779  *--------------------------------------------------------------------*/
00780   void SetTableType(enum tableTypes newTableType) { tableType = newTableType; }
00781 
00782 /*---------------------------------------------------------------------
00783  * Update the data type.
00784  *--------------------------------------------------------------------*/
00785   void SetSDataType(enum sDataTypes newDataType) { sDataType = newDataType; }
00786 
00787 /*---------------------------------------------------------------------
00788  * Update the variable count.  Note that the variable count is _not_
00789  * automatically increased when a new item is added to the nameTable
00790  * unless the AddVar() method is used.
00791  *--------------------------------------------------------------------*/
00792   void SetVarCount(int newVarCount) { varCount = newVarCount; }
00793 
00794 /*---------------------------------------------------------------------
00795  * Update the nametable size.  If an attempt is made to resize the nametable
00796  *  to a size which is smaller than the variable count,  The routine exits
00797  *  without modifying the size of the table.
00798  *--------------------------------------------------------------------*/
00799   void SetTableSize(int newTableSize)
00800   {
00801     if (newTableSize < varCount) {
00802       LegoNonFatal ("nameTable::SetTableSize",
00803                     "New size of nameTable too small.");
00804       return;
00805     }
00806     nameTable **tmpVarTablePtr = new nameTable*[newTableSize];
00807     if ( varCount != 0 ) {
00808       for (int idx=0; idx<varCount; idx++) {
00809         tmpVarTablePtr[idx] = varTablePtr[idx];
00810       }
00811     }
00812     delete [] varTablePtr;
00813     varTablePtr = tmpVarTablePtr;
00814     tableSize = newTableSize;
00815   }
00816 
00817 /*---------------------------------------------------------------------
00818  * update the number of parameters.
00819  *--------------------------------------------------------------------*/
00820   void SetParmCount(int newParmCount) { parmCount = newParmCount; }
00821 
00822 /*---------------------------------------------------------------------
00823  * increment by one - the number of parameters field.
00824  *--------------------------------------------------------------------*/
00825   void IncParmCount(void) { parmCount++; }
00826 
00827 /*---------------------------------------------------------------------
00828  * No method is provided to update the nameTable pointer.
00829  *--------------------------------------------------------------------*/
00830 
00831 /*---------------------------------------------------------------------
00832  * get the region name.
00833  *--------------------------------------------------------------------*/
00834   char *GetRegionName(void) { return regionName; }
00835 
00836 /*---------------------------------------------------------------------
00837  * get the table type.
00838  *--------------------------------------------------------------------*/
00839   enum tableTypes GetTableType(void) { return tableType; }
00840 
00841 /*---------------------------------------------------------------------
00842  * get the data type.
00843  *--------------------------------------------------------------------*/
00844   enum sDataTypes GetSDataType(void) { return sDataType; }
00845 
00846 /*---------------------------------------------------------------------
00847  * get the number of variables currently in the nametable.
00848  *--------------------------------------------------------------------*/
00849   int    GetVarCount(void) { return varCount; }
00850 
00851 /*---------------------------------------------------------------------
00852  * get the maximum size of the nametable.
00853  *--------------------------------------------------------------------*/
00854   int    GetTableSize(void) { return tableSize; }
00855 
00856 /*---------------------------------------------------------------------
00857  * get the parameter count.
00858  *--------------------------------------------------------------------*/
00859   int    GetParmCount(void) { return parmCount; }
00860 
00861 /*---------------------------------------------------------------------
00862  * get the pointer to this symbol table's nametable.
00863  *--------------------------------------------------------------------*/
00864   nameTable **GetVarTablePtr(void) { return varTablePtr; }
00865 
00866 // Larin 
00867 
00868   nameTable *GetVarTablePtr(int indx) { return varTablePtr[indx];  } 
00869 
00870 /*---------------------------------------------------------------------
00871  * Double the size of this symboltable's nameTable.
00872  *--------------------------------------------------------------------*/
00873   void ResizeNameTable(void)
00874   {
00875     nameTable **tmpVarTablePtr = new nameTable*[2 * tableSize];
00876     if ( varCount != 0 ) {
00877       for (int idx=0; idx<varCount; idx++) {
00878         tmpVarTablePtr[idx] = varTablePtr[idx];
00879       }
00880     }
00881     delete [] varTablePtr;
00882     varTablePtr = tmpVarTablePtr;
00883     tableSize = 2 * tableSize;
00884   }
00885 
00886 /*---------------------------------------------------------------------
00887  * Resize an array in this symboltable's nameTable.
00888  *--------------------------------------------------------------------*/
00889   void ResizeVariableArray(int entryIdx, int newElementSize);
00890 
00891   // Find a given variable name in the table.
00892 
00893   nameTable *LocateVar (char *name)
00894   {
00895     int i;
00896     nameTable *candidate;
00897 
00898     for (i = 0; i < parmCount; i++)
00899       {
00900         candidate = varTablePtr[i];
00901         if (candidate == NULL) continue;
00902         if (strcmp (candidate->GetVarName(), name) == 0)
00903           return candidate;
00904       } // end for
00905     return NULL;
00906   } // end LocateVar
00907 
00908   // Write out the symbol table in Rebel.
00909   void symbolTable::WriteRebel (FILE *rebelfile)
00910   {
00911     int i;
00912 
00913     fprintf (rebelfile,"  (ms ");
00914     switch (sDataType)
00915       {
00916       case LIR_DATA:
00917         fprintf (rebelfile, "data)\n"); break;
00918       case LIR_DATA1:
00919         fprintf (rebelfile, "data1)\n"); break;
00920       case LIR_BSS:
00921         fprintf (rebelfile, "bss)\n"); break;
00922       case LIR_TEXT:
00923         fprintf (rebelfile, "text)\n"); break;
00924       
00925       //HZ: adding support for other data type
00926       case LIR_SDATA:
00927         fprintf (rebelfile, "sdata)\n"); break;
00928       case LIR_SBSS:
00929         fprintf (rebelfile, "sbss)\n"); break;
00930       case LIR_COMMENT:
00931         fprintf (rebelfile, "dcomment)\n"); break;
00932       case LIR_CTORS:
00933         fprintf (rebelfile, "ctors)\n"); break;
00934       case LIR_DTORS:
00935         fprintf (rebelfile, "dtors)\n"); break;
00936       default:
00937               fprintf(stderr, "WriteRebel Error: data type unrecognized.\n");
00938               exit (-1);      
00939       } // end switch
00940 
00941     for (i = 0; i < varCount; i++)
00942       varTablePtr[i]->WriteRebel (rebelfile);
00943     return;
00944   } // end WriteRebel
00945 
00946 };
00947 
00948 class tableEntry {
00949 
00950    symbolTable *tablePtr;   /* pointer to symbol table                   */
00951    int   entryIndex;        /* index of entry in symbolTable's nameTable */
00952 
00953 public:
00954 
00955    tableEntry() {
00956       tablePtr = NULL;
00957       entryIndex = 0;
00958    }
00959 
00960   ~tableEntry() {}
00961 
00962 /*---------------------------------------------------------------------
00963  * Update the symbol table pointer.
00964  *--------------------------------------------------------------------*/
00965    void SetTablePtr(symbolTable *newTablePtr) { tablePtr = newTablePtr; }
00966 
00967 /*---------------------------------------------------------------------
00968  * Update the entry index.
00969  *--------------------------------------------------------------------*/
00970    void SetEntryIndex(int newEntryIndex) { entryIndex = newEntryIndex; }
00971 
00972 /*---------------------------------------------------------------------
00973  * get the symbol table pointer.
00974  *--------------------------------------------------------------------*/
00975    symbolTable *GetTablePtr(void) { return tablePtr; }
00976 
00977 /*---------------------------------------------------------------------
00978  * get the entry index.
00979  *--------------------------------------------------------------------*/
00980    int GetEntryIndex(void) { return entryIndex; }
00981 };
00982 
00983 #endif // LSYMBOLTABLE_H
00984 

Generated on Mon Jul 21 20:27:58 2003 for TINKER LEGO DOC by doxygen 1.3.2