1 /* $NetBSD: procs.c,v 1.12 2007/01/18 12:43:38 cbiere 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.12 2007/01/18 12:43:38 cbiere 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 %p, sbrk %p bzero size %d at addr %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 %p part %d size %d\n",addr, part, size);
76 ENDDEBUG
77 bzero(addr, part);
78 IFDEBUG(N)
79 fprintf(OUT, "after bzero addr %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(%p, %p,%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(idx, oldstate, newstate, action)
181 int idx, 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@%p, val %p\n",
188 idx, 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 extern FILE *statevalfile;
206
207 if (oe == (struct Object *)0 ) {
208 Ndefevent++;
209 fprintf(stderr, "DEFAULT EVENTS aren't implemented; trans ignored\n");
210 return (-1);
211 }
212 Ntrans++;
213 IFDEBUG(g)
214 fprintf(stdout,
215 "PREDTAB: s %5s; e %5s\n", os->obj_kind==OBJ_SET?"SET":"item",
216 oe->obj_kind==OBJ_SET?"SET":"item");
217 ENDDEBUG
218 if (os->obj_kind == OBJ_SET) s = os->obj_members;
219 else s = os;
220 if (oe->obj_kind == OBJ_SET) firste = oe->obj_members;
221 else firste = oe;
222 if(newstate) {
223 fprintf(statevalfile, "{0x%x,0x%x},\n",newstate, action);
224 Index++;
225 }
226 while (s) {
227 if( !newstate ) { /* !newstate --> SAME */
228 /* i.e., use old obj_number */
229 fprintf(statevalfile, "{0x%x,0x%x},\n",s->obj_number, action);
230 Index++;
231 }
232 e = firste;
233 while (e) {
234 event = e->obj_number; state = s->obj_number;
235 IFDEBUG(g)
236 fprintf(stdout,"pred table event=0x%x, state 0x%x\n",
237 event, state);
238 fflush(stdout);
239 ENDDEBUG
240 if( !str /* DEFAULT PREDICATE */) {
241 Ndefpred++;
242 IFDEBUG(g)
243 fprintf(stdout,
244 "DEFAULT pred state 0x%x, event 0x%x, Index 0x%x\n",
245 state, event, Index);
246 fflush(stdout);
247 ENDDEBUG
248 } else
249 Npred++;
250 /* put at END of list */
251 #ifndef LINT
252 IFDEBUG(g)
253 fprintf(stdout,
254 "predicate for event 0x%x, state 0x%x is 0x%x, %s\n",
255 event, state, Index, str);
256 fflush(stdout);
257 ENDDEBUG
258 #endif /* LINT */
259 for( ((q = &Predlist[(event<<Eventshift)+state]),
260 (p = Predlist[(event<<Eventshift)+state]));
261 p ; p = p->p_next ) {
262 q = &p->p_next;
263 }
264
265 p = (struct Predicate *)Malloc(sizeof(struct Predicate));
266 p->p_next = (struct Predicate *)0;
267 p->p_str = str;
268 p->p_index = Index;
269 p->p_transno = transno;
270 *q = p;
271
272 IFDEBUG(g)
273 fprintf(stdout,
274 "predtable index 0x%x, transno %d, E %p, S %p\n",
275 Index, transno, e, s);
276 ENDDEBUG
277
278 e = e->obj_members;
279 }
280 s = s->obj_members;
281 }
282 return Index ;
283 }
284
285 void
286 printprotoerrs()
287 {
288 register int e,s;
289
290 fprintf(stderr, "[ Event, State ] without any transitions :\n");
291 for(e = 0; e < Nevents; e++) {
292 fprintf(stderr, "Event 0x%x: states ", e);
293 for(s = 0; s < Nstates; s++) {
294 if( Predlist[(e<<Eventshift)+s] == 0 )
295 fprintf(stderr, "0x%x ", s);
296 }
297 fprintf(stderr, "\n");
298 }
299 }
300
301 #ifndef LINT
302 void
303 dump_predtable(f)
304 FILE *f;
305 {
306 struct Predicate *p;
307 register int e,s, hadapred;
308 int defaultindex;
309 int defaultItrans;
310
311 #ifdef notdef
312 extern int bytesmalloced;
313 extern int byteswasted;
314
315 fprintf(stdout,
316 " Xebec used %8d bytes of storage, wasted %8d bytes\n",
317 bytesmalloced, byteswasted);
318 #endif /* notdef */
319 fprintf(stdout,
320 " %8d states\n %8d events\n %8d transitions\n",
321 Nstates, Nevents, Ntrans);
322 fprintf(stdout,
323 " %8d predicates\n %8d default predicates used\n",
324 Npred, Ndefpred);
325 fprintf(stdout,
326 " %8d null actions\n",
327 Nnulla);
328
329 putdriver(f, 5);
330 for(e = 0; e < Nevents; e++) { for(s = 0; s < Nstates; s++) {
331 p = Predlist[(e<<Eventshift)+s];
332 hadapred=0;
333 defaultindex=0;
334 defaultItrans=0;
335 if(p) {
336 IFDEBUG(d)
337 fflush(f);
338 ENDDEBUG
339 while(p) {
340 if(p->p_str) {
341 if(!hadapred)
342 fprintf(f, "case 0x%x:\n\t", (e<<Eventshift) + s);
343 hadapred = 1;
344 fprintf(f, "if %s return 0x%x;\n\t else ",
345 p->p_str, p->p_index);
346 } else {
347 if(defaultindex) {
348 fprintf(stderr,
349 "\nConflict between transitions %d and %d: duplicate default \n",
350 p->p_transno, defaultItrans);
351 Exit(-1);
352 }
353 defaultindex = p->p_index;
354 defaultItrans = p->p_transno;
355 }
356 p = p->p_next;
357 }
358 if( hadapred) {
359 fprintf(f, "return 0x%x;\n", defaultindex);
360 }
361 IFDEBUG(d)
362 fflush(f);
363 ENDDEBUG
364 }
365 IFDEBUG(g)
366 fprintf(stdout,
367 "loop: e 0x%x s 0x%x hadapred 0x%x dindex 0x%x for trans 0x%x\n",
368 e, s, hadapred, defaultindex, defaultItrans);
369 ENDDEBUG
370 if ( hadapred ) {
371 /* put a -1 in the array - Predlist is temporary storage */
372 Predlist[(e<<Eventshift)+s] = (struct Predicate *)(-1);
373 } else {
374 /* put defaultindex in the array */
375 /* if defaultindex is zero, then the driver will
376 * cause an erroraction (same as if no default
377 * were given and none of the predicates were true;
378 * also same as if no preds or defaults were given
379 * for this combo)
380 */
381 Predlist[(e<<Eventshift)+s] = (struct Predicate *)(defaultindex);
382 }
383 } }
384 fprintf(f, "default: return 0;\n} /* end switch */\n");
385 #ifdef notdef
386 fprintf(f, "/*NOTREACHED*/return 0;\n} /* _Xebec_index() */\n");
387 #else /* !notdef */
388 fprintf(f, "} /* _Xebec_index() */\n");
389 #endif /* notdef */
390 fprintf(f, "static int inx[%d][%d] = { {", Nevents+1,Nstates);
391 for(s = 0; s< Nstates; s++) fprintf(f, "0,"); /* event 0 */
392 fprintf(f, "},\n");
393
394 for(e = 0; e < Nevents; e++) {
395 fprintf(f, " {");
396 for(s = 0; s < Nstates; s++) {
397 register struct Predicate *xyz = Predlist[(e<<Eventshift)+s];
398 /* this kludge is to avoid a lint msg. concerning
399 * loss of bits
400 */
401 if (xyz == (struct Predicate *)(-1))
402 fprintf(f, "-1,");
403 else
404 fprintf(f, "%p,", Predlist[(e<<Eventshift)+s]);
405 }
406 fprintf(f, " },\n");
407 }
408 fprintf(f, "};");
409 }
410 #endif /* LINT */
411
412 char *
413 stash(buf)
414 char *buf;
415 {
416 register int len;
417 register char *c;
418
419 /* grot */
420 len = strlen(buf);
421 c = Malloc(len+1);
422 #ifdef LINT
423 c =
424 #endif /* LINT */
425 strcpy(c, buf);
426
427 IFDEBUG(z)
428 fprintf(stdout,"stash %s at %p\n", c,c);
429 ENDDEBUG
430 return(c);
431 }
432
433 #ifdef notdef
434 dump_pentry(event,state)
435 int event,state;
436 {
437 register struct Predicate *p, **q;
438
439 for(
440 ((q = &Predlist[(event<<Eventshift) +state]),
441 (p = Predlist[(event<<Eventshift) + state]));
442 p!= (struct Predicate *)0 ; p = p->p_next ) {
443 #ifndef LINT
444 IFDEBUG(a)
445 fprintf(OUT,
446 "dump_pentry for event 0x%x, state 0x%x is 0x%x\n",
447 event, state, p);
448 ENDDEBUG
449 #endif /* LINT */
450 q = &p->p_next;
451 }
452 }
453 #endif /* notdef */
Cache object: 29520418c9e34123b9844eee19ec49f0
|