-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathpcre2_jit_compile.c
14135 lines (12296 loc) · 424 KB
/
pcre2_jit_compile.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*************************************************
* Perl-Compatible Regular Expressions *
*************************************************/
/* PCRE is a library of functions to support regular expressions whose syntax
and semantics are as close as possible to those of the Perl 5 language.
Written by Philip Hazel
This module by Zoltan Herczeg
Original API code Copyright (c) 1997-2012 University of Cambridge
New API code Copyright (c) 2016-2024 University of Cambridge
-----------------------------------------------------------------------------
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the University of Cambridge nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
-----------------------------------------------------------------------------
*/
#if defined(__has_feature)
#if __has_feature(memory_sanitizer)
#include <sanitizer/msan_interface.h>
#endif /* __has_feature(memory_sanitizer) */
#endif /* defined(__has_feature) */
#include "pcre2_internal.h"
#ifdef SUPPORT_JIT
/* All-in-one: Since we use the JIT compiler only from here,
we just include it. This way we don't need to touch the build
system files. */
#define SLJIT_CONFIG_AUTO 1
#define SLJIT_CONFIG_STATIC 1
#define SLJIT_VERBOSE 0
#ifdef PCRE2_DEBUG
#define SLJIT_DEBUG 1
#else
#define SLJIT_DEBUG 0
#endif
#define SLJIT_MALLOC(size, allocator_data) pcre2_jit_malloc(size, allocator_data)
#define SLJIT_FREE(ptr, allocator_data) pcre2_jit_free(ptr, allocator_data)
static void * pcre2_jit_malloc(size_t size, void *allocator_data)
{
pcre2_memctl *allocator = ((pcre2_memctl*)allocator_data);
return allocator->malloc(size, allocator->memory_data);
}
static void pcre2_jit_free(void *ptr, void *allocator_data)
{
pcre2_memctl *allocator = ((pcre2_memctl*)allocator_data);
allocator->free(ptr, allocator->memory_data);
}
#include "../deps/sljit/sljit_src/sljitLir.c"
#if defined SLJIT_CONFIG_UNSUPPORTED && SLJIT_CONFIG_UNSUPPORTED
#error Unsupported architecture
#endif
/* Defines for debugging purposes. */
/* 1 - Use unoptimized capturing brackets.
2 - Enable capture_last_ptr (includes option 1). */
/* #define DEBUG_FORCE_UNOPTIMIZED_CBRAS 2 */
/* 1 - Always have a control head. */
/* #define DEBUG_FORCE_CONTROL_HEAD 1 */
/* Allocate memory for the regex stack on the real machine stack.
Fast, but limited size. */
#define MACHINE_STACK_SIZE 32768
/* Growth rate for stack allocated by the OS. Should be the multiply
of page size. */
#define STACK_GROWTH_RATE 8192
/* Enable to check that the allocation could destroy temporaries. */
#if defined SLJIT_DEBUG && SLJIT_DEBUG
#define DESTROY_REGISTERS 1
#endif
/*
Short summary about the backtracking mechanism empolyed by the jit code generator:
The code generator follows the recursive nature of the PERL compatible regular
expressions. The basic blocks of regular expressions are condition checkers
whose execute different commands depending on the result of the condition check.
The relationship between the operators can be horizontal (concatenation) and
vertical (sub-expression) (See struct backtrack_common for more details).
'ab' - 'a' and 'b' regexps are concatenated
'a+' - 'a' is the sub-expression of the '+' operator
The condition checkers are boolean (true/false) checkers. Machine code is generated
for the checker itself and for the actions depending on the result of the checker.
The 'true' case is called as the matching path (expected path), and the other is called as
the 'backtrack' path. Branch instructions are expesive for all CPUs, so we avoid taken
branches on the matching path.
Greedy star operator (*) :
Matching path: match happens.
Backtrack path: match failed.
Non-greedy star operator (*?) :
Matching path: no need to perform a match.
Backtrack path: match is required.
The following example shows how the code generated for a capturing bracket
with two alternatives. Let A, B, C, D are arbirary regular expressions, and
we have the following regular expression:
A(B|C)D
The generated code will be the following:
A matching path
'(' matching path (pushing arguments to the stack)
B matching path
')' matching path (pushing arguments to the stack)
D matching path
return with successful match
D backtrack path
')' backtrack path (If we arrived from "C" jump to the backtrack of "C")
B backtrack path
C expected path
jump to D matching path
C backtrack path
A backtrack path
Notice, that the order of backtrack code paths are the opposite of the fast
code paths. In this way the topmost value on the stack is always belong
to the current backtrack code path. The backtrack path must check
whether there is a next alternative. If so, it needs to jump back to
the matching path eventually. Otherwise it needs to clear out its own stack
frame and continue the execution on the backtrack code paths.
*/
/*
Saved stack frames:
Atomic blocks and asserts require reloading the values of private data
when the backtrack mechanism performed. Because of OP_RECURSE, the data
are not necessarly known in compile time, thus we need a dynamic restore
mechanism.
The stack frames are stored in a chain list, and have the following format:
([ capturing bracket offset ][ start value ][ end value ])+ ... [ 0 ] [ previous head ]
Thus we can restore the private data to a particular point in the stack.
*/
typedef struct jit_arguments {
/* Pointers first. */
struct sljit_stack *stack;
PCRE2_SPTR str;
PCRE2_SPTR begin;
PCRE2_SPTR end;
pcre2_match_data *match_data;
PCRE2_SPTR startchar_ptr;
PCRE2_UCHAR *mark_ptr;
int (*callout)(pcre2_callout_block *, void *);
void *callout_data;
/* Everything else after. */
sljit_uw offset_limit;
sljit_u32 limit_match;
sljit_u32 oveccount;
sljit_u32 options;
} jit_arguments;
#define JIT_NUMBER_OF_COMPILE_MODES 3
typedef struct executable_functions {
void *executable_funcs[JIT_NUMBER_OF_COMPILE_MODES];
void *read_only_data_heads[JIT_NUMBER_OF_COMPILE_MODES];
sljit_uw executable_sizes[JIT_NUMBER_OF_COMPILE_MODES];
sljit_u32 top_bracket;
sljit_u32 limit_match;
} executable_functions;
typedef struct jump_list {
struct sljit_jump *jump;
struct jump_list *next;
} jump_list;
typedef struct stub_list {
struct sljit_jump *start;
struct sljit_label *quit;
struct stub_list *next;
} stub_list;
enum frame_types {
no_frame = -1,
no_stack = -2
};
enum control_types {
type_mark = 0,
type_then_trap = 1
};
enum early_fail_types {
type_skip = 0,
type_fail = 1,
type_fail_range = 2
};
typedef int (SLJIT_FUNC *jit_function)(jit_arguments *args);
/* The following structure is the key data type for the recursive
code generator. It is allocated by compile_matchingpath, and contains
the arguments for compile_backtrackingpath. Must be the first member
of its descendants. */
typedef struct backtrack_common {
/* Backtracking path of an opcode, which falls back
to our opcode, if it cannot resume matching. */
struct backtrack_common *prev;
/* Backtracks for opcodes without backtracking path.
These opcodes are between 'prev' and the current
opcode, and they never resume the match. */
jump_list *simple_backtracks;
/* Internal backtracking list for block constructs
which contains other opcodes, such as brackets,
asserts, conditionals, etc. */
struct backtrack_common *top;
/* Backtracks used internally by the opcode. For component
opcodes, this list is also used by those opcodes without
backtracking path which follows the 'top' backtrack. */
jump_list *own_backtracks;
/* Opcode pointer. */
PCRE2_SPTR cc;
} backtrack_common;
typedef struct assert_backtrack {
backtrack_common common;
jump_list *condfailed;
/* Less than 0 if a frame is not needed. */
int framesize;
/* Points to our private memory word on the stack. */
int private_data_ptr;
/* For iterators. */
struct sljit_label *matchingpath;
} assert_backtrack;
typedef struct bracket_backtrack {
backtrack_common common;
/* Where to coninue if an alternative is successfully matched. */
struct sljit_label *alternative_matchingpath;
/* For rmin and rmax iterators. */
struct sljit_label *recursive_matchingpath;
/* For greedy ? operator. */
struct sljit_label *zero_matchingpath;
/* Contains the branches of a failed condition. */
union {
/* Both for OP_COND, OP_SCOND, OP_ASSERT_SCS. */
jump_list *no_capture;
assert_backtrack *assert;
/* For OP_ONCE. Less than 0 if not needed. */
int framesize;
} u;
/* For brackets with >3 alternatives. */
struct sljit_jump *matching_mov_addr;
/* Points to our private memory word on the stack. */
int private_data_ptr;
} bracket_backtrack;
typedef struct bracketpos_backtrack {
backtrack_common common;
/* Points to our private memory word on the stack. */
int private_data_ptr;
/* Reverting stack is needed. */
int framesize;
/* Allocated stack size. */
int stacksize;
} bracketpos_backtrack;
typedef struct braminzero_backtrack {
backtrack_common common;
struct sljit_label *matchingpath;
} braminzero_backtrack;
typedef struct char_iterator_backtrack {
backtrack_common common;
/* Next iteration. */
struct sljit_label *matchingpath;
/* Creating a range based on the next character. */
struct {
unsigned int othercasebit;
PCRE2_UCHAR chr;
BOOL charpos_enabled;
} charpos;
} char_iterator_backtrack;
typedef struct ref_iterator_backtrack {
backtrack_common common;
/* Next iteration. */
struct sljit_label *matchingpath;
} ref_iterator_backtrack;
typedef struct recurse_entry {
struct recurse_entry *next;
/* Contains the function entry label. */
struct sljit_label *entry_label;
/* Contains the function entry label. */
struct sljit_label *backtrack_label;
/* Collects the entry calls until the function is not created. */
jump_list *entry_calls;
/* Collects the backtrack calls until the function is not created. */
jump_list *backtrack_calls;
/* Points to the starting opcode. */
sljit_sw start;
} recurse_entry;
typedef struct recurse_backtrack {
backtrack_common common;
/* Return to the matching path. */
struct sljit_label *matchingpath;
/* Recursive pattern. */
recurse_entry *entry;
/* Pattern is inlined. */
BOOL inlined_pattern;
} recurse_backtrack;
typedef struct vreverse_backtrack {
backtrack_common common;
/* Return to the matching path. */
struct sljit_label *matchingpath;
} vreverse_backtrack;
#define OP_THEN_TRAP OP_TABLE_LENGTH
typedef struct then_trap_backtrack {
backtrack_common common;
/* If then_trap is not NULL, this structure contains the real
then_trap for the backtracking path. */
struct then_trap_backtrack *then_trap;
/* Points to the starting opcode. */
sljit_sw start;
/* Exit point for the then opcodes of this alternative. */
jump_list *quit;
/* Frame size of the current alternative. */
int framesize;
} then_trap_backtrack;
#define MAX_N_CHARS 12
#define MAX_DIFF_CHARS 5
typedef struct fast_forward_char_data {
/* Number of characters in the chars array, 255 for any character. */
sljit_u8 count;
/* Number of last UTF-8 characters in the chars array. */
sljit_u8 last_count;
/* Available characters in the current position. */
PCRE2_UCHAR chars[MAX_DIFF_CHARS];
} fast_forward_char_data;
#define MAX_CLASS_RANGE_SIZE 4
#define MAX_CLASS_CHARS_SIZE 3
typedef struct compiler_common {
/* The sljit ceneric compiler. */
struct sljit_compiler *compiler;
/* Compiled regular expression. */
pcre2_real_code *re;
/* First byte code. */
PCRE2_SPTR start;
/* Maps private data offset to each opcode. */
sljit_s32 *private_data_ptrs;
/* Chain list of read-only data ptrs. */
void *read_only_data_head;
/* Tells whether the capturing bracket is optimized. */
sljit_u8 *optimized_cbracket;
/* Tells whether the starting offset is a target of then. */
sljit_u8 *then_offsets;
/* Current position where a THEN must jump. */
then_trap_backtrack *then_trap;
/* Starting offset of private data for capturing brackets. */
sljit_s32 cbra_ptr;
#if defined SLJIT_DEBUG && SLJIT_DEBUG
/* End offset of locals for assertions. */
sljit_s32 locals_size;
#endif
/* Output vector starting point. Must be divisible by 2. */
sljit_s32 ovector_start;
/* Points to the starting character of the current match. */
sljit_s32 start_ptr;
/* Last known position of the requested byte. */
sljit_s32 req_char_ptr;
/* Head of the last recursion. */
sljit_s32 recursive_head_ptr;
/* First inspected character for partial matching.
(Needed for avoiding zero length partial matches.) */
sljit_s32 start_used_ptr;
/* Starting pointer for partial soft matches. */
sljit_s32 hit_start;
/* Pointer of the match end position. */
sljit_s32 match_end_ptr;
/* Points to the marked string. */
sljit_s32 mark_ptr;
/* Head of the recursive control verb management chain.
Each item must have a previous offset and type
(see control_types) values. See do_search_mark. */
sljit_s32 control_head_ptr;
/* The offset of the saved STR_END in the outermost
scan substring block. Since scan substring restores
STR_END after a match, it is enough to restore
STR_END inside a scan substring block. */
sljit_s32 restore_end_ptr;
/* Points to the last matched capture block index. */
sljit_s32 capture_last_ptr;
/* Fast forward skipping byte code pointer. */
PCRE2_SPTR fast_forward_bc_ptr;
/* Locals used by fast fail optimization. */
sljit_s32 early_fail_start_ptr;
sljit_s32 early_fail_end_ptr;
/* Variables used by recursive call generator. */
sljit_s32 recurse_bitset_size;
uint8_t *recurse_bitset;
/* Flipped and lower case tables. */
const sljit_u8 *fcc;
sljit_sw lcc;
/* Mode can be PCRE2_JIT_COMPLETE and others. */
int mode;
/* TRUE, when empty match is accepted for partial matching. */
BOOL allow_empty_partial;
/* TRUE, when minlength is greater than 0. */
BOOL might_be_empty;
/* \K is found in the pattern. */
BOOL has_set_som;
/* (*SKIP:arg) is found in the pattern. */
BOOL has_skip_arg;
/* (*THEN) is found in the pattern. */
BOOL has_then;
/* (*SKIP) or (*SKIP:arg) is found in lookbehind assertion. */
BOOL has_skip_in_assert_back;
/* Quit is redirected by recurse, negative assertion, or positive assertion in conditional block. */
BOOL local_quit_available;
/* Currently in a positive assertion. */
BOOL in_positive_assertion;
/* Newline control. */
int nltype;
sljit_u32 nlmax;
sljit_u32 nlmin;
int newline;
int bsr_nltype;
sljit_u32 bsr_nlmax;
sljit_u32 bsr_nlmin;
/* Dollar endonly. */
int endonly;
/* Tables. */
sljit_sw ctypes;
/* Named capturing brackets. */
PCRE2_SPTR name_table;
sljit_sw name_count;
sljit_sw name_entry_size;
/* Labels and jump lists. */
struct sljit_label *partialmatchlabel;
struct sljit_label *quit_label;
struct sljit_label *abort_label;
struct sljit_label *accept_label;
struct sljit_label *ff_newline_shortcut;
stub_list *stubs;
recurse_entry *entries;
recurse_entry *currententry;
jump_list *partialmatch;
jump_list *quit;
jump_list *positive_assertion_quit;
jump_list *abort;
jump_list *failed_match;
jump_list *accept;
jump_list *calllimit;
jump_list *stackalloc;
jump_list *revertframes;
jump_list *wordboundary;
jump_list *ucp_wordboundary;
jump_list *anynewline;
jump_list *hspace;
jump_list *vspace;
jump_list *casefulcmp;
jump_list *caselesscmp;
jump_list *reset_match;
/* Same as reset_match, but resets the STR_PTR as well. */
jump_list *restart_match;
BOOL unset_backref;
BOOL alt_circumflex;
#ifdef SUPPORT_UNICODE
BOOL utf;
BOOL invalid_utf;
BOOL ucp;
/* Points to saving area for iref. */
jump_list *getucd;
jump_list *getucdtype;
#if PCRE2_CODE_UNIT_WIDTH == 8
jump_list *utfreadchar;
jump_list *utfreadtype8;
jump_list *utfpeakcharback;
#endif
#if PCRE2_CODE_UNIT_WIDTH == 8 || PCRE2_CODE_UNIT_WIDTH == 16
jump_list *utfreadchar_invalid;
jump_list *utfreadnewline_invalid;
jump_list *utfmoveback_invalid;
jump_list *utfpeakcharback_invalid;
#endif
#endif /* SUPPORT_UNICODE */
} compiler_common;
/* For byte_sequence_compare. */
typedef struct compare_context {
int length;
int sourcereg;
#if defined SLJIT_UNALIGNED && SLJIT_UNALIGNED
int ucharptr;
union {
sljit_s32 asint;
sljit_u16 asushort;
#if PCRE2_CODE_UNIT_WIDTH == 8
sljit_u8 asbyte;
sljit_u8 asuchars[4];
#elif PCRE2_CODE_UNIT_WIDTH == 16
sljit_u16 asuchars[2];
#elif PCRE2_CODE_UNIT_WIDTH == 32
sljit_u32 asuchars[1];
#endif
} c;
union {
sljit_s32 asint;
sljit_u16 asushort;
#if PCRE2_CODE_UNIT_WIDTH == 8
sljit_u8 asbyte;
sljit_u8 asuchars[4];
#elif PCRE2_CODE_UNIT_WIDTH == 16
sljit_u16 asuchars[2];
#elif PCRE2_CODE_UNIT_WIDTH == 32
sljit_u32 asuchars[1];
#endif
} oc;
#endif
} compare_context;
/* Undefine sljit macros. */
#undef CMP
/* Used for accessing the elements of the stack. */
#define STACK(i) ((i) * SSIZE_OF(sw))
#ifdef SLJIT_PREF_SHIFT_REG
#if SLJIT_PREF_SHIFT_REG == SLJIT_R2
/* Nothing. */
#elif SLJIT_PREF_SHIFT_REG == SLJIT_R3
#define SHIFT_REG_IS_R3
#else
#error "Unsupported shift register"
#endif
#endif
#define TMP1 SLJIT_R0
#ifdef SHIFT_REG_IS_R3
#define TMP2 SLJIT_R3
#define TMP3 SLJIT_R2
#else
#define TMP2 SLJIT_R2
#define TMP3 SLJIT_R3
#endif
#define STR_PTR SLJIT_R1
#define STR_END SLJIT_S0
#define STACK_TOP SLJIT_S1
#define STACK_LIMIT SLJIT_S2
#define COUNT_MATCH SLJIT_S3
#define ARGUMENTS SLJIT_S4
#define RETURN_ADDR SLJIT_R4
#if (defined SLJIT_CONFIG_X86_32 && SLJIT_CONFIG_X86_32)
#define HAS_VIRTUAL_REGISTERS 1
#else
#define HAS_VIRTUAL_REGISTERS 0
#endif
/* Local space layout. */
/* Max limit of recursions. */
#define LIMIT_MATCH (0 * sizeof(sljit_sw))
/* Local variables. Their number is computed by check_opcode_types. */
#define LOCAL0 (1 * sizeof(sljit_sw))
#define LOCAL1 (2 * sizeof(sljit_sw))
#define LOCAL2 (3 * sizeof(sljit_sw))
#define LOCAL3 (4 * sizeof(sljit_sw))
#define LOCAL4 (5 * sizeof(sljit_sw))
/* The output vector is stored on the stack, and contains pointers
to characters. The vector data is divided into two groups: the first
group contains the start / end character pointers, and the second is
the start pointers when the end of the capturing group has not yet reached. */
#define OVECTOR_START (common->ovector_start)
#define OVECTOR(i) (OVECTOR_START + (i) * SSIZE_OF(sw))
#define OVECTOR_PRIV(i) (common->cbra_ptr + (i) * SSIZE_OF(sw))
#define PRIVATE_DATA(cc) (common->private_data_ptrs[(cc) - common->start])
#if PCRE2_CODE_UNIT_WIDTH == 8
#define MOV_UCHAR SLJIT_MOV_U8
#define IN_UCHARS(x) (x)
#elif PCRE2_CODE_UNIT_WIDTH == 16
#define MOV_UCHAR SLJIT_MOV_U16
#define UCHAR_SHIFT (1)
#define IN_UCHARS(x) ((x) * 2)
#elif PCRE2_CODE_UNIT_WIDTH == 32
#define MOV_UCHAR SLJIT_MOV_U32
#define UCHAR_SHIFT (2)
#define IN_UCHARS(x) ((x) * 4)
#else
#error Unsupported compiling mode
#endif
/* Shortcuts. */
#define DEFINE_COMPILER \
struct sljit_compiler *compiler = common->compiler
#define OP1(op, dst, dstw, src, srcw) \
sljit_emit_op1(compiler, (op), (dst), (dstw), (src), (srcw))
#define OP2(op, dst, dstw, src1, src1w, src2, src2w) \
sljit_emit_op2(compiler, (op), (dst), (dstw), (src1), (src1w), (src2), (src2w))
#define OP2U(op, src1, src1w, src2, src2w) \
sljit_emit_op2u(compiler, (op), (src1), (src1w), (src2), (src2w))
#define OP_SRC(op, src, srcw) \
sljit_emit_op_src(compiler, (op), (src), (srcw))
#define LABEL() \
sljit_emit_label(compiler)
#define JUMP(type) \
sljit_emit_jump(compiler, (type))
#define JUMPTO(type, label) \
sljit_set_label(sljit_emit_jump(compiler, (type)), (label))
#define JUMPHERE(jump) \
sljit_set_label((jump), sljit_emit_label(compiler))
#define SET_LABEL(jump, label) \
sljit_set_label((jump), (label))
#define CMP(type, src1, src1w, src2, src2w) \
sljit_emit_cmp(compiler, (type), (src1), (src1w), (src2), (src2w))
#define CMPTO(type, src1, src1w, src2, src2w, label) \
sljit_set_label(sljit_emit_cmp(compiler, (type), (src1), (src1w), (src2), (src2w)), (label))
#define OP_FLAGS(op, dst, dstw, type) \
sljit_emit_op_flags(compiler, (op), (dst), (dstw), (type))
#define SELECT(type, dst_reg, src1, src1w, src2_reg) \
sljit_emit_select(compiler, (type), (dst_reg), (src1), (src1w), (src2_reg))
#define GET_LOCAL_BASE(dst, dstw, offset) \
sljit_get_local_base(compiler, (dst), (dstw), (offset))
#define READ_CHAR_MAX ((sljit_u32)0xffffffff)
#define INVALID_UTF_CHAR -1
#define UNASSIGNED_UTF_CHAR 888
#if defined SUPPORT_UNICODE
#if PCRE2_CODE_UNIT_WIDTH == 8
#define GETCHARINC_INVALID(c, ptr, end, invalid_action) \
{ \
if (ptr[0] <= 0x7f) \
c = *ptr++; \
else if (ptr + 1 < end && ptr[1] >= 0x80 && ptr[1] < 0xc0) \
{ \
c = ptr[1] - 0x80; \
\
if (ptr[0] >= 0xc2 && ptr[0] <= 0xdf) \
{ \
c |= (ptr[0] - 0xc0) << 6; \
ptr += 2; \
} \
else if (ptr + 2 < end && ptr[2] >= 0x80 && ptr[2] < 0xc0) \
{ \
c = c << 6 | (ptr[2] - 0x80); \
\
if (ptr[0] >= 0xe0 && ptr[0] <= 0xef) \
{ \
c |= (ptr[0] - 0xe0) << 12; \
ptr += 3; \
\
if (c < 0x800 || (c >= 0xd800 && c < 0xe000)) \
{ \
invalid_action; \
} \
} \
else if (ptr + 3 < end && ptr[3] >= 0x80 && ptr[3] < 0xc0) \
{ \
c = c << 6 | (ptr[3] - 0x80); \
\
if (ptr[0] >= 0xf0 && ptr[0] <= 0xf4) \
{ \
c |= (ptr[0] - 0xf0) << 18; \
ptr += 4; \
\
if (c >= 0x110000 || c < 0x10000) \
{ \
invalid_action; \
} \
} \
else \
{ \
invalid_action; \
} \
} \
else \
{ \
invalid_action; \
} \
} \
else \
{ \
invalid_action; \
} \
} \
else \
{ \
invalid_action; \
} \
}
#define GETCHARBACK_INVALID(c, ptr, start, invalid_action) \
{ \
c = ptr[-1]; \
if (c <= 0x7f) \
ptr--; \
else if (ptr - 1 > start && ptr[-1] >= 0x80 && ptr[-1] < 0xc0) \
{ \
c -= 0x80; \
\
if (ptr[-2] >= 0xc2 && ptr[-2] <= 0xdf) \
{ \
c |= (ptr[-2] - 0xc0) << 6; \
ptr -= 2; \
} \
else if (ptr - 2 > start && ptr[-2] >= 0x80 && ptr[-2] < 0xc0) \
{ \
c = c << 6 | (ptr[-2] - 0x80); \
\
if (ptr[-3] >= 0xe0 && ptr[-3] <= 0xef) \
{ \
c |= (ptr[-3] - 0xe0) << 12; \
ptr -= 3; \
\
if (c < 0x800 || (c >= 0xd800 && c < 0xe000)) \
{ \
invalid_action; \
} \
} \
else if (ptr - 3 > start && ptr[-3] >= 0x80 && ptr[-3] < 0xc0) \
{ \
c = c << 6 | (ptr[-3] - 0x80); \
\
if (ptr[-4] >= 0xf0 && ptr[-4] <= 0xf4) \
{ \
c |= (ptr[-4] - 0xf0) << 18; \
ptr -= 4; \
\
if (c >= 0x110000 || c < 0x10000) \
{ \
invalid_action; \
} \
} \
else \
{ \
invalid_action; \
} \
} \
else \
{ \
invalid_action; \
} \
} \
else \
{ \
invalid_action; \
} \
} \
else \
{ \
invalid_action; \
} \
}
#elif PCRE2_CODE_UNIT_WIDTH == 16
#define GETCHARINC_INVALID(c, ptr, end, invalid_action) \
{ \
if (ptr[0] < 0xd800 || ptr[0] >= 0xe000) \
c = *ptr++; \
else if (ptr[0] < 0xdc00 && ptr + 1 < end && ptr[1] >= 0xdc00 && ptr[1] < 0xe000) \
{ \
c = (((ptr[0] - 0xd800) << 10) | (ptr[1] - 0xdc00)) + 0x10000; \
ptr += 2; \
} \
else \
{ \
invalid_action; \
} \
}
#define GETCHARBACK_INVALID(c, ptr, start, invalid_action) \
{ \
c = ptr[-1]; \
if (c < 0xd800 || c >= 0xe000) \
ptr--; \
else if (c >= 0xdc00 && ptr - 1 > start && ptr[-2] >= 0xd800 && ptr[-2] < 0xdc00) \
{ \
c = (((ptr[-2] - 0xd800) << 10) | (c - 0xdc00)) + 0x10000; \
ptr -= 2; \
} \
else \
{ \
invalid_action; \
} \
}
#elif PCRE2_CODE_UNIT_WIDTH == 32
#define GETCHARINC_INVALID(c, ptr, end, invalid_action) \
{ \
if (ptr[0] < 0xd800 || (ptr[0] >= 0xe000 && ptr[0] < 0x110000)) \
c = *ptr++; \
else \
{ \
invalid_action; \
} \
}
#define GETCHARBACK_INVALID(c, ptr, start, invalid_action) \
{ \
c = ptr[-1]; \
if (ptr[-1] < 0xd800 || (ptr[-1] >= 0xe000 && ptr[-1] < 0x110000)) \
ptr--; \
else \
{ \
invalid_action; \
} \
}
#endif /* PCRE2_CODE_UNIT_WIDTH == [8|16|32] */
#endif /* SUPPORT_UNICODE */
static PCRE2_SPTR bracketend(PCRE2_SPTR cc)
{
SLJIT_ASSERT((*cc >= OP_ASSERT && *cc <= OP_ASSERT_SCS) || (*cc >= OP_ONCE && *cc <= OP_SCOND));
do cc += GET(cc, 1); while (*cc == OP_ALT);
SLJIT_ASSERT(*cc >= OP_KET && *cc <= OP_KETRPOS);
cc += 1 + LINK_SIZE;
return cc;
}
static int no_alternatives(PCRE2_SPTR cc)
{
int count = 0;
SLJIT_ASSERT((*cc >= OP_ASSERT && *cc <= OP_ASSERT_SCS) || (*cc >= OP_ONCE && *cc <= OP_SCOND));
do
{
cc += GET(cc, 1);
count++;
}
while (*cc == OP_ALT);
SLJIT_ASSERT(*cc >= OP_KET && *cc <= OP_KETRPOS);
return count;
}
static BOOL find_vreverse(PCRE2_SPTR cc)
{
SLJIT_ASSERT(*cc == OP_ASSERTBACK || *cc == OP_ASSERTBACK_NOT || *cc == OP_ASSERTBACK_NA);
do
{
if (cc[1 + LINK_SIZE] == OP_VREVERSE)
return TRUE;
cc += GET(cc, 1);
}
while (*cc == OP_ALT);
return FALSE;
}
/* Functions whose might need modification for all new supported opcodes:
next_opcode
check_opcode_types
set_private_data_ptrs
get_framesize
init_frame
get_recurse_data_length
copy_recurse_data
compile_matchingpath
compile_backtrackingpath
*/
static PCRE2_SPTR next_opcode(compiler_common *common, PCRE2_SPTR cc)
{
SLJIT_UNUSED_ARG(common);
switch(*cc)
{
case OP_SOD:
case OP_SOM:
case OP_SET_SOM:
case OP_NOT_WORD_BOUNDARY:
case OP_WORD_BOUNDARY:
case OP_NOT_DIGIT:
case OP_DIGIT:
case OP_NOT_WHITESPACE:
case OP_WHITESPACE:
case OP_NOT_WORDCHAR:
case OP_WORDCHAR:
case OP_ANY:
case OP_ALLANY:
case OP_NOTPROP:
case OP_PROP:
case OP_ANYNL:
case OP_NOT_HSPACE:
case OP_HSPACE:
case OP_NOT_VSPACE:
case OP_VSPACE:
case OP_EXTUNI:
case OP_EODN:
case OP_EOD:
case OP_CIRC:
case OP_CIRCM:
case OP_DOLL:
case OP_DOLLM:
case OP_CRSTAR:
case OP_CRMINSTAR:
case OP_CRPLUS:
case OP_CRMINPLUS:
case OP_CRQUERY:
case OP_CRMINQUERY:
case OP_CRRANGE:
case OP_CRMINRANGE:
case OP_CRPOSSTAR:
case OP_CRPOSPLUS:
case OP_CRPOSQUERY:
case OP_CRPOSRANGE:
case OP_CLASS:
case OP_NCLASS:
case OP_REF:
case OP_REFI:
case OP_DNREF:
case OP_DNREFI:
case OP_RECURSE:
case OP_CALLOUT:
case OP_ALT:
case OP_KET:
case OP_KETRMAX:
case OP_KETRMIN:
case OP_KETRPOS:
case OP_REVERSE:
case OP_VREVERSE:
case OP_ASSERT:
case OP_ASSERT_NOT:
case OP_ASSERTBACK:
case OP_ASSERTBACK_NOT:
case OP_ASSERT_NA:
case OP_ASSERTBACK_NA:
case OP_ASSERT_SCS:
case OP_ONCE:
case OP_SCRIPT_RUN:
case OP_BRA:
case OP_BRAPOS:
case OP_CBRA:
case OP_CBRAPOS:
case OP_COND:
case OP_SBRA:
case OP_SBRAPOS:
case OP_SCBRA:
case OP_SCBRAPOS:
case OP_SCOND:
case OP_CREF:
case OP_DNCREF:
case OP_RREF:
case OP_DNRREF:
case OP_FALSE:
case OP_TRUE:
case OP_BRAZERO:
case OP_BRAMINZERO: