sound/hda: Fix name for the Wildcat Point-LP (Broadwell PCH audio) model.
[dragonfly.git] / contrib / byacc / lr0.c
blob162d1067d8b2b015c00bb5502094641c864f8e65
1 /* $Id: lr0.c,v 1.16 2014/04/07 21:53:50 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 *redset;
34 static Value_t *shiftset;
36 static Value_t **kernel_base;
37 static Value_t **kernel_end;
38 static Value_t *kernel_items;
40 static void
41 allocate_itemsets(void)
43 Value_t *itemp;
44 Value_t *item_end;
45 int symbol;
46 int i;
47 int count;
48 int max;
49 Value_t *symbol_count;
51 count = 0;
52 symbol_count = NEW2(nsyms, Value_t);
54 item_end = ritem + nitems;
55 for (itemp = ritem; itemp < item_end; itemp++)
57 symbol = *itemp;
58 if (symbol >= 0)
60 count++;
61 symbol_count[symbol]++;
65 kernel_base = NEW2(nsyms, Value_t *);
66 kernel_items = NEW2(count, Value_t);
68 count = 0;
69 max = 0;
70 for (i = 0; i < nsyms; i++)
72 kernel_base[i] = kernel_items + count;
73 count += symbol_count[i];
74 if (max < symbol_count[i])
75 max = symbol_count[i];
78 shift_symbol = symbol_count;
79 kernel_end = NEW2(nsyms, Value_t *);
82 static void
83 allocate_storage(void)
85 allocate_itemsets();
86 shiftset = NEW2(nsyms, Value_t);
87 redset = NEW2(nrules + 1, Value_t);
88 state_set = NEW2(nitems, core *);
91 static void
92 append_states(void)
94 int i;
95 int j;
96 Value_t symbol;
98 #ifdef TRACE
99 fprintf(stderr, "Entering append_states()\n");
100 #endif
101 for (i = 1; i < nshifts; i++)
103 symbol = shift_symbol[i];
104 j = i;
105 while (j > 0 && shift_symbol[j - 1] > symbol)
107 shift_symbol[j] = shift_symbol[j - 1];
108 j--;
110 shift_symbol[j] = symbol;
113 for (i = 0; i < nshifts; i++)
115 symbol = shift_symbol[i];
116 shiftset[i] = get_state(symbol);
120 static void
121 free_storage(void)
123 FREE(shift_symbol);
124 FREE(redset);
125 FREE(shiftset);
126 FREE(kernel_base);
127 FREE(kernel_end);
128 FREE(kernel_items);
129 FREE(state_set);
132 static void
133 generate_states(void)
135 allocate_storage();
136 itemset = NEW2(nitems, Value_t);
137 ruleset = NEW2(WORDSIZE(nrules), unsigned);
138 set_first_derives();
139 initialize_states();
141 while (this_state)
143 closure(this_state->items, this_state->nitems);
144 save_reductions();
145 new_itemsets();
146 append_states();
148 if (nshifts > 0)
149 save_shifts();
151 this_state = this_state->next;
154 free_storage();
157 static Value_t
158 get_state(int symbol)
160 int key;
161 Value_t *isp1;
162 Value_t *isp2;
163 Value_t *iend;
164 core *sp;
165 int found;
166 int n;
168 #ifdef TRACE
169 fprintf(stderr, "Entering get_state(%d)\n", symbol);
170 #endif
172 isp1 = kernel_base[symbol];
173 iend = kernel_end[symbol];
174 n = (int)(iend - isp1);
176 key = *isp1;
177 assert(0 <= key && key < nitems);
178 sp = state_set[key];
179 if (sp)
181 found = 0;
182 while (!found)
184 if (sp->nitems == n)
186 found = 1;
187 isp1 = kernel_base[symbol];
188 isp2 = sp->items;
190 while (found && isp1 < iend)
192 if (*isp1++ != *isp2++)
193 found = 0;
197 if (!found)
199 if (sp->link)
201 sp = sp->link;
203 else
205 sp = sp->link = new_state(symbol);
206 found = 1;
211 else
213 state_set[key] = sp = new_state(symbol);
216 return (sp->number);
219 static void
220 initialize_states(void)
222 unsigned i;
223 Value_t *start_derives;
224 core *p;
226 start_derives = derives[start_symbol];
227 for (i = 0; start_derives[i] >= 0; ++i)
228 continue;
230 p = (core *)MALLOC(sizeof(core) + i * sizeof(Value_t));
231 NO_SPACE(p);
233 p->next = 0;
234 p->link = 0;
235 p->number = 0;
236 p->accessing_symbol = 0;
237 p->nitems = (Value_t) i;
239 for (i = 0; start_derives[i] >= 0; ++i)
240 p->items[i] = rrhs[start_derives[i]];
242 first_state = last_state = this_state = p;
243 nstates = 1;
246 static void
247 new_itemsets(void)
249 Value_t i;
250 int shiftcount;
251 Value_t *isp;
252 Value_t *ksp;
253 Value_t symbol;
255 for (i = 0; i < nsyms; i++)
256 kernel_end[i] = 0;
258 shiftcount = 0;
259 isp = itemset;
260 while (isp < itemsetend)
262 i = *isp++;
263 symbol = ritem[i];
264 if (symbol > 0)
266 ksp = kernel_end[symbol];
267 if (!ksp)
269 shift_symbol[shiftcount++] = symbol;
270 ksp = kernel_base[symbol];
273 *ksp++ = (Value_t) (i + 1);
274 kernel_end[symbol] = ksp;
278 nshifts = shiftcount;
281 static core *
282 new_state(int symbol)
284 unsigned n;
285 core *p;
286 Value_t *isp1;
287 Value_t *isp2;
288 Value_t *iend;
290 #ifdef TRACE
291 fprintf(stderr, "Entering new_state(%d)\n", symbol);
292 #endif
294 if (nstates >= MAXYYINT)
295 fatal("too many states");
297 isp1 = kernel_base[symbol];
298 iend = kernel_end[symbol];
299 n = (unsigned)(iend - isp1);
301 p = (core *)allocate((sizeof(core) + (n - 1) * sizeof(Value_t)));
302 p->accessing_symbol = (Value_t) symbol;
303 p->number = (Value_t) nstates;
304 p->nitems = (Value_t) n;
306 isp2 = p->items;
307 while (isp1 < iend)
308 *isp2++ = *isp1++;
310 last_state->next = p;
311 last_state = p;
313 nstates++;
315 return (p);
318 /* show_cores is used for debugging */
319 #ifdef DEBUG
320 void
321 show_cores(void)
323 core *p;
324 int i, j, k, n;
325 int itemno;
327 k = 0;
328 for (p = first_state; p; ++k, p = p->next)
330 if (k)
331 printf("\n");
332 printf("state %d, number = %d, accessing symbol = %s\n",
333 k, p->number, symbol_name[p->accessing_symbol]);
334 n = p->nitems;
335 for (i = 0; i < n; ++i)
337 itemno = p->items[i];
338 printf("%4d ", itemno);
339 j = itemno;
340 while (ritem[j] >= 0)
341 ++j;
342 printf("%s :", symbol_name[rlhs[-ritem[j]]]);
343 j = rrhs[-ritem[j]];
344 while (j < itemno)
345 printf(" %s", symbol_name[ritem[j++]]);
346 printf(" .");
347 while (ritem[j] >= 0)
348 printf(" %s", symbol_name[ritem[j++]]);
349 printf("\n");
350 fflush(stdout);
355 /* show_ritems is used for debugging */
357 void
358 show_ritems(void)
360 int i;
362 for (i = 0; i < nitems; ++i)
363 printf("ritem[%d] = %d\n", i, ritem[i]);
366 /* show_rrhs is used for debugging */
367 void
368 show_rrhs(void)
370 int i;
372 for (i = 0; i < nrules; ++i)
373 printf("rrhs[%d] = %d\n", i, rrhs[i]);
376 /* show_shifts is used for debugging */
378 void
379 show_shifts(void)
381 shifts *p;
382 int i, j, k;
384 k = 0;
385 for (p = first_shift; p; ++k, p = p->next)
387 if (k)
388 printf("\n");
389 printf("shift %d, number = %d, nshifts = %d\n", k, p->number,
390 p->nshifts);
391 j = p->nshifts;
392 for (i = 0; i < j; ++i)
393 printf("\t%d\n", p->shift[i]);
396 #endif
398 static void
399 save_shifts(void)
401 shifts *p;
402 Value_t *sp1;
403 Value_t *sp2;
404 Value_t *send;
406 p = (shifts *)allocate((sizeof(shifts) +
407 (unsigned)(nshifts - 1) * sizeof(Value_t)));
409 p->number = this_state->number;
410 p->nshifts = (Value_t) nshifts;
412 sp1 = shiftset;
413 sp2 = p->shift;
414 send = shiftset + nshifts;
416 while (sp1 < send)
417 *sp2++ = *sp1++;
419 if (last_shift)
421 last_shift->next = p;
422 last_shift = p;
424 else
426 first_shift = p;
427 last_shift = p;
431 static void
432 save_reductions(void)
434 Value_t *isp;
435 Value_t *rp1;
436 Value_t *rp2;
437 int item;
438 Value_t count;
439 reductions *p;
440 Value_t *rend;
442 count = 0;
443 for (isp = itemset; isp < itemsetend; isp++)
445 item = ritem[*isp];
446 if (item < 0)
448 redset[count++] = (Value_t) - item;
452 if (count)
454 p = (reductions *)allocate((sizeof(reductions) +
455 (unsigned)(count - 1) *
456 sizeof(Value_t)));
458 p->number = this_state->number;
459 p->nreds = count;
461 rp1 = redset;
462 rp2 = p->rules;
463 rend = rp1 + count;
465 while (rp1 < rend)
466 *rp2++ = *rp1++;
468 if (last_reduction)
470 last_reduction->next = p;
471 last_reduction = p;
473 else
475 first_reduction = p;
476 last_reduction = p;
481 static void
482 set_derives(void)
484 Value_t i, k;
485 int lhs;
486 Value_t *rules;
488 derives = NEW2(nsyms, Value_t *);
489 rules = NEW2(nvars + nrules, Value_t);
491 k = 0;
492 for (lhs = start_symbol; lhs < nsyms; lhs++)
494 derives[lhs] = rules + k;
495 for (i = 0; i < nrules; i++)
497 if (rlhs[i] == lhs)
499 rules[k] = i;
500 k++;
503 rules[k] = -1;
504 k++;
507 #ifdef DEBUG
508 print_derives();
509 #endif
512 #ifdef DEBUG
513 void
514 print_derives(void)
516 int i;
517 Value_t *sp;
519 printf("\nDERIVES\n\n");
521 for (i = start_symbol; i < nsyms; i++)
523 printf("%s derives ", symbol_name[i]);
524 for (sp = derives[i]; *sp >= 0; sp++)
526 printf(" %d", *sp);
528 putchar('\n');
531 putchar('\n');
533 #endif
535 static void
536 set_nullable(void)
538 int i, j;
539 int empty;
540 int done_flag;
542 nullable = TMALLOC(char, nsyms);
543 NO_SPACE(nullable);
545 for (i = 0; i < nsyms; ++i)
546 nullable[i] = 0;
548 done_flag = 0;
549 while (!done_flag)
551 done_flag = 1;
552 for (i = 1; i < nitems; i++)
554 empty = 1;
555 while ((j = ritem[i]) >= 0)
557 if (!nullable[j])
558 empty = 0;
559 ++i;
561 if (empty)
563 j = rlhs[-j];
564 if (!nullable[j])
566 nullable[j] = 1;
567 done_flag = 0;
573 #ifdef DEBUG
574 for (i = 0; i < nsyms; i++)
576 if (nullable[i])
577 printf("%s is nullable\n", symbol_name[i]);
578 else
579 printf("%s is not nullable\n", symbol_name[i]);
581 #endif
584 void
585 lr0(void)
587 set_derives();
588 set_nullable();
589 generate_states();
592 #ifdef NO_LEAKS
593 void
594 lr0_leaks(void)
596 if (derives)
598 DO_FREE(derives[start_symbol]);
599 DO_FREE(derives);
601 DO_FREE(nullable);
603 #endif