Annotation of Net2/netiso/xebec/procs.c, revision 1.1.1.3

1.1.1.3 ! root        1: /* procs.c,v 1.2 1993/05/20 05:28:29 cgd Exp */
1.1       root        2: /*
                      3:  * This code is such a kludge that I don't want to put my name on it.
                      4:  * It was a ridiculously fast hack and needs rewriting.
                      5:  * However it does work...
                      6:  */
                      7: 
                      8: #include <stdio.h>
                      9: #include <strings.h>
                     10: #include "malloc.h"
                     11: #include "main.h"
                     12: #include "debug.h"
                     13: #include "sets.h"
                     14: #include "procs.h"
                     15: 
                     16: struct Predicate {
                     17:        int p_index;
                     18:        int p_transno;
                     19:        char *p_str;
                     20:        struct Predicate *p_next;
                     21: };
                     22: 
                     23: struct Stateent {
                     24:        int s_index;
                     25:        int s_newstate;
                     26:        int s_action;
                     27:        struct Stateent *s_next;
                     28: };
                     29: 
                     30: struct Object *SameState = (struct Object *)-1;
                     31: int Index = 0;
                     32: int Nstates = 0;
                     33: int Nevents = 0;
                     34: struct Predicate **Predlist;
                     35: struct Stateent **Statelist;
                     36: extern FILE *astringfile;
                     37: 
                     38: end_events() {
                     39:        int size, part;
                     40:        char *addr;
                     41: 
                     42:        IFDEBUG(X)
                     43:                /* finish estring[], start astring[] */
                     44:        if(debug['X'] < 2 )
                     45:                fprintf(astringfile, "};\n\nchar *%s_astring[] = {\n\"NULLACTION\",\n",
                     46:                        protocol);
                     47:        ENDDEBUG
                     48:        /* NOSTRICT */
                     49:        Statelist = 
                     50:          (struct Stateent **) Malloc((Nstates+1) * sizeof(struct Statent *));
                     51:        /* NOSTRICT */
                     52:        Predlist =  
                     53:          (struct Predicate **) 
                     54:          Malloc ( (((Nevents)<<Eventshift)+Nstates)*sizeof(struct Predicate *) );
                     55: 
                     56:        size = (((Nevents)<<Eventshift)+Nstates)*sizeof(struct Predicate *) ;
                     57:        addr = (char *)Predlist;
                     58:        IFDEBUG(N)
                     59:                fprintf(OUT, "Predlist at 0x%x, sbrk 0x%x bzero size %d at addr 0x%x\n",
                     60:                Predlist, sbrk(0), size, addr);
                     61:        ENDDEBUG
                     62: #define BZSIZE 8192
                     63:        while(size) {
                     64:                part = size>BZSIZE?BZSIZE:size;
                     65:        IFDEBUG(N)
                     66:                fprintf(OUT, "bzero addr 0x%x part %d size %d\n",addr, part, size);
                     67:        ENDDEBUG
                     68:                bzero(addr, part);
                     69:        IFDEBUG(N)
                     70:                fprintf(OUT, "after bzero addr 0x%x part %d size %d\n",addr, part, size);
                     71:        ENDDEBUG
                     72:                addr += part;
                     73:                size -= part;
                     74: 
                     75:        }
                     76:        IFDEBUG(N)
                     77:                fprintf(OUT, "endevents..done \n");
                     78:        ENDDEBUG
                     79: }
                     80: 
                     81: int acttable(f,actstring)
                     82: char *actstring;
                     83: FILE *f;
                     84: {
                     85:        static Actindex = 0;
                     86:        extern FILE *astringfile;
                     87:        extern int pgoption;
                     88: 
                     89:        IFDEBUG(a)
                     90:                fprintf(OUT,"acttable()\n");
                     91:        ENDDEBUG
                     92:        fprintf(f, "case 0x%x: \n", ++Actindex);
                     93: 
                     94:        if(pgoption) {
                     95:                fprintf(f, "asm(\" # dummy statement\");\n");
                     96:                fprintf(f, "asm(\"_Xebec_action_%x: \");\n", Actindex );
                     97:                fprintf(f, "asm(\".data\");\n");
                     98:                fprintf(f, "asm(\".globl _Xebec_action_%x# X profiling\");\n",
                     99:                        Actindex );
                    100:                fprintf(f, "asm(\".long 0 # X profiling\");\n");
                    101:                fprintf(f, "asm(\".text # X profiling\");\n");
                    102:                fprintf(f, "asm(\"cas r0,r15,r0 # X profiling\");\n");
                    103:                fprintf(f, "asm(\"bali r15,mcount   # X profiling\");\n");
                    104:        }
                    105: 
                    106:        fprintf(f, "\t\t%s\n\t\t break;\n", actstring);
                    107:        IFDEBUG(X)
                    108:                if(debug['X']<2) {
                    109:                        register int len = 0;
                    110:                        fputc('"',astringfile);
                    111:                        while(*actstring) {
                    112:                                if( *actstring == '\n' ) {
                    113:                                        fputc('\\', astringfile);
                    114:                                        len++;
                    115:                                        fputc('n', astringfile);
                    116:                                } else if (*actstring == '\\') {
                    117:                                        fputc('\\', astringfile);
                    118:                                        len ++;
                    119:                                        fputc('\\', astringfile);
                    120:                                } else if (*actstring == '\"') {
                    121:                                        fputc('\\', astringfile);
                    122:                                        len ++;
                    123:                                        fputc('\"', astringfile);
                    124:                                } else fputc(*actstring, astringfile);
                    125:                                actstring++;
                    126:                                len++;
                    127:                        }
                    128:                        fprintf(astringfile,"\",\n");
                    129:                        if (len > LINELEN) {
                    130:                                fprintf(stderr, "Action too long: %d\n",len); Exit(-1);
                    131:                        }
                    132:                }
                    133:        ENDDEBUG
                    134: 
                    135:        return(Actindex);
                    136: }
                    137: 
                    138: static int Npred=0, Ndefpred=0, Ntrans=0, Ndefevent=0, Nnulla=0;
                    139: 
                    140: statetable(string, oldstate, newstate, action, event)
                    141: char *string;
                    142: int action;
                    143: struct Object *oldstate, *newstate, *event; 
                    144: {
                    145:        register int different;
                    146: 
                    147:        IFDEBUG(a)
                    148:                fprintf(OUT,"statetable(0x%x, 0x%x,0x%x, 0x%x)\n",
                    149:                        string, oldstate, newstate, action);
                    150:                fprintf(OUT,"statetable(%s, %s,%s, 0x%x)\n",
                    151:                        string, oldstate->obj_name, newstate->obj_name, action);
                    152:        ENDDEBUG
                    153: 
                    154:        if( !action) Nnulla++;
                    155:        if( newstate->obj_kind == OBJ_SET) {
                    156:                fprintf(stderr, "Newstate cannot be a set\n");
                    157:                Exit(-1);
                    158:        }
                    159:        different = (newstate != SameState);
                    160: 
                    161:        (void) predtable( oldstate, event, string,
                    162:                                action, (newstate->obj_number) * different );
                    163:        IFDEBUG(a)
                    164:                fprintf(OUT,"EXIT statetable\n");
                    165:        ENDDEBUG
                    166: }
                    167: 
                    168: stateentry(index, oldstate, newstate, action)
                    169: int index, action;
                    170: int oldstate, newstate; 
                    171: {
                    172:        extern FILE *statevalfile;
                    173: 
                    174:        IFDEBUG(a)
                    175:                fprintf(OUT,"stateentry(0x%x,0x%x,0x%x,0x%x) Statelist@0x%x, val 0x%x\n",
                    176:                        index, oldstate, newstate,action, &Statelist, Statelist);
                    177:        ENDDEBUG
                    178: 
                    179: 
                    180:        fprintf(statevalfile, "{0x%x,0x%x},\n", newstate, action);
                    181: }
                    182: 
                    183: int predtable(os, oe, str, action, newstate)
                    184: struct Object *os, *oe;
                    185: char *str;
                    186: int action, newstate;
                    187: {
                    188:        register struct Predicate *p, **q;
                    189:        register int event, state;
                    190:        register struct Object *e, *s;
                    191:        struct Object *firste;
                    192: 
                    193:        if (oe == (struct Object *)0 ) {
                    194:                Ndefevent ++;
                    195:                fprintf(stderr, "DEFAULT EVENTS aren't implemented; trans ignored\n");
                    196:                return;
                    197:        }
                    198:        Ntrans++;
                    199:        IFDEBUG(g)
                    200:                fprintf(stdout,
                    201:                "PREDTAB: s %5s;  e %5s\n", os->obj_kind==OBJ_SET?"SET":"item",
                    202:                        oe->obj_kind==OBJ_SET?"SET":"item");
                    203:        ENDDEBUG
                    204:        if (os->obj_kind == OBJ_SET) s = os->obj_members;
                    205:        else s = os;
                    206:        if (oe->obj_kind == OBJ_SET) firste = oe->obj_members;
                    207:        else firste = oe;
                    208:        if(newstate) {
                    209:                fprintf(statevalfile, "{0x%x,0x%x},\n",newstate, action);
                    210:                Index++;
                    211:        }
                    212:        while (s) {
                    213:                if( !newstate ) { /* !newstate --> SAME */
                    214:                        /* i.e., use old obj_number */
                    215:                        fprintf(statevalfile, "{0x%x,0x%x},\n",s->obj_number, action);
                    216:                        Index++;
                    217:                }
                    218:                e = firste;
                    219:                while (e) {
                    220:                        event = e->obj_number; state = s->obj_number;
                    221:                        IFDEBUG(g)
                    222:                                fprintf(stdout,"pred table event=0x%x, state 0x%x\n",
                    223:                                event, state);
                    224:                                fflush(stdout);
                    225:                        ENDDEBUG
                    226:                        if( !str /* DEFAULT PREDICATE */) {
                    227:                                Ndefpred++;
                    228:                                IFDEBUG(g)
                    229:                                        fprintf(stdout,
                    230:                                        "DEFAULT pred state 0x%x, event 0x%x, Index 0x%x\n",
                    231:                                        state, event, Index);
                    232:                                        fflush(stdout);
                    233:                                ENDDEBUG
                    234:                        } else 
                    235:                                Npred++;
                    236:                        /* put at END of list */
                    237: #ifndef LINT
                    238:                        IFDEBUG(g)
                    239:                                fprintf(stdout, 
                    240:                                "predicate for event 0x%x, state 0x%x is 0x%x, %s\n", 
                    241:                                event, state, Index, str);
                    242:                                fflush(stdout);
                    243:                        ENDDEBUG
                    244: #endif LINT
                    245:                        for( ((q = &Predlist[(event<<Eventshift)+state]), 
                    246:                                         (p = Predlist[(event<<Eventshift)+state]));
                    247:                                                        p ; p = p->p_next ) {
                    248:                                q = &p->p_next;
                    249:                        }
                    250: 
                    251:                        p = (struct Predicate *)Malloc(sizeof(struct Predicate));
                    252:                        p->p_next = (struct Predicate *)0;
                    253:                        p->p_str = str;
                    254:                        p->p_index = Index;
                    255:                        p->p_transno = transno;
                    256:                        *q = p;
                    257: 
                    258:                        IFDEBUG(g)
                    259:                                fprintf(stdout, 
                    260:                                  "predtable index 0x%x, transno %d, E 0x%x, S 0x%x\n",
                    261:                                         Index, transno, e, s);
                    262:                        ENDDEBUG
                    263: 
                    264:                        e = e->obj_members;
                    265:                }
                    266:                s = s->obj_members;
                    267:        }
                    268:        return Index ;
                    269: }
                    270: 
                    271: printprotoerrs()
                    272: {
                    273:        register int e,s;
                    274: 
                    275:        fprintf(stderr, "[ Event, State ] without any transitions :\n");
                    276:        for(e = 0; e < Nevents; e++) { 
                    277:                fprintf(stderr, "Event 0x%x: states ", e);
                    278:                for(s = 0; s < Nstates; s++) {
                    279:                        if( Predlist[(e<<Eventshift)+s] == 0 )
                    280:                                fprintf(stderr, "0x%x ", s);
                    281:                }
                    282:                fprintf(stderr, "\n");
                    283:        }
                    284: }
                    285: 
                    286: #ifndef LINT
                    287: dump_predtable(f)
                    288: FILE *f;
                    289: {
                    290:        struct Predicate *p;
                    291:        register int e,s, hadapred;
                    292:        int defaultindex;
                    293:        int defaultItrans;
                    294:        extern int bytesmalloced;
                    295:        extern int byteswasted;
                    296: 
                    297: #ifdef notdef
                    298:        fprintf(stdout,
                    299:                " Xebec used %8d bytes of storage, wasted %8d bytes\n", 
                    300:                bytesmalloced, byteswasted);
                    301: #endif notdef
                    302:        fprintf(stdout, 
                    303:                " %8d states\n %8d events\n %8d transitions\n",
                    304:                Nstates, Nevents, Ntrans);
                    305:        fprintf(stdout,
                    306:                " %8d predicates\n %8d default predicates used\n",
                    307:                Npred, Ndefpred);
                    308:        fprintf(stdout,
                    309:                " %8d null actions\n",
                    310:                Nnulla);
                    311: 
                    312:        putdriver(f, 5);
                    313:        for(e = 0; e < Nevents; e++) { for(s = 0; s < Nstates; s++) {
                    314:                p = Predlist[(e<<Eventshift)+s];
                    315:                hadapred=0;
                    316:                defaultindex=0;
                    317:                defaultItrans=0;
                    318:                if(p) {
                    319:                        IFDEBUG(d)
                    320:                                fflush(f);
                    321:                        ENDDEBUG
                    322:                        while(p) {
                    323:                                if(p->p_str) {
                    324:                                        if(!hadapred)
                    325:                                                fprintf(f, "case 0x%x:\n\t", (e<<Eventshift) + s);
                    326:                                        hadapred = 1;
                    327:                                        fprintf(f, "if %s return 0x%x;\n\t else ", 
                    328:                                        p->p_str, p->p_index);
                    329:                                } else {
                    330:                                        if(defaultindex) {
                    331:                                                fprintf(stderr, 
                    332: "\nConflict between transitions %d and %d: duplicate default \n",
                    333:                                                p->p_transno, defaultItrans);
                    334:                                                Exit(-1);
                    335:                                        }
                    336:                                        defaultindex = p->p_index;
                    337:                                        defaultItrans = p->p_transno;
                    338:                                }
                    339:                                p = p->p_next;
                    340:                        }
                    341:                        if( hadapred)  {
                    342:                                fprintf(f, "return 0x%x;\n", defaultindex);
                    343:                        }
                    344:                        IFDEBUG(d)
                    345:                                fflush(f);
                    346:                        ENDDEBUG
                    347:                } 
                    348:                IFDEBUG(g)
                    349:                fprintf(stdout, 
                    350:                "loop: e 0x%x s 0x%x hadapred 0x%x dindex 0x%x for trans 0x%x\n",
                    351:                        e, s, hadapred, defaultindex, defaultItrans);
                    352:                ENDDEBUG
                    353:                if ( hadapred ) {
                    354:                        /* put a -1 in the array  - Predlist is temporary storage */
                    355:                        Predlist[(e<<Eventshift)+s] = (struct Predicate *)(-1);
                    356:                } else {
                    357:                        /* put defaultindex in the array */
                    358:                        /* if defaultindex is zero, then the driver will
                    359:                         * cause an erroraction (same as if no default
                    360:                         * were given and none of the predicates were true;
                    361:                         * also same as if no preds or defaults were given
                    362:                         * for this combo)
                    363:                         */
                    364:                        Predlist[(e<<Eventshift)+s] = (struct Predicate *)(defaultindex);
                    365:                }
                    366:        } }
                    367:        fprintf(f, "default: return 0;\n} /* end switch */\n");
                    368: #ifdef notdef
                    369:        fprintf(f, "/*NOTREACHED*/return 0;\n} /* _Xebec_index() */\n");
                    370: #else notdef
                    371:        fprintf(f, "} /* _Xebec_index() */\n");
                    372: #endif notdef
                    373:        fprintf(f, "static int inx[%d][%d] = { {", Nevents+1,Nstates);
                    374:        for(s = 0; s< Nstates; s++) fprintf(f, "0,"); /* event 0 */
                    375:        fprintf(f, "},\n");
                    376: 
                    377:        for(e = 0; e < Nevents; e++) { 
                    378:                fprintf(f, " {"); 
                    379:                for(s = 0; s < Nstates; s++) {
                    380:                        register struct Predicate *xyz = Predlist[(e<<Eventshift)+s];
                    381:                        /* this kludge is to avoid a lint msg. concerning
                    382:                         * loss of bits 
                    383:                         */
                    384:                        if (xyz == (struct Predicate *)(-1))
                    385:                                fprintf(f, "-1,");
                    386:                        else
                    387:                                fprintf(f, "0x%x,", Predlist[(e<<Eventshift)+s]);
                    388:                }
                    389:                fprintf(f, " },\n"); 
                    390:        }
                    391:        fprintf(f, "};");
                    392: }
                    393: #endif LINT
                    394: 
                    395: char *
                    396: stash(buf)
                    397: char *buf;
                    398: {
                    399:        register int len;
                    400:        register char *c;
                    401: 
                    402:        /* grot */
                    403:        len = strlen(buf);
                    404:        c = Malloc(len+1);
                    405: #ifdef LINT
                    406:        c =
                    407: #endif LINT
                    408:        strcpy(c, buf);
                    409: 
                    410:        IFDEBUG(z)
                    411:                fprintf(stdout,"stash %s at 0x%x\n", c,c);
                    412:        ENDDEBUG
                    413:        return(c);
                    414: }
                    415: 
                    416: #ifdef notdef
                    417: dump_pentry(event,state)
                    418: int event,state;
                    419: {
                    420:        register struct Predicate *p, **q;
                    421: 
                    422:        for( 
                    423:        ((q = &Predlist[(event<<Eventshift) +state]), 
                    424:         (p = Predlist[(event<<Eventshift) + state]));
                    425:                p!= (struct Predicate *)0 ; p = p->p_next ) {
                    426: #ifndef LINT
                    427:                IFDEBUG(a)
                    428:                        fprintf(OUT, 
                    429:                        "dump_pentry for event 0x%x, state 0x%x is 0x%x\n", 
                    430:                         event, state, p);
                    431:                ENDDEBUG
                    432: #endif LINT
                    433:                q = &p->p_next;
                    434:        }
                    435: }
                    436: #endif notdef

unix.superglobalmegacorp.com

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