home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_100 / 148_01 / a99symb.c < prev    next >
Text File  |  1987-09-28  |  4KB  |  155 lines

  1. /*
  2.     TMS9900/99105  Cross-Assembler  v. 1.0
  3.  
  4.     January, 1985
  5.  
  6.     Original 6800 version Copyright (c) 1980 William C. Colley, III.
  7.     Modified for the TMS9900/99105  Series by Alexander Cameron.
  8.  
  9. File:    a99symb.c
  10.  
  11. Routines to manipulate the symbol table.
  12. */
  13.  
  14. /*  Get Globals:  */
  15.  
  16. #include "a99.gbl"
  17.  
  18. /*
  19. This function adds a new entry to the symbol table.  The function
  20. returns values of either 0 or -1.  If the value is 0, the symbol is
  21. already in the table and the global variable sympoint points to the
  22. existing entry.  If the value is -1, the symbol has just been entered
  23. into the table and sympoint points to the new entry.  If the symbol
  24. table is full, the function triggers an abort of the assembly.
  25. */
  26.  
  27. addsym(symbol)
  28. char *symbol;
  29. {
  30.     int t;
  31.     if ((t = slookup(symbol)) > 0)
  32.     {
  33.         wipeout("\nSymbol Table Overflow.\n");
  34.     }
  35.     if (t != 0) movmem(symbol, sympoint, SYMLEN);
  36.     return(t);
  37. }
  38.  
  39. /*
  40. This function checks the symbol table for a given symbol.  The function returns
  41. one of three values as follows:
  42.  
  43.      1 = symbol not found and symbol table full.
  44.      0 = symbol found.  sympoint points to the matching entry.
  45.     -1 = symbol not found.  sympoint points to where the symbol
  46.          should have been.
  47.  
  48. Jan 85 - Added code to cater for quadratic probing when handling
  49.      collisions. (See Wirth's Algorithms + Data Structures = Programs
  50.      pp 268)
  51. */
  52. slookup(symbol)
  53. char *symbol;
  54. {
  55.     int h,d;
  56.     d=1;
  57.     h=hash(symbol);
  58.     sympoint = &symtbl[h];
  59.     while((sympoint -> symname[0] & 0x7f) != '\0')
  60.     {
  61.         if (symcmp(symbol,sympoint->symname) == 0) return(0);
  62.         h += d;
  63.         d += 2;
  64.         sympoint = &symtbl[h];
  65.         if (h >= SYMBOLS) h -= SYMBOLS;
  66.         if (d == SYMBOLS) return(1);
  67.     }
  68.     return(-1);
  69. }
  70.  
  71. /*
  72. This function returns a hash value for a given symbol.  The hash value
  73. is calculated by folding the symbol name up into 16 bits (2 bytes, thus
  74. the symbol length must be even) mod the number of symbols.
  75.  
  76. The earlier versions of the cross-assembler assigned j as an int
  77. consequently if j took on a value greater than 32367 the entry pointer
  78. overflowed and the assembler wound up stuffing symbols into the  middle
  79. of the programme.  Assigning j to unsigned solved this bug. 
  80. */
  81.  
  82. hash(symbol)
  83. char *symbol;
  84. {
  85.     char i;
  86.     unsigned j;
  87.     for (i = j = 0; i < (SYMLEN / 2); i++)
  88.     {
  89.         j += (*symbol++ << 8) + *symbol++;
  90.     }
  91.     return(j % SYMBOLS);
  92. }
  93.  
  94. /*
  95. Function to sort the symbol table.  The function
  96. returns the number of entries in the table.
  97.  
  98. Set flag if you just want a count
  99. */
  100. sortsym(flag)
  101. char flag;
  102. {
  103.     int n, symcmp();
  104.     struct symbtbl *tptr;
  105.     n = 0;
  106.     for (tptr = sympoint = symtbl; tptr < symend; tptr++)
  107.     {
  108.         if ((tptr -> symname[0] & 0x7f) != '\0')
  109.         {
  110.         
  111.             if(!flag )
  112.             {
  113.                 movmem(tptr->symname,sympoint->symname,(SYMLEN+2));
  114.                 sympoint++;
  115.             }
  116.             n++;
  117.         }
  118.     }
  119.     if(flag) return n;
  120.     qsort(&symtbl,n,(SYMLEN+2),&symcmp);
  121.     return n;
  122. }
  123.  
  124. /*
  125. This function compares two symbols.  It returns zero if the
  126. symbols are the same, not zero if they are different.
  127. */
  128.  
  129. symcmp(sym1,sym2)
  130. char *sym1, *sym2;
  131. {
  132.     char i;
  133.     int t;
  134.     for (i = 0; i < SYMLEN; i++)
  135.     {
  136.         if ((t = (*sym1++ & 0x7f) - (*sym2++ & 0x7f)) != 0) break;
  137.     }
  138.     return(t);
  139. }
  140.  
  141. /*
  142. Function to abort an assembly.  The parameter reason holds a string that
  143. will be printed to explain why the assembly bombed.  Note that I can't just
  144. call exit since this will not restore the currently logged disk drive, and
  145. software that changes the currently logged disk drive annoys me greatly.
  146. */
  147.  
  148. wipeout(reason)
  149. char *reason;
  150. {
  151.     puts(reason);
  152.     exit();
  153. }
  154.  
  155.