1*17720Sralph static char sccsid[] = "@(#)c21.c 4.14 01/16/85"; 21498Sbill /* char C21[] = {"@(#)c21.c 1.83 80/10/16 21:18:22 JFR"}; /* sccs ident */ 31496Sbill 41496Sbill /* 51496Sbill * C object code improver-- second part 61496Sbill */ 71496Sbill 81496Sbill #include "c2.h" 91496Sbill #include <stdio.h> 101496Sbill #include <ctype.h> 111496Sbill 121496Sbill #define NUSE 6 131496Sbill int ioflag; 141496Sbill int biti[NUSE] = {1,2,4,8,16,32}; 155794Sroot int bitsize[] = { /* index by type codes */ 166958Srrh 0, /* 0 not allocated */ 176958Srrh 8, /* 1 BYTE */ 186958Srrh 16, /* 2 WORD */ 196958Srrh 32, /* 3 LONG */ 206958Srrh 32, /* 4 FFLOAT / 216958Srrh 64, /* 5 DFLOAT */ 226958Srrh 64, /* 6 QUAD */ 236958Srrh 0, /* 7 OP2 */ 246958Srrh 0, /* 8 OP3 */ 256958Srrh 0, /* 9 OPB */ 266958Srrh 0, /* 10 OPX */ 276958Srrh 64, /* 11 GFLOAT */ 286958Srrh 128, /* 12 HFLOAT */ 296958Srrh 128 /* 13 OCTA */ 305794Sroot }; 311496Sbill int pos,siz; long f; /* for bit field communication */ 321496Sbill struct node *uses[NUSE]; /* for backwards flow analysis */ 331496Sbill char *lastrand; /* last operand of instruction */ 341496Sbill struct node *bflow(); 351496Sbill struct node *bicopt(); 361496Sbill char *findcon(); 371496Sbill 381496Sbill redun3(p,split) register struct node *p; int split; { 391496Sbill /* check for 3 addr instr which should be 2 addr */ 401496Sbill if (OP3==((p->subop>>4)&0xF)) { 411496Sbill if (split) splitrand(p); 421496Sbill if (equstr(regs[RT1],regs[RT3]) 431496Sbill && (p->op==ADD || p->op==MUL || p->op==BIS || p->op==XOR)) { 441496Sbill register char *t=regs[RT1]; regs[RT1]=regs[RT2]; regs[RT2]=t; 451496Sbill } 461496Sbill if (equstr(regs[RT2],regs[RT3])) { 471496Sbill p->subop=(p->subop&0xF)|(OP2<<4); p->pop=0; 481496Sbill lastrand=regs[RT2]; *regs[RT3]=0; return(1); 491496Sbill } 501496Sbill } return(0); 511496Sbill } 521496Sbill 531496Sbill bmove() { 541496Sbill register struct node *p, *lastp; register char *cp1,*cp2; register int r; 551496Sbill refcount(); 561496Sbill for (p=lastp= &first; 0!=(p=p->forw); lastp=p); 571496Sbill clearreg(); clearuse(); 581496Sbill for (p=lastp; p!= &first; p=p->back) { 591496Sbill if (debug) { 601496Sbill printf("Uses:\n"); 611496Sbill for (r=NUSE;--r>=0;) if (uses[r]) 621496Sbill printf("%d: %s\n",r,uses[r]->code? uses[r]->code:""); 631496Sbill printf("-\n"); 641496Sbill } 651496Sbill r=(p->subop>>4)&0xF; 661496Sbill if (OP2==r && (cp1=p->code, *cp1++)=='$' && *cp1++=='0' && *cp1++==',' && 671496Sbill !source(cp1)) {/* a no-op unless MUL or DIV */ 681496Sbill if (p->op==MUL) {p->op=MOV; p->subop&=0xF; p->pop=0;} 691496Sbill else if (p->op==DIV) fprintf(stderr,"c2: zero divide\n"); 701496Sbill else {delnode(p); redunm++; continue;} 711496Sbill } 721496Sbill if (OP3==r && 0!=redun3(p,1)) {newcode(p); redunm++;} 731496Sbill switch (p->op) { 741496Sbill case LABEL: case DLABEL: 751496Sbill for (r=NUSE; --r>=0;) 761496Sbill if (uses[r]) p->ref=(struct node *) (((int)p->ref)|biti[r]); 771496Sbill break; 781496Sbill case CALLS: 791496Sbill clearuse(); goto std; 801496Sbill case 0: 81*17720Sralph /* 82*17720Sralph * Be conservative -- if we don't know what it is, then we 83*17720Sralph * assume that it can set anything. 84*17720Sralph */ 85*17720Sralph for ( r = 0; r < NUSE; ++r ) 86*17720Sralph uses[r] = p; 87*17720Sralph break; 881496Sbill case SUB: 891496Sbill if ((p->subop&0xF)!=LONG) goto std; cp1=p->code; 901496Sbill if (*cp1++!='$') goto std; splitrand(p); 911496Sbill if (equstr(regs[RT2],"fp") && !indexa(regs[RT1])) {/* address comp. */ 923933Sroot char buf[C2_ASIZE]; cp2=buf; *cp2++='-'; 931496Sbill cp1=regs[RT1]+1; while (*cp2++= *cp1++); --cp2; 941496Sbill cp1="(fp),"; while (*cp2++= *cp1++); --cp2; 951496Sbill cp1=regs[RT3]; while (*cp2++= *cp1++); 961496Sbill p->code=copy(buf); p->combop=T(MOVA,LONG); p->pop=0; 971496Sbill } else if (*cp1++=='-' && 0<=(r=getnum(cp1))) { 981496Sbill p->op=ADD; p->pop=0; *--cp1='$'; p->code=cp1; 991496Sbill } goto std; 1001496Sbill case ADD: 1011496Sbill if ((p->subop&0xF)!=LONG) goto std; cp1=p->code; 1021496Sbill if (*cp1++!='$') goto std; splitrand(p); 1031496Sbill if (isstatic(cp1) && (r=isreg(regs[RT2]))>=0 && r<NUSE && uses[r]==p->forw) 1041496Sbill { 1051496Sbill /* address comp: 1061496Sbill ** addl2 $_foo,r0 \ movab _foo[r0],bar 1071496Sbill ** movl r0,bar / 1081496Sbill */ 1091496Sbill register struct node *pnext = p->forw; 1103933Sroot char buf[C2_ASIZE]; 1111496Sbill 1121496Sbill if (pnext->op == MOV && pnext->subop == LONG) 1131496Sbill { 1141496Sbill cp1 = ®s[RT1][1]; cp2 = &buf[0]; 1151496Sbill while (*cp2++ = *cp1++) ; cp2--; 1161496Sbill splitrand(pnext); 1171496Sbill if (r == isreg(regs[RT1])) 1181496Sbill { 1191496Sbill delnode(p); p = pnext; 1201496Sbill p->op = MOVA; p->subop = BYTE; 1211496Sbill p->pop = 0; 1221496Sbill cp1 = regs[RT1]; *cp2++ = '['; 1231496Sbill while (*cp2++ = *cp1++) ; cp2--; 1241496Sbill *cp2++ = ']'; *cp2++ = ','; 1251496Sbill cp1 = regs[RT2]; 1261496Sbill while (*cp2++ = *cp1++) ; 1271496Sbill p->code = copy(buf); 1281496Sbill } 1291496Sbill } 1301496Sbill } 1311496Sbill else 1321496Sbill if (equstr(regs[RT2],"fp") && !indexa(regs[RT1])) {/* address comp. */ 1331496Sbill cp2=cp1-1; cp1=regs[RT1]+1; while (*cp2++= *cp1++); --cp2; 1341496Sbill cp1="(fp)"; while (*cp2++= *cp1++); *--cp2=','; 1351496Sbill p->combop=T(MOVA,LONG); p->pop=0; 1361496Sbill } else if (*cp1++=='-' && 0<=(r=getnum(cp1))) { 1371496Sbill p->op=SUB; p->pop=0; *--cp1='$'; p->code=cp1; 1381496Sbill } 1391496Sbill /* fall thru ... */ 1401496Sbill case CASE: 1411496Sbill default: std: 1421496Sbill p=bflow(p); break; 1431496Sbill case MUL: 1441496Sbill { 1451496Sbill /* 1461496Sbill ** Change multiplication by constant powers of 2 to 1471496Sbill ** shifts. 1481496Sbill */ 1491496Sbill splitrand(p); 1501496Sbill if (regs[RT1][0] != '$' || regs[RT1][1] == '-') goto std; 1511496Sbill if ((r = ispow2(getnum(®s[RT1][1]))) < 0) goto std; 1521496Sbill switch (r) 1531496Sbill { 1541496Sbill case 0: /* mull3 $1,x,y */ 1551496Sbill if (p->subop == U(LONG,OP3)) 1561496Sbill { 1571496Sbill if (equstr(regs[RT2], regs[RT3])) 1581496Sbill { 1591496Sbill delnode(p); p = p->forw; 1601496Sbill } 1611496Sbill else 1621496Sbill { 1631496Sbill p->op = MOV; p->subop = LONG; 1641496Sbill p->pop = 0; newcode(p); nchange++; 1651496Sbill } 1661496Sbill } 1671496Sbill else 1681496Sbill if (p->subop == U(LONG,OP2)) 1691496Sbill { 1701496Sbill delnode(p); p = p->forw; 1711496Sbill } 1721496Sbill goto std; 1731496Sbill 1741496Sbill case 1: /* mull2 $2,x */ 1751496Sbill if (p->subop == U(LONG, OP2) && !source(regs[RT2])) 1761496Sbill { 1771496Sbill strcpy(regs[RT1], regs[RT2]); 1781496Sbill p->op = ADD; p->pop = 0; newcode(p); nchange++; 1791496Sbill } 1801496Sbill goto std; 1811496Sbill } 1821496Sbill if(p->subop==U(LONG,OP3)||(p->subop==U(LONG,OP2)&&!source(regs[RT2]))) 1831496Sbill { 1841496Sbill if (p->subop == U(LONG,OP2)) 1851496Sbill strcpy(regs[RT3], regs[RT2]); 1861496Sbill sprintf(regs[RT1], "$%d", r); 1871496Sbill p->op = ASH; p->subop = LONG; 1881496Sbill p->pop = 0; newcode(p); nchange++; 1891496Sbill } 1901496Sbill goto std; 1911496Sbill } 1921496Sbill case ASH: 1931496Sbill { 1941496Sbill /* address comp: 1951496Sbill ** ashl $1,bar,r0 \ movl bar,r0 1961496Sbill ** movab _foo[r0] / movaw _foo[r0] 1971496Sbill ** 1981496Sbill ** ashl $2,r0,r0 \ moval _foo[r0] 1991496Sbill ** movab _foo[r0] / 2001496Sbill */ 2011496Sbill register struct node *pf; 2021496Sbill register int shfrom, shto; 2031496Sbill long shcnt; 2041496Sbill char *regfrom; 2051496Sbill 2061496Sbill splitrand(p); 2071496Sbill if (regs[RT1][0] != '$') goto std; 2081496Sbill if ((shcnt = getnum(®s[RT1][1])) < 1 || shcnt > 3) goto std; 2091496Sbill if ((shfrom = isreg(regs[RT2])) >= 0) 2101496Sbill regfrom = copy(regs[RT2],"]"); 2111496Sbill if ((shto = isreg(regs[RT3])) >= 0 && shto<NUSE) 2121496Sbill { 2131496Sbill int regnum; 2141496Sbill 2151496Sbill if (uses[shto] != (pf = p->forw)) goto ashadd; 2161496Sbill if (pf->op != MOVA && pf->op != PUSHA) goto ashadd; 2171496Sbill if (pf->subop != BYTE) goto ashadd; 2181496Sbill splitrand(pf); 2191496Sbill if (!indexa(regs[RT1])) goto std; 2201496Sbill cp2 = regs[RT1]; 2211496Sbill if(!isstatic(cp2)) goto std; 2221496Sbill while (*cp2++ != '[') ; 2231496Sbill if (*cp2++ != 'r' || !isdigit(*cp2)) goto std; 2241496Sbill regnum = *cp2++ - '0'; 2251496Sbill if (isdigit(*cp2)) 2261496Sbill { 2271496Sbill if (cp2[1] != ']') goto std; 2281496Sbill regnum *= 10; regnum += *cp2 - '0'; 2291496Sbill } 2301496Sbill if (regnum != shto) goto std; 2311496Sbill if (shfrom >= 0) /* ashl $N,r*,r0 */ 2321496Sbill { 2331496Sbill delnode(p); 2341496Sbill if (shfrom != shto) 2351496Sbill { 2361496Sbill uses[shto] = NULL; splitrand(pf); 2371496Sbill cp2=regs[RT1]; while (*cp2++!='['); 2381496Sbill cp1=regfrom; while (*cp2++= *cp1++); 2391496Sbill newcode(pf); 2401496Sbill } 2411496Sbill } 2421496Sbill else 2431496Sbill { 2441496Sbill p->op = MOV; splitrand(p); 2451496Sbill strcpy(regs[RT1], regs[RT2]); 2461496Sbill strcpy(regs[RT2], regs[RT3]); 2471496Sbill regs[RT3][0] = '\0'; 2481496Sbill p->pop = 0; newcode(p); 2491496Sbill } 2501496Sbill switch (shcnt) 2511496Sbill { 2521496Sbill case 1: pf->subop = WORD; break; 2531496Sbill case 2: pf->subop = LONG; break; 2541496Sbill case 3: pf->subop = QUAD; break; 2551496Sbill } 2561496Sbill redunm++; nsaddr++; nchange++; 2571496Sbill } 2581496Sbill goto std; 2591496Sbill ashadd: 2601496Sbill /* at this point, RT2 and RT3 are guaranteed to be simple regs*/ 2611496Sbill if (shcnt == 1 && equstr(regs[RT2], regs[RT3])) 2621496Sbill { 2631496Sbill /* 2641496Sbill ** quickie: 2651496Sbill ** ashl $1,A,A > addl2 A,A 2661496Sbill */ 2671496Sbill p->op = ADD; p->subop = U(LONG,OP2); p->pop = 0; 2681496Sbill strcpy(regs[RT1], regs[RT2]); regs[RT3][0] = '\0'; 2691496Sbill newcode(p); nchange++; 2701496Sbill } 2711496Sbill goto std; 2721496Sbill } 2731496Sbill 2741496Sbill case EXTV: 2751496Sbill case EXTZV: 2761496Sbill { 2771496Sbill /* bit tests: 2781496Sbill ** extv A,$1,B,rC \ 2791496Sbill ** tstl rC > jbc A,B,D 2801496Sbill ** jeql D / 2811496Sbill ** 2821496Sbill ** also byte- and word-size fields: 2831496Sbill ** extv $n*8,$8,A,B > cvtbl n+A,B 28416133Sralph ** extv $n*16,$16,A,B > cvtwl 2n+A,B 2851496Sbill ** extzv $n*8,$8,A,B > movzbl n+A,B 28616133Sralph ** extzv $n*16,$16,A,B > movzwl 2n+A,B 2871496Sbill */ 2881496Sbill register struct node *pf; /* forward node */ 2891496Sbill register struct node *pn; /* next node (after pf) */ 2901496Sbill int flen; /* field length */ 2911496Sbill 2921496Sbill splitrand(p); 2931496Sbill if (regs[RT2][0] != '$') goto std; 2941496Sbill if ((flen = getnum(®s[RT2][1])) < 0) goto std; 2951496Sbill if (flen == 1) 2961496Sbill { 2971496Sbill register int extreg; /* reg extracted to */ 2981496Sbill 2991496Sbill extreg = isreg(regs[RT4]); 3001496Sbill if (extreg < 0 || extreg >= NUSE) goto std; 3011496Sbill if ((pf = p->forw)->op != TST) goto std; 3021496Sbill if (uses[extreg] && uses[extreg] != pf) goto std; 3031496Sbill splitrand(pf); 3041496Sbill if (extreg != isreg(regs[RT1])) goto std; 3051496Sbill if ((pn = pf->forw)->op != CBR) goto std; 3061496Sbill if (pn->subop != JEQ && pn->subop != JNE) goto std; 3071496Sbill delnode(p); delnode(pf); 3081496Sbill pn->subop = (pn->subop == JEQ) ? JBC : JBS; 3091496Sbill for(cp2=p->code; *cp2++!=',';); 3101496Sbill for(cp1=cp2; *cp1++!=',';); 3111496Sbill while (*cp1!=',') *cp2++= *cp1++; *cp2='\0'; 3121496Sbill pn->code = p->code; pn->pop = NULL; 3131496Sbill uses[extreg] = NULL; 3141496Sbill } 3151496Sbill else 3161496Sbill if (flen == 8 || flen == 16) 3171496Sbill { 3181496Sbill register int boff; /* bit offset */ 3191496Sbill register int coff; /* chunk (byte or word) offset*/ 3201496Sbill 3211496Sbill if (regs[RT1][0] != '$') goto std; 3221496Sbill if ((boff = getnum(®s[RT1][1])) < 0) goto std; 3231496Sbill coff = boff / flen; 3241496Sbill if (coff && (isreg(regs[RT3]) >= 0)) goto std; 3251496Sbill if (boff < 0 || (boff % flen) != 0) goto std; 3261496Sbill p->op = (p->op == EXTV) ? CVT : MOVZ; 3271496Sbill p->subop = U((flen == 8 ? BYTE : WORD), LONG); 3281496Sbill if (coff == 0) 3291496Sbill strcpy(regs[RT1], regs[RT3]); 3301496Sbill else 33116133Sralph sprintf(regs[RT1], "%d%s%s", 33216133Sralph (flen == 8 ? coff : 2*coff), 33316133Sralph (regs[RT3][0] == '(' ? "" : "+"), 3341496Sbill regs[RT3]); 3351496Sbill strcpy(regs[RT2], regs[RT4]); 3361496Sbill regs[RT3][0] = '\0'; regs[RT4][0] = '\0'; 3371496Sbill p->pop = 0; newcode(p); 3381496Sbill } 3391496Sbill nchange++; 3401496Sbill goto std; 3411496Sbill } 3421496Sbill 3431496Sbill case CMP: 3441496Sbill { 3451496Sbill /* comparison to -63 to -1: 3461496Sbill ** cmpl r0,$-1 > incl r0 3471496Sbill ** jeql ... 3481496Sbill ** 3491496Sbill ** cmpl r0,$-63 > addl2 $63,r0 3501496Sbill ** jeql ... 3511496Sbill */ 3521496Sbill register int num; 3531496Sbill register int reg; 3541496Sbill register struct node *regp = p->back; 3551496Sbill 3561496Sbill if (p->forw->op != CBR) goto std; 3571496Sbill if (p->forw->subop != JEQ && p->forw->subop != JNE) goto std; 3581496Sbill splitrand(p); 3591496Sbill if (strncmp(regs[RT2], "$-", 2) != 0) goto std; 3601496Sbill reg = r = isreg(regs[RT1]); 3611496Sbill if (r < 0) goto std; 3621496Sbill if (r < NUSE && uses[r] != 0) goto std; 3631496Sbill if (r >= NUSE && regp->op == MOV && p->subop == regp->subop) 3641496Sbill { 3651496Sbill if (*regp->code != 'r') goto std; 3661496Sbill reg = regp->code[1] - '0'; 3671496Sbill if (isdigit(regp->code[2]) || reg >= NUSE || uses[reg]) 3681496Sbill goto std; 3691496Sbill } 3701496Sbill if (r >= NUSE) goto std; 3711496Sbill if (reg != r) 3721496Sbill sprintf(regs[RT1], "r%d", reg); 3731496Sbill if ((num = getnum(®s[RT2][2])) <= 0 || num > 63) goto std; 3741496Sbill if (num == 1) 3751496Sbill { 3761496Sbill p->op = INC; regs[RT2][0] = '\0'; 3771496Sbill } 3781496Sbill else 3791496Sbill { 3801496Sbill register char *t; 3811496Sbill 3821496Sbill t=regs[RT1];regs[RT1]=regs[RT2];regs[RT2]=t; 3831496Sbill p->op = ADD; p->subop = U(p->subop, OP2); 3841496Sbill for (t = ®s[RT1][2]; t[-1] = *t; t++) ; 3851496Sbill } 3861496Sbill p->pop = 0; newcode(p); 3871496Sbill nchange++; 3881496Sbill goto std; 3891496Sbill } 3901496Sbill 3911496Sbill case JSB: 3921496Sbill if (equstr(p->code,"mcount")) {uses[0]=p; regs[0][0]= -1;} 3931496Sbill goto std; 3941496Sbill case JBR: case JMP: 3951496Sbill clearuse(); 3961496Sbill if (p->subop==RET || p->subop==RSB) {uses[0]=p; regs[0][0]= -1; break;} 3971496Sbill if (p->ref==0) goto std; /* jmp (r0) */ 3981496Sbill /* fall through */ 3991496Sbill case CBR: 4001496Sbill if (p->ref->ref!=0) for (r=NUSE;--r>=0;) 4011496Sbill if (biti[r] & (int)p->ref->ref) {uses[r]=p; regs[r][0]= -1;} 4021496Sbill case EROU: case JSW: 4031496Sbill case TEXT: case DATA: case BSS: case ALIGN: case WGEN: case END: ; 4041496Sbill } 4051496Sbill } 4061496Sbill for (p= &first; p!=0; p=p->forw) 4071496Sbill if (p->op==LABEL || p->op==DLABEL) p->ref=0; /* erase our tracks */ 4081496Sbill } 4091496Sbill 4101496Sbill rmove() 4111496Sbill { 4121496Sbill register struct node *p, *lastp; 4131496Sbill register int r; 4141496Sbill int r1; 4151496Sbill 4161496Sbill clearreg(); 4171496Sbill for (p=first.forw; p!=0; p = p->forw) { 4181496Sbill lastp=p; 4191496Sbill if (debug) { 42017210Sralph if (*conloc) { 42117210Sralph r1=conval[0]; 42217210Sralph printf("Con %s = %d%d %s\n", conloc, r1&0xF, r1>>4, conval+1); 42317210Sralph } 4241496Sbill printf("Regs:\n"); 4251496Sbill for (r=0; r<NREG; r++) 4261496Sbill if (regs[r][0]) { 4271496Sbill r1=regs[r][0]; 4281496Sbill printf("%d: %d%d %s\n", r, r1&0xF, r1>>4, regs[r]+1); 4291496Sbill } 4301496Sbill printf("-\n"); 4311496Sbill } 4321496Sbill switch (p->op) { 4331496Sbill 4341496Sbill case CVT: 4351496Sbill splitrand(p); goto mov; 4361496Sbill 4371496Sbill case MOV: 4381496Sbill splitrand(p); 4391496Sbill if ((r = findrand(regs[RT1],p->subop)) >= 0) { 4401496Sbill if (r == isreg(regs[RT2]) && p->forw->op!=CBR) { 4411496Sbill delnode(p); redunm++; break; 4421496Sbill } 4431496Sbill } 4441496Sbill mov: 4451496Sbill repladdr(p); 4461496Sbill r = isreg(regs[RT1]); 4471496Sbill r1 = isreg(regs[RT2]); 4481496Sbill dest(regs[RT2],p->subop); 4491506Sbill if (r>=0) { 4501506Sbill if (r1>=0) savereg(r1, regs[r]+1, p->subop); 4511506Sbill else if (p->op!=CVT) savereg(r, regs[RT2], p->subop); 4521506Sbill } else if (r1>=0) savereg(r1, regs[RT1], p->subop); 4531506Sbill else if (p->op!=CVT) setcon(regs[RT1], regs[RT2], p->subop); 4541496Sbill break; 4551496Sbill 4561496Sbill /* .rx,.wx */ 4571496Sbill case MFPR: 4581496Sbill case COM: 4591496Sbill case NEG: 4601496Sbill /* .rx,.wx or .rx,.rx,.wx */ 4611496Sbill case ADD: 4621496Sbill case SUB: 4631496Sbill case BIC: 4641496Sbill case BIS: 4651496Sbill case XOR: 4661496Sbill case MUL: 4671496Sbill case DIV: 4681496Sbill case ASH: 4691496Sbill case MOVZ: 4701496Sbill /* .rx,.rx,.rx,.wx */ 4711496Sbill case EXTV: 4721496Sbill case EXTZV: 4731496Sbill case INSV: 4741496Sbill splitrand(p); 4751496Sbill repladdr(p); 4761496Sbill dest(lastrand,p->subop); 4771496Sbill if (p->op==INSV) ccloc[0]=0; 4781496Sbill break; 4791496Sbill 4801496Sbill /* .mx or .wx */ 4811496Sbill case CLR: 4821496Sbill case INC: 4831496Sbill case DEC: 4841496Sbill splitrand(p); 4851496Sbill dest(lastrand,p->subop); 4861496Sbill if (p->op==CLR) 4871496Sbill if ((r = isreg(regs[RT1])) >= 0) 4881496Sbill savereg(r, "$0", p->subop); 4891496Sbill else 4901496Sbill setcon("$0", regs[RT1], p->subop); 4911496Sbill break; 4921496Sbill 4931496Sbill /* .rx */ 4941496Sbill case TST: 4951496Sbill case PUSH: 4961496Sbill splitrand(p); 4971496Sbill lastrand=regs[RT1+1]; /* fool repladdr into doing 1 operand */ 4981496Sbill repladdr(p); 4991496Sbill if (p->op==TST && equstr(lastrand=regs[RT1], ccloc+1) 5001496Sbill && ((0xf&(ccloc[0]>>4))==p->subop || equtype(ccloc[0],p->subop)) 5011496Sbill &&!source(lastrand)) { 5021496Sbill delnode(p); p = p->back; nrtst++; nchange++; 5031496Sbill } 5041496Sbill setcc(lastrand,p->subop); 5051496Sbill break; 5061496Sbill 5071496Sbill /* .rx,.rx,.rx */ 5081496Sbill case PROBER: 5091496Sbill case PROBEW: 5101496Sbill case CASE: 5111496Sbill case MOVC3: 5121496Sbill /* .rx,.rx */ 5131496Sbill case MTPR: 5141496Sbill case CALLS: 5151496Sbill case CMP: 5161496Sbill case BIT: 5171496Sbill splitrand(p); 5181496Sbill /* fool repladdr into doing right number of operands */ 5193957Sbugs if (p->op==CASE || p->op==PROBER || p->op==PROBEW) lastrand=regs[RT4]; 5203958Sbugs /* else if (p->op==CMPV || p->op==CMPZV) lastrand=regs[RT4+1]; */ 5213957Sbugs else if (p->op==MOVC3) lastrand=regs[RT1]; 5221496Sbill else lastrand=regs[RT3]; 5231496Sbill repladdr(p); 5241496Sbill if (p->op==CALLS || p->op==MOVC3) clearreg(); 5251496Sbill if (p->op==BIT) bitopt(p); 5261496Sbill ccloc[0]=0; break; 5271496Sbill 5281496Sbill case CBR: 5291496Sbill if (p->subop>=JBC) { 5301496Sbill splitrand(p); 5311496Sbill if (p->subop<JBCC) lastrand=regs[RT3]; /* 2 operands can be optimized */ 5321496Sbill else lastrand=regs[RT2]; /* .mb destinations lose */ 5331496Sbill repladdr(p); 5341496Sbill } 5351496Sbill ccloc[0] = 0; 5361496Sbill break; 5371496Sbill 5381496Sbill case JBR: 5391496Sbill redunbr(p); 5401496Sbill 5411496Sbill /* .wx,.bb */ 5421496Sbill case SOB: 5431496Sbill 5441496Sbill default: 5451496Sbill clearreg(); 5461496Sbill } 5471496Sbill } 5481496Sbill } 5491496Sbill 5501496Sbill char * 5511496Sbill byondrd(p) register struct node *p; { 5521496Sbill /* return pointer to register which is "beyond last read/modify operand" */ 5531496Sbill if (OP2==(p->subop>>4)) return(regs[RT3]); 5541496Sbill switch (p->op) { 5551496Sbill case MFPR: 5561496Sbill case JSB: 5571496Sbill case PUSHA: 5581496Sbill case TST: case INC: case DEC: case PUSH: return(regs[RT2]); 5591496Sbill case MTPR: 5601496Sbill case BIT: case CMP: case CALLS: return(regs[RT3]); 5611496Sbill case PROBER: case PROBEW: 5621496Sbill case CASE: case MOVC3: return(regs[RT4]); 5631496Sbill } 5641496Sbill return(lastrand); 5651496Sbill } 5661496Sbill 5671496Sbill struct node * 5681496Sbill bflow(p) 5691496Sbill register struct node *p; 5701496Sbill { 5711496Sbill register char *cp1,*cp2,**preg; register int r; 5721496Sbill int flow= -1; 5731496Sbill struct node *olduse=0; 5741496Sbill splitrand(p); 5751496Sbill if (p->op!=PUSH && p->subop && 0<=(r=isreg(lastrand)) && r<NUSE && uses[r]==p->forw) { 5761496Sbill if (equtype(p->subop,regs[r][0]) 5771496Sbill || ((p->op==CVT || p->op==MOVZ) 5781496Sbill && 0xf®s[r][0] && compat(0xf&(p->subop>>4),regs[r][0]))) { 5791496Sbill register int r2; 5801496Sbill if (regs[r][1]!=0) {/* send directly to destination */ 5811496Sbill if (p->op==INC || p->op==DEC) { 5821496Sbill if (p->op==DEC) p->op=SUB; else p->op=ADD; 5831496Sbill p->subop=(OP2<<4)+(p->subop&0xF); /* use 2 now, convert to 3 later */ 5841496Sbill p->pop=0; 5851496Sbill cp1=lastrand; cp2=regs[RT2]; while (*cp2++= *cp1++); /* copy reg */ 5861496Sbill cp1=lastrand; *cp1++='$'; *cp1++='1'; *cp1=0; 5871496Sbill } 5881496Sbill cp1=regs[r]+1; cp2=lastrand; 5891496Sbill if (OP2==(p->subop>>4)) {/* use 3 operand form of instruction */ 5901496Sbill p->pop=0; 5911496Sbill p->subop += (OP3-OP2)<<4; lastrand=cp2=regs[RT3]; 5921496Sbill } 5931496Sbill while (*cp2++= *cp1++); 5941496Sbill if (p->op==MOVA && p->forw->op==PUSH) { 5951496Sbill p->op=PUSHA; *regs[RT2]=0; p->pop=0; 5961496Sbill } else if (p->op==MOV && p->forw->op==PUSH) { 5971496Sbill p->op=PUSH ; *regs[RT2]=0; p->pop=0; 5981496Sbill } 5991496Sbill delnode(p->forw); 6001496Sbill if (0<=(r2=isreg(lastrand)) && r2<NUSE) { 6011496Sbill uses[r2]=uses[r]; uses[r]=0; 6021496Sbill } 6031496Sbill redun3(p,0); 6041496Sbill newcode(p); redunm++; flow=r; 6051496Sbill } else if (p->op==MOV && p->forw->op!=EXTV && p->forw->op!=EXTZV) { 6061496Sbill /* superfluous fetch */ 6071496Sbill int nmatch; 6082829Stoy char src[C2_ASIZE]; 6091496Sbill movit: 6101496Sbill cp2=src; cp1=regs[RT1]; while (*cp2++= *cp1++); 6111496Sbill splitrand(p->forw); 6121496Sbill if (p->forw->op != INC && p->forw->op != DEC) 6131496Sbill lastrand=byondrd(p->forw); 6141496Sbill nmatch=0; 6151496Sbill for (preg=regs+RT1;*preg!=lastrand;preg++) 6161496Sbill if (r==isreg(*preg)) { 6171496Sbill cp2= *preg; cp1=src; while (*cp2++= *cp1++); ++nmatch; 6181496Sbill } 6191496Sbill if (nmatch==1) { 6201496Sbill if (OP2==(p->forw->subop>>4) && equstr(src,regs[RT2])) { 6211496Sbill p->forw->pop=0; 6221496Sbill p->forw->subop += (OP3-OP2)<<4; cp1=regs[RT3]; 6231496Sbill *cp1++='r'; *cp1++=r+'0'; *cp1=0; 6241496Sbill } 6251496Sbill delnode(p); p=p->forw; 6261496Sbill if (0<=(r2=isreg(src)) && r2<NUSE) { 6271496Sbill uses[r2]=uses[r]; uses[r]=0; 6281496Sbill } 6291496Sbill redun3(p,0); 6301496Sbill newcode(p); redunm++; flow=r; 6311496Sbill } else splitrand(p); 6321496Sbill } 6331496Sbill } else if (p->op==MOV && (p->forw->op==CVT || p->forw->op==MOVZ) 6341496Sbill && p->forw->subop&0xf /* if base or index, then forget it */ 6351496Sbill && compat(p->subop,p->forw->subop) && !source(cp1=regs[RT1]) 6361496Sbill && !indexa(cp1)) goto movit; 6371496Sbill } 6381496Sbill /* adjust 'lastrand' past any 'read' or 'modify' operands. */ 6391496Sbill lastrand=byondrd(p); 6401496Sbill /* a 'write' clobbers the register. */ 6411496Sbill if (0<=(r=isreg(lastrand)) && r<NUSE 6421496Sbill || OP2==(p->subop>>4) && 0<=(r=isreg(regs[RT2])) && r<NUSE && uses[r]==0) { 6431496Sbill /* writing a dead register is useless, but watch side effects */ 6441496Sbill switch (p->op) { 6451498Sbill case ACB: 6461496Sbill case AOBLEQ: case AOBLSS: case SOBGTR: case SOBGEQ: break; 6471498Sbill default: 6481496Sbill if (uses[r]==0) {/* no direct uses, check for use of condition codes */ 6491496Sbill register struct node *q=p; 6501496Sbill while ((q=nonlab(q->forw))->combop==JBR) q=q->ref; /* cc unused, unchanged */ 6511496Sbill if (q->op!=CBR) {/* ... and destroyed */ 6521496Sbill preg=regs+RT1; 6531496Sbill while (cp1= *preg++) { 6541496Sbill if (cp1==lastrand) {redunm++; delnode(p); return(p->forw);} 6551496Sbill if (source(cp1) || equstr(cp1,lastrand)) break; 6561496Sbill } 6571496Sbill } 6581496Sbill } 6591496Sbill flow=r; 6601496Sbill } 6611496Sbill } 6621496Sbill if (0<=(r=flow)) {olduse=uses[r]; uses[r]=0; *(short *)(regs[r])=0;} 6631496Sbill /* these two are here, rather than in bmove(), 6641496Sbill /* because I decided that it was better to go for 3-address code 6651496Sbill /* (save time) rather than fancy jbxx (save 1 byte) 6661496Sbill /* on sequences like bisl2 $64,r0; movl r0,foo 6671496Sbill */ 6681496Sbill if (p->op==BIC) {p=bicopt(p); splitrand(p); lastrand=byondrd(p);} 6691496Sbill if (p->op==BIS) {bixprep(p,JBSS); lastrand=byondrd(p);} 6701496Sbill /* now look for 'read' or 'modify' (read & write) uses */ 6711496Sbill preg=regs+RT1; 6721496Sbill while (*(cp1= *preg++)) { 6731496Sbill /* check for r */ 6741496Sbill if (lastrand!=cp1 && 0<=(r=isreg(cp1)) && r<NUSE && uses[r]==0) { 6751496Sbill uses[r]=p; cp2=regs[r]; *cp2++=p->subop; 6761506Sbill if (p->op==ASH && preg==(regs+RT1+1)) cp2[-1]=BYTE; /* stupid DEC */ 6771496Sbill if (p->op==MOV || p->op==PUSH || p->op==CVT || p->op==MOVZ || p->op==COM || p->op==NEG) { 6781496Sbill if (p->op==PUSH) cp1="-(sp)"; 6791496Sbill else { 6801496Sbill cp1=regs[RT2]; 6811496Sbill if (0<=(r=isreg(cp1)) && r<NUSE && uses[r]==0) 6821496Sbill uses[r]=olduse; /* reincarnation!! */ 6831496Sbill /* as in addl2 r0,r1; movl r1,r0; ret */ 6841496Sbill if (p->op!=MOV) cp1=0; 6851496Sbill } 6861496Sbill if (cp1) while (*cp2++= *cp1++); 6871506Sbill else *cp2=0; 6881506Sbill } else *cp2=0; 6891496Sbill continue; 6901496Sbill } 6911496Sbill /* check for (r),(r)+,-(r),[r] */ 6921496Sbill do if (*cp1=='(' || *cp1=='[') {/* get register number */ 6931496Sbill char t; 6941496Sbill cp2= ++cp1; while (*++cp1!=')' && *cp1!=']'); t= *cp1; *cp1=0; 6951496Sbill if (0<=(r=isreg(cp2)) && r<NUSE && (uses[r]==0 || uses[r]==p)) { 6961496Sbill uses[r]=p; regs[r][0]=(*--cp2=='[' ? OPX<<4 : OPB<<4); 6971496Sbill } 6981496Sbill *cp1=t; 6991496Sbill } while (*++cp1); 7001496Sbill } 7011496Sbill /* pushax or movax possibility? */ 7021496Sbill cp1=regs[RT1]; 7031496Sbill if (*cp1++=='$' && isstatic(cp1) && natural(regs[RT1])) { 7041496Sbill if (p->combop==T(MOV,LONG)) { 7051496Sbill if (regs[RT1][1]=='L' && 0!=(p->labno=getnum(regs[RT1]+2))) { 7061496Sbill cp1=p->code; while (*cp1++!=','); p->code= --cp1; 7071496Sbill } 7081496Sbill p->combop=T(MOVA,LONG); ++p->code; p->pop=0; 7091496Sbill } else if (p->combop==T(PUSH,LONG)) { 7101496Sbill p->combop=T(PUSHA,LONG); ++p->code; p->pop=0; 7111496Sbill } else if ((p->combop&0xFFFF)==T(ADD,U(LONG,OP3)) 7121496Sbill && 0<=(r=isreg(regs[RT2]))) { 7131496Sbill cp1=cp2=p->code; ++cp1; 7141496Sbill do *cp2++= *cp1; while (*cp1++!=','); cp2[-1]='['; 7151496Sbill do *cp2++= *cp1; while (*cp1++!=','); cp2[-1]=']'; 7161496Sbill if (!equstr(regs[RT3],"-(sp)")) p->combop=T(MOVA,BYTE); 7171496Sbill else {p->combop=T(PUSHA,BYTE); *cp2=0;} 7181496Sbill if (uses[r]==0) {uses[r]=p; regs[r][0]=OPX<<4;} 7191496Sbill p->pop=0; 7201496Sbill } 7211496Sbill } 7221496Sbill return(p); 7231496Sbill } 7241496Sbill 7251496Sbill ispow2(n) register long n; {/* -1 -> no; else -> log to base 2 */ 7261496Sbill register int log; 7271496Sbill if (n==0 || n&(n-1)) return(-1); log=0; 7281496Sbill for (;;) {n >>= 1; if (n==0) return(log); ++log; if (n== -1) return(log);} 7291496Sbill } 7301496Sbill 7311496Sbill bitopt(p) register struct node *p; { 7321496Sbill /* change "bitx $<power_of_2>,a" followed by JEQ or JNE 7331496Sbill /* into JBC or JBS. watch out for I/O registers. (?) 7341496Sbill /* assumes that 'splitrand' has already been called. 7351496Sbill */ 7361496Sbill register char *cp1,*cp2; int b; 7371496Sbill cp1=regs[RT1]; cp2=regs[RT2]; 7381496Sbill if (*cp1++!='$' || !okio(cp2) || p->forw->op!=CBR || p->forw->subop&-2 || 7391496Sbill 0>(b=ispow2(getnum(cp1))) || 7401496Sbill p->subop!=BYTE && (source(cp2) || indexa(cp2))) return; 7411496Sbill if (b>=bitsize[p->subop]) {/* you dummy! */ 7421496Sbill if (source(cp2)) {/* side effect: auto increment or decrement */ 7431496Sbill p->pop=0; 7441496Sbill p->op=TST; --cp1; while (*cp1++= *cp2++); 7451496Sbill regs[RT2][0]=0; newcode(p); 7461496Sbill } else delnode(p); 7471496Sbill p = p->forw; 7481496Sbill if (p->subop==JEQ) {p->combop=JBR; p->pop=0;} 7491496Sbill else delnode(p); 7501496Sbill nchange++; nbj++; return; 7511496Sbill } 7521496Sbill if (cp1=p->forw->code) {/* destination is not an internal label */ 7531496Sbill cp2=regs[RT3]; while (*cp2++= *cp1++); 7541496Sbill } 7551496Sbill if (b==0 && (p->subop==LONG || !indexa(regs[RT2]))) {/* JLB optimization, ala BLISS */ 7561496Sbill cp2=regs[RT1]; cp1=regs[RT2]; while (*cp2++= *cp1++); 7571496Sbill cp2=regs[RT2]; cp1=regs[RT3]; while (*cp2++= *cp1++); 7581496Sbill *(regs[RT3])=0; p->forw->subop += JLBC-JBC; 7591496Sbill p->forw->pop=0; 7601496Sbill } else { 7611496Sbill cp1=regs[RT1]+1; 7621496Sbill if (b>9) *cp1++= b/10 +'0'; *cp1++= b%10 +'0'; *cp1=0; /* $<bit_number> */ 7631496Sbill } 7641496Sbill nbj++; newcode(p); p->combop = p->forw->combop+((JBC-JEQ)<<8); 7651496Sbill p->labno = p->forw->labno; delnode(p->forw); 7661496Sbill p->pop=0; 7671496Sbill } 7681496Sbill 7691496Sbill isfield(n) register long n; {/* -1 -> no; else -> position of low bit */ 7701496Sbill register int pos; register long t; 7711496Sbill t= ((n-1)|n) +1; 77216832Sralph if (n!=0 && (0==t || 0<=ispow2(t))) { 7731496Sbill pos=0; while(!(n&1)) {n >>= 1; ++pos;} return(pos); 7741496Sbill } else return(-1); 7751496Sbill } 7761496Sbill 7771496Sbill bixprep(p,bix) register struct node *p; { 7781496Sbill /* initial setup, single-bit checking for bisopt, bicopt. 7791496Sbill /* return: 0->don't bother any more; 1->worthwhile trying 7801496Sbill */ 7811496Sbill register char *cp1,*cp2; 7821496Sbill splitrand(p); cp1=regs[RT1]; cp2=regs[RT2]; 7831496Sbill if (*cp1++!='$' || 0>(pos=isfield(f=getnum(cp1))) 7841496Sbill || !okio(cp2) || indexa(cp2) || source(cp2) || !okio(lastrand)) return(0); 7851496Sbill f |= f-1; if (++f==0) siz=32-pos; else siz=ispow2(f)-pos; 7861496Sbill if (siz==1 && pos>5 && (p->subop>>4)==OP2 && (p->subop&0xF)!=BYTE 7871496Sbill && pos<bitsize[p->subop&0xF]) { 7881496Sbill p->ref = insertl(p->forw); p->combop = CBR | (bix<<8); 7891496Sbill p->pop=0; 7901496Sbill p->labno = p->ref->labno; 7911496Sbill if (pos>9) {*cp1++= pos/10 +'0'; pos %= 10;} 7921496Sbill *cp1++=pos+'0'; *cp1=0; newcode(p); nbj++; return(0); 7931496Sbill } 7941496Sbill return(1); 7951496Sbill } 7961496Sbill 7971496Sbill 7981496Sbill struct node * 7991496Sbill bicopt(p) register struct node *p; { 8001496Sbill /* use field operations or MOVZ if possible. done as part of 'bflow'. 8011496Sbill */ 8021496Sbill register char *cp1,*cp2; int r; 8033933Sroot char src[C2_ASIZE]; 80416133Sralph char lhssiz, subop; 8051496Sbill if (!bixprep(p,JBCC)) return(p); 8061496Sbill if (f==0) {/* the BIC isolates low order bits */ 8071496Sbill siz=pos; pos=0; 8081496Sbill if ((p->subop&0xF)==LONG && *(regs[RT2])!='$') {/* result of EXTZV is long */ 8091496Sbill /* save source of BICL in 'src' */ 8101496Sbill cp1=regs[RT2]; cp2=src; while (*cp2++= *cp1++); 8111496Sbill if (p->back->op==ASH) {/* try for more */ 8121496Sbill splitrand(p->back); cp1=regs[RT1]; cp2=regs[RT3]; 8131496Sbill if (*cp1++=='$' && *(regs[RT2])!='$' && !indexa(regs[RT2]) 8141496Sbill && 0>(f=getnum(cp1)) && equstr(src,cp2) 81516832Sralph && 0<=(r=isreg(cp2)) && r<NUSE 81616832Sralph && siz-f <= 32) { /* a good ASH */ 8171496Sbill pos -= f; cp1=regs[RT2]; cp2=src; while (*cp2++= *cp1++); 8181496Sbill delnode(p->back); 8191496Sbill } 8201496Sbill } 82116133Sralph /* 82216133Sralph * 'pos', 'siz' known; find out the size of the 82316133Sralph * left-hand operand of what the bicl will turn into. 82416133Sralph */ 82516133Sralph if (pos==0 && siz==16) 82616133Sralph lhssiz = WORD; /* movzwl */ 82716133Sralph else 82816133Sralph lhssiz = BYTE; /* movzbl or extzvl */ 8291496Sbill if (p->back->op==CVT || p->back->op==MOVZ) {/* greedy, aren't we? */ 8301496Sbill splitrand(p->back); cp1=regs[RT1]; cp2=regs[RT2]; 83116133Sralph /* 83216133Sralph * If indexa(cp1) || autoid(cp1), the fold may 83316133Sralph * still be OK if the CVT/MOVZ has the same 83416133Sralph * size operand on its left size as what we 83516133Sralph * will turn the bicl into. 83616133Sralph * However, if the CVT is from a float or 83716133Sralph * double, forget it! 83816133Sralph */ 83916133Sralph subop = p->back->subop&0xF; /* type of LHS of CVT/MOVZ */ 84016133Sralph if (equstr(src,cp2) && okio(cp1) 84116133Sralph && subop != FFLOAT && subop != DFLOAT 84216133Sralph && subop != GFLOAT && subop != HFLOAT 84316133Sralph && ((!indexa(cp1) && !autoid(cp1)) || lhssiz == subop) 8441496Sbill && 0<=(r=isreg(cp2)) && r<NUSE 84517237Ssam && bitsize[subop]>=(pos+siz) 8461496Sbill && bitsize[p->back->subop>>4]>=(pos+siz)) {/* good CVT */ 8471496Sbill cp1=regs[RT1]; cp2=src; while (*cp2++= *cp1++); 8481496Sbill delnode(p->back); 8491496Sbill } 8501496Sbill } 8511496Sbill /* 'pos', 'siz' known; source of field is in 'src' */ 8521496Sbill splitrand(p); /* retrieve destination of BICL */ 85316133Sralph if ((siz==8 || siz==16) && pos==0) { 85416133Sralph p->combop = T(MOVZ,U(lhssiz,LONG)); 8551496Sbill sprintf(line,"%s,%s",src,lastrand); 8561496Sbill } else { 8571496Sbill p->combop = T(EXTZV,LONG); 8581496Sbill sprintf(line,"$%d,$%d,%s,%s",pos,siz,src,lastrand); 8591496Sbill } 8601496Sbill p->pop=0; 8611496Sbill p->code = copy(line); nfield++; return(p); 8621496Sbill }/* end EXTZV possibility */ 8631496Sbill }/* end low order bits */ 8641496Sbill /* unfortunately, INSV clears the condition codes, thus cannot be used */ 8651496Sbill /* else {/* see if BICL2 of positive field should be INSV $0 */ 8661496Sbill /* if (p->subop==(LONG | (OP2<<4)) && 6<=(pos+siz)) { 8671496Sbill /* p->combop = INSV; 8681496Sbill /* sprintf(line,"$0,$%d,$%d,%s",pos,siz,lastrand); 8691496Sbill /* p->code = copy(line); nfield++; return(p); 8701496Sbill /* } 8711496Sbill /* } 8721496Sbill */ 8731496Sbill return(p); 8741496Sbill } 8751496Sbill 8761496Sbill jumpsw() 8771496Sbill { 8781496Sbill register struct node *p, *p1; 8791496Sbill register t; 8801496Sbill int nj; 8811496Sbill 8821496Sbill t = 0; 8831496Sbill nj = 0; 8841496Sbill for (p=first.forw; p!=0; p = p->forw) 8851496Sbill p->seq = ++t; 8861496Sbill for (p=first.forw; p!=0; p = p1) { 8871496Sbill p1 = p->forw; 8881496Sbill if (p->op == CBR && p1->op==JBR && p->ref && p1->ref 8891496Sbill && abs(p->seq - p->ref->seq) > abs(p1->seq - p1->ref->seq)) { 8901496Sbill if (p->ref==p1->ref) 8911496Sbill continue; 8921496Sbill p->subop = revbr[p->subop]; 8931496Sbill p->pop=0; 8941496Sbill t = p1->ref; 8951496Sbill p1->ref = p->ref; 8961496Sbill p->ref = t; 8971496Sbill t = p1->labno; 8981496Sbill p1->labno = p->labno; 8991496Sbill p->labno = t; 9001496Sbill #ifdef COPYCODE 9011496Sbill if (p->labno == 0) { 9021496Sbill t = p1->code; p1->code = p->code; p->code = t; 9031496Sbill } 9041496Sbill #endif 9051496Sbill nrevbr++; 9061496Sbill nj++; 9071496Sbill } 9081496Sbill } 9091496Sbill return(nj); 9101496Sbill } 9111496Sbill 9121496Sbill addsob() 9131496Sbill { 9141496Sbill register struct node *p, *p1, *p2, *p3; 9151496Sbill 9161496Sbill for (p = &first; (p1 = p->forw)!=0; p = p1) { 9171496Sbill if (p->combop==T(DEC,LONG) && p1->op==CBR) { 9183933Sroot if (abs(p->seq - p1->ref->seq) > 8) continue; 9191496Sbill if (p1->subop==JGE || p1->subop==JGT) { 9201496Sbill if (p1->subop==JGE) p->combop=SOBGEQ; else p->combop=SOBGTR; 9211496Sbill p->pop=0; 9221496Sbill p->labno = p1->labno; delnode(p1); nsob++; 9231496Sbill } 9241496Sbill } else if (p->combop==T(INC,LONG)) { 9251496Sbill if (p1->op==LABEL && p1->refc==1 && p1->forw->combop==T(CMP,LONG) 9261496Sbill && (p2=p1->forw->forw)->combop==T(CBR,JLE) 9271496Sbill && (p3=p2->ref->back)->combop==JBR && p3->ref==p1 9281496Sbill && p3->forw->op==LABEL && p3->forw==p2->ref) { 9291496Sbill /* change INC LAB: CMP to LAB: INC CMP */ 9301496Sbill p->back->forw=p1; p1->back=p->back; 9311496Sbill p->forw=p1->forw; p1->forw->back=p; 9321496Sbill p->back=p1; p1->forw=p; 9331496Sbill p1=p->forw; 9341496Sbill /* adjust beginning value by 1 */ 9351496Sbill p2=alloc(sizeof first); p2->combop=T(DEC,LONG); 9361496Sbill p2->pop=0; 9371496Sbill p2->forw=p3; p2->back=p3->back; p3->back->forw=p2; 9381496Sbill p3->back=p2; p2->code=p->code; p2->labno=0; 9391496Sbill } 9401496Sbill if (p1->combop==T(CMP,LONG) && (p2=p1->forw)->op==CBR) { 9411496Sbill register char *cp1,*cp2; 9421496Sbill splitrand(p1); if (!equstr(p->code,regs[RT1])) continue; 9433933Sroot if (abs(p->seq - p2->ref->seq)>8) {/* outside byte displ range */ 9441496Sbill if (p2->subop!=JLE) continue; 9451496Sbill p->combop=T(ACB,LONG); 9461496Sbill cp2=regs[RT1]; cp1=regs[RT2]; while (*cp2++= *cp1++); /* limit */ 9471496Sbill cp2=regs[RT2]; cp1="$1"; while (*cp2++= *cp1++); /* increment */ 9481496Sbill cp2=regs[RT3]; cp1=p->code; while (*cp2++= *cp1++); /* index */ 9491496Sbill p->pop=0; newcode(p); 9501496Sbill p->labno = p2->labno; delnode(p2); delnode(p1); nsob++; 9511496Sbill } else if (p2->subop==JLE || p2->subop==JLT) { 9521496Sbill if (p2->subop==JLE) p->combop=AOBLEQ; else p->combop=AOBLSS; 9531496Sbill cp2=regs[RT1]; cp1=regs[RT2]; while (*cp2++= *cp1++); /* limit */ 9541496Sbill cp2=regs[RT2]; cp1=p->code; while (*cp2++= *cp1++); /* index */ 9551496Sbill p->pop=0; newcode(p); 9561496Sbill p->labno = p2->labno; delnode(p2); delnode(p1); nsob++; 9571496Sbill } 9581496Sbill } 9591496Sbill } 9601496Sbill } 9611496Sbill } 9621496Sbill 9631496Sbill abs(x) 9641496Sbill { 9651496Sbill return(x<0? -x: x); 9661496Sbill } 9671496Sbill 9681496Sbill equop(p1, p2) 9691496Sbill register struct node *p1; 9701496Sbill struct node *p2; 9711496Sbill { 9721496Sbill register char *cp1, *cp2; 9731496Sbill 9741496Sbill if (p1->combop != p2->combop) 9751496Sbill return(0); 9761496Sbill if (p1->op>0 && p1->op<MOV) 9771496Sbill return(0); 9781496Sbill if (p1->op==MOVA && p1->labno!=p2->labno) return(0); 9791496Sbill cp1 = p1->code; 9801496Sbill cp2 = p2->code; 9811496Sbill if (cp1==0 && cp2==0) 9821496Sbill return(1); 9831496Sbill if (cp1==0 || cp2==0) 9841496Sbill return(0); 9851496Sbill while (*cp1 == *cp2++) 9861496Sbill if (*cp1++ == 0) 9871496Sbill return(1); 9881496Sbill return(0); 9891496Sbill } 9901496Sbill 9911496Sbill delnode(p) register struct node *p; { 9921496Sbill p->back->forw = p->forw; 9931496Sbill p->forw->back = p->back; 9941496Sbill } 9951496Sbill 9961496Sbill decref(p) 9971496Sbill register struct node *p; 9981496Sbill { 9991496Sbill if (p && --p->refc <= 0) { 10001496Sbill nrlab++; 10011496Sbill delnode(p); 10021496Sbill } 10031496Sbill } 10041496Sbill 10051496Sbill struct node * 10061496Sbill nonlab(ap) 10071496Sbill struct node *ap; 10081496Sbill { 10091496Sbill register struct node *p; 10101496Sbill 10111496Sbill p = ap; 10121496Sbill while (p && p->op==LABEL) 10131496Sbill p = p->forw; 10141496Sbill return(p); 10151496Sbill } 10161496Sbill 10171496Sbill clearuse() { 10181496Sbill register struct node **i; 10191496Sbill for (i=uses+NUSE; i>uses;) *--i=0; 10201496Sbill } 10211496Sbill 10221496Sbill clearreg() { 10231496Sbill register short **i; 10241496Sbill for (i=regs+NREG; i>regs;) **--i=0; 10251496Sbill conloc[0] = 0; ccloc[0] = 0; 10261496Sbill } 10271496Sbill 10281496Sbill savereg(ai, s, type) 10291496Sbill register char *s; 10301496Sbill { 10311496Sbill register char *p, *sp; 10321496Sbill 10331496Sbill sp = p = regs[ai]; 10341496Sbill if (source(s)) /* side effects in addressing */ 10351496Sbill return; 10361496Sbill /* if any indexing, must be parameter or local */ 10371496Sbill /* indirection (as in "*-4(fp)") is ok, however */ 10381496Sbill *p++ = type; 10391496Sbill while (*p++ = *s) 10401496Sbill if (*s=='[' || *s++=='(' && *s!='a' && *s!='f') {*sp = 0; return;} 10411496Sbill } 10421496Sbill 10431496Sbill dest(s,type) 10441496Sbill register char *s; 10451496Sbill { 10461496Sbill register int i; 10471496Sbill 10481496Sbill source(s); /* handle addressing side effects */ 104917210Sralph if (!natural(s)) { 105017210Sralph /* wild store, everything except constants vanishes */ 105117210Sralph for (i=NREG; --i>=0;) 105217210Sralph if (regs[i][1] != '$') *(short *)(regs[i]) = 0; 105317210Sralph conloc[0] = 0; ccloc[0] = 0; 105417210Sralph return; 105517210Sralph } 10561496Sbill if ((i = isreg(s)) >= 0) { 10571496Sbill *(short *)(regs[i]) = 0; /* if register destination, that reg is a goner */ 10585794Sroot switch(type & 0xF){ 10595794Sroot case DFLOAT: /* clobber two at once */ 10605794Sroot /*FALLTHROUGH*/ 10615794Sroot case GFLOAT: 10625794Sroot *(short *)(regs[i+1]) = 0; 10635794Sroot break; 10645794Sroot case HFLOAT: /* clobber four at once */ 10655794Sroot *(short *)(regs[i+1]) = 0; 10665794Sroot *(short *)(regs[i+2]) = 0; 10675794Sroot *(short *)(regs[i+3]) = 0; 10685794Sroot break; 10695794Sroot } 10705794Sroot switch((type>>4)&0xF){ 10715794Sroot case DFLOAT: /* clobber two at once */ 10725794Sroot /*FALLTHROUGH*/ 10735794Sroot case GFLOAT: 10745794Sroot *(short *)(regs[i+1]) = 0; 10755794Sroot break; 10765794Sroot case HFLOAT: /* clobber four at once */ 10775794Sroot *(short *)(regs[i+1]) = 0; 10785794Sroot *(short *)(regs[i+2]) = 0; 10795794Sroot *(short *)(regs[i+3]) = 0; 10805794Sroot break; 10815794Sroot } 10825794Sroot /* 10835794Sroot if (DFLOAT==(type&0xF) || DFLOAT==((type>>4)&0xF)) 10845794Sroot *(short *)(regs[i+1]) = 0; 10855794Sroot */ 10861496Sbill } 10871496Sbill for (i=NREG; --i>=0;) 10881496Sbill if (regs[i][1]=='*' && equstr(s, regs[i]+2)) 10891496Sbill *(short *)(regs[i]) = 0; /* previous indirection through destination is invalid */ 10901496Sbill while ((i = findrand(s,0)) >= 0) /* previous values of destination are invalid */ 10911496Sbill *(short *)(regs[i]) = 0; 109217210Sralph if (*conloc && equstr(conloc, s)) 109317210Sralph conloc[0] = 0; 109417210Sralph setcc(s, type); /* natural destinations set condition codes */ 10951496Sbill } 10961496Sbill 109717210Sralph /* separate operands at commas, set up 'regs' and 'lastrand' */ 10981496Sbill splitrand(p) struct node *p; { 109917210Sralph register char *p1, *p2; 110017210Sralph register char **preg; 110117210Sralph 110217210Sralph preg = regs+RT1; 110317210Sralph if (p1 = p->code) 110417210Sralph while (*p1) { 110517210Sralph lastrand = p2 = *preg++; 110617210Sralph while (*p1) 110717210Sralph if (',' == (*p2++ = *p1++)) { 110817210Sralph --p2; 110917210Sralph break; 111017210Sralph } 111117210Sralph *p2 = 0; 111217210Sralph } 111317210Sralph while (preg < (regs+RT1+5)) 111417210Sralph *(*preg++) = 0; 11151496Sbill } 11161496Sbill 11171496Sbill compat(have, want) { 11181496Sbill register int hsrc, hdst; 11191496Sbill if (0==(want &= 0xF)) return(1); /* anything satisfies a wildcard want */ 11201496Sbill hsrc=have&0xF; if (0==(hdst=((have>>4)&0xF)) || hdst>=OP2) hdst=hsrc; 11215794Sroot if (want>=FFLOAT) return(hdst==want && hsrc==want); 11225794Sroot /* FLOAT, DFLOAT not compat: rounding */ 11235794Sroot return(hsrc>=want && hdst>=want && hdst<FFLOAT); 11241496Sbill } 11251496Sbill 11261496Sbill equtype(t1,t2) {return(compat(t1,t2) && compat(t2,t1));} 11271496Sbill 11281496Sbill findrand(as, type) 11291496Sbill char *as; 11301496Sbill { 11311496Sbill register char **i; 11321496Sbill for (i = regs+NREG; --i>=regs;) { 11331496Sbill if (**i && equstr(*i+1, as) && compat(**i,type)) 11341496Sbill return(i-regs); 11351496Sbill } 11361496Sbill return(-1); 11371496Sbill } 11381496Sbill 11391496Sbill isreg(s) 11401496Sbill register char *s; 11411496Sbill { 11421496Sbill if (*s++!='r' || !isdigit(*s++)) return(-1); 11431496Sbill if (*s==0) return(*--s-'0'); 11441496Sbill if (*(s-1)=='1' && isdigit(*s++) && *s==0) return(10+*--s-'0'); 11451496Sbill return(-1); 11461496Sbill } 11471496Sbill 11481496Sbill check() 11491496Sbill { 11501496Sbill register struct node *p, *lp; 11511496Sbill 11521496Sbill lp = &first; 11531496Sbill for (p=first.forw; p!=0; p = p->forw) { 11541496Sbill if (p->back != lp) 11551496Sbill abort(-1); 11561496Sbill lp = p; 11571496Sbill } 11581496Sbill } 11591496Sbill 11601496Sbill source(ap) 11611496Sbill char *ap; 11621496Sbill { 11631496Sbill register char *p1, *p2; 11641496Sbill 11651496Sbill p1 = ap; 11661496Sbill p2 = p1; 11671496Sbill if (*p1==0) 11681496Sbill return(0); 11691496Sbill while (*p2++ && *(p2-1)!='['); 11701496Sbill if (*p1=='-' && *(p1+1)=='(' 11711496Sbill || *p1=='*' && *(p1+1)=='-' && *(p1+2)=='(' 11721496Sbill || *(p2-2)=='+') { 11731496Sbill while (*p1 && *p1++!='r'); 11741496Sbill if (isdigit(*p1++)) 11751496Sbill if (isdigit(*p1)) *(short *)(regs[10+*p1-'0'])=0; 11761496Sbill else *(short *)(regs[*--p1-'0'])=0; 11771496Sbill return(1); 11781496Sbill } 11791496Sbill return(0); 11801496Sbill } 11811496Sbill 11821496Sbill newcode(p) struct node *p; { 11831496Sbill register char *p1,*p2,**preg; 11841496Sbill preg=regs+RT1; p2=line; 11851496Sbill while (*(p1= *preg++)) {while (*p2++= *p1++); *(p2-1)=',';} 11861496Sbill *--p2=0; 11871496Sbill p->code=copy(line); 11881496Sbill } 11891496Sbill 11901496Sbill repladdr(p) 11911496Sbill struct node *p; 11921496Sbill { 11931496Sbill register r; 11941496Sbill register char *p1, *p2; 11951496Sbill char **preg; int nrepl; 11961496Sbill 11971496Sbill preg=regs+RT1; nrepl=0; 11981496Sbill while (lastrand!=(p1= *preg++)) 11991496Sbill if (!source(p1) && 0<=(r=findrand(p1,p->subop))) { 12001496Sbill *p1++='r'; if (r>9) {*p1++='1'; r -= 10;} *p1++=r+'0'; *p1=0; 12011496Sbill nrepl++; nsaddr++; 12021496Sbill } 12031496Sbill if (nrepl) newcode(p); 12041496Sbill } 12051496Sbill 12061496Sbill /* movedat() 12071496Sbill /* { 12081496Sbill /* register struct node *p1, *p2; 12091496Sbill /* struct node *p3; 12101496Sbill /* register seg; 12111496Sbill /* struct node data; 12121496Sbill /* struct node *datp; 12131496Sbill /* 12141496Sbill /* if (first.forw == 0) 12151496Sbill /* return; 12161496Sbill /* datp = &data; 12171496Sbill /* for (p1 = first.forw; p1!=0; p1 = p1->forw) { 12181496Sbill /* if (p1->op == DATA) { 12191496Sbill /* p2 = p1->forw; 12201496Sbill /* while (p2 && p2->op!=TEXT) 12211496Sbill /* p2 = p2->forw; 12221496Sbill /* if (p2==0) 12231496Sbill /* break; 12241496Sbill /* p3 = p1->back; 12251496Sbill /* p1->back->forw = p2->forw; 12261496Sbill /* p2->forw->back = p3; 12271496Sbill /* p2->forw = 0; 12281496Sbill /* datp->forw = p1; 12291496Sbill /* p1->back = datp; 12301496Sbill /* p1 = p3; 12311496Sbill /* datp = p2; 12321496Sbill /* } 12331496Sbill /* } 12341496Sbill /* if (data.forw) { 12351496Sbill /* datp->forw = first.forw; 12361496Sbill /* first.forw->back = datp; 12371496Sbill /* data.forw->back = &first; 12381496Sbill /* first.forw = data.forw; 12391496Sbill /* } 12401496Sbill /* seg = -1; 12411496Sbill /* for (p1 = first.forw; p1!=0; p1 = p1->forw) { 12421496Sbill /* if (p1->op==TEXT||p1->op==DATA||p1->op==BSS) { 12431496Sbill /* if (p1->op == seg || p1->forw&&p1->forw->op==seg) { 12441496Sbill /* p1->back->forw = p1->forw; 12451496Sbill /* p1->forw->back = p1->back; 12461496Sbill /* p1 = p1->back; 12471496Sbill /* continue; 12481496Sbill /* } 12491496Sbill /* seg = p1->op; 12501496Sbill /* } 12511496Sbill /* } 12521496Sbill /* } 12531496Sbill */ 12541496Sbill 12551496Sbill redunbr(p) 12561496Sbill register struct node *p; 12571496Sbill { 12581496Sbill register struct node *p1; 12591496Sbill register char *ap1; 12601496Sbill char *ap2; 12611496Sbill 12621496Sbill if ((p1 = p->ref) == 0) 12631496Sbill return; 12641496Sbill p1 = nonlab(p1); 12651496Sbill if (p1->op==TST) { 12661496Sbill splitrand(p1); 12671496Sbill savereg(RT2, "$0", p1->subop); 12681496Sbill } else if (p1->op==CMP) 12691496Sbill splitrand(p1); 12701496Sbill else 12711496Sbill return; 12721496Sbill if (p1->forw->op==CBR) { 12731496Sbill ap1 = findcon(RT1, p1->subop); 12741496Sbill ap2 = findcon(RT2, p1->subop); 12751496Sbill p1 = p1->forw; 12761496Sbill if (compare(p1->subop, ap1, ap2)) { 12771496Sbill nredunj++; 12781496Sbill nchange++; 12791496Sbill decref(p->ref); 12801496Sbill p->ref = p1->ref; 12811496Sbill p->labno = p1->labno; 12821496Sbill #ifdef COPYCODE 12831496Sbill if (p->labno == 0) 12841496Sbill p->code = p1->code; 128517210Sralph #endif 12861496Sbill if (p->ref) 12871496Sbill p->ref->refc++; 12881496Sbill } 12891496Sbill } else if (p1->op==TST && equstr(regs[RT1],ccloc+1) && 12901496Sbill equtype(ccloc[0],p1->subop)) { 12911496Sbill p1=insertl(p1->forw); decref(p->ref); p->ref=p1; 12921496Sbill nrtst++; nchange++; 12931496Sbill } 12941496Sbill } 12951496Sbill 12961496Sbill char * 12971496Sbill findcon(i, type) 12981496Sbill { 12991496Sbill register char *p; 13001496Sbill register r; 13011496Sbill 13021496Sbill p = regs[i]; 13031496Sbill if (*p=='$') 13041496Sbill return(p); 13051496Sbill if ((r = isreg(p)) >= 0 && compat(regs[r][0],type)) 13061496Sbill return(regs[r]+1); 13071496Sbill if (equstr(p, conloc)) 13081496Sbill return(conval+1); 13091496Sbill return(p); 13101496Sbill } 13111496Sbill 13121496Sbill compare(op, acp1, acp2) 13131496Sbill char *acp1, *acp2; 13141496Sbill { 13151496Sbill register char *cp1, *cp2; 13161496Sbill register n1; 13171496Sbill int n2; int sign; 13181496Sbill 13191496Sbill cp1 = acp1; 13201496Sbill cp2 = acp2; 13211496Sbill if (*cp1++ != '$' || *cp2++ != '$') 13221496Sbill return(0); 13231496Sbill n1 = 0; sign=1; if (*cp2=='-') {++cp2; sign= -1;} 13241496Sbill while (isdigit(*cp2)) {n1 *= 10; n1 += (*cp2++ - '0')*sign;} 13251496Sbill n2 = n1; 13261496Sbill n1 = 0; sign=1; if (*cp1=='-') {++cp1; sign= -1;} 13271496Sbill while (isdigit(*cp1)) {n1 *= 10; n1 += (*cp1++ - '0')*sign;} 13281496Sbill if (*cp1=='+') 13291496Sbill cp1++; 13301496Sbill if (*cp2=='+') 13311496Sbill cp2++; 13321496Sbill do { 13331496Sbill if (*cp1++ != *cp2) 13341496Sbill return(0); 13351496Sbill } while (*cp2++); 13361496Sbill cp1 = n1; 13371496Sbill cp2 = n2; 13381496Sbill switch(op) { 13391496Sbill 13401496Sbill case JEQ: 13411496Sbill return(cp1 == cp2); 13421496Sbill case JNE: 13431496Sbill return(cp1 != cp2); 13441496Sbill case JLE: 13451496Sbill return(((int)cp1) <= ((int)cp2)); 13461496Sbill case JGE: 13471496Sbill return(((int)cp1) >= ((int)cp2)); 13481496Sbill case JLT: 13491496Sbill return(((int)cp1) < ((int)cp2)); 13501496Sbill case JGT: 13511496Sbill return(((int)cp1) > ((int)cp2)); 13521496Sbill case JLO: 13531496Sbill return(cp1 < cp2); 13541496Sbill case JHI: 13551496Sbill return(cp1 > cp2); 13561496Sbill case JLOS: 13571496Sbill return(cp1 <= cp2); 13581496Sbill case JHIS: 13591496Sbill return(cp1 >= cp2); 13601496Sbill } 13611496Sbill return(0); 13621496Sbill } 13631496Sbill 13641496Sbill setcon(cv, cl, type) 13651496Sbill register char *cv, *cl; 13661496Sbill { 13671496Sbill register char *p; 13681496Sbill 13691496Sbill if (*cv != '$') 13701496Sbill return; 13711496Sbill if (!natural(cl)) 13721496Sbill return; 13731496Sbill p = conloc; 13741496Sbill while (*p++ = *cl++); 13751496Sbill p = conval; 13761496Sbill *p++ = type; 13771496Sbill while (*p++ = *cv++); 13781496Sbill } 13791496Sbill 13801496Sbill equstr(p1, p2) 13811496Sbill register char *p1, *p2; 13821496Sbill { 13831496Sbill do { 13841496Sbill if (*p1++ != *p2) 13851496Sbill return(0); 13861496Sbill } while (*p2++); 13871496Sbill return(1); 13881496Sbill } 13891496Sbill 13901496Sbill setcc(ap,type) 13911496Sbill char *ap; 13921496Sbill { 13931496Sbill register char *p, *p1; 13941496Sbill 13951496Sbill p = ap; 13961496Sbill if (!natural(p)) { 13971496Sbill ccloc[0] = 0; 13981496Sbill return; 13991496Sbill } 14001496Sbill p1 = ccloc; 14011496Sbill *p1++ = type; 14021496Sbill while (*p1++ = *p++); 14031496Sbill } 14041496Sbill 14051496Sbill okio(p) register char *p; {/* 0->probable I/O space address; 1->not */ 14061496Sbill if (ioflag && (!natural(p) || 0>getnum(p))) return(0); 14071496Sbill return(1); 14081496Sbill } 14091496Sbill 14101496Sbill indexa(p) register char *p; {/* 1-> uses [r] addressing mode; 0->doesn't */ 14111496Sbill while (*p) if (*p++=='[') return(1); 14121496Sbill return(0); 14131496Sbill } 14141496Sbill 14151496Sbill natural(p) 14161496Sbill register char *p; 14171496Sbill {/* 1->simple local, parameter, global, or register; 0->otherwise */ 141817210Sralph if (*p=='*' || *p=='(' || *p=='-'&&p[1]=='(' || *p=='$'&&getnum(p+1)) 14191496Sbill return(0); 14201496Sbill while (*p++); 14211496Sbill p--; 142217210Sralph if (*--p=='+' || *p==']' || *p==')' && p[-2]!='a' && p[-2]!='f') 14231496Sbill return(0); 14241496Sbill return(1); 14251496Sbill } 14261496Sbill 14271496Sbill /* 14281496Sbill ** Tell if an argument is most likely static. 14291496Sbill */ 14301496Sbill 14311496Sbill isstatic(cp) 14321496Sbill register char *cp; 14331496Sbill { 14341496Sbill if (*cp == '_' || *cp == 'L' || (*cp++ == 'v' && *cp == '.')) 14351496Sbill return (1); 14361496Sbill return (0); 14371496Sbill } 143816133Sralph 143916133Sralph autoid(p) register char *p; {/* 1-> uses autoincrement/autodecrement; 0->doesn't */ 144016133Sralph if (*p == '-' && *(p+1) == '(') return(1); 144116133Sralph while (*p) p++; 144216133Sralph if (*--p == '+' && *--p == ')') return(1); 144316133Sralph return(0); 144416133Sralph } 1445