home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_300 / 338_02 / symtbl.c < prev    next >
Text File  |  1989-11-30  |  5KB  |  169 lines

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