2 * Stack-less Just-In-Time compiler
4 * Copyright Zoltan Herczeg (hzmester@freemail.hu). All rights reserved.
6 * Redistribution and use in source and binary forms, with or without modification, are
7 * permitted provided that the following conditions are met:
9 * 1. Redistributions of source code must retain the above copyright notice, this list of
10 * conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
13 * of conditions and the following disclaimer in the documentation and/or other materials
14 * provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) AND CONTRIBUTORS ``AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
19 * SHALL THE COPYRIGHT HOLDER(S) OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
21 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
22 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
24 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #ifdef REGEX_MATCH_VERBOSE
36 /* Extra, hidden flags:
37 {id!} where id > 0 found in the code. */
38 #define REGEX_ID_CHECK 0x100
39 /* When REGEX_NEWLINE && REGEX_MATCH_BEGIN defined, the pattern turn to a normal search,
40 which starts with [\r\n] character range. */
41 #define REGEX_FAKE_MATCH_BEGIN 0x200
42 /* When REGEX_NEWLINE && REGEX_MATCH_END defined, the pattern turn to a normal search,
43 which ends with [\r\n] character range. */
44 #define REGEX_FAKE_MATCH_END 0x400
46 /* --------------------------------------------------------------------- */
47 /* Structures for JIT-ed pattern matching */
48 /* --------------------------------------------------------------------- */
54 /* Number of state descriptors for one term. */
61 sljit_sw (SLJIT_FUNC
*call_init
)(void *next
, void* match
);
63 #if (defined SLJIT_INDIRECT_CALL && SLJIT_INDIRECT_CALL)
64 struct sljit_function_context context
;
69 /* Variable sized array to contain the handler addresses. */
70 sljit_uw entry_addrs
[1];
75 /* Current and next state array. */
80 /* String character index (ever increasing). */
82 /* Best match found so far (members in priority order). */
86 /* Bool flags (encoded as word). */
88 sljit_sw fast_forward
;
90 struct regex_machine
*machine
;
94 void (SLJIT_FUNC
*call_continue
)(struct regex_match
*match
, const regex_char_t
*input_string
, int length
);
97 /* Variable sized array to contain the state arrays. */
102 ITEM[0] - pointer to the address inside the machine code
103 ITEM[1] - next pointer
104 ITEM[2] - string started from (optional)
105 ITEM[3] - max ID (optional) */
107 /* Register allocation. */
108 /* Current state array (loaded & stored: regex_match->current). */
109 #define R_CURR_STATE SLJIT_S0
110 /* Next state array (loaded & stored: regex_match->next). */
111 #define R_NEXT_STATE SLJIT_S1
112 /* Head (loaded & stored: regex_match->head). */
113 #define R_NEXT_HEAD SLJIT_S2
114 /* String fragment pointer. */
115 #define R_STRING SLJIT_S3
116 /* String fragment length. */
117 #define R_LENGTH SLJIT_S4
118 /* 'struct regex_match*' */
119 #define R_REGEX_MATCH SLJIT_R0
120 /* Current character. */
121 #define R_CURR_CHAR SLJIT_R1
122 /* Temporary register. */
123 #define R_TEMP SLJIT_R2
124 /* Caches the regex_match->best_begin. */
125 #define R_BEST_BEGIN SLJIT_R3
126 /* Current character index. */
127 #define R_CURR_INDEX SLJIT_R4
129 /* --------------------------------------------------------------------- */
130 /* Stack management */
131 /* --------------------------------------------------------------------- */
133 /* Try to allocate 2^n blocks. */
134 #define STACK_FRAGMENT_SIZE (((64 * sizeof(struct stack_item)) - (sizeof(struct stack_fragment_data))) / (sizeof(struct stack_item)))
141 struct stack_fragment_data
{
142 struct stack_fragment
*next
;
143 struct stack_fragment
*prev
;
146 struct stack_fragment
{
147 struct stack_fragment_data data
;
148 struct stack_item items
[STACK_FRAGMENT_SIZE
];
152 struct stack_fragment
*first
;
153 struct stack_fragment
*last
;
158 #if (defined SLJIT_DEBUG && SLJIT_DEBUG)
160 static void stack_check(struct stack
*stack
)
162 struct stack_fragment
*curr
;
168 SLJIT_ASSERT(stack
->index
>= 0 && stack
->index
< STACK_FRAGMENT_SIZE
);
170 if (stack
->first
== NULL
) {
171 SLJIT_ASSERT(stack
->first
== NULL
&& stack
->last
== NULL
);
172 SLJIT_ASSERT(stack
->index
== STACK_FRAGMENT_SIZE
- 1 && stack
->count
== 0);
177 if (stack
->last
== NULL
) {
178 SLJIT_ASSERT(stack
->index
== STACK_FRAGMENT_SIZE
- 1 && stack
->count
== 0);
182 SLJIT_ASSERT(stack
->index
>= 0 && stack
->count
>= 0);
184 SLJIT_ASSERT(stack
->first
->data
.prev
== NULL
);
187 if (curr
== stack
->last
)
190 SLJIT_ASSERT(curr
->data
.next
->data
.prev
== curr
);
191 curr
= curr
->data
.next
;
198 static void stack_init(struct stack
*stack
)
202 stack
->index
= STACK_FRAGMENT_SIZE
- 1;
206 static void stack_destroy(struct stack
*stack
)
208 struct stack_fragment
*curr
= stack
->first
;
209 struct stack_fragment
*prev
;
211 #if (defined SLJIT_DEBUG && SLJIT_DEBUG)
217 curr
= curr
->data
.next
;
218 SLJIT_FREE(prev
, NULL
);
222 static SLJIT_INLINE
struct stack_item
* stack_top(struct stack
*stack
)
224 SLJIT_ASSERT(stack
->last
);
225 return stack
->last
->items
+ stack
->index
;
228 static int stack_push(struct stack
*stack
, int type
, int value
)
232 if (stack
->index
>= STACK_FRAGMENT_SIZE
) {
234 if (!stack
->last
->data
.next
) {
235 stack
->last
->data
.next
= (struct stack_fragment
*)SLJIT_MALLOC(sizeof(struct stack_fragment
), NULL
);
236 if (!stack
->last
->data
.next
)
238 stack
->last
->data
.next
->data
.next
= NULL
;
239 stack
->last
->data
.next
->data
.prev
= stack
->last
;
241 stack
->last
= stack
->last
->data
.next
;
244 else if (!stack
->first
) {
245 stack
->last
= (struct stack_fragment
*)SLJIT_MALLOC(sizeof(struct stack_fragment
), NULL
);
248 stack
->last
->data
.prev
= NULL
;
249 stack
->last
->data
.next
= NULL
;
250 stack
->first
= stack
->last
;
254 stack
->last
= stack
->first
;
257 stack
->last
->items
[stack
->index
].type
= type
;
258 stack
->last
->items
[stack
->index
].value
= value
;
260 #if (defined SLJIT_DEBUG && SLJIT_DEBUG)
266 static struct stack_item
* stack_pop(struct stack
*stack
)
268 struct stack_item
*ret
= stack_top(stack
);
270 if (stack
->index
> 0)
273 stack
->last
= stack
->last
->data
.prev
;
274 stack
->index
= STACK_FRAGMENT_SIZE
- 1;
278 #if (defined SLJIT_DEBUG && SLJIT_DEBUG)
284 static SLJIT_INLINE
void stack_clone(struct stack
*src
, struct stack
*dst
)
289 static int stack_push_copy(struct stack
*stack
, int items
, int length
)
291 struct stack_fragment
*frag1
;
293 struct stack_fragment
*frag2
;
297 SLJIT_ASSERT(stack
->count
>= length
&& items
<= length
&& items
> 0);
299 /* Allocate the necessary elements. */
303 while (counter
> 0) {
304 if (stack
->index
+ counter
>= STACK_FRAGMENT_SIZE
) {
305 counter
-= STACK_FRAGMENT_SIZE
- stack
->index
- 1 + 1;
307 if (!stack
->last
->data
.next
) {
308 stack
->last
->data
.next
= (struct stack_fragment
*)SLJIT_MALLOC(sizeof(struct stack_fragment
), NULL
);
309 if (!stack
->last
->data
.next
)
311 stack
->last
->data
.next
->data
.next
= NULL
;
312 stack
->last
->data
.next
->data
.prev
= stack
->last
;
314 stack
->last
= stack
->last
->data
.next
;
317 stack
->index
+= counter
;
325 frag2
->items
[ind2
--] = frag1
->items
[ind1
--];
327 ind1
= STACK_FRAGMENT_SIZE
- 1;
328 frag1
= frag1
->data
.prev
;
331 ind2
= STACK_FRAGMENT_SIZE
- 1;
332 frag2
= frag2
->data
.prev
;
337 #if (defined SLJIT_DEBUG && SLJIT_DEBUG)
340 stack
->count
+= items
;
344 /* --------------------------------------------------------------------- */
346 /* --------------------------------------------------------------------- */
361 /* generator only. */
374 struct compiler_common
{
375 /* Temporary stacks. */
380 /* Encoded size of the dfa representation. */
382 /* Number of terms. */
384 /* Number of state descriptors for one term (same as machine->no_states). */
386 /* Number of type_rng_(char|left)-s in the longest character range. */
387 sljit_sw longest_range_size
;
389 /* DFA linear representation (size: dfa_size). */
390 struct stack_item
*dfa_transitions
;
391 /* Term id and search state pairs (size: dfa_size). */
392 struct stack_item
*search_states
;
395 struct sljit_compiler
*compiler
;
396 /* Machine data, which must be kept for later use. */
397 struct regex_machine
*machine
;
398 /* Temporary space for jumps (size: longest_range_size). */
399 struct sljit_jump
**range_jump_list
;
402 static const regex_char_t
* decode_number(const regex_char_t
*regex_string
, int length
, int *result
)
406 SLJIT_ASSERT(length
> 0);
407 if (*regex_string
< '0' || *regex_string
> '9') {
412 while (length
> 0 && *regex_string
>= '0' && *regex_string
<= '9') {
413 value
= value
* 10 + (*regex_string
- '0');
422 static int iterate(struct stack
*stack
, int min
, int max
)
425 struct stack_item
*item
;
430 stack_clone(stack
, &it
);
432 /* Calculate size. */
434 item
= stack_pop(&it
);
435 switch (item
->type
) {
442 case type_qestion_mark
:
455 SLJIT_ASSERT(depth
> 0);
462 SLJIT_ASSERT(depth
> 0);
467 SLJIT_ASSERT(item
->type
!= type_begin
&& item
->type
!= type_end
);
475 if (min
== 0 && max
== 0) {
476 /* {0,0} case, not {0,} case: delete subtree. */
477 stack_clone(&it
, stack
);
478 /* and put an empty bracket expression instead of it. */
479 if (stack_push(stack
, type_open_br
, 0))
480 return REGEX_MEMORY_ERROR
;
481 if (stack_push(stack
, type_close_br
, 0))
482 return REGEX_MEMORY_ERROR
;
486 count
= stack
->count
- count
;
488 /* Put an open bracket before the sequence. */
489 if (stack_push_copy(stack
, 1, count
))
492 #if (defined SLJIT_DEBUG && SLJIT_DEBUG)
493 SLJIT_ASSERT(stack_push(&it
, type_open_br
, 0) == 0);
495 stack_push(&it
, type_open_br
, 0);
500 len
= len
* (max
- 1);
502 /* Insert ? operators. */
508 if (stack_push_copy(stack
, count
, count
))
513 if (stack_push_copy(stack
, count
, count
))
515 if (stack_push(stack
, type_qestion_mark
, 0))
516 return REGEX_MEMORY_ERROR
;
522 SLJIT_ASSERT(max
> 0);
525 if (stack_push(stack
, type_qestion_mark
, 0))
526 return REGEX_MEMORY_ERROR
;
530 if (stack_push_copy(stack
, count
, count
))
536 SLJIT_ASSERT(min
> 0);
538 /* Insert + operator. */
541 if (stack_push_copy(stack
, count
, count
))
546 if (stack_push(stack
, type_plus_sign
, 0))
547 return REGEX_MEMORY_ERROR
;
550 /* Close the opened bracket. */
551 if (stack_push(stack
, type_close_br
, 0))
552 return REGEX_MEMORY_ERROR
;
557 static int parse_iterator(const regex_char_t
*regex_string
, int length
, struct stack
*stack
, sljit_sw
*dfa_size
, int begin
)
559 /* We only know that *regex_string == { . */
561 const regex_char_t
*base_from
= regex_string
;
562 const regex_char_t
*from
;
567 /* Decode left value. */
571 if (*regex_string
== ',') {
578 regex_string
= decode_number(regex_string
, length
, &val1
);
581 length
-= regex_string
- from
;
585 if (*regex_string
== '}') {
590 else if (length
>= 2 && *regex_string
== '!' && regex_string
[1] == '}') {
591 /* Non posix extension. */
592 if (stack_push(stack
, type_id
, val1
))
595 return (regex_string
- base_from
) + 1;
598 if (*regex_string
!= ',')
608 /* Decode right value. */
612 if (*regex_string
== '}')
616 regex_string
= decode_number(regex_string
, length
, &val2
);
617 length
-= regex_string
- from
;
618 if (val2
< 0 || length
== 0 || *regex_string
!= '}' || val2
< val1
)
621 SLJIT_ASSERT(val1
== 0);
628 if (val1
> 1 || val2
> 1) {
629 val1
= iterate(stack
, val1
, val2
);
634 else if (val1
== 0 && val2
== 0) {
635 if (stack_push(stack
, type_asterisk
, 0))
639 else if (val1
== 1 && val2
== 0) {
640 if (stack_push(stack
, type_plus_sign
, 0))
644 else if (val1
== 0 && val2
== 1) {
645 if (stack_push(stack
, type_qestion_mark
, 0))
649 else if (val1
== -1) {
650 val1
= iterate(stack
, 0, 0);
654 SLJIT_ASSERT(*dfa_size
>= 2);
658 SLJIT_ASSERT(val1
== 1 && val2
== 1);
660 return regex_string
- base_from
;
663 static int parse_char_range(const regex_char_t
*regex_string
, int length
, struct compiler_common
*compiler_common
)
665 struct stack
* stack
= &compiler_common
->stack
;
666 const regex_char_t
*base_from
= regex_string
;
667 regex_char_t left_char
, right_char
, tmp_char
;
675 if (*regex_string
!= '^') {
676 if (stack_push(stack
, type_rng_start
, 0))
686 if (stack_push(stack
, type_rng_start
, 1))
689 /* For both the type_rng_start & type_rng_end. */
690 compiler_common
->dfa_size
+= 2;
692 /* Range must be at least 1 character. */
693 if (*regex_string
== ']') {
696 if (stack_push(stack
, type_rng_char
, ']'))
698 compiler_common
->dfa_size
++;
705 if (*regex_string
== ']')
708 if (*regex_string
!= '\\')
709 left_char
= *regex_string
;
715 left_char
= *regex_string
;
720 /* Is a range here? */
721 if (length
>= 3 && *regex_string
== '-' && *(regex_string
+ 1) != ']') {
725 if (*regex_string
!= '\\')
726 right_char
= *regex_string
;
732 right_char
= *regex_string
;
737 if (left_char
> right_char
) {
738 /* Swap if necessary. */
739 tmp_char
= left_char
;
740 left_char
= right_char
;
741 right_char
= tmp_char
;
744 if (stack_push(stack
, type_rng_left
, left_char
))
746 if (stack_push(stack
, type_rng_right
, right_char
))
748 compiler_common
->dfa_size
+= 2;
751 if (stack_push(stack
, type_rng_char
, left_char
))
753 compiler_common
->dfa_size
++;
757 if (stack_push(stack
, type_rng_end
, 0))
759 return regex_string
- base_from
;
762 static int parse(const regex_char_t
*regex_string
, int length
, struct compiler_common
*compiler_common
)
764 /* Depth of bracketed expressions. */
766 /* Have we already found a term? '1' if not yet. */
768 /* Cache stack pointer. */
769 struct stack
* stack
= &compiler_common
->stack
;
772 /* Type_begin and type_end. */
773 compiler_common
->dfa_size
= 2;
775 if (stack_push(stack
, type_begin
, 0))
776 return REGEX_MEMORY_ERROR
;
778 if (length
> 0 && *regex_string
== '^') {
779 compiler_common
->flags
|= REGEX_MATCH_BEGIN
;
784 if ((compiler_common
->flags
& (REGEX_MATCH_BEGIN
| REGEX_NEWLINE
)) == (REGEX_MATCH_BEGIN
| REGEX_NEWLINE
)) {
785 /* Replace REGEX_MATCH_BEGIN flag to REGEX_FAKE_MATCH_BEGIN */
786 compiler_common
->flags
&= ~REGEX_MATCH_BEGIN
;
787 compiler_common
->flags
|= REGEX_FAKE_MATCH_BEGIN
;
788 /* and append a new-line search. */
789 if (stack_push(stack
, type_newline
, 0))
790 return REGEX_MEMORY_ERROR
;
791 compiler_common
->dfa_size
++;
792 /* Begin intentionally kept as 1. */
796 switch (*regex_string
) {
801 return REGEX_INVALID_REGEX
;
802 if (stack_push(stack
, type_char
, *regex_string
))
803 return REGEX_MEMORY_ERROR
;
805 compiler_common
->dfa_size
++;
809 if (stack_push(stack
, type_rng_start
, 1))
810 return REGEX_MEMORY_ERROR
;
811 if (compiler_common
->flags
& REGEX_NEWLINE
) {
812 if (stack_push(stack
, type_rng_char
, '\n'))
813 return REGEX_MEMORY_ERROR
;
814 if (stack_push(stack
, type_rng_char
, '\r'))
815 return REGEX_MEMORY_ERROR
;
816 compiler_common
->dfa_size
+= 2;
818 if (stack_push(stack
, type_rng_end
, 1))
819 return REGEX_MEMORY_ERROR
;
821 compiler_common
->dfa_size
+= 2;
826 if (stack_push(stack
, type_open_br
, 0))
827 return REGEX_MEMORY_ERROR
;
833 return REGEX_INVALID_REGEX
;
835 if (stack_push(stack
, type_close_br
, 0))
836 return REGEX_MEMORY_ERROR
;
841 if (stack_push(stack
, type_select
, 0))
842 return REGEX_MEMORY_ERROR
;
844 compiler_common
->dfa_size
+= 2;
849 return REGEX_INVALID_REGEX
;
850 if (stack_push(stack
, type_asterisk
, 0))
851 return REGEX_MEMORY_ERROR
;
852 compiler_common
->dfa_size
+= 2;
858 return REGEX_INVALID_REGEX
;
859 if (stack_push(stack
, (*regex_string
== '+') ? type_plus_sign
: type_qestion_mark
, 0))
860 return REGEX_MEMORY_ERROR
;
861 compiler_common
->dfa_size
++;
865 tmp
= parse_iterator(regex_string
, length
, stack
, &compiler_common
->dfa_size
, begin
);
872 return REGEX_MEMORY_ERROR
;
874 /* Not a valid range expression. */
875 SLJIT_ASSERT(tmp
== -2);
876 if (stack_push(stack
, type_char
, '{'))
877 return REGEX_MEMORY_ERROR
;
878 compiler_common
->dfa_size
++;
883 tmp
= parse_char_range(regex_string
, length
, compiler_common
);
889 return REGEX_MEMORY_ERROR
;
891 SLJIT_ASSERT(tmp
== -2);
892 return REGEX_INVALID_REGEX
;
898 if (length
== 1 && *regex_string
== '$') {
899 compiler_common
->flags
|= REGEX_MATCH_END
;
902 if (stack_push(stack
, type_char
, *regex_string
))
903 return REGEX_MEMORY_ERROR
;
905 compiler_common
->dfa_size
++;
913 return REGEX_INVALID_REGEX
;
915 if ((compiler_common
->flags
& (REGEX_MATCH_END
| REGEX_NEWLINE
)) == (REGEX_MATCH_END
| REGEX_NEWLINE
)) {
916 /* Replace REGEX_MATCH_END flag to REGEX_FAKE_MATCH_END */
917 compiler_common
->flags
&= ~REGEX_MATCH_END
;
918 compiler_common
->flags
|= REGEX_FAKE_MATCH_END
;
919 /* and append a new-line search. */
920 if (stack_push(stack
, type_newline
, 1))
921 return REGEX_MEMORY_ERROR
;
922 compiler_common
->dfa_size
++;
923 /* Begin intentionally kept as 1. */
926 if (stack_push(stack
, type_end
, 0))
927 return REGEX_MEMORY_ERROR
;
929 return REGEX_NO_ERROR
;
932 /* --------------------------------------------------------------------- */
933 /* Generating machine state transitions */
934 /* --------------------------------------------------------------------- */
936 #define PUT_TRANSITION(typ, val) \
939 transitions_ptr->type = typ; \
940 transitions_ptr->value = val; \
943 static struct stack_item
* handle_iteratives(struct stack_item
*transitions_ptr
, struct stack_item
*transitions
, struct stack
*depth
)
945 struct stack_item
*item
;
948 item
= stack_top(depth
);
950 switch (item
->type
) {
952 SLJIT_ASSERT(transitions
[item
->value
].type
== type_branch
);
953 transitions
[item
->value
].value
= transitions_ptr
- transitions
;
954 PUT_TRANSITION(type_branch
, item
->value
+ 1);
958 SLJIT_ASSERT(transitions
[item
->value
].type
== type_branch
);
959 transitions
[item
->value
].value
= transitions_ptr
- transitions
;
962 case type_qestion_mark
:
963 PUT_TRANSITION(type_branch
, item
->value
);
967 return transitions_ptr
;
973 static int generate_transitions(struct compiler_common
*compiler_common
)
975 struct stack
*stack
= &compiler_common
->stack
;
976 struct stack
*depth
= &compiler_common
->depth
;
977 struct stack_item
*transitions_ptr
;
978 struct stack_item
*item
;
981 compiler_common
->dfa_transitions
= SLJIT_MALLOC(sizeof(struct stack_item
) * compiler_common
->dfa_size
, NULL
);
982 if (!compiler_common
->dfa_transitions
)
983 return REGEX_MEMORY_ERROR
;
985 /* Go through the items of the stack and generate the necessary branches and jumps (edges of DFA). */
986 transitions_ptr
= compiler_common
->dfa_transitions
+ compiler_common
->dfa_size
;
987 while (stack
->count
> 0) {
988 item
= stack_pop(stack
);
989 switch (item
->type
) {
992 item
= stack_pop(depth
);
993 if (item
->type
== type_select
)
994 PUT_TRANSITION(type_branch
, item
->value
+ 1);
996 SLJIT_ASSERT(item
->type
== type_close_br
);
997 if (stack
->count
== 0)
998 PUT_TRANSITION(type_begin
, 0);
1000 transitions_ptr
= handle_iteratives(transitions_ptr
, compiler_common
->dfa_transitions
, depth
);
1005 if (item
->type
== type_end
)
1006 *--transitions_ptr
= *item
;
1007 if (stack_push(depth
, type_close_br
, transitions_ptr
- compiler_common
->dfa_transitions
))
1008 return REGEX_MEMORY_ERROR
;
1012 item
= stack_top(depth
);
1013 if (item
->type
== type_select
) {
1014 SLJIT_ASSERT(compiler_common
->dfa_transitions
[item
->value
].type
== type_jump
);
1015 PUT_TRANSITION(type_branch
, item
->value
+ 1);
1016 PUT_TRANSITION(type_jump
, item
->value
);
1017 item
->value
= transitions_ptr
- compiler_common
->dfa_transitions
;
1020 SLJIT_ASSERT(item
->type
== type_close_br
);
1021 item
->type
= type_select
;
1022 PUT_TRANSITION(type_jump
, item
->value
);
1023 item
->value
= transitions_ptr
- compiler_common
->dfa_transitions
;
1028 case type_plus_sign
:
1029 case type_qestion_mark
:
1030 if (item
->type
!= type_qestion_mark
)
1031 PUT_TRANSITION(type_branch
, 0);
1032 if (stack_push(depth
, item
->type
, transitions_ptr
- compiler_common
->dfa_transitions
))
1033 return REGEX_MEMORY_ERROR
;
1038 case type_rng_start
:
1039 /* Requires handle_iteratives. */
1040 *--transitions_ptr
= *item
;
1041 transitions_ptr
= handle_iteratives(transitions_ptr
, compiler_common
->dfa_transitions
, depth
);
1045 *--transitions_ptr
= *item
;
1050 SLJIT_ASSERT(compiler_common
->dfa_transitions
== transitions_ptr
);
1051 SLJIT_ASSERT(depth
->count
== 0);
1052 return REGEX_NO_ERROR
;
1055 #undef PUT_TRANSITION
1057 #ifdef REGEX_MATCH_VERBOSE
1059 static void verbose_transitions(struct compiler_common
*compiler_common
)
1061 struct stack_item
*transitions_ptr
= compiler_common
->dfa_transitions
;
1062 struct stack_item
*transitions_end
= transitions_ptr
+ compiler_common
->dfa_size
;
1063 struct stack_item
*search_states_ptr
= compiler_common
->search_states
;
1066 printf("-----------------\nTransitions\n-----------------\n");
1068 while (transitions_ptr
< transitions_end
) {
1069 printf("[%3d] ", pos
++);
1070 if (search_states_ptr
->type
>= 0)
1071 printf("(%3d) ", search_states_ptr
->type
);
1072 switch (transitions_ptr
->type
) {
1074 printf("type_begin\n");
1078 printf("type_end\n");
1082 if (transitions_ptr
->value
>= ' ')
1083 printf("type_char '%c'\n", transitions_ptr
->value
);
1085 printf("type_char 0x%x\n", transitions_ptr
->value
);
1089 printf("type_newline %s\n", transitions_ptr
->value
? "(end)" : "(begin)");
1093 printf("type_id %d\n", transitions_ptr
->value
);
1096 case type_rng_start
:
1097 printf("type_rng_start %s\n", transitions_ptr
->value
? "(invert)" : "(normal)");
1101 printf("type_rng_end\n");
1105 if (transitions_ptr
->value
>= ' ')
1106 printf("type_rng_char '%c'\n", transitions_ptr
->value
);
1108 printf("type_rng_char 0x%x\n", transitions_ptr
->value
);
1112 if (transitions_ptr
->value
>= ' ')
1113 printf("type_rng_left '%c'\n", transitions_ptr
->value
);
1115 printf("type_rng_left 0x%x\n", transitions_ptr
->value
);
1118 case type_rng_right
:
1119 if (transitions_ptr
->value
>= ' ')
1120 printf("type_rng_right '%c'\n", transitions_ptr
->value
);
1122 printf("type_rng_right 0x%x\n", transitions_ptr
->value
);
1126 printf("type_branch -> %d\n", transitions_ptr
->value
);
1130 printf("type_jump -> %d\n", transitions_ptr
->value
);
1134 printf("UNEXPECTED TYPE\n");
1138 search_states_ptr
++;
1141 if (!(compiler_common
->flags
& (REGEX_MATCH_BEGIN
| REGEX_MATCH_END
| REGEX_NEWLINE
| REGEX_ID_CHECK
| REGEX_FAKE_MATCH_BEGIN
| REGEX_FAKE_MATCH_END
)))
1143 if (compiler_common
->flags
& REGEX_MATCH_BEGIN
)
1144 printf("REGEX_MATCH_BEGIN ");
1145 if (compiler_common
->flags
& REGEX_MATCH_END
)
1146 printf("REGEX_MATCH_END ");
1147 if (compiler_common
->flags
& REGEX_NEWLINE
)
1148 printf("REGEX_NEWLINE ");
1149 if (compiler_common
->flags
& REGEX_ID_CHECK
)
1150 printf("REGEX_ID_CHECK ");
1151 if (compiler_common
->flags
& REGEX_FAKE_MATCH_BEGIN
)
1152 printf("REGEX_FAKE_MATCH_BEGIN ");
1153 if (compiler_common
->flags
& REGEX_FAKE_MATCH_END
)
1154 printf("REGEX_FAKE_MATCH_END ");
1155 if (compiler_common
->longest_range_size
> 0)
1156 printf("(longest range: %ld) ", (long)compiler_common
->longest_range_size
);
1162 /* --------------------------------------------------------------------- */
1164 /* --------------------------------------------------------------------- */
1166 static int generate_search_states(struct compiler_common
*compiler_common
)
1168 struct stack_item
*transitions_ptr
= compiler_common
->dfa_transitions
;
1169 struct stack_item
*transitions_end
= transitions_ptr
+ compiler_common
->dfa_size
;
1170 struct stack_item
*search_states_ptr
;
1171 struct stack_item
*rng_start
= NULL
;
1173 compiler_common
->terms_size
= !(compiler_common
->flags
& REGEX_FAKE_MATCH_END
) ? 1 : 2;
1174 compiler_common
->longest_range_size
= 0;
1175 compiler_common
->search_states
= SLJIT_MALLOC(sizeof(struct stack_item
) * compiler_common
->dfa_size
, NULL
);
1176 if (!compiler_common
->search_states
)
1177 return REGEX_MEMORY_ERROR
;
1179 search_states_ptr
= compiler_common
->search_states
;
1180 while (transitions_ptr
< transitions_end
) {
1181 switch (transitions_ptr
->type
) {
1184 search_states_ptr
->type
= 0;
1188 search_states_ptr
->type
= compiler_common
->terms_size
++;
1192 if (transitions_ptr
->value
)
1193 search_states_ptr
->type
= 1;
1195 search_states_ptr
->type
= compiler_common
->terms_size
++;
1196 SLJIT_ASSERT(search_states_ptr
->type
== 1 || search_states_ptr
->type
== 2);
1200 if (transitions_ptr
->value
> 0)
1201 compiler_common
->flags
|= REGEX_ID_CHECK
;
1202 search_states_ptr
->type
= -1;
1205 case type_rng_start
:
1206 search_states_ptr
->type
= compiler_common
->terms_size
;
1207 rng_start
= search_states_ptr
;
1211 search_states_ptr
->type
= compiler_common
->terms_size
++;
1212 /* Ok, this is a blunt over estimation :) */
1213 if (compiler_common
->longest_range_size
< search_states_ptr
- rng_start
)
1214 compiler_common
->longest_range_size
= search_states_ptr
- rng_start
;
1218 search_states_ptr
->type
= -1;
1221 search_states_ptr
->value
= -1;
1222 search_states_ptr
++;
1225 return REGEX_NO_ERROR
;
1228 static int trace_transitions(int from
, struct compiler_common
*compiler_common
)
1231 struct stack
*stack
= &compiler_common
->stack
;
1232 struct stack
*depth
= &compiler_common
->depth
;
1233 struct stack_item
*dfa_transitions
= compiler_common
->dfa_transitions
;
1234 struct stack_item
*search_states
= compiler_common
->search_states
;
1236 SLJIT_ASSERT(search_states
[from
].type
>= 0);
1240 /* Be prepared for any paths (loops, etc). */
1242 if (dfa_transitions
[from
].type
== type_id
)
1243 if (id
< dfa_transitions
[from
].value
)
1244 id
= dfa_transitions
[from
].value
;
1246 if (search_states
[from
].value
< id
) {
1248 if (search_states
[from
].value
== -1)
1249 if (stack_push(stack
, 0, from
))
1250 return REGEX_MEMORY_ERROR
;
1251 search_states
[from
].value
= id
;
1253 if (dfa_transitions
[from
].type
== type_branch
) {
1254 if (stack_push(depth
, id
, from
))
1255 return REGEX_MEMORY_ERROR
;
1259 else if (dfa_transitions
[from
].type
== type_jump
) {
1260 from
= dfa_transitions
[from
].value
;
1263 else if (search_states
[from
].type
< 0) {
1269 /* Back tracking. */
1270 if (depth
->count
> 0) {
1271 id
= stack_top(depth
)->type
;
1272 from
= dfa_transitions
[stack_pop(depth
)->value
].value
;
1279 /* --------------------------------------------------------------------- */
1280 /* Code generator */
1281 /* --------------------------------------------------------------------- */
1283 #define TERM_OFFSET_OF(index, offs) (((index) * no_states + (offs)) * sizeof(sljit_sw))
1284 #define TERM_REL_OFFSET_OF(base, offs) ((base) + ((offs) * sizeof(sljit_sw)))
1286 #define EMIT_OP1(type, arg1, arg2, arg3, arg4) \
1287 CHECK(sljit_emit_op1(compiler, type, arg1, arg2, arg3, arg4))
1289 #define EMIT_OP2(type, arg1, arg2, arg3, arg4, arg5, arg6) \
1290 CHECK(sljit_emit_op2(compiler, type, arg1, arg2, arg3, arg4, arg5, arg6))
1292 #define EMIT_LABEL(label) \
1293 label = sljit_emit_label(compiler); \
1296 #define EMIT_JUMP(jump, type) \
1297 jump = sljit_emit_jump(compiler, type); \
1300 #define EMIT_CMP(jump, type, arg1, arg2, arg3, arg4) \
1301 jump = sljit_emit_cmp(compiler, type, arg1, arg2, arg3, arg4); \
1304 /* CHECK depends on the use case. */
1306 #define CHECK(exp) \
1307 if (SLJIT_UNLIKELY(exp)) \
1308 return REGEX_MEMORY_ERROR
1310 static int compile_uncond_tran(struct compiler_common
*compiler_common
, int reg
)
1312 struct sljit_compiler
*compiler
= compiler_common
->compiler
;
1313 struct stack
*stack
= &compiler_common
->stack
;
1314 struct stack_item
*search_states
= compiler_common
->search_states
;
1315 int flags
= compiler_common
->flags
;
1316 sljit_sw no_states
= compiler_common
->no_states
;
1318 sljit_sw offset
, value
;
1320 if (reg
!= R_CURR_STATE
|| !(compiler_common
->flags
& REGEX_FAKE_MATCH_BEGIN
)) {
1321 CHECK(trace_transitions(0, compiler_common
));
1324 CHECK(trace_transitions(1, compiler_common
));
1327 while (stack
->count
> 0) {
1328 value
= stack_pop(stack
)->value
;
1329 if (search_states
[value
].type
>= 0) {
1330 offset
= TERM_OFFSET_OF(search_states
[value
].type
, 0);
1331 EMIT_OP1(SLJIT_MOV
, SLJIT_MEM1(reg
), TERM_REL_OFFSET_OF(offset
, 1), SLJIT_IMM
, head
);
1335 if (!(flags
& REGEX_MATCH_BEGIN
)) {
1336 EMIT_OP1(SLJIT_MOV
, SLJIT_MEM1(reg
), TERM_REL_OFFSET_OF(offset
, 2), R_TEMP
, 0);
1337 if (flags
& REGEX_ID_CHECK
) {
1338 EMIT_OP1(SLJIT_MOV
, SLJIT_MEM1(reg
), TERM_REL_OFFSET_OF(offset
, 3), SLJIT_IMM
, search_states
[value
].value
);
1341 else if (flags
& REGEX_ID_CHECK
) {
1342 EMIT_OP1(SLJIT_MOV
, SLJIT_MEM1(reg
), TERM_REL_OFFSET_OF(offset
, 2), SLJIT_IMM
, search_states
[value
].value
);
1345 search_states
[value
].value
= -1;
1347 if (reg
== R_NEXT_STATE
) {
1348 EMIT_OP1(SLJIT_MOV
, R_TEMP
, 0, R_NEXT_HEAD
, 0);
1350 else if (flags
& REGEX_FAKE_MATCH_BEGIN
) {
1351 SLJIT_ASSERT(compiler_common
->dfa_transitions
[1].type
== type_newline
&& !compiler_common
->dfa_transitions
[1].value
);
1352 offset
= TERM_OFFSET_OF(search_states
[1].type
, 0);
1354 SLJIT_ASSERT(!(flags
& REGEX_MATCH_BEGIN
));
1356 EMIT_OP1(SLJIT_MOV
, SLJIT_MEM1(reg
), TERM_REL_OFFSET_OF(offset
, 1), SLJIT_IMM
, head
);
1359 EMIT_OP1(SLJIT_MOV
, SLJIT_MEM1(reg
), TERM_REL_OFFSET_OF(offset
, 2), SLJIT_IMM
, 1);
1360 if (flags
& REGEX_ID_CHECK
) {
1361 EMIT_OP1(SLJIT_MOV
, SLJIT_MEM1(reg
), TERM_REL_OFFSET_OF(offset
, 3), SLJIT_IMM
, 0);
1364 EMIT_OP1(SLJIT_MOV
, R_NEXT_HEAD
, 0, SLJIT_IMM
, head
);
1365 return REGEX_NO_ERROR
;
1368 static int compile_cond_tran(struct compiler_common
*compiler_common
, sljit_sw curr_index
)
1370 struct sljit_compiler
*compiler
= compiler_common
->compiler
;
1371 struct stack
*stack
= &compiler_common
->stack
;
1372 struct stack_item
*search_states
= compiler_common
->search_states
;
1377 struct sljit_jump
*jump1
;
1378 struct sljit_jump
*jump2
;
1379 struct sljit_jump
*jump3
;
1380 struct sljit_jump
*jump4
;
1381 struct sljit_jump
*jump5
;
1382 struct sljit_label
*label1
;
1384 flags
= compiler_common
->flags
;
1385 no_states
= compiler_common
->no_states
;
1387 EMIT_OP1(SLJIT_MOV
, SLJIT_MEM1(R_REGEX_MATCH
), SLJIT_OFFSETOF(struct regex_match
, fast_forward
), SLJIT_IMM
, 0);
1388 if (!(flags
& (REGEX_ID_CHECK
| REGEX_MATCH_BEGIN
))) {
1389 EMIT_OP1(SLJIT_MOV
, R_TEMP
, 0, SLJIT_MEM1(R_CURR_STATE
), TERM_OFFSET_OF(curr_index
, 2));
1392 while (stack
->count
> 0) {
1393 value
= stack_pop(stack
)->value
;
1394 if (search_states
[value
].type
>= 0) {
1395 #ifdef REGEX_MATCH_VERBOSE
1396 if (flags
& REGEX_MATCH_VERBOSE
)
1397 printf("-> (%3d:%3d) ", search_states
[value
].type
, search_states
[value
].value
);
1399 offset
= TERM_OFFSET_OF(search_states
[value
].type
, 0);
1401 if (!(flags
& REGEX_ID_CHECK
)) {
1402 if (!(flags
& REGEX_MATCH_BEGIN
)) {
1403 /* Check whether item is inserted. */
1404 EMIT_CMP(jump1
, SLJIT_NOT_EQUAL
, SLJIT_MEM1(R_NEXT_STATE
), offset
+ sizeof(sljit_sw
), SLJIT_IMM
, -1);
1405 EMIT_OP1(SLJIT_MOV
, SLJIT_MEM1(R_NEXT_STATE
), offset
+ sizeof(sljit_sw
), R_NEXT_HEAD
, 0);
1407 EMIT_OP1(SLJIT_MOV
, R_NEXT_HEAD
, 0, SLJIT_IMM
, offset
);
1409 EMIT_JUMP(jump2
, SLJIT_JUMP
);
1411 /* Check whether old index <= index. */
1413 sljit_set_label(jump1
, label1
);
1415 EMIT_CMP(jump1
, SLJIT_LESS_EQUAL
, SLJIT_MEM1(R_NEXT_STATE
), offset
+ 2 * sizeof(sljit_sw
), R_TEMP
, 0);
1418 sljit_set_label(jump2
, label1
);
1419 EMIT_OP1(SLJIT_MOV
, SLJIT_MEM1(R_NEXT_STATE
), offset
+ 2 * sizeof(sljit_sw
), R_TEMP
, 0);
1422 sljit_set_label(jump1
, label1
);
1425 /* Check whether item is inserted. */
1426 EMIT_CMP(jump1
, SLJIT_NOT_EQUAL
, SLJIT_MEM1(R_NEXT_STATE
), offset
+ sizeof(sljit_sw
), SLJIT_IMM
, -1);
1427 EMIT_OP1(SLJIT_MOV
, SLJIT_MEM1(R_NEXT_STATE
), offset
+ sizeof(sljit_sw
), R_NEXT_HEAD
, 0);
1429 EMIT_OP1(SLJIT_MOV
, R_NEXT_HEAD
, 0, SLJIT_IMM
, offset
);
1432 sljit_set_label(jump1
, label1
);
1436 if (!(flags
& REGEX_MATCH_BEGIN
)) {
1437 EMIT_OP1(SLJIT_MOV
, R_TEMP
, 0, SLJIT_MEM1(R_CURR_STATE
), TERM_OFFSET_OF(curr_index
, 2));
1439 /* Check whether item is inserted. */
1440 EMIT_CMP(jump1
, SLJIT_NOT_EQUAL
, SLJIT_MEM1(R_NEXT_STATE
), offset
+ sizeof(sljit_sw
), SLJIT_IMM
, -1);
1441 EMIT_OP1(SLJIT_MOV
, SLJIT_MEM1(R_NEXT_STATE
), offset
+ sizeof(sljit_sw
), R_NEXT_HEAD
, 0);
1443 EMIT_OP1(SLJIT_MOV
, R_NEXT_HEAD
, 0, SLJIT_IMM
, offset
);
1445 EMIT_JUMP(jump2
, SLJIT_JUMP
);
1447 /* Check whether old index != index. */
1449 sljit_set_label(jump1
, label1
);
1451 EMIT_OP2(SLJIT_SUB
| SLJIT_SET_Z
| SLJIT_SET_LESS
, SLJIT_UNUSED
, 0, SLJIT_MEM1(R_NEXT_STATE
), offset
+ 2 * sizeof(sljit_sw
), R_TEMP
, 0);
1452 EMIT_JUMP(jump1
, SLJIT_LESS
);
1453 EMIT_JUMP(jump3
, SLJIT_NOT_EQUAL
); /* Greater. */
1455 /* Old index == index. */
1456 EMIT_OP1(SLJIT_MOV
, R_TEMP
, 0, SLJIT_MEM1(R_CURR_STATE
), TERM_OFFSET_OF(curr_index
, 3));
1457 if (search_states
[value
].value
> 0) {
1458 EMIT_CMP(jump4
, SLJIT_GREATER
, R_TEMP
, 0, SLJIT_IMM
, search_states
[value
].value
);
1460 EMIT_OP1(SLJIT_MOV
, R_TEMP
, 0, SLJIT_IMM
, search_states
[value
].value
);
1462 sljit_set_label(jump4
, label1
);
1465 EMIT_OP2(SLJIT_SUB
| SLJIT_SET_GREATER_EQUAL
, SLJIT_UNUSED
, 0, SLJIT_MEM1(R_NEXT_STATE
), offset
+ 3 * sizeof(sljit_sw
), R_TEMP
, 0);
1466 EMIT_JUMP(jump4
, SLJIT_GREATER_EQUAL
);
1467 EMIT_JUMP(jump5
, SLJIT_JUMP
);
1469 /* Overwrite index & id. */
1471 sljit_set_label(jump3
, label1
);
1472 sljit_set_label(jump2
, label1
);
1473 EMIT_OP1(SLJIT_MOV
, SLJIT_MEM1(R_NEXT_STATE
), offset
+ 2 * sizeof(sljit_sw
), R_TEMP
, 0);
1475 EMIT_OP1(SLJIT_MOV
, R_TEMP
, 0, SLJIT_MEM1(R_CURR_STATE
), TERM_OFFSET_OF(curr_index
, 3));
1476 if (search_states
[value
].value
> 0) {
1477 EMIT_CMP(jump3
, SLJIT_GREATER
, R_TEMP
, 0, SLJIT_IMM
, search_states
[value
].value
);
1479 EMIT_OP1(SLJIT_MOV
, R_TEMP
, 0, SLJIT_IMM
, search_states
[value
].value
);
1481 sljit_set_label(jump3
, label1
);
1485 sljit_set_label(jump5
, label1
);
1486 EMIT_OP1(SLJIT_MOV
, SLJIT_MEM1(R_NEXT_STATE
), offset
+ 3 * sizeof(sljit_sw
), R_TEMP
, 0);
1490 sljit_set_label(jump1
, label1
);
1491 sljit_set_label(jump4
, label1
);
1494 EMIT_OP1(SLJIT_MOV
, R_TEMP
, 0, SLJIT_MEM1(R_CURR_STATE
), TERM_OFFSET_OF(curr_index
, 2));
1496 if (search_states
[value
].value
> 0) {
1497 EMIT_CMP(jump1
, SLJIT_GREATER
, R_TEMP
, 0, SLJIT_IMM
, search_states
[value
].value
);
1499 EMIT_OP1(SLJIT_MOV
, R_TEMP
, 0, SLJIT_IMM
, search_states
[value
].value
);
1501 sljit_set_label(jump1
, label1
);
1504 /* Check whether item is inserted. */
1505 EMIT_CMP(jump1
, SLJIT_NOT_EQUAL
, SLJIT_MEM1(R_NEXT_STATE
), offset
+ sizeof(sljit_sw
), SLJIT_IMM
, -1);
1506 EMIT_OP1(SLJIT_MOV
, SLJIT_MEM1(R_NEXT_STATE
), offset
+ sizeof(sljit_sw
), R_NEXT_HEAD
, 0);
1508 EMIT_OP1(SLJIT_MOV
, R_NEXT_HEAD
, 0, SLJIT_IMM
, offset
);
1510 EMIT_JUMP(jump2
, SLJIT_JUMP
);
1512 /* Check whether old id >= id. */
1514 sljit_set_label(jump1
, label1
);
1516 EMIT_CMP(jump1
, SLJIT_GREATER_EQUAL
, SLJIT_MEM1(R_NEXT_STATE
), offset
+ 2 * sizeof(sljit_sw
), R_TEMP
, 0);
1519 sljit_set_label(jump2
, label1
);
1520 EMIT_OP1(SLJIT_MOV
, SLJIT_MEM1(R_NEXT_STATE
), offset
+ 2 * sizeof(sljit_sw
), R_TEMP
, 0);
1523 sljit_set_label(jump1
, label1
);
1527 search_states
[value
].value
= -1;
1530 #ifdef REGEX_MATCH_VERBOSE
1531 if (flags
& REGEX_MATCH_VERBOSE
)
1534 return REGEX_NO_ERROR
;
1537 static int compile_end_check(struct compiler_common
*compiler_common
, struct sljit_label
*end_check_label
)
1539 struct sljit_compiler
*compiler
= compiler_common
->compiler
;
1540 struct sljit_jump
*jump
;
1541 struct sljit_jump
*clear_states_jump
;
1542 struct sljit_label
*label
;
1543 struct sljit_label
*leave_label
;
1544 struct sljit_label
*begin_loop_label
;
1546 /* Priority order: best_begin > best_end > best_id.
1548 if (new best_begin > old test_begin) do nothing
1549 otherwise we know that new_end > old_end, since R_CURR_INDEX ever increasing
1550 therefore we must overwrite all best_* variables (new_id also contains the highest id for this turn). */
1552 /* Both R_CURR_CHAR and R_BEST_BEGIN used as temporary registers. */
1554 if (!(compiler_common
->flags
& REGEX_MATCH_BEGIN
)) {
1555 EMIT_OP1(SLJIT_MOV
, R_CURR_CHAR
, 0, SLJIT_MEM1(R_CURR_STATE
), TERM_REL_OFFSET_OF(0, 2));
1556 EMIT_CMP(jump
, !(compiler_common
->flags
& REGEX_MATCH_NON_GREEDY
) ? SLJIT_LESS
: SLJIT_LESS_EQUAL
, SLJIT_MEM1(R_REGEX_MATCH
), SLJIT_OFFSETOF(struct regex_match
, best_begin
), R_CURR_CHAR
, 0);
1557 sljit_set_label(jump
, end_check_label
);
1559 EMIT_OP1(SLJIT_MOV
, SLJIT_MEM1(R_REGEX_MATCH
), SLJIT_OFFSETOF(struct regex_match
, best_begin
), R_CURR_CHAR
, 0);
1560 if (!(compiler_common
->flags
& (REGEX_FAKE_MATCH_BEGIN
| REGEX_FAKE_MATCH_END
))) {
1561 EMIT_OP1(SLJIT_MOV
, SLJIT_MEM1(R_REGEX_MATCH
), SLJIT_OFFSETOF(struct regex_match
, best_end
), R_CURR_INDEX
, 0);
1564 if ((compiler_common
->flags
& (REGEX_FAKE_MATCH_BEGIN
| REGEX_FAKE_MATCH_END
)) == (REGEX_FAKE_MATCH_BEGIN
| REGEX_FAKE_MATCH_END
)) {
1565 EMIT_OP2(SLJIT_SUB
, SLJIT_MEM1(R_REGEX_MATCH
), SLJIT_OFFSETOF(struct regex_match
, best_end
), R_CURR_INDEX
, 0, SLJIT_IMM
, 2);
1568 EMIT_OP2(SLJIT_SUB
, SLJIT_MEM1(R_REGEX_MATCH
), SLJIT_OFFSETOF(struct regex_match
, best_end
), R_CURR_INDEX
, 0, SLJIT_IMM
, 1);
1571 if (compiler_common
->flags
& REGEX_ID_CHECK
) {
1572 EMIT_OP1(SLJIT_MOV
, SLJIT_MEM1(R_REGEX_MATCH
), SLJIT_OFFSETOF(struct regex_match
, best_id
), SLJIT_MEM1(R_CURR_STATE
), TERM_REL_OFFSET_OF(0, 3));
1575 EMIT_CMP(clear_states_jump
, SLJIT_LESS
, R_CURR_CHAR
, 0, R_BEST_BEGIN
, 0);
1577 EMIT_LABEL(leave_label
);
1578 EMIT_OP1(SLJIT_MOV
, R_BEST_BEGIN
, 0, R_CURR_CHAR
, 0);
1579 EMIT_JUMP(jump
, SLJIT_JUMP
);
1580 sljit_set_label(jump
, end_check_label
);
1582 /* A loop to clear all states, which are > (or >=) than R_CURR_CHAR. */
1584 sljit_set_label(clear_states_jump
, label
);
1586 EMIT_OP1(SLJIT_MOV
, R_TEMP
, 0, R_NEXT_HEAD
, 0);
1587 EMIT_OP1(SLJIT_MOV
, R_NEXT_HEAD
, 0, SLJIT_IMM
, 0);
1589 /* Begin of the loop. */
1590 EMIT_LABEL(begin_loop_label
);
1591 EMIT_CMP(jump
, SLJIT_EQUAL
, R_TEMP
, 0, SLJIT_IMM
, 0);
1592 sljit_set_label(jump
, leave_label
);
1594 EMIT_OP2(SLJIT_ADD
, R_TEMP
, 0, R_TEMP
, 0, R_CURR_STATE
, 0);
1595 EMIT_OP1(SLJIT_MOV
, R_BEST_BEGIN
, 0, SLJIT_MEM1(R_TEMP
), sizeof(sljit_sw
));
1596 EMIT_CMP(clear_states_jump
, !(compiler_common
->flags
& REGEX_MATCH_NON_GREEDY
) ? SLJIT_GREATER
: SLJIT_GREATER_EQUAL
, SLJIT_MEM1(R_TEMP
), 2 * sizeof(sljit_sw
), R_CURR_CHAR
, 0);
1598 /* Case 1: keep this case. */
1599 EMIT_OP1(SLJIT_MOV
, SLJIT_MEM1(R_TEMP
), sizeof(sljit_sw
), R_NEXT_HEAD
, 0);
1600 EMIT_OP2(SLJIT_SUB
, R_NEXT_HEAD
, 0, R_TEMP
, 0, R_CURR_STATE
, 0);
1602 EMIT_OP1(SLJIT_MOV
, R_TEMP
, 0, R_BEST_BEGIN
, 0);
1603 EMIT_JUMP(jump
, SLJIT_JUMP
);
1604 sljit_set_label(jump
, begin_loop_label
);
1606 /* Case 2: remove this case. */
1608 sljit_set_label(clear_states_jump
, label
);
1610 EMIT_OP1(SLJIT_MOV
, SLJIT_MEM1(R_TEMP
), sizeof(sljit_sw
), SLJIT_IMM
, -1);
1612 EMIT_OP1(SLJIT_MOV
, R_TEMP
, 0, R_BEST_BEGIN
, 0);
1613 EMIT_JUMP(jump
, SLJIT_JUMP
);
1614 sljit_set_label(jump
, begin_loop_label
);
1617 EMIT_OP1(SLJIT_MOV
, R_BEST_BEGIN
, 0, SLJIT_IMM
, 0);
1618 EMIT_OP1(SLJIT_MOV
, SLJIT_MEM1(R_REGEX_MATCH
), SLJIT_OFFSETOF(struct regex_match
, best_begin
), SLJIT_IMM
, 0);
1619 EMIT_OP1(SLJIT_MOV
, SLJIT_MEM1(R_REGEX_MATCH
), SLJIT_OFFSETOF(struct regex_match
, best_end
), R_CURR_INDEX
, 0);
1620 if (compiler_common
->flags
& REGEX_ID_CHECK
) {
1621 EMIT_OP1(SLJIT_MOV
, SLJIT_MEM1(R_REGEX_MATCH
), SLJIT_OFFSETOF(struct regex_match
, best_id
), SLJIT_MEM1(R_CURR_STATE
), TERM_REL_OFFSET_OF(0, 2));
1623 EMIT_JUMP(jump
, SLJIT_JUMP
);
1624 sljit_set_label(jump
, end_check_label
);
1626 return REGEX_NO_ERROR
;
1629 static int compile_leave_fast_forward(struct compiler_common
*compiler_common
, struct sljit_label
*fast_forward_label
)
1631 struct sljit_compiler
*compiler
= compiler_common
->compiler
;
1632 struct stack
*stack
= &compiler_common
->stack
;
1633 struct stack_item
*dfa_transitions
= compiler_common
->dfa_transitions
;
1634 struct stack_item
*search_states
= compiler_common
->search_states
;
1636 struct sljit_jump
*jump
;
1637 int init_range
= 1, prev_value
= 0;
1639 while (stack
->count
> 0) {
1640 ind
= stack_pop(stack
)->value
;
1641 search_states
[ind
].value
= -1;
1642 if (search_states
[ind
].type
>= 0) {
1643 if (dfa_transitions
[ind
].type
== type_char
) {
1644 EMIT_CMP(jump
, SLJIT_EQUAL
, R_CURR_CHAR
, 0, SLJIT_IMM
, dfa_transitions
[ind
].value
);
1645 sljit_set_label(jump
, fast_forward_label
);
1647 else if (dfa_transitions
[ind
].type
== type_rng_start
) {
1648 SLJIT_ASSERT(!dfa_transitions
[ind
].value
);
1650 while (dfa_transitions
[ind
].type
!= type_rng_end
) {
1651 if (dfa_transitions
[ind
].type
== type_rng_char
) {
1652 EMIT_CMP(jump
, SLJIT_EQUAL
, R_CURR_CHAR
, 0, SLJIT_IMM
, dfa_transitions
[ind
].value
);
1653 sljit_set_label(jump
, fast_forward_label
);
1656 SLJIT_ASSERT(dfa_transitions
[ind
].type
== type_rng_left
);
1658 EMIT_OP1(SLJIT_MOV
, R_TEMP
, 0, R_CURR_CHAR
, 0);
1661 if (dfa_transitions
[ind
].value
!= prev_value
) {
1662 /* Best compatibility to all archs. */
1663 prev_value
-= dfa_transitions
[ind
].value
;
1664 if (prev_value
< 0) {
1665 EMIT_OP2(SLJIT_SUB
, R_TEMP
, 0, R_TEMP
, 0, SLJIT_IMM
, -prev_value
);
1668 EMIT_OP2(SLJIT_ADD
, R_TEMP
, 0, R_TEMP
, 0, SLJIT_IMM
, prev_value
);
1670 prev_value
= dfa_transitions
[ind
].value
;
1672 EMIT_CMP(jump
, SLJIT_LESS_EQUAL
, R_TEMP
, 0, SLJIT_IMM
, dfa_transitions
[ind
+ 1].value
- dfa_transitions
[ind
].value
);
1673 sljit_set_label(jump
, fast_forward_label
);
1680 SLJIT_ASSERT(dfa_transitions
[ind
].type
== type_newline
);
1681 EMIT_CMP(jump
, SLJIT_EQUAL
, R_CURR_CHAR
, 0, SLJIT_IMM
, '\n');
1682 sljit_set_label(jump
, fast_forward_label
);
1683 EMIT_CMP(jump
, SLJIT_EQUAL
, R_CURR_CHAR
, 0, SLJIT_IMM
, '\r');
1684 sljit_set_label(jump
, fast_forward_label
);
1688 return REGEX_NO_ERROR
;
1691 static int compile_newline_check(struct compiler_common
*compiler_common
, sljit_sw ind
)
1693 struct sljit_compiler
*compiler
= compiler_common
->compiler
;
1694 struct sljit_jump
*jump1
;
1695 struct sljit_jump
*jump2
;
1696 struct sljit_label
*label
;
1700 /* Check whether a new-line character is found. */
1701 EMIT_CMP(jump1
, SLJIT_EQUAL
, R_CURR_CHAR
, 0, SLJIT_IMM
, '\n');
1702 EMIT_CMP(jump2
, SLJIT_EQUAL
, R_CURR_CHAR
, 0, SLJIT_IMM
, '\r');
1704 no_states
= compiler_common
->no_states
;
1705 offset
= TERM_OFFSET_OF(compiler_common
->search_states
[ind
].type
, 1);
1706 EMIT_OP1(SLJIT_MOV
, R_TEMP
, 0, SLJIT_MEM1(R_CURR_STATE
), offset
);
1707 EMIT_OP1(SLJIT_MOV
, SLJIT_MEM1(R_CURR_STATE
), offset
, SLJIT_IMM
, -1);
1708 CHECK(sljit_emit_ijump(compiler
, SLJIT_JUMP
, SLJIT_MEM2(R_CURR_STATE
, R_TEMP
), 0));
1711 sljit_set_label(jump1
, label
);
1712 sljit_set_label(jump2
, label
);
1713 return REGEX_NO_ERROR
;
1718 #define CHECK(exp) \
1719 if (SLJIT_UNLIKELY(exp)) \
1722 static SLJIT_INLINE
void range_set_label(struct sljit_jump
**range_jump_list
, struct sljit_label
*label
)
1724 while (*range_jump_list
) {
1725 sljit_set_label(*range_jump_list
, label
);
1730 static sljit_sw
compile_range_check(struct compiler_common
*compiler_common
, sljit_sw ind
)
1732 struct sljit_compiler
*compiler
= compiler_common
->compiler
;
1733 struct stack_item
*dfa_transitions
= compiler_common
->dfa_transitions
;
1734 struct sljit_jump
**range_jump_list
= compiler_common
->range_jump_list
;
1735 int invert
= dfa_transitions
[ind
].value
;
1736 struct sljit_label
*label
;
1739 int init_range
= 1, prev_value
= 0;
1743 while (dfa_transitions
[ind
].type
!= type_rng_end
) {
1744 if (dfa_transitions
[ind
].type
== type_rng_char
) {
1745 EMIT_CMP(*range_jump_list
, SLJIT_EQUAL
, R_CURR_CHAR
, 0, SLJIT_IMM
, dfa_transitions
[ind
].value
);
1749 SLJIT_ASSERT(dfa_transitions
[ind
].type
== type_rng_left
);
1751 EMIT_OP1(SLJIT_MOV
, R_TEMP
, 0, R_CURR_CHAR
, 0);
1754 if (dfa_transitions
[ind
].value
!= prev_value
) {
1755 /* Best compatibility to all archs. */
1756 prev_value
-= dfa_transitions
[ind
].value
;
1757 if (prev_value
< 0) {
1758 EMIT_OP2(SLJIT_SUB
, R_TEMP
, 0, R_TEMP
, 0, SLJIT_IMM
, -prev_value
);
1761 EMIT_OP2(SLJIT_ADD
, R_TEMP
, 0, R_TEMP
, 0, SLJIT_IMM
, prev_value
);
1763 prev_value
= dfa_transitions
[ind
].value
;
1765 EMIT_CMP(*range_jump_list
, SLJIT_LESS_EQUAL
, R_TEMP
, 0, SLJIT_IMM
, dfa_transitions
[ind
+ 1].value
- dfa_transitions
[ind
].value
);
1772 *range_jump_list
= NULL
;
1775 no_states
= compiler_common
->no_states
;
1776 offset
= TERM_OFFSET_OF(compiler_common
->search_states
[ind
].type
, 1);
1777 EMIT_OP1(SLJIT_MOV
, R_TEMP
, 0, SLJIT_MEM1(R_CURR_STATE
), offset
);
1778 EMIT_OP1(SLJIT_MOV
, SLJIT_MEM1(R_CURR_STATE
), offset
, SLJIT_IMM
, -1);
1779 CHECK(sljit_emit_ijump(compiler
, SLJIT_JUMP
, SLJIT_MEM2(R_CURR_STATE
, R_TEMP
), 0));
1782 range_set_label(compiler_common
->range_jump_list
, label
);
1783 /* Clears the jump list. */
1784 *compiler_common
->range_jump_list
= NULL
;
1789 #undef TERM_OFFSET_OF
1797 /* --------------------------------------------------------------------- */
1799 /* --------------------------------------------------------------------- */
1801 #define TERM_OFFSET_OF(ind, offs) (((ind) * compiler_common.no_states + (offs)) * sizeof(sljit_sw))
1803 #define EMIT_OP1(type, arg1, arg2, arg3, arg4) \
1804 CHECK(sljit_emit_op1(compiler_common.compiler, type, arg1, arg2, arg3, arg4))
1806 #define EMIT_OP2(type, arg1, arg2, arg3, arg4, arg5, arg6) \
1807 CHECK(sljit_emit_op2(compiler_common.compiler, type, arg1, arg2, arg3, arg4, arg5, arg6))
1809 #define EMIT_LABEL(label) \
1810 label = sljit_emit_label(compiler_common.compiler); \
1813 #define EMIT_JUMP(jump, type) \
1814 jump = sljit_emit_jump(compiler_common.compiler, type); \
1817 #define EMIT_CMP(jump, type, arg1, arg2, arg3, arg4) \
1818 jump = sljit_emit_cmp(compiler_common.compiler, type, arg1, arg2, arg3, arg4); \
1821 /* A do {} while(0) expression helps to avoid goto statements. */
1822 #define BEGIN_GUARD \
1828 #define CHECK(exp) \
1829 if (SLJIT_UNLIKELY(exp)) \
1832 struct regex_machine
* regex_compile(const regex_char_t
*regex_string
, int length
, int re_flags
, int *error
)
1834 struct compiler_common compiler_common
;
1836 int error_code
, done
, suggest_fast_forward
;
1837 /* ID of an empty match (-1 if not reachable). */
1840 struct sljit_jump
*jump
;
1841 struct sljit_jump
*best_match_found_jump
;
1842 struct sljit_jump
*fast_forward_jump
= NULL
;
1843 struct sljit_jump
*length_is_zero_jump
;
1844 struct sljit_jump
*end_check_jump
= NULL
;
1845 struct sljit_jump
*best_match_check_jump
= NULL
;
1846 struct sljit_jump
*non_greedy_end_jump
= NULL
;
1847 struct sljit_label
*label
;
1848 struct sljit_label
*end_check_label
= NULL
;
1849 struct sljit_label
*start_label
;
1850 struct sljit_label
*fast_forward_label
;
1851 struct sljit_label
*fast_forward_return_label
;
1854 *error
= REGEX_NO_ERROR
;
1855 #ifdef REGEX_MATCH_VERBOSE
1856 compiler_common
.flags
= re_flags
& (REGEX_MATCH_BEGIN
| REGEX_MATCH_END
| REGEX_MATCH_NON_GREEDY
| REGEX_NEWLINE
| REGEX_MATCH_VERBOSE
);
1858 compiler_common
.flags
= re_flags
& (REGEX_MATCH_BEGIN
| REGEX_MATCH_END
| REGEX_MATCH_NON_GREEDY
| REGEX_NEWLINE
);
1861 /* Step 1: parsing (Left->Right).
1862 Syntax check and AST generator. */
1863 error_code
= parse(regex_string
, length
, &compiler_common
);
1865 stack_destroy(&compiler_common
.stack
);
1867 *error
= error_code
;
1871 /* Step 2: generating branches (Right->Left). */
1872 error_code
= generate_transitions(&compiler_common
);
1873 stack_destroy(&compiler_common
.stack
);
1874 stack_destroy(&compiler_common
.depth
);
1876 if (compiler_common
.dfa_transitions
)
1877 SLJIT_FREE(compiler_common
.dfa_transitions
, NULL
);
1879 *error
= error_code
;
1883 /* Step 3: Generate necessary data for depth-first search (Left->Right). */
1884 error_code
= generate_search_states(&compiler_common
);
1886 SLJIT_FREE(compiler_common
.dfa_transitions
, NULL
);
1888 *error
= error_code
;
1892 #ifdef REGEX_MATCH_VERBOSE
1893 if (compiler_common
.flags
& REGEX_MATCH_VERBOSE
)
1894 verbose_transitions(&compiler_common
);
1897 /* Step 4: Left->Right generate code. */
1898 stack_init(&compiler_common
.stack
);
1899 stack_init(&compiler_common
.depth
);
1901 compiler_common
.machine
= NULL
;
1902 compiler_common
.compiler
= NULL
;
1903 compiler_common
.range_jump_list
= NULL
;
1907 compiler_common
.machine
= (struct regex_machine
*)SLJIT_MALLOC(sizeof(struct regex_machine
) + (compiler_common
.terms_size
- 1) * sizeof(sljit_uw
), NULL
);
1908 CHECK(!compiler_common
.machine
);
1910 compiler_common
.compiler
= sljit_create_compiler(NULL
, NULL
);
1911 CHECK(!compiler_common
.compiler
);
1913 if (compiler_common
.longest_range_size
> 0) {
1914 compiler_common
.range_jump_list
= (struct sljit_jump
**)SLJIT_MALLOC(sizeof(struct sljit_jump
*) * compiler_common
.longest_range_size
, NULL
);
1915 CHECK(!compiler_common
.range_jump_list
);
1918 if ((compiler_common
.flags
& REGEX_ID_CHECK
) && !(compiler_common
.flags
& REGEX_MATCH_BEGIN
))
1919 compiler_common
.no_states
= 4;
1920 else if (!(compiler_common
.flags
& REGEX_ID_CHECK
) && (compiler_common
.flags
& REGEX_MATCH_BEGIN
))
1921 compiler_common
.no_states
= 2;
1923 compiler_common
.no_states
= 3;
1925 compiler_common
.machine
->flags
= compiler_common
.flags
;
1926 compiler_common
.machine
->no_states
= compiler_common
.no_states
;
1927 compiler_common
.machine
->size
= compiler_common
.machine
->no_states
* compiler_common
.terms_size
;
1929 /* Study the regular expression. */
1930 empty_match_id
= -1;
1931 suggest_fast_forward
= 1;
1932 if (!(compiler_common
.flags
& REGEX_FAKE_MATCH_BEGIN
)) {
1933 CHECK(trace_transitions(0, &compiler_common
));
1934 while (compiler_common
.stack
.count
> 0) {
1935 ind
= stack_pop(&compiler_common
.stack
)->value
;
1936 if (compiler_common
.search_states
[ind
].type
== 0) {
1937 SLJIT_ASSERT(compiler_common
.dfa_transitions
[ind
].type
== type_end
);
1938 suggest_fast_forward
= 0;
1939 empty_match_id
= compiler_common
.search_states
[ind
].value
;
1941 else if (compiler_common
.search_states
[ind
].type
> 0) {
1942 SLJIT_ASSERT(compiler_common
.dfa_transitions
[ind
].type
!= type_end
);
1943 if (compiler_common
.dfa_transitions
[ind
].type
== type_rng_start
&& compiler_common
.dfa_transitions
[ind
].value
)
1944 suggest_fast_forward
= 0;
1946 compiler_common
.search_states
[ind
].value
= -1;
1950 SLJIT_ASSERT(compiler_common
.dfa_transitions
[1].type
== type_newline
);
1951 CHECK(trace_transitions(1, &compiler_common
));
1952 while (compiler_common
.stack
.count
> 0) {
1953 ind
= stack_pop(&compiler_common
.stack
)->value
;
1954 if (compiler_common
.search_states
[ind
].type
== 0) {
1955 SLJIT_ASSERT(compiler_common
.dfa_transitions
[ind
].type
== type_end
);
1956 suggest_fast_forward
= 0;
1957 empty_match_id
= compiler_common
.search_states
[ind
].value
;
1959 compiler_common
.search_states
[ind
].value
= -1;
1963 /* Step 4.1: Generate entry. */
1964 CHECK(sljit_emit_enter(compiler_common
.compiler
, 0, SLJIT_ARG1(SW
) | SLJIT_ARG2(SW
) | SLJIT_ARG3(SW
), 5, 5, 0, 0, 0));
1966 /* Copy arguments to their place. */
1967 EMIT_OP1(SLJIT_MOV
, R_REGEX_MATCH
, 0, SLJIT_S0
, 0);
1968 EMIT_OP1(SLJIT_MOV
, R_STRING
, 0, SLJIT_S1
, 0);
1969 EMIT_OP2(SLJIT_ADD
, R_LENGTH
, 0, SLJIT_S2
, 0, SLJIT_IMM
, 1);
1971 /* Init global registers. */
1972 EMIT_OP1(SLJIT_MOV
, R_CURR_STATE
, 0, SLJIT_MEM1(R_REGEX_MATCH
), SLJIT_OFFSETOF(struct regex_match
, current
));
1973 EMIT_OP1(SLJIT_MOV
, R_NEXT_STATE
, 0, SLJIT_MEM1(R_REGEX_MATCH
), SLJIT_OFFSETOF(struct regex_match
, next
));
1974 EMIT_OP1(SLJIT_MOV
, R_NEXT_HEAD
, 0, SLJIT_MEM1(R_REGEX_MATCH
), SLJIT_OFFSETOF(struct regex_match
, head
));
1975 EMIT_OP1(SLJIT_MOV
, R_BEST_BEGIN
, 0, SLJIT_MEM1(R_REGEX_MATCH
), SLJIT_OFFSETOF(struct regex_match
, best_begin
));
1976 EMIT_OP1(SLJIT_MOV
, R_CURR_INDEX
, 0, SLJIT_MEM1(R_REGEX_MATCH
), SLJIT_OFFSETOF(struct regex_match
, index
));
1978 /* Check whether the best match has already found in a previous frame. */
1979 EMIT_CMP(jump
, SLJIT_EQUAL
, SLJIT_MEM1(R_REGEX_MATCH
), SLJIT_OFFSETOF(struct regex_match
, fast_quit
), SLJIT_IMM
, 0);
1980 EMIT_JUMP(best_match_found_jump
, SLJIT_JUMP
);
1982 #ifdef REGEX_MATCH_VERBOSE
1983 if (compiler_common
.flags
& REGEX_MATCH_VERBOSE
)
1984 printf("\n-----------------\nTrace\n-----------------\n");
1987 /* Step 4.2: Generate code for state 0. */
1989 sljit_emit_op0(compiler_common
.compiler
, SLJIT_ENDBR
);
1990 compiler_common
.machine
->entry_addrs
[0] = (sljit_uw
)label
;
1992 /* Swapping current and next. */
1993 EMIT_OP1(SLJIT_MOV
, R_TEMP
, 0, R_CURR_STATE
, 0);
1994 EMIT_OP1(SLJIT_MOV
, R_CURR_STATE
, 0, R_NEXT_STATE
, 0);
1995 EMIT_OP1(SLJIT_MOV
, R_NEXT_STATE
, 0, R_TEMP
, 0);
1997 /* Checking whether the best case needs to be updated. */
1998 if (!(compiler_common
.flags
& REGEX_MATCH_END
)) {
1999 EMIT_CMP(end_check_jump
, SLJIT_NOT_EQUAL
, SLJIT_MEM1(R_CURR_STATE
), TERM_REL_OFFSET_OF(0, 1), SLJIT_IMM
, -1);
2000 EMIT_LABEL(end_check_label
);
2002 EMIT_OP1(SLJIT_MOV
, SLJIT_MEM1(R_NEXT_STATE
), TERM_REL_OFFSET_OF(0, 1), SLJIT_IMM
, -1);
2003 EMIT_OP2(SLJIT_ADD
, R_CURR_INDEX
, 0, R_CURR_INDEX
, 0, SLJIT_IMM
, 1);
2005 /* Checking whether best case has already found. */
2006 if (!(compiler_common
.flags
& REGEX_MATCH_END
) || (compiler_common
.flags
& REGEX_MATCH_BEGIN
)) {
2007 if (!(compiler_common
.flags
& REGEX_MATCH_BEGIN
)) {
2008 /* We can bail out if no more active states remain and R_BEST_BEGIN != -1. */
2009 EMIT_CMP(best_match_check_jump
, SLJIT_NOT_EQUAL
, R_BEST_BEGIN
, 0, SLJIT_IMM
, -1);
2012 /* We can bail out if no more active states remain (regardless of R_BEST_BEGIN). */
2013 EMIT_CMP(best_match_check_jump
, SLJIT_EQUAL
, R_NEXT_HEAD
, 0, SLJIT_IMM
, 0);
2017 EMIT_LABEL(start_label
);
2018 sljit_set_label(jump
, start_label
);
2020 if (!(compiler_common
.flags
& REGEX_MATCH_BEGIN
) && suggest_fast_forward
) {
2021 EMIT_CMP(fast_forward_jump
, SLJIT_NOT_EQUAL
, SLJIT_MEM1(R_REGEX_MATCH
), SLJIT_OFFSETOF(struct regex_match
, fast_forward
), SLJIT_IMM
, 0);
2024 /* Loading the next character. */
2025 EMIT_OP2(SLJIT_SUB
| SLJIT_SET_Z
, R_LENGTH
, 0, R_LENGTH
, 0, SLJIT_IMM
, 1);
2026 EMIT_JUMP(length_is_zero_jump
, SLJIT_EQUAL
);
2028 EMIT_OP1(SLJIT_MOV
, R_TEMP
, 0, R_STRING
, 0);
2029 #ifdef REGEX_USE_8BIT_CHARS
2030 EMIT_OP1(SLJIT_MOV_U8
, R_CURR_CHAR
, 0, SLJIT_MEM1(R_TEMP
), 0);
2031 EMIT_OP2(SLJIT_ADD
, R_TEMP
, 0, R_TEMP
, 0, SLJIT_IMM
, 1);
2033 EMIT_OP1(SLJIT_MOV_UH
, R_CURR_CHAR
, 0, SLJIT_MEM1(R_TEMP
), 0);
2034 EMIT_OP2(SLJIT_ADD
, R_TEMP
, 0, R_TEMP
, 0, SLJIT_IMM
, 2);
2036 EMIT_OP1(SLJIT_MOV
, R_STRING
, 0, R_TEMP
, 0);
2038 #ifdef REGEX_MATCH_VERBOSE
2039 if (compiler_common
.flags
& REGEX_MATCH_VERBOSE
) {
2040 printf("(%3d): ", 0);
2041 CHECK(trace_transitions(0, &compiler_common
));
2042 while (compiler_common
.stack
.count
> 0) {
2043 ind
= stack_pop(&compiler_common
.stack
)->value
;
2044 if (compiler_common
.search_states
[ind
].type
>= 0)
2045 printf("-> (%3d:%3d) ", compiler_common
.search_states
[ind
].type
, compiler_common
.search_states
[ind
].value
);
2046 compiler_common
.search_states
[ind
].value
= -1;
2052 EMIT_LABEL(fast_forward_return_label
);
2053 if (!(compiler_common
.flags
& REGEX_MATCH_BEGIN
)) {
2054 EMIT_OP1(SLJIT_MOV
, SLJIT_MEM1(R_REGEX_MATCH
), SLJIT_OFFSETOF(struct regex_match
, fast_forward
), SLJIT_IMM
, 1);
2055 if (!(compiler_common
.flags
& REGEX_MATCH_END
)) {
2056 EMIT_CMP(jump
, SLJIT_NOT_EQUAL
, R_BEST_BEGIN
, 0, SLJIT_IMM
, -1);
2059 EMIT_OP1(SLJIT_MOV
, R_TEMP
, 0, R_CURR_INDEX
, 0);
2060 CHECK(compile_uncond_tran(&compiler_common
, R_NEXT_STATE
));
2061 /* And branching to the first state. */
2062 CHECK(sljit_emit_ijump(compiler_common
.compiler
, SLJIT_JUMP
, SLJIT_MEM2(R_CURR_STATE
, R_TEMP
), 0));
2064 if (!(compiler_common
.flags
& REGEX_MATCH_END
)) {
2066 sljit_set_label(jump
, label
);
2069 /* This is the case where we only have to reset the R_NEXT_HEAD. */
2070 EMIT_OP1(SLJIT_MOV
, R_TEMP
, 0, R_NEXT_HEAD
, 0);
2071 EMIT_OP1(SLJIT_MOV
, R_NEXT_HEAD
, 0, SLJIT_IMM
, 0);
2072 CHECK(sljit_emit_ijump(compiler_common
.compiler
, SLJIT_JUMP
, SLJIT_MEM2(R_CURR_STATE
, R_TEMP
), 0));
2074 /* Fast-forward loop. */
2075 if (fast_forward_jump
) {
2076 /* Quit from fast-forward loop. */
2077 EMIT_LABEL(fast_forward_label
);
2078 EMIT_OP2(SLJIT_SUB
, R_TEMP
, 0, R_NEXT_HEAD
, 0, SLJIT_IMM
, 1);
2079 EMIT_OP1(SLJIT_MOV
, R_LENGTH
, 0, R_NEXT_STATE
, 0);
2080 EMIT_OP1(SLJIT_MOV
, R_STRING
, 0, R_CURR_STATE
, 0);
2081 EMIT_OP1(SLJIT_MOV
, R_CURR_INDEX
, 0, R_NEXT_HEAD
, 0);
2082 EMIT_OP1(SLJIT_MOV
, R_NEXT_STATE
, 0, SLJIT_MEM1(R_REGEX_MATCH
), SLJIT_OFFSETOF(struct regex_match
, next
));
2083 EMIT_OP1(SLJIT_MOV
, R_CURR_STATE
, 0, SLJIT_MEM1(R_REGEX_MATCH
), SLJIT_OFFSETOF(struct regex_match
, current
));
2084 EMIT_OP1(SLJIT_MOV
, R_NEXT_HEAD
, 0, SLJIT_MEM1(R_REGEX_MATCH
), SLJIT_OFFSETOF(struct regex_match
, head
));
2086 /* Update the start field of the locations. */
2087 CHECK(trace_transitions(0, &compiler_common
));
2088 while (compiler_common
.stack
.count
> 0) {
2089 ind
= stack_pop(&compiler_common
.stack
)->value
;
2090 if (compiler_common
.search_states
[ind
].type
>= 0) {
2091 EMIT_OP1(SLJIT_MOV
, SLJIT_MEM1(R_CURR_STATE
), TERM_OFFSET_OF(compiler_common
.search_states
[ind
].type
, 2), R_TEMP
, 0);
2093 compiler_common
.search_states
[ind
].value
= -1;
2095 EMIT_OP1(SLJIT_MOV
, SLJIT_MEM1(R_REGEX_MATCH
), SLJIT_OFFSETOF(struct regex_match
, fast_forward
), SLJIT_IMM
, 0);
2096 EMIT_JUMP(jump
, SLJIT_JUMP
);
2097 sljit_set_label(jump
, fast_forward_return_label
);
2099 /* Start fast-forward. */
2101 sljit_set_label(fast_forward_jump
, label
);
2103 /* Moving everything to registers. */
2104 EMIT_OP1(SLJIT_MOV
, SLJIT_MEM1(R_REGEX_MATCH
), SLJIT_OFFSETOF(struct regex_match
, next
), R_NEXT_STATE
, 0);
2105 EMIT_OP1(SLJIT_MOV
, SLJIT_MEM1(R_REGEX_MATCH
), SLJIT_OFFSETOF(struct regex_match
, current
), R_CURR_STATE
, 0);
2106 EMIT_OP1(SLJIT_MOV
, SLJIT_MEM1(R_REGEX_MATCH
), SLJIT_OFFSETOF(struct regex_match
, head
), R_NEXT_HEAD
, 0);
2107 EMIT_OP1(SLJIT_MOV
, R_NEXT_STATE
, 0, R_LENGTH
, 0);
2108 EMIT_OP1(SLJIT_MOV
, R_CURR_STATE
, 0, R_STRING
, 0);
2109 EMIT_OP1(SLJIT_MOV
, R_NEXT_HEAD
, 0, R_CURR_INDEX
, 0);
2111 /* Fast forward mainloop. */
2113 EMIT_OP2(SLJIT_SUB
| SLJIT_SET_Z
, R_NEXT_STATE
, 0, R_NEXT_STATE
, 0, SLJIT_IMM
, 1);
2114 EMIT_JUMP(fast_forward_jump
, SLJIT_EQUAL
);
2116 #ifdef REGEX_USE_8BIT_CHARS
2117 EMIT_OP1(SLJIT_MOV_U8
, R_CURR_CHAR
, 0, SLJIT_MEM1(R_CURR_STATE
), 0);
2118 EMIT_OP2(SLJIT_ADD
, R_CURR_STATE
, 0, R_CURR_STATE
, 0, SLJIT_IMM
, 1);
2120 EMIT_OP1(SLJIT_MOV_UH
, R_CURR_CHAR
, 0, SLJIT_MEM1(R_CURR_STATE
), 0);
2121 EMIT_OP2(SLJIT_ADD
, R_CURR_STATE
, 0, R_CURR_STATE
, 0, SLJIT_IMM
, 2);
2124 CHECK(trace_transitions(0, &compiler_common
));
2125 CHECK(compile_leave_fast_forward(&compiler_common
, fast_forward_label
));
2127 EMIT_OP2(SLJIT_ADD
, R_NEXT_HEAD
, 0, R_NEXT_HEAD
, 0, SLJIT_IMM
, 1);
2128 EMIT_JUMP(jump
, SLJIT_JUMP
);
2129 sljit_set_label(jump
, label
);
2131 /* String is finished. */
2133 sljit_set_label(fast_forward_jump
, label
);
2134 EMIT_OP1(SLJIT_MOV
, SLJIT_MEM1(R_REGEX_MATCH
), SLJIT_OFFSETOF(struct regex_match
, index
), R_NEXT_HEAD
, 0);
2135 EMIT_JUMP(fast_forward_jump
, SLJIT_JUMP
);
2139 if (end_check_jump
) {
2141 sljit_set_label(end_check_jump
, label
);
2143 if (!(compiler_common
.flags
& REGEX_MATCH_NON_GREEDY
) || !(compiler_common
.flags
& REGEX_MATCH_BEGIN
)) {
2144 CHECK(compile_end_check(&compiler_common
, end_check_label
));
2147 /* Since we leave, we do not need to update the R_BEST_BEGIN. */
2148 EMIT_OP1(SLJIT_MOV
, SLJIT_MEM1(R_REGEX_MATCH
), SLJIT_OFFSETOF(struct regex_match
, best_begin
), SLJIT_IMM
, 0);
2149 EMIT_OP1(SLJIT_MOV
, SLJIT_MEM1(R_REGEX_MATCH
), SLJIT_OFFSETOF(struct regex_match
, best_end
), R_CURR_INDEX
, 0);
2150 if (compiler_common
.flags
& REGEX_ID_CHECK
) {
2151 EMIT_OP1(SLJIT_MOV
, SLJIT_MEM1(R_REGEX_MATCH
), SLJIT_OFFSETOF(struct regex_match
, best_id
), SLJIT_MEM1(R_CURR_STATE
), TERM_REL_OFFSET_OF(0, 2));
2153 EMIT_OP1(SLJIT_MOV
, SLJIT_MEM1(R_REGEX_MATCH
), SLJIT_OFFSETOF(struct regex_match
, fast_quit
), SLJIT_IMM
, 1);
2154 EMIT_JUMP(non_greedy_end_jump
, SLJIT_JUMP
);
2159 if (best_match_check_jump
) {
2161 sljit_set_label(best_match_check_jump
, label
);
2163 if (!(compiler_common
.flags
& REGEX_MATCH_BEGIN
)) {
2164 EMIT_CMP(jump
, SLJIT_NOT_EQUAL
, R_NEXT_HEAD
, 0, SLJIT_IMM
, 0);
2165 sljit_set_label(jump
, start_label
);
2167 EMIT_OP1(SLJIT_MOV
, SLJIT_MEM1(R_REGEX_MATCH
), SLJIT_OFFSETOF(struct regex_match
, fast_quit
), SLJIT_IMM
, 1);
2170 /* Leaving matching and storing the necessary values. */
2172 sljit_set_label(length_is_zero_jump
, label
);
2173 if (non_greedy_end_jump
)
2174 sljit_set_label(non_greedy_end_jump
, label
);
2176 EMIT_OP1(SLJIT_MOV
, SLJIT_MEM1(R_REGEX_MATCH
), SLJIT_OFFSETOF(struct regex_match
, index
), R_CURR_INDEX
, 0);
2177 EMIT_OP1(SLJIT_MOV
, SLJIT_MEM1(R_REGEX_MATCH
), SLJIT_OFFSETOF(struct regex_match
, head
), R_NEXT_HEAD
, 0);
2178 EMIT_OP1(SLJIT_MOV
, SLJIT_MEM1(R_REGEX_MATCH
), SLJIT_OFFSETOF(struct regex_match
, next
), R_NEXT_STATE
, 0);
2179 EMIT_OP1(SLJIT_MOV
, SLJIT_MEM1(R_REGEX_MATCH
), SLJIT_OFFSETOF(struct regex_match
, current
), R_CURR_STATE
, 0);
2181 /* Exit from JIT. */
2183 sljit_set_label(best_match_found_jump
, label
);
2184 if (fast_forward_jump
)
2185 sljit_set_label(fast_forward_jump
, label
);
2186 CHECK(sljit_emit_return(compiler_common
.compiler
, SLJIT_UNUSED
, 0, 0));
2188 for (ind
= 1; ind
< compiler_common
.dfa_size
- 1; ind
++) {
2189 if (compiler_common
.search_states
[ind
].type
>= 0) {
2190 SLJIT_ASSERT(compiler_common
.search_states
[ind
].type
< compiler_common
.terms_size
);
2192 sljit_emit_op0(compiler_common
.compiler
, SLJIT_ENDBR
);
2193 compiler_common
.machine
->entry_addrs
[compiler_common
.search_states
[ind
].type
] = (sljit_uw
)label
;
2195 if (compiler_common
.dfa_transitions
[ind
].type
== type_char
) {
2196 EMIT_CMP(jump
, SLJIT_NOT_EQUAL
, R_CURR_CHAR
, 0, SLJIT_IMM
, compiler_common
.dfa_transitions
[ind
].value
);
2198 else if (compiler_common
.dfa_transitions
[ind
].type
== type_rng_start
) {
2199 ind
= compile_range_check(&compiler_common
, ind
);
2203 SLJIT_ASSERT(compiler_common
.dfa_transitions
[ind
].type
== type_newline
);
2204 CHECK(compile_newline_check(&compiler_common
, ind
));
2207 CHECK(trace_transitions(ind
, &compiler_common
));
2208 #ifdef REGEX_MATCH_VERBOSE
2209 if (compiler_common
.flags
& REGEX_MATCH_VERBOSE
)
2210 printf("(%3d): ", compiler_common
.search_states
[ind
].type
);
2212 CHECK(compile_cond_tran(&compiler_common
, compiler_common
.search_states
[ind
].type
));
2214 if (compiler_common
.dfa_transitions
[ind
].type
== type_char
) {
2216 sljit_set_label(jump
, label
);
2218 else if (compiler_common
.dfa_transitions
[ind
].type
== type_rng_end
) {
2220 range_set_label(compiler_common
.range_jump_list
, label
);
2223 SLJIT_ASSERT(compiler_common
.dfa_transitions
[ind
].type
== type_newline
);
2226 /* Branch to the next item in the list. */
2227 EMIT_OP1(SLJIT_MOV
, R_TEMP
, 0, SLJIT_MEM1(R_CURR_STATE
), TERM_OFFSET_OF(compiler_common
.search_states
[ind
].type
, 1));
2228 EMIT_OP1(SLJIT_MOV
, SLJIT_MEM1(R_CURR_STATE
), TERM_OFFSET_OF(compiler_common
.search_states
[ind
].type
, 1), SLJIT_IMM
, -1);
2229 CHECK(sljit_emit_ijump(compiler_common
.compiler
, SLJIT_JUMP
, SLJIT_MEM2(R_CURR_STATE
, R_TEMP
), 0));
2233 if (ind
== compiler_common
.dfa_size
- 1) {
2234 /* Generate an init stub function. */
2236 CHECK(sljit_emit_enter(compiler_common
.compiler
, 0, SLJIT_ARG1(SW
) | SLJIT_ARG2(SW
), 3, 3, 0, 0, 0));
2238 if (empty_match_id
== -1) {
2239 EMIT_OP1(SLJIT_MOV
, SLJIT_MEM1(SLJIT_S1
), SLJIT_OFFSETOF(struct regex_match
, best_begin
), SLJIT_IMM
, -1);
2240 EMIT_OP1(SLJIT_MOV
, SLJIT_MEM1(SLJIT_S1
), SLJIT_OFFSETOF(struct regex_match
, best_id
), SLJIT_IMM
, 0);
2243 EMIT_OP1(SLJIT_MOV
, SLJIT_MEM1(SLJIT_S1
), SLJIT_OFFSETOF(struct regex_match
, best_begin
), SLJIT_IMM
, 0);
2244 EMIT_OP1(SLJIT_MOV
, SLJIT_MEM1(SLJIT_S1
), SLJIT_OFFSETOF(struct regex_match
, best_id
), SLJIT_IMM
, empty_match_id
);
2247 EMIT_OP1(SLJIT_MOV
, SLJIT_MEM1(SLJIT_S1
), SLJIT_OFFSETOF(struct regex_match
, index
), SLJIT_IMM
, !(compiler_common
.flags
& REGEX_FAKE_MATCH_BEGIN
) ? 1 : 2);
2249 if (!(compiler_common
.flags
& REGEX_MATCH_NON_GREEDY
) || empty_match_id
== -1) {
2250 /* The else is a really rare event, so we still generate an empty function instead of a runtime pointer check. */
2251 SLJIT_ASSERT(R_CURR_STATE
== SLJIT_S0
);
2252 if (!(compiler_common
.flags
& REGEX_MATCH_BEGIN
)) {
2253 /* R_CURR_INDEX (put to R_TEMP) is zero. */
2254 EMIT_OP1(SLJIT_MOV
, R_TEMP
, 0, SLJIT_IMM
, 0);
2256 CHECK(compile_uncond_tran(&compiler_common
, R_CURR_STATE
));
2259 EMIT_OP1(SLJIT_MOV
, R_NEXT_HEAD
, 0, SLJIT_IMM
, 0);
2261 CHECK(sljit_emit_return(compiler_common
.compiler
, SLJIT_MOV
, R_NEXT_HEAD
, 0));
2263 compiler_common
.machine
->continue_match
= sljit_generate_code(compiler_common
.compiler
);
2264 #ifndef SLJIT_INDIRECT_CALL
2265 compiler_common
.machine
->u
.init_match
= (void*)(sljit_sw
)sljit_get_label_addr(label
);
2267 sljit_set_function_context(&compiler_common
.machine
->u
.init_match
, &compiler_common
.machine
->context
, sljit_get_label_addr(label
), regex_compile
);
2269 #ifdef REGEX_MATCH_VERBOSE
2270 if (compiler_common
.flags
& REGEX_MATCH_VERBOSE
)
2271 printf("Continue match: %p Init match: %p\n\n", compiler_common
.machine
->continue_match
, compiler_common
.machine
->u
.init_match
);
2273 if (compiler_common
.machine
->continue_match
) {
2274 for (ind
= 0; ind
< compiler_common
.terms_size
; ++ind
)
2275 compiler_common
.machine
->entry_addrs
[ind
] = sljit_get_label_addr((struct sljit_label
*)compiler_common
.machine
->entry_addrs
[ind
]);
2281 stack_destroy(&compiler_common
.stack
);
2282 stack_destroy(&compiler_common
.depth
);
2283 SLJIT_FREE(compiler_common
.dfa_transitions
, NULL
);
2284 SLJIT_FREE(compiler_common
.search_states
, NULL
);
2285 if (compiler_common
.range_jump_list
)
2286 SLJIT_FREE(compiler_common
.range_jump_list
, NULL
);
2287 if (compiler_common
.compiler
)
2288 sljit_free_compiler(compiler_common
.compiler
);
2290 return compiler_common
.machine
;
2292 if (compiler_common
.machine
) {
2293 SLJIT_FREE(compiler_common
.machine
, NULL
);
2296 *error
= REGEX_MEMORY_ERROR
;
2300 #undef TERM_OFFSET_OF
2310 void regex_free_machine(struct regex_machine
*machine
)
2312 sljit_free_code(machine
->continue_match
, NULL
);
2313 SLJIT_FREE(machine
, NULL
);
2316 const char* regex_get_platform_name(void)
2318 return sljit_get_platform_name();
2321 /* --------------------------------------------------------------------- */
2322 /* Mathching utilities */
2323 /* --------------------------------------------------------------------- */
2325 struct regex_match
* regex_begin_match(struct regex_machine
*machine
)
2330 sljit_sw
*entry_addrs
;
2332 struct regex_match
*match
= (struct regex_match
*)SLJIT_MALLOC(sizeof(struct regex_match
) + (machine
->size
* 2 - 1) * sizeof(sljit_sw
), NULL
);
2336 ptr1
= match
->states
;
2337 ptr2
= match
->states
+ machine
->size
;
2339 entry_addrs
= (sljit_sw
*)machine
->entry_addrs
;
2341 match
->current
= ptr1
;
2344 match
->machine
= machine
;
2346 /* Init machine states. */
2347 switch (machine
->no_states
) {
2349 while (ptr1
< end
) {
2350 *ptr1
++ = *entry_addrs
;
2351 *ptr2
++ = *entry_addrs
++;
2358 while (ptr1
< end
) {
2359 *ptr1
++ = *entry_addrs
;
2360 *ptr2
++ = *entry_addrs
++;
2369 while (ptr1
< end
) {
2370 *ptr1
++ = *entry_addrs
;
2371 *ptr2
++ = *entry_addrs
++;
2382 SLJIT_UNREACHABLE();
2386 SLJIT_ASSERT(ptr1
== end
);
2388 match
->u
.continue_match
= machine
->continue_match
;
2390 regex_reset_match(match
);
2394 void regex_reset_match(struct regex_match
*match
)
2396 struct regex_machine
*machine
= match
->machine
;
2397 sljit_sw current
, ind
;
2398 sljit_sw
*current_ptr
;
2400 match
->best_end
= 0;
2401 match
->fast_quit
= 0;
2402 match
->fast_forward
= 0;
2404 if (match
->head
!= 0) {
2405 /* Clear the current state. */
2406 current
= match
->head
;
2407 current_ptr
= match
->current
;
2409 ind
= (current
/ sizeof(sljit_sw
)) + 1;
2410 current
= current_ptr
[ind
];
2411 current_ptr
[ind
] = -1;
2412 } while (current
!= 0);
2414 match
->head
= machine
->u
.call_init(match
->current
, match
);
2417 void regex_free_match(struct regex_match
*match
)
2419 SLJIT_FREE(match
, NULL
);
2422 void regex_continue_match(struct regex_match
*match
, const regex_char_t
*input_string
, int length
)
2424 match
->u
.call_continue(match
, input_string
, length
);
2427 int regex_get_result(struct regex_match
*match
, int *end
, int *id
)
2429 int flags
= match
->machine
->flags
;
2432 *end
= match
->best_end
;
2433 *id
= match
->best_id
;
2434 if (!(flags
& (REGEX_MATCH_END
| REGEX_FAKE_MATCH_END
)))
2435 return match
->best_begin
;
2437 if (flags
& REGEX_FAKE_MATCH_END
) {
2438 SLJIT_ASSERT(!(flags
& (REGEX_MATCH_BEGIN
| REGEX_MATCH_END
)));
2439 if (match
->best_begin
!= -1)
2440 return match
->best_begin
;
2442 no_states
= match
->machine
->no_states
;
2443 if (match
->current
[no_states
+ 1] == -1)
2445 if (flags
& REGEX_ID_CHECK
)
2446 *id
= match
->current
[no_states
+ 3];
2447 if (!(flags
& REGEX_FAKE_MATCH_BEGIN
))
2448 *end
= match
->index
- 1;
2450 *end
= match
->index
- 2;
2451 return match
->current
[no_states
+ 2];
2454 /* Check the status of the last code. */
2455 if (!(flags
& REGEX_MATCH_BEGIN
)) {
2456 /* No shortcut in this case. */
2457 if (!(flags
& REGEX_ID_CHECK
)) {
2458 if (match
->current
[1] == -1)
2460 *end
= match
->index
- 1;
2461 return match
->current
[2];
2464 if (match
->current
[1] == -1)
2466 *end
= match
->index
- 1;
2467 *id
= match
->current
[3];
2468 return match
->current
[2];
2471 /* Shortcut is possible in this case. */
2472 if (!(flags
& REGEX_ID_CHECK
)) {
2473 if (match
->current
[1] == -1 || match
->head
== -1)
2475 *end
= match
->index
- 1;
2479 if (match
->current
[1] == -1 || match
->head
== -1)
2481 *end
= match
->index
- 1;
2482 *id
= match
->current
[2];
2487 int regex_is_match_finished(struct regex_match
*match
)
2489 return match
->fast_quit
;
2492 #ifdef REGEX_MATCH_VERBOSE
2493 void regex_continue_match_debug(struct regex_match
*match
, const regex_char_t
*input_string
, int length
)
2498 #if (defined SLJIT_DEBUG && SLJIT_DEBUG)
2501 sljit_sw no_states
= match
->machine
->no_states
;
2502 sljit_sw len
= match
->machine
->size
;
2504 while (length
> 0) {
2505 match
->u
.call_continue(match
, input_string
, 1);
2507 if (match
->fast_forward
) {
2508 if (match
->machine
->flags
& REGEX_MATCH_VERBOSE
)
2509 printf("fast forward\n");
2512 /* Verbose (first). */
2513 if (match
->machine
->flags
& REGEX_MATCH_VERBOSE
) {
2514 ptr
= match
->current
;
2517 printf("'%c' (%3ld->%3ld [%3ld]) ", *input_string
, (long)match
->best_begin
, (long)match
->best_end
, (long)match
->best_id
);
2519 printf("[%3ld:", (long)count
++);
2520 switch (no_states
) {
2530 printf("+,%3ld] ", (long)ptr
[2]);
2537 printf("+,%3ld,%3ld] ", (long)ptr
[2], (long)ptr
[3]);
2539 printf(" ,XXX,XXX] ");
2547 #if (defined SLJIT_DEBUG && SLJIT_DEBUG)
2548 /* Sanity check (later). */
2552 SLJIT_ASSERT(ptr
[1] == -1);
2556 /* Check number of active elements. */
2557 ptr
= match
->current
+ no_states
;
2558 end
= ptr
+ len
- no_states
;
2566 /* Check chain list. */
2567 current
= match
->head
;
2568 ptr
= match
->current
;
2569 while (current
!= 0) {
2570 SLJIT_ASSERT(current
>= 0 && current
< len
* sizeof(sljit_sw
));
2571 SLJIT_ASSERT((current
% (no_states
* sizeof(sljit_sw
))) == 0);
2572 SLJIT_ASSERT(count
> 0);
2573 current
= ptr
[(current
/ sizeof(sljit_sw
)) + 1];
2576 SLJIT_ASSERT(count
== 0);
2579 if (match
->fast_quit
) {
2580 /* the machine has stopped working. */
2581 if (match
->machine
->flags
& REGEX_MATCH_VERBOSE
)
2582 printf("Best match has found\n");