home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / INFO / CROSSASM / 68ASMSIM.ZIP / asmsrc / symbol.c < prev   
Encoding:
C/C++ Source or Header  |  1988-09-07  |  4.0 KB  |  163 lines

  1. /***********************************************************************
  2.  *
  3.  *        SYMBOL.C
  4.  *        Symbol Handling Routines for 68000 Assembler
  5.  *
  6.  *    Function: lookup()
  7.  *        Searches the symbol table for a previously defined
  8.  *        symbol or creates a new symbol. The routine functions
  9.  *        as follows: 
  10.  *
  11.  *        Symbol
  12.  *        Found                  Returned
  13.  *        In    Create      Action      Error
  14.  *        Table?  Flag      Taken          Code
  15.  *        ------  ------    --------------  -----------
  16.  *          N     FALSE     None          UNDEFINED
  17.  *          N     TRUE      Symbol created  OK
  18.  *          Y     FALSE     None          OK
  19.  *          Y    TRUE      None          MULTIPLE_DEFS
  20.  *
  21.  *        In addition, the routine always returns a pointer to
  22.  *        the structure (type symbolDef) that which contains the
  23.  *        symbol that was found or created. The routine uses a 
  24.  *        hash function to index into an array of pointers to 
  25.  *        ordered linked lists of symbol definitions.
  26.  *
  27.  *        define()
  28.  *        Defines the symbol whose name is specified to have the
  29.  *        value specified. If check is TRUE, then the symbol is
  30.  *        is assumed to already exist and its value is checked 
  31.  *        against the passed value; a PHASE_ERROR results if the
  32.  *        values are not the same. When check is TRUE the routine
  33.  *        also sets the backRef bit for the symbol. If check is
  34.  *        FALSE, then the symbol is defined and its value is set
  35.  *        equal to the supplied number. The function returns a 
  36.  *        pointer to the symbol definition structure.
  37.  *
  38.  *     Usage:    symbolDef *lookup(sym, create, errorPtr)
  39.  *        char *sym;
  40.  *        int create, *errorPtr;
  41.  *
  42.  *         symbolDef *define(sym, value, check, errorPtr)
  43.  *        char *sym;
  44.  *        int value, check, *errorPtr;
  45.  *
  46.  *      Author: Paul McKee
  47.  *        ECE492    North Carolina State University
  48.  *
  49.  *        Date:    11/28/86
  50.  *
  51.  ************************************************************************/
  52.  
  53.  
  54. #include <stdio.h>
  55. #include <ctype.h>
  56. #include "asm.h"
  57.  
  58. /* MAXHASH is the range of the hash function. hash()
  59.    returns values from 0 to MAXHASH inclusive. */
  60.     
  61. #define MAXHASH 26
  62.  
  63. static symbolDef *htable[MAXHASH+1];
  64.  
  65.  
  66. symbolDef *lookup(sym, create, errorPtr)
  67. char *sym;
  68. int create, *errorPtr;
  69. {
  70. int h, cmp;
  71. symbolDef *s, *last, *t;
  72. static char initialized = FALSE;
  73.  
  74.     if (!initialized) {
  75.         for (h = 0; h <= MAXHASH; h++)
  76.             htable[h] = 0;
  77.         initialized = TRUE;
  78.         }
  79.  
  80.     h = hash(sym);
  81.     if (s = htable[h]) {
  82.         /* Search the linked list for a matching symbol */
  83.         last = NULL;
  84.         while (s && (cmp = strcmp(s->name, sym)) < 0) {
  85.             last = s;
  86.             s = s->next;
  87.             }
  88.         /* If a match was found, return pointer to the structure */
  89.         if (s && !cmp) {
  90.             if (create)
  91.                 NEWERROR(*errorPtr, MULTIPLE_DEFS);
  92.             t = s;
  93.             }
  94.         /* Otherwise insert the symbol in the list */
  95.         else if (create)
  96.             if (last) {
  97.                 /* The symbol goes after an existing symbol */
  98.                 t = (symbolDef *) malloc(sizeof(symbolDef));
  99.                 last->next = t;
  100.                 t->next = s;
  101.                 strcpy(t->name, sym);
  102.                 }
  103.             else {
  104.                 /* The symbol goes at the head of a list */
  105.                 t = (symbolDef *) malloc(sizeof(symbolDef));
  106.                 t->next = htable[h];
  107.                 htable[h] = t;
  108.                 strcpy(t->name, sym);
  109.                 }
  110.         else
  111.             NEWERROR(*errorPtr, UNDEFINED);
  112.         }
  113.     else if (create) {
  114.         t = (symbolDef *) malloc(sizeof(symbolDef));
  115.         htable[h] = t;
  116.         t->next = NULL;
  117.         strcpy(t->name, sym);
  118.         }
  119.     else
  120.         NEWERROR(*errorPtr, UNDEFINED);
  121.     return t;
  122. }                
  123.             
  124.  
  125. int hash(symbol)
  126. char *symbol;
  127. {
  128. int sum;
  129.  
  130.     sum = 0;
  131.     while (*symbol) {
  132.         sum += (isupper(*symbol)) ? (*symbol - 'A') : 26;
  133.         symbol++;
  134.         }
  135.     return (sum % 27);
  136. }
  137.  
  138.  
  139. symbolDef *define(sym, value, check, errorPtr)
  140. char *sym;
  141. long    value;
  142. int    check, *errorPtr;
  143. {
  144. symbolDef *symbol;
  145.  
  146.     symbol = lookup(sym, !check, errorPtr);
  147.     if (*errorPtr < ERROR)
  148.         if (check) {
  149.             if (symbol->value != value)
  150.                 NEWERROR(*errorPtr, PHASE_ERROR);
  151.             symbol->flags |= BACKREF;
  152. /*            printf("Marking symbol \"%s\"\n", sym); */
  153.             }
  154.         else {
  155. /*            printf("Defining the symbol \"%s\" as %08lX.\n", sym, value); */
  156.             symbol->value = value;
  157.             symbol->flags = 0;
  158.             }
  159.     return symbol;
  160. }
  161.  
  162.  
  163.