home *** CD-ROM | disk | FTP | other *** search
/ The Fred Fish Collection 1.5 / ffcollection-1-5-1992-11.iso / ff_progs / prog_c / zc.lzh / ZC / ZCSRC.LZH / top / sym.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-05-25  |  3.1 KB  |  159 lines

  1. /* Copyright (c) 1988 by Sozobon, Limited.  Author: Tony Andrews
  2.  *
  3.  * Permission is granted to anyone to use this software for any purpose
  4.  * on any computer system, and to redistribute it freely, with the
  5.  * following restrictions:
  6.  * 1) No charge may be made other than reasonable charges for reproduction.
  7.  * 2) Modified versions must be clearly marked as such.
  8.  * 3) The authors are not responsible for any harmful consequences
  9.  *    of using this software, even if they result from defects in it.
  10.  */
  11.  
  12. /*
  13.  * Changes for Amiga version by Jeff Lydiatt marked with Jal.
  14.  */
  15.  
  16. #include "top.h"
  17.  
  18. /*
  19.  * Symbol table:
  20.  *
  21.  * For each symbol, contains a pointer to the block starting at the
  22.  * given symbol, and a pointer to the next symbol in the symbol table.
  23.  */
  24. struct    sym {
  25.     char    *name;
  26.     BLOCK    *bp;
  27.     struct    sym    *next;
  28. };
  29.  
  30. struct    sym    *sfirst = NULL;
  31. struct    sym    *slast  = NULL;
  32.  
  33. /*
  34.  * newblock(name) - allocate a new block structure and initialize it
  35.  */
  36. BLOCK *
  37. newblock(name)
  38. register char    *name;
  39. {
  40.     register BLOCK    *bp;
  41.  
  42.     if ((bp = (BLOCK *) alloc(sizeof(BLOCK))) == NULL)
  43.         return NULL;
  44.  
  45.     /*
  46.      * Initialize the allocated block structure.
  47.      */
  48.  
  49.     if ((bp->name = strsave(name)) == NULL) {
  50.         free(bp);
  51.         return NULL;
  52.     }
  53.  
  54.     bp->flags = 0;
  55.     bp->ref   = 0;
  56.     bp->bcode = 0;
  57.     bp->first = NULL;
  58.     bp->last  = NULL;
  59.     bp->bcond = NULL;
  60.     bp->bfall = NULL;
  61.     bp->chain = NULL;
  62.     bp->next  = NULL;
  63.     bp->rref = bp->rset = 0;
  64.  
  65.     return bp;
  66. }
  67.  
  68. /*
  69.  * mksym(name) - make a new symbol table entry
  70.  *
  71.  * mksym creates new symbol table entries, and allocates space for the
  72.  * 'block' structure that will be used for the symbol. This can happen
  73.  * when a reference to a block is detected, but before the block has
  74.  * been encountered. Since we allocate the block structure here, other
  75.  * blocks can reference it before we actually read it in.
  76.  */
  77. BLOCK *
  78. mksym(name)
  79. register char    *name;
  80. {
  81.     register struct    sym    *new;
  82.  
  83.     if ((new = (struct sym *) alloc(sizeof(struct sym))) == NULL)
  84.         return NULL;
  85.  
  86.     if ((new->bp = newblock(name)) == NULL) {
  87.         free(new);
  88.         return NULL;
  89.     }
  90.  
  91.     new->name = new->bp->name;
  92.     new->next = NULL;
  93.  
  94.     if (sfirst == NULL)
  95.         sfirst = slast = new;
  96.     else {
  97.         slast->next = new;
  98.         slast = new;
  99.     }
  100.  
  101.     return new->bp;
  102. }
  103.  
  104. /*
  105.  * getsym(name) - return a pointer to the block for symbol 'name'
  106.  *
  107.  * Scans the symbol table for the given symbol and returns a pointer
  108.  * to its block, when found, or NULL if not present.
  109.  */
  110. BLOCK *
  111. getsym(name)
  112. register char    *name;
  113. {
  114.     register struct    sym    *sp;
  115.  
  116.     for (sp = sfirst; sp != NULL ;sp = sp->next) {
  117.         if (strcmp(sp->name, name) == 0)
  118.             return sp->bp;
  119.     }
  120.     return NULL;
  121. }
  122.  
  123. /*
  124.  * freesym() - free all symbol table space
  125.  */
  126. void
  127. freesym()
  128. {
  129.     register struct    sym    *sp, *nexts;
  130.     register INST    *ip, *nexti;
  131.  
  132.     for (sp = sfirst; sp != NULL ;sp = nexts) {
  133.         nexts = sp->next;
  134.         for (ip = sp->bp->first; ip != NULL ; ip = nexti) {
  135.             nexti = ip->next;
  136.             if (ip->src.astr != NULL)
  137.                 free(ip->src.astr);
  138.             if (ip->dst.astr != NULL)
  139.                 free(ip->dst.astr);
  140.             free(ip);
  141.         }
  142.         free(sp->name);
  143.         free(sp->bp);
  144.         free(sp);
  145.     }
  146.     sfirst = slast = NULL;
  147. }
  148.  
  149. char *
  150. mktmp()
  151. {
  152.     static    char    tname[32];
  153.     static    int    tnum = 0;
  154.  
  155.     sprintf(tname, "T%d", tnum++);
  156.  
  157.     return tname;
  158. }
  159.