1*1506Sbill static char sccsid[] = "@(#)c21.c 4.3 10/18/80"; 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}; 151496Sbill int bitsize[4] = {0,8,16,32}; /* index by type codes */ 161496Sbill int pos,siz; long f; /* for bit field communication */ 171496Sbill struct node *uses[NUSE]; /* for backwards flow analysis */ 181496Sbill char *lastrand; /* last operand of instruction */ 191496Sbill struct node *bflow(); 201496Sbill struct node *bicopt(); 211496Sbill char *findcon(); 221496Sbill 231496Sbill redun3(p,split) register struct node *p; int split; { 241496Sbill /* check for 3 addr instr which should be 2 addr */ 251496Sbill if (OP3==((p->subop>>4)&0xF)) { 261496Sbill if (split) splitrand(p); 271496Sbill if (equstr(regs[RT1],regs[RT3]) 281496Sbill && (p->op==ADD || p->op==MUL || p->op==BIS || p->op==XOR)) { 291496Sbill register char *t=regs[RT1]; regs[RT1]=regs[RT2]; regs[RT2]=t; 301496Sbill } 311496Sbill if (equstr(regs[RT2],regs[RT3])) { 321496Sbill p->subop=(p->subop&0xF)|(OP2<<4); p->pop=0; 331496Sbill lastrand=regs[RT2]; *regs[RT3]=0; return(1); 341496Sbill } 351496Sbill } return(0); 361496Sbill } 371496Sbill 381496Sbill bmove() { 391496Sbill register struct node *p, *lastp; register char *cp1,*cp2; register int r; 401496Sbill refcount(); 411496Sbill for (p=lastp= &first; 0!=(p=p->forw); lastp=p); 421496Sbill clearreg(); clearuse(); 431496Sbill for (p=lastp; p!= &first; p=p->back) { 441496Sbill if (debug) { 451496Sbill printf("Uses:\n"); 461496Sbill for (r=NUSE;--r>=0;) if (uses[r]) 471496Sbill printf("%d: %s\n",r,uses[r]->code? uses[r]->code:""); 481496Sbill printf("-\n"); 491496Sbill } 501496Sbill r=(p->subop>>4)&0xF; 511496Sbill if (OP2==r && (cp1=p->code, *cp1++)=='$' && *cp1++=='0' && *cp1++==',' && 521496Sbill !source(cp1)) {/* a no-op unless MUL or DIV */ 531496Sbill if (p->op==MUL) {p->op=MOV; p->subop&=0xF; p->pop=0;} 541496Sbill else if (p->op==DIV) fprintf(stderr,"c2: zero divide\n"); 551496Sbill else {delnode(p); redunm++; continue;} 561496Sbill } 571496Sbill if (OP3==r && 0!=redun3(p,1)) {newcode(p); redunm++;} 581496Sbill switch (p->op) { 591496Sbill case LABEL: case DLABEL: 601496Sbill for (r=NUSE; --r>=0;) 611496Sbill if (uses[r]) p->ref=(struct node *) (((int)p->ref)|biti[r]); 621496Sbill break; 631496Sbill case CALLS: 641496Sbill clearuse(); goto std; 651496Sbill case 0: 661496Sbill clearuse(); break; 671496Sbill case SUB: 681496Sbill if ((p->subop&0xF)!=LONG) goto std; cp1=p->code; 691496Sbill if (*cp1++!='$') goto std; splitrand(p); 701496Sbill if (equstr(regs[RT2],"fp") && !indexa(regs[RT1])) {/* address comp. */ 711496Sbill char buf[50]; cp2=buf; *cp2++='-'; 721496Sbill cp1=regs[RT1]+1; while (*cp2++= *cp1++); --cp2; 731496Sbill cp1="(fp),"; while (*cp2++= *cp1++); --cp2; 741496Sbill cp1=regs[RT3]; while (*cp2++= *cp1++); 751496Sbill p->code=copy(buf); p->combop=T(MOVA,LONG); p->pop=0; 761496Sbill } else if (*cp1++=='-' && 0<=(r=getnum(cp1))) { 771496Sbill p->op=ADD; p->pop=0; *--cp1='$'; p->code=cp1; 781496Sbill } goto std; 791496Sbill case ADD: 801496Sbill if ((p->subop&0xF)!=LONG) goto std; cp1=p->code; 811496Sbill if (*cp1++!='$') goto std; splitrand(p); 821496Sbill if (isstatic(cp1) && (r=isreg(regs[RT2]))>=0 && r<NUSE && uses[r]==p->forw) 831496Sbill { 841496Sbill /* address comp: 851496Sbill ** addl2 $_foo,r0 \ movab _foo[r0],bar 861496Sbill ** movl r0,bar / 871496Sbill */ 881496Sbill register struct node *pnext = p->forw; 891496Sbill char buf[50]; 901496Sbill 911496Sbill if (pnext->op == MOV && pnext->subop == LONG) 921496Sbill { 931496Sbill cp1 = ®s[RT1][1]; cp2 = &buf[0]; 941496Sbill while (*cp2++ = *cp1++) ; cp2--; 951496Sbill splitrand(pnext); 961496Sbill if (r == isreg(regs[RT1])) 971496Sbill { 981496Sbill delnode(p); p = pnext; 991496Sbill p->op = MOVA; p->subop = BYTE; 1001496Sbill p->pop = 0; 1011496Sbill cp1 = regs[RT1]; *cp2++ = '['; 1021496Sbill while (*cp2++ = *cp1++) ; cp2--; 1031496Sbill *cp2++ = ']'; *cp2++ = ','; 1041496Sbill cp1 = regs[RT2]; 1051496Sbill while (*cp2++ = *cp1++) ; 1061496Sbill p->code = copy(buf); 1071496Sbill } 1081496Sbill } 1091496Sbill } 1101496Sbill else 1111496Sbill if (equstr(regs[RT2],"fp") && !indexa(regs[RT1])) {/* address comp. */ 1121496Sbill cp2=cp1-1; cp1=regs[RT1]+1; while (*cp2++= *cp1++); --cp2; 1131496Sbill cp1="(fp)"; while (*cp2++= *cp1++); *--cp2=','; 1141496Sbill p->combop=T(MOVA,LONG); p->pop=0; 1151496Sbill } else if (*cp1++=='-' && 0<=(r=getnum(cp1))) { 1161496Sbill p->op=SUB; p->pop=0; *--cp1='$'; p->code=cp1; 1171496Sbill } 1181496Sbill /* fall thru ... */ 1191496Sbill case CASE: 1201496Sbill default: std: 1211496Sbill p=bflow(p); break; 1221496Sbill case MUL: 1231496Sbill { 1241496Sbill /* 1251496Sbill ** Change multiplication by constant powers of 2 to 1261496Sbill ** shifts. 1271496Sbill */ 1281496Sbill splitrand(p); 1291496Sbill if (regs[RT1][0] != '$' || regs[RT1][1] == '-') goto std; 1301496Sbill if ((r = ispow2(getnum(®s[RT1][1]))) < 0) goto std; 1311496Sbill switch (r) 1321496Sbill { 1331496Sbill case 0: /* mull3 $1,x,y */ 1341496Sbill if (p->subop == U(LONG,OP3)) 1351496Sbill { 1361496Sbill if (equstr(regs[RT2], regs[RT3])) 1371496Sbill { 1381496Sbill delnode(p); p = p->forw; 1391496Sbill } 1401496Sbill else 1411496Sbill { 1421496Sbill p->op = MOV; p->subop = LONG; 1431496Sbill p->pop = 0; newcode(p); nchange++; 1441496Sbill } 1451496Sbill } 1461496Sbill else 1471496Sbill if (p->subop == U(LONG,OP2)) 1481496Sbill { 1491496Sbill delnode(p); p = p->forw; 1501496Sbill } 1511496Sbill goto std; 1521496Sbill 1531496Sbill case 1: /* mull2 $2,x */ 1541496Sbill if (p->subop == U(LONG, OP2) && !source(regs[RT2])) 1551496Sbill { 1561496Sbill strcpy(regs[RT1], regs[RT2]); 1571496Sbill p->op = ADD; p->pop = 0; newcode(p); nchange++; 1581496Sbill } 1591496Sbill goto std; 1601496Sbill } 1611496Sbill if(p->subop==U(LONG,OP3)||(p->subop==U(LONG,OP2)&&!source(regs[RT2]))) 1621496Sbill { 1631496Sbill if (p->subop == U(LONG,OP2)) 1641496Sbill strcpy(regs[RT3], regs[RT2]); 1651496Sbill sprintf(regs[RT1], "$%d", r); 1661496Sbill p->op = ASH; p->subop = LONG; 1671496Sbill p->pop = 0; newcode(p); nchange++; 1681496Sbill } 1691496Sbill goto std; 1701496Sbill } 1711496Sbill case ASH: 1721496Sbill { 1731496Sbill /* address comp: 1741496Sbill ** ashl $1,bar,r0 \ movl bar,r0 1751496Sbill ** movab _foo[r0] / movaw _foo[r0] 1761496Sbill ** 1771496Sbill ** ashl $2,r0,r0 \ moval _foo[r0] 1781496Sbill ** movab _foo[r0] / 1791496Sbill */ 1801496Sbill register struct node *pf; 1811496Sbill register int shfrom, shto; 1821496Sbill long shcnt; 1831496Sbill char *regfrom; 1841496Sbill 1851496Sbill splitrand(p); 1861496Sbill if (regs[RT1][0] != '$') goto std; 1871496Sbill if ((shcnt = getnum(®s[RT1][1])) < 1 || shcnt > 3) goto std; 1881496Sbill if ((shfrom = isreg(regs[RT2])) >= 0) 1891496Sbill regfrom = copy(regs[RT2],"]"); 1901496Sbill if ((shto = isreg(regs[RT3])) >= 0 && shto<NUSE) 1911496Sbill { 1921496Sbill int regnum; 1931496Sbill 1941496Sbill if (uses[shto] != (pf = p->forw)) goto ashadd; 1951496Sbill if (pf->op != MOVA && pf->op != PUSHA) goto ashadd; 1961496Sbill if (pf->subop != BYTE) goto ashadd; 1971496Sbill splitrand(pf); 1981496Sbill if (!indexa(regs[RT1])) goto std; 1991496Sbill cp2 = regs[RT1]; 2001496Sbill if(!isstatic(cp2)) goto std; 2011496Sbill while (*cp2++ != '[') ; 2021496Sbill if (*cp2++ != 'r' || !isdigit(*cp2)) goto std; 2031496Sbill regnum = *cp2++ - '0'; 2041496Sbill if (isdigit(*cp2)) 2051496Sbill { 2061496Sbill if (cp2[1] != ']') goto std; 2071496Sbill regnum *= 10; regnum += *cp2 - '0'; 2081496Sbill } 2091496Sbill if (regnum != shto) goto std; 2101496Sbill if (shfrom >= 0) /* ashl $N,r*,r0 */ 2111496Sbill { 2121496Sbill delnode(p); 2131496Sbill if (shfrom != shto) 2141496Sbill { 2151496Sbill uses[shto] = NULL; splitrand(pf); 2161496Sbill cp2=regs[RT1]; while (*cp2++!='['); 2171496Sbill cp1=regfrom; while (*cp2++= *cp1++); 2181496Sbill newcode(pf); 2191496Sbill } 2201496Sbill } 2211496Sbill else 2221496Sbill { 2231496Sbill p->op = MOV; splitrand(p); 2241496Sbill strcpy(regs[RT1], regs[RT2]); 2251496Sbill strcpy(regs[RT2], regs[RT3]); 2261496Sbill regs[RT3][0] = '\0'; 2271496Sbill p->pop = 0; newcode(p); 2281496Sbill } 2291496Sbill switch (shcnt) 2301496Sbill { 2311496Sbill case 1: pf->subop = WORD; break; 2321496Sbill case 2: pf->subop = LONG; break; 2331496Sbill case 3: pf->subop = QUAD; break; 2341496Sbill } 2351496Sbill redunm++; nsaddr++; nchange++; 2361496Sbill } 2371496Sbill goto std; 2381496Sbill ashadd: 2391496Sbill /* at this point, RT2 and RT3 are guaranteed to be simple regs*/ 2401496Sbill if (shcnt == 1 && equstr(regs[RT2], regs[RT3])) 2411496Sbill { 2421496Sbill /* 2431496Sbill ** quickie: 2441496Sbill ** ashl $1,A,A > addl2 A,A 2451496Sbill */ 2461496Sbill p->op = ADD; p->subop = U(LONG,OP2); p->pop = 0; 2471496Sbill strcpy(regs[RT1], regs[RT2]); regs[RT3][0] = '\0'; 2481496Sbill newcode(p); nchange++; 2491496Sbill } 2501496Sbill goto std; 2511496Sbill } 2521496Sbill 2531496Sbill case EXTV: 2541496Sbill case EXTZV: 2551496Sbill { 2561496Sbill /* bit tests: 2571496Sbill ** extv A,$1,B,rC \ 2581496Sbill ** tstl rC > jbc A,B,D 2591496Sbill ** jeql D / 2601496Sbill ** 2611496Sbill ** also byte- and word-size fields: 2621496Sbill ** extv $n*8,$8,A,B > cvtbl n+A,B 2631496Sbill ** extv $n*16,$16,A,B > cvtwl n+A,B 2641496Sbill ** extzv $n*8,$8,A,B > movzbl n+A,B 2651496Sbill ** extzv $n*16,$16,A,B > movzwl n+A,B 2661496Sbill */ 2671496Sbill register struct node *pf; /* forward node */ 2681496Sbill register struct node *pn; /* next node (after pf) */ 2691496Sbill int flen; /* field length */ 2701496Sbill 2711496Sbill splitrand(p); 2721496Sbill if (regs[RT2][0] != '$') goto std; 2731496Sbill if ((flen = getnum(®s[RT2][1])) < 0) goto std; 2741496Sbill if (flen == 1) 2751496Sbill { 2761496Sbill register int extreg; /* reg extracted to */ 2771496Sbill 2781496Sbill extreg = isreg(regs[RT4]); 2791496Sbill if (extreg < 0 || extreg >= NUSE) goto std; 2801496Sbill if ((pf = p->forw)->op != TST) goto std; 2811496Sbill if (uses[extreg] && uses[extreg] != pf) goto std; 2821496Sbill splitrand(pf); 2831496Sbill if (extreg != isreg(regs[RT1])) goto std; 2841496Sbill if ((pn = pf->forw)->op != CBR) goto std; 2851496Sbill if (pn->subop != JEQ && pn->subop != JNE) goto std; 2861496Sbill delnode(p); delnode(pf); 2871496Sbill pn->subop = (pn->subop == JEQ) ? JBC : JBS; 2881496Sbill for(cp2=p->code; *cp2++!=',';); 2891496Sbill for(cp1=cp2; *cp1++!=',';); 2901496Sbill while (*cp1!=',') *cp2++= *cp1++; *cp2='\0'; 2911496Sbill pn->code = p->code; pn->pop = NULL; 2921496Sbill uses[extreg] = NULL; 2931496Sbill } 2941496Sbill else 2951496Sbill if (flen == 8 || flen == 16) 2961496Sbill { 2971496Sbill register int boff; /* bit offset */ 2981496Sbill register int coff; /* chunk (byte or word) offset*/ 2991496Sbill 3001496Sbill if (regs[RT1][0] != '$') goto std; 3011496Sbill if ((boff = getnum(®s[RT1][1])) < 0) goto std; 3021496Sbill coff = boff / flen; 3031496Sbill if (coff && (isreg(regs[RT3]) >= 0)) goto std; 3041496Sbill if (boff < 0 || (boff % flen) != 0) goto std; 3051496Sbill p->op = (p->op == EXTV) ? CVT : MOVZ; 3061496Sbill p->subop = U((flen == 8 ? BYTE : WORD), LONG); 3071496Sbill if (coff == 0) 3081496Sbill strcpy(regs[RT1], regs[RT3]); 3091496Sbill else 3101496Sbill sprintf(regs[RT1], "%d%s%s", coff, regs[RT3][0]=='(' ? "":"+", 3111496Sbill regs[RT3]); 3121496Sbill strcpy(regs[RT2], regs[RT4]); 3131496Sbill regs[RT3][0] = '\0'; regs[RT4][0] = '\0'; 3141496Sbill p->pop = 0; newcode(p); 3151496Sbill } 3161496Sbill nchange++; 3171496Sbill goto std; 3181496Sbill } 3191496Sbill 3201496Sbill case CMP: 3211496Sbill { 3221496Sbill /* comparison to -63 to -1: 3231496Sbill ** cmpl r0,$-1 > incl r0 3241496Sbill ** jeql ... 3251496Sbill ** 3261496Sbill ** cmpl r0,$-63 > addl2 $63,r0 3271496Sbill ** jeql ... 3281496Sbill */ 3291496Sbill register int num; 3301496Sbill register int reg; 3311496Sbill register struct node *regp = p->back; 3321496Sbill 3331496Sbill if (p->forw->op != CBR) goto std; 3341496Sbill if (p->forw->subop != JEQ && p->forw->subop != JNE) goto std; 3351496Sbill splitrand(p); 3361496Sbill if (strncmp(regs[RT2], "$-", 2) != 0) goto std; 3371496Sbill reg = r = isreg(regs[RT1]); 3381496Sbill if (r < 0) goto std; 3391496Sbill if (r < NUSE && uses[r] != 0) goto std; 3401496Sbill if (r >= NUSE && regp->op == MOV && p->subop == regp->subop) 3411496Sbill { 3421496Sbill if (*regp->code != 'r') goto std; 3431496Sbill reg = regp->code[1] - '0'; 3441496Sbill if (isdigit(regp->code[2]) || reg >= NUSE || uses[reg]) 3451496Sbill goto std; 3461496Sbill } 3471496Sbill if (r >= NUSE) goto std; 3481496Sbill if (reg != r) 3491496Sbill sprintf(regs[RT1], "r%d", reg); 3501496Sbill if ((num = getnum(®s[RT2][2])) <= 0 || num > 63) goto std; 3511496Sbill if (num == 1) 3521496Sbill { 3531496Sbill p->op = INC; regs[RT2][0] = '\0'; 3541496Sbill } 3551496Sbill else 3561496Sbill { 3571496Sbill register char *t; 3581496Sbill 3591496Sbill t=regs[RT1];regs[RT1]=regs[RT2];regs[RT2]=t; 3601496Sbill p->op = ADD; p->subop = U(p->subop, OP2); 3611496Sbill for (t = ®s[RT1][2]; t[-1] = *t; t++) ; 3621496Sbill } 3631496Sbill p->pop = 0; newcode(p); 3641496Sbill nchange++; 3651496Sbill goto std; 3661496Sbill } 3671496Sbill 3681496Sbill case JSB: 3691496Sbill if (equstr(p->code,"mcount")) {uses[0]=p; regs[0][0]= -1;} 3701496Sbill goto std; 3711496Sbill case JBR: case JMP: 3721496Sbill clearuse(); 3731496Sbill if (p->subop==RET || p->subop==RSB) {uses[0]=p; regs[0][0]= -1; break;} 3741496Sbill if (p->ref==0) goto std; /* jmp (r0) */ 3751496Sbill /* fall through */ 3761496Sbill case CBR: 3771496Sbill if (p->ref->ref!=0) for (r=NUSE;--r>=0;) 3781496Sbill if (biti[r] & (int)p->ref->ref) {uses[r]=p; regs[r][0]= -1;} 3791496Sbill case EROU: case JSW: 3801496Sbill case TEXT: case DATA: case BSS: case ALIGN: case WGEN: case END: ; 3811496Sbill } 3821496Sbill } 3831496Sbill for (p= &first; p!=0; p=p->forw) 3841496Sbill if (p->op==LABEL || p->op==DLABEL) p->ref=0; /* erase our tracks */ 3851496Sbill } 3861496Sbill 3871496Sbill rmove() 3881496Sbill { 3891496Sbill register struct node *p, *lastp; 3901496Sbill register int r; 3911496Sbill int r1; 3921496Sbill 3931496Sbill clearreg(); 3941496Sbill for (p=first.forw; p!=0; p = p->forw) { 3951496Sbill lastp=p; 3961496Sbill if (debug) { 3971496Sbill printf("Regs:\n"); 3981496Sbill for (r=0; r<NREG; r++) 3991496Sbill if (regs[r][0]) { 4001496Sbill r1=regs[r][0]; 4011496Sbill printf("%d: %d%d %s\n", r, r1&0xF, r1>>4, regs[r]+1); 4021496Sbill } 4031496Sbill printf("-\n"); 4041496Sbill } 4051496Sbill switch (p->op) { 4061496Sbill 4071496Sbill case CVT: 4081496Sbill splitrand(p); goto mov; 4091496Sbill 4101496Sbill case MOV: 4111496Sbill splitrand(p); 4121496Sbill if ((r = findrand(regs[RT1],p->subop)) >= 0) { 4131496Sbill if (r == isreg(regs[RT2]) && p->forw->op!=CBR) { 4141496Sbill delnode(p); redunm++; break; 4151496Sbill } 4161496Sbill } 4171496Sbill mov: 4181496Sbill repladdr(p); 4191496Sbill r = isreg(regs[RT1]); 4201496Sbill r1 = isreg(regs[RT2]); 4211496Sbill dest(regs[RT2],p->subop); 422*1506Sbill if (r>=0) { 423*1506Sbill if (r1>=0) savereg(r1, regs[r]+1, p->subop); 424*1506Sbill else if (p->op!=CVT) savereg(r, regs[RT2], p->subop); 425*1506Sbill } else if (r1>=0) savereg(r1, regs[RT1], p->subop); 426*1506Sbill else if (p->op!=CVT) setcon(regs[RT1], regs[RT2], p->subop); 4271496Sbill break; 4281496Sbill 4291496Sbill /* .rx,.wx */ 4301496Sbill case MFPR: 4311496Sbill case COM: 4321496Sbill case NEG: 4331496Sbill /* .rx,.wx or .rx,.rx,.wx */ 4341496Sbill case ADD: 4351496Sbill case SUB: 4361496Sbill case BIC: 4371496Sbill case BIS: 4381496Sbill case XOR: 4391496Sbill case MUL: 4401496Sbill case DIV: 4411496Sbill case ASH: 4421496Sbill case MOVZ: 4431496Sbill /* .rx,.rx,.rx,.wx */ 4441496Sbill case EXTV: 4451496Sbill case EXTZV: 4461496Sbill case INSV: 4471496Sbill splitrand(p); 4481496Sbill repladdr(p); 4491496Sbill dest(lastrand,p->subop); 4501496Sbill if (p->op==INSV) ccloc[0]=0; 4511496Sbill break; 4521496Sbill 4531496Sbill /* .mx or .wx */ 4541496Sbill case CLR: 4551496Sbill case INC: 4561496Sbill case DEC: 4571496Sbill splitrand(p); 4581496Sbill dest(lastrand,p->subop); 4591496Sbill if (p->op==CLR) 4601496Sbill if ((r = isreg(regs[RT1])) >= 0) 4611496Sbill savereg(r, "$0", p->subop); 4621496Sbill else 4631496Sbill setcon("$0", regs[RT1], p->subop); 4641496Sbill break; 4651496Sbill 4661496Sbill /* .rx */ 4671496Sbill case TST: 4681496Sbill case PUSH: 4691496Sbill splitrand(p); 4701496Sbill lastrand=regs[RT1+1]; /* fool repladdr into doing 1 operand */ 4711496Sbill repladdr(p); 4721496Sbill if (p->op==TST && equstr(lastrand=regs[RT1], ccloc+1) 4731496Sbill && ((0xf&(ccloc[0]>>4))==p->subop || equtype(ccloc[0],p->subop)) 4741496Sbill &&!source(lastrand)) { 4751496Sbill delnode(p); p = p->back; nrtst++; nchange++; 4761496Sbill } 4771496Sbill setcc(lastrand,p->subop); 4781496Sbill break; 4791496Sbill 4801496Sbill /* .rx,.rx,.rx */ 4811496Sbill case PROBER: 4821496Sbill case PROBEW: 4831496Sbill case CASE: 4841496Sbill case MOVC3: 4851496Sbill /* .rx,.rx */ 4861496Sbill case MTPR: 4871496Sbill case CALLS: 4881496Sbill case CMP: 4891496Sbill case BIT: 4901496Sbill splitrand(p); 4911496Sbill /* fool repladdr into doing right number of operands */ 4921496Sbill if (p->op==CASE || p->op==MOVC3 || p->op==PROBER || p->op==PROBEW) 4931496Sbill lastrand=regs[RT4]; 4941496Sbill else lastrand=regs[RT3]; 4951496Sbill repladdr(p); 4961496Sbill if (p->op==CALLS || p->op==MOVC3) clearreg(); 4971496Sbill if (p->op==BIT) bitopt(p); 4981496Sbill ccloc[0]=0; break; 4991496Sbill 5001496Sbill case CBR: 5011496Sbill if (p->subop>=JBC) { 5021496Sbill splitrand(p); 5031496Sbill if (p->subop<JBCC) lastrand=regs[RT3]; /* 2 operands can be optimized */ 5041496Sbill else lastrand=regs[RT2]; /* .mb destinations lose */ 5051496Sbill repladdr(p); 5061496Sbill } 5071496Sbill ccloc[0] = 0; 5081496Sbill break; 5091496Sbill 5101496Sbill case JBR: 5111496Sbill redunbr(p); 5121496Sbill 5131496Sbill /* .wx,.bb */ 5141496Sbill case SOB: 5151496Sbill 5161496Sbill default: 5171496Sbill clearreg(); 5181496Sbill } 5191496Sbill } 5201496Sbill } 5211496Sbill 5221496Sbill char * 5231496Sbill byondrd(p) register struct node *p; { 5241496Sbill /* return pointer to register which is "beyond last read/modify operand" */ 5251496Sbill if (OP2==(p->subop>>4)) return(regs[RT3]); 5261496Sbill switch (p->op) { 5271496Sbill case MFPR: 5281496Sbill case JSB: 5291496Sbill case PUSHA: 5301496Sbill case TST: case INC: case DEC: case PUSH: return(regs[RT2]); 5311496Sbill case MTPR: 5321496Sbill case BIT: case CMP: case CALLS: return(regs[RT3]); 5331496Sbill case PROBER: case PROBEW: 5341496Sbill case CASE: case MOVC3: return(regs[RT4]); 5351496Sbill } 5361496Sbill return(lastrand); 5371496Sbill } 5381496Sbill 5391496Sbill struct node * 5401496Sbill bflow(p) 5411496Sbill register struct node *p; 5421496Sbill { 5431496Sbill register char *cp1,*cp2,**preg; register int r; 5441496Sbill int flow= -1; 5451496Sbill struct node *olduse=0; 5461496Sbill splitrand(p); 5471496Sbill if (p->op!=PUSH && p->subop && 0<=(r=isreg(lastrand)) && r<NUSE && uses[r]==p->forw) { 5481496Sbill if (equtype(p->subop,regs[r][0]) 5491496Sbill || ((p->op==CVT || p->op==MOVZ) 5501496Sbill && 0xf®s[r][0] && compat(0xf&(p->subop>>4),regs[r][0]))) { 5511496Sbill register int r2; 5521496Sbill if (regs[r][1]!=0) {/* send directly to destination */ 5531496Sbill if (p->op==INC || p->op==DEC) { 5541496Sbill if (p->op==DEC) p->op=SUB; else p->op=ADD; 5551496Sbill p->subop=(OP2<<4)+(p->subop&0xF); /* use 2 now, convert to 3 later */ 5561496Sbill p->pop=0; 5571496Sbill cp1=lastrand; cp2=regs[RT2]; while (*cp2++= *cp1++); /* copy reg */ 5581496Sbill cp1=lastrand; *cp1++='$'; *cp1++='1'; *cp1=0; 5591496Sbill } 5601496Sbill cp1=regs[r]+1; cp2=lastrand; 5611496Sbill if (OP2==(p->subop>>4)) {/* use 3 operand form of instruction */ 5621496Sbill p->pop=0; 5631496Sbill p->subop += (OP3-OP2)<<4; lastrand=cp2=regs[RT3]; 5641496Sbill } 5651496Sbill while (*cp2++= *cp1++); 5661496Sbill if (p->op==MOVA && p->forw->op==PUSH) { 5671496Sbill p->op=PUSHA; *regs[RT2]=0; p->pop=0; 5681496Sbill } else if (p->op==MOV && p->forw->op==PUSH) { 5691496Sbill p->op=PUSH ; *regs[RT2]=0; p->pop=0; 5701496Sbill } 5711496Sbill delnode(p->forw); 5721496Sbill if (0<=(r2=isreg(lastrand)) && r2<NUSE) { 5731496Sbill uses[r2]=uses[r]; uses[r]=0; 5741496Sbill } 5751496Sbill redun3(p,0); 5761496Sbill newcode(p); redunm++; flow=r; 5771496Sbill } else if (p->op==MOV && p->forw->op!=EXTV && p->forw->op!=EXTZV) { 5781496Sbill /* superfluous fetch */ 5791496Sbill int nmatch; 5801496Sbill char src[20]; 5811496Sbill movit: 5821496Sbill cp2=src; cp1=regs[RT1]; while (*cp2++= *cp1++); 5831496Sbill splitrand(p->forw); 5841496Sbill if (p->forw->op != INC && p->forw->op != DEC) 5851496Sbill lastrand=byondrd(p->forw); 5861496Sbill nmatch=0; 5871496Sbill for (preg=regs+RT1;*preg!=lastrand;preg++) 5881496Sbill if (r==isreg(*preg)) { 5891496Sbill cp2= *preg; cp1=src; while (*cp2++= *cp1++); ++nmatch; 5901496Sbill } 5911496Sbill if (nmatch==1) { 5921496Sbill if (OP2==(p->forw->subop>>4) && equstr(src,regs[RT2])) { 5931496Sbill p->forw->pop=0; 5941496Sbill p->forw->subop += (OP3-OP2)<<4; cp1=regs[RT3]; 5951496Sbill *cp1++='r'; *cp1++=r+'0'; *cp1=0; 5961496Sbill } 5971496Sbill delnode(p); p=p->forw; 5981496Sbill if (0<=(r2=isreg(src)) && r2<NUSE) { 5991496Sbill uses[r2]=uses[r]; uses[r]=0; 6001496Sbill } 6011496Sbill redun3(p,0); 6021496Sbill newcode(p); redunm++; flow=r; 6031496Sbill } else splitrand(p); 6041496Sbill } 6051496Sbill } else if (p->op==MOV && (p->forw->op==CVT || p->forw->op==MOVZ) 6061496Sbill && p->forw->subop&0xf /* if base or index, then forget it */ 6071496Sbill && compat(p->subop,p->forw->subop) && !source(cp1=regs[RT1]) 6081496Sbill && !indexa(cp1)) goto movit; 6091496Sbill } 6101496Sbill /* adjust 'lastrand' past any 'read' or 'modify' operands. */ 6111496Sbill lastrand=byondrd(p); 6121496Sbill /* a 'write' clobbers the register. */ 6131496Sbill if (0<=(r=isreg(lastrand)) && r<NUSE 6141496Sbill || OP2==(p->subop>>4) && 0<=(r=isreg(regs[RT2])) && r<NUSE && uses[r]==0) { 6151496Sbill /* writing a dead register is useless, but watch side effects */ 6161496Sbill switch (p->op) { 6171498Sbill case ACB: 6181496Sbill case AOBLEQ: case AOBLSS: case SOBGTR: case SOBGEQ: break; 6191498Sbill default: 6201496Sbill if (uses[r]==0) {/* no direct uses, check for use of condition codes */ 6211496Sbill register struct node *q=p; 6221496Sbill while ((q=nonlab(q->forw))->combop==JBR) q=q->ref; /* cc unused, unchanged */ 6231496Sbill if (q->op!=CBR) {/* ... and destroyed */ 6241496Sbill preg=regs+RT1; 6251496Sbill while (cp1= *preg++) { 6261496Sbill if (cp1==lastrand) {redunm++; delnode(p); return(p->forw);} 6271496Sbill if (source(cp1) || equstr(cp1,lastrand)) break; 6281496Sbill } 6291496Sbill } 6301496Sbill } 6311496Sbill flow=r; 6321496Sbill } 6331496Sbill } 6341496Sbill if (0<=(r=flow)) {olduse=uses[r]; uses[r]=0; *(short *)(regs[r])=0;} 6351496Sbill /* these two are here, rather than in bmove(), 6361496Sbill /* because I decided that it was better to go for 3-address code 6371496Sbill /* (save time) rather than fancy jbxx (save 1 byte) 6381496Sbill /* on sequences like bisl2 $64,r0; movl r0,foo 6391496Sbill */ 6401496Sbill if (p->op==BIC) {p=bicopt(p); splitrand(p); lastrand=byondrd(p);} 6411496Sbill if (p->op==BIS) {bixprep(p,JBSS); lastrand=byondrd(p);} 6421496Sbill /* now look for 'read' or 'modify' (read & write) uses */ 6431496Sbill preg=regs+RT1; 6441496Sbill while (*(cp1= *preg++)) { 6451496Sbill /* check for r */ 6461496Sbill if (lastrand!=cp1 && 0<=(r=isreg(cp1)) && r<NUSE && uses[r]==0) { 6471496Sbill uses[r]=p; cp2=regs[r]; *cp2++=p->subop; 648*1506Sbill if (p->op==ASH && preg==(regs+RT1+1)) cp2[-1]=BYTE; /* stupid DEC */ 6491496Sbill if (p->op==MOV || p->op==PUSH || p->op==CVT || p->op==MOVZ || p->op==COM || p->op==NEG) { 6501496Sbill if (p->op==PUSH) cp1="-(sp)"; 6511496Sbill else { 6521496Sbill cp1=regs[RT2]; 6531496Sbill if (0<=(r=isreg(cp1)) && r<NUSE && uses[r]==0) 6541496Sbill uses[r]=olduse; /* reincarnation!! */ 6551496Sbill /* as in addl2 r0,r1; movl r1,r0; ret */ 6561496Sbill if (p->op!=MOV) cp1=0; 6571496Sbill } 6581496Sbill if (cp1) while (*cp2++= *cp1++); 659*1506Sbill else *cp2=0; 660*1506Sbill } else *cp2=0; 6611496Sbill continue; 6621496Sbill } 6631496Sbill /* check for (r),(r)+,-(r),[r] */ 6641496Sbill do if (*cp1=='(' || *cp1=='[') {/* get register number */ 6651496Sbill char t; 6661496Sbill cp2= ++cp1; while (*++cp1!=')' && *cp1!=']'); t= *cp1; *cp1=0; 6671496Sbill if (0<=(r=isreg(cp2)) && r<NUSE && (uses[r]==0 || uses[r]==p)) { 6681496Sbill uses[r]=p; regs[r][0]=(*--cp2=='[' ? OPX<<4 : OPB<<4); 6691496Sbill } 6701496Sbill *cp1=t; 6711496Sbill } while (*++cp1); 6721496Sbill } 6731496Sbill /* pushax or movax possibility? */ 6741496Sbill cp1=regs[RT1]; 6751496Sbill if (*cp1++=='$' && isstatic(cp1) && natural(regs[RT1])) { 6761496Sbill if (p->combop==T(MOV,LONG)) { 6771496Sbill if (regs[RT1][1]=='L' && 0!=(p->labno=getnum(regs[RT1]+2))) { 6781496Sbill cp1=p->code; while (*cp1++!=','); p->code= --cp1; 6791496Sbill } 6801496Sbill p->combop=T(MOVA,LONG); ++p->code; p->pop=0; 6811496Sbill } else if (p->combop==T(PUSH,LONG)) { 6821496Sbill p->combop=T(PUSHA,LONG); ++p->code; p->pop=0; 6831496Sbill } else if ((p->combop&0xFFFF)==T(ADD,U(LONG,OP3)) 6841496Sbill && 0<=(r=isreg(regs[RT2]))) { 6851496Sbill cp1=cp2=p->code; ++cp1; 6861496Sbill do *cp2++= *cp1; while (*cp1++!=','); cp2[-1]='['; 6871496Sbill do *cp2++= *cp1; while (*cp1++!=','); cp2[-1]=']'; 6881496Sbill if (!equstr(regs[RT3],"-(sp)")) p->combop=T(MOVA,BYTE); 6891496Sbill else {p->combop=T(PUSHA,BYTE); *cp2=0;} 6901496Sbill if (uses[r]==0) {uses[r]=p; regs[r][0]=OPX<<4;} 6911496Sbill p->pop=0; 6921496Sbill } 6931496Sbill } 6941496Sbill return(p); 6951496Sbill } 6961496Sbill 6971496Sbill ispow2(n) register long n; {/* -1 -> no; else -> log to base 2 */ 6981496Sbill register int log; 6991496Sbill if (n==0 || n&(n-1)) return(-1); log=0; 7001496Sbill for (;;) {n >>= 1; if (n==0) return(log); ++log; if (n== -1) return(log);} 7011496Sbill } 7021496Sbill 7031496Sbill bitopt(p) register struct node *p; { 7041496Sbill /* change "bitx $<power_of_2>,a" followed by JEQ or JNE 7051496Sbill /* into JBC or JBS. watch out for I/O registers. (?) 7061496Sbill /* assumes that 'splitrand' has already been called. 7071496Sbill */ 7081496Sbill register char *cp1,*cp2; int b; 7091496Sbill cp1=regs[RT1]; cp2=regs[RT2]; 7101496Sbill if (*cp1++!='$' || !okio(cp2) || p->forw->op!=CBR || p->forw->subop&-2 || 7111496Sbill 0>(b=ispow2(getnum(cp1))) || 7121496Sbill p->subop!=BYTE && (source(cp2) || indexa(cp2))) return; 7131496Sbill if (b>=bitsize[p->subop]) {/* you dummy! */ 7141496Sbill if (source(cp2)) {/* side effect: auto increment or decrement */ 7151496Sbill p->pop=0; 7161496Sbill p->op=TST; --cp1; while (*cp1++= *cp2++); 7171496Sbill regs[RT2][0]=0; newcode(p); 7181496Sbill } else delnode(p); 7191496Sbill p = p->forw; 7201496Sbill if (p->subop==JEQ) {p->combop=JBR; p->pop=0;} 7211496Sbill else delnode(p); 7221496Sbill nchange++; nbj++; return; 7231496Sbill } 7241496Sbill if (cp1=p->forw->code) {/* destination is not an internal label */ 7251496Sbill cp2=regs[RT3]; while (*cp2++= *cp1++); 7261496Sbill } 7271496Sbill if (b==0 && (p->subop==LONG || !indexa(regs[RT2]))) {/* JLB optimization, ala BLISS */ 7281496Sbill cp2=regs[RT1]; cp1=regs[RT2]; while (*cp2++= *cp1++); 7291496Sbill cp2=regs[RT2]; cp1=regs[RT3]; while (*cp2++= *cp1++); 7301496Sbill *(regs[RT3])=0; p->forw->subop += JLBC-JBC; 7311496Sbill p->forw->pop=0; 7321496Sbill } else { 7331496Sbill cp1=regs[RT1]+1; 7341496Sbill if (b>9) *cp1++= b/10 +'0'; *cp1++= b%10 +'0'; *cp1=0; /* $<bit_number> */ 7351496Sbill } 7361496Sbill nbj++; newcode(p); p->combop = p->forw->combop+((JBC-JEQ)<<8); 7371496Sbill p->labno = p->forw->labno; delnode(p->forw); 7381496Sbill p->pop=0; 7391496Sbill } 7401496Sbill 7411496Sbill isfield(n) register long n; {/* -1 -> no; else -> position of low bit */ 7421496Sbill register int pos; register long t; 7431496Sbill t= ((n-1)|n) +1; 7441496Sbill if (n!=0 && (0==t || 0==n || 0<=ispow2(t))) { 7451496Sbill pos=0; while(!(n&1)) {n >>= 1; ++pos;} return(pos); 7461496Sbill } else return(-1); 7471496Sbill } 7481496Sbill 7491496Sbill bixprep(p,bix) register struct node *p; { 7501496Sbill /* initial setup, single-bit checking for bisopt, bicopt. 7511496Sbill /* return: 0->don't bother any more; 1->worthwhile trying 7521496Sbill */ 7531496Sbill register char *cp1,*cp2; 7541496Sbill splitrand(p); cp1=regs[RT1]; cp2=regs[RT2]; 7551496Sbill if (*cp1++!='$' || 0>(pos=isfield(f=getnum(cp1))) 7561496Sbill || !okio(cp2) || indexa(cp2) || source(cp2) || !okio(lastrand)) return(0); 7571496Sbill f |= f-1; if (++f==0) siz=32-pos; else siz=ispow2(f)-pos; 7581496Sbill if (siz==1 && pos>5 && (p->subop>>4)==OP2 && (p->subop&0xF)!=BYTE 7591496Sbill && pos<bitsize[p->subop&0xF]) { 7601496Sbill p->ref = insertl(p->forw); p->combop = CBR | (bix<<8); 7611496Sbill p->pop=0; 7621496Sbill p->labno = p->ref->labno; 7631496Sbill if (pos>9) {*cp1++= pos/10 +'0'; pos %= 10;} 7641496Sbill *cp1++=pos+'0'; *cp1=0; newcode(p); nbj++; return(0); 7651496Sbill } 7661496Sbill return(1); 7671496Sbill } 7681496Sbill 7691496Sbill 7701496Sbill struct node * 7711496Sbill bicopt(p) register struct node *p; { 7721496Sbill /* use field operations or MOVZ if possible. done as part of 'bflow'. 7731496Sbill */ 7741496Sbill register char *cp1,*cp2; int r; 7751496Sbill char src[50]; 7761496Sbill if (!bixprep(p,JBCC)) return(p); 7771496Sbill if (f==0) {/* the BIC isolates low order bits */ 7781496Sbill siz=pos; pos=0; 7791496Sbill if ((p->subop&0xF)==LONG && *(regs[RT2])!='$') {/* result of EXTZV is long */ 7801496Sbill /* save source of BICL in 'src' */ 7811496Sbill cp1=regs[RT2]; cp2=src; while (*cp2++= *cp1++); 7821496Sbill if (p->back->op==ASH) {/* try for more */ 7831496Sbill splitrand(p->back); cp1=regs[RT1]; cp2=regs[RT3]; 7841496Sbill if (*cp1++=='$' && *(regs[RT2])!='$' && !indexa(regs[RT2]) 7851496Sbill && 0>(f=getnum(cp1)) && equstr(src,cp2) 7861496Sbill && 0<=(r=isreg(cp2)) && r<NUSE) {/* a good ASH */ 7871496Sbill pos -= f; cp1=regs[RT2]; cp2=src; while (*cp2++= *cp1++); 7881496Sbill delnode(p->back); 7891496Sbill } 7901496Sbill } 7911496Sbill if (p->back->op==CVT || p->back->op==MOVZ) {/* greedy, aren't we? */ 7921496Sbill splitrand(p->back); cp1=regs[RT1]; cp2=regs[RT2]; 7931496Sbill if (equstr(src,cp2) && okio(cp1) && !indexa(cp1) 7941496Sbill && 0<=(r=isreg(cp2)) && r<NUSE 7951496Sbill && bitsize[p->back->subop&0xF]>=(pos+siz) 7961496Sbill && bitsize[p->back->subop>>4]>=(pos+siz)) {/* good CVT */ 7971496Sbill cp1=regs[RT1]; cp2=src; while (*cp2++= *cp1++); 7981496Sbill delnode(p->back); 7991496Sbill } 8001496Sbill } 8011496Sbill /* 'pos', 'siz' known; source of field is in 'src' */ 8021496Sbill splitrand(p); /* retrieve destination of BICL */ 8031496Sbill if (siz==8 && pos==0) { 8041496Sbill p->combop = T(MOVZ,U(BYTE,LONG)); 8051496Sbill sprintf(line,"%s,%s",src,lastrand); 8061496Sbill } else { 8071496Sbill p->combop = T(EXTZV,LONG); 8081496Sbill sprintf(line,"$%d,$%d,%s,%s",pos,siz,src,lastrand); 8091496Sbill } 8101496Sbill p->pop=0; 8111496Sbill p->code = copy(line); nfield++; return(p); 8121496Sbill }/* end EXTZV possibility */ 8131496Sbill }/* end low order bits */ 8141496Sbill /* unfortunately, INSV clears the condition codes, thus cannot be used */ 8151496Sbill /* else {/* see if BICL2 of positive field should be INSV $0 */ 8161496Sbill /* if (p->subop==(LONG | (OP2<<4)) && 6<=(pos+siz)) { 8171496Sbill /* p->combop = INSV; 8181496Sbill /* sprintf(line,"$0,$%d,$%d,%s",pos,siz,lastrand); 8191496Sbill /* p->code = copy(line); nfield++; return(p); 8201496Sbill /* } 8211496Sbill /* } 8221496Sbill */ 8231496Sbill return(p); 8241496Sbill } 8251496Sbill 8261496Sbill jumpsw() 8271496Sbill { 8281496Sbill register struct node *p, *p1; 8291496Sbill register t; 8301496Sbill int nj; 8311496Sbill 8321496Sbill t = 0; 8331496Sbill nj = 0; 8341496Sbill for (p=first.forw; p!=0; p = p->forw) 8351496Sbill p->seq = ++t; 8361496Sbill for (p=first.forw; p!=0; p = p1) { 8371496Sbill p1 = p->forw; 8381496Sbill if (p->op == CBR && p1->op==JBR && p->ref && p1->ref 8391496Sbill && abs(p->seq - p->ref->seq) > abs(p1->seq - p1->ref->seq)) { 8401496Sbill if (p->ref==p1->ref) 8411496Sbill continue; 8421496Sbill p->subop = revbr[p->subop]; 8431496Sbill p->pop=0; 8441496Sbill t = p1->ref; 8451496Sbill p1->ref = p->ref; 8461496Sbill p->ref = t; 8471496Sbill t = p1->labno; 8481496Sbill p1->labno = p->labno; 8491496Sbill p->labno = t; 8501496Sbill #ifdef COPYCODE 8511496Sbill if (p->labno == 0) { 8521496Sbill t = p1->code; p1->code = p->code; p->code = t; 8531496Sbill } 8541496Sbill #endif 8551496Sbill nrevbr++; 8561496Sbill nj++; 8571496Sbill } 8581496Sbill } 8591496Sbill return(nj); 8601496Sbill } 8611496Sbill 8621496Sbill addsob() 8631496Sbill { 8641496Sbill register struct node *p, *p1, *p2, *p3; 8651496Sbill 8661496Sbill for (p = &first; (p1 = p->forw)!=0; p = p1) { 8671496Sbill if (p->combop==T(DEC,LONG) && p1->op==CBR) { 8681496Sbill if (abs(p->seq - p1->ref->seq) > 12) continue; 8691496Sbill if (p1->subop==JGE || p1->subop==JGT) { 8701496Sbill if (p1->subop==JGE) p->combop=SOBGEQ; else p->combop=SOBGTR; 8711496Sbill p->pop=0; 8721496Sbill p->labno = p1->labno; delnode(p1); nsob++; 8731496Sbill } 8741496Sbill } else if (p->combop==T(INC,LONG)) { 8751496Sbill if (p1->op==LABEL && p1->refc==1 && p1->forw->combop==T(CMP,LONG) 8761496Sbill && (p2=p1->forw->forw)->combop==T(CBR,JLE) 8771496Sbill && (p3=p2->ref->back)->combop==JBR && p3->ref==p1 8781496Sbill && p3->forw->op==LABEL && p3->forw==p2->ref) { 8791496Sbill /* change INC LAB: CMP to LAB: INC CMP */ 8801496Sbill p->back->forw=p1; p1->back=p->back; 8811496Sbill p->forw=p1->forw; p1->forw->back=p; 8821496Sbill p->back=p1; p1->forw=p; 8831496Sbill p1=p->forw; 8841496Sbill /* adjust beginning value by 1 */ 8851496Sbill p2=alloc(sizeof first); p2->combop=T(DEC,LONG); 8861496Sbill p2->pop=0; 8871496Sbill p2->forw=p3; p2->back=p3->back; p3->back->forw=p2; 8881496Sbill p3->back=p2; p2->code=p->code; p2->labno=0; 8891496Sbill } 8901496Sbill if (p1->combop==T(CMP,LONG) && (p2=p1->forw)->op==CBR) { 8911496Sbill register char *cp1,*cp2; 8921496Sbill splitrand(p1); if (!equstr(p->code,regs[RT1])) continue; 8931496Sbill if (abs(p->seq - p2->ref->seq)>12) {/* outside byte displ range */ 8941496Sbill if (p2->subop!=JLE) continue; 8951496Sbill p->combop=T(ACB,LONG); 8961496Sbill cp2=regs[RT1]; cp1=regs[RT2]; while (*cp2++= *cp1++); /* limit */ 8971496Sbill cp2=regs[RT2]; cp1="$1"; while (*cp2++= *cp1++); /* increment */ 8981496Sbill cp2=regs[RT3]; cp1=p->code; while (*cp2++= *cp1++); /* index */ 8991496Sbill p->pop=0; newcode(p); 9001496Sbill p->labno = p2->labno; delnode(p2); delnode(p1); nsob++; 9011496Sbill } else if (p2->subop==JLE || p2->subop==JLT) { 9021496Sbill if (p2->subop==JLE) p->combop=AOBLEQ; else p->combop=AOBLSS; 9031496Sbill cp2=regs[RT1]; cp1=regs[RT2]; while (*cp2++= *cp1++); /* limit */ 9041496Sbill cp2=regs[RT2]; cp1=p->code; while (*cp2++= *cp1++); /* index */ 9051496Sbill p->pop=0; newcode(p); 9061496Sbill p->labno = p2->labno; delnode(p2); delnode(p1); nsob++; 9071496Sbill } 9081496Sbill } 9091496Sbill } 9101496Sbill } 9111496Sbill } 9121496Sbill 9131496Sbill abs(x) 9141496Sbill { 9151496Sbill return(x<0? -x: x); 9161496Sbill } 9171496Sbill 9181496Sbill equop(p1, p2) 9191496Sbill register struct node *p1; 9201496Sbill struct node *p2; 9211496Sbill { 9221496Sbill register char *cp1, *cp2; 9231496Sbill 9241496Sbill if (p1->combop != p2->combop) 9251496Sbill return(0); 9261496Sbill if (p1->op>0 && p1->op<MOV) 9271496Sbill return(0); 9281496Sbill if (p1->op==MOVA && p1->labno!=p2->labno) return(0); 9291496Sbill cp1 = p1->code; 9301496Sbill cp2 = p2->code; 9311496Sbill if (cp1==0 && cp2==0) 9321496Sbill return(1); 9331496Sbill if (cp1==0 || cp2==0) 9341496Sbill return(0); 9351496Sbill while (*cp1 == *cp2++) 9361496Sbill if (*cp1++ == 0) 9371496Sbill return(1); 9381496Sbill return(0); 9391496Sbill } 9401496Sbill 9411496Sbill delnode(p) register struct node *p; { 9421496Sbill p->back->forw = p->forw; 9431496Sbill p->forw->back = p->back; 9441496Sbill } 9451496Sbill 9461496Sbill decref(p) 9471496Sbill register struct node *p; 9481496Sbill { 9491496Sbill if (p && --p->refc <= 0) { 9501496Sbill nrlab++; 9511496Sbill delnode(p); 9521496Sbill } 9531496Sbill } 9541496Sbill 9551496Sbill struct node * 9561496Sbill nonlab(ap) 9571496Sbill struct node *ap; 9581496Sbill { 9591496Sbill register struct node *p; 9601496Sbill 9611496Sbill p = ap; 9621496Sbill while (p && p->op==LABEL) 9631496Sbill p = p->forw; 9641496Sbill return(p); 9651496Sbill } 9661496Sbill 9671496Sbill clearuse() { 9681496Sbill register struct node **i; 9691496Sbill for (i=uses+NUSE; i>uses;) *--i=0; 9701496Sbill } 9711496Sbill 9721496Sbill clearreg() { 9731496Sbill register short **i; 9741496Sbill for (i=regs+NREG; i>regs;) **--i=0; 9751496Sbill conloc[0] = 0; ccloc[0] = 0; 9761496Sbill } 9771496Sbill 9781496Sbill savereg(ai, s, type) 9791496Sbill register char *s; 9801496Sbill { 9811496Sbill register char *p, *sp; 9821496Sbill 9831496Sbill sp = p = regs[ai]; 9841496Sbill if (source(s)) /* side effects in addressing */ 9851496Sbill return; 9861496Sbill /* if any indexing, must be parameter or local */ 9871496Sbill /* indirection (as in "*-4(fp)") is ok, however */ 9881496Sbill *p++ = type; 9891496Sbill while (*p++ = *s) 9901496Sbill if (*s=='[' || *s++=='(' && *s!='a' && *s!='f') {*sp = 0; return;} 9911496Sbill } 9921496Sbill 9931496Sbill dest(s,type) 9941496Sbill register char *s; 9951496Sbill { 9961496Sbill register int i; 9971496Sbill 9981496Sbill source(s); /* handle addressing side effects */ 9991496Sbill if ((i = isreg(s)) >= 0) { 10001496Sbill *(short *)(regs[i]) = 0; /* if register destination, that reg is a goner */ 10011496Sbill if (DOUBLE==(type&0xF) || DOUBLE==((type>>4)&0xF)) 10021496Sbill *(short *)(regs[i+1]) = 0; /* clobber two at once */ 10031496Sbill } 10041496Sbill for (i=NREG; --i>=0;) 10051496Sbill if (regs[i][1]=='*' && equstr(s, regs[i]+2)) 10061496Sbill *(short *)(regs[i]) = 0; /* previous indirection through destination is invalid */ 10071496Sbill while ((i = findrand(s,0)) >= 0) /* previous values of destination are invalid */ 10081496Sbill *(short *)(regs[i]) = 0; 10091496Sbill if (!natural(s)) {/* wild store, everything except constants vanishes */ 10101496Sbill for (i=NREG; --i>=0;) if (regs[i][1] != '$') *(short *)(regs[i]) = 0; 10111496Sbill conloc[0] = 0; ccloc[0] = 0; 10121496Sbill } else setcc(s,type); /* natural destinations set condition codes */ 10131496Sbill } 10141496Sbill 10151496Sbill splitrand(p) struct node *p; { 10161496Sbill /* separate operands at commas, set up 'regs' and 'lastrand' */ 10171496Sbill register char *p1, *p2; register char **preg; 10181496Sbill preg=regs+RT1; 10191496Sbill if (p1=p->code) while (*p1) { 10201496Sbill lastrand=p2= *preg++; 10211496Sbill while (*p1) if (','==(*p2++= *p1++)) {--p2; break;} 10221496Sbill *p2=0; 10231496Sbill } 10241496Sbill while (preg<(regs+RT1+5)) *(*preg++)=0; 10251496Sbill } 10261496Sbill 10271496Sbill compat(have, want) { 10281496Sbill register int hsrc, hdst; 10291496Sbill if (0==(want &= 0xF)) return(1); /* anything satisfies a wildcard want */ 10301496Sbill hsrc=have&0xF; if (0==(hdst=((have>>4)&0xF)) || hdst>=OP2) hdst=hsrc; 10311496Sbill if (want>=FLOAT) return(hdst==want && hsrc==want); 10321496Sbill /* FLOAT, DOUBLE not compat: rounding */ 10331496Sbill return(hsrc>=want && hdst>=want && hdst<FLOAT); 10341496Sbill } 10351496Sbill 10361496Sbill equtype(t1,t2) {return(compat(t1,t2) && compat(t2,t1));} 10371496Sbill 10381496Sbill findrand(as, type) 10391496Sbill char *as; 10401496Sbill { 10411496Sbill register char **i; 10421496Sbill for (i = regs+NREG; --i>=regs;) { 10431496Sbill if (**i && equstr(*i+1, as) && compat(**i,type)) 10441496Sbill return(i-regs); 10451496Sbill } 10461496Sbill return(-1); 10471496Sbill } 10481496Sbill 10491496Sbill isreg(s) 10501496Sbill register char *s; 10511496Sbill { 10521496Sbill if (*s++!='r' || !isdigit(*s++)) return(-1); 10531496Sbill if (*s==0) return(*--s-'0'); 10541496Sbill if (*(s-1)=='1' && isdigit(*s++) && *s==0) return(10+*--s-'0'); 10551496Sbill return(-1); 10561496Sbill } 10571496Sbill 10581496Sbill check() 10591496Sbill { 10601496Sbill register struct node *p, *lp; 10611496Sbill 10621496Sbill lp = &first; 10631496Sbill for (p=first.forw; p!=0; p = p->forw) { 10641496Sbill if (p->back != lp) 10651496Sbill abort(-1); 10661496Sbill lp = p; 10671496Sbill } 10681496Sbill } 10691496Sbill 10701496Sbill source(ap) 10711496Sbill char *ap; 10721496Sbill { 10731496Sbill register char *p1, *p2; 10741496Sbill 10751496Sbill p1 = ap; 10761496Sbill p2 = p1; 10771496Sbill if (*p1==0) 10781496Sbill return(0); 10791496Sbill while (*p2++ && *(p2-1)!='['); 10801496Sbill if (*p1=='-' && *(p1+1)=='(' 10811496Sbill || *p1=='*' && *(p1+1)=='-' && *(p1+2)=='(' 10821496Sbill || *(p2-2)=='+') { 10831496Sbill while (*p1 && *p1++!='r'); 10841496Sbill if (isdigit(*p1++)) 10851496Sbill if (isdigit(*p1)) *(short *)(regs[10+*p1-'0'])=0; 10861496Sbill else *(short *)(regs[*--p1-'0'])=0; 10871496Sbill return(1); 10881496Sbill } 10891496Sbill return(0); 10901496Sbill } 10911496Sbill 10921496Sbill newcode(p) struct node *p; { 10931496Sbill register char *p1,*p2,**preg; 10941496Sbill preg=regs+RT1; p2=line; 10951496Sbill while (*(p1= *preg++)) {while (*p2++= *p1++); *(p2-1)=',';} 10961496Sbill *--p2=0; 10971496Sbill p->code=copy(line); 10981496Sbill } 10991496Sbill 11001496Sbill repladdr(p) 11011496Sbill struct node *p; 11021496Sbill { 11031496Sbill register r; 11041496Sbill register char *p1, *p2; 11051496Sbill char **preg; int nrepl; 11061496Sbill 11071496Sbill preg=regs+RT1; nrepl=0; 11081496Sbill while (lastrand!=(p1= *preg++)) 11091496Sbill if (!source(p1) && 0<=(r=findrand(p1,p->subop))) { 11101496Sbill *p1++='r'; if (r>9) {*p1++='1'; r -= 10;} *p1++=r+'0'; *p1=0; 11111496Sbill nrepl++; nsaddr++; 11121496Sbill } 11131496Sbill if (nrepl) newcode(p); 11141496Sbill } 11151496Sbill 11161496Sbill /* movedat() 11171496Sbill /* { 11181496Sbill /* register struct node *p1, *p2; 11191496Sbill /* struct node *p3; 11201496Sbill /* register seg; 11211496Sbill /* struct node data; 11221496Sbill /* struct node *datp; 11231496Sbill /* 11241496Sbill /* if (first.forw == 0) 11251496Sbill /* return; 11261496Sbill /* datp = &data; 11271496Sbill /* for (p1 = first.forw; p1!=0; p1 = p1->forw) { 11281496Sbill /* if (p1->op == DATA) { 11291496Sbill /* p2 = p1->forw; 11301496Sbill /* while (p2 && p2->op!=TEXT) 11311496Sbill /* p2 = p2->forw; 11321496Sbill /* if (p2==0) 11331496Sbill /* break; 11341496Sbill /* p3 = p1->back; 11351496Sbill /* p1->back->forw = p2->forw; 11361496Sbill /* p2->forw->back = p3; 11371496Sbill /* p2->forw = 0; 11381496Sbill /* datp->forw = p1; 11391496Sbill /* p1->back = datp; 11401496Sbill /* p1 = p3; 11411496Sbill /* datp = p2; 11421496Sbill /* } 11431496Sbill /* } 11441496Sbill /* if (data.forw) { 11451496Sbill /* datp->forw = first.forw; 11461496Sbill /* first.forw->back = datp; 11471496Sbill /* data.forw->back = &first; 11481496Sbill /* first.forw = data.forw; 11491496Sbill /* } 11501496Sbill /* seg = -1; 11511496Sbill /* for (p1 = first.forw; p1!=0; p1 = p1->forw) { 11521496Sbill /* if (p1->op==TEXT||p1->op==DATA||p1->op==BSS) { 11531496Sbill /* if (p1->op == seg || p1->forw&&p1->forw->op==seg) { 11541496Sbill /* p1->back->forw = p1->forw; 11551496Sbill /* p1->forw->back = p1->back; 11561496Sbill /* p1 = p1->back; 11571496Sbill /* continue; 11581496Sbill /* } 11591496Sbill /* seg = p1->op; 11601496Sbill /* } 11611496Sbill /* } 11621496Sbill /* } 11631496Sbill */ 11641496Sbill 11651496Sbill redunbr(p) 11661496Sbill register struct node *p; 11671496Sbill { 11681496Sbill register struct node *p1; 11691496Sbill register char *ap1; 11701496Sbill char *ap2; 11711496Sbill 11721496Sbill if ((p1 = p->ref) == 0) 11731496Sbill return; 11741496Sbill p1 = nonlab(p1); 11751496Sbill if (p1->op==TST) { 11761496Sbill splitrand(p1); 11771496Sbill savereg(RT2, "$0", p1->subop); 11781496Sbill } else if (p1->op==CMP) 11791496Sbill splitrand(p1); 11801496Sbill else 11811496Sbill return; 11821496Sbill if (p1->forw->op==CBR) { 11831496Sbill ap1 = findcon(RT1, p1->subop); 11841496Sbill ap2 = findcon(RT2, p1->subop); 11851496Sbill p1 = p1->forw; 11861496Sbill if (compare(p1->subop, ap1, ap2)) { 11871496Sbill nredunj++; 11881496Sbill nchange++; 11891496Sbill decref(p->ref); 11901496Sbill p->ref = p1->ref; 11911496Sbill p->labno = p1->labno; 11921496Sbill #ifdef COPYCODE 11931496Sbill if (p->labno == 0) 11941496Sbill p->code = p1->code; 11951496Sbill if (p->ref) 11961496Sbill #endif 11971496Sbill p->ref->refc++; 11981496Sbill } 11991496Sbill } else if (p1->op==TST && equstr(regs[RT1],ccloc+1) && 12001496Sbill equtype(ccloc[0],p1->subop)) { 12011496Sbill p1=insertl(p1->forw); decref(p->ref); p->ref=p1; 12021496Sbill nrtst++; nchange++; 12031496Sbill } 12041496Sbill } 12051496Sbill 12061496Sbill char * 12071496Sbill findcon(i, type) 12081496Sbill { 12091496Sbill register char *p; 12101496Sbill register r; 12111496Sbill 12121496Sbill p = regs[i]; 12131496Sbill if (*p=='$') 12141496Sbill return(p); 12151496Sbill if ((r = isreg(p)) >= 0 && compat(regs[r][0],type)) 12161496Sbill return(regs[r]+1); 12171496Sbill if (equstr(p, conloc)) 12181496Sbill return(conval+1); 12191496Sbill return(p); 12201496Sbill } 12211496Sbill 12221496Sbill compare(op, acp1, acp2) 12231496Sbill char *acp1, *acp2; 12241496Sbill { 12251496Sbill register char *cp1, *cp2; 12261496Sbill register n1; 12271496Sbill int n2; int sign; 12281496Sbill 12291496Sbill cp1 = acp1; 12301496Sbill cp2 = acp2; 12311496Sbill if (*cp1++ != '$' || *cp2++ != '$') 12321496Sbill return(0); 12331496Sbill n1 = 0; sign=1; if (*cp2=='-') {++cp2; sign= -1;} 12341496Sbill while (isdigit(*cp2)) {n1 *= 10; n1 += (*cp2++ - '0')*sign;} 12351496Sbill n2 = n1; 12361496Sbill n1 = 0; sign=1; if (*cp1=='-') {++cp1; sign= -1;} 12371496Sbill while (isdigit(*cp1)) {n1 *= 10; n1 += (*cp1++ - '0')*sign;} 12381496Sbill if (*cp1=='+') 12391496Sbill cp1++; 12401496Sbill if (*cp2=='+') 12411496Sbill cp2++; 12421496Sbill do { 12431496Sbill if (*cp1++ != *cp2) 12441496Sbill return(0); 12451496Sbill } while (*cp2++); 12461496Sbill cp1 = n1; 12471496Sbill cp2 = n2; 12481496Sbill switch(op) { 12491496Sbill 12501496Sbill case JEQ: 12511496Sbill return(cp1 == cp2); 12521496Sbill case JNE: 12531496Sbill return(cp1 != cp2); 12541496Sbill case JLE: 12551496Sbill return(((int)cp1) <= ((int)cp2)); 12561496Sbill case JGE: 12571496Sbill return(((int)cp1) >= ((int)cp2)); 12581496Sbill case JLT: 12591496Sbill return(((int)cp1) < ((int)cp2)); 12601496Sbill case JGT: 12611496Sbill return(((int)cp1) > ((int)cp2)); 12621496Sbill case JLO: 12631496Sbill return(cp1 < cp2); 12641496Sbill case JHI: 12651496Sbill return(cp1 > cp2); 12661496Sbill case JLOS: 12671496Sbill return(cp1 <= cp2); 12681496Sbill case JHIS: 12691496Sbill return(cp1 >= cp2); 12701496Sbill } 12711496Sbill return(0); 12721496Sbill } 12731496Sbill 12741496Sbill setcon(cv, cl, type) 12751496Sbill register char *cv, *cl; 12761496Sbill { 12771496Sbill register char *p; 12781496Sbill 12791496Sbill if (*cv != '$') 12801496Sbill return; 12811496Sbill if (!natural(cl)) 12821496Sbill return; 12831496Sbill p = conloc; 12841496Sbill while (*p++ = *cl++); 12851496Sbill p = conval; 12861496Sbill *p++ = type; 12871496Sbill while (*p++ = *cv++); 12881496Sbill } 12891496Sbill 12901496Sbill equstr(p1, p2) 12911496Sbill register char *p1, *p2; 12921496Sbill { 12931496Sbill do { 12941496Sbill if (*p1++ != *p2) 12951496Sbill return(0); 12961496Sbill } while (*p2++); 12971496Sbill return(1); 12981496Sbill } 12991496Sbill 13001496Sbill setcc(ap,type) 13011496Sbill char *ap; 13021496Sbill { 13031496Sbill register char *p, *p1; 13041496Sbill 13051496Sbill p = ap; 13061496Sbill if (!natural(p)) { 13071496Sbill ccloc[0] = 0; 13081496Sbill return; 13091496Sbill } 13101496Sbill p1 = ccloc; 13111496Sbill *p1++ = type; 13121496Sbill while (*p1++ = *p++); 13131496Sbill } 13141496Sbill 13151496Sbill okio(p) register char *p; {/* 0->probable I/O space address; 1->not */ 13161496Sbill if (ioflag && (!natural(p) || 0>getnum(p))) return(0); 13171496Sbill return(1); 13181496Sbill } 13191496Sbill 13201496Sbill indexa(p) register char *p; {/* 1-> uses [r] addressing mode; 0->doesn't */ 13211496Sbill while (*p) if (*p++=='[') return(1); 13221496Sbill return(0); 13231496Sbill } 13241496Sbill 13251496Sbill natural(p) 13261496Sbill register char *p; 13271496Sbill {/* 1->simple local, parameter, global, or register; 0->otherwise */ 13281496Sbill if (*p=='*' || *p=='(' || *p=='-'&&*(p+1)=='(' || *p=='$'&&getnum(p+1)) 13291496Sbill return(0); 13301496Sbill while (*p++); 13311496Sbill p--; 13321496Sbill if (*--p=='+' || *p==']' || *p==')' && *(p-2)!='a' && *(p-2)!='f') 13331496Sbill return(0); 13341496Sbill return(1); 13351496Sbill } 13361496Sbill 13371496Sbill /* 13381496Sbill ** Tell if an argument is most likely static. 13391496Sbill */ 13401496Sbill 13411496Sbill isstatic(cp) 13421496Sbill register char *cp; 13431496Sbill { 13441496Sbill if (*cp == '_' || *cp == 'L' || (*cp++ == 'v' && *cp == '.')) 13451496Sbill return (1); 13461496Sbill return (0); 13471496Sbill } 1348