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

Diff of /code/trunk/pcre_jit_compile.c

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

revision 1275 by zherczeg, Sun Mar 10 05:32:10 2013 UTC revision 1419 by zherczeg, Sun Dec 29 04:42:14 2013 UTC
# Line 168  typedef struct jit_arguments { Line 168  typedef struct jit_arguments {
168    pcre_uchar *mark_ptr;    pcre_uchar *mark_ptr;
169    void *callout_data;    void *callout_data;
170    /* Everything else after. */    /* Everything else after. */
171      pcre_uint32 limit_match;
172    int real_offset_count;    int real_offset_count;
173    int offset_count;    int offset_count;
   int call_limit;  
174    pcre_uint8 notbol;    pcre_uint8 notbol;
175    pcre_uint8 noteol;    pcre_uint8 noteol;
176    pcre_uint8 notempty;    pcre_uint8 notempty;
# Line 182  typedef struct executable_functions { Line 182  typedef struct executable_functions {
182    PUBL(jit_callback) callback;    PUBL(jit_callback) callback;
183    void *userdata;    void *userdata;
184    pcre_uint32 top_bracket;    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 202  enum frame_types { Line 203  enum frame_types {
203  };  };
204    
205  enum control_types {  enum control_types {
206    type_commit = 0,    type_mark = 0,
207    type_prune = 1,    type_then_trap = 1
   type_skip = 2  
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 227  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 private_data_ptr;    int private_data_ptr;
# Line 248  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. */
# Line 283  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 {
# Line 291  typedef struct recurse_backtrack { Line 291  typedef struct recurse_backtrack {
291    BOOL inlined_pattern;    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. */    /* The sljit ceneric compiler. */
# Line 299  typedef struct compiler_common { Line 314  typedef struct compiler_common {
314    /* First byte code. */    /* First byte code. */
315    pcre_uchar *start;    pcre_uchar *start;
316    /* Maps private data offset to each opcode. */    /* Maps private data offset to each opcode. */
317    int *private_data_ptrs;    sljit_si *private_data_ptrs;
318    /* Tells whether the capturing bracket is optimized. */    /* Tells whether the capturing bracket is optimized. */
319    pcre_uint8 *optimized_cbracket;    pcre_uint8 *optimized_cbracket;
320      /* Tells whether the starting offset is a target of then. */
321      pcre_uint8 *then_offsets;
322      /* Current position where a THEN must jump. */
323      then_trap_backtrack *then_trap;
324    /* Starting offset of private data for capturing brackets. */    /* Starting offset of private data for capturing brackets. */
325    int cbra_ptr;    int cbra_ptr;
326    /* Output vector starting point. Must be divisible by 2. */    /* Output vector starting point. Must be divisible by 2. */
# Line 330  typedef struct compiler_common { Line 349  typedef struct compiler_common {
349    sljit_sw 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 in the pattern. */    /* \K is found in the pattern. */
353    BOOL has_set_som;    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. */    /* Needs to know the start position anytime. */
359    BOOL needs_start_ptr;    BOOL needs_start_ptr;
360    /* Currently in recurse or assert. */    /* Currently in recurse or negative assert. */
361    BOOL local_exit;    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;
# Line 346  typedef struct compiler_common { Line 371  typedef struct compiler_common {
371    sljit_sw ctypes;    sljit_sw ctypes;
372    int digits[2 + MAX_RANGE_SIZE];    int digits[2 + MAX_RANGE_SIZE];
373    /* Named capturing brackets. */    /* Named capturing brackets. */
374    sljit_uw name_table;    pcre_uchar *name_table;
375    sljit_sw name_count;    sljit_sw name_count;
376    sljit_sw name_entry_size;    sljit_sw name_entry_size;
377    
# Line 360  typedef struct compiler_common { Line 385  typedef struct compiler_common {
385    recurse_entry *currententry;    recurse_entry *currententry;
386    jump_list *partialmatch;    jump_list *partialmatch;
387    jump_list *quit;    jump_list *quit;
388      jump_list *positive_assert_quit;
389    jump_list *forced_quit;    jump_list *forced_quit;
390    jump_list *accept;    jump_list *accept;
391    jump_list *calllimit;    jump_list *calllimit;
# Line 438  typedef struct compare_context { Line 464  typedef struct compare_context {
464  #define STACK_TOP     SLJIT_SCRATCH_REG2  #define STACK_TOP     SLJIT_SCRATCH_REG2
465  #define STACK_LIMIT   SLJIT_SAVED_REG3  #define STACK_LIMIT   SLJIT_SAVED_REG3
466  #define ARGUMENTS     SLJIT_SAVED_EREG1  #define ARGUMENTS     SLJIT_SAVED_EREG1
467  #define CALL_COUNT    SLJIT_SAVED_EREG2  #define COUNT_MATCH   SLJIT_SAVED_EREG2
468  #define RETURN_ADDR   SLJIT_TEMPORARY_EREG1  #define RETURN_ADDR   SLJIT_TEMPORARY_EREG1
469    
470  /* Local space layout. */  /* Local space layout. */
# Line 449  typedef struct compare_context { Line 475  typedef struct compare_context {
475  #define POSSESSIVE0      (2 * sizeof(sljit_sw))  #define POSSESSIVE0      (2 * sizeof(sljit_sw))
476  #define POSSESSIVE1      (3 * sizeof(sljit_sw))  #define POSSESSIVE1      (3 * sizeof(sljit_sw))
477  /* Max limit of recursions. */  /* Max limit of recursions. */
478  #define CALL_LIMIT       (4 * sizeof(sljit_sw))  #define LIMIT_MATCH      (4 * sizeof(sljit_sw))
479  /* The output vector is stored on the stack, and contains pointers  /* The output vector is stored on the stack, and contains pointers
480  to characters. The vector data is divided into two groups: the first  to characters. The vector data is divided into two groups: the first
481  group contains the start / end character pointers, and the second is  group contains the start / end character pointers, and the second is
482  the start pointers when the end of the capturing group has not yet reached. */  the start pointers when the end of the capturing group has not yet reached. */
483  #define OVECTOR_START    (common->ovector_start)  #define OVECTOR_START    (common->ovector_start)
484  #define OVECTOR(i)       (OVECTOR_START + (i) * sizeof(sljit_sw))  #define OVECTOR(i)       (OVECTOR_START + (i) * (sljit_sw)sizeof(sljit_sw))
485  #define OVECTOR_PRIV(i)  (common->cbra_ptr + (i) * sizeof(sljit_sw))  #define OVECTOR_PRIV(i)  (common->cbra_ptr + (i) * (sljit_sw)sizeof(sljit_sw))
486  #define PRIVATE_DATA(cc) (common->private_data_ptrs[(cc) - common->start])  #define PRIVATE_DATA(cc) (common->private_data_ptrs[(cc) - common->start])
487    
488  #if defined COMPILE_PCRE8  #if defined COMPILE_PCRE8
# Line 507  cc += 1 + LINK_SIZE; Line 533  cc += 1 + LINK_SIZE;
533  return cc;  return cc;
534  }  }
535    
536    static int ones_in_half_byte[16] = {
537      /* 0 */ 0, 1, 1, 2, /* 4 */ 1, 2, 2, 3,
538      /* 8 */ 1, 2, 2, 3, /* 12 */ 2, 3, 3, 4
539    };
540    
541  /* Functions whose might need modification for all new supported opcodes:  /* Functions whose might need modification for all new supported opcodes:
542   next_opcode   next_opcode
543   get_private_data_length   check_opcode_types
544   set_private_data_ptrs   set_private_data_ptrs
545   get_framesize   get_framesize
546   init_frame   init_frame
547   get_private_data_length_for_copy   get_private_data_copy_length
548   copy_private_data   copy_private_data
549   compile_matchingpath   compile_matchingpath
550   compile_backtrackingpath   compile_backtrackingpath
# Line 559  switch(*cc) Line 590  switch(*cc)
590    case OP_CRMINQUERY:    case OP_CRMINQUERY:
591    case OP_CRRANGE:    case OP_CRRANGE:
592    case OP_CRMINRANGE:    case OP_CRMINRANGE:
593      case OP_CRPOSSTAR:
594      case OP_CRPOSPLUS:
595      case OP_CRPOSQUERY:
596      case OP_CRPOSRANGE:
597    case OP_CLASS:    case OP_CLASS:
598    case OP_NCLASS:    case OP_NCLASS:
599    case OP_REF:    case OP_REF:
600    case OP_REFI:    case OP_REFI:
601      case OP_DNREF:
602      case OP_DNREFI:
603    case OP_RECURSE:    case OP_RECURSE:
604    case OP_CALLOUT:    case OP_CALLOUT:
605    case OP_ALT:    case OP_ALT:
# Line 588  switch(*cc) Line 625  switch(*cc)
625    case OP_SCBRAPOS:    case OP_SCBRAPOS:
626    case OP_SCOND:    case OP_SCOND:
627    case OP_CREF:    case OP_CREF:
628    case OP_NCREF:    case OP_DNCREF:
629    case OP_RREF:    case OP_RREF:
630    case OP_NRREF:    case OP_DNRREF:
631    case OP_DEF:    case OP_DEF:
632    case OP_BRAZERO:    case OP_BRAZERO:
633    case OP_BRAMINZERO:    case OP_BRAMINZERO:
634    case OP_BRAPOSZERO:    case OP_BRAPOSZERO:
635    case OP_PRUNE:    case OP_PRUNE:
636    case OP_SKIP:    case OP_SKIP:
637      case OP_THEN:
638    case OP_COMMIT:    case OP_COMMIT:
639    case OP_FAIL:    case OP_FAIL:
640    case OP_ACCEPT:    case OP_ACCEPT:
# Line 696  switch(*cc) Line 734  switch(*cc)
734    
735    case OP_MARK:    case OP_MARK:
736    case OP_PRUNE_ARG:    case OP_PRUNE_ARG:
737      case OP_SKIP_ARG:
738      case OP_THEN_ARG:
739    return cc + 1 + 2 + cc[1];    return cc + 1 + 2 + cc[1];
740    
741    default:    default:
742      /* All opcodes are supported now! */
743      SLJIT_ASSERT_STOP();
744    return NULL;    return NULL;
745    }    }
746  }  }
747    
748  #define CASE_ITERATOR_PRIVATE_DATA_1 \  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_PRIVATE_DATA_2A \  
     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_PRIVATE_DATA_2B \  
     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_PRIVATE_DATA_1 \  
     case OP_TYPEMINSTAR: \  
     case OP_TYPEMINPLUS: \  
     case OP_TYPEQUERY: \  
     case OP_TYPEMINQUERY:  
   
 #define CASE_ITERATOR_TYPE_PRIVATE_DATA_2A \  
     case OP_TYPESTAR: \  
     case OP_TYPEPLUS:  
   
 #define CASE_ITERATOR_TYPE_PRIVATE_DATA_2B \  
     case OP_TYPEUPTO: \  
     case OP_TYPEMINUPTO:  
   
 static int get_class_iterator_size(pcre_uchar *cc)  
749  {  {
750  switch(*cc)  int count;
751    {  pcre_uchar *slot;
   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_private_data_length(compiler_common *common, pcre_uchar *cc, pcre_uchar *ccend)  
 {  
 int private_data_length = 0;  
 pcre_uchar *alternative;  
 pcre_uchar *name;  
 pcre_uchar *end = NULL;  
 int space, size, i;  
 pcre_uint32 bracketlen;  
752    
753  /* Calculate important variables (like stack size) and checks whether all opcodes are supported. */  /* Calculate important variables (like stack size) and checks whether all opcodes are supported. */
754  while (cc < ccend)  while (cc < ccend)
755    {    {
   space = 0;  
   size = 0;  
   bracketlen = 0;  
756    switch(*cc)    switch(*cc)
757      {      {
758      case OP_SET_SOM:      case OP_SET_SOM:
# Line 808  while (cc < ccend) Line 766  while (cc < ccend)
766      cc += 1 + IMM2_SIZE;      cc += 1 + IMM2_SIZE;
767      break;      break;
768    
     case OP_ASSERT:  
     case OP_ASSERT_NOT:  
     case OP_ASSERTBACK:  
     case OP_ASSERTBACK_NOT:  
     case OP_ONCE:  
     case OP_ONCE_NC:  
     case OP_BRAPOS:  
     case OP_SBRA:  
     case OP_SBRAPOS:  
     private_data_length += sizeof(sljit_sw);  
     bracketlen = 1 + LINK_SIZE;  
     break;  
   
769      case OP_CBRAPOS:      case OP_CBRAPOS:
770      case OP_SCBRAPOS:      case OP_SCBRAPOS:
     private_data_length += sizeof(sljit_sw);  
771      common->optimized_cbracket[GET2(cc, 1 + LINK_SIZE)] = 0;      common->optimized_cbracket[GET2(cc, 1 + LINK_SIZE)] = 0;
772      bracketlen = 1 + LINK_SIZE + IMM2_SIZE;      cc += 1 + LINK_SIZE + IMM2_SIZE;
773      break;      break;
774    
775      case OP_COND:      case OP_COND:
# Line 833  while (cc < ccend) Line 777  while (cc < ccend)
777      /* Only AUTO_CALLOUT can insert this opcode. We do      /* Only AUTO_CALLOUT can insert this opcode. We do
778         not intend to support this case. */         not intend to support this case. */
779      if (cc[1 + LINK_SIZE] == OP_CALLOUT)      if (cc[1 + LINK_SIZE] == OP_CALLOUT)
780        return -1;        return FALSE;
781        cc += 1 + LINK_SIZE;
     if (*cc == OP_COND)  
       {  
       /* Might be a hidden SCOND. */  
       alternative = cc + GET(cc, 1);  
       if (*alternative == OP_KETRMAX || *alternative == OP_KETRMIN)  
         private_data_length += sizeof(sljit_sw);  
       }  
     else  
       private_data_length += sizeof(sljit_sw);  
     bracketlen = 1 + LINK_SIZE;  
782      break;      break;
783    
784      case OP_CREF:      case OP_CREF:
785      i = GET2(cc, 1);      common->optimized_cbracket[GET2(cc, 1)] = 0;
     common->optimized_cbracket[i] = 0;  
786      cc += 1 + IMM2_SIZE;      cc += 1 + IMM2_SIZE;
787      break;      break;
788    
789      case OP_NCREF:      case OP_DNREF:
790      bracketlen = GET2(cc, 1);      case OP_DNREFI:
791      name = (pcre_uchar *)common->name_table;      case OP_DNCREF:
792      alternative = name;      count = GET2(cc, 1 + IMM2_SIZE);
793      for (i = 0; i < common->name_count; i++)      slot = common->name_table + GET2(cc, 1) * common->name_entry_size;
794        {      while (count-- > 0)
       if (GET2(name, 0) == bracketlen) break;  
       name += common->name_entry_size;  
       }  
     SLJIT_ASSERT(i != common->name_count);  
   
     for (i = 0; i < common->name_count; i++)  
795        {        {
796        if (STRCMP_UC_UC(alternative + IMM2_SIZE, name + IMM2_SIZE) == 0)        common->optimized_cbracket[GET2(slot, 0)] = 0;
797          common->optimized_cbracket[GET2(alternative, 0)] = 0;        slot += common->name_entry_size;
       alternative += common->name_entry_size;  
798        }        }
799      bracketlen = 0;      cc += 1 + 2 * IMM2_SIZE;
     cc += 1 + IMM2_SIZE;  
     break;  
   
     case OP_BRA:  
     bracketlen = 1 + LINK_SIZE;  
     break;  
   
     case OP_CBRA:  
     case OP_SCBRA:  
     bracketlen = 1 + LINK_SIZE + IMM2_SIZE;  
     break;  
   
     CASE_ITERATOR_PRIVATE_DATA_1  
     space = 1;  
     size = -2;  
     break;  
   
     CASE_ITERATOR_PRIVATE_DATA_2A  
     space = 2;  
     size = -2;  
     break;  
   
     CASE_ITERATOR_PRIVATE_DATA_2B  
     space = 2;  
     size = -(2 + IMM2_SIZE);  
     break;  
   
     CASE_ITERATOR_TYPE_PRIVATE_DATA_1  
     space = 1;  
     size = 1;  
     break;  
   
     CASE_ITERATOR_TYPE_PRIVATE_DATA_2A  
     if (cc[1] != OP_ANYNL && cc[1] != OP_EXTUNI)  
       space = 2;  
     size = 1;  
     break;  
   
     CASE_ITERATOR_TYPE_PRIVATE_DATA_2B  
     if (cc[1 + IMM2_SIZE] != OP_ANYNL && cc[1 + IMM2_SIZE] != OP_EXTUNI)  
       space = 2;  
     size = 1 + IMM2_SIZE;  
     break;  
   
     case OP_CLASS:  
     case OP_NCLASS:  
     size += 1 + 32 / sizeof(pcre_uchar);  
     space = get_class_iterator_size(cc + size);  
     break;  
   
 #if defined SUPPORT_UTF || !defined COMPILE_PCRE8  
     case OP_XCLASS:  
     size = GET(cc, 1);  
     space = get_class_iterator_size(cc + size);  
800      break;      break;
 #endif  
801    
802      case OP_RECURSE:      case OP_RECURSE:
803      /* Set its value only once. */      /* Set its value only once. */
# Line 947  while (cc < ccend) Line 818  while (cc < ccend)
818      cc += 2 + 2 * LINK_SIZE;      cc += 2 + 2 * LINK_SIZE;
819      break;      break;
820    
821        case OP_THEN_ARG:
822        common->has_then = TRUE;
823        common->control_head_ptr = 1;
824        /* Fall through. */
825    
826      case OP_PRUNE_ARG:      case OP_PRUNE_ARG:
827      common->needs_start_ptr = TRUE;      common->needs_start_ptr = TRUE;
     common->control_head_ptr = 1;  
828      /* Fall through. */      /* Fall through. */
829    
830      case OP_MARK:      case OP_MARK:
# Line 961  while (cc < ccend) Line 836  while (cc < ccend)
836      cc += 1 + 2 + cc[1];      cc += 1 + 2 + cc[1];
837      break;      break;
838    
839        case OP_THEN:
840        common->has_then = TRUE;
841        common->control_head_ptr = 1;
842        /* Fall through. */
843    
844      case OP_PRUNE:      case OP_PRUNE:
845      case OP_SKIP:      case OP_SKIP:
846      common->needs_start_ptr = TRUE;      common->needs_start_ptr = TRUE;
847      /* Fall through. */      cc += 1;
848        break;
849    
850      case OP_COMMIT:      case OP_SKIP_ARG:
851      common->control_head_ptr = 1;      common->control_head_ptr = 1;
852      cc += 1;      common->has_skip_arg = TRUE;
853        cc += 1 + 2 + cc[1];
854      break;      break;
855    
856      default:      default:
857      cc = next_opcode(common, cc);      cc = next_opcode(common, cc);
858      if (cc == NULL)      if (cc == NULL)
859        return -1;        return FALSE;
860      break;      break;
861      }      }
862      }
863    return TRUE;
864    }
865    
866    if (space > 0 && cc >= end)  static int get_class_iterator_size(pcre_uchar *cc)
867      private_data_length += sizeof(sljit_sw) * space;  {
868    switch(*cc)
869      {
870      case OP_CRSTAR:
871      case OP_CRPLUS:
872      return 2;
873    
874    if (size != 0)    case OP_CRMINSTAR:
875      case OP_CRMINPLUS:
876      case OP_CRQUERY:
877      case OP_CRMINQUERY:
878      return 1;
879    
880      case OP_CRRANGE:
881      case OP_CRMINRANGE:
882      if (GET2(cc, 1) == GET2(cc, 1 + IMM2_SIZE))
883        return 0;
884      return 2;
885    
886      default:
887      return 0;
888      }
889    }
890    
891    static BOOL detect_repeat(compiler_common *common, pcre_uchar *begin)
892    {
893    pcre_uchar *end = bracketend(begin);
894    pcre_uchar *next;
895    pcre_uchar *next_end;
896    pcre_uchar *max_end;
897    pcre_uchar type;
898    sljit_sw length = end - begin;
899    int min, max, i;
900    
901    /* Detect fixed iterations first. */
902    if (end[-(1 + LINK_SIZE)] != OP_KET)
903      return FALSE;
904    
905    /* Already detected repeat. */
906    if (common->private_data_ptrs[end - common->start - LINK_SIZE] != 0)
907      return TRUE;
908    
909    next = end;
910    min = 1;
911    while (1)
912      {
913      if (*next != *begin)
914        break;
915      next_end = bracketend(next);
916      if (next_end - next != length || memcmp(begin, next, IN_UCHARS(length)) != 0)
917        break;
918      next = next_end;
919      min++;
920      }
921    
922    if (min == 2)
923      return FALSE;
924    
925    max = 0;
926    max_end = next;
927    if (*next == OP_BRAZERO || *next == OP_BRAMINZERO)
928      {
929      type = *next;
930      while (1)
931      {      {
932      if (size < 0)      if (next[0] != type || next[1] != OP_BRA || next[2 + LINK_SIZE] != *begin)
933        {        break;
934        cc += -size;      next_end = bracketend(next + 2 + LINK_SIZE);
935  #ifdef SUPPORT_UTF      if (next_end - next != (length + 2 + LINK_SIZE) || memcmp(begin, next + 2 + LINK_SIZE, IN_UCHARS(length)) != 0)
936        if (common->utf && HAS_EXTRALEN(cc[-1])) cc += GET_EXTRALEN(cc[-1]);        break;
937  #endif      next = next_end;
938        }      max++;
     else  
       cc += size;  
939      }      }
940    
941    if (bracketlen != 0)    if (next[0] == type && next[1] == *begin && max >= 1)
942      {      {
943      if (cc >= end)      next_end = bracketend(next + 1);
944        if (next_end - next == (length + 1) && memcmp(begin, next + 1, IN_UCHARS(length)) == 0)
945        {        {
946        end = bracketend(cc);        for (i = 0; i < max; i++, next_end += 1 + LINK_SIZE)
947        if (end[-1 - LINK_SIZE] == OP_KET)          if (*next_end != OP_KET)
948          end = NULL;            break;
949    
950          if (i == max)
951            {
952            common->private_data_ptrs[max_end - common->start - LINK_SIZE] = next_end - max_end;
953            common->private_data_ptrs[max_end - common->start - LINK_SIZE + 1] = (type == OP_BRAZERO) ? OP_UPTO : OP_MINUPTO;
954            /* +2 the original and the last. */
955            common->private_data_ptrs[max_end - common->start - LINK_SIZE + 2] = max + 2;
956            if (min == 1)
957              return TRUE;
958            min--;
959            max_end -= (1 + LINK_SIZE) + GET(max_end, -LINK_SIZE);
960            }
961        }        }
     cc += bracketlen;  
962      }      }
963    }    }
964  return private_data_length;  
965    if (min >= 3)
966      {
967      common->private_data_ptrs[end - common->start - LINK_SIZE] = max_end - end;
968      common->private_data_ptrs[end - common->start - LINK_SIZE + 1] = OP_EXACT;
969      common->private_data_ptrs[end - common->start - LINK_SIZE + 2] = min;
970      return TRUE;
971      }
972    
973    return FALSE;
974  }  }
975    
976  static void set_private_data_ptrs(compiler_common *common, int private_data_ptr, pcre_uchar *ccend)  #define CASE_ITERATOR_PRIVATE_DATA_1 \
977        case OP_MINSTAR: \
978        case OP_MINPLUS: \
979        case OP_QUERY: \
980        case OP_MINQUERY: \
981        case OP_MINSTARI: \
982        case OP_MINPLUSI: \
983        case OP_QUERYI: \
984        case OP_MINQUERYI: \
985        case OP_NOTMINSTAR: \
986        case OP_NOTMINPLUS: \
987        case OP_NOTQUERY: \
988        case OP_NOTMINQUERY: \
989        case OP_NOTMINSTARI: \
990        case OP_NOTMINPLUSI: \
991        case OP_NOTQUERYI: \
992        case OP_NOTMINQUERYI:
993    
994    #define CASE_ITERATOR_PRIVATE_DATA_2A \
995        case OP_STAR: \
996        case OP_PLUS: \
997        case OP_STARI: \
998        case OP_PLUSI: \
999        case OP_NOTSTAR: \
1000        case OP_NOTPLUS: \
1001        case OP_NOTSTARI: \
1002        case OP_NOTPLUSI:
1003    
1004    #define CASE_ITERATOR_PRIVATE_DATA_2B \
1005        case OP_UPTO: \
1006        case OP_MINUPTO: \
1007        case OP_UPTOI: \
1008        case OP_MINUPTOI: \
1009        case OP_NOTUPTO: \
1010        case OP_NOTMINUPTO: \
1011        case OP_NOTUPTOI: \
1012        case OP_NOTMINUPTOI:
1013    
1014    #define CASE_ITERATOR_TYPE_PRIVATE_DATA_1 \
1015        case OP_TYPEMINSTAR: \
1016        case OP_TYPEMINPLUS: \
1017        case OP_TYPEQUERY: \
1018        case OP_TYPEMINQUERY:
1019    
1020    #define CASE_ITERATOR_TYPE_PRIVATE_DATA_2A \
1021        case OP_TYPESTAR: \
1022        case OP_TYPEPLUS:
1023    
1024    #define CASE_ITERATOR_TYPE_PRIVATE_DATA_2B \
1025        case OP_TYPEUPTO: \
1026        case OP_TYPEMINUPTO:
1027    
1028    static void set_private_data_ptrs(compiler_common *common, int *private_data_start, pcre_uchar *ccend)
1029  {  {
1030  pcre_uchar *cc = common->start;  pcre_uchar *cc = common->start;
1031  pcre_uchar *alternative;  pcre_uchar *alternative;
1032  pcre_uchar *end = NULL;  pcre_uchar *end = NULL;
1033    int private_data_ptr = *private_data_start;
1034  int space, size, bracketlen;  int space, size, bracketlen;
1035    
1036  while (cc < ccend)  while (cc < ccend)
# Line 1020  while (cc < ccend) Line 1038  while (cc < ccend)
1038    space = 0;    space = 0;
1039    size = 0;    size = 0;
1040    bracketlen = 0;    bracketlen = 0;
1041      if (private_data_ptr > SLJIT_MAX_LOCAL_SIZE)
1042        return;
1043    
1044      if (*cc == OP_ONCE || *cc == OP_ONCE_NC || *cc == OP_BRA || *cc == OP_CBRA || *cc == OP_COND)
1045        if (detect_repeat(common, cc))
1046          {
1047          /* These brackets are converted to repeats, so no global
1048          based single character repeat is allowed. */
1049          if (cc >= end)
1050            end = bracketend(cc);
1051          }
1052    
1053    switch(*cc)    switch(*cc)
1054      {      {
1055        case OP_KET:
1056        if (common->private_data_ptrs[cc + 1 - common->start] != 0)
1057          {
1058          common->private_data_ptrs[cc - common->start] = private_data_ptr;
1059          private_data_ptr += sizeof(sljit_sw);
1060          cc += common->private_data_ptrs[cc + 1 - common->start];
1061          }
1062        cc += 1 + LINK_SIZE;
1063        break;
1064    
1065      case OP_ASSERT:      case OP_ASSERT:
1066      case OP_ASSERT_NOT:      case OP_ASSERT_NOT:
1067      case OP_ASSERTBACK:      case OP_ASSERTBACK:
# Line 1115  while (cc < ccend) Line 1155  while (cc < ccend)
1155      break;      break;
1156      }      }
1157    
1158      /* Character iterators, which are not inside a repeated bracket,
1159         gets a private slot instead of allocating it on the stack. */
1160    if (space > 0 && cc >= end)    if (space > 0 && cc >= end)
1161      {      {
1162      common->private_data_ptrs[cc - common->start] = private_data_ptr;      common->private_data_ptrs[cc - common->start] = private_data_ptr;
# Line 1145  while (cc < ccend) Line 1187  while (cc < ccend)
1187      cc += bracketlen;      cc += bracketlen;
1188      }      }
1189    }    }
1190    *private_data_start = private_data_ptr;
1191  }  }
1192    
1193  /* Returns with a frame_types (always < 0) if no need for frame. */  /* Returns with a frame_types (always < 0) if no need for frame. */
1194  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)
1195  {  {
 pcre_uchar *ccend = bracketend(cc) - (1 + LINK_SIZE);  
1196  int length = 0;  int length = 0;
1197  int possessive = 0;  int possessive = 0;
1198  BOOL stack_restore = FALSE;  BOOL stack_restore = FALSE;
# Line 1159  BOOL setmark_found = recursive; Line 1201  BOOL setmark_found = recursive;
1201  /* The last capture is a local variable even for recursions. */  /* The last capture is a local variable even for recursions. */
1202  BOOL capture_last_found = FALSE;  BOOL capture_last_found = FALSE;
1203    
1204  if (!recursive && (*cc == OP_CBRAPOS || *cc == OP_SCBRAPOS))  #if defined DEBUG_FORCE_CONTROL_HEAD && DEBUG_FORCE_CONTROL_HEAD
1205    SLJIT_ASSERT(common->control_head_ptr != 0);
1206    *needs_control_head = TRUE;
1207    #else
1208    *needs_control_head = FALSE;
1209    #endif
1210    
1211    if (ccend == NULL)
1212    {    {
1213    possessive = length = (common->capture_last_ptr != 0) ? 5 : 3;    ccend = bracketend(cc) - (1 + LINK_SIZE);
1214    /* This is correct regardless of common->capture_last_ptr. */    if (!recursive && (*cc == OP_CBRAPOS || *cc == OP_SCBRAPOS))
1215    capture_last_found = TRUE;      {
1216        possessive = length = (common->capture_last_ptr != 0) ? 5 : 3;
1217        /* This is correct regardless of common->capture_last_ptr. */
1218        capture_last_found = TRUE;
1219        }
1220      cc = next_opcode(common, cc);
1221    }    }
1222    
 cc = next_opcode(common, cc);  
1223  SLJIT_ASSERT(cc != NULL);  SLJIT_ASSERT(cc != NULL);
1224  while (cc < ccend)  while (cc < ccend)
1225    switch(*cc)    switch(*cc)
# Line 1184  while (cc < ccend) Line 1237  while (cc < ccend)
1237    
1238      case OP_MARK:      case OP_MARK:
1239      case OP_PRUNE_ARG:      case OP_PRUNE_ARG:
1240        case OP_THEN_ARG:
1241      SLJIT_ASSERT(common->mark_ptr != 0);      SLJIT_ASSERT(common->mark_ptr != 0);
1242      stack_restore = TRUE;      stack_restore = TRUE;
1243      if (!setmark_found)      if (!setmark_found)
# Line 1191  while (cc < ccend) Line 1245  while (cc < ccend)
1245        length += 2;        length += 2;
1246        setmark_found = TRUE;        setmark_found = TRUE;
1247        }        }
1248        if (common->control_head_ptr != 0)
1249          *needs_control_head = TRUE;
1250      cc += 1 + 2 + cc[1];      cc += 1 + 2 + cc[1];
1251      break;      break;
1252    
# Line 1310  if (length > 0) Line 1366  if (length > 0)
1366  return stack_restore ? no_frame : no_stack;  return stack_restore ? no_frame : no_stack;
1367  }  }
1368    
1369  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)
1370  {  {
1371  DEFINE_COMPILER;  DEFINE_COMPILER;
 pcre_uchar *ccend = bracketend(cc) - (1 + LINK_SIZE);  
1372  BOOL setsom_found = recursive;  BOOL setsom_found = recursive;
1373  BOOL setmark_found = recursive;  BOOL setmark_found = recursive;
1374  /* The last capture is a local variable even for recursions. */  /* The last capture is a local variable even for recursions. */
# Line 1325  SLJIT_UNUSED_ARG(stacktop); Line 1380  SLJIT_UNUSED_ARG(stacktop);
1380  SLJIT_ASSERT(stackpos >= stacktop + 2);  SLJIT_ASSERT(stackpos >= stacktop + 2);
1381    
1382  stackpos = STACK(stackpos);  stackpos = STACK(stackpos);
1383  if (recursive || (*cc != OP_CBRAPOS && *cc != OP_SCBRAPOS))  if (ccend == NULL)
1384    cc = next_opcode(common, cc);    {
1385      ccend = bracketend(cc) - (1 + LINK_SIZE);
1386      if (recursive || (*cc != OP_CBRAPOS && *cc != OP_SCBRAPOS))
1387        cc = next_opcode(common, cc);
1388      }
1389    
1390  SLJIT_ASSERT(cc != NULL);  SLJIT_ASSERT(cc != NULL);
1391  while (cc < ccend)  while (cc < ccend)
1392    switch(*cc)    switch(*cc)
# Line 1347  while (cc < ccend) Line 1407  while (cc < ccend)
1407    
1408      case OP_MARK:      case OP_MARK:
1409      case OP_PRUNE_ARG:      case OP_PRUNE_ARG:
1410        case OP_THEN_ARG:
1411      SLJIT_ASSERT(common->mark_ptr != 0);      SLJIT_ASSERT(common->mark_ptr != 0);
1412      if (!setmark_found)      if (!setmark_found)
1413        {        {
# Line 1427  OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), st Line 1488  OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), st
1488  SLJIT_ASSERT(stackpos == STACK(stacktop));  SLJIT_ASSERT(stackpos == STACK(stacktop));
1489  }  }
1490    
1491  static SLJIT_INLINE int get_private_data_length_for_copy(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)
1492  {  {
1493  int private_data_length = common->control_head_ptr ? 3 : 2;  int private_data_length = needs_control_head ? 3 : 2;
1494  int size;  int size;
1495  pcre_uchar *alternative;  pcre_uchar *alternative;
1496  /* Calculate the sum of the private machine words. */  /* Calculate the sum of the private machine words. */
# Line 1438  while (cc < ccend) Line 1499  while (cc < ccend)
1499    size = 0;    size = 0;
1500    switch(*cc)    switch(*cc)
1501      {      {
1502        case OP_KET:
1503        if (PRIVATE_DATA(cc) != 0)
1504          private_data_length++;
1505        cc += 1 + LINK_SIZE;
1506        break;
1507    
1508      case OP_ASSERT:      case OP_ASSERT:
1509      case OP_ASSERT_NOT:      case OP_ASSERT_NOT:
1510      case OP_ASSERTBACK:      case OP_ASSERTBACK:
# Line 1542  return private_data_length; Line 1609  return private_data_length;
1609  }  }
1610    
1611  static void copy_private_data(compiler_common *common, pcre_uchar *cc, pcre_uchar *ccend,  static void copy_private_data(compiler_common *common, pcre_uchar *cc, pcre_uchar *ccend,
1612    BOOL save, int stackptr, int stacktop)    BOOL save, int stackptr, int stacktop, BOOL needs_control_head)
1613  {  {
1614  DEFINE_COMPILER;  DEFINE_COMPILER;
1615  int srcw[2];  int srcw[2];
# Line 1563  stacktop = STACK(stacktop - 1); Line 1630  stacktop = STACK(stacktop - 1);
1630    
1631  if (!save)  if (!save)
1632    {    {
1633    stackptr += (common->control_head_ptr ? 2 : 1) * sizeof(sljit_sw);    stackptr += (needs_control_head ? 2 : 1) * sizeof(sljit_sw);
1634    if (stackptr < stacktop)    if (stackptr < stacktop)
1635      {      {
1636      OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(STACK_TOP), stackptr);      OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(STACK_TOP), stackptr);
# Line 1588  do Line 1655  do
1655      SLJIT_ASSERT(save && common->recursive_head_ptr != 0);      SLJIT_ASSERT(save && common->recursive_head_ptr != 0);
1656      count = 1;      count = 1;
1657      srcw[0] = common->recursive_head_ptr;      srcw[0] = common->recursive_head_ptr;
1658      if (common->control_head_ptr != 0)      if (needs_control_head)
1659        {        {
1660          SLJIT_ASSERT(common->control_head_ptr != 0);
1661        count = 2;        count = 2;
1662        srcw[1] = common->control_head_ptr;        srcw[1] = common->control_head_ptr;
1663        }        }
# Line 1605  do Line 1673  do
1673    
1674      switch(*cc)      switch(*cc)
1675        {        {
1676          case OP_KET:
1677          if (PRIVATE_DATA(cc) != 0)
1678            {
1679            count = 1;
1680            srcw[0] = PRIVATE_DATA(cc);
1681            }
1682          cc += 1 + LINK_SIZE;
1683          break;
1684    
1685        case OP_ASSERT:        case OP_ASSERT:
1686        case OP_ASSERT_NOT:        case OP_ASSERT_NOT:
1687        case OP_ASSERTBACK:        case OP_ASSERTBACK:
# Line 1851  if (save) Line 1928  if (save)
1928  SLJIT_ASSERT(cc == ccend && stackptr == stacktop && (save || (tmp1empty && tmp2empty)));  SLJIT_ASSERT(cc == ccend && stackptr == stacktop && (save || (tmp1empty && tmp2empty)));
1929  }  }
1930    
1931    static SLJIT_INLINE pcre_uchar *set_then_offsets(compiler_common *common, pcre_uchar *cc, pcre_uint8 *current_offset)
1932    {
1933    pcre_uchar *end = bracketend(cc);
1934    BOOL has_alternatives = cc[GET(cc, 1)] == OP_ALT;
1935    
1936    /* Assert captures then. */
1937    if (*cc >= OP_ASSERT && *cc <= OP_ASSERTBACK_NOT)
1938      current_offset = NULL;
1939    /* Conditional block does not. */
1940    if (*cc == OP_COND || *cc == OP_SCOND)
1941      has_alternatives = FALSE;
1942    
1943    cc = next_opcode(common, cc);
1944    if (has_alternatives)
1945      current_offset = common->then_offsets + (cc - common->start);
1946    
1947    while (cc < end)
1948      {
1949      if ((*cc >= OP_ASSERT && *cc <= OP_ASSERTBACK_NOT) || (*cc >= OP_ONCE && *cc <= OP_SCOND))
1950        cc = set_then_offsets(common, cc, current_offset);
1951      else
1952        {
1953        if (*cc == OP_ALT && has_alternatives)
1954          current_offset = common->then_offsets + (cc + 1 + LINK_SIZE - common->start);
1955        if (*cc >= OP_THEN && *cc <= OP_THEN_ARG && current_offset != NULL)
1956          *current_offset = 1;
1957        cc = next_opcode(common, cc);
1958        }
1959      }
1960    
1961    return end;
1962    }
1963    
1964  #undef CASE_ITERATOR_PRIVATE_DATA_1  #undef CASE_ITERATOR_PRIVATE_DATA_1
1965  #undef CASE_ITERATOR_PRIVATE_DATA_2A  #undef CASE_ITERATOR_PRIVATE_DATA_2A
1966  #undef CASE_ITERATOR_PRIVATE_DATA_2B  #undef CASE_ITERATOR_PRIVATE_DATA_2B
# Line 1914  while (list_item) Line 2024  while (list_item)
2024  common->stubs = NULL;  common->stubs = NULL;
2025  }  }
2026    
2027  static SLJIT_INLINE void decrease_call_count(compiler_common *common)  static SLJIT_INLINE void count_match(compiler_common *common)
2028  {  {
2029  DEFINE_COMPILER;  DEFINE_COMPILER;
2030    
2031  OP2(SLJIT_SUB | SLJIT_SET_E, CALL_COUNT, 0, CALL_COUNT, 0, SLJIT_IMM, 1);  OP2(SLJIT_SUB | SLJIT_SET_E, COUNT_MATCH, 0, COUNT_MATCH, 0, SLJIT_IMM, 1);
2032  add_jump(compiler, &common->calllimit, JUMP(SLJIT_C_ZERO));  add_jump(compiler, &common->calllimit, JUMP(SLJIT_C_ZERO));
2033  }  }
2034    
# Line 1998  else Line 2108  else
2108  OP1(SLJIT_MOV, STACK_TOP, 0, ARGUMENTS, 0);  OP1(SLJIT_MOV, STACK_TOP, 0, ARGUMENTS, 0);
2109  if (common->mark_ptr != 0)  if (common->mark_ptr != 0)
2110    OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), common->mark_ptr, SLJIT_IMM, 0);    OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), common->mark_ptr, SLJIT_IMM, 0);
2111  SLJIT_ASSERT(common->control_head_ptr != 0);  if (common->control_head_ptr != 0)
2112  OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), common->control_head_ptr, SLJIT_IMM, 0);    OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), common->control_head_ptr, SLJIT_IMM, 0);
2113  OP1(SLJIT_MOV, STACK_TOP, 0, SLJIT_MEM1(STACK_TOP), SLJIT_OFFSETOF(jit_arguments, stack));  OP1(SLJIT_MOV, STACK_TOP, 0, SLJIT_MEM1(STACK_TOP), SLJIT_OFFSETOF(jit_arguments, stack));
2114  OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), common->start_ptr);  OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), common->start_ptr);
2115  OP1(SLJIT_MOV, STACK_TOP, 0, SLJIT_MEM1(STACK_TOP), SLJIT_OFFSETOF(struct sljit_stack, base));  OP1(SLJIT_MOV, STACK_TOP, 0, SLJIT_MEM1(STACK_TOP), SLJIT_OFFSETOF(struct sljit_stack, base));
2116  }  }
2117    
2118  static sljit_sw do_check_control_chain(sljit_sw *current)  static sljit_sw SLJIT_CALL do_search_mark(sljit_sw *current, const pcre_uchar *skip_arg)
2119  {  {
2120  sljit_sw return_value = 0;  while (current != NULL)
   
 SLJIT_ASSERT(current != NULL);  
 do  
2121    {    {
2122    switch (current[-2])    switch (current[-2])
2123      {      {
2124      case type_commit:      case type_then_trap:
     /* Commit overwrites all. */  
     return -1;  
   
     case type_prune:  
2125      break;      break;
2126    
2127      case type_skip:      case type_mark:
2128      /* Overwrites prune, but not other skips. */      if (STRCMP_UC_UC(skip_arg, (pcre_uchar *)current[-3]) == 0)
2129      if (return_value == 0)        return current[-4];
       return_value = current[-3];  
2130      break;      break;
2131    
2132      default:      default:
# Line 2033  do Line 2135  do
2135      }      }
2136    current = (sljit_sw*)current[-1];    current = (sljit_sw*)current[-1];
2137    }    }
2138  while (current != NULL);  return -1;
 return return_value;  
2139  }  }
2140    
2141  static SLJIT_INLINE void copy_ovector(compiler_common *common, int topbracket)  static SLJIT_INLINE void copy_ovector(compiler_common *common, int topbracket)
# Line 2106  OP1(SLJIT_MOV, SLJIT_SAVED_REG1, 0, SLJI Line 2207  OP1(SLJIT_MOV, SLJIT_SAVED_REG1, 0, SLJI
2207  OP1(SLJIT_MOV, SLJIT_SCRATCH_REG2, 0, SLJIT_MEM1(SLJIT_SCRATCH_REG2), SLJIT_OFFSETOF(jit_arguments, offsets));  OP1(SLJIT_MOV, SLJIT_SCRATCH_REG2, 0, SLJIT_MEM1(SLJIT_SCRATCH_REG2), SLJIT_OFFSETOF(jit_arguments, offsets));
2208    
2209  jump = CMP(SLJIT_C_SIG_LESS, SLJIT_SCRATCH_REG3, 0, SLJIT_IMM, 3);  jump = CMP(SLJIT_C_SIG_LESS, SLJIT_SCRATCH_REG3, 0, SLJIT_IMM, 3);
2210  OP2(SLJIT_SUB, SLJIT_SCRATCH_REG3, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), common->mode == JIT_PARTIAL_HARD_COMPILE ? common->start_ptr : (common->hit_start + sizeof(sljit_sw)), SLJIT_SAVED_REG1, 0);  OP2(SLJIT_SUB, SLJIT_SCRATCH_REG3, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), common->mode == JIT_PARTIAL_HARD_COMPILE ? common->start_ptr : (common->hit_start + (int)sizeof(sljit_sw)), SLJIT_SAVED_REG1, 0);
2211  #if defined COMPILE_PCRE16 || defined COMPILE_PCRE32  #if defined COMPILE_PCRE16 || defined COMPILE_PCRE32
2212  OP2(SLJIT_ASHR, SLJIT_SCRATCH_REG3, 0, SLJIT_SCRATCH_REG3, 0, SLJIT_IMM, UCHAR_SHIFT);  OP2(SLJIT_ASHR, SLJIT_SCRATCH_REG3, 0, SLJIT_SCRATCH_REG3, 0, SLJIT_IMM, UCHAR_SHIFT);
2213  #endif  #endif
# Line 2274  return (bit < 256) ? ((0 << 8) | bit) : Line 2375  return (bit < 256) ? ((0 << 8) | bit) :
2375    
2376  static void check_partial(compiler_common *common, BOOL force)  static void check_partial(compiler_common *common, BOOL force)
2377  {  {
2378  /* Checks whether a partial matching is occured. Does not modify registers. */  /* Checks whether a partial matching is occurred. Does not modify registers. */
2379  DEFINE_COMPILER;  DEFINE_COMPILER;
2380  struct sljit_jump *jump = NULL;  struct sljit_jump *jump = NULL;
2381    
# Line 2798  if (newlinecheck) Line 2899  if (newlinecheck)
2899  return mainloop;  return mainloop;
2900  }  }
2901    
2902  #define MAX_N_CHARS 3  static int scan_prefix(compiler_common *common, pcre_uchar *cc, pcre_uint32 *chars, int max_chars)
   
 static SLJIT_INLINE BOOL fast_forward_first_n_chars(compiler_common *common, BOOL firstline)  
2903  {  {
2904  DEFINE_COMPILER;  /* Recursive function, which scans prefix literals. */
2905  struct sljit_label *start;  int len, repeat, len_save, consumed = 0;
2906  struct sljit_jump *quit;  pcre_int32 caseless, chr, mask;
2907  pcre_uint32 chars[MAX_N_CHARS * 2];  pcre_uchar *alternative, *cc_save;
2908  pcre_uchar *cc = common->start + 1 + LINK_SIZE;  BOOL last, any;
 int location = 0;  
 pcre_int32 len, c, bit, caseless;  
 int must_stop;  
   
 /* We do not support alternatives now. */  
 if (*(common->start + GET(common->start, 1)) == OP_ALT)  
   return FALSE;  
2909    
2910    repeat = 1;
2911  while (TRUE)  while (TRUE)
2912    {    {
2913      last = TRUE;
2914      any = FALSE;
2915    caseless = 0;    caseless = 0;
2916    must_stop = 1;    switch (*cc)
   switch(*cc)  
2917      {      {
     case OP_CHAR:  
     must_stop = 0;  
     cc++;  
     break;  
   
2918      case OP_CHARI:      case OP_CHARI:
2919      caseless = 1;      caseless = 1;
2920      must_stop = 0;      case OP_CHAR:
2921        last = FALSE;
2922      cc++;      cc++;
2923      break;      break;
2924    
# Line 2853  while (TRUE) Line 2943  while (TRUE)
2943      cc++;      cc++;
2944      break;      break;
2945    
2946        case OP_EXACTI:
2947        caseless = 1;
2948      case OP_EXACT:      case OP_EXACT:
2949        repeat = GET2(cc, 1);
2950        last = FALSE;
2951      cc += 1 + IMM2_SIZE;      cc += 1 + IMM2_SIZE;
2952      break;      break;
2953    
# Line 2864  while (TRUE) Line 2958  while (TRUE)
2958      cc++;      cc++;
2959      break;      break;
2960    
2961      case OP_EXACTI:      case OP_KET:
2962      caseless = 1;      cc += 1 + LINK_SIZE;
2963      cc += 1 + IMM2_SIZE;      continue;
2964    
2965        case OP_ALT:
2966        cc += GET(cc, 1);
2967        continue;
2968    
2969        case OP_ONCE:
2970        case OP_ONCE_NC:
2971        case OP_BRA:
2972        case OP_BRAPOS:
2973        case OP_CBRA:
2974        case OP_CBRAPOS:
2975        alternative = cc + GET(cc, 1);
2976        while (*alternative == OP_ALT)
2977          {
2978          max_chars = scan_prefix(common, alternative + 1 + LINK_SIZE, chars, max_chars);
2979          if (max_chars == 0)
2980            return consumed;
2981          alternative += GET(alternative, 1);
2982          }
2983    
2984        if (*cc == OP_CBRA || *cc == OP_CBRAPOS)
2985          cc += IMM2_SIZE;
2986        cc += 1 + LINK_SIZE;
2987        continue;
2988    
2989        case OP_CLASS:
2990        case OP_NCLASS:
2991        any = TRUE;
2992        cc += 1 + 32 / sizeof(pcre_uchar);
2993        break;
2994    
2995    #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
2996        case OP_XCLASS:
2997        any = TRUE;
2998        cc += GET(cc, 1);
2999      break;      break;
3000    #endif
3001    
3002        case OP_NOT_DIGIT:
3003        case OP_DIGIT:
3004        case OP_NOT_WHITESPACE:
3005        case OP_WHITESPACE:
3006        case OP_NOT_WORDCHAR:
3007        case OP_WORDCHAR:
3008        case OP_ANY:
3009        case OP_ALLANY:
3010        any = TRUE;
3011        cc++;
3012        break;
3013    
3014    #ifdef SUPPORT_UCP
3015        case OP_NOTPROP:
3016        case OP_PROP:
3017        any = TRUE;
3018        cc += 1 + 2;
3019        break;
3020    #endif
3021    
3022        case OP_TYPEEXACT:
3023        repeat = GET2(cc, 1);
3024        cc += 1 + IMM2_SIZE;
3025        continue;
3026    
3027        default:
3028        return consumed;
3029        }
3030    
3031      if (any)
3032        {
3033    #ifdef SUPPORT_UTF
3034        if (common->utf) return consumed;
3035    #endif
3036    #if defined COMPILE_PCRE8
3037        mask = 0xff;
3038    #elif defined COMPILE_PCRE16
3039        mask = 0xffff;
3040    #elif defined COMPILE_PCRE32
3041        mask = 0xffffffff;
3042    #else
3043        SLJIT_ASSERT_STOP();
3044    #endif
3045    
3046        do
3047          {
3048          chars[0] = mask;
3049          chars[1] = mask;
3050    
3051          if (--max_chars == 0)
3052            return consumed;
3053          consumed++;
3054          chars += 2;
3055          }
3056        while (--repeat > 0);
3057    
3058        repeat = 1;
3059        continue;
3060        }
3061    
3062      len = 1;
3063    #ifdef SUPPORT_UTF
3064      if (common->utf && HAS_EXTRALEN(*cc)) len += GET_EXTRALEN(*cc);
3065    #endif
3066    
3067      if (caseless != 0 && char_has_othercase(common, cc))
3068        {
3069        caseless = char_get_othercase_bit(common, cc);
3070        if (caseless == 0)
3071          return consumed;
3072    #ifdef COMPILE_PCRE8
3073        caseless = ((caseless & 0xff) << 8) | (len - (caseless >> 8));
3074    #else
3075        if ((caseless & 0x100) != 0)
3076          caseless = ((caseless & 0xff) << 16) | (len - (caseless >> 9));
3077        else
3078          caseless = ((caseless & 0xff) << 8) | (len - (caseless >> 9));
3079    #endif
3080        }
3081      else
3082        caseless = 0;
3083    
3084      len_save = len;
3085      cc_save = cc;
3086      while (TRUE)
3087        {
3088        do
3089          {
3090          chr = *cc;
3091    #ifdef COMPILE_PCRE32
3092          if (SLJIT_UNLIKELY(chr == NOTACHAR))
3093            return consumed;
3094    #endif
3095          mask = 0;
3096          if (len == (caseless & 0xff))
3097            {
3098            mask = caseless >> 8;
3099            chr |= mask;
3100            }
3101    
3102          if (chars[0] == NOTACHAR)
3103            {
3104            chars[0] = chr;
3105            chars[1] = mask;
3106            }
3107          else
3108            {
3109            mask |= chars[0] ^ chr;
3110            chr |= mask;
3111            chars[0] = chr;
3112            chars[1] |= mask;
3113            }
3114    
3115          len--;
3116          if (--max_chars == 0)
3117            return consumed;
3118          consumed++;
3119          chars += 2;
3120          cc++;
3121          }
3122        while (len > 0);
3123    
3124        if (--repeat == 0)
3125          break;
3126    
3127        len = len_save;
3128        cc = cc_save;
3129        }
3130    
3131      repeat = 1;
3132      if (last)
3133        return consumed;
3134      }
3135    }
3136    
3137    #define MAX_N_CHARS 16
3138    
3139    static SLJIT_INLINE BOOL fast_forward_first_n_chars(compiler_common *common, BOOL firstline)
3140    {
3141    DEFINE_COMPILER;
3142    struct sljit_label *start;
3143    struct sljit_jump *quit;
3144    pcre_uint32 chars[MAX_N_CHARS * 2];
3145    pcre_uint8 ones[MAX_N_CHARS];
3146    pcre_uint32 mask;
3147    int i, max;
3148    int offsets[3];
3149    
3150      default:  for (i = 0; i < MAX_N_CHARS; i++)
3151      must_stop = 2;    {
3152      break;    chars[i << 1] = NOTACHAR;
3153      }    chars[(i << 1) + 1] = 0;
3154      }
3155    
3156    if (must_stop == 2)  max = scan_prefix(common, common->start, chars, MAX_N_CHARS);
       break;  
3157    
3158    len = 1;  if (max <= 1)
3159  #ifdef SUPPORT_UTF    return FALSE;
   if (common->utf && HAS_EXTRALEN(cc[0])) len += GET_EXTRALEN(cc[0]);  
 #endif  
3160    
3161    if (caseless && char_has_othercase(common, cc))  for (i = 0; i < max; i++)
3162      {
3163      mask = chars[(i << 1) + 1];
3164      ones[i] = ones_in_half_byte[mask & 0xf];
3165      mask >>= 4;
3166      while (mask != 0)
3167      {      {
3168      caseless = char_get_othercase_bit(common, cc);      ones[i] += ones_in_half_byte[mask & 0xf];
3169      if (caseless == 0)      mask >>= 4;
       return FALSE;  
 #ifdef COMPILE_PCRE8  
     caseless = ((caseless & 0xff) << 8) | (len - (caseless >> 8));  
 #else  
     if ((caseless & 0x100) != 0)  
       caseless = ((caseless & 0xff) << 16) | (len - (caseless >> 9));  
     else  
       caseless = ((caseless & 0xff) << 8) | (len - (caseless >> 9));  
 #endif  
3170      }      }
3171    else    }
     caseless = 0;  
3172    
3173    while (len > 0 && location < MAX_N_CHARS * 2)  offsets[0] = -1;
3174      {  /* Scan forward. */
3175      c = *cc;  for (i = 0; i < max; i++)
3176      bit = 0;    if (ones[i] <= 2) {
3177      if (len == (caseless & 0xff))      offsets[0] = i;
3178        break;
3179      }
3180    
3181    if (offsets[0] == -1)
3182      return FALSE;
3183    
3184    /* Scan backward. */
3185    offsets[1] = -1;
3186    for (i = max - 1; i > offsets[0]; i--)
3187      if (ones[i] <= 2) {
3188        offsets[1] = i;
3189        break;
3190      }
3191    
3192    offsets[2] = -1;
3193    if (offsets[1] >= 0)
3194      {
3195      /* Scan from middle. */
3196      for (i = (offsets[0] + offsets[1]) / 2 + 1; i < offsets[1]; i++)
3197        if (ones[i] <= 2)
3198        {        {
3199        bit = caseless >> 8;        offsets[2] = i;
3200        c |= bit;        break;
3201        }        }
3202    
3203      chars[location] = c;    if (offsets[2] == -1)
3204      chars[location + 1] = bit;      {
3205        for (i = (offsets[0] + offsets[1]) / 2; i > offsets[0]; i--)
3206      len--;        if (ones[i] <= 2)
3207      location += 2;          {
3208      cc++;          offsets[2] = i;
3209            break;
3210            }
3211      }      }
   
   if (location >= MAX_N_CHARS * 2 || must_stop != 0)  
     break;  
3212    }    }
3213    
3214  /* At least two characters are required. */  SLJIT_ASSERT(offsets[1] == -1 || (offsets[0] < offsets[1]));
3215  if (location < 2 * 2)  SLJIT_ASSERT(offsets[2] == -1 || (offsets[0] < offsets[2] && offsets[1] > offsets[2]));
3216      return FALSE;  
3217    chars[0] = chars[offsets[0] << 1];
3218    chars[1] = chars[(offsets[0] << 1) + 1];
3219    if (offsets[2] >= 0)
3220      {
3221      chars[2] = chars[offsets[2] << 1];
3222      chars[3] = chars[(offsets[2] << 1) + 1];
3223      }
3224    if (offsets[1] >= 0)
3225      {
3226      chars[4] = chars[offsets[1] << 1];
3227      chars[5] = chars[(offsets[1] << 1) + 1];
3228      }
3229    
3230    max -= 1;
3231  if (firstline)  if (firstline)
3232    {    {
3233    SLJIT_ASSERT(common->first_line_end != 0);    SLJIT_ASSERT(common->first_line_end != 0);
3234    OP1(SLJIT_MOV, TMP3, 0, STR_END, 0);    OP1(SLJIT_MOV, TMP3, 0, STR_END, 0);
3235    OP2(SLJIT_SUB, STR_END, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), common->first_line_end, SLJIT_IMM, IN_UCHARS((location >> 1) - 1));    OP2(SLJIT_SUB, STR_END, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), common->first_line_end, SLJIT_IMM, IN_UCHARS(max));
3236    }    }
3237  else  else
3238    OP2(SLJIT_SUB, STR_END, 0, STR_END, 0, SLJIT_IMM, IN_UCHARS((location >> 1) - 1));    OP2(SLJIT_SUB, STR_END, 0, STR_END, 0, SLJIT_IMM, IN_UCHARS(max));
3239    
3240  start = LABEL();  start = LABEL();
3241  quit = CMP(SLJIT_C_GREATER_EQUAL, STR_PTR, 0, STR_END, 0);  quit = CMP(SLJIT_C_GREATER_EQUAL, STR_PTR, 0, STR_END, 0);
3242    
3243  OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(0));  OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(offsets[0]));
3244  OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(1));  if (offsets[1] >= 0)
3245      OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(offsets[1]));
3246  OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));  OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
3247    
3248  if (chars[1] != 0)  if (chars[1] != 0)
3249    OP2(SLJIT_OR, TMP1, 0, TMP1, 0, SLJIT_IMM, chars[1]);    OP2(SLJIT_OR, TMP1, 0, TMP1, 0, SLJIT_IMM, chars[1]);
3250  CMPTO(SLJIT_C_NOT_EQUAL, TMP1, 0, SLJIT_IMM, chars[0], start);  CMPTO(SLJIT_C_NOT_EQUAL, TMP1, 0, SLJIT_IMM, chars[0], start);
3251  if (location > 2 * 2)  if (offsets[2] >= 0)
3252    OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(1));    OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(offsets[2] - 1));
3253  if (chars[3] != 0)  
3254    OP2(SLJIT_OR, TMP2, 0, TMP2, 0, SLJIT_IMM, chars[3]);  if (offsets[1] >= 0)
 CMPTO(SLJIT_C_NOT_EQUAL, TMP2, 0, SLJIT_IMM, chars[2], start);  
 if (location > 2 * 2)  
3255    {    {
3256    if (chars[5] != 0)    if (chars[5] != 0)
3257      OP2(SLJIT_OR, TMP1, 0, TMP1, 0, SLJIT_IMM, chars[5]);      OP2(SLJIT_OR, TMP2, 0, TMP2, 0, SLJIT_IMM, chars[5]);
3258    CMPTO(SLJIT_C_NOT_EQUAL, TMP1, 0, SLJIT_IMM, chars[4], start);    CMPTO(SLJIT_C_NOT_EQUAL, TMP2, 0, SLJIT_IMM, chars[4], start);
3259      }
3260    
3261    if (offsets[2] >= 0)
3262      {
3263      if (chars[3] != 0)
3264        OP2(SLJIT_OR, TMP1, 0, TMP1, 0, SLJIT_IMM, chars[3]);
3265      CMPTO(SLJIT_C_NOT_EQUAL, TMP1, 0, SLJIT_IMM, chars[2], start);
3266    }    }
3267  OP2(SLJIT_SUB, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));  OP2(SLJIT_SUB, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
3268    
# Line 2961  JUMPHERE(quit); Line 3271  JUMPHERE(quit);
3271  if (firstline)  if (firstline)
3272    OP1(SLJIT_MOV, STR_END, 0, TMP3, 0);    OP1(SLJIT_MOV, STR_END, 0, TMP3, 0);
3273  else  else
3274    OP2(SLJIT_ADD, STR_END, 0, STR_END, 0, SLJIT_IMM, IN_UCHARS((location >> 1) - 1));    OP2(SLJIT_ADD, STR_END, 0, STR_END, 0, SLJIT_IMM, IN_UCHARS(max));
3275  return TRUE;  return TRUE;
3276  }  }
3277    
# Line 3110  if (firstline) Line 3420  if (firstline)
3420    OP1(SLJIT_MOV, STR_END, 0, TMP3, 0);    OP1(SLJIT_MOV, STR_END, 0, TMP3, 0);
3421  }  }
3422    
3423  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);
3424    
3425  static SLJIT_INLINE void fast_forward_start_bits(compiler_common *common, sljit_uw start_bits, BOOL firstline)  static SLJIT_INLINE void fast_forward_start_bits(compiler_common *common, pcre_uint8 *start_bits, BOOL firstline)
3426  {  {
3427  DEFINE_COMPILER;  DEFINE_COMPILER;
3428  struct sljit_label *start;  struct sljit_label *start;
3429  struct sljit_jump *quit;  struct sljit_jump *quit;
3430  struct sljit_jump *found = NULL;  struct sljit_jump *found = NULL;
3431  jump_list *matches = NULL;  jump_list *matches = NULL;
 pcre_uint8 inverted_start_bits[32];  
 int i;  
3432  #ifndef COMPILE_PCRE8  #ifndef COMPILE_PCRE8
3433  struct sljit_jump *jump;  struct sljit_jump *jump;
3434  #endif  #endif
3435    
 for (i = 0; i < 32; ++i)  
   inverted_start_bits[i] = ~(((pcre_uint8*)start_bits)[i]);  
   
3436  if (firstline)  if (firstline)
3437    {    {
3438    SLJIT_ASSERT(common->first_line_end != 0);    SLJIT_ASSERT(common->first_line_end != 0);
# Line 3143  if (common->utf) Line 3448  if (common->utf)
3448    OP1(SLJIT_MOV, TMP3, 0, TMP1, 0);    OP1(SLJIT_MOV, TMP3, 0, TMP1, 0);
3449  #endif  #endif
3450    
3451  if (!check_class_ranges(common, inverted_start_bits, (inverted_start_bits[31] & 0x80) != 0, &matches))  if (!check_class_ranges(common, start_bits, (start_bits[31] & 0x80) != 0, TRUE, &matches))
3452    {    {
3453  #ifndef COMPILE_PCRE8  #ifndef COMPILE_PCRE8
3454    jump = CMP(SLJIT_C_LESS, TMP1, 0, SLJIT_IMM, 255);    jump = CMP(SLJIT_C_LESS, TMP1, 0, SLJIT_IMM, 255);
# Line 3152  if (!check_class_ranges(common, inverted Line 3457  if (!check_class_ranges(common, inverted
3457  #endif  #endif
3458    OP2(SLJIT_AND, TMP2, 0, TMP1, 0, SLJIT_IMM, 0x7);    OP2(SLJIT_AND, TMP2, 0, TMP1, 0, SLJIT_IMM, 0x7);
3459    OP2(SLJIT_LSHR, TMP1, 0, TMP1, 0, SLJIT_IMM, 3);    OP2(SLJIT_LSHR, TMP1, 0, TMP1, 0, SLJIT_IMM, 3);
3460    OP1(SLJIT_MOV_UB, TMP1, 0, SLJIT_MEM1(TMP1), start_bits);    OP1(SLJIT_MOV_UB, TMP1, 0, SLJIT_MEM1(TMP1), (sljit_sw)start_bits);
3461    OP2(SLJIT_SHL, TMP2, 0, SLJIT_IMM, 1, TMP2, 0);    OP2(SLJIT_SHL, TMP2, 0, SLJIT_IMM, 1, TMP2, 0);
3462    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);
3463    found = JUMP(SLJIT_C_NOT_ZERO);    found = JUMP(SLJIT_C_NOT_ZERO);
# Line 3412  sljit_emit_fast_return(compiler, SLJIT_M Line 3717  sljit_emit_fast_return(compiler, SLJIT_M
3717  static BOOL check_ranges(compiler_common *common, int *ranges, jump_list **backtracks, BOOL readch)  static BOOL check_ranges(compiler_common *common, int *ranges, jump_list **backtracks, BOOL readch)
3718  {  {
3719  DEFINE_COMPILER;  DEFINE_COMPILER;
 struct sljit_jump *jump;  
3720    
3721  if (ranges[0] < 0)  if (ranges[0] < 0 || ranges[0] > 4)
3722    return FALSE;    return FALSE;
3723    
3724    /* No character is accepted. */
3725    if (ranges[0] == 0 && ranges[1] == 0)
3726      add_jump(compiler, backtracks, JUMP(SLJIT_JUMP));
3727    
3728    if (readch)
3729      read_char(common);
3730    
3731  switch(ranges[0])  switch(ranges[0])
3732    {    {
3733      case 0:
3734      /* When ranges[1] != 0, all characters are accepted. */
3735      return TRUE;
3736    
3737    case 1:    case 1:
   if (readch)  
     read_char(common);  
3738    add_jump(compiler, backtracks, CMP(ranges[1] == 0 ? SLJIT_C_LESS : SLJIT_C_GREATER_EQUAL, 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[2]));
3739    return TRUE;    return TRUE;
3740    
3741    case 2:    case 2:
3742    if (readch)    if (ranges[2] + 1 != ranges[3])
3743      read_char(common);      {
3744    OP2(SLJIT_SUB, TMP1, 0, TMP1, 0, SLJIT_IMM, ranges[2]);      OP2(SLJIT_SUB, TMP1, 0, TMP1, 0, SLJIT_IMM, ranges[2]);
3745    add_jump(compiler, backtracks, CMP(ranges[1] != 0 ? SLJIT_C_LESS : SLJIT_C_GREATER_EQUAL, TMP1, 0, SLJIT_IMM, ranges[3] - 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]));
3746        }
3747      else
3748        add_jump(compiler, backtracks, CMP(ranges[1] != 0 ? SLJIT_C_EQUAL : SLJIT_C_NOT_EQUAL, TMP1, 0, SLJIT_IMM, ranges[2]));
3749    return TRUE;    return TRUE;
3750    
3751    case 4:    case 3:
3752    if (ranges[2] + 1 == ranges[3] && ranges[4] + 1 == ranges[5])    if (ranges[1] != 0)
3753      {      {
3754      if (readch)      add_jump(compiler, backtracks, CMP(SLJIT_C_GREATER_EQUAL, TMP1, 0, SLJIT_IMM, ranges[4]));
3755        read_char(common);      if (ranges[2] + 1 != ranges[3])
     if (ranges[1] != 0)  
3756        {        {
3757        add_jump(compiler, backtracks, CMP(SLJIT_C_EQUAL, TMP1, 0, SLJIT_IMM, ranges[2]));        OP2(SLJIT_SUB, TMP1, 0, TMP1, 0, SLJIT_IMM, ranges[2]);
3758        add_jump(compiler, backtracks, CMP(SLJIT_C_EQUAL, TMP1, 0, SLJIT_IMM, ranges[4]));        add_jump(compiler, backtracks, CMP(SLJIT_C_LESS, TMP1, 0, SLJIT_IMM, ranges[3] - ranges[2]));
3759        }        }
3760      else      else
3761          add_jump(compiler, backtracks, CMP(SLJIT_C_EQUAL, TMP1, 0, SLJIT_IMM, ranges[2]));
3762        return TRUE;
3763        }
3764    
3765      add_jump(compiler, backtracks, CMP(SLJIT_C_LESS, TMP1, 0, SLJIT_IMM, ranges[2]));
3766      if (ranges[3] + 1 != ranges[4])
3767        {
3768        OP2(SLJIT_SUB, TMP1, 0, TMP1, 0, SLJIT_IMM, ranges[3]);
3769        add_jump(compiler, backtracks, CMP(SLJIT_C_LESS, TMP1, 0, SLJIT_IMM, ranges[4] - ranges[3]));
3770        }
3771      else
3772        add_jump(compiler, backtracks, CMP(SLJIT_C_EQUAL, TMP1, 0, SLJIT_IMM, ranges[3]));
3773      return TRUE;
3774    
3775      case 4:
3776      if ((ranges[3] - ranges[2]) == (ranges[5] - ranges[4])
3777          && (ranges[2] | (ranges[4] - ranges[2])) == ranges[4]
3778          && is_powerof2(ranges[4] - ranges[2]))
3779        {
3780        OP2(SLJIT_OR, TMP1, 0, TMP1, 0, SLJIT_IMM, ranges[4] - ranges[2]);
3781        if (ranges[4] + 1 != ranges[5])
3782        {        {
3783        jump = CMP(SLJIT_C_EQUAL, TMP1, 0, SLJIT_IMM, ranges[2]);        OP2(SLJIT_SUB, TMP1, 0, TMP1, 0, SLJIT_IMM, ranges[4]);
3784        add_jump(compiler, backtracks, CMP(SLJIT_C_NOT_EQUAL, 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]));
       JUMPHERE(jump);  
3785        }        }
3786        else
3787          add_jump(compiler, backtracks, CMP(ranges[1] != 0 ? SLJIT_C_EQUAL : SLJIT_C_NOT_EQUAL, TMP1, 0, SLJIT_IMM, ranges[4]));
3788      return TRUE;      return TRUE;
3789      }      }
3790    if ((ranges[3] - ranges[2]) == (ranges[5] - ranges[4]) && is_powerof2(ranges[4] - ranges[2]))  
3791      if (ranges[1] != 0)
3792      {      {
3793      if (readch)      if (ranges[2] + 1 != ranges[3])
3794        read_char(common);        {
3795      OP2(SLJIT_OR, TMP1, 0, TMP1, 0, SLJIT_IMM, ranges[4] - ranges[2]);        OP2(SLJIT_SUB, TMP1, 0, TMP1, 0, SLJIT_IMM, ranges[2]);
3796      OP2(SLJIT_SUB, TMP1, 0, TMP1, 0, SLJIT_IMM, ranges[4]);        add_jump(compiler, backtracks, CMP(SLJIT_C_LESS, TMP1, 0, SLJIT_IMM, ranges[3] - ranges[2]));
3797      add_jump(compiler, backtracks, CMP(ranges[1] != 0 ? SLJIT_C_LESS : SLJIT_C_GREATER_EQUAL, TMP1, 0, SLJIT_IMM, ranges[5] - ranges[4]));        ranges[4] -= ranges[2];
3798          ranges[5] -= ranges[2];
3799          }
3800        else
3801          add_jump(compiler, backtracks, CMP(SLJIT_C_EQUAL, TMP1, 0, SLJIT_IMM, ranges[2]));
3802    
3803        if (ranges[4] + 1 != ranges[5])
3804          {
3805          OP2(SLJIT_SUB, TMP1, 0, TMP1, 0, SLJIT_IMM, ranges[4]);
3806          add_jump(compiler, backtracks, CMP(SLJIT_C_LESS, TMP1, 0, SLJIT_IMM, ranges[5] - ranges[4]));
3807          }
3808        else
3809          add_jump(compiler, backtracks, CMP(SLJIT_C_EQUAL, TMP1, 0, SLJIT_IMM, ranges[4]));
3810      return TRUE;      return TRUE;
3811      }      }
3812    return FALSE;  
3813      OP2(SLJIT_SUB, TMP1, 0, TMP1, 0, SLJIT_IMM, ranges[2]);
3814      add_jump(compiler, backtracks, CMP(SLJIT_C_GREATER_EQUAL, TMP1, 0, SLJIT_IMM, ranges[5] - ranges[2]));
3815      if (ranges[3] + 1 != ranges[4])
3816        {
3817        OP2(SLJIT_SUB, TMP1, 0, TMP1, 0, SLJIT_IMM, ranges[3] - ranges[2]);
3818        add_jump(compiler, backtracks, CMP(SLJIT_C_LESS, TMP1, 0, SLJIT_IMM, ranges[4] - ranges[3]));
3819        }
3820      else
3821        add_jump(compiler, backtracks, CMP(SLJIT_C_EQUAL, TMP1, 0, SLJIT_IMM, ranges[3] - ranges[2]));
3822      return TRUE;
3823    
3824    default:    default:
3825      SLJIT_ASSERT_STOP();
3826    return FALSE;    return FALSE;
3827    }    }
3828  }  }
# Line 3496  if (bit != 0) Line 3857  if (bit != 0)
3857  ranges[0] = length;  ranges[0] = length;
3858  }  }
3859    
3860  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)
3861  {  {
3862  int ranges[2 + MAX_RANGE_SIZE];  int ranges[2 + MAX_RANGE_SIZE];
3863  pcre_uint8 bit, cbit, all;  pcre_uint8 bit, cbit, all;
3864  int i, byte, length = 0;  int i, byte, length = 0;
3865    
3866  bit = bits[0] & 0x1;  bit = bits[0] & 0x1;
3867  ranges[1] = bit;  ranges[1] = !invert ? bit : (bit ^ 0x1);
3868  /* Can be 0 or 255. */  /* All bits will be zero or one (since bit is zero or one). */
3869  all = -bit;  all = -bit;
3870    
3871  for (i = 0; i < 256; )  for (i = 0; i < 256; )
# Line 3925  static void compile_xclass_matchingpath( Line 4286  static void compile_xclass_matchingpath(
4286  {  {
4287  DEFINE_COMPILER;  DEFINE_COMPILER;
4288  jump_list *found = NULL;  jump_list *found = NULL;
4289  jump_list **list = (*cc & XCL_NOT) == 0 ? &found : backtracks;  jump_list **list = (cc[0] & XCL_NOT) == 0 ? &found : backtracks;
4290  pcre_int32 c, charoffset;  pcre_int32 c, charoffset;
 const pcre_uint32 *other_cases;  
4291  struct sljit_jump *jump = NULL;  struct sljit_jump *jump = NULL;
4292  pcre_uchar *ccbegin;  pcre_uchar *ccbegin;
4293  int compares, invertcmp, numberofcmps;  int compares, invertcmp, numberofcmps;
4294    
4295  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
4296  BOOL needstype = FALSE, needsscript = FALSE, needschar = FALSE;  BOOL needstype = FALSE, needsscript = FALSE, needschar = FALSE;
4297  BOOL charsaved = FALSE;  BOOL charsaved = FALSE;
4298  int typereg = TMP1, scriptreg = TMP1;  int typereg = TMP1, scriptreg = TMP1;
4299    const pcre_uint32 *other_cases;
4300  pcre_int32 typeoffset;  pcre_int32 typeoffset;
4301  #endif  #endif
4302    
# Line 3943  pcre_int32 typeoffset; Line 4305  pcre_int32 typeoffset;
4305  detect_partial_match(common, backtracks);  detect_partial_match(common, backtracks);
4306  read_char(common);  read_char(common);
4307    
4308  if ((*cc++ & XCL_MAP) != 0)  cc++;
4309    if ((cc[-1] & XCL_HASPROP) == 0)
4310    {    {
4311    OP1(SLJIT_MOV, TMP3, 0, TMP1, 0);    if ((cc[-1] & XCL_MAP) != 0)
4312  #ifndef COMPILE_PCRE8      {
4313    jump = CMP(SLJIT_C_GREATER, TMP1, 0, SLJIT_IMM, 255);      OP1(SLJIT_MOV, TMP3, 0, TMP1, 0);
4314  #elif defined SUPPORT_UTF  #ifdef SUPPORT_UCP
4315    if (common->utf)      charsaved = TRUE;
     jump = CMP(SLJIT_C_GREATER, TMP1, 0, SLJIT_IMM, 255);  
4316  #endif  #endif
4317        if (!check_class_ranges(common, (const pcre_uint8 *)cc, TRUE, FALSE, backtracks))
4318          {
4319          jump = CMP(SLJIT_C_GREATER, TMP1, 0, SLJIT_IMM, 255);
4320    
4321          OP2(SLJIT_AND, TMP2, 0, TMP1, 0, SLJIT_IMM, 0x7);
4322          OP2(SLJIT_LSHR, TMP1, 0, TMP1, 0, SLJIT_IMM, 3);
4323          OP1(SLJIT_MOV_UB, TMP1, 0, SLJIT_MEM1(TMP1), (sljit_sw)cc);
4324          OP2(SLJIT_SHL, TMP2, 0, SLJIT_IMM, 1, TMP2, 0);
4325          OP2(SLJIT_AND | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, TMP2, 0);
4326          add_jump(compiler, &found, JUMP(SLJIT_C_NOT_ZERO));
4327          add_jump(compiler, backtracks, JUMP(SLJIT_JUMP));
4328    
4329          JUMPHERE(jump);
4330          }
4331        else
4332          add_jump(compiler, &found, CMP(SLJIT_C_LESS_EQUAL, TMP3, 0, SLJIT_IMM, 0xff));
4333    
4334    if (!check_class_ranges(common, (const pcre_uint8 *)cc, TRUE, list))      OP1(SLJIT_MOV, TMP1, 0, TMP3, 0);
4335        cc += 32 / sizeof(pcre_uchar);
4336        }
4337      else
4338        add_jump(compiler, (cc[-1] & XCL_NOT) == 0 ? backtracks : &found, CMP(SLJIT_C_LESS_EQUAL, TMP1, 0, SLJIT_IMM, 0xff));
4339      }
4340    else if ((cc[-1] & XCL_MAP) != 0)
4341      {
4342      OP1(SLJIT_MOV, TMP3, 0, TMP1, 0);
4343    #ifdef SUPPORT_UCP
4344      charsaved = TRUE;
4345    #endif
4346      if (!check_class_ranges(common, (const pcre_uint8 *)cc, FALSE, TRUE, list))
4347      {      {
4348    #ifdef COMPILE_PCRE8
4349        SLJIT_ASSERT(common->utf);
4350    #endif
4351        jump = CMP(SLJIT_C_GREATER, TMP1, 0, SLJIT_IMM, 255);
4352    
4353      OP2(SLJIT_AND, TMP2, 0, TMP1, 0, SLJIT_IMM, 0x7);      OP2(SLJIT_AND, TMP2, 0, TMP1, 0, SLJIT_IMM, 0x7);
4354      OP2(SLJIT_LSHR, TMP1, 0, TMP1, 0, SLJIT_IMM, 3);      OP2(SLJIT_LSHR, TMP1, 0, TMP1, 0, SLJIT_IMM, 3);
4355      OP1(SLJIT_MOV_UB, TMP1, 0, SLJIT_MEM1(TMP1), (sljit_sw)cc);      OP1(SLJIT_MOV_UB, TMP1, 0, SLJIT_MEM1(TMP1), (sljit_sw)cc);
4356      OP2(SLJIT_SHL, TMP2, 0, SLJIT_IMM, 1, TMP2, 0);      OP2(SLJIT_SHL, TMP2, 0, SLJIT_IMM, 1, TMP2, 0);
4357      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);
4358      add_jump(compiler, list, JUMP(SLJIT_C_NOT_ZERO));      add_jump(compiler, list, JUMP(SLJIT_C_NOT_ZERO));
     }  
4359    
 #ifndef COMPILE_PCRE8  
   JUMPHERE(jump);  
 #elif defined SUPPORT_UTF  
   if (common->utf)  
4360      JUMPHERE(jump);      JUMPHERE(jump);
4361  #endif      }
4362    
4363    OP1(SLJIT_MOV, TMP1, 0, TMP3, 0);    OP1(SLJIT_MOV, TMP1, 0, TMP3, 0);
 #ifdef SUPPORT_UCP  
   charsaved = TRUE;  
 #endif  
4364    cc += 32 / sizeof(pcre_uchar);    cc += 32 / sizeof(pcre_uchar);
4365    }    }
4366    
# Line 4030  while (*cc != XCL_END) Line 4418  while (*cc != XCL_END)
4418        case PT_SPACE:        case PT_SPACE:
4419        case PT_PXSPACE:        case PT_PXSPACE:
4420        case PT_WORD:        case PT_WORD:
4421          case PT_PXGRAPH:
4422          case PT_PXPRINT:
4423          case PT_PXPUNCT:
4424        needstype = TRUE;        needstype = TRUE;
4425        needschar = TRUE;        needschar = TRUE;
4426        break;        break;
# Line 4217  while (*cc != XCL_END) Line 4608  while (*cc != XCL_END)
4608    
4609        case PT_SPACE:        case PT_SPACE:
4610        case PT_PXSPACE:        case PT_PXSPACE:
       if (*cc == PT_SPACE)  
         {  
         OP1(SLJIT_MOV, TMP2, 0, SLJIT_IMM, 0);  
         jump = CMP(SLJIT_C_EQUAL, TMP1, 0, SLJIT_IMM, 11 - charoffset);  
         }  
4611        SET_CHAR_OFFSET(9);        SET_CHAR_OFFSET(9);
4612        OP2(SLJIT_SUB | SLJIT_SET_U, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 13 - 9);        OP2(SLJIT_SUB | SLJIT_SET_U, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0xd - 0x9);
4613        OP_FLAGS(SLJIT_MOV, TMP2, 0, SLJIT_UNUSED, 0, SLJIT_C_LESS_EQUAL);        OP_FLAGS(SLJIT_MOV, TMP2, 0, SLJIT_UNUSED, 0, SLJIT_C_LESS_EQUAL);
4614        if (*cc == PT_SPACE)  
4615          JUMPHERE(jump);        OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x85 - 0x9);
4616          OP_FLAGS(SLJIT_OR | SLJIT_SET_E, TMP2, 0, TMP2, 0, SLJIT_C_EQUAL);
4617    
4618          OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x180e - 0x9);
4619          OP_FLAGS(SLJIT_OR | SLJIT_SET_E, TMP2, 0, TMP2, 0, SLJIT_C_EQUAL);
4620    
4621        SET_TYPE_OFFSET(ucp_Zl);        SET_TYPE_OFFSET(ucp_Zl);
4622        OP2(SLJIT_SUB | SLJIT_SET_U, SLJIT_UNUSED, 0, typereg, 0, SLJIT_IMM, ucp_Zs - ucp_Zl);        OP2(SLJIT_SUB | SLJIT_SET_U, SLJIT_UNUSED, 0, typereg, 0, SLJIT_IMM, ucp_Zs - ucp_Zl);
# Line 4318  while (*cc != XCL_END) Line 4708  while (*cc != XCL_END)
4708        OP_FLAGS(SLJIT_OR | SLJIT_SET_E, TMP2, 0, TMP2, 0, SLJIT_C_GREATER_EQUAL);        OP_FLAGS(SLJIT_OR | SLJIT_SET_E, TMP2, 0, TMP2, 0, SLJIT_C_GREATER_EQUAL);
4709        jump = JUMP(SLJIT_C_NOT_ZERO ^ invertcmp);        jump = JUMP(SLJIT_C_NOT_ZERO ^ invertcmp);
4710        break;        break;
4711    
4712          case PT_PXGRAPH:
4713          /* C and Z groups are the farthest two groups. */
4714          SET_TYPE_OFFSET(ucp_Ll);
4715          OP2(SLJIT_SUB | SLJIT_SET_U, SLJIT_UNUSED, 0, typereg, 0, SLJIT_IMM, ucp_So - ucp_Ll);
4716          OP_FLAGS(SLJIT_MOV, TMP2, 0, SLJIT_UNUSED, 0, SLJIT_C_GREATER);
4717    
4718          jump = CMP(SLJIT_C_NOT_EQUAL, typereg, 0, SLJIT_IMM, ucp_Cf - ucp_Ll);
4719    
4720          /* In case of ucp_Cf, we overwrite the result. */
4721          SET_CHAR_OFFSET(0x2066);
4722          OP2(SLJIT_SUB | SLJIT_SET_U, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x2069 - 0x2066);
4723          OP_FLAGS(SLJIT_MOV, TMP2, 0, SLJIT_UNUSED, 0, SLJIT_C_LESS_EQUAL);
4724    
4725          OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x061c - 0x2066);
4726          OP_FLAGS(SLJIT_OR, TMP2, 0, TMP2, 0, SLJIT_C_EQUAL);
4727    
4728          OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x180e - 0x2066);
4729          OP_FLAGS(SLJIT_OR, TMP2, 0, TMP2, 0, SLJIT_C_EQUAL);
4730    
4731          JUMPHERE(jump);
4732          jump = CMP(SLJIT_C_ZERO ^ invertcmp, TMP2, 0, SLJIT_IMM, 0);
4733          break;
4734    
4735          case PT_PXPRINT:
4736          /* C and Z groups are the farthest two groups. */
4737          SET_TYPE_OFFSET(ucp_Ll);
4738          OP2(SLJIT_SUB | SLJIT_SET_U, SLJIT_UNUSED, 0, typereg, 0, SLJIT_IMM, ucp_So - ucp_Ll);
4739          OP_FLAGS(SLJIT_MOV, TMP2, 0, SLJIT_UNUSED, 0, SLJIT_C_GREATER);
4740    
4741          OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, typereg, 0, SLJIT_IMM, ucp_Zs - ucp_Ll);
4742          OP_FLAGS(SLJIT_AND, TMP2, 0, TMP2, 0, SLJIT_C_NOT_EQUAL);
4743    
4744          jump = CMP(SLJIT_C_NOT_EQUAL, typereg, 0, SLJIT_IMM, ucp_Cf - ucp_Ll);
4745    
4746          /* In case of ucp_Cf, we overwrite the result. */
4747          SET_CHAR_OFFSET(0x2066);
4748          OP2(SLJIT_SUB | SLJIT_SET_U, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x2069 - 0x2066);
4749          OP_FLAGS(SLJIT_MOV, TMP2, 0, SLJIT_UNUSED, 0, SLJIT_C_LESS_EQUAL);
4750    
4751          OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x061c - 0x2066);
4752          OP_FLAGS(SLJIT_OR, TMP2, 0, TMP2, 0, SLJIT_C_EQUAL);
4753    
4754          JUMPHERE(jump);
4755          jump = CMP(SLJIT_C_ZERO ^ invertcmp, TMP2, 0, SLJIT_IMM, 0);
4756          break;
4757    
4758          case PT_PXPUNCT:
4759          SET_TYPE_OFFSET(ucp_Sc);
4760          OP2(SLJIT_SUB | SLJIT_SET_U, SLJIT_UNUSED, 0, typereg, 0, SLJIT_IMM, ucp_So - ucp_Sc);
4761          OP_FLAGS(SLJIT_MOV, TMP2, 0, SLJIT_UNUSED, 0, SLJIT_C_LESS_EQUAL);
4762    
4763          SET_CHAR_OFFSET(0);
4764          OP2(SLJIT_SUB | SLJIT_SET_U, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0xff);
4765          OP_FLAGS(SLJIT_AND, TMP2, 0, TMP2, 0, SLJIT_C_LESS_EQUAL);
4766    
4767          SET_TYPE_OFFSET(ucp_Pc);
4768          OP2(SLJIT_SUB | SLJIT_SET_U, SLJIT_UNUSED, 0, typereg, 0, SLJIT_IMM, ucp_Ps - ucp_Pc);
4769          OP_FLAGS(SLJIT_OR | SLJIT_SET_E, TMP2, 0, TMP2, 0, SLJIT_C_LESS_EQUAL);
4770          jump = JUMP(SLJIT_C_NOT_ZERO ^ invertcmp);
4771          break;
4772        }        }
4773      cc += 2;      cc += 2;
4774      }      }
# Line 4464  switch(type) Line 4915  switch(type)
4915  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
4916    case OP_NOTPROP:    case OP_NOTPROP:
4917    case OP_PROP:    case OP_PROP:
4918    propdata[0] = 0;    propdata[0] = XCL_HASPROP;
4919    propdata[1] = type == OP_NOTPROP ? XCL_NOTPROP : XCL_PROP;    propdata[1] = type == OP_NOTPROP ? XCL_NOTPROP : XCL_PROP;
4920    propdata[2] = cc[0];    propdata[2] = cc[0];
4921    propdata[3] = cc[1];    propdata[3] = cc[1];
# Line 4823  switch(type) Line 5274  switch(type)
5274    case OP_NCLASS:    case OP_NCLASS:
5275    detect_partial_match(common, backtracks);    detect_partial_match(common, backtracks);
5276    read_char(common);    read_char(common);
5277    if (check_class_ranges(common, (const pcre_uint8 *)cc, type == OP_NCLASS, backtracks))    if (check_class_ranges(common, (const pcre_uint8 *)cc, type == OP_NCLASS, FALSE, backtracks))
5278      return cc + 32 / sizeof(pcre_uchar);      return cc + 32 / sizeof(pcre_uchar);
5279    
5280  #if defined SUPPORT_UTF || !defined COMPILE_PCRE8  #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
# Line 4956  if (context.length > 0) Line 5407  if (context.length > 0)
5407  return compile_char1_matchingpath(common, *cc, cc + 1, backtracks);  return compile_char1_matchingpath(common, *cc, cc + 1, backtracks);
5408  }  }
5409    
 static struct sljit_jump *compile_ref_checks(compiler_common *common, pcre_uchar *cc, jump_list **backtracks)  
 {  
 DEFINE_COMPILER;  
 int offset = GET2(cc, 1) << 1;  
   
 OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(offset));  
 if (!common->jscript_compat)  
   {  
   if (backtracks == NULL)  
     {  
     /* OVECTOR(1) contains the "string begin - 1" constant. */  
     OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(1));  
     OP_FLAGS(SLJIT_MOV, TMP2, 0, SLJIT_UNUSED, 0, SLJIT_C_EQUAL);  
     OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(offset + 1));  
     OP_FLAGS(SLJIT_OR | SLJIT_SET_E, TMP2, 0, TMP2, 0, SLJIT_C_EQUAL);  
     return JUMP(SLJIT_C_NOT_ZERO);  
     }  
   add_jump(compiler, backtracks, CMP(SLJIT_C_EQUAL, TMP1, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(1)));  
   }  
 return CMP(SLJIT_C_EQUAL, TMP1, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(offset + 1));  
 }  
   
5410  /* Forward definitions. */  /* Forward definitions. */
5411  static void compile_matchingpath(compiler_common *, pcre_uchar *, pcre_uchar *, backtrack_common *);  static void compile_matchingpath(compiler_common *, pcre_uchar *, pcre_uchar *, backtrack_common *);
5412  static void compile_backtrackingpath(compiler_common *, struct backtrack_common *);  static void compile_backtrackingpath(compiler_common *, struct backtrack_common *);
# Line 5010  static void compile_backtrackingpath(com Line 5439  static void compile_backtrackingpath(com
5439    
5440  #define BACKTRACK_AS(type) ((type *)backtrack)  #define BACKTRACK_AS(type) ((type *)backtrack)
5441    
5442  static pcre_uchar *compile_ref_matchingpath(compiler_common *common, pcre_uchar *cc, jump_list **backtracks, BOOL withchecks, BOOL emptyfail)  static void compile_dnref_search(compiler_common *common, pcre_uchar *cc, jump_list **backtracks)
5443    {
5444    /* The OVECTOR offset goes to TMP2. */
5445    DEFINE_COMPILER;
5446    int count = GET2(cc, 1 + IMM2_SIZE);
5447    pcre_uchar *slot = common->name_table + GET2(cc, 1) * common->name_entry_size;
5448    unsigned int offset;
5449    jump_list *found = NULL;
5450    
5451    SLJIT_ASSERT(*cc == OP_DNREF || *cc == OP_DNREFI);
5452    
5453    OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(1));
5454    
5455    count--;
5456    while (count-- > 0)
5457      {
5458      offset = GET2(slot, 0) << 1;
5459      GET_LOCAL_BASE(TMP2, 0, OVECTOR(offset));
5460      add_jump(compiler, &found, CMP(SLJIT_C_NOT_EQUAL, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(offset), TMP1, 0));
5461      slot += common->name_entry_size;
5462      }
5463    
5464    offset = GET2(slot, 0) << 1;
5465    GET_LOCAL_BASE(TMP2, 0, OVECTOR(offset));
5466    if (backtracks != NULL && !common->jscript_compat)
5467      add_jump(compiler, backtracks, CMP(SLJIT_C_EQUAL, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(offset), TMP1, 0));
5468    
5469    set_jumps(found, LABEL());
5470    }
5471    
5472    static void compile_ref_matchingpath(compiler_common *common, pcre_uchar *cc, jump_list **backtracks, BOOL withchecks, BOOL emptyfail)
5473  {  {
5474  DEFINE_COMPILER;  DEFINE_COMPILER;
5475  int offset = GET2(cc, 1) << 1;  BOOL ref = (*cc == OP_REF || *cc == OP_REFI);
5476    int offset = 0;
5477  struct sljit_jump *jump = NULL;  struct sljit_jump *jump = NULL;
5478  struct sljit_jump *partial;  struct sljit_jump *partial;
5479  struct sljit_jump *nopartial;  struct sljit_jump *nopartial;
5480    
5481  OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(offset));  if (ref)
5482  /* OVECTOR(1) contains the "string begin - 1" constant. */    {
5483  if (withchecks && !common->jscript_compat)    offset = GET2(cc, 1) << 1;
5484    add_jump(compiler, backtracks, CMP(SLJIT_C_EQUAL, TMP1, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(1)));    OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(offset));
5485      /* OVECTOR(1) contains the "string begin - 1" constant. */
5486      if (withchecks && !common->jscript_compat)
5487        add_jump(compiler, backtracks, CMP(SLJIT_C_EQUAL, TMP1, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(1)));
5488      }
5489    else
5490      OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(TMP2), 0);
5491    
5492  #if defined SUPPORT_UTF && defined SUPPORT_UCP  #if defined SUPPORT_UTF && defined SUPPORT_UCP
5493  if (common->utf && *cc == OP_REFI)  if (common->utf && *cc == OP_REFI)
5494    {    {
5495    SLJIT_ASSERT(TMP1 == SLJIT_SCRATCH_REG1 && STACK_TOP == SLJIT_SCRATCH_REG2 && TMP2 == SLJIT_SCRATCH_REG3);    SLJIT_ASSERT(TMP1 == SLJIT_SCRATCH_REG1 && STACK_TOP == SLJIT_SCRATCH_REG2 && TMP2 == SLJIT_SCRATCH_REG3);
5496    OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(offset + 1));    if (ref)
5497        OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(offset + 1));
5498      else
5499        OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(TMP2), sizeof(sljit_sw));
5500    
5501    if (withchecks)    if (withchecks)
5502      jump = CMP(SLJIT_C_EQUAL, TMP1, 0, TMP2, 0);      jump = CMP(SLJIT_C_EQUAL, TMP1, 0, TMP2, 0);
5503    
# Line 5052  if (common->utf && *cc == OP_REFI) Line 5522  if (common->utf && *cc == OP_REFI)
5522  else  else
5523  #endif /* SUPPORT_UTF && SUPPORT_UCP */  #endif /* SUPPORT_UTF && SUPPORT_UCP */
5524    {    {
5525    OP2(SLJIT_SUB | SLJIT_SET_E, TMP2, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(offset + 1), TMP1, 0);    if (ref)
5526        OP2(SLJIT_SUB | SLJIT_SET_E, TMP2, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(offset + 1), TMP1, 0);
5527      else
5528        OP2(SLJIT_SUB | SLJIT_SET_E, TMP2, 0, SLJIT_MEM1(TMP2), sizeof(sljit_sw), TMP1, 0);
5529    
5530    if (withchecks)    if (withchecks)
5531      jump = JUMP(SLJIT_C_ZERO);      jump = JUMP(SLJIT_C_ZERO);
5532    
# Line 5089  if (jump != NULL) Line 5563  if (jump != NULL)
5563    else    else
5564      JUMPHERE(jump);      JUMPHERE(jump);
5565    }    }
 return cc + 1 + IMM2_SIZE;  
5566  }  }
5567    
5568  static SLJIT_INLINE pcre_uchar *compile_ref_iterator_matchingpath(compiler_common *common, pcre_uchar *cc, backtrack_common *parent)  static SLJIT_INLINE pcre_uchar *compile_ref_iterator_matchingpath(compiler_common *common, pcre_uchar *cc, backtrack_common *parent)
5569  {  {
5570  DEFINE_COMPILER;  DEFINE_COMPILER;
5571    BOOL ref = (*cc == OP_REF || *cc == OP_REFI);
5572  backtrack_common *backtrack;  backtrack_common *backtrack;
5573  pcre_uchar type;  pcre_uchar type;
5574    int offset = 0;
5575  struct sljit_label *label;  struct sljit_label *label;
5576  struct sljit_jump *zerolength;  struct sljit_jump *zerolength;
5577  struct sljit_jump *jump = NULL;  struct sljit_jump *jump = NULL;
# Line 5106  BOOL minimize; Line 5581  BOOL minimize;
5581    
5582  PUSH_BACKTRACK(sizeof(iterator_backtrack), cc, NULL);  PUSH_BACKTRACK(sizeof(iterator_backtrack), cc, NULL);
5583    
5584    if (ref)
5585      offset = GET2(cc, 1) << 1;
5586    else
5587      cc += IMM2_SIZE;
5588  type = cc[1 + IMM2_SIZE];  type = cc[1 + IMM2_SIZE];
5589    
5590    SLJIT_COMPILE_ASSERT((OP_CRSTAR & 0x1) == 0, crstar_opcode_must_be_even);
5591  minimize = (type & 0x1) != 0;  minimize = (type & 0x1) != 0;
5592  switch(type)  switch(type)
5593    {    {
# Line 5144  if (!minimize) Line 5625  if (!minimize)
5625    if (min == 0)    if (min == 0)
5626      {      {
5627      allocate_stack(common, 2);      allocate_stack(common, 2);
5628        if (ref)
5629          OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(offset));
5630      OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), STR_PTR, 0);      OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), STR_PTR, 0);
5631      OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(1), SLJIT_IMM, 0);      OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(1), SLJIT_IMM, 0);
5632      /* Temporary release of STR_PTR. */      /* Temporary release of STR_PTR. */
5633      OP2(SLJIT_SUB, STACK_TOP, 0, STACK_TOP, 0, SLJIT_IMM, sizeof(sljit_sw));      OP2(SLJIT_SUB, STACK_TOP, 0, STACK_TOP, 0, SLJIT_IMM, sizeof(sljit_sw));
5634      zerolength = compile_ref_checks(common, ccbegin, NULL);      /* Handles both invalid and empty cases. Since the minimum repeat,
5635        is zero the invalid case is basically the same as an empty case. */
5636        if (ref)
5637          zerolength = CMP(SLJIT_C_EQUAL, TMP1, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(offset + 1));
5638        else
5639          {
5640          compile_dnref_search(common, ccbegin, NULL);
5641          OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(TMP2), 0);
5642          OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), POSSESSIVE1, TMP2, 0);
5643          zerolength = CMP(SLJIT_C_EQUAL, TMP1, 0, SLJIT_MEM1(TMP2), sizeof(sljit_sw));
5644          }
5645      /* Restore if not zero length. */      /* Restore if not zero length. */
5646      OP2(SLJIT_ADD, STACK_TOP, 0, STACK_TOP, 0, SLJIT_IMM, sizeof(sljit_sw));      OP2(SLJIT_ADD, STACK_TOP, 0, STACK_TOP, 0, SLJIT_IMM, sizeof(sljit_sw));
5647      }      }
5648    else    else
5649      {      {
5650      allocate_stack(common, 1);      allocate_stack(common, 1);
5651        if (ref)
5652          OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(offset));
5653      OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), SLJIT_IMM, 0);      OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), SLJIT_IMM, 0);
5654      zerolength = compile_ref_checks(common, ccbegin, &backtrack->topbacktracks);      if (ref)
5655          {
5656          add_jump(compiler, &backtrack->topbacktracks, CMP(SLJIT_C_EQUAL, TMP1, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(1)));
5657          zerolength = CMP(SLJIT_C_EQUAL, TMP1, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(offset + 1));
5658          }
5659        else
5660          {
5661          compile_dnref_search(common, ccbegin, &backtrack->topbacktracks);
5662          OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(TMP2), 0);
5663          OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), POSSESSIVE1, TMP2, 0);
5664          zerolength = CMP(SLJIT_C_EQUAL, TMP1, 0, SLJIT_MEM1(TMP2), sizeof(sljit_sw));
5665          }
5666      }      }
5667    
5668    if (min > 1 || max > 1)    if (min > 1 || max > 1)
5669      OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), POSSESSIVE0, SLJIT_IMM, 0);      OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), POSSESSIVE0, SLJIT_IMM, 0);
5670    
5671    label = LABEL();    label = LABEL();
5672      if (!ref)
5673        OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), POSSESSIVE1);
5674    compile_ref_matchingpath(common, ccbegin, &backtrack->topbacktracks, FALSE, FALSE);    compile_ref_matchingpath(common, ccbegin, &backtrack->topbacktracks, FALSE, FALSE);
5675    
5676    if (min > 1 || max > 1)    if (min > 1 || max > 1)
# Line 5193  if (!minimize) Line 5701  if (!minimize)
5701    JUMPHERE(zerolength);    JUMPHERE(zerolength);
5702    BACKTRACK_AS(iterator_backtrack)->matchingpath = LABEL();    BACKTRACK_AS(iterator_backtrack)->matchingpath = LABEL();
5703    
5704    decrease_call_count(common);    count_match(common);
5705    return cc;    return cc;
5706    }    }
5707    
5708  allocate_stack(common, 2);  allocate_stack(common, ref ? 2 : 3);
5709    if (ref)
5710      OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(offset));
5711  OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), SLJIT_IMM, 0);  OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), SLJIT_IMM, 0);
5712  if (type != OP_CRMINSTAR)  if (type != OP_CRMINSTAR)
5713    OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(1), SLJIT_IMM, 0);    OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(1), SLJIT_IMM, 0);
5714    
5715  if (min == 0)  if (min == 0)
5716    {    {
5717    zerolength = compile_ref_checks(common, ccbegin, NULL);    /* Handles both invalid and empty cases. Since the minimum repeat,
5718      is zero the invalid case is basically the same as an empty case. */
5719      if (ref)
5720        zerolength = CMP(SLJIT_C_EQUAL, TMP1, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(offset + 1));
5721      else
5722        {
5723        compile_dnref_search(common, ccbegin, NULL);
5724        OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(TMP2), 0);
5725        OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(2), TMP2, 0);
5726        zerolength = CMP(SLJIT_C_EQUAL, TMP1, 0, SLJIT_MEM1(TMP2), sizeof(sljit_sw));
5727        }
5728      /* Length is non-zero, we can match real repeats. */
5729    OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), STR_PTR, 0);    OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), STR_PTR, 0);
5730    jump = JUMP(SLJIT_JUMP);    jump = JUMP(SLJIT_JUMP);
5731    }    }
5732  else  else
5733    zerolength = compile_ref_checks(common, ccbegin, &backtrack->topbacktracks);    {
5734      if (ref)
5735        {
5736        add_jump(compiler, &backtrack->topbacktracks, CMP(SLJIT_C_EQUAL, TMP1, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(1)));
5737        zerolength = CMP(SLJIT_C_EQUAL, TMP1, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(offset + 1));
5738        }
5739      else
5740        {
5741        compile_dnref_search(common, ccbegin, &backtrack->topbacktracks);
5742        OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(TMP2), 0);
5743        OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(2), TMP2, 0);
5744        zerolength = CMP(SLJIT_C_EQUAL, TMP1, 0, SLJIT_MEM1(TMP2), sizeof(sljit_sw));
5745        }
5746      }
5747    
5748  BACKTRACK_AS(iterator_backtrack)->matchingpath = LABEL();  BACKTRACK_AS(iterator_backtrack)->matchingpath = LABEL();
5749  if (max > 0)  if (max > 0)
5750    add_jump(compiler, &backtrack->topbacktracks, CMP(SLJIT_C_GREATER_EQUAL, SLJIT_MEM1(STACK_TOP), STACK(1), SLJIT_IMM, max));    add_jump(compiler, &backtrack->topbacktracks, CMP(SLJIT_C_GREATER_EQUAL, SLJIT_MEM1(STACK_TOP), STACK(1), SLJIT_IMM, max));
5751    
5752    if (!ref)
5753      OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(STACK_TOP), STACK(2));
5754  compile_ref_matchingpath(common, ccbegin, &backtrack->topbacktracks, TRUE, TRUE);  compile_ref_matchingpath(common, ccbegin, &backtrack->topbacktracks, TRUE, TRUE);
5755  OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), STR_PTR, 0);  OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), STR_PTR, 0);
5756    
# Line 5232  if (jump != NULL) Line 5768  if (jump != NULL)
5768    JUMPHERE(jump);    JUMPHERE(jump);
5769  JUMPHERE(zerolength);  JUMPHERE(zerolength);
5770    
5771  decrease_call_count(common);  count_match(common);
5772  return cc;  return cc;
5773  }  }
5774    
# Line 5242  DEFINE_COMPILER; Line 5778  DEFINE_COMPILER;
5778  backtrack_common *backtrack;  backtrack_common *backtrack;
5779  recurse_entry *entry = common->entries;  recurse_entry *entry = common->entries;
5780  recurse_entry *prev = NULL;  recurse_entry *prev = NULL;
5781  int start = GET(cc, 1);  sljit_sw start = GET(cc, 1);
5782  pcre_uchar *start_cc;  pcre_uchar *start_cc;
5783    BOOL needs_control_head;
5784    
5785  PUSH_BACKTRACK(sizeof(recurse_backtrack), cc, NULL);  PUSH_BACKTRACK(sizeof(recurse_backtrack), cc, NULL);
5786    
5787  /* Inlining simple patterns. */  /* Inlining simple patterns. */
5788  if (get_framesize(common, common->start + start, TRUE) == no_stack)  if (get_framesize(common, common->start + start, NULL, TRUE, &needs_control_head) == no_stack)
5789    {    {
5790    start_cc = common->start + start;    start_cc = common->start + start;
5791    compile_matchingpath(common, next_opcode(common, start_cc), bracketend(start_cc) - (1 + LINK_SIZE), backtrack);    compile_matchingpath(common, next_opcode(common, start_cc), bracketend(start_cc) - (1 + LINK_SIZE), backtrack);
# Line 5408  static pcre_uchar *compile_assert_matchi Line 5945  static pcre_uchar *compile_assert_matchi
5945  DEFINE_COMPILER;  DEFINE_COMPILER;
5946  int framesize;  int framesize;
5947  int extrasize;  int extrasize;
5948  BOOL needs_control_head = common->control_head_ptr != 0;  BOOL needs_control_head;
5949  int private_data_ptr;  int private_data_ptr;
5950  backtrack_common altbacktrack;  backtrack_common altbacktrack;
5951  pcre_uchar *ccbegin;  pcre_uchar *ccbegin;
# Line 5418  jump_list *tmp = NULL; Line 5955  jump_list *tmp = NULL;
5955  jump_list **target = (conditional) ? &backtrack->condfailed : &backtrack->common.topbacktracks;  jump_list **target = (conditional) ? &backtrack->condfailed : &backtrack->common.topbacktracks;
5956  jump_list **found;  jump_list **found;
5957  /* Saving previous accept variables. */  /* Saving previous accept variables. */
5958    BOOL save_local_exit = common->local_exit;
5959    BOOL save_positive_assert = common->positive_assert;
5960    then_trap_backtrack *save_then_trap = common->then_trap;
5961  struct sljit_label *save_quit_label = common->quit_label;  struct sljit_label *save_quit_label = common->quit_label;
5962  struct sljit_label *save_accept_label = common->accept_label;  struct sljit_label *save_accept_label = common->accept_label;
5963  jump_list *save_quit = common->quit;  jump_list *save_quit = common->quit;
5964    jump_list *save_positive_assert_quit = common->positive_assert_quit;
5965  jump_list *save_accept = common->accept;  jump_list *save_accept = common->accept;
 BOOL save_local_exit = common->local_exit;  
5966  struct sljit_jump *jump;  struct sljit_jump *jump;
5967  struct sljit_jump *brajump = NULL;  struct sljit_jump *brajump = NULL;
5968    
5969    /* Assert captures then. */
5970    common->then_trap = NULL;
5971    
5972  if (*cc == OP_BRAZERO || *cc == OP_BRAMINZERO)  if (*cc == OP_BRAZERO || *cc == OP_BRAMINZERO)
5973    {    {
5974    SLJIT_ASSERT(!conditional);    SLJIT_ASSERT(!conditional);
# Line 5434  if (*cc == OP_BRAZERO || *cc == OP_BRAMI Line 5977  if (*cc == OP_BRAZERO || *cc == OP_BRAMI
5977    }    }
5978  private_data_ptr = PRIVATE_DATA(cc);  private_data_ptr = PRIVATE_DATA(cc);
5979  SLJIT_ASSERT(private_data_ptr != 0);  SLJIT_ASSERT(private_data_ptr != 0);
5980  framesize = get_framesize(common, cc, FALSE);  framesize = get_framesize(common, cc, NULL, FALSE, &needs_control_head);
5981  backtrack->framesize = framesize;  backtrack->framesize = framesize;
5982  backtrack->private_data_ptr = private_data_ptr;  backtrack->private_data_ptr = private_data_ptr;
5983  opcode = *cc;  opcode = *cc;
# Line 5454  if (bra == OP_BRAMINZERO) Line 5997  if (bra == OP_BRAMINZERO)
5997  if (framesize < 0)  if (framesize < 0)
5998    {    {
5999    extrasize = needs_control_head ? 2 : 1;    extrasize = needs_control_head ? 2 : 1;
6000    if (framesize != no_stack)    if (framesize == no_frame)
6001      OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), private_data_ptr, STACK_TOP, 0);      OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), private_data_ptr, STACK_TOP, 0);
6002    allocate_stack(common, extrasize);    allocate_stack(common, extrasize);
6003    if (needs_control_head)    if (needs_control_head)
# Line 5484  else Line 6027  else
6027      }      }
6028    else    else
6029      OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(1), TMP1, 0);      OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(1), TMP1, 0);
6030    init_frame(common, ccbegin, framesize + extrasize - 1, extrasize, FALSE);    init_frame(common, ccbegin, NULL, framesize + extrasize - 1, extrasize, FALSE);
6031      }
6032    
6033    memset(&altbacktrack, 0, sizeof(backtrack_common));
6034    if (opcode == OP_ASSERT_NOT || opcode == OP_ASSERTBACK_NOT)
6035      {
6036      /* Negative assert is stronger than positive assert. */
6037      common->local_exit = TRUE;
6038      common->quit_label = NULL;
6039      common->quit = NULL;
6040      common->positive_assert = FALSE;
6041    }    }
6042    else
6043      common->positive_assert = TRUE;
6044    common->positive_assert_quit = NULL;
6045    
 memset(&altbacktrack, 0, sizeof(backtrack_common));  
 common->local_exit = TRUE;  
 common->quit_label = NULL;  
 common->quit = NULL;  
6046  while (1)  while (1)
6047    {    {
6048    common->accept_label = NULL;    common->accept_label = NULL;
# Line 5505  while (1) Line 6057  while (1)
6057    compile_matchingpath(common, ccbegin + 1 + LINK_SIZE, cc, &altbacktrack);    compile_matchingpath(common, ccbegin + 1 + LINK_SIZE, cc, &altbacktrack);
6058    if (SLJIT_UNLIKELY(sljit_get_compiler_error(compiler)))    if (SLJIT_UNLIKELY(sljit_get_compiler_error(compiler)))
6059      {      {
6060      common->local_exit = save_local_exit;      if (opcode == OP_ASSERT_NOT || opcode == OP_ASSERTBACK_NOT)
6061      common->quit_label = save_quit_label;        {
6062          common->local_exit = save_local_exit;
6063          common->quit_label = save_quit_label;
6064          common->quit = save_quit;
6065          }
6066        common->positive_assert = save_positive_assert;
6067        common->then_trap = save_then_trap;
6068      common->accept_label = save_accept_label;      common->accept_label = save_accept_label;
6069      common->quit = save_quit;      common->positive_assert_quit = save_positive_assert_quit;
6070      common->accept = save_accept;      common->accept = save_accept;
6071      return NULL;      return NULL;
6072      }      }
# Line 5519  while (1) Line 6077  while (1)
6077    /* Reset stack. */    /* Reset stack. */
6078    if (framesize < 0)    if (framesize < 0)
6079      {      {
6080      if (framesize != no_stack)      if (framesize == no_frame)
6081        OP1(SLJIT_MOV, STACK_TOP, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), private_data_ptr);        OP1(SLJIT_MOV, STACK_TOP, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), private_data_ptr);
6082      else      else
6083        free_stack(common, extrasize);        free_stack(common, extrasize);
# Line 5573  while (1) Line 6131  while (1)
6131    compile_backtrackingpath(common, altbacktrack.top);    compile_backtrackingpath(common, altbacktrack.top);
6132    if (SLJIT_UNLIKELY(sljit_get_compiler_error(compiler)))    if (SLJIT_UNLIKELY(sljit_get_compiler_error(compiler)))
6133      {      {
6134      common->local_exit = save_local_exit;      if (opcode == OP_ASSERT_NOT || opcode == OP_ASSERTBACK_NOT)
6135      common->quit_label = save_quit_label;        {
6136          common->local_exit = save_local_exit;
6137          common->quit_label = save_quit_label;
6138          common->quit = save_quit;
6139          }
6140        common->positive_assert = save_positive_assert;
6141        common->then_trap = save_then_trap;
6142      common->accept_label = save_accept_label;      common->accept_label = save_accept_label;
6143      common->quit = save_quit;      common->positive_assert_quit = save_positive_assert_quit;
6144      common->accept = save_accept;      common->accept = save_accept;
6145      return NULL;      return NULL;
6146      }      }
# Line 5589  while (1) Line 6153  while (1)
6153    cc += GET(cc, 1);    cc += GET(cc, 1);
6154    }    }
6155    
6156    if (opcode == OP_ASSERT_NOT || opcode == OP_ASSERTBACK_NOT)
6157      {
6158      SLJIT_ASSERT(common->positive_assert_quit == NULL);
6159      /* Makes the check less complicated below. */
6160      common->positive_assert_quit = common->quit;
6161      }
6162    
6163  /* None of them matched. */  /* None of them matched. */
6164  if (common->quit != NULL)  if (common->positive_assert_quit != NULL)
6165    {    {
6166    jump = JUMP(SLJIT_JUMP);    jump = JUMP(SLJIT_JUMP);
6167    set_jumps(common->quit, LABEL());    set_jumps(common->positive_assert_quit, LABEL());
6168    SLJIT_ASSERT(framesize != no_stack);    SLJIT_ASSERT(framesize != no_stack);
6169    if (framesize < 0)    if (framesize < 0)
6170      OP2(SLJIT_ADD, STACK_TOP, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), private_data_ptr, SLJIT_IMM, extrasize * sizeof(sljit_sw));      OP2(SLJIT_ADD, STACK_TOP, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), private_data_ptr, SLJIT_IMM, extrasize * sizeof(sljit_sw));
# Line 5753  else Line 6324  else
6324      }      }
6325    }    }
6326    
6327  common->local_exit = save_local_exit;  if (opcode == OP_ASSERT_NOT || opcode == OP_ASSERTBACK_NOT)
6328  common->quit_label = save_quit_label;    {
6329      common->local_exit = save_local_exit;
6330      common->quit_label = save_quit_label;
6331      common->quit = save_quit;
6332      }
6333    common->positive_assert = save_positive_assert;
6334    common->then_trap = save_then_trap;
6335  common->accept_label = save_accept_label;  common->accept_label = save_accept_label;
6336  common->quit = save_quit;  common->positive_assert_quit = save_positive_assert_quit;
6337  common->accept = save_accept;  common->accept = save_accept;
6338  return cc + 1 + LINK_SIZE;  return cc + 1 + LINK_SIZE;
6339  }  }
6340    
6341  static sljit_sw SLJIT_CALL do_searchovector(sljit_uw refno, sljit_sw* locals, pcre_uchar *name_table)  static SLJIT_INLINE void match_once_common(compiler_common *common, pcre_uchar ket, int framesize, int private_data_ptr, BOOL has_alternatives, BOOL needs_control_head)
6342  {  {
6343  int condition = FALSE;  DEFINE_COMPILER;
6344  pcre_uchar *slotA = name_table;  int stacksize;
 pcre_uchar *slotB;  
 sljit_sw name_count = locals[LOCALS0 / sizeof(sljit_sw)];  
 sljit_sw name_entry_size = locals[LOCALS1 / sizeof(sljit_sw)];  
 sljit_sw no_capture;  
 int i;  
   
 locals += refno & 0xff;  
 refno >>= 8;  
 no_capture = locals[1];  
6345    
6346  for (i = 0; i < name_count; i++)  if (framesize < 0)
6347    {    {
6348    if (GET2(slotA, 0) == refno) break;    if (framesize == no_frame)
6349    slotA += name_entry_size;      OP1(SLJIT_MOV, STACK_TOP, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), private_data_ptr);
6350    }    else
6351        {
6352        stacksize = needs_control_head ? 1 : 0;
6353        if (ket != OP_KET || has_alternatives)
6354          stacksize++;
6355        free_stack(common, stacksize);
6356        }
6357    
6358  if (i < name_count)    if (needs_control_head)
6359    {      OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(STACK_TOP), (ket != OP_KET || has_alternatives) ? sizeof(sljit_sw) : 0);
   /* Found a name for the number - there can be only one; duplicate names  
   for different numbers are allowed, but not vice versa. First scan down  
   for duplicates. */  
6360    
6361    slotB = slotA;    /* TMP2 which is set here used by OP_KETRMAX below. */
6362    while (slotB > name_table)    if (ket == OP_KETRMAX)
6363        OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(STACK_TOP), 0);
6364      else if (ket == OP_KETRMIN)
6365      {      {
6366      slotB -= name_entry_size;      /* Move the STR_PTR to the private_data_ptr. */
6367      if (STRCMP_UC_UC(slotA + IMM2_SIZE, slotB + IMM2_SIZE) == 0)      OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), private_data_ptr, SLJIT_MEM1(STACK_TOP), 0);
       {  
       condition = locals[GET2(slotB, 0) << 1] != no_capture;  
       if (condition) break;  
       }  
     else break;  
6368      }      }
6369      }
6370    else
6371      {
6372      stacksize = (ket != OP_KET || has_alternatives) ? 2 : 1;
6373      OP2(SLJIT_ADD, STACK_TOP, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), private_data_ptr, SLJIT_IMM, (framesize + stacksize) * sizeof(sljit_sw));
6374      if (needs_control_head)
6375        OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(STACK_TOP), 0);
6376    
6377    /* Scan up for duplicates */    if (ket == OP_KETRMAX)
   if (!condition)  
6378      {      {
6379      slotB = slotA;      /* TMP2 which is set here used by OP_KETRMAX below. */
6380      for (i++; i < name_count; i++)      OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(STACK_TOP), STACK(0));
       {  
       slotB += name_entry_size;  
       if (STRCMP_UC_UC(slotA + IMM2_SIZE, slotB + IMM2_SIZE) == 0)  
         {  
         condition = locals[GET2(slotB, 0) << 1] != no_capture;  
         if (condition) break;  
         }  
       else break;  
       }  
6381      }      }
6382    }    }
6383  return condition;  if (needs_control_head)
6384      OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), common->control_head_ptr, TMP1, 0);
6385  }  }
6386    
6387  static sljit_sw SLJIT_CALL do_searchgroups(sljit_uw recno, sljit_uw* locals, pcre_uchar *name_table)  static SLJIT_INLINE int match_capture_common(compiler_common *common, int stacksize, int offset, int private_data_ptr)
6388  {  {
6389  int condition = FALSE;  DEFINE_COMPILER;
 pcre_uchar *slotA = name_table;  
 pcre_uchar *slotB;  
 sljit_uw name_count = locals[LOCALS0 / sizeof(sljit_sw)];  
 sljit_uw name_entry_size = locals[LOCALS1 / sizeof(sljit_sw)];  
 sljit_uw group_num = locals[POSSESSIVE0 / sizeof(sljit_sw)];  
 sljit_uw i;  
6390    
6391  for (i = 0; i < name_count; i++)  if (common->capture_last_ptr != 0)
6392    {    {
6393    if (GET2(slotA, 0) == recno) break;    OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), common->capture_last_ptr);
6394    slotA += name_entry_size;    OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), common->capture_last_ptr, SLJIT_IMM, offset >> 1);
6395      OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(stacksize), TMP1, 0);
6396      stacksize++;
6397    }    }
6398    if (common->optimized_cbracket[offset >> 1] == 0)
 if (i < name_count)  
6399    {    {
6400    /* Found a name for the number - there can be only one; duplicate    OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(offset));
6401    names for different numbers are allowed, but not vice versa. First    OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(offset + 1));
6402    scan down for duplicates. */    OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(stacksize), TMP1, 0);
6403      OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), private_data_ptr);
6404    slotB = slotA;    OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(stacksize + 1), TMP2, 0);
6405    while (slotB > name_table)    OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(offset + 1), STR_PTR, 0);
6406      {    OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(offset), TMP1, 0);
6407      slotB -= name_entry_size;    stacksize += 2;
     if (STRCMP_UC_UC(slotA + IMM2_SIZE, slotB + IMM2_SIZE) == 0)  
       {  
       condition = GET2(slotB, 0) == group_num;  
       if (condition) break;  
       }  
     else break;  
     }  
   
   /* Scan up for duplicates */  
   if (!condition)  
     {  
     slotB = slotA;  
     for (i++; i < name_count; i++)  
       {  
       slotB += name_entry_size;  
       if (STRCMP_UC_UC(slotA + IMM2_SIZE, slotB + IMM2_SIZE) == 0)  
         {  
         condition = GET2(slotB, 0) == group_num;  
         if (condition) break;  
         }  
       else break;  
       }  
     }  
6408    }    }
6409  return condition;  return stacksize;
6410  }  }
6411    
6412  /*  /*
# Line 5932  backtrack_common *backtrack; Line 6470  backtrack_common *backtrack;
6470  pcre_uchar opcode;  pcre_uchar opcode;
6471  int private_data_ptr = 0;  int private_data_ptr = 0;
6472  int offset = 0;  int offset = 0;
6473  int stacksize;  int i, stacksize;
6474    int repeat_ptr = 0, repeat_length = 0;
6475    int repeat_type = 0, repeat_count = 0;
6476  pcre_uchar *ccbegin;  pcre_uchar *ccbegin;
6477  pcre_uchar *matchingpath;  pcre_uchar *matchingpath;
6478    pcre_uchar *slot;
6479  pcre_uchar bra = OP_BRA;  pcre_uchar bra = OP_BRA;
6480  pcre_uchar ket;  pcre_uchar ket;
6481  assert_backtrack *assert;  assert_backtrack *assert;
6482  BOOL has_alternatives;  BOOL has_alternatives;
6483    BOOL needs_control_head = FALSE;
6484  struct sljit_jump *jump;  struct sljit_jump *jump;
6485  struct sljit_jump *skip;  struct sljit_jump *skip;
6486  struct sljit_label *rmaxlabel = NULL;  struct sljit_label *rmax_label = NULL;
6487  struct sljit_jump *braminzerojump = NULL;  struct sljit_jump *braminzero = NULL;
6488    
6489  PUSH_BACKTRACK(sizeof(bracket_backtrack), cc, NULL);  PUSH_BACKTRACK(sizeof(bracket_backtrack), cc, NULL);
6490    
# Line 5955  if (*cc == OP_BRAZERO || *cc == OP_BRAMI Line 6497  if (*cc == OP_BRAZERO || *cc == OP_BRAMI
6497    
6498  opcode = *cc;  opcode = *cc;
6499  ccbegin = cc;  ccbegin = cc;
6500  matchingpath = ccbegin + 1 + LINK_SIZE;  matchingpath = bracketend(cc) - 1 - LINK_SIZE;
6501    ket = *matchingpath;
6502    if (ket == OP_KET && PRIVATE_DATA(matchingpath) != 0)
6503      {
6504      repeat_ptr = PRIVATE_DATA(matchingpath);
6505      repeat_length = PRIVATE_DATA(matchingpath + 1);
6506      repeat_type = PRIVATE_DATA(matchingpath + 2);
6507      repeat_count = PRIVATE_DATA(matchingpath + 3);
6508      SLJIT_ASSERT(repeat_length != 0 && repeat_type != 0 && repeat_count != 0);
6509      if (repeat_type == OP_UPTO)
6510        ket = OP_KETRMAX;
6511      if (repeat_type == OP_MINUPTO)
6512        ket = OP_KETRMIN;
6513      }
6514    
6515  if ((opcode == OP_COND || opcode == OP_SCOND) && cc[1 + LINK_SIZE] == OP_DEF)  if ((opcode == OP_COND || opcode == OP_SCOND) && cc[1 + LINK_SIZE] == OP_DEF)
6516    {    {
6517    /* Drop this bracket_backtrack. */    /* Drop this bracket_backtrack. */
6518    parent->top = backtrack->prev;    parent->top = backtrack->prev;
6519    return bracketend(cc);    return matchingpath + 1 + LINK_SIZE + repeat_length;
6520    }    }
6521    
6522  ket = *(bracketend(cc) - 1 - LINK_SIZE);  matchingpath = ccbegin + 1 + LINK_SIZE;
6523  SLJIT_ASSERT(ket == OP_KET || ket == OP_KETRMAX || ket == OP_KETRMIN);  SLJIT_ASSERT(ket == OP_KET || ket == OP_KETRMAX || ket == OP_KETRMIN);
6524  SLJIT_ASSERT(!((bra == OP_BRAZERO && ket == OP_KETRMIN) || (bra == OP_BRAMINZERO && ket == OP_KETRMAX)));  SLJIT_ASSERT(!((bra == OP_BRAZERO && ket == OP_KETRMIN) || (bra == OP_BRAMINZERO && ket == OP_KETRMAX)));
6525  cc += GET(cc, 1);  cc += GET(cc, 1);
6526    
6527  has_alternatives = *cc == OP_ALT;  has_alternatives = *cc == OP_ALT;
6528  if (SLJIT_UNLIKELY(opcode == OP_COND) || SLJIT_UNLIKELY(opcode == OP_SCOND))  if (SLJIT_UNLIKELY(opcode == OP_COND || opcode == OP_SCOND))
6529    {    has_alternatives = (*matchingpath == OP_RREF || *matchingpath == OP_DNRREF) ? FALSE : TRUE;
   has_alternatives = (*matchingpath == OP_RREF) ? FALSE : TRUE;  
   if (*matchingpath == OP_NRREF)  
     {  
     stacksize = GET2(matchingpath, 1);  
     if (common->currententry == NULL || stacksize == RREF_ANY)  
       has_alternatives = FALSE;  
     else if (common->currententry->start == 0)  
       has_alternatives = stacksize != 0;  
     else  
       has_alternatives = stacksize != (int)GET2(common->start, common->currententry->start + 1 + LINK_SIZE);  
     }  
   }  
6530    
6531  if (SLJIT_UNLIKELY(opcode == OP_COND) && (*cc == OP_KETRMAX || *cc == OP_KETRMIN))  if (SLJIT_UNLIKELY(opcode == OP_COND) && (*cc == OP_KETRMAX || *cc == OP_KETRMIN))
6532    opcode = OP_SCOND;    opcode = OP_SCOND;
# Line 6014  else if (opcode == OP_ONCE || opcode == Line 6557  else if (opcode == OP_ONCE || opcode ==
6557    SLJIT_ASSERT(private_data_ptr != 0);    SLJIT_ASSERT(private_data_ptr != 0);
6558    BACKTRACK_AS(bracket_backtrack)->private_data_ptr = private_data_ptr;    BACKTRACK_AS(bracket_backtrack)->private_data_ptr = private_data_ptr;
6559    if (opcode == OP_ONCE)    if (opcode == OP_ONCE)
6560      BACKTRACK_AS(bracket_backtrack)->u.framesize = get_framesize(common, ccbegin, FALSE);      BACKTRACK_AS(bracket_backtrack)->u.framesize = get_framesize(common, ccbegin, NULL, FALSE, &needs_control_head);
6561    }    }
6562    
6563  /* Instructions before the first alternative. */  /* Instructions before the first alternative. */
6564  stacksize = 0;  stacksize = 0;
6565  if ((ket == OP_KETRMAX) || (ket == OP_KETRMIN && bra != OP_BRAMINZERO))  if (ket == OP_KETRMAX || (ket == OP_KETRMIN && bra != OP_BRAMINZERO))
6566    stacksize++;    stacksize++;
6567  if (bra == OP_BRAZERO)  if (bra == OP_BRAZERO)
6568    stacksize++;    stacksize++;
# Line 6028  if (stacksize > 0) Line 6571  if (stacksize > 0)
6571    allocate_stack(common, stacksize);    allocate_stack(common, stacksize);
6572    
6573  stacksize = 0;  stacksize = 0;
6574  if ((ket == OP_KETRMAX) || (ket == OP_KETRMIN && bra != OP_BRAMINZERO))  if (ket == OP_KETRMAX || (ket == OP_KETRMIN && bra != OP_BRAMINZERO))
6575    {    {
6576    OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(stacksize), SLJIT_IMM, 0);    OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(stacksize), SLJIT_IMM, 0);
6577    stacksize++;    stacksize++;
# Line 6044  if (bra == OP_BRAMINZERO) Line 6587  if (bra == OP_BRAMINZERO)
6587    if (ket != OP_KETRMIN)    if (ket != OP_KETRMIN)
6588      {      {
6589      free_stack(common, 1);      free_stack(common, 1);
6590      braminzerojump = CMP(SLJIT_C_EQUAL, STR_PTR, 0, SLJIT_IMM, 0);      braminzero = CMP(SLJIT_C_EQUAL, STR_PTR, 0, SLJIT_IMM, 0);
6591      }      }
6592    else    else
6593      {      {
# Line 6059  if (bra == OP_BRAMINZERO) Line 6602  if (bra == OP_BRAMINZERO)
6602        if (opcode != OP_ONCE || BACKTRACK_AS(bracket_backtrack)->u.framesize < 0)        if (opcode != OP_ONCE || BACKTRACK_AS(bracket_backtrack)->u.framesize < 0)
6603          {          {
6604          /* When we come from outside, private_data_ptr contains the previous STR_PTR. */          /* When we come from outside, private_data_ptr contains the previous STR_PTR. */
6605          braminzerojump = CMP(SLJIT_C_EQUAL, STR_PTR, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), private_data_ptr);          braminzero = CMP(SLJIT_C_EQUAL, STR_PTR, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), private_data_ptr);
6606          }          }
6607        else        else
6608          {          {
6609          /* Except when the whole stack frame must be saved. */          /* Except when the whole stack frame must be saved. */
6610          OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), private_data_ptr);          OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), private_data_ptr);
6611          braminzerojump = CMP(SLJIT_C_EQUAL, STR_PTR, 0, SLJIT_MEM1(TMP1), (BACKTRACK_AS(bracket_backtrack)->u.framesize + 1) * sizeof(sljit_sw));          braminzero = CMP(SLJIT_C_EQUAL, STR_PTR, 0, SLJIT_MEM1(TMP1), (BACKTRACK_AS(bracket_backtrack)->u.framesize + 1) * sizeof(sljit_sw));
6612          }          }
6613        JUMPHERE(skip);        JUMPHERE(skip);
6614        }        }
# Line 6078  if (bra == OP_BRAMINZERO) Line 6621  if (bra == OP_BRAMINZERO)
6621      }      }
6622    }    }
6623    
6624    if (repeat_type != 0)
6625      {
6626      OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), repeat_ptr, SLJIT_IMM, repeat_count);
6627      if (repeat_type == OP_EXACT)
6628        rmax_label = LABEL();
6629      }
6630    
6631  if (ket == OP_KETRMIN)  if (ket == OP_KETRMIN)
6632    BACKTRACK_AS(bracket_backtrack)->recursive_matchingpath = LABEL();    BACKTRACK_AS(bracket_backtrack)->recursive_matchingpath = LABEL();
6633    
6634  if (ket == OP_KETRMAX)  if (ket == OP_KETRMAX)
6635    {    {
6636    rmaxlabel = LABEL();    rmax_label = LABEL();
6637    if (has_alternatives && opcode != OP_ONCE && opcode < OP_SBRA)    if (has_alternatives && opcode != OP_ONCE && opcode < OP_SBRA && repeat_type == 0)
6638      BACKTRACK_AS(bracket_backtrack)->alternative_matchingpath = rmaxlabel;      BACKTRACK_AS(bracket_backtrack)->alternative_matchingpath = rmax_label;
6639    }    }
6640    
6641  /* Handling capturing brackets and alternatives. */  /* Handling capturing brackets and alternatives. */
6642  if (opcode == OP_ONCE)  if (opcode == OP_ONCE)
6643    {    {
6644      stacksize = 0;
6645      if (needs_control_head)
6646        {
6647        OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), common->control_head_ptr);
6648        stacksize++;
6649        }
6650    
6651    if (BACKTRACK_AS(bracket_backtrack)->u.framesize < 0)    if (BACKTRACK_AS(bracket_backtrack)->u.framesize < 0)
6652      {      {
6653      /* Neither capturing brackets nor recursions are not found in the block. */      /* Neither capturing brackets nor recursions are found in the block. */
6654      if (ket == OP_KETRMIN)      if (ket == OP_KETRMIN)
6655        {        {
6656        OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), private_data_ptr);        stacksize += 2;
6657        allocate_stack(common, 2);        if (!needs_control_head)
6658        OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), STR_PTR, 0);          OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), private_data_ptr);
       OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(1), TMP2, 0);  
       OP2(SLJIT_SUB, SLJIT_MEM1(SLJIT_LOCALS_REG), private_data_ptr, STACK_TOP, 0, SLJIT_IMM, sizeof(sljit_sw));  
6659        }        }
6660      else if (ket == OP_KETRMAX || has_alternatives)      else
6661        {        {
6662        OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), private_data_ptr, STACK_TOP, 0);        if (BACKTRACK_AS(bracket_backtrack)->u.framesize == no_frame)
6663        allocate_stack(common, 1);          OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), private_data_ptr, STACK_TOP, 0);
6664        OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), STR_PTR, 0);        if (ket == OP_KETRMAX || has_alternatives)
6665            stacksize++;
6666        }        }
6667      else  
6668        OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), private_data_ptr, STACK_TOP, 0);      if (stacksize > 0)
6669          allocate_stack(common, stacksize);
6670    
6671        stacksize = 0;
6672        if (needs_control_head)
6673          {
6674          stacksize++;
6675          OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), TMP2, 0);
6676          }
6677    
6678        if (ket == OP_KETRMIN)
6679          {
6680          if (needs_control_head)
6681            OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), private_data_ptr);
6682          OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(stacksize), STR_PTR, 0);
6683          if (BACKTRACK_AS(bracket_backtrack)->u.framesize == no_frame)
6684            OP2(SLJIT_SUB, SLJIT_MEM1(SLJIT_LOCALS_REG), private_data_ptr, STACK_TOP, 0, SLJIT_IMM, needs_control_head ? (2 * sizeof(sljit_sw)) : sizeof(sljit_sw));
6685          OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(stacksize + 1), TMP2, 0);
6686          }
6687        else if (ket == OP_KETRMAX || has_alternatives)
6688          OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(stacksize), STR_PTR, 0);
6689      }      }
6690    else    else
6691      {      {
6692      if (ket == OP_KETRMIN || ket == OP_KETRMAX || has_alternatives)      if (ket != OP_KET || has_alternatives)
6693          stacksize++;
6694    
6695        stacksize += BACKTRACK_AS(bracket_backtrack)->u.framesize + 1;
6696        allocate_stack(common, stacksize);
6697    
6698        if (needs_control_head)
6699          OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), TMP2, 0);
6700    
6701        OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), private_data_ptr);
6702        OP2(SLJIT_SUB, TMP2, 0, STACK_TOP, 0, SLJIT_IMM, stacksize * sizeof(sljit_sw));
6703    
6704        stacksize = needs_control_head ? 1 : 0;
6705        if (ket != OP_KET || has_alternatives)
6706        {        {
6707        allocate_stack(common, BACKTRACK_AS(bracket_backtrack)->u.framesize + 2);        OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(stacksize), STR_PTR, 0);
       OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), private_data_ptr);  
       OP2(SLJIT_SUB, TMP2, 0, STACK_TOP, 0, SLJIT_IMM, -STACK(BACKTRACK_AS(bracket_backtrack)->u.framesize + 1));  
       OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), STR_PTR, 0);  
6708        OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), private_data_ptr, TMP2, 0);        OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), private_data_ptr, TMP2, 0);
6709        OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(1), TMP1, 0);        stacksize++;
6710        init_frame(common, ccbegin, BACKTRACK_AS(bracket_backtrack)->u.framesize + 1, 2, FALSE);        OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(stacksize), TMP1, 0);
6711        }        }
6712      else      else
6713        {        {
       allocate_stack(common, BACKTRACK_AS(bracket_backtrack)->u.framesize + 1);  
       OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), private_data_ptr);  
       OP2(SLJIT_SUB, TMP2, 0, STACK_TOP, 0, SLJIT_IMM, -STACK(BACKTRACK_AS(bracket_backtrack)->u.framesize));  
6714        OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), private_data_ptr, TMP2, 0);        OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), private_data_ptr, TMP2, 0);
6715        OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), TMP1, 0);        OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(stacksize), TMP1, 0);
       init_frame(common, ccbegin, BACKTRACK_AS(bracket_backtrack)->u.framesize, 1, FALSE);  
6716        }        }
6717        init_frame(common, ccbegin, NULL, BACKTRACK_AS(bracket_backtrack)->u.framesize + stacksize, stacksize + 1, FALSE);
6718      }      }
6719    }    }
6720  else if (opcode == OP_CBRA || opcode == OP_SCBRA)  else if (opcode == OP_CBRA || opcode == OP_SCBRA)
# Line 6180  if (opcode == OP_COND || opcode == OP_SC Line 6763  if (opcode == OP_COND || opcode == OP_SC
6763        CMP(SLJIT_C_EQUAL, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(GET2(matchingpath, 1) << 1), SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(1)));        CMP(SLJIT_C_EQUAL, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(GET2(matchingpath, 1) << 1), SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(1)));
6764      matchingpath += 1 + IMM2_SIZE;      matchingpath += 1 + IMM2_SIZE;
6765      }      }
6766    else if (*matchingpath == OP_NCREF)    else if (*matchingpath == OP_DNCREF)
6767      {      {
6768      SLJIT_ASSERT(has_alternatives);      SLJIT_ASSERT(has_alternatives);
     stacksize = GET2(matchingpath, 1);  
     jump = CMP(SLJIT_C_NOT_EQUAL, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(stacksize << 1), SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(1));  
   
     OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), POSSESSIVE1, STACK_TOP, 0);  
     OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), LOCALS0, SLJIT_IMM, common->name_count);  
     OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), LOCALS1, SLJIT_IMM, common->name_entry_size);  
     OP1(SLJIT_MOV, SLJIT_SCRATCH_REG1, 0, SLJIT_IMM, (stacksize << 8) | (common->ovector_start / sizeof(sljit_sw)));  
     GET_LOCAL_BASE(SLJIT_SCRATCH_REG2, 0, 0);  
     OP1(SLJIT_MOV, SLJIT_SCRATCH_REG3, 0, SLJIT_IMM, common->name_table);  
     sljit_emit_ijump(compiler, SLJIT_CALL3, SLJIT_IMM, SLJIT_FUNC_OFFSET(do_searchovector));  
     OP1(SLJIT_MOV, STACK_TOP, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), POSSESSIVE1);  
     add_jump(compiler, &(BACKTRACK_AS(bracket_backtrack)->u.condfailed), CMP(SLJIT_C_EQUAL, SLJIT_SCRATCH_REG1, 0, SLJIT_IMM, 0));  
6769    
6770      JUMPHERE(jump);      i = GET2(matchingpath, 1 + IMM2_SIZE);
6771      matchingpath += 1 + IMM2_SIZE;      slot = common->name_table + GET2(matchingpath, 1) * common->name_entry_size;
6772        OP1(SLJIT_MOV, TMP3, 0, STR_PTR, 0);
6773        OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(1));
6774        OP2(SLJIT_SUB | SLJIT_SET_E, TMP2, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(GET2(slot, 0) << 1), TMP1, 0);
6775        slot += common->name_entry_size;
6776        i--;
6777        while (i-- > 0)
6778          {
6779          OP2(SLJIT_SUB, STR_PTR, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(GET2(slot, 0) << 1), TMP1, 0);
6780          OP2(SLJIT_OR | SLJIT_SET_E, TMP2, 0, TMP2, 0, STR_PTR, 0);
6781          slot += common->name_entry_size;
6782          }
6783        OP1(SLJIT_MOV, STR_PTR, 0, TMP3, 0);
6784        add_jump(compiler, &(BACKTRACK_AS(bracket_backtrack)->u.condfailed), JUMP(SLJIT_C_ZERO));
6785        matchingpath += 1 + 2 * IMM2_SIZE;
6786      }      }
6787    else if (*matchingpath == OP_RREF || *matchingpath == OP_NRREF)    else if (*matchingpath == OP_RREF || *matchingpath == OP_DNRREF)
6788      {      {
6789      /* Never has other case. */      /* Never has other case. */
6790      BACKTRACK_AS(bracket_backtrack)->u.condfailed = NULL;      BACKTRACK_AS(bracket_backtrack)->u.condfailed = NULL;
6791        SLJIT_ASSERT(!has_alternatives);
6792    
6793      stacksize = GET2(matchingpath, 1);      if (*matchingpath == OP_RREF)
     if (common->currententry == NULL)  
       stacksize = 0;  
     else if (stacksize == RREF_ANY)  
       stacksize = 1;  
     else if (common->currententry->start == 0)  
       stacksize = stacksize == 0;  
     else  
       stacksize = stacksize == (int)GET2(common->start, common->currententry->start + 1 + LINK_SIZE);  
   
     if (*matchingpath == OP_RREF || stacksize || common->currententry == NULL)  
6794        {        {
6795        SLJIT_ASSERT(!has_alternatives);        stacksize = GET2(matchingpath, 1);
6796          if (common->currententry == NULL)
6797            stacksize = 0;
6798          else if (stacksize == RREF_ANY)
6799            stacksize = 1;
6800          else if (common->currententry->start == 0)
6801            stacksize = stacksize == 0;
6802          else
6803            stacksize = stacksize == (int)GET2(common->start, common->currententry->start + 1 + LINK_SIZE);
6804    
6805        if (stacksize != 0)        if (stacksize != 0)
6806          matchingpath += 1 + IMM2_SIZE;          matchingpath += 1 + IMM2_SIZE;
6807          }
6808        else
6809          {
6810          if (common->currententry == NULL || common->currententry->start == 0)
6811            stacksize = 0;
6812        else        else
6813          {          {
6814            stacksize = GET2(matchingpath, 1 + IMM2_SIZE);
6815            slot = common->name_table + GET2(matchingpath, 1) * common->name_entry_size;
6816            i = (int)GET2(common->start, common->currententry->start + 1 + LINK_SIZE);
6817            while (stacksize > 0)
6818              {
6819              if ((int)GET2(slot, 0) == i)
6820                break;
6821              slot += common->name_entry_size;
6822              stacksize--;
6823              }
6824            }
6825    
6826          if (stacksize != 0)
6827            matchingpath += 1 + 2 * IMM2_SIZE;
6828          }
6829    
6830          /* The stacksize == 0 is a common "else" case. */
6831          if (stacksize == 0)
6832            {
6833          if (*cc == OP_ALT)          if (*cc == OP_ALT)
6834            {            {
6835            matchingpath = cc + 1 + LINK_SIZE;            matchingpath = cc + 1 + LINK_SIZE;
# Line 6229  if (opcode == OP_COND || opcode == OP_SC Line 6838  if (opcode == OP_COND || opcode == OP_SC
6838          else          else
6839            matchingpath = cc;            matchingpath = cc;
6840          }          }
       }  
     else  
       {  
       SLJIT_ASSERT(has_alternatives);  
   
       stacksize = GET2(matchingpath, 1);  
       OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), POSSESSIVE1, STACK_TOP, 0);  
       OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), LOCALS0, SLJIT_IMM, common->name_count);  
       OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), LOCALS1, SLJIT_IMM, common->name_entry_size);  
       OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), POSSESSIVE0, SLJIT_IMM, GET2(common->start, common->currententry->start + 1 + LINK_SIZE));  
       OP1(SLJIT_MOV, SLJIT_SCRATCH_REG1, 0, SLJIT_IMM, stacksize);  
       GET_LOCAL_BASE(SLJIT_SCRATCH_REG2, 0, 0);  
       OP1(SLJIT_MOV, SLJIT_SCRATCH_REG3, 0, SLJIT_IMM, common->name_table);  
       sljit_emit_ijump(compiler, SLJIT_CALL3, SLJIT_IMM, SLJIT_FUNC_OFFSET(do_searchgroups));  
       OP1(SLJIT_MOV, STACK_TOP, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), POSSESSIVE1);  
       add_jump(compiler, &(BACKTRACK_AS(bracket_backtrack)->u.condfailed), CMP(SLJIT_C_EQUAL, SLJIT_SCRATCH_REG1, 0, SLJIT_IMM, 0));  
       matchingpath += 1 + IMM2_SIZE;  
       }  
6841      }      }
6842    else    else
6843      {      {
# Line 6267  if (SLJIT_UNLIKELY(sljit_get_compiler_er Line 6858  if (SLJIT_UNLIKELY(sljit_get_compiler_er
6858    return NULL;    return NULL;
6859    
6860  if (opcode == OP_ONCE)  if (opcode == OP_ONCE)
6861    {    match_once_common(common, ket, BACKTRACK_AS(bracket_backtrack)->u.framesize, private_data_ptr, has_alternatives, needs_control_head);
   if (BACKTRACK_AS(bracket_backtrack)->u.framesize < 0)  
     {  
     OP1(SLJIT_MOV, STACK_TOP, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), private_data_ptr);  
     /* TMP2 which is set here used by OP_KETRMAX below. */  
     if (ket == OP_KETRMAX)  
       OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(STACK_TOP), 0);  
     else if (ket == OP_KETRMIN)  
       {  
       /* Move the STR_PTR to the private_data_ptr. */  
       OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), private_data_ptr, SLJIT_MEM1(STACK_TOP), 0);  
       }  
     }  
   else  
     {  
     stacksize = (ket == OP_KETRMIN || ket == OP_KETRMAX || has_alternatives) ? 2 : 1;  
     OP2(SLJIT_ADD, STACK_TOP, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), private_data_ptr, SLJIT_IMM, (BACKTRACK_AS(bracket_backtrack)->u.framesize + stacksize) * sizeof(sljit_sw));  
     if (ket == OP_KETRMAX)  
       {  
       /* TMP2 which is set here used by OP_KETRMAX below. */  
       OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(STACK_TOP), STACK(0));  
       }  
     }  
   }  
6862    
6863  stacksize = 0;  stacksize = 0;
6864    if (repeat_type == OP_MINUPTO)
6865      {
6866      /* We need to preserve the counter. TMP2 will be used below. */
6867      OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), repeat_ptr);
6868      stacksize++;
6869      }
6870  if (ket != OP_KET || bra != OP_BRA)  if (ket != OP_KET || bra != OP_BRA)
6871    stacksize++;    stacksize++;
6872  if (offset != 0)  if (offset != 0)
# Line 6309  if (stacksize > 0) Line 6883  if (stacksize > 0)
6883    allocate_stack(common, stacksize);    allocate_stack(common, stacksize);
6884    
6885  stacksize = 0;  stacksize = 0;
6886    if (repeat_type == OP_MINUPTO)
6887      {
6888      /* TMP2 was set above. */
6889      OP2(SLJIT_SUB, SLJIT_MEM1(STACK_TOP), STACK(stacksize), TMP2, 0, SLJIT_IMM, 1);
6890      stacksize++;
6891      }
6892    
6893  if (ket != OP_KET || bra != OP_BRA)  if (ket != OP_KET || bra != OP_BRA)
6894    {    {
6895    if (ket != OP_KET)    if (ket != OP_KET)
# Line 6319  if (ket != OP_KET || bra != OP_BRA) Line 6900  if (ket != OP_KET || bra != OP_BRA)
6900    }    }
6901    
6902  if (offset != 0)  if (offset != 0)
6903    {    stacksize = match_capture_common(common, stacksize, offset, private_data_ptr);
   if (common->capture_last_ptr != 0)  
     {  
     OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), common->capture_last_ptr);  
     OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), common->capture_last_ptr, SLJIT_IMM, offset >> 1);  
     OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(stacksize), TMP2, 0);  
     stacksize++;  
     }  
   if (common->optimized_cbracket[offset >> 1] == 0)  
     {  
     OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(offset));  
     OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(offset + 1));  
     OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(stacksize), TMP1, 0);  
     OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), private_data_ptr);  
     OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(stacksize + 1), TMP2, 0);  
     OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(offset + 1), STR_PTR, 0);  
     OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(offset), TMP1, 0);  
     stacksize += 2;  
     }  
   }  
6904    
6905  if (has_alternatives)  if (has_alternatives)
6906    {    {
# Line 6357  if (offset != 0 && common->optimized_cbr Line 6919  if (offset != 0 && common->optimized_cbr
6919    
6920  if (ket == OP_KETRMAX)  if (ket == OP_KETRMAX)
6921    {    {
6922    if (opcode == OP_ONCE || opcode >= OP_SBRA)    if (repeat_type != 0)
6923        {
6924        if (has_alternatives)
6925          BACKTRACK_AS(bracket_backtrack)->alternative_matchingpath = LABEL();
6926        OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_MEM1(SLJIT_LOCALS_REG), repeat_ptr, SLJIT_MEM1(SLJIT_LOCALS_REG), repeat_ptr, SLJIT_IMM, 1);
6927        JUMPTO(SLJIT_C_NOT_ZERO, rmax_label);
6928        /* Drop STR_PTR for greedy plus quantifier. */
6929        if (opcode != OP_ONCE)
6930          free_stack(common, 1);
6931        }
6932      else if (opcode == OP_ONCE || opcode >= OP_SBRA)
6933      {      {
6934      if (has_alternatives)      if (has_alternatives)
6935        BACKTRACK_AS(bracket_backtrack)->alternative_matchingpath = LABEL();        BACKTRACK_AS(bracket_backtrack)->alternative_matchingpath = LABEL();
6936      /* Checking zero-length iteration. */      /* Checking zero-length iteration. */
6937      if (opcode != OP_ONCE)      if (opcode != OP_ONCE)
6938        {        {
6939        CMPTO(SLJIT_C_NOT_EQUAL, SLJIT_MEM1(SLJIT_LOCALS_REG), private_data_ptr, STR_PTR, 0, rmaxlabel);        CMPTO(SLJIT_C_NOT_EQUAL, SLJIT_MEM1(SLJIT_LOCALS_REG), private_data_ptr, STR_PTR, 0, rmax_label);
6940        /* Drop STR_PTR for greedy plus quantifier. */        /* Drop STR_PTR for greedy plus quantifier. */
6941        if (bra != OP_BRAZERO)        if (bra != OP_BRAZERO)
6942          free_stack(common, 1);          free_stack(common, 1);
6943        }        }
6944      else      else
6945        /* TMP2 must contain the starting STR_PTR. */        /* TMP2 must contain the starting STR_PTR. */
6946        CMPTO(SLJIT_C_NOT_EQUAL, TMP2, 0, STR_PTR, 0, rmaxlabel);        CMPTO(SLJIT_C_NOT_EQUAL, TMP2, 0, STR_PTR, 0, rmax_label);
6947      }      }
6948    else    else
6949      JUMPTO(SLJIT_JUMP, rmaxlabel);      JUMPTO(SLJIT_JUMP, rmax_label);
6950    BACKTRACK_AS(bracket_backtrack)->recursive_matchingpath = LABEL();    BACKTRACK_AS(bracket_backtrack)->recursive_matchingpath = LABEL();
6951    }    }
6952    
6953    if (repeat_type == OP_EXACT)
6954      {
6955      OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_MEM1(SLJIT_LOCALS_REG), repeat_ptr, SLJIT_MEM1(SLJIT_LOCALS_REG), repeat_ptr, SLJIT_IMM, 1);
6956      JUMPTO(SLJIT_C_NOT_ZERO, rmax_label);
6957      }
6958    else if (repeat_type == OP_UPTO)
6959      {
6960      /* We need to preserve the counter. */
6961      OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), repeat_ptr);
6962      allocate_stack(common, 1);
6963      OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), TMP2, 0);
6964      }
6965    
6966  if (bra == OP_BRAZERO)  if (bra == OP_BRAZERO)
6967    BACKTRACK_AS(bracket_backtrack)->zero_matchingpath = LABEL();    BACKTRACK_AS(bracket_backtrack)->zero_matchingpath = LABEL();
6968    
# Line 6385  if (bra == OP_BRAMINZERO) Line 6970  if (bra == OP_BRAMINZERO)
6970    {    {
6971    /* This is a backtrack path! (From the viewpoint of OP_BRAMINZERO) */    /* This is a backtrack path! (From the viewpoint of OP_BRAMINZERO) */
6972    JUMPTO(SLJIT_JUMP, ((braminzero_backtrack *)parent)->matchingpath);    JUMPTO(SLJIT_JUMP, ((braminzero_backtrack *)parent)->matchingpath);
6973    if (braminzerojump != NULL)    if (braminzero != NULL)
6974      {      {
6975      JUMPHERE(braminzerojump);      JUMPHERE(braminzero);
6976      /* We need to release the end pointer to perform the      /* We need to release the end pointer to perform the
6977      backtrack for the zero-length iteration. When      backtrack for the zero-length iteration. When
6978      framesize is < 0, OP_ONCE will do the release itself. */      framesize is < 0, OP_ONCE will do the release itself. */
# Line 6403  if (bra == OP_BRAMINZERO) Line 6988  if (bra == OP_BRAMINZERO)
6988    }    }
6989    
6990  if ((ket != OP_KET && bra != OP_BRAMINZERO) || bra == OP_BRAZERO)  if ((ket != OP_KET && bra != OP_BRAMINZERO) || bra == OP_BRAZERO)
6991    decrease_call_count(common);    count_match(common);
6992    
6993  /* Skip the other alternatives. */  /* Skip the other alternatives. */
6994  while (*cc == OP_ALT)  while (*cc == OP_ALT)
6995    cc += GET(cc, 1);    cc += GET(cc, 1);
6996  cc += 1 + LINK_SIZE;  cc += 1 + LINK_SIZE;
6997  return cc;  
6998    /* Temporarily encoding the needs_control_head in framesize. */
6999    if (opcode == OP_ONCE)
7000      BACKTRACK_AS(bracket_backtrack)->u.framesize = (BACKTRACK_AS(bracket_backtrack)->u.framesize << 1) | (needs_control_head ? 1 : 0);
7001    return cc + repeat_length;
7002  }  }
7003    
7004  static pcre_uchar *compile_bracketpos_matchingpath(compiler_common *common, pcre_uchar *cc, backtrack_common *parent)  static pcre_uchar *compile_bracketpos_matchingpath(compiler_common *common, pcre_uchar *cc, backtrack_common *parent)
# Line 6419  backtrack_common *backtrack; Line 7008  backtrack_common *backtrack;
7008  pcre_uchar opcode;  pcre_uchar opcode;
7009  int private_data_ptr;  int private_data_ptr;
7010  int cbraprivptr = 0;  int cbraprivptr = 0;
7011    BOOL needs_control_head;
7012  int framesize;  int framesize;
7013  int stacksize;  int stacksize;
7014  int offset = 0;  int offset = 0;
7015  BOOL zero = FALSE;  BOOL zero = FALSE;
7016  pcre_uchar *ccbegin = NULL;  pcre_uchar *ccbegin = NULL;
7017  int stack;  int stack; /* Also contains the offset of control head. */
7018  struct sljit_label *loop = NULL;  struct sljit_label *loop = NULL;
7019  struct jump_list *emptymatch = NULL;  struct jump_list *emptymatch = NULL;
7020    
# Line 6462  switch(opcode) Line 7052  switch(opcode)
7052    break;    break;
7053    }    }
7054    
7055  framesize = get_framesize(common, cc, FALSE);  framesize = get_framesize(common, cc, NULL, FALSE, &needs_control_head);
7056  BACKTRACK_AS(bracketpos_backtrack)->framesize = framesize;  BACKTRACK_AS(bracketpos_backtrack)->framesize = framesize;
7057  if (framesize < 0)  if (framesize < 0)
7058    {    {
# Line 6475  if (framesize < 0) Line 7065  if (framesize < 0)
7065    else    else
7066      stacksize = 1;      stacksize = 1;
7067    
7068      if (needs_control_head)
7069        stacksize++;
7070    if (!zero)    if (!zero)
7071      stacksize++;      stacksize++;
7072    
# Line 6483  if (framesize < 0) Line 7075  if (framesize < 0)
7075    if (framesize == no_frame)    if (framesize == no_frame)
7076      OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), private_data_ptr, STACK_TOP, 0);      OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), private_data_ptr, STACK_TOP, 0);
7077    
7078      stack = 0;
7079    if (offset != 0)    if (offset != 0)
7080      {      {
7081        stack = 2;
7082      OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(offset));      OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(offset));
7083      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));
7084      OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), TMP1, 0);      OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), TMP1, 0);
7085      if (common->capture_last_ptr != 0)      if (common->capture_last_ptr != 0)
7086        OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), common->capture_last_ptr);        OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), common->capture_last_ptr);
7087      OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(1), TMP2, 0);      OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(1), TMP2, 0);
7088        if (needs_control_head)
7089          OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), common->control_head_ptr);
7090      if (common->capture_last_ptr != 0)      if (common->capture_last_ptr != 0)
7091          {
7092        OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(2), TMP1, 0);        OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(2), TMP1, 0);
7093          stack = 3;
7094          }
7095      }      }
7096    else    else
7097        {
7098        if (needs_control_head)
7099          OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), common->control_head_ptr);
7100      OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), STR_PTR, 0);      OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), STR_PTR, 0);
7101        stack = 1;
7102        }
7103    
7104      if (needs_control_head)
7105        stack++;
7106    if (!zero)    if (!zero)
7107      OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(stacksize - 1), SLJIT_IMM, 1);      OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(stack), SLJIT_IMM, 1);
7108      if (needs_control_head)
7109        {
7110        stack--;
7111        OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(stack), TMP2, 0);
7112        }
7113    }    }
7114  else  else
7115    {    {
7116    stacksize = framesize + 1;    stacksize = framesize + 1;
7117    if (!zero)    if (!zero)
7118      stacksize++;      stacksize++;
7119    if (opcode == OP_BRAPOS || opcode == OP_SBRAPOS)    if (needs_control_head)
7120        stacksize++;
7121      if (offset == 0)
7122      stacksize++;      stacksize++;
7123    BACKTRACK_AS(bracketpos_backtrack)->stacksize = stacksize;    BACKTRACK_AS(bracketpos_backtrack)->stacksize = stacksize;
7124    
7125    allocate_stack(common, stacksize);    allocate_stack(common, stacksize);
7126    OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), private_data_ptr);    OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), private_data_ptr);
7127    OP2(SLJIT_SUB, TMP2, 0, STACK_TOP, 0, SLJIT_IMM, -STACK(stacksize - 1));    if (needs_control_head)
7128    OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), private_data_ptr, TMP2, 0);      OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), common->control_head_ptr);
7129      OP2(SLJIT_SUB, SLJIT_MEM1(SLJIT_LOCALS_REG), private_data_ptr, STACK_TOP, 0, SLJIT_IMM, -STACK(stacksize - 1));
7130    
7131    stack = 0;    stack = 0;
7132    if (!zero)    if (!zero)
7133      {      {
7134      OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), SLJIT_IMM, 1);      OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), SLJIT_IMM, 1);
7135        stack = 1;
7136        }
7137      if (needs_control_head)
7138        {
7139        OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(stack), TMP2, 0);
7140      stack++;      stack++;
7141      }      }
7142    if (opcode == OP_BRAPOS || opcode == OP_SBRAPOS)    if (offset == 0)
7143      {      {
7144      OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(stack), STR_PTR, 0);      OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(stack), STR_PTR, 0);
7145      stack++;      stack++;
7146      }      }
7147    OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(stack), TMP1, 0);    OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(stack), TMP1, 0);
7148    init_frame(common, cc, stacksize - 1, stacksize - framesize, FALSE);    init_frame(common, cc, NULL, stacksize - 1, stacksize - framesize, FALSE);
7149      stack -= 1 + (offset == 0);
7150    }    }
7151    
7152  if (offset != 0)  if (offset != 0)
# Line 6602  while (*cc != OP_KETRPOS) Line 7222  while (*cc != OP_KETRPOS)
7222          OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), SLJIT_IMM, 0);          OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), SLJIT_IMM, 0);
7223        }        }
7224      }      }
7225    
7226      if (needs_control_head)
7227        OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), common->control_head_ptr, SLJIT_MEM1(STACK_TOP), STACK(stack));
7228    
7229    JUMPTO(SLJIT_JUMP, loop);    JUMPTO(SLJIT_JUMP, loop);
7230    flush_stubs(common);    flush_stubs(common);
7231    
# Line 6638  while (*cc != OP_KETRPOS) Line 7262  while (*cc != OP_KETRPOS)
7262    ccbegin = cc + 1 + LINK_SIZE;    ccbegin = cc + 1 + LINK_SIZE;
7263    }    }
7264    
7265    /* We don't have to restore the control head in case of a failed match. */
7266    
7267  backtrack->topbacktracks = NULL;  backtrack->topbacktracks = NULL;
7268  if (!zero)  if (!zero)
7269    {    {
# Line 6649  if (!zero) Line 7275  if (!zero)
7275    
7276  /* None of them matched. */  /* None of them matched. */
7277  set_jumps(emptymatch, LABEL());  set_jumps(emptymatch, LABEL());
7278  decrease_call_count(common);  count_match(common);
7279  return cc + 1 + LINK_SIZE;  return cc + 1 + LINK_SIZE;
7280  }  }
7281    
7282  static SLJIT_INLINE pcre_uchar *get_iterator_parameters(compiler_common *common, pcre_uchar *cc, pcre_uchar *opcode, pcre_uchar *type, int *arg1, int *arg2, pcre_uchar **end)  static SLJIT_INLINE pcre_uchar *get_iterator_parameters(compiler_common *common, pcre_uchar *cc, pcre_uchar *opcode, pcre_uchar *type, int *max, int *min, pcre_uchar **end)
7283  {  {
7284  int class_len;  int class_len;
7285    
# Line 6689  else if (*opcode >= OP_TYPESTAR && *opco Line 7315  else if (*opcode >= OP_TYPESTAR && *opco
7315    }    }
7316  else  else
7317    {    {
7318    SLJIT_ASSERT(*opcode >= OP_CLASS || *opcode <= OP_XCLASS);    SLJIT_ASSERT(*opcode == OP_CLASS || *opcode == OP_NCLASS || *opcode == OP_XCLASS);
7319    *type = *opcode;    *type = *opcode;
7320    cc++;    cc++;
7321    class_len = (*type < OP_XCLASS) ? (int)(1 + (32 / sizeof(pcre_uchar))) : GET(cc, 0);    class_len = (*type < OP_XCLASS) ? (int)(1 + (32 / sizeof(pcre_uchar))) : GET(cc, 0);
# Line 6700  else Line 7326  else
7326      if (end != NULL)      if (end != NULL)
7327        *end = cc + class_len;        *end = cc + class_len;
7328      }      }
7329      else if (*opcode >= OP_CRPOSSTAR && *opcode <= OP_CRPOSQUERY)
7330        {
7331        *opcode -= OP_CRPOSSTAR - OP_POSSTAR;
7332        if (end != NULL)
7333          *end = cc + class_len;
7334        }
7335    else    else
7336      {      {
7337      SLJIT_ASSERT(*opcode == OP_CRRANGE || *opcode == OP_CRMINRANGE);      SLJIT_ASSERT(*opcode == OP_CRRANGE || *opcode == OP_CRMINRANGE || *opcode == OP_CRPOSRANGE);
7338      *arg1 = GET2(cc, (class_len + IMM2_SIZE));      *max = GET2(cc, (class_len + IMM2_SIZE));
7339      *arg2 = GET2(cc, class_len);      *min = GET2(cc, class_len);
7340    
7341      if (*arg2 == 0)      if (*min == 0)
7342        {        {
7343        SLJIT_ASSERT(*arg1 != 0);        SLJIT_ASSERT(*max != 0);
7344        *opcode = (*opcode == OP_CRRANGE) ? OP_UPTO : OP_MINUPTO;        *opcode = (*opcode == OP_CRRANGE) ? OP_UPTO : (*opcode == OP_CRMINRANGE ? OP_MINUPTO : OP_POSUPTO);
7345        }        }
7346      if (*arg1 == *arg2)      if (*max == *min)
7347        *opcode = OP_EXACT;        *opcode = OP_EXACT;
7348    
7349      if (end != NULL)      if (end != NULL)
# Line 6722  else Line 7354  else
7354    
7355  if (*opcode == OP_UPTO || *opcode == OP_MINUPTO || *opcode == OP_EXACT || *opcode == OP_POSUPTO)  if (*opcode == OP_UPTO || *opcode == OP_MINUPTO || *opcode == OP_EXACT || *opcode == OP_POSUPTO)
7356    {    {
7357    *arg1 = GET2(cc, 0);    *max = GET2(cc, 0);
7358    cc += IMM2_SIZE;    cc += IMM2_SIZE;
7359    }    }
7360    
# Line 6751  DEFINE_COMPILER; Line 7383  DEFINE_COMPILER;
7383  backtrack_common *backtrack;  backtrack_common *backtrack;
7384  pcre_uchar opcode;  pcre_uchar opcode;
7385  pcre_uchar type;  pcre_uchar type;
7386  int arg1 = -1, arg2 = -1;  int max = -1, min = -1;
7387  pcre_uchar* end;  pcre_uchar* end;
7388  jump_list *nomatch = NULL;  jump_list *nomatch = NULL;
7389  struct sljit_jump *jump = NULL;  struct sljit_jump *jump = NULL;
# Line 6764  int tmp_base, tmp_offset; Line 7396  int tmp_base, tmp_offset;
7396    
7397  PUSH_BACKTRACK(sizeof(iterator_backtrack), cc, NULL);  PUSH_BACKTRACK(sizeof(iterator_backtrack), cc, NULL);
7398    
7399  cc = get_iterator_parameters(common, cc, &opcode, &type, &arg1, &arg2, &end);  cc = get_iterator_parameters(common, cc, &opcode, &type, &max, &min, &end);
7400    
7401  switch(type)  switch(type)
7402    {    {
# Line 6835  switch(opcode) Line 7467  switch(opcode)
7467        {        {
7468        OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), POSSESSIVE0);        OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), POSSESSIVE0);
7469        OP2(SLJIT_ADD, TMP1, 0, TMP1, 0, SLJIT_IMM, 1);        OP2(SLJIT_ADD, TMP1, 0, TMP1, 0, SLJIT_IMM, 1);
7470        if (opcode == OP_CRRANGE && arg2 > 0)        if (opcode == OP_CRRANGE && min > 0)
7471          CMPTO(SLJIT_C_LESS, TMP1, 0, SLJIT_IMM, arg2, label);          CMPTO(SLJIT_C_LESS, TMP1, 0, SLJIT_IMM, min, label);
7472        if (opcode == OP_UPTO || (opcode == OP_CRRANGE && arg1 > 0))        if (opcode == OP_UPTO || (opcode == OP_CRRANGE && max > 0))
7473          jump = CMP(SLJIT_C_GREATER_EQUAL, TMP1, 0, SLJIT_IMM, arg1);          jump = CMP(SLJIT_C_GREATER_EQUAL, TMP1, 0, SLJIT_IMM, max);
7474        OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), POSSESSIVE0, TMP1, 0);        OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), POSSESSIVE0, TMP1, 0);
7475        }        }
7476    
# Line 6865  switch(opcode) Line 7497  switch(opcode)
7497      OP1(SLJIT_MOV, base, offset0, STR_PTR, 0);      OP1(SLJIT_MOV, base, offset0, STR_PTR, 0);
7498      if (opcode <= OP_PLUS)      if (opcode <= OP_PLUS)
7499        JUMPTO(SLJIT_JUMP, label);        JUMPTO(SLJIT_JUMP, label);
7500      else if (opcode == OP_CRRANGE && arg1 == 0)      else if (opcode == OP_CRRANGE && max == 0)
7501        {        {
7502        OP2(SLJIT_ADD, base, offset1, base, offset1, SLJIT_IMM, 1);        OP2(SLJIT_ADD, base, offset1, base, offset1, SLJIT_IMM, 1);
7503        JUMPTO(SLJIT_JUMP, label);        JUMPTO(SLJIT_JUMP, label);
# Line 6875  switch(opcode) Line 7507  switch(opcode)
7507        OP1(SLJIT_MOV, TMP1, 0, base, offset1);        OP1(SLJIT_MOV, TMP1, 0, base, offset1);
7508        OP2(SLJIT_ADD, TMP1, 0, TMP1, 0, SLJIT_IMM, 1);        OP2(SLJIT_ADD, TMP1, 0, TMP1, 0, SLJIT_IMM, 1);
7509        OP1(SLJIT_MOV, base, offset1, TMP1, 0);        OP1(SLJIT_MOV, base, offset1, TMP1, 0);
7510        CMPTO(SLJIT_C_LESS, TMP1, 0, SLJIT_IMM, arg1 + 1, label);        CMPTO(SLJIT_C_LESS, TMP1, 0, SLJIT_IMM, max + 1, label);
7511        }        }
7512      set_jumps(nomatch, LABEL());      set_jumps(nomatch, LABEL());
7513      if (opcode == OP_CRRANGE)      if (opcode == OP_CRRANGE)
7514        add_jump(compiler, &backtrack->topbacktracks, CMP(SLJIT_C_LESS, base, offset1, SLJIT_IMM, arg2 + 1));        add_jump(compiler, &backtrack->topbacktracks, CMP(SLJIT_C_LESS, base, offset1, SLJIT_IMM, min + 1));
7515      OP1(SLJIT_MOV, STR_PTR, 0, base, offset0);      OP1(SLJIT_MOV, STR_PTR, 0, base, offset0);
7516      }      }
7517    BACKTRACK_AS(iterator_backtrack)->matchingpath = LABEL();    BACKTRACK_AS(iterator_backtrack)->matchingpath = LABEL();
# Line 6917  switch(opcode) Line 7549  switch(opcode)
7549    break;    break;
7550    
7551    case OP_EXACT:    case OP_EXACT:
7552    OP1(SLJIT_MOV, tmp_base, tmp_offset, SLJIT_IMM, arg1);    OP1(SLJIT_MOV, tmp_base, tmp_offset, SLJIT_IMM, max);
7553    label = LABEL();    label = LABEL();
7554    compile_char1_matchingpath(common, type, cc, &backtrack->topbacktracks);    compile_char1_matchingpath(common, type, cc, &backtrack->topbacktracks);
7555    OP2(SLJIT_SUB | SLJIT_SET_E, tmp_base, tmp_offset, tmp_base, tmp_offset, SLJIT_IMM, 1);    OP2(SLJIT_SUB | SLJIT_SET_E, tmp_base, tmp_offset, tmp_base, tmp_offset, SLJIT_IMM, 1);
# Line 6930  switch(opcode) Line 7562  switch(opcode)
7562    if (opcode == OP_POSPLUS)    if (opcode == OP_POSPLUS)
7563      compile_char1_matchingpath(common, type, cc, &backtrack->topbacktracks);      compile_char1_matchingpath(common, type, cc, &backtrack->topbacktracks);
7564    if (opcode == OP_POSUPTO)    if (opcode == OP_POSUPTO)
7565      OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), POSSESSIVE1, SLJIT_IMM, arg1);      OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), POSSESSIVE1, SLJIT_IMM, max);
7566    OP1(SLJIT_MOV, tmp_base, tmp_offset, STR_PTR, 0);    OP1(SLJIT_MOV, tmp_base, tmp_offset, STR_PTR, 0);
7567    label = LABEL();    label = LABEL();
7568    compile_char1_matchingpath(common, type, cc, &nomatch);    compile_char1_matchingpath(common, type, cc, &nomatch);
# Line 6946  switch(opcode) Line 7578  switch(opcode)
7578    OP1(SLJIT_MOV, STR_PTR, 0, tmp_base, tmp_offset);    OP1(SLJIT_MOV, STR_PTR, 0, tmp_base, tmp_offset);
7579    break;    break;
7580    
7581    case OP_POSQUERY:    case OP_POSQUERY:
7582    OP1(SLJIT_MOV, tmp_base, tmp_offset, STR_PTR, 0);    OP1(SLJIT_MOV, tmp_base, tmp_offset, STR_PTR, 0);
7583    compile_char1_matchingpath(common, type, cc, &nomatch);    compile_char1_matchingpath(common, type, cc, &nomatch);
7584    OP1(SLJIT_MOV, tmp_base, tmp_offset, STR_PTR, 0);    OP1(SLJIT_MOV, tmp_base, tmp_offset, STR_PTR, 0);
7585    set_jumps(nomatch, LABEL());    set_jumps(nomatch, LABEL());
7586    OP1(SLJIT_MOV, STR_PTR, 0, tmp_base, tmp_offset);    OP1(SLJIT_MOV, STR_PTR, 0, tmp_base, tmp_offset);
7587    break;    break;
7588    
7589      case OP_CRPOSRANGE:
7590      /* Combination of OP_EXACT and OP_POSSTAR or OP_POSUPTO */
7591      OP1(SLJIT_MOV, tmp_base, tmp_offset, SLJIT_IMM, min);
7592      label = LABEL();
7593      compile_char1_matchingpath(common, type, cc, &backtrack->topbacktracks);
7594      OP2(SLJIT_SUB | SLJIT_SET_E, tmp_base, tmp_offset, tmp_base, tmp_offset, SLJIT_IMM, 1);
7595      JUMPTO(SLJIT_C_NOT_ZERO, label);
7596    
7597      if (max != 0)
7598        {
7599        SLJIT_ASSERT(max - min > 0);
7600        OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), POSSESSIVE1, SLJIT_IMM, max - min);
7601        }
7602      OP1(SLJIT_MOV, tmp_base, tmp_offset, STR_PTR, 0);
7603      label = LABEL();
7604      compile_char1_matchingpath(common, type, cc, &nomatch);
7605      OP1(SLJIT_MOV, tmp_base, tmp_offset, STR_PTR, 0);
7606      if (max == 0)
7607        JUMPTO(SLJIT_JUMP, label);
7608      else
7609        {
7610        OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_MEM1(SLJIT_LOCALS_REG), POSSESSIVE1, SLJIT_MEM1(SLJIT_LOCALS_REG), POSSESSIVE1, SLJIT_IMM, 1);
7611        JUMPTO(SLJIT_C_NOT_ZERO, label);
7612        }
7613      set_jumps(nomatch, LABEL());
7614      OP1(SLJIT_MOV, STR_PTR, 0, tmp_base, tmp_offset);
7615      break;
7616    
7617    default:    default:
7618    SLJIT_ASSERT_STOP();    SLJIT_ASSERT_STOP();
7619    break;    break;
7620    }    }
7621    
7622  decrease_call_count(common);  count_match(common);
7623  return end;  return end;
7624  }  }
7625    
# Line 7026  if (!optimized_cbracket) Line 7686  if (!optimized_cbracket)
7686  return cc + 1 + IMM2_SIZE;  return cc + 1 + IMM2_SIZE;
7687  }  }
7688    
7689    static SLJIT_INLINE pcre_uchar *compile_control_verb_matchingpath(compiler_common *common, pcre_uchar *cc, backtrack_common *parent)
7690    {
7691    DEFINE_COMPILER;
7692    backtrack_common *backtrack;
7693    pcre_uchar opcode = *cc;
7694    pcre_uchar *ccend = cc + 1;
7695    
7696    if (opcode == OP_PRUNE_ARG || opcode == OP_SKIP_ARG || opcode == OP_THEN_ARG)
7697      ccend += 2 + cc[1];
7698    
7699    PUSH_BACKTRACK(sizeof(backtrack_common), cc, NULL);
7700    
7701    if (opcode == OP_SKIP)
7702      {
7703      allocate_stack(common, 1);
7704      OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), STR_PTR, 0);
7705      return ccend;
7706      }
7707    
7708    if (opcode == OP_PRUNE_ARG || opcode == OP_THEN_ARG)
7709      {
7710      OP1(SLJIT_MOV, TMP1, 0, ARGUMENTS, 0);
7711      OP1(SLJIT_MOV, TMP2, 0, SLJIT_IMM, (sljit_sw)(cc + 2));
7712      OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), common->mark_ptr, TMP2, 0);
7713      OP1(SLJIT_MOV, SLJIT_MEM1(TMP1), SLJIT_OFFSETOF(jit_arguments, mark_ptr), TMP2, 0);
7714      }
7715    
7716    return ccend;
7717    }
7718    
7719    static pcre_uchar then_trap_opcode[1] = { OP_THEN_TRAP };
7720    
7721    static SLJIT_INLINE void compile_then_trap_matchingpath(compiler_common *common, pcre_uchar *cc, pcre_uchar *ccend, backtrack_common *parent)
7722    {
7723    DEFINE_COMPILER;
7724    backtrack_common *backtrack;
7725    BOOL needs_control_head;
7726    int size;
7727    
7728    PUSH_BACKTRACK_NOVALUE(sizeof(then_trap_backtrack), cc);
7729    common->then_trap = BACKTRACK_AS(then_trap_backtrack);
7730    BACKTRACK_AS(then_trap_backtrack)->common.cc = then_trap_opcode;
7731    BACKTRACK_AS(then_trap_backtrack)->start = (sljit_sw)(cc - common->start);
7732    BACKTRACK_AS(then_trap_backtrack)->framesize = get_framesize(common, cc, ccend, FALSE, &needs_control_head);
7733    
7734    size = BACKTRACK_AS(then_trap_backtrack)->framesize;
7735    size = 3 + (size < 0 ? 0 : size);
7736    
7737    OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), common->control_head_ptr);
7738    allocate_stack(common, size);
7739    if (size > 3)
7740      OP2(SLJIT_SUB, SLJIT_MEM1(SLJIT_LOCALS_REG), common->control_head_ptr, STACK_TOP, 0, SLJIT_IMM, (size - 3) * sizeof(sljit_sw));
7741    else
7742      OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), common->control_head_ptr, STACK_TOP, 0);
7743    OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(size - 1), SLJIT_IMM, BACKTRACK_AS(then_trap_backtrack)->start);
7744    OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(size - 2), SLJIT_IMM, type_then_trap);
7745    OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(size - 3), TMP2, 0);
7746    
7747    size = BACKTRACK_AS(then_trap_backtrack)->framesize;
7748    if (size >= 0)
7749      init_frame(common, cc, ccend, size - 1, 0, FALSE);
7750    }
7751    
7752  static void compile_matchingpath(compiler_common *common, pcre_uchar *cc, pcre_uchar *ccend, backtrack_common *parent)  static void compile_matchingpath(compiler_common *common, pcre_uchar *cc, pcre_uchar *ccend, backtrack_common *parent)
7753  {  {
7754  DEFINE_COMPILER;  DEFINE_COMPILER;
7755  backtrack_common *backtrack;  backtrack_common *backtrack;
7756    BOOL has_then_trap = FALSE;
7757    then_trap_backtrack *save_then_trap = NULL;
7758    
7759    SLJIT_ASSERT(*ccend == OP_END || (*ccend >= OP_ALT && *ccend <= OP_KETRPOS));
7760    
7761    if (common->has_then && common->then_offsets[cc - common->start] != 0)
7762      {
7763      SLJIT_ASSERT(*ccend != OP_END && common->control_head_ptr != 0);
7764      has_then_trap = TRUE;
7765      save_then_trap = common->then_trap;
7766      /* Tail item on backtrack. */
7767      compile_then_trap_matchingpath(common, cc, ccend, parent);
7768      }
7769    
7770  while (cc < ccend)  while (cc < ccend)
7771    {    {
# Line 7155  while (cc < ccend) Line 7891  while (cc < ccend)
7891    
7892      case OP_CLASS:      case OP_CLASS:
7893      case OP_NCLASS:      case OP_NCLASS:
7894      if (cc[1 + (32 / sizeof(pcre_uchar))] >= OP_CRSTAR && cc[1 + (32 / sizeof(pcre_uchar))] <= OP_CRMINRANGE)      if (cc[1 + (32 / sizeof(pcre_uchar))] >= OP_CRSTAR && cc[1 + (32 / sizeof(pcre_uchar))] <= OP_CRPOSRANGE)
7895        cc = compile_iterator_matchingpath(common, cc, parent);        cc = compile_iterator_matchingpath(common, cc, parent);
7896      else      else
7897        cc = compile_char1_matchingpath(common, *cc, cc + 1, parent->top != NULL ? &parent->top->nextbacktracks : &parent->topbacktracks);        cc = compile_char1_matchingpath(common, *cc, cc + 1, parent->top != NULL ? &parent->top->nextbacktracks : &parent->topbacktracks);
# Line 7163  while (cc < ccend) Line 7899  while (cc < ccend)
7899    
7900  #if defined SUPPORT_UTF || defined COMPILE_PCRE16 || defined COMPILE_PCRE32  #if defined SUPPORT_UTF || defined COMPILE_PCRE16 || defined COMPILE_PCRE32
7901      case OP_XCLASS:      case OP_XCLASS:
7902      if (*(cc + GET(cc, 1)) >= OP_CRSTAR && *(cc + GET(cc, 1)) <= OP_CRMINRANGE)      if (*(cc + GET(cc, 1)) >= OP_CRSTAR && *(cc + GET(cc, 1)) <= OP_CRPOSRANGE)
7903        cc = compile_iterator_matchingpath(common, cc, parent);        cc = compile_iterator_matchingpath(common, cc, parent);
7904      else      else
7905        cc = compile_char1_matchingpath(common, *cc, cc + 1, parent->top != NULL ? &parent->top->nextbacktracks : &parent->topbacktracks);        cc = compile_char1_matchingpath(common, *cc, cc + 1, parent->top != NULL ? &parent->top->nextbacktracks : &parent->topbacktracks);
# Line 7172  while (cc < ccend) Line 7908  while (cc < ccend)
7908    
7909      case OP_REF:      case OP_REF:
7910      case OP_REFI:      case OP_REFI:
7911      if (cc[1 + IMM2_SIZE] >= OP_CRSTAR && cc[1 + IMM2_SIZE] <= OP_CRMINRANGE)      if (cc[1 + IMM2_SIZE] >= OP_CRSTAR && cc[1 + IMM2_SIZE] <= OP_CRPOSRANGE)
7912        cc = compile_ref_iterator_matchingpath(common, cc, parent);        cc = compile_ref_iterator_matchingpath(common, cc, parent);
7913      else      else
7914        cc = compile_ref_matchingpath(common, cc, parent->top != NULL ? &parent->top->nextbacktracks : &parent->topbacktracks, TRUE, FALSE);        {
7915          compile_ref_matchingpath(common, cc, parent->top != NULL ? &parent->top->nextbacktracks : &parent->topbacktracks, TRUE, FALSE);
7916          cc += 1 + IMM2_SIZE;
7917          }
7918        break;
7919    
7920        case OP_DNREF:
7921        case OP_DNREFI:
7922        if (cc[1 + 2 * IMM2_SIZE] >= OP_CRSTAR && cc[1 + 2 * IMM2_SIZE] <= OP_CRPOSRANGE)
7923          cc = compile_ref_iterator_matchingpath(common, cc, parent);
7924        else
7925          {
7926          compile_dnref_search(common, cc, parent->top != NULL ? &parent->top->nextbacktracks : &parent->topbacktracks);
7927          compile_ref_matchingpath(common, cc, parent->top != NULL ? &parent->top->nextbacktracks : &parent->topbacktracks, TRUE, FALSE);
7928          cc += 1 + 2 * IMM2_SIZE;
7929          }
7930      break;      break;
7931    
7932      case OP_RECURSE:      case OP_RECURSE:
# Line 7210  while (cc < ccend) Line 7961  while (cc < ccend)
7961        }        }
7962      BACKTRACK_AS(braminzero_backtrack)->matchingpath = LABEL();      BACKTRACK_AS(braminzero_backtrack)->matchingpath = LABEL();
7963      if (cc[1] > OP_ASSERTBACK_NOT)      if (cc[1] > OP_ASSERTBACK_NOT)
7964        decrease_call_count(common);        count_match(common);
7965      break;      break;
7966    
7967      case OP_ONCE:      case OP_ONCE:
# Line 7246  while (cc < ccend) Line 7997  while (cc < ccend)
7997      PUSH_BACKTRACK_NOVALUE(sizeof(backtrack_common), cc);      PUSH_BACKTRACK_NOVALUE(sizeof(backtrack_common), cc);
7998      SLJIT_ASSERT(common->mark_ptr != 0);      SLJIT_ASSERT(common->mark_ptr != 0);
7999      OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), common->mark_ptr);      OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), common->mark_ptr);
8000      allocate_stack(common, 1);      allocate_stack(common, common->has_skip_arg ? 5 : 1);
8001      OP1(SLJIT_MOV, TMP1, 0, ARGUMENTS, 0);      OP1(SLJIT_MOV, TMP1, 0, ARGUMENTS, 0);
8002      OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), TMP2, 0);      OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(common->has_skip_arg ? 4 : 0), TMP2, 0);
8003      OP1(SLJIT_MOV, TMP2, 0, SLJIT_IMM, (sljit_sw)(cc + 2));      OP1(SLJIT_MOV, TMP2, 0, SLJIT_IMM, (sljit_sw)(cc + 2));
8004      OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), common->mark_ptr, TMP2, 0);      OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), common->mark_ptr, TMP2, 0);
8005      OP1(SLJIT_MOV, SLJIT_MEM1(TMP1), SLJIT_OFFSETOF(jit_arguments, mark_ptr), TMP2, 0);      OP1(SLJIT_MOV, SLJIT_MEM1(TMP1), SLJIT_OFFSETOF(jit_arguments, mark_ptr), TMP2, 0);
8006        if (common->has_skip_arg)
8007          {
8008          OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), common->control_head_ptr);
8009          OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), common->control_head_ptr, STACK_TOP, 0);
8010          OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(1), SLJIT_IMM, type_mark);
8011          OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(2), SLJIT_IMM, (sljit_sw)(cc + 2));
8012          OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(3), STR_PTR, 0);
8013          OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), TMP1, 0);
8014          }
8015      cc += 1 + 2 + cc[1];      cc += 1 + 2 + cc[1];
8016      break;      break;
8017    
     case OP_PRUNE_ARG:  
     OP1(SLJIT_MOV, TMP1, 0, ARGUMENTS, 0);  
     OP1(SLJIT_MOV, TMP2, 0, SLJIT_IMM, (sljit_sw)(cc + 2));  
     OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), common->mark_ptr, TMP2, 0);  
     OP1(SLJIT_MOV, SLJIT_MEM1(TMP1), SLJIT_OFFSETOF(jit_arguments, mark_ptr), TMP2, 0);  
     /* Fall through. */  
   
8018      case OP_PRUNE:      case OP_PRUNE:
8019      case OP_COMMIT:      case OP_PRUNE_ARG:
     PUSH_BACKTRACK_NOVALUE(sizeof(backtrack_common), cc);  
     SLJIT_ASSERT(common->control_head_ptr != 0);  
     OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), common->control_head_ptr);  
     allocate_stack(common, 2);  
     OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), common->control_head_ptr, STACK_TOP, 0);  
     OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(1), SLJIT_IMM, *cc == OP_COMMIT ? type_commit : type_prune);  
     OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), TMP2, 0);  
     cc += (*cc == OP_PRUNE_ARG) ? (1 + 2 + cc[1]) : 1;  
     break;  
   
8020      case OP_SKIP:      case OP_SKIP:
8021      PUSH_BACKTRACK_NOVALUE(sizeof(backtrack_common), cc);      case OP_SKIP_ARG:
8022      OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), common->control_head_ptr);      case OP_THEN:
8023      allocate_stack(common, 3);      case OP_THEN_ARG:
8024      OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), common->control_head_ptr, STACK_TOP, 0);      case OP_COMMIT:
8025      OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(1), SLJIT_IMM, type_skip);      cc = compile_control_verb_matchingpath(common, cc, parent);
     OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(2), STR_PTR, 0);  
     OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), TMP2, 0);  
     cc += 1;  
8026      break;      break;
8027    
8028      case OP_FAIL:      case OP_FAIL:
# Line 7306  while (cc < ccend) Line 8046  while (cc < ccend)
8046    if (cc == NULL)    if (cc == NULL)
8047      return;      return;
8048    }    }
8049    
8050    if (has_then_trap)
8051      {
8052      /* Head item on backtrack. */
8053      PUSH_BACKTRACK_NOVALUE(sizeof(then_trap_backtrack), cc);
8054      BACKTRACK_AS(then_trap_backtrack)->common.cc = then_trap_opcode;
8055      BACKTRACK_AS(then_trap_backtrack)->then_trap = common->then_trap;
8056      common->then_trap = save_then_trap;
8057      }
8058  SLJIT_ASSERT(cc == ccend);  SLJIT_ASSERT(cc == ccend);
8059  }  }
8060    
# Line 7330  DEFINE_COMPILER; Line 8079  DEFINE_COMPILER;
8079  pcre_uchar *cc = current->cc;  pcre_uchar *cc = current->cc;
8080  pcre_uchar opcode;  pcre_uchar opcode;
8081  pcre_uchar type;  pcre_uchar type;
8082  int arg1 = -1, arg2 = -1;  int max = -1, min = -1;
8083  struct sljit_label *label = NULL;  struct sljit_label *label = NULL;
8084  struct sljit_jump *jump = NULL;  struct sljit_jump *jump = NULL;
8085  jump_list *jumplist = NULL;  jump_list *jumplist = NULL;
# Line 7339  int base = (private_data_ptr == 0) ? SLJ Line 8088  int base = (private_data_ptr == 0) ? SLJ
8088  int offset0 = (private_data_ptr == 0) ? STACK(0) : private_data_ptr;  int offset0 = (private_data_ptr == 0) ? STACK(0) : private_data_ptr;
8089  int offset1 = (private_data_ptr == 0) ? STACK(1) : private_data_ptr + (int)sizeof(sljit_sw);  int offset1 = (private_data_ptr == 0) ? STACK(1) : private_data_ptr + (int)sizeof(sljit_sw);
8090    
8091  cc = get_iterator_parameters(common, cc, &opcode, &type, &arg1, &arg2, NULL);  cc = get_iterator_parameters(common, cc, &opcode, &type, &max, &min, NULL);
8092    
8093  switch(opcode)  switch(opcode)
8094    {    {
# Line 7358  switch(opcode) Line 8107  switch(opcode)
8107    else    else
8108      {      {
8109      if (opcode == OP_UPTO)      if (opcode == OP_UPTO)
8110        arg2 = 0;        min = 0;
8111      if (opcode <= OP_PLUS)      if (opcode <= OP_PLUS)
8112        {        {
8113        OP1(SLJIT_MOV, STR_PTR, 0, base, offset0);        OP1(SLJIT_MOV, STR_PTR, 0, base, offset0);
# Line 7368  switch(opcode) Line 8117  switch(opcode)
8117        {        {
8118        OP1(SLJIT_MOV, TMP1, 0, base, offset1);        OP1(SLJIT_MOV, TMP1, 0, base, offset1);
8119        OP1(SLJIT_MOV, STR_PTR, 0, base, offset0);        OP1(SLJIT_MOV, STR_PTR, 0, base, offset0);
8120        jump = CMP(SLJIT_C_LESS_EQUAL, TMP1, 0, SLJIT_IMM, arg2 + 1);        jump = CMP(SLJIT_C_LESS_EQUAL, TMP1, 0, SLJIT_IMM, min + 1);
8121        OP2(SLJIT_SUB, base, offset1, TMP1, 0, SLJIT_IMM, 1);        OP2(SLJIT_SUB, base, offset1, TMP1, 0, SLJIT_IMM, 1);
8122        }        }
8123      skip_char_back(common);      skip_char_back(common);
# Line 7413  switch(opcode) Line 8162  switch(opcode)
8162    OP1(SLJIT_MOV, base, offset1, TMP1, 0);    OP1(SLJIT_MOV, base, offset1, TMP1, 0);
8163    
8164    if (opcode == OP_CRMINRANGE)    if (opcode == OP_CRMINRANGE)
8165      CMPTO(SLJIT_C_LESS, TMP1, 0, SLJIT_IMM, arg2 + 1, label);      CMPTO(SLJIT_C_LESS, TMP1, 0, SLJIT_IMM, min + 1, label);
8166    
8167    if (opcode == OP_CRMINRANGE && arg1 == 0)    if (opcode == OP_CRMINRANGE && max == 0)
8168      JUMPTO(SLJIT_JUMP, CURRENT_AS(iterator_backtrack)->matchingpath);      JUMPTO(SLJIT_JUMP, CURRENT_AS(iterator_backtrack)->matchingpath);
8169    else    else
8170      CMPTO(SLJIT_C_LESS, TMP1, 0, SLJIT_IMM, arg1 + 2, CURRENT_AS(iterator_backtrack)->matchingpath);      CMPTO(SLJIT_C_LESS, TMP1, 0, SLJIT_IMM, max + 2, CURRENT_AS(iterator_backtrack)->matchingpath);
8171    
8172    set_jumps(jumplist, LABEL());    set_jumps(jumplist, LABEL());
8173    if (private_data_ptr == 0)    if (private_data_ptr == 0)
# Line 7453  switch(opcode) Line 8202  switch(opcode)
8202    
8203    case OP_EXACT:    case OP_EXACT:
8204    case OP_POSPLUS:    case OP_POSPLUS:
8205      case OP_CRPOSRANGE:
8206    set_jumps(current->topbacktracks, LABEL());    set_jumps(current->topbacktracks, LABEL());
8207    break;    break;
8208    
# Line 7467  switch(opcode) Line 8217  switch(opcode)
8217    }    }
8218  }  }
8219    
8220  static void compile_ref_iterator_backtrackingpath(compiler_common *common, struct backtrack_common *current)  static SLJIT_INLINE void compile_ref_iterator_backtrackingpath(compiler_common *common, struct backtrack_common *current)
8221  {  {
8222  DEFINE_COMPILER;  DEFINE_COMPILER;
8223  pcre_uchar *cc = current->cc;  pcre_uchar *cc = current->cc;
8224    BOOL ref = (*cc == OP_REF || *cc == OP_REFI);
8225  pcre_uchar type;  pcre_uchar type;
8226    
8227  type = cc[1 + IMM2_SIZE];  type = cc[ref ? 1 + IMM2_SIZE : 1 + 2 * IMM2_SIZE];
8228    
8229  if ((type & 0x1) == 0)  if ((type & 0x1) == 0)
8230    {    {
8231      /* Maximize case. */
8232    set_jumps(current->topbacktracks, LABEL());    set_jumps(current->topbacktracks, LABEL());
8233    OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(STACK_TOP), STACK(0));    OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(STACK_TOP), STACK(0));
8234    free_stack(common, 1);    free_stack(common, 1);
# Line 7486  if ((type & 0x1) == 0) Line 8239  if ((type & 0x1) == 0)
8239  OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(STACK_TOP), STACK(0));  OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(STACK_TOP), STACK(0));
8240  CMPTO(SLJIT_C_NOT_EQUAL, STR_PTR, 0, SLJIT_IMM, 0, CURRENT_AS(iterator_backtrack)->matchingpath);  CMPTO(SLJIT_C_NOT_EQUAL, STR_PTR, 0, SLJIT_IMM, 0, CURRENT_AS(iterator_backtrack)->matchingpath);
8241  set_jumps(current->topbacktracks, LABEL());  set_jumps(current->topbacktracks, LABEL());
8242  free_stack(common, 2);  free_stack(common, ref ? 2 : 3);
8243  }  }
8244    
8245  static void compile_recurse_backtrackingpath(compiler_common *common, struct backtrack_common *current)  static SLJIT_INLINE void compile_recurse_backtrackingpath(compiler_common *common, struct backtrack_common *current)
8246  {  {
8247  DEFINE_COMPILER;  DEFINE_COMPILER;
8248    
# Line 7585  if (bra == OP_BRAZERO) Line 8338  if (bra == OP_BRAZERO)
8338  static void compile_bracket_backtrackingpath(compiler_common *common, struct backtrack_common *current)  static void compile_bracket_backtrackingpath(compiler_common *common, struct backtrack_common *current)
8339  {  {
8340  DEFINE_COMPILER;  DEFINE_COMPILER;
8341  int opcode;  int opcode, stacksize, count;
8342  int offset = 0;  int offset = 0;
8343  int private_data_ptr = CURRENT_AS(bracket_backtrack)->private_data_ptr;  int private_data_ptr = CURRENT_AS(bracket_backtrack)->private_data_ptr;
8344  int stacksize;  int repeat_ptr = 0, repeat_type = 0, repeat_count = 0;
 int count;  
8345  pcre_uchar *cc = current->cc;  pcre_uchar *cc = current->cc;
8346  pcre_uchar *ccbegin;  pcre_uchar *ccbegin;
8347  pcre_uchar *ccprev;  pcre_uchar *ccprev;
# Line 7599  pcre_uchar bra = OP_BRA; Line 8351  pcre_uchar bra = OP_BRA;
8351  pcre_uchar ket;  pcre_uchar ket;
8352  assert_backtrack *assert;  assert_backtrack *assert;
8353  BOOL has_alternatives;  BOOL has_alternatives;
8354    BOOL needs_control_head = FALSE;
8355  struct sljit_jump *brazero = NULL;  struct sljit_jump *brazero = NULL;
8356  struct sljit_jump *once = NULL;  struct sljit_jump *once = NULL;
8357  struct sljit_jump *cond = NULL;  struct sljit_jump *cond = NULL;
8358  struct sljit_label *rminlabel = NULL;  struct sljit_label *rmin_label = NULL;
8359    struct sljit_label *exact_label = NULL;
8360    
8361  if (*cc == OP_BRAZERO || *cc == OP_BRAMINZERO)  if (*cc == OP_BRAZERO || *cc == OP_BRAMINZERO)
8362    {    {
# Line 7611  if (*cc == OP_BRAZERO || *cc == OP_BRAMI Line 8365  if (*cc == OP_BRAZERO || *cc == OP_BRAMI
8365    }    }
8366    
8367  opcode = *cc;  opcode = *cc;
8368    ccbegin = bracketend(cc) - 1 - LINK_SIZE;
8369    ket = *ccbegin;
8370    if (ket == OP_KET && PRIVATE_DATA(ccbegin) != 0)
8371      {
8372      repeat_ptr = PRIVATE_DATA(ccbegin);
8373      repeat_type = PRIVATE_DATA(ccbegin + 2);
8374      repeat_count = PRIVATE_DATA(ccbegin + 3);
8375      SLJIT_ASSERT(repeat_type != 0 && repeat_count != 0);
8376      if (repeat_type == OP_UPTO)
8377        ket = OP_KETRMAX;
8378      if (repeat_type == OP_MINUPTO)
8379        ket = OP_KETRMIN;
8380      }
8381  ccbegin = cc;  ccbegin = cc;
 ket = *(bracketend(ccbegin) - 1 - LINK_SIZE);  
8382  cc += GET(cc, 1);  cc += GET(cc, 1);
8383  has_alternatives = *cc == OP_ALT;  has_alternatives = *cc == OP_ALT;
8384  if (SLJIT_UNLIKELY(opcode == OP_COND) || SLJIT_UNLIKELY(opcode == OP_SCOND))  if (SLJIT_UNLIKELY(opcode == OP_COND) || SLJIT_UNLIKELY(opcode == OP_SCOND))
# Line 7624  if (SLJIT_UNLIKELY(opcode == OP_COND) && Line 8390  if (SLJIT_UNLIKELY(opcode == OP_COND) &&
8390  if (SLJIT_UNLIKELY(opcode == OP_ONCE_NC))  if (SLJIT_UNLIKELY(opcode == OP_ONCE_NC))
8391    opcode = OP_ONCE;    opcode = OP_ONCE;
8392    
8393    /* Decoding the needs_control_head in framesize. */
8394    if (opcode == OP_ONCE)
8395      {
8396      needs_control_head = (CURRENT_AS(bracket_backtrack)->u.framesize & 0x1) != 0;
8397      CURRENT_AS(bracket_backtrack)->u.framesize >>= 1;
8398      }
8399    
8400    if (ket != OP_KET && repeat_type != 0)
8401      {
8402      /* TMP1 is used in OP_KETRMIN below. */
8403      OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(STACK_TOP), STACK(0));
8404      free_stack(common, 1);
8405      if (repeat_type == OP_UPTO)
8406        OP2(SLJIT_ADD, SLJIT_MEM1(SLJIT_LOCALS_REG), repeat_ptr, TMP1, 0, SLJIT_IMM, 1);
8407      else
8408        OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), repeat_ptr, TMP1, 0);
8409      }
8410    
8411  if (ket == OP_KETRMAX)  if (ket == OP_KETRMAX)
8412    {    {
8413    if (bra == OP_BRAZERO)    if (bra == OP_BRAZERO)
# Line 7638  else if (ket == OP_KETRMIN) Line 8422  else if (ket == OP_KETRMIN)
8422    if (bra != OP_BRAMINZERO)    if (bra != OP_BRAMINZERO)
8423      {      {
8424      OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(STACK_TOP), STACK(0));      OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(STACK_TOP), STACK(0));
8425      if (opcode >= OP_SBRA || opcode == OP_ONCE)      if (repeat_type != 0)
8426          {
8427          /* TMP1 was set a few lines above. */
8428          CMPTO(SLJIT_C_NOT_EQUAL, TMP1, 0, SLJIT_IMM, 0, CURRENT_AS(bracket_backtrack)->recursive_matchingpath);
8429          /* Drop STR_PTR for non-greedy plus quantifier. */
8430          if (opcode != OP_ONCE)
8431            free_stack(common, 1);
8432          }
8433        else if (opcode >= OP_SBRA || opcode == OP_ONCE)
8434        {        {
8435        /* Checking zero-length iteration. */        /* Checking zero-length iteration. */
8436        if (opcode != OP_ONCE || CURRENT_AS(bracket_backtrack)->u.framesize < 0)        if (opcode != OP_ONCE || CURRENT_AS(bracket_backtrack)->u.framesize < 0)
# Line 7648  else if (ket == OP_KETRMIN) Line 8440  else if (ket == OP_KETRMIN)
8440          OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), private_data_ptr);          OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), private_data_ptr);
8441          CMPTO(SLJIT_C_NOT_EQUAL, STR_PTR, 0, SLJIT_MEM1(TMP1), (CURRENT_AS(bracket_backtrack)->u.framesize + 1) * sizeof(sljit_sw), CURRENT_AS(bracket_backtrack)->recursive_matchingpath);          CMPTO(SLJIT_C_NOT_EQUAL, STR_PTR, 0, SLJIT_MEM1(TMP1), (CURRENT_AS(bracket_backtrack)->u.framesize + 1) * sizeof(sljit_sw), CURRENT_AS(bracket_backtrack)->recursive_matchingpath);
8442          }          }
8443          /* Drop STR_PTR for non-greedy plus quantifier. */
8444        if (opcode != OP_ONCE)        if (opcode != OP_ONCE)
8445          free_stack(common, 1);          free_stack(common, 1);
8446        }        }
8447      else      else
8448        JUMPTO(SLJIT_JUMP, CURRENT_AS(bracket_backtrack)->recursive_matchingpath);        JUMPTO(SLJIT_JUMP, CURRENT_AS(bracket_backtrack)->recursive_matchingpath);
8449      }      }
8450    rminlabel = LABEL();    rmin_label = LABEL();
8451      if (repeat_type != 0)
8452        OP2(SLJIT_ADD, SLJIT_MEM1(SLJIT_LOCALS_REG), repeat_ptr, SLJIT_MEM1(SLJIT_LOCALS_REG), repeat_ptr, SLJIT_IMM, 1);
8453    }    }
8454  else if (bra == OP_BRAZERO)  else if (bra == OP_BRAZERO)
8455    {    {
# Line 7662  else if (bra == OP_BRAZERO) Line 8457  else if (bra == OP_BRAZERO)
8457    free_stack(common, 1);    free_stack(common, 1);
8458    brazero = CMP(SLJIT_C_NOT_EQUAL, TMP1, 0, SLJIT_IMM, 0);    brazero = CMP(SLJIT_C_NOT_EQUAL, TMP1, 0, SLJIT_IMM, 0);
8459    }    }
8460    else if (repeat_type == OP_EXACT)
8461      {
8462      OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), repeat_ptr, SLJIT_IMM, 1);
8463      exact_label = LABEL();
8464      }
8465    
8466  if (offset != 0)  if (offset != 0)
8467    {    {
# Line 7781  if (has_alternatives) Line 8581  if (has_alternatives)
8581      current->top = NULL;      current->top = NULL;
8582      current->topbacktracks = NULL;      current->topbacktracks = NULL;
8583      current->nextbacktracks = NULL;      current->nextbacktracks = NULL;
8584        /* Conditional blocks always have an additional alternative, even if it is empty. */
8585      if (*cc == OP_ALT)      if (*cc == OP_ALT)
8586        {        {
8587        ccprev = cc + 1 + LINK_SIZE;        ccprev = cc + 1 + LINK_SIZE;
8588        cc += GET(cc, 1);        cc += GET(cc, 1);
8589        if (opcode != OP_COND && opcode != OP_SCOND)        if (opcode != OP_COND && opcode != OP_SCOND)
8590          {          {
8591          if (private_data_ptr != 0 && opcode != OP_ONCE)          if (opcode != OP_ONCE)
8592            OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), private_data_ptr);            {
8593              if (private_data_ptr != 0)
8594                OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), private_data_ptr);
8595              else
8596                OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(STACK_TOP), STACK(0));
8597              }
8598          else          else
8599            OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(STACK_TOP), STACK(0));            OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(STACK_TOP), STACK(needs_control_head ? 1 : 0));
8600          }          }
8601        compile_matchingpath(common, ccprev, cc, current);        compile_matchingpath(common, ccprev, cc, current);
8602        if (SLJIT_UNLIKELY(sljit_get_compiler_