xref: /csrg-svn/old/pcc/c2.vax/c21.c (revision 33360)
118381Sralph #ifndef lint
2*33360Sdonn static	char sccsid[] = "@(#)c21.c 4.23 01/15/88";
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 
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 
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);
2371496Sbill 				if (shfrom != shto)
2381496Sbill 				{
2391496Sbill 					uses[shto] = NULL; splitrand(pf);
2401496Sbill 					cp2=regs[RT1]; while (*cp2++!='[');
2411496Sbill 					cp1=regfrom; while (*cp2++= *cp1++);
24218381Sralph 					*--cp2 = ']';
24318381Sralph 					*++cp2 = '\0';
2441496Sbill 					newcode(pf);
2451496Sbill 				}
2461496Sbill 			}
2471496Sbill 			else
2481496Sbill 			{
2491496Sbill 				p->op = MOV; splitrand(p);
2501496Sbill 				strcpy(regs[RT1], regs[RT2]);
2511496Sbill 				strcpy(regs[RT2], regs[RT3]);
2521496Sbill 				regs[RT3][0] = '\0';
2531496Sbill 				p->pop = 0; newcode(p);
2541496Sbill 			}
2551496Sbill 			switch (shcnt)
2561496Sbill 			{
2571496Sbill 			case 1:	pf->subop = WORD; break;
2581496Sbill 			case 2:	pf->subop = LONG; break;
2591496Sbill 			case 3:	pf->subop = QUAD; break;
2601496Sbill 			}
2611496Sbill 			redunm++; nsaddr++; nchange++;
26225756Sdonn 			goto std;
2631496Sbill 		}
2641496Sbill ashadd:
2651496Sbill 		/* at this point, RT2 and RT3 are guaranteed to be simple regs*/
2661496Sbill 		if (shcnt == 1 && equstr(regs[RT2], regs[RT3]))
2671496Sbill 		{
2681496Sbill 			/*
2691496Sbill 			** quickie:
2701496Sbill 			**	ashl	$1,A,A	>	addl2	A,A
2711496Sbill 			*/
2721496Sbill 			p->op = ADD; p->subop = U(LONG,OP2); p->pop = 0;
2731496Sbill 			strcpy(regs[RT1], regs[RT2]); regs[RT3][0] = '\0';
2741496Sbill 			newcode(p); nchange++;
2751496Sbill 		}
2761496Sbill 		goto std;
2771496Sbill 	}
2781496Sbill 
2791496Sbill 	case EXTV:
2801496Sbill 	case EXTZV:
2811496Sbill 	{
2821496Sbill 		/* bit tests:
2831496Sbill 		**	extv	A,$1,B,rC  \
2841496Sbill 		**	tstl	rC	    >	jbc	A,B,D
2851496Sbill 		**	jeql	D	   /
2861496Sbill 		**
2871496Sbill 		** also byte- and word-size fields:
2881496Sbill 		**	extv	$n*8,$8,A,B	>	cvtbl	n+A,B
28916133Sralph 		**	extv	$n*16,$16,A,B	>	cvtwl	2n+A,B
2901496Sbill 		**	extzv	$n*8,$8,A,B	>	movzbl	n+A,B
29116133Sralph 		**	extzv	$n*16,$16,A,B	>	movzwl	2n+A,B
2921496Sbill 		*/
2931496Sbill 		register struct	node	*pf;	/* forward node */
2941496Sbill 		register struct	node	*pn;	/* next node (after pf) */
2951496Sbill 		int	flen;			/* field length */
2961496Sbill 
2971496Sbill 		splitrand(p);
2981496Sbill 		if (regs[RT2][0] != '$') goto std;
2991496Sbill 		if ((flen = getnum(&regs[RT2][1])) < 0) goto std;
3001496Sbill 		if (flen == 1)
3011496Sbill 		{
3021496Sbill 			register int	extreg;		/* reg extracted to */
3031496Sbill 
3041496Sbill 			extreg = isreg(regs[RT4]);
3051496Sbill 			if (extreg < 0 || extreg >= NUSE) goto std;
3061496Sbill 			if ((pf = p->forw)->op != TST) goto std;
3071496Sbill 			if (uses[extreg] && uses[extreg] != pf) goto std;
3081496Sbill 			splitrand(pf);
3091496Sbill 			if (extreg != isreg(regs[RT1])) goto std;
3101496Sbill 			if ((pn = pf->forw)->op != CBR) goto std;
3111496Sbill 			if (pn->subop != JEQ && pn->subop != JNE) goto std;
3121496Sbill 			delnode(p); delnode(pf);
3131496Sbill 			pn->subop = (pn->subop == JEQ) ? JBC : JBS;
3141496Sbill 			for(cp2=p->code; *cp2++!=',';);
3151496Sbill 			for(cp1=cp2;     *cp1++!=',';);
3161496Sbill 			while (*cp1!=',') *cp2++= *cp1++; *cp2='\0';
3171496Sbill 			pn->code = p->code; pn->pop = NULL;
3181496Sbill 			uses[extreg] = NULL;
3191496Sbill 		}
3201496Sbill 		else
3211496Sbill 		if (flen == 8 || flen == 16)
3221496Sbill 		{
3231496Sbill 			register int	boff;	/* bit offset */
3241496Sbill 			register int	coff;	/* chunk (byte or word) offset*/
3251496Sbill 
3261496Sbill 			if (regs[RT1][0] != '$') goto std;
3271496Sbill 			if ((boff = getnum(&regs[RT1][1])) < 0) goto std;
3281496Sbill 			coff = boff / flen;
3291496Sbill 			if (coff && (isreg(regs[RT3]) >= 0)) goto std;
3301496Sbill 			if (boff < 0 || (boff % flen) != 0) goto std;
3311496Sbill 			p->op = (p->op == EXTV) ? CVT : MOVZ;
3321496Sbill 			p->subop = U((flen == 8 ? BYTE : WORD), LONG);
3331496Sbill 			if (coff == 0)
3341496Sbill 				strcpy(regs[RT1], regs[RT3]);
3351496Sbill 			else
33616133Sralph 				sprintf(regs[RT1], "%d%s%s",
33716133Sralph 					(flen == 8 ? coff : 2*coff),
33816133Sralph 					(regs[RT3][0] == '(' ? "" : "+"),
3391496Sbill 					regs[RT3]);
3401496Sbill 			strcpy(regs[RT2], regs[RT4]);
3411496Sbill 			regs[RT3][0] = '\0'; regs[RT4][0] = '\0';
3421496Sbill 			p->pop = 0; newcode(p);
3431496Sbill 		}
3441496Sbill 		nchange++;
3451496Sbill 		goto std;
3461496Sbill 	}
3471496Sbill 
3481496Sbill 	case CMP:
3491496Sbill 	{
3501496Sbill 		/* comparison to -63 to -1:
3511496Sbill 		**	cmpl	r0,$-1	>	incl	r0
3521496Sbill 		**	jeql	...
3531496Sbill 		**
3541496Sbill 		**	cmpl	r0,$-63	>	addl2	$63,r0
3551496Sbill 		**	jeql	...
3561496Sbill 		*/
3571496Sbill 		register int	num;
3581496Sbill 		register int	reg;
3591496Sbill 		register struct	node	*regp = p->back;
3601496Sbill 
3611496Sbill 		if (p->forw->op != CBR) goto std;
3621496Sbill 		if (p->forw->subop != JEQ && p->forw->subop != JNE) goto std;
3631496Sbill 		splitrand(p);
3641496Sbill 		if (strncmp(regs[RT2], "$-", 2) != 0) goto std;
3651496Sbill 		reg = r = isreg(regs[RT1]);
3661496Sbill 		if (r < 0) goto std;
3671496Sbill 		if (r < NUSE && uses[r] != 0) goto std;
3681496Sbill 		if (r >= NUSE && regp->op == MOV && p->subop == regp->subop)
3691496Sbill 		{
3701496Sbill 			if (*regp->code != 'r') goto std;
3711496Sbill 			reg = regp->code[1] - '0';
3721496Sbill 			if (isdigit(regp->code[2]) || reg >= NUSE || uses[reg])
3731496Sbill 				goto std;
3741496Sbill 		}
3751496Sbill 		if (r >= NUSE) goto std;
3761496Sbill 		if (reg != r)
3771496Sbill 			sprintf(regs[RT1], "r%d", reg);
3781496Sbill 		if ((num = getnum(&regs[RT2][2])) <= 0 || num > 63) goto std;
3791496Sbill 		if (num == 1)
3801496Sbill 		{
3811496Sbill 			p->op = INC; regs[RT2][0] = '\0';
3821496Sbill 		}
3831496Sbill 		else
3841496Sbill 		{
3851496Sbill 			register char	*t;
3861496Sbill 
3871496Sbill 			t=regs[RT1];regs[RT1]=regs[RT2];regs[RT2]=t;
3881496Sbill 			p->op = ADD; p->subop = U(p->subop, OP2);
3891496Sbill 			for (t = &regs[RT1][2]; t[-1] = *t; t++) ;
3901496Sbill 		}
3911496Sbill 		p->pop = 0; newcode(p);
3921496Sbill 		nchange++;
3931496Sbill 		goto std;
3941496Sbill 	}
3951496Sbill 
3961496Sbill 	case JSB:
3971496Sbill 		if (equstr(p->code,"mcount")) {uses[0]=p; regs[0][0]= -1;}
3981496Sbill 		goto std;
3991496Sbill 	case JBR: case JMP:
4001496Sbill 		clearuse();
4011496Sbill 		if (p->subop==RET || p->subop==RSB) {uses[0]=p; regs[0][0]= -1; break;}
4021496Sbill 		if (p->ref==0) goto std;	/* jmp (r0) */
4031496Sbill 		/* fall through */
4041496Sbill 	case CBR:
4051496Sbill 		if (p->ref->ref!=0) for (r=NUSE;--r>=0;)
4061496Sbill 			if (biti[r] & (int)p->ref->ref) {uses[r]=p; regs[r][0]= -1;}
4071496Sbill 	case EROU: case JSW:
4081496Sbill 	case TEXT: case DATA: case BSS: case ALIGN: case WGEN: case END: ;
4091496Sbill 	}
4101496Sbill 	}
4111496Sbill 	for (p= &first; p!=0; p=p->forw)
4121496Sbill 		if (p->op==LABEL || p->op==DLABEL) p->ref=0;	/* erase our tracks */
4131496Sbill }
4141496Sbill 
4151496Sbill rmove()
4161496Sbill {
41718381Sralph 	register struct node *p;
4181496Sbill 	register int r;
4191496Sbill 	int r1;
4201496Sbill 
4211496Sbill 	clearreg();
4221496Sbill 	for (p=first.forw; p!=0; p = p->forw) {
4231496Sbill 	if (debug) {
42417210Sralph 		if (*conloc) {
42517210Sralph 			r1=conval[0];
42617210Sralph 			printf("Con %s = %d%d %s\n", conloc, r1&0xF, r1>>4, conval+1);
42717210Sralph 		}
4281496Sbill 		printf("Regs:\n");
4291496Sbill 		for (r=0; r<NREG; r++)
4301496Sbill 			if (regs[r][0]) {
4311496Sbill 				r1=regs[r][0];
4321496Sbill 				printf("%d: %d%d %s\n", r, r1&0xF, r1>>4, regs[r]+1);
4331496Sbill 			}
4341496Sbill 		printf("-\n");
4351496Sbill 	}
4361496Sbill 	switch (p->op) {
4371496Sbill 
4381496Sbill 	case CVT:
4391496Sbill 		splitrand(p); goto mov;
4401496Sbill 
4411496Sbill 	case MOV:
4421496Sbill 		splitrand(p);
4431496Sbill 		if ((r = findrand(regs[RT1],p->subop)) >= 0) {
4441496Sbill 			if (r == isreg(regs[RT2]) && p->forw->op!=CBR) {
4451496Sbill 				delnode(p); redunm++; break;
4461496Sbill 			}
4471496Sbill 		}
4481496Sbill mov:
4491496Sbill 		repladdr(p);
4501496Sbill 		r = isreg(regs[RT1]);
4511496Sbill 		r1 = isreg(regs[RT2]);
4521496Sbill 		dest(regs[RT2],p->subop);
4531506Sbill 		if (r>=0) {
4541506Sbill 			if (r1>=0) savereg(r1, regs[r]+1, p->subop);
4551506Sbill 			else if (p->op!=CVT) savereg(r, regs[RT2], p->subop);
4561506Sbill 		} else if (r1>=0) savereg(r1, regs[RT1], p->subop);
4571506Sbill 		else if (p->op!=CVT) setcon(regs[RT1], regs[RT2], p->subop);
4581496Sbill 		break;
4591496Sbill 
4601496Sbill /* .rx,.wx */
4611496Sbill 	case MFPR:
4621496Sbill 	case COM:
4631496Sbill 	case NEG:
4641496Sbill /* .rx,.wx or .rx,.rx,.wx */
4651496Sbill 	case ADD:
4661496Sbill 	case SUB:
4671496Sbill 	case BIC:
4681496Sbill 	case BIS:
4691496Sbill 	case XOR:
4701496Sbill 	case MUL:
4711496Sbill 	case DIV:
4721496Sbill 	case ASH:
4731496Sbill 	case MOVZ:
4741496Sbill /* .rx,.rx,.rx,.wx */
4751496Sbill 	case EXTV:
4761496Sbill 	case EXTZV:
4771496Sbill 	case INSV:
4781496Sbill 		splitrand(p);
4791496Sbill 		repladdr(p);
4801496Sbill 		dest(lastrand,p->subop);
4811496Sbill 		if (p->op==INSV) ccloc[0]=0;
4821496Sbill 		break;
4831496Sbill 
4841496Sbill /* .mx or .wx */
4851496Sbill 	case CLR:
4861496Sbill 	case INC:
4871496Sbill 	case DEC:
4881496Sbill 		splitrand(p);
4891496Sbill 		dest(lastrand,p->subop);
4901496Sbill 		if (p->op==CLR)
4911496Sbill 			if ((r = isreg(regs[RT1])) >= 0)
4921496Sbill 				savereg(r, "$0", p->subop);
4931496Sbill 			else
4941496Sbill 				setcon("$0", regs[RT1], p->subop);
4951496Sbill 		break;
4961496Sbill 
4971496Sbill /* .rx */
4981496Sbill 	case TST:
4991496Sbill 	case PUSH:
5001496Sbill 		splitrand(p);
5011496Sbill 		lastrand=regs[RT1+1]; /* fool repladdr into doing 1 operand */
5021496Sbill 		repladdr(p);
5031496Sbill 		if (p->op==TST && equstr(lastrand=regs[RT1], ccloc+1)
5041496Sbill 		  && ((0xf&(ccloc[0]>>4))==p->subop || equtype(ccloc[0],p->subop))
5051496Sbill 		  &&!source(lastrand)) {
5061496Sbill 			delnode(p); p = p->back; nrtst++; nchange++;
5071496Sbill 		}
5081496Sbill 		setcc(lastrand,p->subop);
5091496Sbill 		break;
5101496Sbill 
5111496Sbill /* .rx,.rx,.rx */
5121496Sbill 	case PROBER:
5131496Sbill 	case PROBEW:
5141496Sbill 	case CASE:
5151496Sbill 	case MOVC3:
5161496Sbill /* .rx,.rx */
5171496Sbill 	case MTPR:
5181496Sbill 	case CALLS:
5191496Sbill 	case CMP:
5201496Sbill 	case BIT:
5211496Sbill 		splitrand(p);
5221496Sbill 		/* fool repladdr into doing right number of operands */
5233957Sbugs 		if (p->op==CASE || p->op==PROBER || p->op==PROBEW) lastrand=regs[RT4];
5243958Sbugs /*		else if (p->op==CMPV || p->op==CMPZV) lastrand=regs[RT4+1]; */
5253957Sbugs 		else if (p->op==MOVC3) lastrand=regs[RT1];
5261496Sbill 		else lastrand=regs[RT3];
5271496Sbill 		repladdr(p);
5281496Sbill 		if (p->op==CALLS || p->op==MOVC3) clearreg();
5291496Sbill 		if (p->op==BIT) bitopt(p);
5301496Sbill 		ccloc[0]=0; break;
5311496Sbill 
5321496Sbill 	case CBR:
5331496Sbill 		if (p->subop>=JBC) {
5341496Sbill 			splitrand(p);
5351496Sbill 			if (p->subop<JBCC) lastrand=regs[RT3]; /* 2 operands can be optimized */
5361496Sbill 			else lastrand=regs[RT2]; /* .mb destinations lose */
5371496Sbill 			repladdr(p);
5381496Sbill 		}
5391496Sbill 		ccloc[0] = 0;
5401496Sbill 		break;
5411496Sbill 
5421496Sbill 	case JBR:
5431496Sbill 		redunbr(p);
5441496Sbill 
5451496Sbill /* .wx,.bb */
5461496Sbill 	case SOB:
5471496Sbill 
5481496Sbill 	default:
5491496Sbill 		clearreg();
5501496Sbill 	}
5511496Sbill 	}
5521496Sbill }
5531496Sbill 
5541496Sbill char *
5551496Sbill byondrd(p) register struct node *p; {
5561496Sbill /* return pointer to register which is "beyond last read/modify operand" */
5571496Sbill 	if (OP2==(p->subop>>4)) return(regs[RT3]);
5581496Sbill 	switch (p->op) {
5591496Sbill 		case MFPR:
5601496Sbill 		case JSB:
5611496Sbill 		case PUSHA:
5621496Sbill 		case TST: case INC: case DEC: case PUSH:	return(regs[RT2]);
5631496Sbill 		case MTPR:
5641496Sbill 		case BIT: case CMP: case CALLS:				return(regs[RT3]);
5651496Sbill 		case PROBER: case PROBEW:
5661496Sbill 		case CASE: case MOVC3:						return(regs[RT4]);
5671496Sbill 	}
5681496Sbill 	return(lastrand);
5691496Sbill }
5701496Sbill 
5711496Sbill struct node *
5721496Sbill bflow(p)
5731496Sbill register struct node *p;
5741496Sbill {
5751496Sbill 	register char *cp1,*cp2,**preg; register int r;
5761496Sbill 	int flow= -1;
5771496Sbill 	struct node *olduse=0;
5781496Sbill 	splitrand(p);
5791496Sbill 	if (p->op!=PUSH && p->subop && 0<=(r=isreg(lastrand)) && r<NUSE && uses[r]==p->forw) {
58032986Sdonn 	if ((p->op==CVT || p->op==MOVZ)
58132986Sdonn 		&& (p->forw->op==CVT || p->forw->op==MOVZ)
58232986Sdonn 		&& p->forw->subop&0xf
58332987Sdonn 		&& equtype(p->subop,p->forw->subop)
58432986Sdonn 		&& !source(cp1=regs[RT1])
58532986Sdonn 		&& !indexa(cp1)) goto movit;
5861496Sbill 	if (equtype(p->subop,regs[r][0])
5871496Sbill 	|| ((p->op==CVT || p->op==MOVZ)
5881496Sbill 			 && 0xf&regs[r][0] && compat(0xf&(p->subop>>4),regs[r][0]))) {
5891496Sbill 		register int r2;
5901496Sbill 		if (regs[r][1]!=0) {/* send directly to destination */
5911496Sbill 			if (p->op==INC || p->op==DEC) {
5921496Sbill 				if (p->op==DEC) p->op=SUB; else p->op=ADD;
5931496Sbill 				p->subop=(OP2<<4)+(p->subop&0xF); /* use 2 now, convert to 3 later */
5941496Sbill 				p->pop=0;
5951496Sbill 				cp1=lastrand; cp2=regs[RT2]; while (*cp2++= *cp1++); /* copy reg */
5961496Sbill 				cp1=lastrand; *cp1++='$'; *cp1++='1'; *cp1=0;
5971496Sbill 			}
5981496Sbill 			cp1=regs[r]+1; cp2=lastrand;
5991496Sbill 			if (OP2==(p->subop>>4)) {/* use 3 operand form of instruction */
6001496Sbill 				p->pop=0;
6011496Sbill 				p->subop += (OP3-OP2)<<4; lastrand=cp2=regs[RT3];
6021496Sbill 			}
6031496Sbill 			while (*cp2++= *cp1++);
6041496Sbill 			if (p->op==MOVA && p->forw->op==PUSH) {
6051496Sbill 				p->op=PUSHA; *regs[RT2]=0; p->pop=0;
6061496Sbill 			} else if (p->op==MOV && p->forw->op==PUSH) {
6071496Sbill 				p->op=PUSH ; *regs[RT2]=0; p->pop=0;
6081496Sbill 			}
6091496Sbill 			delnode(p->forw);
6101496Sbill 			if (0<=(r2=isreg(lastrand)) && r2<NUSE) {
6111496Sbill 				uses[r2]=uses[r]; uses[r]=0;
6121496Sbill 			}
61318381Sralph 			(void) redun3(p,0);
6141496Sbill 			newcode(p); redunm++; flow=r;
6151496Sbill 		} else if (p->op==MOV && p->forw->op!=EXTV && p->forw->op!=EXTZV) {
6161496Sbill 			/* superfluous fetch */
6171496Sbill 			int nmatch;
6182829Stoy 			char src[C2_ASIZE];
6191496Sbill 	movit:
6201496Sbill 			cp2=src; cp1=regs[RT1]; while (*cp2++= *cp1++);
6211496Sbill 			splitrand(p->forw);
6221496Sbill 			if (p->forw->op != INC && p->forw->op != DEC)
6231496Sbill 				lastrand=byondrd(p->forw);
6241496Sbill 			nmatch=0;
6251496Sbill 			for (preg=regs+RT1;*preg!=lastrand;preg++)
6261496Sbill 				if (r==isreg(*preg)) {
6271496Sbill 				cp2= *preg; cp1=src; while (*cp2++= *cp1++); ++nmatch;
6281496Sbill 			}
6291496Sbill 			if (nmatch==1) {
6301496Sbill 				if (OP2==(p->forw->subop>>4) && equstr(src,regs[RT2])) {
6311496Sbill 					p->forw->pop=0;
6321496Sbill 					p->forw->subop += (OP3-OP2)<<4; cp1=regs[RT3];
6331496Sbill 					*cp1++='r'; *cp1++=r+'0'; *cp1=0;
6341496Sbill 				}
6351496Sbill 				delnode(p); p=p->forw;
6361496Sbill 				if (0<=(r2=isreg(src)) && r2<NUSE) {
6371496Sbill 					uses[r2]=uses[r]; uses[r]=0;
6381496Sbill 				}
63918381Sralph 				(void) redun3(p,0);
640*33360Sdonn 				newcode(p); redunm++;
641*33360Sdonn 				return(p);	/* avoid stale uses[] data */
6421496Sbill 			} else splitrand(p);
6431496Sbill 		}
6441496Sbill 	} else if (p->op==MOV && (p->forw->op==CVT || p->forw->op==MOVZ)
6451496Sbill 		&& p->forw->subop&0xf 	/* if base or index, then forget it */
6461496Sbill 		&& compat(p->subop,p->forw->subop) && !source(cp1=regs[RT1])
6471496Sbill 		&& !indexa(cp1)) goto movit;
6481496Sbill 	}
6491496Sbill 	/* adjust 'lastrand' past any 'read' or 'modify' operands. */
6501496Sbill 	lastrand=byondrd(p);
6511496Sbill 	/* a 'write' clobbers the register. */
6521496Sbill 	if (0<=(r=isreg(lastrand)) && r<NUSE
6531496Sbill 	|| OP2==(p->subop>>4) && 0<=(r=isreg(regs[RT2])) && r<NUSE && uses[r]==0) {
6541496Sbill 		/* writing a dead register is useless, but watch side effects */
6551496Sbill 		switch (p->op) {
6561498Sbill 		case ACB:
6571496Sbill 		case AOBLEQ: case AOBLSS: case SOBGTR: case SOBGEQ: break;
6581498Sbill 		default:
6591496Sbill 			if (uses[r]==0) {/* no direct uses, check for use of condition codes */
6601496Sbill 				register struct node *q=p;
6611496Sbill 				while ((q=nonlab(q->forw))->combop==JBR) q=q->ref;	/* cc unused, unchanged */
6621496Sbill 				if (q->op!=CBR) {/* ... and destroyed */
6631496Sbill 					preg=regs+RT1;
6641496Sbill 					while (cp1= *preg++) {
6651496Sbill 						if (cp1==lastrand) {redunm++; delnode(p); return(p->forw);}
6661496Sbill 						if (source(cp1) || equstr(cp1,lastrand)) break;
6671496Sbill 					}
6681496Sbill 				}
6691496Sbill 			}
6701496Sbill 			flow=r;
6711496Sbill 		}
6721496Sbill 	}
67318381Sralph 	if (0<=(r=flow)) {
67418381Sralph 		olduse=uses[r];
67518381Sralph 		uses[r]=0;
67618381Sralph 		regs[r][0]=regs[r][1]=0;
67718381Sralph 	}
6781496Sbill 		/* these two are here, rather than in bmove(),
6791496Sbill 		/* because I decided that it was better to go for 3-address code
6801496Sbill 		/* (save time) rather than fancy jbxx (save 1 byte)
6811496Sbill 		/* on sequences like  bisl2 $64,r0; movl r0,foo
6821496Sbill 		*/
6831496Sbill 	if (p->op==BIC) {p=bicopt(p); splitrand(p); lastrand=byondrd(p);}
68418381Sralph 	if (p->op==BIS) {(void) bixprep(p,JBSS);    lastrand=byondrd(p);}
6851496Sbill 	/* now look for 'read' or 'modify' (read & write) uses */
6861496Sbill 	preg=regs+RT1;
6871496Sbill 	while (*(cp1= *preg++)) {
6881496Sbill 		/* check for  r  */
6891496Sbill 		if (lastrand!=cp1 && 0<=(r=isreg(cp1)) && r<NUSE && uses[r]==0) {
6901496Sbill 			uses[r]=p; cp2=regs[r]; *cp2++=p->subop;
6911506Sbill 			if (p->op==ASH && preg==(regs+RT1+1)) cp2[-1]=BYTE; /* stupid DEC */
6921496Sbill 			if (p->op==MOV || p->op==PUSH || p->op==CVT || p->op==MOVZ || p->op==COM || p->op==NEG) {
6931496Sbill 				if (p->op==PUSH) cp1="-(sp)";
6941496Sbill 				else {
6951496Sbill 					cp1=regs[RT2];
6961496Sbill 					if (0<=(r=isreg(cp1)) && r<NUSE && uses[r]==0)
6971496Sbill 						uses[r]=olduse; /* reincarnation!! */
6981496Sbill 					/* as in  addl2 r0,r1;  movl r1,r0;  ret  */
6991496Sbill 					if (p->op!=MOV) cp1=0;
7001496Sbill 				}
7011496Sbill 				if (cp1) while (*cp2++= *cp1++);
7021506Sbill 				else *cp2=0;
7031506Sbill 			} else *cp2=0;
7041496Sbill 			continue;
7051496Sbill 		}
7061496Sbill 		/* check for (r),(r)+,-(r),[r] */
7071496Sbill 		do if (*cp1=='(' || *cp1=='[') {/* get register number */
7081496Sbill 			char t;
7091496Sbill 			cp2= ++cp1; while (*++cp1!=')' && *cp1!=']'); t= *cp1; *cp1=0;
7101496Sbill 			if (0<=(r=isreg(cp2)) && r<NUSE && (uses[r]==0 || uses[r]==p)) {
7111496Sbill 				uses[r]=p; regs[r][0]=(*--cp2=='[' ? OPX<<4 : OPB<<4);
7121496Sbill 			}
7131496Sbill 			*cp1=t;
7141496Sbill 		} while (*++cp1);
7151496Sbill 	}
7161496Sbill 	/* pushax or movax possibility? */
7171496Sbill 	cp1=regs[RT1];
7181496Sbill 	if (*cp1++=='$' && isstatic(cp1) && natural(regs[RT1])) {
7191496Sbill 		if (p->combop==T(MOV,LONG)) {
7201496Sbill 			if (regs[RT1][1]=='L' && 0!=(p->labno=getnum(regs[RT1]+2))) {
7211496Sbill 				cp1=p->code; while (*cp1++!=','); p->code= --cp1;
7221496Sbill 			}
7231496Sbill 			p->combop=T(MOVA,LONG); ++p->code; p->pop=0;
7241496Sbill 		} else if (p->combop==T(PUSH,LONG)) {
7251496Sbill 			p->combop=T(PUSHA,LONG); ++p->code; p->pop=0;
7261496Sbill 		} else if ((p->combop&0xFFFF)==T(ADD,U(LONG,OP3))
7271496Sbill 				 && 0<=(r=isreg(regs[RT2]))) {
7281496Sbill 			cp1=cp2=p->code; ++cp1;
7291496Sbill 			do *cp2++= *cp1; while (*cp1++!=','); cp2[-1]='[';
7301496Sbill 			do *cp2++= *cp1; while (*cp1++!=','); cp2[-1]=']';
7311496Sbill 			if (!equstr(regs[RT3],"-(sp)")) p->combop=T(MOVA,BYTE);
7321496Sbill 			else {p->combop=T(PUSHA,BYTE); *cp2=0;}
73324399Smckusick 			if (r < NUSE && uses[r] == 0) {
73424399Smckusick 				uses[r]=p;
73524399Smckusick 				regs[r][0]=OPX<<4;
73624399Smckusick 			}
7371496Sbill 			p->pop=0;
7381496Sbill 		}
7391496Sbill 	}
7401496Sbill 	return(p);
7411496Sbill }
7421496Sbill 
7431496Sbill ispow2(n) register long n; {/* -1 -> no; else -> log to base 2 */
7441496Sbill 	register int log;
7451496Sbill 	if (n==0 || n&(n-1)) return(-1); log=0;
7461496Sbill 	for (;;) {n >>= 1; if (n==0) return(log); ++log; if (n== -1) return(log);}
7471496Sbill }
7481496Sbill 
7491496Sbill bitopt(p) register struct node *p; {
7501496Sbill 	/* change "bitx $<power_of_2>,a" followed by JEQ or JNE
7511496Sbill 	/* into JBC or JBS.  watch out for I/O registers. (?)
7521496Sbill 	/* assumes that 'splitrand' has already been called.
7531496Sbill 	*/
7541496Sbill 	register char *cp1,*cp2; int b;
7551496Sbill 	cp1=regs[RT1]; cp2=regs[RT2];
7561496Sbill 	if (*cp1++!='$' || !okio(cp2) || p->forw->op!=CBR || p->forw->subop&-2 ||
75727461Sdonn 		0>(b=ispow2(getnum(cp1)))) return;
75827461Sdonn 	if (p->subop!=BYTE && !(b==0 && p->subop==LONG) &&
75927461Sdonn 		(source(cp2) || indexa(cp2))) return;
7601496Sbill 	if (b>=bitsize[p->subop]) {/* you dummy! */
7611496Sbill 		if (source(cp2)) {/* side effect: auto increment or decrement */
7621496Sbill 			p->pop=0;
7631496Sbill 			p->op=TST; --cp1; while (*cp1++= *cp2++);
7641496Sbill 			regs[RT2][0]=0; newcode(p);
7651496Sbill 		} else delnode(p);
7661496Sbill 		p = p->forw;
7671496Sbill 		if (p->subop==JEQ) {p->combop=JBR; p->pop=0;}
7681496Sbill 		else delnode(p);
7691496Sbill 		nchange++; nbj++; return;
7701496Sbill 	}
7711496Sbill 	if (cp1=p->forw->code) {/* destination is not an internal label */
7721496Sbill 		cp2=regs[RT3]; while (*cp2++= *cp1++);
7731496Sbill 	}
77427461Sdonn 	if (b==0 && (p->subop==LONG || !(source(regs[RT2]) || indexa(regs[RT2])))) {
77527461Sdonn 		/* JLB optimization, ala BLISS */
7761496Sbill 		cp2=regs[RT1]; cp1=regs[RT2]; while (*cp2++= *cp1++);
7771496Sbill 		cp2=regs[RT2]; cp1=regs[RT3]; while (*cp2++= *cp1++);
7781496Sbill 		*(regs[RT3])=0; p->forw->subop += JLBC-JBC;
7791496Sbill 		p->forw->pop=0;
7801496Sbill 	} else {
7811496Sbill 		cp1=regs[RT1]+1;
7821496Sbill 		if (b>9) *cp1++= b/10 +'0'; *cp1++= b%10 +'0'; *cp1=0; /* $<bit_number> */
7831496Sbill 	}
7841496Sbill 	nbj++; newcode(p); p->combop = p->forw->combop+((JBC-JEQ)<<8);
7851496Sbill 	p->labno = p->forw->labno; delnode(p->forw);
7861496Sbill 	p->pop=0;
7871496Sbill }
7881496Sbill 
7891496Sbill isfield(n) register long n; {/* -1 -> no; else -> position of low bit */
79018381Sralph 	register int p; register long t;
7911496Sbill 	t= ((n-1)|n) +1;
79216832Sralph 	if (n!=0 && (0==t || 0<=ispow2(t))) {
79318381Sralph 		p=0; while(!(n&1)) {n >>= 1; ++p;} return(p);
7941496Sbill 	} else return(-1);
7951496Sbill }
7961496Sbill 
7971496Sbill bixprep(p,bix) register struct node *p; {
7981496Sbill /* initial setup, single-bit checking for bisopt, bicopt.
7991496Sbill /* return: 0->don't bother any more; 1->worthwhile trying
8001496Sbill */
8011496Sbill 	register char *cp1,*cp2;
8021496Sbill 	splitrand(p); cp1=regs[RT1]; cp2=regs[RT2];
8031496Sbill 	if (*cp1++!='$' || 0>(pos=isfield(f=getnum(cp1)))
8041496Sbill 	  || !okio(cp2) || indexa(cp2) || source(cp2) || !okio(lastrand)) return(0);
8051496Sbill 	f |= f-1; if (++f==0) siz=32-pos; else siz=ispow2(f)-pos;
8061496Sbill 	if (siz==1 && pos>5 && (p->subop>>4)==OP2 && (p->subop&0xF)!=BYTE
8071496Sbill 	  && pos<bitsize[p->subop&0xF]) {
8081496Sbill 		p->ref = insertl(p->forw); p->combop = CBR | (bix<<8);
8091496Sbill 		p->pop=0;
8101496Sbill 		p->labno = p->ref->labno;
8111496Sbill 		if (pos>9) {*cp1++= pos/10 +'0'; pos %= 10;}
8121496Sbill 		*cp1++=pos+'0'; *cp1=0; newcode(p); nbj++; return(0);
8131496Sbill 	}
8141496Sbill 	return(1);
8151496Sbill }
8161496Sbill 
8171496Sbill 
8181496Sbill struct node *
8191496Sbill bicopt(p) register struct node *p; {
8201496Sbill /* use field operations or MOVZ if possible.  done as part of 'bflow'.
8211496Sbill */
8221496Sbill 	register char *cp1,*cp2; int r;
8233933Sroot 	char src[C2_ASIZE];
82418381Sralph 	char lhssiz, sop;
8251496Sbill 	if (!bixprep(p,JBCC)) return(p);
8261496Sbill 	if (f==0) {/* the BIC isolates low order bits */
8271496Sbill 		siz=pos; pos=0;
8281496Sbill 		if ((p->subop&0xF)==LONG && *(regs[RT2])!='$') {/* result of EXTZV is long */
8291496Sbill 			/* save source of BICL in 'src' */
8301496Sbill 			cp1=regs[RT2]; cp2=src; while (*cp2++= *cp1++);
8311496Sbill 			if (p->back->op==ASH) {/* try for more */
8321496Sbill 				splitrand(p->back); cp1=regs[RT1]; cp2=regs[RT3];
8331496Sbill 				if (*cp1++=='$' && *(regs[RT2])!='$' && !indexa(regs[RT2])
8341496Sbill 				  && 0>(f=getnum(cp1)) && equstr(src,cp2)
83516832Sralph 				  && 0<=(r=isreg(cp2)) && r<NUSE
83616832Sralph 				  && siz-f <= 32) {	/* a good ASH */
8371496Sbill 					pos -= f; cp1=regs[RT2]; cp2=src; while (*cp2++= *cp1++);
8381496Sbill 					delnode(p->back);
8391496Sbill 				}
8401496Sbill 			}
84116133Sralph 			/*
84216133Sralph 			 * 'pos', 'siz' known; find out the size of the
84316133Sralph 			 * left-hand operand of what the bicl will turn into.
84416133Sralph 			 */
84516133Sralph 			if (pos==0 && siz==16)
84616133Sralph 				lhssiz = WORD;	/* movzwl */
84716133Sralph 			else
84816133Sralph 				lhssiz = BYTE;	/* movzbl or extzvl */
8491496Sbill 			if (p->back->op==CVT || p->back->op==MOVZ) {/* greedy, aren't we? */
8501496Sbill 				splitrand(p->back); cp1=regs[RT1]; cp2=regs[RT2];
85116133Sralph 				/*
85216133Sralph 				 * If indexa(cp1) || autoid(cp1), the fold may
85316133Sralph 				 * still be OK if the CVT/MOVZ has the same
85416133Sralph 				 * size operand on its left size as what we
85516133Sralph 				 * will turn the bicl into.
85616133Sralph 				 * However, if the CVT is from a float or
85716133Sralph 				 * double, forget it!
85816133Sralph 				 */
85918381Sralph 				sop = p->back->subop&0xF;	/* type of LHS of CVT/MOVZ */
86016133Sralph 				if (equstr(src,cp2) && okio(cp1)
86118381Sralph 				  && sop != FFLOAT && sop != DFLOAT
86218381Sralph 				  && sop != GFLOAT && sop != HFLOAT
86318381Sralph 				  && ((!indexa(cp1) && !autoid(cp1)) || lhssiz == sop)
8641496Sbill 				  && 0<=(r=isreg(cp2)) && r<NUSE
86518381Sralph 				  && bitsize[sop]>=(pos+siz)
8661496Sbill 				  && bitsize[p->back->subop>>4]>=(pos+siz)) {/* good CVT */
8671496Sbill 					cp1=regs[RT1]; cp2=src; while (*cp2++= *cp1++);
8681496Sbill 					delnode(p->back);
8691496Sbill 				}
8701496Sbill 			}
8711496Sbill 			/* 'pos', 'siz' known; source of field is in 'src' */
8721496Sbill 			splitrand(p); /* retrieve destination of BICL */
87316133Sralph 			if ((siz==8 || siz==16) && pos==0) {
87416133Sralph 				p->combop = T(MOVZ,U(lhssiz,LONG));
8751496Sbill 				sprintf(line,"%s,%s",src,lastrand);
8761496Sbill 			} else {
8771496Sbill 				p->combop = T(EXTZV,LONG);
8781496Sbill 				sprintf(line,"$%d,$%d,%s,%s",pos,siz,src,lastrand);
8791496Sbill 			}
8801496Sbill 			p->pop=0;
8811496Sbill 			p->code = copy(line); nfield++; return(p);
8821496Sbill 		}/* end EXTZV possibility */
8831496Sbill 	}/* end low order bits */
8841496Sbill /* unfortunately, INSV clears the condition codes, thus cannot be used */
8851496Sbill /*	else {/* see if BICL2 of positive field should be INSV $0 */
8861496Sbill /*		if (p->subop==(LONG | (OP2<<4)) && 6<=(pos+siz)) {
8871496Sbill /*			p->combop = INSV;
8881496Sbill /*			sprintf(line,"$0,$%d,$%d,%s",pos,siz,lastrand);
8891496Sbill /*			p->code = copy(line); nfield++; return(p);
8901496Sbill /*		}
8911496Sbill /*	}
8921496Sbill */
8931496Sbill 	return(p);
8941496Sbill }
8951496Sbill 
8961496Sbill jumpsw()
8971496Sbill {
8981496Sbill 	register struct node *p, *p1;
89918381Sralph 	register struct node *tp;
90018381Sralph 	long tl;
90118381Sralph 	char *tcp;
90218381Sralph 	int ti;
9031496Sbill 	int nj;
9041496Sbill 
90518381Sralph 	ti = 0;
9061496Sbill 	nj = 0;
9071496Sbill 	for (p=first.forw; p!=0; p = p->forw)
90818381Sralph 		p->seq = ++ti;
9091496Sbill 	for (p=first.forw; p!=0; p = p1) {
9101496Sbill 		p1 = p->forw;
9111496Sbill 		if (p->op == CBR && p1->op==JBR && p->ref && p1->ref
9121496Sbill 		 && abs(p->seq - p->ref->seq) > abs(p1->seq - p1->ref->seq)) {
9131496Sbill 			if (p->ref==p1->ref)
9141496Sbill 				continue;
9151496Sbill 			p->subop = revbr[p->subop];
9161496Sbill 			p->pop=0;
91718381Sralph 			tp = p1->ref;
9181496Sbill 			p1->ref = p->ref;
91918381Sralph 			p->ref = tp;
92018381Sralph 			tl = p1->labno;
9211496Sbill 			p1->labno = p->labno;
92218381Sralph 			p->labno = tl;
9231496Sbill #ifdef COPYCODE
9241496Sbill 			if (p->labno == 0) {
92518381Sralph 				tcp = p1->code;
92618381Sralph 				p1->code = p->code;
92718381Sralph 				p->code = tcp;
9281496Sbill 			}
9291496Sbill #endif
9301496Sbill 			nrevbr++;
9311496Sbill 			nj++;
9321496Sbill 		}
9331496Sbill 	}
9341496Sbill 	return(nj);
9351496Sbill }
9361496Sbill 
9371496Sbill addsob()
9381496Sbill {
9391496Sbill 	register struct node *p, *p1, *p2, *p3;
9401496Sbill 
9411496Sbill 	for (p = &first; (p1 = p->forw)!=0; p = p1) {
9421496Sbill 	if (p->combop==T(DEC,LONG) && p1->op==CBR) {
9433933Sroot 		if (abs(p->seq - p1->ref->seq) > 8) continue;
9441496Sbill 		if (p1->subop==JGE || p1->subop==JGT) {
9451496Sbill 			if (p1->subop==JGE) p->combop=SOBGEQ; else p->combop=SOBGTR;
9461496Sbill 			p->pop=0;
9471496Sbill 			p->labno = p1->labno; delnode(p1); nsob++;
9481496Sbill 		}
9491496Sbill 	} else if (p->combop==T(INC,LONG)) {
9501496Sbill 		if (p1->op==LABEL && p1->refc==1 && p1->forw->combop==T(CMP,LONG)
9511496Sbill 		  && (p2=p1->forw->forw)->combop==T(CBR,JLE)
9521496Sbill 		  && (p3=p2->ref->back)->combop==JBR && p3->ref==p1
9531496Sbill 		  && p3->forw->op==LABEL && p3->forw==p2->ref) {
9541496Sbill 			/* change	INC LAB: CMP	to	LAB: INC CMP */
9551496Sbill 			p->back->forw=p1; p1->back=p->back;
9561496Sbill 			p->forw=p1->forw; p1->forw->back=p;
9571496Sbill 			p->back=p1; p1->forw=p;
9581496Sbill 			p1=p->forw;
9591496Sbill 			/* adjust beginning value by 1 */
9601496Sbill 				p2=alloc(sizeof first); p2->combop=T(DEC,LONG);
9611496Sbill 				p2->pop=0;
9621496Sbill 				p2->forw=p3; p2->back=p3->back; p3->back->forw=p2;
9631496Sbill 				p3->back=p2; p2->code=p->code; p2->labno=0;
9641496Sbill 		}
9651496Sbill 		if (p1->combop==T(CMP,LONG) && (p2=p1->forw)->op==CBR) {
9661496Sbill 			register char *cp1,*cp2;
9671496Sbill 			splitrand(p1); if (!equstr(p->code,regs[RT1])) continue;
9683933Sroot 			if (abs(p->seq - p2->ref->seq)>8) {/* outside byte displ range */
9691496Sbill 				if (p2->subop!=JLE) continue;
9701496Sbill 				p->combop=T(ACB,LONG);
9711496Sbill 				cp2=regs[RT1]; cp1=regs[RT2]; while (*cp2++= *cp1++); /* limit */
9721496Sbill 				cp2=regs[RT2]; cp1="$1"; while (*cp2++= *cp1++); /* increment */
9731496Sbill 				cp2=regs[RT3]; cp1=p->code; while (*cp2++= *cp1++); /* index */
9741496Sbill 				p->pop=0; newcode(p);
9751496Sbill 				p->labno = p2->labno; delnode(p2); delnode(p1); nsob++;
9761496Sbill 			} else if (p2->subop==JLE || p2->subop==JLT) {
9771496Sbill 				if (p2->subop==JLE) p->combop=AOBLEQ; else p->combop=AOBLSS;
9781496Sbill 				cp2=regs[RT1]; cp1=regs[RT2]; while (*cp2++= *cp1++); /* limit */
9791496Sbill 				cp2=regs[RT2]; cp1=p->code; while (*cp2++= *cp1++); /* index */
9801496Sbill 				p->pop=0; newcode(p);
9811496Sbill 				p->labno = p2->labno; delnode(p2); delnode(p1); nsob++;
9821496Sbill 			}
9831496Sbill 		}
9841496Sbill 	}
9851496Sbill 	}
9861496Sbill }
9871496Sbill 
9881496Sbill equop(p1, p2)
9891496Sbill register struct node *p1;
9901496Sbill struct node *p2;
9911496Sbill {
9921496Sbill 	register char *cp1, *cp2;
9931496Sbill 
9941496Sbill 	if (p1->combop != p2->combop)
9951496Sbill 		return(0);
99632985Sdonn 	if (p1->combop == 0 && p1->pop != p2->pop)
99732985Sdonn 		return(0);
9981496Sbill 	if (p1->op>0 && p1->op<MOV)
9991496Sbill 		return(0);
100018381Sralph 	switch (p1->combop) {
100118381Sralph 	case EROU:	case JSW:	case TEXT:	case DATA:
100218381Sralph 	case BSS:	case ALIGN:	case WGEN:	case END:
100318381Sralph 		/*
100418381Sralph 		 * Consider all pseudo-ops to be unique.
100518381Sralph 		 */
100618381Sralph 		return(0);
100718381Sralph 	}
10081496Sbill 	if (p1->op==MOVA && p1->labno!=p2->labno) return(0);
10091496Sbill 	cp1 = p1->code;
10101496Sbill 	cp2 = p2->code;
10111496Sbill 	if (cp1==0 && cp2==0)
10121496Sbill 		return(1);
10131496Sbill 	if (cp1==0 || cp2==0)
10141496Sbill 		return(0);
10151496Sbill 	while (*cp1 == *cp2++)
10161496Sbill 		if (*cp1++ == 0)
10171496Sbill 			return(1);
10181496Sbill 	return(0);
10191496Sbill }
10201496Sbill 
102118381Sralph #ifndef delnode
10221496Sbill delnode(p) register struct node *p; {
10231496Sbill 	p->back->forw = p->forw;
10241496Sbill 	p->forw->back = p->back;
10251496Sbill }
102618381Sralph #endif
10271496Sbill 
102818381Sralph #ifndef decref
10291496Sbill decref(p)
10301496Sbill register struct node *p;
10311496Sbill {
10321496Sbill 	if (p && --p->refc <= 0) {
10331496Sbill 		nrlab++;
10341496Sbill 		delnode(p);
10351496Sbill 	}
10361496Sbill }
103718381Sralph #endif
10381496Sbill 
10391496Sbill struct node *
10401496Sbill nonlab(ap)
10411496Sbill struct node *ap;
10421496Sbill {
10431496Sbill 	register struct node *p;
10441496Sbill 
10451496Sbill 	p = ap;
10461496Sbill 	while (p && p->op==LABEL)
10471496Sbill 		p = p->forw;
10481496Sbill 	return(p);
10491496Sbill }
10501496Sbill 
10511496Sbill clearuse() {
10521496Sbill 	register struct node **i;
10531496Sbill 	for (i=uses+NUSE; i>uses;) *--i=0;
10541496Sbill }
10551496Sbill 
10561496Sbill clearreg() {
105718381Sralph 	register char **i;
105818381Sralph 	for (i=regs; i<regs+NREG; ++i) {
105918381Sralph 		**i = 0;
106018381Sralph 		*(*i+1) = 0;
106118381Sralph 	}
106218381Sralph 	conloc[0] = 0;
106318381Sralph 	ccloc[0] = 0;
10641496Sbill }
10651496Sbill 
10661496Sbill savereg(ai, s, type)
10671496Sbill register char *s;
10681496Sbill {
10691496Sbill 	register char *p, *sp;
10701496Sbill 
10711496Sbill 	sp = p = regs[ai];
10721496Sbill 	if (source(s)) /* side effects in addressing */
10731496Sbill 		return;
10741496Sbill 	/* if any indexing, must be parameter or local */
10751496Sbill 	/* indirection (as in "*-4(fp)") is ok, however */
10761496Sbill 	*p++ = type;
10771496Sbill 	while (*p++ = *s)
10781496Sbill 		if (*s=='[' || *s++=='(' && *s!='a' && *s!='f') {*sp = 0; return;}
10791496Sbill }
10801496Sbill 
10811496Sbill dest(s,type)
10821496Sbill register char *s;
10831496Sbill {
10841496Sbill 	register int i;
10851496Sbill 
108618381Sralph 	(void) source(s); /* handle addressing side effects */
108717210Sralph 	if (!natural(s)) {
108817210Sralph 		/* wild store, everything except constants vanishes */
108917210Sralph 		for (i=NREG; --i>=0;)
109018381Sralph 			if (regs[i][1] != '$')
109118381Sralph 				regs[i][0] = regs[i][1] = 0;
109217210Sralph 		conloc[0] = 0; ccloc[0] = 0;
109317210Sralph 		return;
109417210Sralph 	}
10951496Sbill 	if ((i = isreg(s)) >= 0) {
109618381Sralph 		/* if register destination, that reg is a goner */
109718381Sralph 		regs[i][0] = regs[i][1] = 0;
10985794Sroot 		switch(type & 0xF){
10995794Sroot 		case DFLOAT:	/* clobber two at once */
11005794Sroot 			/*FALLTHROUGH*/
11015794Sroot 		case GFLOAT:
110218381Sralph 			regs[i+1][0] = regs[i+1][1] = 0;
11035794Sroot 			break;
11045794Sroot 		case HFLOAT:	/* clobber four at once */
110518381Sralph 			regs[i+1][0] = regs[i+1][1] = 0;
110618381Sralph 			regs[i+2][0] = regs[i+2][1] = 0;
110718381Sralph 			regs[i+3][0] = regs[i+3][1] = 0;
11085794Sroot 			break;
11095794Sroot 		}
11105794Sroot 		switch((type>>4)&0xF){
11115794Sroot 		case DFLOAT:	/* clobber two at once */
11125794Sroot 			/*FALLTHROUGH*/
11135794Sroot 		case GFLOAT:
111418381Sralph 			regs[i+1][0] = regs[i+1][1] = 0;
11155794Sroot 			break;
11165794Sroot 		case HFLOAT:	/* clobber four at once */
111718381Sralph 			regs[i+1][0] = regs[i+1][1] = 0;
111818381Sralph 			regs[i+2][0] = regs[i+2][1] = 0;
111918381Sralph 			regs[i+3][0] = regs[i+3][1] = 0;
11205794Sroot 			break;
11215794Sroot 		}
11221496Sbill 	}
11231496Sbill 	for (i=NREG; --i>=0;)
11241496Sbill 		if (regs[i][1]=='*' && equstr(s, regs[i]+2))
112518381Sralph 			regs[i][0] = regs[i][1] = 0; /* previous indirection through destination is invalid */
11261496Sbill 	while ((i = findrand(s,0)) >= 0) /* previous values of destination are invalid */
112718381Sralph 		regs[i][0] = regs[i][1] = 0;
112817210Sralph 	if (*conloc && equstr(conloc, s))
112917210Sralph 		conloc[0] = 0;
113017210Sralph 	setcc(s, type); /* natural destinations set condition codes */
11311496Sbill }
11321496Sbill 
113317210Sralph /* separate operands at commas, set up 'regs' and 'lastrand' */
11341496Sbill splitrand(p) struct node *p; {
113517210Sralph 	register char *p1, *p2;
113617210Sralph 	register char **preg;
113717210Sralph 
113817210Sralph 	preg = regs+RT1;
113917210Sralph 	if (p1 = p->code)
114017210Sralph 		while (*p1) {
114117210Sralph 			lastrand = p2 = *preg++;
114217210Sralph 			while (*p1)
114317210Sralph 				if (',' == (*p2++ = *p1++)) {
114417210Sralph 					--p2;
114517210Sralph 					break;
114617210Sralph 				}
114717210Sralph 			*p2 = 0;
114817210Sralph 		}
114917210Sralph 	while (preg < (regs+RT1+5))
115017210Sralph 		*(*preg++) = 0;
11511496Sbill }
11521496Sbill 
11531496Sbill compat(have, want) {
11541496Sbill register int hsrc, hdst;
11551496Sbill if (0==(want &= 0xF)) return(1); /* anything satisfies a wildcard want */
115626407Sdonn hsrc=have&0xF;
115726407Sdonn if (0==(hdst=((have>>4)&0xF)) || (hdst>=OP2 && hdst<=OPX)) hdst=hsrc;
11585794Sroot if (want>=FFLOAT) return(hdst==want && hsrc==want);
11595794Sroot 	/* FLOAT, DFLOAT not compat: rounding */
11605794Sroot return(hsrc>=want && hdst>=want && hdst<FFLOAT);
11611496Sbill }
11621496Sbill 
11631496Sbill equtype(t1,t2) {return(compat(t1,t2) && compat(t2,t1));}
11641496Sbill 
11651496Sbill findrand(as, type)
11661496Sbill char *as;
11671496Sbill {
11681496Sbill 	register char **i;
11691496Sbill 	for (i = regs+NREG; --i>=regs;) {
11701496Sbill 		if (**i && equstr(*i+1, as) && compat(**i,type))
11711496Sbill 			return(i-regs);
11721496Sbill 	}
11731496Sbill 	return(-1);
11741496Sbill }
11751496Sbill 
11761496Sbill isreg(s)
11771496Sbill register char *s;
11781496Sbill {
11791496Sbill 	if (*s++!='r' || !isdigit(*s++)) return(-1);
11801496Sbill 	if (*s==0) return(*--s-'0');
11811496Sbill 	if (*(s-1)=='1' && isdigit(*s++) && *s==0) return(10+*--s-'0');
11821496Sbill 	return(-1);
11831496Sbill }
11841496Sbill 
11851496Sbill check()
11861496Sbill {
11871496Sbill 	register struct node *p, *lp;
11881496Sbill 
11891496Sbill 	lp = &first;
11901496Sbill 	for (p=first.forw; p!=0; p = p->forw) {
119118381Sralph 		if (p->back != lp) {
119218381Sralph 			fprintf(stderr, "c2: failed internal consistency check -- help!\n");
119318381Sralph 			exit(-1);
119418381Sralph 		}
11951496Sbill 		lp = p;
11961496Sbill 	}
11971496Sbill }
11981496Sbill 
11991496Sbill source(ap)
12001496Sbill char *ap;
12011496Sbill {
12021496Sbill 	register char *p1, *p2;
12031496Sbill 
12041496Sbill 	p1 = ap;
12051496Sbill 	p2 = p1;
12061496Sbill 	if (*p1==0)
12071496Sbill 		return(0);
12081496Sbill 	while (*p2++ && *(p2-1)!='[');
12091496Sbill 	if (*p1=='-' && *(p1+1)=='('
12101496Sbill 	 || *p1=='*' && *(p1+1)=='-' && *(p1+2)=='('
12111496Sbill 	 || *(p2-2)=='+') {
12121496Sbill 		while (*p1 && *p1++!='r');
12131496Sbill 		if (isdigit(*p1++))
121418381Sralph 			if (isdigit(*p1))
121518381Sralph 				regs[10+*p1-'0'][0] = regs[10+*p1-'0'][1] = 0;
121618381Sralph 			else {
121718381Sralph 				--p1;
121818381Sralph 				regs[*p1-'0'][0] = regs[*p1-'0'][1] = 0;
121918381Sralph 			}
12201496Sbill 		return(1);
12211496Sbill 	}
12221496Sbill 	return(0);
12231496Sbill }
12241496Sbill 
12251496Sbill newcode(p) struct node *p; {
12261496Sbill 	register char *p1,*p2,**preg;
12271496Sbill 	preg=regs+RT1; p2=line;
12281496Sbill 	while (*(p1= *preg++)) {while (*p2++= *p1++); *(p2-1)=',';}
12291496Sbill 	*--p2=0;
12301496Sbill 	p->code=copy(line);
12311496Sbill }
12321496Sbill 
12331496Sbill repladdr(p)
12341496Sbill struct node *p;
12351496Sbill {
12361496Sbill 	register r;
123718381Sralph 	register char *p1;
12381496Sbill 	char **preg; int nrepl;
12391496Sbill 
12401496Sbill 	preg=regs+RT1; nrepl=0;
12411496Sbill 	while (lastrand!=(p1= *preg++))
12421496Sbill 		if (!source(p1) && 0<=(r=findrand(p1,p->subop))) {
12431496Sbill 			*p1++='r'; if (r>9) {*p1++='1'; r -= 10;} *p1++=r+'0'; *p1=0;
12441496Sbill 			nrepl++; nsaddr++;
12451496Sbill 		}
12461496Sbill 	if (nrepl) newcode(p);
12471496Sbill }
12481496Sbill 
12491496Sbill /* movedat()
12501496Sbill /* {
12511496Sbill /* 	register struct node *p1, *p2;
12521496Sbill /* 	struct node *p3;
12531496Sbill /* 	register seg;
12541496Sbill /* 	struct node data;
12551496Sbill /* 	struct node *datp;
12561496Sbill /*
12571496Sbill /* 	if (first.forw == 0)
12581496Sbill /* 		return;
12591496Sbill /* 	datp = &data;
12601496Sbill /* 	for (p1 = first.forw; p1!=0; p1 = p1->forw) {
12611496Sbill /* 		if (p1->op == DATA) {
12621496Sbill /* 			p2 = p1->forw;
12631496Sbill /* 			while (p2 && p2->op!=TEXT)
12641496Sbill /* 				p2 = p2->forw;
12651496Sbill /* 			if (p2==0)
12661496Sbill /* 				break;
12671496Sbill /* 			p3 = p1->back;
12681496Sbill /* 			p1->back->forw = p2->forw;
12691496Sbill /* 			p2->forw->back = p3;
12701496Sbill /* 			p2->forw = 0;
12711496Sbill /* 			datp->forw = p1;
12721496Sbill /* 			p1->back = datp;
12731496Sbill /* 			p1 = p3;
12741496Sbill /* 			datp = p2;
12751496Sbill /* 		}
12761496Sbill /* 	}
12771496Sbill /* 	if (data.forw) {
12781496Sbill /* 		datp->forw = first.forw;
12791496Sbill /* 		first.forw->back = datp;
12801496Sbill /* 		data.forw->back = &first;
12811496Sbill /* 		first.forw = data.forw;
12821496Sbill /* 	}
12831496Sbill /* 	seg = -1;
12841496Sbill /* 	for (p1 = first.forw; p1!=0; p1 = p1->forw) {
12851496Sbill /* 		if (p1->op==TEXT||p1->op==DATA||p1->op==BSS) {
12861496Sbill /* 			if (p1->op == seg || p1->forw&&p1->forw->op==seg) {
12871496Sbill /* 				p1->back->forw = p1->forw;
12881496Sbill /* 				p1->forw->back = p1->back;
12891496Sbill /* 				p1 = p1->back;
12901496Sbill /* 				continue;
12911496Sbill /* 			}
12921496Sbill /* 			seg = p1->op;
12931496Sbill /* 		}
12941496Sbill /* 	}
12951496Sbill /* }
12961496Sbill */
12971496Sbill 
12981496Sbill redunbr(p)
12991496Sbill register struct node *p;
13001496Sbill {
13011496Sbill 	register struct node *p1;
13021496Sbill 	register char *ap1;
13031496Sbill 	char *ap2;
13041496Sbill 
13051496Sbill 	if ((p1 = p->ref) == 0)
13061496Sbill 		return;
13071496Sbill 	p1 = nonlab(p1);
13081496Sbill 	if (p1->op==TST) {
13091496Sbill 		splitrand(p1);
13101496Sbill 		savereg(RT2, "$0", p1->subop);
13111496Sbill 	} else if (p1->op==CMP)
13121496Sbill 		splitrand(p1);
13131496Sbill 	else
13141496Sbill 		return;
13151496Sbill 	if (p1->forw->op==CBR) {
13161496Sbill 		ap1 = findcon(RT1, p1->subop);
13171496Sbill 		ap2 = findcon(RT2, p1->subop);
13181496Sbill 		p1 = p1->forw;
13191496Sbill 		if (compare(p1->subop, ap1, ap2)) {
13201496Sbill 			nredunj++;
13211496Sbill 			nchange++;
13221496Sbill 			decref(p->ref);
13231496Sbill 			p->ref = p1->ref;
13241496Sbill 			p->labno = p1->labno;
13251496Sbill #ifdef COPYCODE
13261496Sbill 			if (p->labno == 0)
13271496Sbill 				p->code = p1->code;
132817210Sralph #endif
13291496Sbill 			if (p->ref)
13301496Sbill 				p->ref->refc++;
13311496Sbill 		}
13321496Sbill 	} else if (p1->op==TST && equstr(regs[RT1],ccloc+1) &&
13331496Sbill 			equtype(ccloc[0],p1->subop)) {
13341496Sbill 		p1=insertl(p1->forw); decref(p->ref); p->ref=p1;
13351496Sbill 		nrtst++; nchange++;
13361496Sbill 	}
13371496Sbill }
13381496Sbill 
13391496Sbill char *
13401496Sbill findcon(i, type)
13411496Sbill {
13421496Sbill 	register char *p;
13431496Sbill 	register r;
13441496Sbill 
13451496Sbill 	p = regs[i];
13461496Sbill 	if (*p=='$')
13471496Sbill 		return(p);
13481496Sbill 	if ((r = isreg(p)) >= 0 && compat(regs[r][0],type))
13491496Sbill 		return(regs[r]+1);
13501496Sbill 	if (equstr(p, conloc))
13511496Sbill 		return(conval+1);
13521496Sbill 	return(p);
13531496Sbill }
13541496Sbill 
135518381Sralph compare(opc, acp1, acp2)
13561496Sbill char *acp1, *acp2;
13571496Sbill {
13581496Sbill 	register char *cp1, *cp2;
13591496Sbill 	register n1;
13601496Sbill 	int n2;	int sign;
13611496Sbill 
13621496Sbill 	cp1 = acp1;
13631496Sbill 	cp2 = acp2;
13641496Sbill 	if (*cp1++ != '$' || *cp2++ != '$')
13651496Sbill 		return(0);
13661496Sbill 	n1 = 0; sign=1; if (*cp2=='-') {++cp2; sign= -1;}
13671496Sbill 	while (isdigit(*cp2)) {n1 *= 10; n1 += (*cp2++ - '0')*sign;}
13681496Sbill 	n2 = n1;
13691496Sbill 	n1 = 0; sign=1; if (*cp1=='-') {++cp1; sign= -1;}
13701496Sbill 	while (isdigit(*cp1)) {n1 *= 10; n1 += (*cp1++ - '0')*sign;}
13711496Sbill 	if (*cp1=='+')
13721496Sbill 		cp1++;
13731496Sbill 	if (*cp2=='+')
13741496Sbill 		cp2++;
13751496Sbill 	do {
13761496Sbill 		if (*cp1++ != *cp2)
13771496Sbill 			return(0);
13781496Sbill 	} while (*cp2++);
137918381Sralph 	switch(opc) {
13801496Sbill 
13811496Sbill 	case JEQ:
138218381Sralph 		return(n1 == n2);
13831496Sbill 	case JNE:
138418381Sralph 		return(n1 != n2);
13851496Sbill 	case JLE:
138618381Sralph 		return(n1 <= n2);
13871496Sbill 	case JGE:
138818381Sralph 		return(n1 >= n2);
13891496Sbill 	case JLT:
139018381Sralph 		return(n1 < n2);
13911496Sbill 	case JGT:
139218381Sralph 		return(n1 > n2);
13931496Sbill 	case JLO:
139418381Sralph 		return((unsigned) n1 < (unsigned) n2);
13951496Sbill 	case JHI:
139618381Sralph 		return((unsigned) n1 > (unsigned) n2);
13971496Sbill 	case JLOS:
139818381Sralph 		return((unsigned) n1 <= (unsigned) n2);
13991496Sbill 	case JHIS:
140018381Sralph 		return((unsigned) n1 >= (unsigned) n2);
14011496Sbill 	}
14021496Sbill 	return(0);
14031496Sbill }
14041496Sbill 
14051496Sbill setcon(cv, cl, type)
14061496Sbill register char *cv, *cl;
14071496Sbill {
14081496Sbill 	register char *p;
14091496Sbill 
14101496Sbill 	if (*cv != '$')
14111496Sbill 		return;
14121496Sbill 	if (!natural(cl))
14131496Sbill 		return;
14141496Sbill 	p = conloc;
14151496Sbill 	while (*p++ = *cl++);
14161496Sbill 	p = conval;
14171496Sbill 	*p++ = type;
14181496Sbill 	while (*p++ = *cv++);
14191496Sbill }
14201496Sbill 
14211496Sbill equstr(p1, p2)
14221496Sbill register char *p1, *p2;
14231496Sbill {
14241496Sbill 	do {
14251496Sbill 		if (*p1++ != *p2)
14261496Sbill 			return(0);
14271496Sbill 	} while (*p2++);
14281496Sbill 	return(1);
14291496Sbill }
14301496Sbill 
14311496Sbill setcc(ap,type)
14321496Sbill char *ap;
14331496Sbill {
14341496Sbill 	register char *p, *p1;
14351496Sbill 
14361496Sbill 	p = ap;
14371496Sbill 	if (!natural(p)) {
14381496Sbill 		ccloc[0] = 0;
14391496Sbill 		return;
14401496Sbill 	}
14411496Sbill 	p1 = ccloc;
14421496Sbill 	*p1++ = type;
14431496Sbill 	while (*p1++ = *p++);
14441496Sbill }
14451496Sbill 
14461496Sbill okio(p) register char *p; {/* 0->probable I/O space address; 1->not */
14471496Sbill 	if (ioflag && (!natural(p) || 0>getnum(p))) return(0);
14481496Sbill 	return(1);
14491496Sbill }
14501496Sbill 
14511496Sbill indexa(p) register char *p; {/* 1-> uses [r] addressing mode; 0->doesn't */
14521496Sbill 	while (*p) if (*p++=='[') return(1);
14531496Sbill 	return(0);
14541496Sbill }
14551496Sbill 
14561496Sbill natural(p)
14571496Sbill register char *p;
14581496Sbill {/* 1->simple local, parameter, global, or register; 0->otherwise */
145917210Sralph 	if (*p=='*' || *p=='(' || *p=='-'&&p[1]=='(' || *p=='$'&&getnum(p+1))
14601496Sbill 		return(0);
14611496Sbill 	while (*p++);
14621496Sbill 	p--;
146317210Sralph 	if (*--p=='+' || *p==']' || *p==')' && p[-2]!='a' && p[-2]!='f')
14641496Sbill 		return(0);
14651496Sbill 	return(1);
14661496Sbill }
14671496Sbill 
14681496Sbill /*
14691496Sbill ** Tell if an argument is most likely static.
14701496Sbill */
14711496Sbill 
14721496Sbill isstatic(cp)
14731496Sbill register char	*cp;
14741496Sbill {
14751496Sbill 	if (*cp == '_' || *cp == 'L' || (*cp++ == 'v' && *cp == '.'))
14761496Sbill 		return (1);
14771496Sbill 	return (0);
14781496Sbill }
147916133Sralph 
148016133Sralph autoid(p) register char *p; {/* 1-> uses autoincrement/autodecrement; 0->doesn't */
148116133Sralph 	if (*p == '-' && *(p+1) == '(') return(1);
148216133Sralph 	while (*p) p++;
148316133Sralph 	if (*--p == '+' && *--p == ')') return(1);
148416133Sralph 	return(0);
148516133Sralph   }
1486