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

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

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

revision 999 by zherczeg, Mon Aug 6 07:36:49 2012 UTC revision 1422 by zherczeg, Mon Dec 30 19:05:36 2013 UTC
# Line 6  Line 6 
6  and semantics are as close as possible to those of the Perl 5 language.  and semantics are as close as possible to those of the Perl 5 language.
7    
8                         Written by Philip Hazel                         Written by Philip Hazel
9             Copyright (c) 1997-2012 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-2012                        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
# Line 65  system files. */ Line 65  system files. */
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 134  The generated code will be the following Line 146  The generated code will be the following
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 backtrack 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 {
# Line 154  typedef struct jit_arguments { Line 166  typedef struct jit_arguments {
166    int *offsets;    int *offsets;
167    pcre_uchar *uchar_ptr;    pcre_uchar *uchar_ptr;
168    pcre_uchar *mark_ptr;    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      int offset_count;
174    pcre_uint8 notbol;    pcre_uint8 notbol;
175    pcre_uint8 noteol;    pcre_uint8 noteol;
176    pcre_uint8 notempty;    pcre_uint8 notempty;
# Line 167  typedef struct executable_functions { Line 181  typedef struct executable_functions {
181    void *executable_funcs[JIT_NUMBER_OF_COMPILE_MODES];    void *executable_funcs[JIT_NUMBER_OF_COMPILE_MODES];
182    PUBL(jit_callback) callback;    PUBL(jit_callback) callback;
183    void *userdata;    void *userdata;
184      pcre_uint32 top_bracket;
185      pcre_uint32 limit_match;
186    sljit_uw executable_sizes[JIT_NUMBER_OF_COMPILE_MODES];    sljit_uw executable_sizes[JIT_NUMBER_OF_COMPILE_MODES];
187  } executable_functions;  } executable_functions;
188    
# Line 175  typedef struct jump_list { Line 191  typedef struct jump_list {
191    struct jump_list *next;    struct jump_list *next;
192  } jump_list;  } jump_list;
193    
 enum stub_types { stack_alloc };  
   
194  typedef struct stub_list {  typedef struct stub_list {
   enum stub_types type;  
   int data;  
195    struct sljit_jump *start;    struct sljit_jump *start;
196    struct sljit_label *quit;    struct sljit_label *quit;
197    struct stub_list *next;    struct stub_list *next;
198  } stub_list;  } stub_list;
199    
200    enum frame_types {
201      no_frame = -1,
202      no_stack = -2
203    };
204    
205    enum control_types {
206      type_mark = 0,
207      type_then_trap = 1
208    };
209    
210  typedef int (SLJIT_CALL *jit_function)(jit_arguments *args);  typedef int (SLJIT_CALL *jit_function)(jit_arguments *args);
211    
212  /* The following structure is the key data type for the recursive  /* The following structure is the key data type for the recursive
213  code generator. It is allocated by compile_matchingpath, and contains  code generator. It is allocated by compile_matchingpath, and contains
214  the aguments for compile_backtrackingpath. Must be the first member  the arguments for compile_backtrackingpath. Must be the first member
215  of its descendants. */  of its descendants. */
216  typedef struct backtrack_common {  typedef struct backtrack_common {
217    /* Concatenation stack. */    /* Concatenation stack. */
# Line 205  typedef struct backtrack_common { Line 227  typedef struct backtrack_common {
227  typedef struct assert_backtrack {  typedef struct assert_backtrack {
228    backtrack_common common;    backtrack_common common;
229    jump_list *condfailed;    jump_list *condfailed;
230    /* Less than 0 (-1) if a frame is not needed. */    /* Less than 0 if a frame is not needed. */
231    int framesize;    int framesize;
232    /* Points to our private memory word on the stack. */    /* Points to our private memory word on the stack. */
233    int localptr;    int private_data_ptr;
234    /* For iterators. */    /* For iterators. */
235    struct sljit_label *matchingpath;    struct sljit_label *matchingpath;
236  } assert_backtrack;  } assert_backtrack;
# Line 226  typedef struct bracket_backtrack { Line 248  typedef struct bracket_backtrack {
248      /* Both for OP_COND, OP_SCOND. */      /* Both for OP_COND, OP_SCOND. */
249      jump_list *condfailed;      jump_list *condfailed;
250      assert_backtrack *assert;      assert_backtrack *assert;
251      /* For OP_ONCE. -1 if not needed. */      /* For OP_ONCE. Less than 0 if not needed. */
252      int framesize;      int framesize;
253    } u;    } u;
254    /* Points to our private memory word on the stack. */    /* Points to our private memory word on the stack. */
255    int localptr;    int private_data_ptr;
256  } bracket_backtrack;  } bracket_backtrack;
257    
258  typedef struct bracketpos_backtrack {  typedef struct bracketpos_backtrack {
259    backtrack_common common;    backtrack_common common;
260    /* Points to our private memory word on the stack. */    /* Points to our private memory word on the stack. */
261    int localptr;    int private_data_ptr;
262    /* Reverting stack is needed. */    /* Reverting stack is needed. */
263    int framesize;    int framesize;
264    /* Allocated stack size. */    /* Allocated stack size. */
# Line 261  typedef struct recurse_entry { Line 283  typedef struct recurse_entry {
283    /* Collects the calls until the function is not created. */    /* Collects the calls until the function is not created. */
284    jump_list *calls;    jump_list *calls;
285    /* Points to the starting opcode. */    /* Points to the starting opcode. */
286    int start;    sljit_sw start;
287  } recurse_entry;  } recurse_entry;
288    
289  typedef struct recurse_backtrack {  typedef struct recurse_backtrack {
290    backtrack_common common;    backtrack_common common;
291      BOOL inlined_pattern;
292  } recurse_backtrack;  } recurse_backtrack;
293    
294  #define MAX_RANGE_SIZE 6  #define OP_THEN_TRAP OP_TABLE_LENGTH
295    
296    typedef struct then_trap_backtrack {
297      backtrack_common common;
298      /* If then_trap is not NULL, this structure contains the real
299      then_trap for the backtracking path. */
300      struct then_trap_backtrack *then_trap;
301      /* Points to the starting opcode. */
302      sljit_sw start;
303      /* Exit point for the then opcodes of this alternative. */
304      jump_list *quit;
305      /* Frame size of the current alternative. */
306      int framesize;
307    } then_trap_backtrack;
308    
309    #define MAX_RANGE_SIZE 4
310    
311  typedef struct compiler_common {  typedef struct compiler_common {
312      /* The sljit ceneric compiler. */
313    struct sljit_compiler *compiler;    struct sljit_compiler *compiler;
314      /* First byte code. */
315    pcre_uchar *start;    pcre_uchar *start;
316      /* Maps private data offset to each opcode. */
317    /* Opcode local area direct map. */    sljit_si *private_data_ptrs;
318    int *localptrs;    /* Tells whether the capturing bracket is optimized. */
319    int cbraptr;    pcre_uint8 *optimized_cbracket;
320    /* OVector starting point. Must be divisible by 2. */    /* Tells whether the starting offset is a target of then. */
321      pcre_uint8 *then_offsets;
322      /* Current position where a THEN must jump. */
323      then_trap_backtrack *then_trap;
324      /* Starting offset of private data for capturing brackets. */
325      int cbra_ptr;
326      /* Output vector starting point. Must be divisible by 2. */
327    int ovector_start;    int ovector_start;
328    /* Last known position of the requested byte. */    /* Last known position of the requested byte. */
329    int req_char_ptr;    int req_char_ptr;
330    /* Head of the last recursion. */    /* Head of the last recursion. */
331    int recursive_head;    int recursive_head_ptr;
332    /* First inspected character for partial matching. */    /* First inspected character for partial matching. */
333    int start_used_ptr;    int start_used_ptr;
334    /* Starting pointer for partial soft matches. */    /* Starting pointer for partial soft matches. */
# Line 291  typedef struct compiler_common { Line 337  typedef struct compiler_common {
337    int first_line_end;    int first_line_end;
338    /* Points to the marked string. */    /* Points to the marked string. */
339    int mark_ptr;    int mark_ptr;
340      /* Recursive control verb management chain. */
341      int control_head_ptr;
342      /* Points to the last matched capture block index. */
343      int capture_last_ptr;
344      /* Points to the starting position of the current match. */
345      int start_ptr;
346    
347    /* Flipped and lower case tables. */    /* Flipped and lower case tables. */
348    const pcre_uint8 *fcc;    const pcre_uint8 *fcc;
349    sljit_w lcc;    sljit_sw lcc;
350    /* Mode can be PCRE_STUDY_JIT_COMPILE and others. */    /* Mode can be PCRE_STUDY_JIT_COMPILE and others. */
351    int mode;    int mode;
352      /* \K is found in the pattern. */
353      BOOL has_set_som;
354      /* (*SKIP:arg) is found in the pattern. */
355      BOOL has_skip_arg;
356      /* (*THEN) is found in the pattern. */
357      BOOL has_then;
358      /* Needs to know the start position anytime. */
359      BOOL needs_start_ptr;
360      /* Currently in recurse or negative assert. */
361      BOOL local_exit;
362      /* Currently in a positive assert. */
363      BOOL positive_assert;
364    /* Newline control. */    /* Newline control. */
365    int nltype;    int nltype;
366    int newline;    int newline;
367    int bsr_nltype;    int bsr_nltype;
368    /* Dollar endonly. */    /* Dollar endonly. */
369    int endonly;    int endonly;
   BOOL has_set_som;  
370    /* Tables. */    /* Tables. */
371    sljit_w ctypes;    sljit_sw ctypes;
   int digits[2 + MAX_RANGE_SIZE];  
372    /* Named capturing brackets. */    /* Named capturing brackets. */
373    sljit_uw name_table;    pcre_uchar *name_table;
374    sljit_w name_count;    sljit_sw name_count;
375    sljit_w name_entry_size;    sljit_sw name_entry_size;
376    
377    /* Labels and jump lists. */    /* Labels and jump lists. */
378    struct sljit_label *partialmatchlabel;    struct sljit_label *partialmatchlabel;
379    struct sljit_label *quitlabel;    struct sljit_label *quit_label;
380    struct sljit_label *acceptlabel;    struct sljit_label *forced_quit_label;
381      struct sljit_label *accept_label;
382    stub_list *stubs;    stub_list *stubs;
383    recurse_entry *entries;    recurse_entry *entries;
384    recurse_entry *currententry;    recurse_entry *currententry;
385    jump_list *partialmatch;    jump_list *partialmatch;
386    jump_list *quit;    jump_list *quit;
387      jump_list *positive_assert_quit;
388      jump_list *forced_quit;
389    jump_list *accept;    jump_list *accept;
390    jump_list *calllimit;    jump_list *calllimit;
391    jump_list *stackalloc;    jump_list *stackalloc;
# Line 331  typedef struct compiler_common { Line 396  typedef struct compiler_common {
396    jump_list *vspace;    jump_list *vspace;
397    jump_list *casefulcmp;    jump_list *casefulcmp;
398    jump_list *caselesscmp;    jump_list *caselesscmp;
399      jump_list *reset_match;
400    BOOL jscript_compat;    BOOL jscript_compat;
401  #ifdef SUPPORT_UTF  #ifdef SUPPORT_UTF
402    BOOL utf;    BOOL utf;
403  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
404    BOOL use_ucp;    BOOL use_ucp;
405  #endif  #endif
   jump_list *utfreadchar;  
406  #ifdef COMPILE_PCRE8  #ifdef COMPILE_PCRE8
407      jump_list *utfreadchar;
408      jump_list *utfreadchar16;
409    jump_list *utfreadtype8;    jump_list *utfreadtype8;
410  #endif  #endif
411  #endif /* SUPPORT_UTF */  #endif /* SUPPORT_UTF */
# Line 355  typedef struct compare_context { Line 422  typedef struct compare_context {
422  #if defined SLJIT_UNALIGNED && SLJIT_UNALIGNED  #if defined SLJIT_UNALIGNED && SLJIT_UNALIGNED
423    int ucharptr;    int ucharptr;
424    union {    union {
425      sljit_i asint;      sljit_si asint;
426      sljit_uh asushort;      sljit_uh asushort;
427  #ifdef COMPILE_PCRE8  #if defined COMPILE_PCRE8
428      sljit_ub asbyte;      sljit_ub asbyte;
429      sljit_ub asuchars[4];      sljit_ub asuchars[4];
430  #else  #elif defined COMPILE_PCRE16
 #ifdef COMPILE_PCRE16  
431      sljit_uh asuchars[2];      sljit_uh asuchars[2];
432  #endif  #elif defined COMPILE_PCRE32
433        sljit_ui asuchars[1];
434  #endif  #endif
435    } c;    } c;
436    union {    union {
437      sljit_i asint;      sljit_si asint;
438      sljit_uh asushort;      sljit_uh asushort;
439  #ifdef COMPILE_PCRE8  #if defined COMPILE_PCRE8
440      sljit_ub asbyte;      sljit_ub asbyte;
441      sljit_ub asuchars[4];      sljit_ub asuchars[4];
442  #else  #elif defined COMPILE_PCRE16
 #ifdef COMPILE_PCRE16  
443      sljit_uh asuchars[2];      sljit_uh asuchars[2];
444  #endif  #elif defined COMPILE_PCRE32
445        sljit_ui asuchars[1];
446  #endif  #endif
447    } oc;    } oc;
448  #endif  #endif
449  } compare_context;  } compare_context;
450    
 enum {  
   frame_end = 0,  
   frame_setstrbegin = -1,  
   frame_setmark = -2  
 };  
   
451  /* Undefine sljit macros. */  /* Undefine sljit macros. */
452  #undef CMP  #undef CMP
453    
454  /* Used for accessing the elements of the stack. */  /* Used for accessing the elements of the stack. */
455  #define STACK(i)      ((-(i) - 1) * (int)sizeof(sljit_w))  #define STACK(i)      ((-(i) - 1) * (int)sizeof(sljit_sw))
456    
457  #define TMP1          SLJIT_TEMPORARY_REG1  #define TMP1          SLJIT_SCRATCH_REG1
458  #define TMP2          SLJIT_TEMPORARY_REG3  #define TMP2          SLJIT_SCRATCH_REG3
459  #define TMP3          SLJIT_TEMPORARY_EREG2  #define TMP3          SLJIT_TEMPORARY_EREG2
460  #define STR_PTR       SLJIT_SAVED_REG1  #define STR_PTR       SLJIT_SAVED_REG1
461  #define STR_END       SLJIT_SAVED_REG2  #define STR_END       SLJIT_SAVED_REG2
462  #define STACK_TOP     SLJIT_TEMPORARY_REG2  #define STACK_TOP     SLJIT_SCRATCH_REG2
463  #define STACK_LIMIT   SLJIT_SAVED_REG3  #define STACK_LIMIT   SLJIT_SAVED_REG3
464  #define ARGUMENTS     SLJIT_SAVED_EREG1  #define ARGUMENTS     SLJIT_SAVED_EREG1
465  #define CALL_COUNT    SLJIT_SAVED_EREG2  #define COUNT_MATCH   SLJIT_SAVED_EREG2
466  #define RETURN_ADDR   SLJIT_TEMPORARY_EREG1  #define RETURN_ADDR   SLJIT_TEMPORARY_EREG1
467    
468  /* Locals layout. */  /* Local space layout. */
469  /* These two locals can be used by the current opcode. */  /* These two locals can be used by the current opcode. */
470  #define LOCALS0          (0 * sizeof(sljit_w))  #define LOCALS0          (0 * sizeof(sljit_sw))
471  #define LOCALS1          (1 * sizeof(sljit_w))  #define LOCALS1          (1 * sizeof(sljit_sw))
472  /* Two local variables for possessive quantifiers (char1 cannot use them). */  /* Two local variables for possessive quantifiers (char1 cannot use them). */
473  #define POSSESSIVE0      (2 * sizeof(sljit_w))  #define POSSESSIVE0      (2 * sizeof(sljit_sw))
474  #define POSSESSIVE1      (3 * sizeof(sljit_w))  #define POSSESSIVE1      (3 * sizeof(sljit_sw))
475  /* Max limit of recursions. */  /* Max limit of recursions. */
476  #define CALL_LIMIT       (4 * sizeof(sljit_w))  #define LIMIT_MATCH      (4 * sizeof(sljit_sw))
477  /* The output vector is stored on the stack, and contains pointers  /* The output vector is stored on the stack, and contains pointers
478  to characters. The vector data is divided into two groups: the first  to characters. The vector data is divided into two groups: the first
479  group contains the start / end character pointers, and the second is  group contains the start / end character pointers, and the second is
480  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. */
481  #define OVECTOR_START    (common->ovector_start)  #define OVECTOR_START    (common->ovector_start)
482  #define OVECTOR(i)       (OVECTOR_START + (i) * sizeof(sljit_w))  #define OVECTOR(i)       (OVECTOR_START + (i) * (sljit_sw)sizeof(sljit_sw))
483  #define OVECTOR_PRIV(i)  (common->cbraptr + (i) * sizeof(sljit_w))  #define OVECTOR_PRIV(i)  (common->cbra_ptr + (i) * (sljit_sw)sizeof(sljit_sw))
484  #define PRIV_DATA(cc)    (common->localptrs[(cc) - common->start])  #define PRIVATE_DATA(cc) (common->private_data_ptrs[(cc) - common->start])
485    
486  #ifdef COMPILE_PCRE8  #if defined COMPILE_PCRE8
487  #define MOV_UCHAR  SLJIT_MOV_UB  #define MOV_UCHAR  SLJIT_MOV_UB
488  #define MOVU_UCHAR SLJIT_MOVU_UB  #define MOVU_UCHAR SLJIT_MOVU_UB
489  #else  #elif defined COMPILE_PCRE16
 #ifdef COMPILE_PCRE16  
490  #define MOV_UCHAR  SLJIT_MOV_UH  #define MOV_UCHAR  SLJIT_MOV_UH
491  #define MOVU_UCHAR SLJIT_MOVU_UH  #define MOVU_UCHAR SLJIT_MOVU_UH
492    #elif defined COMPILE_PCRE32
493    #define MOV_UCHAR  SLJIT_MOV_UI
494    #define MOVU_UCHAR SLJIT_MOVU_UI
495  #else  #else
496  #error Unsupported compiling mode  #error Unsupported compiling mode
497  #endif  #endif
 #endif  
498    
499  /* Shortcuts. */  /* Shortcuts. */
500  #define DEFINE_COMPILER \  #define DEFINE_COMPILER \
# Line 449  the start pointers when the end of the c Line 511  the start pointers when the end of the c
511    sljit_set_label(sljit_emit_jump(compiler, (type)), (label))    sljit_set_label(sljit_emit_jump(compiler, (type)), (label))
512  #define JUMPHERE(jump) \  #define JUMPHERE(jump) \
513    sljit_set_label((jump), sljit_emit_label(compiler))    sljit_set_label((jump), sljit_emit_label(compiler))
514    #define SET_LABEL(jump, label) \
515      sljit_set_label((jump), (label))
516  #define CMP(type, src1, src1w, src2, src2w) \  #define CMP(type, src1, src1w, src2, src2w) \
517    sljit_emit_cmp(compiler, (type), (src1), (src1w), (src2), (src2w))    sljit_emit_cmp(compiler, (type), (src1), (src1w), (src2), (src2w))
518  #define CMPTO(type, src1, src1w, src2, src2w, label) \  #define CMPTO(type, src1, src1w, src2, src2w, label) \
519    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))
520  #define COND_VALUE(op, dst, dstw, type) \  #define OP_FLAGS(op, dst, dstw, src, srcw, type) \
521    sljit_emit_cond_value(compiler, (op), (dst), (dstw), (type))    sljit_emit_op_flags(compiler, (op), (dst), (dstw), (src), (srcw), (type))
522  #define GET_LOCAL_BASE(dst, dstw, offset) \  #define GET_LOCAL_BASE(dst, dstw, offset) \
523    sljit_get_local_base(compiler, (dst), (dstw), (offset))    sljit_get_local_base(compiler, (dst), (dstw), (offset))
524    
# Line 467  cc += 1 + LINK_SIZE; Line 531  cc += 1 + LINK_SIZE;
531  return cc;  return cc;
532  }  }
533    
534    static int ones_in_half_byte[16] = {
535      /* 0 */ 0, 1, 1, 2, /* 4 */ 1, 2, 2, 3,
536      /* 8 */ 1, 2, 2, 3, /* 12 */ 2, 3, 3, 4
537    };
538    
539  /* Functions whose might need modification for all new supported opcodes:  /* Functions whose might need modification for all new supported opcodes:
540   next_opcode   next_opcode
541   get_localspace   check_opcode_types
542   set_localptrs   set_private_data_ptrs
543   get_framesize   get_framesize
544   init_frame   init_frame
545   get_localsize   get_private_data_copy_length
546   copy_locals   copy_private_data
547   compile_matchingpath   compile_matchingpath
548   compile_backtrackingpath   compile_backtrackingpath
549  */  */
# Line 497  switch(*cc) Line 566  switch(*cc)
566    case OP_WORDCHAR:    case OP_WORDCHAR:
567    case OP_ANY:    case OP_ANY:
568    case OP_ALLANY:    case OP_ALLANY:
569      case OP_NOTPROP:
570      case OP_PROP:
571    case OP_ANYNL:    case OP_ANYNL:
572    case OP_NOT_HSPACE:    case OP_NOT_HSPACE:
573    case OP_HSPACE:    case OP_HSPACE:
# Line 509  switch(*cc) Line 580  switch(*cc)
580    case OP_CIRCM:    case OP_CIRCM:
581    case OP_DOLL:    case OP_DOLL:
582    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:  
583    case OP_CRSTAR:    case OP_CRSTAR:
584    case OP_CRMINSTAR:    case OP_CRMINSTAR:
585    case OP_CRPLUS:    case OP_CRPLUS:
586    case OP_CRMINPLUS:    case OP_CRMINPLUS:
587    case OP_CRQUERY:    case OP_CRQUERY:
588    case OP_CRMINQUERY:    case OP_CRMINQUERY:
589      case OP_CRRANGE:
590      case OP_CRMINRANGE:
591      case OP_CRPOSSTAR:
592      case OP_CRPOSPLUS:
593      case OP_CRPOSQUERY:
594      case OP_CRPOSRANGE:
595      case OP_CLASS:
596      case OP_NCLASS:
597      case OP_REF:
598      case OP_REFI:
599      case OP_DNREF:
600      case OP_DNREFI:
601      case OP_RECURSE:
602      case OP_CALLOUT:
603      case OP_ALT:
604      case OP_KET:
605      case OP_KETRMAX:
606      case OP_KETRMIN:
607      case OP_KETRPOS:
608      case OP_REVERSE:
609      case OP_ASSERT:
610      case OP_ASSERT_NOT:
611      case OP_ASSERTBACK:
612      case OP_ASSERTBACK_NOT:
613      case OP_ONCE:
614      case OP_ONCE_NC:
615      case OP_BRA:
616      case OP_BRAPOS:
617      case OP_CBRA:
618      case OP_CBRAPOS:
619      case OP_COND:
620      case OP_SBRA:
621      case OP_SBRAPOS:
622      case OP_SCBRA:
623      case OP_SCBRAPOS:
624      case OP_SCOND:
625      case OP_CREF:
626      case OP_DNCREF:
627      case OP_RREF:
628      case OP_DNRREF:
629    case OP_DEF:    case OP_DEF:
630    case OP_BRAZERO:    case OP_BRAZERO:
631    case OP_BRAMINZERO:    case OP_BRAMINZERO:
632    case OP_BRAPOSZERO:    case OP_BRAPOSZERO:
633      case OP_PRUNE:
634      case OP_SKIP:
635      case OP_THEN:
636    case OP_COMMIT:    case OP_COMMIT:
637    case OP_FAIL:    case OP_FAIL:
638    case OP_ACCEPT:    case OP_ACCEPT:
639    case OP_ASSERT_ACCEPT:    case OP_ASSERT_ACCEPT:
640      case OP_CLOSE:
641    case OP_SKIPZERO:    case OP_SKIPZERO:
642    return cc + 1;    return cc + PRIV(OP_lengths)[*cc];
   
   case OP_ANYBYTE:  
 #ifdef SUPPORT_UTF  
   if (common->utf) return NULL;  
 #endif  
   return cc + 1;  
643    
644    case OP_CHAR:    case OP_CHAR:
645    case OP_CHARI:    case OP_CHARI:
# Line 551  switch(*cc) Line 651  switch(*cc)
651    case OP_MINPLUS:    case OP_MINPLUS:
652    case OP_QUERY:    case OP_QUERY:
653    case OP_MINQUERY:    case OP_MINQUERY:
654      case OP_UPTO:
655      case OP_MINUPTO:
656      case OP_EXACT:
657    case OP_POSSTAR:    case OP_POSSTAR:
658    case OP_POSPLUS:    case OP_POSPLUS:
659    case OP_POSQUERY:    case OP_POSQUERY:
660      case OP_POSUPTO:
661    case OP_STARI:    case OP_STARI:
662    case OP_MINSTARI:    case OP_MINSTARI:
663    case OP_PLUSI:    case OP_PLUSI:
664    case OP_MINPLUSI:    case OP_MINPLUSI:
665    case OP_QUERYI:    case OP_QUERYI:
666    case OP_MINQUERYI:    case OP_MINQUERYI:
667      case OP_UPTOI:
668      case OP_MINUPTOI:
669      case OP_EXACTI:
670    case OP_POSSTARI:    case OP_POSSTARI:
671    case OP_POSPLUSI:    case OP_POSPLUSI:
672    case OP_POSQUERYI:    case OP_POSQUERYI:
673      case OP_POSUPTOI:
674    case OP_NOTSTAR:    case OP_NOTSTAR:
675    case OP_NOTMINSTAR:    case OP_NOTMINSTAR:
676    case OP_NOTPLUS:    case OP_NOTPLUS:
677    case OP_NOTMINPLUS:    case OP_NOTMINPLUS:
678    case OP_NOTQUERY:    case OP_NOTQUERY:
679    case OP_NOTMINQUERY:    case OP_NOTMINQUERY:
680      case OP_NOTUPTO:
681      case OP_NOTMINUPTO:
682      case OP_NOTEXACT:
683    case OP_NOTPOSSTAR:    case OP_NOTPOSSTAR:
684    case OP_NOTPOSPLUS:    case OP_NOTPOSPLUS:
685    case OP_NOTPOSQUERY:    case OP_NOTPOSQUERY:
686      case OP_NOTPOSUPTO:
687    case OP_NOTSTARI:    case OP_NOTSTARI:
688    case OP_NOTMINSTARI:    case OP_NOTMINSTARI:
689    case OP_NOTPLUSI:    case OP_NOTPLUSI:
690    case OP_NOTMINPLUSI:    case OP_NOTMINPLUSI:
691    case OP_NOTQUERYI:    case OP_NOTQUERYI:
692    case OP_NOTMINQUERYI:    case OP_NOTMINQUERYI:
   case OP_NOTPOSSTARI:  
   case OP_NOTPOSPLUSI:  
   case OP_NOTPOSQUERYI:  
   cc += 2;  
 #ifdef SUPPORT_UTF  
   if (common->utf && HAS_EXTRALEN(cc[-1])) cc += GET_EXTRALEN(cc[-1]);  
 #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:  
693    case OP_NOTUPTOI:    case OP_NOTUPTOI:
694    case OP_NOTMINUPTOI:    case OP_NOTMINUPTOI:
695    case OP_NOTEXACTI:    case OP_NOTEXACTI:
696      case OP_NOTPOSSTARI:
697      case OP_NOTPOSPLUSI:
698      case OP_NOTPOSQUERYI:
699    case OP_NOTPOSUPTOI:    case OP_NOTPOSUPTOI:
700    cc += 2 + IMM2_SIZE;    cc += PRIV(OP_lengths)[*cc];
701  #ifdef SUPPORT_UTF  #ifdef SUPPORT_UTF
702    if (common->utf && HAS_EXTRALEN(cc[-1])) cc += GET_EXTRALEN(cc[-1]);    if (common->utf && HAS_EXTRALEN(cc[-1])) cc += GET_EXTRALEN(cc[-1]);
703  #endif  #endif
704    return cc;    return cc;
705    
706    case OP_NOTPROP:    /* Special cases. */
707    case OP_PROP:    case OP_TYPESTAR:
708    return cc + 1 + 2;    case OP_TYPEMINSTAR:
709      case OP_TYPEPLUS:
710      case OP_TYPEMINPLUS:
711      case OP_TYPEQUERY:
712      case OP_TYPEMINQUERY:
713    case OP_TYPEUPTO:    case OP_TYPEUPTO:
714    case OP_TYPEMINUPTO:    case OP_TYPEMINUPTO:
715    case OP_TYPEEXACT:    case OP_TYPEEXACT:
716      case OP_TYPEPOSSTAR:
717      case OP_TYPEPOSPLUS:
718      case OP_TYPEPOSQUERY:
719    case OP_TYPEPOSUPTO:    case OP_TYPEPOSUPTO:
720    case OP_REF:    return cc + PRIV(OP_lengths)[*cc] - 1;
   case OP_REFI:  
   case OP_CREF:  
   case OP_NCREF:  
   case OP_RREF:  
   case OP_NRREF:  
   case OP_CLOSE:  
   cc += 1 + IMM2_SIZE;  
   return cc;  
721    
722    case OP_CRRANGE:    case OP_ANYBYTE:
723    case OP_CRMINRANGE:  #ifdef SUPPORT_UTF
724    return cc + 1 + 2 * IMM2_SIZE;    if (common->utf) return NULL;
725    #endif
726    case OP_CLASS:    return cc + 1;
   case OP_NCLASS:  
   return cc + 1 + 32 / sizeof(pcre_uchar);  
727    
728  #if defined SUPPORT_UTF || !defined COMPILE_PCRE8  #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
729    case OP_XCLASS:    case OP_XCLASS:
730    return cc + GET(cc, 1);    return cc + GET(cc, 1);
731  #endif  #endif
732    
   case OP_RECURSE:  
   case OP_ASSERT:  
   case OP_ASSERT_NOT:  
   case OP_ASSERTBACK:  
   case OP_ASSERTBACK_NOT:  
   case OP_REVERSE:  
   case OP_ONCE:  
   case OP_ONCE_NC:  
   case OP_BRA:  
   case OP_BRAPOS:  
   case OP_COND:  
   case OP_SBRA:  
   case OP_SBRAPOS:  
   case OP_SCOND:  
   case OP_ALT:  
   case OP_KET:  
   case OP_KETRMAX:  
   case OP_KETRMIN:  
   case OP_KETRPOS:  
   return cc + 1 + LINK_SIZE;  
   
   case OP_CBRA:  
   case OP_CBRAPOS:  
   case OP_SCBRA:  
   case OP_SCBRAPOS:  
   return cc + 1 + LINK_SIZE + IMM2_SIZE;  
   
733    case OP_MARK:    case OP_MARK:
734      case OP_PRUNE_ARG:
735      case OP_SKIP_ARG:
736      case OP_THEN_ARG:
737    return cc + 1 + 2 + cc[1];    return cc + 1 + 2 + cc[1];
738    
739    default:    default:
740      /* All opcodes are supported now! */
741      SLJIT_ASSERT_STOP();
742    return NULL;    return NULL;
743    }    }
744  }  }
745    
746  #define CASE_ITERATOR_LOCAL1 \  static BOOL check_opcode_types(compiler_common *common, pcre_uchar *cc, pcre_uchar *ccend)
     case OP_MINSTAR: \  
     case OP_MINPLUS: \  
     case OP_QUERY: \  
     case OP_MINQUERY: \  
     case OP_MINSTARI: \  
     case OP_MINPLUSI: \  
     case OP_QUERYI: \  
     case OP_MINQUERYI: \  
     case OP_NOTMINSTAR: \  
     case OP_NOTMINPLUS: \  
     case OP_NOTQUERY: \  
     case OP_NOTMINQUERY: \  
     case OP_NOTMINSTARI: \  
     case OP_NOTMINPLUSI: \  
     case OP_NOTQUERYI: \  
     case OP_NOTMINQUERYI:  
   
 #define CASE_ITERATOR_LOCAL2A \  
     case OP_STAR: \  
     case OP_PLUS: \  
     case OP_STARI: \  
     case OP_PLUSI: \  
     case OP_NOTSTAR: \  
     case OP_NOTPLUS: \  
     case OP_NOTSTARI: \  
     case OP_NOTPLUSI:  
   
 #define CASE_ITERATOR_LOCAL2B \  
     case OP_UPTO: \  
     case OP_MINUPTO: \  
     case OP_UPTOI: \  
     case OP_MINUPTOI: \  
     case OP_NOTUPTO: \  
     case OP_NOTMINUPTO: \  
     case OP_NOTUPTOI: \  
     case OP_NOTMINUPTOI:  
   
 #define CASE_ITERATOR_TYPE_LOCAL1 \  
     case OP_TYPEMINSTAR: \  
     case OP_TYPEMINPLUS: \  
     case OP_TYPEQUERY: \  
     case OP_TYPEMINQUERY:  
   
 #define CASE_ITERATOR_TYPE_LOCAL2A \  
     case OP_TYPESTAR: \  
     case OP_TYPEPLUS:  
   
 #define CASE_ITERATOR_TYPE_LOCAL2B \  
     case OP_TYPEUPTO: \  
     case OP_TYPEMINUPTO:  
   
 static int get_class_iterator_size(pcre_uchar *cc)  
 {  
 switch(*cc)  
   {  
   case OP_CRSTAR:  
   case OP_CRPLUS:  
   return 2;  
   
   case OP_CRMINSTAR:  
   case OP_CRMINPLUS:  
   case OP_CRQUERY:  
   case OP_CRMINQUERY:  
   return 1;  
   
   case OP_CRRANGE:  
   case OP_CRMINRANGE:  
   if (GET2(cc, 1) == GET2(cc, 1 + IMM2_SIZE))  
     return 0;  
   return 2;  
   
   default:  
   return 0;  
   }  
 }  
   
 static int get_localspace(compiler_common *common, pcre_uchar *cc, pcre_uchar *ccend)  
747  {  {
748  int localspace = 0;  int count;
749  pcre_uchar *alternative;  pcre_uchar *slot;
 pcre_uchar *end = NULL;  
 int space, size, bracketlen;  
750    
751  /* 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. */
752  while (cc < ccend)  while (cc < ccend)
753    {    {
   space = 0;  
   size = 0;  
   bracketlen = 0;  
754    switch(*cc)    switch(*cc)
755      {      {
756      case OP_SET_SOM:      case OP_SET_SOM:
# Line 772  while (cc < ccend) Line 758  while (cc < ccend)
758      cc += 1;      cc += 1;
759      break;      break;
760    
761      case OP_ASSERT:      case OP_REF:
762      case OP_ASSERT_NOT:      case OP_REFI:
763      case OP_ASSERTBACK:      common->optimized_cbracket[GET2(cc, 1)] = 0;
764      case OP_ASSERTBACK_NOT:      cc += 1 + IMM2_SIZE;
     case OP_ONCE:  
     case OP_ONCE_NC:  
     case OP_BRAPOS:  
     case OP_SBRA:  
     case OP_SBRAPOS:  
     case OP_SCOND:  
     localspace += sizeof(sljit_w);  
     bracketlen = 1 + LINK_SIZE;  
765      break;      break;
766    
767      case OP_CBRAPOS:      case OP_CBRAPOS:
768      case OP_SCBRAPOS:      case OP_SCBRAPOS:
769      localspace += sizeof(sljit_w);      common->optimized_cbracket[GET2(cc, 1 + LINK_SIZE)] = 0;
770      bracketlen = 1 + LINK_SIZE + IMM2_SIZE;      cc += 1 + LINK_SIZE + IMM2_SIZE;
771      break;      break;
772    
773      case OP_COND:      case OP_COND:
774      /* Might be a hidden SCOND. */      case OP_SCOND:
775      alternative = cc + GET(cc, 1);      /* Only AUTO_CALLOUT can insert this opcode. We do
776      if (*alternative == OP_KETRMAX || *alternative == OP_KETRMIN)         not intend to support this case. */
777        localspace += sizeof(sljit_w);      if (cc[1 + LINK_SIZE] == OP_CALLOUT)
778      bracketlen = 1 + LINK_SIZE;        return FALSE;
779      break;      cc += 1 + LINK_SIZE;
   
     case OP_BRA:  
     bracketlen = 1 + LINK_SIZE;  
     break;  
   
     case OP_CBRA:  
     case OP_SCBRA:  
     bracketlen = 1 + LINK_SIZE + IMM2_SIZE;  
     break;  
   
     CASE_ITERATOR_LOCAL1  
     space = 1;  
     size = -2;  
     break;  
   
     CASE_ITERATOR_LOCAL2A  
     space = 2;  
     size = -2;  
     break;  
   
     CASE_ITERATOR_LOCAL2B  
     space = 2;  
     size = -(2 + IMM2_SIZE);  
     break;  
   
     CASE_ITERATOR_TYPE_LOCAL1  
     space = 1;  
     size = 1;  
     break;  
   
     CASE_ITERATOR_TYPE_LOCAL2A  
     if (cc[1] != OP_ANYNL && cc[1] != OP_EXTUNI)  
       space = 2;  
     size = 1;  
     break;  
   
     CASE_ITERATOR_TYPE_LOCAL2B  
     if (cc[1 + IMM2_SIZE] != OP_ANYNL && cc[1 + IMM2_SIZE] != OP_EXTUNI)  
       space = 2;  
     size = 1 + IMM2_SIZE;  
780      break;      break;
781    
782      case OP_CLASS:      case OP_CREF:
783      case OP_NCLASS:      common->optimized_cbracket[GET2(cc, 1)] = 0;
784      size += 1 + 32 / sizeof(pcre_uchar);      cc += 1 + IMM2_SIZE;
     space = get_class_iterator_size(cc + size);  
785      break;      break;
786    
787  #if defined SUPPORT_UTF || !defined COMPILE_PCRE8      case OP_DNREF:
788      case OP_XCLASS:      case OP_DNREFI:
789      size = GET(cc, 1);      case OP_DNCREF:
790      space = get_class_iterator_size(cc + size);      count = GET2(cc, 1 + IMM2_SIZE);
791        slot = common->name_table + GET2(cc, 1) * common->name_entry_size;
792        while (count-- > 0)
793          {
794          common->optimized_cbracket[GET2(slot, 0)] = 0;
795          slot += common->name_entry_size;
796          }
797        cc += 1 + 2 * IMM2_SIZE;
798      break;      break;
 #endif  
799    
800      case OP_RECURSE:      case OP_RECURSE:
801      /* Set its value only once. */      /* Set its value only once. */
802      if (common->recursive_head == 0)      if (common->recursive_head_ptr == 0)
803        {        {
804        common->recursive_head = common->ovector_start;        common->recursive_head_ptr = common->ovector_start;
805        common->ovector_start += sizeof(sljit_w);        common->ovector_start += sizeof(sljit_sw);
806        }        }
807      cc += 1 + LINK_SIZE;      cc += 1 + LINK_SIZE;
808      break;      break;
809    
810        case OP_CALLOUT:
811        if (common->capture_last_ptr == 0)
812          {
813          common->capture_last_ptr = common->ovector_start;
814          common->ovector_start += sizeof(sljit_sw);
815          }
816        cc += 2 + 2 * LINK_SIZE;
817        break;
818    
819        case OP_THEN_ARG:
820        common->has_then = TRUE;
821        common->control_head_ptr = 1;
822        /* Fall through. */
823    
824        case OP_PRUNE_ARG:
825        common->needs_start_ptr = TRUE;
826        /* Fall through. */
827    
828      case OP_MARK:      case OP_MARK:
829      if (common->mark_ptr == 0)      if (common->mark_ptr == 0)
830        {        {
831        common->mark_ptr = common->ovector_start;        common->mark_ptr = common->ovector_start;
832        common->ovector_start += sizeof(sljit_w);        common->ovector_start += sizeof(sljit_sw);
833        }        }
834      cc += 1 + 2 + cc[1];      cc += 1 + 2 + cc[1];
835      break;      break;
836    
837        case OP_THEN:
838        common->has_then = TRUE;
839        common->control_head_ptr = 1;
840        /* Fall through. */
841    
842        case OP_PRUNE:
843        case OP_SKIP:
844        common->needs_start_ptr = TRUE;
845        cc += 1;
846        break;
847    
848        case OP_SKIP_ARG:
849        common->control_head_ptr = 1;
850        common->has_skip_arg = TRUE;
851        cc += 1 + 2 + cc[1];
852        break;
853    
854      default:      default:
855      cc = next_opcode(common, cc);      cc = next_opcode(common, cc);
856      if (cc == NULL)      if (cc == NULL)
857        return -1;        return FALSE;
858      break;      break;
859      }      }
860      }
861    return TRUE;
862    }
863    
864    if (space > 0 && cc >= end)  static int get_class_iterator_size(pcre_uchar *cc)
865      localspace += sizeof(sljit_w) * space;  {
866    switch(*cc)
867      {
868      case OP_CRSTAR:
869      case OP_CRPLUS:
870      return 2;
871    
872    if (size != 0)    case OP_CRMINSTAR:
873      case OP_CRMINPLUS:
874      case OP_CRQUERY:
875      case OP_CRMINQUERY:
876      return 1;
877    
878      case OP_CRRANGE:
879      case OP_CRMINRANGE:
880      if (GET2(cc, 1) == GET2(cc, 1 + IMM2_SIZE))
881        return 0;
882      return 2;
883    
884      default:
885      return 0;
886      }
887    }
888    
889    static BOOL detect_repeat(compiler_common *common, pcre_uchar *begin)
890    {
891    pcre_uchar *end = bracketend(begin);
892    pcre_uchar *next;
893    pcre_uchar *next_end;
894    pcre_uchar *max_end;
895    pcre_uchar type;
896    sljit_sw length = end - begin;
897    int min, max, i;
898    
899    /* Detect fixed iterations first. */
900    if (end[-(1 + LINK_SIZE)] != OP_KET)
901      return FALSE;
902    
903    /* Already detected repeat. */
904    if (common->private_data_ptrs[end - common->start - LINK_SIZE] != 0)
905      return TRUE;
906    
907    next = end;
908    min = 1;
909    while (1)
910      {
911      if (*next != *begin)
912        break;
913      next_end = bracketend(next);
914      if (next_end - next != length || memcmp(begin, next, IN_UCHARS(length)) != 0)
915        break;
916      next = next_end;
917      min++;
918      }
919    
920    if (min == 2)
921      return FALSE;
922    
923    max = 0;
924    max_end = next;
925    if (*next == OP_BRAZERO || *next == OP_BRAMINZERO)
926      {
927      type = *next;
928      while (1)
929      {      {
930      if (size < 0)      if (next[0] != type || next[1] != OP_BRA || next[2 + LINK_SIZE] != *begin)
931        {        break;
932        cc += -size;      next_end = bracketend(next + 2 + LINK_SIZE);
933  #ifdef SUPPORT_UTF      if (next_end - next != (length + 2 + LINK_SIZE) || memcmp(begin, next + 2 + LINK_SIZE, IN_UCHARS(length)) != 0)
934        if (common->utf && HAS_EXTRALEN(cc[-1])) cc += GET_EXTRALEN(cc[-1]);        break;
935  #endif      next = next_end;
936        }      max++;
     else  
       cc += size;  
937      }      }
938    
939    if (bracketlen > 0)    if (next[0] == type && next[1] == *begin && max >= 1)
940      {      {
941      if (cc >= end)      next_end = bracketend(next + 1);
942        if (next_end - next == (length + 1) && memcmp(begin, next + 1, IN_UCHARS(length)) == 0)
943        {        {
944        end = bracketend(cc);        for (i = 0; i < max; i++, next_end += 1 + LINK_SIZE)
945        if (end[-1 - LINK_SIZE] == OP_KET)          if (*next_end != OP_KET)
946          end = NULL;            break;
947    
948          if (i == max)
949            {
950            common->private_data_ptrs[max_end - common->start - LINK_SIZE] = next_end - max_end;
951            common->private_data_ptrs[max_end - common->start - LINK_SIZE + 1] = (type == OP_BRAZERO) ? OP_UPTO : OP_MINUPTO;
952            /* +2 the original and the last. */
953            common->private_data_ptrs[max_end - common->start - LINK_SIZE + 2] = max + 2;
954            if (min == 1)
955              return TRUE;
956            min--;
957            max_end -= (1 + LINK_SIZE) + GET(max_end, -LINK_SIZE);
958            }
959        }        }
     cc += bracketlen;  
960      }      }
961    }    }
962  return localspace;  
963    if (min >= 3)
964      {
965      common->private_data_ptrs[end - common->start - LINK_SIZE] = max_end - end;
966      common->private_data_ptrs[end - common->start - LINK_SIZE + 1] = OP_EXACT;
967      common->private_data_ptrs[end - common->start - LINK_SIZE + 2] = min;
968      return TRUE;
969      }
970    
971    return FALSE;
972  }  }
973    
974  static void set_localptrs(compiler_common *common, int localptr, pcre_uchar *ccend)  #define CASE_ITERATOR_PRIVATE_DATA_1 \
975        case OP_MINSTAR: \
976        case OP_MINPLUS: \
977        case OP_QUERY: \
978        case OP_MINQUERY: \
979        case OP_MINSTARI: \
980        case OP_MINPLUSI: \
981        case OP_QUERYI: \
982        case OP_MINQUERYI: \
983        case OP_NOTMINSTAR: \
984        case OP_NOTMINPLUS: \
985        case OP_NOTQUERY: \
986        case OP_NOTMINQUERY: \
987        case OP_NOTMINSTARI: \
988        case OP_NOTMINPLUSI: \
989        case OP_NOTQUERYI: \
990        case OP_NOTMINQUERYI:
991    
992    #define CASE_ITERATOR_PRIVATE_DATA_2A \
993        case OP_STAR: \
994        case OP_PLUS: \
995        case OP_STARI: \
996        case OP_PLUSI: \
997        case OP_NOTSTAR: \
998        case OP_NOTPLUS: \
999        case OP_NOTSTARI: \
1000        case OP_NOTPLUSI:
1001    
1002    #define CASE_ITERATOR_PRIVATE_DATA_2B \
1003        case OP_UPTO: \
1004        case OP_MINUPTO: \
1005        case OP_UPTOI: \
1006        case OP_MINUPTOI: \
1007        case OP_NOTUPTO: \
1008        case OP_NOTMINUPTO: \
1009        case OP_NOTUPTOI: \
1010        case OP_NOTMINUPTOI:
1011    
1012    #define CASE_ITERATOR_TYPE_PRIVATE_DATA_1 \
1013        case OP_TYPEMINSTAR: \
1014        case OP_TYPEMINPLUS: \
1015        case OP_TYPEQUERY: \
1016        case OP_TYPEMINQUERY:
1017    
1018    #define CASE_ITERATOR_TYPE_PRIVATE_DATA_2A \
1019        case OP_TYPESTAR: \
1020        case OP_TYPEPLUS:
1021    
1022    #define CASE_ITERATOR_TYPE_PRIVATE_DATA_2B \
1023        case OP_TYPEUPTO: \
1024        case OP_TYPEMINUPTO:
1025    
1026    static void set_private_data_ptrs(compiler_common *common, int *private_data_start, pcre_uchar *ccend)
1027  {  {
1028  pcre_uchar *cc = common->start;  pcre_uchar *cc = common->start;
1029  pcre_uchar *alternative;  pcre_uchar *alternative;
1030  pcre_uchar *end = NULL;  pcre_uchar *end = NULL;
1031    int private_data_ptr = *private_data_start;
1032  int space, size, bracketlen;  int space, size, bracketlen;
1033    
1034  while (cc < ccend)  while (cc < ccend)
# Line 922  while (cc < ccend) Line 1036  while (cc < ccend)
1036    space = 0;    space = 0;
1037    size = 0;    size = 0;
1038    bracketlen = 0;    bracketlen = 0;
1039      if (private_data_ptr > SLJIT_MAX_LOCAL_SIZE)
1040        return;
1041    
1042      if (*cc == OP_ONCE || *cc == OP_ONCE_NC || *cc == OP_BRA || *cc == OP_CBRA || *cc == OP_COND)
1043        if (detect_repeat(common, cc))
1044          {
1045          /* These brackets are converted to repeats, so no global
1046          based single character repeat is allowed. */
1047          if (cc >= end)
1048            end = bracketend(cc);
1049          }
1050    
1051    switch(*cc)    switch(*cc)
1052      {      {
1053        case OP_KET:
1054        if (common->private_data_ptrs[cc + 1 - common->start] != 0)
1055          {
1056          common->private_data_ptrs[cc - common->start] = private_data_ptr;
1057          private_data_ptr += sizeof(sljit_sw);
1058          cc += common->private_data_ptrs[cc + 1 - common->start];
1059          }
1060        cc += 1 + LINK_SIZE;
1061        break;
1062    
1063      case OP_ASSERT:      case OP_ASSERT:
1064      case OP_ASSERT_NOT:      case OP_ASSERT_NOT:
1065      case OP_ASSERTBACK:      case OP_ASSERTBACK:
# Line 934  while (cc < ccend) Line 1070  while (cc < ccend)
1070      case OP_SBRA:      case OP_SBRA:
1071      case OP_SBRAPOS:      case OP_SBRAPOS:
1072      case OP_SCOND:      case OP_SCOND:
1073      common->localptrs[cc - common->start] = localptr;      common->private_data_ptrs[cc - common->start] = private_data_ptr;
1074      localptr += sizeof(sljit_w);      private_data_ptr += sizeof(sljit_sw);
1075      bracketlen = 1 + LINK_SIZE;      bracketlen = 1 + LINK_SIZE;
1076      break;      break;
1077    
1078      case OP_CBRAPOS:      case OP_CBRAPOS:
1079      case OP_SCBRAPOS:      case OP_SCBRAPOS:
1080      common->localptrs[cc - common->start] = localptr;      common->private_data_ptrs[cc - common->start] = private_data_ptr;
1081      localptr += sizeof(sljit_w);      private_data_ptr += sizeof(sljit_sw);
1082      bracketlen = 1 + LINK_SIZE + IMM2_SIZE;      bracketlen = 1 + LINK_SIZE + IMM2_SIZE;
1083      break;      break;
1084    
# Line 951  while (cc < ccend) Line 1087  while (cc < ccend)
1087      alternative = cc + GET(cc, 1);      alternative = cc + GET(cc, 1);
1088      if (*alternative == OP_KETRMAX || *alternative == OP_KETRMIN)      if (*alternative == OP_KETRMAX || *alternative == OP_KETRMIN)
1089        {        {
1090        common->localptrs[cc - common->start] = localptr;        common->private_data_ptrs[cc - common->start] = private_data_ptr;
1091        localptr += sizeof(sljit_w);        private_data_ptr += sizeof(sljit_sw);
1092        }        }
1093      bracketlen = 1 + LINK_SIZE;      bracketlen = 1 + LINK_SIZE;
1094      break;      break;
# Line 966  while (cc < ccend) Line 1102  while (cc < ccend)
1102      bracketlen = 1 + LINK_SIZE + IMM2_SIZE;      bracketlen = 1 + LINK_SIZE + IMM2_SIZE;
1103      break;      break;
1104    
1105      CASE_ITERATOR_LOCAL1      CASE_ITERATOR_PRIVATE_DATA_1
1106      space = 1;      space = 1;
1107      size = -2;      size = -2;
1108      break;      break;
1109    
1110      CASE_ITERATOR_LOCAL2A      CASE_ITERATOR_PRIVATE_DATA_2A
1111      space = 2;      space = 2;
1112      size = -2;      size = -2;
1113      break;      break;
1114    
1115      CASE_ITERATOR_LOCAL2B      CASE_ITERATOR_PRIVATE_DATA_2B
1116      space = 2;      space = 2;
1117      size = -(2 + IMM2_SIZE);      size = -(2 + IMM2_SIZE);
1118      break;      break;
1119    
1120      CASE_ITERATOR_TYPE_LOCAL1      CASE_ITERATOR_TYPE_PRIVATE_DATA_1
1121      space = 1;      space = 1;
1122      size = 1;      size = 1;
1123      break;      break;
1124    
1125      CASE_ITERATOR_TYPE_LOCAL2A      CASE_ITERATOR_TYPE_PRIVATE_DATA_2A
1126      if (cc[1] != OP_ANYNL && cc[1] != OP_EXTUNI)      if (cc[1] != OP_ANYNL && cc[1] != OP_EXTUNI)
1127        space = 2;        space = 2;
1128      size = 1;      size = 1;
1129      break;      break;
1130    
1131      CASE_ITERATOR_TYPE_LOCAL2B      CASE_ITERATOR_TYPE_PRIVATE_DATA_2B
1132      if (cc[1 + IMM2_SIZE] != OP_ANYNL && cc[1 + IMM2_SIZE] != OP_EXTUNI)      if (cc[1 + IMM2_SIZE] != OP_ANYNL && cc[1 + IMM2_SIZE] != OP_EXTUNI)
1133        space = 2;        space = 2;
1134      size = 1 + IMM2_SIZE;      size = 1 + IMM2_SIZE;
# Line 1017  while (cc < ccend) Line 1153  while (cc < ccend)
1153      break;      break;
1154      }      }
1155    
1156      /* Character iterators, which are not inside a repeated bracket,
1157         gets a private slot instead of allocating it on the stack. */
1158    if (space > 0 && cc >= end)    if (space > 0 && cc >= end)
1159      {      {
1160      common->localptrs[cc - common->start] = localptr;      common->private_data_ptrs[cc - common->start] = private_data_ptr;
1161      localptr += sizeof(sljit_w) * space;      private_data_ptr += sizeof(sljit_sw) * space;
1162      }      }
1163    
1164    if (size != 0)    if (size != 0)
# Line 1047  while (cc < ccend) Line 1185  while (cc < ccend)
1185      cc += bracketlen;      cc += bracketlen;
1186      }      }
1187    }    }
1188    *private_data_start = private_data_ptr;
1189  }  }
1190    
1191  /* Returns with -1 if no need for frame. */  /* Returns with a frame_types (always < 0) if no need for frame. */
1192  static int get_framesize(compiler_common *common, pcre_uchar *cc, BOOL recursive)  static int get_framesize(compiler_common *common, pcre_uchar *cc, pcre_uchar *ccend, BOOL recursive, BOOL* needs_control_head)
1193  {  {
 pcre_uchar *ccend = bracketend(cc);  
1194  int length = 0;  int length = 0;
1195  BOOL possessive = FALSE;  int possessive = 0;
1196    BOOL stack_restore = FALSE;
1197  BOOL setsom_found = recursive;  BOOL setsom_found = recursive;
1198  BOOL setmark_found = recursive;  BOOL setmark_found = recursive;
1199    /* The last capture is a local variable even for recursions. */
1200    BOOL capture_last_found = FALSE;
1201    
1202    #if defined DEBUG_FORCE_CONTROL_HEAD && DEBUG_FORCE_CONTROL_HEAD
1203    SLJIT_ASSERT(common->control_head_ptr != 0);
1204    *needs_control_head = TRUE;
1205    #else
1206    *needs_control_head = FALSE;
1207    #endif
1208    
1209  if (!recursive && (*cc == OP_CBRAPOS || *cc == OP_SCBRAPOS))  if (ccend == NULL)
1210    {    {
1211    length = 3;    ccend = bracketend(cc) - (1 + LINK_SIZE);
1212    possessive = TRUE;    if (!recursive && (*cc == OP_CBRAPOS || *cc == OP_SCBRAPOS))
1213        {
1214        possessive = length = (common->capture_last_ptr != 0) ? 5 : 3;
1215        /* This is correct regardless of common->capture_last_ptr. */
1216        capture_last_found = TRUE;
1217        }
1218      cc = next_opcode(common, cc);
1219    }    }
1220    
 cc = next_opcode(common, cc);  
1221  SLJIT_ASSERT(cc != NULL);  SLJIT_ASSERT(cc != NULL);
1222  while (cc < ccend)  while (cc < ccend)
1223    switch(*cc)    switch(*cc)
1224      {      {
1225      case OP_SET_SOM:      case OP_SET_SOM:
1226      SLJIT_ASSERT(common->has_set_som);      SLJIT_ASSERT(common->has_set_som);
1227        stack_restore = TRUE;
1228      if (!setsom_found)      if (!setsom_found)
1229        {        {
1230        length += 2;        length += 2;
# Line 1080  while (cc < ccend) Line 1234  while (cc < ccend)
1234      break;      break;
1235    
1236      case OP_MARK:      case OP_MARK:
1237        case OP_PRUNE_ARG:
1238        case OP_THEN_ARG:
1239      SLJIT_ASSERT(common->mark_ptr != 0);      SLJIT_ASSERT(common->mark_ptr != 0);
1240        stack_restore = TRUE;
1241      if (!setmark_found)      if (!setmark_found)
1242        {        {
1243        length += 2;        length += 2;
1244        setmark_found = TRUE;        setmark_found = TRUE;
1245        }        }
1246        if (common->control_head_ptr != 0)
1247          *needs_control_head = TRUE;
1248      cc += 1 + 2 + cc[1];      cc += 1 + 2 + cc[1];
1249      break;      break;
1250    
1251      case OP_RECURSE:      case OP_RECURSE:
1252        stack_restore = TRUE;
1253      if (common->has_set_som && !setsom_found)      if (common->has_set_som && !setsom_found)
1254        {        {
1255        length += 2;        length += 2;
# Line 1100  while (cc < ccend) Line 1260  while (cc < ccend)
1260        length += 2;        length += 2;
1261        setmark_found = TRUE;        setmark_found = TRUE;
1262        }        }
1263        if (common->capture_last_ptr != 0 && !capture_last_found)
1264          {
1265          length += 2;
1266          capture_last_found = TRUE;
1267          }
1268      cc += 1 + LINK_SIZE;      cc += 1 + LINK_SIZE;
1269      break;      break;
1270    
# Line 1107  while (cc < ccend) Line 1272  while (cc < ccend)
1272      case OP_CBRAPOS:      case OP_CBRAPOS:
1273      case OP_SCBRA:      case OP_SCBRA:
1274      case OP_SCBRAPOS:      case OP_SCBRAPOS:
1275        stack_restore = TRUE;
1276        if (common->capture_last_ptr != 0 && !capture_last_found)
1277          {
1278          length += 2;
1279          capture_last_found = TRUE;
1280          }
1281      length += 3;      length += 3;
1282      cc += 1 + LINK_SIZE + IMM2_SIZE;      cc += 1 + LINK_SIZE + IMM2_SIZE;
1283      break;      break;
1284    
1285      default:      default:
1286        stack_restore = TRUE;
1287        /* Fall through. */
1288    
1289        case OP_NOT_WORD_BOUNDARY:
1290        case OP_WORD_BOUNDARY:
1291        case OP_NOT_DIGIT:
1292        case OP_DIGIT:
1293        case OP_NOT_WHITESPACE:
1294        case OP_WHITESPACE:
1295        case OP_NOT_WORDCHAR:
1296        case OP_WORDCHAR:
1297        case OP_ANY:
1298        case OP_ALLANY:
1299        case OP_ANYBYTE:
1300        case OP_NOTPROP:
1301        case OP_PROP:
1302        case OP_ANYNL:
1303        case OP_NOT_HSPACE:
1304        case OP_HSPACE:
1305        case OP_NOT_VSPACE:
1306        case OP_VSPACE:
1307        case OP_EXTUNI:
1308        case OP_EODN:
1309        case OP_EOD:
1310        case OP_CIRC:
1311        case OP_CIRCM:
1312        case OP_DOLL:
1313        case OP_DOLLM:
1314        case OP_CHAR:
1315        case OP_CHARI:
1316        case OP_NOT:
1317        case OP_NOTI:
1318    
1319        case OP_EXACT:
1320        case OP_POSSTAR:
1321        case OP_POSPLUS:
1322        case OP_POSQUERY:
1323        case OP_POSUPTO:
1324    
1325        case OP_EXACTI:
1326        case OP_POSSTARI:
1327        case OP_POSPLUSI:
1328        case OP_POSQUERYI:
1329        case OP_POSUPTOI:
1330    
1331        case OP_NOTEXACT:
1332        case OP_NOTPOSSTAR:
1333        case OP_NOTPOSPLUS:
1334        case OP_NOTPOSQUERY:
1335        case OP_NOTPOSUPTO:
1336    
1337        case OP_NOTEXACTI:
1338        case OP_NOTPOSSTARI:
1339        case OP_NOTPOSPLUSI:
1340        case OP_NOTPOSQUERYI:
1341        case OP_NOTPOSUPTOI:
1342    
1343        case OP_TYPEEXACT:
1344        case OP_TYPEPOSSTAR:
1345        case OP_TYPEPOSPLUS:
1346        case OP_TYPEPOSQUERY:
1347        case OP_TYPEPOSUPTO:
1348    
1349        case OP_CLASS:
1350        case OP_NCLASS:
1351        case OP_XCLASS:
1352    
1353      cc = next_opcode(common, cc);      cc = next_opcode(common, cc);
1354      SLJIT_ASSERT(cc != NULL);      SLJIT_ASSERT(cc != NULL);
1355      break;      break;
1356      }      }
1357    
1358  /* Possessive quantifiers can use a special case. */  /* Possessive quantifiers can use a special case. */
1359  if (SLJIT_UNLIKELY(possessive) && length == 3)  if (SLJIT_UNLIKELY(possessive == length))
1360    return -1;    return stack_restore ? no_frame : no_stack;
1361    
1362  if (length > 0)  if (length > 0)
1363    return length + 1;    return length + 1;
1364  return -1;  return stack_restore ? no_frame : no_stack;
1365  }  }
1366    
1367  static void init_frame(compiler_common *common, pcre_uchar *cc, int stackpos, int stacktop, BOOL recursive)  static void init_frame(compiler_common *common, pcre_uchar *cc, pcre_uchar *ccend, int stackpos, int stacktop, BOOL recursive)
1368  {  {
1369  DEFINE_COMPILER;  DEFINE_COMPILER;
 pcre_uchar *ccend = bracketend(cc);  
1370  BOOL setsom_found = recursive;  BOOL setsom_found = recursive;
1371  BOOL setmark_found = recursive;  BOOL setmark_found = recursive;
1372    /* The last capture is a local variable even for recursions. */
1373    BOOL capture_last_found = FALSE;
1374  int offset;  int offset;
1375    
1376  /* >= 1 + shortest item size (2) */  /* >= 1 + shortest item size (2) */
# Line 1139  SLJIT_UNUSED_ARG(stacktop); Line 1378  SLJIT_UNUSED_ARG(stacktop);
1378  SLJIT_ASSERT(stackpos >= stacktop + 2);  SLJIT_ASSERT(stackpos >= stacktop + 2);
1379    
1380  stackpos = STACK(stackpos);  stackpos = STACK(stackpos);
1381  if (recursive || (*cc != OP_CBRAPOS && *cc != OP_SCBRAPOS))  if (ccend == NULL)
1382    cc = next_opcode(common, cc);    {
1383      ccend = bracketend(cc) - (1 + LINK_SIZE);
1384      if (recursive || (*cc != OP_CBRAPOS && *cc != OP_SCBRAPOS))
1385        cc = next_opcode(common, cc);
1386      }
1387    
1388  SLJIT_ASSERT(cc != NULL);  SLJIT_ASSERT(cc != NULL);
1389  while (cc < ccend)  while (cc < ccend)
1390    switch(*cc)    switch(*cc)
# Line 1150  while (cc < ccend) Line 1394  while (cc < ccend)
1394      if (!setsom_found)      if (!setsom_found)
1395        {        {
1396        OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(0));        OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(0));
1397        OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, SLJIT_IMM, frame_setstrbegin);        OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, SLJIT_IMM, -OVECTOR(0));
1398        stackpos += (int)sizeof(sljit_w);        stackpos += (int)sizeof(sljit_sw);
1399        OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, TMP1, 0);        OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, TMP1, 0);
1400        stackpos += (int)sizeof(sljit_w);        stackpos += (int)sizeof(sljit_sw);
1401        setsom_found = TRUE;        setsom_found = TRUE;
1402        }        }
1403      cc += 1;      cc += 1;
1404      break;      break;
1405    
1406      case OP_MARK:      case OP_MARK:
1407        case OP_PRUNE_ARG:
1408        case OP_THEN_ARG:
1409      SLJIT_ASSERT(common->mark_ptr != 0);      SLJIT_ASSERT(common->mark_ptr != 0);
1410      if (!setmark_found)      if (!setmark_found)
1411        {        {
1412        OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), common->mark_ptr);        OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), common->mark_ptr);
1413        OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, SLJIT_IMM, frame_setmark);        OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, SLJIT_IMM, -common->mark_ptr);
1414        stackpos += (int)sizeof(sljit_w);        stackpos += (int)sizeof(sljit_sw);
1415        OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, TMP1, 0);        OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, TMP1, 0);
1416        stackpos += (int)sizeof(sljit_w);        stackpos += (int)sizeof(sljit_sw);
1417        setmark_found = TRUE;        setmark_found = TRUE;
1418        }        }
1419      cc += 1 + 2 + cc[1];      cc += 1 + 2 + cc[1];
# Line 1177  while (cc < ccend) Line 1423  while (cc < ccend)
1423      if (common->has_set_som && !setsom_found)      if (common->has_set_som && !setsom_found)
1424        {        {
1425        OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(0));        OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(0));
1426        OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, SLJIT_IMM, frame_setstrbegin);        OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, SLJIT_IMM, -OVECTOR(0));
1427        stackpos += (int)sizeof(sljit_w);        stackpos += (int)sizeof(sljit_sw);
1428        OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, TMP1, 0);        OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, TMP1, 0);
1429        stackpos += (int)sizeof(sljit_w);        stackpos += (int)sizeof(sljit_sw);
1430        setsom_found = TRUE;        setsom_found = TRUE;
1431        }        }
1432      if (common->mark_ptr != 0 && !setmark_found)      if (common->mark_ptr != 0 && !setmark_found)
1433        {        {
1434        OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), common->mark_ptr);        OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), common->mark_ptr);
1435        OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, SLJIT_IMM, frame_setmark);        OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, SLJIT_IMM, -common->mark_ptr);
1436        stackpos += (int)sizeof(sljit_w);        stackpos += (int)sizeof(sljit_sw);
1437        OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, TMP1, 0);        OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, TMP1, 0);
1438        stackpos += (int)sizeof(sljit_w);        stackpos += (int)sizeof(sljit_sw);
1439        setmark_found = TRUE;        setmark_found = TRUE;
1440        }        }
1441        if (common->capture_last_ptr != 0 && !capture_last_found)
1442          {
1443          OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), common->capture_last_ptr);
1444          OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, SLJIT_IMM, -common->capture_last_ptr);
1445          stackpos += (int)sizeof(sljit_sw);
1446          OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, TMP1, 0);
1447          stackpos += (int)sizeof(sljit_sw);
1448          capture_last_found = TRUE;
1449          }
1450      cc += 1 + LINK_SIZE;      cc += 1 + LINK_SIZE;
1451      break;      break;
1452    
# Line 1199  while (cc < ccend) Line 1454  while (cc < ccend)
1454      case OP_CBRAPOS:      case OP_CBRAPOS:
1455      case OP_SCBRA:      case OP_SCBRA:
1456      case OP_SCBRAPOS:      case OP_SCBRAPOS:
1457        if (common->capture_last_ptr != 0 && !capture_last_found)
1458          {
1459          OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), common->capture_last_ptr);
1460          OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, SLJIT_IMM, -common->capture_last_ptr);
1461          stackpos += (int)sizeof(sljit_sw);
1462          OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, TMP1, 0);
1463          stackpos += (int)sizeof(sljit_sw);
1464          capture_last_found = TRUE;
1465          }
1466      offset = (GET2(cc, 1 + LINK_SIZE)) << 1;      offset = (GET2(cc, 1 + LINK_SIZE)) << 1;
1467      OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, SLJIT_IMM, OVECTOR(offset));      OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, SLJIT_IMM, OVECTOR(offset));
1468      stackpos += (int)sizeof(sljit_w);      stackpos += (int)sizeof(sljit_sw);
1469      OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(offset));      OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(offset));
1470      OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(offset + 1));      OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(offset + 1));
1471      OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, TMP1, 0);      OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, TMP1, 0);
1472      stackpos += (int)sizeof(sljit_w);      stackpos += (int)sizeof(sljit_sw);
1473      OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, TMP2, 0);      OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, TMP2, 0);
1474      stackpos += (int)sizeof(sljit_w);      stackpos += (int)sizeof(sljit_sw);
1475    
1476      cc += 1 + LINK_SIZE + IMM2_SIZE;      cc += 1 + LINK_SIZE + IMM2_SIZE;
1477      break;      break;
# Line 1218  while (cc < ccend) Line 1482  while (cc < ccend)
1482      break;      break;
1483      }      }
1484    
1485  OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, SLJIT_IMM, frame_end);  OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, SLJIT_IMM, 0);
1486  SLJIT_ASSERT(stackpos == STACK(stacktop));  SLJIT_ASSERT(stackpos == STACK(stacktop));
1487  }  }
1488    
1489  static SLJIT_INLINE int get_localsize(compiler_common *common, pcre_uchar *cc, pcre_uchar *ccend)  static SLJIT_INLINE int get_private_data_copy_length(compiler_common *common, pcre_uchar *cc, pcre_uchar *ccend, BOOL needs_control_head)
1490  {  {
1491  int localsize = 2;  int private_data_length = needs_control_head ? 3 : 2;
1492  int size;  int size;
1493  pcre_uchar *alternative;  pcre_uchar *alternative;
1494  /* Calculate the sum of the local variables. */  /* Calculate the sum of the private machine words. */
1495  while (cc < ccend)  while (cc < ccend)
1496    {    {
1497    size = 0;    size = 0;
1498    switch(*cc)    switch(*cc)
1499      {      {
1500        case OP_KET:
1501        if (PRIVATE_DATA(cc) != 0)
1502          private_data_length++;
1503        cc += 1 + LINK_SIZE;
1504        break;
1505    
1506      case OP_ASSERT:      case OP_ASSERT:
1507      case OP_ASSERT_NOT:      case OP_ASSERT_NOT:
1508      case OP_ASSERTBACK:      case OP_ASSERTBACK:
# Line 1243  while (cc < ccend) Line 1513  while (cc < ccend)
1513      case OP_SBRA:      case OP_SBRA:
1514      case OP_SBRAPOS:      case OP_SBRAPOS:
1515      case OP_SCOND:      case OP_SCOND:
1516      localsize++;      private_data_length++;
1517      cc += 1 + LINK_SIZE;      cc += 1 + LINK_SIZE;
1518      break;      break;
1519    
1520      case OP_CBRA:      case OP_CBRA:
1521      case OP_SCBRA:      case OP_SCBRA:
1522      localsize++;      if (common->optimized_cbracket[GET2(cc, 1 + LINK_SIZE)] == 0)
1523          private_data_length++;
1524      cc += 1 + LINK_SIZE + IMM2_SIZE;      cc += 1 + LINK_SIZE + IMM2_SIZE;
1525      break;      break;
1526    
1527      case OP_CBRAPOS:      case OP_CBRAPOS:
1528      case OP_SCBRAPOS:      case OP_SCBRAPOS:
1529      localsize += 2;      private_data_length += 2;
1530      cc += 1 + LINK_SIZE + IMM2_SIZE;      cc += 1 + LINK_SIZE + IMM2_SIZE;
1531      break;      break;
1532    
# Line 1263  while (cc < ccend) Line 1534  while (cc < ccend)
1534      /* Might be a hidden SCOND. */      /* Might be a hidden SCOND. */
1535      alternative = cc + GET(cc, 1);      alternative = cc + GET(cc, 1);
1536      if (*alternative == OP_KETRMAX || *alternative == OP_KETRMIN)      if (*alternative == OP_KETRMAX || *alternative == OP_KETRMIN)
1537        localsize++;        private_data_length++;
1538      cc += 1 + LINK_SIZE;      cc += 1 + LINK_SIZE;
1539      break;      break;
1540    
1541      CASE_ITERATOR_LOCAL1      CASE_ITERATOR_PRIVATE_DATA_1
1542      if (PRIV_DATA(cc))      if (PRIVATE_DATA(cc))
1543        localsize++;        private_data_length++;
1544      cc += 2;      cc += 2;
1545  #ifdef SUPPORT_UTF  #ifdef SUPPORT_UTF
1546      if (common->utf && HAS_EXTRALEN(cc[-1])) cc += GET_EXTRALEN(cc[-1]);      if (common->utf && HAS_EXTRALEN(cc[-1])) cc += GET_EXTRALEN(cc[-1]);
1547  #endif  #endif
1548      break;      break;
1549    
1550      CASE_ITERATOR_LOCAL2A      CASE_ITERATOR_PRIVATE_DATA_2A
1551      if (PRIV_DATA(cc))      if (PRIVATE_DATA(cc))
1552        localsize += 2;        private_data_length += 2;
1553      cc += 2;      cc += 2;
1554  #ifdef SUPPORT_UTF  #ifdef SUPPORT_UTF
1555      if (common->utf && HAS_EXTRALEN(cc[-1])) cc += GET_EXTRALEN(cc[-1]);      if (common->utf && HAS_EXTRALEN(cc[-1])) cc += GET_EXTRALEN(cc[-1]);
1556  #endif  #endif
1557      break;      break;
1558    
1559      CASE_ITERATOR_LOCAL2B      CASE_ITERATOR_PRIVATE_DATA_2B
1560      if (PRIV_DATA(cc))      if (PRIVATE_DATA(cc))
1561        localsize += 2;        private_data_length += 2;
1562      cc += 2 + IMM2_SIZE;      cc += 2 + IMM2_SIZE;
1563  #ifdef SUPPORT_UTF  #ifdef SUPPORT_UTF
1564      if (common->utf && HAS_EXTRALEN(cc[-1])) cc += GET_EXTRALEN(cc[-1]);      if (common->utf && HAS_EXTRALEN(cc[-1])) cc += GET_EXTRALEN(cc[-1]);
1565  #endif  #endif
1566      break;      break;
1567    
1568      CASE_ITERATOR_TYPE_LOCAL1      CASE_ITERATOR_TYPE_PRIVATE_DATA_1
1569      if (PRIV_DATA(cc))      if (PRIVATE_DATA(cc))
1570        localsize++;        private_data_length++;
1571      cc += 1;      cc += 1;
1572      break;      break;
1573    
1574      CASE_ITERATOR_TYPE_LOCAL2A      CASE_ITERATOR_TYPE_PRIVATE_DATA_2A
1575      if (PRIV_DATA(cc))      if (PRIVATE_DATA(cc))
1576        localsize += 2;        private_data_length += 2;
1577      cc += 1;      cc += 1;
1578      break;      break;
1579    
1580      CASE_ITERATOR_TYPE_LOCAL2B      CASE_ITERATOR_TYPE_PRIVATE_DATA_2B
1581      if (PRIV_DATA(cc))      if (PRIVATE_DATA(cc))
1582        localsize += 2;        private_data_length += 2;
1583      cc += 1 + IMM2_SIZE;      cc += 1 + IMM2_SIZE;
1584      break;      break;
1585    
# Line 1320  while (cc < ccend) Line 1591  while (cc < ccend)
1591  #else  #else
1592      size = 1 + 32 / (int)sizeof(pcre_uchar);      size = 1 + 32 / (int)sizeof(pcre_uchar);
1593  #endif  #endif
1594      if (PRIV_DATA(cc))      if (PRIVATE_DATA(cc))
1595        localsize += get_class_iterator_size(cc + size);        private_data_length += get_class_iterator_size(cc + size);
1596      cc += size;      cc += size;
1597      break;      break;
1598    
# Line 1332  while (cc < ccend) Line 1603  while (cc < ccend)
1603      }      }
1604    }    }
1605  SLJIT_ASSERT(cc == ccend);  SLJIT_ASSERT(cc == ccend);
1606  return localsize;  return private_data_length;
1607  }  }
1608    
1609  static void copy_locals(compiler_common *common, pcre_uchar *cc, pcre_uchar *ccend,  static void copy_private_data(compiler_common *common, pcre_uchar *cc, pcre_uchar *ccend,
1610    BOOL save, int stackptr, int stacktop)    BOOL save, int stackptr, int stacktop, BOOL needs_control_head)
1611  {  {
1612  DEFINE_COMPILER;  DEFINE_COMPILER;
1613  int srcw[2];  int srcw[2];
# Line 1357  stacktop = STACK(stacktop - 1); Line 1628  stacktop = STACK(stacktop - 1);
1628    
1629  if (!save)  if (!save)
1630    {    {
1631    stackptr += sizeof(sljit_w);    stackptr += (needs_control_head ? 2 : 1) * sizeof(sljit_sw);
1632    if (stackptr < stacktop)    if (stackptr < stacktop)
1633      {      {
1634      OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(STACK_TOP), stackptr);      OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(STACK_TOP), stackptr);
1635      stackptr += sizeof(sljit_w);      stackptr += sizeof(sljit_sw);
1636      tmp1empty = FALSE;      tmp1empty = FALSE;
1637      }      }
1638    if (stackptr < stacktop)    if (stackptr < stacktop)
1639      {      {
1640      OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(STACK_TOP), stackptr);      OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(STACK_TOP), stackptr);
1641      stackptr += sizeof(sljit_w);      stackptr += sizeof(sljit_sw);
1642      tmp2empty = FALSE;      tmp2empty = FALSE;
1643      }      }
1644    /* The tmp1next must be TRUE in either way. */    /* The tmp1next must be TRUE in either way. */
1645    }    }
1646    
1647  while (status != end)  do
1648    {    {
1649    count = 0;    count = 0;
1650    switch(status)    switch(status)
1651      {      {
1652      case start:      case start:
1653      SLJIT_ASSERT(save && common->recursive_head != 0);      SLJIT_ASSERT(save && common->recursive_head_ptr != 0);
1654      count = 1;      count = 1;
1655      srcw[0] = common->recursive_head;      srcw[0] = common->recursive_head_ptr;
1656        if (needs_control_head)
1657          {
1658          SLJIT_ASSERT(common->control_head_ptr != 0);
1659          count = 2;
1660          srcw[1] = common->control_head_ptr;
1661          }
1662      status = loop;      status = loop;
1663      break;      break;
1664    
# Line 1394  while (status != end) Line 1671  while (status != end)
1671    
1672      switch(*cc)      switch(*cc)
1673        {        {
1674          case OP_KET:
1675          if (PRIVATE_DATA(cc) != 0)
1676            {
1677            count = 1;
1678            srcw[0] = PRIVATE_DATA(cc);
1679            }
1680          cc += 1 + LINK_SIZE;
1681          break;
1682    
1683        case OP_ASSERT:        case OP_ASSERT:
1684        case OP_ASSERT_NOT:        case OP_ASSERT_NOT:
1685        case OP_ASSERTBACK:        case OP_ASSERTBACK:
# Line 1405  while (status != end) Line 1691  while (status != end)
1691        case OP_SBRAPOS:        case OP_SBRAPOS:
1692        case OP_SCOND:        case OP_SCOND:
1693        count = 1;        count = 1;
1694        srcw[0] = PRIV_DATA(cc);        srcw[0] = PRIVATE_DATA(cc);
1695        SLJIT_ASSERT(srcw[0] != 0);        SLJIT_ASSERT(srcw[0] != 0);
1696        cc += 1 + LINK_SIZE;        cc += 1 + LINK_SIZE;
1697        break;        break;
1698    
1699        case OP_CBRA:        case OP_CBRA:
1700        case OP_SCBRA:        case OP_SCBRA:
1701        count = 1;        if (common->optimized_cbracket[GET2(cc, 1 + LINK_SIZE)] == 0)
1702        srcw[0] = OVECTOR_PRIV(GET2(cc, 1 + LINK_SIZE));          {
1703            count = 1;
1704            srcw[0] = OVECTOR_PRIV(GET2(cc, 1 + LINK_SIZE));
1705            }
1706        cc += 1 + LINK_SIZE + IMM2_SIZE;        cc += 1 + LINK_SIZE + IMM2_SIZE;
1707        break;        break;
1708    
1709        case OP_CBRAPOS:        case OP_CBRAPOS:
1710        case OP_SCBRAPOS:        case OP_SCBRAPOS:
1711        count = 2;        count = 2;
1712        srcw[0] = OVECTOR_PRIV(GET2(cc, 1 + LINK_SIZE));        srcw[0] = PRIVATE_DATA(cc);
1713        srcw[1] = PRIV_DATA(cc);        srcw[1] = OVECTOR_PRIV(GET2(cc, 1 + LINK_SIZE));
1714        SLJIT_ASSERT(srcw[0] != 0);        SLJIT_ASSERT(srcw[0] != 0 && srcw[1] != 0);
1715        cc += 1 + LINK_SIZE + IMM2_SIZE;        cc += 1 + LINK_SIZE + IMM2_SIZE;
1716        break;        break;
1717    
# Line 1432  while (status != end) Line 1721  while (status != end)
1721        if (*alternative == OP_KETRMAX || *alternative == OP_KETRMIN)        if (*alternative == OP_KETRMAX || *alternative == OP_KETRMIN)
1722          {          {
1723          count = 1;          count = 1;
1724          srcw[0] = PRIV_DATA(cc);          srcw[0] = PRIVATE_DATA(cc);
1725          SLJIT_ASSERT(srcw[0] != 0);          SLJIT_ASSERT(srcw[0] != 0);
1726          }          }
1727        cc += 1 + LINK_SIZE;        cc += 1 + LINK_SIZE;
1728        break;        break;
1729    
1730        CASE_ITERATOR_LOCAL1        CASE_ITERATOR_PRIVATE_DATA_1
1731        if (PRIV_DATA(cc))        if (PRIVATE_DATA(cc))
1732          {          {
1733          count = 1;          count = 1;
1734          srcw[0] = PRIV_DATA(cc);          srcw[0] = PRIVATE_DATA(cc);
1735          }          }
1736        cc += 2;        cc += 2;
1737  #ifdef SUPPORT_UTF  #ifdef SUPPORT_UTF
# Line 1450  while (status != end) Line 1739  while (status != end)
1739  #endif  #endif
1740        break;        break;
1741    
1742        CASE_ITERATOR_LOCAL2A        CASE_ITERATOR_PRIVATE_DATA_2A
1743        if (PRIV_DATA(cc))        if (PRIVATE_DATA(cc))
1744          {          {
1745          count = 2;          count = 2;
1746          srcw[0] = PRIV_DATA(cc);          srcw[0] = PRIVATE_DATA(cc);
1747          srcw[1] = PRIV_DATA(cc) + sizeof(sljit_w);          srcw[1] = PRIVATE_DATA(cc) + sizeof(sljit_sw);
1748          }          }
1749        cc += 2;        cc += 2;
1750  #ifdef SUPPORT_UTF  #ifdef SUPPORT_UTF
# Line 1463  while (status != end) Line 1752  while (status != end)
1752  #endif  #endif
1753        break;        break;
1754    
1755        CASE_ITERATOR_LOCAL2B        CASE_ITERATOR_PRIVATE_DATA_2B
1756        if (PRIV_DATA(cc))        if (PRIVATE_DATA(cc))
1757          {          {
1758          count = 2;          count = 2;
1759          srcw[0] = PRIV_DATA(cc);          srcw[0] = PRIVATE_DATA(cc);
1760          srcw[1] = PRIV_DATA(cc) + sizeof(sljit_w);          srcw[1] = PRIVATE_DATA(cc) + sizeof(sljit_sw);
1761          }          }
1762        cc += 2 + IMM2_SIZE;        cc += 2 + IMM2_SIZE;
1763  #ifdef SUPPORT_UTF  #ifdef SUPPORT_UTF
# Line 1476  while (status != end) Line 1765  while (status != end)
1765  #endif  #endif
1766        break;        break;
1767    
1768        CASE_ITERATOR_TYPE_LOCAL1        CASE_ITERATOR_TYPE_PRIVATE_DATA_1
1769        if (PRIV_DATA(cc))        if (PRIVATE_DATA(cc))
1770          {          {
1771          count = 1;          count = 1;
1772          srcw[0] = PRIV_DATA(cc);          srcw[0] = PRIVATE_DATA(cc);
1773          }          }
1774        cc += 1;        cc += 1;
1775        break;        break;
1776    
1777        CASE_ITERATOR_TYPE_LOCAL2A        CASE_ITERATOR_TYPE_PRIVATE_DATA_2A
1778        if (PRIV_DATA(cc))        if (PRIVATE_DATA(cc))
1779          {          {
1780          count = 2;          count = 2;
1781          srcw[0] = PRIV_DATA(cc);          srcw[0] = PRIVATE_DATA(cc);
1782          srcw[1] = srcw[0] + sizeof(sljit_w);          srcw[1] = srcw[0] + sizeof(sljit_sw);
1783          }          }
1784        cc += 1;        cc += 1;
1785        break;        break;
1786    
1787        CASE_ITERATOR_TYPE_LOCAL2B        CASE_ITERATOR_TYPE_PRIVATE_DATA_2B
1788        if (PRIV_DATA(cc))        if (PRIVATE_DATA(cc))
1789          {          {
1790          count = 2;          count = 2;
1791          srcw[0] = PRIV_DATA(cc);          srcw[0] = PRIVATE_DATA(cc);
1792          srcw[1] = srcw[0] + sizeof(sljit_w);          srcw[1] = srcw[0] + sizeof(sljit_sw);
1793          }          }
1794        cc += 1 + IMM2_SIZE;        cc += 1 + IMM2_SIZE;
1795        break;        break;
# Line 1513  while (status != end) Line 1802  while (status != end)
1802  #else  #else
1803        size = 1 + 32 / (int)sizeof(pcre_uchar);        size = 1 + 32 / (int)sizeof(pcre_uchar);
1804  #endif  #endif
1805        if (PRIV_DATA(cc))        if (PRIVATE_DATA(cc))
1806          switch(get_class_iterator_size(cc + size))          switch(get_class_iterator_size(cc + size))
1807            {            {
1808            case 1:            case 1:
1809            count = 1;            count = 1;
1810            srcw[0] = PRIV_DATA(cc);            srcw[0] = PRIVATE_DATA(cc);
1811            break;            break;
1812    
1813            case 2:            case 2:
1814            count = 2;            count = 2;
1815            srcw[0] = PRIV_DATA(cc);            srcw[0] = PRIVATE_DATA(cc);
1816            srcw[1] = srcw[0] + sizeof(sljit_w);            srcw[1] = srcw[0] + sizeof(sljit_sw);
1817            break;            break;
1818    
1819            default:            default:
# Line 1556  while (status != end) Line 1845  while (status != end)
1845          if (!tmp1empty)          if (!tmp1empty)
1846            {            {
1847            OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackptr, TMP1, 0);            OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackptr, TMP1, 0);
1848            stackptr += sizeof(sljit_w);            stackptr += sizeof(sljit_sw);
1849            }            }
1850          OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), srcw[count]);          OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), srcw[count]);
1851          tmp1empty = FALSE;          tmp1empty = FALSE;
# Line 1567  while (status != end) Line 1856  while (status != end)
1856          if (!tmp2empty)          if (!tmp2empty)
1857            {            {
1858            OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackptr, TMP2, 0);            OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackptr, TMP2, 0);
1859            stackptr += sizeof(sljit_w);            stackptr += sizeof(sljit_sw);
1860            }            }
1861          OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), srcw[count]);          OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), srcw[count]);
1862          tmp2empty = FALSE;          tmp2empty = FALSE;
# Line 1584  while (status != end) Line 1873  while (status != end)
1873          if (!tmp1empty)          if (!tmp1empty)
1874            {            {
1875            OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(STACK_TOP), stackptr);            OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(STACK_TOP), stackptr);
1876            stackptr += sizeof(sljit_w);            stackptr += sizeof(sljit_sw);
1877            }            }
1878          tmp1next = FALSE;          tmp1next = FALSE;
1879          }          }
# Line 1596  while (status != end) Line 1885  while (status != end)
1885          if (!tmp2empty)          if (!tmp2empty)
1886            {            {
1887            OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(STACK_TOP), stackptr);            OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(STACK_TOP), stackptr);
1888            stackptr += sizeof(sljit_w);            stackptr += sizeof(sljit_sw);
1889            }            }
1890          tmp1next = TRUE;          tmp1next = TRUE;
1891          }          }
1892        }        }
1893      }      }
1894    }    }
1895    while (status != end);
1896    
1897  if (save)  if (save)
1898    {    {
# Line 1611  if (save) Line 1901  if (save)
1901      if (!tmp1empty)      if (!tmp1empty)
1902        {        {
1903        OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackptr, TMP1, 0);        OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackptr, TMP1, 0);
1904        stackptr += sizeof(sljit_w);        stackptr += sizeof(sljit_sw);
1905        }        }
1906      if (!tmp2empty)      if (!tmp2empty)
1907        {        {
1908        OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackptr, TMP2, 0);        OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackptr, TMP2, 0);
1909        stackptr += sizeof(sljit_w);        stackptr += sizeof(sljit_sw);
1910        }        }
1911      }      }
1912    else    else
# Line 1624  if (save) Line 1914  if (save)
1914      if (!tmp2empty)      if (!tmp2empty)
1915        {        {
1916        OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackptr, TMP2, 0);        OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackptr, TMP2, 0);
1917        stackptr += sizeof(sljit_w);        stackptr += sizeof(sljit_sw);
1918        }        }
1919      if (!tmp1empty)      if (!tmp1empty)
1920        {        {
1921        OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackptr, TMP1, 0);        OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackptr, TMP1, 0);
1922        stackptr += sizeof(sljit_w);        stackptr += sizeof(sljit_sw);
1923        }        }
1924      }      }
1925    }    }
1926  SLJIT_ASSERT(cc == ccend && stackptr == stacktop && (save || (tmp1empty && tmp2empty)));  SLJIT_ASSERT(cc == ccend && stackptr == stacktop && (save || (tmp1empty && tmp2empty)));
1927  }  }
1928    
1929  #undef CASE_ITERATOR_LOCAL1  static SLJIT_INLINE pcre_uchar *set_then_offsets(compiler_common *common, pcre_uchar *cc, pcre_uint8 *current_offset)
1930  #undef CASE_ITERATOR_LOCAL2A  {
1931  #undef CASE_ITERATOR_LOCAL2B  pcre_uchar *end = bracketend(cc);
1932  #undef CASE_ITERATOR_TYPE_LOCAL1  BOOL has_alternatives = cc[GET(cc, 1)] == OP_ALT;
1933  #undef CASE_ITERATOR_TYPE_LOCAL2A  
1934  #undef CASE_ITERATOR_TYPE_LOCAL2B  /* Assert captures then. */
1935    if (*cc >= OP_ASSERT && *cc <= OP_ASSERTBACK_NOT)
1936      current_offset = NULL;
1937    /* Conditional block does not. */
1938    if (*cc == OP_COND || *cc == OP_SCOND)
1939      has_alternatives = FALSE;
1940    
1941    cc = next_opcode(common, cc);
1942    if (has_alternatives)
1943      current_offset = common->then_offsets + (cc - common->start);
1944    
1945    while (cc < end)
1946      {
1947      if ((*cc >= OP_ASSERT && *cc <= OP_ASSERTBACK_NOT) || (*cc >= OP_ONCE && *cc <= OP_SCOND))
1948        cc = set_then_offsets(common, cc, current_offset);
1949      else
1950        {
1951        if (*cc == OP_ALT && has_alternatives)
1952          current_offset = common->then_offsets + (cc + 1 + LINK_SIZE - common->start);
1953        if (*cc >= OP_THEN && *cc <= OP_THEN_ARG && current_offset != NULL)
1954          *current_offset = 1;
1955        cc = next_opcode(common, cc);
1956        }
1957      }
1958    
1959    return end;
1960    }
1961    
1962  static SLJIT_INLINE BOOL ispowerof2(unsigned int value)  #undef CASE_ITERATOR_PRIVATE_DATA_1
1963    #undef CASE_ITERATOR_PRIVATE_DATA_2A
1964    #undef CASE_ITERATOR_PRIVATE_DATA_2B
1965    #undef CASE_ITERATOR_TYPE_PRIVATE_DATA_1
1966    #undef CASE_ITERATOR_TYPE_PRIVATE_DATA_2A
1967    #undef CASE_ITERATOR_TYPE_PRIVATE_DATA_2B
1968    
1969    static SLJIT_INLINE BOOL is_powerof2(unsigned int value)
1970  {  {
1971  return (value & (value - 1)) == 0;  return (value & (value - 1)) == 0;
1972  }  }
# Line 1654  while (list) Line 1977  while (list)
1977    {    {
1978    /* sljit_set_label is clever enough to do nothing    /* sljit_set_label is clever enough to do nothing
1979    if either the jump or the label is NULL. */    if either the jump or the label is NULL. */
1980    sljit_set_label(list->jump, label);    SET_LABEL(list->jump, label);
1981    list = list->next;    list = list->next;
1982    }    }
1983  }  }
# Line 1670  if (list_item) Line 1993  if (list_item)
1993    }    }
1994  }  }
1995    
1996  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)
1997  {  {
1998  DEFINE_COMPILER;  DEFINE_COMPILER;
1999  stub_list* list_item = sljit_alloc_memory(compiler, sizeof(stub_list));  stub_list* list_item = sljit_alloc_memory(compiler, sizeof(stub_list));
2000    
2001  if (list_item)  if (list_item)
2002    {    {
   list_item->type = type;  
   list_item->data = data;  
2003    list_item->start = start;    list_item->start = start;
2004    list_item->quit = LABEL();    list_item->quit = LABEL();
2005    list_item->next = common->stubs;    list_item->next = common->stubs;
# Line 1694  stub_list* list_item = common->stubs; Line 2015  stub_list* list_item = common->stubs;
2015  while (list_item)  while (list_item)
2016    {    {
2017    JUMPHERE(list_item->start);    JUMPHERE(list_item->start);
2018    switch(list_item->type)    add_jump(compiler, &common->stackalloc, JUMP(SLJIT_FAST_CALL));
     {  
     case stack_alloc:  
     add_jump(compiler, &common->stackalloc, JUMP(SLJIT_FAST_CALL));  
     break;  
     }  
2019    JUMPTO(SLJIT_JUMP, list_item->quit);    JUMPTO(SLJIT_JUMP, list_item->quit);
2020    list_item = list_item->next;    list_item = list_item->next;
2021    }    }
2022  common->stubs = NULL;  common->stubs = NULL;
2023  }  }
2024    
2025  static SLJIT_INLINE void decrease_call_count(compiler_common *common)  static SLJIT_INLINE void count_match(compiler_common *common)
2026  {  {
2027  DEFINE_COMPILER;  DEFINE_COMPILER;
2028    
2029  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);
2030  add_jump(compiler, &common->calllimit, JUMP(SLJIT_C_ZERO));  add_jump(compiler, &common->calllimit, JUMP(SLJIT_C_ZERO));
2031  }  }
2032    
# Line 1719  static SLJIT_INLINE void allocate_stack( Line 2035  static SLJIT_INLINE void allocate_stack(
2035  /* May destroy all locals and registers except TMP2. */  /* May destroy all locals and registers except TMP2. */
2036  DEFINE_COMPILER;  DEFINE_COMPILER;
2037    
2038  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));
2039  #ifdef DESTROY_REGISTERS  #ifdef DESTROY_REGISTERS
2040  OP1(SLJIT_MOV, TMP1, 0, SLJIT_IMM, 12345);  OP1(SLJIT_MOV, TMP1, 0, SLJIT_IMM, 12345);
2041  OP1(SLJIT_MOV, TMP3, 0, TMP1, 0);  OP1(SLJIT_MOV, TMP3, 0, TMP1, 0);
# Line 1727  OP1(SLJIT_MOV, RETURN_ADDR, 0, TMP1, 0); Line 2043  OP1(SLJIT_MOV, RETURN_ADDR, 0, TMP1, 0);
2043  OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), LOCALS0, TMP1, 0);  OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), LOCALS0, TMP1, 0);
2044  OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), LOCALS1, TMP1, 0);  OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), LOCALS1, TMP1, 0);
2045  #endif  #endif
2046  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));
2047  }  }
2048    
2049  static SLJIT_INLINE void free_stack(compiler_common *common, int size)  static SLJIT_INLINE void free_stack(compiler_common *common, int size)
2050  {  {
2051  DEFINE_COMPILER;  DEFINE_COMPILER;
2052  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));
2053  }  }
2054    
2055  static SLJIT_INLINE void reset_ovector(compiler_common *common, int length)  static SLJIT_INLINE void reset_ovector(compiler_common *common, int length)
# Line 1741  static SLJIT_INLINE void reset_ovector(c Line 2057  static SLJIT_INLINE void reset_ovector(c
2057  DEFINE_COMPILER;  DEFINE_COMPILER;
2058  struct sljit_label *loop;  struct sljit_label *loop;
2059  int i;  int i;
2060    
2061  /* At this point we can freely use all temporary registers. */  /* At this point we can freely use all temporary registers. */
2062    SLJIT_ASSERT(length > 1);
2063  /* TMP1 returns with begin - 1. */  /* TMP1 returns with begin - 1. */
2064  OP2(SLJIT_SUB, SLJIT_TEMPORARY_REG1, 0, SLJIT_MEM1(SLJIT_SAVED_REG1), SLJIT_OFFSETOF(jit_arguments, begin), SLJIT_IMM, IN_UCHARS(1));  OP2(SLJIT_SUB, SLJIT_SCRATCH_REG1, 0, SLJIT_MEM1(SLJIT_SAVED_REG1), SLJIT_OFFSETOF(jit_arguments, begin), SLJIT_IMM, IN_UCHARS(1));
2065    if (length < 8)
2066      {
2067      for (i = 1; i < length; i++)
2068        OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(i), SLJIT_SCRATCH_REG1, 0);
2069      }
2070    else
2071      {
2072      GET_LOCAL_BASE(SLJIT_SCRATCH_REG2, 0, OVECTOR_START);
2073      OP1(SLJIT_MOV, SLJIT_SCRATCH_REG3, 0, SLJIT_IMM, length - 1);
2074      loop = LABEL();
2075      OP1(SLJIT_MOVU, SLJIT_MEM1(SLJIT_SCRATCH_REG2), sizeof(sljit_sw), SLJIT_SCRATCH_REG1, 0);
2076      OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_SCRATCH_REG3, 0, SLJIT_SCRATCH_REG3, 0, SLJIT_IMM, 1);
2077      JUMPTO(SLJIT_C_NOT_ZERO, loop);
2078      }
2079    }
2080    
2081    static SLJIT_INLINE void do_reset_match(compiler_common *common, int length)
2082    {
2083    DEFINE_COMPILER;
2084    struct sljit_label *loop;
2085    int i;
2086    
2087    SLJIT_ASSERT(length > 1);
2088    /* OVECTOR(1) contains the "string begin - 1" constant. */
2089    if (length > 2)
2090      OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(1));
2091  if (length < 8)  if (length < 8)
2092    {    {
2093    for (i = 0; i < length; i++)    for (i = 2; i < length; i++)
2094      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);
2095    }    }
2096  else  else
2097    {    {
2098    GET_LOCAL_BASE(SLJIT_TEMPORARY_REG2, 0, OVECTOR_START - sizeof(sljit_w));    GET_LOCAL_BASE(TMP2, 0, OVECTOR_START + sizeof(sljit_sw));
2099    OP1(SLJIT_MOV, SLJIT_TEMPORARY_REG3, 0, SLJIT_IMM, length);    OP1(SLJIT_MOV, STACK_TOP, 0, SLJIT_IMM, length - 2);
2100    loop = LABEL();    loop = LABEL();
2101    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);
2102    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);
2103    JUMPTO(SLJIT_C_NOT_ZERO, loop);    JUMPTO(SLJIT_C_NOT_ZERO, loop);
2104    }    }
2105    
2106    OP1(SLJIT_MOV, STACK_TOP, 0, ARGUMENTS, 0);
2107    if (common->mark_ptr != 0)
2108      OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), common->mark_ptr, SLJIT_IMM, 0);
2109    if (common->control_head_ptr != 0)
2110      OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), common->control_head_ptr, SLJIT_IMM, 0);
2111    OP1(SLJIT_MOV, STACK_TOP, 0, SLJIT_MEM1(STACK_TOP), SLJIT_OFFSETOF(jit_arguments, stack));
2112    OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), common->start_ptr);
2113    OP1(SLJIT_MOV, STACK_TOP, 0, SLJIT_MEM1(STACK_TOP), SLJIT_OFFSETOF(struct sljit_stack, base));
2114    }
2115    
2116    static sljit_sw SLJIT_CALL do_search_mark(sljit_sw *current, const pcre_uchar *skip_arg)
2117    {
2118    while (current != NULL)
2119      {
2120      switch (current[-2])
2121        {
2122        case type_then_trap:
2123        break;
2124    
2125        case type_mark:
2126        if (STRCMP_UC_UC(skip_arg, (pcre_uchar *)current[-3]) == 0)
2127          return current[-4];
2128        break;
2129    
2130        default:
2131        SLJIT_ASSERT_STOP();
2132        break;
2133        }
2134      current = (sljit_sw*)current[-1];
2135      }
2136    return -1;
2137  }  }
2138    
2139  static SLJIT_INLINE void copy_ovector(compiler_common *common, int topbracket)  static SLJIT_INLINE void copy_ovector(compiler_common *common, int topbracket)
2140  {  {
2141  DEFINE_COMPILER;  DEFINE_COMPILER;
2142  struct sljit_label *loop;  struct sljit_label *loop;
2143  struct sljit_jump *earlyexit;  struct sljit_jump *early_quit;
2144    
2145  /* At this point we can freely use all registers. */  /* At this point we can freely use all registers. */
2146  OP1(SLJIT_MOV, SLJIT_SAVED_REG3, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(1));  OP1(SLJIT_MOV, SLJIT_SAVED_REG3, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(1));
2147  OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(1), STR_PTR, 0);  OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(1), STR_PTR, 0);
2148    
2149  OP1(SLJIT_MOV, SLJIT_TEMPORARY_REG1, 0, ARGUMENTS, 0);  OP1(SLJIT_MOV, SLJIT_SCRATCH_REG1, 0, ARGUMENTS, 0);
2150  if (common->mark_ptr != 0)  if (common->mark_ptr != 0)
2151    OP1(SLJIT_MOV, SLJIT_TEMPORARY_REG3, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), common->mark_ptr);    OP1(SLJIT_MOV, SLJIT_SCRATCH_REG3, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), common->mark_ptr);
2152  OP1(SLJIT_MOV_SI, SLJIT_TEMPORARY_REG2, 0, SLJIT_MEM1(SLJIT_TEMPORARY_REG1), SLJIT_OFFSETOF(jit_arguments, offsetcount));  OP1(SLJIT_MOV_SI, SLJIT_SCRATCH_REG2, 0, SLJIT_MEM1(SLJIT_SCRATCH_REG1), SLJIT_OFFSETOF(jit_arguments, offset_count));
2153  if (common->mark_ptr != 0)  if (common->mark_ptr != 0)
2154    OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_TEMPORARY_REG1), SLJIT_OFFSETOF(jit_arguments, mark_ptr), SLJIT_TEMPORARY_REG3, 0);    OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SCRATCH_REG1), SLJIT_OFFSETOF(jit_arguments, mark_ptr), SLJIT_SCRATCH_REG3, 0);
2155  OP2(SLJIT_SUB, SLJIT_TEMPORARY_REG3, 0, SLJIT_MEM1(SLJIT_TEMPORARY_REG1), SLJIT_OFFSETOF(jit_arguments, offsets), SLJIT_IMM, sizeof(int));  OP2(SLJIT_SUB, SLJIT_SCRATCH_REG3, 0, SLJIT_MEM1(SLJIT_SCRATCH_REG1), SLJIT_OFFSETOF(jit_arguments, offsets), SLJIT_IMM, sizeof(int));
2156  OP1(SLJIT_MOV, SLJIT_TEMPORARY_REG1, 0, SLJIT_MEM1(SLJIT_TEMPORARY_REG1), SLJIT_OFFSETOF(jit_arguments, begin));  OP1(SLJIT_MOV, SLJIT_SCRATCH_REG1, 0, SLJIT_MEM1(SLJIT_SCRATCH_REG1), SLJIT_OFFSETOF(jit_arguments, begin));
2157  GET_LOCAL_BASE(SLJIT_SAVED_REG1, 0, OVECTOR_START);  GET_LOCAL_BASE(SLJIT_SAVED_REG1, 0, OVECTOR_START);
2158  /* Unlikely, but possible */  /* Unlikely, but possible */
2159  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);
2160  loop = LABEL();  loop = LABEL();
2161  OP2(SLJIT_SUB, SLJIT_SAVED_REG2, 0, SLJIT_MEM1(SLJIT_SAVED_REG1), 0, SLJIT_TEMPORARY_REG1, 0);  OP2(SLJIT_SUB, SLJIT_SAVED_REG2, 0, SLJIT_MEM1(SLJIT_SAVED_REG1), 0, SLJIT_SCRATCH_REG1, 0);
2162  OP2(SLJIT_ADD, SLJIT_SAVED_REG1, 0, SLJIT_SAVED_REG1, 0, SLJIT_IMM, sizeof(sljit_w));  OP2(SLJIT_ADD, SLJIT_SAVED_REG1, 0, SLJIT_SAVED_REG1, 0, SLJIT_IMM, sizeof(sljit_sw));
2163  /* Copy the integer value to the output buffer */  /* Copy the integer value to the output buffer */
2164  #ifdef COMPILE_PCRE16  #if defined COMPILE_PCRE16 || defined COMPILE_PCRE32
2165  OP2(SLJIT_ASHR, SLJIT_SAVED_REG2, 0, SLJIT_SAVED_REG2, 0, SLJIT_IMM, 1);  OP2(SLJIT_ASHR, SLJIT_SAVED_REG2, 0, SLJIT_SAVED_REG2, 0, SLJIT_IMM, UCHAR_SHIFT);
2166  #endif  #endif
2167  OP1(SLJIT_MOVU_SI, SLJIT_MEM1(SLJIT_TEMPORARY_REG3), sizeof(int), SLJIT_SAVED_REG2, 0);  OP1(SLJIT_MOVU_SI, SLJIT_MEM1(SLJIT_SCRATCH_REG3), sizeof(int), SLJIT_SAVED_REG2, 0);
2168  OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_TEMPORARY_REG2, 0, SLJIT_TEMPORARY_REG2, 0, SLJIT_IMM, 1);  OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_SCRATCH_REG2, 0, SLJIT_SCRATCH_REG2, 0, SLJIT_IMM, 1);
2169  JUMPTO(SLJIT_C_NOT_ZERO, loop);  JUMPTO(SLJIT_C_NOT_ZERO, loop);
2170  JUMPHERE(earlyexit);  JUMPHERE(early_quit);
2171    
2172  /* Calculate the return value, which is the maximum ovector value. */  /* Calculate the return value, which is the maximum ovector value. */
2173  if (topbracket > 1)  if (topbracket > 1)
2174    {    {
2175    GET_LOCAL_BASE(SLJIT_TEMPORARY_REG1, 0, OVECTOR_START + topbracket * 2 * sizeof(sljit_w));    GET_LOCAL_BASE(SLJIT_SCRATCH_REG1, 0, OVECTOR_START + topbracket * 2 * sizeof(sljit_sw));
2176    OP1(SLJIT_MOV, SLJIT_TEMPORARY_REG2, 0, SLJIT_IMM, topbracket + 1);    OP1(SLJIT_MOV, SLJIT_SCRATCH_REG2, 0, SLJIT_IMM, topbracket + 1);
2177    
2178    /* OVECTOR(0) is never equal to SLJIT_SAVED_REG3. */    /* OVECTOR(0) is never equal to SLJIT_SAVED_REG3. */
2179    loop = LABEL();    loop = LABEL();
2180    OP1(SLJIT_MOVU, SLJIT_TEMPORARY_REG3, 0, SLJIT_MEM1(SLJIT_TEMPORARY_REG1), -(2 * (sljit_w)sizeof(sljit_w)));    OP1(SLJIT_MOVU, SLJIT_SCRATCH_REG3, 0, SLJIT_MEM1(SLJIT_SCRATCH_REG1), -(2 * (sljit_sw)sizeof(sljit_sw)));
2181    OP2(SLJIT_SUB, SLJIT_TEMPORARY_REG2, 0, SLJIT_TEMPORARY_REG2, 0, SLJIT_IMM, 1);    OP2(SLJIT_SUB, SLJIT_SCRATCH_REG2, 0, SLJIT_SCRATCH_REG2, 0, SLJIT_IMM, 1);
2182    CMPTO(SLJIT_C_EQUAL, SLJIT_TEMPORARY_REG3, 0, SLJIT_SAVED_REG3, 0, loop);    CMPTO(SLJIT_C_EQUAL, SLJIT_SCRATCH_REG3, 0, SLJIT_SAVED_REG3, 0, loop);
2183    OP1(SLJIT_MOV, SLJIT_RETURN_REG, 0, SLJIT_TEMPORARY_REG2, 0);    OP1(SLJIT_MOV, SLJIT_RETURN_REG, 0, SLJIT_SCRATCH_REG2, 0);
2184    }    }
2185  else  else
2186    OP1(SLJIT_MOV, SLJIT_RETURN_REG, 0, SLJIT_IMM, 1);    OP1(SLJIT_MOV, SLJIT_RETURN_REG, 0, SLJIT_IMM, 1);
# Line 1813  else Line 2189  else
2189  static SLJIT_INLINE void return_with_partial_match(compiler_common *common, struct sljit_label *quit)  static SLJIT_INLINE void return_with_partial_match(compiler_common *common, struct sljit_label *quit)
2190  {  {
2191  DEFINE_COMPILER;  DEFINE_COMPILER;
2192    struct sljit_jump *jump;
2193    
2194  SLJIT_COMPILE_ASSERT(STR_END == SLJIT_SAVED_REG2, str_end_must_be_saved_reg2);  SLJIT_COMPILE_ASSERT(STR_END == SLJIT_SAVED_REG2, str_end_must_be_saved_reg2);
2195  SLJIT_ASSERT(common->start_used_ptr != 0 && (common->mode == JIT_PARTIAL_SOFT_COMPILE ? common->hit_start != 0 : common->hit_start == 0));  SLJIT_ASSERT(common->start_used_ptr != 0 && common->start_ptr != 0
2196      && (common->mode == JIT_PARTIAL_SOFT_COMPILE ? common->hit_start != 0 : common->hit_start == 0));
2197    
2198  OP1(SLJIT_MOV, SLJIT_TEMPORARY_REG2, 0, ARGUMENTS, 0);  OP1(SLJIT_MOV, SLJIT_SCRATCH_REG2, 0, ARGUMENTS, 0);
2199  OP1(SLJIT_MOV, SLJIT_RETURN_REG, 0, SLJIT_IMM, PCRE_ERROR_PARTIAL);  OP1(SLJIT_MOV, SLJIT_RETURN_REG, 0, SLJIT_IMM, PCRE_ERROR_PARTIAL);
2200  OP1(SLJIT_MOV_SI, SLJIT_TEMPORARY_REG3, 0, SLJIT_MEM1(SLJIT_TEMPORARY_REG2), SLJIT_OFFSETOF(jit_arguments, offsetcount));  OP1(SLJIT_MOV_SI, SLJIT_SCRATCH_REG3, 0, SLJIT_MEM1(SLJIT_SCRATCH_REG2), SLJIT_OFFSETOF(jit_arguments, real_offset_count));
2201  CMPTO(SLJIT_C_LESS, SLJIT_TEMPORARY_REG3, 0, SLJIT_IMM, 2, quit);  CMPTO(SLJIT_C_SIG_LESS, SLJIT_SCRATCH_REG3, 0, SLJIT_IMM, 2, quit);
2202    
2203  /* Store match begin and end. */  /* Store match begin and end. */
2204  OP1(SLJIT_MOV, SLJIT_SAVED_REG1, 0, SLJIT_MEM1(SLJIT_TEMPORARY_REG2), SLJIT_OFFSETOF(jit_arguments, begin));  OP1(SLJIT_MOV, SLJIT_SAVED_REG1, 0, SLJIT_MEM1(SLJIT_SCRATCH_REG2), SLJIT_OFFSETOF(jit_arguments, begin));
2205  OP1(SLJIT_MOV, SLJIT_TEMPORARY_REG2, 0, SLJIT_MEM1(SLJIT_TEMPORARY_REG2), SLJIT_OFFSETOF(jit_arguments, offsets));  OP1(SLJIT_MOV, SLJIT_SCRATCH_REG2, 0, SLJIT_MEM1(SLJIT_SCRATCH_REG2), SLJIT_OFFSETOF(jit_arguments, offsets));
2206  OP1(SLJIT_MOV, SLJIT_TEMPORARY_REG3, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), common->mode == JIT_PARTIAL_HARD_COMPILE ? common->start_used_ptr : common->hit_start);  
2207    jump = CMP(SLJIT_C_SIG_LESS, SLJIT_SCRATCH_REG3, 0, SLJIT_IMM, 3);
2208    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);
2209    #if defined COMPILE_PCRE16 || defined COMPILE_PCRE32
2210    OP2(SLJIT_ASHR, SLJIT_SCRATCH_REG3, 0, SLJIT_SCRATCH_REG3, 0, SLJIT_IMM, UCHAR_SHIFT);
2211    #endif
2212    OP1(SLJIT_MOV_SI, SLJIT_MEM1(SLJIT_SCRATCH_REG2), 2 * sizeof(int), SLJIT_SCRATCH_REG3, 0);
2213    JUMPHERE(jump);
2214    
2215    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);
2216  OP2(SLJIT_SUB, SLJIT_SAVED_REG2, 0, STR_END, 0, SLJIT_SAVED_REG1, 0);  OP2(SLJIT_SUB, SLJIT_SAVED_REG2, 0, STR_END, 0, SLJIT_SAVED_REG1, 0);
2217  #ifdef COMPILE_PCRE16  #if defined COMPILE_PCRE16 || defined COMPILE_PCRE32
2218  OP2(SLJIT_ASHR, SLJIT_SAVED_REG2, 0, SLJIT_SAVED_REG2, 0, SLJIT_IMM, 1);  OP2(SLJIT_ASHR, SLJIT_SAVED_REG2, 0, SLJIT_SAVED_REG2, 0, SLJIT_IMM, UCHAR_SHIFT);
2219  #endif  #endif
2220  OP1(SLJIT_MOV_SI, SLJIT_MEM1(SLJIT_TEMPORARY_REG2), sizeof(int), SLJIT_SAVED_REG2, 0);  OP1(SLJIT_MOV_SI, SLJIT_MEM1(SLJIT_SCRATCH_REG2), sizeof(int), SLJIT_SAVED_REG2, 0);
2221    
2222  OP2(SLJIT_SUB, SLJIT_TEMPORARY_REG3, 0, SLJIT_TEMPORARY_REG3, 0, SLJIT_SAVED_REG1, 0);  OP2(SLJIT_SUB, SLJIT_SCRATCH_REG3, 0, SLJIT_SCRATCH_REG3, 0, SLJIT_SAVED_REG1, 0);
2223  #ifdef COMPILE_PCRE16  #if defined COMPILE_PCRE16 || defined COMPILE_PCRE32
2224  OP2(SLJIT_ASHR, SLJIT_TEMPORARY_REG3, 0, SLJIT_TEMPORARY_REG3, 0, SLJIT_IMM, 1);  OP2(SLJIT_ASHR, SLJIT_SCRATCH_REG3, 0, SLJIT_SCRATCH_REG3, 0, SLJIT_IMM, UCHAR_SHIFT);
2225  #endif  #endif
2226  OP1(SLJIT_MOV_SI, SLJIT_MEM1(SLJIT_TEMPORARY_REG2), 0, SLJIT_TEMPORARY_REG3, 0);  OP1(SLJIT_MOV_SI, SLJIT_MEM1(SLJIT_SCRATCH_REG2), 0, SLJIT_SCRATCH_REG3, 0);
2227    
2228  JUMPTO(SLJIT_JUMP, quit);  JUMPTO(SLJIT_JUMP, quit);
2229  }  }
# Line 1949  if (c <= 127 && bit == 0x20) Line 2336  if (c <= 127 && bit == 0x20)
2336    return (0 << 8) | 0x20;    return (0 << 8) | 0x20;
2337    
2338  /* Since c != oc, they must have at least 1 bit difference. */  /* Since c != oc, they must have at least 1 bit difference. */
2339  if (!ispowerof2(bit))  if (!is_powerof2(bit))
2340    return 0;    return 0;
2341    
2342  #ifdef COMPILE_PCRE8  #if defined COMPILE_PCRE8
2343    
2344  #ifdef SUPPORT_UTF  #ifdef SUPPORT_UTF
2345  if (common->utf && c > 127)  if (common->utf && c > 127)
# Line 1968  if (common->utf && c > 127) Line 2355  if (common->utf && c > 127)
2355  #endif /* SUPPORT_UTF */  #endif /* SUPPORT_UTF */
2356  return (0 << 8) | bit;  return (0 << 8) | bit;
2357    
2358  #else /* COMPILE_PCRE8 */  #elif defined COMPILE_PCRE16 || defined COMPILE_PCRE32
2359    
 #ifdef COMPILE_PCRE16  
2360  #ifdef SUPPORT_UTF  #ifdef SUPPORT_UTF
2361  if (common->utf && c > 65535)  if (common->utf && c > 65535)
2362    {    {
# Line 1981  if (common->utf && c > 65535) Line 2367  if (common->utf && c > 65535)
2367    }    }
2368  #endif /* SUPPORT_UTF */  #endif /* SUPPORT_UTF */
2369  return (bit < 256) ? ((0 << 8) | bit) : ((1 << 8) | (bit >> 8));  return (bit < 256) ? ((0 << 8) | bit) : ((1 << 8) | (bit >> 8));
 #endif /* COMPILE_PCRE16 */  
2370    
2371  #endif /* COMPILE_PCRE8 */  #endif /* COMPILE_PCRE[8|16|32] */
2372  }  }
2373    
2374  static void check_partial(compiler_common *common, BOOL force)  static void check_partial(compiler_common *common, BOOL force)
2375  {  {
2376  /* Checks whether a partial matching is occured. Does not modify registers. */  /* Checks whether a partial matching is occurred. Does not modify registers. */
2377  DEFINE_COMPILER;  DEFINE_COMPILER;
2378  struct sljit_jump *jump = NULL;  struct sljit_jump *jump = NULL;
2379    
# Line 2003  else if (common->mode == JIT_PARTIAL_SOF Line 2388  else if (common->mode == JIT_PARTIAL_SOF
2388    jump = CMP(SLJIT_C_EQUAL, SLJIT_MEM1(SLJIT_LOCALS_REG), common->start_used_ptr, SLJIT_IMM, -1);    jump = CMP(SLJIT_C_EQUAL, SLJIT_MEM1(SLJIT_LOCALS_REG), common->start_used_ptr, SLJIT_IMM, -1);
2389    
2390  if (common->mode == JIT_PARTIAL_SOFT_COMPILE)  if (common->mode == JIT_PARTIAL_SOFT_COMPILE)
2391    OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), common->hit_start, SLJIT_IMM, -1);    OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), common->hit_start, SLJIT_IMM, 0);
2392  else  else
2393    {    {
2394    if (common->partialmatchlabel != NULL)    if (common->partialmatchlabel != NULL)
# Line 2016  if (jump != NULL) Line 2401  if (jump != NULL)
2401    JUMPHERE(jump);    JUMPHERE(jump);
2402  }  }
2403    
2404  static struct sljit_jump *check_str_end(compiler_common *common)  static void check_str_end(compiler_common *common, jump_list **end_reached)
2405  {  {
2406  /* Does not affect registers. Usually used in a tight spot. */  /* Does not affect registers. Usually used in a tight spot. */
2407  DEFINE_COMPILER;  DEFINE_COMPILER;
2408  struct sljit_jump *jump;  struct sljit_jump *jump;
 struct sljit_jump *nohit;  
 struct sljit_jump *return_value;  
2409    
2410  if (common->mode == JIT_COMPILE)  if (common->mode == JIT_COMPILE)
2411    return CMP(SLJIT_C_GREATER_EQUAL, STR_PTR, 0, STR_END, 0);    {
2412      add_jump(compiler, end_reached, CMP(SLJIT_C_GREATER_EQUAL, STR_PTR, 0, STR_END, 0));
2413      return;
2414      }
2415    
2416  jump = CMP(SLJIT_C_LESS, STR_PTR, 0, STR_END, 0);  jump = CMP(SLJIT_C_LESS, STR_PTR, 0, STR_END, 0);
2417  if (common->mode == JIT_PARTIAL_SOFT_COMPILE)  if (common->mode == JIT_PARTIAL_SOFT_COMPILE)
2418    {    {
2419    nohit = CMP(SLJIT_C_GREATER_EQUAL, SLJIT_MEM1(SLJIT_LOCALS_REG), common->start_used_ptr, STR_PTR, 0);    add_jump(compiler, end_reached, CMP(SLJIT_C_GREATER_EQUAL, SLJIT_MEM1(SLJIT_LOCALS_REG), common->start_used_ptr, STR_PTR, 0));
2420    OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), common->hit_start, SLJIT_IMM, -1);    OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), common->hit_start, SLJIT_IMM, 0);
2421    JUMPHERE(nohit);    add_jump(compiler, end_reached, JUMP(SLJIT_JUMP));
   return_value = JUMP(SLJIT_JUMP);  
2422    }    }
2423  else  else
2424    {    {
2425    return_value = CMP(SLJIT_C_GREATER_EQUAL, SLJIT_MEM1(SLJIT_LOCALS_REG), common->start_used_ptr, STR_PTR, 0);    add_jump(compiler, end_reached, CMP(SLJIT_C_GREATER_EQUAL, SLJIT_MEM1(SLJIT_LOCALS_REG), common->start_used_ptr, STR_PTR, 0));
2426    if (common->partialmatchlabel != NULL)    if (common->partialmatchlabel != NULL)
2427      JUMPTO(SLJIT_JUMP, common->partialmatchlabel);      JUMPTO(SLJIT_JUMP, common->partialmatchlabel);
2428    else    else
2429      add_jump(compiler, &common->partialmatch, JUMP(SLJIT_JUMP));      add_jump(compiler, &common->partialmatch, JUMP(SLJIT_JUMP));
2430    }    }
2431  JUMPHERE(jump);  JUMPHERE(jump);
 return return_value;  
2432  }  }
2433    
2434  static void detect_partial_match(compiler_common *common, jump_list **backtracks)  static void detect_partial_match(compiler_common *common, jump_list **backtracks)
# Line 2063  jump = CMP(SLJIT_C_LESS, STR_PTR, 0, STR Line 2447  jump = CMP(SLJIT_C_LESS, STR_PTR, 0, STR
2447  add_jump(compiler, backtracks, CMP(SLJIT_C_GREATER_EQUAL, SLJIT_MEM1(SLJIT_LOCALS_REG), common->start_used_ptr, STR_PTR, 0));  add_jump(compiler, backtracks, CMP(SLJIT_C_GREATER_EQUAL, SLJIT_MEM1(SLJIT_LOCALS_REG), common->start_used_ptr, STR_PTR, 0));
2448  if (common->mode == JIT_PARTIAL_SOFT_COMPILE)  if (common->mode == JIT_PARTIAL_SOFT_COMPILE)
2449    {    {
2450    OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), common->hit_start, SLJIT_IMM, -1);    OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), common->hit_start, SLJIT_IMM, 0);
2451    add_jump(compiler, backtracks, JUMP(SLJIT_JUMP));    add_jump(compiler, backtracks, JUMP(SLJIT_JUMP));
2452    }    }
2453  else  else
2454    {    {
2455    if (common->partialmatchlabel != NULL)    if (common->partialmatchlabel != NULL)
2456      JUMPTO(SLJIT_JUMP, common->partialmatchlabel);      JUMPTO(SLJIT_JUMP, common->partialmatchlabel);
2457    else    else
2458      add_jump(compiler, &common->partialmatch, JUMP(SLJIT_JUMP));      add_jump(compiler, &common->partialmatch, JUMP(SLJIT_JUMP));
2459      }
2460    JUMPHERE(jump);
2461    }
2462    
2463    static void peek_char(compiler_common *common)
2464    {
2465    /* Reads the character into TMP1, keeps STR_PTR.
2466    Does not check STR_END. TMP2 Destroyed. */
2467    DEFINE_COMPILER;
2468    #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
2469    struct sljit_jump *jump;
2470    #endif
2471    
2472    OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), 0);
2473    #if defined SUPPORT_UTF && defined COMPILE_PCRE8
2474    if (common->utf)
2475      {
2476      jump = CMP(SLJIT_C_LESS, TMP1, 0, SLJIT_IMM, 0xc0);
2477      OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
2478      add_jump(compiler, &common->utfreadchar, JUMP(SLJIT_FAST_CALL));
2479      OP2(SLJIT_SUB, STR_PTR, 0, STR_PTR, 0, TMP2, 0);
2480      JUMPHERE(jump);
2481      }
2482    #endif /* SUPPORT_UTF && !COMPILE_PCRE32 */
2483    
2484    #if defined SUPPORT_UTF && defined COMPILE_PCRE16
2485    if (common->utf)
2486      {
2487      OP2(SLJIT_SUB, TMP2, 0, TMP1, 0, SLJIT_IMM, 0xd800);
2488      jump = CMP(SLJIT_C_GREATER, TMP2, 0, SLJIT_IMM, 0xdc00 - 0xd800 - 1);
2489      /* TMP2 contains the high surrogate. */
2490      OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(0));
2491      OP2(SLJIT_ADD, TMP2, 0, TMP2, 0, SLJIT_IMM, 0x40);
2492      OP2(SLJIT_SHL, TMP2, 0, TMP2, 0, SLJIT_IMM, 10);
2493      OP2(SLJIT_AND, TMP1, 0, TMP1, 0, SLJIT_IMM, 0x3ff);
2494      OP2(SLJIT_OR, TMP1, 0, TMP1, 0, TMP2, 0);
2495      JUMPHERE(jump);
2496      }
2497    #endif
2498    }
2499    
2500    #if defined SUPPORT_UTF && defined COMPILE_PCRE8
2501    
2502    static BOOL is_char7_bitset(const pcre_uint8* bitset, BOOL nclass)
2503    {
2504    /* Tells whether the character codes below 128 are enough
2505    to determine a match. */
2506    const pcre_uint8 value = nclass ? 0xff : 0;
2507    const pcre_uint8* end = bitset + 32;
2508    
2509    bitset += 16;
2510    do
2511      {
2512      if (*bitset++ != value)
2513        return FALSE;
2514    }    }
2515  JUMPHERE(jump);  while (bitset < end);
2516    return TRUE;
2517  }  }
2518    
2519  static void read_char(compiler_common *common)  static void read_char7_type(compiler_common *common, BOOL full_read)
2520  {  {
2521  /* Reads the character into TMP1, updates STR_PTR.  /* Reads the precise character type of a character into TMP1, if the character
2522  Does not check STR_END. TMP2 Destroyed. */  is less than 128. Otherwise it returns with zero. Does not check STR_END. The
2523    full_read argument tells whether characters above max are accepted or not. */
2524  DEFINE_COMPILER;  DEFINE_COMPILER;
 #ifdef SUPPORT_UTF  
2525  struct sljit_jump *jump;  struct sljit_jump *jump;
 #endif  
2526    
2527  OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), 0);  SLJIT_ASSERT(common->utf);
2528  #ifdef SUPPORT_UTF  
2529  if (common->utf)  OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), 0);
2530    OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
2531    
2532    OP1(SLJIT_MOV_UB, TMP1, 0, SLJIT_MEM1(TMP2), common->ctypes);
2533    
2534    if (full_read)
2535    {    {
2536  #ifdef COMPILE_PCRE8    jump = CMP(SLJIT_C_LESS, TMP2, 0, SLJIT_IMM, 0xc0);
2537    jump = CMP(SLJIT_C_LESS, TMP1, 0, SLJIT_IMM, 0xc0);    OP1(SLJIT_MOV_UB, TMP2, 0, SLJIT_MEM1(TMP2), (sljit_sw)PRIV(utf8_table4) - 0xc0);
2538  #else    OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, TMP2, 0);
 #ifdef COMPILE_PCRE16  
   jump = CMP(SLJIT_C_LESS, TMP1, 0, SLJIT_IMM, 0xd800);  
 #endif  
 #endif /* COMPILE_PCRE8 */  
   add_jump(compiler, &common->utfreadchar, JUMP(SLJIT_FAST_CALL));  
2539    JUMPHERE(jump);    JUMPHERE(jump);
2540    }    }
 #endif  
 OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));  
2541  }  }
2542    
2543  static void peek_char(compiler_common *common)  #endif /* SUPPORT_UTF && COMPILE_PCRE8 */
2544    
2545    static void read_char_max(compiler_common *common, pcre_uint32 max, BOOL full_read)
2546  {  {
2547  /* Reads the character into TMP1, keeps STR_PTR.  /* Reads the precise value of a character into TMP1, if the character is
2548  Does not check STR_END. TMP2 Destroyed. */  less than or equal to max. Otherwise it returns with a value greater than max.
2549    Does not check STR_END. The full_read argument tells whether characters above
2550    max are accepted or not. */
2551  DEFINE_COMPILER;  DEFINE_COMPILER;
2552  #ifdef SUPPORT_UTF  #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
2553  struct sljit_jump *jump;  struct sljit_jump *jump;
2554  #endif  #endif
2555    
2556    SLJIT_UNUSED_ARG(full_read);
2557    SLJIT_UNUSED_ARG(max);
2558    
2559  OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), 0);  OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), 0);
2560  #ifdef SUPPORT_UTF  OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
2561    
2562    #if defined SUPPORT_UTF && defined COMPILE_PCRE8
2563  if (common->utf)  if (common->utf)
2564    {    {
2565  #ifdef COMPILE_PCRE8    if (max < 128 && !full_read)
2566        return;
2567    
2568    jump = CMP(SLJIT_C_LESS, TMP1, 0, SLJIT_IMM, 0xc0);    jump = CMP(SLJIT_C_LESS, TMP1, 0, SLJIT_IMM, 0xc0);
2569  #else    if (max >= 0x800)
2570  #ifdef COMPILE_PCRE16      add_jump(compiler, (max < 0x10000) ? &common->utfreadchar16 : &common->utfreadchar, JUMP(SLJIT_FAST_CALL));
2571    jump = CMP(SLJIT_C_LESS, TMP1, 0, SLJIT_IMM, 0xd800);    else if (max < 128)
2572        {
2573        OP1(SLJIT_MOV_UB, TMP2, 0, SLJIT_MEM1(TMP1), (sljit_sw)PRIV(utf8_table4) - 0xc0);
2574        OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, TMP2, 0);
2575        }
2576      else
2577        {
2578        OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(0));
2579        if (!full_read)
2580          OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
2581        else
2582          OP1(SLJIT_MOV_UB, RETURN_ADDR, 0, SLJIT_MEM1(TMP1), (sljit_sw)PRIV(utf8_table4) - 0xc0);
2583        OP2(SLJIT_AND, TMP1, 0, TMP1, 0, SLJIT_IMM, 0x3f);
2584        OP2(SLJIT_SHL, TMP1, 0, TMP1, 0, SLJIT_IMM, 6);
2585        OP2(SLJIT_AND, TMP2, 0, TMP2, 0, SLJIT_IMM, 0x3f);
2586        OP2(SLJIT_OR, TMP1, 0, TMP1, 0, TMP2, 0);
2587        if (full_read)
2588          OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, RETURN_ADDR, 0);
2589        }
2590      JUMPHERE(jump);
2591      }
2592  #endif  #endif
2593  #endif /* COMPILE_PCRE8 */  
2594    add_jump(compiler, &common->utfreadchar, JUMP(SLJIT_FAST_CALL));  #if defined SUPPORT_UTF && defined COMPILE_PCRE16
2595    OP2(SLJIT_SUB, STR_PTR, 0, STR_PTR, 0, TMP2, 0);  if (common->utf)
2596      {
2597      if (max >= 0x10000)
2598        {
2599        OP2(SLJIT_SUB, TMP2, 0, TMP1, 0, SLJIT_IMM, 0xd800);
2600        jump = CMP(SLJIT_C_GREATER, TMP2, 0, SLJIT_IMM, 0xdc00 - 0xd800 - 1);
2601        /* TMP2 contains the high surrogate. */
2602        OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(0));
2603        OP2(SLJIT_ADD, TMP2, 0, TMP2, 0, SLJIT_IMM, 0x40);
2604        OP2(SLJIT_SHL, TMP2, 0, TMP2, 0, SLJIT_IMM, 10);
2605        OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
2606        OP2(SLJIT_AND, TMP1, 0, TMP1, 0, SLJIT_IMM, 0x3ff);
2607        OP2(SLJIT_OR, TMP1, 0, TMP1, 0, TMP2, 0);
2608        JUMPHERE(jump);
2609        return;
2610        }
2611    
2612      if (max < 0xd800 && !full_read)
2613        return;
2614    
2615      /* Skip low surrogate if necessary. */
2616      OP2(SLJIT_SUB, TMP2, 0, TMP1, 0, SLJIT_IMM, 0xd800);
2617      jump = CMP(SLJIT_C_GREATER, TMP2, 0, SLJIT_IMM, 0xdc00 - 0xd800 - 1);
2618      if (full_read)
2619        OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
2620      if (max >= 0xd800)
2621        OP1(SLJIT_MOV, TMP1, 0, SLJIT_IMM, 0x10000);
2622    JUMPHERE(jump);    JUMPHERE(jump);
2623    }    }
2624  #endif  #endif
2625  }  }
2626    
2627  static void read_char8_type(compiler_common *common)  static SLJIT_INLINE void read_char(compiler_common *common)
2628    {
2629    read_char_max(common, 0x7fffffff, TRUE);
2630    }
2631    
2632    static void read_char8_type(compiler_common *common, BOOL full_read)
2633  {  {
2634  /* 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.
2635    The full_read argument tells whether characters above max are accepted or not. */
2636  DEFINE_COMPILER;  DEFINE_COMPILER;
2637  #if defined SUPPORT_UTF || defined COMPILE_PCRE16  #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
2638  struct sljit_jump *jump;  struct sljit_jump *jump;
2639  #endif  #endif
2640    #if defined SUPPORT_UTF && defined COMPILE_PCRE8
2641    struct sljit_jump *jump2;
2642    #endif
2643    
2644  #ifdef SUPPORT_UTF  SLJIT_UNUSED_ARG(full_read);
2645    
2646    OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), 0);
2647    OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
2648    
2649    #if defined SUPPORT_UTF && defined COMPILE_PCRE8
2650  if (common->utf)  if (common->utf)
2651    {    {
   OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), 0);  
   OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));  
 #ifdef COMPILE_PCRE8  
2652    /* This can be an extra read in some situations, but hopefully    /* This can be an extra read in some situations, but hopefully
2653    it is needed in most cases. */    it is needed in most cases. */
2654    OP1(SLJIT_MOV_UB, TMP1, 0, SLJIT_MEM1(TMP2), common->ctypes);    OP1(SLJIT_MOV_UB, TMP1, 0, SLJIT_MEM1(TMP2), common->ctypes);
2655    jump = CMP(SLJIT_C_LESS, TMP2, 0, SLJIT_IMM, 0xc0);    jump = CMP(SLJIT_C_LESS, TMP2, 0, SLJIT_IMM, 0xc0);
2656    add_jump(compiler, &common->utfreadtype8, JUMP(SLJIT_FAST_CALL));    if (!full_read)
2657    JUMPHERE(jump);      {
2658  #else      OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(0));
2659  #ifdef COMPILE_PCRE16      OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
2660    OP1(SLJIT_MOV, TMP1, 0, SLJIT_IMM, 0);      OP2(SLJIT_AND, TMP2, 0, TMP2, 0, SLJIT_IMM, 0x3f);
2661    jump = CMP(SLJIT_C_GREATER, TMP2, 0, SLJIT_IMM, 255);      OP2(SLJIT_SHL, TMP2, 0, TMP2, 0, SLJIT_IMM, 6);
2662    OP1(SLJIT_MOV_UB, TMP1, 0, SLJIT_MEM1(TMP2), common->ctypes);      OP2(SLJIT_AND, TMP1, 0, TMP1, 0, SLJIT_IMM, 0x3f);
2663        OP2(SLJIT_OR, TMP2, 0, TMP2, 0, TMP1, 0);
2664        OP1(SLJIT_MOV, TMP1, 0, SLJIT_IMM, 0);
2665        jump2 = CMP(SLJIT_C_GREATER, TMP2, 0, SLJIT_IMM, 255);
2666        OP1(SLJIT_MOV_UB, TMP1, 0, SLJIT_MEM1(TMP2), common->ctypes);
2667        JUMPHERE(jump2);
2668        }
2669      else
2670        add_jump(compiler, &common->utfreadtype8, JUMP(SLJIT_FAST_CALL));
2671    JUMPHERE(jump);    JUMPHERE(jump);
   /* Skip low surrogate if necessary. */  
   OP2(SLJIT_AND, TMP2, 0, TMP2, 0, SLJIT_IMM, 0xfc00);  
   OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP2, 0, SLJIT_IMM, 0xd800);  
   COND_VALUE(SLJIT_MOV, TMP2, 0, SLJIT_C_EQUAL);  
   OP2(SLJIT_SHL, TMP2, 0, TMP2, 0, SLJIT_IMM, 1);  
   OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, TMP2, 0);  
 #endif  
 #endif /* COMPILE_PCRE8 */  
2672    return;    return;
2673    }    }
2674  #endif  #endif /* SUPPORT_UTF && COMPILE_PCRE8 */
2675  OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), 0);  
2676  OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));  #if !defined COMPILE_PCRE8
 #ifdef COMPILE_PCRE16  
2677  /* The ctypes array contains only 256 values. */  /* The ctypes array contains only 256 values. */
2678  OP1(SLJIT_MOV, TMP1, 0, SLJIT_IMM, 0);  OP1(SLJIT_MOV, TMP1, 0, SLJIT_IMM, 0);
2679  jump = CMP(SLJIT_C_GREATER, TMP2, 0, SLJIT_IMM, 255);  jump = CMP(SLJIT_C_GREATER, TMP2, 0, SLJIT_IMM, 255);
2680  #endif  #endif
2681  OP1(SLJIT_MOV_UB, TMP1, 0, SLJIT_MEM1(TMP2), common->ctypes);  OP1(SLJIT_MOV_UB, TMP1, 0, SLJIT_MEM1(TMP2), common->ctypes);
2682  #ifdef COMPILE_PCRE16  #if !defined COMPILE_PCRE8
2683  JUMPHERE(jump);  JUMPHERE(jump);
2684  #endif  #endif
2685    
2686    #if defined SUPPORT_UTF && defined COMPILE_PCRE16
2687    if (common->utf && full_read)
2688      {
2689      /* Skip low surrogate if necessary. */
2690      OP2(SLJIT_SUB, TMP2, 0, TMP2, 0, SLJIT_IMM, 0xd800);
2691      jump = CMP(SLJIT_C_GREATER, TMP2, 0, SLJIT_IMM, 0xdc00 - 0xd800 - 1);
2692      OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
2693      JUMPHERE(jump);
2694      }
2695    #endif /* SUPPORT_UTF && COMPILE_PCRE16 */
2696  }  }
2697    
2698  static void skip_char_back(compiler_common *common)  static void skip_char_back(compiler_common *common)
2699  {  {
2700  /* Goes one character back. Affects STR_PTR and TMP1. Does not check begin. */  /* Goes one character back. Affects STR_PTR and TMP1. Does not check begin. */
2701  DEFINE_COMPILER;  DEFINE_COMPILER;
2702  #if defined SUPPORT_UTF && defined COMPILE_PCRE8  #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
2703    #if defined COMPILE_PCRE8
2704  struct sljit_label *label;  struct sljit_label *label;
2705    
2706  if (common->utf)  if (common->utf)
# Line 2196  if (common->utf) Line 2712  if (common->utf)
2712    CMPTO(SLJIT_C_EQUAL, TMP1, 0, SLJIT_IMM, 0x80, label);    CMPTO(SLJIT_C_EQUAL, TMP1, 0, SLJIT_IMM, 0x80, label);
2713    return;    return;
2714    }    }
2715  #endif  #elif defined COMPILE_PCRE16
 #if defined SUPPORT_UTF && defined COMPILE_PCRE16  
2716  if (common->utf)  if (common->utf)
2717    {    {
2718    OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), -IN_UCHARS(1));    OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), -IN_UCHARS(1));
# Line 2205  if (common->utf) Line 2720  if (common->utf)
2720    /* Skip low surrogate if necessary. */    /* Skip low surrogate if necessary. */
2721    OP2(SLJIT_AND, TMP1, 0, TMP1, 0, SLJIT_IMM, 0xfc00);    OP2(SLJIT_AND, TMP1, 0, TMP1, 0, SLJIT_IMM, 0xfc00);
2722    OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0xdc00);    OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0xdc00);
2723    COND_VALUE(SLJIT_MOV, TMP1, 0, SLJIT_C_EQUAL);    OP_FLAGS(SLJIT_MOV, TMP1, 0, SLJIT_UNUSED, 0, SLJIT_C_EQUAL);
2724    OP2(SLJIT_SHL, TMP1, 0, TMP1, 0, SLJIT_IMM, 1);    OP2(SLJIT_SHL, TMP1, 0, TMP1, 0, SLJIT_IMM, 1);
2725    OP2(SLJIT_SUB, STR_PTR, 0, STR_PTR, 0, TMP1, 0);    OP2(SLJIT_SUB, STR_PTR, 0, STR_PTR, 0, TMP1, 0);
2726    return;    return;
2727    }    }
2728  #endif  #endif /* COMPILE_PCRE[8|16] */
2729    #endif /* SUPPORT_UTF && !COMPILE_PCRE32 */
2730  OP2(SLJIT_SUB, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));  OP2(SLJIT_SUB, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
2731  }  }
2732    
# Line 2227  if (nltype == NLTYPE_ANY) Line 2743  if (nltype == NLTYPE_ANY)
2743  else if (nltype == NLTYPE_ANYCRLF)  else if (nltype == NLTYPE_ANYCRLF)
2744    {    {
2745    OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, CHAR_CR);    OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, CHAR_CR);
2746    COND_VALUE(SLJIT_MOV, TMP2, 0, SLJIT_C_EQUAL);    OP_FLAGS(SLJIT_MOV, TMP2, 0, SLJIT_UNUSED, 0, SLJIT_C_EQUAL);
2747    OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, CHAR_NL);    OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, CHAR_NL);
2748    COND_VALUE(SLJIT_OR | SLJIT_SET_E, TMP2, 0, SLJIT_C_EQUAL);    OP_FLAGS(SLJIT_OR | SLJIT_SET_E, TMP2, 0, TMP2, 0, SLJIT_C_EQUAL);
2749    add_jump(compiler, backtracks, JUMP(jumpiftrue ? SLJIT_C_NOT_ZERO : SLJIT_C_ZERO));    add_jump(compiler, backtracks, JUMP(jumpiftrue ? SLJIT_C_NOT_ZERO : SLJIT_C_ZERO));
2750    }    }
2751  else  else
# Line 2241  else Line 2757  else
2757    
2758  #ifdef SUPPORT_UTF  #ifdef SUPPORT_UTF
2759    
2760  #ifdef COMPILE_PCRE8  #if defined COMPILE_PCRE8
2761  static void do_utfreadchar(compiler_common *common)  static void do_utfreadchar(compiler_common *common)
2762  {  {
2763  /* Fast decoding a UTF-8 character. TMP1 contains the first byte  /* Fast decoding a UTF-8 character. TMP1 contains the first byte
2764  of the character (>= 0xc0). Return char value in TMP1, length - 1 in TMP2. */  of the character (>= 0xc0). Return char value in TMP1, length in TMP2. */
2765  DEFINE_COMPILER;  DEFINE_COMPILER;
2766  struct sljit_jump *jump;  struct sljit_jump *jump;
2767    
2768  sljit_emit_fast_enter(compiler, RETURN_ADDR, 0);  sljit_emit_fast_enter(compiler, RETURN_ADDR, 0);
2769    OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(0));
2770    OP2(SLJIT_AND, TMP1, 0, TMP1, 0, SLJIT_IMM, 0x3f);
2771    OP2(SLJIT_SHL, TMP1, 0, TMP1, 0, SLJIT_IMM, 6);
2772    OP2(SLJIT_AND, TMP2, 0, TMP2, 0, SLJIT_IMM, 0x3f);
2773    OP2(SLJIT_OR, TMP1, 0, TMP1, 0, TMP2, 0);
2774    
2775  /* Searching for the first zero. */  /* Searching for the first zero. */
2776  OP2(SLJIT_AND | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x20);  OP2(SLJIT_AND | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x800);
2777  jump = JUMP(SLJIT_C_NOT_ZERO);  jump = JUMP(SLJIT_C_NOT_ZERO);
2778  /* Two byte sequence. */  /* Two byte sequence. */
 OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(1));  
2779  OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));  OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
2780  OP2(SLJIT_AND, TMP1, 0, TMP1, 0, SLJIT_IMM, 0x1f);  OP1(SLJIT_MOV, TMP2, 0, SLJIT_IMM, IN_UCHARS(2));
2781    sljit_emit_fast_return(compiler, RETURN_ADDR, 0);
2782    
2783    JUMPHERE(jump);
2784    OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(1));
2785    OP2(SLJIT_XOR, TMP1, 0, TMP1, 0, SLJIT_IMM, 0x800);
2786  OP2(SLJIT_SHL, TMP1, 0, TMP1, 0, SLJIT_IMM, 6);  OP2(SLJIT_SHL, TMP1, 0, TMP1, 0, SLJIT_IMM, 6);
2787  OP2(SLJIT_AND, TMP2, 0, TMP2, 0, SLJIT_IMM, 0x3f);  OP2(SLJIT_AND, TMP2, 0, TMP2, 0, SLJIT_IMM, 0x3f);
2788  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, IN_UCHARS(1));  
 sljit_emit_fast_return(compiler, RETURN_ADDR, 0);  
 JUMPHERE(jump);  
2789    
2790  OP2(SLJIT_AND | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x10);  OP2(SLJIT_AND | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x10000);
2791  jump = JUMP(SLJIT_C_NOT_ZERO);  jump = JUMP(SLJIT_C_NOT_ZERO);
2792  /* Three byte sequence. */  /* Three byte sequence. */
 OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(1));  
 OP2(SLJIT_AND, TMP1, 0, TMP1, 0, SLJIT_IMM, 0x0f);  
 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(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(2));  
2793  OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(2));  OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(2));
2794  OP2(SLJIT_AND, TMP2, 0, TMP2, 0, SLJIT_IMM, 0x3f);  OP1(SLJIT_MOV, TMP2, 0, SLJIT_IMM, IN_UCHARS(3));
 OP2(SLJIT_OR, TMP1, 0, TMP1, 0, TMP2, 0);  
 OP1(SLJIT_MOV, TMP2, 0, SLJIT_IMM, IN_UCHARS(2));  
2795  sljit_emit_fast_return(compiler, RETURN_ADDR, 0);  sljit_emit_fast_return(compiler, RETURN_ADDR, 0);
 JUMPHERE(jump);  
2796    
2797  /* Four byte sequence. */  /* Four byte sequence. */
2798  OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(1));  JUMPHERE(jump);
2799  OP2(SLJIT_AND, TMP1, 0, TMP1, 0, SLJIT_IMM, 0x07);  OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(2));
2800  OP2(SLJIT_SHL, TMP1, 0, TMP1, 0, SLJIT_IMM, 18);  OP2(SLJIT_XOR, TMP1, 0, TMP1, 0, SLJIT_IMM, 0x10000);
2801    OP2(SLJIT_SHL, TMP1, 0, TMP1, 0, SLJIT_IMM, 6);
2802    OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(3));
2803  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);  
2804  OP2(SLJIT_OR, TMP1, 0, TMP1, 0, TMP2, 0);  OP2(SLJIT_OR, TMP1, 0, TMP1, 0, TMP2, 0);
2805  OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(2));  OP1(SLJIT_MOV, TMP2, 0, SLJIT_IMM, IN_UCHARS(4));
2806    sljit_emit_fast_return(compiler, RETURN_ADDR, 0);
2807    }
2808    
2809    static void do_utfreadchar16(compiler_common *common)
2810    {
2811    /* Fast decoding a UTF-8 character. TMP1 contains the first byte
2812    of the character (>= 0xc0). Return value in TMP1. */
2813    DEFINE_COMPILER;
2814    struct sljit_jump *jump;
2815    
2816    sljit_emit_fast_enter(compiler, RETURN_ADDR, 0);
2817    OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(0));
2818    OP2(SLJIT_AND, TMP1, 0, TMP1, 0, SLJIT_IMM, 0x3f);
2819    OP2(SLJIT_SHL, TMP1, 0, TMP1, 0, SLJIT_IMM, 6);
2820  OP2(SLJIT_AND, TMP2, 0, TMP2, 0, SLJIT_IMM, 0x3f);  OP2(SLJIT_AND, TMP2, 0, TMP2, 0, SLJIT_IMM, 0x3f);
 OP2(SLJIT_SHL, TMP2, 0, TMP2, 0, SLJIT_IMM, 6);  
2821  OP2(SLJIT_OR, TMP1, 0, TMP1, 0, TMP2, 0);  OP2(SLJIT_OR, TMP1, 0, TMP1, 0, TMP2, 0);
2822  OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(3));  
2823  OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(3));  /* Searching for the first zero. */
2824    OP2(SLJIT_AND | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x800);
2825    jump = JUMP(SLJIT_C_NOT_ZERO);
2826    /* Two byte sequence. */
2827    OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
2828    sljit_emit_fast_return(compiler, RETURN_ADDR, 0);
2829    
2830    JUMPHERE(jump);
2831    OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(1));
2832    OP2(SLJIT_XOR, TMP1, 0, TMP1, 0, SLJIT_IMM, 0x800);
2833    OP2(SLJIT_SHL, TMP1, 0, TMP1, 0, SLJIT_IMM, 6);
2834  OP2(SLJIT_AND, TMP2, 0, TMP2, 0, SLJIT_IMM, 0x3f);  OP2(SLJIT_AND, TMP2, 0, TMP2, 0, SLJIT_IMM, 0x3f);
2835  OP2(SLJIT_OR, TMP1, 0, TMP1, 0, TMP2, 0);  OP2(SLJIT_OR, TMP1, 0, TMP1, 0, TMP2, 0);
2836  OP1(SLJIT_MOV, TMP2, 0, SLJIT_IMM, IN_UCHARS(3));  /* Three byte sequence. */
2837    OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(2));
2838  sljit_emit_fast_return(compiler, RETURN_ADDR, 0);  sljit_emit_fast_return(compiler, RETURN_ADDR, 0);
2839  }  }
2840    
# Line 2316  jump = JUMP(SLJIT_C_NOT_ZERO); Line 2854  jump = JUMP(SLJIT_C_NOT_ZERO);
2854  OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(0));  OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(0));
2855  OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));  OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
2856  OP2(SLJIT_AND, TMP2, 0, TMP2, 0, SLJIT_IMM, 0x1f);  OP2(SLJIT_AND, TMP2, 0, TMP2, 0, SLJIT_IMM, 0x1f);
2857    /* The upper 5 bits are known at this point. */
2858    compare = CMP(SLJIT_C_GREATER, TMP2, 0, SLJIT_IMM, 0x3);
2859  OP2(SLJIT_SHL, TMP2, 0, TMP2, 0, SLJIT_IMM, 6);  OP2(SLJIT_SHL, TMP2, 0, TMP2, 0, SLJIT_IMM, 6);
2860  OP2(SLJIT_AND, TMP1, 0, TMP1, 0, SLJIT_IMM, 0x3f);  OP2(SLJIT_AND, TMP1, 0, TMP1, 0, SLJIT_IMM, 0x3f);
2861  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);  
2862  OP1(SLJIT_MOV_UB, TMP1, 0, SLJIT_MEM1(TMP2), common->ctypes);  OP1(SLJIT_MOV_UB, TMP1, 0, SLJIT_MEM1(TMP2), common->ctypes);
2863  sljit_emit_fast_return(compiler, RETURN_ADDR, 0);  sljit_emit_fast_return(compiler, RETURN_ADDR, 0);
2864    
2865  JUMPHERE(compare);  JUMPHERE(compare);
2866  OP1(SLJIT_MOV, TMP1, 0, SLJIT_IMM, 0);  OP1(SLJIT_MOV, TMP1, 0, SLJIT_IMM, 0);
2867  sljit_emit_fast_return(compiler, RETURN_ADDR, 0);  sljit_emit_fast_return(compiler, RETURN_ADDR, 0);
 JUMPHERE(jump);  
2868    
2869  /* We only have types for characters less than 256. */  /* We only have types for characters less than 256. */
 OP1(SLJIT_MOV_UB, TMP1, 0, SLJIT_MEM1(TMP2), (sljit_w)PRIV(utf8_table4) - 0xc0);  
 OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, TMP1, 0);  
 OP1(SLJIT_MOV, TMP1, 0, SLJIT_IMM, 0);  
 sljit_emit_fast_return(compiler, RETURN_ADDR, 0);  
 }  
   
 #else /* COMPILE_PCRE8 */  
   
 #ifdef COMPILE_PCRE16  
 static void do_utfreadchar(compiler_common *common)  
 {  
 /* Fast decoding a UTF-16 character. TMP1 contains the first 16 bit char  
 of the character (>= 0xd800). Return char value in TMP1, length - 1 in TMP2. */  
 DEFINE_COMPILER;  
 struct sljit_jump *jump;  
   
 sljit_emit_fast_enter(compiler, RETURN_ADDR, 0);  
 jump = CMP(SLJIT_C_LESS, TMP1, 0, SLJIT_IMM, 0xdc00);  
 /* Do nothing, only return. */  
 sljit_emit_fast_return(compiler, RETURN_ADDR, 0);  
   
2870  JUMPHERE(jump);  JUMPHERE(jump);
2871  /* Combine two 16 bit characters. */  OP1(SLJIT_MOV_UB, TMP2, 0, SLJIT_MEM1(TMP2), (sljit_sw)PRIV(utf8_table4) - 0xc0);
2872  OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(1));  OP1(SLJIT_MOV, TMP1, 0, SLJIT_IMM, 0);
2873  OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));  OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, TMP2, 0);
 OP2(SLJIT_AND, TMP1, 0, TMP1, 0, SLJIT_IMM, 0x3ff);  
 OP2(SLJIT_SHL, TMP1, 0, TMP1, 0, SLJIT_IMM, 10);  
 OP2(SLJIT_AND, TMP2, 0, TMP2, 0, SLJIT_IMM, 0x3ff);  
 OP2(SLJIT_OR, TMP1, 0, TMP1, 0, TMP2, 0);  
 OP1(SLJIT_MOV, TMP2, 0, SLJIT_IMM, IN_UCHARS(1));  
 OP2(SLJIT_ADD, TMP1, 0, TMP1, 0, SLJIT_IMM, 0x10000);  
2874  sljit_emit_fast_return(compiler, RETURN_ADDR, 0);  sljit_emit_fast_return(compiler, RETURN_ADDR, 0);
2875  }  }
 #endif /* COMPILE_PCRE16 */  
2876    
2877  #endif /* COMPILE_PCRE8 */  #endif /* COMPILE_PCRE8 */
2878    
# Line 2384  SLJIT_ASSERT(UCD_BLOCK_SIZE == 128 && si Line 2894  SLJIT_ASSERT(UCD_BLOCK_SIZE == 128 && si
2894    
2895  sljit_emit_fast_enter(compiler, RETURN_ADDR, 0);  sljit_emit_fast_enter(compiler, RETURN_ADDR, 0);
2896  OP2(SLJIT_LSHR, TMP2, 0, TMP1, 0, SLJIT_IMM, UCD_BLOCK_SHIFT);  OP2(SLJIT_LSHR, TMP2, 0, TMP1, 0, SLJIT_IMM, UCD_BLOCK_SHIFT);
2897  OP1(SLJIT_MOV_UB, TMP2, 0, SLJIT_MEM1(TMP2), (sljit_w)PRIV(ucd_stage1));  OP1(SLJIT_MOV_UB, TMP2, 0, SLJIT_MEM1(TMP2), (sljit_sw)PRIV(ucd_stage1));
2898  OP2(SLJIT_AND, TMP1, 0, TMP1, 0, SLJIT_IMM, UCD_BLOCK_MASK);  OP2(SLJIT_AND, TMP1, 0, TMP1, 0, SLJIT_IMM, UCD_BLOCK_MASK);
2899  OP2(SLJIT_SHL, TMP2, 0, TMP2, 0, SLJIT_IMM, UCD_BLOCK_SHIFT);  OP2(SLJIT_SHL, TMP2, 0, TMP2, 0, SLJIT_IMM, UCD_BLOCK_SHIFT);
2900  OP2(SLJIT_ADD, TMP1, 0, TMP1, 0, TMP2, 0);  OP2(SLJIT_ADD, TMP1, 0, TMP1, 0, TMP2, 0);
2901  OP1(SLJIT_MOV, TMP2, 0, SLJIT_IMM, (sljit_w)PRIV(ucd_stage2));  OP1(SLJIT_MOV, TMP2, 0, SLJIT_IMM, (sljit_sw)PRIV(ucd_stage2));
2902  OP1(SLJIT_MOV_UH, TMP2, 0, SLJIT_MEM2(TMP2, TMP1), 1);  OP1(SLJIT_MOV_UH, TMP2, 0, SLJIT_MEM2(TMP2, TMP1), 1);
2903  OP1(SLJIT_MOV, TMP1, 0, SLJIT_IMM, (sljit_w)PRIV(ucd_records) + SLJIT_OFFSETOF(ucd_record, chartype));  OP1(SLJIT_MOV, TMP1, 0, SLJIT_IMM, (sljit_sw)PRIV(ucd_records) + SLJIT_OFFSETOF(ucd_record, chartype));
2904  OP1(SLJIT_MOV_UB, TMP1, 0, SLJIT_MEM2(TMP1, TMP2), 3);  OP1(SLJIT_MOV_UB, TMP1, 0, SLJIT_MEM2(TMP1, TMP2), 3);
2905  sljit_emit_fast_return(compiler, RETURN_ADDR, 0);  sljit_emit_fast_return(compiler, RETURN_ADDR, 0);
2906  }  }
# Line 2404  struct sljit_label *newlinelabel = NULL; Line 2914  struct sljit_label *newlinelabel = NULL;
2914  struct sljit_jump *start;  struct sljit_jump *start;
2915  struct sljit_jump *end = NULL;  struct sljit_jump *end = NULL;
2916  struct sljit_jump *nl = NULL;  struct sljit_jump *nl = NULL;
2917  #ifdef SUPPORT_UTF  #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
2918  struct sljit_jump *singlechar;  struct sljit_jump *singlechar;
2919  #endif  #endif
2920  jump_list *newline = NULL;  jump_list *newline = NULL;
# Line 2459  if (newlinecheck) Line 2969  if (newlinecheck)
2969    end = CMP(SLJIT_C_GREATER_EQUAL, STR_PTR, 0, STR_END, 0);    end = CMP(SLJIT_C_GREATER_EQUAL, STR_PTR, 0, STR_END, 0);
2970    OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), 0);    OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), 0);
2971    OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, common->newline & 0xff);    OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, common->newline & 0xff);
2972    COND_VALUE(SLJIT_MOV, TMP1, 0, SLJIT_C_EQUAL);    OP_FLAGS(SLJIT_MOV, TMP1, 0, SLJIT_UNUSED, 0, SLJIT_C_EQUAL);
2973  #ifdef COMPILE_PCRE16  #if defined COMPILE_PCRE16 || defined COMPILE_PCRE32
2974    OP2(SLJIT_SHL, TMP1, 0, TMP1, 0, SLJIT_IMM, 1);    OP2(SLJIT_SHL, TMP1, 0, TMP1, 0, SLJIT_IMM, UCHAR_SHIFT);
2975  #endif  #endif
2976    OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, TMP1, 0);    OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, TMP1, 0);
2977    nl = JUMP(SLJIT_JUMP);    nl = JUMP(SLJIT_JUMP);
# Line 2482  if (newlinecheck) Line 2992  if (newlinecheck)
2992    CMPTO(SLJIT_C_EQUAL, TMP1, 0, SLJIT_IMM, (common->newline >> 8) & 0xff, newlinelabel);    CMPTO(SLJIT_C_EQUAL, TMP1, 0, SLJIT_IMM, (common->newline >> 8) & 0xff, newlinelabel);
2993    
2994  OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));  OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
2995  #if defined SUPPORT_UTF && defined COMPILE_PCRE8  #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
2996    #if defined COMPILE_PCRE8
2997  if (common->utf)  if (common->utf)
2998    {    {
2999    singlechar = CMP(SLJIT_C_LESS, TMP1, 0, SLJIT_IMM, 0xc0);    singlechar = CMP(SLJIT_C_LESS, TMP1, 0, SLJIT_IMM, 0xc0);
3000    OP1(SLJIT_MOV_UB, TMP1, 0, SLJIT_MEM1(TMP1), (sljit_w)PRIV(utf8_table4) - 0xc0);    OP1(SLJIT_MOV_UB, TMP1, 0, SLJIT_MEM1(TMP1), (sljit_sw)PRIV(utf8_table4) - 0xc0);
3001    OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, TMP1, 0);    OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, TMP1, 0);
3002    JUMPHERE(singlechar);    JUMPHERE(singlechar);
3003    }    }
3004  #endif  #elif defined COMPILE_PCRE16
 #if defined SUPPORT_UTF && defined COMPILE_PCRE16  
3005  if (common->utf)  if (common->utf)
3006    {    {
3007    singlechar = CMP(SLJIT_C_LESS, TMP1, 0, SLJIT_IMM, 0xd800);    singlechar = CMP(SLJIT_C_LESS, TMP1, 0, SLJIT_IMM, 0xd800);
3008    OP2(SLJIT_AND, TMP1, 0, TMP1, 0, SLJIT_IMM, 0xfc00);    OP2(SLJIT_AND, TMP1, 0, TMP1, 0, SLJIT_IMM, 0xfc00);
3009    OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0xd800);    OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0xd800);
3010    COND_VALUE(SLJIT_MOV, TMP1, 0, SLJIT_C_EQUAL);    OP_FLAGS(SLJIT_MOV, TMP1, 0, SLJIT_UNUSED, 0, SLJIT_C_EQUAL);
3011    OP2(SLJIT_SHL, TMP1, 0, TMP1, 0, SLJIT_IMM, 1);    OP2(SLJIT_SHL, TMP1, 0, TMP1, 0, SLJIT_IMM, 1);
3012    OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, TMP1, 0);    OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, TMP1, 0);
3013    JUMPHERE(singlechar);    JUMPHERE(singlechar);
3014    }    }
3015  #endif  #endif /* COMPILE_PCRE[8|16] */
3016    #endif /* SUPPORT_UTF && !COMPILE_PCRE32 */
3017  JUMPHERE(start);  JUMPHERE(start);
3018    
3019  if (newlinecheck)  if (newlinecheck)
# Line 2514  if (newlinecheck) Line 3025  if (newlinecheck)
3025  return mainloop;  return mainloop;
3026  }  }
3027    
3028  static SLJIT_INLINE BOOL fast_forward_first_two_chars(compiler_common *common, BOOL firstline)  static int scan_prefix(compiler_common *common, pcre_uchar *cc, pcre_uint32 *chars, int max_chars)
3029  {  {
3030  DEFINE_COMPILER;  /* Recursive function, which scans prefix literals. */
3031  struct sljit_label *start;  int len, repeat, len_save, consumed = 0;
3032  struct sljit_jump *quit;  pcre_uint32 caseless, chr, mask;
3033  struct sljit_jump *found;  pcre_uchar *alternative, *cc_save;
3034  pcre_int32 chars[4];  BOOL last, any;
 pcre_uchar *cc = common->start + 1 + IMM2_SIZE;  
 int location = 0;  
 pcre_int32 len, c, bit, caseless;  
 BOOL must_end;  
   
 #ifdef COMPILE_PCRE8  
 union {  
     sljit_uh ascombined;  
     sljit_ub asuchars[2];  
 } pair;  
 #else  
 union {  
     sljit_ui ascombined;  
     sljit_uh asuchars[2];  
 } pair;  
 #endif  
   
 if (*(common->start + GET(common->start, 1)) == OP_ALT)  
   return FALSE;  
3035    
3036    repeat = 1;
3037  while (TRUE)  while (TRUE)
3038    {    {
3039      last = TRUE;
3040      any = FALSE;
3041    caseless = 0;    caseless = 0;
3042    must_end = TRUE;    switch (*cc)
   switch(*cc)  
3043      {      {
     case OP_CHAR:  
     must_end = FALSE;  
     cc++;  
     break;  
   
3044      case OP_CHARI:      case OP_CHARI:
3045      caseless = 1;      caseless = 1;
3046      must_end = FALSE;      case OP_CHAR:
3047        last = FALSE;
3048      cc++;      cc++;
3049      break;      break;
3050    
# Line 2573  while (TRUE) Line 3063  while (TRUE)
3063      cc++;      cc++;
3064      continue;      continue;
3065    
3066      case OP_PLUS:      case OP_PLUS:
3067      case OP_MINPLUS:      case OP_MINPLUS:
3068      case OP_POSPLUS:      case OP_POSPLUS:
3069      cc++;      cc++;
3070        break;
3071    
3072        case OP_EXACTI:
3073        caseless = 1;
3074        case OP_EXACT:
3075        repeat = GET2(cc, 1);
3076        last = FALSE;
3077        cc += 1 + IMM2_SIZE;
3078        break;
3079    
3080        case OP_PLUSI:
3081        case OP_MINPLUSI:
3082        case OP_POSPLUSI:
3083        caseless = 1;
3084        cc++;
3085        break;
3086    
3087        case OP_KET:
3088        cc += 1 + LINK_SIZE;
3089        continue;
3090    
3091        case OP_ALT:
3092        cc += GET(cc, 1);
3093        continue;
3094    
3095        case OP_ONCE:
3096        case OP_ONCE_NC:
3097        case OP_BRA:
3098        case OP_BRAPOS:
3099        case OP_CBRA:
3100        case OP_CBRAPOS:
3101        alternative = cc + GET(cc, 1);
3102        while (*alternative == OP_ALT)
3103          {
3104          max_chars = scan_prefix(common, alternative + 1 + LINK_SIZE, chars, max_chars);
3105          if (max_chars == 0)
3106            return consumed;
3107          alternative += GET(alternative, 1);
3108          }
3109    
3110        if (*cc == OP_CBRA || *cc == OP_CBRAPOS)
3111          cc += IMM2_SIZE;
3112        cc += 1 + LINK_SIZE;
3113        continue;
3114    
3115        case OP_CLASS:
3116        case OP_NCLASS:
3117        any = TRUE;
3118        cc += 1 + 32 / sizeof(pcre_uchar);
3119      break;      break;
3120    
3121      case OP_EXACT:  #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
3122      cc += 1 + IMM2_SIZE;      case OP_XCLASS:
3123        any = TRUE;
3124        cc += GET(cc, 1);
3125      break;      break;
3126    #endif
3127    
3128      case OP_PLUSI:      case OP_NOT_DIGIT:
3129      case OP_MINPLUSI:      case OP_DIGIT:
3130      case OP_POSPLUSI:      case OP_NOT_WHITESPACE:
3131      caseless = 1;      case OP_WHITESPACE:
3132        case OP_NOT_WORDCHAR:
3133        case OP_WORDCHAR:
3134        case OP_ANY:
3135        case OP_ALLANY:
3136        any = TRUE;
3137      cc++;      cc++;
3138      break;      break;
3139    
3140      case OP_EXACTI:  #ifdef SUPPORT_UCP
3141      caseless = 1;      case OP_NOTPROP:
3142      cc += 1 + IMM2_SIZE;      case OP_PROP:
3143        any = TRUE;
3144        cc += 1 + 2;
3145      break;      break;
3146    #endif
3147    
3148        case OP_TYPEEXACT:
3149        repeat = GET2(cc, 1);
3150        cc += 1 + IMM2_SIZE;
3151        continue;
3152    
3153      default:      default:
3154      return FALSE;      return consumed;
3155        }
3156    
3157      if (any)
3158        {
3159    #ifdef SUPPORT_UTF
3160        if (common->utf) return consumed;
3161    #endif
3162    #if defined COMPILE_PCRE8
3163        mask = 0xff;
3164    #elif defined COMPILE_PCRE16
3165        mask = 0xffff;
3166    #elif defined COMPILE_PCRE32
3167        mask = 0xffffffff;
3168    #else
3169        SLJIT_ASSERT_STOP();
3170    #endif
3171    
3172        do
3173          {
3174          chars[0] = mask;
3175          chars[1] = mask;
3176    
3177          if (--max_chars == 0)
3178            return consumed;
3179          consumed++;
3180          chars += 2;
3181          }
3182        while (--repeat > 0);
3183    
3184        repeat = 1;
3185        continue;
3186      }      }
3187    
3188    len = 1;    len = 1;
3189  #ifdef SUPPORT_UTF  #ifdef SUPPORT_UTF
3190    if (common->utf && HAS_EXTRALEN(cc[0])) len += GET_EXTRALEN(cc[0]);    if (common->utf && HAS_EXTRALEN(*cc)) len += GET_EXTRALEN(*cc);
3191  #endif  #endif
3192    
3193    if (caseless && char_has_othercase(common, cc))    if (caseless != 0 && char_has_othercase(common, cc))
3194      {      {
3195      caseless = char_get_othercase_bit(common, cc);      caseless = char_get_othercase_bit(common, cc);
3196      if (caseless == 0)      if (caseless == 0)
3197        return FALSE;        return consumed;
3198  #ifdef COMPILE_PCRE8  #ifdef COMPILE_PCRE8
3199      caseless = ((caseless & 0xff) << 8) | (len - (caseless >> 8));      caseless = ((caseless & 0xff) << 8) | (len - (caseless >> 8));
3200  #else  #else
# Line 2621  while (TRUE) Line 3207  while (TRUE)
3207    else    else
3208      caseless = 0;      caseless = 0;
3209    
3210    while (len > 0 && location < 2 * 2)    len_save = len;
3211      {    cc_save = cc;
3212      c = *cc;    while (TRUE)
3213      bit = 0;      {
3214      if (len == (caseless & 0xff))      do
3215        {        {
3216        bit = caseless >> 8;        chr = *cc;
3217        c |= bit;  #ifdef COMPILE_PCRE32
3218          if (SLJIT_UNLIKELY(chr == NOTACHAR))
3219            return consumed;
3220    #endif
3221          mask = 0;
3222          if (len == (caseless & 0xff))
3223            {
3224            mask = caseless >> 8;
3225            chr |= mask;
3226            }
3227    
3228          if (chars[0] == NOTACHAR)
3229            {
3230            chars[0] = chr;
3231            chars[1] = mask;
3232            }
3233          else
3234            {
3235            mask |= chars[0] ^ chr;
3236            chr |= mask;
3237            chars[0] = chr;
3238            chars[1] |= mask;
3239            }
3240    
3241          len--;
3242          if (--max_chars == 0)
3243            return consumed;
3244          consumed++;
3245          chars += 2;
3246          cc++;
3247        }        }
3248        while (len > 0);
3249    
3250      chars[location] = c;      if (--repeat == 0)
3251      chars[location + 1] = bit;        break;
3252    
3253      len--;      len = len_save;
3254      location += 2;      cc = cc_save;
3255      cc++;      }
3256    
3257      repeat = 1;
3258      if (last)
3259        return consumed;
3260      }
3261    }
3262    
3263    #define MAX_N_CHARS 16
3264    
3265    static SLJIT_INLINE BOOL fast_forward_first_n_chars(compiler_common *common, BOOL firstline)
3266    {
3267    DEFINE_COMPILER;
3268    struct sljit_label *start;
3269    struct sljit_jump *quit;
3270    pcre_uint32 chars[MAX_N_CHARS * 2];
3271    pcre_uint8 ones[MAX_N_CHARS];
3272    pcre_uint32 mask;
3273    int i, max;
3274    int offsets[3];
3275    
3276    for (i = 0; i < MAX_N_CHARS; i++)
3277      {
3278      chars[i << 1] = NOTACHAR;
3279      chars[(i << 1) + 1] = 0;
3280      }
3281    
3282    max = scan_prefix(common, common->start, chars, MAX_N_CHARS);
3283    
3284    if (max <= 1)
3285      return FALSE;
3286    
3287    for (i = 0; i < max; i++)
3288      {
3289      mask = chars[(i << 1) + 1];
3290      ones[i] = ones_in_half_byte[mask & 0xf];
3291      mask >>= 4;
3292      while (mask != 0)
3293        {
3294        ones[i] += ones_in_half_byte[mask & 0xf];
3295        mask >>= 4;
3296      }      }
3297      }
3298    
3299    if (location == 2 * 2)  offsets[0] = -1;
3300    /* Scan forward. */
3301    for (i = 0; i < max; i++)
3302      if (ones[i] <= 2) {
3303        offsets[0] = i;
3304      break;      break;
   else if (must_end)  
     return FALSE;  
3305    }    }
3306    
3307    if (offsets[0] == -1)
3308      return FALSE;
3309    
3310    /* Scan backward. */
3311    offsets[1] = -1;
3312    for (i = max - 1; i > offsets[0]; i--)
3313      if (ones[i] <= 2) {
3314        offsets[1] = i;
3315        break;
3316      }
3317    
3318    offsets[2] = -1;
3319    if (offsets[1] >= 0)
3320      {
3321      /* Scan from middle. */
3322      for (i = (offsets[0] + offsets[1]) / 2 + 1; i < offsets[1]; i++)
3323        if (ones[i] <= 2)
3324          {
3325          offsets[2] = i;
3326          break;
3327          }
3328    
3329      if (offsets[2] == -1)
3330        {
3331        for (i = (offsets[0] + offsets[1]) / 2; i > offsets[0]; i--)
3332          if (ones[i] <= 2)
3333            {
3334            offsets[2] = i;
3335            break;
3336            }
3337        }
3338      }
3339    
3340    SLJIT_ASSERT(offsets[1] == -1 || (offsets[0] < offsets[1]));
3341    SLJIT_ASSERT(offsets[2] == -1 || (offsets[0] < offsets[2] && offsets[1] > offsets[2]));
3342    
3343    chars[0] = chars[offsets[0] << 1];
3344    chars[1] = chars[(offsets[0] << 1) + 1];
3345    if (offsets[2] >= 0)
3346      {
3347      chars[2] = chars[offsets[2] << 1];
3348      chars[3] = chars[(offsets[2] << 1) + 1];
3349      }
3350    if (offsets[1] >= 0)
3351      {
3352      chars[4] = chars[offsets[1] << 1];
3353      chars[5] = chars[(offsets[1] << 1) + 1];
3354      }
3355    
3356    max -= 1;
3357  if (firstline)  if (firstline)
3358    {    {
3359    SLJIT_ASSERT(common->first_line_end != 0);    SLJIT_ASSERT(common->first_line_end != 0);
3360    OP1(SLJIT_MOV, TMP3, 0, STR_END, 0);    OP1(SLJIT_MOV, TMP3, 0, STR_END, 0);
3361    OP2(SLJIT_SUB, STR_END, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), common->first_line_end, SLJIT_IMM, 1);    OP2(SLJIT_SUB, STR_END, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), common->first_line_end, SLJIT_IMM, IN_UCHARS(max));
3362    }    }
3363  else  else
3364    OP2(SLJIT_SUB, STR_END, 0, STR_END, 0, SLJIT_IMM, 1);    OP2(SLJIT_SUB, STR_END, 0, STR_END, 0, SLJIT_IMM, IN_UCHARS(max));
3365    
3366  start = LABEL();  start = LABEL();
3367  quit = CMP(SLJIT_C_GREATER_EQUAL, STR_PTR, 0, STR_END, 0);  quit = CMP(SLJIT_C_GREATER_EQUAL, STR_PTR, 0, STR_END, 0);
 #if defined SLJIT_UNALIGNED && SLJIT_UNALIGNED  
 #ifdef COMPILE_PCRE8  
 OP1(SLJIT_MOV_UH, TMP1, 0, SLJIT_MEM1(STR_PTR), 0);  
 #else /* COMPILE_PCRE8 */  
 OP1(SLJIT_MOV_UI, TMP1, 0, SLJIT_MEM1(STR_PTR), 0);  
 #endif  
   
 #else /* SLJIT_UNALIGNED */  
   
 #if defined SLJIT_BIG_ENDIAN && SLJIT_BIG_ENDIAN  
 OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), 0);  
 OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(1));  
 #else /* SLJIT_BIG_ENDIAN */  
 OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(1));  
 OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), 0);  
 #endif /* SLJIT_BIG_ENDIAN */  
3368    
3369  #ifdef COMPILE_PCRE8  OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(offsets[0]));
3370  OP2(SLJIT_SHL, TMP2, 0, TMP2, 0, SLJIT_IMM, 8);  if (offsets[1] >= 0)
3371  #else /* COMPILE_PCRE8 */    OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(offsets[1]));
3372  OP2(SLJIT_SHL, TMP2, 0, TMP2, 0, SLJIT_IMM, 16);  OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
 #endif  
 OP2(SLJIT_OR, TMP1, 0, TMP1, 0, TMP2, 0);  
3373    
3374  #endif  if (chars[1] != 0)
3375      OP2(SLJIT_OR, TMP1, 0, TMP1, 0, SLJIT_IMM, chars[1]);
3376    CMPTO(SLJIT_C_NOT_EQUAL, TMP1, 0, SLJIT_IMM, chars[0], start);
3377    if (offsets[2] >= 0)
3378      OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(offsets[2] - 1));
3379    
3380  if (chars[1] != 0 || chars[3] != 0)  if (offsets[1] >= 0)
3381    {    {
3382    pair.asuchars[0] = chars[1];    if (chars[5] != 0)
3383    pair.asuchars[1] = chars[3];      OP2(SLJIT_OR, TMP2, 0, TMP2, 0, SLJIT_IMM, chars[5]);
3384    OP2(SLJIT_OR, TMP1, 0, TMP1, 0, SLJIT_IMM, pair.ascombined);    CMPTO(SLJIT_C_NOT_EQUAL, TMP2, 0, SLJIT_IMM, chars[4], start);
3385    }    }
3386    
3387  pair.asuchars[0] = chars[0];  if (offsets[2] >= 0)
3388  pair.asuchars[1] = chars[2];    {
3389  found = CMP(SLJIT_C_EQUAL, TMP1, 0, SLJIT_IMM, pair.ascombined);    if (chars[3] != 0)
3390        OP2(SLJIT_OR, TMP1, 0, TMP1, 0, SLJIT_IMM, chars[3]);
3391      CMPTO(SLJIT_C_NOT_EQUAL, TMP1, 0, SLJIT_IMM, chars[2], start);
3392      }
3393    OP2(SLJIT_SUB, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
3394    
 OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));  
 JUMPTO(SLJIT_JUMP, start);  
 JUMPHERE(found);  
3395  JUMPHERE(quit);  JUMPHERE(quit);
3396    
3397  if (firstline)  if (firstline)
3398    OP1(SLJIT_MOV, STR_END, 0, TMP3, 0);    OP1(SLJIT_MOV, STR_END, 0, TMP3, 0);
3399  else  else
3400    OP2(SLJIT_ADD, STR_END, 0, STR_END, 0, SLJIT_IMM, 1);    OP2(SLJIT_ADD, STR_END, 0, STR_END, 0, SLJIT_IMM, IN_UCHARS(max));
3401  return TRUE;  return TRUE;
3402  }  }
3403    
3404    #undef MAX_N_CHARS
3405    
3406  static SLJIT_INLINE void fast_forward_first_char(compiler_common *common, pcre_uchar first_char, BOOL caseless, BOOL firstline)  static SLJIT_INLINE void fast_forward_first_char(compiler_common *common, pcre_uchar first_char, BOOL caseless, BOOL firstline)
3407  {  {
3408  DEFINE_COMPILER;  DEFINE_COMPILER;
# Line 2738  if (first_char == oc) Line 3436  if (first_char == oc)
3436  else  else
3437    {    {
3438    bit = first_char ^ oc;    bit = first_char ^ oc;
3439    if (ispowerof2(bit))    if (is_powerof2(bit))
3440      {      {
3441      OP2(SLJIT_OR, TMP2, 0, TMP1, 0, SLJIT_IMM, bit);      OP2(SLJIT_OR, TMP2, 0, TMP1, 0, SLJIT_IMM, bit);
3442      found = CMP(SLJIT_C_EQUAL, TMP2, 0, SLJIT_IMM, first_char | bit);      found = CMP(SLJIT_C_EQUAL, TMP2, 0, SLJIT_IMM, first_char | bit);
# Line 2746  else Line 3444  else
3444    else    else
3445      {      {
3446      OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, first_char);      OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, first_char);
3447      COND_VALUE(SLJIT_MOV, TMP2, 0, SLJIT_C_EQUAL);      OP_FLAGS(SLJIT_MOV, TMP2, 0, SLJIT_UNUSED, 0, SLJIT_C_EQUAL);
3448      OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, oc);      OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, oc);
3449      COND_VALUE(SLJIT_OR | SLJIT_SET_E, TMP2, 0, SLJIT_C_EQUAL);      OP_FLAGS(SLJIT_OR | SLJIT_SET_E, TMP2, 0, TMP2, 0, SLJIT_C_EQUAL);
3450      found = JUMP(SLJIT_C_NOT_ZERO);      found = JUMP(SLJIT_C_NOT_ZERO);
3451      }      }
3452    }    }
# Line 2790  if (common->nltype == NLTYPE_FIXED && co Line 3488  if (common->nltype == NLTYPE_FIXED && co
3488    
3489    OP2(SLJIT_ADD, TMP1, 0, TMP1, 0, SLJIT_IMM, IN_UCHARS(2));    OP2(SLJIT_ADD, TMP1, 0, TMP1, 0, SLJIT_IMM, IN_UCHARS(2));
3490    OP2(SLJIT_SUB | SLJIT_SET_U, SLJIT_UNUSED, 0, STR_PTR, 0, TMP1, 0);    OP2(SLJIT_SUB | SLJIT_SET_U, SLJIT_UNUSED, 0, STR_PTR, 0, TMP1, 0);
3491    COND_VALUE(SLJIT_MOV, TMP2, 0, SLJIT_C_GREATER_EQUAL);    OP_FLAGS(SLJIT_MOV, TMP2, 0, SLJIT_UNUSED, 0, SLJIT_C_GREATER_EQUAL);
3492  #ifdef COMPILE_PCRE16  #if defined COMPILE_PCRE16 || defined COMPILE_PCRE32
3493    OP2(SLJIT_SHL, TMP2, 0, TMP2, 0, SLJIT_IMM, 1);    OP2(SLJIT_SHL, TMP2, 0, TMP2, 0, SLJIT_IMM, UCHAR_SHIFT);
3494  #endif  #endif
3495    OP2(SLJIT_SUB, STR_PTR, 0, STR_PTR, 0, TMP2, 0);    OP2(SLJIT_SUB, STR_PTR, 0, STR_PTR, 0, TMP2, 0);
3496    
# Line 2833  if (common->nltype == NLTYPE_ANY || comm Line 3531  if (common->nltype == NLTYPE_ANY || comm
3531    notfoundnl = CMP(SLJIT_C_GREATER_EQUAL, STR_PTR, 0, STR_END, 0);    notfoundnl = CMP(SLJIT_C_GREATER_EQUAL, STR_PTR, 0, STR_END, 0);
3532    OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), 0);    OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), 0);
3533    OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, CHAR_NL);    OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, CHAR_NL);
3534    COND_VALUE(SLJIT_MOV, TMP1, 0, SLJIT_C_EQUAL);    OP_FLAGS(SLJIT_MOV, TMP1, 0, SLJIT_UNUSED, 0, SLJIT_C_EQUAL);
3535  #ifdef COMPILE_PCRE16  #if defined COMPILE_PCRE16 || defined COMPILE_PCRE32
3536    OP2(SLJIT_SHL, TMP1, 0, TMP1, 0, SLJIT_IMM, 1);    OP2(SLJIT_SHL, TMP1, 0, TMP1, 0, SLJIT_IMM, UCHAR_SHIFT);
3537  #endif  #endif
3538    OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, TMP1, 0);    OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, TMP1, 0);
3539    JUMPHERE(notfoundnl);    JUMPHERE(notfoundnl);
# Line 2848  if (firstline) Line 3546  if (firstline)
3546    OP1(SLJIT_MOV, STR_END, 0, TMP3, 0);    OP1(SLJIT_MOV, STR_END, 0, TMP3, 0);
3547  }  }
3548    
3549  static SLJIT_INLINE void fast_forward_start_bits(compiler_common *common, sljit_uw start_bits, BOOL firstline)  static BOOL check_class_ranges(compiler_common *common, const pcre_uint8 *bits, BOOL nclass, BOOL invert, jump_list **backtracks);
3550    
3551    static SLJIT_INLINE void fast_forward_start_bits(compiler_common *common, pcre_uint8 *start_bits, BOOL firstline)
3552  {  {
3553  DEFINE_COMPILER;  DEFINE_COMPILER;
3554  struct sljit_label *start;  struct sljit_label *start;
3555  struct sljit_jump *quit;  struct sljit_jump *quit;
3556  struct sljit_jump *found;  struct sljit_jump *found = NULL;
3557    jump_list *matches = NULL;
3558  #ifndef COMPILE_PCRE8  #ifndef COMPILE_PCRE8
3559  struct sljit_jump *jump;  struct sljit_jump *jump;
3560  #endif  #endif
# Line 2872  OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_P Line 3573  OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_P
3573  if (common->utf)  if (common->utf)
3574    OP1(SLJIT_MOV, TMP3, 0, TMP1, 0);    OP1(SLJIT_MOV, TMP3, 0, TMP1, 0);
3575  #endif  #endif
3576    
3577    if (!check_class_ranges(common, start_bits, (start_bits[31] & 0x80) != 0, TRUE, &matches))
3578      {
3579  #ifndef COMPILE_PCRE8  #ifndef COMPILE_PCRE8
3580  jump = CMP(SLJIT_C_LESS, TMP1, 0, SLJIT_IMM, 255);    jump = CMP(SLJIT_C_LESS, TMP1, 0, SLJIT_IMM, 255);
3581  OP1(SLJIT_MOV, TMP1, 0, SLJIT_IMM, 255);    OP1(SLJIT_MOV, TMP1, 0, SLJIT_IMM, 255);
3582  JUMPHERE(jump);    JUMPHERE(jump);
3583  #endif  #endif
3584  OP2(SLJIT_AND, TMP2, 0, TMP1, 0, SLJIT_IMM, 0x7);    OP2(SLJIT_AND, TMP2, 0, TMP1, 0, SLJIT_IMM, 0x7);
3585  OP2(SLJIT_LSHR, TMP1, 0, TMP1, 0, SLJIT_IMM, 3);    OP2(SLJIT_LSHR, TMP1, 0, TMP1, 0, SLJIT_IMM, 3);
3586  OP1(SLJIT_MOV_UB, TMP1, 0, SLJIT_MEM1(TMP1), start_bits);    OP1(SLJIT_MOV_UB, TMP1, 0, SLJIT_MEM1(TMP1), (sljit_sw)start_bits);
3587  OP2(SLJIT_SHL, TMP2, 0, SLJIT_IMM, 1, TMP2, 0);    OP2(SLJIT_SHL, TMP2, 0, SLJIT_IMM, 1, TMP2, 0);
3588  OP2(SLJIT_AND | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, TMP2, 0);    OP2(SLJIT_AND | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, TMP2, 0);
3589  found = JUMP(SLJIT_C_NOT_ZERO);    found = JUMP(SLJIT_C_NOT_ZERO);
3590      }
3591    
3592  #ifdef SUPPORT_UTF  #ifdef SUPPORT_UTF
3593  if (common->utf)  if (common->utf)
3594    OP1(SLJIT_MOV, TMP1, 0, TMP3, 0);    OP1(SLJIT_MOV, TMP1, 0, TMP3, 0);
3595  #endif  #endif
3596  OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));  OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
3597  #if defined SUPPORT_UTF && defined COMPILE_PCRE8  #ifdef SUPPORT_UTF
3598    #if defined COMPILE_PCRE8
3599  if (common->utf)  if (common->utf)
3600    {    {
3601    CMPTO(SLJIT_C_LESS, TMP1, 0, SLJIT_IMM, 0xc0, start);    CMPTO(SLJIT_C_LESS, TMP1, 0, SLJIT_IMM, 0xc0, start);
3602    OP1(SLJIT_MOV_UB, TMP1, 0, SLJIT_MEM1(TMP1), (sljit_w)PRIV(utf8_table4) - 0xc0);    OP1(SLJIT_MOV_UB, TMP1, 0, SLJIT_MEM1(TMP1), (sljit_sw)PRIV(utf8_table4) - 0xc0);
3603    OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, TMP1, 0);    OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, TMP1, 0);
3604    }    }
3605  #endif  #elif defined COMPILE_PCRE16
 #if defined SUPPORT_UTF && defined COMPILE_PCRE16  
3606  if (common->utf)  if (common->utf)
3607    {    {
3608    CMPTO(SLJIT_C_LESS, TMP1, 0, SLJIT_IMM, 0xd800, start);    CMPTO(SLJIT_C_LESS, TMP1, 0, SLJIT_IMM, 0xd800, start);
3609    OP2(SLJIT_AND, TMP1, 0, TMP1, 0, SLJIT_IMM, 0xfc00);    OP2(SLJIT_AND, TMP1, 0, TMP1, 0, SLJIT_IMM, 0xfc00);
3610    OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0xd800);    OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0xd800);
3611    COND_VALUE(SLJIT_MOV, TMP1, 0, SLJIT_C_EQUAL);    OP_FLAGS(SLJIT_MOV, TMP1, 0, SLJIT_UNUSED, 0, SLJIT_C_EQUAL);
3612    OP2(SLJIT_SHL, TMP1, 0, TMP1, 0, SLJIT_IMM, 1);    OP2(SLJIT_SHL, TMP1, 0, TMP1, 0, SLJIT_IMM, 1);
3613    OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, TMP1, 0);    OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, TMP1, 0);
3614    }    }
3615  #endif  #endif /* COMPILE_PCRE[8|16] */
3616    #endif /* SUPPORT_UTF */
3617  JUMPTO(SLJIT_JUMP, start);  JUMPTO(SLJIT_JUMP, start);
3618  JUMPHERE(found);  if (found != NULL)
3619      JUMPHERE(found);
3620    if (matches != NULL)
3621      set_jumps(matches, LABEL());
3622  JUMPHERE(quit);  JUMPHERE(quit);
3623    
3624  if (firstline)  if (firstline)
# Line 2925  struct sljit_jump *alreadyfound; Line 3634  struct sljit_jump *alreadyfound;
3634  struct sljit_jump *found;  struct sljit_jump *found;
3635  struct sljit_jump *foundoc = NULL;  struct sljit_jump *foundoc = NULL;
3636  struct sljit_jump *notfound;  struct sljit_jump *notfound;
3637  pcre_uchar oc, bit;  pcre_uint32 oc, bit;
3638    
3639  SLJIT_ASSERT(common->req_char_ptr != 0);  SLJIT_ASSERT(common->req_char_ptr != 0);
3640  OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), common->req_char_ptr);  OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), common->req_char_ptr);
# Line 2956  if (req_char == oc) Line 3665  if (req_char == oc)
3665  else  else
3666    {    {
3667    bit = req_char ^ oc;    bit = req_char ^ oc;
3668    if (ispowerof2(bit))    if (is_powerof2(bit))
3669      {      {
3670      OP2(SLJIT_OR, TMP2, 0, TMP2, 0, SLJIT_IMM, bit);      OP2(SLJIT_OR, TMP2, 0, TMP2, 0, SLJIT_IMM, bit);
3671      found = CMP(SLJIT_C_EQUAL, TMP2, 0, SLJIT_IMM, req_char | bit);      found = CMP(SLJIT_C_EQUAL, TMP2, 0, SLJIT_IMM, req_char | bit);
# Line 2992  GET_LOCAL_BASE(TMP3, 0, 0); Line 3701  GET_LOCAL_BASE(TMP3, 0, 0);
3701  /* Drop frames until we reach STACK_TOP. */  /* Drop frames until we reach STACK_TOP. */
3702  mainloop = LABEL();  mainloop = LABEL();
3703  OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(TMP1), 0);  OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(TMP1), 0);
3704  jump = CMP(SLJIT_C_SIG_LESS_EQUAL, TMP2, 0, SLJIT_IMM, frame_end);  OP2(SLJIT_SUB | SLJIT_SET_S, SLJIT_UNUSED, 0, TMP2, 0, SLJIT_IMM, 0);
3705    jump = JUMP(SLJIT_C_SIG_LESS_EQUAL);
3706    
3707  OP2(SLJIT_ADD, TMP2, 0, TMP2, 0, TMP3, 0);  OP2(SLJIT_ADD, TMP2, 0, TMP2, 0, TMP3, 0);
3708  OP1(SLJIT_MOV, SLJIT_MEM1(TMP2), 0, SLJIT_MEM1(TMP1), sizeof(sljit_w));  OP1(SLJIT_MOV, SLJIT_MEM1(TMP2), 0, SLJIT_MEM1(TMP1), sizeof(sljit_sw));
3709  OP1(SLJIT_MOV, SLJIT_MEM1(TMP2), sizeof(sljit_w), SLJIT_MEM1(TMP1), 2 * sizeof(sljit_w));  OP1(SLJIT_MOV, SLJIT_MEM1(TMP2), sizeof(sljit_sw), SLJIT_MEM1(TMP1), 2 * sizeof(sljit_sw));
3710  OP2(SLJIT_ADD, TMP1, 0, TMP1, 0, SLJIT_IMM, 3 * sizeof(sljit_w));  OP2(SLJIT_ADD, TMP1, 0, TMP1, 0, SLJIT_IMM, 3 * sizeof(sljit_sw));
3711  JUMPTO(SLJIT_JUMP, mainloop);  JUMPTO(SLJIT_JUMP, mainloop);
3712    
3713  JUMPHERE(jump);  JUMPHERE(jump);
3714  jump = CMP(SLJIT_C_NOT_EQUAL, TMP2, 0, SLJIT_IMM, frame_end);  jump = JUMP(SLJIT_C_SIG_LESS);
3715  /* End of dropping frames. */  /* End of dropping frames. */
3716  sljit_emit_fast_return(compiler, RETURN_ADDR, 0);  sljit_emit_fast_return(compiler, RETURN_ADDR, 0);
3717    
3718  JUMPHERE(jump);  JUMPHERE(jump);
3719  jump = CMP(SLJIT_C_NOT_EQUAL, TMP2, 0, SLJIT_IMM, frame_setstrbegin);  OP1(SLJIT_NEG, TMP2, 0, TMP2, 0);
3720  /* Set string begin. */  OP2(SLJIT_ADD, TMP2, 0, TMP2, 0, TMP3, 0);
3721  OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(TMP1), sizeof(sljit_w));  OP1(SLJIT_MOV, SLJIT_MEM1(TMP2), 0, SLJIT_MEM1(TMP1), sizeof(sljit_sw));
3722  OP2(SLJIT_ADD, TMP1, 0, TMP1, 0, SLJIT_IMM, 2 * sizeof(sljit_w));  OP2(SLJIT_ADD, TMP1, 0, TMP1, 0, SLJIT_IMM, 2 * sizeof(sljit_sw));
 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(0), TMP2, 0);  
 JUMPTO(SLJIT_JUMP, mainloop);  
   
 JUMPHERE(jump);  
 if (common->mark_ptr != 0)  
   {  
   jump = CMP(SLJIT_C_NOT_EQUAL, TMP2, 0, SLJIT_IMM, frame_setmark);  
   OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(TMP1), sizeof(sljit_w));  
   OP2(SLJIT_ADD, TMP1, 0, TMP1, 0, SLJIT_IMM, 2 * sizeof(sljit_w));  
   OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), common->mark_ptr, TMP2, 0);  
   JUMPTO(SLJIT_JUMP, mainloop);  
   
   JUMPHERE(jump);  
   }  
   
 /* Unknown command. */  
 OP2(SLJIT_ADD, TMP1, 0, TMP1, 0, SLJIT_IMM, 2 * sizeof(sljit_w));  
3723  JUMPTO(SLJIT_JUMP, mainloop);  JUMPTO(SLJIT_JUMP, mainloop);
3724  }  }
3725    
# Line 3033  static void check_wordboundary(compiler_ Line 3727  static void check_wordboundary(compiler_
3727  {  {
3728  DEFINE_COMPILER;  DEFINE_COMPILER;
3729  struct sljit_jump *skipread;  struct sljit_jump *skipread;
3730    jump_list *skipread_list = NULL;
3731  #if !(defined COMPILE_PCRE8) || defined SUPPORT_UTF  #if !(defined COMPILE_PCRE8) || defined SUPPORT_UTF
3732  struct sljit_jump *jump;  struct sljit_jump *jump;
3733  #endif  #endif
# Line 3058  if (common->use_ucp) Line 3753  if (common->use_ucp)
3753    add_jump(compiler, &common->getucd, JUMP(SLJIT_FAST_CALL));    add_jump(compiler, &common->getucd, JUMP(SLJIT_FAST_CALL));
3754    OP2(SLJIT_SUB, TMP1, 0, TMP1, 0, SLJIT_IMM, ucp_Ll);    OP2(SLJIT_SUB, TMP1, 0, TMP1, 0, SLJIT_IMM, ucp_Ll);
3755    OP2(SLJIT_SUB | SLJIT_SET_U, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, ucp_Lu - ucp_Ll);    OP2(SLJIT_SUB | SLJIT_SET_U, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, ucp_Lu - ucp_Ll);
3756    COND_VALUE(SLJIT_MOV, TMP2, 0, SLJIT_C_LESS_EQUAL);    OP_FLAGS(SLJIT_MOV, TMP2, 0, SLJIT_UNUSED, 0, SLJIT_C_LESS_EQUAL);
3757    OP2(SLJIT_SUB, TMP1, 0, TMP1, 0, SLJIT_IMM, ucp_Nd - ucp_Ll);    OP2(SLJIT_SUB, TMP1, 0, TMP1, 0, SLJIT_IMM, ucp_Nd - ucp_Ll);
3758    OP2(SLJIT_SUB | SLJIT_SET_U, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, ucp_No - ucp_Nd);    OP2(SLJIT_SUB | SLJIT_SET_U, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, ucp_No - ucp_Nd);
3759    COND_VALUE(SLJIT_OR, TMP2, 0, SLJIT_C_LESS_EQUAL);    OP_FLAGS(SLJIT_OR, TMP2, 0, TMP2, 0, SLJIT_C_LESS_EQUAL);
3760    JUMPHERE(jump);    JUMPHERE(jump);
3761    OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), LOCALS1, TMP2, 0);    OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), LOCALS1, TMP2, 0);
3762    }    }
# Line 3089  else Line 3784  else
3784    }    }
3785  JUMPHERE(skipread);  JUMPHERE(skipread);
3786    
3787  OP1(SLJIT_MOV, TMP2, 0, SLJIT_IMM, 0);  OP1(SLJIT_MOV, TMP2, 0, SLJIT_IMM, 0);
3788  skipread = check_str_end(common);  check_str_end(common, &skipread_list);
3789  peek_char(common);  peek_char(common);
3790    
3791  /* Testing char type. This is a code duplication. */  /* Testing char type. This is a code duplication. */
3792  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
3793  if (common->use_ucp)  if (common->use_ucp)
   {  
   OP1(SLJIT_MOV, TMP2, 0, SLJIT_IMM, 1);  
   jump = CMP(SLJIT_C_EQUAL, TMP1, 0, SLJIT_IMM, CHAR_UNDERSCORE);  
   add_jump(compiler, &common->getucd, JUMP(SLJIT_FAST_CALL));  
   OP2(SLJIT_SUB, TMP1, 0, TMP1, 0, SLJIT_IMM, ucp_Ll);  
   OP2(SLJIT_SUB | SLJIT_SET_U, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, ucp_Lu - ucp_Ll);  
   COND_VALUE(SLJIT_MOV, TMP2, 0, SLJIT_C_LESS_EQUAL);  
   OP2(SLJIT_SUB, TMP1, 0, TMP1, 0, SLJIT_IMM, ucp_Nd - ucp_Ll);  
   OP2(SLJIT_SUB | SLJIT_SET_U, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, ucp_No - ucp_Nd);  
   COND_VALUE(SLJIT_OR, TMP2, 0, SLJIT_C_LESS_EQUAL);  
   JUMPHERE(jump);  
   }  
 else  
 #endif  
   {  
 #ifndef COMPILE_PCRE8  
   /* TMP2 may be destroyed by peek_char. */  
   OP1(SLJIT_MOV, TMP2, 0, SLJIT_IMM, 0);  
   jump = CMP(SLJIT_C_GREATER, TMP1, 0, SLJIT_IMM, 255);  
 #elif defined SUPPORT_UTF  
   OP1(SLJIT_MOV, TMP2, 0, SLJIT_IMM, 0);  
   jump = NULL;  
   if (common->utf)  
     jump = CMP(SLJIT_C_GREATER, TMP1, 0, SLJIT_IMM, 255);  
 #endif  
   OP1(SLJIT_MOV_UB, TMP2, 0, SLJIT_MEM1(TMP1), common->ctypes);  
   OP2(SLJIT_LSHR, TMP2, 0, TMP2, 0, SLJIT_IMM, 4 /* ctype_word */);  
   OP2(SLJIT_AND, TMP2, 0, TMP2, 0, SLJIT_IMM, 1);  
 #ifndef COMPILE_PCRE8  
   JUMPHERE(jump);  
 #elif defined SUPPORT_UTF  
   if (jump != NULL)  
     JUMPHERE(jump);  
 #endif /* COMPILE_PCRE8 */  
   }  
 JUMPHERE(skipread);  
   
 OP2(SLJIT_XOR | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP2, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), LOCALS1);  
 sljit_emit_fast_return(compiler, SLJIT_MEM1(SLJIT_LOCALS_REG), LOCALS0);  
 }  
   
 /*  
   range format:  
   
   ranges[0] = length of the range (max MAX_RANGE_SIZE, -1 means invalid range).  
   ranges[1] = first bit (0 or 1)  
   ranges[2-length] = position of the bit change (when the current bit is not equal to the previous)  
 */  
   
 static BOOL check_ranges(compiler_common *common, int *ranges, jump_list **backtracks, BOOL readch)  
 {  
 DEFINE_COMPILER;  
 struct sljit_jump *jump;  
   
 if (ranges[0] < 0)  
   return FALSE;  
   
 switch(ranges[0])  
   {  
   case 1:  
   if (readch)  
     read_char(common);  
   add_jump(compiler, backtracks, CMP(ranges[1] == 0 ? SLJIT_C_LESS : SLJIT_C_GREATER_EQUAL, TMP1, 0, SLJIT_IMM, ranges[2]));  
   return TRUE;  
   
   case 2:  
   if (readch)  
     read_char(common);  
   OP2(SLJIT_SUB, TMP1, 0, TMP1, 0, SLJIT_IMM, ranges[2]);  
   add_jump(compiler, backtracks, CMP(ranges[1] != 0 ? SLJIT_C_LESS : SLJIT_C_GREATER_EQUAL, TMP1, 0, SLJIT_IMM, ranges[3] - ranges[2]));  
   return TRUE;  
   
   case 4:  
   if (ranges[2] + 1 == ranges[3] && ranges[4] + 1 == ranges[5])  
     {  
     if (readch)  
       read_char(common);  
     if (ranges[1] != 0)  
       {  
       add_jump(compiler, backtracks, CMP(SLJIT_C_EQUAL, TMP1, 0, SLJIT_IMM, ranges[2]));  
       add_jump(compiler, backtracks, CMP(SLJIT_C_EQUAL, TMP1, 0, SLJIT_IMM, ranges[4]));  
       }  
     else  
       {  
       jump = CMP(SLJIT_C_EQUAL, TMP1, 0, SLJIT_IMM, ranges[2]);  
       add_jump(compiler, backtracks, CMP(SLJIT_C_NOT_EQUAL, TMP1, 0, SLJIT_IMM, ranges[4]));  
       JUMPHERE(jump);  
       }  
     return TRUE;  
     }  
   if ((ranges[3] - ranges[2]) == (ranges[5] - ranges[4]) && ispowerof2(ranges[4] - ranges[2]))  
     {  
     if (readch)  
       read_char(common);  
     OP2(SLJIT_OR, TMP1, 0, TMP1, 0, SLJIT_IMM, ranges[4] - ranges[2]);  
     OP2(SLJIT_SUB, TMP1, 0, TMP1, 0, SLJIT_IMM, ranges[4]);  
     add_jump(compiler, backtracks, CMP(ranges[1] != 0 ? SLJIT_C_LESS : SLJIT_C_GREATER_EQUAL, TMP1, 0, SLJIT_IMM, ranges[5] - ranges[4]));  
     return TRUE;  
     }  
   return FALSE;  
   
   default:  
   return FALSE;  
   }  
 }  
   
 static void get_ctype_ranges(compiler_common *common, int flag, int *ranges)  
 {  
 int i, bit, length;  
 const pcre_uint8 *ctypes = (const pcre_uint8*)common->ctypes;  
   
 bit = ctypes[0] & flag;  
 ranges[0] = -1;  
 ranges[1] = bit != 0 ? 1 : 0;  
 length = 0;  
   
 for (i = 1; i < 256; i++)  
   if ((ctypes[i] & flag) != bit)  
     {  
     if (length >= MAX_RANGE_SIZE)  
       return;  
     ranges[2 + length] = i;  
     length++;  
     bit ^= flag;  
     }  
   
 if (bit != 0)  
3794    {    {
3795    if (length >= MAX_RANGE_SIZE)    OP1(SLJIT_MOV, TMP2, 0, SLJIT_IMM, 1);
3796      return;    jump = CMP(SLJIT_C_EQUAL, TMP1, 0, SLJIT_IMM, CHAR_UNDERSCORE);
3797    ranges[2 + length] = 256;    add_jump(compiler, &common->getucd, JUMP(SLJIT_FAST_CALL));
3798    length++;    OP2(SLJIT_SUB, TMP1, 0, TMP1, 0, SLJIT_IMM, ucp_Ll);
3799      OP2(SLJIT_SUB | SLJIT_SET_U, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, ucp_Lu - ucp_Ll);
3800      OP_FLAGS(SLJIT_MOV, TMP2, 0, SLJIT_UNUSED, 0, SLJIT_C_LESS_EQUAL);
3801      OP2(SLJIT_SUB, TMP1, 0, TMP1, 0, SLJIT_IMM, ucp_Nd - ucp_Ll);
3802      OP2(SLJIT_SUB | SLJIT_SET_U, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, ucp_No - ucp_Nd);
3803      OP_FLAGS(SLJIT_OR, TMP2, 0, TMP2, 0, SLJIT_C_LESS_EQUAL);
3804      JUMPHERE(jump);
3805      }
3806    else
3807    #endif
3808      {
3809    #ifndef COMPILE_PCRE8
3810      /* TMP2 may be destroyed by peek_char. */
3811      OP1(SLJIT_MOV, TMP2, 0, SLJIT_IMM, 0);
3812      jump = CMP(SLJIT_C_GREATER, TMP1, 0, SLJIT_IMM, 255);
3813    #elif defined SUPPORT_UTF
3814      OP1(SLJIT_MOV, TMP2, 0, SLJIT_IMM, 0);
3815      jump = NULL;
3816      if (common->utf)
3817        jump = CMP(SLJIT_C_GREATER, TMP1, 0, SLJIT_IMM, 255);
3818    #endif
3819      OP1(SLJIT_MOV_UB, TMP2, 0, SLJIT_MEM1(TMP1), common->ctypes);
3820      OP2(SLJIT_LSHR, TMP2, 0, TMP2, 0, SLJIT_IMM, 4 /* ctype_word */);
3821      OP2(SLJIT_AND, TMP2, 0, TMP2, 0, SLJIT_IMM, 1);
3822    #ifndef COMPILE_PCRE8
3823      JUMPHERE(jump);
3824    #elif defined SUPPORT_UTF
3825      if (jump != NULL)
3826        JUMPHERE(jump);
3827    #endif /* COMPILE_PCRE8 */
3828    }    }
3829  ranges[0] = length;  set_jumps(skipread_list, LABEL());
3830    
3831    OP2(SLJIT_XOR | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP2, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), LOCALS1);
3832    sljit_emit_fast_return(compiler, SLJIT_MEM1(SLJIT_LOCALS_REG), LOCALS0);
3833  }  }
3834    
3835  static BOOL check_class_ranges(compiler_common *common, const pcre_uint8 *bits, BOOL nclass, jump_list **backtracks)  static BOOL check_class_ranges(compiler_common *common, const pcre_uint8 *bits, BOOL nclass, BOOL invert, jump_list **backtracks)
3836  {  {
3837  int ranges[2 + MAX_RANGE_SIZE];  DEFINE_COMPILER;
3838    int ranges[MAX_RANGE_SIZE];
3839  pcre_uint8 bit, cbit, all;  pcre_uint8 bit, cbit, all;
3840  int i, byte, length = 0;  int i, byte, length = 0;
3841    
3842  bit = bits[0] & 0x1;  bit = bits[0] & 0x1;
3843  ranges[1] = bit;  /* All bits will be zero or one (since bit is zero or one). */
 /* Can be 0 or 255. */  
3844  all = -bit;  all = -bit;
3845    
3846  for (i = 0; i < 256; )  for (i = 0; i < 256; )
# Line 3255  for (i = 0; i < 256; ) Line 3855  for (i = 0; i < 256; )
3855        {        {
3856        if (length >= MAX_RANGE_SIZE)        if (length >= MAX_RANGE_SIZE)
3857          return FALSE;          return FALSE;
3858        ranges[2 + length] = i;        ranges[length] = i;
3859        length++;        length++;
3860        bit = cbit;        bit = cbit;
3861        all = -cbit;        all = -cbit;
# Line 3268  if (((bit == 0) && nclass) || ((bit == 1 Line 3868  if (((bit == 0) && nclass) || ((bit == 1
3868    {    {
3869    if (length >= MAX_RANGE_SIZE)    if (length >= MAX_RANGE_SIZE)
3870      return FALSE;      return FALSE;
3871    ranges[2 + length] = 256;    ranges[length] = 256;
3872    length++;    length++;
3873    }    }
 ranges[0] = length;  
3874    
3875  return check_ranges(common, ranges, backtracks, FALSE);  if (length < 0 || length > 4)
3876      return FALSE;
3877    
3878    bit = bits[0] & 0x1;
3879    if (invert) bit ^= 0x1;
3880    
3881    /* No character is accepted. */
3882    if (length == 0 && bit == 0)
3883      add_jump(compiler, backtracks, JUMP(SLJIT_JUMP));
3884    
3885    switch(length)
3886      {
3887      case 0:
3888      /* When bit != 0, all characters are accepted. */
3889      return TRUE;
3890    
3891      case 1:
3892      add_jump(compiler, backtracks, CMP(bit == 0 ? SLJIT_C_LESS : SLJIT_C_GREATER_EQUAL, TMP1, 0, SLJIT_IMM, ranges[0]));
3893      return TRUE;
3894    
3895      case 2:
3896      if (ranges[0] + 1 != ranges[1])
3897        {
3898        OP2(SLJIT_SUB, TMP1, 0, TMP1, 0, SLJIT_IMM, ranges[0]);
3899        add_jump(compiler, backtracks, CMP(bit != 0 ? SLJIT_C_LESS : SLJIT_C_GREATER_EQUAL, TMP1, 0, SLJIT_IMM, ranges[1] - ranges[0]));
3900        }
3901      else
3902        add_jump(compiler, backtracks, CMP(bit != 0 ? SLJIT_C_EQUAL : SLJIT_C_NOT_EQUAL, TMP1, 0, SLJIT_IMM, ranges[0]));
3903      return TRUE;
3904    
3905      case 3:
3906      if (bit != 0)
3907        {
3908        add_jump(compiler, backtracks, CMP(SLJIT_C_GREATER_EQUAL, TMP1, 0, SLJIT_IMM, ranges[2]));
3909        if (ranges[0] + 1 != ranges[1])
3910          {
3911          OP2(SLJIT_SUB, TMP1, 0, TMP1, 0, SLJIT_IMM, ranges[0]);
3912          add_jump(compiler, backtracks, CMP(SLJIT_C_LESS, TMP1, 0, SLJIT_IMM, ranges[1] - ranges[0]));
3913          }
3914        else
3915          add_jump(compiler, backtracks, CMP(SLJIT_C_EQUAL, TMP1, 0, SLJIT_IMM, ranges[0]));
3916        return TRUE;
3917        }
3918    
3919      add_jump(compiler, backtracks, CMP(SLJIT_C_LESS, TMP1, 0, SLJIT_IMM, ranges[0]));
3920      if (ranges[1] + 1 != ranges[2])
3921        {
3922        OP2(SLJIT_SUB, TMP1, 0, TMP1, 0, SLJIT_IMM, ranges[1]);
3923        add_jump(compiler, backtracks, CMP(SLJIT_C_LESS, TMP1, 0, SLJIT_IMM, ranges[2] - ranges[1]));
3924        }
3925      else
3926        add_jump(compiler, backtracks, CMP(SLJIT_C_EQUAL, TMP1, 0, SLJIT_IMM, ranges[1]));
3927      return TRUE;
3928    
3929      case 4:
3930      if ((ranges[1] - ranges[0]) == (ranges[3] - ranges[2])
3931          && (ranges[0] | (ranges[2] - ranges[0])) == ranges[2]
3932          && is_powerof2(ranges[2] - ranges[0]))
3933        {
3934