/[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 1448 - (show annotations)
Mon Jan 13 21:25:53 2014 UTC (5 years, 9 months ago) by zherczeg
File MIME type: text/plain
File size: 334722 byte(s)
JIT: Add not (^) and query (?) support to fast forward search.
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 #define MAX_N_CHARS 16
3153 #define MAX_N_BYTES 8
3154
3155 static SLJIT_INLINE void add_prefix_byte(pcre_uint8 byte, pcre_uint8 *bytes)
3156 {
3157 pcre_uint8 len = bytes[0];
3158 int i;
3159
3160 if (len == 255)
3161 return;
3162
3163 if (len == 0)
3164 {
3165 bytes[0] = 1;
3166 bytes[1] = byte;
3167 return;
3168 }
3169
3170 for (i = len; i > 0; i--)
3171 if (bytes[i] == byte)
3172 return;
3173
3174 if (len >= MAX_N_BYTES - 1)
3175 {
3176 bytes[0] = 255;
3177 return;
3178 }
3179
3180 len++;
3181 bytes[len] = byte;
3182 bytes[0] = len;
3183 }
3184
3185 static int scan_prefix(compiler_common *common, pcre_uchar *cc, pcre_uint32 *chars, pcre_uint8 *bytes, int max_chars)
3186 {
3187 /* Recursive function, which scans prefix literals. */
3188 BOOL last, any, caseless;
3189 int len, repeat, len_save, consumed = 0;
3190 pcre_uint32 chr, mask;
3191 pcre_uchar *alternative, *cc_save, *oc;
3192 #if defined SUPPORT_UTF && defined COMPILE_PCRE8
3193 pcre_uchar othercase[8];
3194 #elif defined SUPPORT_UTF && defined COMPILE_PCRE16
3195 pcre_uchar othercase[2];
3196 #else
3197 pcre_uchar othercase[1];
3198 #endif
3199
3200 repeat = 1;
3201 while (TRUE)
3202 {
3203 last = TRUE;
3204 any = FALSE;
3205 caseless = FALSE;
3206 switch (*cc)
3207 {
3208 case OP_CHARI:
3209 caseless = TRUE;
3210 case OP_CHAR:
3211 last = FALSE;
3212 cc++;
3213 break;
3214
3215 case OP_SOD:
3216 case OP_SOM:
3217 case OP_SET_SOM:
3218 case OP_NOT_WORD_BOUNDARY:
3219 case OP_WORD_BOUNDARY:
3220 case OP_EODN:
3221 case OP_EOD:
3222 case OP_CIRC:
3223 case OP_CIRCM:
3224 case OP_DOLL:
3225 case OP_DOLLM:
3226 /* Zero width assertions. */
3227 cc++;
3228 continue;
3229
3230 case OP_ASSERT:
3231 case OP_ASSERT_NOT:
3232 case OP_ASSERTBACK:
3233 case OP_ASSERTBACK_NOT:
3234 cc = bracketend(cc);
3235 continue;
3236
3237 case OP_PLUSI:
3238 case OP_MINPLUSI:
3239 case OP_POSPLUSI:
3240 caseless = TRUE;
3241 case OP_PLUS:
3242 case OP_MINPLUS:
3243 case OP_POSPLUS:
3244 cc++;
3245 break;
3246
3247 case OP_EXACTI:
3248 caseless = TRUE;
3249 case OP_EXACT:
3250 repeat = GET2(cc, 1);
3251 last = FALSE;
3252 cc += 1 + IMM2_SIZE;
3253 break;
3254
3255 case OP_QUERYI:
3256 case OP_MINQUERYI:
3257 case OP_POSQUERYI:
3258 caseless = TRUE;
3259 case OP_QUERY:
3260 case OP_MINQUERY:
3261 case OP_POSQUERY:
3262 len = 1;
3263 cc++;
3264 #ifdef SUPPORT_UTF
3265 if (common->utf && HAS_EXTRALEN(*cc)) len += GET_EXTRALEN(*cc);
3266 #endif
3267 max_chars = scan_prefix(common, cc + len, chars, bytes, max_chars);
3268 if (max_chars == 0)
3269 return consumed;
3270 last = FALSE;
3271 break;
3272
3273 case OP_KET:
3274 cc += 1 + LINK_SIZE;
3275 continue;
3276
3277 case OP_ALT:
3278 cc += GET(cc, 1);
3279 continue;
3280
3281 case OP_ONCE:
3282 case OP_ONCE_NC:
3283 case OP_BRA:
3284 case OP_BRAPOS:
3285 case OP_CBRA:
3286 case OP_CBRAPOS:
3287 alternative = cc + GET(cc, 1);
3288 while (*alternative == OP_ALT)
3289 {
3290 max_chars = scan_prefix(common, alternative + 1 + LINK_SIZE, chars, bytes, max_chars);
3291 if (max_chars == 0)
3292 return consumed;
3293 alternative += GET(alternative, 1);
3294 }
3295
3296 if (*cc == OP_CBRA || *cc == OP_CBRAPOS)
3297 cc += IMM2_SIZE;
3298 cc += 1 + LINK_SIZE;
3299 continue;
3300
3301 case OP_CLASS:
3302 #if defined SUPPORT_UTF && defined COMPILE_PCRE8
3303 if (common->utf && !is_char7_bitset((const pcre_uint8 *)(cc + 1), FALSE)) return consumed;
3304 #endif
3305 any = TRUE;
3306 cc += 1 + 32 / sizeof(pcre_uchar);
3307 break;
3308
3309 case OP_NCLASS:
3310 #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
3311 if (common->utf) return consumed;
3312 #endif
3313 any = TRUE;
3314 cc += 1 + 32 / sizeof(pcre_uchar);
3315 break;
3316
3317 #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
3318 case OP_XCLASS:
3319 #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
3320 if (common->utf) return consumed;
3321 #endif
3322 any = TRUE;
3323 cc += GET(cc, 1);
3324 break;
3325 #endif
3326
3327 case OP_DIGIT:
3328 #if defined SUPPORT_UTF && defined COMPILE_PCRE8
3329 if (common->utf && !is_char7_bitset((const pcre_uint8 *)common->ctypes - cbit_length + cbit_digit, FALSE))
3330 return consumed;
3331 #endif
3332 any = TRUE;
3333 cc++;
3334 break;
3335
3336 case OP_WHITESPACE:
3337 #if defined SUPPORT_UTF && defined COMPILE_PCRE8
3338 if (common->utf && !is_char7_bitset((const pcre_uint8 *)common->ctypes - cbit_length + cbit_space, FALSE))
3339 return consumed;
3340 #endif
3341 any = TRUE;
3342 cc++;
3343 break;
3344
3345 case OP_WORDCHAR:
3346 #if defined SUPPORT_UTF && defined COMPILE_PCRE8
3347 if (common->utf && !is_char7_bitset((const pcre_uint8 *)common->ctypes - cbit_length + cbit_word, FALSE))
3348 return consumed;
3349 #endif
3350 any = TRUE;
3351 cc++;
3352 break;
3353
3354 case OP_NOT:
3355 case OP_NOTI:
3356 cc++;
3357 /* Fall through. */
3358 case OP_NOT_DIGIT:
3359 case OP_NOT_WHITESPACE:
3360 case OP_NOT_WORDCHAR:
3361 case OP_ANY:
3362 case OP_ALLANY:
3363 #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
3364 if (common->utf) return consumed;
3365 #endif
3366 any = TRUE;
3367 cc++;
3368 break;
3369
3370 #ifdef SUPPORT_UCP
3371 case OP_NOTPROP:
3372 case OP_PROP:
3373 #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
3374 if (common->utf) return consumed;
3375 #endif
3376 any = TRUE;
3377 cc += 1 + 2;
3378 break;
3379 #endif
3380
3381 case OP_TYPEEXACT:
3382 repeat = GET2(cc, 1);
3383 cc += 1 + IMM2_SIZE;
3384 continue;
3385
3386 case OP_NOTEXACT:
3387 case OP_NOTEXACTI:
3388 #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
3389 if (common->utf) return consumed;
3390 #endif
3391 any = TRUE;
3392 repeat = GET2(cc, 1);
3393 cc += 1 + IMM2_SIZE + 1;
3394 break;
3395
3396 default:
3397 return consumed;
3398 }
3399
3400 if (any)
3401 {
3402 #if defined COMPILE_PCRE8
3403 mask = 0xff;
3404 #elif defined COMPILE_PCRE16
3405 mask = 0xffff;
3406 #elif defined COMPILE_PCRE32
3407 mask = 0xffffffff;
3408 #else
3409 SLJIT_ASSERT_STOP();
3410 #endif
3411
3412 do
3413 {
3414 chars[0] = mask;
3415 chars[1] = mask;
3416 bytes[0] = 255;
3417
3418 consumed++;
3419 if (--max_chars == 0)
3420 return consumed;
3421 chars += 2;
3422 bytes += MAX_N_BYTES;
3423 }
3424 while (--repeat > 0);
3425
3426 repeat = 1;
3427 continue;
3428 }
3429
3430 len = 1;
3431 #ifdef SUPPORT_UTF
3432 if (common->utf && HAS_EXTRALEN(*cc)) len += GET_EXTRALEN(*cc);
3433 #endif
3434
3435 if (caseless && char_has_othercase(common, cc))
3436 {
3437 #ifdef SUPPORT_UTF
3438 if (common->utf)
3439 {
3440 GETCHAR(chr, cc);
3441 if ((int)PRIV(ord2utf)(char_othercase(common, chr), othercase) != len)
3442 return consumed;
3443 }
3444 else
3445 #endif
3446 {
3447 chr = *cc;
3448 othercase[0] = TABLE_GET(chr, common->fcc, chr);
3449 }
3450 }
3451 else
3452 caseless = FALSE;
3453
3454 len_save = len;
3455 cc_save = cc;
3456 while (TRUE)
3457 {
3458 oc = othercase;
3459 do
3460 {
3461 chr = *cc;
3462 #ifdef COMPILE_PCRE32
3463 if (SLJIT_UNLIKELY(chr == NOTACHAR))
3464 return consumed;
3465 #endif
3466 add_prefix_byte((pcre_uint8)chr, bytes);
3467
3468 mask = 0;
3469 if (caseless)
3470 {
3471 add_prefix_byte((pcre_uint8)*oc, bytes);
3472 mask = *cc ^ *oc;
3473 chr |= mask;
3474 }
3475
3476 #ifdef COMPILE_PCRE32
3477 if (chars[0] == NOTACHAR && chars[1] == 0)
3478 #else
3479 if (chars[0] == NOTACHAR)
3480 #endif
3481 {
3482 chars[0] = chr;
3483 chars[1] = mask;
3484 }
3485 else
3486 {
3487 mask |= chars[0] ^ chr;
3488 chr |= mask;
3489 chars[0] = chr;
3490 chars[1] |= mask;
3491 }
3492
3493 len--;
3494 consumed++;
3495 if (--max_chars == 0)
3496 return consumed;
3497 chars += 2;
3498 bytes += MAX_N_BYTES;
3499 cc++;
3500 oc++;
3501 }
3502 while (len > 0);
3503
3504 if (--repeat == 0)
3505 break;
3506
3507 len = len_save;
3508 cc = cc_save;
3509 }
3510
3511 repeat = 1;
3512 if (last)
3513 return consumed;
3514 }
3515 }
3516
3517 static SLJIT_INLINE BOOL fast_forward_first_n_chars(compiler_common *common, BOOL firstline)
3518 {
3519 DEFINE_COMPILER;
3520 struct sljit_label *start;
3521 struct sljit_jump *quit;
3522 pcre_uint32 chars[MAX_N_CHARS * 2];
3523 pcre_uint8 bytes[MAX_N_CHARS * MAX_N_BYTES];
3524 pcre_uint8 ones[MAX_N_CHARS];
3525 int offsets[3];
3526 pcre_uint32 mask;
3527 pcre_uint8 *byte_set, *byte_set_end;
3528 int i, max, from;
3529 int range_right = -1, range_len = 3 - 1;
3530 sljit_ub *update_table = NULL;
3531 BOOL in_range;
3532
3533 /* This is even TRUE, if both are NULL. */
3534 SLJIT_ASSERT(common->read_only_data_ptr == common->read_only_data);
3535
3536 for (i = 0; i < MAX_N_CHARS; i++)
3537 {
3538 chars[i << 1] = NOTACHAR;
3539 chars[(i << 1) + 1] = 0;
3540 bytes[i * MAX_N_BYTES] = 0;
3541 }
3542
3543 max = scan_prefix(common, common->start, chars, bytes, MAX_N_CHARS);
3544
3545 if (max <= 1)
3546 return FALSE;
3547
3548 for (i = 0; i < max; i++)
3549 {
3550 mask = chars[(i << 1) + 1];
3551 ones[i] = ones_in_half_byte[mask & 0xf];
3552 mask >>= 4;
3553 while (mask != 0)
3554 {
3555 ones[i] += ones_in_half_byte[mask & 0xf];
3556 mask >>= 4;
3557 }
3558 }
3559
3560 in_range = FALSE;
3561 for (i = 0; i <= max; i++)
3562 {
3563 if (in_range && (i - from) > range_len && (bytes[(i - 1) * MAX_N_BYTES] <= 4))
3564 {
3565 range_len = i - from;
3566 range_right = i - 1;
3567 }
3568
3569 if (i < max && bytes[i * MAX_N_BYTES] < 255)
3570 {
3571 if (!in_range)
3572 {
3573 in_range = TRUE;
3574 from = i;
3575 }
3576 }
3577 else if (in_range)
3578 in_range = FALSE;
3579 }
3580
3581 if (range_right >= 0)
3582 {
3583 /* Since no data is consumed (see the assert in the beginning
3584 of this function), this space can be reallocated. */
3585 if (common->read_only_data)
3586 SLJIT_FREE(common->read_only_data);
3587
3588 common->read_only_data_size += 256;
3589 common->read_only_data = (sljit_uw *)SLJIT_MALLOC(common->read_only_data_size);
3590 if (common->read_only_data == NULL)
3591 return TRUE;
3592
3593 update_table = (sljit_ub *)common->read_only_data;
3594 common->read_only_data_ptr = (sljit_uw *)(update_table + 256);
3595 memset(update_table, IN_UCHARS(range_len), 256);
3596
3597 for (i = 0; i < range_len; i++)
3598 {
3599 byte_set = bytes + ((range_right - i) * MAX_N_BYTES);
3600 SLJIT_ASSERT(byte_set[0] > 0 && byte_set[0] < 255);
3601 byte_set_end = byte_set + byte_set[0];
3602 byte_set++;
3603 while (byte_set <= byte_set_end)
3604 {
3605 if (update_table[*byte_set] > IN_UCHARS(i))
3606 update_table[*byte_set] = IN_UCHARS(i);
3607 byte_set++;
3608 }
3609 }
3610 }
3611
3612 offsets[0] = -1;
3613 /* Scan forward. */
3614 for (i = 0; i < max; i++)
3615 if (ones[i] <= 2) {
3616 offsets[0] = i;
3617 break;
3618 }
3619
3620 if (offsets[0] < 0 && range_right < 0)
3621 return FALSE;
3622
3623 if (offsets[0] >= 0)
3624 {
3625 /* Scan backward. */
3626 offsets[1] = -1;
3627 for (i = max - 1; i > offsets[0]; i--)
3628 if (ones[i] <= 2 && i != range_right)
3629 {
3630 offsets[1] = i;
3631 break;
3632 }
3633
3634 /* This case is handled better by fast_forward_first_char. */
3635 if (offsets[1] == -1 && offsets[0] == 0 && range_right < 0)
3636 return FALSE;
3637
3638 offsets[2] = -1;
3639 /* We only search for a middle character if there is no range check. */
3640 if (offsets[1] >= 0 && range_right == -1)
3641 {
3642 /* Scan from middle. */
3643 for (i = (offsets[0] + offsets[1]) / 2 + 1; i < offsets[1]; i++)
3644 if (ones[i] <= 2)
3645 {
3646 offsets[2] = i;
3647 break;
3648 }
3649
3650 if (offsets[2] == -1)
3651 {
3652 for (i = (offsets[0] + offsets[1]) / 2; i > offsets[0]; i--)
3653 if (ones[i] <= 2)
3654 {
3655 offsets[2] = i;
3656 break;
3657 }
3658 }
3659 }
3660
3661 SLJIT_ASSERT(offsets[1] == -1 || (offsets[0] < offsets[1]));
3662 SLJIT_ASSERT(offsets[2] == -1 || (offsets[0] < offsets[2] && offsets[1] > offsets[2]));
3663
3664 chars[0] = chars[offsets[0] << 1];
3665 chars[1] = chars[(offsets[0] << 1) + 1];
3666 if (offsets[2] >= 0)
3667 {
3668 chars[2] = chars[offsets[2] << 1];
3669 chars[3] = chars[(offsets[2] << 1) + 1];
3670 }
3671 if (offsets[1] >= 0)
3672 {
3673 chars[4] = chars[offsets[1] << 1];
3674 chars[5] = chars[(offsets[1] << 1) + 1];
3675 }
3676 }
3677
3678 max -= 1;
3679 if (firstline)
3680 {
3681 SLJIT_ASSERT(common->first_line_end != 0);
3682 OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), common->first_line_end);
3683 OP1(SLJIT_MOV, TMP3, 0, STR_END, 0);
3684 OP2(SLJIT_SUB, STR_END, 0, STR_END, 0, SLJIT_IMM, IN_UCHARS(max));
3685 quit = CMP(SLJIT_C_LESS_EQUAL, STR_END, 0, TMP1, 0);
3686 OP1(SLJIT_MOV, STR_END, 0, TMP1, 0);
3687 JUMPHERE(quit);
3688 }
3689 else
3690 OP2(SLJIT_SUB, STR_END, 0, STR_END, 0, SLJIT_IMM, IN_UCHARS(max));
3691
3692 #if !(defined SLJIT_CONFIG_X86_32 && SLJIT_CONFIG_X86_32)
3693 if (range_right >= 0)
3694 OP1(SLJIT_MOV, RETURN_ADDR, 0, SLJIT_IMM, (sljit_sw)update_table);
3695 #endif
3696
3697 start = LABEL();
3698 quit = CMP(SLJIT_C_GREATER_EQUAL, STR_PTR, 0, STR_END, 0);
3699
3700 SLJIT_ASSERT(range_right >= 0 || offsets[0] >= 0);
3701
3702 if (range_right >= 0)
3703 {
3704 #if defined COMPILE_PCRE8 || (defined SLJIT_LITTLE_ENDIAN && SLJIT_LITTLE_ENDIAN)
3705 OP1(SLJIT_MOV_UB, TMP1, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(range_right));
3706 #else
3707 OP1(SLJIT_MOV_UB, TMP1, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(range_right + 1) - 1);
3708 #endif
3709
3710 #if !(defined SLJIT_CONFIG_X86_32 && SLJIT_CONFIG_X86_32)
3711 OP1(SLJIT_MOV_UB, TMP1, 0, SLJIT_MEM2(RETURN_ADDR, TMP1), 0);
3712 #else
3713 OP1(SLJIT_MOV_UB, TMP1, 0, SLJIT_MEM1(TMP1), (sljit_sw)update_table);
3714 #endif
3715 OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, TMP1, 0);
3716 CMPTO(SLJIT_C_NOT_EQUAL, TMP1, 0, SLJIT_IMM, 0, start);
3717 }
3718
3719 if (offsets[0] >= 0)
3720 {
3721 OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(offsets[0]));
3722 if (offsets[1] >= 0)
3723 OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(offsets[1]));
3724 OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
3725
3726 if (chars[1] != 0)
3727 OP2(SLJIT_OR, TMP1, 0, TMP1, 0, SLJIT_IMM, chars[1]);
3728 CMPTO(SLJIT_C_NOT_EQUAL, TMP1, 0, SLJIT_IMM, chars[0], start);
3729 if (offsets[2] >= 0)
3730 OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(offsets[2] - 1));
3731
3732 if (offsets[1] >= 0)
3733 {
3734 if (chars[5] != 0)
3735 OP2(SLJIT_OR, TMP2, 0, TMP2, 0, SLJIT_IMM, chars[5]);
3736 CMPTO(SLJIT_C_NOT_EQUAL, TMP2, 0, SLJIT_IMM, chars[4], start);
3737 }
3738
3739 if (offsets[2] >= 0)
3740 {
3741 if (chars[3] != 0)
3742 OP2(SLJIT_OR, TMP1, 0, TMP1, 0, SLJIT_IMM, chars[3]);
3743 CMPTO(SLJIT_C_NOT_EQUAL, TMP1, 0, SLJIT_IMM, chars[2], start);
3744 }
3745 OP2(SLJIT_SUB, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
3746 }
3747
3748 JUMPHERE(quit);
3749
3750 if (firstline)
3751 {
3752 if (range_right >= 0)
3753 OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), common->first_line_end);
3754 OP1(SLJIT_MOV, STR_END, 0, TMP3, 0);
3755 if (range_right >= 0)
3756 {
3757 quit = CMP(SLJIT_C_LESS_EQUAL, STR_PTR, 0, TMP1, 0);
3758 OP1(SLJIT_MOV, STR_PTR, 0, TMP1, 0);
3759 JUMPHERE(quit);
3760 }
3761 }
3762 else
3763 OP2(SLJIT_ADD, STR_END, 0, STR_END, 0, SLJIT_IMM, IN_UCHARS(max));
3764 return TRUE;
3765 }
3766
3767 #undef MAX_N_CHARS
3768 #undef MAX_N_BYTES
3769
3770 static SLJIT_INLINE void fast_forward_first_char(compiler_common *common, pcre_uchar first_char, BOOL caseless, BOOL firstline)
3771 {
3772 DEFINE_COMPILER;
3773 struct sljit_label *start;
3774 struct sljit_jump *quit;
3775 struct sljit_jump *found;
3776 pcre_uchar oc, bit;
3777
3778 if (firstline)
3779 {
3780 SLJIT_ASSERT(common->first_line_end != 0);
3781 OP1(SLJIT_MOV, TMP3, 0, STR_END, 0);
3782 OP1(SLJIT_MOV, STR_END, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), common->first_line_end);
3783 }
3784
3785 start = LABEL();
3786 quit = CMP(SLJIT_C_GREATER_EQUAL, STR_PTR, 0, STR_END, 0);
3787 OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), 0);
3788
3789 oc = first_char;
3790 if (caseless)
3791 {
3792 oc = TABLE_GET(first_char, common->fcc, first_char);
3793 #if defined SUPPORT_UCP && !(defined COMPILE_PCRE8)
3794 if (first_char > 127 && common->utf)
3795 oc = UCD_OTHERCASE(first_char);
3796 #endif
3797 }
3798 if (first_char == oc)
3799 found = CMP(SLJIT_C_EQUAL, TMP1, 0, SLJIT_IMM, first_char);
3800 else
3801 {
3802 bit = first_char ^ oc;
3803 if (is_powerof2(bit))
3804 {
3805 OP2(SLJIT_OR, TMP2, 0, TMP1, 0, SLJIT_IMM, bit);
3806 found = CMP(SLJIT_C_EQUAL, TMP2, 0, SLJIT_IMM, first_char | bit);
3807 }
3808 else
3809 {
3810 OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, first_char);
3811 OP_FLAGS(SLJIT_MOV, TMP2, 0, SLJIT_UNUSED, 0, SLJIT_C_EQUAL);
3812 OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, oc);
3813 OP_FLAGS(SLJIT_OR | SLJIT_SET_E, TMP2, 0, TMP2, 0, SLJIT_C_EQUAL);
3814 found = JUMP(SLJIT_C_NOT_ZERO);
3815 }
3816 }
3817
3818 OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
3819 JUMPTO(SLJIT_JUMP, start);
3820 JUMPHERE(found);
3821 JUMPHERE(quit);
3822
3823 if (firstline)
3824 OP1(SLJIT_MOV, STR_END, 0, TMP3, 0);
3825 }
3826
3827 static SLJIT_INLINE void fast_forward_newline(compiler_common *common, BOOL firstline)
3828 {
3829 DEFINE_COMPILER;
3830 struct sljit_label *loop;
3831 struct sljit_jump *lastchar;
3832 struct sljit_jump *firstchar;
3833 struct sljit_jump *quit;
3834 struct sljit_jump *foundcr = NULL;
3835 struct sljit_jump *notfoundnl;
3836 jump_list *newline = NULL;
3837
3838 if (firstline)
3839 {
3840 SLJIT_ASSERT(common->first_line_end != 0);
3841 OP1(SLJIT_MOV, TMP3, 0, STR_END, 0);
3842 OP1(SLJIT_MOV, STR_END, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), common->first_line_end);
3843 }
3844
3845 if (common->nltype == NLTYPE_FIXED && common->newline > 255)
3846 {
3847 lastchar = CMP(SLJIT_C_GREATER_EQUAL, STR_PTR, 0, STR_END, 0);
3848 OP1(SLJIT_MOV, TMP1, 0, ARGUMENTS, 0);
3849 OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(TMP1), SLJIT_OFFSETOF(jit_arguments, str));
3850 OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(TMP1), SLJIT_OFFSETOF(jit_arguments, begin));
3851 firstchar = CMP(SLJIT_C_LESS_EQUAL, STR_PTR, 0, TMP2, 0);
3852
3853 OP2(SLJIT_ADD, TMP1, 0, TMP1, 0, SLJIT_IMM, IN_UCHARS(2));
3854 OP2(SLJIT_SUB | SLJIT_SET_U, SLJIT_UNUSED, 0, STR_PTR, 0, TMP1, 0);
3855 OP_FLAGS(SLJIT_MOV, TMP2, 0, SLJIT_UNUSED, 0, SLJIT_C_GREATER_EQUAL);
3856 #if defined COMPILE_PCRE16 || defined COMPILE_PCRE32
3857 OP2(SLJIT_SHL, TMP2, 0, TMP2, 0, SLJIT_IMM, UCHAR_SHIFT);
3858 #endif
3859 OP2(SLJIT_SUB, STR_PTR, 0, STR_PTR, 0, TMP2, 0);
3860
3861 loop = LABEL();
3862 OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
3863 quit = CMP(SLJIT_C_GREATER_EQUAL, STR_PTR, 0, STR_END, 0);
3864 OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(-2));
3865 OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(-1));
3866 CMPTO(SLJIT_C_NOT_EQUAL, TMP1, 0, SLJIT_IMM, (common->newline >> 8) & 0xff, loop);
3867 CMPTO(SLJIT_C_NOT_EQUAL, TMP2, 0, SLJIT_IMM, common->newline & 0xff, loop);
3868
3869 JUMPHERE(quit);
3870 JUMPHERE(firstchar);
3871 JUMPHERE(lastchar);
3872
3873 if (firstline)
3874 OP1(SLJIT_MOV, STR_END, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), POSSESSIVE0);
3875 return;
3876 }
3877
3878 OP1(SLJIT_MOV, TMP1, 0, ARGUMENTS, 0);
3879 OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(TMP1), SLJIT_OFFSETOF(jit_arguments, str));
3880 firstchar = CMP(SLJIT_C_LESS_EQUAL, STR_PTR, 0, TMP2, 0);
3881 skip_char_back(common);
3882
3883 loop = LABEL();
3884 read_char_range(common, common->nlmin, common->nlmax, TRUE);
3885 lastchar = CMP(SLJIT_C_GREATER_EQUAL, STR_PTR, 0, STR_END, 0);
3886 if (common->nltype == NLTYPE_ANY || common->nltype == NLTYPE_ANYCRLF)
3887 foundcr = CMP(SLJIT_C_EQUAL, TMP1, 0, SLJIT_IMM, CHAR_CR);
3888 check_newlinechar(common, common->nltype, &newline, FALSE);
3889 set_jumps(newline, loop);
3890
3891 if (common->nltype == NLTYPE_ANY || common->nltype == NLTYPE_ANYCRLF)
3892 {
3893 quit = JUMP(SLJIT_JUMP);
3894 JUMPHERE(foundcr);
3895 notfoundnl = CMP(SLJIT_C_GREATER_EQUAL, STR_PTR, 0, STR_END, 0);
3896 OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), 0);
3897 OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, CHAR_NL);
3898 OP_FLAGS(SLJIT_MOV, TMP1, 0, SLJIT_UNUSED, 0, SLJIT_C_EQUAL);
3899 #if defined COMPILE_PCRE16 || defined COMPILE_PCRE32
3900 OP2(SLJIT_SHL, TMP1, 0, TMP1, 0, SLJIT_IMM, UCHAR_SHIFT);
3901 #endif
3902 OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, TMP1, 0);
3903 JUMPHERE(notfoundnl);
3904 JUMPHERE(quit);
3905 }
3906 JUMPHERE(lastchar);
3907 JUMPHERE(firstchar);
3908
3909 if (firstline)
3910 OP1(SLJIT_MOV, STR_END, 0, TMP3, 0);
3911 }
3912
3913 static BOOL check_class_ranges(compiler_common *common, const pcre_uint8 *bits, BOOL nclass, BOOL invert, jump_list **backtracks);
3914
3915 static SLJIT_INLINE void fast_forward_start_bits(compiler_common *common, pcre_uint8 *start_bits, BOOL firstline)
3916 {
3917 DEFINE_COMPILER;
3918 struct sljit_label *start;
3919 struct sljit_jump *quit;
3920 struct sljit_jump *found = NULL;
3921 jump_list *matches = NULL;
3922 #ifndef COMPILE_PCRE8
3923 struct sljit_jump *jump;
3924 #endif
3925
3926 if (firstline)
3927 {
3928 SLJIT_ASSERT(common->first_line_end != 0);
3929 OP1(SLJIT_MOV, RETURN_ADDR, 0, STR_END, 0);
3930 OP1(SLJIT_MOV, STR_END, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), common->first_line_end);
3931 }
3932
3933 start = LABEL();
3934 quit = CMP(SLJIT_C_GREATER_EQUAL, STR_PTR, 0, STR_END, 0);
3935 OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), 0);
3936 #ifdef SUPPORT_UTF
3937 if (common->utf)
3938 OP1(SLJIT_MOV, TMP3, 0, TMP1, 0);
3939 #endif
3940
3941 if (!check_class_ranges(common, start_bits, (start_bits[31] & 0x80) != 0, TRUE, &matches))
3942 {
3943 #ifndef COMPILE_PCRE8
3944 jump = CMP(SLJIT_C_LESS, TMP1, 0, SLJIT_IMM, 255);
3945 OP1(SLJIT_MOV, TMP1, 0, SLJIT_IMM, 255);
3946 JUMPHERE(jump);
3947 #endif
3948 OP2(SLJIT_AND, TMP2, 0, TMP1, 0, SLJIT_IMM, 0x7);
3949 OP2(SLJIT_LSHR, TMP1, 0, TMP1, 0, SLJIT_IMM, 3);
3950 OP1(SLJIT_MOV_UB, TMP1, 0, SLJIT_MEM1(TMP1), (sljit_sw)start_bits);
3951 OP2(SLJIT_SHL, TMP2, 0, SLJIT_IMM, 1, TMP2, 0);
3952 OP2(SLJIT_AND | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, TMP2, 0);
3953 found = JUMP(SLJIT_C_NOT_ZERO);
3954 }
3955
3956 #ifdef SUPPORT_UTF
3957 if (common->utf)
3958 OP1(SLJIT_MOV, TMP1, 0, TMP3, 0);
3959 #endif
3960 OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
3961 #ifdef SUPPORT_UTF
3962 #if defined COMPILE_PCRE8
3963 if (common->utf)
3964 {
3965 CMPTO(SLJIT_C_LESS, TMP1, 0, SLJIT_IMM, 0xc0, start);
3966 OP1(SLJIT_MOV_UB, TMP1, 0, SLJIT_MEM1(TMP1), (sljit_sw)PRIV(utf8_table4) - 0xc0);
3967 OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, TMP1, 0);
3968 }
3969 #elif defined COMPILE_PCRE16
3970 if (common->utf)
3971 {
3972 CMPTO(SLJIT_C_LESS, TMP1, 0, SLJIT_IMM, 0xd800, start);
3973 OP2(SLJIT_AND, TMP1, 0, TMP1, 0, SLJIT_IMM, 0xfc00);
3974 OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0xd800);
3975 OP_FLAGS(SLJIT_MOV, TMP1, 0, SLJIT_UNUSED, 0, SLJIT_C_EQUAL);
3976 OP2(SLJIT_SHL, TMP1, 0, TMP1, 0, SLJIT_IMM, 1);
3977 OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, TMP1, 0);
3978 }
3979 #endif /* COMPILE_PCRE[8|16] */
3980 #endif /* SUPPORT_UTF */
3981 JUMPTO(SLJIT_JUMP, start);
3982 if (found != NULL)
3983 JUMPHERE(found);
3984 if (matches != NULL)
3985 set_jumps(matches, LABEL());
3986 JUMPHERE(quit);
3987
3988 if (firstline)
3989 OP1(SLJIT_MOV, STR_END, 0, RETURN_ADDR, 0);
3990 }
3991
3992 static SLJIT_INLINE struct sljit_jump *search_requested_char(compiler_common *common, pcre_uchar req_char, BOOL caseless, BOOL has_firstchar)
3993 {
3994 DEFINE_COMPILER;
3995 struct sljit_label *loop;
3996 struct sljit_jump *toolong;
3997 struct sljit_jump *alreadyfound;
3998 struct sljit_jump *found;
3999 struct sljit_jump *foundoc = NULL;
4000 struct sljit_jump *notfound;
4001 pcre_uint32 oc, bit;
4002
4003 SLJIT_ASSERT(common->req_char_ptr != 0);
4004 OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), common->req_char_ptr);
4005 OP2(SLJIT_ADD, TMP1, 0, STR_PTR, 0, SLJIT_IMM, REQ_BYTE_MAX);
4006 toolong = CMP(SLJIT_C_LESS, TMP1, 0, STR_END, 0);
4007 alreadyfound = CMP(SLJIT_C_LESS, STR_PTR, 0, TMP2, 0);
4008
4009 if (has_firstchar)
4010 OP2(SLJIT_ADD, TMP1, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
4011 else
4012 OP1(SLJIT_MOV, TMP1, 0, STR_PTR, 0);
4013
4014 loop = LABEL();
4015 notfound = CMP(SLJIT_C_GREATER_EQUAL, TMP1, 0, STR_END, 0);
4016
4017 OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(TMP1), 0);
4018 oc = req_char;
4019 if (caseless)
4020 {
4021 oc = TABLE_GET(req_char, common->fcc, req_char);
4022 #if defined SUPPORT_UCP && !(defined COMPILE_PCRE8)
4023 if (req_char > 127 && common->utf)
4024 oc = UCD_OTHERCASE(req_char);
4025 #endif
4026 }
4027 if (req_char == oc)
4028 found = CMP(SLJIT_C_EQUAL, TMP2, 0, SLJIT_IMM, req_char);
4029 else
4030 {
4031 bit = req_char ^ oc;
4032 if (is_powerof2(bit))
4033 {
4034 OP2(SLJIT_OR, TMP2, 0, TMP2, 0, SLJIT_IMM, bit);
4035 found = CMP(SLJIT_C_EQUAL, TMP2, 0, SLJIT_IMM, req_char | bit);
4036 }
4037 else
4038 {
4039 found = CMP(SLJIT_C_EQUAL, TMP2, 0, SLJIT_IMM, req_char);
4040 foundoc = CMP(SLJIT_C_EQUAL, TMP2, 0, SLJIT_IMM, oc);
4041 }
4042 }
4043 OP2(SLJIT_ADD, TMP1, 0, TMP1, 0, SLJIT_IMM, IN_UCHARS(1));
4044 JUMPTO(SLJIT_JUMP, loop);
4045
4046 JUMPHERE(found);
4047 if (foundoc)
4048 JUMPHERE(foundoc);
4049 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), common->req_char_ptr, TMP1, 0);
4050 JUMPHERE(alreadyfound);
4051 JUMPHERE(toolong);
4052 return notfound;
4053 }
4054
4055 static void do_revertframes(compiler_common *common)
4056 {
4057 DEFINE_COMPILER;
4058 struct sljit_jump *jump;
4059 struct sljit_label *mainloop;
4060
4061 sljit_emit_fast_enter(compiler, RETURN_ADDR, 0);
4062 OP1(SLJIT_MOV, TMP1, 0, STACK_TOP, 0);
4063 GET_LOCAL_BASE(TMP3, 0, 0);
4064
4065 /* Drop frames until we reach STACK_TOP. */
4066 mainloop = LABEL();
4067 OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(TMP1), 0);
4068 OP2(SLJIT_SUB | SLJIT_SET_S, SLJIT_UNUSED, 0, TMP2, 0, SLJIT_IMM, 0);
4069 jump = JUMP(SLJIT_C_SIG_LESS_EQUAL);
4070
4071 OP2(SLJIT_ADD, TMP2, 0, TMP2, 0, TMP3, 0);
4072 OP1(SLJIT_MOV, SLJIT_MEM1(TMP2), 0, SLJIT_MEM1(TMP1), sizeof(sljit_sw));
4073 OP1(SLJIT_MOV, SLJIT_MEM1(TMP2), sizeof(sljit_sw), SLJIT_MEM1(TMP1), 2 * sizeof(sljit_sw));
4074 OP2(SLJIT_ADD, TMP1, 0, TMP1, 0, SLJIT_IMM, 3 * sizeof(sljit_sw));
4075 JUMPTO(SLJIT_JUMP, mainloop);
4076
4077 JUMPHERE(jump);
4078 jump = JUMP(SLJIT_C_SIG_LESS);
4079 /* End of dropping frames. */
4080 sljit_emit_fast_return(compiler, RETURN_ADDR, 0);
4081
4082 JUMPHERE(jump);
4083 OP1(SLJIT_NEG, TMP2, 0, TMP2, 0);
4084 OP2(SLJIT_ADD, TMP2, 0, TMP2, 0, TMP3, 0);
4085 OP1(SLJIT_MOV, SLJIT_MEM1(TMP2), 0, SLJIT_MEM1(TMP1), sizeof(sljit_sw));
4086 OP2(SLJIT_ADD, TMP1, 0, TMP1, 0, SLJIT_IMM, 2 * sizeof(sljit_sw));
4087 JUMPTO(SLJIT_JUMP, mainloop);
4088 }
4089
4090 static void check_wordboundary(compiler_common *common)
4091 {
4092 DEFINE_COMPILER;
4093 struct sljit_jump *skipread;
4094 jump_list *skipread_list = NULL;
4095 #if !(defined COMPILE_PCRE8) || defined SUPPORT_UTF
4096 struct sljit_jump *jump;
4097 #endif
4098
4099 SLJIT_COMPILE_ASSERT(ctype_word == 0x10, ctype_word_must_be_16);
4100
4101 sljit_emit_fast_enter(compiler, SLJIT_MEM1(SLJIT_LOCALS_REG), LOCALS0);
4102 /* Get type of the previous char, and put it to LOCALS1. */
4103 OP1(SLJIT_MOV, TMP1, 0, ARGUMENTS, 0);
4104 OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(TMP1), SLJIT_OFFSETOF(jit_arguments, begin));
4105 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), LOCALS1, SLJIT_IMM, 0);
4106 skipread = CMP(SLJIT_C_LESS_EQUAL, STR_PTR, 0, TMP1, 0);
4107 skip_char_back(common);
4108 check_start_used_ptr(common);
4109 read_char(common);
4110
4111 /* Testing char type. */
4112 #ifdef SUPPORT_UCP
4113 if (common->use_ucp)
4114 {
4115 OP1(SLJIT_MOV, TMP2, 0, SLJIT_IMM, 1);
4116 jump = CMP(SLJIT_C_EQUAL, TMP1, 0, SLJIT_IMM, CHAR_UNDERSCORE);
4117 add_jump(compiler, &common->getucd, JUMP(SLJIT_FAST_CALL));
4118 OP2(SLJIT_SUB, TMP1, 0, TMP1, 0, SLJIT_IMM, ucp_Ll);
4119 OP2(SLJIT_SUB | SLJIT_SET_U, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, ucp_Lu - ucp_Ll);
4120 OP_FLAGS(SLJIT_MOV, TMP2, 0, SLJIT_UNUSED, 0, SLJIT_C_LESS_EQUAL);
4121 OP2(SLJIT_SUB, TMP1, 0, TMP1, 0, SLJIT_IMM, ucp_Nd - ucp_Ll);
4122 OP2(SLJIT_SUB | SLJIT_SET_U, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, ucp_No - ucp_Nd);
4123 OP_FLAGS(SLJIT_OR, TMP2, 0, TMP2, 0, SLJIT_C_LESS_EQUAL);
4124 JUMPHERE(jump);
4125 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), LOCALS1, TMP2, 0);
4126 }
4127 else
4128 #endif
4129 {
4130 #ifndef COMPILE_PCRE8
4131 jump = CMP(SLJIT_C_GREATER, TMP1, 0, SLJIT_IMM, 255);
4132 #elif defined SUPPORT_UTF
4133 /* Here LOCALS1 has already been zeroed. */
4134 jump = NULL;
4135 if (common->utf)
4136 jump = CMP(SLJIT_C_GREATER, TMP1, 0, SLJIT_IMM, 255);
4137 #endif /* COMPILE_PCRE8 */
4138 OP1(SLJIT_MOV_UB, TMP1, 0, SLJIT_MEM1(TMP1), common->ctypes);
4139 OP2(SLJIT_LSHR, TMP1, 0, TMP1, 0, SLJIT_IMM, 4 /* ctype_word */);
4140 OP2(SLJIT_AND, TMP1, 0, TMP1, 0, SLJIT_IMM, 1);
4141 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), LOCALS1, TMP1, 0);
4142 #ifndef COMPILE_PCRE8
4143 JUMPHERE(jump);
4144 #elif defined SUPPORT_UTF
4145 if (jump != NULL)
4146 JUMPHERE(jump);
4147 #endif /* COMPILE_PCRE8 */
4148 }
4149 JUMPHERE(skipread);
4150
4151 OP1(SLJIT_MOV, TMP2, 0, SLJIT_IMM, 0);
4152 check_str_end(common, &skipread_list);
4153 peek_char(common, READ_CHAR_MAX);
4154
4155 /* Testing char type. This is a code duplication. */
4156 #ifdef SUPPORT_UCP
4157 if (common->use_ucp)
4158 {
4159 OP1(SLJIT_MOV, TMP2, 0, SLJIT_IMM, 1);
4160 jump = CMP(SLJIT_C_EQUAL, TMP1, 0, SLJIT_IMM, CHAR_UNDERSCORE);
4161 add_jump(compiler, &common->getucd, JUMP(SLJIT_FAST_CALL));
4162 OP2(SLJIT_SUB, TMP1, 0, TMP1, 0, SLJIT_IMM, ucp_Ll);
4163 OP2(SLJIT_SUB | SLJIT_SET_U, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, ucp_Lu - ucp_Ll);
4164 OP_FLAGS(SLJIT_MOV, TMP2, 0, SLJIT_UNUSED, 0, SLJIT_C_LESS_EQUAL);
4165 OP2(SLJIT_SUB, TMP1, 0, TMP1, 0, SLJIT_IMM, ucp_Nd - ucp_Ll);
4166 OP2(SLJIT_SUB | SLJIT_SET_U, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, ucp_No - ucp_Nd);
4167 OP_FLAGS(SLJIT_OR, TMP2, 0, TMP2, 0, SLJIT_C_LESS_EQUAL);
4168 JUMPHERE(jump);
4169 }
4170 else
4171 #endif
4172 {
4173 #ifndef COMPILE_PCRE8
4174 /* TMP2 may be destroyed by peek_char. */
4175 OP1(SLJIT_MOV, TMP2, 0, SLJIT_IMM, 0);
4176 jump = CMP(SLJIT_C_GREATER, TMP1, 0, SLJIT_IMM, 255);
4177 #elif defined SUPPORT_UTF
4178 OP1(SLJIT_MOV, TMP2, 0, SLJIT_IMM, 0);
4179 jump = NULL;
4180 if (common->utf)
4181 jump = CMP(SLJIT_C_GREATER, TMP1, 0, SLJIT_IMM, 255);
4182 #endif
4183 OP1(SLJIT_MOV_UB, TMP2, 0, SLJIT_MEM1(TMP1), common->ctypes);
4184 OP2(SLJIT_LSHR, TMP2, 0, TMP2, 0, SLJIT_IMM, 4 /* ctype_word */);
4185 OP2(SLJIT_AND, TMP2, 0, TMP2, 0, SLJIT_IMM, 1);
4186 #ifndef COMPILE_PCRE8
4187 JUMPHERE(jump);
4188 #elif defined SUPPORT_UTF
4189 if (jump != NULL)
4190 JUMPHERE(jump);
4191 #endif /* COMPILE_PCRE8 */
4192 }
4193 set_jumps(skipread_list, LABEL());
4194
4195 OP2(SLJIT_XOR | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP2, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), LOCALS1);
4196 sljit_emit_fast_return(compiler, SLJIT_MEM1(SLJIT_LOCALS_REG), LOCALS0);
4197 }
4198
4199 static BOOL check_class_ranges(compiler_common *common, const pcre_uint8 *bits, BOOL nclass, BOOL invert, jump_list **backtracks)
4200 {
4201 DEFINE_COMPILER;
4202 int ranges[MAX_RANGE_SIZE];
4203 pcre_uint8 bit, cbit, all;
4204 int i, byte, length = 0;
4205
4206 bit = bits[0] & 0x1;
4207 /* All bits will be zero or one (since bit is zero or one). */
4208 all = -bit;
4209
4210 for (i = 0; i < 256; )
4211 {
4212 byte = i >> 3;
4213 if ((i & 0x7) == 0 && bits[byte] == all)
4214 i += 8;
4215 else
4216 {
4217 cbit = (bits[byte] >> (i & 0x7)) & 0x1;
4218 if (cbit != bit)
4219 {
4220 if (length >= MAX_RANGE_SIZE)
4221 return FALSE;
4222 ranges[length] = i;
4223 length++;
4224 bit = cbit;
4225 all = -cbit;
4226 }
4227 i++;
4228 }
4229 }
4230
4231 if (((bit == 0) && nclass) || ((bit == 1) && !nclass))
4232 {
4233 if (length >= MAX_RANGE_SIZE)
4234 return FALSE;
4235 ranges[length] = 256;
4236 length++;
4237 }
4238
4239 if (length < 0 || length > 4)
4240 return FALSE;
4241
4242 bit = bits[0] & 0x1;
4243 if (invert) bit ^= 0x1;
4244
4245 /* No character is accepted. */
4246 if (length == 0 && bit == 0)
4247 add_jump(compiler, backtracks, JUMP(SLJIT_JUMP));
4248
4249 switch(length)
4250 {
4251 case 0:
4252 /* When bit != 0, all characters are accepted. */
4253 return TRUE;
4254
4255 case 1:
4256 add_jump(compiler, backtracks, CMP(bit == 0 ? SLJIT_C_LESS : SLJIT_C_GREATER_EQUAL, TMP1, 0, SLJIT_IMM, ranges[0]));
4257 return TRUE;
4258
4259 case 2:
4260 if (ranges[0] + 1 != ranges[1])
4261 {
4262 OP2(SLJIT_SUB, TMP1, 0, TMP1, 0, SLJIT_IMM, ranges[0]);
4263 add_jump(compiler, backtracks, CMP(bit != 0 ? SLJIT_C_LESS : SLJIT_C_GREATER_EQUAL, TMP1, 0, SLJIT_IMM, ranges[1] - ranges[0]));
4264 }
4265 else
4266 add_jump(compiler, backtracks, CMP(bit != 0 ? SLJIT_C_EQUAL : SLJIT_C_NOT_EQUAL, TMP1, 0, SLJIT_IMM, ranges[0]));
4267 return TRUE;
4268
4269 case 3:
4270 if (bit != 0)
4271 {
4272 add_jump(compiler, backtracks, CMP(SLJIT_C_GREATER_EQUAL, TMP1, 0, SLJIT_IMM, ranges[2]));
4273 if (ranges[0] + 1 != ranges[1])
4274 {
4275 OP2(SLJIT_SUB, TMP1, 0, TMP1, 0, SLJIT_IMM, ranges[0]);
4276 add_jump(compiler, backtracks, CMP(SLJIT_C_LESS, TMP1, 0, SLJIT_IMM, ranges[1] - ranges[0]));
4277 }
4278 else
4279 add_jump(compiler, backtracks, CMP(SLJIT_C_EQUAL, TMP1, 0, SLJIT_IMM, ranges[0]));
4280 return TRUE;
4281 }
4282
4283 add_jump(compiler, backtracks, CMP(SLJIT_C_LESS, TMP1, 0, SLJIT_IMM, ranges[0]));
4284 if (ranges[1] + 1 != ranges[2])
4285 {
4286 OP2(SLJIT_SUB, TMP1, 0, TMP1, 0, SLJIT_IMM, ranges[1]);
4287 add_jump(compiler, backtracks, CMP(SLJIT_C_LESS, TMP1, 0, SLJIT_IMM, ranges[2] - ranges[1]));
4288 }
4289 else
4290 add_jump(compiler, backtracks, CMP(SLJIT_C_EQUAL, TMP1, 0, SLJIT_IMM, ranges[1]));
4291 return TRUE;
4292
4293 case 4:
4294 if ((ranges[1] - ranges[0]) == (ranges[3] - ranges[2])
4295 && (ranges[0] | (ranges[2] - ranges[0])) == ranges[2]
4296 && is_powerof2(ranges[2] - ranges[0]))
4297 {
4298 OP2(SLJIT_OR, TMP1, 0, TMP1, 0, SLJIT_IMM, ranges[2] - ranges[0]);
4299 if (ranges[2] + 1 != ranges[3])
4300 {
4301 OP2(SLJIT_SUB, TMP1, 0, TMP1, 0, SLJIT_IMM, ranges[2]);
4302 add_jump(compiler, backtracks, CMP(bit != 0 ? SLJIT_C_LESS : SLJIT_C_GREATER_EQUAL, TMP1, 0, SLJIT_IMM, ranges[3] - ranges[2]));
4303 }
4304 else
4305 add_jump(compiler, backtracks, CMP(bit != 0 ? SLJIT_C_EQUAL : SLJIT_C_NOT_EQUAL, TMP1, 0, SLJIT_IMM, ranges[2]));
4306 return TRUE;
4307 }
4308
4309 if (bit != 0)
4310 {
4311 i = 0;
4312 if (ranges[0] + 1 != ranges[1])
4313 {
4314 OP2(SLJIT_SUB, TMP1, 0, TMP1, 0, SLJIT_IMM, ranges[0]);
4315 add_jump(compiler, backtracks, CMP(SLJIT_C_LESS, TMP1, 0, SLJIT_IMM, ranges[1] - ranges[0]));
4316 i = ranges[0];
4317 }
4318 else
4319 add_jump(compiler, backtracks, CMP(SLJIT_C_EQUAL, TMP1, 0, SLJIT_IMM, ranges[0]));
4320
4321 if (ranges[2] + 1 != ranges[3])
4322 {
4323 OP2(SLJIT_SUB, TMP1, 0, TMP1, 0, SLJIT_IMM, ranges[2] - i);
4324 add_jump(compiler, backtracks, CMP(SLJIT_C_LESS, TMP1, 0, SLJIT_IMM, ranges[3] - ranges[2]));
4325 }
4326 else
4327 add_jump(compiler, backtracks, CMP(SLJIT_C_EQUAL, TMP1, 0, SLJIT_IMM, ranges[2] - i));
4328 return TRUE;
4329 }
4330
4331 OP2(SLJIT_SUB, TMP1, 0, TMP1, 0, SLJIT_IMM, ranges[0]);
4332 add_jump(compiler, backtracks, CMP(SLJIT_C_GREATER_EQUAL, TMP1, 0, SLJIT_IMM, ranges[3] - ranges[0]));
4333 if (ranges[1] + 1 != ranges[2])
4334 {
4335 OP2(SLJIT_SUB, TMP1, 0, TMP1, 0, SLJIT_IMM, ranges[1] - ranges[0]);
4336 add_jump(compiler, backtracks, CMP(SLJIT_C_LESS, TMP1, 0, SLJIT_IMM, ranges[2] - ranges[1]));
4337 }
4338 else
4339 add_jump(compiler, backtracks, CMP(SLJIT_C_EQUAL, TMP1, 0, SLJIT_IMM, ranges[1] - ranges[0]));
4340 return TRUE;
4341
4342 default:
4343 SLJIT_ASSERT_STOP();
4344 return FALSE;
4345 }
4346 }
4347
4348 static void check_anynewline(compiler_common *common)
4349 {
4350 /* Check whether TMP1 contains a newline character. TMP2 destroyed. */
4351 DEFINE_COMPILER;
4352
4353 sljit_emit_fast_enter(compiler, RETURN_ADDR, 0);
4354
4355 OP2(SLJIT_SUB, TMP1, 0, TMP1, 0, SLJIT_IMM, 0x0a);
4356 OP2(SLJIT_SUB | SLJIT_SET_U, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x0d - 0x0a);
4357 OP_FLAGS(SLJIT_MOV, TMP2, 0, SLJIT_UNUSED, 0, SLJIT_C_LESS_EQUAL);
4358 OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x85 - 0x0a);
4359 #if defined SUPPORT_UTF || defined COMPILE_PCRE16 || defined COMPILE_PCRE32
4360 #ifdef COMPILE_PCRE8
4361 if (common->utf)
4362 {
4363 #endif
4364 OP_FLAGS(SLJIT_OR, TMP2, 0, TMP2, 0, SLJIT_C_EQUAL);
4365 OP2(SLJIT_OR, TMP1, 0, TMP1, 0, SLJIT_IMM, 0x1);
4366 OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x2029 - 0x0a);
4367 #ifdef COMPILE_PCRE8
4368 }
4369 #endif
4370 #endif /* SUPPORT_UTF || COMPILE_PCRE16 || COMPILE_PCRE32 */
4371 OP_FLAGS(SLJIT_OR | SLJIT_SET_E, TMP2, 0, TMP2, 0, SLJIT_C_EQUAL);
4372 sljit_emit_fast_return(compiler, RETURN_ADDR, 0);
4373 }
4374
4375 static void check_hspace(compiler_common *common)
4376 {
4377 /* Check whether TMP1 contains a newline character. TMP2 destroyed. */
4378 DEFINE_COMPILER;
4379
4380 sljit_emit_fast_enter(compiler, RETURN_ADDR, 0);
4381
4382 OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x09);
4383 OP_FLAGS(SLJIT_MOV, TMP2, 0, SLJIT_UNUSED, 0, SLJIT_C_EQUAL);
4384 OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x20);
4385 OP_FLAGS(SLJIT_OR, TMP2, 0, TMP2, 0, SLJIT_C_EQUAL);
4386 OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0xa0);
4387 #if defined SUPPORT_UTF || defined COMPILE_PCRE16 || defined COMPILE_PCRE32
4388 #ifdef COMPILE_PCRE8
4389 if (common->utf)
4390 {
4391 #endif
4392 OP_FLAGS(SLJIT_OR, TMP2, 0, TMP2, 0, SLJIT_C_EQUAL);
4393 OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x1680);
4394 OP_FLAGS(SLJIT_OR, TMP2, 0, TMP2, 0, SLJIT_C_EQUAL);
4395 OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x180e);
4396 OP_FLAGS(SLJIT_OR, TMP2, 0, TMP2, 0, SLJIT_C_EQUAL);
4397 OP2(SLJIT_SUB, TMP1, 0, TMP1, 0, SLJIT_IMM, 0x2000);
4398 OP2(SLJIT_SUB | SLJIT_SET_U, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x200A - 0x2000);
4399 OP_FLAGS(SLJIT_OR, TMP2, 0, TMP2, 0, SLJIT_C_LESS_EQUAL);
4400 OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x202f - 0x2000);
4401 OP_FLAGS(SLJIT_OR, TMP2, 0, TMP2, 0, SLJIT_C_EQUAL);
4402 OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x205f - 0x2000);
4403 OP_FLAGS(SLJIT_OR, TMP2, 0, TMP2, 0, SLJIT_C_EQUAL);
4404 OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x3000 - 0x2000);
4405 #ifdef COMPILE_PCRE8
4406 }
4407 #endif
4408 #endif /* SUPPORT_UTF || COMPILE_PCRE16 || COMPILE_PCRE32 */
4409 OP_FLAGS(SLJIT_OR | SLJIT_SET_E, TMP2, 0, TMP2, 0, SLJIT_C_EQUAL);
4410
4411 sljit_emit_fast_return(compiler, RETURN_ADDR, 0);
4412 }
4413
4414 static void check_vspace(compiler_common *common)
4415 {
4416 /* Check whether TMP1 contains a newline character. TMP2 destroyed. */
4417 DEFINE_COMPILER;
4418
4419 sljit_emit_fast_enter(compiler, RETURN_ADDR, 0);
4420
4421 OP2(SLJIT_SUB, TMP1, 0, TMP1, 0, SLJIT_IMM, 0x0a);
4422 OP2(SLJIT_SUB | SLJIT_SET_U, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x0d - 0x0a);
4423 OP_FLAGS(SLJIT_MOV, TMP2, 0, SLJIT_UNUSED, 0, SLJIT_C_LESS_EQUAL);
4424 OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x85 - 0x0a);
4425 #if defined SUPPORT_UTF || defined COMPILE_PCRE16 || defined COMPILE_PCRE32
4426 #ifdef COMPILE_PCRE8
4427 if (common->utf)
4428 {
4429 #endif
4430 OP_FLAGS(SLJIT_OR | SLJIT_SET_E, TMP2, 0, TMP2, 0, SLJIT_C_EQUAL);
4431 OP2(SLJIT_OR, TMP1, 0, TMP1, 0, SLJIT_IMM, 0x1);
4432 OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x2029 - 0x0a);
4433 #ifdef COMPILE_PCRE8
4434 }
4435 #endif
4436 #endif /* SUPPORT_UTF || COMPILE_PCRE16 || COMPILE_PCRE32 */
4437 OP_FLAGS(SLJIT_OR | SLJIT_SET_E, TMP2, 0, TMP2, 0, SLJIT_C_EQUAL);
4438
4439 sljit_emit_fast_return(compiler, RETURN_ADDR, 0);
4440 }
4441
4442 #define CHAR1 STR_END
4443 #define CHAR2 STACK_TOP
4444
4445 static void do_casefulcmp(compiler_common *common)
4446 {
4447 DEFINE_COMPILER;
4448 struct sljit_jump *jump;
4449 struct sljit_label *label;
4450
4451 sljit_emit_fast_enter(compiler, RETURN_ADDR, 0);
4452 OP2(SLJIT_SUB, STR_PTR, 0, STR_PTR, 0, TMP2, 0);
4453 OP1(SLJIT_MOV, TMP3, 0, CHAR1, 0);
4454 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), LOCALS0, CHAR2, 0);
4455 OP2(SLJIT_SUB, TMP1, 0, TMP1, 0, SLJIT_IMM, IN_UCHARS(1));
4456 OP2(SLJIT_SUB, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
4457
4458 label = LABEL();
4459 OP1(MOVU_UCHAR, CHAR1, 0, SLJIT_MEM1(TMP1), IN_UCHARS(1));
4460 OP1(MOVU_UCHAR, CHAR2, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(1));
4461 jump = CMP(SLJIT_C_NOT_EQUAL, CHAR1, 0, CHAR2, 0);
4462 OP2(SLJIT_SUB | SLJIT_SET_E, TMP2, 0, TMP2, 0, SLJIT_IMM, IN_UCHARS(1));
4463 JUMPTO(SLJIT_C_NOT_ZERO, label);
4464
4465 JUMPHERE(jump);
4466 OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
4467 OP1(SLJIT_MOV, CHAR1, 0, TMP3, 0);
4468 OP1(SLJIT_MOV, CHAR2, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), LOCALS0);
4469 sljit_emit_fast_return(compiler, RETURN_ADDR, 0);
4470 }
4471
4472 #define LCC_TABLE STACK_LIMIT
4473
4474 static void do_caselesscmp(compiler_common *common)
4475 {
4476 DEFINE_COMPILER;
4477 struct sljit_jump *jump;
4478 struct sljit_label *label;
4479
4480 sljit_emit_fast_enter(compiler, RETURN_ADDR, 0);
4481 OP2(SLJIT_SUB, STR_PTR, 0, STR_PTR, 0, TMP2, 0);
4482
4483 OP1(SLJIT_MOV, TMP3, 0, LCC_TABLE, 0);
4484 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), LOCALS0, CHAR1, 0);
4485 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), LOCALS1, CHAR2, 0);
4486 OP1(SLJIT_MOV, LCC_TABLE, 0, SLJIT_IMM, common->lcc);
4487 OP2(SLJIT_SUB, TMP1, 0, TMP1, 0, SLJIT_IMM, IN_UCHARS(1));
4488 OP2(SLJIT_SUB, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
4489
4490 label = LABEL();
4491 OP1(MOVU_UCHAR, CHAR1, 0, SLJIT_MEM1(TMP1), IN_UCHARS(1));
4492 OP1(MOVU_UCHAR, CHAR2, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(1));
4493 #ifndef COMPILE_PCRE8
4494 jump = CMP(SLJIT_C_GREATER, CHAR1, 0, SLJIT_IMM, 255);
4495 #endif
4496 OP1(SLJIT_MOV_UB, CHAR1, 0, SLJIT_MEM2(LCC_TABLE, CHAR1), 0);
4497 #ifndef COMPILE_PCRE8
4498 JUMPHERE(jump);
4499 jump = CMP(SLJIT_C_GREATER, CHAR2, 0, SLJIT_IMM, 255);
4500 #endif
4501 OP1(SLJIT_MOV_UB, CHAR2, 0, SLJIT_MEM2(LCC_TABLE, CHAR2), 0);
4502 #ifndef COMPILE_PCRE8
4503 JUMPHERE(jump);
4504 #endif
4505 jump = CMP(SLJIT_C_NOT_EQUAL, CHAR1, 0, CHAR2, 0);
4506 OP2(SLJIT_SUB | SLJIT_SET_E, TMP2, 0, TMP2, 0, SLJIT_IMM, IN_UCHARS(1));
4507 JUMPTO(SLJIT_C_NOT_ZERO, label);
4508
4509 JUMPHERE(jump);
4510 OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
4511 OP1(SLJIT_MOV, LCC_TABLE, 0, TMP3, 0);
4512 OP1(SLJIT_MOV, CHAR1, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), LOCALS0);
4513 OP1(SLJIT_MOV, CHAR2, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), LOCALS1);
4514 sljit_emit_fast_return(compiler, RETURN_ADDR, 0);
4515 }
4516
4517 #undef LCC_TABLE
4518 #undef CHAR1
4519 #undef CHAR2
4520
4521 #if defined SUPPORT_UTF && defined SUPPORT_UCP
4522
4523 static const pcre_uchar * SLJIT_CALL do_utf_caselesscmp(pcre_uchar *src1, jit_arguments *args, pcre_uchar *end1)
4524 {
4525 /* This function would be ineffective to do in JIT level. */
4526 pcre_uint32 c1, c2;
4527 const pcre_uchar *src2 = args->uchar_ptr;
4528 const pcre_uchar *end2 = args->end;
4529 const ucd_record *ur;
4530 const pcre_uint32 *pp;
4531
4532 while (src1 < end1)
4533 {
4534 if (src2 >= end2)
4535 return (pcre_uchar*)1;
4536 GETCHARINC(c1, src1);
4537 GETCHARINC(c2, src2);
4538 ur = GET_UCD(c2);
4539 if (c1 != c2 && c1 != c2 + ur->other_case)
4540 {
4541 pp = PRIV(ucd_caseless_sets) + ur->caseset;
4542 for (;;)
4543 {
4544 if (c1 < *pp) return NULL;
4545 if (c1 == *pp++) break;
4546 }
4547 }
4548 }
4549 return src2;
4550 }
4551
4552 #endif /* SUPPORT_UTF && SUPPORT_UCP */
4553
4554 static pcre_uchar *byte_sequence_compare(compiler_common *common, BOOL caseless, pcre_uchar *cc,
4555 compare_context* context, jump_list **backtracks)
4556 {
4557 DEFINE_COMPILER;
4558 unsigned int othercasebit = 0;
4559 pcre_uchar *othercasechar = NULL;
4560 #ifdef SUPPORT_UTF
4561 int utflength;
4562 #endif
4563
4564 if (caseless && char_has_othercase(common, cc))
4565 {
4566 othercasebit = char_get_othercase_bit(common, cc);
4567 SLJIT_ASSERT(othercasebit);
4568 /* Extracting bit difference info. */
4569 #if defined COMPILE_PCRE8
4570 othercasechar = cc + (othercasebit >> 8);
4571 othercasebit &= 0xff;
4572 #elif defined COMPILE_PCRE16 || defined COMPILE_PCRE32
4573 /* Note that this code only handles characters in the BMP. If there
4574 ever are characters outside the BMP whose othercase differs in only one
4575 bit from itself (there currently are none), this code will need to be
4576 revised for COMPILE_PCRE32. */
4577 othercasechar = cc + (othercasebit >> 9);
4578 if ((othercasebit & 0x100) != 0)
4579 othercasebit = (othercasebit & 0xff) << 8;
4580 else
4581 othercasebit &= 0xff;
4582 #endif /* COMPILE_PCRE[8|16|32] */
4583 }
4584
4585 if (context->sourcereg == -1)
4586 {
4587 #if defined COMPILE_PCRE8
4588 #if defined SLJIT_UNALIGNED && SLJIT_UNALIGNED
4589 if (context->length >= 4)
4590 OP1(SLJIT_MOV_SI, TMP1, 0, SLJIT_MEM1(STR_PTR), -context->length);
4591 else if (context->length >= 2)
4592 OP1(SLJIT_MOV_UH, TMP1, 0, SLJIT_MEM1(STR_PTR), -context->length);
4593 else
4594 #endif
4595 OP1(SLJIT_MOV_UB, TMP1, 0, SLJIT_MEM1(STR_PTR), -context->length);
4596 #elif defined COMPILE_PCRE16
4597 #if defined SLJIT_UNALIGNED && SLJIT_UNALIGNED
4598 if (context->length >= 4)
4599 OP1(SLJIT_MOV_SI, TMP1, 0, SLJIT_MEM1(STR_PTR), -context->length);
4600 else
4601 #endif
4602 OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), -context->length);
4603 #elif defined COMPILE_PCRE32
4604 OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), -context->length);
4605 #endif /* COMPILE_PCRE[8|16|32] */
4606 context->sourcereg = TMP2;
4607 }
4608
4609 #ifdef SUPPORT_UTF
4610 utflength = 1;
4611 if (common->utf && HAS_EXTRALEN(*cc))
4612 utflength += GET_EXTRALEN(*cc);
4613
4614 do
4615 {
4616 #endif
4617
4618 context->length -= IN_UCHARS(1);
4619 #if (defined SLJIT_UNALIGNED && SLJIT_UNALIGNED) && (defined COMPILE_PCRE8 || defined COMPILE_PCRE16)
4620
4621 /* Unaligned read is supported. */
4622 if (othercasebit != 0 && othercasechar == cc)
4623 {
4624 context->c.asuchars[context->ucharptr] = *cc | othercasebit;
4625 context->oc.asuchars[context->ucharptr] = othercasebit;
4626 }
4627 else
4628 {
4629 context->c.asuchars[context->ucharptr] = *cc;
4630 context->oc.asuchars[context->ucharptr] = 0;
4631 }
4632 context->ucharptr++;
4633
4634 #if defined COMPILE_PCRE8
4635 if (context->ucharptr >= 4 || context->length == 0 || (context->ucharptr == 2 && context->length == 1))
4636 #else
4637 if (context->ucharptr >= 2 || context->length == 0)
4638 #endif
4639 {
4640 if (context->length >= 4)
4641 OP1(SLJIT_MOV_SI, context->sourcereg, 0, SLJIT_MEM1(STR_PTR), -context->length);
4642 else if (context->length >= 2)
4643 OP1(SLJIT_MOV_UH, context->sourcereg, 0, SLJIT_MEM1(STR_PTR), -context->length);
4644 #if defined COMPILE_PCRE8
4645 else if (context->length >= 1)
4646 OP1(SLJIT_MOV_UB, context->sourcereg, 0, SLJIT_MEM1(STR_PTR), -context->length);
4647 #endif /* COMPILE_PCRE8 */
4648 context->sourcereg = context->sourcereg == TMP1 ? TMP2 : TMP1;
4649
4650 switch(context->ucharptr)
4651 {
4652 case 4 / sizeof(pcre_uchar):
4653 if (context->oc.asint != 0)
4654 OP2(SLJIT_OR, context->sourcereg, 0, context->sourcereg, 0, SLJIT_IMM, context->oc.asint);
4655 add_jump(compiler, backtracks, CMP(SLJIT_C_NOT_EQUAL, context->sourcereg, 0, SLJIT_IMM, context->c.asint | context->oc.asint));
4656 break;
4657
4658 case 2 / sizeof(pcre_uchar):
4659 if (context->oc.asushort != 0)
4660 OP2(SLJIT_OR, context->sourcereg, 0, context->sourcereg, 0, SLJIT_IMM, context->oc.asushort);
4661 add_jump(compiler, backtracks, CMP(SLJIT_C_NOT_EQUAL, context->sourcereg, 0, SLJIT_IMM, context->c.asushort | context->oc.asushort));
4662 break;
4663
4664 #ifdef COMPILE_PCRE8
4665 case 1:
4666 if (context->oc.asbyte != 0)
4667 OP2(SLJIT_OR, context->sourcereg, 0, context->sourcereg, 0, SLJIT_IMM, context->oc.asbyte);
4668 add_jump(compiler, backtracks, CMP(SLJIT_C_NOT_EQUAL, context->sourcereg, 0, SLJIT_IMM, context->c.asbyte | context->oc.asbyte));
4669 break;
4670 #endif
4671
4672 default:
4673 SLJIT_ASSERT_STOP();
4674 break;
4675 }
4676 context->ucharptr = 0;
4677 }
4678
4679 #else
4680
4681 /* Unaligned read is unsupported or in 32 bit mode. */
4682 if (context->length >= 1)
4683 OP1(MOV_UCHAR, context->sourcereg, 0, SLJIT_MEM1(STR_PTR), -context->length);
4684
4685 context->sourcereg = context->sourcereg == TMP1 ? TMP2 : TMP1;
4686
4687 if (othercasebit != 0 && othercasechar == cc)
4688 {
4689 OP2(SLJIT_OR, context->sourcereg, 0, context->sourcereg, 0, SLJIT_IMM, othercasebit);
4690 add_jump(compiler, backtracks, CMP(SLJIT_C_NOT_EQUAL, context->sourcereg, 0, SLJIT_IMM, *cc | othercasebit));
4691 }
4692 else
4693 add_jump(compiler, backtracks, CMP(SLJIT_C_NOT_EQUAL, context->sourcereg, 0, SLJIT_IMM, *cc));
4694
4695 #endif
4696
4697 cc++;
4698 #ifdef SUPPORT_UTF
4699 utflength--;
4700 }
4701 while (utflength > 0);
4702 #endif
4703
4704 return cc;
4705 }
4706
4707 #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
4708
4709 #define SET_TYPE_OFFSET(value) \
4710 if ((value) != typeoffset) \
4711 { \
4712 if ((value) < typeoffset) \
4713 OP2(SLJIT_ADD, typereg, 0, typereg, 0, SLJIT_IMM, typeoffset - (value)); \
4714 else \
4715 OP2(SLJIT_SUB, typereg, 0, typereg, 0, SLJIT_IMM, (value) - typeoffset); \
4716 } \
4717 typeoffset = (value);
4718
4719 #define SET_CHAR_OFFSET(value) \
4720 if ((value) != charoffset) \
4721 { \
4722 if ((value) < charoffset) \
4723 OP2(SLJIT_ADD, TMP1, 0, TMP1, 0, SLJIT_IMM, (sljit_sw)(charoffset - (value))); \
4724 else \
4725 OP2(SLJIT_SUB, TMP1, 0, TMP1, 0, SLJIT_IMM, (sljit_sw)((value) - charoffset)); \
4726 } \
4727 charoffset = (value);
4728
4729 static void compile_xclass_matchingpath(compiler_common *common, pcre_uchar *cc, jump_list **backtracks)
4730 {
4731 DEFINE_COMPILER;
4732 jump_list *found = NULL;
4733 jump_list **list = (cc[0] & XCL_NOT) == 0 ? &found : backtracks;
4734 sljit_uw c, charoffset, max = 256, min = READ_CHAR_MAX;
4735 struct sljit_jump *jump = NULL;
4736 pcre_uchar *ccbegin;
4737 int compares, invertcmp, numberofcmps;
4738 #if defined SUPPORT_UTF && (defined COMPILE_PCRE8 || defined COMPILE_PCRE16)
4739 BOOL utf = common->utf;
4740 #endif
4741
4742 #ifdef SUPPORT_UCP
4743 BOOL needstype = FALSE, needsscript = FALSE, needschar = FALSE;
4744 BOOL charsaved = FALSE;
4745 int typereg = TMP1, scriptreg = TMP1;
4746 const pcre_uint32 *other_cases;
4747 sljit_uw typeoffset;
4748 #endif
4749
4750 /* Scanning the necessary info. */
4751 cc++;
4752 ccbegin = cc;
4753 compares = 0;
4754 if (cc[-1] & XCL_MAP)
4755 {
4756 min = 0;
4757 cc += 32 / sizeof(pcre_uchar);
4758 }
4759
4760 while (*cc != XCL_END)
4761 {
4762 compares++;
4763 if (*cc == XCL_SINGLE)
4764 {
4765 cc ++;
4766 GETCHARINCTEST(c, cc);
4767 if (c > max) max = c;
4768 if (c < min) min = c;
4769 #ifdef SUPPORT_UCP
4770 needschar = TRUE;
4771 #endif
4772 }
4773 else if (*cc == XCL_RANGE)
4774 {
4775 cc ++;
4776 GETCHARINCTEST(c, cc);
4777 if (c < min) min = c;
4778 GETCHARINCTEST(c, cc);
4779 if (c > max) max = c;
4780 #ifdef SUPPORT_UCP
4781 needschar = TRUE;
4782 #endif
4783 }
4784 #ifdef SUPPORT_UCP
4785 else
4786 {
4787 SLJIT_ASSERT(*cc == XCL_PROP || *cc == XCL_NOTPROP);
4788 cc++;
4789 if (*cc == PT_CLIST)
4790 {
4791 other_cases = PRIV(ucd_caseless_sets) + cc[1];
4792 while (*other_cases != NOTACHAR)
4793 {
4794 if (*other_cases > max) max = *other_cases;
4795 if (*other_cases < min) min = *other_cases;
4796 other_cases++;
4797 }
4798 }
4799 else
4800 {
4801 max = READ_CHAR_MAX;
4802 min = 0;
4803 }
4804
4805 switch(*cc)
4806 {
4807 case PT_ANY:
4808 break;
4809
4810 case PT_LAMP:
4811 case PT_GC:
4812 case PT_PC:
4813 case PT_ALNUM:
4814 needstype = TRUE;
4815 break;
4816
4817 case PT_SC:
4818 needsscript = TRUE;
4819 break;
4820
4821 case PT_SPACE:
4822 case PT_PXSPACE:
4823 case PT_WORD:
4824 case PT_PXGRAPH:
4825 case PT_PXPRINT:
4826 case PT_PXPUNCT:
4827 needstype = TRUE;
4828 needschar = TRUE;
4829 break;
4830
4831 case PT_CLIST:
4832 case PT_UCNC:
4833 needschar = TRUE;
4834 break;
4835
4836 default:
4837 SLJIT_ASSERT_STOP();
4838 break;
4839 }
4840 cc += 2;
4841 }
4842 #endif
4843 }
4844
4845 /* We are not necessary in utf mode even in 8 bit mode. */
4846 cc = ccbegin;
4847 detect_partial_match(common, backtracks);
4848 read_char_range(common, min, max, (cc[-1] & XCL_NOT) != 0);
4849
4850 if ((cc[-1] & XCL_HASPROP) == 0)
4851 {
4852 if ((cc[-1] & XCL_MAP) != 0)
4853 {
4854 jump = CMP(SLJIT_C_GREATER, TMP1, 0, SLJIT_IMM, 255);
4855 if (!check_class_ranges(common, (const pcre_uint8 *)cc, (((const pcre_uint8 *)cc)[31] & 0x80) != 0, TRUE, &found))
4856 {
4857 OP2(SLJIT_AND, TMP2, 0, TMP1, 0, SLJIT_IMM, 0x7);
4858 OP2(SLJIT_LSHR, TMP1, 0, TMP1, 0, SLJIT_IMM, 3);
4859 OP1(SLJIT_MOV_UB, TMP1, 0, SLJIT_MEM1(TMP1), (sljit_sw)cc);
4860 OP2(SLJIT_SHL, TMP2, 0, SLJIT_IMM, 1, TMP2, 0);
4861 OP2(SLJIT_AND | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, TMP2, 0);
4862 add_jump(compiler, &found, JUMP(SLJIT_C_NOT_ZERO));
4863 }
4864
4865 add_jump(compiler, backtracks, JUMP(SLJIT_JUMP));
4866 JUMPHERE(jump);
4867
4868 cc += 32 / sizeof(pcre_uchar);
4869 }
4870 else
4871 {
4872 OP2(SLJIT_SUB, TMP2, 0, TMP1, 0, SLJIT_IMM, min);
4873 add_jump(compiler, (cc[-1] & XCL_NOT) == 0 ? backtracks : &found, CMP(SLJIT_C_GREATER, TMP2, 0, SLJIT_IMM, max - min));
4874 }
4875 }
4876 else if ((cc[-1] & XCL_MAP) != 0)
4877 {
4878 OP1(SLJIT_MOV, TMP3, 0, TMP1, 0);
4879 #ifdef SUPPORT_UCP
4880 charsaved = TRUE;
4881 #endif
4882 if (!check_class_ranges(common, (const pcre_uint8 *)cc, FALSE, TRUE, list))
4883 {
4884 #ifdef COMPILE_PCRE8
4885 SLJIT_ASSERT(common->utf);
4886 #endif
4887 jump = CMP(SLJIT_C_GREATER, TMP1, 0, SLJIT_IMM, 255);
4888
4889 OP2(SLJIT_AND, TMP2, 0, TMP1, 0, SLJIT_IMM, 0x7);
4890 OP2(SLJIT_LSHR, TMP1, 0, TMP1, 0, SLJIT_IMM, 3);
4891 OP1(SLJIT_MOV_UB, TMP1, 0, SLJIT_MEM1(TMP1), (sljit_sw)cc);
4892 OP2(SLJIT_SHL, TMP2, 0, SLJIT_IMM, 1, TMP2, 0);
4893 OP2(SLJIT_AND | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, TMP2, 0);
4894 add_jump(compiler, list, JUMP(SLJIT_C_NOT_ZERO));
4895
4896 JUMPHERE(jump);
4897 }
4898
4899 OP1(SLJIT_MOV, TMP1, 0, TMP3, 0);
4900 cc += 32 / sizeof(pcre_uchar);
4901 }
4902
4903 #ifdef SUPPORT_UCP
4904 /* Simple register allocation. TMP1 is preferred if possible. */
4905 if (needstype || needsscript)
4906 {
4907 if (needschar && !charsaved)
4908 OP1(SLJIT_MOV, TMP3, 0, TMP1, 0);
4909 add_jump(compiler, &common->getucd, JUMP(SLJIT_FAST_CALL));
4910 if (needschar)
4911 {
4912 if (needstype)
4913 {
4914 OP1(SLJIT_MOV, RETURN_ADDR, 0, TMP1, 0);
4915 typereg = RETURN_ADDR;
4916 }
4917
4918 if (needsscript)
4919 scriptreg = TMP3;
4920 OP1(SLJIT_MOV, TMP1, 0, TMP3, 0);
4921 }
4922 else if (needstype && needsscript)
4923 scriptreg = TMP3;
4924 /* In all other cases only one of them was specified, and that can goes to TMP1. */
4925
4926 if (needsscript)
4927 {
4928 if (scriptreg == TMP1)
4929 {
4930 OP1(SLJIT_MOV, scriptreg, 0, SLJIT_IMM, (sljit_sw)PRIV(ucd_records) + SLJIT_OFFSETOF(ucd_record, script));
4931 OP1(SLJIT_MOV_UB, scriptreg, 0, SLJIT_MEM2(scriptreg, TMP2), 3);
4932 }
4933 else
4934 {
4935 OP2(SLJIT_SHL, TMP2, 0, TMP2, 0, SLJIT_IMM, 3);
4936 OP2(SLJIT_ADD, TMP2, 0, TMP2, 0, SLJIT_IMM, (sljit_sw)PRIV(ucd_records) + SLJIT_OFFSETOF(ucd_record, script));
4937 OP1(SLJIT_MOV_UB, scriptreg, 0, SLJIT_MEM1(TMP2), 0);
4938 }
4939 }
4940 }
4941 #endif
4942
4943 /* Generating code. */
4944 charoffset = 0;
4945 numberofcmps = 0;
4946 #ifdef SUPPORT_UCP
4947 typeoffset = 0;
4948 #endif
4949
4950 while (*cc != XCL_END)
4951 {
4952 compares--;
4953 invertcmp = (compares == 0 && list != backtracks);
4954 jump = NULL;
4955
4956 if (*cc == XCL_SINGLE)
4957 {
4958 cc ++;
4959 GETCHARINCTEST(c, cc);
4960
4961 if (numberofcmps < 3 && (*cc == XCL_SINGLE || *cc == XCL_RANGE))
4962 {
4963 OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, (sljit_sw)(c - charoffset));
4964 OP_FLAGS(numberofcmps == 0 ? SLJIT_MOV : SLJIT_OR, TMP2, 0, numberofcmps == 0 ? SLJIT_UNUSED : TMP2, 0, SLJIT_C_EQUAL);
4965 numberofcmps++;
4966 }
4967 else if (numberofcmps > 0)
4968 {
4969 OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, (sljit_sw)(c - charoffset));
4970 OP_FLAGS(SLJIT_OR | SLJIT_SET_E, TMP2, 0, TMP2, 0, SLJIT_C_EQUAL);
4971 jump = JUMP(SLJIT_C_NOT_ZERO ^ invertcmp);
4972 numberofcmps = 0;
4973 }
4974 else
4975 {
4976 jump = CMP(SLJIT_C_EQUAL ^ invertcmp, TMP1, 0, SLJIT_IMM, (sljit_sw)(c - charoffset));
4977 numberofcmps = 0;
4978 }
4979 }
4980 else if (*cc == XCL_RANGE)
4981 {
4982 cc ++;
4983 GETCHARINCTEST(c, cc);
4984 SET_CHAR_OFFSET(c);
4985 GETCHARINCTEST(c, cc);
4986
4987 if (numberofcmps < 3 && (*cc == XCL_SINGLE || *cc == XCL_RANGE))
4988 {
4989 OP2(SLJIT_SUB | SLJIT_SET_U, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, (sljit_sw)(c - charoffset));
4990 OP_FLAGS(numberofcmps == 0 ? SLJIT_MOV : SLJIT_OR, TMP2, 0, numberofcmps == 0 ? SLJIT_UNUSED : TMP2, 0, SLJIT_C_LESS_EQUAL);
4991 numberofcmps++;
4992 }
4993 else if (numberofcmps > 0)
4994 {
4995 OP2(SLJIT_SUB | SLJIT_SET_U, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, (sljit_sw)(c - charoffset));
4996 OP_FLAGS(SLJIT_OR | SLJIT_SET_E, TMP2, 0, TMP2, 0, SLJIT_C_LESS_EQUAL);
4997 jump = JUMP(SLJIT_C_NOT_ZERO ^ invertcmp);
4998 numberofcmps = 0;
4999 }
5000 else
5001 {
5002 jump = CMP(SLJIT_C_LESS_EQUAL ^ invertcmp, TMP1, 0, SLJIT_IMM, (sljit_sw)(c - charoffset));
5003 numberofcmps = 0;
5004 }
5005 }
5006 #ifdef SUPPORT_UCP
5007 else
5008 {
5009 if (*cc == XCL_NOTPROP)
5010 invertcmp ^= 0x1;
5011 cc++;
5012 switch(*cc)
5013 {
5014 case PT_ANY:
5015 if (list != backtracks)
5016 {
5017 if ((cc[-1] == XCL_NOTPROP && compares > 0) || (cc[-1] == XCL_PROP && compares == 0))
5018 continue;
5019 }
5020 else if (cc[-1] == XCL_NOTPROP)
5021 continue;
5022 jump = JUMP(SLJIT_JUMP);
5023 break;
5024
5025 case PT_LAMP:
5026 OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, typereg, 0, SLJIT_IMM, ucp_Lu - typeoffset);
5027 OP_FLAGS(SLJIT_MOV, TMP2, 0, SLJIT_UNUSED, 0, SLJIT_C_EQUAL);
5028 OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, typereg, 0, SLJIT_IMM, ucp_Ll - typeoffset);
5029 OP_FLAGS(SLJIT_OR, TMP2, 0, TMP2, 0, SLJIT_C_EQUAL);
5030 OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, typereg, 0, SLJIT_IMM, ucp_Lt - typeoffset);
5031 OP_FLAGS(SLJIT_OR | SLJIT_SET_E, TMP2, 0, TMP2, 0, SLJIT_C_EQUAL);
5032 jump = JUMP(SLJIT_C_NOT_ZERO ^ invertcmp);
5033 break;
5034
5035 case PT_GC:
5036 c = PRIV(ucp_typerange)[(int)cc[1] * 2];
5037 SET_TYPE_OFFSET(c);
5038 jump = CMP(SLJIT_C_LESS_EQUAL ^ invertcmp, typereg, 0, SLJIT_IMM, PRIV(ucp_typerange)[(int)cc[1] * 2 + 1] - c);
5039 break;
5040
5041 case PT_PC:
5042 jump = CMP(SLJIT_C_EQUAL ^ invertcmp, typereg, 0, SLJIT_IMM, (int)cc[1] - typeoffset);
5043 break;
5044
5045 case PT_SC:
5046 jump = CMP(SLJIT_C_EQUAL ^ invertcmp, scriptreg, 0, SLJIT_IMM, (int)cc[1]);
5047 break;
5048
5049 case PT_SPACE:
5050 case PT_PXSPACE:
5051 SET_CHAR_OFFSET(9);
5052 OP2(SLJIT_SUB | SLJIT_SET_U, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0xd - 0x9);
5053 OP_FLAGS(SLJIT_MOV, TMP2, 0, SLJIT_UNUSED, 0, SLJIT_C_LESS_EQUAL);
5054
5055 OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x85 - 0x9);
5056 OP_FLAGS(SLJIT_OR | SLJIT_SET_E, TMP2, 0, TMP2, 0, SLJIT_C_EQUAL);
5057
5058 OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x180e - 0x9);
5059 OP_FLAGS(SLJIT_OR | SLJIT_SET_E, TMP2, 0, TMP2, 0, SLJIT_C_EQUAL);
5060
5061 SET_TYPE_OFFSET(ucp_Zl);
5062 OP2(SLJIT_SUB | SLJIT_SET_U, SLJIT_UNUSED, 0, typereg, 0, SLJIT_IMM, ucp_Zs - ucp_Zl);
5063 OP_FLAGS(SLJIT_OR | SLJIT_SET_E, TMP2, 0, TMP2, 0, SLJIT_C_LESS_EQUAL);
5064 jump = JUMP(SLJIT_C_NOT_ZERO ^ invertcmp);
5065 break;
5066
5067 case PT_WORD:
5068 OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, (sljit_sw)(CHAR_UNDERSCORE - charoffset));
5069 OP_FLAGS(SLJIT_MOV, TMP2, 0, SLJIT_UNUSED, 0, SLJIT_C_EQUAL);
5070 /* Fall through. */
5071
5072 case PT_ALNUM:
5073 SET_TYPE_OFFSET(ucp_Ll);
5074 OP2(SLJIT_SUB | SLJIT_SET_U, SLJIT_UNUSED, 0, typereg, 0, SLJIT_IMM, ucp_Lu - ucp_Ll);
5075 OP_FLAGS((*cc == PT_ALNUM) ? SLJIT_MOV : SLJIT_OR, TMP2, 0, (*cc == PT_ALNUM) ? SLJIT_UNUSED : TMP2, 0, SLJIT_C_LESS_EQUAL);
5076 SET_TYPE_OFFSET(ucp_Nd);
5077 OP2(SLJIT_SUB | SLJIT_SET_U, SLJIT_UNUSED, 0, typereg, 0, SLJIT_IMM, ucp_No - ucp_Nd);
5078 OP_FLAGS(SLJIT_OR | SLJIT_SET_E, TMP2, 0, TMP2, 0, SLJIT_C_LESS_EQUAL);
5079 jump = JUMP(SLJIT_C_NOT_ZERO ^ invertcmp);
5080 break;
5081
5082 case PT_CLIST:
5083 other_cases = PRIV(ucd_caseless_sets) + cc[1];
5084
5085 /* At least three characters are required.
5086 Otherwise this case would be handled by the normal code path. */
5087 SLJIT_ASSERT(other_cases[0] != NOTACHAR && other_cases[1] != NOTACHAR && other_cases[2] != NOTACHAR);
5088 SLJIT_ASSERT(other_cases[0] < other_cases[1] && other_cases[1] < other_cases[2]);
5089
5090 /* Optimizing character pairs, if their difference is power of 2. */
5091 if (is_powerof2(other_cases[1] ^ other_cases[0]))
5092 {
5093 if (charoffset == 0)
5094 OP2(SLJIT_OR, TMP2, 0, TMP1, 0, SLJIT_IMM, other_cases[1] ^ other_cases[0]);
5095 else
5096 {
5097 OP2(SLJIT_ADD, TMP2, 0, TMP1, 0, SLJIT_IMM, (sljit_sw)charoffset);
5098 OP2(SLJIT_OR, TMP2, 0, TMP2, 0, SLJIT_IMM, other_cases[1] ^ other_cases[0]);
5099 }
5100 OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP2, 0, SLJIT_IMM, other_cases[1]);
5101 OP_FLAGS(SLJIT_MOV, TMP2, 0, SLJIT_UNUSED, 0, SLJIT_C_EQUAL);
5102 other_cases += 2;
5103 }
5104 else if (is_powerof2(other_cases[2] ^ other_cases[1]))
5105 {
5106 if (charoffset == 0)
5107 OP2(SLJIT_OR, TMP2, 0, TMP1, 0, SLJIT_IMM, other_cases[2] ^ other_cases[1]);
5108 else
5109 {
5110 OP2(SLJIT_ADD, TMP2, 0, TMP1, 0, SLJIT_IMM, (sljit_sw)charoffset);
5111 OP2(SLJIT_OR, TMP2, 0, TMP2, 0, SLJIT_IMM, other_cases[1] ^ other_cases[0]);
5112 }
5113 OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP2, 0, SLJIT_IMM, other_cases[2]);
5114 OP_FLAGS(SLJIT_MOV, TMP2, 0, SLJIT_UNUSED, 0, SLJIT_C_EQUAL);
5115
5116 OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, (sljit_sw)(other_cases[0] - charoffset));
5117 OP_FLAGS(SLJIT_OR | ((other_cases[3] == NOTACHAR) ? SLJIT_SET_E : 0), TMP2, 0, TMP2, 0, SLJIT_C_EQUAL);
5118
5119 other_cases += 3;
5120 }
5121 else
5122 {
5123 OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, (sljit_sw)(*other_cases++ - charoffset));
5124 OP_FLAGS(SLJIT_MOV, TMP2, 0, SLJIT_UNUSED, 0, SLJIT_C_EQUAL);
5125 }
5126
5127 while (*other_cases != NOTACHAR)
5128 {
5129 OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, (sljit_sw)(*other_cases++ - charoffset));
5130 OP_FLAGS(SLJIT_OR | ((*other_cases == NOTACHAR) ? SLJIT_SET_E : 0), TMP2, 0, TMP2, 0, SLJIT_C_EQUAL);
5131 }
5132 jump = JUMP(SLJIT_C_NOT_ZERO ^ invertcmp);
5133 break;
5134
5135 case PT_UCNC:
5136 OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, (sljit_sw)(CHAR_DOLLAR_SIGN - charoffset));
5137 OP_FLAGS(SLJIT_MOV, TMP2, 0, SLJIT_UNUSED, 0, SLJIT_C_EQUAL);
5138 OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, (sljit_sw)(CHAR_COMMERCIAL_AT - charoffset));
5139 OP_FLAGS(SLJIT_OR, TMP2, 0, TMP2, 0, SLJIT_C_EQUAL);
5140 OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, (sljit_sw)(CHAR_GRAVE_ACCENT - charoffset));
5141 OP_FLAGS(SLJIT_OR, TMP2, 0, TMP2, 0, SLJIT_C_EQUAL);
5142
5143 SET_CHAR_OFFSET(0xa0);
5144 OP2(SLJIT_SUB | SLJIT_SET_U, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, (sljit_sw)(0xd7ff - charoffset));
5145 OP_FLAGS(SLJIT_OR, TMP2, 0, TMP2, 0, SLJIT_C_LESS_EQUAL);
5146 SET_CHAR_OFFSET(0);
5147 OP2(SLJIT_SUB | SLJIT_SET_U, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0xe000 - 0);
5148 OP_FLAGS(SLJIT_OR | SLJIT_SET_E, TMP2, 0, TMP2, 0, SLJIT_C_GREATER_EQUAL);
5149 jump = JUMP(SLJIT_C_NOT_ZERO ^ invertcmp);
5150 break;
5151
5152 case PT_PXGRAPH:
5153 /* C and Z groups are the farthest two groups. */
5154 SET_TYPE_OFFSET(ucp_Ll);
5155 OP2(SLJIT_SUB | SLJIT_SET_U, SLJIT_UNUSED, 0, typereg, 0, SLJIT_IMM, ucp_So - ucp_Ll);
5156 OP_FLAGS(SLJIT_MOV, TMP2, 0, SLJIT_UNUSED, 0, SLJIT_C_GREATER);
5157
5158 jump = CMP(SLJIT_C_NOT_EQUAL, typereg, 0, SLJIT_IMM, ucp_Cf - ucp_Ll);
5159
5160 /* In case of ucp_Cf, we overwrite the result. */
5161 SET_CHAR_OFFSET(0x2066);
5162 OP2(SLJIT_SUB | SLJIT_SET_U, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x2069 - 0x2066);
5163 OP_FLAGS(SLJIT_MOV, TMP2, 0, SLJIT_UNUSED, 0, SLJIT_C_LESS_EQUAL);
5164
5165 OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x061c - 0x2066);
5166 OP_FLAGS(SLJIT_OR, TMP2, 0, TMP2, 0, SLJIT_C_EQUAL);
5167
5168 OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x180e - 0x2066);
5169 OP_FLAGS(SLJIT_OR, TMP2, 0, TMP2, 0, SLJIT_C_EQUAL);
5170
5171 JUMPHERE(jump);
5172 jump = CMP(SLJIT_C_ZERO ^ invertcmp, TMP2, 0, SLJIT_IMM, 0);
5173 break;
5174
5175 case PT_PXPRINT:
5176 /* C and Z groups are the farthest two groups. */
5177 SET_TYPE_OFFSET(ucp_Ll);
5178 OP2(SLJIT_SUB | SLJIT_SET_U, SLJIT_UNUSED, 0, typereg, 0, SLJIT_IMM, ucp_So - ucp_Ll);
5179 OP_FLAGS(SLJIT_MOV, TMP2, 0, SLJIT_UNUSED, 0, SLJIT_C_GREATER);
5180
5181 OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, typereg, 0, SLJIT_IMM, ucp_Zs - ucp_Ll);
5182 OP_FLAGS(SLJIT_AND, TMP2, 0, TMP2, 0, SLJIT_C_NOT_EQUAL);
5183
5184 jump = CMP(SLJIT_C_NOT_EQUAL, typereg, 0, SLJIT_IMM, ucp_Cf - ucp_Ll);
5185
5186 /* In case of ucp_Cf, we overwrite the result. */
5187 SET_CHAR_OFFSET(0x2066);
5188 OP2(SLJIT_SUB | SLJIT_SET_U, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x2069 - 0x2066);
5189 OP_FLAGS(SLJIT_MOV, TMP2, 0, SLJIT_UNUSED, 0, SLJIT_C_LESS_EQUAL);
5190
5191 OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x061c - 0x2066);
5192 OP_FLAGS(SLJIT_OR, TMP2, 0, TMP2, 0, SLJIT_C_EQUAL);
5193
5194 JUMPHERE(jump);
5195 jump = CMP(SLJIT_C_ZERO ^ invertcmp, TMP2, 0, SLJIT_IMM, 0);
5196 break;
5197
5198 case PT_PXPUNCT:
5199 SET_TYPE_OFFSET(ucp_Sc);
5200 OP2(SLJIT_SUB | SLJIT_SET_U, SLJIT_UNUSED, 0, typereg, 0, SLJIT_IMM, ucp_So - ucp_Sc);
5201 OP_FLAGS(SLJIT_MOV, TMP2, 0, SLJIT_UNUSED, 0, SLJIT_C_LESS_EQUAL);
5202
5203 SET_CHAR_OFFSET(0);
5204 OP2(SLJIT_SUB | SLJIT_SET_U, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0xff);
5205 OP_FLAGS(SLJIT_AND, TMP2, 0, TMP2, 0, SLJIT_C_LESS_EQUAL);
5206
5207 SET_TYPE_OFFSET(ucp_Pc);
5208 OP2(SLJIT_SUB | SLJIT_SET_U, SLJIT_UNUSED, 0, typereg, 0, SLJIT_IMM, ucp_Ps - ucp_Pc);
5209 OP_FLAGS(SLJIT_OR | SLJIT_SET_E, TMP2, 0, TMP2, 0, SLJIT_C_LESS_EQUAL);
5210 jump = JUMP(SLJIT_C_NOT_ZERO ^ invertcmp);
5211 break;
5212 }
5213 cc += 2;
5214 }
5215 #endif
5216
5217 if (jump != NULL)
5218 add_jump(compiler, compares > 0 ? list : backtracks, jump);
5219 }
5220
5221 if (found != NULL)
5222 set_jumps(found, LABEL());
5223 }
5224
5225 #undef SET_TYPE_OFFSET
5226 #undef SET_CHAR_OFFSET
5227
5228 #endif
5229
5230 static pcre_uchar *compile_char1_matchingpath(compiler_common *common, pcre_uchar type, pcre_uchar *cc, jump_list **backtracks)
5231 {
5232 DEFINE_COMPILER;
5233 int length;
5234 unsigned int c, oc, bit;
5235 compare_context context;
5236 struct sljit_jump *jump[4];
5237 jump_list *end_list;
5238 #ifdef SUPPORT_UTF
5239 struct sljit_label *label;
5240 #ifdef SUPPORT_UCP
5241 pcre_uchar propdata[5];
5242 #endif
5243 #endif /* SUPPORT_UTF */
5244
5245 switch(type)
5246 {
5247 case OP_SOD:
5248 OP1(SLJIT_MOV, TMP1, 0, ARGUMENTS, 0);
5249 OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(TMP1), SLJIT_OFFSETOF(jit_arguments, begin));
5250 add_jump(compiler, backtracks, CMP(SLJIT_C_NOT_EQUAL, STR_PTR, 0, TMP1, 0));
5251 return cc;
5252
5253 case OP_SOM:
5254 OP1(SLJIT_MOV, TMP1, 0, ARGUMENTS, 0);
5255 OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(TMP1), SLJIT_OFFSETOF(jit_arguments, str));
5256 add_jump(compiler, backtracks, CMP(SLJIT_C_NOT_EQUAL, STR_PTR, 0, TMP1, 0));
5257 return cc;
5258
5259 case OP_NOT_WORD_BOUNDARY:
5260 case OP_WORD_BOUNDARY:
5261 add_jump(compiler, &common->wordboundary, JUMP(SLJIT_FAST_CALL));
5262 add_jump(compiler, backtracks, JUMP(type == OP_NOT_WORD_BOUNDARY ? SLJIT_C_NOT_ZERO : SLJIT_C_ZERO));
5263 return cc;
5264
5265 case OP_NOT_DIGIT:
5266 case OP_DIGIT:
5267 /* Digits are usually 0-9, so it is worth to optimize them. */
5268 detect_partial_match(common, backtracks);
5269 #if defined SUPPORT_UTF && defined COMPILE_PCRE8
5270 if (common->utf && is_char7_bitset((const pcre_uint8*)common->ctypes - cbit_length + cbit_digit, FALSE))
5271 read_char7_type(common, type == OP_NOT_DIGIT);
5272 else
5273 #endif
5274 read_char8_type(common, type == OP_NOT_DIGIT);
5275 /* Flip the starting bit in the negative case. */
5276 OP2(SLJIT_AND | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, ctype_digit);
5277 add_jump(compiler, backtracks, JUMP(type == OP_DIGIT ? SLJIT_C_ZERO : SLJIT_C_NOT_ZERO));
5278 return cc;
5279
5280 case OP_NOT_WHITESPACE:
5281 case OP_WHITESPACE:
5282 detect_partial_match(common, backtracks);
5283 #if defined SUPPORT_UTF && defined COMPILE_PCRE8
5284 if (common->utf && is_char7_bitset((const pcre_uint8*)common->ctypes - cbit_length + cbit_space, FALSE))
5285 read_char7_type(common, type == OP_NOT_WHITESPACE);
5286 else
5287 #endif
5288 read_char8_type(common, type == OP_NOT_WHITESPACE);
5289 OP2(SLJIT_AND | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, ctype_space);
5290 add_jump(compiler, backtracks, JUMP(type == OP_WHITESPACE ? SLJIT_C_ZERO : SLJIT_C_NOT_ZERO));
5291 return cc;
5292
5293 case OP_NOT_WORDCHAR:
5294 case OP_WORDCHAR:
5295 detect_partial_match(common, backtracks);
5296 #if defined SUPPORT_UTF && defined COMPILE_PCRE8
5297 if (common->utf && is_char7_bitset((const pcre_uint8*)common->ctypes - cbit_length + cbit_word, FALSE))
5298 read_char7_type(common, type == OP_NOT_WORDCHAR);
5299 else
5300 #endif
5301 read_char8_type(common, type == OP_NOT_WORDCHAR);
5302 OP2(SLJIT_AND | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, ctype_word);
5303 add_jump(compiler, backtracks, JUMP(type == OP_WORDCHAR ? SLJIT_C_ZERO : SLJIT_C_NOT_ZERO));
5304 return cc;
5305
5306 case OP_ANY:
5307 detect_partial_match(common, backtracks);
5308 read_char_range(common, common->nlmin, common->nlmax, TRUE);
5309 if (common->nltype == NLTYPE_FIXED && common->newline > 255)
5310 {
5311 jump[0] = CMP(SLJIT_C_NOT_EQUAL, TMP1, 0, SLJIT_IMM, (common->newline >> 8) & 0xff);
5312 end_list = NULL;
5313 if (common->mode != JIT_PARTIAL_HARD_COMPILE)
5314 add_jump(compiler, &end_list, CMP(SLJIT_C_GREATER_EQUAL, STR_PTR, 0, STR_END, 0));
5315 else
5316 check_str_end(common, &end_list);
5317
5318 OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), 0);
5319 add_jump(compiler, backtracks, CMP(SLJIT_C_EQUAL, TMP1, 0, SLJIT_IMM, common->newline & 0xff));
5320 set_jumps(end_list, LABEL());
5321 JUMPHERE(jump[0]);
5322 }
5323 else
5324 check_newlinechar(common, common->nltype, backtracks, TRUE);
5325 return cc;
5326
5327 case OP_ALLANY:
5328 detect_partial_match(common, backtracks);
5329 #ifdef SUPPORT_UTF
5330 if (common->utf)
5331 {
5332 OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), 0);
5333 OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
5334 #if defined COMPILE_PCRE8 || defined COMPILE_PCRE16
5335 #if defined COMPILE_PCRE8
5336 jump[0] = CMP(SLJIT_C_LESS, TMP1, 0, SLJIT_IMM, 0xc0);
5337 OP1(SLJIT_MOV_UB, TMP1, 0, SLJIT_MEM1(TMP1), (sljit_sw)PRIV(utf8_table4) - 0xc0);
5338 OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, TMP1, 0);
5339 #elif defined COMPILE_PCRE16
5340 jump[0] = CMP(SLJIT_C_LESS, TMP1, 0, SLJIT_IMM, 0xd800);
5341 OP2(SLJIT_AND, TMP1, 0, TMP1, 0, SLJIT_IMM, 0xfc00);
5342 OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0xd800);
5343 OP_FLAGS(SLJIT_MOV, TMP1, 0, SLJIT_UNUSED, 0, SLJIT_C_EQUAL);
5344 OP2(SLJIT_SHL, TMP1, 0, TMP1, 0, SLJIT_IMM, 1);
5345 OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, TMP1, 0);
5346 #endif
5347 JUMPHERE(jump[0]);
5348 #endif /* COMPILE_PCRE[8|16] */
5349 return cc;
5350 }
5351 #endif
5352 OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
5353 return cc;
5354
5355 case OP_ANYBYTE:
5356 detect_partial_match(common, backtracks);
5357 OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
5358 return cc;
5359
5360 #ifdef SUPPORT_UTF
5361 #ifdef SUPPORT_UCP
5362 case OP_NOTPROP:
5363 case OP_PROP:
5364 propdata[0] = XCL_HASPROP;
5365 propdata[1] = type == OP_NOTPROP ? XCL_NOTPROP : XCL_PROP;
5366 propdata[2] = cc[0];
5367 propdata[3] = cc[1];
5368 propdata[4] = XCL_END;
5369 compile_xclass_matchingpath(common, propdata, backtracks);
5370 return cc + 2;
5371 #endif
5372 #endif
5373
5374 case OP_ANYNL:
5375 detect_partial_match(common, backtracks);
5376 read_char_range(common, common->bsr_nlmin, common->bsr_nlmax, FALSE);
5377 jump[0] = CMP(SLJIT_C_NOT_EQUAL, TMP1, 0, SLJIT_IMM, CHAR_CR);
5378 /* We don't need to handle soft partial matching case. */
5379 end_list = NULL;
5380 if (common->mode != JIT_PARTIAL_HARD_COMPILE)
5381 add_jump(compiler, &end_list, CMP(SLJIT_C_GREATER_EQUAL, STR_PTR, 0, STR_END, 0));
5382 else
5383 check_str_end(common, &end_list);
5384 OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), 0);
5385 jump[1] = CMP(SLJIT_C_NOT_EQUAL, TMP1, 0, SLJIT_IMM, CHAR_NL);
5386 OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
5387 jump[2] = JUMP(SLJIT_JUMP);
5388 JUMPHERE(jump[0]);
5389 check_newlinechar(common, common->bsr_nltype, backtracks, FALSE);
5390 set_jumps(end_list, LABEL());
5391 JUMPHERE(jump[1]);
5392 JUMPHERE(jump[2]);
5393 return cc;
5394
5395 case OP_NOT_HSPACE:
5396 case OP_HSPACE:
5397 detect_partial_match(common, backtracks);
5398 read_char_range(common, 0x9, 0x3000, type == OP_NOT_HSPACE);
5399 add_jump(compiler, &common->hspace, JUMP(SLJIT_FAST_CALL));
5400 add_jump(compiler, backtracks, JUMP(type == OP_NOT_HSPACE ? SLJIT_C_NOT_ZERO : SLJIT_C_ZERO));
5401 return cc;
5402
5403 case OP_NOT_VSPACE:
5404 case OP_VSPACE:
5405 detect_partial_match(common, backtracks);
5406 read_char_range(common, 0xa, 0x2029, type == OP_NOT_VSPACE);
5407 add_jump(compiler, &common->vspace, JUMP(SLJIT_FAST_CALL));
5408 add_jump(compiler, backtracks, JUMP(type == OP_NOT_VSPACE ? SLJIT_C_NOT_ZERO : SLJIT_C_ZERO));
5409 return cc;
5410
5411 #ifdef SUPPORT_UCP
5412 case OP_EXTUNI:
5413 detect_partial_match(common, backtracks);
5414 read_char(common);
5415 add_jump(compiler, &common->getucd, JUMP(SLJIT_FAST_CALL));
5416 OP1(SLJIT_MOV, TMP1, 0, SLJIT_IMM, (sljit_sw)PRIV(ucd_records) + SLJIT_OFFSETOF(ucd_record, gbprop));
5417 /* Optimize register allocation: use a real register. */
5418 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), LOCALS0, STACK_TOP, 0);
5419 OP1(SLJIT_MOV_UB, STACK_TOP, 0, SLJIT_MEM2(TMP1, TMP2), 3);
5420
5421 label = LABEL();
5422 jump[0] = CMP(SLJIT_C_GREATER_EQUAL, STR_PTR, 0, STR_END, 0);
5423 OP1(SLJIT_MOV, TMP3, 0, STR_PTR, 0);
5424 read_char(common);
5425 add_jump(compiler, &common->getucd, JUMP(SLJIT_FAST_CALL));
5426 OP1(SLJIT_MOV, TMP1, 0, SLJIT_IMM, (sljit_sw)PRIV(ucd_records) + SLJIT_OFFSETOF(ucd_record, gbprop));
5427 OP1(SLJIT_MOV_UB, TMP2, 0, SLJIT_MEM2(TMP1, TMP2), 3);
5428
5429 OP2(SLJIT_SHL, STACK_TOP, 0, STACK_TOP, 0, SLJIT_IMM, 2);
5430 OP1(SLJIT_MOV_UI, TMP1, 0, SLJIT_MEM1(STACK_TOP), (sljit_sw)PRIV(ucp_gbtable));
5431 OP1(SLJIT_MOV, STACK_TOP, 0, TMP2, 0);
5432 OP2(SLJIT_SHL, TMP2, 0, SLJIT_IMM, 1, TMP2, 0);
5433 OP2(SLJIT_AND | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, TMP2, 0);
5434 JUMPTO(SLJIT_C_NOT_ZERO, label);
5435
5436 OP1(SLJIT_MOV, STR_PTR, 0, TMP3, 0);
5437 JUMPHERE(jump[0]);
5438 OP1(SLJIT_MOV, STACK_TOP, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), LOCALS0);
5439
5440 if (common->mode == JIT_PARTIAL_HARD_COMPILE)
5441 {
5442 jump[0] = CMP(SLJIT_C_LESS, STR_PTR, 0, STR_END, 0);
5443 /* Since we successfully read a char above, partial matching must occure. */
5444 check_partial(common, TRUE);
5445 JUMPHERE(jump[0]);
5446 }
5447 return cc;
5448 #endif
5449
5450 case OP_EODN:
5451 /* Requires rather complex checks. */
5452 jump[0] = CMP(SLJIT_C_GREATER_EQUAL, STR_PTR, 0, STR_END, 0);
5453 if (common->nltype == NLTYPE_FIXED && common->newline > 255)
5454 {
5455 OP2(SLJIT_ADD, TMP2, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(2));
5456 OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(0));
5457 if (common->mode == JIT_COMPILE)
5458 add_jump(compiler, backtracks, CMP(SLJIT_C_NOT_EQUAL, TMP2, 0, STR_END, 0));
5459 else
5460 {
5461 jump[1] = CMP(SLJIT_C_EQUAL, TMP2, 0, STR_END, 0);
5462 OP2(SLJIT_SUB | SLJIT_SET_U, SLJIT_UNUSED, 0, TMP2, 0, STR_END, 0);
5463 OP_FLAGS(SLJIT_MOV, TMP2, 0, SLJIT_UNUSED, 0, SLJIT_C_LESS);
5464 OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, (common->newline >> 8) & 0xff);
5465 OP_FLAGS(SLJIT_OR | SLJIT_SET_E, TMP2, 0, TMP2, 0, SLJIT_C_NOT_EQUAL);
5466 add_jump(compiler, backtracks, JUMP(SLJIT_C_NOT_EQUAL));
5467 check_partial(common, TRUE);
5468 add_jump(compiler, backtracks, JUMP(SLJIT_JUMP));
5469 JUMPHERE(jump[1]);
5470 }
5471 OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(1));
5472 add_jump(compiler, backtracks, CMP(SLJIT_C_NOT_EQUAL, TMP1, 0, SLJIT_IMM, (common->newline >> 8) & 0xff));
5473 add_jump(compiler, backtracks, CMP(SLJIT_C_NOT_EQUAL, TMP2, 0, SLJIT_IMM, common->newline & 0xff));
5474 }
5475 else if (common->nltype == NLTYPE_FIXED)
5476 {
5477 OP2(SLJIT_ADD, TMP2, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
5478 OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(0));
5479 add_jump(compiler, backtracks, CMP(SLJIT_C_NOT_EQUAL, TMP2, 0, STR_END, 0));
5480 add_jump(compiler, backtracks, CMP(SLJIT_C_NOT_EQUAL, TMP1, 0, SLJIT_IMM, common->newline));
5481 }
5482 else
5483 {
5484 OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(0));
5485 jump[1] = CMP(SLJIT_C_NOT_EQUAL, TMP1, 0, SLJIT_IMM, CHAR_CR);
5486 OP2(SLJIT_ADD, TMP2, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(2));
5487 OP2(SLJIT_SUB | SLJIT_SET_U, SLJIT_UNUSED, 0, TMP2, 0, STR_END, 0);
5488 jump[2] = JUMP(SLJIT_C_GREATER);
5489 add_jump(compiler, backtracks, JUMP(SLJIT_C_LESS));
5490 /* Equal. */
5491 OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(1));
5492 jump[3] = CMP(SLJIT_C_EQUAL, TMP1, 0, SLJIT_IMM, CHAR_NL);
5493 add_jump(compiler, backtracks, JUMP(SLJIT_JUMP));
5494
5495 JUMPHERE(jump[1]);
5496 if (common->nltype == NLTYPE_ANYCRLF)
5497 {
5498 OP2(SLJIT_ADD, TMP2, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
5499 add_jump(compiler, backtracks, CMP(SLJIT_C_LESS, TMP2, 0, STR_END, 0));
5500 add_jump(compiler, backtracks, CMP(SLJIT_C_NOT_EQUAL, TMP1, 0, SLJIT_IMM, CHAR_NL));
5501 }
5502 else
5503 {
5504 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), LOCALS1, STR_PTR, 0);
5505 read_char_range(common, common->nlmin, common->nlmax, TRUE);
5506 add_jump(compiler, backtracks, CMP(SLJIT_C_NOT_EQUAL, STR_PTR, 0, STR_END, 0));
5507 add_jump(compiler, &common->anynewline, JUMP(SLJIT_FAST_CALL));
5508 add_jump(compiler, backtracks, JUMP(SLJIT_C_ZERO));
5509 OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), LOCALS1);
5510 }
5511 JUMPHERE(jump[2]);
5512 JUMPHERE(jump[3]);
5513 }
5514 JUMPHERE(jump[0]);
5515 check_partial(common, FALSE);
5516 return cc;
5517
5518 case OP_EOD:
5519 add_jump(compiler, backtracks, CMP(SLJIT_C_LESS, STR_PTR, 0, STR_END, 0));
5520 check_partial(common, FALSE);
5521 return cc;
5522
5523 case OP_CIRC:
5524 OP1(SLJIT_MOV, TMP2, 0, ARGUMENTS, 0);
5525 OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(TMP2), SLJIT_OFFSETOF(jit_arguments, begin));
5526 add_jump(compiler, backtracks, CMP(SLJIT_C_GREATER, STR_PTR, 0, TMP1, 0));
5527 OP1(SLJIT_MOV_UB, TMP2, 0, SLJIT_MEM1(TMP2), SLJIT_OFFSETOF(jit_arguments, notbol));
5528 add_jump(compiler, backtracks, CMP(SLJIT_C_NOT_EQUAL, TMP2, 0, SLJIT_IMM, 0));
5529 return cc;
5530
5531 case OP_CIRCM:
5532 OP1(SLJIT_MOV, TMP2, 0, ARGUMENTS, 0);
5533 OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(TMP2), SLJIT_OFFSETOF(jit_arguments, begin));
5534 jump[1] = CMP(SLJIT_C_GREATER, STR_PTR, 0, TMP1, 0);
5535 OP1(SLJIT_MOV_UB, TMP2, 0, SLJIT_MEM1(TMP2), SLJIT_OFFSETOF(jit_arguments, notbol));
5536 add_jump(compiler, backtracks, CMP(SLJIT_C_NOT_EQUAL, TMP2, 0, SLJIT_IMM, 0));
5537 jump[0] = JUMP(SLJIT_JUMP);
5538 JUMPHERE(jump[1]);
5539
5540 add_jump(compiler, backtracks, CMP(SLJIT_C_GREATER_EQUAL, STR_PTR, 0, STR_END, 0));
5541 if (common->nltype == NLTYPE_FIXED && common->newline > 255)
5542 {
5543 OP2(SLJIT_SUB, TMP2, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(2));
5544 add_jump(compiler, backtracks, CMP(SLJIT_C_LESS, TMP2, 0, TMP1, 0));
5545 OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(-2));
5546 OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(-1));
5547 add_jump(compiler, backtracks, CMP(SLJIT_C_NOT_EQUAL, TMP1, 0, SLJIT_IMM, (common->newline >> 8) & 0xff));
5548 add_jump(compiler, backtracks, CMP(SLJIT_C_NOT_EQUAL, TMP2, 0, SLJIT_IMM, common->newline & 0xff));
5549 }
5550 else
5551 {
5552 skip_char_back(common);
5553 read_char_range(common, common->nlmin, common->nlmax, TRUE);
5554 check_newlinechar(common, common->nltype, backtracks, FALSE);
5555 }
5556 JUMPHERE(jump[0]);
5557 return cc;
5558
5559 case OP_DOLL:
5560 OP1(SLJIT_MOV, TMP2, 0, ARGUMENTS, 0);
5561 OP1(SLJIT_MOV_UB, TMP2, 0, SLJIT_MEM1(TMP2), SLJIT_OFFSETOF(jit_arguments, noteol));
5562 add_jump(compiler, backtracks, CMP(SLJIT_C_NOT_EQUAL, TMP2, 0, SLJIT_IMM, 0));
5563
5564 if (!common->endonly)
5565 compile_char1_matchingpath(common, OP_EODN, cc, backtracks);
5566 else
5567 {
5568 add_jump(compiler, backtracks, CMP(SLJIT_C_LESS, STR_PTR, 0, STR_END, 0));
5569 check_partial(common, FALSE);
5570 }
5571 return cc;
5572
5573 case OP_DOLLM:
5574 jump[1] = CMP(SLJIT_C_LESS, STR_PTR, 0, STR_END, 0);
5575 OP1(SLJIT_MOV, TMP2, 0, ARGUMENTS, 0);
5576 OP1(SLJIT_MOV_UB, TMP2, 0, SLJIT_MEM1(TMP2), SLJIT_OFFSETOF(jit_arguments, noteol));
5577 add_jump(compiler, backtracks, CMP(SLJIT_C_NOT_EQUAL, TMP2, 0, SLJIT_IMM, 0));
5578 check_partial(common, FALSE);
5579 jump[0] = JUMP(SLJIT_JUMP);
5580 JUMPHERE(jump[1]);
5581
5582 if (common->nltype == NLTYPE_FIXED && common->newline > 255)
5583 {
5584 OP2(SLJIT_ADD, TMP2, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(2));
5585 OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(0));
5586 if (common->mode == JIT_COMPILE)
5587 add_jump(compiler, backtracks, CMP(SLJIT_C_GREATER, TMP2, 0, STR_END, 0));
5588 else
5589 {
5590 jump[1] = CMP(SLJIT_C_LESS_EQUAL, TMP2, 0, STR_END, 0);
5591 /* STR_PTR = STR_END - IN_UCHARS(1) */
5592 add_jump(compiler, backtracks, CMP(SLJIT_C_NOT_EQUAL, TMP1, 0, SLJIT_IMM, (common->newline >> 8) & 0xff));
5593 check_partial(common, TRUE);
5594 add_jump(compiler, backtracks, JUMP(SLJIT_JUMP));
5595 JUMPHERE(jump[1]);
5596 }
5597
5598 OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(1));
5599 add_jump(compiler, backtracks, CMP(SLJIT_C_NOT_EQUAL, TMP1, 0, SLJIT_IMM, (common->newline >> 8) & 0xff));
5600 add_jump(compiler, backtracks, CMP(SLJIT_C_NOT_EQUAL, TMP2, 0, SLJIT_IMM, common->newline & 0xff));
5601 }
5602 else
5603 {
5604 peek_char(common, common->nlmax);
5605 check_newlinechar(common, common->nltype, backtracks, FALSE);
5606 }
5607 JUMPHERE(jump[0]);
5608 return cc;
5609
5610 case OP_CHAR:
5611 case OP_CHARI:
5612 length = 1;
5613 #ifdef SUPPORT_UTF
5614 if (common->utf && HAS_EXTRALEN(*cc)) length += GET_EXTRALEN(*cc);
5615 #endif
5616 if (common->mode == JIT_COMPILE && (type == OP_CHAR || !char_has_othercase(common, cc) || char_get_othercase_bit(common, cc) != 0))
5617 {
5618 OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(length));
5619 add_jump(compiler, backtracks, CMP(SLJIT_C_GREATER, STR_PTR, 0, STR_END, 0));
5620
5621 context.length = IN_UCHARS(length);
5622 context.sourcereg = -1;
5623 #if defined SLJIT_UNALIGNED && SLJIT_UNALIGNED
5624 context.ucharptr = 0;
5625 #endif
5626 return byte_sequence_compare(common, type == OP_CHARI, cc, &context, backtracks);
5627 }
5628
5629 detect_partial_match(common, backtracks);
5630 #ifdef SUPPORT_UTF
5631 if (common->utf)
5632 {
5633 GETCHAR(c, cc);
5634 }
5635 else
5636 #endif
5637 c = *cc;
5638
5639 if (type == OP_CHAR || !char_has_othercase(common, cc))
5640 {
5641 read_char_range(common, c, c, FALSE);
5642 add_jump(compiler, backtracks, CMP(SLJIT_C_NOT_EQUAL, TMP1, 0, SLJIT_IMM, c));
5643 return cc + length;
5644 }
5645 oc = char_othercase(common, c);
5646 read_char_range(common, c < oc ? c : oc, c > oc ? c : oc, FALSE);
5647 bit = c ^ oc;
5648 if (is_powerof2(bit))
5649 {
5650 OP2(SLJIT_OR, TMP1, 0, TMP1, 0, SLJIT_IMM, bit);
5651 add_jump(compiler, backtracks, CMP(SLJIT_C_NOT_EQUAL, TMP1, 0, SLJIT_IMM, c | bit));
5652 return cc + length;
5653 }
5654 jump[0] = CMP(SLJIT_C_EQUAL, TMP1, 0, SLJIT_IMM, c);
5655 add_jump(compiler, backtracks, CMP(SLJIT_C_NOT_EQUAL, TMP1, 0, SLJIT_IMM, oc));
5656 JUMPHERE(jump[0]);
5657 return cc + length;
5658
5659 case OP_NOT:
5660 case OP_NOTI:
5661 detect_partial_match(common, backtracks);
5662 length = 1;
5663 #ifdef SUPPORT_UTF
5664 if (common->utf)
5665 {
5666 #ifdef COMPILE_PCRE8
5667 c = *cc;
5668 if (c < 128)
5669 {
5670 OP1(SLJIT_MOV_UB, TMP1, 0, SLJIT_MEM1(STR_PTR), 0);
5671 if (type == OP_NOT || !char_has_othercase(common, cc))
5672 add_jump(compiler, backtracks, CMP(SLJIT_C_EQUAL, TMP1, 0, SLJIT_IMM, c));
5673 else
5674 {
5675 /* Since UTF8 code page is fixed, we know that c is in [a-z] or [A-Z] range. */
5676 OP2(SLJIT_OR, TMP2, 0, TMP1, 0, SLJIT_IMM, 0x20);
5677 add_jump(compiler, backtracks, CMP(SLJIT_C_EQUAL, TMP2, 0, SLJIT_IMM, c | 0x20));
5678 }
5679 /* Skip the variable-length character. */
5680 OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
5681 jump[0] = CMP(SLJIT_C_LESS, TMP1, 0, SLJIT_IMM, 0xc0);
5682 OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(TMP1), (sljit_sw)PRIV(utf8_table4) - 0xc0);
5683 OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, TMP1, 0);
5684 JUMPHERE(jump[0]);
5685 return cc + 1;
5686 }
5687 else
5688 #endif /* COMPILE_PCRE8 */
5689 {
5690 GETCHARLEN(c, cc, length);
5691 }
5692 }
5693 else
5694 #endif /* SUPPORT_UTF */
5695 c = *cc;
5696
5697 if (type == OP_NOT || !char_has_othercase(common, cc))
5698 {
5699 read_char_range(common, c, c, TRUE);
5700 add_jump(compiler, backtracks, CMP(SLJIT_C_EQUAL, TMP1, 0, SLJIT_IMM, c));
5701 }
5702 else
5703 {
5704 oc = char_othercase(common, c);
5705 read_char_range(common, c < oc ? c : oc, c > oc ? c : oc, TRUE);
5706 bit = c ^ oc;
5707 if (is_powerof2(bit))
5708 {
5709 OP2(SLJIT_OR, TMP1, 0, TMP1, 0, SLJIT_IMM, bit);
5710 add_jump(compiler, backtracks, CMP(SLJIT_C_EQUAL, TMP1, 0, SLJIT_IMM, c | bit));
5711 }
5712 else
5713 {
5714 add_jump(compiler, backtracks, CMP(SLJIT_C_EQUAL, TMP1, 0, SLJIT_IMM, c));
5715 add_jump(compiler, backtracks, CMP(SLJIT_C_EQUAL, TMP1, 0, SLJIT_IMM, oc));
5716 }
5717 }
5718 return cc + length;
5719
5720 case OP_CLASS:
5721 case OP_NCLASS:
5722 detect_partial_match(common, backtracks);
5723
5724 #if defined SUPPORT_UTF && defined COMPILE_PCRE8
5725 bit = (common->utf && is_char7_bitset((const pcre_uint8 *)cc, type == OP_NCLASS)) ? 127 : 255;
5726 read_char_range(common, 0, bit, type == OP_NCLASS);
5727 #else
5728 read_char_range(common, 0, 255, type == OP_NCLASS);
5729 #endif
5730
5731 if (check_class_ranges(common, (const pcre_uint8 *)cc, type == OP_NCLASS, FALSE, backtracks))
5732 return cc + 32 / sizeof(pcre_uchar);
5733
5734 #if defined SUPPORT_UTF && defined COMPILE_PCRE8
5735 jump[0] = NULL;
5736 if (common->utf)
5737 {
5738 jump[0] = CMP(SLJIT_C_GREATER, TMP1, 0, SLJIT_IMM, bit);
5739 if (type == OP_CLASS)
5740 {
5741 add_jump(compiler, backtracks, jump[0]);
5742 jump[0] = NULL;
5743 }
5744 }
5745 #elif !defined COMPILE_PCRE8
5746 jump[0] = CMP(SLJIT_C_GREATER, TMP1, 0, SLJIT_IMM, 255);
5747 if (type == OP_CLASS)
5748 {
5749 add_jump(compiler, backtracks, jump[0]);
5750 jump[0] = NULL;
5751 }
5752 #endif /* SUPPORT_UTF && COMPILE_PCRE8 */
5753
5754 OP2(SLJIT_AND, TMP2, 0, TMP1, 0, SLJIT_IMM, 0x7);
5755 OP2(SLJIT_LSHR, TMP1, 0, TMP1, 0, SLJIT_IMM, 3);
5756 OP1(SLJIT_MOV_UB, TMP1, 0, SLJIT_MEM1(TMP1), (sljit_sw)cc);
5757 OP2(SLJIT_SHL, TMP2, 0, SLJIT_IMM, 1, TMP2, 0);
5758 OP2(SLJIT_AND | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, TMP2, 0);
5759 add_jump(compiler, backtracks, JUMP(SLJIT_C_ZERO));
5760
5761 #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
5762 if (jump[0] != NULL)
5763 JUMPHERE(jump[0]);
5764 #endif
5765
5766 return cc + 32 / sizeof(pcre_uchar);
5767
5768 #if defined SUPPORT_UTF || defined COMPILE_PCRE16 || defined COMPILE_PCRE32
5769 case OP_XCLASS:
5770 compile_xclass_matchingpath(common, cc + LINK_SIZE, backtracks);
5771 return cc + GET(cc, 0) - 1;
5772 #endif
5773
5774 case OP_REVERSE:
5775 length = GET(cc, 0);
5776 if (length == 0)
5777 return cc + LINK_SIZE;
5778 OP1(SLJIT_MOV, TMP1, 0, ARGUMENTS, 0);
5779 #ifdef SUPPORT_UTF
5780 if (common->utf)
5781 {
5782 OP1(SLJIT_MOV, TMP3, 0, SLJIT_MEM1(TMP1), SLJIT_OFFSETOF(jit_arguments, begin));
5783 OP1(SLJIT_MOV, TMP2, 0, SLJIT_IMM, length);
5784 label = LABEL();
5785 add_jump(compiler, backtracks, CMP(SLJIT_C_LESS_EQUAL, STR_PTR, 0, TMP3, 0));
5786 skip_char_back(common);
5787 OP2(SLJIT_SUB | SLJIT_SET_E, TMP2, 0, TMP2, 0, SLJIT_IMM, 1);
5788 JUMPTO(SLJIT_C_NOT_ZERO, label);
5789 }
5790 else
5791 #endif
5792 {
5793 OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(TMP1), SLJIT_OFFSETOF(jit_arguments, begin));
5794 OP2(SLJIT_SUB, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(length));
5795 add_jump(compiler, backtracks, CMP(SLJIT_C_LESS, STR_PTR, 0, TMP1, 0));
5796 }
5797 check_start_used_ptr(common);
5798 return cc + LINK_SIZE;
5799 }
5800 SLJIT_ASSERT_STOP();
5801 return cc;
5802 }
5803
5804 static SLJIT_INLINE pcre_uchar *compile_charn_matchingpath(compiler_common *common, pcre_uchar *cc, pcre_uchar *ccend, jump_list **backtracks)
5805 {
5806 /* This function consumes at least one input character. */
5807 /* To decrease the number of length checks, we try to concatenate the fixed length character sequences. */
5808 DEFINE_COMPILER;
5809 pcre_uchar *ccbegin = cc;
5810 compare_context context;
5811 int size;
5812
5813 context.length = 0;
5814 do
5815 {
5816 if (cc >= ccend)
5817 break;
5818
5819 if (*cc == OP_CHAR)
5820 {
5821 size = 1;
5822 #ifdef SUPPORT_UTF
5823 if (common->utf && HAS_EXTRALEN(cc[1]))
5824 size += GET_EXTRALEN(cc[1]);
5825 #endif
5826 }
5827 else if (*cc == OP_CHARI)
5828 {
5829 size = 1;
5830 #ifdef SUPPORT_UTF
5831 if (common->utf)
5832 {
5833 if (char_has_othercase(common, cc + 1) && char_get_othercase_bit(common, cc + 1) == 0)
5834 size = 0;
5835 else if (HAS_EXTRALEN(cc[1]))
5836 size += GET_EXTRALEN(cc[1]);
5837 }
5838 else
5839 #endif
5840 if (char_has_othercase(common, cc + 1) && char_get_othercase_bit(common, cc + 1) == 0)
5841 size = 0;
5842 }
5843 else
5844 size = 0;
5845
5846 cc += 1 + size;
5847 context.length += IN_UCHARS(size);
5848 }
5849 while (size > 0 && context.length <= 128);
5850
5851 cc = ccbegin;
5852 if (context.length > 0)
5853 {
5854 /* We have a fixed-length byte sequence. */
5855 OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, context.length);
5856 add_jump(compiler, backtracks, CMP(SLJIT_C_GREATER, STR_PTR, 0, STR_END, 0));
5857
5858 context.sourcereg = -1;
5859 #if defined SLJIT_UNALIGNED && SLJIT_UNALIGNED
5860 context.ucharptr = 0;
5861 #endif
5862 do cc = byte_sequence_compare(common, *cc == OP_CHARI, cc + 1, &context, backtracks); while (context.length > 0);
5863 return cc;
5864 }
5865
5866 /* A non-fixed length character will be checked if length == 0. */
5867 return compile_char1_matchingpath(common, *cc, cc + 1, backtracks);
5868 }
5869
5870 /* Forward definitions. */
5871 static void compile_matchingpath(compiler_common *, pcre_uchar *, pcre_uchar *, backtrack_common *);
5872 static void compile_backtrackingpath(compiler_common *, struct backtrack_common *);
5873
5874 #define PUSH_BACKTRACK(size, ccstart, error) \
5875 do \
5876 { \
5877 backtrack = sljit_alloc_memory(compiler, (size)); \
5878 if (SLJIT_UNLIKELY(sljit_get_compiler_error(compiler))) \
5879 return error; \
5880 memset(backtrack, 0, size); \
5881 backtrack->prev = parent->top; \
5882 backtrack->cc = (ccstart); \
5883 parent->top = backtrack; \
5884 } \
5885 while (0)
5886
5887 #define PUSH_BACKTRACK_NOVALUE(size, ccstart) \
5888 do \
5889 { \
5890 backtrack = sljit_alloc_memory(compiler, (size)); \
5891 if (SLJIT_UNLIKELY(sljit_get_compiler_error(compiler))) \
5892 return; \
5893 memset(backtrack, 0, size); \
5894 backtrack->prev = parent->top; \
5895 backtrack->cc = (ccstart); \
5896 parent->top = backtrack; \
5897 } \
5898 while (0)
5899
5900 #define BACKTRACK_AS(type) ((type *)backtrack)
5901
5902 static void compile_dnref_search(compiler_common *common, pcre_uchar *cc, jump_list **backtracks)
5903 {
5904 /* The OVECTOR offset goes to TMP2. */
5905 DEFINE_COMPILER;
5906 int count = GET2(cc, 1 + IMM2_SIZE);
5907 pcre_uchar *slot = common->name_table + GET2(cc, 1) * common->name_entry_size;
5908 unsigned int offset;
5909 jump_list *found = NULL;
5910
5911 SLJIT_ASSERT(*cc == OP_DNREF || *cc == OP_DNREFI);
5912
5913 OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(1));
5914
5915 count--;
5916 while (count-- > 0)
5917 {
5918 offset = GET2(slot, 0) << 1;
5919 GET_LOCAL_BASE(TMP2, 0, OVECTOR(offset));
5920 add_jump(compiler, &found, CMP(SLJIT_C_NOT_EQUAL, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(offset), TMP1, 0));
5921 slot += common->name_entry_size;
5922 }
5923
5924 offset = GET2(slot, 0) << 1;
5925 GET_LOCAL_BASE(TMP2, 0, OVECTOR(offset));
5926 if (backtracks != NULL && !common->jscript_compat)
5927 add_jump(compiler, backtracks, CMP(SLJIT_C_EQUAL, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(offset), TMP1, 0));
5928
5929 set_jumps(found, LABEL());
5930 }
5931
5932 static void compile_ref_matchingpath(compiler_common *common, pcre_uchar *cc, jump_list **backtracks, BOOL withchecks, BOOL emptyfail)
5933 {
5934 DEFINE_COMPILER;
5935 BOOL ref = (*cc == OP_REF || *cc == OP_REFI);
5936 int offset = 0;
5937 struct sljit_jump *jump = NULL;
5938 struct sljit_jump *partial;
5939 struct sljit_jump *nopartial;
5940
5941 if (ref)
5942 {
5943 offset = GET2(cc, 1) << 1;
5944 OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(offset));
5945 /* OVECTOR(1) contains the "string begin - 1" constant. */
5946 if (withchecks && !common->jscript_compat)
5947 add_jump(compiler, backtracks, CMP(SLJIT_C_EQUAL, TMP1, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(1)));
5948 }
5949 else
5950 OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(TMP2), 0);
5951
5952 #if defined SUPPORT_UTF && defined SUPPORT_UCP
5953 if (common->utf && *cc == OP_REFI)
5954 {
5955 SLJIT_ASSERT(TMP1 == SLJIT_SCRATCH_REG1 && STACK_TOP == SLJIT_SCRATCH_REG2 && TMP2 == SLJIT_SCRATCH_REG3);
5956 if (ref)
5957 OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(offset + 1));
5958 else
5959 OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(TMP2), sizeof(sljit_sw));
5960
5961 if (withchecks)
5962 jump = CMP(SLJIT_C_EQUAL, TMP1, 0, TMP2, 0);
5963
5964 /* Needed to save important temporary registers. */
5965 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), LOCALS0, STACK_TOP, 0);
5966 OP1(SLJIT_MOV, SLJIT_SCRATCH_REG2, 0, ARGUMENTS, 0);
5967 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SCRATCH_REG2), SLJIT_OFFSETOF(jit_arguments, uchar_ptr), STR_PTR, 0);
5968 sljit_emit_ijump(compiler, SLJIT_CALL3, SLJIT_IMM, SLJIT_FUNC_OFFSET(do_utf_caselesscmp));
5969 OP1(SLJIT_MOV, STACK_TOP, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), LOCALS0);
5970 if (common->mode == JIT_COMPILE)
5971 add_jump(compiler, backtracks, CMP(SLJIT_C_LESS_EQUAL, SLJIT_RETURN_REG, 0, SLJIT_IMM, 1));
5972 else
5973 {
5974 add_jump(compiler, backtracks, CMP(SLJIT_C_EQUAL, SLJIT_RETURN_REG, 0, SLJIT_IMM, 0));
5975 nopartial = CMP(SLJIT_C_NOT_EQUAL, SLJIT_RETURN_REG, 0, SLJIT_IMM, 1);
5976 check_partial(common, FALSE);
5977 add_jump(compiler, backtracks, JUMP(SLJIT_JUMP));
5978 JUMPHERE(nopartial);
5979 }
5980 OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_RETURN_REG, 0);
5981 }
5982 else
5983 #endif /* SUPPORT_UTF && SUPPORT_UCP */
5984 {
5985 if (ref)
5986 OP2(SLJIT_SUB | SLJIT_SET_E, TMP2, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(offset + 1), TMP1, 0);
5987 else
5988 OP2(SLJIT_SUB | SLJIT_SET_E, TMP2, 0, SLJIT_MEM1(TMP2), sizeof(sljit_sw), TMP1, 0);
5989
5990 if (withchecks)
5991 jump = JUMP(SLJIT_C_ZERO);
5992
5993 OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, TMP2, 0);
5994 partial = CMP(SLJIT_C_GREATER, STR_PTR, 0, STR_END, 0);
5995 if (common->mode == JIT_COMPILE)
5996 add_jump(compiler, backtracks, partial);
5997
5998 add_jump(compiler, *cc == OP_REF ? &common->casefulcmp : &common->caselesscmp, JUMP(SLJIT_FAST_CALL));
5999 add_jump(compiler, backtracks, CMP(SLJIT_C_NOT_EQUAL, TMP2, 0, SLJIT_IMM, 0));
6000
6001 if (common->mode != JIT_COMPILE)
6002 {
6003 nopartial = JUMP(SLJIT_JUMP);
6004 JUMPHERE(partial);
6005 /* TMP2 -= STR_END - STR_PTR */
6006 OP2(SLJIT_SUB, TMP2, 0, TMP2, 0, STR_PTR, 0);
6007 OP2(SLJIT_ADD, TMP2, 0, TMP2, 0, STR_END, 0);
6008 partial = CMP(SLJIT_C_EQUAL, TMP2, 0, SLJIT_IMM, 0);
6009 OP1(SLJIT_MOV, STR_PTR, 0, STR_END, 0);
6010 add_jump(compiler, *cc == OP_REF ? &common->casefulcmp : &common->caselesscmp, JUMP(SLJIT_FAST_CALL));
6011 add_jump(compiler, backtracks, CMP(SLJIT_C_NOT_EQUAL, TMP2, 0, SLJIT_IMM, 0));
6012 JUMPHERE(partial);
6013 check_partial(common, FALSE);
6014 add_jump(compiler, backtracks, JUMP(SLJIT_JUMP));
6015 JUMPHERE(nopartial);
6016 }
6017 }
6018
6019 if (jump != NULL)
6020 {
6021 if (emptyfail)
6022 add_jump(compiler, backtracks, jump);
6023 else
6024 JUMPHERE(jump);
6025 }
6026 }
6027
6028 static SLJIT_INLINE pcre_uchar *compile_ref_iterator_matchingpath(compiler_common *common, pcre_uchar *cc, backtrack_common *parent)
6029 {
6030 DEFINE_COMPILER;
6031 BOOL ref = (*cc == OP_REF || *cc == OP_REFI);
6032 backtrack_common *backtrack;
6033 pcre_uchar type;
6034 int offset = 0;
6035 struct sljit_label *label;
6036 struct sljit_jump *zerolength;
6037 struct sljit_jump *jump = NULL;
6038 pcre_uchar *ccbegin = cc;
6039 int min = 0, max = 0;
6040 BOOL minimize;
6041
6042 PUSH_BACKTRACK(sizeof(iterator_backtrack), cc, NULL);
6043
6044 if (ref)
6045 offset = GET2(cc, 1) << 1;
6046 else
6047 cc += IMM2_SIZE;
6048 type = cc[1 + IMM2_SIZE];
6049
6050 SLJIT_COMPILE_ASSERT((OP_CRSTAR & 0x1) == 0, crstar_opcode_must_be_even);
6051 minimize = (type & 0x1) != 0;
6052 switch(type)
6053 {
6054 case OP_CRSTAR:
6055 case OP_CRMINSTAR:
6056 min = 0;
6057 max = 0;
6058 cc += 1 + IMM2_SIZE + 1;
6059 break;
6060 case OP_CRPLUS:
6061 case OP_CRMINPLUS:
6062 min = 1;
6063 max = 0;
6064 cc += 1 + IMM2_SIZE + 1;
6065 break;
6066 case OP_CRQUERY:
6067 case OP_CRMINQUERY:
6068 min = 0;
6069 max = 1;
6070 cc += 1 + IMM2_SIZE + 1;
6071 break;
6072 case OP_CRRANGE:
6073 case OP_CRMINRANGE:
6074 min = GET2(cc, 1 + IMM2_SIZE + 1);
6075 max = GET2(cc, 1 + IMM2_SIZE + 1 + IMM2_SIZE);
6076 cc += 1 + IMM2_SIZE + 1 + 2 * IMM2_SIZE;
6077 break;
6078 default:
6079 SLJIT_ASSERT_STOP();
6080 break;
6081 }
6082
6083 if (!minimize)
6084 {
6085 if (min == 0)
6086 {
6087 allocate_stack(common, 2);
6088 if (ref)
6089 OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(offset));
6090 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), STR_PTR, 0);
6091 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(1), SLJIT_IMM, 0);
6092 /* Temporary release of STR_PTR. */
6093 OP2(SLJIT_SUB, STACK_TOP, 0, STACK_TOP, 0, SLJIT_IMM, sizeof(sljit_sw));
6094 /* Handles both invalid and empty cases. Since the minimum repeat,
6095 is zero the invalid case is basically the same as an empty case. */
6096 if (ref)
6097 zerolength = CMP(SLJIT_C_EQUAL, TMP1, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(offset + 1));
6098 else
6099 {
6100 compile_dnref_search(common, ccbegin, NULL);
6101 OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(TMP2), 0);
6102 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), POSSESSIVE1, TMP2, 0);
6103 zerolength = CMP(SLJIT_C_EQUAL, TMP1, 0, SLJIT_MEM1(TMP2), sizeof(sljit_sw));
6104 }
6105 /* Restore if not zero length. */
6106 OP2(SLJIT_ADD, STACK_TOP, 0, STACK_TOP, 0, SLJIT_IMM, sizeof(sljit_sw));
6107 }
6108 else
6109 {
6110 allocate_stack(common, 1);
6111 if (ref)
6112 OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(offset));
6113 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), SLJIT_IMM, 0);
6114 if (ref)
6115 {
6116 add_jump(compiler, &backtrack->topbacktracks, CMP(SLJIT_C_EQUAL, TMP1, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(1)));
6117 zerolength = CMP(SLJIT_C_EQUAL, TMP1, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(offset + 1));
6118 }
6119 else
6120 {
6121 compile_dnref_search(common, ccbegin, &backtrack->topbacktracks);
6122 OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(TMP2), 0);
6123 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), POSSESSIVE1, TMP2, 0);
6124 zerolength = CMP(SLJIT_C_EQUAL, TMP1, 0, SLJIT_MEM1(TMP2), sizeof(sljit_sw));
6125 }
6126 }
6127
6128 if (min > 1 || max > 1)
6129 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), POSSESSIVE0, SLJIT_IMM, 0);
6130
6131 label = LABEL();
6132 if (!ref)
6133 OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), POSSESSIVE1);
6134 compile_ref_matchingpath(common, ccbegin, &backtrack->topbacktracks, FALSE, FALSE);
6135
6136 if (min > 1 || max > 1)
6137 {
6138 OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), POSSESSIVE0);
6139 OP2(SLJIT_ADD, TMP1, 0, TMP1, 0, SLJIT_IMM, 1);
6140 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), POSSESSIVE0, TMP1, 0);
6141 if (min > 1)
6142 CMPTO(SLJIT_C_LESS, TMP1, 0, SLJIT_IMM, min, label);
6143 if (max > 1)
6144 {
6145 jump = CMP(SLJIT_C_GREATER_EQUAL, TMP1, 0, SLJIT_IMM, max);
6146 allocate_stack(common, 1);
6147 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), STR_PTR, 0);
6148 JUMPTO(SLJIT_JUMP, label);
6149 JUMPHERE(jump);
6150 }
6151 }
6152
6153 if (max == 0)
6154 {
6155 /* Includes min > 1 case as well. */
6156 allocate_stack(common, 1);
6157 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), STR_PTR, 0);
6158 JUMPTO(SLJIT_JUMP, label);
6159 }
6160
6161 JUMPHERE(zerolength);
6162 BACKTRACK_AS(iterator_backtrack)->matchingpath = LABEL();
6163
6164 count_match(common);
6165 return cc;
6166 }
6167
6168 allocate_stack(common, ref ? 2 : 3);
6169 if (ref)
6170 OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(offset));
6171 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), SLJIT_IMM, 0);
6172 if (type != OP_CRMINSTAR)
6173 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(1), SLJIT_IMM, 0);
6174
6175 if (min == 0)
6176 {
6177 /* Handles both invalid and empty cases. Since the minimum repeat,
6178 is zero the invalid case is basically the same as an empty case. */
6179 if (ref)
6180 zerolength = CMP(SLJIT_C_EQUAL, TMP1, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(offset + 1));
6181 else
6182 {
6183 compile_dnref_search(common, ccbegin, NULL);
6184 OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(TMP2), 0);
6185 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(2), TMP2, 0);
6186 zerolength = CMP(SLJIT_C_EQUAL, TMP1, 0, SLJIT_MEM1(TMP2), sizeof(sljit_sw));
6187 }
6188 /* Length is non-zero, we can match real repeats. */
6189 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), STR_PTR, 0);
6190 jump = JUMP(SLJIT_JUMP);
6191 }
6192 else
6193 {
6194 if (ref)
6195 {
6196 add_jump(compiler, &backtrack->topbacktracks, CMP(SLJIT_C_EQUAL, TMP1, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(1)));
6197 zerolength = CMP(SLJIT_C_EQUAL, TMP1, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(offset + 1));
6198 }
6199 else
6200 {
6201 compile_dnref_search(common, ccbegin, &backtrack->topbacktracks);
6202 OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(TMP2), 0);
6203 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(2), TMP2, 0);
6204 zerolength = CMP(SLJIT_C_EQUAL, TMP1, 0, SLJIT_MEM1(TMP2), sizeof(sljit_sw));
6205 }
6206 }
6207
6208 BACKTRACK_AS(iterator_backtrack)->matchingpath = LABEL();
6209 if (max > 0)
6210 add_jump(compiler, &backtrack->topbacktracks, CMP(SLJIT_C_GREATER_EQUAL, SLJIT_MEM1(STACK_TOP), STACK(1), SLJIT_IMM, max));
6211
6212 if (!ref)
6213 OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(STACK_TOP), STACK(2));
6214 compile_ref_matchingpath(common, ccbegin, &backtrack->topbacktracks, TRUE, TRUE);
6215 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), STR_PTR, 0);
6216
6217 if (min > 1)
6218 {
6219 OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(STACK_TOP), STACK(1));
6220 OP2(SLJIT_ADD, TMP1, 0, TMP1, 0, SLJIT_IMM, 1);
6221 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(1), TMP1, 0);
6222 CMPTO(SLJIT_C_LESS, TMP1, 0, SLJIT_IMM, min, BACKTRACK_AS(iterator_backtrack)->matchingpath);
6223 }
6224 else if (max > 0)
6225 OP2(SLJIT_ADD, SLJIT_MEM1(STACK_TOP), STACK(1), SLJIT_MEM1(STACK_TOP), STACK(1), SLJIT_IMM, 1);
6226
6227 if (jump != NULL)
6228 JUMPHERE(jump);
6229 JUMPHERE(zerolength);
6230
6231 count_match(common);
6232 return cc;
6233 }
6234
6235 static SLJIT_INLINE pcre_uchar *compile_recurse_matchingpath(compiler_common *common, pcre_uchar *cc, backtrack_common *parent)
6236 {
6237 DEFINE_COMPILER;
6238 backtrack_common *backtrack;
6239 recurse_entry *entry = common->entries;
6240 recurse_entry *prev = NULL;
6241 sljit_sw start = GET(cc, 1);
6242 pcre_uchar *start_cc;
6243 BOOL needs_control_head;
6244
6245 PUSH_BACKTRACK(sizeof(recurse_backtrack), cc, NULL);
6246
6247 /* Inlining simple patterns. */
6248 if (get_framesize(common, common->start + start, NULL, TRUE, &needs_control_head) == no_stack)
6249 {
6250 start_cc = common->start + start;
6251 compile_matchingpath(common, next_opcode(common, start_cc), bracketend(start_cc) - (1 + LINK_SIZE), backtrack);
6252 BACKTRACK_AS(recurse_backtrack)->inlined_pattern = TRUE;
6253 return cc + 1 + LINK_SIZE;
6254 }
6255
6256 while (entry != NULL)
6257 {
6258 if (entry->start == start)
6259 break;
6260 prev = entry;
6261 entry = entry->next;
6262 }
6263
6264 if (entry == NULL)
6265 {
6266 entry = sljit_alloc_memory(compiler, sizeof(recurse_entry));
6267 if (SLJIT_UNLIKELY(sljit_get_compiler_error(compiler)))
6268 return NULL;
6269 entry->next = NULL;
6270 entry->entry = NULL;
6271 entry->calls = NULL;
6272 entry->start = start;
6273
6274 if (prev != NULL)
6275 prev->next = entry;
6276 else
6277 common->entries = entry;
6278 }
6279
6280 if (common->has_set_som && common->mark_ptr != 0)
6281 {
6282 OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(0));
6283 allocate_stack(common, 2);
6284 OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), common->mark_ptr);
6285 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), TMP2, 0);
6286 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(1), TMP1, 0);
6287 }
6288 else if (common->has_set_som || common->mark_ptr != 0)
6289 {
6290 OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), common->has_set_som ? (int)(OVECTOR(0)) : common->mark_ptr);
6291 allocate_stack(common, 1);
6292 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), TMP2, 0);
6293 }
6294
6295 if (entry->entry == NULL)
6296 add_jump(compiler, &entry->calls, JUMP(SLJIT_FAST_CALL));
6297 else
6298 JUMPTO(SLJIT_FAST_CALL, entry->entry);
6299 /* Leave if the match is failed. */
6300 add_jump(compiler, &backtrack->topbacktracks, CMP(SLJIT_C_EQUAL, TMP1, 0, SLJIT_IMM, 0));
6301 return cc + 1 + LINK_SIZE;
6302 }
6303
6304 static int SLJIT_CALL do_callout(struct jit_arguments* arguments, PUBL(callout_block) *callout_block, pcre_uchar **jit_ovector)
6305 {
6306 const pcre_uchar *begin = arguments->begin;
6307 int *offset_vector = arguments->offsets;
6308 int offset_count = arguments->offset_count;
6309 int i;
6310
6311 if (PUBL(callout) == NULL)
6312 return 0;
6313
6314 callout_block->version = 2;
6315 callout_block->callout_data = arguments->callout_data;
6316
6317 /* Offsets in subject. */
6318 callout_block->subject_length = arguments->end - arguments->begin;
6319 callout_block->start_match = (pcre_uchar*)callout_block->subject - arguments->begin;
6320 callout_block->current_position = (pcre_uchar*)callout_block->offset_vector - arguments->begin;
6321 #if defined COMPILE_PCRE8
6322 callout_block->subject = (PCRE_SPTR)begin;
6323 #elif defined COMPILE_PCRE16
6324 callout_block->subject = (PCRE_SPTR16)begin;
6325 #elif defined COMPILE_PCRE32
6326 callout_block->subject = (PCRE_SPTR32)begin;
6327 #endif
6328
6329 /* Convert and copy the JIT offset vector to the offset_vector array. */
6330 callout_block->capture_top = 0;
6331 callout_block->offset_vector = offset_vector;
6332 for (i = 2; i < offset_count; i += 2)
6333 {
6334 offset_vector[i] = jit_ovector[i] - begin;
6335 offset_vector[i + 1] = jit_ovector[i + 1] - begin;
6336 if (jit_ovector[i] >= begin)
6337 callout_block->capture_top = i;
6338 }
6339
6340 callout_block->capture_top = (callout_block->capture_top >> 1) + 1;
6341 if (offset_count > 0)
6342 offset_vector[0] = -1;
6343 if (offset_count > 1)
6344 offset_vector[1] = -1;
6345 return (*PUBL(callout))(callout_block);
6346 }
6347
6348 /* Aligning to 8 byte. */
6349 #define CALLOUT_ARG_SIZE \
6350 (((int)sizeof(PUBL(callout_block)) + 7) & ~7)
6351
6352 #define CALLOUT_ARG_OFFSET(arg) \
6353 (-CALLOUT_ARG_SIZE + SLJIT_OFFSETOF(PUBL(callout_block), arg))
6354
6355 static SLJIT_INLINE pcre_uchar *compile_callout_matchingpath(compiler_common *common, pcre_uchar *cc, backtrack_common *parent)
6356 {
6357 DEFINE_COMPILER;
6358 backtrack_common *backtrack;
6359
6360 PUSH_BACKTRACK(sizeof(backtrack_common), cc, NULL);
6361
6362 allocate_stack(common, CALLOUT_ARG_SIZE / sizeof(sljit_sw));
6363
6364 OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), common->capture_last_ptr);
6365 OP1(SLJIT_MOV, TMP1, 0, ARGUMENTS, 0);
6366 SLJIT_ASSERT(common->capture_last_ptr != 0);
6367 OP1(SLJIT_MOV_SI, SLJIT_MEM1(STACK_TOP), CALLOUT_ARG_OFFSET(callout_number), SLJIT_IMM, cc[1]);
6368 OP1(SLJIT_MOV_SI, SLJIT_MEM1(STACK_TOP), CALLOUT_ARG_OFFSET(capture_last), TMP2, 0);
6369
6370 /* These pointer sized fields temporarly stores internal variables. */
6371 OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(0));
6372 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), CALLOUT_ARG_OFFSET(offset_vector), STR_PTR, 0);
6373 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), CALLOUT_ARG_OFFSET(subject), TMP2, 0);
6374
6375 if (common->mark_ptr != 0)
6376 OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(TMP1), SLJIT_OFFSETOF(jit_arguments, mark_ptr));
6377 OP1(SLJIT_MOV_SI, SLJIT_MEM1(STACK_TOP), CALLOUT_ARG_OFFSET(pattern_position), SLJIT_IMM, GET(cc, 2));
6378 OP1(SLJIT_MOV_SI, SLJIT_MEM1(STACK_TOP), CALLOUT_ARG_OFFSET(next_item_length), SLJIT_IMM, GET(cc, 2 + LINK_SIZE));
6379 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), CALLOUT_ARG_OFFSET(mark), (common->mark_ptr != 0) ? TMP2 : SLJIT_IMM, 0);
6380
6381 /* Needed to save important temporary registers. */
6382 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), LOCALS0, STACK_TOP, 0);
6383 OP2(SLJIT_SUB, SLJIT_SCRATCH_REG2, 0, STACK_TOP, 0, SLJIT_IMM, CALLOUT_ARG_SIZE);
6384 GET_LOCAL_BASE(SLJIT_SCRATCH_REG3, 0, OVECTOR_START);
6385 sljit_emit_ijump(compiler, SLJIT_CALL3, SLJIT_IMM, SLJIT_FUNC_OFFSET(do_callout));
6386 OP1(SLJIT_MOV_SI, SLJIT_RETURN_REG, 0, SLJIT_RETURN_REG, 0);
6387 OP1(SLJIT_MOV, STACK_TOP, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), LOCALS0);
6388 free_stack(common, CALLOUT_ARG_SIZE / sizeof(sljit_sw));
6389
6390 /* Check return value. */
6391 OP2(SLJIT_SUB | SLJIT_SET_S, SLJIT_UNUSED, 0, SLJIT_RETURN_REG, 0, SLJIT_IMM, 0);
6392 add_jump(compiler, &backtrack->topbacktracks, JUMP(SLJIT_C_SIG_GREATER));
6393 if (common->forced_quit_label == NULL)
6394 add_jump(compiler, &common->forced_quit, JUMP(SLJIT_C_SIG_LESS));
6395 else
6396 JUMPTO(SLJIT_C_SIG_LESS, common->forced_quit_label);
6397 return cc + 2 + 2 * LINK_SIZE;
6398 }
6399
6400 #undef CALLOUT_ARG_SIZE
6401 #undef CALLOUT_ARG_OFFSET
6402
6403 static pcre_uchar *compile_assert_matchingpath(compiler_common *common, pcre_uchar *cc, assert_backtrack *backtrack, BOOL conditional)
6404 {
6405 DEFINE_COMPILER;
6406 int framesize;
6407 int extrasize;
6408 BOOL needs_control_head;
6409 int private_data_ptr;
6410 backtrack_common altbacktrack;
6411 pcre_uchar *ccbegin;
6412 pcre_uchar opcode;
6413 pcre_uchar bra = OP_BRA;
6414 jump_list *tmp = NULL;
6415 jump_list **target = (conditional) ? &backtrack->condfailed : &backtrack->common.topbacktracks;
6416 jump_list **found;
6417 /* Saving previous accept variables. */
6418 BOOL save_local_exit = common->local_exit;
6419 BOOL save_positive_assert = common->positive_assert;
6420 then_trap_backtrack *save_then_trap = common->then_trap;
6421 struct sljit_label *save_quit_label = common->quit_label;
6422 struct sljit_label *save_accept_label = common->accept_label;
6423 jump_list *save_quit = common->quit;
6424 jump_list *save_positive_assert_quit = common->positive_assert_quit;
6425 jump_list *save_accept = common->accept;
6426 struct sljit_jump *jump;
6427 struct sljit_jump *brajump = NULL;
6428
6429 /* Assert captures then. */
6430 common->then_trap = NULL;
6431
6432 if (*cc == OP_BRAZERO || *cc == OP_BRAMINZERO)
6433 {
6434 SLJIT_ASSERT(!conditional);
6435 bra = *cc;
6436 cc++;
6437 }
6438 private_data_ptr = PRIVATE_DATA(cc);
6439 SLJIT_ASSERT(private_data_ptr != 0);
6440 framesize = get_framesize(common, cc, NULL, FALSE, &needs_control_head);
6441 backtrack->framesize = framesize;
6442 backtrack->private_data_ptr = private_data_ptr;
6443 opcode = *cc;
6444 SLJIT_ASSERT(opcode >= OP_ASSERT && opcode <= OP_ASSERTBACK_NOT);
6445 found = (opcode == OP_ASSERT || opcode == OP_ASSERTBACK) ? &tmp : target;
6446 ccbegin = cc;
6447 cc += GET(cc, 1);
6448
6449 if (bra == OP_BRAMINZERO)
6450 {
6451 /* This is a braminzero backtrack path. */
6452 OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(STACK_TOP), STACK(0));
6453 free_stack(common, 1);
6454 brajump = CMP(SLJIT_C_EQUAL, STR_PTR, 0, SLJIT_IMM, 0);
6455 }
6456
6457 if (framesize < 0)
6458 {
6459 extrasize = needs_control_head ? 2 : 1;
6460 if (framesize == no_frame)
6461 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), private_data_ptr, STACK_TOP, 0);
6462 allocate_stack(common, extrasize);
6463 if (needs_control_head)
6464 OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), common->control_head_ptr);
6465 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), STR_PTR, 0);
6466 if (needs_control_head)
6467 {
6468 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), common->control_head_ptr, SLJIT_IMM, 0);
6469 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(1), TMP1, 0);
6470 }
6471 }
6472 else
6473 {
6474 extrasize = needs_control_head ? 3 : 2;
6475 allocate_stack(common, framesize + extrasize);
6476 OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), private_data_ptr);
6477 OP2(SLJIT_SUB, TMP2, 0, STACK_TOP, 0, SLJIT_IMM, (framesize + extrasize) * sizeof(sljit_sw));
6478 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), private_data_ptr, TMP2, 0);
6479 if (needs_control_head)
6480 OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), common->control_head_ptr);
6481 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), STR_PTR, 0);
6482 if (needs_control_head)
6483 {
6484 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(2), TMP1, 0);
6485 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(1), TMP2, 0);
6486 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), common->control_head_ptr, SLJIT_IMM, 0);
6487 }
6488 else
6489 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(1), TMP1, 0);
6490 init_frame(common, ccbegin, NULL, framesize + extrasize - 1, extrasize, FALSE);
6491 }
6492
6493 memset(&altbacktrack, 0, sizeof(backtrack_common));
6494 if (opcode == OP_ASSERT_NOT || opcode == OP_ASSERTBACK_NOT)
6495 {
6496 /* Negative assert is stronger than positive assert. */
6497 common->local_exit = TRUE;
6498 common->quit_label = NULL;
6499 common->quit = NULL;
6500 common->positive_assert = FALSE;
6501 }
6502 else
6503 common->positive_assert = TRUE;
6504 common->positive_assert_quit = NULL;
6505
6506 while (1)
6507 {
6508 common->accept_label = NULL;
6509 common->accept = NULL;
6510 altbacktrack.top = NULL;
6511 altbacktrack.topbacktracks = NULL;
6512
6513 if (*ccbegin == OP_ALT)
6514 OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(STACK_TOP), STACK(0));
6515
6516 altbacktrack.cc = ccbegin;
6517 compile_matchingpath(common, ccbegin + 1 + LINK_SIZE, cc, &altbacktrack);
6518 if (SLJIT_UNLIKELY(sljit_get_compiler_error(compiler)))
6519 {
6520 if (opcode == OP_ASSERT_NOT || opcode == OP_ASSERTBACK_NOT)
6521 {
6522 common->local_exit = save_local_exit;
6523 common->quit_label = save_quit_label;
6524 common->quit = save_quit;
6525 }
6526 common->positive_assert = save_positive_assert;
6527 common->then_trap = save_then_trap;
6528 common->accept_label = save_accept_label;
6529 common->positive_assert_quit = save_positive_assert_quit;
6530 common->accept = save_accept;
6531 return NULL;
6532 }
6533 common->accept_label = LABEL();
6534 if (common->accept != NULL)
6535 set_jumps(common->accept, common->accept_label);
6536
6537 /* Reset stack. */
6538 if (framesize < 0)
6539 {
6540 if (framesize == no_frame)
6541 OP1(SLJIT_MOV, STACK_TOP, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), private_data_ptr);
6542 else
6543 free_stack(common, extrasize);
6544 if (needs_control_head)
6545 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), common->control_head_ptr, SLJIT_MEM1(STACK_TOP), 0);
6546 }
6547 else
6548 {
6549 if ((opcode != OP_ASSERT_NOT && opcode != OP_ASSERTBACK_NOT) || conditional)
6550 {
6551 /* We don't need to keep the STR_PTR, only the previous private_data_ptr. */
6552 OP2(SLJIT_ADD, STACK_TOP, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), private_data_ptr, SLJIT_IMM, (framesize + 1) * sizeof(sljit_sw));
6553 if (needs_control_head)
6554 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), common->control_head_ptr, SLJIT_MEM1(STACK_TOP), 0);
6555 }
6556 else
6557 {
6558 OP1(SLJIT_MOV, STACK_TOP, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), private_data_ptr);
6559 if (needs_control_head)
6560 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), common->control_head_ptr, SLJIT_MEM1(STACK_TOP), (framesize + 1) * sizeof(sljit_sw));
6561 add_jump(compiler, &common->revertframes, JUMP(SLJIT_FAST_CALL));
6562 }
6563 }
6564
6565 if (opcode == OP_ASSERT_NOT || opcode == OP_ASSERTBACK_NOT)
6566 {
6567 /* We know that STR_PTR was stored on the top of the stack. */
6568 if (conditional)
6569 OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(STACK_TOP), needs_control_head ? sizeof(sljit_sw) : 0);
6570 else if (bra == OP_BRAZERO)
6571 {
6572 if (framesize < 0)
6573 OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(STACK_TOP), (extrasize - 1) * sizeof(sljit_sw));
6574 else
6575 {
6576 OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(STACK_TOP), framesize * sizeof(sljit_sw));
6577 OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(STACK_TOP), (framesize + extrasize - 1) * sizeof(sljit_sw));
6578 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), private_data_ptr, TMP1, 0);
6579 }
6580 OP2(SLJIT_ADD, STACK_TOP, 0, STACK_TOP, 0, SLJIT_IMM, sizeof(sljit_sw));
6581 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), SLJIT_IMM, 0);
6582 }
6583 else if (framesize >= 0)
6584 {
6585 /* For OP_BRA and OP_BRAMINZERO. */
6586 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), private_data_ptr, SLJIT_MEM1(STACK_TOP), framesize * sizeof(sljit_sw));
6587 }
6588 }
6589 add_jump(compiler, found, JUMP(SLJIT_JUMP));
6590
6591 compile_backtrackingpath(common, altbacktrack.top);
6592 if (SLJIT_UNLIKELY(sljit_get_compiler_error(compiler)))
6593 {
6594 if (opcode == OP_ASSERT_NOT || opcode == OP_ASSERTBACK_NOT)
6595 {
6596 common->local_exit = save_local_exit;
6597 common->quit_label = save_quit_label;
6598 common->quit = save_quit;
6599 }
6600 common->positive_assert = save_positive_assert;
6601 common->then_trap = save_then_trap;
6602 common->accept_label = save_accept_label;
6603 common->positive_assert_quit = save_positive_assert_quit;
6604 common->accept = save_accept;
6605 return NULL;
6606 }
6607 set_jumps(altbacktrack.topbacktracks, LABEL());
6608
6609 if (*cc != OP_ALT)
6610 break;
6611
6612 ccbegin = cc;
6613 cc += GET(cc, 1);
6614 }
6615
6616 if (opcode == OP_ASSERT_NOT || opcode == OP_ASSERTBACK_NOT)
6617 {
6618 SLJIT_ASSERT(common->positive_assert_quit == NULL);
6619 /* Makes the check less complicated below. */
6620 common->positive_assert_quit = common->quit;
6621 }
6622
6623 /* None of them matched. */
6624 if (common->positive_assert_quit != NULL)
6625 {
6626 jump = JUMP(SLJIT_JUMP);
6627 set_jumps(common->positive_assert_quit, LABEL());
6628 SLJIT_ASSERT(framesize != no_stack);
6629 if (framesize < 0)
6630 OP2(SLJIT_ADD, STACK_TOP, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), private_data_ptr, SLJIT_IMM, extrasize * sizeof(sljit_sw));
6631 else
6632 {
6633 OP1(SLJIT_MOV, STACK_TOP, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), private_data_ptr);
6634 add_jump(compiler, &common->revertframes, JUMP(SLJIT_FAST_CALL));
6635 OP2(SLJIT_ADD, STACK_TOP, 0, STACK_TOP, 0, SLJIT_IMM, (framesize + extrasize) * sizeof(sljit_sw));
6636 }
6637 JUMPHERE(jump);
6638 }
6639
6640 if (needs_control_head)
6641 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), common->control_head_ptr, SLJIT_MEM1(STACK_TOP), STACK(1));
6642
6643 if (opcode == OP_ASSERT || opcode == OP_ASSERTBACK)
6644 {
6645 /* Assert is failed. */
6646 if (conditional || bra == OP_BRAZERO)
6647 OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(STACK_TOP), STACK(0));
6648
6649 if (framesize < 0)
6650 {
6651 /* The topmost item should be 0. */
6652 if (bra == OP_BRAZERO)
6653 {
6654 if (extrasize == 2)
6655 free_stack(common, 1);
6656 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), SLJIT_IMM, 0);
6657 }
6658 else
6659 free_stack(common, extrasize);
6660 }
6661 else
6662 {
6663 OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(STACK_TOP), STACK(extrasize - 1));
6664 /* The topmost item should be 0. */
6665 if (bra == OP_BRAZERO)
6666 {
6667 free_stack(common, framesize + extrasize - 1);
6668 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), SLJIT_IMM, 0);
6669 }
6670 else
6671 free_stack(common, framesize + extrasize);
6672 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), private_data_ptr, TMP1, 0);
6673 }
6674 jump = JUMP(SLJIT_JUMP);
6675 if (bra != OP_BRAZERO)
6676 add_jump(compiler, target, jump);
6677
6678 /* Assert is successful. */
6679 set_jumps(tmp, LABEL());
6680 if (framesize < 0)
6681 {
6682 /* We know that STR_PTR was stored on the top of the stack. */
6683 OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(STACK_TOP), (extrasize - 1) * sizeof(sljit_sw));
6684 /* Keep the STR_PTR on the top of the stack. */
6685 if (bra == OP_BRAZERO)
6686 {
6687 OP2(SLJIT_ADD, STACK_TOP, 0, STACK_TOP, 0, SLJIT_IMM, sizeof(sljit_sw));
6688 if (extrasize == 2)
6689 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), STR_PTR, 0);
6690 }
6691 else if (bra == OP_BRAMINZERO)
6692 {
6693 OP2(SLJIT_ADD, STACK_TOP, 0, STACK_TOP, 0, SLJIT_IMM, sizeof(sljit_sw));
6694 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), SLJIT_IMM, 0);
6695 }
6696 }
6697 else
6698 {
6699 if (bra == OP_BRA)
6700 {
6701 /* We don't need to keep the STR_PTR, only the previous private_data_ptr. */
6702 OP2(SLJIT_ADD, STACK_TOP, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), private_data_ptr, SLJIT_IMM, (framesize + 1) * sizeof(sljit_sw));
6703 OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(STACK_TOP), (extrasize - 2) * sizeof(sljit_sw));
6704 }
6705 else
6706 {
6707 /* We don't need to keep the STR_PTR, only the previous private_data_ptr. */
6708 OP2(SLJIT_ADD, STACK_TOP, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), private_data_ptr, SLJIT_IMM, (framesize + 2) * sizeof(sljit_sw));
6709 if (extrasize == 2)
6710 {
6711 OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(STACK_TOP), STACK(0));
6712 if (bra == OP_BRAMINZERO)
6713 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), SLJIT_IMM, 0);
6714 }
6715 else
6716 {
6717 OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(STACK_TOP), 0);
6718 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), bra == OP_BRAZERO ? STR_PTR : SLJIT_IMM, 0);
6719 }
6720 }
6721 }
6722
6723 if (bra == OP_BRAZERO)
6724 {
6725 backtrack->matchingpath = LABEL();
6726 SET_LABEL(jump, backtrack->matchingpath);
6727 }
6728 else if (bra == OP_BRAMINZERO)
6729 {
6730 JUMPTO(SLJIT_JUMP, backtrack->matchingpath);
6731 JUMPHERE(brajump);
6732 if (framesize >= 0)
6733 {
6734 OP1(SLJIT_MOV, STACK_TOP, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), private_data_ptr);
6735 add_jump(compiler, &common->revertframes, JUMP(SLJIT_FAST_CALL));
6736 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), private_data_ptr, SLJIT_MEM1(STACK_TOP), framesize * sizeof(sljit_sw));
6737 }
6738 set_jumps(backtrack->common.topbacktracks, LABEL());
6739 }
6740 }
6741 else
6742 {
6743 /* AssertNot is successful. */
6744 if (framesize < 0)
6745 {
6746 OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(STACK_TOP), STACK(0));
6747 if (bra != OP_BRA)
6748 {
6749 if (extrasize == 2)
6750 free_stack(common, 1);
6751 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), SLJIT_IMM, 0);
6752 }
6753 else
6754 free_stack(common, extrasize);
6755 }
6756 else
6757 {
6758 OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(STACK_TOP), STACK(0));
6759 OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(STACK_TOP), STACK(extrasize - 1));