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