home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 6 / FreshFish_September1994.bin / new / dev / c / hce / hcesource / top / source / sym.c < prev    next >
C/C++ Source or Header  |  1992-09-02  |  3KB  |  164 lines

  1. /* Copyright (c) 1988,1991 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. #include "top.h"
  12.  
  13. /*
  14.  * Symbol table:
  15.  *
  16.  * For each symbol, contains a pointer to the block starting at the
  17.  * given symbol, and a pointer to the next symbol in the symbol table.
  18.  */
  19. struct    sym {
  20.     char    *name;
  21.     BLOCK    *bp;
  22.     struct    sym    *next;
  23. };
  24.  
  25. struct    sym    *sfirst = NULL;
  26. struct    sym    *slast  = NULL;
  27.  
  28. /*
  29.  * newblock(name) - allocate a new block structure and initialize it
  30.  */
  31. BLOCK *
  32. newblock(name)
  33. register char    *name;
  34. {
  35.     register BLOCK    *bp;
  36.  
  37.     if ((bp = (BLOCK *) alloc(sizeof(BLOCK))) == NULL)
  38.         return NULL;
  39.  
  40.     /*
  41.      * Initialize the allocated block structure.
  42.      */
  43.  
  44.     if ((bp->name = strsave(name)) == NULL) {
  45.         free(bp);
  46.         return NULL;
  47.     }
  48.  
  49.     bp->flags = 0;
  50.     bp->ref   = 0;
  51.     bp->bcode = 0;
  52.     bp->first = NULL;
  53.     bp->last  = NULL;
  54.     bp->bcond = NULL;
  55.     bp->bfall = NULL;
  56.     bp->chain = NULL;
  57.     bp->next  = NULL;
  58.     bp->rref = bp->rset = 0;
  59.  
  60.     return bp;
  61. }
  62.  
  63. /*
  64.  * mksym(name) - make a new symbol table entry
  65.  *
  66.  * mksym creates new symbol table entries, and allocates space for the
  67.  * 'block' structure that will be used for the symbol. This can happen
  68.  * when a reference to a block is detected, but before the block has
  69.  * been encountered. Since we allocate the block structure here, other
  70.  * blocks can reference it before we actually read it in.
  71.  */
  72. BLOCK *
  73. mksym(name)
  74. register char    *name;
  75. {
  76.     register struct    sym    *new;
  77.  
  78.     if ((new = (struct sym *) alloc(sizeof(struct sym))) == NULL)
  79.         return NULL;
  80.  
  81.     if ((new->bp = newblock(name)) == NULL) {
  82.         free(new);
  83.         return NULL;
  84.     }
  85.  
  86.     new->name = new->bp->name;
  87.     new->next = NULL;
  88.  
  89.     if (sfirst == NULL)
  90.         sfirst = slast = new;
  91.     else {
  92.         slast->next = new;
  93.         slast = new;
  94.     }
  95.  
  96.     return new->bp;
  97. }
  98.  
  99. /*
  100.  * getsym(name) - return a pointer to the block for symbol 'name'
  101.  *
  102.  * Scans the symbol table for the given symbol and returns a pointer
  103.  * to its block, when found, or NULL if not present.
  104.  */
  105. BLOCK *
  106. getsym(name)
  107. register char    *name;
  108. {
  109.     register struct    sym    *sp;
  110.  
  111.     for (sp = sfirst; sp != NULL ;sp = sp->next) {
  112.         if (strcmp(sp->name, name) == 0)
  113.             return sp->bp;
  114.     }
  115.     return NULL;
  116. }
  117.  
  118. /*
  119.  * freeop() - free an operand
  120.  */
  121. void
  122. freeop(op)
  123. struct    opnd    *op;
  124. {
  125.     if (is_astr(op->amode) && op->astr != NULL)
  126.         free(op->astr);
  127. }
  128.  
  129. /*
  130.  * freesym() - free all symbol table space
  131.  */
  132. void
  133. freesym()
  134. {
  135.     register struct    sym    *sp, *nexts;
  136.     register INST    *ip, *nexti;
  137.  
  138.     for (sp = sfirst; sp != NULL ;sp = nexts) {
  139.         nexts = sp->next;
  140.         for (ip = sp->bp->first; ip != NULL ; ip = nexti) {
  141.             nexti = ip->next;
  142.  
  143.             freeop(&ip->src);
  144.             freeop(&ip->dst);
  145.             free(ip);
  146.         }
  147.         free(sp->name);
  148.         free(sp->bp);
  149.         free(sp);
  150.     }
  151.     sfirst = slast = NULL;
  152. }
  153.  
  154. char *
  155. mktmp()
  156. {
  157.     static    char    tname[32];
  158.     static    int    tnum = 0;
  159.  
  160.     sprintf(tname, "T%d", tnum++);
  161.  
  162.     return tname;
  163. }
  164.