home *** CD-ROM | disk | FTP | other *** search
- /*
- ** execution begins here
- */
- main(argc, argv) int argc, *argv; {
- argcs=argc;
- argvs=argv;
-
- #ifdef DYNAMIC
-
- swnext=calloc(SWTABSZ, 1);
- swend=swnext+((SWTABSZ-SWSIZ)>>1);
- stage=calloc(STAGESIZE, 1);
- stagelast=stage+STAGELIMIT;
- wq=calloc(WQTABSZ*BPW, 1);
- litq=calloc(LITABSZ, 1);
- macn=calloc(MACNSIZE,1);
- /*10*/
- macq=calloc(MACQSIZE, 1);
- pline=calloc(LINESIZE, 1);
- mline=calloc(LINESIZE, 1);
-
- #else /* DYNAMIC */
-
- swnext=swq;
- swend=swnext+SWTABSZ-SWSIZ;
- stagelast=stage+STAGELIMIT;
-
- #endif /* DYNAMIC */
-
- swactive= /* not in switch */
- stagenext= /* direct output mode */
- iflevel= /* #if... nesting level = 0 */
- skiplevel= /* #if... not encountered */
- macptr= /* clear the macro pool */
- csp = /* stack ptr (relative) */
- errflag= /* not skipping errors till ";" */
- eof= /* not eof yet */
- ncmp= /* not in compound statement */
- files=
- filearg= 0;
- quote[1]='\0';
- func1= /* first function */
- ccode=1; /* enable preprocessing */
- wqptr=wq; /* clear while queue */
- quote[0]='"'; /* fake a quote literal */
- input=input2= EOF;
-
- /*
- * this is where the nitty-gritty begins
- */
- ask(); /* get user options */
- openfile(); /* and initial input file */
- preprocess(); /* fetch first line */
-
- #ifdef DYNAMIC
-
- symtab=calloc((NUMLOCS*SYMAVG + NUMGLBS*SYMMAX), 1);
-
- #endif /* DYNAMIC */
- /*10*/
-
- glbptr=STARTGLB;
- glbflag=1;
- ctext=0;
- header(); /* intro code */
- setops(); /* set values in op arrays */
- parse(); /* process ALL input */
- outside(); /* verify outside any function */
- trailer(); /* follow-up code */
- fclose(output); /*close output file*/
- }
-
- /*
- ** process all input text
- **
- ** At this level, only static declarations,
- ** defines, includes and function
- ** definitions are legal...
- */
- parse() {
- while (eof==0) {
- if(amatch("extern", 6)) dodeclare(EXTERNAL);
- else if(dodeclare(STATIC));
- else if(match("#asm")) doasm();
- else if(match("#include"))doinclude();
- else if(match("#define")) addmac();
- else newfunc();
- blanks(); /* force eof if pending */
- }
- }
-
- /*
- ** dump the literal pool
- */
- dumplits(size) int size; {
- int j, k;
- k=0;
- while (k<litptr) {
- poll(1); /* allow program interruption */
- defstorage(size);
- j=10;
- while(j--) {
- outdec(getint(litq+k, size));
- k=k+size;
- if ((j==0)|(k>=litptr)) {
- nl();
- break;
- }
- outbyte(',');
- }
- }
- }
-
- /*
- ** dump zeroes for default initial values
- */
- dumpzero(size, count) int size, count; {
- int j;
- while (count > 0) {
- poll(1); /* allow program interruption */
- defstorage(size);
- j=30;
- while(j--) {
- outdec(0);
- if ((--count <= 0)|(j==0)) {
- nl();
- break;
- }
- outbyte(',');
- }
- }
- }
-
- /*
- ** verify compile ends outside any function
- */
- outside() {
- if (ncmp) error("no closing bracket");
- }
-
- /*
- ** get run options
- */
- ask() {
- int i;
- i=listfp=nxtlab=0;
- output=stdout;
-
- #ifdef OPTIMIZE
-
- optimize=
-
- #endif /* OPTIMIZE */
-
- alarm=monitor=pause=NO;
- line=mline;
- while(getarg(++i, line, LINESIZE, argcs, argvs)!=EOF) {
- if(line[0]!='-') continue;
- if((toupper(line[1])=='L')&(isdigit(line[2]))&(line[3]<=' ')) {
- listfp=line[2]-'0';
- continue;
- }
- if(line[2]<=' ') {
- if(toupper(line[1])=='A') {
- alarm=YES;
- continue;
- }
- if(toupper(line[1])=='M') {
- monitor=YES;
- continue;
- }
-
- #ifdef OPTIMIZE
-
- if(toupper(line[1])=='O') {
- optimize=YES;
- continue;
- }
-
- #endif /* OPTIMIZE */
-
- if(toupper(line[1])=='P') {
- pause=YES;
- continue;
- }
- }
-
- #ifndef LINK
-
- if(toupper(line[1])=='B') {
- bump(0); bump(2);
- if(number(&nxtlab)) continue;
- }
-
- #endif /* LINK */
-
- sout("usage: cc [file]... [-c] [-m] [-a] [-p] [-l#]", stderr);
-
- #ifdef OPTIMIZE
-
- sout(" [-o]", stderr);
-
- #endif /* OPTIMIZE */
-
- #ifndef LINK
-
- sout(" [-b#]", stderr);
-
- #endif /* LINK */
-
- sout("\n", stderr);
- abort(ERRCODE);
- }
- }
-
- /*
- ** input and output file opens
- */
- openfile() { /*entire function revised*/ /*39*/
- char outfn[15];
- int i, j, ext;
- input = EOF;
- while (getarg(++filearg, pline, LINESIZE, argcs, argvs) !=EOF) {
- /* "{" missing from text in Handbook -- probably pasteup error */
- if(pline[0]=='-') continue;
- ext = NO;
- i = -1;
- j = 0;
- while (pline[++i]) {
- if(pline[i] == '.') {
- ext = YES;
- break;
- }
- if ( j < 10) outfn[j++] = pline[i];
- }
- if(!ext) {
- strcpy(pline + i, ".C");
- }
- input = mustopen(pline, "r");
- if(!files && isatty(stdout)) {
- strcpy(outfn + j, ".MAC");
- output = mustopen(outfn, "w");
- }
- files=YES;
- kill();
- return;
- }
- if (files++) eof=YES;
- else input=stdin;
- kill();
- }
-
- /*
- ** open a file with error checking
- */
- mustopen(fn, mode) char *fn, *mode; { /*39*/
- int fd;
- if (fd = fopen(fn, mode)) return fd;
- sout("open error on ",stderr);
- lout(fn, stderr);
- abort(ERRCODE);
- }
-
- setops() {
- op2[00]= op[00]= ffor; /* heir5 */
- op2[01]= op[01]= ffxor; /* heir6 */
- op2[02]= op[02]= ffand; /* heir7 */
- op2[03]= op[03]= ffeq; /* heir8 */
- op2[04]= op[04]= ffne;
- op2[05]=ule; op[05]= ffle; /* heir9 */
- op2[06]=uge; op[06]= ffge;
- op2[07]=ult; op[07]= fflt;
- op2[08]=ugt; op[08]= ffgt;
- op2[09]= op[09]= ffasr; /* heir10 */
- op2[10]= op[10]= ffasl;
- op2[11]= op[11]= ffadd; /* heir11 */
- op2[12]= op[12]= ffsub;
- op2[13]= op[13]=ffmult; /* heir12 */
- op2[14]= op[14]= ffdiv;
- op2[15]= op[15]= ffmod;
- }