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