1 /*************************************************
2 * Perl-Compatible Regular Expressions *
3 *************************************************/
5 /* PCRE is a library of functions to support regular expressions whose syntax
6 and semantics are as close as possible to those of the Perl 5 language.
8 Written by Philip Hazel
9 Original API code Copyright (c) 1997-2012 University of Cambridge
10 New API code Copyright (c) 2016-2018 University of Cambridge
12 -----------------------------------------------------------------------------
13 Redistribution and use in source and binary forms, with or without
14 modification, are permitted provided that the following conditions are met:
16 * Redistributions of source code must retain the above copyright notice,
17 this list of conditions and the following disclaimer.
19 * Redistributions in binary form must reproduce the above copyright
20 notice, this list of conditions and the following disclaimer in the
21 documentation and/or other materials provided with the distribution.
23 * Neither the name of the University of Cambridge nor the names of its
24 contributors may be used to endorse or promote products derived from
25 this software without specific prior written permission.
27 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
28 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
31 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 POSSIBILITY OF SUCH DAMAGE.
38 -----------------------------------------------------------------------------
45 #include "pcre2_internal.h"
49 /* All-in-one: Since we use the JIT compiler only from here,
50 we just include it. This way we don't need to touch the build
53 #define SLJIT_CONFIG_AUTO 1
54 #define SLJIT_CONFIG_STATIC 1
55 #define SLJIT_VERBOSE 0
63 #define SLJIT_MALLOC(size, allocator_data) pcre2_jit_malloc(size, allocator_data)
64 #define SLJIT_FREE(ptr, allocator_data) pcre2_jit_free(ptr, allocator_data)
66 static void * pcre2_jit_malloc(size_t size, void *allocator_data)
68 pcre2_memctl *allocator = ((pcre2_memctl*)allocator_data);
69 return allocator->malloc(size, allocator->memory_data);
72 static void pcre2_jit_free(void *ptr, void *allocator_data)
74 pcre2_memctl *allocator = ((pcre2_memctl*)allocator_data);
75 allocator->free(ptr, allocator->memory_data);
78 #include "sljit/sljitLir.c"
80 #if defined SLJIT_CONFIG_UNSUPPORTED && SLJIT_CONFIG_UNSUPPORTED
81 #error Unsupported architecture
84 /* Defines for debugging purposes. */
86 /* 1 - Use unoptimized capturing brackets.
87 2 - Enable capture_last_ptr (includes option 1). */
88 /* #define DEBUG_FORCE_UNOPTIMIZED_CBRAS 2 */
90 /* 1 - Always have a control head. */
91 /* #define DEBUG_FORCE_CONTROL_HEAD 1 */
93 /* Allocate memory for the regex stack on the real machine stack.
94 Fast, but limited size. */
95 #define MACHINE_STACK_SIZE 32768
97 /* Growth rate for stack allocated by the OS. Should be the multiply
99 #define STACK_GROWTH_RATE 8192
101 /* Enable to check that the allocation could destroy temporaries. */
102 #if defined SLJIT_DEBUG && SLJIT_DEBUG
103 #define DESTROY_REGISTERS 1
107 Short summary about the backtracking mechanism empolyed by the jit code generator:
109 The code generator follows the recursive nature of the PERL compatible regular
110 expressions. The basic blocks of regular expressions are condition checkers
111 whose execute different commands depending on the result of the condition check.
112 The relationship between the operators can be horizontal (concatenation) and
113 vertical (sub-expression) (See struct backtrack_common for more details).
115 'ab' - 'a' and 'b' regexps are concatenated
116 'a+' - 'a' is the sub-expression of the '+' operator
118 The condition checkers are boolean (true/false) checkers. Machine code is generated
119 for the checker itself and for the actions depending on the result of the checker.
120 The 'true' case is called as the matching path (expected path), and the other is called as
121 the 'backtrack' path. Branch instructions are expesive for all CPUs, so we avoid taken
122 branches on the matching path.
124 Greedy star operator (*) :
125 Matching path: match happens.
126 Backtrack path: match failed.
127 Non-greedy star operator (*?) :
128 Matching path: no need to perform a match.
129 Backtrack path: match is required.
131 The following example shows how the code generated for a capturing bracket
132 with two alternatives. Let A, B, C, D are arbirary regular expressions, and
133 we have the following regular expression:
137 The generated code will be the following:
140 '(' matching path (pushing arguments to the stack)
142 ')' matching path (pushing arguments to the stack)
144 return with successful match
147 ')' backtrack path (If we arrived from "C" jump to the backtrack of "C")
150 jump to D matching path
154 Notice, that the order of backtrack code paths are the opposite of the fast
155 code paths. In this way the topmost value on the stack is always belong
156 to the current backtrack code path. The backtrack path must check
157 whether there is a next alternative. If so, it needs to jump back to
158 the matching path eventually. Otherwise it needs to clear out its own stack
159 frame and continue the execution on the backtrack code paths.
165 Atomic blocks and asserts require reloading the values of private data
166 when the backtrack mechanism performed. Because of OP_RECURSE, the data
167 are not necessarly known in compile time, thus we need a dynamic restore
170 The stack frames are stored in a chain list, and have the following format:
171 ([ capturing bracket offset ][ start value ][ end value ])+ ... [ 0 ] [ previous head ]
173 Thus we can restore the private data to a particular point in the stack.
176 typedef struct jit_arguments {
177 /* Pointers first. */
178 struct sljit_stack *stack;
182 pcre2_match_data *match_data;
183 PCRE2_SPTR startchar_ptr;
184 PCRE2_UCHAR *mark_ptr;
185 int (*callout)(pcre2_callout_block *, void *);
187 /* Everything else after. */
188 sljit_uw offset_limit;
189 sljit_u32 limit_match;
194 #define JIT_NUMBER_OF_COMPILE_MODES 3
196 typedef struct executable_functions {
197 void *executable_funcs[JIT_NUMBER_OF_COMPILE_MODES];
198 void *read_only_data_heads[JIT_NUMBER_OF_COMPILE_MODES];
199 sljit_uw executable_sizes[JIT_NUMBER_OF_COMPILE_MODES];
200 sljit_u32 top_bracket;
201 sljit_u32 limit_match;
202 } executable_functions;
204 typedef struct jump_list {
205 struct sljit_jump *jump;
206 struct jump_list *next;
209 typedef struct stub_list {
210 struct sljit_jump *start;
211 struct sljit_label *quit;
212 struct stub_list *next;
215 typedef struct label_addr_list {
216 struct sljit_label *label;
217 sljit_uw *update_addr;
218 struct label_addr_list *next;
231 typedef int (SLJIT_FUNC *jit_function)(jit_arguments *args);
233 /* The following structure is the key data type for the recursive
234 code generator. It is allocated by compile_matchingpath, and contains
235 the arguments for compile_backtrackingpath. Must be the first member
236 of its descendants. */
237 typedef struct backtrack_common {
238 /* Concatenation stack. */
239 struct backtrack_common *prev;
240 jump_list *nextbacktracks;
241 /* Internal stack (for component operators). */
242 struct backtrack_common *top;
243 jump_list *topbacktracks;
244 /* Opcode pointer. */
248 typedef struct assert_backtrack {
249 backtrack_common common;
250 jump_list *condfailed;
251 /* Less than 0 if a frame is not needed. */
253 /* Points to our private memory word on the stack. */
254 int private_data_ptr;
256 struct sljit_label *matchingpath;
259 typedef struct bracket_backtrack {
260 backtrack_common common;
261 /* Where to coninue if an alternative is successfully matched. */
262 struct sljit_label *alternative_matchingpath;
263 /* For rmin and rmax iterators. */
264 struct sljit_label *recursive_matchingpath;
265 /* For greedy ? operator. */
266 struct sljit_label *zero_matchingpath;
267 /* Contains the branches of a failed condition. */
269 /* Both for OP_COND, OP_SCOND. */
270 jump_list *condfailed;
271 assert_backtrack *assert;
272 /* For OP_ONCE. Less than 0 if not needed. */
275 /* Points to our private memory word on the stack. */
276 int private_data_ptr;
279 typedef struct bracketpos_backtrack {
280 backtrack_common common;
281 /* Points to our private memory word on the stack. */
282 int private_data_ptr;
283 /* Reverting stack is needed. */
285 /* Allocated stack size. */
287 } bracketpos_backtrack;
289 typedef struct braminzero_backtrack {
290 backtrack_common common;
291 struct sljit_label *matchingpath;
292 } braminzero_backtrack;
294 typedef struct char_iterator_backtrack {
295 backtrack_common common;
296 /* Next iteration. */
297 struct sljit_label *matchingpath;
299 jump_list *backtracks;
301 unsigned int othercasebit;
306 } char_iterator_backtrack;
308 typedef struct ref_iterator_backtrack {
309 backtrack_common common;
310 /* Next iteration. */
311 struct sljit_label *matchingpath;
312 } ref_iterator_backtrack;
314 typedef struct recurse_entry {
315 struct recurse_entry *next;
316 /* Contains the function entry label. */
317 struct sljit_label *entry_label;
318 /* Contains the function entry label. */
319 struct sljit_label *backtrack_label;
320 /* Collects the entry calls until the function is not created. */
321 jump_list *entry_calls;
322 /* Collects the backtrack calls until the function is not created. */
323 jump_list *backtrack_calls;
324 /* Points to the starting opcode. */
328 typedef struct recurse_backtrack {
329 backtrack_common common;
330 /* Return to the matching path. */
331 struct sljit_label *matchingpath;
332 /* Recursive pattern. */
333 recurse_entry *entry;
334 /* Pattern is inlined. */
335 BOOL inlined_pattern;
338 #define OP_THEN_TRAP OP_TABLE_LENGTH
340 typedef struct then_trap_backtrack {
341 backtrack_common common;
342 /* If then_trap is not NULL, this structure contains the real
343 then_trap for the backtracking path. */
344 struct then_trap_backtrack *then_trap;
345 /* Points to the starting opcode. */
347 /* Exit point for the then opcodes of this alternative. */
349 /* Frame size of the current alternative. */
351 } then_trap_backtrack;
353 #define MAX_N_CHARS 12
354 #define MAX_DIFF_CHARS 5
356 typedef struct fast_forward_char_data {
357 /* Number of characters in the chars array, 255 for any character. */
359 /* Number of last UTF-8 characters in the chars array. */
361 /* Available characters in the current position. */
362 PCRE2_UCHAR chars[MAX_DIFF_CHARS];
363 } fast_forward_char_data;
365 #define MAX_CLASS_RANGE_SIZE 4
366 #define MAX_CLASS_CHARS_SIZE 3
368 typedef struct compiler_common {
369 /* The sljit ceneric compiler. */
370 struct sljit_compiler *compiler;
371 /* Compiled regular expression. */
373 /* First byte code. */
375 /* Maps private data offset to each opcode. */
376 sljit_s32 *private_data_ptrs;
377 /* Chain list of read-only data ptrs. */
378 void *read_only_data_head;
379 /* Tells whether the capturing bracket is optimized. */
380 sljit_u8 *optimized_cbracket;
381 /* Tells whether the starting offset is a target of then. */
382 sljit_u8 *then_offsets;
383 /* Current position where a THEN must jump. */
384 then_trap_backtrack *then_trap;
385 /* Starting offset of private data for capturing brackets. */
387 /* Output vector starting point. Must be divisible by 2. */
388 sljit_s32 ovector_start;
389 /* Points to the starting character of the current match. */
391 /* Last known position of the requested byte. */
392 sljit_s32 req_char_ptr;
393 /* Head of the last recursion. */
394 sljit_s32 recursive_head_ptr;
395 /* First inspected character for partial matching.
396 (Needed for avoiding zero length partial matches.) */
397 sljit_s32 start_used_ptr;
398 /* Starting pointer for partial soft matches. */
400 /* Pointer of the match end position. */
401 sljit_s32 match_end_ptr;
402 /* Points to the marked string. */
404 /* Recursive control verb management chain. */
405 sljit_s32 control_head_ptr;
406 /* Points to the last matched capture block index. */
407 sljit_s32 capture_last_ptr;
408 /* Fast forward skipping byte code pointer. */
409 PCRE2_SPTR fast_forward_bc_ptr;
410 /* Locals used by fast fail optimization. */
411 sljit_s32 fast_fail_start_ptr;
412 sljit_s32 fast_fail_end_ptr;
414 /* Flipped and lower case tables. */
417 /* Mode can be PCRE2_JIT_COMPLETE and others. */
419 /* TRUE, when minlength is greater than 0. */
421 /* \K is found in the pattern. */
423 /* (*SKIP:arg) is found in the pattern. */
425 /* (*THEN) is found in the pattern. */
427 /* (*SKIP) or (*SKIP:arg) is found in lookbehind assertion. */
428 BOOL has_skip_in_assert_back;
429 /* Quit is redirected by recurse, negative assertion, or positive assertion in conditional block. */
430 BOOL local_quit_available;
431 /* Currently in a positive assertion. */
432 BOOL in_positive_assertion;
433 /* Newline control. */
441 /* Dollar endonly. */
445 /* Named capturing brackets. */
446 PCRE2_SPTR name_table;
448 sljit_sw name_entry_size;
450 /* Labels and jump lists. */
451 struct sljit_label *partialmatchlabel;
452 struct sljit_label *quit_label;
453 struct sljit_label *abort_label;
454 struct sljit_label *accept_label;
455 struct sljit_label *ff_newline_shortcut;
457 label_addr_list *label_addrs;
458 recurse_entry *entries;
459 recurse_entry *currententry;
460 jump_list *partialmatch;
462 jump_list *positive_assertion_quit;
464 jump_list *failed_match;
466 jump_list *calllimit;
467 jump_list *stackalloc;
468 jump_list *revertframes;
469 jump_list *wordboundary;
470 jump_list *anynewline;
473 jump_list *casefulcmp;
474 jump_list *caselesscmp;
475 jump_list *reset_match;
478 #ifdef SUPPORT_UNICODE
482 #if PCRE2_CODE_UNIT_WIDTH == 8
483 jump_list *utfreadchar;
484 jump_list *utfreadchar16;
485 jump_list *utfreadtype8;
487 #endif /* SUPPORT_UNICODE */
490 /* For byte_sequence_compare. */
492 typedef struct compare_context {
495 #if defined SLJIT_UNALIGNED && SLJIT_UNALIGNED
500 #if PCRE2_CODE_UNIT_WIDTH == 8
502 sljit_u8 asuchars[4];
503 #elif PCRE2_CODE_UNIT_WIDTH == 16
504 sljit_u16 asuchars[2];
505 #elif PCRE2_CODE_UNIT_WIDTH == 32
506 sljit_u32 asuchars[1];
512 #if PCRE2_CODE_UNIT_WIDTH == 8
514 sljit_u8 asuchars[4];
515 #elif PCRE2_CODE_UNIT_WIDTH == 16
516 sljit_u16 asuchars[2];
517 #elif PCRE2_CODE_UNIT_WIDTH == 32
518 sljit_u32 asuchars[1];
524 /* Undefine sljit macros. */
527 /* Used for accessing the elements of the stack. */
528 #define STACK(i) ((i) * (int)sizeof(sljit_sw))
530 #ifdef SLJIT_PREF_SHIFT_REG
531 #if SLJIT_PREF_SHIFT_REG == SLJIT_R2
533 #elif SLJIT_PREF_SHIFT_REG == SLJIT_R3
534 #define SHIFT_REG_IS_R3
536 #error "Unsupported shift register"
540 #define TMP1 SLJIT_R0
541 #ifdef SHIFT_REG_IS_R3
542 #define TMP2 SLJIT_R3
543 #define TMP3 SLJIT_R2
545 #define TMP2 SLJIT_R2
546 #define TMP3 SLJIT_R3
548 #define STR_PTR SLJIT_R1
549 #define STR_END SLJIT_S0
550 #define STACK_TOP SLJIT_S1
551 #define STACK_LIMIT SLJIT_S2
552 #define COUNT_MATCH SLJIT_S3
553 #define ARGUMENTS SLJIT_S4
554 #define RETURN_ADDR SLJIT_R4
556 /* Local space layout. */
557 /* These two locals can be used by the current opcode. */
558 #define LOCALS0 (0 * sizeof(sljit_sw))
559 #define LOCALS1 (1 * sizeof(sljit_sw))
560 /* Two local variables for possessive quantifiers (char1 cannot use them). */
561 #define POSSESSIVE0 (2 * sizeof(sljit_sw))
562 #define POSSESSIVE1 (3 * sizeof(sljit_sw))
563 /* Max limit of recursions. */
564 #define LIMIT_MATCH (4 * sizeof(sljit_sw))
565 /* The output vector is stored on the stack, and contains pointers
566 to characters. The vector data is divided into two groups: the first
567 group contains the start / end character pointers, and the second is
568 the start pointers when the end of the capturing group has not yet reached. */
569 #define OVECTOR_START (common->ovector_start)
570 #define OVECTOR(i) (OVECTOR_START + (i) * (sljit_sw)sizeof(sljit_sw))
571 #define OVECTOR_PRIV(i) (common->cbra_ptr + (i) * (sljit_sw)sizeof(sljit_sw))
572 #define PRIVATE_DATA(cc) (common->private_data_ptrs[(cc) - common->start])
574 #if PCRE2_CODE_UNIT_WIDTH == 8
575 #define MOV_UCHAR SLJIT_MOV_U8
576 #define IN_UCHARS(x) (x)
577 #elif PCRE2_CODE_UNIT_WIDTH == 16
578 #define MOV_UCHAR SLJIT_MOV_U16
579 #define UCHAR_SHIFT (1)
580 #define IN_UCHARS(x) ((x) * 2)
581 #elif PCRE2_CODE_UNIT_WIDTH == 32
582 #define MOV_UCHAR SLJIT_MOV_U32
583 #define UCHAR_SHIFT (2)
584 #define IN_UCHARS(x) ((x) * 4)
586 #error Unsupported compiling mode
590 #define DEFINE_COMPILER \
591 struct sljit_compiler *compiler = common->compiler
592 #define OP1(op, dst, dstw, src, srcw) \
593 sljit_emit_op1(compiler, (op), (dst), (dstw), (src), (srcw))
594 #define OP2(op, dst, dstw, src1, src1w, src2, src2w) \
595 sljit_emit_op2(compiler, (op), (dst), (dstw), (src1), (src1w), (src2), (src2w))
597 sljit_emit_label(compiler)
599 sljit_emit_jump(compiler, (type))
600 #define JUMPTO(type, label) \
601 sljit_set_label(sljit_emit_jump(compiler, (type)), (label))
602 #define JUMPHERE(jump) \
603 sljit_set_label((jump), sljit_emit_label(compiler))
604 #define SET_LABEL(jump, label) \
605 sljit_set_label((jump), (label))
606 #define CMP(type, src1, src1w, src2, src2w) \
607 sljit_emit_cmp(compiler, (type), (src1), (src1w), (src2), (src2w))
608 #define CMPTO(type, src1, src1w, src2, src2w, label) \
609 sljit_set_label(sljit_emit_cmp(compiler, (type), (src1), (src1w), (src2), (src2w)), (label))
610 #define OP_FLAGS(op, dst, dstw, type) \
611 sljit_emit_op_flags(compiler, (op), (dst), (dstw), (type))
612 #define CMOV(type, dst_reg, src, srcw) \
613 sljit_emit_cmov(compiler, (type), (dst_reg), (src), (srcw))
614 #define GET_LOCAL_BASE(dst, dstw, offset) \
615 sljit_get_local_base(compiler, (dst), (dstw), (offset))
617 #define READ_CHAR_MAX 0x7fffffff
619 #define INVALID_UTF_CHAR 888
621 static PCRE2_SPTR bracketend(PCRE2_SPTR cc)
623 SLJIT_ASSERT((*cc >= OP_ASSERT && *cc <= OP_ASSERTBACK_NOT) || (*cc >= OP_ONCE && *cc <= OP_SCOND));
624 do cc += GET(cc, 1); while (*cc == OP_ALT);
625 SLJIT_ASSERT(*cc >= OP_KET && *cc <= OP_KETRPOS);
630 static int no_alternatives(PCRE2_SPTR cc)
633 SLJIT_ASSERT((*cc >= OP_ASSERT && *cc <= OP_ASSERTBACK_NOT) || (*cc >= OP_ONCE && *cc <= OP_SCOND));
639 while (*cc == OP_ALT);
640 SLJIT_ASSERT(*cc >= OP_KET && *cc <= OP_KETRPOS);
644 /* Functions whose might need modification for all new supported opcodes:
647 set_private_data_ptrs
650 get_recurse_data_length
653 compile_backtrackingpath
656 static PCRE2_SPTR next_opcode(compiler_common *common, PCRE2_SPTR cc)
658 SLJIT_UNUSED_ARG(common);
664 case OP_NOT_WORD_BOUNDARY:
665 case OP_WORD_BOUNDARY:
668 case OP_NOT_WHITESPACE:
670 case OP_NOT_WORDCHAR:
717 case OP_ASSERTBACK_NOT:
744 case OP_ASSERT_ACCEPT:
747 return cc + PRIV(OP_lengths)[*cc];
797 case OP_NOTMINQUERYI:
803 case OP_NOTPOSQUERYI:
805 cc += PRIV(OP_lengths)[*cc];
806 #ifdef SUPPORT_UNICODE
807 if (common->utf && HAS_EXTRALEN(cc[-1])) cc += GET_EXTRALEN(cc[-1]);
817 case OP_TYPEMINQUERY:
823 case OP_TYPEPOSQUERY:
825 return cc + PRIV(OP_lengths)[*cc] - 1;
828 #ifdef SUPPORT_UNICODE
829 if (common->utf) return NULL;
834 return cc + GET(cc, 1 + 2*LINK_SIZE);
836 #if defined SUPPORT_UNICODE || PCRE2_CODE_UNIT_WIDTH != 8
838 return cc + GET(cc, 1);
846 return cc + 1 + 2 + cc[1];
849 /* All opcodes are supported now! */
855 static BOOL check_opcode_types(compiler_common *common, PCRE2_SPTR cc, PCRE2_SPTR ccend)
859 PCRE2_SPTR assert_back_end = cc - 1;
861 /* Calculate important variables (like stack size) and checks whether all opcodes are supported. */
867 common->has_set_som = TRUE;
868 common->might_be_empty = TRUE;
874 common->optimized_cbracket[GET2(cc, 1)] = 0;
880 common->optimized_cbracket[GET2(cc, 1 + LINK_SIZE)] = 0;
881 cc += 1 + LINK_SIZE + IMM2_SIZE;
886 /* Only AUTO_CALLOUT can insert this opcode. We do
887 not intend to support this case. */
888 if (cc[1 + LINK_SIZE] == OP_CALLOUT || cc[1 + LINK_SIZE] == OP_CALLOUT_STR)
894 common->optimized_cbracket[GET2(cc, 1)] = 0;
901 count = GET2(cc, 1 + IMM2_SIZE);
902 slot = common->name_table + GET2(cc, 1) * common->name_entry_size;
905 common->optimized_cbracket[GET2(slot, 0)] = 0;
906 slot += common->name_entry_size;
908 cc += 1 + 2 * IMM2_SIZE;
912 /* Set its value only once. */
913 if (common->recursive_head_ptr == 0)
915 common->recursive_head_ptr = common->ovector_start;
916 common->ovector_start += sizeof(sljit_sw);
923 if (common->capture_last_ptr == 0)
925 common->capture_last_ptr = common->ovector_start;
926 common->ovector_start += sizeof(sljit_sw);
928 cc += (*cc == OP_CALLOUT) ? PRIV(OP_lengths)[OP_CALLOUT] : GET(cc, 1 + 2*LINK_SIZE);
932 slot = bracketend(cc);
933 if (slot > assert_back_end)
934 assert_back_end = slot;
939 common->has_then = TRUE;
940 common->control_head_ptr = 1;
946 if (common->mark_ptr == 0)
948 common->mark_ptr = common->ovector_start;
949 common->ovector_start += sizeof(sljit_sw);
955 common->has_then = TRUE;
956 common->control_head_ptr = 1;
961 if (cc < assert_back_end)
962 common->has_skip_in_assert_back = TRUE;
967 common->control_head_ptr = 1;
968 common->has_skip_arg = TRUE;
969 if (cc < assert_back_end)
970 common->has_skip_in_assert_back = TRUE;
975 cc = next_opcode(common, cc);
984 static BOOL is_accelerated_repeat(PCRE2_SPTR cc)
994 return (cc[1] != OP_ANYNL && cc[1] != OP_EXTUNI);
1018 case OP_NOTMINSTARI:
1020 case OP_NOTMINPLUSI:
1021 case OP_NOTPOSSTARI:
1022 case OP_NOTPOSPLUSI:
1027 #if defined SUPPORT_UNICODE || PCRE2_CODE_UNIT_WIDTH != 8
1029 cc += (*cc == OP_XCLASS) ? GET(cc, 1) : (int)(1 + (32 / sizeof(PCRE2_UCHAR)));
1031 cc += (1 + (32 / sizeof(PCRE2_UCHAR)));
1049 static SLJIT_INLINE BOOL detect_fast_forward_skip(compiler_common *common, int *private_data_start)
1051 PCRE2_SPTR cc = common->start;
1054 /* Skip not repeated brackets. */
1062 case OP_NOT_WORD_BOUNDARY:
1063 case OP_WORD_BOUNDARY:
1070 /* Zero width assertions. */
1075 if (*cc != OP_BRA && *cc != OP_CBRA)
1078 end = cc + GET(cc, 1);
1079 if (*end != OP_KET || PRIVATE_DATA(end) != 0)
1083 if (common->optimized_cbracket[GET2(cc, 1 + LINK_SIZE)] == 0)
1087 cc += 1 + LINK_SIZE;
1090 if (is_accelerated_repeat(cc))
1092 common->fast_forward_bc_ptr = cc;
1093 common->private_data_ptrs[(cc + 1) - common->start] = *private_data_start;
1094 *private_data_start += sizeof(sljit_sw);
1100 static SLJIT_INLINE void detect_fast_fail(compiler_common *common, PCRE2_SPTR cc, int *private_data_start, sljit_s32 depth)
1102 PCRE2_SPTR next_alt;
1104 SLJIT_ASSERT(*cc == OP_BRA || *cc == OP_CBRA);
1106 if (*cc == OP_CBRA && common->optimized_cbracket[GET2(cc, 1 + LINK_SIZE)] == 0)
1109 next_alt = bracketend(cc) - (1 + LINK_SIZE);
1110 if (*next_alt != OP_KET || PRIVATE_DATA(next_alt) != 0)
1115 next_alt = cc + GET(cc, 1);
1117 cc += 1 + LINK_SIZE + ((*cc == OP_CBRA) ? IMM2_SIZE : 0);
1126 case OP_NOT_WORD_BOUNDARY:
1127 case OP_WORD_BOUNDARY:
1134 /* Zero width assertions. */
1141 if (depth > 0 && (*cc == OP_BRA || *cc == OP_CBRA))
1142 detect_fast_fail(common, cc, private_data_start, depth - 1);
1144 if (is_accelerated_repeat(cc))
1146 common->private_data_ptrs[(cc + 1) - common->start] = *private_data_start;
1148 if (common->fast_fail_start_ptr == 0)
1149 common->fast_fail_start_ptr = *private_data_start;
1151 *private_data_start += sizeof(sljit_sw);
1152 common->fast_fail_end_ptr = *private_data_start;
1154 if (*private_data_start > SLJIT_MAX_LOCAL_SIZE)
1160 while (*cc == OP_ALT);
1163 static int get_class_iterator_size(PCRE2_SPTR cc)
1182 max = GET2(cc, 1 + IMM2_SIZE);
1184 return (*cc == OP_CRRANGE) ? 2 : 1;
1195 static BOOL detect_repeat(compiler_common *common, PCRE2_SPTR begin)
1197 PCRE2_SPTR end = bracketend(begin);
1199 PCRE2_SPTR next_end;
1202 sljit_sw length = end - begin;
1203 sljit_s32 min, max, i;
1205 /* Detect fixed iterations first. */
1206 if (end[-(1 + LINK_SIZE)] != OP_KET)
1209 /* Already detected repeat. */
1210 if (common->private_data_ptrs[end - common->start - LINK_SIZE] != 0)
1217 if (*next != *begin)
1219 next_end = bracketend(next);
1220 if (next_end - next != length || memcmp(begin, next, IN_UCHARS(length)) != 0)
1231 if (*next == OP_BRAZERO || *next == OP_BRAMINZERO)
1236 if (next[0] != type || next[1] != OP_BRA || next[2 + LINK_SIZE] != *begin)
1238 next_end = bracketend(next + 2 + LINK_SIZE);
1239 if (next_end - next != (length + 2 + LINK_SIZE) || memcmp(begin, next + 2 + LINK_SIZE, IN_UCHARS(length)) != 0)
1245 if (next[0] == type && next[1] == *begin && max >= 1)
1247 next_end = bracketend(next + 1);
1248 if (next_end - next == (length + 1) && memcmp(begin, next + 1, IN_UCHARS(length)) == 0)
1250 for (i = 0; i < max; i++, next_end += 1 + LINK_SIZE)
1251 if (*next_end != OP_KET)
1256 common->private_data_ptrs[max_end - common->start - LINK_SIZE] = next_end - max_end;
1257 common->private_data_ptrs[max_end - common->start - LINK_SIZE + 1] = (type == OP_BRAZERO) ? OP_UPTO : OP_MINUPTO;
1258 /* +2 the original and the last. */
1259 common->private_data_ptrs[max_end - common->start - LINK_SIZE + 2] = max + 2;
1263 max_end -= (1 + LINK_SIZE) + GET(max_end, -LINK_SIZE);
1271 common->private_data_ptrs[end - common->start - LINK_SIZE] = max_end - end;
1272 common->private_data_ptrs[end - common->start - LINK_SIZE + 1] = OP_EXACT;
1273 common->private_data_ptrs[end - common->start - LINK_SIZE + 2] = min;
1280 #define CASE_ITERATOR_PRIVATE_DATA_1 \
1288 case OP_MINQUERYI: \
1289 case OP_NOTMINSTAR: \
1290 case OP_NOTMINPLUS: \
1292 case OP_NOTMINQUERY: \
1293 case OP_NOTMINSTARI: \
1294 case OP_NOTMINPLUSI: \
1295 case OP_NOTQUERYI: \
1296 case OP_NOTMINQUERYI:
1298 #define CASE_ITERATOR_PRIVATE_DATA_2A \
1308 #define CASE_ITERATOR_PRIVATE_DATA_2B \
1314 case OP_NOTMINUPTO: \
1316 case OP_NOTMINUPTOI:
1318 #define CASE_ITERATOR_TYPE_PRIVATE_DATA_1 \
1319 case OP_TYPEMINSTAR: \
1320 case OP_TYPEMINPLUS: \
1321 case OP_TYPEQUERY: \
1322 case OP_TYPEMINQUERY:
1324 #define CASE_ITERATOR_TYPE_PRIVATE_DATA_2A \
1328 #define CASE_ITERATOR_TYPE_PRIVATE_DATA_2B \
1330 case OP_TYPEMINUPTO:
1332 static void set_private_data_ptrs(compiler_common *common, int *private_data_start, PCRE2_SPTR ccend)
1334 PCRE2_SPTR cc = common->start;
1335 PCRE2_SPTR alternative;
1336 PCRE2_SPTR end = NULL;
1337 int private_data_ptr = *private_data_start;
1338 int space, size, bracketlen;
1339 BOOL repeat_check = TRUE;
1346 if (private_data_ptr > SLJIT_MAX_LOCAL_SIZE)
1349 if (repeat_check && (*cc == OP_ONCE || *cc == OP_BRA || *cc == OP_CBRA || *cc == OP_COND))
1351 if (detect_repeat(common, cc))
1353 /* These brackets are converted to repeats, so no global
1354 based single character repeat is allowed. */
1356 end = bracketend(cc);
1359 repeat_check = TRUE;
1364 if (common->private_data_ptrs[cc + 1 - common->start] != 0)
1366 common->private_data_ptrs[cc - common->start] = private_data_ptr;
1367 private_data_ptr += sizeof(sljit_sw);
1368 cc += common->private_data_ptrs[cc + 1 - common->start];
1370 cc += 1 + LINK_SIZE;
1376 case OP_ASSERTBACK_NOT:
1382 common->private_data_ptrs[cc - common->start] = private_data_ptr;
1383 private_data_ptr += sizeof(sljit_sw);
1384 bracketlen = 1 + LINK_SIZE;
1389 common->private_data_ptrs[cc - common->start] = private_data_ptr;
1390 private_data_ptr += sizeof(sljit_sw);
1391 bracketlen = 1 + LINK_SIZE + IMM2_SIZE;
1395 /* Might be a hidden SCOND. */
1396 alternative = cc + GET(cc, 1);
1397 if (*alternative == OP_KETRMAX || *alternative == OP_KETRMIN)
1399 common->private_data_ptrs[cc - common->start] = private_data_ptr;
1400 private_data_ptr += sizeof(sljit_sw);
1402 bracketlen = 1 + LINK_SIZE;
1406 bracketlen = 1 + LINK_SIZE;
1411 bracketlen = 1 + LINK_SIZE + IMM2_SIZE;
1417 repeat_check = FALSE;
1421 CASE_ITERATOR_PRIVATE_DATA_1
1426 CASE_ITERATOR_PRIVATE_DATA_2A
1431 CASE_ITERATOR_PRIVATE_DATA_2B
1433 size = -(2 + IMM2_SIZE);
1436 CASE_ITERATOR_TYPE_PRIVATE_DATA_1
1441 CASE_ITERATOR_TYPE_PRIVATE_DATA_2A
1442 if (cc[1] != OP_ANYNL && cc[1] != OP_EXTUNI)
1448 if (cc[1 + IMM2_SIZE] != OP_ANYNL && cc[1 + IMM2_SIZE] != OP_EXTUNI)
1450 size = 1 + IMM2_SIZE;
1453 case OP_TYPEMINUPTO:
1455 size = 1 + IMM2_SIZE;
1460 space = get_class_iterator_size(cc + size);
1461 size = 1 + 32 / sizeof(PCRE2_UCHAR);
1464 #if defined SUPPORT_UNICODE || PCRE2_CODE_UNIT_WIDTH != 8
1466 space = get_class_iterator_size(cc + size);
1472 cc = next_opcode(common, cc);
1473 SLJIT_ASSERT(cc != NULL);
1477 /* Character iterators, which are not inside a repeated bracket,
1478 gets a private slot instead of allocating it on the stack. */
1479 if (space > 0 && cc >= end)
1481 common->private_data_ptrs[cc - common->start] = private_data_ptr;
1482 private_data_ptr += sizeof(sljit_sw) * space;
1490 #ifdef SUPPORT_UNICODE
1491 if (common->utf && HAS_EXTRALEN(cc[-1])) cc += GET_EXTRALEN(cc[-1]);
1502 end = bracketend(cc);
1503 if (end[-1 - LINK_SIZE] == OP_KET)
1509 *private_data_start = private_data_ptr;
1512 /* Returns with a frame_types (always < 0) if no need for frame. */
1513 static int get_framesize(compiler_common *common, PCRE2_SPTR cc, PCRE2_SPTR ccend, BOOL recursive, BOOL *needs_control_head)
1517 BOOL stack_restore = FALSE;
1518 BOOL setsom_found = recursive;
1519 BOOL setmark_found = recursive;
1520 /* The last capture is a local variable even for recursions. */
1521 BOOL capture_last_found = FALSE;
1523 #if defined DEBUG_FORCE_CONTROL_HEAD && DEBUG_FORCE_CONTROL_HEAD
1524 SLJIT_ASSERT(common->control_head_ptr != 0);
1525 *needs_control_head = TRUE;
1527 *needs_control_head = FALSE;
1532 ccend = bracketend(cc) - (1 + LINK_SIZE);
1533 if (!recursive && (*cc == OP_CBRAPOS || *cc == OP_SCBRAPOS))
1535 possessive = length = (common->capture_last_ptr != 0) ? 5 : 3;
1536 /* This is correct regardless of common->capture_last_ptr. */
1537 capture_last_found = TRUE;
1539 cc = next_opcode(common, cc);
1542 SLJIT_ASSERT(cc != NULL);
1547 SLJIT_ASSERT(common->has_set_som);
1548 stack_restore = TRUE;
1552 setsom_found = TRUE;
1561 SLJIT_ASSERT(common->mark_ptr != 0);
1562 stack_restore = TRUE;
1566 setmark_found = TRUE;
1568 if (common->control_head_ptr != 0)
1569 *needs_control_head = TRUE;
1570 cc += 1 + 2 + cc[1];
1574 stack_restore = TRUE;
1575 if (common->has_set_som && !setsom_found)
1578 setsom_found = TRUE;
1580 if (common->mark_ptr != 0 && !setmark_found)
1583 setmark_found = TRUE;
1585 if (common->capture_last_ptr != 0 && !capture_last_found)
1588 capture_last_found = TRUE;
1590 cc += 1 + LINK_SIZE;
1597 stack_restore = TRUE;
1598 if (common->capture_last_ptr != 0 && !capture_last_found)
1601 capture_last_found = TRUE;
1604 cc += 1 + LINK_SIZE + IMM2_SIZE;
1608 stack_restore = TRUE;
1609 if (common->control_head_ptr != 0)
1610 *needs_control_head = TRUE;
1615 stack_restore = TRUE;
1618 case OP_NOT_WORD_BOUNDARY:
1619 case OP_WORD_BOUNDARY:
1622 case OP_NOT_WHITESPACE:
1624 case OP_NOT_WORDCHAR:
1663 case OP_NOTPOSQUERY:
1667 case OP_NOTPOSSTARI:
1668 case OP_NOTPOSPLUSI:
1669 case OP_NOTPOSQUERYI:
1670 case OP_NOTPOSUPTOI:
1673 case OP_TYPEPOSSTAR:
1674 case OP_TYPEPOSPLUS:
1675 case OP_TYPEPOSQUERY:
1676 case OP_TYPEPOSUPTO:
1683 case OP_CALLOUT_STR:
1685 cc = next_opcode(common, cc);
1686 SLJIT_ASSERT(cc != NULL);
1690 /* Possessive quantifiers can use a special case. */
1691 if (SLJIT_UNLIKELY(possessive == length))
1692 return stack_restore ? no_frame : no_stack;
1696 return stack_restore ? no_frame : no_stack;
1699 static void init_frame(compiler_common *common, PCRE2_SPTR cc, PCRE2_SPTR ccend, int stackpos, int stacktop)
1702 BOOL setsom_found = FALSE;
1703 BOOL setmark_found = FALSE;
1704 /* The last capture is a local variable even for recursions. */
1705 BOOL capture_last_found = FALSE;
1708 /* >= 1 + shortest item size (2) */
1709 SLJIT_UNUSED_ARG(stacktop);
1710 SLJIT_ASSERT(stackpos >= stacktop + 2);
1712 stackpos = STACK(stackpos);
1715 ccend = bracketend(cc) - (1 + LINK_SIZE);
1716 if (*cc != OP_CBRAPOS && *cc != OP_SCBRAPOS)
1717 cc = next_opcode(common, cc);
1720 SLJIT_ASSERT(cc != NULL);
1725 SLJIT_ASSERT(common->has_set_som);
1728 OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), OVECTOR(0));
1729 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, SLJIT_IMM, -OVECTOR(0));
1730 stackpos -= (int)sizeof(sljit_sw);
1731 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, TMP1, 0);
1732 stackpos -= (int)sizeof(sljit_sw);
1733 setsom_found = TRUE;
1742 SLJIT_ASSERT(common->mark_ptr != 0);
1745 OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), common->mark_ptr);
1746 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, SLJIT_IMM, -common->mark_ptr);
1747 stackpos -= (int)sizeof(sljit_sw);
1748 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, TMP1, 0);
1749 stackpos -= (int)sizeof(sljit_sw);
1750 setmark_found = TRUE;
1752 cc += 1 + 2 + cc[1];
1756 if (common->has_set_som && !setsom_found)
1758 OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), OVECTOR(0));
1759 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, SLJIT_IMM, -OVECTOR(0));
1760 stackpos -= (int)sizeof(sljit_sw);
1761 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, TMP1, 0);
1762 stackpos -= (int)sizeof(sljit_sw);
1763 setsom_found = TRUE;
1765 if (common->mark_ptr != 0 && !setmark_found)
1767 OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), common->mark_ptr);
1768 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, SLJIT_IMM, -common->mark_ptr);
1769 stackpos -= (int)sizeof(sljit_sw);
1770 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, TMP1, 0);
1771 stackpos -= (int)sizeof(sljit_sw);
1772 setmark_found = TRUE;
1774 if (common->capture_last_ptr != 0 && !capture_last_found)
1776 OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), common->capture_last_ptr);
1777 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, SLJIT_IMM, -common->capture_last_ptr);
1778 stackpos -= (int)sizeof(sljit_sw);
1779 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, TMP1, 0);
1780 stackpos -= (int)sizeof(sljit_sw);
1781 capture_last_found = TRUE;
1783 cc += 1 + LINK_SIZE;
1790 if (common->capture_last_ptr != 0 && !capture_last_found)
1792 OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), common->capture_last_ptr);
1793 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, SLJIT_IMM, -common->capture_last_ptr);
1794 stackpos -= (int)sizeof(sljit_sw);
1795 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, TMP1, 0);
1796 stackpos -= (int)sizeof(sljit_sw);
1797 capture_last_found = TRUE;
1799 offset = (GET2(cc, 1 + LINK_SIZE)) << 1;
1800 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, SLJIT_IMM, OVECTOR(offset));
1801 stackpos -= (int)sizeof(sljit_sw);
1802 OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), OVECTOR(offset));
1803 OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_SP), OVECTOR(offset + 1));
1804 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, TMP1, 0);
1805 stackpos -= (int)sizeof(sljit_sw);
1806 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, TMP2, 0);
1807 stackpos -= (int)sizeof(sljit_sw);
1809 cc += 1 + LINK_SIZE + IMM2_SIZE;
1813 cc = next_opcode(common, cc);
1814 SLJIT_ASSERT(cc != NULL);
1818 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, SLJIT_IMM, 0);
1819 SLJIT_ASSERT(stackpos == STACK(stacktop));
1822 #define RECURSE_TMP_REG_COUNT 3
1824 typedef struct delayed_mem_copy_status {
1825 struct sljit_compiler *compiler;
1826 int store_bases[RECURSE_TMP_REG_COUNT];
1827 int store_offsets[RECURSE_TMP_REG_COUNT];
1828 int tmp_regs[RECURSE_TMP_REG_COUNT];
1829 int saved_tmp_regs[RECURSE_TMP_REG_COUNT];
1831 } delayed_mem_copy_status;
1833 static void delayed_mem_copy_init(delayed_mem_copy_status *status, compiler_common *common)
1837 for (i = 0; i < RECURSE_TMP_REG_COUNT; i++)
1839 SLJIT_ASSERT(status->tmp_regs[i] >= 0);
1840 SLJIT_ASSERT(sljit_get_register_index(status->saved_tmp_regs[i]) < 0 || status->tmp_regs[i] == status->saved_tmp_regs[i]);
1842 status->store_bases[i] = -1;
1844 status->next_tmp_reg = 0;
1845 status->compiler = common->compiler;
1848 static void delayed_mem_copy_move(delayed_mem_copy_status *status, int load_base, sljit_sw load_offset,
1849 int store_base, sljit_sw store_offset)
1851 struct sljit_compiler *compiler = status->compiler;
1852 int next_tmp_reg = status->next_tmp_reg;
1853 int tmp_reg = status->tmp_regs[next_tmp_reg];
1855 SLJIT_ASSERT(load_base > 0 && store_base > 0);
1857 if (status->store_bases[next_tmp_reg] == -1)
1859 /* Preserve virtual registers. */
1860 if (sljit_get_register_index(status->saved_tmp_regs[next_tmp_reg]) < 0)
1861 OP1(SLJIT_MOV, status->saved_tmp_regs[next_tmp_reg], 0, tmp_reg, 0);
1864 OP1(SLJIT_MOV, SLJIT_MEM1(status->store_bases[next_tmp_reg]), status->store_offsets[next_tmp_reg], tmp_reg, 0);
1866 OP1(SLJIT_MOV, tmp_reg, 0, SLJIT_MEM1(load_base), load_offset);
1867 status->store_bases[next_tmp_reg] = store_base;
1868 status->store_offsets[next_tmp_reg] = store_offset;
1870 status->next_tmp_reg = (next_tmp_reg + 1) % RECURSE_TMP_REG_COUNT;
1873 static void delayed_mem_copy_finish(delayed_mem_copy_status *status)
1875 struct sljit_compiler *compiler = status->compiler;
1876 int next_tmp_reg = status->next_tmp_reg;
1877 int tmp_reg, saved_tmp_reg, i;
1879 for (i = 0; i < RECURSE_TMP_REG_COUNT; i++)
1881 if (status->store_bases[next_tmp_reg] != -1)
1883 tmp_reg = status->tmp_regs[next_tmp_reg];
1884 saved_tmp_reg = status->saved_tmp_regs[next_tmp_reg];
1886 OP1(SLJIT_MOV, SLJIT_MEM1(status->store_bases[next_tmp_reg]), status->store_offsets[next_tmp_reg], tmp_reg, 0);
1888 /* Restore virtual registers. */
1889 if (sljit_get_register_index(saved_tmp_reg) < 0)
1890 OP1(SLJIT_MOV, tmp_reg, 0, saved_tmp_reg, 0);
1893 next_tmp_reg = (next_tmp_reg + 1) % RECURSE_TMP_REG_COUNT;
1897 #undef RECURSE_TMP_REG_COUNT
1899 static int get_recurse_data_length(compiler_common *common, PCRE2_SPTR cc, PCRE2_SPTR ccend,
1900 BOOL *needs_control_head, BOOL *has_quit, BOOL *has_accept)
1904 PCRE2_SPTR alternative;
1905 BOOL quit_found = FALSE;
1906 BOOL accept_found = FALSE;
1907 BOOL setsom_found = FALSE;
1908 BOOL setmark_found = FALSE;
1909 BOOL capture_last_found = FALSE;
1910 BOOL control_head_found = FALSE;
1912 #if defined DEBUG_FORCE_CONTROL_HEAD && DEBUG_FORCE_CONTROL_HEAD
1913 SLJIT_ASSERT(common->control_head_ptr != 0);
1914 control_head_found = TRUE;
1917 /* Calculate the sum of the private machine words. */
1924 SLJIT_ASSERT(common->has_set_som);
1925 setsom_found = TRUE;
1930 if (common->has_set_som)
1931 setsom_found = TRUE;
1932 if (common->mark_ptr != 0)
1933 setmark_found = TRUE;
1934 if (common->capture_last_ptr != 0)
1935 capture_last_found = TRUE;
1936 cc += 1 + LINK_SIZE;
1940 if (PRIVATE_DATA(cc) != 0)
1943 SLJIT_ASSERT(PRIVATE_DATA(cc + 1) != 0);
1944 cc += PRIVATE_DATA(cc + 1);
1946 cc += 1 + LINK_SIZE;
1952 case OP_ASSERTBACK_NOT:
1959 SLJIT_ASSERT(PRIVATE_DATA(cc) != 0);
1960 cc += 1 + LINK_SIZE;
1966 if (common->capture_last_ptr != 0)
1967 capture_last_found = TRUE;
1968 if (common->optimized_cbracket[GET2(cc, 1 + LINK_SIZE)] == 0)
1970 cc += 1 + LINK_SIZE + IMM2_SIZE;
1976 if (common->capture_last_ptr != 0)
1977 capture_last_found = TRUE;
1978 cc += 1 + LINK_SIZE + IMM2_SIZE;
1982 /* Might be a hidden SCOND. */
1983 alternative = cc + GET(cc, 1);
1984 if (*alternative == OP_KETRMAX || *alternative == OP_KETRMIN)
1986 cc += 1 + LINK_SIZE;
1989 CASE_ITERATOR_PRIVATE_DATA_1
1990 if (PRIVATE_DATA(cc) != 0)
1993 #ifdef SUPPORT_UNICODE
1994 if (common->utf && HAS_EXTRALEN(cc[-1])) cc += GET_EXTRALEN(cc[-1]);
1998 CASE_ITERATOR_PRIVATE_DATA_2A
1999 if (PRIVATE_DATA(cc) != 0)
2002 #ifdef SUPPORT_UNICODE
2003 if (common->utf && HAS_EXTRALEN(cc[-1])) cc += GET_EXTRALEN(cc[-1]);
2007 CASE_ITERATOR_PRIVATE_DATA_2B
2008 if (PRIVATE_DATA(cc) != 0)
2010 cc += 2 + IMM2_SIZE;
2011 #ifdef SUPPORT_UNICODE
2012 if (common->utf && HAS_EXTRALEN(cc[-1])) cc += GET_EXTRALEN(cc[-1]);
2016 CASE_ITERATOR_TYPE_PRIVATE_DATA_1
2017 if (PRIVATE_DATA(cc) != 0)
2022 CASE_ITERATOR_TYPE_PRIVATE_DATA_2A
2023 if (PRIVATE_DATA(cc) != 0)
2028 CASE_ITERATOR_TYPE_PRIVATE_DATA_2B
2029 if (PRIVATE_DATA(cc) != 0)
2031 cc += 1 + IMM2_SIZE;
2036 #if defined SUPPORT_UNICODE || PCRE2_CODE_UNIT_WIDTH != 8
2038 size = (*cc == OP_XCLASS) ? GET(cc, 1) : 1 + 32 / (int)sizeof(PCRE2_UCHAR);
2040 size = 1 + 32 / (int)sizeof(PCRE2_UCHAR);
2042 if (PRIVATE_DATA(cc) != 0)
2043 length += get_class_iterator_size(cc + size);
2051 SLJIT_ASSERT(common->mark_ptr != 0);
2053 setmark_found = TRUE;
2054 if (common->control_head_ptr != 0)
2055 control_head_found = TRUE;
2059 cc += 1 + 2 + cc[1];
2071 cc += 1 + 2 + cc[1];
2075 SLJIT_ASSERT(common->control_head_ptr != 0);
2077 if (!control_head_found)
2078 control_head_found = TRUE;
2083 case OP_ASSERT_ACCEPT:
2084 accept_found = TRUE;
2089 cc = next_opcode(common, cc);
2090 SLJIT_ASSERT(cc != NULL);
2094 SLJIT_ASSERT(cc == ccend);
2096 if (control_head_found)
2098 if (capture_last_found)
2108 *needs_control_head = control_head_found;
2109 *has_quit = quit_found;
2110 *has_accept = accept_found;
2114 enum copy_recurse_data_types {
2115 recurse_copy_from_global,
2116 recurse_copy_private_to_global,
2117 recurse_copy_shared_to_global,
2118 recurse_copy_kept_shared_to_global,
2122 static void copy_recurse_data(compiler_common *common, PCRE2_SPTR cc, PCRE2_SPTR ccend,
2123 int type, int stackptr, int stacktop, BOOL has_quit)
2125 delayed_mem_copy_status status;
2126 PCRE2_SPTR alternative;
2127 sljit_sw private_srcw[2];
2128 sljit_sw shared_srcw[3];
2129 sljit_sw kept_shared_srcw[2];
2130 int private_count, shared_count, kept_shared_count;
2131 int from_sp, base_reg, offset, i;
2132 BOOL setsom_found = FALSE;
2133 BOOL setmark_found = FALSE;
2134 BOOL capture_last_found = FALSE;
2135 BOOL control_head_found = FALSE;
2137 #if defined DEBUG_FORCE_CONTROL_HEAD && DEBUG_FORCE_CONTROL_HEAD
2138 SLJIT_ASSERT(common->control_head_ptr != 0);
2139 control_head_found = TRUE;
2144 case recurse_copy_from_global:
2146 base_reg = STACK_TOP;
2149 case recurse_copy_private_to_global:
2150 case recurse_copy_shared_to_global:
2151 case recurse_copy_kept_shared_to_global:
2153 base_reg = STACK_TOP;
2157 SLJIT_ASSERT(type == recurse_swap_global);
2163 stackptr = STACK(stackptr);
2164 stacktop = STACK(stacktop);
2166 status.tmp_regs[0] = TMP1;
2167 status.saved_tmp_regs[0] = TMP1;
2169 if (base_reg != TMP2)
2171 status.tmp_regs[1] = TMP2;
2172 status.saved_tmp_regs[1] = TMP2;
2176 status.saved_tmp_regs[1] = RETURN_ADDR;
2177 if (sljit_get_register_index (RETURN_ADDR) == -1)
2178 status.tmp_regs[1] = STR_PTR;
2180 status.tmp_regs[1] = RETURN_ADDR;
2183 status.saved_tmp_regs[2] = TMP3;
2184 if (sljit_get_register_index (TMP3) == -1)
2185 status.tmp_regs[2] = STR_END;
2187 status.tmp_regs[2] = TMP3;
2189 delayed_mem_copy_init(&status, common);
2191 if (type != recurse_copy_shared_to_global && type != recurse_copy_kept_shared_to_global)
2193 SLJIT_ASSERT(type == recurse_copy_from_global || type == recurse_copy_private_to_global || type == recurse_swap_global);
2196 delayed_mem_copy_move(&status, base_reg, stackptr, SLJIT_SP, common->recursive_head_ptr);
2198 if (from_sp || type == recurse_swap_global)
2199 delayed_mem_copy_move(&status, SLJIT_SP, common->recursive_head_ptr, base_reg, stackptr);
2202 stackptr += sizeof(sljit_sw);
2204 #if defined DEBUG_FORCE_CONTROL_HEAD && DEBUG_FORCE_CONTROL_HEAD
2205 if (type != recurse_copy_shared_to_global)
2208 delayed_mem_copy_move(&status, base_reg, stackptr, SLJIT_SP, common->control_head_ptr);
2210 if (from_sp || type == recurse_swap_global)
2211 delayed_mem_copy_move(&status, SLJIT_SP, common->control_head_ptr, base_reg, stackptr);
2214 stackptr += sizeof(sljit_sw);
2221 kept_shared_count = 0;
2226 SLJIT_ASSERT(common->has_set_som);
2227 if (has_quit && !setsom_found)
2229 kept_shared_srcw[0] = OVECTOR(0);
2230 kept_shared_count = 1;
2231 setsom_found = TRUE;
2239 if (common->has_set_som && !setsom_found)
2241 kept_shared_srcw[0] = OVECTOR(0);
2242 kept_shared_count = 1;
2243 setsom_found = TRUE;
2245 if (common->mark_ptr != 0 && !setmark_found)
2247 kept_shared_srcw[kept_shared_count] = common->mark_ptr;
2248 kept_shared_count++;
2249 setmark_found = TRUE;
2252 if (common->capture_last_ptr != 0 && !capture_last_found)
2254 shared_srcw[0] = common->capture_last_ptr;
2256 capture_last_found = TRUE;
2258 cc += 1 + LINK_SIZE;
2262 if (PRIVATE_DATA(cc) != 0)
2265 private_srcw[0] = PRIVATE_DATA(cc);
2266 SLJIT_ASSERT(PRIVATE_DATA(cc + 1) != 0);
2267 cc += PRIVATE_DATA(cc + 1);
2269 cc += 1 + LINK_SIZE;
2275 case OP_ASSERTBACK_NOT:
2282 private_srcw[0] = PRIVATE_DATA(cc);
2283 cc += 1 + LINK_SIZE;
2288 offset = (GET2(cc, 1 + LINK_SIZE)) << 1;
2289 shared_srcw[0] = OVECTOR(offset);
2290 shared_srcw[1] = OVECTOR(offset + 1);
2293 if (common->capture_last_ptr != 0 && !capture_last_found)
2295 shared_srcw[2] = common->capture_last_ptr;
2297 capture_last_found = TRUE;
2300 if (common->optimized_cbracket[GET2(cc, 1 + LINK_SIZE)] == 0)
2303 private_srcw[0] = OVECTOR_PRIV(GET2(cc, 1 + LINK_SIZE));
2305 cc += 1 + LINK_SIZE + IMM2_SIZE;
2310 offset = (GET2(cc, 1 + LINK_SIZE)) << 1;
2311 shared_srcw[0] = OVECTOR(offset);
2312 shared_srcw[1] = OVECTOR(offset + 1);
2315 if (common->capture_last_ptr != 0 && !capture_last_found)
2317 shared_srcw[2] = common->capture_last_ptr;
2319 capture_last_found = TRUE;
2323 private_srcw[0] = PRIVATE_DATA(cc);
2324 private_srcw[1] = OVECTOR_PRIV(GET2(cc, 1 + LINK_SIZE));
2325 cc += 1 + LINK_SIZE + IMM2_SIZE;
2329 /* Might be a hidden SCOND. */
2330 alternative = cc + GET(cc, 1);
2331 if (*alternative == OP_KETRMAX || *alternative == OP_KETRMIN)
2334 private_srcw[0] = PRIVATE_DATA(cc);
2336 cc += 1 + LINK_SIZE;
2339 CASE_ITERATOR_PRIVATE_DATA_1
2340 if (PRIVATE_DATA(cc))
2343 private_srcw[0] = PRIVATE_DATA(cc);
2346 #ifdef SUPPORT_UNICODE
2347 if (common->utf && HAS_EXTRALEN(cc[-1])) cc += GET_EXTRALEN(cc[-1]);
2351 CASE_ITERATOR_PRIVATE_DATA_2A
2352 if (PRIVATE_DATA(cc))
2355 private_srcw[0] = PRIVATE_DATA(cc);
2356 private_srcw[1] = PRIVATE_DATA(cc) + sizeof(sljit_sw);
2359 #ifdef SUPPORT_UNICODE
2360 if (common->utf && HAS_EXTRALEN(cc[-1])) cc += GET_EXTRALEN(cc[-1]);
2364 CASE_ITERATOR_PRIVATE_DATA_2B
2365 if (PRIVATE_DATA(cc))
2368 private_srcw[0] = PRIVATE_DATA(cc);
2369 private_srcw[1] = PRIVATE_DATA(cc) + sizeof(sljit_sw);
2371 cc += 2 + IMM2_SIZE;
2372 #ifdef SUPPORT_UNICODE
2373 if (common->utf && HAS_EXTRALEN(cc[-1])) cc += GET_EXTRALEN(cc[-1]);
2377 CASE_ITERATOR_TYPE_PRIVATE_DATA_1
2378 if (PRIVATE_DATA(cc))
2381 private_srcw[0] = PRIVATE_DATA(cc);
2386 CASE_ITERATOR_TYPE_PRIVATE_DATA_2A
2387 if (PRIVATE_DATA(cc))
2390 private_srcw[0] = PRIVATE_DATA(cc);
2391 private_srcw[1] = private_srcw[0] + sizeof(sljit_sw);
2396 CASE_ITERATOR_TYPE_PRIVATE_DATA_2B
2397 if (PRIVATE_DATA(cc))
2400 private_srcw[0] = PRIVATE_DATA(cc);
2401 private_srcw[1] = private_srcw[0] + sizeof(sljit_sw);
2403 cc += 1 + IMM2_SIZE;
2408 #if defined SUPPORT_UNICODE || PCRE2_CODE_UNIT_WIDTH != 8
2410 i = (*cc == OP_XCLASS) ? GET(cc, 1) : 1 + 32 / (int)sizeof(PCRE2_UCHAR);
2412 i = 1 + 32 / (int)sizeof(PCRE2_UCHAR);
2414 if (PRIVATE_DATA(cc) != 0)
2415 switch(get_class_iterator_size(cc + i))
2419 private_srcw[0] = PRIVATE_DATA(cc);
2424 private_srcw[0] = PRIVATE_DATA(cc);
2425 private_srcw[1] = private_srcw[0] + sizeof(sljit_sw);
2429 SLJIT_UNREACHABLE();
2439 SLJIT_ASSERT(common->mark_ptr != 0);
2440 if (has_quit && !setmark_found)
2442 kept_shared_srcw[0] = common->mark_ptr;
2443 kept_shared_count = 1;
2444 setmark_found = TRUE;
2446 if (common->control_head_ptr != 0 && !control_head_found)
2448 shared_srcw[0] = common->control_head_ptr;
2450 control_head_found = TRUE;
2452 cc += 1 + 2 + cc[1];
2456 SLJIT_ASSERT(common->control_head_ptr != 0);
2457 if (!control_head_found)
2459 shared_srcw[0] = common->control_head_ptr;
2461 control_head_found = TRUE;
2467 cc = next_opcode(common, cc);
2468 SLJIT_ASSERT(cc != NULL);
2472 if (type != recurse_copy_shared_to_global && type != recurse_copy_kept_shared_to_global)
2474 SLJIT_ASSERT(type == recurse_copy_from_global || type == recurse_copy_private_to_global || type == recurse_swap_global);
2476 for (i = 0; i < private_count; i++)
2478 SLJIT_ASSERT(private_srcw[i] != 0);
2481 delayed_mem_copy_move(&status, base_reg, stackptr, SLJIT_SP, private_srcw[i]);
2483 if (from_sp || type == recurse_swap_global)
2484 delayed_mem_copy_move(&status, SLJIT_SP, private_srcw[i], base_reg, stackptr);
2486 stackptr += sizeof(sljit_sw);
2490 stackptr += sizeof(sljit_sw) * private_count;
2492 if (type != recurse_copy_private_to_global && type != recurse_copy_kept_shared_to_global)
2494 SLJIT_ASSERT(type == recurse_copy_from_global || type == recurse_copy_shared_to_global || type == recurse_swap_global);
2496 for (i = 0; i < shared_count; i++)
2498 SLJIT_ASSERT(shared_srcw[i] != 0);
2501 delayed_mem_copy_move(&status, base_reg, stackptr, SLJIT_SP, shared_srcw[i]);
2503 if (from_sp || type == recurse_swap_global)
2504 delayed_mem_copy_move(&status, SLJIT_SP, shared_srcw[i], base_reg, stackptr);
2506 stackptr += sizeof(sljit_sw);
2510 stackptr += sizeof(sljit_sw) * shared_count;
2512 if (type != recurse_copy_private_to_global && type != recurse_swap_global)
2514 SLJIT_ASSERT(type == recurse_copy_from_global || type == recurse_copy_shared_to_global || type == recurse_copy_kept_shared_to_global);
2516 for (i = 0; i < kept_shared_count; i++)
2518 SLJIT_ASSERT(kept_shared_srcw[i] != 0);
2521 delayed_mem_copy_move(&status, base_reg, stackptr, SLJIT_SP, kept_shared_srcw[i]);
2523 if (from_sp || type == recurse_swap_global)
2524 delayed_mem_copy_move(&status, SLJIT_SP, kept_shared_srcw[i], base_reg, stackptr);
2526 stackptr += sizeof(sljit_sw);
2530 stackptr += sizeof(sljit_sw) * kept_shared_count;
2533 SLJIT_ASSERT(cc == ccend && stackptr == stacktop);
2535 delayed_mem_copy_finish(&status);
2538 static SLJIT_INLINE PCRE2_SPTR set_then_offsets(compiler_common *common, PCRE2_SPTR cc, sljit_u8 *current_offset)
2540 PCRE2_SPTR end = bracketend(cc);
2541 BOOL has_alternatives = cc[GET(cc, 1)] == OP_ALT;
2543 /* Assert captures then. */
2544 if (*cc >= OP_ASSERT && *cc <= OP_ASSERTBACK_NOT)
2545 current_offset = NULL;
2546 /* Conditional block does not. */
2547 if (*cc == OP_COND || *cc == OP_SCOND)
2548 has_alternatives = FALSE;
2550 cc = next_opcode(common, cc);
2551 if (has_alternatives)
2552 current_offset = common->then_offsets + (cc - common->start);
2556 if ((*cc >= OP_ASSERT && *cc <= OP_ASSERTBACK_NOT) || (*cc >= OP_ONCE && *cc <= OP_SCOND))
2557 cc = set_then_offsets(common, cc, current_offset);
2560 if (*cc == OP_ALT && has_alternatives)
2561 current_offset = common->then_offsets + (cc + 1 + LINK_SIZE - common->start);
2562 if (*cc >= OP_THEN && *cc <= OP_THEN_ARG && current_offset != NULL)
2563 *current_offset = 1;
2564 cc = next_opcode(common, cc);
2571 #undef CASE_ITERATOR_PRIVATE_DATA_1
2572 #undef CASE_ITERATOR_PRIVATE_DATA_2A
2573 #undef CASE_ITERATOR_PRIVATE_DATA_2B
2574 #undef CASE_ITERATOR_TYPE_PRIVATE_DATA_1
2575 #undef CASE_ITERATOR_TYPE_PRIVATE_DATA_2A
2576 #undef CASE_ITERATOR_TYPE_PRIVATE_DATA_2B
2578 static SLJIT_INLINE BOOL is_powerof2(unsigned int value)
2580 return (value & (value - 1)) == 0;
2583 static SLJIT_INLINE void set_jumps(jump_list *list, struct sljit_label *label)
2587 /* sljit_set_label is clever enough to do nothing
2588 if either the jump or the label is NULL. */
2589 SET_LABEL(list->jump, label);
2594 static SLJIT_INLINE void add_jump(struct sljit_compiler *compiler, jump_list **list, struct sljit_jump *jump)
2596 jump_list *list_item = sljit_alloc_memory(compiler, sizeof(jump_list));
2599 list_item->next = *list;
2600 list_item->jump = jump;
2605 static void add_stub(compiler_common *common, struct sljit_jump *start)
2608 stub_list *list_item = sljit_alloc_memory(compiler, sizeof(stub_list));
2612 list_item->start = start;
2613 list_item->quit = LABEL();
2614 list_item->next = common->stubs;
2615 common->stubs = list_item;
2619 static void flush_stubs(compiler_common *common)
2622 stub_list *list_item = common->stubs;
2626 JUMPHERE(list_item->start);
2627 add_jump(compiler, &common->stackalloc, JUMP(SLJIT_FAST_CALL));
2628 JUMPTO(SLJIT_JUMP, list_item->quit);
2629 list_item = list_item->next;
2631 common->stubs = NULL;
2634 static void add_label_addr(compiler_common *common, sljit_uw *update_addr)
2637 label_addr_list *label_addr;
2639 label_addr = sljit_alloc_memory(compiler, sizeof(label_addr_list));
2640 if (label_addr == NULL)
2642 label_addr->label = LABEL();
2643 label_addr->update_addr = update_addr;
2644 label_addr->next = common->label_addrs;
2645 common->label_addrs = label_addr;
2648 static SLJIT_INLINE void count_match(compiler_common *common)
2652 OP2(SLJIT_SUB | SLJIT_SET_Z, COUNT_MATCH, 0, COUNT_MATCH, 0, SLJIT_IMM, 1);
2653 add_jump(compiler, &common->calllimit, JUMP(SLJIT_ZERO));
2656 static SLJIT_INLINE void allocate_stack(compiler_common *common, int size)
2658 /* May destroy all locals and registers except TMP2. */
2661 SLJIT_ASSERT(size > 0);
2662 OP2(SLJIT_SUB, STACK_TOP, 0, STACK_TOP, 0, SLJIT_IMM, size * sizeof(sljit_sw));
2663 #ifdef DESTROY_REGISTERS
2664 OP1(SLJIT_MOV, TMP1, 0, SLJIT_IMM, 12345);
2665 OP1(SLJIT_MOV, TMP3, 0, TMP1, 0);
2666 OP1(SLJIT_MOV, RETURN_ADDR, 0, TMP1, 0);
2667 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), LOCALS0, TMP1, 0);
2668 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), LOCALS1, TMP1, 0);
2670 add_stub(common, CMP(SLJIT_LESS, STACK_TOP, 0, STACK_LIMIT, 0));
2673 static SLJIT_INLINE void free_stack(compiler_common *common, int size)
2677 SLJIT_ASSERT(size > 0);
2678 OP2(SLJIT_ADD, STACK_TOP, 0, STACK_TOP, 0, SLJIT_IMM, size * sizeof(sljit_sw));
2681 static sljit_uw * allocate_read_only_data(compiler_common *common, sljit_uw size)
2686 if (SLJIT_UNLIKELY(sljit_get_compiler_error(compiler)))
2689 result = (sljit_uw *)SLJIT_MALLOC(size + sizeof(sljit_uw), compiler->allocator_data);
2690 if (SLJIT_UNLIKELY(result == NULL))
2692 sljit_set_compiler_memory_error(compiler);
2696 *(void**)result = common->read_only_data_head;
2697 common->read_only_data_head = (void *)result;
2701 static SLJIT_INLINE void reset_ovector(compiler_common *common, int length)
2704 struct sljit_label *loop;
2707 /* At this point we can freely use all temporary registers. */
2708 SLJIT_ASSERT(length > 1);
2709 /* TMP1 returns with begin - 1. */
2710 OP2(SLJIT_SUB, SLJIT_R0, 0, SLJIT_MEM1(SLJIT_S0), SLJIT_OFFSETOF(jit_arguments, begin), SLJIT_IMM, IN_UCHARS(1));
2713 for (i = 1; i < length; i++)
2714 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), OVECTOR(i), SLJIT_R0, 0);
2718 if (sljit_emit_mem(compiler, SLJIT_MOV | SLJIT_MEM_SUPP | SLJIT_MEM_STORE | SLJIT_MEM_PRE, SLJIT_R0, SLJIT_MEM1(SLJIT_R1), sizeof(sljit_sw)) == SLJIT_SUCCESS)
2720 GET_LOCAL_BASE(SLJIT_R1, 0, OVECTOR_START);
2721 OP1(SLJIT_MOV, SLJIT_R2, 0, SLJIT_IMM, length - 1);
2723 sljit_emit_mem(compiler, SLJIT_MOV | SLJIT_MEM_STORE | SLJIT_MEM_PRE, SLJIT_R0, SLJIT_MEM1(SLJIT_R1), sizeof(sljit_sw));
2724 OP2(SLJIT_SUB | SLJIT_SET_Z, SLJIT_R2, 0, SLJIT_R2, 0, SLJIT_IMM, 1);
2725 JUMPTO(SLJIT_NOT_ZERO, loop);
2729 GET_LOCAL_BASE(SLJIT_R1, 0, OVECTOR_START + sizeof(sljit_sw));
2730 OP1(SLJIT_MOV, SLJIT_R2, 0, SLJIT_IMM, length - 1);
2732 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_R1), 0, SLJIT_R0, 0);
2733 OP2(SLJIT_ADD, SLJIT_R1, 0, SLJIT_R1, 0, SLJIT_IMM, sizeof(sljit_sw));
2734 OP2(SLJIT_SUB | SLJIT_SET_Z, SLJIT_R2, 0, SLJIT_R2, 0, SLJIT_IMM, 1);
2735 JUMPTO(SLJIT_NOT_ZERO, loop);
2740 static SLJIT_INLINE void reset_fast_fail(compiler_common *common)
2745 SLJIT_ASSERT(common->fast_fail_start_ptr < common->fast_fail_end_ptr);
2747 OP2(SLJIT_SUB, TMP1, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
2748 for (i = common->fast_fail_start_ptr; i < common->fast_fail_end_ptr; i += sizeof(sljit_sw))
2749 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), i, TMP1, 0);
2752 static SLJIT_INLINE void do_reset_match(compiler_common *common, int length)
2755 struct sljit_label *loop;
2758 SLJIT_ASSERT(length > 1);
2759 /* OVECTOR(1) contains the "string begin - 1" constant. */
2761 OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), OVECTOR(1));
2764 for (i = 2; i < length; i++)
2765 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), OVECTOR(i), TMP1, 0);
2769 if (sljit_emit_mem(compiler, SLJIT_MOV | SLJIT_MEM_SUPP | SLJIT_MEM_STORE | SLJIT_MEM_PRE, TMP1, SLJIT_MEM1(TMP2), sizeof(sljit_sw)) == SLJIT_SUCCESS)
2771 GET_LOCAL_BASE(TMP2, 0, OVECTOR_START + sizeof(sljit_sw));
2772 OP1(SLJIT_MOV, STACK_TOP, 0, SLJIT_IMM, length - 2);
2774 sljit_emit_mem(compiler, SLJIT_MOV | SLJIT_MEM_STORE | SLJIT_MEM_PRE, TMP1, SLJIT_MEM1(TMP2), sizeof(sljit_sw));
2775 OP2(SLJIT_SUB | SLJIT_SET_Z, STACK_TOP, 0, STACK_TOP, 0, SLJIT_IMM, 1);
2776 JUMPTO(SLJIT_NOT_ZERO, loop);
2780 GET_LOCAL_BASE(TMP2, 0, OVECTOR_START + 2 * sizeof(sljit_sw));
2781 OP1(SLJIT_MOV, STACK_TOP, 0, SLJIT_IMM, length - 2);
2783 OP1(SLJIT_MOV, SLJIT_MEM1(TMP2), 0, TMP1, 0);
2784 OP2(SLJIT_ADD, TMP2, 0, TMP2, 0, SLJIT_IMM, sizeof(sljit_sw));
2785 OP2(SLJIT_SUB | SLJIT_SET_Z, STACK_TOP, 0, STACK_TOP, 0, SLJIT_IMM, 1);
2786 JUMPTO(SLJIT_NOT_ZERO, loop);
2790 OP1(SLJIT_MOV, STACK_TOP, 0, ARGUMENTS, 0);
2791 if (common->mark_ptr != 0)
2792 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->mark_ptr, SLJIT_IMM, 0);
2793 if (common->control_head_ptr != 0)
2794 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->control_head_ptr, SLJIT_IMM, 0);
2795 OP1(SLJIT_MOV, STACK_TOP, 0, SLJIT_MEM1(STACK_TOP), SLJIT_OFFSETOF(jit_arguments, stack));
2796 OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), common->start_ptr);
2797 OP1(SLJIT_MOV, STACK_TOP, 0, SLJIT_MEM1(STACK_TOP), SLJIT_OFFSETOF(struct sljit_stack, end));
2800 static sljit_sw SLJIT_FUNC do_search_mark(sljit_sw *current, PCRE2_SPTR skip_arg)
2802 while (current != NULL)
2806 case type_then_trap:
2810 if (PRIV(strcmp)(skip_arg, (PCRE2_SPTR)current[2]) == 0)
2815 SLJIT_UNREACHABLE();
2818 SLJIT_ASSERT(current[0] == 0 || current < (sljit_sw*)current[0]);
2819 current = (sljit_sw*)current[0];
2824 static SLJIT_INLINE void copy_ovector(compiler_common *common, int topbracket)
2827 struct sljit_label *loop;
2830 /* At this point we can freely use all registers. */
2831 OP1(SLJIT_MOV, SLJIT_S2, 0, SLJIT_MEM1(SLJIT_SP), OVECTOR(1));
2832 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), OVECTOR(1), STR_PTR, 0);
2834 OP1(SLJIT_MOV, SLJIT_R0, 0, ARGUMENTS, 0);
2835 OP1(SLJIT_MOV, SLJIT_S0, 0, SLJIT_MEM1(SLJIT_SP), common->start_ptr);
2836 if (common->mark_ptr != 0)
2837 OP1(SLJIT_MOV, SLJIT_R2, 0, SLJIT_MEM1(SLJIT_SP), common->mark_ptr);
2838 OP1(SLJIT_MOV_U32, SLJIT_R1, 0, SLJIT_MEM1(SLJIT_R0), SLJIT_OFFSETOF(jit_arguments, oveccount));
2839 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_R0), SLJIT_OFFSETOF(jit_arguments, startchar_ptr), SLJIT_S0, 0);
2840 if (common->mark_ptr != 0)
2841 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_R0), SLJIT_OFFSETOF(jit_arguments, mark_ptr), SLJIT_R2, 0);
2842 OP2(SLJIT_ADD, SLJIT_R2, 0, SLJIT_MEM1(SLJIT_R0), SLJIT_OFFSETOF(jit_arguments, match_data),
2843 SLJIT_IMM, SLJIT_OFFSETOF(pcre2_match_data, ovector) - sizeof(PCRE2_SIZE));
2845 has_pre = sljit_emit_mem(compiler, SLJIT_MOV | SLJIT_MEM_SUPP | SLJIT_MEM_PRE, SLJIT_S1, SLJIT_MEM1(SLJIT_S0), sizeof(sljit_sw)) == SLJIT_SUCCESS;
2847 GET_LOCAL_BASE(SLJIT_S0, 0, OVECTOR_START - (has_pre ? sizeof(sljit_sw) : 0));
2848 OP1(SLJIT_MOV, SLJIT_R0, 0, SLJIT_MEM1(SLJIT_R0), SLJIT_OFFSETOF(jit_arguments, begin));
2853 sljit_emit_mem(compiler, SLJIT_MOV | SLJIT_MEM_PRE, SLJIT_S1, SLJIT_MEM1(SLJIT_S0), sizeof(sljit_sw));
2856 OP1(SLJIT_MOV, SLJIT_S1, 0, SLJIT_MEM1(SLJIT_S0), 0);
2857 OP2(SLJIT_ADD, SLJIT_S0, 0, SLJIT_S0, 0, SLJIT_IMM, sizeof(sljit_sw));
2860 OP2(SLJIT_ADD, SLJIT_R2, 0, SLJIT_R2, 0, SLJIT_IMM, sizeof(PCRE2_SIZE));
2861 OP2(SLJIT_SUB, SLJIT_S1, 0, SLJIT_S1, 0, SLJIT_R0, 0);
2862 /* Copy the integer value to the output buffer */
2863 #if PCRE2_CODE_UNIT_WIDTH == 16 || PCRE2_CODE_UNIT_WIDTH == 32
2864 OP2(SLJIT_ASHR, SLJIT_S1, 0, SLJIT_S1, 0, SLJIT_IMM, UCHAR_SHIFT);
2867 SLJIT_ASSERT(sizeof(PCRE2_SIZE) == 4 || sizeof(PCRE2_SIZE) == 8);
2868 OP1(((sizeof(PCRE2_SIZE) == 4) ? SLJIT_MOV_U32 : SLJIT_MOV), SLJIT_MEM1(SLJIT_R2), 0, SLJIT_S1, 0);
2870 OP2(SLJIT_SUB | SLJIT_SET_Z, SLJIT_R1, 0, SLJIT_R1, 0, SLJIT_IMM, 1);
2871 JUMPTO(SLJIT_NOT_ZERO, loop);
2873 /* Calculate the return value, which is the maximum ovector value. */
2876 if (sljit_emit_mem(compiler, SLJIT_MOV | SLJIT_MEM_SUPP | SLJIT_MEM_PRE, SLJIT_R2, SLJIT_MEM1(SLJIT_R0), -(2 * (sljit_sw)sizeof(sljit_sw))) == SLJIT_SUCCESS)
2878 GET_LOCAL_BASE(SLJIT_R0, 0, OVECTOR_START + topbracket * 2 * sizeof(sljit_sw));
2879 OP1(SLJIT_MOV, SLJIT_R1, 0, SLJIT_IMM, topbracket + 1);
2881 /* OVECTOR(0) is never equal to SLJIT_S2. */
2883 sljit_emit_mem(compiler, SLJIT_MOV | SLJIT_MEM_PRE, SLJIT_R2, SLJIT_MEM1(SLJIT_R0), -(2 * (sljit_sw)sizeof(sljit_sw)));
2884 OP2(SLJIT_SUB, SLJIT_R1, 0, SLJIT_R1, 0, SLJIT_IMM, 1);
2885 CMPTO(SLJIT_EQUAL, SLJIT_R2, 0, SLJIT_S2, 0, loop);
2886 OP1(SLJIT_MOV, SLJIT_RETURN_REG, 0, SLJIT_R1, 0);
2890 GET_LOCAL_BASE(SLJIT_R0, 0, OVECTOR_START + (topbracket - 1) * 2 * sizeof(sljit_sw));
2891 OP1(SLJIT_MOV, SLJIT_R1, 0, SLJIT_IMM, topbracket + 1);
2893 /* OVECTOR(0) is never equal to SLJIT_S2. */
2895 OP1(SLJIT_MOV, SLJIT_R2, 0, SLJIT_MEM1(SLJIT_R0), 0);
2896 OP2(SLJIT_SUB, SLJIT_R0, 0, SLJIT_R0, 0, SLJIT_IMM, 2 * (sljit_sw)sizeof(sljit_sw));
2897 OP2(SLJIT_SUB, SLJIT_R1, 0, SLJIT_R1, 0, SLJIT_IMM, 1);
2898 CMPTO(SLJIT_EQUAL, SLJIT_R2, 0, SLJIT_S2, 0, loop);
2899 OP1(SLJIT_MOV, SLJIT_RETURN_REG, 0, SLJIT_R1, 0);
2903 OP1(SLJIT_MOV, SLJIT_RETURN_REG, 0, SLJIT_IMM, 1);
2906 static SLJIT_INLINE void return_with_partial_match(compiler_common *common, struct sljit_label *quit)
2909 sljit_s32 mov_opcode;
2911 SLJIT_COMPILE_ASSERT(STR_END == SLJIT_S0, str_end_must_be_saved_reg0);
2912 SLJIT_ASSERT(common->start_used_ptr != 0 && common->start_ptr != 0
2913 && (common->mode == PCRE2_JIT_PARTIAL_SOFT ? common->hit_start != 0 : common->hit_start == 0));
2915 OP1(SLJIT_MOV, SLJIT_R1, 0, ARGUMENTS, 0);
2916 OP1(SLJIT_MOV, SLJIT_R2, 0, SLJIT_MEM1(SLJIT_SP),
2917 common->mode == PCRE2_JIT_PARTIAL_SOFT ? common->hit_start : common->start_ptr);
2918 OP1(SLJIT_MOV, SLJIT_RETURN_REG, 0, SLJIT_IMM, PCRE2_ERROR_PARTIAL);
2920 /* Store match begin and end. */
2921 OP1(SLJIT_MOV, SLJIT_S1, 0, SLJIT_MEM1(SLJIT_R1), SLJIT_OFFSETOF(jit_arguments, begin));
2922 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_R1), SLJIT_OFFSETOF(jit_arguments, startchar_ptr), SLJIT_R2, 0);
2923 OP1(SLJIT_MOV, SLJIT_R1, 0, SLJIT_MEM1(SLJIT_R1), SLJIT_OFFSETOF(jit_arguments, match_data));
2925 mov_opcode = (sizeof(PCRE2_SIZE) == 4) ? SLJIT_MOV_U32 : SLJIT_MOV;
2927 OP2(SLJIT_SUB, SLJIT_R2, 0, SLJIT_R2, 0, SLJIT_S1, 0);
2928 #if PCRE2_CODE_UNIT_WIDTH == 16 || PCRE2_CODE_UNIT_WIDTH == 32
2929 OP2(SLJIT_ASHR, SLJIT_R2, 0, SLJIT_R2, 0, SLJIT_IMM, UCHAR_SHIFT);
2931 OP1(mov_opcode, SLJIT_MEM1(SLJIT_R1), SLJIT_OFFSETOF(pcre2_match_data, ovector), SLJIT_R2, 0);
2933 OP2(SLJIT_SUB, STR_END, 0, STR_END, 0, SLJIT_S1, 0);
2934 #if PCRE2_CODE_UNIT_WIDTH == 16 || PCRE2_CODE_UNIT_WIDTH == 32
2935 OP2(SLJIT_ASHR, STR_END, 0, STR_END, 0, SLJIT_IMM, UCHAR_SHIFT);
2937 OP1(mov_opcode, SLJIT_MEM1(SLJIT_R1), SLJIT_OFFSETOF(pcre2_match_data, ovector) + sizeof(PCRE2_SIZE), STR_END, 0);
2939 JUMPTO(SLJIT_JUMP, quit);
2942 static SLJIT_INLINE void check_start_used_ptr(compiler_common *common)
2944 /* May destroy TMP1. */
2946 struct sljit_jump *jump;
2948 if (common->mode == PCRE2_JIT_PARTIAL_SOFT)
2950 /* The value of -1 must be kept for start_used_ptr! */
2951 OP2(SLJIT_ADD, TMP1, 0, SLJIT_MEM1(SLJIT_SP), common->start_used_ptr, SLJIT_IMM, 1);
2952 /* Jumps if start_used_ptr < STR_PTR, or start_used_ptr == -1. Although overwriting
2953 is not necessary if start_used_ptr == STR_PTR, it does not hurt as well. */
2954 jump = CMP(SLJIT_LESS_EQUAL, TMP1, 0, STR_PTR, 0);
2955 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->start_used_ptr, STR_PTR, 0);
2958 else if (common->mode == PCRE2_JIT_PARTIAL_HARD)
2960 jump = CMP(SLJIT_LESS_EQUAL, SLJIT_MEM1(SLJIT_SP), common->start_used_ptr, STR_PTR, 0);
2961 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->start_used_ptr, STR_PTR, 0);
2966 static SLJIT_INLINE BOOL char_has_othercase(compiler_common *common, PCRE2_SPTR cc)
2968 /* Detects if the character has an othercase. */
2971 #ifdef SUPPORT_UNICODE
2977 return c != UCD_OTHERCASE(c);
2979 #if PCRE2_CODE_UNIT_WIDTH != 8
2980 return common->fcc[c] != c;
2986 return MAX_255(c) ? common->fcc[c] != c : FALSE;
2989 static SLJIT_INLINE unsigned int char_othercase(compiler_common *common, unsigned int c)
2991 /* Returns with the othercase. */
2992 #ifdef SUPPORT_UNICODE
2993 if (common->utf && c > 127)
2995 return UCD_OTHERCASE(c);
2998 return TABLE_GET(c, common->fcc, c);
3001 static unsigned int char_get_othercase_bit(compiler_common *common, PCRE2_SPTR cc)
3003 /* Detects if the character and its othercase has only 1 bit difference. */
3004 unsigned int c, oc, bit;
3005 #if defined SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH == 8
3009 #ifdef SUPPORT_UNICODE
3014 oc = common->fcc[c];
3017 oc = UCD_OTHERCASE(c);
3023 oc = TABLE_GET(c, common->fcc, c);
3027 oc = TABLE_GET(c, common->fcc, c);
3030 SLJIT_ASSERT(c != oc);
3033 /* Optimized for English alphabet. */
3034 if (c <= 127 && bit == 0x20)
3035 return (0 << 8) | 0x20;
3037 /* Since c != oc, they must have at least 1 bit difference. */
3038 if (!is_powerof2(bit))
3041 #if PCRE2_CODE_UNIT_WIDTH == 8
3043 #ifdef SUPPORT_UNICODE
3044 if (common->utf && c > 127)
3046 n = GET_EXTRALEN(*cc);
3047 while ((bit & 0x3f) == 0)
3052 return (n << 8) | bit;
3054 #endif /* SUPPORT_UNICODE */
3055 return (0 << 8) | bit;
3057 #elif PCRE2_CODE_UNIT_WIDTH == 16 || PCRE2_CODE_UNIT_WIDTH == 32
3059 #ifdef SUPPORT_UNICODE
3060 if (common->utf && c > 65535)
3062 if (bit >= (1 << 10))
3065 return (bit < 256) ? ((2 << 8) | bit) : ((3 << 8) | (bit >> 8));
3067 #endif /* SUPPORT_UNICODE */
3068 return (bit < 256) ? ((0 << 8) | bit) : ((1 << 8) | (bit >> 8));
3070 #endif /* PCRE2_CODE_UNIT_WIDTH == [8|16|32] */
3073 static void check_partial(compiler_common *common, BOOL force)
3075 /* Checks whether a partial matching is occurred. Does not modify registers. */
3077 struct sljit_jump *jump = NULL;
3079 SLJIT_ASSERT(!force || common->mode != PCRE2_JIT_COMPLETE);
3081 if (common->mode == PCRE2_JIT_COMPLETE)
3085 jump = CMP(SLJIT_GREATER_EQUAL, SLJIT_MEM1(SLJIT_SP), common->start_used_ptr, STR_PTR, 0);
3086 else if (common->mode == PCRE2_JIT_PARTIAL_SOFT)
3087 jump = CMP(SLJIT_EQUAL, SLJIT_MEM1(SLJIT_SP), common->start_used_ptr, SLJIT_IMM, -1);
3089 if (common->mode == PCRE2_JIT_PARTIAL_SOFT)
3090 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->hit_start, SLJIT_IMM, 0);
3093 if (common->partialmatchlabel != NULL)
3094 JUMPTO(SLJIT_JUMP, common->partialmatchlabel);
3096 add_jump(compiler, &common->partialmatch, JUMP(SLJIT_JUMP));
3103 static void check_str_end(compiler_common *common, jump_list **end_reached)
3105 /* Does not affect registers. Usually used in a tight spot. */
3107 struct sljit_jump *jump;
3109 if (common->mode == PCRE2_JIT_COMPLETE)
3111 add_jump(compiler, end_reached, CMP(SLJIT_GREATER_EQUAL, STR_PTR, 0, STR_END, 0));
3115 jump = CMP(SLJIT_LESS, STR_PTR, 0, STR_END, 0);
3116 if (common->mode == PCRE2_JIT_PARTIAL_SOFT)
3118 add_jump(compiler, end_reached, CMP(SLJIT_GREATER_EQUAL, SLJIT_MEM1(SLJIT_SP), common->start_used_ptr, STR_PTR, 0));
3119 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->hit_start, SLJIT_IMM, 0);
3120 add_jump(compiler, end_reached, JUMP(SLJIT_JUMP));
3124 add_jump(compiler, end_reached, CMP(SLJIT_GREATER_EQUAL, SLJIT_MEM1(SLJIT_SP), common->start_used_ptr, STR_PTR, 0));
3125 if (common->partialmatchlabel != NULL)
3126 JUMPTO(SLJIT_JUMP, common->partialmatchlabel);
3128 add_jump(compiler, &common->partialmatch, JUMP(SLJIT_JUMP));
3133 static void detect_partial_match(compiler_common *common, jump_list **backtracks)
3136 struct sljit_jump *jump;
3138 if (common->mode == PCRE2_JIT_COMPLETE)
3140 add_jump(compiler, backtracks, CMP(SLJIT_GREATER_EQUAL, STR_PTR, 0, STR_END, 0));
3144 /* Partial matching mode. */
3145 jump = CMP(SLJIT_LESS, STR_PTR, 0, STR_END, 0);
3146 add_jump(compiler, backtracks, CMP(SLJIT_GREATER_EQUAL, SLJIT_MEM1(SLJIT_SP), common->start_used_ptr, STR_PTR, 0));
3147 if (common->mode == PCRE2_JIT_PARTIAL_SOFT)
3149 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->hit_start, SLJIT_IMM, 0);
3150 add_jump(compiler, backtracks, JUMP(SLJIT_JUMP));
3154 if (common->partialmatchlabel != NULL)
3155 JUMPTO(SLJIT_JUMP, common->partialmatchlabel);
3157 add_jump(compiler, &common->partialmatch, JUMP(SLJIT_JUMP));
3162 static void peek_char(compiler_common *common, sljit_u32 max)
3164 /* Reads the character into TMP1, keeps STR_PTR.
3165 Does not check STR_END. TMP2 Destroyed. */
3167 #if defined SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH != 32
3168 struct sljit_jump *jump;
3171 SLJIT_UNUSED_ARG(max);
3173 OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), 0);
3174 #if defined SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH == 8
3177 if (max < 128) return;
3179 jump = CMP(SLJIT_LESS, TMP1, 0, SLJIT_IMM, 0xc0);
3180 OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
3181 add_jump(compiler, &common->utfreadchar, JUMP(SLJIT_FAST_CALL));
3182 OP2(SLJIT_SUB, STR_PTR, 0, STR_PTR, 0, TMP2, 0);
3185 #endif /* SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH != 32 */
3187 #if defined SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH == 16
3190 if (max < 0xd800) return;
3192 OP2(SLJIT_SUB, TMP2, 0, TMP1, 0, SLJIT_IMM, 0xd800);
3193 jump = CMP(SLJIT_GREATER, TMP2, 0, SLJIT_IMM, 0xdc00 - 0xd800 - 1);
3194 /* TMP2 contains the high surrogate. */
3195 OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(0));
3196 OP2(SLJIT_ADD, TMP2, 0, TMP2, 0, SLJIT_IMM, 0x40);
3197 OP2(SLJIT_SHL, TMP2, 0, TMP2, 0, SLJIT_IMM, 10);
3198 OP2(SLJIT_AND, TMP1, 0, TMP1, 0, SLJIT_IMM, 0x3ff);
3199 OP2(SLJIT_OR, TMP1, 0, TMP1, 0, TMP2, 0);
3205 #if defined SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH == 8
3207 static BOOL is_char7_bitset(const sljit_u8 *bitset, BOOL nclass)
3209 /* Tells whether the character codes below 128 are enough
3210 to determine a match. */
3211 const sljit_u8 value = nclass ? 0xff : 0;
3212 const sljit_u8 *end = bitset + 32;
3217 if (*bitset++ != value)
3220 while (bitset < end);
3224 static void read_char7_type(compiler_common *common, BOOL full_read)
3226 /* Reads the precise character type of a character into TMP1, if the character
3227 is less than 128. Otherwise it returns with zero. Does not check STR_END. The
3228 full_read argument tells whether characters above max are accepted or not. */
3230 struct sljit_jump *jump;
3232 SLJIT_ASSERT(common->utf);
3234 OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), 0);
3235 OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
3237 OP1(SLJIT_MOV_U8, TMP1, 0, SLJIT_MEM1(TMP2), common->ctypes);
3241 jump = CMP(SLJIT_LESS, TMP2, 0, SLJIT_IMM, 0xc0);
3242 OP1(SLJIT_MOV_U8, TMP2, 0, SLJIT_MEM1(TMP2), (sljit_sw)PRIV(utf8_table4) - 0xc0);
3243 OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, TMP2, 0);
3248 #endif /* SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH == 8 */
3250 static void read_char_range(compiler_common *common, sljit_u32 min, sljit_u32 max, BOOL update_str_ptr)
3252 /* Reads the precise value of a character into TMP1, if the character is
3253 between min and max (c >= min && c <= max). Otherwise it returns with a value
3254 outside the range. Does not check STR_END. */
3256 #if defined SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH != 32
3257 struct sljit_jump *jump;
3259 #if defined SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH == 8
3260 struct sljit_jump *jump2;
3263 SLJIT_UNUSED_ARG(update_str_ptr);
3264 SLJIT_UNUSED_ARG(min);
3265 SLJIT_UNUSED_ARG(max);
3266 SLJIT_ASSERT(min <= max);
3268 OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(0));
3269 OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
3271 #if defined SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH == 8
3274 if (max < 128 && !update_str_ptr) return;
3276 jump = CMP(SLJIT_LESS, TMP1, 0, SLJIT_IMM, 0xc0);
3279 OP2(SLJIT_SUB, TMP2, 0, TMP1, 0, SLJIT_IMM, 0xf0);
3281 OP1(SLJIT_MOV_U8, RETURN_ADDR, 0, SLJIT_MEM1(TMP1), (sljit_sw)PRIV(utf8_table4) - 0xc0);
3282 OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(0));
3283 jump2 = CMP(SLJIT_GREATER, TMP2, 0, SLJIT_IMM, 0x7);
3284 OP2(SLJIT_SHL, TMP2, 0, TMP2, 0, SLJIT_IMM, 6);
3285 OP2(SLJIT_AND, TMP1, 0, TMP1, 0, SLJIT_IMM, 0x3f);
3286 OP2(SLJIT_OR, TMP1, 0, TMP1, 0, TMP2, 0);
3287 OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(1));
3288 OP2(SLJIT_SHL, TMP1, 0, TMP1, 0, SLJIT_IMM, 6);
3289 OP2(SLJIT_AND, TMP2, 0, TMP2, 0, SLJIT_IMM, 0x3f);
3290 OP2(SLJIT_OR, TMP1, 0, TMP1, 0, TMP2, 0);
3291 OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(2));
3292 if (!update_str_ptr)
3293 OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(3));
3294 OP2(SLJIT_SHL, TMP1, 0, TMP1, 0, SLJIT_IMM, 6);
3295 OP2(SLJIT_AND, TMP2, 0, TMP2, 0, SLJIT_IMM, 0x3f);
3296 OP2(SLJIT_OR, TMP1, 0, TMP1, 0, TMP2, 0);
3299 OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, RETURN_ADDR, 0);
3301 else if (min >= 0x800 && max <= 0xffff)
3303 OP2(SLJIT_SUB, TMP2, 0, TMP1, 0, SLJIT_IMM, 0xe0);
3305 OP1(SLJIT_MOV_U8, RETURN_ADDR, 0, SLJIT_MEM1(TMP1), (sljit_sw)PRIV(utf8_table4) - 0xc0);
3306 OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(0));
3307 jump2 = CMP(SLJIT_GREATER, TMP2, 0, SLJIT_IMM, 0xf);
3308 OP2(SLJIT_SHL, TMP2, 0, TMP2, 0, SLJIT_IMM, 6);
3309 OP2(SLJIT_AND, TMP1, 0, TMP1, 0, SLJIT_IMM, 0x3f);
3310 OP2(SLJIT_OR, TMP1, 0, TMP1, 0, TMP2, 0);
3311 OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(1));
3312 if (!update_str_ptr)
3313 OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(2));
3314 OP2(SLJIT_SHL, TMP1, 0, TMP1, 0, SLJIT_IMM, 6);
3315 OP2(SLJIT_AND, TMP2, 0, TMP2, 0, SLJIT_IMM, 0x3f);
3316 OP2(SLJIT_OR, TMP1, 0, TMP1, 0, TMP2, 0);
3319 OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, RETURN_ADDR, 0);
3321 else if (max >= 0x800)
3322 add_jump(compiler, (max < 0x10000) ? &common->utfreadchar16 : &common->utfreadchar, JUMP(SLJIT_FAST_CALL));
3325 OP1(SLJIT_MOV_U8, TMP2, 0, SLJIT_MEM1(TMP1), (sljit_sw)PRIV(utf8_table4) - 0xc0);
3326 OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, TMP2, 0);
3330 OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(0));
3331 if (!update_str_ptr)
3332 OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
3334 OP1(SLJIT_MOV_U8, RETURN_ADDR, 0, SLJIT_MEM1(TMP1), (sljit_sw)PRIV(utf8_table4) - 0xc0);
3335 OP2(SLJIT_AND, TMP1, 0, TMP1, 0, SLJIT_IMM, 0x3f);
3336 OP2(SLJIT_SHL, TMP1, 0, TMP1, 0, SLJIT_IMM, 6);
3337 OP2(SLJIT_AND, TMP2, 0, TMP2, 0, SLJIT_IMM, 0x3f);
3338 OP2(SLJIT_OR, TMP1, 0, TMP1, 0, TMP2, 0);
3340 OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, RETURN_ADDR, 0);
3346 #if defined SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH == 16
3351 OP2(SLJIT_SUB, TMP2, 0, TMP1, 0, SLJIT_IMM, 0xd800);
3352 jump = CMP(SLJIT_GREATER, TMP2, 0, SLJIT_IMM, 0xdc00 - 0xd800 - 1);
3353 /* TMP2 contains the high surrogate. */
3354 OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(0));
3355 OP2(SLJIT_ADD, TMP2, 0, TMP2, 0, SLJIT_IMM, 0x40);
3356 OP2(SLJIT_SHL, TMP2, 0, TMP2, 0, SLJIT_IMM, 10);
3357 OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
3358 OP2(SLJIT_AND, TMP1, 0, TMP1, 0, SLJIT_IMM, 0x3ff);
3359 OP2(SLJIT_OR, TMP1, 0, TMP1, 0, TMP2, 0);
3364 if (max < 0xd800 && !update_str_ptr) return;
3366 /* Skip low surrogate if necessary. */
3367 OP2(SLJIT_SUB, TMP2, 0, TMP1, 0, SLJIT_IMM, 0xd800);
3368 jump = CMP(SLJIT_GREATER, TMP2, 0, SLJIT_IMM, 0xdc00 - 0xd800 - 1);
3370 OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
3372 OP1(SLJIT_MOV, TMP1, 0, SLJIT_IMM, 0x10000);
3378 static SLJIT_INLINE void read_char(compiler_common *common)
3380 read_char_range(common, 0, READ_CHAR_MAX, TRUE);
3383 static void read_char8_type(compiler_common *common, BOOL update_str_ptr)
3385 /* Reads the character type into TMP1, updates STR_PTR. Does not check STR_END. */
3387 #if defined SUPPORT_UNICODE || PCRE2_CODE_UNIT_WIDTH != 8
3388 struct sljit_jump *jump;
3390 #if defined SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH == 8
3391 struct sljit_jump *jump2;
3394 SLJIT_UNUSED_ARG(update_str_ptr);
3396 OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), 0);
3397 OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
3399 #if defined SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH == 8
3402 /* This can be an extra read in some situations, but hopefully
3403 it is needed in most cases. */
3404 OP1(SLJIT_MOV_U8, TMP1, 0, SLJIT_MEM1(TMP2), common->ctypes);
3405 jump = CMP(SLJIT_LESS, TMP2, 0, SLJIT_IMM, 0xc0);
3406 if (!update_str_ptr)
3408 OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(0));
3409 OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
3410 OP2(SLJIT_AND, TMP2, 0, TMP2, 0, SLJIT_IMM, 0x3f);
3411 OP2(SLJIT_SHL, TMP2, 0, TMP2, 0, SLJIT_IMM, 6);
3412 OP2(SLJIT_AND, TMP1, 0, TMP1, 0, SLJIT_IMM, 0x3f);
3413 OP2(SLJIT_OR, TMP2, 0, TMP2, 0, TMP1, 0);
3414 OP1(SLJIT_MOV, TMP1, 0, SLJIT_IMM, 0);
3415 jump2 = CMP(SLJIT_GREATER, TMP2, 0, SLJIT_IMM, 255);
3416 OP1(SLJIT_MOV_U8, TMP1, 0, SLJIT_MEM1(TMP2), common->ctypes);
3420 add_jump(compiler, &common->utfreadtype8, JUMP(SLJIT_FAST_CALL));
3424 #endif /* SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH == 8 */
3426 #if PCRE2_CODE_UNIT_WIDTH != 8
3427 /* The ctypes array contains only 256 values. */
3428 OP1(SLJIT_MOV, TMP1, 0, SLJIT_IMM, 0);
3429 jump = CMP(SLJIT_GREATER, TMP2, 0, SLJIT_IMM, 255);
3431 OP1(SLJIT_MOV_U8, TMP1, 0, SLJIT_MEM1(TMP2), common->ctypes);
3432 #if PCRE2_CODE_UNIT_WIDTH != 8
3436 #if defined SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH == 16
3437 if (common->utf && update_str_ptr)
3439 /* Skip low surrogate if necessary. */
3440 OP2(SLJIT_SUB, TMP2, 0, TMP2, 0, SLJIT_IMM, 0xd800);
3441 jump = CMP(SLJIT_GREATER, TMP2, 0, SLJIT_IMM, 0xdc00 - 0xd800 - 1);
3442 OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
3445 #endif /* SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH == 16 */
3448 static void skip_char_back(compiler_common *common)
3450 /* Goes one character back. Affects STR_PTR and TMP1. Does not check begin. */
3452 #if defined SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH != 32
3453 #if PCRE2_CODE_UNIT_WIDTH == 8
3454 struct sljit_label *label;
3459 OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), -IN_UCHARS(1));
3460 OP2(SLJIT_SUB, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
3461 OP2(SLJIT_AND, TMP1, 0, TMP1, 0, SLJIT_IMM, 0xc0);
3462 CMPTO(SLJIT_EQUAL, TMP1, 0, SLJIT_IMM, 0x80, label);
3465 #elif PCRE2_CODE_UNIT_WIDTH == 16
3468 OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), -IN_UCHARS(1));
3469 OP2(SLJIT_SUB, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
3470 /* Skip low surrogate if necessary. */
3471 OP2(SLJIT_AND, TMP1, 0, TMP1, 0, SLJIT_IMM, 0xfc00);
3472 OP2(SLJIT_SUB | SLJIT_SET_Z, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0xdc00);
3473 OP_FLAGS(SLJIT_MOV, TMP1, 0, SLJIT_EQUAL);
3474 OP2(SLJIT_SHL, TMP1, 0, TMP1, 0, SLJIT_IMM, 1);
3475 OP2(SLJIT_SUB, STR_PTR, 0, STR_PTR, 0, TMP1, 0);
3478 #endif /* PCRE2_CODE_UNIT_WIDTH == [8|16] */
3479 #endif /* SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH != 32 */
3480 OP2(SLJIT_SUB, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
3483 static void check_newlinechar(compiler_common *common, int nltype, jump_list **backtracks, BOOL jumpifmatch)
3485 /* Character comes in TMP1. Checks if it is a newline. TMP2 may be destroyed. */
3487 struct sljit_jump *jump;
3489 if (nltype == NLTYPE_ANY)
3491 add_jump(compiler, &common->anynewline, JUMP(SLJIT_FAST_CALL));
3492 sljit_set_current_flags(compiler, SLJIT_SET_Z);
3493 add_jump(compiler, backtracks, JUMP(jumpifmatch ? SLJIT_NOT_ZERO : SLJIT_ZERO));
3495 else if (nltype == NLTYPE_ANYCRLF)
3499 add_jump(compiler, backtracks, CMP(SLJIT_EQUAL, TMP1, 0, SLJIT_IMM, CHAR_CR));
3500 add_jump(compiler, backtracks, CMP(SLJIT_EQUAL, TMP1, 0, SLJIT_IMM, CHAR_NL));
3504 jump = CMP(SLJIT_EQUAL, TMP1, 0, SLJIT_IMM, CHAR_CR);
3505 add_jump(compiler, backtracks, CMP(SLJIT_NOT_EQUAL, TMP1, 0, SLJIT_IMM, CHAR_NL));
3511 SLJIT_ASSERT(nltype == NLTYPE_FIXED && common->newline < 256);
3512 add_jump(compiler, backtracks, CMP(jumpifmatch ? SLJIT_EQUAL : SLJIT_NOT_EQUAL, TMP1, 0, SLJIT_IMM, common->newline));
3516 #ifdef SUPPORT_UNICODE
3518 #if PCRE2_CODE_UNIT_WIDTH == 8
3519 static void do_utfreadchar(compiler_common *common)
3521 /* Fast decoding a UTF-8 character. TMP1 contains the first byte
3522 of the character (>= 0xc0). Return char value in TMP1, length in TMP2. */
3524 struct sljit_jump *jump;
3526 sljit_emit_fast_enter(compiler, RETURN_ADDR, 0);
3527 OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(0));
3528 OP2(SLJIT_AND, TMP1, 0, TMP1, 0, SLJIT_IMM, 0x3f);
3529 OP2(SLJIT_SHL, TMP1, 0, TMP1, 0, SLJIT_IMM, 6);
3530 OP2(SLJIT_AND, TMP2, 0, TMP2, 0, SLJIT_IMM, 0x3f);
3531 OP2(SLJIT_OR, TMP1, 0, TMP1, 0, TMP2, 0);
3533 /* Searching for the first zero. */
3534 OP2(SLJIT_AND | SLJIT_SET_Z, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x800);
3535 jump = JUMP(SLJIT_NOT_ZERO);
3536 /* Two byte sequence. */
3537 OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
3538 OP1(SLJIT_MOV, TMP2, 0, SLJIT_IMM, IN_UCHARS(2));
3539 sljit_emit_fast_return(compiler, RETURN_ADDR, 0);
3542 OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(1));
3543 OP2(SLJIT_XOR, TMP1, 0, TMP1, 0, SLJIT_IMM, 0x800);
3544 OP2(SLJIT_SHL, TMP1, 0, TMP1, 0, SLJIT_IMM, 6);
3545 OP2(SLJIT_AND, TMP2, 0, TMP2, 0, SLJIT_IMM, 0x3f);
3546 OP2(SLJIT_OR, TMP1, 0, TMP1, 0, TMP2, 0);
3548 OP2(SLJIT_AND | SLJIT_SET_Z, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x10000);
3549 jump = JUMP(SLJIT_NOT_ZERO);
3550 /* Three byte sequence. */
3551 OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(2));
3552 OP1(SLJIT_MOV, TMP2, 0, SLJIT_IMM, IN_UCHARS(3));
3553 sljit_emit_fast_return(compiler, RETURN_ADDR, 0);
3555 /* Four byte sequence. */
3557 OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(2));
3558 OP2(SLJIT_XOR, TMP1, 0, TMP1, 0, SLJIT_IMM, 0x10000);
3559 OP2(SLJIT_SHL, TMP1, 0, TMP1, 0, SLJIT_IMM, 6);
3560 OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(3));
3561 OP2(SLJIT_AND, TMP2, 0, TMP2, 0, SLJIT_IMM, 0x3f);
3562 OP2(SLJIT_OR, TMP1, 0, TMP1, 0, TMP2, 0);
3563 OP1(SLJIT_MOV, TMP2, 0, SLJIT_IMM, IN_UCHARS(4));
3564 sljit_emit_fast_return(compiler, RETURN_ADDR, 0);
3567 static void do_utfreadchar16(compiler_common *common)
3569 /* Fast decoding a UTF-8 character. TMP1 contains the first byte
3570 of the character (>= 0xc0). Return value in TMP1. */
3572 struct sljit_jump *jump;
3574 sljit_emit_fast_enter(compiler, RETURN_ADDR, 0);
3575 OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(0));
3576 OP2(SLJIT_AND, TMP1, 0, TMP1, 0, SLJIT_IMM, 0x3f);
3577 OP2(SLJIT_SHL, TMP1, 0, TMP1, 0, SLJIT_IMM, 6);
3578 OP2(SLJIT_AND, TMP2, 0, TMP2, 0, SLJIT_IMM, 0x3f);
3579 OP2(SLJIT_OR, TMP1, 0, TMP1, 0, TMP2, 0);
3581 /* Searching for the first zero. */
3582 OP2(SLJIT_AND | SLJIT_SET_Z, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x800);
3583 jump = JUMP(SLJIT_NOT_ZERO);
3584 /* Two byte sequence. */
3585 OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
3586 sljit_emit_fast_return(compiler, RETURN_ADDR, 0);
3589 OP2(SLJIT_AND | SLJIT_SET_Z, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x400);
3590 OP_FLAGS(SLJIT_MOV, TMP2, 0, SLJIT_NOT_ZERO);
3591 /* This code runs only in 8 bit mode. No need to shift the value. */
3592 OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, TMP2, 0);
3593 OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(1));
3594 OP2(SLJIT_XOR, TMP1, 0, TMP1, 0, SLJIT_IMM, 0x800);
3595 OP2(SLJIT_SHL, TMP1, 0, TMP1, 0, SLJIT_IMM, 6);
3596 OP2(SLJIT_AND, TMP2, 0, TMP2, 0, SLJIT_IMM, 0x3f);
3597 OP2(SLJIT_OR, TMP1, 0, TMP1, 0, TMP2, 0);
3598 /* Three byte sequence. */
3599 OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(2));
3600 sljit_emit_fast_return(compiler, RETURN_ADDR, 0);
3603 static void do_utfreadtype8(compiler_common *common)
3605 /* Fast decoding a UTF-8 character type. TMP2 contains the first byte
3606 of the character (>= 0xc0). Return value in TMP1. */
3608 struct sljit_jump *jump;
3609 struct sljit_jump *compare;
3611 sljit_emit_fast_enter(compiler, RETURN_ADDR, 0);
3613 OP2(SLJIT_AND | SLJIT_SET_Z, SLJIT_UNUSED, 0, TMP2, 0, SLJIT_IMM, 0x20);
3614 jump = JUMP(SLJIT_NOT_ZERO);
3615 /* Two byte sequence. */
3616 OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(0));
3617 OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
3618 OP2(SLJIT_AND, TMP2, 0, TMP2, 0, SLJIT_IMM, 0x1f);
3619 /* The upper 5 bits are known at this point. */
3620 compare = CMP(SLJIT_GREATER, TMP2, 0, SLJIT_IMM, 0x3);
3621 OP2(SLJIT_SHL, TMP2, 0, TMP2, 0, SLJIT_IMM, 6);
3622 OP2(SLJIT_AND, TMP1, 0, TMP1, 0, SLJIT_IMM, 0x3f);
3623 OP2(SLJIT_OR, TMP2, 0, TMP2, 0, TMP1, 0);
3624 OP1(SLJIT_MOV_U8, TMP1, 0, SLJIT_MEM1(TMP2), common->ctypes);
3625 sljit_emit_fast_return(compiler, RETURN_ADDR, 0);
3628 OP1(SLJIT_MOV, TMP1, 0, SLJIT_IMM, 0);
3629 sljit_emit_fast_return(compiler, RETURN_ADDR, 0);
3631 /* We only have types for characters less than 256. */
3633 OP1(SLJIT_MOV_U8, TMP2, 0, SLJIT_MEM1(TMP2), (sljit_sw)PRIV(utf8_table4) - 0xc0);
3634 OP1(SLJIT_MOV, TMP1, 0, SLJIT_IMM, 0);
3635 OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, TMP2, 0);
3636 sljit_emit_fast_return(compiler, RETURN_ADDR, 0);
3639 #endif /* PCRE2_CODE_UNIT_WIDTH == 8 */
3641 /* UCD_BLOCK_SIZE must be 128 (see the assert below). */
3642 #define UCD_BLOCK_MASK 127
3643 #define UCD_BLOCK_SHIFT 7
3645 static void do_getucd(compiler_common *common)
3647 /* Search the UCD record for the character comes in TMP1.
3648 Returns chartype in TMP1 and UCD offset in TMP2. */
3650 #if PCRE2_CODE_UNIT_WIDTH == 32
3651 struct sljit_jump *jump;
3654 #if defined SLJIT_DEBUG && SLJIT_DEBUG
3655 /* dummy_ucd_record */
3656 const ucd_record *record = GET_UCD(INVALID_UTF_CHAR);
3657 SLJIT_ASSERT(record->script == ucp_Common && record->chartype == ucp_Cn && record->gbprop == ucp_gbOther);
3658 SLJIT_ASSERT(record->caseset == 0 && record->other_case == 0);
3661 SLJIT_ASSERT(UCD_BLOCK_SIZE == 128 && sizeof(ucd_record) == 8);
3663 sljit_emit_fast_enter(compiler, RETURN_ADDR, 0);
3665 #if PCRE2_CODE_UNIT_WIDTH == 32
3668 jump = CMP(SLJIT_LESS, TMP1, 0, SLJIT_IMM, MAX_UTF_CODE_POINT + 1);
3669 OP1(SLJIT_MOV, TMP1, 0, SLJIT_IMM, INVALID_UTF_CHAR);
3674 OP2(SLJIT_LSHR, TMP2, 0, TMP1, 0, SLJIT_IMM, UCD_BLOCK_SHIFT);
3675 OP2(SLJIT_SHL, TMP2, 0, TMP2, 0, SLJIT_IMM, 1);
3676 OP1(SLJIT_MOV_U16, TMP2, 0, SLJIT_MEM1(TMP2), (sljit_sw)PRIV(ucd_stage1));
3677 OP2(SLJIT_AND, TMP1, 0, TMP1, 0, SLJIT_IMM, UCD_BLOCK_MASK);
3678 OP2(SLJIT_SHL, TMP2, 0, TMP2, 0, SLJIT_IMM, UCD_BLOCK_SHIFT);
3679 OP2(SLJIT_ADD, TMP1, 0, TMP1, 0, TMP2, 0);
3680 OP1(SLJIT_MOV, TMP2, 0, SLJIT_IMM, (sljit_sw)PRIV(ucd_stage2));
3681 OP1(SLJIT_MOV_U16, TMP2, 0, SLJIT_MEM2(TMP2, TMP1), 1);
3682 OP1(SLJIT_MOV, TMP1, 0, SLJIT_IMM, (sljit_sw)PRIV(ucd_records) + SLJIT_OFFSETOF(ucd_record, chartype));
3683 OP1(SLJIT_MOV_U8, TMP1, 0, SLJIT_MEM2(TMP1, TMP2), 3);
3684 sljit_emit_fast_return(compiler, RETURN_ADDR, 0);
3687 #endif /* SUPPORT_UNICODE */
3689 static SLJIT_INLINE struct sljit_label *mainloop_entry(compiler_common *common)
3692 struct sljit_label *mainloop;
3693 struct sljit_label *newlinelabel = NULL;
3694 struct sljit_jump *start;
3695 struct sljit_jump *end = NULL;
3696 struct sljit_jump *end2 = NULL;
3697 #if defined SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH != 32
3698 struct sljit_jump *singlechar;
3700 jump_list *newline = NULL;
3701 sljit_u32 overall_options = common->re->overall_options;
3702 BOOL hascrorlf = (common->re->flags & PCRE2_HASCRORLF) != 0;
3703 BOOL newlinecheck = FALSE;
3704 BOOL readuchar = FALSE;
3706 if (!(hascrorlf || (overall_options & PCRE2_FIRSTLINE) != 0)
3707 && (common->nltype == NLTYPE_ANY || common->nltype == NLTYPE_ANYCRLF || common->newline > 255))
3708 newlinecheck = TRUE;
3710 SLJIT_ASSERT(common->abort_label == NULL);
3712 if ((overall_options & PCRE2_FIRSTLINE) != 0)
3714 /* Search for the end of the first line. */
3715 SLJIT_ASSERT(common->match_end_ptr != 0);
3716 OP1(SLJIT_MOV, TMP3, 0, STR_PTR, 0);
3718 if (common->nltype == NLTYPE_FIXED && common->newline > 255)
3721 OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
3722 end = CMP(SLJIT_GREATER_EQUAL, STR_PTR, 0, STR_END, 0);
3723 OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(-1));
3724 OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(0));
3725 CMPTO(SLJIT_NOT_EQUAL, TMP1, 0, SLJIT_IMM, (common->newline >> 8) & 0xff, mainloop);
3726 CMPTO(SLJIT_NOT_EQUAL, TMP2, 0, SLJIT_IMM, common->newline & 0xff, mainloop);
3728 OP2(SLJIT_SUB, SLJIT_MEM1(SLJIT_SP), common->match_end_ptr, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
3732 end = CMP(SLJIT_GREATER_EQUAL, STR_PTR, 0, STR_END, 0);
3734 /* Continual stores does not cause data dependency. */
3735 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->match_end_ptr, STR_PTR, 0);
3736 read_char_range(common, common->nlmin, common->nlmax, TRUE);
3737 check_newlinechar(common, common->nltype, &newline, TRUE);
3738 CMPTO(SLJIT_LESS, STR_PTR, 0, STR_END, 0, mainloop);
3740 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->match_end_ptr, STR_PTR, 0);
3741 set_jumps(newline, LABEL());
3744 OP1(SLJIT_MOV, STR_PTR, 0, TMP3, 0);
3746 else if ((overall_options & PCRE2_USE_OFFSET_LIMIT) != 0)
3748 /* Check whether offset limit is set and valid. */
3749 SLJIT_ASSERT(common->match_end_ptr != 0);
3751 OP1(SLJIT_MOV, TMP1, 0, ARGUMENTS, 0);
3752 OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(TMP1), SLJIT_OFFSETOF(jit_arguments, offset_limit));
3753 OP1(SLJIT_MOV, TMP2, 0, STR_END, 0);
3754 end = CMP(SLJIT_EQUAL, TMP1, 0, SLJIT_IMM, (sljit_sw) PCRE2_UNSET);
3755 OP1(SLJIT_MOV, TMP2, 0, ARGUMENTS, 0);
3756 #if PCRE2_CODE_UNIT_WIDTH == 16
3757 OP2(SLJIT_SHL, TMP1, 0, TMP1, 0, SLJIT_IMM, 1);
3758 #elif PCRE2_CODE_UNIT_WIDTH == 32
3759 OP2(SLJIT_SHL, TMP1, 0, TMP1, 0, SLJIT_IMM, 2);
3761 OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(TMP2), SLJIT_OFFSETOF(jit_arguments, begin));
3762 OP2(SLJIT_ADD, TMP2, 0, TMP2, 0, TMP1, 0);
3763 end2 = CMP(SLJIT_LESS_EQUAL, TMP2, 0, STR_END, 0);
3764 OP1(SLJIT_MOV, TMP2, 0, STR_END, 0);
3766 OP1(SLJIT_MOV, SLJIT_RETURN_REG, 0, SLJIT_IMM, PCRE2_ERROR_NOMATCH);
3767 add_jump(compiler, &common->abort, CMP(SLJIT_LESS, TMP2, 0, STR_PTR, 0));
3769 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->match_end_ptr, TMP2, 0);
3772 start = JUMP(SLJIT_JUMP);
3776 newlinelabel = LABEL();
3777 OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
3778 end = CMP(SLJIT_GREATER_EQUAL, STR_PTR, 0, STR_END, 0);
3779 OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), 0);
3780 OP2(SLJIT_SUB | SLJIT_SET_Z, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, common->newline & 0xff);
3781 OP_FLAGS(SLJIT_MOV, TMP1, 0, SLJIT_EQUAL);
3782 #if PCRE2_CODE_UNIT_WIDTH == 16 || PCRE2_CODE_UNIT_WIDTH == 32
3783 OP2(SLJIT_SHL, TMP1, 0, TMP1, 0, SLJIT_IMM, UCHAR_SHIFT);
3785 OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, TMP1, 0);
3786 end2 = JUMP(SLJIT_JUMP);
3791 /* Increasing the STR_PTR here requires one less jump in the most common case. */
3792 #ifdef SUPPORT_UNICODE
3793 if (common->utf) readuchar = TRUE;
3795 if (newlinecheck) readuchar = TRUE;
3798 OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), 0);
3801 CMPTO(SLJIT_EQUAL, TMP1, 0, SLJIT_IMM, (common->newline >> 8) & 0xff, newlinelabel);
3803 OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
3804 #if defined SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH != 32
3805 #if PCRE2_CODE_UNIT_WIDTH == 8
3808 singlechar = CMP(SLJIT_LESS, TMP1, 0, SLJIT_IMM, 0xc0);
3809 OP1(SLJIT_MOV_U8, TMP1, 0, SLJIT_MEM1(TMP1), (sljit_sw)PRIV(utf8_table4) - 0xc0);
3810 OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, TMP1, 0);
3811 JUMPHERE(singlechar);
3813 #elif PCRE2_CODE_UNIT_WIDTH == 16
3816 singlechar = CMP(SLJIT_LESS, TMP1, 0, SLJIT_IMM, 0xd800);
3817 OP2(SLJIT_AND, TMP1, 0, TMP1, 0, SLJIT_IMM, 0xfc00);
3818 OP2(SLJIT_SUB | SLJIT_SET_Z, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0xd800);
3819 OP_FLAGS(SLJIT_MOV, TMP1, 0, SLJIT_EQUAL);
3820 OP2(SLJIT_SHL, TMP1, 0, TMP1, 0, SLJIT_IMM, 1);
3821 OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, TMP1, 0);
3822 JUMPHERE(singlechar);
3824 #endif /* PCRE2_CODE_UNIT_WIDTH == [8|16] */
3825 #endif /* SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH != 32 */
3838 static SLJIT_INLINE void add_prefix_char(PCRE2_UCHAR chr, fast_forward_char_data *chars, BOOL last)
3840 sljit_u32 i, count = chars->count;
3848 chars->chars[0] = chr;
3851 chars->last_count = 1;
3855 for (i = 0; i < count; i++)
3856 if (chars->chars[i] == chr)
3859 if (count >= MAX_DIFF_CHARS)
3865 chars->chars[count] = chr;
3866 chars->count = count + 1;
3869 chars->last_count++;
3872 static int scan_prefix(compiler_common *common, PCRE2_SPTR cc, fast_forward_char_data *chars, int max_chars, sljit_u32 *rec_count)
3874 /* Recursive function, which scans prefix literals. */
3875 BOOL last, any, class, caseless;
3876 int len, repeat, len_save, consumed = 0;
3877 sljit_u32 chr; /* Any unicode character. */
3878 sljit_u8 *bytes, *bytes_end, byte;
3879 PCRE2_SPTR alternative, cc_save, oc;
3880 #if defined SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH == 8
3881 PCRE2_UCHAR othercase[4];
3882 #elif defined SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH == 16
3883 PCRE2_UCHAR othercase[2];
3885 PCRE2_UCHAR othercase[1];
3891 if (*rec_count == 0)
3913 case OP_NOT_WORD_BOUNDARY:
3914 case OP_WORD_BOUNDARY:
3921 /* Zero width assertions. */
3928 case OP_ASSERTBACK_NOT:
3929 cc = bracketend(cc);
3947 repeat = GET2(cc, 1);
3949 cc += 1 + IMM2_SIZE;
3962 #ifdef SUPPORT_UNICODE
3963 if (common->utf && HAS_EXTRALEN(*cc)) len += GET_EXTRALEN(*cc);
3965 max_chars = scan_prefix(common, cc + len, chars, max_chars, rec_count);
3972 cc += 1 + LINK_SIZE;
3984 alternative = cc + GET(cc, 1);
3985 while (*alternative == OP_ALT)
3987 max_chars = scan_prefix(common, alternative + 1 + LINK_SIZE, chars, max_chars, rec_count);
3990 alternative += GET(alternative, 1);
3993 if (*cc == OP_CBRA || *cc == OP_CBRAPOS)
3995 cc += 1 + LINK_SIZE;
3999 #if defined SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH == 8
4000 if (common->utf && !is_char7_bitset((const sljit_u8 *)(cc + 1), FALSE))
4007 #if defined SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH != 32
4008 if (common->utf) return consumed;
4013 #if defined SUPPORT_UNICODE || PCRE2_CODE_UNIT_WIDTH != 8
4015 #if defined SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH != 32
4016 if (common->utf) return consumed;
4024 #if defined SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH == 8
4025 if (common->utf && !is_char7_bitset((const sljit_u8 *)common->ctypes - cbit_length + cbit_digit, FALSE))
4033 #if defined SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH == 8
4034 if (common->utf && !is_char7_bitset((const sljit_u8 *)common->ctypes - cbit_length + cbit_space, FALSE))
4042 #if defined SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH == 8
4043 if (common->utf && !is_char7_bitset((const sljit_u8 *)common->ctypes - cbit_length + cbit_word, FALSE))
4055 case OP_NOT_WHITESPACE:
4056 case OP_NOT_WORDCHAR:
4059 #if defined SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH != 32
4060 if (common->utf) return consumed;
4066 #ifdef SUPPORT_UNICODE
4069 #if PCRE2_CODE_UNIT_WIDTH != 32
4070 if (common->utf) return consumed;
4078 repeat = GET2(cc, 1);
4079 cc += 1 + IMM2_SIZE;
4084 #if defined SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH != 32
4085 if (common->utf) return consumed;
4088 repeat = GET2(cc, 1);
4089 cc += 1 + IMM2_SIZE + 1;
4103 if (--max_chars == 0)
4107 while (--repeat > 0);
4115 bytes = (sljit_u8*) (cc + 1);
4116 cc += 1 + 32 / sizeof(PCRE2_UCHAR);
4126 max_chars = scan_prefix(common, cc + 1, chars, max_chars, rec_count);
4140 repeat = GET2(cc, 1);
4148 if (bytes[31] & 0x80)
4150 else if (chars->count != 255)
4152 bytes_end = bytes + 32;
4157 SLJIT_ASSERT((chr & 0x7) == 0);
4164 if ((byte & 0x1) != 0)
4165 add_prefix_char(chr, chars, TRUE);
4170 chr = (chr + 7) & ~7;
4173 while (chars->count != 255 && bytes < bytes_end);
4174 bytes = bytes_end - 32;
4178 if (--max_chars == 0)
4182 while (--repeat > 0);
4200 if (GET2(cc, 1) != GET2(cc, 1 + IMM2_SIZE))
4202 cc += 1 + 2 * IMM2_SIZE;
4211 #ifdef SUPPORT_UNICODE
4212 if (common->utf && HAS_EXTRALEN(*cc)) len += GET_EXTRALEN(*cc);
4215 if (caseless && char_has_othercase(common, cc))
4217 #ifdef SUPPORT_UNICODE
4221 if ((int)PRIV(ord2utf)(char_othercase(common, chr), othercase) != len)
4228 othercase[0] = TABLE_GET(chr, common->fcc, chr);
4234 othercase[0] = 0; /* Stops compiler warning - PH */
4248 add_prefix_char(*cc, chars, len == 0);
4251 add_prefix_char(*oc, chars, len == 0);
4253 if (--max_chars == 0)
4274 #if defined SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH != 32
4275 static void jumpto_if_not_utf_char_start(struct sljit_compiler *compiler, sljit_s32 reg, struct sljit_label *label)
4277 #if PCRE2_CODE_UNIT_WIDTH == 8
4278 OP2(SLJIT_AND, reg, 0, reg, 0, SLJIT_IMM, 0xc0);
4279 CMPTO(SLJIT_EQUAL, reg, 0, SLJIT_IMM, 0x80, label);
4280 #elif PCRE2_CODE_UNIT_WIDTH == 16
4281 OP2(SLJIT_AND, reg, 0, reg, 0, SLJIT_IMM, 0xfc00);
4282 CMPTO(SLJIT_EQUAL, reg, 0, SLJIT_IMM, 0xdc00, label);
4284 #error "Unknown code width"
4289 #if (defined SLJIT_CONFIG_X86 && SLJIT_CONFIG_X86) && !(defined SUPPORT_VALGRIND)
4291 #if defined SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH != 32
4292 static struct sljit_jump *jump_if_utf_char_start(struct sljit_compiler *compiler, sljit_s32 reg)
4294 #if PCRE2_CODE_UNIT_WIDTH == 8
4295 OP2(SLJIT_AND, reg, 0, reg, 0, SLJIT_IMM, 0xc0);
4296 return CMP(SLJIT_NOT_EQUAL, reg, 0, SLJIT_IMM, 0x80);
4297 #elif PCRE2_CODE_UNIT_WIDTH == 16
4298 OP2(SLJIT_AND, reg, 0, reg, 0, SLJIT_IMM, 0xfc00);
4299 return CMP(SLJIT_NOT_EQUAL, reg, 0, SLJIT_IMM, 0xdc00);
4301 #error "Unknown code width"
4306 static sljit_s32 character_to_int32(PCRE2_UCHAR chr)
4308 sljit_s32 value = (sljit_s32)chr;
4309 #if PCRE2_CODE_UNIT_WIDTH == 8
4310 #define SSE2_COMPARE_TYPE_INDEX 0
4311 return (value << 24) | (value << 16) | (value << 8) | value;
4312 #elif PCRE2_CODE_UNIT_WIDTH == 16
4313 #define SSE2_COMPARE_TYPE_INDEX 1
4314 return (value << 16) | value;
4315 #elif PCRE2_CODE_UNIT_WIDTH == 32
4316 #define SSE2_COMPARE_TYPE_INDEX 2
4319 #error "Unsupported unit width"
4323 static void load_from_mem_sse2(struct sljit_compiler *compiler, sljit_s32 dst_xmm_reg, sljit_s32 src_general_reg)
4325 #if (defined SLJIT_CONFIG_X86_64 && SLJIT_CONFIG_X86_64)
4326 sljit_u8 instruction[5];
4328 sljit_u8 instruction[4];
4331 SLJIT_ASSERT(dst_xmm_reg < 8);
4333 /* MOVDQA xmm1, xmm2/m128 */
4334 #if (defined SLJIT_CONFIG_X86_64 && SLJIT_CONFIG_X86_64)
4335 if (src_general_reg < 8)
4337 instruction[0] = 0x66;
4338 instruction[1] = 0x0f;
4339 instruction[2] = 0x6f;
4340 instruction[3] = (dst_xmm_reg << 3) | src_general_reg;
4341 sljit_emit_op_custom(compiler, instruction, 4);
4345 instruction[0] = 0x66;
4346 instruction[1] = 0x41;
4347 instruction[2] = 0x0f;
4348 instruction[3] = 0x6f;
4349 instruction[4] = (dst_xmm_reg << 3) | (src_general_reg & 0x7);
4350 sljit_emit_op_custom(compiler, instruction, 4);
4353 instruction[0] = 0x66;
4354 instruction[1] = 0x0f;
4355 instruction[2] = 0x6f;
4356 instruction[3] = (dst_xmm_reg << 3) | src_general_reg;
4357 sljit_emit_op_custom(compiler, instruction, 4);
4361 static void fast_forward_char_pair_sse2_compare(struct sljit_compiler *compiler, PCRE2_UCHAR char1, PCRE2_UCHAR char2,
4362 sljit_u32 bit, sljit_s32 dst_ind, sljit_s32 cmp1_ind, sljit_s32 cmp2_ind, sljit_s32 tmp_ind)
4364 sljit_u8 instruction[4];
4365 instruction[0] = 0x66;
4366 instruction[1] = 0x0f;
4368 if (char1 == char2 || bit != 0)
4372 /* POR xmm1, xmm2/m128 */
4373 /* instruction[0] = 0x66; */
4374 /* instruction[1] = 0x0f; */
4375 instruction[2] = 0xeb;
4376 instruction[3] = 0xc0 | (dst_ind << 3) | cmp2_ind;
4377 sljit_emit_op_custom(compiler, instruction, 4);
4380 /* PCMPEQB/W/D xmm1, xmm2/m128 */
4381 /* instruction[0] = 0x66; */
4382 /* instruction[1] = 0x0f; */
4383 instruction[2] = 0x74 + SSE2_COMPARE_TYPE_INDEX;
4384 instruction[3] = 0xc0 | (dst_ind << 3) | cmp1_ind;
4385 sljit_emit_op_custom(compiler, instruction, 4);
4389 /* MOVDQA xmm1, xmm2/m128 */
4390 /* instruction[0] = 0x66; */
4391 /* instruction[1] = 0x0f; */
4392 instruction[2] = 0x6f;
4393 instruction[3] = 0xc0 | (tmp_ind << 3) | dst_ind;
4394 sljit_emit_op_custom(compiler, instruction, 4);
4396 /* PCMPEQB/W/D xmm1, xmm2/m128 */
4397 /* instruction[0] = 0x66; */
4398 /* instruction[1] = 0x0f; */
4399 instruction[2] = 0x74 + SSE2_COMPARE_TYPE_INDEX;
4400 instruction[3] = 0xc0 | (dst_ind << 3) | cmp1_ind;
4401 sljit_emit_op_custom(compiler, instruction, 4);
4403 instruction[3] = 0xc0 | (tmp_ind << 3) | cmp2_ind;
4404 sljit_emit_op_custom(compiler, instruction, 4);
4406 /* POR xmm1, xmm2/m128 */
4407 /* instruction[0] = 0x66; */
4408 /* instruction[1] = 0x0f; */
4409 instruction[2] = 0xeb;
4410 instruction[3] = 0xc0 | (dst_ind << 3) | tmp_ind;
4411 sljit_emit_op_custom(compiler, instruction, 4);
4415 static void fast_forward_first_char2_sse2(compiler_common *common, PCRE2_UCHAR char1, PCRE2_UCHAR char2, sljit_s32 offset)
4418 struct sljit_label *start;
4419 #if defined SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH != 32
4420 struct sljit_label *restart;
4422 struct sljit_jump *quit;
4423 struct sljit_jump *partial_quit[2];
4424 sljit_u8 instruction[8];
4425 sljit_s32 tmp1_ind = sljit_get_register_index(TMP1);
4426 sljit_s32 str_ptr_ind = sljit_get_register_index(STR_PTR);
4427 sljit_s32 data_ind = 0;
4428 sljit_s32 tmp_ind = 1;
4429 sljit_s32 cmp1_ind = 2;
4430 sljit_s32 cmp2_ind = 3;
4433 SLJIT_UNUSED_ARG(offset);
4437 bit = char1 ^ char2;
4438 if (!is_powerof2(bit))
4442 partial_quit[0] = CMP(SLJIT_GREATER_EQUAL, STR_PTR, 0, STR_END, 0);
4443 if (common->mode == PCRE2_JIT_COMPLETE)
4444 add_jump(compiler, &common->failed_match, partial_quit[0]);
4446 /* First part (unaligned start) */
4448 OP1(SLJIT_MOV, TMP1, 0, SLJIT_IMM, character_to_int32(char1 | bit));
4450 SLJIT_ASSERT(tmp1_ind < 8);
4452 /* MOVD xmm, r/m32 */
4453 instruction[0] = 0x66;
4454 instruction[1] = 0x0f;
4455 instruction[2] = 0x6e;
4456 instruction[3] = 0xc0 | (cmp1_ind << 3) | tmp1_ind;
4457 sljit_emit_op_custom(compiler, instruction, 4);
4461 OP1(SLJIT_MOV, TMP1, 0, SLJIT_IMM, character_to_int32(bit != 0 ? bit : char2));
4463 /* MOVD xmm, r/m32 */
4464 instruction[3] = 0xc0 | (cmp2_ind << 3) | tmp1_ind;
4465 sljit_emit_op_custom(compiler, instruction, 4);
4468 OP1(SLJIT_MOV, TMP2, 0, STR_PTR, 0);
4470 /* PSHUFD xmm1, xmm2/m128, imm8 */
4471 /* instruction[0] = 0x66; */
4472 /* instruction[1] = 0x0f; */
4473 instruction[2] = 0x70;
4474 instruction[3] = 0xc0 | (cmp1_ind << 3) | 2;
4476 sljit_emit_op_custom(compiler, instruction, 5);
4480 /* PSHUFD xmm1, xmm2/m128, imm8 */
4481 instruction[3] = 0xc0 | (cmp2_ind << 3) | 3;
4482 sljit_emit_op_custom(compiler, instruction, 5);
4485 #if defined SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH != 32
4488 OP2(SLJIT_AND, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, ~0xf);
4489 OP2(SLJIT_AND, TMP2, 0, TMP2, 0, SLJIT_IMM, 0xf);
4491 load_from_mem_sse2(compiler, data_ind, str_ptr_ind);
4492 fast_forward_char_pair_sse2_compare(compiler, char1, char2, bit, data_ind, cmp1_ind, cmp2_ind, tmp_ind);
4494 /* PMOVMSKB reg, xmm */
4495 /* instruction[0] = 0x66; */
4496 /* instruction[1] = 0x0f; */
4497 instruction[2] = 0xd7;
4498 instruction[3] = 0xc0 | (tmp1_ind << 3) | 0;
4499 sljit_emit_op_custom(compiler, instruction, 4);
4501 OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, TMP2, 0);
4502 OP2(SLJIT_LSHR, TMP1, 0, TMP1, 0, TMP2, 0);
4504 /* BSF r32, r/m32 */
4505 instruction[0] = 0x0f;
4506 instruction[1] = 0xbc;
4507 instruction[2] = 0xc0 | (tmp1_ind << 3) | tmp1_ind;
4508 sljit_emit_op_custom(compiler, instruction, 3);
4509 sljit_set_current_flags(compiler, SLJIT_SET_Z);
4511 quit = JUMP(SLJIT_NOT_ZERO);
4513 OP2(SLJIT_SUB, STR_PTR, 0, STR_PTR, 0, TMP2, 0);
4516 OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, 16);
4518 partial_quit[1] = CMP(SLJIT_GREATER_EQUAL, STR_PTR, 0, STR_END, 0);
4519 if (common->mode == PCRE2_JIT_COMPLETE)
4520 add_jump(compiler, &common->failed_match, partial_quit[1]);
4522 /* Second part (aligned) */
4524 load_from_mem_sse2(compiler, 0, str_ptr_ind);
4525 fast_forward_char_pair_sse2_compare(compiler, char1, char2, bit, data_ind, cmp1_ind, cmp2_ind, tmp_ind);
4527 /* PMOVMSKB reg, xmm */
4528 instruction[0] = 0x66;
4529 instruction[1] = 0x0f;
4530 instruction[2] = 0xd7;
4531 instruction[3] = 0xc0 | (tmp1_ind << 3) | 0;
4532 sljit_emit_op_custom(compiler, instruction, 4);
4534 /* BSF r32, r/m32 */
4535 instruction[0] = 0x0f;
4536 instruction[1] = 0xbc;
4537 instruction[2] = 0xc0 | (tmp1_ind << 3) | tmp1_ind;
4538 sljit_emit_op_custom(compiler, instruction, 3);
4539 sljit_set_current_flags(compiler, SLJIT_SET_Z);
4541 JUMPTO(SLJIT_ZERO, start);
4544 OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, TMP1, 0);
4546 if (common->mode != PCRE2_JIT_COMPLETE)
4548 JUMPHERE(partial_quit[0]);
4549 JUMPHERE(partial_quit[1]);
4550 OP2(SLJIT_SUB | SLJIT_SET_GREATER, SLJIT_UNUSED, 0, STR_PTR, 0, STR_END, 0);
4551 CMOV(SLJIT_GREATER, STR_PTR, STR_END, 0);
4554 add_jump(compiler, &common->failed_match, CMP(SLJIT_GREATER_EQUAL, STR_PTR, 0, STR_END, 0));
4556 #if defined SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH != 32
4557 if (common->utf && offset > 0)
4559 SLJIT_ASSERT(common->mode == PCRE2_JIT_COMPLETE);
4561 OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(-offset));
4563 quit = jump_if_utf_char_start(compiler, TMP1);
4565 OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
4566 add_jump(compiler, &common->failed_match, CMP(SLJIT_GREATER_EQUAL, STR_PTR, 0, STR_END, 0));
4567 OP1(SLJIT_MOV, TMP2, 0, STR_PTR, 0);
4568 JUMPTO(SLJIT_JUMP, restart);
4577 static SLJIT_INLINE sljit_u32 max_fast_forward_char_pair_sse2_offset(void)
4579 #if PCRE2_CODE_UNIT_WIDTH == 8
4581 #elif PCRE2_CODE_UNIT_WIDTH == 16
4583 #elif PCRE2_CODE_UNIT_WIDTH == 32
4586 #error "Unsupported unit width"
4590 static void fast_forward_char_pair_sse2(compiler_common *common, sljit_s32 offs1,
4591 PCRE2_UCHAR char1a, PCRE2_UCHAR char1b, sljit_s32 offs2, PCRE2_UCHAR char2a, PCRE2_UCHAR char2b)
4596 sljit_u32 diff = IN_UCHARS(offs1 - offs2);
4597 sljit_s32 tmp1_ind = sljit_get_register_index(TMP1);
4598 sljit_s32 tmp2_ind = sljit_get_register_index(TMP2);
4599 sljit_s32 str_ptr_ind = sljit_get_register_index(STR_PTR);
4600 sljit_s32 data1_ind = 0;
4601 sljit_s32 data2_ind = 1;
4602 sljit_s32 tmp_ind = 2;
4603 sljit_s32 cmp1a_ind = 3;
4604 sljit_s32 cmp1b_ind = 4;
4605 sljit_s32 cmp2a_ind = 5;
4606 sljit_s32 cmp2b_ind = 6;
4607 struct sljit_label *start;
4608 #if defined SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH != 32
4609 struct sljit_label *restart;
4611 struct sljit_jump *jump[2];
4613 sljit_u8 instruction[8];
4615 SLJIT_ASSERT(common->mode == PCRE2_JIT_COMPLETE && offs1 > offs2);
4616 SLJIT_ASSERT(diff <= IN_UCHARS(max_fast_forward_char_pair_sse2_offset()));
4617 SLJIT_ASSERT(tmp1_ind < 8 && tmp2_ind == 1);
4620 if (common->match_end_ptr != 0)
4622 OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), common->match_end_ptr);
4623 OP1(SLJIT_MOV, TMP3, 0, STR_END, 0);
4624 OP2(SLJIT_ADD, TMP1, 0, TMP1, 0, SLJIT_IMM, IN_UCHARS(offs1 + 1));
4626 OP2(SLJIT_SUB | SLJIT_SET_LESS, SLJIT_UNUSED, 0, TMP1, 0, STR_END, 0);
4627 CMOV(SLJIT_LESS, STR_END, TMP1, 0);
4630 OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(offs1));
4631 add_jump(compiler, &common->failed_match, CMP(SLJIT_GREATER_EQUAL, STR_PTR, 0, STR_END, 0));
4633 /* MOVD xmm, r/m32 */
4634 instruction[0] = 0x66;
4635 instruction[1] = 0x0f;
4636 instruction[2] = 0x6e;
4638 if (char1a == char1b)
4639 OP1(SLJIT_MOV, TMP1, 0, SLJIT_IMM, character_to_int32(char1a));
4642 bit1 = char1a ^ char1b;
4643 if (is_powerof2(bit1))
4645 OP1(SLJIT_MOV, TMP1, 0, SLJIT_IMM, character_to_int32(char1a | bit1));
4646 OP1(SLJIT_MOV, TMP2, 0, SLJIT_IMM, character_to_int32(bit1));
4651 OP1(SLJIT_MOV, TMP1, 0, SLJIT_IMM, character_to_int32(char1a));
4652 OP1(SLJIT_MOV, TMP2, 0, SLJIT_IMM, character_to_int32(char1b));
4656 instruction[3] = 0xc0 | (cmp1a_ind << 3) | tmp1_ind;
4657 sljit_emit_op_custom(compiler, instruction, 4);
4659 if (char1a != char1b)
4661 instruction[3] = 0xc0 | (cmp1b_ind << 3) | tmp2_ind;
4662 sljit_emit_op_custom(compiler, instruction, 4);
4665 if (char2a == char2b)
4666 OP1(SLJIT_MOV, TMP1, 0, SLJIT_IMM, character_to_int32(char2a));
4669 bit2 = char2a ^ char2b;
4670 if (is_powerof2(bit2))
4672 OP1(SLJIT_MOV, TMP1, 0, SLJIT_IMM, character_to_int32(char2a | bit2));
4673 OP1(SLJIT_MOV, TMP2, 0, SLJIT_IMM, character_to_int32(bit2));
4678 OP1(SLJIT_MOV, TMP1, 0, SLJIT_IMM, character_to_int32(char2a));
4679 OP1(SLJIT_MOV, TMP2, 0, SLJIT_IMM, character_to_int32(char2b));
4683 instruction[3] = 0xc0 | (cmp2a_ind << 3) | tmp1_ind;
4684 sljit_emit_op_custom(compiler, instruction, 4);
4686 if (char2a != char2b)
4688 instruction[3] = 0xc0 | (cmp2b_ind << 3) | tmp2_ind;
4689 sljit_emit_op_custom(compiler, instruction, 4);
4692 /* PSHUFD xmm1, xmm2/m128, imm8 */
4693 /* instruction[0] = 0x66; */
4694 /* instruction[1] = 0x0f; */
4695 instruction[2] = 0x70;
4698 instruction[3] = 0xc0 | (cmp1a_ind << 3) | cmp1a_ind;
4699 sljit_emit_op_custom(compiler, instruction, 5);
4701 if (char1a != char1b)
4703 instruction[3] = 0xc0 | (cmp1b_ind << 3) | cmp1b_ind;
4704 sljit_emit_op_custom(compiler, instruction, 5);
4707 instruction[3] = 0xc0 | (cmp2a_ind << 3) | cmp2a_ind;
4708 sljit_emit_op_custom(compiler, instruction, 5);
4710 if (char2a != char2b)
4712 instruction[3] = 0xc0 | (cmp2b_ind << 3) | cmp2b_ind;
4713 sljit_emit_op_custom(compiler, instruction, 5);
4716 #if defined SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH != 32
4720 OP2(SLJIT_SUB, TMP1, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(offs1 - offs2));
4721 OP1(SLJIT_MOV, TMP2, 0, STR_PTR, 0);
4722 OP2(SLJIT_AND, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, ~0xf);
4723 OP2(SLJIT_AND, TMP1, 0, TMP1, 0, SLJIT_IMM, ~0xf);
4725 load_from_mem_sse2(compiler, data1_ind, str_ptr_ind);
4727 jump[0] = CMP(SLJIT_EQUAL, STR_PTR, 0, TMP1, 0);
4729 load_from_mem_sse2(compiler, data2_ind, tmp1_ind);
4731 /* MOVDQA xmm1, xmm2/m128 */
4732 /* instruction[0] = 0x66; */
4733 /* instruction[1] = 0x0f; */
4734 instruction[2] = 0x6f;
4735 instruction[3] = 0xc0 | (tmp_ind << 3) | data1_ind;
4736 sljit_emit_op_custom(compiler, instruction, 4);
4738 /* PSLLDQ xmm1, xmm2/m128, imm8 */
4739 /* instruction[0] = 0x66; */
4740 /* instruction[1] = 0x0f; */
4741 instruction[2] = 0x73;
4742 instruction[3] = 0xc0 | (7 << 3) | tmp_ind;
4743 instruction[4] = diff;
4744 sljit_emit_op_custom(compiler, instruction, 5);
4746 /* PSRLDQ xmm1, xmm2/m128, imm8 */
4747 /* instruction[0] = 0x66; */
4748 /* instruction[1] = 0x0f; */
4749 /* instruction[2] = 0x73; */
4750 instruction[3] = 0xc0 | (3 << 3) | data2_ind;
4751 instruction[4] = 16 - diff;
4752 sljit_emit_op_custom(compiler, instruction, 5);
4754 /* POR xmm1, xmm2/m128 */
4755 /* instruction[0] = 0x66; */
4756 /* instruction[1] = 0x0f; */
4757 instruction[2] = 0xeb;
4758 instruction[3] = 0xc0 | (data2_ind << 3) | tmp_ind;
4759 sljit_emit_op_custom(compiler, instruction, 4);
4761 jump[1] = JUMP(SLJIT_JUMP);
4765 /* MOVDQA xmm1, xmm2/m128 */
4766 /* instruction[0] = 0x66; */
4767 /* instruction[1] = 0x0f; */
4768 instruction[2] = 0x6f;
4769 instruction[3] = 0xc0 | (data2_ind << 3) | data1_ind;
4770 sljit_emit_op_custom(compiler, instruction, 4);
4772 /* PSLLDQ xmm1, xmm2/m128, imm8 */
4773 /* instruction[0] = 0x66; */
4774 /* instruction[1] = 0x0f; */
4775 instruction[2] = 0x73;
4776 instruction[3] = 0xc0 | (7 << 3) | data2_ind;
4777 instruction[4] = diff;
4778 sljit_emit_op_custom(compiler, instruction, 5);
4782 OP2(SLJIT_AND, TMP2, 0, TMP2, 0, SLJIT_IMM, 0xf);
4784 fast_forward_char_pair_sse2_compare(compiler, char2a, char2b, bit2, data2_ind, cmp2a_ind, cmp2b_ind, tmp_ind);
4785 fast_forward_char_pair_sse2_compare(compiler, char1a, char1b, bit1, data1_ind, cmp1a_ind, cmp1b_ind, tmp_ind);
4787 /* PAND xmm1, xmm2/m128 */
4788 /* instruction[0] = 0x66; */
4789 /* instruction[1] = 0x0f; */
4790 instruction[2] = 0xdb;
4791 instruction[3] = 0xc0 | (data1_ind << 3) | data2_ind;
4792 sljit_emit_op_custom(compiler, instruction, 4);
4794 /* PMOVMSKB reg, xmm */
4795 /* instruction[0] = 0x66; */
4796 /* instruction[1] = 0x0f; */
4797 instruction[2] = 0xd7;
4798 instruction[3] = 0xc0 | (tmp1_ind << 3) | 0;
4799 sljit_emit_op_custom(compiler, instruction, 4);
4801 /* Ignore matches before the first STR_PTR. */
4802 OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, TMP2, 0);
4803 OP2(SLJIT_LSHR, TMP1, 0, TMP1, 0, TMP2, 0);
4805 /* BSF r32, r/m32 */
4806 instruction[0] = 0x0f;
4807 instruction[1] = 0xbc;
4808 instruction[2] = 0xc0 | (tmp1_ind << 3) | tmp1_ind;
4809 sljit_emit_op_custom(compiler, instruction, 3);
4810 sljit_set_current_flags(compiler, SLJIT_SET_Z);
4812 jump[0] = JUMP(SLJIT_NOT_ZERO);
4814 OP2(SLJIT_SUB, STR_PTR, 0, STR_PTR, 0, TMP2, 0);
4817 instruction[0] = 0x66;
4818 instruction[1] = 0x0f;
4822 load_from_mem_sse2(compiler, data2_ind, str_ptr_ind);
4824 OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, 16);
4825 add_jump(compiler, &common->failed_match, CMP(SLJIT_GREATER_EQUAL, STR_PTR, 0, STR_END, 0));
4827 load_from_mem_sse2(compiler, data1_ind, str_ptr_ind);
4829 /* PSRLDQ xmm1, xmm2/m128, imm8 */
4830 /* instruction[0] = 0x66; */
4831 /* instruction[1] = 0x0f; */
4832 instruction[2] = 0x73;
4833 instruction[3] = 0xc0 | (3 << 3) | data2_ind;
4834 instruction[4] = 16 - diff;
4835 sljit_emit_op_custom(compiler, instruction, 5);
4837 /* MOVDQA xmm1, xmm2/m128 */
4838 /* instruction[0] = 0x66; */
4839 /* instruction[1] = 0x0f; */
4840 instruction[2] = 0x6f;
4841 instruction[3] = 0xc0 | (tmp_ind << 3) | data1_ind;
4842 sljit_emit_op_custom(compiler, instruction, 4);
4844 /* PSLLDQ xmm1, xmm2/m128, imm8 */
4845 /* instruction[0] = 0x66; */
4846 /* instruction[1] = 0x0f; */
4847 instruction[2] = 0x73;
4848 instruction[3] = 0xc0 | (7 << 3) | tmp_ind;
4849 instruction[4] = diff;
4850 sljit_emit_op_custom(compiler, instruction, 5);
4852 /* POR xmm1, xmm2/m128 */
4853 /* instruction[0] = 0x66; */
4854 /* instruction[1] = 0x0f; */
4855 instruction[2] = 0xeb;
4856 instruction[3] = 0xc0 | (data2_ind << 3) | tmp_ind;
4857 sljit_emit_op_custom(compiler, instruction, 4);
4859 fast_forward_char_pair_sse2_compare(compiler, char1a, char1b, bit1, data1_ind, cmp1a_ind, cmp1b_ind, tmp_ind);
4860 fast_forward_char_pair_sse2_compare(compiler, char2a, char2b, bit2, data2_ind, cmp2a_ind, cmp2b_ind, tmp_ind);
4862 /* PAND xmm1, xmm2/m128 */
4863 /* instruction[0] = 0x66; */
4864 /* instruction[1] = 0x0f; */
4865 instruction[2] = 0xdb;
4866 instruction[3] = 0xc0 | (data1_ind << 3) | data2_ind;
4867 sljit_emit_op_custom(compiler, instruction, 4);
4869 /* PMOVMSKB reg, xmm */
4870 /* instruction[0] = 0x66; */
4871 /* instruction[1] = 0x0f; */
4872 instruction[2] = 0xd7;
4873 instruction[3] = 0xc0 | (tmp1_ind << 3) | 0;
4874 sljit_emit_op_custom(compiler, instruction, 4);
4876 /* BSF r32, r/m32 */
4877 instruction[0] = 0x0f;
4878 instruction[1] = 0xbc;
4879 instruction[2] = 0xc0 | (tmp1_ind << 3) | tmp1_ind;
4880 sljit_emit_op_custom(compiler, instruction, 3);
4881 sljit_set_current_flags(compiler, SLJIT_SET_Z);
4883 JUMPTO(SLJIT_ZERO, start);
4887 OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, TMP1, 0);
4889 add_jump(compiler, &common->failed_match, CMP(SLJIT_GREATER_EQUAL, STR_PTR, 0, STR_END, 0));
4891 if (common->match_end_ptr != 0)
4892 OP1(SLJIT_MOV, STR_END, 0, SLJIT_MEM1(SLJIT_SP), common->match_end_ptr);
4894 #if defined SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH != 32
4897 OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(-offs1));
4899 jump[0] = jump_if_utf_char_start(compiler, TMP1);
4901 OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
4902 CMPTO(SLJIT_LESS, STR_PTR, 0, STR_END, 0, restart);
4904 add_jump(compiler, &common->failed_match, JUMP(SLJIT_JUMP));
4910 OP2(SLJIT_SUB, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(offs1));
4912 if (common->match_end_ptr != 0)
4913 OP1(SLJIT_MOV, STR_END, 0, TMP3, 0);
4916 static BOOL check_fast_forward_char_pair_sse2(compiler_common *common, fast_forward_char_data *chars, int max)
4918 sljit_s32 i, j, priority, count;
4919 sljit_u32 priorities;
4920 PCRE2_UCHAR a1, a2, b1, b2;
4925 for (i = 0; i < max; i++)
4927 if (chars[i].last_count > 2)
4929 SLJIT_ASSERT(chars[i].last_count <= 7);
4931 priorities |= (1 << chars[i].last_count);
4939 for (priority = 7; priority > 2; priority--)
4941 if ((priorities & (1 << priority)) == 0)
4944 for (i = max - 1; i >= 1; i--)
4945 if (chars[i].last_count >= priority)
4947 SLJIT_ASSERT(chars[i].count <= 2 && chars[i].count >= 1);
4949 a1 = chars[i].chars[0];
4950 a2 = chars[i].chars[1];
4952 j = i - max_fast_forward_char_pair_sse2_offset();
4958 if (chars[j].last_count >= priority)
4960 b1 = chars[j].chars[0];
4961 b2 = chars[j].chars[1];
4963 if (a1 != b1 && a1 != b2 && a2 != b1 && a2 != b2)
4965 fast_forward_char_pair_sse2(common, i, a1, a2, j, b1, b2);
4979 #undef SSE2_COMPARE_TYPE_INDEX
4983 static void fast_forward_first_char2(compiler_common *common, PCRE2_UCHAR char1, PCRE2_UCHAR char2, sljit_s32 offset)
4986 struct sljit_label *start;
4987 struct sljit_jump *match;
4988 struct sljit_jump *partial_quit;
4990 BOOL has_match_end = (common->match_end_ptr != 0);
4992 SLJIT_ASSERT(common->mode == PCRE2_JIT_COMPLETE || offset == 0);
4995 OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), common->match_end_ptr);
4998 OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(offset));
5002 OP1(SLJIT_MOV, TMP3, 0, STR_END, 0);
5004 OP2(SLJIT_ADD, TMP1, 0, TMP1, 0, SLJIT_IMM, IN_UCHARS(offset + 1));
5005 OP2(SLJIT_SUB | SLJIT_SET_GREATER, SLJIT_UNUSED, 0, STR_END, 0, TMP1, 0);
5006 CMOV(SLJIT_GREATER, STR_END, TMP1, 0);
5009 #if (defined SLJIT_CONFIG_X86 && SLJIT_CONFIG_X86) && !(defined SUPPORT_VALGRIND)
5011 /* SSE2 accelerated first character search. */
5013 if (sljit_has_cpu_feature(SLJIT_HAS_SSE2))
5015 fast_forward_first_char2_sse2(common, char1, char2, offset);
5018 OP2(SLJIT_SUB, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(offset));
5021 OP1(SLJIT_MOV, STR_END, 0, TMP3, 0);
5029 partial_quit = CMP(SLJIT_GREATER_EQUAL, STR_PTR, 0, STR_END, 0);
5030 if (common->mode == PCRE2_JIT_COMPLETE)
5031 add_jump(compiler, &common->failed_match, partial_quit);
5033 OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), 0);
5034 OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
5037 CMPTO(SLJIT_NOT_EQUAL, TMP1, 0, SLJIT_IMM, char1, start);
5040 mask = char1 ^ char2;
5041 if (is_powerof2(mask))
5043 OP2(SLJIT_OR, TMP1, 0, TMP1, 0, SLJIT_IMM, mask);
5044 CMPTO(SLJIT_NOT_EQUAL, TMP1, 0, SLJIT_IMM, char1 | mask, start);
5048 match = CMP(SLJIT_EQUAL, TMP1, 0, SLJIT_IMM, char1);
5049 CMPTO(SLJIT_NOT_EQUAL, TMP1, 0, SLJIT_IMM, char2, start);
5054 #if defined SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH != 32
5055 if (common->utf && offset > 0)
5057 OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(-(offset + 1)));
5058 jumpto_if_not_utf_char_start(compiler, TMP1, start);
5062 OP2(SLJIT_SUB, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(offset + 1));
5064 if (common->mode != PCRE2_JIT_COMPLETE)
5065 JUMPHERE(partial_quit);
5068 OP1(SLJIT_MOV, STR_END, 0, TMP3, 0);
5071 static SLJIT_INLINE BOOL fast_forward_first_n_chars(compiler_common *common)
5074 struct sljit_label *start;
5075 struct sljit_jump *match;
5076 fast_forward_char_data chars[MAX_N_CHARS];
5079 PCRE2_UCHAR *char_set, *char_set_end;
5081 int range_right = -1, range_len;
5082 sljit_u8 *update_table = NULL;
5084 sljit_u32 rec_count;
5086 for (i = 0; i < MAX_N_CHARS; i++)
5089 chars[i].last_count = 0;
5093 max = scan_prefix(common, common->start, chars, MAX_N_CHARS, &rec_count);
5098 /* Convert last_count to priority. */
5099 for (i = 0; i < max; i++)
5101 SLJIT_ASSERT(chars[i].count > 0 && chars[i].last_count <= chars[i].count);
5103 if (chars[i].count == 1)
5105 chars[i].last_count = (chars[i].last_count == 1) ? 7 : 5;
5106 /* Simplifies algorithms later. */
5107 chars[i].chars[1] = chars[i].chars[0];
5109 else if (chars[i].count == 2)
5111 SLJIT_ASSERT(chars[i].chars[0] != chars[i].chars[1]);
5113 if (is_powerof2(chars[i].chars[0] ^ chars[i].chars[1]))
5114 chars[i].last_count = (chars[i].last_count == 2) ? 6 : 4;
5116 chars[i].last_count = (chars[i].last_count == 2) ? 3 : 2;
5119 chars[i].last_count = (chars[i].count == 255) ? 0 : 1;
5122 #if (defined SLJIT_CONFIG_X86 && SLJIT_CONFIG_X86) && !(defined SUPPORT_VALGRIND) && !(defined _WIN64)
5123 if (check_fast_forward_char_pair_sse2(common, chars, max))
5128 /* Prevent compiler "uninitialized" warning */
5130 range_len = 4 /* minimum length */ - 1;
5131 for (i = 0; i <= max; i++)
5133 if (in_range && (i - from) > range_len && (chars[i - 1].count < 255))
5135 range_len = i - from;
5136 range_right = i - 1;
5139 if (i < max && chars[i].count < 255)
5141 SLJIT_ASSERT(chars[i].count > 0);
5152 if (range_right >= 0)
5154 update_table = (sljit_u8 *)allocate_read_only_data(common, 256);
5155 if (update_table == NULL)
5157 memset(update_table, IN_UCHARS(range_len), 256);
5159 for (i = 0; i < range_len; i++)
5161 SLJIT_ASSERT(chars[range_right - i].count > 0 && chars[range_right - i].count < 255);
5163 char_set = chars[range_right - i].chars;
5164 char_set_end = char_set + chars[range_right - i].count;
5167 if (update_table[(*char_set) & 0xff] > IN_UCHARS(i))
5168 update_table[(*char_set) & 0xff] = IN_UCHARS(i);
5171 while (char_set < char_set_end);
5177 for (i = 0; i < max; i++)
5179 if (range_right == i)
5184 if (chars[i].last_count >= 2)
5187 else if (chars[offset].last_count < chars[i].last_count)
5191 SLJIT_ASSERT(offset == -1 || (chars[offset].count >= 1 && chars[offset].count <= 2));
5193 if (range_right < 0)
5197 /* Works regardless the value is 1 or 2. */
5198 fast_forward_first_char2(common, chars[offset].chars[0], chars[offset].chars[1], offset);
5202 SLJIT_ASSERT(range_right != offset);
5204 if (common->match_end_ptr != 0)
5206 OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), common->match_end_ptr);
5207 OP1(SLJIT_MOV, TMP3, 0, STR_END, 0);
5208 OP2(SLJIT_SUB, STR_END, 0, STR_END, 0, SLJIT_IMM, IN_UCHARS(max));
5209 OP2(SLJIT_SUB | SLJIT_SET_GREATER, SLJIT_UNUSED, 0, STR_END, 0, TMP1, 0);
5210 CMOV(SLJIT_GREATER, STR_END, TMP1, 0);
5213 OP2(SLJIT_SUB, STR_END, 0, STR_END, 0, SLJIT_IMM, IN_UCHARS(max));
5215 SLJIT_ASSERT(range_right >= 0);
5217 #if !(defined SLJIT_CONFIG_X86_32 && SLJIT_CONFIG_X86_32)
5218 OP1(SLJIT_MOV, RETURN_ADDR, 0, SLJIT_IMM, (sljit_sw)update_table);
5222 add_jump(compiler, &common->failed_match, CMP(SLJIT_GREATER, STR_PTR, 0, STR_END, 0));
5224 #if PCRE2_CODE_UNIT_WIDTH == 8 || (defined SLJIT_LITTLE_ENDIAN && SLJIT_LITTLE_ENDIAN)
5225 OP1(SLJIT_MOV_U8, TMP1, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(range_right));
5227 OP1(SLJIT_MOV_U8, TMP1, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(range_right + 1) - 1);
5230 #if !(defined SLJIT_CONFIG_X86_32 && SLJIT_CONFIG_X86_32)
5231 OP1(SLJIT_MOV_U8, TMP1, 0, SLJIT_MEM2(RETURN_ADDR, TMP1), 0);
5233 OP1(SLJIT_MOV_U8, TMP1, 0, SLJIT_MEM1(TMP1), (sljit_sw)update_table);
5235 OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, TMP1, 0);
5236 CMPTO(SLJIT_NOT_EQUAL, TMP1, 0, SLJIT_IMM, 0, start);
5240 OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(offset));
5241 OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
5243 if (chars[offset].count == 1)
5244 CMPTO(SLJIT_NOT_EQUAL, TMP1, 0, SLJIT_IMM, chars[offset].chars[0], start);
5247 mask = chars[offset].chars[0] ^ chars[offset].chars[1];
5248 if (is_powerof2(mask))
5250 OP2(SLJIT_OR, TMP1, 0, TMP1, 0, SLJIT_IMM, mask);
5251 CMPTO(SLJIT_NOT_EQUAL, TMP1, 0, SLJIT_IMM, chars[offset].chars[0] | mask, start);
5255 match = CMP(SLJIT_EQUAL, TMP1, 0, SLJIT_IMM, chars[offset].chars[0]);
5256 CMPTO(SLJIT_NOT_EQUAL, TMP1, 0, SLJIT_IMM, chars[offset].chars[1], start);
5262 #if defined SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH != 32
5263 if (common->utf && offset != 0)
5267 OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), 0);
5268 OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
5271 OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(-1));
5273 jumpto_if_not_utf_char_start(compiler, TMP1, start);
5276 OP2(SLJIT_SUB, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
5281 OP2(SLJIT_SUB, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
5283 if (common->match_end_ptr != 0)
5284 OP1(SLJIT_MOV, STR_END, 0, TMP3, 0);
5286 OP2(SLJIT_ADD, STR_END, 0, STR_END, 0, SLJIT_IMM, IN_UCHARS(max));
5290 static SLJIT_INLINE void fast_forward_first_char(compiler_common *common)
5292 PCRE2_UCHAR first_char = (PCRE2_UCHAR)(common->re->first_codeunit);
5296 if ((common->re->flags & PCRE2_FIRSTCASELESS) != 0)
5298 oc = TABLE_GET(first_char, common->fcc, first_char);
5299 #if defined SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH != 8
5300 if (first_char > 127 && common->utf)
5301 oc = UCD_OTHERCASE(first_char);
5305 fast_forward_first_char2(common, first_char, oc, 0);
5308 static SLJIT_INLINE void fast_forward_newline(compiler_common *common)
5311 struct sljit_label *loop;
5312 struct sljit_jump *lastchar;
5313 struct sljit_jump *firstchar;
5314 struct sljit_jump *quit;
5315 struct sljit_jump *foundcr = NULL;
5316 struct sljit_jump *notfoundnl;
5317 jump_list *newline = NULL;
5319 if (common->match_end_ptr != 0)
5321 OP1(SLJIT_MOV, TMP3, 0, STR_END, 0);
5322 OP1(SLJIT_MOV, STR_END, 0, SLJIT_MEM1(SLJIT_SP), common->match_end_ptr);
5325 if (common->nltype == NLTYPE_FIXED && common->newline > 255)
5327 lastchar = CMP(SLJIT_GREATER_EQUAL, STR_PTR, 0, STR_END, 0);
5328 OP1(SLJIT_MOV, TMP1, 0, ARGUMENTS, 0);
5329 OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(TMP1), SLJIT_OFFSETOF(jit_arguments, str));
5330 OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(TMP1), SLJIT_OFFSETOF(jit_arguments, begin));
5331 firstchar = CMP(SLJIT_LESS_EQUAL, STR_PTR, 0, TMP2, 0);
5333 OP2(SLJIT_ADD, TMP1, 0, TMP1, 0, SLJIT_IMM, IN_UCHARS(2));
5334 OP2(SLJIT_SUB | SLJIT_SET_GREATER_EQUAL, SLJIT_UNUSED, 0, STR_PTR, 0, TMP1, 0);
5335 OP_FLAGS(SLJIT_MOV, TMP2, 0, SLJIT_GREATER_EQUAL);
5336 #if PCRE2_CODE_UNIT_WIDTH == 16 || PCRE2_CODE_UNIT_WIDTH == 32
5337 OP2(SLJIT_SHL, TMP2, 0, TMP2, 0, SLJIT_IMM, UCHAR_SHIFT);
5339 OP2(SLJIT_SUB, STR_PTR, 0, STR_PTR, 0, TMP2, 0);
5342 OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
5343 quit = CMP(SLJIT_GREATER_EQUAL, STR_PTR, 0, STR_END, 0);
5344 OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(-2));
5345 OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(-1));
5346 CMPTO(SLJIT_NOT_EQUAL, TMP1, 0, SLJIT_IMM, (common->newline >> 8) & 0xff, loop);
5347 CMPTO(SLJIT_NOT_EQUAL, TMP2, 0, SLJIT_IMM, common->newline & 0xff, loop);
5350 JUMPHERE(firstchar);
5353 if (common->match_end_ptr != 0)
5354 OP1(SLJIT_MOV, STR_END, 0, TMP3, 0);
5358 OP1(SLJIT_MOV, TMP1, 0, ARGUMENTS, 0);
5359 OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(TMP1), SLJIT_OFFSETOF(jit_arguments, str));
5360 firstchar = CMP(SLJIT_LESS_EQUAL, STR_PTR, 0, TMP2, 0);
5361 skip_char_back(common);
5364 common->ff_newline_shortcut = loop;
5366 read_char_range(common, common->nlmin, common->nlmax, TRUE);
5367 lastchar = CMP(SLJIT_GREATER_EQUAL, STR_PTR, 0, STR_END, 0);
5368 if (common->nltype == NLTYPE_ANY || common->nltype == NLTYPE_ANYCRLF)
5369 foundcr = CMP(SLJIT_EQUAL, TMP1, 0, SLJIT_IMM, CHAR_CR);
5370 check_newlinechar(common, common->nltype, &newline, FALSE);
5371 set_jumps(newline, loop);
5373 if (common->nltype == NLTYPE_ANY || common->nltype == NLTYPE_ANYCRLF)
5375 quit = JUMP(SLJIT_JUMP);
5377 notfoundnl = CMP(SLJIT_GREATER_EQUAL, STR_PTR, 0, STR_END, 0);
5378 OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), 0);
5379 OP2(SLJIT_SUB | SLJIT_SET_Z, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, CHAR_NL);
5380 OP_FLAGS(SLJIT_MOV, TMP1, 0, SLJIT_EQUAL);
5381 #if PCRE2_CODE_UNIT_WIDTH == 16 || PCRE2_CODE_UNIT_WIDTH == 32
5382 OP2(SLJIT_SHL, TMP1, 0, TMP1, 0, SLJIT_IMM, UCHAR_SHIFT);
5384 OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, TMP1, 0);
5385 JUMPHERE(notfoundnl);
5389 JUMPHERE(firstchar);
5391 if (common->match_end_ptr != 0)
5392 OP1(SLJIT_MOV, STR_END, 0, TMP3, 0);
5395 static BOOL optimize_class(compiler_common *common, const sljit_u8 *bits, BOOL nclass, BOOL invert, jump_list **backtracks);
5397 static SLJIT_INLINE void fast_forward_start_bits(compiler_common *common)
5400 const sljit_u8 *start_bits = common->re->start_bitmap;
5401 struct sljit_label *start;
5402 struct sljit_jump *partial_quit;
5403 #if PCRE2_CODE_UNIT_WIDTH != 8
5404 struct sljit_jump *found = NULL;
5406 jump_list *matches = NULL;
5408 if (common->match_end_ptr != 0)
5410 OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), common->match_end_ptr);
5411 OP1(SLJIT_MOV, RETURN_ADDR, 0, STR_END, 0);
5412 OP2(SLJIT_ADD, TMP1, 0, TMP1, 0, SLJIT_IMM, IN_UCHARS(1));
5413 OP2(SLJIT_SUB | SLJIT_SET_GREATER, SLJIT_UNUSED, 0, STR_END, 0, TMP1, 0);
5414 CMOV(SLJIT_GREATER, STR_END, TMP1, 0);
5419 partial_quit = CMP(SLJIT_GREATER_EQUAL, STR_PTR, 0, STR_END, 0);
5420 if (common->mode == PCRE2_JIT_COMPLETE)
5421 add_jump(compiler, &common->failed_match, partial_quit);
5423 OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), 0);
5424 OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
5426 if (!optimize_class(common, start_bits, (start_bits[31] & 0x80) != 0, FALSE, &matches))
5428 #if PCRE2_CODE_UNIT_WIDTH != 8
5429 if ((start_bits[31] & 0x80) != 0)
5430 found = CMP(SLJIT_GREATER_EQUAL, TMP1, 0, SLJIT_IMM, 255);
5432 CMPTO(SLJIT_GREATER_EQUAL, TMP1, 0, SLJIT_IMM, 255, start);
5433 #elif defined SUPPORT_UNICODE
5434 if (common->utf && is_char7_bitset(start_bits, FALSE))
5435 CMPTO(SLJIT_GREATER, TMP1, 0, SLJIT_IMM, 127, start);
5437 OP2(SLJIT_AND, TMP2, 0, TMP1, 0, SLJIT_IMM, 0x7);
5438 OP2(SLJIT_LSHR, TMP1, 0, TMP1, 0, SLJIT_IMM, 3);
5439 OP1(SLJIT_MOV_U8, TMP1, 0, SLJIT_MEM1(TMP1), (sljit_sw)start_bits);
5440 if (sljit_get_register_index(TMP3) >= 0)
5442 OP2(SLJIT_SHL, TMP3, 0, SLJIT_IMM, 1, TMP2, 0);
5443 OP2(SLJIT_AND | SLJIT_SET_Z, SLJIT_UNUSED, 0, TMP1, 0, TMP3, 0);
5447 OP2(SLJIT_SHL, TMP2, 0, SLJIT_IMM, 1, TMP2, 0);
5448 OP2(SLJIT_AND | SLJIT_SET_Z, SLJIT_UNUSED, 0, TMP1, 0, TMP2, 0);
5450 JUMPTO(SLJIT_ZERO, start);
5453 set_jumps(matches, start);
5455 #if PCRE2_CODE_UNIT_WIDTH != 8
5460 OP2(SLJIT_SUB, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
5462 if (common->mode != PCRE2_JIT_COMPLETE)
5463 JUMPHERE(partial_quit);
5465 if (common->match_end_ptr != 0)
5466 OP1(SLJIT_MOV, STR_END, 0, RETURN_ADDR, 0);
5469 static SLJIT_INLINE struct sljit_jump *search_requested_char(compiler_common *common, PCRE2_UCHAR req_char, BOOL caseless, BOOL has_firstchar)
5472 struct sljit_label *loop;
5473 struct sljit_jump *toolong;
5474 struct sljit_jump *alreadyfound;
5475 struct sljit_jump *found;
5476 struct sljit_jump *foundoc = NULL;
5477 struct sljit_jump *notfound;
5480 SLJIT_ASSERT(common->req_char_ptr != 0);
5481 OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_SP), common->req_char_ptr);
5482 OP2(SLJIT_ADD, TMP1, 0, STR_PTR, 0, SLJIT_IMM, REQ_CU_MAX);
5483 toolong = CMP(SLJIT_LESS, TMP1, 0, STR_END, 0);
5484 alreadyfound = CMP(SLJIT_LESS, STR_PTR, 0, TMP2, 0);
5487 OP2(SLJIT_ADD, TMP1, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
5489 OP1(SLJIT_MOV, TMP1, 0, STR_PTR, 0);
5492 notfound = CMP(SLJIT_GREATER_EQUAL, TMP1, 0, STR_END, 0);
5494 OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(TMP1), 0);
5498 oc = TABLE_GET(req_char, common->fcc, req_char);
5499 #if defined SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH != 8
5500 if (req_char > 127 && common->utf)
5501 oc = UCD_OTHERCASE(req_char);
5505 found = CMP(SLJIT_EQUAL, TMP2, 0, SLJIT_IMM, req_char);
5508 bit = req_char ^ oc;
5509 if (is_powerof2(bit))
5511 OP2(SLJIT_OR, TMP2, 0, TMP2, 0, SLJIT_IMM, bit);
5512 found = CMP(SLJIT_EQUAL, TMP2, 0, SLJIT_IMM, req_char | bit);
5516 found = CMP(SLJIT_EQUAL, TMP2, 0, SLJIT_IMM, req_char);
5517 foundoc = CMP(SLJIT_EQUAL, TMP2, 0, SLJIT_IMM, oc);
5520 OP2(SLJIT_ADD, TMP1, 0, TMP1, 0, SLJIT_IMM, IN_UCHARS(1));
5521 JUMPTO(SLJIT_JUMP, loop);
5526 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->req_char_ptr, TMP1, 0);
5527 JUMPHERE(alreadyfound);
5532 static void do_revertframes(compiler_common *common)
5535 struct sljit_jump *jump;
5536 struct sljit_label *mainloop;
5538 sljit_emit_fast_enter(compiler, RETURN_ADDR, 0);
5539 GET_LOCAL_BASE(TMP1, 0, 0);
5541 /* Drop frames until we reach STACK_TOP. */
5543 OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(STACK_TOP), -sizeof(sljit_sw));
5544 jump = CMP(SLJIT_SIG_LESS_EQUAL, TMP2, 0, SLJIT_IMM, 0);
5546 OP2(SLJIT_ADD, TMP2, 0, TMP2, 0, TMP1, 0);
5547 if (sljit_get_register_index (TMP3) < 0)
5549 OP1(SLJIT_MOV, SLJIT_MEM1(TMP2), 0, SLJIT_MEM1(STACK_TOP), -(2 * sizeof(sljit_sw)));
5550 OP1(SLJIT_MOV, SLJIT_MEM1(TMP2), sizeof(sljit_sw), SLJIT_MEM1(STACK_TOP), -(3 * sizeof(sljit_sw)));
5551 OP2(SLJIT_SUB, STACK_TOP, 0, STACK_TOP, 0, SLJIT_IMM, 3 * sizeof(sljit_sw));
5555 OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(STACK_TOP), -(2 * sizeof(sljit_sw)));
5556 OP1(SLJIT_MOV, TMP3, 0, SLJIT_MEM1(STACK_TOP), -(3 * sizeof(sljit_sw)));
5557 OP2(SLJIT_SUB, STACK_TOP, 0, STACK_TOP, 0, SLJIT_IMM, 3 * sizeof(sljit_sw));
5558 OP1(SLJIT_MOV, SLJIT_MEM1(TMP2), 0, TMP1, 0);
5559 GET_LOCAL_BASE(TMP1, 0, 0);
5560 OP1(SLJIT_MOV, SLJIT_MEM1(TMP2), sizeof(sljit_sw), TMP3, 0);
5562 JUMPTO(SLJIT_JUMP, mainloop);
5565 jump = CMP(SLJIT_NOT_ZERO /* SIG_LESS */, TMP2, 0, SLJIT_IMM, 0);
5566 /* End of reverting values. */
5567 sljit_emit_fast_return(compiler, RETURN_ADDR, 0);
5570 OP1(SLJIT_NEG, TMP2, 0, TMP2, 0);
5571 OP2(SLJIT_ADD, TMP2, 0, TMP2, 0, TMP1, 0);
5572 if (sljit_get_register_index (TMP3) < 0)
5574 OP1(SLJIT_MOV, SLJIT_MEM1(TMP2), 0, SLJIT_MEM1(STACK_TOP), -(2 * sizeof(sljit_sw)));
5575 OP2(SLJIT_SUB, STACK_TOP, 0, STACK_TOP, 0, SLJIT_IMM, 2 * sizeof(sljit_sw));
5579 OP1(SLJIT_MOV, TMP3, 0, SLJIT_MEM1(STACK_TOP), -(2 * sizeof(sljit_sw)));
5580 OP2(SLJIT_SUB, STACK_TOP, 0, STACK_TOP, 0, SLJIT_IMM, 2 * sizeof(sljit_sw));
5581 OP1(SLJIT_MOV, SLJIT_MEM1(TMP2), 0, TMP3, 0);
5583 JUMPTO(SLJIT_JUMP, mainloop);
5586 static void check_wordboundary(compiler_common *common)
5589 struct sljit_jump *skipread;
5590 jump_list *skipread_list = NULL;
5591 #if PCRE2_CODE_UNIT_WIDTH != 8 || defined SUPPORT_UNICODE
5592 struct sljit_jump *jump;
5595 SLJIT_COMPILE_ASSERT(ctype_word == 0x10, ctype_word_must_be_16);
5597 sljit_emit_fast_enter(compiler, SLJIT_MEM1(SLJIT_SP), LOCALS0);
5598 /* Get type of the previous char, and put it to LOCALS1. */
5599 OP1(SLJIT_MOV, TMP1, 0, ARGUMENTS, 0);
5600 OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(TMP1), SLJIT_OFFSETOF(jit_arguments, begin));
5601 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), LOCALS1, SLJIT_IMM, 0);
5602 skipread = CMP(SLJIT_LESS_EQUAL, STR_PTR, 0, TMP1, 0);
5603 skip_char_back(common);
5604 check_start_used_ptr(common);
5607 /* Testing char type. */
5608 #ifdef SUPPORT_UNICODE
5609 if (common->use_ucp)
5611 OP1(SLJIT_MOV, TMP2, 0, SLJIT_IMM, 1);
5612 jump = CMP(SLJIT_EQUAL, TMP1, 0, SLJIT_IMM, CHAR_UNDERSCORE);
5613 add_jump(compiler, &common->getucd, JUMP(SLJIT_FAST_CALL));
5614 OP2(SLJIT_SUB, TMP1, 0, TMP1, 0, SLJIT_IMM, ucp_Ll);
5615 OP2(SLJIT_SUB | SLJIT_SET_LESS_EQUAL, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, ucp_Lu - ucp_Ll);
5616 OP_FLAGS(SLJIT_MOV, TMP2, 0, SLJIT_LESS_EQUAL);
5617 OP2(SLJIT_SUB, TMP1, 0, TMP1, 0, SLJIT_IMM, ucp_Nd - ucp_Ll);
5618 OP2(SLJIT_SUB | SLJIT_SET_LESS_EQUAL, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, ucp_No - ucp_Nd);
5619 OP_FLAGS(SLJIT_OR, TMP2, 0, SLJIT_LESS_EQUAL);
5621 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), LOCALS1, TMP2, 0);
5626 #if PCRE2_CODE_UNIT_WIDTH != 8
5627 jump = CMP(SLJIT_GREATER, TMP1, 0, SLJIT_IMM, 255);
5628 #elif defined SUPPORT_UNICODE
5629 /* Here LOCALS1 has already been zeroed. */
5632 jump = CMP(SLJIT_GREATER, TMP1, 0, SLJIT_IMM, 255);
5633 #endif /* PCRE2_CODE_UNIT_WIDTH == 8 */
5634 OP1(SLJIT_MOV_U8, TMP1, 0, SLJIT_MEM1(TMP1), common->ctypes);
5635 OP2(SLJIT_LSHR, TMP1, 0, TMP1, 0, SLJIT_IMM, 4 /* ctype_word */);
5636 OP2(SLJIT_AND, TMP1, 0, TMP1, 0, SLJIT_IMM, 1);
5637 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), LOCALS1, TMP1, 0);
5638 #if PCRE2_CODE_UNIT_WIDTH != 8
5640 #elif defined SUPPORT_UNICODE
5643 #endif /* PCRE2_CODE_UNIT_WIDTH == 8 */
5647 OP1(SLJIT_MOV, TMP2, 0, SLJIT_IMM, 0);
5648 check_str_end(common, &skipread_list);
5649 peek_char(common, READ_CHAR_MAX);
5651 /* Testing char type. This is a code duplication. */
5652 #ifdef SUPPORT_UNICODE
5653 if (common->use_ucp)
5655 OP1(SLJIT_MOV, TMP2, 0, SLJIT_IMM, 1);
5656 jump = CMP(SLJIT_EQUAL, TMP1, 0, SLJIT_IMM, CHAR_UNDERSCORE);
5657 add_jump(compiler, &common->getucd, JUMP(SLJIT_FAST_CALL));
5658 OP2(SLJIT_SUB, TMP1, 0, TMP1, 0, SLJIT_IMM, ucp_Ll);
5659 OP2(SLJIT_SUB | SLJIT_SET_LESS_EQUAL, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, ucp_Lu - ucp_Ll);
5660 OP_FLAGS(SLJIT_MOV, TMP2, 0, SLJIT_LESS_EQUAL);
5661 OP2(SLJIT_SUB, TMP1, 0, TMP1, 0, SLJIT_IMM, ucp_Nd - ucp_Ll);
5662 OP2(SLJIT_SUB | SLJIT_SET_LESS_EQUAL, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, ucp_No - ucp_Nd);
5663 OP_FLAGS(SLJIT_OR, TMP2, 0, SLJIT_LESS_EQUAL);
5669 #if PCRE2_CODE_UNIT_WIDTH != 8
5670 /* TMP2 may be destroyed by peek_char. */
5671 OP1(SLJIT_MOV, TMP2, 0, SLJIT_IMM, 0);
5672 jump = CMP(SLJIT_GREATER, TMP1, 0, SLJIT_IMM, 255);
5673 #elif defined SUPPORT_UNICODE
5674 OP1(SLJIT_MOV, TMP2, 0, SLJIT_IMM, 0);
5677 jump = CMP(SLJIT_GREATER, TMP1, 0, SLJIT_IMM, 255);
5679 OP1(SLJIT_MOV_U8, TMP2, 0, SLJIT_MEM1(TMP1), common->ctypes);
5680 OP2(SLJIT_LSHR, TMP2, 0, TMP2, 0, SLJIT_IMM, 4 /* ctype_word */);
5681 OP2(SLJIT_AND, TMP2, 0, TMP2, 0, SLJIT_IMM, 1);
5682 #if PCRE2_CODE_UNIT_WIDTH != 8
5684 #elif defined SUPPORT_UNICODE
5687 #endif /* PCRE2_CODE_UNIT_WIDTH == 8 */
5689 set_jumps(skipread_list, LABEL());
5691 OP2(SLJIT_XOR | SLJIT_SET_Z, SLJIT_UNUSED, 0, TMP2, 0, SLJIT_MEM1(SLJIT_SP), LOCALS1);
5692 sljit_emit_fast_return(compiler, SLJIT_MEM1(SLJIT_SP), LOCALS0);
5695 static BOOL optimize_class_ranges(compiler_common *common, const sljit_u8 *bits, BOOL nclass, BOOL invert, jump_list **backtracks)
5697 /* May destroy TMP1. */
5699 int ranges[MAX_CLASS_RANGE_SIZE];
5700 sljit_u8 bit, cbit, all;
5701 int i, byte, length = 0;
5703 bit = bits[0] & 0x1;
5704 /* All bits will be zero or one (since bit is zero or one). */
5707 for (i = 0; i < 256; )
5710 if ((i & 0x7) == 0 && bits[byte] == all)
5714 cbit = (bits[byte] >> (i & 0x7)) & 0x1;
5717 if (length >= MAX_CLASS_RANGE_SIZE)
5728 if (((bit == 0) && nclass) || ((bit == 1) && !nclass))
5730 if (length >= MAX_CLASS_RANGE_SIZE)
5732 ranges[length] = 256;
5736 if (length < 0 || length > 4)
5739 bit = bits[0] & 0x1;
5740 if (invert) bit ^= 0x1;
5742 /* No character is accepted. */
5743 if (length == 0 && bit == 0)
5744 add_jump(compiler, backtracks, JUMP(SLJIT_JUMP));
5749 /* When bit != 0, all characters are accepted. */
5753 add_jump(compiler, backtracks, CMP(bit == 0 ? SLJIT_LESS : SLJIT_GREATER_EQUAL, TMP1, 0, SLJIT_IMM, ranges[0]));
5757 if (ranges[0] + 1 != ranges[1])
5759 OP2(SLJIT_SUB, TMP1, 0, TMP1, 0, SLJIT_IMM, ranges[0]);
5760 add_jump(compiler, backtracks, CMP(bit != 0 ? SLJIT_LESS : SLJIT_GREATER_EQUAL, TMP1, 0, SLJIT_IMM, ranges[1] - ranges[0]));
5763 add_jump(compiler, backtracks, CMP(bit != 0 ? SLJIT_EQUAL : SLJIT_NOT_EQUAL, TMP1, 0, SLJIT_IMM, ranges[0]));
5769 add_jump(compiler, backtracks, CMP(SLJIT_GREATER_EQUAL, TMP1, 0, SLJIT_IMM, ranges[2]));
5770 if (ranges[0] + 1 != ranges[1])
5772 OP2(SLJIT_SUB, TMP1, 0, TMP1, 0, SLJIT_IMM, ranges[0]);
5773 add_jump(compiler, backtracks, CMP(SLJIT_LESS, TMP1, 0, SLJIT_IMM, ranges[1] - ranges[0]));
5776 add_jump(compiler, backtracks, CMP(SLJIT_EQUAL, TMP1, 0, SLJIT_IMM, ranges[0]));
5780 add_jump(compiler, backtracks, CMP(SLJIT_LESS, TMP1, 0, SLJIT_IMM, ranges[0]));
5781 if (ranges[1] + 1 != ranges[2])
5783 OP2(SLJIT_SUB, TMP1, 0, TMP1, 0, SLJIT_IMM, ranges[1]);
5784 add_jump(compiler, backtracks, CMP(SLJIT_LESS, TMP1, 0, SLJIT_IMM, ranges[2] - ranges[1]));
5787 add_jump(compiler, backtracks, CMP(SLJIT_EQUAL, TMP1, 0, SLJIT_IMM, ranges[1]));
5791 if ((ranges[1] - ranges[0]) == (ranges[3] - ranges[2])
5792 && (ranges[0] | (ranges[2] - ranges[0])) == ranges[2]
5793 && (ranges[1] & (ranges[2] - ranges[0])) == 0
5794 && is_powerof2(ranges[2] - ranges[0]))
5796 SLJIT_ASSERT((ranges[0] & (ranges[2] - ranges[0])) == 0 && (ranges[2] & ranges[3] & (ranges[2] - ranges[0])) != 0);
5797 OP2(SLJIT_OR, TMP1, 0, TMP1, 0, SLJIT_IMM, ranges[2] - ranges[0]);
5798 if (ranges[2] + 1 != ranges[3])
5800 OP2(SLJIT_SUB, TMP1, 0, TMP1, 0, SLJIT_IMM, ranges[2]);
5801 add_jump(compiler, backtracks, CMP(bit != 0 ? SLJIT_LESS : SLJIT_GREATER_EQUAL, TMP1, 0, SLJIT_IMM, ranges[3] - ranges[2]));
5804 add_jump(compiler, backtracks, CMP(bit != 0 ? SLJIT_EQUAL : SLJIT_NOT_EQUAL, TMP1, 0, SLJIT_IMM, ranges[2]));
5811 if (ranges[0] + 1 != ranges[1])
5813 OP2(SLJIT_SUB, TMP1, 0, TMP1, 0, SLJIT_IMM, ranges[0]);
5814 add_jump(compiler, backtracks, CMP(SLJIT_LESS, TMP1, 0, SLJIT_IMM, ranges[1] - ranges[0]));
5818 add_jump(compiler, backtracks, CMP(SLJIT_EQUAL, TMP1, 0, SLJIT_IMM, ranges[0]));
5820 if (ranges[2] + 1 != ranges[3])
5822 OP2(SLJIT_SUB, TMP1, 0, TMP1, 0, SLJIT_IMM, ranges[2] - i);
5823 add_jump(compiler, backtracks, CMP(SLJIT_LESS, TMP1, 0, SLJIT_IMM, ranges[3] - ranges[2]));
5826 add_jump(compiler, backtracks, CMP(SLJIT_EQUAL, TMP1, 0, SLJIT_IMM, ranges[2] - i));
5830 OP2(SLJIT_SUB, TMP1, 0, TMP1, 0, SLJIT_IMM, ranges[0]);
5831 add_jump(compiler, backtracks, CMP(SLJIT_GREATER_EQUAL, TMP1, 0, SLJIT_IMM, ranges[3] - ranges[0]));
5832 if (ranges[1] + 1 != ranges[2])
5834 OP2(SLJIT_SUB, TMP1, 0, TMP1, 0, SLJIT_IMM, ranges[1] - ranges[0]);
5835 add_jump(compiler, backtracks, CMP(SLJIT_LESS, TMP1, 0, SLJIT_IMM, ranges[2] - ranges[1]));
5838 add_jump(compiler, backtracks, CMP(SLJIT_EQUAL, TMP1, 0, SLJIT_IMM, ranges[1] - ranges[0]));
5842 SLJIT_UNREACHABLE();
5847 static BOOL optimize_class_chars(compiler_common *common, const sljit_u8 *bits, BOOL nclass, BOOL invert, jump_list **backtracks)
5849 /* May destroy TMP1. */
5851 uint16_t char_list[MAX_CLASS_CHARS_SIZE];
5854 int i, j, k, len, c;
5856 if (!sljit_has_cpu_feature(SLJIT_HAS_CMOV))
5864 for (i = 0; i < 32; i++)
5880 if ((c & 0x20) != 0)
5882 for (k = 0; k < len; k++)
5883 if (char_list[k] == c - 0x20)
5885 char_list[k] |= 0x120;
5892 if (len >= MAX_CLASS_CHARS_SIZE)
5895 char_list[len++] = (uint16_t) c;
5904 if (len == 0) return FALSE; /* Should never occur, but stops analyzers complaining. */
5909 if (char_list[0] == 0)
5912 OP2(SLJIT_SUB | SLJIT_SET_Z, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0);
5913 OP_FLAGS(SLJIT_MOV, TMP2, 0, SLJIT_ZERO);
5916 OP1(SLJIT_MOV, TMP2, 0, SLJIT_IMM, 0);
5920 if ((char_list[i] & 0x100) != 0)
5924 OP2(SLJIT_SUB | SLJIT_SET_Z, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, char_list[i]);
5925 CMOV(SLJIT_ZERO, TMP2, TMP1, 0);
5932 OP2(SLJIT_OR, TMP1, 0, TMP1, 0, SLJIT_IMM, 0x20);
5934 for (i = 0; i < len; i++)
5935 if ((char_list[i] & 0x100) != 0)
5938 OP2(SLJIT_SUB | SLJIT_SET_Z, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, char_list[i] & 0xff);
5939 CMOV(SLJIT_ZERO, TMP2, TMP1, 0);
5943 type = nclass ? SLJIT_NOT_EQUAL : SLJIT_EQUAL;
5944 add_jump(compiler, backtracks, CMP(type, TMP2, 0, SLJIT_IMM, 0));
5948 static BOOL optimize_class(compiler_common *common, const sljit_u8 *bits, BOOL nclass, BOOL invert, jump_list **backtracks)
5950 /* May destroy TMP1. */
5951 if (optimize_class_ranges(common, bits, nclass, invert, backtracks))
5953 return optimize_class_chars(common, bits, nclass, invert, backtracks);
5956 static void check_anynewline(compiler_common *common)
5958 /* Check whether TMP1 contains a newline character. TMP2 destroyed. */
5961 sljit_emit_fast_enter(compiler, RETURN_ADDR, 0);
5963 OP2(SLJIT_SUB, TMP1, 0, TMP1, 0, SLJIT_IMM, 0x0a);
5964 OP2(SLJIT_SUB | SLJIT_SET_LESS_EQUAL, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x0d - 0x0a);
5965 OP_FLAGS(SLJIT_MOV, TMP2, 0, SLJIT_LESS_EQUAL);
5966 OP2(SLJIT_SUB | SLJIT_SET_Z, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x85 - 0x0a);
5967 #if defined SUPPORT_UNICODE || PCRE2_CODE_UNIT_WIDTH == 16 || PCRE2_CODE_UNIT_WIDTH == 32
5968 #if PCRE2_CODE_UNIT_WIDTH == 8
5972 OP_FLAGS(SLJIT_OR, TMP2, 0, SLJIT_EQUAL);
5973 OP2(SLJIT_OR, TMP1, 0, TMP1, 0, SLJIT_IMM, 0x1);
5974 OP2(SLJIT_SUB | SLJIT_SET_Z, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x2029 - 0x0a);
5975 #if PCRE2_CODE_UNIT_WIDTH == 8
5978 #endif /* SUPPORT_UNICODE || PCRE2_CODE_UNIT_WIDTH == [16|32] */
5979 OP_FLAGS(SLJIT_OR | SLJIT_SET_Z, TMP2, 0, SLJIT_EQUAL);
5980 sljit_emit_fast_return(compiler, RETURN_ADDR, 0);
5983 static void check_hspace(compiler_common *common)
5985 /* Check whether TMP1 contains a newline character. TMP2 destroyed. */
5988 sljit_emit_fast_enter(compiler, RETURN_ADDR, 0);
5990 OP2(SLJIT_SUB | SLJIT_SET_Z, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x09);
5991 OP_FLAGS(SLJIT_MOV, TMP2, 0, SLJIT_EQUAL);
5992 OP2(SLJIT_SUB | SLJIT_SET_Z, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x20);
5993 OP_FLAGS(SLJIT_OR, TMP2, 0, SLJIT_EQUAL);
5994 OP2(SLJIT_SUB | SLJIT_SET_Z, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0xa0);
5995 #if defined SUPPORT_UNICODE || PCRE2_CODE_UNIT_WIDTH == 16 || PCRE2_CODE_UNIT_WIDTH == 32
5996 #if PCRE2_CODE_UNIT_WIDTH == 8
6000 OP_FLAGS(SLJIT_OR, TMP2, 0, SLJIT_EQUAL);
6001 OP2(SLJIT_SUB | SLJIT_SET_Z, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x1680);
6002 OP_FLAGS(SLJIT_OR, TMP2, 0, SLJIT_EQUAL);
6003 OP2(SLJIT_SUB | SLJIT_SET_Z, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x180e);
6004 OP_FLAGS(SLJIT_OR, TMP2, 0, SLJIT_EQUAL);
6005 OP2(SLJIT_SUB, TMP1, 0, TMP1, 0, SLJIT_IMM, 0x2000);
6006 OP2(SLJIT_SUB | SLJIT_SET_LESS_EQUAL, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x200A - 0x2000);
6007 OP_FLAGS(SLJIT_OR, TMP2, 0, SLJIT_LESS_EQUAL);
6008 OP2(SLJIT_SUB | SLJIT_SET_Z, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x202f - 0x2000);
6009 OP_FLAGS(SLJIT_OR, TMP2, 0, SLJIT_EQUAL);
6010 OP2(SLJIT_SUB | SLJIT_SET_Z, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x205f - 0x2000);
6011 OP_FLAGS(SLJIT_OR, TMP2, 0, SLJIT_EQUAL);
6012 OP2(SLJIT_SUB | SLJIT_SET_Z, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x3000 - 0x2000);
6013 #if PCRE2_CODE_UNIT_WIDTH == 8
6016 #endif /* SUPPORT_UNICODE || PCRE2_CODE_UNIT_WIDTH == [16|32] */
6017 OP_FLAGS(SLJIT_OR | SLJIT_SET_Z, TMP2, 0, SLJIT_EQUAL);
6019 sljit_emit_fast_return(compiler, RETURN_ADDR, 0);
6022 static void check_vspace(compiler_common *common)
6024 /* Check whether TMP1 contains a newline character. TMP2 destroyed. */
6027 sljit_emit_fast_enter(compiler, RETURN_ADDR, 0);
6029 OP2(SLJIT_SUB, TMP1, 0, TMP1, 0, SLJIT_IMM, 0x0a);
6030 OP2(SLJIT_SUB | SLJIT_SET_LESS_EQUAL, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x0d - 0x0a);
6031 OP_FLAGS(SLJIT_MOV, TMP2, 0, SLJIT_LESS_EQUAL);
6032 OP2(SLJIT_SUB | SLJIT_SET_Z, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x85 - 0x0a);
6033 #if defined SUPPORT_UNICODE || PCRE2_CODE_UNIT_WIDTH == 16 || PCRE2_CODE_UNIT_WIDTH == 32
6034 #if PCRE2_CODE_UNIT_WIDTH == 8
6038 OP_FLAGS(SLJIT_OR, TMP2, 0, SLJIT_EQUAL);
6039 OP2(SLJIT_OR, TMP1, 0, TMP1, 0, SLJIT_IMM, 0x1);
6040 OP2(SLJIT_SUB | SLJIT_SET_Z, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x2029 - 0x0a);
6041 #if PCRE2_CODE_UNIT_WIDTH == 8
6044 #endif /* SUPPORT_UNICODE || PCRE2_CODE_UNIT_WIDTH == [16|32] */
6045 OP_FLAGS(SLJIT_OR | SLJIT_SET_Z, TMP2, 0, SLJIT_EQUAL);
6047 sljit_emit_fast_return(compiler, RETURN_ADDR, 0);
6050 static void do_casefulcmp(compiler_common *common)
6053 struct sljit_jump *jump;
6054 struct sljit_label *label;
6058 if (sljit_get_register_index(TMP3) < 0)
6060 char1_reg = STR_END;
6061 char2_reg = STACK_TOP;
6066 char2_reg = RETURN_ADDR;
6069 sljit_emit_fast_enter(compiler, SLJIT_MEM1(SLJIT_SP), LOCALS0);
6070 OP2(SLJIT_SUB, STR_PTR, 0, STR_PTR, 0, TMP2, 0);
6072 if (char1_reg == STR_END)
6074 OP1(SLJIT_MOV, TMP3, 0, char1_reg, 0);
6075 OP1(SLJIT_MOV, RETURN_ADDR, 0, char2_reg, 0);
6078 if (sljit_emit_mem(compiler, MOV_UCHAR | SLJIT_MEM_SUPP | SLJIT_MEM_POST, char1_reg, SLJIT_MEM1(TMP1), IN_UCHARS(1)) == SLJIT_SUCCESS)
6081 sljit_emit_mem(compiler, MOV_UCHAR | SLJIT_MEM_POST, char1_reg, SLJIT_MEM1(TMP1), IN_UCHARS(1));
6082 sljit_emit_mem(compiler, MOV_UCHAR | SLJIT_MEM_POST, char2_reg, SLJIT_MEM1(STR_PTR), IN_UCHARS(1));
6083 jump = CMP(SLJIT_NOT_EQUAL, char1_reg, 0, char2_reg, 0);
6084 OP2(SLJIT_SUB | SLJIT_SET_Z, TMP2, 0, TMP2, 0, SLJIT_IMM, IN_UCHARS(1));
6085 JUMPTO(SLJIT_NOT_ZERO, label);
6088 OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), LOCALS0);
6090 else if (sljit_emit_mem(compiler, MOV_UCHAR | SLJIT_MEM_SUPP | SLJIT_MEM_PRE, char1_reg, SLJIT_MEM1(TMP1), IN_UCHARS(1)) == SLJIT_SUCCESS)
6092 OP2(SLJIT_SUB, TMP1, 0, TMP1, 0, SLJIT_IMM, IN_UCHARS(1));
6093 OP2(SLJIT_SUB, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
6096 sljit_emit_mem(compiler, MOV_UCHAR | SLJIT_MEM_PRE, char1_reg, SLJIT_MEM1(TMP1), IN_UCHARS(1));
6097 sljit_emit_mem(compiler, MOV_UCHAR | SLJIT_MEM_PRE, char2_reg, SLJIT_MEM1(STR_PTR), IN_UCHARS(1));
6098 jump = CMP(SLJIT_NOT_EQUAL, char1_reg, 0, char2_reg, 0);
6099 OP2(SLJIT_SUB | SLJIT_SET_Z, TMP2, 0, TMP2, 0, SLJIT_IMM, IN_UCHARS(1));
6100 JUMPTO(SLJIT_NOT_ZERO, label);
6103 OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), LOCALS0);
6104 OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
6109 OP1(MOV_UCHAR, char1_reg, 0, SLJIT_MEM1(TMP1), 0);
6110 OP1(MOV_UCHAR, char2_reg, 0, SLJIT_MEM1(STR_PTR), 0);
6111 OP2(SLJIT_ADD, TMP1, 0, TMP1, 0, SLJIT_IMM, IN_UCHARS(1));
6112 OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
6113 jump = CMP(SLJIT_NOT_EQUAL, char1_reg, 0, char2_reg, 0);
6114 OP2(SLJIT_SUB | SLJIT_SET_Z, TMP2, 0, TMP2, 0, SLJIT_IMM, IN_UCHARS(1));
6115 JUMPTO(SLJIT_NOT_ZERO, label);
6118 OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), LOCALS0);
6121 if (char1_reg == STR_END)
6123 OP1(SLJIT_MOV, char1_reg, 0, TMP3, 0);
6124 OP1(SLJIT_MOV, char2_reg, 0, RETURN_ADDR, 0);
6127 sljit_emit_fast_return(compiler, TMP1, 0);
6130 static void do_caselesscmp(compiler_common *common)
6133 struct sljit_jump *jump;
6134 struct sljit_label *label;
6135 int char1_reg = STR_END;
6140 if (sljit_get_register_index(TMP3) < 0)
6142 char2_reg = STACK_TOP;
6143 lcc_table = STACK_LIMIT;
6147 char2_reg = RETURN_ADDR;
6151 if (sljit_emit_mem(compiler, MOV_UCHAR | SLJIT_MEM_SUPP | SLJIT_MEM_POST, char1_reg, SLJIT_MEM1(TMP1), IN_UCHARS(1)) == SLJIT_SUCCESS)
6153 else if (sljit_emit_mem(compiler, MOV_UCHAR | SLJIT_MEM_SUPP | SLJIT_MEM_PRE, char1_reg, SLJIT_MEM1(TMP1), IN_UCHARS(1)) == SLJIT_SUCCESS)
6156 sljit_emit_fast_enter(compiler, SLJIT_MEM1(SLJIT_SP), LOCALS0);
6157 OP2(SLJIT_SUB, STR_PTR, 0, STR_PTR, 0, TMP2, 0);
6159 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), LOCALS1, char1_reg, 0);
6161 if (char2_reg == STACK_TOP)
6163 OP1(SLJIT_MOV, TMP3, 0, char2_reg, 0);
6164 OP1(SLJIT_MOV, RETURN_ADDR, 0, lcc_table, 0);
6167 OP1(SLJIT_MOV, lcc_table, 0, SLJIT_IMM, common->lcc);
6172 sljit_emit_mem(compiler, MOV_UCHAR | SLJIT_MEM_POST, char1_reg, SLJIT_MEM1(TMP1), IN_UCHARS(1));
6173 sljit_emit_mem(compiler, MOV_UCHAR | SLJIT_MEM_POST, char2_reg, SLJIT_MEM1(STR_PTR), IN_UCHARS(1));
6175 else if (opt_type == 2)
6177 OP2(SLJIT_SUB, TMP1, 0, TMP1, 0, SLJIT_IMM, IN_UCHARS(1));
6178 OP2(SLJIT_SUB, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
6181 sljit_emit_mem(compiler, MOV_UCHAR | SLJIT_MEM_PRE, char1_reg, SLJIT_MEM1(TMP1), IN_UCHARS(1));
6182 sljit_emit_mem(compiler, MOV_UCHAR | SLJIT_MEM_PRE, char2_reg, SLJIT_MEM1(STR_PTR), IN_UCHARS(1));
6187 OP1(MOV_UCHAR, char1_reg, 0, SLJIT_MEM1(TMP1), 0);
6188 OP1(MOV_UCHAR, char2_reg, 0, SLJIT_MEM1(STR_PTR), 0);
6189 OP2(SLJIT_ADD, TMP1, 0, TMP1, 0, SLJIT_IMM, IN_UCHARS(1));
6192 #if PCRE2_CODE_UNIT_WIDTH != 8
6193 jump = CMP(SLJIT_GREATER, char1_reg, 0, SLJIT_IMM, 255);
6195 OP1(SLJIT_MOV_U8, char1_reg, 0, SLJIT_MEM2(lcc_table, char1_reg), 0);
6196 #if PCRE2_CODE_UNIT_WIDTH != 8
6198 jump = CMP(SLJIT_GREATER, char2_reg, 0, SLJIT_IMM, 255);
6200 OP1(SLJIT_MOV_U8, char2_reg, 0, SLJIT_MEM2(lcc_table, char2_reg), 0);
6201 #if PCRE2_CODE_UNIT_WIDTH != 8
6206 OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
6208 jump = CMP(SLJIT_NOT_EQUAL, char1_reg, 0, char2_reg, 0);
6209 OP2(SLJIT_SUB | SLJIT_SET_Z, TMP2, 0, TMP2, 0, SLJIT_IMM, IN_UCHARS(1));
6210 JUMPTO(SLJIT_NOT_ZERO, label);
6213 OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), LOCALS0);
6216 OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
6218 if (char2_reg == STACK_TOP)
6220 OP1(SLJIT_MOV, char2_reg, 0, TMP3, 0);
6221 OP1(SLJIT_MOV, lcc_table, 0, RETURN_ADDR, 0);
6224 OP1(SLJIT_MOV, char1_reg, 0, SLJIT_MEM1(SLJIT_SP), LOCALS1);
6225 sljit_emit_fast_return(compiler, TMP1, 0);
6228 #if defined SUPPORT_UNICODE
6230 static PCRE2_SPTR SLJIT_FUNC do_utf_caselesscmp(PCRE2_SPTR src1, PCRE2_SPTR src2, PCRE2_SPTR end1, PCRE2_SPTR end2)
6232 /* This function would be ineffective to do in JIT level. */
6234 const ucd_record *ur;
6235 const sljit_u32 *pp;
6240 return (PCRE2_SPTR)1;
6241 GETCHARINC(c1, src1);
6242 GETCHARINC(c2, src2);
6244 if (c1 != c2 && c1 != c2 + ur->other_case)
6246 pp = PRIV(ucd_caseless_sets) + ur->caseset;
6249 if (c1 < *pp) return NULL;
6250 if (c1 == *pp++) break;
6257 #endif /* SUPPORT_UNICODE */
6259 static PCRE2_SPTR byte_sequence_compare(compiler_common *common, BOOL caseless, PCRE2_SPTR cc,
6260 compare_context *context, jump_list **backtracks)
6263 unsigned int othercasebit = 0;
6264 PCRE2_SPTR othercasechar = NULL;
6265 #ifdef SUPPORT_UNICODE
6269 if (caseless && char_has_othercase(common, cc))
6271 othercasebit = char_get_othercase_bit(common, cc);
6272 SLJIT_ASSERT(othercasebit);
6273 /* Extracting bit difference info. */
6274 #if PCRE2_CODE_UNIT_WIDTH == 8
6275 othercasechar = cc + (othercasebit >> 8);
6276 othercasebit &= 0xff;
6277 #elif PCRE2_CODE_UNIT_WIDTH == 16 || PCRE2_CODE_UNIT_WIDTH == 32
6278 /* Note that this code only handles characters in the BMP. If there
6279 ever are characters outside the BMP whose othercase differs in only one
6280 bit from itself (there currently are none), this code will need to be
6281 revised for PCRE2_CODE_UNIT_WIDTH == 32. */
6282 othercasechar = cc + (othercasebit >> 9);
6283 if ((othercasebit & 0x100) != 0)
6284 othercasebit = (othercasebit & 0xff) << 8;
6286 othercasebit &= 0xff;
6287 #endif /* PCRE2_CODE_UNIT_WIDTH == [8|16|32] */
6290 if (context->sourcereg == -1)
6292 #if PCRE2_CODE_UNIT_WIDTH == 8
6293 #if defined SLJIT_UNALIGNED && SLJIT_UNALIGNED
6294 if (context->length >= 4)
6295 OP1(SLJIT_MOV_S32, TMP1, 0, SLJIT_MEM1(STR_PTR), -context->length);
6296 else if (context->length >= 2)
6297 OP1(SLJIT_MOV_U16, TMP1, 0, SLJIT_MEM1(STR_PTR), -context->length);
6300 OP1(SLJIT_MOV_U8, TMP1, 0, SLJIT_MEM1(STR_PTR), -context->length);
6301 #elif PCRE2_CODE_UNIT_WIDTH == 16
6302 #if defined SLJIT_UNALIGNED && SLJIT_UNALIGNED
6303 if (context->length >= 4)
6304 OP1(SLJIT_MOV_S32, TMP1, 0, SLJIT_MEM1(STR_PTR), -context->length);
6307 OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), -context->length);
6308 #elif PCRE2_CODE_UNIT_WIDTH == 32
6309 OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), -context->length);
6310 #endif /* PCRE2_CODE_UNIT_WIDTH == [8|16|32] */
6311 context->sourcereg = TMP2;
6314 #ifdef SUPPORT_UNICODE
6316 if (common->utf && HAS_EXTRALEN(*cc))
6317 utflength += GET_EXTRALEN(*cc);
6323 context->length -= IN_UCHARS(1);
6324 #if (defined SLJIT_UNALIGNED && SLJIT_UNALIGNED) && (PCRE2_CODE_UNIT_WIDTH == 8 || PCRE2_CODE_UNIT_WIDTH == 16)
6326 /* Unaligned read is supported. */
6327 if (othercasebit != 0 && othercasechar == cc)
6329 context->c.asuchars[context->ucharptr] = *cc | othercasebit;
6330 context->oc.asuchars[context->ucharptr] = othercasebit;
6334 context->c.asuchars[context->ucharptr] = *cc;
6335 context->oc.asuchars[context->ucharptr] = 0;
6337 context->ucharptr++;
6339 #if PCRE2_CODE_UNIT_WIDTH == 8
6340 if (context->ucharptr >= 4 || context->length == 0 || (context->ucharptr == 2 && context->length == 1))
6342 if (context->ucharptr >= 2 || context->length == 0)
6345 if (context->length >= 4)
6346 OP1(SLJIT_MOV_S32, context->sourcereg, 0, SLJIT_MEM1(STR_PTR), -context->length);
6347 else if (context->length >= 2)
6348 OP1(SLJIT_MOV_U16, context->sourcereg, 0, SLJIT_MEM1(STR_PTR), -context->length);
6349 #if PCRE2_CODE_UNIT_WIDTH == 8
6350 else if (context->length >= 1)
6351 OP1(SLJIT_MOV_U8, context->sourcereg, 0, SLJIT_MEM1(STR_PTR), -context->length);
6352 #endif /* PCRE2_CODE_UNIT_WIDTH == 8 */
6353 context->sourcereg = context->sourcereg == TMP1 ? TMP2 : TMP1;
6355 switch(context->ucharptr)
6357 case 4 / sizeof(PCRE2_UCHAR):
6358 if (context->oc.asint != 0)
6359 OP2(SLJIT_OR, context->sourcereg, 0, context->sourcereg, 0, SLJIT_IMM, context->oc.asint);
6360 add_jump(compiler, backtracks, CMP(SLJIT_NOT_EQUAL, context->sourcereg, 0, SLJIT_IMM, context->c.asint | context->oc.asint));
6363 case 2 / sizeof(PCRE2_UCHAR):
6364 if (context->oc.asushort != 0)
6365 OP2(SLJIT_OR, context->sourcereg, 0, context->sourcereg, 0, SLJIT_IMM, context->oc.asushort);
6366 add_jump(compiler, backtracks, CMP(SLJIT_NOT_EQUAL, context->sourcereg, 0, SLJIT_IMM, context->c.asushort | context->oc.asushort));
6369 #if PCRE2_CODE_UNIT_WIDTH == 8
6371 if (context->oc.asbyte != 0)
6372 OP2(SLJIT_OR, context->sourcereg, 0, context->sourcereg, 0, SLJIT_IMM, context->oc.asbyte);
6373 add_jump(compiler, backtracks, CMP(SLJIT_NOT_EQUAL, context->sourcereg, 0, SLJIT_IMM, context->c.asbyte | context->oc.asbyte));
6378 SLJIT_UNREACHABLE();
6381 context->ucharptr = 0;
6386 /* Unaligned read is unsupported or in 32 bit mode. */
6387 if (context->length >= 1)
6388 OP1(MOV_UCHAR, context->sourcereg, 0, SLJIT_MEM1(STR_PTR), -context->length);
6390 context->sourcereg = context->sourcereg == TMP1 ? TMP2 : TMP1;
6392 if (othercasebit != 0 && othercasechar == cc)
6394 OP2(SLJIT_OR, context->sourcereg, 0, context->sourcereg, 0, SLJIT_IMM, othercasebit);
6395 add_jump(compiler, backtracks, CMP(SLJIT_NOT_EQUAL, context->sourcereg, 0, SLJIT_IMM, *cc | othercasebit));
6398 add_jump(compiler, backtracks, CMP(SLJIT_NOT_EQUAL, context->sourcereg, 0, SLJIT_IMM, *cc));
6403 #ifdef SUPPORT_UNICODE
6406 while (utflength > 0);
6412 #if defined SUPPORT_UNICODE || PCRE2_CODE_UNIT_WIDTH != 8
6414 #define SET_TYPE_OFFSET(value) \
6415 if ((value) != typeoffset) \
6417 if ((value) < typeoffset) \
6418 OP2(SLJIT_ADD, typereg, 0, typereg, 0, SLJIT_IMM, typeoffset - (value)); \
6420 OP2(SLJIT_SUB, typereg, 0, typereg, 0, SLJIT_IMM, (value) - typeoffset); \
6422 typeoffset = (value);
6424 #define SET_CHAR_OFFSET(value) \
6425 if ((value) != charoffset) \
6427 if ((value) < charoffset) \
6428 OP2(SLJIT_ADD, TMP1, 0, TMP1, 0, SLJIT_IMM, (sljit_sw)(charoffset - (value))); \
6430 OP2(SLJIT_SUB, TMP1, 0, TMP1, 0, SLJIT_IMM, (sljit_sw)((value) - charoffset)); \
6432 charoffset = (value);
6434 static PCRE2_SPTR compile_char1_matchingpath(compiler_common *common, PCRE2_UCHAR type, PCRE2_SPTR cc, jump_list **backtracks, BOOL check_str_ptr);
6436 static void compile_xclass_matchingpath(compiler_common *common, PCRE2_SPTR cc, jump_list **backtracks)
6439 jump_list *found = NULL;
6440 jump_list **list = (cc[0] & XCL_NOT) == 0 ? &found : backtracks;
6441 sljit_uw c, charoffset, max = 256, min = READ_CHAR_MAX;
6442 struct sljit_jump *jump = NULL;
6444 int compares, invertcmp, numberofcmps;
6445 #if defined SUPPORT_UNICODE && (PCRE2_CODE_UNIT_WIDTH == 8 || PCRE2_CODE_UNIT_WIDTH == 16)
6446 BOOL utf = common->utf;
6449 #ifdef SUPPORT_UNICODE
6450 BOOL needstype = FALSE, needsscript = FALSE, needschar = FALSE;
6451 BOOL charsaved = FALSE;
6453 const sljit_u32 *other_cases;
6454 sljit_uw typeoffset;
6457 /* Scanning the necessary info. */
6462 if (cc[-1] & XCL_MAP)
6465 cc += 32 / sizeof(PCRE2_UCHAR);
6468 while (*cc != XCL_END)
6471 if (*cc == XCL_SINGLE)
6474 GETCHARINCTEST(c, cc);
6475 if (c > max) max = c;
6476 if (c < min) min = c;
6477 #ifdef SUPPORT_UNICODE
6481 else if (*cc == XCL_RANGE)
6484 GETCHARINCTEST(c, cc);
6485 if (c < min) min = c;
6486 GETCHARINCTEST(c, cc);
6487 if (c > max) max = c;
6488 #ifdef SUPPORT_UNICODE
6492 #ifdef SUPPORT_UNICODE
6495 SLJIT_ASSERT(*cc == XCL_PROP || *cc == XCL_NOTPROP);
6497 if (*cc == PT_CLIST)
6499 other_cases = PRIV(ucd_caseless_sets) + cc[1];
6500 while (*other_cases != NOTACHAR)
6502 if (*other_cases > max) max = *other_cases;
6503 if (*other_cases < min) min = *other_cases;
6509 max = READ_CHAR_MAX;
6516 /* Any either accepts everything or ignored. */
6517 if (cc[-1] == XCL_PROP)
6519 compile_char1_matchingpath(common, OP_ALLANY, cc, backtracks, FALSE);
6520 if (list == backtracks)
6521 add_jump(compiler, backtracks, JUMP(SLJIT_JUMP));
6553 SLJIT_UNREACHABLE();
6560 SLJIT_ASSERT(compares > 0);
6562 /* We are not necessary in utf mode even in 8 bit mode. */
6564 read_char_range(common, min, max, (cc[-1] & XCL_NOT) != 0);
6566 if ((cc[-1] & XCL_HASPROP) == 0)
6568 if ((cc[-1] & XCL_MAP) != 0)
6570 jump = CMP(SLJIT_GREATER, TMP1, 0, SLJIT_IMM, 255);
6571 if (!optimize_class(common, (const sljit_u8 *)cc, (((const sljit_u8 *)cc)[31] & 0x80) != 0, TRUE, &found))
6573 OP2(SLJIT_AND, TMP2, 0, TMP1, 0, SLJIT_IMM, 0x7);
6574 OP2(SLJIT_LSHR, TMP1, 0, TMP1, 0, SLJIT_IMM, 3);
6575 OP1(SLJIT_MOV_U8, TMP1, 0, SLJIT_MEM1(TMP1), (sljit_sw)cc);
6576 OP2(SLJIT_SHL, TMP2, 0, SLJIT_IMM, 1, TMP2, 0);
6577 OP2(SLJIT_AND | SLJIT_SET_Z, SLJIT_UNUSED, 0, TMP1, 0, TMP2, 0);
6578 add_jump(compiler, &found, JUMP(SLJIT_NOT_ZERO));
6581 add_jump(compiler, backtracks, JUMP(SLJIT_JUMP));
6584 cc += 32 / sizeof(PCRE2_UCHAR);
6588 OP2(SLJIT_SUB, TMP2, 0, TMP1, 0, SLJIT_IMM, min);
6589 add_jump(compiler, (cc[-1] & XCL_NOT) == 0 ? backtracks : &found, CMP(SLJIT_GREATER, TMP2, 0, SLJIT_IMM, max - min));
6592 else if ((cc[-1] & XCL_MAP) != 0)
6594 OP1(SLJIT_MOV, RETURN_ADDR, 0, TMP1, 0);
6595 #ifdef SUPPORT_UNICODE
6598 if (!optimize_class(common, (const sljit_u8 *)cc, FALSE, TRUE, list))
6600 #if PCRE2_CODE_UNIT_WIDTH == 8
6604 jump = CMP(SLJIT_GREATER, TMP1, 0, SLJIT_IMM, 255);
6606 OP2(SLJIT_AND, TMP2, 0, TMP1, 0, SLJIT_IMM, 0x7);
6607 OP2(SLJIT_LSHR, TMP1, 0, TMP1, 0, SLJIT_IMM, 3);
6608 OP1(SLJIT_MOV_U8, TMP1, 0, SLJIT_MEM1(TMP1), (sljit_sw)cc);
6609 OP2(SLJIT_SHL, TMP2, 0, SLJIT_IMM, 1, TMP2, 0);
6610 OP2(SLJIT_AND | SLJIT_SET_Z, SLJIT_UNUSED, 0, TMP1, 0, TMP2, 0);
6611 add_jump(compiler, list, JUMP(SLJIT_NOT_ZERO));
6613 #if PCRE2_CODE_UNIT_WIDTH == 8
6619 OP1(SLJIT_MOV, TMP1, 0, RETURN_ADDR, 0);
6620 cc += 32 / sizeof(PCRE2_UCHAR);
6623 #ifdef SUPPORT_UNICODE
6624 if (needstype || needsscript)
6626 if (needschar && !charsaved)
6627 OP1(SLJIT_MOV, RETURN_ADDR, 0, TMP1, 0);
6629 #if PCRE2_CODE_UNIT_WIDTH == 32
6632 jump = CMP(SLJIT_LESS, TMP1, 0, SLJIT_IMM, MAX_UTF_CODE_POINT + 1);
6633 OP1(SLJIT_MOV, TMP1, 0, SLJIT_IMM, INVALID_UTF_CHAR);
6638 OP2(SLJIT_LSHR, TMP2, 0, TMP1, 0, SLJIT_IMM, UCD_BLOCK_SHIFT);
6639 OP2(SLJIT_SHL, TMP2, 0, TMP2, 0, SLJIT_IMM, 1);
6640 OP1(SLJIT_MOV_U16, TMP2, 0, SLJIT_MEM1(TMP2), (sljit_sw)PRIV(ucd_stage1));
6641 OP2(SLJIT_AND, TMP1, 0, TMP1, 0, SLJIT_IMM, UCD_BLOCK_MASK);
6642 OP2(SLJIT_SHL, TMP2, 0, TMP2, 0, SLJIT_IMM, UCD_BLOCK_SHIFT);
6643 OP2(SLJIT_ADD, TMP1, 0, TMP1, 0, TMP2, 0);
6644 OP1(SLJIT_MOV, TMP2, 0, SLJIT_IMM, (sljit_sw)PRIV(ucd_stage2));
6645 OP1(SLJIT_MOV_U16, TMP2, 0, SLJIT_MEM2(TMP2, TMP1), 1);
6647 /* Before anything else, we deal with scripts. */
6650 OP1(SLJIT_MOV, TMP1, 0, SLJIT_IMM, (sljit_sw)PRIV(ucd_records) + SLJIT_OFFSETOF(ucd_record, script));
6651 OP1(SLJIT_MOV_U8, TMP1, 0, SLJIT_MEM2(TMP1, TMP2), 3);
6655 while (*cc != XCL_END)
6657 if (*cc == XCL_SINGLE)
6660 GETCHARINCTEST(c, cc);
6662 else if (*cc == XCL_RANGE)
6665 GETCHARINCTEST(c, cc);
6666 GETCHARINCTEST(c, cc);
6670 SLJIT_ASSERT(*cc == XCL_PROP || *cc == XCL_NOTPROP);
6675 invertcmp = (compares == 0 && list != backtracks);
6676 if (cc[-1] == XCL_NOTPROP)
6678 jump = CMP(SLJIT_EQUAL ^ invertcmp, TMP1, 0, SLJIT_IMM, (int)cc[1]);
6679 add_jump(compiler, compares > 0 ? list : backtracks, jump);
6690 OP1(SLJIT_MOV, TMP1, 0, RETURN_ADDR, 0);
6697 OP1(SLJIT_MOV, TMP1, 0, SLJIT_IMM, (sljit_sw)PRIV(ucd_records) + SLJIT_OFFSETOF(ucd_record, chartype));
6698 OP1(SLJIT_MOV_U8, TMP1, 0, SLJIT_MEM2(TMP1, TMP2), 3);
6702 OP2(SLJIT_SHL, TMP2, 0, TMP2, 0, SLJIT_IMM, 3);
6703 OP1(SLJIT_MOV_U8, RETURN_ADDR, 0, SLJIT_MEM1(TMP2), (sljit_sw)PRIV(ucd_records) + SLJIT_OFFSETOF(ucd_record, chartype));
6704 typereg = RETURN_ADDR;
6710 /* Generating code. */
6713 #ifdef SUPPORT_UNICODE
6717 while (*cc != XCL_END)
6720 invertcmp = (compares == 0 && list != backtracks);
6723 if (*cc == XCL_SINGLE)
6726 GETCHARINCTEST(c, cc);
6728 if (numberofcmps < 3 && (*cc == XCL_SINGLE || *cc == XCL_RANGE))
6730 OP2(SLJIT_SUB | SLJIT_SET_Z, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, (sljit_sw)(c - charoffset));
6731 OP_FLAGS(numberofcmps == 0 ? SLJIT_MOV : SLJIT_OR, TMP2, 0, SLJIT_EQUAL);
6734 else if (numberofcmps > 0)
6736 OP2(SLJIT_SUB | SLJIT_SET_Z, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, (sljit_sw)(c - charoffset));
6737 OP_FLAGS(SLJIT_OR | SLJIT_SET_Z, TMP2, 0, SLJIT_EQUAL);
6738 jump = JUMP(SLJIT_NOT_ZERO ^ invertcmp);
6743 jump = CMP(SLJIT_EQUAL ^ invertcmp, TMP1, 0, SLJIT_IMM, (sljit_sw)(c - charoffset));
6747 else if (*cc == XCL_RANGE)
6750 GETCHARINCTEST(c, cc);
6752 GETCHARINCTEST(c, cc);
6754 if (numberofcmps < 3 && (*cc == XCL_SINGLE || *cc == XCL_RANGE))
6756 OP2(SLJIT_SUB | SLJIT_SET_LESS_EQUAL, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, (sljit_sw)(c - charoffset));
6757 OP_FLAGS(numberofcmps == 0 ? SLJIT_MOV : SLJIT_OR, TMP2, 0, SLJIT_LESS_EQUAL);
6760 else if (numberofcmps > 0)
6762 OP2(SLJIT_SUB | SLJIT_SET_LESS_EQUAL, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, (sljit_sw)(c - charoffset));
6763 OP_FLAGS(SLJIT_OR | SLJIT_SET_Z, TMP2, 0, SLJIT_LESS_EQUAL);
6764 jump = JUMP(SLJIT_NOT_ZERO ^ invertcmp);
6769 jump = CMP(SLJIT_LESS_EQUAL ^ invertcmp, TMP1, 0, SLJIT_IMM, (sljit_sw)(c - charoffset));
6773 #ifdef SUPPORT_UNICODE
6776 SLJIT_ASSERT(*cc == XCL_PROP || *cc == XCL_NOTPROP);
6777 if (*cc == XCL_NOTPROP)
6784 jump = JUMP(SLJIT_JUMP);
6788 OP2(SLJIT_SUB | SLJIT_SET_Z, SLJIT_UNUSED, 0, typereg, 0, SLJIT_IMM, ucp_Lu - typeoffset);
6789 OP_FLAGS(SLJIT_MOV, TMP2, 0, SLJIT_EQUAL);
6790 OP2(SLJIT_SUB | SLJIT_SET_Z, SLJIT_UNUSED, 0, typereg, 0, SLJIT_IMM, ucp_Ll - typeoffset);
6791 OP_FLAGS(SLJIT_OR, TMP2, 0, SLJIT_EQUAL);
6792 OP2(SLJIT_SUB | SLJIT_SET_Z, SLJIT_UNUSED, 0, typereg, 0, SLJIT_IMM, ucp_Lt - typeoffset);
6793 OP_FLAGS(SLJIT_OR | SLJIT_SET_Z, TMP2, 0, SLJIT_EQUAL);
6794 jump = JUMP(SLJIT_NOT_ZERO ^ invertcmp);
6798 c = PRIV(ucp_typerange)[(int)cc[1] * 2];
6800 jump = CMP(SLJIT_LESS_EQUAL ^ invertcmp, typereg, 0, SLJIT_IMM, PRIV(ucp_typerange)[(int)cc[1] * 2 + 1] - c);
6804 jump = CMP(SLJIT_EQUAL ^ invertcmp, typereg, 0, SLJIT_IMM, (int)cc[1] - typeoffset);
6815 OP2(SLJIT_SUB | SLJIT_SET_LESS_EQUAL, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0xd - 0x9);
6816 OP_FLAGS(SLJIT_MOV, TMP2, 0, SLJIT_LESS_EQUAL);
6818 OP2(SLJIT_SUB | SLJIT_SET_Z, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x85 - 0x9);
6819 OP_FLAGS(SLJIT_OR, TMP2, 0, SLJIT_EQUAL);
6821 OP2(SLJIT_SUB | SLJIT_SET_Z, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x180e - 0x9);
6822 OP_FLAGS(SLJIT_OR, TMP2, 0, SLJIT_EQUAL);
6824 SET_TYPE_OFFSET(ucp_Zl);
6825 OP2(SLJIT_SUB | SLJIT_SET_LESS_EQUAL, SLJIT_UNUSED, 0, typereg, 0, SLJIT_IMM, ucp_Zs - ucp_Zl);
6826 OP_FLAGS(SLJIT_OR | SLJIT_SET_Z, TMP2, 0, SLJIT_LESS_EQUAL);
6827 jump = JUMP(SLJIT_NOT_ZERO ^ invertcmp);
6831 OP2(SLJIT_SUB | SLJIT_SET_Z, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, (sljit_sw)(CHAR_UNDERSCORE - charoffset));
6832 OP_FLAGS(SLJIT_MOV, TMP2, 0, SLJIT_EQUAL);
6836 SET_TYPE_OFFSET(ucp_Ll);
6837 OP2(SLJIT_SUB | SLJIT_SET_LESS_EQUAL, SLJIT_UNUSED, 0, typereg, 0, SLJIT_IMM, ucp_Lu - ucp_Ll);
6838 OP_FLAGS((*cc == PT_ALNUM) ? SLJIT_MOV : SLJIT_OR, TMP2, 0, SLJIT_LESS_EQUAL);
6839 SET_TYPE_OFFSET(ucp_Nd);
6840 OP2(SLJIT_SUB | SLJIT_SET_LESS_EQUAL, SLJIT_UNUSED, 0, typereg, 0, SLJIT_IMM, ucp_No - ucp_Nd);
6841 OP_FLAGS(SLJIT_OR | SLJIT_SET_Z, TMP2, 0, SLJIT_LESS_EQUAL);
6842 jump = JUMP(SLJIT_NOT_ZERO ^ invertcmp);
6846 other_cases = PRIV(ucd_caseless_sets) + cc[1];
6848 /* At least three characters are required.
6849 Otherwise this case would be handled by the normal code path. */
6850 SLJIT_ASSERT(other_cases[0] != NOTACHAR && other_cases[1] != NOTACHAR && other_cases[2] != NOTACHAR);
6851 SLJIT_ASSERT(other_cases[0] < other_cases[1] && other_cases[1] < other_cases[2]);
6853 /* Optimizing character pairs, if their difference is power of 2. */
6854 if (is_powerof2(other_cases[1] ^ other_cases[0]))
6856 if (charoffset == 0)
6857 OP2(SLJIT_OR, TMP2, 0, TMP1, 0, SLJIT_IMM, other_cases[1] ^ other_cases[0]);
6860 OP2(SLJIT_ADD, TMP2, 0, TMP1, 0, SLJIT_IMM, (sljit_sw)charoffset);
6861 OP2(SLJIT_OR, TMP2, 0, TMP2, 0, SLJIT_IMM, other_cases[1] ^ other_cases[0]);
6863 OP2(SLJIT_SUB | SLJIT_SET_Z, SLJIT_UNUSED, 0, TMP2, 0, SLJIT_IMM, other_cases[1]);
6864 OP_FLAGS(SLJIT_MOV, TMP2, 0, SLJIT_EQUAL);
6867 else if (is_powerof2(other_cases[2] ^ other_cases[1]))
6869 if (charoffset == 0)
6870 OP2(SLJIT_OR, TMP2, 0, TMP1, 0, SLJIT_IMM, other_cases[2] ^ other_cases[1]);
6873 OP2(SLJIT_ADD, TMP2, 0, TMP1, 0, SLJIT_IMM, (sljit_sw)charoffset);
6874 OP2(SLJIT_OR, TMP2, 0, TMP2, 0, SLJIT_IMM, other_cases[1] ^ other_cases[0]);
6876 OP2(SLJIT_SUB | SLJIT_SET_Z, SLJIT_UNUSED, 0, TMP2, 0, SLJIT_IMM, other_cases[2]);
6877 OP_FLAGS(SLJIT_MOV, TMP2, 0, SLJIT_EQUAL);
6879 OP2(SLJIT_SUB | SLJIT_SET_Z, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, (sljit_sw)(other_cases[0] - charoffset));
6880 OP_FLAGS(SLJIT_OR | ((other_cases[3] == NOTACHAR) ? SLJIT_SET_Z : 0), TMP2, 0, SLJIT_EQUAL);
6886 OP2(SLJIT_SUB | SLJIT_SET_Z, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, (sljit_sw)(*other_cases++ - charoffset));
6887 OP_FLAGS(SLJIT_MOV, TMP2, 0, SLJIT_EQUAL);
6890 while (*other_cases != NOTACHAR)
6892 OP2(SLJIT_SUB | SLJIT_SET_Z, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, (sljit_sw)(*other_cases++ - charoffset));
6893 OP_FLAGS(SLJIT_OR | ((*other_cases == NOTACHAR) ? SLJIT_SET_Z : 0), TMP2, 0, SLJIT_EQUAL);
6895 jump = JUMP(SLJIT_NOT_ZERO ^ invertcmp);
6899 OP2(SLJIT_SUB | SLJIT_SET_Z, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, (sljit_sw)(CHAR_DOLLAR_SIGN - charoffset));
6900 OP_FLAGS(SLJIT_MOV, TMP2, 0, SLJIT_EQUAL);
6901 OP2(SLJIT_SUB | SLJIT_SET_Z, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, (sljit_sw)(CHAR_COMMERCIAL_AT - charoffset));
6902 OP_FLAGS(SLJIT_OR, TMP2, 0, SLJIT_EQUAL);
6903 OP2(SLJIT_SUB | SLJIT_SET_Z, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, (sljit_sw)(CHAR_GRAVE_ACCENT - charoffset));
6904 OP_FLAGS(SLJIT_OR, TMP2, 0, SLJIT_EQUAL);
6906 SET_CHAR_OFFSET(0xa0);
6907 OP2(SLJIT_SUB | SLJIT_SET_LESS_EQUAL, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, (sljit_sw)(0xd7ff - charoffset));
6908 OP_FLAGS(SLJIT_OR, TMP2, 0, SLJIT_LESS_EQUAL);
6910 OP2(SLJIT_SUB | SLJIT_SET_GREATER_EQUAL, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0xe000 - 0);
6911 OP_FLAGS(SLJIT_OR | SLJIT_SET_Z, TMP2, 0, SLJIT_GREATER_EQUAL);
6912 jump = JUMP(SLJIT_NOT_ZERO ^ invertcmp);
6916 /* C and Z groups are the farthest two groups. */
6917 SET_TYPE_OFFSET(ucp_Ll);
6918 OP2(SLJIT_SUB | SLJIT_SET_GREATER, SLJIT_UNUSED, 0, typereg, 0, SLJIT_IMM, ucp_So - ucp_Ll);
6919 OP_FLAGS(SLJIT_MOV, TMP2, 0, SLJIT_GREATER);
6921 jump = CMP(SLJIT_NOT_EQUAL, typereg, 0, SLJIT_IMM, ucp_Cf - ucp_Ll);
6923 /* In case of ucp_Cf, we overwrite the result. */
6924 SET_CHAR_OFFSET(0x2066);
6925 OP2(SLJIT_SUB | SLJIT_SET_LESS_EQUAL, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x2069 - 0x2066);
6926 OP_FLAGS(SLJIT_MOV, TMP2, 0, SLJIT_LESS_EQUAL);
6928 OP2(SLJIT_SUB | SLJIT_SET_Z, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x061c - 0x2066);
6929 OP_FLAGS(SLJIT_OR, TMP2, 0, SLJIT_EQUAL);
6931 OP2(SLJIT_SUB | SLJIT_SET_Z, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x180e - 0x2066);
6932 OP_FLAGS(SLJIT_OR, TMP2, 0, SLJIT_EQUAL);
6935 jump = CMP(SLJIT_ZERO ^ invertcmp, TMP2, 0, SLJIT_IMM, 0);
6939 /* C and Z groups are the farthest two groups. */
6940 SET_TYPE_OFFSET(ucp_Ll);
6941 OP2(SLJIT_SUB | SLJIT_SET_GREATER, SLJIT_UNUSED, 0, typereg, 0, SLJIT_IMM, ucp_So - ucp_Ll);
6942 OP_FLAGS(SLJIT_MOV, TMP2, 0, SLJIT_GREATER);
6944 OP2(SLJIT_SUB | SLJIT_SET_Z, SLJIT_UNUSED, 0, typereg, 0, SLJIT_IMM, ucp_Zs - ucp_Ll);
6945 OP_FLAGS(SLJIT_AND, TMP2, 0, SLJIT_NOT_EQUAL);
6947 jump = CMP(SLJIT_NOT_EQUAL, typereg, 0, SLJIT_IMM, ucp_Cf - ucp_Ll);
6949 /* In case of ucp_Cf, we overwrite the result. */
6950 SET_CHAR_OFFSET(0x2066);
6951 OP2(SLJIT_SUB | SLJIT_SET_LESS_EQUAL, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x2069 - 0x2066);
6952 OP_FLAGS(SLJIT_MOV, TMP2, 0, SLJIT_LESS_EQUAL);
6954 OP2(SLJIT_SUB | SLJIT_SET_Z, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x061c - 0x2066);
6955 OP_FLAGS(SLJIT_OR, TMP2, 0, SLJIT_EQUAL);
6958 jump = CMP(SLJIT_ZERO ^ invertcmp, TMP2, 0, SLJIT_IMM, 0);
6962 SET_TYPE_OFFSET(ucp_Sc);
6963 OP2(SLJIT_SUB | SLJIT_SET_LESS_EQUAL, SLJIT_UNUSED, 0, typereg, 0, SLJIT_IMM, ucp_So - ucp_Sc);
6964 OP_FLAGS(SLJIT_MOV, TMP2, 0, SLJIT_LESS_EQUAL);
6967 OP2(SLJIT_SUB | SLJIT_SET_LESS_EQUAL, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x7f);
6968 OP_FLAGS(SLJIT_AND, TMP2, 0, SLJIT_LESS_EQUAL);
6970 SET_TYPE_OFFSET(ucp_Pc);
6971 OP2(SLJIT_SUB | SLJIT_SET_LESS_EQUAL, SLJIT_UNUSED, 0, typereg, 0, SLJIT_IMM, ucp_Ps - ucp_Pc);
6972 OP_FLAGS(SLJIT_OR | SLJIT_SET_Z, TMP2, 0, SLJIT_LESS_EQUAL);
6973 jump = JUMP(SLJIT_NOT_ZERO ^ invertcmp);
6977 SLJIT_UNREACHABLE();
6985 add_jump(compiler, compares > 0 ? list : backtracks, jump);
6989 set_jumps(found, LABEL());
6992 #undef SET_TYPE_OFFSET
6993 #undef SET_CHAR_OFFSET
6997 static PCRE2_SPTR compile_simple_assertion_matchingpath(compiler_common *common, PCRE2_UCHAR type, PCRE2_SPTR cc, jump_list **backtracks)
7001 struct sljit_jump *jump[4];
7002 #ifdef SUPPORT_UNICODE
7003 struct sljit_label *label;
7004 #endif /* SUPPORT_UNICODE */
7009 OP1(SLJIT_MOV, TMP1, 0, ARGUMENTS, 0);
7010 OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(TMP1), SLJIT_OFFSETOF(jit_arguments, begin));
7011 add_jump(compiler, backtracks, CMP(SLJIT_NOT_EQUAL, STR_PTR, 0, TMP1, 0));
7015 OP1(SLJIT_MOV, TMP1, 0, ARGUMENTS, 0);
7016 OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(TMP1), SLJIT_OFFSETOF(jit_arguments, str));
7017 add_jump(compiler, backtracks, CMP(SLJIT_NOT_EQUAL, STR_PTR, 0, TMP1, 0));
7020 case OP_NOT_WORD_BOUNDARY:
7021 case OP_WORD_BOUNDARY:
7022 add_jump(compiler, &common->wordboundary, JUMP(SLJIT_FAST_CALL));
7023 sljit_set_current_flags(compiler, SLJIT_SET_Z);
7024 add_jump(compiler, backtracks, JUMP(type == OP_NOT_WORD_BOUNDARY ? SLJIT_NOT_ZERO : SLJIT_ZERO));
7028 /* Requires rather complex checks. */
7029 jump[0] = CMP(SLJIT_GREATER_EQUAL, STR_PTR, 0, STR_END, 0);
7030 if (common->nltype == NLTYPE_FIXED && common->newline > 255)
7032 OP2(SLJIT_ADD, TMP2, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(2));
7033 OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(0));
7034 if (common->mode == PCRE2_JIT_COMPLETE)
7035 add_jump(compiler, backtracks, CMP(SLJIT_NOT_EQUAL, TMP2, 0, STR_END, 0));
7038 jump[1] = CMP(SLJIT_EQUAL, TMP2, 0, STR_END, 0);
7039 OP2(SLJIT_SUB | SLJIT_SET_LESS, SLJIT_UNUSED, 0, TMP2, 0, STR_END, 0);
7040 OP_FLAGS(SLJIT_MOV, TMP2, 0, SLJIT_LESS);
7041 OP2(SLJIT_SUB | SLJIT_SET_Z, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, (common->newline >> 8) & 0xff);
7042 OP_FLAGS(SLJIT_OR | SLJIT_SET_Z, TMP2, 0, SLJIT_NOT_EQUAL);
7043 add_jump(compiler, backtracks, JUMP(SLJIT_NOT_EQUAL));
7044 check_partial(common, TRUE);
7045 add_jump(compiler, backtracks, JUMP(SLJIT_JUMP));
7048 OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(1));
7049 add_jump(compiler, backtracks, CMP(SLJIT_NOT_EQUAL, TMP1, 0, SLJIT_IMM, (common->newline >> 8) & 0xff));
7050 add_jump(compiler, backtracks, CMP(SLJIT_NOT_EQUAL, TMP2, 0, SLJIT_IMM, common->newline & 0xff));
7052 else if (common->nltype == NLTYPE_FIXED)
7054 OP2(SLJIT_ADD, TMP2, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
7055 OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(0));
7056 add_jump(compiler, backtracks, CMP(SLJIT_NOT_EQUAL, TMP2, 0, STR_END, 0));
7057 add_jump(compiler, backtracks, CMP(SLJIT_NOT_EQUAL, TMP1, 0, SLJIT_IMM, common->newline));
7061 OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(0));
7062 jump[1] = CMP(SLJIT_NOT_EQUAL, TMP1, 0, SLJIT_IMM, CHAR_CR);
7063 OP2(SLJIT_ADD, TMP2, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(2));
7064 OP2(SLJIT_SUB | SLJIT_SET_Z | SLJIT_SET_GREATER, SLJIT_UNUSED, 0, TMP2, 0, STR_END, 0);
7065 jump[2] = JUMP(SLJIT_GREATER);
7066 add_jump(compiler, backtracks, JUMP(SLJIT_NOT_EQUAL) /* LESS */);
7068 OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(1));
7069 jump[3] = CMP(SLJIT_EQUAL, TMP1, 0, SLJIT_IMM, CHAR_NL);
7070 add_jump(compiler, backtracks, JUMP(SLJIT_JUMP));
7073 if (common->nltype == NLTYPE_ANYCRLF)
7075 OP2(SLJIT_ADD, TMP2, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
7076 add_jump(compiler, backtracks, CMP(SLJIT_LESS, TMP2, 0, STR_END, 0));
7077 add_jump(compiler, backtracks, CMP(SLJIT_NOT_EQUAL, TMP1, 0, SLJIT_IMM, CHAR_NL));
7081 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), LOCALS1, STR_PTR, 0);
7082 read_char_range(common, common->nlmin, common->nlmax, TRUE);
7083 add_jump(compiler, backtracks, CMP(SLJIT_NOT_EQUAL, STR_PTR, 0, STR_END, 0));
7084 add_jump(compiler, &common->anynewline, JUMP(SLJIT_FAST_CALL));
7085 sljit_set_current_flags(compiler, SLJIT_SET_Z);
7086 add_jump(compiler, backtracks, JUMP(SLJIT_ZERO));
7087 OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(SLJIT_SP), LOCALS1);
7093 check_partial(common, FALSE);
7097 add_jump(compiler, backtracks, CMP(SLJIT_LESS, STR_PTR, 0, STR_END, 0));
7098 check_partial(common, FALSE);
7102 OP1(SLJIT_MOV, TMP2, 0, ARGUMENTS, 0);
7103 OP2(SLJIT_AND32 | SLJIT_SET_Z, SLJIT_UNUSED, 0, SLJIT_MEM1(TMP2), SLJIT_OFFSETOF(jit_arguments, options), SLJIT_IMM, PCRE2_NOTEOL);
7104 add_jump(compiler, backtracks, JUMP(SLJIT_NOT_ZERO32));
7106 if (!common->endonly)
7107 compile_simple_assertion_matchingpath(common, OP_EODN, cc, backtracks);
7110 add_jump(compiler, backtracks, CMP(SLJIT_LESS, STR_PTR, 0, STR_END, 0));
7111 check_partial(common, FALSE);
7116 jump[1] = CMP(SLJIT_LESS, STR_PTR, 0, STR_END, 0);
7117 OP1(SLJIT_MOV, TMP2, 0, ARGUMENTS, 0);
7118 OP2(SLJIT_AND32 | SLJIT_SET_Z, SLJIT_UNUSED, 0, SLJIT_MEM1(TMP2), SLJIT_OFFSETOF(jit_arguments, options), SLJIT_IMM, PCRE2_NOTEOL);
7119 add_jump(compiler, backtracks, JUMP(SLJIT_NOT_ZERO32));
7120 check_partial(common, FALSE);
7121 jump[0] = JUMP(SLJIT_JUMP);
7124 if (common->nltype == NLTYPE_FIXED && common->newline > 255)
7126 OP2(SLJIT_ADD, TMP2, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(2));
7127 OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(0));
7128 if (common->mode == PCRE2_JIT_COMPLETE)
7129 add_jump(compiler, backtracks, CMP(SLJIT_GREATER, TMP2, 0, STR_END, 0));
7132 jump[1] = CMP(SLJIT_LESS_EQUAL, TMP2, 0, STR_END, 0);
7133 /* STR_PTR = STR_END - IN_UCHARS(1) */
7134 add_jump(compiler, backtracks, CMP(SLJIT_NOT_EQUAL, TMP1, 0, SLJIT_IMM, (common->newline >> 8) & 0xff));
7135 check_partial(common, TRUE);
7136 add_jump(compiler, backtracks, JUMP(SLJIT_JUMP));
7140 OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(1));
7141 add_jump(compiler, backtracks, CMP(SLJIT_NOT_EQUAL, TMP1, 0, SLJIT_IMM, (common->newline >> 8) & 0xff));
7142 add_jump(compiler, backtracks, CMP(SLJIT_NOT_EQUAL, TMP2, 0, SLJIT_IMM, common->newline & 0xff));
7146 peek_char(common, common->nlmax);
7147 check_newlinechar(common, common->nltype, backtracks, FALSE);
7153 OP1(SLJIT_MOV, TMP2, 0, ARGUMENTS, 0);
7154 OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(TMP2), SLJIT_OFFSETOF(jit_arguments, begin));
7155 add_jump(compiler, backtracks, CMP(SLJIT_GREATER, STR_PTR, 0, TMP1, 0));
7156 OP2(SLJIT_AND32 | SLJIT_SET_Z, SLJIT_UNUSED, 0, SLJIT_MEM1(TMP2), SLJIT_OFFSETOF(jit_arguments, options), SLJIT_IMM, PCRE2_NOTBOL);
7157 add_jump(compiler, backtracks, JUMP(SLJIT_NOT_ZERO32));
7161 OP1(SLJIT_MOV, TMP2, 0, ARGUMENTS, 0);
7162 OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(TMP2), SLJIT_OFFSETOF(jit_arguments, begin));
7163 jump[1] = CMP(SLJIT_GREATER, STR_PTR, 0, TMP1, 0);
7164 OP2(SLJIT_AND32 | SLJIT_SET_Z, SLJIT_UNUSED, 0, SLJIT_MEM1(TMP2), SLJIT_OFFSETOF(jit_arguments, options), SLJIT_IMM, PCRE2_NOTBOL);
7165 add_jump(compiler, backtracks, JUMP(SLJIT_NOT_ZERO32));
7166 jump[0] = JUMP(SLJIT_JUMP);
7169 if (!common->alt_circumflex)
7170 add_jump(compiler, backtracks, CMP(SLJIT_GREATER_EQUAL, STR_PTR, 0, STR_END, 0));
7172 if (common->nltype == NLTYPE_FIXED && common->newline > 255)
7174 OP2(SLJIT_SUB, TMP2, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(2));
7175 add_jump(compiler, backtracks, CMP(SLJIT_LESS, TMP2, 0, TMP1, 0));
7176 OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(-2));
7177 OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(-1));
7178 add_jump(compiler, backtracks, CMP(SLJIT_NOT_EQUAL, TMP1, 0, SLJIT_IMM, (common->newline >> 8) & 0xff));
7179 add_jump(compiler, backtracks, CMP(SLJIT_NOT_EQUAL, TMP2, 0, SLJIT_IMM, common->newline & 0xff));
7183 skip_char_back(common);
7184 read_char_range(common, common->nlmin, common->nlmax, TRUE);
7185 check_newlinechar(common, common->nltype, backtracks, FALSE);
7191 length = GET(cc, 0);
7193 return cc + LINK_SIZE;
7194 OP1(SLJIT_MOV, TMP1, 0, ARGUMENTS, 0);
7195 #ifdef SUPPORT_UNICODE
7198 OP1(SLJIT_MOV, TMP3, 0, SLJIT_MEM1(TMP1), SLJIT_OFFSETOF(jit_arguments, begin));
7199 OP1(SLJIT_MOV, TMP2, 0, SLJIT_IMM, length);
7201 add_jump(compiler, backtracks, CMP(SLJIT_LESS_EQUAL, STR_PTR, 0, TMP3, 0));
7202 skip_char_back(common);
7203 OP2(SLJIT_SUB | SLJIT_SET_Z, TMP2, 0, TMP2, 0, SLJIT_IMM, 1);
7204 JUMPTO(SLJIT_NOT_ZERO, label);
7209 OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(TMP1), SLJIT_OFFSETOF(jit_arguments, begin));
7210 OP2(SLJIT_SUB, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(length));
7211 add_jump(compiler, backtracks, CMP(SLJIT_LESS, STR_PTR, 0, TMP1, 0));
7213 check_start_used_ptr(common);
7214 return cc + LINK_SIZE;
7216 SLJIT_UNREACHABLE();
7220 #ifdef SUPPORT_UNICODE
7222 #if PCRE2_CODE_UNIT_WIDTH != 32
7224 static PCRE2_SPTR SLJIT_FUNC do_extuni_utf(jit_arguments *args, PCRE2_SPTR cc)
7226 PCRE2_SPTR start_subject = args->begin;
7227 PCRE2_SPTR end_subject = args->end;
7228 int lgb, rgb, len, ricount;
7229 PCRE2_SPTR prevcc, bptr;
7234 lgb = UCD_GRAPHBREAK(c);
7236 while (cc < end_subject)
7239 GETCHARLEN(c, cc, len);
7240 rgb = UCD_GRAPHBREAK(c);
7242 if ((PRIV(ucp_gbtable)[lgb] & (1 << rgb)) == 0) break;
7244 /* Not breaking between Regional Indicators is allowed only if there
7245 are an even number of preceding RIs. */
7247 if (lgb == ucp_gbRegionalIndicator && rgb == ucp_gbRegionalIndicator)
7252 /* bptr is pointing to the left-hand character */
7253 while (bptr > start_subject)
7259 if (UCD_GRAPHBREAK(c) != ucp_gbRegionalIndicator) break;
7264 if ((ricount & 1) != 0) break; /* Grapheme break required */
7267 /* If Extend or ZWJ follows Extended_Pictographic, do not update lgb; this
7268 allows any number of them before a following Extended_Pictographic. */
7270 if ((rgb != ucp_gbExtend && rgb != ucp_gbZWJ) ||
7271 lgb != ucp_gbExtended_Pictographic)
7283 static PCRE2_SPTR SLJIT_FUNC do_extuni_no_utf(jit_arguments *args, PCRE2_SPTR cc)
7285 PCRE2_SPTR start_subject = args->begin;
7286 PCRE2_SPTR end_subject = args->end;
7287 int lgb, rgb, ricount;
7292 lgb = UCD_GRAPHBREAK(c);
7294 while (cc < end_subject)
7297 rgb = UCD_GRAPHBREAK(c);
7299 if ((PRIV(ucp_gbtable)[lgb] & (1 << rgb)) == 0) break;
7301 /* Not breaking between Regional Indicators is allowed only if there
7302 are an even number of preceding RIs. */
7304 if (lgb == ucp_gbRegionalIndicator && rgb == ucp_gbRegionalIndicator)
7309 /* bptr is pointing to the left-hand character */
7310 while (bptr > start_subject)
7315 if (UCD_GRAPHBREAK(c) != ucp_gbRegionalIndicator) break;
7320 if ((ricount & 1) != 0) break; /* Grapheme break required */
7323 /* If Extend or ZWJ follows Extended_Pictographic, do not update lgb; this
7324 allows any number of them before a following Extended_Pictographic. */
7326 if ((rgb != ucp_gbExtend && rgb != ucp_gbZWJ) ||
7327 lgb != ucp_gbExtended_Pictographic)
7338 static PCRE2_SPTR compile_char1_matchingpath(compiler_common *common, PCRE2_UCHAR type, PCRE2_SPTR cc, jump_list **backtracks, BOOL check_str_ptr)
7342 unsigned int c, oc, bit;
7343 compare_context context;
7344 struct sljit_jump *jump[3];
7345 jump_list *end_list;
7346 #ifdef SUPPORT_UNICODE
7347 PCRE2_UCHAR propdata[5];
7348 #endif /* SUPPORT_UNICODE */
7354 /* Digits are usually 0-9, so it is worth to optimize them. */
7356 detect_partial_match(common, backtracks);
7357 #if defined SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH == 8
7358 if (common->utf && is_char7_bitset((const sljit_u8*)common->ctypes - cbit_length + cbit_digit, FALSE))
7359 read_char7_type(common, type == OP_NOT_DIGIT);
7362 read_char8_type(common, type == OP_NOT_DIGIT);
7363 /* Flip the starting bit in the negative case. */
7364 OP2(SLJIT_AND | SLJIT_SET_Z, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, ctype_digit);
7365 add_jump(compiler, backtracks, JUMP(type == OP_DIGIT ? SLJIT_ZERO : SLJIT_NOT_ZERO));
7368 case OP_NOT_WHITESPACE:
7371 detect_partial_match(common, backtracks);
7372 #if defined SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH == 8
7373 if (common->utf && is_char7_bitset((const sljit_u8*)common->ctypes - cbit_length + cbit_space, FALSE))
7374 read_char7_type(common, type == OP_NOT_WHITESPACE);
7377 read_char8_type(common, type == OP_NOT_WHITESPACE);
7378 OP2(SLJIT_AND | SLJIT_SET_Z, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, ctype_space);
7379 add_jump(compiler, backtracks, JUMP(type == OP_WHITESPACE ? SLJIT_ZERO : SLJIT_NOT_ZERO));
7382 case OP_NOT_WORDCHAR:
7385 detect_partial_match(common, backtracks);
7386 #if defined SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH == 8
7387 if (common->utf && is_char7_bitset((const sljit_u8*)common->ctypes - cbit_length + cbit_word, FALSE))
7388 read_char7_type(common, type == OP_NOT_WORDCHAR);
7391 read_char8_type(common, type == OP_NOT_WORDCHAR);
7392 OP2(SLJIT_AND | SLJIT_SET_Z, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, ctype_word);
7393 add_jump(compiler, backtracks, JUMP(type == OP_WORDCHAR ? SLJIT_ZERO : SLJIT_NOT_ZERO));
7398 detect_partial_match(common, backtracks);
7399 read_char_range(common, common->nlmin, common->nlmax, TRUE);
7400 if (common->nltype == NLTYPE_FIXED && common->newline > 255)
7402 jump[0] = CMP(SLJIT_NOT_EQUAL, TMP1, 0, SLJIT_IMM, (common->newline >> 8) & 0xff);
7404 if (common->mode != PCRE2_JIT_PARTIAL_HARD)
7405 add_jump(compiler, &end_list, CMP(SLJIT_GREATER_EQUAL, STR_PTR, 0, STR_END, 0));
7407 check_str_end(common, &end_list);
7409 OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), 0);
7410 add_jump(compiler, backtracks, CMP(SLJIT_EQUAL, TMP1, 0, SLJIT_IMM, common->newline & 0xff));
7411 set_jumps(end_list, LABEL());
7415 check_newlinechar(common, common->nltype, backtracks, TRUE);
7420 detect_partial_match(common, backtracks);
7421 #if defined SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH != 32
7424 OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), 0);
7425 OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
7426 #if PCRE2_CODE_UNIT_WIDTH == 8 || PCRE2_CODE_UNIT_WIDTH == 16
7427 #if PCRE2_CODE_UNIT_WIDTH == 8
7428 jump[0] = CMP(SLJIT_LESS, TMP1, 0, SLJIT_IMM, 0xc0);
7429 OP1(SLJIT_MOV_U8, TMP1, 0, SLJIT_MEM1(TMP1), (sljit_sw)PRIV(utf8_table4) - 0xc0);
7430 OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, TMP1, 0);
7431 #elif PCRE2_CODE_UNIT_WIDTH == 16
7432 jump[0] = CMP(SLJIT_LESS, TMP1, 0, SLJIT_IMM, 0xd800);
7433 OP2(SLJIT_AND, TMP1, 0, TMP1, 0, SLJIT_IMM, 0xfc00);
7434 OP2(SLJIT_SUB | SLJIT_SET_Z, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0xd800);
7435 OP_FLAGS(SLJIT_MOV, TMP1, 0, SLJIT_EQUAL);
7436 OP2(SLJIT_SHL, TMP1, 0, TMP1, 0, SLJIT_IMM, 1);
7437 OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, TMP1, 0);
7440 #endif /* PCRE2_CODE_UNIT_WIDTH == [8|16] */
7444 OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
7449 detect_partial_match(common, backtracks);
7450 OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
7453 #ifdef SUPPORT_UNICODE
7456 propdata[0] = XCL_HASPROP;
7457 propdata[1] = type == OP_NOTPROP ? XCL_NOTPROP : XCL_PROP;
7458 propdata[2] = cc[0];
7459 propdata[3] = cc[1];
7460 propdata[4] = XCL_END;
7462 detect_partial_match(common, backtracks);
7463 compile_xclass_matchingpath(common, propdata, backtracks);
7469 detect_partial_match(common, backtracks);
7470 read_char_range(common, common->bsr_nlmin, common->bsr_nlmax, FALSE);
7471 jump[0] = CMP(SLJIT_NOT_EQUAL, TMP1, 0, SLJIT_IMM, CHAR_CR);
7472 /* We don't need to handle soft partial matching case. */
7474 if (common->mode != PCRE2_JIT_PARTIAL_HARD)
7475 add_jump(compiler, &end_list, CMP(SLJIT_GREATER_EQUAL, STR_PTR, 0, STR_END, 0));
7477 check_str_end(common, &end_list);
7478 OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), 0);
7479 jump[1] = CMP(SLJIT_NOT_EQUAL, TMP1, 0, SLJIT_IMM, CHAR_NL);
7480 OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
7481 jump[2] = JUMP(SLJIT_JUMP);
7483 check_newlinechar(common, common->bsr_nltype, backtracks, FALSE);
7484 set_jumps(end_list, LABEL());
7492 detect_partial_match(common, backtracks);
7493 read_char_range(common, 0x9, 0x3000, type == OP_NOT_HSPACE);
7494 add_jump(compiler, &common->hspace, JUMP(SLJIT_FAST_CALL));
7495 sljit_set_current_flags(compiler, SLJIT_SET_Z);
7496 add_jump(compiler, backtracks, JUMP(type == OP_NOT_HSPACE ? SLJIT_NOT_ZERO : SLJIT_ZERO));
7502 detect_partial_match(common, backtracks);
7503 read_char_range(common, 0xa, 0x2029, type == OP_NOT_VSPACE);
7504 add_jump(compiler, &common->vspace, JUMP(SLJIT_FAST_CALL));
7505 sljit_set_current_flags(compiler, SLJIT_SET_Z);
7506 add_jump(compiler, backtracks, JUMP(type == OP_NOT_VSPACE ? SLJIT_NOT_ZERO : SLJIT_ZERO));
7509 #ifdef SUPPORT_UNICODE
7512 detect_partial_match(common, backtracks);
7514 SLJIT_ASSERT(TMP1 == SLJIT_R0 && STR_PTR == SLJIT_R1);
7515 OP1(SLJIT_MOV, SLJIT_R0, 0, ARGUMENTS, 0);
7517 #if PCRE2_CODE_UNIT_WIDTH != 32
7518 sljit_emit_icall(compiler, SLJIT_CALL, SLJIT_RET(SW) | SLJIT_ARG1(SW) | SLJIT_ARG2(SW), SLJIT_IMM,
7519 common->utf ? SLJIT_FUNC_OFFSET(do_extuni_utf) : SLJIT_FUNC_OFFSET(do_extuni_no_utf));
7521 sljit_emit_icall(compiler, SLJIT_CALL, SLJIT_RET(SW) | SLJIT_ARG1(SW) | SLJIT_ARG2(SW), SLJIT_IMM, SLJIT_FUNC_OFFSET(do_extuni_no_utf));
7524 OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_RETURN_REG, 0);
7526 if (common->mode == PCRE2_JIT_PARTIAL_HARD)
7528 jump[0] = CMP(SLJIT_LESS, SLJIT_RETURN_REG, 0, STR_END, 0);
7529 /* Since we successfully read a char above, partial matching must occure. */
7530 check_partial(common, TRUE);
7539 #ifdef SUPPORT_UNICODE
7540 if (common->utf && HAS_EXTRALEN(*cc)) length += GET_EXTRALEN(*cc);
7542 if (common->mode == PCRE2_JIT_COMPLETE && check_str_ptr
7543 && (type == OP_CHAR || !char_has_othercase(common, cc) || char_get_othercase_bit(common, cc) != 0))
7545 OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(length));
7546 add_jump(compiler, backtracks, CMP(SLJIT_GREATER, STR_PTR, 0, STR_END, 0));
7548 context.length = IN_UCHARS(length);
7549 context.sourcereg = -1;
7550 #if defined SLJIT_UNALIGNED && SLJIT_UNALIGNED
7551 context.ucharptr = 0;
7553 return byte_sequence_compare(common, type == OP_CHARI, cc, &context, backtracks);
7557 detect_partial_match(common, backtracks);
7558 #ifdef SUPPORT_UNICODE
7567 if (type == OP_CHAR || !char_has_othercase(common, cc))
7569 read_char_range(common, c, c, FALSE);
7570 add_jump(compiler, backtracks, CMP(SLJIT_NOT_EQUAL, TMP1, 0, SLJIT_IMM, c));
7573 oc = char_othercase(common, c);
7574 read_char_range(common, c < oc ? c : oc, c > oc ? c : oc, FALSE);
7576 if (is_powerof2(bit))
7578 OP2(SLJIT_OR, TMP1, 0, TMP1, 0, SLJIT_IMM, bit);
7579 add_jump(compiler, backtracks, CMP(SLJIT_NOT_EQUAL, TMP1, 0, SLJIT_IMM, c | bit));
7582 jump[0] = CMP(SLJIT_EQUAL, TMP1, 0, SLJIT_IMM, c);
7583 add_jump(compiler, backtracks, CMP(SLJIT_NOT_EQUAL, TMP1, 0, SLJIT_IMM, oc));
7590 detect_partial_match(common, backtracks);
7593 #ifdef SUPPORT_UNICODE
7596 #if PCRE2_CODE_UNIT_WIDTH == 8
7600 OP1(SLJIT_MOV_U8, TMP1, 0, SLJIT_MEM1(STR_PTR), 0);
7601 if (type == OP_NOT || !char_has_othercase(common, cc))
7602 add_jump(compiler, backtracks, CMP(SLJIT_EQUAL, TMP1, 0, SLJIT_IMM, c));
7605 /* Since UTF8 code page is fixed, we know that c is in [a-z] or [A-Z] range. */
7606 OP2(SLJIT_OR, TMP2, 0, TMP1, 0, SLJIT_IMM, 0x20);
7607 add_jump(compiler, backtracks, CMP(SLJIT_EQUAL, TMP2, 0, SLJIT_IMM, c | 0x20));
7609 /* Skip the variable-length character. */
7610 OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
7611 jump[0] = CMP(SLJIT_LESS, TMP1, 0, SLJIT_IMM, 0xc0);
7612 OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(TMP1), (sljit_sw)PRIV(utf8_table4) - 0xc0);
7613 OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, TMP1, 0);
7618 #endif /* PCRE2_CODE_UNIT_WIDTH == 8 */
7620 GETCHARLEN(c, cc, length);
7624 #endif /* SUPPORT_UNICODE */
7627 if (type == OP_NOT || !char_has_othercase(common, cc))
7629 read_char_range(common, c, c, TRUE);
7630 add_jump(compiler, backtracks, CMP(SLJIT_EQUAL, TMP1, 0, SLJIT_IMM, c));
7634 oc = char_othercase(common, c);
7635 read_char_range(common, c < oc ? c : oc, c > oc ? c : oc, TRUE);
7637 if (is_powerof2(bit))
7639 OP2(SLJIT_OR, TMP1, 0, TMP1, 0, SLJIT_IMM, bit);
7640 add_jump(compiler, backtracks, CMP(SLJIT_EQUAL, TMP1, 0, SLJIT_IMM, c | bit));
7644 add_jump(compiler, backtracks, CMP(SLJIT_EQUAL, TMP1, 0, SLJIT_IMM, c));
7645 add_jump(compiler, backtracks, CMP(SLJIT_EQUAL, TMP1, 0, SLJIT_IMM, oc));
7653 detect_partial_match(common, backtracks);
7655 #if defined SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH == 8
7656 bit = (common->utf && is_char7_bitset((const sljit_u8 *)cc, type == OP_NCLASS)) ? 127 : 255;
7657 read_char_range(common, 0, bit, type == OP_NCLASS);
7659 read_char_range(common, 0, 255, type == OP_NCLASS);
7662 if (optimize_class(common, (const sljit_u8 *)cc, type == OP_NCLASS, FALSE, backtracks))
7663 return cc + 32 / sizeof(PCRE2_UCHAR);
7665 #if defined SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH == 8
7669 jump[0] = CMP(SLJIT_GREATER, TMP1, 0, SLJIT_IMM, bit);
7670 if (type == OP_CLASS)
7672 add_jump(compiler, backtracks, jump[0]);
7676 #elif PCRE2_CODE_UNIT_WIDTH != 8
7677 jump[0] = CMP(SLJIT_GREATER, TMP1, 0, SLJIT_IMM, 255);
7678 if (type == OP_CLASS)
7680 add_jump(compiler, backtracks, jump[0]);
7683 #endif /* SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH == 8 */
7685 OP2(SLJIT_AND, TMP2, 0, TMP1, 0, SLJIT_IMM, 0x7);
7686 OP2(SLJIT_LSHR, TMP1, 0, TMP1, 0, SLJIT_IMM, 3);
7687 OP1(SLJIT_MOV_U8, TMP1, 0, SLJIT_MEM1(TMP1), (sljit_sw)cc);
7688 OP2(SLJIT_SHL, TMP2, 0, SLJIT_IMM, 1, TMP2, 0);
7689 OP2(SLJIT_AND | SLJIT_SET_Z, SLJIT_UNUSED, 0, TMP1, 0, TMP2, 0);
7690 add_jump(compiler, backtracks, JUMP(SLJIT_ZERO));
7692 #if defined SUPPORT_UNICODE || PCRE2_CODE_UNIT_WIDTH != 8
7693 if (jump[0] != NULL)
7696 return cc + 32 / sizeof(PCRE2_UCHAR);
7698 #if defined SUPPORT_UNICODE || PCRE2_CODE_UNIT_WIDTH == 16 || PCRE2_CODE_UNIT_WIDTH == 32
7701 detect_partial_match(common, backtracks);
7702 compile_xclass_matchingpath(common, cc + LINK_SIZE, backtracks);
7703 return cc + GET(cc, 0) - 1;
7706 SLJIT_UNREACHABLE();
7710 static SLJIT_INLINE PCRE2_SPTR compile_charn_matchingpath(compiler_common *common, PCRE2_SPTR cc, PCRE2_SPTR ccend, jump_list **backtracks)
7712 /* This function consumes at least one input character. */
7713 /* To decrease the number of length checks, we try to concatenate the fixed length character sequences. */
7715 PCRE2_SPTR ccbegin = cc;
7716 compare_context context;
7728 #ifdef SUPPORT_UNICODE
7729 if (common->utf && HAS_EXTRALEN(cc[1]))
7730 size += GET_EXTRALEN(cc[1]);
7733 else if (*cc == OP_CHARI)
7736 #ifdef SUPPORT_UNICODE
7739 if (char_has_othercase(common, cc + 1) && char_get_othercase_bit(common, cc + 1) == 0)
7741 else if (HAS_EXTRALEN(cc[1]))
7742 size += GET_EXTRALEN(cc[1]);
7746 if (char_has_othercase(common, cc + 1) && char_get_othercase_bit(common, cc + 1) == 0)
7753 context.length += IN_UCHARS(size);
7755 while (size > 0 && context.length <= 128);
7758 if (context.length > 0)
7760 /* We have a fixed-length byte sequence. */
7761 OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, context.length);
7762 add_jump(compiler, backtracks, CMP(SLJIT_GREATER, STR_PTR, 0, STR_END, 0));
7764 context.sourcereg = -1;
7765 #if defined SLJIT_UNALIGNED && SLJIT_UNALIGNED
7766 context.ucharptr = 0;
7768 do cc = byte_sequence_compare(common, *cc == OP_CHARI, cc + 1, &context, backtracks); while (context.length > 0);
7772 /* A non-fixed length character will be checked if length == 0. */
7773 return compile_char1_matchingpath(common, *cc, cc + 1, backtracks, TRUE);
7776 /* Forward definitions. */
7777 static void compile_matchingpath(compiler_common *, PCRE2_SPTR, PCRE2_SPTR, backtrack_common *);
7778 static void compile_backtrackingpath(compiler_common *, struct backtrack_common *);
7780 #define PUSH_BACKTRACK(size, ccstart, error) \
7783 backtrack = sljit_alloc_memory(compiler, (size)); \
7784 if (SLJIT_UNLIKELY(sljit_get_compiler_error(compiler))) \
7786 memset(backtrack, 0, size); \
7787 backtrack->prev = parent->top; \
7788 backtrack->cc = (ccstart); \
7789 parent->top = backtrack; \
7793 #define PUSH_BACKTRACK_NOVALUE(size, ccstart) \
7796 backtrack = sljit_alloc_memory(compiler, (size)); \
7797 if (SLJIT_UNLIKELY(sljit_get_compiler_error(compiler))) \
7799 memset(backtrack, 0, size); \
7800 backtrack->prev = parent->top; \
7801 backtrack->cc = (ccstart); \
7802 parent->top = backtrack; \
7806 #define BACKTRACK_AS(type) ((type *)backtrack)
7808 static void compile_dnref_search(compiler_common *common, PCRE2_SPTR cc, jump_list **backtracks)
7810 /* The OVECTOR offset goes to TMP2. */
7812 int count = GET2(cc, 1 + IMM2_SIZE);
7813 PCRE2_SPTR slot = common->name_table + GET2(cc, 1) * common->name_entry_size;
7814 unsigned int offset;
7815 jump_list *found = NULL;
7817 SLJIT_ASSERT(*cc == OP_DNREF || *cc == OP_DNREFI);
7819 OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), OVECTOR(1));
7824 offset = GET2(slot, 0) << 1;
7825 GET_LOCAL_BASE(TMP2, 0, OVECTOR(offset));
7826 add_jump(compiler, &found, CMP(SLJIT_NOT_EQUAL, SLJIT_MEM1(SLJIT_SP), OVECTOR(offset), TMP1, 0));
7827 slot += common->name_entry_size;
7830 offset = GET2(slot, 0) << 1;
7831 GET_LOCAL_BASE(TMP2, 0, OVECTOR(offset));
7832 if (backtracks != NULL && !common->unset_backref)
7833 add_jump(compiler, backtracks, CMP(SLJIT_EQUAL, SLJIT_MEM1(SLJIT_SP), OVECTOR(offset), TMP1, 0));
7835 set_jumps(found, LABEL());
7838 static void compile_ref_matchingpath(compiler_common *common, PCRE2_SPTR cc, jump_list **backtracks, BOOL withchecks, BOOL emptyfail)
7841 BOOL ref = (*cc == OP_REF || *cc == OP_REFI);
7843 struct sljit_jump *jump = NULL;
7844 struct sljit_jump *partial;
7845 struct sljit_jump *nopartial;
7849 offset = GET2(cc, 1) << 1;
7850 OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), OVECTOR(offset));
7851 /* OVECTOR(1) contains the "string begin - 1" constant. */
7852 if (withchecks && !common->unset_backref)
7853 add_jump(compiler, backtracks, CMP(SLJIT_EQUAL, TMP1, 0, SLJIT_MEM1(SLJIT_SP), OVECTOR(1)));
7856 OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(TMP2), 0);
7858 #if defined SUPPORT_UNICODE
7859 if (common->utf && *cc == OP_REFI)
7861 SLJIT_ASSERT(TMP1 == SLJIT_R0 && STR_PTR == SLJIT_R1);
7863 OP1(SLJIT_MOV, SLJIT_R2, 0, SLJIT_MEM1(SLJIT_SP), OVECTOR(offset + 1));
7865 OP1(SLJIT_MOV, SLJIT_R2, 0, SLJIT_MEM1(TMP2), sizeof(sljit_sw));
7868 jump = CMP(SLJIT_EQUAL, TMP1, 0, SLJIT_R2, 0);
7869 /* No free saved registers so save data on stack. */
7871 OP1(SLJIT_MOV, SLJIT_R3, 0, STR_END, 0);
7872 sljit_emit_icall(compiler, SLJIT_CALL, SLJIT_RET(SW) | SLJIT_ARG1(SW) | SLJIT_ARG2(SW) | SLJIT_ARG3(SW) | SLJIT_ARG4(SW), SLJIT_IMM, SLJIT_FUNC_OFFSET(do_utf_caselesscmp));
7873 OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_RETURN_REG, 0);
7875 if (common->mode == PCRE2_JIT_COMPLETE)
7876 add_jump(compiler, backtracks, CMP(SLJIT_LESS_EQUAL, SLJIT_RETURN_REG, 0, SLJIT_IMM, 1));
7879 OP2(SLJIT_SUB | SLJIT_SET_Z | SLJIT_SET_LESS, SLJIT_UNUSED, 0, SLJIT_RETURN_REG, 0, SLJIT_IMM, 1);
7881 add_jump(compiler, backtracks, JUMP(SLJIT_LESS));
7883 nopartial = JUMP(SLJIT_NOT_EQUAL);
7884 OP1(SLJIT_MOV, STR_PTR, 0, STR_END, 0);
7885 check_partial(common, FALSE);
7886 add_jump(compiler, backtracks, JUMP(SLJIT_JUMP));
7887 JUMPHERE(nopartial);
7891 #endif /* SUPPORT_UNICODE */
7894 OP2(SLJIT_SUB | SLJIT_SET_Z, TMP2, 0, SLJIT_MEM1(SLJIT_SP), OVECTOR(offset + 1), TMP1, 0);
7896 OP2(SLJIT_SUB | SLJIT_SET_Z, TMP2, 0, SLJIT_MEM1(TMP2), sizeof(sljit_sw), TMP1, 0);
7899 jump = JUMP(SLJIT_ZERO);
7901 OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, TMP2, 0);
7902 partial = CMP(SLJIT_GREATER, STR_PTR, 0, STR_END, 0);
7903 if (common->mode == PCRE2_JIT_COMPLETE)
7904 add_jump(compiler, backtracks, partial);
7906 add_jump(compiler, *cc == OP_REF ? &common->casefulcmp : &common->caselesscmp, JUMP(SLJIT_FAST_CALL));
7907 add_jump(compiler, backtracks, CMP(SLJIT_NOT_EQUAL, TMP2, 0, SLJIT_IMM, 0));
7909 if (common->mode != PCRE2_JIT_COMPLETE)
7911 nopartial = JUMP(SLJIT_JUMP);
7913 /* TMP2 -= STR_END - STR_PTR */
7914 OP2(SLJIT_SUB, TMP2, 0, TMP2, 0, STR_PTR, 0);
7915 OP2(SLJIT_ADD, TMP2, 0, TMP2, 0, STR_END, 0);
7916 partial = CMP(SLJIT_EQUAL, TMP2, 0, SLJIT_IMM, 0);
7917 OP1(SLJIT_MOV, STR_PTR, 0, STR_END, 0);
7918 add_jump(compiler, *cc == OP_REF ? &common->casefulcmp : &common->caselesscmp, JUMP(SLJIT_FAST_CALL));
7919 add_jump(compiler, backtracks, CMP(SLJIT_NOT_EQUAL, TMP2, 0, SLJIT_IMM, 0));
7921 check_partial(common, FALSE);
7922 add_jump(compiler, backtracks, JUMP(SLJIT_JUMP));
7923 JUMPHERE(nopartial);
7930 add_jump(compiler, backtracks, jump);
7936 static SLJIT_INLINE PCRE2_SPTR compile_ref_iterator_matchingpath(compiler_common *common, PCRE2_SPTR cc, backtrack_common *parent)
7939 BOOL ref = (*cc == OP_REF || *cc == OP_REFI);
7940 backtrack_common *backtrack;
7943 struct sljit_label *label;
7944 struct sljit_jump *zerolength;
7945 struct sljit_jump *jump = NULL;
7946 PCRE2_SPTR ccbegin = cc;
7947 int min = 0, max = 0;
7950 PUSH_BACKTRACK(sizeof(ref_iterator_backtrack), cc, NULL);
7953 offset = GET2(cc, 1) << 1;
7956 type = cc[1 + IMM2_SIZE];
7958 SLJIT_COMPILE_ASSERT((OP_CRSTAR & 0x1) == 0, crstar_opcode_must_be_even);
7959 minimize = (type & 0x1) != 0;
7966 cc += 1 + IMM2_SIZE + 1;
7972 cc += 1 + IMM2_SIZE + 1;
7978 cc += 1 + IMM2_SIZE + 1;
7982 min = GET2(cc, 1 + IMM2_SIZE + 1);
7983 max = GET2(cc, 1 + IMM2_SIZE + 1 + IMM2_SIZE);
7984 cc += 1 + IMM2_SIZE + 1 + 2 * IMM2_SIZE;
7987 SLJIT_UNREACHABLE();
7995 allocate_stack(common, 2);
7997 OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), OVECTOR(offset));
7998 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), STR_PTR, 0);
7999 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(1), SLJIT_IMM, 0);
8000 /* Temporary release of STR_PTR. */
8001 OP2(SLJIT_ADD, STACK_TOP, 0, STACK_TOP, 0, SLJIT_IMM, sizeof(sljit_sw));
8002 /* Handles both invalid and empty cases. Since the minimum repeat,
8003 is zero the invalid case is basically the same as an empty case. */
8005 zerolength = CMP(SLJIT_EQUAL, TMP1, 0, SLJIT_MEM1(SLJIT_SP), OVECTOR(offset + 1));
8008 compile_dnref_search(common, ccbegin, NULL);
8009 OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(TMP2), 0);
8010 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), POSSESSIVE1, TMP2, 0);
8011 zerolength = CMP(SLJIT_EQUAL, TMP1, 0, SLJIT_MEM1(TMP2), sizeof(sljit_sw));
8013 /* Restore if not zero length. */
8014 OP2(SLJIT_SUB, STACK_TOP, 0, STACK_TOP, 0, SLJIT_IMM, sizeof(sljit_sw));
8018 allocate_stack(common, 1);
8020 OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), OVECTOR(offset));
8021 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), SLJIT_IMM, 0);
8024 add_jump(compiler, &backtrack->topbacktracks, CMP(SLJIT_EQUAL, TMP1, 0, SLJIT_MEM1(SLJIT_SP), OVECTOR(1)));
8025 zerolength = CMP(SLJIT_EQUAL, TMP1, 0, SLJIT_MEM1(SLJIT_SP), OVECTOR(offset + 1));
8029 compile_dnref_search(common, ccbegin, &backtrack->topbacktracks);
8030 OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(TMP2), 0);
8031 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), POSSESSIVE1, TMP2, 0);
8032 zerolength = CMP(SLJIT_EQUAL, TMP1, 0, SLJIT_MEM1(TMP2), sizeof(sljit_sw));
8036 if (min > 1 || max > 1)
8037 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), POSSESSIVE0, SLJIT_IMM, 0);
8041 OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_SP), POSSESSIVE1);
8042 compile_ref_matchingpath(common, ccbegin, &backtrack->topbacktracks, FALSE, FALSE);
8044 if (min > 1 || max > 1)
8046 OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), POSSESSIVE0);
8047 OP2(SLJIT_ADD, TMP1, 0, TMP1, 0, SLJIT_IMM, 1);
8048 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), POSSESSIVE0, TMP1, 0);
8050 CMPTO(SLJIT_LESS, TMP1, 0, SLJIT_IMM, min, label);
8053 jump = CMP(SLJIT_GREATER_EQUAL, TMP1, 0, SLJIT_IMM, max);
8054 allocate_stack(common, 1);
8055 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), STR_PTR, 0);
8056 JUMPTO(SLJIT_JUMP, label);
8063 /* Includes min > 1 case as well. */
8064 allocate_stack(common, 1);
8065 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), STR_PTR, 0);
8066 JUMPTO(SLJIT_JUMP, label);
8069 JUMPHERE(zerolength);
8070 BACKTRACK_AS(ref_iterator_backtrack)->matchingpath = LABEL();
8072 count_match(common);
8076 allocate_stack(common, ref ? 2 : 3);
8078 OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), OVECTOR(offset));
8079 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), SLJIT_IMM, 0);
8080 if (type != OP_CRMINSTAR)
8081 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(1), SLJIT_IMM, 0);
8085 /* Handles both invalid and empty cases. Since the minimum repeat,
8086 is zero the invalid case is basically the same as an empty case. */
8088 zerolength = CMP(SLJIT_EQUAL, TMP1, 0, SLJIT_MEM1(SLJIT_SP), OVECTOR(offset + 1));
8091 compile_dnref_search(common, ccbegin, NULL);
8092 OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(TMP2), 0);
8093 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(2), TMP2, 0);
8094 zerolength = CMP(SLJIT_EQUAL, TMP1, 0, SLJIT_MEM1(TMP2), sizeof(sljit_sw));
8096 /* Length is non-zero, we can match real repeats. */
8097 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), STR_PTR, 0);
8098 jump = JUMP(SLJIT_JUMP);
8104 add_jump(compiler, &backtrack->topbacktracks, CMP(SLJIT_EQUAL, TMP1, 0, SLJIT_MEM1(SLJIT_SP), OVECTOR(1)));
8105 zerolength = CMP(SLJIT_EQUAL, TMP1, 0, SLJIT_MEM1(SLJIT_SP), OVECTOR(offset + 1));
8109 compile_dnref_search(common, ccbegin, &backtrack->topbacktracks);
8110 OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(TMP2), 0);
8111 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(2), TMP2, 0);
8112 zerolength = CMP(SLJIT_EQUAL, TMP1, 0, SLJIT_MEM1(TMP2), sizeof(sljit_sw));
8116 BACKTRACK_AS(ref_iterator_backtrack)->matchingpath = LABEL();
8118 add_jump(compiler, &backtrack->topbacktracks, CMP(SLJIT_GREATER_EQUAL, SLJIT_MEM1(STACK_TOP), STACK(1), SLJIT_IMM, max));
8121 OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(STACK_TOP), STACK(2));
8122 compile_ref_matchingpath(common, ccbegin, &backtrack->topbacktracks, TRUE, TRUE);
8123 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), STR_PTR, 0);
8127 OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(STACK_TOP), STACK(1));
8128 OP2(SLJIT_ADD, TMP1, 0, TMP1, 0, SLJIT_IMM, 1);
8129 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(1), TMP1, 0);
8130 CMPTO(SLJIT_LESS, TMP1, 0, SLJIT_IMM, min, BACKTRACK_AS(ref_iterator_backtrack)->matchingpath);
8133 OP2(SLJIT_ADD, SLJIT_MEM1(STACK_TOP), STACK(1), SLJIT_MEM1(STACK_TOP), STACK(1), SLJIT_IMM, 1);
8137 JUMPHERE(zerolength);
8139 count_match(common);
8143 static SLJIT_INLINE PCRE2_SPTR compile_recurse_matchingpath(compiler_common *common, PCRE2_SPTR cc, backtrack_common *parent)
8146 backtrack_common *backtrack;
8147 recurse_entry *entry = common->entries;
8148 recurse_entry *prev = NULL;
8149 sljit_sw start = GET(cc, 1);
8150 PCRE2_SPTR start_cc;
8151 BOOL needs_control_head;
8153 PUSH_BACKTRACK(sizeof(recurse_backtrack), cc, NULL);
8155 /* Inlining simple patterns. */
8156 if (get_framesize(common, common->start + start, NULL, TRUE, &needs_control_head) == no_stack)
8158 start_cc = common->start + start;
8159 compile_matchingpath(common, next_opcode(common, start_cc), bracketend(start_cc) - (1 + LINK_SIZE), backtrack);
8160 BACKTRACK_AS(recurse_backtrack)->inlined_pattern = TRUE;
8161 return cc + 1 + LINK_SIZE;
8164 while (entry != NULL)
8166 if (entry->start == start)
8169 entry = entry->next;
8174 entry = sljit_alloc_memory(compiler, sizeof(recurse_entry));
8175 if (SLJIT_UNLIKELY(sljit_get_compiler_error(compiler)))
8178 entry->entry_label = NULL;
8179 entry->backtrack_label = NULL;
8180 entry->entry_calls = NULL;
8181 entry->backtrack_calls = NULL;
8182 entry->start = start;
8187 common->entries = entry;
8190 BACKTRACK_AS(recurse_backtrack)->entry = entry;
8192 if (entry->entry_label == NULL)
8193 add_jump(compiler, &entry->entry_calls, JUMP(SLJIT_FAST_CALL));
8195 JUMPTO(SLJIT_FAST_CALL, entry->entry_label);
8196 /* Leave if the match is failed. */
8197 add_jump(compiler, &backtrack->topbacktracks, CMP(SLJIT_EQUAL, TMP1, 0, SLJIT_IMM, 0));
8198 BACKTRACK_AS(recurse_backtrack)->matchingpath = LABEL();
8199 return cc + 1 + LINK_SIZE;
8202 static sljit_s32 SLJIT_FUNC do_callout(struct jit_arguments *arguments, pcre2_callout_block *callout_block, PCRE2_SPTR *jit_ovector)
8205 PCRE2_SIZE *ovector;
8206 sljit_u32 oveccount, capture_top;
8208 if (arguments->callout == NULL)
8211 SLJIT_COMPILE_ASSERT(sizeof (PCRE2_SIZE) <= sizeof (sljit_sw), pcre2_size_must_be_lower_than_sljit_sw_size);
8213 begin = arguments->begin;
8214 ovector = (PCRE2_SIZE*)(callout_block + 1);
8215 oveccount = callout_block->capture_top;
8217 SLJIT_ASSERT(oveccount >= 1);
8219 callout_block->version = 2;
8220 callout_block->callout_flags = 0;
8222 /* Offsets in subject. */
8223 callout_block->subject_length = arguments->end - arguments->begin;
8224 callout_block->start_match = jit_ovector[0] - begin;
8225 callout_block->current_position = (PCRE2_SPTR)callout_block->offset_vector - begin;
8226 callout_block->subject = begin;
8228 /* Convert and copy the JIT offset vector to the ovector array. */
8229 callout_block->capture_top = 1;
8230 callout_block->offset_vector = ovector;
8232 ovector[0] = PCRE2_UNSET;
8233 ovector[1] = PCRE2_UNSET;
8238 /* Convert pointers to sizes. */
8239 while (--oveccount != 0)
8243 ovector[0] = (PCRE2_SIZE)(jit_ovector[0] - begin);
8244 ovector[1] = (PCRE2_SIZE)(jit_ovector[1] - begin);
8246 if (ovector[0] != PCRE2_UNSET)
8247 callout_block->capture_top = capture_top;
8253 return (arguments->callout)(callout_block, arguments->callout_data);
8256 #define CALLOUT_ARG_OFFSET(arg) \
8257 SLJIT_OFFSETOF(pcre2_callout_block, arg)
8259 static SLJIT_INLINE PCRE2_SPTR compile_callout_matchingpath(compiler_common *common, PCRE2_SPTR cc, backtrack_common *parent)
8262 backtrack_common *backtrack;
8263 sljit_s32 mov_opcode;
8264 unsigned int callout_length = (*cc == OP_CALLOUT)
8265 ? PRIV(OP_lengths)[OP_CALLOUT] : GET(cc, 1 + 2 * LINK_SIZE);
8269 sljit_uw callout_arg_size = (common->re->top_bracket + 1) * 2 * sizeof(sljit_sw);
8271 PUSH_BACKTRACK(sizeof(backtrack_common), cc, NULL);
8273 callout_arg_size = (sizeof(pcre2_callout_block) + callout_arg_size + sizeof(sljit_sw) - 1) / sizeof(sljit_sw);
8275 allocate_stack(common, callout_arg_size);
8277 SLJIT_ASSERT(common->capture_last_ptr != 0);
8278 OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_SP), common->capture_last_ptr);
8279 OP1(SLJIT_MOV, TMP1, 0, ARGUMENTS, 0);
8280 value1 = (*cc == OP_CALLOUT) ? cc[1 + 2 * LINK_SIZE] : 0;
8281 OP1(SLJIT_MOV_U32, SLJIT_MEM1(STACK_TOP), CALLOUT_ARG_OFFSET(callout_number), SLJIT_IMM, value1);
8282 OP1(SLJIT_MOV_U32, SLJIT_MEM1(STACK_TOP), CALLOUT_ARG_OFFSET(capture_last), TMP2, 0);
8283 OP1(SLJIT_MOV_U32, SLJIT_MEM1(STACK_TOP), CALLOUT_ARG_OFFSET(capture_top), SLJIT_IMM, common->re->top_bracket + 1);
8285 /* These pointer sized fields temporarly stores internal variables. */
8286 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), CALLOUT_ARG_OFFSET(offset_vector), STR_PTR, 0);
8288 if (common->mark_ptr != 0)
8289 OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(TMP1), SLJIT_OFFSETOF(jit_arguments, mark_ptr));
8290 mov_opcode = (sizeof(PCRE2_SIZE) == 4) ? SLJIT_MOV_U32 : SLJIT_MOV;
8291 OP1(mov_opcode, SLJIT_MEM1(STACK_TOP), CALLOUT_ARG_OFFSET(pattern_position), SLJIT_IMM, GET(cc, 1));
8292 OP1(mov_opcode, SLJIT_MEM1(STACK_TOP), CALLOUT_ARG_OFFSET(next_item_length), SLJIT_IMM, GET(cc, 1 + LINK_SIZE));
8294 if (*cc == OP_CALLOUT)
8302 value1 = (sljit_sw) (cc + (1 + 4*LINK_SIZE) + 1);
8303 value2 = (callout_length - (1 + 4*LINK_SIZE + 2));
8304 value3 = (sljit_sw) (GET(cc, 1 + 3*LINK_SIZE));
8307 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), CALLOUT_ARG_OFFSET(callout_string), SLJIT_IMM, value1);
8308 OP1(mov_opcode, SLJIT_MEM1(STACK_TOP), CALLOUT_ARG_OFFSET(callout_string_length), SLJIT_IMM, value2);
8309 OP1(mov_opcode, SLJIT_MEM1(STACK_TOP), CALLOUT_ARG_OFFSET(callout_string_offset), SLJIT_IMM, value3);
8310 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), CALLOUT_ARG_OFFSET(mark), (common->mark_ptr != 0) ? TMP2 : SLJIT_IMM, 0);
8312 SLJIT_ASSERT(TMP1 == SLJIT_R0 && STR_PTR == SLJIT_R1);
8314 /* Needed to save important temporary registers. */
8315 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), LOCALS0, STR_PTR, 0);
8316 /* SLJIT_R0 = arguments */
8317 OP1(SLJIT_MOV, SLJIT_R1, 0, STACK_TOP, 0);
8318 GET_LOCAL_BASE(SLJIT_R2, 0, OVECTOR_START);
8319 sljit_emit_icall(compiler, SLJIT_CALL, SLJIT_RET(S32) | SLJIT_ARG1(SW) | SLJIT_ARG2(SW) | SLJIT_ARG3(SW), SLJIT_IMM, SLJIT_FUNC_OFFSET(do_callout));
8320 OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(SLJIT_SP), LOCALS0);
8321 free_stack(common, callout_arg_size);
8323 /* Check return value. */
8324 OP2(SLJIT_SUB32 | SLJIT_SET_Z | SLJIT_SET_SIG_GREATER, SLJIT_UNUSED, 0, SLJIT_RETURN_REG, 0, SLJIT_IMM, 0);
8325 add_jump(compiler, &backtrack->topbacktracks, JUMP(SLJIT_SIG_GREATER32));
8326 if (common->abort_label == NULL)
8327 add_jump(compiler, &common->abort, JUMP(SLJIT_NOT_EQUAL32) /* SIG_LESS */);
8329 JUMPTO(SLJIT_NOT_EQUAL32 /* SIG_LESS */, common->abort_label);
8330 return cc + callout_length;
8333 #undef CALLOUT_ARG_SIZE
8334 #undef CALLOUT_ARG_OFFSET
8336 static SLJIT_INLINE BOOL assert_needs_str_ptr_saving(PCRE2_SPTR cc)
8342 case OP_CALLOUT_STR:
8343 cc += GET(cc, 1 + 2*LINK_SIZE);
8346 case OP_NOT_WORD_BOUNDARY:
8347 case OP_WORD_BOUNDARY:
8354 cc += PRIV(OP_lengths)[*cc];
8366 static PCRE2_SPTR compile_assert_matchingpath(compiler_common *common, PCRE2_SPTR cc, assert_backtrack *backtrack, BOOL conditional)
8371 BOOL local_quit_available = FALSE;
8372 BOOL needs_control_head;
8373 int private_data_ptr;
8374 backtrack_common altbacktrack;
8377 PCRE2_UCHAR bra = OP_BRA;
8378 jump_list *tmp = NULL;
8379 jump_list **target = (conditional) ? &backtrack->condfailed : &backtrack->common.topbacktracks;
8381 /* Saving previous accept variables. */
8382 BOOL save_local_quit_available = common->local_quit_available;
8383 BOOL save_in_positive_assertion = common->in_positive_assertion;
8384 then_trap_backtrack *save_then_trap = common->then_trap;
8385 struct sljit_label *save_quit_label = common->quit_label;
8386 struct sljit_label *save_accept_label = common->accept_label;
8387 jump_list *save_quit = common->quit;
8388 jump_list *save_positive_assertion_quit = common->positive_assertion_quit;
8389 jump_list *save_accept = common->accept;
8390 struct sljit_jump *jump;
8391 struct sljit_jump *brajump = NULL;
8393 /* Assert captures then. */
8394 common->then_trap = NULL;
8396 if (*cc == OP_BRAZERO || *cc == OP_BRAMINZERO)
8398 SLJIT_ASSERT(!conditional);
8402 private_data_ptr = PRIVATE_DATA(cc);
8403 SLJIT_ASSERT(private_data_ptr != 0);
8404 framesize = get_framesize(common, cc, NULL, FALSE, &needs_control_head);
8405 backtrack->framesize = framesize;
8406 backtrack->private_data_ptr = private_data_ptr;
8408 SLJIT_ASSERT(opcode >= OP_ASSERT && opcode <= OP_ASSERTBACK_NOT);
8409 found = (opcode == OP_ASSERT || opcode == OP_ASSERTBACK) ? &tmp : target;
8413 if (bra == OP_BRAMINZERO)
8415 /* This is a braminzero backtrack path. */
8416 OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(STACK_TOP), STACK(0));
8417 free_stack(common, 1);
8418 brajump = CMP(SLJIT_EQUAL, STR_PTR, 0, SLJIT_IMM, 0);
8424 if (bra == OP_BRA && !assert_needs_str_ptr_saving(ccbegin + 1 + LINK_SIZE))
8427 if (needs_control_head)
8430 if (framesize == no_frame)
8431 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), private_data_ptr, STACK_TOP, 0);
8434 allocate_stack(common, extrasize);
8436 if (needs_control_head)
8437 OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), common->control_head_ptr);
8440 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), STR_PTR, 0);
8442 if (needs_control_head)
8444 SLJIT_ASSERT(extrasize == 2);
8445 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->control_head_ptr, SLJIT_IMM, 0);
8446 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(1), TMP1, 0);
8451 extrasize = needs_control_head ? 3 : 2;
8452 allocate_stack(common, framesize + extrasize);
8454 OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), private_data_ptr);
8455 OP2(SLJIT_ADD, TMP2, 0, STACK_TOP, 0, SLJIT_IMM, (framesize + extrasize) * sizeof(sljit_sw));
8456 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), private_data_ptr, TMP2, 0);
8457 if (needs_control_head)
8458 OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_SP), common->control_head_ptr);
8459 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), STR_PTR, 0);
8461 if (needs_control_head)
8463 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(2), TMP1, 0);
8464 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(1), TMP2, 0);
8465 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->control_head_ptr, SLJIT_IMM, 0);
8468 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(1), TMP1, 0);
8470 init_frame(common, ccbegin, NULL, framesize + extrasize - 1, extrasize);
8473 memset(&altbacktrack, 0, sizeof(backtrack_common));
8474 if (conditional || (opcode == OP_ASSERT_NOT || opcode == OP_ASSERTBACK_NOT))
8476 /* Control verbs cannot escape from these asserts. */
8477 local_quit_available = TRUE;
8478 common->local_quit_available = TRUE;
8479 common->quit_label = NULL;
8480 common->quit = NULL;
8483 common->in_positive_assertion = (opcode == OP_ASSERT || opcode == OP_ASSERTBACK);
8484 common->positive_assertion_quit = NULL;
8488 common->accept_label = NULL;
8489 common->accept = NULL;
8490 altbacktrack.top = NULL;
8491 altbacktrack.topbacktracks = NULL;
8493 if (*ccbegin == OP_ALT && extrasize > 0)
8494 OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(STACK_TOP), STACK(0));
8496 altbacktrack.cc = ccbegin;
8497 compile_matchingpath(common, ccbegin + 1 + LINK_SIZE, cc, &altbacktrack);
8498 if (SLJIT_UNLIKELY(sljit_get_compiler_error(compiler)))
8500 if (local_quit_available)
8502 common->local_quit_available = save_local_quit_available;
8503 common->quit_label = save_quit_label;
8504 common->quit = save_quit;
8506 common->in_positive_assertion = save_in_positive_assertion;
8507 common->then_trap = save_then_trap;
8508 common->accept_label = save_accept_label;
8509 common->positive_assertion_quit = save_positive_assertion_quit;
8510 common->accept = save_accept;
8513 common->accept_label = LABEL();
8514 if (common->accept != NULL)
8515 set_jumps(common->accept, common->accept_label);
8520 if (framesize == no_frame)
8521 OP1(SLJIT_MOV, STACK_TOP, 0, SLJIT_MEM1(SLJIT_SP), private_data_ptr);
8522 else if (extrasize > 0)
8523 free_stack(common, extrasize);
8525 if (needs_control_head)
8526 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->control_head_ptr, SLJIT_MEM1(STACK_TOP), STACK(-1));
8530 if ((opcode != OP_ASSERT_NOT && opcode != OP_ASSERTBACK_NOT) || conditional)
8532 /* We don't need to keep the STR_PTR, only the previous private_data_ptr. */
8533 OP2(SLJIT_SUB, STACK_TOP, 0, SLJIT_MEM1(SLJIT_SP), private_data_ptr, SLJIT_IMM, (framesize + 1) * sizeof(sljit_sw));
8534 if (needs_control_head)
8535 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->control_head_ptr, SLJIT_MEM1(STACK_TOP), STACK(-1));
8539 OP1(SLJIT_MOV, STACK_TOP, 0, SLJIT_MEM1(SLJIT_SP), private_data_ptr);
8540 if (needs_control_head)
8541 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->control_head_ptr, SLJIT_MEM1(STACK_TOP), STACK(-framesize - 2));
8542 add_jump(compiler, &common->revertframes, JUMP(SLJIT_FAST_CALL));
8543 OP2(SLJIT_ADD, STACK_TOP, 0, STACK_TOP, 0, SLJIT_IMM, (framesize - 1) * sizeof(sljit_sw));
8547 if (opcode == OP_ASSERT_NOT || opcode == OP_ASSERTBACK_NOT)
8549 /* We know that STR_PTR was stored on the top of the stack. */
8553 OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(STACK_TOP), needs_control_head ? STACK(-2) : STACK(-1));
8555 else if (bra == OP_BRAZERO)
8558 OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(STACK_TOP), STACK(-extrasize));
8561 OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(STACK_TOP), STACK(-framesize - 1));
8562 OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(STACK_TOP), STACK(-framesize - extrasize));
8563 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), private_data_ptr, TMP1, 0);
8565 OP2(SLJIT_SUB, STACK_TOP, 0, STACK_TOP, 0, SLJIT_IMM, sizeof(sljit_sw));
8566 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), SLJIT_IMM, 0);
8568 else if (framesize >= 0)
8570 /* For OP_BRA and OP_BRAMINZERO. */
8571 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), private_data_ptr, SLJIT_MEM1(STACK_TOP), STACK(-framesize - 1));
8574 add_jump(compiler, found, JUMP(SLJIT_JUMP));
8576 compile_backtrackingpath(common, altbacktrack.top);
8577 if (SLJIT_UNLIKELY(sljit_get_compiler_error(compiler)))
8579 if (local_quit_available)
8581 common->local_quit_available = save_local_quit_available;
8582 common->quit_label = save_quit_label;
8583 common->quit = save_quit;
8585 common->in_positive_assertion = save_in_positive_assertion;
8586 common->then_trap = save_then_trap;
8587 common->accept_label = save_accept_label;
8588 common->positive_assertion_quit = save_positive_assertion_quit;
8589 common->accept = save_accept;
8592 set_jumps(altbacktrack.topbacktracks, LABEL());
8601 if (local_quit_available)
8603 SLJIT_ASSERT(common->positive_assertion_quit == NULL);
8604 /* Makes the check less complicated below. */
8605 common->positive_assertion_quit = common->quit;
8608 /* None of them matched. */
8609 if (common->positive_assertion_quit != NULL)
8611 jump = JUMP(SLJIT_JUMP);
8612 set_jumps(common->positive_assertion_quit, LABEL());
8613 SLJIT_ASSERT(framesize != no_stack);
8615 OP2(SLJIT_SUB, STACK_TOP, 0, SLJIT_MEM1(SLJIT_SP), private_data_ptr, SLJIT_IMM, extrasize * sizeof(sljit_sw));
8618 OP1(SLJIT_MOV, STACK_TOP, 0, SLJIT_MEM1(SLJIT_SP), private_data_ptr);
8619 add_jump(compiler, &common->revertframes, JUMP(SLJIT_FAST_CALL));
8620 OP2(SLJIT_SUB, STACK_TOP, 0, STACK_TOP, 0, SLJIT_IMM, (extrasize + 1) * sizeof(sljit_sw));
8625 if (needs_control_head)
8626 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->control_head_ptr, SLJIT_MEM1(STACK_TOP), STACK(1));
8628 if (opcode == OP_ASSERT || opcode == OP_ASSERTBACK)
8630 /* Assert is failed. */
8631 if ((conditional && extrasize > 0) || bra == OP_BRAZERO)
8632 OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(STACK_TOP), STACK(0));
8636 /* The topmost item should be 0. */
8637 if (bra == OP_BRAZERO)
8640 free_stack(common, 1);
8641 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), SLJIT_IMM, 0);
8643 else if (extrasize > 0)
8644 free_stack(common, extrasize);
8648 OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(STACK_TOP), STACK(extrasize - 1));
8649 /* The topmost item should be 0. */
8650 if (bra == OP_BRAZERO)
8652 free_stack(common, framesize + extrasize - 1);
8653 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), SLJIT_IMM, 0);
8656 free_stack(common, framesize + extrasize);
8657 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), private_data_ptr, TMP1, 0);
8659 jump = JUMP(SLJIT_JUMP);
8660 if (bra != OP_BRAZERO)
8661 add_jump(compiler, target, jump);
8663 /* Assert is successful. */
8664 set_jumps(tmp, LABEL());
8667 /* We know that STR_PTR was stored on the top of the stack. */
8669 OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(STACK_TOP), STACK(-extrasize));
8671 /* Keep the STR_PTR on the top of the stack. */
8672 if (bra == OP_BRAZERO)
8674 OP2(SLJIT_SUB, STACK_TOP, 0, STACK_TOP, 0, SLJIT_IMM, sizeof(sljit_sw));
8676 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), STR_PTR, 0);
8678 else if (bra == OP_BRAMINZERO)
8680 OP2(SLJIT_SUB, STACK_TOP, 0, STACK_TOP, 0, SLJIT_IMM, sizeof(sljit_sw));
8681 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), SLJIT_IMM, 0);
8688 /* We don't need to keep the STR_PTR, only the previous private_data_ptr. */
8689 OP2(SLJIT_SUB, STACK_TOP, 0, SLJIT_MEM1(SLJIT_SP), private_data_ptr, SLJIT_IMM, (framesize + 1) * sizeof(sljit_sw));
8690 OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(STACK_TOP), STACK(-extrasize + 1));
8694 /* We don't need to keep the STR_PTR, only the previous private_data_ptr. */
8695 OP2(SLJIT_SUB, STACK_TOP, 0, SLJIT_MEM1(SLJIT_SP), private_data_ptr, SLJIT_IMM, (framesize + 2) * sizeof(sljit_sw));
8698 OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(STACK_TOP), STACK(0));
8699 if (bra == OP_BRAMINZERO)
8700 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), SLJIT_IMM, 0);
8704 OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(STACK_TOP), 0);
8705 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), bra == OP_BRAZERO ? STR_PTR : SLJIT_IMM, 0);
8710 if (bra == OP_BRAZERO)
8712 backtrack->matchingpath = LABEL();
8713 SET_LABEL(jump, backtrack->matchingpath);
8715 else if (bra == OP_BRAMINZERO)
8717 JUMPTO(SLJIT_JUMP, backtrack->matchingpath);
8721 OP1(SLJIT_MOV, STACK_TOP, 0, SLJIT_MEM1(SLJIT_SP), private_data_ptr);
8722 add_jump(compiler, &common->revertframes, JUMP(SLJIT_FAST_CALL));
8723 OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(STACK_TOP), STACK(-2));
8724 OP2(SLJIT_ADD, STACK_TOP, 0, STACK_TOP, 0, SLJIT_IMM, (framesize - 1) * sizeof(sljit_sw));
8725 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), private_data_ptr, TMP1, 0);
8727 set_jumps(backtrack->common.topbacktracks, LABEL());
8732 /* AssertNot is successful. */
8736 OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(STACK_TOP), STACK(0));
8741 free_stack(common, 1);
8742 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), SLJIT_IMM, 0);
8744 else if (extrasize > 0)
8745 free_stack(common, extrasize);
8749 OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(STACK_TOP), STACK(0));
8750 OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(STACK_TOP), STACK(extrasize - 1));
8751 /* The topmost item should be 0. */
8754 free_stack(common, framesize + extrasize - 1);
8755 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), SLJIT_IMM, 0);
8758 free_stack(common, framesize + extrasize);
8759 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), private_data_ptr, TMP1, 0);
8762 if (bra == OP_BRAZERO)
8763 backtrack->matchingpath = LABEL();
8764 else if (bra == OP_BRAMINZERO)
8766 JUMPTO(SLJIT_JUMP, backtrack->matchingpath);
8772 SLJIT_ASSERT(found == &backtrack->common.topbacktracks);
8773 set_jumps(backtrack->common.topbacktracks, LABEL());
8774 backtrack->common.topbacktracks = NULL;
8778 if (local_quit_available)
8780 common->local_quit_available = save_local_quit_available;
8781 common->quit_label = save_quit_label;
8782 common->quit = save_quit;
8784 common->in_positive_assertion = save_in_positive_assertion;
8785 common->then_trap = save_then_trap;
8786 common->accept_label = save_accept_label;
8787 common->positive_assertion_quit = save_positive_assertion_quit;
8788 common->accept = save_accept;
8789 return cc + 1 + LINK_SIZE;
8792 static SLJIT_INLINE void match_once_common(compiler_common *common, PCRE2_UCHAR ket, int framesize, int private_data_ptr, BOOL has_alternatives, BOOL needs_control_head)
8799 if (framesize == no_frame)
8800 OP1(SLJIT_MOV, STACK_TOP, 0, SLJIT_MEM1(SLJIT_SP), private_data_ptr);
8803 stacksize = needs_control_head ? 1 : 0;
8804 if (ket != OP_KET || has_alternatives)
8808 free_stack(common, stacksize);
8811 if (needs_control_head)
8812 OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(STACK_TOP), (ket != OP_KET || has_alternatives) ? STACK(-2) : STACK(-1));
8814 /* TMP2 which is set here used by OP_KETRMAX below. */
8815 if (ket == OP_KETRMAX)
8816 OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(STACK_TOP), STACK(-1));
8817 else if (ket == OP_KETRMIN)
8819 /* Move the STR_PTR to the private_data_ptr. */
8820 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), private_data_ptr, SLJIT_MEM1(STACK_TOP), STACK(-1));
8825 stacksize = (ket != OP_KET || has_alternatives) ? 2 : 1;
8826 OP2(SLJIT_SUB, STACK_TOP, 0, SLJIT_MEM1(SLJIT_SP), private_data_ptr, SLJIT_IMM, (framesize + stacksize) * sizeof(sljit_sw));
8827 if (needs_control_head)
8828 OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(STACK_TOP), STACK(-1));
8830 if (ket == OP_KETRMAX)
8832 /* TMP2 which is set here used by OP_KETRMAX below. */
8833 OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(STACK_TOP), STACK(0));
8836 if (needs_control_head)
8837 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->control_head_ptr, TMP1, 0);
8840 static SLJIT_INLINE int match_capture_common(compiler_common *common, int stacksize, int offset, int private_data_ptr)
8844 if (common->capture_last_ptr != 0)
8846 OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), common->capture_last_ptr);
8847 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->capture_last_ptr, SLJIT_IMM, offset >> 1);
8848 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(stacksize), TMP1, 0);
8851 if (common->optimized_cbracket[offset >> 1] == 0)
8853 OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), OVECTOR(offset));
8854 OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_SP), OVECTOR(offset + 1));
8855 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(stacksize), TMP1, 0);
8856 OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), private_data_ptr);
8857 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(stacksize + 1), TMP2, 0);
8858 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), OVECTOR(offset + 1), STR_PTR, 0);
8859 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), OVECTOR(offset), TMP1, 0);
8866 Handling bracketed expressions is probably the most complex part.
8868 Stack layout naming characters:
8869 S - Push the current STR_PTR
8871 A - Push the current STR_PTR. Needed for restoring the STR_PTR
8872 before the next alternative. Not pushed if there are no alternatives.
8873 M - Any values pushed by the current alternative. Can be empty, or anything.
8874 C - Push the previous OVECTOR(i), OVECTOR(i+1) and OVECTOR_PRIV(i) to the stack.
8875 L - Push the previous local (pointed by localptr) to the stack
8876 () - opional values stored on the stack
8877 ()* - optonal, can be stored multiple times
8879 The following list shows the regular expression templates, their PCRE byte codes
8880 and stack layout supported by pcre-sljit.
8882 (?:) OP_BRA | OP_KET A M
8883 () OP_CBRA | OP_KET C M
8884 (?:)+ OP_BRA | OP_KETRMAX 0 A M S ( A M S )*
8885 OP_SBRA | OP_KETRMAX 0 L M S ( L M S )*
8886 (?:)+? OP_BRA | OP_KETRMIN 0 A M S ( A M S )*
8887 OP_SBRA | OP_KETRMIN 0 L M S ( L M S )*
8888 ()+ OP_CBRA | OP_KETRMAX 0 C M S ( C M S )*
8889 OP_SCBRA | OP_KETRMAX 0 C M S ( C M S )*
8890 ()+? OP_CBRA | OP_KETRMIN 0 C M S ( C M S )*
8891 OP_SCBRA | OP_KETRMIN 0 C M S ( C M S )*
8892 (?:)? OP_BRAZERO | OP_BRA | OP_KET S ( A M 0 )
8893 (?:)?? OP_BRAMINZERO | OP_BRA | OP_KET S ( A M 0 )
8894 ()? OP_BRAZERO | OP_CBRA | OP_KET S ( C M 0 )
8895 ()?? OP_BRAMINZERO | OP_CBRA | OP_KET S ( C M 0 )
8896 (?:)* OP_BRAZERO | OP_BRA | OP_KETRMAX S 0 ( A M S )*
8897 OP_BRAZERO | OP_SBRA | OP_KETRMAX S 0 ( L M S )*
8898 (?:)*? OP_BRAMINZERO | OP_BRA | OP_KETRMIN S 0 ( A M S )*
8899 OP_BRAMINZERO | OP_SBRA | OP_KETRMIN S 0 ( L M S )*
8900 ()* OP_BRAZERO | OP_CBRA | OP_KETRMAX S 0 ( C M S )*
8901 OP_BRAZERO | OP_SCBRA | OP_KETRMAX S 0 ( C M S )*
8902 ()*? OP_BRAMINZERO | OP_CBRA | OP_KETRMIN S 0 ( C M S )*
8903 OP_BRAMINZERO | OP_SCBRA | OP_KETRMIN S 0 ( C M S )*
8906 Stack layout naming characters:
8907 A - Push the alternative index (starting from 0) on the stack.
8908 Not pushed if there is no alternatives.
8909 M - Any values pushed by the current alternative. Can be empty, or anything.
8911 The next list shows the possible content of a bracket:
8912 (|) OP_*BRA | OP_ALT ... M A
8913 (?()|) OP_*COND | OP_ALT M A
8914 (?>|) OP_ONCE | OP_ALT ... [stack trace] M A
8915 Or nothing, if trace is unnecessary
8918 static PCRE2_SPTR compile_bracket_matchingpath(compiler_common *common, PCRE2_SPTR cc, backtrack_common *parent)
8921 backtrack_common *backtrack;
8923 int private_data_ptr = 0;
8926 int repeat_ptr = 0, repeat_length = 0;
8927 int repeat_type = 0, repeat_count = 0;
8929 PCRE2_SPTR matchingpath;
8931 PCRE2_UCHAR bra = OP_BRA;
8933 assert_backtrack *assert;
8934 BOOL has_alternatives;
8935 BOOL needs_control_head = FALSE;
8936 struct sljit_jump *jump;
8937 struct sljit_jump *skip;
8938 struct sljit_label *rmax_label = NULL;
8939 struct sljit_jump *braminzero = NULL;
8941 PUSH_BACKTRACK(sizeof(bracket_backtrack), cc, NULL);
8943 if (*cc == OP_BRAZERO || *cc == OP_BRAMINZERO)
8952 matchingpath = bracketend(cc) - 1 - LINK_SIZE;
8953 ket = *matchingpath;
8954 if (ket == OP_KET && PRIVATE_DATA(matchingpath) != 0)
8956 repeat_ptr = PRIVATE_DATA(matchingpath);
8957 repeat_length = PRIVATE_DATA(matchingpath + 1);
8958 repeat_type = PRIVATE_DATA(matchingpath + 2);
8959 repeat_count = PRIVATE_DATA(matchingpath + 3);
8960 SLJIT_ASSERT(repeat_length != 0 && repeat_type != 0 && repeat_count != 0);
8961 if (repeat_type == OP_UPTO)
8963 if (repeat_type == OP_MINUPTO)
8967 matchingpath = ccbegin + 1 + LINK_SIZE;
8968 SLJIT_ASSERT(ket == OP_KET || ket == OP_KETRMAX || ket == OP_KETRMIN);
8969 SLJIT_ASSERT(!((bra == OP_BRAZERO && ket == OP_KETRMIN) || (bra == OP_BRAMINZERO && ket == OP_KETRMAX)));
8972 has_alternatives = *cc == OP_ALT;
8973 if (SLJIT_UNLIKELY(opcode == OP_COND || opcode == OP_SCOND))
8975 SLJIT_COMPILE_ASSERT(OP_DNRREF == OP_RREF + 1 && OP_FALSE == OP_RREF + 2 && OP_TRUE == OP_RREF + 3,
8976 compile_time_checks_must_be_grouped_together);
8977 has_alternatives = ((*matchingpath >= OP_RREF && *matchingpath <= OP_TRUE) || *matchingpath == OP_FAIL) ? FALSE : TRUE;
8980 if (SLJIT_UNLIKELY(opcode == OP_COND) && (*cc == OP_KETRMAX || *cc == OP_KETRMIN))
8983 if (opcode == OP_CBRA || opcode == OP_SCBRA)
8985 /* Capturing brackets has a pre-allocated space. */
8986 offset = GET2(ccbegin, 1 + LINK_SIZE);
8987 if (common->optimized_cbracket[offset] == 0)
8989 private_data_ptr = OVECTOR_PRIV(offset);
8995 private_data_ptr = OVECTOR(offset);
8997 BACKTRACK_AS(bracket_backtrack)->private_data_ptr = private_data_ptr;
8998 matchingpath += IMM2_SIZE;
9000 else if (opcode == OP_ONCE || opcode == OP_SBRA || opcode == OP_SCOND)
9002 /* Other brackets simply allocate the next entry. */
9003 private_data_ptr = PRIVATE_DATA(ccbegin);
9004 SLJIT_ASSERT(private_data_ptr != 0);
9005 BACKTRACK_AS(bracket_backtrack)->private_data_ptr = private_data_ptr;
9006 if (opcode == OP_ONCE)
9007 BACKTRACK_AS(bracket_backtrack)->u.framesize = get_framesize(common, ccbegin, NULL, FALSE, &needs_control_head);
9010 /* Instructions before the first alternative. */
9012 if (ket == OP_KETRMAX || (ket == OP_KETRMIN && bra != OP_BRAMINZERO))
9014 if (bra == OP_BRAZERO)
9018 allocate_stack(common, stacksize);
9021 if (ket == OP_KETRMAX || (ket == OP_KETRMIN && bra != OP_BRAMINZERO))
9023 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(stacksize), SLJIT_IMM, 0);
9027 if (bra == OP_BRAZERO)
9028 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(stacksize), STR_PTR, 0);
9030 if (bra == OP_BRAMINZERO)
9032 /* This is a backtrack path! (Since the try-path of OP_BRAMINZERO matches to the empty string) */
9033 OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(STACK_TOP), STACK(0));
9034 if (ket != OP_KETRMIN)
9036 free_stack(common, 1);
9037 braminzero = CMP(SLJIT_EQUAL, STR_PTR, 0, SLJIT_IMM, 0);
9041 if (opcode == OP_ONCE || opcode >= OP_SBRA)
9043 jump = CMP(SLJIT_NOT_EQUAL, STR_PTR, 0, SLJIT_IMM, 0);
9044 OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(STACK_TOP), STACK(1));
9045 /* Nothing stored during the first run. */
9046 skip = JUMP(SLJIT_JUMP);
9048 /* Checking zero-length iteration. */
9049 if (opcode != OP_ONCE || BACKTRACK_AS(bracket_backtrack)->u.framesize < 0)
9051 /* When we come from outside, private_data_ptr contains the previous STR_PTR. */
9052 braminzero = CMP(SLJIT_EQUAL, STR_PTR, 0, SLJIT_MEM1(SLJIT_SP), private_data_ptr);
9056 /* Except when the whole stack frame must be saved. */
9057 OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), private_data_ptr);
9058 braminzero = CMP(SLJIT_EQUAL, STR_PTR, 0, SLJIT_MEM1(TMP1), STACK(-BACKTRACK_AS(bracket_backtrack)->u.framesize - 2));
9064 jump = CMP(SLJIT_NOT_EQUAL, STR_PTR, 0, SLJIT_IMM, 0);
9065 OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(STACK_TOP), STACK(1));
9071 if (repeat_type != 0)
9073 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), repeat_ptr, SLJIT_IMM, repeat_count);
9074 if (repeat_type == OP_EXACT)
9075 rmax_label = LABEL();
9078 if (ket == OP_KETRMIN)
9079 BACKTRACK_AS(bracket_backtrack)->recursive_matchingpath = LABEL();
9081 if (ket == OP_KETRMAX)
9083 rmax_label = LABEL();
9084 if (has_alternatives && opcode != OP_ONCE && opcode < OP_SBRA && repeat_type == 0)
9085 BACKTRACK_AS(bracket_backtrack)->alternative_matchingpath = rmax_label;
9088 /* Handling capturing brackets and alternatives. */
9089 if (opcode == OP_ONCE)
9092 if (needs_control_head)
9094 OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_SP), common->control_head_ptr);
9098 if (BACKTRACK_AS(bracket_backtrack)->u.framesize < 0)
9100 /* Neither capturing brackets nor recursions are found in the block. */
9101 if (ket == OP_KETRMIN)
9104 if (!needs_control_head)
9105 OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_SP), private_data_ptr);
9109 if (BACKTRACK_AS(bracket_backtrack)->u.framesize == no_frame)
9110 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), private_data_ptr, STACK_TOP, 0);
9111 if (ket == OP_KETRMAX || has_alternatives)
9116 allocate_stack(common, stacksize);
9119 if (needs_control_head)
9122 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), TMP2, 0);
9125 if (ket == OP_KETRMIN)
9127 if (needs_control_head)
9128 OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_SP), private_data_ptr);
9129 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(stacksize), STR_PTR, 0);
9130 if (BACKTRACK_AS(bracket_backtrack)->u.framesize == no_frame)
9131 OP2(SLJIT_ADD, SLJIT_MEM1(SLJIT_SP), private_data_ptr, STACK_TOP, 0, SLJIT_IMM, needs_control_head ? (2 * sizeof(sljit_sw)) : sizeof(sljit_sw));
9132 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(stacksize + 1), TMP2, 0);
9134 else if (ket == OP_KETRMAX || has_alternatives)
9135 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(stacksize), STR_PTR, 0);
9139 if (ket != OP_KET || has_alternatives)
9142 stacksize += BACKTRACK_AS(bracket_backtrack)->u.framesize + 1;
9143 allocate_stack(common, stacksize);
9145 if (needs_control_head)
9146 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), TMP2, 0);
9148 OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), private_data_ptr);
9149 OP2(SLJIT_ADD, TMP2, 0, STACK_TOP, 0, SLJIT_IMM, stacksize * sizeof(sljit_sw));
9151 stacksize = needs_control_head ? 1 : 0;
9152 if (ket != OP_KET || has_alternatives)
9154 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(stacksize), STR_PTR, 0);
9155 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), private_data_ptr, TMP2, 0);
9157 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(stacksize), TMP1, 0);
9161 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), private_data_ptr, TMP2, 0);
9162 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(stacksize), TMP1, 0);
9164 init_frame(common, ccbegin, NULL, BACKTRACK_AS(bracket_backtrack)->u.framesize + stacksize, stacksize + 1);
9167 else if (opcode == OP_CBRA || opcode == OP_SCBRA)
9169 /* Saving the previous values. */
9170 if (common->optimized_cbracket[offset >> 1] != 0)
9172 SLJIT_ASSERT(private_data_ptr == OVECTOR(offset));
9173 allocate_stack(common, 2);
9174 OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), private_data_ptr);
9175 OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_SP), private_data_ptr + sizeof(sljit_sw));
9176 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), private_data_ptr, STR_PTR, 0);
9177 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), TMP1, 0);
9178 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(1), TMP2, 0);
9182 OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_SP), private_data_ptr);
9183 allocate_stack(common, 1);
9184 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), private_data_ptr, STR_PTR, 0);
9185 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), TMP2, 0);
9188 else if (opcode == OP_SBRA || opcode == OP_SCOND)
9190 /* Saving the previous value. */
9191 OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_SP), private_data_ptr);
9192 allocate_stack(common, 1);
9193 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), private_data_ptr, STR_PTR, 0);
9194 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), TMP2, 0);
9196 else if (has_alternatives)
9198 /* Pushing the starting string pointer. */
9199 allocate_stack(common, 1);
9200 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), STR_PTR, 0);
9203 /* Generating code for the first alternative. */
9204 if (opcode == OP_COND || opcode == OP_SCOND)
9206 if (*matchingpath == OP_CREF)
9208 SLJIT_ASSERT(has_alternatives);
9209 add_jump(compiler, &(BACKTRACK_AS(bracket_backtrack)->u.condfailed),
9210 CMP(SLJIT_EQUAL, SLJIT_MEM1(SLJIT_SP), OVECTOR(GET2(matchingpath, 1) << 1), SLJIT_MEM1(SLJIT_SP), OVECTOR(1)));
9211 matchingpath += 1 + IMM2_SIZE;
9213 else if (*matchingpath == OP_DNCREF)
9215 SLJIT_ASSERT(has_alternatives);
9217 i = GET2(matchingpath, 1 + IMM2_SIZE);
9218 slot = common->name_table + GET2(matchingpath, 1) * common->name_entry_size;
9219 OP1(SLJIT_MOV, TMP3, 0, STR_PTR, 0);
9220 OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), OVECTOR(1));
9221 OP2(SLJIT_SUB | SLJIT_SET_Z, TMP2, 0, SLJIT_MEM1(SLJIT_SP), OVECTOR(GET2(slot, 0) << 1), TMP1, 0);
9222 slot += common->name_entry_size;
9226 OP2(SLJIT_SUB, STR_PTR, 0, SLJIT_MEM1(SLJIT_SP), OVECTOR(GET2(slot, 0) << 1), TMP1, 0);
9227 OP2(SLJIT_OR | SLJIT_SET_Z, TMP2, 0, TMP2, 0, STR_PTR, 0);
9228 slot += common->name_entry_size;
9230 OP1(SLJIT_MOV, STR_PTR, 0, TMP3, 0);
9231 add_jump(compiler, &(BACKTRACK_AS(bracket_backtrack)->u.condfailed), JUMP(SLJIT_ZERO));
9232 matchingpath += 1 + 2 * IMM2_SIZE;
9234 else if ((*matchingpath >= OP_RREF && *matchingpath <= OP_TRUE) || *matchingpath == OP_FAIL)
9236 /* Never has other case. */
9237 BACKTRACK_AS(bracket_backtrack)->u.condfailed = NULL;
9238 SLJIT_ASSERT(!has_alternatives);
9240 if (*matchingpath == OP_TRUE)
9245 else if (*matchingpath == OP_FALSE || *matchingpath == OP_FAIL)
9247 else if (*matchingpath == OP_RREF)
9249 stacksize = GET2(matchingpath, 1);
9250 if (common->currententry == NULL)
9252 else if (stacksize == RREF_ANY)
9254 else if (common->currententry->start == 0)
9255 stacksize = stacksize == 0;
9257 stacksize = stacksize == (int)GET2(common->start, common->currententry->start + 1 + LINK_SIZE);
9260 matchingpath += 1 + IMM2_SIZE;
9264 if (common->currententry == NULL || common->currententry->start == 0)
9268 stacksize = GET2(matchingpath, 1 + IMM2_SIZE);
9269 slot = common->name_table + GET2(matchingpath, 1) * common->name_entry_size;
9270 i = (int)GET2(common->start, common->currententry->start + 1 + LINK_SIZE);
9271 while (stacksize > 0)
9273 if ((int)GET2(slot, 0) == i)
9275 slot += common->name_entry_size;
9281 matchingpath += 1 + 2 * IMM2_SIZE;
9284 /* The stacksize == 0 is a common "else" case. */
9289 matchingpath = cc + 1 + LINK_SIZE;
9298 SLJIT_ASSERT(has_alternatives && *matchingpath >= OP_ASSERT && *matchingpath <= OP_ASSERTBACK_NOT);
9299 /* Similar code as PUSH_BACKTRACK macro. */
9300 assert = sljit_alloc_memory(compiler, sizeof(assert_backtrack));
9301 if (SLJIT_UNLIKELY(sljit_get_compiler_error(compiler)))
9303 memset(assert, 0, sizeof(assert_backtrack));
9304 assert->common.cc = matchingpath;
9305 BACKTRACK_AS(bracket_backtrack)->u.assert = assert;
9306 matchingpath = compile_assert_matchingpath(common, matchingpath, assert, TRUE);
9310 compile_matchingpath(common, matchingpath, cc, backtrack);
9311 if (SLJIT_UNLIKELY(sljit_get_compiler_error(compiler)))
9314 if (opcode == OP_ONCE)
9315 match_once_common(common, ket, BACKTRACK_AS(bracket_backtrack)->u.framesize, private_data_ptr, has_alternatives, needs_control_head);
9318 if (repeat_type == OP_MINUPTO)
9320 /* We need to preserve the counter. TMP2 will be used below. */
9321 OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_SP), repeat_ptr);
9324 if (ket != OP_KET || bra != OP_BRA)
9328 if (common->capture_last_ptr != 0)
9330 if (common->optimized_cbracket[offset >> 1] == 0)
9333 if (has_alternatives && opcode != OP_ONCE)
9337 allocate_stack(common, stacksize);
9340 if (repeat_type == OP_MINUPTO)
9342 /* TMP2 was set above. */
9343 OP2(SLJIT_SUB, SLJIT_MEM1(STACK_TOP), STACK(stacksize), TMP2, 0, SLJIT_IMM, 1);
9347 if (ket != OP_KET || bra != OP_BRA)
9350 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(stacksize), STR_PTR, 0);
9352 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(stacksize), SLJIT_IMM, 0);
9357 stacksize = match_capture_common(common, stacksize, offset, private_data_ptr);
9359 if (has_alternatives)
9361 if (opcode != OP_ONCE)
9362 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(stacksize), SLJIT_IMM, 0);
9363 if (ket != OP_KETRMAX)
9364 BACKTRACK_AS(bracket_backtrack)->alternative_matchingpath = LABEL();
9367 /* Must be after the matchingpath label. */
9368 if (offset != 0 && common->optimized_cbracket[offset >> 1] != 0)
9370 SLJIT_ASSERT(private_data_ptr == OVECTOR(offset + 0));
9371 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), OVECTOR(offset + 1), STR_PTR, 0);
9374 if (ket == OP_KETRMAX)
9376 if (repeat_type != 0)
9378 if (has_alternatives)
9379 BACKTRACK_AS(bracket_backtrack)->alternative_matchingpath = LABEL();
9380 OP2(SLJIT_SUB | SLJIT_SET_Z, SLJIT_MEM1(SLJIT_SP), repeat_ptr, SLJIT_MEM1(SLJIT_SP), repeat_ptr, SLJIT_IMM, 1);
9381 JUMPTO(SLJIT_NOT_ZERO, rmax_label);
9382 /* Drop STR_PTR for greedy plus quantifier. */
9383 if (opcode != OP_ONCE)
9384 free_stack(common, 1);
9386 else if (opcode == OP_ONCE || opcode >= OP_SBRA)
9388 if (has_alternatives)
9389 BACKTRACK_AS(bracket_backtrack)->alternative_matchingpath = LABEL();
9390 /* Checking zero-length iteration. */
9391 if (opcode != OP_ONCE)
9393 CMPTO(SLJIT_NOT_EQUAL, SLJIT_MEM1(SLJIT_SP), private_data_ptr, STR_PTR, 0, rmax_label);
9394 /* Drop STR_PTR for greedy plus quantifier. */
9395 if (bra != OP_BRAZERO)
9396 free_stack(common, 1);
9399 /* TMP2 must contain the starting STR_PTR. */
9400 CMPTO(SLJIT_NOT_EQUAL, TMP2, 0, STR_PTR, 0, rmax_label);
9403 JUMPTO(SLJIT_JUMP, rmax_label);
9404 BACKTRACK_AS(bracket_backtrack)->recursive_matchingpath = LABEL();
9407 if (repeat_type == OP_EXACT)
9409 count_match(common);
9410 OP2(SLJIT_SUB | SLJIT_SET_Z, SLJIT_MEM1(SLJIT_SP), repeat_ptr, SLJIT_MEM1(SLJIT_SP), repeat_ptr, SLJIT_IMM, 1);
9411 JUMPTO(SLJIT_NOT_ZERO, rmax_label);
9413 else if (repeat_type == OP_UPTO)
9415 /* We need to preserve the counter. */
9416 OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_SP), repeat_ptr);
9417 allocate_stack(common, 1);
9418 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), TMP2, 0);
9421 if (bra == OP_BRAZERO)
9422 BACKTRACK_AS(bracket_backtrack)->zero_matchingpath = LABEL();
9424 if (bra == OP_BRAMINZERO)
9426 /* This is a backtrack path! (From the viewpoint of OP_BRAMINZERO) */
9427 JUMPTO(SLJIT_JUMP, ((braminzero_backtrack *)parent)->matchingpath);
9428 if (braminzero != NULL)
9430 JUMPHERE(braminzero);
9431 /* We need to release the end pointer to perform the
9432 backtrack for the zero-length iteration. When
9433 framesize is < 0, OP_ONCE will do the release itself. */
9434 if (opcode == OP_ONCE && BACKTRACK_AS(bracket_backtrack)->u.framesize >= 0)
9436 OP1(SLJIT_MOV, STACK_TOP, 0, SLJIT_MEM1(SLJIT_SP), private_data_ptr);
9437 add_jump(compiler, &common->revertframes, JUMP(SLJIT_FAST_CALL));
9438 OP2(SLJIT_ADD, STACK_TOP, 0, STACK_TOP, 0, SLJIT_IMM, (BACKTRACK_AS(bracket_backtrack)->u.framesize - 1) * sizeof(sljit_sw));
9440 else if (ket == OP_KETRMIN && opcode != OP_ONCE)
9441 free_stack(common, 1);
9443 /* Continue to the normal backtrack. */
9446 if ((ket != OP_KET && bra != OP_BRAMINZERO) || bra == OP_BRAZERO)
9447 count_match(common);
9449 /* Skip the other alternatives. */
9450 while (*cc == OP_ALT)
9452 cc += 1 + LINK_SIZE;
9454 if (opcode == OP_ONCE)
9456 /* We temporarily encode the needs_control_head in the lowest bit.
9457 Note: on the target architectures of SLJIT the ((x << 1) >> 1) returns
9458 the same value for small signed numbers (including negative numbers). */
9459 BACKTRACK_AS(bracket_backtrack)->u.framesize = (BACKTRACK_AS(bracket_backtrack)->u.framesize << 1) | (needs_control_head ? 1 : 0);
9461 return cc + repeat_length;
9464 static PCRE2_SPTR compile_bracketpos_matchingpath(compiler_common *common, PCRE2_SPTR cc, backtrack_common *parent)
9467 backtrack_common *backtrack;
9469 int private_data_ptr;
9470 int cbraprivptr = 0;
9471 BOOL needs_control_head;
9476 PCRE2_SPTR ccbegin = NULL;
9477 int stack; /* Also contains the offset of control head. */
9478 struct sljit_label *loop = NULL;
9479 struct jump_list *emptymatch = NULL;
9481 PUSH_BACKTRACK(sizeof(bracketpos_backtrack), cc, NULL);
9482 if (*cc == OP_BRAPOSZERO)
9489 private_data_ptr = PRIVATE_DATA(cc);
9490 SLJIT_ASSERT(private_data_ptr != 0);
9491 BACKTRACK_AS(bracketpos_backtrack)->private_data_ptr = private_data_ptr;
9496 ccbegin = cc + 1 + LINK_SIZE;
9501 offset = GET2(cc, 1 + LINK_SIZE);
9502 /* This case cannot be optimized in the same was as
9503 normal capturing brackets. */
9504 SLJIT_ASSERT(common->optimized_cbracket[offset] == 0);
9505 cbraprivptr = OVECTOR_PRIV(offset);
9507 ccbegin = cc + 1 + LINK_SIZE + IMM2_SIZE;
9511 SLJIT_UNREACHABLE();
9515 framesize = get_framesize(common, cc, NULL, FALSE, &needs_control_head);
9516 BACKTRACK_AS(bracketpos_backtrack)->framesize = framesize;
9522 if (common->capture_last_ptr != 0)
9528 if (needs_control_head)
9533 BACKTRACK_AS(bracketpos_backtrack)->stacksize = stacksize;
9534 allocate_stack(common, stacksize);
9535 if (framesize == no_frame)
9536 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), private_data_ptr, STACK_TOP, 0);
9542 OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), OVECTOR(offset));
9543 OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_SP), OVECTOR(offset + 1));
9544 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), TMP1, 0);
9545 if (common->capture_last_ptr != 0)
9546 OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), common->capture_last_ptr);
9547 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(1), TMP2, 0);
9548 if (needs_control_head)
9549 OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_SP), common->control_head_ptr);
9550 if (common->capture_last_ptr != 0)
9552 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(2), TMP1, 0);
9558 if (needs_control_head)
9559 OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_SP), common->control_head_ptr);
9560 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), STR_PTR, 0);
9564 if (needs_control_head)
9567 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(stack), SLJIT_IMM, 1);
9568 if (needs_control_head)
9571 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(stack), TMP2, 0);
9576 stacksize = framesize + 1;
9579 if (needs_control_head)
9583 BACKTRACK_AS(bracketpos_backtrack)->stacksize = stacksize;
9585 allocate_stack(common, stacksize);
9586 OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), private_data_ptr);
9587 if (needs_control_head)
9588 OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_SP), common->control_head_ptr);
9589 OP2(SLJIT_ADD, SLJIT_MEM1(SLJIT_SP), private_data_ptr, STACK_TOP, 0, SLJIT_IMM, stacksize * sizeof(sljit_sw));
9594 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), SLJIT_IMM, 1);
9597 if (needs_control_head)
9599 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(stack), TMP2, 0);
9604 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(stack), STR_PTR, 0);
9607 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(stack), TMP1, 0);
9608 init_frame(common, cc, NULL, stacksize - 1, stacksize - framesize);
9609 stack -= 1 + (offset == 0);
9613 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), cbraprivptr, STR_PTR, 0);
9616 while (*cc != OP_KETRPOS)
9618 backtrack->top = NULL;
9619 backtrack->topbacktracks = NULL;
9622 compile_matchingpath(common, ccbegin, cc, backtrack);
9623 if (SLJIT_UNLIKELY(sljit_get_compiler_error(compiler)))
9628 if (framesize == no_frame)
9629 OP1(SLJIT_MOV, STACK_TOP, 0, SLJIT_MEM1(SLJIT_SP), private_data_ptr);
9633 OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), cbraprivptr);
9634 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), OVECTOR(offset + 1), STR_PTR, 0);
9635 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), cbraprivptr, STR_PTR, 0);
9636 if (common->capture_last_ptr != 0)
9637 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->capture_last_ptr, SLJIT_IMM, offset >> 1);
9638 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), OVECTOR(offset), TMP1, 0);
9642 if (opcode == OP_SBRAPOS)
9643 OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(STACK_TOP), STACK(0));
9644 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), STR_PTR, 0);
9647 /* Even if the match is empty, we need to reset the control head. */
9648 if (needs_control_head)
9649 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->control_head_ptr, SLJIT_MEM1(STACK_TOP), STACK(stack));
9651 if (opcode == OP_SBRAPOS || opcode == OP_SCBRAPOS)
9652 add_jump(compiler, &emptymatch, CMP(SLJIT_EQUAL, TMP1, 0, STR_PTR, 0));
9655 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(stacksize - 1), SLJIT_IMM, 0);
9661 OP2(SLJIT_SUB, STACK_TOP, 0, SLJIT_MEM1(SLJIT_SP), private_data_ptr, SLJIT_IMM, stacksize * sizeof(sljit_sw));
9662 OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), cbraprivptr);
9663 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), OVECTOR(offset + 1), STR_PTR, 0);
9664 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), cbraprivptr, STR_PTR, 0);
9665 if (common->capture_last_ptr != 0)
9666 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->capture_last_ptr, SLJIT_IMM, offset >> 1);
9667 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), OVECTOR(offset), TMP1, 0);
9671 OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_SP), private_data_ptr);
9672 OP2(SLJIT_SUB, STACK_TOP, 0, TMP2, 0, SLJIT_IMM, stacksize * sizeof(sljit_sw));
9673 if (opcode == OP_SBRAPOS)
9674 OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(TMP2), STACK(-framesize - 2));
9675 OP1(SLJIT_MOV, SLJIT_MEM1(TMP2), STACK(-framesize - 2), STR_PTR, 0);
9678 /* Even if the match is empty, we need to reset the control head. */
9679 if (needs_control_head)
9680 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->control_head_ptr, SLJIT_MEM1(STACK_TOP), STACK(stack));
9682 if (opcode == OP_SBRAPOS || opcode == OP_SCBRAPOS)
9683 add_jump(compiler, &emptymatch, CMP(SLJIT_EQUAL, TMP1, 0, STR_PTR, 0));
9688 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(stacksize - 1), SLJIT_IMM, 0);
9690 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), SLJIT_IMM, 0);
9694 JUMPTO(SLJIT_JUMP, loop);
9695 flush_stubs(common);
9697 compile_backtrackingpath(common, backtrack->top);
9698 if (SLJIT_UNLIKELY(sljit_get_compiler_error(compiler)))
9700 set_jumps(backtrack->topbacktracks, LABEL());
9705 OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(SLJIT_SP), cbraprivptr);
9707 OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(STACK_TOP), STACK(0));
9713 /* Last alternative. */
9714 if (*cc == OP_KETRPOS)
9715 OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_SP), private_data_ptr);
9716 OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(SLJIT_SP), cbraprivptr);
9720 OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_SP), private_data_ptr);
9721 OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(TMP2), STACK(-framesize - 2));
9725 if (*cc == OP_KETRPOS)
9727 ccbegin = cc + 1 + LINK_SIZE;
9730 /* We don't have to restore the control head in case of a failed match. */
9732 backtrack->topbacktracks = NULL;
9736 add_jump(compiler, &backtrack->topbacktracks, CMP(SLJIT_NOT_EQUAL, SLJIT_MEM1(STACK_TOP), STACK(stacksize - 1), SLJIT_IMM, 0));
9737 else /* TMP2 is set to [private_data_ptr] above. */
9738 add_jump(compiler, &backtrack->topbacktracks, CMP(SLJIT_NOT_EQUAL, SLJIT_MEM1(TMP2), STACK(-stacksize), SLJIT_IMM, 0));
9741 /* None of them matched. */
9742 set_jumps(emptymatch, LABEL());
9743 count_match(common);
9744 return cc + 1 + LINK_SIZE;
9747 static SLJIT_INLINE PCRE2_SPTR get_iterator_parameters(compiler_common *common, PCRE2_SPTR cc, PCRE2_UCHAR *opcode, PCRE2_UCHAR *type, sljit_u32 *max, sljit_u32 *exact, PCRE2_SPTR *end)
9754 if (*opcode >= OP_STAR && *opcode <= OP_POSUPTO)
9759 else if (*opcode >= OP_STARI && *opcode <= OP_POSUPTOI)
9763 *opcode -= OP_STARI - OP_STAR;
9765 else if (*opcode >= OP_NOTSTAR && *opcode <= OP_NOTPOSUPTO)
9769 *opcode -= OP_NOTSTAR - OP_STAR;
9771 else if (*opcode >= OP_NOTSTARI && *opcode <= OP_NOTPOSUPTOI)
9775 *opcode -= OP_NOTSTARI - OP_STAR;
9777 else if (*opcode >= OP_TYPESTAR && *opcode <= OP_TYPEPOSUPTO)
9780 *opcode -= OP_TYPESTAR - OP_STAR;
9785 SLJIT_ASSERT(*opcode == OP_CLASS || *opcode == OP_NCLASS || *opcode == OP_XCLASS);
9788 class_len = (*type < OP_XCLASS) ? (int)(1 + (32 / sizeof(PCRE2_UCHAR))) : GET(cc, 0);
9789 *opcode = cc[class_len - 1];
9791 if (*opcode >= OP_CRSTAR && *opcode <= OP_CRMINQUERY)
9793 *opcode -= OP_CRSTAR - OP_STAR;
9794 *end = cc + class_len;
9796 if (*opcode == OP_PLUS || *opcode == OP_MINPLUS)
9799 *opcode -= OP_PLUS - OP_STAR;
9802 else if (*opcode >= OP_CRPOSSTAR && *opcode <= OP_CRPOSQUERY)
9804 *opcode -= OP_CRPOSSTAR - OP_POSSTAR;
9805 *end = cc + class_len;
9807 if (*opcode == OP_POSPLUS)
9810 *opcode = OP_POSSTAR;
9815 SLJIT_ASSERT(*opcode == OP_CRRANGE || *opcode == OP_CRMINRANGE || *opcode == OP_CRPOSRANGE);
9816 *max = GET2(cc, (class_len + IMM2_SIZE));
9817 *exact = GET2(cc, class_len);
9821 if (*opcode == OP_CRPOSRANGE)
9822 *opcode = OP_POSSTAR;
9824 *opcode -= OP_CRRANGE - OP_STAR;
9833 if (*opcode == OP_CRPOSRANGE)
9834 *opcode = OP_POSQUERY;
9836 *opcode -= OP_CRRANGE - OP_QUERY;
9840 if (*opcode == OP_CRPOSRANGE)
9841 *opcode = OP_POSUPTO;
9843 *opcode -= OP_CRRANGE - OP_UPTO;
9846 *end = cc + class_len + 2 * IMM2_SIZE;
9854 *exact = GET2(cc, 0);
9861 *opcode -= OP_PLUS - OP_STAR;
9866 *opcode = OP_POSSTAR;
9877 if (*type == OP_END)
9880 *end = next_opcode(common, cc);
9886 #ifdef SUPPORT_UNICODE
9887 if (common->utf && HAS_EXTRALEN(*cc)) *end += GET_EXTRALEN(*cc);
9892 static PCRE2_SPTR compile_iterator_matchingpath(compiler_common *common, PCRE2_SPTR cc, backtrack_common *parent)
9895 backtrack_common *backtrack;
9898 sljit_u32 max = 0, exact;
9900 sljit_s32 fast_str_ptr;
9901 BOOL charpos_enabled;
9902 PCRE2_UCHAR charpos_char;
9903 unsigned int charpos_othercasebit;
9905 jump_list *no_match = NULL;
9906 jump_list *no_char1_match = NULL;
9907 struct sljit_jump *jump = NULL;
9908 struct sljit_label *label;
9909 int private_data_ptr = PRIVATE_DATA(cc);
9910 int base = (private_data_ptr == 0) ? SLJIT_MEM1(STACK_TOP) : SLJIT_MEM1(SLJIT_SP);
9911 int offset0 = (private_data_ptr == 0) ? STACK(0) : private_data_ptr;
9912 int offset1 = (private_data_ptr == 0) ? STACK(1) : private_data_ptr + (int)sizeof(sljit_sw);
9913 int tmp_base, tmp_offset;
9915 PUSH_BACKTRACK(sizeof(char_iterator_backtrack), cc, NULL);
9917 fast_str_ptr = PRIVATE_DATA(cc + 1);
9920 SLJIT_ASSERT(common->fast_forward_bc_ptr == NULL || fast_str_ptr == 0 || cc == common->fast_forward_bc_ptr);
9922 if (cc == common->fast_forward_bc_ptr)
9924 else if (common->fast_fail_start_ptr == 0)
9927 SLJIT_ASSERT(common->fast_forward_bc_ptr != NULL || fast_str_ptr == 0
9928 || (fast_str_ptr >= common->fast_fail_start_ptr && fast_str_ptr <= common->fast_fail_end_ptr));
9930 cc = get_iterator_parameters(common, cc, &opcode, &type, &max, &exact, &end);
9932 if (type != OP_EXTUNI)
9939 tmp_base = SLJIT_MEM1(SLJIT_SP);
9940 tmp_offset = POSSESSIVE0;
9943 if (fast_fail && fast_str_ptr != 0)
9944 add_jump(compiler, &backtrack->topbacktracks, CMP(SLJIT_LESS_EQUAL, STR_PTR, 0, SLJIT_MEM1(SLJIT_SP), fast_str_ptr));
9946 /* Handle fixed part first. */
9949 SLJIT_ASSERT(fast_str_ptr == 0);
9950 if (common->mode == PCRE2_JIT_COMPLETE
9951 #ifdef SUPPORT_UNICODE
9956 OP2(SLJIT_ADD, TMP1, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(exact));
9957 add_jump(compiler, &backtrack->topbacktracks, CMP(SLJIT_GREATER, TMP1, 0, STR_END, 0));
9958 OP1(SLJIT_MOV, tmp_base, tmp_offset, SLJIT_IMM, exact);
9960 compile_char1_matchingpath(common, type, cc, &backtrack->topbacktracks, FALSE);
9961 OP2(SLJIT_SUB | SLJIT_SET_Z, tmp_base, tmp_offset, tmp_base, tmp_offset, SLJIT_IMM, 1);
9962 JUMPTO(SLJIT_NOT_ZERO, label);
9966 OP1(SLJIT_MOV, tmp_base, tmp_offset, SLJIT_IMM, exact);
9968 compile_char1_matchingpath(common, type, cc, &backtrack->topbacktracks, TRUE);
9969 OP2(SLJIT_SUB | SLJIT_SET_Z, tmp_base, tmp_offset, tmp_base, tmp_offset, SLJIT_IMM, 1);
9970 JUMPTO(SLJIT_NOT_ZERO, label);
9973 else if (exact == 1)
9974 compile_char1_matchingpath(common, type, cc, &backtrack->topbacktracks, TRUE);
9980 SLJIT_ASSERT(fast_str_ptr == 0 || opcode == OP_STAR);
9982 if (type == OP_ANYNL || type == OP_EXTUNI)
9984 SLJIT_ASSERT(private_data_ptr == 0);
9985 SLJIT_ASSERT(fast_str_ptr == 0);
9987 allocate_stack(common, 2);
9988 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), STR_PTR, 0);
9989 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(1), SLJIT_IMM, 0);
9991 if (opcode == OP_UPTO)
9992 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), POSSESSIVE0, SLJIT_IMM, max);
9995 compile_char1_matchingpath(common, type, cc, &BACKTRACK_AS(char_iterator_backtrack)->u.backtracks, TRUE);
9996 if (opcode == OP_UPTO)
9998 OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), POSSESSIVE0);
9999 OP2(SLJIT_SUB | SLJIT_SET_Z, TMP1, 0, TMP1, 0, SLJIT_IMM, 1);
10000 jump = JUMP(SLJIT_ZERO);
10001 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), POSSESSIVE0, TMP1, 0);
10004 /* We cannot use TMP3 because of this allocate_stack. */
10005 allocate_stack(common, 1);
10006 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), STR_PTR, 0);
10007 JUMPTO(SLJIT_JUMP, label);
10013 charpos_enabled = FALSE;
10015 charpos_othercasebit = 0;
10017 if ((type != OP_CHAR && type != OP_CHARI) && (*end == OP_CHAR || *end == OP_CHARI))
10019 charpos_enabled = TRUE;
10020 #ifdef SUPPORT_UNICODE
10021 charpos_enabled = !common->utf || !HAS_EXTRALEN(end[1]);
10023 if (charpos_enabled && *end == OP_CHARI && char_has_othercase(common, end + 1))
10025 charpos_othercasebit = char_get_othercase_bit(common, end + 1);
10026 if (charpos_othercasebit == 0)
10027 charpos_enabled = FALSE;
10030 if (charpos_enabled)
10032 charpos_char = end[1];
10033 /* Consumpe the OP_CHAR opcode. */
10035 #if PCRE2_CODE_UNIT_WIDTH == 8
10036 SLJIT_ASSERT((charpos_othercasebit >> 8) == 0);
10037 #elif PCRE2_CODE_UNIT_WIDTH == 16 || PCRE2_CODE_UNIT_WIDTH == 32
10038 SLJIT_ASSERT((charpos_othercasebit >> 9) == 0);
10039 if ((charpos_othercasebit & 0x100) != 0)
10040 charpos_othercasebit = (charpos_othercasebit & 0xff) << 8;
10042 if (charpos_othercasebit != 0)
10043 charpos_char |= charpos_othercasebit;
10045 BACKTRACK_AS(char_iterator_backtrack)->u.charpos.enabled = TRUE;
10046 BACKTRACK_AS(char_iterator_backtrack)->u.charpos.chr = charpos_char;
10047 BACKTRACK_AS(char_iterator_backtrack)->u.charpos.othercasebit = charpos_othercasebit;
10051 if (charpos_enabled)
10053 if (opcode == OP_UPTO)
10054 OP1(SLJIT_MOV, tmp_base, tmp_offset, SLJIT_IMM, max + 1);
10056 /* Search the first instance of charpos_char. */
10057 jump = JUMP(SLJIT_JUMP);
10059 if (opcode == OP_UPTO)
10061 OP2(SLJIT_SUB | SLJIT_SET_Z, tmp_base, tmp_offset, tmp_base, tmp_offset, SLJIT_IMM, 1);
10062 add_jump(compiler, &backtrack->topbacktracks, JUMP(SLJIT_ZERO));
10064 compile_char1_matchingpath(common, type, cc, &backtrack->topbacktracks, FALSE);
10065 if (fast_str_ptr != 0)
10066 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), fast_str_ptr, STR_PTR, 0);
10069 detect_partial_match(common, &backtrack->topbacktracks);
10070 OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(0));
10071 if (charpos_othercasebit != 0)
10072 OP2(SLJIT_OR, TMP1, 0, TMP1, 0, SLJIT_IMM, charpos_othercasebit);
10073 CMPTO(SLJIT_NOT_EQUAL, TMP1, 0, SLJIT_IMM, charpos_char, label);
10075 if (private_data_ptr == 0)
10076 allocate_stack(common, 2);
10077 OP1(SLJIT_MOV, base, offset0, STR_PTR, 0);
10078 OP1(SLJIT_MOV, base, offset1, STR_PTR, 0);
10079 if (opcode == OP_UPTO)
10081 OP2(SLJIT_SUB | SLJIT_SET_Z, tmp_base, tmp_offset, tmp_base, tmp_offset, SLJIT_IMM, 1);
10082 add_jump(compiler, &no_match, JUMP(SLJIT_ZERO));
10085 /* Search the last instance of charpos_char. */
10087 compile_char1_matchingpath(common, type, cc, &no_match, FALSE);
10088 if (fast_str_ptr != 0)
10089 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), fast_str_ptr, STR_PTR, 0);
10090 detect_partial_match(common, &no_match);
10091 OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(0));
10092 if (charpos_othercasebit != 0)
10093 OP2(SLJIT_OR, TMP1, 0, TMP1, 0, SLJIT_IMM, charpos_othercasebit);
10094 if (opcode == OP_STAR)
10096 CMPTO(SLJIT_NOT_EQUAL, TMP1, 0, SLJIT_IMM, charpos_char, label);
10097 OP1(SLJIT_MOV, base, offset0, STR_PTR, 0);
10101 jump = CMP(SLJIT_NOT_EQUAL, TMP1, 0, SLJIT_IMM, charpos_char);
10102 OP1(SLJIT_MOV, base, offset0, STR_PTR, 0);
10106 if (opcode == OP_UPTO)
10108 OP2(SLJIT_SUB | SLJIT_SET_Z, tmp_base, tmp_offset, tmp_base, tmp_offset, SLJIT_IMM, 1);
10109 JUMPTO(SLJIT_NOT_ZERO, label);
10112 JUMPTO(SLJIT_JUMP, label);
10114 set_jumps(no_match, LABEL());
10115 OP1(SLJIT_MOV, STR_PTR, 0, base, offset0);
10116 OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
10117 OP1(SLJIT_MOV, base, offset0, STR_PTR, 0);
10119 #if defined SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH != 32
10120 else if (common->utf)
10122 if (private_data_ptr == 0)
10123 allocate_stack(common, 2);
10125 OP1(SLJIT_MOV, base, offset0, STR_PTR, 0);
10126 OP1(SLJIT_MOV, base, offset1, STR_PTR, 0);
10128 if (opcode == OP_UPTO)
10129 OP1(SLJIT_MOV, tmp_base, tmp_offset, SLJIT_IMM, max);
10132 compile_char1_matchingpath(common, type, cc, &no_match, TRUE);
10133 OP1(SLJIT_MOV, base, offset0, STR_PTR, 0);
10135 if (opcode == OP_UPTO)
10137 OP2(SLJIT_SUB | SLJIT_SET_Z, tmp_base, tmp_offset, tmp_base, tmp_offset, SLJIT_IMM, 1);
10138 JUMPTO(SLJIT_NOT_ZERO, label);
10141 JUMPTO(SLJIT_JUMP, label);
10143 set_jumps(no_match, LABEL());
10144 OP1(SLJIT_MOV, STR_PTR, 0, base, offset0);
10145 if (fast_str_ptr != 0)
10146 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), fast_str_ptr, STR_PTR, 0);
10151 if (private_data_ptr == 0)
10152 allocate_stack(common, 2);
10154 OP1(SLJIT_MOV, base, offset1, STR_PTR, 0);
10155 if (opcode == OP_UPTO)
10156 OP1(SLJIT_MOV, tmp_base, tmp_offset, SLJIT_IMM, max);
10159 detect_partial_match(common, &no_match);
10160 compile_char1_matchingpath(common, type, cc, &no_char1_match, FALSE);
10161 if (opcode == OP_UPTO)
10163 OP2(SLJIT_SUB | SLJIT_SET_Z, tmp_base, tmp_offset, tmp_base, tmp_offset, SLJIT_IMM, 1);
10164 JUMPTO(SLJIT_NOT_ZERO, label);
10165 OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
10168 JUMPTO(SLJIT_JUMP, label);
10170 set_jumps(no_char1_match, LABEL());
10171 OP2(SLJIT_SUB, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
10172 set_jumps(no_match, LABEL());
10173 OP1(SLJIT_MOV, base, offset0, STR_PTR, 0);
10174 if (fast_str_ptr != 0)
10175 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), fast_str_ptr, STR_PTR, 0);
10178 BACKTRACK_AS(char_iterator_backtrack)->matchingpath = LABEL();
10182 if (private_data_ptr == 0)
10183 allocate_stack(common, 1);
10184 OP1(SLJIT_MOV, base, offset0, STR_PTR, 0);
10185 BACKTRACK_AS(char_iterator_backtrack)->matchingpath = LABEL();
10186 if (fast_str_ptr != 0)
10187 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), fast_str_ptr, STR_PTR, 0);
10191 SLJIT_ASSERT(fast_str_ptr == 0);
10192 if (private_data_ptr == 0)
10193 allocate_stack(common, 2);
10194 OP1(SLJIT_MOV, base, offset0, STR_PTR, 0);
10195 OP1(SLJIT_MOV, base, offset1, SLJIT_IMM, max + 1);
10196 BACKTRACK_AS(char_iterator_backtrack)->matchingpath = LABEL();
10201 SLJIT_ASSERT(fast_str_ptr == 0);
10202 if (private_data_ptr == 0)
10203 allocate_stack(common, 1);
10204 OP1(SLJIT_MOV, base, offset0, STR_PTR, 0);
10205 if (opcode == OP_QUERY)
10206 compile_char1_matchingpath(common, type, cc, &BACKTRACK_AS(char_iterator_backtrack)->u.backtracks, TRUE);
10207 BACKTRACK_AS(char_iterator_backtrack)->matchingpath = LABEL();
10214 #if defined SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH != 32
10217 OP1(SLJIT_MOV, tmp_base, tmp_offset, STR_PTR, 0);
10219 compile_char1_matchingpath(common, type, cc, &no_match, TRUE);
10220 OP1(SLJIT_MOV, tmp_base, tmp_offset, STR_PTR, 0);
10221 JUMPTO(SLJIT_JUMP, label);
10222 set_jumps(no_match, LABEL());
10223 OP1(SLJIT_MOV, STR_PTR, 0, tmp_base, tmp_offset);
10224 if (fast_str_ptr != 0)
10225 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), fast_str_ptr, STR_PTR, 0);
10230 detect_partial_match(common, &no_match);
10231 compile_char1_matchingpath(common, type, cc, &no_char1_match, FALSE);
10232 JUMPTO(SLJIT_JUMP, label);
10233 set_jumps(no_char1_match, LABEL());
10234 OP2(SLJIT_SUB, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
10235 set_jumps(no_match, LABEL());
10236 if (fast_str_ptr != 0)
10237 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), fast_str_ptr, STR_PTR, 0);
10241 SLJIT_ASSERT(fast_str_ptr == 0);
10242 #if defined SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH != 32
10245 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), POSSESSIVE1, STR_PTR, 0);
10246 OP1(SLJIT_MOV, tmp_base, tmp_offset, SLJIT_IMM, max);
10248 compile_char1_matchingpath(common, type, cc, &no_match, TRUE);
10249 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), POSSESSIVE1, STR_PTR, 0);
10250 OP2(SLJIT_SUB | SLJIT_SET_Z, tmp_base, tmp_offset, tmp_base, tmp_offset, SLJIT_IMM, 1);
10251 JUMPTO(SLJIT_NOT_ZERO, label);
10252 set_jumps(no_match, LABEL());
10253 OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(SLJIT_SP), POSSESSIVE1);
10257 OP1(SLJIT_MOV, tmp_base, tmp_offset, SLJIT_IMM, max);
10259 detect_partial_match(common, &no_match);
10260 compile_char1_matchingpath(common, type, cc, &no_char1_match, FALSE);
10261 OP2(SLJIT_SUB | SLJIT_SET_Z, tmp_base, tmp_offset, tmp_base, tmp_offset, SLJIT_IMM, 1);
10262 JUMPTO(SLJIT_NOT_ZERO, label);
10263 OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
10264 set_jumps(no_char1_match, LABEL());
10265 OP2(SLJIT_SUB, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
10266 set_jumps(no_match, LABEL());
10270 SLJIT_ASSERT(fast_str_ptr == 0);
10271 OP1(SLJIT_MOV, tmp_base, tmp_offset, STR_PTR, 0);
10272 compile_char1_matchingpath(common, type, cc, &no_match, TRUE);
10273 OP1(SLJIT_MOV, tmp_base, tmp_offset, STR_PTR, 0);
10274 set_jumps(no_match, LABEL());
10275 OP1(SLJIT_MOV, STR_PTR, 0, tmp_base, tmp_offset);
10279 SLJIT_UNREACHABLE();
10283 count_match(common);
10287 static SLJIT_INLINE PCRE2_SPTR compile_fail_accept_matchingpath(compiler_common *common, PCRE2_SPTR cc, backtrack_common *parent)
10290 backtrack_common *backtrack;
10292 PUSH_BACKTRACK(sizeof(backtrack_common), cc, NULL);
10294 if (*cc == OP_FAIL)
10296 add_jump(compiler, &backtrack->topbacktracks, JUMP(SLJIT_JUMP));
10300 if (*cc == OP_ACCEPT && common->currententry == NULL && (common->re->overall_options & PCRE2_ENDANCHORED) != 0)
10301 add_jump(compiler, &common->reset_match, CMP(SLJIT_NOT_EQUAL, STR_PTR, 0, STR_END, 0));
10303 if (*cc == OP_ASSERT_ACCEPT || common->currententry != NULL || !common->might_be_empty)
10305 /* No need to check notempty conditions. */
10306 if (common->accept_label == NULL)
10307 add_jump(compiler, &common->accept, JUMP(SLJIT_JUMP));
10309 JUMPTO(SLJIT_JUMP, common->accept_label);
10313 if (common->accept_label == NULL)
10314 add_jump(compiler, &common->accept, CMP(SLJIT_NOT_EQUAL, STR_PTR, 0, SLJIT_MEM1(SLJIT_SP), OVECTOR(0)));
10316 CMPTO(SLJIT_NOT_EQUAL, STR_PTR, 0, SLJIT_MEM1(SLJIT_SP), OVECTOR(0), common->accept_label);
10317 OP1(SLJIT_MOV, TMP1, 0, ARGUMENTS, 0);
10318 OP1(SLJIT_MOV_U32, TMP2, 0, SLJIT_MEM1(TMP1), SLJIT_OFFSETOF(jit_arguments, options));
10319 OP2(SLJIT_AND | SLJIT_SET_Z, SLJIT_UNUSED, 0, TMP2, 0, SLJIT_IMM, PCRE2_NOTEMPTY);
10320 add_jump(compiler, &backtrack->topbacktracks, JUMP(SLJIT_NOT_ZERO));
10321 OP2(SLJIT_AND | SLJIT_SET_Z, SLJIT_UNUSED, 0, TMP2, 0, SLJIT_IMM, PCRE2_NOTEMPTY_ATSTART);
10322 if (common->accept_label == NULL)
10323 add_jump(compiler, &common->accept, JUMP(SLJIT_ZERO));
10325 JUMPTO(SLJIT_ZERO, common->accept_label);
10326 OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(TMP1), SLJIT_OFFSETOF(jit_arguments, str));
10327 if (common->accept_label == NULL)
10328 add_jump(compiler, &common->accept, CMP(SLJIT_NOT_EQUAL, TMP2, 0, STR_PTR, 0));
10330 CMPTO(SLJIT_NOT_EQUAL, TMP2, 0, STR_PTR, 0, common->accept_label);
10331 add_jump(compiler, &backtrack->topbacktracks, JUMP(SLJIT_JUMP));
10335 static SLJIT_INLINE PCRE2_SPTR compile_close_matchingpath(compiler_common *common, PCRE2_SPTR cc)
10338 int offset = GET2(cc, 1);
10339 BOOL optimized_cbracket = common->optimized_cbracket[offset] != 0;
10341 /* Data will be discarded anyway... */
10342 if (common->currententry != NULL)
10343 return cc + 1 + IMM2_SIZE;
10345 if (!optimized_cbracket)
10346 OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), OVECTOR_PRIV(offset));
10348 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), OVECTOR(offset + 1), STR_PTR, 0);
10349 if (!optimized_cbracket)
10350 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), OVECTOR(offset), TMP1, 0);
10351 return cc + 1 + IMM2_SIZE;
10354 static SLJIT_INLINE PCRE2_SPTR compile_control_verb_matchingpath(compiler_common *common, PCRE2_SPTR cc, backtrack_common *parent)
10357 backtrack_common *backtrack;
10358 PCRE2_UCHAR opcode = *cc;
10359 PCRE2_SPTR ccend = cc + 1;
10361 if (opcode == OP_COMMIT_ARG || opcode == OP_PRUNE_ARG ||
10362 opcode == OP_SKIP_ARG || opcode == OP_THEN_ARG)
10363 ccend += 2 + cc[1];
10365 PUSH_BACKTRACK(sizeof(backtrack_common), cc, NULL);
10367 if (opcode == OP_SKIP)
10369 allocate_stack(common, 1);
10370 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), STR_PTR, 0);
10374 if (opcode == OP_COMMIT_ARG || opcode == OP_PRUNE_ARG || opcode == OP_THEN_ARG)
10376 OP1(SLJIT_MOV, TMP1, 0, ARGUMENTS, 0);
10377 OP1(SLJIT_MOV, TMP2, 0, SLJIT_IMM, (sljit_sw)(cc + 2));
10378 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->mark_ptr, TMP2, 0);
10379 OP1(SLJIT_MOV, SLJIT_MEM1(TMP1), SLJIT_OFFSETOF(jit_arguments, mark_ptr), TMP2, 0);
10385 static PCRE2_UCHAR then_trap_opcode[1] = { OP_THEN_TRAP };
10387 static SLJIT_INLINE void compile_then_trap_matchingpath(compiler_common *common, PCRE2_SPTR cc, PCRE2_SPTR ccend, backtrack_common *parent)
10390 backtrack_common *backtrack;
10391 BOOL needs_control_head;
10394 PUSH_BACKTRACK_NOVALUE(sizeof(then_trap_backtrack), cc);
10395 common->then_trap = BACKTRACK_AS(then_trap_backtrack);
10396 BACKTRACK_AS(then_trap_backtrack)->common.cc = then_trap_opcode;
10397 BACKTRACK_AS(then_trap_backtrack)->start = (sljit_sw)(cc - common->start);
10398 BACKTRACK_AS(then_trap_backtrack)->framesize = get_framesize(common, cc, ccend, FALSE, &needs_control_head);
10400 size = BACKTRACK_AS(then_trap_backtrack)->framesize;
10401 size = 3 + (size < 0 ? 0 : size);
10403 OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_SP), common->control_head_ptr);
10404 allocate_stack(common, size);
10406 OP2(SLJIT_ADD, SLJIT_MEM1(SLJIT_SP), common->control_head_ptr, STACK_TOP, 0, SLJIT_IMM, (size - 3) * sizeof(sljit_sw));
10408 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->control_head_ptr, STACK_TOP, 0);
10409 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(size - 1), SLJIT_IMM, BACKTRACK_AS(then_trap_backtrack)->start);
10410 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(size - 2), SLJIT_IMM, type_then_trap);
10411 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(size - 3), TMP2, 0);
10413 size = BACKTRACK_AS(then_trap_backtrack)->framesize;
10415 init_frame(common, cc, ccend, size - 1, 0);
10418 static void compile_matchingpath(compiler_common *common, PCRE2_SPTR cc, PCRE2_SPTR ccend, backtrack_common *parent)
10421 backtrack_common *backtrack;
10422 BOOL has_then_trap = FALSE;
10423 then_trap_backtrack *save_then_trap = NULL;
10425 SLJIT_ASSERT(*ccend == OP_END || (*ccend >= OP_ALT && *ccend <= OP_KETRPOS));
10427 if (common->has_then && common->then_offsets[cc - common->start] != 0)
10429 SLJIT_ASSERT(*ccend != OP_END && common->control_head_ptr != 0);
10430 has_then_trap = TRUE;
10431 save_then_trap = common->then_trap;
10432 /* Tail item on backtrack. */
10433 compile_then_trap_matchingpath(common, cc, ccend, parent);
10442 case OP_NOT_WORD_BOUNDARY:
10443 case OP_WORD_BOUNDARY:
10451 cc = compile_simple_assertion_matchingpath(common, *cc, cc + 1, parent->top != NULL ? &parent->top->nextbacktracks : &parent->topbacktracks);
10456 case OP_NOT_WHITESPACE:
10457 case OP_WHITESPACE:
10458 case OP_NOT_WORDCHAR:
10466 case OP_NOT_HSPACE:
10468 case OP_NOT_VSPACE:
10473 cc = compile_char1_matchingpath(common, *cc, cc + 1, parent->top != NULL ? &parent->top->nextbacktracks : &parent->topbacktracks, TRUE);
10477 PUSH_BACKTRACK_NOVALUE(sizeof(backtrack_common), cc);
10478 OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_SP), OVECTOR(0));
10479 allocate_stack(common, 1);
10480 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), OVECTOR(0), STR_PTR, 0);
10481 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), TMP2, 0);
10487 if (common->mode == PCRE2_JIT_COMPLETE)
10488 cc = compile_charn_matchingpath(common, cc, ccend, parent->top != NULL ? &parent->top->nextbacktracks : &parent->topbacktracks);
10490 cc = compile_char1_matchingpath(common, *cc, cc + 1, parent->top != NULL ? &parent->top->nextbacktracks : &parent->topbacktracks, TRUE);
10520 case OP_NOTMINSTAR:
10522 case OP_NOTMINPLUS:
10524 case OP_NOTMINQUERY:
10526 case OP_NOTMINUPTO:
10528 case OP_NOTPOSSTAR:
10529 case OP_NOTPOSPLUS:
10530 case OP_NOTPOSQUERY:
10531 case OP_NOTPOSUPTO:
10533 case OP_NOTMINSTARI:
10535 case OP_NOTMINPLUSI:
10537 case OP_NOTMINQUERYI:
10539 case OP_NOTMINUPTOI:
10541 case OP_NOTPOSSTARI:
10542 case OP_NOTPOSPLUSI:
10543 case OP_NOTPOSQUERYI:
10544 case OP_NOTPOSUPTOI:
10546 case OP_TYPEMINSTAR:
10548 case OP_TYPEMINPLUS:
10550 case OP_TYPEMINQUERY:
10552 case OP_TYPEMINUPTO:
10554 case OP_TYPEPOSSTAR:
10555 case OP_TYPEPOSPLUS:
10556 case OP_TYPEPOSQUERY:
10557 case OP_TYPEPOSUPTO:
10558 cc = compile_iterator_matchingpath(common, cc, parent);
10563 if (cc[1 + (32 / sizeof(PCRE2_UCHAR))] >= OP_CRSTAR && cc[1 + (32 / sizeof(PCRE2_UCHAR))] <= OP_CRPOSRANGE)
10564 cc = compile_iterator_matchingpath(common, cc, parent);
10566 cc = compile_char1_matchingpath(common, *cc, cc + 1, parent->top != NULL ? &parent->top->nextbacktracks : &parent->topbacktracks, TRUE);
10569 #if defined SUPPORT_UNICODE || PCRE2_CODE_UNIT_WIDTH == 16 || PCRE2_CODE_UNIT_WIDTH == 32
10571 if (*(cc + GET(cc, 1)) >= OP_CRSTAR && *(cc + GET(cc, 1)) <= OP_CRPOSRANGE)
10572 cc = compile_iterator_matchingpath(common, cc, parent);
10574 cc = compile_char1_matchingpath(common, *cc, cc + 1, parent->top != NULL ? &parent->top->nextbacktracks : &parent->topbacktracks, TRUE);
10580 if (cc[1 + IMM2_SIZE] >= OP_CRSTAR && cc[1 + IMM2_SIZE] <= OP_CRPOSRANGE)
10581 cc = compile_ref_iterator_matchingpath(common, cc, parent);
10584 compile_ref_matchingpath(common, cc, parent->top != NULL ? &parent->top->nextbacktracks : &parent->topbacktracks, TRUE, FALSE);
10585 cc += 1 + IMM2_SIZE;
10591 if (cc[1 + 2 * IMM2_SIZE] >= OP_CRSTAR && cc[1 + 2 * IMM2_SIZE] <= OP_CRPOSRANGE)
10592 cc = compile_ref_iterator_matchingpath(common, cc, parent);
10595 compile_dnref_search(common, cc, parent->top != NULL ? &parent->top->nextbacktracks : &parent->topbacktracks);
10596 compile_ref_matchingpath(common, cc, parent->top != NULL ? &parent->top->nextbacktracks : &parent->topbacktracks, TRUE, FALSE);
10597 cc += 1 + 2 * IMM2_SIZE;
10602 cc = compile_recurse_matchingpath(common, cc, parent);
10606 case OP_CALLOUT_STR:
10607 cc = compile_callout_matchingpath(common, cc, parent);
10611 case OP_ASSERT_NOT:
10612 case OP_ASSERTBACK:
10613 case OP_ASSERTBACK_NOT:
10614 PUSH_BACKTRACK_NOVALUE(sizeof(assert_backtrack), cc);
10615 cc = compile_assert_matchingpath(common, cc, BACKTRACK_AS(assert_backtrack), FALSE);
10618 case OP_BRAMINZERO:
10619 PUSH_BACKTRACK_NOVALUE(sizeof(braminzero_backtrack), cc);
10620 cc = bracketend(cc + 1);
10621 if (*(cc - 1 - LINK_SIZE) != OP_KETRMIN)
10623 allocate_stack(common, 1);
10624 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), STR_PTR, 0);
10628 allocate_stack(common, 2);
10629 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), SLJIT_IMM, 0);
10630 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(1), STR_PTR, 0);
10632 BACKTRACK_AS(braminzero_backtrack)->matchingpath = LABEL();
10633 count_match(common);
10643 cc = compile_bracket_matchingpath(common, cc, parent);
10647 if (cc[1] > OP_ASSERTBACK_NOT)
10648 cc = compile_bracket_matchingpath(common, cc, parent);
10651 PUSH_BACKTRACK_NOVALUE(sizeof(assert_backtrack), cc);
10652 cc = compile_assert_matchingpath(common, cc, BACKTRACK_AS(assert_backtrack), FALSE);
10660 case OP_BRAPOSZERO:
10661 cc = compile_bracketpos_matchingpath(common, cc, parent);
10665 PUSH_BACKTRACK_NOVALUE(sizeof(backtrack_common), cc);
10666 SLJIT_ASSERT(common->mark_ptr != 0);
10667 OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_SP), common->mark_ptr);
10668 allocate_stack(common, common->has_skip_arg ? 5 : 1);
10669 OP1(SLJIT_MOV, TMP1, 0, ARGUMENTS, 0);
10670 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(common->has_skip_arg ? 4 : 0), TMP2, 0);
10671 OP1(SLJIT_MOV, TMP2, 0, SLJIT_IMM, (sljit_sw)(cc + 2));
10672 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->mark_ptr, TMP2, 0);
10673 OP1(SLJIT_MOV, SLJIT_MEM1(TMP1), SLJIT_OFFSETOF(jit_arguments, mark_ptr), TMP2, 0);
10674 if (common->has_skip_arg)
10676 OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), common->control_head_ptr);
10677 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->control_head_ptr, STACK_TOP, 0);
10678 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(1), SLJIT_IMM, type_mark);
10679 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(2), SLJIT_IMM, (sljit_sw)(cc + 2));
10680 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(3), STR_PTR, 0);
10681 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), TMP1, 0);
10683 cc += 1 + 2 + cc[1];
10693 case OP_COMMIT_ARG:
10694 cc = compile_control_verb_matchingpath(common, cc, parent);
10699 case OP_ASSERT_ACCEPT:
10700 cc = compile_fail_accept_matchingpath(common, cc, parent);
10704 cc = compile_close_matchingpath(common, cc);
10708 cc = bracketend(cc + 1);
10712 SLJIT_UNREACHABLE();
10721 /* Head item on backtrack. */
10722 PUSH_BACKTRACK_NOVALUE(sizeof(then_trap_backtrack), cc);
10723 BACKTRACK_AS(then_trap_backtrack)->common.cc = then_trap_opcode;
10724 BACKTRACK_AS(then_trap_backtrack)->then_trap = common->then_trap;
10725 common->then_trap = save_then_trap;
10727 SLJIT_ASSERT(cc == ccend);
10730 #undef PUSH_BACKTRACK
10731 #undef PUSH_BACKTRACK_NOVALUE
10732 #undef BACKTRACK_AS
10734 #define COMPILE_BACKTRACKINGPATH(current) \
10737 compile_backtrackingpath(common, (current)); \
10738 if (SLJIT_UNLIKELY(sljit_get_compiler_error(compiler))) \
10743 #define CURRENT_AS(type) ((type *)current)
10745 static void compile_iterator_backtrackingpath(compiler_common *common, struct backtrack_common *current)
10748 PCRE2_SPTR cc = current->cc;
10749 PCRE2_UCHAR opcode;
10751 sljit_u32 max = 0, exact;
10752 struct sljit_label *label = NULL;
10753 struct sljit_jump *jump = NULL;
10754 jump_list *jumplist = NULL;
10756 int private_data_ptr = PRIVATE_DATA(cc);
10757 int base = (private_data_ptr == 0) ? SLJIT_MEM1(STACK_TOP) : SLJIT_MEM1(SLJIT_SP);
10758 int offset0 = (private_data_ptr == 0) ? STACK(0) : private_data_ptr;
10759 int offset1 = (private_data_ptr == 0) ? STACK(1) : private_data_ptr + (int)sizeof(sljit_sw);
10761 cc = get_iterator_parameters(common, cc, &opcode, &type, &max, &exact, &end);
10767 if (type == OP_ANYNL || type == OP_EXTUNI)
10769 SLJIT_ASSERT(private_data_ptr == 0);
10770 set_jumps(CURRENT_AS(char_iterator_backtrack)->u.backtracks, LABEL());
10771 OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(STACK_TOP), STACK(0));
10772 free_stack(common, 1);
10773 CMPTO(SLJIT_NOT_EQUAL, STR_PTR, 0, SLJIT_IMM, 0, CURRENT_AS(char_iterator_backtrack)->matchingpath);
10777 if (CURRENT_AS(char_iterator_backtrack)->u.charpos.enabled)
10779 OP1(SLJIT_MOV, STR_PTR, 0, base, offset0);
10780 OP1(SLJIT_MOV, TMP2, 0, base, offset1);
10781 OP2(SLJIT_SUB, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
10783 jump = CMP(SLJIT_LESS_EQUAL, STR_PTR, 0, TMP2, 0);
10785 OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(-1));
10786 OP1(SLJIT_MOV, base, offset0, STR_PTR, 0);
10787 if (CURRENT_AS(char_iterator_backtrack)->u.charpos.othercasebit != 0)
10788 OP2(SLJIT_OR, TMP1, 0, TMP1, 0, SLJIT_IMM, CURRENT_AS(char_iterator_backtrack)->u.charpos.othercasebit);
10789 CMPTO(SLJIT_EQUAL, TMP1, 0, SLJIT_IMM, CURRENT_AS(char_iterator_backtrack)->u.charpos.chr, CURRENT_AS(char_iterator_backtrack)->matchingpath);
10790 skip_char_back(common);
10791 CMPTO(SLJIT_GREATER, STR_PTR, 0, TMP2, 0, label);
10795 OP1(SLJIT_MOV, STR_PTR, 0, base, offset0);
10796 jump = CMP(SLJIT_LESS_EQUAL, STR_PTR, 0, base, offset1);
10797 skip_char_back(common);
10798 OP1(SLJIT_MOV, base, offset0, STR_PTR, 0);
10799 JUMPTO(SLJIT_JUMP, CURRENT_AS(char_iterator_backtrack)->matchingpath);
10802 if (private_data_ptr == 0)
10803 free_stack(common, 2);
10808 OP1(SLJIT_MOV, STR_PTR, 0, base, offset0);
10809 compile_char1_matchingpath(common, type, cc, &jumplist, TRUE);
10810 OP1(SLJIT_MOV, base, offset0, STR_PTR, 0);
10811 JUMPTO(SLJIT_JUMP, CURRENT_AS(char_iterator_backtrack)->matchingpath);
10812 set_jumps(jumplist, LABEL());
10813 if (private_data_ptr == 0)
10814 free_stack(common, 1);
10818 OP1(SLJIT_MOV, TMP1, 0, base, offset1);
10819 OP1(SLJIT_MOV, STR_PTR, 0, base, offset0);
10820 OP2(SLJIT_SUB | SLJIT_SET_Z, TMP1, 0, TMP1, 0, SLJIT_IMM, 1);
10821 add_jump(compiler, &jumplist, JUMP(SLJIT_ZERO));
10823 OP1(SLJIT_MOV, base, offset1, TMP1, 0);
10824 compile_char1_matchingpath(common, type, cc, &jumplist, TRUE);
10825 OP1(SLJIT_MOV, base, offset0, STR_PTR, 0);
10826 JUMPTO(SLJIT_JUMP, CURRENT_AS(char_iterator_backtrack)->matchingpath);
10828 set_jumps(jumplist, LABEL());
10829 if (private_data_ptr == 0)
10830 free_stack(common, 2);
10834 OP1(SLJIT_MOV, STR_PTR, 0, base, offset0);
10835 OP1(SLJIT_MOV, base, offset0, SLJIT_IMM, 0);
10836 CMPTO(SLJIT_NOT_EQUAL, STR_PTR, 0, SLJIT_IMM, 0, CURRENT_AS(char_iterator_backtrack)->matchingpath);
10837 jump = JUMP(SLJIT_JUMP);
10838 set_jumps(CURRENT_AS(char_iterator_backtrack)->u.backtracks, LABEL());
10839 OP1(SLJIT_MOV, STR_PTR, 0, base, offset0);
10840 OP1(SLJIT_MOV, base, offset0, SLJIT_IMM, 0);
10841 JUMPTO(SLJIT_JUMP, CURRENT_AS(char_iterator_backtrack)->matchingpath);
10843 if (private_data_ptr == 0)
10844 free_stack(common, 1);
10848 OP1(SLJIT_MOV, STR_PTR, 0, base, offset0);
10849 OP1(SLJIT_MOV, base, offset0, SLJIT_IMM, 0);
10850 jump = CMP(SLJIT_EQUAL, STR_PTR, 0, SLJIT_IMM, 0);
10851 compile_char1_matchingpath(common, type, cc, &jumplist, TRUE);
10852 JUMPTO(SLJIT_JUMP, CURRENT_AS(char_iterator_backtrack)->matchingpath);
10853 set_jumps(jumplist, LABEL());
10855 if (private_data_ptr == 0)
10856 free_stack(common, 1);
10866 SLJIT_UNREACHABLE();
10870 set_jumps(current->topbacktracks, LABEL());
10873 static SLJIT_INLINE void compile_ref_iterator_backtrackingpath(compiler_common *common, struct backtrack_common *current)
10876 PCRE2_SPTR cc = current->cc;
10877 BOOL ref = (*cc == OP_REF || *cc == OP_REFI);
10880 type = cc[ref ? 1 + IMM2_SIZE : 1 + 2 * IMM2_SIZE];
10882 if ((type & 0x1) == 0)
10884 /* Maximize case. */
10885 set_jumps(current->topbacktracks, LABEL());
10886 OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(STACK_TOP), STACK(0));
10887 free_stack(common, 1);
10888 CMPTO(SLJIT_NOT_EQUAL, STR_PTR, 0, SLJIT_IMM, 0, CURRENT_AS(ref_iterator_backtrack)->matchingpath);
10892 OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(STACK_TOP), STACK(0));
10893 CMPTO(SLJIT_NOT_EQUAL, STR_PTR, 0, SLJIT_IMM, 0, CURRENT_AS(ref_iterator_backtrack)->matchingpath);
10894 set_jumps(current->topbacktracks, LABEL());
10895 free_stack(common, ref ? 2 : 3);
10898 static SLJIT_INLINE void compile_recurse_backtrackingpath(compiler_common *common, struct backtrack_common *current)
10901 recurse_entry *entry;
10903 if (!CURRENT_AS(recurse_backtrack)->inlined_pattern)
10905 entry = CURRENT_AS(recurse_backtrack)->entry;
10906 if (entry->backtrack_label == NULL)
10907 add_jump(compiler, &entry->backtrack_calls, JUMP(SLJIT_FAST_CALL));
10909 JUMPTO(SLJIT_FAST_CALL, entry->backtrack_label);
10910 CMPTO(SLJIT_NOT_EQUAL, TMP1, 0, SLJIT_IMM, 0, CURRENT_AS(recurse_backtrack)->matchingpath);
10913 compile_backtrackingpath(common, current->top);
10915 set_jumps(current->topbacktracks, LABEL());
10918 static void compile_assert_backtrackingpath(compiler_common *common, struct backtrack_common *current)
10921 PCRE2_SPTR cc = current->cc;
10922 PCRE2_UCHAR bra = OP_BRA;
10923 struct sljit_jump *brajump = NULL;
10925 SLJIT_ASSERT(*cc != OP_BRAMINZERO);
10926 if (*cc == OP_BRAZERO)
10932 if (bra == OP_BRAZERO)
10934 SLJIT_ASSERT(current->topbacktracks == NULL);
10935 OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(STACK_TOP), STACK(0));
10938 if (CURRENT_AS(assert_backtrack)->framesize < 0)
10940 set_jumps(current->topbacktracks, LABEL());
10942 if (bra == OP_BRAZERO)
10944 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), SLJIT_IMM, 0);
10945 CMPTO(SLJIT_NOT_EQUAL, STR_PTR, 0, SLJIT_IMM, 0, CURRENT_AS(assert_backtrack)->matchingpath);
10946 free_stack(common, 1);
10951 if (bra == OP_BRAZERO)
10953 if (*cc == OP_ASSERT_NOT || *cc == OP_ASSERTBACK_NOT)
10955 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), SLJIT_IMM, 0);
10956 CMPTO(SLJIT_NOT_EQUAL, STR_PTR, 0, SLJIT_IMM, 0, CURRENT_AS(assert_backtrack)->matchingpath);
10957 free_stack(common, 1);
10960 free_stack(common, 1);
10961 brajump = CMP(SLJIT_EQUAL, STR_PTR, 0, SLJIT_IMM, 0);
10964 if (*cc == OP_ASSERT || *cc == OP_ASSERTBACK)
10966 OP1(SLJIT_MOV, STACK_TOP, 0, SLJIT_MEM1(SLJIT_SP), CURRENT_AS(assert_backtrack)->private_data_ptr);
10967 add_jump(compiler, &common->revertframes, JUMP(SLJIT_FAST_CALL));
10968 OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(STACK_TOP), STACK(-2));
10969 OP2(SLJIT_ADD, STACK_TOP, 0, STACK_TOP, 0, SLJIT_IMM, (CURRENT_AS(assert_backtrack)->framesize - 1) * sizeof(sljit_sw));
10970 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), CURRENT_AS(assert_backtrack)->private_data_ptr, TMP1, 0);
10972 set_jumps(current->topbacktracks, LABEL());
10975 set_jumps(current->topbacktracks, LABEL());
10977 if (bra == OP_BRAZERO)
10979 /* We know there is enough place on the stack. */
10980 OP2(SLJIT_SUB, STACK_TOP, 0, STACK_TOP, 0, SLJIT_IMM, sizeof(sljit_sw));
10981 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), SLJIT_IMM, 0);
10982 JUMPTO(SLJIT_JUMP, CURRENT_AS(assert_backtrack)->matchingpath);
10987 static void compile_bracket_backtrackingpath(compiler_common *common, struct backtrack_common *current)
10990 int opcode, stacksize, alt_count, alt_max;
10992 int private_data_ptr = CURRENT_AS(bracket_backtrack)->private_data_ptr;
10993 int repeat_ptr = 0, repeat_type = 0, repeat_count = 0;
10994 PCRE2_SPTR cc = current->cc;
10995 PCRE2_SPTR ccbegin;
10997 PCRE2_UCHAR bra = OP_BRA;
10999 assert_backtrack *assert;
11000 sljit_uw *next_update_addr = NULL;
11001 BOOL has_alternatives;
11002 BOOL needs_control_head = FALSE;
11003 struct sljit_jump *brazero = NULL;
11004 struct sljit_jump *alt1 = NULL;
11005 struct sljit_jump *alt2 = NULL;
11006 struct sljit_jump *once = NULL;
11007 struct sljit_jump *cond = NULL;
11008 struct sljit_label *rmin_label = NULL;
11009 struct sljit_label *exact_label = NULL;
11011 if (*cc == OP_BRAZERO || *cc == OP_BRAMINZERO)
11018 ccbegin = bracketend(cc) - 1 - LINK_SIZE;
11020 if (ket == OP_KET && PRIVATE_DATA(ccbegin) != 0)
11022 repeat_ptr = PRIVATE_DATA(ccbegin);
11023 repeat_type = PRIVATE_DATA(ccbegin + 2);
11024 repeat_count = PRIVATE_DATA(ccbegin + 3);
11025 SLJIT_ASSERT(repeat_type != 0 && repeat_count != 0);
11026 if (repeat_type == OP_UPTO)
11028 if (repeat_type == OP_MINUPTO)
11033 has_alternatives = *cc == OP_ALT;
11034 if (SLJIT_UNLIKELY(opcode == OP_COND) || SLJIT_UNLIKELY(opcode == OP_SCOND))
11035 has_alternatives = (ccbegin[1 + LINK_SIZE] >= OP_ASSERT && ccbegin[1 + LINK_SIZE] <= OP_ASSERTBACK_NOT) || CURRENT_AS(bracket_backtrack)->u.condfailed != NULL;
11036 if (opcode == OP_CBRA || opcode == OP_SCBRA)
11037 offset = (GET2(ccbegin, 1 + LINK_SIZE)) << 1;
11038 if (SLJIT_UNLIKELY(opcode == OP_COND) && (*cc == OP_KETRMAX || *cc == OP_KETRMIN))
11041 alt_max = has_alternatives ? no_alternatives(ccbegin) : 0;
11043 /* Decoding the needs_control_head in framesize. */
11044 if (opcode == OP_ONCE)
11046 needs_control_head = (CURRENT_AS(bracket_backtrack)->u.framesize & 0x1) != 0;
11047 CURRENT_AS(bracket_backtrack)->u.framesize >>= 1;
11050 if (ket != OP_KET && repeat_type != 0)
11052 /* TMP1 is used in OP_KETRMIN below. */
11053 OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(STACK_TOP), STACK(0));
11054 free_stack(common, 1);
11055 if (repeat_type == OP_UPTO)
11056 OP2(SLJIT_ADD, SLJIT_MEM1(SLJIT_SP), repeat_ptr, TMP1, 0, SLJIT_IMM, 1);
11058 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), repeat_ptr, TMP1, 0);
11061 if (ket == OP_KETRMAX)
11063 if (bra == OP_BRAZERO)
11065 OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(STACK_TOP), STACK(0));
11066 free_stack(common, 1);
11067 brazero = CMP(SLJIT_EQUAL, TMP1, 0, SLJIT_IMM, 0);
11070 else if (ket == OP_KETRMIN)
11072 if (bra != OP_BRAMINZERO)
11074 OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(STACK_TOP), STACK(0));
11075 if (repeat_type != 0)
11077 /* TMP1 was set a few lines above. */
11078 CMPTO(SLJIT_NOT_EQUAL, TMP1, 0, SLJIT_IMM, 0, CURRENT_AS(bracket_backtrack)->recursive_matchingpath);
11079 /* Drop STR_PTR for non-greedy plus quantifier. */
11080 if (opcode != OP_ONCE)
11081 free_stack(common, 1);
11083 else if (opcode >= OP_SBRA || opcode == OP_ONCE)
11085 /* Checking zero-length iteration. */
11086 if (opcode != OP_ONCE || CURRENT_AS(bracket_backtrack)->u.framesize < 0)
11087 CMPTO(SLJIT_NOT_EQUAL, STR_PTR, 0, SLJIT_MEM1(SLJIT_SP), private_data_ptr, CURRENT_AS(bracket_backtrack)->recursive_matchingpath);
11090 OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), private_data_ptr);
11091 CMPTO(SLJIT_NOT_EQUAL, STR_PTR, 0, SLJIT_MEM1(TMP1), STACK(-CURRENT_AS(bracket_backtrack)->u.framesize - 2), CURRENT_AS(bracket_backtrack)->recursive_matchingpath);
11093 /* Drop STR_PTR for non-greedy plus quantifier. */
11094 if (opcode != OP_ONCE)
11095 free_stack(common, 1);
11098 JUMPTO(SLJIT_JUMP, CURRENT_AS(bracket_backtrack)->recursive_matchingpath);
11100 rmin_label = LABEL();
11101 if (repeat_type != 0)
11102 OP2(SLJIT_ADD, SLJIT_MEM1(SLJIT_SP), repeat_ptr, SLJIT_MEM1(SLJIT_SP), repeat_ptr, SLJIT_IMM, 1);
11104 else if (bra == OP_BRAZERO)
11106 OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(STACK_TOP), STACK(0));
11107 free_stack(common, 1);
11108 brazero = CMP(SLJIT_NOT_EQUAL, TMP1, 0, SLJIT_IMM, 0);
11110 else if (repeat_type == OP_EXACT)
11112 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), repeat_ptr, SLJIT_IMM, 1);
11113 exact_label = LABEL();
11118 if (common->capture_last_ptr != 0)
11120 SLJIT_ASSERT(common->optimized_cbracket[offset >> 1] == 0);
11121 OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(STACK_TOP), STACK(0));
11122 OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(STACK_TOP), STACK(1));
11123 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->capture_last_ptr, TMP1, 0);
11124 OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(STACK_TOP), STACK(2));
11125 free_stack(common, 3);
11126 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), OVECTOR(offset), TMP2, 0);
11127 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), OVECTOR(offset + 1), TMP1, 0);
11129 else if (common->optimized_cbracket[offset >> 1] == 0)
11131 OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(STACK_TOP), STACK(0));
11132 OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(STACK_TOP), STACK(1));
11133 free_stack(common, 2);
11134 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), OVECTOR(offset), TMP1, 0);
11135 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), OVECTOR(offset + 1), TMP2, 0);
11139 if (SLJIT_UNLIKELY(opcode == OP_ONCE))
11141 if (CURRENT_AS(bracket_backtrack)->u.framesize >= 0)
11143 OP1(SLJIT_MOV, STACK_TOP, 0, SLJIT_MEM1(SLJIT_SP), private_data_ptr);
11144 add_jump(compiler, &common->revertframes, JUMP(SLJIT_FAST_CALL));
11145 OP2(SLJIT_ADD, STACK_TOP, 0, STACK_TOP, 0, SLJIT_IMM, (CURRENT_AS(bracket_backtrack)->u.framesize - 1) * sizeof(sljit_sw));
11147 once = JUMP(SLJIT_JUMP);
11149 else if (SLJIT_UNLIKELY(opcode == OP_COND) || SLJIT_UNLIKELY(opcode == OP_SCOND))
11151 if (has_alternatives)
11153 /* Always exactly one alternative. */
11154 OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(STACK_TOP), STACK(0));
11155 free_stack(common, 1);
11158 alt1 = CMP(SLJIT_EQUAL, TMP1, 0, SLJIT_IMM, sizeof(sljit_uw));
11161 else if (has_alternatives)
11163 OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(STACK_TOP), STACK(0));
11164 free_stack(common, 1);
11168 /* Table jump if alt_max is greater than 4. */
11169 next_update_addr = allocate_read_only_data(common, alt_max * sizeof(sljit_uw));
11170 if (SLJIT_UNLIKELY(next_update_addr == NULL))
11172 sljit_emit_ijump(compiler, SLJIT_JUMP, SLJIT_MEM1(TMP1), (sljit_sw)next_update_addr);
11173 add_label_addr(common, next_update_addr++);
11178 alt2 = CMP(SLJIT_GREATER_EQUAL, TMP1, 0, SLJIT_IMM, 2 * sizeof(sljit_uw));
11179 alt1 = CMP(SLJIT_GREATER_EQUAL, TMP1, 0, SLJIT_IMM, sizeof(sljit_uw));
11183 COMPILE_BACKTRACKINGPATH(current->top);
11184 if (current->topbacktracks)
11185 set_jumps(current->topbacktracks, LABEL());
11187 if (SLJIT_UNLIKELY(opcode == OP_COND) || SLJIT_UNLIKELY(opcode == OP_SCOND))
11189 /* Conditional block always has at most one alternative. */
11190 if (ccbegin[1 + LINK_SIZE] >= OP_ASSERT && ccbegin[1 + LINK_SIZE] <= OP_ASSERTBACK_NOT)
11192 SLJIT_ASSERT(has_alternatives);
11193 assert = CURRENT_AS(bracket_backtrack)->u.assert;
11194 if (assert->framesize >= 0 && (ccbegin[1 + LINK_SIZE] == OP_ASSERT || ccbegin[1 + LINK_SIZE] == OP_ASSERTBACK))
11196 OP1(SLJIT_MOV, STACK_TOP, 0, SLJIT_MEM1(SLJIT_SP), assert->private_data_ptr);
11197 add_jump(compiler, &common->revertframes, JUMP(SLJIT_FAST_CALL));
11198 OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(STACK_TOP), STACK(-2));
11199 OP2(SLJIT_ADD, STACK_TOP, 0, STACK_TOP, 0, SLJIT_IMM, (assert->framesize - 1) * sizeof(sljit_sw));
11200 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), assert->private_data_ptr, TMP1, 0);
11202 cond = JUMP(SLJIT_JUMP);
11203 set_jumps(CURRENT_AS(bracket_backtrack)->u.assert->condfailed, LABEL());
11205 else if (CURRENT_AS(bracket_backtrack)->u.condfailed != NULL)
11207 SLJIT_ASSERT(has_alternatives);
11208 cond = JUMP(SLJIT_JUMP);
11209 set_jumps(CURRENT_AS(bracket_backtrack)->u.condfailed, LABEL());
11212 SLJIT_ASSERT(!has_alternatives);
11215 if (has_alternatives)
11217 alt_count = sizeof(sljit_uw);
11220 current->top = NULL;
11221 current->topbacktracks = NULL;
11222 current->nextbacktracks = NULL;
11223 /* Conditional blocks always have an additional alternative, even if it is empty. */
11226 ccprev = cc + 1 + LINK_SIZE;
11228 if (opcode != OP_COND && opcode != OP_SCOND)
11230 if (opcode != OP_ONCE)
11232 if (private_data_ptr != 0)
11233 OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(SLJIT_SP), private_data_ptr);
11235 OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(STACK_TOP), STACK(0));
11238 OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(STACK_TOP), STACK(needs_control_head ? 1 : 0));
11240 compile_matchingpath(common, ccprev, cc, current);
11241 if (SLJIT_UNLIKELY(sljit_get_compiler_error(compiler)))
11245 /* Instructions after the current alternative is successfully matched. */
11246 /* There is a similar code in compile_bracket_matchingpath. */
11247 if (opcode == OP_ONCE)
11248 match_once_common(common, ket, CURRENT_AS(bracket_backtrack)->u.framesize, private_data_ptr, has_alternatives, needs_control_head);
11251 if (repeat_type == OP_MINUPTO)
11253 /* We need to preserve the counter. TMP2 will be used below. */
11254 OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_SP), repeat_ptr);
11257 if (ket != OP_KET || bra != OP_BRA)
11261 if (common->capture_last_ptr != 0)
11263 if (common->optimized_cbracket[offset >> 1] == 0)
11266 if (opcode != OP_ONCE)
11270 allocate_stack(common, stacksize);
11273 if (repeat_type == OP_MINUPTO)
11275 /* TMP2 was set above. */
11276 OP2(SLJIT_SUB, SLJIT_MEM1(STACK_TOP), STACK(stacksize), TMP2, 0, SLJIT_IMM, 1);
11280 if (ket != OP_KET || bra != OP_BRA)
11283 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(stacksize), STR_PTR, 0);
11285 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(stacksize), SLJIT_IMM, 0);
11290 stacksize = match_capture_common(common, stacksize, offset, private_data_ptr);
11292 if (opcode != OP_ONCE)
11293 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(stacksize), SLJIT_IMM, alt_count);
11295 if (offset != 0 && ket == OP_KETRMAX && common->optimized_cbracket[offset >> 1] != 0)
11297 /* If ket is not OP_KETRMAX, this code path is executed after the jump to alternative_matchingpath. */
11298 SLJIT_ASSERT(private_data_ptr == OVECTOR(offset + 0));
11299 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), OVECTOR(offset + 1), STR_PTR, 0);
11302 JUMPTO(SLJIT_JUMP, CURRENT_AS(bracket_backtrack)->alternative_matchingpath);
11304 if (opcode != OP_ONCE)
11307 add_label_addr(common, next_update_addr++);
11310 if (alt_count != 2 * sizeof(sljit_uw))
11313 if (alt_max == 3 && alt_count == sizeof(sljit_uw))
11314 alt2 = CMP(SLJIT_GREATER_EQUAL, TMP1, 0, SLJIT_IMM, 2 * sizeof(sljit_uw));
11320 alt1 = CMP(SLJIT_GREATER_EQUAL, TMP1, 0, SLJIT_IMM, 3 * sizeof(sljit_uw));
11323 alt_count += sizeof(sljit_uw);
11326 COMPILE_BACKTRACKINGPATH(current->top);
11327 if (current->topbacktracks)
11328 set_jumps(current->topbacktracks, LABEL());
11329 SLJIT_ASSERT(!current->nextbacktracks);
11331 while (*cc == OP_ALT);
11335 SLJIT_ASSERT(opcode == OP_COND || opcode == OP_SCOND);
11336 assert = CURRENT_AS(bracket_backtrack)->u.assert;
11337 if ((ccbegin[1 + LINK_SIZE] == OP_ASSERT_NOT || ccbegin[1 + LINK_SIZE] == OP_ASSERTBACK_NOT) && assert->framesize >= 0)
11339 OP1(SLJIT_MOV, STACK_TOP, 0, SLJIT_MEM1(SLJIT_SP), assert->private_data_ptr);
11340 add_jump(compiler, &common->revertframes, JUMP(SLJIT_FAST_CALL));
11341 OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(STACK_TOP), STACK(-2));
11342 OP2(SLJIT_ADD, STACK_TOP, 0, STACK_TOP, 0, SLJIT_IMM, (assert->framesize - 1) * sizeof(sljit_sw));
11343 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), assert->private_data_ptr, TMP1, 0);
11348 /* Free the STR_PTR. */
11349 if (private_data_ptr == 0)
11350 free_stack(common, 1);
11355 /* Using both tmp register is better for instruction scheduling. */
11356 if (common->optimized_cbracket[offset >> 1] != 0)
11358 OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(STACK_TOP), STACK(0));
11359 OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(STACK_TOP), STACK(1));
11360 free_stack(common, 2);
11361 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), OVECTOR(offset), TMP1, 0);
11362 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), OVECTOR(offset + 1), TMP2, 0);
11366 OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(STACK_TOP), STACK(0));
11367 free_stack(common, 1);
11368 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), private_data_ptr, TMP1, 0);
11371 else if (opcode == OP_SBRA || opcode == OP_SCOND)
11373 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), private_data_ptr, SLJIT_MEM1(STACK_TOP), STACK(0));
11374 free_stack(common, 1);
11376 else if (opcode == OP_ONCE)
11378 cc = ccbegin + GET(ccbegin, 1);
11379 stacksize = needs_control_head ? 1 : 0;
11381 if (CURRENT_AS(bracket_backtrack)->u.framesize >= 0)
11383 /* Reset head and drop saved frame. */
11384 stacksize += CURRENT_AS(bracket_backtrack)->u.framesize + ((ket != OP_KET || *cc == OP_ALT) ? 2 : 1);
11386 else if (ket == OP_KETRMAX || (*cc == OP_ALT && ket != OP_KETRMIN))
11388 /* The STR_PTR must be released. */
11393 free_stack(common, stacksize);
11396 /* Restore previous private_data_ptr */
11397 if (CURRENT_AS(bracket_backtrack)->u.framesize >= 0)
11398 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), private_data_ptr, SLJIT_MEM1(STACK_TOP), STACK(-CURRENT_AS(bracket_backtrack)->u.framesize - 1));
11399 else if (ket == OP_KETRMIN)
11401 OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(STACK_TOP), STACK(1));
11402 /* See the comment below. */
11403 free_stack(common, 2);
11404 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), private_data_ptr, TMP1, 0);
11408 if (repeat_type == OP_EXACT)
11410 OP2(SLJIT_ADD, TMP1, 0, SLJIT_MEM1(SLJIT_SP), repeat_ptr, SLJIT_IMM, 1);
11411 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), repeat_ptr, TMP1, 0);
11412 CMPTO(SLJIT_LESS_EQUAL, TMP1, 0, SLJIT_IMM, repeat_count, exact_label);
11414 else if (ket == OP_KETRMAX)
11416 OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(STACK_TOP), STACK(0));
11417 if (bra != OP_BRAZERO)
11418 free_stack(common, 1);
11420 CMPTO(SLJIT_NOT_EQUAL, STR_PTR, 0, SLJIT_IMM, 0, CURRENT_AS(bracket_backtrack)->recursive_matchingpath);
11421 if (bra == OP_BRAZERO)
11423 OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(STACK_TOP), STACK(1));
11424 JUMPTO(SLJIT_JUMP, CURRENT_AS(bracket_backtrack)->zero_matchingpath);
11426 free_stack(common, 1);
11429 else if (ket == OP_KETRMIN)
11431 OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(STACK_TOP), STACK(0));
11433 /* OP_ONCE removes everything in case of a backtrack, so we don't
11434 need to explicitly release the STR_PTR. The extra release would
11435 affect badly the free_stack(2) above. */
11436 if (opcode != OP_ONCE)
11437 free_stack(common, 1);
11438 CMPTO(SLJIT_NOT_EQUAL, TMP1, 0, SLJIT_IMM, 0, rmin_label);
11439 if (opcode == OP_ONCE)
11440 free_stack(common, bra == OP_BRAMINZERO ? 2 : 1);
11441 else if (bra == OP_BRAMINZERO)
11442 free_stack(common, 1);
11444 else if (bra == OP_BRAZERO)
11446 OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(STACK_TOP), STACK(0));
11447 JUMPTO(SLJIT_JUMP, CURRENT_AS(bracket_backtrack)->zero_matchingpath);
11452 static SLJIT_INLINE void compile_bracketpos_backtrackingpath(compiler_common *common, struct backtrack_common *current)
11456 struct sljit_jump *jump;
11458 if (CURRENT_AS(bracketpos_backtrack)->framesize < 0)
11460 if (*current->cc == OP_CBRAPOS || *current->cc == OP_SCBRAPOS)
11462 offset = (GET2(current->cc, 1 + LINK_SIZE)) << 1;
11463 OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(STACK_TOP), STACK(0));
11464 OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(STACK_TOP), STACK(1));
11465 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), OVECTOR(offset), TMP1, 0);
11466 if (common->capture_last_ptr != 0)
11467 OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(STACK_TOP), STACK(2));
11468 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), OVECTOR(offset + 1), TMP2, 0);
11469 if (common->capture_last_ptr != 0)
11470 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->capture_last_ptr, TMP1, 0);
11472 set_jumps(current->topbacktracks, LABEL());
11473 free_stack(common, CURRENT_AS(bracketpos_backtrack)->stacksize);
11477 OP1(SLJIT_MOV, STACK_TOP, 0, SLJIT_MEM1(SLJIT_SP), CURRENT_AS(bracketpos_backtrack)->private_data_ptr);
11478 add_jump(compiler, &common->revertframes, JUMP(SLJIT_FAST_CALL));
11479 OP2(SLJIT_ADD, STACK_TOP, 0, STACK_TOP, 0, SLJIT_IMM, (CURRENT_AS(bracketpos_backtrack)->framesize - 1) * sizeof(sljit_sw));
11481 if (current->topbacktracks)
11483 jump = JUMP(SLJIT_JUMP);
11484 set_jumps(current->topbacktracks, LABEL());
11485 /* Drop the stack frame. */
11486 free_stack(common, CURRENT_AS(bracketpos_backtrack)->stacksize);
11489 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), CURRENT_AS(bracketpos_backtrack)->private_data_ptr, SLJIT_MEM1(STACK_TOP), STACK(-CURRENT_AS(bracketpos_backtrack)->framesize - 1));
11492 static SLJIT_INLINE void compile_braminzero_backtrackingpath(compiler_common *common, struct backtrack_common *current)
11494 assert_backtrack backtrack;
11496 current->top = NULL;
11497 current->topbacktracks = NULL;
11498 current->nextbacktracks = NULL;
11499 if (current->cc[1] > OP_ASSERTBACK_NOT)
11501 /* Manual call of compile_bracket_matchingpath and compile_bracket_backtrackingpath. */
11502 compile_bracket_matchingpath(common, current->cc, current);
11503 compile_bracket_backtrackingpath(common, current->top);
11507 memset(&backtrack, 0, sizeof(backtrack));
11508 backtrack.common.cc = current->cc;
11509 backtrack.matchingpath = CURRENT_AS(braminzero_backtrack)->matchingpath;
11510 /* Manual call of compile_assert_matchingpath. */
11511 compile_assert_matchingpath(common, current->cc, &backtrack, FALSE);
11513 SLJIT_ASSERT(!current->nextbacktracks && !current->topbacktracks);
11516 static SLJIT_INLINE void compile_control_verb_backtrackingpath(compiler_common *common, struct backtrack_common *current)
11519 PCRE2_UCHAR opcode = *current->cc;
11520 struct sljit_label *loop;
11521 struct sljit_jump *jump;
11523 if (opcode == OP_THEN || opcode == OP_THEN_ARG)
11525 if (common->then_trap != NULL)
11527 SLJIT_ASSERT(common->control_head_ptr != 0);
11529 OP1(SLJIT_MOV, STACK_TOP, 0, SLJIT_MEM1(SLJIT_SP), common->control_head_ptr);
11530 OP1(SLJIT_MOV, TMP1, 0, SLJIT_IMM, type_then_trap);
11531 OP1(SLJIT_MOV, TMP2, 0, SLJIT_IMM, common->then_trap->start);
11532 jump = JUMP(SLJIT_JUMP);
11535 OP1(SLJIT_MOV, STACK_TOP, 0, SLJIT_MEM1(STACK_TOP), STACK(0));
11537 CMPTO(SLJIT_NOT_EQUAL, SLJIT_MEM1(STACK_TOP), STACK(1), TMP1, 0, loop);
11538 CMPTO(SLJIT_NOT_EQUAL, SLJIT_MEM1(STACK_TOP), STACK(2), TMP2, 0, loop);
11539 add_jump(compiler, &common->then_trap->quit, JUMP(SLJIT_JUMP));
11542 else if (!common->local_quit_available && common->in_positive_assertion)
11544 add_jump(compiler, &common->positive_assertion_quit, JUMP(SLJIT_JUMP));
11549 if (common->local_quit_available)
11551 /* Abort match with a fail. */
11552 if (common->quit_label == NULL)
11553 add_jump(compiler, &common->quit, JUMP(SLJIT_JUMP));
11555 JUMPTO(SLJIT_JUMP, common->quit_label);
11559 if (opcode == OP_SKIP_ARG)
11561 SLJIT_ASSERT(common->control_head_ptr != 0 && TMP1 == SLJIT_R0 && STR_PTR == SLJIT_R1);
11562 OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), common->control_head_ptr);
11563 OP1(SLJIT_MOV, SLJIT_R1, 0, SLJIT_IMM, (sljit_sw)(current->cc + 2));
11564 sljit_emit_icall(compiler, SLJIT_CALL, SLJIT_RET(SW) | SLJIT_ARG1(SW) | SLJIT_ARG2(SW), SLJIT_IMM, SLJIT_FUNC_OFFSET(do_search_mark));
11566 OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_R0, 0);
11567 add_jump(compiler, &common->reset_match, CMP(SLJIT_NOT_EQUAL, SLJIT_R0, 0, SLJIT_IMM, 0));
11571 if (opcode == OP_SKIP)
11572 OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(STACK_TOP), STACK(0));
11574 OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_IMM, 0);
11575 add_jump(compiler, &common->reset_match, JUMP(SLJIT_JUMP));
11578 static SLJIT_INLINE void compile_then_trap_backtrackingpath(compiler_common *common, struct backtrack_common *current)
11581 struct sljit_jump *jump;
11584 if (CURRENT_AS(then_trap_backtrack)->then_trap)
11586 common->then_trap = CURRENT_AS(then_trap_backtrack)->then_trap;
11590 size = CURRENT_AS(then_trap_backtrack)->framesize;
11591 size = 3 + (size < 0 ? 0 : size);
11593 OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(STACK_TOP), STACK(size - 3));
11594 free_stack(common, size);
11595 jump = JUMP(SLJIT_JUMP);
11597 set_jumps(CURRENT_AS(then_trap_backtrack)->quit, LABEL());
11598 /* STACK_TOP is set by THEN. */
11599 if (CURRENT_AS(then_trap_backtrack)->framesize >= 0)
11601 add_jump(compiler, &common->revertframes, JUMP(SLJIT_FAST_CALL));
11602 OP2(SLJIT_ADD, STACK_TOP, 0, STACK_TOP, 0, SLJIT_IMM, (CURRENT_AS(then_trap_backtrack)->framesize - 1) * sizeof(sljit_sw));
11604 OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(STACK_TOP), STACK(0));
11605 free_stack(common, 3);
11608 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->control_head_ptr, TMP1, 0);
11611 static void compile_backtrackingpath(compiler_common *common, struct backtrack_common *current)
11614 then_trap_backtrack *save_then_trap = common->then_trap;
11618 if (current->nextbacktracks != NULL)
11619 set_jumps(current->nextbacktracks, LABEL());
11620 switch(*current->cc)
11623 OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(STACK_TOP), STACK(0));
11624 free_stack(common, 1);
11625 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), OVECTOR(0), TMP1, 0);
11655 case OP_NOTMINSTAR:
11657 case OP_NOTMINPLUS:
11659 case OP_NOTMINQUERY:
11661 case OP_NOTMINUPTO:
11663 case OP_NOTPOSSTAR:
11664 case OP_NOTPOSPLUS:
11665 case OP_NOTPOSQUERY:
11666 case OP_NOTPOSUPTO:
11668 case OP_NOTMINSTARI:
11670 case OP_NOTMINPLUSI:
11672 case OP_NOTMINQUERYI:
11674 case OP_NOTMINUPTOI:
11676 case OP_NOTPOSSTARI:
11677 case OP_NOTPOSPLUSI:
11678 case OP_NOTPOSQUERYI:
11679 case OP_NOTPOSUPTOI:
11681 case OP_TYPEMINSTAR:
11683 case OP_TYPEMINPLUS:
11685 case OP_TYPEMINQUERY:
11687 case OP_TYPEMINUPTO:
11689 case OP_TYPEPOSSTAR:
11690 case OP_TYPEPOSPLUS:
11691 case OP_TYPEPOSQUERY:
11692 case OP_TYPEPOSUPTO:
11695 #if defined SUPPORT_UNICODE || PCRE2_CODE_UNIT_WIDTH != 8
11698 compile_iterator_backtrackingpath(common, current);
11705 compile_ref_iterator_backtrackingpath(common, current);
11709 compile_recurse_backtrackingpath(common, current);
11713 case OP_ASSERT_NOT:
11714 case OP_ASSERTBACK:
11715 case OP_ASSERTBACK_NOT:
11716 compile_assert_backtrackingpath(common, current);
11726 compile_bracket_backtrackingpath(common, current);
11730 if (current->cc[1] > OP_ASSERTBACK_NOT)
11731 compile_bracket_backtrackingpath(common, current);
11733 compile_assert_backtrackingpath(common, current);
11740 case OP_BRAPOSZERO:
11741 compile_bracketpos_backtrackingpath(common, current);
11744 case OP_BRAMINZERO:
11745 compile_braminzero_backtrackingpath(common, current);
11749 OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(STACK_TOP), STACK(common->has_skip_arg ? 4 : 0));
11750 if (common->has_skip_arg)
11751 OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(STACK_TOP), STACK(0));
11752 free_stack(common, common->has_skip_arg ? 5 : 1);
11753 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->mark_ptr, TMP1, 0);
11754 if (common->has_skip_arg)
11755 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->control_head_ptr, TMP2, 0);
11764 compile_control_verb_backtrackingpath(common, current);
11768 case OP_COMMIT_ARG:
11769 if (!common->local_quit_available)
11770 OP1(SLJIT_MOV, SLJIT_RETURN_REG, 0, SLJIT_IMM, PCRE2_ERROR_NOMATCH);
11771 if (common->quit_label == NULL)
11772 add_jump(compiler, &common->quit, JUMP(SLJIT_JUMP));
11774 JUMPTO(SLJIT_JUMP, common->quit_label);
11778 case OP_CALLOUT_STR:
11781 case OP_ASSERT_ACCEPT:
11782 set_jumps(current->topbacktracks, LABEL());
11786 /* A virtual opcode for then traps. */
11787 compile_then_trap_backtrackingpath(common, current);
11791 SLJIT_UNREACHABLE();
11794 current = current->prev;
11796 common->then_trap = save_then_trap;
11799 static SLJIT_INLINE void compile_recurse(compiler_common *common)
11802 PCRE2_SPTR cc = common->start + common->currententry->start;
11803 PCRE2_SPTR ccbegin = cc + 1 + LINK_SIZE + (*cc == OP_BRA ? 0 : IMM2_SIZE);
11804 PCRE2_SPTR ccend = bracketend(cc) - (1 + LINK_SIZE);
11805 BOOL needs_control_head;
11808 int private_data_size = get_recurse_data_length(common, ccbegin, ccend, &needs_control_head, &has_quit, &has_accept);
11809 int alt_count, alt_max, local_size;
11810 backtrack_common altbacktrack;
11811 jump_list *match = NULL;
11812 sljit_uw *next_update_addr = NULL;
11813 struct sljit_jump *alt1 = NULL;
11814 struct sljit_jump *alt2 = NULL;
11815 struct sljit_jump *accept_exit = NULL;
11816 struct sljit_label *quit;
11818 /* Recurse captures then. */
11819 common->then_trap = NULL;
11821 SLJIT_ASSERT(*cc == OP_BRA || *cc == OP_CBRA || *cc == OP_CBRAPOS || *cc == OP_SCBRA || *cc == OP_SCBRAPOS);
11823 alt_max = no_alternatives(cc);
11826 /* Matching path. */
11827 SLJIT_ASSERT(common->currententry->entry_label == NULL && common->recursive_head_ptr != 0);
11828 common->currententry->entry_label = LABEL();
11829 set_jumps(common->currententry->entry_calls, common->currententry->entry_label);
11831 sljit_emit_fast_enter(compiler, TMP2, 0);
11832 count_match(common);
11834 local_size = (alt_max > 1) ? 2 : 1;
11836 /* (Reversed) stack layout:
11837 [private data][return address][optional: str ptr] ... [optional: alternative index][recursive_head_ptr] */
11839 allocate_stack(common, private_data_size + local_size);
11840 /* Save return address. */
11841 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(local_size - 1), TMP2, 0);
11843 copy_recurse_data(common, ccbegin, ccend, recurse_copy_from_global, local_size, private_data_size + local_size, has_quit);
11845 /* This variable is saved and restored all time when we enter or exit from a recursive context. */
11846 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->recursive_head_ptr, STACK_TOP, 0);
11848 if (needs_control_head)
11849 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->control_head_ptr, SLJIT_IMM, 0);
11852 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), STR_PTR, 0);
11854 memset(&altbacktrack, 0, sizeof(backtrack_common));
11855 common->quit_label = NULL;
11856 common->accept_label = NULL;
11857 common->quit = NULL;
11858 common->accept = NULL;
11859 altbacktrack.cc = ccbegin;
11863 altbacktrack.top = NULL;
11864 altbacktrack.topbacktracks = NULL;
11866 if (altbacktrack.cc != ccbegin)
11867 OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(STACK_TOP), STACK(0));
11869 compile_matchingpath(common, altbacktrack.cc, cc, &altbacktrack);
11870 if (SLJIT_UNLIKELY(sljit_get_compiler_error(compiler)))
11873 allocate_stack(common, (alt_max > 1 || has_accept) ? 2 : 1);
11874 OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_SP), common->recursive_head_ptr);
11876 if (alt_max > 1 || has_accept)
11877 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(1), SLJIT_IMM, alt_count);
11879 add_jump(compiler, &match, JUMP(SLJIT_JUMP));
11881 if (alt_count == 0)
11883 /* Backtracking path entry. */
11884 SLJIT_ASSERT(common->currententry->backtrack_label == NULL);
11885 common->currententry->backtrack_label = LABEL();
11886 set_jumps(common->currententry->backtrack_calls, common->currententry->backtrack_label);
11888 sljit_emit_fast_enter(compiler, TMP1, 0);
11891 accept_exit = CMP(SLJIT_EQUAL, SLJIT_MEM1(STACK_TOP), STACK(1), SLJIT_IMM, alt_max * sizeof (sljit_sw));
11893 OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(STACK_TOP), STACK(0));
11894 /* Save return address. */
11895 OP1(SLJIT_MOV, SLJIT_MEM1(TMP2), STACK(local_size - 1), TMP1, 0);
11897 copy_recurse_data(common, ccbegin, ccend, recurse_swap_global, local_size, private_data_size + local_size, has_quit);
11901 OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(STACK_TOP), STACK(1));
11902 free_stack(common, 2);
11906 /* Table jump if alt_max is greater than 4. */
11907 next_update_addr = allocate_read_only_data(common, alt_max * sizeof(sljit_uw));
11908 if (SLJIT_UNLIKELY(next_update_addr == NULL))
11910 sljit_emit_ijump(compiler, SLJIT_JUMP, SLJIT_MEM1(TMP1), (sljit_sw)next_update_addr);
11911 add_label_addr(common, next_update_addr++);
11916 alt2 = CMP(SLJIT_GREATER_EQUAL, TMP1, 0, SLJIT_IMM, 2 * sizeof(sljit_uw));
11917 alt1 = CMP(SLJIT_GREATER_EQUAL, TMP1, 0, SLJIT_IMM, sizeof(sljit_uw));
11921 free_stack(common, has_accept ? 2 : 1);
11923 else if (alt_max > 4)
11924 add_label_addr(common, next_update_addr++);
11927 if (alt_count != 2 * sizeof(sljit_uw))
11930 if (alt_max == 3 && alt_count == sizeof(sljit_uw))
11931 alt2 = CMP(SLJIT_GREATER_EQUAL, TMP1, 0, SLJIT_IMM, 2 * sizeof(sljit_uw));
11937 alt1 = CMP(SLJIT_GREATER_EQUAL, TMP1, 0, SLJIT_IMM, 3 * sizeof(sljit_uw));
11941 alt_count += sizeof(sljit_uw);
11943 compile_backtrackingpath(common, altbacktrack.top);
11944 if (SLJIT_UNLIKELY(sljit_get_compiler_error(compiler)))
11946 set_jumps(altbacktrack.topbacktracks, LABEL());
11951 altbacktrack.cc = cc + 1 + LINK_SIZE;
11955 /* No alternative is matched. */
11959 copy_recurse_data(common, ccbegin, ccend, recurse_copy_private_to_global, local_size, private_data_size + local_size, has_quit);
11961 OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(STACK_TOP), STACK(local_size - 1));
11962 free_stack(common, private_data_size + local_size);
11963 OP1(SLJIT_MOV, TMP1, 0, SLJIT_IMM, 0);
11964 sljit_emit_fast_return(compiler, TMP2, 0);
11966 if (common->quit != NULL)
11968 SLJIT_ASSERT(has_quit);
11970 set_jumps(common->quit, LABEL());
11971 OP1(SLJIT_MOV, STACK_TOP, 0, SLJIT_MEM1(SLJIT_SP), common->recursive_head_ptr);
11972 copy_recurse_data(common, ccbegin, ccend, recurse_copy_shared_to_global, local_size, private_data_size + local_size, has_quit);
11973 JUMPTO(SLJIT_JUMP, quit);
11978 JUMPHERE(accept_exit);
11979 free_stack(common, 2);
11981 /* Save return address. */
11982 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(local_size - 1), TMP1, 0);
11984 copy_recurse_data(common, ccbegin, ccend, recurse_copy_kept_shared_to_global, local_size, private_data_size + local_size, has_quit);
11986 OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(STACK_TOP), STACK(local_size - 1));
11987 free_stack(common, private_data_size + local_size);
11988 OP1(SLJIT_MOV, TMP1, 0, SLJIT_IMM, 0);
11989 sljit_emit_fast_return(compiler, TMP2, 0);
11992 if (common->accept != NULL)
11994 SLJIT_ASSERT(has_accept);
11996 set_jumps(common->accept, LABEL());
11998 OP1(SLJIT_MOV, STACK_TOP, 0, SLJIT_MEM1(SLJIT_SP), common->recursive_head_ptr);
11999 OP1(SLJIT_MOV, TMP2, 0, STACK_TOP, 0);
12001 allocate_stack(common, 2);
12002 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(1), SLJIT_IMM, alt_count);
12005 set_jumps(match, LABEL());
12007 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), TMP2, 0);
12009 copy_recurse_data(common, ccbegin, ccend, recurse_swap_global, local_size, private_data_size + local_size, has_quit);
12011 OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(TMP2), STACK(local_size - 1));
12012 OP1(SLJIT_MOV, TMP1, 0, SLJIT_IMM, 1);
12013 sljit_emit_fast_return(compiler, TMP2, 0);
12016 #undef COMPILE_BACKTRACKINGPATH
12019 static int jit_compile(pcre2_code *code, sljit_u32 mode)
12021 pcre2_real_code *re = (pcre2_real_code *)code;
12022 struct sljit_compiler *compiler;
12023 backtrack_common rootbacktrack;
12024 compiler_common common_data;
12025 compiler_common *common = &common_data;
12026 const sljit_u8 *tables = re->tables;
12027 void *allocator_data = &re->memctl;
12028 int private_data_size;
12030 executable_functions *functions;
12031 void *executable_func;
12032 sljit_uw executable_size;
12033 sljit_uw total_length;
12034 label_addr_list *label_addr;
12035 struct sljit_label *mainloop_label = NULL;
12036 struct sljit_label *continue_match_label;
12037 struct sljit_label *empty_match_found_label = NULL;
12038 struct sljit_label *empty_match_backtrack_label = NULL;
12039 struct sljit_label *reset_match_label;
12040 struct sljit_label *quit_label;
12041 struct sljit_jump *jump;
12042 struct sljit_jump *minlength_check_failed = NULL;
12043 struct sljit_jump *reqbyte_notfound = NULL;
12044 struct sljit_jump *empty_match = NULL;
12045 struct sljit_jump *end_anchor_failed = NULL;
12047 SLJIT_ASSERT(tables);
12049 memset(&rootbacktrack, 0, sizeof(backtrack_common));
12050 memset(common, 0, sizeof(compiler_common));
12052 common->name_table = (PCRE2_SPTR)((uint8_t *)re + sizeof(pcre2_real_code));
12053 rootbacktrack.cc = common->name_table + re->name_count * re->name_entry_size;
12055 common->start = rootbacktrack.cc;
12056 common->read_only_data_head = NULL;
12057 common->fcc = tables + fcc_offset;
12058 common->lcc = (sljit_sw)(tables + lcc_offset);
12059 common->mode = mode;
12060 common->might_be_empty = re->minlength == 0;
12061 common->nltype = NLTYPE_FIXED;
12062 switch(re->newline_convention)
12064 case PCRE2_NEWLINE_CR: common->newline = CHAR_CR; break;
12065 case PCRE2_NEWLINE_LF: common->newline = CHAR_NL; break;
12066 case PCRE2_NEWLINE_CRLF: common->newline = (CHAR_CR << 8) | CHAR_NL; break;
12067 case PCRE2_NEWLINE_ANY: common->newline = (CHAR_CR << 8) | CHAR_NL; common->nltype = NLTYPE_ANY; break;
12068 case PCRE2_NEWLINE_ANYCRLF: common->newline = (CHAR_CR << 8) | CHAR_NL; common->nltype = NLTYPE_ANYCRLF; break;
12069 default: return PCRE2_ERROR_INTERNAL;
12071 common->nlmax = READ_CHAR_MAX;
12073 if (re->bsr_convention == PCRE2_BSR_UNICODE)
12074 common->bsr_nltype = NLTYPE_ANY;
12075 else if (re->bsr_convention == PCRE2_BSR_ANYCRLF)
12076 common->bsr_nltype = NLTYPE_ANYCRLF;
12080 common->bsr_nltype = NLTYPE_ANYCRLF;
12082 common->bsr_nltype = NLTYPE_ANY;
12085 common->bsr_nlmax = READ_CHAR_MAX;
12086 common->bsr_nlmin = 0;
12087 common->endonly = (re->overall_options & PCRE2_DOLLAR_ENDONLY) != 0;
12088 common->ctypes = (sljit_sw)(tables + ctypes_offset);
12089 common->name_count = re->name_count;
12090 common->name_entry_size = re->name_entry_size;
12091 common->unset_backref = (re->overall_options & PCRE2_MATCH_UNSET_BACKREF) != 0;
12092 common->alt_circumflex = (re->overall_options & PCRE2_ALT_CIRCUMFLEX) != 0;
12093 #ifdef SUPPORT_UNICODE
12094 /* PCRE_UTF[16|32] have the same value as PCRE_UTF8. */
12095 common->utf = (re->overall_options & PCRE2_UTF) != 0;
12096 common->use_ucp = (re->overall_options & PCRE2_UCP) != 0;
12099 if (common->nltype == NLTYPE_ANY)
12100 common->nlmax = 0x2029;
12101 else if (common->nltype == NLTYPE_ANYCRLF)
12102 common->nlmax = (CHAR_CR > CHAR_NL) ? CHAR_CR : CHAR_NL;
12105 /* We only care about the first newline character. */
12106 common->nlmax = common->newline & 0xff;
12109 if (common->nltype == NLTYPE_FIXED)
12110 common->nlmin = common->newline & 0xff;
12112 common->nlmin = (CHAR_CR < CHAR_NL) ? CHAR_CR : CHAR_NL;
12114 if (common->bsr_nltype == NLTYPE_ANY)
12115 common->bsr_nlmax = 0x2029;
12117 common->bsr_nlmax = (CHAR_CR > CHAR_NL) ? CHAR_CR : CHAR_NL;
12118 common->bsr_nlmin = (CHAR_CR < CHAR_NL) ? CHAR_CR : CHAR_NL;
12120 #endif /* SUPPORT_UNICODE */
12121 ccend = bracketend(common->start);
12123 /* Calculate the local space size on the stack. */
12124 common->ovector_start = LIMIT_MATCH + sizeof(sljit_sw);
12125 common->optimized_cbracket = (sljit_u8 *)SLJIT_MALLOC(re->top_bracket + 1, allocator_data);
12126 if (!common->optimized_cbracket)
12127 return PCRE2_ERROR_NOMEMORY;
12128 #if defined DEBUG_FORCE_UNOPTIMIZED_CBRAS && DEBUG_FORCE_UNOPTIMIZED_CBRAS == 1
12129 memset(common->optimized_cbracket, 0, re->top_bracket + 1);
12131 memset(common->optimized_cbracket, 1, re->top_bracket + 1);
12134 SLJIT_ASSERT(*common->start == OP_BRA && ccend[-(1 + LINK_SIZE)] == OP_KET);
12135 #if defined DEBUG_FORCE_UNOPTIMIZED_CBRAS && DEBUG_FORCE_UNOPTIMIZED_CBRAS == 2
12136 common->capture_last_ptr = common->ovector_start;
12137 common->ovector_start += sizeof(sljit_sw);
12139 if (!check_opcode_types(common, common->start, ccend))
12141 SLJIT_FREE(common->optimized_cbracket, allocator_data);
12142 return PCRE2_ERROR_NOMEMORY;
12145 /* Checking flags and updating ovector_start. */
12146 if (mode == PCRE2_JIT_COMPLETE && (re->flags & PCRE2_LASTSET) != 0 && (re->overall_options & PCRE2_NO_START_OPTIMIZE) == 0)
12148 common->req_char_ptr = common->ovector_start;
12149 common->ovector_start += sizeof(sljit_sw);
12151 if (mode != PCRE2_JIT_COMPLETE)
12153 common->start_used_ptr = common->ovector_start;
12154 common->ovector_start += sizeof(sljit_sw);
12155 if (mode == PCRE2_JIT_PARTIAL_SOFT)
12157 common->hit_start = common->ovector_start;
12158 common->ovector_start += sizeof(sljit_sw);
12161 if ((re->overall_options & (PCRE2_FIRSTLINE | PCRE2_USE_OFFSET_LIMIT)) != 0)
12163 common->match_end_ptr = common->ovector_start;
12164 common->ovector_start += sizeof(sljit_sw);
12166 #if defined DEBUG_FORCE_CONTROL_HEAD && DEBUG_FORCE_CONTROL_HEAD
12167 common->control_head_ptr = 1;
12169 if (common->control_head_ptr != 0)
12171 common->control_head_ptr = common->ovector_start;
12172 common->ovector_start += sizeof(sljit_sw);
12174 if (common->has_set_som)
12176 /* Saving the real start pointer is necessary. */
12177 common->start_ptr = common->ovector_start;
12178 common->ovector_start += sizeof(sljit_sw);
12181 /* Aligning ovector to even number of sljit words. */
12182 if ((common->ovector_start & sizeof(sljit_sw)) != 0)
12183 common->ovector_start += sizeof(sljit_sw);
12185 if (common->start_ptr == 0)
12186 common->start_ptr = OVECTOR(0);
12188 /* Capturing brackets cannot be optimized if callouts are allowed. */
12189 if (common->capture_last_ptr != 0)
12190 memset(common->optimized_cbracket, 0, re->top_bracket + 1);
12192 SLJIT_ASSERT(!(common->req_char_ptr != 0 && common->start_used_ptr != 0));
12193 common->cbra_ptr = OVECTOR_START + (re->top_bracket + 1) * 2 * sizeof(sljit_sw);
12195 total_length = ccend - common->start;
12196 common->private_data_ptrs = (sljit_s32 *)SLJIT_MALLOC(total_length * (sizeof(sljit_s32) + (common->has_then ? 1 : 0)), allocator_data);
12197 if (!common->private_data_ptrs)
12199 SLJIT_FREE(common->optimized_cbracket, allocator_data);
12200 return PCRE2_ERROR_NOMEMORY;
12202 memset(common->private_data_ptrs, 0, total_length * sizeof(sljit_s32));
12204 private_data_size = common->cbra_ptr + (re->top_bracket + 1) * sizeof(sljit_sw);
12205 set_private_data_ptrs(common, &private_data_size, ccend);
12206 if ((re->overall_options & PCRE2_ANCHORED) == 0 && (re->overall_options & PCRE2_NO_START_OPTIMIZE) == 0)
12208 if (!detect_fast_forward_skip(common, &private_data_size) && !common->has_skip_in_assert_back)
12209 detect_fast_fail(common, common->start, &private_data_size, 4);
12212 SLJIT_ASSERT(common->fast_fail_start_ptr <= common->fast_fail_end_ptr);
12214 if (private_data_size > SLJIT_MAX_LOCAL_SIZE)
12216 SLJIT_FREE(common->private_data_ptrs, allocator_data);
12217 SLJIT_FREE(common->optimized_cbracket, allocator_data);
12218 return PCRE2_ERROR_NOMEMORY;
12221 if (common->has_then)
12223 common->then_offsets = (sljit_u8 *)(common->private_data_ptrs + total_length);
12224 memset(common->then_offsets, 0, total_length);
12225 set_then_offsets(common, common->start, NULL);
12228 compiler = sljit_create_compiler(allocator_data);
12231 SLJIT_FREE(common->optimized_cbracket, allocator_data);
12232 SLJIT_FREE(common->private_data_ptrs, allocator_data);
12233 return PCRE2_ERROR_NOMEMORY;
12235 common->compiler = compiler;
12237 /* Main pcre_jit_exec entry. */
12238 sljit_emit_enter(compiler, 0, SLJIT_ARG1(SW), 5, 5, 0, 0, private_data_size);
12240 /* Register init. */
12241 reset_ovector(common, (re->top_bracket + 1) * 2);
12242 if (common->req_char_ptr != 0)
12243 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->req_char_ptr, SLJIT_R0, 0);
12245 OP1(SLJIT_MOV, ARGUMENTS, 0, SLJIT_S0, 0);
12246 OP1(SLJIT_MOV, TMP1, 0, SLJIT_S0, 0);
12247 OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(TMP1), SLJIT_OFFSETOF(jit_arguments, str));
12248 OP1(SLJIT_MOV, STR_END, 0, SLJIT_MEM1(TMP1), SLJIT_OFFSETOF(jit_arguments, end));
12249 OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(TMP1), SLJIT_OFFSETOF(jit_arguments, stack));
12250 OP1(SLJIT_MOV_U32, TMP1, 0, SLJIT_MEM1(TMP1), SLJIT_OFFSETOF(jit_arguments, limit_match));
12251 OP1(SLJIT_MOV, STACK_TOP, 0, SLJIT_MEM1(TMP2), SLJIT_OFFSETOF(struct sljit_stack, end));
12252 OP1(SLJIT_MOV, STACK_LIMIT, 0, SLJIT_MEM1(TMP2), SLJIT_OFFSETOF(struct sljit_stack, start));
12253 OP2(SLJIT_ADD, TMP1, 0, TMP1, 0, SLJIT_IMM, 1);
12254 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), LIMIT_MATCH, TMP1, 0);
12256 if (common->fast_fail_start_ptr < common->fast_fail_end_ptr)
12257 reset_fast_fail(common);
12259 if (mode == PCRE2_JIT_PARTIAL_SOFT)
12260 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->hit_start, SLJIT_IMM, -1);
12261 if (common->mark_ptr != 0)
12262 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->mark_ptr, SLJIT_IMM, 0);
12263 if (common->control_head_ptr != 0)
12264 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->control_head_ptr, SLJIT_IMM, 0);
12266 /* Main part of the matching */
12267 if ((re->overall_options & PCRE2_ANCHORED) == 0)
12269 mainloop_label = mainloop_entry(common);
12270 continue_match_label = LABEL();
12271 /* Forward search if possible. */
12272 if ((re->overall_options & PCRE2_NO_START_OPTIMIZE) == 0)
12274 if (mode == PCRE2_JIT_COMPLETE && fast_forward_first_n_chars(common))
12276 else if ((re->flags & PCRE2_FIRSTSET) != 0)
12277 fast_forward_first_char(common);
12278 else if ((re->flags & PCRE2_STARTLINE) != 0)
12279 fast_forward_newline(common);
12280 else if ((re->flags & PCRE2_FIRSTMAPSET) != 0)
12281 fast_forward_start_bits(common);
12285 continue_match_label = LABEL();
12287 if (mode == PCRE2_JIT_COMPLETE && re->minlength > 0 && (re->overall_options & PCRE2_NO_START_OPTIMIZE) == 0)
12289 OP1(SLJIT_MOV, SLJIT_RETURN_REG, 0, SLJIT_IMM, PCRE2_ERROR_NOMATCH);
12290 OP2(SLJIT_ADD, TMP2, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(re->minlength));
12291 minlength_check_failed = CMP(SLJIT_GREATER, TMP2, 0, STR_END, 0);
12293 if (common->req_char_ptr != 0)
12294 reqbyte_notfound = search_requested_char(common, (PCRE2_UCHAR)(re->last_codeunit), (re->flags & PCRE2_LASTCASELESS) != 0, (re->flags & PCRE2_FIRSTSET) != 0);
12296 /* Store the current STR_PTR in OVECTOR(0). */
12297 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), OVECTOR(0), STR_PTR, 0);
12298 /* Copy the limit of allowed recursions. */
12299 OP1(SLJIT_MOV, COUNT_MATCH, 0, SLJIT_MEM1(SLJIT_SP), LIMIT_MATCH);
12300 if (common->capture_last_ptr != 0)
12301 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->capture_last_ptr, SLJIT_IMM, 0);
12302 if (common->fast_forward_bc_ptr != NULL)
12303 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), PRIVATE_DATA(common->fast_forward_bc_ptr + 1), STR_PTR, 0);
12305 if (common->start_ptr != OVECTOR(0))
12306 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->start_ptr, STR_PTR, 0);
12308 /* Copy the beginning of the string. */
12309 if (mode == PCRE2_JIT_PARTIAL_SOFT)
12311 jump = CMP(SLJIT_NOT_EQUAL, SLJIT_MEM1(SLJIT_SP), common->hit_start, SLJIT_IMM, -1);
12312 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->start_used_ptr, STR_PTR, 0);
12315 else if (mode == PCRE2_JIT_PARTIAL_HARD)
12316 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->start_used_ptr, STR_PTR, 0);
12318 compile_matchingpath(common, common->start, ccend, &rootbacktrack);
12319 if (SLJIT_UNLIKELY(sljit_get_compiler_error(compiler)))
12321 sljit_free_compiler(compiler);
12322 SLJIT_FREE(common->optimized_cbracket, allocator_data);
12323 SLJIT_FREE(common->private_data_ptrs, allocator_data);
12324 PRIV(jit_free_rodata)(common->read_only_data_head, compiler->allocator_data);
12325 return PCRE2_ERROR_NOMEMORY;
12328 if ((re->overall_options & PCRE2_ENDANCHORED) != 0)
12329 end_anchor_failed = CMP(SLJIT_NOT_EQUAL, STR_PTR, 0, STR_END, 0);
12331 if (common->might_be_empty)
12333 empty_match = CMP(SLJIT_EQUAL, STR_PTR, 0, SLJIT_MEM1(SLJIT_SP), OVECTOR(0));
12334 empty_match_found_label = LABEL();
12337 common->accept_label = LABEL();
12338 if (common->accept != NULL)
12339 set_jumps(common->accept, common->accept_label);
12341 /* This means we have a match. Update the ovector. */
12342 copy_ovector(common, re->top_bracket + 1);
12343 common->quit_label = common->abort_label = LABEL();
12344 if (common->quit != NULL)
12345 set_jumps(common->quit, common->quit_label);
12346 if (common->abort != NULL)
12347 set_jumps(common->abort, common->abort_label);
12348 if (minlength_check_failed != NULL)
12349 SET_LABEL(minlength_check_failed, common->abort_label);
12350 sljit_emit_return(compiler, SLJIT_MOV, SLJIT_RETURN_REG, 0);
12352 if (common->failed_match != NULL)
12354 SLJIT_ASSERT(common->mode == PCRE2_JIT_COMPLETE);
12355 set_jumps(common->failed_match, LABEL());
12356 OP1(SLJIT_MOV, SLJIT_RETURN_REG, 0, SLJIT_IMM, PCRE2_ERROR_NOMATCH);
12357 JUMPTO(SLJIT_JUMP, common->abort_label);
12360 if ((re->overall_options & PCRE2_ENDANCHORED) != 0)
12361 JUMPHERE(end_anchor_failed);
12363 if (mode != PCRE2_JIT_COMPLETE)
12365 common->partialmatchlabel = LABEL();
12366 set_jumps(common->partialmatch, common->partialmatchlabel);
12367 return_with_partial_match(common, common->quit_label);
12370 if (common->might_be_empty)
12371 empty_match_backtrack_label = LABEL();
12372 compile_backtrackingpath(common, rootbacktrack.top);
12373 if (SLJIT_UNLIKELY(sljit_get_compiler_error(compiler)))
12375 sljit_free_compiler(compiler);
12376 SLJIT_FREE(common->optimized_cbracket, allocator_data);
12377 SLJIT_FREE(common->private_data_ptrs, allocator_data);
12378 PRIV(jit_free_rodata)(common->read_only_data_head, compiler->allocator_data);
12379 return PCRE2_ERROR_NOMEMORY;
12382 SLJIT_ASSERT(rootbacktrack.prev == NULL);
12383 reset_match_label = LABEL();
12385 if (mode == PCRE2_JIT_PARTIAL_SOFT)
12387 /* Update hit_start only in the first time. */
12388 jump = CMP(SLJIT_NOT_EQUAL, SLJIT_MEM1(SLJIT_SP), common->hit_start, SLJIT_IMM, 0);
12389 OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), common->start_ptr);
12390 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->start_used_ptr, SLJIT_IMM, -1);
12391 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->hit_start, TMP1, 0);
12395 /* Check we have remaining characters. */
12396 if ((re->overall_options & PCRE2_ANCHORED) == 0 && common->match_end_ptr != 0)
12398 OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), common->match_end_ptr);
12401 OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(SLJIT_SP),
12402 (common->fast_forward_bc_ptr != NULL) ? (PRIVATE_DATA(common->fast_forward_bc_ptr + 1)) : common->start_ptr);
12404 if ((re->overall_options & PCRE2_ANCHORED) == 0)
12406 if (common->ff_newline_shortcut != NULL)
12408 /* There cannot be more newlines if PCRE2_FIRSTLINE is set. */
12409 if ((re->overall_options & PCRE2_FIRSTLINE) == 0)
12411 if (common->match_end_ptr != 0)
12413 OP1(SLJIT_MOV, TMP3, 0, STR_END, 0);
12414 OP1(SLJIT_MOV, STR_END, 0, TMP1, 0);
12415 CMPTO(SLJIT_LESS, STR_PTR, 0, TMP1, 0, common->ff_newline_shortcut);
12416 OP1(SLJIT_MOV, STR_END, 0, TMP3, 0);
12419 CMPTO(SLJIT_LESS, STR_PTR, 0, STR_END, 0, common->ff_newline_shortcut);
12423 CMPTO(SLJIT_LESS, STR_PTR, 0, (common->match_end_ptr == 0) ? STR_END : TMP1, 0, mainloop_label);
12426 /* No more remaining characters. */
12427 if (reqbyte_notfound != NULL)
12428 JUMPHERE(reqbyte_notfound);
12430 if (mode == PCRE2_JIT_PARTIAL_SOFT)
12431 CMPTO(SLJIT_NOT_EQUAL, SLJIT_MEM1(SLJIT_SP), common->hit_start, SLJIT_IMM, -1, common->partialmatchlabel);
12433 OP1(SLJIT_MOV, SLJIT_RETURN_REG, 0, SLJIT_IMM, PCRE2_ERROR_NOMATCH);
12434 JUMPTO(SLJIT_JUMP, common->quit_label);
12436 flush_stubs(common);
12438 if (common->might_be_empty)
12440 JUMPHERE(empty_match);
12441 OP1(SLJIT_MOV, TMP1, 0, ARGUMENTS, 0);
12442 OP1(SLJIT_MOV_U32, TMP2, 0, SLJIT_MEM1(TMP1), SLJIT_OFFSETOF(jit_arguments, options));
12443 OP2(SLJIT_AND | SLJIT_SET_Z, SLJIT_UNUSED, 0, TMP2, 0, SLJIT_IMM, PCRE2_NOTEMPTY);
12444 JUMPTO(SLJIT_NOT_ZERO, empty_match_backtrack_label);
12445 OP2(SLJIT_AND | SLJIT_SET_Z, SLJIT_UNUSED, 0, TMP2, 0, SLJIT_IMM, PCRE2_NOTEMPTY_ATSTART);
12446 JUMPTO(SLJIT_ZERO, empty_match_found_label);
12447 OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(TMP1), SLJIT_OFFSETOF(jit_arguments, str));
12448 CMPTO(SLJIT_NOT_EQUAL, TMP2, 0, STR_PTR, 0, empty_match_found_label);
12449 JUMPTO(SLJIT_JUMP, empty_match_backtrack_label);
12452 common->fast_forward_bc_ptr = NULL;
12453 common->fast_fail_start_ptr = 0;
12454 common->fast_fail_end_ptr = 0;
12455 common->currententry = common->entries;
12456 common->local_quit_available = TRUE;
12457 quit_label = common->quit_label;
12458 while (common->currententry != NULL)
12460 /* Might add new entries. */
12461 compile_recurse(common);
12462 if (SLJIT_UNLIKELY(sljit_get_compiler_error(compiler)))
12464 sljit_free_compiler(compiler);
12465 SLJIT_FREE(common->optimized_cbracket, allocator_data);
12466 SLJIT_FREE(common->private_data_ptrs, allocator_data);
12467 PRIV(jit_free_rodata)(common->read_only_data_head, compiler->allocator_data);
12468 return PCRE2_ERROR_NOMEMORY;
12470 flush_stubs(common);
12471 common->currententry = common->currententry->next;
12473 common->local_quit_available = FALSE;
12474 common->quit_label = quit_label;
12476 /* Allocating stack, returns with PCRE_ERROR_JIT_STACKLIMIT if fails. */
12477 /* This is a (really) rare case. */
12478 set_jumps(common->stackalloc, LABEL());
12479 /* RETURN_ADDR is not a saved register. */
12480 sljit_emit_fast_enter(compiler, SLJIT_MEM1(SLJIT_SP), LOCALS0);
12482 SLJIT_ASSERT(TMP1 == SLJIT_R0 && STR_PTR == SLJIT_R1);
12484 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), LOCALS1, STR_PTR, 0);
12485 OP1(SLJIT_MOV, SLJIT_R0, 0, ARGUMENTS, 0);
12486 OP2(SLJIT_SUB, SLJIT_R1, 0, STACK_LIMIT, 0, SLJIT_IMM, STACK_GROWTH_RATE);
12487 OP1(SLJIT_MOV, SLJIT_R0, 0, SLJIT_MEM1(SLJIT_R0), SLJIT_OFFSETOF(jit_arguments, stack));
12488 OP1(SLJIT_MOV, STACK_LIMIT, 0, TMP2, 0);
12490 sljit_emit_icall(compiler, SLJIT_CALL, SLJIT_RET(SW) | SLJIT_ARG1(SW) | SLJIT_ARG2(SW), SLJIT_IMM, SLJIT_FUNC_OFFSET(sljit_stack_resize));
12492 jump = CMP(SLJIT_EQUAL, SLJIT_RETURN_REG, 0, SLJIT_IMM, 0);
12493 OP1(SLJIT_MOV, TMP2, 0, STACK_LIMIT, 0);
12494 OP1(SLJIT_MOV, STACK_LIMIT, 0, SLJIT_RETURN_REG, 0);
12495 OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), LOCALS0);
12496 OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(SLJIT_SP), LOCALS1);
12497 sljit_emit_fast_return(compiler, TMP1, 0);
12499 /* Allocation failed. */
12501 /* We break the return address cache here, but this is a really rare case. */
12502 OP1(SLJIT_MOV, SLJIT_RETURN_REG, 0, SLJIT_IMM, PCRE2_ERROR_JIT_STACKLIMIT);
12503 JUMPTO(SLJIT_JUMP, common->quit_label);
12505 /* Call limit reached. */
12506 set_jumps(common->calllimit, LABEL());
12507 OP1(SLJIT_MOV, SLJIT_RETURN_REG, 0, SLJIT_IMM, PCRE2_ERROR_MATCHLIMIT);
12508 JUMPTO(SLJIT_JUMP, common->quit_label);
12510 if (common->revertframes != NULL)
12512 set_jumps(common->revertframes, LABEL());
12513 do_revertframes(common);
12515 if (common->wordboundary != NULL)
12517 set_jumps(common->wordboundary, LABEL());
12518 check_wordboundary(common);
12520 if (common->anynewline != NULL)
12522 set_jumps(common->anynewline, LABEL());
12523 check_anynewline(common);
12525 if (common->hspace != NULL)
12527 set_jumps(common->hspace, LABEL());
12528 check_hspace(common);
12530 if (common->vspace != NULL)
12532 set_jumps(common->vspace, LABEL());
12533 check_vspace(common);
12535 if (common->casefulcmp != NULL)
12537 set_jumps(common->casefulcmp, LABEL());
12538 do_casefulcmp(common);
12540 if (common->caselesscmp != NULL)
12542 set_jumps(common->caselesscmp, LABEL());
12543 do_caselesscmp(common);
12545 if (common->reset_match != NULL)
12547 set_jumps(common->reset_match, LABEL());
12548 do_reset_match(common, (re->top_bracket + 1) * 2);
12549 CMPTO(SLJIT_GREATER, STR_PTR, 0, TMP1, 0, continue_match_label);
12550 OP1(SLJIT_MOV, STR_PTR, 0, TMP1, 0);
12551 JUMPTO(SLJIT_JUMP, reset_match_label);
12553 #ifdef SUPPORT_UNICODE
12554 #if PCRE2_CODE_UNIT_WIDTH == 8
12555 if (common->utfreadchar != NULL)
12557 set_jumps(common->utfreadchar, LABEL());
12558 do_utfreadchar(common);
12560 if (common->utfreadchar16 != NULL)
12562 set_jumps(common->utfreadchar16, LABEL());
12563 do_utfreadchar16(common);
12565 if (common->utfreadtype8 != NULL)
12567 set_jumps(common->utfreadtype8, LABEL());
12568 do_utfreadtype8(common);
12570 #endif /* PCRE2_CODE_UNIT_WIDTH == 8 */
12571 if (common->getucd != NULL)
12573 set_jumps(common->getucd, LABEL());
12576 #endif /* SUPPORT_UNICODE */
12578 SLJIT_FREE(common->optimized_cbracket, allocator_data);
12579 SLJIT_FREE(common->private_data_ptrs, allocator_data);
12581 executable_func = sljit_generate_code(compiler);
12582 executable_size = sljit_get_generated_code_size(compiler);
12583 label_addr = common->label_addrs;
12584 while (label_addr != NULL)
12586 *label_addr->update_addr = sljit_get_label_addr(label_addr->label);
12587 label_addr = label_addr->next;
12589 sljit_free_compiler(compiler);
12590 if (executable_func == NULL)
12592 PRIV(jit_free_rodata)(common->read_only_data_head, compiler->allocator_data);
12593 return PCRE2_ERROR_NOMEMORY;
12596 /* Reuse the function descriptor if possible. */
12597 if (re->executable_jit != NULL)
12598 functions = (executable_functions *)re->executable_jit;
12601 functions = SLJIT_MALLOC(sizeof(executable_functions), allocator_data);
12602 if (functions == NULL)
12604 /* This case is highly unlikely since we just recently
12605 freed a lot of memory. Not impossible though. */
12606 sljit_free_code(executable_func);
12607 PRIV(jit_free_rodata)(common->read_only_data_head, compiler->allocator_data);
12608 return PCRE2_ERROR_NOMEMORY;
12610 memset(functions, 0, sizeof(executable_functions));
12611 functions->top_bracket = re->top_bracket + 1;
12612 functions->limit_match = re->limit_match;
12613 re->executable_jit = functions;
12616 /* Turn mode into an index. */
12617 if (mode == PCRE2_JIT_COMPLETE)
12620 mode = (mode == PCRE2_JIT_PARTIAL_SOFT) ? 1 : 2;
12622 SLJIT_ASSERT(mode < JIT_NUMBER_OF_COMPILE_MODES);
12623 functions->executable_funcs[mode] = executable_func;
12624 functions->read_only_data_heads[mode] = common->read_only_data_head;
12625 functions->executable_sizes[mode] = executable_size;
12631 /*************************************************
12632 * JIT compile a Regular Expression *
12633 *************************************************/
12635 /* This function used JIT to convert a previously-compiled pattern into machine
12639 code a compiled pattern
12640 options JIT option bits
12642 Returns: 0: success or (*NOJIT) was used
12646 #define PUBLIC_JIT_COMPILE_OPTIONS \
12647 (PCRE2_JIT_COMPLETE|PCRE2_JIT_PARTIAL_SOFT|PCRE2_JIT_PARTIAL_HARD)
12649 PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
12650 pcre2_jit_compile(pcre2_code *code, uint32_t options)
12652 #ifndef SUPPORT_JIT
12656 return PCRE2_ERROR_JIT_BADOPTION;
12658 #else /* SUPPORT_JIT */
12660 pcre2_real_code *re = (pcre2_real_code *)code;
12661 executable_functions *functions;
12665 return PCRE2_ERROR_NULL;
12667 if ((options & ~PUBLIC_JIT_COMPILE_OPTIONS) != 0)
12668 return PCRE2_ERROR_JIT_BADOPTION;
12670 if ((re->flags & PCRE2_NOJIT) != 0) return 0;
12672 functions = (executable_functions *)re->executable_jit;
12674 if ((options & PCRE2_JIT_COMPLETE) != 0 && (functions == NULL
12675 || functions->executable_funcs[0] == NULL)) {
12676 result = jit_compile(code, PCRE2_JIT_COMPLETE);
12681 if ((options & PCRE2_JIT_PARTIAL_SOFT) != 0 && (functions == NULL
12682 || functions->executable_funcs[1] == NULL)) {
12683 result = jit_compile(code, PCRE2_JIT_PARTIAL_SOFT);
12688 if ((options & PCRE2_JIT_PARTIAL_HARD) != 0 && (functions == NULL
12689 || functions->executable_funcs[2] == NULL)) {
12690 result = jit_compile(code, PCRE2_JIT_PARTIAL_HARD);
12697 #endif /* SUPPORT_JIT */
12700 /* JIT compiler uses an all-in-one approach. This improves security,
12701 since the code generator functions are not exported. */
12703 #define INCLUDED_FROM_PCRE2_JIT_COMPILE
12705 #include "pcre2_jit_match.c"
12706 #include "pcre2_jit_misc.c"
12708 /* End of pcre2_jit_compile.c */