xref: /minix3/external/bsd/flex/dist/tblcmp.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /*	$NetBSD: tblcmp.c,v 1.3 2014/10/30 18:44:05 christos Exp $	*/
2357f1050SThomas Veerman 
3357f1050SThomas Veerman /* tblcmp - table compression routines */
4357f1050SThomas Veerman 
5357f1050SThomas Veerman /*  Copyright (c) 1990 The Regents of the University of California. */
6357f1050SThomas Veerman /*  All rights reserved. */
7357f1050SThomas Veerman 
8357f1050SThomas Veerman /*  This code is derived from software contributed to Berkeley by */
9357f1050SThomas Veerman /*  Vern Paxson. */
10357f1050SThomas Veerman 
11357f1050SThomas Veerman /*  The United States Government has rights in this work pursuant */
12357f1050SThomas Veerman /*  to contract no. DE-AC03-76SF00098 between the United States */
13357f1050SThomas Veerman /*  Department of Energy and the University of California. */
14357f1050SThomas Veerman 
15357f1050SThomas Veerman /*  This file is part of flex. */
16357f1050SThomas Veerman 
17357f1050SThomas Veerman /*  Redistribution and use in source and binary forms, with or without */
18357f1050SThomas Veerman /*  modification, are permitted provided that the following conditions */
19357f1050SThomas Veerman /*  are met: */
20357f1050SThomas Veerman 
21357f1050SThomas Veerman /*  1. Redistributions of source code must retain the above copyright */
22357f1050SThomas Veerman /*     notice, this list of conditions and the following disclaimer. */
23357f1050SThomas Veerman /*  2. Redistributions in binary form must reproduce the above copyright */
24357f1050SThomas Veerman /*     notice, this list of conditions and the following disclaimer in the */
25357f1050SThomas Veerman /*     documentation and/or other materials provided with the distribution. */
26357f1050SThomas Veerman 
27357f1050SThomas Veerman /*  Neither the name of the University nor the names of its contributors */
28357f1050SThomas Veerman /*  may be used to endorse or promote products derived from this software */
29357f1050SThomas Veerman /*  without specific prior written permission. */
30357f1050SThomas Veerman 
31357f1050SThomas Veerman /*  THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR */
32357f1050SThomas Veerman /*  IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED */
33357f1050SThomas Veerman /*  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR */
34357f1050SThomas Veerman /*  PURPOSE. */
35357f1050SThomas Veerman #include "flexdef.h"
36*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: tblcmp.c,v 1.3 2014/10/30 18:44:05 christos Exp $");
37*0a6a1f1dSLionel Sambuc 
38357f1050SThomas Veerman 
39357f1050SThomas Veerman 
40357f1050SThomas Veerman /* declarations for functions that have forward references */
41357f1050SThomas Veerman 
42357f1050SThomas Veerman void mkentry PROTO ((register int *, int, int, int, int));
43357f1050SThomas Veerman void mkprot PROTO ((int[], int, int));
44357f1050SThomas Veerman void mktemplate PROTO ((int[], int, int));
45357f1050SThomas Veerman void mv2front PROTO ((int));
46357f1050SThomas Veerman int tbldiff PROTO ((int[], int, int[]));
47357f1050SThomas Veerman 
48357f1050SThomas Veerman 
49357f1050SThomas Veerman /* bldtbl - build table entries for dfa state
50357f1050SThomas Veerman  *
51357f1050SThomas Veerman  * synopsis
52357f1050SThomas Veerman  *   int state[numecs], statenum, totaltrans, comstate, comfreq;
53357f1050SThomas Veerman  *   bldtbl( state, statenum, totaltrans, comstate, comfreq );
54357f1050SThomas Veerman  *
55357f1050SThomas Veerman  * State is the statenum'th dfa state.  It is indexed by equivalence class and
56357f1050SThomas Veerman  * gives the number of the state to enter for a given equivalence class.
57357f1050SThomas Veerman  * totaltrans is the total number of transitions out of the state.  Comstate
58357f1050SThomas Veerman  * is that state which is the destination of the most transitions out of State.
59357f1050SThomas Veerman  * Comfreq is how many transitions there are out of State to Comstate.
60357f1050SThomas Veerman  *
61357f1050SThomas Veerman  * A note on terminology:
62357f1050SThomas Veerman  *    "protos" are transition tables which have a high probability of
63357f1050SThomas Veerman  * either being redundant (a state processed later will have an identical
64357f1050SThomas Veerman  * transition table) or nearly redundant (a state processed later will have
65357f1050SThomas Veerman  * many of the same out-transitions).  A "most recently used" queue of
66357f1050SThomas Veerman  * protos is kept around with the hope that most states will find a proto
67357f1050SThomas Veerman  * which is similar enough to be usable, and therefore compacting the
68357f1050SThomas Veerman  * output tables.
69357f1050SThomas Veerman  *    "templates" are a special type of proto.  If a transition table is
70357f1050SThomas Veerman  * homogeneous or nearly homogeneous (all transitions go to the same
71357f1050SThomas Veerman  * destination) then the odds are good that future states will also go
72357f1050SThomas Veerman  * to the same destination state on basically the same character set.
73357f1050SThomas Veerman  * These homogeneous states are so common when dealing with large rule
74357f1050SThomas Veerman  * sets that they merit special attention.  If the transition table were
75357f1050SThomas Veerman  * simply made into a proto, then (typically) each subsequent, similar
76357f1050SThomas Veerman  * state will differ from the proto for two out-transitions.  One of these
77357f1050SThomas Veerman  * out-transitions will be that character on which the proto does not go
78357f1050SThomas Veerman  * to the common destination, and one will be that character on which the
79357f1050SThomas Veerman  * state does not go to the common destination.  Templates, on the other
80357f1050SThomas Veerman  * hand, go to the common state on EVERY transition character, and therefore
81357f1050SThomas Veerman  * cost only one difference.
82357f1050SThomas Veerman  */
83357f1050SThomas Veerman 
bldtbl(state,statenum,totaltrans,comstate,comfreq)84357f1050SThomas Veerman void    bldtbl (state, statenum, totaltrans, comstate, comfreq)
85357f1050SThomas Veerman      int     state[], statenum, totaltrans, comstate, comfreq;
86357f1050SThomas Veerman {
87357f1050SThomas Veerman 	int     extptr, extrct[2][CSIZE + 1];
88357f1050SThomas Veerman 	int     mindiff, minprot, i, d;
89357f1050SThomas Veerman 
90357f1050SThomas Veerman 	/* If extptr is 0 then the first array of extrct holds the result
91357f1050SThomas Veerman 	 * of the "best difference" to date, which is those transitions
92357f1050SThomas Veerman 	 * which occur in "state" but not in the proto which, to date,
93357f1050SThomas Veerman 	 * has the fewest differences between itself and "state".  If
94357f1050SThomas Veerman 	 * extptr is 1 then the second array of extrct hold the best
95357f1050SThomas Veerman 	 * difference.  The two arrays are toggled between so that the
96357f1050SThomas Veerman 	 * best difference to date can be kept around and also a difference
97357f1050SThomas Veerman 	 * just created by checking against a candidate "best" proto.
98357f1050SThomas Veerman 	 */
99357f1050SThomas Veerman 
100357f1050SThomas Veerman 	extptr = 0;
101357f1050SThomas Veerman 
102357f1050SThomas Veerman 	/* If the state has too few out-transitions, don't bother trying to
103357f1050SThomas Veerman 	 * compact its tables.
104357f1050SThomas Veerman 	 */
105357f1050SThomas Veerman 
106357f1050SThomas Veerman 	if ((totaltrans * 100) < (numecs * PROTO_SIZE_PERCENTAGE))
107357f1050SThomas Veerman 		mkentry (state, numecs, statenum, JAMSTATE, totaltrans);
108357f1050SThomas Veerman 
109357f1050SThomas Veerman 	else {
110357f1050SThomas Veerman 		/* "checkcom" is true if we should only check "state" against
111357f1050SThomas Veerman 		 * protos which have the same "comstate" value.
112357f1050SThomas Veerman 		 */
113357f1050SThomas Veerman 		int     checkcom =
114357f1050SThomas Veerman 
115357f1050SThomas Veerman 			comfreq * 100 > totaltrans * CHECK_COM_PERCENTAGE;
116357f1050SThomas Veerman 
117357f1050SThomas Veerman 		minprot = firstprot;
118357f1050SThomas Veerman 		mindiff = totaltrans;
119357f1050SThomas Veerman 
120357f1050SThomas Veerman 		if (checkcom) {
121357f1050SThomas Veerman 			/* Find first proto which has the same "comstate". */
122357f1050SThomas Veerman 			for (i = firstprot; i != NIL; i = protnext[i])
123357f1050SThomas Veerman 				if (protcomst[i] == comstate) {
124357f1050SThomas Veerman 					minprot = i;
125357f1050SThomas Veerman 					mindiff = tbldiff (state, minprot,
126357f1050SThomas Veerman 							   extrct[extptr]);
127357f1050SThomas Veerman 					break;
128357f1050SThomas Veerman 				}
129357f1050SThomas Veerman 		}
130357f1050SThomas Veerman 
131357f1050SThomas Veerman 		else {
132357f1050SThomas Veerman 			/* Since we've decided that the most common destination
133357f1050SThomas Veerman 			 * out of "state" does not occur with a high enough
134357f1050SThomas Veerman 			 * frequency, we set the "comstate" to zero, assuring
135357f1050SThomas Veerman 			 * that if this state is entered into the proto list,
136357f1050SThomas Veerman 			 * it will not be considered a template.
137357f1050SThomas Veerman 			 */
138357f1050SThomas Veerman 			comstate = 0;
139357f1050SThomas Veerman 
140357f1050SThomas Veerman 			if (firstprot != NIL) {
141357f1050SThomas Veerman 				minprot = firstprot;
142357f1050SThomas Veerman 				mindiff = tbldiff (state, minprot,
143357f1050SThomas Veerman 						   extrct[extptr]);
144357f1050SThomas Veerman 			}
145357f1050SThomas Veerman 		}
146357f1050SThomas Veerman 
147357f1050SThomas Veerman 		/* We now have the first interesting proto in "minprot".  If
148357f1050SThomas Veerman 		 * it matches within the tolerances set for the first proto,
149357f1050SThomas Veerman 		 * we don't want to bother scanning the rest of the proto list
150357f1050SThomas Veerman 		 * to see if we have any other reasonable matches.
151357f1050SThomas Veerman 		 */
152357f1050SThomas Veerman 
153357f1050SThomas Veerman 		if (mindiff * 100 >
154357f1050SThomas Veerman 		    totaltrans * FIRST_MATCH_DIFF_PERCENTAGE) {
155357f1050SThomas Veerman 			/* Not a good enough match.  Scan the rest of the
156357f1050SThomas Veerman 			 * protos.
157357f1050SThomas Veerman 			 */
158357f1050SThomas Veerman 			for (i = minprot; i != NIL; i = protnext[i]) {
159357f1050SThomas Veerman 				d = tbldiff (state, i, extrct[1 - extptr]);
160357f1050SThomas Veerman 				if (d < mindiff) {
161357f1050SThomas Veerman 					extptr = 1 - extptr;
162357f1050SThomas Veerman 					mindiff = d;
163357f1050SThomas Veerman 					minprot = i;
164357f1050SThomas Veerman 				}
165357f1050SThomas Veerman 			}
166357f1050SThomas Veerman 		}
167357f1050SThomas Veerman 
168357f1050SThomas Veerman 		/* Check if the proto we've decided on as our best bet is close
169357f1050SThomas Veerman 		 * enough to the state we want to match to be usable.
170357f1050SThomas Veerman 		 */
171357f1050SThomas Veerman 
172357f1050SThomas Veerman 		if (mindiff * 100 >
173357f1050SThomas Veerman 		    totaltrans * ACCEPTABLE_DIFF_PERCENTAGE) {
174357f1050SThomas Veerman 			/* No good.  If the state is homogeneous enough,
175357f1050SThomas Veerman 			 * we make a template out of it.  Otherwise, we
176357f1050SThomas Veerman 			 * make a proto.
177357f1050SThomas Veerman 			 */
178357f1050SThomas Veerman 
179357f1050SThomas Veerman 			if (comfreq * 100 >=
180357f1050SThomas Veerman 			    totaltrans * TEMPLATE_SAME_PERCENTAGE)
181357f1050SThomas Veerman 					mktemplate (state, statenum,
182357f1050SThomas Veerman 						    comstate);
183357f1050SThomas Veerman 
184357f1050SThomas Veerman 			else {
185357f1050SThomas Veerman 				mkprot (state, statenum, comstate);
186357f1050SThomas Veerman 				mkentry (state, numecs, statenum,
187357f1050SThomas Veerman 					 JAMSTATE, totaltrans);
188357f1050SThomas Veerman 			}
189357f1050SThomas Veerman 		}
190357f1050SThomas Veerman 
191357f1050SThomas Veerman 		else {		/* use the proto */
192357f1050SThomas Veerman 			mkentry (extrct[extptr], numecs, statenum,
193357f1050SThomas Veerman 				 prottbl[minprot], mindiff);
194357f1050SThomas Veerman 
195357f1050SThomas Veerman 			/* If this state was sufficiently different from the
196357f1050SThomas Veerman 			 * proto we built it from, make it, too, a proto.
197357f1050SThomas Veerman 			 */
198357f1050SThomas Veerman 
199357f1050SThomas Veerman 			if (mindiff * 100 >=
200357f1050SThomas Veerman 			    totaltrans * NEW_PROTO_DIFF_PERCENTAGE)
201357f1050SThomas Veerman 					mkprot (state, statenum, comstate);
202357f1050SThomas Veerman 
203357f1050SThomas Veerman 			/* Since mkprot added a new proto to the proto queue,
204357f1050SThomas Veerman 			 * it's possible that "minprot" is no longer on the
205357f1050SThomas Veerman 			 * proto queue (if it happened to have been the last
206357f1050SThomas Veerman 			 * entry, it would have been bumped off).  If it's
207357f1050SThomas Veerman 			 * not there, then the new proto took its physical
208357f1050SThomas Veerman 			 * place (though logically the new proto is at the
209357f1050SThomas Veerman 			 * beginning of the queue), so in that case the
210357f1050SThomas Veerman 			 * following call will do nothing.
211357f1050SThomas Veerman 			 */
212357f1050SThomas Veerman 
213357f1050SThomas Veerman 			mv2front (minprot);
214357f1050SThomas Veerman 		}
215357f1050SThomas Veerman 	}
216357f1050SThomas Veerman }
217357f1050SThomas Veerman 
218357f1050SThomas Veerman 
219357f1050SThomas Veerman /* cmptmps - compress template table entries
220357f1050SThomas Veerman  *
221357f1050SThomas Veerman  * Template tables are compressed by using the 'template equivalence
222357f1050SThomas Veerman  * classes', which are collections of transition character equivalence
223357f1050SThomas Veerman  * classes which always appear together in templates - really meta-equivalence
224357f1050SThomas Veerman  * classes.
225357f1050SThomas Veerman  */
226357f1050SThomas Veerman 
cmptmps()227357f1050SThomas Veerman void    cmptmps ()
228357f1050SThomas Veerman {
229357f1050SThomas Veerman 	int     tmpstorage[CSIZE + 1];
230357f1050SThomas Veerman 	register int *tmp = tmpstorage, i, j;
231357f1050SThomas Veerman 	int     totaltrans, trans;
232357f1050SThomas Veerman 
233357f1050SThomas Veerman 	peakpairs = numtemps * numecs + tblend;
234357f1050SThomas Veerman 
235357f1050SThomas Veerman 	if (usemecs) {
236357f1050SThomas Veerman 		/* Create equivalence classes based on data gathered on
237357f1050SThomas Veerman 		 * template transitions.
238357f1050SThomas Veerman 		 */
239357f1050SThomas Veerman 		nummecs = cre8ecs (tecfwd, tecbck, numecs);
240357f1050SThomas Veerman 	}
241357f1050SThomas Veerman 
242357f1050SThomas Veerman 	else
243357f1050SThomas Veerman 		nummecs = numecs;
244357f1050SThomas Veerman 
245357f1050SThomas Veerman 	while (lastdfa + numtemps + 1 >= current_max_dfas)
246357f1050SThomas Veerman 		increase_max_dfas ();
247357f1050SThomas Veerman 
248357f1050SThomas Veerman 	/* Loop through each template. */
249357f1050SThomas Veerman 
250357f1050SThomas Veerman 	for (i = 1; i <= numtemps; ++i) {
251357f1050SThomas Veerman 		/* Number of non-jam transitions out of this template. */
252357f1050SThomas Veerman 		totaltrans = 0;
253357f1050SThomas Veerman 
254357f1050SThomas Veerman 		for (j = 1; j <= numecs; ++j) {
255357f1050SThomas Veerman 			trans = tnxt[numecs * i + j];
256357f1050SThomas Veerman 
257357f1050SThomas Veerman 			if (usemecs) {
258357f1050SThomas Veerman 				/* The absolute value of tecbck is the
259357f1050SThomas Veerman 				 * meta-equivalence class of a given
260357f1050SThomas Veerman 				 * equivalence class, as set up by cre8ecs().
261357f1050SThomas Veerman 				 */
262357f1050SThomas Veerman 				if (tecbck[j] > 0) {
263357f1050SThomas Veerman 					tmp[tecbck[j]] = trans;
264357f1050SThomas Veerman 
265357f1050SThomas Veerman 					if (trans > 0)
266357f1050SThomas Veerman 						++totaltrans;
267357f1050SThomas Veerman 				}
268357f1050SThomas Veerman 			}
269357f1050SThomas Veerman 
270357f1050SThomas Veerman 			else {
271357f1050SThomas Veerman 				tmp[j] = trans;
272357f1050SThomas Veerman 
273357f1050SThomas Veerman 				if (trans > 0)
274357f1050SThomas Veerman 					++totaltrans;
275357f1050SThomas Veerman 			}
276357f1050SThomas Veerman 		}
277357f1050SThomas Veerman 
278357f1050SThomas Veerman 		/* It is assumed (in a rather subtle way) in the skeleton
279357f1050SThomas Veerman 		 * that if we're using meta-equivalence classes, the def[]
280357f1050SThomas Veerman 		 * entry for all templates is the jam template, i.e.,
281357f1050SThomas Veerman 		 * templates never default to other non-jam table entries
282357f1050SThomas Veerman 		 * (e.g., another template)
283357f1050SThomas Veerman 		 */
284357f1050SThomas Veerman 
285357f1050SThomas Veerman 		/* Leave room for the jam-state after the last real state. */
286357f1050SThomas Veerman 		mkentry (tmp, nummecs, lastdfa + i + 1, JAMSTATE,
287357f1050SThomas Veerman 			 totaltrans);
288357f1050SThomas Veerman 	}
289357f1050SThomas Veerman }
290357f1050SThomas Veerman 
291357f1050SThomas Veerman 
292357f1050SThomas Veerman 
293357f1050SThomas Veerman /* expand_nxt_chk - expand the next check arrays */
294357f1050SThomas Veerman 
expand_nxt_chk()295357f1050SThomas Veerman void    expand_nxt_chk ()
296357f1050SThomas Veerman {
297357f1050SThomas Veerman 	register int old_max = current_max_xpairs;
298357f1050SThomas Veerman 
299357f1050SThomas Veerman 	current_max_xpairs += MAX_XPAIRS_INCREMENT;
300357f1050SThomas Veerman 
301357f1050SThomas Veerman 	++num_reallocs;
302357f1050SThomas Veerman 
303357f1050SThomas Veerman 	nxt = reallocate_integer_array (nxt, current_max_xpairs);
304357f1050SThomas Veerman 	chk = reallocate_integer_array (chk, current_max_xpairs);
305357f1050SThomas Veerman 
306357f1050SThomas Veerman 	zero_out ((char *) (chk + old_max),
307357f1050SThomas Veerman 		  (size_t) (MAX_XPAIRS_INCREMENT * sizeof (int)));
308357f1050SThomas Veerman }
309357f1050SThomas Veerman 
310357f1050SThomas Veerman 
311357f1050SThomas Veerman /* find_table_space - finds a space in the table for a state to be placed
312357f1050SThomas Veerman  *
313357f1050SThomas Veerman  * synopsis
314357f1050SThomas Veerman  *     int *state, numtrans, block_start;
315357f1050SThomas Veerman  *     int find_table_space();
316357f1050SThomas Veerman  *
317357f1050SThomas Veerman  *     block_start = find_table_space( state, numtrans );
318357f1050SThomas Veerman  *
319357f1050SThomas Veerman  * State is the state to be added to the full speed transition table.
320357f1050SThomas Veerman  * Numtrans is the number of out-transitions for the state.
321357f1050SThomas Veerman  *
322357f1050SThomas Veerman  * find_table_space() returns the position of the start of the first block (in
323357f1050SThomas Veerman  * chk) able to accommodate the state
324357f1050SThomas Veerman  *
325357f1050SThomas Veerman  * In determining if a state will or will not fit, find_table_space() must take
326357f1050SThomas Veerman  * into account the fact that an end-of-buffer state will be added at [0],
327357f1050SThomas Veerman  * and an action number will be added in [-1].
328357f1050SThomas Veerman  */
329357f1050SThomas Veerman 
find_table_space(state,numtrans)330357f1050SThomas Veerman int     find_table_space (state, numtrans)
331357f1050SThomas Veerman      int    *state, numtrans;
332357f1050SThomas Veerman {
333357f1050SThomas Veerman 	/* Firstfree is the position of the first possible occurrence of two
334357f1050SThomas Veerman 	 * consecutive unused records in the chk and nxt arrays.
335357f1050SThomas Veerman 	 */
336357f1050SThomas Veerman 	register int i;
337357f1050SThomas Veerman 	register int *state_ptr, *chk_ptr;
338357f1050SThomas Veerman 	register int *ptr_to_last_entry_in_state;
339357f1050SThomas Veerman 
340357f1050SThomas Veerman 	/* If there are too many out-transitions, put the state at the end of
341357f1050SThomas Veerman 	 * nxt and chk.
342357f1050SThomas Veerman 	 */
343357f1050SThomas Veerman 	if (numtrans > MAX_XTIONS_FULL_INTERIOR_FIT) {
344357f1050SThomas Veerman 		/* If table is empty, return the first available spot in
345357f1050SThomas Veerman 		 * chk/nxt, which should be 1.
346357f1050SThomas Veerman 		 */
347357f1050SThomas Veerman 		if (tblend < 2)
348357f1050SThomas Veerman 			return 1;
349357f1050SThomas Veerman 
350357f1050SThomas Veerman 		/* Start searching for table space near the end of
351357f1050SThomas Veerman 		 * chk/nxt arrays.
352357f1050SThomas Veerman 		 */
353357f1050SThomas Veerman 		i = tblend - numecs;
354357f1050SThomas Veerman 	}
355357f1050SThomas Veerman 
356357f1050SThomas Veerman 	else
357357f1050SThomas Veerman 		/* Start searching for table space from the beginning
358357f1050SThomas Veerman 		 * (skipping only the elements which will definitely not
359357f1050SThomas Veerman 		 * hold the new state).
360357f1050SThomas Veerman 		 */
361357f1050SThomas Veerman 		i = firstfree;
362357f1050SThomas Veerman 
363357f1050SThomas Veerman 	while (1) {		/* loops until a space is found */
364357f1050SThomas Veerman 		while (i + numecs >= current_max_xpairs)
365357f1050SThomas Veerman 			expand_nxt_chk ();
366357f1050SThomas Veerman 
367357f1050SThomas Veerman 		/* Loops until space for end-of-buffer and action number
368357f1050SThomas Veerman 		 * are found.
369357f1050SThomas Veerman 		 */
370357f1050SThomas Veerman 		while (1) {
371357f1050SThomas Veerman 			/* Check for action number space. */
372357f1050SThomas Veerman 			if (chk[i - 1] == 0) {
373357f1050SThomas Veerman 				/* Check for end-of-buffer space. */
374357f1050SThomas Veerman 				if (chk[i] == 0)
375357f1050SThomas Veerman 					break;
376357f1050SThomas Veerman 
377357f1050SThomas Veerman 				else
378357f1050SThomas Veerman 					/* Since i != 0, there is no use
379357f1050SThomas Veerman 					 * checking to see if (++i) - 1 == 0,
380357f1050SThomas Veerman 					 * because that's the same as i == 0,
381357f1050SThomas Veerman 					 * so we skip a space.
382357f1050SThomas Veerman 					 */
383357f1050SThomas Veerman 					i += 2;
384357f1050SThomas Veerman 			}
385357f1050SThomas Veerman 
386357f1050SThomas Veerman 			else
387357f1050SThomas Veerman 				++i;
388357f1050SThomas Veerman 
389357f1050SThomas Veerman 			while (i + numecs >= current_max_xpairs)
390357f1050SThomas Veerman 				expand_nxt_chk ();
391357f1050SThomas Veerman 		}
392357f1050SThomas Veerman 
393357f1050SThomas Veerman 		/* If we started search from the beginning, store the new
394357f1050SThomas Veerman 		 * firstfree for the next call of find_table_space().
395357f1050SThomas Veerman 		 */
396357f1050SThomas Veerman 		if (numtrans <= MAX_XTIONS_FULL_INTERIOR_FIT)
397357f1050SThomas Veerman 			firstfree = i + 1;
398357f1050SThomas Veerman 
399357f1050SThomas Veerman 		/* Check to see if all elements in chk (and therefore nxt)
400357f1050SThomas Veerman 		 * that are needed for the new state have not yet been taken.
401357f1050SThomas Veerman 		 */
402357f1050SThomas Veerman 
403357f1050SThomas Veerman 		state_ptr = &state[1];
404357f1050SThomas Veerman 		ptr_to_last_entry_in_state = &chk[i + numecs + 1];
405357f1050SThomas Veerman 
406357f1050SThomas Veerman 		for (chk_ptr = &chk[i + 1];
407357f1050SThomas Veerman 		     chk_ptr != ptr_to_last_entry_in_state; ++chk_ptr)
408357f1050SThomas Veerman 			if (*(state_ptr++) != 0 && *chk_ptr != 0)
409357f1050SThomas Veerman 				break;
410357f1050SThomas Veerman 
411357f1050SThomas Veerman 		if (chk_ptr == ptr_to_last_entry_in_state)
412357f1050SThomas Veerman 			return i;
413357f1050SThomas Veerman 
414357f1050SThomas Veerman 		else
415357f1050SThomas Veerman 			++i;
416357f1050SThomas Veerman 	}
417357f1050SThomas Veerman }
418357f1050SThomas Veerman 
419357f1050SThomas Veerman 
420357f1050SThomas Veerman /* inittbl - initialize transition tables
421357f1050SThomas Veerman  *
422357f1050SThomas Veerman  * Initializes "firstfree" to be one beyond the end of the table.  Initializes
423357f1050SThomas Veerman  * all "chk" entries to be zero.
424357f1050SThomas Veerman  */
inittbl()425357f1050SThomas Veerman void    inittbl ()
426357f1050SThomas Veerman {
427357f1050SThomas Veerman 	register int i;
428357f1050SThomas Veerman 
429357f1050SThomas Veerman 	zero_out ((char *) chk,
430357f1050SThomas Veerman 
431357f1050SThomas Veerman 		  (size_t) (current_max_xpairs * sizeof (int)));
432357f1050SThomas Veerman 
433357f1050SThomas Veerman 	tblend = 0;
434357f1050SThomas Veerman 	firstfree = tblend + 1;
435357f1050SThomas Veerman 	numtemps = 0;
436357f1050SThomas Veerman 
437357f1050SThomas Veerman 	if (usemecs) {
438357f1050SThomas Veerman 		/* Set up doubly-linked meta-equivalence classes; these
439357f1050SThomas Veerman 		 * are sets of equivalence classes which all have identical
440357f1050SThomas Veerman 		 * transitions out of TEMPLATES.
441357f1050SThomas Veerman 		 */
442357f1050SThomas Veerman 
443357f1050SThomas Veerman 		tecbck[1] = NIL;
444357f1050SThomas Veerman 
445357f1050SThomas Veerman 		for (i = 2; i <= numecs; ++i) {
446357f1050SThomas Veerman 			tecbck[i] = i - 1;
447357f1050SThomas Veerman 			tecfwd[i - 1] = i;
448357f1050SThomas Veerman 		}
449357f1050SThomas Veerman 
450357f1050SThomas Veerman 		tecfwd[numecs] = NIL;
451357f1050SThomas Veerman 	}
452357f1050SThomas Veerman }
453357f1050SThomas Veerman 
454357f1050SThomas Veerman 
455357f1050SThomas Veerman /* mkdeftbl - make the default, "jam" table entries */
456357f1050SThomas Veerman 
mkdeftbl()457357f1050SThomas Veerman void    mkdeftbl ()
458357f1050SThomas Veerman {
459357f1050SThomas Veerman 	int     i;
460357f1050SThomas Veerman 
461357f1050SThomas Veerman 	jamstate = lastdfa + 1;
462357f1050SThomas Veerman 
463357f1050SThomas Veerman 	++tblend;		/* room for transition on end-of-buffer character */
464357f1050SThomas Veerman 
465357f1050SThomas Veerman 	while (tblend + numecs >= current_max_xpairs)
466357f1050SThomas Veerman 		expand_nxt_chk ();
467357f1050SThomas Veerman 
468357f1050SThomas Veerman 	/* Add in default end-of-buffer transition. */
469357f1050SThomas Veerman 	nxt[tblend] = end_of_buffer_state;
470357f1050SThomas Veerman 	chk[tblend] = jamstate;
471357f1050SThomas Veerman 
472357f1050SThomas Veerman 	for (i = 1; i <= numecs; ++i) {
473357f1050SThomas Veerman 		nxt[tblend + i] = 0;
474357f1050SThomas Veerman 		chk[tblend + i] = jamstate;
475357f1050SThomas Veerman 	}
476357f1050SThomas Veerman 
477357f1050SThomas Veerman 	jambase = tblend;
478357f1050SThomas Veerman 
479357f1050SThomas Veerman 	base[jamstate] = jambase;
480357f1050SThomas Veerman 	def[jamstate] = 0;
481357f1050SThomas Veerman 
482357f1050SThomas Veerman 	tblend += numecs;
483357f1050SThomas Veerman 	++numtemps;
484357f1050SThomas Veerman }
485357f1050SThomas Veerman 
486357f1050SThomas Veerman 
487357f1050SThomas Veerman /* mkentry - create base/def and nxt/chk entries for transition array
488357f1050SThomas Veerman  *
489357f1050SThomas Veerman  * synopsis
490357f1050SThomas Veerman  *   int state[numchars + 1], numchars, statenum, deflink, totaltrans;
491357f1050SThomas Veerman  *   mkentry( state, numchars, statenum, deflink, totaltrans );
492357f1050SThomas Veerman  *
493357f1050SThomas Veerman  * "state" is a transition array "numchars" characters in size, "statenum"
494357f1050SThomas Veerman  * is the offset to be used into the base/def tables, and "deflink" is the
495357f1050SThomas Veerman  * entry to put in the "def" table entry.  If "deflink" is equal to
496357f1050SThomas Veerman  * "JAMSTATE", then no attempt will be made to fit zero entries of "state"
497357f1050SThomas Veerman  * (i.e., jam entries) into the table.  It is assumed that by linking to
498357f1050SThomas Veerman  * "JAMSTATE" they will be taken care of.  In any case, entries in "state"
499357f1050SThomas Veerman  * marking transitions to "SAME_TRANS" are treated as though they will be
500357f1050SThomas Veerman  * taken care of by whereever "deflink" points.  "totaltrans" is the total
501357f1050SThomas Veerman  * number of transitions out of the state.  If it is below a certain threshold,
502357f1050SThomas Veerman  * the tables are searched for an interior spot that will accommodate the
503357f1050SThomas Veerman  * state array.
504357f1050SThomas Veerman  */
505357f1050SThomas Veerman 
mkentry(state,numchars,statenum,deflink,totaltrans)506357f1050SThomas Veerman void    mkentry (state, numchars, statenum, deflink, totaltrans)
507357f1050SThomas Veerman      register int *state;
508357f1050SThomas Veerman      int     numchars, statenum, deflink, totaltrans;
509357f1050SThomas Veerman {
510357f1050SThomas Veerman 	register int minec, maxec, i, baseaddr;
511357f1050SThomas Veerman 	int     tblbase, tbllast;
512357f1050SThomas Veerman 
513357f1050SThomas Veerman 	if (totaltrans == 0) {	/* there are no out-transitions */
514357f1050SThomas Veerman 		if (deflink == JAMSTATE)
515357f1050SThomas Veerman 			base[statenum] = JAMSTATE;
516357f1050SThomas Veerman 		else
517357f1050SThomas Veerman 			base[statenum] = 0;
518357f1050SThomas Veerman 
519357f1050SThomas Veerman 		def[statenum] = deflink;
520357f1050SThomas Veerman 		return;
521357f1050SThomas Veerman 	}
522357f1050SThomas Veerman 
523357f1050SThomas Veerman 	for (minec = 1; minec <= numchars; ++minec) {
524357f1050SThomas Veerman 		if (state[minec] != SAME_TRANS)
525357f1050SThomas Veerman 			if (state[minec] != 0 || deflink != JAMSTATE)
526357f1050SThomas Veerman 				break;
527357f1050SThomas Veerman 	}
528357f1050SThomas Veerman 
529357f1050SThomas Veerman 	if (totaltrans == 1) {
530357f1050SThomas Veerman 		/* There's only one out-transition.  Save it for later to fill
531357f1050SThomas Veerman 		 * in holes in the tables.
532357f1050SThomas Veerman 		 */
533357f1050SThomas Veerman 		stack1 (statenum, minec, state[minec], deflink);
534357f1050SThomas Veerman 		return;
535357f1050SThomas Veerman 	}
536357f1050SThomas Veerman 
537357f1050SThomas Veerman 	for (maxec = numchars; maxec > 0; --maxec) {
538357f1050SThomas Veerman 		if (state[maxec] != SAME_TRANS)
539357f1050SThomas Veerman 			if (state[maxec] != 0 || deflink != JAMSTATE)
540357f1050SThomas Veerman 				break;
541357f1050SThomas Veerman 	}
542357f1050SThomas Veerman 
543357f1050SThomas Veerman 	/* Whether we try to fit the state table in the middle of the table
544357f1050SThomas Veerman 	 * entries we have already generated, or if we just take the state
545357f1050SThomas Veerman 	 * table at the end of the nxt/chk tables, we must make sure that we
546357f1050SThomas Veerman 	 * have a valid base address (i.e., non-negative).  Note that
547357f1050SThomas Veerman 	 * negative base addresses dangerous at run-time (because indexing
548357f1050SThomas Veerman 	 * the nxt array with one and a low-valued character will access
549357f1050SThomas Veerman 	 * memory before the start of the array.
550357f1050SThomas Veerman 	 */
551357f1050SThomas Veerman 
552357f1050SThomas Veerman 	/* Find the first transition of state that we need to worry about. */
553357f1050SThomas Veerman 	if (totaltrans * 100 <= numchars * INTERIOR_FIT_PERCENTAGE) {
554357f1050SThomas Veerman 		/* Attempt to squeeze it into the middle of the tables. */
555357f1050SThomas Veerman 		baseaddr = firstfree;
556357f1050SThomas Veerman 
557357f1050SThomas Veerman 		while (baseaddr < minec) {
558357f1050SThomas Veerman 			/* Using baseaddr would result in a negative base
559357f1050SThomas Veerman 			 * address below; find the next free slot.
560357f1050SThomas Veerman 			 */
561357f1050SThomas Veerman 			for (++baseaddr; chk[baseaddr] != 0; ++baseaddr) ;
562357f1050SThomas Veerman 		}
563357f1050SThomas Veerman 
564357f1050SThomas Veerman 		while (baseaddr + maxec - minec + 1 >= current_max_xpairs)
565357f1050SThomas Veerman 			expand_nxt_chk ();
566357f1050SThomas Veerman 
567357f1050SThomas Veerman 		for (i = minec; i <= maxec; ++i)
568357f1050SThomas Veerman 			if (state[i] != SAME_TRANS &&
569357f1050SThomas Veerman 			    (state[i] != 0 || deflink != JAMSTATE) &&
570357f1050SThomas Veerman 			    chk[baseaddr + i - minec] != 0) {	/* baseaddr unsuitable - find another */
571357f1050SThomas Veerman 				for (++baseaddr;
572357f1050SThomas Veerman 				     baseaddr < current_max_xpairs &&
573357f1050SThomas Veerman 				     chk[baseaddr] != 0; ++baseaddr) ;
574357f1050SThomas Veerman 
575357f1050SThomas Veerman 				while (baseaddr + maxec - minec + 1 >=
576357f1050SThomas Veerman 				       current_max_xpairs)
577357f1050SThomas Veerman 						expand_nxt_chk ();
578357f1050SThomas Veerman 
579357f1050SThomas Veerman 				/* Reset the loop counter so we'll start all
580357f1050SThomas Veerman 				 * over again next time it's incremented.
581357f1050SThomas Veerman 				 */
582357f1050SThomas Veerman 
583357f1050SThomas Veerman 				i = minec - 1;
584357f1050SThomas Veerman 			}
585357f1050SThomas Veerman 	}
586357f1050SThomas Veerman 
587357f1050SThomas Veerman 	else {
588357f1050SThomas Veerman 		/* Ensure that the base address we eventually generate is
589357f1050SThomas Veerman 		 * non-negative.
590357f1050SThomas Veerman 		 */
591357f1050SThomas Veerman 		baseaddr = MAX (tblend + 1, minec);
592357f1050SThomas Veerman 	}
593357f1050SThomas Veerman 
594357f1050SThomas Veerman 	tblbase = baseaddr - minec;
595357f1050SThomas Veerman 	tbllast = tblbase + maxec;
596357f1050SThomas Veerman 
597357f1050SThomas Veerman 	while (tbllast + 1 >= current_max_xpairs)
598357f1050SThomas Veerman 		expand_nxt_chk ();
599357f1050SThomas Veerman 
600357f1050SThomas Veerman 	base[statenum] = tblbase;
601357f1050SThomas Veerman 	def[statenum] = deflink;
602357f1050SThomas Veerman 
603357f1050SThomas Veerman 	for (i = minec; i <= maxec; ++i)
604357f1050SThomas Veerman 		if (state[i] != SAME_TRANS)
605357f1050SThomas Veerman 			if (state[i] != 0 || deflink != JAMSTATE) {
606357f1050SThomas Veerman 				nxt[tblbase + i] = state[i];
607357f1050SThomas Veerman 				chk[tblbase + i] = statenum;
608357f1050SThomas Veerman 			}
609357f1050SThomas Veerman 
610357f1050SThomas Veerman 	if (baseaddr == firstfree)
611357f1050SThomas Veerman 		/* Find next free slot in tables. */
612357f1050SThomas Veerman 		for (++firstfree; chk[firstfree] != 0; ++firstfree) ;
613357f1050SThomas Veerman 
614357f1050SThomas Veerman 	tblend = MAX (tblend, tbllast);
615357f1050SThomas Veerman }
616357f1050SThomas Veerman 
617357f1050SThomas Veerman 
618357f1050SThomas Veerman /* mk1tbl - create table entries for a state (or state fragment) which
619357f1050SThomas Veerman  *            has only one out-transition
620357f1050SThomas Veerman  */
621357f1050SThomas Veerman 
mk1tbl(state,sym,onenxt,onedef)622357f1050SThomas Veerman void    mk1tbl (state, sym, onenxt, onedef)
623357f1050SThomas Veerman      int     state, sym, onenxt, onedef;
624357f1050SThomas Veerman {
625357f1050SThomas Veerman 	if (firstfree < sym)
626357f1050SThomas Veerman 		firstfree = sym;
627357f1050SThomas Veerman 
628357f1050SThomas Veerman 	while (chk[firstfree] != 0)
629357f1050SThomas Veerman 		if (++firstfree >= current_max_xpairs)
630357f1050SThomas Veerman 			expand_nxt_chk ();
631357f1050SThomas Veerman 
632357f1050SThomas Veerman 	base[state] = firstfree - sym;
633357f1050SThomas Veerman 	def[state] = onedef;
634357f1050SThomas Veerman 	chk[firstfree] = state;
635357f1050SThomas Veerman 	nxt[firstfree] = onenxt;
636357f1050SThomas Veerman 
637357f1050SThomas Veerman 	if (firstfree > tblend) {
638357f1050SThomas Veerman 		tblend = firstfree++;
639357f1050SThomas Veerman 
640357f1050SThomas Veerman 		if (firstfree >= current_max_xpairs)
641357f1050SThomas Veerman 			expand_nxt_chk ();
642357f1050SThomas Veerman 	}
643357f1050SThomas Veerman }
644357f1050SThomas Veerman 
645357f1050SThomas Veerman 
646357f1050SThomas Veerman /* mkprot - create new proto entry */
647357f1050SThomas Veerman 
mkprot(state,statenum,comstate)648357f1050SThomas Veerman void    mkprot (state, statenum, comstate)
649357f1050SThomas Veerman      int     state[], statenum, comstate;
650357f1050SThomas Veerman {
651357f1050SThomas Veerman 	int     i, slot, tblbase;
652357f1050SThomas Veerman 
653357f1050SThomas Veerman 	if (++numprots >= MSP || numecs * numprots >= PROT_SAVE_SIZE) {
654357f1050SThomas Veerman 		/* Gotta make room for the new proto by dropping last entry in
655357f1050SThomas Veerman 		 * the queue.
656357f1050SThomas Veerman 		 */
657357f1050SThomas Veerman 		slot = lastprot;
658357f1050SThomas Veerman 		lastprot = protprev[lastprot];
659357f1050SThomas Veerman 		protnext[lastprot] = NIL;
660357f1050SThomas Veerman 	}
661357f1050SThomas Veerman 
662357f1050SThomas Veerman 	else
663357f1050SThomas Veerman 		slot = numprots;
664357f1050SThomas Veerman 
665357f1050SThomas Veerman 	protnext[slot] = firstprot;
666357f1050SThomas Veerman 
667357f1050SThomas Veerman 	if (firstprot != NIL)
668357f1050SThomas Veerman 		protprev[firstprot] = slot;
669357f1050SThomas Veerman 
670357f1050SThomas Veerman 	firstprot = slot;
671357f1050SThomas Veerman 	prottbl[slot] = statenum;
672357f1050SThomas Veerman 	protcomst[slot] = comstate;
673357f1050SThomas Veerman 
674357f1050SThomas Veerman 	/* Copy state into save area so it can be compared with rapidly. */
675357f1050SThomas Veerman 	tblbase = numecs * (slot - 1);
676357f1050SThomas Veerman 
677357f1050SThomas Veerman 	for (i = 1; i <= numecs; ++i)
678357f1050SThomas Veerman 		protsave[tblbase + i] = state[i];
679357f1050SThomas Veerman }
680357f1050SThomas Veerman 
681357f1050SThomas Veerman 
682357f1050SThomas Veerman /* mktemplate - create a template entry based on a state, and connect the state
683357f1050SThomas Veerman  *              to it
684357f1050SThomas Veerman  */
685357f1050SThomas Veerman 
mktemplate(state,statenum,comstate)686357f1050SThomas Veerman void    mktemplate (state, statenum, comstate)
687357f1050SThomas Veerman      int     state[], statenum, comstate;
688357f1050SThomas Veerman {
689357f1050SThomas Veerman 	int     i, numdiff, tmpbase, tmp[CSIZE + 1];
690357f1050SThomas Veerman 	Char    transset[CSIZE + 1];
691357f1050SThomas Veerman 	int     tsptr;
692357f1050SThomas Veerman 
693357f1050SThomas Veerman 	++numtemps;
694357f1050SThomas Veerman 
695357f1050SThomas Veerman 	tsptr = 0;
696357f1050SThomas Veerman 
697357f1050SThomas Veerman 	/* Calculate where we will temporarily store the transition table
698357f1050SThomas Veerman 	 * of the template in the tnxt[] array.  The final transition table
699357f1050SThomas Veerman 	 * gets created by cmptmps().
700357f1050SThomas Veerman 	 */
701357f1050SThomas Veerman 
702357f1050SThomas Veerman 	tmpbase = numtemps * numecs;
703357f1050SThomas Veerman 
704357f1050SThomas Veerman 	if (tmpbase + numecs >= current_max_template_xpairs) {
705357f1050SThomas Veerman 		current_max_template_xpairs +=
706357f1050SThomas Veerman 			MAX_TEMPLATE_XPAIRS_INCREMENT;
707357f1050SThomas Veerman 
708357f1050SThomas Veerman 		++num_reallocs;
709357f1050SThomas Veerman 
710357f1050SThomas Veerman 		tnxt = reallocate_integer_array (tnxt,
711357f1050SThomas Veerman 						 current_max_template_xpairs);
712357f1050SThomas Veerman 	}
713357f1050SThomas Veerman 
714357f1050SThomas Veerman 	for (i = 1; i <= numecs; ++i)
715357f1050SThomas Veerman 		if (state[i] == 0)
716357f1050SThomas Veerman 			tnxt[tmpbase + i] = 0;
717357f1050SThomas Veerman 		else {
718357f1050SThomas Veerman 			transset[tsptr++] = i;
719357f1050SThomas Veerman 			tnxt[tmpbase + i] = comstate;
720357f1050SThomas Veerman 		}
721357f1050SThomas Veerman 
722357f1050SThomas Veerman 	if (usemecs)
723357f1050SThomas Veerman 		mkeccl (transset, tsptr, tecfwd, tecbck, numecs, 0);
724357f1050SThomas Veerman 
725357f1050SThomas Veerman 	mkprot (tnxt + tmpbase, -numtemps, comstate);
726357f1050SThomas Veerman 
727357f1050SThomas Veerman 	/* We rely on the fact that mkprot adds things to the beginning
728357f1050SThomas Veerman 	 * of the proto queue.
729357f1050SThomas Veerman 	 */
730357f1050SThomas Veerman 
731357f1050SThomas Veerman 	numdiff = tbldiff (state, firstprot, tmp);
732357f1050SThomas Veerman 	mkentry (tmp, numecs, statenum, -numtemps, numdiff);
733357f1050SThomas Veerman }
734357f1050SThomas Veerman 
735357f1050SThomas Veerman 
736357f1050SThomas Veerman /* mv2front - move proto queue element to front of queue */
737357f1050SThomas Veerman 
mv2front(qelm)738357f1050SThomas Veerman void    mv2front (qelm)
739357f1050SThomas Veerman      int     qelm;
740357f1050SThomas Veerman {
741357f1050SThomas Veerman 	if (firstprot != qelm) {
742357f1050SThomas Veerman 		if (qelm == lastprot)
743357f1050SThomas Veerman 			lastprot = protprev[lastprot];
744357f1050SThomas Veerman 
745357f1050SThomas Veerman 		protnext[protprev[qelm]] = protnext[qelm];
746357f1050SThomas Veerman 
747357f1050SThomas Veerman 		if (protnext[qelm] != NIL)
748357f1050SThomas Veerman 			protprev[protnext[qelm]] = protprev[qelm];
749357f1050SThomas Veerman 
750357f1050SThomas Veerman 		protprev[qelm] = NIL;
751357f1050SThomas Veerman 		protnext[qelm] = firstprot;
752357f1050SThomas Veerman 		protprev[firstprot] = qelm;
753357f1050SThomas Veerman 		firstprot = qelm;
754357f1050SThomas Veerman 	}
755357f1050SThomas Veerman }
756357f1050SThomas Veerman 
757357f1050SThomas Veerman 
758357f1050SThomas Veerman /* place_state - place a state into full speed transition table
759357f1050SThomas Veerman  *
760357f1050SThomas Veerman  * State is the statenum'th state.  It is indexed by equivalence class and
761357f1050SThomas Veerman  * gives the number of the state to enter for a given equivalence class.
762357f1050SThomas Veerman  * Transnum is the number of out-transitions for the state.
763357f1050SThomas Veerman  */
764357f1050SThomas Veerman 
place_state(state,statenum,transnum)765357f1050SThomas Veerman void    place_state (state, statenum, transnum)
766357f1050SThomas Veerman      int    *state, statenum, transnum;
767357f1050SThomas Veerman {
768357f1050SThomas Veerman 	register int i;
769357f1050SThomas Veerman 	register int *state_ptr;
770357f1050SThomas Veerman 	int     position = find_table_space (state, transnum);
771357f1050SThomas Veerman 
772357f1050SThomas Veerman 	/* "base" is the table of start positions. */
773357f1050SThomas Veerman 	base[statenum] = position;
774357f1050SThomas Veerman 
775357f1050SThomas Veerman 	/* Put in action number marker; this non-zero number makes sure that
776357f1050SThomas Veerman 	 * find_table_space() knows that this position in chk/nxt is taken
777357f1050SThomas Veerman 	 * and should not be used for another accepting number in another
778357f1050SThomas Veerman 	 * state.
779357f1050SThomas Veerman 	 */
780357f1050SThomas Veerman 	chk[position - 1] = 1;
781357f1050SThomas Veerman 
782357f1050SThomas Veerman 	/* Put in end-of-buffer marker; this is for the same purposes as
783357f1050SThomas Veerman 	 * above.
784357f1050SThomas Veerman 	 */
785357f1050SThomas Veerman 	chk[position] = 1;
786357f1050SThomas Veerman 
787357f1050SThomas Veerman 	/* Place the state into chk and nxt. */
788357f1050SThomas Veerman 	state_ptr = &state[1];
789357f1050SThomas Veerman 
790357f1050SThomas Veerman 	for (i = 1; i <= numecs; ++i, ++state_ptr)
791357f1050SThomas Veerman 		if (*state_ptr != 0) {
792357f1050SThomas Veerman 			chk[position + i] = i;
793357f1050SThomas Veerman 			nxt[position + i] = *state_ptr;
794357f1050SThomas Veerman 		}
795357f1050SThomas Veerman 
796357f1050SThomas Veerman 	if (position + numecs > tblend)
797357f1050SThomas Veerman 		tblend = position + numecs;
798357f1050SThomas Veerman }
799357f1050SThomas Veerman 
800357f1050SThomas Veerman 
801357f1050SThomas Veerman /* stack1 - save states with only one out-transition to be processed later
802357f1050SThomas Veerman  *
803357f1050SThomas Veerman  * If there's room for another state on the "one-transition" stack, the
804357f1050SThomas Veerman  * state is pushed onto it, to be processed later by mk1tbl.  If there's
805357f1050SThomas Veerman  * no room, we process the sucker right now.
806357f1050SThomas Veerman  */
807357f1050SThomas Veerman 
stack1(statenum,sym,nextstate,deflink)808357f1050SThomas Veerman void    stack1 (statenum, sym, nextstate, deflink)
809357f1050SThomas Veerman      int     statenum, sym, nextstate, deflink;
810357f1050SThomas Veerman {
811357f1050SThomas Veerman 	if (onesp >= ONE_STACK_SIZE - 1)
812357f1050SThomas Veerman 		mk1tbl (statenum, sym, nextstate, deflink);
813357f1050SThomas Veerman 
814357f1050SThomas Veerman 	else {
815357f1050SThomas Veerman 		++onesp;
816357f1050SThomas Veerman 		onestate[onesp] = statenum;
817357f1050SThomas Veerman 		onesym[onesp] = sym;
818357f1050SThomas Veerman 		onenext[onesp] = nextstate;
819357f1050SThomas Veerman 		onedef[onesp] = deflink;
820357f1050SThomas Veerman 	}
821357f1050SThomas Veerman }
822357f1050SThomas Veerman 
823357f1050SThomas Veerman 
824357f1050SThomas Veerman /* tbldiff - compute differences between two state tables
825357f1050SThomas Veerman  *
826357f1050SThomas Veerman  * "state" is the state array which is to be extracted from the pr'th
827357f1050SThomas Veerman  * proto.  "pr" is both the number of the proto we are extracting from
828357f1050SThomas Veerman  * and an index into the save area where we can find the proto's complete
829357f1050SThomas Veerman  * state table.  Each entry in "state" which differs from the corresponding
830357f1050SThomas Veerman  * entry of "pr" will appear in "ext".
831357f1050SThomas Veerman  *
832357f1050SThomas Veerman  * Entries which are the same in both "state" and "pr" will be marked
833357f1050SThomas Veerman  * as transitions to "SAME_TRANS" in "ext".  The total number of differences
834357f1050SThomas Veerman  * between "state" and "pr" is returned as function value.  Note that this
835357f1050SThomas Veerman  * number is "numecs" minus the number of "SAME_TRANS" entries in "ext".
836357f1050SThomas Veerman  */
837357f1050SThomas Veerman 
tbldiff(state,pr,ext)838357f1050SThomas Veerman int     tbldiff (state, pr, ext)
839357f1050SThomas Veerman      int     state[], pr, ext[];
840357f1050SThomas Veerman {
841357f1050SThomas Veerman 	register int i, *sp = state, *ep = ext, *protp;
842357f1050SThomas Veerman 	register int numdiff = 0;
843357f1050SThomas Veerman 
844357f1050SThomas Veerman 	protp = &protsave[numecs * (pr - 1)];
845357f1050SThomas Veerman 
846357f1050SThomas Veerman 	for (i = numecs; i > 0; --i) {
847357f1050SThomas Veerman 		if (*++protp == *++sp)
848357f1050SThomas Veerman 			*++ep = SAME_TRANS;
849357f1050SThomas Veerman 		else {
850357f1050SThomas Veerman 			*++ep = *sp;
851357f1050SThomas Veerman 			++numdiff;
852357f1050SThomas Veerman 		}
853357f1050SThomas Veerman 	}
854357f1050SThomas Veerman 
855357f1050SThomas Veerman 	return numdiff;
856357f1050SThomas Veerman }
857