drm - Fix major stalls by fixing an improper taskqueue priority
[dragonfly.git] / contrib / byacc / closure.c
blobf5c3f04d74fd9db19a3ec864b6f919b16c96aa87
1 /* $Id: closure.c,v 1.11 2014/09/18 00:40:07 tom Exp $ */
3 #include "defs.h"
5 Value_t *itemset;
6 Value_t *itemsetend;
7 unsigned *ruleset;
9 static unsigned *first_base;
10 static unsigned *first_derives;
11 static unsigned *EFF;
13 #ifdef DEBUG
14 static void print_closure(int);
15 static void print_EFF(void);
16 static void print_first_derives(void);
17 #endif
19 static void
20 set_EFF(void)
22 unsigned *row;
23 int symbol;
24 Value_t *sp;
25 int rowsize;
26 int i;
27 int rule;
29 rowsize = WORDSIZE(nvars);
30 EFF = NEW2(nvars * rowsize, unsigned);
32 row = EFF;
33 for (i = start_symbol; i < nsyms; i++)
35 sp = derives[i];
36 for (rule = *sp; rule > 0; rule = *++sp)
38 symbol = ritem[rrhs[rule]];
39 if (ISVAR(symbol))
41 symbol -= start_symbol;
42 SETBIT(row, symbol);
45 row += rowsize;
48 reflexive_transitive_closure(EFF, nvars);
50 #ifdef DEBUG
51 print_EFF();
52 #endif
55 void
56 set_first_derives(void)
58 unsigned *rrow;
59 unsigned *vrow;
60 int j;
61 unsigned k;
62 unsigned cword = 0;
63 Value_t *rp;
65 int rule;
66 int i;
67 int rulesetsize;
68 int varsetsize;
70 rulesetsize = WORDSIZE(nrules);
71 varsetsize = WORDSIZE(nvars);
72 first_base = NEW2(nvars * rulesetsize, unsigned);
73 first_derives = first_base - ntokens * rulesetsize;
75 set_EFF();
77 rrow = first_derives + ntokens * rulesetsize;
78 for (i = start_symbol; i < nsyms; i++)
80 vrow = EFF + ((i - ntokens) * varsetsize);
81 k = BITS_PER_WORD;
82 for (j = start_symbol; j < nsyms; k++, j++)
84 if (k >= BITS_PER_WORD)
86 cword = *vrow++;
87 k = 0;
90 if (cword & (unsigned)(1 << k))
92 rp = derives[j];
93 while ((rule = *rp++) >= 0)
95 SETBIT(rrow, rule);
100 rrow += rulesetsize;
103 #ifdef DEBUG
104 print_first_derives();
105 #endif
107 FREE(EFF);
110 void
111 closure(Value_t *nucleus, int n)
113 unsigned ruleno;
114 unsigned word;
115 unsigned i;
116 Value_t *csp;
117 unsigned *dsp;
118 unsigned *rsp;
119 int rulesetsize;
121 Value_t *csend;
122 unsigned *rsend;
123 int symbol;
124 Value_t itemno;
126 rulesetsize = WORDSIZE(nrules);
127 rsend = ruleset + rulesetsize;
128 for (rsp = ruleset; rsp < rsend; rsp++)
129 *rsp = 0;
131 csend = nucleus + n;
132 for (csp = nucleus; csp < csend; ++csp)
134 symbol = ritem[*csp];
135 if (ISVAR(symbol))
137 dsp = first_derives + symbol * rulesetsize;
138 rsp = ruleset;
139 while (rsp < rsend)
140 *rsp++ |= *dsp++;
144 ruleno = 0;
145 itemsetend = itemset;
146 csp = nucleus;
147 for (rsp = ruleset; rsp < rsend; ++rsp)
149 word = *rsp;
150 if (word)
152 for (i = 0; i < BITS_PER_WORD; ++i)
154 if (word & (unsigned)(1 << i))
156 itemno = rrhs[ruleno + i];
157 while (csp < csend && *csp < itemno)
158 *itemsetend++ = *csp++;
159 *itemsetend++ = itemno;
160 while (csp < csend && *csp == itemno)
161 ++csp;
165 ruleno += BITS_PER_WORD;
168 while (csp < csend)
169 *itemsetend++ = *csp++;
171 #ifdef DEBUG
172 print_closure(n);
173 #endif
176 void
177 finalize_closure(void)
179 FREE(itemset);
180 FREE(ruleset);
181 FREE(first_base);
184 #ifdef DEBUG
186 static void
187 print_closure(int n)
189 Value_t *isp;
191 printf("\n\nn = %d\n\n", n);
192 for (isp = itemset; isp < itemsetend; isp++)
193 printf(" %d\n", *isp);
196 static void
197 print_EFF(void)
199 int i, j;
200 unsigned *rowp;
201 unsigned word;
202 unsigned k;
204 printf("\n\nEpsilon Free Firsts\n");
206 for (i = start_symbol; i < nsyms; i++)
208 printf("\n%s", symbol_name[i]);
209 rowp = EFF + ((i - start_symbol) * WORDSIZE(nvars));
210 word = *rowp++;
212 k = BITS_PER_WORD;
213 for (j = 0; j < nvars; k++, j++)
215 if (k >= BITS_PER_WORD)
217 word = *rowp++;
218 k = 0;
221 if (word & (1 << k))
222 printf(" %s", symbol_name[start_symbol + j]);
227 static void
228 print_first_derives(void)
230 int i;
231 int j;
232 unsigned *rp;
233 unsigned cword = 0;
234 unsigned k;
236 printf("\n\n\nFirst Derives\n");
238 for (i = start_symbol; i < nsyms; i++)
240 printf("\n%s derives\n", symbol_name[i]);
241 rp = first_derives + i * WORDSIZE(nrules);
242 k = BITS_PER_WORD;
243 for (j = 0; j <= nrules; k++, j++)
245 if (k >= BITS_PER_WORD)
247 cword = *rp++;
248 k = 0;
251 if (cword & (1 << k))
252 printf(" %d\n", j);
256 fflush(stdout);
259 #endif