/[pcre]/code/tags/pcre-8.37/pcre_jit_compile.c
ViewVC logotype

Diff of /code/tags/pcre-8.37/pcre_jit_compile.c

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

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