home *** CD-ROM | disk | FTP | other *** search
/ Oakland CPM Archive / oakcpm.iso / cpm / misc / smc203.ark / CC11.C < prev    next >
Encoding:
C/C++ Source or Header  |  1983-07-28  |  10.1 KB  |  462 lines

  1.  
  2. /*
  3. ** execution begins here
  4. */
  5. /*
  6.  * there are four different versions of the beginning of the
  7.  * main procedure, depending on the compiler used and whether
  8.  * command line processing is used.
  9.  */
  10. #ifdef CMD_LINE
  11. #ifdef N_CMD_LN
  12.  
  13. #ifdef FULLC
  14. main(argc, argv) int argc; char *argv[]; {
  15. #else  /* FULLC */
  16. main(argc, argv) int argc, argv[]; {
  17. #endif  /* FULLC */
  18.  
  19.   int ii;
  20.  
  21.   if (argc > MAXARGS) {
  22.     lout("too many command arguments", stderr);
  23.     abort(ERRCODE);
  24.     }
  25.   argcs=argc;
  26.   ii = 0;
  27.   while(argc--)
  28.     argvs[ii++] = *argv++;  /* make static copy of args */
  29.  
  30. #else  /* N_CMD_LN */
  31. /*
  32.  * original command line processing.
  33.  * NOTE: the function 'getarg' is not in the library
  34.  * supplied with this distribution.
  35.  */
  36. main(argc, argv) int argc, *argv; {
  37.   argcs=argc;
  38.   argvs=argv;
  39. #endif  /* N_CMD_LN */
  40.  
  41. #else /* CMD_LINE */
  42. main() {
  43. #endif /* CMD_LINE */
  44.  
  45.   sout("Small-C Ver. ", stderr);
  46.   sout(VERSION, stderr);
  47.   sout("   ", stderr);
  48.   lout(DATE, stderr);
  49.  
  50. #ifdef DYNAMIC
  51.   swnext=alloc(SWTABSZ);
  52.   swend=swnext+((SWTABSZ-SWSIZ)>>1);
  53.   stage=alloc(STAGESIZE);
  54.   stagelast=stage+STAGELIMIT;
  55.   wq=alloc(WQTABSZ*BPW);
  56.   litq=alloc(LITABSZ);
  57.  
  58. #ifdef HASH
  59.   macn=alloc(MACNSIZE);
  60.   cptr=macn-1;
  61.   while(++cptr < MACNEND) *cptr=0;
  62. #endif /* HASH */
  63.  
  64.   macq=alloc(MACQSIZE);
  65.   pline=alloc(LINESIZE);
  66.   mline=alloc(LINESIZE);
  67.  
  68. #else /* DYNAMIC */
  69.   swnext=swq;
  70.   swend=swnext+SWTABSZ-SWSIZ;
  71.   stagelast=stage+STAGELIMIT;
  72. #endif /* DYNAMIC */
  73.  
  74.   swactive=       /* not in switch */
  75.   stagenext=      /* direct output mode */
  76.   iflevel=        /* #if... nesting level = 0 */
  77.   skiplevel=      /* #if... not encountered */
  78.   macptr=         /* clear the macro pool */
  79.   csp =           /* stack ptr (relative) */
  80.   errflag=        /* not skipping errors till ";" */
  81.   eof=            /* not eof yet */
  82.   ncmp=           /* not in compound statement */
  83.   files=
  84.   filearg= 0;
  85.   quote[1]='\0';
  86.   func1=          /* first function */
  87.   ccode=1;        /* enable preprocessing */
  88.   wqptr=wq;       /* clear while queue */
  89.   quote[0]='"';   /* fake a quote literal */
  90.   input=input2= EOF;
  91.  
  92. /*
  93.  * this is where the nitty-gritty begins
  94.  */
  95.   ask();          /* get user options */
  96.   openin();       /* and initial input file */
  97.   preprocess();   /* fetch first line */
  98.  
  99.  
  100. #ifdef DYNAMIC
  101. #ifdef HASH
  102.   symtab=alloc(NUMLOCS*SYMAVG + NUMGLBS*SYMMAX);
  103. #else /* HASH */
  104.   symtab=alloc(NUMLOCS*SYMAVG);
  105.   /*  global space is allocated with each new entry  */
  106. #endif /* HASH */
  107. #endif /* DYNAMIC */
  108.  
  109. #ifdef HASH
  110.   cptr=STARTGLB-1;
  111.   while(++cptr < ENDGLB) *cptr=0;
  112. #endif /* HASH */
  113.  
  114.   glbptr=STARTGLB;
  115.   glbflag=1;
  116.   ctext=0;
  117.   header();          /* intro code */
  118.   setops();          /* set values in op arrays */
  119.   parse();           /* process ALL input */
  120.   outside();         /* verify outside any function */
  121.   trailer();         /* follow-up code */
  122.   fclose(output);
  123.   }
  124.  
  125. /*
  126. ** process all input text
  127. **
  128. ** At this level, only static declarations,
  129. **      defines, includes and function
  130. **      definitions are legal...
  131. */
  132. parse() {
  133.   while (eof==0) {
  134.     if(amatch("extern", 6))   dodeclare(EXTERNAL);
  135.     else if(dodeclare(STATIC));
  136.     else if(match("#asm"))    doasm();
  137.     else if(match("#include"))doinclude();
  138.     else if(match("#define")) addmac();
  139.     else                      newfunc();
  140.     blanks();       /* force eof if pending */
  141.     }
  142.   }
  143.  
  144. /*
  145. ** dump the literal pool
  146. */
  147. dumplits(size) int size; {
  148.   int j, k;
  149.   k=0;
  150.   while (k<litptr) {
  151.  
  152. #ifdef POLL
  153.     CCPOLL(1); /* allow program interruption */
  154. #endif
  155.  
  156.     defstorage(size);
  157.     j=10;
  158.     while(j--) {
  159.       outdec(getint(litq+k, size));
  160.       k=k+size;
  161.       if ((j==0)|(k>=litptr)) {
  162.         nl();
  163.         break;
  164.         }
  165.       outbyte(',');
  166.       }
  167.     }
  168.   }
  169.  
  170. /*
  171. ** dump zeroes for default initial values
  172. */
  173. dumpzero(size, count) int size, count; {
  174.   int j;
  175.   while (count > 0) {
  176.  
  177. #ifdef POLL
  178.     CCPOLL(1); /* allow program interruption */
  179. #endif
  180.  
  181.     defstorage(size);
  182.     j=30;
  183.     while(j--) {
  184.       outdec(0);
  185.       if ((--count <= 0)|(j==0)) {
  186.         nl();
  187.         break;
  188.         }
  189.       outbyte(',');
  190.       }
  191.     }
  192.   }
  193.  
  194. /*
  195. ** verify compile ends outside any function
  196. */
  197. outside()  {
  198.   if (ncmp) error("no closing bracket");
  199.   }
  200.  
  201. /*
  202. ** get run options
  203. */
  204. #ifdef CMD_LINE
  205. ask() {
  206.   int i;
  207.   i=listfp=nxtlab=0;
  208.  
  209. #ifdef C80
  210.   output=fout;    /* V6 convention */
  211. #else /* C80 */
  212.   output=stdout;  /* V7 convention */
  213. #endif /* C80 */
  214.  
  215. #ifdef OPTIMIZE
  216.   optimize=
  217. #endif /* OPTIMIZE */
  218.  
  219.   alarm=monitor=pause=m80flg=NO;
  220.   line=mline;
  221.  
  222. #ifdef N_CMD_LN
  223.   while(++i < argcs) {
  224.     line = argvs[i];
  225.     if (line[0] == '-') {
  226.       switch (upper(line[1])) {
  227.         case 'L':  if (numeric(line[2]) & (line[3] <= ' ')) {
  228.                      listfp = line[2] - '0';
  229.                      break;
  230.                      }
  231.                    else
  232.                      goto errcase;
  233.         case 'A':  alarm = YES;
  234.                    break;
  235.         case 'M':  monitor = YES;
  236.                    break;
  237.         case 'C':  m80flg = YES;
  238.                    break;
  239.         case 'P':  pause = YES;
  240.                    break;
  241.  
  242. #ifdef OPTIMIZE
  243.         case 'O':  optimize = YES;
  244.                    break;
  245. #endif /* OPTIMIZE */
  246.  
  247. #ifndef LINK
  248.         case 'B':  {
  249.                     bump(0); bump(2);
  250.                     if(number(&nxtlab)) break;
  251.                     }
  252.                     /* fall through to error case */
  253. #endif /* LINK */
  254.  
  255.         errcase:
  256.         default:  sout("usage: cc [file]... [-c] [-m] [-a] [-p] [-l#]", stderr);
  257.  
  258. #ifdef OPTIMIZE
  259.                   sout(" [-o]", stderr);
  260. #endif /* OPTIMIZE */
  261.  
  262. #ifndef LINK
  263.                   sout(" [-b#]", stderr);
  264. #endif /* LINK */
  265.  
  266.                   sout("\n", stderr);
  267.                   abort(ERRCODE);
  268.         }
  269.       }
  270.     }
  271.  
  272. #else   /* N_CMD_LN */
  273.   /*
  274.   ** use original input processing abortion
  275.   */
  276.   while(getarg(++i, line, LINESIZE, argcs, argvs)!=EOF) {
  277.     if(line[0]!='-') continue;
  278.     if((upper(line[1])=='L')&(numeric(line[2]))&(line[3]<=' ')) {
  279.       listfp=line[2]-'0';
  280.       continue;
  281.       }
  282.     if(line[2]<=' ') {
  283.       if(upper(line[1])=='A') {
  284.         alarm=YES;
  285.         continue;
  286.         }
  287.       if(upper(line[1])=='M') {
  288.         monitor=YES;
  289.         continue;
  290.         }
  291.       if(upper(line[1]=='C') {
  292.         m80flg=YES;
  293.         continue;
  294.         }
  295. #ifdef OPTIMIZE
  296.       if(upper(line[1])=='O') {
  297.         optimize=YES;
  298.         continue;
  299.         }
  300. #endif /* OPTIMIZE */
  301.       if(upper(line[1])=='P') {
  302.         pause=YES;
  303.         continue;
  304.         }
  305.       }
  306. #ifndef LINK
  307.     if(upper(line[1])=='B') {
  308.       bump(0); bump(2);
  309.       if(number(&nxtlab)) continue;
  310.       }
  311. #endif /* LINK */
  312.  
  313.     sout("usage: cc [file]... [-c] [-m] [-a] [-p] [-l#]", stderr);
  314.  
  315. #ifdef OPTIMIZE
  316.     sout(" [-o]", stderr);
  317. #endif /* OPTIMIZE */
  318.  
  319. #ifndef LINK
  320.     sout(" [-b#]", stderr);
  321. #endif /* LINK */
  322.  
  323.     sout("\n", stderr);
  324.     abort(ERRCODE);
  325.     }
  326. #endif  /* N_CMD_LN */
  327.  
  328.   }
  329.  
  330. #else /* CMD_LINE */
  331. ask() {
  332.  
  333. #ifdef OPTIMIZE
  334.   optimize=
  335. #endif /* OPTIMIZE */
  336.  
  337.   monitor=alarm=pause=m80flg=listfp=nxtlab=0;
  338.   line=mline;
  339.   while(1) {
  340.     prompt("Output file: ", line, LINESIZE);
  341.     if(output=fopen(line, "w")) break;
  342.     else { sout("open error: ", stderr); lout(line, stderr); }
  343.     }
  344.  
  345. #ifndef LINK
  346.   while(1) {
  347.     prompt("Beginning label number: ", line, LINESIZE);
  348.     bump(0);
  349.     if(number(&nxtlab)) break;
  350.     }
  351. #endif /* LINK */
  352.  
  353.   while(1) {
  354.     prompt("Monitor function headers? ", line, LINESIZE);
  355.     if(upper(*line)=='Y') monitor=YES;
  356.     else if(upper(*line)!='N') continue;
  357.     break;
  358.     }
  359.   while(1) {
  360.     prompt("Sound alarm on errors? ", line, LINESIZE);
  361.     if(upper(*line)=='Y') alarm=YES;
  362.     else if(upper(*line)!='N') continue;
  363.     break;
  364.     }
  365.   while(1) {
  366.     prompt("Pause on errors? ", line, LINESIZE);
  367.     if(upper(*line)=='Y') pause=YES;
  368.     else if(upper(*line)!='N') continue;
  369.     break;
  370.     }
  371.   while(1) {
  372.     prompt("Generate M80 code? ", line, LINESIZE);
  373.     if(upper(*line)=='Y') m80flg=YES;
  374.     else if(upper(*line)!='N') continue;
  375.     break;
  376.     }
  377.  
  378. #ifdef OPTIMIZE
  379.   while(1) {
  380.     prompt("Optimize for size? ", line, LINESIZE);
  381.     if(upper(*line)=='Y') optimize=YES;
  382.     else if(upper(*line)!='N') continue;
  383.     break;
  384.     }
  385. #endif /* OPTIMIZE */
  386.  
  387.   while(1) {
  388.     prompt("Listing file descriptor: ", line, LINESIZE);
  389.     if(numeric(*line)&(line[1]==NULL)) listfp = *line-'0';
  390.     else if(*line!=NULL) continue;
  391.     break;
  392.     }
  393.   }
  394. #endif /* CMD_LINE */
  395.  
  396. /*
  397. ** get next input file
  398. */
  399. openin() {
  400.   input = EOF;
  401.   line = pline;
  402.  
  403. #ifdef CMD_LINE
  404.  
  405. #ifdef N_CMD_LN
  406.   while(++filearg < argcs) {
  407.     line = argvs[filearg];
  408. #else  /* N_CMD_LN */
  409.   while(getarg(++filearg, line, LINESIZE, argcs, argvs)!=EOF) {
  410. #endif  /* N_CMD_LN */
  411.     if(line[0]=='-') continue;
  412.  
  413. #else /* CMD_LINE */
  414.   while(prompt("Input file: ", line, LINESIZE)) {
  415. #endif /* CMD_LINE */
  416.  
  417.     if((input=fopen(line,"r"))==NULL) {
  418.       sout("open error: ", stderr);
  419.       lout(line, stderr);
  420.       abort(ERRCODE);
  421.       }
  422.     files=YES;
  423.     kill();
  424.     return;
  425.     }
  426.   if(files++) eof=YES;
  427.  
  428. #ifdef C80
  429.   else input=fin;    /* V6 convention */
  430. #else
  431.   else input=stdin;  /* V7 convention */
  432. #endif
  433.  
  434.   kill();
  435.   }
  436.  
  437. #ifndef CMD_LINE
  438. prompt(msg, ans, anslen) char *msg, *ans; int anslen; {
  439.   sout(msg, stderr);
  440.   xgets(ans, anslen, stderr);
  441.   }
  442. #endif /* CMD_LINE */
  443.  
  444. setops() {
  445.   op2[00]=     op[00]=  zzor;  /* heir5 */
  446.   op2[01]=     op[01]= zzxor;  /* heir6 */
  447.   op2[02]=     op[02]= zzand;  /* heir7 */
  448.   op2[03]=     op[03]=  zzeq;  /* heir8 */
  449.   op2[04]=     op[04]=  zzne;
  450.   op2[05]=ule; op[05]=  zzle;  /* heir9 */
  451.   op2[06]=uge; op[06]=  zzge;
  452.   op2[07]=ult; op[07]=  zzlt;
  453.   op2[08]=ugt; op[08]=  zzgt;
  454.   op2[09]=     op[09]= zzasr;  /* heir10 */
  455.   op2[10]=     op[10]= zzasl;
  456.   op2[11]=     op[11]= zzadd;  /* heir11 */
  457.   op2[12]=     op[12]= zzsub;
  458.   op2[13]=     op[13]=zzmult;  /* heir12 */
  459.   op2[14]=     op[14]= zzdiv;
  460.   op2[15]=     op[15]= zzmod;
  461.   }
  462. σσσσσσσσσσσσσσσσσσσ