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

Diff of /code/trunk/pcre_jit_compile.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 741 by zherczeg, Mon Oct 31 09:31:46 2011 UTC revision 1572 by zherczeg, Thu Jun 25 04:02:53 2015 UTC
# Line 6  Line 6 
6  and semantics are as close as possible to those of the Perl 5 language.  and semantics are as close as possible to those of the Perl 5 language.
7    
8                         Written by Philip Hazel                         Written by Philip Hazel
9             Copyright (c) 1997-2008 University of Cambridge             Copyright (c) 1997-2013 University of Cambridge
10    
11    The machine code generator part (this module) was written by Zoltan Herczeg    The machine code generator part (this module) was written by Zoltan Herczeg
12                        Copyright (c) 2010-2011                        Copyright (c) 2010-2013
13    
14  -----------------------------------------------------------------------------  -----------------------------------------------------------------------------
15  Redistribution and use in source and binary forms, with or without  Redistribution and use in source and binary forms, with or without
# Line 46  POSSIBILITY OF SUCH DAMAGE. Line 46  POSSIBILITY OF SUCH DAMAGE.
46    
47  #include "pcre_internal.h"  #include "pcre_internal.h"
48    
49  #ifdef SUPPORT_JIT  #if defined SUPPORT_JIT
50    
51  /* All-in-one: Since we use the JIT compiler only from here,  /* 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  we just include it. This way we don't need to touch the build
53  system files. */  system files. */
54    
55    #define SLJIT_MALLOC(size, allocator_data) (PUBL(malloc))(size)
56    #define SLJIT_FREE(ptr, allocator_data) (PUBL(free))(ptr)
57  #define SLJIT_CONFIG_AUTO 1  #define SLJIT_CONFIG_AUTO 1
58  #define SLJIT_CONFIG_STATIC 1  #define SLJIT_CONFIG_STATIC 1
59  #define SLJIT_VERBOSE 0  #define SLJIT_VERBOSE 0
# Line 60  system files. */ Line 62  system files. */
62  #include "sljit/sljitLir.c"  #include "sljit/sljitLir.c"
63    
64  #if defined SLJIT_CONFIG_UNSUPPORTED && SLJIT_CONFIG_UNSUPPORTED  #if defined SLJIT_CONFIG_UNSUPPORTED && SLJIT_CONFIG_UNSUPPORTED
65  #error "Unsupported architecture"  #error Unsupported architecture
66  #endif  #endif
67    
68  /* Allocate memory on the stack. Fast, but limited size. */  /* Defines for debugging purposes. */
 #define LOCAL_SPACE_SIZE 32768  
69    
70    /* 1 - Use unoptimized capturing brackets.
71       2 - Enable capture_last_ptr (includes option 1). */
72    /* #define DEBUG_FORCE_UNOPTIMIZED_CBRAS 2 */
73    
74    /* 1 - Always have a control head. */
75    /* #define DEBUG_FORCE_CONTROL_HEAD 1 */
76    
77    /* Allocate memory for the regex stack on the real machine stack.
78    Fast, but limited size. */
79    #define MACHINE_STACK_SIZE 32768
80    
81    /* Growth rate for stack allocated by the OS. Should be the multiply
82    of page size. */
83  #define STACK_GROWTH_RATE 8192  #define STACK_GROWTH_RATE 8192
84    
85  /* Enable to check that the allocation could destroy temporaries. */  /* Enable to check that the allocation could destroy temporaries. */
# Line 80  The code generator follows the recursive Line 94  The code generator follows the recursive
94  expressions. The basic blocks of regular expressions are condition checkers  expressions. The basic blocks of regular expressions are condition checkers
95  whose execute different commands depending on the result of the condition check.  whose execute different commands depending on the result of the condition check.
96  The relationship between the operators can be horizontal (concatenation) and  The relationship between the operators can be horizontal (concatenation) and
97  vertical (sub-expression) (See struct fallback_common for more details).  vertical (sub-expression) (See struct backtrack_common for more details).
98    
99    'ab' - 'a' and 'b' regexps are concatenated    'ab' - 'a' and 'b' regexps are concatenated
100    'a+' - 'a' is the sub-expression of the '+' operator    'a+' - 'a' is the sub-expression of the '+' operator
101    
102  The condition checkers are boolean (true/false) checkers. Machine code is generated  The condition checkers are boolean (true/false) checkers. Machine code is generated
103  for the checker itself and for the actions depending on the result of the checker.  for the checker itself and for the actions depending on the result of the checker.
104  The 'true' case is called as the hot path (expected path), and the other is called as  The 'true' case is called as the matching path (expected path), and the other is called as
105  the 'fallback' path. Branch instructions are expesive for all CPUs, so we avoid taken  the 'backtrack' path. Branch instructions are expesive for all CPUs, so we avoid taken
106  branches on the hot path.  branches on the matching path.
107    
108   Greedy star operator (*) :   Greedy star operator (*) :
109     Hot path: match happens.     Matching path: match happens.
110     Fallback path: match failed.     Backtrack path: match failed.
111   Non-greedy star operator (*?) :   Non-greedy star operator (*?) :
112     Hot path: no need to perform a match.     Matching path: no need to perform a match.
113     Fallback path: match is required.     Backtrack path: match is required.
114    
115  The following example shows how the code generated for a capturing bracket  The following example shows how the code generated for a capturing bracket
116  with two alternatives. Let A, B, C, D are arbirary regular expressions, and  with two alternatives. Let A, B, C, D are arbirary regular expressions, and
# Line 106  we have the following regular expression Line 120  we have the following regular expression
120    
121  The generated code will be the following:  The generated code will be the following:
122    
123   A hot path   A matching path
124   '(' hot path (pushing arguments to the stack)   '(' matching path (pushing arguments to the stack)
125   B hot path   B matching path
126   ')' hot path (pushing arguments to the stack)   ')' matching path (pushing arguments to the stack)
127   D hot path   D matching path
128   return with successful match   return with successful match
129    
130   D fallback path   D backtrack path
131   ')' fallback path (If we arrived from "C" jump to the fallback of "C")   ')' backtrack path (If we arrived from "C" jump to the backtrack of "C")
132   B fallback path   B backtrack path
133   C expected path   C expected path
134   jump to D hot path   jump to D matching path
135   C fallback path   C backtrack path
136   A fallback path   A backtrack path
137    
138   Notice, that the order of fallback code paths are the opposite of the fast   Notice, that the order of backtrack code paths are the opposite of the fast
139   code paths. In this way the topmost value on the stack is always belong   code paths. In this way the topmost value on the stack is always belong
140   to the current fallback code path. The fallback code path must check   to the current backtrack code path. The backtrack path must check
141   whether there is a next alternative. If so, it needs to jump back to   whether there is a next alternative. If so, it needs to jump back to
142   the hot path eventually. Otherwise it needs to clear out its own stack   the matching path eventually. Otherwise it needs to clear out its own stack
143   frame and continue the execution on the fallback code paths.   frame and continue the execution on the backtrack code paths.
144  */  */
145    
146  /*  /*
147  Saved stack frames:  Saved stack frames:
148    
149  Atomic blocks and asserts require reloading the values of local variables  Atomic blocks and asserts require reloading the values of private data
150  when the fallback mechanism performed. Because of OP_RECURSE, the locals  when the backtrack mechanism performed. Because of OP_RECURSE, the data
151  are not necessarly known in compile time, thus we need a dynamic restore  are not necessarly known in compile time, thus we need a dynamic restore
152  mechanism.  mechanism.
153    
154  The stack frames are stored in a chain list, and have the following format:  The stack frames are stored in a chain list, and have the following format:
155  ([ capturing bracket offset ][ start value ][ end value ])+ ... [ 0 ] [ previous head ]  ([ capturing bracket offset ][ start value ][ end value ])+ ... [ 0 ] [ previous head ]
156    
157  Thus we can restore the locals to a particular point in the stack.  Thus we can restore the private data to a particular point in the stack.
158  */  */
159    
160  typedef struct jit_arguments {  typedef struct jit_arguments {
161    /* Pointers first. */    /* Pointers first. */
162    struct sljit_stack *stack;    struct sljit_stack *stack;
163    PCRE_SPTR str;    const pcre_uchar *str;
164    PCRE_SPTR begin;    const pcre_uchar *begin;
165    PCRE_SPTR end;    const pcre_uchar *end;
166    int *offsets;    int *offsets;
167    uschar *ptr;    pcre_uchar *uchar_ptr;
168      pcre_uchar *mark_ptr;
169      void *callout_data;
170    /* Everything else after. */    /* Everything else after. */
171    int offsetcount;    pcre_uint32 limit_match;
172    int calllimit;    int real_offset_count;
173    uschar notbol;    int offset_count;
174    uschar noteol;    pcre_uint8 notbol;
175    uschar notempty;    pcre_uint8 noteol;
176    uschar notempty_atstart;    pcre_uint8 notempty;
177      pcre_uint8 notempty_atstart;
178  } jit_arguments;  } jit_arguments;
179    
180  typedef struct executable_function {  typedef struct executable_functions {
181    void *executable_func;    void *executable_funcs[JIT_NUMBER_OF_COMPILE_MODES];
182    pcre_jit_callback callback;    void *read_only_data_heads[JIT_NUMBER_OF_COMPILE_MODES];
183      sljit_uw executable_sizes[JIT_NUMBER_OF_COMPILE_MODES];
184      PUBL(jit_callback) callback;
185    void *userdata;    void *userdata;
186  } executable_function;    pcre_uint32 top_bracket;
187      pcre_uint32 limit_match;
188    } executable_functions;
189    
190  typedef struct jump_list {  typedef struct jump_list {
191    struct sljit_jump *jump;    struct sljit_jump *jump;
192    struct jump_list *next;    struct jump_list *next;
193  } jump_list;  } jump_list;
194    
 enum stub_types { stack_alloc };  
   
195  typedef struct stub_list {  typedef struct stub_list {
   enum stub_types type;  
   int data;  
196    struct sljit_jump *start;    struct sljit_jump *start;
197    struct sljit_label *leave;    struct sljit_label *quit;
198    struct stub_list *next;    struct stub_list *next;
199  } stub_list;  } stub_list;
200    
201    typedef struct label_addr_list {
202      struct sljit_label *label;
203      sljit_uw *update_addr;
204      struct label_addr_list *next;
205    } label_addr_list;
206    
207    enum frame_types {
208      no_frame = -1,
209      no_stack = -2
210    };
211    
212    enum control_types {
213      type_mark = 0,
214      type_then_trap = 1
215    };
216    
217  typedef int (SLJIT_CALL *jit_function)(jit_arguments *args);  typedef int (SLJIT_CALL *jit_function)(jit_arguments *args);
218    
219  /* The following structure is the key data type for the recursive  /* The following structure is the key data type for the recursive
220  code generator. It is allocated by compile_hotpath, and contains  code generator. It is allocated by compile_matchingpath, and contains
221  the aguments for compile_fallbackpath. Must be the first member  the arguments for compile_backtrackingpath. Must be the first member
222  of its descendants. */  of its descendants. */
223  typedef struct fallback_common {  typedef struct backtrack_common {
224    /* Concatenation stack. */    /* Concatenation stack. */
225    struct fallback_common *prev;    struct backtrack_common *prev;
226    jump_list *nextfallbacks;    jump_list *nextbacktracks;
227    /* Internal stack (for component operators). */    /* Internal stack (for component operators). */
228    struct fallback_common *top;    struct backtrack_common *top;
229    jump_list *topfallbacks;    jump_list *topbacktracks;
230    /* Opcode pointer. */    /* Opcode pointer. */
231    uschar *cc;    pcre_uchar *cc;
232  } fallback_common;  } backtrack_common;
233    
234  typedef struct assert_fallback {  typedef struct assert_backtrack {
235    fallback_common common;    backtrack_common common;
236    jump_list *condfailed;    jump_list *condfailed;
237    /* Less than 0 (-1) if a frame is not needed. */    /* Less than 0 if a frame is not needed. */
238    int framesize;    int framesize;
239    /* Points to our private memory word on the stack. */    /* Points to our private memory word on the stack. */
240    int localptr;    int private_data_ptr;
241    /* For iterators. */    /* For iterators. */
242    struct sljit_label *hotpath;    struct sljit_label *matchingpath;
243  } assert_fallback;  } assert_backtrack;
244    
245  typedef struct bracket_fallback {  typedef struct bracket_backtrack {
246    fallback_common common;    backtrack_common common;
247    /* Where to coninue if an alternative is successfully matched. */    /* Where to coninue if an alternative is successfully matched. */
248    struct sljit_label *althotpath;    struct sljit_label *alternative_matchingpath;
249    /* For rmin and rmax iterators. */    /* For rmin and rmax iterators. */
250    struct sljit_label *recursivehotpath;    struct sljit_label *recursive_matchingpath;
251    /* For greedy ? operator. */    /* For greedy ? operator. */
252    struct sljit_label *zerohotpath;    struct sljit_label *zero_matchingpath;
253    /* Contains the branches of a failed condition. */    /* Contains the branches of a failed condition. */
254    union {    union {
255      /* Both for OP_COND, OP_SCOND. */      /* Both for OP_COND, OP_SCOND. */
256      jump_list *condfailed;      jump_list *condfailed;
257      assert_fallback *assert;      assert_backtrack *assert;
258      /* For OP_ONCE. -1 if not needed. */      /* For OP_ONCE. Less than 0 if not needed. */
259      int framesize;      int framesize;
260    } u;    } u;
261    /* Points to our private memory word on the stack. */    /* Points to our private memory word on the stack. */
262    int localptr;    int private_data_ptr;
263  } bracket_fallback;  } bracket_backtrack;
264    
265  typedef struct bracketpos_fallback {  typedef struct bracketpos_backtrack {
266    fallback_common common;    backtrack_common common;
267    /* Points to our private memory word on the stack. */    /* Points to our private memory word on the stack. */
268    int localptr;    int private_data_ptr;
269    /* Reverting stack is needed. */    /* Reverting stack is needed. */
270    int framesize;    int framesize;
271    /* Allocated stack size. */    /* Allocated stack size. */
272    int stacksize;    int stacksize;
273  } bracketpos_fallback;  } bracketpos_backtrack;
274    
275  typedef struct braminzero_fallback {  typedef struct braminzero_backtrack {
276    fallback_common common;    backtrack_common common;
277    struct sljit_label *hotpath;    struct sljit_label *matchingpath;
278  } braminzero_fallback;  } braminzero_backtrack;
279    
280  typedef struct iterator_fallback {  typedef struct iterator_backtrack {
281    fallback_common common;    backtrack_common common;
282    /* Next iteration. */    /* Next iteration. */
283    struct sljit_label *hotpath;    struct sljit_label *matchingpath;
284  } iterator_fallback;  } iterator_backtrack;
285    
286  typedef struct recurse_entry {  typedef struct recurse_entry {
287    struct recurse_entry *next;    struct recurse_entry *next;
# Line 257  typedef struct recurse_entry { Line 290  typedef struct recurse_entry {
290    /* Collects the calls until the function is not created. */    /* Collects the calls until the function is not created. */
291    jump_list *calls;    jump_list *calls;
292    /* Points to the starting opcode. */    /* Points to the starting opcode. */
293    int start;    sljit_sw start;
294  } recurse_entry;  } recurse_entry;
295    
296  typedef struct recurse_fallback {  typedef struct recurse_backtrack {
297    fallback_common common;    backtrack_common common;
298  } recurse_fallback;    BOOL inlined_pattern;
299    } recurse_backtrack;
300    
301    #define OP_THEN_TRAP OP_TABLE_LENGTH
302    
303    typedef struct then_trap_backtrack {
304      backtrack_common common;
305      /* If then_trap is not NULL, this structure contains the real
306      then_trap for the backtracking path. */
307      struct then_trap_backtrack *then_trap;
308      /* Points to the starting opcode. */
309      sljit_sw start;
310      /* Exit point for the then opcodes of this alternative. */
311      jump_list *quit;
312      /* Frame size of the current alternative. */
313      int framesize;
314    } then_trap_backtrack;
315    
316    #define MAX_RANGE_SIZE 4
317    
318  typedef struct compiler_common {  typedef struct compiler_common {
319      /* The sljit ceneric compiler. */
320    struct sljit_compiler *compiler;    struct sljit_compiler *compiler;
321    uschar *start;    /* First byte code. */
322    int localsize;    pcre_uchar *start;
323    int *localptrs;    /* Maps private data offset to each opcode. */
324    const uschar *fcc;    sljit_si *private_data_ptrs;
325    sljit_w lcc;    /* Chain list of read-only data ptrs. */
326    int cbraptr;    void *read_only_data_head;
327      /* Tells whether the capturing bracket is optimized. */
328      pcre_uint8 *optimized_cbracket;
329      /* Tells whether the starting offset is a target of then. */
330      pcre_uint8 *then_offsets;
331      /* Current position where a THEN must jump. */
332      then_trap_backtrack *then_trap;
333      /* Starting offset of private data for capturing brackets. */
334      int cbra_ptr;
335      /* Output vector starting point. Must be divisible by 2. */
336      int ovector_start;
337      /* Last known position of the requested byte. */
338      int req_char_ptr;
339      /* Head of the last recursion. */
340      int recursive_head_ptr;
341      /* First inspected character for partial matching. */
342      int start_used_ptr;
343      /* Starting pointer for partial soft matches. */
344      int hit_start;
345      /* End pointer of the first line. */
346      int first_line_end;
347      /* Points to the marked string. */
348      int mark_ptr;
349      /* Recursive control verb management chain. */
350      int control_head_ptr;
351      /* Points to the last matched capture block index. */
352      int capture_last_ptr;
353      /* Points to the starting position of the current match. */
354      int start_ptr;
355    
356      /* Flipped and lower case tables. */
357      const pcre_uint8 *fcc;
358      sljit_sw lcc;
359      /* Mode can be PCRE_STUDY_JIT_COMPILE and others. */
360      int mode;
361      /* TRUE, when minlength is greater than 0. */
362      BOOL might_be_empty;
363      /* \K is found in the pattern. */
364      BOOL has_set_som;
365      /* (*SKIP:arg) is found in the pattern. */
366      BOOL has_skip_arg;
367      /* (*THEN) is found in the pattern. */
368      BOOL has_then;
369      /* Needs to know the start position anytime. */
370      BOOL needs_start_ptr;
371      /* Currently in recurse or negative assert. */
372      BOOL local_exit;
373      /* Currently in a positive assert. */
374      BOOL positive_assert;
375      /* Newline control. */
376    int nltype;    int nltype;
377      pcre_uint32 nlmax;
378      pcre_uint32 nlmin;
379    int newline;    int newline;
380    int bsr_nltype;    int bsr_nltype;
381      pcre_uint32 bsr_nlmax;
382      pcre_uint32 bsr_nlmin;
383      /* Dollar endonly. */
384    int endonly;    int endonly;
385    sljit_w ctypes;    /* Tables. */
386    sljit_uw name_table;    sljit_sw ctypes;
387    sljit_w name_count;    /* Named capturing brackets. */
388    sljit_w name_entry_size;    pcre_uchar *name_table;
389    struct sljit_label *acceptlabel;    sljit_sw name_count;
390      sljit_sw name_entry_size;
391    
392      /* Labels and jump lists. */
393      struct sljit_label *partialmatchlabel;
394      struct sljit_label *quit_label;
395      struct sljit_label *forced_quit_label;
396      struct sljit_label *accept_label;
397      struct sljit_label *ff_newline_shortcut;
398    stub_list *stubs;    stub_list *stubs;
399      label_addr_list *label_addrs;
400    recurse_entry *entries;    recurse_entry *entries;
401    recurse_entry *currententry;    recurse_entry *currententry;
402      jump_list *partialmatch;
403      jump_list *quit;
404      jump_list *positive_assert_quit;
405      jump_list *forced_quit;
406    jump_list *accept;    jump_list *accept;
407    jump_list *calllimit;    jump_list *calllimit;
408    jump_list *stackalloc;    jump_list *stackalloc;
# Line 294  typedef struct compiler_common { Line 413  typedef struct compiler_common {
413    jump_list *vspace;    jump_list *vspace;
414    jump_list *casefulcmp;    jump_list *casefulcmp;
415    jump_list *caselesscmp;    jump_list *caselesscmp;
416      jump_list *reset_match;
417    BOOL jscript_compat;    BOOL jscript_compat;
418  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF
419    BOOL utf8;    BOOL utf;
420  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
421    BOOL useucp;    BOOL use_ucp;
422  #endif  #endif
423    jump_list *utf8readchar;  #ifdef COMPILE_PCRE8
424    jump_list *utf8readtype8;    jump_list *utfreadchar;
425      jump_list *utfreadchar16;
426      jump_list *utfreadtype8;
427  #endif  #endif
428    #endif /* SUPPORT_UTF */
429  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
430    jump_list *getucd;    jump_list *getucd;
431  #endif  #endif
# Line 314  typedef struct compare_context { Line 437  typedef struct compare_context {
437    int length;    int length;
438    int sourcereg;    int sourcereg;
439  #if defined SLJIT_UNALIGNED && SLJIT_UNALIGNED  #if defined SLJIT_UNALIGNED && SLJIT_UNALIGNED
440    int byteptr;    int ucharptr;
441    union {    union {
442      int asint;      sljit_si asint;
443      short asshort;      sljit_uh asushort;
444    #if defined COMPILE_PCRE8
445      sljit_ub asbyte;      sljit_ub asbyte;
446      sljit_ub asbytes[4];      sljit_ub asuchars[4];
447    #elif defined COMPILE_PCRE16
448        sljit_uh asuchars[2];
449    #elif defined COMPILE_PCRE32
450        sljit_ui asuchars[1];
451    #endif
452    } c;    } c;
453    union {    union {
454      int asint;      sljit_si asint;
455      short asshort;      sljit_uh asushort;
456    #if defined COMPILE_PCRE8
457      sljit_ub asbyte;      sljit_ub asbyte;
458      sljit_ub asbytes[4];      sljit_ub asuchars[4];
459    #elif defined COMPILE_PCRE16
460        sljit_uh asuchars[2];
461    #elif defined COMPILE_PCRE32
462        sljit_ui asuchars[1];
463    #endif
464    } oc;    } oc;
465  #endif  #endif
466  } compare_context;  } compare_context;
467    
468  enum {  /* Undefine sljit macros. */
469    frame_end = 0,  #undef CMP
   frame_setstrbegin = -1  
 };  
470    
471  /* Used for accessing the elements of the stack. */  /* Used for accessing the elements of the stack. */
472  #define STACK(i)      ((-(i) - 1) * (int)sizeof(sljit_w))  #define STACK(i)      ((-(i) - 1) * (int)sizeof(sljit_sw))
473    
474  #define TMP1          SLJIT_TEMPORARY_REG1  #define TMP1          SLJIT_R0
475  #define TMP2          SLJIT_TEMPORARY_REG3  #define TMP2          SLJIT_R2
476  #define TMP3          SLJIT_TEMPORARY_EREG2  #define TMP3          SLJIT_R3
477  #define STR_PTR       SLJIT_GENERAL_REG1  #define STR_PTR       SLJIT_S0
478  #define STR_END       SLJIT_GENERAL_REG2  #define STR_END       SLJIT_S1
479  #define STACK_TOP     SLJIT_TEMPORARY_REG2  #define STACK_TOP     SLJIT_R1
480  #define STACK_LIMIT   SLJIT_GENERAL_REG3  #define STACK_LIMIT   SLJIT_S2
481  #define ARGUMENTS     SLJIT_GENERAL_EREG1  #define COUNT_MATCH   SLJIT_S3
482  #define CALL_COUNT    SLJIT_GENERAL_EREG2  #define ARGUMENTS     SLJIT_S4
483  #define RETURN_ADDR   SLJIT_TEMPORARY_EREG1  #define RETURN_ADDR   SLJIT_R4
484    
485  /* Locals layout. */  /* Local space layout. */
486  /* These two locals can be used by the current opcode. */  /* These two locals can be used by the current opcode. */
487  #define LOCALS0          (0 * sizeof(sljit_w))  #define LOCALS0          (0 * sizeof(sljit_sw))
488  #define LOCALS1          (1 * sizeof(sljit_w))  #define LOCALS1          (1 * sizeof(sljit_sw))
489  /* Two local variables for possessive quantifiers (char1 cannot use them). */  /* Two local variables for possessive quantifiers (char1 cannot use them). */
490  #define POSSESSIVE0      (2 * sizeof(sljit_w))  #define POSSESSIVE0      (2 * sizeof(sljit_sw))
491  #define POSSESSIVE1      (3 * sizeof(sljit_w))  #define POSSESSIVE1      (3 * sizeof(sljit_sw))
 /* Head of the last recursion. */  
 #define RECURSIVE_HEAD   (4 * sizeof(sljit_w))  
492  /* Max limit of recursions. */  /* Max limit of recursions. */
493  #define CALL_LIMIT       (5 * sizeof(sljit_w))  #define LIMIT_MATCH      (4 * sizeof(sljit_sw))
 /* Last known position of the requested byte. */  
 #define REQ_BYTE_PTR     (6 * sizeof(sljit_w))  
 /* End pointer of the first line. */  
 #define FIRSTLINE_END    (7 * sizeof(sljit_w))  
494  /* The output vector is stored on the stack, and contains pointers  /* The output vector is stored on the stack, and contains pointers
495  to characters. The vector data is divided into two groups: the first  to characters. The vector data is divided into two groups: the first
496  group contains the start / end character pointers, and the second is  group contains the start / end character pointers, and the second is
497  the start pointers when the end of the capturing group has not yet reached. */  the start pointers when the end of the capturing group has not yet reached. */
498  #define OVECTOR_START    (8 * sizeof(sljit_w))  #define OVECTOR_START    (common->ovector_start)
499  #define OVECTOR(i)       (OVECTOR_START + (i) * sizeof(sljit_w))  #define OVECTOR(i)       (OVECTOR_START + (i) * (sljit_sw)sizeof(sljit_sw))
500  #define OVECTOR_PRIV(i)  (common->cbraptr + (i) * sizeof(sljit_w))  #define OVECTOR_PRIV(i)  (common->cbra_ptr + (i) * (sljit_sw)sizeof(sljit_sw))
501  #define PRIV(cc)         (common->localptrs[(cc) - common->start])  #define PRIVATE_DATA(cc) (common->private_data_ptrs[(cc) - common->start])
502    
503    #if defined COMPILE_PCRE8
504    #define MOV_UCHAR  SLJIT_MOV_UB
505    #define MOVU_UCHAR SLJIT_MOVU_UB
506    #elif defined COMPILE_PCRE16
507    #define MOV_UCHAR  SLJIT_MOV_UH
508    #define MOVU_UCHAR SLJIT_MOVU_UH
509    #elif defined COMPILE_PCRE32
510    #define MOV_UCHAR  SLJIT_MOV_UI
511    #define MOVU_UCHAR SLJIT_MOVU_UI
512    #else
513    #error Unsupported compiling mode
514    #endif
515    
516  /* Shortcuts. */  /* Shortcuts. */
517  #define DEFINE_COMPILER \  #define DEFINE_COMPILER \
# Line 388  the start pointers when the end of the c Line 528  the start pointers when the end of the c
528    sljit_set_label(sljit_emit_jump(compiler, (type)), (label))    sljit_set_label(sljit_emit_jump(compiler, (type)), (label))
529  #define JUMPHERE(jump) \  #define JUMPHERE(jump) \
530    sljit_set_label((jump), sljit_emit_label(compiler))    sljit_set_label((jump), sljit_emit_label(compiler))
531    #define SET_LABEL(jump, label) \
532      sljit_set_label((jump), (label))
533  #define CMP(type, src1, src1w, src2, src2w) \  #define CMP(type, src1, src1w, src2, src2w) \
534    sljit_emit_cmp(compiler, (type), (src1), (src1w), (src2), (src2w))    sljit_emit_cmp(compiler, (type), (src1), (src1w), (src2), (src2w))
535  #define CMPTO(type, src1, src1w, src2, src2w, label) \  #define CMPTO(type, src1, src1w, src2, src2w, label) \
536    sljit_set_label(sljit_emit_cmp(compiler, (type), (src1), (src1w), (src2), (src2w)), (label))    sljit_set_label(sljit_emit_cmp(compiler, (type), (src1), (src1w), (src2), (src2w)), (label))
537  #define COND_VALUE(op, dst, dstw, type) \  #define OP_FLAGS(op, dst, dstw, src, srcw, type) \
538    sljit_emit_cond_value(compiler, (op), (dst), (dstw), (type))    sljit_emit_op_flags(compiler, (op), (dst), (dstw), (src), (srcw), (type))
539    #define GET_LOCAL_BASE(dst, dstw, offset) \
540      sljit_get_local_base(compiler, (dst), (dstw), (offset))
541    
542    #define READ_CHAR_MAX 0x7fffffff
543    
544  static uschar* bracketend(uschar* cc)  static pcre_uchar *bracketend(pcre_uchar *cc)
545  {  {
546  SLJIT_ASSERT((*cc >= OP_ASSERT && *cc <= OP_ASSERTBACK_NOT) || (*cc >= OP_ONCE && *cc <= OP_SCOND));  SLJIT_ASSERT((*cc >= OP_ASSERT && *cc <= OP_ASSERTBACK_NOT) || (*cc >= OP_ONCE && *cc <= OP_SCOND));
547  do cc += GET(cc, 1); while (*cc == OP_ALT);  do cc += GET(cc, 1); while (*cc == OP_ALT);
# Line 404  cc += 1 + LINK_SIZE; Line 550  cc += 1 + LINK_SIZE;
550  return cc;  return cc;
551  }  }
552    
553    static int no_alternatives(pcre_uchar *cc)
554    {
555    int count = 0;
556    SLJIT_ASSERT((*cc >= OP_ASSERT && *cc <= OP_ASSERTBACK_NOT) || (*cc >= OP_ONCE && *cc <= OP_SCOND));
557    do
558      {
559      cc += GET(cc, 1);
560      count++;
561      }
562    while (*cc == OP_ALT);
563    SLJIT_ASSERT(*cc >= OP_KET && *cc <= OP_KETRPOS);
564    return count;
565    }
566    
567    static int ones_in_half_byte[16] = {
568      /* 0 */ 0, 1, 1, 2, /* 4 */ 1, 2, 2, 3,
569      /* 8 */ 1, 2, 2, 3, /* 12 */ 2, 3, 3, 4
570    };
571    
572  /* Functions whose might need modification for all new supported opcodes:  /* Functions whose might need modification for all new supported opcodes:
573   next_opcode   next_opcode
574   get_localspace   check_opcode_types
575   set_localptrs   set_private_data_ptrs
576   get_framesize   get_framesize
577   init_frame   init_frame
578   get_localsize   get_private_data_copy_length
579   copy_locals   copy_private_data
580   compile_hotpath   compile_matchingpath
581   compile_fallbackpath   compile_backtrackingpath
582  */  */
583    
584  static uschar *next_opcode(compiler_common *common, uschar *cc)  static pcre_uchar *next_opcode(compiler_common *common, pcre_uchar *cc)
585  {  {
586  SLJIT_UNUSED_ARG(common);  SLJIT_UNUSED_ARG(common);
587  switch(*cc)  switch(*cc)
# Line 434  switch(*cc) Line 599  switch(*cc)
599    case OP_WORDCHAR:    case OP_WORDCHAR:
600    case OP_ANY:    case OP_ANY:
601    case OP_ALLANY:    case OP_ALLANY:
602      case OP_NOTPROP:
603      case OP_PROP:
604    case OP_ANYNL:    case OP_ANYNL:
605    case OP_NOT_HSPACE:    case OP_NOT_HSPACE:
606    case OP_HSPACE:    case OP_HSPACE:
# Line 446  switch(*cc) Line 613  switch(*cc)
613    case OP_CIRCM:    case OP_CIRCM:
614    case OP_DOLL:    case OP_DOLL:
615    case OP_DOLLM:    case OP_DOLLM:
   case OP_TYPESTAR:  
   case OP_TYPEMINSTAR:  
   case OP_TYPEPLUS:  
   case OP_TYPEMINPLUS:  
   case OP_TYPEQUERY:  
   case OP_TYPEMINQUERY:  
   case OP_TYPEPOSSTAR:  
   case OP_TYPEPOSPLUS:  
   case OP_TYPEPOSQUERY:  
616    case OP_CRSTAR:    case OP_CRSTAR:
617    case OP_CRMINSTAR:    case OP_CRMINSTAR:
618    case OP_CRPLUS:    case OP_CRPLUS:
619    case OP_CRMINPLUS:    case OP_CRMINPLUS:
620    case OP_CRQUERY:    case OP_CRQUERY:
621    case OP_CRMINQUERY:    case OP_CRMINQUERY:
622      case OP_CRRANGE:
623      case OP_CRMINRANGE:
624      case OP_CRPOSSTAR:
625      case OP_CRPOSPLUS:
626      case OP_CRPOSQUERY:
627      case OP_CRPOSRANGE:
628      case OP_CLASS:
629      case OP_NCLASS:
630      case OP_REF:
631      case OP_REFI:
632      case OP_DNREF:
633      case OP_DNREFI:
634      case OP_RECURSE:
635      case OP_CALLOUT:
636      case OP_ALT:
637      case OP_KET:
638      case OP_KETRMAX:
639      case OP_KETRMIN:
640      case OP_KETRPOS:
641      case OP_REVERSE:
642      case OP_ASSERT:
643      case OP_ASSERT_NOT:
644      case OP_ASSERTBACK:
645      case OP_ASSERTBACK_NOT:
646      case OP_ONCE:
647      case OP_ONCE_NC:
648      case OP_BRA:
649      case OP_BRAPOS:
650      case OP_CBRA:
651      case OP_CBRAPOS:
652      case OP_COND:
653      case OP_SBRA:
654      case OP_SBRAPOS:
655      case OP_SCBRA:
656      case OP_SCBRAPOS:
657      case OP_SCOND:
658      case OP_CREF:
659      case OP_DNCREF:
660      case OP_RREF:
661      case OP_DNRREF:
662    case OP_DEF:    case OP_DEF:
663    case OP_BRAZERO:    case OP_BRAZERO:
664    case OP_BRAMINZERO:    case OP_BRAMINZERO:
665    case OP_BRAPOSZERO:    case OP_BRAPOSZERO:
666      case OP_PRUNE:
667      case OP_SKIP:
668      case OP_THEN:
669      case OP_COMMIT:
670    case OP_FAIL:    case OP_FAIL:
671    case OP_ACCEPT:    case OP_ACCEPT:
672    case OP_ASSERT_ACCEPT:    case OP_ASSERT_ACCEPT:
673      case OP_CLOSE:
674    case OP_SKIPZERO:    case OP_SKIPZERO:
675    return cc + 1;    return cc + PRIV(OP_lengths)[*cc];
   
   case OP_ANYBYTE:  
 #ifdef SUPPORT_UTF8  
   if (common->utf8) return NULL;  
 #endif  
   return cc + 1;  
676    
677    case OP_CHAR:    case OP_CHAR:
678    case OP_CHARI:    case OP_CHARI:
679    case OP_NOT:    case OP_NOT:
680    case OP_NOTI:    case OP_NOTI:
   
681    case OP_STAR:    case OP_STAR:
682    case OP_MINSTAR:    case OP_MINSTAR:
683    case OP_PLUS:    case OP_PLUS:
684    case OP_MINPLUS:    case OP_MINPLUS:
685    case OP_QUERY:    case OP_QUERY:
686    case OP_MINQUERY:    case OP_MINQUERY:
687      case OP_UPTO:
688      case OP_MINUPTO:
689      case OP_EXACT:
690    case OP_POSSTAR:    case OP_POSSTAR:
691    case OP_POSPLUS:    case OP_POSPLUS:
692    case OP_POSQUERY:    case OP_POSQUERY:
693      case OP_POSUPTO:
694    case OP_STARI:    case OP_STARI:
695    case OP_MINSTARI:    case OP_MINSTARI:
696    case OP_PLUSI:    case OP_PLUSI:
697    case OP_MINPLUSI:    case OP_MINPLUSI:
698    case OP_QUERYI:    case OP_QUERYI:
699    case OP_MINQUERYI:    case OP_MINQUERYI:
700      case OP_UPTOI:
701      case OP_MINUPTOI:
702      case OP_EXACTI:
703    case OP_POSSTARI:    case OP_POSSTARI:
704    case OP_POSPLUSI:    case OP_POSPLUSI:
705    case OP_POSQUERYI:    case OP_POSQUERYI:
706      case OP_POSUPTOI:
707    case OP_NOTSTAR:    case OP_NOTSTAR:
708    case OP_NOTMINSTAR:    case OP_NOTMINSTAR:
709    case OP_NOTPLUS:    case OP_NOTPLUS:
710    case OP_NOTMINPLUS:    case OP_NOTMINPLUS:
711    case OP_NOTQUERY:    case OP_NOTQUERY:
712    case OP_NOTMINQUERY:    case OP_NOTMINQUERY:
713      case OP_NOTUPTO:
714      case OP_NOTMINUPTO:
715      case OP_NOTEXACT:
716    case OP_NOTPOSSTAR:    case OP_NOTPOSSTAR:
717    case OP_NOTPOSPLUS:    case OP_NOTPOSPLUS:
718    case OP_NOTPOSQUERY:    case OP_NOTPOSQUERY:
719      case OP_NOTPOSUPTO:
720    case OP_NOTSTARI:    case OP_NOTSTARI:
721    case OP_NOTMINSTARI:    case OP_NOTMINSTARI:
722    case OP_NOTPLUSI:    case OP_NOTPLUSI:
723    case OP_NOTMINPLUSI:    case OP_NOTMINPLUSI:
724    case OP_NOTQUERYI:    case OP_NOTQUERYI:
725    case OP_NOTMINQUERYI:    case OP_NOTMINQUERYI:
   case OP_NOTPOSSTARI:  
   case OP_NOTPOSPLUSI:  
   case OP_NOTPOSQUERYI:  
   cc += 2;  
 #ifdef SUPPORT_UTF8  
   if (common->utf8 && cc[-1] >= 0xc0) cc += _pcre_utf8_table4[cc[-1] & 0x3f];  
 #endif  
   return cc;  
   
   case OP_UPTO:  
   case OP_MINUPTO:  
   case OP_EXACT:  
   case OP_POSUPTO:  
   case OP_UPTOI:  
   case OP_MINUPTOI:  
   case OP_EXACTI:  
   case OP_POSUPTOI:  
   case OP_NOTUPTO:  
   case OP_NOTMINUPTO:  
   case OP_NOTEXACT:  
   case OP_NOTPOSUPTO:  
726    case OP_NOTUPTOI:    case OP_NOTUPTOI:
727    case OP_NOTMINUPTOI:    case OP_NOTMINUPTOI:
728    case OP_NOTEXACTI:    case OP_NOTEXACTI:
729      case OP_NOTPOSSTARI:
730      case OP_NOTPOSPLUSI:
731      case OP_NOTPOSQUERYI:
732    case OP_NOTPOSUPTOI:    case OP_NOTPOSUPTOI:
733    cc += 4;    cc += PRIV(OP_lengths)[*cc];
734  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF
735    if (common->utf8 && cc[-1] >= 0xc0) cc += _pcre_utf8_table4[cc[-1] & 0x3f];    if (common->utf && HAS_EXTRALEN(cc[-1])) cc += GET_EXTRALEN(cc[-1]);
736  #endif  #endif
737    return cc;    return cc;
738    
739    case OP_NOTPROP:    /* Special cases. */
740    case OP_PROP:    case OP_TYPESTAR:
741      case OP_TYPEMINSTAR:
742      case OP_TYPEPLUS:
743      case OP_TYPEMINPLUS:
744      case OP_TYPEQUERY:
745      case OP_TYPEMINQUERY:
746    case OP_TYPEUPTO:    case OP_TYPEUPTO:
747    case OP_TYPEMINUPTO:    case OP_TYPEMINUPTO:
748    case OP_TYPEEXACT:    case OP_TYPEEXACT:
749      case OP_TYPEPOSSTAR:
750      case OP_TYPEPOSPLUS:
751      case OP_TYPEPOSQUERY:
752    case OP_TYPEPOSUPTO:    case OP_TYPEPOSUPTO:
753    case OP_REF:    return cc + PRIV(OP_lengths)[*cc] - 1;
   case OP_REFI:  
   case OP_CREF:  
   case OP_NCREF:  
   case OP_RREF:  
   case OP_NRREF:  
   case OP_CLOSE:  
   cc += 3;  
   return cc;  
754    
755    case OP_CRRANGE:    case OP_ANYBYTE:
756    case OP_CRMINRANGE:  #ifdef SUPPORT_UTF
757    return cc + 5;    if (common->utf) return NULL;
758    #endif
759    case OP_CLASS:    return cc + 1;
   case OP_NCLASS:  
   return cc + 33;  
760    
761  #ifdef SUPPORT_UTF8  #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
762    case OP_XCLASS:    case OP_XCLASS:
763    return cc + GET(cc, 1);    return cc + GET(cc, 1);
764  #endif  #endif
765    
766    case OP_RECURSE:    case OP_MARK:
767    case OP_ASSERT:    case OP_PRUNE_ARG:
768    case OP_ASSERT_NOT:    case OP_SKIP_ARG:
769    case OP_ASSERTBACK:    case OP_THEN_ARG:
770    case OP_ASSERTBACK_NOT:    return cc + 1 + 2 + cc[1];
   case OP_REVERSE:  
   case OP_ONCE:  
   case OP_ONCE_NC:  
   case OP_BRA:  
   case OP_BRAPOS:  
   case OP_COND:  
   case OP_SBRA:  
   case OP_SBRAPOS:  
   case OP_SCOND:  
   case OP_ALT:  
   case OP_KET:  
   case OP_KETRMAX:  
   case OP_KETRMIN:  
   case OP_KETRPOS:  
   return cc + 1 + LINK_SIZE;  
   
   case OP_CBRA:  
   case OP_CBRAPOS:  
   case OP_SCBRA:  
   case OP_SCBRAPOS:  
   return cc + 1 + LINK_SIZE + 2;  
771    
772    default:    default:
773      /* All opcodes are supported now! */
774      SLJIT_ASSERT_STOP();
775    return NULL;    return NULL;
776    }    }
777  }  }
778    
779  static int get_localspace(compiler_common *common, uschar *cc, uschar *ccend)  static BOOL check_opcode_types(compiler_common *common, pcre_uchar *cc, pcre_uchar *ccend)
780  {  {
781  int localspace = 0;  int count;
782  uschar *alternative;  pcre_uchar *slot;
783    
784  /* Calculate important variables (like stack size) and checks whether all opcodes are supported. */  /* Calculate important variables (like stack size) and checks whether all opcodes are supported. */
785  while (cc < ccend)  while (cc < ccend)
786    {    {
787    switch(*cc)    switch(*cc)
788      {      {
789      case OP_ASSERT:      case OP_SET_SOM:
790      case OP_ASSERT_NOT:      common->has_set_som = TRUE;
791      case OP_ASSERTBACK:      common->might_be_empty = TRUE;
792      case OP_ASSERTBACK_NOT:      cc += 1;
793      case OP_ONCE:      break;
794      case OP_ONCE_NC:  
795      case OP_BRAPOS:      case OP_REF:
796      case OP_SBRA:      case OP_REFI:
797      case OP_SBRAPOS:      common->optimized_cbracket[GET2(cc, 1)] = 0;
798      case OP_SCOND:      cc += 1 + IMM2_SIZE;
     localspace += sizeof(sljit_w);  
     cc += 1 + LINK_SIZE;  
799      break;      break;
800    
801      case OP_CBRAPOS:      case OP_CBRAPOS:
802      case OP_SCBRAPOS:      case OP_SCBRAPOS:
803      localspace += sizeof(sljit_w);      common->optimized_cbracket[GET2(cc, 1 + LINK_SIZE)] = 0;
804      cc += 1 + LINK_SIZE + 2;      cc += 1 + LINK_SIZE + IMM2_SIZE;
805      break;      break;
806    
807      case OP_COND:      case OP_COND:
     /* Might be a hidden SCOND. */  
     alternative = cc + GET(cc, 1);  
     if (*alternative == OP_KETRMAX || *alternative == OP_KETRMIN)  
       localspace += sizeof(sljit_w);  
     cc += 1 + LINK_SIZE;  
     break;  
   
     default:  
     cc = next_opcode(common, cc);  
     if (cc == NULL)  
       return -1;  
     break;  
     }  
   }  
 return localspace;  
 }  
   
 static void set_localptrs(compiler_common *common, int localptr, uschar *ccend)  
 {  
 uschar *cc = common->start;  
 uschar *alternative;  
 while (cc < ccend)  
   {  
   switch(*cc)  
     {  
     case OP_ASSERT:  
     case OP_ASSERT_NOT:  
     case OP_ASSERTBACK:  
     case OP_ASSERTBACK_NOT:  
     case OP_ONCE:  
     case OP_ONCE_NC:  
     case OP_BRAPOS:  
     case OP_SBRA:  
     case OP_SBRAPOS:  
808      case OP_SCOND:      case OP_SCOND:
809      common->localptrs[cc - common->start] = localptr;      /* Only AUTO_CALLOUT can insert this opcode. We do
810      localptr += sizeof(sljit_w);         not intend to support this case. */
811        if (cc[1 + LINK_SIZE] == OP_CALLOUT)
812          return FALSE;
813      cc += 1 + LINK_SIZE;      cc += 1 + LINK_SIZE;
814      break;      break;
815    
816      case OP_CBRAPOS:      case OP_CREF:
817      case OP_SCBRAPOS:      common->optimized_cbracket[GET2(cc, 1)] = 0;
818      common->localptrs[cc - common->start] = localptr;      cc += 1 + IMM2_SIZE;
819      localptr += sizeof(sljit_w);      break;
820      cc += 1 + LINK_SIZE + 2;  
821        case OP_DNREF:
822        case OP_DNREFI:
823        case OP_DNCREF:
824        count = GET2(cc, 1 + IMM2_SIZE);
825        slot = common->name_table + GET2(cc, 1) * common->name_entry_size;
826        while (count-- > 0)
827          {
828          common->optimized_cbracket[GET2(slot, 0)] = 0;
829          slot += common->name_entry_size;
830          }
831        cc += 1 + 2 * IMM2_SIZE;
832      break;      break;
833    
834      case OP_COND:      case OP_RECURSE:
835      /* Might be a hidden SCOND. */      /* Set its value only once. */
836      alternative = cc + GET(cc, 1);      if (common->recursive_head_ptr == 0)
     if (*alternative == OP_KETRMAX || *alternative == OP_KETRMIN)  
837        {        {
838        common->localptrs[cc - common->start] = localptr;        common->recursive_head_ptr = common->ovector_start;
839        localptr += sizeof(sljit_w);        common->ovector_start += sizeof(sljit_sw);
840        }        }
841      cc += 1 + LINK_SIZE;      cc += 1 + LINK_SIZE;
842      break;      break;
843    
844      default:      case OP_CALLOUT:
845      cc = next_opcode(common, cc);      if (common->capture_last_ptr == 0)
846      SLJIT_ASSERT(cc != NULL);        {
847          common->capture_last_ptr = common->ovector_start;
848          common->ovector_start += sizeof(sljit_sw);
849          }
850        cc += 2 + 2 * LINK_SIZE;
851      break;      break;
     }  
   }  
 }  
852    
853  /* Returns with -1 if no need for frame. */      case OP_THEN_ARG:
854  static int get_framesize(compiler_common *common, uschar *cc, BOOL recursive)      common->has_then = TRUE;
855  {      common->control_head_ptr = 1;
856  uschar *ccend = bracketend(cc);      /* Fall through. */
 int length = 0;  
 BOOL possessive = FALSE;  
 BOOL setsom_found = FALSE;  
857    
858  if (!recursive && (*cc == OP_CBRAPOS || *cc == OP_SCBRAPOS))      case OP_PRUNE_ARG:
859    {      common->needs_start_ptr = TRUE;
860    length = 3;      /* Fall through. */
   possessive = TRUE;  
   }  
861    
862  cc = next_opcode(common, cc);      case OP_MARK:
863  SLJIT_ASSERT(cc != NULL);      if (common->mark_ptr == 0)
 while (cc < ccend)  
   switch(*cc)  
     {  
     case OP_SET_SOM:  
     case OP_RECURSE:  
     if (!setsom_found)  
864        {        {
865        length += 2;        common->mark_ptr = common->ovector_start;
866        setsom_found = TRUE;        common->ovector_start += sizeof(sljit_sw);
867        }        }
868      cc += (*cc == OP_SET_SOM) ? 1 : 1 + LINK_SIZE;      cc += 1 + 2 + cc[1];
869      break;      break;
870    
871      case OP_CBRA:      case OP_THEN:
872      case OP_CBRAPOS:      common->has_then = TRUE;
873      case OP_SCBRA:      common->control_head_ptr = 1;
874      case OP_SCBRAPOS:      /* Fall through. */
875      length += 3;  
876      cc += 1 + LINK_SIZE + 2;      case OP_PRUNE:
877        case OP_SKIP:
878        common->needs_start_ptr = TRUE;
879        cc += 1;
880        break;
881    
882        case OP_SKIP_ARG:
883        common->control_head_ptr = 1;
884        common->has_skip_arg = TRUE;
885        cc += 1 + 2 + cc[1];
886      break;      break;
887    
888      default:      default:
889      cc = next_opcode(common, cc);      cc = next_opcode(common, cc);
890      SLJIT_ASSERT(cc != NULL);      if (cc == NULL)
891          return FALSE;
892      break;      break;
893      }      }
894      }
895  /* Possessive quantifiers can use a special case. */  return TRUE;
 if (SLJIT_UNLIKELY(possessive) && length == 3)  
   return -1;  
   
 if (length > 0)  
   return length + 1;  
 return -1;  
896  }  }
897    
898  static void init_frame(compiler_common *common, uschar *cc, int stackpos, int stacktop, BOOL recursive)  static int get_class_iterator_size(pcre_uchar *cc)
899  {  {
900  DEFINE_COMPILER;  switch(*cc)
901  uschar *ccend = bracketend(cc);    {
902  BOOL setsom_found = FALSE;    case OP_CRSTAR:
903  int offset;    case OP_CRPLUS:
904      return 2;
905    
906  /* >= 1 + shortest item size (2) */    case OP_CRMINSTAR:
907  SLJIT_ASSERT(stackpos >= stacktop + 2);    case OP_CRMINPLUS:
908      case OP_CRQUERY:
909      case OP_CRMINQUERY:
910      return 1;
911    
912  stackpos = STACK(stackpos);    case OP_CRRANGE:
913  if (recursive || (*cc != OP_CBRAPOS && *cc != OP_SCBRAPOS))    case OP_CRMINRANGE:
914    cc = next_opcode(common, cc);    if (GET2(cc, 1) == GET2(cc, 1 + IMM2_SIZE))
915  SLJIT_ASSERT(cc != NULL);      return 0;
916  while (cc < ccend)    return 2;
917    switch(*cc)  
918      default:
919      return 0;
920      }
921    }
922    
923    static BOOL detect_repeat(compiler_common *common, pcre_uchar *begin)
924    {
925    pcre_uchar *end = bracketend(begin);
926    pcre_uchar *next;
927    pcre_uchar *next_end;
928    pcre_uchar *max_end;
929    pcre_uchar type;
930    sljit_sw length = end - begin;
931    int min, max, i;
932    
933    /* Detect fixed iterations first. */
934    if (end[-(1 + LINK_SIZE)] != OP_KET)
935      return FALSE;
936    
937    /* Already detected repeat. */
938    if (common->private_data_ptrs[end - common->start - LINK_SIZE] != 0)
939      return TRUE;
940    
941    next = end;
942    min = 1;
943    while (1)
944      {
945      if (*next != *begin)
946        break;
947      next_end = bracketend(next);
948      if (next_end - next != length || memcmp(begin, next, IN_UCHARS(length)) != 0)
949        break;
950      next = next_end;
951      min++;
952      }
953    
954    if (min == 2)
955      return FALSE;
956    
957    max = 0;
958    max_end = next;
959    if (*next == OP_BRAZERO || *next == OP_BRAMINZERO)
960      {
961      type = *next;
962      while (1)
963      {      {
964      case OP_SET_SOM:      if (next[0] != type || next[1] != OP_BRA || next[2 + LINK_SIZE] != *begin)
965      case OP_RECURSE:        break;
966      if (!setsom_found)      next_end = bracketend(next + 2 + LINK_SIZE);
967        if (next_end - next != (length + 2 + LINK_SIZE) || memcmp(begin, next + 2 + LINK_SIZE, IN_UCHARS(length)) != 0)
968          break;
969        next = next_end;
970        max++;
971        }
972    
973      if (next[0] == type && next[1] == *begin && max >= 1)
974        {
975        next_end = bracketend(next + 1);
976        if (next_end - next == (length + 1) && memcmp(begin, next + 1, IN_UCHARS(length)) == 0)
977        {        {
978        OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(0));        for (i = 0; i < max; i++, next_end += 1 + LINK_SIZE)
979        OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, SLJIT_IMM, frame_setstrbegin);          if (*next_end != OP_KET)
980        stackpos += (int)sizeof(sljit_w);            break;
981        OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, TMP1, 0);  
982        stackpos += (int)sizeof(sljit_w);        if (i == max)
983        setsom_found = TRUE;          {
984            common->private_data_ptrs[max_end - common->start - LINK_SIZE] = next_end - max_end;
985            common->private_data_ptrs[max_end - common->start - LINK_SIZE + 1] = (type == OP_BRAZERO) ? OP_UPTO : OP_MINUPTO;
986            /* +2 the original and the last. */
987            common->private_data_ptrs[max_end - common->start - LINK_SIZE + 2] = max + 2;
988            if (min == 1)
989              return TRUE;
990            min--;
991            max_end -= (1 + LINK_SIZE) + GET(max_end, -LINK_SIZE);
992            }
993        }        }
994      cc += (*cc == OP_SET_SOM) ? 1 : 1 + LINK_SIZE;      }
995      break;    }
996    
997      case OP_CBRA:  if (min >= 3)
998      case OP_CBRAPOS:    {
999      case OP_SCBRA:    common->private_data_ptrs[end - common->start - LINK_SIZE] = max_end - end;
1000      case OP_SCBRAPOS:    common->private_data_ptrs[end - common->start - LINK_SIZE + 1] = OP_EXACT;
1001      offset = (GET2(cc, 1 + LINK_SIZE)) << 1;    common->private_data_ptrs[end - common->start - LINK_SIZE + 2] = min;
1002      OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, SLJIT_IMM, OVECTOR(offset));    return TRUE;
1003      stackpos += (int)sizeof(sljit_w);    }
1004      OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(offset));  
1005      OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(offset + 1));  return FALSE;
1006      OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, TMP1, 0);  }
1007      stackpos += (int)sizeof(sljit_w);  
1008      OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, TMP2, 0);  #define CASE_ITERATOR_PRIVATE_DATA_1 \
1009      stackpos += (int)sizeof(sljit_w);      case OP_MINSTAR: \
1010        case OP_MINPLUS: \
1011        case OP_QUERY: \
1012        case OP_MINQUERY: \
1013        case OP_MINSTARI: \
1014        case OP_MINPLUSI: \
1015        case OP_QUERYI: \
1016        case OP_MINQUERYI: \
1017        case OP_NOTMINSTAR: \
1018        case OP_NOTMINPLUS: \
1019        case OP_NOTQUERY: \
1020        case OP_NOTMINQUERY: \
1021        case OP_NOTMINSTARI: \
1022        case OP_NOTMINPLUSI: \
1023        case OP_NOTQUERYI: \
1024        case OP_NOTMINQUERYI:
1025    
1026      cc += 1 + LINK_SIZE + 2;  #define CASE_ITERATOR_PRIVATE_DATA_2A \
1027      break;      case OP_STAR: \
1028        case OP_PLUS: \
1029        case OP_STARI: \
1030        case OP_PLUSI: \
1031        case OP_NOTSTAR: \
1032        case OP_NOTPLUS: \
1033        case OP_NOTSTARI: \
1034        case OP_NOTPLUSI:
1035    
1036      default:  #define CASE_ITERATOR_PRIVATE_DATA_2B \
1037      cc = next_opcode(common, cc);      case OP_UPTO: \
1038      SLJIT_ASSERT(cc != NULL);      case OP_MINUPTO: \
1039      break;      case OP_UPTOI: \
1040      }      case OP_MINUPTOI: \
1041        case OP_NOTUPTO: \
1042        case OP_NOTMINUPTO: \
1043        case OP_NOTUPTOI: \
1044        case OP_NOTMINUPTOI:
1045    
1046  OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, SLJIT_IMM, frame_end);  #define CASE_ITERATOR_TYPE_PRIVATE_DATA_1 \
1047  SLJIT_ASSERT(stackpos == STACK(stacktop));      case OP_TYPEMINSTAR: \
1048  }      case OP_TYPEMINPLUS: \
1049        case OP_TYPEQUERY: \
1050        case OP_TYPEMINQUERY:
1051    
1052    #define CASE_ITERATOR_TYPE_PRIVATE_DATA_2A \
1053        case OP_TYPESTAR: \
1054        case OP_TYPEPLUS:
1055    
1056    #define CASE_ITERATOR_TYPE_PRIVATE_DATA_2B \
1057        case OP_TYPEUPTO: \
1058        case OP_TYPEMINUPTO:
1059    
1060  static SLJIT_INLINE int get_localsize(compiler_common *common, uschar *cc, uschar *ccend)  static void set_private_data_ptrs(compiler_common *common, int *private_data_start, pcre_uchar *ccend)
1061  {  {
1062  int localsize = 2;  pcre_uchar *cc = common->start;
1063  uschar *alternative;  pcre_uchar *alternative;
1064  /* Calculate the sum of the local variables. */  pcre_uchar *end = NULL;
1065    int private_data_ptr = *private_data_start;
1066    int space, size, bracketlen;
1067    
1068  while (cc < ccend)  while (cc < ccend)
1069    {    {
1070      space = 0;
1071      size = 0;
1072      bracketlen = 0;
1073      if (private_data_ptr > SLJIT_MAX_LOCAL_SIZE)
1074        break;
1075    
1076      if (*cc == OP_ONCE || *cc == OP_ONCE_NC || *cc == OP_BRA || *cc == OP_CBRA || *cc == OP_COND)
1077        if (detect_repeat(common, cc))
1078          {
1079          /* These brackets are converted to repeats, so no global
1080          based single character repeat is allowed. */
1081          if (cc >= end)
1082            end = bracketend(cc);
1083          }
1084    
1085    switch(*cc)    switch(*cc)
1086      {      {
1087        case OP_KET:
1088        if (common->private_data_ptrs[cc + 1 - common->start] != 0)
1089          {
1090          common->private_data_ptrs[cc - common->start] = private_data_ptr;
1091          private_data_ptr += sizeof(sljit_sw);
1092          cc += common->private_data_ptrs[cc + 1 - common->start];
1093          }
1094        cc += 1 + LINK_SIZE;
1095        break;
1096    
1097      case OP_ASSERT:      case OP_ASSERT:
1098      case OP_ASSERT_NOT:      case OP_ASSERT_NOT:
1099      case OP_ASSERTBACK:      case OP_ASSERTBACK:
# Line 832  while (cc < ccend) Line 1104  while (cc < ccend)
1104      case OP_SBRA:      case OP_SBRA:
1105      case OP_SBRAPOS:      case OP_SBRAPOS:
1106      case OP_SCOND:      case OP_SCOND:
1107      localsize++;      common->private_data_ptrs[cc - common->start] = private_data_ptr;
1108      cc += 1 + LINK_SIZE;      private_data_ptr += sizeof(sljit_sw);
1109      break;      bracketlen = 1 + LINK_SIZE;
   
     case OP_CBRA:  
     case OP_SCBRA:  
     localsize++;  
     cc += 1 + LINK_SIZE + 2;  
1110      break;      break;
1111    
1112      case OP_CBRAPOS:      case OP_CBRAPOS:
1113      case OP_SCBRAPOS:      case OP_SCBRAPOS:
1114      localsize += 2;      common->private_data_ptrs[cc - common->start] = private_data_ptr;
1115      cc += 1 + LINK_SIZE + 2;      private_data_ptr += sizeof(sljit_sw);
1116        bracketlen = 1 + LINK_SIZE + IMM2_SIZE;
1117      break;      break;
1118    
1119      case OP_COND:      case OP_COND:
1120      /* Might be a hidden SCOND. */      /* Might be a hidden SCOND. */
1121      alternative = cc + GET(cc, 1);      alternative = cc + GET(cc, 1);
1122      if (*alternative == OP_KETRMAX || *alternative == OP_KETRMIN)      if (*alternative == OP_KETRMAX || *alternative == OP_KETRMIN)
1123        localsize++;        {
1124      cc += 1 + LINK_SIZE;        common->private_data_ptrs[cc - common->start] = private_data_ptr;
1125          private_data_ptr += sizeof(sljit_sw);
1126          }
1127        bracketlen = 1 + LINK_SIZE;
1128        break;
1129    
1130        case OP_BRA:
1131        bracketlen = 1 + LINK_SIZE;
1132        break;
1133    
1134        case OP_CBRA:
1135        case OP_SCBRA:
1136        bracketlen = 1 + LINK_SIZE + IMM2_SIZE;
1137        break;
1138    
1139        CASE_ITERATOR_PRIVATE_DATA_1
1140        space = 1;
1141        size = -2;
1142        break;
1143    
1144        CASE_ITERATOR_PRIVATE_DATA_2A
1145        space = 2;
1146        size = -2;
1147        break;
1148    
1149        CASE_ITERATOR_PRIVATE_DATA_2B
1150        space = 2;
1151        size = -(2 + IMM2_SIZE);
1152        break;
1153    
1154        CASE_ITERATOR_TYPE_PRIVATE_DATA_1
1155        space = 1;
1156        size = 1;
1157        break;
1158    
1159        CASE_ITERATOR_TYPE_PRIVATE_DATA_2A
1160        if (cc[1] != OP_ANYNL && cc[1] != OP_EXTUNI)
1161          space = 2;
1162        size = 1;
1163        break;
1164    
1165        case OP_TYPEUPTO:
1166        if (cc[1 + IMM2_SIZE] != OP_ANYNL && cc[1 + IMM2_SIZE] != OP_EXTUNI)
1167          space = 2;
1168        size = 1 + IMM2_SIZE;
1169        break;
1170    
1171        case OP_TYPEMINUPTO:
1172        space = 2;
1173        size = 1 + IMM2_SIZE;
1174      break;      break;
1175    
1176        case OP_CLASS:
1177        case OP_NCLASS:
1178        size += 1 + 32 / sizeof(pcre_uchar);
1179        space = get_class_iterator_size(cc + size);
1180        break;
1181    
1182    #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
1183        case OP_XCLASS:
1184        size = GET(cc, 1);
1185        space = get_class_iterator_size(cc + size);
1186        break;
1187    #endif
1188    
1189      default:      default:
1190      cc = next_opcode(common, cc);      cc = next_opcode(common, cc);
1191      SLJIT_ASSERT(cc != NULL);      SLJIT_ASSERT(cc != NULL);
1192      break;      break;
1193      }      }
1194    
1195      /* Character iterators, which are not inside a repeated bracket,
1196         gets a private slot instead of allocating it on the stack. */
1197      if (space > 0 && cc >= end)
1198        {
1199        common->private_data_ptrs[cc - common->start] = private_data_ptr;
1200        private_data_ptr += sizeof(sljit_sw) * space;
1201        }
1202    
1203      if (size != 0)
1204        {
1205        if (size < 0)
1206          {
1207          cc += -size;
1208    #ifdef SUPPORT_UTF
1209          if (common->utf && HAS_EXTRALEN(cc[-1])) cc += GET_EXTRALEN(cc[-1]);
1210    #endif
1211          }
1212        else
1213          cc += size;
1214        }
1215    
1216      if (bracketlen > 0)
1217        {
1218        if (cc >= end)
1219          {
1220          end = bracketend(cc);
1221          if (end[-1 - LINK_SIZE] == OP_KET)
1222            end = NULL;
1223          }
1224        cc += bracketlen;
1225        }
1226    }    }
1227  SLJIT_ASSERT(cc == ccend);  *private_data_start = private_data_ptr;
 return localsize;  
1228  }  }
1229    
1230  static void copy_locals(compiler_common *common, uschar *cc, uschar *ccend,  /* Returns with a frame_types (always < 0) if no need for frame. */
1231    BOOL save, int stackptr, int stacktop)  static int get_framesize(compiler_common *common, pcre_uchar *cc, pcre_uchar *ccend, BOOL recursive, BOOL *needs_control_head)
1232  {  {
1233  DEFINE_COMPILER;  int length = 0;
1234  int srcw[2];  int possessive = 0;
1235  int count;  BOOL stack_restore = FALSE;
1236  BOOL tmp1next = TRUE;  BOOL setsom_found = recursive;
1237  BOOL tmp1empty = TRUE;  BOOL setmark_found = recursive;
1238  BOOL tmp2empty = TRUE;  /* The last capture is a local variable even for recursions. */
1239  uschar *alternative;  BOOL capture_last_found = FALSE;
1240  enum {  
1241    start,  #if defined DEBUG_FORCE_CONTROL_HEAD && DEBUG_FORCE_CONTROL_HEAD
1242    loop,  SLJIT_ASSERT(common->control_head_ptr != 0);
1243    end  *needs_control_head = TRUE;
1244  } status;  #else
1245    *needs_control_head = FALSE;
1246  status = save ? start : loop;  #endif
 stackptr = STACK(stackptr - 2);  
 stacktop = STACK(stacktop - 1);  
1247    
1248  if (!save)  if (ccend == NULL)
1249    {    {
1250    stackptr += sizeof(sljit_w);    ccend = bracketend(cc) - (1 + LINK_SIZE);
1251    if (stackptr < stacktop)    if (!recursive && (*cc == OP_CBRAPOS || *cc == OP_SCBRAPOS))
     {  
     OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(STACK_TOP), stackptr);  
     stackptr += sizeof(sljit_w);  
     tmp1empty = FALSE;  
     }  
   if (stackptr < stacktop)  
1252      {      {
1253      OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(STACK_TOP), stackptr);      possessive = length = (common->capture_last_ptr != 0) ? 5 : 3;
1254      stackptr += sizeof(sljit_w);      /* This is correct regardless of common->capture_last_ptr. */
1255      tmp2empty = FALSE;      capture_last_found = TRUE;
1256      }      }
1257    /* The tmp1next must be TRUE in either way. */    cc = next_opcode(common, cc);
1258    }    }
1259    
1260  while (status != end)  SLJIT_ASSERT(cc != NULL);
1261    {  while (cc < ccend)
1262    count = 0;    switch(*cc)
   switch(status)  
1263      {      {
1264      case start:      case OP_SET_SOM:
1265      SLJIT_ASSERT(save);      SLJIT_ASSERT(common->has_set_som);
1266      count = 1;      stack_restore = TRUE;
1267      srcw[0] = RECURSIVE_HEAD;      if (!setsom_found)
1268      status = loop;        {
1269          length += 2;
1270          setsom_found = TRUE;
1271          }
1272        cc += 1;
1273      break;      break;
1274    
1275      case loop:      case OP_MARK:
1276      if (cc >= ccend)      case OP_PRUNE_ARG:
1277        case OP_THEN_ARG:
1278        SLJIT_ASSERT(common->mark_ptr != 0);
1279        stack_restore = TRUE;
1280        if (!setmark_found)
1281        {        {
1282        status = end;        length += 2;
1283        break;        setmark_found = TRUE;
1284        }        }
1285        if (common->control_head_ptr != 0)
1286          *needs_control_head = TRUE;
1287        cc += 1 + 2 + cc[1];
1288        break;
1289    
1290      switch(*cc)      case OP_RECURSE:
1291        stack_restore = TRUE;
1292        if (common->has_set_som && !setsom_found)
1293        {        {
1294        case OP_ASSERT:        length += 2;
1295        case OP_ASSERT_NOT:        setsom_found = TRUE;
1296        case OP_ASSERTBACK:        }
1297        case OP_ASSERTBACK_NOT:      if (common->mark_ptr != 0 && !setmark_found)
1298        case OP_ONCE:        {
1299        case OP_ONCE_NC:        length += 2;
1300        case OP_BRAPOS:        setmark_found = TRUE;
1301        case OP_SBRA:        }
1302        case OP_SBRAPOS:      if (common->capture_last_ptr != 0 && !capture_last_found)
1303        case OP_SCOND:        {
1304        count = 1;        length += 2;
1305        srcw[0] = PRIV(cc);        capture_last_found = TRUE;
1306        SLJIT_ASSERT(srcw[0] != 0);        }
1307        cc += 1 + LINK_SIZE;      cc += 1 + LINK_SIZE;
1308        break;      break;
   
       case OP_CBRA:  
       case OP_SCBRA:  
       count = 1;  
       srcw[0] = OVECTOR_PRIV(GET2(cc, 1 + LINK_SIZE));  
       cc += 1 + LINK_SIZE + 2;  
       break;  
   
       case OP_CBRAPOS:  
       case OP_SCBRAPOS:  
       count = 2;  
       srcw[1] = OVECTOR_PRIV(GET2(cc, 1 + LINK_SIZE));  
       srcw[0] = PRIV(cc);  
       SLJIT_ASSERT(srcw[0] != 0);  
       cc += 1 + LINK_SIZE + 2;  
       break;  
   
       case OP_COND:  
       /* Might be a hidden SCOND. */  
       alternative = cc + GET(cc, 1);  
       if (*alternative == OP_KETRMAX || *alternative == OP_KETRMIN)  
         {  
         count = 1;  
         srcw[0] = PRIV(cc);  
         SLJIT_ASSERT(srcw[0] != 0);  
         }  
       cc += 1 + LINK_SIZE;  
       break;  
1309    
1310        default:      case OP_CBRA:
1311        cc = next_opcode(common, cc);      case OP_CBRAPOS:
1312        SLJIT_ASSERT(cc != NULL);      case OP_SCBRA:
1313        break;      case OP_SCBRAPOS:
1314        stack_restore = TRUE;
1315        if (common->capture_last_ptr != 0 && !capture_last_found)
1316          {
1317          length += 2;
1318          capture_last_found = TRUE;
1319        }        }
1320        length += 3;
1321        cc += 1 + LINK_SIZE + IMM2_SIZE;
1322      break;      break;
1323    
1324      case end:      default:
1325      SLJIT_ASSERT_STOP();      stack_restore = TRUE;
1326        /* Fall through. */
1327    
1328        case OP_NOT_WORD_BOUNDARY:
1329        case OP_WORD_BOUNDARY:
1330        case OP_NOT_DIGIT:
1331        case OP_DIGIT:
1332        case OP_NOT_WHITESPACE:
1333        case OP_WHITESPACE:
1334        case OP_NOT_WORDCHAR:
1335        case OP_WORDCHAR:
1336        case OP_ANY:
1337        case OP_ALLANY:
1338        case OP_ANYBYTE:
1339        case OP_NOTPROP:
1340        case OP_PROP:
1341        case OP_ANYNL:
1342        case OP_NOT_HSPACE:
1343        case OP_HSPACE:
1344        case OP_NOT_VSPACE:
1345        case OP_VSPACE:
1346        case OP_EXTUNI:
1347        case OP_EODN:
1348        case OP_EOD:
1349        case OP_CIRC:
1350        case OP_CIRCM:
1351        case OP_DOLL:
1352        case OP_DOLLM:
1353        case OP_CHAR:
1354        case OP_CHARI:
1355        case OP_NOT:
1356        case OP_NOTI:
1357    
1358        case OP_EXACT:
1359        case OP_POSSTAR:
1360        case OP_POSPLUS:
1361        case OP_POSQUERY:
1362        case OP_POSUPTO:
1363    
1364        case OP_EXACTI:
1365        case OP_POSSTARI:
1366        case OP_POSPLUSI:
1367        case OP_POSQUERYI:
1368        case OP_POSUPTOI:
1369    
1370        case OP_NOTEXACT:
1371        case OP_NOTPOSSTAR:
1372        case OP_NOTPOSPLUS:
1373        case OP_NOTPOSQUERY:
1374        case OP_NOTPOSUPTO:
1375    
1376        case OP_NOTEXACTI:
1377        case OP_NOTPOSSTARI:
1378        case OP_NOTPOSPLUSI:
1379        case OP_NOTPOSQUERYI:
1380        case OP_NOTPOSUPTOI:
1381    
1382        case OP_TYPEEXACT:
1383        case OP_TYPEPOSSTAR:
1384        case OP_TYPEPOSPLUS:
1385        case OP_TYPEPOSQUERY:
1386        case OP_TYPEPOSUPTO:
1387    
1388        case OP_CLASS:
1389        case OP_NCLASS:
1390        case OP_XCLASS:
1391    
1392        cc = next_opcode(common, cc);
1393        SLJIT_ASSERT(cc != NULL);
1394      break;      break;
1395      }      }
1396    
1397    while (count > 0)  /* Possessive quantifiers can use a special case. */
1398    if (SLJIT_UNLIKELY(possessive == length))
1399      return stack_restore ? no_frame : no_stack;
1400    
1401    if (length > 0)
1402      return length + 1;
1403    return stack_restore ? no_frame : no_stack;
1404    }
1405    
1406    static void init_frame(compiler_common *common, pcre_uchar *cc, pcre_uchar *ccend, int stackpos, int stacktop, BOOL recursive)
1407    {
1408    DEFINE_COMPILER;
1409    BOOL setsom_found = recursive;
1410    BOOL setmark_found = recursive;
1411    /* The last capture is a local variable even for recursions. */
1412    BOOL capture_last_found = FALSE;
1413    int offset;
1414    
1415    /* >= 1 + shortest item size (2) */
1416    SLJIT_UNUSED_ARG(stacktop);
1417    SLJIT_ASSERT(stackpos >= stacktop + 2);
1418    
1419    stackpos = STACK(stackpos);
1420    if (ccend == NULL)
1421      {
1422      ccend = bracketend(cc) - (1 + LINK_SIZE);
1423      if (recursive || (*cc != OP_CBRAPOS && *cc != OP_SCBRAPOS))
1424        cc = next_opcode(common, cc);
1425      }
1426    
1427    SLJIT_ASSERT(cc != NULL);
1428    while (cc < ccend)
1429      switch(*cc)
1430      {      {
1431      count--;      case OP_SET_SOM:
1432      if (save)      SLJIT_ASSERT(common->has_set_som);
1433        if (!setsom_found)
1434        {        {
1435        if (tmp1next)        OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), OVECTOR(0));
1436          {        OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, SLJIT_IMM, -OVECTOR(0));
1437          if (!tmp1empty)        stackpos += (int)sizeof(sljit_sw);
1438            {        OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, TMP1, 0);
1439            OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackptr, TMP1, 0);        stackpos += (int)sizeof(sljit_sw);
1440            stackptr += sizeof(sljit_w);        setsom_found = TRUE;
           }  
         OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), srcw[count]);  
         tmp1empty = FALSE;  
         tmp1next = FALSE;  
         }  
       else  
         {  
         if (!tmp2empty)  
           {  
           OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackptr, TMP2, 0);  
           stackptr += sizeof(sljit_w);  
           }  
         OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), srcw[count]);  
         tmp2empty = FALSE;  
         tmp1next = TRUE;  
         }  
1441        }        }
1442      else      cc += 1;
1443        break;
1444    
1445        case OP_MARK:
1446        case OP_PRUNE_ARG:
1447        case OP_THEN_ARG:
1448        SLJIT_ASSERT(common->mark_ptr != 0);
1449        if (!setmark_found)
1450          {
1451          OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), common->mark_ptr);
1452          OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, SLJIT_IMM, -common->mark_ptr);
1453          stackpos += (int)sizeof(sljit_sw);
1454          OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, TMP1, 0);
1455          stackpos += (int)sizeof(sljit_sw);
1456          setmark_found = TRUE;
1457          }
1458        cc += 1 + 2 + cc[1];
1459        break;
1460    
1461        case OP_RECURSE:
1462        if (common->has_set_som && !setsom_found)
1463        {        {
1464        if (tmp1next)        OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), OVECTOR(0));
1465          {        OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, SLJIT_IMM, -OVECTOR(0));
1466          SLJIT_ASSERT(!tmp1empty);        stackpos += (int)sizeof(sljit_sw);
1467          OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), srcw[count], TMP1, 0);        OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, TMP1, 0);
1468          tmp1empty = stackptr >= stacktop;        stackpos += (int)sizeof(sljit_sw);
1469          if (!tmp1empty)        setsom_found = TRUE;
1470            {        }
1471            OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(STACK_TOP), stackptr);      if (common->mark_ptr != 0 && !setmark_found)
1472            stackptr += sizeof(sljit_w);        {
1473            }        OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), common->mark_ptr);
1474          tmp1next = FALSE;        OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, SLJIT_IMM, -common->mark_ptr);
1475          }        stackpos += (int)sizeof(sljit_sw);
1476        else        OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, TMP1, 0);
1477          {        stackpos += (int)sizeof(sljit_sw);
1478          SLJIT_ASSERT(!tmp2empty);        setmark_found = TRUE;
1479          OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), srcw[count], TMP2, 0);        }
1480          tmp2empty = stackptr >= stacktop;      if (common->capture_last_ptr != 0 && !capture_last_found)
1481          if (!tmp2empty)        {
1482            {        OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), common->capture_last_ptr);
1483            OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(STACK_TOP), stackptr);        OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, SLJIT_IMM, -common->capture_last_ptr);
1484            stackptr += sizeof(sljit_w);        stackpos += (int)sizeof(sljit_sw);
1485            }        OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, TMP1, 0);
1486          tmp1next = TRUE;        stackpos += (int)sizeof(sljit_sw);
1487          }        capture_last_found = TRUE;
1488          }
1489        cc += 1 + LINK_SIZE;
1490        break;
1491    
1492        case OP_CBRA:
1493        case OP_CBRAPOS:
1494        case OP_SCBRA:
1495        case OP_SCBRAPOS:
1496        if (common->capture_last_ptr != 0 && !capture_last_found)
1497          {
1498          OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), common->capture_last_ptr);
1499          OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, SLJIT_IMM, -common->capture_last_ptr);
1500          stackpos += (int)sizeof(sljit_sw);
1501          OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, TMP1, 0);
1502          stackpos += (int)sizeof(sljit_sw);
1503          capture_last_found = TRUE;
1504          }
1505        offset = (GET2(cc, 1 + LINK_SIZE)) << 1;
1506        OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, SLJIT_IMM, OVECTOR(offset));
1507        stackpos += (int)sizeof(sljit_sw);
1508        OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), OVECTOR(offset));
1509        OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_SP), OVECTOR(offset + 1));
1510        OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, TMP1, 0);
1511        stackpos += (int)sizeof(sljit_sw);
1512        OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, TMP2, 0);
1513        stackpos += (int)sizeof(sljit_sw);
1514    
1515        cc += 1 + LINK_SIZE + IMM2_SIZE;
1516        break;
1517    
1518        default:
1519        cc = next_opcode(common, cc);
1520        SLJIT_ASSERT(cc != NULL);
1521        break;
1522        }
1523    
1524    OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, SLJIT_IMM, 0);
1525    SLJIT_ASSERT(stackpos == STACK(stacktop));
1526    }
1527    
1528    static SLJIT_INLINE int get_private_data_copy_length(compiler_common *common, pcre_uchar *cc, pcre_uchar *ccend, BOOL needs_control_head)
1529    {
1530    int private_data_length = needs_control_head ? 3 : 2;
1531    int size;
1532    pcre_uchar *alternative;
1533    /* Calculate the sum of the private machine words. */
1534    while (cc < ccend)
1535      {
1536      size = 0;
1537      switch(*cc)
1538        {
1539        case OP_KET:
1540        if (PRIVATE_DATA(cc) != 0)
1541          {
1542          private_data_length++;
1543          SLJIT_ASSERT(PRIVATE_DATA(cc + 1) != 0);
1544          cc += PRIVATE_DATA(cc + 1);
1545        }        }
1546        cc += 1 + LINK_SIZE;
1547        break;
1548    
1549        case OP_ASSERT:
1550        case OP_ASSERT_NOT:
1551        case OP_ASSERTBACK:
1552        case OP_ASSERTBACK_NOT:
1553        case OP_ONCE:
1554        case OP_ONCE_NC:
1555        case OP_BRAPOS:
1556        case OP_SBRA:
1557        case OP_SBRAPOS:
1558        case OP_SCOND:
1559        private_data_length++;
1560        SLJIT_ASSERT(PRIVATE_DATA(cc) != 0);
1561        cc += 1 + LINK_SIZE;
1562        break;
1563    
1564        case OP_CBRA:
1565        case OP_SCBRA:
1566        if (common->optimized_cbracket[GET2(cc, 1 + LINK_SIZE)] == 0)
1567          private_data_length++;
1568        cc += 1 + LINK_SIZE + IMM2_SIZE;
1569        break;
1570    
1571        case OP_CBRAPOS:
1572        case OP_SCBRAPOS:
1573        private_data_length += 2;
1574        cc += 1 + LINK_SIZE + IMM2_SIZE;
1575        break;
1576    
1577        case OP_COND:
1578        /* Might be a hidden SCOND. */
1579        alternative = cc + GET(cc, 1);
1580        if (*alternative == OP_KETRMAX || *alternative == OP_KETRMIN)
1581          private_data_length++;
1582        cc += 1 + LINK_SIZE;
1583        break;
1584    
1585        CASE_ITERATOR_PRIVATE_DATA_1
1586        if (PRIVATE_DATA(cc))
1587          private_data_length++;
1588        cc += 2;
1589    #ifdef SUPPORT_UTF
1590        if (common->utf && HAS_EXTRALEN(cc[-1])) cc += GET_EXTRALEN(cc[-1]);
1591    #endif
1592        break;
1593    
1594        CASE_ITERATOR_PRIVATE_DATA_2A
1595        if (PRIVATE_DATA(cc))
1596          private_data_length += 2;
1597        cc += 2;
1598    #ifdef SUPPORT_UTF
1599        if (common->utf && HAS_EXTRALEN(cc[-1])) cc += GET_EXTRALEN(cc[-1]);
1600    #endif
1601        break;
1602    
1603        CASE_ITERATOR_PRIVATE_DATA_2B
1604        if (PRIVATE_DATA(cc))
1605          private_data_length += 2;
1606        cc += 2 + IMM2_SIZE;
1607    #ifdef SUPPORT_UTF
1608        if (common->utf && HAS_EXTRALEN(cc[-1])) cc += GET_EXTRALEN(cc[-1]);
1609    #endif
1610        break;
1611    
1612        CASE_ITERATOR_TYPE_PRIVATE_DATA_1
1613        if (PRIVATE_DATA(cc))
1614          private_data_length++;
1615        cc += 1;
1616        break;
1617    
1618        CASE_ITERATOR_TYPE_PRIVATE_DATA_2A
1619        if (PRIVATE_DATA(cc))
1620          private_data_length += 2;
1621        cc += 1;
1622        break;
1623    
1624        CASE_ITERATOR_TYPE_PRIVATE_DATA_2B
1625        if (PRIVATE_DATA(cc))
1626          private_data_length += 2;
1627        cc += 1 + IMM2_SIZE;
1628        break;
1629    
1630        case OP_CLASS:
1631        case OP_NCLASS:
1632    #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
1633        case OP_XCLASS:
1634        size = (*cc == OP_XCLASS) ? GET(cc, 1) : 1 + 32 / (int)sizeof(pcre_uchar);
1635    #else
1636        size = 1 + 32 / (int)sizeof(pcre_uchar);
1637    #endif
1638        if (PRIVATE_DATA(cc))
1639          private_data_length += get_class_iterator_size(cc + size);
1640        cc += size;
1641        break;
1642    
1643        default:
1644        cc = next_opcode(common, cc);
1645        SLJIT_ASSERT(cc != NULL);
1646        break;
1647      }      }
1648    }    }
1649    SLJIT_ASSERT(cc == ccend);
1650    return private_data_length;
1651    }
1652    
1653    static void copy_private_data(compiler_common *common, pcre_uchar *cc, pcre_uchar *ccend,
1654      BOOL save, int stackptr, int stacktop, BOOL needs_control_head)
1655    {
1656    DEFINE_COMPILER;
1657    int srcw[2];
1658    int count, size;
1659    BOOL tmp1next = TRUE;
1660    BOOL tmp1empty = TRUE;
1661    BOOL tmp2empty = TRUE;
1662    pcre_uchar *alternative;
1663    enum {
1664      start,
1665      loop,
1666      end
1667    } status;
1668    
1669    status = save ? start : loop;
1670    stackptr = STACK(stackptr - 2);
1671    stacktop = STACK(stacktop - 1);
1672    
1673    if (!save)
1674      {
1675      stackptr += (needs_control_head ? 2 : 1) * sizeof(sljit_sw);
1676      if (stackptr < stacktop)
1677        {
1678        OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(STACK_TOP), stackptr);
1679        stackptr += sizeof(sljit_sw);
1680        tmp1empty = FALSE;
1681        }
1682      if (stackptr < stacktop)
1683        {
1684        OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(STACK_TOP), stackptr);
1685        stackptr += sizeof(sljit_sw);
1686        tmp2empty = FALSE;
1687        }
1688      /* The tmp1next must be TRUE in either way. */
1689      }
1690    
1691    do
1692      {
1693      count = 0;
1694      switch(status)
1695        {
1696        case start:
1697        SLJIT_ASSERT(save && common->recursive_head_ptr != 0);
1698        count = 1;
1699        srcw[0] = common->recursive_head_ptr;
1700        if (needs_control_head)
1701          {
1702          SLJIT_ASSERT(common->control_head_ptr != 0);
1703          count = 2;
1704          srcw[1] = common->control_head_ptr;
1705          }
1706        status = loop;
1707        break;
1708    
1709        case loop:
1710        if (cc >= ccend)
1711          {
1712          status = end;
1713          break;
1714          }
1715    
1716        switch(*cc)
1717          {
1718          case OP_KET:
1719          if (PRIVATE_DATA(cc) != 0)
1720            {
1721            count = 1;
1722            srcw[0] = PRIVATE_DATA(cc);
1723            SLJIT_ASSERT(PRIVATE_DATA(cc + 1) != 0);
1724            cc += PRIVATE_DATA(cc + 1);
1725            }
1726          cc += 1 + LINK_SIZE;
1727          break;
1728    
1729          case OP_ASSERT:
1730          case OP_ASSERT_NOT:
1731          case OP_ASSERTBACK:
1732          case OP_ASSERTBACK_NOT:
1733          case OP_ONCE:
1734          case OP_ONCE_NC:
1735          case OP_BRAPOS:
1736          case OP_SBRA:
1737          case OP_SBRAPOS:
1738          case OP_SCOND:
1739          count = 1;
1740          srcw[0] = PRIVATE_DATA(cc);
1741          SLJIT_ASSERT(srcw[0] != 0);
1742          cc += 1 + LINK_SIZE;
1743          break;
1744    
1745          case OP_CBRA:
1746          case OP_SCBRA:
1747          if (common->optimized_cbracket[GET2(cc, 1 + LINK_SIZE)] == 0)
1748            {
1749            count = 1;
1750            srcw[0] = OVECTOR_PRIV(GET2(cc, 1 + LINK_SIZE));
1751            }
1752          cc += 1 + LINK_SIZE + IMM2_SIZE;
1753          break;
1754    
1755          case OP_CBRAPOS:
1756          case OP_SCBRAPOS:
1757          count = 2;
1758          srcw[0] = PRIVATE_DATA(cc);
1759          srcw[1] = OVECTOR_PRIV(GET2(cc, 1 + LINK_SIZE));
1760          SLJIT_ASSERT(srcw[0] != 0 && srcw[1] != 0);
1761          cc += 1 + LINK_SIZE + IMM2_SIZE;
1762          break;
1763    
1764          case OP_COND:
1765          /* Might be a hidden SCOND. */
1766          alternative = cc + GET(cc, 1);
1767          if (*alternative == OP_KETRMAX || *alternative == OP_KETRMIN)
1768            {
1769            count = 1;
1770            srcw[0] = PRIVATE_DATA(cc);
1771            SLJIT_ASSERT(srcw[0] != 0);
1772            }
1773          cc += 1 + LINK_SIZE;
1774          break;
1775    
1776          CASE_ITERATOR_PRIVATE_DATA_1
1777          if (PRIVATE_DATA(cc))
1778            {
1779            count = 1;
1780            srcw[0] = PRIVATE_DATA(cc);
1781            }
1782          cc += 2;
1783    #ifdef SUPPORT_UTF
1784          if (common->utf && HAS_EXTRALEN(cc[-1])) cc += GET_EXTRALEN(cc[-1]);
1785    #endif
1786          break;
1787    
1788          CASE_ITERATOR_PRIVATE_DATA_2A
1789          if (PRIVATE_DATA(cc))
1790            {
1791            count = 2;
1792            srcw[0] = PRIVATE_DATA(cc);
1793            srcw[1] = PRIVATE_DATA(cc) + sizeof(sljit_sw);
1794            }
1795          cc += 2;
1796    #ifdef SUPPORT_UTF
1797          if (common->utf && HAS_EXTRALEN(cc[-1])) cc += GET_EXTRALEN(cc[-1]);
1798    #endif
1799          break;
1800    
1801          CASE_ITERATOR_PRIVATE_DATA_2B
1802          if (PRIVATE_DATA(cc))
1803            {
1804            count = 2;
1805            srcw[0] = PRIVATE_DATA(cc);
1806            srcw[1] = PRIVATE_DATA(cc) + sizeof(sljit_sw);
1807            }
1808          cc += 2 + IMM2_SIZE;
1809    #ifdef SUPPORT_UTF
1810          if (common->utf && HAS_EXTRALEN(cc[-1])) cc += GET_EXTRALEN(cc[-1]);
1811    #endif
1812          break;
1813    
1814          CASE_ITERATOR_TYPE_PRIVATE_DATA_1
1815          if (PRIVATE_DATA(cc))
1816            {
1817            count = 1;
1818            srcw[0] = PRIVATE_DATA(cc);
1819            }
1820          cc += 1;
1821          break;
1822    
1823          CASE_ITERATOR_TYPE_PRIVATE_DATA_2A
1824          if (PRIVATE_DATA(cc))
1825            {
1826            count = 2;
1827            srcw[0] = PRIVATE_DATA(cc);
1828            srcw[1] = srcw[0] + sizeof(sljit_sw);
1829            }
1830          cc += 1;
1831          break;
1832    
1833          CASE_ITERATOR_TYPE_PRIVATE_DATA_2B
1834          if (PRIVATE_DATA(cc))
1835            {
1836            count = 2;
1837            srcw[0] = PRIVATE_DATA(cc);
1838            srcw[1] = srcw[0] + sizeof(sljit_sw);
1839            }
1840          cc += 1 + IMM2_SIZE;
1841          break;
1842    
1843          case OP_CLASS:
1844          case OP_NCLASS:
1845    #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
1846          case OP_XCLASS:
1847          size = (*cc == OP_XCLASS) ? GET(cc, 1) : 1 + 32 / (int)sizeof(pcre_uchar);
1848    #else
1849          size = 1 + 32 / (int)sizeof(pcre_uchar);
1850    #endif
1851          if (PRIVATE_DATA(cc))
1852            switch(get_class_iterator_size(cc + size))
1853              {
1854              case 1:
1855              count = 1;
1856              srcw[0] = PRIVATE_DATA(cc);
1857              break;
1858    
1859              case 2:
1860              count = 2;
1861              srcw[0] = PRIVATE_DATA(cc);
1862              srcw[1] = srcw[0] + sizeof(sljit_sw);
1863              break;
1864    
1865              default:
1866              SLJIT_ASSERT_STOP();
1867              break;
1868              }
1869          cc += size;
1870          break;
1871    
1872          default:
1873          cc = next_opcode(common, cc);
1874          SLJIT_ASSERT(cc != NULL);
1875          break;
1876          }
1877        break;
1878    
1879        case end:
1880        SLJIT_ASSERT_STOP();
1881        break;
1882        }
1883    
1884      while (count > 0)
1885        {
1886        count--;
1887        if (save)
1888          {
1889          if (tmp1next)
1890            {
1891            if (!tmp1empty)
1892              {
1893              OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackptr, TMP1, 0);
1894              stackptr += sizeof(sljit_sw);
1895              }
1896            OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), srcw[count]);
1897            tmp1empty = FALSE;
1898            tmp1next = FALSE;
1899            }
1900          else
1901            {
1902            if (!tmp2empty)
1903              {
1904              OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackptr, TMP2, 0);
1905              stackptr += sizeof(sljit_sw);
1906              }
1907            OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_SP), srcw[count]);
1908            tmp2empty = FALSE;
1909            tmp1next = TRUE;
1910            }
1911          }
1912        else
1913          {
1914          if (tmp1next)
1915            {
1916            SLJIT_ASSERT(!tmp1empty);
1917            OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), srcw[count], TMP1, 0);
1918            tmp1empty = stackptr >= stacktop;
1919            if (!tmp1empty)
1920              {
1921              OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(STACK_TOP), stackptr);
1922              stackptr += sizeof(sljit_sw);
1923              }
1924            tmp1next = FALSE;
1925            }
1926          else
1927            {
1928            SLJIT_ASSERT(!tmp2empty);
1929            OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), srcw[count], TMP2, 0);
1930            tmp2empty = stackptr >= stacktop;
1931            if (!tmp2empty)
1932              {
1933              OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(STACK_TOP), stackptr);
1934              stackptr += sizeof(sljit_sw);
1935              }
1936            tmp1next = TRUE;
1937            }
1938          }
1939        }
1940      }
1941    while (status != end);
1942    
1943    if (save)
1944      {
1945      if (tmp1next)
1946        {
1947        if (!tmp1empty)
1948          {
1949          OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackptr, TMP1, 0);
1950          stackptr += sizeof(sljit_sw);
1951          }
1952        if (!tmp2empty)
1953          {
1954          OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackptr, TMP2, 0);
1955          stackptr += sizeof(sljit_sw);
1956          }
1957        }
1958      else
1959        {
1960        if (!tmp2empty)
1961          {
1962          OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackptr, TMP2, 0);
1963          stackptr += sizeof(sljit_sw);
1964          }
1965        if (!tmp1empty)
1966          {
1967          OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackptr, TMP1, 0);
1968          stackptr += sizeof(sljit_sw);
1969          }
1970        }
1971      }
1972    SLJIT_ASSERT(cc == ccend && stackptr == stacktop && (save || (tmp1empty && tmp2empty)));
1973    }
1974    
1975    static SLJIT_INLINE pcre_uchar *set_then_offsets(compiler_common *common, pcre_uchar *cc, pcre_uint8 *current_offset)
1976    {
1977    pcre_uchar *end = bracketend(cc);
1978    BOOL has_alternatives = cc[GET(cc, 1)] == OP_ALT;
1979    
1980    /* Assert captures then. */
1981    if (*cc >= OP_ASSERT && *cc <= OP_ASSERTBACK_NOT)
1982      current_offset = NULL;
1983    /* Conditional block does not. */
1984    if (*cc == OP_COND || *cc == OP_SCOND)
1985      has_alternatives = FALSE;
1986    
1987    cc = next_opcode(common, cc);
1988    if (has_alternatives)
1989      current_offset = common->then_offsets + (cc - common->start);
1990    
1991    while (cc < end)
1992      {
1993      if ((*cc >= OP_ASSERT && *cc <= OP_ASSERTBACK_NOT) || (*cc >= OP_ONCE && *cc <= OP_SCOND))
1994        cc = set_then_offsets(common, cc, current_offset);
1995      else
1996        {
1997        if (*cc == OP_ALT && has_alternatives)
1998          current_offset = common->then_offsets + (cc + 1 + LINK_SIZE - common->start);
1999        if (*cc >= OP_THEN && *cc <= OP_THEN_ARG && current_offset != NULL)
2000          *current_offset = 1;
2001        cc = next_opcode(common, cc);
2002        }
2003      }
2004    
2005    return end;
2006    }
2007    
2008    #undef CASE_ITERATOR_PRIVATE_DATA_1
2009    #undef CASE_ITERATOR_PRIVATE_DATA_2A
2010    #undef CASE_ITERATOR_PRIVATE_DATA_2B
2011    #undef CASE_ITERATOR_TYPE_PRIVATE_DATA_1
2012    #undef CASE_ITERATOR_TYPE_PRIVATE_DATA_2A
2013    #undef CASE_ITERATOR_TYPE_PRIVATE_DATA_2B
2014    
2015    static SLJIT_INLINE BOOL is_powerof2(unsigned int value)
2016    {
2017    return (value & (value - 1)) == 0;
2018    }
2019    
2020    static SLJIT_INLINE void set_jumps(jump_list *list, struct sljit_label *label)
2021    {
2022    while (list)
2023      {
2024      /* sljit_set_label is clever enough to do nothing
2025      if either the jump or the label is NULL. */
2026      SET_LABEL(list->jump, label);
2027      list = list->next;
2028      }
2029    }
2030    
2031    static SLJIT_INLINE void add_jump(struct sljit_compiler *compiler, jump_list **list, struct sljit_jump *jump)
2032    {
2033    jump_list *list_item = sljit_alloc_memory(compiler, sizeof(jump_list));
2034    if (list_item)
2035      {
2036      list_item->next = *list;
2037      list_item->jump = jump;
2038      *list = list_item;
2039      }
2040    }
2041    
2042    static void add_stub(compiler_common *common, struct sljit_jump *start)
2043    {
2044    DEFINE_COMPILER;
2045    stub_list *list_item = sljit_alloc_memory(compiler, sizeof(stub_list));
2046    
2047    if (list_item)
2048      {
2049      list_item->start = start;
2050      list_item->quit = LABEL();
2051      list_item->next = common->stubs;
2052      common->stubs = list_item;
2053      }
2054    }
2055    
2056    static void flush_stubs(compiler_common *common)
2057    {
2058    DEFINE_COMPILER;
2059    stub_list *list_item = common->stubs;
2060    
2061    while (list_item)
2062      {
2063      JUMPHERE(list_item->start);
2064      add_jump(compiler, &common->stackalloc, JUMP(SLJIT_FAST_CALL));
2065      JUMPTO(SLJIT_JUMP, list_item->quit);
2066      list_item = list_item->next;
2067      }
2068    common->stubs = NULL;
2069    }
2070    
2071    static void add_label_addr(compiler_common *common, sljit_uw *update_addr)
2072    {
2073    DEFINE_COMPILER;
2074    label_addr_list *label_addr;
2075    
2076    label_addr = sljit_alloc_memory(compiler, sizeof(label_addr_list));
2077    if (label_addr == NULL)
2078      return;
2079    label_addr->label = LABEL();
2080    label_addr->update_addr = update_addr;
2081    label_addr->next = common->label_addrs;
2082    common->label_addrs = label_addr;
2083    }
2084    
2085    static SLJIT_INLINE void count_match(compiler_common *common)
2086    {
2087    DEFINE_COMPILER;
2088    
2089    OP2(SLJIT_SUB | SLJIT_SET_E, COUNT_MATCH, 0, COUNT_MATCH, 0, SLJIT_IMM, 1);
2090    add_jump(compiler, &common->calllimit, JUMP(SLJIT_ZERO));
2091    }
2092    
2093    static SLJIT_INLINE void allocate_stack(compiler_common *common, int size)
2094    {
2095    /* May destroy all locals and registers except TMP2. */
2096    DEFINE_COMPILER;
2097    
2098    OP2(SLJIT_ADD, STACK_TOP, 0, STACK_TOP, 0, SLJIT_IMM, size * sizeof(sljit_sw));
2099    #ifdef DESTROY_REGISTERS
2100    OP1(SLJIT_MOV, TMP1, 0, SLJIT_IMM, 12345);
2101    OP1(SLJIT_MOV, TMP3, 0, TMP1, 0);
2102    OP1(SLJIT_MOV, RETURN_ADDR, 0, TMP1, 0);
2103    OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), LOCALS0, TMP1, 0);
2104    OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), LOCALS1, TMP1, 0);
2105    #endif
2106    add_stub(common, CMP(SLJIT_GREATER, STACK_TOP, 0, STACK_LIMIT, 0));
2107    }
2108    
2109    static SLJIT_INLINE void free_stack(compiler_common *common, int size)
2110    {
2111    DEFINE_COMPILER;
2112    OP2(SLJIT_SUB, STACK_TOP, 0, STACK_TOP, 0, SLJIT_IMM, size * sizeof(sljit_sw));
2113    }
2114    
2115    static sljit_uw * allocate_read_only_data(compiler_common *common, sljit_uw size)
2116    {
2117    DEFINE_COMPILER;
2118    sljit_uw *result;
2119    
2120    if (SLJIT_UNLIKELY(sljit_get_compiler_error(compiler)))
2121      return NULL;
2122    
2123    result = (sljit_uw *)SLJIT_MALLOC(size + sizeof(sljit_uw), compiler->allocator_data);
2124    if (SLJIT_UNLIKELY(result == NULL))
2125      {
2126      sljit_set_compiler_memory_error(compiler);
2127      return NULL;
2128      }
2129    
2130    *(void**)result = common->read_only_data_head;
2131    common->read_only_data_head = (void *)result;
2132    return result + 1;
2133    }
2134    
2135    static void free_read_only_data(void *current, void *allocator_data)
2136    {
2137    void *next;
2138    
2139    SLJIT_UNUSED_ARG(allocator_data);
2140    
2141    while (current != NULL)
2142      {
2143      next = *(void**)current;
2144      SLJIT_FREE(current, allocator_data);
2145      current = next;
2146      }
2147    }
2148    
2149    static SLJIT_INLINE void reset_ovector(compiler_common *common, int length)
2150    {
2151    DEFINE_COMPILER;
2152    struct sljit_label *loop;
2153    int i;
2154    
2155    /* At this point we can freely use all temporary registers. */
2156    SLJIT_ASSERT(length > 1);
2157    /* TMP1 returns with begin - 1. */
2158    OP2(SLJIT_SUB, SLJIT_R0, 0, SLJIT_MEM1(SLJIT_S0), SLJIT_OFFSETOF(jit_arguments, begin), SLJIT_IMM, IN_UCHARS(1));
2159    if (length < 8)
2160      {
2161      for (i = 1; i < length; i++)
2162        OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), OVECTOR(i), SLJIT_R0, 0);
2163      }
2164    else
2165      {
2166      GET_LOCAL_BASE(SLJIT_R1, 0, OVECTOR_START);
2167      OP1(SLJIT_MOV, SLJIT_R2, 0, SLJIT_IMM, length - 1);
2168      loop = LABEL();
2169      OP1(SLJIT_MOVU, SLJIT_MEM1(SLJIT_R1), sizeof(sljit_sw), SLJIT_R0, 0);
2170      OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_R2, 0, SLJIT_R2, 0, SLJIT_IMM, 1);
2171      JUMPTO(SLJIT_NOT_ZERO, loop);
2172      }
2173    }
2174    
2175    static SLJIT_INLINE void do_reset_match(compiler_common *common, int length)
2176    {
2177    DEFINE_COMPILER;
2178    struct sljit_label *loop;
2179    int i;
2180    
2181    SLJIT_ASSERT(length > 1);
2182    /* OVECTOR(1) contains the "string begin - 1" constant. */
2183    if (length > 2)
2184      OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), OVECTOR(1));
2185    if (length < 8)
2186      {
2187      for (i = 2; i < length; i++)
2188        OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), OVECTOR(i), TMP1, 0);
2189      }
2190    else
2191      {
2192      GET_LOCAL_BASE(TMP2, 0, OVECTOR_START + sizeof(sljit_sw));
2193      OP1(SLJIT_MOV, STACK_TOP, 0, SLJIT_IMM, length - 2);
2194      loop = LABEL();
2195      OP1(SLJIT_MOVU, SLJIT_MEM1(TMP2), sizeof(sljit_sw), TMP1, 0);
2196      OP2(SLJIT_SUB | SLJIT_SET_E, STACK_TOP, 0, STACK_TOP, 0, SLJIT_IMM, 1);
2197      JUMPTO(SLJIT_NOT_ZERO, loop);
2198      }
2199    
2200    OP1(SLJIT_MOV, STACK_TOP, 0, ARGUMENTS, 0);
2201    if (common->mark_ptr != 0)
2202      OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->mark_ptr, SLJIT_IMM, 0);
2203    if (common->control_head_ptr != 0)
2204      OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->control_head_ptr, SLJIT_IMM, 0);
2205    OP1(SLJIT_MOV, STACK_TOP, 0, SLJIT_MEM1(STACK_TOP), SLJIT_OFFSETOF(jit_arguments, stack));
2206    OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), common->start_ptr);
2207    OP1(SLJIT_MOV, STACK_TOP, 0, SLJIT_MEM1(STACK_TOP), SLJIT_OFFSETOF(struct sljit_stack, base));
2208    }
2209    
2210    static sljit_sw SLJIT_CALL do_search_mark(sljit_sw *current, const pcre_uchar *skip_arg)
2211    {
2212    while (current != NULL)
2213      {
2214      switch (current[-2])
2215        {
2216        case type_then_trap:
2217        break;
2218    
2219        case type_mark:
2220        if (STRCMP_UC_UC(skip_arg, (pcre_uchar *)current[-3]) == 0)
2221          return current[-4];
2222        break;
2223    
2224        default:
2225        SLJIT_ASSERT_STOP();
2226        break;
2227        }
2228      current = (sljit_sw*)current[-1];
2229      }
2230    return -1;
2231    }
2232    
2233    static SLJIT_INLINE void copy_ovector(compiler_common *common, int topbracket)
2234    {
2235    DEFINE_COMPILER;
2236    struct sljit_label *loop;
2237    struct sljit_jump *early_quit;
2238    
2239    /* At this point we can freely use all registers. */
2240    OP1(SLJIT_MOV, SLJIT_S2, 0, SLJIT_MEM1(SLJIT_SP), OVECTOR(1));
2241    OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), OVECTOR(1), STR_PTR, 0);
2242    
2243    OP1(SLJIT_MOV, SLJIT_R0, 0, ARGUMENTS, 0);
2244    if (common->mark_ptr != 0)
2245      OP1(SLJIT_MOV, SLJIT_R2, 0, SLJIT_MEM1(SLJIT_SP), common->mark_ptr);
2246    OP1(SLJIT_MOV_SI, SLJIT_R1, 0, SLJIT_MEM1(SLJIT_R0), SLJIT_OFFSETOF(jit_arguments, offset_count));
2247    if (common->mark_ptr != 0)
2248      OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_R0), SLJIT_OFFSETOF(jit_arguments, mark_ptr), SLJIT_R2, 0);
2249    OP2(SLJIT_SUB, SLJIT_R2, 0, SLJIT_MEM1(SLJIT_R0), SLJIT_OFFSETOF(jit_arguments, offsets), SLJIT_IMM, sizeof(int));
2250    OP1(SLJIT_MOV, SLJIT_R0, 0, SLJIT_MEM1(SLJIT_R0), SLJIT_OFFSETOF(jit_arguments, begin));
2251    GET_LOCAL_BASE(SLJIT_S0, 0, OVECTOR_START);
2252    /* Unlikely, but possible */
2253    early_quit = CMP(SLJIT_EQUAL, SLJIT_R1, 0, SLJIT_IMM, 0);
2254    loop = LABEL();
2255    OP2(SLJIT_SUB, SLJIT_S1, 0, SLJIT_MEM1(SLJIT_S0), 0, SLJIT_R0, 0);
2256    OP2(SLJIT_ADD, SLJIT_S0, 0, SLJIT_S0, 0, SLJIT_IMM, sizeof(sljit_sw));
2257    /* Copy the integer value to the output buffer */
2258    #if defined COMPILE_PCRE16 || defined COMPILE_PCRE32
2259    OP2(SLJIT_ASHR, SLJIT_S1, 0, SLJIT_S1, 0, SLJIT_IMM, UCHAR_SHIFT);
2260    #endif
2261    OP1(SLJIT_MOVU_SI, SLJIT_MEM1(SLJIT_R2), sizeof(int), SLJIT_S1, 0);
2262    OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_R1, 0, SLJIT_R1, 0, SLJIT_IMM, 1);
2263    JUMPTO(SLJIT_NOT_ZERO, loop);
2264    JUMPHERE(early_quit);
2265    
2266    /* Calculate the return value, which is the maximum ovector value. */
2267    if (topbracket > 1)
2268      {
2269      GET_LOCAL_BASE(SLJIT_R0, 0, OVECTOR_START + topbracket * 2 * sizeof(sljit_sw));
2270      OP1(SLJIT_MOV, SLJIT_R1, 0, SLJIT_IMM, topbracket + 1);
2271    
2272      /* OVECTOR(0) is never equal to SLJIT_S2. */
2273      loop = LABEL();
2274      OP1(SLJIT_MOVU, SLJIT_R2, 0, SLJIT_MEM1(SLJIT_R0), -(2 * (sljit_sw)sizeof(sljit_sw)));
2275      OP2(SLJIT_SUB, SLJIT_R1, 0, SLJIT_R1, 0, SLJIT_IMM, 1);
2276      CMPTO(SLJIT_EQUAL, SLJIT_R2, 0, SLJIT_S2, 0, loop);
2277      OP1(SLJIT_MOV, SLJIT_RETURN_REG, 0, SLJIT_R1, 0);
2278      }
2279    else
2280      OP1(SLJIT_MOV, SLJIT_RETURN_REG, 0, SLJIT_IMM, 1);
2281    }
2282    
2283    static SLJIT_INLINE void return_with_partial_match(compiler_common *common, struct sljit_label *quit)
2284    {
2285    DEFINE_COMPILER;
2286    struct sljit_jump *jump;
2287    
2288    SLJIT_COMPILE_ASSERT(STR_END == SLJIT_S1, str_end_must_be_saved_reg2);
2289    SLJIT_ASSERT(common->start_used_ptr != 0 && common->start_ptr != 0
2290      && (common->mode == JIT_PARTIAL_SOFT_COMPILE ? common->hit_start != 0 : common->hit_start == 0));
2291    
2292    OP1(SLJIT_MOV, SLJIT_R1, 0, ARGUMENTS, 0);
2293    OP1(SLJIT_MOV, SLJIT_RETURN_REG, 0, SLJIT_IMM, PCRE_ERROR_PARTIAL);
2294    OP1(SLJIT_MOV_SI, SLJIT_R2, 0, SLJIT_MEM1(SLJIT_R1), SLJIT_OFFSETOF(jit_arguments, real_offset_count));
2295    CMPTO(SLJIT_SIG_LESS, SLJIT_R2, 0, SLJIT_IMM, 2, quit);
2296    
2297    /* Store match begin and end. */
2298    OP1(SLJIT_MOV, SLJIT_S0, 0, SLJIT_MEM1(SLJIT_R1), SLJIT_OFFSETOF(jit_arguments, begin));
2299    OP1(SLJIT_MOV, SLJIT_R1, 0, SLJIT_MEM1(SLJIT_R1), SLJIT_OFFSETOF(jit_arguments, offsets));
2300    
2301    jump = CMP(SLJIT_SIG_LESS, SLJIT_R2, 0, SLJIT_IMM, 3);
2302    OP2(SLJIT_SUB, SLJIT_R2, 0, SLJIT_MEM1(SLJIT_SP), common->mode == JIT_PARTIAL_HARD_COMPILE ? common->start_ptr : (common->hit_start + (int)sizeof(sljit_sw)), SLJIT_S0, 0);
2303    #if defined COMPILE_PCRE16 || defined COMPILE_PCRE32
2304    OP2(SLJIT_ASHR, SLJIT_R2, 0, SLJIT_R2, 0, SLJIT_IMM, UCHAR_SHIFT);
2305    #endif
2306    OP1(SLJIT_MOV_SI, SLJIT_MEM1(SLJIT_R1), 2 * sizeof(int), SLJIT_R2, 0);
2307    JUMPHERE(jump);
2308    
2309    OP1(SLJIT_MOV, SLJIT_R2, 0, SLJIT_MEM1(SLJIT_SP), common->mode == JIT_PARTIAL_HARD_COMPILE ? common->start_used_ptr : common->hit_start);
2310    OP2(SLJIT_SUB, SLJIT_S1, 0, STR_END, 0, SLJIT_S0, 0);
2311    #if defined COMPILE_PCRE16 || defined COMPILE_PCRE32
2312    OP2(SLJIT_ASHR, SLJIT_S1, 0, SLJIT_S1, 0, SLJIT_IMM, UCHAR_SHIFT);
2313    #endif
2314    OP1(SLJIT_MOV_SI, SLJIT_MEM1(SLJIT_R1), sizeof(int), SLJIT_S1, 0);
2315    
2316    OP2(SLJIT_SUB, SLJIT_R2, 0, SLJIT_R2, 0, SLJIT_S0, 0);
2317    #if defined COMPILE_PCRE16 || defined COMPILE_PCRE32
2318    OP2(SLJIT_ASHR, SLJIT_R2, 0, SLJIT_R2, 0, SLJIT_IMM, UCHAR_SHIFT);
2319    #endif
2320    OP1(SLJIT_MOV_SI, SLJIT_MEM1(SLJIT_R1), 0, SLJIT_R2, 0);
2321    
2322    JUMPTO(SLJIT_JUMP, quit);
2323    }
2324    
2325    static SLJIT_INLINE void check_start_used_ptr(compiler_common *common)
2326    {
2327    /* May destroy TMP1. */
2328    DEFINE_COMPILER;
2329    struct sljit_jump *jump;
2330    
2331    if (common->mode == JIT_PARTIAL_SOFT_COMPILE)
2332      {
2333      /* The value of -1 must be kept for start_used_ptr! */
2334      OP2(SLJIT_ADD, TMP1, 0, SLJIT_MEM1(SLJIT_SP), common->start_used_ptr, SLJIT_IMM, 1);
2335      /* Jumps if start_used_ptr < STR_PTR, or start_used_ptr == -1. Although overwriting
2336      is not necessary if start_used_ptr == STR_PTR, it does not hurt as well. */
2337      jump = CMP(SLJIT_LESS_EQUAL, TMP1, 0, STR_PTR, 0);
2338      OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->start_used_ptr, STR_PTR, 0);
2339      JUMPHERE(jump);
2340      }
2341    else if (common->mode == JIT_PARTIAL_HARD_COMPILE)
2342      {
2343      jump = CMP(SLJIT_LESS_EQUAL, SLJIT_MEM1(SLJIT_SP), common->start_used_ptr, STR_PTR, 0);
2344      OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->start_used_ptr, STR_PTR, 0);
2345      JUMPHERE(jump);
2346      }
2347    }
2348    
2349    static SLJIT_INLINE BOOL char_has_othercase(compiler_common *common, pcre_uchar *cc)
2350    {
2351    /* Detects if the character has an othercase. */
2352    unsigned int c;
2353    
2354    #ifdef SUPPORT_UTF
2355    if (common->utf)
2356      {
2357      GETCHAR(c, cc);
2358      if (c > 127)
2359        {
2360    #ifdef SUPPORT_UCP
2361        return c != UCD_OTHERCASE(c);
2362    #else
2363        return FALSE;
2364    #endif
2365        }
2366    #ifndef COMPILE_PCRE8
2367      return common->fcc[c] != c;
2368    #endif
2369      }
2370    else
2371    #endif
2372      c = *cc;
2373    return MAX_255(c) ? common->fcc[c] != c : FALSE;
2374    }
2375    
2376    static SLJIT_INLINE unsigned int char_othercase(compiler_common *common, unsigned int c)
2377    {
2378    /* Returns with the othercase. */
2379    #ifdef SUPPORT_UTF
2380    if (common->utf && c > 127)
2381      {
2382    #ifdef SUPPORT_UCP
2383      return UCD_OTHERCASE(c);
2384    #else
2385      return c;
2386    #endif
2387      }
2388    #endif
2389    return TABLE_GET(c, common->fcc, c);
2390    }
2391    
2392    static unsigned int char_get_othercase_bit(compiler_common *common, pcre_uchar *cc)
2393    {
2394    /* Detects if the character and its othercase has only 1 bit difference. */
2395    unsigned int c, oc, bit;
2396    #if defined SUPPORT_UTF && defined COMPILE_PCRE8
2397    int n;
2398    #endif
2399    
2400    #ifdef SUPPORT_UTF
2401    if (common->utf)
2402      {
2403      GETCHAR(c, cc);
2404      if (c <= 127)
2405        oc = common->fcc[c];
2406      else
2407        {
2408    #ifdef SUPPORT_UCP
2409        oc = UCD_OTHERCASE(c);
2410    #else
2411        oc = c;
2412    #endif
2413        }
2414      }
2415    else
2416      {
2417      c = *cc;
2418      oc = TABLE_GET(c, common->fcc, c);
2419      }
2420    #else
2421    c = *cc;
2422    oc = TABLE_GET(c, common->fcc, c);
2423    #endif
2424    
2425    SLJIT_ASSERT(c != oc);
2426    
2427    bit = c ^ oc;
2428    /* Optimized for English alphabet. */
2429    if (c <= 127 && bit == 0x20)
2430      return (0 << 8) | 0x20;
2431    
2432    /* Since c != oc, they must have at least 1 bit difference. */
2433    if (!is_powerof2(bit))
2434      return 0;
2435    
2436    #if defined COMPILE_PCRE8
2437    
2438    #ifdef SUPPORT_UTF
2439    if (common->utf && c > 127)
2440      {
2441      n = GET_EXTRALEN(*cc);
2442      while ((bit & 0x3f) == 0)
2443        {
2444        n--;
2445        bit >>= 6;
2446        }
2447      return (n << 8) | bit;
2448      }
2449    #endif /* SUPPORT_UTF */
2450    return (0 << 8) | bit;
2451    
2452    #elif defined COMPILE_PCRE16 || defined COMPILE_PCRE32
2453    
2454    #ifdef SUPPORT_UTF
2455    if (common->utf && c > 65535)
2456      {
2457      if (bit >= (1 << 10))
2458        bit >>= 10;
2459      else
2460        return (bit < 256) ? ((2 << 8) | bit) : ((3 << 8) | (bit >> 8));
2461      }
2462    #endif /* SUPPORT_UTF */
2463    return (bit < 256) ? ((0 << 8) | bit) : ((1 << 8) | (bit >> 8));
2464    
2465    #endif /* COMPILE_PCRE[8|16|32] */
2466    }
2467    
2468    static void check_partial(compiler_common *common, BOOL force)
2469    {
2470    /* Checks whether a partial matching is occurred. Does not modify registers. */
2471    DEFINE_COMPILER;
2472    struct sljit_jump *jump = NULL;
2473    
2474    SLJIT_ASSERT(!force || common->mode != JIT_COMPILE);
2475    
2476    if (common->mode == JIT_COMPILE)
2477      return;
2478    
2479    if (!force)
2480      jump = CMP(SLJIT_GREATER_EQUAL, SLJIT_MEM1(SLJIT_SP), common->start_used_ptr, STR_PTR, 0);
2481    else if (common->mode == JIT_PARTIAL_SOFT_COMPILE)
2482      jump = CMP(SLJIT_EQUAL, SLJIT_MEM1(SLJIT_SP), common->start_used_ptr, SLJIT_IMM, -1);
2483    
2484    if (common->mode == JIT_PARTIAL_SOFT_COMPILE)
2485      OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->hit_start, SLJIT_IMM, 0);
2486    else
2487      {
2488      if (common->partialmatchlabel != NULL)
2489        JUMPTO(SLJIT_JUMP, common->partialmatchlabel);
2490      else
2491        add_jump(compiler, &common->partialmatch, JUMP(SLJIT_JUMP));
2492      }
2493    
2494    if (jump != NULL)
2495      JUMPHERE(jump);
2496    }
2497    
2498    static void check_str_end(compiler_common *common, jump_list **end_reached)
2499    {
2500    /* Does not affect registers. Usually used in a tight spot. */
2501    DEFINE_COMPILER;
2502    struct sljit_jump *jump;
2503    
2504    if (common->mode == JIT_COMPILE)
2505      {
2506      add_jump(compiler, end_reached, CMP(SLJIT_GREATER_EQUAL, STR_PTR, 0, STR_END, 0));
2507      return;
2508      }
2509    
2510    jump = CMP(SLJIT_LESS, STR_PTR, 0, STR_END, 0);
2511    if (common->mode == JIT_PARTIAL_SOFT_COMPILE)
2512      {
2513      add_jump(compiler, end_reached, CMP(SLJIT_GREATER_EQUAL, SLJIT_MEM1(SLJIT_SP), common->start_used_ptr, STR_PTR, 0));
2514      OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->hit_start, SLJIT_IMM, 0);
2515      add_jump(compiler, end_reached, JUMP(SLJIT_JUMP));
2516      }
2517    else
2518      {
2519      add_jump(compiler, end_reached, CMP(SLJIT_GREATER_EQUAL, SLJIT_MEM1(SLJIT_SP), common->start_used_ptr, STR_PTR, 0));
2520      if (common->partialmatchlabel != NULL)
2521        JUMPTO(SLJIT_JUMP, common->partialmatchlabel);
2522      else
2523        add_jump(compiler, &common->partialmatch, JUMP(SLJIT_JUMP));
2524      }
2525    JUMPHERE(jump);
2526    }
2527    
2528    static void detect_partial_match(compiler_common *common, jump_list **backtracks)
2529    {
2530    DEFINE_COMPILER;
2531    struct sljit_jump *jump;
2532    
2533    if (common->mode == JIT_COMPILE)
2534      {
2535      add_jump(compiler, backtracks, CMP(SLJIT_GREATER_EQUAL, STR_PTR, 0, STR_END, 0));
2536      return;
2537      }
2538    
2539    /* Partial matching mode. */
2540    jump = CMP(SLJIT_LESS, STR_PTR, 0, STR_END, 0);
2541    add_jump(compiler, backtracks, CMP(SLJIT_GREATER_EQUAL, SLJIT_MEM1(SLJIT_SP), common->start_used_ptr, STR_PTR, 0));
2542    if (common->mode == JIT_PARTIAL_SOFT_COMPILE)
2543      {
2544      OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->hit_start, SLJIT_IMM, 0);
2545      add_jump(compiler, backtracks, JUMP(SLJIT_JUMP));
2546      }
2547    else
2548      {
2549      if (common->partialmatchlabel != NULL)
2550        JUMPTO(SLJIT_JUMP, common->partialmatchlabel);
2551      else
2552        add_jump(compiler, &common->partialmatch, JUMP(SLJIT_JUMP));
2553      }
2554    JUMPHERE(jump);
2555    }
2556    
2557    static void peek_char(compiler_common *common, pcre_uint32 max)
2558    {
2559    /* Reads the character into TMP1, keeps STR_PTR.
2560    Does not check STR_END. TMP2 Destroyed. */
2561    DEFINE_COMPILER;
2562    #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
2563    struct sljit_jump *jump;
2564    #endif
2565    
2566    SLJIT_UNUSED_ARG(max);
2567    
2568    OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), 0);
2569    #if defined SUPPORT_UTF && defined COMPILE_PCRE8
2570    if (common->utf)
2571      {
2572      if (max < 128) return;
2573    
2574      jump = CMP(SLJIT_LESS, TMP1, 0, SLJIT_IMM, 0xc0);
2575      OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
2576      add_jump(compiler, &common->utfreadchar, JUMP(SLJIT_FAST_CALL));
2577      OP2(SLJIT_SUB, STR_PTR, 0, STR_PTR, 0, TMP2, 0);
2578      JUMPHERE(jump);
2579      }
2580    #endif /* SUPPORT_UTF && !COMPILE_PCRE32 */
2581    
2582    #if defined SUPPORT_UTF && defined COMPILE_PCRE16
2583    if (common->utf)
2584      {
2585      if (max < 0xd800) return;
2586    
2587      OP2(SLJIT_SUB, TMP2, 0, TMP1, 0, SLJIT_IMM, 0xd800);
2588      jump = CMP(SLJIT_GREATER, TMP2, 0, SLJIT_IMM, 0xdc00 - 0xd800 - 1);
2589      /* TMP2 contains the high surrogate. */
2590      OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(0));
2591      OP2(SLJIT_ADD, TMP2, 0, TMP2, 0, SLJIT_IMM, 0x40);
2592      OP2(SLJIT_SHL, TMP2, 0, TMP2, 0, SLJIT_IMM, 10);
2593      OP2(SLJIT_AND, TMP1, 0, TMP1, 0, SLJIT_IMM, 0x3ff);
2594      OP2(SLJIT_OR, TMP1, 0, TMP1, 0, TMP2, 0);
2595      JUMPHERE(jump);
2596      }
2597    #endif
2598    }
2599    
2600    #if defined SUPPORT_UTF && defined COMPILE_PCRE8
2601    
2602    static BOOL is_char7_bitset(const pcre_uint8 *bitset, BOOL nclass)
2603    {
2604    /* Tells whether the character codes below 128 are enough
2605    to determine a match. */
2606    const pcre_uint8 value = nclass ? 0xff : 0;
2607    const pcre_uint8 *end = bitset + 32;
2608    
2609    bitset += 16;
2610    do
2611      {
2612      if (*bitset++ != value)
2613        return FALSE;
2614      }
2615    while (bitset < end);
2616    return TRUE;
2617    }
2618    
2619    static void read_char7_type(compiler_common *common, BOOL full_read)
2620    {
2621    /* Reads the precise character type of a character into TMP1, if the character
2622    is less than 128. Otherwise it returns with zero. Does not check STR_END. The
2623    full_read argument tells whether characters above max are accepted or not. */
2624    DEFINE_COMPILER;
2625    struct sljit_jump *jump;
2626    
2627    SLJIT_ASSERT(common->utf);
2628    
2629    OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), 0);
2630    OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
2631    
2632    OP1(SLJIT_MOV_UB, TMP1, 0, SLJIT_MEM1(TMP2), common->ctypes);
2633    
2634    if (full_read)
2635      {
2636      jump = CMP(SLJIT_LESS, TMP2, 0, SLJIT_IMM, 0xc0);
2637      OP1(SLJIT_MOV_UB, TMP2, 0, SLJIT_MEM1(TMP2), (sljit_sw)PRIV(utf8_table4) - 0xc0);
2638      OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, TMP2, 0);
2639      JUMPHERE(jump);
2640      }
2641    }
2642    
2643    #endif /* SUPPORT_UTF && COMPILE_PCRE8 */
2644    
2645    static void read_char_range(compiler_common *common, pcre_uint32 min, pcre_uint32 max, BOOL update_str_ptr)
2646    {
2647    /* Reads the precise value of a character into TMP1, if the character is
2648    between min and max (c >= min && c <= max). Otherwise it returns with a value
2649    outside the range. Does not check STR_END. */
2650    DEFINE_COMPILER;
2651    #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
2652    struct sljit_jump *jump;
2653    #endif
2654    #if defined SUPPORT_UTF && defined COMPILE_PCRE8
2655    struct sljit_jump *jump2;
2656    #endif
2657    
2658    SLJIT_UNUSED_ARG(update_str_ptr);
2659    SLJIT_UNUSED_ARG(min);
2660    SLJIT_UNUSED_ARG(max);
2661    SLJIT_ASSERT(min <= max);
2662    
2663    OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(0));
2664    OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
2665    
2666    #if defined SUPPORT_UTF && defined COMPILE_PCRE8
2667    if (common->utf)
2668      {
2669      if (max < 128 && !update_str_ptr) return;
2670    
2671      jump = CMP(SLJIT_LESS, TMP1, 0, SLJIT_IMM, 0xc0);
2672      if (min >= 0x10000)
2673        {
2674        OP2(SLJIT_SUB, TMP2, 0, TMP1, 0, SLJIT_IMM, 0xf0);
2675        if (update_str_ptr)
2676          OP1(SLJIT_MOV_UB, RETURN_ADDR, 0, SLJIT_MEM1(TMP1), (sljit_sw)PRIV(utf8_table4) - 0xc0);
2677        OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(0));
2678        jump2 = CMP(SLJIT_GREATER, TMP2, 0, SLJIT_IMM, 0x7);
2679        OP2(SLJIT_SHL, TMP2, 0, TMP2, 0, SLJIT_IMM, 6);
2680        OP2(SLJIT_AND, TMP1, 0, TMP1, 0, SLJIT_IMM, 0x3f);
2681        OP2(SLJIT_OR, TMP1, 0, TMP1, 0, TMP2, 0);
2682        OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(1));
2683        OP2(SLJIT_SHL, TMP1, 0, TMP1, 0, SLJIT_IMM, 6);
2684        OP2(SLJIT_AND, TMP2, 0, TMP2, 0, SLJIT_IMM, 0x3f);
2685        OP2(SLJIT_OR, TMP1, 0, TMP1, 0, TMP2, 0);
2686        OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(2));
2687        if (!update_str_ptr)
2688          OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(3));
2689        OP2(SLJIT_SHL, TMP1, 0, TMP1, 0, SLJIT_IMM, 6);
2690        OP2(SLJIT_AND, TMP2, 0, TMP2, 0, SLJIT_IMM, 0x3f);
2691        OP2(SLJIT_OR, TMP1, 0, TMP1, 0, TMP2, 0);
2692        JUMPHERE(jump2);
2693        if (update_str_ptr)
2694          OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, RETURN_ADDR, 0);
2695        }
2696      else if (min >= 0x800 && max <= 0xffff)
2697        {
2698        OP2(SLJIT_SUB, TMP2, 0, TMP1, 0, SLJIT_IMM, 0xe0);
2699        if (update_str_ptr)
2700          OP1(SLJIT_MOV_UB, RETURN_ADDR, 0, SLJIT_MEM1(TMP1), (sljit_sw)PRIV(utf8_table4) - 0xc0);
2701        OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(0));
2702        jump2 = CMP(SLJIT_GREATER, TMP2, 0, SLJIT_IMM, 0xf);
2703        OP2(SLJIT_SHL, TMP2, 0, TMP2, 0, SLJIT_IMM, 6);
2704        OP2(SLJIT_AND, TMP1, 0, TMP1, 0, SLJIT_IMM, 0x3f);
2705        OP2(SLJIT_OR, TMP1, 0, TMP1, 0, TMP2, 0);
2706        OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(1));
2707        if (!update_str_ptr)
2708          OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(2));
2709        OP2(SLJIT_SHL, TMP1, 0, TMP1, 0, SLJIT_IMM, 6);
2710        OP2(SLJIT_AND, TMP2, 0, TMP2, 0, SLJIT_IMM, 0x3f);
2711        OP2(SLJIT_OR, TMP1, 0, TMP1, 0, TMP2, 0);
2712        JUMPHERE(jump2);
2713        if (update_str_ptr)
2714          OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, RETURN_ADDR, 0);
2715        }
2716      else if (max >= 0x800)
2717        add_jump(compiler, (max < 0x10000) ? &common->utfreadchar16 : &common->utfreadchar, JUMP(SLJIT_FAST_CALL));
2718      else if (max < 128)
2719        {
2720        OP1(SLJIT_MOV_UB, TMP2, 0, SLJIT_MEM1(TMP1), (sljit_sw)PRIV(utf8_table4) - 0xc0);
2721        OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, TMP2, 0);
2722        }
2723      else
2724        {
2725        OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(0));
2726        if (!update_str_ptr)
2727          OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
2728        else
2729          OP1(SLJIT_MOV_UB, RETURN_ADDR, 0, SLJIT_MEM1(TMP1), (sljit_sw)PRIV(utf8_table4) - 0xc0);
2730        OP2(SLJIT_AND, TMP1, 0, TMP1, 0, SLJIT_IMM, 0x3f);
2731        OP2(SLJIT_SHL, TMP1, 0, TMP1, 0, SLJIT_IMM, 6);
2732        OP2(SLJIT_AND, TMP2, 0, TMP2, 0, SLJIT_IMM, 0x3f);
2733        OP2(SLJIT_OR, TMP1, 0, TMP1, 0, TMP2, 0);
2734        if (update_str_ptr)
2735          OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, RETURN_ADDR, 0);
2736        }
2737      JUMPHERE(jump);
2738      }
2739    #endif
2740    
2741    #if defined SUPPORT_UTF && defined COMPILE_PCRE16
2742    if (common->utf)
2743      {
2744      if (max >= 0x10000)
2745        {
2746        OP2(SLJIT_SUB, TMP2, 0, TMP1, 0, SLJIT_IMM, 0xd800);
2747        jump = CMP(SLJIT_GREATER, TMP2, 0, SLJIT_IMM, 0xdc00 - 0xd800 - 1);
2748        /* TMP2 contains the high surrogate. */
2749        OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(0));
2750        OP2(SLJIT_ADD, TMP2, 0, TMP2, 0, SLJIT_IMM, 0x40);
2751        OP2(SLJIT_SHL, TMP2, 0, TMP2, 0, SLJIT_IMM, 10);
2752        OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
2753        OP2(SLJIT_AND, TMP1, 0, TMP1, 0, SLJIT_IMM, 0x3ff);
2754        OP2(SLJIT_OR, TMP1, 0, TMP1, 0, TMP2, 0);
2755        JUMPHERE(jump);
2756        return;
2757        }
2758    
2759      if (max < 0xd800 && !update_str_ptr) return;
2760    
2761      /* Skip low surrogate if necessary. */
2762      OP2(SLJIT_SUB, TMP2, 0, TMP1, 0, SLJIT_IMM, 0xd800);
2763      jump = CMP(SLJIT_GREATER, TMP2, 0, SLJIT_IMM, 0xdc00 - 0xd800 - 1);
2764      if (update_str_ptr)
2765        OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
2766      if (max >= 0xd800)
2767        OP1(SLJIT_MOV, TMP1, 0, SLJIT_IMM, 0x10000);
2768      JUMPHERE(jump);
2769      }
2770    #endif
2771    }
2772    
2773    static SLJIT_INLINE void read_char(compiler_common *common)
2774    {
2775    read_char_range(common, 0, READ_CHAR_MAX, TRUE);
2776    }
2777    
2778    static void read_char8_type(compiler_common *common, BOOL update_str_ptr)
2779    {
2780    /* Reads the character type into TMP1, updates STR_PTR. Does not check STR_END. */
2781    DEFINE_COMPILER;
2782    #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
2783    struct sljit_jump *jump;
2784    #endif
2785    #if defined SUPPORT_UTF && defined COMPILE_PCRE8
2786    struct sljit_jump *jump2;
2787    #endif
2788    
2789    SLJIT_UNUSED_ARG(update_str_ptr);
2790    
2791    OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), 0);
2792    OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
2793    
2794    #if defined SUPPORT_UTF && defined COMPILE_PCRE8
2795    if (common->utf)
2796      {
2797      /* This can be an extra read in some situations, but hopefully
2798      it is needed in most cases. */
2799      OP1(SLJIT_MOV_UB, TMP1, 0, SLJIT_MEM1(TMP2), common->ctypes);
2800      jump = CMP(SLJIT_LESS, TMP2, 0, SLJIT_IMM, 0xc0);
2801      if (!update_str_ptr)
2802        {
2803        OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(0));
2804        OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
2805        OP2(SLJIT_AND, TMP2, 0, TMP2, 0, SLJIT_IMM, 0x3f);
2806        OP2(SLJIT_SHL, TMP2, 0, TMP2, 0, SLJIT_IMM, 6);
2807        OP2(SLJIT_AND, TMP1, 0, TMP1, 0, SLJIT_IMM, 0x3f);
2808        OP2(SLJIT_OR, TMP2, 0, TMP2, 0, TMP1, 0);
2809        OP1(SLJIT_MOV, TMP1, 0, SLJIT_IMM, 0);
2810        jump2 = CMP(SLJIT_GREATER, TMP2, 0, SLJIT_IMM, 255);
2811        OP1(SLJIT_MOV_UB, TMP1, 0, SLJIT_MEM1(TMP2), common->ctypes);
2812        JUMPHERE(jump2);
2813        }
2814      else
2815        add_jump(compiler, &common->utfreadtype8, JUMP(SLJIT_FAST_CALL));
2816      JUMPHERE(jump);
2817      return;
2818      }
2819    #endif /* SUPPORT_UTF && COMPILE_PCRE8 */
2820    
2821    #if !defined COMPILE_PCRE8
2822    /* The ctypes array contains only 256 values. */
2823    OP1(SLJIT_MOV, TMP1, 0, SLJIT_IMM, 0);
2824    jump = CMP(SLJIT_GREATER, TMP2, 0, SLJIT_IMM, 255);
2825    #endif
2826    OP1(SLJIT_MOV_UB, TMP1, 0, SLJIT_MEM1(TMP2), common->ctypes);
2827    #if !defined COMPILE_PCRE8
2828    JUMPHERE(jump);
2829    #endif
2830    
2831    #if defined SUPPORT_UTF && defined COMPILE_PCRE16
2832    if (common->utf && update_str_ptr)
2833      {
2834      /* Skip low surrogate if necessary. */
2835      OP2(SLJIT_SUB, TMP2, 0, TMP2, 0, SLJIT_IMM, 0xd800);
2836      jump = CMP(SLJIT_GREATER, TMP2, 0, SLJIT_IMM, 0xdc00 - 0xd800 - 1);
2837      OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
2838      JUMPHERE(jump);
2839      }
2840    #endif /* SUPPORT_UTF && COMPILE_PCRE16 */
2841    }
2842    
2843    static void skip_char_back(compiler_common *common)
2844    {
2845    /* Goes one character back. Affects STR_PTR and TMP1. Does not check begin. */
2846    DEFINE_COMPILER;
2847    #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
2848    #if defined COMPILE_PCRE8
2849    struct sljit_label *label;
2850    
2851    if (common->utf)
2852      {
2853      label = LABEL();
2854      OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), -IN_UCHARS(1));
2855      OP2(SLJIT_SUB, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
2856      OP2(SLJIT_AND, TMP1, 0, TMP1, 0, SLJIT_IMM, 0xc0);
2857      CMPTO(SLJIT_EQUAL, TMP1, 0, SLJIT_IMM, 0x80, label);
2858      return;
2859      }
2860    #elif defined COMPILE_PCRE16
2861    if (common->utf)
2862      {
2863      OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), -IN_UCHARS(1));
2864      OP2(SLJIT_SUB, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
2865      /* Skip low surrogate if necessary. */
2866      OP2(SLJIT_AND, TMP1, 0, TMP1, 0, SLJIT_IMM, 0xfc00);
2867      OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0xdc00);
2868      OP_FLAGS(SLJIT_MOV, TMP1, 0, SLJIT_UNUSED, 0, SLJIT_EQUAL);
2869      OP2(SLJIT_SHL, TMP1, 0, TMP1, 0, SLJIT_IMM, 1);
2870      OP2(SLJIT_SUB, STR_PTR, 0, STR_PTR, 0, TMP1, 0);
2871      return;
2872      }
2873    #endif /* COMPILE_PCRE[8|16] */
2874    #endif /* SUPPORT_UTF && !COMPILE_PCRE32 */
2875    OP2(SLJIT_SUB, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
2876    }
2877    
2878    static void check_newlinechar(compiler_common *common, int nltype, jump_list **backtracks, BOOL jumpifmatch)
2879    {
2880    /* Character comes in TMP1. Checks if it is a newline. TMP2 may be destroyed. */
2881    DEFINE_COMPILER;
2882    struct sljit_jump *jump;
2883    
2884    if (nltype == NLTYPE_ANY)
2885      {
2886      add_jump(compiler, &common->anynewline, JUMP(SLJIT_FAST_CALL));
2887      add_jump(compiler, backtracks, JUMP(jumpifmatch ? SLJIT_NOT_ZERO : SLJIT_ZERO));
2888      }
2889    else if (nltype == NLTYPE_ANYCRLF)
2890      {
2891      if (jumpifmatch)
2892        {
2893        add_jump(compiler, backtracks, CMP(SLJIT_EQUAL, TMP1, 0, SLJIT_IMM, CHAR_CR));
2894        add_jump(compiler, backtracks, CMP(SLJIT_EQUAL, TMP1, 0, SLJIT_IMM, CHAR_NL));
2895        }
2896      else
2897        {
2898        jump = CMP(SLJIT_EQUAL, TMP1, 0, SLJIT_IMM, CHAR_CR);
2899        add_jump(compiler, backtracks, CMP(SLJIT_NOT_EQUAL, TMP1, 0, SLJIT_IMM, CHAR_NL));
2900        JUMPHERE(jump);
2901        }
2902      }
2903    else
2904      {
2905      SLJIT_ASSERT(nltype == NLTYPE_FIXED && common->newline < 256);
2906      add_jump(compiler, backtracks, CMP(jumpifmatch ? SLJIT_EQUAL : SLJIT_NOT_EQUAL, TMP1, 0, SLJIT_IMM, common->newline));
2907      }
2908    }
2909    
2910    #ifdef SUPPORT_UTF
2911    
2912    #if defined COMPILE_PCRE8
2913    static void do_utfreadchar(compiler_common *common)
2914    {
2915    /* Fast decoding a UTF-8 character. TMP1 contains the first byte
2916    of the character (>= 0xc0). Return char value in TMP1, length in TMP2. */
2917    DEFINE_COMPILER;
2918    struct sljit_jump *jump;
2919    
2920    sljit_emit_fast_enter(compiler, RETURN_ADDR, 0);
2921    OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(0));
2922    OP2(SLJIT_AND, TMP1, 0, TMP1, 0, SLJIT_IMM, 0x3f);
2923    OP2(SLJIT_SHL, TMP1, 0, TMP1, 0, SLJIT_IMM, 6);
2924    OP2(SLJIT_AND, TMP2, 0, TMP2, 0, SLJIT_IMM, 0x3f);
2925    OP2(SLJIT_OR, TMP1, 0, TMP1, 0, TMP2, 0);
2926    
2927    /* Searching for the first zero. */
2928    OP2(SLJIT_AND | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x800);
2929    jump = JUMP(SLJIT_NOT_ZERO);
2930    /* Two byte sequence. */
2931    OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
2932    OP1(SLJIT_MOV, TMP2, 0, SLJIT_IMM, IN_UCHARS(2));
2933    sljit_emit_fast_return(compiler, RETURN_ADDR, 0);
2934    
2935    JUMPHERE(jump);
2936    OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(1));
2937    OP2(SLJIT_XOR, TMP1, 0, TMP1, 0, SLJIT_IMM, 0x800);
2938    OP2(SLJIT_SHL, TMP1, 0, TMP1, 0, SLJIT_IMM, 6);
2939    OP2(SLJIT_AND, TMP2, 0, TMP2, 0, SLJIT_IMM, 0x3f);
2940    OP2(SLJIT_OR, TMP1, 0, TMP1, 0, TMP2, 0);
2941    
2942    OP2(SLJIT_AND | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x10000);
2943    jump = JUMP(SLJIT_NOT_ZERO);
2944    /* Three byte sequence. */
2945    OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(2));
2946    OP1(SLJIT_MOV, TMP2, 0, SLJIT_IMM, IN_UCHARS(3));
2947    sljit_emit_fast_return(compiler, RETURN_ADDR, 0);
2948    
2949    /* Four byte sequence. */
2950    JUMPHERE(jump);
2951    OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(2));
2952    OP2(SLJIT_XOR, TMP1, 0, TMP1, 0, SLJIT_IMM, 0x10000);
2953    OP2(SLJIT_SHL, TMP1, 0, TMP1, 0, SLJIT_IMM, 6);
2954    OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(3));
2955    OP2(SLJIT_AND, TMP2, 0, TMP2, 0, SLJIT_IMM, 0x3f);
2956    OP2(SLJIT_OR, TMP1, 0, TMP1, 0, TMP2, 0);
2957    OP1(SLJIT_MOV, TMP2, 0, SLJIT_IMM, IN_UCHARS(4));
2958    sljit_emit_fast_return(compiler, RETURN_ADDR, 0);
2959    }
2960    
2961    static void do_utfreadchar16(compiler_common *common)
2962    {
2963    /* Fast decoding a UTF-8 character. TMP1 contains the first byte
2964    of the character (>= 0xc0). Return value in TMP1. */
2965    DEFINE_COMPILER;
2966    struct sljit_jump *jump;
2967    
2968    sljit_emit_fast_enter(compiler, RETURN_ADDR, 0);
2969    OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(0));
2970    OP2(SLJIT_AND, TMP1, 0, TMP1, 0, SLJIT_IMM, 0x3f);
2971    OP2(SLJIT_SHL, TMP1, 0, TMP1, 0, SLJIT_IMM, 6);
2972    OP2(SLJIT_AND, TMP2, 0, TMP2, 0, SLJIT_IMM, 0x3f);
2973    OP2(SLJIT_OR, TMP1, 0, TMP1, 0, TMP2, 0);
2974    
2975    /* Searching for the first zero. */
2976    OP2(SLJIT_AND | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x800);
2977    jump = JUMP(SLJIT_NOT_ZERO);
2978    /* Two byte sequence. */
2979    OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
2980    sljit_emit_fast_return(compiler, RETURN_ADDR, 0);
2981    
2982    JUMPHERE(jump);
2983    OP2(SLJIT_AND | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x400);
2984    OP_FLAGS(SLJIT_MOV, TMP2, 0, SLJIT_UNUSED, 0, SLJIT_NOT_ZERO);
2985    /* This code runs only in 8 bit mode. No need to shift the value. */
2986    OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, TMP2, 0);
2987    OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(1));
2988    OP2(SLJIT_XOR, TMP1, 0, TMP1, 0, SLJIT_IMM, 0x800);
2989    OP2(SLJIT_SHL, TMP1, 0, TMP1, 0, SLJIT_IMM, 6);
2990    OP2(SLJIT_AND, TMP2, 0, TMP2, 0, SLJIT_IMM, 0x3f);
2991    OP2(SLJIT_OR, TMP1, 0, TMP1, 0, TMP2, 0);
2992    /* Three byte sequence. */
2993    OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(2));
2994    sljit_emit_fast_return(compiler, RETURN_ADDR, 0);
2995    }
2996    
2997    static void do_utfreadtype8(compiler_common *common)
2998    {
2999    /* Fast decoding a UTF-8 character type. TMP2 contains the first byte
3000    of the character (>= 0xc0). Return value in TMP1. */
3001    DEFINE_COMPILER;
3002    struct sljit_jump *jump;
3003    struct sljit_jump *compare;
3004    
3005    sljit_emit_fast_enter(compiler, RETURN_ADDR, 0);
3006    
3007    OP2(SLJIT_AND | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP2, 0, SLJIT_IMM, 0x20);
3008    jump = JUMP(SLJIT_NOT_ZERO);
3009    /* Two byte sequence. */
3010    OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(0));
3011    OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
3012    OP2(SLJIT_AND, TMP2, 0, TMP2, 0, SLJIT_IMM, 0x1f);
3013    /* The upper 5 bits are known at this point. */
3014    compare = CMP(SLJIT_GREATER, TMP2, 0, SLJIT_IMM, 0x3);
3015    OP2(SLJIT_SHL, TMP2, 0, TMP2, 0, SLJIT_IMM, 6);
3016    OP2(SLJIT_AND, TMP1, 0, TMP1, 0, SLJIT_IMM, 0x3f);
3017    OP2(SLJIT_OR, TMP2, 0, TMP2, 0, TMP1, 0);
3018    OP1(SLJIT_MOV_UB, TMP1, 0, SLJIT_MEM1(TMP2), common->ctypes);
3019    sljit_emit_fast_return(compiler, RETURN_ADDR, 0);
3020    
3021    JUMPHERE(compare);
3022    OP1(SLJIT_MOV, TMP1, 0, SLJIT_IMM, 0);
3023    sljit_emit_fast_return(compiler, RETURN_ADDR, 0);
3024    
3025    /* We only have types for characters less than 256. */
3026    JUMPHERE(jump);
3027    OP1(SLJIT_MOV_UB, TMP2, 0, SLJIT_MEM1(TMP2), (sljit_sw)PRIV(utf8_table4) - 0xc0);
3028    OP1(SLJIT_MOV, TMP1, 0, SLJIT_IMM, 0);
3029    OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, TMP2, 0);
3030    sljit_emit_fast_return(compiler, RETURN_ADDR, 0);
3031    }
3032    
3033    #endif /* COMPILE_PCRE8 */
3034    
3035    #endif /* SUPPORT_UTF */
3036    
3037    #ifdef SUPPORT_UCP
3038    
3039    /* UCD_BLOCK_SIZE must be 128 (see the assert below). */
3040    #define UCD_BLOCK_MASK 127
3041    #define UCD_BLOCK_SHIFT 7
3042    
3043    static void do_getucd(compiler_common *common)
3044    {
3045    /* Search the UCD record for the character comes in TMP1.
3046    Returns chartype in TMP1 and UCD offset in TMP2. */
3047    DEFINE_COMPILER;
3048    
3049    SLJIT_ASSERT(UCD_BLOCK_SIZE == 128 && sizeof(ucd_record) == 8);
3050    
3051    sljit_emit_fast_enter(compiler, RETURN_ADDR, 0);
3052    OP2(SLJIT_LSHR, TMP2, 0, TMP1, 0, SLJIT_IMM, UCD_BLOCK_SHIFT);
3053    OP1(SLJIT_MOV_UB, TMP2, 0, SLJIT_MEM1(TMP2), (sljit_sw)PRIV(ucd_stage1));
3054    OP2(SLJIT_AND, TMP1, 0, TMP1, 0, SLJIT_IMM, UCD_BLOCK_MASK);
3055    OP2(SLJIT_SHL, TMP2, 0, TMP2, 0, SLJIT_IMM, UCD_BLOCK_SHIFT);
3056    OP2(SLJIT_ADD, TMP1, 0, TMP1, 0, TMP2, 0);
3057    OP1(SLJIT_MOV, TMP2, 0, SLJIT_IMM, (sljit_sw)PRIV(ucd_stage2));
3058    OP1(SLJIT_MOV_UH, TMP2, 0, SLJIT_MEM2(TMP2, TMP1), 1);
3059    OP1(SLJIT_MOV, TMP1, 0, SLJIT_IMM, (sljit_sw)PRIV(ucd_records) + SLJIT_OFFSETOF(ucd_record, chartype));
3060    OP1(SLJIT_MOV_UB, TMP1, 0, SLJIT_MEM2(TMP1, TMP2), 3);
3061    sljit_emit_fast_return(compiler, RETURN_ADDR, 0);
3062    }
3063    #endif
3064    
3065    static SLJIT_INLINE struct sljit_label *mainloop_entry(compiler_common *common, BOOL hascrorlf, BOOL firstline)
3066    {
3067    DEFINE_COMPILER;
3068    struct sljit_label *mainloop;
3069    struct sljit_label *newlinelabel = NULL;
3070    struct sljit_jump *start;
3071    struct sljit_jump *end = NULL;
3072    struct sljit_jump *nl = NULL;
3073    #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
3074    struct sljit_jump *singlechar;
3075    #endif
3076    jump_list *newline = NULL;
3077    BOOL newlinecheck = FALSE;
3078    BOOL readuchar = FALSE;
3079    
3080  if (save)  if (!(hascrorlf || firstline) && (common->nltype == NLTYPE_ANY ||
3081        common->nltype == NLTYPE_ANYCRLF || common->newline > 255))
3082      newlinecheck = TRUE;
3083    
3084    if (firstline)
3085    {    {
3086    if (tmp1next)    /* Search for the end of the first line. */
3087      SLJIT_ASSERT(common->first_line_end != 0);
3088      OP1(SLJIT_MOV, TMP3, 0, STR_PTR, 0);
3089    
3090      if (common->nltype == NLTYPE_FIXED && common->newline > 255)
3091      {      {
3092      if (!tmp1empty)      mainloop = LABEL();
3093        {      OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
3094        OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackptr, TMP1, 0);      end = CMP(SLJIT_GREATER_EQUAL, STR_PTR, 0, STR_END, 0);
3095        stackptr += sizeof(sljit_w);      OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(-1));
3096        }      OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(0));
3097      if (!tmp2empty)      CMPTO(SLJIT_NOT_EQUAL, TMP1, 0, SLJIT_IMM, (common->newline >> 8) & 0xff, mainloop);
3098        {      CMPTO(SLJIT_NOT_EQUAL, TMP2, 0, SLJIT_IMM, common->newline & 0xff, mainloop);
3099        OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackptr, TMP2, 0);      JUMPHERE(end);
3100        stackptr += sizeof(sljit_w);      OP2(SLJIT_SUB, SLJIT_MEM1(SLJIT_SP), common->first_line_end, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
       }  
3101      }      }
3102    else    else
3103      {      {
3104      if (!tmp2empty)      end = CMP(SLJIT_GREATER_EQUAL, STR_PTR, 0, STR_END, 0);
3105        {      mainloop = LABEL();
3106        OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackptr, TMP2, 0);      /* Continual stores does not cause data dependency. */
3107        stackptr += sizeof(sljit_w);      OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->first_line_end, STR_PTR, 0);
3108        }      read_char_range(common, common->nlmin, common->nlmax, TRUE);
3109      if (!tmp1empty)      check_newlinechar(common, common->nltype, &newline, TRUE);
3110        {      CMPTO(SLJIT_LESS, STR_PTR, 0, STR_END, 0, mainloop);
3111        OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackptr, TMP1, 0);      JUMPHERE(end);
3112        stackptr += sizeof(sljit_w);      OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->first_line_end, STR_PTR, 0);
3113        }      set_jumps(newline, LABEL());
3114      }      }
3115    
3116      OP1(SLJIT_MOV, STR_PTR, 0, TMP3, 0);
3117    }    }
 SLJIT_ASSERT(cc == ccend && stackptr == stacktop && (save || (tmp1empty && tmp2empty)));  
 }  
3118    
3119  static SLJIT_INLINE BOOL ispowerof2(unsigned int value)  start = JUMP(SLJIT_JUMP);
 {  
 return (value & (value - 1)) == 0;  
 }  
3120    
3121  static SLJIT_INLINE void set_jumps(jump_list *list, struct sljit_label *label)  if (newlinecheck)
 {  
 while (list)  
3122    {    {
3123    /* sljit_set_label is clever enough to do nothing    newlinelabel = LABEL();
3124    if either the jump or the label is NULL */    OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
3125    sljit_set_label(list->jump, label);    end = CMP(SLJIT_GREATER_EQUAL, STR_PTR, 0, STR_END, 0);
3126    list = list->next;    OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), 0);
3127      OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, common->newline & 0xff);
3128      OP_FLAGS(SLJIT_MOV, TMP1, 0, SLJIT_UNUSED, 0, SLJIT_EQUAL);
3129    #if defined COMPILE_PCRE16 || defined COMPILE_PCRE32
3130      OP2(SLJIT_SHL, TMP1, 0, TMP1, 0, SLJIT_IMM, UCHAR_SHIFT);
3131    #endif
3132      OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, TMP1, 0);
3133      nl = JUMP(SLJIT_JUMP);
3134    }    }
 }  
3135    
3136  static SLJIT_INLINE void add_jump(struct sljit_compiler *compiler, jump_list **list, struct sljit_jump* jump)  mainloop = LABEL();
3137  {  
3138  jump_list *list_item = sljit_alloc_memory(compiler, sizeof(jump_list));  /* Increasing the STR_PTR here requires one less jump in the most common case. */
3139  if (list_item)  #ifdef SUPPORT_UTF
3140    if (common->utf) readuchar = TRUE;
3141    #endif
3142    if (newlinecheck) readuchar = TRUE;
3143    
3144    if (readuchar)
3145      OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), 0);
3146    
3147    if (newlinecheck)
3148      CMPTO(SLJIT_EQUAL, TMP1, 0, SLJIT_IMM, (common->newline >> 8) & 0xff, newlinelabel);
3149    
3150    OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
3151    #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
3152    #if defined COMPILE_PCRE8
3153    if (common->utf)
3154    {    {
3155    list_item->next = *list;    singlechar = CMP(SLJIT_LESS, TMP1, 0, SLJIT_IMM, 0xc0);
3156    list_item->jump = jump;    OP1(SLJIT_MOV_UB, TMP1, 0, SLJIT_MEM1(TMP1), (sljit_sw)PRIV(utf8_table4) - 0xc0);
3157    *list = list_item;    OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, TMP1, 0);
3158      JUMPHERE(singlechar);
3159      }
3160    #elif defined COMPILE_PCRE16
3161    if (common->utf)
3162      {
3163      singlechar = CMP(SLJIT_LESS, TMP1, 0, SLJIT_IMM, 0xd800);
3164      OP2(SLJIT_AND, TMP1, 0, TMP1, 0, SLJIT_IMM, 0xfc00);
3165      OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0xd800);
3166      OP_FLAGS(SLJIT_MOV, TMP1, 0, SLJIT_UNUSED, 0, SLJIT_EQUAL);
3167      OP2(SLJIT_SHL, TMP1, 0, TMP1, 0, SLJIT_IMM, 1);
3168      OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, TMP1, 0);
3169      JUMPHERE(singlechar);
3170      }
3171    #endif /* COMPILE_PCRE[8|16] */
3172    #endif /* SUPPORT_UTF && !COMPILE_PCRE32 */
3173    JUMPHERE(start);
3174    
3175    if (newlinecheck)
3176      {
3177      JUMPHERE(end);
3178      JUMPHERE(nl);
3179    }    }
3180    
3181    return mainloop;
3182  }  }
3183    
3184  static void add_stub(compiler_common *common, enum stub_types type, int data, struct sljit_jump *start)  #define MAX_N_CHARS 16
3185    #define MAX_N_BYTES 8
3186    
3187    static SLJIT_INLINE void add_prefix_byte(pcre_uint8 byte, pcre_uint8 *bytes)
3188  {  {
3189  DEFINE_COMPILER;  pcre_uint8 len = bytes[0];
3190  stub_list* list_item = sljit_alloc_memory(compiler, sizeof(stub_list));  int i;
3191    
3192  if (list_item)  if (len == 255)
3193      return;
3194    
3195    if (len == 0)
3196    {    {
3197    list_item->type = type;    bytes[0] = 1;
3198    list_item->data = data;    bytes[1] = byte;
3199    list_item->start = start;    return;
   list_item->leave = LABEL();  
   list_item->next = common->stubs;  
   common->stubs = list_item;  
3200    }    }
 }  
3201    
3202  static void flush_stubs(compiler_common *common)  for (i = len; i > 0; i--)
3203  {    if (bytes[i] == byte)
3204  DEFINE_COMPILER;      return;
 stub_list* list_item = common->stubs;  
3205    
3206  while (list_item)  if (len >= MAX_N_BYTES - 1)
3207    {    {
3208    JUMPHERE(list_item->start);    bytes[0] = 255;
3209    switch(list_item->type)    return;
3210      }
3211    
3212    len++;
3213    bytes[len] = byte;
3214    bytes[0] = len;
3215    }
3216    
3217    static int scan_prefix(compiler_common *common, pcre_uchar *cc, pcre_uint32 *chars, pcre_uint8 *bytes, int max_chars)
3218    {
3219    /* Recursive function, which scans prefix literals. */
3220    BOOL last, any, caseless;
3221    int len, repeat, len_save, consumed = 0;
3222    pcre_uint32 chr, mask;
3223    pcre_uchar *alternative, *cc_save, *oc;
3224    #if defined SUPPORT_UTF && defined COMPILE_PCRE8
3225    pcre_uchar othercase[8];
3226    #elif defined SUPPORT_UTF && defined COMPILE_PCRE16
3227    pcre_uchar othercase[2];
3228    #else
3229    pcre_uchar othercase[1];
3230    #endif
3231    
3232    repeat = 1;
3233    while (TRUE)
3234      {
3235      last = TRUE;
3236      any = FALSE;
3237      caseless = FALSE;
3238      switch (*cc)
3239      {      {
3240      case stack_alloc:      case OP_CHARI:
3241      add_jump(compiler, &common->stackalloc, JUMP(SLJIT_FAST_CALL));      caseless = TRUE;
3242        case OP_CHAR:
3243        last = FALSE;
3244        cc++;
3245      break;      break;
     }  
   JUMPTO(SLJIT_JUMP, list_item->leave);  
   list_item = list_item->next;  
   }  
 common->stubs = NULL;  
 }  
3246    
3247  static SLJIT_INLINE void decrease_call_count(compiler_common *common)      case OP_SOD:
3248  {      case OP_SOM:
3249  DEFINE_COMPILER;      case OP_SET_SOM:
3250        case OP_NOT_WORD_BOUNDARY:
3251        case OP_WORD_BOUNDARY:
3252        case OP_EODN:
3253        case OP_EOD:
3254        case OP_CIRC:
3255        case OP_CIRCM:
3256        case OP_DOLL:
3257        case OP_DOLLM:
3258        /* Zero width assertions. */
3259        cc++;
3260        continue;
3261    
3262  OP2(SLJIT_SUB | SLJIT_SET_E, CALL_COUNT, 0, CALL_COUNT, 0, SLJIT_IMM, 1);      case OP_ASSERT:
3263  add_jump(compiler, &common->calllimit, JUMP(SLJIT_C_ZERO));      case OP_ASSERT_NOT:
3264  }      case OP_ASSERTBACK:
3265        case OP_ASSERTBACK_NOT:
3266        cc = bracketend(cc);
3267        continue;
3268    
3269  static SLJIT_INLINE void allocate_stack(compiler_common *common, int size)      case OP_PLUSI:
3270  {      case OP_MINPLUSI:
3271  /* May destroy all locals and registers except TMP2. */      case OP_POSPLUSI:
3272  DEFINE_COMPILER;      caseless = TRUE;
3273        case OP_PLUS:
3274        case OP_MINPLUS:
3275        case OP_POSPLUS:
3276        cc++;
3277        break;
3278    
3279  OP2(SLJIT_ADD, STACK_TOP, 0, STACK_TOP, 0, SLJIT_IMM, size * sizeof(sljit_w));      case OP_EXACTI:
3280  #ifdef DESTROY_REGISTERS      caseless = TRUE;
3281  OP1(SLJIT_MOV, TMP1, 0, SLJIT_IMM, 12345);      case OP_EXACT:
3282  OP1(SLJIT_MOV, TMP3, 0, TMP1, 0);      repeat = GET2(cc, 1);
3283  OP1(SLJIT_MOV, RETURN_ADDR, 0, TMP1, 0);      last = FALSE;
3284  OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), LOCALS0, TMP1, 0);      cc += 1 + IMM2_SIZE;
3285  OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), LOCALS1, TMP1, 0);      break;
3286    
3287        case OP_QUERYI:
3288        case OP_MINQUERYI:
3289        case OP_POSQUERYI:
3290        caseless = TRUE;
3291        case OP_QUERY:
3292        case OP_MINQUERY:
3293        case OP_POSQUERY:
3294        len = 1;
3295        cc++;
3296    #ifdef SUPPORT_UTF
3297        if (common->utf && HAS_EXTRALEN(*cc)) len += GET_EXTRALEN(*cc);
3298  #endif  #endif
3299  add_stub(common, stack_alloc, 0, CMP(SLJIT_C_GREATER, STACK_TOP, 0, STACK_LIMIT, 0));      max_chars = scan_prefix(common, cc + len, chars, bytes, max_chars);
3300  }      if (max_chars == 0)
3301          return consumed;
3302        last = FALSE;
3303        break;
3304    
3305  static SLJIT_INLINE void free_stack(compiler_common *common, int size)      case OP_KET:
3306  {      cc += 1 + LINK_SIZE;
3307  DEFINE_COMPILER;      continue;
 OP2(SLJIT_SUB, STACK_TOP, 0, STACK_TOP, 0, SLJIT_IMM, size * sizeof(sljit_w));  
 }  
3308    
3309  static SLJIT_INLINE void reset_ovector(compiler_common *common, int length)      case OP_ALT:
3310  {      cc += GET(cc, 1);
3311  DEFINE_COMPILER;      continue;
 struct sljit_label *loop;  
 int i;  
 /* At this point we can freely use all temporary registers. */  
 /* TMP1 returns with begin - 1. */  
 OP2(SLJIT_SUB, SLJIT_TEMPORARY_REG1, 0, SLJIT_MEM1(SLJIT_GENERAL_REG1), SLJIT_OFFSETOF(jit_arguments, begin), SLJIT_IMM, 1);  
 if (length < 8)  
   {  
   for (i = 0; i < length; i++)  
     OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(i), SLJIT_TEMPORARY_REG1, 0);  
   }  
 else  
   {  
   OP2(SLJIT_ADD, SLJIT_TEMPORARY_REG2, 0, SLJIT_LOCALS_REG, 0, SLJIT_IMM, OVECTOR_START - sizeof(sljit_w));  
   OP1(SLJIT_MOV, SLJIT_TEMPORARY_REG3, 0, SLJIT_IMM, length);  
   loop = LABEL();  
   OP1(SLJIT_MOVU, SLJIT_MEM1(SLJIT_TEMPORARY_REG2), sizeof(sljit_w), SLJIT_TEMPORARY_REG1, 0);  
   OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_TEMPORARY_REG3, 0, SLJIT_TEMPORARY_REG3, 0, SLJIT_IMM, 1);  
   JUMPTO(SLJIT_C_NOT_ZERO, loop);  
   }  
 }  
3312    
3313  static SLJIT_INLINE void copy_ovector(compiler_common *common, int topbracket)      case OP_ONCE:
3314  {      case OP_ONCE_NC:
3315  DEFINE_COMPILER;      case OP_BRA:
3316  struct sljit_label *loop;      case OP_BRAPOS:
3317  struct sljit_jump *earlyexit;      case OP_CBRA:
3318        case OP_CBRAPOS:
3319        alternative = cc + GET(cc, 1);
3320        while (*alternative == OP_ALT)
3321          {
3322          max_chars = scan_prefix(common, alternative + 1 + LINK_SIZE, chars, bytes, max_chars);
3323          if (max_chars == 0)
3324            return consumed;
3325          alternative += GET(alternative, 1);
3326          }
3327    
3328  /* At this point we can freely use all registers. */      if (*cc == OP_CBRA || *cc == OP_CBRAPOS)
3329  OP1(SLJIT_MOV, SLJIT_GENERAL_REG3, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(1));        cc += IMM2_SIZE;
3330  OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(1), STR_PTR, 0);      cc += 1 + LINK_SIZE;
3331        continue;
3332    
3333  OP1(SLJIT_MOV, SLJIT_TEMPORARY_REG1, 0, ARGUMENTS, 0);      case OP_CLASS:
3334  OP1(SLJIT_MOV_SI, SLJIT_TEMPORARY_REG2, 0, SLJIT_MEM1(SLJIT_TEMPORARY_REG1), SLJIT_OFFSETOF(jit_arguments, offsetcount));  #if defined SUPPORT_UTF && defined COMPILE_PCRE8
3335  OP2(SLJIT_SUB, SLJIT_TEMPORARY_REG3, 0, SLJIT_MEM1(SLJIT_TEMPORARY_REG1), SLJIT_OFFSETOF(jit_arguments, offsets), SLJIT_IMM, sizeof(int));      if (common->utf && !is_char7_bitset((const pcre_uint8 *)(cc + 1), FALSE)) return consumed;
3336  OP1(SLJIT_MOV, SLJIT_TEMPORARY_REG1, 0, SLJIT_MEM1(SLJIT_TEMPORARY_REG1), SLJIT_OFFSETOF(jit_arguments, begin));  #endif
3337  OP2(SLJIT_ADD, SLJIT_GENERAL_REG1, 0, SLJIT_LOCALS_REG, 0, SLJIT_IMM, OVECTOR_START);      any = TRUE;
3338  /* Unlikely, but possible */      cc += 1 + 32 / sizeof(pcre_uchar);
3339  earlyexit = CMP(SLJIT_C_EQUAL, SLJIT_TEMPORARY_REG2, 0, SLJIT_IMM, 0);      break;
 loop = LABEL();  
 OP2(SLJIT_SUB, SLJIT_GENERAL_REG2, 0, SLJIT_MEM1(SLJIT_GENERAL_REG1), 0, SLJIT_TEMPORARY_REG1, 0);  
 OP2(SLJIT_ADD, SLJIT_GENERAL_REG1, 0, SLJIT_GENERAL_REG1, 0, SLJIT_IMM, sizeof(sljit_w));  
 /* Copy the integer value to the output buffer */  
 OP1(SLJIT_MOVU_SI, SLJIT_MEM1(SLJIT_TEMPORARY_REG3), sizeof(int), SLJIT_GENERAL_REG2, 0);  
 OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_TEMPORARY_REG2, 0, SLJIT_TEMPORARY_REG2, 0, SLJIT_IMM, 1);  
 JUMPTO(SLJIT_C_NOT_ZERO, loop);  
 JUMPHERE(earlyexit);  
3340    
3341  /* Calculate the return value, which is the maximum ovector value. */      case OP_NCLASS:
3342  if (topbracket > 1)  #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
3343    {      if (common->utf) return consumed;
3344    OP2(SLJIT_ADD, SLJIT_TEMPORARY_REG1, 0, SLJIT_LOCALS_REG, 0, SLJIT_IMM, OVECTOR_START + topbracket * 2 * sizeof(sljit_w));  #endif
3345    OP1(SLJIT_MOV, SLJIT_TEMPORARY_REG2, 0, SLJIT_IMM, topbracket + 1);      any = TRUE;
3346        cc += 1 + 32 / sizeof(pcre_uchar);
3347        break;
3348    
3349    /* OVECTOR(0) is never equal to SLJIT_GENERAL_REG3. */  #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
3350    loop = LABEL();      case OP_XCLASS:
3351    OP1(SLJIT_MOVU, SLJIT_TEMPORARY_REG3, 0, SLJIT_MEM1(SLJIT_TEMPORARY_REG1), -(2 * (sljit_w)sizeof(sljit_w)));  #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
3352    OP2(SLJIT_SUB, SLJIT_TEMPORARY_REG2, 0, SLJIT_TEMPORARY_REG2, 0, SLJIT_IMM, 1);      if (common->utf) return consumed;
3353    CMPTO(SLJIT_C_EQUAL, SLJIT_TEMPORARY_REG3, 0, SLJIT_GENERAL_REG3, 0, loop);  #endif
3354    OP1(SLJIT_MOV, SLJIT_RETURN_REG, 0, SLJIT_TEMPORARY_REG2, 0);      any = TRUE;
3355    }      cc += GET(cc, 1);
3356  else      break;
3357    OP1(SLJIT_MOV, SLJIT_RETURN_REG, 0, SLJIT_IMM, 1);  #endif
 }  
3358    
3359  static SLJIT_INLINE BOOL char_has_othercase(compiler_common *common, uschar* cc)      case OP_DIGIT:
3360  {  #if defined SUPPORT_UTF && defined COMPILE_PCRE8
3361  /* Detects if the character has an othercase. */      if (common->utf && !is_char7_bitset((const pcre_uint8 *)common->ctypes - cbit_length + cbit_digit, FALSE))
3362  unsigned int c;        return consumed;
3363    #endif
3364        any = TRUE;
3365        cc++;
3366        break;
3367    
3368  #ifdef SUPPORT_UTF8      case OP_WHITESPACE:
3369  if (common->utf8)  #if defined SUPPORT_UTF && defined COMPILE_PCRE8
3370    {      if (common->utf && !is_char7_bitset((const pcre_uint8 *)common->ctypes - cbit_length + cbit_space, FALSE))
3371    GETCHAR(c, cc);        return consumed;
   if (c > 127)  
     {  
 #ifdef SUPPORT_UCP  
     return c != UCD_OTHERCASE(c);  
 #else  
     return FALSE;  
3372  #endif  #endif
3373      }      any = TRUE;
3374    }      cc++;
3375  else      break;
3376    
3377        case OP_WORDCHAR:
3378    #if defined SUPPORT_UTF && defined COMPILE_PCRE8
3379        if (common->utf && !is_char7_bitset((const pcre_uint8 *)common->ctypes - cbit_length + cbit_word, FALSE))
3380          return consumed;
3381  #endif  #endif
3382    c = *cc;      any = TRUE;
3383  return common->fcc[c] != c;      cc++;
3384  }      break;
3385    
3386        case OP_NOT:
3387        case OP_NOTI:
3388        cc++;
3389        /* Fall through. */
3390        case OP_NOT_DIGIT:
3391        case OP_NOT_WHITESPACE:
3392        case OP_NOT_WORDCHAR:
3393        case OP_ANY:
3394        case OP_ALLANY:
3395    #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
3396        if (common->utf) return consumed;
3397    #endif
3398        any = TRUE;
3399        cc++;
3400        break;
3401    
 static SLJIT_INLINE unsigned int char_othercase(compiler_common *common, unsigned int c)  
 {  
 /* Returns with the othercase. */  
 #ifdef SUPPORT_UTF8  
 if (common->utf8 && c > 127)  
   {  
3402  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
3403    return UCD_OTHERCASE(c);      case OP_NOTPROP:
3404  #else      case OP_PROP:
3405    return c;  #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
3406        if (common->utf) return consumed;
3407  #endif  #endif
3408    }      any = TRUE;
3409        cc += 1 + 2;
3410        break;
3411  #endif  #endif
 return common->fcc[c];  
 }  
3412    
3413  static unsigned int char_get_othercase_bit(compiler_common *common, uschar* cc)      case OP_TYPEEXACT:
3414  {      repeat = GET2(cc, 1);
3415  /* Detects if the character and its othercase has only 1 bit difference. */      cc += 1 + IMM2_SIZE;
3416  unsigned int c, oc, bit;      continue;
 #ifdef SUPPORT_UTF8  
 int n;  
 #endif  
3417    
3418  #ifdef SUPPORT_UTF8      case OP_NOTEXACT:
3419  if (common->utf8)      case OP_NOTEXACTI:
3420    {  #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
3421    GETCHAR(c, cc);      if (common->utf) return consumed;
   if (c <= 127)  
     oc = common->fcc[c];  
   else  
     {  
 #ifdef SUPPORT_UCP  
     oc = UCD_OTHERCASE(c);  
 #else  
     oc = c;  
3422  #endif  #endif
3423        any = TRUE;
3424        repeat = GET2(cc, 1);
3425        cc += 1 + IMM2_SIZE + 1;
3426        break;
3427    
3428        default:
3429        return consumed;
3430      }      }
3431    }  
3432  else    if (any)
3433    {      {
3434    c = *cc;  #if defined COMPILE_PCRE8
3435    oc = common->fcc[c];      mask = 0xff;
3436    }  #elif defined COMPILE_PCRE16
3437        mask = 0xffff;
3438    #elif defined COMPILE_PCRE32
3439        mask = 0xffffffff;
3440  #else  #else
3441  c = *cc;      SLJIT_ASSERT_STOP();
 oc = common->fcc[c];  
3442  #endif  #endif
3443    
3444  SLJIT_ASSERT(c != oc);      do
3445          {
3446  bit = c ^ oc;        chars[0] = mask;
3447  /* Optimized for English alphabet. */        chars[1] = mask;
3448  if (c <= 127 && bit == 0x20)        bytes[0] = 255;
   return (0 << 8) | 0x20;  
3449    
3450  /* Since c != oc, they must have at least 1 bit difference. */        consumed++;
3451  if (!ispowerof2(bit))        if (--max_chars == 0)
3452    return 0;          return consumed;
3453          chars += 2;
3454          bytes += MAX_N_BYTES;
3455          }
3456        while (--repeat > 0);
3457    
3458  #ifdef SUPPORT_UTF8      repeat = 1;
3459  if (common->utf8 && c > 127)      continue;
   {  
   n = _pcre_utf8_table4[*cc & 0x3f];  
   while ((bit & 0x3f) == 0)  
     {  
     n--;  
     bit >>= 6;  
3460      }      }
   return (n << 8) | bit;  
   }  
 #endif  
 return (0 << 8) | bit;  
 }  
   
 static SLJIT_INLINE void check_input_end(compiler_common *common, jump_list **fallbacks)  
 {  
 DEFINE_COMPILER;  
 add_jump(compiler, fallbacks, CMP(SLJIT_C_GREATER_EQUAL, STR_PTR, 0, STR_END, 0));  
 }  
3461