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

Diff of /code/trunk/pcre_jit_compile.c

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

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