xref: /csrg-svn/old/pcc/mip/common.c (revision 32805)
1*32805Sdonn /*	common.c	4.3	87/12/09	*/
218385Sralph 
318385Sralph #ifdef PASS1COMMON
418385Sralph #include "pass1.h"
518385Sralph #else
618385Sralph #ifdef PASS2COMMON
718385Sralph #include "pass2.h"
818385Sralph #endif
918385Sralph #endif
1018385Sralph 
1118385Sralph #ifdef FORT
1218385Sralph #undef BUFSTDERR
1318385Sralph #endif
1418385Sralph #ifndef ONEPASS
1518385Sralph #undef BUFSTDERR
1618385Sralph #endif
1718385Sralph # ifndef EXIT
1818385Sralph # define EXIT exit
1918385Sralph # endif
2018385Sralph 
2118385Sralph int nerrors = 0;  /* number of errors */
2218385Sralph 
2318385Sralph extern unsigned int offsz;
2418385Sralph 
2518385Sralph unsigned caloff(){
2618385Sralph 	register i;
2718385Sralph 	unsigned int temp;
2818385Sralph 	unsigned int off;
2918385Sralph 	temp = 1;
3018385Sralph 	i = 0;
3118385Sralph 	do {
3218385Sralph 		temp <<= 1;
3318385Sralph 		++i;
3418385Sralph 		} while( temp != 0 );
3518385Sralph 	off = 1 << (i-1);
3618385Sralph 	return (off);
3718385Sralph 	}
3818385Sralph 
3918385Sralph NODE *lastfree;  /* pointer to last free node; (for allocator) */
4018385Sralph 
4118385Sralph 	/* VARARGS1 */
4218385Sralph uerror( s, a ) char *s; { /* nonfatal error message */
4318385Sralph 	/* the routine where is different for pass 1 and pass 2;
4418385Sralph 	/*  it tells where the error took place */
4518385Sralph 
4618385Sralph 	++nerrors;
4718385Sralph 	where('u');
4818385Sralph 	fprintf( stderr, s, a );
4918385Sralph 	fprintf( stderr, "\n" );
5018385Sralph #ifdef BUFSTDERR
5118385Sralph 	fflush(stderr);
5218385Sralph #endif
5318385Sralph 	if( nerrors > 30 ) cerror( "too many errors");
5418385Sralph 	}
5518385Sralph 
5618385Sralph 	/* VARARGS1 */
5718385Sralph cerror( s, a, b, c ) char *s; { /* compiler error: die */
5818385Sralph 	where('c');
5918385Sralph 	if( nerrors && nerrors <= 30 ){ /* give the compiler the benefit of the doubt */
6018385Sralph 		fprintf( stderr, "cannot recover from earlier errors: goodbye!\n" );
6118385Sralph 		}
6218385Sralph 	else {
6318385Sralph 		fprintf( stderr, "compiler error: " );
6418385Sralph 		fprintf( stderr, s, a, b, c );
6518385Sralph 		fprintf( stderr, "\n" );
6618385Sralph 		}
6718385Sralph #ifdef BUFSTDERR
6818385Sralph 	fflush(stderr);
6918385Sralph #endif
7018385Sralph 	EXIT(1);
7118385Sralph 	}
7218385Sralph 
7318385Sralph int Wflag = 0; /* Non-zero means do not print warnings */
7418385Sralph 
7518385Sralph 	/* VARARGS1 */
7618385Sralph werror( s, a, b ) char *s; {  /* warning */
7718385Sralph 	if(Wflag) return;
7818385Sralph 	where('w');
7918385Sralph 	fprintf( stderr, "warning: " );
8018385Sralph 	fprintf( stderr, s, a, b );
8118385Sralph 	fprintf( stderr, "\n" );
8218385Sralph #ifdef BUFSTDERR
8318385Sralph 	fflush(stderr);
8418385Sralph #endif
8518385Sralph 	}
8618385Sralph 
8718385Sralph tinit(){ /* initialize expression tree search */
8818385Sralph 
8924401Smckusick 	register NODE *p;
9018385Sralph 
9118385Sralph 	for( p=node; p<= &node[TREESZ-1]; ++p ) p->in.op = FREE;
9218385Sralph 	lastfree = node;
9318385Sralph 
9418385Sralph 	}
9518385Sralph 
9618385Sralph # define TNEXT(p) (p== &node[TREESZ-1]?node:p+1)
9718385Sralph 
9818385Sralph NODE *
9918385Sralph talloc(){
10024401Smckusick 	register NODE *p, *q;
10118385Sralph 
10218385Sralph 	q = lastfree;
10318385Sralph 	for( p = TNEXT(q); p!=q; p= TNEXT(p))
10418385Sralph 		if( p->in.op ==FREE ) return(lastfree=p);
10518385Sralph 
10618385Sralph 	cerror( "out of tree space; simplify expression");
10718385Sralph 	/* NOTREACHED */
10818385Sralph 	}
10918385Sralph 
11018385Sralph tcheck(){ /* ensure that all nodes have been freed */
11118385Sralph 
11224401Smckusick 	register NODE *p;
11318385Sralph 
11418385Sralph 	if( !nerrors )
11518385Sralph 		for( p=node; p<= &node[TREESZ-1]; ++p )
11618385Sralph 			if( p->in.op != FREE ) cerror( "wasted space: %o", p );
11718385Sralph 	tinit();
11818385Sralph #ifdef FLEXNAMES
11918385Sralph 	freetstr();
12018385Sralph #endif
12118385Sralph 	}
12218385Sralph tfree( p )  NODE *p; {
12318385Sralph 	/* free the tree p */
12418385Sralph 	extern tfree1();
12518385Sralph 
12618385Sralph 	if( p->in.op != FREE ) walkf( p, tfree1 );
12718385Sralph 
12818385Sralph 	}
12918385Sralph 
13018385Sralph tfree1(p)  NODE *p; {
13118385Sralph 	if( p == 0 ) cerror( "freeing blank tree!");
13218385Sralph 	else p->in.op = FREE;
13318385Sralph 	}
13418385Sralph 
13518385Sralph fwalk( t, f, down ) register NODE *t; int (*f)(); {
13618385Sralph 
13718385Sralph 	int down1, down2;
13818385Sralph 
13918385Sralph 	more:
14018385Sralph 	down1 = down2 = 0;
14118385Sralph 
14218385Sralph 	(*f)( t, down, &down1, &down2 );
14318385Sralph 
14418385Sralph 	switch( optype( t->in.op ) ){
14518385Sralph 
14618385Sralph 	case BITYPE:
14718385Sralph 		fwalk( t->in.left, f, down1 );
14818385Sralph 		t = t->in.right;
14918385Sralph 		down = down2;
15018385Sralph 		goto more;
15118385Sralph 
15218385Sralph 	case UTYPE:
15318385Sralph 		t = t->in.left;
15418385Sralph 		down = down1;
15518385Sralph 		goto more;
15618385Sralph 
15718385Sralph 		}
15818385Sralph 	}
15918385Sralph 
16024401Smckusick #ifndef vax
16118385Sralph walkf( t, f ) register NODE *t;  int (*f)(); {
16218385Sralph 	register opty;
16318385Sralph 
16418385Sralph 	opty = optype(t->in.op);
16518385Sralph 
16618385Sralph 	if( opty != LTYPE ) walkf( t->in.left, f );
16718385Sralph 	if( opty == BITYPE ) walkf( t->in.right, f );
16818385Sralph 	(*f)( t );
16918385Sralph 	}
17024401Smckusick #else
17124401Smckusick #define	NR	100
17218385Sralph 
17324401Smckusick /*
17424401Smckusick  * Deliberately avoids recursion -- use this version on machines with
17524401Smckusick  * expensive procedure calls.
17624401Smckusick  */
17724401Smckusick walkf(t, f)
17824401Smckusick 	register NODE *t;
17924401Smckusick 	register int (*f)();
18024401Smckusick {
18124401Smckusick 	register int i = 1;
18224401Smckusick 	register int opty = optype(t->in.op);
18324401Smckusick 	static NODE *at[NR];
18424401Smckusick 	static int ao[NR];
18518385Sralph 
18624401Smckusick #define	PUSH(dir, state) \
18724401Smckusick 	(ao[i] = state, at[i++] = t, t = t->in.dir, opty = optype(t->in.op))
18824401Smckusick #define	POP() \
18924401Smckusick 	(opty = ao[--i], t = at[i])
19018385Sralph 
19124401Smckusick 	do {
19224401Smckusick 		switch (opty) {
19324401Smckusick 		case LTYPE:	(*f)(t); POP(); break;
19424401Smckusick 		case UTYPE:	PUSH(left, LTYPE); break;
19524401Smckusick 		case BITYPE:	PUSH(left, BITYPE+1); break;
19624401Smckusick 		case BITYPE+1:	PUSH(right, LTYPE); break;
19724401Smckusick 		default:
19824401Smckusick 			cerror("bad op type in walkf");
19924401Smckusick 		}
20024401Smckusick 		if (i >= NR) {
20124401Smckusick 			walkf(t, f);
20224401Smckusick 			POP();
20324401Smckusick 		}
20424401Smckusick 	} while (i > 0);
20524401Smckusick }
20624401Smckusick #undef NR
20724401Smckusick #undef PUSH
20824401Smckusick #undef POP
20924401Smckusick #endif
21024401Smckusick 
21124401Smckusick 
21224401Smckusick 
21318385Sralph int dope[ DSIZE ];
21418385Sralph char *opst[DSIZE];
21518385Sralph 
21618385Sralph struct dopest { int dopeop; char opst[8]; int dopeval; } indope[] = {
21718385Sralph 
21818385Sralph 	NAME, "NAME", LTYPE,
21918385Sralph 	STRING, "STRING", LTYPE,
22018385Sralph 	REG, "REG", LTYPE,
22118385Sralph 	OREG, "OREG", LTYPE,
22218385Sralph 	ICON, "ICON", LTYPE,
22318385Sralph 	FCON, "FCON", LTYPE,
22418385Sralph 	DCON, "DCON", LTYPE,
22518385Sralph 	CCODES, "CCODES", LTYPE,
22618385Sralph 	UNARY MINUS, "U-", UTYPE,
22718385Sralph 	UNARY MUL, "U*", UTYPE,
22818385Sralph 	UNARY AND, "U&", UTYPE,
22918385Sralph 	UNARY CALL, "UCALL", UTYPE|CALLFLG,
23018385Sralph 	UNARY FORTCALL, "UFCALL", UTYPE|CALLFLG,
23118385Sralph 	NOT, "!", UTYPE|LOGFLG,
23218385Sralph 	COMPL, "~", UTYPE,
23318385Sralph 	FORCE, "FORCE", UTYPE,
23418385Sralph 	INIT, "INIT", UTYPE,
23518385Sralph 	SCONV, "SCONV", UTYPE,
23618385Sralph 	PCONV, "PCONV", UTYPE,
23718385Sralph 	PLUS, "+", BITYPE|FLOFLG|SIMPFLG|COMMFLG,
23818385Sralph 	ASG PLUS, "+=", BITYPE|ASGFLG|ASGOPFLG|FLOFLG|SIMPFLG|COMMFLG,
23918385Sralph 	MINUS, "-", BITYPE|FLOFLG|SIMPFLG,
24018385Sralph 	ASG MINUS, "-=", BITYPE|FLOFLG|SIMPFLG|ASGFLG|ASGOPFLG,
24118385Sralph 	MUL, "*", BITYPE|FLOFLG|MULFLG,
24218385Sralph 	ASG MUL, "*=", BITYPE|FLOFLG|MULFLG|ASGFLG|ASGOPFLG,
24318385Sralph 	AND, "&", BITYPE|SIMPFLG|COMMFLG,
24418385Sralph 	ASG AND, "&=", BITYPE|SIMPFLG|COMMFLG|ASGFLG|ASGOPFLG,
24518385Sralph 	QUEST, "?", BITYPE,
24618385Sralph 	COLON, ":", BITYPE,
24718385Sralph 	ANDAND, "&&", BITYPE|LOGFLG,
24818385Sralph 	OROR, "||", BITYPE|LOGFLG,
24918385Sralph 	CM, ",", BITYPE,
25018385Sralph 	COMOP, ",OP", BITYPE,
25118385Sralph 	ASSIGN, "=", BITYPE|ASGFLG,
25218385Sralph 	DIV, "/", BITYPE|FLOFLG|MULFLG|DIVFLG,
25318385Sralph 	ASG DIV, "/=", BITYPE|FLOFLG|MULFLG|DIVFLG|ASGFLG|ASGOPFLG,
25418385Sralph 	MOD, "%", BITYPE|DIVFLG,
25518385Sralph 	ASG MOD, "%=", BITYPE|DIVFLG|ASGFLG|ASGOPFLG,
25618385Sralph 	LS, "<<", BITYPE|SHFFLG,
25718385Sralph 	ASG LS, "<<=", BITYPE|SHFFLG|ASGFLG|ASGOPFLG,
25818385Sralph 	RS, ">>", BITYPE|SHFFLG,
25918385Sralph 	ASG RS, ">>=", BITYPE|SHFFLG|ASGFLG|ASGOPFLG,
26018385Sralph 	OR, "|", BITYPE|COMMFLG|SIMPFLG,
26118385Sralph 	ASG OR, "|=", BITYPE|COMMFLG|SIMPFLG|ASGFLG|ASGOPFLG,
26218385Sralph 	ER, "^", BITYPE|COMMFLG|SIMPFLG,
26318385Sralph 	ASG ER, "^=", BITYPE|COMMFLG|SIMPFLG|ASGFLG|ASGOPFLG,
26418385Sralph 	INCR, "++", BITYPE|ASGFLG,
26518385Sralph 	DECR, "--", BITYPE|ASGFLG,
26618385Sralph 	STREF, "->", BITYPE,
26718385Sralph 	CALL, "CALL", BITYPE|CALLFLG,
26818385Sralph 	FORTCALL, "FCALL", BITYPE|CALLFLG,
26918385Sralph 	EQ, "==", BITYPE|LOGFLG,
27018385Sralph 	NE, "!=", BITYPE|LOGFLG,
27118385Sralph 	LE, "<=", BITYPE|LOGFLG,
27218385Sralph 	LT, "<", BITYPE|LOGFLG,
27318385Sralph 	GE, ">", BITYPE|LOGFLG,
27418385Sralph 	GT, ">", BITYPE|LOGFLG,
27518385Sralph 	UGT, "UGT", BITYPE|LOGFLG,
27618385Sralph 	UGE, "UGE", BITYPE|LOGFLG,
27718385Sralph 	ULT, "ULT", BITYPE|LOGFLG,
27818385Sralph 	ULE, "ULE", BITYPE|LOGFLG,
27918385Sralph #ifdef ARS
28018385Sralph 	ARS, "A>>", BITYPE,
28118385Sralph #endif
28218385Sralph 	TYPE, "TYPE", LTYPE,
28318385Sralph 	LB, "[", BITYPE,
28418385Sralph 	CBRANCH, "CBRANCH", BITYPE,
28518385Sralph 	FLD, "FLD", UTYPE,
28618385Sralph 	PMCONV, "PMCONV", BITYPE,
28718385Sralph 	PVCONV, "PVCONV", BITYPE,
28818385Sralph 	RETURN, "RETURN", BITYPE|ASGFLG|ASGOPFLG,
28918385Sralph 	CAST, "CAST", BITYPE|ASGFLG|ASGOPFLG,
29018385Sralph 	GOTO, "GOTO", UTYPE,
29118385Sralph 	STASG, "STASG", BITYPE|ASGFLG,
29218385Sralph 	STARG, "STARG", UTYPE,
29318385Sralph 	STCALL, "STCALL", BITYPE|CALLFLG,
29418385Sralph 	UNARY STCALL, "USTCALL", UTYPE|CALLFLG,
29518385Sralph 
29618385Sralph 	-1,	"",	0
29718385Sralph };
29818385Sralph 
29918385Sralph mkdope(){
30018385Sralph 	register struct dopest *q;
30118385Sralph 
30218385Sralph 	for( q = indope; q->dopeop >= 0; ++q ){
30318385Sralph 		dope[q->dopeop] = q->dopeval;
30418385Sralph 		opst[q->dopeop] = q->opst;
30518385Sralph 		}
30618385Sralph 	}
30718385Sralph # ifndef BUG4
30818385Sralph tprint( t )  TWORD t; { /* output a nice description of the type of t */
30918385Sralph 
31018385Sralph 	static char * tnames[] = {
31118385Sralph 		"undef",
31218385Sralph 		"farg",
31318385Sralph 		"char",
31418385Sralph 		"short",
31518385Sralph 		"int",
31618385Sralph 		"long",
31718385Sralph 		"float",
31818385Sralph 		"double",
31918385Sralph 		"strty",
32018385Sralph 		"unionty",
32118385Sralph 		"enumty",
32218385Sralph 		"moety",
32318385Sralph 		"uchar",
32418385Sralph 		"ushort",
32518385Sralph 		"unsigned",
32618385Sralph 		"ulong",
32718385Sralph 		"?", "?"
32818385Sralph 		};
32918385Sralph 
33018385Sralph 	for(;; t = DECREF(t) ){
33118385Sralph 
33218385Sralph 		if( ISPTR(t) ) printf( "PTR " );
33318385Sralph 		else if( ISFTN(t) ) printf( "FTN " );
33418385Sralph 		else if( ISARY(t) ) printf( "ARY " );
33518385Sralph 		else {
33618385Sralph 			printf( "%s", tnames[t] );
33718385Sralph 			return;
33818385Sralph 			}
33918385Sralph 		}
34018385Sralph 	}
34118385Sralph # endif
34218385Sralph 
34318385Sralph #ifdef FLEXNAMES
34418385Sralph #define	NTSTRBUF	40
34518385Sralph #define	TSTRSZ		2048
34618385Sralph char	itstrbuf[TSTRSZ];
34718385Sralph char	*tstrbuf[NTSTRBUF] = { itstrbuf };
34818385Sralph char	**curtstr = tstrbuf;
34918385Sralph int	tstrused;
350*32805Sdonn char	*malloc();
351*32805Sdonn char	*strcpy();
35218385Sralph 
35318385Sralph char *
35418385Sralph tstr(cp)
35518385Sralph 	register char *cp;
35618385Sralph {
35718385Sralph 	register int i = strlen(cp);
35818385Sralph 	register char *dp;
35918385Sralph 
36018385Sralph 	if (tstrused + i >= TSTRSZ) {
36118385Sralph 		if (++curtstr >= &tstrbuf[NTSTRBUF])
36218385Sralph 			cerror("out of temporary string space");
36318385Sralph 		tstrused = 0;
36418385Sralph 		if (*curtstr == 0) {
365*32805Sdonn 			dp = malloc(TSTRSZ);
36618385Sralph 			if (dp == 0)
36718385Sralph 				cerror("out of memory (tstr)");
36818385Sralph 			*curtstr = dp;
36918385Sralph 		}
37018385Sralph 	}
371*32805Sdonn 	(void) strcpy(dp = *curtstr+tstrused, cp);
37218385Sralph 	tstrused += i + 1;
37318385Sralph 	return (dp);
37418385Sralph }
37518385Sralph #endif
376