Annotation of researchv10no/cmd/kasb/kas1.c, revision 1.1.1.1

1.1       root        1: /* @(#)kas1.c  1.2 */
                      2: #undef vax
                      3: #define        pdp11   1
                      4: #include <stdio.h>
                      5: #include "a.out.h"
                      6: #include <sys/kmc.h>
                      7: #include "kas.h"
                      8: 
                      9: FILE   *txtfil;
                     10: int    hshused;
                     11: int    lineno  = 1;
                     12: struct symtab *nextsym = {symtab};
                     13: #ifdef vax
                     14: struct filehdr hdr;
                     15: struct aouthdr aout;
                     16: struct scnhdr scn;
                     17: struct syment sym;
                     18: #endif
                     19: #ifdef pdp11
                     20: struct exec hdr;
                     21: #endif
                     22: extern char yytext[];
                     23: 
                     24: main(argc, argv)
                     25: char **argv;
                     26: {
                     27:        register struct symtab *sp, *ip;
                     28:        int c;
                     29:        register struct symtab **hp;
                     30:        int infound = 0;
                     31: 
                     32:        outfile = "a.out";
                     33:        while (argc > 1) {
                     34:                if (argv[1][0]=='-' && argv[1][1]=='o') {
                     35:                        if (argc <3) {
                     36:                                fprintf(stderr, "missing -o file\n");
                     37:                                exit(1);
                     38:                        }
                     39:                        outfile = argv[2];
                     40:                        argc -= 2;
                     41:                        argv += 2;
                     42:                        continue;
                     43:                }
                     44:                if (argv[1][0]=='-' && argv[1][1]=='d') {
                     45:                        if (argc <3) {
                     46:                                fprintf(stderr, "missing -d file");
                     47:                                exit(1);
                     48:                        }
                     49:                        dbfile = argv[2];
                     50:                        dbmode++;
                     51:                        if ((dbfd = open(dbfile, 2))<0) {
                     52:                                fprintf(stderr, "can not open %s\n", dbfile);
                     53:                                exit(2);
                     54:                        }
                     55:                        signal(2,1);
                     56:                        argc -= 2;
                     57:                        argv += 2;
                     58:                        continue;
                     59:                }
                     60:                if (infound) {
                     61:                        fprintf(stderr, "too many arguments\n");
                     62:                        exit(1);
                     63:                }
                     64:                infound++;
                     65:                infile = argv[1];
                     66:                if (freopen(infile, "r", stdin) == NULL) {
                     67:                        fprintf(stderr, "can not open %s\n", infile);
                     68:                        exit(2);
                     69:                }
                     70:                argc--;
                     71:                argv++;
                     72:        }
                     73:        for (ip=instab; ip->name[0]!=0; ip++) {
                     74:                register char *p1, *p2;
                     75:                for (p1=ip->name,p2=yytext; p2<yytext+NCPS;)
                     76:                        *p2++ = *p1++;
                     77:                *p2++ = 0;
                     78:                hp = lookup(0);
                     79:                if (*hp==NULL) {
                     80:                        *hp = ip;
                     81:                }
                     82:        }
                     83:        yytext[0] = '.'; yytext[1] = '\0';
                     84:        dot = *lookup(1);
                     85:        yyparse();
                     86:        if (dbmode)
                     87:                exit(0);
                     88:        txtfil = fopen(outfile, "w");
                     89:        if (txtfil==NULL) {
                     90:                fprintf(stderr, "can not create %s\n", outfile);
                     91:                exit(1);
                     92:        }
                     93: #ifdef vax
                     94:        hdr.f_magic = VAXROMAGIC;
                     95:        hdr.f_nscns = 2;
                     96:        aout.magic = 0410;
                     97:        aout.tsize = tsize*2;
                     98:        aout.dsize = (dsize+1)&~1;
                     99:        aout.bsize = 0;
                    100:        hdr.f_timdat = time(NULL);
                    101:        hdr.f_symptr = sizeof(struct filehdr)
                    102:                 + (2 * sizeof(struct scnhdr))
                    103:                 + sizeof(struct aouthdr)
                    104:                 + aout.tsize
                    105:                 + aout.dsize ;
                    106:        hdr.f_nsyms = nextsym-symtab;
                    107:        hdr.f_opthdr = sizeof(struct aouthdr);
                    108:        hdr.f_flags = F_AR32WR|F_LNNO|F_RELFLG;
                    109:        fwrite(&hdr, sizeof hdr, 1, txtfil);
                    110:        fwrite(&aout, sizeof aout, 1, txtfil);
                    111:        strncpy(scn.s_name,_TEXT,sizeof(scn.s_name));
                    112:        scn.s_size = aout.tsize;
                    113:        scn.s_scnptr = sizeof(struct filehdr) + sizeof(struct aouthdr) +
                    114:                (2 * sizeof(struct scnhdr));
                    115:        fwrite(&scn, sizeof scn, 1, txtfil);
                    116:        strncpy(scn.s_name,_DATA,sizeof(scn.s_name));
                    117:        scn.s_size = aout.dsize;
                    118:        scn.s_scnptr += aout.tsize;
                    119:        fwrite(&scn, sizeof scn, 1, txtfil);
                    120:        fwrite(ispace, aout.tsize, 1, txtfil);
                    121:        fwrite(dspace, aout.dsize, 1, txtfil);
                    122: #endif
                    123: #ifdef pdp11
                    124:        hdr.a_magic = 0410;
                    125:        hdr.a_text = tsize*2;
                    126:        hdr.a_data = (dsize+1)&~1;
                    127:        hdr.a_syms = (int)nextsym-(int)symtab;
                    128:        hdr.a_flag = 1;
                    129:        fwrite(&hdr, sizeof hdr, 1, txtfil);
                    130:        fwrite(ispace, hdr.a_text, 1, txtfil);
                    131:        fwrite(dspace, hdr.a_data, 1, txtfil);
                    132: #endif
                    133:        for (sp=symtab; sp<nextsym; sp++) {
                    134: #ifdef pdp11
                    135:                sp->type >>= 12;
                    136: #endif
                    137: #ifdef vax
                    138:                strncpy(sym.n_name, sp->name, SYMNMLEN);
                    139:                sym.n_value = sp->value;
                    140:                if (sp->type == XTEXT)
                    141:                        sym.n_scnum = 1;
                    142:                else
                    143:                        sym.n_scnum = 2;
                    144:                sym.n_sclass = C_EXT;
                    145:                fwrite(&sym, 1, SYMESZ, txtfil);
                    146: #endif
                    147:                if (sp->type == XUNDEF)
                    148:                        fprintf(stderr, "%.8s undefined\n", sp->name);
                    149:        }
                    150: #ifdef pdp11
                    151:        fwrite(symtab, sizeof(symtab[0]), nextsym-symtab, txtfil);
                    152: #endif
                    153:        exit(anyerrs!=0);
                    154: }
                    155: 
                    156: struct symtab **
                    157: lookup(instflg)
                    158: {
                    159:        int ihash;
                    160:        register struct symtab **hp;
                    161:        register char *p1, *p2;
                    162: 
                    163:        ihash = 0;
                    164:        p1 = yytext;
                    165:        while (*p1) {
                    166:                ihash <<= 1;
                    167:                ihash += *p1++;
                    168:        }
                    169:        ihash &= 077777;
                    170:        while (p1<yytext+NCPS)
                    171:                *p1++ = 0;
                    172:        hp = &hshtab[ihash%NHASH];
                    173:        while (*hp) {
                    174:                p2 = (*hp)->name;
                    175:                for (p1=yytext; p1<yytext+NCPS;)
                    176:                        if (*p1++ != *p2++)
                    177:                                goto no;
                    178:                return(hp);
                    179:        no:
                    180:                if (++hp >= &hshtab[NHASH])
                    181:                        hp = hshtab;
                    182:        }
                    183:        if(++hshused >= NHASH) {
                    184:                yyerror("Symbol table overflow");
                    185:                exit(2);
                    186:        }
                    187:        if (instflg) {
                    188:                for (p1=yytext,p2=nextsym->name; p1<yytext+NCPS;)
                    189:                        *p2++ = *p1++;
                    190:                *hp = nextsym++;
                    191:        }
                    192:        return(hp);
                    193: }
                    194: 
                    195: cksrc(s, d)
                    196: {
                    197:        switch(d&(7<<8)) {
                    198:        case DSTREGH:
                    199:        case DSTREGL:
                    200:        case DSTREG:
                    201:        case DSTBREG:
                    202:                if ((s&017) != (d&017)) {
                    203:                        yyerror("illegal source");
                    204:                        return(d);
                    205:                }
                    206:        }
                    207:        return(s|d);
                    208: }
                    209: 
                    210: ckreg(s, d)
                    211: {
                    212:        switch(d&(7<<8)) {
                    213:        case DSTREGH:
                    214:        case DSTREGL:
                    215:                if ((s&017) != 0) {
                    216:                        yyerror("illegal source");
                    217:                        return(d);
                    218:                }
                    219:                break;
                    220:        case DSTREG:
                    221:        case DSTBREG:
                    222:                if ((s&017) != (d&017)) {
                    223:                        yyerror("illegal source");
                    224:                        return(d);
                    225:                }
                    226:        }
                    227:        return(s|d);
                    228: }
                    229: 
                    230: ckdst(a, b)
                    231: {
                    232:        if ((a&DSTMARI) && (b&DSTMARI)) {
                    233:                yyerror("illegal destination");
                    234:                return(a);
                    235:        }
                    236:        if ((a&DSTBREG) && (b&DSTBREG)) {
                    237:                if ((((a&DSTBREG)!=DSTBRG) || ((b&DSTBREG)!=DSTREG))
                    238:                 && (((b&DSTBREG)!=DSTBRG) || ((a&DSTBREG)!=DSTREG))) {
                    239:                        yyerror("illegal destination");
                    240:                        return(a);
                    241:                }
                    242:        }
                    243:        return(a|b);
                    244: }
                    245: 
                    246: putins(ins)
                    247: {
                    248:        char    csr[8];
                    249:        register p;
                    250: 
                    251:        p = dot->value++;
                    252:        if (dot->value==NKMCI) {
                    253:                dot->value = 0;
                    254:        }
                    255:        if ((p&01777)==01777) {
                    256:                yyerror("warning: text 1k boundary crossing");
                    257:        }
                    258:        if (dbmode) {
                    259:                if (reloc[p]&RLCBR) {
                    260:                        yyerror("undefined trace instruction");
                    261:                        reloc[p] = 0;
                    262:                        return;
                    263:                }
                    264:                if (ksty(KMS, csr, ins)==0)
                    265:                        fprintf(stdout, "%4o%4o%4o%4o%7o\n",
                    266:                        csr[2]&0377,csr[3]&0377,csr[4]&0377,csr[5]&0377,ins);
                    267:                return;
                    268:        }
                    269:        if (reloc[p]&RLUSE) {
                    270:                yyerror("text location redefined");
                    271:                return;
                    272:        }
                    273:        ispace[p] = ins;
                    274:        reloc[p] |= RLUSE;
                    275:        if (++p>tsize)
                    276:                tsize = p;
                    277: }
                    278: 
                    279: putdat(ins)
                    280: {
                    281:        register p;
                    282: 
                    283:        p = dot->value++;
                    284:        if (dot->value==NKMCD)
                    285:                dot->value = 0;
                    286:        if (dbmode) {
                    287:                lseek(dbfd, (long)(p+NKMCI*2), 0);
                    288:                write(dbfd, &ins, 1);
                    289:                return;
                    290:        }
                    291:        if (dspace[p]) {
                    292:                yyerror("data location redefined");
                    293:                return;
                    294:        }
                    295:        dspace[p] = ins;
                    296:        if (++p>dsize)
                    297:                dsize = p;
                    298: }
                    299: 
                    300: backup(sp, val)
                    301: register struct symtab *sp;
                    302: {
                    303:        register backp, next;
                    304: 
                    305:        for (backp = sp->value; backp; backp = (reloc[next]&RLBUP)) {
                    306:                next = backp&RLNEXT;
                    307:                switch(backp&RLCMASK) {
                    308:                case RLCBR:
                    309:                        if ((next&~01777) != (val&~01777))
                    310:                                yyerror("forward branch error");
                    311:                        ispace[next] |= (val&01400)<<3;
                    312:                case RLCMV:
                    313:                        ispace[next] |= val&0377;
                    314:                        break;
                    315:                case RLCPOP:
                    316:                        ispace[next] |= (val>>8)&0377;
                    317:                        break;
                    318:                case RLCPG:
                    319:                        ispace[next] |= (val&01400)<<3;
                    320:                        break;
                    321:                }
                    322:        }
                    323:        sp->value = val;
                    324: }

unix.superglobalmegacorp.com

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