Annotation of researchv10no/cmd/lcomp/5bb.c, revision 1.1.1.1

1.1       root        1: #define unsafe 1       /* pretend killing all but C is as good as killing
                      2:                         * all condition codes */
                      3: #ifdef unsafe
                      4: #define E      (C|K)
                      5: #else
                      6: #define E      K
                      7: #endif
                      8: #include "stdio.h"
                      9: #include "instr.c"
                     10: #include "ctype.h"
                     11: struct inst *index[128];
                     12: 
                     13: #define SBBLK  1       /* looking for the start of a basic block */
                     14: #define SINST  2       /* thinking about generating a tally */
                     15: #define SPRO   3       /* thinking about generating prolog code */
                     16: #define SMAYBE 4       /* seen _, thinking about SPRO */
                     17: 
                     18: #define MAXLA  5       /* max no. of lines that can be read for lookahead */
                     19: 
                     20: FILE *outs, *fd, *outl;        /* outs goes to assembler, outl is for listing */
                     21: extern FILE *popen();
                     22: char line[256], fname[256];    /* file names must fit in fname */
                     23: 
                     24: char labuf[5][256];            /* look ahead needed for 5.0 stab entries */
                     25: int lastla, lookahead;
                     26: char begfcn[] = "      .def    .bf";   /* 5.0 stab entry */
                     27: 
                     28: int lineno, lastline;
                     29: int base=0;
                     30: 
                     31: int cnt;
                     32: int state = SBBLK;
                     33: char *ptr, *curarg;
                     34: char curdir[256];
                     35: 
                     36: main(argc, argv)
                     37: char **argv;
                     38: {      int i;
                     39:        if(argc <= 1) {
                     40:                fprintf(stderr, "no files given\n");
                     41:                exit(1);
                     42:        }
                     43:        for(i = 0; insts[i].iname; i++)
                     44:                ;
                     45:        for(; i >= 0; i--)
                     46:                index[insts[i].iname[0]] = insts + i;
                     47:        fd = popen("pwd", "r");
                     48:        for(i = 0; i < sizeof(curdir) && !feof(fd); i++)
                     49:                curdir[i] = getc(fd);
                     50:        curdir[i-2] = 0;        /* thisdir\n */
                     51:        fclose(fd);
                     52:        for(i = 1; i < argc; i++) {
                     53:                if(setfd(argv[i]))      /* fix fd, outs, outl */
                     54:                        doarg();        /* do the work */
                     55:        }
                     56:        exit(0);
                     57: }
                     58: 
                     59: setfd(s)
                     60: char *s;
                     61: {      char outnams[24], outnaml[24];
                     62:        fname[0] = 0;
                     63:        cnt = 3;
                     64:        if(fd != NULL)
                     65:                fclose(fd);
                     66:        if(outs != NULL)
                     67:                fclose(outs);
                     68:        if(outl != NULL)
                     69:                fclose(outl);
                     70:        sprintf(outnams, "X%s", s);
                     71:        sprintf(outnaml, "%sL", s);
                     72:        lastline = lineno = 0;
                     73:        fd = fopen(s, "r");
                     74:        if(fd == NULL) {
                     75:                perror(s);
                     76:                return(0);
                     77:        }
                     78:        outs = fopen(outnams, "w");
                     79:        if(outs == NULL) {
                     80:                perror(outnams);
                     81:                return(0);
                     82:        }
                     83:        outl = fopen(outnaml, "w");
                     84:        if(outl == NULL) {
                     85:                perror(outnaml);
                     86:                return(0);
                     87:        }
                     88:        curarg = s;
                     89:        return(1);
                     90: }
                     91: 
                     92: doarg()
                     93: {      struct inst *x, *firstword();
                     94: 
                     95:        state = SBBLK;
                     96:        lookahead = 0;          /* empty buffer */
                     97:        lastla = 0;
                     98:        for(;;) {
                     99:                if (lookahead != lastla){
                    100:                        strcpy(line,labuf[ (lookahead++) % MAXLA ] );
                    101:                        if (lookahead == lastla) lookahead = lastla = 0;
                    102:                }
                    103:                else
                    104:                        (void) fgets(line, sizeof(line), fd);
                    105:                if(feof(fd))
                    106:                        break;
                    107:                for(ptr = line; isspace(*ptr); *ptr++)
                    108:                        ;
                    109:                if(*ptr == 0 || *ptr == '#')
                    110:                        continue;
                    111:                testlabel();
                    112:                /* deal with symbol table info */
                    113:                if(*ptr == '.') {
                    114:                        stab();
                    115:                        fprintf(outs, " %s", ptr);
                    116:                        continue;
                    117:                }
                    118:                if(*ptr == 0 || *ptr == '\n')
                    119:                        continue;
                    120:                x = firstword();
                    121:                if(x == 0){
                    122:                        printf("unknown inst: %s\n",ptr);
                    123:                        continue;
                    124:                }
                    125: #ifdef u3b
                    126:                if ((state == SMAYBE) && !strncmp(ptr,"save",4)){
                    127:                        state = SPRO;
                    128:                        getlnum();      /* 5.0 true line # */
                    129:                }
                    130: #endif
                    131:                if(state == SPRO)
                    132:                        prolog(x);
                    133:                if(state == SINST)
                    134:                        tally(x);
                    135:                if(state == SBBLK && (x->type & JUMP))
                    136:                        state = SINST;
                    137:                outinstr();
                    138:                if(x->type & BYTE)
                    139:                        fixinstr(x);
                    140:                fprintf(outs, " %s", ptr);
                    141:        }
                    142:        finish();
                    143: }
                    144: 
                    145: 
                    146: getlnum(){     /* get true line number from 5.0 sdb info */
                    147:        char *la;
                    148:        int l;
                    149: 
                    150:        /* get true line number by looking ahead */
                    151:        /* this is necessary for 5.0 sdb output  */
                    152:        do
                    153:                (void) fgets(labuf[l = ((lastla++)%MAXLA)], sizeof(line), fd);
                    154:        while (!feof(fd) && strncmp(begfcn, labuf[l],
                    155:                        9) && (lookahead != (lastla)%MAXLA ));
                    156:        if ( strncmp(begfcn, labuf[l], 9) ) 
                    157:                return;         /* bad input  or not 5.0 */
                    158:        la = labuf[l];
                    159:        while ( *la != '\0' ){
                    160:                while ( *la != ';' && *la != '\0') la++;
                    161:                if ( *la == ';' ) la++;
                    162:                while ( isspace(*la) ) la++;
                    163:                if ( !strncmp(la, ".line" , 5) ){
                    164:                        lineno = base = atoi( la + 5 );
                    165:                        return;
                    166:                }
                    167:        }
                    168: }
                    169: 
                    170: 
                    171: /* unbelievable variability in sdb info */
                    172: stab()
                    173: {      char buf[128];
                    174:        int i, j, k;
                    175: 
                    176: #ifndef u3b
                    177:        if(state == SMAYBE && strncmp(ptr, ".word", 5) == 0) {
                    178:                state = SPRO;
                    179:                getlnum();
                    180:                return;
                    181:        }
                    182: #endif
                    183: 
                    184:        if((i = *(ptr + 1)) != 's' && i != 'f' && i != 'l')
                    185:                return;
                    186:        /* real compiler output */
                    187:        if(sscanf(ptr, ".stabs \"%[^\"]\", %o", buf, &i) == 2 && i == 0144)
                    188:                strcat(fname, buf);
                    189:        else if(sscanf(ptr, ".stabd %o,%o,%o", &i, &j, &k) == 3 && i == 0104)
                    190:                lineno = k;
                    191:        /* pwb 3.0 */
                    192:        else if(sscanf(ptr, ".stab %[^,],%[^,],%[^,],%[^,],%[^,],%[^,],%[^,],%[^,],%d",
                    193:                buf, buf+2, buf+4, buf+6, buf+8, buf+10, buf+12, buf+14, &i)
                    194:                == 9 && i == 144) {
                    195:                        for(i = k = 0; buf[i] ; i++)
                    196:                                if(buf[i] == '\'')
                    197:                                        buf[k++] = buf[++i];
                    198:                        buf[k] = 0;
                    199:                        strcat(fname, buf);
                    200:                }
                    201:        else if(sscanf(ptr, ".stab %[0,]%d,%d,%d", buf,&i, &j, &k) == 4
                    202:                && i == 104)
                    203:                lineno = k;
                    204:        /* pwb 5.0 */
                    205:        else if(sscanf(ptr," .file \"%[^\"]\"", buf) == 1)
                    206:                strcat(fname, buf);
                    207:        else if(sscanf(ptr, " .ln %d",&k) == 1)
                    208:                lineno = k + base - 1;
                    209: }
                    210: 
                    211: testlabel()
                    212: {      char *p;
                    213:        int i;
                    214: 
                    215: for(;;){
                    216:        for(p = ptr; *p; p++) {
                    217:                if(*p == ':')
                    218:                        break;
                    219:                if(!isalnum(*p) && *p != '_' && *p != '.')
                    220:                        return;
                    221:        }
                    222:        if(*p == 0)
                    223:                return;
                    224:        *p++ = 0;       /* that is overwriting the : */
                    225:        fprintf(outs, "%s:\n", ptr);
                    226:        if (lineno != lastline){
                    227:                for (i=lastline+1; i < lineno ; i++)    /* allign label with right inst */
                    228:                        fprintf(outl, "0 %s: %d\n", fname, i);
                    229:                lastline = lineno - 1;
                    230:        }
                    231:        fprintf(outl, "%d %s:\n", 4*(cnt - 1), ptr);
                    232: #ifdef u3b
                    233:        if(*ptr != '.')
                    234: #else
                    235:        if(*ptr == '_' )
                    236: #endif
                    237:                state = SMAYBE;
                    238:        else
                    239:                if(state != SPRO)
                    240:                        state = SINST;
                    241:        for(ptr = p; isspace(*ptr); ptr++)
                    242:                ;
                    243:        }       /* L68:L70: ... */
                    244: }
                    245: 
                    246: struct inst *
                    247: firstword()
                    248: {      char buf[sizeof(line)], *p, *q;
                    249:        struct inst *x;
                    250:        for(p = buf, q = ptr; isalnum(*q); )
                    251:                *p++ = *q++;
                    252:        if(p == buf)
                    253:                return((struct inst *)0);
                    254:        *p = 0;
                    255:        for(x = index[buf[0]]; x && x->iname[0] == buf[0]; x++)
                    256:                if(strcmp(buf, x->iname) == 0)
                    257:                        return(x);
                    258:        return(0);
                    259: }
                    260: 
                    261: outinstr()
                    262: {      int i;
                    263:        for(i = lastline + 1; i < lineno; i++)
                    264:                fprintf(outl, "0 %s: %d\n", fname, i);
                    265:        if (lastline != lineno){
                    266:                fprintf(outl, "%d %s: %d\n", 4*(cnt - 1), fname, lineno);
                    267:                lastline = lineno;
                    268:        }
                    269:        fprintf(outl, "%d       %s", 4*(cnt - 1), ptr);
                    270: }
                    271: 
                    272: #ifdef u3b
                    273: 
                    274: /* 3b code  ***********************************/
                    275: tally(x)
                    276: struct inst *x;
                    277: {
                    278:        if(x->type & E)
                    279:                fprintf(outs, " addw2 &1,locprof+%d\n", 4*cnt++);
                    280:        else {
                    281:                fprintf(outs, " stsm &1,savecc\n");
                    282:                fprintf(outs, " gcc %%r0\n");
                    283:                fprintf(outs, " addw2 &1,locprof+%d\n", 4*cnt++);
                    284:                fprintf(outs, " scc %%r0\n");
                    285:                fprintf(outs, " lsm &1,savecc\n");
                    286:        }
                    287:        state = SBBLK;
                    288: }
                    289: 
                    290: prolog(x)      /* no liveness test, presumes can't get here by jump */
                    291: struct inst *x;
                    292: {      int i;
                    293:        fprintf(outs, " .data\n");
                    294:        fprintf(outs, " .globl proFptr\n");     /* the global chain */
                    295:        fprintf(outs, " .globl savecc\n");
                    296:        fprintf(outs, " .text\n");
                    297:        fprintf(outs, " cmpw &0,locprof+4\n");
                    298:        fprintf(outs, " jne L%da\n", i = cnt);
                    299:        fprintf(outs, " movw proFptr,locprof+4\n");
                    300:        fprintf(outs, " movaw locprof,proFptr\n");
                    301:        fprintf(outs, "L%da: addw2 &1,locprof+%d\n", i, 4*cnt++);
                    302:        state = SBBLK;
                    303: }
                    304: 
                    305: finish()
                    306: {      int i;
                    307:        fprintf(outs, " .data\n");
                    308:        fprintf(outs, " .align  4\n");
                    309:        fprintf(outs, "locprof:\n");
                    310:        fprintf(outs, " .word   %d\n", cnt);
                    311:        fprintf(outs, " .word 0\n");
                    312:        fprintf(outs, " .word L%db\n", cnt);
                    313:        fprintf(outs, " .zero %d\n", 4 * cnt);
                    314:        fprintf(outs, "L%db: .byte ", cnt);
                    315:        for(i = 0; curdir[i]; i++)
                    316:                fprintf(outs, " 0x%x,", curdir[i]);
                    317:        fprintf(outs, " 0x%x\n", '/');
                    318:        fprintf(outs, " .byte ");
                    319:        if(fname[0])
                    320:                for(i = 0; fname[i]; i++)
                    321:                        fprintf(outs, " 0x%x,", fname[i]);
                    322:        else
                    323:                for(i = 0; curarg[i]; i++)
                    324:                        fprintf(outs, " 0x%x,", curarg[i]);
                    325:        fprintf(outs, "0\n");
                    326: }
                    327: 
                    328: #else
                    329: 
                    330: /*  Vax code  **************************/
                    331: tally(x)
                    332: struct inst *x;
                    333: {
                    334:        if(x->type & E)
                    335:                fprintf(outs, " incl locprof+%d\n", 4*cnt++);
                    336:        else {
                    337:                fprintf(outs, " movpsl -(sp)\n");
                    338:                fprintf(outs, " incl locprof+%d\n", 4*cnt++);
                    339:                fprintf(outs, " movw (sp)+,(sp)\n");
                    340:                fprintf(outs, " bicpsw $0xff\n");
                    341:                fprintf(outs, " bispsw (sp)+\n");
                    342:                /* thanks to kirk mckusick */
                    343:        }
                    344:        state = SBBLK;
                    345: }
                    346: 
                    347: 
                    348: prolog(x)      /* no liveness test, presumes can't get here by jump */
                    349: struct inst *x;
                    350: {      int i;
                    351:        fprintf(outs, " .data\n");
                    352:        fprintf(outs, " .comm _proFptr,4\n");   /* the global chain */
                    353:        fprintf(outs, " .text\n");
                    354:        /*if(!(x->type & E))
                    355:                fprintf(outs, " movpsl -(sp)\n");*/
                    356:        fprintf(outs, " tstl locprof+4\n");
                    357:        fprintf(outs, " bneq L%da\n", i = cnt);
                    358:        fprintf(outs, " movl _proFptr,locprof+4\n");
                    359:        fprintf(outs, " moval locprof,_proFptr\n");
                    360:        fprintf(outs, "L%da: incl locprof+%d\n", i, 4*cnt++);
                    361:        /*if(!(x->type & E)) {
                    362:                fprintf(outs, " movw (sp)+,(sp)\n");
                    363:                fprintf(outs, " bicpsw $0xff\n");
                    364:                fprintf(outs, " bispsw (sp)+\n");
                    365:        }*/
                    366:        state = SBBLK;
                    367: }
                    368: 
                    369: finish()
                    370: {      int i;
                    371:        fprintf(outs, " .data\n");
                    372:        fprintf(outs, "locprof: .long %d\n", cnt);
                    373:        fprintf(outs, " .long 0\n");
                    374:        fprintf(outs, " .long L%db\n", cnt);
                    375:        fprintf(outs, " .space %d\n", 4 * cnt);
                    376:        fprintf(outs, "L%db: .byte ", cnt);
                    377:        for(i = 0; curdir[i]; i++)
                    378:                fprintf(outs, "0x%x,", curdir[i]);
                    379:        fprintf(outs, "0x%x\n", '/');
                    380:        fprintf(outs, " .byte ");
                    381:        if(fname[0])
                    382:                for(i = 0; fname[i]; i++)
                    383:                        fprintf(outs, "0x%x,", fname[i]);
                    384:        else
                    385:                for(i = 0; curarg[i]; i++)
                    386:                        fprintf(outs, "0x%x,", curarg[i]);
                    387:        fprintf(outs, "0\n");
                    388: }
                    389: 
                    390: #endif
                    391: 
                    392: 
                    393: fixinstr(x)
                    394: struct inst *x;
                    395: {
                    396: #ifndef u3b
                    397:        if(x->iname[0] == 'b')
                    398:                *ptr = 'j';     /* let assembler worry about branches */
                    399:        /* this is where the code for aob and sob goes */
                    400: #endif
                    401: }
                    402: 
                    403: 
                    404: 
                    405: 
                    406: 

unix.superglobalmegacorp.com

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