1 /* $NetBSD: procs.c,v 1.15 2009/03/18 10:22:44 cegger Exp $ */
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...
10 __KERNEL_RCSID(0, "$NetBSD: procs.c,v 1.15 2009/03/18 10:22:44 cegger Exp $");
25 struct Predicate
*p_next
;
32 struct Stateent
*s_next
;
35 struct Object
*SameState
= (struct Object
*)-1;
39 struct Predicate
**Predlist
;
40 struct Stateent
**Statelist
;
41 extern FILE *astringfile
;
52 /* finish estring[], start astring[] */
54 fprintf(astringfile
, "};\n\nchar *%s_astring[] = {\n\"NULLACTION\",\n",
59 (struct Stateent
**) Malloc((Nstates
+1) * sizeof(struct Statent
*));
63 Malloc ( (((Nevents
)<<Eventshift
)+Nstates
)*sizeof(struct Predicate
*) );
65 size
= (((Nevents
)<<Eventshift
)+Nstates
)*sizeof(struct Predicate
*) ;
66 addr
= (char *)Predlist
;
68 fprintf(OUT
, "Predlist at %p, sbrk %p bzero size %d at addr %p\n",
69 Predlist
, sbrk(0), size
, addr
);
73 part
= size
>BZSIZE
?BZSIZE
:size
;
75 fprintf(OUT
, "bzero addr %p part %d size %d\n",addr
, part
, size
);
77 memset(addr
, 0, part
);
79 fprintf(OUT
, "after bzero addr %p part %d size %d\n",addr
, part
, size
);
86 fprintf(OUT
, "endevents..done \n");
91 acttable(FILE *f
,char *actstring
)
93 static int Actindex
= 0;
94 extern FILE *astringfile
;
98 fprintf(OUT
,"acttable()\n");
100 fprintf(f
, "case 0x%x: \n", ++Actindex
);
103 fprintf(f
, "asm(\" # dummy statement\");\n");
104 fprintf(f
, "asm(\"_Xebec_action_%x: \");\n", Actindex
);
105 fprintf(f
, "asm(\".data\");\n");
106 fprintf(f
, "asm(\".globl _Xebec_action_%x# X profiling\");\n",
108 fprintf(f
, "asm(\".long 0 # X profiling\");\n");
109 fprintf(f
, "asm(\".text # X profiling\");\n");
110 fprintf(f
, "asm(\"cas r0,r15,r0 # X profiling\");\n");
111 fprintf(f
, "asm(\"bali r15,mcount # X profiling\");\n");
114 fprintf(f
, "\t\t%s\n\t\t break;\n", actstring
);
117 register int len
= 0;
118 fputc('"',astringfile
);
120 if( *actstring
== '\n' ) {
121 fputc('\\', astringfile
);
123 fputc('n', astringfile
);
124 } else if (*actstring
== '\\') {
125 fputc('\\', astringfile
);
127 fputc('\\', astringfile
);
128 } else if (*actstring
== '\"') {
129 fputc('\\', astringfile
);
131 fputc('\"', astringfile
);
132 } else fputc(*actstring
, astringfile
);
136 fprintf(astringfile
,"\",\n");
138 fprintf(stderr
, "Action too long: %d\n",len
); Exit(-1);
146 static int Npred
=0, Ndefpred
=0, Ntrans
=0, Ndefevent
=0, Nnulla
=0;
149 statetable(char *string
, struct Object
*oldstate
, struct Object
*newstate
, int action
, struct Object
*event
)
151 register int different
;
154 fprintf(OUT
,"statetable(%p, %p,%p, 0x%x)\n",
155 string
, oldstate
, newstate
, action
);
156 fprintf(OUT
,"statetable(%s, %s,%s, 0x%x)\n",
157 string
, oldstate
->obj_name
, newstate
->obj_name
, action
);
160 if( !action
) Nnulla
++;
161 if( newstate
->obj_kind
== OBJ_SET
) {
162 fprintf(stderr
, "Newstate cannot be a set\n");
165 different
= (newstate
!= SameState
);
167 (void) predtable( oldstate
, event
, string
,
168 action
, (newstate
->obj_number
) * different
);
170 fprintf(OUT
,"EXIT statetable\n");
175 stateentry(int idx
, int oldstate
, int newstate
, int action
)
177 extern FILE *statevalfile
;
180 fprintf(OUT
,"stateentry(0x%x,0x%x,0x%x,0x%x) Statelist@%p, val %p\n",
181 idx
, oldstate
, newstate
,action
, &Statelist
, Statelist
);
185 fprintf(statevalfile
, "{0x%x,0x%x},\n", newstate
, action
);
189 predtable(struct Object
*os
, struct Object
*oe
, char *str
, int action
, int newstate
)
191 register struct Predicate
*p
, **q
;
192 register int event
, state
;
193 register struct Object
*e
, *s
;
194 struct Object
*firste
;
195 extern FILE *statevalfile
;
197 if (oe
== (struct Object
*)0 ) {
199 fprintf(stderr
, "DEFAULT EVENTS aren't implemented; trans ignored\n");
205 "PREDTAB: s %5s; e %5s\n", os
->obj_kind
==OBJ_SET
?"SET":"item",
206 oe
->obj_kind
==OBJ_SET
?"SET":"item");
208 if (os
->obj_kind
== OBJ_SET
) s
= os
->obj_members
;
210 if (oe
->obj_kind
== OBJ_SET
) firste
= oe
->obj_members
;
213 fprintf(statevalfile
, "{0x%x,0x%x},\n",newstate
, action
);
217 if( !newstate
) { /* !newstate --> SAME */
218 /* i.e., use old obj_number */
219 fprintf(statevalfile
, "{0x%x,0x%x},\n",s
->obj_number
, action
);
224 event
= e
->obj_number
; state
= s
->obj_number
;
226 fprintf(stdout
,"pred table event=0x%x, state 0x%x\n",
230 if( !str
/* DEFAULT PREDICATE */) {
234 "DEFAULT pred state 0x%x, event 0x%x, Index 0x%x\n",
235 state
, event
, Index
);
240 /* put at END of list */
244 "predicate for event 0x%x, state 0x%x is 0x%x, %s\n",
245 event
, state
, Index
, str
);
249 for( ((q
= &Predlist
[(event
<<Eventshift
)+state
]),
250 (p
= Predlist
[(event
<<Eventshift
)+state
]));
251 p
; p
= p
->p_next
) {
255 p
= (struct Predicate
*)Malloc(sizeof(struct Predicate
));
256 p
->p_next
= (struct Predicate
*)0;
259 p
->p_transno
= transno
;
264 "predtable index 0x%x, transno %d, E %p, S %p\n",
265 Index
, transno
, e
, s
);
280 fprintf(stderr
, "[ Event, State ] without any transitions :\n");
281 for(e
= 0; e
< Nevents
; e
++) {
282 fprintf(stderr
, "Event 0x%x: states ", e
);
283 for(s
= 0; s
< Nstates
; s
++) {
284 if( Predlist
[(e
<<Eventshift
)+s
] == 0 )
285 fprintf(stderr
, "0x%x ", s
);
287 fprintf(stderr
, "\n");
293 dump_predtable(FILE *f
)
296 register int e
,s
, hadapred
;
301 extern int bytesmalloced
;
302 extern int byteswasted
;
305 " Xebec used %8d bytes of storage, wasted %8d bytes\n",
306 bytesmalloced
, byteswasted
);
309 " %8d states\n %8d events\n %8d transitions\n",
310 Nstates
, Nevents
, Ntrans
);
312 " %8d predicates\n %8d default predicates used\n",
315 " %8d null actions\n",
319 for(e
= 0; e
< Nevents
; e
++) { for(s
= 0; s
< Nstates
; s
++) {
320 p
= Predlist
[(e
<<Eventshift
)+s
];
331 fprintf(f
, "case 0x%x:\n\t", (e
<<Eventshift
) + s
);
333 fprintf(f
, "if %s return 0x%x;\n\t else ",
334 p
->p_str
, p
->p_index
);
338 "\nConflict between transitions %d and %d: duplicate default \n",
339 p
->p_transno
, defaultItrans
);
342 defaultindex
= p
->p_index
;
343 defaultItrans
= p
->p_transno
;
348 fprintf(f
, "return 0x%x;\n", defaultindex
);
356 "loop: e 0x%x s 0x%x hadapred 0x%x dindex 0x%x for trans 0x%x\n",
357 e
, s
, hadapred
, defaultindex
, defaultItrans
);
360 /* put a -1 in the array - Predlist is temporary storage */
361 Predlist
[(e
<<Eventshift
)+s
] = (struct Predicate
*)(-1);
363 /* put defaultindex in the array */
364 /* if defaultindex is zero, then the driver will
365 * cause an erroraction (same as if no default
366 * were given and none of the predicates were true;
367 * also same as if no preds or defaults were given
370 Predlist
[(e
<<Eventshift
)+s
] = (struct Predicate
*)(defaultindex
);
373 fprintf(f
, "default: return 0;\n} /* end switch */\n");
375 fprintf(f
, "/*NOTREACHED*/return 0;\n} /* _Xebec_index() */\n");
377 fprintf(f
, "} /* _Xebec_index() */\n");
379 fprintf(f
, "static int inx[%d][%d] = { {", Nevents
+1,Nstates
);
380 for(s
= 0; s
< Nstates
; s
++) fprintf(f
, "0,"); /* event 0 */
383 for(e
= 0; e
< Nevents
; e
++) {
385 for(s
= 0; s
< Nstates
; s
++) {
386 register struct Predicate
*xyz
= Predlist
[(e
<<Eventshift
)+s
];
387 /* this kludge is to avoid a lint msg. concerning
390 if (xyz
== (struct Predicate
*)(-1))
393 fprintf(f
, "%p,", Predlist
[(e
<<Eventshift
)+s
]);
416 fprintf(stdout
,"stash %s at %p\n", c
,c
);
422 dump_pentry(int event
,int state
)
424 register struct Predicate
*p
, **q
;
427 ((q
= &Predlist
[(event
<<Eventshift
) +state
]),
428 (p
= Predlist
[(event
<<Eventshift
) + state
]));
429 p
!= (struct Predicate
*)0 ; p
= p
->p_next
) {
433 "dump_pentry for event 0x%x, state 0x%x is 0x%x\n",