Age Owner Branch data TLA Line data Source code
1 : : /*
2 : : * DFA routines
3 : : * This file is #included by regexec.c.
4 : : *
5 : : * Copyright (c) 1998, 1999 Henry Spencer. All rights reserved.
6 : : *
7 : : * Development of this software was funded, in part, by Cray Research Inc.,
8 : : * UUNET Communications Services Inc., Sun Microsystems Inc., and Scriptics
9 : : * Corporation, none of whom are responsible for the results. The author
10 : : * thanks all of them.
11 : : *
12 : : * Redistribution and use in source and binary forms -- with or without
13 : : * modification -- are permitted for any purpose, provided that
14 : : * redistributions in source form retain this entire copyright notice and
15 : : * indicate the origin and nature of any modifications.
16 : : *
17 : : * I'd appreciate being given credit for this package in the documentation
18 : : * of software which uses it, but that is not a requirement.
19 : : *
20 : : * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
21 : : * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
22 : : * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
23 : : * HENRY SPENCER BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 : : * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 : : * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26 : : * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 : : * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 : : * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29 : : * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 : : *
31 : : * src/backend/regex/rege_dfa.c
32 : : *
33 : : */
34 : :
35 : : /*
36 : : * longest - longest-preferred matching engine
37 : : *
38 : : * On success, returns match endpoint address. Returns NULL on no match.
39 : : * Internal errors also return NULL, with v->err set.
40 : : */
41 : : static chr *
2999 tgl@sss.pgh.pa.us 42 :CBC 470987 : longest(struct vars *v,
43 : : struct dfa *d,
44 : : chr *start, /* where the match should start */
45 : : chr *stop, /* match must end at or before here */
46 : : int *hitstopp) /* record whether hit v->stop, if non-NULL */
47 : : {
48 : : chr *cp;
8069 bruce@momjian.us 49 [ + + ]: 470987 : chr *realstop = (stop == v->stop) ? stop : stop + 1;
50 : : color co;
51 : : struct sset *css;
52 : : struct sset *ss;
53 : : chr *post;
54 : : int i;
8249 tgl@sss.pgh.pa.us 55 : 470987 : struct colormap *cm = d->cm;
56 : :
57 : : /* prevent "uninitialized variable" warnings */
3627 58 [ + + ]: 470987 : if (hitstopp != NULL)
59 : 453448 : *hitstopp = 0;
60 : :
61 : : /* if this is a backref to a known string, just match against that */
1649 62 [ + + ]: 470987 : if (d->backno >= 0)
63 : : {
64 [ - + ]: 793 : assert((size_t) d->backno < v->nmatch);
65 [ + + ]: 793 : if (v->pmatch[d->backno].rm_so >= 0)
66 : : {
67 : 616 : cp = dfa_backref(v, d, start, start, stop, false);
68 [ + + + - : 616 : if (cp == v->stop && stop == v->stop && hitstopp != NULL)
- + ]
1649 tgl@sss.pgh.pa.us 69 :UBC 0 : *hitstopp = 1;
1649 tgl@sss.pgh.pa.us 70 :CBC 616 : return cp;
71 : : }
72 : : }
73 : :
74 : : /* fast path for matchall NFAs */
1659 75 [ + + ]: 470371 : if (d->cnfa->flags & MATCHALL)
76 : : {
77 : 2728 : size_t nchr = stop - start;
78 : 2728 : size_t maxmatchall = d->cnfa->maxmatchall;
79 : :
80 [ + + ]: 2728 : if (nchr < d->cnfa->minmatchall)
81 : 165 : return NULL;
82 [ + + ]: 2563 : if (maxmatchall == DUPINF)
83 : : {
84 [ + + + + ]: 1720 : if (stop == v->stop && hitstopp != NULL)
85 : 5 : *hitstopp = 1;
86 : : }
87 : : else
88 : : {
89 [ + + + + : 843 : if (stop == v->stop && nchr <= maxmatchall + 1 && hitstopp != NULL)
+ + ]
90 : 84 : *hitstopp = 1;
91 [ + + ]: 843 : if (nchr > maxmatchall)
92 : 573 : return start + maxmatchall;
93 : : }
94 : 1990 : return stop;
95 : : }
96 : :
97 : : /* initialize */
8249 98 : 467643 : css = initialize(v, d, start);
3627 99 [ - + ]: 467643 : if (css == NULL)
3627 tgl@sss.pgh.pa.us 100 :UBC 0 : return NULL;
8249 tgl@sss.pgh.pa.us 101 :CBC 467643 : cp = start;
102 : :
103 : : /* startup */
104 : : FDEBUG(("+++ startup +++\n"));
8069 bruce@momjian.us 105 [ + + ]: 467643 : if (cp == v->start)
106 : : {
107 : 1758 : co = d->cnfa->bos[(v->eflags & REG_NOTBOL) ? 0 : 1];
108 : : FDEBUG(("color %ld\n", (long) co));
109 : : }
110 : : else
111 : : {
8249 tgl@sss.pgh.pa.us 112 [ + - ]: 465885 : co = GETCOLOR(cm, *(cp - 1));
113 : : FDEBUG(("char %c, color %ld\n", (char) *(cp - 1), (long) co));
114 : : }
115 : 467643 : css = miss(v, d, css, co, cp, start);
116 [ + + ]: 467643 : if (css == NULL)
117 : 216 : return NULL;
118 : 467427 : css->lastseen = cp;
119 : :
120 : : /*
121 : : * This is the main text-scanning loop. It seems worth having two copies
122 : : * to avoid the overhead of REG_FTRACE tests here, even in REG_DEBUG
123 : : * builds, when you're not actively tracing.
124 : : */
125 : : #ifdef REG_DEBUG
126 : : if (v->eflags & REG_FTRACE)
127 : : {
128 : : while (cp < realstop)
129 : : {
130 : : FDEBUG(("+++ at c%d +++\n", (int) (css - d->ssets)));
131 : : co = GETCOLOR(cm, *cp);
132 : : FDEBUG(("char %c, color %ld\n", (char) *cp, (long) co));
133 : : ss = css->outs[co];
134 : : if (ss == NULL)
135 : : {
136 : : ss = miss(v, d, css, co, cp + 1, start);
137 : : if (ss == NULL)
138 : : break; /* NOTE BREAK OUT */
139 : : }
140 : : cp++;
141 : : ss->lastseen = cp;
142 : : css = ss;
143 : : }
144 : : }
145 : : else
146 : : #endif
147 : : {
8069 bruce@momjian.us 148 [ + + ]: 5952970 : while (cp < realstop)
149 : : {
8249 tgl@sss.pgh.pa.us 150 [ + + ]: 5939812 : co = GETCOLOR(cm, *cp);
151 : 5939812 : ss = css->outs[co];
8069 bruce@momjian.us 152 [ + + ]: 5939812 : if (ss == NULL)
153 : : {
154 : 1497745 : ss = miss(v, d, css, co, cp + 1, start);
8249 tgl@sss.pgh.pa.us 155 [ + + ]: 1497745 : if (ss == NULL)
8069 bruce@momjian.us 156 : 454269 : break; /* NOTE BREAK OUT */
157 : : }
8249 tgl@sss.pgh.pa.us 158 : 5485543 : cp++;
159 : 5485543 : ss->lastseen = cp;
160 : 5485543 : css = ss;
161 : : }
162 : : }
163 : :
3627 164 [ - + ]: 467427 : if (ISERR())
3627 tgl@sss.pgh.pa.us 165 :UBC 0 : return NULL;
166 : :
167 : : /* shutdown */
168 : : FDEBUG(("+++ shutdown at c%d +++\n", (int) (css - d->ssets)));
8069 bruce@momjian.us 169 [ + + + + ]:CBC 467427 : if (cp == v->stop && stop == v->stop)
170 : : {
8249 tgl@sss.pgh.pa.us 171 [ + + ]: 7176 : if (hitstopp != NULL)
172 : 3651 : *hitstopp = 1;
8069 bruce@momjian.us 173 : 7176 : co = d->cnfa->eos[(v->eflags & REG_NOTEOL) ? 0 : 1];
174 : : FDEBUG(("color %ld\n", (long) co));
8249 tgl@sss.pgh.pa.us 175 : 7176 : ss = miss(v, d, css, co, cp, start);
3627 176 [ - + ]: 7176 : if (ISERR())
3627 tgl@sss.pgh.pa.us 177 :UBC 0 : return NULL;
178 : : /* special case: match ended at eol? */
8069 bruce@momjian.us 179 [ + + + - ]:CBC 7176 : if (ss != NULL && (ss->flags & POSTSTATE))
8249 tgl@sss.pgh.pa.us 180 : 3916 : return cp;
181 [ - + ]: 3260 : else if (ss != NULL)
8249 tgl@sss.pgh.pa.us 182 :UBC 0 : ss->lastseen = cp; /* to be tidy */
183 : : }
184 : :
185 : : /* find last match, if any */
8249 tgl@sss.pgh.pa.us 186 :CBC 463511 : post = d->lastpost;
187 [ + + ]: 2404035 : for (ss = d->ssets, i = d->nssused; i > 0; ss++, i--)
8069 bruce@momjian.us 188 [ + + + + : 1940524 : if ((ss->flags & POSTSTATE) && post != ss->lastseen &&
+ + ]
189 [ + + ]: 7046 : (post == NULL || post < ss->lastseen))
8249 tgl@sss.pgh.pa.us 190 : 467927 : post = ss->lastseen;
8069 bruce@momjian.us 191 [ + + ]: 463511 : if (post != NULL) /* found one */
8249 tgl@sss.pgh.pa.us 192 : 461269 : return post - 1;
193 : :
194 : 2242 : return NULL;
195 : : }
196 : :
197 : : /*
198 : : * shortest - shortest-preferred matching engine
199 : : *
200 : : * On success, returns match endpoint address. Returns NULL on no match.
201 : : * Internal errors also return NULL, with v->err set.
202 : : */
203 : : static chr *
2999 204 : 4037052 : shortest(struct vars *v,
205 : : struct dfa *d,
206 : : chr *start, /* where the match should start */
207 : : chr *min, /* match must end at or after here */
208 : : chr *max, /* match must end at or before here */
209 : : chr **coldp, /* store coldstart pointer here, if non-NULL */
210 : : int *hitstopp) /* record whether hit v->stop, if non-NULL */
211 : : {
212 : : chr *cp;
8069 bruce@momjian.us 213 [ + + ]: 4037052 : chr *realmin = (min == v->stop) ? min : min + 1;
214 [ + + ]: 4037052 : chr *realmax = (max == v->stop) ? max : max + 1;
215 : : color co;
216 : : struct sset *css;
217 : : struct sset *ss;
8249 tgl@sss.pgh.pa.us 218 : 4037052 : struct colormap *cm = d->cm;
219 : :
220 : : /* prevent "uninitialized variable" warnings */
3627 221 [ + + ]: 4037052 : if (coldp != NULL)
222 : 4036222 : *coldp = NULL;
223 [ + + ]: 4037052 : if (hitstopp != NULL)
224 : 159 : *hitstopp = 0;
225 : :
226 : : /* if this is a backref to a known string, just match against that */
1649 227 [ - + ]: 4037052 : if (d->backno >= 0)
228 : : {
1649 tgl@sss.pgh.pa.us 229 [ # # ]:UBC 0 : assert((size_t) d->backno < v->nmatch);
230 [ # # ]: 0 : if (v->pmatch[d->backno].rm_so >= 0)
231 : : {
232 : 0 : cp = dfa_backref(v, d, start, min, max, true);
233 [ # # # # ]: 0 : if (cp != NULL && coldp != NULL)
234 : 0 : *coldp = start;
235 : : /* there is no case where we should set *hitstopp */
236 : 0 : return cp;
237 : : }
238 : : }
239 : :
240 : : /* fast path for matchall NFAs */
1659 tgl@sss.pgh.pa.us 241 [ + + ]:CBC 4037052 : if (d->cnfa->flags & MATCHALL)
242 : : {
243 : 987 : size_t nchr = min - start;
244 : :
245 [ + + ]: 987 : if (d->cnfa->maxmatchall != DUPINF &&
246 [ - + ]: 12 : nchr > d->cnfa->maxmatchall)
1659 tgl@sss.pgh.pa.us 247 :UBC 0 : return NULL;
1659 tgl@sss.pgh.pa.us 248 [ + + ]:CBC 987 : if ((max - start) < d->cnfa->minmatchall)
249 : 9 : return NULL;
250 [ + + ]: 978 : if (nchr < d->cnfa->minmatchall)
251 : 65 : min = start + d->cnfa->minmatchall;
252 [ + + ]: 978 : if (coldp != NULL)
253 : 448 : *coldp = start;
254 : : /* there is no case where we should set *hitstopp */
255 : 978 : return min;
256 : : }
257 : :
258 : : /* initialize */
8249 259 : 4036065 : css = initialize(v, d, start);
3627 260 [ - + ]: 4036065 : if (css == NULL)
3627 tgl@sss.pgh.pa.us 261 :UBC 0 : return NULL;
8249 tgl@sss.pgh.pa.us 262 :CBC 4036065 : cp = start;
263 : :
264 : : /* startup */
265 : : FDEBUG(("--- startup ---\n"));
8069 bruce@momjian.us 266 [ + + ]: 4036065 : if (cp == v->start)
267 : : {
268 : 3587103 : co = d->cnfa->bos[(v->eflags & REG_NOTBOL) ? 0 : 1];
269 : : FDEBUG(("color %ld\n", (long) co));
270 : : }
271 : : else
272 : : {
8249 tgl@sss.pgh.pa.us 273 [ + - ]: 448962 : co = GETCOLOR(cm, *(cp - 1));
274 : : FDEBUG(("char %c, color %ld\n", (char) *(cp - 1), (long) co));
275 : : }
276 : 4036065 : css = miss(v, d, css, co, cp, start);
277 [ + + ]: 4036065 : if (css == NULL)
278 : 6 : return NULL;
279 : 4036059 : css->lastseen = cp;
280 : 4036059 : ss = css;
281 : :
282 : : /*
283 : : * This is the main text-scanning loop. It seems worth having two copies
284 : : * to avoid the overhead of REG_FTRACE tests here, even in REG_DEBUG
285 : : * builds, when you're not actively tracing.
286 : : */
287 : : #ifdef REG_DEBUG
288 : : if (v->eflags & REG_FTRACE)
289 : : {
290 : : while (cp < realmax)
291 : : {
292 : : FDEBUG(("--- at c%d ---\n", (int) (css - d->ssets)));
293 : : co = GETCOLOR(cm, *cp);
294 : : FDEBUG(("char %c, color %ld\n", (char) *cp, (long) co));
295 : : ss = css->outs[co];
296 : : if (ss == NULL)
297 : : {
298 : : ss = miss(v, d, css, co, cp + 1, start);
299 : : if (ss == NULL)
300 : : break; /* NOTE BREAK OUT */
301 : : }
302 : : cp++;
303 : : ss->lastseen = cp;
304 : : css = ss;
305 : : if ((ss->flags & POSTSTATE) && cp >= realmin)
306 : : break; /* NOTE BREAK OUT */
307 : : }
308 : : }
309 : : else
310 : : #endif
311 : : {
8069 bruce@momjian.us 312 [ + + ]: 22524034 : while (cp < realmax)
313 : : {
8249 tgl@sss.pgh.pa.us 314 [ + + ]: 22257062 : co = GETCOLOR(cm, *cp);
315 : 22257062 : ss = css->outs[co];
8069 bruce@momjian.us 316 [ + + ]: 22257062 : if (ss == NULL)
317 : : {
318 : 6912384 : ss = miss(v, d, css, co, cp + 1, start);
8249 tgl@sss.pgh.pa.us 319 [ + + ]: 6912384 : if (ss == NULL)
8069 bruce@momjian.us 320 : 3201200 : break; /* NOTE BREAK OUT */
321 : : }
8249 tgl@sss.pgh.pa.us 322 : 19055862 : cp++;
323 : 19055862 : ss->lastseen = cp;
324 : 19055862 : css = ss;
8069 bruce@momjian.us 325 [ + + + + ]: 19055862 : if ((ss->flags & POSTSTATE) && cp >= realmin)
326 : 567887 : break; /* NOTE BREAK OUT */
327 : : }
328 : : }
329 : :
8249 tgl@sss.pgh.pa.us 330 [ + + ]: 4036059 : if (ss == NULL)
331 : 3201200 : return NULL;
332 : :
7266 bruce@momjian.us 333 [ + + ]: 834859 : if (coldp != NULL) /* report last no-progress state set, if any */
8249 tgl@sss.pgh.pa.us 334 : 834591 : *coldp = lastcold(v, d);
335 : :
8069 bruce@momjian.us 336 [ + + + + ]: 834859 : if ((ss->flags & POSTSTATE) && cp > min)
337 : : {
8249 tgl@sss.pgh.pa.us 338 [ - + ]: 567871 : assert(cp >= realmin);
339 : 567871 : cp--;
340 : : }
8069 bruce@momjian.us 341 [ + - + - ]: 266988 : else if (cp == v->stop && max == v->stop)
342 : : {
343 : 266988 : co = d->cnfa->eos[(v->eflags & REG_NOTEOL) ? 0 : 1];
344 : : FDEBUG(("color %ld\n", (long) co));
8249 tgl@sss.pgh.pa.us 345 : 266988 : ss = miss(v, d, css, co, cp, start);
346 : : /* match might have ended at eol */
8069 bruce@momjian.us 347 [ + + - + : 266988 : if ((ss == NULL || !(ss->flags & POSTSTATE)) && hitstopp != NULL)
+ + ]
8249 tgl@sss.pgh.pa.us 348 : 6 : *hitstopp = 1;
349 : : }
350 : :
8069 bruce@momjian.us 351 [ + + - + ]: 834859 : if (ss == NULL || !(ss->flags & POSTSTATE))
8249 tgl@sss.pgh.pa.us 352 : 257248 : return NULL;
353 : :
354 : 577611 : return cp;
355 : : }
356 : :
357 : : /*
358 : : * matchuntil - incremental matching engine
359 : : *
360 : : * This is meant for use with a search-style NFA (that is, the pattern is
361 : : * known to act as though it had a leading .*). We determine whether a
362 : : * match exists starting at v->start and ending at probe. Multiple calls
363 : : * require only O(N) time not O(N^2) so long as the probe values are
364 : : * nondecreasing. *lastcss and *lastcp must be initialized to NULL before
365 : : * starting a series of calls.
366 : : *
367 : : * Returns 1 if a match exists, 0 if not.
368 : : * Internal errors also return 0, with v->err set.
369 : : */
370 : : static int
2999 371 : 60 : matchuntil(struct vars *v,
372 : : struct dfa *d,
373 : : chr *probe, /* we want to know if a match ends here */
374 : : struct sset **lastcss, /* state storage across calls */
375 : : chr **lastcp) /* state storage across calls */
376 : : {
3599 377 : 60 : chr *cp = *lastcp;
378 : : color co;
379 : 60 : struct sset *css = *lastcss;
380 : : struct sset *ss;
381 : 60 : struct colormap *cm = d->cm;
382 : :
383 : : /* fast path for matchall NFAs */
1659 384 [ + + ]: 60 : if (d->cnfa->flags & MATCHALL)
385 : : {
386 : 18 : size_t nchr = probe - v->start;
387 : :
388 [ + + ]: 18 : if (nchr < d->cnfa->minmatchall)
389 : 9 : return 0;
390 : : /* maxmatchall will always be infinity, cf. makesearch() */
1471 391 [ - + ]: 9 : assert(d->cnfa->maxmatchall == DUPINF);
1659 392 : 9 : return 1;
393 : : }
394 : :
395 : : /* initialize and startup, or restart, if necessary */
3599 396 [ + + + + ]: 42 : if (cp == NULL || cp > probe)
397 : : {
398 : 12 : cp = v->start;
399 : 12 : css = initialize(v, d, cp);
400 [ - + ]: 12 : if (css == NULL)
3599 tgl@sss.pgh.pa.us 401 :UBC 0 : return 0;
402 : :
403 : : FDEBUG((">>> startup >>>\n"));
3599 tgl@sss.pgh.pa.us 404 :CBC 12 : co = d->cnfa->bos[(v->eflags & REG_NOTBOL) ? 0 : 1];
405 : : FDEBUG(("color %ld\n", (long) co));
406 : :
407 : 12 : css = miss(v, d, css, co, cp, v->start);
408 [ - + ]: 12 : if (css == NULL)
3599 tgl@sss.pgh.pa.us 409 :UBC 0 : return 0;
3599 tgl@sss.pgh.pa.us 410 :CBC 12 : css->lastseen = cp;
411 : : }
412 [ - + ]: 30 : else if (css == NULL)
413 : : {
414 : : /* we previously found that no match is possible beyond *lastcp */
3599 tgl@sss.pgh.pa.us 415 :UBC 0 : return 0;
416 : : }
3599 tgl@sss.pgh.pa.us 417 :CBC 42 : ss = css;
418 : :
419 : : /*
420 : : * This is the main text-scanning loop. It seems worth having two copies
421 : : * to avoid the overhead of REG_FTRACE tests here, even in REG_DEBUG
422 : : * builds, when you're not actively tracing.
423 : : */
424 : : #ifdef REG_DEBUG
425 : : if (v->eflags & REG_FTRACE)
426 : : {
427 : : while (cp < probe)
428 : : {
429 : : FDEBUG((">>> at c%d >>>\n", (int) (css - d->ssets)));
430 : : co = GETCOLOR(cm, *cp);
431 : : FDEBUG(("char %c, color %ld\n", (char) *cp, (long) co));
432 : : ss = css->outs[co];
433 : : if (ss == NULL)
434 : : {
435 : : ss = miss(v, d, css, co, cp + 1, v->start);
436 : : if (ss == NULL)
437 : : break; /* NOTE BREAK OUT */
438 : : }
439 : : cp++;
440 : : ss->lastseen = cp;
441 : : css = ss;
442 : : }
443 : : }
444 : : else
445 : : #endif
446 : : {
447 [ + + ]: 90 : while (cp < probe)
448 : : {
449 [ + - ]: 48 : co = GETCOLOR(cm, *cp);
450 : 48 : ss = css->outs[co];
451 [ + + ]: 48 : if (ss == NULL)
452 : : {
453 : 6 : ss = miss(v, d, css, co, cp + 1, v->start);
454 [ - + ]: 6 : if (ss == NULL)
3599 tgl@sss.pgh.pa.us 455 :UBC 0 : break; /* NOTE BREAK OUT */
456 : : }
3599 tgl@sss.pgh.pa.us 457 :CBC 48 : cp++;
458 : 48 : ss->lastseen = cp;
459 : 48 : css = ss;
460 : : }
461 : : }
462 : :
463 : 42 : *lastcss = ss;
464 : 42 : *lastcp = cp;
465 : :
466 [ - + ]: 42 : if (ss == NULL)
3599 tgl@sss.pgh.pa.us 467 :UBC 0 : return 0; /* impossible match, or internal error */
468 : :
469 : : /* We need to process one more chr, or the EOS symbol, to check match */
3599 tgl@sss.pgh.pa.us 470 [ + - ]:CBC 42 : if (cp < v->stop)
471 : : {
472 : : FDEBUG((">>> at c%d >>>\n", (int) (css - d->ssets)));
473 [ + - ]: 42 : co = GETCOLOR(cm, *cp);
474 : : FDEBUG(("char %c, color %ld\n", (char) *cp, (long) co));
475 : 42 : ss = css->outs[co];
476 [ + + ]: 42 : if (ss == NULL)
477 : 27 : ss = miss(v, d, css, co, cp + 1, v->start);
478 : : }
479 : : else
480 : : {
3599 tgl@sss.pgh.pa.us 481 [ # # ]:UBC 0 : assert(cp == v->stop);
482 : 0 : co = d->cnfa->eos[(v->eflags & REG_NOTEOL) ? 0 : 1];
483 : : FDEBUG(("color %ld\n", (long) co));
484 : 0 : ss = miss(v, d, css, co, cp, v->start);
485 : : }
486 : :
3599 tgl@sss.pgh.pa.us 487 [ + - + + ]:CBC 42 : if (ss == NULL || !(ss->flags & POSTSTATE))
488 : 30 : return 0;
489 : :
490 : 12 : return 1;
491 : : }
492 : :
493 : : /*
494 : : * dfa_backref - find best match length for a known backref string
495 : : *
496 : : * When the backref's referent is already available, we can deliver an exact
497 : : * answer with considerably less work than running the backref node's NFA.
498 : : *
499 : : * Return match endpoint for longest or shortest valid repeated match,
500 : : * or NULL if there is no valid match.
501 : : *
502 : : * Should be in sync with cbrdissect(), although that has the different task
503 : : * of checking a match to a predetermined section of the string.
504 : : */
505 : : static chr *
1649 506 : 616 : dfa_backref(struct vars *v,
507 : : struct dfa *d,
508 : : chr *start, /* where the match should start */
509 : : chr *min, /* match must end at or after here */
510 : : chr *max, /* match must end at or before here */
511 : : bool shortest)
512 : : {
513 : 616 : int n = d->backno;
514 : 616 : int backmin = d->backmin;
515 : 616 : int backmax = d->backmax;
516 : : size_t numreps;
517 : : size_t minreps;
518 : : size_t maxreps;
519 : : size_t brlen;
520 : : chr *brstring;
521 : : chr *p;
522 : :
523 : : /* get the backreferenced string (caller should have checked this) */
524 [ - + ]: 616 : if (v->pmatch[n].rm_so == -1)
1649 tgl@sss.pgh.pa.us 525 :UBC 0 : return NULL;
1649 tgl@sss.pgh.pa.us 526 :CBC 616 : brstring = v->start + v->pmatch[n].rm_so;
527 : 616 : brlen = v->pmatch[n].rm_eo - v->pmatch[n].rm_so;
528 : :
529 : : /* special-case zero-length backreference to avoid divide by zero */
530 [ + + ]: 616 : if (brlen == 0)
531 : : {
532 : : /*
533 : : * matches only a zero-length string, but any number of repetitions
534 : : * can be considered to be present
535 : : */
536 [ + - + - ]: 1 : if (min == start && backmin <= backmax)
537 : 1 : return start;
1649 tgl@sss.pgh.pa.us 538 :UBC 0 : return NULL;
539 : : }
540 : :
541 : : /*
542 : : * convert min and max into numbers of possible repetitions of the backref
543 : : * string, rounding appropriately
544 : : */
1649 tgl@sss.pgh.pa.us 545 [ + - ]:CBC 615 : if (min <= start)
546 : 615 : minreps = 0;
547 : : else
1649 tgl@sss.pgh.pa.us 548 :UBC 0 : minreps = (min - start - 1) / brlen + 1;
1649 tgl@sss.pgh.pa.us 549 :CBC 615 : maxreps = (max - start) / brlen;
550 : :
551 : : /* apply bounds, then see if there is any allowed match length */
552 [ + + ]: 615 : if (minreps < backmin)
553 : 597 : minreps = backmin;
554 [ + + + + ]: 615 : if (backmax != DUPINF && maxreps > backmax)
555 : 297 : maxreps = backmax;
556 [ + + ]: 615 : if (maxreps < minreps)
557 : 134 : return NULL;
558 : :
559 : : /* quick exit if zero-repetitions match is valid and preferred */
560 [ - + - - ]: 481 : if (shortest && minreps == 0)
1649 tgl@sss.pgh.pa.us 561 :UBC 0 : return start;
562 : :
563 : : /* okay, compare the actual string contents */
1649 tgl@sss.pgh.pa.us 564 :CBC 481 : p = start;
565 : 481 : numreps = 0;
566 [ + + ]: 570 : while (numreps < maxreps)
567 : : {
568 [ + + ]: 492 : if ((*v->g->compare) (brstring, p, brlen) != 0)
569 : 403 : break;
570 : 89 : p += brlen;
571 : 89 : numreps++;
572 [ - + - - ]: 89 : if (shortest && numreps >= minreps)
1649 tgl@sss.pgh.pa.us 573 :UBC 0 : break;
574 : : }
575 : :
1649 tgl@sss.pgh.pa.us 576 [ + + ]:CBC 481 : if (numreps >= minreps)
577 : 82 : return p;
578 : 399 : return NULL;
579 : : }
580 : :
581 : : /*
582 : : * lastcold - determine last point at which no progress had been made
583 : : */
584 : : static chr * /* endpoint, or NULL */
2999 585 : 834591 : lastcold(struct vars *v,
586 : : struct dfa *d)
587 : : {
588 : : struct sset *ss;
589 : : chr *nopr;
590 : : int i;
591 : :
8249 592 : 834591 : nopr = d->lastnopr;
593 [ + + ]: 834591 : if (nopr == NULL)
594 : 834589 : nopr = v->start;
595 [ + + ]: 4465477 : for (ss = d->ssets, i = d->nssused; i > 0; ss++, i--)
8069 bruce@momjian.us 596 [ + + + + ]: 3630886 : if ((ss->flags & NOPROGRESS) && nopr < ss->lastseen)
8249 tgl@sss.pgh.pa.us 597 : 1189355 : nopr = ss->lastseen;
598 : 834591 : return nopr;
599 : : }
600 : :
601 : : /*
602 : : * newdfa - set up a fresh DFA
603 : : *
604 : : * Returns NULL (and sets v->err) on failure.
605 : : */
606 : : static struct dfa *
2999 607 : 4501884 : newdfa(struct vars *v,
608 : : struct cnfa *cnfa,
609 : : struct colormap *cm,
610 : : struct smalldfa *sml) /* preallocated space, may be NULL */
611 : : {
612 : : struct dfa *d;
8069 bruce@momjian.us 613 : 4501884 : size_t nss = cnfa->nstates * 2;
614 : 4501884 : int wordsper = (cnfa->nstates + UBITS - 1) / UBITS;
1658 tgl@sss.pgh.pa.us 615 : 4501884 : bool ismalloced = false;
616 : :
8249 617 [ + - - + ]: 4501884 : assert(cnfa != NULL && cnfa->nstates != 0);
618 : :
8069 bruce@momjian.us 619 [ + + + + ]: 4501884 : if (nss <= FEWSTATES && cnfa->ncolors <= FEWCOLORS)
620 : : {
8249 tgl@sss.pgh.pa.us 621 [ - + ]: 2198116 : assert(wordsper == 1);
4950 622 [ + + ]: 2198116 : if (sml == NULL)
623 : : {
624 : 11725 : sml = (struct smalldfa *) MALLOC(sizeof(struct smalldfa));
625 [ - + ]: 11725 : if (sml == NULL)
626 : : {
8249 tgl@sss.pgh.pa.us 627 [ # # ]:UBC 0 : ERR(REG_ESPACE);
628 : 0 : return NULL;
629 : : }
1658 tgl@sss.pgh.pa.us 630 :CBC 11725 : ismalloced = true;
631 : : }
4950 632 : 2198116 : d = &sml->dfa;
633 : 2198116 : d->ssets = sml->ssets;
634 : 2198116 : d->statesarea = sml->statesarea;
8249 635 : 2198116 : d->work = &d->statesarea[nss];
4950 636 : 2198116 : d->outsarea = sml->outsarea;
637 : 2198116 : d->incarea = sml->incarea;
1658 638 : 2198116 : d->ismalloced = ismalloced;
639 : 2198116 : d->arraysmalloced = false; /* not separately allocated, anyway */
640 : : }
641 : : else
642 : : {
8069 bruce@momjian.us 643 : 2303768 : d = (struct dfa *) MALLOC(sizeof(struct dfa));
644 [ - + ]: 2303768 : if (d == NULL)
645 : : {
8249 tgl@sss.pgh.pa.us 646 [ # # ]:UBC 0 : ERR(REG_ESPACE);
647 : 0 : return NULL;
648 : : }
8069 bruce@momjian.us 649 :CBC 2303768 : d->ssets = (struct sset *) MALLOC(nss * sizeof(struct sset));
650 : 2303768 : d->statesarea = (unsigned *) MALLOC((nss + WORK) * wordsper *
651 : : sizeof(unsigned));
8249 tgl@sss.pgh.pa.us 652 : 2303768 : d->work = &d->statesarea[nss * wordsper];
8069 bruce@momjian.us 653 : 2303768 : d->outsarea = (struct sset **) MALLOC(nss * cnfa->ncolors *
654 : : sizeof(struct sset *));
655 : 2303768 : d->incarea = (struct arcp *) MALLOC(nss * cnfa->ncolors *
656 : : sizeof(struct arcp));
1658 tgl@sss.pgh.pa.us 657 : 2303768 : d->ismalloced = true;
658 : 2303768 : d->arraysmalloced = true;
659 : : /* now freedfa() will behave sanely */
8249 660 [ + - + - ]: 2303768 : if (d->ssets == NULL || d->statesarea == NULL ||
8069 bruce@momjian.us 661 [ + - - + ]: 2303768 : d->outsarea == NULL || d->incarea == NULL)
662 : : {
8249 tgl@sss.pgh.pa.us 663 :UBC 0 : freedfa(d);
664 [ # # ]: 0 : ERR(REG_ESPACE);
665 : 0 : return NULL;
666 : : }
667 : : }
668 : :
8069 bruce@momjian.us 669 [ + + ]:CBC 4501884 : d->nssets = (v->eflags & REG_SMALL) ? 7 : nss;
8249 tgl@sss.pgh.pa.us 670 : 4501884 : d->nssused = 0;
671 : 4501884 : d->nstates = cnfa->nstates;
672 : 4501884 : d->ncolors = cnfa->ncolors;
673 : 4501884 : d->wordsper = wordsper;
674 : 4501884 : d->cnfa = cnfa;
675 : 4501884 : d->cm = cm;
676 : 4501884 : d->lastpost = NULL;
677 : 4501884 : d->lastnopr = NULL;
678 : 4501884 : d->search = d->ssets;
1649 679 : 4501884 : d->backno = -1; /* may be set by caller */
680 : 4501884 : d->backmin = d->backmax = 0;
681 : :
682 : : /* initialization of sset fields is done as needed */
683 : :
8249 684 : 4501884 : return d;
685 : : }
686 : :
687 : : /*
688 : : * freedfa - free a DFA
689 : : */
690 : : static void
2999 691 : 4501884 : freedfa(struct dfa *d)
692 : : {
1658 693 [ + + ]: 4501884 : if (d->arraysmalloced)
694 : : {
8249 695 [ + - ]: 2303768 : if (d->ssets != NULL)
696 : 2303768 : FREE(d->ssets);
697 [ + - ]: 2303768 : if (d->statesarea != NULL)
698 : 2303768 : FREE(d->statesarea);
699 [ + - ]: 2303768 : if (d->outsarea != NULL)
700 : 2303768 : FREE(d->outsarea);
701 [ + - ]: 2303768 : if (d->incarea != NULL)
702 : 2303768 : FREE(d->incarea);
703 : : }
704 : :
1658 705 [ + + ]: 4501884 : if (d->ismalloced)
706 : 2315493 : FREE(d);
8249 707 : 4501884 : }
708 : :
709 : : /*
710 : : * hash - construct a hash code for a bitvector
711 : : *
712 : : * There are probably better ways, but they're more expensive.
713 : : */
714 : : static unsigned
715 : 16332 : hash(unsigned *uv,
716 : : int n)
717 : : {
718 : : int i;
719 : : unsigned h;
720 : :
721 : 16332 : h = 0;
722 [ + + ]: 75804 : for (i = 0; i < n; i++)
723 : 59472 : h ^= uv[i];
724 : 16332 : return h;
725 : : }
726 : :
727 : : /*
728 : : * initialize - hand-craft a cache entry for startup, otherwise get ready
729 : : */
730 : : static struct sset *
2999 731 : 4503720 : initialize(struct vars *v,
732 : : struct dfa *d,
733 : : chr *start)
734 : : {
735 : : struct sset *ss;
736 : : int i;
737 : :
738 : : /* is previous one still there? */
8069 bruce@momjian.us 739 [ + + + - ]: 4503720 : if (d->nssused > 0 && (d->ssets[0].flags & STARTER))
8249 tgl@sss.pgh.pa.us 740 : 3164 : ss = &d->ssets[0];
741 : : else
742 : : { /* no, must (re)build it */
743 : 4500556 : ss = getvacant(v, d, start, start);
3627 744 [ - + ]: 4500556 : if (ss == NULL)
3627 tgl@sss.pgh.pa.us 745 :UBC 0 : return NULL;
8249 tgl@sss.pgh.pa.us 746 [ + + ]:CBC 9002457 : for (i = 0; i < d->wordsper; i++)
747 : 4501901 : ss->states[i] = 0;
748 : 4500556 : BSET(ss->states, d->cnfa->pre);
749 [ + + ]: 4500556 : ss->hash = HASH(ss->states, d->wordsper);
750 [ - + ]: 4500556 : assert(d->cnfa->pre != d->cnfa->post);
8069 bruce@momjian.us 751 : 4500556 : ss->flags = STARTER | LOCKED | NOPROGRESS;
752 : : /* lastseen dealt with below */
753 : : }
754 : :
8249 tgl@sss.pgh.pa.us 755 [ + + ]: 9045352 : for (i = 0; i < d->nssused; i++)
756 : 4541632 : d->ssets[i].lastseen = NULL;
757 : 4503720 : ss->lastseen = start; /* maybe untrue, but harmless */
758 : 4503720 : d->lastpost = NULL;
759 : 4503720 : d->lastnopr = NULL;
760 : 4503720 : return ss;
761 : : }
762 : :
763 : : /*
764 : : * miss - handle a stateset cache miss
765 : : *
766 : : * css is the current stateset, co is the color of the current input character,
767 : : * cp points to the character after that (which is where we may need to test
768 : : * LACONs). start does not affect matching behavior but is needed for pickss'
769 : : * heuristics about which stateset cache entry to replace.
770 : : *
771 : : * Ordinarily, returns the address of the next stateset (the one that is
772 : : * valid after consuming the input character). Returns NULL if no valid
773 : : * NFA states remain, ie we have a certain match failure.
774 : : * Internal errors also return NULL, with v->err set.
775 : : */
776 : : static struct sset *
2999 777 : 13188046 : miss(struct vars *v,
778 : : struct dfa *d,
779 : : struct sset *css,
780 : : color co,
781 : : chr *cp, /* next chr */
782 : : chr *start) /* where the attempt got started */
783 : : {
8249 784 : 13188046 : struct cnfa *cnfa = d->cnfa;
785 : : int i;
786 : : unsigned h;
787 : : struct carc *ca;
788 : : struct sset *p;
789 : : int ispseudocolor;
790 : : int ispost;
791 : : int noprogress;
792 : : int gotstate;
793 : : int dolacons;
794 : : int sawlacons;
795 : :
796 : : /* for convenience, we can be called even if it might not be a miss */
8069 bruce@momjian.us 797 [ + + ]: 13188046 : if (css->outs[co] != NULL)
798 : : {
799 : : FDEBUG(("hit\n"));
8249 tgl@sss.pgh.pa.us 800 : 2356 : return css->outs[co];
801 : : }
802 : : FDEBUG(("miss\n"));
803 : :
804 : : /*
805 : : * Checking for operation cancel in the inner text search loop seems
806 : : * unduly expensive. As a compromise, check during cache misses.
807 : : */
882 tmunro@postgresql.or 808 [ + + ]: 13185690 : INTERRUPT(v->re);
809 : :
810 : : /*
811 : : * What set of states would we end up in after consuming the co character?
812 : : * We first consider PLAIN arcs that consume the character, and then look
813 : : * to see what LACON arcs could be traversed after consuming it.
814 : : */
8249 tgl@sss.pgh.pa.us 815 [ + + ]: 26416033 : for (i = 0; i < d->wordsper; i++)
3627 816 : 13230343 : d->work[i] = 0; /* build new stateset bitmap in d->work */
1659 817 : 13185690 : ispseudocolor = d->cm->cd[co].flags & PSEUDO;
8249 818 : 13185690 : ispost = 0;
819 : 13185690 : noprogress = 1;
820 : 13185690 : gotstate = 0;
821 [ + + ]: 155368180 : for (i = 0; i < d->nstates; i++)
822 [ + + ]: 142182490 : if (ISBSET(css->states, i))
4809 823 [ + + ]: 58996147 : for (ca = cnfa->states[i]; ca->co != COLORLESS; ca++)
1659 824 [ + + ]: 41016587 : if (ca->co == co ||
825 [ + + + + ]: 34470848 : (ca->co == RAINBOW && !ispseudocolor))
826 : : {
8249 827 : 15358272 : BSET(d->work, ca->to);
828 : 15358272 : gotstate = 1;
829 [ + + ]: 15358272 : if (ca->to == cnfa->post)
830 : 1056400 : ispost = 1;
4809 831 [ + + ]: 15358272 : if (!(cnfa->stflags[ca->to] & CNFA_NOPROGRESS))
8249 832 : 4342653 : noprogress = 0;
833 : : FDEBUG(("%d -> %d\n", i, ca->to));
834 : : }
3627 835 [ + + ]: 13185690 : if (!gotstate)
836 : 3916199 : return NULL; /* character cannot reach any new state */
837 : 9269491 : dolacons = (cnfa->flags & HASLACONS);
8249 838 : 9269491 : sawlacons = 0;
839 : : /* outer loop handles transitive closure of reachable-by-LACON states */
8069 bruce@momjian.us 840 [ + + ]: 9270296 : while (dolacons)
841 : : {
8249 tgl@sss.pgh.pa.us 842 : 805 : dolacons = 0;
843 [ + + ]: 7376 : for (i = 0; i < d->nstates; i++)
844 [ + + ]: 6571 : if (ISBSET(d->work, i))
4809 845 [ + + ]: 2606 : for (ca = cnfa->states[i]; ca->co != COLORLESS; ca++)
846 : : {
847 [ + + ]: 1563 : if (ca->co < cnfa->ncolors)
2999 848 : 1377 : continue; /* not a LACON arc */
8249 849 [ + + ]: 186 : if (ISBSET(d->work, ca->to))
2999 850 : 66 : continue; /* arc would be a no-op anyway */
851 : 120 : sawlacons = 1; /* this LACON affects our result */
8249 852 [ + + ]: 120 : if (!lacon(v, cnfa, cp, ca->co))
853 : : {
3627 854 [ - + ]: 63 : if (ISERR())
3627 tgl@sss.pgh.pa.us 855 :UBC 0 : return NULL;
2999 tgl@sss.pgh.pa.us 856 :CBC 63 : continue; /* LACON arc cannot be traversed */
857 : : }
3627 858 [ - + ]: 57 : if (ISERR())
3627 tgl@sss.pgh.pa.us 859 :UBC 0 : return NULL;
8249 tgl@sss.pgh.pa.us 860 :CBC 57 : BSET(d->work, ca->to);
861 : 57 : dolacons = 1;
862 [ - + ]: 57 : if (ca->to == cnfa->post)
8249 tgl@sss.pgh.pa.us 863 :UBC 0 : ispost = 1;
4809 tgl@sss.pgh.pa.us 864 [ + - ]:CBC 57 : if (!(cnfa->stflags[ca->to] & CNFA_NOPROGRESS))
8249 865 : 57 : noprogress = 0;
866 : : FDEBUG(("%d :> %d\n", i, ca->to));
867 : : }
868 : : }
869 [ + + ]: 9269491 : h = HASH(d->work, d->wordsper);
870 : :
871 : : /* Is this stateset already in the cache? */
872 [ + + ]: 28370480 : for (p = d->ssets, i = d->nssused; i > 0; p++, i--)
8069 bruce@momjian.us 873 [ + + + + : 20399952 : if (HIT(h, d->work, p, d->wordsper))
+ + ]
874 : : {
875 : : FDEBUG(("cached c%d\n", (int) (p - d->ssets)));
876 : : break; /* NOTE BREAK OUT */
877 : : }
878 [ + + ]: 9269491 : if (i == 0)
879 : : { /* nope, need a new cache entry */
8249 tgl@sss.pgh.pa.us 880 : 7970528 : p = getvacant(v, d, cp, start);
3627 881 [ - + ]: 7970528 : if (p == NULL)
3627 tgl@sss.pgh.pa.us 882 :UBC 0 : return NULL;
8249 tgl@sss.pgh.pa.us 883 [ - + ]:CBC 7970528 : assert(p != css);
884 [ + + ]: 15977693 : for (i = 0; i < d->wordsper; i++)
885 : 8007165 : p->states[i] = d->work[i];
886 : 7970528 : p->hash = h;
887 [ + + ]: 7970528 : p->flags = (ispost) ? POSTSTATE : 0;
888 [ + + ]: 7970528 : if (noprogress)
889 : 4502701 : p->flags |= NOPROGRESS;
890 : : /* lastseen to be dealt with by caller */
891 : : }
892 : :
893 : : /*
894 : : * Link new stateset to old, unless a LACON affected the result, in which
895 : : * case we don't create the link. That forces future transitions across
896 : : * this same arc (same prior stateset and character color) to come through
897 : : * miss() again, so that we can recheck the LACON(s), which might or might
898 : : * not pass since context will be different.
899 : : */
8069 bruce@momjian.us 900 [ + + ]: 9269491 : if (!sawlacons)
901 : : {
902 : : FDEBUG(("c%d[%d]->c%d\n",
903 : : (int) (css - d->ssets), co, (int) (p - d->ssets)));
8249 tgl@sss.pgh.pa.us 904 : 9269403 : css->outs[co] = p;
905 : 9269403 : css->inchain[co] = p->ins;
906 : 9269403 : p->ins.ss = css;
3305 907 : 9269403 : p->ins.co = co;
908 : : }
8249 909 : 9269491 : return p;
910 : : }
911 : :
912 : : /*
913 : : * lacon - lookaround-constraint checker for miss()
914 : : */
915 : : static int /* predicate: constraint satisfied? */
2999 916 : 120 : lacon(struct vars *v,
917 : : struct cnfa *pcnfa, /* parent cnfa */
918 : : chr *cp,
919 : : color co) /* "color" of the lookaround constraint */
920 : : {
921 : : int n;
922 : : struct subre *sub;
923 : : struct dfa *d;
924 : : chr *end;
925 : : int satisfied;
926 : :
927 : : /* Since this is recursive, it could be driven to stack overflow */
3627 928 [ - + ]: 120 : if (STACK_TOO_DEEP(v->re))
929 : : {
3627 tgl@sss.pgh.pa.us 930 [ # # ]:UBC 0 : ERR(REG_ETOOBIG);
931 : 0 : return 0;
932 : : }
933 : :
8249 tgl@sss.pgh.pa.us 934 :CBC 120 : n = co - pcnfa->ncolors;
3599 935 [ + - + - : 120 : assert(n > 0 && n < v->g->nlacons && v->g->lacons != NULL);
- + ]
936 : : FDEBUG(("=== testing lacon %d\n", n));
8249 937 : 120 : sub = &v->g->lacons[n];
3599 938 : 120 : d = getladfa(v, n);
8069 bruce@momjian.us 939 [ - + ]: 120 : if (d == NULL)
8249 tgl@sss.pgh.pa.us 940 :UBC 0 : return 0;
1659 tgl@sss.pgh.pa.us 941 [ + + ]:CBC 120 : if (LATYPE_IS_AHEAD(sub->latype))
942 : : {
943 : : /* used to use longest() here, but shortest() could be much cheaper */
3599 944 : 60 : end = shortest(v, d, cp, cp, v->stop,
945 : : (chr **) NULL, (int *) NULL);
1659 946 [ + + ]: 60 : satisfied = LATYPE_IS_POS(sub->latype) ? (end != NULL) : (end == NULL);
947 : : }
948 : : else
949 : : {
950 : : /*
951 : : * To avoid doing O(N^2) work when repeatedly testing a lookbehind
952 : : * constraint in an N-character string, we use matchuntil() which can
953 : : * cache the DFA state across calls. We only need to restart if the
954 : : * probe point decreases, which is not common. The NFA we're using is
955 : : * a search NFA, so it doesn't mind scanning over stuff before the
956 : : * nominal match.
957 : : */
3599 958 : 60 : satisfied = matchuntil(v, d, cp, &v->lblastcss[n], &v->lblastcp[n]);
1659 959 [ - + ]: 60 : if (!LATYPE_IS_POS(sub->latype))
3599 tgl@sss.pgh.pa.us 960 :UBC 0 : satisfied = !satisfied;
961 : : }
962 : : FDEBUG(("=== lacon %d satisfied %d\n", n, satisfied));
3599 tgl@sss.pgh.pa.us 963 :CBC 120 : return satisfied;
964 : : }
965 : :
966 : : /*
967 : : * getvacant - get a vacant state set
968 : : *
969 : : * This routine clears out the inarcs and outarcs, but does not otherwise
970 : : * clear the innards of the state set -- that's up to the caller.
971 : : */
972 : : static struct sset *
2999 973 : 12471084 : getvacant(struct vars *v,
974 : : struct dfa *d,
975 : : chr *cp,
976 : : chr *start)
977 : : {
978 : : int i;
979 : : struct sset *ss;
980 : : struct sset *p;
981 : : struct arcp ap;
982 : : color co;
983 : :
8249 984 : 12471084 : ss = pickss(v, d, cp, start);
3627 985 [ - + ]: 12471084 : if (ss == NULL)
3627 tgl@sss.pgh.pa.us 986 :UBC 0 : return NULL;
8069 bruce@momjian.us 987 [ - + ]:CBC 12471084 : assert(!(ss->flags & LOCKED));
988 : :
989 : : /* clear out its inarcs, including self-referential ones */
8249 tgl@sss.pgh.pa.us 990 : 12471084 : ap = ss->ins;
8069 bruce@momjian.us 991 [ + + ]: 12471096 : while ((p = ap.ss) != NULL)
992 : : {
8249 tgl@sss.pgh.pa.us 993 : 12 : co = ap.co;
994 : : FDEBUG(("zapping c%d's %ld outarc\n", (int) (p - d->ssets), (long) co));
995 : 12 : p->outs[co] = NULL;
996 : 12 : ap = p->inchain[co];
2999 997 : 12 : p->inchain[co].ss = NULL; /* paranoia */
998 : : }
8249 999 : 12471084 : ss->ins.ss = NULL;
1000 : :
1001 : : /* take it off the inarc chains of the ssets reached by its outarcs */
8069 bruce@momjian.us 1002 [ + + ]: 146904392 : for (i = 0; i < d->ncolors; i++)
1003 : : {
8249 tgl@sss.pgh.pa.us 1004 : 134433308 : p = ss->outs[i];
1005 [ - + ]: 134433308 : assert(p != ss); /* not self-referential */
1006 [ + + ]: 134433308 : if (p == NULL)
8069 bruce@momjian.us 1007 : 134433242 : continue; /* NOTE CONTINUE */
1008 : : FDEBUG(("del outarc %d from c%d's in chn\n", i, (int) (p - d->ssets)));
8249 tgl@sss.pgh.pa.us 1009 [ + + + - ]: 66 : if (p->ins.ss == ss && p->ins.co == i)
1010 : 60 : p->ins = ss->inchain[i];
1011 : : else
1012 : : {
7287 1013 : 6 : struct arcp lastap = {NULL, 0};
1014 : :
8249 1015 [ - + ]: 6 : assert(p->ins.ss != NULL);
1016 [ + - ]: 12 : for (ap = p->ins; ap.ss != NULL &&
8069 bruce@momjian.us 1017 [ + + - + ]: 12 : !(ap.ss == ss && ap.co == i);
1018 : 6 : ap = ap.ss->inchain[ap.co])
8249 tgl@sss.pgh.pa.us 1019 : 6 : lastap = ap;
1020 [ - + ]: 6 : assert(ap.ss != NULL);
1021 : 6 : lastap.ss->inchain[lastap.co] = ss->inchain[i];
1022 : : }
1023 : 66 : ss->outs[i] = NULL;
1024 : 66 : ss->inchain[i].ss = NULL;
1025 : : }
1026 : :
1027 : : /* if ss was a success state, may need to remember location */
8069 bruce@momjian.us 1028 [ + + + - ]: 12471084 : if ((ss->flags & POSTSTATE) && ss->lastseen != d->lastpost &&
1029 [ + + + - ]: 18 : (d->lastpost == NULL || d->lastpost < ss->lastseen))
8249 tgl@sss.pgh.pa.us 1030 : 18 : d->lastpost = ss->lastseen;
1031 : :
1032 : : /* likewise for a no-progress state */
8069 bruce@momjian.us 1033 [ + + + - ]: 12471084 : if ((ss->flags & NOPROGRESS) && ss->lastseen != d->lastnopr &&
1034 [ - + - - ]: 6 : (d->lastnopr == NULL || d->lastnopr < ss->lastseen))
8249 tgl@sss.pgh.pa.us 1035 : 6 : d->lastnopr = ss->lastseen;
1036 : :
1037 : 12471084 : return ss;
1038 : : }
1039 : :
1040 : : /*
1041 : : * pickss - pick the next stateset to be used
1042 : : */
1043 : : static struct sset *
2999 1044 : 12471084 : pickss(struct vars *v,
1045 : : struct dfa *d,
1046 : : chr *cp,
1047 : : chr *start)
1048 : : {
1049 : : int i;
1050 : : struct sset *ss;
1051 : : struct sset *end;
1052 : : chr *ancient;
1053 : :
1054 : : /* shortcut for cases where cache isn't full */
8069 bruce@momjian.us 1055 [ + + ]: 12471084 : if (d->nssused < d->nssets)
1056 : : {
8249 tgl@sss.pgh.pa.us 1057 : 12471018 : i = d->nssused;
1058 : 12471018 : d->nssused++;
1059 : 12471018 : ss = &d->ssets[i];
1060 : : FDEBUG(("new c%d\n", i));
1061 : : /* set up innards */
1062 : 12471018 : ss->states = &d->statesarea[i * d->wordsper];
1063 : 12471018 : ss->flags = 0;
1064 : 12471018 : ss->ins.ss = NULL;
1065 : 12471018 : ss->ins.co = WHITE; /* give it some value */
1066 : 12471018 : ss->outs = &d->outsarea[i * d->ncolors];
1067 : 12471018 : ss->inchain = &d->incarea[i * d->ncolors];
8069 bruce@momjian.us 1068 [ + + ]: 146902790 : for (i = 0; i < d->ncolors; i++)
1069 : : {
8249 tgl@sss.pgh.pa.us 1070 : 134431772 : ss->outs[i] = NULL;
1071 : 134431772 : ss->inchain[i].ss = NULL;
1072 : : }
1073 : 12471018 : return ss;
1074 : : }
1075 : :
1076 : : /* look for oldest, or old enough anyway */
8069 bruce@momjian.us 1077 [ + - ]: 66 : if (cp - start > d->nssets * 2 / 3) /* oldest 33% are expendable */
1078 : 66 : ancient = cp - d->nssets * 2 / 3;
1079 : : else
8249 tgl@sss.pgh.pa.us 1080 :UBC 0 : ancient = start;
8249 tgl@sss.pgh.pa.us 1081 [ + + ]:CBC 72 : for (ss = d->search, end = &d->ssets[d->nssets]; ss < end; ss++)
1082 [ + - + - ]: 66 : if ((ss->lastseen == NULL || ss->lastseen < ancient) &&
8069 bruce@momjian.us 1083 [ + + ]: 66 : !(ss->flags & LOCKED))
1084 : : {
8249 tgl@sss.pgh.pa.us 1085 : 60 : d->search = ss + 1;
1086 : : FDEBUG(("replacing c%d\n", (int) (ss - d->ssets)));
1087 : 60 : return ss;
1088 : : }
1089 [ + - ]: 12 : for (ss = d->ssets, end = d->search; ss < end; ss++)
1090 [ + - + - ]: 12 : if ((ss->lastseen == NULL || ss->lastseen < ancient) &&
8069 bruce@momjian.us 1091 [ + + ]: 12 : !(ss->flags & LOCKED))
1092 : : {
8249 tgl@sss.pgh.pa.us 1093 : 6 : d->search = ss + 1;
1094 : : FDEBUG(("replacing c%d\n", (int) (ss - d->ssets)));
1095 : 6 : return ss;
1096 : : }
1097 : :
1098 : : /* nobody's old enough?!? -- something's really wrong */
1099 : : FDEBUG(("cannot find victim to replace!\n"));
8249 tgl@sss.pgh.pa.us 1100 [ # # ]:UBC 0 : ERR(REG_ASSERT);
3627 1101 : 0 : return NULL;
1102 : : }
|