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