1 /* $NetBSD: tblcmp.c,v 1.3 2014/10/30 18:44:05 christos Exp $ */
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 */
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 */
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 */
36 __RCSID("$NetBSD: tblcmp.c,v 1.3 2014/10/30 18:44:05 christos Exp $");
40 /* declarations for functions that have forward references */
42 void mkentry
PROTO ((register int *, int, int, int, int));
43 void mkprot
PROTO ((int[], int, int));
44 void mktemplate
PROTO ((int[], int, int));
45 void mv2front
PROTO ((int));
46 int tbldiff
PROTO ((int[], int, int[]));
49 /* bldtbl - build table entries for dfa state
52 * int state[numecs], statenum, totaltrans, comstate, comfreq;
53 * bldtbl( state, statenum, totaltrans, comstate, comfreq );
55 * State is the statenum'th dfa state. It is indexed by equivalence class and
56 * gives the number of the state to enter for a given equivalence class.
57 * totaltrans is the total number of transitions out of the state. Comstate
58 * is that state which is the destination of the most transitions out of State.
59 * Comfreq is how many transitions there are out of State to Comstate.
61 * A note on terminology:
62 * "protos" are transition tables which have a high probability of
63 * either being redundant (a state processed later will have an identical
64 * transition table) or nearly redundant (a state processed later will have
65 * many of the same out-transitions). A "most recently used" queue of
66 * protos is kept around with the hope that most states will find a proto
67 * which is similar enough to be usable, and therefore compacting the
69 * "templates" are a special type of proto. If a transition table is
70 * homogeneous or nearly homogeneous (all transitions go to the same
71 * destination) then the odds are good that future states will also go
72 * to the same destination state on basically the same character set.
73 * These homogeneous states are so common when dealing with large rule
74 * sets that they merit special attention. If the transition table were
75 * simply made into a proto, then (typically) each subsequent, similar
76 * state will differ from the proto for two out-transitions. One of these
77 * out-transitions will be that character on which the proto does not go
78 * to the common destination, and one will be that character on which the
79 * state does not go to the common destination. Templates, on the other
80 * hand, go to the common state on EVERY transition character, and therefore
81 * cost only one difference.
84 void bldtbl (state
, statenum
, totaltrans
, comstate
, comfreq
)
85 int state
[], statenum
, totaltrans
, comstate
, comfreq
;
87 int extptr
, extrct
[2][CSIZE
+ 1];
88 int mindiff
, minprot
, i
, d
;
90 /* If extptr is 0 then the first array of extrct holds the result
91 * of the "best difference" to date, which is those transitions
92 * which occur in "state" but not in the proto which, to date,
93 * has the fewest differences between itself and "state". If
94 * extptr is 1 then the second array of extrct hold the best
95 * difference. The two arrays are toggled between so that the
96 * best difference to date can be kept around and also a difference
97 * just created by checking against a candidate "best" proto.
102 /* If the state has too few out-transitions, don't bother trying to
103 * compact its tables.
106 if ((totaltrans
* 100) < (numecs
* PROTO_SIZE_PERCENTAGE
))
107 mkentry (state
, numecs
, statenum
, JAMSTATE
, totaltrans
);
110 /* "checkcom" is true if we should only check "state" against
111 * protos which have the same "comstate" value.
115 comfreq
* 100 > totaltrans
* CHECK_COM_PERCENTAGE
;
118 mindiff
= totaltrans
;
121 /* Find first proto which has the same "comstate". */
122 for (i
= firstprot
; i
!= NIL
; i
= protnext
[i
])
123 if (protcomst
[i
] == comstate
) {
125 mindiff
= tbldiff (state
, minprot
,
132 /* Since we've decided that the most common destination
133 * out of "state" does not occur with a high enough
134 * frequency, we set the "comstate" to zero, assuring
135 * that if this state is entered into the proto list,
136 * it will not be considered a template.
140 if (firstprot
!= NIL
) {
142 mindiff
= tbldiff (state
, minprot
,
147 /* We now have the first interesting proto in "minprot". If
148 * it matches within the tolerances set for the first proto,
149 * we don't want to bother scanning the rest of the proto list
150 * to see if we have any other reasonable matches.
154 totaltrans
* FIRST_MATCH_DIFF_PERCENTAGE
) {
155 /* Not a good enough match. Scan the rest of the
158 for (i
= minprot
; i
!= NIL
; i
= protnext
[i
]) {
159 d
= tbldiff (state
, i
, extrct
[1 - extptr
]);
168 /* Check if the proto we've decided on as our best bet is close
169 * enough to the state we want to match to be usable.
173 totaltrans
* ACCEPTABLE_DIFF_PERCENTAGE
) {
174 /* No good. If the state is homogeneous enough,
175 * we make a template out of it. Otherwise, we
180 totaltrans
* TEMPLATE_SAME_PERCENTAGE
)
181 mktemplate (state
, statenum
,
185 mkprot (state
, statenum
, comstate
);
186 mkentry (state
, numecs
, statenum
,
187 JAMSTATE
, totaltrans
);
191 else { /* use the proto */
192 mkentry (extrct
[extptr
], numecs
, statenum
,
193 prottbl
[minprot
], mindiff
);
195 /* If this state was sufficiently different from the
196 * proto we built it from, make it, too, a proto.
200 totaltrans
* NEW_PROTO_DIFF_PERCENTAGE
)
201 mkprot (state
, statenum
, comstate
);
203 /* Since mkprot added a new proto to the proto queue,
204 * it's possible that "minprot" is no longer on the
205 * proto queue (if it happened to have been the last
206 * entry, it would have been bumped off). If it's
207 * not there, then the new proto took its physical
208 * place (though logically the new proto is at the
209 * beginning of the queue), so in that case the
210 * following call will do nothing.
219 /* cmptmps - compress template table entries
221 * Template tables are compressed by using the 'template equivalence
222 * classes', which are collections of transition character equivalence
223 * classes which always appear together in templates - really meta-equivalence
229 int tmpstorage
[CSIZE
+ 1];
230 register int *tmp
= tmpstorage
, i
, j
;
231 int totaltrans
, trans
;
233 peakpairs
= numtemps
* numecs
+ tblend
;
236 /* Create equivalence classes based on data gathered on
237 * template transitions.
239 nummecs
= cre8ecs (tecfwd
, tecbck
, numecs
);
245 while (lastdfa
+ numtemps
+ 1 >= current_max_dfas
)
246 increase_max_dfas ();
248 /* Loop through each template. */
250 for (i
= 1; i
<= numtemps
; ++i
) {
251 /* Number of non-jam transitions out of this template. */
254 for (j
= 1; j
<= numecs
; ++j
) {
255 trans
= tnxt
[numecs
* i
+ j
];
258 /* The absolute value of tecbck is the
259 * meta-equivalence class of a given
260 * equivalence class, as set up by cre8ecs().
263 tmp
[tecbck
[j
]] = trans
;
278 /* It is assumed (in a rather subtle way) in the skeleton
279 * that if we're using meta-equivalence classes, the def[]
280 * entry for all templates is the jam template, i.e.,
281 * templates never default to other non-jam table entries
282 * (e.g., another template)
285 /* Leave room for the jam-state after the last real state. */
286 mkentry (tmp
, nummecs
, lastdfa
+ i
+ 1, JAMSTATE
,
293 /* expand_nxt_chk - expand the next check arrays */
295 void expand_nxt_chk ()
297 register int old_max
= current_max_xpairs
;
299 current_max_xpairs
+= MAX_XPAIRS_INCREMENT
;
303 nxt
= reallocate_integer_array (nxt
, current_max_xpairs
);
304 chk
= reallocate_integer_array (chk
, current_max_xpairs
);
306 zero_out ((char *) (chk
+ old_max
),
307 (size_t) (MAX_XPAIRS_INCREMENT
* sizeof (int)));
311 /* find_table_space - finds a space in the table for a state to be placed
314 * int *state, numtrans, block_start;
315 * int find_table_space();
317 * block_start = find_table_space( state, numtrans );
319 * State is the state to be added to the full speed transition table.
320 * Numtrans is the number of out-transitions for the state.
322 * find_table_space() returns the position of the start of the first block (in
323 * chk) able to accommodate the state
325 * In determining if a state will or will not fit, find_table_space() must take
326 * into account the fact that an end-of-buffer state will be added at [0],
327 * and an action number will be added in [-1].
330 int find_table_space (state
, numtrans
)
331 int *state
, numtrans
;
333 /* Firstfree is the position of the first possible occurrence of two
334 * consecutive unused records in the chk and nxt arrays.
337 register int *state_ptr
, *chk_ptr
;
338 register int *ptr_to_last_entry_in_state
;
340 /* If there are too many out-transitions, put the state at the end of
343 if (numtrans
> MAX_XTIONS_FULL_INTERIOR_FIT
) {
344 /* If table is empty, return the first available spot in
345 * chk/nxt, which should be 1.
350 /* Start searching for table space near the end of
357 /* Start searching for table space from the beginning
358 * (skipping only the elements which will definitely not
359 * hold the new state).
363 while (1) { /* loops until a space is found */
364 while (i
+ numecs
>= current_max_xpairs
)
367 /* Loops until space for end-of-buffer and action number
371 /* Check for action number space. */
372 if (chk
[i
- 1] == 0) {
373 /* Check for end-of-buffer space. */
378 /* Since i != 0, there is no use
379 * checking to see if (++i) - 1 == 0,
380 * because that's the same as i == 0,
381 * so we skip a space.
389 while (i
+ numecs
>= current_max_xpairs
)
393 /* If we started search from the beginning, store the new
394 * firstfree for the next call of find_table_space().
396 if (numtrans
<= MAX_XTIONS_FULL_INTERIOR_FIT
)
399 /* Check to see if all elements in chk (and therefore nxt)
400 * that are needed for the new state have not yet been taken.
403 state_ptr
= &state
[1];
404 ptr_to_last_entry_in_state
= &chk
[i
+ numecs
+ 1];
406 for (chk_ptr
= &chk
[i
+ 1];
407 chk_ptr
!= ptr_to_last_entry_in_state
; ++chk_ptr
)
408 if (*(state_ptr
++) != 0 && *chk_ptr
!= 0)
411 if (chk_ptr
== ptr_to_last_entry_in_state
)
420 /* inittbl - initialize transition tables
422 * Initializes "firstfree" to be one beyond the end of the table. Initializes
423 * all "chk" entries to be zero.
429 zero_out ((char *) chk
,
431 (size_t) (current_max_xpairs
* sizeof (int)));
434 firstfree
= tblend
+ 1;
438 /* Set up doubly-linked meta-equivalence classes; these
439 * are sets of equivalence classes which all have identical
440 * transitions out of TEMPLATES.
445 for (i
= 2; i
<= numecs
; ++i
) {
450 tecfwd
[numecs
] = NIL
;
455 /* mkdeftbl - make the default, "jam" table entries */
461 jamstate
= lastdfa
+ 1;
463 ++tblend
; /* room for transition on end-of-buffer character */
465 while (tblend
+ numecs
>= current_max_xpairs
)
468 /* Add in default end-of-buffer transition. */
469 nxt
[tblend
] = end_of_buffer_state
;
470 chk
[tblend
] = jamstate
;
472 for (i
= 1; i
<= numecs
; ++i
) {
474 chk
[tblend
+ i
] = jamstate
;
479 base
[jamstate
] = jambase
;
487 /* mkentry - create base/def and nxt/chk entries for transition array
490 * int state[numchars + 1], numchars, statenum, deflink, totaltrans;
491 * mkentry( state, numchars, statenum, deflink, totaltrans );
493 * "state" is a transition array "numchars" characters in size, "statenum"
494 * is the offset to be used into the base/def tables, and "deflink" is the
495 * entry to put in the "def" table entry. If "deflink" is equal to
496 * "JAMSTATE", then no attempt will be made to fit zero entries of "state"
497 * (i.e., jam entries) into the table. It is assumed that by linking to
498 * "JAMSTATE" they will be taken care of. In any case, entries in "state"
499 * marking transitions to "SAME_TRANS" are treated as though they will be
500 * taken care of by whereever "deflink" points. "totaltrans" is the total
501 * number of transitions out of the state. If it is below a certain threshold,
502 * the tables are searched for an interior spot that will accommodate the
506 void mkentry (state
, numchars
, statenum
, deflink
, totaltrans
)
508 int numchars
, statenum
, deflink
, totaltrans
;
510 register int minec
, maxec
, i
, baseaddr
;
511 int tblbase
, tbllast
;
513 if (totaltrans
== 0) { /* there are no out-transitions */
514 if (deflink
== JAMSTATE
)
515 base
[statenum
] = JAMSTATE
;
519 def
[statenum
] = deflink
;
523 for (minec
= 1; minec
<= numchars
; ++minec
) {
524 if (state
[minec
] != SAME_TRANS
)
525 if (state
[minec
] != 0 || deflink
!= JAMSTATE
)
529 if (totaltrans
== 1) {
530 /* There's only one out-transition. Save it for later to fill
531 * in holes in the tables.
533 stack1 (statenum
, minec
, state
[minec
], deflink
);
537 for (maxec
= numchars
; maxec
> 0; --maxec
) {
538 if (state
[maxec
] != SAME_TRANS
)
539 if (state
[maxec
] != 0 || deflink
!= JAMSTATE
)
543 /* Whether we try to fit the state table in the middle of the table
544 * entries we have already generated, or if we just take the state
545 * table at the end of the nxt/chk tables, we must make sure that we
546 * have a valid base address (i.e., non-negative). Note that
547 * negative base addresses dangerous at run-time (because indexing
548 * the nxt array with one and a low-valued character will access
549 * memory before the start of the array.
552 /* Find the first transition of state that we need to worry about. */
553 if (totaltrans
* 100 <= numchars
* INTERIOR_FIT_PERCENTAGE
) {
554 /* Attempt to squeeze it into the middle of the tables. */
555 baseaddr
= firstfree
;
557 while (baseaddr
< minec
) {
558 /* Using baseaddr would result in a negative base
559 * address below; find the next free slot.
561 for (++baseaddr
; chk
[baseaddr
] != 0; ++baseaddr
) ;
564 while (baseaddr
+ maxec
- minec
+ 1 >= current_max_xpairs
)
567 for (i
= minec
; i
<= maxec
; ++i
)
568 if (state
[i
] != SAME_TRANS
&&
569 (state
[i
] != 0 || deflink
!= JAMSTATE
) &&
570 chk
[baseaddr
+ i
- minec
] != 0) { /* baseaddr unsuitable - find another */
572 baseaddr
< current_max_xpairs
&&
573 chk
[baseaddr
] != 0; ++baseaddr
) ;
575 while (baseaddr
+ maxec
- minec
+ 1 >=
579 /* Reset the loop counter so we'll start all
580 * over again next time it's incremented.
588 /* Ensure that the base address we eventually generate is
591 baseaddr
= MAX (tblend
+ 1, minec
);
594 tblbase
= baseaddr
- minec
;
595 tbllast
= tblbase
+ maxec
;
597 while (tbllast
+ 1 >= current_max_xpairs
)
600 base
[statenum
] = tblbase
;
601 def
[statenum
] = deflink
;
603 for (i
= minec
; i
<= maxec
; ++i
)
604 if (state
[i
] != SAME_TRANS
)
605 if (state
[i
] != 0 || deflink
!= JAMSTATE
) {
606 nxt
[tblbase
+ i
] = state
[i
];
607 chk
[tblbase
+ i
] = statenum
;
610 if (baseaddr
== firstfree
)
611 /* Find next free slot in tables. */
612 for (++firstfree
; chk
[firstfree
] != 0; ++firstfree
) ;
614 tblend
= MAX (tblend
, tbllast
);
618 /* mk1tbl - create table entries for a state (or state fragment) which
619 * has only one out-transition
622 void mk1tbl (state
, sym
, onenxt
, onedef
)
623 int state
, sym
, onenxt
, onedef
;
628 while (chk
[firstfree
] != 0)
629 if (++firstfree
>= current_max_xpairs
)
632 base
[state
] = firstfree
- sym
;
634 chk
[firstfree
] = state
;
635 nxt
[firstfree
] = onenxt
;
637 if (firstfree
> tblend
) {
638 tblend
= firstfree
++;
640 if (firstfree
>= current_max_xpairs
)
646 /* mkprot - create new proto entry */
648 void mkprot (state
, statenum
, comstate
)
649 int state
[], statenum
, comstate
;
651 int i
, slot
, tblbase
;
653 if (++numprots
>= MSP
|| numecs
* numprots
>= PROT_SAVE_SIZE
) {
654 /* Gotta make room for the new proto by dropping last entry in
658 lastprot
= protprev
[lastprot
];
659 protnext
[lastprot
] = NIL
;
665 protnext
[slot
] = firstprot
;
667 if (firstprot
!= NIL
)
668 protprev
[firstprot
] = slot
;
671 prottbl
[slot
] = statenum
;
672 protcomst
[slot
] = comstate
;
674 /* Copy state into save area so it can be compared with rapidly. */
675 tblbase
= numecs
* (slot
- 1);
677 for (i
= 1; i
<= numecs
; ++i
)
678 protsave
[tblbase
+ i
] = state
[i
];
682 /* mktemplate - create a template entry based on a state, and connect the state
686 void mktemplate (state
, statenum
, comstate
)
687 int state
[], statenum
, comstate
;
689 int i
, numdiff
, tmpbase
, tmp
[CSIZE
+ 1];
690 Char transset
[CSIZE
+ 1];
697 /* Calculate where we will temporarily store the transition table
698 * of the template in the tnxt[] array. The final transition table
699 * gets created by cmptmps().
702 tmpbase
= numtemps
* numecs
;
704 if (tmpbase
+ numecs
>= current_max_template_xpairs
) {
705 current_max_template_xpairs
+=
706 MAX_TEMPLATE_XPAIRS_INCREMENT
;
710 tnxt
= reallocate_integer_array (tnxt
,
711 current_max_template_xpairs
);
714 for (i
= 1; i
<= numecs
; ++i
)
716 tnxt
[tmpbase
+ i
] = 0;
718 transset
[tsptr
++] = i
;
719 tnxt
[tmpbase
+ i
] = comstate
;
723 mkeccl (transset
, tsptr
, tecfwd
, tecbck
, numecs
, 0);
725 mkprot (tnxt
+ tmpbase
, -numtemps
, comstate
);
727 /* We rely on the fact that mkprot adds things to the beginning
728 * of the proto queue.
731 numdiff
= tbldiff (state
, firstprot
, tmp
);
732 mkentry (tmp
, numecs
, statenum
, -numtemps
, numdiff
);
736 /* mv2front - move proto queue element to front of queue */
741 if (firstprot
!= qelm
) {
742 if (qelm
== lastprot
)
743 lastprot
= protprev
[lastprot
];
745 protnext
[protprev
[qelm
]] = protnext
[qelm
];
747 if (protnext
[qelm
] != NIL
)
748 protprev
[protnext
[qelm
]] = protprev
[qelm
];
750 protprev
[qelm
] = NIL
;
751 protnext
[qelm
] = firstprot
;
752 protprev
[firstprot
] = qelm
;
758 /* place_state - place a state into full speed transition table
760 * State is the statenum'th state. It is indexed by equivalence class and
761 * gives the number of the state to enter for a given equivalence class.
762 * Transnum is the number of out-transitions for the state.
765 void place_state (state
, statenum
, transnum
)
766 int *state
, statenum
, transnum
;
769 register int *state_ptr
;
770 int position
= find_table_space (state
, transnum
);
772 /* "base" is the table of start positions. */
773 base
[statenum
] = position
;
775 /* Put in action number marker; this non-zero number makes sure that
776 * find_table_space() knows that this position in chk/nxt is taken
777 * and should not be used for another accepting number in another
780 chk
[position
- 1] = 1;
782 /* Put in end-of-buffer marker; this is for the same purposes as
787 /* Place the state into chk and nxt. */
788 state_ptr
= &state
[1];
790 for (i
= 1; i
<= numecs
; ++i
, ++state_ptr
)
791 if (*state_ptr
!= 0) {
792 chk
[position
+ i
] = i
;
793 nxt
[position
+ i
] = *state_ptr
;
796 if (position
+ numecs
> tblend
)
797 tblend
= position
+ numecs
;
801 /* stack1 - save states with only one out-transition to be processed later
803 * If there's room for another state on the "one-transition" stack, the
804 * state is pushed onto it, to be processed later by mk1tbl. If there's
805 * no room, we process the sucker right now.
808 void stack1 (statenum
, sym
, nextstate
, deflink
)
809 int statenum
, sym
, nextstate
, deflink
;
811 if (onesp
>= ONE_STACK_SIZE
- 1)
812 mk1tbl (statenum
, sym
, nextstate
, deflink
);
816 onestate
[onesp
] = statenum
;
818 onenext
[onesp
] = nextstate
;
819 onedef
[onesp
] = deflink
;
824 /* tbldiff - compute differences between two state tables
826 * "state" is the state array which is to be extracted from the pr'th
827 * proto. "pr" is both the number of the proto we are extracting from
828 * and an index into the save area where we can find the proto's complete
829 * state table. Each entry in "state" which differs from the corresponding
830 * entry of "pr" will appear in "ext".
832 * Entries which are the same in both "state" and "pr" will be marked
833 * as transitions to "SAME_TRANS" in "ext". The total number of differences
834 * between "state" and "pr" is returned as function value. Note that this
835 * number is "numecs" minus the number of "SAME_TRANS" entries in "ext".
838 int tbldiff (state
, pr
, ext
)
839 int state
[], pr
, ext
[];
841 register int i
, *sp
= state
, *ep
= ext
, *protp
;
842 register int numdiff
= 0;
844 protp
= &protsave
[numecs
* (pr
- 1)];
846 for (i
= numecs
; i
> 0; --i
) {
847 if (*++protp
== *++sp
)