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