1*3957Sbugs static char sccsid[] = "@(#)c21.c 4.6 07/08/81"; 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. */ 713933Sroot char buf[C2_ASIZE]; 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; 893933Sroot char buf[C2_ASIZE]; 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); 4221506Sbill if (r>=0) { 4231506Sbill if (r1>=0) savereg(r1, regs[r]+1, p->subop); 4241506Sbill else if (p->op!=CVT) savereg(r, regs[RT2], p->subop); 4251506Sbill } else if (r1>=0) savereg(r1, regs[RT1], p->subop); 4261506Sbill 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 */ 492*3957Sbugs if (p->op==CASE || p->op==PROBER || p->op==PROBEW) lastrand=regs[RT4]; 493*3957Sbugs else if (p->op==CMPV || p->op==CMPZV) lastrand=regs[RT4+1]; 494*3957Sbugs else if (p->op==MOVC3) lastrand=regs[RT1]; 4951496Sbill else lastrand=regs[RT3]; 4961496Sbill repladdr(p); 4971496Sbill if (p->op==CALLS || p->op==MOVC3) clearreg(); 4981496Sbill if (p->op==BIT) bitopt(p); 4991496Sbill ccloc[0]=0; break; 5001496Sbill 5011496Sbill case CBR: 5021496Sbill if (p->subop>=JBC) { 5031496Sbill splitrand(p); 5041496Sbill if (p->subop<JBCC) lastrand=regs[RT3]; /* 2 operands can be optimized */ 5051496Sbill else lastrand=regs[RT2]; /* .mb destinations lose */ 5061496Sbill repladdr(p); 5071496Sbill } 5081496Sbill ccloc[0] = 0; 5091496Sbill break; 5101496Sbill 5111496Sbill case JBR: 5121496Sbill redunbr(p); 5131496Sbill 5141496Sbill /* .wx,.bb */ 5151496Sbill case SOB: 5161496Sbill 5171496Sbill default: 5181496Sbill clearreg(); 5191496Sbill } 5201496Sbill } 5211496Sbill } 5221496Sbill 5231496Sbill char * 5241496Sbill byondrd(p) register struct node *p; { 5251496Sbill /* return pointer to register which is "beyond last read/modify operand" */ 5261496Sbill if (OP2==(p->subop>>4)) return(regs[RT3]); 5271496Sbill switch (p->op) { 5281496Sbill case MFPR: 5291496Sbill case JSB: 5301496Sbill case PUSHA: 5311496Sbill case TST: case INC: case DEC: case PUSH: return(regs[RT2]); 5321496Sbill case MTPR: 5331496Sbill case BIT: case CMP: case CALLS: return(regs[RT3]); 5341496Sbill case PROBER: case PROBEW: 5351496Sbill case CASE: case MOVC3: return(regs[RT4]); 5361496Sbill } 5371496Sbill return(lastrand); 5381496Sbill } 5391496Sbill 5401496Sbill struct node * 5411496Sbill bflow(p) 5421496Sbill register struct node *p; 5431496Sbill { 5441496Sbill register char *cp1,*cp2,**preg; register int r; 5451496Sbill int flow= -1; 5461496Sbill struct node *olduse=0; 5471496Sbill splitrand(p); 5481496Sbill if (p->op!=PUSH && p->subop && 0<=(r=isreg(lastrand)) && r<NUSE && uses[r]==p->forw) { 5491496Sbill if (equtype(p->subop,regs[r][0]) 5501496Sbill || ((p->op==CVT || p->op==MOVZ) 5511496Sbill && 0xf®s[r][0] && compat(0xf&(p->subop>>4),regs[r][0]))) { 5521496Sbill register int r2; 5531496Sbill if (regs[r][1]!=0) {/* send directly to destination */ 5541496Sbill if (p->op==INC || p->op==DEC) { 5551496Sbill if (p->op==DEC) p->op=SUB; else p->op=ADD; 5561496Sbill p->subop=(OP2<<4)+(p->subop&0xF); /* use 2 now, convert to 3 later */ 5571496Sbill p->pop=0; 5581496Sbill cp1=lastrand; cp2=regs[RT2]; while (*cp2++= *cp1++); /* copy reg */ 5591496Sbill cp1=lastrand; *cp1++='$'; *cp1++='1'; *cp1=0; 5601496Sbill } 5611496Sbill cp1=regs[r]+1; cp2=lastrand; 5621496Sbill if (OP2==(p->subop>>4)) {/* use 3 operand form of instruction */ 5631496Sbill p->pop=0; 5641496Sbill p->subop += (OP3-OP2)<<4; lastrand=cp2=regs[RT3]; 5651496Sbill } 5661496Sbill while (*cp2++= *cp1++); 5671496Sbill if (p->op==MOVA && p->forw->op==PUSH) { 5681496Sbill p->op=PUSHA; *regs[RT2]=0; p->pop=0; 5691496Sbill } else if (p->op==MOV && p->forw->op==PUSH) { 5701496Sbill p->op=PUSH ; *regs[RT2]=0; p->pop=0; 5711496Sbill } 5721496Sbill delnode(p->forw); 5731496Sbill if (0<=(r2=isreg(lastrand)) && r2<NUSE) { 5741496Sbill uses[r2]=uses[r]; uses[r]=0; 5751496Sbill } 5761496Sbill redun3(p,0); 5771496Sbill newcode(p); redunm++; flow=r; 5781496Sbill } else if (p->op==MOV && p->forw->op!=EXTV && p->forw->op!=EXTZV) { 5791496Sbill /* superfluous fetch */ 5801496Sbill int nmatch; 5812829Stoy char src[C2_ASIZE]; 5821496Sbill movit: 5831496Sbill cp2=src; cp1=regs[RT1]; while (*cp2++= *cp1++); 5841496Sbill splitrand(p->forw); 5851496Sbill if (p->forw->op != INC && p->forw->op != DEC) 5861496Sbill lastrand=byondrd(p->forw); 5871496Sbill nmatch=0; 5881496Sbill for (preg=regs+RT1;*preg!=lastrand;preg++) 5891496Sbill if (r==isreg(*preg)) { 5901496Sbill cp2= *preg; cp1=src; while (*cp2++= *cp1++); ++nmatch; 5911496Sbill } 5921496Sbill if (nmatch==1) { 5931496Sbill if (OP2==(p->forw->subop>>4) && equstr(src,regs[RT2])) { 5941496Sbill p->forw->pop=0; 5951496Sbill p->forw->subop += (OP3-OP2)<<4; cp1=regs[RT3]; 5961496Sbill *cp1++='r'; *cp1++=r+'0'; *cp1=0; 5971496Sbill } 5981496Sbill delnode(p); p=p->forw; 5991496Sbill if (0<=(r2=isreg(src)) && r2<NUSE) { 6001496Sbill uses[r2]=uses[r]; uses[r]=0; 6011496Sbill } 6021496Sbill redun3(p,0); 6031496Sbill newcode(p); redunm++; flow=r; 6041496Sbill } else splitrand(p); 6051496Sbill } 6061496Sbill } else if (p->op==MOV && (p->forw->op==CVT || p->forw->op==MOVZ) 6071496Sbill && p->forw->subop&0xf /* if base or index, then forget it */ 6081496Sbill && compat(p->subop,p->forw->subop) && !source(cp1=regs[RT1]) 6091496Sbill && !indexa(cp1)) goto movit; 6101496Sbill } 6111496Sbill /* adjust 'lastrand' past any 'read' or 'modify' operands. */ 6121496Sbill lastrand=byondrd(p); 6131496Sbill /* a 'write' clobbers the register. */ 6141496Sbill if (0<=(r=isreg(lastrand)) && r<NUSE 6151496Sbill || OP2==(p->subop>>4) && 0<=(r=isreg(regs[RT2])) && r<NUSE && uses[r]==0) { 6161496Sbill /* writing a dead register is useless, but watch side effects */ 6171496Sbill switch (p->op) { 6181498Sbill case ACB: 6191496Sbill case AOBLEQ: case AOBLSS: case SOBGTR: case SOBGEQ: break; 6201498Sbill default: 6211496Sbill if (uses[r]==0) {/* no direct uses, check for use of condition codes */ 6221496Sbill register struct node *q=p; 6231496Sbill while ((q=nonlab(q->forw))->combop==JBR) q=q->ref; /* cc unused, unchanged */ 6241496Sbill if (q->op!=CBR) {/* ... and destroyed */ 6251496Sbill preg=regs+RT1; 6261496Sbill while (cp1= *preg++) { 6271496Sbill if (cp1==lastrand) {redunm++; delnode(p); return(p->forw);} 6281496Sbill if (source(cp1) || equstr(cp1,lastrand)) break; 6291496Sbill } 6301496Sbill } 6311496Sbill } 6321496Sbill flow=r; 6331496Sbill } 6341496Sbill } 6351496Sbill if (0<=(r=flow)) {olduse=uses[r]; uses[r]=0; *(short *)(regs[r])=0;} 6361496Sbill /* these two are here, rather than in bmove(), 6371496Sbill /* because I decided that it was better to go for 3-address code 6381496Sbill /* (save time) rather than fancy jbxx (save 1 byte) 6391496Sbill /* on sequences like bisl2 $64,r0; movl r0,foo 6401496Sbill */ 6411496Sbill if (p->op==BIC) {p=bicopt(p); splitrand(p); lastrand=byondrd(p);} 6421496Sbill if (p->op==BIS) {bixprep(p,JBSS); lastrand=byondrd(p);} 6431496Sbill /* now look for 'read' or 'modify' (read & write) uses */ 6441496Sbill preg=regs+RT1; 6451496Sbill while (*(cp1= *preg++)) { 6461496Sbill /* check for r */ 6471496Sbill if (lastrand!=cp1 && 0<=(r=isreg(cp1)) && r<NUSE && uses[r]==0) { 6481496Sbill uses[r]=p; cp2=regs[r]; *cp2++=p->subop; 6491506Sbill if (p->op==ASH && preg==(regs+RT1+1)) cp2[-1]=BYTE; /* stupid DEC */ 6501496Sbill if (p->op==MOV || p->op==PUSH || p->op==CVT || p->op==MOVZ || p->op==COM || p->op==NEG) { 6511496Sbill if (p->op==PUSH) cp1="-(sp)"; 6521496Sbill else { 6531496Sbill cp1=regs[RT2]; 6541496Sbill if (0<=(r=isreg(cp1)) && r<NUSE && uses[r]==0) 6551496Sbill uses[r]=olduse; /* reincarnation!! */ 6561496Sbill /* as in addl2 r0,r1; movl r1,r0; ret */ 6571496Sbill if (p->op!=MOV) cp1=0; 6581496Sbill } 6591496Sbill if (cp1) while (*cp2++= *cp1++); 6601506Sbill else *cp2=0; 6611506Sbill } else *cp2=0; 6621496Sbill continue; 6631496Sbill } 6641496Sbill /* check for (r),(r)+,-(r),[r] */ 6651496Sbill do if (*cp1=='(' || *cp1=='[') {/* get register number */ 6661496Sbill char t; 6671496Sbill cp2= ++cp1; while (*++cp1!=')' && *cp1!=']'); t= *cp1; *cp1=0; 6681496Sbill if (0<=(r=isreg(cp2)) && r<NUSE && (uses[r]==0 || uses[r]==p)) { 6691496Sbill uses[r]=p; regs[r][0]=(*--cp2=='[' ? OPX<<4 : OPB<<4); 6701496Sbill } 6711496Sbill *cp1=t; 6721496Sbill } while (*++cp1); 6731496Sbill } 6741496Sbill /* pushax or movax possibility? */ 6751496Sbill cp1=regs[RT1]; 6761496Sbill if (*cp1++=='$' && isstatic(cp1) && natural(regs[RT1])) { 6771496Sbill if (p->combop==T(MOV,LONG)) { 6781496Sbill if (regs[RT1][1]=='L' && 0!=(p->labno=getnum(regs[RT1]+2))) { 6791496Sbill cp1=p->code; while (*cp1++!=','); p->code= --cp1; 6801496Sbill } 6811496Sbill p->combop=T(MOVA,LONG); ++p->code; p->pop=0; 6821496Sbill } else if (p->combop==T(PUSH,LONG)) { 6831496Sbill p->combop=T(PUSHA,LONG); ++p->code; p->pop=0; 6841496Sbill } else if ((p->combop&0xFFFF)==T(ADD,U(LONG,OP3)) 6851496Sbill && 0<=(r=isreg(regs[RT2]))) { 6861496Sbill cp1=cp2=p->code; ++cp1; 6871496Sbill do *cp2++= *cp1; while (*cp1++!=','); cp2[-1]='['; 6881496Sbill do *cp2++= *cp1; while (*cp1++!=','); cp2[-1]=']'; 6891496Sbill if (!equstr(regs[RT3],"-(sp)")) p->combop=T(MOVA,BYTE); 6901496Sbill else {p->combop=T(PUSHA,BYTE); *cp2=0;} 6911496Sbill if (uses[r]==0) {uses[r]=p; regs[r][0]=OPX<<4;} 6921496Sbill p->pop=0; 6931496Sbill } 6941496Sbill } 6951496Sbill return(p); 6961496Sbill } 6971496Sbill 6981496Sbill ispow2(n) register long n; {/* -1 -> no; else -> log to base 2 */ 6991496Sbill register int log; 7001496Sbill if (n==0 || n&(n-1)) return(-1); log=0; 7011496Sbill for (;;) {n >>= 1; if (n==0) return(log); ++log; if (n== -1) return(log);} 7021496Sbill } 7031496Sbill 7041496Sbill bitopt(p) register struct node *p; { 7051496Sbill /* change "bitx $<power_of_2>,a" followed by JEQ or JNE 7061496Sbill /* into JBC or JBS. watch out for I/O registers. (?) 7071496Sbill /* assumes that 'splitrand' has already been called. 7081496Sbill */ 7091496Sbill register char *cp1,*cp2; int b; 7101496Sbill cp1=regs[RT1]; cp2=regs[RT2]; 7111496Sbill if (*cp1++!='$' || !okio(cp2) || p->forw->op!=CBR || p->forw->subop&-2 || 7121496Sbill 0>(b=ispow2(getnum(cp1))) || 7131496Sbill p->subop!=BYTE && (source(cp2) || indexa(cp2))) return; 7141496Sbill if (b>=bitsize[p->subop]) {/* you dummy! */ 7151496Sbill if (source(cp2)) {/* side effect: auto increment or decrement */ 7161496Sbill p->pop=0; 7171496Sbill p->op=TST; --cp1; while (*cp1++= *cp2++); 7181496Sbill regs[RT2][0]=0; newcode(p); 7191496Sbill } else delnode(p); 7201496Sbill p = p->forw; 7211496Sbill if (p->subop==JEQ) {p->combop=JBR; p->pop=0;} 7221496Sbill else delnode(p); 7231496Sbill nchange++; nbj++; return; 7241496Sbill } 7251496Sbill if (cp1=p->forw->code) {/* destination is not an internal label */ 7261496Sbill cp2=regs[RT3]; while (*cp2++= *cp1++); 7271496Sbill } 7281496Sbill if (b==0 && (p->subop==LONG || !indexa(regs[RT2]))) {/* JLB optimization, ala BLISS */ 7291496Sbill cp2=regs[RT1]; cp1=regs[RT2]; while (*cp2++= *cp1++); 7301496Sbill cp2=regs[RT2]; cp1=regs[RT3]; while (*cp2++= *cp1++); 7311496Sbill *(regs[RT3])=0; p->forw->subop += JLBC-JBC; 7321496Sbill p->forw->pop=0; 7331496Sbill } else { 7341496Sbill cp1=regs[RT1]+1; 7351496Sbill if (b>9) *cp1++= b/10 +'0'; *cp1++= b%10 +'0'; *cp1=0; /* $<bit_number> */ 7361496Sbill } 7371496Sbill nbj++; newcode(p); p->combop = p->forw->combop+((JBC-JEQ)<<8); 7381496Sbill p->labno = p->forw->labno; delnode(p->forw); 7391496Sbill p->pop=0; 7401496Sbill } 7411496Sbill 7421496Sbill isfield(n) register long n; {/* -1 -> no; else -> position of low bit */ 7431496Sbill register int pos; register long t; 7441496Sbill t= ((n-1)|n) +1; 7451496Sbill if (n!=0 && (0==t || 0==n || 0<=ispow2(t))) { 7461496Sbill pos=0; while(!(n&1)) {n >>= 1; ++pos;} return(pos); 7471496Sbill } else return(-1); 7481496Sbill } 7491496Sbill 7501496Sbill bixprep(p,bix) register struct node *p; { 7511496Sbill /* initial setup, single-bit checking for bisopt, bicopt. 7521496Sbill /* return: 0->don't bother any more; 1->worthwhile trying 7531496Sbill */ 7541496Sbill register char *cp1,*cp2; 7551496Sbill splitrand(p); cp1=regs[RT1]; cp2=regs[RT2]; 7561496Sbill if (*cp1++!='$' || 0>(pos=isfield(f=getnum(cp1))) 7571496Sbill || !okio(cp2) || indexa(cp2) || source(cp2) || !okio(lastrand)) return(0); 7581496Sbill f |= f-1; if (++f==0) siz=32-pos; else siz=ispow2(f)-pos; 7591496Sbill if (siz==1 && pos>5 && (p->subop>>4)==OP2 && (p->subop&0xF)!=BYTE 7601496Sbill && pos<bitsize[p->subop&0xF]) { 7611496Sbill p->ref = insertl(p->forw); p->combop = CBR | (bix<<8); 7621496Sbill p->pop=0; 7631496Sbill p->labno = p->ref->labno; 7641496Sbill if (pos>9) {*cp1++= pos/10 +'0'; pos %= 10;} 7651496Sbill *cp1++=pos+'0'; *cp1=0; newcode(p); nbj++; return(0); 7661496Sbill } 7671496Sbill return(1); 7681496Sbill } 7691496Sbill 7701496Sbill 7711496Sbill struct node * 7721496Sbill bicopt(p) register struct node *p; { 7731496Sbill /* use field operations or MOVZ if possible. done as part of 'bflow'. 7741496Sbill */ 7751496Sbill register char *cp1,*cp2; int r; 7763933Sroot char src[C2_ASIZE]; 7771496Sbill if (!bixprep(p,JBCC)) return(p); 7781496Sbill if (f==0) {/* the BIC isolates low order bits */ 7791496Sbill siz=pos; pos=0; 7801496Sbill if ((p->subop&0xF)==LONG && *(regs[RT2])!='$') {/* result of EXTZV is long */ 7811496Sbill /* save source of BICL in 'src' */ 7821496Sbill cp1=regs[RT2]; cp2=src; while (*cp2++= *cp1++); 7831496Sbill if (p->back->op==ASH) {/* try for more */ 7841496Sbill splitrand(p->back); cp1=regs[RT1]; cp2=regs[RT3]; 7851496Sbill if (*cp1++=='$' && *(regs[RT2])!='$' && !indexa(regs[RT2]) 7861496Sbill && 0>(f=getnum(cp1)) && equstr(src,cp2) 7871496Sbill && 0<=(r=isreg(cp2)) && r<NUSE) {/* a good ASH */ 7881496Sbill pos -= f; cp1=regs[RT2]; cp2=src; while (*cp2++= *cp1++); 7891496Sbill delnode(p->back); 7901496Sbill } 7911496Sbill } 7921496Sbill if (p->back->op==CVT || p->back->op==MOVZ) {/* greedy, aren't we? */ 7931496Sbill splitrand(p->back); cp1=regs[RT1]; cp2=regs[RT2]; 7941496Sbill if (equstr(src,cp2) && okio(cp1) && !indexa(cp1) 7951496Sbill && 0<=(r=isreg(cp2)) && r<NUSE 7961496Sbill && bitsize[p->back->subop&0xF]>=(pos+siz) 7971496Sbill && bitsize[p->back->subop>>4]>=(pos+siz)) {/* good CVT */ 7981496Sbill cp1=regs[RT1]; cp2=src; while (*cp2++= *cp1++); 7991496Sbill delnode(p->back); 8001496Sbill } 8011496Sbill } 8021496Sbill /* 'pos', 'siz' known; source of field is in 'src' */ 8031496Sbill splitrand(p); /* retrieve destination of BICL */ 8041496Sbill if (siz==8 && pos==0) { 8051496Sbill p->combop = T(MOVZ,U(BYTE,LONG)); 8061496Sbill sprintf(line,"%s,%s",src,lastrand); 8071496Sbill } else { 8081496Sbill p->combop = T(EXTZV,LONG); 8091496Sbill sprintf(line,"$%d,$%d,%s,%s",pos,siz,src,lastrand); 8101496Sbill } 8111496Sbill p->pop=0; 8121496Sbill p->code = copy(line); nfield++; return(p); 8131496Sbill }/* end EXTZV possibility */ 8141496Sbill }/* end low order bits */ 8151496Sbill /* unfortunately, INSV clears the condition codes, thus cannot be used */ 8161496Sbill /* else {/* see if BICL2 of positive field should be INSV $0 */ 8171496Sbill /* if (p->subop==(LONG | (OP2<<4)) && 6<=(pos+siz)) { 8181496Sbill /* p->combop = INSV; 8191496Sbill /* sprintf(line,"$0,$%d,$%d,%s",pos,siz,lastrand); 8201496Sbill /* p->code = copy(line); nfield++; return(p); 8211496Sbill /* } 8221496Sbill /* } 8231496Sbill */ 8241496Sbill return(p); 8251496Sbill } 8261496Sbill 8271496Sbill jumpsw() 8281496Sbill { 8291496Sbill register struct node *p, *p1; 8301496Sbill register t; 8311496Sbill int nj; 8321496Sbill 8331496Sbill t = 0; 8341496Sbill nj = 0; 8351496Sbill for (p=first.forw; p!=0; p = p->forw) 8361496Sbill p->seq = ++t; 8371496Sbill for (p=first.forw; p!=0; p = p1) { 8381496Sbill p1 = p->forw; 8391496Sbill if (p->op == CBR && p1->op==JBR && p->ref && p1->ref 8401496Sbill && abs(p->seq - p->ref->seq) > abs(p1->seq - p1->ref->seq)) { 8411496Sbill if (p->ref==p1->ref) 8421496Sbill continue; 8431496Sbill p->subop = revbr[p->subop]; 8441496Sbill p->pop=0; 8451496Sbill t = p1->ref; 8461496Sbill p1->ref = p->ref; 8471496Sbill p->ref = t; 8481496Sbill t = p1->labno; 8491496Sbill p1->labno = p->labno; 8501496Sbill p->labno = t; 8511496Sbill #ifdef COPYCODE 8521496Sbill if (p->labno == 0) { 8531496Sbill t = p1->code; p1->code = p->code; p->code = t; 8541496Sbill } 8551496Sbill #endif 8561496Sbill nrevbr++; 8571496Sbill nj++; 8581496Sbill } 8591496Sbill } 8601496Sbill return(nj); 8611496Sbill } 8621496Sbill 8631496Sbill addsob() 8641496Sbill { 8651496Sbill register struct node *p, *p1, *p2, *p3; 8661496Sbill 8671496Sbill for (p = &first; (p1 = p->forw)!=0; p = p1) { 8681496Sbill if (p->combop==T(DEC,LONG) && p1->op==CBR) { 8693933Sroot if (abs(p->seq - p1->ref->seq) > 8) continue; 8701496Sbill if (p1->subop==JGE || p1->subop==JGT) { 8711496Sbill if (p1->subop==JGE) p->combop=SOBGEQ; else p->combop=SOBGTR; 8721496Sbill p->pop=0; 8731496Sbill p->labno = p1->labno; delnode(p1); nsob++; 8741496Sbill } 8751496Sbill } else if (p->combop==T(INC,LONG)) { 8761496Sbill if (p1->op==LABEL && p1->refc==1 && p1->forw->combop==T(CMP,LONG) 8771496Sbill && (p2=p1->forw->forw)->combop==T(CBR,JLE) 8781496Sbill && (p3=p2->ref->back)->combop==JBR && p3->ref==p1 8791496Sbill && p3->forw->op==LABEL && p3->forw==p2->ref) { 8801496Sbill /* change INC LAB: CMP to LAB: INC CMP */ 8811496Sbill p->back->forw=p1; p1->back=p->back; 8821496Sbill p->forw=p1->forw; p1->forw->back=p; 8831496Sbill p->back=p1; p1->forw=p; 8841496Sbill p1=p->forw; 8851496Sbill /* adjust beginning value by 1 */ 8861496Sbill p2=alloc(sizeof first); p2->combop=T(DEC,LONG); 8871496Sbill p2->pop=0; 8881496Sbill p2->forw=p3; p2->back=p3->back; p3->back->forw=p2; 8891496Sbill p3->back=p2; p2->code=p->code; p2->labno=0; 8901496Sbill } 8911496Sbill if (p1->combop==T(CMP,LONG) && (p2=p1->forw)->op==CBR) { 8921496Sbill register char *cp1,*cp2; 8931496Sbill splitrand(p1); if (!equstr(p->code,regs[RT1])) continue; 8943933Sroot if (abs(p->seq - p2->ref->seq)>8) {/* outside byte displ range */ 8951496Sbill if (p2->subop!=JLE) continue; 8961496Sbill p->combop=T(ACB,LONG); 8971496Sbill cp2=regs[RT1]; cp1=regs[RT2]; while (*cp2++= *cp1++); /* limit */ 8981496Sbill cp2=regs[RT2]; cp1="$1"; while (*cp2++= *cp1++); /* increment */ 8991496Sbill cp2=regs[RT3]; cp1=p->code; while (*cp2++= *cp1++); /* index */ 9001496Sbill p->pop=0; newcode(p); 9011496Sbill p->labno = p2->labno; delnode(p2); delnode(p1); nsob++; 9021496Sbill } else if (p2->subop==JLE || p2->subop==JLT) { 9031496Sbill if (p2->subop==JLE) p->combop=AOBLEQ; else p->combop=AOBLSS; 9041496Sbill cp2=regs[RT1]; cp1=regs[RT2]; while (*cp2++= *cp1++); /* limit */ 9051496Sbill cp2=regs[RT2]; cp1=p->code; while (*cp2++= *cp1++); /* index */ 9061496Sbill p->pop=0; newcode(p); 9071496Sbill p->labno = p2->labno; delnode(p2); delnode(p1); nsob++; 9081496Sbill } 9091496Sbill } 9101496Sbill } 9111496Sbill } 9121496Sbill } 9131496Sbill 9141496Sbill abs(x) 9151496Sbill { 9161496Sbill return(x<0? -x: x); 9171496Sbill } 9181496Sbill 9191496Sbill equop(p1, p2) 9201496Sbill register struct node *p1; 9211496Sbill struct node *p2; 9221496Sbill { 9231496Sbill register char *cp1, *cp2; 9241496Sbill 9251496Sbill if (p1->combop != p2->combop) 9261496Sbill return(0); 9271496Sbill if (p1->op>0 && p1->op<MOV) 9281496Sbill return(0); 9291496Sbill if (p1->op==MOVA && p1->labno!=p2->labno) return(0); 9301496Sbill cp1 = p1->code; 9311496Sbill cp2 = p2->code; 9321496Sbill if (cp1==0 && cp2==0) 9331496Sbill return(1); 9341496Sbill if (cp1==0 || cp2==0) 9351496Sbill return(0); 9361496Sbill while (*cp1 == *cp2++) 9371496Sbill if (*cp1++ == 0) 9381496Sbill return(1); 9391496Sbill return(0); 9401496Sbill } 9411496Sbill 9421496Sbill delnode(p) register struct node *p; { 9431496Sbill p->back->forw = p->forw; 9441496Sbill p->forw->back = p->back; 9451496Sbill } 9461496Sbill 9471496Sbill decref(p) 9481496Sbill register struct node *p; 9491496Sbill { 9501496Sbill if (p && --p->refc <= 0) { 9511496Sbill nrlab++; 9521496Sbill delnode(p); 9531496Sbill } 9541496Sbill } 9551496Sbill 9561496Sbill struct node * 9571496Sbill nonlab(ap) 9581496Sbill struct node *ap; 9591496Sbill { 9601496Sbill register struct node *p; 9611496Sbill 9621496Sbill p = ap; 9631496Sbill while (p && p->op==LABEL) 9641496Sbill p = p->forw; 9651496Sbill return(p); 9661496Sbill } 9671496Sbill 9681496Sbill clearuse() { 9691496Sbill register struct node **i; 9701496Sbill for (i=uses+NUSE; i>uses;) *--i=0; 9711496Sbill } 9721496Sbill 9731496Sbill clearreg() { 9741496Sbill register short **i; 9751496Sbill for (i=regs+NREG; i>regs;) **--i=0; 9761496Sbill conloc[0] = 0; ccloc[0] = 0; 9771496Sbill } 9781496Sbill 9791496Sbill savereg(ai, s, type) 9801496Sbill register char *s; 9811496Sbill { 9821496Sbill register char *p, *sp; 9831496Sbill 9841496Sbill sp = p = regs[ai]; 9851496Sbill if (source(s)) /* side effects in addressing */ 9861496Sbill return; 9871496Sbill /* if any indexing, must be parameter or local */ 9881496Sbill /* indirection (as in "*-4(fp)") is ok, however */ 9891496Sbill *p++ = type; 9901496Sbill while (*p++ = *s) 9911496Sbill if (*s=='[' || *s++=='(' && *s!='a' && *s!='f') {*sp = 0; return;} 9921496Sbill } 9931496Sbill 9941496Sbill dest(s,type) 9951496Sbill register char *s; 9961496Sbill { 9971496Sbill register int i; 9981496Sbill 9991496Sbill source(s); /* handle addressing side effects */ 10001496Sbill if ((i = isreg(s)) >= 0) { 10011496Sbill *(short *)(regs[i]) = 0; /* if register destination, that reg is a goner */ 10021496Sbill if (DOUBLE==(type&0xF) || DOUBLE==((type>>4)&0xF)) 10031496Sbill *(short *)(regs[i+1]) = 0; /* clobber two at once */ 10041496Sbill } 10051496Sbill for (i=NREG; --i>=0;) 10061496Sbill if (regs[i][1]=='*' && equstr(s, regs[i]+2)) 10071496Sbill *(short *)(regs[i]) = 0; /* previous indirection through destination is invalid */ 10081496Sbill while ((i = findrand(s,0)) >= 0) /* previous values of destination are invalid */ 10091496Sbill *(short *)(regs[i]) = 0; 10101496Sbill if (!natural(s)) {/* wild store, everything except constants vanishes */ 10111496Sbill for (i=NREG; --i>=0;) if (regs[i][1] != '$') *(short *)(regs[i]) = 0; 10121496Sbill conloc[0] = 0; ccloc[0] = 0; 10131496Sbill } else setcc(s,type); /* natural destinations set condition codes */ 10141496Sbill } 10151496Sbill 10161496Sbill splitrand(p) struct node *p; { 10171496Sbill /* separate operands at commas, set up 'regs' and 'lastrand' */ 10181496Sbill register char *p1, *p2; register char **preg; 10191496Sbill preg=regs+RT1; 10201496Sbill if (p1=p->code) while (*p1) { 10211496Sbill lastrand=p2= *preg++; 10221496Sbill while (*p1) if (','==(*p2++= *p1++)) {--p2; break;} 10231496Sbill *p2=0; 10241496Sbill } 10251496Sbill while (preg<(regs+RT1+5)) *(*preg++)=0; 10261496Sbill } 10271496Sbill 10281496Sbill compat(have, want) { 10291496Sbill register int hsrc, hdst; 10301496Sbill if (0==(want &= 0xF)) return(1); /* anything satisfies a wildcard want */ 10311496Sbill hsrc=have&0xF; if (0==(hdst=((have>>4)&0xF)) || hdst>=OP2) hdst=hsrc; 10321496Sbill if (want>=FLOAT) return(hdst==want && hsrc==want); 10331496Sbill /* FLOAT, DOUBLE not compat: rounding */ 10341496Sbill return(hsrc>=want && hdst>=want && hdst<FLOAT); 10351496Sbill } 10361496Sbill 10371496Sbill equtype(t1,t2) {return(compat(t1,t2) && compat(t2,t1));} 10381496Sbill 10391496Sbill findrand(as, type) 10401496Sbill char *as; 10411496Sbill { 10421496Sbill register char **i; 10431496Sbill for (i = regs+NREG; --i>=regs;) { 10441496Sbill if (**i && equstr(*i+1, as) && compat(**i,type)) 10451496Sbill return(i-regs); 10461496Sbill } 10471496Sbill return(-1); 10481496Sbill } 10491496Sbill 10501496Sbill isreg(s) 10511496Sbill register char *s; 10521496Sbill { 10531496Sbill if (*s++!='r' || !isdigit(*s++)) return(-1); 10541496Sbill if (*s==0) return(*--s-'0'); 10551496Sbill if (*(s-1)=='1' && isdigit(*s++) && *s==0) return(10+*--s-'0'); 10561496Sbill return(-1); 10571496Sbill } 10581496Sbill 10591496Sbill check() 10601496Sbill { 10611496Sbill register struct node *p, *lp; 10621496Sbill 10631496Sbill lp = &first; 10641496Sbill for (p=first.forw; p!=0; p = p->forw) { 10651496Sbill if (p->back != lp) 10661496Sbill abort(-1); 10671496Sbill lp = p; 10681496Sbill } 10691496Sbill } 10701496Sbill 10711496Sbill source(ap) 10721496Sbill char *ap; 10731496Sbill { 10741496Sbill register char *p1, *p2; 10751496Sbill 10761496Sbill p1 = ap; 10771496Sbill p2 = p1; 10781496Sbill if (*p1==0) 10791496Sbill return(0); 10801496Sbill while (*p2++ && *(p2-1)!='['); 10811496Sbill if (*p1=='-' && *(p1+1)=='(' 10821496Sbill || *p1=='*' && *(p1+1)=='-' && *(p1+2)=='(' 10831496Sbill || *(p2-2)=='+') { 10841496Sbill while (*p1 && *p1++!='r'); 10851496Sbill if (isdigit(*p1++)) 10861496Sbill if (isdigit(*p1)) *(short *)(regs[10+*p1-'0'])=0; 10871496Sbill else *(short *)(regs[*--p1-'0'])=0; 10881496Sbill return(1); 10891496Sbill } 10901496Sbill return(0); 10911496Sbill } 10921496Sbill 10931496Sbill newcode(p) struct node *p; { 10941496Sbill register char *p1,*p2,**preg; 10951496Sbill preg=regs+RT1; p2=line; 10961496Sbill while (*(p1= *preg++)) {while (*p2++= *p1++); *(p2-1)=',';} 10971496Sbill *--p2=0; 10981496Sbill p->code=copy(line); 10991496Sbill } 11001496Sbill 11011496Sbill repladdr(p) 11021496Sbill struct node *p; 11031496Sbill { 11041496Sbill register r; 11051496Sbill register char *p1, *p2; 11061496Sbill char **preg; int nrepl; 11071496Sbill 11081496Sbill preg=regs+RT1; nrepl=0; 11091496Sbill while (lastrand!=(p1= *preg++)) 11101496Sbill if (!source(p1) && 0<=(r=findrand(p1,p->subop))) { 11111496Sbill *p1++='r'; if (r>9) {*p1++='1'; r -= 10;} *p1++=r+'0'; *p1=0; 11121496Sbill nrepl++; nsaddr++; 11131496Sbill } 11141496Sbill if (nrepl) newcode(p); 11151496Sbill } 11161496Sbill 11171496Sbill /* movedat() 11181496Sbill /* { 11191496Sbill /* register struct node *p1, *p2; 11201496Sbill /* struct node *p3; 11211496Sbill /* register seg; 11221496Sbill /* struct node data; 11231496Sbill /* struct node *datp; 11241496Sbill /* 11251496Sbill /* if (first.forw == 0) 11261496Sbill /* return; 11271496Sbill /* datp = &data; 11281496Sbill /* for (p1 = first.forw; p1!=0; p1 = p1->forw) { 11291496Sbill /* if (p1->op == DATA) { 11301496Sbill /* p2 = p1->forw; 11311496Sbill /* while (p2 && p2->op!=TEXT) 11321496Sbill /* p2 = p2->forw; 11331496Sbill /* if (p2==0) 11341496Sbill /* break; 11351496Sbill /* p3 = p1->back; 11361496Sbill /* p1->back->forw = p2->forw; 11371496Sbill /* p2->forw->back = p3; 11381496Sbill /* p2->forw = 0; 11391496Sbill /* datp->forw = p1; 11401496Sbill /* p1->back = datp; 11411496Sbill /* p1 = p3; 11421496Sbill /* datp = p2; 11431496Sbill /* } 11441496Sbill /* } 11451496Sbill /* if (data.forw) { 11461496Sbill /* datp->forw = first.forw; 11471496Sbill /* first.forw->back = datp; 11481496Sbill /* data.forw->back = &first; 11491496Sbill /* first.forw = data.forw; 11501496Sbill /* } 11511496Sbill /* seg = -1; 11521496Sbill /* for (p1 = first.forw; p1!=0; p1 = p1->forw) { 11531496Sbill /* if (p1->op==TEXT||p1->op==DATA||p1->op==BSS) { 11541496Sbill /* if (p1->op == seg || p1->forw&&p1->forw->op==seg) { 11551496Sbill /* p1->back->forw = p1->forw; 11561496Sbill /* p1->forw->back = p1->back; 11571496Sbill /* p1 = p1->back; 11581496Sbill /* continue; 11591496Sbill /* } 11601496Sbill /* seg = p1->op; 11611496Sbill /* } 11621496Sbill /* } 11631496Sbill /* } 11641496Sbill */ 11651496Sbill 11661496Sbill redunbr(p) 11671496Sbill register struct node *p; 11681496Sbill { 11691496Sbill register struct node *p1; 11701496Sbill register char *ap1; 11711496Sbill char *ap2; 11721496Sbill 11731496Sbill if ((p1 = p->ref) == 0) 11741496Sbill return; 11751496Sbill p1 = nonlab(p1); 11761496Sbill if (p1->op==TST) { 11771496Sbill splitrand(p1); 11781496Sbill savereg(RT2, "$0", p1->subop); 11791496Sbill } else if (p1->op==CMP) 11801496Sbill splitrand(p1); 11811496Sbill else 11821496Sbill return; 11831496Sbill if (p1->forw->op==CBR) { 11841496Sbill ap1 = findcon(RT1, p1->subop); 11851496Sbill ap2 = findcon(RT2, p1->subop); 11861496Sbill p1 = p1->forw; 11871496Sbill if (compare(p1->subop, ap1, ap2)) { 11881496Sbill nredunj++; 11891496Sbill nchange++; 11901496Sbill decref(p->ref); 11911496Sbill p->ref = p1->ref; 11921496Sbill p->labno = p1->labno; 11931496Sbill #ifdef COPYCODE 11941496Sbill if (p->labno == 0) 11951496Sbill p->code = p1->code; 11961496Sbill if (p->ref) 11971496Sbill #endif 11981496Sbill p->ref->refc++; 11991496Sbill } 12001496Sbill } else if (p1->op==TST && equstr(regs[RT1],ccloc+1) && 12011496Sbill equtype(ccloc[0],p1->subop)) { 12021496Sbill p1=insertl(p1->forw); decref(p->ref); p->ref=p1; 12031496Sbill nrtst++; nchange++; 12041496Sbill } 12051496Sbill } 12061496Sbill 12071496Sbill char * 12081496Sbill findcon(i, type) 12091496Sbill { 12101496Sbill register char *p; 12111496Sbill register r; 12121496Sbill 12131496Sbill p = regs[i]; 12141496Sbill if (*p=='$') 12151496Sbill return(p); 12161496Sbill if ((r = isreg(p)) >= 0 && compat(regs[r][0],type)) 12171496Sbill return(regs[r]+1); 12181496Sbill if (equstr(p, conloc)) 12191496Sbill return(conval+1); 12201496Sbill return(p); 12211496Sbill } 12221496Sbill 12231496Sbill compare(op, acp1, acp2) 12241496Sbill char *acp1, *acp2; 12251496Sbill { 12261496Sbill register char *cp1, *cp2; 12271496Sbill register n1; 12281496Sbill int n2; int sign; 12291496Sbill 12301496Sbill cp1 = acp1; 12311496Sbill cp2 = acp2; 12321496Sbill if (*cp1++ != '$' || *cp2++ != '$') 12331496Sbill return(0); 12341496Sbill n1 = 0; sign=1; if (*cp2=='-') {++cp2; sign= -1;} 12351496Sbill while (isdigit(*cp2)) {n1 *= 10; n1 += (*cp2++ - '0')*sign;} 12361496Sbill n2 = n1; 12371496Sbill n1 = 0; sign=1; if (*cp1=='-') {++cp1; sign= -1;} 12381496Sbill while (isdigit(*cp1)) {n1 *= 10; n1 += (*cp1++ - '0')*sign;} 12391496Sbill if (*cp1=='+') 12401496Sbill cp1++; 12411496Sbill if (*cp2=='+') 12421496Sbill cp2++; 12431496Sbill do { 12441496Sbill if (*cp1++ != *cp2) 12451496Sbill return(0); 12461496Sbill } while (*cp2++); 12471496Sbill cp1 = n1; 12481496Sbill cp2 = n2; 12491496Sbill switch(op) { 12501496Sbill 12511496Sbill case JEQ: 12521496Sbill return(cp1 == cp2); 12531496Sbill case JNE: 12541496Sbill return(cp1 != cp2); 12551496Sbill case JLE: 12561496Sbill return(((int)cp1) <= ((int)cp2)); 12571496Sbill case JGE: 12581496Sbill return(((int)cp1) >= ((int)cp2)); 12591496Sbill case JLT: 12601496Sbill return(((int)cp1) < ((int)cp2)); 12611496Sbill case JGT: 12621496Sbill return(((int)cp1) > ((int)cp2)); 12631496Sbill case JLO: 12641496Sbill return(cp1 < cp2); 12651496Sbill case JHI: 12661496Sbill return(cp1 > cp2); 12671496Sbill case JLOS: 12681496Sbill return(cp1 <= cp2); 12691496Sbill case JHIS: 12701496Sbill return(cp1 >= cp2); 12711496Sbill } 12721496Sbill return(0); 12731496Sbill } 12741496Sbill 12751496Sbill setcon(cv, cl, type) 12761496Sbill register char *cv, *cl; 12771496Sbill { 12781496Sbill register char *p; 12791496Sbill 12801496Sbill if (*cv != '$') 12811496Sbill return; 12821496Sbill if (!natural(cl)) 12831496Sbill return; 12841496Sbill p = conloc; 12851496Sbill while (*p++ = *cl++); 12861496Sbill p = conval; 12871496Sbill *p++ = type; 12881496Sbill while (*p++ = *cv++); 12891496Sbill } 12901496Sbill 12911496Sbill equstr(p1, p2) 12921496Sbill register char *p1, *p2; 12931496Sbill { 12941496Sbill do { 12951496Sbill if (*p1++ != *p2) 12961496Sbill return(0); 12971496Sbill } while (*p2++); 12981496Sbill return(1); 12991496Sbill } 13001496Sbill 13011496Sbill setcc(ap,type) 13021496Sbill char *ap; 13031496Sbill { 13041496Sbill register char *p, *p1; 13051496Sbill 13061496Sbill p = ap; 13071496Sbill if (!natural(p)) { 13081496Sbill ccloc[0] = 0; 13091496Sbill return; 13101496Sbill } 13111496Sbill p1 = ccloc; 13121496Sbill *p1++ = type; 13131496Sbill while (*p1++ = *p++); 13141496Sbill } 13151496Sbill 13161496Sbill okio(p) register char *p; {/* 0->probable I/O space address; 1->not */ 13171496Sbill if (ioflag && (!natural(p) || 0>getnum(p))) return(0); 13181496Sbill return(1); 13191496Sbill } 13201496Sbill 13211496Sbill indexa(p) register char *p; {/* 1-> uses [r] addressing mode; 0->doesn't */ 13221496Sbill while (*p) if (*p++=='[') return(1); 13231496Sbill return(0); 13241496Sbill } 13251496Sbill 13261496Sbill natural(p) 13271496Sbill register char *p; 13281496Sbill {/* 1->simple local, parameter, global, or register; 0->otherwise */ 13291496Sbill if (*p=='*' || *p=='(' || *p=='-'&&*(p+1)=='(' || *p=='$'&&getnum(p+1)) 13301496Sbill return(0); 13311496Sbill while (*p++); 13321496Sbill p--; 13331496Sbill if (*--p=='+' || *p==']' || *p==')' && *(p-2)!='a' && *(p-2)!='f') 13341496Sbill return(0); 13351496Sbill return(1); 13361496Sbill } 13371496Sbill 13381496Sbill /* 13391496Sbill ** Tell if an argument is most likely static. 13401496Sbill */ 13411496Sbill 13421496Sbill isstatic(cp) 13431496Sbill register char *cp; 13441496Sbill { 13451496Sbill if (*cp == '_' || *cp == 'L' || (*cp++ == 'v' && *cp == '.')) 13461496Sbill return (1); 13471496Sbill return (0); 13481496Sbill } 1349