1 /* tblcmp - table compression routines */
3 /* Copyright (c) 1990 The Regents of the University of California. */
4 /* All rights reserved. */
6 /* This code is derived from software contributed to Berkeley by */
9 /* The United States Government has rights in this work pursuant */
10 /* to contract no. DE-AC03-76SF00098 between the United States */
11 /* Department of Energy and the University of California. */
13 /* This file is part of flex. */
15 /* Redistribution and use in source and binary forms, with or without */
16 /* modification, are permitted provided that the following conditions */
19 /* 1. Redistributions of source code must retain the above copyright */
20 /* notice, this list of conditions and the following disclaimer. */
21 /* 2. Redistributions in binary form must reproduce the above copyright */
22 /* notice, this list of conditions and the following disclaimer in the */
23 /* documentation and/or other materials provided with the distribution. */
25 /* Neither the name of the University nor the names of its contributors */
26 /* may be used to endorse or promote products derived from this software */
27 /* without specific prior written permission. */
29 /* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR */
30 /* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED */
31 /* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR */
37 /* declarations for functions that have forward references */
39 void mkentry
PROTO ((register int *, int, int, int, int));
40 void mkprot
PROTO ((int[], int, int));
41 void mktemplate
PROTO ((int[], int, int));
42 void mv2front
PROTO ((int));
43 int tbldiff
PROTO ((int[], int, int[]));
46 /* bldtbl - build table entries for dfa state
49 * int state[numecs], statenum, totaltrans, comstate, comfreq;
50 * bldtbl( state, statenum, totaltrans, comstate, comfreq );
52 * State is the statenum'th dfa state. It is indexed by equivalence class and
53 * gives the number of the state to enter for a given equivalence class.
54 * totaltrans is the total number of transitions out of the state. Comstate
55 * is that state which is the destination of the most transitions out of State.
56 * Comfreq is how many transitions there are out of State to Comstate.
58 * A note on terminology:
59 * "protos" are transition tables which have a high probability of
60 * either being redundant (a state processed later will have an identical
61 * transition table) or nearly redundant (a state processed later will have
62 * many of the same out-transitions). A "most recently used" queue of
63 * protos is kept around with the hope that most states will find a proto
64 * which is similar enough to be usable, and therefore compacting the
66 * "templates" are a special type of proto. If a transition table is
67 * homogeneous or nearly homogeneous (all transitions go to the same
68 * destination) then the odds are good that future states will also go
69 * to the same destination state on basically the same character set.
70 * These homogeneous states are so common when dealing with large rule
71 * sets that they merit special attention. If the transition table were
72 * simply made into a proto, then (typically) each subsequent, similar
73 * state will differ from the proto for two out-transitions. One of these
74 * out-transitions will be that character on which the proto does not go
75 * to the common destination, and one will be that character on which the
76 * state does not go to the common destination. Templates, on the other
77 * hand, go to the common state on EVERY transition character, and therefore
78 * cost only one difference.
81 void bldtbl (state
, statenum
, totaltrans
, comstate
, comfreq
)
82 int state
[], statenum
, totaltrans
, comstate
, comfreq
;
84 int extptr
, extrct
[2][CSIZE
+ 1];
85 int mindiff
, minprot
, i
, d
;
87 /* If extptr is 0 then the first array of extrct holds the result
88 * of the "best difference" to date, which is those transitions
89 * which occur in "state" but not in the proto which, to date,
90 * has the fewest differences between itself and "state". If
91 * extptr is 1 then the second array of extrct hold the best
92 * difference. The two arrays are toggled between so that the
93 * best difference to date can be kept around and also a difference
94 * just created by checking against a candidate "best" proto.
99 /* If the state has too few out-transitions, don't bother trying to
100 * compact its tables.
103 if ((totaltrans
* 100) < (numecs
* PROTO_SIZE_PERCENTAGE
))
104 mkentry (state
, numecs
, statenum
, JAMSTATE
, totaltrans
);
107 /* "checkcom" is true if we should only check "state" against
108 * protos which have the same "comstate" value.
112 comfreq
* 100 > totaltrans
* CHECK_COM_PERCENTAGE
;
115 mindiff
= totaltrans
;
118 /* Find first proto which has the same "comstate". */
119 for (i
= firstprot
; i
!= NIL
; i
= protnext
[i
])
120 if (protcomst
[i
] == comstate
) {
122 mindiff
= tbldiff (state
, minprot
,
129 /* Since we've decided that the most common destination
130 * out of "state" does not occur with a high enough
131 * frequency, we set the "comstate" to zero, assuring
132 * that if this state is entered into the proto list,
133 * it will not be considered a template.
137 if (firstprot
!= NIL
) {
139 mindiff
= tbldiff (state
, minprot
,
144 /* We now have the first interesting proto in "minprot". If
145 * it matches within the tolerances set for the first proto,
146 * we don't want to bother scanning the rest of the proto list
147 * to see if we have any other reasonable matches.
151 totaltrans
* FIRST_MATCH_DIFF_PERCENTAGE
) {
152 /* Not a good enough match. Scan the rest of the
155 for (i
= minprot
; i
!= NIL
; i
= protnext
[i
]) {
156 d
= tbldiff (state
, i
, extrct
[1 - extptr
]);
165 /* Check if the proto we've decided on as our best bet is close
166 * enough to the state we want to match to be usable.
170 totaltrans
* ACCEPTABLE_DIFF_PERCENTAGE
) {
171 /* No good. If the state is homogeneous enough,
172 * we make a template out of it. Otherwise, we
177 totaltrans
* TEMPLATE_SAME_PERCENTAGE
)
178 mktemplate (state
, statenum
,
182 mkprot (state
, statenum
, comstate
);
183 mkentry (state
, numecs
, statenum
,
184 JAMSTATE
, totaltrans
);
188 else { /* use the proto */
189 mkentry (extrct
[extptr
], numecs
, statenum
,
190 prottbl
[minprot
], mindiff
);
192 /* If this state was sufficiently different from the
193 * proto we built it from, make it, too, a proto.
197 totaltrans
* NEW_PROTO_DIFF_PERCENTAGE
)
198 mkprot (state
, statenum
, comstate
);
200 /* Since mkprot added a new proto to the proto queue,
201 * it's possible that "minprot" is no longer on the
202 * proto queue (if it happened to have been the last
203 * entry, it would have been bumped off). If it's
204 * not there, then the new proto took its physical
205 * place (though logically the new proto is at the
206 * beginning of the queue), so in that case the
207 * following call will do nothing.
216 /* cmptmps - compress template table entries
218 * Template tables are compressed by using the 'template equivalence
219 * classes', which are collections of transition character equivalence
220 * classes which always appear together in templates - really meta-equivalence
226 int tmpstorage
[CSIZE
+ 1];
227 register int *tmp
= tmpstorage
, i
, j
;
228 int totaltrans
, trans
;
230 peakpairs
= numtemps
* numecs
+ tblend
;
233 /* Create equivalence classes based on data gathered on
234 * template transitions.
236 nummecs
= cre8ecs (tecfwd
, tecbck
, numecs
);
242 while (lastdfa
+ numtemps
+ 1 >= current_max_dfas
)
243 increase_max_dfas ();
245 /* Loop through each template. */
247 for (i
= 1; i
<= numtemps
; ++i
) {
248 /* Number of non-jam transitions out of this template. */
251 for (j
= 1; j
<= numecs
; ++j
) {
252 trans
= tnxt
[numecs
* i
+ j
];
255 /* The absolute value of tecbck is the
256 * meta-equivalence class of a given
257 * equivalence class, as set up by cre8ecs().
260 tmp
[tecbck
[j
]] = trans
;
275 /* It is assumed (in a rather subtle way) in the skeleton
276 * that if we're using meta-equivalence classes, the def[]
277 * entry for all templates is the jam template, i.e.,
278 * templates never default to other non-jam table entries
279 * (e.g., another template)
282 /* Leave room for the jam-state after the last real state. */
283 mkentry (tmp
, nummecs
, lastdfa
+ i
+ 1, JAMSTATE
,
290 /* expand_nxt_chk - expand the next check arrays */
292 void expand_nxt_chk ()
294 register int old_max
= current_max_xpairs
;
296 current_max_xpairs
+= MAX_XPAIRS_INCREMENT
;
300 nxt
= reallocate_integer_array (nxt
, current_max_xpairs
);
301 chk
= reallocate_integer_array (chk
, current_max_xpairs
);
303 zero_out ((char *) (chk
+ old_max
),
304 (size_t) (MAX_XPAIRS_INCREMENT
* sizeof (int)));
308 /* find_table_space - finds a space in the table for a state to be placed
311 * int *state, numtrans, block_start;
312 * int find_table_space();
314 * block_start = find_table_space( state, numtrans );
316 * State is the state to be added to the full speed transition table.
317 * Numtrans is the number of out-transitions for the state.
319 * find_table_space() returns the position of the start of the first block (in
320 * chk) able to accommodate the state
322 * In determining if a state will or will not fit, find_table_space() must take
323 * into account the fact that an end-of-buffer state will be added at [0],
324 * and an action number will be added in [-1].
327 int find_table_space (state
, numtrans
)
328 int *state
, numtrans
;
330 /* Firstfree is the position of the first possible occurrence of two
331 * consecutive unused records in the chk and nxt arrays.
334 register int *state_ptr
, *chk_ptr
;
335 register int *ptr_to_last_entry_in_state
;
337 /* If there are too many out-transitions, put the state at the end of
340 if (numtrans
> MAX_XTIONS_FULL_INTERIOR_FIT
) {
341 /* If table is empty, return the first available spot in
342 * chk/nxt, which should be 1.
347 /* Start searching for table space near the end of
354 /* Start searching for table space from the beginning
355 * (skipping only the elements which will definitely not
356 * hold the new state).
360 while (1) { /* loops until a space is found */
361 while (i
+ numecs
>= current_max_xpairs
)
364 /* Loops until space for end-of-buffer and action number
368 /* Check for action number space. */
369 if (chk
[i
- 1] == 0) {
370 /* Check for end-of-buffer space. */
375 /* Since i != 0, there is no use
376 * checking to see if (++i) - 1 == 0,
377 * because that's the same as i == 0,
378 * so we skip a space.
386 while (i
+ numecs
>= current_max_xpairs
)
390 /* If we started search from the beginning, store the new
391 * firstfree for the next call of find_table_space().
393 if (numtrans
<= MAX_XTIONS_FULL_INTERIOR_FIT
)
396 /* Check to see if all elements in chk (and therefore nxt)
397 * that are needed for the new state have not yet been taken.
400 state_ptr
= &state
[1];
401 ptr_to_last_entry_in_state
= &chk
[i
+ numecs
+ 1];
403 for (chk_ptr
= &chk
[i
+ 1];
404 chk_ptr
!= ptr_to_last_entry_in_state
; ++chk_ptr
)
405 if (*(state_ptr
++) != 0 && *chk_ptr
!= 0)
408 if (chk_ptr
== ptr_to_last_entry_in_state
)
417 /* inittbl - initialize transition tables
419 * Initializes "firstfree" to be one beyond the end of the table. Initializes
420 * all "chk" entries to be zero.
426 zero_out ((char *) chk
,
428 (size_t) (current_max_xpairs
* sizeof (int)));
431 firstfree
= tblend
+ 1;
435 /* Set up doubly-linked meta-equivalence classes; these
436 * are sets of equivalence classes which all have identical
437 * transitions out of TEMPLATES.
442 for (i
= 2; i
<= numecs
; ++i
) {
447 tecfwd
[numecs
] = NIL
;
452 /* mkdeftbl - make the default, "jam" table entries */
458 jamstate
= lastdfa
+ 1;
460 ++tblend
; /* room for transition on end-of-buffer character */
462 while (tblend
+ numecs
>= current_max_xpairs
)
465 /* Add in default end-of-buffer transition. */
466 nxt
[tblend
] = end_of_buffer_state
;
467 chk
[tblend
] = jamstate
;
469 for (i
= 1; i
<= numecs
; ++i
) {
471 chk
[tblend
+ i
] = jamstate
;
476 base
[jamstate
] = jambase
;
484 /* mkentry - create base/def and nxt/chk entries for transition array
487 * int state[numchars + 1], numchars, statenum, deflink, totaltrans;
488 * mkentry( state, numchars, statenum, deflink, totaltrans );
490 * "state" is a transition array "numchars" characters in size, "statenum"
491 * is the offset to be used into the base/def tables, and "deflink" is the
492 * entry to put in the "def" table entry. If "deflink" is equal to
493 * "JAMSTATE", then no attempt will be made to fit zero entries of "state"
494 * (i.e., jam entries) into the table. It is assumed that by linking to
495 * "JAMSTATE" they will be taken care of. In any case, entries in "state"
496 * marking transitions to "SAME_TRANS" are treated as though they will be
497 * taken care of by whereever "deflink" points. "totaltrans" is the total
498 * number of transitions out of the state. If it is below a certain threshold,
499 * the tables are searched for an interior spot that will accommodate the
503 void mkentry (state
, numchars
, statenum
, deflink
, totaltrans
)
505 int numchars
, statenum
, deflink
, totaltrans
;
507 register int minec
, maxec
, i
, baseaddr
;
508 int tblbase
, tbllast
;
510 if (totaltrans
== 0) { /* there are no out-transitions */
511 if (deflink
== JAMSTATE
)
512 base
[statenum
] = JAMSTATE
;
516 def
[statenum
] = deflink
;
520 for (minec
= 1; minec
<= numchars
; ++minec
) {
521 if (state
[minec
] != SAME_TRANS
)
522 if (state
[minec
] != 0 || deflink
!= JAMSTATE
)
526 if (totaltrans
== 1) {
527 /* There's only one out-transition. Save it for later to fill
528 * in holes in the tables.
530 stack1 (statenum
, minec
, state
[minec
], deflink
);
534 for (maxec
= numchars
; maxec
> 0; --maxec
) {
535 if (state
[maxec
] != SAME_TRANS
)
536 if (state
[maxec
] != 0 || deflink
!= JAMSTATE
)
540 /* Whether we try to fit the state table in the middle of the table
541 * entries we have already generated, or if we just take the state
542 * table at the end of the nxt/chk tables, we must make sure that we
543 * have a valid base address (i.e., non-negative). Note that
544 * negative base addresses dangerous at run-time (because indexing
545 * the nxt array with one and a low-valued character will access
546 * memory before the start of the array.
549 /* Find the first transition of state that we need to worry about. */
550 if (totaltrans
* 100 <= numchars
* INTERIOR_FIT_PERCENTAGE
) {
551 /* Attempt to squeeze it into the middle of the tables. */
552 baseaddr
= firstfree
;
554 while (baseaddr
< minec
) {
555 /* Using baseaddr would result in a negative base
556 * address below; find the next free slot.
558 for (++baseaddr
; chk
[baseaddr
] != 0; ++baseaddr
) ;
561 while (baseaddr
+ maxec
- minec
+ 1 >= current_max_xpairs
)
564 for (i
= minec
; i
<= maxec
; ++i
)
565 if (state
[i
] != SAME_TRANS
&&
566 (state
[i
] != 0 || deflink
!= JAMSTATE
) &&
567 chk
[baseaddr
+ i
- minec
] != 0) { /* baseaddr unsuitable - find another */
569 baseaddr
< current_max_xpairs
&&
570 chk
[baseaddr
] != 0; ++baseaddr
) ;
572 while (baseaddr
+ maxec
- minec
+ 1 >=
576 /* Reset the loop counter so we'll start all
577 * over again next time it's incremented.
585 /* Ensure that the base address we eventually generate is
588 baseaddr
= MAX (tblend
+ 1, minec
);
591 tblbase
= baseaddr
- minec
;
592 tbllast
= tblbase
+ maxec
;
594 while (tbllast
+ 1 >= current_max_xpairs
)
597 base
[statenum
] = tblbase
;
598 def
[statenum
] = deflink
;
600 for (i
= minec
; i
<= maxec
; ++i
)
601 if (state
[i
] != SAME_TRANS
)
602 if (state
[i
] != 0 || deflink
!= JAMSTATE
) {
603 nxt
[tblbase
+ i
] = state
[i
];
604 chk
[tblbase
+ i
] = statenum
;
607 if (baseaddr
== firstfree
)
608 /* Find next free slot in tables. */
609 for (++firstfree
; chk
[firstfree
] != 0; ++firstfree
) ;
611 tblend
= MAX (tblend
, tbllast
);
615 /* mk1tbl - create table entries for a state (or state fragment) which
616 * has only one out-transition
619 void mk1tbl (state
, sym
, onenxt
, onedef
)
620 int state
, sym
, onenxt
, onedef
;
625 while (chk
[firstfree
] != 0)
626 if (++firstfree
>= current_max_xpairs
)
629 base
[state
] = firstfree
- sym
;
631 chk
[firstfree
] = state
;
632 nxt
[firstfree
] = onenxt
;
634 if (firstfree
> tblend
) {
635 tblend
= firstfree
++;
637 if (firstfree
>= current_max_xpairs
)
643 /* mkprot - create new proto entry */
645 void mkprot (state
, statenum
, comstate
)
646 int state
[], statenum
, comstate
;
648 int i
, slot
, tblbase
;
650 if (++numprots
>= MSP
|| numecs
* numprots
>= PROT_SAVE_SIZE
) {
651 /* Gotta make room for the new proto by dropping last entry in
655 lastprot
= protprev
[lastprot
];
656 protnext
[lastprot
] = NIL
;
662 protnext
[slot
] = firstprot
;
664 if (firstprot
!= NIL
)
665 protprev
[firstprot
] = slot
;
668 prottbl
[slot
] = statenum
;
669 protcomst
[slot
] = comstate
;
671 /* Copy state into save area so it can be compared with rapidly. */
672 tblbase
= numecs
* (slot
- 1);
674 for (i
= 1; i
<= numecs
; ++i
)
675 protsave
[tblbase
+ i
] = state
[i
];
679 /* mktemplate - create a template entry based on a state, and connect the state
683 void mktemplate (state
, statenum
, comstate
)
684 int state
[], statenum
, comstate
;
686 int i
, numdiff
, tmpbase
, tmp
[CSIZE
+ 1];
687 Char transset
[CSIZE
+ 1];
694 /* Calculate where we will temporarily store the transition table
695 * of the template in the tnxt[] array. The final transition table
696 * gets created by cmptmps().
699 tmpbase
= numtemps
* numecs
;
701 if (tmpbase
+ numecs
>= current_max_template_xpairs
) {
702 current_max_template_xpairs
+=
703 MAX_TEMPLATE_XPAIRS_INCREMENT
;
707 tnxt
= reallocate_integer_array (tnxt
,
708 current_max_template_xpairs
);
711 for (i
= 1; i
<= numecs
; ++i
)
713 tnxt
[tmpbase
+ i
] = 0;
715 transset
[tsptr
++] = i
;
716 tnxt
[tmpbase
+ i
] = comstate
;
720 mkeccl (transset
, tsptr
, tecfwd
, tecbck
, numecs
, 0);
722 mkprot (tnxt
+ tmpbase
, -numtemps
, comstate
);
724 /* We rely on the fact that mkprot adds things to the beginning
725 * of the proto queue.
728 numdiff
= tbldiff (state
, firstprot
, tmp
);
729 mkentry (tmp
, numecs
, statenum
, -numtemps
, numdiff
);
733 /* mv2front - move proto queue element to front of queue */
738 if (firstprot
!= qelm
) {
739 if (qelm
== lastprot
)
740 lastprot
= protprev
[lastprot
];
742 protnext
[protprev
[qelm
]] = protnext
[qelm
];
744 if (protnext
[qelm
] != NIL
)
745 protprev
[protnext
[qelm
]] = protprev
[qelm
];
747 protprev
[qelm
] = NIL
;
748 protnext
[qelm
] = firstprot
;
749 protprev
[firstprot
] = qelm
;
755 /* place_state - place a state into full speed transition table
757 * State is the statenum'th state. It is indexed by equivalence class and
758 * gives the number of the state to enter for a given equivalence class.
759 * Transnum is the number of out-transitions for the state.
762 void place_state (state
, statenum
, transnum
)
763 int *state
, statenum
, transnum
;
766 register int *state_ptr
;
767 int position
= find_table_space (state
, transnum
);
769 /* "base" is the table of start positions. */
770 base
[statenum
] = position
;
772 /* Put in action number marker; this non-zero number makes sure that
773 * find_table_space() knows that this position in chk/nxt is taken
774 * and should not be used for another accepting number in another
777 chk
[position
- 1] = 1;
779 /* Put in end-of-buffer marker; this is for the same purposes as
784 /* Place the state into chk and nxt. */
785 state_ptr
= &state
[1];
787 for (i
= 1; i
<= numecs
; ++i
, ++state_ptr
)
788 if (*state_ptr
!= 0) {
789 chk
[position
+ i
] = i
;
790 nxt
[position
+ i
] = *state_ptr
;
793 if (position
+ numecs
> tblend
)
794 tblend
= position
+ numecs
;
798 /* stack1 - save states with only one out-transition to be processed later
800 * If there's room for another state on the "one-transition" stack, the
801 * state is pushed onto it, to be processed later by mk1tbl. If there's
802 * no room, we process the sucker right now.
805 void stack1 (statenum
, sym
, nextstate
, deflink
)
806 int statenum
, sym
, nextstate
, deflink
;
808 if (onesp
>= ONE_STACK_SIZE
- 1)
809 mk1tbl (statenum
, sym
, nextstate
, deflink
);
813 onestate
[onesp
] = statenum
;
815 onenext
[onesp
] = nextstate
;
816 onedef
[onesp
] = deflink
;
821 /* tbldiff - compute differences between two state tables
823 * "state" is the state array which is to be extracted from the pr'th
824 * proto. "pr" is both the number of the proto we are extracting from
825 * and an index into the save area where we can find the proto's complete
826 * state table. Each entry in "state" which differs from the corresponding
827 * entry of "pr" will appear in "ext".
829 * Entries which are the same in both "state" and "pr" will be marked
830 * as transitions to "SAME_TRANS" in "ext". The total number of differences
831 * between "state" and "pr" is returned as function value. Note that this
832 * number is "numecs" minus the number of "SAME_TRANS" entries in "ext".
835 int tbldiff (state
, pr
, ext
)
836 int state
[], pr
, ext
[];
838 register int i
, *sp
= state
, *ep
= ext
, *protp
;
839 register int numdiff
= 0;
841 protp
= &protsave
[numecs
* (pr
- 1)];
843 for (i
= numecs
; i
> 0; --i
) {
844 if (*++protp
== *++sp
)