118381Sralph #ifndef lint 2*34255Sdonn static char sccsid[] = "@(#)c21.c 4.24 05/11/88"; 318381Sralph #endif 41498Sbill /* char C21[] = {"@(#)c21.c 1.83 80/10/16 21:18:22 JFR"}; /* sccs ident */ 51496Sbill 61496Sbill /* 71496Sbill * C object code improver-- second part 81496Sbill */ 91496Sbill 101496Sbill #include "c2.h" 111496Sbill #include <stdio.h> 121496Sbill #include <ctype.h> 131496Sbill 141496Sbill #define NUSE 6 151496Sbill int ioflag; 161496Sbill int biti[NUSE] = {1,2,4,8,16,32}; 175794Sroot int bitsize[] = { /* index by type codes */ 186958Srrh 0, /* 0 not allocated */ 196958Srrh 8, /* 1 BYTE */ 206958Srrh 16, /* 2 WORD */ 216958Srrh 32, /* 3 LONG */ 2232986Sdonn 32, /* 4 FFLOAT */ 236958Srrh 64, /* 5 DFLOAT */ 246958Srrh 64, /* 6 QUAD */ 256958Srrh 0, /* 7 OP2 */ 266958Srrh 0, /* 8 OP3 */ 276958Srrh 0, /* 9 OPB */ 286958Srrh 0, /* 10 OPX */ 296958Srrh 64, /* 11 GFLOAT */ 306958Srrh 128, /* 12 HFLOAT */ 316958Srrh 128 /* 13 OCTA */ 325794Sroot }; 331496Sbill int pos,siz; long f; /* for bit field communication */ 341496Sbill struct node *uses[NUSE]; /* for backwards flow analysis */ 351496Sbill char *lastrand; /* last operand of instruction */ 361496Sbill struct node *bflow(); 371496Sbill struct node *bicopt(); 381496Sbill char *findcon(); 3918381Sralph char *strcpy(); 401496Sbill 411496Sbill redun3(p,split) register struct node *p; int split; { 421496Sbill /* check for 3 addr instr which should be 2 addr */ 431496Sbill if (OP3==((p->subop>>4)&0xF)) { 441496Sbill if (split) splitrand(p); 451496Sbill if (equstr(regs[RT1],regs[RT3]) 461496Sbill && (p->op==ADD || p->op==MUL || p->op==BIS || p->op==XOR)) { 471496Sbill register char *t=regs[RT1]; regs[RT1]=regs[RT2]; regs[RT2]=t; 481496Sbill } 491496Sbill if (equstr(regs[RT2],regs[RT3])) { 501496Sbill p->subop=(p->subop&0xF)|(OP2<<4); p->pop=0; 511496Sbill lastrand=regs[RT2]; *regs[RT3]=0; return(1); 521496Sbill } 531496Sbill } return(0); 541496Sbill } 551496Sbill 561496Sbill bmove() { 571496Sbill register struct node *p, *lastp; register char *cp1,*cp2; register int r; 581496Sbill refcount(); 591496Sbill for (p=lastp= &first; 0!=(p=p->forw); lastp=p); 601496Sbill clearreg(); clearuse(); 611496Sbill for (p=lastp; p!= &first; p=p->back) { 621496Sbill if (debug) { 631496Sbill printf("Uses:\n"); 641496Sbill for (r=NUSE;--r>=0;) if (uses[r]) 651496Sbill printf("%d: %s\n",r,uses[r]->code? uses[r]->code:""); 661496Sbill printf("-\n"); 671496Sbill } 681496Sbill r=(p->subop>>4)&0xF; 691496Sbill if (OP2==r && (cp1=p->code, *cp1++)=='$' && *cp1++=='0' && *cp1++==',' && 701496Sbill !source(cp1)) {/* a no-op unless MUL or DIV */ 711496Sbill if (p->op==MUL) {p->op=MOV; p->subop&=0xF; p->pop=0;} 721496Sbill else if (p->op==DIV) fprintf(stderr,"c2: zero divide\n"); 731496Sbill else {delnode(p); redunm++; continue;} 741496Sbill } 751496Sbill if (OP3==r && 0!=redun3(p,1)) {newcode(p); redunm++;} 761496Sbill switch (p->op) { 771496Sbill case LABEL: case DLABEL: 781496Sbill for (r=NUSE; --r>=0;) 791496Sbill if (uses[r]) p->ref=(struct node *) (((int)p->ref)|biti[r]); 801496Sbill break; 811496Sbill case CALLS: 821496Sbill clearuse(); goto std; 831496Sbill case 0: 8417720Sralph /* 8517720Sralph * Be conservative -- if we don't know what it is, then we 8617720Sralph * assume that it can set anything. 8717720Sralph */ 8817720Sralph for ( r = 0; r < NUSE; ++r ) 8917720Sralph uses[r] = p; 9017720Sralph break; 911496Sbill case SUB: 921496Sbill if ((p->subop&0xF)!=LONG) goto std; cp1=p->code; 931496Sbill if (*cp1++!='$') goto std; splitrand(p); 941496Sbill if (equstr(regs[RT2],"fp") && !indexa(regs[RT1])) {/* address comp. */ 953933Sroot char buf[C2_ASIZE]; cp2=buf; *cp2++='-'; 961496Sbill cp1=regs[RT1]+1; while (*cp2++= *cp1++); --cp2; 971496Sbill cp1="(fp),"; while (*cp2++= *cp1++); --cp2; 981496Sbill cp1=regs[RT3]; while (*cp2++= *cp1++); 991496Sbill p->code=copy(buf); p->combop=T(MOVA,LONG); p->pop=0; 1001496Sbill } else if (*cp1++=='-' && 0<=(r=getnum(cp1))) { 1011496Sbill p->op=ADD; p->pop=0; *--cp1='$'; p->code=cp1; 1021496Sbill } goto std; 1031496Sbill case ADD: 1041496Sbill if ((p->subop&0xF)!=LONG) goto std; cp1=p->code; 1051496Sbill if (*cp1++!='$') goto std; splitrand(p); 1061496Sbill if (isstatic(cp1) && (r=isreg(regs[RT2]))>=0 && r<NUSE && uses[r]==p->forw) 1071496Sbill { 1081496Sbill /* address comp: 1091496Sbill ** addl2 $_foo,r0 \ movab _foo[r0],bar 1101496Sbill ** movl r0,bar / 1111496Sbill */ 1121496Sbill register struct node *pnext = p->forw; 1133933Sroot char buf[C2_ASIZE]; 1141496Sbill 1151496Sbill if (pnext->op == MOV && pnext->subop == LONG) 1161496Sbill { 1171496Sbill cp1 = ®s[RT1][1]; cp2 = &buf[0]; 1181496Sbill while (*cp2++ = *cp1++) ; cp2--; 1191496Sbill splitrand(pnext); 1201496Sbill if (r == isreg(regs[RT1])) 1211496Sbill { 1221496Sbill delnode(p); p = pnext; 1231496Sbill p->op = MOVA; p->subop = BYTE; 1241496Sbill p->pop = 0; 1251496Sbill cp1 = regs[RT1]; *cp2++ = '['; 1261496Sbill while (*cp2++ = *cp1++) ; cp2--; 1271496Sbill *cp2++ = ']'; *cp2++ = ','; 1281496Sbill cp1 = regs[RT2]; 1291496Sbill while (*cp2++ = *cp1++) ; 1301496Sbill p->code = copy(buf); 1311496Sbill } 1321496Sbill } 1331496Sbill } 1341496Sbill else 1351496Sbill if (equstr(regs[RT2],"fp") && !indexa(regs[RT1])) {/* address comp. */ 1361496Sbill cp2=cp1-1; cp1=regs[RT1]+1; while (*cp2++= *cp1++); --cp2; 1371496Sbill cp1="(fp)"; while (*cp2++= *cp1++); *--cp2=','; 1381496Sbill p->combop=T(MOVA,LONG); p->pop=0; 1391496Sbill } else if (*cp1++=='-' && 0<=(r=getnum(cp1))) { 1401496Sbill p->op=SUB; p->pop=0; *--cp1='$'; p->code=cp1; 1411496Sbill } 1421496Sbill /* fall thru ... */ 1431496Sbill case CASE: 1441496Sbill default: std: 1451496Sbill p=bflow(p); break; 1461496Sbill case MUL: 1471496Sbill { 1481496Sbill /* 1491496Sbill ** Change multiplication by constant powers of 2 to 1501496Sbill ** shifts. 1511496Sbill */ 1521496Sbill splitrand(p); 1531496Sbill if (regs[RT1][0] != '$' || regs[RT1][1] == '-') goto std; 1541496Sbill if ((r = ispow2(getnum(®s[RT1][1]))) < 0) goto std; 1551496Sbill switch (r) 1561496Sbill { 1571496Sbill case 0: /* mull3 $1,x,y */ 1581496Sbill if (p->subop == U(LONG,OP3)) 1591496Sbill { 1601496Sbill if (equstr(regs[RT2], regs[RT3])) 1611496Sbill { 1621496Sbill delnode(p); p = p->forw; 1631496Sbill } 1641496Sbill else 1651496Sbill { 1661496Sbill p->op = MOV; p->subop = LONG; 1671496Sbill p->pop = 0; newcode(p); nchange++; 1681496Sbill } 1691496Sbill } 1701496Sbill else 1711496Sbill if (p->subop == U(LONG,OP2)) 1721496Sbill { 1731496Sbill delnode(p); p = p->forw; 1741496Sbill } 1751496Sbill goto std; 1761496Sbill 1771496Sbill case 1: /* mull2 $2,x */ 1781496Sbill if (p->subop == U(LONG, OP2) && !source(regs[RT2])) 1791496Sbill { 1801496Sbill strcpy(regs[RT1], regs[RT2]); 1811496Sbill p->op = ADD; p->pop = 0; newcode(p); nchange++; 1821496Sbill } 1831496Sbill goto std; 1841496Sbill } 1851496Sbill if(p->subop==U(LONG,OP3)||(p->subop==U(LONG,OP2)&&!source(regs[RT2]))) 1861496Sbill { 1871496Sbill if (p->subop == U(LONG,OP2)) 1881496Sbill strcpy(regs[RT3], regs[RT2]); 1891496Sbill sprintf(regs[RT1], "$%d", r); 1901496Sbill p->op = ASH; p->subop = LONG; 1911496Sbill p->pop = 0; newcode(p); nchange++; 1921496Sbill } 1931496Sbill goto std; 1941496Sbill } 1951496Sbill case ASH: 1961496Sbill { 1971496Sbill /* address comp: 1981496Sbill ** ashl $1,bar,r0 \ movl bar,r0 1991496Sbill ** movab _foo[r0] / movaw _foo[r0] 2001496Sbill ** 2011496Sbill ** ashl $2,r0,r0 \ moval _foo[r0] 2021496Sbill ** movab _foo[r0] / 2031496Sbill */ 2041496Sbill register struct node *pf; 2051496Sbill register int shfrom, shto; 2061496Sbill long shcnt; 2071496Sbill char *regfrom; 2081496Sbill 2091496Sbill splitrand(p); 2101496Sbill if (regs[RT1][0] != '$') goto std; 2111496Sbill if ((shcnt = getnum(®s[RT1][1])) < 1 || shcnt > 3) goto std; 2121496Sbill if ((shfrom = isreg(regs[RT2])) >= 0) 21318381Sralph regfrom = copy(regs[RT2]); 2141496Sbill if ((shto = isreg(regs[RT3])) >= 0 && shto<NUSE) 2151496Sbill { 2161496Sbill int regnum; 2171496Sbill 2181496Sbill if (uses[shto] != (pf = p->forw)) goto ashadd; 2191496Sbill if (pf->op != MOVA && pf->op != PUSHA) goto ashadd; 2201496Sbill if (pf->subop != BYTE) goto ashadd; 2211496Sbill splitrand(pf); 2221496Sbill if (!indexa(regs[RT1])) goto std; 2231496Sbill cp2 = regs[RT1]; 2241496Sbill if(!isstatic(cp2)) goto std; 2251496Sbill while (*cp2++ != '[') ; 2261496Sbill if (*cp2++ != 'r' || !isdigit(*cp2)) goto std; 2271496Sbill regnum = *cp2++ - '0'; 2281496Sbill if (isdigit(*cp2)) 2291496Sbill { 2301496Sbill if (cp2[1] != ']') goto std; 2311496Sbill regnum *= 10; regnum += *cp2 - '0'; 2321496Sbill } 2331496Sbill if (regnum != shto) goto std; 2341496Sbill if (shfrom >= 0) /* ashl $N,r*,r0 */ 2351496Sbill { 2361496Sbill delnode(p); 237*34255Sdonn p = pf; 2381496Sbill if (shfrom != shto) 2391496Sbill { 240*34255Sdonn uses[shto] = NULL; splitrand(p); 2411496Sbill cp2=regs[RT1]; while (*cp2++!='['); 2421496Sbill cp1=regfrom; while (*cp2++= *cp1++); 24318381Sralph *--cp2 = ']'; 24418381Sralph *++cp2 = '\0'; 245*34255Sdonn newcode(p); 2461496Sbill } 247*34255Sdonn if (p->op == MOVA) 248*34255Sdonn { 249*34255Sdonn int movato; 250*34255Sdonn 251*34255Sdonn splitrand(p); 252*34255Sdonn if ((movato = isreg(regs[RT2])) >= 0 253*34255Sdonn && movato < NUSE) 254*34255Sdonn /* 255*34255Sdonn * this register is dead; 256*34255Sdonn * resurrect it temporarily 257*34255Sdonn */ 258*34255Sdonn uses[movato] = p; 259*34255Sdonn } 2601496Sbill } 2611496Sbill else 2621496Sbill { 2631496Sbill p->op = MOV; splitrand(p); 2641496Sbill strcpy(regs[RT1], regs[RT2]); 2651496Sbill strcpy(regs[RT2], regs[RT3]); 2661496Sbill regs[RT3][0] = '\0'; 2671496Sbill p->pop = 0; newcode(p); 2681496Sbill } 2691496Sbill switch (shcnt) 2701496Sbill { 2711496Sbill case 1: pf->subop = WORD; break; 2721496Sbill case 2: pf->subop = LONG; break; 2731496Sbill case 3: pf->subop = QUAD; break; 2741496Sbill } 275*34255Sdonn pf->pop = 0; 2761496Sbill redunm++; nsaddr++; nchange++; 27725756Sdonn goto std; 2781496Sbill } 2791496Sbill ashadd: 2801496Sbill /* at this point, RT2 and RT3 are guaranteed to be simple regs*/ 2811496Sbill if (shcnt == 1 && equstr(regs[RT2], regs[RT3])) 2821496Sbill { 2831496Sbill /* 2841496Sbill ** quickie: 2851496Sbill ** ashl $1,A,A > addl2 A,A 2861496Sbill */ 2871496Sbill p->op = ADD; p->subop = U(LONG,OP2); p->pop = 0; 2881496Sbill strcpy(regs[RT1], regs[RT2]); regs[RT3][0] = '\0'; 2891496Sbill newcode(p); nchange++; 2901496Sbill } 2911496Sbill goto std; 2921496Sbill } 2931496Sbill 2941496Sbill case EXTV: 2951496Sbill case EXTZV: 2961496Sbill { 2971496Sbill /* bit tests: 2981496Sbill ** extv A,$1,B,rC \ 2991496Sbill ** tstl rC > jbc A,B,D 3001496Sbill ** jeql D / 3011496Sbill ** 3021496Sbill ** also byte- and word-size fields: 3031496Sbill ** extv $n*8,$8,A,B > cvtbl n+A,B 30416133Sralph ** extv $n*16,$16,A,B > cvtwl 2n+A,B 3051496Sbill ** extzv $n*8,$8,A,B > movzbl n+A,B 30616133Sralph ** extzv $n*16,$16,A,B > movzwl 2n+A,B 3071496Sbill */ 3081496Sbill register struct node *pf; /* forward node */ 3091496Sbill register struct node *pn; /* next node (after pf) */ 3101496Sbill int flen; /* field length */ 3111496Sbill 3121496Sbill splitrand(p); 3131496Sbill if (regs[RT2][0] != '$') goto std; 3141496Sbill if ((flen = getnum(®s[RT2][1])) < 0) goto std; 3151496Sbill if (flen == 1) 3161496Sbill { 3171496Sbill register int extreg; /* reg extracted to */ 3181496Sbill 3191496Sbill extreg = isreg(regs[RT4]); 3201496Sbill if (extreg < 0 || extreg >= NUSE) goto std; 3211496Sbill if ((pf = p->forw)->op != TST) goto std; 3221496Sbill if (uses[extreg] && uses[extreg] != pf) goto std; 3231496Sbill splitrand(pf); 3241496Sbill if (extreg != isreg(regs[RT1])) goto std; 3251496Sbill if ((pn = pf->forw)->op != CBR) goto std; 3261496Sbill if (pn->subop != JEQ && pn->subop != JNE) goto std; 3271496Sbill delnode(p); delnode(pf); 3281496Sbill pn->subop = (pn->subop == JEQ) ? JBC : JBS; 3291496Sbill for(cp2=p->code; *cp2++!=',';); 3301496Sbill for(cp1=cp2; *cp1++!=',';); 3311496Sbill while (*cp1!=',') *cp2++= *cp1++; *cp2='\0'; 3321496Sbill pn->code = p->code; pn->pop = NULL; 3331496Sbill uses[extreg] = NULL; 334*34255Sdonn p = pn; 335*34255Sdonn break; 3361496Sbill } 3371496Sbill else 3381496Sbill if (flen == 8 || flen == 16) 3391496Sbill { 3401496Sbill register int boff; /* bit offset */ 3411496Sbill register int coff; /* chunk (byte or word) offset*/ 3421496Sbill 3431496Sbill if (regs[RT1][0] != '$') goto std; 3441496Sbill if ((boff = getnum(®s[RT1][1])) < 0) goto std; 3451496Sbill coff = boff / flen; 3461496Sbill if (coff && (isreg(regs[RT3]) >= 0)) goto std; 3471496Sbill if (boff < 0 || (boff % flen) != 0) goto std; 3481496Sbill p->op = (p->op == EXTV) ? CVT : MOVZ; 3491496Sbill p->subop = U((flen == 8 ? BYTE : WORD), LONG); 3501496Sbill if (coff == 0) 3511496Sbill strcpy(regs[RT1], regs[RT3]); 3521496Sbill else 35316133Sralph sprintf(regs[RT1], "%d%s%s", 35416133Sralph (flen == 8 ? coff : 2*coff), 35516133Sralph (regs[RT3][0] == '(' ? "" : "+"), 3561496Sbill regs[RT3]); 3571496Sbill strcpy(regs[RT2], regs[RT4]); 3581496Sbill regs[RT3][0] = '\0'; regs[RT4][0] = '\0'; 3591496Sbill p->pop = 0; newcode(p); 3601496Sbill } 3611496Sbill nchange++; 3621496Sbill goto std; 3631496Sbill } 3641496Sbill 3651496Sbill case CMP: 3661496Sbill { 3671496Sbill /* comparison to -63 to -1: 3681496Sbill ** cmpl r0,$-1 > incl r0 3691496Sbill ** jeql ... 3701496Sbill ** 3711496Sbill ** cmpl r0,$-63 > addl2 $63,r0 3721496Sbill ** jeql ... 3731496Sbill */ 3741496Sbill register int num; 3751496Sbill register int reg; 3761496Sbill register struct node *regp = p->back; 3771496Sbill 3781496Sbill if (p->forw->op != CBR) goto std; 3791496Sbill if (p->forw->subop != JEQ && p->forw->subop != JNE) goto std; 3801496Sbill splitrand(p); 3811496Sbill if (strncmp(regs[RT2], "$-", 2) != 0) goto std; 3821496Sbill reg = r = isreg(regs[RT1]); 3831496Sbill if (r < 0) goto std; 3841496Sbill if (r < NUSE && uses[r] != 0) goto std; 3851496Sbill if (r >= NUSE && regp->op == MOV && p->subop == regp->subop) 3861496Sbill { 3871496Sbill if (*regp->code != 'r') goto std; 3881496Sbill reg = regp->code[1] - '0'; 3891496Sbill if (isdigit(regp->code[2]) || reg >= NUSE || uses[reg]) 3901496Sbill goto std; 3911496Sbill } 3921496Sbill if (r >= NUSE) goto std; 3931496Sbill if (reg != r) 3941496Sbill sprintf(regs[RT1], "r%d", reg); 3951496Sbill if ((num = getnum(®s[RT2][2])) <= 0 || num > 63) goto std; 3961496Sbill if (num == 1) 3971496Sbill { 3981496Sbill p->op = INC; regs[RT2][0] = '\0'; 3991496Sbill } 4001496Sbill else 4011496Sbill { 4021496Sbill register char *t; 4031496Sbill 4041496Sbill t=regs[RT1];regs[RT1]=regs[RT2];regs[RT2]=t; 4051496Sbill p->op = ADD; p->subop = U(p->subop, OP2); 4061496Sbill for (t = ®s[RT1][2]; t[-1] = *t; t++) ; 4071496Sbill } 4081496Sbill p->pop = 0; newcode(p); 4091496Sbill nchange++; 4101496Sbill goto std; 4111496Sbill } 4121496Sbill 4131496Sbill case JSB: 4141496Sbill if (equstr(p->code,"mcount")) {uses[0]=p; regs[0][0]= -1;} 4151496Sbill goto std; 4161496Sbill case JBR: case JMP: 4171496Sbill clearuse(); 4181496Sbill if (p->subop==RET || p->subop==RSB) {uses[0]=p; regs[0][0]= -1; break;} 4191496Sbill if (p->ref==0) goto std; /* jmp (r0) */ 4201496Sbill /* fall through */ 4211496Sbill case CBR: 4221496Sbill if (p->ref->ref!=0) for (r=NUSE;--r>=0;) 4231496Sbill if (biti[r] & (int)p->ref->ref) {uses[r]=p; regs[r][0]= -1;} 4241496Sbill case EROU: case JSW: 4251496Sbill case TEXT: case DATA: case BSS: case ALIGN: case WGEN: case END: ; 4261496Sbill } 4271496Sbill } 4281496Sbill for (p= &first; p!=0; p=p->forw) 4291496Sbill if (p->op==LABEL || p->op==DLABEL) p->ref=0; /* erase our tracks */ 4301496Sbill } 4311496Sbill 4321496Sbill rmove() 4331496Sbill { 43418381Sralph register struct node *p; 4351496Sbill register int r; 4361496Sbill int r1; 4371496Sbill 4381496Sbill clearreg(); 4391496Sbill for (p=first.forw; p!=0; p = p->forw) { 4401496Sbill if (debug) { 44117210Sralph if (*conloc) { 44217210Sralph r1=conval[0]; 44317210Sralph printf("Con %s = %d%d %s\n", conloc, r1&0xF, r1>>4, conval+1); 44417210Sralph } 4451496Sbill printf("Regs:\n"); 4461496Sbill for (r=0; r<NREG; r++) 4471496Sbill if (regs[r][0]) { 4481496Sbill r1=regs[r][0]; 4491496Sbill printf("%d: %d%d %s\n", r, r1&0xF, r1>>4, regs[r]+1); 4501496Sbill } 4511496Sbill printf("-\n"); 4521496Sbill } 4531496Sbill switch (p->op) { 4541496Sbill 4551496Sbill case CVT: 4561496Sbill splitrand(p); goto mov; 4571496Sbill 4581496Sbill case MOV: 4591496Sbill splitrand(p); 4601496Sbill if ((r = findrand(regs[RT1],p->subop)) >= 0) { 4611496Sbill if (r == isreg(regs[RT2]) && p->forw->op!=CBR) { 4621496Sbill delnode(p); redunm++; break; 4631496Sbill } 4641496Sbill } 4651496Sbill mov: 4661496Sbill repladdr(p); 4671496Sbill r = isreg(regs[RT1]); 4681496Sbill r1 = isreg(regs[RT2]); 4691496Sbill dest(regs[RT2],p->subop); 4701506Sbill if (r>=0) { 4711506Sbill if (r1>=0) savereg(r1, regs[r]+1, p->subop); 4721506Sbill else if (p->op!=CVT) savereg(r, regs[RT2], p->subop); 4731506Sbill } else if (r1>=0) savereg(r1, regs[RT1], p->subop); 4741506Sbill else if (p->op!=CVT) setcon(regs[RT1], regs[RT2], p->subop); 4751496Sbill break; 4761496Sbill 4771496Sbill /* .rx,.wx */ 4781496Sbill case MFPR: 4791496Sbill case COM: 4801496Sbill case NEG: 4811496Sbill /* .rx,.wx or .rx,.rx,.wx */ 4821496Sbill case ADD: 4831496Sbill case SUB: 4841496Sbill case BIC: 4851496Sbill case BIS: 4861496Sbill case XOR: 4871496Sbill case MUL: 4881496Sbill case DIV: 4891496Sbill case ASH: 4901496Sbill case MOVZ: 4911496Sbill /* .rx,.rx,.rx,.wx */ 4921496Sbill case EXTV: 4931496Sbill case EXTZV: 4941496Sbill case INSV: 4951496Sbill splitrand(p); 4961496Sbill repladdr(p); 4971496Sbill dest(lastrand,p->subop); 4981496Sbill if (p->op==INSV) ccloc[0]=0; 4991496Sbill break; 5001496Sbill 5011496Sbill /* .mx or .wx */ 5021496Sbill case CLR: 5031496Sbill case INC: 5041496Sbill case DEC: 5051496Sbill splitrand(p); 5061496Sbill dest(lastrand,p->subop); 5071496Sbill if (p->op==CLR) 5081496Sbill if ((r = isreg(regs[RT1])) >= 0) 5091496Sbill savereg(r, "$0", p->subop); 5101496Sbill else 5111496Sbill setcon("$0", regs[RT1], p->subop); 5121496Sbill break; 5131496Sbill 5141496Sbill /* .rx */ 5151496Sbill case TST: 5161496Sbill case PUSH: 5171496Sbill splitrand(p); 5181496Sbill lastrand=regs[RT1+1]; /* fool repladdr into doing 1 operand */ 5191496Sbill repladdr(p); 5201496Sbill if (p->op==TST && equstr(lastrand=regs[RT1], ccloc+1) 5211496Sbill && ((0xf&(ccloc[0]>>4))==p->subop || equtype(ccloc[0],p->subop)) 5221496Sbill &&!source(lastrand)) { 5231496Sbill delnode(p); p = p->back; nrtst++; nchange++; 5241496Sbill } 5251496Sbill setcc(lastrand,p->subop); 5261496Sbill break; 5271496Sbill 5281496Sbill /* .rx,.rx,.rx */ 5291496Sbill case PROBER: 5301496Sbill case PROBEW: 5311496Sbill case CASE: 5321496Sbill case MOVC3: 5331496Sbill /* .rx,.rx */ 5341496Sbill case MTPR: 5351496Sbill case CALLS: 5361496Sbill case CMP: 5371496Sbill case BIT: 5381496Sbill splitrand(p); 5391496Sbill /* fool repladdr into doing right number of operands */ 5403957Sbugs if (p->op==CASE || p->op==PROBER || p->op==PROBEW) lastrand=regs[RT4]; 5413958Sbugs /* else if (p->op==CMPV || p->op==CMPZV) lastrand=regs[RT4+1]; */ 5423957Sbugs else if (p->op==MOVC3) lastrand=regs[RT1]; 5431496Sbill else lastrand=regs[RT3]; 5441496Sbill repladdr(p); 5451496Sbill if (p->op==CALLS || p->op==MOVC3) clearreg(); 5461496Sbill if (p->op==BIT) bitopt(p); 5471496Sbill ccloc[0]=0; break; 5481496Sbill 5491496Sbill case CBR: 5501496Sbill if (p->subop>=JBC) { 5511496Sbill splitrand(p); 5521496Sbill if (p->subop<JBCC) lastrand=regs[RT3]; /* 2 operands can be optimized */ 5531496Sbill else lastrand=regs[RT2]; /* .mb destinations lose */ 5541496Sbill repladdr(p); 5551496Sbill } 5561496Sbill ccloc[0] = 0; 5571496Sbill break; 5581496Sbill 5591496Sbill case JBR: 5601496Sbill redunbr(p); 5611496Sbill 5621496Sbill /* .wx,.bb */ 5631496Sbill case SOB: 5641496Sbill 5651496Sbill default: 5661496Sbill clearreg(); 5671496Sbill } 5681496Sbill } 5691496Sbill } 5701496Sbill 5711496Sbill char * 5721496Sbill byondrd(p) register struct node *p; { 5731496Sbill /* return pointer to register which is "beyond last read/modify operand" */ 5741496Sbill if (OP2==(p->subop>>4)) return(regs[RT3]); 5751496Sbill switch (p->op) { 5761496Sbill case MFPR: 5771496Sbill case JSB: 5781496Sbill case PUSHA: 5791496Sbill case TST: case INC: case DEC: case PUSH: return(regs[RT2]); 5801496Sbill case MTPR: 5811496Sbill case BIT: case CMP: case CALLS: return(regs[RT3]); 5821496Sbill case PROBER: case PROBEW: 5831496Sbill case CASE: case MOVC3: return(regs[RT4]); 5841496Sbill } 5851496Sbill return(lastrand); 5861496Sbill } 5871496Sbill 5881496Sbill struct node * 5891496Sbill bflow(p) 5901496Sbill register struct node *p; 5911496Sbill { 5921496Sbill register char *cp1,*cp2,**preg; register int r; 5931496Sbill int flow= -1; 5941496Sbill struct node *olduse=0; 5951496Sbill splitrand(p); 5961496Sbill if (p->op!=PUSH && p->subop && 0<=(r=isreg(lastrand)) && r<NUSE && uses[r]==p->forw) { 59732986Sdonn if ((p->op==CVT || p->op==MOVZ) 59832986Sdonn && (p->forw->op==CVT || p->forw->op==MOVZ) 59932986Sdonn && p->forw->subop&0xf 60032987Sdonn && equtype(p->subop,p->forw->subop) 60132986Sdonn && !source(cp1=regs[RT1]) 60232986Sdonn && !indexa(cp1)) goto movit; 6031496Sbill if (equtype(p->subop,regs[r][0]) 6041496Sbill || ((p->op==CVT || p->op==MOVZ) 6051496Sbill && 0xf®s[r][0] && compat(0xf&(p->subop>>4),regs[r][0]))) { 6061496Sbill register int r2; 6071496Sbill if (regs[r][1]!=0) {/* send directly to destination */ 6081496Sbill if (p->op==INC || p->op==DEC) { 6091496Sbill if (p->op==DEC) p->op=SUB; else p->op=ADD; 6101496Sbill p->subop=(OP2<<4)+(p->subop&0xF); /* use 2 now, convert to 3 later */ 6111496Sbill p->pop=0; 6121496Sbill cp1=lastrand; cp2=regs[RT2]; while (*cp2++= *cp1++); /* copy reg */ 6131496Sbill cp1=lastrand; *cp1++='$'; *cp1++='1'; *cp1=0; 6141496Sbill } 6151496Sbill cp1=regs[r]+1; cp2=lastrand; 6161496Sbill if (OP2==(p->subop>>4)) {/* use 3 operand form of instruction */ 6171496Sbill p->pop=0; 6181496Sbill p->subop += (OP3-OP2)<<4; lastrand=cp2=regs[RT3]; 6191496Sbill } 6201496Sbill while (*cp2++= *cp1++); 6211496Sbill if (p->op==MOVA && p->forw->op==PUSH) { 6221496Sbill p->op=PUSHA; *regs[RT2]=0; p->pop=0; 6231496Sbill } else if (p->op==MOV && p->forw->op==PUSH) { 6241496Sbill p->op=PUSH ; *regs[RT2]=0; p->pop=0; 6251496Sbill } 6261496Sbill delnode(p->forw); 6271496Sbill if (0<=(r2=isreg(lastrand)) && r2<NUSE) { 6281496Sbill uses[r2]=uses[r]; uses[r]=0; 6291496Sbill } 63018381Sralph (void) redun3(p,0); 6311496Sbill newcode(p); redunm++; flow=r; 6321496Sbill } else if (p->op==MOV && p->forw->op!=EXTV && p->forw->op!=EXTZV) { 6331496Sbill /* superfluous fetch */ 6341496Sbill int nmatch; 6352829Stoy char src[C2_ASIZE]; 6361496Sbill movit: 6371496Sbill cp2=src; cp1=regs[RT1]; while (*cp2++= *cp1++); 6381496Sbill splitrand(p->forw); 6391496Sbill if (p->forw->op != INC && p->forw->op != DEC) 6401496Sbill lastrand=byondrd(p->forw); 6411496Sbill nmatch=0; 6421496Sbill for (preg=regs+RT1;*preg!=lastrand;preg++) 6431496Sbill if (r==isreg(*preg)) { 6441496Sbill cp2= *preg; cp1=src; while (*cp2++= *cp1++); ++nmatch; 6451496Sbill } 6461496Sbill if (nmatch==1) { 6471496Sbill if (OP2==(p->forw->subop>>4) && equstr(src,regs[RT2])) { 6481496Sbill p->forw->pop=0; 6491496Sbill p->forw->subop += (OP3-OP2)<<4; cp1=regs[RT3]; 6501496Sbill *cp1++='r'; *cp1++=r+'0'; *cp1=0; 6511496Sbill } 6521496Sbill delnode(p); p=p->forw; 6531496Sbill if (0<=(r2=isreg(src)) && r2<NUSE) { 6541496Sbill uses[r2]=uses[r]; uses[r]=0; 6551496Sbill } 65618381Sralph (void) redun3(p,0); 65733360Sdonn newcode(p); redunm++; 65833360Sdonn return(p); /* avoid stale uses[] data */ 6591496Sbill } else splitrand(p); 6601496Sbill } 6611496Sbill } else if (p->op==MOV && (p->forw->op==CVT || p->forw->op==MOVZ) 6621496Sbill && p->forw->subop&0xf /* if base or index, then forget it */ 6631496Sbill && compat(p->subop,p->forw->subop) && !source(cp1=regs[RT1]) 6641496Sbill && !indexa(cp1)) goto movit; 6651496Sbill } 6661496Sbill /* adjust 'lastrand' past any 'read' or 'modify' operands. */ 6671496Sbill lastrand=byondrd(p); 6681496Sbill /* a 'write' clobbers the register. */ 6691496Sbill if (0<=(r=isreg(lastrand)) && r<NUSE 6701496Sbill || OP2==(p->subop>>4) && 0<=(r=isreg(regs[RT2])) && r<NUSE && uses[r]==0) { 6711496Sbill /* writing a dead register is useless, but watch side effects */ 6721496Sbill switch (p->op) { 6731498Sbill case ACB: 6741496Sbill case AOBLEQ: case AOBLSS: case SOBGTR: case SOBGEQ: break; 6751498Sbill default: 6761496Sbill if (uses[r]==0) {/* no direct uses, check for use of condition codes */ 6771496Sbill register struct node *q=p; 6781496Sbill while ((q=nonlab(q->forw))->combop==JBR) q=q->ref; /* cc unused, unchanged */ 6791496Sbill if (q->op!=CBR) {/* ... and destroyed */ 6801496Sbill preg=regs+RT1; 6811496Sbill while (cp1= *preg++) { 6821496Sbill if (cp1==lastrand) {redunm++; delnode(p); return(p->forw);} 6831496Sbill if (source(cp1) || equstr(cp1,lastrand)) break; 6841496Sbill } 6851496Sbill } 6861496Sbill } 6871496Sbill flow=r; 6881496Sbill } 6891496Sbill } 69018381Sralph if (0<=(r=flow)) { 69118381Sralph olduse=uses[r]; 69218381Sralph uses[r]=0; 69318381Sralph regs[r][0]=regs[r][1]=0; 69418381Sralph } 6951496Sbill /* these two are here, rather than in bmove(), 6961496Sbill /* because I decided that it was better to go for 3-address code 6971496Sbill /* (save time) rather than fancy jbxx (save 1 byte) 6981496Sbill /* on sequences like bisl2 $64,r0; movl r0,foo 6991496Sbill */ 7001496Sbill if (p->op==BIC) {p=bicopt(p); splitrand(p); lastrand=byondrd(p);} 70118381Sralph if (p->op==BIS) {(void) bixprep(p,JBSS); lastrand=byondrd(p);} 7021496Sbill /* now look for 'read' or 'modify' (read & write) uses */ 7031496Sbill preg=regs+RT1; 7041496Sbill while (*(cp1= *preg++)) { 7051496Sbill /* check for r */ 7061496Sbill if (lastrand!=cp1 && 0<=(r=isreg(cp1)) && r<NUSE && uses[r]==0) { 7071496Sbill uses[r]=p; cp2=regs[r]; *cp2++=p->subop; 7081506Sbill if (p->op==ASH && preg==(regs+RT1+1)) cp2[-1]=BYTE; /* stupid DEC */ 7091496Sbill if (p->op==MOV || p->op==PUSH || p->op==CVT || p->op==MOVZ || p->op==COM || p->op==NEG) { 7101496Sbill if (p->op==PUSH) cp1="-(sp)"; 7111496Sbill else { 7121496Sbill cp1=regs[RT2]; 7131496Sbill if (0<=(r=isreg(cp1)) && r<NUSE && uses[r]==0) 7141496Sbill uses[r]=olduse; /* reincarnation!! */ 7151496Sbill /* as in addl2 r0,r1; movl r1,r0; ret */ 7161496Sbill if (p->op!=MOV) cp1=0; 7171496Sbill } 7181496Sbill if (cp1) while (*cp2++= *cp1++); 7191506Sbill else *cp2=0; 7201506Sbill } else *cp2=0; 7211496Sbill continue; 7221496Sbill } 7231496Sbill /* check for (r),(r)+,-(r),[r] */ 7241496Sbill do if (*cp1=='(' || *cp1=='[') {/* get register number */ 7251496Sbill char t; 7261496Sbill cp2= ++cp1; while (*++cp1!=')' && *cp1!=']'); t= *cp1; *cp1=0; 7271496Sbill if (0<=(r=isreg(cp2)) && r<NUSE && (uses[r]==0 || uses[r]==p)) { 7281496Sbill uses[r]=p; regs[r][0]=(*--cp2=='[' ? OPX<<4 : OPB<<4); 7291496Sbill } 7301496Sbill *cp1=t; 7311496Sbill } while (*++cp1); 7321496Sbill } 7331496Sbill /* pushax or movax possibility? */ 7341496Sbill cp1=regs[RT1]; 7351496Sbill if (*cp1++=='$' && isstatic(cp1) && natural(regs[RT1])) { 7361496Sbill if (p->combop==T(MOV,LONG)) { 7371496Sbill if (regs[RT1][1]=='L' && 0!=(p->labno=getnum(regs[RT1]+2))) { 7381496Sbill cp1=p->code; while (*cp1++!=','); p->code= --cp1; 7391496Sbill } 7401496Sbill p->combop=T(MOVA,LONG); ++p->code; p->pop=0; 7411496Sbill } else if (p->combop==T(PUSH,LONG)) { 7421496Sbill p->combop=T(PUSHA,LONG); ++p->code; p->pop=0; 7431496Sbill } else if ((p->combop&0xFFFF)==T(ADD,U(LONG,OP3)) 7441496Sbill && 0<=(r=isreg(regs[RT2]))) { 7451496Sbill cp1=cp2=p->code; ++cp1; 7461496Sbill do *cp2++= *cp1; while (*cp1++!=','); cp2[-1]='['; 7471496Sbill do *cp2++= *cp1; while (*cp1++!=','); cp2[-1]=']'; 7481496Sbill if (!equstr(regs[RT3],"-(sp)")) p->combop=T(MOVA,BYTE); 7491496Sbill else {p->combop=T(PUSHA,BYTE); *cp2=0;} 75024399Smckusick if (r < NUSE && uses[r] == 0) { 75124399Smckusick uses[r]=p; 75224399Smckusick regs[r][0]=OPX<<4; 75324399Smckusick } 7541496Sbill p->pop=0; 7551496Sbill } 7561496Sbill } 7571496Sbill return(p); 7581496Sbill } 7591496Sbill 7601496Sbill ispow2(n) register long n; {/* -1 -> no; else -> log to base 2 */ 7611496Sbill register int log; 7621496Sbill if (n==0 || n&(n-1)) return(-1); log=0; 7631496Sbill for (;;) {n >>= 1; if (n==0) return(log); ++log; if (n== -1) return(log);} 7641496Sbill } 7651496Sbill 7661496Sbill bitopt(p) register struct node *p; { 7671496Sbill /* change "bitx $<power_of_2>,a" followed by JEQ or JNE 7681496Sbill /* into JBC or JBS. watch out for I/O registers. (?) 7691496Sbill /* assumes that 'splitrand' has already been called. 7701496Sbill */ 7711496Sbill register char *cp1,*cp2; int b; 7721496Sbill cp1=regs[RT1]; cp2=regs[RT2]; 7731496Sbill if (*cp1++!='$' || !okio(cp2) || p->forw->op!=CBR || p->forw->subop&-2 || 77427461Sdonn 0>(b=ispow2(getnum(cp1)))) return; 77527461Sdonn if (p->subop!=BYTE && !(b==0 && p->subop==LONG) && 77627461Sdonn (source(cp2) || indexa(cp2))) return; 7771496Sbill if (b>=bitsize[p->subop]) {/* you dummy! */ 7781496Sbill if (source(cp2)) {/* side effect: auto increment or decrement */ 7791496Sbill p->pop=0; 7801496Sbill p->op=TST; --cp1; while (*cp1++= *cp2++); 7811496Sbill regs[RT2][0]=0; newcode(p); 7821496Sbill } else delnode(p); 7831496Sbill p = p->forw; 7841496Sbill if (p->subop==JEQ) {p->combop=JBR; p->pop=0;} 7851496Sbill else delnode(p); 7861496Sbill nchange++; nbj++; return; 7871496Sbill } 7881496Sbill if (cp1=p->forw->code) {/* destination is not an internal label */ 7891496Sbill cp2=regs[RT3]; while (*cp2++= *cp1++); 7901496Sbill } 79127461Sdonn if (b==0 && (p->subop==LONG || !(source(regs[RT2]) || indexa(regs[RT2])))) { 79227461Sdonn /* JLB optimization, ala BLISS */ 7931496Sbill cp2=regs[RT1]; cp1=regs[RT2]; while (*cp2++= *cp1++); 7941496Sbill cp2=regs[RT2]; cp1=regs[RT3]; while (*cp2++= *cp1++); 7951496Sbill *(regs[RT3])=0; p->forw->subop += JLBC-JBC; 7961496Sbill p->forw->pop=0; 7971496Sbill } else { 7981496Sbill cp1=regs[RT1]+1; 7991496Sbill if (b>9) *cp1++= b/10 +'0'; *cp1++= b%10 +'0'; *cp1=0; /* $<bit_number> */ 8001496Sbill } 8011496Sbill nbj++; newcode(p); p->combop = p->forw->combop+((JBC-JEQ)<<8); 8021496Sbill p->labno = p->forw->labno; delnode(p->forw); 8031496Sbill p->pop=0; 8041496Sbill } 8051496Sbill 8061496Sbill isfield(n) register long n; {/* -1 -> no; else -> position of low bit */ 80718381Sralph register int p; register long t; 8081496Sbill t= ((n-1)|n) +1; 80916832Sralph if (n!=0 && (0==t || 0<=ispow2(t))) { 81018381Sralph p=0; while(!(n&1)) {n >>= 1; ++p;} return(p); 8111496Sbill } else return(-1); 8121496Sbill } 8131496Sbill 8141496Sbill bixprep(p,bix) register struct node *p; { 8151496Sbill /* initial setup, single-bit checking for bisopt, bicopt. 8161496Sbill /* return: 0->don't bother any more; 1->worthwhile trying 8171496Sbill */ 8181496Sbill register char *cp1,*cp2; 8191496Sbill splitrand(p); cp1=regs[RT1]; cp2=regs[RT2]; 8201496Sbill if (*cp1++!='$' || 0>(pos=isfield(f=getnum(cp1))) 8211496Sbill || !okio(cp2) || indexa(cp2) || source(cp2) || !okio(lastrand)) return(0); 8221496Sbill f |= f-1; if (++f==0) siz=32-pos; else siz=ispow2(f)-pos; 8231496Sbill if (siz==1 && pos>5 && (p->subop>>4)==OP2 && (p->subop&0xF)!=BYTE 8241496Sbill && pos<bitsize[p->subop&0xF]) { 8251496Sbill p->ref = insertl(p->forw); p->combop = CBR | (bix<<8); 8261496Sbill p->pop=0; 8271496Sbill p->labno = p->ref->labno; 8281496Sbill if (pos>9) {*cp1++= pos/10 +'0'; pos %= 10;} 8291496Sbill *cp1++=pos+'0'; *cp1=0; newcode(p); nbj++; return(0); 8301496Sbill } 8311496Sbill return(1); 8321496Sbill } 8331496Sbill 8341496Sbill 8351496Sbill struct node * 8361496Sbill bicopt(p) register struct node *p; { 8371496Sbill /* use field operations or MOVZ if possible. done as part of 'bflow'. 8381496Sbill */ 8391496Sbill register char *cp1,*cp2; int r; 8403933Sroot char src[C2_ASIZE]; 84118381Sralph char lhssiz, sop; 8421496Sbill if (!bixprep(p,JBCC)) return(p); 8431496Sbill if (f==0) {/* the BIC isolates low order bits */ 8441496Sbill siz=pos; pos=0; 8451496Sbill if ((p->subop&0xF)==LONG && *(regs[RT2])!='$') {/* result of EXTZV is long */ 8461496Sbill /* save source of BICL in 'src' */ 8471496Sbill cp1=regs[RT2]; cp2=src; while (*cp2++= *cp1++); 8481496Sbill if (p->back->op==ASH) {/* try for more */ 8491496Sbill splitrand(p->back); cp1=regs[RT1]; cp2=regs[RT3]; 8501496Sbill if (*cp1++=='$' && *(regs[RT2])!='$' && !indexa(regs[RT2]) 8511496Sbill && 0>(f=getnum(cp1)) && equstr(src,cp2) 85216832Sralph && 0<=(r=isreg(cp2)) && r<NUSE 85316832Sralph && siz-f <= 32) { /* a good ASH */ 8541496Sbill pos -= f; cp1=regs[RT2]; cp2=src; while (*cp2++= *cp1++); 8551496Sbill delnode(p->back); 8561496Sbill } 8571496Sbill } 85816133Sralph /* 85916133Sralph * 'pos', 'siz' known; find out the size of the 86016133Sralph * left-hand operand of what the bicl will turn into. 86116133Sralph */ 86216133Sralph if (pos==0 && siz==16) 86316133Sralph lhssiz = WORD; /* movzwl */ 86416133Sralph else 86516133Sralph lhssiz = BYTE; /* movzbl or extzvl */ 8661496Sbill if (p->back->op==CVT || p->back->op==MOVZ) {/* greedy, aren't we? */ 8671496Sbill splitrand(p->back); cp1=regs[RT1]; cp2=regs[RT2]; 86816133Sralph /* 86916133Sralph * If indexa(cp1) || autoid(cp1), the fold may 87016133Sralph * still be OK if the CVT/MOVZ has the same 87116133Sralph * size operand on its left size as what we 87216133Sralph * will turn the bicl into. 87316133Sralph * However, if the CVT is from a float or 87416133Sralph * double, forget it! 87516133Sralph */ 87618381Sralph sop = p->back->subop&0xF; /* type of LHS of CVT/MOVZ */ 87716133Sralph if (equstr(src,cp2) && okio(cp1) 87818381Sralph && sop != FFLOAT && sop != DFLOAT 87918381Sralph && sop != GFLOAT && sop != HFLOAT 88018381Sralph && ((!indexa(cp1) && !autoid(cp1)) || lhssiz == sop) 8811496Sbill && 0<=(r=isreg(cp2)) && r<NUSE 88218381Sralph && bitsize[sop]>=(pos+siz) 8831496Sbill && bitsize[p->back->subop>>4]>=(pos+siz)) {/* good CVT */ 8841496Sbill cp1=regs[RT1]; cp2=src; while (*cp2++= *cp1++); 8851496Sbill delnode(p->back); 8861496Sbill } 8871496Sbill } 8881496Sbill /* 'pos', 'siz' known; source of field is in 'src' */ 8891496Sbill splitrand(p); /* retrieve destination of BICL */ 89016133Sralph if ((siz==8 || siz==16) && pos==0) { 89116133Sralph p->combop = T(MOVZ,U(lhssiz,LONG)); 8921496Sbill sprintf(line,"%s,%s",src,lastrand); 8931496Sbill } else { 8941496Sbill p->combop = T(EXTZV,LONG); 8951496Sbill sprintf(line,"$%d,$%d,%s,%s",pos,siz,src,lastrand); 8961496Sbill } 8971496Sbill p->pop=0; 8981496Sbill p->code = copy(line); nfield++; return(p); 8991496Sbill }/* end EXTZV possibility */ 9001496Sbill }/* end low order bits */ 9011496Sbill /* unfortunately, INSV clears the condition codes, thus cannot be used */ 9021496Sbill /* else {/* see if BICL2 of positive field should be INSV $0 */ 9031496Sbill /* if (p->subop==(LONG | (OP2<<4)) && 6<=(pos+siz)) { 9041496Sbill /* p->combop = INSV; 9051496Sbill /* sprintf(line,"$0,$%d,$%d,%s",pos,siz,lastrand); 9061496Sbill /* p->code = copy(line); nfield++; return(p); 9071496Sbill /* } 9081496Sbill /* } 9091496Sbill */ 9101496Sbill return(p); 9111496Sbill } 9121496Sbill 9131496Sbill jumpsw() 9141496Sbill { 9151496Sbill register struct node *p, *p1; 91618381Sralph register struct node *tp; 91718381Sralph long tl; 91818381Sralph char *tcp; 91918381Sralph int ti; 9201496Sbill int nj; 9211496Sbill 92218381Sralph ti = 0; 9231496Sbill nj = 0; 9241496Sbill for (p=first.forw; p!=0; p = p->forw) 92518381Sralph p->seq = ++ti; 9261496Sbill for (p=first.forw; p!=0; p = p1) { 9271496Sbill p1 = p->forw; 9281496Sbill if (p->op == CBR && p1->op==JBR && p->ref && p1->ref 9291496Sbill && abs(p->seq - p->ref->seq) > abs(p1->seq - p1->ref->seq)) { 9301496Sbill if (p->ref==p1->ref) 9311496Sbill continue; 9321496Sbill p->subop = revbr[p->subop]; 9331496Sbill p->pop=0; 93418381Sralph tp = p1->ref; 9351496Sbill p1->ref = p->ref; 93618381Sralph p->ref = tp; 93718381Sralph tl = p1->labno; 9381496Sbill p1->labno = p->labno; 93918381Sralph p->labno = tl; 9401496Sbill #ifdef COPYCODE 9411496Sbill if (p->labno == 0) { 94218381Sralph tcp = p1->code; 94318381Sralph p1->code = p->code; 94418381Sralph p->code = tcp; 9451496Sbill } 9461496Sbill #endif 9471496Sbill nrevbr++; 9481496Sbill nj++; 9491496Sbill } 9501496Sbill } 9511496Sbill return(nj); 9521496Sbill } 9531496Sbill 9541496Sbill addsob() 9551496Sbill { 9561496Sbill register struct node *p, *p1, *p2, *p3; 9571496Sbill 9581496Sbill for (p = &first; (p1 = p->forw)!=0; p = p1) { 9591496Sbill if (p->combop==T(DEC,LONG) && p1->op==CBR) { 9603933Sroot if (abs(p->seq - p1->ref->seq) > 8) continue; 9611496Sbill if (p1->subop==JGE || p1->subop==JGT) { 9621496Sbill if (p1->subop==JGE) p->combop=SOBGEQ; else p->combop=SOBGTR; 9631496Sbill p->pop=0; 9641496Sbill p->labno = p1->labno; delnode(p1); nsob++; 9651496Sbill } 9661496Sbill } else if (p->combop==T(INC,LONG)) { 9671496Sbill if (p1->op==LABEL && p1->refc==1 && p1->forw->combop==T(CMP,LONG) 9681496Sbill && (p2=p1->forw->forw)->combop==T(CBR,JLE) 9691496Sbill && (p3=p2->ref->back)->combop==JBR && p3->ref==p1 9701496Sbill && p3->forw->op==LABEL && p3->forw==p2->ref) { 9711496Sbill /* change INC LAB: CMP to LAB: INC CMP */ 9721496Sbill p->back->forw=p1; p1->back=p->back; 9731496Sbill p->forw=p1->forw; p1->forw->back=p; 9741496Sbill p->back=p1; p1->forw=p; 9751496Sbill p1=p->forw; 9761496Sbill /* adjust beginning value by 1 */ 9771496Sbill p2=alloc(sizeof first); p2->combop=T(DEC,LONG); 9781496Sbill p2->pop=0; 9791496Sbill p2->forw=p3; p2->back=p3->back; p3->back->forw=p2; 9801496Sbill p3->back=p2; p2->code=p->code; p2->labno=0; 9811496Sbill } 9821496Sbill if (p1->combop==T(CMP,LONG) && (p2=p1->forw)->op==CBR) { 9831496Sbill register char *cp1,*cp2; 9841496Sbill splitrand(p1); if (!equstr(p->code,regs[RT1])) continue; 9853933Sroot if (abs(p->seq - p2->ref->seq)>8) {/* outside byte displ range */ 9861496Sbill if (p2->subop!=JLE) continue; 9871496Sbill p->combop=T(ACB,LONG); 9881496Sbill cp2=regs[RT1]; cp1=regs[RT2]; while (*cp2++= *cp1++); /* limit */ 9891496Sbill cp2=regs[RT2]; cp1="$1"; while (*cp2++= *cp1++); /* increment */ 9901496Sbill cp2=regs[RT3]; cp1=p->code; while (*cp2++= *cp1++); /* index */ 9911496Sbill p->pop=0; newcode(p); 9921496Sbill p->labno = p2->labno; delnode(p2); delnode(p1); nsob++; 9931496Sbill } else if (p2->subop==JLE || p2->subop==JLT) { 9941496Sbill if (p2->subop==JLE) p->combop=AOBLEQ; else p->combop=AOBLSS; 9951496Sbill cp2=regs[RT1]; cp1=regs[RT2]; while (*cp2++= *cp1++); /* limit */ 9961496Sbill cp2=regs[RT2]; cp1=p->code; while (*cp2++= *cp1++); /* index */ 9971496Sbill p->pop=0; newcode(p); 9981496Sbill p->labno = p2->labno; delnode(p2); delnode(p1); nsob++; 9991496Sbill } 10001496Sbill } 10011496Sbill } 10021496Sbill } 10031496Sbill } 10041496Sbill 10051496Sbill equop(p1, p2) 10061496Sbill register struct node *p1; 10071496Sbill struct node *p2; 10081496Sbill { 10091496Sbill register char *cp1, *cp2; 10101496Sbill 10111496Sbill if (p1->combop != p2->combop) 10121496Sbill return(0); 101332985Sdonn if (p1->combop == 0 && p1->pop != p2->pop) 101432985Sdonn return(0); 10151496Sbill if (p1->op>0 && p1->op<MOV) 10161496Sbill return(0); 101718381Sralph switch (p1->combop) { 101818381Sralph case EROU: case JSW: case TEXT: case DATA: 101918381Sralph case BSS: case ALIGN: case WGEN: case END: 102018381Sralph /* 102118381Sralph * Consider all pseudo-ops to be unique. 102218381Sralph */ 102318381Sralph return(0); 102418381Sralph } 10251496Sbill if (p1->op==MOVA && p1->labno!=p2->labno) return(0); 10261496Sbill cp1 = p1->code; 10271496Sbill cp2 = p2->code; 10281496Sbill if (cp1==0 && cp2==0) 10291496Sbill return(1); 10301496Sbill if (cp1==0 || cp2==0) 10311496Sbill return(0); 10321496Sbill while (*cp1 == *cp2++) 10331496Sbill if (*cp1++ == 0) 10341496Sbill return(1); 10351496Sbill return(0); 10361496Sbill } 10371496Sbill 103818381Sralph #ifndef delnode 10391496Sbill delnode(p) register struct node *p; { 10401496Sbill p->back->forw = p->forw; 10411496Sbill p->forw->back = p->back; 10421496Sbill } 104318381Sralph #endif 10441496Sbill 104518381Sralph #ifndef decref 10461496Sbill decref(p) 10471496Sbill register struct node *p; 10481496Sbill { 10491496Sbill if (p && --p->refc <= 0) { 10501496Sbill nrlab++; 10511496Sbill delnode(p); 10521496Sbill } 10531496Sbill } 105418381Sralph #endif 10551496Sbill 10561496Sbill struct node * 10571496Sbill nonlab(ap) 10581496Sbill struct node *ap; 10591496Sbill { 10601496Sbill register struct node *p; 10611496Sbill 10621496Sbill p = ap; 10631496Sbill while (p && p->op==LABEL) 10641496Sbill p = p->forw; 10651496Sbill return(p); 10661496Sbill } 10671496Sbill 10681496Sbill clearuse() { 10691496Sbill register struct node **i; 10701496Sbill for (i=uses+NUSE; i>uses;) *--i=0; 10711496Sbill } 10721496Sbill 10731496Sbill clearreg() { 107418381Sralph register char **i; 107518381Sralph for (i=regs; i<regs+NREG; ++i) { 107618381Sralph **i = 0; 107718381Sralph *(*i+1) = 0; 107818381Sralph } 107918381Sralph conloc[0] = 0; 108018381Sralph ccloc[0] = 0; 10811496Sbill } 10821496Sbill 10831496Sbill savereg(ai, s, type) 10841496Sbill register char *s; 10851496Sbill { 10861496Sbill register char *p, *sp; 10871496Sbill 10881496Sbill sp = p = regs[ai]; 10891496Sbill if (source(s)) /* side effects in addressing */ 10901496Sbill return; 10911496Sbill /* if any indexing, must be parameter or local */ 10921496Sbill /* indirection (as in "*-4(fp)") is ok, however */ 10931496Sbill *p++ = type; 10941496Sbill while (*p++ = *s) 10951496Sbill if (*s=='[' || *s++=='(' && *s!='a' && *s!='f') {*sp = 0; return;} 10961496Sbill } 10971496Sbill 10981496Sbill dest(s,type) 10991496Sbill register char *s; 11001496Sbill { 11011496Sbill register int i; 11021496Sbill 110318381Sralph (void) source(s); /* handle addressing side effects */ 110417210Sralph if (!natural(s)) { 110517210Sralph /* wild store, everything except constants vanishes */ 110617210Sralph for (i=NREG; --i>=0;) 110718381Sralph if (regs[i][1] != '$') 110818381Sralph regs[i][0] = regs[i][1] = 0; 110917210Sralph conloc[0] = 0; ccloc[0] = 0; 111017210Sralph return; 111117210Sralph } 11121496Sbill if ((i = isreg(s)) >= 0) { 111318381Sralph /* if register destination, that reg is a goner */ 111418381Sralph regs[i][0] = regs[i][1] = 0; 11155794Sroot switch(type & 0xF){ 11165794Sroot case DFLOAT: /* clobber two at once */ 11175794Sroot /*FALLTHROUGH*/ 11185794Sroot case GFLOAT: 111918381Sralph regs[i+1][0] = regs[i+1][1] = 0; 11205794Sroot break; 11215794Sroot case HFLOAT: /* clobber four at once */ 112218381Sralph regs[i+1][0] = regs[i+1][1] = 0; 112318381Sralph regs[i+2][0] = regs[i+2][1] = 0; 112418381Sralph regs[i+3][0] = regs[i+3][1] = 0; 11255794Sroot break; 11265794Sroot } 11275794Sroot switch((type>>4)&0xF){ 11285794Sroot case DFLOAT: /* clobber two at once */ 11295794Sroot /*FALLTHROUGH*/ 11305794Sroot case GFLOAT: 113118381Sralph regs[i+1][0] = regs[i+1][1] = 0; 11325794Sroot break; 11335794Sroot case HFLOAT: /* clobber four at once */ 113418381Sralph regs[i+1][0] = regs[i+1][1] = 0; 113518381Sralph regs[i+2][0] = regs[i+2][1] = 0; 113618381Sralph regs[i+3][0] = regs[i+3][1] = 0; 11375794Sroot break; 11385794Sroot } 11391496Sbill } 11401496Sbill for (i=NREG; --i>=0;) 11411496Sbill if (regs[i][1]=='*' && equstr(s, regs[i]+2)) 114218381Sralph regs[i][0] = regs[i][1] = 0; /* previous indirection through destination is invalid */ 11431496Sbill while ((i = findrand(s,0)) >= 0) /* previous values of destination are invalid */ 114418381Sralph regs[i][0] = regs[i][1] = 0; 114517210Sralph if (*conloc && equstr(conloc, s)) 114617210Sralph conloc[0] = 0; 114717210Sralph setcc(s, type); /* natural destinations set condition codes */ 11481496Sbill } 11491496Sbill 115017210Sralph /* separate operands at commas, set up 'regs' and 'lastrand' */ 11511496Sbill splitrand(p) struct node *p; { 115217210Sralph register char *p1, *p2; 115317210Sralph register char **preg; 115417210Sralph 115517210Sralph preg = regs+RT1; 115617210Sralph if (p1 = p->code) 115717210Sralph while (*p1) { 115817210Sralph lastrand = p2 = *preg++; 115917210Sralph while (*p1) 116017210Sralph if (',' == (*p2++ = *p1++)) { 116117210Sralph --p2; 116217210Sralph break; 116317210Sralph } 116417210Sralph *p2 = 0; 116517210Sralph } 116617210Sralph while (preg < (regs+RT1+5)) 116717210Sralph *(*preg++) = 0; 11681496Sbill } 11691496Sbill 11701496Sbill compat(have, want) { 11711496Sbill register int hsrc, hdst; 11721496Sbill if (0==(want &= 0xF)) return(1); /* anything satisfies a wildcard want */ 117326407Sdonn hsrc=have&0xF; 117426407Sdonn if (0==(hdst=((have>>4)&0xF)) || (hdst>=OP2 && hdst<=OPX)) hdst=hsrc; 11755794Sroot if (want>=FFLOAT) return(hdst==want && hsrc==want); 11765794Sroot /* FLOAT, DFLOAT not compat: rounding */ 11775794Sroot return(hsrc>=want && hdst>=want && hdst<FFLOAT); 11781496Sbill } 11791496Sbill 11801496Sbill equtype(t1,t2) {return(compat(t1,t2) && compat(t2,t1));} 11811496Sbill 11821496Sbill findrand(as, type) 11831496Sbill char *as; 11841496Sbill { 11851496Sbill register char **i; 11861496Sbill for (i = regs+NREG; --i>=regs;) { 11871496Sbill if (**i && equstr(*i+1, as) && compat(**i,type)) 11881496Sbill return(i-regs); 11891496Sbill } 11901496Sbill return(-1); 11911496Sbill } 11921496Sbill 11931496Sbill isreg(s) 11941496Sbill register char *s; 11951496Sbill { 11961496Sbill if (*s++!='r' || !isdigit(*s++)) return(-1); 11971496Sbill if (*s==0) return(*--s-'0'); 11981496Sbill if (*(s-1)=='1' && isdigit(*s++) && *s==0) return(10+*--s-'0'); 11991496Sbill return(-1); 12001496Sbill } 12011496Sbill 12021496Sbill check() 12031496Sbill { 12041496Sbill register struct node *p, *lp; 12051496Sbill 12061496Sbill lp = &first; 12071496Sbill for (p=first.forw; p!=0; p = p->forw) { 120818381Sralph if (p->back != lp) { 120918381Sralph fprintf(stderr, "c2: failed internal consistency check -- help!\n"); 121018381Sralph exit(-1); 121118381Sralph } 12121496Sbill lp = p; 12131496Sbill } 12141496Sbill } 12151496Sbill 12161496Sbill source(ap) 12171496Sbill char *ap; 12181496Sbill { 12191496Sbill register char *p1, *p2; 12201496Sbill 12211496Sbill p1 = ap; 12221496Sbill p2 = p1; 12231496Sbill if (*p1==0) 12241496Sbill return(0); 12251496Sbill while (*p2++ && *(p2-1)!='['); 12261496Sbill if (*p1=='-' && *(p1+1)=='(' 12271496Sbill || *p1=='*' && *(p1+1)=='-' && *(p1+2)=='(' 12281496Sbill || *(p2-2)=='+') { 12291496Sbill while (*p1 && *p1++!='r'); 12301496Sbill if (isdigit(*p1++)) 123118381Sralph if (isdigit(*p1)) 123218381Sralph regs[10+*p1-'0'][0] = regs[10+*p1-'0'][1] = 0; 123318381Sralph else { 123418381Sralph --p1; 123518381Sralph regs[*p1-'0'][0] = regs[*p1-'0'][1] = 0; 123618381Sralph } 12371496Sbill return(1); 12381496Sbill } 12391496Sbill return(0); 12401496Sbill } 12411496Sbill 12421496Sbill newcode(p) struct node *p; { 12431496Sbill register char *p1,*p2,**preg; 12441496Sbill preg=regs+RT1; p2=line; 12451496Sbill while (*(p1= *preg++)) {while (*p2++= *p1++); *(p2-1)=',';} 12461496Sbill *--p2=0; 12471496Sbill p->code=copy(line); 12481496Sbill } 12491496Sbill 12501496Sbill repladdr(p) 12511496Sbill struct node *p; 12521496Sbill { 12531496Sbill register r; 125418381Sralph register char *p1; 12551496Sbill char **preg; int nrepl; 12561496Sbill 12571496Sbill preg=regs+RT1; nrepl=0; 12581496Sbill while (lastrand!=(p1= *preg++)) 12591496Sbill if (!source(p1) && 0<=(r=findrand(p1,p->subop))) { 12601496Sbill *p1++='r'; if (r>9) {*p1++='1'; r -= 10;} *p1++=r+'0'; *p1=0; 12611496Sbill nrepl++; nsaddr++; 12621496Sbill } 12631496Sbill if (nrepl) newcode(p); 12641496Sbill } 12651496Sbill 12661496Sbill /* movedat() 12671496Sbill /* { 12681496Sbill /* register struct node *p1, *p2; 12691496Sbill /* struct node *p3; 12701496Sbill /* register seg; 12711496Sbill /* struct node data; 12721496Sbill /* struct node *datp; 12731496Sbill /* 12741496Sbill /* if (first.forw == 0) 12751496Sbill /* return; 12761496Sbill /* datp = &data; 12771496Sbill /* for (p1 = first.forw; p1!=0; p1 = p1->forw) { 12781496Sbill /* if (p1->op == DATA) { 12791496Sbill /* p2 = p1->forw; 12801496Sbill /* while (p2 && p2->op!=TEXT) 12811496Sbill /* p2 = p2->forw; 12821496Sbill /* if (p2==0) 12831496Sbill /* break; 12841496Sbill /* p3 = p1->back; 12851496Sbill /* p1->back->forw = p2->forw; 12861496Sbill /* p2->forw->back = p3; 12871496Sbill /* p2->forw = 0; 12881496Sbill /* datp->forw = p1; 12891496Sbill /* p1->back = datp; 12901496Sbill /* p1 = p3; 12911496Sbill /* datp = p2; 12921496Sbill /* } 12931496Sbill /* } 12941496Sbill /* if (data.forw) { 12951496Sbill /* datp->forw = first.forw; 12961496Sbill /* first.forw->back = datp; 12971496Sbill /* data.forw->back = &first; 12981496Sbill /* first.forw = data.forw; 12991496Sbill /* } 13001496Sbill /* seg = -1; 13011496Sbill /* for (p1 = first.forw; p1!=0; p1 = p1->forw) { 13021496Sbill /* if (p1->op==TEXT||p1->op==DATA||p1->op==BSS) { 13031496Sbill /* if (p1->op == seg || p1->forw&&p1->forw->op==seg) { 13041496Sbill /* p1->back->forw = p1->forw; 13051496Sbill /* p1->forw->back = p1->back; 13061496Sbill /* p1 = p1->back; 13071496Sbill /* continue; 13081496Sbill /* } 13091496Sbill /* seg = p1->op; 13101496Sbill /* } 13111496Sbill /* } 13121496Sbill /* } 13131496Sbill */ 13141496Sbill 13151496Sbill redunbr(p) 13161496Sbill register struct node *p; 13171496Sbill { 13181496Sbill register struct node *p1; 13191496Sbill register char *ap1; 13201496Sbill char *ap2; 13211496Sbill 13221496Sbill if ((p1 = p->ref) == 0) 13231496Sbill return; 13241496Sbill p1 = nonlab(p1); 13251496Sbill if (p1->op==TST) { 13261496Sbill splitrand(p1); 13271496Sbill savereg(RT2, "$0", p1->subop); 13281496Sbill } else if (p1->op==CMP) 13291496Sbill splitrand(p1); 13301496Sbill else 13311496Sbill return; 13321496Sbill if (p1->forw->op==CBR) { 13331496Sbill ap1 = findcon(RT1, p1->subop); 13341496Sbill ap2 = findcon(RT2, p1->subop); 13351496Sbill p1 = p1->forw; 13361496Sbill if (compare(p1->subop, ap1, ap2)) { 13371496Sbill nredunj++; 13381496Sbill nchange++; 13391496Sbill decref(p->ref); 13401496Sbill p->ref = p1->ref; 13411496Sbill p->labno = p1->labno; 13421496Sbill #ifdef COPYCODE 13431496Sbill if (p->labno == 0) 13441496Sbill p->code = p1->code; 134517210Sralph #endif 13461496Sbill if (p->ref) 13471496Sbill p->ref->refc++; 13481496Sbill } 13491496Sbill } else if (p1->op==TST && equstr(regs[RT1],ccloc+1) && 13501496Sbill equtype(ccloc[0],p1->subop)) { 1351*34255Sdonn p1=insertl(p1->forw); 1352*34255Sdonn decref(p->ref); 1353*34255Sdonn p->ref=p1; 1354*34255Sdonn p->labno=p1->labno; 13551496Sbill nrtst++; nchange++; 13561496Sbill } 13571496Sbill } 13581496Sbill 13591496Sbill char * 13601496Sbill findcon(i, type) 13611496Sbill { 13621496Sbill register char *p; 13631496Sbill register r; 13641496Sbill 13651496Sbill p = regs[i]; 13661496Sbill if (*p=='$') 13671496Sbill return(p); 13681496Sbill if ((r = isreg(p)) >= 0 && compat(regs[r][0],type)) 13691496Sbill return(regs[r]+1); 13701496Sbill if (equstr(p, conloc)) 13711496Sbill return(conval+1); 13721496Sbill return(p); 13731496Sbill } 13741496Sbill 137518381Sralph compare(opc, acp1, acp2) 13761496Sbill char *acp1, *acp2; 13771496Sbill { 13781496Sbill register char *cp1, *cp2; 13791496Sbill register n1; 13801496Sbill int n2; int sign; 13811496Sbill 13821496Sbill cp1 = acp1; 13831496Sbill cp2 = acp2; 13841496Sbill if (*cp1++ != '$' || *cp2++ != '$') 13851496Sbill return(0); 13861496Sbill n1 = 0; sign=1; if (*cp2=='-') {++cp2; sign= -1;} 13871496Sbill while (isdigit(*cp2)) {n1 *= 10; n1 += (*cp2++ - '0')*sign;} 13881496Sbill n2 = n1; 13891496Sbill n1 = 0; sign=1; if (*cp1=='-') {++cp1; sign= -1;} 13901496Sbill while (isdigit(*cp1)) {n1 *= 10; n1 += (*cp1++ - '0')*sign;} 13911496Sbill if (*cp1=='+') 13921496Sbill cp1++; 13931496Sbill if (*cp2=='+') 13941496Sbill cp2++; 13951496Sbill do { 13961496Sbill if (*cp1++ != *cp2) 13971496Sbill return(0); 13981496Sbill } while (*cp2++); 139918381Sralph switch(opc) { 14001496Sbill 14011496Sbill case JEQ: 140218381Sralph return(n1 == n2); 14031496Sbill case JNE: 140418381Sralph return(n1 != n2); 14051496Sbill case JLE: 140618381Sralph return(n1 <= n2); 14071496Sbill case JGE: 140818381Sralph return(n1 >= n2); 14091496Sbill case JLT: 141018381Sralph return(n1 < n2); 14111496Sbill case JGT: 141218381Sralph return(n1 > n2); 14131496Sbill case JLO: 141418381Sralph return((unsigned) n1 < (unsigned) n2); 14151496Sbill case JHI: 141618381Sralph return((unsigned) n1 > (unsigned) n2); 14171496Sbill case JLOS: 141818381Sralph return((unsigned) n1 <= (unsigned) n2); 14191496Sbill case JHIS: 142018381Sralph return((unsigned) n1 >= (unsigned) n2); 14211496Sbill } 14221496Sbill return(0); 14231496Sbill } 14241496Sbill 14251496Sbill setcon(cv, cl, type) 14261496Sbill register char *cv, *cl; 14271496Sbill { 14281496Sbill register char *p; 14291496Sbill 14301496Sbill if (*cv != '$') 14311496Sbill return; 14321496Sbill if (!natural(cl)) 14331496Sbill return; 14341496Sbill p = conloc; 14351496Sbill while (*p++ = *cl++); 14361496Sbill p = conval; 14371496Sbill *p++ = type; 14381496Sbill while (*p++ = *cv++); 14391496Sbill } 14401496Sbill 14411496Sbill equstr(p1, p2) 14421496Sbill register char *p1, *p2; 14431496Sbill { 14441496Sbill do { 14451496Sbill if (*p1++ != *p2) 14461496Sbill return(0); 14471496Sbill } while (*p2++); 14481496Sbill return(1); 14491496Sbill } 14501496Sbill 14511496Sbill setcc(ap,type) 14521496Sbill char *ap; 14531496Sbill { 14541496Sbill register char *p, *p1; 14551496Sbill 14561496Sbill p = ap; 14571496Sbill if (!natural(p)) { 14581496Sbill ccloc[0] = 0; 14591496Sbill return; 14601496Sbill } 14611496Sbill p1 = ccloc; 14621496Sbill *p1++ = type; 14631496Sbill while (*p1++ = *p++); 14641496Sbill } 14651496Sbill 14661496Sbill okio(p) register char *p; {/* 0->probable I/O space address; 1->not */ 14671496Sbill if (ioflag && (!natural(p) || 0>getnum(p))) return(0); 14681496Sbill return(1); 14691496Sbill } 14701496Sbill 14711496Sbill indexa(p) register char *p; {/* 1-> uses [r] addressing mode; 0->doesn't */ 14721496Sbill while (*p) if (*p++=='[') return(1); 14731496Sbill return(0); 14741496Sbill } 14751496Sbill 14761496Sbill natural(p) 14771496Sbill register char *p; 14781496Sbill {/* 1->simple local, parameter, global, or register; 0->otherwise */ 147917210Sralph if (*p=='*' || *p=='(' || *p=='-'&&p[1]=='(' || *p=='$'&&getnum(p+1)) 14801496Sbill return(0); 14811496Sbill while (*p++); 14821496Sbill p--; 148317210Sralph if (*--p=='+' || *p==']' || *p==')' && p[-2]!='a' && p[-2]!='f') 14841496Sbill return(0); 14851496Sbill return(1); 14861496Sbill } 14871496Sbill 14881496Sbill /* 14891496Sbill ** Tell if an argument is most likely static. 14901496Sbill */ 14911496Sbill 14921496Sbill isstatic(cp) 14931496Sbill register char *cp; 14941496Sbill { 14951496Sbill if (*cp == '_' || *cp == 'L' || (*cp++ == 'v' && *cp == '.')) 14961496Sbill return (1); 14971496Sbill return (0); 14981496Sbill } 149916133Sralph 150016133Sralph autoid(p) register char *p; {/* 1-> uses autoincrement/autodecrement; 0->doesn't */ 150116133Sralph if (*p == '-' && *(p+1) == '(') return(1); 150216133Sralph while (*p) p++; 150316133Sralph if (*--p == '+' && *--p == ')') return(1); 150416133Sralph return(0); 150516133Sralph } 1506