xref: /csrg-svn/old/pcc/mip/common.c (revision 18385)
1*18385Sralph /*	common.c	4.1	85/03/19	*/
2*18385Sralph 
3*18385Sralph #ifdef PASS1COMMON
4*18385Sralph #include "pass1.h"
5*18385Sralph #else
6*18385Sralph #ifdef PASS2COMMON
7*18385Sralph #include "pass2.h"
8*18385Sralph #endif
9*18385Sralph #endif
10*18385Sralph 
11*18385Sralph #ifdef FORT
12*18385Sralph #undef BUFSTDERR
13*18385Sralph #endif
14*18385Sralph #ifndef ONEPASS
15*18385Sralph #undef BUFSTDERR
16*18385Sralph #endif
17*18385Sralph # ifndef EXIT
18*18385Sralph # define EXIT exit
19*18385Sralph # endif
20*18385Sralph 
21*18385Sralph int nerrors = 0;  /* number of errors */
22*18385Sralph 
23*18385Sralph extern unsigned int offsz;
24*18385Sralph 
25*18385Sralph unsigned caloff(){
26*18385Sralph 	register i;
27*18385Sralph 	unsigned int temp;
28*18385Sralph 	unsigned int off;
29*18385Sralph 	temp = 1;
30*18385Sralph 	i = 0;
31*18385Sralph 	do {
32*18385Sralph 		temp <<= 1;
33*18385Sralph 		++i;
34*18385Sralph 		} while( temp != 0 );
35*18385Sralph 	off = 1 << (i-1);
36*18385Sralph 	return (off);
37*18385Sralph 	}
38*18385Sralph 
39*18385Sralph NODE *lastfree;  /* pointer to last free node; (for allocator) */
40*18385Sralph 
41*18385Sralph 	/* VARARGS1 */
42*18385Sralph uerror( s, a ) char *s; { /* nonfatal error message */
43*18385Sralph 	/* the routine where is different for pass 1 and pass 2;
44*18385Sralph 	/*  it tells where the error took place */
45*18385Sralph 
46*18385Sralph 	++nerrors;
47*18385Sralph 	where('u');
48*18385Sralph 	fprintf( stderr, s, a );
49*18385Sralph 	fprintf( stderr, "\n" );
50*18385Sralph #ifdef BUFSTDERR
51*18385Sralph 	fflush(stderr);
52*18385Sralph #endif
53*18385Sralph 	if( nerrors > 30 ) cerror( "too many errors");
54*18385Sralph 	}
55*18385Sralph 
56*18385Sralph 	/* VARARGS1 */
57*18385Sralph cerror( s, a, b, c ) char *s; { /* compiler error: die */
58*18385Sralph 	where('c');
59*18385Sralph 	if( nerrors && nerrors <= 30 ){ /* give the compiler the benefit of the doubt */
60*18385Sralph 		fprintf( stderr, "cannot recover from earlier errors: goodbye!\n" );
61*18385Sralph 		}
62*18385Sralph 	else {
63*18385Sralph 		fprintf( stderr, "compiler error: " );
64*18385Sralph 		fprintf( stderr, s, a, b, c );
65*18385Sralph 		fprintf( stderr, "\n" );
66*18385Sralph 		}
67*18385Sralph #ifdef BUFSTDERR
68*18385Sralph 	fflush(stderr);
69*18385Sralph #endif
70*18385Sralph 	EXIT(1);
71*18385Sralph 	}
72*18385Sralph 
73*18385Sralph int Wflag = 0; /* Non-zero means do not print warnings */
74*18385Sralph 
75*18385Sralph 	/* VARARGS1 */
76*18385Sralph werror( s, a, b ) char *s; {  /* warning */
77*18385Sralph 	if(Wflag) return;
78*18385Sralph 	where('w');
79*18385Sralph 	fprintf( stderr, "warning: " );
80*18385Sralph 	fprintf( stderr, s, a, b );
81*18385Sralph 	fprintf( stderr, "\n" );
82*18385Sralph #ifdef BUFSTDERR
83*18385Sralph 	fflush(stderr);
84*18385Sralph #endif
85*18385Sralph 	}
86*18385Sralph 
87*18385Sralph tinit(){ /* initialize expression tree search */
88*18385Sralph 
89*18385Sralph 	NODE *p;
90*18385Sralph 
91*18385Sralph 	for( p=node; p<= &node[TREESZ-1]; ++p ) p->in.op = FREE;
92*18385Sralph 	lastfree = node;
93*18385Sralph 
94*18385Sralph 	}
95*18385Sralph 
96*18385Sralph # define TNEXT(p) (p== &node[TREESZ-1]?node:p+1)
97*18385Sralph 
98*18385Sralph NODE *
99*18385Sralph talloc(){
100*18385Sralph 	NODE *p, *q;
101*18385Sralph 
102*18385Sralph 	q = lastfree;
103*18385Sralph 	for( p = TNEXT(q); p!=q; p= TNEXT(p))
104*18385Sralph 		if( p->in.op ==FREE ) return(lastfree=p);
105*18385Sralph 
106*18385Sralph 	cerror( "out of tree space; simplify expression");
107*18385Sralph 	/* NOTREACHED */
108*18385Sralph 	}
109*18385Sralph 
110*18385Sralph tcheck(){ /* ensure that all nodes have been freed */
111*18385Sralph 
112*18385Sralph 	NODE *p;
113*18385Sralph 
114*18385Sralph 	if( !nerrors )
115*18385Sralph 		for( p=node; p<= &node[TREESZ-1]; ++p )
116*18385Sralph 			if( p->in.op != FREE ) cerror( "wasted space: %o", p );
117*18385Sralph 	tinit();
118*18385Sralph #ifdef FLEXNAMES
119*18385Sralph 	freetstr();
120*18385Sralph #endif
121*18385Sralph 	}
122*18385Sralph tfree( p )  NODE *p; {
123*18385Sralph 	/* free the tree p */
124*18385Sralph 	extern tfree1();
125*18385Sralph 
126*18385Sralph 	if( p->in.op != FREE ) walkf( p, tfree1 );
127*18385Sralph 
128*18385Sralph 	}
129*18385Sralph 
130*18385Sralph tfree1(p)  NODE *p; {
131*18385Sralph 	if( p == 0 ) cerror( "freeing blank tree!");
132*18385Sralph 	else p->in.op = FREE;
133*18385Sralph 	}
134*18385Sralph 
135*18385Sralph fwalk( t, f, down ) register NODE *t; int (*f)(); {
136*18385Sralph 
137*18385Sralph 	int down1, down2;
138*18385Sralph 
139*18385Sralph 	more:
140*18385Sralph 	down1 = down2 = 0;
141*18385Sralph 
142*18385Sralph 	(*f)( t, down, &down1, &down2 );
143*18385Sralph 
144*18385Sralph 	switch( optype( t->in.op ) ){
145*18385Sralph 
146*18385Sralph 	case BITYPE:
147*18385Sralph 		fwalk( t->in.left, f, down1 );
148*18385Sralph 		t = t->in.right;
149*18385Sralph 		down = down2;
150*18385Sralph 		goto more;
151*18385Sralph 
152*18385Sralph 	case UTYPE:
153*18385Sralph 		t = t->in.left;
154*18385Sralph 		down = down1;
155*18385Sralph 		goto more;
156*18385Sralph 
157*18385Sralph 		}
158*18385Sralph 	}
159*18385Sralph 
160*18385Sralph walkf( t, f ) register NODE *t;  int (*f)(); {
161*18385Sralph 	register opty;
162*18385Sralph 
163*18385Sralph 	opty = optype(t->in.op);
164*18385Sralph 
165*18385Sralph 	if( opty != LTYPE ) walkf( t->in.left, f );
166*18385Sralph 	if( opty == BITYPE ) walkf( t->in.right, f );
167*18385Sralph 	(*f)( t );
168*18385Sralph 	}
169*18385Sralph 
170*18385Sralph 
171*18385Sralph 
172*18385Sralph int dope[ DSIZE ];
173*18385Sralph char *opst[DSIZE];
174*18385Sralph 
175*18385Sralph struct dopest { int dopeop; char opst[8]; int dopeval; } indope[] = {
176*18385Sralph 
177*18385Sralph 	NAME, "NAME", LTYPE,
178*18385Sralph 	STRING, "STRING", LTYPE,
179*18385Sralph 	REG, "REG", LTYPE,
180*18385Sralph 	OREG, "OREG", LTYPE,
181*18385Sralph 	ICON, "ICON", LTYPE,
182*18385Sralph 	FCON, "FCON", LTYPE,
183*18385Sralph 	DCON, "DCON", LTYPE,
184*18385Sralph 	CCODES, "CCODES", LTYPE,
185*18385Sralph 	UNARY MINUS, "U-", UTYPE,
186*18385Sralph 	UNARY MUL, "U*", UTYPE,
187*18385Sralph 	UNARY AND, "U&", UTYPE,
188*18385Sralph 	UNARY CALL, "UCALL", UTYPE|CALLFLG,
189*18385Sralph 	UNARY FORTCALL, "UFCALL", UTYPE|CALLFLG,
190*18385Sralph 	NOT, "!", UTYPE|LOGFLG,
191*18385Sralph 	COMPL, "~", UTYPE,
192*18385Sralph 	FORCE, "FORCE", UTYPE,
193*18385Sralph 	INIT, "INIT", UTYPE,
194*18385Sralph 	SCONV, "SCONV", UTYPE,
195*18385Sralph 	PCONV, "PCONV", UTYPE,
196*18385Sralph 	PLUS, "+", BITYPE|FLOFLG|SIMPFLG|COMMFLG,
197*18385Sralph 	ASG PLUS, "+=", BITYPE|ASGFLG|ASGOPFLG|FLOFLG|SIMPFLG|COMMFLG,
198*18385Sralph 	MINUS, "-", BITYPE|FLOFLG|SIMPFLG,
199*18385Sralph 	ASG MINUS, "-=", BITYPE|FLOFLG|SIMPFLG|ASGFLG|ASGOPFLG,
200*18385Sralph 	MUL, "*", BITYPE|FLOFLG|MULFLG,
201*18385Sralph 	ASG MUL, "*=", BITYPE|FLOFLG|MULFLG|ASGFLG|ASGOPFLG,
202*18385Sralph 	AND, "&", BITYPE|SIMPFLG|COMMFLG,
203*18385Sralph 	ASG AND, "&=", BITYPE|SIMPFLG|COMMFLG|ASGFLG|ASGOPFLG,
204*18385Sralph 	QUEST, "?", BITYPE,
205*18385Sralph 	COLON, ":", BITYPE,
206*18385Sralph 	ANDAND, "&&", BITYPE|LOGFLG,
207*18385Sralph 	OROR, "||", BITYPE|LOGFLG,
208*18385Sralph 	CM, ",", BITYPE,
209*18385Sralph 	COMOP, ",OP", BITYPE,
210*18385Sralph 	ASSIGN, "=", BITYPE|ASGFLG,
211*18385Sralph 	DIV, "/", BITYPE|FLOFLG|MULFLG|DIVFLG,
212*18385Sralph 	ASG DIV, "/=", BITYPE|FLOFLG|MULFLG|DIVFLG|ASGFLG|ASGOPFLG,
213*18385Sralph 	MOD, "%", BITYPE|DIVFLG,
214*18385Sralph 	ASG MOD, "%=", BITYPE|DIVFLG|ASGFLG|ASGOPFLG,
215*18385Sralph 	LS, "<<", BITYPE|SHFFLG,
216*18385Sralph 	ASG LS, "<<=", BITYPE|SHFFLG|ASGFLG|ASGOPFLG,
217*18385Sralph 	RS, ">>", BITYPE|SHFFLG,
218*18385Sralph 	ASG RS, ">>=", BITYPE|SHFFLG|ASGFLG|ASGOPFLG,
219*18385Sralph 	OR, "|", BITYPE|COMMFLG|SIMPFLG,
220*18385Sralph 	ASG OR, "|=", BITYPE|COMMFLG|SIMPFLG|ASGFLG|ASGOPFLG,
221*18385Sralph 	ER, "^", BITYPE|COMMFLG|SIMPFLG,
222*18385Sralph 	ASG ER, "^=", BITYPE|COMMFLG|SIMPFLG|ASGFLG|ASGOPFLG,
223*18385Sralph 	INCR, "++", BITYPE|ASGFLG,
224*18385Sralph 	DECR, "--", BITYPE|ASGFLG,
225*18385Sralph 	STREF, "->", BITYPE,
226*18385Sralph 	CALL, "CALL", BITYPE|CALLFLG,
227*18385Sralph 	FORTCALL, "FCALL", BITYPE|CALLFLG,
228*18385Sralph 	EQ, "==", BITYPE|LOGFLG,
229*18385Sralph 	NE, "!=", BITYPE|LOGFLG,
230*18385Sralph 	LE, "<=", BITYPE|LOGFLG,
231*18385Sralph 	LT, "<", BITYPE|LOGFLG,
232*18385Sralph 	GE, ">", BITYPE|LOGFLG,
233*18385Sralph 	GT, ">", BITYPE|LOGFLG,
234*18385Sralph 	UGT, "UGT", BITYPE|LOGFLG,
235*18385Sralph 	UGE, "UGE", BITYPE|LOGFLG,
236*18385Sralph 	ULT, "ULT", BITYPE|LOGFLG,
237*18385Sralph 	ULE, "ULE", BITYPE|LOGFLG,
238*18385Sralph #ifdef ARS
239*18385Sralph 	ARS, "A>>", BITYPE,
240*18385Sralph #endif
241*18385Sralph 	TYPE, "TYPE", LTYPE,
242*18385Sralph 	LB, "[", BITYPE,
243*18385Sralph 	CBRANCH, "CBRANCH", BITYPE,
244*18385Sralph 	FLD, "FLD", UTYPE,
245*18385Sralph 	PMCONV, "PMCONV", BITYPE,
246*18385Sralph 	PVCONV, "PVCONV", BITYPE,
247*18385Sralph 	RETURN, "RETURN", BITYPE|ASGFLG|ASGOPFLG,
248*18385Sralph 	CAST, "CAST", BITYPE|ASGFLG|ASGOPFLG,
249*18385Sralph 	GOTO, "GOTO", UTYPE,
250*18385Sralph 	STASG, "STASG", BITYPE|ASGFLG,
251*18385Sralph 	STARG, "STARG", UTYPE,
252*18385Sralph 	STCALL, "STCALL", BITYPE|CALLFLG,
253*18385Sralph 	UNARY STCALL, "USTCALL", UTYPE|CALLFLG,
254*18385Sralph 
255*18385Sralph 	-1,	"",	0
256*18385Sralph };
257*18385Sralph 
258*18385Sralph mkdope(){
259*18385Sralph 	register struct dopest *q;
260*18385Sralph 
261*18385Sralph 	for( q = indope; q->dopeop >= 0; ++q ){
262*18385Sralph 		dope[q->dopeop] = q->dopeval;
263*18385Sralph 		opst[q->dopeop] = q->opst;
264*18385Sralph 		}
265*18385Sralph 	}
266*18385Sralph # ifndef BUG4
267*18385Sralph tprint( t )  TWORD t; { /* output a nice description of the type of t */
268*18385Sralph 
269*18385Sralph 	static char * tnames[] = {
270*18385Sralph 		"undef",
271*18385Sralph 		"farg",
272*18385Sralph 		"char",
273*18385Sralph 		"short",
274*18385Sralph 		"int",
275*18385Sralph 		"long",
276*18385Sralph 		"float",
277*18385Sralph 		"double",
278*18385Sralph 		"strty",
279*18385Sralph 		"unionty",
280*18385Sralph 		"enumty",
281*18385Sralph 		"moety",
282*18385Sralph 		"uchar",
283*18385Sralph 		"ushort",
284*18385Sralph 		"unsigned",
285*18385Sralph 		"ulong",
286*18385Sralph 		"?", "?"
287*18385Sralph 		};
288*18385Sralph 
289*18385Sralph 	for(;; t = DECREF(t) ){
290*18385Sralph 
291*18385Sralph 		if( ISPTR(t) ) printf( "PTR " );
292*18385Sralph 		else if( ISFTN(t) ) printf( "FTN " );
293*18385Sralph 		else if( ISARY(t) ) printf( "ARY " );
294*18385Sralph 		else {
295*18385Sralph 			printf( "%s", tnames[t] );
296*18385Sralph 			return;
297*18385Sralph 			}
298*18385Sralph 		}
299*18385Sralph 	}
300*18385Sralph # endif
301*18385Sralph 
302*18385Sralph #ifdef FLEXNAMES
303*18385Sralph #define	NTSTRBUF	40
304*18385Sralph #define	TSTRSZ		2048
305*18385Sralph char	itstrbuf[TSTRSZ];
306*18385Sralph char	*tstrbuf[NTSTRBUF] = { itstrbuf };
307*18385Sralph char	**curtstr = tstrbuf;
308*18385Sralph int	tstrused;
309*18385Sralph 
310*18385Sralph char *
311*18385Sralph tstr(cp)
312*18385Sralph 	register char *cp;
313*18385Sralph {
314*18385Sralph 	register int i = strlen(cp);
315*18385Sralph 	register char *dp;
316*18385Sralph 
317*18385Sralph 	if (tstrused + i >= TSTRSZ) {
318*18385Sralph 		if (++curtstr >= &tstrbuf[NTSTRBUF])
319*18385Sralph 			cerror("out of temporary string space");
320*18385Sralph 		tstrused = 0;
321*18385Sralph 		if (*curtstr == 0) {
322*18385Sralph 			dp = (char *)malloc(TSTRSZ);
323*18385Sralph 			if (dp == 0)
324*18385Sralph 				cerror("out of memory (tstr)");
325*18385Sralph 			*curtstr = dp;
326*18385Sralph 		}
327*18385Sralph 	}
328*18385Sralph 	strcpy(dp = *curtstr+tstrused, cp);
329*18385Sralph 	tstrused += i + 1;
330*18385Sralph 	return (dp);
331*18385Sralph }
332*18385Sralph #endif
333