Annotation of researchv9/cmd/sun/pcc/code.c, revision 1.1.1.2

1.1       root        1: #ifndef lint
                      2: static char sccsid[] = "@(#)code.c 1.1 86/02/03 SMI";
                      3: #endif
                      4: 
                      5: 
                      6: # include "cpass1.h"
                      7: # include <sys/types.h>
                      8: 
                      9: # include <a.out.h>
                     10: # include <stab.h>
                     11: 
                     12: #ifndef VAX
                     13: extern char *rnames[];
                     14: #endif
                     15: 
                     16: #ifndef ONEPASS
                     17: int usedregs;
                     18: int usedfpregs;
                     19: #endif !ONEPASS
                     20: 
                     21: int proflg = 0;        /* are we generating profiling code? */
                     22: int strftn = 0;  /* is the current function one which returns a value */
                     23: int fltfun = 0; /* is function float or double? */
                     24: int optimize = 0; /* is optimization enabled? If so, no SLINEs for you */
                     25: #ifdef STACKPROBE
                     26: int noprobe= 1; /* are we not generating 68000 stack probes? */
                     27: #endif
                     28: int gdebug;
                     29: int fdefflag;  /* are we within a function definition ? */
                     30: char NULLNAME[8];
                     31: int labelno;
                     32: 
                     33: branch( n )
                     34: {
                     35:        /* output a branch to label n */
                     36:        /* exception is an ordinary function branching to retlab: then, return */
                     37:        if( n == retlab && !strftn ){
                     38: #ifdef VAX
                     39:                printf( "       ret\n" );
                     40: #else
                     41:                printf( "       jra     LE%d\n", ftnno );
                     42: #endif
                     43:                }
                     44:        else 
                     45: #ifdef VAX
                     46:                printf( "       jbr     L%d\n", n );
                     47: #else
                     48:                printf("        jra     L%d\n", n );
                     49: #endif
                     50: }
                     51: 
                     52: int lastloc = { -1 };
                     53: 
                     54: #ifdef VAX
                     55: short log2tab[] = {0, 0, 1, 2, 2, 3, 3, 3, 3};
                     56: #define LOG2SZ 9
                     57: #endif
                     58: 
                     59: defalign(n) 
                     60: {
                     61:        /* cause the alignment to become a multiple of n */
                     62: #ifdef VAX
                     63:        n /= SZCHAR;
                     64:        if( lastloc != PROG && n > 1 ) printf( "        .align  %d\n", n >= 0 && n < LOG2SZ ? log2tab[n] : 0 );
                     65: #else
                     66:        if ( lastloc != PROG && n > 1 ) printf("        .even\n");
                     67: #endif
                     68: }
                     69: 
                     70: locctr( l )
                     71: {
                     72:        register temp;
                     73:        /* l is PROG, ADATA, DATA, STRNG, ISTRNG, or STAB */
                     74: 
                     75:        if( l == lastloc ) return(l);
                     76:        temp = lastloc;
                     77:        lastloc = l;
                     78:        switch( l ){
                     79: 
                     80:        case PROG:
                     81:                printf( "       .text\n" );
                     82:                break;
                     83: 
                     84:        case DATA:
                     85:        case ADATA:
                     86:                if (temp != DATA && temp != ADATA)
                     87:                        printf( "       .data\n" );
                     88:                break;
                     89: 
                     90:        /* The following levels of .data statements will be problems if
                     91:           there is ever any relocation required. */
                     92:        case STRNG:
                     93: #ifdef VAX
                     94:                printf( "       .data   1\n" );
                     95: #else
                     96:                printf( "       .data1\n" );
                     97: #endif
                     98:                break;
                     99: 
                    100:        case ISTRNG:
                    101: #ifdef VAX
                    102:                printf( "       .data   2\n" );
                    103: #else
                    104:                printf( "       .data2\n" );
                    105: #endif
                    106:                break;
                    107: 
                    108:        case STAB:
                    109:                printf( "       .stab\n" );
                    110:                break;
                    111: 
                    112:        default:
                    113:                cerror( "illegal location counter" );
                    114:                }
                    115: 
                    116:        return( temp );
                    117: }
                    118: 
                    119: deflab( n )
                    120: {
                    121:        /* output something to define the current position as label n */
                    122:        printf( "L%d:\n", n );
                    123: }
                    124: 
                    125: int crslab = 10;
                    126: 
                    127: getlab()
                    128: {
                    129:        /* return a number usable for a label */
                    130:        return( ++crslab );
                    131: }
                    132: 
                    133: 
                    134: #ifdef VAX
                    135: int ent_mask[] = {
                    136:        0,0,0,0,0, 0xfc0, 0xf80, 0xf00, 0xe00, 0xc00, 0x800, 0};
                    137: 
                    138: int reg_use = 11;
                    139: #else
                    140: extern int regsused;
                    141: #endif
                    142: 
                    143: efcode()
                    144: {
                    145:        /* code for the end of a function */
                    146: 
                    147: #ifdef VAX
                    148:        if( strftn ){  /* copy output (in R2) to caller */
                    149:                register NODE *l, *r;
                    150:                register struct symtab *p;
                    151:                register TWORD t;
                    152:                register int j;
                    153:                int i;
                    154: 
                    155:                p = STP(curftn);
                    156:                t = p->stype;
                    157:                t = DECREF(t);
                    158: 
                    159:                deflab( retlab );
                    160: 
                    161:                i = getlab();   /* label for return area */
                    162: #ifndef LCOMM
                    163:                printf("        .data\n" );
                    164:                printf("        .align  2\n" );
                    165:                printf("L%d:    .space  %d\n", i, tsize(t, p->dimoff, p->sizoff)/SZCHAR );
                    166:                printf("        .text\n" );
                    167: #else
                    168:                { int sz = tsize(t, p->dimoff, p->sizoff) / SZCHAR;
                    169:                if (sz % sizeof (int))
                    170:                        sz += sizeof (int) - (sz % sizeof (int));
                    171:                printf("        .lcomm  L%d,%d\n", i, sz);
                    172:                }
                    173: #endif
                    174:                psline(lineno);
                    175:                printf("        movab   L%d,r1\n", i);
                    176: 
                    177:                reached = 1;
                    178:                l = block( REG, NIL, NIL, PTR|t, p->dimoff, p->sizoff );
                    179:                l->tn.rval = 1;  /* R1 */
                    180:                l->tn.lval = 0;  /* no offset */
                    181:                r = block( REG, NIL, NIL, PTR|t, p->dimoff, p->sizoff );
                    182:                r->tn.rval = 0;  /* R0 */
                    183:                r->tn.lval = 0;
                    184:                l = buildtree( UNARY MUL, l, NIL );
                    185:                r = buildtree( UNARY MUL, r, NIL );
                    186:                l = buildtree( ASSIGN, l, r );
                    187:                l->in.op = FREE;
                    188:                ecomp( l->in.left );
                    189:                printf( "       movab   L%d,r0\n", i );
                    190:                /* turn off strftn flag, so return sequence will be generated */
                    191:                strftn = 0;
                    192:                }
                    193:        branch( retlab );
                    194: #ifndef VMS
                    195:        printf( "       .set    L%d,0x%x\n", ftnno, ent_mask[reg_use] );
                    196: #else
                    197:        printf( "       .set    L%d,%d  # Hex = 0x%x\n", ftnno, 0x3c| ent_mask[reg_use], ent_mask[reg_use]  );
                    198:        /* KLS kludge, under VMS if you use regs 2-5, you must save them. */
                    199: #endif
                    200:        reg_use = 11;
                    201:        p2bend();
                    202:        fdefflag = 0;
                    203: #else
                    204: /* 68k code */
                    205: /* NOTE: We still have to add FTN and stab support here */
                    206:        if( strftn ){  /* copy output (in r0) to caller */
                    207:                register struct symtab *p;
                    208:                register int stlab;
                    209:                register int count;
                    210:                int size;
                    211: 
                    212:                p = STP(curftn);
                    213: 
                    214:                deflab( retlab );
                    215: 
                    216:                stlab = getlab();
                    217:                printf( "       movl    d0,a0\n" );
                    218:                printf( "       movl    #L%d,a1\n", stlab );
                    219:                size = tsize( DECREF(p->stype), p->dimoff, p->sizoff ) / SZCHAR;
                    220:                printf( "       .bss\nL%d:      .=.+%d\n        .text\n",
                    221:                        stlab, size );
                    222:                if (size <= 20) {
                    223:                        count = size/2;
                    224:                        while( size ){ /* simple load/store loop */
                    225:                                count = (size > 2) ? 4 : 2;
                    226:                                printf("        mov%c   a0@+,a1@+\n",
                    227:                                        count==2 ? 'w' : 'l');
                    228:                                size -= count;
                    229:                        }
                    230:                } else {
                    231:                        int residue;
                    232: 
                    233:                        count = size / sizeof(long);
                    234:                        residue = size % sizeof(long);
                    235:                        if (count > 0x7fff) {
                    236:                                printf("        movl    #%d,d0\n", count-1);
                    237:                                printf("1:      movl    a0@+,a1@+\n");
                    238:                                printf("        dbra    d0,1b\n");
                    239:                                printf("        subql   #1,d0\n");
                    240:                                printf("        jcc     1b\n");
                    241:                        } else {
                    242:                                printf("        movw    #%d,d0\n", count-1);
                    243:                                printf("1:      movl    a0@+,a1@+\n");
                    244:                                printf("        dbra    d0,1b\n");
                    245:                        }
                    246:                        switch(residue) {
                    247:                        case 1:
                    248:                                printf("        movb    a0@+,a1@+\n");
                    249:                                break;
                    250:                        case 2:
                    251:                                printf("        movw    a0@+,a1@+\n");
                    252:                                break;
                    253:                        case 3:
                    254:                                printf("        movw    a0@+,a1@+\n");
                    255:                                printf("        movb    a0@+,a1@+\n");
                    256:                                break;
                    257:                        default:
                    258:                                break;
                    259:                        }
                    260:                }
                    261:                printf( "       movl    #L%d,d0\n", stlab );
                    262:                /* turn off strftn flag, so return sequence will be generated */
                    263:                strftn = 0;
                    264:                }
                    265:        /* branch( retlab ); */
                    266:        p2bend();
1.1.1.2 ! root      267:        dbfunend(labelno++);
1.1       root      268:        fdefflag = 0;
                    269: #endif
                    270: }
                    271: 
                    272: int ftlab1, ftlab2;
                    273: 
                    274: bfcode( a, n ) int a[]; 
                    275: {
                    276:        /* code for the beginning of a function; a is an array of
                    277:                indices in stab for the arguments; n is the number */
                    278:        register i;
                    279:        register temp;
                    280:        register struct symtab *p;
                    281:        int off;
                    282:        char *toreg();
                    283: 
                    284:        locctr( PROG );
                    285:        p = STP(curftn);
                    286:        if (p == NULL) return;
                    287:        temp = p->stype;
                    288:        temp = DECREF(temp);
                    289: #ifdef VAX
                    290:        printf( "       .align  1\n");
                    291: #else
                    292:        /* magic cookie for c2 */
                    293:        /* uses old (pre-TERROR) type format */
                    294:        printf( "|#PROC# %#o\n", (temp&BTMASK) | ((temp&~BTMASK)>>2) );
                    295: #endif
                    296:        defnam( p );
                    297:        strftn = (temp==STRTY) || (temp==UNIONTY);
                    298:        fltfun = (temp==FLOAT) || (temp==DOUBLE);
                    299: 
                    300:        retlab = getlab();
                    301: 
                    302:        /* routine prolog */
                    303: 
                    304: #ifdef VAX
                    305:        printf( "       .word   L%d\n", ftnno);
                    306: #else  !VAX
                    307:        printf("        link    a6,#0\n");
                    308:        printf("        addl    #-LF%d,sp\n", ftnno);
                    309: #ifdef STACKPROBE
                    310:        if (noprobe!=1){
                    311:            printf("    tstb    sp@(-LP%d)\n",ftnno);
                    312:        }
                    313: #endif
                    314:        printf("        moveml  #LS%d,sp@\n",ftnno);
                    315:        if (use68881) {
                    316:                /*
                    317:                 * save floating registers used for variables
                    318:                 */
                    319:                if (use68020) {
                    320:                        printf("        fmovem  #LSS%d,a6@(-LFF%d:l)\n",
                    321:                                ftnno, ftnno);
                    322:                } else {
                    323:                        /* 68881 without 68020; unlikely combination */
                    324:                        printf("        movl    #-LFF%d,a0\n", ftnno);
                    325:                        printf("        fmovem  #LSS%d,a6@(0,a0:l)\n", ftnno);
                    326:                }
                    327:        }
                    328: #endif !VAX
                    329: 
                    330: #ifdef VAX
                    331:        ftlab1 = getlab();
                    332:        ftlab2 = getlab();
                    333:        printf( "       jbr     L%d\n", ftlab1);
                    334:        printf( "L%d:\n", ftlab2);
                    335: #endif
                    336:        if( proflg ) {  /* profile code */
                    337:                i = getlab();
                    338: #ifdef VAX
                    339:                printf("        movab   L%d,r0\n", i);
                    340:                printf("        jsb     mcount\n");
                    341:                printf("        .data\n");
                    342:                printf("        .align  2\n");
                    343:                printf("L%d:    .long   0\n", i);
                    344:                printf("        .text\n");
                    345: #else
                    346:                printf("        movl    #L%d,a0\n",i);
                    347:                printf("        jsr     mcount\n");
                    348:                printf("        .bss\n  .even\n");
                    349:                printf("L%d:    .skip   4\n     .text\n",i);
                    350: #endif
                    351:                psline(lineno);
                    352:                }
                    353: 
                    354:        off = ARGINIT;
                    355: 
                    356:        for( i=0; i<n; ++i ){
                    357: #ifndef VAX
                    358:                char type;
                    359: #endif
                    360: 
                    361:                p = STP(a[i]);
                    362:                if( p->sclass == REGISTER ){
                    363:                        temp = p->offset;  /* save register number */
                    364:                        p->sclass = PARAM;  /* forget that it is a register */
                    365:                        p->offset = NOOFFSET;
                    366:                        oalloc( p, &off );
                    367: #ifdef VAX
                    368: /*tbl*/                        printf( "       %s      %d(ap),r%d\n",
                    369:                                toreg(p->stype), p->offset/SZCHAR, temp );
                    370: #else
                    371:                        printf( "       %s      a6@(%d),%s\n",
                    372:                                toreg(p->stype), p->offset/SZCHAR,
                    373:                                rnames[temp]);
                    374:                        markused(temp);
                    375: #endif
                    376:                        p->offset = temp;  /* remember register number */
                    377:                        p->sclass = REGISTER;   /* remember that it is a register */
                    378:                        }
                    379:                /* The 68k doesn't have this code.  I'll try it for a while */
                    380: #ifdef VAX
                    381:                else if( p->stype == STRTY || p->stype == UNIONTY ) {
                    382:                        p->offset = NOOFFSET;
                    383:                        if( oalloc( p, &off ) ) cerror( "bad argument" );
                    384:                        SETOFF( off, ALSTACK );
                    385:                        }
                    386: #endif
                    387:                else {
                    388:                        if( oalloc( p, &off ) ) cerror( "bad argument" );
                    389:                        }
                    390: 
                    391:                }
                    392:        fdefflag = 1;
                    393: }
                    394: 
                    395: bccode()
                    396: { /* called just before the first executable statment */
                    397:                /* by now, the automatics and register variables are allocated */
                    398:        SETOFF( autooff, SZINT );
                    399:        /* set aside store area offset */
                    400:        p2bbeg( autooff, regvar );
                    401: #ifdef VAX
                    402:        reg_use = (reg_use > regvar ? regvar : reg_use);
                    403: #endif
                    404: }
                    405: 
                    406: ejobcode( flag )
                    407: {
                    408:        /* called just before final exit */
                    409:        /* flag is 1 if errors, 0 if none */
1.1.1.2 ! root      410:        ejsdb();
1.1       root      411: }
                    412: 
                    413: aobeg()
                    414: {
                    415:        /* called before removing automatics from stab */
                    416: }
                    417: 
                    418: aocode(p) struct symtab *p; 
                    419: {
                    420:        /* called when automatic p removed from stab */
                    421: }
                    422: 
                    423: aoend()
                    424: {
                    425:        /* called after removing all automatics from stab */
                    426: }
                    427: 
                    428: defnam( p ) register struct symtab *p; 
                    429: {
                    430:        /* define the current location as the name p->sname */
                    431: 
                    432:        if( p->sclass == EXTDEF ){
                    433:                printf( "       .globl  %s\n", exname( p->sname ) );
                    434:                }
                    435:        if( p->sclass == STATIC && p->slevel>1 ) deflab( p->offset );
                    436:        else printf( "%s:\n", exname( p->sname ) );
                    437: 
                    438: }
                    439: 
                    440: bycode( t, i )
                    441: {
                    442: #ifdef ASSTRINGS
                    443: static int     lastoctal = 0;
                    444: #endif
                    445: 
                    446:        /* put byte i+1 in a string */
                    447: 
                    448: #ifdef ASSTRINGS
                    449: 
                    450:        i &= 077;
                    451:        if ( t < 0 ){
                    452:                if ( i != 0 )   printf( "\"\n" );
                    453:        } else {
                    454:                if ( i == 0 ) printf("\t.ascii\t\"");
                    455:                if ( t == '\\' || t == '"'){
                    456:                        lastoctal = 0;
                    457:                        printf("\\%c", t);
                    458:                }
                    459:                        /*
                    460:                         *      We escape the colon in strings so that
                    461:                         *      c2 will, in its infinite wisdom, interpret
                    462:                         *      the characters preceding the colon as a label.
                    463:                         *      If we didn't escape the colon, c2 would
                    464:                         *      throw away any trailing blanks or tabs after
                    465:                         *      the colon, but reconstruct a assembly
                    466:                         *      language semantically correct program.
                    467:                         *      C2 hasn't been taught about strings.
                    468:                         */
                    469:                else if ( t == ':' || t < 040 || t >= 0177 || t == '|' || t == ';' ){
                    470:                        lastoctal++;
                    471:                        printf("\\%o",t);
                    472:                }
                    473:                else if ( lastoctal && '0' <= t && t <= '9' ){
                    474:                        lastoctal = 0;
                    475:                        printf("\"\n\t.ascii\t\"%c", t );
                    476:                }
                    477:                else
                    478:                {       
                    479:                        lastoctal = 0;
                    480:                        putchar(t);
                    481:                }
                    482:                if ( i == 077 ) printf("\"\n");
                    483:        }
                    484: #else
                    485: 
                    486:        i &= 07;
                    487:        if( t < 0 ){ /* end of the string */
                    488:                if( i != 0 ) printf( "\n" );
                    489:                }
                    490: 
                    491:        else { /* stash byte t into string */
                    492:                if( i == 0 ) printf( "  .byte   " );
                    493:                else printf( "," );
                    494: #ifdef VAX
                    495:                printf( "0x%x", t );
                    496: #else
                    497:                printf( "0x%x", t );
                    498: #endif
                    499:                if( i == 07 ) printf( "\n" );
                    500:                }
                    501: #endif
                    502: }
                    503: 
                    504: zecode( n )
                    505: {
                    506:        /* n integer words of zeros */
                    507:        OFFSZ temp;
                    508: #ifndef VAX
                    509:        register i;
                    510: #endif
                    511: 
                    512:        if( n <= 0 ) return;
                    513: #ifdef VAX
                    514:        printf( "       .space  %d\n", (SZINT/SZCHAR)*n );
                    515: #else
                    516:        printf( "       .skip   %d\n", (SZINT/SZCHAR)*n );
                    517: #endif
                    518:        temp = n;
                    519:        inoff += temp*SZINT;
                    520: }
                    521: 
                    522: fldal( t ) unsigned t; 
                    523: { /* return the alignment of field of type t */
                    524:        uerror( "illegal field type" );
                    525:        return( ALINT );
                    526: }
                    527: 
                    528: fldty( p ) struct symtab *p; 
                    529: { /* fix up type of field p */
                    530:        ;
                    531: }
                    532: 
                    533: 
                    534: where(c)
                    535: { /* print location of error  */
                    536:        /* c is either 'u', 'c', or 'w' */
                    537:        /* GCOS version */
                    538:        fprintf( stderr, "%s, line %d: ", ftitle, lineno );
                    539: }
                    540: 
                    541: 
                    542: /* tbl - toreg() returns a pointer to a char string
                    543:                  which is the correct  "register move" for the passed type 
                    544:  */
                    545: struct type_move 
                    546: {TWORD fromtype; char tostrng[8];} toreg_strs[] =
                    547:        {
                    548: #ifdef VAX
                    549:        CHAR, "cvtbl",
                    550:        SHORT, "cvtwl",
                    551:        INT, "movl",
                    552:        LONG, "movl",
                    553:        FLOAT, "movf",
                    554:        DOUBLE, "movd",
                    555:        UCHAR,  "movzbl",
                    556:        USHORT, "movzwl",
                    557:        UNSIGNED,       "movl",
                    558:        ULONG,  "movl",
                    559: #else
                    560:        CHAR, "movb",
                    561:        UCHAR, "movb",
                    562:        SHORT, "movw",
                    563:        USHORT, "movw",
                    564: #endif
                    565:        -1, ""
                    566:        };
                    567: 
                    568: char
                    569: *toreg(type)
                    570:        TWORD type;
                    571: {
                    572:        struct type_move *p;
                    573: 
                    574:        for ( p=toreg_strs; p->fromtype > 0; p++)
                    575:                if (p->fromtype == type)
                    576:                        return(p->tostrng);
                    577: 
                    578:        /* type not found, must be a pointer type */
                    579:        if (use68881 && type == FLOAT) {
                    580:                return("fmoves");
                    581:        }
                    582:        if (use68881 && type == DOUBLE) {
                    583:                return("fmoved");
                    584:        }
                    585:        return("movl");
                    586: }
                    587: /* tbl */
                    588: 
                    589: 
                    590: main( argc, argv ) char *argv[]; 
                    591: {
                    592:        int v;
                    593: #ifdef BUFSTDERR
                    594:        char errbuf[BUFSIZ];
                    595:        setbuf(stderr, errbuf);
                    596: #endif
                    597: /*     ffloat_(); /* HACK -- avoid sky board even if present */
                    598:        v = mainp1( argc, argv );
                    599:        floatnote();
                    600:        exit( v );
                    601: }
                    602: 
                    603: struct sw heapsw[SWITSZ];      /* heap for switches */
                    604: 
                    605: genswitch(p,n) register struct sw *p;
                    606: {
                    607:        /*      p points to an array of structures, each consisting
                    608:                of a constant value and a label.
                    609:                The first is >=0 if there is a default label;
                    610:                its value is the label number
                    611:                The entries p[1] to p[n] are the nontrivial cases
                    612:                */
                    613:        register i;
                    614:        register CONSZ j, range;
                    615:        register dlab, swlab;
                    616: 
                    617:        range = p[n].sval-p[1].sval;
                    618: 
                    619:        if( range>0 && range <= 3*n && n>=4 ){ /* implement a direct switch */
                    620: 
                    621:                dlab = p->slab >= 0 ? p->slab : getlab();
                    622: #ifdef VAX
                    623:                swlab = getlab();
                    624: 
                    625:                /* already in r0 */
                    626:                printf("        casel   r0,$%ld,$%ld\n", p[1].sval, range);
                    627:                printf("L%d:\n", swlab);
                    628:                for( i=1,j=p[1].sval; i<=n; j++) {
                    629:                        printf("        .word   L%d-L%d\n", (j == p[i].sval ? ((j=p[i++].sval), p[i-1].slab) : dlab),
                    630:                                swlab);
                    631:                        }
                    632: 
                    633:                if( p->slab >= 0 ) branch( dlab );
                    634:                else printf("L%d:\n", dlab);
                    635: #else
                    636:                if( p[1].sval ){
                    637:                        printf( "       subl    #" );
                    638:                        printf( CONFMT, p[1].sval );
                    639:                        printf( ",d0\n" );
                    640:                        }
                    641: 
                    642:                /* note that this is a cl; it thus checks
                    643:                   for numbers below range as well as out of range.
                    644:                   */
                    645:                printf( "       cmpl    #%ld,d0\n", range );
                    646:                printf( "       jhi     L%d\n", dlab );
                    647: 
                    648:                if (use68020) {
                    649:                        printf( "       movw    pc@(6,d0:w:2),d0\n" );
                    650:                } else {
                    651:                        printf( "       addw    d0,d0\n" );
                    652:                        printf( "       movw    pc@(6,d0:w),d0\n" );
                    653:                }
                    654:                printf( "       jmp     pc@(2,d0:w)\n" );
                    655: 
                    656:                /* output table */
                    657: 
                    658:   /*   printf( "L%d = \n", swlab=getlab() ); */
                    659:                printf( "L%d:  \n", swlab=getlab() );
                    660:                for( i=1,j=p[1].sval; i<=n; ++j )
                    661:                        printf( "       .word   L%d-L%d\n", ( j == p[i].sval ) ?
                    662:                                p[i++].slab : dlab, swlab );
                    663:                if( p->slab< 0 ) deflab( dlab );
                    664: #endif
                    665:                return;
                    666: 
                    667:                }
                    668: 
                    669:        if( n>8 ) {     /* heap switch */
                    670: #ifdef VAX
                    671:                heapsw[0].slab = dlab = p->slab >= 0 ? p->slab : getlab();
                    672:                makeheap(p, n, 1);      /* build heap */
                    673: 
                    674:                walkheap(1, n); /* produce code */
                    675: 
                    676:                if( p->slab >= 0 )
                    677:                        branch( dlab );
                    678:                else
                    679:                        printf("L%d:\n", dlab);
                    680:                return;
                    681: #else
                    682:                /* drop comparison loop, then switch table, then comparison table */
                    683:                int tbllab, llab;
                    684:                int biggest, smallest, range;
                    685:                char sizechar;
                    686:                char *sizeword;
                    687:                int unsgned = 1;
                    688:                tbllab = getlab();
                    689:                dlab = p->slab>=0 ? p->slab : getlab();
                    690:                biggest = p[n].sval;
                    691:                smallest = p[1].sval;
                    692:                range = biggest-smallest;
                    693:                if   (biggest<=0377 && smallest>=0){
                    694:                    sizechar = 'b';sizeword = ".byte"; 
                    695:                    smallest = 0;
                    696:                }else if ( range<=0377 && range > 0 ){
                    697:                    sizechar = 'b';sizeword = ".byte";
                    698:                }else if (biggest<=0177777 && smallest>=0){
                    699:                    sizechar = 'w';sizeword = ".word"; 
                    700:                    smallest = 0;
                    701:                }else if ( range<=0177777 && range > 0 ){
                    702:                    sizechar = 'w';sizeword = ".word";
                    703:                }else{
                    704:                    sizechar = 'l';sizeword = ".long"; smallest = 0; unsgned = 0;
                    705:                }
                    706: 
                    707:                if( smallest ){
                    708:                    printf( "   %sl     #%d,d0\n", smallest<0?"add":"sub",
                    709:                                abs(smallest) );
                    710:                    printf( "   cmpl    #%d,d0\n", range );
                    711:                    printf( "   jhi     L%d\n", dlab );
                    712:                } else if (sizechar != 'l' ){
                    713:                    printf( "   cmpl    #%d,d0\n", biggest );
                    714:                    printf( "   jhi     L%d\n", dlab );
                    715:                }
                    716:                printf( "       lea     L%d,a0\n        mov%s   #%d,d1\n",
                    717:                        tbllab, (n<=128)?"eq":"w",n-1);
                    718:                llab = getlab();
                    719:                printf( "L%d:   cmp%c   a0@+,d0\n       db%s    d1,L%d\n",
                    720:                        llab, sizechar, unsgned?"cc":"ge", llab);
                    721:                printf( "       jne     L%d\n", dlab );
                    722:                if (use68020) {
                    723:                        printf( "       movw    pc@(6,d1:w:2),d0\n" );
                    724:                } else {
                    725:                        printf( "       addw    d1,d1\n" );
                    726:                        printf( "       movw    pc@(6,d1:w),d0\n" );
                    727:                }
                    728:                printf( "       jmp     pc@(2,d0:w)\n" );
                    729:                /* put out statement list forwards, comparison table backwards*/
                    730:                printf( "L%d:  \n", swlab=getlab() );
                    731:                for( i=1; i<=n; i++ )
                    732:                        printf( "       .word   L%d-L%d\n", p[i].slab, swlab );
                    733:                printf( "L%d:  \n", tbllab );
                    734:                for( i=n; i>=1; i-- )
                    735:                    printf( "   %s      %d\n", sizeword, p[i].sval-smallest );
                    736:                if (sizechar == 'b')
                    737:                    printf("    .even\n");
                    738:                if( p->slab< 0 ) deflab( dlab );
                    739:                return;
                    740: #endif
                    741:        }
                    742: 
                    743:        /* debugging code */
                    744: 
                    745:        /* out for the moment
                    746:        if( n >= 4 ) werror( "inefficient switch: %d, %d", n, (int) (range/n) );
                    747:        */
                    748: 
                    749:        /* simple switch code */
                    750: 
                    751:        for( i=1; i<=n; ++i ){
                    752:                /* already in r0 */
                    753: 
                    754: #ifdef VAX
                    755:                printf( "       cmpl    r0,$" );
                    756: #else
                    757:                printf( "       cmpl    #" );
                    758: #endif
                    759:                printf( CONFMT, p[i].sval );
                    760: #ifdef VAX
                    761:                printf( "\n     jeql    L%d\n", p[i].slab );
                    762: #else
                    763:                printf( ",d0\n  jeq     L%d\n", p[i].slab );
                    764: #endif
                    765:                }
                    766: 
                    767:        if( p->slab>=0 ) branch( p->slab );
                    768: }
                    769: 
                    770: #ifdef VAX
                    771: 
                    772: makeheap(p, m, n)
                    773: register struct sw *p;
                    774: {
                    775:        register int q;
                    776: 
                    777:        q = select(m);
                    778:        heapsw[n] = p[q];
                    779:        if( q>1 ) makeheap(p, q-1, 2*n);
                    780:        if( q<m ) makeheap(p+q, m-q, 2*n+1);
                    781: }
                    782: 
                    783: select(m) 
                    784: {
                    785:        register int l,i,k;
                    786: 
                    787:        for(i=1; ; i*=2)
                    788:                if( (i-1) > m ) break;
                    789:        l = ((k = i/2 - 1) + 1)/2;
                    790:        return( l + (m-k < l ? m-k : l));
                    791: }
                    792: 
                    793: walkheap(start, limit)
                    794: {
                    795:        int label;
                    796: 
                    797: 
                    798:        if( start > limit ) return;
                    799: #ifdef VAX
                    800:                printf( "       cmpl    r0,$" );
                    801: #else
                    802:                printf( "       cmpl    #" );
                    803: #endif
                    804:                printf( CONFMT, heapsw[start].sval );
                    805: #ifdef VAX
                    806:                printf( "\n     jeql    L%d\n", heapsw[start].slab );
                    807: #else
                    808:                printf( ",d0\n  beq     L%d\n", heapsw[start].slab );
                    809: #endif
                    810:        if( (2*start) > limit ) {
                    811: #ifdef VAX
                    812:                printf("        jbr     L%d\n", heapsw[0].slab);
                    813: #else
                    814:                printf("        bra     L%d\n", heapsw[0].slab);
                    815: #endif
                    816:                return;
                    817:        }
                    818:        if( (2*start+1) <= limit ) {
                    819:                label = getlab();
                    820: #ifdef VAX
                    821:                printf("        jgtr    L%d\n", label);
                    822: #else
                    823:                printf("        bgt     L%d\n", label);
                    824: #endif 
                    825:        } else
                    826: #ifdef VAX 
                    827:                printf("        jgtr    L%d\n", heapsw[0].slab);
                    828: #else 
                    829:                printf("        bgt     L%d\n", heapsw[0].slab);
                    830: #endif 
                    831:        walkheap( 2*start, limit);
                    832:        if( (2*start+1) <= limit ) {
                    833:                printf("L%d:\n", label);
                    834:                walkheap( 2*start+1, limit);
                    835:        }
                    836: }
                    837: #endif VAX heap code

unix.superglobalmegacorp.com

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