xref: /csrg-svn/old/as.vax/asscan1.c (revision 13448)
15797Srrh /*
25797Srrh  *	Copyright (c) 1982 Regents of the University of California
35797Srrh  */
45797Srrh #ifndef lint
5*13448Srrh static char sccsid[] = "@(#)asscan1.c 4.4 06/29/83";
65797Srrh #endif not lint
75797Srrh 
85797Srrh #include "asscanl.h"
95797Srrh 
105797Srrh inittmpfile()
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 
305797Srrh closetmpfile()
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){
415797Srrh 				if (writeTEST((char *)&tokbuf[bufno ^ 1], sizeof *emptybuf, 1, tmpfil)){
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;
535797Srrh 			if (writeTEST((char *)&tokbuf[bufno], sizeof *emptybuf, 1, tmpfil))
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\"",
1515797Srrh 					((struct symtab *)yylval)->s_name);
1525797Srrh 				break;
1535797Srrh 		case	REG:	printf(" r%d",
1545797Srrh 					yylval);
1555797Srrh 				break;
1565797Srrh 		case	IJXXX:
1575797Srrh 		case	INST0:
1585797Srrh 		case	INSTn:	if (ITABCHECK(yyopcode))
1595797Srrh 					printf("%.8s", ITABFETCH(yyopcode)->s_name);
1605797Srrh 				else
1615797Srrh 					printf("IJXXX or INST0 or INSTn can't get into the itab\n");
1625797Srrh 				break;
163*13448Srrh 		case	STRING:	printf("length %d ", ((u_short *)yylval)[-1]);
164*13448Srrh 				printf("value\"%*s\"",
165*13448Srrh 					((u_short *)yylval)[-1],
166*13448Srrh 					(char *)yylval);
1675797Srrh 				break;
1685797Srrh 		}  		/*end of the debug switch*/
1695797Srrh 		printf("\n");
1705797Srrh 		}
1715797Srrh #endif DEBUG
1725797Srrh 
1735797Srrh 	} else {	/* start a new buffer */
1745797Srrh 	    if (useVM){
1755797Srrh 		if (passno == 2){
1765797Srrh 			tok_temp = emptybuf->tok_next;
1775797Srrh 			emptybuf->tok_next = tok_free;
1785797Srrh 			tok_free = emptybuf;
1795797Srrh 			emptybuf = tok_temp;
1805797Srrh 		} else {
1815797Srrh 			emptybuf = emptybuf->tok_next;
1825797Srrh 		}
1835797Srrh 		bufno += 1;
1845797Srrh 		if (emptybuf == 0){
1855797Srrh 			struct	tokbufdesc *newdallop;
1865797Srrh 			int	i;
1875797Srrh 			if (passno == 2)
1885797Srrh 				goto badread;
1895797Srrh 			emptybuf = newdallop = (struct tokbufdesc *)
1905797Srrh 			  Calloc(TOKDALLOP, sizeof (struct tokbufdesc));
1915797Srrh 			for (i=0; i < TOKDALLOP; i++){
1925797Srrh 				buftail->tok_next = newdallop;
1935797Srrh 				buftail = newdallop;
1945797Srrh 				newdallop += 1;
1955797Srrh 			}
1965797Srrh 			buftail->tok_next = 0;
1975797Srrh 		}	/*end of need to get more buffers*/
1985797Srrh 		(bytetoktype *)bufptr = &(emptybuf->toks[0]);
1995797Srrh 		if (passno == 1)
2005797Srrh 			scan_dot_s(emptybuf);
2015797Srrh 	    } else {	/*don't use VM*/
2025797Srrh 		bufno ^= 1;
2035797Srrh 		emptybuf = &tokbuf[bufno];
2045797Srrh 		((bytetoktype *)bufptr) = &(emptybuf->toks[0]);
2055797Srrh 		if (passno == 1){
2065797Srrh 			/*
2075797Srrh 			 *	First check if there are things to write
2085797Srrh 			 *	out at all
2095797Srrh 			 */
2105797Srrh 			if (emptybuf->tok_count >= 0){
2115797Srrh 			    if (writeTEST((char *)emptybuf, sizeof *emptybuf, 1, tmpfil)){
2125797Srrh 				yyerror("Unexpected end of file writing the interpass tmp file");
2135797Srrh 				exit(2);
2145797Srrh 			    }
2155797Srrh 			}
2165797Srrh 			scan_dot_s(emptybuf);
2175797Srrh 		} else {	/*pass 2*/
2185797Srrh 		    if (readTEST((char *)emptybuf, sizeof *emptybuf, 1, tmpfil)){
2195797Srrh 			 badread:
2205797Srrh 			     yyerror("Unexpected end of file while reading the interpass tmp file");
2215797Srrh 			     exit(1);
2225797Srrh 		    }
2235797Srrh 		}
2245797Srrh 	    }	/*end of using a real live file*/
2255797Srrh 	    (char *)tokub = (char *)bufptr + emptybuf->tok_count;
2265797Srrh #ifdef DEBUG
2275797Srrh 	    firsttoken = bufptr;
2285797Srrh 	    if (debug)
2295797Srrh 		printf("created buffernumber %d with %d tokens\n",
2305797Srrh 			bufno, emptybuf->tok_count);
2315797Srrh #endif DEBUG
2325797Srrh 	    goto top;
2335797Srrh 	}	/*end of reading/creating a new buffer*/
2345797Srrh 	tokptr = bufptr;		/*copy back the global value*/
2355797Srrh 	return(val);
2365797Srrh }	/*end of yylex*/
2375797Srrh 
2385797Srrh 
2395797Srrh buildskip(from, to)
2405797Srrh 	register	ptrall	from, to;
2415797Srrh {
2425797Srrh 	int	diff;
2435797Srrh 	register	struct	tokbufdesc *middlebuf;
2445797Srrh 	/*
2455797Srrh 	 *	check if from and to are in the same buffer
2465797Srrh 	 *	from and to DIFFER BY AT MOST 1 buffer and to is
2475797Srrh 	 *	always ahead of from, with to being in the buffer emptybuf
2485797Srrh 	 *	points to.
2495797Srrh 	 *	The hard part here is accounting for the case where the
2505797Srrh 	 *	skip is to cross a buffer boundary; we must construct
2515797Srrh 	 *	two skips.
2525797Srrh 	 *
2535797Srrh 	 *	Figure out where the buffer boundary between from and to is
2545797Srrh 	 *	It's easy in VM, as buffers increase to high memory, but
2555797Srrh 	 *	w/o VM, we alternate between two buffers, and want
2565797Srrh 	 *	to look at the exact middle of the contiguous buffer region.
2575797Srrh 	 */
2585797Srrh 	middlebuf = useVM ? emptybuf : &tokbuf[1];
2595797Srrh 	if (  ( (bytetoktype *)from > (bytetoktype *)middlebuf)
2605797Srrh 	    ^ ( (bytetoktype *)to > (bytetoktype *)middlebuf)
2615797Srrh 	   ){	/*split across a buffer boundary*/
2625797Srrh 		ptoken(from, SKIP);
2635797Srrh 		/*
2645797Srrh 		 *	Set the skip so it lands someplace beyond
2655797Srrh 		 *	the end of this buffer.
2665797Srrh 		 *	When we pull this skip out in the second pass,
2675797Srrh 		 *	we will temporarily move the current pointer
2685797Srrh 		 *	out beyond the end of the buffer, but immediately
2695797Srrh 		 *	do a compare and fail the compare, and then reset
2705797Srrh 		 *	all the pointers correctly to point into the next buffer.
2715797Srrh 		 */
2725797Srrh 		bskiplg(from,  TOKBUFLG + 1);
2735797Srrh 		/*
2745797Srrh 		 *	Now, force from to be in the same buffer as to
2755797Srrh 		 */
2765797Srrh 		(bytetoktype *)from = (bytetoktype *)&(emptybuf->toks[0]);
2775797Srrh 	}
2785797Srrh 	/*
2795797Srrh 	 *	Now, to and from are in the same buffer
2805797Srrh 	 */
2815797Srrh 	if (from > to)
2825797Srrh 		yyerror("Internal error: bad skip construction");
2835797Srrh 	else {
2845797Srrh 		if ( (diff = (bytetoktype *)to - (bytetoktype *)from) >=
2855797Srrh 			(sizeof(bytetoktype) + sizeof(lgtype) + 1)) {
2865797Srrh 				ptoken(from, SKIP);
2875797Srrh 				bskipfromto(from, to);
2885797Srrh 		} else {
2895797Srrh 			for ( ; diff > 0; --diff)
2905797Srrh 				ptoken(from, VOID);
2915797Srrh 		}
2925797Srrh 	}
2935797Srrh }
2945797Srrh 
2955797Srrh movestr(to, from, lg)
296*13448Srrh 	char	*to;	/* 4(ap) */
297*13448Srrh 	char	*from;	/* 8(ap) */
298*13448Srrh 	int	lg;	/* 12(ap) */
2995797Srrh {
300*13448Srrh 	if (lg <= 0)
301*13448Srrh 		return;
302*13448Srrh 	;
303*13448Srrh 	asm("movc3	12(ap),*8(ap),*4(ap)");
304*13448Srrh 	;
3055797Srrh }
3065797Srrh 
3075797Srrh new_dot_s(namep)
3085797Srrh 	char	*namep;
3095797Srrh {
3105797Srrh 	newfflag = 1;
3115797Srrh 	newfname = namep;
3125797Srrh 	dotsname = namep;
3135797Srrh 	lineno = 1;
3145797Srrh 	scanlineno = 1;
3155797Srrh }
316*13448Srrh 
317*13448Srrh min(a, b)
318*13448Srrh {
319*13448Srrh 	return(a < b ? a : b);
320*13448Srrh }
321