home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / bsd_srcs / sys / netiso / xebec / procs.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-10-05  |  10.1 KB  |  438 lines

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