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