[BACK]Return to tblcmp.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / lex

Annotation of src/usr.bin/lex/tblcmp.c, Revision 1.8

1.8     ! tedu        1: /*     $OpenBSD: tblcmp.c,v 1.7 2015/11/19 19:43:40 tedu 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.8     ! tedu      313:        zero_out((char *) (chk + old_max),
        !           314:            (size_t) (MAX_XPAIRS_INCREMENT * sizeof(int)));
1.7       tedu      315: }
1.1       deraadt   316:
                    317:
                    318: /* find_table_space - finds a space in the table for a state to be placed
                    319:  *
                    320:  * synopsis
                    321:  *     int *state, numtrans, block_start;
                    322:  *     int find_table_space();
                    323:  *
                    324:  *     block_start = find_table_space( state, numtrans );
                    325:  *
                    326:  * State is the state to be added to the full speed transition table.
                    327:  * Numtrans is the number of out-transitions for the state.
                    328:  *
                    329:  * find_table_space() returns the position of the start of the first block (in
                    330:  * chk) able to accommodate the state
                    331:  *
                    332:  * In determining if a state will or will not fit, find_table_space() must take
                    333:  * into account the fact that an end-of-buffer state will be added at [0],
                    334:  * and an action number will be added in [-1].
                    335:  */
                    336:
1.8     ! tedu      337: int
        !           338: find_table_space(state, numtrans)
        !           339:        int *state, numtrans;
1.7       tedu      340: {
1.8     ! tedu      341:        /*
        !           342:         * Firstfree is the position of the first possible occurrence of two
1.1       deraadt   343:         * consecutive unused records in the chk and nxt arrays.
                    344:         */
1.5       mpech     345:        int i;
                    346:        int *state_ptr, *chk_ptr;
                    347:        int *ptr_to_last_entry_in_state;
1.1       deraadt   348:
1.8     ! tedu      349:        /*
        !           350:         * If there are too many out-transitions, put the state at the end of
1.1       deraadt   351:         * nxt and chk.
                    352:         */
1.7       tedu      353:        if (numtrans > MAX_XTIONS_FULL_INTERIOR_FIT) {
1.8     ! tedu      354:                /*
        !           355:                 * If table is empty, return the first available spot in
1.1       deraadt   356:                 * chk/nxt, which should be 1.
                    357:                 */
1.7       tedu      358:                if (tblend < 2)
1.1       deraadt   359:                        return 1;
                    360:
1.8     ! tedu      361:                /*
        !           362:                 * Start searching for table space near the end of chk/nxt
        !           363:                 * arrays.
1.1       deraadt   364:                 */
                    365:                i = tblend - numecs;
1.8     ! tedu      366:        } else
        !           367:                /*
        !           368:                 * Start searching for table space from the beginning
        !           369:                 * (skipping only the elements which will definitely not hold
        !           370:                 * the new state).
1.1       deraadt   371:                 */
                    372:                i = firstfree;
                    373:
1.7       tedu      374:        while (1) {             /* loops until a space is found */
                    375:                while (i + numecs >= current_max_xpairs)
1.8     ! tedu      376:                        expand_nxt_chk();
1.1       deraadt   377:
1.8     ! tedu      378:                /*
        !           379:                 * Loops until space for end-of-buffer and action number are
        !           380:                 * found.
1.1       deraadt   381:                 */
1.7       tedu      382:                while (1) {
1.1       deraadt   383:                        /* Check for action number space. */
1.7       tedu      384:                        if (chk[i - 1] == 0) {
1.1       deraadt   385:                                /* Check for end-of-buffer space. */
1.7       tedu      386:                                if (chk[i] == 0)
1.1       deraadt   387:                                        break;
                    388:
                    389:                                else
1.8     ! tedu      390:                                        /*
        !           391:                                         * Since i != 0, there is no use
1.1       deraadt   392:                                         * checking to see if (++i) - 1 == 0,
                    393:                                         * because that's the same as i == 0,
                    394:                                         * so we skip a space.
                    395:                                         */
                    396:                                        i += 2;
1.8     ! tedu      397:                        } else
1.1       deraadt   398:                                ++i;
                    399:
1.7       tedu      400:                        while (i + numecs >= current_max_xpairs)
1.8     ! tedu      401:                                expand_nxt_chk();
1.7       tedu      402:                }
1.1       deraadt   403:
1.8     ! tedu      404:                /*
        !           405:                 * If we started search from the beginning, store the new
1.1       deraadt   406:                 * firstfree for the next call of find_table_space().
                    407:                 */
1.7       tedu      408:                if (numtrans <= MAX_XTIONS_FULL_INTERIOR_FIT)
1.1       deraadt   409:                        firstfree = i + 1;
                    410:
1.8     ! tedu      411:                /*
        !           412:                 * Check to see if all elements in chk (and therefore nxt)
1.1       deraadt   413:                 * that are needed for the new state have not yet been taken.
                    414:                 */
                    415:
                    416:                state_ptr = &state[1];
                    417:                ptr_to_last_entry_in_state = &chk[i + numecs + 1];
                    418:
1.7       tedu      419:                for (chk_ptr = &chk[i + 1];
1.8     ! tedu      420:                    chk_ptr != ptr_to_last_entry_in_state; ++chk_ptr)
1.7       tedu      421:                        if (*(state_ptr++) != 0 && *chk_ptr != 0)
1.1       deraadt   422:                                break;
                    423:
1.7       tedu      424:                if (chk_ptr == ptr_to_last_entry_in_state)
1.1       deraadt   425:                        return i;
                    426:
                    427:                else
1.7       tedu      428:                        ++i;
1.1       deraadt   429:        }
1.7       tedu      430: }
1.1       deraadt   431:
                    432:
                    433: /* inittbl - initialize transition tables
                    434:  *
                    435:  * Initializes "firstfree" to be one beyond the end of the table.  Initializes
                    436:  * all "chk" entries to be zero.
                    437:  */
1.8     ! tedu      438: void
        !           439: inittbl()
1.7       tedu      440: {
1.5       mpech     441:        int i;
1.1       deraadt   442:
1.8     ! tedu      443:        zero_out((char *) chk,
1.7       tedu      444:
1.8     ! tedu      445:            (size_t) (current_max_xpairs * sizeof(int)));
1.1       deraadt   446:
                    447:        tblend = 0;
                    448:        firstfree = tblend + 1;
                    449:        numtemps = 0;
                    450:
1.7       tedu      451:        if (usemecs) {
1.8     ! tedu      452:                /*
        !           453:                 * Set up doubly-linked meta-equivalence classes; these are
        !           454:                 * sets of equivalence classes which all have identical
1.1       deraadt   455:                 * transitions out of TEMPLATES.
                    456:                 */
                    457:
                    458:                tecbck[1] = NIL;
                    459:
1.7       tedu      460:                for (i = 2; i <= numecs; ++i) {
1.1       deraadt   461:                        tecbck[i] = i - 1;
                    462:                        tecfwd[i - 1] = i;
1.7       tedu      463:                }
1.1       deraadt   464:
                    465:                tecfwd[numecs] = NIL;
                    466:        }
1.7       tedu      467: }
1.1       deraadt   468:
                    469:
                    470: /* mkdeftbl - make the default, "jam" table entries */
                    471:
1.8     ! tedu      472: void
        !           473: mkdeftbl()
1.7       tedu      474: {
1.8     ! tedu      475:        int i;
1.1       deraadt   476:
                    477:        jamstate = lastdfa + 1;
                    478:
1.8     ! tedu      479:        ++tblend;               /* room for transition on end-of-buffer
        !           480:                                 * character */
1.1       deraadt   481:
1.7       tedu      482:        while (tblend + numecs >= current_max_xpairs)
1.8     ! tedu      483:                expand_nxt_chk();
1.1       deraadt   484:
                    485:        /* Add in default end-of-buffer transition. */
                    486:        nxt[tblend] = end_of_buffer_state;
                    487:        chk[tblend] = jamstate;
                    488:
1.7       tedu      489:        for (i = 1; i <= numecs; ++i) {
1.1       deraadt   490:                nxt[tblend + i] = 0;
                    491:                chk[tblend + i] = jamstate;
1.7       tedu      492:        }
1.1       deraadt   493:
                    494:        jambase = tblend;
                    495:
                    496:        base[jamstate] = jambase;
                    497:        def[jamstate] = 0;
                    498:
                    499:        tblend += numecs;
                    500:        ++numtemps;
1.7       tedu      501: }
1.1       deraadt   502:
                    503:
                    504: /* mkentry - create base/def and nxt/chk entries for transition array
                    505:  *
                    506:  * synopsis
                    507:  *   int state[numchars + 1], numchars, statenum, deflink, totaltrans;
                    508:  *   mkentry( state, numchars, statenum, deflink, totaltrans );
                    509:  *
                    510:  * "state" is a transition array "numchars" characters in size, "statenum"
                    511:  * is the offset to be used into the base/def tables, and "deflink" is the
                    512:  * entry to put in the "def" table entry.  If "deflink" is equal to
                    513:  * "JAMSTATE", then no attempt will be made to fit zero entries of "state"
                    514:  * (i.e., jam entries) into the table.  It is assumed that by linking to
                    515:  * "JAMSTATE" they will be taken care of.  In any case, entries in "state"
                    516:  * marking transitions to "SAME_TRANS" are treated as though they will be
                    517:  * taken care of by whereever "deflink" points.  "totaltrans" is the total
                    518:  * number of transitions out of the state.  If it is below a certain threshold,
                    519:  * the tables are searched for an interior spot that will accommodate the
                    520:  * state array.
                    521:  */
                    522:
1.8     ! tedu      523: void
        !           524: mkentry(state, numchars, statenum, deflink, totaltrans)
        !           525:        int *state;
        !           526:        int numchars, statenum, deflink, totaltrans;
1.7       tedu      527: {
1.5       mpech     528:        int minec, maxec, i, baseaddr;
1.8     ! tedu      529:        int tblbase, tbllast;
1.1       deraadt   530:
1.7       tedu      531:        if (totaltrans == 0) {  /* there are no out-transitions */
                    532:                if (deflink == JAMSTATE)
1.1       deraadt   533:                        base[statenum] = JAMSTATE;
                    534:                else
                    535:                        base[statenum] = 0;
                    536:
                    537:                def[statenum] = deflink;
                    538:                return;
1.7       tedu      539:        }
                    540:        for (minec = 1; minec <= numchars; ++minec) {
                    541:                if (state[minec] != SAME_TRANS)
                    542:                        if (state[minec] != 0 || deflink != JAMSTATE)
1.1       deraadt   543:                                break;
1.7       tedu      544:        }
1.1       deraadt   545:
1.7       tedu      546:        if (totaltrans == 1) {
1.8     ! tedu      547:                /*
        !           548:                 * There's only one out-transition.  Save it for later to
        !           549:                 * fill in holes in the tables.
1.1       deraadt   550:                 */
1.8     ! tedu      551:                stack1(statenum, minec, state[minec], deflink);
1.1       deraadt   552:                return;
1.7       tedu      553:        }
                    554:        for (maxec = numchars; maxec > 0; --maxec) {
                    555:                if (state[maxec] != SAME_TRANS)
                    556:                        if (state[maxec] != 0 || deflink != JAMSTATE)
1.1       deraadt   557:                                break;
1.7       tedu      558:        }
1.1       deraadt   559:
1.8     ! tedu      560:        /*
        !           561:         * Whether we try to fit the state table in the middle of the table
1.1       deraadt   562:         * entries we have already generated, or if we just take the state
                    563:         * table at the end of the nxt/chk tables, we must make sure that we
                    564:         * have a valid base address (i.e., non-negative).  Note that
                    565:         * negative base addresses dangerous at run-time (because indexing
                    566:         * the nxt array with one and a low-valued character will access
                    567:         * memory before the start of the array.
                    568:         */
                    569:
                    570:        /* Find the first transition of state that we need to worry about. */
1.7       tedu      571:        if (totaltrans * 100 <= numchars * INTERIOR_FIT_PERCENTAGE) {
1.1       deraadt   572:                /* Attempt to squeeze it into the middle of the tables. */
                    573:                baseaddr = firstfree;
                    574:
1.7       tedu      575:                while (baseaddr < minec) {
1.8     ! tedu      576:                        /*
        !           577:                         * Using baseaddr would result in a negative base
1.1       deraadt   578:                         * address below; find the next free slot.
                    579:                         */
1.8     ! tedu      580:                        for (++baseaddr; chk[baseaddr] != 0; ++baseaddr);
1.7       tedu      581:                }
1.1       deraadt   582:
1.7       tedu      583:                while (baseaddr + maxec - minec + 1 >= current_max_xpairs)
1.8     ! tedu      584:                        expand_nxt_chk();
1.1       deraadt   585:
1.7       tedu      586:                for (i = minec; i <= maxec; ++i)
                    587:                        if (state[i] != SAME_TRANS &&
                    588:                            (state[i] != 0 || deflink != JAMSTATE) &&
1.8     ! tedu      589:                            chk[baseaddr + i - minec] != 0) {   /* baseaddr unsuitable -
        !           590:                                                                 * find another */
1.7       tedu      591:                                for (++baseaddr;
1.8     ! tedu      592:                                    baseaddr < current_max_xpairs &&
        !           593:                                    chk[baseaddr] != 0; ++baseaddr);
1.7       tedu      594:
                    595:                                while (baseaddr + maxec - minec + 1 >=
1.8     ! tedu      596:                                    current_max_xpairs)
        !           597:                                        expand_nxt_chk();
1.1       deraadt   598:
1.8     ! tedu      599:                                /*
        !           600:                                 * Reset the loop counter so we'll start all
1.1       deraadt   601:                                 * over again next time it's incremented.
                    602:                                 */
                    603:
                    604:                                i = minec - 1;
1.7       tedu      605:                        }
1.8     ! tedu      606:        } else {
        !           607:                /*
        !           608:                 * Ensure that the base address we eventually generate is
1.1       deraadt   609:                 * non-negative.
                    610:                 */
1.8     ! tedu      611:                baseaddr = MAX(tblend + 1, minec);
1.7       tedu      612:        }
1.1       deraadt   613:
                    614:        tblbase = baseaddr - minec;
                    615:        tbllast = tblbase + maxec;
                    616:
1.7       tedu      617:        while (tbllast + 1 >= current_max_xpairs)
1.8     ! tedu      618:                expand_nxt_chk();
1.1       deraadt   619:
                    620:        base[statenum] = tblbase;
                    621:        def[statenum] = deflink;
                    622:
1.7       tedu      623:        for (i = minec; i <= maxec; ++i)
                    624:                if (state[i] != SAME_TRANS)
                    625:                        if (state[i] != 0 || deflink != JAMSTATE) {
1.1       deraadt   626:                                nxt[tblbase + i] = state[i];
                    627:                                chk[tblbase + i] = statenum;
1.7       tedu      628:                        }
                    629:        if (baseaddr == firstfree)
1.1       deraadt   630:                /* Find next free slot in tables. */
1.8     ! tedu      631:                for (++firstfree; chk[firstfree] != 0; ++firstfree);
1.1       deraadt   632:
1.8     ! tedu      633:        tblend = MAX(tblend, tbllast);
1.7       tedu      634: }
1.1       deraadt   635:
                    636:
                    637: /* mk1tbl - create table entries for a state (or state fragment) which
                    638:  *            has only one out-transition
                    639:  */
                    640:
1.8     ! tedu      641: void
        !           642: mk1tbl(state, sym, onenxt, onedef)
        !           643:        int state, sym, onenxt, onedef;
1.7       tedu      644: {
                    645:        if (firstfree < sym)
1.1       deraadt   646:                firstfree = sym;
                    647:
1.7       tedu      648:        while (chk[firstfree] != 0)
                    649:                if (++firstfree >= current_max_xpairs)
1.8     ! tedu      650:                        expand_nxt_chk();
1.1       deraadt   651:
                    652:        base[state] = firstfree - sym;
                    653:        def[state] = onedef;
                    654:        chk[firstfree] = state;
                    655:        nxt[firstfree] = onenxt;
                    656:
1.7       tedu      657:        if (firstfree > tblend) {
1.1       deraadt   658:                tblend = firstfree++;
                    659:
1.7       tedu      660:                if (firstfree >= current_max_xpairs)
1.8     ! tedu      661:                        expand_nxt_chk();
1.1       deraadt   662:        }
1.7       tedu      663: }
1.1       deraadt   664:
                    665:
                    666: /* mkprot - create new proto entry */
                    667:
1.8     ! tedu      668: void
        !           669: mkprot(state, statenum, comstate)
        !           670:        int state[], statenum, comstate;
1.7       tedu      671: {
1.8     ! tedu      672:        int i, slot, tblbase;
1.1       deraadt   673:
1.7       tedu      674:        if (++numprots >= MSP || numecs * numprots >= PROT_SAVE_SIZE) {
1.8     ! tedu      675:                /*
        !           676:                 * Gotta make room for the new proto by dropping last entry
        !           677:                 * in the queue.
1.1       deraadt   678:                 */
                    679:                slot = lastprot;
                    680:                lastprot = protprev[lastprot];
                    681:                protnext[lastprot] = NIL;
1.8     ! tedu      682:        } else
1.1       deraadt   683:                slot = numprots;
                    684:
                    685:        protnext[slot] = firstprot;
                    686:
1.7       tedu      687:        if (firstprot != NIL)
1.1       deraadt   688:                protprev[firstprot] = slot;
                    689:
                    690:        firstprot = slot;
                    691:        prottbl[slot] = statenum;
                    692:        protcomst[slot] = comstate;
                    693:
                    694:        /* Copy state into save area so it can be compared with rapidly. */
                    695:        tblbase = numecs * (slot - 1);
                    696:
1.7       tedu      697:        for (i = 1; i <= numecs; ++i)
1.1       deraadt   698:                protsave[tblbase + i] = state[i];
1.7       tedu      699: }
1.1       deraadt   700:
                    701:
                    702: /* mktemplate - create a template entry based on a state, and connect the state
                    703:  *              to it
                    704:  */
                    705:
1.8     ! tedu      706: void
        !           707: mktemplate(state, statenum, comstate)
        !           708:        int state[], statenum, comstate;
        !           709: {
        !           710:        int i, numdiff, tmpbase, tmp[CSIZE + 1];
        !           711:        Char transset[CSIZE + 1];
        !           712:        int tsptr;
1.1       deraadt   713:
                    714:        ++numtemps;
                    715:
                    716:        tsptr = 0;
                    717:
1.8     ! tedu      718:        /*
        !           719:         * Calculate where we will temporarily store the transition table of
        !           720:         * the template in the tnxt[] array.  The final transition table gets
        !           721:         * created by cmptmps().
1.1       deraadt   722:         */
                    723:
                    724:        tmpbase = numtemps * numecs;
                    725:
1.7       tedu      726:        if (tmpbase + numecs >= current_max_template_xpairs) {
                    727:                current_max_template_xpairs +=
1.8     ! tedu      728:                    MAX_TEMPLATE_XPAIRS_INCREMENT;
1.1       deraadt   729:
                    730:                ++num_reallocs;
                    731:
1.8     ! tedu      732:                tnxt = reallocate_integer_array(tnxt,
        !           733:                    current_max_template_xpairs);
1.7       tedu      734:        }
                    735:        for (i = 1; i <= numecs; ++i)
                    736:                if (state[i] == 0)
1.1       deraadt   737:                        tnxt[tmpbase + i] = 0;
1.7       tedu      738:                else {
1.1       deraadt   739:                        transset[tsptr++] = i;
                    740:                        tnxt[tmpbase + i] = comstate;
1.7       tedu      741:                }
1.1       deraadt   742:
1.7       tedu      743:        if (usemecs)
1.8     ! tedu      744:                mkeccl(transset, tsptr, tecfwd, tecbck, numecs, 0);
1.1       deraadt   745:
1.8     ! tedu      746:        mkprot(tnxt + tmpbase, -numtemps, comstate);
1.1       deraadt   747:
1.8     ! tedu      748:        /*
        !           749:         * We rely on the fact that mkprot adds things to the beginning of
        !           750:         * the proto queue.
1.1       deraadt   751:         */
                    752:
1.8     ! tedu      753:        numdiff = tbldiff(state, firstprot, tmp);
        !           754:        mkentry(tmp, numecs, statenum, -numtemps, numdiff);
1.7       tedu      755: }
1.1       deraadt   756:
                    757:
                    758: /* mv2front - move proto queue element to front of queue */
                    759:
1.8     ! tedu      760: void
        !           761: mv2front(qelm)
        !           762:        int qelm;
1.7       tedu      763: {
                    764:        if (firstprot != qelm) {
                    765:                if (qelm == lastprot)
1.1       deraadt   766:                        lastprot = protprev[lastprot];
                    767:
                    768:                protnext[protprev[qelm]] = protnext[qelm];
                    769:
1.7       tedu      770:                if (protnext[qelm] != NIL)
1.1       deraadt   771:                        protprev[protnext[qelm]] = protprev[qelm];
                    772:
                    773:                protprev[qelm] = NIL;
                    774:                protnext[qelm] = firstprot;
                    775:                protprev[firstprot] = qelm;
                    776:                firstprot = qelm;
                    777:        }
1.7       tedu      778: }
1.1       deraadt   779:
                    780:
                    781: /* place_state - place a state into full speed transition table
                    782:  *
                    783:  * State is the statenum'th state.  It is indexed by equivalence class and
                    784:  * gives the number of the state to enter for a given equivalence class.
                    785:  * Transnum is the number of out-transitions for the state.
                    786:  */
                    787:
1.8     ! tedu      788: void
        !           789: place_state(state, statenum, transnum)
        !           790:        int *state, statenum, transnum;
1.7       tedu      791: {
1.5       mpech     792:        int i;
                    793:        int *state_ptr;
1.8     ! tedu      794:        int position = find_table_space(state, transnum);
1.1       deraadt   795:
                    796:        /* "base" is the table of start positions. */
                    797:        base[statenum] = position;
                    798:
1.8     ! tedu      799:        /*
        !           800:         * Put in action number marker; this non-zero number makes sure that
1.1       deraadt   801:         * find_table_space() knows that this position in chk/nxt is taken
                    802:         * and should not be used for another accepting number in another
                    803:         * state.
                    804:         */
                    805:        chk[position - 1] = 1;
                    806:
1.8     ! tedu      807:        /*
        !           808:         * Put in end-of-buffer marker; this is for the same purposes as
1.1       deraadt   809:         * above.
                    810:         */
                    811:        chk[position] = 1;
                    812:
                    813:        /* Place the state into chk and nxt. */
                    814:        state_ptr = &state[1];
                    815:
1.7       tedu      816:        for (i = 1; i <= numecs; ++i, ++state_ptr)
                    817:                if (*state_ptr != 0) {
1.1       deraadt   818:                        chk[position + i] = i;
                    819:                        nxt[position + i] = *state_ptr;
1.7       tedu      820:                }
                    821:        if (position + numecs > tblend)
1.1       deraadt   822:                tblend = position + numecs;
1.7       tedu      823: }
1.1       deraadt   824:
                    825:
                    826: /* stack1 - save states with only one out-transition to be processed later
                    827:  *
                    828:  * If there's room for another state on the "one-transition" stack, the
                    829:  * state is pushed onto it, to be processed later by mk1tbl.  If there's
                    830:  * no room, we process the sucker right now.
                    831:  */
                    832:
1.8     ! tedu      833: void
        !           834: stack1(statenum, sym, nextstate, deflink)
        !           835:        int statenum, sym, nextstate, deflink;
1.7       tedu      836: {
                    837:        if (onesp >= ONE_STACK_SIZE - 1)
1.8     ! tedu      838:                mk1tbl(statenum, sym, nextstate, deflink);
1.1       deraadt   839:
1.7       tedu      840:        else {
1.1       deraadt   841:                ++onesp;
                    842:                onestate[onesp] = statenum;
                    843:                onesym[onesp] = sym;
                    844:                onenext[onesp] = nextstate;
                    845:                onedef[onesp] = deflink;
                    846:        }
1.7       tedu      847: }
1.1       deraadt   848:
                    849:
                    850: /* tbldiff - compute differences between two state tables
                    851:  *
                    852:  * "state" is the state array which is to be extracted from the pr'th
                    853:  * proto.  "pr" is both the number of the proto we are extracting from
                    854:  * and an index into the save area where we can find the proto's complete
                    855:  * state table.  Each entry in "state" which differs from the corresponding
                    856:  * entry of "pr" will appear in "ext".
                    857:  *
                    858:  * Entries which are the same in both "state" and "pr" will be marked
                    859:  * as transitions to "SAME_TRANS" in "ext".  The total number of differences
                    860:  * between "state" and "pr" is returned as function value.  Note that this
                    861:  * number is "numecs" minus the number of "SAME_TRANS" entries in "ext".
                    862:  */
                    863:
1.8     ! tedu      864: int
        !           865: tbldiff(state, pr, ext)
        !           866:        int state[], pr, ext[];
1.7       tedu      867: {
1.5       mpech     868:        int i, *sp = state, *ep = ext, *protp;
                    869:        int numdiff = 0;
1.1       deraadt   870:
                    871:        protp = &protsave[numecs * (pr - 1)];
                    872:
1.7       tedu      873:        for (i = numecs; i > 0; --i) {
                    874:                if (*++protp == *++sp)
1.1       deraadt   875:                        *++ep = SAME_TRANS;
1.7       tedu      876:                else {
1.1       deraadt   877:                        *++ep = *sp;
                    878:                        ++numdiff;
                    879:                }
1.7       tedu      880:        }
1.1       deraadt   881:
                    882:        return numdiff;
1.7       tedu      883: }