/[pcre]/code/trunk/pcre_jit_compile.c
ViewVC logotype

Contents of /code/trunk/pcre_jit_compile.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1577 - (show annotations)
Mon Jul 20 07:53:12 2015 UTC (4 years, 2 months ago) by zherczeg
File MIME type: text/plain
File size: 331237 byte(s)
Fix infinite recursion in the JIT compiler when certain patterns when certain patterns are analysed.
1 /*************************************************
2 * Perl-Compatible Regular Expressions *
3 *************************************************/
4
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.
7
8 Written by Philip Hazel
9 Copyright (c) 1997-2013 University of Cambridge
10
11 The machine code generator part (this module) was written by Zoltan Herczeg
12 Copyright (c) 2010-2013
13
14 -----------------------------------------------------------------------------
15 Redistribution and use in source and binary forms, with or without
16 modification, are permitted provided that the following conditions are met:
17
18 * Redistributions of source code must retain the above copyright notice,
19 this list of conditions and the following disclaimer.
20
21 * Redistributions in binary form must reproduce the above copyright
22 notice, this list of conditions and the following disclaimer in the
23 documentation and/or other materials provided with the distribution.
24
25 * Neither the name of the University of Cambridge nor the names of its
26 contributors may be used to endorse or promote products derived from
27 this software without specific prior written permission.
28
29 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
30 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
33 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
34 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
35 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
36 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
37 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
39 POSSIBILITY OF SUCH DAMAGE.
40 -----------------------------------------------------------------------------
41 */
42
43 #ifdef HAVE_CONFIG_H
44 #include "config.h"
45 #endif
46
47 #include "pcre_internal.h"
48
49 #if defined SUPPORT_JIT
50
51 /* All-in-one: Since we use the JIT compiler only from here,
52 we just include it. This way we don't need to touch the build
53 system files. */
54
55 #define SLJIT_MALLOC(size, allocator_data) (PUBL(malloc))(size)
56 #define SLJIT_FREE(ptr, allocator_data) (PUBL(free))(ptr)
57 #define SLJIT_CONFIG_AUTO 1
58 #define SLJIT_CONFIG_STATIC 1
59 #define SLJIT_VERBOSE 0
60 #define SLJIT_DEBUG 0
61
62 #include "sljit/sljitLir.c"
63
64 #if defined SLJIT_CONFIG_UNSUPPORTED && SLJIT_CONFIG_UNSUPPORTED
65 #error Unsupported architecture
66 #endif
67
68 /* Defines for debugging purposes. */
69
70 /* 1 - Use unoptimized capturing brackets.
71 2 - Enable capture_last_ptr (includes option 1). */
72 /* #define DEBUG_FORCE_UNOPTIMIZED_CBRAS 2 */
73
74 /* 1 - Always have a control head. */
75 /* #define DEBUG_FORCE_CONTROL_HEAD 1 */
76
77 /* Allocate memory for the regex stack on the real machine stack.
78 Fast, but limited size. */
79 #define MACHINE_STACK_SIZE 32768
80
81 /* Growth rate for stack allocated by the OS. Should be the multiply
82 of page size. */
83 #define STACK_GROWTH_RATE 8192
84
85 /* Enable to check that the allocation could destroy temporaries. */
86 #if defined SLJIT_DEBUG && SLJIT_DEBUG
87 #define DESTROY_REGISTERS 1
88 #endif
89
90 /*
91 Short summary about the backtracking mechanism empolyed by the jit code generator:
92
93 The code generator follows the recursive nature of the PERL compatible regular
94 expressions. The basic blocks of regular expressions are condition checkers
95 whose execute different commands depending on the result of the condition check.
96 The relationship between the operators can be horizontal (concatenation) and
97 vertical (sub-expression) (See struct backtrack_common for more details).
98
99 'ab' - 'a' and 'b' regexps are concatenated
100 'a+' - 'a' is the sub-expression of the '+' operator
101
102 The condition checkers are boolean (true/false) checkers. Machine code is generated
103 for the checker itself and for the actions depending on the result of the checker.
104 The 'true' case is called as the matching path (expected path), and the other is called as
105 the 'backtrack' path. Branch instructions are expesive for all CPUs, so we avoid taken
106 branches on the matching path.
107
108 Greedy star operator (*) :
109 Matching path: match happens.
110 Backtrack path: match failed.
111 Non-greedy star operator (*?) :
112 Matching path: no need to perform a match.
113 Backtrack path: match is required.
114
115 The following example shows how the code generated for a capturing bracket
116 with two alternatives. Let A, B, C, D are arbirary regular expressions, and
117 we have the following regular expression:
118
119 A(B|C)D
120
121 The generated code will be the following:
122
123 A matching path
124 '(' matching path (pushing arguments to the stack)
125 B matching path
126 ')' matching path (pushing arguments to the stack)
127 D matching path
128 return with successful match
129
130 D backtrack path
131 ')' backtrack path (If we arrived from "C" jump to the backtrack of "C")
132 B backtrack path
133 C expected path
134 jump to D matching path
135 C backtrack path
136 A backtrack path
137
138 Notice, that the order of backtrack code paths are the opposite of the fast
139 code paths. In this way the topmost value on the stack is always belong
140 to the current backtrack code path. The backtrack path must check
141 whether there is a next alternative. If so, it needs to jump back to
142 the matching path eventually. Otherwise it needs to clear out its own stack
143 frame and continue the execution on the backtrack code paths.
144 */
145
146 /*
147 Saved stack frames:
148
149 Atomic blocks and asserts require reloading the values of private data
150 when the backtrack mechanism performed. Because of OP_RECURSE, the data
151 are not necessarly known in compile time, thus we need a dynamic restore
152 mechanism.
153
154 The stack frames are stored in a chain list, and have the following format:
155 ([ capturing bracket offset ][ start value ][ end value ])+ ... [ 0 ] [ previous head ]
156
157 Thus we can restore the private data to a particular point in the stack.
158 */
159
160 typedef struct jit_arguments {
161 /* Pointers first. */
162 struct sljit_stack *stack;
163 const pcre_uchar *str;
164 const pcre_uchar *begin;
165 const pcre_uchar *end;
166 int *offsets;
167 pcre_uchar *uchar_ptr;
168 pcre_uchar *mark_ptr;
169 void *callout_data;
170 /* Everything else after. */
171 pcre_uint32 limit_match;
172 int real_offset_count;
173 int offset_count;
174 pcre_uint8 notbol;
175 pcre_uint8 noteol;
176 pcre_uint8 notempty;
177 pcre_uint8 notempty_atstart;
178 } jit_arguments;
179
180 typedef struct executable_functions {
181 void *executable_funcs[JIT_NUMBER_OF_COMPILE_MODES];
182 void *read_only_data_heads[JIT_NUMBER_OF_COMPILE_MODES];
183 sljit_uw executable_sizes[JIT_NUMBER_OF_COMPILE_MODES];
184 PUBL(jit_callback) callback;
185 void *userdata;
186 pcre_uint32 top_bracket;
187 pcre_uint32 limit_match;
188 } executable_functions;
189
190 typedef struct jump_list {
191 struct sljit_jump *jump;
192 struct jump_list *next;
193 } jump_list;
194
195 typedef struct stub_list {
196 struct sljit_jump *start;
197 struct sljit_label *quit;
198 struct stub_list *next;
199 } stub_list;
200
201 typedef struct label_addr_list {
202 struct sljit_label *label;
203 sljit_uw *update_addr;
204 struct label_addr_list *next;
205 } label_addr_list;
206
207 enum frame_types {
208 no_frame = -1,
209 no_stack = -2
210 };
211
212 enum control_types {
213 type_mark = 0,
214 type_then_trap = 1
215 };
216
217 typedef int (SLJIT_CALL *jit_function)(jit_arguments *args);
218
219 /* The following structure is the key data type for the recursive
220 code generator. It is allocated by compile_matchingpath, and contains
221 the arguments for compile_backtrackingpath. Must be the first member
222 of its descendants. */
223 typedef struct backtrack_common {
224 /* Concatenation stack. */
225 struct backtrack_common *prev;
226 jump_list *nextbacktracks;
227 /* Internal stack (for component operators). */
228 struct backtrack_common *top;
229 jump_list *topbacktracks;
230 /* Opcode pointer. */
231 pcre_uchar *cc;
232 } backtrack_common;
233
234 typedef struct assert_backtrack {
235 backtrack_common common;
236 jump_list *condfailed;
237 /* Less than 0 if a frame is not needed. */
238 int framesize;
239 /* Points to our private memory word on the stack. */
240 int private_data_ptr;
241 /* For iterators. */
242 struct sljit_label *matchingpath;
243 } assert_backtrack;
244
245 typedef struct bracket_backtrack {
246 backtrack_common common;
247 /* Where to coninue if an alternative is successfully matched. */
248 struct sljit_label *alternative_matchingpath;
249 /* For rmin and rmax iterators. */
250 struct sljit_label *recursive_matchingpath;
251 /* For greedy ? operator. */
252 struct sljit_label *zero_matchingpath;
253 /* Contains the branches of a failed condition. */
254 union {
255 /* Both for OP_COND, OP_SCOND. */
256 jump_list *condfailed;
257 assert_backtrack *assert;
258 /* For OP_ONCE. Less than 0 if not needed. */
259 int framesize;
260 } u;
261 /* Points to our private memory word on the stack. */
262 int private_data_ptr;
263 } bracket_backtrack;
264
265 typedef struct bracketpos_backtrack {
266 backtrack_common common;
267 /* Points to our private memory word on the stack. */
268 int private_data_ptr;
269 /* Reverting stack is needed. */
270 int framesize;
271 /* Allocated stack size. */
272 int stacksize;
273 } bracketpos_backtrack;
274
275 typedef struct braminzero_backtrack {
276 backtrack_common common;
277 struct sljit_label *matchingpath;
278 } braminzero_backtrack;
279
280 typedef struct iterator_backtrack {
281 backtrack_common common;
282 /* Next iteration. */
283 struct sljit_label *matchingpath;
284 } iterator_backtrack;
285
286 typedef struct recurse_entry {
287 struct recurse_entry *next;
288 /* Contains the function entry. */
289 struct sljit_label *entry;
290 /* Collects the calls until the function is not created. */
291 jump_list *calls;
292 /* Points to the starting opcode. */
293 sljit_sw start;
294 } recurse_entry;
295
296 typedef struct recurse_backtrack {
297 backtrack_common common;
298 BOOL inlined_pattern;
299 } recurse_backtrack;
300
301 #define OP_THEN_TRAP OP_TABLE_LENGTH
302
303 typedef struct then_trap_backtrack {
304 backtrack_common common;
305 /* If then_trap is not NULL, this structure contains the real
306 then_trap for the backtracking path. */
307 struct then_trap_backtrack *then_trap;
308 /* Points to the starting opcode. */
309 sljit_sw start;
310 /* Exit point for the then opcodes of this alternative. */
311 jump_list *quit;
312 /* Frame size of the current alternative. */
313 int framesize;
314 } then_trap_backtrack;
315
316 #define MAX_RANGE_SIZE 4
317
318 typedef struct compiler_common {
319 /* The sljit ceneric compiler. */
320 struct sljit_compiler *compiler;
321 /* First byte code. */
322 pcre_uchar *start;
323 /* Maps private data offset to each opcode. */
324 sljit_si *private_data_ptrs;
325 /* Chain list of read-only data ptrs. */
326 void *read_only_data_head;
327 /* Tells whether the capturing bracket is optimized. */
328 pcre_uint8 *optimized_cbracket;
329 /* Tells whether the starting offset is a target of then. */
330 pcre_uint8 *then_offsets;
331 /* Current position where a THEN must jump. */
332 then_trap_backtrack *then_trap;
333 /* Starting offset of private data for capturing brackets. */
334 int cbra_ptr;
335 /* Output vector starting point. Must be divisible by 2. */
336 int ovector_start;
337 /* Last known position of the requested byte. */
338 int req_char_ptr;
339 /* Head of the last recursion. */
340 int recursive_head_ptr;
341 /* First inspected character for partial matching. */
342 int start_used_ptr;
343 /* Starting pointer for partial soft matches. */
344 int hit_start;
345 /* End pointer of the first line. */
346 int first_line_end;
347 /* Points to the marked string. */
348 int mark_ptr;
349 /* Recursive control verb management chain. */
350 int control_head_ptr;
351 /* Points to the last matched capture block index. */
352 int capture_last_ptr;
353 /* Points to the starting position of the current match. */
354 int start_ptr;
355
356 /* Flipped and lower case tables. */
357 const pcre_uint8 *fcc;
358 sljit_sw lcc;
359 /* Mode can be PCRE_STUDY_JIT_COMPILE and others. */
360 int mode;
361 /* TRUE, when minlength is greater than 0. */
362 BOOL might_be_empty;
363 /* \K is found in the pattern. */
364 BOOL has_set_som;
365 /* (*SKIP:arg) is found in the pattern. */
366 BOOL has_skip_arg;
367 /* (*THEN) is found in the pattern. */
368 BOOL has_then;
369 /* Needs to know the start position anytime. */
370 BOOL needs_start_ptr;
371 /* Currently in recurse or negative assert. */
372 BOOL local_exit;
373 /* Currently in a positive assert. */
374 BOOL positive_assert;
375 /* Newline control. */
376 int nltype;
377 pcre_uint32 nlmax;
378 pcre_uint32 nlmin;
379 int newline;
380 int bsr_nltype;
381 pcre_uint32 bsr_nlmax;
382 pcre_uint32 bsr_nlmin;
383 /* Dollar endonly. */
384 int endonly;
385 /* Tables. */
386 sljit_sw ctypes;
387 /* Named capturing brackets. */
388 pcre_uchar *name_table;
389 sljit_sw name_count;
390 sljit_sw name_entry_size;
391
392 /* Labels and jump lists. */
393 struct sljit_label *partialmatchlabel;
394 struct sljit_label *quit_label;
395 struct sljit_label *forced_quit_label;
396 struct sljit_label *accept_label;
397 struct sljit_label *ff_newline_shortcut;
398 stub_list *stubs;
399 label_addr_list *label_addrs;
400 recurse_entry *entries;
401 recurse_entry *currententry;
402 jump_list *partialmatch;
403 jump_list *quit;
404 jump_list *positive_assert_quit;
405 jump_list *forced_quit;
406 jump_list *accept;
407 jump_list *calllimit;
408 jump_list *stackalloc;
409 jump_list *revertframes;
410 jump_list *wordboundary;
411 jump_list *anynewline;
412 jump_list *hspace;
413 jump_list *vspace;
414 jump_list *casefulcmp;
415 jump_list *caselesscmp;
416 jump_list *reset_match;
417 BOOL jscript_compat;
418 #ifdef SUPPORT_UTF
419 BOOL utf;
420 #ifdef SUPPORT_UCP
421 BOOL use_ucp;
422 #endif
423 #ifdef COMPILE_PCRE8
424 jump_list *utfreadchar;
425 jump_list *utfreadchar16;
426 jump_list *utfreadtype8;
427 #endif
428 #endif /* SUPPORT_UTF */
429 #ifdef SUPPORT_UCP
430 jump_list *getucd;
431 #endif
432 } compiler_common;
433
434 /* For byte_sequence_compare. */
435
436 typedef struct compare_context {
437 int length;
438 int sourcereg;
439 #if defined SLJIT_UNALIGNED && SLJIT_UNALIGNED
440 int ucharptr;
441 union {
442 sljit_si asint;
443 sljit_uh asushort;
444 #if defined COMPILE_PCRE8
445 sljit_ub asbyte;
446 sljit_ub asuchars[4];
447 #elif defined COMPILE_PCRE16
448 sljit_uh asuchars[2];
449 #elif defined COMPILE_PCRE32
450 sljit_ui asuchars[1];
451 #endif
452 } c;
453 union {
454 sljit_si asint;
455 sljit_uh asushort;
456 #if defined COMPILE_PCRE8
457 sljit_ub asbyte;
458 sljit_ub asuchars[4];
459 #elif defined COMPILE_PCRE16
460 sljit_uh asuchars[2];
461 #elif defined COMPILE_PCRE32
462 sljit_ui asuchars[1];
463 #endif
464 } oc;
465 #endif
466 } compare_context;
467
468 /* Undefine sljit macros. */
469 #undef CMP
470
471 /* Used for accessing the elements of the stack. */
472 #define STACK(i) ((-(i) - 1) * (int)sizeof(sljit_sw))
473
474 #define TMP1 SLJIT_R0
475 #define TMP2 SLJIT_R2
476 #define TMP3 SLJIT_R3
477 #define STR_PTR SLJIT_S0
478 #define STR_END SLJIT_S1
479 #define STACK_TOP SLJIT_R1
480 #define STACK_LIMIT SLJIT_S2
481 #define COUNT_MATCH SLJIT_S3
482 #define ARGUMENTS SLJIT_S4
483 #define RETURN_ADDR SLJIT_R4
484
485 /* Local space layout. */
486 /* These two locals can be used by the current opcode. */
487 #define LOCALS0 (0 * sizeof(sljit_sw))
488 #define LOCALS1 (1 * sizeof(sljit_sw))
489 /* Two local variables for possessive quantifiers (char1 cannot use them). */
490 #define POSSESSIVE0 (2 * sizeof(sljit_sw))
491 #define POSSESSIVE1 (3 * sizeof(sljit_sw))
492 /* Max limit of recursions. */
493 #define LIMIT_MATCH (4 * sizeof(sljit_sw))
494 /* The output vector is stored on the stack, and contains pointers
495 to characters. The vector data is divided into two groups: the first
496 group contains the start / end character pointers, and the second is
497 the start pointers when the end of the capturing group has not yet reached. */
498 #define OVECTOR_START (common->ovector_start)
499 #define OVECTOR(i) (OVECTOR_START + (i) * (sljit_sw)sizeof(sljit_sw))
500 #define OVECTOR_PRIV(i) (common->cbra_ptr + (i) * (sljit_sw)sizeof(sljit_sw))
501 #define PRIVATE_DATA(cc) (common->private_data_ptrs[(cc) - common->start])
502
503 #if defined COMPILE_PCRE8
504 #define MOV_UCHAR SLJIT_MOV_UB
505 #define MOVU_UCHAR SLJIT_MOVU_UB
506 #elif defined COMPILE_PCRE16
507 #define MOV_UCHAR SLJIT_MOV_UH
508 #define MOVU_UCHAR SLJIT_MOVU_UH
509 #elif defined COMPILE_PCRE32
510 #define MOV_UCHAR SLJIT_MOV_UI
511 #define MOVU_UCHAR SLJIT_MOVU_UI
512 #else
513 #error Unsupported compiling mode
514 #endif
515
516 /* Shortcuts. */
517 #define DEFINE_COMPILER \
518 struct sljit_compiler *compiler = common->compiler
519 #define OP1(op, dst, dstw, src, srcw) \
520 sljit_emit_op1(compiler, (op), (dst), (dstw), (src), (srcw))
521 #define OP2(op, dst, dstw, src1, src1w, src2, src2w) \
522 sljit_emit_op2(compiler, (op), (dst), (dstw), (src1), (src1w), (src2), (src2w))
523 #define LABEL() \
524 sljit_emit_label(compiler)
525 #define JUMP(type) \
526 sljit_emit_jump(compiler, (type))
527 #define JUMPTO(type, label) \
528 sljit_set_label(sljit_emit_jump(compiler, (type)), (label))
529 #define JUMPHERE(jump) \
530 sljit_set_label((jump), sljit_emit_label(compiler))
531 #define SET_LABEL(jump, label) \
532 sljit_set_label((jump), (label))
533 #define CMP(type, src1, src1w, src2, src2w) \
534 sljit_emit_cmp(compiler, (type), (src1), (src1w), (src2), (src2w))
535 #define CMPTO(type, src1, src1w, src2, src2w, label) \
536 sljit_set_label(sljit_emit_cmp(compiler, (type), (src1), (src1w), (src2), (src2w)), (label))
537 #define OP_FLAGS(op, dst, dstw, src, srcw, type) \
538 sljit_emit_op_flags(compiler, (op), (dst), (dstw), (src), (srcw), (type))
539 #define GET_LOCAL_BASE(dst, dstw, offset) \
540 sljit_get_local_base(compiler, (dst), (dstw), (offset))
541
542 #define READ_CHAR_MAX 0x7fffffff
543
544 static pcre_uchar *bracketend(pcre_uchar *cc)
545 {
546 SLJIT_ASSERT((*cc >= OP_ASSERT && *cc <= OP_ASSERTBACK_NOT) || (*cc >= OP_ONCE && *cc <= OP_SCOND));
547 do cc += GET(cc, 1); while (*cc == OP_ALT);
548 SLJIT_ASSERT(*cc >= OP_KET && *cc <= OP_KETRPOS);
549 cc += 1 + LINK_SIZE;
550 return cc;
551 }
552
553 static int no_alternatives(pcre_uchar *cc)
554 {
555 int count = 0;
556 SLJIT_ASSERT((*cc >= OP_ASSERT && *cc <= OP_ASSERTBACK_NOT) || (*cc >= OP_ONCE && *cc <= OP_SCOND));
557 do
558 {
559 cc += GET(cc, 1);
560 count++;
561 }
562 while (*cc == OP_ALT);
563 SLJIT_ASSERT(*cc >= OP_KET && *cc <= OP_KETRPOS);
564 return count;
565 }
566
567 static int ones_in_half_byte[16] = {
568 /* 0 */ 0, 1, 1, 2, /* 4 */ 1, 2, 2, 3,
569 /* 8 */ 1, 2, 2, 3, /* 12 */ 2, 3, 3, 4
570 };
571
572 /* Functions whose might need modification for all new supported opcodes:
573 next_opcode
574 check_opcode_types
575 set_private_data_ptrs
576 get_framesize
577 init_frame
578 get_private_data_copy_length
579 copy_private_data
580 compile_matchingpath
581 compile_backtrackingpath
582 */
583
584 static pcre_uchar *next_opcode(compiler_common *common, pcre_uchar *cc)
585 {
586 SLJIT_UNUSED_ARG(common);
587 switch(*cc)
588 {
589 case OP_SOD:
590 case OP_SOM:
591 case OP_SET_SOM:
592 case OP_NOT_WORD_BOUNDARY:
593 case OP_WORD_BOUNDARY:
594 case OP_NOT_DIGIT:
595 case OP_DIGIT:
596 case OP_NOT_WHITESPACE:
597 case OP_WHITESPACE:
598 case OP_NOT_WORDCHAR:
599 case OP_WORDCHAR:
600 case OP_ANY:
601 case OP_ALLANY:
602 case OP_NOTPROP:
603 case OP_PROP:
604 case OP_ANYNL:
605 case OP_NOT_HSPACE:
606 case OP_HSPACE:
607 case OP_NOT_VSPACE:
608 case OP_VSPACE:
609 case OP_EXTUNI:
610 case OP_EODN:
611 case OP_EOD:
612 case OP_CIRC:
613 case OP_CIRCM:
614 case OP_DOLL:
615 case OP_DOLLM:
616 case OP_CRSTAR:
617 case OP_CRMINSTAR:
618 case OP_CRPLUS:
619 case OP_CRMINPLUS:
620 case OP_CRQUERY:
621 case OP_CRMINQUERY:
622 case OP_CRRANGE:
623 case OP_CRMINRANGE:
624 case OP_CRPOSSTAR:
625 case OP_CRPOSPLUS:
626 case OP_CRPOSQUERY:
627 case OP_CRPOSRANGE:
628 case OP_CLASS:
629 case OP_NCLASS:
630 case OP_REF:
631 case OP_REFI:
632 case OP_DNREF:
633 case OP_DNREFI:
634 case OP_RECURSE:
635 case OP_CALLOUT:
636 case OP_ALT:
637 case OP_KET:
638 case OP_KETRMAX:
639 case OP_KETRMIN:
640 case OP_KETRPOS:
641 case OP_REVERSE:
642 case OP_ASSERT:
643 case OP_ASSERT_NOT:
644 case OP_ASSERTBACK:
645 case OP_ASSERTBACK_NOT:
646 case OP_ONCE:
647 case OP_ONCE_NC:
648 case OP_BRA:
649 case OP_BRAPOS:
650 case OP_CBRA:
651 case OP_CBRAPOS:
652 case OP_COND:
653 case OP_SBRA:
654 case OP_SBRAPOS:
655 case OP_SCBRA:
656 case OP_SCBRAPOS:
657 case OP_SCOND:
658 case OP_CREF:
659 case OP_DNCREF:
660 case OP_RREF:
661 case OP_DNRREF:
662 case OP_DEF:
663 case OP_BRAZERO:
664 case OP_BRAMINZERO:
665 case OP_BRAPOSZERO:
666 case OP_PRUNE:
667 case OP_SKIP:
668 case OP_THEN:
669 case OP_COMMIT:
670 case OP_FAIL:
671 case OP_ACCEPT:
672 case OP_ASSERT_ACCEPT:
673 case OP_CLOSE:
674 case OP_SKIPZERO:
675 return cc + PRIV(OP_lengths)[*cc];
676
677 case OP_CHAR:
678 case OP_CHARI:
679 case OP_NOT:
680 case OP_NOTI:
681 case OP_STAR:
682 case OP_MINSTAR:
683 case OP_PLUS:
684 case OP_MINPLUS:
685 case OP_QUERY:
686 case OP_MINQUERY:
687 case OP_UPTO:
688 case OP_MINUPTO:
689 case OP_EXACT:
690 case OP_POSSTAR:
691 case OP_POSPLUS:
692 case OP_POSQUERY:
693 case OP_POSUPTO:
694 case OP_STARI:
695 case OP_MINSTARI:
696 case OP_PLUSI:
697 case OP_MINPLUSI:
698 case OP_QUERYI:
699 case OP_MINQUERYI:
700 case OP_UPTOI:
701 case OP_MINUPTOI:
702 case OP_EXACTI:
703 case OP_POSSTARI:
704 case OP_POSPLUSI:
705 case OP_POSQUERYI:
706 case OP_POSUPTOI:
707 case OP_NOTSTAR:
708 case OP_NOTMINSTAR:
709 case OP_NOTPLUS:
710 case OP_NOTMINPLUS:
711 case OP_NOTQUERY:
712 case OP_NOTMINQUERY:
713 case OP_NOTUPTO:
714 case OP_NOTMINUPTO:
715 case OP_NOTEXACT:
716 case OP_NOTPOSSTAR:
717 case OP_NOTPOSPLUS:
718 case OP_NOTPOSQUERY:
719 case OP_NOTPOSUPTO:
720 case OP_NOTSTARI:
721 case OP_NOTMINSTARI:
722 case OP_NOTPLUSI:
723 case OP_NOTMINPLUSI:
724 case OP_NOTQUERYI:
725 case OP_NOTMINQUERYI:
726 case OP_NOTUPTOI:
727 case OP_NOTMINUPTOI:
728 case OP_NOTEXACTI:
729 case OP_NOTPOSSTARI:
730 case OP_NOTPOSPLUSI:
731 case OP_NOTPOSQUERYI:
732 case OP_NOTPOSUPTOI:
733 cc += PRIV(OP_lengths)[*cc];
734 #ifdef SUPPORT_UTF
735 if (common->utf && HAS_EXTRALEN(cc[-1])) cc += GET_EXTRALEN(cc[-1]);
736 #endif
737 return cc;
738
739 /* Special cases. */
740 case OP_TYPESTAR:
741 case OP_TYPEMINSTAR:
742 case OP_TYPEPLUS:
743 case OP_TYPEMINPLUS:
744 case OP_TYPEQUERY:
745 case OP_TYPEMINQUERY:
746 case OP_TYPEUPTO:
747 case OP_TYPEMINUPTO:
748 case OP_TYPEEXACT:
749 case OP_TYPEPOSSTAR:
750 case OP_TYPEPOSPLUS:
751 case OP_TYPEPOSQUERY:
752 case OP_TYPEPOSUPTO:
753 return cc + PRIV(OP_lengths)[*cc] - 1;
754
755 case OP_ANYBYTE:
756 #ifdef SUPPORT_UTF
757 if (common->utf) return NULL;
758 #endif
759 return cc + 1;
760
761 #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
762 case OP_XCLASS:
763 return cc + GET(cc, 1);
764 #endif
765
766 case OP_MARK:
767 case OP_PRUNE_ARG:
768 case OP_SKIP_ARG:
769 case OP_THEN_ARG:
770 return cc + 1 + 2 + cc[1];
771
772 default:
773 /* All opcodes are supported now! */
774 SLJIT_ASSERT_STOP();
775 return NULL;
776 }
777 }
778
779 static BOOL check_opcode_types(compiler_common *common, pcre_uchar *cc, pcre_uchar *ccend)
780 {
781 int count;
782 pcre_uchar *slot;
783
784 /* Calculate important variables (like stack size) and checks whether all opcodes are supported. */
785 while (cc < ccend)
786 {
787 switch(*cc)
788 {
789 case OP_SET_SOM:
790 common->has_set_som = TRUE;
791 common->might_be_empty = TRUE;
792 cc += 1;
793 break;
794
795 case OP_REF:
796 case OP_REFI:
797 common->optimized_cbracket[GET2(cc, 1)] = 0;
798 cc += 1 + IMM2_SIZE;
799 break;
800
801 case OP_CBRAPOS:
802 case OP_SCBRAPOS:
803 common->optimized_cbracket[GET2(cc, 1 + LINK_SIZE)] = 0;
804 cc += 1 + LINK_SIZE + IMM2_SIZE;
805 break;
806
807 case OP_COND:
808 case OP_SCOND:
809 /* Only AUTO_CALLOUT can insert this opcode. We do
810 not intend to support this case. */
811 if (cc[1 + LINK_SIZE] == OP_CALLOUT)
812 return FALSE;
813 cc += 1 + LINK_SIZE;
814 break;
815
816 case OP_CREF:
817 common->optimized_cbracket[GET2(cc, 1)] = 0;
818 cc += 1 + IMM2_SIZE;
819 break;
820
821 case OP_DNREF:
822 case OP_DNREFI:
823 case OP_DNCREF:
824 count = GET2(cc, 1 + IMM2_SIZE);
825 slot = common->name_table + GET2(cc, 1) * common->name_entry_size;
826 while (count-- > 0)
827 {
828 common->optimized_cbracket[GET2(slot, 0)] = 0;
829 slot += common->name_entry_size;
830 }
831 cc += 1 + 2 * IMM2_SIZE;
832 break;
833
834 case OP_RECURSE:
835 /* Set its value only once. */
836 if (common->recursive_head_ptr == 0)
837 {
838 common->recursive_head_ptr = common->ovector_start;
839 common->ovector_start += sizeof(sljit_sw);
840 }
841 cc += 1 + LINK_SIZE;
842 break;
843
844 case OP_CALLOUT:
845 if (common->capture_last_ptr == 0)
846 {
847 common->capture_last_ptr = common->ovector_start;
848 common->ovector_start += sizeof(sljit_sw);
849 }
850 cc += 2 + 2 * LINK_SIZE;
851 break;
852
853 case OP_THEN_ARG:
854 common->has_then = TRUE;
855 common->control_head_ptr = 1;
856 /* Fall through. */
857
858 case OP_PRUNE_ARG:
859 common->needs_start_ptr = TRUE;
860 /* Fall through. */
861
862 case OP_MARK:
863 if (common->mark_ptr == 0)
864 {
865 common->mark_ptr = common->ovector_start;
866 common->ovector_start += sizeof(sljit_sw);
867 }
868 cc += 1 + 2 + cc[1];
869 break;
870
871 case OP_THEN:
872 common->has_then = TRUE;
873 common->control_head_ptr = 1;
874 /* Fall through. */
875
876 case OP_PRUNE:
877 case OP_SKIP:
878 common->needs_start_ptr = TRUE;
879 cc += 1;
880 break;
881
882 case OP_SKIP_ARG:
883 common->control_head_ptr = 1;
884 common->has_skip_arg = TRUE;
885 cc += 1 + 2 + cc[1];
886 break;
887
888 default:
889 cc = next_opcode(common, cc);
890 if (cc == NULL)
891 return FALSE;
892 break;
893 }
894 }
895 return TRUE;
896 }
897
898 static int get_class_iterator_size(pcre_uchar *cc)
899 {
900 switch(*cc)
901 {
902 case OP_CRSTAR:
903 case OP_CRPLUS:
904 return 2;
905
906 case OP_CRMINSTAR:
907 case OP_CRMINPLUS:
908 case OP_CRQUERY:
909 case OP_CRMINQUERY:
910 return 1;
911
912 case OP_CRRANGE:
913 case OP_CRMINRANGE:
914 if (GET2(cc, 1) == GET2(cc, 1 + IMM2_SIZE))
915 return 0;
916 return 2;
917
918 default:
919 return 0;
920 }
921 }
922
923 static BOOL detect_repeat(compiler_common *common, pcre_uchar *begin)
924 {
925 pcre_uchar *end = bracketend(begin);
926 pcre_uchar *next;
927 pcre_uchar *next_end;
928 pcre_uchar *max_end;
929 pcre_uchar type;
930 sljit_sw length = end - begin;
931 int min, max, i;
932
933 /* Detect fixed iterations first. */
934 if (end[-(1 + LINK_SIZE)] != OP_KET)
935 return FALSE;
936
937 /* Already detected repeat. */
938 if (common->private_data_ptrs[end - common->start - LINK_SIZE] != 0)
939 return TRUE;
940
941 next = end;
942 min = 1;
943 while (1)
944 {
945 if (*next != *begin)
946 break;
947 next_end = bracketend(next);
948 if (next_end - next != length || memcmp(begin, next, IN_UCHARS(length)) != 0)
949 break;
950 next = next_end;
951 min++;
952 }
953
954 if (min == 2)
955 return FALSE;
956
957 max = 0;
958 max_end = next;
959 if (*next == OP_BRAZERO || *next == OP_BRAMINZERO)
960 {
961 type = *next;
962 while (1)
963 {
964 if (next[0] != type || next[1] != OP_BRA || next[2 + LINK_SIZE] != *begin)
965 break;
966 next_end = bracketend(next + 2 + LINK_SIZE);
967 if (next_end - next != (length + 2 + LINK_SIZE) || memcmp(begin, next + 2 + LINK_SIZE, IN_UCHARS(length)) != 0)
968 break;
969 next = next_end;
970 max++;
971 }
972
973 if (next[0] == type && next[1] == *begin && max >= 1)
974 {
975 next_end = bracketend(next + 1);
976 if (next_end - next == (length + 1) && memcmp(begin, next + 1, IN_UCHARS(length)) == 0)
977 {
978 for (i = 0; i < max; i++, next_end += 1 + LINK_SIZE)
979 if (*next_end != OP_KET)
980 break;
981
982 if (i == max)
983 {
984 common->private_data_ptrs[max_end - common->start - LINK_SIZE] = next_end - max_end;
985 common->private_data_ptrs[max_end - common->start - LINK_SIZE + 1] = (type == OP_BRAZERO) ? OP_UPTO : OP_MINUPTO;
986 /* +2 the original and the last. */
987 common->private_data_ptrs[max_end - common->start - LINK_SIZE + 2] = max + 2;
988 if (min == 1)
989 return TRUE;
990 min--;
991 max_end -= (1 + LINK_SIZE) + GET(max_end, -LINK_SIZE);
992 }
993 }
994 }
995 }
996
997 if (min >= 3)
998 {
999 common->private_data_ptrs[end - common->start - LINK_SIZE] = max_end - end;
1000 common->private_data_ptrs[end - common->start - LINK_SIZE + 1] = OP_EXACT;
1001 common->private_data_ptrs[end - common->start - LINK_SIZE + 2] = min;
1002 return TRUE;
1003 }
1004
1005 return FALSE;
1006 }
1007
1008 #define CASE_ITERATOR_PRIVATE_DATA_1 \
1009 case OP_MINSTAR: \
1010 case OP_MINPLUS: \
1011 case OP_QUERY: \
1012 case OP_MINQUERY: \
1013 case OP_MINSTARI: \
1014 case OP_MINPLUSI: \
1015 case OP_QUERYI: \
1016 case OP_MINQUERYI: \
1017 case OP_NOTMINSTAR: \
1018 case OP_NOTMINPLUS: \
1019 case OP_NOTQUERY: \
1020 case OP_NOTMINQUERY: \
1021 case OP_NOTMINSTARI: \
1022 case OP_NOTMINPLUSI: \
1023 case OP_NOTQUERYI: \
1024 case OP_NOTMINQUERYI:
1025
1026 #define CASE_ITERATOR_PRIVATE_DATA_2A \
1027 case OP_STAR: \
1028 case OP_PLUS: \
1029 case OP_STARI: \
1030 case OP_PLUSI: \
1031 case OP_NOTSTAR: \
1032 case OP_NOTPLUS: \
1033 case OP_NOTSTARI: \
1034 case OP_NOTPLUSI:
1035
1036 #define CASE_ITERATOR_PRIVATE_DATA_2B \
1037 case OP_UPTO: \
1038 case OP_MINUPTO: \
1039 case OP_UPTOI: \
1040 case OP_MINUPTOI: \
1041 case OP_NOTUPTO: \
1042 case OP_NOTMINUPTO: \
1043 case OP_NOTUPTOI: \
1044 case OP_NOTMINUPTOI:
1045
1046 #define CASE_ITERATOR_TYPE_PRIVATE_DATA_1 \
1047 case OP_TYPEMINSTAR: \
1048 case OP_TYPEMINPLUS: \
1049 case OP_TYPEQUERY: \
1050 case OP_TYPEMINQUERY:
1051
1052 #define CASE_ITERATOR_TYPE_PRIVATE_DATA_2A \
1053 case OP_TYPESTAR: \
1054 case OP_TYPEPLUS:
1055
1056 #define CASE_ITERATOR_TYPE_PRIVATE_DATA_2B \
1057 case OP_TYPEUPTO: \
1058 case OP_TYPEMINUPTO:
1059
1060 static void set_private_data_ptrs(compiler_common *common, int *private_data_start, pcre_uchar *ccend)
1061 {
1062 pcre_uchar *cc = common->start;
1063 pcre_uchar *alternative;
1064 pcre_uchar *end = NULL;
1065 int private_data_ptr = *private_data_start;
1066 int space, size, bracketlen;
1067
1068 while (cc < ccend)
1069 {
1070 space = 0;
1071 size = 0;
1072 bracketlen = 0;
1073 if (private_data_ptr > SLJIT_MAX_LOCAL_SIZE)
1074 break;
1075
1076 if (*cc == OP_ONCE || *cc == OP_ONCE_NC || *cc == OP_BRA || *cc == OP_CBRA || *cc == OP_COND)
1077 if (detect_repeat(common, cc))
1078 {
1079 /* These brackets are converted to repeats, so no global
1080 based single character repeat is allowed. */
1081 if (cc >= end)
1082 end = bracketend(cc);
1083 }
1084
1085 switch(*cc)
1086 {
1087 case OP_KET:
1088 if (common->private_data_ptrs[cc + 1 - common->start] != 0)
1089 {
1090 common->private_data_ptrs[cc - common->start] = private_data_ptr;
1091 private_data_ptr += sizeof(sljit_sw);
1092 cc += common->private_data_ptrs[cc + 1 - common->start];
1093 }
1094 cc += 1 + LINK_SIZE;
1095 break;
1096
1097 case OP_ASSERT:
1098 case OP_ASSERT_NOT:
1099 case OP_ASSERTBACK:
1100 case OP_ASSERTBACK_NOT:
1101 case OP_ONCE:
1102 case OP_ONCE_NC:
1103 case OP_BRAPOS:
1104 case OP_SBRA:
1105 case OP_SBRAPOS:
1106 case OP_SCOND:
1107 common->private_data_ptrs[cc - common->start] = private_data_ptr;
1108 private_data_ptr += sizeof(sljit_sw);
1109 bracketlen = 1 + LINK_SIZE;
1110 break;
1111
1112 case OP_CBRAPOS:
1113 case OP_SCBRAPOS:
1114 common->private_data_ptrs[cc - common->start] = private_data_ptr;
1115 private_data_ptr += sizeof(sljit_sw);
1116 bracketlen = 1 + LINK_SIZE + IMM2_SIZE;
1117 break;
1118
1119 case OP_COND:
1120 /* Might be a hidden SCOND. */
1121 alternative = cc + GET(cc, 1);
1122 if (*alternative == OP_KETRMAX || *alternative == OP_KETRMIN)
1123 {
1124 common->private_data_ptrs[cc - common->start] = private_data_ptr;
1125 private_data_ptr += sizeof(sljit_sw);
1126 }
1127 bracketlen = 1 + LINK_SIZE;
1128 break;
1129
1130 case OP_BRA:
1131 bracketlen = 1 + LINK_SIZE;
1132 break;
1133
1134 case OP_CBRA:
1135 case OP_SCBRA:
1136 bracketlen = 1 + LINK_SIZE + IMM2_SIZE;
1137 break;
1138
1139 CASE_ITERATOR_PRIVATE_DATA_1
1140 space = 1;
1141 size = -2;
1142 break;
1143
1144 CASE_ITERATOR_PRIVATE_DATA_2A
1145 space = 2;
1146 size = -2;
1147 break;
1148
1149 CASE_ITERATOR_PRIVATE_DATA_2B
1150 space = 2;
1151 size = -(2 + IMM2_SIZE);
1152 break;
1153
1154 CASE_ITERATOR_TYPE_PRIVATE_DATA_1
1155 space = 1;
1156 size = 1;
1157 break;
1158
1159 CASE_ITERATOR_TYPE_PRIVATE_DATA_2A
1160 if (cc[1] != OP_ANYNL && cc[1] != OP_EXTUNI)
1161 space = 2;
1162 size = 1;
1163 break;
1164
1165 case OP_TYPEUPTO:
1166 if (cc[1 + IMM2_SIZE] != OP_ANYNL && cc[1 + IMM2_SIZE] != OP_EXTUNI)
1167 space = 2;
1168 size = 1 + IMM2_SIZE;
1169 break;
1170
1171 case OP_TYPEMINUPTO:
1172 space = 2;
1173 size = 1 + IMM2_SIZE;
1174 break;
1175
1176 case OP_CLASS:
1177 case OP_NCLASS:
1178 size += 1 + 32 / sizeof(pcre_uchar);
1179 space = get_class_iterator_size(cc + size);
1180 break;
1181
1182 #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
1183 case OP_XCLASS:
1184 size = GET(cc, 1);
1185 space = get_class_iterator_size(cc + size);
1186 break;
1187 #endif
1188
1189 default:
1190 cc = next_opcode(common, cc);
1191 SLJIT_ASSERT(cc != NULL);
1192 break;
1193 }
1194
1195 /* Character iterators, which are not inside a repeated bracket,
1196 gets a private slot instead of allocating it on the stack. */
1197 if (space > 0 && cc >= end)
1198 {
1199 common->private_data_ptrs[cc - common->start] = private_data_ptr;
1200 private_data_ptr += sizeof(sljit_sw) * space;
1201 }
1202
1203 if (size != 0)
1204 {
1205 if (size < 0)
1206 {
1207 cc += -size;
1208 #ifdef SUPPORT_UTF
1209 if (common->utf && HAS_EXTRALEN(cc[-1])) cc += GET_EXTRALEN(cc[-1]);
1210 #endif
1211 }
1212 else
1213 cc += size;
1214 }
1215
1216 if (bracketlen > 0)
1217 {
1218 if (cc >= end)
1219 {
1220 end = bracketend(cc);
1221 if (end[-1 - LINK_SIZE] == OP_KET)
1222 end = NULL;
1223 }
1224 cc += bracketlen;
1225 }
1226 }
1227 *private_data_start = private_data_ptr;
1228 }
1229
1230 /* Returns with a frame_types (always < 0) if no need for frame. */
1231 static int get_framesize(compiler_common *common, pcre_uchar *cc, pcre_uchar *ccend, BOOL recursive, BOOL *needs_control_head)
1232 {
1233 int length = 0;
1234 int possessive = 0;
1235 BOOL stack_restore = FALSE;
1236 BOOL setsom_found = recursive;
1237 BOOL setmark_found = recursive;
1238 /* The last capture is a local variable even for recursions. */
1239 BOOL capture_last_found = FALSE;
1240
1241 #if defined DEBUG_FORCE_CONTROL_HEAD && DEBUG_FORCE_CONTROL_HEAD
1242 SLJIT_ASSERT(common->control_head_ptr != 0);
1243 *needs_control_head = TRUE;
1244 #else
1245 *needs_control_head = FALSE;
1246 #endif
1247
1248 if (ccend == NULL)
1249 {
1250 ccend = bracketend(cc) - (1 + LINK_SIZE);
1251 if (!recursive && (*cc == OP_CBRAPOS || *cc == OP_SCBRAPOS))
1252 {
1253 possessive = length = (common->capture_last_ptr != 0) ? 5 : 3;
1254 /* This is correct regardless of common->capture_last_ptr. */
1255 capture_last_found = TRUE;
1256 }
1257 cc = next_opcode(common, cc);
1258 }
1259
1260 SLJIT_ASSERT(cc != NULL);
1261 while (cc < ccend)
1262 switch(*cc)
1263 {
1264 case OP_SET_SOM:
1265 SLJIT_ASSERT(common->has_set_som);
1266 stack_restore = TRUE;
1267 if (!setsom_found)
1268 {
1269 length += 2;
1270 setsom_found = TRUE;
1271 }
1272 cc += 1;
1273 break;
1274
1275 case OP_MARK:
1276 case OP_PRUNE_ARG:
1277 case OP_THEN_ARG:
1278 SLJIT_ASSERT(common->mark_ptr != 0);
1279 stack_restore = TRUE;
1280 if (!setmark_found)
1281 {
1282 length += 2;
1283 setmark_found = TRUE;
1284 }
1285 if (common->control_head_ptr != 0)
1286 *needs_control_head = TRUE;
1287 cc += 1 + 2 + cc[1];
1288 break;
1289
1290 case OP_RECURSE:
1291 stack_restore = TRUE;
1292 if (common->has_set_som && !setsom_found)
1293 {
1294 length += 2;
1295 setsom_found = TRUE;
1296 }
1297 if (common->mark_ptr != 0 && !setmark_found)
1298 {
1299 length += 2;
1300 setmark_found = TRUE;
1301 }
1302 if (common->capture_last_ptr != 0 && !capture_last_found)
1303 {
1304 length += 2;
1305 capture_last_found = TRUE;
1306 }
1307 cc += 1 + LINK_SIZE;
1308 break;
1309
1310 case OP_CBRA:
1311 case OP_CBRAPOS:
1312 case OP_SCBRA:
1313 case OP_SCBRAPOS:
1314 stack_restore = TRUE;
1315 if (common->capture_last_ptr != 0 && !capture_last_found)
1316 {
1317 length += 2;
1318 capture_last_found = TRUE;
1319 }
1320 length += 3;
1321 cc += 1 + LINK_SIZE + IMM2_SIZE;
1322 break;
1323
1324 default:
1325 stack_restore = TRUE;
1326 /* Fall through. */
1327
1328 case OP_NOT_WORD_BOUNDARY:
1329 case OP_WORD_BOUNDARY:
1330 case OP_NOT_DIGIT:
1331 case OP_DIGIT:
1332 case OP_NOT_WHITESPACE:
1333 case OP_WHITESPACE:
1334 case OP_NOT_WORDCHAR:
1335 case OP_WORDCHAR:
1336 case OP_ANY:
1337 case OP_ALLANY:
1338 case OP_ANYBYTE:
1339 case OP_NOTPROP:
1340 case OP_PROP:
1341 case OP_ANYNL:
1342 case OP_NOT_HSPACE:
1343 case OP_HSPACE:
1344 case OP_NOT_VSPACE:
1345 case OP_VSPACE:
1346 case OP_EXTUNI:
1347 case OP_EODN:
1348 case OP_EOD:
1349 case OP_CIRC:
1350 case OP_CIRCM:
1351 case OP_DOLL:
1352 case OP_DOLLM:
1353 case OP_CHAR:
1354 case OP_CHARI:
1355 case OP_NOT:
1356 case OP_NOTI:
1357
1358 case OP_EXACT:
1359 case OP_POSSTAR:
1360 case OP_POSPLUS:
1361 case OP_POSQUERY:
1362 case OP_POSUPTO:
1363
1364 case OP_EXACTI:
1365 case OP_POSSTARI:
1366 case OP_POSPLUSI:
1367 case OP_POSQUERYI:
1368 case OP_POSUPTOI:
1369
1370 case OP_NOTEXACT:
1371 case OP_NOTPOSSTAR:
1372 case OP_NOTPOSPLUS:
1373 case OP_NOTPOSQUERY:
1374 case OP_NOTPOSUPTO:
1375
1376 case OP_NOTEXACTI:
1377 case OP_NOTPOSSTARI:
1378 case OP_NOTPOSPLUSI:
1379 case OP_NOTPOSQUERYI:
1380 case OP_NOTPOSUPTOI:
1381
1382 case OP_TYPEEXACT:
1383 case OP_TYPEPOSSTAR:
1384 case OP_TYPEPOSPLUS:
1385 case OP_TYPEPOSQUERY:
1386 case OP_TYPEPOSUPTO:
1387
1388 case OP_CLASS:
1389 case OP_NCLASS:
1390 case OP_XCLASS:
1391
1392 cc = next_opcode(common, cc);
1393 SLJIT_ASSERT(cc != NULL);
1394 break;
1395 }
1396
1397 /* Possessive quantifiers can use a special case. */
1398 if (SLJIT_UNLIKELY(possessive == length))
1399 return stack_restore ? no_frame : no_stack;
1400
1401 if (length > 0)
1402 return length + 1;
1403 return stack_restore ? no_frame : no_stack;
1404 }
1405
1406 static void init_frame(compiler_common *common, pcre_uchar *cc, pcre_uchar *ccend, int stackpos, int stacktop, BOOL recursive)
1407 {
1408 DEFINE_COMPILER;
1409 BOOL setsom_found = recursive;
1410 BOOL setmark_found = recursive;
1411 /* The last capture is a local variable even for recursions. */
1412 BOOL capture_last_found = FALSE;
1413 int offset;
1414
1415 /* >= 1 + shortest item size (2) */
1416 SLJIT_UNUSED_ARG(stacktop);
1417 SLJIT_ASSERT(stackpos >= stacktop + 2);
1418
1419 stackpos = STACK(stackpos);
1420 if (ccend == NULL)
1421 {
1422 ccend = bracketend(cc) - (1 + LINK_SIZE);
1423 if (recursive || (*cc != OP_CBRAPOS && *cc != OP_SCBRAPOS))
1424 cc = next_opcode(common, cc);
1425 }
1426
1427 SLJIT_ASSERT(cc != NULL);
1428 while (cc < ccend)
1429 switch(*cc)
1430 {
1431 case OP_SET_SOM:
1432 SLJIT_ASSERT(common->has_set_som);
1433 if (!setsom_found)
1434 {
1435 OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), OVECTOR(0));
1436 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, SLJIT_IMM, -OVECTOR(0));
1437 stackpos += (int)sizeof(sljit_sw);
1438 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, TMP1, 0);
1439 stackpos += (int)sizeof(sljit_sw);
1440 setsom_found = TRUE;
1441 }
1442 cc += 1;
1443 break;
1444
1445 case OP_MARK:
1446 case OP_PRUNE_ARG:
1447 case OP_THEN_ARG:
1448 SLJIT_ASSERT(common->mark_ptr != 0);
1449 if (!setmark_found)
1450 {
1451 OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), common->mark_ptr);
1452 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, SLJIT_IMM, -common->mark_ptr);
1453 stackpos += (int)sizeof(sljit_sw);
1454 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, TMP1, 0);
1455 stackpos += (int)sizeof(sljit_sw);
1456 setmark_found = TRUE;
1457 }
1458 cc += 1 + 2 + cc[1];
1459 break;
1460
1461 case OP_RECURSE:
1462 if (common->has_set_som && !setsom_found)
1463 {
1464 OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), OVECTOR(0));
1465 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, SLJIT_IMM, -OVECTOR(0));
1466 stackpos += (int)sizeof(sljit_sw);
1467 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, TMP1, 0);
1468 stackpos += (int)sizeof(sljit_sw);
1469 setsom_found = TRUE;
1470 }
1471 if (common->mark_ptr != 0 && !setmark_found)
1472 {
1473 OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), common->mark_ptr);
1474 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, SLJIT_IMM, -common->mark_ptr);
1475 stackpos += (int)sizeof(sljit_sw);
1476 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, TMP1, 0);
1477 stackpos += (int)sizeof(sljit_sw);
1478 setmark_found = TRUE;
1479 }
1480 if (common->capture_last_ptr != 0 && !capture_last_found)
1481 {
1482 OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), common->capture_last_ptr);
1483 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, SLJIT_IMM, -common->capture_last_ptr);
1484 stackpos += (int)sizeof(sljit_sw);
1485 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, TMP1, 0);
1486 stackpos += (int)sizeof(sljit_sw);
1487 capture_last_found = TRUE;
1488 }
1489 cc += 1 + LINK_SIZE;
1490 break;
1491
1492 case OP_CBRA:
1493 case OP_CBRAPOS:
1494 case OP_SCBRA:
1495 case OP_SCBRAPOS:
1496 if (common->capture_last_ptr != 0 && !capture_last_found)
1497 {
1498 OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), common->capture_last_ptr);
1499 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, SLJIT_IMM, -common->capture_last_ptr);
1500 stackpos += (int)sizeof(sljit_sw);
1501 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, TMP1, 0);
1502 stackpos += (int)sizeof(sljit_sw);
1503 capture_last_found = TRUE;
1504 }
1505 offset = (GET2(cc, 1 + LINK_SIZE)) << 1;
1506 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, SLJIT_IMM, OVECTOR(offset));
1507 stackpos += (int)sizeof(sljit_sw);
1508 OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), OVECTOR(offset));
1509 OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_SP), OVECTOR(offset + 1));
1510 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, TMP1, 0);
1511 stackpos += (int)sizeof(sljit_sw);
1512 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, TMP2, 0);
1513 stackpos += (int)sizeof(sljit_sw);
1514
1515 cc += 1 + LINK_SIZE + IMM2_SIZE;
1516 break;
1517
1518 default:
1519 cc = next_opcode(common, cc);
1520 SLJIT_ASSERT(cc != NULL);
1521 break;
1522 }
1523
1524 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, SLJIT_IMM, 0);
1525 SLJIT_ASSERT(stackpos == STACK(stacktop));
1526 }
1527
1528 static SLJIT_INLINE int get_private_data_copy_length(compiler_common *common, pcre_uchar *cc, pcre_uchar *ccend, BOOL needs_control_head)
1529 {
1530 int private_data_length = needs_control_head ? 3 : 2;
1531 int size;
1532 pcre_uchar *alternative;
1533 /* Calculate the sum of the private machine words. */
1534 while (cc < ccend)
1535 {
1536 size = 0;
1537 switch(*cc)
1538 {
1539 case OP_KET:
1540 if (PRIVATE_DATA(cc) != 0)
1541 {
1542 private_data_length++;
1543 SLJIT_ASSERT(PRIVATE_DATA(cc + 1) != 0);
1544 cc += PRIVATE_DATA(cc + 1);
1545 }
1546 cc += 1 + LINK_SIZE;
1547 break;
1548
1549 case OP_ASSERT:
1550 case OP_ASSERT_NOT:
1551 case OP_ASSERTBACK:
1552 case OP_ASSERTBACK_NOT:
1553 case OP_ONCE:
1554 case OP_ONCE_NC:
1555 case OP_BRAPOS:
1556 case OP_SBRA:
1557 case OP_SBRAPOS:
1558 case OP_SCOND:
1559 private_data_length++;
1560 SLJIT_ASSERT(PRIVATE_DATA(cc) != 0);
1561 cc += 1 + LINK_SIZE;
1562 break;
1563
1564 case OP_CBRA:
1565 case OP_SCBRA:
1566 if (common->optimized_cbracket[GET2(cc, 1 + LINK_SIZE)] == 0)
1567 private_data_length++;
1568 cc += 1 + LINK_SIZE + IMM2_SIZE;
1569 break;
1570
1571 case OP_CBRAPOS:
1572 case OP_SCBRAPOS:
1573 private_data_length += 2;
1574 cc += 1 + LINK_SIZE + IMM2_SIZE;
1575 break;
1576
1577 case OP_COND:
1578 /* Might be a hidden SCOND. */
1579 alternative = cc + GET(cc, 1);
1580 if (*alternative == OP_KETRMAX || *alternative == OP_KETRMIN)
1581 private_data_length++;
1582 cc += 1 + LINK_SIZE;
1583 break;
1584
1585 CASE_ITERATOR_PRIVATE_DATA_1
1586 if (PRIVATE_DATA(cc))
1587 private_data_length++;
1588 cc += 2;
1589 #ifdef SUPPORT_UTF
1590 if (common->utf && HAS_EXTRALEN(cc[-1])) cc += GET_EXTRALEN(cc[-1]);
1591 #endif
1592 break;
1593
1594 CASE_ITERATOR_PRIVATE_DATA_2A
1595 if (PRIVATE_DATA(cc))
1596 private_data_length += 2;
1597 cc += 2;
1598 #ifdef SUPPORT_UTF
1599 if (common->utf && HAS_EXTRALEN(cc[-1])) cc += GET_EXTRALEN(cc[-1]);
1600 #endif
1601 break;
1602
1603 CASE_ITERATOR_PRIVATE_DATA_2B
1604 if (PRIVATE_DATA(cc))
1605 private_data_length += 2;
1606 cc += 2 + IMM2_SIZE;
1607 #ifdef SUPPORT_UTF
1608 if (common->utf && HAS_EXTRALEN(cc[-1])) cc += GET_EXTRALEN(cc[-1]);
1609 #endif
1610 break;
1611
1612 CASE_ITERATOR_TYPE_PRIVATE_DATA_1
1613 if (PRIVATE_DATA(cc))
1614 private_data_length++;
1615 cc += 1;
1616 break;
1617
1618 CASE_ITERATOR_TYPE_PRIVATE_DATA_2A
1619 if (PRIVATE_DATA(cc))
1620 private_data_length += 2;
1621 cc += 1;
1622 break;
1623
1624 CASE_ITERATOR_TYPE_PRIVATE_DATA_2B
1625 if (PRIVATE_DATA(cc))
1626 private_data_length += 2;
1627 cc += 1 + IMM2_SIZE;
1628 break;
1629
1630 case OP_CLASS:
1631 case OP_NCLASS:
1632 #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
1633 case OP_XCLASS:
1634 size = (*cc == OP_XCLASS) ? GET(cc, 1) : 1 + 32 / (int)sizeof(pcre_uchar);
1635 #else
1636 size = 1 + 32 / (int)sizeof(pcre_uchar);
1637 #endif
1638 if (PRIVATE_DATA(cc))
1639 private_data_length += get_class_iterator_size(cc + size);
1640 cc += size;
1641 break;
1642
1643 default:
1644 cc = next_opcode(common, cc);
1645 SLJIT_ASSERT(cc != NULL);
1646 break;
1647 }
1648 }
1649 SLJIT_ASSERT(cc == ccend);
1650 return private_data_length;
1651 }
1652
1653 static void copy_private_data(compiler_common *common, pcre_uchar *cc, pcre_uchar *ccend,
1654 BOOL save, int stackptr, int stacktop, BOOL needs_control_head)
1655 {
1656 DEFINE_COMPILER;
1657 int srcw[2];
1658 int count, size;
1659 BOOL tmp1next = TRUE;
1660 BOOL tmp1empty = TRUE;
1661 BOOL tmp2empty = TRUE;
1662 pcre_uchar *alternative;
1663 enum {
1664 start,
1665 loop,
1666 end
1667 } status;
1668
1669 status = save ? start : loop;
1670 stackptr = STACK(stackptr - 2);
1671 stacktop = STACK(stacktop - 1);
1672
1673 if (!save)
1674 {
1675 stackptr += (needs_control_head ? 2 : 1) * sizeof(sljit_sw);
1676 if (stackptr < stacktop)
1677 {
1678 OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(STACK_TOP), stackptr);
1679 stackptr += sizeof(sljit_sw);
1680 tmp1empty = FALSE;
1681 }
1682 if (stackptr < stacktop)
1683 {
1684 OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(STACK_TOP), stackptr);
1685 stackptr += sizeof(sljit_sw);
1686 tmp2empty = FALSE;
1687 }
1688 /* The tmp1next must be TRUE in either way. */
1689 }
1690
1691 do
1692 {
1693 count = 0;
1694 switch(status)
1695 {
1696 case start:
1697 SLJIT_ASSERT(save && common->recursive_head_ptr != 0);
1698 count = 1;
1699 srcw[0] = common->recursive_head_ptr;
1700 if (needs_control_head)
1701 {
1702 SLJIT_ASSERT(common->control_head_ptr != 0);
1703 count = 2;
1704 srcw[1] = common->control_head_ptr;
1705 }
1706 status = loop;
1707 break;
1708
1709 case loop:
1710 if (cc >= ccend)
1711 {
1712 status = end;
1713 break;
1714 }
1715
1716 switch(*cc)
1717 {
1718 case OP_KET:
1719 if (PRIVATE_DATA(cc) != 0)
1720 {
1721 count = 1;
1722 srcw[0] = PRIVATE_DATA(cc);
1723 SLJIT_ASSERT(PRIVATE_DATA(cc + 1) != 0);
1724 cc += PRIVATE_DATA(cc + 1);
1725 }
1726 cc += 1 + LINK_SIZE;
1727 break;
1728
1729 case OP_ASSERT:
1730 case OP_ASSERT_NOT:
1731 case OP_ASSERTBACK:
1732 case OP_ASSERTBACK_NOT:
1733 case OP_ONCE:
1734 case OP_ONCE_NC:
1735 case OP_BRAPOS:
1736 case OP_SBRA:
1737 case OP_SBRAPOS:
1738 case OP_SCOND:
1739 count = 1;
1740 srcw[0] = PRIVATE_DATA(cc);
1741 SLJIT_ASSERT(srcw[0] != 0);
1742 cc += 1 + LINK_SIZE;
1743 break;
1744
1745 case OP_CBRA:
1746 case OP_SCBRA:
1747 if (common->optimized_cbracket[GET2(cc, 1 + LINK_SIZE)] == 0)
1748 {
1749 count = 1;
1750 srcw[0] = OVECTOR_PRIV(GET2(cc, 1 + LINK_SIZE));
1751 }
1752 cc += 1 + LINK_SIZE + IMM2_SIZE;
1753 break;
1754
1755 case OP_CBRAPOS:
1756 case OP_SCBRAPOS:
1757 count = 2;
1758 srcw[0] = PRIVATE_DATA(cc);
1759 srcw[1] = OVECTOR_PRIV(GET2(cc, 1 + LINK_SIZE));
1760 SLJIT_ASSERT(srcw[0] != 0 && srcw[1] != 0);
1761 cc += 1 + LINK_SIZE + IMM2_SIZE;
1762 break;
1763
1764 case OP_COND:
1765 /* Might be a hidden SCOND. */
1766 alternative = cc + GET(cc, 1);
1767 if (*alternative == OP_KETRMAX || *alternative == OP_KETRMIN)
1768 {
1769 count = 1;
1770 srcw[0] = PRIVATE_DATA(cc);
1771 SLJIT_ASSERT(srcw[0] != 0);
1772 }
1773 cc += 1 + LINK_SIZE;
1774 break;
1775
1776 CASE_ITERATOR_PRIVATE_DATA_1
1777 if (PRIVATE_DATA(cc))
1778 {
1779 count = 1;
1780 srcw[0] = PRIVATE_DATA(cc);
1781 }
1782 cc += 2;
1783 #ifdef SUPPORT_UTF
1784 if (common->utf && HAS_EXTRALEN(cc[-1])) cc += GET_EXTRALEN(cc[-1]);
1785 #endif
1786 break;
1787
1788 CASE_ITERATOR_PRIVATE_DATA_2A
1789 if (PRIVATE_DATA(cc))
1790 {
1791 count = 2;
1792 srcw[0] = PRIVATE_DATA(cc);
1793 srcw[1] = PRIVATE_DATA(cc) + sizeof(sljit_sw);
1794 }
1795 cc += 2;
1796 #ifdef SUPPORT_UTF
1797 if (common->utf && HAS_EXTRALEN(cc[-1])) cc += GET_EXTRALEN(cc[-1]);
1798 #endif
1799 break;
1800
1801 CASE_ITERATOR_PRIVATE_DATA_2B
1802 if (PRIVATE_DATA(cc))
1803 {
1804 count = 2;
1805 srcw[0] = PRIVATE_DATA(cc);
1806 srcw[1] = PRIVATE_DATA(cc) + sizeof(sljit_sw);
1807 }
1808 cc += 2 + IMM2_SIZE;
1809 #ifdef SUPPORT_UTF
1810 if (common->utf && HAS_EXTRALEN(cc[-1])) cc += GET_EXTRALEN(cc[-1]);
1811 #endif
1812 break;
1813
1814 CASE_ITERATOR_TYPE_PRIVATE_DATA_1
1815 if (PRIVATE_DATA(cc))
1816 {
1817 count = 1;
1818 srcw[0] = PRIVATE_DATA(cc);
1819 }
1820 cc += 1;
1821 break;
1822
1823 CASE_ITERATOR_TYPE_PRIVATE_DATA_2A
1824 if (PRIVATE_DATA(cc))
1825 {
1826 count = 2;
1827 srcw[0] = PRIVATE_DATA(cc);
1828 srcw[1] = srcw[0] + sizeof(sljit_sw);
1829 }
1830 cc += 1;
1831 break;
1832
1833 CASE_ITERATOR_TYPE_PRIVATE_DATA_2B
1834 if (PRIVATE_DATA(cc))
1835 {
1836 count = 2;
1837 srcw[0] = PRIVATE_DATA(cc);
1838 srcw[1] = srcw[0] + sizeof(sljit_sw);
1839 }
1840 cc += 1 + IMM2_SIZE;
1841 break;
1842
1843 case OP_CLASS:
1844 case OP_NCLASS:
1845 #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
1846 case OP_XCLASS:
1847 size = (*cc == OP_XCLASS) ? GET(cc, 1) : 1 + 32 / (int)sizeof(pcre_uchar);
1848 #else
1849 size = 1 + 32 / (int)sizeof(pcre_uchar);
1850 #endif
1851 if (PRIVATE_DATA(cc))
1852 switch(get_class_iterator_size(cc + size))
1853 {
1854 case 1:
1855 count = 1;
1856 srcw[0] = PRIVATE_DATA(cc);
1857 break;
1858
1859 case 2:
1860 count = 2;
1861 srcw[0] = PRIVATE_DATA(cc);
1862 srcw[1] = srcw[0] + sizeof(sljit_sw);
1863 break;
1864
1865 default:
1866 SLJIT_ASSERT_STOP();
1867 break;
1868 }
1869 cc += size;
1870 break;
1871
1872 default:
1873 cc = next_opcode(common, cc);
1874 SLJIT_ASSERT(cc != NULL);
1875 break;
1876 }
1877 break;
1878
1879 case end:
1880 SLJIT_ASSERT_STOP();
1881 break;
1882 }
1883
1884 while (count > 0)
1885 {
1886 count--;
1887 if (save)
1888 {
1889 if (tmp1next)
1890 {
1891 if (!tmp1empty)
1892 {
1893 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackptr, TMP1, 0);
1894 stackptr += sizeof(sljit_sw);
1895 }
1896 OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), srcw[count]);
1897 tmp1empty = FALSE;
1898 tmp1next = FALSE;
1899 }
1900 else
1901 {
1902 if (!tmp2empty)
1903 {
1904 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackptr, TMP2, 0);
1905 stackptr += sizeof(sljit_sw);
1906 }
1907 OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_SP), srcw[count]);
1908 tmp2empty = FALSE;
1909 tmp1next = TRUE;
1910 }
1911 }
1912 else
1913 {
1914 if (tmp1next)
1915 {
1916 SLJIT_ASSERT(!tmp1empty);
1917 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), srcw[count], TMP1, 0);
1918 tmp1empty = stackptr >= stacktop;
1919 if (!tmp1empty)
1920 {
1921 OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(STACK_TOP), stackptr);
1922 stackptr += sizeof(sljit_sw);
1923 }
1924 tmp1next = FALSE;
1925 }
1926 else
1927 {
1928 SLJIT_ASSERT(!tmp2empty);
1929 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), srcw[count], TMP2, 0);
1930 tmp2empty = stackptr >= stacktop;
1931 if (!tmp2empty)
1932 {
1933 OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(STACK_TOP), stackptr);
1934 stackptr += sizeof(sljit_sw);
1935 }
1936 tmp1next = TRUE;
1937 }
1938 }
1939 }
1940 }
1941 while (status != end);
1942
1943 if (save)
1944 {
1945 if (tmp1next)
1946 {
1947 if (!tmp1empty)
1948 {
1949 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackptr, TMP1, 0);
1950 stackptr += sizeof(sljit_sw);
1951 }
1952 if (!tmp2empty)
1953 {
1954 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackptr, TMP2, 0);
1955 stackptr += sizeof(sljit_sw);
1956 }
1957 }
1958 else
1959 {
1960 if (!tmp2empty)
1961 {
1962 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackptr, TMP2, 0);
1963 stackptr += sizeof(sljit_sw);
1964 }
1965 if (!tmp1empty)
1966 {
1967 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackptr, TMP1, 0);
1968 stackptr += sizeof(sljit_sw);
1969 }
1970 }
1971 }
1972 SLJIT_ASSERT(cc == ccend && stackptr == stacktop && (save || (tmp1empty && tmp2empty)));
1973 }
1974
1975 static SLJIT_INLINE pcre_uchar *set_then_offsets(compiler_common *common, pcre_uchar *cc, pcre_uint8 *current_offset)
1976 {
1977 pcre_uchar *end = bracketend(cc);
1978 BOOL has_alternatives = cc[GET(cc, 1)] == OP_ALT;
1979
1980 /* Assert captures then. */
1981 if (*cc >= OP_ASSERT && *cc <= OP_ASSERTBACK_NOT)
1982 current_offset = NULL;
1983 /* Conditional block does not. */
1984 if (*cc == OP_COND || *cc == OP_SCOND)
1985 has_alternatives = FALSE;
1986
1987 cc = next_opcode(common, cc);
1988 if (has_alternatives)
1989 current_offset = common->then_offsets + (cc - common->start);
1990
1991 while (cc < end)
1992 {
1993 if ((*cc >= OP_ASSERT && *cc <= OP_ASSERTBACK_NOT) || (*cc >= OP_ONCE && *cc <= OP_SCOND))
1994 cc = set_then_offsets(common, cc, current_offset);
1995 else
1996 {
1997 if (*cc == OP_ALT && has_alternatives)
1998 current_offset = common->then_offsets + (cc + 1 + LINK_SIZE - common->start);
1999 if (*cc >= OP_THEN && *cc <= OP_THEN_ARG && current_offset != NULL)
2000 *current_offset = 1;
2001 cc = next_opcode(common, cc);
2002 }
2003 }
2004
2005 return end;
2006 }
2007
2008 #undef CASE_ITERATOR_PRIVATE_DATA_1
2009 #undef CASE_ITERATOR_PRIVATE_DATA_2A
2010 #undef CASE_ITERATOR_PRIVATE_DATA_2B
2011 #undef CASE_ITERATOR_TYPE_PRIVATE_DATA_1
2012 #undef CASE_ITERATOR_TYPE_PRIVATE_DATA_2A
2013 #undef CASE_ITERATOR_TYPE_PRIVATE_DATA_2B
2014
2015 static SLJIT_INLINE BOOL is_powerof2(unsigned int value)
2016 {
2017 return (value & (value - 1)) == 0;
2018 }
2019
2020 static SLJIT_INLINE void set_jumps(jump_list *list, struct sljit_label *label)
2021 {
2022 while (list)
2023 {
2024 /* sljit_set_label is clever enough to do nothing
2025 if either the jump or the label is NULL. */
2026 SET_LABEL(list->jump, label);
2027 list = list->next;
2028 }
2029 }
2030
2031 static SLJIT_INLINE void add_jump(struct sljit_compiler *compiler, jump_list **list, struct sljit_jump *jump)
2032 {
2033 jump_list *list_item = sljit_alloc_memory(compiler, sizeof(jump_list));
2034 if (list_item)
2035 {
2036 list_item->next = *list;
2037 list_item->jump = jump;
2038 *list = list_item;
2039 }
2040 }
2041
2042 static void add_stub(compiler_common *common, struct sljit_jump *start)
2043 {
2044 DEFINE_COMPILER;
2045 stub_list *list_item = sljit_alloc_memory(compiler, sizeof(stub_list));
2046
2047 if (list_item)
2048 {
2049 list_item->start = start;
2050 list_item->quit = LABEL();
2051 list_item->next = common->stubs;
2052 common->stubs = list_item;
2053 }
2054 }
2055
2056 static void flush_stubs(compiler_common *common)
2057 {
2058 DEFINE_COMPILER;
2059 stub_list *list_item = common->stubs;
2060
2061 while (list_item)
2062 {
2063 JUMPHERE(list_item->start);
2064 add_jump(compiler, &common->stackalloc, JUMP(SLJIT_FAST_CALL));
2065 JUMPTO(SLJIT_JUMP, list_item->quit);
2066 list_item = list_item->next;
2067 }
2068 common->stubs = NULL;
2069 }
2070
2071 static void add_label_addr(compiler_common *common, sljit_uw *update_addr)
2072 {
2073 DEFINE_COMPILER;
2074 label_addr_list *label_addr;
2075
2076 label_addr = sljit_alloc_memory(compiler, sizeof(label_addr_list));
2077 if (label_addr == NULL)
2078 return;
2079 label_addr->label = LABEL();
2080 label_addr->update_addr = update_addr;
2081 label_addr->next = common->label_addrs;
2082 common->label_addrs = label_addr;
2083 }
2084
2085 static SLJIT_INLINE void count_match(compiler_common *common)
2086 {
2087 DEFINE_COMPILER;
2088
2089 OP2(SLJIT_SUB | SLJIT_SET_E, COUNT_MATCH, 0, COUNT_MATCH, 0, SLJIT_IMM, 1);
2090 add_jump(compiler, &common->calllimit, JUMP(SLJIT_ZERO));
2091 }
2092
2093 static SLJIT_INLINE void allocate_stack(compiler_common *common, int size)
2094 {
2095 /* May destroy all locals and registers except TMP2. */
2096 DEFINE_COMPILER;
2097
2098 OP2(SLJIT_ADD, STACK_TOP, 0, STACK_TOP, 0, SLJIT_IMM, size * sizeof(sljit_sw));
2099 #ifdef DESTROY_REGISTERS
2100 OP1(SLJIT_MOV, TMP1, 0, SLJIT_IMM, 12345);
2101 OP1(SLJIT_MOV, TMP3, 0, TMP1, 0);
2102 OP1(SLJIT_MOV, RETURN_ADDR, 0, TMP1, 0);
2103 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), LOCALS0, TMP1, 0);
2104 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), LOCALS1, TMP1, 0);
2105 #endif
2106 add_stub(common, CMP(SLJIT_GREATER, STACK_TOP, 0, STACK_LIMIT, 0));
2107 }
2108
2109 static SLJIT_INLINE void free_stack(compiler_common *common, int size)
2110 {
2111 DEFINE_COMPILER;
2112 OP2(SLJIT_SUB, STACK_TOP, 0, STACK_TOP, 0, SLJIT_IMM, size * sizeof(sljit_sw));
2113 }
2114
2115 static sljit_uw * allocate_read_only_data(compiler_common *common, sljit_uw size)
2116 {
2117 DEFINE_COMPILER;
2118 sljit_uw *result;
2119
2120 if (SLJIT_UNLIKELY(sljit_get_compiler_error(compiler)))
2121 return NULL;
2122
2123 result = (sljit_uw *)SLJIT_MALLOC(size + sizeof(sljit_uw), compiler->allocator_data);
2124 if (SLJIT_UNLIKELY(result == NULL))
2125 {
2126 sljit_set_compiler_memory_error(compiler);
2127 return NULL;
2128 }
2129
2130 *(void**)result = common->read_only_data_head;
2131 common->read_only_data_head = (void *)result;
2132 return result + 1;
2133 }
2134
2135 static void free_read_only_data(void *current, void *allocator_data)
2136 {
2137 void *next;
2138
2139 SLJIT_UNUSED_ARG(allocator_data);
2140
2141 while (current != NULL)
2142 {
2143 next = *(void**)current;
2144 SLJIT_FREE(current, allocator_data);
2145 current = next;
2146 }
2147 }
2148
2149 static SLJIT_INLINE void reset_ovector(compiler_common *common, int length)
2150 {
2151 DEFINE_COMPILER;
2152 struct sljit_label *loop;
2153 int i;
2154
2155 /* At this point we can freely use all temporary registers. */
2156 SLJIT_ASSERT(length > 1);
2157 /* TMP1 returns with begin - 1. */
2158 OP2(SLJIT_SUB, SLJIT_R0, 0, SLJIT_MEM1(SLJIT_S0), SLJIT_OFFSETOF(jit_arguments, begin), SLJIT_IMM, IN_UCHARS(1));
2159 if (length < 8)
2160 {
2161 for (i = 1; i < length; i++)
2162 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), OVECTOR(i), SLJIT_R0, 0);
2163 }
2164 else
2165 {
2166 GET_LOCAL_BASE(SLJIT_R1, 0, OVECTOR_START);
2167 OP1(SLJIT_MOV, SLJIT_R2, 0, SLJIT_IMM, length - 1);
2168 loop = LABEL();
2169 OP1(SLJIT_MOVU, SLJIT_MEM1(SLJIT_R1), sizeof(sljit_sw), SLJIT_R0, 0);
2170 OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_R2, 0, SLJIT_R2, 0, SLJIT_IMM, 1);
2171 JUMPTO(SLJIT_NOT_ZERO, loop);
2172 }
2173 }
2174
2175 static SLJIT_INLINE void do_reset_match(compiler_common *common, int length)
2176 {
2177 DEFINE_COMPILER;
2178 struct sljit_label *loop;
2179 int i;
2180
2181 SLJIT_ASSERT(length > 1);
2182 /* OVECTOR(1) contains the "string begin - 1" constant. */
2183 if (length > 2)
2184 OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), OVECTOR(1));
2185 if (length < 8)
2186 {
2187 for (i = 2; i < length; i++)
2188 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), OVECTOR(i), TMP1, 0);
2189 }
2190 else
2191 {
2192 GET_LOCAL_BASE(TMP2, 0, OVECTOR_START + sizeof(sljit_sw));
2193 OP1(SLJIT_MOV, STACK_TOP, 0, SLJIT_IMM, length - 2);
2194 loop = LABEL();
2195 OP1(SLJIT_MOVU, SLJIT_MEM1(TMP2), sizeof(sljit_sw), TMP1, 0);
2196 OP2(SLJIT_SUB | SLJIT_SET_E, STACK_TOP, 0, STACK_TOP, 0, SLJIT_IMM, 1);
2197 JUMPTO(SLJIT_NOT_ZERO, loop);
2198 }
2199
2200 OP1(SLJIT_MOV, STACK_TOP, 0, ARGUMENTS, 0);
2201 if (common->mark_ptr != 0)
2202 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->mark_ptr, SLJIT_IMM, 0);
2203 if (common->control_head_ptr != 0)
2204 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->control_head_ptr, SLJIT_IMM, 0);
2205 OP1(SLJIT_MOV, STACK_TOP, 0, SLJIT_MEM1(STACK_TOP), SLJIT_OFFSETOF(jit_arguments, stack));
2206 OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), common->start_ptr);
2207 OP1(SLJIT_MOV, STACK_TOP, 0, SLJIT_MEM1(STACK_TOP), SLJIT_OFFSETOF(struct sljit_stack, base));
2208 }
2209
2210 static sljit_sw SLJIT_CALL do_search_mark(sljit_sw *current, const pcre_uchar *skip_arg)
2211 {
2212 while (current != NULL)
2213 {
2214 switch (current[-2])
2215 {
2216 case type_then_trap:
2217 break;
2218
2219 case type_mark:
2220 if (STRCMP_UC_UC(skip_arg, (pcre_uchar *)current[-3]) == 0)
2221 return current[-4];
2222 break;
2223
2224 default:
2225 SLJIT_ASSERT_STOP();
2226 break;
2227 }
2228 current = (sljit_sw*)current[-1];
2229 }
2230 return -1;
2231 }
2232
2233 static SLJIT_INLINE void copy_ovector(compiler_common *common, int topbracket)
2234 {
2235 DEFINE_COMPILER;
2236 struct sljit_label *loop;
2237 struct sljit_jump *early_quit;
2238
2239 /* At this point we can freely use all registers. */
2240 OP1(SLJIT_MOV, SLJIT_S2, 0, SLJIT_MEM1(SLJIT_SP), OVECTOR(1));
2241 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), OVECTOR(1), STR_PTR, 0);
2242
2243 OP1(SLJIT_MOV, SLJIT_R0, 0, ARGUMENTS, 0);
2244 if (common->mark_ptr != 0)
2245 OP1(SLJIT_MOV, SLJIT_R2, 0, SLJIT_MEM1(SLJIT_SP), common->mark_ptr);
2246 OP1(SLJIT_MOV_SI, SLJIT_R1, 0, SLJIT_MEM1(SLJIT_R0), SLJIT_OFFSETOF(jit_arguments, offset_count));
2247 if (common->mark_ptr != 0)
2248 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_R0), SLJIT_OFFSETOF(jit_arguments, mark_ptr), SLJIT_R2, 0);
2249 OP2(SLJIT_SUB, SLJIT_R2, 0, SLJIT_MEM1(SLJIT_R0), SLJIT_OFFSETOF(jit_arguments, offsets), SLJIT_IMM, sizeof(int));
2250 OP1(SLJIT_MOV, SLJIT_R0, 0, SLJIT_MEM1(SLJIT_R0), SLJIT_OFFSETOF(jit_arguments, begin));
2251 GET_LOCAL_BASE(SLJIT_S0, 0, OVECTOR_START);
2252 /* Unlikely, but possible */
2253 early_quit = CMP(SLJIT_EQUAL, SLJIT_R1, 0, SLJIT_IMM, 0);
2254 loop = LABEL();
2255 OP2(SLJIT_SUB, SLJIT_S1, 0, SLJIT_MEM1(SLJIT_S0), 0, SLJIT_R0, 0);
2256 OP2(SLJIT_ADD, SLJIT_S0, 0, SLJIT_S0, 0, SLJIT_IMM, sizeof(sljit_sw));
2257 /* Copy the integer value to the output buffer */
2258 #if defined COMPILE_PCRE16 || defined COMPILE_PCRE32
2259 OP2(SLJIT_ASHR, SLJIT_S1, 0, SLJIT_S1, 0, SLJIT_IMM, UCHAR_SHIFT);
2260 #endif
2261 OP1(SLJIT_MOVU_SI, SLJIT_MEM1(SLJIT_R2), sizeof(int), SLJIT_S1, 0);
2262 OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_R1, 0, SLJIT_R1, 0, SLJIT_IMM, 1);
2263 JUMPTO(SLJIT_NOT_ZERO, loop);
2264 JUMPHERE(early_quit);
2265
2266 /* Calculate the return value, which is the maximum ovector value. */
2267 if (topbracket > 1)
2268 {
2269 GET_LOCAL_BASE(SLJIT_R0, 0, OVECTOR_START + topbracket * 2 * sizeof(sljit_sw));
2270 OP1(SLJIT_MOV, SLJIT_R1, 0, SLJIT_IMM, topbracket + 1);
2271
2272 /* OVECTOR(0) is never equal to SLJIT_S2. */
2273 loop = LABEL();
2274 OP1(SLJIT_MOVU, SLJIT_R2, 0, SLJIT_MEM1(SLJIT_R0), -(2 * (sljit_sw)sizeof(sljit_sw)));
2275 OP2(SLJIT_SUB, SLJIT_R1, 0, SLJIT_R1, 0, SLJIT_IMM, 1);
2276 CMPTO(SLJIT_EQUAL, SLJIT_R2, 0, SLJIT_S2, 0, loop);
2277 OP1(SLJIT_MOV, SLJIT_RETURN_REG, 0, SLJIT_R1, 0);
2278 }
2279 else
2280 OP1(SLJIT_MOV, SLJIT_RETURN_REG, 0, SLJIT_IMM, 1);
2281 }
2282
2283 static SLJIT_INLINE void return_with_partial_match(compiler_common *common, struct sljit_label *quit)
2284 {
2285 DEFINE_COMPILER;
2286 struct sljit_jump *jump;
2287
2288 SLJIT_COMPILE_ASSERT(STR_END == SLJIT_S1, str_end_must_be_saved_reg2);
2289 SLJIT_ASSERT(common->start_used_ptr != 0 && common->start_ptr != 0
2290 && (common->mode == JIT_PARTIAL_SOFT_COMPILE ? common->hit_start != 0 : common->hit_start == 0));
2291
2292 OP1(SLJIT_MOV, SLJIT_R1, 0, ARGUMENTS, 0);
2293 OP1(SLJIT_MOV, SLJIT_RETURN_REG, 0, SLJIT_IMM, PCRE_ERROR_PARTIAL);
2294 OP1(SLJIT_MOV_SI, SLJIT_R2, 0, SLJIT_MEM1(SLJIT_R1), SLJIT_OFFSETOF(jit_arguments, real_offset_count));
2295 CMPTO(SLJIT_SIG_LESS, SLJIT_R2, 0, SLJIT_IMM, 2, quit);
2296
2297 /* Store match begin and end. */
2298 OP1(SLJIT_MOV, SLJIT_S0, 0, SLJIT_MEM1(SLJIT_R1), SLJIT_OFFSETOF(jit_arguments, begin));
2299 OP1(SLJIT_MOV, SLJIT_R1, 0, SLJIT_MEM1(SLJIT_R1), SLJIT_OFFSETOF(jit_arguments, offsets));
2300
2301 jump = CMP(SLJIT_SIG_LESS, SLJIT_R2, 0, SLJIT_IMM, 3);
2302 OP2(SLJIT_SUB, SLJIT_R2, 0, SLJIT_MEM1(SLJIT_SP), common->mode == JIT_PARTIAL_HARD_COMPILE ? common->start_ptr : (common->hit_start + (int)sizeof(sljit_sw)), SLJIT_S0, 0);
2303 #if defined COMPILE_PCRE16 || defined COMPILE_PCRE32
2304 OP2(SLJIT_ASHR, SLJIT_R2, 0, SLJIT_R2, 0, SLJIT_IMM, UCHAR_SHIFT);
2305 #endif
2306 OP1(SLJIT_MOV_SI, SLJIT_MEM1(SLJIT_R1), 2 * sizeof(int), SLJIT_R2, 0);
2307 JUMPHERE(jump);
2308
2309 OP1(SLJIT_MOV, SLJIT_R2, 0, SLJIT_MEM1(SLJIT_SP), common->mode == JIT_PARTIAL_HARD_COMPILE ? common->start_used_ptr : common->hit_start);
2310 OP2(SLJIT_SUB, SLJIT_S1, 0, STR_END, 0, SLJIT_S0, 0);
2311 #if defined COMPILE_PCRE16 || defined COMPILE_PCRE32
2312 OP2(SLJIT_ASHR, SLJIT_S1, 0, SLJIT_S1, 0, SLJIT_IMM, UCHAR_SHIFT);
2313 #endif
2314 OP1(SLJIT_MOV_SI, SLJIT_MEM1(SLJIT_R1), sizeof(int), SLJIT_S1, 0);
2315
2316 OP2(SLJIT_SUB, SLJIT_R2, 0, SLJIT_R2, 0, SLJIT_S0, 0);
2317 #if defined COMPILE_PCRE16 || defined COMPILE_PCRE32
2318 OP2(SLJIT_ASHR, SLJIT_R2, 0, SLJIT_R2, 0, SLJIT_IMM, UCHAR_SHIFT);
2319 #endif
2320 OP1(SLJIT_MOV_SI, SLJIT_MEM1(SLJIT_R1), 0, SLJIT_R2, 0);
2321
2322 JUMPTO(SLJIT_JUMP, quit);
2323 }
2324
2325 static SLJIT_INLINE void check_start_used_ptr(compiler_common *common)
2326 {
2327 /* May destroy TMP1. */
2328 DEFINE_COMPILER;
2329 struct sljit_jump *jump;
2330
2331 if (common->mode == JIT_PARTIAL_SOFT_COMPILE)
2332 {
2333 /* The value of -1 must be kept for start_used_ptr! */
2334 OP2(SLJIT_ADD, TMP1, 0, SLJIT_MEM1(SLJIT_SP), common->start_used_ptr, SLJIT_IMM, 1);
2335 /* Jumps if start_used_ptr < STR_PTR, or start_used_ptr == -1. Although overwriting
2336 is not necessary if start_used_ptr == STR_PTR, it does not hurt as well. */
2337 jump = CMP(SLJIT_LESS_EQUAL, TMP1, 0, STR_PTR, 0);
2338 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->start_used_ptr, STR_PTR, 0);
2339 JUMPHERE(jump);
2340 }
2341 else if (common->mode == JIT_PARTIAL_HARD_COMPILE)
2342 {
2343 jump = CMP(SLJIT_LESS_EQUAL, SLJIT_MEM1(SLJIT_SP), common->start_used_ptr, STR_PTR, 0);
2344 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->start_used_ptr, STR_PTR, 0);
2345 JUMPHERE(jump);
2346 }
2347 }
2348
2349 static SLJIT_INLINE BOOL char_has_othercase(compiler_common *common, pcre_uchar *cc)
2350 {
2351 /* Detects if the character has an othercase. */
2352 unsigned int c;
2353
2354 #ifdef SUPPORT_UTF
2355 if (common->utf)
2356 {
2357 GETCHAR(c, cc);
2358 if (c > 127)
2359 {
2360 #ifdef SUPPORT_UCP
2361 return c != UCD_OTHERCASE(c);
2362 #else
2363 return FALSE;
2364 #endif
2365 }
2366 #ifndef COMPILE_PCRE8
2367 return common->fcc[c] != c;
2368 #endif
2369 }
2370 else
2371 #endif
2372 c = *cc;
2373 return MAX_255(c) ? common->fcc[c] != c : FALSE;
2374 }
2375
2376 static SLJIT_INLINE unsigned int char_othercase(compiler_common *common, unsigned int c)
2377 {
2378 /* Returns with the othercase. */
2379 #ifdef SUPPORT_UTF
2380 if (common->utf && c > 127)
2381 {
2382 #ifdef SUPPORT_UCP
2383 return UCD_OTHERCASE(c);
2384 #else
2385 return c;
2386 #endif
2387 }
2388 #endif
2389 return TABLE_GET(c, common->fcc, c);
2390 }
2391
2392 static unsigned int char_get_othercase_bit(compiler_common *common, pcre_uchar *cc)
2393 {
2394 /* Detects if the character and its othercase has only 1 bit difference. */
2395 unsigned int c, oc, bit;
2396 #if defined SUPPORT_UTF && defined COMPILE_PCRE8
2397 int n;
2398 #endif
2399
2400 #ifdef SUPPORT_UTF
2401 if (common->utf)
2402 {
2403 GETCHAR(c, cc);
2404 if (c <= 127)
2405 oc = common->fcc[c];
2406 else
2407 {
2408 #ifdef SUPPORT_UCP
2409 oc = UCD_OTHERCASE(c);
2410 #else
2411 oc = c;
2412 #endif
2413 }
2414 }
2415 else
2416 {
2417 c = *cc;
2418 oc = TABLE_GET(c, common->fcc, c);
2419 }
2420 #else
2421 c = *cc;
2422 oc = TABLE_GET(c, common->fcc, c);
2423 #endif
2424
2425 SLJIT_ASSERT(c != oc);
2426
2427 bit = c ^ oc;
2428 /* Optimized for English alphabet. */
2429 if (c <= 127 && bit == 0x20)
2430 return (0 << 8) | 0x20;
2431
2432 /* Since c != oc, they must have at least 1 bit difference. */
2433 if (!is_powerof2(bit))
2434 return 0;
2435
2436 #if defined COMPILE_PCRE8
2437
2438 #ifdef SUPPORT_UTF
2439 if (common->utf && c > 127)
2440 {
2441 n = GET_EXTRALEN(*cc);
2442 while ((bit & 0x3f) == 0)
2443 {
2444 n--;
2445 bit >>= 6;
2446 }
2447 return (n << 8) | bit;
2448 }
2449 #endif /* SUPPORT_UTF */
2450 return (0 << 8) | bit;
2451
2452 #elif defined COMPILE_PCRE16 || defined COMPILE_PCRE32
2453
2454 #ifdef SUPPORT_UTF
2455 if (common->utf && c > 65535)
2456 {
2457 if (bit >= (1 << 10))
2458 bit >>= 10;
2459 else
2460 return (bit < 256) ? ((2 << 8) | bit) : ((3 << 8) | (bit >> 8));
2461 }
2462 #endif /* SUPPORT_UTF */
2463 return (bit < 256) ? ((0 << 8) | bit) : ((1 << 8) | (bit >> 8));
2464
2465 #endif /* COMPILE_PCRE[8|16|32] */
2466 }
2467
2468 static void check_partial(compiler_common *common, BOOL force)
2469 {
2470 /* Checks whether a partial matching is occurred. Does not modify registers. */
2471 DEFINE_COMPILER;
2472 struct sljit_jump *jump = NULL;
2473
2474 SLJIT_ASSERT(!force || common->mode != JIT_COMPILE);
2475
2476 if (common->mode == JIT_COMPILE)
2477 return;
2478
2479 if (!force)
2480 jump = CMP(SLJIT_GREATER_EQUAL, SLJIT_MEM1(SLJIT_SP), common->start_used_ptr, STR_PTR, 0);
2481 else if (common->mode == JIT_PARTIAL_SOFT_COMPILE)
2482 jump = CMP(SLJIT_EQUAL, SLJIT_MEM1(SLJIT_SP), common->start_used_ptr, SLJIT_IMM, -1);
2483
2484 if (common->mode == JIT_PARTIAL_SOFT_COMPILE)
2485 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->hit_start, SLJIT_IMM, 0);
2486 else
2487 {
2488 if (common->partialmatchlabel != NULL)
2489 JUMPTO(SLJIT_JUMP, common->partialmatchlabel);
2490 else
2491 add_jump(compiler, &common->partialmatch, JUMP(SLJIT_JUMP));
2492 }
2493
2494 if (jump != NULL)
2495 JUMPHERE(jump);
2496 }
2497
2498 static void check_str_end(compiler_common *common, jump_list **end_reached)
2499 {
2500 /* Does not affect registers. Usually used in a tight spot. */
2501 DEFINE_COMPILER;
2502 struct sljit_jump *jump;
2503
2504 if (common->mode == JIT_COMPILE)
2505 {
2506 add_jump(compiler, end_reached, CMP(SLJIT_GREATER_EQUAL, STR_PTR, 0, STR_END, 0));
2507 return;
2508 }
2509
2510 jump = CMP(SLJIT_LESS, STR_PTR, 0, STR_END, 0);
2511 if (common->mode == JIT_PARTIAL_SOFT_COMPILE)
2512 {
2513 add_jump(compiler, end_reached, CMP(SLJIT_GREATER_EQUAL, SLJIT_MEM1(SLJIT_SP), common->start_used_ptr, STR_PTR, 0));
2514 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->hit_start, SLJIT_IMM, 0);
2515 add_jump(compiler, end_reached, JUMP(SLJIT_JUMP));
2516 }
2517 else
2518 {
2519 add_jump(compiler, end_reached, CMP(SLJIT_GREATER_EQUAL, SLJIT_MEM1(SLJIT_SP), common->start_used_ptr, STR_PTR, 0));
2520 if (common->partialmatchlabel != NULL)
2521 JUMPTO(SLJIT_JUMP, common->partialmatchlabel);
2522 else
2523 add_jump(compiler, &common->partialmatch, JUMP(SLJIT_JUMP));
2524 }
2525 JUMPHERE(jump);
2526 }
2527
2528 static void detect_partial_match(compiler_common *common, jump_list **backtracks)
2529 {
2530 DEFINE_COMPILER;
2531 struct sljit_jump *jump;
2532
2533 if (common->mode == JIT_COMPILE)
2534 {
2535 add_jump(compiler, backtracks, CMP(SLJIT_GREATER_EQUAL, STR_PTR, 0, STR_END, 0));
2536 return;
2537 }
2538
2539 /* Partial matching mode. */
2540 jump = CMP(SLJIT_LESS, STR_PTR, 0, STR_END, 0);
2541 add_jump(compiler, backtracks, CMP(SLJIT_GREATER_EQUAL, SLJIT_MEM1(SLJIT_SP), common->start_used_ptr, STR_PTR, 0));
2542 if (common->mode == JIT_PARTIAL_SOFT_COMPILE)
2543 {
2544 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->hit_start, SLJIT_IMM, 0);
2545 add_jump(compiler, backtracks, JUMP(SLJIT_JUMP));
2546 }
2547 else
2548 {
2549 if (common->partialmatchlabel != NULL)
2550 JUMPTO(SLJIT_JUMP, common->partialmatchlabel);
2551 else
2552 add_jump(compiler, &common->partialmatch, JUMP(SLJIT_JUMP));
2553 }
2554 JUMPHERE(jump);
2555 }
2556
2557 static void peek_char(compiler_common *common, pcre_uint32 max)
2558 {
2559 /* Reads the character into TMP1, keeps STR_PTR.
2560 Does not check STR_END. TMP2 Destroyed. */
2561 DEFINE_COMPILER;
2562 #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
2563 struct sljit_jump *jump;
2564 #endif
2565
2566 SLJIT_UNUSED_ARG(max);
2567
2568 OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), 0);
2569 #if defined SUPPORT_UTF && defined COMPILE_PCRE8
2570 if (common->utf)
2571 {
2572 if (max < 128) return;
2573
2574 jump = CMP(SLJIT_LESS, TMP1, 0, SLJIT_IMM, 0xc0);
2575 OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
2576 add_jump(compiler, &common->utfreadchar, JUMP(SLJIT_FAST_CALL));
2577 OP2(SLJIT_SUB, STR_PTR, 0, STR_PTR, 0, TMP2, 0);
2578 JUMPHERE(jump);
2579 }
2580 #endif /* SUPPORT_UTF && !COMPILE_PCRE32 */
2581
2582 #if defined SUPPORT_UTF && defined COMPILE_PCRE16
2583 if (common->utf)
2584 {
2585 if (max < 0xd800) return;
2586
2587 OP2(SLJIT_SUB, TMP2, 0, TMP1, 0, SLJIT_IMM, 0xd800);
2588 jump = CMP(SLJIT_GREATER, TMP2, 0, SLJIT_IMM, 0xdc00 - 0xd800 - 1);
2589 /* TMP2 contains the high surrogate. */
2590 OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(0));
2591 OP2(SLJIT_ADD, TMP2, 0, TMP2, 0, SLJIT_IMM, 0x40);
2592 OP2(SLJIT_SHL, TMP2, 0, TMP2, 0, SLJIT_IMM, 10);
2593 OP2(SLJIT_AND, TMP1, 0, TMP1, 0, SLJIT_IMM, 0x3ff);
2594 OP2(SLJIT_OR, TMP1, 0, TMP1, 0, TMP2, 0);
2595 JUMPHERE(jump);
2596 }
2597 #endif
2598 }
2599
2600 #if defined SUPPORT_UTF && defined COMPILE_PCRE8
2601
2602 static BOOL is_char7_bitset(const pcre_uint8 *bitset, BOOL nclass)
2603 {
2604 /* Tells whether the character codes below 128 are enough
2605 to determine a match. */
2606 const pcre_uint8 value = nclass ? 0xff : 0;
2607 const pcre_uint8 *end = bitset + 32;
2608
2609 bitset += 16;
2610 do
2611 {
2612 if (*bitset++ != value)
2613 return FALSE;
2614 }
2615 while (bitset < end);
2616 return TRUE;
2617 }
2618
2619 static void read_char7_type(compiler_common *common, BOOL full_read)
2620 {
2621 /* Reads the precise character type of a character into TMP1, if the character
2622 is less than 128. Otherwise it returns with zero. Does not check STR_END. The
2623 full_read argument tells whether characters above max are accepted or not. */
2624 DEFINE_COMPILER;
2625 struct sljit_jump *jump;
2626
2627 SLJIT_ASSERT(common->utf);
2628
2629 OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), 0);
2630 OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
2631
2632 OP1(SLJIT_MOV_UB, TMP1, 0, SLJIT_MEM1(TMP2), common->ctypes);
2633
2634 if (full_read)
2635 {
2636 jump = CMP(SLJIT_LESS, TMP2, 0, SLJIT_IMM, 0xc0);
2637 OP1(SLJIT_MOV_UB, TMP2, 0, SLJIT_MEM1(TMP2), (sljit_sw)PRIV(utf8_table4) - 0xc0);
2638 OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, TMP2, 0);
2639 JUMPHERE(jump);
2640 }
2641 }
2642
2643 #endif /* SUPPORT_UTF && COMPILE_PCRE8 */
2644
2645 static void read_char_range(compiler_common *common, pcre_uint32 min, pcre_uint32 max, BOOL update_str_ptr)
2646 {
2647 /* Reads the precise value of a character into TMP1, if the character is
2648 between min and max (c >= min && c <= max). Otherwise it returns with a value
2649 outside the range. Does not check STR_END. */
2650 DEFINE_COMPILER;
2651 #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
2652 struct sljit_jump *jump;
2653 #endif
2654 #if defined SUPPORT_UTF && defined COMPILE_PCRE8
2655 struct sljit_jump *jump2;
2656 #endif
2657
2658 SLJIT_UNUSED_ARG(update_str_ptr);
2659 SLJIT_UNUSED_ARG(min);
2660 SLJIT_UNUSED_ARG(max);
2661 SLJIT_ASSERT(min <= max);
2662
2663 OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(0));
2664 OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
2665
2666 #if defined SUPPORT_UTF && defined COMPILE_PCRE8
2667 if (common->utf)
2668 {
2669 if (max < 128 && !update_str_ptr) return;
2670
2671 jump = CMP(SLJIT_LESS, TMP1, 0, SLJIT_IMM, 0xc0);
2672 if (min >= 0x10000)
2673 {
2674 OP2(SLJIT_SUB, TMP2, 0, TMP1, 0, SLJIT_IMM, 0xf0);
2675 if (update_str_ptr)
2676 OP1(SLJIT_MOV_UB, RETURN_ADDR, 0, SLJIT_MEM1(TMP1), (sljit_sw)PRIV(utf8_table4) - 0xc0);
2677 OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(0));
2678 jump2 = CMP(SLJIT_GREATER, TMP2, 0, SLJIT_IMM, 0x7);
2679 OP2(SLJIT_SHL, TMP2, 0, TMP2, 0, SLJIT_IMM, 6);
2680 OP2(SLJIT_AND, TMP1, 0, TMP1, 0, SLJIT_IMM, 0x3f);
2681 OP2(SLJIT_OR, TMP1, 0, TMP1, 0, TMP2, 0);
2682 OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(1));
2683 OP2(SLJIT_SHL, TMP1, 0, TMP1, 0, SLJIT_IMM, 6);
2684 OP2(SLJIT_AND, TMP2, 0, TMP2, 0, SLJIT_IMM, 0x3f);
2685 OP2(SLJIT_OR, TMP1, 0, TMP1, 0, TMP2, 0);
2686 OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(2));
2687 if (!update_str_ptr)
2688 OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(3));
2689 OP2(SLJIT_SHL, TMP1, 0, TMP1, 0, SLJIT_IMM, 6);
2690 OP2(SLJIT_AND, TMP2, 0, TMP2, 0, SLJIT_IMM, 0x3f);
2691 OP2(SLJIT_OR, TMP1, 0, TMP1, 0, TMP2, 0);
2692 JUMPHERE(jump2);
2693 if (update_str_ptr)
2694 OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, RETURN_ADDR, 0);
2695 }
2696 else if (min >= 0x800 && max <= 0xffff)
2697 {
2698 OP2(SLJIT_SUB, TMP2, 0, TMP1, 0, SLJIT_IMM, 0xe0);
2699 if (update_str_ptr)
2700 OP1(SLJIT_MOV_UB, RETURN_ADDR, 0, SLJIT_MEM1(TMP1), (sljit_sw)PRIV(utf8_table4) - 0xc0);
2701 OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(0));
2702 jump2 = CMP(SLJIT_GREATER, TMP2, 0, SLJIT_IMM, 0xf);
2703 OP2(SLJIT_SHL, TMP2, 0, TMP2, 0, SLJIT_IMM, 6);
2704 OP2(SLJIT_AND, TMP1, 0, TMP1, 0, SLJIT_IMM, 0x3f);
2705 OP2(SLJIT_OR, TMP1, 0, TMP1, 0, TMP2, 0);
2706 OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(1));
2707 if (!update_str_ptr)
2708 OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(2));
2709 OP2(SLJIT_SHL, TMP1, 0, TMP1, 0, SLJIT_IMM, 6);
2710 OP2(SLJIT_AND, TMP2, 0, TMP2, 0, SLJIT_IMM, 0x3f);
2711 OP2(SLJIT_OR, TMP1, 0, TMP1, 0, TMP2, 0);
2712 JUMPHERE(jump2);
2713 if (update_str_ptr)
2714 OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, RETURN_ADDR, 0);
2715 }
2716 else if (max >= 0x800)
2717 add_jump(compiler, (max < 0x10000) ? &common->utfreadchar16 : &common->utfreadchar, JUMP(SLJIT_FAST_CALL));
2718 else if (max < 128)
2719 {
2720 OP1(SLJIT_MOV_UB, TMP2, 0, SLJIT_MEM1(TMP1), (sljit_sw)PRIV(utf8_table4) - 0xc0);
2721 OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, TMP2, 0);
2722 }
2723 else
2724 {
2725 OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(0));
2726 if (!update_str_ptr)
2727 OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
2728 else
2729 OP1(SLJIT_MOV_UB, RETURN_ADDR, 0, SLJIT_MEM1(TMP1), (sljit_sw)PRIV(utf8_table4) - 0xc0);
2730 OP2(SLJIT_AND, TMP1, 0, TMP1, 0, SLJIT_IMM, 0x3f);
2731 OP2(SLJIT_SHL, TMP1, 0, TMP1, 0, SLJIT_IMM, 6);
2732 OP2(SLJIT_AND, TMP2, 0, TMP2, 0, SLJIT_IMM, 0x3f);
2733 OP2(SLJIT_OR, TMP1, 0, TMP1, 0, TMP2, 0);
2734 if (update_str_ptr)
2735 OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, RETURN_ADDR, 0);
2736 }
2737 JUMPHERE(jump);
2738 }
2739 #endif
2740
2741 #if defined SUPPORT_UTF && defined COMPILE_PCRE16
2742 if (common->utf)
2743 {
2744 if (max >= 0x10000)
2745 {
2746 OP2(SLJIT_SUB, TMP2, 0, TMP1, 0, SLJIT_IMM, 0xd800);
2747 jump = CMP(SLJIT_GREATER, TMP2, 0, SLJIT_IMM, 0xdc00 - 0xd800 - 1);
2748 /* TMP2 contains the high surrogate. */
2749 OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(0));
2750 OP2(SLJIT_ADD, TMP2, 0, TMP2, 0, SLJIT_IMM, 0x40);
2751 OP2(SLJIT_SHL, TMP2, 0, TMP2, 0, SLJIT_IMM, 10);
2752 OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
2753 OP2(SLJIT_AND, TMP1, 0, TMP1, 0, SLJIT_IMM, 0x3ff);
2754 OP2(SLJIT_OR, TMP1, 0, TMP1, 0, TMP2, 0);
2755 JUMPHERE(jump);
2756 return;
2757 }
2758
2759 if (max < 0xd800 && !update_str_ptr) return;
2760
2761 /* Skip low surrogate if necessary. */
2762 OP2(SLJIT_SUB, TMP2, 0, TMP1, 0, SLJIT_IMM, 0xd800);
2763 jump = CMP(SLJIT_GREATER, TMP2, 0, SLJIT_IMM, 0xdc00 - 0xd800 - 1);
2764 if (update_str_ptr)
2765 OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
2766 if (max >= 0xd800)
2767 OP1(SLJIT_MOV, TMP1, 0, SLJIT_IMM, 0x10000);
2768 JUMPHERE(jump);
2769 }
2770 #endif
2771 }
2772
2773 static SLJIT_INLINE void read_char(compiler_common *common)
2774 {
2775 read_char_range(common, 0, READ_CHAR_MAX, TRUE);
2776 }
2777
2778 static void read_char8_type(compiler_common *common, BOOL update_str_ptr)
2779 {
2780 /* Reads the character type into TMP1, updates STR_PTR. Does not check STR_END. */
2781 DEFINE_COMPILER;
2782 #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
2783 struct sljit_jump *jump;
2784 #endif
2785 #if defined SUPPORT_UTF && defined COMPILE_PCRE8
2786 struct sljit_jump *jump2;
2787 #endif
2788
2789 SLJIT_UNUSED_ARG(update_str_ptr);
2790
2791 OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), 0);
2792 OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
2793
2794 #if defined SUPPORT_UTF && defined COMPILE_PCRE8
2795 if (common->utf)
2796 {
2797 /* This can be an extra read in some situations, but hopefully
2798 it is needed in most cases. */
2799 OP1(SLJIT_MOV_UB, TMP1, 0, SLJIT_MEM1(TMP2), common->ctypes);
2800 jump = CMP(SLJIT_LESS, TMP2, 0, SLJIT_IMM, 0xc0);
2801 if (!update_str_ptr)
2802 {
2803 OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(0));
2804 OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
2805 OP2(SLJIT_AND, TMP2, 0, TMP2, 0, SLJIT_IMM, 0x3f);
2806 OP2(SLJIT_SHL, TMP2, 0, TMP2, 0, SLJIT_IMM, 6);
2807 OP2(SLJIT_AND, TMP1, 0, TMP1, 0, SLJIT_IMM, 0x3f);
2808 OP2(SLJIT_OR, TMP2, 0, TMP2, 0, TMP1, 0);
2809 OP1(SLJIT_MOV, TMP1, 0, SLJIT_IMM, 0);
2810 jump2 = CMP(SLJIT_GREATER, TMP2, 0, SLJIT_IMM, 255);
2811 OP1(SLJIT_MOV_UB, TMP1, 0, SLJIT_MEM1(TMP2), common->ctypes);
2812 JUMPHERE(jump2);
2813 }
2814 else
2815 add_jump(compiler, &common->utfreadtype8, JUMP(SLJIT_FAST_CALL));
2816 JUMPHERE(jump);
2817 return;
2818 }
2819 #endif /* SUPPORT_UTF && COMPILE_PCRE8 */
2820
2821 #if !defined COMPILE_PCRE8
2822 /* The ctypes array contains only 256 values. */
2823 OP1(SLJIT_MOV, TMP1, 0, SLJIT_IMM, 0);
2824 jump = CMP(SLJIT_GREATER, TMP2, 0, SLJIT_IMM, 255);
2825 #endif
2826 OP1(SLJIT_MOV_UB, TMP1, 0, SLJIT_MEM1(TMP2), common->ctypes);
2827 #if !defined COMPILE_PCRE8
2828 JUMPHERE(jump);
2829 #endif
2830
2831 #if defined SUPPORT_UTF && defined COMPILE_PCRE16
2832 if (common->utf && update_str_ptr)
2833 {
2834 /* Skip low surrogate if necessary. */
2835 OP2(SLJIT_SUB, TMP2, 0, TMP2, 0, SLJIT_IMM, 0xd800);
2836 jump = CMP(SLJIT_GREATER, TMP2, 0, SLJIT_IMM, 0xdc00 - 0xd800 - 1);
2837 OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
2838 JUMPHERE(jump);
2839 }
2840 #endif /* SUPPORT_UTF && COMPILE_PCRE16 */
2841 }
2842
2843 static void skip_char_back(compiler_common *common)
2844 {
2845 /* Goes one character back. Affects STR_PTR and TMP1. Does not check begin. */
2846 DEFINE_COMPILER;
2847 #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
2848 #if defined COMPILE_PCRE8
2849 struct sljit_label *label;
2850
2851 if (common->utf)
2852 {
2853 label = LABEL();
2854 OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), -IN_UCHARS(1));
2855 OP2(SLJIT_SUB, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
2856 OP2(SLJIT_AND, TMP1, 0, TMP1, 0, SLJIT_IMM, 0xc0);
2857 CMPTO(SLJIT_EQUAL, TMP1, 0, SLJIT_IMM, 0x80, label);
2858 return;
2859 }
2860 #elif defined COMPILE_PCRE16
2861 if (common->utf)
2862 {
2863 OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), -IN_UCHARS(1));
2864 OP2(SLJIT_SUB, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
2865 /* Skip low surrogate if necessary. */
2866 OP2(SLJIT_AND, TMP1, 0, TMP1, 0, SLJIT_IMM, 0xfc00);
2867 OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0xdc00);
2868 OP_FLAGS(SLJIT_MOV, TMP1, 0, SLJIT_UNUSED, 0, SLJIT_EQUAL);
2869 OP2(SLJIT_SHL, TMP1, 0, TMP1, 0, SLJIT_IMM, 1);
2870 OP2(SLJIT_SUB, STR_PTR, 0, STR_PTR, 0, TMP1, 0);
2871 return;
2872 }
2873 #endif /* COMPILE_PCRE[8|16] */
2874 #endif /* SUPPORT_UTF && !COMPILE_PCRE32 */
2875 OP2(SLJIT_SUB, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
2876 }
2877
2878 static void check_newlinechar(compiler_common *common, int nltype, jump_list **backtracks, BOOL jumpifmatch)
2879 {
2880 /* Character comes in TMP1. Checks if it is a newline. TMP2 may be destroyed. */
2881 DEFINE_COMPILER;
2882 struct sljit_jump *jump;
2883
2884 if (nltype == NLTYPE_ANY)
2885 {
2886 add_jump(compiler, &common->anynewline, JUMP(SLJIT_FAST_CALL));
2887 add_jump(compiler, backtracks, JUMP(jumpifmatch ? SLJIT_NOT_ZERO : SLJIT_ZERO));
2888 }
2889 else if (nltype == NLTYPE_ANYCRLF)
2890 {
2891 if (jumpifmatch)
2892 {
2893 add_jump(compiler, backtracks, CMP(SLJIT_EQUAL, TMP1, 0, SLJIT_IMM, CHAR_CR));
2894 add_jump(compiler, backtracks, CMP(SLJIT_EQUAL, TMP1, 0, SLJIT_IMM, CHAR_NL));
2895 }
2896 else
2897 {
2898 jump = CMP(SLJIT_EQUAL, TMP1, 0, SLJIT_IMM, CHAR_CR);
2899 add_jump(compiler, backtracks, CMP(SLJIT_NOT_EQUAL, TMP1, 0, SLJIT_IMM, CHAR_NL));
2900 JUMPHERE(jump);
2901 }
2902 }
2903 else
2904 {
2905 SLJIT_ASSERT(nltype == NLTYPE_FIXED && common->newline < 256);
2906 add_jump(compiler, backtracks, CMP(jumpifmatch ? SLJIT_EQUAL : SLJIT_NOT_EQUAL, TMP1, 0, SLJIT_IMM, common->newline));
2907 }
2908 }
2909
2910 #ifdef SUPPORT_UTF
2911
2912 #if defined COMPILE_PCRE8
2913 static void do_utfreadchar(compiler_common *common)
2914 {
2915 /* Fast decoding a UTF-8 character. TMP1 contains the first byte
2916 of the character (>= 0xc0). Return char value in TMP1, length in TMP2. */
2917 DEFINE_COMPILER;
2918 struct sljit_jump *jump;
2919
2920 sljit_emit_fast_enter(compiler, RETURN_ADDR, 0);
2921 OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(0));
2922 OP2(SLJIT_AND, TMP1, 0, TMP1, 0, SLJIT_IMM, 0x3f);
2923 OP2(SLJIT_SHL, TMP1, 0, TMP1, 0, SLJIT_IMM, 6);
2924 OP2(SLJIT_AND, TMP2, 0, TMP2, 0, SLJIT_IMM, 0x3f);
2925 OP2(SLJIT_OR, TMP1, 0, TMP1, 0, TMP2, 0);
2926
2927 /* Searching for the first zero. */
2928 OP2(SLJIT_AND | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x800);
2929 jump = JUMP(SLJIT_NOT_ZERO);
2930 /* Two byte sequence. */
2931 OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
2932 OP1(SLJIT_MOV, TMP2, 0, SLJIT_IMM, IN_UCHARS(2));
2933 sljit_emit_fast_return(compiler, RETURN_ADDR, 0);
2934
2935 JUMPHERE(jump);
2936 OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(1));
2937 OP2(SLJIT_XOR, TMP1, 0, TMP1, 0, SLJIT_IMM, 0x800);
2938 OP2(SLJIT_SHL, TMP1, 0, TMP1, 0, SLJIT_IMM, 6);
2939 OP2(SLJIT_AND, TMP2, 0, TMP2, 0, SLJIT_IMM, 0x3f);
2940 OP2(SLJIT_OR, TMP1, 0, TMP1, 0, TMP2, 0);
2941
2942 OP2(SLJIT_AND | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x10000);
2943 jump = JUMP(SLJIT_NOT_ZERO);
2944 /* Three byte sequence. */
2945 OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(2));
2946 OP1(SLJIT_MOV, TMP2, 0, SLJIT_IMM, IN_UCHARS(3));
2947 sljit_emit_fast_return(compiler, RETURN_ADDR, 0);
2948
2949 /* Four byte sequence. */
2950 JUMPHERE(jump);
2951 OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(2));
2952 OP2(SLJIT_XOR, TMP1, 0, TMP1, 0, SLJIT_IMM, 0x10000);
2953 OP2(SLJIT_SHL, TMP1, 0, TMP1, 0, SLJIT_IMM, 6);
2954 OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(3));
2955 OP2(SLJIT_AND, TMP2, 0, TMP2, 0, SLJIT_IMM, 0x3f);
2956 OP2(SLJIT_OR, TMP1, 0, TMP1, 0, TMP2, 0);
2957 OP1(SLJIT_MOV, TMP2, 0, SLJIT_IMM, IN_UCHARS(4));
2958 sljit_emit_fast_return(compiler, RETURN_ADDR, 0);
2959 }
2960
2961 static void do_utfreadchar16(compiler_common *common)
2962 {
2963 /* Fast decoding a UTF-8 character. TMP1 contains the first byte
2964 of the character (>= 0xc0). Return value in TMP1. */
2965 DEFINE_COMPILER;
2966 struct sljit_jump *jump;
2967
2968 sljit_emit_fast_enter(compiler, RETURN_ADDR, 0);
2969 OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(0));
2970 OP2(SLJIT_AND, TMP1, 0, TMP1, 0, SLJIT_IMM, 0x3f);
2971 OP2(SLJIT_SHL, TMP1, 0, TMP1, 0, SLJIT_IMM, 6);
2972 OP2(SLJIT_AND, TMP2, 0, TMP2, 0, SLJIT_IMM, 0x3f);
2973 OP2(SLJIT_OR, TMP1, 0, TMP1, 0, TMP2, 0);
2974
2975 /* Searching for the first zero. */
2976 OP2(SLJIT_AND | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x800);
2977 jump = JUMP(SLJIT_NOT_ZERO);
2978 /* Two byte sequence. */
2979 OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
2980 sljit_emit_fast_return(compiler, RETURN_ADDR, 0);
2981
2982 JUMPHERE(jump);
2983 OP2(SLJIT_AND | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x400);
2984 OP_FLAGS(SLJIT_MOV, TMP2, 0, SLJIT_UNUSED, 0, SLJIT_NOT_ZERO);
2985 /* This code runs only in 8 bit mode. No need to shift the value. */
2986 OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, TMP2, 0);
2987 OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(1));
2988 OP2(SLJIT_XOR, TMP1, 0, TMP1, 0, SLJIT_IMM, 0x800);
2989 OP2(SLJIT_SHL, TMP1, 0, TMP1, 0, SLJIT_IMM, 6);
2990 OP2(SLJIT_AND, TMP2, 0, TMP2, 0, SLJIT_IMM, 0x3f);
2991 OP2(SLJIT_OR, TMP1, 0, TMP1, 0, TMP2, 0);
2992 /* Three byte sequence. */
2993 OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(2));
2994 sljit_emit_fast_return(compiler, RETURN_ADDR, 0);
2995 }
2996
2997 static void do_utfreadtype8(compiler_common *common)
2998 {
2999 /* Fast decoding a UTF-8 character type. TMP2 contains the first byte
3000 of the character (>= 0xc0). Return value in TMP1. */
3001 DEFINE_COMPILER;
3002 struct sljit_jump *jump;
3003 struct sljit_jump *compare;
3004
3005 sljit_emit_fast_enter(compiler, RETURN_ADDR, 0);
3006
3007 OP2(SLJIT_AND | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP2, 0, SLJIT_IMM, 0x20);
3008 jump = JUMP(SLJIT_NOT_ZERO);
3009 /* Two byte sequence. */
3010 OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(0));
3011 OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
3012 OP2(SLJIT_AND, TMP2, 0, TMP2, 0, SLJIT_IMM, 0x1f);
3013 /* The upper 5 bits are known at this point. */
3014 compare = CMP(SLJIT_GREATER, TMP2, 0, SLJIT_IMM, 0x3);
3015 OP2(SLJIT_SHL, TMP2, 0, TMP2, 0, SLJIT_IMM, 6);
3016 OP2(SLJIT_AND, TMP1, 0, TMP1, 0, SLJIT_IMM, 0x3f);
3017 OP2(SLJIT_OR, TMP2, 0, TMP2, 0, TMP1, 0);
3018 OP1(SLJIT_MOV_UB, TMP1, 0, SLJIT_MEM1(TMP2), common->ctypes);
3019 sljit_emit_fast_return(compiler, RETURN_ADDR, 0);
3020
3021 JUMPHERE(compare);
3022 OP1(SLJIT_MOV, TMP1, 0, SLJIT_IMM, 0);
3023 sljit_emit_fast_return(compiler, RETURN_ADDR, 0);
3024
3025 /* We only have types for characters less than 256. */
3026 JUMPHERE(jump);
3027 OP1(SLJIT_MOV_UB, TMP2, 0, SLJIT_MEM1(TMP2), (sljit_sw)PRIV(utf8_table4) - 0xc0);
3028 OP1(SLJIT_MOV, TMP1, 0, SLJIT_IMM, 0);
3029 OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, TMP2, 0);
3030 sljit_emit_fast_return(compiler, RETURN_ADDR, 0);
3031 }
3032
3033 #endif /* COMPILE_PCRE8 */
3034
3035 #endif /* SUPPORT_UTF */
3036
3037 #ifdef SUPPORT_UCP
3038
3039 /* UCD_BLOCK_SIZE must be 128 (see the assert below). */
3040 #define UCD_BLOCK_MASK 127
3041 #define UCD_BLOCK_SHIFT 7
3042
3043 static void do_getucd(compiler_common *common)
3044 {
3045 /* Search the UCD record for the character comes in TMP1.
3046 Returns chartype in TMP1 and UCD offset in TMP2. */
3047 DEFINE_COMPILER;
3048
3049 SLJIT_ASSERT(UCD_BLOCK_SIZE == 128 && sizeof(ucd_record) == 8);
3050
3051 sljit_emit_fast_enter(compiler, RETURN_ADDR, 0);
3052 OP2(SLJIT_LSHR, TMP2, 0, TMP1, 0, SLJIT_IMM, UCD_BLOCK_SHIFT);
3053 OP1(SLJIT_MOV_UB, TMP2, 0, SLJIT_MEM1(TMP2), (sljit_sw)PRIV(ucd_stage1));
3054 OP2(SLJIT_AND, TMP1, 0, TMP1, 0, SLJIT_IMM, UCD_BLOCK_MASK);
3055 OP2(SLJIT_SHL, TMP2, 0, TMP2, 0, SLJIT_IMM, UCD_BLOCK_SHIFT);
3056 OP2(SLJIT_ADD, TMP1, 0, TMP1, 0, TMP2, 0);
3057 OP1(SLJIT_MOV, TMP2, 0, SLJIT_IMM, (sljit_sw)PRIV(ucd_stage2));
3058 OP1(SLJIT_MOV_UH, TMP2, 0, SLJIT_MEM2(TMP2, TMP1), 1);
3059 OP1(SLJIT_MOV, TMP1, 0, SLJIT_IMM, (sljit_sw)PRIV(ucd_records) + SLJIT_OFFSETOF(ucd_record, chartype));
3060 OP1(SLJIT_MOV_UB, TMP1, 0, SLJIT_MEM2(TMP1, TMP2), 3);
3061 sljit_emit_fast_return(compiler, RETURN_ADDR, 0);
3062 }
3063 #endif
3064
3065 static SLJIT_INLINE struct sljit_label *mainloop_entry(compiler_common *common, BOOL hascrorlf, BOOL firstline)
3066 {
3067 DEFINE_COMPILER;
3068 struct sljit_label *mainloop;
3069 struct sljit_label *newlinelabel = NULL;
3070 struct sljit_jump *start;
3071 struct sljit_jump *end = NULL;
3072 struct sljit_jump *nl = NULL;
3073 #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
3074 struct sljit_jump *singlechar;
3075 #endif
3076 jump_list *newline = NULL;
3077 BOOL newlinecheck = FALSE;
3078 BOOL readuchar = FALSE;
3079
3080 if (!(hascrorlf || firstline) && (common->nltype == NLTYPE_ANY ||
3081 common->nltype == NLTYPE_ANYCRLF || common->newline > 255))
3082 newlinecheck = TRUE;
3083
3084 if (firstline)
3085 {
3086 /* Search for the end of the first line. */
3087 SLJIT_ASSERT(common->first_line_end != 0);
3088 OP1(SLJIT_MOV, TMP3, 0, STR_PTR, 0);
3089
3090 if (common->nltype == NLTYPE_FIXED && common->newline > 255)
3091 {
3092 mainloop = LABEL();
3093 OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
3094 end = CMP(SLJIT_GREATER_EQUAL, STR_PTR, 0, STR_END, 0);
3095 OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(-1));
3096 OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(0));
3097 CMPTO(SLJIT_NOT_EQUAL, TMP1, 0, SLJIT_IMM, (common->newline >> 8) & 0xff, mainloop);
3098 CMPTO(SLJIT_NOT_EQUAL, TMP2, 0, SLJIT_IMM, common->newline & 0xff, mainloop);
3099 JUMPHERE(end);
3100 OP2(SLJIT_SUB, SLJIT_MEM1(SLJIT_SP), common->first_line_end, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
3101 }
3102 else
3103 {
3104 end = CMP(SLJIT_GREATER_EQUAL, STR_PTR, 0, STR_END, 0);
3105 mainloop = LABEL();
3106 /* Continual stores does not cause data dependency. */
3107 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->first_line_end, STR_PTR, 0);
3108 read_char_range(common, common->nlmin, common->nlmax, TRUE);
3109 check_newlinechar(common, common->nltype, &newline, TRUE);
3110 CMPTO(SLJIT_LESS, STR_PTR, 0, STR_END, 0, mainloop);
3111 JUMPHERE(end);
3112 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->first_line_end, STR_PTR, 0);
3113 set_jumps(newline, LABEL());
3114 }
3115
3116 OP1(SLJIT_MOV, STR_PTR, 0, TMP3, 0);
3117 }
3118
3119 start = JUMP(SLJIT_JUMP);
3120
3121 if (newlinecheck)
3122 {
3123 newlinelabel = LABEL();
3124 OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
3125 end = CMP(SLJIT_GREATER_EQUAL, STR_PTR, 0, STR_END, 0);
3126 OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), 0);
3127 OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, common->newline & 0xff);
3128 OP_FLAGS(SLJIT_MOV, TMP1, 0, SLJIT_UNUSED, 0, SLJIT_EQUAL);
3129 #if defined COMPILE_PCRE16 || defined COMPILE_PCRE32
3130 OP2(SLJIT_SHL, TMP1, 0, TMP1, 0, SLJIT_IMM, UCHAR_SHIFT);
3131 #endif
3132 OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, TMP1, 0);
3133 nl = JUMP(SLJIT_JUMP);
3134 }
3135
3136 mainloop = LABEL();
3137
3138 /* Increasing the STR_PTR here requires one less jump in the most common case. */
3139 #ifdef SUPPORT_UTF
3140 if (common->utf) readuchar = TRUE;
3141 #endif
3142 if (newlinecheck) readuchar = TRUE;
3143
3144 if (readuchar)
3145 OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), 0);
3146
3147 if (newlinecheck)
3148 CMPTO(SLJIT_EQUAL, TMP1, 0, SLJIT_IMM, (common->newline >> 8) & 0xff, newlinelabel);
3149
3150 OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
3151 #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
3152 #if defined COMPILE_PCRE8
3153 if (common->utf)
3154 {
3155 singlechar = CMP(SLJIT_LESS, TMP1, 0, SLJIT_IMM, 0xc0);
3156 OP1(SLJIT_MOV_UB, TMP1, 0, SLJIT_MEM1(TMP1), (sljit_sw)PRIV(utf8_table4) - 0xc0);
3157 OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, TMP1, 0);
3158 JUMPHERE(singlechar);
3159 }
3160 #elif defined COMPILE_PCRE16
3161 if (common->utf)
3162 {
3163 singlechar = CMP(SLJIT_LESS, TMP1, 0, SLJIT_IMM, 0xd800);
3164 OP2(SLJIT_AND, TMP1, 0, TMP1, 0, SLJIT_IMM, 0xfc00);
3165 OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0xd800);
3166 OP_FLAGS(SLJIT_MOV, TMP1, 0, SLJIT_UNUSED, 0, SLJIT_EQUAL);
3167 OP2(SLJIT_SHL, TMP1, 0, TMP1, 0, SLJIT_IMM, 1);
3168 OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, TMP1, 0);
3169 JUMPHERE(singlechar);
3170 }
3171 #endif /* COMPILE_PCRE[8|16] */
3172 #endif /* SUPPORT_UTF && !COMPILE_PCRE32 */
3173 JUMPHERE(start);
3174
3175 if (newlinecheck)
3176 {
3177 JUMPHERE(end);
3178 JUMPHERE(nl);
3179 }
3180
3181 return mainloop;
3182 }
3183
3184 #define MAX_N_CHARS 16
3185 #define MAX_N_BYTES 8
3186
3187 static SLJIT_INLINE void add_prefix_byte(pcre_uint8 byte, pcre_uint8 *bytes)
3188 {
3189 pcre_uint8 len = bytes[0];
3190 int i;
3191
3192 if (len == 255)
3193 return;
3194
3195 if (len == 0)
3196 {
3197 bytes[0] = 1;
3198 bytes[1] = byte;
3199 return;
3200 }
3201
3202 for (i = len; i > 0; i--)
3203 if (bytes[i] == byte)
3204 return;
3205
3206 if (len >= MAX_N_BYTES - 1)
3207 {
3208 bytes[0] = 255;
3209 return;
3210 }
3211
3212 len++;
3213 bytes[len] = byte;
3214 bytes[0] = len;
3215 }
3216
3217 static int scan_prefix(compiler_common *common, pcre_uchar *cc, pcre_uint32 *chars, pcre_uint8 *bytes, int max_chars, pcre_uint32 *rec_count)
3218 {
3219 /* Recursive function, which scans prefix literals. */
3220 BOOL last, any, caseless;
3221 int len, repeat, len_save, consumed = 0;
3222 pcre_uint32 chr, mask;
3223 pcre_uchar *alternative, *cc_save, *oc;
3224 #if defined SUPPORT_UTF && defined COMPILE_PCRE8
3225 pcre_uchar othercase[8];
3226 #elif defined SUPPORT_UTF && defined COMPILE_PCRE16
3227 pcre_uchar othercase[2];
3228 #else
3229 pcre_uchar othercase[1];
3230 #endif
3231
3232 repeat = 1;
3233 while (TRUE)
3234 {
3235 if (*rec_count == 0)
3236 return 0;
3237 rec_count--;
3238
3239 last = TRUE;
3240 any = FALSE;
3241 caseless = FALSE;
3242
3243 switch (*cc)
3244 {
3245 case OP_CHARI:
3246 caseless = TRUE;
3247 case OP_CHAR:
3248 last = FALSE;
3249 cc++;
3250 break;
3251
3252 case OP_SOD:
3253 case OP_SOM:
3254 case OP_SET_SOM:
3255 case OP_NOT_WORD_BOUNDARY:
3256 case OP_WORD_BOUNDARY:
3257 case OP_EODN:
3258 case OP_EOD:
3259 case OP_CIRC:
3260 case OP_CIRCM:
3261 case OP_DOLL:
3262 case OP_DOLLM:
3263 /* Zero width assertions. */
3264 cc++;
3265 continue;
3266
3267 case OP_ASSERT:
3268 case OP_ASSERT_NOT:
3269 case OP_ASSERTBACK:
3270 case OP_ASSERTBACK_NOT:
3271 cc = bracketend(cc);
3272 continue;
3273
3274 case OP_PLUSI:
3275 case OP_MINPLUSI:
3276 case OP_POSPLUSI:
3277 caseless = TRUE;
3278 case OP_PLUS:
3279 case OP_MINPLUS:
3280 case OP_POSPLUS:
3281 cc++;
3282 break;
3283
3284 case OP_EXACTI:
3285 caseless = TRUE;
3286 case OP_EXACT:
3287 repeat = GET2(cc, 1);
3288 last = FALSE;
3289 cc += 1 + IMM2_SIZE;
3290 break;
3291
3292 case OP_QUERYI:
3293 case OP_MINQUERYI:
3294 case OP_POSQUERYI:
3295 caseless = TRUE;
3296 case OP_QUERY:
3297 case OP_MINQUERY:
3298 case OP_POSQUERY:
3299 len = 1;
3300 cc++;
3301 #ifdef SUPPORT_UTF
3302 if (common->utf && HAS_EXTRALEN(*cc)) len += GET_EXTRALEN(*cc);
3303 #endif
3304 max_chars = scan_prefix(common, cc + len, chars, bytes, max_chars, rec_count);
3305 if (max_chars == 0)
3306 return consumed;
3307 last = FALSE;
3308 break;
3309
3310 case OP_KET:
3311 cc += 1 + LINK_SIZE;
3312 continue;
3313
3314 case OP_ALT:
3315 cc += GET(cc, 1);
3316 continue;
3317
3318 case OP_ONCE:
3319 case OP_ONCE_NC:
3320 case OP_BRA:
3321 case OP_BRAPOS:
3322 case OP_CBRA:
3323 case OP_CBRAPOS:
3324 alternative = cc + GET(cc, 1);
3325 while (*alternative == OP_ALT)
3326 {
3327 max_chars = scan_prefix(common, alternative + 1 + LINK_SIZE, chars, bytes, max_chars, rec_count);
3328 if (max_chars == 0)
3329 return consumed;
3330 alternative += GET(alternative, 1);
3331 }
3332
3333 if (*cc == OP_CBRA || *cc == OP_CBRAPOS)
3334 cc += IMM2_SIZE;
3335 cc += 1 + LINK_SIZE;
3336 continue;
3337
3338 case OP_CLASS:
3339 #if defined SUPPORT_UTF && defined COMPILE_PCRE8
3340 if (common->utf && !is_char7_bitset((const pcre_uint8 *)(cc + 1), FALSE)) return consumed;
3341 #endif
3342 any = TRUE;
3343 cc += 1 + 32 / sizeof(pcre_uchar);
3344 break;
3345
3346 case OP_NCLASS:
3347 #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
3348 if (common->utf) return consumed;
3349 #endif
3350 any = TRUE;
3351 cc += 1 + 32 / sizeof(pcre_uchar);
3352 break;
3353
3354 #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
3355 case OP_XCLASS:
3356 #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
3357 if (common->utf) return consumed;
3358 #endif
3359 any = TRUE;
3360 cc += GET(cc, 1);
3361 break;
3362 #endif
3363
3364 case OP_DIGIT:
3365 #if defined SUPPORT_UTF && defined COMPILE_PCRE8
3366 if (common->utf && !is_char7_bitset((const pcre_uint8 *)common->ctypes - cbit_length + cbit_digit, FALSE))
3367 return consumed;
3368 #endif
3369 any = TRUE;
3370 cc++;
3371 break;
3372
3373 case OP_WHITESPACE:
3374 #if defined SUPPORT_UTF && defined COMPILE_PCRE8
3375 if (common->utf && !is_char7_bitset((const pcre_uint8 *)common->ctypes - cbit_length + cbit_space, FALSE))
3376 return consumed;
3377 #endif
3378 any = TRUE;
3379 cc++;
3380 break;
3381
3382 case OP_WORDCHAR:
3383 #if defined SUPPORT_UTF && defined COMPILE_PCRE8
3384 if (common->utf && !is_char7_bitset((const pcre_uint8 *)common->ctypes - cbit_length + cbit_word, FALSE))
3385 return consumed;
3386 #endif
3387 any = TRUE;
3388 cc++;
3389 break;
3390
3391 case OP_NOT:
3392 case OP_NOTI:
3393 cc++;
3394 /* Fall through. */
3395 case OP_NOT_DIGIT:
3396 case OP_NOT_WHITESPACE:
3397 case OP_NOT_WORDCHAR:
3398 case OP_ANY:
3399 case OP_ALLANY:
3400 #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
3401 if (common->utf) return consumed;
3402 #endif
3403 any = TRUE;
3404 cc++;
3405 break;
3406
3407 #ifdef SUPPORT_UCP
3408 case OP_NOTPROP:
3409 case OP_PROP:
3410 #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
3411 if (common->utf) return consumed;
3412 #endif
3413 any = TRUE;
3414 cc += 1 + 2;
3415 break;
3416 #endif
3417
3418 case OP_TYPEEXACT:
3419 repeat = GET2(cc, 1);
3420 cc += 1 + IMM2_SIZE;
3421 continue;
3422
3423 case OP_NOTEXACT:
3424 case OP_NOTEXACTI:
3425 #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
3426 if (common->utf) return consumed;
3427 #endif
3428 any = TRUE;
3429 repeat = GET2(cc, 1);
3430 cc += 1 + IMM2_SIZE + 1;
3431 break;
3432
3433 default:
3434 return consumed;
3435 }
3436
3437 if (any)
3438 {
3439 #if defined COMPILE_PCRE8
3440 mask = 0xff;
3441 #elif defined COMPILE_PCRE16
3442 mask = 0xffff;
3443 #elif defined COMPILE_PCRE32
3444 mask = 0xffffffff;
3445 #else
3446 SLJIT_ASSERT_STOP();
3447 #endif
3448
3449 do
3450 {
3451 chars[0] = mask;
3452 chars[1] = mask;
3453 bytes[0] = 255;
3454
3455 consumed++;
3456 if (--max_chars == 0)
3457 return consumed;
3458 chars += 2;
3459 bytes += MAX_N_BYTES;
3460 }
3461 while (--repeat > 0);
3462
3463 repeat = 1;
3464 continue;
3465 }
3466
3467 len = 1;
3468 #ifdef SUPPORT_UTF
3469 if (common->utf && HAS_EXTRALEN(*cc)) len += GET_EXTRALEN(*cc);
3470 #endif
3471
3472 if (caseless && char_has_othercase(common, cc))
3473 {
3474 #ifdef SUPPORT_UTF
3475 if (common->utf)
3476 {
3477 GETCHAR(chr, cc);
3478 if ((int)PRIV(ord2utf)(char_othercase(common, chr), othercase) != len)
3479 return consumed;
3480 }
3481 else
3482 #endif
3483 {
3484 chr = *cc;
3485 othercase[0] = TABLE_GET(chr, common->fcc, chr);
3486 }
3487 }
3488 else
3489 caseless = FALSE;
3490
3491 len_save = len;
3492 cc_save = cc;
3493 while (TRUE)
3494 {
3495 oc = othercase;
3496 do
3497 {
3498 chr = *cc;
3499 #ifdef COMPILE_PCRE32
3500 if (SLJIT_UNLIKELY(chr == NOTACHAR))
3501 return consumed;
3502 #endif
3503 add_prefix_byte((pcre_uint8)chr, bytes);
3504
3505 mask = 0;
3506 if (caseless)
3507 {
3508 add_prefix_byte((pcre_uint8)*oc, bytes);
3509 mask = *cc ^ *oc;
3510 chr |= mask;
3511 }
3512
3513 #ifdef COMPILE_PCRE32
3514 if (chars[0] == NOTACHAR && chars[1] == 0)
3515 #else
3516 if (chars[0] == NOTACHAR)
3517 #endif
3518 {
3519 chars[0] = chr;
3520 chars[1] = mask;
3521 }
3522 else
3523 {
3524 mask |= chars[0] ^ chr;
3525 chr |= mask;
3526 chars[0] = chr;
3527 chars[1] |= mask;
3528 }
3529
3530 len--;
3531 consumed++;
3532 if (--max_chars == 0)
3533 return consumed;
3534 chars += 2;
3535 bytes += MAX_N_BYTES;
3536 cc++;
3537 oc++;
3538 }
3539 while (len > 0);
3540
3541 if (--repeat == 0)
3542 break;
3543
3544 len = len_save;
3545 cc = cc_save;
3546 }
3547
3548 repeat = 1;
3549 if (last)
3550 return consumed;
3551 }
3552 }
3553
3554 static SLJIT_INLINE BOOL fast_forward_first_n_chars(compiler_common *common, BOOL firstline)
3555 {
3556 DEFINE_COMPILER;
3557 struct sljit_label *start;
3558 struct sljit_jump *quit;
3559 pcre_uint32 chars[MAX_N_CHARS * 2];
3560 pcre_uint8 bytes[MAX_N_CHARS * MAX_N_BYTES];
3561 pcre_uint8 ones[MAX_N_CHARS];
3562 int offsets[3];
3563 pcre_uint32 mask;
3564 pcre_uint8 *byte_set, *byte_set_end;
3565 int i, max, from;
3566 int range_right = -1, range_len = 3 - 1;
3567 sljit_ub *update_table = NULL;
3568 BOOL in_range;
3569 pcre_uint32 rec_count;
3570
3571 for (i = 0; i < MAX_N_CHARS; i++)
3572 {
3573 chars[i << 1] = NOTACHAR;
3574 chars[(i << 1) + 1] = 0;
3575 bytes[i * MAX_N_BYTES] = 0;
3576 }
3577
3578 rec_count = 10000;
3579 max = scan_prefix(common, common->start, chars, bytes, MAX_N_CHARS, &rec_count);
3580
3581 if (max <= 1)
3582 return FALSE;
3583
3584 for (i = 0; i < max; i++)
3585 {
3586 mask = chars[(i << 1) + 1];
3587 ones[i] = ones_in_half_byte[mask & 0xf];
3588 mask >>= 4;
3589 while (mask != 0)
3590 {
3591 ones[i] += ones_in_half_byte[mask & 0xf];
3592 mask >>= 4;
3593 }
3594 }
3595
3596 in_range = FALSE;
3597 from = 0; /* Prevent compiler "uninitialized" warning */
3598 for (i = 0; i <= max; i++)
3599 {
3600 if (in_range && (i - from) > range_len && (bytes[(i - 1) * MAX_N_BYTES] <= 4))
3601 {
3602 range_len = i - from;
3603 range_right = i - 1;
3604 }
3605
3606 if (i < max && bytes[i * MAX_N_BYTES] < 255)
3607 {
3608 if (!in_range)
3609 {
3610 in_range = TRUE;
3611 from = i;
3612 }
3613 }
3614 else if (in_range)
3615 in_range = FALSE;
3616 }
3617
3618 if (range_right >= 0)
3619 {
3620 update_table = (sljit_ub *)allocate_read_only_data(common, 256);
3621 if (update_table == NULL)
3622 return TRUE;
3623 memset(update_table, IN_UCHARS(range_len), 256);
3624
3625 for (i = 0; i < range_len; i++)
3626 {
3627 byte_set = bytes + ((range_right - i) * MAX_N_BYTES);
3628 SLJIT_ASSERT(byte_set[0] > 0 && byte_set[0] < 255);
3629 byte_set_end = byte_set + byte_set[0];
3630 byte_set++;
3631 while (byte_set <= byte_set_end)
3632 {
3633 if (update_table[*byte_set] > IN_UCHARS(i))
3634 update_table[*byte_set] = IN_UCHARS(i);
3635 byte_set++;
3636 }
3637 }
3638 }
3639
3640 offsets[0] = -1;
3641 /* Scan forward. */
3642 for (i = 0; i < max; i++)
3643 if (ones[i] <= 2) {
3644 offsets[0] = i;
3645 break;
3646 }
3647
3648 if (offsets[0] < 0 && range_right < 0)
3649 return FALSE;
3650
3651 if (offsets[0] >= 0)
3652 {
3653 /* Scan backward. */
3654 offsets[1] = -1;
3655 for (i = max - 1; i > offsets[0]; i--)
3656 if (ones[i] <= 2 && i != range_right)
3657 {
3658 offsets[1] = i;
3659 break;
3660 }
3661
3662 /* This case is handled better by fast_forward_first_char. */
3663 if (offsets[1] == -1 && offsets[0] == 0 && range_right < 0)
3664 return FALSE;
3665
3666 offsets[2] = -1;
3667 /* We only search for a middle character if there is no range check. */
3668 if (offsets[1] >= 0 && range_right == -1)
3669 {
3670 /* Scan from middle. */
3671 for (i = (offsets[0] + offsets[1]) / 2 + 1; i < offsets[1]; i++)
3672 if (ones[i] <= 2)
3673 {
3674 offsets[2] = i;
3675 break;
3676 }
3677
3678 if (offsets[2] == -1)
3679 {
3680 for (i = (offsets[0] + offsets[1]) / 2; i > offsets[0]; i--)
3681 if (ones[i] <= 2)
3682 {
3683 offsets[2] = i;
3684 break;
3685 }
3686 }
3687 }
3688
3689 SLJIT_ASSERT(offsets[1] == -1 || (offsets[0] < offsets[1]));
3690 SLJIT_ASSERT(offsets[2] == -1 || (offsets[0] < offsets[2] && offsets[1] > offsets[2]));
3691
3692 chars[0] = chars[offsets[0] << 1];
3693 chars[1] = chars[(offsets[0] << 1) + 1];
3694 if (offsets[2] >= 0)
3695 {
3696 chars[2] = chars[offsets[2] << 1];
3697 chars[3] = chars[(offsets[2] << 1) + 1];
3698 }
3699 if (offsets[1] >= 0)
3700 {
3701 chars[4] = chars[offsets[1] << 1];
3702 chars[5] = chars[(offsets[1] << 1) + 1];
3703 }
3704 }
3705
3706 max -= 1;
3707 if (firstline)
3708 {
3709 SLJIT_ASSERT(common->first_line_end != 0);
3710 OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), common->first_line_end);
3711 OP1(SLJIT_MOV, TMP3, 0, STR_END, 0);
3712 OP2(SLJIT_SUB, STR_END, 0, STR_END, 0, SLJIT_IMM, IN_UCHARS(max));
3713 quit = CMP(SLJIT_LESS_EQUAL, STR_END, 0, TMP1, 0);
3714 OP1(SLJIT_MOV, STR_END, 0, TMP1, 0);
3715 JUMPHERE(quit);
3716 }
3717 else
3718 OP2(SLJIT_SUB, STR_END, 0, STR_END, 0, SLJIT_IMM, IN_UCHARS(max));
3719
3720 #if !(defined SLJIT_CONFIG_X86_32 && SLJIT_CONFIG_X86_32)
3721 if (range_right >= 0)
3722 OP1(SLJIT_MOV, RETURN_ADDR, 0, SLJIT_IMM, (sljit_sw)update_table);
3723 #endif
3724
3725 start = LABEL();
3726 quit = CMP(SLJIT_GREATER_EQUAL, STR_PTR, 0, STR_END, 0);
3727
3728 SLJIT_ASSERT(range_right >= 0 || offsets[0] >= 0);
3729
3730 if (range_right >= 0)
3731 {
3732 #if defined COMPILE_PCRE8 || (defined SLJIT_LITTLE_ENDIAN && SLJIT_LITTLE_ENDIAN)
3733 OP1(SLJIT_MOV_UB, TMP1, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(range_right));
3734 #else
3735 OP1(SLJIT_MOV_UB, TMP1, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(range_right + 1) - 1);
3736 #endif
3737
3738 #if !(defined SLJIT_CONFIG_X86_32 && SLJIT_CONFIG_X86_32)
3739 OP1(SLJIT_MOV_UB, TMP1, 0, SLJIT_MEM2(RETURN_ADDR, TMP1), 0);
3740 #else
3741 OP1(SLJIT_MOV_UB, TMP1, 0, SLJIT_MEM1(TMP1), (sljit_sw)update_table);
3742 #endif
3743 OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, TMP1, 0);
3744 CMPTO(SLJIT_NOT_EQUAL, TMP1, 0, SLJIT_IMM, 0, start);
3745 }
3746
3747 if (offsets[0] >= 0)
3748 {
3749 OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(offsets[0]));
3750 if (offsets[1] >= 0)
3751 OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(offsets[1]));
3752 OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
3753
3754 if (chars[1] != 0)
3755 OP2(SLJIT_OR, TMP1, 0, TMP1, 0, SLJIT_IMM, chars[1]);
3756 CMPTO(SLJIT_NOT_EQUAL, TMP1, 0, SLJIT_IMM, chars[0], start);
3757 if (offsets[2] >= 0)
3758 OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(offsets[2] - 1));
3759
3760 if (offsets[1] >= 0)
3761 {
3762 if (chars[5] != 0)
3763 OP2(SLJIT_OR, TMP2, 0, TMP2, 0, SLJIT_IMM, chars[5]);
3764 CMPTO(SLJIT_NOT_EQUAL, TMP2, 0, SLJIT_IMM, chars[4], start);
3765 }
3766
3767 if (offsets[2] >= 0)
3768 {
3769 if (chars[3] != 0)
3770 OP2(SLJIT_OR, TMP1, 0, TMP1, 0, SLJIT_IMM, chars[3]);
3771 CMPTO(SLJIT_NOT_EQUAL, TMP1, 0, SLJIT_IMM, chars[2], start);
3772 }
3773 OP2(SLJIT_SUB, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
3774 }
3775
3776 JUMPHERE(quit);
3777
3778 if (firstline)
3779 {
3780 if (range_right >= 0)
3781 OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), common->first_line_end);
3782 OP1(SLJIT_MOV, STR_END, 0, TMP3, 0);
3783 if (range_right >= 0)
3784 {
3785 quit = CMP(SLJIT_LESS_EQUAL, STR_PTR, 0, TMP1, 0);
3786 OP1(SLJIT_MOV, STR_PTR, 0, TMP1, 0);
3787 JUMPHERE(quit);
3788 }
3789 }
3790 else
3791 OP2(SLJIT_ADD, STR_END, 0, STR_END, 0, SLJIT_IMM, IN_UCHARS(max));
3792 return TRUE;
3793 }
3794
3795 #undef MAX_N_CHARS
3796 #undef MAX_N_BYTES
3797
3798 static SLJIT_INLINE void fast_forward_first_char(compiler_common *common, pcre_uchar first_char, BOOL caseless, BOOL firstline)
3799 {
3800 DEFINE_COMPILER;
3801 struct sljit_label *start;
3802 struct sljit_jump *quit;
3803 struct sljit_jump *found;
3804 pcre_uchar oc, bit;
3805
3806 if (firstline)
3807 {
3808 SLJIT_ASSERT(common->first_line_end != 0);
3809 OP1(SLJIT_MOV, TMP3, 0, STR_END, 0);
3810 OP1(SLJIT_MOV, STR_END, 0, SLJIT_MEM1(SLJIT_SP), common->first_line_end);
3811 }
3812
3813 start = LABEL();
3814 quit = CMP(SLJIT_GREATER_EQUAL, STR_PTR, 0, STR_END, 0);
3815 OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), 0);
3816
3817 oc = first_char;
3818 if (caseless)
3819 {
3820 oc = TABLE_GET(first_char, common->fcc, first_char);
3821 #if defined SUPPORT_UCP && !(defined COMPILE_PCRE8)
3822 if (first_char > 127 && common->utf)
3823 oc = UCD_OTHERCASE(first_char);
3824 #endif
3825 }
3826 if (first_char == oc)
3827 found = CMP(SLJIT_EQUAL, TMP1, 0, SLJIT_IMM, first_char);
3828 else
3829 {
3830 bit = first_char ^ oc;
3831 if (is_powerof2(bit))
3832 {
3833 OP2(SLJIT_OR, TMP2, 0, TMP1, 0, SLJIT_IMM, bit);
3834 found = CMP(SLJIT_EQUAL, TMP2, 0, SLJIT_IMM, first_char | bit);
3835 }
3836 else
3837 {
3838 OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, first_char);
3839 OP_FLAGS(SLJIT_MOV, TMP2, 0, SLJIT_UNUSED, 0, SLJIT_EQUAL);
3840 OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, oc);
3841 OP_FLAGS(SLJIT_OR | SLJIT_SET_E, TMP2, 0, TMP2, 0, SLJIT_EQUAL);
3842 found = JUMP(SLJIT_NOT_ZERO);
3843 }
3844 }
3845
3846 OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
3847 JUMPTO(SLJIT_JUMP, start);
3848 JUMPHERE(found);
3849 JUMPHERE(quit);
3850
3851 if (firstline)
3852 OP1(SLJIT_MOV, STR_END, 0, TMP3, 0);
3853 }
3854
3855 static SLJIT_INLINE void fast_forward_newline(compiler_common *common, BOOL firstline)
3856 {
3857 DEFINE_COMPILER;
3858 struct sljit_label *loop;
3859 struct sljit_jump *lastchar;
3860 struct sljit_jump *firstchar;
3861 struct sljit_jump *quit;
3862 struct sljit_jump *foundcr = NULL;
3863 struct sljit_jump *notfoundnl;
3864 jump_list *newline = NULL;
3865
3866 if (firstline)
3867 {
3868 SLJIT_ASSERT(common->first_line_end != 0);
3869 OP1(SLJIT_MOV, TMP3, 0, STR_END, 0);
3870 OP1(SLJIT_MOV, STR_END, 0, SLJIT_MEM1(SLJIT_SP), common->first_line_end);
3871 }
3872
3873 if (common->nltype == NLTYPE_FIXED && common->newline > 255)
3874 {
3875 lastchar = CMP(SLJIT_GREATER_EQUAL, STR_PTR, 0, STR_END, 0);
3876 OP1(SLJIT_MOV, TMP1, 0, ARGUMENTS, 0);
3877 OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(TMP1), SLJIT_OFFSETOF(jit_arguments, str));
3878 OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(TMP1), SLJIT_OFFSETOF(jit_arguments, begin));
3879 firstchar = CMP(SLJIT_LESS_EQUAL, STR_PTR, 0, TMP2, 0);
3880
3881 OP2(SLJIT_ADD, TMP1, 0, TMP1, 0, SLJIT_IMM, IN_UCHARS(2));
3882 OP2(SLJIT_SUB | SLJIT_SET_U, SLJIT_UNUSED, 0, STR_PTR, 0, TMP1, 0);
3883 OP_FLAGS(SLJIT_MOV, TMP2, 0, SLJIT_UNUSED, 0, SLJIT_GREATER_EQUAL);
3884 #if defined COMPILE_PCRE16 || defined COMPILE_PCRE32
3885 OP2(SLJIT_SHL, TMP2, 0, TMP2, 0, SLJIT_IMM, UCHAR_SHIFT);
3886 #endif
3887 OP2(SLJIT_SUB, STR_PTR, 0, STR_PTR, 0, TMP2, 0);
3888
3889 loop = LABEL();
3890 OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
3891 quit = CMP(SLJIT_GREATER_EQUAL, STR_PTR, 0, STR_END, 0);
3892 OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(-2));
3893 OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(-1));
3894 CMPTO(SLJIT_NOT_EQUAL, TMP1, 0, SLJIT_IMM, (common->newline >> 8) & 0xff, loop);
3895 CMPTO(SLJIT_NOT_EQUAL, TMP2, 0, SLJIT_IMM, common->newline & 0xff, loop);
3896
3897 JUMPHERE(quit);
3898 JUMPHERE(firstchar);
3899 JUMPHERE(lastchar);
3900
3901 if (firstline)
3902 OP1(SLJIT_MOV, STR_END, 0, TMP3, 0);
3903 return;
3904 }
3905
3906 OP1(SLJIT_MOV, TMP1, 0, ARGUMENTS, 0);
3907 OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(TMP1), SLJIT_OFFSETOF(jit_arguments, str));
3908 firstchar = CMP(SLJIT_LESS_EQUAL, STR_PTR, 0, TMP2, 0);
3909 skip_char_back(common);
3910
3911 loop = LABEL();
3912 common->ff_newline_shortcut = loop;
3913
3914 read_char_range(common, common->nlmin, common->nlmax, TRUE);
3915 lastchar = CMP(SLJIT_GREATER_EQUAL, STR_PTR, 0, STR_END, 0);
3916 if (common->nltype == NLTYPE_ANY || common->nltype == NLTYPE_ANYCRLF)
3917 foundcr = CMP(SLJIT_EQUAL, TMP1, 0, SLJIT_IMM, CHAR_CR);
3918 check_newlinechar(common, common->nltype, &newline, FALSE);
3919 set_jumps(newline, loop);
3920
3921 if (common->nltype == NLTYPE_ANY || common->nltype == NLTYPE_ANYCRLF)
3922 {
3923 quit = JUMP(SLJIT_JUMP);
3924 JUMPHERE(foundcr);
3925 notfoundnl = CMP(SLJIT_GREATER_EQUAL, STR_PTR, 0, STR_END, 0);
3926 OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), 0);
3927 OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, CHAR_NL);
3928 OP_FLAGS(SLJIT_MOV, TMP1, 0, SLJIT_UNUSED, 0, SLJIT_EQUAL);
3929 #if defined COMPILE_PCRE16 || defined COMPILE_PCRE32
3930 OP2(SLJIT_SHL, TMP1, 0, TMP1, 0, SLJIT_IMM, UCHAR_SHIFT);
3931 #endif
3932 OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, TMP1, 0);
3933 JUMPHERE(notfoundnl);
3934 JUMPHERE(quit);
3935 }
3936 JUMPHERE(lastchar);
3937 JUMPHERE(firstchar);
3938
3939 if (firstline)
3940 OP1(SLJIT_MOV, STR_END, 0, TMP3, 0);
3941 }
3942
3943 static BOOL check_class_ranges(compiler_common *common, const pcre_uint8 *bits, BOOL nclass, BOOL invert, jump_list **backtracks);
3944
3945 static SLJIT_INLINE void fast_forward_start_bits(compiler_common *common, pcre_uint8 *start_bits, BOOL firstline)
3946 {
3947 DEFINE_COMPILER;
3948 struct sljit_label *start;
3949 struct sljit_jump *quit;
3950 struct sljit_jump *found = NULL;
3951 jump_list *matches = NULL;
3952 #ifndef COMPILE_PCRE8
3953 struct sljit_jump *jump;
3954 #endif
3955
3956 if (firstline)
3957 {
3958 SLJIT_ASSERT(common->first_line_end != 0);
3959 OP1(SLJIT_MOV, RETURN_ADDR, 0, STR_END, 0);
3960 OP1(SLJIT_MOV, STR_END, 0, SLJIT_MEM1(SLJIT_SP), common->first_line_end);
3961 }
3962
3963 start = LABEL();
3964 quit = CMP(SLJIT_GREATER_EQUAL, STR_PTR, 0, STR_END, 0);
3965 OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), 0);
3966 #ifdef SUPPORT_UTF
3967 if (common->utf)
3968 OP1(SLJIT_MOV, TMP3, 0, TMP1, 0);
3969 #endif
3970
3971 if (!check_class_ranges(common, start_bits, (start_bits[31] & 0x80) != 0, TRUE, &matches))
3972 {
3973 #ifndef COMPILE_PCRE8
3974 jump = CMP(SLJIT_LESS, TMP1, 0, SLJIT_IMM, 255);
3975 OP1(SLJIT_MOV, TMP1, 0, SLJIT_IMM, 255);
3976 JUMPHERE(jump);
3977 #endif
3978 OP2(SLJIT_AND, TMP2, 0, TMP1, 0, SLJIT_IMM, 0x7);
3979 OP2(SLJIT_LSHR, TMP1, 0, TMP1, 0, SLJIT_IMM, 3);
3980 OP1(SLJIT_MOV_UB, TMP1, 0, SLJIT_MEM1(TMP1), (sljit_sw)start_bits);
3981 OP2(SLJIT_SHL, TMP2, 0, SLJIT_IMM, 1, TMP2, 0);
3982 OP2(SLJIT_AND | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, TMP2, 0);
3983 found = JUMP(SLJIT_NOT_ZERO);
3984 }
3985
3986 #ifdef SUPPORT_UTF
3987 if (common->utf)
3988 OP1(SLJIT_MOV, TMP1, 0, TMP3, 0);
3989 #endif
3990 OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
3991 #ifdef SUPPORT_UTF
3992 #if defined COMPILE_PCRE8
3993 if (common->utf)
3994 {
3995 CMPTO(SLJIT_LESS, TMP1, 0, SLJIT_IMM, 0xc0, start);
3996 OP1(SLJIT_MOV_UB, TMP1, 0, SLJIT_MEM1(TMP1), (sljit_sw)PRIV(utf8_table4) - 0xc0);
3997 OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, TMP1, 0);
3998 }
3999 #elif defined COMPILE_PCRE16
4000 if (common->utf)
4001 {
4002 CMPTO(SLJIT_LESS, TMP1, 0, SLJIT_IMM, 0xd800, start);
4003 OP2(SLJIT_AND, TMP1, 0, TMP1, 0, SLJIT_IMM, 0xfc00);
4004 OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0xd800);
4005 OP_FLAGS(SLJIT_MOV, TMP1, 0, SLJIT_UNUSED, 0, SLJIT_EQUAL);
4006 OP2(SLJIT_SHL, TMP1, 0, TMP1, 0, SLJIT_IMM, 1);
4007 OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, TMP1, 0);
4008 }
4009 #endif /* COMPILE_PCRE[8|16] */
4010 #endif /* SUPPORT_UTF */
4011 JUMPTO(SLJIT_JUMP, start);
4012 if (found != NULL)
4013 JUMPHERE(found);
4014 if (matches != NULL)
4015 set_jumps(matches, LABEL());
4016 JUMPHERE(quit);
4017
4018 if (firstline)
4019 OP1(SLJIT_MOV, STR_END, 0, RETURN_ADDR, 0);
4020 }
4021
4022 static SLJIT_INLINE struct sljit_jump *search_requested_char(compiler_common *common, pcre_uchar req_char, BOOL caseless, BOOL has_firstchar)
4023 {
4024 DEFINE_COMPILER;
4025 struct sljit_label *loop;
4026 struct sljit_jump *toolong;
4027 struct sljit_jump *alreadyfound;
4028 struct sljit_jump *found;
4029 struct sljit_jump *foundoc = NULL;
4030 struct sljit_jump *notfound;
4031 pcre_uint32 oc, bit;
4032
4033 SLJIT_ASSERT(common->req_char_ptr != 0);
4034 OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_SP), common->req_char_ptr);
4035 OP2(SLJIT_ADD, TMP1, 0, STR_PTR, 0, SLJIT_IMM, REQ_BYTE_MAX);
4036 toolong = CMP(SLJIT_LESS, TMP1, 0, STR_END, 0);
4037 alreadyfound = CMP(SLJIT_LESS, STR_PTR, 0, TMP2, 0);
4038
4039 if (has_firstchar)
4040 OP2(SLJIT_ADD, TMP1, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
4041 else
4042 OP1(SLJIT_MOV, TMP1, 0, STR_PTR, 0);
4043
4044 loop = LABEL();
4045 notfound = CMP(SLJIT_GREATER_EQUAL, TMP1, 0, STR_END, 0);
4046
4047 OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(TMP1), 0);
4048 oc = req_char;
4049 if (caseless)
4050 {
4051 oc = TABLE_GET(req_char, common->fcc, req_char);
4052 #if defined SUPPORT_UCP && !(defined COMPILE_PCRE8)
4053 if (req_char > 127 && common->utf)
4054 oc = UCD_OTHERCASE(req_char);
4055 #endif
4056 }
4057 if (req_char == oc)
4058 found = CMP(SLJIT_EQUAL, TMP2, 0, SLJIT_IMM, req_char);
4059 else
4060 {
4061 bit = req_char ^ oc;
4062 if (is_powerof2(bit))
4063 {
4064 OP2(SLJIT_OR, TMP2, 0, TMP2, 0, SLJIT_IMM, bit);
4065 found = CMP(SLJIT_EQUAL, TMP2, 0, SLJIT_IMM, req_char | bit);
4066 }
4067 else
4068 {
4069 found = CMP(SLJIT_EQUAL, TMP2, 0, SLJIT_IMM, req_char);
4070 foundoc = CMP(SLJIT_EQUAL, TMP2, 0, SLJIT_IMM, oc);
4071 }
4072 }
4073 OP2(SLJIT_ADD, TMP1, 0, TMP1, 0, SLJIT_IMM, IN_UCHARS(1));
4074 JUMPTO(SLJIT_JUMP, loop);
4075
4076 JUMPHERE(found);
4077 if (foundoc)
4078 JUMPHERE(foundoc);
4079 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->req_char_ptr, TMP1, 0);
4080 JUMPHERE(alreadyfound);
4081 JUMPHERE(toolong);
4082 return notfound;
4083 }
4084
4085 static void do_revertframes(compiler_common *common)
4086 {
4087 DEFINE_COMPILER;
4088 struct sljit_jump *jump;
4089 struct sljit_label *mainloop;
4090
4091 sljit_emit_fast_enter(compiler, RETURN_ADDR, 0);
4092 OP1(SLJIT_MOV, TMP1, 0, STACK_TOP, 0);
4093 GET_LOCAL_BASE(TMP3, 0, 0);
4094
4095 /* Drop frames until we reach STACK_TOP. */
4096 mainloop = LABEL();
4097 OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(TMP1), 0);
4098 OP2(SLJIT_SUB | SLJIT_SET_S, SLJIT_UNUSED, 0, TMP2, 0, SLJIT_IMM, 0);
4099 jump = JUMP(SLJIT_SIG_LESS_EQUAL);
4100
4101 OP2(SLJIT_ADD, TMP2, 0, TMP2, 0, TMP3, 0);
4102 OP1(SLJIT_MOV, SLJIT_MEM1(TMP2), 0, SLJIT_MEM1(TMP1), sizeof(sljit_sw));
4103 OP1(SLJIT_MOV, SLJIT_MEM1(TMP2), sizeof(sljit_sw), SLJIT_MEM1(TMP1), 2 * sizeof(sljit_sw));
4104 OP2(SLJIT_ADD, TMP1, 0, TMP1, 0, SLJIT_IMM, 3 * sizeof(sljit_sw));
4105 JUMPTO(SLJIT_JUMP, mainloop);
4106
4107 JUMPHERE(jump);
4108 jump = JUMP(SLJIT_SIG_LESS);
4109 /* End of dropping frames. */
4110 sljit_emit_fast_return(compiler, RETURN_ADDR, 0);
4111
4112 JUMPHERE(jump);
4113 OP1(SLJIT_NEG, TMP2, 0, TMP2, 0);
4114 OP2(SLJIT_ADD, TMP2, 0, TMP2, 0, TMP3, 0);
4115 OP1(SLJIT_MOV, SLJIT_MEM1(TMP2), 0, SLJIT_MEM1(TMP1), sizeof(sljit_sw));
4116 OP2(SLJIT_ADD, TMP1, 0, TMP1, 0, SLJIT_IMM, 2 * sizeof(sljit_sw));
4117 JUMPTO(SLJIT_JUMP, mainloop);
4118 }
4119
4120 static void check_wordboundary(compiler_common *common)
4121 {
4122 DEFINE_COMPILER;
4123 struct sljit_jump *skipread;
4124 jump_list *skipread_list = NULL;
4125 #if !(defined COMPILE_PCRE8) || defined SUPPORT_UTF
4126 struct sljit_jump *jump;
4127 #endif
4128
4129 SLJIT_COMPILE_ASSERT(ctype_word == 0x10, ctype_word_must_be_16);
4130
4131 sljit_emit_fast_enter(compiler, SLJIT_MEM1(SLJIT_SP), LOCALS0);
4132 /* Get type of the previous char, and put it to LOCALS1. */
4133 OP1(SLJIT_MOV, TMP1, 0, ARGUMENTS, 0);
4134 OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(TMP1), SLJIT_OFFSETOF(jit_arguments, begin));
4135 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), LOCALS1, SLJIT_IMM, 0);
4136 skipread = CMP(SLJIT_LESS_EQUAL, STR_PTR, 0, TMP1, 0);
4137 skip_char_back(common);
4138 check_start_used_ptr(common);
4139 read_char(common);
4140
4141 /* Testing char type. */
4142 #ifdef SUPPORT_UCP
4143 if (common->use_ucp)
4144 {
4145 OP1(SLJIT_MOV, TMP2, 0, SLJIT_IMM, 1);
4146 jump = CMP(SLJIT_EQUAL, TMP1, 0, SLJIT_IMM, CHAR_UNDERSCORE);
4147 add_jump(compiler, &common->getucd, JUMP(SLJIT_FAST_CALL));
4148 OP2(SLJIT_SUB, TMP1, 0, TMP1, 0, SLJIT_IMM, ucp_Ll);
4149 OP2(SLJIT_SUB | SLJIT_SET_U, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, ucp_Lu - ucp_Ll);
4150 OP_FLAGS(SLJIT_MOV, TMP2, 0, SLJIT_UNUSED, 0, SLJIT_LESS_EQUAL);
4151 OP2(SLJIT_SUB, TMP1, 0, TMP1, 0, SLJIT_IMM, ucp_Nd - ucp_Ll);
4152 OP2(SLJIT_SUB | SLJIT_SET_U, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, ucp_No - ucp_Nd);
4153 OP_FLAGS(SLJIT_OR, TMP2, 0, TMP2, 0, SLJIT_LESS_EQUAL);
4154 JUMPHERE(jump);
4155 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), LOCALS1, TMP2, 0);
4156 }
4157 else
4158 #endif
4159 {
4160 #ifndef COMPILE_PCRE8
4161 jump = CMP(SLJIT_GREATER, TMP1, 0, SLJIT_IMM, 255);
4162 #elif defined SUPPORT_UTF
4163 /* Here LOCALS1 has already been zeroed. */
4164 jump = NULL;
4165 if (common->utf)
4166 jump = CMP(SLJIT_GREATER, TMP1, 0, SLJIT_IMM, 255);
4167 #endif /* COMPILE_PCRE8 */
4168 OP1(SLJIT_MOV_UB, TMP1, 0, SLJIT_MEM1(TMP1), common->ctypes);
4169 OP2(SLJIT_LSHR, TMP1, 0, TMP1, 0, SLJIT_IMM, 4 /* ctype_word */);
4170 OP2(SLJIT_AND, TMP1, 0, TMP1, 0, SLJIT_IMM, 1);
4171 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), LOCALS1, TMP1, 0);
4172 #ifndef COMPILE_PCRE8
4173 JUMPHERE(jump);
4174 #elif defined SUPPORT_UTF
4175 if (jump != NULL)
4176 JUMPHERE(jump);
4177 #endif /* COMPILE_PCRE8 */
4178 }
4179 JUMPHERE(skipread);
4180
4181 OP1(SLJIT_MOV, TMP2, 0, SLJIT_IMM, 0);
4182 check_str_end(common, &skipread_list);
4183 peek_char(common, READ_CHAR_MAX);
4184
4185 /* Testing char type. This is a code duplication. */
4186 #ifdef SUPPORT_UCP
4187 if (common->use_ucp)
4188 {
4189 OP1(SLJIT_MOV, TMP2, 0, SLJIT_IMM, 1);
4190 jump = CMP(SLJIT_EQUAL, TMP1, 0, SLJIT_IMM, CHAR_UNDERSCORE);
4191 add_jump(compiler, &common->getucd, JUMP(SLJIT_FAST_CALL));
4192 OP2(SLJIT_SUB, TMP1, 0, TMP1, 0, SLJIT_IMM, ucp_Ll);
4193 OP2(SLJIT_SUB | SLJIT_SET_U, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, ucp_Lu - ucp_Ll);
4194 OP_FLAGS(SLJIT_MOV, TMP2, 0, SLJIT_UNUSED, 0, SLJIT_LESS_EQUAL);
4195 OP2(SLJIT_SUB, TMP1, 0, TMP1, 0, SLJIT_IMM, ucp_Nd - ucp_Ll);
4196 OP2(SLJIT_SUB | SLJIT_SET_U, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, ucp_No - ucp_Nd);
4197 OP_FLAGS(SLJIT_OR, TMP2, 0, TMP2, 0, SLJIT_LESS_EQUAL);
4198 JUMPHERE(jump);
4199 }
4200 else
4201 #endif
4202 {
4203 #ifndef COMPILE_PCRE8
4204 /* TMP2 may be destroyed by peek_char. */
4205 OP1(SLJIT_MOV, TMP2, 0, SLJIT_IMM, 0);
4206 jump = CMP(SLJIT_GREATER, TMP1, 0, SLJIT_IMM, 255);
4207 #elif defined SUPPORT_UTF
4208 OP1(SLJIT_MOV, TMP2, 0, SLJIT_IMM, 0);
4209 jump = NULL;
4210 if (common->utf)
4211 jump = CMP(SLJIT_GREATER, TMP1, 0, SLJIT_IMM, 255);
4212 #endif
4213 OP1(SLJIT_MOV_UB, TMP2, 0, SLJIT_MEM1(TMP1), common->ctypes);
4214 OP2(SLJIT_LSHR, TMP2, 0, TMP2, 0, SLJIT_IMM, 4 /* ctype_word */);
4215 OP2(SLJIT_AND, TMP2, 0, TMP2, 0, SLJIT_IMM, 1);
4216 #ifndef COMPILE_PCRE8
4217 JUMPHERE(jump);
4218 #elif defined SUPPORT_UTF
4219 if (jump != NULL)
4220 JUMPHERE(jump);
4221 #endif /* COMPILE_PCRE8 */
4222 }
4223 set_jumps(skipread_list, LABEL());
4224
4225 OP2(SLJIT_XOR | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP2, 0, SLJIT_MEM1(SLJIT_SP), LOCALS1);
4226 sljit_emit_fast_return(compiler, SLJIT_MEM1(SLJIT_SP), LOCALS0);
4227 }
4228
4229 static BOOL check_class_ranges(compiler_common *common, const pcre_uint8 *bits, BOOL nclass, BOOL invert, jump_list **backtracks)
4230 {
4231 DEFINE_COMPILER;
4232 int ranges[MAX_RANGE_SIZE];
4233 pcre_uint8 bit, cbit, all;
4234 int i, byte, length = 0;
4235
4236 bit = bits[0] & 0x1;
4237 /* All bits will be zero or one (since bit is zero or one). */
4238 all = -bit;
4239
4240 for (i = 0; i < 256; )
4241 {
4242 byte = i >> 3;
4243 if ((i & 0x7) == 0 && bits[byte] == all)
4244 i += 8;
4245 else
4246 {
4247 cbit = (bits[byte] >> (i & 0x7)) & 0x1;
4248 if (cbit != bit)
4249 {
4250 if (length >= MAX_RANGE_SIZE)
4251 return FALSE;
4252 ranges[length] = i;
4253 length++;
4254 bit = cbit;
4255 all = -cbit;
4256 }
4257 i++;
4258 }
4259 }
4260
4261 if (((bit == 0) && nclass) || ((bit == 1) && !nclass))
4262 {
4263 if (length >= MAX_RANGE_SIZE)
4264 return FALSE;
4265 ranges[length] = 256;
4266 length++;
4267 }
4268
4269 if (length < 0 || length > 4)
4270 return FALSE;
4271
4272 bit = bits[0] & 0x1;
4273 if (invert) bit ^= 0x1;
4274
4275 /* No character is accepted. */
4276 if (length == 0 && bit == 0)
4277 add_jump(compiler, backtracks, JUMP(SLJIT_JUMP));
4278
4279 switch(length)
4280 {
4281 case 0:
4282 /* When bit != 0, all characters are accepted. */
4283 return TRUE;
4284
4285 case 1:
4286 add_jump(compiler, backtracks, CMP(bit == 0 ? SLJIT_LESS : SLJIT_GREATER_EQUAL, TMP1, 0, SLJIT_IMM, ranges[0]));
4287 return TRUE;
4288
4289 case 2:
4290 if (ranges[0] + 1 != ranges[1])
4291 {
4292 OP2(SLJIT_SUB, TMP1, 0, TMP1, 0, SLJIT_IMM, ranges[0]);
4293 add_jump(compiler, backtracks, CMP(bit != 0 ? SLJIT_LESS : SLJIT_GREATER_EQUAL, TMP1, 0, SLJIT_IMM, ranges[1] - ranges[0]));
4294 }
4295 else
4296 add_jump(compiler, backtracks, CMP(bit != 0 ? SLJIT_EQUAL : SLJIT_NOT_EQUAL, TMP1, 0, SLJIT_IMM, ranges[0]));
4297 return TRUE;
4298
4299 case 3:
4300 if (bit != 0)
4301 {
4302 add_jump(compiler, backtracks, CMP(SLJIT_GREATER_EQUAL, TMP1, 0, SLJIT_IMM, ranges[2]));
4303 if (ranges[0] + 1 != ranges[1])
4304 {
4305 OP2(SLJIT_SUB, TMP1, 0, TMP1, 0, SLJIT_IMM, ranges[0]);
4306 add_jump(compiler, backtracks, CMP(SLJIT_LESS, TMP1, 0, SLJIT_IMM, ranges[1] - ranges[0]));
4307 }
4308 else
4309 add_jump(compiler, backtracks, CMP(SLJIT_EQUAL, TMP1, 0, SLJIT_IMM, ranges[0]));
4310 return TRUE;
4311 }
4312
4313 add_jump(compiler, backtracks, CMP(SLJIT_LESS, TMP1, 0, SLJIT_IMM, ranges[0]));
4314 if (ranges[1] + 1 != ranges[2])
4315 {
4316 OP2(SLJIT_SUB, TMP1, 0, TMP1, 0, SLJIT_IMM, ranges[1]);
4317 add_jump(compiler, backtracks, CMP(SLJIT_LESS, TMP1, 0, SLJIT_IMM, ranges[2] - ranges[1]));
4318 }
4319 else
4320 add_jump(compiler, backtracks, CMP(SLJIT_EQUAL, TMP1, 0, SLJIT_IMM, ranges[1]));
4321 return TRUE;
4322
4323 case 4:
4324 if ((ranges[1] - ranges[0]) == (ranges[3] - ranges[2])
4325 && (ranges[0] | (ranges[2] - ranges[0])) == ranges[2]
4326 && is_powerof2(ranges[2] - ranges[0]))
4327 {
4328 OP2(SLJIT_OR, TMP1, 0, TMP1, 0, SLJIT_IMM, ranges[2] - ranges[0]);
4329 if (ranges[2] + 1 != ranges[3])
4330 {
4331 OP2(SLJIT_SUB, TMP1, 0, TMP1, 0, SLJIT_IMM, ranges[2]);
4332 add_jump(compiler, backtracks, CMP(bit != 0 ? SLJIT_LESS : SLJIT_GREATER_EQUAL, TMP1, 0, SLJIT_IMM, ranges[3] - ranges[2]));
4333 }
4334 else
4335 add_jump(compiler, backtracks, CMP(bit != 0 ? SLJIT_EQUAL : SLJIT_NOT_EQUAL, TMP1, 0, SLJIT_IMM, ranges[2]));
4336 return TRUE;
4337 }
4338
4339 if (bit != 0)
4340 {
4341 i = 0;
4342 if (ranges[0] + 1 != ranges[1])
4343 {
4344 OP2(SLJIT_SUB, TMP1, 0, TMP1, 0, SLJIT_IMM, ranges[0]);
4345 add_jump(compiler, backtracks, CMP(SLJIT_LESS, TMP1, 0, SLJIT_IMM, ranges[1] - ranges[0]));
4346 i = ranges[0];
4347 }
4348 else
4349 add_jump(compiler, backtracks, CMP(SLJIT_EQUAL, TMP1, 0, SLJIT_IMM, ranges[0]));
4350
4351 if (ranges[2] + 1 != ranges[3])
4352 {
4353 OP2(SLJIT_SUB, TMP1, 0, TMP1, 0, SLJIT_IMM, ranges[2] - i);
4354 add_jump(compiler, backtracks, CMP(SLJIT_LESS, TMP1, 0, SLJIT_IMM, ranges[3] - ranges[2]));
4355 }
4356 else
4357 add_jump(compiler, backtracks, CMP(SLJIT_EQUAL, TMP1, 0, SLJIT_IMM, ranges[2] - i));
4358 return TRUE;
4359 }
4360
4361 OP2(SLJIT_SUB, TMP1, 0, TMP1, 0, SLJIT_IMM, ranges[0]);
4362 add_jump(compiler, backtracks, CMP(SLJIT_GREATER_EQUAL, TMP1, 0, SLJIT_IMM, ranges[3] - ranges[0]));
4363 if (ranges[1] + 1 != ranges[2])
4364 {
4365 OP2(SLJIT_SUB, TMP1, 0, TMP1, 0, SLJIT_IMM, ranges[1] - ranges[0]);
4366 add_jump(compiler, backtracks, CMP(SLJIT_LESS, TMP1, 0, SLJIT_IMM, ranges[2] - ranges[1]));
4367 }
4368 else
4369 add_jump(compiler, backtracks, CMP(SLJIT_EQUAL, TMP1, 0, SLJIT_IMM, ranges[1] - ranges[0]));
4370 return TRUE;
4371
4372 default:
4373 SLJIT_ASSERT_STOP();
4374 return FALSE;
4375 }
4376 }
4377
4378 static void check_anynewline(compiler_common *common)
4379 {
4380 /* Check whether TMP1 contains a newline character. TMP2 destroyed. */
4381 DEFINE_COMPILER;
4382
4383 sljit_emit_fast_enter(compiler, RETURN_ADDR, 0);
4384
4385 OP2(SLJIT_SUB, TMP1, 0, TMP1, 0, SLJIT_IMM, 0x0a);
4386 OP2(SLJIT_SUB | SLJIT_SET_U, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x0d - 0x0a);
4387 OP_FLAGS(SLJIT_MOV, TMP2, 0, SLJIT_UNUSED, 0, SLJIT_LESS_EQUAL);
4388 OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x85 - 0x0a);
4389 #if defined SUPPORT_UTF || defined COMPILE_PCRE16 || defined COMPILE_PCRE32
4390 #ifdef COMPILE_PCRE8
4391 if (common->utf)
4392 {
4393 #endif
4394 OP_FLAGS(SLJIT_OR, TMP2, 0, TMP2, 0, SLJIT_EQUAL);
4395 OP2(SLJIT_OR, TMP1, 0, TMP1, 0, SLJIT_IMM, 0x1);
4396 OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x2029 - 0x0a);
4397 #ifdef COMPILE_PCRE8
4398 }
4399 #endif
4400 #endif /* SUPPORT_UTF || COMPILE_PCRE16 || COMPILE_PCRE32 */
4401 OP_FLAGS(SLJIT_OR | SLJIT_SET_E, TMP2, 0, TMP2, 0, SLJIT_EQUAL);
4402 sljit_emit_fast_return(compiler, RETURN_ADDR, 0);
4403 }
4404
4405 static void check_hspace(compiler_common *common)
4406 {
4407 /* Check whether TMP1 contains a newline character. TMP2 destroyed. */
4408 DEFINE_COMPILER;
4409
4410 sljit_emit_fast_enter(compiler, RETURN_ADDR, 0);
4411
4412 OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x09);
4413 OP_FLAGS(SLJIT_MOV, TMP2, 0, SLJIT_UNUSED, 0, SLJIT_EQUAL);
4414 OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x20);
4415 OP_FLAGS(SLJIT_OR, TMP2, 0, TMP2, 0, SLJIT_EQUAL);
4416 OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0xa0);
4417 #if defined SUPPORT_UTF || defined COMPILE_PCRE16 || defined COMPILE_PCRE32
4418 #ifdef COMPILE_PCRE8
4419 if (common->utf)
4420 {
4421 #endif
4422 OP_FLAGS(SLJIT_OR, TMP2, 0, TMP2, 0, SLJIT_EQUAL);
4423 OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x1680);
4424 OP_FLAGS(SLJIT_OR, TMP2, 0, TMP2, 0, SLJIT_EQUAL);
4425 OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x180e);
4426 OP_FLAGS(SLJIT_OR, TMP2, 0, TMP2, 0, SLJIT_EQUAL);
4427 OP2(SLJIT_SUB, TMP1, 0, TMP1, 0, SLJIT_IMM, 0x2000);
4428 OP2(SLJIT_SUB | SLJIT_SET_U, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x200A - 0x2000);
4429 OP_FLAGS(SLJIT_OR, TMP2, 0, TMP2, 0, SLJIT_LESS_EQUAL);
4430 OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x202f - 0x2000);
4431 OP_FLAGS(SLJIT_OR, TMP2, 0, TMP2, 0, SLJIT_EQUAL);
4432 OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x205f - 0x2000);
4433 OP_FLAGS(SLJIT_OR, TMP2, 0, TMP2, 0, SLJIT_EQUAL);
4434 OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x3000 - 0x2000);
4435 #ifdef COMPILE_PCRE8
4436 }
4437 #endif
4438 #endif /* SUPPORT_UTF || COMPILE_PCRE16 || COMPILE_PCRE32 */
4439 OP_FLAGS(SLJIT_OR | SLJIT_SET_E, TMP2, 0, TMP2, 0, SLJIT_EQUAL);
4440
4441 sljit_emit_fast_return(compiler, RETURN_ADDR, 0);
4442 }
4443
4444 static void check_vspace(compiler_common *common)
4445 {
4446 /* Check whether TMP1 contains a newline character. TMP2 destroyed. */
4447 DEFINE_COMPILER;
4448
4449 sljit_emit_fast_enter(compiler, RETURN_ADDR, 0);
4450
4451 OP2(SLJIT_SUB, TMP1, 0, TMP1, 0, SLJIT_IMM, 0x0a);
4452 OP2(SLJIT_SUB | SLJIT_SET_U, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x0d - 0x0a);
4453 OP_FLAGS(SLJIT_MOV, TMP2, 0, SLJIT_UNUSED, 0, SLJIT_LESS_EQUAL);
4454 OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x85 - 0x0a);
4455 #if defined SUPPORT_UTF || defined COMPILE_PCRE16 || defined COMPILE_PCRE32
4456 #ifdef COMPILE_PCRE8
4457 if (common->utf)
4458 {
4459 #endif
4460 OP_FLAGS(SLJIT_OR | SLJIT_SET_E, TMP2, 0, TMP2, 0, SLJIT_EQUAL);
4461 OP2(SLJIT_OR, TMP1, 0, TMP1, 0, SLJIT_IMM, 0x1);
4462 OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x2029 - 0x0a);
4463 #ifdef COMPILE_PCRE8
4464 }
4465 #endif
4466 #endif /* SUPPORT_UTF || COMPILE_PCRE16 || COMPILE_PCRE32 */
4467 OP_FLAGS(SLJIT_OR | SLJIT_SET_E, TMP2, 0, TMP2, 0, SLJIT_EQUAL);
4468
4469 sljit_emit_fast_return(compiler, RETURN_ADDR, 0);
4470 }
4471
4472 #define CHAR1 STR_END
4473 #define CHAR2 STACK_TOP
4474
4475 static void do_casefulcmp(compiler_common *common)
4476 {
4477 DEFINE_COMPILER;
4478 struct sljit_jump *jump;
4479 struct sljit_label *label;
4480
4481 sljit_emit_fast_enter(compiler, RETURN_ADDR, 0);
4482 OP2(SLJIT_SUB, STR_PTR, 0, STR_PTR, 0, TMP2, 0);
4483 OP1(SLJIT_MOV, TMP3, 0, CHAR1, 0);
4484 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), LOCALS0, CHAR2, 0);
4485 OP2(SLJIT_SUB, TMP1, 0, TMP1, 0, SLJIT_IMM, IN_UCHARS(1));
4486 OP2(SLJIT_SUB, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
4487
4488 label = LABEL();
4489 OP1(MOVU_UCHAR, CHAR1, 0, SLJIT_MEM1(TMP1), IN_UCHARS(1));
4490 OP1(MOVU_UCHAR, CHAR2, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(1));
4491 jump = CMP(SLJIT_NOT_EQUAL, CHAR1, 0, CHAR2, 0);
4492 OP2(SLJIT_SUB | SLJIT_SET_E, TMP2, 0, TMP2, 0, SLJIT_IMM, IN_UCHARS(1));
4493 JUMPTO(SLJIT_NOT_ZERO, label);
4494
4495 JUMPHERE(jump);
4496 OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
4497 OP1(SLJIT_MOV, CHAR1, 0, TMP3, 0);
4498 OP1(SLJIT_MOV, CHAR2, 0, SLJIT_MEM1(SLJIT_SP), LOCALS0);
4499 sljit_emit_fast_return(compiler, RETURN_ADDR, 0);
4500 }
4501
4502 #define LCC_TABLE STACK_LIMIT
4503
4504 static void do_caselesscmp(compiler_common *common)
4505 {
4506 DEFINE_COMPILER;
4507 struct sljit_jump *jump;
4508 struct sljit_label *label;
4509
4510 sljit_emit_fast_enter(compiler, RETURN_ADDR, 0);
4511 OP2(SLJIT_SUB, STR_PTR, 0, STR_PTR, 0, TMP2, 0);
4512
4513 OP1(SLJIT_MOV, TMP3, 0, LCC_TABLE, 0);
4514 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), LOCALS0, CHAR1, 0);
4515 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), LOCALS1, CHAR2, 0);
4516 OP1(SLJIT_MOV, LCC_TABLE, 0, SLJIT_IMM, common->lcc);
4517 OP2(SLJIT_SUB, TMP1, 0, TMP1, 0, SLJIT_IMM, IN_UCHARS(1));
4518 OP2(SLJIT_SUB, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
4519
4520 label = LABEL();
4521 OP1(MOVU_UCHAR, CHAR1, 0, SLJIT_MEM1(TMP1), IN_UCHARS(1));
4522 OP1(MOVU_UCHAR, CHAR2, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(1));
4523 #ifndef COMPILE_PCRE8
4524 jump = CMP(SLJIT_GREATER, CHAR1, 0, SLJIT_IMM, 255);
4525 #endif
4526 OP1(SLJIT_MOV_UB, CHAR1, 0, SLJIT_MEM2(LCC_TABLE, CHAR1), 0);
4527 #ifndef COMPILE_PCRE8
4528 JUMPHERE(jump);
4529 jump = CMP(SLJIT_GREATER, CHAR2, 0, SLJIT_IMM, 255);
4530 #endif
4531 OP1(SLJIT_MOV_UB, CHAR2, 0, SLJIT_MEM2(LCC_TABLE, CHAR2), 0);
4532 #ifndef COMPILE_PCRE8
4533 JUMPHERE(jump);
4534 #endif
4535 jump = CMP(SLJIT_NOT_EQUAL, CHAR1, 0, CHAR2, 0);
4536 OP2(SLJIT_SUB | SLJIT_SET_E, TMP2, 0, TMP2, 0, SLJIT_IMM, IN_UCHARS(1));
4537 JUMPTO(SLJIT_NOT_ZERO, label);
4538
4539 JUMPHERE(jump);
4540 OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
4541 OP1(SLJIT_MOV, LCC_TABLE, 0, TMP3, 0);
4542 OP1(SLJIT_MOV, CHAR1, 0, SLJIT_MEM1(SLJIT_SP), LOCALS0);
4543 OP1(SLJIT_MOV, CHAR2, 0, SLJIT_MEM1(SLJIT_SP), LOCALS1);
4544 sljit_emit_fast_return(compiler, RETURN_ADDR, 0);
4545 }
4546
4547 #undef LCC_TABLE
4548 #undef CHAR1
4549 #undef CHAR2
4550
4551 #if defined SUPPORT_UTF && defined SUPPORT_UCP
4552
4553 static const pcre_uchar * SLJIT_CALL do_utf_caselesscmp(pcre_uchar *src1, jit_arguments *args, pcre_uchar *end1)
4554 {
4555 /* This function would be ineffective to do in JIT level. */
4556 pcre_uint32 c1, c2;
4557 const pcre_uchar *src2 = args->uchar_ptr;
4558 const pcre_uchar *end2 = args->end;
4559 const ucd_record *ur;
4560 const pcre_uint32 *pp;
4561
4562 while (src1 < end1)
4563 {
4564 if (src2 >= end2)
4565 return (pcre_uchar*)1;
4566 GETCHARINC(c1, src1);
4567 GETCHARINC(c2, src2);
4568 ur = GET_UCD(c2);
4569 if (c1 != c2 && c1 != c2 + ur->other_case)
4570 {
4571 pp = PRIV(ucd_caseless_sets) + ur->caseset;
4572 for (;;)
4573 {
4574 if (c1 < *pp) return NULL;
4575 if (c1 == *pp++) break;
4576 }
4577 }
4578 }
4579 return src2;
4580 }
4581
4582 #endif /* SUPPORT_UTF && SUPPORT_UCP */
4583
4584 static pcre_uchar *byte_sequence_compare(compiler_common *common, BOOL caseless, pcre_uchar *cc,
4585 compare_context *context, jump_list **backtracks)
4586 {
4587 DEFINE_COMPILER;
4588 unsigned int othercasebit = 0;
4589 pcre_uchar *othercasechar = NULL;
4590 #ifdef SUPPORT_UTF
4591 int utflength;
4592 #endif
4593
4594 if (caseless && char_has_othercase(common, cc))
4595 {
4596 othercasebit = char_get_othercase_bit(common, cc);
4597 SLJIT_ASSERT(othercasebit);
4598 /* Extracting bit difference info. */
4599 #if defined COMPILE_PCRE8
4600 othercasechar = cc + (othercasebit >> 8);
4601 othercasebit &= 0xff;
4602 #elif defined COMPILE_PCRE16 || defined COMPILE_PCRE32
4603 /* Note that this code only handles characters in the BMP. If there
4604 ever are characters outside the BMP whose othercase differs in only one
4605 bit from itself (there currently are none), this code will need to be
4606 revised for COMPILE_PCRE32. */
4607 othercasechar = cc + (othercasebit >> 9);
4608 if ((othercasebit & 0x100) != 0)
4609 othercasebit = (othercasebit & 0xff) << 8;
4610 else
4611 othercasebit &= 0xff;
4612 #endif /* COMPILE_PCRE[8|16|32] */
4613 }
4614
4615 if (context->sourcereg == -1)
4616 {
4617 #if defined COMPILE_PCRE8
4618 #if defined SLJIT_UNALIGNED && SLJIT_UNALIGNED
4619 if (context->length >= 4)
4620 OP1(SLJIT_MOV_SI, TMP1, 0, SLJIT_MEM1(STR_PTR), -context->length);
4621 else if (context->length >= 2)
4622 OP1(SLJIT_MOV_UH, TMP1, 0, SLJIT_MEM1(STR_PTR), -context->length);
4623 else
4624 #endif
4625 OP1(SLJIT_MOV_UB, TMP1, 0, SLJIT_MEM1(STR_PTR), -context->length);
4626 #elif defined COMPILE_PCRE16
4627 #if defined SLJIT_UNALIGNED && SLJIT_UNALIGNED
4628 if (context->length >= 4)
4629 OP1(SLJIT_MOV_SI, TMP1, 0, SLJIT_MEM1(STR_PTR), -context->length);
4630 else
4631 #endif
4632 OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), -context->length);
4633 #elif defined COMPILE_PCRE32
4634 OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), -context->length);
4635 #endif /* COMPILE_PCRE[8|16|32] */
4636 context->sourcereg = TMP2;
4637 }
4638
4639 #ifdef SUPPORT_UTF
4640 utflength = 1;
4641 if (common->utf && HAS_EXTRALEN(*cc))
4642 utflength += GET_EXTRALEN(*cc);
4643
4644 do
4645 {
4646 #endif
4647
4648 context->length -= IN_UCHARS(1);
4649 #if (defined SLJIT_UNALIGNED && SLJIT_UNALIGNED) && (defined COMPILE_PCRE8 || defined COMPILE_PCRE16)
4650
4651 /* Unaligned read is supported. */
4652 if (othercasebit != 0 && othercasechar == cc)
4653 {
4654 context->c.asuchars[context->ucharptr] = *cc | othercasebit;
4655 context->oc.asuchars[context->ucharptr] = othercasebit;
4656 }
4657 else
4658 {
4659 context->c.asuchars[context->ucharptr] = *cc;
4660 context->oc.asuchars[context->ucharptr] = 0;
4661 }
4662 context->ucharptr++;
4663
4664 #if defined COMPILE_PCRE8
4665 if (context->ucharptr >= 4 || context->length == 0 || (context->ucharptr == 2 && context->length == 1))
4666 #else
4667 if (context->ucharptr >= 2 || context->length == 0)
4668 #endif
4669 {
4670 if (context->length >= 4)
4671 OP1(SLJIT_MOV_SI, context->sourcereg, 0, SLJIT_MEM1(STR_PTR), -context->length);
4672 else if (context->length >= 2)
4673 OP1(SLJIT_MOV_UH, context->sourcereg, 0, SLJIT_MEM1(STR_PTR), -context->length);
4674 #if defined COMPILE_PCRE8
4675 else if (context->length >= 1)
4676 OP1(SLJIT_MOV_UB, context->sourcereg, 0, SLJIT_MEM1(STR_PTR), -context->length);
4677 #endif /* COMPILE_PCRE8 */
4678 context->sourcereg = context->sourcereg == TMP1 ? TMP2 : TMP1;
4679
4680 switch(context->ucharptr)
4681 {
4682 case 4 / sizeof(pcre_uchar):
4683 if (context->oc.asint != 0)
4684 OP2(SLJIT_OR, context->sourcereg, 0, context->sourcereg, 0, SLJIT_IMM, context->oc.asint);
4685 add_jump(compiler, backtracks, CMP(SLJIT_NOT_EQUAL, context->sourcereg, 0, SLJIT_IMM, context->c.asint | context->oc.asint));
4686 break;
4687
4688 case 2 / sizeof(pcre_uchar):
4689 if (context->oc.asushort != 0)
4690 OP2(SLJIT_OR, context->sourcereg, 0, context->sourcereg, 0, SLJIT_IMM, context->oc.asushort);
4691 add_jump(compiler, backtracks, CMP(SLJIT_NOT_EQUAL, context->sourcereg, 0, SLJIT_IMM, context->c.asushort | context->oc.asushort));
4692 break;
4693
4694 #ifdef COMPILE_PCRE8
4695 case 1:
4696 if (context->oc.asbyte != 0)
4697 OP2(SLJIT_OR, context->sourcereg, 0, context->sourcereg, 0, SLJIT_IMM, context->oc.asbyte);
4698 add_jump(compiler, backtracks, CMP(SLJIT_NOT_EQUAL, context->sourcereg, 0, SLJIT_IMM, context->c.asbyte | context->oc.asbyte));
4699 break;
4700 #endif
4701
4702 default:
4703 SLJIT_ASSERT_STOP();
4704 break;
4705 }
4706 context->ucharptr = 0;
4707 }
4708
4709 #else
4710
4711 /* Unaligned read is unsupported or in 32 bit mode. */
4712 if (context->length >= 1)
4713 OP1(MOV_UCHAR, context->sourcereg, 0, SLJIT_MEM1(STR_PTR), -context->length);
4714
4715 context->sourcereg = context->sourcereg == TMP1 ? TMP2 : TMP1;
4716
4717 if (othercasebit != 0 && othercasechar == cc)
4718 {
4719 OP2(SLJIT_OR, context->sourcereg, 0, context->sourcereg, 0, SLJIT_IMM, othercasebit);
4720 add_jump(compiler, backtracks, CMP(SLJIT_NOT_EQUAL, context->sourcereg, 0, SLJIT_IMM, *cc | othercasebit));
4721 }
4722 else
4723 add_jump(compiler, backtracks, CMP(SLJIT_NOT_EQUAL, context->sourcereg, 0, SLJIT_IMM, *cc));
4724
4725 #endif
4726
4727 cc++;
4728 #ifdef SUPPORT_UTF
4729 utflength--;
4730 }
4731 while (utflength > 0);
4732 #endif
4733
4734 return cc;
4735 }
4736
4737 #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
4738
4739 #define SET_TYPE_OFFSET(value) \
4740 if ((value) != typeoffset) \
4741 { \
4742 if ((value) < typeoffset) \
4743 OP2(SLJIT_ADD, typereg, 0, typereg, 0, SLJIT_IMM, typeoffset - (value)); \
4744 else \
4745 OP2(SLJIT_SUB, typereg, 0, typereg, 0, SLJIT_IMM, (value) - typeoffset); \
4746 } \
4747 typeoffset = (value);
4748
4749 #define SET_CHAR_OFFSET(value) \
4750 if ((value) != charoffset) \
4751 { \
4752 if ((value) < charoffset) \
4753 OP2(SLJIT_ADD, TMP1, 0, TMP1, 0, SLJIT_IMM, (sljit_sw)(charoffset - (value))); \
4754 else \
4755 OP2(SLJIT_SUB, TMP1, 0, TMP1, 0, SLJIT_IMM, (sljit_sw)((value) - charoffset)); \
4756 } \
4757 charoffset = (value);
4758
4759 static void compile_xclass_matchingpath(compiler_common *common, pcre_uchar *cc, jump_list **backtracks)
4760 {
4761 DEFINE_COMPILER;
4762 jump_list *found = NULL;
4763 jump_list **list = (cc[0] & XCL_NOT) == 0 ? &found : backtracks;
4764 sljit_uw c, charoffset, max = 256, min = READ_CHAR_MAX;
4765 struct sljit_jump *jump = NULL;
4766 pcre_uchar *ccbegin;
4767 int compares, invertcmp, numberofcmps;
4768 #if defined SUPPORT_UTF && (defined COMPILE_PCRE8 || defined COMPILE_PCRE16)
4769 BOOL utf = common->utf;
4770 #endif
4771
4772 #ifdef SUPPORT_UCP
4773 BOOL needstype = FALSE, needsscript = FALSE, needschar = FALSE;
4774 BOOL charsaved = FALSE;
4775 int typereg = TMP1, scriptreg = TMP1;
4776 const pcre_uint32 *other_cases;
4777 sljit_uw typeoffset;
4778 #endif
4779
4780 /* Scanning the necessary info. */
4781 cc++;
4782 ccbegin = cc;
4783 compares = 0;
4784 if (cc[-1] & XCL_MAP)
4785 {
4786 min = 0;
4787 cc += 32 / sizeof(pcre_uchar);
4788 }
4789
4790 while (*cc != XCL_END)
4791 {
4792 compares++;
4793 if (*cc == XCL_SINGLE)
4794 {
4795 cc ++;
4796 GETCHARINCTEST(c, cc);
4797 if (c > max) max = c;
4798 if (c < min) min = c;
4799 #ifdef SUPPORT_UCP
4800 needschar = TRUE;
4801 #endif
4802 }
4803 else if (*cc == XCL_RANGE)
4804 {
4805 cc ++;
4806 GETCHARINCTEST(c, cc);
4807 if (c < min) min = c;
4808 GETCHARINCTEST(c, cc);
4809 if (c > max) max = c;
4810 #ifdef SUPPORT_UCP
4811 needschar = TRUE;
4812 #endif
4813 }
4814 #ifdef SUPPORT_UCP
4815 else
4816 {
4817 SLJIT_ASSERT(*cc == XCL_PROP || *cc == XCL_NOTPROP);
4818 cc++;
4819 if (*cc == PT_CLIST)
4820 {
4821 other_cases = PRIV(ucd_caseless_sets) + cc[1];
4822 while (*other_cases != NOTACHAR)
4823 {
4824 if (*other_cases > max) max = *other_cases;
4825 if (*other_cases < min) min = *other_cases;
4826 other_cases++;
4827 }
4828 }
4829 else
4830 {
4831 max = READ_CHAR_MAX;
4832 min = 0;
4833 }
4834
4835 switch(*cc)
4836 {
4837 case PT_ANY:
4838 break;
4839
4840 case PT_LAMP:
4841 case PT_GC:
4842 case PT_PC:
4843 case PT_ALNUM:
4844 needstype = TRUE;
4845 break;
4846
4847 case PT_SC:
4848 needsscript = TRUE;
4849 break;
4850
4851 case PT_SPACE:
4852 case PT_PXSPACE:
4853 case PT_WORD:
4854 case PT_PXGRAPH:
4855 case PT_PXPRINT:
4856 case PT_PXPUNCT:
4857 needstype = TRUE;
4858 needschar = TRUE;
4859 break;
4860
4861 case PT_CLIST:
4862 case PT_UCNC:
4863 needschar = TRUE;
4864 break;
4865
4866 default:
4867 SLJIT_ASSERT_STOP();
4868 break;
4869 }
4870 cc += 2;
4871 }
4872 #endif
4873 }
4874
4875 /* We are not necessary in utf mode even in 8 bit mode. */
4876 cc = ccbegin;
4877 detect_partial_match(common, backtracks);
4878 read_char_range(common, min, max, (cc[-1] & XCL_NOT) != 0);
4879
4880 if ((cc[-1] & XCL_HASPROP) == 0)
4881 {
4882 if ((cc[-1] & XCL_MAP) != 0)
4883 {
4884 jump = CMP(SLJIT_GREATER, TMP1, 0, SLJIT_IMM, 255);
4885 if (!check_class_ranges(common, (const pcre_uint8 *)cc, (((const pcre_uint8 *)cc)[31] & 0x80) != 0, TRUE, &found))
4886 {
4887 OP2(SLJIT_AND, TMP2, 0, TMP1, 0, SLJIT_IMM, 0x7);
4888 OP2(SLJIT_LSHR, TMP1, 0, TMP1, 0, SLJIT_IMM, 3);
4889 OP1(SLJIT_MOV_UB, TMP1, 0, SLJIT_MEM1(TMP1), (sljit_sw)cc);
4890 OP2(SLJIT_SHL, TMP2, 0, SLJIT_IMM, 1, TMP2, 0);
4891 OP2(SLJIT_AND | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, TMP2, 0);
4892 add_jump(compiler, &found, JUMP(SLJIT_NOT_ZERO));
4893 }
4894
4895 add_jump(compiler, backtracks, JUMP(SLJIT_JUMP));
4896 JUMPHERE(jump);
4897
4898 cc += 32 / sizeof(pcre_uchar);
4899 }
4900 else
4901 {
4902 OP2(SLJIT_SUB, TMP2, 0, TMP1, 0, SLJIT_IMM, min);
4903 add_jump(compiler, (cc[-1] & XCL_NOT) == 0 ? backtracks : &found, CMP(SLJIT_GREATER, TMP2, 0, SLJIT_IMM, max - min));
4904 }
4905 }
4906 else if ((cc[-1] & XCL_MAP) != 0)
4907 {
4908 OP1(SLJIT_MOV, TMP3, 0, TMP1, 0);
4909 #ifdef SUPPORT_UCP
4910 charsaved = TRUE;
4911 #endif
4912 if (!check_class_ranges(common, (const pcre_uint8 *)cc, FALSE, TRUE, list))
4913 {
4914 #ifdef COMPILE_PCRE8
4915 SLJIT_ASSERT(common->utf);
4916 #endif
4917 jump = CMP(SLJIT_GREATER, TMP1, 0, SLJIT_IMM, 255);
4918
4919 OP2(SLJIT_AND, TMP2, 0, TMP1, 0, SLJIT_IMM, 0x7);
4920 OP2(SLJIT_LSHR, TMP1, 0, TMP1, 0, SLJIT_IMM, 3);
4921 OP1(SLJIT_MOV_UB, TMP1, 0, SLJIT_MEM1(TMP1), (sljit_sw)cc);
4922 OP2(SLJIT_SHL, TMP2, 0, SLJIT_IMM, 1, TMP2, 0);
4923 OP2(SLJIT_AND | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, TMP2, 0);
4924 add_jump(compiler, list, JUMP(SLJIT_NOT_ZERO));
4925
4926 JUMPHERE(jump);
4927 }
4928
4929 OP1(SLJIT_MOV, TMP1, 0, TMP3, 0);
4930 cc += 32 / sizeof(pcre_uchar);
4931 }
4932
4933 #ifdef SUPPORT_UCP
4934 /* Simple register allocation. TMP1 is preferred if possible. */
4935 if (needstype || needsscript)
4936 {
4937 if (needschar && !charsaved)
4938 OP1(SLJIT_MOV, TMP3, 0, TMP1, 0);
4939 add_jump(compiler, &common->getucd, JUMP(SLJIT_FAST_CALL));
4940 if (needschar)
4941 {
4942 if (needstype)
4943 {
4944 OP1(SLJIT_MOV, RETURN_ADDR, 0, TMP1, 0);
4945 typereg = RETURN_ADDR;
4946 }
4947
4948 if (needsscript)
4949 scriptreg = TMP3;
4950 OP1(SLJIT_MOV, TMP1, 0, TMP3, 0);
4951 }
4952 else if (needstype && needsscript)
4953 scriptreg = TMP3;
4954 /* In all other cases only one of them was specified, and that can goes to TMP1. */
4955
4956 if (needsscript)
4957 {
4958 if (scriptreg == TMP1)
4959 {
4960 OP1(SLJIT_MOV, scriptreg, 0, SLJIT_IMM, (sljit_sw)PRIV(ucd_records) + SLJIT_OFFSETOF(ucd_record, script));
4961 OP1(SLJIT_MOV_UB, scriptreg, 0, SLJIT_MEM2(scriptreg, TMP2), 3);
4962 }
4963 else
4964 {
4965 OP2(SLJIT_SHL, TMP2, 0, TMP2, 0, SLJIT_IMM, 3);
4966 OP2(SLJIT_ADD, TMP2, 0, TMP2, 0, SLJIT_IMM, (sljit_sw)PRIV(ucd_records) + SLJIT_OFFSETOF(ucd_record, script));
4967 OP1(SLJIT_MOV_UB, scriptreg, 0, SLJIT_MEM1(TMP2), 0);
4968 }
4969 }
4970 }
4971 #endif
4972
4973 /* Generating code. */
4974 charoffset = 0;
4975 numberofcmps = 0;
4976 #ifdef SUPPORT_UCP
4977 typeoffset = 0;
4978 #endif
4979
4980 while (*cc != XCL_END)
4981 {
4982 compares--;
4983 invertcmp = (compares == 0 && list != backtracks);
4984 jump = NULL;
4985
4986 if (*cc == XCL_SINGLE)
4987 {
4988 cc ++;
4989 GETCHARINCTEST(c, cc);
4990
4991 if (numberofcmps < 3 && (*cc == XCL_SINGLE || *cc == XCL_RANGE))
4992 {
4993 OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, (sljit_sw)(c - charoffset));
4994 OP_FLAGS(numberofcmps == 0 ? SLJIT_MOV : SLJIT_OR, TMP2, 0, numberofcmps == 0 ? SLJIT_UNUSED : TMP2, 0, SLJIT_EQUAL);
4995 numberofcmps++;
4996 }
4997 else if (numberofcmps > 0)
4998 {
4999 OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, (sljit_sw)(c - charoffset));
5000 OP_FLAGS(SLJIT_OR | SLJIT_SET_E, TMP2, 0, TMP2, 0, SLJIT_EQUAL);
5001 jump = JUMP(SLJIT_NOT_ZERO ^ invertcmp);
5002 numberofcmps = 0;
5003 }
5004 else
5005 {
5006 jump = CMP(SLJIT_EQUAL ^ invertcmp, TMP1, 0, SLJIT_IMM, (sljit_sw)(c - charoffset));
5007 numberofcmps = 0;
5008 }
5009 }
5010 else if (*cc == XCL_RANGE)
5011 {
5012 cc ++;
5013 GETCHARINCTEST(c, cc);
5014 SET_CHAR_OFFSET(c);
5015 GETCHARINCTEST(c, cc);
5016
5017 if (numberofcmps < 3 && (*cc == XCL_SINGLE || *cc == XCL_RANGE))
5018 {
5019 OP2(SLJIT_SUB | SLJIT_SET_U, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, (sljit_sw)(c - charoffset));
5020 OP_FLAGS(numberofcmps == 0 ? SLJIT_MOV : SLJIT_OR, TMP2, 0, numberofcmps == 0 ? SLJIT_UNUSED : TMP2, 0, SLJIT_LESS_EQUAL);
5021 numberofcmps++;
5022 }
5023 else if (numberofcmps > 0)
5024 {
5025 OP2(SLJIT_SUB | SLJIT_SET_U, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, (sljit_sw)(c - charoffset));
5026 OP_FLAGS(SLJIT_OR | SLJIT_SET_E, TMP2, 0, TMP2, 0, SLJIT_LESS_EQUAL);
5027 jump = JUMP(SLJIT_NOT_ZERO ^ invertcmp);
5028 numberofcmps = 0;
5029 }
5030 else
5031 {
5032 jump = CMP(SLJIT_LESS_EQUAL ^ invertcmp, TMP1, 0, SLJIT_IMM, (sljit_sw)(c - charoffset));
5033 numberofcmps = 0;
5034 }
5035 }
5036 #ifdef SUPPORT_UCP
5037 else
5038 {
5039 if (*cc == XCL_NOTPROP)
5040 invertcmp ^= 0x1;
5041 cc++;
5042 switch(*cc)
5043 {
5044 case PT_ANY:
5045 if (list != backtracks)
5046 {
5047 if ((cc[-1] == XCL_NOTPROP && compares > 0) || (cc[-1] == XCL_PROP && compares == 0))
5048 continue;
5049 }
5050 else if (cc[-1] == XCL_NOTPROP)
5051 continue;
5052 jump = JUMP(SLJIT_JUMP);
5053 break;
5054
5055 case PT_LAMP:
5056 OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, typereg, 0, SLJIT_IMM, ucp_Lu - typeoffset);
5057 OP_FLAGS(SLJIT_MOV, TMP2, 0, SLJIT_UNUSED, 0, SLJIT_EQUAL);
5058 OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, typereg, 0, SLJIT_IMM, ucp_Ll - typeoffset);
5059 OP_FLAGS(SLJIT_OR, TMP2, 0, TMP2, 0, SLJIT_EQUAL);
5060 OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, typereg, 0, SLJIT_IMM, ucp_Lt - typeoffset);
5061 OP_FLAGS(SLJIT_OR | SLJIT_SET_E, TMP2, 0, TMP2, 0, SLJIT_EQUAL);
5062 jump = JUMP(SLJIT_NOT_ZERO ^ invertcmp);
5063 break;
5064
5065 case PT_GC:
5066 c = PRIV(ucp_typerange)[(int)cc[1] * 2];
5067 SET_TYPE_OFFSET(c);
5068 jump = CMP(SLJIT_LESS_EQUAL ^ invertcmp, typereg, 0, SLJIT_IMM, PRIV(ucp_typerange)[(int)cc[1] * 2 + 1] - c);
5069 break;
5070
5071 case PT_PC:
5072 jump = CMP(SLJIT_EQUAL ^ invertcmp, typereg, 0, SLJIT_IMM, (int)cc[1] - typeoffset);
5073 break;
5074
5075 case PT_SC:
5076 jump = CMP(SLJIT_EQUAL ^ invertcmp, scriptreg, 0, SLJIT_IMM, (int)cc[1]);
5077 break;
5078
5079 case PT_SPACE:
5080 case PT_PXSPACE:
5081 SET_CHAR_OFFSET(9);
5082 OP2(SLJIT_SUB | SLJIT_SET_U, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0xd - 0x9);
5083 OP_FLAGS(SLJIT_MOV, TMP2, 0, SLJIT_UNUSED, 0, SLJIT_LESS_EQUAL);
5084
5085 OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x85 - 0x9);
5086 OP_FLAGS(SLJIT_OR | SLJIT_SET_E, TMP2, 0, TMP2, 0, SLJIT_EQUAL);
5087
5088 OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x180e - 0x9);
5089 OP_FLAGS(SLJIT_OR | SLJIT_SET_E, TMP2, 0, TMP2, 0, SLJIT_EQUAL);
5090
5091 SET_TYPE_OFFSET(ucp_Zl);
5092 OP2(SLJIT_SUB | SLJIT_SET_U, SLJIT_UNUSED, 0, typereg, 0, SLJIT_IMM, ucp_Zs - ucp_Zl);
5093 OP_FLAGS(SLJIT_OR | SLJIT_SET_E, TMP2, 0, TMP2, 0, SLJIT_LESS_EQUAL);
5094 jump = JUMP(SLJIT_NOT_ZERO ^ invertcmp);
5095 break;
5096
5097 case PT_WORD:
5098 OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, (sljit_sw)(CHAR_UNDERSCORE - charoffset));
5099 OP_FLAGS(SLJIT_MOV, TMP2, 0, SLJIT_UNUSED, 0, SLJIT_EQUAL);
5100 /* Fall through. */
5101
5102 case PT_ALNUM:
5103 SET_TYPE_OFFSET(ucp_Ll);
5104 OP2(SLJIT_SUB | SLJIT_SET_U, SLJIT_UNUSED, 0, typereg, 0, SLJIT_IMM, ucp_Lu - ucp_Ll);
5105 OP_FLAGS((*cc == PT_ALNUM) ? SLJIT_MOV : SLJIT_OR, TMP2, 0, (*cc == PT_ALNUM) ? SLJIT_UNUSED : TMP2, 0, SLJIT_LESS_EQUAL);
5106 SET_TYPE_OFFSET(ucp_Nd);
5107 OP2(SLJIT_SUB | SLJIT_SET_U, SLJIT_UNUSED, 0, typereg, 0, SLJIT_IMM, ucp_No - ucp_Nd);
5108 OP_FLAGS(SLJIT_OR | SLJIT_SET_E, TMP2, 0, TMP2, 0, SLJIT_LESS_EQUAL);
5109 jump = JUMP(SLJIT_NOT_ZERO ^ invertcmp);
5110 break;
5111
5112 case PT_CLIST:
5113 other_cases = PRIV(ucd_caseless_sets) + cc[1];
5114
5115 /* At least three characters are required.
5116 Otherwise this case would be handled by the normal code path. */
5117 SLJIT_ASSERT(other_cases[0] != NOTACHAR && other_cases[1] != NOTACHAR && other_cases[2] != NOTACHAR);
5118 SLJIT_ASSERT(other_cases[0] < other_cases[1] && other_cases[1] < other_cases[2]);
5119
5120 /* Optimizing character pairs, if their difference is power of 2. */
5121 if (is_powerof2(other_cases[1] ^ other_cases[0]))
5122 {
5123 if (charoffset == 0)
5124 OP2(SLJIT_OR, TMP2, 0, TMP1, 0, SLJIT_IMM, other_cases[1] ^ other_cases[0]);
5125 else
5126 {
5127 OP2(SLJIT_ADD, TMP2, 0, TMP1, 0, SLJIT_IMM, (sljit_sw)charoffset);
5128 OP2(SLJIT_OR, TMP2, 0, TMP2, 0, SLJIT_IMM, other_cases[1] ^ other_cases[0]);
5129 }
5130 OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP2, 0, SLJIT_IMM, other_cases[1]);
5131 OP_FLAGS(SLJIT_MOV, TMP2, 0, SLJIT_UNUSED, 0, SLJIT_EQUAL);
5132 other_cases += 2;
5133 }
5134 else if (is_powerof2(other_cases[2] ^ other_cases[1]))
5135 {
5136 if (charoffset == 0)
5137 OP2(SLJIT_OR, TMP2, 0, TMP1, 0, SLJIT_IMM, other_cases[2] ^ other_cases[1]);
5138 else
5139 {
5140 OP2(SLJIT_ADD, TMP2, 0, TMP1, 0, SLJIT_IMM, (sljit_sw)charoffset);
5141 OP2(SLJIT_OR, TMP2, 0, TMP2, 0, SLJIT_IMM, other_cases[1] ^ other_cases[0]);
5142 }
5143 OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP2, 0, SLJIT_IMM, other_cases[2]);
5144 OP_FLAGS(SLJIT_MOV, TMP2, 0, SLJIT_UNUSED, 0, SLJIT_EQUAL);
5145
5146 OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, (sljit_sw)(other_cases[0] - charoffset));
5147 OP_FLAGS(SLJIT_OR | ((other_cases[3] == NOTACHAR) ? SLJIT_SET_E : 0), TMP2, 0, TMP2, 0, SLJIT_EQUAL);
5148
5149 other_cases += 3;
5150 }
5151 else
5152 {
5153 OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, (sljit_sw)(*other_cases++ - charoffset));
5154 OP_FLAGS(SLJIT_MOV, TMP2, 0, SLJIT_UNUSED, 0, SLJIT_EQUAL);
5155 }
5156
5157 while (*other_cases != NOTACHAR)
5158 {
5159 OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, (sljit_sw)(*other_cases++ - charoffset));
5160 OP_FLAGS(SLJIT_OR | ((*other_cases == NOTACHAR) ? SLJIT_SET_E : 0), TMP2, 0, TMP2, 0, SLJIT_EQUAL);
5161 }
5162 jump = JUMP(SLJIT_NOT_ZERO ^ invertcmp);
5163 break;
5164
5165 case PT_UCNC:
5166 OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, (sljit_sw)(CHAR_DOLLAR_SIGN - charoffset));
5167 OP_FLAGS(SLJIT_MOV, TMP2, 0, SLJIT_UNUSED, 0, SLJIT_EQUAL);
5168 OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, (sljit_sw)(CHAR_COMMERCIAL_AT - charoffset));
5169 OP_FLAGS(SLJIT_OR, TMP2, 0, TMP2, 0, SLJIT_EQUAL);
5170 OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, (sljit_sw)(CHAR_GRAVE_ACCENT - charoffset));
5171 OP_FLAGS(SLJIT_OR, TMP2, 0, TMP2, 0, SLJIT_EQUAL);
5172
5173 SET_CHAR_OFFSET(0xa0);
5174 OP2(SLJIT_SUB | SLJIT_SET_U, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, (sljit_sw)(0xd7ff - charoffset));
5175 OP_FLAGS(SLJIT_OR, TMP2, 0, TMP2, 0, SLJIT_LESS_EQUAL);
5176 SET_CHAR_OFFSET(0);
5177 OP2(SLJIT_SUB | SLJIT_SET_U, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0xe000 - 0);
5178 OP_FLAGS(SLJIT_OR | SLJIT_SET_E, TMP2, 0, TMP2, 0, SLJIT_GREATER_EQUAL);
5179 jump = JUMP(SLJIT_NOT_ZERO ^ invertcmp);
5180 break;
5181
5182 case PT_PXGRAPH:
5183 /* C and Z groups are the farthest two groups. */
5184 SET_TYPE_OFFSET(ucp_Ll);
5185 OP2(SLJIT_SUB | SLJIT_SET_U, SLJIT_UNUSED, 0, typereg, 0, SLJIT_IMM, ucp_So - ucp_Ll);
5186 OP_FLAGS(SLJIT_MOV, TMP2, 0, SLJIT_UNUSED, 0, SLJIT_GREATER);
5187
5188 jump = CMP(SLJIT_NOT_EQUAL, typereg, 0, SLJIT_IMM, ucp_Cf - ucp_Ll);
5189
5190 /* In case of ucp_Cf, we overwrite the result. */
5191 SET_CHAR_OFFSET(0x2066);
5192 OP2(SLJIT_SUB | SLJIT_SET_U, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x2069 - 0x2066);
5193 OP_FLAGS(SLJIT_MOV, TMP2, 0, SLJIT_UNUSED, 0, SLJIT_LESS_EQUAL);
5194
5195 OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x061c - 0x2066);
5196 OP_FLAGS(SLJIT_OR, TMP2, 0, TMP2, 0, SLJIT_EQUAL);
5197
5198 OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x180e - 0x2066);
5199 OP_FLAGS(SLJIT_OR, TMP2, 0, TMP2, 0, SLJIT_EQUAL);
5200
5201 JUMPHERE(jump);
5202 jump = CMP(SLJIT_ZERO ^ invertcmp, TMP2, 0, SLJIT_IMM, 0);
5203 break;
5204
5205 case PT_PXPRINT:
5206 /* C and Z groups are the farthest two groups. */
5207 SET_TYPE_OFFSET(ucp_Ll);
5208 OP2(SLJIT_SUB | SLJIT_SET_U, SLJIT_UNUSED, 0, typereg, 0, SLJIT_IMM, ucp_So - ucp_Ll);
5209 OP_FLAGS(SLJIT_MOV, TMP2, 0, SLJIT_UNUSED, 0, SLJIT_GREATER);
5210
5211 OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, typereg, 0, SLJIT_IMM, ucp_Zs - ucp_Ll);
5212 OP_FLAGS(SLJIT_AND, TMP2, 0, TMP2, 0, SLJIT_NOT_EQUAL);
5213
5214 jump = CMP(SLJIT_NOT_EQUAL, typereg, 0, SLJIT_IMM, ucp_Cf - ucp_Ll);
5215
5216 /* In case of ucp_Cf, we overwrite the result. */
5217 SET_CHAR_OFFSET(0x2066);
5218 OP2(SLJIT_SUB | SLJIT_SET_U, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x2069 - 0x2066);
5219 OP_FLAGS(SLJIT_MOV, TMP2, 0, SLJIT_UNUSED, 0, SLJIT_LESS_EQUAL);
5220
5221 OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x061c - 0x2066);
5222 OP_FLAGS(SLJIT_OR, TMP2, 0, TMP2, 0, SLJIT_EQUAL);
5223
5224 JUMPHERE(jump);
5225 jump = CMP(SLJIT_ZERO ^ invertcmp, TMP2, 0, SLJIT_IMM, 0);
5226 break;
5227
5228 case PT_PXPUNCT:
5229 SET_TYPE_OFFSET(ucp_Sc);
5230 OP2(SLJIT_SUB | SLJIT_SET_U, SLJIT_UNUSED, 0, typereg, 0, SLJIT_IMM, ucp_So - ucp_Sc);
5231 OP_FLAGS(SLJIT_MOV, TMP2, 0, SLJIT_UNUSED, 0, SLJIT_LESS_EQUAL);
5232
5233 SET_CHAR_OFFSET(0);
5234 OP2(SLJIT_SUB | SLJIT_SET_U, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0xff);
5235 OP_FLAGS(SLJIT_AND, TMP2, 0, TMP2, 0, SLJIT_LESS_EQUAL);
5236
5237 SET_TYPE_OFFSET(ucp_Pc);
5238 OP2(SLJIT_SUB | SLJIT_SET_U, SLJIT_UNUSED, 0, typereg, 0, SLJIT_IMM, ucp_Ps - ucp_Pc);
5239 OP_FLAGS(SLJIT_OR | SLJIT_SET_E, TMP2, 0, TMP2, 0, SLJIT_LESS_EQUAL);
5240 jump = JUMP(SLJIT_NOT_ZERO ^ invertcmp);
5241 break;
5242 }
5243 cc += 2;
5244 }
5245 #endif
5246
5247 if (jump != NULL)
5248 add_jump(compiler, compares > 0 ? list : backtracks, jump);
5249 }
5250
5251 if (found != NULL)
5252 set_jumps(found, LABEL());
5253 }
5254
5255 #undef SET_TYPE_OFFSET
5256 #undef SET_CHAR_OFFSET
5257
5258 #endif
5259
5260 static pcre_uchar *compile_char1_matchingpath(compiler_common *common, pcre_uchar type, pcre_uchar *cc, jump_list **backtracks)
5261 {
5262 DEFINE_COMPILER;
5263 int length;
5264 unsigned int c, oc, bit;
5265 compare_context context;
5266 struct sljit_jump *jump[4];
5267 jump_list *end_list;
5268 #ifdef SUPPORT_UTF
5269 struct sljit_label *label;
5270 #ifdef SUPPORT_UCP
5271 pcre_uchar propdata[5];
5272 #endif
5273 #endif /* SUPPORT_UTF */
5274
5275 switch(type)
5276 {
5277 case OP_SOD:
5278 OP1(SLJIT_MOV, TMP1, 0, ARGUMENTS, 0);
5279 OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(TMP1), SLJIT_OFFSETOF(jit_arguments, begin));
5280 add_jump(compiler, backtracks, CMP(SLJIT_NOT_EQUAL, STR_PTR, 0, TMP1, 0));
5281 return cc;
5282
5283 case OP_SOM:
5284 OP1(SLJIT_MOV, TMP1, 0, ARGUMENTS, 0);
5285 OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(TMP1), SLJIT_OFFSETOF(jit_arguments, str));
5286 add_jump(compiler, backtracks, CMP(SLJIT_NOT_EQUAL, STR_PTR, 0, TMP1, 0));
5287 return cc;
5288
5289 case OP_NOT_WORD_BOUNDARY:
5290 case OP_WORD_BOUNDARY:
5291 add_jump(compiler, &common->wordboundary, JUMP(SLJIT_FAST_CALL));
5292 add_jump(compiler, backtracks, JUMP(type == OP_NOT_WORD_BOUNDARY ? SLJIT_NOT_ZERO : SLJIT_ZERO));
5293 return cc;
5294
5295 case OP_NOT_DIGIT:
5296 case OP_DIGIT:
5297 /* Digits are usually 0-9, so it is worth to optimize them. */
5298 detect_partial_match(common, backtracks);
5299 #if defined SUPPORT_UTF && defined COMPILE_PCRE8
5300 if (common->utf && is_char7_bitset((const pcre_uint8*)common->ctypes - cbit_length + cbit_digit, FALSE))
5301 read_char7_type(common, type == OP_NOT_DIGIT);
5302 else
5303 #endif
5304 read_char8_type(common, type == OP_NOT_DIGIT);
5305 /* Flip the starting bit in the negative case. */
5306 OP2(SLJIT_AND | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, ctype_digit);
5307 add_jump(compiler, backtracks, JUMP(type == OP_DIGIT ? SLJIT_ZERO : SLJIT_NOT_ZERO));
5308 return cc;
5309
5310 case OP_NOT_WHITESPACE:
5311 case OP_WHITESPACE:
5312 detect_partial_match(common, backtracks);
5313 #if defined SUPPORT_UTF && defined COMPILE_PCRE8
5314 if (common->utf && is_char7_bitset((const pcre_uint8*)common->ctypes - cbit_length + cbit_space, FALSE))
5315 read_char7_type(common, type == OP_NOT_WHITESPACE);
5316 else
5317 #endif
5318 read_char8_type(common, type == OP_NOT_WHITESPACE);
5319 OP2(SLJIT_AND | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, ctype_space);
5320 add_jump(compiler, backtracks, JUMP(type == OP_WHITESPACE ? SLJIT_ZERO : SLJIT_NOT_ZERO));
5321 return cc;
5322
5323 case OP_NOT_WORDCHAR:
5324 case OP_WORDCHAR:
5325 detect_partial_match(common, backtracks);
5326 #if defined SUPPORT_UTF && defined COMPILE_PCRE8
5327 if (common->utf && is_char7_bitset((const pcre_uint8*)common->ctypes - cbit_length + cbit_word, FALSE))
5328 read_char7_type(common, type == OP_NOT_WORDCHAR);
5329 else
5330 #endif
5331 read_char8_type(common, type == OP_NOT_WORDCHAR);
5332 OP2(SLJIT_AND | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, ctype_word);
5333 add_jump(compiler, backtracks, JUMP(type == OP_WORDCHAR ? SLJIT_ZERO : SLJIT_NOT_ZERO));
5334 return cc;
5335
5336 case OP_ANY:
5337 detect_partial_match(common, backtracks);
5338 read_char_range(common, common->nlmin, common->nlmax, TRUE);
5339 if (common->nltype == NLTYPE_FIXED && common->newline > 255)
5340 {
5341 jump[0] = CMP(SLJIT_NOT_EQUAL, TMP1, 0, SLJIT_IMM, (common->newline >> 8) & 0xff);
5342 end_list = NULL;
5343 if (common->mode != JIT_PARTIAL_HARD_COMPILE)
5344 add_jump(compiler, &end_list, CMP(SLJIT_GREATER_EQUAL, STR_PTR, 0, STR_END, 0));
5345 else
5346 check_str_end(common, &end_list);
5347
5348 OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), 0);
5349 add_jump(compiler, backtracks, CMP(SLJIT_EQUAL, TMP1, 0, SLJIT_IMM, common->newline & 0xff));
5350 set_jumps(end_list, LABEL());
5351 JUMPHERE(jump[0]);
5352 }
5353 else
5354 check_newlinechar(common, common->nltype, backtracks, TRUE);
5355 return cc;
5356
5357 case OP_ALLANY:
5358 detect_partial_match(common, backtracks);
5359 #ifdef SUPPORT_UTF
5360 if (common->utf)
5361 {
5362 OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), 0);
5363 OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
5364 #if defined COMPILE_PCRE8 || defined COMPILE_PCRE16
5365 #if defined COMPILE_PCRE8
5366 jump[0] = CMP(SLJIT_LESS, TMP1, 0, SLJIT_IMM, 0xc0);
5367 OP1(SLJIT_MOV_UB, TMP1, 0, SLJIT_MEM1(TMP1), (sljit_sw)PRIV(utf8_table4) - 0xc0);
5368 OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, TMP1, 0);
5369 #elif defined COMPILE_PCRE16
5370 jump[0] = CMP(SLJIT_LESS, TMP1, 0, SLJIT_IMM, 0xd800);
5371 OP2(SLJIT_AND, TMP1, 0, TMP1, 0, SLJIT_IMM, 0xfc00);
5372 OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0xd800);
5373 OP_FLAGS(SLJIT_MOV, TMP1, 0, SLJIT_UNUSED, 0, SLJIT_EQUAL);
5374 OP2(SLJIT_SHL, TMP1, 0, TMP1, 0, SLJIT_IMM, 1);
5375 OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, TMP1, 0);
5376 #endif
5377 JUMPHERE(jump[0]);
5378 #endif /* COMPILE_PCRE[8|16] */
5379 return cc;
5380 }
5381 #endif
5382 OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
5383 return cc;
5384
5385 case OP_ANYBYTE:
5386 detect_partial_match(common, backtracks);
5387 OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
5388 return cc;
5389
5390 #ifdef SUPPORT_UTF
5391 #ifdef SUPPORT_UCP
5392 case OP_NOTPROP:
5393 case OP_PROP:
5394 propdata[0] = XCL_HASPROP;
5395 propdata[1] = type == OP_NOTPROP ? XCL_NOTPROP : XCL_PROP;
5396 propdata[2] = cc[0];
5397 propdata[3] = cc[1];
5398 propdata[4] = XCL_END;
5399 compile_xclass_matchingpath(common, propdata, backtracks);
5400 return cc + 2;
5401 #endif
5402 #endif
5403
5404 case OP_ANYNL:
5405 detect_partial_match(common, backtracks);
5406 read_char_range(common, common->bsr_nlmin, common->bsr_nlmax, FALSE);
5407 jump[0] = CMP(SLJIT_NOT_EQUAL, TMP1, 0, SLJIT_IMM, CHAR_CR);
5408 /* We don't need to handle soft partial matching case. */
5409 end_list = NULL;
5410 if (common->mode != JIT_PARTIAL_HARD_COMPILE)
5411 add_jump(compiler, &end_list, CMP(SLJIT_GREATER_EQUAL, STR_PTR, 0, STR_END, 0));
5412 else
5413 check_str_end(common, &end_list);
5414 OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), 0);
5415 jump[1] = CMP(SLJIT_NOT_EQUAL, TMP1, 0, SLJIT_IMM, CHAR_NL);
5416 OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
5417 jump[2] = JUMP(SLJIT_JUMP);
5418 JUMPHERE(jump[0]);
5419 check_newlinechar(common, common->bsr_nltype, backtracks, FALSE);
5420 set_jumps(end_list, LABEL());
5421 JUMPHERE(jump[1]);
5422 JUMPHERE(jump[2]);
5423 return cc;
5424
5425 case OP_NOT_HSPACE:
5426 case OP_HSPACE:
5427 detect_partial_match(common, backtracks);
5428 read_char_range(common, 0x9, 0x3000, type == OP_NOT_HSPACE);
5429 add_jump(compiler, &common->hspace, JUMP(SLJIT_FAST_CALL));
5430 add_jump(compiler, backtracks, JUMP(type == OP_NOT_HSPACE ? SLJIT_NOT_ZERO : SLJIT_ZERO));
5431 return cc;
5432
5433 case OP_NOT_VSPACE:
5434 case OP_VSPACE:
5435 detect_partial_match(common, backtracks);
5436 read_char_range(common, 0xa, 0x2029, type == OP_NOT_VSPACE);
5437 add_jump(compiler, &common->vspace, JUMP(SLJIT_FAST_CALL));
5438 add_jump(compiler, backtracks, JUMP(type == OP_NOT_VSPACE ? SLJIT_NOT_ZERO : SLJIT_ZERO));
5439 return cc;
5440
5441 #ifdef SUPPORT_UCP
5442 case OP_EXTUNI:
5443 detect_partial_match(common, backtracks);
5444 read_char(common);
5445 add_jump(compiler, &common->getucd, JUMP(SLJIT_FAST_CALL));
5446 OP1(SLJIT_MOV, TMP1, 0, SLJIT_IMM, (sljit_sw)PRIV(ucd_records) + SLJIT_OFFSETOF(ucd_record, gbprop));
5447 /* Optimize register allocation: use a real register. */
5448 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), LOCALS0, STACK_TOP, 0);
5449 OP1(SLJIT_MOV_UB, STACK_TOP, 0, SLJIT_MEM2(TMP1, TMP2), 3);
5450
5451 label = LABEL();
5452 jump[0] = CMP(SLJIT_GREATER_EQUAL, STR_PTR, 0, STR_END, 0);
5453 OP1(SLJIT_MOV, TMP3, 0, STR_PTR, 0);
5454 read_char(common);
5455 add_jump(compiler, &common->getucd, JUMP(SLJIT_FAST_CALL));
5456 OP1(SLJIT_MOV, TMP1, 0, SLJIT_IMM, (sljit_sw)PRIV(ucd_records) + SLJIT_OFFSETOF(ucd_record, gbprop));
5457 OP1(SLJIT_MOV_UB, TMP2, 0, SLJIT_MEM2(TMP1, TMP2), 3);
5458
5459 OP2(SLJIT_SHL, STACK_TOP, 0, STACK_TOP, 0, SLJIT_IMM, 2);
5460 OP1(SLJIT_MOV_UI, TMP1, 0, SLJIT_MEM1(STACK_TOP), (sljit_sw)PRIV(ucp_gbtable));
5461 OP1(SLJIT_MOV, STACK_TOP, 0, TMP2, 0);
5462 OP2(SLJIT_SHL, TMP2, 0, SLJIT_IMM, 1, TMP2, 0);
5463 OP2(SLJIT_AND | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, TMP2, 0);
5464 JUMPTO(SLJIT_NOT_ZERO, label);
5465
5466 OP1(SLJIT_MOV, STR_PTR, 0, TMP3, 0);
5467 JUMPHERE(jump[0]);
5468 OP1(SLJIT_MOV, STACK_TOP, 0, SLJIT_MEM1(SLJIT_SP), LOCALS0);
5469
5470 if (common->mode == JIT_PARTIAL_HARD_COMPILE)
5471 {
5472 jump[0] = CMP(SLJIT_LESS, STR_PTR, 0, STR_END, 0);
5473 /* Since we successfully read a char above, partial matching must occure. */
5474 check_partial(common, TRUE);
5475 JUMPHERE(jump[0]);
5476 }
5477 return cc;
5478 #endif
5479
5480 case OP_EODN:
5481 /* Requires rather complex checks. */
5482 jump[0] = CMP(SLJIT_GREATER_EQUAL, STR_PTR, 0, STR_END, 0);
5483 if (common->nltype == NLTYPE_FIXED && common->newline > 255)
5484 {
5485 OP2(SLJIT_ADD, TMP2, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(2));
5486 OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(0));
5487 if (common->mode == JIT_COMPILE)
5488 add_jump(compiler, backtracks, CMP(SLJIT_NOT_EQUAL, TMP2, 0, STR_END, 0));
5489 else
5490 {
5491 jump[1] = CMP(SLJIT_EQUAL, TMP2, 0, STR_END, 0);
5492 OP2(SLJIT_SUB | SLJIT_SET_U, SLJIT_UNUSED, 0, TMP2, 0, STR_END, 0);
5493 OP_FLAGS(SLJIT_MOV, TMP2, 0, SLJIT_UNUSED, 0, SLJIT_LESS);
5494 OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, (common->newline >> 8) & 0xff);
5495 OP_FLAGS(SLJIT_OR | SLJIT_SET_E, TMP2, 0, TMP2, 0, SLJIT_NOT_EQUAL);
5496 add_jump(compiler, backtracks, JUMP(SLJIT_NOT_EQUAL));
5497 check_partial(common, TRUE);
5498 add_jump(compiler, backtracks, JUMP(SLJIT_JUMP));
5499 JUMPHERE(jump[1]);
5500 }
5501 OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(1));
5502 add_jump(compiler, backtracks, CMP(SLJIT_NOT_EQUAL, TMP1, 0, SLJIT_IMM, (common->newline >> 8) & 0xff));
5503 add_jump(compiler, backtracks, CMP(SLJIT_NOT_EQUAL, TMP2, 0, SLJIT_IMM, common->newline & 0xff));
5504 }
5505 else if (common->nltype == NLTYPE_FIXED)
5506 {
5507 OP2(SLJIT_ADD, TMP2, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
5508 OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(0));
5509 add_jump(compiler, backtracks, CMP(SLJIT_NOT_EQUAL, TMP2, 0, STR_END, 0));
5510 add_jump(compiler, backtracks, CMP(SLJIT_NOT_EQUAL, TMP1, 0, SLJIT_IMM, common->newline));
5511 }
5512 else
5513 {
5514 OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(0));
5515 jump[1] = CMP(SLJIT_NOT_EQUAL, TMP1, 0, SLJIT_IMM, CHAR_CR);
5516 OP2(SLJIT_ADD, TMP2, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(2));
5517 OP2(SLJIT_SUB | SLJIT_SET_U, SLJIT_UNUSED, 0, TMP2, 0, STR_END, 0);
5518 jump[2] = JUMP(SLJIT_GREATER);
5519 add_jump(compiler, backtracks, JUMP(SLJIT_LESS));
5520 /* Equal. */
5521 OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(1));
5522 jump[3] = CMP(SLJIT_EQUAL, TMP1, 0, SLJIT_IMM, CHAR_NL);
5523 add_jump(compiler, backtracks, JUMP(SLJIT_JUMP));
5524
5525 JUMPHERE(jump[1]);
5526 if (common->nltype == NLTYPE_ANYCRLF)
5527 {
5528 OP2(SLJIT_ADD, TMP2, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
5529 add_jump(compiler, backtracks, CMP(SLJIT_LESS, TMP2, 0, STR_END, 0));
5530 add_jump(compiler, backtracks, CMP(SLJIT_NOT_EQUAL, TMP1, 0, SLJIT_IMM, CHAR_NL));
5531 }
5532 else
5533 {
5534 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), LOCALS1, STR_PTR, 0);
5535 read_char_range(common, common->nlmin, common->nlmax, TRUE);
5536 add_jump(compiler, backtracks, CMP(SLJIT_NOT_EQUAL, STR_PTR, 0, STR_END, 0));
5537 add_jump(compiler, &common->anynewline, JUMP(SLJIT_FAST_CALL));
5538 add_jump(compiler, backtracks, JUMP(SLJIT_ZERO));
5539 OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(SLJIT_SP), LOCALS1);
5540 }
5541 JUMPHERE(jump[2]);
5542 JUMPHERE(jump[3]);
5543 }
5544 JUMPHERE(jump[0]);
5545 check_partial(common, FALSE);
5546 return cc;
5547
5548 case OP_EOD:
5549 add_jump(compiler, backtracks, CMP(SLJIT_LESS, STR_PTR, 0, STR_END, 0));
5550 check_partial(common, FALSE);
5551 return cc;
5552
5553 case OP_CIRC:
5554 OP1(SLJIT_MOV, TMP2, 0, ARGUMENTS, 0);
5555 OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(TMP2), SLJIT_OFFSETOF(jit_arguments, begin));
5556 add_jump(compiler, backtracks, CMP(SLJIT_GREATER, STR_PTR, 0, TMP1, 0));
5557 OP1(SLJIT_MOV_UB, TMP2, 0, SLJIT_MEM1(TMP2), SLJIT_OFFSETOF(jit_arguments, notbol));
5558 add_jump(compiler, backtracks, CMP(SLJIT_NOT_EQUAL, TMP2, 0, SLJIT_IMM, 0));
5559 return cc;
5560
5561 case OP_CIRCM:
5562 OP1(SLJIT_MOV, TMP2, 0, ARGUMENTS, 0);
5563 OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(TMP2), SLJIT_OFFSETOF(jit_arguments, begin));
5564 jump[1] = CMP(SLJIT_GREATER, STR_PTR, 0, TMP1, 0);
5565 OP1(SLJIT_MOV_UB, TMP2, 0, SLJIT_MEM1(TMP2), SLJIT_OFFSETOF(jit_arguments, notbol));
5566 add_jump(compiler, backtracks, CMP(SLJIT_NOT_EQUAL, TMP2, 0, SLJIT_IMM, 0));
5567 jump[0] = JUMP(SLJIT_JUMP);
5568 JUMPHERE(jump[1]);
5569
5570 add_jump(compiler, backtracks, CMP(SLJIT_GREATER_EQUAL, STR_PTR, 0, STR_END, 0));
5571 if (common->nltype == NLTYPE_FIXED && common->newline > 255)
5572 {
5573 OP2(SLJIT_SUB, TMP2, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(2));
5574 add_jump(compiler, backtracks, CMP(SLJIT_LESS, TMP2, 0, TMP1, 0));
5575 OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(-2));
5576 OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(-1));
5577 add_jump(compiler, backtracks, CMP(SLJIT_NOT_EQUAL, TMP1, 0, SLJIT_IMM, (common->newline >> 8) & 0xff));
5578 add_jump(compiler, backtracks, CMP(SLJIT_NOT_EQUAL, TMP2, 0, SLJIT_IMM, common->newline & 0xff));
5579 }
5580 else
5581 {
5582 skip_char_back(common);
5583 read_char_range(common, common->nlmin, common->nlmax, TRUE);
5584 check_newlinechar(common, common->nltype, backtracks, FALSE);
5585 }
5586 JUMPHERE(jump[0]);
5587 return cc;
5588
5589 case OP_DOLL:
5590 OP1(SLJIT_MOV, TMP2, 0, ARGUMENTS, 0);
5591 OP1(SLJIT_MOV_UB, TMP2, 0, SLJIT_MEM1(TMP2), SLJIT_OFFSETOF(jit_arguments, noteol));
5592 add_jump(compiler, backtracks, CMP(SLJIT_NOT_EQUAL, TMP2, 0, SLJIT_IMM, 0));
5593
5594 if (!common->endonly)
5595 compile_char1_matchingpath(common, OP_EODN, cc, backtracks);
5596 else
5597 {
5598 add_jump(compiler, backtracks, CMP(SLJIT_LESS, STR_PTR, 0, STR_END, 0));
5599 check_partial(common, FALSE);
5600 }
5601 return cc;
5602
5603 case OP_DOLLM:
5604 jump[1] = CMP(SLJIT_LESS, STR_PTR, 0, STR_END, 0);
5605 OP1(SLJIT_MOV, TMP2, 0, ARGUMENTS, 0);
5606 OP1(SLJIT_MOV_UB, TMP2, 0, SLJIT_MEM1(TMP2), SLJIT_OFFSETOF(jit_arguments, noteol));
5607 add_jump(compiler, backtracks, CMP(SLJIT_NOT_EQUAL, TMP2, 0, SLJIT_IMM, 0));
5608 check_partial(common, FALSE);
5609 jump[0] = JUMP(SLJIT_JUMP);
5610 JUMPHERE(jump[1]);
5611
5612 if (common->nltype == NLTYPE_FIXED && common->newline > 255)
5613 {
5614 OP2(SLJIT_ADD, TMP2, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(2));
5615 OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(0));
5616 if (common->mode == JIT_COMPILE)
5617 add_jump(compiler, backtracks, CMP(SLJIT_GREATER, TMP2, 0, STR_END, 0));
5618 else
5619 {
5620 jump[1] = CMP(SLJIT_LESS_EQUAL, TMP2, 0, STR_END, 0);
5621 /* STR_PTR = STR_END - IN_UCHARS(1) */
5622 add_jump(compiler, backtracks, CMP(SLJIT_NOT_EQUAL, TMP1, 0, SLJIT_IMM, (common->newline >> 8) & 0xff));
5623 check_partial(common, TRUE);
5624 add_jump(compiler, backtracks, JUMP(SLJIT_JUMP));
5625 JUMPHERE(jump[1]);
5626 }
5627
5628 OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(1));
5629 add_jump(compiler, backtracks, CMP(SLJIT_NOT_EQUAL, TMP1, 0, SLJIT_IMM, (common->newline >> 8) & 0xff));
5630 add_jump(compiler, backtracks, CMP(SLJIT_NOT_EQUAL, TMP2, 0, SLJIT_IMM, common->newline & 0xff));
5631 }
5632 else
5633 {
5634 peek_char(common, common->nlmax);
5635 check_newlinechar(common, common->nltype, backtracks, FALSE);
5636 }
5637 JUMPHERE(jump[0]);
5638 return cc;
5639
5640 case OP_CHAR:
5641 case OP_CHARI:
5642 length = 1;
5643 #ifdef SUPPORT_UTF
5644 if (common->utf && HAS_EXTRALEN(*cc)) length += GET_EXTRALEN(*cc);
5645 #endif
5646 if (common->mode == JIT_COMPILE && (type == OP_CHAR || !char_has_othercase(common, cc) || char_get_othercase_bit(common, cc) != 0))
5647 {
5648 OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(length));
5649 add_jump(compiler, backtracks, CMP(SLJIT_GREATER, STR_PTR, 0, STR_END, 0));
5650
5651 context.length = IN_UCHARS(length);
5652 context.sourcereg = -1;
5653 #if defined SLJIT_UNALIGNED && SLJIT_UNALIGNED
5654 context.ucharptr = 0;
5655 #endif
5656 return byte_sequence_compare(common, type == OP_CHARI, cc, &context, backtracks);
5657 }
5658
5659 detect_partial_match(common, backtracks);
5660 #ifdef SUPPORT_UTF
5661 if (common->utf)
5662 {
5663 GETCHAR(c, cc);
5664 }
5665 else
5666 #endif
5667 c = *cc;
5668
5669 if (type == OP_CHAR || !char_has_othercase(common, cc))
5670 {
5671 read_char_range(common, c, c, FALSE);
5672 add_jump(compiler, backtracks, CMP(SLJIT_NOT_EQUAL, TMP1, 0, SLJIT_IMM, c));
5673 return cc + length;
5674 }
5675 oc = char_othercase(common, c);
5676 read_char_range(common, c < oc ? c : oc, c > oc ? c : oc, FALSE);
5677 bit = c ^ oc;
5678 if (is_powerof2(bit))
5679 {
5680 OP2(SLJIT_OR, TMP1, 0, TMP1, 0, SLJIT_IMM, bit);
5681 add_jump(compiler, backtracks, CMP(SLJIT_NOT_EQUAL, TMP1, 0, SLJIT_IMM, c | bit));
5682 return cc + length;
5683 }
5684 jump[0] = CMP(SLJIT_EQUAL, TMP1, 0, SLJIT_IMM, c);
5685 add_jump(compiler, backtracks, CMP(SLJIT_NOT_EQUAL, TMP1, 0, SLJIT_IMM, oc));
5686 JUMPHERE(jump[0]);
5687 return cc + length;
5688
5689 case OP_NOT:
5690 case OP_NOTI:
5691 detect_partial_match(common, backtracks);
5692 length = 1;
5693 #ifdef SUPPORT_UTF
5694 if (common->utf)
5695 {
5696 #ifdef COMPILE_PCRE8
5697 c = *cc;
5698 if (c < 128)
5699 {
5700 OP1(SLJIT_MOV_UB, TMP1, 0, SLJIT_MEM1(STR_PTR), 0);
5701 if (type == OP_NOT || !char_has_othercase(common, cc))
5702 add_jump(compiler, backtracks, CMP(SLJIT_EQUAL, TMP1, 0, SLJIT_IMM, c));
5703 else
5704 {
5705 /* Since UTF8 code page is fixed, we know that c is in [a-z] or [A-Z] range. */
5706 OP2(SLJIT_OR, TMP2, 0, TMP1, 0, SLJIT_IMM, 0x20);
5707 add_jump(compiler, backtracks, CMP(SLJIT_EQUAL, TMP2, 0, SLJIT_IMM, c | 0x20));
5708 }
5709 /* Skip the variable-length character. */
5710 OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
5711 jump[0] = CMP(SLJIT_LESS, TMP1, 0, SLJIT_IMM, 0xc0);
5712 OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(TMP1), (sljit_sw)PRIV(utf8_table4) - 0xc0);
5713 OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, TMP1, 0);
5714 JUMPHERE(jump[0]);
5715 return cc + 1;
5716 }
5717 else
5718 #endif /* COMPILE_PCRE8 */
5719 {
5720 GETCHARLEN(c, cc, length);
5721 }
5722 }
5723 else
5724 #endif /* SUPPORT_UTF */
5725 c = *cc;
5726
5727 if (type == OP_NOT || !char_has_othercase(common, cc))
5728 {
5729 read_char_range(common, c, c, TRUE);
5730 add_jump(compiler, backtracks, CMP(SLJIT_EQUAL, TMP1, 0, SLJIT_IMM, c));
5731 }
5732 else
5733 {
5734 oc = char_othercase(common, c);
5735 read_char_range(common, c < oc ? c : oc, c > oc ? c : oc, TRUE);
5736 bit = c ^ oc;
5737 if (is_powerof2(bit))
5738 {
5739 OP2(SLJIT_OR, TMP1, 0, TMP1, 0, SLJIT_IMM, bit);
5740 add_jump(compiler, backtracks, CMP(SLJIT_EQUAL, TMP1, 0, SLJIT_IMM, c | bit));
5741 }
5742 else
5743 {
5744 add_jump(compiler, backtracks, CMP(SLJIT_EQUAL, TMP1, 0, SLJIT_IMM, c));
5745 add_jump(compiler, backtracks, CMP(SLJIT_EQUAL, TMP1, 0, SLJIT_IMM, oc));
5746 }
5747 }
5748 return cc + length;
5749
5750 case OP_CLASS:
5751 case OP_NCLASS:
5752 detect_partial_match(common, backtracks);
5753
5754 #if defined SUPPORT_UTF && defined COMPILE_PCRE8
5755 bit = (common->utf && is_char7_bitset((const pcre_uint8 *)cc, type == OP_NCLASS)) ? 127 : 255;
5756 read_char_range(common, 0, bit, type == OP_NCLASS);
5757 #else
5758 read_char_range(common, 0, 255, type == OP_NCLASS);
5759 #endif
5760
5761 if (check_class_ranges(common, (const pcre_uint8 *)cc, type == OP_NCLASS, FALSE, backtracks))
5762 return cc + 32 / sizeof(pcre_uchar);
5763
5764 #if defined SUPPORT_UTF && defined COMPILE_PCRE8
5765 jump[0] = NULL;
5766 if (common->utf)
5767 {
5768 jump[0] = CMP(SLJIT_GREATER, TMP1, 0, SLJIT_IMM, bit);
5769 if (type == OP_CLASS)
5770 {
5771 add_jump(compiler, backtracks, jump[0]);
5772 jump[0] = NULL;
5773 }
5774 }
5775 #elif !defined COMPILE_PCRE8
5776 jump[0] = CMP(SLJIT_GREATER, TMP1, 0, SLJIT_IMM, 255);
5777 if (type == OP_CLASS)
5778 {
5779 add_jump(compiler, backtracks, jump[0]);
5780 jump[0] = NULL;
5781 }
5782 #endif /* SUPPORT_UTF && COMPILE_PCRE8 */
5783
5784 OP2(SLJIT_AND, TMP2, 0, TMP1, 0, SLJIT_IMM, 0x7);
5785 OP2(SLJIT_LSHR, TMP1, 0, TMP1, 0, SLJIT_IMM, 3);
5786 OP1(SLJIT_MOV_UB, TMP1, 0, SLJIT_MEM1(TMP1), (sljit_sw)cc);
5787 OP2(SLJIT_SHL, TMP2, 0, SLJIT_IMM, 1, TMP2, 0);
5788 OP2(SLJIT_AND | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, TMP2, 0);
5789 add_jump(compiler, backtracks, JUMP(SLJIT_ZERO));
5790
5791 #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
5792 if (jump[0] != NULL)
5793 JUMPHERE(jump[0]);
5794 #endif
5795
5796 return cc + 32 / sizeof(pcre_uchar);
5797
5798 #if defined SUPPORT_UTF || defined COMPILE_PCRE16 || defined COMPILE_PCRE32
5799 case OP_XCLASS:
5800 compile_xclass_matchingpath(common, cc + LINK_SIZE, backtracks);
5801 return cc + GET(cc, 0) - 1;
5802 #endif
5803
5804 case OP_REVERSE:
5805 length = GET(cc, 0);
5806 if (length == 0)
5807 return cc + LINK_SIZE;
5808 OP1(SLJIT_MOV, TMP1, 0, ARGUMENTS, 0);
5809 #ifdef SUPPORT_UTF
5810 if (common->utf)
5811 {
5812 OP1(SLJIT_MOV, TMP3, 0, SLJIT_MEM1(TMP1), SLJIT_OFFSETOF(jit_arguments, begin));
5813 OP1(SLJIT_MOV, TMP2, 0, SLJIT_IMM, length);
5814 label = LABEL();
5815 add_jump(compiler, backtracks, CMP(SLJIT_LESS_EQUAL, STR_PTR, 0, TMP3, 0));
5816 skip_char_back(common);
5817 OP2(SLJIT_SUB | SLJIT_SET_E, TMP2, 0, TMP2, 0, SLJIT_IMM, 1);
5818 JUMPTO(SLJIT_NOT_ZERO, label);
5819 }
5820 else
5821 #endif
5822 {
5823 OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(TMP1), SLJIT_OFFSETOF(jit_arguments, begin));
5824 OP2(SLJIT_SUB, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(length));
5825 add_jump(compiler, backtracks, CMP(SLJIT_LESS, STR_PTR, 0, TMP1, 0));
5826 }
5827 check_start_used_ptr(common);
5828 return cc + LINK_SIZE;
5829 }
5830 SLJIT_ASSERT_STOP();
5831 return cc;
5832 }
5833
5834 static SLJIT_INLINE pcre_uchar *compile_charn_matchingpath(compiler_common *common, pcre_uchar *cc, pcre_uchar *ccend, jump_list **backtracks)
5835 {
5836 /* This function consumes at least one input character. */
5837 /* To decrease the number of length checks, we try to concatenate the fixed length character sequences. */
5838 DEFINE_COMPILER;
5839 pcre_uchar *ccbegin = cc;
5840 compare_context context;
5841 int size;
5842
5843 context.length = 0;
5844 do
5845 {
5846 if (cc >= ccend)
5847 break;
5848
5849 if (*cc == OP_CHAR)
5850 {
5851 size = 1;
5852 #ifdef SUPPORT_UTF
5853 if (common->utf && HAS_EXTRALEN(cc[1]))
5854 size += GET_EXTRALEN(cc[1]);
5855 #endif
5856 }
5857 else if (*cc == OP_CHARI)
5858 {
5859 size = 1;
5860 #ifdef SUPPORT_UTF
5861 if (common->utf)
5862 {
5863 if (char_has_othercase(common, cc + 1) && char_get_othercase_bit(common, cc + 1) == 0)
5864 size = 0;
5865 else if (HAS_EXTRALEN(cc[1]))
5866 size += GET_EXTRALEN(cc[1]);
5867 }
5868 else
5869 #endif
5870 if (char_has_othercase(common, cc + 1) && char_get_othercase_bit(common, cc + 1) == 0)
5871 size = 0;
5872 }
5873 else
5874 size = 0;
5875
5876 cc += 1 + size;
5877 context.length += IN_UCHARS(size);
5878 }
5879 while (size > 0 && context.length <= 128);
5880
5881 cc = ccbegin;
5882 if (context.length > 0)
5883 {
5884 /* We have a fixed-length byte sequence. */
5885 OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, context.length);
5886 add_jump(compiler, backtracks, CMP(SLJIT_GREATER, STR_PTR, 0, STR_END, 0));
5887
5888 context.sourcereg = -1;
5889 #if defined SLJIT_UNALIGNED && SLJIT_UNALIGNED
5890 context.ucharptr = 0;
5891 #endif
5892 do cc = byte_sequence_compare(common, *cc == OP_CHARI, cc + 1, &context, backtracks); while (context.length > 0);
5893 return cc;
5894 }
5895
5896 /* A non-fixed length character will be checked if length == 0. */
5897 return compile_char1_matchingpath(common, *cc, cc + 1, backtracks);
5898 }
5899
5900 /* Forward definitions. */
5901 static void compile_matchingpath(compiler_common *, pcre_uchar *, pcre_uchar *, backtrack_common *);
5902 static void compile_backtrackingpath(compiler_common *, struct backtrack_common *);
5903
5904 #define PUSH_BACKTRACK(size, ccstart, error) \
5905 do \
5906 { \
5907 backtrack = sljit_alloc_memory(compiler, (size)); \
5908 if (SLJIT_UNLIKELY(sljit_get_compiler_error(compiler))) \
5909 return error; \
5910 memset(backtrack, 0, size); \
5911 backtrack->prev = parent->top; \
5912 backtrack->cc = (ccstart); \
5913 parent->top = backtrack; \
5914 } \
5915 while (0)
5916
5917 #define PUSH_BACKTRACK_NOVALUE(size, ccstart) \
5918 do \
5919 { \
5920 backtrack = sljit_alloc_memory(compiler, (size)); \
5921 if (SLJIT_UNLIKELY(sljit_get_compiler_error(compiler))) \
5922 return; \
5923 memset(backtrack, 0, size); \
5924 backtrack->prev = parent->top; \
5925 backtrack->cc = (ccstart); \
5926 parent->top = backtrack; \
5927 } \
5928 while (0)
5929
5930 #define BACKTRACK_AS(type) ((type *)backtrack)
5931
5932 static void compile_dnref_search(compiler_common *common, pcre_uchar *cc, jump_list **backtracks)
5933 {
5934 /* The OVECTOR offset goes to TMP2. */
5935 DEFINE_COMPILER;
5936 int count = GET2(cc, 1 + IMM2_SIZE);
5937 pcre_uchar *slot = common->name_table + GET2(cc, 1) * common->name_entry_size;
5938 unsigned int offset;
5939 jump_list *found = NULL;
5940
5941 SLJIT_ASSERT(*cc == OP_DNREF || *cc == OP_DNREFI);
5942
5943 OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), OVECTOR(1));
5944
5945 count--;
5946 while (count-- > 0)
5947 {
5948 offset = GET2(slot, 0) << 1;
5949 GET_LOCAL_BASE(TMP2, 0, OVECTOR(offset));
5950 add_jump(compiler, &found, CMP(SLJIT_NOT_EQUAL, SLJIT_MEM1(SLJIT_SP), OVECTOR(offset), TMP1, 0));
5951 slot += common->name_entry_size;
5952 }
5953
5954 offset = GET2(slot, 0) << 1;
5955 GET_LOCAL_BASE(TMP2, 0, OVECTOR(offset));
5956 if (backtracks != NULL && !common->jscript_compat)
5957 add_jump(compiler, backtracks, CMP(SLJIT_EQUAL, SLJIT_MEM1(SLJIT_SP), OVECTOR(offset), TMP1, 0));
5958
5959 set_jumps(found, LABEL());
5960 }
5961
5962 static void compile_ref_matchingpath(compiler_common *common, pcre_uchar *cc, jump_list **backtracks, BOOL withchecks, BOOL emptyfail)
5963 {
5964 DEFINE_COMPILER;
5965 BOOL ref = (*cc == OP_REF || *cc == OP_REFI);
5966 int offset = 0;
5967 struct sljit_jump *jump = NULL;
5968 struct sljit_jump *partial;
5969 struct sljit_jump *nopartial;
5970
5971 if (ref)
5972 {
5973 offset = GET2(cc, 1) << 1;
5974 OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), OVECTOR(offset));
5975 /* OVECTOR(1) contains the "string begin - 1" constant. */
5976 if (withchecks && !common->jscript_compat)
5977 add_jump(compiler, backtracks, CMP(SLJIT_EQUAL, TMP1, 0, SLJIT_MEM1(SLJIT_SP), OVECTOR(1)));
5978 }
5979 else
5980 OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(TMP2), 0);
5981
5982 #if defined SUPPORT_UTF && defined SUPPORT_UCP
5983 if (common->utf && *cc == OP_REFI)
5984 {
5985 SLJIT_ASSERT(TMP1 == SLJIT_R0 && STACK_TOP == SLJIT_R1 && TMP2 == SLJIT_R2);
5986 if (ref)
5987 OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_SP), OVECTOR(offset + 1));
5988 else
5989 OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(TMP2), sizeof(sljit_sw));
5990
5991 if (withchecks)
5992 jump = CMP(SLJIT_EQUAL, TMP1, 0, TMP2, 0);
5993
5994 /* Needed to save important temporary registers. */
5995 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), LOCALS0, STACK_TOP, 0);
5996 OP1(SLJIT_MOV, SLJIT_R1, 0, ARGUMENTS, 0);
5997 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_R1), SLJIT_OFFSETOF(jit_arguments, uchar_ptr), STR_PTR, 0);
5998 sljit_emit_ijump(compiler, SLJIT_CALL3, SLJIT_IMM, SLJIT_FUNC_OFFSET(do_utf_caselesscmp));
5999 OP1(SLJIT_MOV, STACK_TOP, 0, SLJIT_MEM1(SLJIT_SP), LOCALS0);
6000 if (common->mode == JIT_COMPILE)
6001 add_jump(compiler, backtracks, CMP(SLJIT_LESS_EQUAL, SLJIT_RETURN_REG, 0, SLJIT_IMM, 1));
6002 else
6003 {
6004 add_jump(compiler, backtracks, CMP(SLJIT_EQUAL, SLJIT_RETURN_REG, 0, SLJIT_IMM, 0));
6005 nopartial = CMP(SLJIT_NOT_EQUAL, SLJIT_RETURN_REG, 0, SLJIT_IMM, 1);
6006 check_partial(common, FALSE);
6007 add_jump(compiler, backtracks, JUMP(SLJIT_JUMP));
6008 JUMPHERE(nopartial);
6009 }
6010 OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_RETURN_REG, 0);
6011 }
6012 else
6013 #endif /* SUPPORT_UTF && SUPPORT_UCP */
6014 {
6015 if (ref)
6016 OP2(SLJIT_SUB | SLJIT_SET_E, TMP2, 0, SLJIT_MEM1(SLJIT_SP), OVECTOR(offset + 1), TMP1, 0);
6017 else
6018 OP2(SLJIT_SUB | SLJIT_SET_E, TMP2, 0, SLJIT_MEM1(TMP2), sizeof(sljit_sw), TMP1, 0);
6019
6020 if (withchecks)
6021 jump = JUMP(SLJIT_ZERO);
6022
6023 OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, TMP2, 0);
6024 partial = CMP(SLJIT_GREATER, STR_PTR, 0, STR_END, 0);
6025 if (common->mode == JIT_COMPILE)
6026 add_jump(compiler, backtracks, partial);
6027
6028 add_jump(compiler, *cc == OP_REF ? &common->casefulcmp : &common->caselesscmp, JUMP(SLJIT_FAST_CALL));
6029 add_jump(compiler, backtracks, CMP(SLJIT_NOT_EQUAL, TMP2, 0, SLJIT_IMM, 0));
6030
6031 if (common->mode != JIT_COMPILE)
6032 {
6033 nopartial = JUMP(SLJIT_JUMP);
6034 JUMPHERE(partial);
6035 /* TMP2 -= STR_END - STR_PTR */
6036 OP2(SLJIT_SUB, TMP2, 0, TMP2, 0, STR_PTR, 0);
6037 OP2(SLJIT_ADD, TMP2, 0, TMP2, 0, STR_END, 0);
6038 partial = CMP(SLJIT_EQUAL, TMP2, 0, SLJIT_IMM, 0);
6039 OP1(SLJIT_MOV, STR_PTR, 0, STR_END, 0);
6040 add_jump(compiler, *cc == OP_REF ? &common->casefulcmp : &common->caselesscmp, JUMP(SLJIT_FAST_CALL));
6041 add_jump(compiler, backtracks, CMP(SLJIT_NOT_EQUAL, TMP2, 0, SLJIT_IMM, 0));
6042 JUMPHERE(partial);
6043 check_partial(common, FALSE);
6044 add_jump(compiler, backtracks, JUMP(SLJIT_JUMP));
6045 JUMPHERE(nopartial);
6046 }
6047 }
6048
6049 if (jump != NULL)
6050 {
6051 if (emptyfail)
6052 add_jump(compiler, backtracks, jump);
6053 else
6054 JUMPHERE(jump);
6055 }
6056 }
6057
6058 static SLJIT_INLINE pcre_uchar *compile_ref_iterator_matchingpath(compiler_common *common, pcre_uchar *cc, backtrack_common *parent)
6059 {
6060 DEFINE_COMPILER;
6061 BOOL ref = (*cc == OP_REF || *cc == OP_REFI);
6062 backtrack_common *backtrack;
6063 pcre_uchar type;
6064 int offset = 0;
6065 struct sljit_label *label;
6066 struct sljit_jump *zerolength;
6067 struct sljit_jump *jump = NULL;
6068 pcre_uchar *ccbegin = cc;
6069 int min = 0, max = 0;
6070 BOOL minimize;
6071
6072 PUSH_BACKTRACK(sizeof(iterator_backtrack), cc, NULL);
6073
6074 if (ref)
6075 offset = GET2(cc, 1) << 1;
6076 else
6077 cc += IMM2_SIZE;
6078 type = cc[1 + IMM2_SIZE];
6079
6080 SLJIT_COMPILE_ASSERT((OP_CRSTAR & 0x1) == 0, crstar_opcode_must_be_even);
6081 minimize = (type & 0x1) != 0;
6082 switch(type)
6083 {
6084 case OP_CRSTAR:
6085 case OP_CRMINSTAR:
6086 min = 0;
6087 max = 0;
6088 cc += 1 + IMM2_SIZE + 1;
6089 break;
6090 case OP_CRPLUS:
6091 case OP_CRMINPLUS:
6092 min = 1;
6093 max = 0;
6094 cc += 1 + IMM2_SIZE + 1;
6095 break;
6096 case OP_CRQUERY:
6097 case OP_CRMINQUERY:
6098 min = 0;
6099 max = 1;
6100 cc += 1 + IMM2_SIZE + 1;
6101 break;
6102 case OP_CRRANGE:
6103 case OP_CRMINRANGE:
6104 min = GET2(cc, 1 + IMM2_SIZE + 1);
6105 max = GET2(cc, 1 + IMM2_SIZE + 1 + IMM2_SIZE);
6106 cc += 1 + IMM2_SIZE + 1 + 2 * IMM2_SIZE;
6107 break;
6108 default:
6109 SLJIT_ASSERT_STOP();
6110 break;
6111 }
6112
6113 if (!minimize)
6114 {
6115 if (min == 0)
6116 {
6117 allocate_stack(common, 2);
6118 if (ref)
6119 OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), OVECTOR(offset));
6120 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), STR_PTR, 0);
6121 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(1), SLJIT_IMM, 0);
6122 /* Temporary release of STR_PTR. */
6123 OP2(SLJIT_SUB, STACK_TOP, 0, STACK_TOP, 0, SLJIT_IMM, sizeof(sljit_sw));
6124 /* Handles both invalid and empty cases. Since the minimum repeat,
6125 is zero the invalid case is basically the same as an empty case. */
6126 if (ref)
6127 zerolength = CMP(SLJIT_EQUAL, TMP1, 0, SLJIT_MEM1(SLJIT_SP), OVECTOR(offset + 1));
6128 else
6129 {
6130 compile_dnref_search(common, ccbegin, NULL);
6131 OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(TMP2), 0);
6132 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), POSSESSIVE1, TMP2, 0);
6133 zerolength = CMP(SLJIT_EQUAL, TMP1, 0, SLJIT_MEM1(TMP2), sizeof(sljit_sw));
6134 }
6135 /* Restore if not zero length. */
6136 OP2(SLJIT_ADD, STACK_TOP, 0, STACK_TOP, 0, SLJIT_IMM, sizeof(sljit_sw));
6137 }
6138 else
6139 {
6140 allocate_stack(common, 1);
6141 if (ref)
6142 OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), OVECTOR(offset));
6143 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), SLJIT_IMM, 0);
6144 if (ref)
6145 {
6146 add_jump(compiler, &backtrack->topbacktracks, CMP(SLJIT_EQUAL, TMP1, 0, SLJIT_MEM1(SLJIT_SP), OVECTOR(1)));
6147 zerolength = CMP(SLJIT_EQUAL, TMP1, 0, SLJIT_MEM1(SLJIT_SP), OVECTOR(offset + 1));
6148 }
6149 else
6150 {
6151 compile_dnref_search(common, ccbegin, &backtrack->topbacktracks);
6152 OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(TMP2), 0);
6153 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), POSSESSIVE1, TMP2, 0);
6154 zerolength = CMP(SLJIT_EQUAL, TMP1, 0, SLJIT_MEM1(TMP2), sizeof(sljit_sw));
6155 }
6156 }
6157
6158 if (min > 1 || max > 1)
6159 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), POSSESSIVE0, SLJIT_IMM, 0);
6160
6161 label = LABEL();
6162 if (!ref)
6163 OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_SP), POSSESSIVE1);
6164 compile_ref_matchingpath(common, ccbegin, &backtrack->topbacktracks, FALSE, FALSE);
6165
6166 if (min > 1 || max > 1)
6167 {
6168 OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), POSSESSIVE0);
6169 OP2(SLJIT_ADD, TMP1, 0, TMP1, 0, SLJIT_IMM, 1);
6170 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), POSSESSIVE0, TMP1, 0);
6171 if (min > 1)
6172 CMPTO(SLJIT_LESS, TMP1, 0, SLJIT_IMM, min, label);
6173 if (max > 1)
6174 {
6175 jump = CMP(SLJIT_GREATER_EQUAL, TMP1, 0, SLJIT_IMM, max);
6176 allocate_stack(common, 1);
6177 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), STR_PTR, 0);
6178 JUMPTO(SLJIT_JUMP, label);
6179 JUMPHERE(jump);
6180 }
6181 }
6182
6183 if (max == 0)
6184 {
6185 /* Includes min > 1 case as well. */
6186 allocate_stack(common, 1);
6187 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), STR_PTR, 0);
6188 JUMPTO(SLJIT_JUMP, label);
6189 }
6190
6191 JUMPHERE(zerolength);
6192 BACKTRACK_AS(iterator_backtrack)->matchingpath = LABEL();
6193
6194 count_match(common);
6195 return cc;
6196 }
6197
6198 allocate_stack(common, ref ? 2 : 3);
6199 if (ref)
6200 OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), OVECTOR(offset));
6201 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), SLJIT_IMM, 0);
6202 if (type != OP_CRMINSTAR)
6203 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(1), SLJIT_IMM, 0);
6204
6205 if (min == 0)
6206 {
6207 /* Handles both invalid and empty cases. Since the minimum repeat,
6208 is zero the invalid case is basically the same as an empty case. */
6209 if (ref)
6210 zerolength = CMP(SLJIT_EQUAL, TMP1, 0, SLJIT_MEM1(SLJIT_SP), OVECTOR(offset + 1));
6211 else
6212 {
6213 compile_dnref_search(common, ccbegin, NULL);
6214 OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(TMP2), 0);
6215 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(2), TMP2, 0);
6216 zerolength = CMP(SLJIT_EQUAL, TMP1, 0, SLJIT_MEM1(TMP2), sizeof(sljit_sw));
6217 }
6218 /* Length is non-zero, we can match real repeats. */
6219 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), STR_PTR, 0);
6220 jump = JUMP(SLJIT_JUMP);
6221 }
6222 else
6223 {
6224 if (ref)
6225 {
6226 add_jump(compiler, &backtrack->topbacktracks, CMP(SLJIT_EQUAL, TMP1, 0, SLJIT_MEM1(SLJIT_SP), OVECTOR(1)));
6227 zerolength = CMP(SLJIT_EQUAL, TMP1, 0, SLJIT_MEM1(SLJIT_SP), OVECTOR(offset + 1));
6228 }
6229 else
6230 {
6231 compile_dnref_search(common, ccbegin, &backtrack->topbacktracks);
6232 OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(TMP2), 0);
6233 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(2), TMP2, 0);
6234 zerolength = CMP(SLJIT_EQUAL, TMP1, 0, SLJIT_MEM1(TMP2), sizeof(sljit_sw));
6235 }
6236 }
6237
6238 BACKTRACK_AS(iterator_backtrack)->matchingpath = LABEL();
6239 if (max > 0)
6240 add_jump(compiler, &backtrack->topbacktracks, CMP(SLJIT_GREATER_EQUAL, SLJIT_MEM1(STACK_TOP), STACK(1), SLJIT_IMM, max));
6241
6242 if (!ref)
6243 OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(STACK_TOP), STACK(2));
6244 compile_ref_matchingpath(common, ccbegin, &backtrack->topbacktracks, TRUE, TRUE);
6245 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), STR_PTR, 0);
6246
6247 if (min > 1)
6248 {
6249 OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(STACK_TOP), STACK(1));
6250 OP2(SLJIT_ADD, TMP1, 0, TMP1, 0, SLJIT_IMM, 1);
6251 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(1), TMP1, 0);
6252 CMPTO(SLJIT_LESS, TMP1, 0, SLJIT_IMM, min, BACKTRACK_AS(iterator_backtrack)->matchingpath);
6253 }
6254 else if (max > 0)
6255 OP2(SLJIT_ADD, SLJIT_MEM1(STACK_TOP), STACK(1), SLJIT_MEM1(STACK_TOP), STACK(1), SLJIT_IMM, 1);
6256
6257 if (jump != NULL)
6258 JUMPHERE(jump);
6259 JUMPHERE(zerolength);
6260
6261 count_match(common);
6262 return cc;
6263 }
6264
6265 static SLJIT_INLINE pcre_uchar *compile_recurse_matchingpath(compiler_common *common, pcre_uchar *cc, backtrack_common *parent)
6266 {
6267 DEFINE_COMPILER;
6268 backtrack_common *backtrack;
6269 recurse_entry *entry = common->entries;
6270 recurse_entry *prev = NULL;
6271 sljit_sw start = GET(cc, 1);
6272 pcre_uchar *start_cc;
6273 BOOL needs_control_head;
6274
6275 PUSH_BACKTRACK(sizeof(recurse_backtrack), cc, NULL);
6276
6277 /* Inlining simple patterns. */
6278 if (get_framesize(common, common->start + start, NULL, TRUE, &needs_control_head) == no_stack)
6279 {
6280 start_cc = common->start + start;
6281 compile_matchingpath(common, next_opcode(common, start_cc), bracketend(start_cc) - (1 + LINK_SIZE), backtrack);
6282 BACKTRACK_AS(recurse_backtrack)->inlined_pattern = TRUE;
6283 return cc + 1 + LINK_SIZE;
6284 }
6285
6286 while (entry != NULL)
6287 {
6288 if (entry->start == start)
6289 break;
6290 prev = entry;
6291 entry = entry->next;
6292 }
6293
6294 if (entry == NULL)
6295 {
6296 entry = sljit_alloc_memory(compiler, sizeof(recurse_entry));
6297 if (SLJIT_UNLIKELY(sljit_get_compiler_error(compiler)))
6298 return NULL;
6299 entry->next = NULL;
6300 entry->entry = NULL;
6301 entry->calls = NULL;
6302 entry->start = start;
6303
6304 if (prev != NULL)
6305 prev->next = entry;
6306 else
6307 common->entries = entry;
6308 }
6309
6310 if (common->has_set_som && common->mark_ptr != 0)
6311 {
6312 OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_SP), OVECTOR(0));
6313 allocate_stack(common, 2);
6314 OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), common->mark_ptr);
6315 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), TMP2, 0);
6316 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(1), TMP1, 0);
6317 }
6318 else if (common->has_set_som || common->mark_ptr != 0)
6319 {
6320 OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_SP), common->has_set_som ? (int)(OVECTOR(0)) : common->mark_ptr);
6321 allocate_stack(common, 1);
6322 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), TMP2, 0);
6323 }
6324
6325 if (entry->entry == NULL)
6326 add_jump(compiler, &entry->calls, JUMP(SLJIT_FAST_CALL));
6327 else
6328 JUMPTO(SLJIT_FAST_CALL, entry->entry);
6329 /* Leave if the match is failed. */
6330 add_jump(compiler, &backtrack->topbacktracks, CMP(SLJIT_EQUAL, TMP1, 0, SLJIT_IMM, 0));
6331 return cc + 1 + LINK_SIZE;
6332 }
6333
6334 static int SLJIT_CALL do_callout(struct jit_arguments *arguments, PUBL(callout_block) *callout_block, pcre_uchar **jit_ovector)
6335 {
6336 const pcre_uchar *begin = arguments->begin;
6337 int *offset_vector = arguments->offsets;
6338 int offset_count = arguments->offset_count;
6339 int i;
6340
6341 if (PUBL(callout) == NULL)
6342 return 0;
6343
6344 callout_block->version = 2;
6345 callout_block->callout_data = arguments->callout_data;
6346
6347 /* Offsets in subject. */
6348 callout_block->subject_length = arguments->end - arguments->begin;
6349 callout_block->start_match = (pcre_uchar*)callout_block->subject - arguments->begin;
6350 callout_block->current_position = (pcre_uchar*)callout_block->offset_vector - arguments->begin;
6351 #if defined COMPILE_PCRE8
6352 callout_block->subject = (PCRE_SPTR)begin;
6353 #elif defined COMPILE_PCRE16
6354 callout_block->subject = (PCRE_SPTR16)begin;
6355 #elif defined COMPILE_PCRE32
6356 callout_block->subject = (PCRE_SPTR32)begin;
6357 #endif
6358
6359 /* Convert and copy the JIT offset vector to the offset_vector array. */
6360 callout_block->capture_top = 0;
6361 callout_block->offset_vector = offset_vector;
6362 for (i = 2; i < offset_count; i += 2)
6363 {
6364 offset_vector[i] = jit_ovector[i] - begin;
6365 offset_vector[i + 1] = jit_ovector[i + 1] - begin;
6366 if (jit_ovector[i] >= begin)
6367 callout_block->capture_top = i;
6368 }
6369
6370 callout_block->capture_top = (callout_block->capture_top >> 1) + 1;
6371 if (offset_count > 0)
6372 offset_vector[0] = -1;
6373 if (offset_count > 1)
6374 offset_vector[1] = -1;
6375 return (*PUBL(callout))(callout_block);
6376 }
6377
6378 /* Aligning to 8 byte. */
6379 #define CALLOUT_ARG_SIZE \
6380 (((int)sizeof(PUBL(callout_block)) + 7) & ~7)
6381
6382 #define CALLOUT_ARG_OFFSET(arg) \
6383 (-CALLOUT_ARG_SIZE + SLJIT_OFFSETOF(PUBL(callout_block), arg))
6384
6385 static SLJIT_INLINE pcre_uchar *compile_callout_matchingpath(compiler_common *common, pcre_uchar *cc, backtrack_common *parent)
6386 {
6387 DEFINE_COMPILER;
6388 backtrack_common *backtrack;
6389
6390 PUSH_BACKTRACK(sizeof(backtrack_common), cc, NULL);
6391
6392 allocate_stack(common, CALLOUT_ARG_SIZE / sizeof(sljit_sw));
6393
6394 SLJIT_ASSERT(common->capture_last_ptr != 0);
6395 OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_SP), common->capture_last_ptr);
6396 OP1(SLJIT_MOV, TMP1, 0, ARGUMENTS, 0);
6397 OP1(SLJIT_MOV_SI, SLJIT_MEM1(STACK_TOP), CALLOUT_ARG_OFFSET(callout_number), SLJIT_IMM, cc[1]);
6398 OP1(SLJIT_MOV_SI, SLJIT_MEM1(STACK_TOP), CALLOUT_ARG_OFFSET(capture_last), TMP2, 0);
6399
6400 /* These pointer sized fields temporarly stores internal variables. */
6401 OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_SP), OVECTOR(0));
6402 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), CALLOUT_ARG_OFFSET(offset_vector), STR_PTR, 0);
6403 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), CALLOUT_ARG_OFFSET(subject), TMP2, 0);
6404
6405 if (common->mark_ptr != 0)
6406 OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(TMP1), SLJIT_OFFSETOF(jit_arguments, mark_ptr));
6407 OP1(SLJIT_MOV_SI, SLJIT_MEM1(STACK_TOP), CALLOUT_ARG_OFFSET(pattern_position), SLJIT_IMM, GET(cc, 2));
6408 OP1(SLJIT_MOV_SI, SLJIT_MEM1(STACK_TOP), CALLOUT_ARG_OFFSET(next_item_length), SLJIT_IMM, GET(cc, 2 + LINK_SIZE));
6409 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), CALLOUT_ARG_OFFSET(mark), (common->mark_ptr != 0) ? TMP2 : SLJIT_IMM, 0);
6410
6411 /* Needed to save important temporary registers. */
6412 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), LOCALS0, STACK_TOP, 0);
6413 OP2(SLJIT_SUB, SLJIT_R1, 0, STACK_TOP, 0, SLJIT_IMM, CALLOUT_ARG_SIZE);
6414 GET_LOCAL_BASE(SLJIT_R2, 0, OVECTOR_START);
6415 sljit_emit_ijump(compiler, SLJIT_CALL3, SLJIT_IMM, SLJIT_FUNC_OFFSET(do_callout));
6416 OP1(SLJIT_MOV_SI, SLJIT_RETURN_REG, 0, SLJIT_RETURN_REG, 0);
6417 OP1(SLJIT_MOV, STACK_TOP, 0, SLJIT_MEM1(SLJIT_SP), LOCALS0);
6418 free_stack(common, CALLOUT_ARG_SIZE / sizeof(sljit_sw));
6419
6420 /* Check return value. */
6421 OP2(SLJIT_SUB | SLJIT_SET_S, SLJIT_UNUSED, 0, SLJIT_RETURN_REG, 0, SLJIT_IMM, 0);
6422 add_jump(compiler, &backtrack->topbacktracks, JUMP(SLJIT_SIG_GREATER));
6423 if (common->forced_quit_label == NULL)
6424 add_jump(compiler, &common->forced_quit, JUMP(SLJIT_SIG_LESS));
6425 else
6426 JUMPTO(SLJIT_SIG_LESS, common->forced_quit_label);
6427 return cc + 2 + 2 * LINK_SIZE;
6428 }
6429
6430 #undef CALLOUT_ARG_SIZE
6431 #undef CALLOUT_ARG_OFFSET
6432
6433 static pcre_uchar *compile_assert_matchingpath(compiler_common *common, pcre_uchar *cc, assert_backtrack *backtrack, BOOL conditional)
6434 {
6435 DEFINE_COMPILER;
6436 int framesize;
6437 int extrasize;
6438 BOOL needs_control_head;
6439 int private_data_ptr;
6440 backtrack_common altbacktrack;
6441 pcre_uchar *ccbegin;
6442 pcre_uchar opcode;
6443 pcre_uchar bra = OP_BRA;
6444 jump_list *tmp = NULL;
6445 jump_list **target = (conditional) ? &backtrack->condfailed : &backtrack->common.topbacktracks;
6446 jump_list **found;
6447 /* Saving previous accept variables. */
6448 BOOL save_local_exit = common->local_exit;
6449 BOOL save_positive_assert = common->positive_assert;
6450 then_trap_backtrack *save_then_trap = common->then_trap;
6451 struct sljit_label *save_quit_label = common->quit_label;
6452 struct sljit_label *save_accept_label = common->accept_label;
6453 jump_list *save_quit = common->quit;
6454 jump_list *save_positive_assert_quit = common->positive_assert_quit;
6455 jump_list *save_accept = common->accept;
6456 struct sljit_jump *jump;
6457 struct sljit_jump *brajump = NULL;
6458
6459 /* Assert captures then. */
6460 common->then_trap = NULL;
6461
6462 if (*cc == OP_BRAZERO || *cc == OP_BRAMINZERO)
6463 {
6464 SLJIT_ASSERT(!conditional);
6465 bra = *cc;
6466 cc++;
6467 }
6468 private_data_ptr = PRIVATE_DATA(cc);
6469 SLJIT_ASSERT(private_data_ptr != 0);
6470 framesize = get_framesize(common, cc, NULL, FALSE, &needs_control_head);
6471 backtrack->framesize = framesize;
6472 backtrack->private_data_ptr = private_data_ptr;
6473 opcode = *cc;
6474 SLJIT_ASSERT(opcode >= OP_ASSERT && opcode <= OP_ASSERTBACK_NOT);
6475 found = (opcode == OP_ASSERT || opcode == OP_ASSERTBACK) ? &tmp : target;
6476 ccbegin = cc;
6477 cc += GET(cc, 1);
6478
6479 if (bra == OP_BRAMINZERO)
6480 {
6481 /* This is a braminzero backtrack path. */
6482 OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(STACK_TOP), STACK(0));
6483 free_stack(common, 1);
6484 brajump = CMP(SLJIT_EQUAL, STR_PTR, 0, SLJIT_IMM, 0);
6485 }
6486
6487 if (framesize < 0)
6488 {
6489 extrasize = needs_control_head ? 2 : 1;
6490 if (framesize == no_frame)
6491 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), private_data_ptr, STACK_TOP, 0);
6492 allocate_stack(common, extrasize);
6493 if (needs_control_head)
6494 OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), common->control_head_ptr);
6495 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), STR_PTR, 0);
6496 if (needs_control_head)
6497 {
6498 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->control_head_ptr, SLJIT_IMM, 0);
6499 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(1), TMP1, 0);
6500 }
6501 }
6502 else
6503 {
6504 extrasize = needs_control_head ? 3 : 2;
6505 allocate_stack(common, framesize + extrasize);
6506 OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), private_data_ptr);
6507 OP2(SLJIT_SUB, TMP2, 0, STACK_TOP, 0, SLJIT_IMM, (framesize + extrasize) * sizeof(sljit_sw));
6508 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), private_data_ptr, TMP2, 0);
6509 if (needs_control_head)
6510 OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_SP), common->control_head_ptr);
6511 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), STR_PTR, 0);
6512 if (needs_control_head)
6513 {
6514 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(2), TMP1, 0);
6515 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(1), TMP2, 0);
6516 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->control_head_ptr, SLJIT_IMM, 0);
6517 }
6518 else
6519 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(1), TMP1, 0);
6520 init_frame(common, ccbegin, NULL, framesize + extrasize - 1, extrasize, FALSE);
6521 }
6522
6523 memset(&altbacktrack, 0, sizeof(backtrack_common));
6524 if (opcode == OP_ASSERT_NOT || opcode == OP_ASSERTBACK_NOT)
6525 {
6526 /* Negative assert is stronger than positive assert. */
6527 common->local_exit = TRUE;
6528 common->quit_label = NULL;
6529 common->quit = NULL;
6530 common->positive_assert = FALSE;
6531 }
6532 else
6533 common->positive_assert = TRUE;
6534 common->positive_assert_quit = NULL;
6535
6536 while (1)
6537 {
6538 common->accept_label = NULL;
6539 common->accept = NULL;
6540 altbacktrack.top = NULL;
6541 altbacktrack.topbacktracks = NULL;
6542
6543 if (*ccbegin == OP_ALT)
6544 OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(STACK_TOP), STACK(0));
6545
6546 altbacktrack.cc = ccbegin;
6547 compile_matchingpath(common, ccbegin + 1 + LINK_SIZE, cc, &altbacktrack);
6548 if (SLJIT_UNLIKELY(sljit_get_compiler_error(compiler)))
6549 {
6550 if (opcode == OP_ASSERT_NOT || opcode == OP_ASSERTBACK_NOT)
6551 {
6552 common->local_exit = save_local_exit;
6553 common->quit_label = save_quit_label;
6554 common->quit = save_quit;
6555 }
6556 common->positive_assert = save_positive_assert;
6557 common->then_trap = save_then_trap;
6558 common->accept_label = save_accept_label;
6559 common->positive_assert_quit = save_positive_assert_quit;
6560 common->accept = save_accept;
6561 return NULL;
6562 }
6563 common->accept_label = LABEL();
6564 if (common->accept != NULL)
6565 set_jumps(common->accept, common->accept_label);
6566
6567 /* Reset stack. */
6568 if (framesize < 0)
6569 {
6570 if (framesize == no_frame)
6571 OP1(SLJIT_MOV, STACK_TOP, 0, SLJIT_MEM1(SLJIT_SP), private_data_ptr);
6572 else
6573 free_stack(common, extrasize);
6574 if (needs_control_head)
6575 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->control_head_ptr, SLJIT_MEM1(STACK_TOP), 0);
6576 }
6577 else
6578 {
6579 if ((opcode != OP_ASSERT_NOT && opcode != OP_ASSERTBACK_NOT) || conditional)
6580 {
6581 /* We don't need to keep the STR_PTR, only the previous private_data_ptr. */
6582 OP2(SLJIT_ADD, STACK_TOP, 0, SLJIT_MEM1(SLJIT_SP), private_data_ptr, SLJIT_IMM, (framesize + 1) * sizeof(sljit_sw));
6583 if (needs_control_head)
6584 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->control_head_ptr, SLJIT_MEM1(STACK_TOP), 0);
6585 }
6586 else
6587 {
6588 OP1(SLJIT_MOV, STACK_TOP, 0, SLJIT_MEM1(SLJIT_SP), private_data_ptr);
6589 if (needs_control_head)
6590 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->control_head_ptr, SLJIT_MEM1(STACK_TOP), (framesize + 1) * sizeof(sljit_sw));
6591 add_jump(compiler, &common->revertframes, JUMP(SLJIT_FAST_CALL));
6592 }
6593 }
6594
6595 if (opcode == OP_ASSERT_NOT || opcode == OP_ASSERTBACK_NOT)
6596 {
6597 /* We know that STR_PTR was stored on the top of the stack. */
6598 if (conditional)
6599 OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(STACK_TOP), needs_control_head ? sizeof(sljit_sw) : 0);
6600 else if (bra == OP_BRAZERO)
6601 {
6602 if (framesize < 0)
6603 OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(STACK_TOP), (extrasize - 1) * sizeof(sljit_sw));
6604 else
6605 {
6606 OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(STACK_TOP), framesize * sizeof(sljit_sw));
6607 OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(STACK_TOP), (framesize + extrasize - 1) * sizeof(sljit_sw));
6608 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), private_data_ptr, TMP1, 0);
6609 }
6610 OP2(SLJIT_ADD, STACK_TOP, 0, STACK_TOP, 0, SLJIT_IMM, sizeof(sljit_sw));
6611 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), SLJIT_IMM, 0);
6612 }
6613 else if (framesize >= 0)
6614 {
6615 /* For OP_BRA and OP_BRAMINZERO. */
6616 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), private_data_ptr, SLJIT_MEM1(STACK_TOP), framesize * sizeof(sljit_sw));
6617 }
6618 }
6619 add_jump(compiler, found, JUMP(SLJIT_JUMP));
6620
6621 compile_backtrackingpath(common, altbacktrack.top);
6622 if (SLJIT_UNLIKELY(sljit_get_compiler_error(compiler)))
6623 {
6624 if (opcode == OP_ASSERT_NOT || opcode == OP_ASSERTBACK_NOT)
6625 {
6626 common->local_exit = save_local_exit;
6627 common->quit_label = save_quit_label;
6628 common->quit = save_quit;
6629 }
6630 common->positive_assert = save_positive_assert;
6631 common->then_trap = save_then_trap;
6632 common->accept_label = save_accept_label;
6633 common->positive_assert_quit = save_positive_assert_quit;
6634 common->accept = save_accept;
6635 return NULL;
6636 }
6637 set_jumps(altbacktrack.topbacktracks, LABEL());
6638
6639 if (*cc != OP_ALT)
6640 break;
6641
6642 ccbegin = cc;
6643 cc += GET(cc, 1);
6644 }
6645
6646 if (opcode == OP_ASSERT_NOT || opcode == OP_ASSERTBACK_NOT)
6647 {
6648 SLJIT_ASSERT(common->positive_assert_quit == NULL);
6649 /* Makes the check less complicated below. */
6650 common->positive_assert_quit = common->quit;
6651 }
6652
6653 /* None of them matched. */
6654 if (common->positive_assert_quit != NULL)
6655 {
6656 jump = JUMP(SLJIT_JUMP);
6657 set_jumps(common->positive_assert_quit, LABEL());
6658 SLJIT_ASSERT(framesize != no_stack);
6659 if (framesize < 0)
6660 OP2(SLJIT_ADD, STACK_TOP, 0, SLJIT_MEM1(SLJIT_SP), private_data_ptr, SLJIT_IMM, extrasize * sizeof(sljit_sw));
6661 else
6662 {
6663 OP1(SLJIT_MOV, STACK_TOP, 0, SLJIT_MEM1(SLJIT_SP), private_data_ptr);
6664 add_jump(compiler, &common->revertframes, JUMP(SLJIT_FAST_CALL));
6665 OP2(SLJIT_ADD, STACK_TOP, 0, STACK_TOP, 0, SLJIT_IMM, (framesize + extrasize) * sizeof(sljit_sw));
6666 }
6667 JUMPHERE(jump);
6668 }
6669
6670 if (needs_control_head)
6671 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->control_head_ptr, SLJIT_MEM1(STACK_TOP), STACK(1));
6672
6673 if (opcode == OP_ASSERT || opcode == OP_ASSERTBACK)
6674 {
6675 /* Assert is failed. */
6676 if (conditional || bra == OP_BRAZERO)
6677 OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(STACK_TOP), STACK(0));
6678
6679 if (framesize < 0)
6680 {
6681 /* The topmost item should be 0. */
6682 if (bra == OP_BRAZERO)
6683 {
6684 if (extrasize == 2)
6685 free_stack(common, 1);
6686 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), SLJIT_IMM, 0);
6687 }
6688 else
6689 free_stack(common, extrasize);
6690 }
6691 else
6692 {
6693 OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(STACK_TOP), STACK(extrasize - 1));
6694 /* The topmost item should be 0. */
6695 if (bra == OP_BRAZERO)
6696 {
6697 free_stack(common, framesize + extrasize - 1);
6698 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), SLJIT_IMM, 0);
6699 }
6700 else
6701 free_stack(common, framesize + extrasize);
6702 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), private_data_ptr, TMP1, 0);
6703 }
6704 jump = JUMP(SLJIT_JUMP);
6705 if (bra != OP_BRAZERO)
6706 add_jump(compiler, target, jump);
6707
6708 /* Assert is successful. */
6709 set_jumps(tmp, LABEL());
6710 if (framesize < 0)
6711 {
6712 /* We know that STR_PTR was stored on the top of the stack. */
6713 OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(STACK_TOP), (extrasize - 1) * sizeof(sljit_sw));
6714 /* Keep the STR_PTR on the top of the stack. */
6715 if (bra == OP_BRAZERO)
6716 {
6717 OP2(SLJIT_ADD, STACK_TOP, 0, STACK_TOP, 0, SLJIT_IMM, sizeof(sljit_sw));
6718 if (extrasize == 2)
6719 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), STR_PTR, 0);
6720 }
6721 else if (bra == OP_BRAMINZERO)
6722 {
6723 OP2(SLJIT_ADD, STACK_TOP, 0, STACK_TOP, 0, SLJIT_IMM, sizeof(sljit_sw));
6724 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), SLJIT_IMM, 0);
6725 }
6726 }
6727 else
6728 {
6729 if (bra == OP_BRA)
6730 {
6731 /* We don't need to keep the STR_PTR, only the previous private_data_ptr. */
6732 OP2(SLJIT_ADD, STACK_TOP, 0, SLJIT_MEM1(SLJIT_SP), private_data_ptr, SLJIT_IMM, (framesize + 1) * sizeof(sljit_sw));
6733 OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(STACK_TOP), (extrasize - 2) * sizeof(sljit_sw));
6734 }
6735 else
6736 {
6737 /* We don't need to keep the STR_PTR, only the previous private_data_ptr. */
6738 OP2(SLJIT_ADD, STACK_TOP, 0, SLJIT_MEM1(SLJIT_SP), private_data_ptr, SLJIT_IMM, (framesize + 2) * sizeof(sljit_sw));
6739 if (extrasize == 2)
6740 {
6741 OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(STACK_TOP), STACK(0));
6742 if (bra == OP_BRAMINZERO)
6743 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), SLJIT_IMM, 0);
6744 }
6745 else
6746 {
6747 OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(STACK_TOP), 0);
6748 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), bra == OP_BRAZERO ? STR_PTR : SLJIT_IMM, 0);
6749 }
6750 }
6751 }
6752
6753 if (bra == OP_BRAZERO)
6754 {
6755 backtrack->matchingpath = LABEL();
6756 SET_LABEL(jump, backtrack->matchingpath);
6757 }
6758 else if (bra == OP_BRAMINZERO)
6759 {
6760 JUMPTO(SLJIT_JUMP, backtrack->matchingpath);
6761 JUMPHERE(brajump);
6762 if (framesize >= 0)
6763 {
6764 OP1(SLJIT_MOV, STACK_TOP, 0, SLJIT_MEM1(SLJIT_SP), private_data_ptr);
6765 add_jump(compiler, &common->revertframes, JUMP(SLJIT_FAST_CALL));
6766 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), private_data_ptr, SLJIT_MEM1(STACK_TOP), framesize * sizeof(sljit_sw));
6767 }
6768 set_jumps(backtrack->common.topbacktracks, LABEL());
6769 }
6770 }
6771 else
6772 {
6773 /* AssertNot is successful. */
6774 if (framesize < 0)
6775 {
6776 OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(STACK_TOP), STACK(0));
6777 if (bra != OP_BRA)
6778 {