Annotation of cci/usr/src/lib/fc2/c21.c, revision 1.1.1.2

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

unix.superglobalmegacorp.com

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