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