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