Blame src/tblcmp.c

Packit f00812
/* tblcmp - table compression routines */
Packit f00812
Packit f00812
/*  Copyright (c) 1990 The Regents of the University of California. */
Packit f00812
/*  All rights reserved. */
Packit f00812
Packit f00812
/*  This code is derived from software contributed to Berkeley by */
Packit f00812
/*  Vern Paxson. */
Packit f00812
Packit f00812
/*  The United States Government has rights in this work pursuant */
Packit f00812
/*  to contract no. DE-AC03-76SF00098 between the United States */
Packit f00812
/*  Department of Energy and the University of California. */
Packit f00812
Packit f00812
/*  This file is part of flex. */
Packit f00812
Packit f00812
/*  Redistribution and use in source and binary forms, with or without */
Packit f00812
/*  modification, are permitted provided that the following conditions */
Packit f00812
/*  are met: */
Packit f00812
Packit f00812
/*  1. Redistributions of source code must retain the above copyright */
Packit f00812
/*     notice, this list of conditions and the following disclaimer. */
Packit f00812
/*  2. Redistributions in binary form must reproduce the above copyright */
Packit f00812
/*     notice, this list of conditions and the following disclaimer in the */
Packit f00812
/*     documentation and/or other materials provided with the distribution. */
Packit f00812
Packit f00812
/*  Neither the name of the University nor the names of its contributors */
Packit f00812
/*  may be used to endorse or promote products derived from this software */
Packit f00812
/*  without specific prior written permission. */
Packit f00812
Packit f00812
/*  THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR */
Packit f00812
/*  IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED */
Packit f00812
/*  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR */
Packit f00812
/*  PURPOSE. */
Packit f00812
Packit f00812
#include "flexdef.h"
Packit f00812
Packit f00812
Packit f00812
/* declarations for functions that have forward references */
Packit f00812
Packit f00812
void mkentry(int *, int, int, int, int);
Packit f00812
void mkprot(int[], int, int);
Packit f00812
void mktemplate(int[], int, int);
Packit f00812
void mv2front(int);
Packit f00812
int tbldiff(int[], int, int[]);
Packit f00812
Packit f00812
Packit f00812
/* bldtbl - build table entries for dfa state
Packit f00812
 *
Packit f00812
 * synopsis
Packit f00812
 *   int state[numecs], statenum, totaltrans, comstate, comfreq;
Packit f00812
 *   bldtbl( state, statenum, totaltrans, comstate, comfreq );
Packit f00812
 *
Packit f00812
 * State is the statenum'th dfa state.  It is indexed by equivalence class and
Packit f00812
 * gives the number of the state to enter for a given equivalence class.
Packit f00812
 * totaltrans is the total number of transitions out of the state.  Comstate
Packit f00812
 * is that state which is the destination of the most transitions out of State.
Packit f00812
 * Comfreq is how many transitions there are out of State to Comstate.
Packit f00812
 *
Packit f00812
 * A note on terminology:
Packit f00812
 *    "protos" are transition tables which have a high probability of
Packit f00812
 * either being redundant (a state processed later will have an identical
Packit f00812
 * transition table) or nearly redundant (a state processed later will have
Packit f00812
 * many of the same out-transitions).  A "most recently used" queue of
Packit f00812
 * protos is kept around with the hope that most states will find a proto
Packit f00812
 * which is similar enough to be usable, and therefore compacting the
Packit f00812
 * output tables.
Packit f00812
 *    "templates" are a special type of proto.  If a transition table is
Packit f00812
 * homogeneous or nearly homogeneous (all transitions go to the same
Packit f00812
 * destination) then the odds are good that future states will also go
Packit f00812
 * to the same destination state on basically the same character set.
Packit f00812
 * These homogeneous states are so common when dealing with large rule
Packit f00812
 * sets that they merit special attention.  If the transition table were
Packit f00812
 * simply made into a proto, then (typically) each subsequent, similar
Packit f00812
 * state will differ from the proto for two out-transitions.  One of these
Packit f00812
 * out-transitions will be that character on which the proto does not go
Packit f00812
 * to the common destination, and one will be that character on which the
Packit f00812
 * state does not go to the common destination.  Templates, on the other
Packit f00812
 * hand, go to the common state on EVERY transition character, and therefore
Packit f00812
 * cost only one difference.
Packit f00812
 */
Packit f00812
Packit f00812
void    bldtbl (int state[], int statenum, int totaltrans, int comstate, int comfreq)
Packit f00812
{
Packit f00812
	int     extptr, extrct[2][CSIZE + 1];
Packit f00812
	int     mindiff, minprot, i, d;
Packit f00812
Packit f00812
	/* If extptr is 0 then the first array of extrct holds the result
Packit f00812
	 * of the "best difference" to date, which is those transitions
Packit f00812
	 * which occur in "state" but not in the proto which, to date,
Packit f00812
	 * has the fewest differences between itself and "state".  If
Packit f00812
	 * extptr is 1 then the second array of extrct hold the best
Packit f00812
	 * difference.  The two arrays are toggled between so that the
Packit f00812
	 * best difference to date can be kept around and also a difference
Packit f00812
	 * just created by checking against a candidate "best" proto.
Packit f00812
	 */
Packit f00812
Packit f00812
	extptr = 0;
Packit f00812
Packit f00812
	/* If the state has too few out-transitions, don't bother trying to
Packit f00812
	 * compact its tables.
Packit f00812
	 */
Packit f00812
Packit f00812
	if ((totaltrans * 100) < (numecs * PROTO_SIZE_PERCENTAGE))
Packit f00812
		mkentry (state, numecs, statenum, JAMSTATE, totaltrans);
Packit f00812
Packit f00812
	else {
Packit f00812
		/* "checkcom" is true if we should only check "state" against
Packit f00812
		 * protos which have the same "comstate" value.
Packit f00812
		 */
Packit f00812
		int     checkcom =
Packit f00812
Packit f00812
			comfreq * 100 > totaltrans * CHECK_COM_PERCENTAGE;
Packit f00812
Packit f00812
		minprot = firstprot;
Packit f00812
		mindiff = totaltrans;
Packit f00812
Packit f00812
		if (checkcom) {
Packit f00812
			/* Find first proto which has the same "comstate". */
Packit f00812
			for (i = firstprot; i != NIL; i = protnext[i])
Packit f00812
				if (protcomst[i] == comstate) {
Packit f00812
					minprot = i;
Packit f00812
					mindiff = tbldiff (state, minprot,
Packit f00812
							   extrct[extptr]);
Packit f00812
					break;
Packit f00812
				}
Packit f00812
		}
Packit f00812
Packit f00812
		else {
Packit f00812
			/* Since we've decided that the most common destination
Packit f00812
			 * out of "state" does not occur with a high enough
Packit f00812
			 * frequency, we set the "comstate" to zero, assuring
Packit f00812
			 * that if this state is entered into the proto list,
Packit f00812
			 * it will not be considered a template.
Packit f00812
			 */
Packit f00812
			comstate = 0;
Packit f00812
Packit f00812
			if (firstprot != NIL) {
Packit f00812
				minprot = firstprot;
Packit f00812
				mindiff = tbldiff (state, minprot,
Packit f00812
						   extrct[extptr]);
Packit f00812
			}
Packit f00812
		}
Packit f00812
Packit f00812
		/* We now have the first interesting proto in "minprot".  If
Packit f00812
		 * it matches within the tolerances set for the first proto,
Packit f00812
		 * we don't want to bother scanning the rest of the proto list
Packit f00812
		 * to see if we have any other reasonable matches.
Packit f00812
		 */
Packit f00812
Packit f00812
		if (mindiff * 100 >
Packit f00812
		    totaltrans * FIRST_MATCH_DIFF_PERCENTAGE) {
Packit f00812
			/* Not a good enough match.  Scan the rest of the
Packit f00812
			 * protos.
Packit f00812
			 */
Packit f00812
			for (i = minprot; i != NIL; i = protnext[i]) {
Packit f00812
				d = tbldiff (state, i, extrct[1 - extptr]);
Packit f00812
				if (d < mindiff) {
Packit f00812
					extptr = 1 - extptr;
Packit f00812
					mindiff = d;
Packit f00812
					minprot = i;
Packit f00812
				}
Packit f00812
			}
Packit f00812
		}
Packit f00812
Packit f00812
		/* Check if the proto we've decided on as our best bet is close
Packit f00812
		 * enough to the state we want to match to be usable.
Packit f00812
		 */
Packit f00812
Packit f00812
		if (mindiff * 100 >
Packit f00812
		    totaltrans * ACCEPTABLE_DIFF_PERCENTAGE) {
Packit f00812
			/* No good.  If the state is homogeneous enough,
Packit f00812
			 * we make a template out of it.  Otherwise, we
Packit f00812
			 * make a proto.
Packit f00812
			 */
Packit f00812
Packit f00812
			if (comfreq * 100 >=
Packit f00812
			    totaltrans * TEMPLATE_SAME_PERCENTAGE)
Packit f00812
					mktemplate (state, statenum,
Packit f00812
						    comstate);
Packit f00812
Packit f00812
			else {
Packit f00812
				mkprot (state, statenum, comstate);
Packit f00812
				mkentry (state, numecs, statenum,
Packit f00812
					 JAMSTATE, totaltrans);
Packit f00812
			}
Packit f00812
		}
Packit f00812
Packit f00812
		else {		/* use the proto */
Packit f00812
			mkentry (extrct[extptr], numecs, statenum,
Packit f00812
				 prottbl[minprot], mindiff);
Packit f00812
Packit f00812
			/* If this state was sufficiently different from the
Packit f00812
			 * proto we built it from, make it, too, a proto.
Packit f00812
			 */
Packit f00812
Packit f00812
			if (mindiff * 100 >=
Packit f00812
			    totaltrans * NEW_PROTO_DIFF_PERCENTAGE)
Packit f00812
					mkprot (state, statenum, comstate);
Packit f00812
Packit f00812
			/* Since mkprot added a new proto to the proto queue,
Packit f00812
			 * it's possible that "minprot" is no longer on the
Packit f00812
			 * proto queue (if it happened to have been the last
Packit f00812
			 * entry, it would have been bumped off).  If it's
Packit f00812
			 * not there, then the new proto took its physical
Packit f00812
			 * place (though logically the new proto is at the
Packit f00812
			 * beginning of the queue), so in that case the
Packit f00812
			 * following call will do nothing.
Packit f00812
			 */
Packit f00812
Packit f00812
			mv2front (minprot);
Packit f00812
		}
Packit f00812
	}
Packit f00812
}
Packit f00812
Packit f00812
Packit f00812
/* cmptmps - compress template table entries
Packit f00812
 *
Packit f00812
 * Template tables are compressed by using the 'template equivalence
Packit f00812
 * classes', which are collections of transition character equivalence
Packit f00812
 * classes which always appear together in templates - really meta-equivalence
Packit f00812
 * classes.
Packit f00812
 */
Packit f00812
Packit f00812
void    cmptmps (void)
Packit f00812
{
Packit f00812
	int tmpstorage[CSIZE + 1];
Packit f00812
	int *tmp = tmpstorage, i, j;
Packit f00812
	int totaltrans, trans;
Packit f00812
Packit f00812
	peakpairs = numtemps * numecs + tblend;
Packit f00812
Packit f00812
	if (usemecs) {
Packit f00812
		/* Create equivalence classes based on data gathered on
Packit f00812
		 * template transitions.
Packit f00812
		 */
Packit f00812
		nummecs = cre8ecs (tecfwd, tecbck, numecs);
Packit f00812
	}
Packit f00812
Packit f00812
	else
Packit f00812
		nummecs = numecs;
Packit f00812
Packit f00812
	while (lastdfa + numtemps + 1 >= current_max_dfas)
Packit f00812
		increase_max_dfas ();
Packit f00812
Packit f00812
	/* Loop through each template. */
Packit f00812
Packit f00812
	for (i = 1; i <= numtemps; ++i) {
Packit f00812
		/* Number of non-jam transitions out of this template. */
Packit f00812
		totaltrans = 0;
Packit f00812
Packit f00812
		for (j = 1; j <= numecs; ++j) {
Packit f00812
			trans = tnxt[numecs * i + j];
Packit f00812
Packit f00812
			if (usemecs) {
Packit f00812
				/* The absolute value of tecbck is the
Packit f00812
				 * meta-equivalence class of a given
Packit f00812
				 * equivalence class, as set up by cre8ecs().
Packit f00812
				 */
Packit f00812
				if (tecbck[j] > 0) {
Packit f00812
					tmp[tecbck[j]] = trans;
Packit f00812
Packit f00812
					if (trans > 0)
Packit f00812
						++totaltrans;
Packit f00812
				}
Packit f00812
			}
Packit f00812
Packit f00812
			else {
Packit f00812
				tmp[j] = trans;
Packit f00812
Packit f00812
				if (trans > 0)
Packit f00812
					++totaltrans;
Packit f00812
			}
Packit f00812
		}
Packit f00812
Packit f00812
		/* It is assumed (in a rather subtle way) in the skeleton
Packit f00812
		 * that if we're using meta-equivalence classes, the def[]
Packit f00812
		 * entry for all templates is the jam template, i.e.,
Packit f00812
		 * templates never default to other non-jam table entries
Packit f00812
		 * (e.g., another template)
Packit f00812
		 */
Packit f00812
Packit f00812
		/* Leave room for the jam-state after the last real state. */
Packit f00812
		mkentry (tmp, nummecs, lastdfa + i + 1, JAMSTATE,
Packit f00812
			 totaltrans);
Packit f00812
	}
Packit f00812
}
Packit f00812
Packit f00812
Packit f00812
Packit f00812
/* expand_nxt_chk - expand the next check arrays */
Packit f00812
Packit f00812
void    expand_nxt_chk (void)
Packit f00812
{
Packit f00812
	int old_max = current_max_xpairs;
Packit f00812
Packit f00812
	current_max_xpairs += MAX_XPAIRS_INCREMENT;
Packit f00812
Packit f00812
	++num_reallocs;
Packit f00812
Packit f00812
	nxt = reallocate_integer_array (nxt, current_max_xpairs);
Packit f00812
	chk = reallocate_integer_array (chk, current_max_xpairs);
Packit f00812
Packit f00812
	memset(chk + old_max, 0, MAX_XPAIRS_INCREMENT * sizeof(int));
Packit f00812
}
Packit f00812
Packit f00812
Packit f00812
/* find_table_space - finds a space in the table for a state to be placed
Packit f00812
 *
Packit f00812
 * synopsis
Packit f00812
 *     int *state, numtrans, block_start;
Packit f00812
 *     int find_table_space();
Packit f00812
 *
Packit f00812
 *     block_start = find_table_space( state, numtrans );
Packit f00812
 *
Packit f00812
 * State is the state to be added to the full speed transition table.
Packit f00812
 * Numtrans is the number of out-transitions for the state.
Packit f00812
 *
Packit f00812
 * find_table_space() returns the position of the start of the first block (in
Packit f00812
 * chk) able to accommodate the state
Packit f00812
 *
Packit f00812
 * In determining if a state will or will not fit, find_table_space() must take
Packit f00812
 * into account the fact that an end-of-buffer state will be added at [0],
Packit f00812
 * and an action number will be added in [-1].
Packit f00812
 */
Packit f00812
Packit f00812
int     find_table_space (int *state, int numtrans)
Packit f00812
{
Packit f00812
	/* Firstfree is the position of the first possible occurrence of two
Packit f00812
	 * consecutive unused records in the chk and nxt arrays.
Packit f00812
	 */
Packit f00812
	int i;
Packit f00812
	int *state_ptr, *chk_ptr;
Packit f00812
	int *ptr_to_last_entry_in_state;
Packit f00812
Packit f00812
	/* If there are too many out-transitions, put the state at the end of
Packit f00812
	 * nxt and chk.
Packit f00812
	 */
Packit f00812
	if (numtrans > MAX_XTIONS_FULL_INTERIOR_FIT) {
Packit f00812
		/* If table is empty, return the first available spot in
Packit f00812
		 * chk/nxt, which should be 1.
Packit f00812
		 */
Packit f00812
		if (tblend < 2)
Packit f00812
			return 1;
Packit f00812
Packit f00812
		/* Start searching for table space near the end of
Packit f00812
		 * chk/nxt arrays.
Packit f00812
		 */
Packit f00812
		i = tblend - numecs;
Packit f00812
	}
Packit f00812
Packit f00812
	else
Packit f00812
		/* Start searching for table space from the beginning
Packit f00812
		 * (skipping only the elements which will definitely not
Packit f00812
		 * hold the new state).
Packit f00812
		 */
Packit f00812
		i = firstfree;
Packit f00812
Packit f00812
	while (1) {		/* loops until a space is found */
Packit f00812
		while (i + numecs >= current_max_xpairs)
Packit f00812
			expand_nxt_chk ();
Packit f00812
Packit f00812
		/* Loops until space for end-of-buffer and action number
Packit f00812
		 * are found.
Packit f00812
		 */
Packit f00812
		while (1) {
Packit f00812
			/* Check for action number space. */
Packit f00812
			if (chk[i - 1] == 0) {
Packit f00812
				/* Check for end-of-buffer space. */
Packit f00812
				if (chk[i] == 0)
Packit f00812
					break;
Packit f00812
Packit f00812
				else
Packit f00812
					/* Since i != 0, there is no use
Packit f00812
					 * checking to see if (++i) - 1 == 0,
Packit f00812
					 * because that's the same as i == 0,
Packit f00812
					 * so we skip a space.
Packit f00812
					 */
Packit f00812
					i += 2;
Packit f00812
			}
Packit f00812
Packit f00812
			else
Packit f00812
				++i;
Packit f00812
Packit f00812
			while (i + numecs >= current_max_xpairs)
Packit f00812
				expand_nxt_chk ();
Packit f00812
		}
Packit f00812
Packit f00812
		/* If we started search from the beginning, store the new
Packit f00812
		 * firstfree for the next call of find_table_space().
Packit f00812
		 */
Packit f00812
		if (numtrans <= MAX_XTIONS_FULL_INTERIOR_FIT)
Packit f00812
			firstfree = i + 1;
Packit f00812
Packit f00812
		/* Check to see if all elements in chk (and therefore nxt)
Packit f00812
		 * that are needed for the new state have not yet been taken.
Packit f00812
		 */
Packit f00812
Packit f00812
		state_ptr = &state[1];
Packit f00812
		ptr_to_last_entry_in_state = &chk[i + numecs + 1];
Packit f00812
Packit f00812
		for (chk_ptr = &chk[i + 1];
Packit f00812
		     chk_ptr != ptr_to_last_entry_in_state; ++chk_ptr)
Packit f00812
			if (*(state_ptr++) != 0 && *chk_ptr != 0)
Packit f00812
				break;
Packit f00812
Packit f00812
		if (chk_ptr == ptr_to_last_entry_in_state)
Packit f00812
			return i;
Packit f00812
Packit f00812
		else
Packit f00812
			++i;
Packit f00812
	}
Packit f00812
}
Packit f00812
Packit f00812
Packit f00812
/* inittbl - initialize transition tables
Packit f00812
 *
Packit f00812
 * Initializes "firstfree" to be one beyond the end of the table.  Initializes
Packit f00812
 * all "chk" entries to be zero.
Packit f00812
 */
Packit f00812
void    inittbl (void)
Packit f00812
{
Packit f00812
	int i;
Packit f00812
Packit f00812
	memset(chk, 0, current_max_xpairs * sizeof(int));
Packit f00812
Packit f00812
	tblend = 0;
Packit f00812
	firstfree = tblend + 1;
Packit f00812
	numtemps = 0;
Packit f00812
Packit f00812
	if (usemecs) {
Packit f00812
		/* Set up doubly-linked meta-equivalence classes; these
Packit f00812
		 * are sets of equivalence classes which all have identical
Packit f00812
		 * transitions out of TEMPLATES.
Packit f00812
		 */
Packit f00812
Packit f00812
		tecbck[1] = NIL;
Packit f00812
Packit f00812
		for (i = 2; i <= numecs; ++i) {
Packit f00812
			tecbck[i] = i - 1;
Packit f00812
			tecfwd[i - 1] = i;
Packit f00812
		}
Packit f00812
Packit f00812
		tecfwd[numecs] = NIL;
Packit f00812
	}
Packit f00812
}
Packit f00812
Packit f00812
Packit f00812
/* mkdeftbl - make the default, "jam" table entries */
Packit f00812
Packit f00812
void    mkdeftbl (void)
Packit f00812
{
Packit f00812
	int     i;
Packit f00812
Packit f00812
	jamstate = lastdfa + 1;
Packit f00812
Packit f00812
	++tblend;		/* room for transition on end-of-buffer character */
Packit f00812
Packit f00812
	while (tblend + numecs >= current_max_xpairs)
Packit f00812
		expand_nxt_chk ();
Packit f00812
Packit f00812
	/* Add in default end-of-buffer transition. */
Packit f00812
	nxt[tblend] = end_of_buffer_state;
Packit f00812
	chk[tblend] = jamstate;
Packit f00812
Packit f00812
	for (i = 1; i <= numecs; ++i) {
Packit f00812
		nxt[tblend + i] = 0;
Packit f00812
		chk[tblend + i] = jamstate;
Packit f00812
	}
Packit f00812
Packit f00812
	jambase = tblend;
Packit f00812
Packit f00812
	base[jamstate] = jambase;
Packit f00812
	def[jamstate] = 0;
Packit f00812
Packit f00812
	tblend += numecs;
Packit f00812
	++numtemps;
Packit f00812
}
Packit f00812
Packit f00812
Packit f00812
/* mkentry - create base/def and nxt/chk entries for transition array
Packit f00812
 *
Packit f00812
 * synopsis
Packit f00812
 *   int state[numchars + 1], numchars, statenum, deflink, totaltrans;
Packit f00812
 *   mkentry( state, numchars, statenum, deflink, totaltrans );
Packit f00812
 *
Packit f00812
 * "state" is a transition array "numchars" characters in size, "statenum"
Packit f00812
 * is the offset to be used into the base/def tables, and "deflink" is the
Packit f00812
 * entry to put in the "def" table entry.  If "deflink" is equal to
Packit f00812
 * "JAMSTATE", then no attempt will be made to fit zero entries of "state"
Packit f00812
 * (i.e., jam entries) into the table.  It is assumed that by linking to
Packit f00812
 * "JAMSTATE" they will be taken care of.  In any case, entries in "state"
Packit f00812
 * marking transitions to "SAME_TRANS" are treated as though they will be
Packit f00812
 * taken care of by whereever "deflink" points.  "totaltrans" is the total
Packit f00812
 * number of transitions out of the state.  If it is below a certain threshold,
Packit f00812
 * the tables are searched for an interior spot that will accommodate the
Packit f00812
 * state array.
Packit f00812
 */
Packit f00812
Packit f00812
void    mkentry (int *state, int numchars, int statenum, int deflink,
Packit f00812
		 int totaltrans)
Packit f00812
{
Packit f00812
	int minec, maxec, i, baseaddr;
Packit f00812
	int tblbase, tbllast;
Packit f00812
Packit f00812
	if (totaltrans == 0) {	/* there are no out-transitions */
Packit f00812
		if (deflink == JAMSTATE)
Packit f00812
			base[statenum] = JAMSTATE;
Packit f00812
		else
Packit f00812
			base[statenum] = 0;
Packit f00812
Packit f00812
		def[statenum] = deflink;
Packit f00812
		return;
Packit f00812
	}
Packit f00812
Packit f00812
	for (minec = 1; minec <= numchars; ++minec) {
Packit f00812
		if (state[minec] != SAME_TRANS)
Packit f00812
			if (state[minec] != 0 || deflink != JAMSTATE)
Packit f00812
				break;
Packit f00812
	}
Packit f00812
Packit f00812
	if (totaltrans == 1) {
Packit f00812
		/* There's only one out-transition.  Save it for later to fill
Packit f00812
		 * in holes in the tables.
Packit f00812
		 */
Packit f00812
		stack1 (statenum, minec, state[minec], deflink);
Packit f00812
		return;
Packit f00812
	}
Packit f00812
Packit f00812
	for (maxec = numchars; maxec > 0; --maxec) {
Packit f00812
		if (state[maxec] != SAME_TRANS)
Packit f00812
			if (state[maxec] != 0 || deflink != JAMSTATE)
Packit f00812
				break;
Packit f00812
	}
Packit f00812
Packit f00812
	/* Whether we try to fit the state table in the middle of the table
Packit f00812
	 * entries we have already generated, or if we just take the state
Packit f00812
	 * table at the end of the nxt/chk tables, we must make sure that we
Packit f00812
	 * have a valid base address (i.e., non-negative).  Note that
Packit f00812
	 * negative base addresses dangerous at run-time (because indexing
Packit f00812
	 * the nxt array with one and a low-valued character will access
Packit f00812
	 * memory before the start of the array.
Packit f00812
	 */
Packit f00812
Packit f00812
	/* Find the first transition of state that we need to worry about. */
Packit f00812
	if (totaltrans * 100 <= numchars * INTERIOR_FIT_PERCENTAGE) {
Packit f00812
		/* Attempt to squeeze it into the middle of the tables. */
Packit f00812
		baseaddr = firstfree;
Packit f00812
Packit f00812
		while (baseaddr < minec) {
Packit f00812
			/* Using baseaddr would result in a negative base
Packit f00812
			 * address below; find the next free slot.
Packit f00812
			 */
Packit f00812
			for (++baseaddr; chk[baseaddr] != 0; ++baseaddr) ;
Packit f00812
		}
Packit f00812
Packit f00812
		while (baseaddr + maxec - minec + 1 >= current_max_xpairs)
Packit f00812
			expand_nxt_chk ();
Packit f00812
Packit f00812
		for (i = minec; i <= maxec; ++i)
Packit f00812
			if (state[i] != SAME_TRANS &&
Packit f00812
			    (state[i] != 0 || deflink != JAMSTATE) &&
Packit f00812
			    chk[baseaddr + i - minec] != 0) {	/* baseaddr unsuitable - find another */
Packit f00812
				for (++baseaddr;
Packit f00812
				     baseaddr < current_max_xpairs &&
Packit f00812
				     chk[baseaddr] != 0; ++baseaddr) ;
Packit f00812
Packit f00812
				while (baseaddr + maxec - minec + 1 >=
Packit f00812
				       current_max_xpairs)
Packit f00812
						expand_nxt_chk ();
Packit f00812
Packit f00812
				/* Reset the loop counter so we'll start all
Packit f00812
				 * over again next time it's incremented.
Packit f00812
				 */
Packit f00812
Packit f00812
				i = minec - 1;
Packit f00812
			}
Packit f00812
	}
Packit f00812
Packit f00812
	else {
Packit f00812
		/* Ensure that the base address we eventually generate is
Packit f00812
		 * non-negative.
Packit f00812
		 */
Packit f00812
		baseaddr = MAX (tblend + 1, minec);
Packit f00812
	}
Packit f00812
Packit f00812
	tblbase = baseaddr - minec;
Packit f00812
	tbllast = tblbase + maxec;
Packit f00812
Packit f00812
	while (tbllast + 1 >= current_max_xpairs)
Packit f00812
		expand_nxt_chk ();
Packit f00812
Packit f00812
	base[statenum] = tblbase;
Packit f00812
	def[statenum] = deflink;
Packit f00812
Packit f00812
	for (i = minec; i <= maxec; ++i)
Packit f00812
		if (state[i] != SAME_TRANS)
Packit f00812
			if (state[i] != 0 || deflink != JAMSTATE) {
Packit f00812
				nxt[tblbase + i] = state[i];
Packit f00812
				chk[tblbase + i] = statenum;
Packit f00812
			}
Packit f00812
Packit f00812
	if (baseaddr == firstfree)
Packit f00812
		/* Find next free slot in tables. */
Packit f00812
		for (++firstfree; chk[firstfree] != 0; ++firstfree) ;
Packit f00812
Packit f00812
	tblend = MAX (tblend, tbllast);
Packit f00812
}
Packit f00812
Packit f00812
Packit f00812
/* mk1tbl - create table entries for a state (or state fragment) which
Packit f00812
 *            has only one out-transition
Packit f00812
 */
Packit f00812
Packit f00812
void    mk1tbl (int state, int sym, int onenxt, int onedef)
Packit f00812
{
Packit f00812
	if (firstfree < sym)
Packit f00812
		firstfree = sym;
Packit f00812
Packit f00812
	while (chk[firstfree] != 0)
Packit f00812
		if (++firstfree >= current_max_xpairs)
Packit f00812
			expand_nxt_chk ();
Packit f00812
Packit f00812
	base[state] = firstfree - sym;
Packit f00812
	def[state] = onedef;
Packit f00812
	chk[firstfree] = state;
Packit f00812
	nxt[firstfree] = onenxt;
Packit f00812
Packit f00812
	if (firstfree > tblend) {
Packit f00812
		tblend = firstfree++;
Packit f00812
Packit f00812
		if (firstfree >= current_max_xpairs)
Packit f00812
			expand_nxt_chk ();
Packit f00812
	}
Packit f00812
}
Packit f00812
Packit f00812
Packit f00812
/* mkprot - create new proto entry */
Packit f00812
Packit f00812
void    mkprot (int state[], int statenum, int comstate)
Packit f00812
{
Packit f00812
	int     i, slot, tblbase;
Packit f00812
Packit f00812
	if (++numprots >= MSP || numecs * numprots >= PROT_SAVE_SIZE) {
Packit f00812
		/* Gotta make room for the new proto by dropping last entry in
Packit f00812
		 * the queue.
Packit f00812
		 */
Packit f00812
		slot = lastprot;
Packit f00812
		lastprot = protprev[lastprot];
Packit f00812
		protnext[lastprot] = NIL;
Packit f00812
	}
Packit f00812
Packit f00812
	else
Packit f00812
		slot = numprots;
Packit f00812
Packit f00812
	protnext[slot] = firstprot;
Packit f00812
Packit f00812
	if (firstprot != NIL)
Packit f00812
		protprev[firstprot] = slot;
Packit f00812
Packit f00812
	firstprot = slot;
Packit f00812
	prottbl[slot] = statenum;
Packit f00812
	protcomst[slot] = comstate;
Packit f00812
Packit f00812
	/* Copy state into save area so it can be compared with rapidly. */
Packit f00812
	tblbase = numecs * (slot - 1);
Packit f00812
Packit f00812
	for (i = 1; i <= numecs; ++i)
Packit f00812
		protsave[tblbase + i] = state[i];
Packit f00812
}
Packit f00812
Packit f00812
Packit f00812
/* mktemplate - create a template entry based on a state, and connect the state
Packit f00812
 *              to it
Packit f00812
 */
Packit f00812
Packit f00812
void    mktemplate (int state[], int statenum, int comstate)
Packit f00812
{
Packit f00812
	int     i, numdiff, tmpbase, tmp[CSIZE + 1];
Packit f00812
	unsigned char    transset[CSIZE + 1];
Packit f00812
	int     tsptr;
Packit f00812
Packit f00812
	++numtemps;
Packit f00812
Packit f00812
	tsptr = 0;
Packit f00812
Packit f00812
	/* Calculate where we will temporarily store the transition table
Packit f00812
	 * of the template in the tnxt[] array.  The final transition table
Packit f00812
	 * gets created by cmptmps().
Packit f00812
	 */
Packit f00812
Packit f00812
	tmpbase = numtemps * numecs;
Packit f00812
Packit f00812
	if (tmpbase + numecs >= current_max_template_xpairs) {
Packit f00812
		current_max_template_xpairs +=
Packit f00812
			MAX_TEMPLATE_XPAIRS_INCREMENT;
Packit f00812
Packit f00812
		++num_reallocs;
Packit f00812
Packit f00812
		tnxt = reallocate_integer_array (tnxt,
Packit f00812
						 current_max_template_xpairs);
Packit f00812
	}
Packit f00812
Packit f00812
	for (i = 1; i <= numecs; ++i)
Packit f00812
		if (state[i] == 0)
Packit f00812
			tnxt[tmpbase + i] = 0;
Packit f00812
		else {
Packit f00812
			transset[tsptr++] = i;
Packit f00812
			tnxt[tmpbase + i] = comstate;
Packit f00812
		}
Packit f00812
Packit f00812
	if (usemecs)
Packit f00812
		mkeccl (transset, tsptr, tecfwd, tecbck, numecs, 0);
Packit f00812
Packit f00812
	mkprot (tnxt + tmpbase, -numtemps, comstate);
Packit f00812
Packit f00812
	/* We rely on the fact that mkprot adds things to the beginning
Packit f00812
	 * of the proto queue.
Packit f00812
	 */
Packit f00812
Packit f00812
	numdiff = tbldiff (state, firstprot, tmp);
Packit f00812
	mkentry (tmp, numecs, statenum, -numtemps, numdiff);
Packit f00812
}
Packit f00812
Packit f00812
Packit f00812
/* mv2front - move proto queue element to front of queue */
Packit f00812
Packit f00812
void    mv2front (int qelm)
Packit f00812
{
Packit f00812
	if (firstprot != qelm) {
Packit f00812
		if (qelm == lastprot)
Packit f00812
			lastprot = protprev[lastprot];
Packit f00812
Packit f00812
		protnext[protprev[qelm]] = protnext[qelm];
Packit f00812
Packit f00812
		if (protnext[qelm] != NIL)
Packit f00812
			protprev[protnext[qelm]] = protprev[qelm];
Packit f00812
Packit f00812
		protprev[qelm] = NIL;
Packit f00812
		protnext[qelm] = firstprot;
Packit f00812
		protprev[firstprot] = qelm;
Packit f00812
		firstprot = qelm;
Packit f00812
	}
Packit f00812
}
Packit f00812
Packit f00812
Packit f00812
/* place_state - place a state into full speed transition table
Packit f00812
 *
Packit f00812
 * State is the statenum'th state.  It is indexed by equivalence class and
Packit f00812
 * gives the number of the state to enter for a given equivalence class.
Packit f00812
 * Transnum is the number of out-transitions for the state.
Packit f00812
 */
Packit f00812
Packit f00812
void    place_state (int *state, int statenum, int transnum)
Packit f00812
{
Packit f00812
	int i;
Packit f00812
	int *state_ptr;
Packit f00812
	int position = find_table_space (state, transnum);
Packit f00812
Packit f00812
	/* "base" is the table of start positions. */
Packit f00812
	base[statenum] = position;
Packit f00812
Packit f00812
	/* Put in action number marker; this non-zero number makes sure that
Packit f00812
	 * find_table_space() knows that this position in chk/nxt is taken
Packit f00812
	 * and should not be used for another accepting number in another
Packit f00812
	 * state.
Packit f00812
	 */
Packit f00812
	chk[position - 1] = 1;
Packit f00812
Packit f00812
	/* Put in end-of-buffer marker; this is for the same purposes as
Packit f00812
	 * above.
Packit f00812
	 */
Packit f00812
	chk[position] = 1;
Packit f00812
Packit f00812
	/* Place the state into chk and nxt. */
Packit f00812
	state_ptr = &state[1];
Packit f00812
Packit f00812
	for (i = 1; i <= numecs; ++i, ++state_ptr)
Packit f00812
		if (*state_ptr != 0) {
Packit f00812
			chk[position + i] = i;
Packit f00812
			nxt[position + i] = *state_ptr;
Packit f00812
		}
Packit f00812
Packit f00812
	if (position + numecs > tblend)
Packit f00812
		tblend = position + numecs;
Packit f00812
}
Packit f00812
Packit f00812
Packit f00812
/* stack1 - save states with only one out-transition to be processed later
Packit f00812
 *
Packit f00812
 * If there's room for another state on the "one-transition" stack, the
Packit f00812
 * state is pushed onto it, to be processed later by mk1tbl.  If there's
Packit f00812
 * no room, we process the sucker right now.
Packit f00812
 */
Packit f00812
Packit f00812
void    stack1 (int statenum, int sym, int nextstate, int deflink)
Packit f00812
{
Packit f00812
	if (onesp >= ONE_STACK_SIZE - 1)
Packit f00812
		mk1tbl (statenum, sym, nextstate, deflink);
Packit f00812
Packit f00812
	else {
Packit f00812
		++onesp;
Packit f00812
		onestate[onesp] = statenum;
Packit f00812
		onesym[onesp] = sym;
Packit f00812
		onenext[onesp] = nextstate;
Packit f00812
		onedef[onesp] = deflink;
Packit f00812
	}
Packit f00812
}
Packit f00812
Packit f00812
Packit f00812
/* tbldiff - compute differences between two state tables
Packit f00812
 *
Packit f00812
 * "state" is the state array which is to be extracted from the pr'th
Packit f00812
 * proto.  "pr" is both the number of the proto we are extracting from
Packit f00812
 * and an index into the save area where we can find the proto's complete
Packit f00812
 * state table.  Each entry in "state" which differs from the corresponding
Packit f00812
 * entry of "pr" will appear in "ext".
Packit f00812
 *
Packit f00812
 * Entries which are the same in both "state" and "pr" will be marked
Packit f00812
 * as transitions to "SAME_TRANS" in "ext".  The total number of differences
Packit f00812
 * between "state" and "pr" is returned as function value.  Note that this
Packit f00812
 * number is "numecs" minus the number of "SAME_TRANS" entries in "ext".
Packit f00812
 */
Packit f00812
Packit f00812
int     tbldiff (int state[], int pr, int ext[])
Packit f00812
{
Packit f00812
	int i, *sp = state, *ep = ext, *protp;
Packit f00812
	int numdiff = 0;
Packit f00812
Packit f00812
	protp = &protsave[numecs * (pr - 1)];
Packit f00812
Packit f00812
	for (i = numecs; i > 0; --i) {
Packit f00812
		if (*++protp == *++sp)
Packit f00812
			*++ep = SAME_TRANS;
Packit f00812
		else {
Packit f00812
			*++ep = *sp;
Packit f00812
			++numdiff;
Packit f00812
		}
Packit f00812
	}
Packit f00812
Packit f00812
	return numdiff;
Packit f00812
}