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