Expand PMF_FN_* macros.
[netbsd-mini2440.git] / external / bsd / flex / dist / tblcmp.c
blobef11c51063ec1b3f5e19b5a9c30ddeb386ad55a7
1 /* $NetBSD$ */
3 /* tblcmp - table compression routines */
5 /* Copyright (c) 1990 The Regents of the University of California. */
6 /* All rights reserved. */
8 /* This code is derived from software contributed to Berkeley by */
9 /* Vern Paxson. */
11 /* The United States Government has rights in this work pursuant */
12 /* to contract no. DE-AC03-76SF00098 between the United States */
13 /* Department of Energy and the University of California. */
15 /* This file is part of flex. */
17 /* Redistribution and use in source and binary forms, with or without */
18 /* modification, are permitted provided that the following conditions */
19 /* are met: */
21 /* 1. Redistributions of source code must retain the above copyright */
22 /* notice, this list of conditions and the following disclaimer. */
23 /* 2. Redistributions in binary form must reproduce the above copyright */
24 /* notice, this list of conditions and the following disclaimer in the */
25 /* documentation and/or other materials provided with the distribution. */
27 /* Neither the name of the University nor the names of its contributors */
28 /* may be used to endorse or promote products derived from this software */
29 /* without specific prior written permission. */
31 /* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR */
32 /* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED */
33 /* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR */
34 /* PURPOSE. */
36 #include "flexdef.h"
39 /* declarations for functions that have forward references */
41 void mkentry PROTO ((register int *, int, int, int, int));
42 void mkprot PROTO ((int[], int, int));
43 void mktemplate PROTO ((int[], int, int));
44 void mv2front PROTO ((int));
45 int tbldiff PROTO ((int[], int, int[]));
48 /* bldtbl - build table entries for dfa state
50 * synopsis
51 * int state[numecs], statenum, totaltrans, comstate, comfreq;
52 * bldtbl( state, statenum, totaltrans, comstate, comfreq );
54 * State is the statenum'th dfa state. It is indexed by equivalence class and
55 * gives the number of the state to enter for a given equivalence class.
56 * totaltrans is the total number of transitions out of the state. Comstate
57 * is that state which is the destination of the most transitions out of State.
58 * Comfreq is how many transitions there are out of State to Comstate.
60 * A note on terminology:
61 * "protos" are transition tables which have a high probability of
62 * either being redundant (a state processed later will have an identical
63 * transition table) or nearly redundant (a state processed later will have
64 * many of the same out-transitions). A "most recently used" queue of
65 * protos is kept around with the hope that most states will find a proto
66 * which is similar enough to be usable, and therefore compacting the
67 * output tables.
68 * "templates" are a special type of proto. If a transition table is
69 * homogeneous or nearly homogeneous (all transitions go to the same
70 * destination) then the odds are good that future states will also go
71 * to the same destination state on basically the same character set.
72 * These homogeneous states are so common when dealing with large rule
73 * sets that they merit special attention. If the transition table were
74 * simply made into a proto, then (typically) each subsequent, similar
75 * state will differ from the proto for two out-transitions. One of these
76 * out-transitions will be that character on which the proto does not go
77 * to the common destination, and one will be that character on which the
78 * state does not go to the common destination. Templates, on the other
79 * hand, go to the common state on EVERY transition character, and therefore
80 * cost only one difference.
83 void bldtbl (state, statenum, totaltrans, comstate, comfreq)
84 int state[], statenum, totaltrans, comstate, comfreq;
86 int extptr, extrct[2][CSIZE + 1];
87 int mindiff, minprot, i, d;
89 /* If extptr is 0 then the first array of extrct holds the result
90 * of the "best difference" to date, which is those transitions
91 * which occur in "state" but not in the proto which, to date,
92 * has the fewest differences between itself and "state". If
93 * extptr is 1 then the second array of extrct hold the best
94 * difference. The two arrays are toggled between so that the
95 * best difference to date can be kept around and also a difference
96 * just created by checking against a candidate "best" proto.
99 extptr = 0;
101 /* If the state has too few out-transitions, don't bother trying to
102 * compact its tables.
105 if ((totaltrans * 100) < (numecs * PROTO_SIZE_PERCENTAGE))
106 mkentry (state, numecs, statenum, JAMSTATE, totaltrans);
108 else {
109 /* "checkcom" is true if we should only check "state" against
110 * protos which have the same "comstate" value.
112 int checkcom =
114 comfreq * 100 > totaltrans * CHECK_COM_PERCENTAGE;
116 minprot = firstprot;
117 mindiff = totaltrans;
119 if (checkcom) {
120 /* Find first proto which has the same "comstate". */
121 for (i = firstprot; i != NIL; i = protnext[i])
122 if (protcomst[i] == comstate) {
123 minprot = i;
124 mindiff = tbldiff (state, minprot,
125 extrct[extptr]);
126 break;
130 else {
131 /* Since we've decided that the most common destination
132 * out of "state" does not occur with a high enough
133 * frequency, we set the "comstate" to zero, assuring
134 * that if this state is entered into the proto list,
135 * it will not be considered a template.
137 comstate = 0;
139 if (firstprot != NIL) {
140 minprot = firstprot;
141 mindiff = tbldiff (state, minprot,
142 extrct[extptr]);
146 /* We now have the first interesting proto in "minprot". If
147 * it matches within the tolerances set for the first proto,
148 * we don't want to bother scanning the rest of the proto list
149 * to see if we have any other reasonable matches.
152 if (mindiff * 100 >
153 totaltrans * FIRST_MATCH_DIFF_PERCENTAGE) {
154 /* Not a good enough match. Scan the rest of the
155 * protos.
157 for (i = minprot; i != NIL; i = protnext[i]) {
158 d = tbldiff (state, i, extrct[1 - extptr]);
159 if (d < mindiff) {
160 extptr = 1 - extptr;
161 mindiff = d;
162 minprot = i;
167 /* Check if the proto we've decided on as our best bet is close
168 * enough to the state we want to match to be usable.
171 if (mindiff * 100 >
172 totaltrans * ACCEPTABLE_DIFF_PERCENTAGE) {
173 /* No good. If the state is homogeneous enough,
174 * we make a template out of it. Otherwise, we
175 * make a proto.
178 if (comfreq * 100 >=
179 totaltrans * TEMPLATE_SAME_PERCENTAGE)
180 mktemplate (state, statenum,
181 comstate);
183 else {
184 mkprot (state, statenum, comstate);
185 mkentry (state, numecs, statenum,
186 JAMSTATE, totaltrans);
190 else { /* use the proto */
191 mkentry (extrct[extptr], numecs, statenum,
192 prottbl[minprot], mindiff);
194 /* If this state was sufficiently different from the
195 * proto we built it from, make it, too, a proto.
198 if (mindiff * 100 >=
199 totaltrans * NEW_PROTO_DIFF_PERCENTAGE)
200 mkprot (state, statenum, comstate);
202 /* Since mkprot added a new proto to the proto queue,
203 * it's possible that "minprot" is no longer on the
204 * proto queue (if it happened to have been the last
205 * entry, it would have been bumped off). If it's
206 * not there, then the new proto took its physical
207 * place (though logically the new proto is at the
208 * beginning of the queue), so in that case the
209 * following call will do nothing.
212 mv2front (minprot);
218 /* cmptmps - compress template table entries
220 * Template tables are compressed by using the 'template equivalence
221 * classes', which are collections of transition character equivalence
222 * classes which always appear together in templates - really meta-equivalence
223 * classes.
226 void cmptmps ()
228 int tmpstorage[CSIZE + 1];
229 register int *tmp = tmpstorage, i, j;
230 int totaltrans, trans;
232 peakpairs = numtemps * numecs + tblend;
234 if (usemecs) {
235 /* Create equivalence classes based on data gathered on
236 * template transitions.
238 nummecs = cre8ecs (tecfwd, tecbck, numecs);
241 else
242 nummecs = numecs;
244 while (lastdfa + numtemps + 1 >= current_max_dfas)
245 increase_max_dfas ();
247 /* Loop through each template. */
249 for (i = 1; i <= numtemps; ++i) {
250 /* Number of non-jam transitions out of this template. */
251 totaltrans = 0;
253 for (j = 1; j <= numecs; ++j) {
254 trans = tnxt[numecs * i + j];
256 if (usemecs) {
257 /* The absolute value of tecbck is the
258 * meta-equivalence class of a given
259 * equivalence class, as set up by cre8ecs().
261 if (tecbck[j] > 0) {
262 tmp[tecbck[j]] = trans;
264 if (trans > 0)
265 ++totaltrans;
269 else {
270 tmp[j] = trans;
272 if (trans > 0)
273 ++totaltrans;
277 /* It is assumed (in a rather subtle way) in the skeleton
278 * that if we're using meta-equivalence classes, the def[]
279 * entry for all templates is the jam template, i.e.,
280 * templates never default to other non-jam table entries
281 * (e.g., another template)
284 /* Leave room for the jam-state after the last real state. */
285 mkentry (tmp, nummecs, lastdfa + i + 1, JAMSTATE,
286 totaltrans);
292 /* expand_nxt_chk - expand the next check arrays */
294 void expand_nxt_chk ()
296 register int old_max = current_max_xpairs;
298 current_max_xpairs += MAX_XPAIRS_INCREMENT;
300 ++num_reallocs;
302 nxt = reallocate_integer_array (nxt, current_max_xpairs);
303 chk = reallocate_integer_array (chk, current_max_xpairs);
305 zero_out ((char *) (chk + old_max),
306 (size_t) (MAX_XPAIRS_INCREMENT * sizeof (int)));
310 /* find_table_space - finds a space in the table for a state to be placed
312 * synopsis
313 * int *state, numtrans, block_start;
314 * int find_table_space();
316 * block_start = find_table_space( state, numtrans );
318 * State is the state to be added to the full speed transition table.
319 * Numtrans is the number of out-transitions for the state.
321 * find_table_space() returns the position of the start of the first block (in
322 * chk) able to accommodate the state
324 * In determining if a state will or will not fit, find_table_space() must take
325 * into account the fact that an end-of-buffer state will be added at [0],
326 * and an action number will be added in [-1].
329 int find_table_space (state, numtrans)
330 int *state, numtrans;
332 /* Firstfree is the position of the first possible occurrence of two
333 * consecutive unused records in the chk and nxt arrays.
335 register int i;
336 register int *state_ptr, *chk_ptr;
337 register int *ptr_to_last_entry_in_state;
339 /* If there are too many out-transitions, put the state at the end of
340 * nxt and chk.
342 if (numtrans > MAX_XTIONS_FULL_INTERIOR_FIT) {
343 /* If table is empty, return the first available spot in
344 * chk/nxt, which should be 1.
346 if (tblend < 2)
347 return 1;
349 /* Start searching for table space near the end of
350 * chk/nxt arrays.
352 i = tblend - numecs;
355 else
356 /* Start searching for table space from the beginning
357 * (skipping only the elements which will definitely not
358 * hold the new state).
360 i = firstfree;
362 while (1) { /* loops until a space is found */
363 while (i + numecs >= current_max_xpairs)
364 expand_nxt_chk ();
366 /* Loops until space for end-of-buffer and action number
367 * are found.
369 while (1) {
370 /* Check for action number space. */
371 if (chk[i - 1] == 0) {
372 /* Check for end-of-buffer space. */
373 if (chk[i] == 0)
374 break;
376 else
377 /* Since i != 0, there is no use
378 * checking to see if (++i) - 1 == 0,
379 * because that's the same as i == 0,
380 * so we skip a space.
382 i += 2;
385 else
386 ++i;
388 while (i + numecs >= current_max_xpairs)
389 expand_nxt_chk ();
392 /* If we started search from the beginning, store the new
393 * firstfree for the next call of find_table_space().
395 if (numtrans <= MAX_XTIONS_FULL_INTERIOR_FIT)
396 firstfree = i + 1;
398 /* Check to see if all elements in chk (and therefore nxt)
399 * that are needed for the new state have not yet been taken.
402 state_ptr = &state[1];
403 ptr_to_last_entry_in_state = &chk[i + numecs + 1];
405 for (chk_ptr = &chk[i + 1];
406 chk_ptr != ptr_to_last_entry_in_state; ++chk_ptr)
407 if (*(state_ptr++) != 0 && *chk_ptr != 0)
408 break;
410 if (chk_ptr == ptr_to_last_entry_in_state)
411 return i;
413 else
414 ++i;
419 /* inittbl - initialize transition tables
421 * Initializes "firstfree" to be one beyond the end of the table. Initializes
422 * all "chk" entries to be zero.
424 void inittbl ()
426 register int i;
428 zero_out ((char *) chk,
430 (size_t) (current_max_xpairs * sizeof (int)));
432 tblend = 0;
433 firstfree = tblend + 1;
434 numtemps = 0;
436 if (usemecs) {
437 /* Set up doubly-linked meta-equivalence classes; these
438 * are sets of equivalence classes which all have identical
439 * transitions out of TEMPLATES.
442 tecbck[1] = NIL;
444 for (i = 2; i <= numecs; ++i) {
445 tecbck[i] = i - 1;
446 tecfwd[i - 1] = i;
449 tecfwd[numecs] = NIL;
454 /* mkdeftbl - make the default, "jam" table entries */
456 void mkdeftbl ()
458 int i;
460 jamstate = lastdfa + 1;
462 ++tblend; /* room for transition on end-of-buffer character */
464 while (tblend + numecs >= current_max_xpairs)
465 expand_nxt_chk ();
467 /* Add in default end-of-buffer transition. */
468 nxt[tblend] = end_of_buffer_state;
469 chk[tblend] = jamstate;
471 for (i = 1; i <= numecs; ++i) {
472 nxt[tblend + i] = 0;
473 chk[tblend + i] = jamstate;
476 jambase = tblend;
478 base[jamstate] = jambase;
479 def[jamstate] = 0;
481 tblend += numecs;
482 ++numtemps;
486 /* mkentry - create base/def and nxt/chk entries for transition array
488 * synopsis
489 * int state[numchars + 1], numchars, statenum, deflink, totaltrans;
490 * mkentry( state, numchars, statenum, deflink, totaltrans );
492 * "state" is a transition array "numchars" characters in size, "statenum"
493 * is the offset to be used into the base/def tables, and "deflink" is the
494 * entry to put in the "def" table entry. If "deflink" is equal to
495 * "JAMSTATE", then no attempt will be made to fit zero entries of "state"
496 * (i.e., jam entries) into the table. It is assumed that by linking to
497 * "JAMSTATE" they will be taken care of. In any case, entries in "state"
498 * marking transitions to "SAME_TRANS" are treated as though they will be
499 * taken care of by whereever "deflink" points. "totaltrans" is the total
500 * number of transitions out of the state. If it is below a certain threshold,
501 * the tables are searched for an interior spot that will accommodate the
502 * state array.
505 void mkentry (state, numchars, statenum, deflink, totaltrans)
506 register int *state;
507 int numchars, statenum, deflink, totaltrans;
509 register int minec, maxec, i, baseaddr;
510 int tblbase, tbllast;
512 if (totaltrans == 0) { /* there are no out-transitions */
513 if (deflink == JAMSTATE)
514 base[statenum] = JAMSTATE;
515 else
516 base[statenum] = 0;
518 def[statenum] = deflink;
519 return;
522 for (minec = 1; minec <= numchars; ++minec) {
523 if (state[minec] != SAME_TRANS)
524 if (state[minec] != 0 || deflink != JAMSTATE)
525 break;
528 if (totaltrans == 1) {
529 /* There's only one out-transition. Save it for later to fill
530 * in holes in the tables.
532 stack1 (statenum, minec, state[minec], deflink);
533 return;
536 for (maxec = numchars; maxec > 0; --maxec) {
537 if (state[maxec] != SAME_TRANS)
538 if (state[maxec] != 0 || deflink != JAMSTATE)
539 break;
542 /* Whether we try to fit the state table in the middle of the table
543 * entries we have already generated, or if we just take the state
544 * table at the end of the nxt/chk tables, we must make sure that we
545 * have a valid base address (i.e., non-negative). Note that
546 * negative base addresses dangerous at run-time (because indexing
547 * the nxt array with one and a low-valued character will access
548 * memory before the start of the array.
551 /* Find the first transition of state that we need to worry about. */
552 if (totaltrans * 100 <= numchars * INTERIOR_FIT_PERCENTAGE) {
553 /* Attempt to squeeze it into the middle of the tables. */
554 baseaddr = firstfree;
556 while (baseaddr < minec) {
557 /* Using baseaddr would result in a negative base
558 * address below; find the next free slot.
560 for (++baseaddr; chk[baseaddr] != 0; ++baseaddr) ;
563 while (baseaddr + maxec - minec + 1 >= current_max_xpairs)
564 expand_nxt_chk ();
566 for (i = minec; i <= maxec; ++i)
567 if (state[i] != SAME_TRANS &&
568 (state[i] != 0 || deflink != JAMSTATE) &&
569 chk[baseaddr + i - minec] != 0) { /* baseaddr unsuitable - find another */
570 for (++baseaddr;
571 baseaddr < current_max_xpairs &&
572 chk[baseaddr] != 0; ++baseaddr) ;
574 while (baseaddr + maxec - minec + 1 >=
575 current_max_xpairs)
576 expand_nxt_chk ();
578 /* Reset the loop counter so we'll start all
579 * over again next time it's incremented.
582 i = minec - 1;
586 else {
587 /* Ensure that the base address we eventually generate is
588 * non-negative.
590 baseaddr = MAX (tblend + 1, minec);
593 tblbase = baseaddr - minec;
594 tbllast = tblbase + maxec;
596 while (tbllast + 1 >= current_max_xpairs)
597 expand_nxt_chk ();
599 base[statenum] = tblbase;
600 def[statenum] = deflink;
602 for (i = minec; i <= maxec; ++i)
603 if (state[i] != SAME_TRANS)
604 if (state[i] != 0 || deflink != JAMSTATE) {
605 nxt[tblbase + i] = state[i];
606 chk[tblbase + i] = statenum;
609 if (baseaddr == firstfree)
610 /* Find next free slot in tables. */
611 for (++firstfree; chk[firstfree] != 0; ++firstfree) ;
613 tblend = MAX (tblend, tbllast);
617 /* mk1tbl - create table entries for a state (or state fragment) which
618 * has only one out-transition
621 void mk1tbl (state, sym, onenxt, onedef)
622 int state, sym, onenxt, onedef;
624 if (firstfree < sym)
625 firstfree = sym;
627 while (chk[firstfree] != 0)
628 if (++firstfree >= current_max_xpairs)
629 expand_nxt_chk ();
631 base[state] = firstfree - sym;
632 def[state] = onedef;
633 chk[firstfree] = state;
634 nxt[firstfree] = onenxt;
636 if (firstfree > tblend) {
637 tblend = firstfree++;
639 if (firstfree >= current_max_xpairs)
640 expand_nxt_chk ();
645 /* mkprot - create new proto entry */
647 void mkprot (state, statenum, comstate)
648 int state[], statenum, comstate;
650 int i, slot, tblbase;
652 if (++numprots >= MSP || numecs * numprots >= PROT_SAVE_SIZE) {
653 /* Gotta make room for the new proto by dropping last entry in
654 * the queue.
656 slot = lastprot;
657 lastprot = protprev[lastprot];
658 protnext[lastprot] = NIL;
661 else
662 slot = numprots;
664 protnext[slot] = firstprot;
666 if (firstprot != NIL)
667 protprev[firstprot] = slot;
669 firstprot = slot;
670 prottbl[slot] = statenum;
671 protcomst[slot] = comstate;
673 /* Copy state into save area so it can be compared with rapidly. */
674 tblbase = numecs * (slot - 1);
676 for (i = 1; i <= numecs; ++i)
677 protsave[tblbase + i] = state[i];
681 /* mktemplate - create a template entry based on a state, and connect the state
682 * to it
685 void mktemplate (state, statenum, comstate)
686 int state[], statenum, comstate;
688 int i, numdiff, tmpbase, tmp[CSIZE + 1];
689 Char transset[CSIZE + 1];
690 int tsptr;
692 ++numtemps;
694 tsptr = 0;
696 /* Calculate where we will temporarily store the transition table
697 * of the template in the tnxt[] array. The final transition table
698 * gets created by cmptmps().
701 tmpbase = numtemps * numecs;
703 if (tmpbase + numecs >= current_max_template_xpairs) {
704 current_max_template_xpairs +=
705 MAX_TEMPLATE_XPAIRS_INCREMENT;
707 ++num_reallocs;
709 tnxt = reallocate_integer_array (tnxt,
710 current_max_template_xpairs);
713 for (i = 1; i <= numecs; ++i)
714 if (state[i] == 0)
715 tnxt[tmpbase + i] = 0;
716 else {
717 transset[tsptr++] = i;
718 tnxt[tmpbase + i] = comstate;
721 if (usemecs)
722 mkeccl (transset, tsptr, tecfwd, tecbck, numecs, 0);
724 mkprot (tnxt + tmpbase, -numtemps, comstate);
726 /* We rely on the fact that mkprot adds things to the beginning
727 * of the proto queue.
730 numdiff = tbldiff (state, firstprot, tmp);
731 mkentry (tmp, numecs, statenum, -numtemps, numdiff);
735 /* mv2front - move proto queue element to front of queue */
737 void mv2front (qelm)
738 int qelm;
740 if (firstprot != qelm) {
741 if (qelm == lastprot)
742 lastprot = protprev[lastprot];
744 protnext[protprev[qelm]] = protnext[qelm];
746 if (protnext[qelm] != NIL)
747 protprev[protnext[qelm]] = protprev[qelm];
749 protprev[qelm] = NIL;
750 protnext[qelm] = firstprot;
751 protprev[firstprot] = qelm;
752 firstprot = qelm;
757 /* place_state - place a state into full speed transition table
759 * State is the statenum'th state. It is indexed by equivalence class and
760 * gives the number of the state to enter for a given equivalence class.
761 * Transnum is the number of out-transitions for the state.
764 void place_state (state, statenum, transnum)
765 int *state, statenum, transnum;
767 register int i;
768 register int *state_ptr;
769 int position = find_table_space (state, transnum);
771 /* "base" is the table of start positions. */
772 base[statenum] = position;
774 /* Put in action number marker; this non-zero number makes sure that
775 * find_table_space() knows that this position in chk/nxt is taken
776 * and should not be used for another accepting number in another
777 * state.
779 chk[position - 1] = 1;
781 /* Put in end-of-buffer marker; this is for the same purposes as
782 * above.
784 chk[position] = 1;
786 /* Place the state into chk and nxt. */
787 state_ptr = &state[1];
789 for (i = 1; i <= numecs; ++i, ++state_ptr)
790 if (*state_ptr != 0) {
791 chk[position + i] = i;
792 nxt[position + i] = *state_ptr;
795 if (position + numecs > tblend)
796 tblend = position + numecs;
800 /* stack1 - save states with only one out-transition to be processed later
802 * If there's room for another state on the "one-transition" stack, the
803 * state is pushed onto it, to be processed later by mk1tbl. If there's
804 * no room, we process the sucker right now.
807 void stack1 (statenum, sym, nextstate, deflink)
808 int statenum, sym, nextstate, deflink;
810 if (onesp >= ONE_STACK_SIZE - 1)
811 mk1tbl (statenum, sym, nextstate, deflink);
813 else {
814 ++onesp;
815 onestate[onesp] = statenum;
816 onesym[onesp] = sym;
817 onenext[onesp] = nextstate;
818 onedef[onesp] = deflink;
823 /* tbldiff - compute differences between two state tables
825 * "state" is the state array which is to be extracted from the pr'th
826 * proto. "pr" is both the number of the proto we are extracting from
827 * and an index into the save area where we can find the proto's complete
828 * state table. Each entry in "state" which differs from the corresponding
829 * entry of "pr" will appear in "ext".
831 * Entries which are the same in both "state" and "pr" will be marked
832 * as transitions to "SAME_TRANS" in "ext". The total number of differences
833 * between "state" and "pr" is returned as function value. Note that this
834 * number is "numecs" minus the number of "SAME_TRANS" entries in "ext".
837 int tbldiff (state, pr, ext)
838 int state[], pr, ext[];
840 register int i, *sp = state, *ep = ext, *protp;
841 register int numdiff = 0;
843 protp = &protsave[numecs * (pr - 1)];
845 for (i = numecs; i > 0; --i) {
846 if (*++protp == *++sp)
847 *++ep = SAME_TRANS;
848 else {
849 *++ep = *sp;
850 ++numdiff;
854 return numdiff;