xref: /csrg-svn/old/as.vax/asscan1.c (revision 13515)
15797Srrh /*
25797Srrh  *	Copyright (c) 1982 Regents of the University of California
35797Srrh  */
45797Srrh #ifndef lint
5*13515Srrh static char sccsid[] = "@(#)asscan1.c 4.5 06/30/83";
65797Srrh #endif not lint
75797Srrh 
85797Srrh #include "asscanl.h"
95797Srrh 
10*13515Srrh inittokfile()
115797Srrh {
125797Srrh 	if (passno == 1){
135797Srrh 		if (useVM){
145797Srrh 			bufstart = &tokbuf[0];
155797Srrh 			buftail = &tokbuf[1];
165797Srrh 			bufstart->tok_next = buftail;
175797Srrh 			buftail->tok_next = 0;
185797Srrh 		}
195797Srrh 		tokbuf[0].tok_count = -1;
205797Srrh 		tokbuf[1].tok_count = -1;
215797Srrh 	}
225797Srrh 	tok_temp = 0;
235797Srrh 	tok_free = 0;
245797Srrh 	bufno = 0;
255797Srrh 	emptybuf = &tokbuf[bufno];
265797Srrh 	tokptr = 0;
275797Srrh 	tokub = 0;
285797Srrh }
295797Srrh 
30*13515Srrh closetokfile()
315797Srrh {
325797Srrh 	if (passno == 1){
335797Srrh 		if (useVM){
345797Srrh 			emptybuf->toks[emptybuf->tok_count++] = PARSEEOF;
355797Srrh 		} else {
365797Srrh 			/*
375797Srrh 			 *	Clean up the buffers that haven't been
385797Srrh 			 *	written out yet
395797Srrh 			 */
405797Srrh 			if (tokbuf[bufno ^ 1].tok_count >= 0){
41*13515Srrh 				if (writeTEST((char *)&tokbuf[bufno ^ 1], sizeof *emptybuf, 1, tokfile)){
425797Srrh 				  badwrite:
435797Srrh 					yyerror("Unexpected end of file writing the interpass tmp file");
445797Srrh 				exit(2);
455797Srrh 				}
465797Srrh 			}
475797Srrh 			/*
485797Srrh 			 *	Ensure that we will read an End of file,
495797Srrh 			 *	if there are more than one file names
505797Srrh 			 *	in the argument list
515797Srrh 			 */
525797Srrh 			tokbuf[bufno].toks[tokbuf[bufno].tok_count++] = PARSEEOF;
53*13515Srrh 			if (writeTEST((char *)&tokbuf[bufno], sizeof *emptybuf, 1, tokfile))
545797Srrh 				goto badwrite;
555797Srrh 		}
565797Srrh 	}	/*end of being pass 1*/
575797Srrh }
585797Srrh 
595797Srrh inttoktype yylex()
605797Srrh {
615797Srrh 	register	ptrall	bufptr;
625797Srrh 	register	inttoktype		val;
635797Srrh 	register	struct	exp	*locxp;
645797Srrh 	/*
655797Srrh 	 *	No local variables to be allocated; this saves
665797Srrh 	 *	one piddling instruction..
675797Srrh 	 */
685797Srrh 	static	int	Lastjxxx;
695797Srrh 
705797Srrh 	bufptr = tokptr;		/*copy in the global value*/
715797Srrh    top:
725797Srrh 	if (bufptr < tokub){
735797Srrh 		gtoken(val, bufptr);
745797Srrh 		switch(yylval = val){
755797Srrh 		case	PARSEEOF:
765797Srrh 				yylval = val = PARSEEOF;
775797Srrh 				break;
785797Srrh 		case	BFINT:
795797Srrh 		case	INT:
805797Srrh 				if (xp >= &explist[NEXP])
815797Srrh 				     yyerror("Too many expressions; try simplyfing");
825797Srrh 				else
835797Srrh 				    locxp = xp++;
845797Srrh 				locxp->e_number = Znumber;
855797Srrh 				locxp->e_number.num_tag = TYPL;
865797Srrh 				glong(locxp->e_xvalue, bufptr);
875797Srrh 			  makevalue:
885797Srrh 				locxp->e_xtype = XABS;
895797Srrh 				locxp->e_xloc = 0;
905797Srrh 				locxp->e_xname = NULL;
915797Srrh 				yylval = (int)locxp;
925797Srrh 				break;
935797Srrh 		case	BIGNUM:
945797Srrh 				if (xp >= &explist[NEXP])
955797Srrh 				     yyerror("Too many expressions; try simplyfing");
965797Srrh 				else
975797Srrh 				    locxp = xp++;
985797Srrh 				gnumber(locxp->e_number, bufptr);
995797Srrh 				goto makevalue;
1005797Srrh 		case	NAME:
1015797Srrh 				gptr(yylval, bufptr);
1025797Srrh 				lastnam = (struct symtab *)yylval;
1035797Srrh 				break;
1045797Srrh 		case	SIZESPEC:
1055797Srrh 		case 	REG:
1065797Srrh 				gchar(yylval, bufptr);
1075797Srrh 				break;
1085797Srrh 		case	INSTn:
1095797Srrh 		case	INST0:
1105797Srrh 				gopcode(yyopcode, bufptr);
1115797Srrh 				break;
1125797Srrh 		case	IJXXX:
1135797Srrh 				gopcode(yyopcode, bufptr);
1145797Srrh 				/* We can't cast Lastjxxx into (int *) here.. */
1155797Srrh 				gptr(Lastjxxx, bufptr);
1165797Srrh 				lastjxxx = (struct symtab *)Lastjxxx;
1175797Srrh 				break;
1185797Srrh 		case	ILINESKIP:
1195797Srrh 				gint(yylval, bufptr);
1205797Srrh 				lineno += yylval;
1215797Srrh 				goto top;
1225797Srrh 		case	SKIP:
1235797Srrh 				eatskiplg(bufptr);
1245797Srrh 				goto top;
1255797Srrh 		case	VOID:
1265797Srrh 				goto top;
1275797Srrh 		case 	STRING:
1285797Srrh 		case 	ISTAB:
1295797Srrh 		case	ISTABSTR:
1305797Srrh 		case	ISTABNONE:
1315797Srrh 		case	ISTABDOT:
1325797Srrh 		case	IALIGN:
1335797Srrh 				gptr(yylval, bufptr);
1345797Srrh 				break;
1355797Srrh 		}
1365797Srrh #ifdef DEBUG
1375797Srrh 		if (toktrace){
1385797Srrh 		char	*tok_to_name();
1395797Srrh 		printf("P: %d T#: %4d, %s ",
1405797Srrh 			passno, bufptr -  firsttoken, tok_to_name(val));
1415797Srrh 		switch(val){
1425797Srrh 		case 	INT:	printf("val %d",
1435797Srrh 					((struct exp *)yylval)->e_xvalue);
1445797Srrh 				break;
1455797Srrh 		case	BFINT:	printf("val %d",
1465797Srrh 					((struct exp *)yylval)->e_xvalue);
1475797Srrh 				break;
1485797Srrh 		case 	BIGNUM: bignumprint(((struct exp*)yylval)->e_number);
1495797Srrh 				break;
1505797Srrh 		case	NAME:	printf("\"%.8s\"",
151*13515Srrh 					FETCHNAME((struct symtab *)yylval));
1525797Srrh 				break;
1535797Srrh 		case	REG:	printf(" r%d",
1545797Srrh 					yylval);
1555797Srrh 				break;
1565797Srrh 		case	IJXXX:
1575797Srrh 		case	INST0:
1585797Srrh 		case	INSTn:	if (ITABCHECK(yyopcode))
159*13515Srrh 					printf("%.8s",
160*13515Srrh 						FETCHNAME(ITABFETCH(yyopcode)));
1615797Srrh 				else
1625797Srrh 					printf("IJXXX or INST0 or INSTn can't get into the itab\n");
1635797Srrh 				break;
16413448Srrh 		case	STRING:	printf("length %d ", ((u_short *)yylval)[-1]);
16513448Srrh 				printf("value\"%*s\"",
16613448Srrh 					((u_short *)yylval)[-1],
16713448Srrh 					(char *)yylval);
1685797Srrh 				break;
1695797Srrh 		}  		/*end of the debug switch*/
1705797Srrh 		printf("\n");
1715797Srrh 		}
1725797Srrh #endif DEBUG
1735797Srrh 
1745797Srrh 	} else {	/* start a new buffer */
1755797Srrh 	    if (useVM){
1765797Srrh 		if (passno == 2){
1775797Srrh 			tok_temp = emptybuf->tok_next;
1785797Srrh 			emptybuf->tok_next = tok_free;
1795797Srrh 			tok_free = emptybuf;
1805797Srrh 			emptybuf = tok_temp;
1815797Srrh 		} else {
1825797Srrh 			emptybuf = emptybuf->tok_next;
1835797Srrh 		}
1845797Srrh 		bufno += 1;
1855797Srrh 		if (emptybuf == 0){
1865797Srrh 			struct	tokbufdesc *newdallop;
1875797Srrh 			int	i;
1885797Srrh 			if (passno == 2)
1895797Srrh 				goto badread;
1905797Srrh 			emptybuf = newdallop = (struct tokbufdesc *)
1915797Srrh 			  Calloc(TOKDALLOP, sizeof (struct tokbufdesc));
1925797Srrh 			for (i=0; i < TOKDALLOP; i++){
1935797Srrh 				buftail->tok_next = newdallop;
1945797Srrh 				buftail = newdallop;
1955797Srrh 				newdallop += 1;
1965797Srrh 			}
1975797Srrh 			buftail->tok_next = 0;
1985797Srrh 		}	/*end of need to get more buffers*/
1995797Srrh 		(bytetoktype *)bufptr = &(emptybuf->toks[0]);
2005797Srrh 		if (passno == 1)
2015797Srrh 			scan_dot_s(emptybuf);
2025797Srrh 	    } else {	/*don't use VM*/
2035797Srrh 		bufno ^= 1;
2045797Srrh 		emptybuf = &tokbuf[bufno];
2055797Srrh 		((bytetoktype *)bufptr) = &(emptybuf->toks[0]);
2065797Srrh 		if (passno == 1){
2075797Srrh 			/*
2085797Srrh 			 *	First check if there are things to write
2095797Srrh 			 *	out at all
2105797Srrh 			 */
2115797Srrh 			if (emptybuf->tok_count >= 0){
212*13515Srrh 			    if (writeTEST((char *)emptybuf, sizeof *emptybuf, 1, tokfile)){
2135797Srrh 				yyerror("Unexpected end of file writing the interpass tmp file");
2145797Srrh 				exit(2);
2155797Srrh 			    }
2165797Srrh 			}
2175797Srrh 			scan_dot_s(emptybuf);
2185797Srrh 		} else {	/*pass 2*/
219*13515Srrh 		    if (readTEST((char *)emptybuf, sizeof *emptybuf, 1, tokfile)){
2205797Srrh 			 badread:
2215797Srrh 			     yyerror("Unexpected end of file while reading the interpass tmp file");
2225797Srrh 			     exit(1);
2235797Srrh 		    }
2245797Srrh 		}
2255797Srrh 	    }	/*end of using a real live file*/
2265797Srrh 	    (char *)tokub = (char *)bufptr + emptybuf->tok_count;
2275797Srrh #ifdef DEBUG
2285797Srrh 	    firsttoken = bufptr;
2295797Srrh 	    if (debug)
2305797Srrh 		printf("created buffernumber %d with %d tokens\n",
2315797Srrh 			bufno, emptybuf->tok_count);
2325797Srrh #endif DEBUG
2335797Srrh 	    goto top;
2345797Srrh 	}	/*end of reading/creating a new buffer*/
2355797Srrh 	tokptr = bufptr;		/*copy back the global value*/
2365797Srrh 	return(val);
2375797Srrh }	/*end of yylex*/
2385797Srrh 
2395797Srrh 
2405797Srrh buildskip(from, to)
2415797Srrh 	register	ptrall	from, to;
2425797Srrh {
2435797Srrh 	int	diff;
2445797Srrh 	register	struct	tokbufdesc *middlebuf;
2455797Srrh 	/*
2465797Srrh 	 *	check if from and to are in the same buffer
2475797Srrh 	 *	from and to DIFFER BY AT MOST 1 buffer and to is
2485797Srrh 	 *	always ahead of from, with to being in the buffer emptybuf
2495797Srrh 	 *	points to.
2505797Srrh 	 *	The hard part here is accounting for the case where the
2515797Srrh 	 *	skip is to cross a buffer boundary; we must construct
2525797Srrh 	 *	two skips.
2535797Srrh 	 *
2545797Srrh 	 *	Figure out where the buffer boundary between from and to is
2555797Srrh 	 *	It's easy in VM, as buffers increase to high memory, but
2565797Srrh 	 *	w/o VM, we alternate between two buffers, and want
2575797Srrh 	 *	to look at the exact middle of the contiguous buffer region.
2585797Srrh 	 */
2595797Srrh 	middlebuf = useVM ? emptybuf : &tokbuf[1];
2605797Srrh 	if (  ( (bytetoktype *)from > (bytetoktype *)middlebuf)
2615797Srrh 	    ^ ( (bytetoktype *)to > (bytetoktype *)middlebuf)
2625797Srrh 	   ){	/*split across a buffer boundary*/
2635797Srrh 		ptoken(from, SKIP);
2645797Srrh 		/*
2655797Srrh 		 *	Set the skip so it lands someplace beyond
2665797Srrh 		 *	the end of this buffer.
2675797Srrh 		 *	When we pull this skip out in the second pass,
2685797Srrh 		 *	we will temporarily move the current pointer
2695797Srrh 		 *	out beyond the end of the buffer, but immediately
2705797Srrh 		 *	do a compare and fail the compare, and then reset
2715797Srrh 		 *	all the pointers correctly to point into the next buffer.
2725797Srrh 		 */
2735797Srrh 		bskiplg(from,  TOKBUFLG + 1);
2745797Srrh 		/*
2755797Srrh 		 *	Now, force from to be in the same buffer as to
2765797Srrh 		 */
2775797Srrh 		(bytetoktype *)from = (bytetoktype *)&(emptybuf->toks[0]);
2785797Srrh 	}
2795797Srrh 	/*
2805797Srrh 	 *	Now, to and from are in the same buffer
2815797Srrh 	 */
2825797Srrh 	if (from > to)
2835797Srrh 		yyerror("Internal error: bad skip construction");
2845797Srrh 	else {
2855797Srrh 		if ( (diff = (bytetoktype *)to - (bytetoktype *)from) >=
2865797Srrh 			(sizeof(bytetoktype) + sizeof(lgtype) + 1)) {
2875797Srrh 				ptoken(from, SKIP);
2885797Srrh 				bskipfromto(from, to);
2895797Srrh 		} else {
2905797Srrh 			for ( ; diff > 0; --diff)
2915797Srrh 				ptoken(from, VOID);
2925797Srrh 		}
2935797Srrh 	}
2945797Srrh }
2955797Srrh 
2965797Srrh movestr(to, from, lg)
29713448Srrh 	char	*to;	/* 4(ap) */
29813448Srrh 	char	*from;	/* 8(ap) */
29913448Srrh 	int	lg;	/* 12(ap) */
3005797Srrh {
30113448Srrh 	if (lg <= 0)
30213448Srrh 		return;
30313448Srrh 	;
30413448Srrh 	asm("movc3	12(ap),*8(ap),*4(ap)");
30513448Srrh 	;
3065797Srrh }
3075797Srrh 
3085797Srrh new_dot_s(namep)
3095797Srrh 	char	*namep;
3105797Srrh {
3115797Srrh 	newfflag = 1;
3125797Srrh 	newfname = namep;
3135797Srrh 	dotsname = namep;
3145797Srrh 	lineno = 1;
3155797Srrh 	scanlineno = 1;
3165797Srrh }
31713448Srrh 
31813448Srrh min(a, b)
31913448Srrh {
32013448Srrh 	return(a < b ? a : b);
32113448Srrh }
322