home *** CD-ROM | disk | FTP | other *** search
/ CP/M / CPM_CDROM.iso / enterprs / cpm / utils / s / smc21src.lzh / CC11.C < prev    next >
Encoding:
Text File  |  1992-12-08  |  6.0 KB  |  282 lines

  1. /*
  2. ** execution begins here
  3. */
  4. main(argc, argv) int argc, *argv; {
  5.   argcs=argc;
  6.   argvs=argv;
  7.  
  8. #ifdef DYNAMIC
  9.  
  10.   swnext=calloc(SWTABSZ, 1);
  11.   swend=swnext+((SWTABSZ-SWSIZ)>>1);
  12.   stage=calloc(STAGESIZE, 1);
  13.   stagelast=stage+STAGELIMIT;
  14.   wq=calloc(WQTABSZ*BPW, 1);
  15.   litq=calloc(LITABSZ, 1);
  16.   macn=calloc(MACNSIZE,1);
  17.         /*10*/
  18.   macq=calloc(MACQSIZE, 1);
  19.   pline=calloc(LINESIZE, 1);
  20.   mline=calloc(LINESIZE, 1);
  21.  
  22. #else /* DYNAMIC */
  23.  
  24.   swnext=swq;
  25.   swend=swnext+SWTABSZ-SWSIZ;
  26.   stagelast=stage+STAGELIMIT;
  27.  
  28. #endif /* DYNAMIC */
  29.  
  30.   swactive=       /* not in switch */
  31.   stagenext=      /* direct output mode */
  32.   iflevel=        /* #if... nesting level = 0 */
  33.   skiplevel=      /* #if... not encountered */
  34.   macptr=         /* clear the macro pool */
  35.   csp =           /* stack ptr (relative) */
  36.   errflag=        /* not skipping errors till ";" */
  37.   eof=            /* not eof yet */
  38.   ncmp=           /* not in compound statement */
  39.   files=
  40.   filearg= 0;
  41.   quote[1]='\0';
  42.   func1=          /* first function */
  43.   ccode=1;        /* enable preprocessing */
  44.   wqptr=wq;       /* clear while queue */
  45.   quote[0]='"';   /* fake a quote literal */
  46.   input=input2= EOF;
  47.  
  48. /*
  49.  * this is where the nitty-gritty begins
  50.  */
  51.   ask();  /* get user options */
  52.   openfile(); /* and initial input file */
  53.   preprocess(); /* fetch first line */
  54.  
  55. #ifdef DYNAMIC
  56.  
  57.   symtab=calloc((NUMLOCS*SYMAVG + NUMGLBS*SYMMAX), 1);
  58.  
  59. #endif /* DYNAMIC */
  60.       /*10*/
  61.  
  62.   glbptr=STARTGLB;
  63.   glbflag=1;
  64.   ctext=0;
  65.   header();          /* intro code */
  66.   setops();          /* set values in op arrays */
  67.   parse();           /* process ALL input */
  68.   outside();         /* verify outside any function */
  69.   trailer();         /* follow-up code */
  70.   fclose(output);    /*close output file*/
  71.   }
  72.  
  73. /*
  74. ** process all input text
  75. **
  76. ** At this level, only static declarations,
  77. **      defines, includes and function
  78. **      definitions are legal...
  79. */
  80. parse() {
  81.   while (eof==0) {
  82.     if(amatch("extern", 6))   dodeclare(EXTERNAL);
  83.     else if(dodeclare(STATIC));
  84.     else if(match("#asm"))    doasm();
  85.     else if(match("#include"))doinclude();
  86.     else if(match("#define")) addmac();
  87.     else                      newfunc();
  88.     blanks();       /* force eof if pending */
  89.     }
  90.   }
  91.  
  92. /*
  93. ** dump the literal pool
  94. */
  95. dumplits(size) int size; {
  96.   int j, k;
  97.   k=0;
  98.   while (k<litptr) {
  99.   poll(1); /* allow program interruption */
  100.   defstorage(size);
  101.   j=10;
  102.   while(j--) {
  103.     outdec(getint(litq+k, size));
  104.     k=k+size;
  105.     if ((j==0)|(k>=litptr)) {
  106.       nl();
  107.       break;
  108.       }
  109.     outbyte(',');
  110.     }
  111.   }
  112. }
  113.  
  114. /*
  115. ** dump zeroes for default initial values
  116. */
  117. dumpzero(size, count) int size, count; {
  118.   int j;
  119.   while (count > 0) {
  120.     poll(1); /* allow program interruption */
  121.     defstorage(size);
  122.     j=30;
  123.     while(j--) {
  124.       outdec(0);
  125.       if ((--count <= 0)|(j==0)) {
  126.         nl();
  127.         break;
  128.         }
  129.       outbyte(',');
  130.       }
  131.     }
  132.   }
  133.  
  134. /*
  135. ** verify compile ends outside any function
  136. */
  137. outside()  {
  138.   if (ncmp) error("no closing bracket");
  139.   }
  140.  
  141. /*
  142. ** get run options
  143. */
  144. ask() {
  145.   int i;
  146.   i=listfp=nxtlab=0;
  147.   output=stdout;  
  148.  
  149. #ifdef OPTIMIZE
  150.  
  151.   optimize=
  152.  
  153. #endif /* OPTIMIZE */
  154.  
  155.   alarm=monitor=pause=NO;
  156.   line=mline;
  157.   while(getarg(++i, line, LINESIZE, argcs, argvs)!=EOF) {
  158.     if(line[0]!='-') continue;
  159.     if((toupper(line[1])=='L')&(isdigit(line[2]))&(line[3]<=' ')) {
  160.       listfp=line[2]-'0';
  161.       continue;
  162.       }
  163.     if(line[2]<=' ') {
  164.       if(toupper(line[1])=='A') {
  165.         alarm=YES;
  166.         continue;
  167.         }
  168.       if(toupper(line[1])=='M') {
  169.         monitor=YES;
  170.         continue;
  171.         }
  172.  
  173. #ifdef OPTIMIZE
  174.  
  175.       if(toupper(line[1])=='O') {
  176.         optimize=YES;
  177.         continue;
  178.         }
  179.  
  180. #endif /* OPTIMIZE */
  181.  
  182.       if(toupper(line[1])=='P') {
  183.         pause=YES;
  184.         continue;
  185.         }
  186.       }
  187.  
  188. #ifndef LINK
  189.  
  190.     if(toupper(line[1])=='B') {
  191.       bump(0); bump(2);
  192.       if(number(&nxtlab)) continue;
  193.       }
  194.  
  195. #endif /* LINK */
  196.  
  197.     sout("usage: cc [file]... [-c] [-m] [-a] [-p] [-l#]", stderr);
  198.  
  199. #ifdef OPTIMIZE
  200.  
  201.     sout(" [-o]", stderr);
  202.  
  203. #endif /* OPTIMIZE */
  204.  
  205. #ifndef LINK
  206.  
  207.     sout(" [-b#]", stderr);
  208.  
  209. #endif /* LINK */
  210.  
  211.     sout("\n", stderr);
  212.     abort(ERRCODE);
  213.     }
  214.   }
  215.  
  216. /*
  217. ** input and output file opens
  218. */
  219. openfile() {  /*entire function revised*/ /*39*/
  220.   char outfn[15];
  221.   int i, j, ext;
  222.   input = EOF;
  223.   while (getarg(++filearg, pline, LINESIZE, argcs, argvs) !=EOF) {
  224.   /* "{" missing from text in Handbook -- probably pasteup error */
  225.     if(pline[0]=='-') continue;
  226.     ext = NO;
  227.     i = -1;
  228.     j = 0;
  229.     while (pline[++i])  {
  230.       if(pline[i] == '.')  {
  231.         ext = YES;
  232.         break;
  233.         }
  234.     if ( j < 10) outfn[j++] = pline[i];
  235.     }
  236.   if(!ext) {
  237.     strcpy(pline + i, ".C");
  238.     }
  239.   input = mustopen(pline, "r");
  240.   if(!files && isatty(stdout)) {
  241.     strcpy(outfn + j, ".MAC");
  242.     output = mustopen(outfn, "w");
  243.     }
  244.   files=YES;
  245.   kill();
  246.   return;
  247.   }
  248. if (files++) eof=YES;
  249. else input=stdin;
  250.   kill();
  251.   }
  252.   
  253. /*
  254. ** open a file with error checking
  255. */
  256. mustopen(fn, mode) char *fn, *mode; {       /*39*/
  257.   int fd;
  258.   if (fd = fopen(fn, mode)) return fd;
  259.   sout("open error on ",stderr);
  260.   lout(fn, stderr);
  261.   abort(ERRCODE);
  262.   }
  263.  
  264. setops() {
  265.   op2[00]=     op[00]=  ffor;  /* heir5 */
  266.   op2[01]=     op[01]= ffxor;  /* heir6 */
  267.   op2[02]=     op[02]= ffand;  /* heir7 */
  268.   op2[03]=     op[03]=  ffeq;  /* heir8 */
  269.   op2[04]=     op[04]=  ffne;
  270.   op2[05]=ule; op[05]=  ffle;  /* heir9 */
  271.   op2[06]=uge; op[06]=  ffge;
  272.   op2[07]=ult; op[07]=  fflt;
  273.   op2[08]=ugt; op[08]=  ffgt;
  274.   op2[09]=     op[09]= ffasr;  /* heir10 */
  275.   op2[10]=     op[10]= ffasl;
  276.   op2[11]=     op[11]= ffadd;  /* heir11 */
  277.   op2[12]=     op[12]= ffsub;
  278.   op2[13]=     op[13]=ffmult;  /* heir12 */
  279.   op2[14]=     op[14]= ffdiv;
  280.   op2[15]=     op[15]= ffmod;
  281.   }
  282.