home *** CD-ROM | disk | FTP | other *** search
/ Fish 'n' More 2 / fishmore-publicdomainlibraryvol.ii1991xetec.iso / dirs / northc_384.lzh / NorthC / Example2.LZH / top / sym.c < prev    next >
C/C++ Source or Header  |  1990-08-30  |  3KB  |  160 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. #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.  * freesym() - free all symbol table space
  120.  */
  121. void
  122. freesym()
  123. {
  124.     register struct    sym    *sp, *nexts;
  125.     register INST    *ip, *nexti;
  126.  
  127.     for (sp = sfirst; sp != NULL ;sp = nexts) {
  128.         nexts = sp->next;
  129.         for (ip = sp->bp->first; ip != NULL ; ip = nexti) {
  130.             nexti = ip->next;
  131.             if (ip->src.astr != NULL)
  132.                 free(ip->src.astr);
  133.             if (ip->dst.astr != NULL)
  134.                 free(ip->dst.astr);
  135.             free(ip);
  136.         }
  137.         free(sp->name);
  138.         free(sp->bp);
  139.         free(sp);
  140.     }
  141.     sfirst = slast = NULL;
  142. }
  143.  
  144. #ifdef NORTHC
  145. static    char    tname[32];
  146. static    int    tnum = 0;
  147. #endif
  148.  
  149. char *
  150. mktmp()
  151. {
  152. #ifndef NORTHC
  153.     static    char    tname[32];
  154.     static    int    tnum = 0;
  155. #endif
  156.     sprintf(tname, "T%d", tnum++);
  157.  
  158.     return tname;
  159. }
  160.