kernel - Handle spinlock indefinite wait edge case
[dragonfly.git] / contrib / byacc / lr0.c
blob0ac211fd858236f084e46adc0375881edc588497
1 /* $Id: lr0.c,v 1.19 2016/06/07 00:21:53 tom Exp $ */
3 #include "defs.h"
5 static core *new_state(int symbol);
6 static Value_t get_state(int symbol);
7 static void allocate_itemsets(void);
8 static void allocate_storage(void);
9 static void append_states(void);
10 static void free_storage(void);
11 static void generate_states(void);
12 static void initialize_states(void);
13 static void new_itemsets(void);
14 static void save_reductions(void);
15 static void save_shifts(void);
16 static void set_derives(void);
17 static void set_nullable(void);
19 int nstates;
20 core *first_state;
21 shifts *first_shift;
22 reductions *first_reduction;
24 static core **state_set;
25 static core *this_state;
26 static core *last_state;
27 static shifts *last_shift;
28 static reductions *last_reduction;
30 static int nshifts;
31 static Value_t *shift_symbol;
33 static Value_t *rules;
35 static Value_t *redset;
36 static Value_t *shiftset;
38 static Value_t **kernel_base;
39 static Value_t **kernel_end;
40 static Value_t *kernel_items;
42 static void
43 allocate_itemsets(void)
45 Value_t *itemp;
46 Value_t *item_end;
47 int symbol;
48 int i;
49 int count;
50 int max;
51 Value_t *symbol_count;
53 count = 0;
54 symbol_count = NEW2(nsyms, Value_t);
56 item_end = ritem + nitems;
57 for (itemp = ritem; itemp < item_end; itemp++)
59 symbol = *itemp;
60 if (symbol >= 0)
62 count++;
63 symbol_count[symbol]++;
67 kernel_base = NEW2(nsyms, Value_t *);
68 kernel_items = NEW2(count, Value_t);
70 count = 0;
71 max = 0;
72 for (i = 0; i < nsyms; i++)
74 kernel_base[i] = kernel_items + count;
75 count += symbol_count[i];
76 if (max < symbol_count[i])
77 max = symbol_count[i];
80 shift_symbol = symbol_count;
81 kernel_end = NEW2(nsyms, Value_t *);
84 static void
85 allocate_storage(void)
87 allocate_itemsets();
88 shiftset = NEW2(nsyms, Value_t);
89 redset = NEW2(nrules + 1, Value_t);
90 state_set = NEW2(nitems, core *);
93 static void
94 append_states(void)
96 int i;
97 int j;
98 Value_t symbol;
100 #ifdef TRACE
101 fprintf(stderr, "Entering append_states()\n");
102 #endif
103 for (i = 1; i < nshifts; i++)
105 symbol = shift_symbol[i];
106 j = i;
107 while (j > 0 && shift_symbol[j - 1] > symbol)
109 shift_symbol[j] = shift_symbol[j - 1];
110 j--;
112 shift_symbol[j] = symbol;
115 for (i = 0; i < nshifts; i++)
117 symbol = shift_symbol[i];
118 shiftset[i] = get_state(symbol);
122 static void
123 free_storage(void)
125 FREE(shift_symbol);
126 FREE(redset);
127 FREE(shiftset);
128 FREE(kernel_base);
129 FREE(kernel_end);
130 FREE(kernel_items);
131 FREE(state_set);
134 static void
135 generate_states(void)
137 allocate_storage();
138 itemset = NEW2(nitems, Value_t);
139 ruleset = NEW2(WORDSIZE(nrules), unsigned);
140 set_first_derives();
141 initialize_states();
143 while (this_state)
145 closure(this_state->items, this_state->nitems);
146 save_reductions();
147 new_itemsets();
148 append_states();
150 if (nshifts > 0)
151 save_shifts();
153 this_state = this_state->next;
156 free_storage();
159 static Value_t
160 get_state(int symbol)
162 int key;
163 Value_t *isp1;
164 Value_t *isp2;
165 Value_t *iend;
166 core *sp;
167 int found;
168 int n;
170 #ifdef TRACE
171 fprintf(stderr, "Entering get_state(%d)\n", symbol);
172 #endif
174 isp1 = kernel_base[symbol];
175 iend = kernel_end[symbol];
176 n = (int)(iend - isp1);
178 key = *isp1;
179 assert(0 <= key && key < nitems);
180 sp = state_set[key];
181 if (sp)
183 found = 0;
184 while (!found)
186 if (sp->nitems == n)
188 found = 1;
189 isp1 = kernel_base[symbol];
190 isp2 = sp->items;
192 while (found && isp1 < iend)
194 if (*isp1++ != *isp2++)
195 found = 0;
199 if (!found)
201 if (sp->link)
203 sp = sp->link;
205 else
207 sp = sp->link = new_state(symbol);
208 found = 1;
213 else
215 state_set[key] = sp = new_state(symbol);
218 return (sp->number);
221 static void
222 initialize_states(void)
224 unsigned i;
225 Value_t *start_derives;
226 core *p;
228 start_derives = derives[start_symbol];
229 for (i = 0; start_derives[i] >= 0; ++i)
230 continue;
232 p = (core *)MALLOC(sizeof(core) + i * sizeof(Value_t));
233 NO_SPACE(p);
235 p->next = 0;
236 p->link = 0;
237 p->number = 0;
238 p->accessing_symbol = 0;
239 p->nitems = (Value_t)i;
241 for (i = 0; start_derives[i] >= 0; ++i)
242 p->items[i] = rrhs[start_derives[i]];
244 first_state = last_state = this_state = p;
245 nstates = 1;
248 static void
249 new_itemsets(void)
251 Value_t i;
252 int shiftcount;
253 Value_t *isp;
254 Value_t *ksp;
255 Value_t symbol;
257 for (i = 0; i < nsyms; i++)
258 kernel_end[i] = 0;
260 shiftcount = 0;
261 isp = itemset;
262 while (isp < itemsetend)
264 i = *isp++;
265 symbol = ritem[i];
266 if (symbol > 0)
268 ksp = kernel_end[symbol];
269 if (!ksp)
271 shift_symbol[shiftcount++] = symbol;
272 ksp = kernel_base[symbol];
275 *ksp++ = (Value_t)(i + 1);
276 kernel_end[symbol] = ksp;
280 nshifts = shiftcount;
283 static core *
284 new_state(int symbol)
286 unsigned n;
287 core *p;
288 Value_t *isp1;
289 Value_t *isp2;
290 Value_t *iend;
292 #ifdef TRACE
293 fprintf(stderr, "Entering new_state(%d)\n", symbol);
294 #endif
296 if (nstates >= MAXYYINT)
297 fatal("too many states");
299 isp1 = kernel_base[symbol];
300 iend = kernel_end[symbol];
301 n = (unsigned)(iend - isp1);
303 p = (core *)allocate((sizeof(core) + (n - 1) * sizeof(Value_t)));
304 p->accessing_symbol = (Value_t)symbol;
305 p->number = (Value_t)nstates;
306 p->nitems = (Value_t)n;
308 isp2 = p->items;
309 while (isp1 < iend)
310 *isp2++ = *isp1++;
312 last_state->next = p;
313 last_state = p;
315 nstates++;
317 return (p);
320 /* show_cores is used for debugging */
321 #ifdef DEBUG
322 void
323 show_cores(void)
325 core *p;
326 int i, j, k, n;
327 int itemno;
329 k = 0;
330 for (p = first_state; p; ++k, p = p->next)
332 if (k)
333 printf("\n");
334 printf("state %d, number = %d, accessing symbol = %s\n",
335 k, p->number, symbol_name[p->accessing_symbol]);
336 n = p->nitems;
337 for (i = 0; i < n; ++i)
339 itemno = p->items[i];
340 printf("%4d ", itemno);
341 j = itemno;
342 while (ritem[j] >= 0)
343 ++j;
344 printf("%s :", symbol_name[rlhs[-ritem[j]]]);
345 j = rrhs[-ritem[j]];
346 while (j < itemno)
347 printf(" %s", symbol_name[ritem[j++]]);
348 printf(" .");
349 while (ritem[j] >= 0)
350 printf(" %s", symbol_name[ritem[j++]]);
351 printf("\n");
352 fflush(stdout);
357 /* show_ritems is used for debugging */
359 void
360 show_ritems(void)
362 int i;
364 for (i = 0; i < nitems; ++i)
365 printf("ritem[%d] = %d\n", i, ritem[i]);
368 /* show_rrhs is used for debugging */
369 void
370 show_rrhs(void)
372 int i;
374 for (i = 0; i < nrules; ++i)
375 printf("rrhs[%d] = %d\n", i, rrhs[i]);
378 /* show_shifts is used for debugging */
380 void
381 show_shifts(void)
383 shifts *p;
384 int i, j, k;
386 k = 0;
387 for (p = first_shift; p; ++k, p = p->next)
389 if (k)
390 printf("\n");
391 printf("shift %d, number = %d, nshifts = %d\n", k, p->number,
392 p->nshifts);
393 j = p->nshifts;
394 for (i = 0; i < j; ++i)
395 printf("\t%d\n", p->shift[i]);
398 #endif
400 static void
401 save_shifts(void)
403 shifts *p;
404 Value_t *sp1;
405 Value_t *sp2;
406 Value_t *send;
408 p = (shifts *)allocate((sizeof(shifts) +
409 (unsigned)(nshifts - 1) * sizeof(Value_t)));
411 p->number = this_state->number;
412 p->nshifts = (Value_t)nshifts;
414 sp1 = shiftset;
415 sp2 = p->shift;
416 send = shiftset + nshifts;
418 while (sp1 < send)
419 *sp2++ = *sp1++;
421 if (last_shift)
423 last_shift->next = p;
424 last_shift = p;
426 else
428 first_shift = p;
429 last_shift = p;
433 static void
434 save_reductions(void)
436 Value_t *isp;
437 Value_t *rp1;
438 Value_t *rp2;
439 int item;
440 Value_t count;
441 reductions *p;
442 Value_t *rend;
444 count = 0;
445 for (isp = itemset; isp < itemsetend; isp++)
447 item = ritem[*isp];
448 if (item < 0)
450 redset[count++] = (Value_t)-item;
454 if (count)
456 p = (reductions *)allocate((sizeof(reductions) +
457 (unsigned)(count - 1) *
458 sizeof(Value_t)));
460 p->number = this_state->number;
461 p->nreds = count;
463 rp1 = redset;
464 rp2 = p->rules;
465 rend = rp1 + count;
467 while (rp1 < rend)
468 *rp2++ = *rp1++;
470 if (last_reduction)
472 last_reduction->next = p;
473 last_reduction = p;
475 else
477 first_reduction = p;
478 last_reduction = p;
483 static void
484 set_derives(void)
486 Value_t i, k;
487 int lhs;
489 derives = NEW2(nsyms, Value_t *);
490 rules = NEW2(nvars + nrules, Value_t);
492 k = 0;
493 for (lhs = start_symbol; lhs < nsyms; lhs++)
495 derives[lhs] = rules + k;
496 for (i = 0; i < nrules; i++)
498 if (rlhs[i] == lhs)
500 rules[k] = i;
501 k++;
504 rules[k] = -1;
505 k++;
508 #ifdef DEBUG
509 print_derives();
510 #endif
513 #ifdef DEBUG
514 void
515 print_derives(void)
517 int i;
518 Value_t *sp;
520 printf("\nDERIVES\n\n");
522 for (i = start_symbol; i < nsyms; i++)
524 printf("%s derives ", symbol_name[i]);
525 for (sp = derives[i]; *sp >= 0; sp++)
527 printf(" %d", *sp);
529 putchar('\n');
532 putchar('\n');
534 #endif
536 static void
537 set_nullable(void)
539 int i, j;
540 int empty;
541 int done_flag;
543 nullable = TMALLOC(char, nsyms);
544 NO_SPACE(nullable);
546 for (i = 0; i < nsyms; ++i)
547 nullable[i] = 0;
549 done_flag = 0;
550 while (!done_flag)
552 done_flag = 1;
553 for (i = 1; i < nitems; i++)
555 empty = 1;
556 while ((j = ritem[i]) >= 0)
558 if (!nullable[j])
559 empty = 0;
560 ++i;
562 if (empty)
564 j = rlhs[-j];
565 if (!nullable[j])
567 nullable[j] = 1;
568 done_flag = 0;
574 #ifdef DEBUG
575 for (i = 0; i < nsyms; i++)
577 if (nullable[i])
578 printf("%s is nullable\n", symbol_name[i]);
579 else
580 printf("%s is not nullable\n", symbol_name[i]);
582 #endif
585 void
586 lr0(void)
588 set_derives();
589 set_nullable();
590 generate_states();
593 #ifdef NO_LEAKS
594 void
595 lr0_leaks(void)
597 if (derives)
599 if (derives[start_symbol] != rules)
601 DO_FREE(derives[start_symbol]);
603 DO_FREE(derives);
604 DO_FREE(rules);
606 DO_FREE(nullable);
608 #endif