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