[perl #22395] regexp /(.*)[bc]/ 10000 times slower in 5.8.0 vs 5.6.1
[p5sagit/p5-mst-13.2.git] / regexp.h
1 /*    regexp.h
2  *
3  *    Copyright (C) 1993, 1994, 1996, 1997, 1999, 2000, 2001, 2003,
4  *    by Larry Wall and others
5  *
6  *    You may distribute under the terms of either the GNU General Public
7  *    License or the Artistic License, as specified in the README file.
8  *
9  */
10
11 /*
12  * Definitions etc. for regexp(3) routines.
13  *
14  * Caveat:  this is V8 regexp(3) [actually, a reimplementation thereof],
15  * not the System V one.
16  */
17 #ifndef PLUGGABLE_RE_EXTENSION
18 /* we don't want to include this stuff if we are inside Nicholas'
19  * pluggable regex engine code */
20
21 struct regnode {
22     U8  flags;
23     U8  type;
24     U16 next_off;
25 };
26
27 typedef struct regnode regnode;
28
29 struct reg_substr_data;
30
31 struct reg_data;
32
33 struct regexp_engine;
34 typedef struct regexp_paren_ofs {
35     I32 *startp;
36     I32 *endp;
37 } regexp_paren_ofs;
38
39 typedef struct regexp {
40         I32 *startp;
41         I32 *endp;
42         regexp_paren_ofs *swap;
43         regnode *regstclass;
44         struct reg_substr_data *substrs;
45         char *precomp;          /* pre-compilation regular expression */
46         struct reg_data *data;  /* Additional data. */
47         char *subbeg;           /* saved or original string 
48                                    so \digit works forever. */
49 #ifdef PERL_OLD_COPY_ON_WRITE
50         SV *saved_copy;         /* If non-NULL, SV which is COW from original */
51 #endif
52         U32 *offsets;           /* offset annotations 20001228 MJD */
53         I32 sublen;             /* Length of string pointed by subbeg */
54         I32 refcnt;
55         I32 minlen;             /* mininum possible length of string to match */
56         I32 minlenret;          /* mininum possible length of $& */
57         I32 prelen;             /* length of precomp */
58         U32 nparens;            /* number of parentheses */
59         U32 lastparen;          /* last paren matched */
60         U32 lastcloseparen;     /* last paren matched */
61         U32 reganch;            /* Internal use only +
62                                    Tainted information used by regexec? */
63         HV *paren_names;        /* Paren names */
64         const struct regexp_engine* engine;
65         regnode program[1];     /* Unwarranted chumminess with compiler. */
66 } regexp;
67
68
69 typedef struct re_scream_pos_data_s
70 {
71     char **scream_olds;         /* match pos */
72     I32 *scream_pos;            /* Internal iterator of scream. */
73 } re_scream_pos_data;
74
75 typedef struct regexp_engine {
76     regexp* (*comp) (pTHX_ char* exp, char* xend, PMOP* pm);
77     I32     (*exec) (pTHX_ regexp* prog, char* stringarg, char* strend,
78                             char* strbeg, I32 minend, SV* screamer,
79                             void* data, U32 flags);
80     char*   (*intuit) (pTHX_ regexp *prog, SV *sv, char *strpos,
81                             char *strend, U32 flags,
82                             struct re_scream_pos_data_s *data);
83     SV*     (*checkstr) (pTHX_ regexp *prog);
84     void    (*free) (pTHX_ struct regexp* r);
85     char*   (*as_str)   (pTHX_ MAGIC *mg, STRLEN *lp, U32 *flags,  I32 *haseval);
86 #ifdef USE_ITHREADS
87     regexp* (*dupe) (pTHX_ const regexp *r, CLONE_PARAMS *param);
88 #endif    
89 } regexp_engine;
90
91 #define ROPT_ANCH               (ROPT_ANCH_BOL|ROPT_ANCH_MBOL|ROPT_ANCH_GPOS|ROPT_ANCH_SBOL)
92 #define ROPT_ANCH_SINGLE        (ROPT_ANCH_SBOL|ROPT_ANCH_GPOS)
93 #define ROPT_ANCH_BOL           0x00000001
94 #define ROPT_ANCH_MBOL          0x00000002
95 #define ROPT_ANCH_SBOL          0x00000004
96 #define ROPT_ANCH_GPOS          0x00000008
97 #define ROPT_SKIP               0x00000010
98 #define ROPT_IMPLICIT           0x00000020      /* Converted .* to ^.* */
99 #define ROPT_NOSCAN             0x00000040      /* Check-string always at start. */
100 #define ROPT_GPOS_SEEN          0x00000080
101 #define ROPT_CHECK_ALL          0x00000100
102 #define ROPT_LOOKBEHIND_SEEN    0x00000200
103 #define ROPT_EVAL_SEEN          0x00000400
104 #define ROPT_CANY_SEEN          0x00000800
105 #define ROPT_SANY_SEEN          ROPT_CANY_SEEN /* src bckwrd cmpt */
106 #define ROPT_GPOS_CHECK         (ROPT_GPOS_SEEN|ROPT_ANCH_GPOS)
107
108 /* 0xF800 of reganch is used by PMf_COMPILETIME */
109
110 #define ROPT_UTF8               0x00010000
111 #define ROPT_NAUGHTY            0x00020000 /* how exponential is this pattern? */
112 #define ROPT_COPY_DONE          0x00040000      /* subbeg is a copy of the string */
113 #define ROPT_TAINTED_SEEN       0x00080000
114 #define ROPT_MATCH_UTF8         0x10000000 /* subbeg is utf-8 */
115 #define ROPT_VERBARG_SEEN       0x20000000
116 #define ROPT_CUTGROUP_SEEN      0x40000000
117
118 #define RE_USE_INTUIT_NOML      0x00100000 /* Best to intuit before matching */
119 #define RE_USE_INTUIT_ML        0x00200000
120 #define REINT_AUTORITATIVE_NOML 0x00400000 /* Can trust a positive answer */
121 #define REINT_AUTORITATIVE_ML   0x00800000
122 #define REINT_ONCE_NOML         0x01000000 /* Intuit can succed once only. */
123 #define REINT_ONCE_ML           0x02000000
124 #define RE_INTUIT_ONECHAR       0x04000000
125 #define RE_INTUIT_TAIL          0x08000000
126
127
128 #define RE_USE_INTUIT           (RE_USE_INTUIT_NOML|RE_USE_INTUIT_ML)
129 #define REINT_AUTORITATIVE      (REINT_AUTORITATIVE_NOML|REINT_AUTORITATIVE_ML)
130 #define REINT_ONCE              (REINT_ONCE_NOML|REINT_ONCE_ML)
131
132 #define RX_HAS_CUTGROUP(prog) ((prog)->reganch & ROPT_CUTGROUP_SEEN)
133 #define RX_MATCH_TAINTED(prog)  ((prog)->reganch & ROPT_TAINTED_SEEN)
134 #define RX_MATCH_TAINTED_on(prog) ((prog)->reganch |= ROPT_TAINTED_SEEN)
135 #define RX_MATCH_TAINTED_off(prog) ((prog)->reganch &= ~ROPT_TAINTED_SEEN)
136 #define RX_MATCH_TAINTED_set(prog, t) ((t) \
137                                        ? RX_MATCH_TAINTED_on(prog) \
138                                        : RX_MATCH_TAINTED_off(prog))
139
140 #define RX_MATCH_COPIED(prog)           ((prog)->reganch & ROPT_COPY_DONE)
141 #define RX_MATCH_COPIED_on(prog)        ((prog)->reganch |= ROPT_COPY_DONE)
142 #define RX_MATCH_COPIED_off(prog)       ((prog)->reganch &= ~ROPT_COPY_DONE)
143 #define RX_MATCH_COPIED_set(prog,t)     ((t) \
144                                          ? RX_MATCH_COPIED_on(prog) \
145                                          : RX_MATCH_COPIED_off(prog))
146
147 #endif /* PLUGGABLE_RE_EXTENSION */
148
149 /* Stuff that needs to be included in the plugable extension goes below here */
150
151 #ifdef PERL_OLD_COPY_ON_WRITE
152 #define RX_MATCH_COPY_FREE(rx) \
153         STMT_START {if (rx->saved_copy) { \
154             SV_CHECK_THINKFIRST_COW_DROP(rx->saved_copy); \
155         } \
156         if (RX_MATCH_COPIED(rx)) { \
157             Safefree(rx->subbeg); \
158             RX_MATCH_COPIED_off(rx); \
159         }} STMT_END
160 #else
161 #define RX_MATCH_COPY_FREE(rx) \
162         STMT_START {if (RX_MATCH_COPIED(rx)) { \
163             Safefree(rx->subbeg); \
164             RX_MATCH_COPIED_off(rx); \
165         }} STMT_END
166 #endif
167
168 #define RX_MATCH_UTF8(prog)             ((prog)->reganch & ROPT_MATCH_UTF8)
169 #define RX_MATCH_UTF8_on(prog)          ((prog)->reganch |= ROPT_MATCH_UTF8)
170 #define RX_MATCH_UTF8_off(prog)         ((prog)->reganch &= ~ROPT_MATCH_UTF8)
171 #define RX_MATCH_UTF8_set(prog, t)      ((t) \
172                         ? (RX_MATCH_UTF8_on(prog), (PL_reg_match_utf8 = 1)) \
173                         : (RX_MATCH_UTF8_off(prog), (PL_reg_match_utf8 = 0)))
174     
175 #define REXEC_COPY_STR  0x01            /* Need to copy the string. */
176 #define REXEC_CHECKED   0x02            /* check_substr already checked. */
177 #define REXEC_SCREAM    0x04            /* use scream table. */
178 #define REXEC_IGNOREPOS 0x08            /* \G matches at start. */
179 #define REXEC_NOT_FIRST 0x10            /* This is another iteration of //g. */
180
181 #define ReREFCNT_inc(re) ((void)(re && re->refcnt++), re)
182 #define ReREFCNT_dec(re) CALLREGFREE(re)
183
184 #define FBMcf_TAIL_DOLLAR       1
185 #define FBMcf_TAIL_DOLLARM      2
186 #define FBMcf_TAIL_Z            4
187 #define FBMcf_TAIL_z            8
188 #define FBMcf_TAIL              (FBMcf_TAIL_DOLLAR|FBMcf_TAIL_DOLLARM|FBMcf_TAIL_Z|FBMcf_TAIL_z)
189
190 #define FBMrf_MULTILINE 1
191
192 /* an accepting state/position*/
193 struct _reg_trie_accepted {
194     U8   *endpos;
195     U16  wordnum;
196 };
197 typedef struct _reg_trie_accepted reg_trie_accepted;
198
199 /* some basic information about the current match that is created by
200  * Perl_regexec_flags and then passed to regtry(), regmatch() etc */
201
202 typedef struct {
203     regexp *prog;
204     char *bol;
205     char *till;
206     SV *sv;
207     char *ganch;
208     char *cutpoint;
209 } regmatch_info;
210  
211
212 /* structures for holding and saving the state maintained by regmatch() */
213
214 #define MAX_RECURSE_EVAL_NOCHANGE_DEPTH 50
215
216 typedef I32 CHECKPOINT;
217
218 typedef struct regmatch_state {
219     int resume_state;           /* where to jump to on return */
220     char *locinput;             /* where to backtrack in string on failure */
221
222     union {
223
224         /* this is a fake union member that matches the first element
225          * of each member that needs to store positive backtrack
226          * information */
227         struct {
228             struct regmatch_state *prev_yes_state;
229         } yes;
230
231         struct {
232             /* this first element must match u.yes */
233             struct regmatch_state *prev_yes_state;
234             reg_trie_accepted *accept_buff;
235             U32         accepted; /* how many accepting states we have seen */
236             U16         *jump;  /* positive offsets from me */
237             regnode     *B;     /* node following the trie */
238             regnode     *me;    /* Which node am I - needed for jump tries*/
239         } trie;
240
241         struct {
242             /* this first element must match u.yes */
243             struct regmatch_state *prev_yes_state;
244             struct regmatch_state *prev_eval;
245             struct regmatch_state *prev_curlyx;
246             regexp      *prev_rex;
247             U32         toggle_reg_flags; /* what bits in PL_reg_flags to
248                                             flip when transitioning between
249                                             inner and outer rexen */
250             CHECKPOINT  cp;     /* remember current savestack indexes */
251             CHECKPOINT  lastcp;
252             regnode     *B;     /* the node following us  */
253             U32        close_paren; /* which close bracket is our end */
254         } eval;
255
256         struct {
257             /* this first element must match u.yes */
258             struct regmatch_state *prev_yes_state;
259             struct regmatch_state *prev_curlyx; /* previous cur_curlyx */
260             CHECKPOINT  cp;     /* remember current savestack index */
261             bool        minmod;
262             int         parenfloor;/* how far back to strip paren data */
263             int         min;    /* the minimal number of A's to match */
264             int         max;    /* the maximal number of A's to match */
265             regnode     *A, *B; /* the nodes corresponding to /A*B/  */
266
267             /* these two are modified by WHILEM */
268             int         count;  /* how many instances of A we've matched */
269             char        *lastloc;/* where previous A matched (0-len detect) */
270         } curlyx;
271
272         struct {
273             /* this first element must match u.yes */
274             struct regmatch_state *prev_yes_state;
275             struct regmatch_state *save_curlyx;
276             CHECKPOINT  cp;     /* remember current savestack indexes */
277             CHECKPOINT  lastcp;
278             char        *save_lastloc;  /* previous curlyx.lastloc */
279             I32         cache_offset;
280             I32         cache_mask;
281         } whilem;
282
283         struct {
284             /* this first element must match u.yes */
285             struct regmatch_state *prev_yes_state;
286             U32 lastparen;
287             regnode *next_branch; /* next branch node */
288             CHECKPOINT cp;
289         } branch;
290
291         struct {
292             /* this first element must match u.yes */
293             struct regmatch_state *prev_yes_state;
294             I32 c1, c2;         /* case fold search */
295             CHECKPOINT cp;
296             I32 alen;           /* length of first-matched A string */
297             I32 count;
298             bool minmod;
299             regnode *A, *B;     /* the nodes corresponding to /A*B/  */
300             regnode *me;        /* the curlym node */
301         } curlym;
302
303         struct {
304             U32 paren;
305             CHECKPOINT cp;
306             I32 c1, c2;         /* case fold search */
307             char *maxpos;       /* highest possible point in string to match */
308             char *oldloc;       /* the previous locinput */
309             int count;
310             int min, max;       /* {m,n} */
311             regnode *A, *B;     /* the nodes corresponding to /A*B/  */
312         } curly; /* and CURLYN/PLUS/STAR */
313
314         struct {
315             /* this first element must match u.yes */
316             struct regmatch_state *prev_yes_state;
317             I32 wanted;
318             I32 logical;        /* saved copy of 'logical' var */
319             regnode  *me; /* the IFMATCH/SUSPEND/UNLESSM node  */
320         } ifmatch; /* and SUSPEND/UNLESSM */
321         
322         struct {
323             /* this first element must match u.yes */
324             struct regmatch_state *prev_yes_state;
325             struct regmatch_state *prev_mark;
326             SV* mark_name;
327             char *mark_loc;
328         } mark;
329     } u;
330 } regmatch_state;
331
332 /* how many regmatch_state structs to allocate as a single slab.
333  * We do it in 4K blocks for efficiency. The "3" is 2 for the next/prev
334  * pointers, plus 1 for any mythical malloc overhead. */
335  
336 #define PERL_REGMATCH_SLAB_SLOTS \
337     ((4096 - 3 * sizeof (void*)) / sizeof(regmatch_state))
338
339 typedef struct regmatch_slab {
340     regmatch_state states[PERL_REGMATCH_SLAB_SLOTS];
341     struct regmatch_slab *prev, *next;
342 } regmatch_slab;
343
344 #define PL_reg_flags            PL_reg_state.re_state_reg_flags
345 #define PL_bostr                PL_reg_state.re_state_bostr
346 #define PL_reginput             PL_reg_state.re_state_reginput
347 #define PL_regeol               PL_reg_state.re_state_regeol
348 #define PL_regstartp            PL_reg_state.re_state_regstartp
349 #define PL_regendp              PL_reg_state.re_state_regendp
350 #define PL_reglastparen         PL_reg_state.re_state_reglastparen
351 #define PL_reglastcloseparen    PL_reg_state.re_state_reglastcloseparen
352 #define PL_reg_start_tmp        PL_reg_state.re_state_reg_start_tmp
353 #define PL_reg_start_tmpl       PL_reg_state.re_state_reg_start_tmpl
354 #define PL_reg_eval_set         PL_reg_state.re_state_reg_eval_set
355 #define PL_reg_match_utf8       PL_reg_state.re_state_reg_match_utf8
356 #define PL_reg_magic            PL_reg_state.re_state_reg_magic
357 #define PL_reg_oldpos           PL_reg_state.re_state_reg_oldpos
358 #define PL_reg_oldcurpm         PL_reg_state.re_state_reg_oldcurpm
359 #define PL_reg_curpm            PL_reg_state.re_state_reg_curpm
360 #define PL_reg_oldsaved         PL_reg_state.re_state_reg_oldsaved
361 #define PL_reg_oldsavedlen      PL_reg_state.re_state_reg_oldsavedlen
362 #define PL_reg_maxiter          PL_reg_state.re_state_reg_maxiter
363 #define PL_reg_leftiter         PL_reg_state.re_state_reg_leftiter
364 #define PL_reg_poscache         PL_reg_state.re_state_reg_poscache
365 #define PL_reg_poscache_size    PL_reg_state.re_state_reg_poscache_size
366 #define PL_regsize              PL_reg_state.re_state_regsize
367 #define PL_reg_starttry         PL_reg_state.re_state_reg_starttry
368 #define PL_nrs                  PL_reg_state.re_state_nrs
369
370 struct re_save_state {
371     U32 re_state_reg_flags;             /* from regexec.c */
372     char *re_state_bostr;
373     char *re_state_reginput;            /* String-input pointer. */
374     char *re_state_regeol;              /* End of input, for $ check. */
375     I32 *re_state_regstartp;            /* Pointer to startp array. */
376     I32 *re_state_regendp;              /* Ditto for endp. */
377     U32 *re_state_reglastparen;         /* Similarly for lastparen. */
378     U32 *re_state_reglastcloseparen;    /* Similarly for lastcloseparen. */
379     char **re_state_reg_start_tmp;      /* from regexec.c */
380     U32 re_state_reg_start_tmpl;        /* from regexec.c */
381     I32 re_state_reg_eval_set;          /* from regexec.c */
382     bool re_state_reg_match_utf8;       /* from regexec.c */
383     MAGIC *re_state_reg_magic;          /* from regexec.c */
384     I32 re_state_reg_oldpos;            /* from regexec.c */
385     PMOP *re_state_reg_oldcurpm;        /* from regexec.c */
386     PMOP *re_state_reg_curpm;           /* from regexec.c */
387     char *re_state_reg_oldsaved;        /* old saved substr during match */
388     STRLEN re_state_reg_oldsavedlen;    /* old length of saved substr during match */
389     I32 re_state_reg_maxiter;           /* max wait until caching pos */
390     I32 re_state_reg_leftiter;          /* wait until caching pos */
391     char *re_state_reg_poscache;        /* cache of pos of WHILEM */
392     STRLEN re_state_reg_poscache_size;  /* size of pos cache of WHILEM */
393     U32 re_state_regsize;               /* from regexec.c */
394     char *re_state_reg_starttry;        /* from regexec.c */
395 #ifdef PERL_OLD_COPY_ON_WRITE
396     SV *re_state_nrs;                   /* was placeholder: unused since 5.8.0 (5.7.2 patch #12027 for bug ID 20010815.012). Used to save rx->saved_copy */
397 #endif
398 };
399
400 #define SAVESTACK_ALLOC_FOR_RE_SAVE_STATE \
401         (1 + ((sizeof(struct re_save_state) - 1) / sizeof(*PL_savestack)))
402 /*
403  * Local variables:
404  * c-indentation-style: bsd
405  * c-basic-offset: 4
406  * indent-tabs-mode: t
407  * End:
408  *
409  * ex: set ts=8 sts=4 sw=4 noet:
410  */