home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume5 / smallc / part2 / sym.c < prev    next >
Encoding:
C/C++ Source or Header  |  1986-11-30  |  3.8 KB  |  249 lines

  1. /*    File sym.c: 2.1 (83/03/20,16:02:19) */
  2. /*% cc -O -c %
  3.  *
  4.  */
  5.  
  6. #include <stdio.h>
  7. #include "defs.h"
  8. #include "data.h"
  9.  
  10. /*
  11.  *    declare a static variable
  12.  */
  13. declglb (typ, stor)
  14. int    typ,
  15.     stor;
  16. {
  17.     int    k, j;
  18.     char    sname[NAMESIZE];
  19.  
  20.     FOREVER {
  21.         FOREVER {
  22.             if (endst ())
  23.                 return;
  24.             k = 1;
  25.             if (match ("*"))
  26.                 j = POINTER;
  27.             else
  28.                 j = VARIABLE;
  29.             if (!symname (sname))
  30.                 illname ();
  31.             if (findglb (sname))
  32.                 multidef (sname);
  33.             if (match ("[")) {
  34.                 k = needsub ();
  35.                 if (k || stor == EXTERN)
  36.                     j = ARRAY;
  37.                 else
  38.                     j = POINTER;
  39.             }
  40.             addglb (sname, j, typ, k, stor);
  41.             break;
  42.         }
  43.         if (!match (","))
  44.             return;
  45.     }
  46. }
  47.  
  48. /*
  49.  *    declare local variables
  50.  *
  51.  *    works just like "declglb", but modifies machine stack and adds
  52.  *    symbol table entry with appropriate stack offset to find it again
  53.  */
  54. declloc (typ, stclass)
  55. int    typ, stclass;
  56. {
  57.     int    k, j;
  58.     char    sname[NAMESIZE];
  59.  
  60.     FOREVER {
  61.         FOREVER {
  62.             if (endst ())
  63.                 return;
  64.             if (match ("*"))
  65.                 j = POINTER;
  66.             else
  67.                 j = VARIABLE;
  68.             if (!symname (sname))
  69.                 illname ();
  70.             if (findloc (sname))
  71.                 multidef (sname);
  72.             if (match ("[")) {
  73.                 k = needsub ();
  74.                 if (k) {
  75.                     j = ARRAY;
  76.                     if (typ == CINT)
  77.                         k = k * intsize();
  78.                 } else {
  79.                     j = POINTER;
  80.                     k = intsize();
  81.                 }
  82.             } else
  83.                 if ((typ == CCHAR) & (j != POINTER))
  84.                     k = 1;
  85.                 else
  86.                     k = intsize();
  87.             if (stclass != LSTATIC) {
  88.                 k = galign(k);
  89.                 stkp = modstk (stkp - k);
  90.                 addloc (sname, j, typ, stkp, AUTO);
  91.             } else
  92.                 addloc( sname, j, typ, k, LSTATIC);
  93.             break;
  94.         }
  95.         if (!match (","))
  96.             return;
  97.     }
  98. }
  99.  
  100. /*
  101.  *    get required array size
  102.  */
  103. needsub ()
  104. {
  105.     int    num[1];
  106.  
  107.     if (match ("]"))
  108.         return (0);
  109.     if (!number (num)) {
  110.         error ("must be constant");
  111.         num[0] = 1;
  112.     }
  113.     if (num[0] < 0) {
  114.         error ("negative size illegal");
  115.         num[0] = (-num[0]);
  116.     }
  117.     needbrack ("]");
  118.     return (num[0]);
  119. }
  120.  
  121. findglb (sname)
  122. char    *sname;
  123. {
  124.     char    *ptr;
  125.  
  126.     ptr = STARTGLB;
  127.     while (ptr != glbptr) {
  128.         if (astreq (sname, ptr, NAMEMAX))
  129.             return (ptr);
  130.         ptr = ptr + SYMSIZ;
  131.     }
  132.     return (0);
  133. }
  134.  
  135. findloc (sname)
  136. char    *sname;
  137. {
  138.     char    *ptr;
  139.  
  140.     ptr = locptr;
  141.     while (ptr != STARTLOC) {
  142.         ptr = ptr - SYMSIZ;
  143.         if (astreq (sname, ptr, NAMEMAX))
  144.             return (ptr);
  145.     }
  146.     return (0);
  147. }
  148.  
  149. addglb (sname, id, typ, value, stor)
  150. char    *sname, id, typ;
  151. int    value,
  152.     stor;
  153. {
  154.     char    *ptr;
  155.  
  156.     if (cptr = findglb (sname))
  157.         return (cptr);
  158.     if (glbptr >= ENDGLB) {
  159.         error ("global symbol table overflow");
  160.         return (0);
  161.     }
  162.     cptr = ptr = glbptr;
  163.     while (an (*ptr++ = *sname++));
  164.     cptr[IDENT] = id;
  165.     cptr[TYPE] = typ;
  166.     cptr[STORAGE] = stor;
  167.     cptr[OFFSET] = value & 0xff;    
  168.     cptr[OFFSET+1] = (value >> 8) & 0xff;
  169.     glbptr = glbptr + SYMSIZ;
  170.     return (cptr);
  171. }
  172.  
  173. addloc (sname, id, typ, value, stclass)
  174. char    *sname, id, typ;
  175. int    value, stclass;
  176. {
  177.     char    *ptr;
  178.     int    k;
  179.  
  180.     if (cptr = findloc (sname))
  181.         return (cptr);
  182.     if (locptr >= ENDLOC) {
  183.         error ("local symbol table overflow");
  184.         return (0);
  185.     }
  186.     cptr = ptr = locptr;
  187.     while (an (*ptr++ = *sname++));
  188.     cptr[IDENT] = id;
  189.     cptr[TYPE] = typ;
  190.     cptr[STORAGE] = stclass;
  191.     if (stclass == LSTATIC) {
  192.         gdata();
  193.         printlabel(k = getlabel());
  194.         col();
  195.         defstorage();
  196.         onum(value);
  197.         nl();
  198.         gtext();
  199.         value = k;
  200.     } else
  201.         value = galign(value);
  202.     cptr[OFFSET] = value & 0xff;
  203.     cptr[OFFSET+1] = (value >> 8) & 0xff;
  204.     locptr = locptr + SYMSIZ;
  205.     return (cptr);
  206. }
  207.  
  208. /*
  209.  *    test if next input string is legal symbol name
  210.  *
  211.  */
  212. symname (sname)
  213. char    *sname;
  214. {
  215.     int    k;
  216.     char    c;
  217.  
  218.     blanks ();
  219.     if (!alpha (ch ()))
  220.         return (0);
  221.     k = 0;
  222.     while (an (ch ()))
  223.         sname[k++] = gch ();
  224.     sname[k] = 0;
  225.     return (1);
  226. }
  227.  
  228. illname ()
  229. {
  230.     error ("illegal symbol name");
  231. }
  232.  
  233. multidef (sname)
  234. char    *sname;
  235. {
  236.     error ("already defined");
  237.     comment ();
  238.     outstr (sname);
  239.     nl ();
  240. }
  241.  
  242. glint(syment) char *syment; {
  243.     int l,u,r;
  244.     l = syment[OFFSET];
  245.     u = syment[OFFSET+1];
  246.     r = (l & 0xff) + ((u << 8) & ~0x00ff);
  247.     return (r);
  248. }
  249.