Annotation of 43BSDTahoe/lib/old_compiler/c2/c2.tahoe/save/c21.c, revision 1.1.1.1

1.1       root        1: #ifndef lint
                      2: static char sccsid[] = "%W% (Berkeley/CCI) %G%";
                      3: #endif
                      4: 
                      5: /*
                      6:  * C object code improver-- second part
                      7:  */
                      8: 
                      9: #include "c2.h"
                     10: #include <stdio.h>
                     11: #include <ctype.h>
                     12: 
                     13: int bitsize[] = {0,8,16,32,64,32,64}; /* index by type codes */
                     14: 
                     15: #define        SWAP(t,a,b)     { t x = (a); (a) = (b); (b) = x; }
                     16: /*
                     17:  * Check for 3 addr instr which should be 2 addr
                     18:  */
                     19: redun3(p)
                     20:        register struct node *p;
                     21: {
                     22:        if (has3ops(p)) {
                     23:                if (equstr(regs[RT1],regs[RT3]) &&
                     24:                   (p->op==ADD || p->op==MUL || p->op==AND || p->op==OR ||
                     25:                    p->op==XOR))
                     26:                        SWAP(char *,regs[RT1],regs[RT2]);
                     27:                if (equstr(regs[RT2],regs[RT3])) {
                     28:                        p->subop=(p->subop&0xF)|(OP2<<4); p->pop=0;
                     29:                        lastrand=regs[RT2]; *regs[RT3]=0;
                     30:                        return (1);
                     31:                }
                     32:        }
                     33:        return (0);
                     34: }
                     35: 
                     36: #define        skipto(cp,c)    while (*(cp)++ != (c))
                     37: #define        scopy(dst,src)  while (*(dst)++ = *(src)++)
                     38: 
                     39: bmove()
                     40: {
                     41:        register struct node *p, *lastp;
                     42:        register char *cp1,*cp2;
                     43:        register int r;
                     44:        struct node *flops();
                     45: 
                     46:        refcount();
                     47:        for (p=lastp= &first; p=p->forw; lastp=p)
                     48:                ;
                     49:        clearreg(); clearuse();
                     50:        for (p=lastp; p!= &first; p=p->back) {
                     51:        if (debug) {
                     52:                printf("Uses: ");
                     53:                if (useacc)
                     54:                        printf("acc: %s\n",useacc->code? useacc->code:"");
                     55:                for (r=NUSE;--r>=0;) if (uses[r])
                     56:                        printf("%d: %s\n",r,uses[r]->code? uses[r]->code:"");
                     57:                printf("-\n");
                     58:        }
                     59:        r=(p->subop>>4)&0xF;
                     60:        splitrand(p);
                     61:        if (OP3==r && redun3(p))
                     62:                newcode(p), redunm++;
                     63:        /* ops that do nothing */
                     64:        if (p->op==MOV && equstr(regs[RT1], regs[RT2])) {
                     65:                if (p->forw->op!=CBR) {
                     66:                        delnode(p), redunm++;
                     67:                        continue;
                     68:                } else {
                     69:                        p->op=TST; p->pop=0;
                     70:                        skipto(p->code, ',');
                     71:                        redunm++;
                     72:                }
                     73:        } else if ((cp1=p->code, *cp1++)=='$' &&
                     74:         (*cp1=='0' || *cp1=='1' || *cp1++=='-' && *cp1=='1') && cp1[1]==',') {
                     75:                switch ((p->code[1]<<8)|ord(p->op)) {
                     76:                case (('0'<<8)|ord(ADD)):
                     77:                case (('0'<<8)|ord(SUB)):
                     78:                case (('-'<<8)|ord(AND)):
                     79:                case (('0'<<8)|ord(OR)):
                     80:                case (('0'<<8)|ord(XOR)):
                     81:                case (('1'<<8)|ord(MUL)):
                     82:                case (('1'<<8)|ord(DIV)):
                     83:                case (('0'<<8)|ord(SHAL)):
                     84:                case (('0'<<8)|ord(SHAR)):
                     85:                case (('0'<<8)|ord(SHL)):
                     86:                case (('0'<<8)|ord(SHR)):
                     87:                        if (r == OP2) {
                     88:                                if (p->forw->op!=CBR) {
                     89:                                        delnode(p), redunm++;
                     90:                                        continue;
                     91:                                } else {
                     92:                                        p->op=TST; p->subop&=0xF; p->pop=0;
                     93:                                        skipto(p->code, ',');
                     94:                                        redunm++;
                     95:                                }
                     96:                        } else {        /* OP3 or shift */
                     97:                                p->op=MOV; p->subop&=0xF; p->pop=0;
                     98:                                skipto(p->code, ',');
                     99:                                p = p->forw; redunm++;
                    100:                                continue;
                    101:                        }
                    102:                        break;
                    103:                case (('0'<<8)|ord(MUL)):
                    104:                case (('0'<<8)|ord(AND)):
                    105:                        p->op=CLR; p->subop&=0xF; p->pop=0;
                    106:                        skipto(p->code, ',');
                    107:                        if (r == OP3)
                    108:                                skipto(p->code, ',');
                    109:                        redunm++;
                    110:                }
                    111:        }
                    112:        switch (p->op) {
                    113:        case LABEL: case DLABEL:
                    114:                for (r=NUSE; --r>=0;)
                    115:                        if (lastuse(r))
                    116:                                p->ref=(struct node *) (((int)p->ref)|(1<<r));
                    117:                if (useacc)
                    118:                        p->ref=(struct node *) (((int)p->ref)|(1<<NUSE));
                    119:                break;
                    120:        case CALLS:
                    121:        case CALLF:
                    122:                clearuse(); goto std;
                    123:        case NIL:
                    124:                clearuse(); break;
                    125:        case CVT: {
                    126:                long n;
                    127:                if ((p->subop&0xF)!=LONG)
                    128:                        goto std;
                    129:                cp1=p->code;
                    130:                if (*cp1++!='$')
                    131:                        goto std;
                    132:                splitrand(p);
                    133:                n = getnum(&regs[RT1][1]);
                    134:                if (r==BYTE && (n<-128 || n>127))
                    135:                        goto std;
                    136:                if (r==WORD && (n<-32768 || n>32767))
                    137:                        goto std;
                    138:                p->op = MOV; p->subop = r; p->pop = 0;
                    139:                goto std;
                    140:        }
                    141: 
                    142:        case SUB:
                    143:                if ((p->subop&0xF)!=LONG)
                    144:                        goto std;
                    145:                cp1=p->code;
                    146:                if (*cp1++!='$')
                    147:                        goto std;
                    148:                splitrand(p);
                    149: #ifdef MOVAFASTER
                    150:                if (equstr(regs[RT2],"fp") && !indexa(regs[RT1])) {/* addr comp. */
                    151:                        char buf[C2_ASIZE]; cp2=buf; *cp2++='-'; 
                    152:                        cp1=regs[RT1]+1; scopy(cp2,cp1); --cp2;
                    153:                        cp1="(fp),"; scopy(cp2,cp1); --cp2;
                    154:                        cp1=regs[RT3]; scopy(cp2,cp1);
                    155:                        p->code=copy(buf); p->op = MOVA; p->subop = BYTE; p->pop=0;
                    156:                } else
                    157: #endif MOVAFASTER
                    158:                if (*cp1++=='-' && 0==(r=getnum(cp1)) && !checkexpr(cp1)) {
                    159:                        p->op=ADD; p->pop=0;
                    160:                        *--cp1='$'; p->code=cp1;
                    161:                }
                    162:                goto std;
                    163:        case ADD:
                    164:                if ((p->subop&0xF)!=LONG)
                    165:                        goto std;
                    166:                cp1=p->code;
                    167:                if (*cp1++!='$')
                    168:                        goto std;
                    169:                splitrand(p);
                    170:                if (isstatic(cp1) && tempreg(regs[RT2],r) && lastuse(r) == p->forw) {
                    171:                        /*
                    172:                         * address comp:
                    173:                         *      addl2   $_foo,r0  \     movab   _foo[r0],bar
                    174:                         *      movl    r0,bar    /
                    175:                         */
                    176:                        register struct node    *pnext = p->forw;
                    177:                        char    buf[C2_ASIZE];
                    178: 
                    179:                        if (pnext->op == MOV && pnext->subop == LONG) {
                    180:                                cp1 = &regs[RT1][1]; cp2 = &buf[0];
                    181:                                scopy(cp2,cp1); cp2--;
                    182:                                splitrand(pnext);
                    183:                                if (r == isreg(regs[RT1])) {
                    184:                                        delnode(p); p = pnext;
                    185:                                        p->op = MOVA; p->subop = BYTE;
                    186:                                        p->pop = 0;
                    187:                                        cp1 = regs[RT1]; *cp2++ = '[';
                    188:                                        scopy(cp2,cp1); cp2--;
                    189:                                        *cp2++ = ']'; *cp2++ = ',';
                    190:                                        cp1 = regs[RT2];
                    191:                                        scopy(cp2,cp1);
                    192:                                        p->code = copy(buf);
                    193:                                }
                    194:                        }
                    195:                }
                    196:                else
                    197: #ifdef MOVAFASTER
                    198:                if (equstr(regs[RT2],"fp") && !indexa(regs[RT1])) {/* address comp. */
                    199:                        cp2=cp1-1; cp1=regs[RT1]+1; scopy(cp2,cp1); --cp2;
                    200:                        cp1="(fp)"; scopy(cp2,cp1); *--cp2=',';
                    201:                        p->op = MOVA; p->subop = BYTE; p->pop=0;
                    202:                } else
                    203: #endif MOVAFASTER
                    204:                if (*cp1++=='-' && 0==(r=getnum(cp1)) && !checkexpr(cp1)) {
                    205:                        p->op=SUB; p->pop=0;
                    206:                        *--cp1='$'; p->code=cp1;
                    207:                }
                    208:                /* fall thru ... */
                    209:        case CASE:
                    210:        default: std:
                    211:                p=bflow(p);
                    212:                break;
                    213:        case MUL:
                    214:                /*
                    215:                 * Change multiplication
                    216:                 * by constant powers of 2 to shifts.
                    217:                 */
                    218:                splitrand(p);
                    219:                if (regs[RT1][0] != '$' || regs[RT1][1] == '-')
                    220:                        goto std;
                    221:                if ((r = ispow2(getnum(&regs[RT1][1]))) <= 0)
                    222:                        goto std;
                    223:                /* mull2 $2,x */
                    224:                if (r == 1 && p->subop == U(LONG, OP2)) {
                    225:                        strcpy(regs[RT1], regs[RT2]);
                    226:                        p->op = ADD; p->pop = 0; newcode(p);
                    227:                        goto std;
                    228:                }
                    229:                if (p->subop == U(LONG,OP2))
                    230:                        strcpy(regs[RT3], regs[RT2]);
                    231:                sprintf(regs[RT1], "$%d", r);
                    232:                p->op = SHL; p->subop = LONG;
                    233:                p->pop = 0; newcode(p);
                    234:                goto std;
                    235: 
                    236:        case SHAL:
                    237:        case SHL: {
                    238:                /*
                    239:                 * bit tests:
                    240:                 *      shll    A,$1,rC    \
                    241:                 *      bitl    B,rC        >   jbc     A,B,D
                    242:                 *      jeql    D          /
                    243:                 *
                    244:                 * address comp:
                    245:                 *      shll    $1,bar,r0  \    movl    bar,r0
                    246:                 *      movab   _foo[r0]   /    movaw   _foo[r0]
                    247:                 *
                    248:                 *      shll    $2,r0,r0   \    moval   _foo[r0]
                    249:                 *      movab   _foo[r0]   /
                    250:                 */
                    251:                register struct node *pf, *pn;
                    252:                register int shfrom, shto;
                    253:                long shcnt;
                    254:                char *regfrom;
                    255: 
                    256:                splitrand(p);
                    257:                if (regs[RT1][0] != '$') {
                    258:                        if (isreg(regs[RT1]) < 0 || regs[RT2][0] != '$' ||
                    259:                            getnum(&regs[RT2][1]) != 1 ||
                    260:                            !tempreg(regs[RT3],r))
                    261:                                goto std;
                    262:                        if ((pf = p->forw)->op != BIT && pf->op!=AND)
                    263:                                goto std;
                    264:                        if (lastuse(r) && lastuse(r) != pf)
                    265:                                goto std;
                    266:                        splitrand(pf);
                    267:                        if (r == isreg(regs[RT1]))
                    268:                                cp2 = regs[RT2];
                    269:                        else if (r == isreg(regs[RT2]))
                    270:                                cp2 = regs[RT1];
                    271:                        else
                    272:                                goto std;
                    273:                        if (*cp2 == '$' || (pn = pf->forw)->op != CBR ||
                    274:                            (pn->subop != JEQ && pn->subop != JNE))
                    275:                                goto std;
                    276:                        delnode(p); delnode(pf);
                    277:                        pn->subop = (pn->subop == JEQ) ? JBC : JBS;
                    278:                        cp1=p->code; skipto(cp1,','); scopy(cp1,cp2);
                    279:                        pn->code = p->code; pn->pop = NULL;
                    280:                        setuse(r,NULL);
                    281:                        nbj++;
                    282:                        p = pn;
                    283:                        goto std;
                    284:                }
                    285:                if ((shcnt = getnum(&regs[RT1][1])) < 1 || shcnt > 2)
                    286:                        goto std;
                    287:                if ((shfrom = isreg(regs[RT2])) >= 0)
                    288:                        regfrom = copy(regs[RT2]);
                    289:                if (tempreg(regs[RT3],shto)) {
                    290:                        int regnum;
                    291: 
                    292:                        if (lastuse(shto) != (pf = p->forw) ||
                    293:                            (pf->op != MOVA && pf->op != PUSHA) ||
                    294:                            pf->subop != BYTE)
                    295:                                goto ashadd;
                    296:                        splitrand(pf);
                    297:                        if (!indexa(regs[RT1]))
                    298:                                goto std;
                    299:                        cp2 = regs[RT1];
                    300:                        if (!isstatic(cp2))
                    301:                                goto std;
                    302:                        skipto(cp2,'[');
                    303:                        if (*cp2++ != 'r' || !isdigit(*cp2))
                    304:                                goto std;
                    305:                        regnum = *cp2++ - '0';
                    306:                        if (isdigit(*cp2)) {
                    307:                                if (cp2[1] != ']')
                    308:                                        goto std;
                    309:                                regnum *= 10; regnum += *cp2 - '0';
                    310:                        }
                    311:                        if (regnum != shto)
                    312:                                goto std;
                    313:                        if (shfrom >= 0) {      /* shll $N,r*,r0 */
                    314:                                delnode(p);
                    315:                                p = pf;
                    316:                                if (shfrom != shto) {
                    317:                                        setuse(shto,NULL); splitrand(pf);
                    318:                                        cp2=regs[RT1]; skipto(cp2,'[');
                    319:                                        cp1=regfrom; scopy(cp2,cp1);
                    320:                                        cp2[-1] = ']'; *cp2 = 0;
                    321:                                        newcode(pf);
                    322:                                }
                    323:                        } else {
                    324:                                p->op = MOV; splitrand(p);
                    325:                                strcpy(regs[RT1], regs[RT2]);
                    326:                                strcpy(regs[RT2], regs[RT3]);
                    327:                                regs[RT3][0] = '\0';
                    328:                                p->pop = 0; newcode(p);
                    329:                        }
                    330:                        switch (shcnt) {
                    331:                        case 1: pf->subop = WORD; break;
                    332:                        case 2: pf->subop = LONG; break;
                    333:                        }
                    334:                        redunm++; nsaddr++;
                    335:                }
                    336:                goto std;
                    337: ashadd:
                    338:                /* at this point, RT2 and RT3 are guaranteed to be simple regs*/
                    339:                if (shcnt == 1) {
                    340:                        /*
                    341:                         * quickie:
                    342:                         *      shll    $1,A,A  >       addl2   A,A
                    343:                         *      shll    $1,A,B  >       addl3   A,A,B
                    344:                         */
                    345:                        p->op = ADD;
                    346:                        strcpy(regs[RT1], regs[RT2]);
                    347:                        if (equstr(regs[RT2], regs[RT3])) {
                    348:                                p->subop = U(LONG,OP2);
                    349:                                regs[RT3][0] = '\0';
                    350:                        } else
                    351:                                p->subop = U(LONG,OP3);
                    352:                        p->pop = 0;
                    353:                        newcode(p);
                    354:                }
                    355:                goto std;
                    356:        }
                    357: 
                    358:        case SHAR:
                    359:        case SHR: {
                    360:                /*
                    361:                 * bit tests:
                    362:                 *      shrl    A,B,rC     \
                    363:                 *      bitl    $1,rC       >   jbc     A,B,D
                    364:                 *      jeql    D          /
                    365:                 */
                    366:                register struct node    *pf;    /* forward node */
                    367:                register struct node    *pn;    /* next node (after pf) */
                    368:                register int    extreg;         /* reg extracted to */
                    369: 
                    370:                splitrand(p);
                    371:                if (isreg(regs[RT1]) < 0 || !tempreg(regs[RT3],extreg) ||
                    372:                    (pf = p->forw)->op != BIT ||
                    373:                    (lastuse(extreg) && lastuse(extreg) != pf))
                    374:                        goto std;
                    375:                splitrand(pf);
                    376:                if (regs[RT1][0] != '$' || getnum(&regs[RT1][1]) != 1 ||
                    377:                    extreg != isreg(regs[RT2]) ||
                    378:                    (pn = pf->forw)->op !=CBR ||
                    379:                    (pn->subop != JEQ && pn->subop != JNE))
                    380:                        goto std;
                    381:                delnode(p); delnode(pf);
                    382:                pn->subop = (pn->subop == JEQ) ? JBC : JBS;
                    383:                cp1 = p->code; skipto(cp1, ','); skipto(cp1,','); *cp1 = '\0';
                    384:                pn->code = p->code; pn->pop = NULL;
                    385:                setuse(extreg,NULL);
                    386:                nbj++; p = pn;
                    387:                goto std;
                    388:        }
                    389: 
                    390:        case AND: {
                    391:                /*
                    392:                 * unsigned conversion:
                    393:                 *      cvtbl   A,B;    andl2   $255,B > movzbl A,B
                    394:                 * also byte- and word-size fields:
                    395:                 *  shrl $(3-n)*8,A,B; andl2 $255,B    >        movzbl  n+A,B
                    396:                 *  shrl $(1-n)*16,A,B; andl2 $65535,B >        movzwl  n+A,B
                    397:                 */
                    398:                char src[C2_ASIZE];
                    399:                register int f; /* field length */
                    400:                register struct node    *pb = p->back;  /* backward node */
                    401: 
                    402:                if (p->subop != U(LONG,OP2))
                    403:                        goto std;
                    404:                splitrand(p); cp1=regs[RT1];
                    405:                if (*cp1++!='$' || (f=getnum(cp1))!=0xff && f!=0xffff)
                    406:                        goto std;
                    407:                f = f==0xff ? 8 : 16;
                    408:                if (pb->op!=CVT && pb->op!=MOVZ && pb->op!=SHAR && pb->op!=SHR)
                    409:                        goto std;
                    410:                /* save source of ANDL in 'src' */
                    411:                strcpy(src, regs[RT2]);
                    412:                splitrand(pb);
                    413:                if (!equstr(src,lastrand))
                    414:                        goto std;
                    415:                if (pb->op==CVT || pb->op==MOVZ) {
                    416:                        if (!(bitsize[pb->subop&0xF]==f &&
                    417:                             bitsize[pb->subop>>4]>=f)) /* good CVT */
                    418:                                goto std;
                    419:                        strcpy(src, regs[RT1]);
                    420:                } else {
                    421:                        register int boff;      /* bit offset */
                    422: 
                    423:                        if (regs[RT1][0] != '$' ||
                    424:                            (boff = getnum(&regs[RT1][1])) < 0 ||
                    425:                            isreg(regs[RT2])>=0 || !natural(regs[RT2]) ||
                    426:                            (boff & (f-1)) != 0)
                    427:                                goto std;
                    428:                        boff = (32-boff-f) / 8;
                    429:                        if (boff == 0)
                    430:                                strcpy(src, regs[RT2]);
                    431:                        else
                    432:                                sprintf(src, "%d%s%s", boff,
                    433:                                    regs[RT2][0]=='(' ? "":"+", regs[RT2]);
                    434:                }
                    435:                delnode(pb);
                    436:                p->op = MOVZ;
                    437:                p->subop = U((f==8 ? BYTE : WORD), LONG);
                    438:                sprintf(line,"%s,%s",src,lastrand);
                    439:                p->pop=0;
                    440:                p->code = copy(line);
                    441:                goto std;
                    442:        }
                    443: 
                    444:        case CMP: {
                    445:                /*
                    446:                 * comparison to -63 to -1:
                    447:                 *      cmpl    r0,$-1  >       incl    r0
                    448:                 *      jeql    ...
                    449:                 *
                    450:                 *      cmpl    r0,$-63 >       addl2   $63,r0
                    451:                 *      jeql    ...
                    452:                 */
                    453:                register int num, reg;
                    454:                register struct node *regp = p->back;
                    455: 
                    456:                if (p->forw->op != CBR ||
                    457:                    (p->forw->subop != JEQ && p->forw->subop != JNE))
                    458:                        goto std;
                    459:                splitrand(p);
                    460:                if (strncmp(regs[RT2], "$-", 2) != 0)
                    461:                        goto std;
                    462:                reg = r = isreg(regs[RT1]);
                    463:                if (r < 0 || (r < NUSE && lastuse(r) != 0))
                    464:                        goto std;
                    465:                if (r >= NUSE && regp->op == MOV && p->subop == regp->subop) {
                    466:                        if (*regp->code != 'r')
                    467:                                goto std;
                    468:                        reg = regp->code[1] - '0';
                    469:                        if (isdigit(regp->code[2]) || reg >= NUSE ||
                    470:                            lastuse(reg) != 0)
                    471:                                goto std;
                    472:                }
                    473:                if (r >= NUSE)
                    474:                        goto std;
                    475:                if (reg != r)
                    476:                        sprintf(regs[RT1], "r%d", reg);
                    477:                if ((num = getnum(&regs[RT2][2])) <= 0 || num > 63)
                    478:                        goto std;
                    479:                if (num == 1) {
                    480:                        p->op = INC; regs[RT2][0] = '\0';
                    481:                } else {
                    482:                        register char   *t;
                    483: 
                    484:                        SWAP(char *,regs[RT1],regs[RT2]);
                    485:                        p->op = ADD; p->subop = U(p->subop, OP2);
                    486:                        for (t = &regs[RT1][2]; t[-1] = *t; t++)
                    487:                                ;
                    488:                }
                    489:                p->pop = 0; newcode(p);
                    490:                goto std;
                    491:        }
                    492: 
                    493:        case JBR: case JMP:
                    494:                clearuse();
                    495:                if ((p->subop&0xF)==RET) {
                    496:                        switch ((p->subop>>4)&0xF) {
                    497:                                case 2: setuse(1,p); regs[1][0]= -1;
                    498:                                case 1: setuse(0,p); regs[0][0]= -1;
                    499:                        }
                    500:                        break;
                    501:                }
                    502:                if (p->ref==0)                  /* jmp (r0) */
                    503:                        goto std;
                    504:                /* fall through */
                    505:        case CBR:
                    506:                if (p->ref->ref!=0) {
                    507:                        for (r=NUSE;--r>=0;)
                    508:                                if ((1<<r) & (int)p->ref->ref) {
                    509:                                        setuse(r,p); regs[r][0]= -1;
                    510:                                }
                    511:                        if ((1<<NUSE) & (int)p->ref->ref)
                    512:                                useacc=p;
                    513:                }
                    514:                break;
                    515:        case LNF: {
                    516:                /* lnf a; addf b ==> ldf b; subf a */
                    517:                register struct node *pf = p->forw;
                    518: 
                    519:                if (pf->op!=ADDF || p->subop!=pf->subop)
                    520:                        break;
                    521:                p->op = LDF;
                    522:                p->pop = 0;
                    523:                pf->op = SUBF;
                    524:                pf->pop = 0;
                    525:                cp1 = p->code;
                    526:                p->code = pf->code;
                    527:                pf->code = cp1;
                    528:                p = pf->forw;
                    529:                break;
                    530:        }
                    531:        case LDF: case LDFD: case CVLF: /* destroy acc */
                    532:                useacc = 0;
                    533:                goto std;
                    534:        case STF: {
                    535:                register struct node *pf;
                    536: 
                    537:                if ((pf=flops(p)) != p) {
                    538:                        p = pf; /* usually p->forw; */
                    539:                        break;
                    540:                }
                    541:                if (ldmov(p)) {
                    542:                        p = p->forw;
                    543:                        break;
                    544:                }
                    545:                if (useacc == 0)
                    546:                        useacc = p;
                    547:                goto std;
                    548:        }
                    549:        case ADDF: case MULF: { /* commutatives - create clients for flops */
                    550:                /* stf a; ldf b; addf a => stf a; ldf a; addf b */
                    551:                register struct node *pb = p->back;
                    552:                register struct node *pbb = pb->back;
                    553: 
                    554:                if (pb->op==LDF && pb->subop==p->subop &&
                    555:                   pbb && pbb->op==STF && pbb->subop==p->subop &&
                    556:                   equstr(pbb->code, p->code)) {
                    557:                        cp1 = p->code;
                    558:                        p->code = pb->code;
                    559:                        pb->code = cp1;
                    560:                }
                    561:        }
                    562:        /* use acc and regs */
                    563:        case CMPF: case CVFL: case SUBF: case DIVF:
                    564:                if (useacc == 0)
                    565:                        useacc = p;
                    566:                goto std;
                    567:        case TSTF: 
                    568:                break;
                    569:        case PUSHD:
                    570:                if (ldmov(p)) {
                    571:                        p = p->forw;
                    572:                        break;
                    573:                }
                    574:        case CVDF: case NEGF: /* use only acc */
                    575:        case SINF: case COSF: case ATANF: case LOGF: case SQRTF: case EXPF:
                    576:                if (useacc == 0)
                    577:                        useacc = p;
                    578:        case EROU: case JSW:
                    579:        case TEXT: case DATA: case BSS: case ALIGN: case WGEN: case END: ;
                    580:        }
                    581:        }
                    582:        for (p= &first; p!=0; p=p->forw)
                    583:                if (p->op==LABEL || p->op==DLABEL)
                    584:                        p->ref=0;       /* erase our tracks */
                    585: }
                    586: 
                    587: /*
                    588:  * Return pointer to register which is
                    589:  * "beyond last read/modify operand"
                    590:  */
                    591: char *
                    592: byondrd(p)
                    593:        register struct node *p;
                    594: {
                    595:        if (has2ops(p))
                    596:                return (regs[RT3]);
                    597:        switch (p->op) {
                    598:        case MFPR:
                    599:        case PUSHA:
                    600:        case TST: case INC: case DEC: case PUSH:
                    601:        case LDF: case LNF: case CVLF: case LDFD:
                    602:        case ADDF: case SUBF: case MULF: case DIVF:
                    603:        case CMPF:
                    604:                return (regs[RT2]);
                    605:        case MTPR:
                    606: #ifndef EMOD
                    607:        case EDIV:
                    608: #endif EMOD
                    609:        case CBR: /* must be JBC/JBS */
                    610:        case BIT: case CMP: case CALLS: case CALLF:
                    611:        case CMPF2:
                    612:                return (regs[RT3]);
                    613:        case EMUL:
                    614:        case PROBE:
                    615:        case MOVBLK:
                    616:        case CASE: 
                    617:                return (regs[RT4]);
                    618:        }
                    619:        return (lastrand);
                    620: }
                    621: 
                    622: struct node *
                    623: bflow(p)
                    624:        register struct node *p;
                    625: {
                    626:        register char *cp1,*cp2,**preg;
                    627:        register int r, fr, dblflg=0;
                    628:        int flow= -1;
                    629:        struct node *olduse=0, *olduse1=0;
                    630: 
                    631:        if (p->subop==QUAD || p->subop==DOUBLE || (p->subop&0xF0)==DOUBLE<<4)
                    632:                dblflg |= 1;    /* double dest */
                    633:        if ((p->subop&0xF)==DOUBLE || p->subop==QUAD)
                    634:                dblflg |= 2;    /* double src */
                    635:        splitrand(p);
                    636:        if (p->op!=PUSH &&
                    637: #ifndef EMOD
                    638:        p->op!=EDIV &&
                    639: #endif EMOD
                    640:        p->op!=EMUL &&
                    641:        p->subop && tempreg(lastrand,r) && lastuse(r) == p->forw) {
                    642:        if (equtype(p->subop,regs[r][0]) ||
                    643:            ((p->op==CVT || p->op==MOVZ || p->op==CVFL) &&
                    644:             (regs[r][0]&0xf) && compat((p->subop>>4)&0xf,regs[r][0])) ||
                    645:            p->op==MOVA && compat(LONG, regs[r][0])) {
                    646:                register int r2;
                    647: 
                    648:                if (regs[r][1]!=0) {    /* send directly to destination */
                    649:                        if (p->op==INC || p->op==DEC) {
                    650:                                p->op = (p->op==DEC) ? SUB : ADD;
                    651:                                /* use 2 now, convert to 3 later */
                    652:                                p->subop=(OP2<<4)+(p->subop&0xF);
                    653:                                p->pop=0;
                    654:                                cp1=lastrand; cp2=regs[RT2];
                    655:                                scopy(cp2,cp1);         /* copy reg */
                    656:                                cp1=lastrand; *cp1++='$'; *cp1++='1'; *cp1=0;
                    657:                        }
                    658:                        cp1=regs[r]+1; cp2=lastrand;
                    659:                        if (has2ops(p)) {
                    660:                                /* use 3 operand form of instruction */
                    661:                                p->pop=0;
                    662:                                p->subop += (OP3-OP2)<<4;
                    663:                                lastrand = cp2 = regs[RT3];
                    664:                        }
                    665:                        scopy(cp2,cp1);
                    666:                        if (p->op==MOVA && p->forw->op==PUSH) {
                    667:                                p->op=PUSHA;
                    668:                                *regs[RT2]=0; p->pop=0;
                    669:                        } else if ((p->op==MOV || p->op==CVT) &&
                    670:                            p->forw->op==PUSH) {
                    671:                                p->op=PUSH; p->subop &= 0xF;
                    672:                                *regs[RT2]=0; p->pop=0;
                    673:                        }
                    674:                        delnode(p->forw);
                    675:                        if (tempreg(lastrand,r2)) {
                    676:                                setuse(r2, lastuse(r));
                    677:                                setuse(r,NULL);
                    678:                        }
                    679:                        redun3(p);
                    680:                        newcode(p); redunm++; flow=r;
                    681:                } else if (p->op==MOV) {        /* superfluous fetch */
                    682:                        int nmatch;
                    683:                        char src[C2_ASIZE];
                    684:        movit:
                    685:                        cp2=src; cp1=regs[RT1]; scopy(cp2,cp1);
                    686:                        splitrand(p->forw);
                    687:                        if (p->forw->op != INC && p->forw->op != DEC)
                    688:                                lastrand=byondrd(p->forw);
                    689:                        nmatch=0;
                    690:                        for (preg=regs+RT1;*preg!=lastrand;preg++)
                    691:                                if (r==isreg(*preg)) {
                    692:                                        cp2= *preg; cp1=src;
                    693:                                        scopy(cp2,cp1);
                    694:                                        ++nmatch;
                    695:                                }
                    696:                        if (nmatch==1) {
                    697:                                if (has2ops(p->forw) && equstr(src,regs[RT2])) {
                    698:                                        p->forw->pop=0;
                    699:                                        p->forw->subop += (OP3-OP2)<<4;
                    700:                                        cp1=regs[RT3];
                    701:                                        *cp1++ = 'r'; *cp1++ = r+'0'; *cp1=0;
                    702:                                }
                    703:                                delnode(p);
                    704:                                p=p->forw;
                    705:                                if (tempreg(src,r2)) {
                    706:                                        setuse(r2,lastuse(r));
                    707:                                        setuse(r,NULL);
                    708:                                }
                    709:                                redun3(p);
                    710:                                newcode(p); redunm++; flow=r;
                    711:                        } else
                    712:                                splitrand(p);
                    713:                }
                    714:        } else if (p->op==MOV && (p->forw->op==CVT || p->forw->op==MOVZ) &&
                    715:            p->forw->subop&0xf &&       /* if base or index, then forget it */
                    716:            compat(p->subop,p->forw->subop) && !indexa(cp1=regs[RT1]))
                    717:                goto movit;
                    718:        }
                    719:        /* adjust 'lastrand' past any 'read' or 'modify' operands. */
                    720:        lastrand=byondrd(p);
                    721:        /* a 'write' clobbers the register. */
                    722:        if (tempreg(lastrand,r) ||
                    723:            (has2ops(p) && tempreg(regs[RT2],r) && lastuse(r)==0)) {
                    724:                /*
                    725:                 * Writing a dead register is useless,
                    726:                 * but watch side effects
                    727:                 */
                    728:                switch (p->op) {
                    729: #ifndef EMOD
                    730:                case EDIV:
                    731: #endif EMOD
                    732:                case EMUL:
                    733:                case AOBLEQ: case AOBLSS:
                    734:                        break;
                    735:                default:
                    736:                        /*
                    737:                         * If no direct uses, check for
                    738:                         * use of condition codes
                    739:                         */
                    740:                        if (lastuse(r)==0 &&
                    741:                            ((dblflg&1)==0 || lastuse(r+1)==0)) {
                    742:                                register struct node *q = p;
                    743: 
                    744:                                while ((q = nonlab(q->forw))->op==JBR &&
                    745:                                    q->subop==0)
                    746:                                        q=q->ref; /* cc unused, unchanged */
                    747:                                if (q->op!=CBR && q->op!=ADDA && q->op!=SUBA) {
                    748:                                        /* ... and destroyed */
                    749:                                        preg=regs+RT1;
                    750:                                        while (cp1 = *preg++) {
                    751:                                                if (cp1==lastrand &&
                    752:                                                    p->op != CLR &&
                    753:                                                    p->op != CVFL) {
                    754:                                                        redunm++;
                    755:                                                        delnode(p);
                    756:                                                        return (p->forw);
                    757:                                                }
                    758:                                                if (equstr(cp1,lastrand))
                    759:                                                        break;
                    760:                                        }
                    761:                                }
                    762:                        }
                    763:                        flow=r;
                    764:                }
                    765:        }
                    766:        if ((r=flow) >= 0) {
                    767:                olduse = lastuse(r); setuse(r,NULL);
                    768:                *(short *)(regs[r])=0;
                    769:                /* if r0 destroyed, dont keep r1 */
                    770:                if (dblflg&1) {
                    771:                        olduse1 = lastuse(++r); setuse(r,NULL);
                    772:                        *(short *)(regs[r])=0;
                    773:                }
                    774:        }
                    775:        /* now look for 'read' or 'modify' (read & write) uses */
                    776:        preg=regs+RT1; 
                    777:        while (*(cp1= *preg++)) {
                    778:                /* check for  r  */
                    779:                if (lastrand!=cp1 && tempreg(cp1,r) && lastuse(r)==0) {
                    780:                        setuse(r,p);
                    781:                        cp2=regs[r]; *cp2++=p->subop;
                    782:                        if ((p->op==SHAL || p->op==SHAR ||
                    783:                            p->op==SHL || p->op==SHR) &&
                    784:                            cp1==regs[RT1])
                    785:                                cp2[-1] = BYTE;
                    786:                        if (p->op==CBR && (p->subop==JBC || p->subop==JBS))
                    787:                                cp2[-1] = LONG;
                    788:                        if (p->op==MOVA && cp1==regs[RT2])
                    789:                                cp2[-1]=LONG;
                    790:                        /* ediv/emod's 2nd operand is quad */
                    791:                        if (((p->op==EDIV
                    792: #ifdef EMOD
                    793:                           || p->op==EMOD
                    794: #endif EMOD
                    795:                           ) && cp1==regs[RT2] || (dblflg&2)) &&
                    796:                           ++r<NUSE && lastuse(r)==0) {
                    797:                                *cp2=0;
                    798:                                setuse(r,p);
                    799:                                cp2=regs[r]; *cp2++=p->subop;
                    800:                        }
                    801:                        if (p->op==MOV || p->op==PUSH || p->op==CVT ||
                    802:                            p->op==MOVZ || p->op==COM || p->op==NEG ||
                    803:                            p->op==STF) {
                    804:                                if (p->op!=PUSH) {
                    805:                                        cp1=regs[RT2];
                    806:                                        if (tempreg(cp1,r)) {
                    807:                                                /*
                    808:                                                 * reincarnation!!
                    809:                                                 * (as in  addl2 r0,r1;
                    810:                                                 *  movl r1,r0;  ret)
                    811:                                                 */
                    812:                                                if (lastuse(r)==0)
                    813:                                                        setuse(r,olduse);
                    814:                                                if ((dblflg&1) &&
                    815:                                                    lastuse(r+1)==0)
                    816:                                                        setuse(r+1,olduse1);
                    817:                                        }
                    818:                                        if (p->op!=MOV)
                    819:                                                cp1=0;
                    820:                                } else
                    821:                                        cp1="-(sp)";
                    822:                                if (cp1)
                    823:                                        scopy(cp2,cp1);
                    824:                                else
                    825:                                        *cp2=0;
                    826:                        } else
                    827:                                *cp2=0;
                    828:                        continue;
                    829:                }
                    830:                /* check for (r),[r] */
                    831:                do {
                    832:                        if (*cp1=='(' || *cp1=='[') { /* get register number */
                    833:                                char t;
                    834:                                for (cp2= ++cp1; *++cp1!=')' && *cp1!=']';)
                    835:                                        ;
                    836:                                t= *cp1; *cp1=0;
                    837:                                if (tempreg(cp2,r) &&
                    838:                                    (lastuse(r)==0 || lastuse(r)==p)) {
                    839:                                        if (lastuse(r))
                    840:                                                setuse(r,p);
                    841:                                        regs[r][0] =
                    842:                                            (*--cp2=='[' ? OPX<<4 : OPB<<4);
                    843:                                }
                    844:                                *cp1=t;
                    845:                        }
                    846:                } while (*++cp1);
                    847:        }
                    848: #ifdef MOVAFASTER
                    849:        /* pushax or movax possibility? */
                    850:        cp1=regs[RT1];
                    851:        if (*cp1++=='$' && isstatic(cp1)) {
                    852:                if (p->op==MOV && p->subop==LONG) {
                    853:                        if (regs[RT1][1]=='L' &&
                    854:                            0!=(p->labno=getnum(regs[RT1]+2))) {
                    855:                                cp1=p->code;
                    856:                                skipto(cp1,',');
                    857:                                p->code= --cp1;
                    858:                        }
                    859:                        p->op = MOVA; p->subop = BYTE; ++p->code; p->pop=0;
                    860:                } else if (p->op==PUSH && p->subop==LONG) {
                    861:                        p->op = PUSHA; p->subop = BYTE; ++p->code; p->pop=0;
                    862:                } else if (p->op==ADD && p->subop==U(LONG,OP3) &&
                    863:                    0<=(r=isreg(regs[RT2]))) {
                    864:                        cp1=cp2=p->code; ++cp1;
                    865:                        do *cp2++= *cp1; skipto(cp1,','); cp2[-1]='[';
                    866:                        do *cp2++= *cp1; skipt(cp1,','); cp2[-1]=']';
                    867:                        if (!equstr(regs[RT3],"-(sp)")) {
                    868:                                p->op = MOVA; p->subop = BYTE;
                    869:                        } else {
                    870:                                p->op = PUSHA; p->subop = BYTE; *cp2=0;
                    871:                        }
                    872:                        if (lastuse(r)==0) {
                    873:                                setuse(r,p); regs[r][0]=OPX<<4;
                    874:                        }
                    875:                        p->pop=0;
                    876:                }
                    877:        }
                    878: #endif MOVAFASTER
                    879:        return (p);
                    880: }
                    881: 
                    882: /* try to eliminate STF's */
                    883: struct node *
                    884: flops(q)
                    885:        register struct node *q;
                    886: {
                    887:        register struct node *p;
                    888:        register int r;
                    889: 
                    890:        if (q->op!=STF || !tempreg(q->code,r))
                    891:                return (q);
                    892:        if (lastuse(r)) {
                    893:                /* see if anyone destroys acc between us */
                    894:                for (p=q->forw; p!=lastuse(r); p=p->forw)
                    895:                        switch (p->op) {
                    896:                        case LABEL:
                    897:                        case LDF: case LNF: case CVLF: case LDFD:
                    898:                        case CVDF: case NEGF: case ADDF: case SUBF:
                    899:                        case MULF: case DIVF: case SINF: case COSF:
                    900:                        case ATANF: case LOGF: case SQRTF: case EXPF:
                    901:                                return (q);
                    902:                        }
                    903:                
                    904:                if (q->subop == p->subop)
                    905:                        switch (p->op) {        /* do it in the accumulator */
                    906:                        case LDF:       /* redundant load */
                    907:                                delnode(p); nld++;
                    908:                                p = p->forw;
                    909:                                break;
                    910:                        case LNF: /* stf r; lnf r ==> negf */
                    911:                                p->op = NEGF;
                    912:                                p->pop = 0;
                    913:                                p->code = 0;
                    914:                                break;
                    915:                        case CMPF2: { /* stf r; cmpf2 r,x ==> cmpf x */
                    916:                                register char *s;
                    917:                                register struct node *p1=p->forw;
                    918: 
                    919:                                for (s=p->code; *s!=','; s++);
                    920:                                *s = 0;
                    921:                                if (isreg(p->code) == r)
                    922:                                        p->code = s+1;
                    923:                                else {
                    924:                                        if (p1->op != CBR || isreg(s+1) != r) {
                    925:                                                *s = ',';
                    926:                                                return (q);
                    927:                                        }
                    928:                                        if (p1->subop > JNE) {
                    929:                                                p1->subop ^= 1;
                    930:                                                p1->pop = 0;
                    931:                                                nrevbr++;
                    932:                                        }
                    933:                                }
                    934:                                p->op = CMPF;
                    935:                                p->pop = 0;
                    936:                                break;
                    937:                        }
                    938:                        default:
                    939:                                return (q);
                    940:                        }
                    941:                else if (p->subop==LONG) {
                    942:                        switch (p->op) {
                    943:                        case TST:       /* stf r; tstl r ==> tstf */
                    944:                                p->op = TSTF;
                    945:                                p->code = 0;
                    946:                                break;
                    947:                        /* send directly to destination */
                    948:                        case MOV:       /* stf r; movl r,x ==> stf x */
                    949:                        case PUSH:      /* stf r; pushl r ==> stf -(sp)/pushd */
                    950:                                if (q->subop == DOUBLE) {
                    951:                                        register struct node *b = p->back;
                    952:                                        /* assume b's 2nd arg is ok */
                    953:                                        if (!(b==lastuse(r+1) && b->op==p->op &&
                    954:                                             b->subop==LONG))
                    955:                                                return (q);
                    956:                                        delnode(b); redunm++;
                    957:                                }
                    958:                                if (p->op==PUSH) {
                    959:                                        if (q->subop == DOUBLE) {
                    960:                                                p->op = PUSHD;
                    961:                                                p->code = 0;
                    962:                                        } else {
                    963:                                                p->op = q->op;
                    964:                                                p->code = copy("-(sp)");
                    965:                                        }
                    966:                                } else {
                    967:                                        p->op = q->op;
                    968:                                        skipto(p->code,',');
                    969:                                }
                    970:                                break;
                    971:                        default:
                    972:                                return (q);
                    973:                        }
                    974:                        p->pop = 0;
                    975:                        p->subop = q->subop;
                    976:                } else
                    977:                        return (q);
                    978:                setuse(r,0);
                    979:                if (q->subop == DOUBLE)
                    980:                        setuse(r+1,0);
                    981:                { /* undo any effect on uses in the area between p and q,
                    982:                   * as we are going over it again */
                    983:                        register struct node *b;
                    984:                        for (b=p; b!=q; b=b->back) {
                    985:                                for (r=0; r<NUSE; r++) {
                    986:                                        if (lastuse(r) == b)
                    987:                                                setuse(r,0);
                    988:                                        if (useacc == b)
                    989:                                                useacc = 0;
                    990:                                }
                    991:                        }
                    992:                }
                    993:                return (p->forw); /* make p the next for bflow */
                    994:        }
                    995:        /* it's a store to reg which isnt used elsewhere */
                    996:        if ((p=q->forw)->op == CBR) {
                    997:                q->op = TSTF;
                    998:                q->pop = 0;
                    999:                q->code = 0;
                   1000:        } else {
                   1001:                delnode(q); nst++;
                   1002:                if (p->op ==STF || p->op==TSTF || p->op==PUSHD) {
                   1003:                        if (useacc == p)
                   1004:                                useacc = 0;
                   1005:                        return (p->forw);       /* so ldmov can be used on p */
                   1006:                }
                   1007:        }
                   1008:        return (p);
                   1009: }
                   1010: 
                   1011: /*
                   1012:  * Try to change load/store sequences to movl
                   1013:  */
                   1014: ldmov(q)
                   1015:        register struct node *q;
                   1016: {
                   1017:        register struct node *p;
                   1018:        register char *s, *pcod, *cp;
                   1019:        char *dlsw();
                   1020: 
                   1021:        p = q->back;
                   1022:        if (!(useacc==0 && (q->op==STF || q->op==TSTF || q->op==PUSHD) &&
                   1023:              ((p->op==LDF && p->subop==q->subop) ||
                   1024:               (p->op==LDFD && q->subop==DOUBLE))))
                   1025:                return (0);
                   1026:        pcod = p->code;
                   1027:        cp = p->code;
                   1028:        /* prepare args for movl/pushl */
                   1029:        if (q->op!=TSTF && q->subop==DOUBLE) {
                   1030:                if (p->op == LDF) {
                   1031:                        if ((s = dlsw(p->code)) == NULL)
                   1032:                                return (0);
                   1033: 
                   1034:                        strcpy(line, s);
                   1035:                        if (q->op == STF) {
                   1036:                                strcat(line, ",");
                   1037:                                if ((s = dlsw(q->code)) == NULL)
                   1038:                                        return (0);
                   1039:                                strcat(line, s);
                   1040:                                p->op = MOV;
                   1041:                        } else
                   1042:                                p->op = PUSH;
                   1043:                } else { /* LDFD */
                   1044:                        if (q->op == STF) {
                   1045:                                if ((s = dlsw(q->code)) == NULL)
                   1046:                                        return (0);
                   1047:                        } else
                   1048:                                s = "-(sp)";
                   1049:                        strcpy(line, s);
                   1050:                        p->op = CLR;
                   1051:                }
                   1052:                p->pop = 0;
                   1053:                p->subop = LONG;
                   1054:                p->code = copy(line);
                   1055:        } else {
                   1056:                if ((p->op == LDF) && (p->subop == DOUBLE) && (indexa(cp))) 
                   1057:                        return (0);
                   1058:                delnode(p);
                   1059:        }
                   1060:        strcpy(line, pcod);
                   1061:        if (q->op == STF) {             /* ldf x; stf y ==> movl x,y */
                   1062:                strcat(line, ",");
                   1063:                strcat(line, q->code);
                   1064:                q->op = MOV;
                   1065:                nst++;
                   1066:        } else if (q->op == TSTF)       /* ldf x; tstf ==> tstl x */
                   1067:                q->op = TST;
                   1068:        else                    /* ldd x; pushd ==> pushl x+4; pushl x */
                   1069:                q->op = PUSH;
                   1070:        q->pop = 0;
                   1071:        q->subop = LONG;
                   1072:        q->code = copy(line);
                   1073:        nld++;
                   1074:        return (1);
                   1075: }
                   1076: 
                   1077: /*
                   1078:  * Reconstruct the address of
                   1079:  * l.s.w. of a double operand
                   1080:  */
                   1081: char *
                   1082: dlsw(d)
                   1083:        register char *d;
                   1084: {
                   1085:        register char *s, *t, *c;
                   1086:        register int r;
                   1087:        static char lsw[C2_ASIZE];
                   1088: 
                   1089:        if (d[0] == '*' || d[0] == '$')
                   1090:                return (NULL);
                   1091:        if (strncmp(d, "(r", 2) == 0 && isdigit(d[2]))
                   1092:                return (NULL);
                   1093:        t = lsw;
                   1094:        if ((r=isreg(d)) >= 0)
                   1095:                sprintf(t, "r%d", r+1);
                   1096:        else {
                   1097:                for (s=d; *s && *s!='('; *t++ = *s++)
                   1098:                        if (*s == '[')
                   1099:                                return (NULL);
                   1100:                if (s!=d)
                   1101:                        *t++ = '+';
                   1102:                *t++ = '4';
                   1103:                while (*t++ = *s)
                   1104:                        if (*s++ == '[' )
                   1105:                                return (NULL);
                   1106:        }
                   1107:        return (lsw);
                   1108: }
                   1109: 
                   1110: checkexpr(p)
                   1111:        register char *p;
                   1112: {
                   1113: 
                   1114:        while (*p && *p != ',') {
                   1115:                if (*p == '+'  || *p == '-')
                   1116:                        return (1);
                   1117:                *p++;
                   1118:        }
                   1119:        return (0);
                   1120: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.