home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_100 / 190_01 / symtbl.c < prev   
Text File  |  1985-11-14  |  5KB  |  176 lines

  1. /******************************************************************
  2.     symtbl.c, the symbol table code for as68
  3. */
  4.  
  5. /*        (C) Copyright 1982 Steve Passe        */
  6. /*        All Rights Reserved                    */
  7.  
  8. /* version 1.01
  9.  
  10.     8/30/83    ver. 1.01 modified for Aztec ver. 1.05g    smp
  11.     9/1/83    split off from as68.c, the main module
  12.  
  13. */
  14.  
  15. /* begincode */
  16.  
  17. /* includes */
  18.  
  19. #define AZTECZII 1
  20.  
  21. #ifndef AZTECZII
  22. #include <stdio.h>
  23. #else
  24. #include "stdio.h"                                /* with aztecII compiler */
  25. #endif
  26. #include "b:as68.h"
  27.  
  28. /* externals */
  29.  
  30. extern struct _symbol *symtbl;
  31. extern struct _symbol *syms_head;            /* head of sym table free space */
  32. extern char *syms_tail;                        /* tail of sym table free space */
  33. extern int symspace;                        /* bytes in symbol table */
  34. extern int symbols;                            /* number of symbols in table */
  35. extern struct _mtable mtable[];
  36.  
  37. /* search the mnemonic table for "mnem" */
  38.  
  39. struct _mtable *
  40. mnemsearch(mnem)
  41. char *mnem;
  42. {
  43.     register int result;
  44.     struct _mtable *bottom = &mtable[0];
  45.     struct _mtable *top = &mtable[MTSIZE - 1];
  46.     struct _mtable *middle;
  47.  
  48.     if (strlen(mnem) > 7) return NULL;
  49.  
  50.     while (bottom <= top) {
  51.         middle = bottom + (top - bottom) / 2;
  52.         if ((result = a1strcmp(mnem, middle->_mnem)) < 0)
  53.             top = middle - 1;
  54.         else if (result > 0)
  55.             bottom = middle + 1;
  56.         else return middle;
  57.     }
  58.     return NULL;
  59. }
  60.  
  61. /* search symbol table for entry,
  62.         return index into table if found,
  63.     or
  64.         return NULL
  65. */
  66.  
  67. symsearch(symbol)
  68. char *symbol;
  69. {
  70.     static int last_sought = NULL;            /* index of last symbol sought */
  71.  
  72.     register int result;
  73.     register int bump = 0;                    /* store index 'bump' factor */
  74.     struct _symbol *bottom;
  75.     struct _symbol *top;
  76.     struct _symbol *middle;
  77.  
  78.     if (!symbol) return last_sought;        /* want index of last search */
  79.  
  80.     bottom = &symtbl[0];                    /* first entry in table */
  81.     top = &symtbl[symbols];                    /* last entry */
  82.                         /* scan for symbol with binary search of pointers */
  83.     while (bottom <= top) {
  84.         middle = bottom + (top - bottom) / 2;
  85.         if ((result = strcmp(symbol, middle->_sym)) < 0) {
  86.             top = middle - 1;
  87.             bump = NULL;                    /* ...would go here... */
  88.         }
  89.         else if (result > 0) {
  90.             bottom = middle + 1;
  91.             bump = 1;                        /* ...would go 1 beyond here... */
  92.         }
  93.         else {                    /* place index where found into last_sought */
  94.             return (last_sought = (middle - &symtbl[0]));
  95.         }
  96.     }
  97.                         /* place index of where it would go into last_sought */
  98.     last_sought = (middle - &symtbl[0]) + bump;        /* add bump factor */
  99.     return NULL;
  100. }
  101.  
  102. /* add symbol, it's value, and it's attributes to table,
  103.     if found,
  104.         and permanent return NULL
  105.         and temporary set value and return TRUE
  106.     else
  107.         if room,
  108.             add it, its value, and its attributes
  109.             and return TRUE
  110.         else return ERROR    
  111. */
  112.  
  113. symadd(sym, v, a, search_flag)
  114. char *sym;
  115. long v;
  116. char a;
  117. FLAG search_flag;
  118. {
  119.     register int index;
  120.  
  121.     if (search_flag) {                                    /* search first... */
  122.         if (index = symsearch(sym))    {                      /* found it */
  123.             if (symtbl[index]._atr & PERMANENT) {            /* if permanent */
  124.                 return NULL;                                /* can't */
  125.             }
  126.             else {                                            /* ok to reset */
  127.                 symtbl[index]._val = v;                    /* set new value */
  128.                 symtbl[index]._atr = a;                    /* set attribute */
  129.                 return TRUE;                                /* done */
  130.             }
  131.         }
  132.     }
  133.     index = symsearch(NULL);                /* get the last sought symbol */
  134.  
  135.     if ((syms_tail - (char *) syms_head)            /* free space in bytes */
  136.         < sizeof(struct _symbol) + strlen(sym) + 1) {    /* space needed */
  137.         return ERROR;                                /* no room */
  138.     }
  139.                                             /* make a hole in array... */
  140.     movmem(symtbl + index, symtbl + index + 1,    /* from index to index + 1 */
  141.         (symbols - index + 1) * sizeof(struct _symbol));    /* second half */
  142.     ++syms_head;                                    /* another struct */
  143.     ++symbols;                                        /* another symbol */
  144.     syms_tail -= (strlen(sym) + 1);                    /* space for symbol */
  145.     strcpy(syms_tail, sym);                            /* move symbol */
  146.     symtbl[index]._sym = syms_tail;                    /* set ptr to it */
  147.     symtbl[index]._val = v;                            /* set value */
  148.     symtbl[index]._atr = a;                            /* set attributes */
  149.     return TRUE;                                    /* whew! */
  150. }
  151.  
  152. /* define a PERMANENT symbol (address or EQU)
  153.     if not present and room,
  154.         enter a permanent symbol and its value
  155.           into table and return TRUE,
  156.     or if present,
  157.         return NULL,
  158.     or if out of symbol space,
  159.         return ERROR
  160. */
  161.  
  162. symenter(sym, v, r_flg)
  163. char *sym;
  164. long v;
  165. char r_flg;
  166. {
  167.     int x;
  168.     if (x = symsearch(sym)) {                /* symbol already defined */
  169.         symtbl[x]._atr |= 0x04;                /* mark as redefine attempted */
  170.         return NULL;
  171.     }
  172.     return (r_flg == 'r') ?
  173.                 symadd(sym, v, 0x03, NO) : symadd(sym, v, 0x01, NO);
  174. }
  175. ndex to index + 1 */
  176.         (symbols - index + 1) * sizeof(struct _symbol));    /*