This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
locale.c: Rmv duplicate strlen()
[perl5.git] / regcomp.h
CommitLineData
a0d0e21e 1/* regcomp.h
d6376244 2 *
4bb101f2 3 * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
663f364b 4 * 2000, 2001, 2002, 2003, 2005, 2006, 2007, by Larry Wall and others
d6376244
JH
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 *
a687059c 9 */
3dd7db29 10
21d0beeb
KW
11#if ! defined(PERL_REGCOMP_H_) && ( defined(PERL_CORE) \
12 || defined(PERL_EXT_RE_BUILD))
12d173c9 13
3dd7db29
JK
14#define PERL_REGCOMP_H_
15
59db1942 16#ifndef RE_PESSIMISTIC_PARENS
b16c8aa5
YO
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 */
59db1942
YO
32#define RE_PESSIMISTIC_PARENS 0
33#endif
34
b16c8aa5
YO
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
e1d1eefb 72#include "regcharclass.h"
a687059c 73
58e23c8d 74/* Convert branch sequences to more efficient trie ops? */
07be1b83 75#define PERL_ENABLE_TRIE_OPTIMISATION 1
58e23c8d 76
486ec47a 77/* Be really aggressive about optimising patterns with trie sequences? */
07be1b83 78#define PERL_ENABLE_EXTENDED_TRIE_OPTIMISATION 1
58e23c8d
YO
79
80/* Should the optimiser take positive assertions into account? */
68ba3a3f 81#define PERL_ENABLE_POSITIVE_ASSERTION_STUDY 0
58e23c8d
YO
82
83/* Not for production use: */
07be1b83 84#define PERL_ENABLE_EXPERIMENTAL_REGEX_OPTIMISATIONS 0
58e23c8d 85
a687059c 86/*
a687059c
LW
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
a3621e74
YO
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.
a687059c
LW
101 */
102
a687059c
LW
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 *
e91177ed 113 * [The "next" pointer is always aligned on an even
62e6ef33 114 * boundary, and reads the offset directly as a short.]
a687059c 115 */
785a26d5
YO
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
c45b4541 120typedef struct regexp_internal {
6d5c990f
RGS
121 regnode *regstclass; /* Optional startclass as identified or constructed
122 by the optimiser */
85900e28 123 struct reg_data *data; /* Additional miscellaneous data used by the program.
6d5c990f
RGS
124 Used to make it easier to clone and free arbitrary
125 data that the regops need. Often the ARG field of
c45b4541
YO
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. */
1604cfb0 129 struct reg_code_blocks *code_blocks;/* positions of literal (?{}) */
c45b4541 130 U32 proglen; /* size of the compiled program in regnodes */
f08cf407
YO
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.*/
85900e28 135 regnode program[1]; /* Unwarranted chumminess with compiler. */
6d5c990f
RGS
136} regexp_internal;
137
85900e28 138#define RXi_SET(x,y) (x)->pprivate = (void*)(y)
6d5c990f
RGS
139#define RXi_GET(x) ((regexp_internal *)((x)->pprivate))
140#define RXi_GET_DECL(r,ri) regexp_internal *ri = RXi_GET(r)
c870f3e4 141#define RXi_GET_DECL_NULL(r,ri) regexp_internal *ri = (r) ? RXi_GET(r) : NULL
785a26d5
YO
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 */
e3e400ec
YO
148#define RXp_INTFLAGS(rx) ((rx)->intflags)
149#define RX_INTFLAGS(prog) RXp_INTFLAGS(ReANY(prog))
150
85900e28
YO
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'" */
e3e400ec
YO
157/* these used to be extflags, but are now intflags */
158#define PREGf_NOSCAN 0x00000040
33c28ab2 159 /* spare */
8e1490ee
YO
160#define PREGf_GPOS_SEEN 0x00000100
161#define PREGf_GPOS_FLOAT 0x00000200
785a26d5 162
d3d47aac
YO
163#define PREGf_ANCH_MBOL 0x00000400
164#define PREGf_ANCH_SBOL 0x00000800
165#define PREGf_ANCH_GPOS 0x00001000
d5a00e4a 166#define PREGf_RECURSE_SEEN 0x00002000
c224bbd5 167#define PREGf_PESSIMIZE_SEEN 0x00004000
8e1490ee 168
d3d47aac
YO
169#define PREGf_ANCH \
170 ( PREGf_ANCH_SBOL | PREGf_ANCH_GPOS | PREGf_ANCH_MBOL )
785a26d5
YO
171
172/* this is where the old regcomp.h started */
a687059c 173
24a3add9
YO
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 *
44eb4cdc
YO
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 *
24a3add9 194 * struct regnode {
44eb4cdc 195 * union regnode_head head;
24a3add9
YO
196 * };
197 *
44eb4cdc
YO
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.
24a3add9
YO
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
44eb4cdc
YO
233union regnode_arg {
234 I32 i32;
235 U32 u32;
236 struct {
237 U16 u16a;
238 U16 u16b;
239 } hi_lo;
240};
241
242
c277df42 243struct regnode_string {
44eb4cdc 244 union regnode_head head;
cd439c50 245 char string[1];
c277df42 246};
a687059c 247
ae06e581 248struct regnode_lstring { /* Constructed this way to keep the string aligned. */
44eb4cdc 249 union regnode_head head;
b292ecb4 250 U32 str_len_u32; /* Only 18 bits allowed before would overflow 'next_off' */
ae06e581
KW
251 char string[1];
252};
253
34924db0 254struct regnode_anyofhs { /* Constructed this way to keep the string aligned. */
44eb4cdc
YO
255 union regnode_head head;
256 union regnode_arg arg1;
34924db0
KW
257 char string[1];
258};
259
44eb4cdc
YO
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
17e3e02a
YO
262 * via ARG1u(), or two unsigned 16 bit values via ARG1a() or ARG1b()
263 */
c277df42 264struct regnode_1 {
44eb4cdc
YO
265 union regnode_head head;
266 union regnode_arg arg1;
c277df42
IZ
267};
268
d3e3ece8 269/* Node whose argument is 'SV *'. This needs to be used very carefully in
24a3add9
YO
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 * */
86451f01 284struct regnode_p {
44eb4cdc 285 union regnode_head head;
24a3add9 286 char arg1_sv_ptr_bytes[sizeof(SV *)];
86451f01
KW
287};
288
17e3e02a
YO
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() */
293struct regnode_2 {
44eb4cdc
YO
294 union regnode_head head;
295 union regnode_arg arg1;
296 union regnode_arg arg2;
6bda09f9
YO
297};
298
17e3e02a
YO
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. */
307struct regnode_3 {
44eb4cdc
YO
308 union regnode_head head;
309 union regnode_arg arg1;
310 union regnode_arg arg2;
311 union regnode_arg arg3;
05b13cf6
YO
312};
313
4c8c99df
KW
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. */
321struct regnode_bbm {
44eb4cdc 322 union regnode_head head;
4c8c99df
KW
323 U8 bitmap[REGNODE_BBM_BITMAP_LEN];
324};
325
85900e28 326#define ANYOF_BITMAP_SIZE (NUM_ANYOF_CODE_POINTS / CHARBITS)
936ed897 327
1ee208c4
KW
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. */
44eb4cdc 338
786e8c11 339/* also used by trie */
936ed897 340struct regnode_charclass {
44eb4cdc
YO
341 union regnode_head head;
342 union regnode_arg arg1;
85900e28 343 char bitmap[ANYOF_BITMAP_SIZE]; /* only compile-time */
936ed897
IZ
344};
345
bf3f2d85 346/* has runtime (locale) \d, \w, ..., [:posix:] classes */
ea461dd6 347struct regnode_charclass_posixl {
44eb4cdc
YO
348 union regnode_head head;
349 union regnode_arg arg1;
85900e28
YO
350 char bitmap[ANYOF_BITMAP_SIZE]; /* both compile-time ... */
351 U32 classflags; /* and run-time */
936ed897
IZ
352};
353
85c8e306 354/* A synthetic start class (SSC); is a regnode_charclass_posixl_fold, plus an
24a3add9
YO
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 */
b8f7bb16 367struct regnode_ssc {
44eb4cdc
YO
368 union regnode_head head;
369 union regnode_arg arg1;
85900e28
YO
370 char bitmap[ANYOF_BITMAP_SIZE]; /* both compile-time ... */
371 U32 classflags; /* ... and run-time */
85c8e306
KW
372
373 /* Auxiliary, only used during construction; NULL afterwards: list of code
374 * points matched */
375 SV* invlist;
b8f7bb16
KW
376};
377
71068078
KW
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. */
7f7274fa
YO
386#define set_ANYOF_SYNTHETIC(n) \
387 STMT_START{ \
388 OP(n) = ANYOF; \
389 NEXT_OFF(n) = 1; \
390 } STMT_END
391
20f4775e 392#define is_ANYOF_SYNTHETIC(n) (REGNODE_TYPE(OP(n)) == ANYOF && NEXT_OFF(n) == 1)
71068078 393
9c7e81e8
AD
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*/
0678333e
YO
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
9c7e81e8 402 smaller default.
1604cfb0 403 --Andy Dougherty 11 June 1998
0678333e 404 --Amended by Yves Orton 15 Jan 2023
9c7e81e8 405*/
0678333e 406#if INTSIZE > 4
9c7e81e8 407# ifndef REG_INFTY
0678333e 408# define REG_INFTY nBIT_IMAX(32)
9c7e81e8
AD
409# endif
410#endif
411
412#ifndef REG_INFTY
0678333e 413# define REG_INFTY I32_MAX
83cfe48c 414#endif
a687059c 415
e91177ed
IZ
416#define ARG_VALUE(arg) (arg)
417#define ARG__SET(arg,val) ((arg) = (val))
c277df42 418
e8e3b0fb
JH
419#undef ARG
420#undef ARG1
421#undef ARG2
422
17e3e02a
YO
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
24a3add9 446#define ARGp(p) ARGp_VALUE_inline(p)
17e3e02a
YO
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
24a3add9 463#define ARGp_SET(p, val) ARGp_SET_inline((p),(val))
c277df42 464
aa48e906
YO
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 */
a687059c 474
44eb4cdc 475#define NODE_ALIGN(node)
c277df42
IZ
476#define SIZE_ALIGN NODE_ALIGN
477
e8e3b0fb
JH
478#undef OP
479#undef OPERAND
e8e3b0fb 480#undef STRING
44eb4cdc
YO
481#undef NEXT_OFF
482#undef NODE_ALIGN
e8e3b0fb 483
44eb4cdc
YO
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 \
1604cfb0
MS
489 regnode types. For some, it's the \
490 character set of the regnode */
53175c60 491#define STR_LENs(p) (__ASSERT_(OP(p) != LEXACT && OP(p) != LEXACT_REQ8) \
44eb4cdc 492 STR_LEN_U8((struct regnode_string *)p))
53175c60 493#define STRINGs(p) (__ASSERT_(OP(p) != LEXACT && OP(p) != LEXACT_REQ8) \
5cd61b66 494 ((struct regnode_string *)p)->string)
53175c60 495#define OPERANDs(p) STRINGs(p)
ae06e581 496
17e3e02a 497#define PARNO(p) ARG1u(p) /* APPLIES for OPEN and CLOSE only */
d7c0b58c 498
44eb4cdc
YO
499#define NODE_ALIGN_FILL(node) (FLAGS(node) = 0)
500
ae06e581
KW
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. */
53175c60 513#define STR_LENl(p) (__ASSERT_(OP(p) == LEXACT || OP(p) == LEXACT_REQ8) \
b292ecb4 514 (((struct regnode_lstring *)p)->str_len_u32))
53175c60 515#define STRINGl(p) (__ASSERT_(OP(p) == LEXACT || OP(p) == LEXACT_REQ8) \
5cd61b66 516 (((struct regnode_lstring *)p)->string))
53175c60 517#define OPERANDl(p) STRINGl(p)
ae06e581 518
53175c60 519#define STR_LEN(p) ((OP(p) == LEXACT || OP(p) == LEXACT_REQ8) \
5cd61b66 520 ? STR_LENl(p) : STR_LENs(p))
53175c60 521#define STRING(p) ((OP(p) == LEXACT || OP(p) == LEXACT_REQ8) \
5cd61b66 522 ? STRINGl(p) : STRINGs(p))
53175c60 523#define OPERAND(p) STRING(p)
c5184715 524
c45abc0a 525/* The number of (smallest) regnode equivalents that a string of length l bytes
1db310d0 526 * occupies - Used by the REGNODE_AFTER() macros and functions. */
85900e28 527#define STR_SZ(l) (((l) + sizeof(regnode) - 1) / sizeof(regnode))
c45abc0a 528
c7fd9c67 529#define setSTR_LEN(p,v) \
ae06e581 530 STMT_START{ \
3f2416ae 531 if (OP(p) == LEXACT || OP(p) == LEXACT_REQ8) \
b292ecb4 532 ((struct regnode_lstring *)(p))->str_len_u32 = (v); \
ae06e581 533 else \
44eb4cdc 534 STR_LEN_U8((struct regnode_string *)(p)) = (v); \
ae06e581 535 } STMT_END
cd439c50 536
13fcf652 537#define ANYOFR_BASE_BITS 20
17e3e02a
YO
538#define ANYOFRbase(p) (ARG1u(p) & nBIT_MASK(ANYOFR_BASE_BITS))
539#define ANYOFRdelta(p) (ARG1u(p) >> ANYOFR_BASE_BITS)
13fcf652 540
e8e3b0fb
JH
541#undef NODE_ALIGN
542#undef ARG_LOC
e8e3b0fb 543
53175c60 544#define NODE_ALIGN(node)
24a3add9 545#define ARGp_BYTES_LOC(p) (((struct regnode_p *)p)->arg1_sv_ptr_bytes)
44eb4cdc
YO
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)
d7c0b58c 558
0e48b698
YO
559/* These should no longer be used directly in most cases. Please use
560 * the REGNODE_AFTER() macros instead. */
85900e28 561#define NODE_STEP_REGNODE 1 /* sizeof(regnode)/sizeof(regnode) */
e91177ed 562
182f0ba9
YO
563/* Core macros for computing "the regnode after this one". See also
564 * Perl_regnode_after() in reginline.h
0e48b698 565 *
182f0ba9
YO
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/>
0e48b698 572 *
182f0ba9
YO
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)
0e48b698 583 *
182f0ba9
YO
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()>.
0e48b698 591 *
182f0ba9
YO
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
493e6288 595 * may have been optimized out of the C<next_off> chain. An example
182f0ba9
YO
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.
0e48b698 603 *
182f0ba9
YO
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.
0e48b698 608 *
182f0ba9
YO
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.
0e48b698
YO
629 */
630#define REGNODE_AFTER_PLUS(p,extra) ((p) + NODE_STEP_REGNODE + (extra))
1db310d0 631/* under DEBUGGING we check that all REGNODE_AFTER optimized macros did the
0e48b698
YO
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
0e48b698
YO
640/* find the regnode after this p by using the opcode we previously extracted
641 * with OP(p) */
e28d2a35 642#define REGNODE_AFTER_opcode(p,op) REGNODE_AFTER_PLUS_DEBUG((p),REGNODE_ARG_LEN(op))
1db310d0 643
0e48b698 644/* find the regnode after this p by using the size of the struct associated with
1db310d0
YO
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))
19a5f8d3
YO
647
648/* find the regnode after this p by using OP(p) to find the regnode type of p */
1db310d0
YO
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)
19a5f8d3 653
0e48b698
YO
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)
c277df42 665
f55b7b26
KW
666#define FILL_NODE(offset, op) \
667 STMT_START { \
668 OP(REGNODE_p(offset)) = op; \
669 NEXT_OFF(REGNODE_p(offset)) = 0; \
d2a88850 670 } STMT_END
f55b7b26
KW
671#define FILL_ADVANCE_NODE(offset, op) \
672 STMT_START { \
673 FILL_NODE(offset, op); \
674 (offset)++; \
d2a88850 675 } STMT_END
17e3e02a 676#define FILL_ADVANCE_NODE_ARG1u(offset, op, arg) \
f55b7b26 677 STMT_START { \
17e3e02a 678 ARG1u_SET(REGNODE_p(offset), arg); \
f55b7b26
KW
679 FILL_ADVANCE_NODE(offset, op); \
680 /* This is used generically for other operations \
681 * that have a longer argument */ \
17e3e02a 682 (offset) += REGNODE_ARG_LEN(op); \
86451f01 683 } STMT_END
17e3e02a 684#define FILL_ADVANCE_NODE_ARGp(offset, op, arg) \
86451f01 685 STMT_START { \
17e3e02a 686 ARGp_SET(REGNODE_p(offset), arg); \
86451f01 687 FILL_ADVANCE_NODE(offset, op); \
17e3e02a 688 (offset) += REGNODE_ARG_LEN(op); \
93539530 689 } STMT_END
17e3e02a 690#define FILL_ADVANCE_NODE_2ui_ARG(offset, op, arg1, arg2) \
f55b7b26 691 STMT_START { \
17e3e02a
YO
692 ARG1u_SET(REGNODE_p(offset), arg1); \
693 ARG2i_SET(REGNODE_p(offset), arg2); \
f55b7b26
KW
694 FILL_ADVANCE_NODE(offset, op); \
695 (offset) += 2; \
93539530 696 } STMT_END
a687059c 697
24a3add9
YO
698/* define these after we define the normal macros, so we can use
699 * ARGp_BYTES_LOC(n) */
700
701static inline SV *
702ARGp_VALUE_inline(struct regnode *node) {
703 SV *ptr;
704 memcpy(&ptr, ARGp_BYTES_LOC(node), sizeof(ptr));
705
706 return ptr;
707}
708
709static inline void
710ARGp_SET_inline(struct regnode *node, SV *ptr) {
711 memcpy(ARGp_BYTES_LOC(node), &ptr, sizeof(ptr));
712}
713
22c35a8c 714#define REG_MAGIC 0234
a687059c 715
ff49def3
KW
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: */
bcdc9e1e
KW
730
731#define ANYOF_MATCHES_ALL_OUTSIDE_BITMAP_VALUE U32_MAX
732#define ANYOF_MATCHES_ALL_OUTSIDE_BITMAP(node) \
17e3e02a 733 (ARG1u(node) == ANYOF_MATCHES_ALL_OUTSIDE_BITMAP_VALUE)
bcdc9e1e
KW
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) \
17e3e02a 738 (ARG1u(node) == ANYOF_MATCHES_NONE_OUTSIDE_BITMAP_VALUE)
bcdc9e1e
KW
739
740#define ANYOF_ONLY_HAS_BITMAP_MASK ANYOF_MATCHES_NONE_OUTSIDE_BITMAP_VALUE
741#define ANYOF_ONLY_HAS_BITMAP(node) \
17e3e02a 742 ((ARG1u(node) & ANYOF_ONLY_HAS_BITMAP_MASK) == ANYOF_ONLY_HAS_BITMAP_MASK)
bcdc9e1e
KW
743
744#define ANYOF_HAS_AUX(node) (! ANYOF_ONLY_HAS_BITMAP(node))
137165a6 745
ff49def3
KW
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.
eafa4197 752 *
ff49def3
KW
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.
eafa4197
KW
778 *
779 * There are 5 cases where the bitmap is insufficient. These are specified by
ff49def3 780 * flags in the node's flags field. We could use five bits to represent the 5
eafa4197
KW
781 * cases, but to save flags bits (which are perennially in short supply), we
782 * play some games. The cases are:
d2f465e9 783 *
eafa4197
KW
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.
eafa4197
KW
786 *
787 * 2) Under /d rules, it can happen that code points that are in the upper
fe8d1b7c
KW
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
eafa4197 790 * example /[\w[:punct:]]/d. This happens only for certain posix classes,
ff49def3 791 * and all such ones also have above-bitmap matches.
eafa4197 792 *
d1c40ef5 793 * Note that /d rules are no longer encouraged; 'use 5.14' or higher
ff49def3
KW
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.
eafa4197
KW
797 *
798 * 3) Also under /d rules, something like /[\Wfoo]/ will match everything in
fe8d1b7c 799 * the \x80-\xFF range, unless the string being matched against is UTF-8.
7a94a5fe
KW
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:
ff49def3
KW
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 *
eafa4197 807 * 4) A user-defined \p{} property may not have been defined by the time the
fe8d1b7c
KW
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
6947c4eb
KW
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.
eafa4197
KW
817 *
818 * 5) /[foo]/il may have folds that are only valid if the runtime locale is a
6947c4eb
KW
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.
d2f465e9 825 *
eafa4197
KW
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.
bcdc9e1e 833 * At the moment, there are two spare flag bits, but this could be increased by
93539530 834 * various tricks:
e2f5e63d 835 *
bcdc9e1e
KW
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.
fe8d1b7c 840 *
46fc0c43 841 * If this is done, an extension would be to make all ANYOFL nodes contain the
6947c4eb
KW
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.
a4525e78 847 *
d1c40ef5 848 * Some flags are not used in synthetic start class (SSC) nodes, so could be
93e92956
KW
849 * shared should new flags be needed for SSCs, like SSC_MATCHES_EMPTY_STRING
850 * now. */
b8c5462f 851
d2f465e9
KW
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 */
85900e28 854#define ANYOF_INVERT 0x01
1984f6a0 855
a0dd4231 856/* For the SSC node only, which cannot be inverted, so is shared with that bit.
93e92956
KW
857 * This is used only during regex compilation. */
858#define SSC_MATCHES_EMPTY_STRING ANYOF_INVERT
a0dd4231 859
509d4de0
KW
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
b63f73da
KW
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
d2f465e9 869 * then. Only set under /l; never in an SSC */
037715a6 870#define ANYOFL_FOLD 0x04
b63f73da 871
6947c4eb
KW
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
d1c40ef5 875
1744d18d 876/* Spare: Be sure to change ANYOF_FLAGS_ALL if this gets used 0x10 */
c8d3cd88 877
bcdc9e1e 878/* Spare: Be sure to change ANYOF_FLAGS_ALL if this gets used 0x20 */
ef87b810 879
6947c4eb
KW
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
f240c685
KW
905
906/* Shared bit:
d2f465e9 907 * Under /d it means the ANYOFD node matches all non-ASCII Latin1
f240c685
KW
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
3dbd8b27
KW
913 * what is meant is the warning. The names are to make sure that you are
914 * cautioned about its shared nature */
3960d57a
KW
915#define ANYOFD_NON_UTF8_MATCHES_ALL_NON_ASCII__shared 0x80
916#define ANYOF_WARN_SUPER__shared 0x80
7bc66b18 917
85900e28 918#define ANYOF_FLAGS_ALL ((U8) ~(0x10|0x20))
653099ff 919
bcdc9e1e 920#define ANYOF_LOCALE_FLAGS ( ANYOFL_FOLD \
6947c4eb
KW
921 | ANYOF_MATCHES_POSIXL \
922 | ANYOFL_UTF8_LOCALE_REQD)
ace6b0e4 923
eff8b7dc
KW
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
d2f465e9 926 * the SSC, other of the flags may get added to it */
d1c40ef5 927#define ANYOF_COMMON_FLAGS 0
eff8b7dc 928
936ed897 929/* Character classes for node->classflags of ANYOF */
653099ff 930/* Should be synchronized with a table in regprop() */
575a8a29 931/* 2n should be the normal one, paired with its complement at 2n+1 */
b8c5462f 932
91456fff 933#define ANYOF_ALPHA ((CC_ALPHA_) * 2)
08ada29e 934#define ANYOF_NALPHA ((ANYOF_ALPHA) + 1)
91456fff 935#define ANYOF_ALPHANUMERIC ((CC_ALPHANUMERIC_) * 2) /* [[:alnum:]] isalnum(3), utf8::IsAlnum */
e8d596e0 936#define ANYOF_NALPHANUMERIC ((ANYOF_ALPHANUMERIC) + 1)
91456fff 937#define ANYOF_ASCII ((CC_ASCII_) * 2)
08ada29e 938#define ANYOF_NASCII ((ANYOF_ASCII) + 1)
91456fff 939#define ANYOF_BLANK ((CC_BLANK_) * 2) /* GNU extension: space and tab: non-vertical space */
e8d596e0 940#define ANYOF_NBLANK ((ANYOF_BLANK) + 1)
91456fff 941#define ANYOF_CASED ((CC_CASED_) * 2) /* Pseudo class for [:lower:] or
b0d691b2
KW
942 [:upper:] under /i */
943#define ANYOF_NCASED ((ANYOF_CASED) + 1)
91456fff 944#define ANYOF_CNTRL ((CC_CNTRL_) * 2)
08ada29e 945#define ANYOF_NCNTRL ((ANYOF_CNTRL) + 1)
91456fff 946#define ANYOF_DIGIT ((CC_DIGIT_) * 2) /* \d */
e8d596e0 947#define ANYOF_NDIGIT ((ANYOF_DIGIT) + 1)
91456fff 948#define ANYOF_GRAPH ((CC_GRAPH_) * 2)
08ada29e 949#define ANYOF_NGRAPH ((ANYOF_GRAPH) + 1)
91456fff 950#define ANYOF_LOWER ((CC_LOWER_) * 2)
08ada29e 951#define ANYOF_NLOWER ((ANYOF_LOWER) + 1)
91456fff 952#define ANYOF_PRINT ((CC_PRINT_) * 2)
08ada29e 953#define ANYOF_NPRINT ((ANYOF_PRINT) + 1)
91456fff 954#define ANYOF_PUNCT ((CC_PUNCT_) * 2)
08ada29e 955#define ANYOF_NPUNCT ((ANYOF_PUNCT) + 1)
91456fff 956#define ANYOF_SPACE ((CC_SPACE_) * 2) /* \s */
e8d596e0 957#define ANYOF_NSPACE ((ANYOF_SPACE) + 1)
91456fff 958#define ANYOF_UPPER ((CC_UPPER_) * 2)
08ada29e 959#define ANYOF_NUPPER ((ANYOF_UPPER) + 1)
91456fff 960#define ANYOF_WORDCHAR ((CC_WORDCHAR_) * 2) /* \w, PL_utf8_alnum, utf8::IsWord, ALNUM */
e8d596e0 961#define ANYOF_NWORDCHAR ((ANYOF_WORDCHAR) + 1)
91456fff 962#define ANYOF_XDIGIT ((CC_XDIGIT_) * 2)
08ada29e 963#define ANYOF_NXDIGIT ((ANYOF_XDIGIT) + 1)
b8c5462f 964
87cad4c0 965/* pseudo classes below this, not stored in the class bitmap, but used as flags
e1d1eefb
YO
966 during compilation of char classes */
967
91456fff 968#define ANYOF_VERTWS ((CC_VERTSPACE_) * 2)
b2ef2e34 969#define ANYOF_NVERTWS ((ANYOF_VERTWS)+1)
e1d1eefb 970
b2ef2e34
KW
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 */
91456fff
KW
973#if CC_VERTSPACE_ != HIGHEST_REGCOMP_DOT_H_SYNC_
974# error Problem with handy.h HIGHEST_REGCOMP_DOT_H_SYNC_ #define
b2ef2e34
KW
975#endif
976
8efd3f97
KW
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 */
91456fff 984# error Problem with handy.h CC_foo_ #defines
a0947d7b
KW
985#endif
986
85900e28
YO
987#define ANYOF_HORIZWS ((ANYOF_POSIXL_MAX)+2) /* = (ANYOF_NVERTWS + 1) */
988#define ANYOF_NHORIZWS ((ANYOF_POSIXL_MAX)+3)
87cad4c0 989
8efd3f97
KW
990#define ANYOF_UNIPROP ((ANYOF_POSIXL_MAX)+4) /* Used to indicate a Unicode
991 property: \p{} or \P{} */
f39a4dc2 992
b8c5462f
JH
993/* Backward source code compatibility. */
994
85900e28
YO
995#define ANYOF_ALNUML ANYOF_ALNUM
996#define ANYOF_NALNUML ANYOF_NALNUM
997#define ANYOF_SPACEL ANYOF_SPACE
998#define ANYOF_NSPACEL ANYOF_NSPACE
8ebfc29a
KW
999#define ANYOF_ALNUM ANYOF_WORDCHAR
1000#define ANYOF_NALNUM ANYOF_NWORDCHAR
b8c5462f
JH
1001
1002/* Utility macros for the bitmap and classes of ANYOF */
1003
85900e28
YO
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)))
ff37df4b 1007
44eb4cdc 1008#define ANYOF_FLAGS(p) (FLAGS(p))
b8c5462f 1009
85900e28 1010#define ANYOF_BIT(c) BITMAP_BIT(c)
b8c5462f 1011
ad494d15
KW
1012#define ANYOF_POSIXL_BITMAP(p) (((regnode_charclass_posixl*) (p))->classflags)
1013
85900e28
YO
1014#define POSIXL_SET(field, c) ((field) |= (1U << (c)))
1015#define ANYOF_POSIXL_SET(p, c) POSIXL_SET(ANYOF_POSIXL_BITMAP(p), (c))
8efd3f97 1016
14ed74f6 1017#define POSIXL_CLEAR(field, c) ((field) &= ~ (1U <<(c)))
ad494d15 1018#define ANYOF_POSIXL_CLEAR(p, c) POSIXL_CLEAR(ANYOF_POSIXL_BITMAP(p), (c))
8efd3f97 1019
85900e28
YO
1020#define POSIXL_TEST(field, c) ((field) & (1U << (c)))
1021#define ANYOF_POSIXL_TEST(p, c) POSIXL_TEST(ANYOF_POSIXL_BITMAP(p), (c))
b8c5462f 1022
85900e28
YO
1023#define POSIXL_ZERO(field) STMT_START { (field) = 0; } STMT_END
1024#define ANYOF_POSIXL_ZERO(ret) POSIXL_ZERO(ANYOF_POSIXL_BITMAP(ret))
14ed74f6
KW
1025
1026#define ANYOF_POSIXL_SET_TO_BITMAP(p, bits) \
ad494d15 1027 STMT_START { ANYOF_POSIXL_BITMAP(p) = (bits); } STMT_END
9cf78fa0
KW
1028
1029/* Shifts a bit to get, eg. 0x4000_0000, then subtracts 1 to get 0x3FFF_FFFF */
ad494d15
KW
1030#define ANYOF_POSIXL_SETALL(ret) \
1031 STMT_START { \
1032 ANYOF_POSIXL_BITMAP(ret) = nBIT_MASK(ANYOF_POSIXL_MAX); \
1033 } STMT_END
8efd3f97 1034#define ANYOF_CLASS_SETALL(ret) ANYOF_POSIXL_SETALL(ret)
9cf78fa0 1035
8efd3f97 1036#define ANYOF_POSIXL_TEST_ANY_SET(p) \
ad494d15 1037 ((ANYOF_FLAGS(p) & ANYOF_MATCHES_POSIXL) && ANYOF_POSIXL_BITMAP(p))
8efd3f97 1038#define ANYOF_CLASS_TEST_ANY_SET(p) ANYOF_POSIXL_TEST_ANY_SET(p)
e867ce45 1039
e0e1be5f
KW
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 \
d223e1ea 1046 == nBIT_MASK(ANYOF_POSIXL_MAX))
e0e1be5f
KW
1047
1048#define ANYOF_POSIXL_TEST_ALL_SET(p) \
d223e1ea 1049 ((ANYOF_FLAGS(p) & ANYOF_MATCHES_POSIXL) \
ad494d15 1050 && ANYOF_POSIXL_BITMAP(p) == nBIT_MASK(ANYOF_POSIXL_MAX))
fb38762f 1051
b27fd97a 1052#define ANYOF_POSIXL_OR(source, dest) STMT_START { (dest)->classflags |= (source)->classflags ; } STMT_END
8efd3f97 1053#define ANYOF_CLASS_OR(source, dest) ANYOF_POSIXL_OR((source), (dest))
936ed897 1054
cdd87c1d
KW
1055#define ANYOF_POSIXL_AND(source, dest) STMT_START { (dest)->classflags &= (source)->classflags ; } STMT_END
1056
85900e28
YO
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))
b8c5462f 1063
85900e28 1064#define ANYOF_BITMAP_SETALL(p) \
1604cfb0 1065 memset (ANYOF_BITMAP(p), 255, ANYOF_BITMAP_SIZE)
85900e28 1066#define ANYOF_BITMAP_CLEARALL(p) \
1604cfb0 1067 Zero (ANYOF_BITMAP(p), ANYOF_BITMAP_SIZE)
f8bef550 1068
a687059c
LW
1069/*
1070 * Utility definitions.
1071 */
2304df62 1072#ifndef CHARMASK
85900e28 1073# define UCHARAT(p) ((int)*(const U8*)(p))
a687059c 1074#else
85900e28 1075# define UCHARAT(p) ((int)*(p)&CHARMASK)
a687059c 1076#endif
a687059c 1077
c45abc0a
KW
1078/* Number of regnode equivalents that 'guy' occupies beyond the size of the
1079 * smallest regnode. */
c277df42
IZ
1080#define EXTRA_SIZE(guy) ((sizeof(guy)-1)/sizeof(struct regnode))
1081
e384d5c1
YO
1082#define REG_ZERO_LEN_SEEN 0x00000001
1083#define REG_LOOKBEHIND_SEEN 0x00000002
3c92cb14
YO
1084/* add a short form alias to keep the line length police happy */
1085#define REG_LB_SEEN REG_LOOKBEHIND_SEEN
e384d5c1 1086#define REG_GPOS_SEEN 0x00000004
471f5387 1087/* spare */
e384d5c1
YO
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
d5a00e4a 1094/* spare */
ee273784 1095#define REG_UNBOUNDED_QUANTIFIER_SEEN 0x00001000
c224bbd5 1096#define REG_PESSIMIZE_SEEN 0x00002000
ee273784 1097
d09b2d29 1098
73c4f7a1
GS
1099START_EXTERN_C
1100
be8e71aa
YO
1101#ifdef PLUGGABLE_RE_EXTENSION
1102#include "re_nodes.h"
1103#else
d09b2d29 1104#include "regnodes.h"
be8e71aa 1105#endif
d09b2d29 1106
f9f4320a
YO
1107#ifndef PLUGGABLE_RE_EXTENSION
1108#ifndef DOINIT
1109EXTCONST regexp_engine PL_core_reg_engine;
1110#else /* DOINIT */
85900e28 1111EXTCONST regexp_engine PL_core_reg_engine = {
fe578d7f 1112 Perl_re_compile,
2fdbfb4d 1113 Perl_regexec_flags,
f9f4320a 1114 Perl_re_intuit_start,
85900e28 1115 Perl_re_intuit_string,
2fdbfb4d
AB
1116 Perl_regfree_internal,
1117 Perl_reg_numbered_buff_fetch,
1118 Perl_reg_numbered_buff_store,
1119 Perl_reg_numbered_buff_length,
192b9cd1
AB
1120 Perl_reg_named_buff,
1121 Perl_reg_named_buff_iter,
49d7dfbc 1122 Perl_reg_qr_package,
85900e28 1123#if defined(USE_ITHREADS)
3c13cae6 1124 Perl_regdupe_internal,
85900e28 1125#endif
3c13cae6 1126 Perl_re_op_compile
f9f4320a
YO
1127};
1128#endif /* DOINIT */
1129#endif /* PLUGGABLE_RE_EXTENSION */
1130
1131
73c4f7a1 1132END_EXTERN_C
cad2e5aa 1133
cad2e5aa 1134
261faec3
MJD
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:
af534a04 1137 * a - AV for paren_name_list under DEBUGGING
0111c4fd 1138 * f - start-class data for regstclass optimization
68e2671b 1139 * l - start op for literal (?{EVAL}) item
d63c20f2 1140 * L - start op for literal (?{EVAL}) item, with separate CV (qr//)
b30fcab9 1141 * r - pointer to an embedded code-containing qr, e.g. /ab$qr/
7a94a5fe
KW
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
a3621e74 1145 * t - trie struct
55eed653 1146 * u - trie struct's widecharmap (a HV, so can't share, must dup)
2b8b4781 1147 * also used for revcharmap and words under DEBUGGING
07be1b83 1148 * T - aho-trie struct
81714fb9 1149 * S - sv for named capture lookup
261faec3 1150 * 20010712 mjd@plover.com
c3613ed7 1151 * (Remember to update re_dup() and pregfree() if you add any items.)
261faec3 1152 */
cad2e5aa
JH
1153struct reg_data {
1154 U32 count;
1155 U8 *what;
1156 void* data[1];
1157};
1158
a1cac82e
NC
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. */
cad2e5aa 1161#define anchored_substr substrs->data[0].substr
33b8afdf 1162#define anchored_utf8 substrs->data[0].utf8_substr
cad2e5aa 1163#define anchored_offset substrs->data[0].min_offset
1de06328
YO
1164#define anchored_end_shift substrs->data[0].end_shift
1165
cad2e5aa 1166#define float_substr substrs->data[1].substr
33b8afdf 1167#define float_utf8 substrs->data[1].utf8_substr
cad2e5aa
JH
1168#define float_min_offset substrs->data[1].min_offset
1169#define float_max_offset substrs->data[1].max_offset
1de06328
YO
1170#define float_end_shift substrs->data[1].end_shift
1171
cad2e5aa 1172#define check_substr substrs->data[2].substr
33b8afdf 1173#define check_utf8 substrs->data[2].utf8_substr
cad2e5aa
JH
1174#define check_offset_min substrs->data[2].min_offset
1175#define check_offset_max substrs->data[2].max_offset
1de06328 1176#define check_end_shift substrs->data[2].end_shift
a3621e74 1177
85900e28
YO
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)
a3621e74
YO
1182
1183/* trie related stuff */
5d9a96ca 1184
a3621e74
YO
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*/
1192struct _reg_trie_trans {
1193 U32 next;
1194 U32 check;
1195};
1196
1197/* a transition list element for the list based representation */
1198struct _reg_trie_trans_list_elem {
1199 U16 forid;
1200 U32 newstate;
1201};
1202typedef 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*/
1209struct _reg_trie_state {
1210 U16 wordnum;
1211 union {
1212 U32 base;
1213 reg_trie_trans_le* list;
1214 } trans;
1215};
1216
2e64971a
DM
1217/* info per word; indexed by wordnum */
1218typedef struct {
85900e28 1219 U16 prev; /* previous word in acceptance chain; eg in
1604cfb0
MS
1220 * zzz|abc|ab/ after matching the chars abc, the
1221 * accepted word is #2, and the previous accepted
1222 * word is #3 */
85900e28
YO
1223 U32 len; /* how many chars long is this word? */
1224 U32 accept; /* accept state for this word */
2e64971a 1225} reg_trie_wordinfo;
a3621e74
YO
1226
1227
a3621e74
YO
1228typedef struct _reg_trie_state reg_trie_state;
1229typedef struct _reg_trie_trans reg_trie_trans;
1230
1231
1232/* anything in here that needs to be freed later
23eab42c
NC
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. */
a3621e74 1236struct _reg_trie_data {
23eab42c 1237 U32 refcount; /* number of times this trie is referenced */
786e8c11
YO
1238 U32 lasttrans; /* last valid transition element */
1239 U16 *charmap; /* byte to charid lookup array */
786e8c11
YO
1240 reg_trie_state *states; /* state data */
1241 reg_trie_trans *trans; /* array of transition elements */
1242 char *bitmap; /* stclass bitmap */
85900e28 1243 U16 *jump; /* optional 1 indexed array of offsets before tail
786e8c11 1244 for the node following a given word. */
acababb4
YO
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
2e64971a 1250 reg_trie_wordinfo *wordinfo; /* array of info per word */
1b5c067c
NC
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? */
2e64971a 1255 U32 prefixlen; /* #chars in common prefix */
85900e28 1256 U32 statecount; /* Build only - number of states in the states array
1e2e3d02 1257 (including the unused zero state) */
786e8c11 1258 U32 wordcount; /* Build only */
acababb4
YO
1259 U16 before_paren;
1260 U16 after_paren;
a3621e74 1261#ifdef DEBUGGING
786e8c11 1262 STRLEN charcount; /* Build only */
a3621e74
YO
1263#endif
1264};
2b8b4781
NC
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
a3621e74
YO
1277typedef struct _reg_trie_data reg_trie_data;
1278
23eab42c
NC
1279/* refcount is first in both this and _reg_trie_data to allow a space
1280 optimisation in Perl_regdupe. */
07be1b83 1281struct _reg_ac_data {
23eab42c 1282 U32 refcount;
1b5c067c 1283 U32 trie;
07be1b83
YO
1284 U32 *fail;
1285 reg_trie_state *states;
07be1b83
YO
1286};
1287typedef struct _reg_ac_data reg_ac_data;
1288
486ec47a 1289/* ANY_BIT doesn't use the structure, so we can borrow it here.
3dab1dad
YO
1290 This is simpler than refactoring all of it as wed end up with
1291 three different sets... */
1292
85900e28
YO
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))
3dab1dad 1298
1de06328
YO
1299#define IS_ANYOF_TRIE(op) ((op)==TRIEC || (op)==AHOCORASICKC)
1300#define IS_TRIE_AC(op) ((op)>=AHOCORASICK)
1301
a3621e74
YO
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
3dab1dad 1307#ifdef DEBUGGING
3dab1dad 1308#define TRIE_CHARCOUNT(trie) ((trie)->charcount)
3dab1dad 1309#else
3dab1dad 1310#define TRIE_CHARCOUNT(trie) (trie_charcount)
3dab1dad 1311#endif
a3621e74 1312
0111c4fd
RGS
1313#define RE_TRIE_MAXBUF_INIT 65536
1314#define RE_TRIE_MAXBUF_NAME "\022E_TRIE_MAXBUF"
a3621e74
YO
1315#define RE_DEBUG_FLAGS "\022E_DEBUG_FLAGS"
1316
6ef7fe53
KW
1317#define RE_COMPILE_RECURSION_INIT 1000
1318#define RE_COMPILE_RECURSION_LIMIT "\022E_COMPILE_RECURSION_LIMIT"
1319
be8e71aa
YO
1320/*
1321
1322RE_DEBUG_FLAGS is used to control what debug output is emitted
1323its divided into three groups of options, some of which interact.
1324The three groups are: Compile, Execute, Extra. There is room for a
1325further group, as currently only the low three bytes are used.
1326
1327 Compile Options:
85900e28 1328
be8e71aa
YO
1329 PARSE
1330 PEEP
1331 TRIE
1332 PROGRAM
a3621e74 1333
be8e71aa 1334 Execute Options:
a3621e74 1335
be8e71aa
YO
1336 INTUIT
1337 MATCH
1338 TRIE
a3621e74 1339
be8e71aa 1340 Extra Options
a3621e74 1341
be8e71aa 1342 TRIE
be8e71aa
YO
1343
1344If you modify any of these make sure you make corresponding changes to
1345re.pm, especially to the documentation.
a3621e74 1346
be8e71aa
YO
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
f7819f85 1356#define RE_DEBUG_COMPILE_FLAGS 0x000010
d9a72fcc 1357#define RE_DEBUG_COMPILE_TEST 0x000020
be8e71aa
YO
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 */
51bd1941 1366#define RE_DEBUG_EXTRA_MASK 0x3FF0000
fd41b2d1 1367#define RE_DEBUG_EXTRA_TRIE 0x0010000
fd41b2d1
KW
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
51bd1941 1373#define RE_DEBUG_EXTRA_WILDCARD 0x2000000
24b23f37 1374/* combined */
fd41b2d1 1375#define RE_DEBUG_EXTRA_STACK 0x0280000
be8e71aa 1376
55a0b497 1377#define RE_DEBUG_FLAG(x) (re_debug_flags & (x))
be8e71aa
YO
1378/* Compile */
1379#define DEBUG_COMPILE_r(x) DEBUG_r( \
55a0b497 1380 if (DEBUG_v_TEST || RE_DEBUG_FLAG(RE_DEBUG_COMPILE_MASK)) x )
be8e71aa 1381#define DEBUG_PARSE_r(x) DEBUG_r( \
55a0b497 1382 if (DEBUG_v_TEST || RE_DEBUG_FLAG(RE_DEBUG_COMPILE_PARSE)) x )
be8e71aa 1383#define DEBUG_OPTIMISE_r(x) DEBUG_r( \
55a0b497 1384 if (DEBUG_v_TEST || RE_DEBUG_FLAG(RE_DEBUG_COMPILE_OPTIMISE)) x )
be8e71aa 1385#define DEBUG_DUMP_r(x) DEBUG_r( \
55a0b497 1386 if (DEBUG_v_TEST || RE_DEBUG_FLAG(RE_DEBUG_COMPILE_DUMP)) x )
be8e71aa 1387#define DEBUG_TRIE_COMPILE_r(x) DEBUG_r( \
55a0b497 1388 if (DEBUG_v_TEST || RE_DEBUG_FLAG(RE_DEBUG_COMPILE_TRIE)) x )
f7819f85 1389#define DEBUG_FLAGS_r(x) DEBUG_r( \
55a0b497 1390 if (DEBUG_v_TEST || RE_DEBUG_FLAG(RE_DEBUG_COMPILE_FLAGS)) x )
d9a72fcc 1391#define DEBUG_TEST_r(x) DEBUG_r( \
55a0b497 1392 if (DEBUG_v_TEST || RE_DEBUG_FLAG(RE_DEBUG_COMPILE_TEST)) x )
be8e71aa
YO
1393/* Execute */
1394#define DEBUG_EXECUTE_r(x) DEBUG_r( \
55a0b497 1395 if (DEBUG_v_TEST || RE_DEBUG_FLAG(RE_DEBUG_EXECUTE_MASK)) x )
be8e71aa 1396#define DEBUG_INTUIT_r(x) DEBUG_r( \
55a0b497 1397 if (DEBUG_v_TEST || RE_DEBUG_FLAG(RE_DEBUG_EXECUTE_INTUIT)) x )
be8e71aa 1398#define DEBUG_MATCH_r(x) DEBUG_r( \
55a0b497 1399 if (DEBUG_v_TEST || RE_DEBUG_FLAG(RE_DEBUG_EXECUTE_MATCH)) x )
be8e71aa 1400#define DEBUG_TRIE_EXECUTE_r(x) DEBUG_r( \
55a0b497 1401 if (DEBUG_v_TEST || RE_DEBUG_FLAG(RE_DEBUG_EXECUTE_TRIE)) x )
be8e71aa
YO
1402
1403/* Extra */
1404#define DEBUG_EXTRA_r(x) DEBUG_r( \
55a0b497 1405 if (DEBUG_v_TEST || RE_DEBUG_FLAG(RE_DEBUG_EXTRA_MASK)) x )
ab3bbdeb 1406#define DEBUG_STATE_r(x) DEBUG_r( \
55a0b497 1407 if (DEBUG_v_TEST || RE_DEBUG_FLAG(RE_DEBUG_EXTRA_STATE)) x )
24b23f37 1408#define DEBUG_STACK_r(x) DEBUG_r( \
55a0b497 1409 if (DEBUG_v_TEST || RE_DEBUG_FLAG(RE_DEBUG_EXTRA_STACK)) x )
e7707071 1410#define DEBUG_BUFFERS_r(x) DEBUG_r( \
55a0b497 1411 if (DEBUG_v_TEST || RE_DEBUG_FLAG(RE_DEBUG_EXTRA_BUFFERS)) x )
e7707071 1412
a5ca303d 1413#define DEBUG_OPTIMISE_MORE_r(x) DEBUG_r( \
dafb2544 1414 if (DEBUG_v_TEST || ((RE_DEBUG_EXTRA_OPTIMISE|RE_DEBUG_COMPILE_OPTIMISE) == \
55a0b497 1415 RE_DEBUG_FLAG(RE_DEBUG_EXTRA_OPTIMISE|RE_DEBUG_COMPILE_OPTIMISE))) x )
be8e71aa 1416#define DEBUG_TRIE_COMPILE_MORE_r(x) DEBUG_TRIE_COMPILE_r( \
55a0b497 1417 if (DEBUG_v_TEST || RE_DEBUG_FLAG(RE_DEBUG_EXTRA_TRIE)) x )
be8e71aa 1418#define DEBUG_TRIE_EXECUTE_MORE_r(x) DEBUG_TRIE_EXECUTE_r( \
55a0b497 1419 if (DEBUG_v_TEST || RE_DEBUG_FLAG(RE_DEBUG_EXTRA_TRIE)) x )
be8e71aa
YO
1420
1421#define DEBUG_TRIE_r(x) DEBUG_r( \
55a0b497
KW
1422 if (DEBUG_v_TEST || RE_DEBUG_FLAG(RE_DEBUG_COMPILE_TRIE \
1423 | RE_DEBUG_EXECUTE_TRIE )) x )
2c296965 1424#define DEBUG_GPOS_r(x) DEBUG_r( \
55a0b497 1425 if (DEBUG_v_TEST || RE_DEBUG_FLAG(RE_DEBUG_EXTRA_GPOS)) x )
be8e71aa 1426
fd41b2d1 1427#define DEBUG_DUMP_PRE_OPTIMIZE_r(x) DEBUG_r( \
55a0b497 1428 if (DEBUG_v_TEST || RE_DEBUG_FLAG(RE_DEBUG_EXTRA_DUMP_PRE_OPTIMIZE)) x )
fd41b2d1 1429
be8e71aa 1430/* initialization */
1fe1d354
KW
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
a3621e74 1445#ifdef DEBUGGING
ab3bbdeb 1446
1fe1d354
KW
1447/* For use in regcomp.c and regexec.c, Get the debug flags, and initialize to
1448 * the defaults if not done already */
619d7873
KW
1449#define DECLARE_AND_GET_RE_DEBUG_FLAGS \
1450 volatile IV re_debug_flags = 0; PERL_UNUSED_VAR(re_debug_flags); \
64901762 1451 DEBUG_r({ \
619d7873
KW
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) { \
0c6362ad 1456 if (!SvIOK(re_debug_flags_sv)) /* If doesn't exist set to default */\
619d7873
KW
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 } \
64901762 1462 })
ab3bbdeb 1463
51bd1941
KW
1464#define isDEBUG_WILDCARD (DEBUG_v_TEST || RE_DEBUG_FLAG(RE_DEBUG_EXTRA_WILDCARD))
1465
eecd4d11
KW
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)], \
c89df6cf 1470 PERL_PV_ESCAPE_RE|PERL_PV_ESCAPE_NONASCII |((isuni) ? PERL_PV_ESCAPE_UNI : 0) ); \
0df25f3d 1471 const int rlen = SvCUR(dsv)
ab3bbdeb 1472
8ec77c3c 1473/* This is currently unsed in the core */
eecd4d11
KW
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)], \
c89df6cf 1478 PERL_PV_ESCAPE_RE|PERL_PV_ESCAPE_NONASCII |((isuni) ? PERL_PV_ESCAPE_UNI : 0) )
ab3bbdeb 1479
eecd4d11
KW
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], \
c89df6cf 1484 ( PERL_PV_PRETTY_QUOTE | PERL_PV_ESCAPE_RE | PERL_PV_ESCAPE_NONASCII | PERL_PV_PRETTY_ELLIPSES | \
ab3bbdeb 1485 ((isuni) ? PERL_PV_ESCAPE_UNI : 0)) \
0a0e9afc 1486 )
ab3bbdeb
YO
1487
1488#define RE_SV_DUMPLEN(ItEm) (SvCUR(ItEm) - (SvTAIL(ItEm)!=0))
1489#define RE_SV_TAIL(ItEm) (SvTAIL(ItEm) ? "$" : "")
85900e28 1490
ab3bbdeb
YO
1491#else /* if not DEBUGGING */
1492
82dc0782
KW
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
ab3bbdeb 1495#define RE_SV_ESCAPE(rpv,isuni,dsv,sv,m)
82dc0782 1496#define RE_PV_QUOTED_DECL(rpv,isuni,dsv,pv,l,m) dNOOP
ab3bbdeb
YO
1497#define RE_SV_DUMPLEN(ItEm)
1498#define RE_SV_TAIL(ItEm)
51bd1941 1499#define isDEBUG_WILDCARD 0
ab3bbdeb
YO
1500
1501#endif /* DEBUG RELATED DEFINES */
a3621e74 1502
0bc0e44f 1503#define FIRST_NON_ASCII_DECIMAL_DIGIT 0x660 /* ARABIC_INDIC_DIGIT_ZERO */
2ca74f27 1504
64935bc6 1505typedef enum {
91456fff 1506 TRADITIONAL_BOUND = CC_WORDCHAR_,
1604cfb0
MS
1507 GCB_BOUND,
1508 LB_BOUND,
1509 SB_BOUND,
1510 WB_BOUND
64935bc6
KW
1511} bound_type;
1512
13fcf652 1513/* This unpacks the FLAGS field of ANYOF[HR]x nodes. The value it contains
3146c00a
KW
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 *
1a737818 1517 * The low bound is stored as 0xC0 + ((the upper 6 bits) >> 2)
3146c00a
KW
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 *
0bf4efd7
KW
1526 * For motivation of this design, see commit message in
1527 * 3146c00a633e9cbed741e10146662fbcedfdb8d3 */
3146c00a
KW
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
93900cf0
KW
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
12d173c9
YO
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)])
20f4775e 1549
85900e28 1550#if defined(PERL_IN_REGEX_ENGINE)
1db310d0
YO
1551#include "reginline.h"
1552#endif
1553
c224bbd5
YO
1554#define EVAL_OPTIMISTIC_FLAG 128
1555#define EVAL_FLAGS_MASK (EVAL_OPTIMISTIC_FLAG-1)
1556
ba6e2c38
YO
1557/* We define PERL_RE_BUILD_DEBUG if we are NOT compiling the re extension and
1558 * we are under DEBUGGING, or if we are ARE compiling the re extension
1559 * and this is not a DEBUGGING enabled build (identified by
1560 * DEBUGGING_RE_ONLY being defined)
1561 */
1562#if ( defined(USE_DYNAMIC_LOADING) && defined(DEBUGGING)) || \
1563 ( defined(PERL_EXT_RE_BUILD) && defined(DEBUGGING_RE_ONLY)) || \
1564 (!defined(PERL_EXT_RE_BUILD) && defined(DEBUGGING))
1565#define PERL_RE_BUILD_DEBUG
1566#endif
1567#if ( defined(USE_DYNAMIC_LOADING) || !defined(PERL_EXT_RE_BUILD) )
1568#define PERL_RE_BUILD_AUX
1569#endif
59db1942 1570
3dd7db29
JK
1571#endif /* PERL_REGCOMP_H_ */
1572
e9a8c099 1573/*
14d04a33 1574 * ex: set ts=8 sts=4 sw=4 et:
e9a8c099 1575 */