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