home *** CD-ROM | disk | FTP | other *** search
/ GEMini Atari / GEMini_Atari_CD-ROM_Walnut_Creek_December_1993.iso / files / program / mit2mot1 / hash.c < prev    next >
C/C++ Source or Header  |  1993-10-23  |  5KB  |  211 lines

  1. #include <stdlib.h>
  2. #include <string.h>
  3. #include "ytab.h"
  4. #include "global.h"
  5.  
  6. #define HASH_SIZE 1009
  7. #define NUMOF(a) (sizeof(a)/sizeof((a)[0]))
  8.  
  9. char *ops[] =
  10. {
  11.   "abcdS", "addaS", "addiS", "addqS", "addxS", "addS", "andiS", "andS",
  12.   "aslS", "aslS", "bchgS", "bclrS", "bC", "bsetS", "bsrS", "btstS",
  13.   "chkS", "clrS", "cmpaS", "cmpiS", "cmpmS", "cmpS", "dbC", "dbfS",
  14.   "dbtS", "divsS", "divuS", "eoriS", "eorS", "exgS", "extS", "jmpS",
  15.   "jsrS", "jC", "leaS", "link", "lslS", "lsrS", "moveaS", "movemS",
  16.   "movepS", "moveqS", "moveS", "movaS", "movmS", "movpS", "movqS", "movS",
  17.   "mulsS", "muluS", "nbcdS", "negxS", "negS", "nop", "notS", "oriS", "orS",
  18.   "peaS", "reset", "rolS", "rorS", "roxlS", "roxrS", "rte", "rtr",
  19.   "rts", "sC", "sfS", "sfS", "sbcdS", "stop", "subaS", "subiS",
  20.   "subqS", "subxS", "subS", "swapS", "tasS", "trap", "trapv", "tstS",
  21.   "unlk"
  22. }, *ccodes[] =
  23. {"ra", "eq", "ne", "ge", "gt", "le", "lt", "cc", "hi", "ls", "cs",
  24.  "hs", "lo", "mi", "pl", "vc", "vs"};
  25. Hash directives[] =
  26. {
  27.   {".abort",    "",        D_UNS,        0},
  28.   {".align",    "",        D_UNS,        0},
  29.   {".ascii",    ".dc.b",    D_SLIST,    0},
  30.   {".asciz",    ".dc.b",    D_SLIST,    0},
  31.   {".byte",    ".dc.b",    D_ELIST,    0},
  32.   {".comm",    ".comm",    D_COMM,        0},
  33.   {".data",    ".data",    D_OPTINT,    0},
  34.   {".desc",    "",        D_UNS,        0},
  35.   {".double",    "",        D_UNS,        0},
  36.   {".even",    ".even",    D_NONE,        0},
  37.   {".file",    "",        D_UNS,        0},
  38.   {".line",    "",        D_UNS,        0},
  39.   {".fill",    "",        D_UNS,        0},
  40.   {".float",    "",        D_UNS,        0},
  41.   {".globl",    ".globl",    D_IDLIST,    0},
  42.   {".global",    ".globl",    D_IDLIST,    0},
  43.   {".int",    ".dc.l",    D_ELIST,    0},
  44.   {".lcomm",    ".ds.b",    D_LCOMM,    0},
  45.   {".long",    ".dc.l",    D_ELIST,    0},
  46.   {".lsym",    "",        D_UNS,        0},
  47.   {".octa",    "",        D_UNS,        0},
  48.   {".org",    ".org",        D_ORG,        0},
  49.   {".set",    ".equ",        D_COMM,        0},
  50.   {".short",    ".dc.w",    D_ELIST,    0},
  51.   {".space",    "",        D_UNS,        0},
  52.   {".text",    ".text",    D_OPTINT,    0},
  53.   {".word",    ".dc.w",    D_ELIST,    0}
  54. };
  55.  
  56. static Hash *htable[HASH_SIZE];
  57.  
  58. static unsigned int 
  59. hash(s)
  60.      register char *s;
  61. {
  62.   register unsigned int t = 0;
  63.  
  64.   for (; *s; s++)
  65.     t = (t * 33 + (*s)) % HASH_SIZE;
  66.   return t;
  67. }
  68.  
  69. void 
  70. mkhash(tok, map, type)
  71.      char *tok, *map;
  72.      int type;
  73. {
  74.   Hash *h, *H;
  75.   unsigned int i;
  76.   h = (Hash *)malloc(sizeof (Hash));
  77.  
  78.   h->htext = strdup(tok);
  79.   h->mapto = strdup(map);
  80.   h->htype = type;
  81.   h->next = 0;
  82.   i = hash(tok);
  83.   if (!htable[i])
  84.     htable[i] = h;
  85.   else {
  86.     for (H = htable[i]; H->next; H = H->next) ;
  87.     H->next = h;
  88.   }
  89. }
  90.  
  91. static void 
  92. do_S(s)
  93.      char *s;
  94. {
  95.   static char buf1[12], buf2[12];
  96.   char *s1, *s2, *s0 = s;
  97.   int type = OP;
  98.  
  99.   for (s1 = buf1, s2 = buf2; *s != 'S';)
  100.     *s1++ = *s2++ = *s++;
  101.   if (*s0 == 'j' && !(s0[1] == 'm' || s0[1] == 's'))
  102.     *buf2 = 'b';
  103.   if (*s0 == 'm' && s0[1] == 'o') {
  104.     if (s0[3] != 'e') {
  105.       buf2[3] = 'e';
  106.       s2 = buf2 + 4;
  107.       if (s0[3] != 'S')
  108.     *s2++ = s0[3];
  109.     }
  110.     if (s2[-1] == 'm')
  111.       type = OP_MREG;
  112.   }
  113.   *s1 = *s2 = '\0';
  114.   mkhash(buf1, buf2, type);
  115.   *s2++ = '.';
  116.   *(s1 + 1) = *(s2 + 1) = '\0';
  117.   *s1 = *s2 = 'b';
  118.   mkhash(buf1, buf2, type);
  119.   *s1 = *s2 = 'w';
  120.   mkhash(buf1, buf2, type);
  121.   *s1 = *s2 = 'l';
  122.   mkhash(buf1, buf2, type);
  123. }
  124.  
  125. static void 
  126. do_C(s)
  127.      char *s;
  128. {
  129.   static char buf[12];
  130.   int i;
  131.   char *s1;
  132.  
  133.   for (s1 = buf; *s != 'C';)
  134.     *s1++ = *s++;
  135.   *(s1 + 2) = 'S';
  136.   *(s1 + 3) = '\0';
  137.   for (i = 0; i < NUMOF(ccodes); i++) {
  138.     *s1 = ccodes[i][0];
  139.     *(s1 + 1) = ccodes[i][1];
  140.     do_S(buf);
  141.   }
  142. }
  143.  
  144. static void 
  145. do_ops()
  146. {
  147.   int i;
  148.   char c;
  149.  
  150.   for (i = 0; i < NUMOF(ops); i++) {
  151.     c = ops[i][strlen(ops[i]) - 1];
  152.     if (c == 'C')
  153.       do_C(ops[i]);
  154.     else if (c == 'S')
  155.       do_S(ops[i]);
  156.     else
  157.       mkhash(ops[i], ops[i], OP);
  158.   }
  159. }
  160.  
  161. void 
  162. init_hash()
  163. {
  164.   int i;
  165.   char buf[3];
  166.  
  167.   for (i = 0; i < HASH_SIZE; i++)
  168.     htable[i] = 0;
  169.   for (i = 0; i < NUMOF(directives); i++)
  170.     mkhash(directives[i].htext, directives[i].mapto, directives[i].htype);
  171.   do_ops();
  172.   for (buf[0] = 'd', buf[1] = '0', buf[2] = '\0'; buf[1] <= '7'; buf[1]++)
  173.     mkhash(buf, buf, REG);
  174.   for (buf[0] = 'a', buf[1] = '0'; buf[1] <= '7'; buf[1]++)
  175.     mkhash(buf, buf, REG);
  176.   mkhash("pc", "pc", REG);
  177.   mkhash("sp", "sp", REG);
  178.   mkhash("usp", "usp", REG);
  179.   mkhash("sr", "sr", REG);
  180.   mkhash("ccr", "ccr", REG);
  181. }
  182.  
  183. int 
  184. lookup(tag, subst)
  185.      char *tag, *subst;
  186. {
  187.   Hash *h;
  188.  
  189.   for (h = htable[hash(tag)]; h; h = h->next)
  190.     if (!strcmp(tag, h->htext)) {
  191.       strcpy(subst, h->mapto);
  192.       return h->htype;
  193.     }
  194.   return 0;
  195. }
  196.  
  197. void 
  198. clear_hash()
  199. {
  200.   Hash *h, *q;
  201.   int i;
  202.  
  203.   for (i = 0; i < HASH_SIZE; i++)
  204.     for (h = htable[i]; h; h = q) {
  205.       q = h->next;
  206.       free(h->htext);
  207.       free(h->mapto);
  208.       free(h);
  209.     }
  210. }
  211.