|
|
1.1 ! root 1: static char sccsid[] = "@(#)c21.c 4.7 7/8/81"; ! 2: /* char C21[] = {"@(#)c21.c 1.83 80/10/16 21:18:22 JFR"}; /* sccs ident */ ! 3: ! 4: /* ! 5: * C object code improver-- second part ! 6: */ ! 7: ! 8: #include "c2.h" ! 9: #include <stdio.h> ! 10: #include <ctype.h> ! 11: ! 12: #define NUSE 6 ! 13: int ioflag; ! 14: int biti[NUSE] = {1,2,4,8,16,32}; ! 15: int bitsize[4] = {0,8,16,32}; /* index by type codes */ ! 16: int pos,siz; long f; /* for bit field communication */ ! 17: struct node *uses[NUSE]; /* for backwards flow analysis */ ! 18: char *lastrand; /* last operand of instruction */ ! 19: struct node *bflow(); ! 20: struct node *bicopt(); ! 21: char *findcon(); ! 22: ! 23: redun3(p,split) register struct node *p; int split; { ! 24: /* check for 3 addr instr which should be 2 addr */ ! 25: if (OP3==((p->subop>>4)&0xF)) { ! 26: if (split) splitrand(p); ! 27: if (equstr(regs[RT1],regs[RT3]) ! 28: && (p->op==ADD || p->op==MUL || p->op==BIS || p->op==XOR)) { ! 29: register char *t=regs[RT1]; regs[RT1]=regs[RT2]; regs[RT2]=t; ! 30: } ! 31: if (equstr(regs[RT2],regs[RT3])) { ! 32: p->subop=(p->subop&0xF)|(OP2<<4); p->pop=0; ! 33: lastrand=regs[RT2]; *regs[RT3]=0; return(1); ! 34: } ! 35: } return(0); ! 36: } ! 37: ! 38: bmove() { ! 39: register struct node *p, *lastp; register char *cp1,*cp2; register int r; ! 40: refcount(); ! 41: for (p=lastp= &first; 0!=(p=p->forw); lastp=p); ! 42: clearreg(); clearuse(); ! 43: for (p=lastp; p!= &first; p=p->back) { ! 44: if (debug) { ! 45: printf("Uses:\n"); ! 46: for (r=NUSE;--r>=0;) if (uses[r]) ! 47: printf("%d: %s\n",r,uses[r]->code? uses[r]->code:""); ! 48: printf("-\n"); ! 49: } ! 50: r=(p->subop>>4)&0xF; ! 51: if (OP2==r && (cp1=p->code, *cp1++)=='$' && *cp1++=='0' && *cp1++==',' && ! 52: !source(cp1)) {/* a no-op unless MUL or DIV */ ! 53: if (p->op==MUL) {p->op=MOV; p->subop&=0xF; p->pop=0;} ! 54: else if (p->op==DIV) fprintf(stderr,"c2: zero divide\n"); ! 55: else {delnode(p); redunm++; continue;} ! 56: } ! 57: if (OP3==r && 0!=redun3(p,1)) {newcode(p); redunm++;} ! 58: switch (p->op) { ! 59: case LABEL: case DLABEL: ! 60: for (r=NUSE; --r>=0;) ! 61: if (uses[r]) p->ref=(struct node *) (((int)p->ref)|biti[r]); ! 62: break; ! 63: case CALLS: ! 64: clearuse(); goto std; ! 65: case 0: ! 66: clearuse(); break; ! 67: case SUB: ! 68: if ((p->subop&0xF)!=LONG) goto std; cp1=p->code; ! 69: if (*cp1++!='$') goto std; splitrand(p); ! 70: if (equstr(regs[RT2],"fp") && !indexa(regs[RT1])) {/* address comp. */ ! 71: char buf[C2_ASIZE]; cp2=buf; *cp2++='-'; ! 72: cp1=regs[RT1]+1; while (*cp2++= *cp1++); --cp2; ! 73: cp1="(fp),"; while (*cp2++= *cp1++); --cp2; ! 74: cp1=regs[RT3]; while (*cp2++= *cp1++); ! 75: p->code=copy(buf); p->combop=T(MOVA,LONG); p->pop=0; ! 76: } else if (*cp1++=='-' && 0<=(r=getnum(cp1))) { ! 77: p->op=ADD; p->pop=0; *--cp1='$'; p->code=cp1; ! 78: } goto std; ! 79: case ADD: ! 80: if ((p->subop&0xF)!=LONG) goto std; cp1=p->code; ! 81: if (*cp1++!='$') goto std; splitrand(p); ! 82: if (isstatic(cp1) && (r=isreg(regs[RT2]))>=0 && r<NUSE && uses[r]==p->forw) ! 83: { ! 84: /* address comp: ! 85: ** addl2 $_foo,r0 \ movab _foo[r0],bar ! 86: ** movl r0,bar / ! 87: */ ! 88: register struct node *pnext = p->forw; ! 89: char buf[C2_ASIZE]; ! 90: ! 91: if (pnext->op == MOV && pnext->subop == LONG) ! 92: { ! 93: cp1 = ®s[RT1][1]; cp2 = &buf[0]; ! 94: while (*cp2++ = *cp1++) ; cp2--; ! 95: splitrand(pnext); ! 96: if (r == isreg(regs[RT1])) ! 97: { ! 98: delnode(p); p = pnext; ! 99: p->op = MOVA; p->subop = BYTE; ! 100: p->pop = 0; ! 101: cp1 = regs[RT1]; *cp2++ = '['; ! 102: while (*cp2++ = *cp1++) ; cp2--; ! 103: *cp2++ = ']'; *cp2++ = ','; ! 104: cp1 = regs[RT2]; ! 105: while (*cp2++ = *cp1++) ; ! 106: p->code = copy(buf); ! 107: } ! 108: } ! 109: } ! 110: else ! 111: if (equstr(regs[RT2],"fp") && !indexa(regs[RT1])) {/* address comp. */ ! 112: cp2=cp1-1; cp1=regs[RT1]+1; while (*cp2++= *cp1++); --cp2; ! 113: cp1="(fp)"; while (*cp2++= *cp1++); *--cp2=','; ! 114: p->combop=T(MOVA,LONG); p->pop=0; ! 115: } else if (*cp1++=='-' && 0<=(r=getnum(cp1))) { ! 116: p->op=SUB; p->pop=0; *--cp1='$'; p->code=cp1; ! 117: } ! 118: /* fall thru ... */ ! 119: case CASE: ! 120: default: std: ! 121: p=bflow(p); break; ! 122: case MUL: ! 123: { ! 124: /* ! 125: ** Change multiplication by constant powers of 2 to ! 126: ** shifts. ! 127: */ ! 128: splitrand(p); ! 129: if (regs[RT1][0] != '$' || regs[RT1][1] == '-') goto std; ! 130: if ((r = ispow2(getnum(®s[RT1][1]))) < 0) goto std; ! 131: switch (r) ! 132: { ! 133: case 0: /* mull3 $1,x,y */ ! 134: if (p->subop == U(LONG,OP3)) ! 135: { ! 136: if (equstr(regs[RT2], regs[RT3])) ! 137: { ! 138: delnode(p); p = p->forw; ! 139: } ! 140: else ! 141: { ! 142: p->op = MOV; p->subop = LONG; ! 143: p->pop = 0; newcode(p); nchange++; ! 144: } ! 145: } ! 146: else ! 147: if (p->subop == U(LONG,OP2)) ! 148: { ! 149: delnode(p); p = p->forw; ! 150: } ! 151: goto std; ! 152: ! 153: case 1: /* mull2 $2,x */ ! 154: if (p->subop == U(LONG, OP2) && !source(regs[RT2])) ! 155: { ! 156: strcpy(regs[RT1], regs[RT2]); ! 157: p->op = ADD; p->pop = 0; newcode(p); nchange++; ! 158: } ! 159: goto std; ! 160: } ! 161: if(p->subop==U(LONG,OP3)||(p->subop==U(LONG,OP2)&&!source(regs[RT2]))) ! 162: { ! 163: if (p->subop == U(LONG,OP2)) ! 164: strcpy(regs[RT3], regs[RT2]); ! 165: sprintf(regs[RT1], "$%d", r); ! 166: p->op = ASH; p->subop = LONG; ! 167: p->pop = 0; newcode(p); nchange++; ! 168: } ! 169: goto std; ! 170: } ! 171: case ASH: ! 172: { ! 173: /* address comp: ! 174: ** ashl $1,bar,r0 \ movl bar,r0 ! 175: ** movab _foo[r0] / movaw _foo[r0] ! 176: ** ! 177: ** ashl $2,r0,r0 \ moval _foo[r0] ! 178: ** movab _foo[r0] / ! 179: */ ! 180: register struct node *pf; ! 181: register int shfrom, shto; ! 182: long shcnt; ! 183: char *regfrom; ! 184: ! 185: splitrand(p); ! 186: if (regs[RT1][0] != '$') goto std; ! 187: if ((shcnt = getnum(®s[RT1][1])) < 1 || shcnt > 3) goto std; ! 188: if ((shfrom = isreg(regs[RT2])) >= 0) ! 189: regfrom = copy(regs[RT2],"]"); ! 190: if ((shto = isreg(regs[RT3])) >= 0 && shto<NUSE) ! 191: { ! 192: int regnum; ! 193: ! 194: if (uses[shto] != (pf = p->forw)) goto ashadd; ! 195: if (pf->op != MOVA && pf->op != PUSHA) goto ashadd; ! 196: if (pf->subop != BYTE) goto ashadd; ! 197: splitrand(pf); ! 198: if (!indexa(regs[RT1])) goto std; ! 199: cp2 = regs[RT1]; ! 200: if(!isstatic(cp2)) goto std; ! 201: while (*cp2++ != '[') ; ! 202: if (*cp2++ != 'r' || !isdigit(*cp2)) goto std; ! 203: regnum = *cp2++ - '0'; ! 204: if (isdigit(*cp2)) ! 205: { ! 206: if (cp2[1] != ']') goto std; ! 207: regnum *= 10; regnum += *cp2 - '0'; ! 208: } ! 209: if (regnum != shto) goto std; ! 210: if (shfrom >= 0) /* ashl $N,r*,r0 */ ! 211: { ! 212: delnode(p); ! 213: if (shfrom != shto) ! 214: { ! 215: uses[shto] = NULL; splitrand(pf); ! 216: cp2=regs[RT1]; while (*cp2++!='['); ! 217: cp1=regfrom; while (*cp2++= *cp1++); ! 218: newcode(pf); ! 219: } ! 220: } ! 221: else ! 222: { ! 223: p->op = MOV; splitrand(p); ! 224: strcpy(regs[RT1], regs[RT2]); ! 225: strcpy(regs[RT2], regs[RT3]); ! 226: regs[RT3][0] = '\0'; ! 227: p->pop = 0; newcode(p); ! 228: } ! 229: switch (shcnt) ! 230: { ! 231: case 1: pf->subop = WORD; break; ! 232: case 2: pf->subop = LONG; break; ! 233: case 3: pf->subop = QUAD; break; ! 234: } ! 235: redunm++; nsaddr++; nchange++; ! 236: } ! 237: goto std; ! 238: ashadd: ! 239: /* at this point, RT2 and RT3 are guaranteed to be simple regs*/ ! 240: if (shcnt == 1 && equstr(regs[RT2], regs[RT3])) ! 241: { ! 242: /* ! 243: ** quickie: ! 244: ** ashl $1,A,A > addl2 A,A ! 245: */ ! 246: p->op = ADD; p->subop = U(LONG,OP2); p->pop = 0; ! 247: strcpy(regs[RT1], regs[RT2]); regs[RT3][0] = '\0'; ! 248: newcode(p); nchange++; ! 249: } ! 250: goto std; ! 251: } ! 252: ! 253: case EXTV: ! 254: case EXTZV: ! 255: { ! 256: /* bit tests: ! 257: ** extv A,$1,B,rC \ ! 258: ** tstl rC > jbc A,B,D ! 259: ** jeql D / ! 260: ** ! 261: ** also byte- and word-size fields: ! 262: ** extv $n*8,$8,A,B > cvtbl n+A,B ! 263: ** extv $n*16,$16,A,B > cvtwl n+A,B ! 264: ** extzv $n*8,$8,A,B > movzbl n+A,B ! 265: ** extzv $n*16,$16,A,B > movzwl n+A,B ! 266: */ ! 267: register struct node *pf; /* forward node */ ! 268: register struct node *pn; /* next node (after pf) */ ! 269: int flen; /* field length */ ! 270: ! 271: splitrand(p); ! 272: if (regs[RT2][0] != '$') goto std; ! 273: if ((flen = getnum(®s[RT2][1])) < 0) goto std; ! 274: if (flen == 1) ! 275: { ! 276: register int extreg; /* reg extracted to */ ! 277: ! 278: extreg = isreg(regs[RT4]); ! 279: if (extreg < 0 || extreg >= NUSE) goto std; ! 280: if ((pf = p->forw)->op != TST) goto std; ! 281: if (uses[extreg] && uses[extreg] != pf) goto std; ! 282: splitrand(pf); ! 283: if (extreg != isreg(regs[RT1])) goto std; ! 284: if ((pn = pf->forw)->op != CBR) goto std; ! 285: if (pn->subop != JEQ && pn->subop != JNE) goto std; ! 286: delnode(p); delnode(pf); ! 287: pn->subop = (pn->subop == JEQ) ? JBC : JBS; ! 288: for(cp2=p->code; *cp2++!=',';); ! 289: for(cp1=cp2; *cp1++!=',';); ! 290: while (*cp1!=',') *cp2++= *cp1++; *cp2='\0'; ! 291: pn->code = p->code; pn->pop = NULL; ! 292: uses[extreg] = NULL; ! 293: } ! 294: else ! 295: if (flen == 8 || flen == 16) ! 296: { ! 297: register int boff; /* bit offset */ ! 298: register int coff; /* chunk (byte or word) offset*/ ! 299: ! 300: if (regs[RT1][0] != '$') goto std; ! 301: if ((boff = getnum(®s[RT1][1])) < 0) goto std; ! 302: coff = boff / flen; ! 303: if (coff && (isreg(regs[RT3]) >= 0)) goto std; ! 304: if (boff < 0 || (boff % flen) != 0) goto std; ! 305: p->op = (p->op == EXTV) ? CVT : MOVZ; ! 306: p->subop = U((flen == 8 ? BYTE : WORD), LONG); ! 307: if (coff == 0) ! 308: strcpy(regs[RT1], regs[RT3]); ! 309: else ! 310: sprintf(regs[RT1], "%d%s%s", coff, regs[RT3][0]=='(' ? "":"+", ! 311: regs[RT3]); ! 312: strcpy(regs[RT2], regs[RT4]); ! 313: regs[RT3][0] = '\0'; regs[RT4][0] = '\0'; ! 314: p->pop = 0; newcode(p); ! 315: } ! 316: nchange++; ! 317: goto std; ! 318: } ! 319: ! 320: case CMP: ! 321: { ! 322: /* comparison to -63 to -1: ! 323: ** cmpl r0,$-1 > incl r0 ! 324: ** jeql ... ! 325: ** ! 326: ** cmpl r0,$-63 > addl2 $63,r0 ! 327: ** jeql ... ! 328: */ ! 329: register int num; ! 330: register int reg; ! 331: register struct node *regp = p->back; ! 332: ! 333: if (p->forw->op != CBR) goto std; ! 334: if (p->forw->subop != JEQ && p->forw->subop != JNE) goto std; ! 335: splitrand(p); ! 336: if (strncmp(regs[RT2], "$-", 2) != 0) goto std; ! 337: reg = r = isreg(regs[RT1]); ! 338: if (r < 0) goto std; ! 339: if (r < NUSE && uses[r] != 0) goto std; ! 340: if (r >= NUSE && regp->op == MOV && p->subop == regp->subop) ! 341: { ! 342: if (*regp->code != 'r') goto std; ! 343: reg = regp->code[1] - '0'; ! 344: if (isdigit(regp->code[2]) || reg >= NUSE || uses[reg]) ! 345: goto std; ! 346: } ! 347: if (r >= NUSE) goto std; ! 348: if (reg != r) ! 349: sprintf(regs[RT1], "r%d", reg); ! 350: if ((num = getnum(®s[RT2][2])) <= 0 || num > 63) goto std; ! 351: if (num == 1) ! 352: { ! 353: p->op = INC; regs[RT2][0] = '\0'; ! 354: } ! 355: else ! 356: { ! 357: register char *t; ! 358: ! 359: t=regs[RT1];regs[RT1]=regs[RT2];regs[RT2]=t; ! 360: p->op = ADD; p->subop = U(p->subop, OP2); ! 361: for (t = ®s[RT1][2]; t[-1] = *t; t++) ; ! 362: } ! 363: p->pop = 0; newcode(p); ! 364: nchange++; ! 365: goto std; ! 366: } ! 367: ! 368: case JSB: ! 369: if (equstr(p->code,"mcount")) {uses[0]=p; regs[0][0]= -1;} ! 370: goto std; ! 371: case JBR: case JMP: ! 372: clearuse(); ! 373: if (p->subop==RET || p->subop==RSB) {uses[0]=p; regs[0][0]= -1; break;} ! 374: if (p->ref==0) goto std; /* jmp (r0) */ ! 375: /* fall through */ ! 376: case CBR: ! 377: if (p->ref->ref!=0) for (r=NUSE;--r>=0;) ! 378: if (biti[r] & (int)p->ref->ref) {uses[r]=p; regs[r][0]= -1;} ! 379: case EROU: case JSW: ! 380: case TEXT: case DATA: case BSS: case ALIGN: case WGEN: case END: ; ! 381: } ! 382: } ! 383: for (p= &first; p!=0; p=p->forw) ! 384: if (p->op==LABEL || p->op==DLABEL) p->ref=0; /* erase our tracks */ ! 385: } ! 386: ! 387: rmove() ! 388: { ! 389: register struct node *p, *lastp; ! 390: register int r; ! 391: int r1; ! 392: ! 393: clearreg(); ! 394: for (p=first.forw; p!=0; p = p->forw) { ! 395: lastp=p; ! 396: if (debug) { ! 397: printf("Regs:\n"); ! 398: for (r=0; r<NREG; r++) ! 399: if (regs[r][0]) { ! 400: r1=regs[r][0]; ! 401: printf("%d: %d%d %s\n", r, r1&0xF, r1>>4, regs[r]+1); ! 402: } ! 403: printf("-\n"); ! 404: } ! 405: switch (p->op) { ! 406: ! 407: case CVT: ! 408: splitrand(p); goto mov; ! 409: ! 410: case MOV: ! 411: splitrand(p); ! 412: if ((r = findrand(regs[RT1],p->subop)) >= 0) { ! 413: if (r == isreg(regs[RT2]) && p->forw->op!=CBR) { ! 414: delnode(p); redunm++; break; ! 415: } ! 416: } ! 417: mov: ! 418: repladdr(p); ! 419: r = isreg(regs[RT1]); ! 420: r1 = isreg(regs[RT2]); ! 421: dest(regs[RT2],p->subop); ! 422: if (r>=0) { ! 423: if (r1>=0) savereg(r1, regs[r]+1, p->subop); ! 424: else if (p->op!=CVT) savereg(r, regs[RT2], p->subop); ! 425: } else if (r1>=0) savereg(r1, regs[RT1], p->subop); ! 426: else if (p->op!=CVT) setcon(regs[RT1], regs[RT2], p->subop); ! 427: break; ! 428: ! 429: /* .rx,.wx */ ! 430: case MFPR: ! 431: case COM: ! 432: case NEG: ! 433: /* .rx,.wx or .rx,.rx,.wx */ ! 434: case ADD: ! 435: case SUB: ! 436: case BIC: ! 437: case BIS: ! 438: case XOR: ! 439: case MUL: ! 440: case DIV: ! 441: case ASH: ! 442: case MOVZ: ! 443: /* .rx,.rx,.rx,.wx */ ! 444: case EXTV: ! 445: case EXTZV: ! 446: case INSV: ! 447: splitrand(p); ! 448: repladdr(p); ! 449: dest(lastrand,p->subop); ! 450: if (p->op==INSV) ccloc[0]=0; ! 451: break; ! 452: ! 453: /* .mx or .wx */ ! 454: case CLR: ! 455: case INC: ! 456: case DEC: ! 457: splitrand(p); ! 458: dest(lastrand,p->subop); ! 459: if (p->op==CLR) ! 460: if ((r = isreg(regs[RT1])) >= 0) ! 461: savereg(r, "$0", p->subop); ! 462: else ! 463: setcon("$0", regs[RT1], p->subop); ! 464: break; ! 465: ! 466: /* .rx */ ! 467: case TST: ! 468: case PUSH: ! 469: splitrand(p); ! 470: lastrand=regs[RT1+1]; /* fool repladdr into doing 1 operand */ ! 471: repladdr(p); ! 472: if (p->op==TST && equstr(lastrand=regs[RT1], ccloc+1) ! 473: && ((0xf&(ccloc[0]>>4))==p->subop || equtype(ccloc[0],p->subop)) ! 474: &&!source(lastrand)) { ! 475: delnode(p); p = p->back; nrtst++; nchange++; ! 476: } ! 477: setcc(lastrand,p->subop); ! 478: break; ! 479: ! 480: /* .rx,.rx,.rx */ ! 481: case PROBER: ! 482: case PROBEW: ! 483: case CASE: ! 484: case MOVC3: ! 485: /* .rx,.rx */ ! 486: case MTPR: ! 487: case CALLS: ! 488: case CMP: ! 489: case BIT: ! 490: splitrand(p); ! 491: /* fool repladdr into doing right number of operands */ ! 492: if (p->op==CASE || p->op==PROBER || p->op==PROBEW) lastrand=regs[RT4]; ! 493: /* else if (p->op==CMPV || p->op==CMPZV) lastrand=regs[RT4+1]; */ ! 494: else if (p->op==MOVC3) lastrand=regs[RT1]; ! 495: else lastrand=regs[RT3]; ! 496: repladdr(p); ! 497: if (p->op==CALLS || p->op==MOVC3) clearreg(); ! 498: if (p->op==BIT) bitopt(p); ! 499: ccloc[0]=0; break; ! 500: ! 501: case CBR: ! 502: if (p->subop>=JBC) { ! 503: splitrand(p); ! 504: if (p->subop<JBCC) lastrand=regs[RT3]; /* 2 operands can be optimized */ ! 505: else lastrand=regs[RT2]; /* .mb destinations lose */ ! 506: repladdr(p); ! 507: } ! 508: ccloc[0] = 0; ! 509: break; ! 510: ! 511: case JBR: ! 512: redunbr(p); ! 513: ! 514: /* .wx,.bb */ ! 515: case SOB: ! 516: ! 517: default: ! 518: clearreg(); ! 519: } ! 520: } ! 521: } ! 522: ! 523: char * ! 524: byondrd(p) register struct node *p; { ! 525: /* return pointer to register which is "beyond last read/modify operand" */ ! 526: if (OP2==(p->subop>>4)) return(regs[RT3]); ! 527: switch (p->op) { ! 528: case MFPR: ! 529: case JSB: ! 530: case PUSHA: ! 531: case TST: case INC: case DEC: case PUSH: return(regs[RT2]); ! 532: case MTPR: ! 533: case BIT: case CMP: case CALLS: return(regs[RT3]); ! 534: case PROBER: case PROBEW: ! 535: case CASE: case MOVC3: return(regs[RT4]); ! 536: } ! 537: return(lastrand); ! 538: } ! 539: ! 540: struct node * ! 541: bflow(p) ! 542: register struct node *p; ! 543: { ! 544: register char *cp1,*cp2,**preg; register int r; ! 545: int flow= -1; ! 546: struct node *olduse=0; ! 547: splitrand(p); ! 548: if (p->op!=PUSH && p->subop && 0<=(r=isreg(lastrand)) && r<NUSE && uses[r]==p->forw) { ! 549: if (equtype(p->subop,regs[r][0]) ! 550: || ((p->op==CVT || p->op==MOVZ) ! 551: && 0xf®s[r][0] && compat(0xf&(p->subop>>4),regs[r][0]))) { ! 552: register int r2; ! 553: if (regs[r][1]!=0) {/* send directly to destination */ ! 554: if (p->op==INC || p->op==DEC) { ! 555: if (p->op==DEC) p->op=SUB; else p->op=ADD; ! 556: p->subop=(OP2<<4)+(p->subop&0xF); /* use 2 now, convert to 3 later */ ! 557: p->pop=0; ! 558: cp1=lastrand; cp2=regs[RT2]; while (*cp2++= *cp1++); /* copy reg */ ! 559: cp1=lastrand; *cp1++='$'; *cp1++='1'; *cp1=0; ! 560: } ! 561: cp1=regs[r]+1; cp2=lastrand; ! 562: if (OP2==(p->subop>>4)) {/* use 3 operand form of instruction */ ! 563: p->pop=0; ! 564: p->subop += (OP3-OP2)<<4; lastrand=cp2=regs[RT3]; ! 565: } ! 566: while (*cp2++= *cp1++); ! 567: if (p->op==MOVA && p->forw->op==PUSH) { ! 568: p->op=PUSHA; *regs[RT2]=0; p->pop=0; ! 569: } else if (p->op==MOV && p->forw->op==PUSH) { ! 570: p->op=PUSH ; *regs[RT2]=0; p->pop=0; ! 571: } ! 572: delnode(p->forw); ! 573: if (0<=(r2=isreg(lastrand)) && r2<NUSE) { ! 574: uses[r2]=uses[r]; uses[r]=0; ! 575: } ! 576: redun3(p,0); ! 577: newcode(p); redunm++; flow=r; ! 578: } else if (p->op==MOV && p->forw->op!=EXTV && p->forw->op!=EXTZV) { ! 579: /* superfluous fetch */ ! 580: int nmatch; ! 581: char src[C2_ASIZE]; ! 582: movit: ! 583: cp2=src; cp1=regs[RT1]; while (*cp2++= *cp1++); ! 584: splitrand(p->forw); ! 585: if (p->forw->op != INC && p->forw->op != DEC) ! 586: lastrand=byondrd(p->forw); ! 587: nmatch=0; ! 588: for (preg=regs+RT1;*preg!=lastrand;preg++) ! 589: if (r==isreg(*preg)) { ! 590: cp2= *preg; cp1=src; while (*cp2++= *cp1++); ++nmatch; ! 591: } ! 592: if (nmatch==1) { ! 593: if (OP2==(p->forw->subop>>4) && equstr(src,regs[RT2])) { ! 594: p->forw->pop=0; ! 595: p->forw->subop += (OP3-OP2)<<4; cp1=regs[RT3]; ! 596: *cp1++='r'; *cp1++=r+'0'; *cp1=0; ! 597: } ! 598: delnode(p); p=p->forw; ! 599: if (0<=(r2=isreg(src)) && r2<NUSE) { ! 600: uses[r2]=uses[r]; uses[r]=0; ! 601: } ! 602: redun3(p,0); ! 603: newcode(p); redunm++; flow=r; ! 604: } else splitrand(p); ! 605: } ! 606: } else if (p->op==MOV && (p->forw->op==CVT || p->forw->op==MOVZ) ! 607: && p->forw->subop&0xf /* if base or index, then forget it */ ! 608: && compat(p->subop,p->forw->subop) && !source(cp1=regs[RT1]) ! 609: && !indexa(cp1)) goto movit; ! 610: } ! 611: /* adjust 'lastrand' past any 'read' or 'modify' operands. */ ! 612: lastrand=byondrd(p); ! 613: /* a 'write' clobbers the register. */ ! 614: if (0<=(r=isreg(lastrand)) && r<NUSE ! 615: || OP2==(p->subop>>4) && 0<=(r=isreg(regs[RT2])) && r<NUSE && uses[r]==0) { ! 616: /* writing a dead register is useless, but watch side effects */ ! 617: switch (p->op) { ! 618: case ACB: ! 619: case AOBLEQ: case AOBLSS: case SOBGTR: case SOBGEQ: break; ! 620: default: ! 621: if (uses[r]==0) {/* no direct uses, check for use of condition codes */ ! 622: register struct node *q=p; ! 623: while ((q=nonlab(q->forw))->combop==JBR) q=q->ref; /* cc unused, unchanged */ ! 624: if (q->op!=CBR) {/* ... and destroyed */ ! 625: preg=regs+RT1; ! 626: while (cp1= *preg++) { ! 627: if (cp1==lastrand) {redunm++; delnode(p); return(p->forw);} ! 628: if (source(cp1) || equstr(cp1,lastrand)) break; ! 629: } ! 630: } ! 631: } ! 632: flow=r; ! 633: } ! 634: } ! 635: if (0<=(r=flow)) {olduse=uses[r]; uses[r]=0; *(short *)(regs[r])=0;} ! 636: /* these two are here, rather than in bmove(), ! 637: /* because I decided that it was better to go for 3-address code ! 638: /* (save time) rather than fancy jbxx (save 1 byte) ! 639: /* on sequences like bisl2 $64,r0; movl r0,foo ! 640: */ ! 641: if (p->op==BIC) {p=bicopt(p); splitrand(p); lastrand=byondrd(p);} ! 642: if (p->op==BIS) {bixprep(p,JBSS); lastrand=byondrd(p);} ! 643: /* now look for 'read' or 'modify' (read & write) uses */ ! 644: preg=regs+RT1; ! 645: while (*(cp1= *preg++)) { ! 646: /* check for r */ ! 647: if (lastrand!=cp1 && 0<=(r=isreg(cp1)) && r<NUSE && uses[r]==0) { ! 648: uses[r]=p; cp2=regs[r]; *cp2++=p->subop; ! 649: if (p->op==ASH && preg==(regs+RT1+1)) cp2[-1]=BYTE; /* stupid DEC */ ! 650: if (p->op==MOV || p->op==PUSH || p->op==CVT || p->op==MOVZ || p->op==COM || p->op==NEG) { ! 651: if (p->op==PUSH) cp1="-(sp)"; ! 652: else { ! 653: cp1=regs[RT2]; ! 654: if (0<=(r=isreg(cp1)) && r<NUSE && uses[r]==0) ! 655: uses[r]=olduse; /* reincarnation!! */ ! 656: /* as in addl2 r0,r1; movl r1,r0; ret */ ! 657: if (p->op!=MOV) cp1=0; ! 658: } ! 659: if (cp1) while (*cp2++= *cp1++); ! 660: else *cp2=0; ! 661: } else *cp2=0; ! 662: continue; ! 663: } ! 664: /* check for (r),(r)+,-(r),[r] */ ! 665: do if (*cp1=='(' || *cp1=='[') {/* get register number */ ! 666: char t; ! 667: cp2= ++cp1; while (*++cp1!=')' && *cp1!=']'); t= *cp1; *cp1=0; ! 668: if (0<=(r=isreg(cp2)) && r<NUSE && (uses[r]==0 || uses[r]==p)) { ! 669: uses[r]=p; regs[r][0]=(*--cp2=='[' ? OPX<<4 : OPB<<4); ! 670: } ! 671: *cp1=t; ! 672: } while (*++cp1); ! 673: } ! 674: /* pushax or movax possibility? */ ! 675: cp1=regs[RT1]; ! 676: if (*cp1++=='$' && isstatic(cp1) && natural(regs[RT1])) { ! 677: if (p->combop==T(MOV,LONG)) { ! 678: if (regs[RT1][1]=='L' && 0!=(p->labno=getnum(regs[RT1]+2))) { ! 679: cp1=p->code; while (*cp1++!=','); p->code= --cp1; ! 680: } ! 681: p->combop=T(MOVA,LONG); ++p->code; p->pop=0; ! 682: } else if (p->combop==T(PUSH,LONG)) { ! 683: p->combop=T(PUSHA,LONG); ++p->code; p->pop=0; ! 684: } else if ((p->combop&0xFFFF)==T(ADD,U(LONG,OP3)) ! 685: && 0<=(r=isreg(regs[RT2]))) { ! 686: cp1=cp2=p->code; ++cp1; ! 687: do *cp2++= *cp1; while (*cp1++!=','); cp2[-1]='['; ! 688: do *cp2++= *cp1; while (*cp1++!=','); cp2[-1]=']'; ! 689: if (!equstr(regs[RT3],"-(sp)")) p->combop=T(MOVA,BYTE); ! 690: else {p->combop=T(PUSHA,BYTE); *cp2=0;} ! 691: if (uses[r]==0) {uses[r]=p; regs[r][0]=OPX<<4;} ! 692: p->pop=0; ! 693: } ! 694: } ! 695: return(p); ! 696: } ! 697: ! 698: ispow2(n) register long n; {/* -1 -> no; else -> log to base 2 */ ! 699: register int log; ! 700: if (n==0 || n&(n-1)) return(-1); log=0; ! 701: for (;;) {n >>= 1; if (n==0) return(log); ++log; if (n== -1) return(log);} ! 702: } ! 703: ! 704: bitopt(p) register struct node *p; { ! 705: /* change "bitx $<power_of_2>,a" followed by JEQ or JNE ! 706: /* into JBC or JBS. watch out for I/O registers. (?) ! 707: /* assumes that 'splitrand' has already been called. ! 708: */ ! 709: register char *cp1,*cp2; int b; ! 710: cp1=regs[RT1]; cp2=regs[RT2]; ! 711: if (*cp1++!='$' || !okio(cp2) || p->forw->op!=CBR || p->forw->subop&-2 || ! 712: 0>(b=ispow2(getnum(cp1))) || ! 713: p->subop!=BYTE && (source(cp2) || indexa(cp2))) return; ! 714: if (b>=bitsize[p->subop]) {/* you dummy! */ ! 715: if (source(cp2)) {/* side effect: auto increment or decrement */ ! 716: p->pop=0; ! 717: p->op=TST; --cp1; while (*cp1++= *cp2++); ! 718: regs[RT2][0]=0; newcode(p); ! 719: } else delnode(p); ! 720: p = p->forw; ! 721: if (p->subop==JEQ) {p->combop=JBR; p->pop=0;} ! 722: else delnode(p); ! 723: nchange++; nbj++; return; ! 724: } ! 725: if (cp1=p->forw->code) {/* destination is not an internal label */ ! 726: cp2=regs[RT3]; while (*cp2++= *cp1++); ! 727: } ! 728: if (b==0 && (p->subop==LONG || !indexa(regs[RT2]))) {/* JLB optimization, ala BLISS */ ! 729: cp2=regs[RT1]; cp1=regs[RT2]; while (*cp2++= *cp1++); ! 730: cp2=regs[RT2]; cp1=regs[RT3]; while (*cp2++= *cp1++); ! 731: *(regs[RT3])=0; p->forw->subop += JLBC-JBC; ! 732: p->forw->pop=0; ! 733: } else { ! 734: cp1=regs[RT1]+1; ! 735: if (b>9) *cp1++= b/10 +'0'; *cp1++= b%10 +'0'; *cp1=0; /* $<bit_number> */ ! 736: } ! 737: nbj++; newcode(p); p->combop = p->forw->combop+((JBC-JEQ)<<8); ! 738: p->labno = p->forw->labno; delnode(p->forw); ! 739: p->pop=0; ! 740: } ! 741: ! 742: isfield(n) register long n; {/* -1 -> no; else -> position of low bit */ ! 743: register int pos; register long t; ! 744: t= ((n-1)|n) +1; ! 745: if (n!=0 && (0==t || 0==n || 0<=ispow2(t))) { ! 746: pos=0; while(!(n&1)) {n >>= 1; ++pos;} return(pos); ! 747: } else return(-1); ! 748: } ! 749: ! 750: bixprep(p,bix) register struct node *p; { ! 751: /* initial setup, single-bit checking for bisopt, bicopt. ! 752: /* return: 0->don't bother any more; 1->worthwhile trying ! 753: */ ! 754: register char *cp1,*cp2; ! 755: splitrand(p); cp1=regs[RT1]; cp2=regs[RT2]; ! 756: if (*cp1++!='$' || 0>(pos=isfield(f=getnum(cp1))) ! 757: || !okio(cp2) || indexa(cp2) || source(cp2) || !okio(lastrand)) return(0); ! 758: f |= f-1; if (++f==0) siz=32-pos; else siz=ispow2(f)-pos; ! 759: if (siz==1 && pos>5 && (p->subop>>4)==OP2 && (p->subop&0xF)!=BYTE ! 760: && pos<bitsize[p->subop&0xF]) { ! 761: p->ref = insertl(p->forw); p->combop = CBR | (bix<<8); ! 762: p->pop=0; ! 763: p->labno = p->ref->labno; ! 764: if (pos>9) {*cp1++= pos/10 +'0'; pos %= 10;} ! 765: *cp1++=pos+'0'; *cp1=0; newcode(p); nbj++; return(0); ! 766: } ! 767: return(1); ! 768: } ! 769: ! 770: ! 771: struct node * ! 772: bicopt(p) register struct node *p; { ! 773: /* use field operations or MOVZ if possible. done as part of 'bflow'. ! 774: */ ! 775: register char *cp1,*cp2; int r; ! 776: char src[C2_ASIZE]; ! 777: if (!bixprep(p,JBCC)) return(p); ! 778: if (f==0) {/* the BIC isolates low order bits */ ! 779: siz=pos; pos=0; ! 780: if ((p->subop&0xF)==LONG && *(regs[RT2])!='$') {/* result of EXTZV is long */ ! 781: /* save source of BICL in 'src' */ ! 782: cp1=regs[RT2]; cp2=src; while (*cp2++= *cp1++); ! 783: if (p->back->op==ASH) {/* try for more */ ! 784: splitrand(p->back); cp1=regs[RT1]; cp2=regs[RT3]; ! 785: if (*cp1++=='$' && *(regs[RT2])!='$' && !indexa(regs[RT2]) ! 786: && 0>(f=getnum(cp1)) && equstr(src,cp2) ! 787: && 0<=(r=isreg(cp2)) && r<NUSE) {/* a good ASH */ ! 788: pos -= f; cp1=regs[RT2]; cp2=src; while (*cp2++= *cp1++); ! 789: delnode(p->back); ! 790: } ! 791: } ! 792: if (p->back->op==CVT || p->back->op==MOVZ) {/* greedy, aren't we? */ ! 793: splitrand(p->back); cp1=regs[RT1]; cp2=regs[RT2]; ! 794: if (equstr(src,cp2) && okio(cp1) && !indexa(cp1) ! 795: && 0<=(r=isreg(cp2)) && r<NUSE ! 796: && bitsize[p->back->subop&0xF]>=(pos+siz) ! 797: && bitsize[p->back->subop>>4]>=(pos+siz)) {/* good CVT */ ! 798: cp1=regs[RT1]; cp2=src; while (*cp2++= *cp1++); ! 799: delnode(p->back); ! 800: } ! 801: } ! 802: /* 'pos', 'siz' known; source of field is in 'src' */ ! 803: splitrand(p); /* retrieve destination of BICL */ ! 804: if (siz==8 && pos==0) { ! 805: p->combop = T(MOVZ,U(BYTE,LONG)); ! 806: sprintf(line,"%s,%s",src,lastrand); ! 807: } else { ! 808: p->combop = T(EXTZV,LONG); ! 809: sprintf(line,"$%d,$%d,%s,%s",pos,siz,src,lastrand); ! 810: } ! 811: p->pop=0; ! 812: p->code = copy(line); nfield++; return(p); ! 813: }/* end EXTZV possibility */ ! 814: }/* end low order bits */ ! 815: /* unfortunately, INSV clears the condition codes, thus cannot be used */ ! 816: /* else {/* see if BICL2 of positive field should be INSV $0 */ ! 817: /* if (p->subop==(LONG | (OP2<<4)) && 6<=(pos+siz)) { ! 818: /* p->combop = INSV; ! 819: /* sprintf(line,"$0,$%d,$%d,%s",pos,siz,lastrand); ! 820: /* p->code = copy(line); nfield++; return(p); ! 821: /* } ! 822: /* } ! 823: */ ! 824: return(p); ! 825: } ! 826: ! 827: jumpsw() ! 828: { ! 829: register struct node *p, *p1; ! 830: register t; ! 831: int nj; ! 832: ! 833: t = 0; ! 834: nj = 0; ! 835: for (p=first.forw; p!=0; p = p->forw) ! 836: p->seq = ++t; ! 837: for (p=first.forw; p!=0; p = p1) { ! 838: p1 = p->forw; ! 839: if (p->op == CBR && p1->op==JBR && p->ref && p1->ref ! 840: && abs(p->seq - p->ref->seq) > abs(p1->seq - p1->ref->seq)) { ! 841: if (p->ref==p1->ref) ! 842: continue; ! 843: p->subop = revbr[p->subop]; ! 844: p->pop=0; ! 845: t = p1->ref; ! 846: p1->ref = p->ref; ! 847: p->ref = t; ! 848: t = p1->labno; ! 849: p1->labno = p->labno; ! 850: p->labno = t; ! 851: #ifdef COPYCODE ! 852: if (p->labno == 0) { ! 853: t = p1->code; p1->code = p->code; p->code = t; ! 854: } ! 855: #endif ! 856: nrevbr++; ! 857: nj++; ! 858: } ! 859: } ! 860: return(nj); ! 861: } ! 862: ! 863: addsob() ! 864: { ! 865: register struct node *p, *p1, *p2, *p3; ! 866: ! 867: for (p = &first; (p1 = p->forw)!=0; p = p1) { ! 868: if (p->combop==T(DEC,LONG) && p1->op==CBR) { ! 869: if (abs(p->seq - p1->ref->seq) > 8) continue; ! 870: if (p1->subop==JGE || p1->subop==JGT) { ! 871: if (p1->subop==JGE) p->combop=SOBGEQ; else p->combop=SOBGTR; ! 872: p->pop=0; ! 873: p->labno = p1->labno; delnode(p1); nsob++; ! 874: } ! 875: } else if (p->combop==T(INC,LONG)) { ! 876: if (p1->op==LABEL && p1->refc==1 && p1->forw->combop==T(CMP,LONG) ! 877: && (p2=p1->forw->forw)->combop==T(CBR,JLE) ! 878: && (p3=p2->ref->back)->combop==JBR && p3->ref==p1 ! 879: && p3->forw->op==LABEL && p3->forw==p2->ref) { ! 880: /* change INC LAB: CMP to LAB: INC CMP */ ! 881: p->back->forw=p1; p1->back=p->back; ! 882: p->forw=p1->forw; p1->forw->back=p; ! 883: p->back=p1; p1->forw=p; ! 884: p1=p->forw; ! 885: /* adjust beginning value by 1 */ ! 886: p2=alloc(sizeof first); p2->combop=T(DEC,LONG); ! 887: p2->pop=0; ! 888: p2->forw=p3; p2->back=p3->back; p3->back->forw=p2; ! 889: p3->back=p2; p2->code=p->code; p2->labno=0; ! 890: } ! 891: if (p1->combop==T(CMP,LONG) && (p2=p1->forw)->op==CBR) { ! 892: register char *cp1,*cp2; ! 893: splitrand(p1); if (!equstr(p->code,regs[RT1])) continue; ! 894: if (abs(p->seq - p2->ref->seq)<8) {/* inside byte displ range */ ! 895: if (p2->subop!=JLE) continue; ! 896: /* ! 897: * acbl take twice as long as aobleq ! 898: * so changed to aobleq - dm 1/19/82 ! 899: */ ! 900: p->combop=AOBLEQ; ! 901: cp2=regs[RT1]; cp1=regs[RT2]; while (*cp2++= *cp1++); /* limit */ ! 902: cp2=regs[RT2]; cp1=p->code; while (*cp2++ = *cp1++); /* index */ ! 903: p->pop=0; newcode(p); ! 904: p->labno = p2->labno; delnode(p2); delnode(p1); nsob++; ! 905: } else ! 906: if (abs(p->seq - p2->ref->seq)>8) {/* outside byte displ range */ ! 907: if (p2->subop!=JLE) continue; ! 908: p->combop=T(ACB,LONG); ! 909: cp2=regs[RT1]; cp1=regs[RT2]; while (*cp2++= *cp1++); /* limit */ ! 910: cp2=regs[RT2]; cp1="$1"; while (*cp2++= *cp1++); ! 911: cp2=regs[RT3]; cp1=p->code; while (*cp2++= *cp1++); ! 912: p->pop=0; newcode(p); ! 913: p->labno = p2->labno; delnode(p2); delnode(p1); nsob++; ! 914: } else if (p2->subop==JLE || p2->subop==JLT) { ! 915: if (p2->subop==JLE) p->combop=AOBLEQ; else p->combop=AOBLSS; ! 916: cp2=regs[RT1]; cp1=regs[RT2]; while (*cp2++= *cp1++); /* limit */ ! 917: cp2=regs[RT2]; cp1=p->code; while (*cp2++= *cp1++); /* index */ ! 918: p->pop=0; newcode(p); ! 919: p->labno = p2->labno; delnode(p2); delnode(p1); nsob++; ! 920: } ! 921: } ! 922: } ! 923: } ! 924: } ! 925: ! 926: abs(x) ! 927: { ! 928: return(x<0? -x: x); ! 929: } ! 930: ! 931: equop(p1, p2) ! 932: register struct node *p1; ! 933: struct node *p2; ! 934: { ! 935: register char *cp1, *cp2; ! 936: ! 937: if (p1->combop != p2->combop) ! 938: return(0); ! 939: if (p1->op>0 && p1->op<MOV) ! 940: return(0); ! 941: if (p1->op==MOVA && p1->labno!=p2->labno) return(0); ! 942: cp1 = p1->code; ! 943: cp2 = p2->code; ! 944: if (cp1==0 && cp2==0) ! 945: return(1); ! 946: if (cp1==0 || cp2==0) ! 947: return(0); ! 948: while (*cp1 == *cp2++) ! 949: if (*cp1++ == 0) ! 950: return(1); ! 951: return(0); ! 952: } ! 953: ! 954: delnode(p) register struct node *p; { ! 955: p->back->forw = p->forw; ! 956: p->forw->back = p->back; ! 957: } ! 958: ! 959: decref(p) ! 960: register struct node *p; ! 961: { ! 962: if (p && --p->refc <= 0) { ! 963: nrlab++; ! 964: delnode(p); ! 965: } ! 966: } ! 967: ! 968: struct node * ! 969: nonlab(ap) ! 970: struct node *ap; ! 971: { ! 972: register struct node *p; ! 973: ! 974: p = ap; ! 975: while (p && p->op==LABEL) ! 976: p = p->forw; ! 977: return(p); ! 978: } ! 979: ! 980: clearuse() { ! 981: register struct node **i; ! 982: for (i=uses+NUSE; i>uses;) *--i=0; ! 983: } ! 984: ! 985: clearreg() { ! 986: register short **i; ! 987: for (i=regs+NREG; i>regs;) **--i=0; ! 988: conloc[0] = 0; ccloc[0] = 0; ! 989: } ! 990: ! 991: savereg(ai, s, type) ! 992: register char *s; ! 993: { ! 994: register char *p, *sp; ! 995: ! 996: sp = p = regs[ai]; ! 997: if (source(s)) /* side effects in addressing */ ! 998: return; ! 999: /* if any indexing, must be parameter or local */ ! 1000: /* indirection (as in "*-4(fp)") is ok, however */ ! 1001: *p++ = type; ! 1002: while (*p++ = *s) ! 1003: if (*s=='[' || *s++=='(' && *s!='a' && *s!='f') {*sp = 0; return;} ! 1004: } ! 1005: ! 1006: dest(s,type) ! 1007: register char *s; ! 1008: { ! 1009: register int i; ! 1010: ! 1011: source(s); /* handle addressing side effects */ ! 1012: if ((i = isreg(s)) >= 0) { ! 1013: *(short *)(regs[i]) = 0; /* if register destination, that reg is a goner */ ! 1014: if (DOUBLE==(type&0xF) || DOUBLE==((type>>4)&0xF)) ! 1015: *(short *)(regs[i+1]) = 0; /* clobber two at once */ ! 1016: } ! 1017: for (i=NREG; --i>=0;) ! 1018: if (regs[i][1]=='*' && equstr(s, regs[i]+2)) ! 1019: *(short *)(regs[i]) = 0; /* previous indirection through destination is invalid */ ! 1020: while ((i = findrand(s,0)) >= 0) /* previous values of destination are invalid */ ! 1021: *(short *)(regs[i]) = 0; ! 1022: if (!natural(s)) {/* wild store, everything except constants vanishes */ ! 1023: for (i=NREG; --i>=0;) if (regs[i][1] != '$') *(short *)(regs[i]) = 0; ! 1024: conloc[0] = 0; ccloc[0] = 0; ! 1025: } else setcc(s,type); /* natural destinations set condition codes */ ! 1026: } ! 1027: ! 1028: splitrand(p) struct node *p; { ! 1029: /* separate operands at commas, set up 'regs' and 'lastrand' */ ! 1030: register char *p1, *p2; register char **preg; ! 1031: preg=regs+RT1; ! 1032: if (p1=p->code) while (*p1) { ! 1033: lastrand=p2= *preg++; ! 1034: while (*p1) if (','==(*p2++= *p1++)) {--p2; break;} ! 1035: *p2=0; ! 1036: } ! 1037: while (preg<(regs+RT1+5)) *(*preg++)=0; ! 1038: } ! 1039: ! 1040: compat(have, want) { ! 1041: register int hsrc, hdst; ! 1042: if (0==(want &= 0xF)) return(1); /* anything satisfies a wildcard want */ ! 1043: hsrc=have&0xF; if (0==(hdst=((have>>4)&0xF)) || hdst>=OP2) hdst=hsrc; ! 1044: if (want>=FLOAT) return(hdst==want && hsrc==want); ! 1045: /* FLOAT, DOUBLE not compat: rounding */ ! 1046: return(hsrc>=want && hdst>=want && hdst<FLOAT); ! 1047: } ! 1048: ! 1049: equtype(t1,t2) {return(compat(t1,t2) && compat(t2,t1));} ! 1050: ! 1051: findrand(as, type) ! 1052: char *as; ! 1053: { ! 1054: register char **i; ! 1055: for (i = regs+NREG; --i>=regs;) { ! 1056: if (**i && equstr(*i+1, as) && compat(**i,type)) ! 1057: return(i-regs); ! 1058: } ! 1059: return(-1); ! 1060: } ! 1061: ! 1062: isreg(s) ! 1063: register char *s; ! 1064: { ! 1065: if (*s++!='r' || !isdigit(*s++)) return(-1); ! 1066: if (*s==0) return(*--s-'0'); ! 1067: if (*(s-1)=='1' && isdigit(*s++) && *s==0) return(10+*--s-'0'); ! 1068: return(-1); ! 1069: } ! 1070: ! 1071: check() ! 1072: { ! 1073: register struct node *p, *lp; ! 1074: ! 1075: lp = &first; ! 1076: for (p=first.forw; p!=0; p = p->forw) { ! 1077: if (p->back != lp) ! 1078: abort(-1); ! 1079: lp = p; ! 1080: } ! 1081: } ! 1082: ! 1083: source(ap) ! 1084: char *ap; ! 1085: { ! 1086: register char *p1, *p2; ! 1087: ! 1088: p1 = ap; ! 1089: p2 = p1; ! 1090: if (*p1==0) ! 1091: return(0); ! 1092: while (*p2++ && *(p2-1)!='['); ! 1093: if (*p1=='-' && *(p1+1)=='(' ! 1094: || *p1=='*' && *(p1+1)=='-' && *(p1+2)=='(' ! 1095: || *(p2-2)=='+') { ! 1096: while (*p1 && *p1++!='r'); ! 1097: if (isdigit(*p1++)) ! 1098: if (isdigit(*p1)) *(short *)(regs[10+*p1-'0'])=0; ! 1099: else *(short *)(regs[*--p1-'0'])=0; ! 1100: return(1); ! 1101: } ! 1102: return(0); ! 1103: } ! 1104: ! 1105: newcode(p) struct node *p; { ! 1106: register char *p1,*p2,**preg; ! 1107: preg=regs+RT1; p2=line; ! 1108: while (*(p1= *preg++)) {while (*p2++= *p1++); *(p2-1)=',';} ! 1109: *--p2=0; ! 1110: p->code=copy(line); ! 1111: } ! 1112: ! 1113: repladdr(p) ! 1114: struct node *p; ! 1115: { ! 1116: register r; ! 1117: register char *p1, *p2; ! 1118: char **preg; int nrepl; ! 1119: ! 1120: preg=regs+RT1; nrepl=0; ! 1121: while (lastrand!=(p1= *preg++)) ! 1122: if (!source(p1) && 0<=(r=findrand(p1,p->subop))) { ! 1123: *p1++='r'; if (r>9) {*p1++='1'; r -= 10;} *p1++=r+'0'; *p1=0; ! 1124: nrepl++; nsaddr++; ! 1125: } ! 1126: if (nrepl) newcode(p); ! 1127: } ! 1128: ! 1129: /* movedat() ! 1130: /* { ! 1131: /* register struct node *p1, *p2; ! 1132: /* struct node *p3; ! 1133: /* register seg; ! 1134: /* struct node data; ! 1135: /* struct node *datp; ! 1136: /* ! 1137: /* if (first.forw == 0) ! 1138: /* return; ! 1139: /* datp = &data; ! 1140: /* for (p1 = first.forw; p1!=0; p1 = p1->forw) { ! 1141: /* if (p1->op == DATA) { ! 1142: /* p2 = p1->forw; ! 1143: /* while (p2 && p2->op!=TEXT) ! 1144: /* p2 = p2->forw; ! 1145: /* if (p2==0) ! 1146: /* break; ! 1147: /* p3 = p1->back; ! 1148: /* p1->back->forw = p2->forw; ! 1149: /* p2->forw->back = p3; ! 1150: /* p2->forw = 0; ! 1151: /* datp->forw = p1; ! 1152: /* p1->back = datp; ! 1153: /* p1 = p3; ! 1154: /* datp = p2; ! 1155: /* } ! 1156: /* } ! 1157: /* if (data.forw) { ! 1158: /* datp->forw = first.forw; ! 1159: /* first.forw->back = datp; ! 1160: /* data.forw->back = &first; ! 1161: /* first.forw = data.forw; ! 1162: /* } ! 1163: /* seg = -1; ! 1164: /* for (p1 = first.forw; p1!=0; p1 = p1->forw) { ! 1165: /* if (p1->op==TEXT||p1->op==DATA||p1->op==BSS) { ! 1166: /* if (p1->op == seg || p1->forw&&p1->forw->op==seg) { ! 1167: /* p1->back->forw = p1->forw; ! 1168: /* p1->forw->back = p1->back; ! 1169: /* p1 = p1->back; ! 1170: /* continue; ! 1171: /* } ! 1172: /* seg = p1->op; ! 1173: /* } ! 1174: /* } ! 1175: /* } ! 1176: */ ! 1177: ! 1178: redunbr(p) ! 1179: register struct node *p; ! 1180: { ! 1181: register struct node *p1; ! 1182: register char *ap1; ! 1183: char *ap2; ! 1184: ! 1185: if ((p1 = p->ref) == 0) ! 1186: return; ! 1187: p1 = nonlab(p1); ! 1188: if (p1->op==TST) { ! 1189: splitrand(p1); ! 1190: savereg(RT2, "$0", p1->subop); ! 1191: } else if (p1->op==CMP) ! 1192: splitrand(p1); ! 1193: else ! 1194: return; ! 1195: if (p1->forw->op==CBR) { ! 1196: ap1 = findcon(RT1, p1->subop); ! 1197: ap2 = findcon(RT2, p1->subop); ! 1198: p1 = p1->forw; ! 1199: if (compare(p1->subop, ap1, ap2)) { ! 1200: nredunj++; ! 1201: nchange++; ! 1202: decref(p->ref); ! 1203: p->ref = p1->ref; ! 1204: p->labno = p1->labno; ! 1205: #ifdef COPYCODE ! 1206: if (p->labno == 0) ! 1207: p->code = p1->code; ! 1208: if (p->ref) ! 1209: #endif ! 1210: p->ref->refc++; ! 1211: } ! 1212: } else if (p1->op==TST && equstr(regs[RT1],ccloc+1) && ! 1213: equtype(ccloc[0],p1->subop)) { ! 1214: p1=insertl(p1->forw); decref(p->ref); p->ref=p1; ! 1215: nrtst++; nchange++; ! 1216: } ! 1217: } ! 1218: ! 1219: char * ! 1220: findcon(i, type) ! 1221: { ! 1222: register char *p; ! 1223: register r; ! 1224: ! 1225: p = regs[i]; ! 1226: if (*p=='$') ! 1227: return(p); ! 1228: if ((r = isreg(p)) >= 0 && compat(regs[r][0],type)) ! 1229: return(regs[r]+1); ! 1230: if (equstr(p, conloc)) ! 1231: return(conval+1); ! 1232: return(p); ! 1233: } ! 1234: ! 1235: compare(op, acp1, acp2) ! 1236: char *acp1, *acp2; ! 1237: { ! 1238: register char *cp1, *cp2; ! 1239: register n1; ! 1240: int n2; int sign; ! 1241: ! 1242: cp1 = acp1; ! 1243: cp2 = acp2; ! 1244: if (*cp1++ != '$' || *cp2++ != '$') ! 1245: return(0); ! 1246: n1 = 0; sign=1; if (*cp2=='-') {++cp2; sign= -1;} ! 1247: while (isdigit(*cp2)) {n1 *= 10; n1 += (*cp2++ - '0')*sign;} ! 1248: n2 = n1; ! 1249: n1 = 0; sign=1; if (*cp1=='-') {++cp1; sign= -1;} ! 1250: while (isdigit(*cp1)) {n1 *= 10; n1 += (*cp1++ - '0')*sign;} ! 1251: if (*cp1=='+') ! 1252: cp1++; ! 1253: if (*cp2=='+') ! 1254: cp2++; ! 1255: do { ! 1256: if (*cp1++ != *cp2) ! 1257: return(0); ! 1258: } while (*cp2++); ! 1259: cp1 = n1; ! 1260: cp2 = n2; ! 1261: switch(op) { ! 1262: ! 1263: case JEQ: ! 1264: return(cp1 == cp2); ! 1265: case JNE: ! 1266: return(cp1 != cp2); ! 1267: case JLE: ! 1268: return(((int)cp1) <= ((int)cp2)); ! 1269: case JGE: ! 1270: return(((int)cp1) >= ((int)cp2)); ! 1271: case JLT: ! 1272: return(((int)cp1) < ((int)cp2)); ! 1273: case JGT: ! 1274: return(((int)cp1) > ((int)cp2)); ! 1275: case JLO: ! 1276: return(cp1 < cp2); ! 1277: case JHI: ! 1278: return(cp1 > cp2); ! 1279: case JLOS: ! 1280: return(cp1 <= cp2); ! 1281: case JHIS: ! 1282: return(cp1 >= cp2); ! 1283: } ! 1284: return(0); ! 1285: } ! 1286: ! 1287: setcon(cv, cl, type) ! 1288: register char *cv, *cl; ! 1289: { ! 1290: register char *p; ! 1291: ! 1292: if (*cv != '$') ! 1293: return; ! 1294: if (!natural(cl)) ! 1295: return; ! 1296: p = conloc; ! 1297: while (*p++ = *cl++); ! 1298: p = conval; ! 1299: *p++ = type; ! 1300: while (*p++ = *cv++); ! 1301: } ! 1302: ! 1303: equstr(p1, p2) ! 1304: register char *p1, *p2; ! 1305: { ! 1306: do { ! 1307: if (*p1++ != *p2) ! 1308: return(0); ! 1309: } while (*p2++); ! 1310: return(1); ! 1311: } ! 1312: ! 1313: setcc(ap,type) ! 1314: char *ap; ! 1315: { ! 1316: register char *p, *p1; ! 1317: ! 1318: p = ap; ! 1319: if (!natural(p)) { ! 1320: ccloc[0] = 0; ! 1321: return; ! 1322: } ! 1323: p1 = ccloc; ! 1324: *p1++ = type; ! 1325: while (*p1++ = *p++); ! 1326: } ! 1327: ! 1328: okio(p) register char *p; {/* 0->probable I/O space address; 1->not */ ! 1329: if (ioflag && (!natural(p) || 0>getnum(p))) return(0); ! 1330: return(1); ! 1331: } ! 1332: ! 1333: indexa(p) register char *p; {/* 1-> uses [r] addressing mode; 0->doesn't */ ! 1334: while (*p) if (*p++=='[') return(1); ! 1335: return(0); ! 1336: } ! 1337: ! 1338: natural(p) ! 1339: register char *p; ! 1340: {/* 1->simple local, parameter, global, or register; 0->otherwise */ ! 1341: if (*p=='*' || *p=='(' || *p=='-'&&*(p+1)=='(' || *p=='$'&&getnum(p+1)) ! 1342: return(0); ! 1343: while (*p++); ! 1344: p--; ! 1345: if (*--p=='+' || *p==']' || *p==')' && *(p-2)!='a' && *(p-2)!='f') ! 1346: return(0); ! 1347: return(1); ! 1348: } ! 1349: ! 1350: /* ! 1351: ** Tell if an argument is most likely static. ! 1352: */ ! 1353: ! 1354: isstatic(cp) ! 1355: register char *cp; ! 1356: { ! 1357: if (*cp == '_' || *cp == 'L' || (*cp++ == 'v' && *cp == '.')) ! 1358: return (1); ! 1359: return (0); ! 1360: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.