home *** CD-ROM | disk | FTP | other *** search
/ messroms.de / 2007-01-13_www.messroms.de.zip / VZ200 / TOOLS / ZCCSRC.ZIP / scc / main.c < prev    next >
C/C++ Source or Header  |  2000-03-06  |  5KB  |  307 lines

  1. /*    File main.c: 2.7 (84/11/28,10:14:56) */
  2. /*% cc -O -c %
  3.  *
  4.  */
  5.  
  6. #include <stdio.h>
  7. #include "defs.h"
  8. #include "data.h"
  9. #include "proto.h"
  10.  
  11. int main(int argc, char **argv)
  12. {
  13.     char *p, *bp;
  14.     int smacptr;
  15.  
  16.     macptr = 0;
  17.     ctext = 0;
  18.     argc--;
  19.     argv++;
  20.     errs = 0;
  21.     aflag = 1;
  22. #ifdef    FRAME_IX
  23.     frameix = 0;
  24. #endif
  25.     while (NULL!=(p = *argv++))
  26.     {
  27.         if (*p == '-')
  28.             while (*++p)
  29.                 switch (*p)
  30.                 {
  31.                 case 't':
  32.                 case 'T':
  33.                     ctext = 1;
  34.                     break;
  35.                 case 's':
  36.                 case 'S':
  37.                     sflag = 1;
  38.                     break;
  39.                 case 'c':
  40.                 case 'C':
  41.                     cflag = 1;
  42.                     break;
  43.                 case 'a':
  44.                 case 'A':
  45.                     aflag = 0;
  46.                     break;
  47.                 case 'd':
  48.                 case 'D':
  49.                     bp = ++p;
  50.                     if (!*p)
  51.                         usage();
  52.                     while (*p && *p != '=')
  53.                         p++;
  54.                     if (*p == '=')
  55.                         *p = '\t';
  56.                     while (*p)
  57.                         p++;
  58.                     p--;
  59.                     defmac(bp);
  60.                     break;
  61. #ifdef    FRAME_IX
  62.                 case 'x':
  63.                 case 'X':
  64.                     frameix = 1;
  65.                     break;
  66. #endif
  67.                 default:
  68.                     usage();
  69.                 }
  70.         else
  71.             break;
  72.     }
  73.  
  74.     smacptr = macptr;
  75.     if (!p)
  76.         usage();
  77.     while (p)
  78.     {
  79.         errfile = 0;
  80.         if (xtypeof(p) == 'c' || xtypeof(p) == 'i')
  81.         {
  82.             glbptr = STARTGLB;
  83.             locptr = STARTLOC;
  84.             wsptr = ws;
  85.             inclsp = 0;
  86.             iflevel = 0;
  87.             skiplevel = 0;
  88.             swstp = 0;
  89.             litptr = 0;
  90.             stkp = 0;
  91.             errcnt = 0;
  92.             ncmp = 0;
  93.             lastst = 0;
  94.             macptr = smacptr;
  95.             input2 = NULL;
  96.             quote[0] = '"';
  97.             quote[1] = 0;
  98.             cmode = 1;
  99.             glbflag = 1;
  100.             nxtlab = 0;
  101.             litlab = getlabel();
  102.             defmac("end\tmemory");
  103.             addglb("memory", ARRAY, CCHAR, 0, EXTERN);
  104.             addglb("stack", ARRAY, CCHAR, 0, EXTERN);
  105.             rglbptr = glbptr;
  106.             addglb("etext", ARRAY, CCHAR, 0, EXTERN);
  107.             addglb("edata", ARRAY, CCHAR, 0, EXTERN);
  108.             defmac("short\tint");
  109.             initmac();
  110.             /* compiler body */
  111.             if (!openin(p))
  112.                 return (1);
  113.             if (!openout())
  114.                 return (1);
  115.             header(p);
  116.             gtext();
  117.             parse();
  118.             fclose(input);
  119.             gdata();
  120.             dumplits();
  121.             gbss();
  122.             dumpglbs();
  123.             errorsummary();
  124.             trailer();
  125.             fclose(output);
  126.             errs = errs || errfile;
  127.         }
  128.         else
  129.         {
  130.             fputs("Don't understand file ", stderr);
  131.             fputs(p, stderr);
  132.             fputs("\n", stderr);
  133.             errs = 1;
  134.         }
  135.         p = *argv++;
  136.     }
  137.     exit(errs != 0);
  138. }
  139.  
  140. void FEvers(void)
  141. {
  142.     outstr("\tFront End (2.7,84/11/28)");
  143. }
  144.  
  145. void usage(void)
  146. {
  147.     fputs("usage: sccXXXX [-tcsa] [-dSYM[=VALUE]] files\n", stderr);
  148.     exit(1);
  149. }
  150.  
  151. /*    Process all input text
  152.  *
  153.  *    At this level, only static declarations, defines,
  154.  *    includes, and function definitions are legal.
  155.  */
  156. void parse(void)
  157. {
  158.     while (!feof(input))
  159.     {
  160.         if (amatch("extern", 6))
  161.             dodcls(EXTERN);
  162.         else
  163.         if (amatch("static", 6))
  164.             dodcls(STATIC);
  165.         else
  166.         if (dodcls(PUBLIC))
  167.             ;
  168.         else
  169.         if (match("#asm"))
  170.             doasm();
  171.         else
  172.         if (match("#include"))
  173.             doinclude();
  174.         else
  175.         if (match("#define"))
  176.             dodefine();
  177.         else
  178.         if (match("#undef"))
  179.             doundef();
  180.         else
  181.         if (match("#line") || match("#"))
  182.             kill();
  183.         else
  184.             newfunc();
  185.         blanks();
  186.     }
  187. }
  188.  
  189. /* Parse top level declarations */
  190. int dodcls(int stclass)
  191. {
  192.     blanks();
  193.     if (amatch("char", 4))
  194.         declglb(CCHAR, stclass);
  195.     else
  196.     if (amatch("int", 3))
  197.         declglb(CINT, stclass);
  198.     else
  199.     if (stclass == PUBLIC)
  200.         return (0);
  201.     else
  202.         declglb(CINT, stclass);
  203.     ns();
  204.     return (1);
  205. }
  206.  
  207.  
  208. /* Dump the literal pool */
  209. void dumplits(void)
  210. {
  211.     int j, k;
  212.  
  213.     if (litptr == 0)
  214.         return;
  215.     printlabel(litlab);
  216.     col();
  217.     k = 0;
  218.     while (k < litptr)
  219.     {
  220.         defbyte();
  221.         j = 8;
  222.         while (j--)
  223.         {
  224.             /* HJB 03/03/00 I want 8 bit string support */
  225.             /* outdec(litq[k++] & 127); */ 
  226.             outdec(litq[k++]);
  227.             if ((j == 0) | (k >= litptr))
  228.             {
  229.                 nl();
  230.                 break;
  231.             }
  232.             outbyte(',');
  233.         }
  234.     }
  235. }
  236.  
  237. /* Dump all static variables */
  238. void dumpglbs(void)
  239. {
  240.     int j;
  241.  
  242.     if (!glbflag)
  243.         return;
  244.     cptr = rglbptr;
  245.     while (cptr < glbptr)
  246.     {
  247.         if (cptr[IDENT] != FUNCTION)
  248.         {
  249.             ppubext(cptr);
  250.             if (cptr[STORAGE] != EXTERN)
  251.             {
  252.                 prefix();
  253.                 outstr(cptr);
  254.                 col();
  255.                 defstorage();
  256.                 j = glint(cptr);
  257.                 if ((cptr[TYPE] == CINT) ||
  258.                     (cptr[IDENT] == POINTER))
  259.                     j = j * intsize();
  260.                 onum(j);
  261.                 nl();
  262.             }
  263.         }
  264.         else
  265.         {
  266.             fpubext(cptr);
  267.         }
  268.         cptr = cptr + SYMSIZ;
  269.     }
  270. }
  271.  
  272. /* Report errors */
  273. void errorsummary(void)
  274. {
  275.     if (ncmp)
  276.         error("missing closing bracket");
  277.     nl();
  278.     comment();
  279.     outdec(errcnt);
  280.     if (errcnt)
  281.         errfile = YES;
  282.     outstr(" error(s) in compilation");
  283.     nl();
  284.     comment();
  285.     ot("Literal pool:");
  286.     outdec(litptr);
  287.     nl();
  288.     comment();
  289.     ot("Global pool :");
  290.     outdec(glbptr - rglbptr);
  291.     nl();
  292.     comment();
  293.     ot("Macro pool  :");
  294.     outdec(macptr);
  295.     nl();
  296.     if (errcnt)
  297.         pl("Error(s)\n");
  298. }
  299.  
  300. char xtypeof(char *s)
  301. {
  302.     s += strlen(s) - 2;
  303.     if (*s == '.')
  304.         return (*(s + 1));
  305.     return (' ');
  306. }
  307.