This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
perl5380delta: a messy commit to get things started
[perl5.git] / regcomp.h
1 /*    regcomp.h
2  *
3  *    Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
4  *    2000, 2001, 2002, 2003, 2005, 2006, 2007, 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 #if ! defined(PERL_REGCOMP_H_) && (   defined(PERL_CORE)            \
12                                    || defined(PERL_EXT_RE_BUILD))
13
14 #define PERL_REGCOMP_H_
15
16 #ifndef RE_PESSIMISTIC_PARENS
17 /* Define this to 1 if you want to enable a really aggressive and
18  * inefficient paren cleanup during backtracking which should ensure
19  * correctness. Doing so should fix any bugs related to backreferences,
20  * at the cost of saving and restoring paren state far more than we
21  * necessarily must.
22  *
23  * When it is set to 0 we try to optimize away unnecessary save/restore
24  * operations which could potentially introduce bugs. We should pass our
25  * test suite with this as 0, but setting it to 1 might fix cases we do
26  * not currently test for. If setting this to 1 does fix a bug, then
27  * review the code related to storing and restoring paren state.
28  *
29  * See comment for VOLATILE_REF below for more details of a
30  * related case.
31  */
32 #define RE_PESSIMISTIC_PARENS 0
33 #endif
34
35 /* a VOLATILE_REF is a ref which is inside of a capturing group and it
36  * refers to the capturing group it is inside of or to a following capture
37  * group which might be affected by what this capture group matches, and
38  * thus the ref requires additional backtracking support. For example:
39  *
40  *  "xa=xaaa" =~ /^(xa|=?\1a){2}\z/
41  *
42  * should not match.  In older perls the matching process would go like this:
43  *
44  * Iter 1: "xa" matches in capture group.
45  * Iter 2: "xa" does not match, goes to next alternation.
46  *         "=" matches in =?
47  *         Bifurcates here (= might not match)
48  *         "xa" matches via \1 from previous iteration
49  *         "a" matches via "a" at end of second alternation
50  *         # at this point $1 is "=xaa"
51  *         \z  does not match -> backtracks.
52  * Backtracks to Iter 2 "=?" Bifurcation point where we have NOT matched "="
53  *         "=xaa" matches via \1 (as $1 has not been reset)
54  *         "a" matches via "a" at end of second alternation
55  *         "\z" does match. -> Pattern matches overall.
56  *
57  * What should happen and now does happen instead is:
58  *
59  * Backtracks to Iter 2 "=?" Bifurcation point where we have NOT matched "=",
60  *         \1 does not match as it is "xa" (as $1 was reset when backtracked)
61  *         and the current character in the string is an "="
62  *
63  * The fact that \1 in this case is marked as a VOLATILE_REF is what ensures
64  * that we reset the capture buffer properly.
65  *
66  * See 59db194299c94c6707095797c3df0e2f67ff82b2
67  * and 38508ce8fc3a1bd12a3bb65e9d4ceb9b396a18db
68  * for more details.
69  */
70 #define VOLATILE_REF 1
71
72 #include "regcharclass.h"
73
74 /* Convert branch sequences to more efficient trie ops? */
75 #define PERL_ENABLE_TRIE_OPTIMISATION 1
76
77 /* Be really aggressive about optimising patterns with trie sequences? */
78 #define PERL_ENABLE_EXTENDED_TRIE_OPTIMISATION 1
79
80 /* Should the optimiser take positive assertions into account? */
81 #define PERL_ENABLE_POSITIVE_ASSERTION_STUDY 0
82
83 /* Not for production use: */
84 #define PERL_ENABLE_EXPERIMENTAL_REGEX_OPTIMISATIONS 0
85
86 /*
87  * Structure for regexp "program".  This is essentially a linear encoding
88  * of a nondeterministic finite-state machine (aka syntax charts or
89  * "railroad normal form" in parsing technology).  Each node is an opcode
90  * plus a "next" pointer, possibly plus an operand.  "Next" pointers of
91  * all nodes except BRANCH implement concatenation; a "next" pointer with
92  * a BRANCH on both ends of it is connecting two alternatives.  (Here we
93  * have one of the subtle syntax dependencies:  an individual BRANCH (as
94  * opposed to a collection of them) is never concatenated with anything
95  * because of operator precedence.)  The operand of some types of node is
96  * a literal string; for others, it is a node leading into a sub-FSM.  In
97  * particular, the operand of a BRANCH node is the first node of the branch.
98  * (NB this is *not* a tree structure:  the tail of the branch connects
99  * to the thing following the set of BRANCHes.)  The opcodes are defined
100  * in regnodes.h which is generated from regcomp.sym by regcomp.pl.
101  */
102
103 /*
104  * A node is one char of opcode followed by two chars of "next" pointer.
105  * "Next" pointers are stored as two 8-bit pieces, high order first.  The
106  * value is a positive offset from the opcode of the node containing it.
107  * An operand, if any, simply follows the node.  (Note that much of the
108  * code generation knows about this implicit relationship.)
109  *
110  * Using two bytes for the "next" pointer is vast overkill for most things,
111  * but allows patterns to get big without disasters.
112  *
113  * [The "next" pointer is always aligned on an even
114  * boundary, and reads the offset directly as a short.]
115  */
116
117 /* This is the stuff that used to live in regexp.h that was truly
118    private to the engine itself. It now lives here. */
119
120 typedef struct regexp_internal {
121         regnode *regstclass;    /* Optional startclass as identified or constructed
122                                    by the optimiser */
123         struct reg_data *data;  /* Additional miscellaneous data used by the program.
124                                    Used to make it easier to clone and free arbitrary
125                                    data that the regops need. Often the ARG field of
126                                    a regop is an index into this structure. NOTE the
127                                    0th element of this structure is NEVER used and is
128                                    strictly reserved for internal purposes. */
129         struct reg_code_blocks *code_blocks;/* positions of literal (?{}) */
130         U32 proglen;            /* size of the compiled program in regnodes */
131         U32 name_list_idx;      /* Optional data index of an array of paren names,
132                                    only valid when RXp_PAREN_NAMES(prog) is true,
133                                    0 means "no value" like any other index into the
134                                    data array.*/
135         regnode program[1];     /* Unwarranted chumminess with compiler. */
136 } regexp_internal;
137
138 #define RXi_SET(x,y) (x)->pprivate = (void*)(y)   
139 #define RXi_GET(x)   ((regexp_internal *)((x)->pprivate))
140 #define RXi_GET_DECL(r,ri) regexp_internal *ri = RXi_GET(r)
141 #define RXi_GET_DECL_NULL(r,ri) regexp_internal *ri = (r) ? RXi_GET(r) : NULL
142 /*
143  * Flags stored in regexp->intflags
144  * These are used only internally to the regexp engine
145  *
146  * See regexp.h for flags used externally to the regexp engine
147  */
148 #define RXp_INTFLAGS(rx)        ((rx)->intflags)
149 #define RX_INTFLAGS(prog)        RXp_INTFLAGS(ReANY(prog))
150
151 #define PREGf_SKIP              0x00000001
152 #define PREGf_IMPLICIT          0x00000002 /* Converted .* to ^.* */
153 #define PREGf_NAUGHTY           0x00000004 /* how exponential is this pattern? */
154 #define PREGf_VERBARG_SEEN      0x00000008
155 #define PREGf_CUTGROUP_SEEN     0x00000010
156 #define PREGf_USE_RE_EVAL       0x00000020 /* compiled with "use re 'eval'" */
157 /* these used to be extflags, but are now intflags */
158 #define PREGf_NOSCAN            0x00000040
159                                 /* spare */
160 #define PREGf_GPOS_SEEN         0x00000100
161 #define PREGf_GPOS_FLOAT        0x00000200
162
163 #define PREGf_ANCH_MBOL         0x00000400
164 #define PREGf_ANCH_SBOL         0x00000800
165 #define PREGf_ANCH_GPOS         0x00001000
166 #define PREGf_RECURSE_SEEN      0x00002000
167 #define PREGf_PESSIMIZE_SEEN    0x00004000
168
169 #define PREGf_ANCH              \
170     ( PREGf_ANCH_SBOL | PREGf_ANCH_GPOS | PREGf_ANCH_MBOL )
171
172 /* this is where the old regcomp.h started */
173
174
175 /* Define the various regnode structures. These all should be a multiple
176  * of 32 bits large, and they should by and large correspond with each other
177  * in terms of naming, etc. Things can and will break in subtle ways if you
178  * change things without care. If you look at regexp.h you will see it
179  * contains this:
180  *
181  * union regnode_head {
182  *   struct {
183  *     union {
184  *       U8 flags;
185  *       U8 str_len_u8;
186  *       U8 first_byte;
187  *     } u_8;
188  *     U8  type;
189  *     U16 next_off;
190  *   } data;
191  *   U32 data_u32;
192  * };
193  *
194  * struct regnode {
195  *   union regnode_head head;
196  * };
197  *
198  * Which really is a complicated and alignment friendly version of
199  *
200  *  struct {
201  *    U8  flags;
202  *    U8  type;
203  *    U16 next_off;
204  *  };
205  *
206  * This structure is the base unit of elements in the regexp program.
207  * When we increment our way through the program we increment by the
208  * size of this structure (32 bits), and in all cases where regnode
209  * sizing is considered it is in units of this structure. All regnodes
210  * have a union regnode_head as their first parameter.
211  *
212  * This implies that no regnode style structure should contain 64 bit
213  * aligned members. Since the base regnode is 32 bits any member might
214  * not be 64 bit aligned no matter how you might try to pad out the
215  * struct itself (the regnode_ssc is special in this regard as it is
216  * never used in a program directly). If you want to store 64 bit
217  * members you need to store them specially. The struct regnode_p and the
218  * ARGp() and ARGp_SET() macros and related inline functions provide an example
219  * solution. Note they deal with a slightly more complicated problem than simple
220  * alignment, as pointers may be 32 bits or 64 bits depending on platform,
221  * but they illustrate the pattern to follow if you want to put a 64 bit value
222  * into a regnode.
223
224  * NOTE: Ideally we do not put pointers into the regnodes in a program. Instead
225  * we put them in the "data" part of the regexp structure and store the index into
226  * the data in the pointers in the regnode. This allows the pointer to be handled
227  * properly during clone/free operations (eg refcount bookkeeping). See S_add_data(),
228  * Perl_regdupe_internal(), Perl_regfree_internal() in regcomp.c for how the data
229  * array can be used, the letters 'arsSu' all refer to different types of SV that
230  * we already have support for in the data array.
231  */
232
233 union regnode_arg {
234     I32 i32;
235     U32 u32;
236     struct {
237         U16 u16a;
238         U16 u16b;
239     } hi_lo;
240 };
241
242
243 struct regnode_string {
244     union regnode_head head;
245     char string[1];
246 };
247
248 struct regnode_lstring { /* Constructed this way to keep the string aligned. */
249     union regnode_head head;
250     U32 str_len_u32;    /* Only 18 bits allowed before would overflow 'next_off' */
251     char string[1];
252 };
253
254 struct regnode_anyofhs { /* Constructed this way to keep the string aligned. */
255     union regnode_head head;
256     union regnode_arg arg1;
257     char string[1];
258 };
259
260 /* Argument bearing node - workhorse, ARG1u() is often used for the data field
261  * Can store either a signed 32 bit value via ARG1i() or unsigned 32 bit value
262  * via ARG1u(), or two unsigned 16 bit values via ARG1a() or ARG1b()
263  */
264 struct regnode_1 {
265     union regnode_head head;
266     union regnode_arg arg1;
267 };
268
269 /* Node whose argument is 'SV *'.  This needs to be used very carefully in
270  * situations where pointers won't become invalid because of, say re-mallocs.
271  *
272  * Note that this regnode type is problematic and should not be used or copied
273  * and will be removed in the future. Pointers should be stored in the data[]
274  * array and an index into the data array stored in the regnode, which allows the
275  * pointers to be handled properly during clone/free operations on the regexp
276  * data structure. As a byproduct it also saves space, often we use a 16 bit
277  * member to store indexes into the data[] array.
278  *
279  * Also note that the weird storage here is because regnodes are 32 bit aligned,
280  * which means we cannot have a 64 bit aligned member. To make things more annoying
281  * the size of a pointer may vary by platform. Thus we use a character array, and
282  * then use inline functions to copy the data in or out.
283  * */
284 struct regnode_p {
285     union regnode_head head;
286     char arg1_sv_ptr_bytes[sizeof(SV *)];
287 };
288
289 /* "Two Node" - similar to a regnode_1 but with space for an extra 32
290  * bit value, or two 16 bit valus. The first fields must match regnode_1.
291  * Extra field can be accessed as (U32)ARG2u() (I32)ARG2i() or (U16)ARG2a()
292  * and (U16)ARG2b() */
293 struct regnode_2 {
294     union regnode_head head;
295     union regnode_arg arg1;
296     union regnode_arg arg2;
297 };
298
299 /* "Three Node" - similar to a regnode_2 but with space for an additional
300  * 32 bit value, or two 16 bit values. The first fields must match regnode_2.
301  * The extra field can be accessed as (U32)ARG3u() (I32)ARG3i() or (U16)ARG3a()
302  * and (U16)ARG3b().
303  * Currently used for the CURLY style regops used to represent quantifers,
304  * storing the min and of the quantifier via ARG1i() and ARG2i(), along with
305  * ARG3a() and ARG3b() which are used to store information about the number of
306  * parens before and inside the quantified expression. */
307 struct regnode_3 {
308     union regnode_head head;
309     union regnode_arg arg1;
310     union regnode_arg arg2;
311     union regnode_arg arg3;
312 };
313
314 #define REGNODE_BBM_BITMAP_LEN                                                  \
315                       /* 6 info bits requires 64 bits; 5 => 32 */               \
316                     ((1 << (UTF_CONTINUATION_BYTE_INFO_BITS)) / CHARBITS)
317
318 /* Used for matching any two-byte UTF-8 character whose start byte is known.
319  * The array is a bitmap capable of representing any possible continuation
320  * byte. */
321 struct regnode_bbm {
322     union regnode_head head;
323     U8 bitmap[REGNODE_BBM_BITMAP_LEN];
324 };
325
326 #define ANYOF_BITMAP_SIZE       (NUM_ANYOF_CODE_POINTS / CHARBITS)
327
328 /* Note that these form structs which are supersets of the next smaller one, by
329  * appending fields.  Alignment problems can occur if one of those optional
330  * fields requires stricter alignment than the base struct.  And formal
331  * parameters that can really be two or more of the structs should be
332  * declared as the smallest one it could be.  See commit message for
333  * 7dcac5f6a5195002b55c935ee1d67f67e1df280b.  Regnode allocation is done
334  * without regard to alignment, and changing it to would also require changing
335  * the code that inserts and deletes regnodes.  The basic single-argument
336  * regnode has a U32, which is what reganode() allocates as a unit.  Therefore
337  * no field can require stricter alignment than U32. */
338     
339 /* also used by trie */
340 struct regnode_charclass {
341     union regnode_head head;
342     union regnode_arg arg1;
343     char bitmap[ANYOF_BITMAP_SIZE];     /* only compile-time */
344 };
345
346 /* has runtime (locale) \d, \w, ..., [:posix:] classes */
347 struct regnode_charclass_posixl {
348     union regnode_head head;
349     union regnode_arg arg1;
350     char bitmap[ANYOF_BITMAP_SIZE];             /* both compile-time ... */
351     U32 classflags;                             /* and run-time */
352 };
353
354 /* A synthetic start class (SSC); is a regnode_charclass_posixl_fold, plus an
355  * extra SV*, used only during regex construction and which is not used by the
356  * main machinery in regexec.c and which does not get embedded in the final compiled
357  * regex program.
358  *
359  * Because it does not get embedded it does not have to comply with the alignment
360  * and sizing constraints required for a normal regnode structure: it MAY contain
361  * pointers or members of whatever size needed and the compiler will do the right
362  * thing. (Every other regnode type is 32 bit aligned.)
363  *
364  * Note that the 'next_off' field is unused, as the SSC stands alone, so there is
365  * never a next node.
366  */
367 struct regnode_ssc {
368     union regnode_head head;
369     union regnode_arg arg1;
370     char bitmap[ANYOF_BITMAP_SIZE];     /* both compile-time ... */
371     U32 classflags;                     /* ... and run-time */
372
373     /* Auxiliary, only used during construction; NULL afterwards: list of code
374      * points matched */
375     SV* invlist;
376 };
377
378 /*  We take advantage of 'next_off' not otherwise being used in the SSC by
379  *  actually using it: by setting it to 1.  This allows us to test and
380  *  distinguish between an SSC and other ANYOF node types, as 'next_off' cannot
381  *  otherwise be 1, because it is the offset to the next regnode expressed in
382  *  units of regnodes.  Since an ANYOF node contains extra fields, it adds up
383  *  to 12 regnode units on 32-bit systems, (hence the minimum this can be (if
384  *  not 0) is 11 there.  Even if things get tightly packed on a 64-bit system,
385  *  it still would be more than 1. */
386 #define set_ANYOF_SYNTHETIC(n)  \
387     STMT_START{                 \
388         OP(n) = ANYOF;          \
389         NEXT_OFF(n) = 1;        \
390     } STMT_END
391
392 #define is_ANYOF_SYNTHETIC(n) (REGNODE_TYPE(OP(n)) == ANYOF && NEXT_OFF(n) == 1)
393
394 /* XXX fix this description.
395    Impose a limit of REG_INFTY on various pattern matching operations
396    to limit stack growth and to avoid "infinite" recursions.
397 */
398 /* The default size for REG_INFTY is I32_MAX, which is the same as UINT_MAX
399    (see perl.h). Unfortunately I32 isn't necessarily 32 bits (see handy.h).
400    On the Cray C90, or Cray T90, I32_MAX is considerably larger than it
401    might be elsewhere. To limit stack growth to reasonable sizes, supply a
402    smaller default.
403         --Andy Dougherty  11 June 1998
404         --Amended by Yves Orton 15 Jan 2023
405 */
406 #if INTSIZE > 4
407 #  ifndef REG_INFTY
408 #    define REG_INFTY  nBIT_IMAX(32)
409 #  endif
410 #endif
411
412 #ifndef REG_INFTY
413 #  define REG_INFTY I32_MAX
414 #endif
415
416 #define ARG_VALUE(arg) (arg)
417 #define ARG__SET(arg,val) ((arg) = (val))
418
419 #undef ARG
420 #undef ARG1
421 #undef ARG2
422
423 /* convention: each arg is is 32 bits, with the "u" suffix
424  * being unsigned 32 bits, the "i" suffix being signed 32 bits,
425  * and the "a" and "b" suffixes being unsigned 16 bit fields.
426  *
427  * We provide all 4 macros for each case for consistency, even
428  * though they arent all used.
429  */
430
431 #define ARG1u(p) ARG_VALUE(ARG1u_LOC(p))
432 #define ARG1i(p) ARG_VALUE(ARG1i_LOC(p))
433 #define ARG1a(p) ARG_VALUE(ARG1a_LOC(p))
434 #define ARG1b(p) ARG_VALUE(ARG1b_LOC(p))
435
436 #define ARG2u(p) ARG_VALUE(ARG2u_LOC(p))
437 #define ARG2i(p) ARG_VALUE(ARG2i_LOC(p))
438 #define ARG2a(p) ARG_VALUE(ARG2a_LOC(p))
439 #define ARG2b(p) ARG_VALUE(ARG2b_LOC(p))
440
441 #define ARG3u(p) ARG_VALUE(ARG3u_LOC(p))
442 #define ARG3i(p) ARG_VALUE(ARG3i_LOC(p))
443 #define ARG3a(p) ARG_VALUE(ARG3a_LOC(p))
444 #define ARG3b(p) ARG_VALUE(ARG3b_LOC(p))
445
446 #define ARGp(p) ARGp_VALUE_inline(p)
447
448 #define ARG1u_SET(p, val) ARG__SET(ARG1u_LOC(p), (val))
449 #define ARG1i_SET(p, val) ARG__SET(ARG1i_LOC(p), (val))
450 #define ARG1a_SET(p, val) ARG__SET(ARG1a_LOC(p), (val))
451 #define ARG1b_SET(p, val) ARG__SET(ARG1b_LOC(p), (val))
452
453 #define ARG2u_SET(p, val) ARG__SET(ARG2u_LOC(p), (val))
454 #define ARG2i_SET(p, val) ARG__SET(ARG2i_LOC(p), (val))
455 #define ARG2a_SET(p, val) ARG__SET(ARG2a_LOC(p), (val))
456 #define ARG2b_SET(p, val) ARG__SET(ARG2b_LOC(p), (val))
457
458 #define ARG3u_SET(p, val) ARG__SET(ARG3u_LOC(p), (val))
459 #define ARG3i_SET(p, val) ARG__SET(ARG3i_LOC(p), (val))
460 #define ARG3a_SET(p, val) ARG__SET(ARG3a_LOC(p), (val))
461 #define ARG3b_SET(p, val) ARG__SET(ARG3b_LOC(p), (val))
462
463 #define ARGp_SET(p, val) ARGp_SET_inline((p),(val))
464
465 /* the following define was set to 0xde in 075abff3
466  * as part of some linting logic. I have set it to 0
467  * as otherwise in every place where we /might/ set flags
468  * we have to set it 0 explicitly, which duplicates
469  * assignments and IMO adds an unacceptable level of
470  * surprise to working in the regex engine. If this
471  * is changed from 0 then at the very least make sure
472  * that SBOL for /^/ sets the flags to 0 explicitly.
473  * -- Yves */
474
475 #define NODE_ALIGN(node)
476 #define SIZE_ALIGN NODE_ALIGN
477
478 #undef OP
479 #undef OPERAND
480 #undef STRING
481 #undef NEXT_OFF
482 #undef NODE_ALIGN
483
484 #define NEXT_OFF(p)     ((p)->head.data.next_off)
485 #define OP(p)           ((p)->head.data.type)
486 #define STR_LEN_U8(p)   ((p)->head.data.u_8.str_len_u8)
487 #define FIRST_BYTE(p)   ((p)->head.data.u_8.first_byte)
488 #define FLAGS(p)        ((p)->head.data.u_8.flags) /* Caution: Doesn't apply to all      \
489                                            regnode types.  For some, it's the \
490                                            character set of the regnode */
491 #define STR_LENs(p)     (__ASSERT_(OP(p) != LEXACT && OP(p) != LEXACT_REQ8)  \
492                                     STR_LEN_U8((struct regnode_string *)p))
493 #define STRINGs(p)      (__ASSERT_(OP(p) != LEXACT && OP(p) != LEXACT_REQ8)  \
494                                     ((struct regnode_string *)p)->string)
495 #define OPERANDs(p)     STRINGs(p)
496
497 #define PARNO(p)        ARG1u(p)          /* APPLIES for OPEN and CLOSE only */
498
499 #define NODE_ALIGN_FILL(node) (FLAGS(node) = 0)
500
501 /* Long strings.  Currently limited to length 18 bits, which handles a 262000
502  * byte string.  The limiting factor is the 16 bit 'next_off' field, which
503  * points to the next regnode, so the furthest away it can be is 2**16.  On
504  * most architectures, regnodes are 2**2 bytes long, so that yields 2**18
505  * bytes.  Should a longer string be desired, we could increase it to 26 bits
506  * fairly easily, by changing this node to have longj type which causes the ARG
507  * field to be used for the link to the next regnode (although code would have
508  * to be changed to account for this), and then use a combination of the flags
509  * and next_off fields for the length.  To get 34 bit length, also change the
510  * node to be an ARG2L, using the second 32 bit field for the length, and not
511  * using the flags nor next_off fields at all.  One could have an llstring node
512  * and even an lllstring type. */
513 #define STR_LENl(p)     (__ASSERT_(OP(p) == LEXACT || OP(p) == LEXACT_REQ8)  \
514                                     (((struct regnode_lstring *)p)->str_len_u32))
515 #define STRINGl(p)      (__ASSERT_(OP(p) == LEXACT || OP(p) == LEXACT_REQ8)  \
516                                     (((struct regnode_lstring *)p)->string))
517 #define OPERANDl(p)     STRINGl(p)
518
519 #define STR_LEN(p)      ((OP(p) == LEXACT || OP(p) == LEXACT_REQ8)           \
520                                                ? STR_LENl(p) : STR_LENs(p))
521 #define STRING(p)       ((OP(p) == LEXACT || OP(p) == LEXACT_REQ8)           \
522                                                ? STRINGl(p)  : STRINGs(p))
523 #define OPERAND(p)      STRING(p)
524
525 /* The number of (smallest) regnode equivalents that a string of length l bytes
526  * occupies - Used by the REGNODE_AFTER() macros and functions. */
527 #define STR_SZ(l)       (((l) + sizeof(regnode) - 1) / sizeof(regnode))
528
529 #define setSTR_LEN(p,v)                                                     \
530     STMT_START{                                                             \
531         if (OP(p) == LEXACT || OP(p) == LEXACT_REQ8)                        \
532             ((struct regnode_lstring *)(p))->str_len_u32 = (v);             \
533         else                                                                \
534             STR_LEN_U8((struct regnode_string *)(p)) = (v);                 \
535     } STMT_END
536
537 #define ANYOFR_BASE_BITS    20
538 #define ANYOFRbase(p)   (ARG1u(p) & nBIT_MASK(ANYOFR_BASE_BITS))
539 #define ANYOFRdelta(p)  (ARG1u(p) >> ANYOFR_BASE_BITS)
540
541 #undef NODE_ALIGN
542 #undef ARG_LOC
543
544 #define NODE_ALIGN(node)
545 #define ARGp_BYTES_LOC(p)  (((struct regnode_p *)p)->arg1_sv_ptr_bytes)
546 #define ARG1u_LOC(p)    (((struct regnode_1 *)p)->arg1.u32)
547 #define ARG1i_LOC(p)    (((struct regnode_1 *)p)->arg1.i32)
548 #define ARG1a_LOC(p)    (((struct regnode_1 *)p)->arg1.hi_lo.u16a)
549 #define ARG1b_LOC(p)    (((struct regnode_1 *)p)->arg1.hi_lo.u16b)
550 #define ARG2u_LOC(p)    (((struct regnode_2 *)p)->arg2.u32)
551 #define ARG2i_LOC(p)    (((struct regnode_2 *)p)->arg2.i32)
552 #define ARG2a_LOC(p)    (((struct regnode_2 *)p)->arg2.hi_lo.u16a)
553 #define ARG2b_LOC(p)    (((struct regnode_2 *)p)->arg2.hi_lo.u16b)
554 #define ARG3u_LOC(p)    (((struct regnode_3 *)p)->arg3.u32)
555 #define ARG3i_LOC(p)    (((struct regnode_3 *)p)->arg3.i32)
556 #define ARG3a_LOC(p)    (((struct regnode_3 *)p)->arg3.hi_lo.u16a)
557 #define ARG3b_LOC(p)    (((struct regnode_3 *)p)->arg3.hi_lo.u16b)
558
559 /* These should no longer be used directly in most cases. Please use
560  * the REGNODE_AFTER() macros instead. */
561 #define NODE_STEP_REGNODE       1       /* sizeof(regnode)/sizeof(regnode) */
562
563 /* Core macros for computing "the regnode after this one". See also
564  * Perl_regnode_after() in reginline.h
565  *
566  * At the struct level regnodes are a linked list, with each node pointing
567  * at the next (via offsets), usually via the C<next_off> field in the
568  * structure. Where there is a need for a node to have two children the
569  * immediate physical successor of the node in the compiled program is used
570  * to represent one of them. A good example is the BRANCH construct,
571  * consider the pattern C</head(?:[ab]foo|[cd]bar)tail/>
572  *
573  *      1: EXACT <head> (3)
574  *      3: BRANCH (8)
575  *      4:   ANYOFR[ab] (6)
576  *      6:   EXACT <foo> (14)
577  *      8: BRANCH (FAIL)
578  *      9:   ANYOFR[cd] (11)
579  *     11:   EXACT <bar> (14)
580  *     13: TAIL (14)
581  *     14: EXACT <tail> (16)
582  *     16: END (0)
583  *
584  * The numbers in parens at the end of each line show the "next_off" value
585  * for that regnode in the program. We can see that the C<next_off> of
586  * the first BRANCH node (#3) is the second BRANCH node (#8), and indicates
587  * where execution should go if the regnodes *following* the BRANCH node fail
588  * to accept the input string. Thus to find the "next BRANCH" we would do
589  * C<Perl_regnext()> and follow the C<next_off> pointer, and to find
590  * the "BRANCHes contents" we would use C<REGNODE_AFTER()>.
591  *
592  * Be aware that C<REGNODE_AFTER()> is not guaranteed to give a *useful*
593  * result once the regex peephole optimizer has run (it will be correct
594  * however!). By the time code in regexec.c executes various regnodes
595  * may have been optimized out of the C<next_off> chain. An example
596  * can be seen above, node 13 will never be reached during execution
597  * flow as it has been stitched out of the C<next_off> chain. Both 6 and
598  * 11 would have pointed at it during compilation, but it exists only to
599  * facilitate the construction of the BRANCH structure and is effectively
600  * a NOOP, and thus the optimizer adjusts the links so it is skipped
601  * from execution time flow. In regexec.c it is only safe to use
602  * REGNODE_AFTER() on specific node types.
603  *
604  * Conversely during compilation C<Perl_regnext()> may not work properly
605  * as the C<next_off> may not be known until "later", (such as in the
606  * case of BRANCH nodes) and thus in regcomp.c the REGNODE_AFTER() macro
607  * is used very heavily instead.
608  *
609  * There are several variants of the REGNODE_AFTER_xxx() macros which
610  * are intended for use in different situations depending on how
611  * confident the code is about what type of node it is trying to find a
612  * successor for.
613  *
614  * So for instance if you know you are dealing with a known node type of
615  * constant size then you should use REGNODE_AFTER_type(n,TYPE).
616  *
617  * If you have a regnode pointer and you know you are dealing with a
618  * regnode type of constant size and you have already extracted its
619  * opcode use: REGNODE_AFTER_opcode(n,OPCODE).
620  *
621  * If you have a regnode and you know it is variable size then you
622  * you can produce optimized code by using REGNODE_AFTER_varies(n).
623  *
624  * If you have a regnode pointer and nothing else use: REGNODE_AFTER(n)
625  * This is the safest option and wraps C<Perl_regnode_after()>. It
626  * should produce the correct result regardless of its argument. The
627  * other options only produce correct results under specific
628  * constraints.
629  */
630 #define        REGNODE_AFTER_PLUS(p,extra)    ((p) + NODE_STEP_REGNODE + (extra))
631 /* under DEBUGGING we check that all REGNODE_AFTER optimized macros did the
632  * same thing that Perl_regnode_after() would have done. Note that when
633  * not compiled under DEBUGGING the assert_() macro is empty. Thus we
634  * don't have to implement different versions for DEBUGGING and not DEBUGGING,
635  * and explains why all the macros use REGNODE_AFTER_PLUS_DEBUG() under the
636  * hood. */
637 #define REGNODE_AFTER_PLUS_DEBUG(p,extra) \
638     (assert_(check_regnode_after(p,extra))  REGNODE_AFTER_PLUS((p),(extra)))
639
640 /* find the regnode after this p by using the opcode we previously extracted
641  * with OP(p) */
642 #define REGNODE_AFTER_opcode(p,op)          REGNODE_AFTER_PLUS_DEBUG((p),REGNODE_ARG_LEN(op))
643
644 /* find the regnode after this p by using the size of the struct associated with
645  * the opcode for p. use this when you *know* that p is pointer to a given type*/
646 #define REGNODE_AFTER_type(p,t)             REGNODE_AFTER_PLUS_DEBUG((p),EXTRA_SIZE(t))
647
648 /* find the regnode after this p by using OP(p) to find the regnode type of p */
649 #define REGNODE_AFTER_varies(p)            regnode_after(p,TRUE)
650
651 /* find the regnode after this p by using OP(p) to find the regnode type of p */
652 #define REGNODE_AFTER(p)            regnode_after(p,FALSE)
653
654
655 /* REGNODE_BEFORE() is trickier to deal with in terms of validation, execution.
656  * All the places that use it assume that p will be one struct regnode large.
657  * So to validate it we do the math to go backwards and then validate that the
658  * type of regnode we landed on is actually one regnode large. In theory if
659  * things go wrong the opcode should be illegal or say the item should be larger
660  * than it is, etc. */
661 #define        REGNODE_BEFORE_BASE(p)        ((p) - NODE_STEP_REGNODE)
662 #define        REGNODE_BEFORE_BASE_DEBUG(p)        \
663     (assert_(check_regnode_after(REGNODE_BEFORE_BASE(p),0))  REGNODE_BEFORE_BASE(p))
664 #define REGNODE_BEFORE(p) REGNODE_BEFORE_BASE_DEBUG(p)
665
666 #define FILL_NODE(offset, op)                                           \
667     STMT_START {                                                        \
668                     OP(REGNODE_p(offset)) = op;                         \
669                     NEXT_OFF(REGNODE_p(offset)) = 0;                    \
670     } STMT_END
671 #define FILL_ADVANCE_NODE(offset, op)                                   \
672     STMT_START {                                                        \
673                     FILL_NODE(offset, op);                              \
674                     (offset)++;                                         \
675     } STMT_END
676 #define FILL_ADVANCE_NODE_ARG1u(offset, op, arg)                        \
677     STMT_START {                                                        \
678                     ARG1u_SET(REGNODE_p(offset), arg);                  \
679                     FILL_ADVANCE_NODE(offset, op);                      \
680                     /* This is used generically for other operations    \
681                      * that have a longer argument */                   \
682                     (offset) += REGNODE_ARG_LEN(op);                    \
683     } STMT_END
684 #define FILL_ADVANCE_NODE_ARGp(offset, op, arg)                         \
685     STMT_START {                                                        \
686                     ARGp_SET(REGNODE_p(offset), arg);                   \
687                     FILL_ADVANCE_NODE(offset, op);                      \
688                     (offset) += REGNODE_ARG_LEN(op);                    \
689     } STMT_END
690 #define FILL_ADVANCE_NODE_2ui_ARG(offset, op, arg1, arg2)               \
691     STMT_START {                                                        \
692                     ARG1u_SET(REGNODE_p(offset), arg1);                 \
693                     ARG2i_SET(REGNODE_p(offset), arg2);                 \
694                     FILL_ADVANCE_NODE(offset, op);                      \
695                     (offset) += 2;                                      \
696     } STMT_END
697
698 /* define these after we define the normal macros, so we can use
699  * ARGp_BYTES_LOC(n) */
700
701 static inline SV *
702 ARGp_VALUE_inline(struct regnode *node) {
703     SV *ptr;
704     memcpy(&ptr, ARGp_BYTES_LOC(node), sizeof(ptr));
705
706     return ptr;
707 }
708
709 static inline void
710 ARGp_SET_inline(struct regnode *node, SV *ptr) {
711     memcpy(ARGp_BYTES_LOC(node), &ptr, sizeof(ptr));
712 }
713
714 #define REG_MAGIC 0234
715
716 /* An ANYOF node matches a single code point based on specified criteria.  It
717  * now comes in several styles, but originally it was just a 256 element
718  * bitmap, indexed by the code point (which was always just a byte).  If the
719  * corresponding bit for a code point is 1, the code point matches; if 0, it
720  * doesn't match (complemented if inverted).  This worked fine before Unicode
721  * existed, but making a bit map long enough to accommodate a bit for every
722  * possible Unicode code point is prohibitively large.  Therefore it is made
723  * much much smaller, and an inversion list is created to handle code points
724  * not represented by the bitmap.  (It is now possible to compile the bitmap to
725  * a larger size to avoid the slower inversion list lookup for however big the
726  * bitmap is set to, but this is rarely done).  If the bitmap is sufficient to
727  * specify all possible matches (with nothing outside it matching), no
728  * inversion list is needed nor included, and the argument to the ANYOF node is
729  * set to the following: */
730
731 #define ANYOF_MATCHES_ALL_OUTSIDE_BITMAP_VALUE   U32_MAX
732 #define ANYOF_MATCHES_ALL_OUTSIDE_BITMAP(node)                              \
733                     (ARG1u(node) == ANYOF_MATCHES_ALL_OUTSIDE_BITMAP_VALUE)
734
735 #define ANYOF_MATCHES_NONE_OUTSIDE_BITMAP_VALUE                             \
736    /* Assumes ALL is odd */  (ANYOF_MATCHES_ALL_OUTSIDE_BITMAP_VALUE - 1)
737 #define ANYOF_MATCHES_NONE_OUTSIDE_BITMAP(node)                             \
738                     (ARG1u(node) == ANYOF_MATCHES_NONE_OUTSIDE_BITMAP_VALUE)
739
740 #define ANYOF_ONLY_HAS_BITMAP_MASK  ANYOF_MATCHES_NONE_OUTSIDE_BITMAP_VALUE
741 #define ANYOF_ONLY_HAS_BITMAP(node)                                         \
742   ((ARG1u(node) & ANYOF_ONLY_HAS_BITMAP_MASK) == ANYOF_ONLY_HAS_BITMAP_MASK)
743
744 #define ANYOF_HAS_AUX(node)  (! ANYOF_ONLY_HAS_BITMAP(node))
745
746 /* There are also ANYOFM nodes, used when the bit patterns representing the
747  * matched code points happen to be such that they can be checked by ANDing
748  * with a mask.  The regex compiler looks for and silently optimizes to using
749  * this node type in the few cases where it works out.  The eight octal digits
750  * form such a group.  These nodes are simple and fast and no further
751  * discussion is needed here.
752  *
753  * And, there are ANYOFH-ish nodes which match only code points that aren't in
754  * the bitmap  (the H stands for High).  These are common for expressing
755  * Unicode properties concerning non-Latin scripts.  They dispense with the
756  * bitmap altogether and don't need any of the flags discussed below.
757  *
758  * And, there are ANYOFR-ish nodes which match within a single range.
759  *
760  * When there is a need to specify what matches outside the bitmap, it is done
761  * by allocating an AV as part of the pattern's compiled form, and the argument
762  * to the node instead of being ANYOF_ONLY_HAS_BITMAP, points to that AV.
763  *
764  * (Actually, that is an oversimplification.  The AV is placed into the
765  * pattern's struct reg_data, and what is stored in the node's argument field
766  * is its index into that struct.  And the inversion list is just one element,
767  * the zeroth, of the AV.)
768  *
769  * There are certain situations where a single inversion list can't handle all
770  * the complexity.  These are dealt with by having extra elements in the AV, by
771  * specifying flag bits in the ANYOF node, and/or special code.  As an example,
772  * there are instances where what the ANYOF node matches is not completely
773  * known until runtime.  In these cases, a flag is set, and the bitmap has a 1
774  * for the code points which are known at compile time to be 1, and a 0 for the
775  * ones that are known to be 0, or require runtime resolution.  Some missing
776  * information can be found by merely seeing if the pattern is UTF-8 or not;
777  * other cases require looking at the extra elements in the AV.
778  *
779  * There are 5 cases where the bitmap is insufficient.  These are specified by
780  * flags in the node's flags field.  We could use five bits to represent the 5
781  * cases, but to save flags bits (which are perennially in short supply), we
782  * play some games.  The cases are:
783  *
784  *  1)  As already mentioned, if some code points outside the bitmap match, and
785  *      some do not, an inversion list is specified to indicate which ones.
786  *
787  *  2)  Under /d rules, it can happen that code points that are in the upper
788  *      latin1 range (\x80-\xFF or their equivalents on EBCDIC platforms) match
789  *      only if the runtime target string being matched against is UTF-8.  For
790  *      example /[\w[:punct:]]/d.  This happens only for certain posix classes,
791  *      and all such ones also have above-bitmap matches.
792  *
793  *      Note that /d rules are no longer encouraged; 'use 5.14' or higher
794  *      deselects them.  But they are still supported, and a flag is required
795  *      so that they can be properly handled.  But it can be a shared flag: see
796  *      4) below.
797  *
798  *  3)  Also under /d rules, something like /[\Wfoo]/ will match everything in
799  *      the \x80-\xFF range, unless the string being matched against is UTF-8.
800  *      An inversion list could be created for this case, but this is
801  *      relatively common, and it turns out that it's all or nothing:  if any
802  *      one of these code points matches, they all do.  Hence a single bit
803  *      suffices.  We use a shared flag that doesn't take up space by itself:
804  *      ANYOFD_NON_UTF8_MATCHES_ALL_NON_ASCII__shared.  This also means there
805  *      is an inversion list for the things that don't fit into the bitmap.
806  *
807  *  4)  A user-defined \p{} property may not have been defined by the time the
808  *      regex is compiled.  In this case, we don't know until runtime what it
809  *      will match, so we have to assume it could match anything, including
810  *      code points that ordinarily would be in the bitmap.  A flag bit is
811  *      necessary to indicate this, though we can use the
812  *      ANYOF_HAS_EXTRA_RUNTIME_MATCHES flag, along with the node not being
813  *      ANYOFD.  The information required to construct the property is stored
814  *      in the AV pointed to by the node's argument.  This case is quite
815  *      uncommon in the field, and the /(?[...])/ construct is a better way to
816  *      accomplish what this feature does.
817  *
818  *  5)  /[foo]/il may have folds that are only valid if the runtime locale is a
819  *      UTF-8 one.  The ANYOF_HAS_EXTRA_RUNTIME_MATCHES flag can also be used
820  *      for these.  The list is stored in a different element of the AV, so its
821  *      existence differentiates this case from that of 4), along with the node
822  *      being ANYOFL, with the ANYOFL_FOLD flag being set.  There are a few
823  *      additional folds valid only if the UTF-8 locale is a Turkic one which
824  *      is tested for explicitly.
825  *
826  * Note that the user-defined property flag and the /il flag can affect whether
827  * an ASCII character matches in the bitmap or not.
828  *
829  * And this still isn't the end of the story.  In some cases, warnings are
830  * supposed to be raised when matching certain categories of code points in the
831  * target string.  Flags are set to indicate this.  This adds up to a bunch of
832  * flags required, and we only have 8 available.  That is why we share some.
833  * At the moment, there are two spare flag bits, but this could be increased by
834  * various tricks:
835  *
836  * ANYOF_MATCHES_POSIXL is redundant with the node type ANYOFPOSIXL.  That flag
837  * could be removed, but at the expense of having to write extra code, which
838  * would take up space, and writing this turns out to be not hard, but not
839  * trivial.
840  *
841  * If this is done, an extension would be to make all ANYOFL nodes contain the
842  * extra 32 bits that ANYOFPOSIXL ones do, doubling each instance's size.  The
843  * posix flags only occupy 30 bits, so the ANYOFL_FOLD  and
844  * ANYOFL_UTF8_LOCALE_REQD bits could be moved to that extra space, but it
845  * would also mean extra instructions, as there are currently places in the
846  * code that assume those two bits are zero.
847  *
848  * Some flags are not used in synthetic start class (SSC) nodes, so could be
849  * shared should new flags be needed for SSCs, like SSC_MATCHES_EMPTY_STRING
850  * now. */
851
852 /* If this is set, the result of the match should be complemented.  regexec.c
853  * is expecting this to be in the low bit.  Never in an SSC */
854 #define ANYOF_INVERT                            0x01
855
856 /* For the SSC node only, which cannot be inverted, so is shared with that bit.
857  * This is used only during regex compilation. */
858 #define SSC_MATCHES_EMPTY_STRING                ANYOF_INVERT
859
860 /* Set if this is a regnode_charclass_posixl vs a regnode_charclass.  This
861  * is used for runtime \d, \w, [:posix:], ..., which are used only in locale
862  * and the optimizer's synthetic start class.  Non-locale \d, etc are resolved
863  * at compile-time.  Only set under /l; can be in SSC */
864 #define ANYOF_MATCHES_POSIXL                    0x02
865
866 /* The fold is calculated and stored in the bitmap where possible at compile
867  * time.  However under locale, the actual folding varies depending on
868  * what the locale is at the time of execution, so it has to be deferred until
869  * then.  Only set under /l; never in an SSC  */
870 #define ANYOFL_FOLD                             0x04
871
872 /* Warn if the runtime locale isn't a UTF-8 one (and the generated node assumes
873  * a UTF-8 locale. */
874 #define ANYOFL_UTF8_LOCALE_REQD                 0x08
875
876 /* Spare: Be sure to change ANYOF_FLAGS_ALL if this gets used  0x10 */
877
878 /* Spare: Be sure to change ANYOF_FLAGS_ALL if this gets used  0x20 */
879
880 /* Shared bit that indicates that there are potential additional matches stored
881  * outside the bitmap, as pointed to by the AV given by the node's argument.
882  * The node type is used at runtime (in conjunction with this flag and other
883  * information available then) to decide if the flag should be acted upon.
884  * This extra information is needed because of at least one of the following
885  * three reasons.
886  *      Under /d and the matched string is in UTF-8, it means the ANYOFD node
887  *          matches more things than in the bitmap.  Those things will be any
888  *          code point too high for the bitmap, but crucially, any non-ASCII
889  *          characters that match iff when using Unicode rules.  These all are
890  *          < 256.
891  *
892  *      Under /l and ANYOFL_FOLD is set, this flag may indicate there are
893  *          potential matches valid only if the locale is a UTF-8 one.  If so,
894  *          a list of them is stored in the AV.
895  *
896  *      For any non-ANYOFD node, there may be a user-defined property that
897  *          wasn't yet defined at the time the regex was compiled, and so must
898  *          be looked up at runtime, The information required to do so will
899  *          also be in the AV.
900  *
901  *      Note that an ANYOFL node may contain both a user-defined property, and
902  *      folds not always valid.  The important thing is that there is an AV to
903  *      look at. */
904 #define ANYOF_HAS_EXTRA_RUNTIME_MATCHES 0x40
905
906 /* Shared bit:
907  *      Under /d it means the ANYOFD node matches all non-ASCII Latin1
908  *          characters when the target string is not in utf8.
909  *      When not under /d, it means the ANYOF node should raise a warning if
910  *          matching against an above-Unicode code point.
911  * (These uses are mutually exclusive because the warning requires a \p{}, and
912  * \p{} implies /u which deselects /d).  An SSC node only has this bit set if
913  * what is meant is the warning.  The names are to make sure that you are
914  * cautioned about its shared nature */
915 #define ANYOFD_NON_UTF8_MATCHES_ALL_NON_ASCII__shared 0x80
916 #define ANYOF_WARN_SUPER__shared                      0x80
917
918 #define ANYOF_FLAGS_ALL         ((U8) ~(0x10|0x20))
919
920 #define ANYOF_LOCALE_FLAGS (  ANYOFL_FOLD               \
921                             | ANYOF_MATCHES_POSIXL      \
922                             | ANYOFL_UTF8_LOCALE_REQD)
923
924 /* These are the flags that apply to both regular ANYOF nodes and synthetic
925  * start class nodes during construction of the SSC.  During finalization of
926  * the SSC, other of the flags may get added to it */
927 #define ANYOF_COMMON_FLAGS      0
928
929 /* Character classes for node->classflags of ANYOF */
930 /* Should be synchronized with a table in regprop() */
931 /* 2n should be the normal one, paired with its complement at 2n+1 */
932
933 #define ANYOF_ALPHA    ((CC_ALPHA_) * 2)
934 #define ANYOF_NALPHA   ((ANYOF_ALPHA) + 1)
935 #define ANYOF_ALPHANUMERIC   ((CC_ALPHANUMERIC_) * 2)    /* [[:alnum:]] isalnum(3), utf8::IsAlnum */
936 #define ANYOF_NALPHANUMERIC  ((ANYOF_ALPHANUMERIC) + 1)
937 #define ANYOF_ASCII    ((CC_ASCII_) * 2)
938 #define ANYOF_NASCII   ((ANYOF_ASCII) + 1)
939 #define ANYOF_BLANK    ((CC_BLANK_) * 2)     /* GNU extension: space and tab: non-vertical space */
940 #define ANYOF_NBLANK   ((ANYOF_BLANK) + 1)
941 #define ANYOF_CASED    ((CC_CASED_) * 2)    /* Pseudo class for [:lower:] or
942                                                [:upper:] under /i */
943 #define ANYOF_NCASED   ((ANYOF_CASED) + 1)
944 #define ANYOF_CNTRL    ((CC_CNTRL_) * 2)
945 #define ANYOF_NCNTRL   ((ANYOF_CNTRL) + 1)
946 #define ANYOF_DIGIT    ((CC_DIGIT_) * 2)     /* \d */
947 #define ANYOF_NDIGIT   ((ANYOF_DIGIT) + 1)
948 #define ANYOF_GRAPH    ((CC_GRAPH_) * 2)
949 #define ANYOF_NGRAPH   ((ANYOF_GRAPH) + 1)
950 #define ANYOF_LOWER    ((CC_LOWER_) * 2)
951 #define ANYOF_NLOWER   ((ANYOF_LOWER) + 1)
952 #define ANYOF_PRINT    ((CC_PRINT_) * 2)
953 #define ANYOF_NPRINT   ((ANYOF_PRINT) + 1)
954 #define ANYOF_PUNCT    ((CC_PUNCT_) * 2)
955 #define ANYOF_NPUNCT   ((ANYOF_PUNCT) + 1)
956 #define ANYOF_SPACE    ((CC_SPACE_) * 2)     /* \s */
957 #define ANYOF_NSPACE   ((ANYOF_SPACE) + 1)
958 #define ANYOF_UPPER    ((CC_UPPER_) * 2)
959 #define ANYOF_NUPPER   ((ANYOF_UPPER) + 1)
960 #define ANYOF_WORDCHAR ((CC_WORDCHAR_) * 2)  /* \w, PL_utf8_alnum, utf8::IsWord, ALNUM */
961 #define ANYOF_NWORDCHAR   ((ANYOF_WORDCHAR) + 1)
962 #define ANYOF_XDIGIT   ((CC_XDIGIT_) * 2)
963 #define ANYOF_NXDIGIT  ((ANYOF_XDIGIT) + 1)
964
965 /* pseudo classes below this, not stored in the class bitmap, but used as flags
966    during compilation of char classes */
967
968 #define ANYOF_VERTWS    ((CC_VERTSPACE_) * 2)
969 #define ANYOF_NVERTWS   ((ANYOF_VERTWS)+1)
970
971 /* It is best if this is the last one, as all above it are stored as bits in a
972  * bitmap, and it isn't part of that bitmap */
973 #if CC_VERTSPACE_ != HIGHEST_REGCOMP_DOT_H_SYNC_
974 #   error Problem with handy.h HIGHEST_REGCOMP_DOT_H_SYNC_ #define
975 #endif
976
977 #define ANYOF_POSIXL_MAX (ANYOF_VERTWS) /* So upper loop limit is written:
978                                          *       '< ANYOF_MAX'
979                                          * Hence doesn't include VERTWS, as that
980                                          * is a pseudo class */
981 #define ANYOF_MAX      ANYOF_POSIXL_MAX
982
983 #if (ANYOF_POSIXL_MAX > 32)   /* Must fit in 32-bit word */
984 #   error Problem with handy.h CC_foo_ #defines
985 #endif
986
987 #define ANYOF_HORIZWS   ((ANYOF_POSIXL_MAX)+2) /* = (ANYOF_NVERTWS + 1) */
988 #define ANYOF_NHORIZWS  ((ANYOF_POSIXL_MAX)+3)
989
990 #define ANYOF_UNIPROP   ((ANYOF_POSIXL_MAX)+4)  /* Used to indicate a Unicode
991                                                    property: \p{} or \P{} */
992
993 /* Backward source code compatibility. */
994
995 #define ANYOF_ALNUML     ANYOF_ALNUM
996 #define ANYOF_NALNUML    ANYOF_NALNUM
997 #define ANYOF_SPACEL     ANYOF_SPACE
998 #define ANYOF_NSPACEL    ANYOF_NSPACE
999 #define ANYOF_ALNUM ANYOF_WORDCHAR
1000 #define ANYOF_NALNUM ANYOF_NWORDCHAR
1001
1002 /* Utility macros for the bitmap and classes of ANYOF */
1003
1004 #define BITMAP_BYTE(p, c)       (( (U8*) (p)) [ ( ( (UV) (c)) >> 3) ] )
1005 #define BITMAP_BIT(c)           (1U << ((c) & 7))
1006 #define BITMAP_TEST(p, c)       (BITMAP_BYTE(p, c) & BITMAP_BIT((U8)(c)))
1007
1008 #define ANYOF_FLAGS(p)          (FLAGS(p))
1009
1010 #define ANYOF_BIT(c)            BITMAP_BIT(c)
1011
1012 #define ANYOF_POSIXL_BITMAP(p)  (((regnode_charclass_posixl*) (p))->classflags)
1013
1014 #define POSIXL_SET(field, c)    ((field) |= (1U << (c)))
1015 #define ANYOF_POSIXL_SET(p, c)  POSIXL_SET(ANYOF_POSIXL_BITMAP(p), (c))
1016
1017 #define POSIXL_CLEAR(field, c) ((field) &= ~ (1U <<(c)))
1018 #define ANYOF_POSIXL_CLEAR(p, c) POSIXL_CLEAR(ANYOF_POSIXL_BITMAP(p), (c))
1019
1020 #define POSIXL_TEST(field, c)   ((field) & (1U << (c)))
1021 #define ANYOF_POSIXL_TEST(p, c) POSIXL_TEST(ANYOF_POSIXL_BITMAP(p), (c))
1022
1023 #define POSIXL_ZERO(field)      STMT_START { (field) = 0; } STMT_END
1024 #define ANYOF_POSIXL_ZERO(ret)  POSIXL_ZERO(ANYOF_POSIXL_BITMAP(ret))
1025
1026 #define ANYOF_POSIXL_SET_TO_BITMAP(p, bits)                                 \
1027                 STMT_START { ANYOF_POSIXL_BITMAP(p) = (bits); } STMT_END
1028
1029 /* Shifts a bit to get, eg. 0x4000_0000, then subtracts 1 to get 0x3FFF_FFFF */
1030 #define ANYOF_POSIXL_SETALL(ret)                                            \
1031                 STMT_START {                                                \
1032                     ANYOF_POSIXL_BITMAP(ret) = nBIT_MASK(ANYOF_POSIXL_MAX); \
1033                 } STMT_END
1034 #define ANYOF_CLASS_SETALL(ret) ANYOF_POSIXL_SETALL(ret)
1035
1036 #define ANYOF_POSIXL_TEST_ANY_SET(p)                               \
1037         ((ANYOF_FLAGS(p) & ANYOF_MATCHES_POSIXL) && ANYOF_POSIXL_BITMAP(p))
1038 #define ANYOF_CLASS_TEST_ANY_SET(p) ANYOF_POSIXL_TEST_ANY_SET(p)
1039
1040 /* Since an SSC always has this field, we don't have to test for that; nor do
1041  * we want to because the bit isn't set for SSC during its construction */
1042 #define ANYOF_POSIXL_SSC_TEST_ANY_SET(p)                               \
1043                             cBOOL(((regnode_ssc*)(p))->classflags)
1044 #define ANYOF_POSIXL_SSC_TEST_ALL_SET(p) /* Are all bits set? */       \
1045         (((regnode_ssc*) (p))->classflags                              \
1046                                         == nBIT_MASK(ANYOF_POSIXL_MAX))
1047
1048 #define ANYOF_POSIXL_TEST_ALL_SET(p)                                   \
1049         ((ANYOF_FLAGS(p) & ANYOF_MATCHES_POSIXL)                       \
1050          && ANYOF_POSIXL_BITMAP(p) == nBIT_MASK(ANYOF_POSIXL_MAX))
1051
1052 #define ANYOF_POSIXL_OR(source, dest) STMT_START { (dest)->classflags |= (source)->classflags ; } STMT_END
1053 #define ANYOF_CLASS_OR(source, dest) ANYOF_POSIXL_OR((source), (dest))
1054
1055 #define ANYOF_POSIXL_AND(source, dest) STMT_START { (dest)->classflags &= (source)->classflags ; } STMT_END
1056
1057 #define ANYOF_BITMAP_ZERO(ret)  Zero(((regnode_charclass*)(ret))->bitmap, ANYOF_BITMAP_SIZE, char)
1058 #define ANYOF_BITMAP(p)         ((regnode_charclass*)(p))->bitmap
1059 #define ANYOF_BITMAP_BYTE(p, c) BITMAP_BYTE(ANYOF_BITMAP(p), c)
1060 #define ANYOF_BITMAP_SET(p, c)  (ANYOF_BITMAP_BYTE(p, c) |=  ANYOF_BIT(c))
1061 #define ANYOF_BITMAP_CLEAR(p,c) (ANYOF_BITMAP_BYTE(p, c) &= ~ANYOF_BIT(c))
1062 #define ANYOF_BITMAP_TEST(p, c) cBOOL(ANYOF_BITMAP_BYTE(p, c) & ANYOF_BIT(c))
1063
1064 #define ANYOF_BITMAP_SETALL(p)          \
1065         memset (ANYOF_BITMAP(p), 255, ANYOF_BITMAP_SIZE)
1066 #define ANYOF_BITMAP_CLEARALL(p)        \
1067         Zero (ANYOF_BITMAP(p), ANYOF_BITMAP_SIZE)
1068
1069 /*
1070  * Utility definitions.
1071  */
1072 #ifndef CHARMASK
1073 #  define UCHARAT(p)    ((int)*(const U8*)(p))
1074 #else
1075 #  define UCHARAT(p)    ((int)*(p)&CHARMASK)
1076 #endif
1077
1078 /* Number of regnode equivalents that 'guy' occupies beyond the size of the
1079  * smallest regnode. */
1080 #define EXTRA_SIZE(guy) ((sizeof(guy)-1)/sizeof(struct regnode))
1081
1082 #define REG_ZERO_LEN_SEEN                   0x00000001
1083 #define REG_LOOKBEHIND_SEEN                 0x00000002
1084 /* add a short form alias to keep the line length police happy */
1085 #define REG_LB_SEEN                         REG_LOOKBEHIND_SEEN
1086 #define REG_GPOS_SEEN                       0x00000004
1087 /* spare */
1088 #define REG_RECURSE_SEEN                    0x00000020
1089 #define REG_TOP_LEVEL_BRANCHES_SEEN         0x00000040
1090 #define REG_VERBARG_SEEN                    0x00000080
1091 #define REG_CUTGROUP_SEEN                   0x00000100
1092 #define REG_RUN_ON_COMMENT_SEEN             0x00000200
1093 #define REG_UNFOLDED_MULTI_SEEN             0x00000400
1094 /* spare */
1095 #define REG_UNBOUNDED_QUANTIFIER_SEEN       0x00001000
1096 #define REG_PESSIMIZE_SEEN                  0x00002000
1097
1098
1099 START_EXTERN_C
1100
1101 #ifdef PLUGGABLE_RE_EXTENSION
1102 #include "re_nodes.h"
1103 #else
1104 #include "regnodes.h"
1105 #endif
1106
1107 #ifndef PLUGGABLE_RE_EXTENSION
1108 #ifndef DOINIT
1109 EXTCONST regexp_engine PL_core_reg_engine;
1110 #else /* DOINIT */
1111 EXTCONST regexp_engine PL_core_reg_engine = { 
1112         Perl_re_compile,
1113         Perl_regexec_flags,
1114         Perl_re_intuit_start,
1115         Perl_re_intuit_string, 
1116         Perl_regfree_internal,
1117         Perl_reg_numbered_buff_fetch,
1118         Perl_reg_numbered_buff_store,
1119         Perl_reg_numbered_buff_length,
1120         Perl_reg_named_buff,
1121         Perl_reg_named_buff_iter,
1122         Perl_reg_qr_package,
1123 #if defined(USE_ITHREADS)        
1124         Perl_regdupe_internal,
1125 #endif        
1126         Perl_re_op_compile
1127 };
1128 #endif /* DOINIT */
1129 #endif /* PLUGGABLE_RE_EXTENSION */
1130
1131
1132 END_EXTERN_C
1133
1134
1135 /* .what is a character array with one character for each member of .data
1136  * The character describes the function of the corresponding .data item:
1137  *   a - AV for paren_name_list under DEBUGGING
1138  *   f - start-class data for regstclass optimization
1139  *   l - start op for literal (?{EVAL}) item
1140  *   L - start op for literal (?{EVAL}) item, with separate CV (qr//)
1141  *   r - pointer to an embedded code-containing qr, e.g. /ab$qr/
1142  *   s - inversion list for Unicode-style character class, and the
1143  *       multicharacter strings resulting from casefolding the single-character
1144  *       entries in the character class
1145  *   t - trie struct
1146  *   u - trie struct's widecharmap (a HV, so can't share, must dup)
1147  *       also used for revcharmap and words under DEBUGGING
1148  *   T - aho-trie struct
1149  *   S - sv for named capture lookup
1150  * 20010712 mjd@plover.com
1151  * (Remember to update re_dup() and pregfree() if you add any items.)
1152  */
1153 struct reg_data {
1154     U32 count;
1155     U8 *what;
1156     void* data[1];
1157 };
1158
1159 /* Code in S_to_utf8_substr() and S_to_byte_substr() in regexec.c accesses
1160    anchored* and float* via array indexes 0 and 1.  */
1161 #define anchored_substr substrs->data[0].substr
1162 #define anchored_utf8 substrs->data[0].utf8_substr
1163 #define anchored_offset substrs->data[0].min_offset
1164 #define anchored_end_shift substrs->data[0].end_shift
1165
1166 #define float_substr substrs->data[1].substr
1167 #define float_utf8 substrs->data[1].utf8_substr
1168 #define float_min_offset substrs->data[1].min_offset
1169 #define float_max_offset substrs->data[1].max_offset
1170 #define float_end_shift substrs->data[1].end_shift
1171
1172 #define check_substr substrs->data[2].substr
1173 #define check_utf8 substrs->data[2].utf8_substr
1174 #define check_offset_min substrs->data[2].min_offset
1175 #define check_offset_max substrs->data[2].max_offset
1176 #define check_end_shift substrs->data[2].end_shift
1177
1178 #define RX_ANCHORED_SUBSTR(rx)  (ReANY(rx)->anchored_substr)
1179 #define RX_ANCHORED_UTF8(rx)    (ReANY(rx)->anchored_utf8)
1180 #define RX_FLOAT_SUBSTR(rx)     (ReANY(rx)->float_substr)
1181 #define RX_FLOAT_UTF8(rx)       (ReANY(rx)->float_utf8)
1182
1183 /* trie related stuff */
1184
1185 /* a transition record for the state machine. the
1186    check field determines which state "owns" the
1187    transition. the char the transition is for is
1188    determined by offset from the owning states base
1189    field.  the next field determines which state
1190    is to be transitioned to if any.
1191 */
1192 struct _reg_trie_trans {
1193   U32 next;
1194   U32 check;
1195 };
1196
1197 /* a transition list element for the list based representation */
1198 struct _reg_trie_trans_list_elem {
1199     U16 forid;
1200     U32 newstate;
1201 };
1202 typedef struct _reg_trie_trans_list_elem reg_trie_trans_le;
1203
1204 /* a state for compressed nodes. base is an offset
1205   into an array of reg_trie_trans array. If wordnum is
1206   nonzero the state is accepting. if base is zero then
1207   the state has no children (and will be accepting)
1208 */
1209 struct _reg_trie_state {
1210   U16 wordnum;
1211   union {
1212     U32                base;
1213     reg_trie_trans_le* list;
1214   } trans;
1215 };
1216
1217 /* info per word; indexed by wordnum */
1218 typedef struct {
1219     U16  prev;  /* previous word in acceptance chain; eg in
1220                  * zzz|abc|ab/ after matching the chars abc, the
1221                  * accepted word is #2, and the previous accepted
1222                  * word is #3 */
1223     U32 len;    /* how many chars long is this word? */
1224     U32 accept; /* accept state for this word */
1225 } reg_trie_wordinfo;
1226
1227
1228 typedef struct _reg_trie_state    reg_trie_state;
1229 typedef struct _reg_trie_trans    reg_trie_trans;
1230
1231
1232 /* anything in here that needs to be freed later
1233    should be dealt with in pregfree.
1234    refcount is first in both this and _reg_ac_data to allow a space
1235    optimisation in Perl_regdupe.  */
1236 struct _reg_trie_data {
1237     U32             refcount;        /* number of times this trie is referenced */
1238     U32             lasttrans;       /* last valid transition element */
1239     U16             *charmap;        /* byte to charid lookup array */
1240     reg_trie_state  *states;         /* state data */
1241     reg_trie_trans  *trans;          /* array of transition elements */
1242     char            *bitmap;         /* stclass bitmap */
1243     U16             *jump;           /* optional 1 indexed array of offsets before tail 
1244                                         for the node following a given word. */
1245     U16             *j_before_paren; /* optional 1 indexed array of parno reset data
1246                                         for the given jump. */
1247     U16             *j_after_paren;  /* optional 1 indexed array of parno reset data
1248                                         for the given jump. */
1249
1250     reg_trie_wordinfo *wordinfo;     /* array of info per word */
1251     U16             uniquecharcount; /* unique chars in trie (width of trans table) */
1252     U32             startstate;      /* initial state - used for common prefix optimisation */
1253     STRLEN          minlen;          /* minimum length of words in trie - build/opt only? */
1254     STRLEN          maxlen;          /* maximum length of words in trie - build/opt only? */
1255     U32             prefixlen;       /* #chars in common prefix */
1256     U32             statecount;      /* Build only - number of states in the states array 
1257                                         (including the unused zero state) */
1258     U32             wordcount;       /* Build only */
1259     U16             before_paren;
1260     U16             after_paren;
1261 #ifdef DEBUGGING
1262     STRLEN          charcount;       /* Build only */
1263 #endif
1264 };
1265 /* There is one (3 under DEBUGGING) pointers that logically belong in this
1266    structure, but are held outside as they need duplication on thread cloning,
1267    whereas the rest of the structure can be read only:
1268     HV              *widecharmap;    code points > 255 to charid
1269 #ifdef DEBUGGING
1270     AV              *words;          Array of words contained in trie, for dumping
1271     AV              *revcharmap;     Map of each charid back to its character representation
1272 #endif
1273 */
1274
1275 #define TRIE_WORDS_OFFSET 2
1276
1277 typedef struct _reg_trie_data reg_trie_data;
1278
1279 /* refcount is first in both this and _reg_trie_data to allow a space
1280    optimisation in Perl_regdupe.  */
1281 struct _reg_ac_data {
1282     U32              refcount;
1283     U32              trie;
1284     U32              *fail;
1285     reg_trie_state   *states;
1286 };
1287 typedef struct _reg_ac_data reg_ac_data;
1288
1289 /* ANY_BIT doesn't use the structure, so we can borrow it here.
1290    This is simpler than refactoring all of it as wed end up with
1291    three different sets... */
1292
1293 #define TRIE_BITMAP(p)          (((reg_trie_data *)(p))->bitmap)
1294 #define TRIE_BITMAP_BYTE(p, c)  BITMAP_BYTE(TRIE_BITMAP(p), c)
1295 #define TRIE_BITMAP_SET(p, c)   (TRIE_BITMAP_BYTE(p, c) |=  ANYOF_BIT((U8)c))
1296 #define TRIE_BITMAP_CLEAR(p,c)  (TRIE_BITMAP_BYTE(p, c) &= ~ANYOF_BIT((U8)c))
1297 #define TRIE_BITMAP_TEST(p, c)  (TRIE_BITMAP_BYTE(p, c) &   ANYOF_BIT((U8)c))
1298
1299 #define IS_ANYOF_TRIE(op) ((op)==TRIEC || (op)==AHOCORASICKC)
1300 #define IS_TRIE_AC(op) ((op)>=AHOCORASICK)
1301
1302 /* these defines assume uniquecharcount is the correct variable, and state may be evaluated twice */
1303 #define TRIE_NODENUM(state) (((state)-1)/(trie->uniquecharcount)+1)
1304 #define SAFE_TRIE_NODENUM(state) ((state) ? (((state)-1)/(trie->uniquecharcount)+1) : (state))
1305 #define TRIE_NODEIDX(state) ((state) ? (((state)-1)*(trie->uniquecharcount)+1) : (state))
1306
1307 #ifdef DEBUGGING
1308 #define TRIE_CHARCOUNT(trie) ((trie)->charcount)
1309 #else
1310 #define TRIE_CHARCOUNT(trie) (trie_charcount)
1311 #endif
1312
1313 #define RE_TRIE_MAXBUF_INIT 65536
1314 #define RE_TRIE_MAXBUF_NAME "\022E_TRIE_MAXBUF"
1315 #define RE_DEBUG_FLAGS "\022E_DEBUG_FLAGS"
1316
1317 #define RE_COMPILE_RECURSION_INIT 1000
1318 #define RE_COMPILE_RECURSION_LIMIT "\022E_COMPILE_RECURSION_LIMIT"
1319
1320 /*
1321
1322 RE_DEBUG_FLAGS is used to control what debug output is emitted
1323 its divided into three groups of options, some of which interact.
1324 The three groups are: Compile, Execute, Extra. There is room for a
1325 further group, as currently only the low three bytes are used.
1326
1327     Compile Options:
1328     
1329     PARSE
1330     PEEP
1331     TRIE
1332     PROGRAM
1333
1334     Execute Options:
1335
1336     INTUIT
1337     MATCH
1338     TRIE
1339
1340     Extra Options
1341
1342     TRIE
1343
1344 If you modify any of these make sure you make corresponding changes to
1345 re.pm, especially to the documentation.
1346
1347 */
1348
1349
1350 /* Compile */
1351 #define RE_DEBUG_COMPILE_MASK      0x0000FF
1352 #define RE_DEBUG_COMPILE_PARSE     0x000001
1353 #define RE_DEBUG_COMPILE_OPTIMISE  0x000002
1354 #define RE_DEBUG_COMPILE_TRIE      0x000004
1355 #define RE_DEBUG_COMPILE_DUMP      0x000008
1356 #define RE_DEBUG_COMPILE_FLAGS     0x000010
1357 #define RE_DEBUG_COMPILE_TEST      0x000020
1358
1359 /* Execute */
1360 #define RE_DEBUG_EXECUTE_MASK      0x00FF00
1361 #define RE_DEBUG_EXECUTE_INTUIT    0x000100
1362 #define RE_DEBUG_EXECUTE_MATCH     0x000200
1363 #define RE_DEBUG_EXECUTE_TRIE      0x000400
1364
1365 /* Extra */
1366 #define RE_DEBUG_EXTRA_MASK              0x3FF0000
1367 #define RE_DEBUG_EXTRA_TRIE              0x0010000
1368 #define RE_DEBUG_EXTRA_STATE             0x0080000
1369 #define RE_DEBUG_EXTRA_OPTIMISE          0x0100000
1370 #define RE_DEBUG_EXTRA_BUFFERS           0x0400000
1371 #define RE_DEBUG_EXTRA_GPOS              0x0800000
1372 #define RE_DEBUG_EXTRA_DUMP_PRE_OPTIMIZE 0x1000000
1373 #define RE_DEBUG_EXTRA_WILDCARD          0x2000000
1374 /* combined */
1375 #define RE_DEBUG_EXTRA_STACK             0x0280000
1376
1377 #define RE_DEBUG_FLAG(x) (re_debug_flags & (x))
1378 /* Compile */
1379 #define DEBUG_COMPILE_r(x) DEBUG_r( \
1380     if (DEBUG_v_TEST || RE_DEBUG_FLAG(RE_DEBUG_COMPILE_MASK)) x  )
1381 #define DEBUG_PARSE_r(x) DEBUG_r( \
1382     if (DEBUG_v_TEST || RE_DEBUG_FLAG(RE_DEBUG_COMPILE_PARSE)) x  )
1383 #define DEBUG_OPTIMISE_r(x) DEBUG_r( \
1384     if (DEBUG_v_TEST || RE_DEBUG_FLAG(RE_DEBUG_COMPILE_OPTIMISE)) x  )
1385 #define DEBUG_DUMP_r(x) DEBUG_r( \
1386     if (DEBUG_v_TEST || RE_DEBUG_FLAG(RE_DEBUG_COMPILE_DUMP)) x  )
1387 #define DEBUG_TRIE_COMPILE_r(x) DEBUG_r( \
1388     if (DEBUG_v_TEST || RE_DEBUG_FLAG(RE_DEBUG_COMPILE_TRIE)) x )
1389 #define DEBUG_FLAGS_r(x) DEBUG_r( \
1390     if (DEBUG_v_TEST || RE_DEBUG_FLAG(RE_DEBUG_COMPILE_FLAGS)) x )
1391 #define DEBUG_TEST_r(x) DEBUG_r( \
1392     if (DEBUG_v_TEST || RE_DEBUG_FLAG(RE_DEBUG_COMPILE_TEST)) x )
1393 /* Execute */
1394 #define DEBUG_EXECUTE_r(x) DEBUG_r( \
1395     if (DEBUG_v_TEST || RE_DEBUG_FLAG(RE_DEBUG_EXECUTE_MASK)) x  )
1396 #define DEBUG_INTUIT_r(x) DEBUG_r( \
1397     if (DEBUG_v_TEST || RE_DEBUG_FLAG(RE_DEBUG_EXECUTE_INTUIT)) x  )
1398 #define DEBUG_MATCH_r(x) DEBUG_r( \
1399     if (DEBUG_v_TEST || RE_DEBUG_FLAG(RE_DEBUG_EXECUTE_MATCH)) x  )
1400 #define DEBUG_TRIE_EXECUTE_r(x) DEBUG_r( \
1401     if (DEBUG_v_TEST || RE_DEBUG_FLAG(RE_DEBUG_EXECUTE_TRIE)) x )
1402
1403 /* Extra */
1404 #define DEBUG_EXTRA_r(x) DEBUG_r( \
1405     if (DEBUG_v_TEST || RE_DEBUG_FLAG(RE_DEBUG_EXTRA_MASK)) x  )
1406 #define DEBUG_STATE_r(x) DEBUG_r( \
1407     if (DEBUG_v_TEST || RE_DEBUG_FLAG(RE_DEBUG_EXTRA_STATE)) x )
1408 #define DEBUG_STACK_r(x) DEBUG_r( \
1409     if (DEBUG_v_TEST || RE_DEBUG_FLAG(RE_DEBUG_EXTRA_STACK)) x )
1410 #define DEBUG_BUFFERS_r(x) DEBUG_r( \
1411     if (DEBUG_v_TEST || RE_DEBUG_FLAG(RE_DEBUG_EXTRA_BUFFERS)) x )
1412
1413 #define DEBUG_OPTIMISE_MORE_r(x) DEBUG_r( \
1414     if (DEBUG_v_TEST || ((RE_DEBUG_EXTRA_OPTIMISE|RE_DEBUG_COMPILE_OPTIMISE) == \
1415          RE_DEBUG_FLAG(RE_DEBUG_EXTRA_OPTIMISE|RE_DEBUG_COMPILE_OPTIMISE))) x )
1416 #define DEBUG_TRIE_COMPILE_MORE_r(x) DEBUG_TRIE_COMPILE_r( \
1417     if (DEBUG_v_TEST || RE_DEBUG_FLAG(RE_DEBUG_EXTRA_TRIE)) x )
1418 #define DEBUG_TRIE_EXECUTE_MORE_r(x) DEBUG_TRIE_EXECUTE_r( \
1419     if (DEBUG_v_TEST || RE_DEBUG_FLAG(RE_DEBUG_EXTRA_TRIE)) x )
1420
1421 #define DEBUG_TRIE_r(x) DEBUG_r( \
1422     if (DEBUG_v_TEST || RE_DEBUG_FLAG(RE_DEBUG_COMPILE_TRIE \
1423         | RE_DEBUG_EXECUTE_TRIE )) x )
1424 #define DEBUG_GPOS_r(x) DEBUG_r( \
1425     if (DEBUG_v_TEST || RE_DEBUG_FLAG(RE_DEBUG_EXTRA_GPOS)) x )
1426
1427 #define DEBUG_DUMP_PRE_OPTIMIZE_r(x) DEBUG_r( \
1428     if (DEBUG_v_TEST || RE_DEBUG_FLAG(RE_DEBUG_EXTRA_DUMP_PRE_OPTIMIZE)) x )
1429
1430 /* initialization */
1431 /* Get the debug flags for code not in regcomp.c nor regexec.c.  This doesn't
1432  * initialize the variable if it isn't already there, instead it just assumes
1433  * the flags are 0 */
1434 #define DECLARE_AND_GET_RE_DEBUG_FLAGS_NON_REGEX                               \
1435     volatile IV re_debug_flags = 0;  PERL_UNUSED_VAR(re_debug_flags);          \
1436     STMT_START {                                                               \
1437         SV * re_debug_flags_sv = NULL;                                         \
1438                      /* get_sv() can return NULL during global destruction. */ \
1439         re_debug_flags_sv = PL_curcop ? get_sv(RE_DEBUG_FLAGS, GV_ADD) : NULL; \
1440         if (re_debug_flags_sv && SvIOK(re_debug_flags_sv))                     \
1441             re_debug_flags=SvIV(re_debug_flags_sv);                            \
1442     } STMT_END
1443
1444
1445 #ifdef DEBUGGING
1446
1447 /* For use in regcomp.c and regexec.c,  Get the debug flags, and initialize to
1448  * the defaults if not done already */
1449 #define DECLARE_AND_GET_RE_DEBUG_FLAGS                                         \
1450     volatile IV re_debug_flags = 0;  PERL_UNUSED_VAR(re_debug_flags);          \
1451     DEBUG_r({                              \
1452         SV * re_debug_flags_sv = NULL;                                         \
1453                      /* get_sv() can return NULL during global destruction. */ \
1454         re_debug_flags_sv = PL_curcop ? get_sv(RE_DEBUG_FLAGS, GV_ADD) : NULL; \
1455         if (re_debug_flags_sv) {                                               \
1456             if (!SvIOK(re_debug_flags_sv)) /* If doesn't exist set to default */\
1457                 sv_setuv(re_debug_flags_sv,                                    \
1458                         /* These defaults should be kept in sync with re.pm */ \
1459                             RE_DEBUG_COMPILE_DUMP | RE_DEBUG_EXECUTE_MASK );   \
1460             re_debug_flags=SvIV(re_debug_flags_sv);                            \
1461         }                                                                      \
1462     })
1463
1464 #define isDEBUG_WILDCARD (DEBUG_v_TEST || RE_DEBUG_FLAG(RE_DEBUG_EXTRA_WILDCARD))
1465
1466 #define RE_PV_COLOR_DECL(rpv,rlen,isuni,dsv,pv,l,m,c1,c2)   \
1467     const char * const rpv =                                \
1468         pv_pretty((dsv), (pv), (l), (m),                    \
1469             PL_colors[(c1)],PL_colors[(c2)],                \
1470             PERL_PV_ESCAPE_RE|PERL_PV_ESCAPE_NONASCII |((isuni) ? PERL_PV_ESCAPE_UNI : 0) );         \
1471     const int rlen = SvCUR(dsv)
1472
1473 /* This is currently unsed in the core */
1474 #define RE_SV_ESCAPE(rpv,isuni,dsv,sv,m)                            \
1475     const char * const rpv =                                        \
1476         pv_pretty((dsv), (SvPV_nolen_const(sv)), (SvCUR(sv)), (m),  \
1477             PL_colors[(c1)],PL_colors[(c2)],                        \
1478             PERL_PV_ESCAPE_RE|PERL_PV_ESCAPE_NONASCII |((isuni) ? PERL_PV_ESCAPE_UNI : 0) )
1479
1480 #define RE_PV_QUOTED_DECL(rpv,isuni,dsv,pv,l,m)                     \
1481     const char * const rpv =                                        \
1482         pv_pretty((dsv), (pv), (l), (m),                            \
1483             PL_colors[0], PL_colors[1],                             \
1484             ( PERL_PV_PRETTY_QUOTE | PERL_PV_ESCAPE_RE | PERL_PV_ESCAPE_NONASCII | PERL_PV_PRETTY_ELLIPSES | \
1485               ((isuni) ? PERL_PV_ESCAPE_UNI : 0))                  \
1486         )
1487
1488 #define RE_SV_DUMPLEN(ItEm) (SvCUR(ItEm) - (SvTAIL(ItEm)!=0))
1489 #define RE_SV_TAIL(ItEm) (SvTAIL(ItEm) ? "$" : "")
1490     
1491 #else /* if not DEBUGGING */
1492
1493 #define DECLARE_AND_GET_RE_DEBUG_FLAGS  dNOOP
1494 #define RE_PV_COLOR_DECL(rpv,rlen,isuni,dsv,pv,l,m,c1,c2)  dNOOP
1495 #define RE_SV_ESCAPE(rpv,isuni,dsv,sv,m)
1496 #define RE_PV_QUOTED_DECL(rpv,isuni,dsv,pv,l,m)  dNOOP
1497 #define RE_SV_DUMPLEN(ItEm)
1498 #define RE_SV_TAIL(ItEm)
1499 #define isDEBUG_WILDCARD 0
1500
1501 #endif /* DEBUG RELATED DEFINES */
1502
1503 #define FIRST_NON_ASCII_DECIMAL_DIGIT 0x660  /* ARABIC_INDIC_DIGIT_ZERO */
1504
1505 typedef enum {
1506         TRADITIONAL_BOUND = CC_WORDCHAR_,
1507         GCB_BOUND,
1508         LB_BOUND,
1509         SB_BOUND,
1510         WB_BOUND
1511 } bound_type;
1512
1513 /* This unpacks the FLAGS field of ANYOF[HR]x nodes.  The value it contains
1514  * gives the strict lower bound for the UTF-8 start byte of any code point
1515  * matchable by the node, and a loose upper bound as well.
1516  *
1517  * The low bound is stored as 0xC0 + ((the upper 6 bits) >> 2)
1518  * The loose upper bound is determined from the lowest 2 bits and the low bound
1519  * (called x) as follows:
1520  *
1521  * 11  The upper limit of the range can be as much as (EF - x) / 8
1522  * 10  The upper limit of the range can be as much as (EF - x) / 4
1523  * 01  The upper limit of the range can be as much as (EF - x) / 2
1524  * 00  The upper limit of the range can be as much as  EF
1525  *
1526  * For motivation of this design, see commit message in
1527  * 3146c00a633e9cbed741e10146662fbcedfdb8d3 */
1528 #ifdef EBCDIC
1529 #  define MAX_ANYOF_HRx_BYTE  0xF4
1530 #else
1531 #  define MAX_ANYOF_HRx_BYTE  0xEF
1532 #endif
1533 #define LOWEST_ANYOF_HRx_BYTE(b) (((b) >> 2) + 0xC0)
1534 #define HIGHEST_ANYOF_HRx_BYTE(b)                                           \
1535                                   (LOWEST_ANYOF_HRx_BYTE(b)                 \
1536           + ((MAX_ANYOF_HRx_BYTE - LOWEST_ANYOF_HRx_BYTE(b)) >> ((b) & 3)))
1537
1538 #if !defined(PERL_IN_XSUB_RE) || defined(PLUGGABLE_RE_EXTENSION)
1539 #  define GET_REGCLASS_AUX_DATA(a,b,c,d,e,f)  get_regclass_aux_data(a,b,c,d,e,f)
1540 #else
1541 #  define GET_REGCLASS_AUX_DATA(a,b,c,d,e,f)  get_re_gclass_aux_data(a,b,c,d,e,f)
1542 #endif
1543
1544 #define REGNODE_TYPE(node)              (PL_regnode_info[(node)].type)
1545 #define REGNODE_OFF_BY_ARG(node)        (PL_regnode_info[(node)].off_by_arg)
1546 #define REGNODE_ARG_LEN(node)           (PL_regnode_info[(node)].arg_len)
1547 #define REGNODE_ARG_LEN_VARIES(node)    (PL_regnode_info[(node)].arg_len_varies)
1548 #define REGNODE_NAME(node)              (PL_regnode_name[(node)])
1549
1550 #if defined(PERL_IN_REGEX_ENGINE)
1551 #include "reginline.h"
1552 #endif
1553
1554 #define EVAL_OPTIMISTIC_FLAG    128
1555 #define EVAL_FLAGS_MASK         (EVAL_OPTIMISTIC_FLAG-1)
1556
1557
1558
1559 #endif /* PERL_REGCOMP_H_ */
1560
1561 /*
1562  * ex: set ts=8 sts=4 sw=4 et:
1563  */