xref: /minix/external/bsd/flex/dist/tblcmp.c (revision 0a6a1f1d)
1 /*	$NetBSD: tblcmp.c,v 1.3 2014/10/30 18:44:05 christos Exp $	*/
2 
3 /* tblcmp - table compression routines */
4 
5 /*  Copyright (c) 1990 The Regents of the University of California. */
6 /*  All rights reserved. */
7 
8 /*  This code is derived from software contributed to Berkeley by */
9 /*  Vern Paxson. */
10 
11 /*  The United States Government has rights in this work pursuant */
12 /*  to contract no. DE-AC03-76SF00098 between the United States */
13 /*  Department of Energy and the University of California. */
14 
15 /*  This file is part of flex. */
16 
17 /*  Redistribution and use in source and binary forms, with or without */
18 /*  modification, are permitted provided that the following conditions */
19 /*  are met: */
20 
21 /*  1. Redistributions of source code must retain the above copyright */
22 /*     notice, this list of conditions and the following disclaimer. */
23 /*  2. Redistributions in binary form must reproduce the above copyright */
24 /*     notice, this list of conditions and the following disclaimer in the */
25 /*     documentation and/or other materials provided with the distribution. */
26 
27 /*  Neither the name of the University nor the names of its contributors */
28 /*  may be used to endorse or promote products derived from this software */
29 /*  without specific prior written permission. */
30 
31 /*  THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR */
32 /*  IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED */
33 /*  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR */
34 /*  PURPOSE. */
35 #include "flexdef.h"
36 __RCSID("$NetBSD: tblcmp.c,v 1.3 2014/10/30 18:44:05 christos Exp $");
37 
38 
39 
40 /* declarations for functions that have forward references */
41 
42 void mkentry PROTO ((register int *, int, int, int, int));
43 void mkprot PROTO ((int[], int, int));
44 void mktemplate PROTO ((int[], int, int));
45 void mv2front PROTO ((int));
46 int tbldiff PROTO ((int[], int, int[]));
47 
48 
49 /* bldtbl - build table entries for dfa state
50  *
51  * synopsis
52  *   int state[numecs], statenum, totaltrans, comstate, comfreq;
53  *   bldtbl( state, statenum, totaltrans, comstate, comfreq );
54  *
55  * State is the statenum'th dfa state.  It is indexed by equivalence class and
56  * gives the number of the state to enter for a given equivalence class.
57  * totaltrans is the total number of transitions out of the state.  Comstate
58  * is that state which is the destination of the most transitions out of State.
59  * Comfreq is how many transitions there are out of State to Comstate.
60  *
61  * A note on terminology:
62  *    "protos" are transition tables which have a high probability of
63  * either being redundant (a state processed later will have an identical
64  * transition table) or nearly redundant (a state processed later will have
65  * many of the same out-transitions).  A "most recently used" queue of
66  * protos is kept around with the hope that most states will find a proto
67  * which is similar enough to be usable, and therefore compacting the
68  * output tables.
69  *    "templates" are a special type of proto.  If a transition table is
70  * homogeneous or nearly homogeneous (all transitions go to the same
71  * destination) then the odds are good that future states will also go
72  * to the same destination state on basically the same character set.
73  * These homogeneous states are so common when dealing with large rule
74  * sets that they merit special attention.  If the transition table were
75  * simply made into a proto, then (typically) each subsequent, similar
76  * state will differ from the proto for two out-transitions.  One of these
77  * out-transitions will be that character on which the proto does not go
78  * to the common destination, and one will be that character on which the
79  * state does not go to the common destination.  Templates, on the other
80  * hand, go to the common state on EVERY transition character, and therefore
81  * cost only one difference.
82  */
83 
bldtbl(state,statenum,totaltrans,comstate,comfreq)84 void    bldtbl (state, statenum, totaltrans, comstate, comfreq)
85      int     state[], statenum, totaltrans, comstate, comfreq;
86 {
87 	int     extptr, extrct[2][CSIZE + 1];
88 	int     mindiff, minprot, i, d;
89 
90 	/* If extptr is 0 then the first array of extrct holds the result
91 	 * of the "best difference" to date, which is those transitions
92 	 * which occur in "state" but not in the proto which, to date,
93 	 * has the fewest differences between itself and "state".  If
94 	 * extptr is 1 then the second array of extrct hold the best
95 	 * difference.  The two arrays are toggled between so that the
96 	 * best difference to date can be kept around and also a difference
97 	 * just created by checking against a candidate "best" proto.
98 	 */
99 
100 	extptr = 0;
101 
102 	/* If the state has too few out-transitions, don't bother trying to
103 	 * compact its tables.
104 	 */
105 
106 	if ((totaltrans * 100) < (numecs * PROTO_SIZE_PERCENTAGE))
107 		mkentry (state, numecs, statenum, JAMSTATE, totaltrans);
108 
109 	else {
110 		/* "checkcom" is true if we should only check "state" against
111 		 * protos which have the same "comstate" value.
112 		 */
113 		int     checkcom =
114 
115 			comfreq * 100 > totaltrans * CHECK_COM_PERCENTAGE;
116 
117 		minprot = firstprot;
118 		mindiff = totaltrans;
119 
120 		if (checkcom) {
121 			/* Find first proto which has the same "comstate". */
122 			for (i = firstprot; i != NIL; i = protnext[i])
123 				if (protcomst[i] == comstate) {
124 					minprot = i;
125 					mindiff = tbldiff (state, minprot,
126 							   extrct[extptr]);
127 					break;
128 				}
129 		}
130 
131 		else {
132 			/* Since we've decided that the most common destination
133 			 * out of "state" does not occur with a high enough
134 			 * frequency, we set the "comstate" to zero, assuring
135 			 * that if this state is entered into the proto list,
136 			 * it will not be considered a template.
137 			 */
138 			comstate = 0;
139 
140 			if (firstprot != NIL) {
141 				minprot = firstprot;
142 				mindiff = tbldiff (state, minprot,
143 						   extrct[extptr]);
144 			}
145 		}
146 
147 		/* We now have the first interesting proto in "minprot".  If
148 		 * it matches within the tolerances set for the first proto,
149 		 * we don't want to bother scanning the rest of the proto list
150 		 * to see if we have any other reasonable matches.
151 		 */
152 
153 		if (mindiff * 100 >
154 		    totaltrans * FIRST_MATCH_DIFF_PERCENTAGE) {
155 			/* Not a good enough match.  Scan the rest of the
156 			 * protos.
157 			 */
158 			for (i = minprot; i != NIL; i = protnext[i]) {
159 				d = tbldiff (state, i, extrct[1 - extptr]);
160 				if (d < mindiff) {
161 					extptr = 1 - extptr;
162 					mindiff = d;
163 					minprot = i;
164 				}
165 			}
166 		}
167 
168 		/* Check if the proto we've decided on as our best bet is close
169 		 * enough to the state we want to match to be usable.
170 		 */
171 
172 		if (mindiff * 100 >
173 		    totaltrans * ACCEPTABLE_DIFF_PERCENTAGE) {
174 			/* No good.  If the state is homogeneous enough,
175 			 * we make a template out of it.  Otherwise, we
176 			 * make a proto.
177 			 */
178 
179 			if (comfreq * 100 >=
180 			    totaltrans * TEMPLATE_SAME_PERCENTAGE)
181 					mktemplate (state, statenum,
182 						    comstate);
183 
184 			else {
185 				mkprot (state, statenum, comstate);
186 				mkentry (state, numecs, statenum,
187 					 JAMSTATE, totaltrans);
188 			}
189 		}
190 
191 		else {		/* use the proto */
192 			mkentry (extrct[extptr], numecs, statenum,
193 				 prottbl[minprot], mindiff);
194 
195 			/* If this state was sufficiently different from the
196 			 * proto we built it from, make it, too, a proto.
197 			 */
198 
199 			if (mindiff * 100 >=
200 			    totaltrans * NEW_PROTO_DIFF_PERCENTAGE)
201 					mkprot (state, statenum, comstate);
202 
203 			/* Since mkprot added a new proto to the proto queue,
204 			 * it's possible that "minprot" is no longer on the
205 			 * proto queue (if it happened to have been the last
206 			 * entry, it would have been bumped off).  If it's
207 			 * not there, then the new proto took its physical
208 			 * place (though logically the new proto is at the
209 			 * beginning of the queue), so in that case the
210 			 * following call will do nothing.
211 			 */
212 
213 			mv2front (minprot);
214 		}
215 	}
216 }
217 
218 
219 /* cmptmps - compress template table entries
220  *
221  * Template tables are compressed by using the 'template equivalence
222  * classes', which are collections of transition character equivalence
223  * classes which always appear together in templates - really meta-equivalence
224  * classes.
225  */
226 
cmptmps()227 void    cmptmps ()
228 {
229 	int     tmpstorage[CSIZE + 1];
230 	register int *tmp = tmpstorage, i, j;
231 	int     totaltrans, trans;
232 
233 	peakpairs = numtemps * numecs + tblend;
234 
235 	if (usemecs) {
236 		/* Create equivalence classes based on data gathered on
237 		 * template transitions.
238 		 */
239 		nummecs = cre8ecs (tecfwd, tecbck, numecs);
240 	}
241 
242 	else
243 		nummecs = numecs;
244 
245 	while (lastdfa + numtemps + 1 >= current_max_dfas)
246 		increase_max_dfas ();
247 
248 	/* Loop through each template. */
249 
250 	for (i = 1; i <= numtemps; ++i) {
251 		/* Number of non-jam transitions out of this template. */
252 		totaltrans = 0;
253 
254 		for (j = 1; j <= numecs; ++j) {
255 			trans = tnxt[numecs * i + j];
256 
257 			if (usemecs) {
258 				/* The absolute value of tecbck is the
259 				 * meta-equivalence class of a given
260 				 * equivalence class, as set up by cre8ecs().
261 				 */
262 				if (tecbck[j] > 0) {
263 					tmp[tecbck[j]] = trans;
264 
265 					if (trans > 0)
266 						++totaltrans;
267 				}
268 			}
269 
270 			else {
271 				tmp[j] = trans;
272 
273 				if (trans > 0)
274 					++totaltrans;
275 			}
276 		}
277 
278 		/* It is assumed (in a rather subtle way) in the skeleton
279 		 * that if we're using meta-equivalence classes, the def[]
280 		 * entry for all templates is the jam template, i.e.,
281 		 * templates never default to other non-jam table entries
282 		 * (e.g., another template)
283 		 */
284 
285 		/* Leave room for the jam-state after the last real state. */
286 		mkentry (tmp, nummecs, lastdfa + i + 1, JAMSTATE,
287 			 totaltrans);
288 	}
289 }
290 
291 
292 
293 /* expand_nxt_chk - expand the next check arrays */
294 
expand_nxt_chk()295 void    expand_nxt_chk ()
296 {
297 	register int old_max = current_max_xpairs;
298 
299 	current_max_xpairs += MAX_XPAIRS_INCREMENT;
300 
301 	++num_reallocs;
302 
303 	nxt = reallocate_integer_array (nxt, current_max_xpairs);
304 	chk = reallocate_integer_array (chk, current_max_xpairs);
305 
306 	zero_out ((char *) (chk + old_max),
307 		  (size_t) (MAX_XPAIRS_INCREMENT * sizeof (int)));
308 }
309 
310 
311 /* find_table_space - finds a space in the table for a state to be placed
312  *
313  * synopsis
314  *     int *state, numtrans, block_start;
315  *     int find_table_space();
316  *
317  *     block_start = find_table_space( state, numtrans );
318  *
319  * State is the state to be added to the full speed transition table.
320  * Numtrans is the number of out-transitions for the state.
321  *
322  * find_table_space() returns the position of the start of the first block (in
323  * chk) able to accommodate the state
324  *
325  * In determining if a state will or will not fit, find_table_space() must take
326  * into account the fact that an end-of-buffer state will be added at [0],
327  * and an action number will be added in [-1].
328  */
329 
find_table_space(state,numtrans)330 int     find_table_space (state, numtrans)
331      int    *state, numtrans;
332 {
333 	/* Firstfree is the position of the first possible occurrence of two
334 	 * consecutive unused records in the chk and nxt arrays.
335 	 */
336 	register int i;
337 	register int *state_ptr, *chk_ptr;
338 	register int *ptr_to_last_entry_in_state;
339 
340 	/* If there are too many out-transitions, put the state at the end of
341 	 * nxt and chk.
342 	 */
343 	if (numtrans > MAX_XTIONS_FULL_INTERIOR_FIT) {
344 		/* If table is empty, return the first available spot in
345 		 * chk/nxt, which should be 1.
346 		 */
347 		if (tblend < 2)
348 			return 1;
349 
350 		/* Start searching for table space near the end of
351 		 * chk/nxt arrays.
352 		 */
353 		i = tblend - numecs;
354 	}
355 
356 	else
357 		/* Start searching for table space from the beginning
358 		 * (skipping only the elements which will definitely not
359 		 * hold the new state).
360 		 */
361 		i = firstfree;
362 
363 	while (1) {		/* loops until a space is found */
364 		while (i + numecs >= current_max_xpairs)
365 			expand_nxt_chk ();
366 
367 		/* Loops until space for end-of-buffer and action number
368 		 * are found.
369 		 */
370 		while (1) {
371 			/* Check for action number space. */
372 			if (chk[i - 1] == 0) {
373 				/* Check for end-of-buffer space. */
374 				if (chk[i] == 0)
375 					break;
376 
377 				else
378 					/* Since i != 0, there is no use
379 					 * checking to see if (++i) - 1 == 0,
380 					 * because that's the same as i == 0,
381 					 * so we skip a space.
382 					 */
383 					i += 2;
384 			}
385 
386 			else
387 				++i;
388 
389 			while (i + numecs >= current_max_xpairs)
390 				expand_nxt_chk ();
391 		}
392 
393 		/* If we started search from the beginning, store the new
394 		 * firstfree for the next call of find_table_space().
395 		 */
396 		if (numtrans <= MAX_XTIONS_FULL_INTERIOR_FIT)
397 			firstfree = i + 1;
398 
399 		/* Check to see if all elements in chk (and therefore nxt)
400 		 * that are needed for the new state have not yet been taken.
401 		 */
402 
403 		state_ptr = &state[1];
404 		ptr_to_last_entry_in_state = &chk[i + numecs + 1];
405 
406 		for (chk_ptr = &chk[i + 1];
407 		     chk_ptr != ptr_to_last_entry_in_state; ++chk_ptr)
408 			if (*(state_ptr++) != 0 && *chk_ptr != 0)
409 				break;
410 
411 		if (chk_ptr == ptr_to_last_entry_in_state)
412 			return i;
413 
414 		else
415 			++i;
416 	}
417 }
418 
419 
420 /* inittbl - initialize transition tables
421  *
422  * Initializes "firstfree" to be one beyond the end of the table.  Initializes
423  * all "chk" entries to be zero.
424  */
inittbl()425 void    inittbl ()
426 {
427 	register int i;
428 
429 	zero_out ((char *) chk,
430 
431 		  (size_t) (current_max_xpairs * sizeof (int)));
432 
433 	tblend = 0;
434 	firstfree = tblend + 1;
435 	numtemps = 0;
436 
437 	if (usemecs) {
438 		/* Set up doubly-linked meta-equivalence classes; these
439 		 * are sets of equivalence classes which all have identical
440 		 * transitions out of TEMPLATES.
441 		 */
442 
443 		tecbck[1] = NIL;
444 
445 		for (i = 2; i <= numecs; ++i) {
446 			tecbck[i] = i - 1;
447 			tecfwd[i - 1] = i;
448 		}
449 
450 		tecfwd[numecs] = NIL;
451 	}
452 }
453 
454 
455 /* mkdeftbl - make the default, "jam" table entries */
456 
mkdeftbl()457 void    mkdeftbl ()
458 {
459 	int     i;
460 
461 	jamstate = lastdfa + 1;
462 
463 	++tblend;		/* room for transition on end-of-buffer character */
464 
465 	while (tblend + numecs >= current_max_xpairs)
466 		expand_nxt_chk ();
467 
468 	/* Add in default end-of-buffer transition. */
469 	nxt[tblend] = end_of_buffer_state;
470 	chk[tblend] = jamstate;
471 
472 	for (i = 1; i <= numecs; ++i) {
473 		nxt[tblend + i] = 0;
474 		chk[tblend + i] = jamstate;
475 	}
476 
477 	jambase = tblend;
478 
479 	base[jamstate] = jambase;
480 	def[jamstate] = 0;
481 
482 	tblend += numecs;
483 	++numtemps;
484 }
485 
486 
487 /* mkentry - create base/def and nxt/chk entries for transition array
488  *
489  * synopsis
490  *   int state[numchars + 1], numchars, statenum, deflink, totaltrans;
491  *   mkentry( state, numchars, statenum, deflink, totaltrans );
492  *
493  * "state" is a transition array "numchars" characters in size, "statenum"
494  * is the offset to be used into the base/def tables, and "deflink" is the
495  * entry to put in the "def" table entry.  If "deflink" is equal to
496  * "JAMSTATE", then no attempt will be made to fit zero entries of "state"
497  * (i.e., jam entries) into the table.  It is assumed that by linking to
498  * "JAMSTATE" they will be taken care of.  In any case, entries in "state"
499  * marking transitions to "SAME_TRANS" are treated as though they will be
500  * taken care of by whereever "deflink" points.  "totaltrans" is the total
501  * number of transitions out of the state.  If it is below a certain threshold,
502  * the tables are searched for an interior spot that will accommodate the
503  * state array.
504  */
505 
mkentry(state,numchars,statenum,deflink,totaltrans)506 void    mkentry (state, numchars, statenum, deflink, totaltrans)
507      register int *state;
508      int     numchars, statenum, deflink, totaltrans;
509 {
510 	register int minec, maxec, i, baseaddr;
511 	int     tblbase, tbllast;
512 
513 	if (totaltrans == 0) {	/* there are no out-transitions */
514 		if (deflink == JAMSTATE)
515 			base[statenum] = JAMSTATE;
516 		else
517 			base[statenum] = 0;
518 
519 		def[statenum] = deflink;
520 		return;
521 	}
522 
523 	for (minec = 1; minec <= numchars; ++minec) {
524 		if (state[minec] != SAME_TRANS)
525 			if (state[minec] != 0 || deflink != JAMSTATE)
526 				break;
527 	}
528 
529 	if (totaltrans == 1) {
530 		/* There's only one out-transition.  Save it for later to fill
531 		 * in holes in the tables.
532 		 */
533 		stack1 (statenum, minec, state[minec], deflink);
534 		return;
535 	}
536 
537 	for (maxec = numchars; maxec > 0; --maxec) {
538 		if (state[maxec] != SAME_TRANS)
539 			if (state[maxec] != 0 || deflink != JAMSTATE)
540 				break;
541 	}
542 
543 	/* Whether we try to fit the state table in the middle of the table
544 	 * entries we have already generated, or if we just take the state
545 	 * table at the end of the nxt/chk tables, we must make sure that we
546 	 * have a valid base address (i.e., non-negative).  Note that
547 	 * negative base addresses dangerous at run-time (because indexing
548 	 * the nxt array with one and a low-valued character will access
549 	 * memory before the start of the array.
550 	 */
551 
552 	/* Find the first transition of state that we need to worry about. */
553 	if (totaltrans * 100 <= numchars * INTERIOR_FIT_PERCENTAGE) {
554 		/* Attempt to squeeze it into the middle of the tables. */
555 		baseaddr = firstfree;
556 
557 		while (baseaddr < minec) {
558 			/* Using baseaddr would result in a negative base
559 			 * address below; find the next free slot.
560 			 */
561 			for (++baseaddr; chk[baseaddr] != 0; ++baseaddr) ;
562 		}
563 
564 		while (baseaddr + maxec - minec + 1 >= current_max_xpairs)
565 			expand_nxt_chk ();
566 
567 		for (i = minec; i <= maxec; ++i)
568 			if (state[i] != SAME_TRANS &&
569 			    (state[i] != 0 || deflink != JAMSTATE) &&
570 			    chk[baseaddr + i - minec] != 0) {	/* baseaddr unsuitable - find another */
571 				for (++baseaddr;
572 				     baseaddr < current_max_xpairs &&
573 				     chk[baseaddr] != 0; ++baseaddr) ;
574 
575 				while (baseaddr + maxec - minec + 1 >=
576 				       current_max_xpairs)
577 						expand_nxt_chk ();
578 
579 				/* Reset the loop counter so we'll start all
580 				 * over again next time it's incremented.
581 				 */
582 
583 				i = minec - 1;
584 			}
585 	}
586 
587 	else {
588 		/* Ensure that the base address we eventually generate is
589 		 * non-negative.
590 		 */
591 		baseaddr = MAX (tblend + 1, minec);
592 	}
593 
594 	tblbase = baseaddr - minec;
595 	tbllast = tblbase + maxec;
596 
597 	while (tbllast + 1 >= current_max_xpairs)
598 		expand_nxt_chk ();
599 
600 	base[statenum] = tblbase;
601 	def[statenum] = deflink;
602 
603 	for (i = minec; i <= maxec; ++i)
604 		if (state[i] != SAME_TRANS)
605 			if (state[i] != 0 || deflink != JAMSTATE) {
606 				nxt[tblbase + i] = state[i];
607 				chk[tblbase + i] = statenum;
608 			}
609 
610 	if (baseaddr == firstfree)
611 		/* Find next free slot in tables. */
612 		for (++firstfree; chk[firstfree] != 0; ++firstfree) ;
613 
614 	tblend = MAX (tblend, tbllast);
615 }
616 
617 
618 /* mk1tbl - create table entries for a state (or state fragment) which
619  *            has only one out-transition
620  */
621 
mk1tbl(state,sym,onenxt,onedef)622 void    mk1tbl (state, sym, onenxt, onedef)
623      int     state, sym, onenxt, onedef;
624 {
625 	if (firstfree < sym)
626 		firstfree = sym;
627 
628 	while (chk[firstfree] != 0)
629 		if (++firstfree >= current_max_xpairs)
630 			expand_nxt_chk ();
631 
632 	base[state] = firstfree - sym;
633 	def[state] = onedef;
634 	chk[firstfree] = state;
635 	nxt[firstfree] = onenxt;
636 
637 	if (firstfree > tblend) {
638 		tblend = firstfree++;
639 
640 		if (firstfree >= current_max_xpairs)
641 			expand_nxt_chk ();
642 	}
643 }
644 
645 
646 /* mkprot - create new proto entry */
647 
mkprot(state,statenum,comstate)648 void    mkprot (state, statenum, comstate)
649      int     state[], statenum, comstate;
650 {
651 	int     i, slot, tblbase;
652 
653 	if (++numprots >= MSP || numecs * numprots >= PROT_SAVE_SIZE) {
654 		/* Gotta make room for the new proto by dropping last entry in
655 		 * the queue.
656 		 */
657 		slot = lastprot;
658 		lastprot = protprev[lastprot];
659 		protnext[lastprot] = NIL;
660 	}
661 
662 	else
663 		slot = numprots;
664 
665 	protnext[slot] = firstprot;
666 
667 	if (firstprot != NIL)
668 		protprev[firstprot] = slot;
669 
670 	firstprot = slot;
671 	prottbl[slot] = statenum;
672 	protcomst[slot] = comstate;
673 
674 	/* Copy state into save area so it can be compared with rapidly. */
675 	tblbase = numecs * (slot - 1);
676 
677 	for (i = 1; i <= numecs; ++i)
678 		protsave[tblbase + i] = state[i];
679 }
680 
681 
682 /* mktemplate - create a template entry based on a state, and connect the state
683  *              to it
684  */
685 
mktemplate(state,statenum,comstate)686 void    mktemplate (state, statenum, comstate)
687      int     state[], statenum, comstate;
688 {
689 	int     i, numdiff, tmpbase, tmp[CSIZE + 1];
690 	Char    transset[CSIZE + 1];
691 	int     tsptr;
692 
693 	++numtemps;
694 
695 	tsptr = 0;
696 
697 	/* Calculate where we will temporarily store the transition table
698 	 * of the template in the tnxt[] array.  The final transition table
699 	 * gets created by cmptmps().
700 	 */
701 
702 	tmpbase = numtemps * numecs;
703 
704 	if (tmpbase + numecs >= current_max_template_xpairs) {
705 		current_max_template_xpairs +=
706 			MAX_TEMPLATE_XPAIRS_INCREMENT;
707 
708 		++num_reallocs;
709 
710 		tnxt = reallocate_integer_array (tnxt,
711 						 current_max_template_xpairs);
712 	}
713 
714 	for (i = 1; i <= numecs; ++i)
715 		if (state[i] == 0)
716 			tnxt[tmpbase + i] = 0;
717 		else {
718 			transset[tsptr++] = i;
719 			tnxt[tmpbase + i] = comstate;
720 		}
721 
722 	if (usemecs)
723 		mkeccl (transset, tsptr, tecfwd, tecbck, numecs, 0);
724 
725 	mkprot (tnxt + tmpbase, -numtemps, comstate);
726 
727 	/* We rely on the fact that mkprot adds things to the beginning
728 	 * of the proto queue.
729 	 */
730 
731 	numdiff = tbldiff (state, firstprot, tmp);
732 	mkentry (tmp, numecs, statenum, -numtemps, numdiff);
733 }
734 
735 
736 /* mv2front - move proto queue element to front of queue */
737 
mv2front(qelm)738 void    mv2front (qelm)
739      int     qelm;
740 {
741 	if (firstprot != qelm) {
742 		if (qelm == lastprot)
743 			lastprot = protprev[lastprot];
744 
745 		protnext[protprev[qelm]] = protnext[qelm];
746 
747 		if (protnext[qelm] != NIL)
748 			protprev[protnext[qelm]] = protprev[qelm];
749 
750 		protprev[qelm] = NIL;
751 		protnext[qelm] = firstprot;
752 		protprev[firstprot] = qelm;
753 		firstprot = qelm;
754 	}
755 }
756 
757 
758 /* place_state - place a state into full speed transition table
759  *
760  * State is the statenum'th state.  It is indexed by equivalence class and
761  * gives the number of the state to enter for a given equivalence class.
762  * Transnum is the number of out-transitions for the state.
763  */
764 
place_state(state,statenum,transnum)765 void    place_state (state, statenum, transnum)
766      int    *state, statenum, transnum;
767 {
768 	register int i;
769 	register int *state_ptr;
770 	int     position = find_table_space (state, transnum);
771 
772 	/* "base" is the table of start positions. */
773 	base[statenum] = position;
774 
775 	/* Put in action number marker; this non-zero number makes sure that
776 	 * find_table_space() knows that this position in chk/nxt is taken
777 	 * and should not be used for another accepting number in another
778 	 * state.
779 	 */
780 	chk[position - 1] = 1;
781 
782 	/* Put in end-of-buffer marker; this is for the same purposes as
783 	 * above.
784 	 */
785 	chk[position] = 1;
786 
787 	/* Place the state into chk and nxt. */
788 	state_ptr = &state[1];
789 
790 	for (i = 1; i <= numecs; ++i, ++state_ptr)
791 		if (*state_ptr != 0) {
792 			chk[position + i] = i;
793 			nxt[position + i] = *state_ptr;
794 		}
795 
796 	if (position + numecs > tblend)
797 		tblend = position + numecs;
798 }
799 
800 
801 /* stack1 - save states with only one out-transition to be processed later
802  *
803  * If there's room for another state on the "one-transition" stack, the
804  * state is pushed onto it, to be processed later by mk1tbl.  If there's
805  * no room, we process the sucker right now.
806  */
807 
stack1(statenum,sym,nextstate,deflink)808 void    stack1 (statenum, sym, nextstate, deflink)
809      int     statenum, sym, nextstate, deflink;
810 {
811 	if (onesp >= ONE_STACK_SIZE - 1)
812 		mk1tbl (statenum, sym, nextstate, deflink);
813 
814 	else {
815 		++onesp;
816 		onestate[onesp] = statenum;
817 		onesym[onesp] = sym;
818 		onenext[onesp] = nextstate;
819 		onedef[onesp] = deflink;
820 	}
821 }
822 
823 
824 /* tbldiff - compute differences between two state tables
825  *
826  * "state" is the state array which is to be extracted from the pr'th
827  * proto.  "pr" is both the number of the proto we are extracting from
828  * and an index into the save area where we can find the proto's complete
829  * state table.  Each entry in "state" which differs from the corresponding
830  * entry of "pr" will appear in "ext".
831  *
832  * Entries which are the same in both "state" and "pr" will be marked
833  * as transitions to "SAME_TRANS" in "ext".  The total number of differences
834  * between "state" and "pr" is returned as function value.  Note that this
835  * number is "numecs" minus the number of "SAME_TRANS" entries in "ext".
836  */
837 
tbldiff(state,pr,ext)838 int     tbldiff (state, pr, ext)
839      int     state[], pr, ext[];
840 {
841 	register int i, *sp = state, *ep = ext, *protp;
842 	register int numdiff = 0;
843 
844 	protp = &protsave[numecs * (pr - 1)];
845 
846 	for (i = numecs; i > 0; --i) {
847 		if (*++protp == *++sp)
848 			*++ep = SAME_TRANS;
849 		else {
850 			*++ep = *sp;
851 			++numdiff;
852 		}
853 	}
854 
855 	return numdiff;
856 }
857