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