xref: /csrg-svn/old/pcc/c2.vax/c21.c (revision 55276)
118381Sralph #ifndef lint
2*55276Ssklower static	char sccsid[] = "@(#)c21.c 4.25 07/15/92";
318381Sralph #endif
41498Sbill /* char C21[] = {"@(#)c21.c 1.83 80/10/16 21:18:22 JFR"}; /* sccs ident */
51496Sbill 
61496Sbill /*
71496Sbill  * C object code improver-- second part
81496Sbill  */
91496Sbill 
101496Sbill #include "c2.h"
111496Sbill #include <stdio.h>
121496Sbill #include <ctype.h>
131496Sbill 
141496Sbill #define NUSE 6
151496Sbill int ioflag;
161496Sbill int biti[NUSE] = {1,2,4,8,16,32};
175794Sroot int bitsize[] = {	/* index by type codes */
186958Srrh 	0,		/* 0	not allocated */
196958Srrh 	8,		/* 1	BYTE */
206958Srrh 	16,		/* 2	WORD */
216958Srrh 	32,		/* 3	LONG */
2232986Sdonn 	32,		/* 4	FFLOAT */
236958Srrh 	64,		/* 5	DFLOAT */
246958Srrh 	64,		/* 6	QUAD */
256958Srrh 	0,		/* 7	OP2 */
266958Srrh 	0,		/* 8	OP3 */
276958Srrh 	0,		/* 9	OPB */
286958Srrh 	0,		/* 10	OPX */
296958Srrh 	64,		/* 11	GFLOAT */
306958Srrh 	128,		/* 12	HFLOAT */
316958Srrh 	128		/* 13	OCTA */
325794Sroot };
331496Sbill int pos,siz; long f; /* for bit field communication */
341496Sbill struct node *uses[NUSE]; /* for backwards flow analysis */
351496Sbill char *lastrand; /* last operand of instruction */
361496Sbill struct node *bflow();
371496Sbill struct node *bicopt();
381496Sbill char *findcon();
3918381Sralph char *strcpy();
401496Sbill 
redun3(p,split)411496Sbill redun3(p,split) register struct node *p; int split; {
421496Sbill /* check for 3 addr instr which should be 2 addr */
431496Sbill 	if (OP3==((p->subop>>4)&0xF)) {
441496Sbill 		if (split) splitrand(p);
451496Sbill 		if (equstr(regs[RT1],regs[RT3])
461496Sbill 		  && (p->op==ADD || p->op==MUL || p->op==BIS || p->op==XOR)) {
471496Sbill 			register char *t=regs[RT1]; regs[RT1]=regs[RT2]; regs[RT2]=t;
481496Sbill 		}
491496Sbill 		if (equstr(regs[RT2],regs[RT3])) {
501496Sbill 			p->subop=(p->subop&0xF)|(OP2<<4); p->pop=0;
511496Sbill 			lastrand=regs[RT2]; *regs[RT3]=0; return(1);
521496Sbill 		}
531496Sbill 	} return(0);
541496Sbill }
551496Sbill 
bmove()561496Sbill bmove() {
571496Sbill 	register struct node *p, *lastp; register char *cp1,*cp2; register int r;
581496Sbill 	refcount();
591496Sbill 	for (p=lastp= &first; 0!=(p=p->forw); lastp=p);
601496Sbill 	clearreg(); clearuse();
611496Sbill 	for (p=lastp; p!= &first; p=p->back) {
621496Sbill 	if (debug) {
631496Sbill 		printf("Uses:\n");
641496Sbill 		for (r=NUSE;--r>=0;) if (uses[r])
651496Sbill 			printf("%d: %s\n",r,uses[r]->code? uses[r]->code:"");
661496Sbill 		printf("-\n");
671496Sbill 	}
681496Sbill 	r=(p->subop>>4)&0xF;
691496Sbill 	if (OP2==r && (cp1=p->code, *cp1++)=='$' && *cp1++=='0' && *cp1++==',' &&
701496Sbill 			!source(cp1)) {/* a no-op unless MUL or DIV */
711496Sbill 		if (p->op==MUL) {p->op=MOV; p->subop&=0xF; p->pop=0;}
721496Sbill 		else if (p->op==DIV) fprintf(stderr,"c2: zero divide\n");
731496Sbill 		else {delnode(p); redunm++; continue;}
741496Sbill 	}
751496Sbill 	if (OP3==r && 0!=redun3(p,1)) {newcode(p); redunm++;}
761496Sbill 	switch (p->op) {
771496Sbill 	case LABEL: case DLABEL:
781496Sbill 		for (r=NUSE; --r>=0;)
791496Sbill 			if (uses[r]) p->ref=(struct node *) (((int)p->ref)|biti[r]);
801496Sbill 		break;
811496Sbill 	case CALLS:
821496Sbill 		clearuse(); goto std;
831496Sbill 	case 0:
8417720Sralph 		/*
8517720Sralph 		 * Be conservative -- if we don't know what it is, then we
8617720Sralph 		 *	assume that it can set anything.
8717720Sralph 		 */
8817720Sralph 		for ( r = 0; r < NUSE; ++r )
8917720Sralph 			uses[r] = p;
9017720Sralph 		break;
911496Sbill 	case SUB:
921496Sbill 		if ((p->subop&0xF)!=LONG) goto std; cp1=p->code;
931496Sbill 		if (*cp1++!='$') goto std; splitrand(p);
941496Sbill 		if (equstr(regs[RT2],"fp") && !indexa(regs[RT1])) {/* address comp. */
953933Sroot 			char buf[C2_ASIZE]; cp2=buf; *cp2++='-';
961496Sbill 			cp1=regs[RT1]+1; while (*cp2++= *cp1++); --cp2;
971496Sbill 			cp1="(fp),"; while (*cp2++= *cp1++); --cp2;
981496Sbill 			cp1=regs[RT3]; while (*cp2++= *cp1++);
991496Sbill 			p->code=copy(buf); p->combop=T(MOVA,LONG); p->pop=0;
1001496Sbill 		} else if (*cp1++=='-' && 0<=(r=getnum(cp1))) {
1011496Sbill 			p->op=ADD; p->pop=0; *--cp1='$'; p->code=cp1;
1021496Sbill 		} goto std;
1031496Sbill 	case ADD:
1041496Sbill 		if ((p->subop&0xF)!=LONG) goto std; cp1=p->code;
1051496Sbill 		if (*cp1++!='$') goto std; splitrand(p);
1061496Sbill 		if (isstatic(cp1) && (r=isreg(regs[RT2]))>=0 && r<NUSE && uses[r]==p->forw)
1071496Sbill 		{
1081496Sbill 			/* address comp:
1091496Sbill 			**	addl2	$_foo,r0  \	movab	_foo[r0],bar
1101496Sbill 			**	movl	r0,bar	  /
1111496Sbill 			*/
1121496Sbill 			register struct	node	*pnext = p->forw;
1133933Sroot 			char	buf[C2_ASIZE];
1141496Sbill 
1151496Sbill 			if (pnext->op == MOV && pnext->subop == LONG)
1161496Sbill 			{
1171496Sbill 				cp1 = &regs[RT1][1]; cp2 = &buf[0];
1181496Sbill 				while (*cp2++ = *cp1++) ; cp2--;
1191496Sbill 				splitrand(pnext);
1201496Sbill 				if (r == isreg(regs[RT1]))
1211496Sbill 				{
1221496Sbill 					delnode(p); p = pnext;
1231496Sbill 					p->op = MOVA; p->subop = BYTE;
1241496Sbill 					p->pop = 0;
1251496Sbill 					cp1 = regs[RT1]; *cp2++ = '[';
1261496Sbill 					while (*cp2++ = *cp1++) ; cp2--;
1271496Sbill 					*cp2++ = ']'; *cp2++ = ',';
1281496Sbill 					cp1 = regs[RT2];
1291496Sbill 					while (*cp2++ = *cp1++) ;
1301496Sbill 					p->code = copy(buf);
1311496Sbill 				}
1321496Sbill 			}
1331496Sbill 		}
1341496Sbill 		else
1351496Sbill 		if (equstr(regs[RT2],"fp") && !indexa(regs[RT1])) {/* address comp. */
1361496Sbill 			cp2=cp1-1; cp1=regs[RT1]+1; while (*cp2++= *cp1++); --cp2;
1371496Sbill 			cp1="(fp)"; while (*cp2++= *cp1++); *--cp2=',';
1381496Sbill 			p->combop=T(MOVA,LONG); p->pop=0;
1391496Sbill 		} else if (*cp1++=='-' && 0<=(r=getnum(cp1))) {
1401496Sbill 			p->op=SUB; p->pop=0; *--cp1='$'; p->code=cp1;
1411496Sbill 		}
1421496Sbill 		/* fall thru ... */
1431496Sbill 	case CASE:
1441496Sbill 	default: std:
1451496Sbill 		p=bflow(p); break;
1461496Sbill 	case MUL:
1471496Sbill 	{
1481496Sbill 		/*
1491496Sbill 		** Change multiplication by constant powers of 2 to
1501496Sbill 		**	shifts.
1511496Sbill 		*/
1521496Sbill 		splitrand(p);
1531496Sbill 		if (regs[RT1][0] != '$' || regs[RT1][1] == '-') goto std;
1541496Sbill 		if ((r = ispow2(getnum(&regs[RT1][1]))) < 0) goto std;
1551496Sbill 		switch (r)
1561496Sbill 		{
1571496Sbill 		case 0:		/* mull3 $1,x,y */
1581496Sbill 			if (p->subop == U(LONG,OP3))
1591496Sbill 			{
1601496Sbill 				if (equstr(regs[RT2], regs[RT3]))
1611496Sbill 				{
1621496Sbill 					delnode(p); p = p->forw;
1631496Sbill 				}
1641496Sbill 				else
1651496Sbill 				{
1661496Sbill 					p->op = MOV; p->subop = LONG;
1671496Sbill 					p->pop = 0; newcode(p); nchange++;
1681496Sbill 				}
1691496Sbill 			}
1701496Sbill 			else
1711496Sbill 			if (p->subop == U(LONG,OP2))
1721496Sbill 			{
1731496Sbill 				delnode(p); p = p->forw;
1741496Sbill 			}
1751496Sbill 			goto std;
1761496Sbill 
1771496Sbill 		case 1:		/* mull2 $2,x */
1781496Sbill 			if (p->subop == U(LONG, OP2) && !source(regs[RT2]))
1791496Sbill 			{
1801496Sbill 				strcpy(regs[RT1], regs[RT2]);
1811496Sbill 				p->op = ADD; p->pop = 0; newcode(p); nchange++;
1821496Sbill 			}
1831496Sbill 			goto std;
1841496Sbill 		}
1851496Sbill 		if(p->subop==U(LONG,OP3)||(p->subop==U(LONG,OP2)&&!source(regs[RT2])))
1861496Sbill 		{
1871496Sbill 			if (p->subop == U(LONG,OP2))
1881496Sbill 				strcpy(regs[RT3], regs[RT2]);
1891496Sbill 			sprintf(regs[RT1], "$%d", r);
1901496Sbill 			p->op = ASH; p->subop = LONG;
1911496Sbill 			p->pop = 0; newcode(p); nchange++;
1921496Sbill 		}
1931496Sbill 		goto std;
1941496Sbill 	}
1951496Sbill 	case ASH:
1961496Sbill 	{
1971496Sbill 		/* address comp:
1981496Sbill 		**	ashl	$1,bar,r0  \	movl	bar,r0
1991496Sbill 		**	movab	_foo[r0]   /	movaw	_foo[r0]
2001496Sbill 		**
2011496Sbill 		**	ashl	$2,r0,r0   \	moval	_foo[r0]
2021496Sbill 		**	movab	_foo[r0]   /
2031496Sbill 		*/
2041496Sbill 		register struct	node	*pf;
2051496Sbill 		register int	shfrom, shto;
2061496Sbill 		long	shcnt;
2071496Sbill 		char	*regfrom;
2081496Sbill 
2091496Sbill 		splitrand(p);
2101496Sbill 		if (regs[RT1][0] != '$') goto std;
2111496Sbill 		if ((shcnt = getnum(&regs[RT1][1])) < 1 || shcnt > 3) goto std;
2121496Sbill 		if ((shfrom = isreg(regs[RT2])) >= 0)
21318381Sralph 			regfrom = copy(regs[RT2]);
2141496Sbill 		if ((shto = isreg(regs[RT3])) >= 0 && shto<NUSE)
2151496Sbill 		{
2161496Sbill 			int	regnum;
2171496Sbill 
2181496Sbill 			if (uses[shto] != (pf = p->forw)) goto ashadd;
2191496Sbill 			if (pf->op != MOVA && pf->op != PUSHA) goto ashadd;
2201496Sbill 			if (pf->subop != BYTE) goto ashadd;
2211496Sbill 			splitrand(pf);
2221496Sbill 			if (!indexa(regs[RT1])) goto std;
2231496Sbill 			cp2 = regs[RT1];
2241496Sbill 			if(!isstatic(cp2)) goto std;
2251496Sbill 			while (*cp2++ != '[') ;
2261496Sbill 			if (*cp2++ != 'r' || !isdigit(*cp2)) goto std;
2271496Sbill 			regnum = *cp2++ - '0';
2281496Sbill 			if (isdigit(*cp2))
2291496Sbill 			{
2301496Sbill 				if (cp2[1] != ']') goto std;
2311496Sbill 				regnum *= 10; regnum += *cp2 - '0';
2321496Sbill 			}
2331496Sbill 			if (regnum != shto) goto std;
2341496Sbill 			if (shfrom >= 0)	/* ashl $N,r*,r0 */
2351496Sbill 			{
2361496Sbill 				delnode(p);
23734255Sdonn 				p = pf;
2381496Sbill 				if (shfrom != shto)
2391496Sbill 				{
24034255Sdonn 					uses[shto] = NULL; splitrand(p);
2411496Sbill 					cp2=regs[RT1]; while (*cp2++!='[');
2421496Sbill 					cp1=regfrom; while (*cp2++= *cp1++);
24318381Sralph 					*--cp2 = ']';
24418381Sralph 					*++cp2 = '\0';
24534255Sdonn 					newcode(p);
2461496Sbill 				}
24734255Sdonn 				if (p->op == MOVA)
24834255Sdonn 				{
24934255Sdonn 					int movato;
25034255Sdonn 
25134255Sdonn 					splitrand(p);
25234255Sdonn 					if ((movato = isreg(regs[RT2])) >= 0
25334255Sdonn 					    && movato < NUSE)
25434255Sdonn 						/*
25534255Sdonn 						 * this register is dead;
25634255Sdonn 						 * resurrect it temporarily
25734255Sdonn 						 */
25834255Sdonn 						uses[movato] = p;
25934255Sdonn 				}
2601496Sbill 			}
2611496Sbill 			else
2621496Sbill 			{
2631496Sbill 				p->op = MOV; splitrand(p);
2641496Sbill 				strcpy(regs[RT1], regs[RT2]);
2651496Sbill 				strcpy(regs[RT2], regs[RT3]);
2661496Sbill 				regs[RT3][0] = '\0';
2671496Sbill 				p->pop = 0; newcode(p);
2681496Sbill 			}
2691496Sbill 			switch (shcnt)
2701496Sbill 			{
2711496Sbill 			case 1:	pf->subop = WORD; break;
2721496Sbill 			case 2:	pf->subop = LONG; break;
2731496Sbill 			case 3:	pf->subop = QUAD; break;
2741496Sbill 			}
27534255Sdonn 			pf->pop = 0;
2761496Sbill 			redunm++; nsaddr++; nchange++;
27725756Sdonn 			goto std;
2781496Sbill 		}
2791496Sbill ashadd:
2801496Sbill 		/* at this point, RT2 and RT3 are guaranteed to be simple regs*/
2811496Sbill 		if (shcnt == 1 && equstr(regs[RT2], regs[RT3]))
2821496Sbill 		{
2831496Sbill 			/*
2841496Sbill 			** quickie:
2851496Sbill 			**	ashl	$1,A,A	>	addl2	A,A
2861496Sbill 			*/
2871496Sbill 			p->op = ADD; p->subop = U(LONG,OP2); p->pop = 0;
2881496Sbill 			strcpy(regs[RT1], regs[RT2]); regs[RT3][0] = '\0';
2891496Sbill 			newcode(p); nchange++;
2901496Sbill 		}
2911496Sbill 		goto std;
2921496Sbill 	}
2931496Sbill 
2941496Sbill 	case EXTV:
2951496Sbill 	case EXTZV:
2961496Sbill 	{
2971496Sbill 		/* bit tests:
2981496Sbill 		**	extv	A,$1,B,rC  \
2991496Sbill 		**	tstl	rC	    >	jbc	A,B,D
3001496Sbill 		**	jeql	D	   /
3011496Sbill 		**
3021496Sbill 		** also byte- and word-size fields:
3031496Sbill 		**	extv	$n*8,$8,A,B	>	cvtbl	n+A,B
30416133Sralph 		**	extv	$n*16,$16,A,B	>	cvtwl	2n+A,B
3051496Sbill 		**	extzv	$n*8,$8,A,B	>	movzbl	n+A,B
30616133Sralph 		**	extzv	$n*16,$16,A,B	>	movzwl	2n+A,B
3071496Sbill 		*/
3081496Sbill 		register struct	node	*pf;	/* forward node */
3091496Sbill 		register struct	node	*pn;	/* next node (after pf) */
3101496Sbill 		int	flen;			/* field length */
3111496Sbill 
3121496Sbill 		splitrand(p);
3131496Sbill 		if (regs[RT2][0] != '$') goto std;
3141496Sbill 		if ((flen = getnum(&regs[RT2][1])) < 0) goto std;
3151496Sbill 		if (flen == 1)
3161496Sbill 		{
3171496Sbill 			register int	extreg;		/* reg extracted to */
3181496Sbill 
3191496Sbill 			extreg = isreg(regs[RT4]);
3201496Sbill 			if (extreg < 0 || extreg >= NUSE) goto std;
3211496Sbill 			if ((pf = p->forw)->op != TST) goto std;
3221496Sbill 			if (uses[extreg] && uses[extreg] != pf) goto std;
3231496Sbill 			splitrand(pf);
3241496Sbill 			if (extreg != isreg(regs[RT1])) goto std;
3251496Sbill 			if ((pn = pf->forw)->op != CBR) goto std;
3261496Sbill 			if (pn->subop != JEQ && pn->subop != JNE) goto std;
3271496Sbill 			delnode(p); delnode(pf);
3281496Sbill 			pn->subop = (pn->subop == JEQ) ? JBC : JBS;
3291496Sbill 			for(cp2=p->code; *cp2++!=',';);
3301496Sbill 			for(cp1=cp2;     *cp1++!=',';);
3311496Sbill 			while (*cp1!=',') *cp2++= *cp1++; *cp2='\0';
3321496Sbill 			pn->code = p->code; pn->pop = NULL;
3331496Sbill 			uses[extreg] = NULL;
33434255Sdonn 			p = pn;
33534255Sdonn 			break;
3361496Sbill 		}
3371496Sbill 		else
3381496Sbill 		if (flen == 8 || flen == 16)
3391496Sbill 		{
3401496Sbill 			register int	boff;	/* bit offset */
3411496Sbill 			register int	coff;	/* chunk (byte or word) offset*/
3421496Sbill 
3431496Sbill 			if (regs[RT1][0] != '$') goto std;
3441496Sbill 			if ((boff = getnum(&regs[RT1][1])) < 0) goto std;
3451496Sbill 			coff = boff / flen;
3461496Sbill 			if (coff && (isreg(regs[RT3]) >= 0)) goto std;
3471496Sbill 			if (boff < 0 || (boff % flen) != 0) goto std;
3481496Sbill 			p->op = (p->op == EXTV) ? CVT : MOVZ;
3491496Sbill 			p->subop = U((flen == 8 ? BYTE : WORD), LONG);
3501496Sbill 			if (coff == 0)
3511496Sbill 				strcpy(regs[RT1], regs[RT3]);
3521496Sbill 			else
35316133Sralph 				sprintf(regs[RT1], "%d%s%s",
35416133Sralph 					(flen == 8 ? coff : 2*coff),
35516133Sralph 					(regs[RT3][0] == '(' ? "" : "+"),
3561496Sbill 					regs[RT3]);
3571496Sbill 			strcpy(regs[RT2], regs[RT4]);
3581496Sbill 			regs[RT3][0] = '\0'; regs[RT4][0] = '\0';
3591496Sbill 			p->pop = 0; newcode(p);
3601496Sbill 		}
3611496Sbill 		nchange++;
3621496Sbill 		goto std;
3631496Sbill 	}
3641496Sbill 
3651496Sbill 	case CMP:
3661496Sbill 	{
3671496Sbill 		/* comparison to -63 to -1:
3681496Sbill 		**	cmpl	r0,$-1	>	incl	r0
3691496Sbill 		**	jeql	...
3701496Sbill 		**
3711496Sbill 		**	cmpl	r0,$-63	>	addl2	$63,r0
3721496Sbill 		**	jeql	...
3731496Sbill 		*/
3741496Sbill 		register int	num;
3751496Sbill 		register int	reg;
3761496Sbill 		register struct	node	*regp = p->back;
3771496Sbill 
3781496Sbill 		if (p->forw->op != CBR) goto std;
3791496Sbill 		if (p->forw->subop != JEQ && p->forw->subop != JNE) goto std;
3801496Sbill 		splitrand(p);
3811496Sbill 		if (strncmp(regs[RT2], "$-", 2) != 0) goto std;
3821496Sbill 		reg = r = isreg(regs[RT1]);
3831496Sbill 		if (r < 0) goto std;
3841496Sbill 		if (r < NUSE && uses[r] != 0) goto std;
3851496Sbill 		if (r >= NUSE && regp->op == MOV && p->subop == regp->subop)
3861496Sbill 		{
3871496Sbill 			if (*regp->code != 'r') goto std;
3881496Sbill 			reg = regp->code[1] - '0';
3891496Sbill 			if (isdigit(regp->code[2]) || reg >= NUSE || uses[reg])
3901496Sbill 				goto std;
3911496Sbill 		}
3921496Sbill 		if (r >= NUSE) goto std;
3931496Sbill 		if (reg != r)
3941496Sbill 			sprintf(regs[RT1], "r%d", reg);
3951496Sbill 		if ((num = getnum(&regs[RT2][2])) <= 0 || num > 63) goto std;
3961496Sbill 		if (num == 1)
3971496Sbill 		{
3981496Sbill 			p->op = INC; regs[RT2][0] = '\0';
3991496Sbill 		}
4001496Sbill 		else
4011496Sbill 		{
4021496Sbill 			register char	*t;
4031496Sbill 
4041496Sbill 			t=regs[RT1];regs[RT1]=regs[RT2];regs[RT2]=t;
4051496Sbill 			p->op = ADD; p->subop = U(p->subop, OP2);
4061496Sbill 			for (t = &regs[RT1][2]; t[-1] = *t; t++) ;
4071496Sbill 		}
4081496Sbill 		p->pop = 0; newcode(p);
4091496Sbill 		nchange++;
4101496Sbill 		goto std;
4111496Sbill 	}
4121496Sbill 
4131496Sbill 	case JSB:
4141496Sbill 		if (equstr(p->code,"mcount")) {uses[0]=p; regs[0][0]= -1;}
4151496Sbill 		goto std;
4161496Sbill 	case JBR: case JMP:
4171496Sbill 		clearuse();
4181496Sbill 		if (p->subop==RET || p->subop==RSB) {uses[0]=p; regs[0][0]= -1; break;}
4191496Sbill 		if (p->ref==0) goto std;	/* jmp (r0) */
4201496Sbill 		/* fall through */
4211496Sbill 	case CBR:
4221496Sbill 		if (p->ref->ref!=0) for (r=NUSE;--r>=0;)
4231496Sbill 			if (biti[r] & (int)p->ref->ref) {uses[r]=p; regs[r][0]= -1;}
4241496Sbill 	case EROU: case JSW:
4251496Sbill 	case TEXT: case DATA: case BSS: case ALIGN: case WGEN: case END: ;
4261496Sbill 	}
4271496Sbill 	}
4281496Sbill 	for (p= &first; p!=0; p=p->forw)
4291496Sbill 		if (p->op==LABEL || p->op==DLABEL) p->ref=0;	/* erase our tracks */
4301496Sbill }
4311496Sbill 
rmove()4321496Sbill rmove()
4331496Sbill {
43418381Sralph 	register struct node *p;
4351496Sbill 	register int r;
4361496Sbill 	int r1;
4371496Sbill 
4381496Sbill 	clearreg();
4391496Sbill 	for (p=first.forw; p!=0; p = p->forw) {
4401496Sbill 	if (debug) {
44117210Sralph 		if (*conloc) {
44217210Sralph 			r1=conval[0];
44317210Sralph 			printf("Con %s = %d%d %s\n", conloc, r1&0xF, r1>>4, conval+1);
44417210Sralph 		}
4451496Sbill 		printf("Regs:\n");
4461496Sbill 		for (r=0; r<NREG; r++)
4471496Sbill 			if (regs[r][0]) {
4481496Sbill 				r1=regs[r][0];
4491496Sbill 				printf("%d: %d%d %s\n", r, r1&0xF, r1>>4, regs[r]+1);
4501496Sbill 			}
4511496Sbill 		printf("-\n");
4521496Sbill 	}
4531496Sbill 	switch (p->op) {
4541496Sbill 
4551496Sbill 	case CVT:
4561496Sbill 		splitrand(p); goto mov;
4571496Sbill 
4581496Sbill 	case MOV:
4591496Sbill 		splitrand(p);
4601496Sbill 		if ((r = findrand(regs[RT1],p->subop)) >= 0) {
4611496Sbill 			if (r == isreg(regs[RT2]) && p->forw->op!=CBR) {
4621496Sbill 				delnode(p); redunm++; break;
4631496Sbill 			}
4641496Sbill 		}
4651496Sbill mov:
4661496Sbill 		repladdr(p);
4671496Sbill 		r = isreg(regs[RT1]);
4681496Sbill 		r1 = isreg(regs[RT2]);
4691496Sbill 		dest(regs[RT2],p->subop);
4701506Sbill 		if (r>=0) {
4711506Sbill 			if (r1>=0) savereg(r1, regs[r]+1, p->subop);
4721506Sbill 			else if (p->op!=CVT) savereg(r, regs[RT2], p->subop);
4731506Sbill 		} else if (r1>=0) savereg(r1, regs[RT1], p->subop);
4741506Sbill 		else if (p->op!=CVT) setcon(regs[RT1], regs[RT2], p->subop);
4751496Sbill 		break;
4761496Sbill 
4771496Sbill /* .rx,.wx */
4781496Sbill 	case MFPR:
4791496Sbill 	case COM:
4801496Sbill 	case NEG:
4811496Sbill /* .rx,.wx or .rx,.rx,.wx */
4821496Sbill 	case ADD:
4831496Sbill 	case SUB:
4841496Sbill 	case BIC:
4851496Sbill 	case BIS:
4861496Sbill 	case XOR:
4871496Sbill 	case MUL:
4881496Sbill 	case DIV:
4891496Sbill 	case ASH:
4901496Sbill 	case MOVZ:
4911496Sbill /* .rx,.rx,.rx,.wx */
4921496Sbill 	case EXTV:
4931496Sbill 	case EXTZV:
4941496Sbill 	case INSV:
4951496Sbill 		splitrand(p);
4961496Sbill 		repladdr(p);
4971496Sbill 		dest(lastrand,p->subop);
4981496Sbill 		if (p->op==INSV) ccloc[0]=0;
4991496Sbill 		break;
5001496Sbill 
5011496Sbill /* .mx or .wx */
5021496Sbill 	case CLR:
5031496Sbill 	case INC:
5041496Sbill 	case DEC:
5051496Sbill 		splitrand(p);
5061496Sbill 		dest(lastrand,p->subop);
5071496Sbill 		if (p->op==CLR)
5081496Sbill 			if ((r = isreg(regs[RT1])) >= 0)
5091496Sbill 				savereg(r, "$0", p->subop);
5101496Sbill 			else
5111496Sbill 				setcon("$0", regs[RT1], p->subop);
5121496Sbill 		break;
5131496Sbill 
5141496Sbill /* .rx */
5151496Sbill 	case TST:
5161496Sbill 	case PUSH:
5171496Sbill 		splitrand(p);
5181496Sbill 		lastrand=regs[RT1+1]; /* fool repladdr into doing 1 operand */
5191496Sbill 		repladdr(p);
5201496Sbill 		if (p->op==TST && equstr(lastrand=regs[RT1], ccloc+1)
5211496Sbill 		  && ((0xf&(ccloc[0]>>4))==p->subop || equtype(ccloc[0],p->subop))
5221496Sbill 		  &&!source(lastrand)) {
5231496Sbill 			delnode(p); p = p->back; nrtst++; nchange++;
5241496Sbill 		}
5251496Sbill 		setcc(lastrand,p->subop);
5261496Sbill 		break;
5271496Sbill 
5281496Sbill /* .rx,.rx,.rx */
5291496Sbill 	case PROBER:
5301496Sbill 	case PROBEW:
5311496Sbill 	case CASE:
5321496Sbill 	case MOVC3:
5331496Sbill /* .rx,.rx */
5341496Sbill 	case MTPR:
5351496Sbill 	case CALLS:
5361496Sbill 	case CMP:
5371496Sbill 	case BIT:
5381496Sbill 		splitrand(p);
5391496Sbill 		/* fool repladdr into doing right number of operands */
5403957Sbugs 		if (p->op==CASE || p->op==PROBER || p->op==PROBEW) lastrand=regs[RT4];
5413958Sbugs /*		else if (p->op==CMPV || p->op==CMPZV) lastrand=regs[RT4+1]; */
5423957Sbugs 		else if (p->op==MOVC3) lastrand=regs[RT1];
5431496Sbill 		else lastrand=regs[RT3];
5441496Sbill 		repladdr(p);
5451496Sbill 		if (p->op==CALLS || p->op==MOVC3) clearreg();
546*55276Ssklower 		/*if (p->op==BIT) bitopt(p);*/
5471496Sbill 		ccloc[0]=0; break;
5481496Sbill 
5491496Sbill 	case CBR:
5501496Sbill 		if (p->subop>=JBC) {
5511496Sbill 			splitrand(p);
5521496Sbill 			if (p->subop<JBCC) lastrand=regs[RT3]; /* 2 operands can be optimized */
5531496Sbill 			else lastrand=regs[RT2]; /* .mb destinations lose */
5541496Sbill 			repladdr(p);
5551496Sbill 		}
5561496Sbill 		ccloc[0] = 0;
5571496Sbill 		break;
5581496Sbill 
5591496Sbill 	case JBR:
5601496Sbill 		redunbr(p);
5611496Sbill 
5621496Sbill /* .wx,.bb */
5631496Sbill 	case SOB:
5641496Sbill 
5651496Sbill 	default:
5661496Sbill 		clearreg();
5671496Sbill 	}
5681496Sbill 	}
5691496Sbill }
5701496Sbill 
5711496Sbill char *
byondrd(p)5721496Sbill byondrd(p) register struct node *p; {
5731496Sbill /* return pointer to register which is "beyond last read/modify operand" */
5741496Sbill 	if (OP2==(p->subop>>4)) return(regs[RT3]);
5751496Sbill 	switch (p->op) {
5761496Sbill 		case MFPR:
5771496Sbill 		case JSB:
5781496Sbill 		case PUSHA:
5791496Sbill 		case TST: case INC: case DEC: case PUSH:	return(regs[RT2]);
5801496Sbill 		case MTPR:
5811496Sbill 		case BIT: case CMP: case CALLS:				return(regs[RT3]);
5821496Sbill 		case PROBER: case PROBEW:
5831496Sbill 		case CASE: case MOVC3:						return(regs[RT4]);
5841496Sbill 	}
5851496Sbill 	return(lastrand);
5861496Sbill }
5871496Sbill 
5881496Sbill struct node *
bflow(p)5891496Sbill bflow(p)
5901496Sbill register struct node *p;
5911496Sbill {
5921496Sbill 	register char *cp1,*cp2,**preg; register int r;
5931496Sbill 	int flow= -1;
5941496Sbill 	struct node *olduse=0;
5951496Sbill 	splitrand(p);
5961496Sbill 	if (p->op!=PUSH && p->subop && 0<=(r=isreg(lastrand)) && r<NUSE && uses[r]==p->forw) {
59732986Sdonn 	if ((p->op==CVT || p->op==MOVZ)
59832986Sdonn 		&& (p->forw->op==CVT || p->forw->op==MOVZ)
59932986Sdonn 		&& p->forw->subop&0xf
60032987Sdonn 		&& equtype(p->subop,p->forw->subop)
60132986Sdonn 		&& !source(cp1=regs[RT1])
60232986Sdonn 		&& !indexa(cp1)) goto movit;
6031496Sbill 	if (equtype(p->subop,regs[r][0])
6041496Sbill 	|| ((p->op==CVT || p->op==MOVZ)
6051496Sbill 			 && 0xf&regs[r][0] && compat(0xf&(p->subop>>4),regs[r][0]))) {
6061496Sbill 		register int r2;
6071496Sbill 		if (regs[r][1]!=0) {/* send directly to destination */
6081496Sbill 			if (p->op==INC || p->op==DEC) {
6091496Sbill 				if (p->op==DEC) p->op=SUB; else p->op=ADD;
6101496Sbill 				p->subop=(OP2<<4)+(p->subop&0xF); /* use 2 now, convert to 3 later */
6111496Sbill 				p->pop=0;
6121496Sbill 				cp1=lastrand; cp2=regs[RT2]; while (*cp2++= *cp1++); /* copy reg */
6131496Sbill 				cp1=lastrand; *cp1++='$'; *cp1++='1'; *cp1=0;
6141496Sbill 			}
6151496Sbill 			cp1=regs[r]+1; cp2=lastrand;
6161496Sbill 			if (OP2==(p->subop>>4)) {/* use 3 operand form of instruction */
6171496Sbill 				p->pop=0;
6181496Sbill 				p->subop += (OP3-OP2)<<4; lastrand=cp2=regs[RT3];
6191496Sbill 			}
6201496Sbill 			while (*cp2++= *cp1++);
6211496Sbill 			if (p->op==MOVA && p->forw->op==PUSH) {
6221496Sbill 				p->op=PUSHA; *regs[RT2]=0; p->pop=0;
6231496Sbill 			} else if (p->op==MOV && p->forw->op==PUSH) {
6241496Sbill 				p->op=PUSH ; *regs[RT2]=0; p->pop=0;
6251496Sbill 			}
6261496Sbill 			delnode(p->forw);
6271496Sbill 			if (0<=(r2=isreg(lastrand)) && r2<NUSE) {
6281496Sbill 				uses[r2]=uses[r]; uses[r]=0;
6291496Sbill 			}
63018381Sralph 			(void) redun3(p,0);
6311496Sbill 			newcode(p); redunm++; flow=r;
6321496Sbill 		} else if (p->op==MOV && p->forw->op!=EXTV && p->forw->op!=EXTZV) {
6331496Sbill 			/* superfluous fetch */
6341496Sbill 			int nmatch;
6352829Stoy 			char src[C2_ASIZE];
6361496Sbill 	movit:
6371496Sbill 			cp2=src; cp1=regs[RT1]; while (*cp2++= *cp1++);
6381496Sbill 			splitrand(p->forw);
6391496Sbill 			if (p->forw->op != INC && p->forw->op != DEC)
6401496Sbill 				lastrand=byondrd(p->forw);
6411496Sbill 			nmatch=0;
6421496Sbill 			for (preg=regs+RT1;*preg!=lastrand;preg++)
6431496Sbill 				if (r==isreg(*preg)) {
6441496Sbill 				cp2= *preg; cp1=src; while (*cp2++= *cp1++); ++nmatch;
6451496Sbill 			}
6461496Sbill 			if (nmatch==1) {
6471496Sbill 				if (OP2==(p->forw->subop>>4) && equstr(src,regs[RT2])) {
6481496Sbill 					p->forw->pop=0;
6491496Sbill 					p->forw->subop += (OP3-OP2)<<4; cp1=regs[RT3];
6501496Sbill 					*cp1++='r'; *cp1++=r+'0'; *cp1=0;
6511496Sbill 				}
6521496Sbill 				delnode(p); p=p->forw;
6531496Sbill 				if (0<=(r2=isreg(src)) && r2<NUSE) {
6541496Sbill 					uses[r2]=uses[r]; uses[r]=0;
6551496Sbill 				}
65618381Sralph 				(void) redun3(p,0);
65733360Sdonn 				newcode(p); redunm++;
65833360Sdonn 				return(p);	/* avoid stale uses[] data */
6591496Sbill 			} else splitrand(p);
6601496Sbill 		}
6611496Sbill 	} else if (p->op==MOV && (p->forw->op==CVT || p->forw->op==MOVZ)
6621496Sbill 		&& p->forw->subop&0xf 	/* if base or index, then forget it */
6631496Sbill 		&& compat(p->subop,p->forw->subop) && !source(cp1=regs[RT1])
6641496Sbill 		&& !indexa(cp1)) goto movit;
6651496Sbill 	}
6661496Sbill 	/* adjust 'lastrand' past any 'read' or 'modify' operands. */
6671496Sbill 	lastrand=byondrd(p);
6681496Sbill 	/* a 'write' clobbers the register. */
6691496Sbill 	if (0<=(r=isreg(lastrand)) && r<NUSE
6701496Sbill 	|| OP2==(p->subop>>4) && 0<=(r=isreg(regs[RT2])) && r<NUSE && uses[r]==0) {
6711496Sbill 		/* writing a dead register is useless, but watch side effects */
6721496Sbill 		switch (p->op) {
6731498Sbill 		case ACB:
6741496Sbill 		case AOBLEQ: case AOBLSS: case SOBGTR: case SOBGEQ: break;
6751498Sbill 		default:
6761496Sbill 			if (uses[r]==0) {/* no direct uses, check for use of condition codes */
6771496Sbill 				register struct node *q=p;
6781496Sbill 				while ((q=nonlab(q->forw))->combop==JBR) q=q->ref;	/* cc unused, unchanged */
6791496Sbill 				if (q->op!=CBR) {/* ... and destroyed */
6801496Sbill 					preg=regs+RT1;
6811496Sbill 					while (cp1= *preg++) {
6821496Sbill 						if (cp1==lastrand) {redunm++; delnode(p); return(p->forw);}
6831496Sbill 						if (source(cp1) || equstr(cp1,lastrand)) break;
6841496Sbill 					}
6851496Sbill 				}
6861496Sbill 			}
6871496Sbill 			flow=r;
6881496Sbill 		}
6891496Sbill 	}
69018381Sralph 	if (0<=(r=flow)) {
69118381Sralph 		olduse=uses[r];
69218381Sralph 		uses[r]=0;
69318381Sralph 		regs[r][0]=regs[r][1]=0;
69418381Sralph 	}
6951496Sbill 		/* these two are here, rather than in bmove(),
6961496Sbill 		/* because I decided that it was better to go for 3-address code
6971496Sbill 		/* (save time) rather than fancy jbxx (save 1 byte)
6981496Sbill 		/* on sequences like  bisl2 $64,r0; movl r0,foo
6991496Sbill 		*/
7001496Sbill 	if (p->op==BIC) {p=bicopt(p); splitrand(p); lastrand=byondrd(p);}
70118381Sralph 	if (p->op==BIS) {(void) bixprep(p,JBSS);    lastrand=byondrd(p);}
7021496Sbill 	/* now look for 'read' or 'modify' (read & write) uses */
7031496Sbill 	preg=regs+RT1;
7041496Sbill 	while (*(cp1= *preg++)) {
7051496Sbill 		/* check for  r  */
7061496Sbill 		if (lastrand!=cp1 && 0<=(r=isreg(cp1)) && r<NUSE && uses[r]==0) {
7071496Sbill 			uses[r]=p; cp2=regs[r]; *cp2++=p->subop;
7081506Sbill 			if (p->op==ASH && preg==(regs+RT1+1)) cp2[-1]=BYTE; /* stupid DEC */
7091496Sbill 			if (p->op==MOV || p->op==PUSH || p->op==CVT || p->op==MOVZ || p->op==COM || p->op==NEG) {
7101496Sbill 				if (p->op==PUSH) cp1="-(sp)";
7111496Sbill 				else {
7121496Sbill 					cp1=regs[RT2];
7131496Sbill 					if (0<=(r=isreg(cp1)) && r<NUSE && uses[r]==0)
7141496Sbill 						uses[r]=olduse; /* reincarnation!! */
7151496Sbill 					/* as in  addl2 r0,r1;  movl r1,r0;  ret  */
7161496Sbill 					if (p->op!=MOV) cp1=0;
7171496Sbill 				}
7181496Sbill 				if (cp1) while (*cp2++= *cp1++);
7191506Sbill 				else *cp2=0;
7201506Sbill 			} else *cp2=0;
7211496Sbill 			continue;
7221496Sbill 		}
7231496Sbill 		/* check for (r),(r)+,-(r),[r] */
7241496Sbill 		do if (*cp1=='(' || *cp1=='[') {/* get register number */
7251496Sbill 			char t;
7261496Sbill 			cp2= ++cp1; while (*++cp1!=')' && *cp1!=']'); t= *cp1; *cp1=0;
7271496Sbill 			if (0<=(r=isreg(cp2)) && r<NUSE && (uses[r]==0 || uses[r]==p)) {
7281496Sbill 				uses[r]=p; regs[r][0]=(*--cp2=='[' ? OPX<<4 : OPB<<4);
7291496Sbill 			}
7301496Sbill 			*cp1=t;
7311496Sbill 		} while (*++cp1);
7321496Sbill 	}
7331496Sbill 	/* pushax or movax possibility? */
7341496Sbill 	cp1=regs[RT1];
7351496Sbill 	if (*cp1++=='$' && isstatic(cp1) && natural(regs[RT1])) {
7361496Sbill 		if (p->combop==T(MOV,LONG)) {
7371496Sbill 			if (regs[RT1][1]=='L' && 0!=(p->labno=getnum(regs[RT1]+2))) {
7381496Sbill 				cp1=p->code; while (*cp1++!=','); p->code= --cp1;
7391496Sbill 			}
7401496Sbill 			p->combop=T(MOVA,LONG); ++p->code; p->pop=0;
7411496Sbill 		} else if (p->combop==T(PUSH,LONG)) {
7421496Sbill 			p->combop=T(PUSHA,LONG); ++p->code; p->pop=0;
7431496Sbill 		} else if ((p->combop&0xFFFF)==T(ADD,U(LONG,OP3))
7441496Sbill 				 && 0<=(r=isreg(regs[RT2]))) {
7451496Sbill 			cp1=cp2=p->code; ++cp1;
7461496Sbill 			do *cp2++= *cp1; while (*cp1++!=','); cp2[-1]='[';
7471496Sbill 			do *cp2++= *cp1; while (*cp1++!=','); cp2[-1]=']';
7481496Sbill 			if (!equstr(regs[RT3],"-(sp)")) p->combop=T(MOVA,BYTE);
7491496Sbill 			else {p->combop=T(PUSHA,BYTE); *cp2=0;}
75024399Smckusick 			if (r < NUSE && uses[r] == 0) {
75124399Smckusick 				uses[r]=p;
75224399Smckusick 				regs[r][0]=OPX<<4;
75324399Smckusick 			}
7541496Sbill 			p->pop=0;
7551496Sbill 		}
7561496Sbill 	}
7571496Sbill 	return(p);
7581496Sbill }
7591496Sbill 
ispow2(n)7601496Sbill ispow2(n) register long n; {/* -1 -> no; else -> log to base 2 */
7611496Sbill 	register int log;
7621496Sbill 	if (n==0 || n&(n-1)) return(-1); log=0;
7631496Sbill 	for (;;) {n >>= 1; if (n==0) return(log); ++log; if (n== -1) return(log);}
7641496Sbill }
7651496Sbill 
766*55276Ssklower #ifdef notdef
bitopt(p)7671496Sbill bitopt(p) register struct node *p; {
7681496Sbill 	/* change "bitx $<power_of_2>,a" followed by JEQ or JNE
7691496Sbill 	/* into JBC or JBS.  watch out for I/O registers. (?)
7701496Sbill 	/* assumes that 'splitrand' has already been called.
7711496Sbill 	*/
7721496Sbill 	register char *cp1,*cp2; int b;
7731496Sbill 	cp1=regs[RT1]; cp2=regs[RT2];
7741496Sbill 	if (*cp1++!='$' || !okio(cp2) || p->forw->op!=CBR || p->forw->subop&-2 ||
77527461Sdonn 		0>(b=ispow2(getnum(cp1)))) return;
77627461Sdonn 	if (p->subop!=BYTE && !(b==0 && p->subop==LONG) &&
77727461Sdonn 		(source(cp2) || indexa(cp2))) return;
7781496Sbill 	if (b>=bitsize[p->subop]) {/* you dummy! */
7791496Sbill 		if (source(cp2)) {/* side effect: auto increment or decrement */
7801496Sbill 			p->pop=0;
7811496Sbill 			p->op=TST; --cp1; while (*cp1++= *cp2++);
7821496Sbill 			regs[RT2][0]=0; newcode(p);
7831496Sbill 		} else delnode(p);
7841496Sbill 		p = p->forw;
7851496Sbill 		if (p->subop==JEQ) {p->combop=JBR; p->pop=0;}
7861496Sbill 		else delnode(p);
7871496Sbill 		nchange++; nbj++; return;
7881496Sbill 	}
7891496Sbill 	if (cp1=p->forw->code) {/* destination is not an internal label */
7901496Sbill 		cp2=regs[RT3]; while (*cp2++= *cp1++);
7911496Sbill 	}
79227461Sdonn 	if (b==0 && (p->subop==LONG || !(source(regs[RT2]) || indexa(regs[RT2])))) {
79327461Sdonn 		/* JLB optimization, ala BLISS */
7941496Sbill 		cp2=regs[RT1]; cp1=regs[RT2]; while (*cp2++= *cp1++);
7951496Sbill 		cp2=regs[RT2]; cp1=regs[RT3]; while (*cp2++= *cp1++);
7961496Sbill 		*(regs[RT3])=0; p->forw->subop += JLBC-JBC;
7971496Sbill 		p->forw->pop=0;
7981496Sbill 	} else {
7991496Sbill 		cp1=regs[RT1]+1;
8001496Sbill 		if (b>9) *cp1++= b/10 +'0'; *cp1++= b%10 +'0'; *cp1=0; /* $<bit_number> */
8011496Sbill 	}
8021496Sbill 	nbj++; newcode(p); p->combop = p->forw->combop+((JBC-JEQ)<<8);
8031496Sbill 	p->labno = p->forw->labno; delnode(p->forw);
8041496Sbill 	p->pop=0;
8051496Sbill }
806*55276Ssklower #endif
8071496Sbill 
isfield(n)8081496Sbill isfield(n) register long n; {/* -1 -> no; else -> position of low bit */
80918381Sralph 	register int p; register long t;
8101496Sbill 	t= ((n-1)|n) +1;
81116832Sralph 	if (n!=0 && (0==t || 0<=ispow2(t))) {
81218381Sralph 		p=0; while(!(n&1)) {n >>= 1; ++p;} return(p);
8131496Sbill 	} else return(-1);
8141496Sbill }
8151496Sbill 
bixprep(p,bix)8161496Sbill bixprep(p,bix) register struct node *p; {
8171496Sbill /* initial setup, single-bit checking for bisopt, bicopt.
8181496Sbill /* return: 0->don't bother any more; 1->worthwhile trying
8191496Sbill */
8201496Sbill 	register char *cp1,*cp2;
8211496Sbill 	splitrand(p); cp1=regs[RT1]; cp2=regs[RT2];
8221496Sbill 	if (*cp1++!='$' || 0>(pos=isfield(f=getnum(cp1)))
8231496Sbill 	  || !okio(cp2) || indexa(cp2) || source(cp2) || !okio(lastrand)) return(0);
8241496Sbill 	f |= f-1; if (++f==0) siz=32-pos; else siz=ispow2(f)-pos;
8251496Sbill 	if (siz==1 && pos>5 && (p->subop>>4)==OP2 && (p->subop&0xF)!=BYTE
8261496Sbill 	  && pos<bitsize[p->subop&0xF]) {
8271496Sbill 		p->ref = insertl(p->forw); p->combop = CBR | (bix<<8);
8281496Sbill 		p->pop=0;
8291496Sbill 		p->labno = p->ref->labno;
8301496Sbill 		if (pos>9) {*cp1++= pos/10 +'0'; pos %= 10;}
8311496Sbill 		*cp1++=pos+'0'; *cp1=0; newcode(p); nbj++; return(0);
8321496Sbill 	}
8331496Sbill 	return(1);
8341496Sbill }
8351496Sbill 
8361496Sbill 
8371496Sbill struct node *
bicopt(p)8381496Sbill bicopt(p) register struct node *p; {
8391496Sbill /* use field operations or MOVZ if possible.  done as part of 'bflow'.
8401496Sbill */
8411496Sbill 	register char *cp1,*cp2; int r;
8423933Sroot 	char src[C2_ASIZE];
84318381Sralph 	char lhssiz, sop;
8441496Sbill 	if (!bixprep(p,JBCC)) return(p);
8451496Sbill 	if (f==0) {/* the BIC isolates low order bits */
8461496Sbill 		siz=pos; pos=0;
8471496Sbill 		if ((p->subop&0xF)==LONG && *(regs[RT2])!='$') {/* result of EXTZV is long */
8481496Sbill 			/* save source of BICL in 'src' */
8491496Sbill 			cp1=regs[RT2]; cp2=src; while (*cp2++= *cp1++);
8501496Sbill 			if (p->back->op==ASH) {/* try for more */
8511496Sbill 				splitrand(p->back); cp1=regs[RT1]; cp2=regs[RT3];
8521496Sbill 				if (*cp1++=='$' && *(regs[RT2])!='$' && !indexa(regs[RT2])
8531496Sbill 				  && 0>(f=getnum(cp1)) && equstr(src,cp2)
85416832Sralph 				  && 0<=(r=isreg(cp2)) && r<NUSE
85516832Sralph 				  && siz-f <= 32) {	/* a good ASH */
8561496Sbill 					pos -= f; cp1=regs[RT2]; cp2=src; while (*cp2++= *cp1++);
8571496Sbill 					delnode(p->back);
8581496Sbill 				}
8591496Sbill 			}
86016133Sralph 			/*
86116133Sralph 			 * 'pos', 'siz' known; find out the size of the
86216133Sralph 			 * left-hand operand of what the bicl will turn into.
86316133Sralph 			 */
86416133Sralph 			if (pos==0 && siz==16)
86516133Sralph 				lhssiz = WORD;	/* movzwl */
86616133Sralph 			else
86716133Sralph 				lhssiz = BYTE;	/* movzbl or extzvl */
8681496Sbill 			if (p->back->op==CVT || p->back->op==MOVZ) {/* greedy, aren't we? */
8691496Sbill 				splitrand(p->back); cp1=regs[RT1]; cp2=regs[RT2];
87016133Sralph 				/*
87116133Sralph 				 * If indexa(cp1) || autoid(cp1), the fold may
87216133Sralph 				 * still be OK if the CVT/MOVZ has the same
87316133Sralph 				 * size operand on its left size as what we
87416133Sralph 				 * will turn the bicl into.
87516133Sralph 				 * However, if the CVT is from a float or
87616133Sralph 				 * double, forget it!
87716133Sralph 				 */
87818381Sralph 				sop = p->back->subop&0xF;	/* type of LHS of CVT/MOVZ */
87916133Sralph 				if (equstr(src,cp2) && okio(cp1)
88018381Sralph 				  && sop != FFLOAT && sop != DFLOAT
88118381Sralph 				  && sop != GFLOAT && sop != HFLOAT
88218381Sralph 				  && ((!indexa(cp1) && !autoid(cp1)) || lhssiz == sop)
8831496Sbill 				  && 0<=(r=isreg(cp2)) && r<NUSE
88418381Sralph 				  && bitsize[sop]>=(pos+siz)
8851496Sbill 				  && bitsize[p->back->subop>>4]>=(pos+siz)) {/* good CVT */
8861496Sbill 					cp1=regs[RT1]; cp2=src; while (*cp2++= *cp1++);
8871496Sbill 					delnode(p->back);
8881496Sbill 				}
8891496Sbill 			}
8901496Sbill 			/* 'pos', 'siz' known; source of field is in 'src' */
8911496Sbill 			splitrand(p); /* retrieve destination of BICL */
89216133Sralph 			if ((siz==8 || siz==16) && pos==0) {
89316133Sralph 				p->combop = T(MOVZ,U(lhssiz,LONG));
8941496Sbill 				sprintf(line,"%s,%s",src,lastrand);
8951496Sbill 			} else {
8961496Sbill 				p->combop = T(EXTZV,LONG);
8971496Sbill 				sprintf(line,"$%d,$%d,%s,%s",pos,siz,src,lastrand);
8981496Sbill 			}
8991496Sbill 			p->pop=0;
9001496Sbill 			p->code = copy(line); nfield++; return(p);
9011496Sbill 		}/* end EXTZV possibility */
9021496Sbill 	}/* end low order bits */
9031496Sbill /* unfortunately, INSV clears the condition codes, thus cannot be used */
9041496Sbill /*	else {/* see if BICL2 of positive field should be INSV $0 */
9051496Sbill /*		if (p->subop==(LONG | (OP2<<4)) && 6<=(pos+siz)) {
9061496Sbill /*			p->combop = INSV;
9071496Sbill /*			sprintf(line,"$0,$%d,$%d,%s",pos,siz,lastrand);
9081496Sbill /*			p->code = copy(line); nfield++; return(p);
9091496Sbill /*		}
9101496Sbill /*	}
9111496Sbill */
9121496Sbill 	return(p);
9131496Sbill }
9141496Sbill 
jumpsw()9151496Sbill jumpsw()
9161496Sbill {
9171496Sbill 	register struct node *p, *p1;
91818381Sralph 	register struct node *tp;
91918381Sralph 	long tl;
92018381Sralph 	char *tcp;
92118381Sralph 	int ti;
9221496Sbill 	int nj;
9231496Sbill 
92418381Sralph 	ti = 0;
9251496Sbill 	nj = 0;
9261496Sbill 	for (p=first.forw; p!=0; p = p->forw)
92718381Sralph 		p->seq = ++ti;
9281496Sbill 	for (p=first.forw; p!=0; p = p1) {
9291496Sbill 		p1 = p->forw;
9301496Sbill 		if (p->op == CBR && p1->op==JBR && p->ref && p1->ref
9311496Sbill 		 && abs(p->seq - p->ref->seq) > abs(p1->seq - p1->ref->seq)) {
9321496Sbill 			if (p->ref==p1->ref)
9331496Sbill 				continue;
9341496Sbill 			p->subop = revbr[p->subop];
9351496Sbill 			p->pop=0;
93618381Sralph 			tp = p1->ref;
9371496Sbill 			p1->ref = p->ref;
93818381Sralph 			p->ref = tp;
93918381Sralph 			tl = p1->labno;
9401496Sbill 			p1->labno = p->labno;
94118381Sralph 			p->labno = tl;
9421496Sbill #ifdef COPYCODE
9431496Sbill 			if (p->labno == 0) {
94418381Sralph 				tcp = p1->code;
94518381Sralph 				p1->code = p->code;
94618381Sralph 				p->code = tcp;
9471496Sbill 			}
9481496Sbill #endif
9491496Sbill 			nrevbr++;
9501496Sbill 			nj++;
9511496Sbill 		}
9521496Sbill 	}
9531496Sbill 	return(nj);
9541496Sbill }
9551496Sbill 
addsob()9561496Sbill addsob()
9571496Sbill {
9581496Sbill 	register struct node *p, *p1, *p2, *p3;
9591496Sbill 
9601496Sbill 	for (p = &first; (p1 = p->forw)!=0; p = p1) {
9611496Sbill 	if (p->combop==T(DEC,LONG) && p1->op==CBR) {
9623933Sroot 		if (abs(p->seq - p1->ref->seq) > 8) continue;
9631496Sbill 		if (p1->subop==JGE || p1->subop==JGT) {
9641496Sbill 			if (p1->subop==JGE) p->combop=SOBGEQ; else p->combop=SOBGTR;
9651496Sbill 			p->pop=0;
9661496Sbill 			p->labno = p1->labno; delnode(p1); nsob++;
9671496Sbill 		}
9681496Sbill 	} else if (p->combop==T(INC,LONG)) {
9691496Sbill 		if (p1->op==LABEL && p1->refc==1 && p1->forw->combop==T(CMP,LONG)
9701496Sbill 		  && (p2=p1->forw->forw)->combop==T(CBR,JLE)
9711496Sbill 		  && (p3=p2->ref->back)->combop==JBR && p3->ref==p1
9721496Sbill 		  && p3->forw->op==LABEL && p3->forw==p2->ref) {
9731496Sbill 			/* change	INC LAB: CMP	to	LAB: INC CMP */
9741496Sbill 			p->back->forw=p1; p1->back=p->back;
9751496Sbill 			p->forw=p1->forw; p1->forw->back=p;
9761496Sbill 			p->back=p1; p1->forw=p;
9771496Sbill 			p1=p->forw;
9781496Sbill 			/* adjust beginning value by 1 */
9791496Sbill 				p2=alloc(sizeof first); p2->combop=T(DEC,LONG);
9801496Sbill 				p2->pop=0;
9811496Sbill 				p2->forw=p3; p2->back=p3->back; p3->back->forw=p2;
9821496Sbill 				p3->back=p2; p2->code=p->code; p2->labno=0;
9831496Sbill 		}
9841496Sbill 		if (p1->combop==T(CMP,LONG) && (p2=p1->forw)->op==CBR) {
9851496Sbill 			register char *cp1,*cp2;
9861496Sbill 			splitrand(p1); if (!equstr(p->code,regs[RT1])) continue;
9873933Sroot 			if (abs(p->seq - p2->ref->seq)>8) {/* outside byte displ range */
9881496Sbill 				if (p2->subop!=JLE) continue;
9891496Sbill 				p->combop=T(ACB,LONG);
9901496Sbill 				cp2=regs[RT1]; cp1=regs[RT2]; while (*cp2++= *cp1++); /* limit */
9911496Sbill 				cp2=regs[RT2]; cp1="$1"; while (*cp2++= *cp1++); /* increment */
9921496Sbill 				cp2=regs[RT3]; cp1=p->code; while (*cp2++= *cp1++); /* index */
9931496Sbill 				p->pop=0; newcode(p);
9941496Sbill 				p->labno = p2->labno; delnode(p2); delnode(p1); nsob++;
9951496Sbill 			} else if (p2->subop==JLE || p2->subop==JLT) {
9961496Sbill 				if (p2->subop==JLE) p->combop=AOBLEQ; else p->combop=AOBLSS;
9971496Sbill 				cp2=regs[RT1]; cp1=regs[RT2]; while (*cp2++= *cp1++); /* limit */
9981496Sbill 				cp2=regs[RT2]; cp1=p->code; while (*cp2++= *cp1++); /* index */
9991496Sbill 				p->pop=0; newcode(p);
10001496Sbill 				p->labno = p2->labno; delnode(p2); delnode(p1); nsob++;
10011496Sbill 			}
10021496Sbill 		}
10031496Sbill 	}
10041496Sbill 	}
10051496Sbill }
10061496Sbill 
equop(p1,p2)10071496Sbill equop(p1, p2)
10081496Sbill register struct node *p1;
10091496Sbill struct node *p2;
10101496Sbill {
10111496Sbill 	register char *cp1, *cp2;
10121496Sbill 
10131496Sbill 	if (p1->combop != p2->combop)
10141496Sbill 		return(0);
101532985Sdonn 	if (p1->combop == 0 && p1->pop != p2->pop)
101632985Sdonn 		return(0);
10171496Sbill 	if (p1->op>0 && p1->op<MOV)
10181496Sbill 		return(0);
101918381Sralph 	switch (p1->combop) {
102018381Sralph 	case EROU:	case JSW:	case TEXT:	case DATA:
102118381Sralph 	case BSS:	case ALIGN:	case WGEN:	case END:
102218381Sralph 		/*
102318381Sralph 		 * Consider all pseudo-ops to be unique.
102418381Sralph 		 */
102518381Sralph 		return(0);
102618381Sralph 	}
10271496Sbill 	if (p1->op==MOVA && p1->labno!=p2->labno) return(0);
10281496Sbill 	cp1 = p1->code;
10291496Sbill 	cp2 = p2->code;
10301496Sbill 	if (cp1==0 && cp2==0)
10311496Sbill 		return(1);
10321496Sbill 	if (cp1==0 || cp2==0)
10331496Sbill 		return(0);
10341496Sbill 	while (*cp1 == *cp2++)
10351496Sbill 		if (*cp1++ == 0)
10361496Sbill 			return(1);
10371496Sbill 	return(0);
10381496Sbill }
10391496Sbill 
104018381Sralph #ifndef delnode
delnode(p)10411496Sbill delnode(p) register struct node *p; {
10421496Sbill 	p->back->forw = p->forw;
10431496Sbill 	p->forw->back = p->back;
10441496Sbill }
104518381Sralph #endif
10461496Sbill 
104718381Sralph #ifndef decref
decref(p)10481496Sbill decref(p)
10491496Sbill register struct node *p;
10501496Sbill {
10511496Sbill 	if (p && --p->refc <= 0) {
10521496Sbill 		nrlab++;
10531496Sbill 		delnode(p);
10541496Sbill 	}
10551496Sbill }
105618381Sralph #endif
10571496Sbill 
10581496Sbill struct node *
nonlab(ap)10591496Sbill nonlab(ap)
10601496Sbill struct node *ap;
10611496Sbill {
10621496Sbill 	register struct node *p;
10631496Sbill 
10641496Sbill 	p = ap;
10651496Sbill 	while (p && p->op==LABEL)
10661496Sbill 		p = p->forw;
10671496Sbill 	return(p);
10681496Sbill }
10691496Sbill 
clearuse()10701496Sbill clearuse() {
10711496Sbill 	register struct node **i;
10721496Sbill 	for (i=uses+NUSE; i>uses;) *--i=0;
10731496Sbill }
10741496Sbill 
clearreg()10751496Sbill clearreg() {
107618381Sralph 	register char **i;
107718381Sralph 	for (i=regs; i<regs+NREG; ++i) {
107818381Sralph 		**i = 0;
107918381Sralph 		*(*i+1) = 0;
108018381Sralph 	}
108118381Sralph 	conloc[0] = 0;
108218381Sralph 	ccloc[0] = 0;
10831496Sbill }
10841496Sbill 
savereg(ai,s,type)10851496Sbill savereg(ai, s, type)
10861496Sbill register char *s;
10871496Sbill {
10881496Sbill 	register char *p, *sp;
10891496Sbill 
10901496Sbill 	sp = p = regs[ai];
10911496Sbill 	if (source(s)) /* side effects in addressing */
10921496Sbill 		return;
10931496Sbill 	/* if any indexing, must be parameter or local */
10941496Sbill 	/* indirection (as in "*-4(fp)") is ok, however */
10951496Sbill 	*p++ = type;
10961496Sbill 	while (*p++ = *s)
10971496Sbill 		if (*s=='[' || *s++=='(' && *s!='a' && *s!='f') {*sp = 0; return;}
10981496Sbill }
10991496Sbill 
dest(s,type)11001496Sbill dest(s,type)
11011496Sbill register char *s;
11021496Sbill {
11031496Sbill 	register int i;
11041496Sbill 
110518381Sralph 	(void) source(s); /* handle addressing side effects */
110617210Sralph 	if (!natural(s)) {
110717210Sralph 		/* wild store, everything except constants vanishes */
110817210Sralph 		for (i=NREG; --i>=0;)
110918381Sralph 			if (regs[i][1] != '$')
111018381Sralph 				regs[i][0] = regs[i][1] = 0;
111117210Sralph 		conloc[0] = 0; ccloc[0] = 0;
111217210Sralph 		return;
111317210Sralph 	}
11141496Sbill 	if ((i = isreg(s)) >= 0) {
111518381Sralph 		/* if register destination, that reg is a goner */
111618381Sralph 		regs[i][0] = regs[i][1] = 0;
11175794Sroot 		switch(type & 0xF){
11185794Sroot 		case DFLOAT:	/* clobber two at once */
11195794Sroot 			/*FALLTHROUGH*/
11205794Sroot 		case GFLOAT:
112118381Sralph 			regs[i+1][0] = regs[i+1][1] = 0;
11225794Sroot 			break;
11235794Sroot 		case HFLOAT:	/* clobber four at once */
112418381Sralph 			regs[i+1][0] = regs[i+1][1] = 0;
112518381Sralph 			regs[i+2][0] = regs[i+2][1] = 0;
112618381Sralph 			regs[i+3][0] = regs[i+3][1] = 0;
11275794Sroot 			break;
11285794Sroot 		}
11295794Sroot 		switch((type>>4)&0xF){
11305794Sroot 		case DFLOAT:	/* clobber two at once */
11315794Sroot 			/*FALLTHROUGH*/
11325794Sroot 		case GFLOAT:
113318381Sralph 			regs[i+1][0] = regs[i+1][1] = 0;
11345794Sroot 			break;
11355794Sroot 		case HFLOAT:	/* clobber four at once */
113618381Sralph 			regs[i+1][0] = regs[i+1][1] = 0;
113718381Sralph 			regs[i+2][0] = regs[i+2][1] = 0;
113818381Sralph 			regs[i+3][0] = regs[i+3][1] = 0;
11395794Sroot 			break;
11405794Sroot 		}
11411496Sbill 	}
11421496Sbill 	for (i=NREG; --i>=0;)
11431496Sbill 		if (regs[i][1]=='*' && equstr(s, regs[i]+2))
114418381Sralph 			regs[i][0] = regs[i][1] = 0; /* previous indirection through destination is invalid */
11451496Sbill 	while ((i = findrand(s,0)) >= 0) /* previous values of destination are invalid */
114618381Sralph 		regs[i][0] = regs[i][1] = 0;
114717210Sralph 	if (*conloc && equstr(conloc, s))
114817210Sralph 		conloc[0] = 0;
114917210Sralph 	setcc(s, type); /* natural destinations set condition codes */
11501496Sbill }
11511496Sbill 
115217210Sralph /* separate operands at commas, set up 'regs' and 'lastrand' */
11531496Sbill splitrand(p) struct node *p; {
115417210Sralph 	register char *p1, *p2;
115517210Sralph 	register char **preg;
115617210Sralph 
115717210Sralph 	preg = regs+RT1;
115817210Sralph 	if (p1 = p->code)
115917210Sralph 		while (*p1) {
116017210Sralph 			lastrand = p2 = *preg++;
116117210Sralph 			while (*p1)
116217210Sralph 				if (',' == (*p2++ = *p1++)) {
116317210Sralph 					--p2;
116417210Sralph 					break;
116517210Sralph 				}
116617210Sralph 			*p2 = 0;
116717210Sralph 		}
116817210Sralph 	while (preg < (regs+RT1+5))
116917210Sralph 		*(*preg++) = 0;
11701496Sbill }
11711496Sbill 
compat(have,want)11721496Sbill compat(have, want) {
11731496Sbill register int hsrc, hdst;
11741496Sbill if (0==(want &= 0xF)) return(1); /* anything satisfies a wildcard want */
117526407Sdonn hsrc=have&0xF;
117626407Sdonn if (0==(hdst=((have>>4)&0xF)) || (hdst>=OP2 && hdst<=OPX)) hdst=hsrc;
11775794Sroot if (want>=FFLOAT) return(hdst==want && hsrc==want);
11785794Sroot 	/* FLOAT, DFLOAT not compat: rounding */
11795794Sroot return(hsrc>=want && hdst>=want && hdst<FFLOAT);
11801496Sbill }
11811496Sbill 
equtype(t1,t2)11821496Sbill equtype(t1,t2) {return(compat(t1,t2) && compat(t2,t1));}
11831496Sbill 
findrand(as,type)11841496Sbill findrand(as, type)
11851496Sbill char *as;
11861496Sbill {
11871496Sbill 	register char **i;
11881496Sbill 	for (i = regs+NREG; --i>=regs;) {
11891496Sbill 		if (**i && equstr(*i+1, as) && compat(**i,type))
11901496Sbill 			return(i-regs);
11911496Sbill 	}
11921496Sbill 	return(-1);
11931496Sbill }
11941496Sbill 
isreg(s)11951496Sbill isreg(s)
11961496Sbill register char *s;
11971496Sbill {
11981496Sbill 	if (*s++!='r' || !isdigit(*s++)) return(-1);
11991496Sbill 	if (*s==0) return(*--s-'0');
12001496Sbill 	if (*(s-1)=='1' && isdigit(*s++) && *s==0) return(10+*--s-'0');
12011496Sbill 	return(-1);
12021496Sbill }
12031496Sbill 
check()12041496Sbill check()
12051496Sbill {
12061496Sbill 	register struct node *p, *lp;
12071496Sbill 
12081496Sbill 	lp = &first;
12091496Sbill 	for (p=first.forw; p!=0; p = p->forw) {
121018381Sralph 		if (p->back != lp) {
121118381Sralph 			fprintf(stderr, "c2: failed internal consistency check -- help!\n");
121218381Sralph 			exit(-1);
121318381Sralph 		}
12141496Sbill 		lp = p;
12151496Sbill 	}
12161496Sbill }
12171496Sbill 
source(ap)12181496Sbill source(ap)
12191496Sbill char *ap;
12201496Sbill {
12211496Sbill 	register char *p1, *p2;
12221496Sbill 
12231496Sbill 	p1 = ap;
12241496Sbill 	p2 = p1;
12251496Sbill 	if (*p1==0)
12261496Sbill 		return(0);
12271496Sbill 	while (*p2++ && *(p2-1)!='[');
12281496Sbill 	if (*p1=='-' && *(p1+1)=='('
12291496Sbill 	 || *p1=='*' && *(p1+1)=='-' && *(p1+2)=='('
12301496Sbill 	 || *(p2-2)=='+') {
12311496Sbill 		while (*p1 && *p1++!='r');
12321496Sbill 		if (isdigit(*p1++))
123318381Sralph 			if (isdigit(*p1))
123418381Sralph 				regs[10+*p1-'0'][0] = regs[10+*p1-'0'][1] = 0;
123518381Sralph 			else {
123618381Sralph 				--p1;
123718381Sralph 				regs[*p1-'0'][0] = regs[*p1-'0'][1] = 0;
123818381Sralph 			}
12391496Sbill 		return(1);
12401496Sbill 	}
12411496Sbill 	return(0);
12421496Sbill }
12431496Sbill 
12441496Sbill newcode(p) struct node *p; {
12451496Sbill 	register char *p1,*p2,**preg;
12461496Sbill 	preg=regs+RT1; p2=line;
12471496Sbill 	while (*(p1= *preg++)) {while (*p2++= *p1++); *(p2-1)=',';}
12481496Sbill 	*--p2=0;
12491496Sbill 	p->code=copy(line);
12501496Sbill }
12511496Sbill 
12521496Sbill repladdr(p)
12531496Sbill struct node *p;
12541496Sbill {
12551496Sbill 	register r;
125618381Sralph 	register char *p1;
12571496Sbill 	char **preg; int nrepl;
12581496Sbill 
12591496Sbill 	preg=regs+RT1; nrepl=0;
12601496Sbill 	while (lastrand!=(p1= *preg++))
12611496Sbill 		if (!source(p1) && 0<=(r=findrand(p1,p->subop))) {
12621496Sbill 			*p1++='r'; if (r>9) {*p1++='1'; r -= 10;} *p1++=r+'0'; *p1=0;
12631496Sbill 			nrepl++; nsaddr++;
12641496Sbill 		}
12651496Sbill 	if (nrepl) newcode(p);
12661496Sbill }
12671496Sbill 
12681496Sbill /* movedat()
12691496Sbill /* {
12701496Sbill /* 	register struct node *p1, *p2;
12711496Sbill /* 	struct node *p3;
12721496Sbill /* 	register seg;
12731496Sbill /* 	struct node data;
12741496Sbill /* 	struct node *datp;
12751496Sbill /*
12761496Sbill /* 	if (first.forw == 0)
12771496Sbill /* 		return;
12781496Sbill /* 	datp = &data;
12791496Sbill /* 	for (p1 = first.forw; p1!=0; p1 = p1->forw) {
12801496Sbill /* 		if (p1->op == DATA) {
12811496Sbill /* 			p2 = p1->forw;
12821496Sbill /* 			while (p2 && p2->op!=TEXT)
12831496Sbill /* 				p2 = p2->forw;
12841496Sbill /* 			if (p2==0)
12851496Sbill /* 				break;
12861496Sbill /* 			p3 = p1->back;
12871496Sbill /* 			p1->back->forw = p2->forw;
12881496Sbill /* 			p2->forw->back = p3;
12891496Sbill /* 			p2->forw = 0;
12901496Sbill /* 			datp->forw = p1;
12911496Sbill /* 			p1->back = datp;
12921496Sbill /* 			p1 = p3;
12931496Sbill /* 			datp = p2;
12941496Sbill /* 		}
12951496Sbill /* 	}
12961496Sbill /* 	if (data.forw) {
12971496Sbill /* 		datp->forw = first.forw;
12981496Sbill /* 		first.forw->back = datp;
12991496Sbill /* 		data.forw->back = &first;
13001496Sbill /* 		first.forw = data.forw;
13011496Sbill /* 	}
13021496Sbill /* 	seg = -1;
13031496Sbill /* 	for (p1 = first.forw; p1!=0; p1 = p1->forw) {
13041496Sbill /* 		if (p1->op==TEXT||p1->op==DATA||p1->op==BSS) {
13051496Sbill /* 			if (p1->op == seg || p1->forw&&p1->forw->op==seg) {
13061496Sbill /* 				p1->back->forw = p1->forw;
13071496Sbill /* 				p1->forw->back = p1->back;
13081496Sbill /* 				p1 = p1->back;
13091496Sbill /* 				continue;
13101496Sbill /* 			}
13111496Sbill /* 			seg = p1->op;
13121496Sbill /* 		}
13131496Sbill /* 	}
13141496Sbill /* }
13151496Sbill */
13161496Sbill 
redunbr(p)13171496Sbill redunbr(p)
13181496Sbill register struct node *p;
13191496Sbill {
13201496Sbill 	register struct node *p1;
13211496Sbill 	register char *ap1;
13221496Sbill 	char *ap2;
13231496Sbill 
13241496Sbill 	if ((p1 = p->ref) == 0)
13251496Sbill 		return;
13261496Sbill 	p1 = nonlab(p1);
13271496Sbill 	if (p1->op==TST) {
13281496Sbill 		splitrand(p1);
13291496Sbill 		savereg(RT2, "$0", p1->subop);
13301496Sbill 	} else if (p1->op==CMP)
13311496Sbill 		splitrand(p1);
13321496Sbill 	else
13331496Sbill 		return;
13341496Sbill 	if (p1->forw->op==CBR) {
13351496Sbill 		ap1 = findcon(RT1, p1->subop);
13361496Sbill 		ap2 = findcon(RT2, p1->subop);
13371496Sbill 		p1 = p1->forw;
13381496Sbill 		if (compare(p1->subop, ap1, ap2)) {
13391496Sbill 			nredunj++;
13401496Sbill 			nchange++;
13411496Sbill 			decref(p->ref);
13421496Sbill 			p->ref = p1->ref;
13431496Sbill 			p->labno = p1->labno;
13441496Sbill #ifdef COPYCODE
13451496Sbill 			if (p->labno == 0)
13461496Sbill 				p->code = p1->code;
134717210Sralph #endif
13481496Sbill 			if (p->ref)
13491496Sbill 				p->ref->refc++;
13501496Sbill 		}
13511496Sbill 	} else if (p1->op==TST && equstr(regs[RT1],ccloc+1) &&
13521496Sbill 			equtype(ccloc[0],p1->subop)) {
135334255Sdonn 		p1=insertl(p1->forw);
135434255Sdonn 		decref(p->ref);
135534255Sdonn 		p->ref=p1;
135634255Sdonn 		p->labno=p1->labno;
13571496Sbill 		nrtst++; nchange++;
13581496Sbill 	}
13591496Sbill }
13601496Sbill 
13611496Sbill char *
findcon(i,type)13621496Sbill findcon(i, type)
13631496Sbill {
13641496Sbill 	register char *p;
13651496Sbill 	register r;
13661496Sbill 
13671496Sbill 	p = regs[i];
13681496Sbill 	if (*p=='$')
13691496Sbill 		return(p);
13701496Sbill 	if ((r = isreg(p)) >= 0 && compat(regs[r][0],type))
13711496Sbill 		return(regs[r]+1);
13721496Sbill 	if (equstr(p, conloc))
13731496Sbill 		return(conval+1);
13741496Sbill 	return(p);
13751496Sbill }
13761496Sbill 
compare(opc,acp1,acp2)137718381Sralph compare(opc, acp1, acp2)
13781496Sbill char *acp1, *acp2;
13791496Sbill {
13801496Sbill 	register char *cp1, *cp2;
13811496Sbill 	register n1;
13821496Sbill 	int n2;	int sign;
13831496Sbill 
13841496Sbill 	cp1 = acp1;
13851496Sbill 	cp2 = acp2;
13861496Sbill 	if (*cp1++ != '$' || *cp2++ != '$')
13871496Sbill 		return(0);
13881496Sbill 	n1 = 0; sign=1; if (*cp2=='-') {++cp2; sign= -1;}
13891496Sbill 	while (isdigit(*cp2)) {n1 *= 10; n1 += (*cp2++ - '0')*sign;}
13901496Sbill 	n2 = n1;
13911496Sbill 	n1 = 0; sign=1; if (*cp1=='-') {++cp1; sign= -1;}
13921496Sbill 	while (isdigit(*cp1)) {n1 *= 10; n1 += (*cp1++ - '0')*sign;}
13931496Sbill 	if (*cp1=='+')
13941496Sbill 		cp1++;
13951496Sbill 	if (*cp2=='+')
13961496Sbill 		cp2++;
13971496Sbill 	do {
13981496Sbill 		if (*cp1++ != *cp2)
13991496Sbill 			return(0);
14001496Sbill 	} while (*cp2++);
140118381Sralph 	switch(opc) {
14021496Sbill 
14031496Sbill 	case JEQ:
140418381Sralph 		return(n1 == n2);
14051496Sbill 	case JNE:
140618381Sralph 		return(n1 != n2);
14071496Sbill 	case JLE:
140818381Sralph 		return(n1 <= n2);
14091496Sbill 	case JGE:
141018381Sralph 		return(n1 >= n2);
14111496Sbill 	case JLT:
141218381Sralph 		return(n1 < n2);
14131496Sbill 	case JGT:
141418381Sralph 		return(n1 > n2);
14151496Sbill 	case JLO:
141618381Sralph 		return((unsigned) n1 < (unsigned) n2);
14171496Sbill 	case JHI:
141818381Sralph 		return((unsigned) n1 > (unsigned) n2);
14191496Sbill 	case JLOS:
142018381Sralph 		return((unsigned) n1 <= (unsigned) n2);
14211496Sbill 	case JHIS:
142218381Sralph 		return((unsigned) n1 >= (unsigned) n2);
14231496Sbill 	}
14241496Sbill 	return(0);
14251496Sbill }
14261496Sbill 
setcon(cv,cl,type)14271496Sbill setcon(cv, cl, type)
14281496Sbill register char *cv, *cl;
14291496Sbill {
14301496Sbill 	register char *p;
14311496Sbill 
14321496Sbill 	if (*cv != '$')
14331496Sbill 		return;
14341496Sbill 	if (!natural(cl))
14351496Sbill 		return;
14361496Sbill 	p = conloc;
14371496Sbill 	while (*p++ = *cl++);
14381496Sbill 	p = conval;
14391496Sbill 	*p++ = type;
14401496Sbill 	while (*p++ = *cv++);
14411496Sbill }
14421496Sbill 
equstr(p1,p2)14431496Sbill equstr(p1, p2)
14441496Sbill register char *p1, *p2;
14451496Sbill {
14461496Sbill 	do {
14471496Sbill 		if (*p1++ != *p2)
14481496Sbill 			return(0);
14491496Sbill 	} while (*p2++);
14501496Sbill 	return(1);
14511496Sbill }
14521496Sbill 
setcc(ap,type)14531496Sbill setcc(ap,type)
14541496Sbill char *ap;
14551496Sbill {
14561496Sbill 	register char *p, *p1;
14571496Sbill 
14581496Sbill 	p = ap;
14591496Sbill 	if (!natural(p)) {
14601496Sbill 		ccloc[0] = 0;
14611496Sbill 		return;
14621496Sbill 	}
14631496Sbill 	p1 = ccloc;
14641496Sbill 	*p1++ = type;
14651496Sbill 	while (*p1++ = *p++);
14661496Sbill }
14671496Sbill 
okio(p)14681496Sbill okio(p) register char *p; {/* 0->probable I/O space address; 1->not */
14691496Sbill 	if (ioflag && (!natural(p) || 0>getnum(p))) return(0);
14701496Sbill 	return(1);
14711496Sbill }
14721496Sbill 
indexa(p)14731496Sbill indexa(p) register char *p; {/* 1-> uses [r] addressing mode; 0->doesn't */
14741496Sbill 	while (*p) if (*p++=='[') return(1);
14751496Sbill 	return(0);
14761496Sbill }
14771496Sbill 
natural(p)14781496Sbill natural(p)
14791496Sbill register char *p;
14801496Sbill {/* 1->simple local, parameter, global, or register; 0->otherwise */
148117210Sralph 	if (*p=='*' || *p=='(' || *p=='-'&&p[1]=='(' || *p=='$'&&getnum(p+1))
14821496Sbill 		return(0);
14831496Sbill 	while (*p++);
14841496Sbill 	p--;
148517210Sralph 	if (*--p=='+' || *p==']' || *p==')' && p[-2]!='a' && p[-2]!='f')
14861496Sbill 		return(0);
14871496Sbill 	return(1);
14881496Sbill }
14891496Sbill 
14901496Sbill /*
14911496Sbill ** Tell if an argument is most likely static.
14921496Sbill */
14931496Sbill 
isstatic(cp)14941496Sbill isstatic(cp)
14951496Sbill register char	*cp;
14961496Sbill {
14971496Sbill 	if (*cp == '_' || *cp == 'L' || (*cp++ == 'v' && *cp == '.'))
14981496Sbill 		return (1);
14991496Sbill 	return (0);
15001496Sbill }
150116133Sralph 
autoid(p)150216133Sralph autoid(p) register char *p; {/* 1-> uses autoincrement/autodecrement; 0->doesn't */
150316133Sralph 	if (*p == '-' && *(p+1) == '(') return(1);
150416133Sralph 	while (*p) p++;
150516133Sralph 	if (*--p == '+' && *--p == ')') return(1);
150616133Sralph 	return(0);
150716133Sralph   }
1508