/[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 1379 by ph10, Mon Oct 14 13:54:07 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 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 6  #define MAX_RANGE_SIZE 6
310    
311  typedef struct compiler_common {  typedef struct compiler_common {
# 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 509  return cc; Line 535  return cc;
535    
536  /* Functions whose might need modification for all new supported opcodes:  /* Functions whose might need modification for all new supported opcodes:
537   next_opcode   next_opcode
538   get_private_data_length   check_opcode_types
539   set_private_data_ptrs   set_private_data_ptrs
540   get_framesize   get_framesize
541   init_frame   init_frame
542   get_private_data_length_for_copy   get_private_data_copy_length
543   copy_private_data   copy_private_data
544   compile_matchingpath   compile_matchingpath
545   compile_backtrackingpath   compile_backtrackingpath
# Line 559  switch(*cc) Line 585  switch(*cc)
585    case OP_CRMINQUERY:    case OP_CRMINQUERY:
586    case OP_CRRANGE:    case OP_CRRANGE:
587    case OP_CRMINRANGE:    case OP_CRMINRANGE:
588      case OP_CRPOSSTAR:
589      case OP_CRPOSPLUS:
590      case OP_CRPOSQUERY:
591      case OP_CRPOSRANGE:
592    case OP_CLASS:    case OP_CLASS:
593    case OP_NCLASS:    case OP_NCLASS:
594    case OP_REF:    case OP_REF:
595    case OP_REFI:    case OP_REFI:
596      case OP_DNREF:
597      case OP_DNREFI:
598    case OP_RECURSE:    case OP_RECURSE:
599    case OP_CALLOUT:    case OP_CALLOUT:
600    case OP_ALT:    case OP_ALT:
# Line 588  switch(*cc) Line 620  switch(*cc)
620    case OP_SCBRAPOS:    case OP_SCBRAPOS:
621    case OP_SCOND:    case OP_SCOND:
622    case OP_CREF:    case OP_CREF:
623    case OP_NCREF:    case OP_DNCREF:
624    case OP_RREF:    case OP_RREF:
625    case OP_NRREF:    case OP_DNRREF:
626    case OP_DEF:    case OP_DEF:
627    case OP_BRAZERO:    case OP_BRAZERO:
628    case OP_BRAMINZERO:    case OP_BRAMINZERO:
629    case OP_BRAPOSZERO:    case OP_BRAPOSZERO:
630    case OP_PRUNE:    case OP_PRUNE:
631    case OP_SKIP:    case OP_SKIP:
632      case OP_THEN:
633    case OP_COMMIT:    case OP_COMMIT:
634    case OP_FAIL:    case OP_FAIL:
635    case OP_ACCEPT:    case OP_ACCEPT:
# Line 696  switch(*cc) Line 729  switch(*cc)
729    
730    case OP_MARK:    case OP_MARK:
731    case OP_PRUNE_ARG:    case OP_PRUNE_ARG:
732      case OP_SKIP_ARG:
733      case OP_THEN_ARG:
734    return cc + 1 + 2 + cc[1];    return cc + 1 + 2 + cc[1];
735    
736    default:    default:
737      /* All opcodes are supported now! */
738      SLJIT_ASSERT_STOP();
739    return NULL;    return NULL;
740    }    }
741  }  }
742    
743  #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)  
 {  
 switch(*cc)  
   {  
   case OP_CRSTAR:  
   case OP_CRPLUS:  
   return 2;  
   
   case OP_CRMINSTAR:  
   case OP_CRMINPLUS:  
   case OP_CRQUERY:  
   case OP_CRMINQUERY:  
   return 1;  
   
   case OP_CRRANGE:  
   case OP_CRMINRANGE:  
   if (GET2(cc, 1) == GET2(cc, 1 + IMM2_SIZE))  
     return 0;  
   return 2;  
   
   default:  
   return 0;  
   }  
 }  
   
 static int get_private_data_length(compiler_common *common, pcre_uchar *cc, pcre_uchar *ccend)  
744  {  {
745  int private_data_length = 0;  int count;
746  pcre_uchar *alternative;  pcre_uchar *slot;
 pcre_uchar *name;  
 pcre_uchar *end = NULL;  
 int space, size, i;  
 pcre_uint32 bracketlen;  
747    
748  /* 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. */
749  while (cc < ccend)  while (cc < ccend)
750    {    {
   space = 0;  
   size = 0;  
   bracketlen = 0;  
751    switch(*cc)    switch(*cc)
752      {      {
753      case OP_SET_SOM:      case OP_SET_SOM:
# Line 808  while (cc < ccend) Line 761  while (cc < ccend)
761      cc += 1 + IMM2_SIZE;      cc += 1 + IMM2_SIZE;
762      break;      break;
763    
     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;  
   
764      case OP_CBRAPOS:      case OP_CBRAPOS:
765      case OP_SCBRAPOS:      case OP_SCBRAPOS:
     private_data_length += sizeof(sljit_sw);  
766      common->optimized_cbracket[GET2(cc, 1 + LINK_SIZE)] = 0;      common->optimized_cbracket[GET2(cc, 1 + LINK_SIZE)] = 0;
767      bracketlen = 1 + LINK_SIZE + IMM2_SIZE;      cc += 1 + LINK_SIZE + IMM2_SIZE;
768      break;      break;
769    
770      case OP_COND:      case OP_COND:
# Line 833  while (cc < ccend) Line 772  while (cc < ccend)
772      /* Only AUTO_CALLOUT can insert this opcode. We do      /* Only AUTO_CALLOUT can insert this opcode. We do
773         not intend to support this case. */         not intend to support this case. */
774      if (cc[1 + LINK_SIZE] == OP_CALLOUT)      if (cc[1 + LINK_SIZE] == OP_CALLOUT)
775        return -1;        return FALSE;
776        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;  
777      break;      break;
778    
779      case OP_CREF:      case OP_CREF:
780      i = GET2(cc, 1);      common->optimized_cbracket[GET2(cc, 1)] = 0;
     common->optimized_cbracket[i] = 0;  
781      cc += 1 + IMM2_SIZE;      cc += 1 + IMM2_SIZE;
782      break;      break;
783    
784      case OP_NCREF:      case OP_DNREF:
785      bracketlen = GET2(cc, 1);      case OP_DNREFI:
786      name = (pcre_uchar *)common->name_table;      case OP_DNCREF:
787      alternative = name;      count = GET2(cc, 1 + IMM2_SIZE);
788      for (i = 0; i < common->name_count; i++)      slot = common->name_table + GET2(cc, 1) * common->name_entry_size;
789        {      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++)  
790        {        {
791        if (STRCMP_UC_UC(alternative + IMM2_SIZE, name + IMM2_SIZE) == 0)        common->optimized_cbracket[GET2(slot, 0)] = 0;
792          common->optimized_cbracket[GET2(alternative, 0)] = 0;        slot += common->name_entry_size;
       alternative += common->name_entry_size;  
793        }        }
794      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;  
795      break;      break;
796    
     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);  
     break;  
 #endif  
   
797      case OP_RECURSE:      case OP_RECURSE:
798      /* Set its value only once. */      /* Set its value only once. */
799      if (common->recursive_head_ptr == 0)      if (common->recursive_head_ptr == 0)
# Line 947  while (cc < ccend) Line 813  while (cc < ccend)
813      cc += 2 + 2 * LINK_SIZE;      cc += 2 + 2 * LINK_SIZE;
814      break;      break;
815    
816        case OP_THEN_ARG:
817        common->has_then = TRUE;
818        common->control_head_ptr = 1;
819        /* Fall through. */
820    
821      case OP_PRUNE_ARG:      case OP_PRUNE_ARG:
822      common->needs_start_ptr = TRUE;      common->needs_start_ptr = TRUE;
     common->control_head_ptr = 1;  
823      /* Fall through. */      /* Fall through. */
824    
825      case OP_MARK:      case OP_MARK:
# Line 961  while (cc < ccend) Line 831  while (cc < ccend)
831      cc += 1 + 2 + cc[1];      cc += 1 + 2 + cc[1];
832      break;      break;
833    
834        case OP_THEN:
835        common->has_then = TRUE;
836        common->control_head_ptr = 1;
837        /* Fall through. */
838    
839      case OP_PRUNE:      case OP_PRUNE:
840      case OP_SKIP:      case OP_SKIP:
841      common->needs_start_ptr = TRUE;      common->needs_start_ptr = TRUE;
842      /* Fall through. */      cc += 1;
843        break;
844    
845      case OP_COMMIT:      case OP_SKIP_ARG:
846      common->control_head_ptr = 1;      common->control_head_ptr = 1;
847      cc += 1;      common->has_skip_arg = TRUE;
848        cc += 1 + 2 + cc[1];
849      break;      break;
850    
851      default:      default:
852      cc = next_opcode(common, cc);      cc = next_opcode(common, cc);
853      if (cc == NULL)      if (cc == NULL)
854        return -1;        return FALSE;
855      break;      break;
856      }      }
857      }
858    return TRUE;
859    }
860    
861    if (space > 0 && cc >= end)  static int get_class_iterator_size(pcre_uchar *cc)
862      private_data_length += sizeof(sljit_sw) * space;  {
863    switch(*cc)
864      {
865      case OP_CRSTAR:
866      case OP_CRPLUS:
867      return 2;
868    
869    if (size != 0)    case OP_CRMINSTAR:
870      case OP_CRMINPLUS:
871      case OP_CRQUERY:
872      case OP_CRMINQUERY:
873      return 1;
874    
875      case OP_CRRANGE:
876      case OP_CRMINRANGE:
877      if (GET2(cc, 1) == GET2(cc, 1 + IMM2_SIZE))
878        return 0;
879      return 2;
880    
881      default:
882      return 0;
883      }
884    }
885    
886    static BOOL detect_repeat(compiler_common *common, pcre_uchar *begin)
887    {
888    pcre_uchar *end = bracketend(begin);
889    pcre_uchar *next;
890    pcre_uchar *next_end;
891    pcre_uchar *max_end;
892    pcre_uchar type;
893    sljit_sw length = end - begin;
894    int min, max, i;
895    
896    /* Detect fixed iterations first. */
897    if (end[-(1 + LINK_SIZE)] != OP_KET)
898      return FALSE;
899    
900    /* Already detected repeat. */
901    if (common->private_data_ptrs[end - common->start - LINK_SIZE] != 0)
902      return TRUE;
903    
904    next = end;
905    min = 1;
906    while (1)
907      {
908      if (*next != *begin)
909        break;
910      next_end = bracketend(next);
911      if (next_end - next != length || memcmp(begin, next, IN_UCHARS(length)) != 0)
912        break;
913      next = next_end;
914      min++;
915      }
916    
917    if (min == 2)
918      return FALSE;
919    
920    max = 0;
921    max_end = next;
922    if (*next == OP_BRAZERO || *next == OP_BRAMINZERO)
923      {
924      type = *next;
925      while (1)
926      {      {
927      if (size < 0)      if (next[0] != type || next[1] != OP_BRA || next[2 + LINK_SIZE] != *begin)
928        {        break;
929        cc += -size;      next_end = bracketend(next + 2 + LINK_SIZE);
930  #ifdef SUPPORT_UTF      if (next_end - next != (length + 2 + LINK_SIZE) || memcmp(begin, next + 2 + LINK_SIZE, IN_UCHARS(length)) != 0)
931        if (common->utf && HAS_EXTRALEN(cc[-1])) cc += GET_EXTRALEN(cc[-1]);        break;
932  #endif      next = next_end;
933        }      max++;
     else  
       cc += size;  
934      }      }
935    
936    if (bracketlen != 0)    if (next[0] == type && next[1] == *begin && max >= 1)
937      {      {
938      if (cc >= end)      next_end = bracketend(next + 1);
939        if (next_end - next == (length + 1) && memcmp(begin, next + 1, IN_UCHARS(length)) == 0)
940        {        {
941        end = bracketend(cc);        for (i = 0; i < max; i++, next_end += 1 + LINK_SIZE)
942        if (end[-1 - LINK_SIZE] == OP_KET)          if (*next_end != OP_KET)
943          end = NULL;            break;
944    
945          if (i == max)
946            {
947            common->private_data_ptrs[max_end - common->start - LINK_SIZE] = next_end - max_end;
948            common->private_data_ptrs[max_end - common->start - LINK_SIZE + 1] = (type == OP_BRAZERO) ? OP_UPTO : OP_MINUPTO;
949            /* +2 the original and the last. */
950            common->private_data_ptrs[max_end - common->start - LINK_SIZE + 2] = max + 2;
951            if (min == 1)
952              return TRUE;
953            min--;
954            max_end -= (1 + LINK_SIZE) + GET(max_end, -LINK_SIZE);
955            }
956        }        }
     cc += bracketlen;  
957      }      }
958    }    }
959  return private_data_length;  
960    if (min >= 3)
961      {
962      common->private_data_ptrs[end - common->start - LINK_SIZE] = max_end - end;
963      common->private_data_ptrs[end - common->start - LINK_SIZE + 1] = OP_EXACT;
964      common->private_data_ptrs[end - common->start - LINK_SIZE + 2] = min;
965      return TRUE;
966      }
967    
968    return FALSE;
969  }  }
970    
971  static void set_private_data_ptrs(compiler_common *common, int private_data_ptr, pcre_uchar *ccend)  #define CASE_ITERATOR_PRIVATE_DATA_1 \
972        case OP_MINSTAR: \
973        case OP_MINPLUS: \
974        case OP_QUERY: \
975        case OP_MINQUERY: \
976        case OP_MINSTARI: \
977        case OP_MINPLUSI: \
978        case OP_QUERYI: \
979        case OP_MINQUERYI: \
980        case OP_NOTMINSTAR: \
981        case OP_NOTMINPLUS: \
982        case OP_NOTQUERY: \
983        case OP_NOTMINQUERY: \
984        case OP_NOTMINSTARI: \
985        case OP_NOTMINPLUSI: \
986        case OP_NOTQUERYI: \
987        case OP_NOTMINQUERYI:
988    
989    #define CASE_ITERATOR_PRIVATE_DATA_2A \
990        case OP_STAR: \
991        case OP_PLUS: \
992        case OP_STARI: \
993        case OP_PLUSI: \
994        case OP_NOTSTAR: \
995        case OP_NOTPLUS: \
996        case OP_NOTSTARI: \
997        case OP_NOTPLUSI:
998    
999    #define CASE_ITERATOR_PRIVATE_DATA_2B \
1000        case OP_UPTO: \
1001        case OP_MINUPTO: \
1002        case OP_UPTOI: \
1003        case OP_MINUPTOI: \
1004        case OP_NOTUPTO: \
1005        case OP_NOTMINUPTO: \
1006        case OP_NOTUPTOI: \
1007        case OP_NOTMINUPTOI:
1008    
1009    #define CASE_ITERATOR_TYPE_PRIVATE_DATA_1 \
1010        case OP_TYPEMINSTAR: \
1011        case OP_TYPEMINPLUS: \
1012        case OP_TYPEQUERY: \
1013        case OP_TYPEMINQUERY:
1014    
1015    #define CASE_ITERATOR_TYPE_PRIVATE_DATA_2A \
1016        case OP_TYPESTAR: \
1017        case OP_TYPEPLUS:
1018    
1019    #define CASE_ITERATOR_TYPE_PRIVATE_DATA_2B \
1020        case OP_TYPEUPTO: \
1021        case OP_TYPEMINUPTO:
1022    
1023    static void set_private_data_ptrs(compiler_common *common, int *private_data_start, pcre_uchar *ccend)
1024  {  {
1025  pcre_uchar *cc = common->start;  pcre_uchar *cc = common->start;
1026  pcre_uchar *alternative;  pcre_uchar *alternative;
1027  pcre_uchar *end = NULL;  pcre_uchar *end = NULL;
1028    int private_data_ptr = *private_data_start;
1029  int space, size, bracketlen;  int space, size, bracketlen;
1030    
1031  while (cc < ccend)  while (cc < ccend)
# Line 1020  while (cc < ccend) Line 1033  while (cc < ccend)
1033    space = 0;    space = 0;
1034    size = 0;    size = 0;
1035    bracketlen = 0;    bracketlen = 0;
1036      if (private_data_ptr > SLJIT_MAX_LOCAL_SIZE)
1037        return;
1038    
1039      if (*cc == OP_ONCE || *cc == OP_ONCE_NC || *cc == OP_BRA || *cc == OP_CBRA || *cc == OP_COND)
1040        if (detect_repeat(common, cc))
1041          {
1042          /* These brackets are converted to repeats, so no global
1043          based single character repeat is allowed. */
1044          if (cc >= end)
1045            end = bracketend(cc);
1046          }
1047    
1048    switch(*cc)    switch(*cc)
1049      {      {
1050        case OP_KET:
1051        if (common->private_data_ptrs[cc + 1 - common->start] != 0)
1052          {
1053          common->private_data_ptrs[cc - common->start] = private_data_ptr;
1054          private_data_ptr += sizeof(sljit_sw);
1055          cc += common->private_data_ptrs[cc + 1 - common->start];
1056          }
1057        cc += 1 + LINK_SIZE;
1058        break;
1059    
1060      case OP_ASSERT:      case OP_ASSERT:
1061      case OP_ASSERT_NOT:      case OP_ASSERT_NOT:
1062      case OP_ASSERTBACK:      case OP_ASSERTBACK:
# Line 1115  while (cc < ccend) Line 1150  while (cc < ccend)
1150      break;      break;
1151      }      }
1152    
1153      /* Character iterators, which are not inside a repeated bracket,
1154         gets a private slot instead of allocating it on the stack. */
1155    if (space > 0 && cc >= end)    if (space > 0 && cc >= end)
1156      {      {
1157      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 1182  while (cc < ccend)
1182      cc += bracketlen;      cc += bracketlen;
1183      }      }
1184    }    }
1185    *private_data_start = private_data_ptr;
1186  }  }
1187    
1188  /* Returns with a frame_types (always < 0) if no need for frame. */  /* Returns with a frame_types (always < 0) if no need for frame. */
1189  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)
1190  {  {
 pcre_uchar *ccend = bracketend(cc) - (1 + LINK_SIZE);  
1191  int length = 0;  int length = 0;
1192  int possessive = 0;  int possessive = 0;
1193  BOOL stack_restore = FALSE;  BOOL stack_restore = FALSE;
# Line 1159  BOOL setmark_found = recursive; Line 1196  BOOL setmark_found = recursive;
1196  /* The last capture is a local variable even for recursions. */  /* The last capture is a local variable even for recursions. */
1197  BOOL capture_last_found = FALSE;  BOOL capture_last_found = FALSE;
1198    
1199  if (!recursive && (*cc == OP_CBRAPOS || *cc == OP_SCBRAPOS))  #if defined DEBUG_FORCE_CONTROL_HEAD && DEBUG_FORCE_CONTROL_HEAD
1200    SLJIT_ASSERT(common->control_head_ptr != 0);
1201    *needs_control_head = TRUE;
1202    #else
1203    *needs_control_head = FALSE;
1204    #endif
1205    
1206    if (ccend == NULL)
1207    {    {
1208    possessive = length = (common->capture_last_ptr != 0) ? 5 : 3;    ccend = bracketend(cc) - (1 + LINK_SIZE);
1209    /* This is correct regardless of common->capture_last_ptr. */    if (!recursive && (*cc == OP_CBRAPOS || *cc == OP_SCBRAPOS))
1210    capture_last_found = TRUE;      {
1211        possessive = length = (common->capture_last_ptr != 0) ? 5 : 3;
1212        /* This is correct regardless of common->capture_last_ptr. */
1213        capture_last_found = TRUE;
1214        }
1215      cc = next_opcode(common, cc);
1216    }    }
1217    
 cc = next_opcode(common, cc);  
1218  SLJIT_ASSERT(cc != NULL);  SLJIT_ASSERT(cc != NULL);
1219  while (cc < ccend)  while (cc < ccend)
1220    switch(*cc)    switch(*cc)
# Line 1184  while (cc < ccend) Line 1232  while (cc < ccend)
1232    
1233      case OP_MARK:      case OP_MARK:
1234      case OP_PRUNE_ARG:      case OP_PRUNE_ARG:
1235        case OP_THEN_ARG:
1236      SLJIT_ASSERT(common->mark_ptr != 0);      SLJIT_ASSERT(common->mark_ptr != 0);
1237      stack_restore = TRUE;      stack_restore = TRUE;
1238      if (!setmark_found)      if (!setmark_found)
# Line 1191  while (cc < ccend) Line 1240  while (cc < ccend)
1240        length += 2;        length += 2;
1241        setmark_found = TRUE;        setmark_found = TRUE;
1242        }        }
1243        if (common->control_head_ptr != 0)
1244          *needs_control_head = TRUE;
1245      cc += 1 + 2 + cc[1];      cc += 1 + 2 + cc[1];
1246      break;      break;
1247    
# Line 1310  if (length > 0) Line 1361  if (length > 0)
1361  return stack_restore ? no_frame : no_stack;  return stack_restore ? no_frame : no_stack;
1362  }  }
1363    
1364  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)
1365  {  {
1366  DEFINE_COMPILER;  DEFINE_COMPILER;
 pcre_uchar *ccend = bracketend(cc) - (1 + LINK_SIZE);  
1367  BOOL setsom_found = recursive;  BOOL setsom_found = recursive;
1368  BOOL setmark_found = recursive;  BOOL setmark_found = recursive;
1369  /* 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 1375  SLJIT_UNUSED_ARG(stacktop);
1375  SLJIT_ASSERT(stackpos >= stacktop + 2);  SLJIT_ASSERT(stackpos >= stacktop + 2);
1376    
1377  stackpos = STACK(stackpos);  stackpos = STACK(stackpos);
1378  if (recursive || (*cc != OP_CBRAPOS && *cc != OP_SCBRAPOS))  if (ccend == NULL)
1379    cc = next_opcode(common, cc);    {
1380      ccend = bracketend(cc) - (1 + LINK_SIZE);
1381      if (recursive || (*cc != OP_CBRAPOS && *cc != OP_SCBRAPOS))
1382        cc = next_opcode(common, cc);
1383      }
1384    
1385  SLJIT_ASSERT(cc != NULL);  SLJIT_ASSERT(cc != NULL);
1386  while (cc < ccend)  while (cc < ccend)
1387    switch(*cc)    switch(*cc)
# Line 1347  while (cc < ccend) Line 1402  while (cc < ccend)
1402    
1403      case OP_MARK:      case OP_MARK:
1404      case OP_PRUNE_ARG:      case OP_PRUNE_ARG:
1405        case OP_THEN_ARG:
1406      SLJIT_ASSERT(common->mark_ptr != 0);      SLJIT_ASSERT(common->mark_ptr != 0);
1407      if (!setmark_found)      if (!setmark_found)
1408        {        {
# Line 1427  OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), st Line 1483  OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), st
1483  SLJIT_ASSERT(stackpos == STACK(stacktop));  SLJIT_ASSERT(stackpos == STACK(stacktop));
1484  }  }
1485    
1486  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)
1487  {  {
1488  int private_data_length = common->control_head_ptr ? 3 : 2;  int private_data_length = needs_control_head ? 3 : 2;
1489  int size;  int size;
1490  pcre_uchar *alternative;  pcre_uchar *alternative;
1491  /* Calculate the sum of the private machine words. */  /* Calculate the sum of the private machine words. */
# Line 1438  while (cc < ccend) Line 1494  while (cc < ccend)
1494    size = 0;    size = 0;
1495    switch(*cc)    switch(*cc)
1496      {      {
1497        case OP_KET:
1498        if (PRIVATE_DATA(cc) != 0)
1499          private_data_length++;
1500        cc += 1 + LINK_SIZE;
1501        break;
1502    
1503      case OP_ASSERT:      case OP_ASSERT:
1504      case OP_ASSERT_NOT:      case OP_ASSERT_NOT:
1505      case OP_ASSERTBACK:      case OP_ASSERTBACK:
# Line 1542  return private_data_length; Line 1604  return private_data_length;
1604  }  }
1605    
1606  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,
1607    BOOL save, int stackptr, int stacktop)    BOOL save, int stackptr, int stacktop, BOOL needs_control_head)
1608  {  {
1609  DEFINE_COMPILER;  DEFINE_COMPILER;
1610  int srcw[2];  int srcw[2];
# Line 1563  stacktop = STACK(stacktop - 1); Line 1625  stacktop = STACK(stacktop - 1);
1625    
1626  if (!save)  if (!save)
1627    {    {
1628    stackptr += (common->control_head_ptr ? 2 : 1) * sizeof(sljit_sw);    stackptr += (needs_control_head ? 2 : 1) * sizeof(sljit_sw);
1629    if (stackptr < stacktop)    if (stackptr < stacktop)
1630      {      {
1631      OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(STACK_TOP), stackptr);      OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(STACK_TOP), stackptr);
# Line 1588  do Line 1650  do
1650      SLJIT_ASSERT(save && common->recursive_head_ptr != 0);      SLJIT_ASSERT(save && common->recursive_head_ptr != 0);
1651      count = 1;      count = 1;
1652      srcw[0] = common->recursive_head_ptr;      srcw[0] = common->recursive_head_ptr;
1653      if (common->control_head_ptr != 0)      if (needs_control_head)
1654        {        {
1655          SLJIT_ASSERT(common->control_head_ptr != 0);
1656        count = 2;        count = 2;
1657        srcw[1] = common->control_head_ptr;        srcw[1] = common->control_head_ptr;
1658        }        }
# Line 1605  do Line 1668  do
1668    
1669      switch(*cc)      switch(*cc)
1670        {        {
1671          case OP_KET:
1672          if (PRIVATE_DATA(cc) != 0)
1673            {
1674            count = 1;
1675            srcw[0] = PRIVATE_DATA(cc);
1676            }
1677          cc += 1 + LINK_SIZE;
1678          break;
1679    
1680        case OP_ASSERT:        case OP_ASSERT:
1681        case OP_ASSERT_NOT:        case OP_ASSERT_NOT:
1682        case OP_ASSERTBACK:        case OP_ASSERTBACK:
# Line 1851  if (save) Line 1923  if (save)
1923  SLJIT_ASSERT(cc == ccend && stackptr == stacktop && (save || (tmp1empty && tmp2empty)));  SLJIT_ASSERT(cc == ccend && stackptr == stacktop && (save || (tmp1empty && tmp2empty)));
1924  }  }
1925    
1926    static SLJIT_INLINE pcre_uchar *set_then_offsets(compiler_common *common, pcre_uchar *cc, pcre_uint8 *current_offset)
1927    {
1928    pcre_uchar *end = bracketend(cc);
1929    BOOL has_alternatives = cc[GET(cc, 1)] == OP_ALT;
1930    
1931    /* Assert captures then. */
1932    if (*cc >= OP_ASSERT && *cc <= OP_ASSERTBACK_NOT)
1933      current_offset = NULL;
1934    /* Conditional block does not. */
1935    if (*cc == OP_COND || *cc == OP_SCOND)
1936      has_alternatives = FALSE;
1937    
1938    cc = next_opcode(common, cc);
1939    if (has_alternatives)
1940      current_offset = common->then_offsets + (cc - common->start);
1941    
1942    while (cc < end)
1943      {
1944      if ((*cc >= OP_ASSERT && *cc <= OP_ASSERTBACK_NOT) || (*cc >= OP_ONCE && *cc <= OP_SCOND))
1945        cc = set_then_offsets(common, cc, current_offset);
1946      else
1947        {
1948        if (*cc == OP_ALT && has_alternatives)
1949          current_offset = common->then_offsets + (cc + 1 + LINK_SIZE - common->start);
1950        if (*cc >= OP_THEN && *cc <= OP_THEN_ARG && current_offset != NULL)
1951          *current_offset = 1;
1952        cc = next_opcode(common, cc);
1953        }
1954      }
1955    
1956    return end;
1957    }
1958    
1959  #undef CASE_ITERATOR_PRIVATE_DATA_1  #undef CASE_ITERATOR_PRIVATE_DATA_1
1960  #undef CASE_ITERATOR_PRIVATE_DATA_2A  #undef CASE_ITERATOR_PRIVATE_DATA_2A
1961  #undef CASE_ITERATOR_PRIVATE_DATA_2B  #undef CASE_ITERATOR_PRIVATE_DATA_2B
# Line 1914  while (list_item) Line 2019  while (list_item)
2019  common->stubs = NULL;  common->stubs = NULL;
2020  }  }
2021    
2022  static SLJIT_INLINE void decrease_call_count(compiler_common *common)  static SLJIT_INLINE void count_match(compiler_common *common)
2023  {  {
2024  DEFINE_COMPILER;  DEFINE_COMPILER;
2025    
2026  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);
2027  add_jump(compiler, &common->calllimit, JUMP(SLJIT_C_ZERO));  add_jump(compiler, &common->calllimit, JUMP(SLJIT_C_ZERO));
2028  }  }
2029    
# Line 1998  else Line 2103  else
2103  OP1(SLJIT_MOV, STACK_TOP, 0, ARGUMENTS, 0);  OP1(SLJIT_MOV, STACK_TOP, 0, ARGUMENTS, 0);
2104  if (common->mark_ptr != 0)  if (common->mark_ptr != 0)
2105    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);
2106  SLJIT_ASSERT(common->control_head_ptr != 0);  if (common->control_head_ptr != 0)
2107  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);
2108  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));
2109  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);
2110  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));
2111  }  }
2112    
2113  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)
2114  {  {
2115  sljit_sw return_value = 0;  while (current != NULL)
   
 SLJIT_ASSERT(current != NULL);  
 do  
2116    {    {
2117    switch (current[-2])    switch (current[-2])
2118      {      {
2119      case type_commit:      case type_then_trap:
     /* Commit overwrites all. */  
     return -1;  
   
     case type_prune:  
2120      break;      break;
2121    
2122      case type_skip:      case type_mark:
2123      /* Overwrites prune, but not other skips. */      if (STRCMP_UC_UC(skip_arg, (pcre_uchar *)current[-3]) == 0)
2124      if (return_value == 0)        return current[-4];
       return_value = current[-3];  
2125      break;      break;
2126    
2127      default:      default:
# Line 2033  do Line 2130  do
2130      }      }
2131    current = (sljit_sw*)current[-1];    current = (sljit_sw*)current[-1];
2132    }    }
2133  while (current != NULL);  return -1;
 return return_value;  
2134  }  }
2135    
2136  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 2202  OP1(SLJIT_MOV, SLJIT_SAVED_REG1, 0, SLJI
2202  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));
2203    
2204  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);
2205  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);
2206  #if defined COMPILE_PCRE16 || defined COMPILE_PCRE32  #if defined COMPILE_PCRE16 || defined COMPILE_PCRE32
2207  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);
2208  #endif  #endif
# Line 2274  return (bit < 256) ? ((0 << 8) | bit) : Line 2370  return (bit < 256) ? ((0 << 8) | bit) :
2370    
2371  static void check_partial(compiler_common *common, BOOL force)  static void check_partial(compiler_common *common, BOOL force)
2372  {  {
2373  /* Checks whether a partial matching is occured. Does not modify registers. */  /* Checks whether a partial matching is occurred. Does not modify registers. */
2374  DEFINE_COMPILER;  DEFINE_COMPILER;
2375  struct sljit_jump *jump = NULL;  struct sljit_jump *jump = NULL;
2376    
# Line 4217  while (*cc != XCL_END) Line 4313  while (*cc != XCL_END)
4313    
4314        case PT_SPACE:        case PT_SPACE:
4315        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);  
         }  
4316        SET_CHAR_OFFSET(9);        SET_CHAR_OFFSET(9);
4317        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);
4318        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);
4319        if (*cc == PT_SPACE)  
4320          JUMPHERE(jump);        OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x85 - 0x9);
4321          OP_FLAGS(SLJIT_OR | SLJIT_SET_E, TMP2, 0, TMP2, 0, SLJIT_C_EQUAL);
4322    
4323          OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x180e - 0x9);
4324          OP_FLAGS(SLJIT_OR | SLJIT_SET_E, TMP2, 0, TMP2, 0, SLJIT_C_EQUAL);
4325    
4326        SET_TYPE_OFFSET(ucp_Zl);        SET_TYPE_OFFSET(ucp_Zl);
4327        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 4956  if (context.length > 0) Line 5051  if (context.length > 0)
5051  return compile_char1_matchingpath(common, *cc, cc + 1, backtracks);  return compile_char1_matchingpath(common, *cc, cc + 1, backtracks);
5052  }  }
5053    
 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));  
 }  
   
5054  /* Forward definitions. */  /* Forward definitions. */
5055  static void compile_matchingpath(compiler_common *, pcre_uchar *, pcre_uchar *, backtrack_common *);  static void compile_matchingpath(compiler_common *, pcre_uchar *, pcre_uchar *, backtrack_common *);
5056  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 5083  static void compile_backtrackingpath(com
5083    
5084  #define BACKTRACK_AS(type) ((type *)backtrack)  #define BACKTRACK_AS(type) ((type *)backtrack)
5085    
5086  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)
5087    {
5088    /* The OVECTOR offset goes to TMP2. */
5089    DEFINE_COMPILER;
5090    int count = GET2(cc, 1 + IMM2_SIZE);
5091    pcre_uchar *slot = common->name_table + GET2(cc, 1) * common->name_entry_size;
5092    unsigned int offset;
5093    jump_list *found = NULL;
5094    
5095    SLJIT_ASSERT(*cc == OP_DNREF || *cc == OP_DNREFI);
5096    
5097    OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(1));
5098    
5099    count--;
5100    while (count-- > 0)
5101      {
5102      offset = GET2(slot, 0) << 1;
5103      GET_LOCAL_BASE(TMP2, 0, OVECTOR(offset));
5104      add_jump(compiler, &found, CMP(SLJIT_C_NOT_EQUAL, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(offset), TMP1, 0));
5105      slot += common->name_entry_size;
5106      }
5107    
5108    offset = GET2(slot, 0) << 1;
5109    GET_LOCAL_BASE(TMP2, 0, OVECTOR(offset));
5110    if (backtracks != NULL && !common->jscript_compat)
5111      add_jump(compiler, backtracks, CMP(SLJIT_C_EQUAL, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(offset), TMP1, 0));
5112    
5113    set_jumps(found, LABEL());
5114    }
5115    
5116    static void compile_ref_matchingpath(compiler_common *common, pcre_uchar *cc, jump_list **backtracks, BOOL withchecks, BOOL emptyfail)
5117  {  {
5118  DEFINE_COMPILER;  DEFINE_COMPILER;
5119  int offset = GET2(cc, 1) << 1;  BOOL ref = (*cc == OP_REF || *cc == OP_REFI);
5120    int offset = 0;
5121  struct sljit_jump *jump = NULL;  struct sljit_jump *jump = NULL;
5122  struct sljit_jump *partial;  struct sljit_jump *partial;
5123  struct sljit_jump *nopartial;  struct sljit_jump *nopartial;
5124    
5125  OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(offset));  if (ref)
5126  /* OVECTOR(1) contains the "string begin - 1" constant. */    {
5127  if (withchecks && !common->jscript_compat)    offset = GET2(cc, 1) << 1;
5128    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));
5129      /* OVECTOR(1) contains the "string begin - 1" constant. */
5130      if (withchecks && !common->jscript_compat)
5131        add_jump(compiler, backtracks, CMP(SLJIT_C_EQUAL, TMP1, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(1)));
5132      }
5133    else
5134      OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(TMP2), 0);
5135    
5136  #if defined SUPPORT_UTF && defined SUPPORT_UCP  #if defined SUPPORT_UTF && defined SUPPORT_UCP
5137  if (common->utf && *cc == OP_REFI)  if (common->utf && *cc == OP_REFI)
5138    {    {
5139    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);
5140    OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(offset + 1));    if (ref)
5141        OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(offset + 1));
5142      else
5143        OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(TMP2), sizeof(sljit_sw));
5144    
5145    if (withchecks)    if (withchecks)
5146      jump = CMP(SLJIT_C_EQUAL, TMP1, 0, TMP2, 0);      jump = CMP(SLJIT_C_EQUAL, TMP1, 0, TMP2, 0);
5147    
# Line 5052  if (common->utf && *cc == OP_REFI) Line 5166  if (common->utf && *cc == OP_REFI)
5166  else  else
5167  #endif /* SUPPORT_UTF && SUPPORT_UCP */  #endif /* SUPPORT_UTF && SUPPORT_UCP */
5168    {    {
5169    OP2(SLJIT_SUB | SLJIT_SET_E, TMP2, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(offset + 1), TMP1, 0);    if (ref)
5170        OP2(SLJIT_SUB | SLJIT_SET_E, TMP2, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(offset + 1), TMP1, 0);
5171      else
5172        OP2(SLJIT_SUB | SLJIT_SET_E, TMP2, 0, SLJIT_MEM1(TMP2), sizeof(sljit_sw), TMP1, 0);
5173    
5174    if (withchecks)    if (withchecks)
5175      jump = JUMP(SLJIT_C_ZERO);      jump = JUMP(SLJIT_C_ZERO);
5176    
# Line 5089  if (jump != NULL) Line 5207  if (jump != NULL)
5207    else    else
5208      JUMPHERE(jump);      JUMPHERE(jump);
5209    }    }
 return cc + 1 + IMM2_SIZE;  
5210  }  }
5211    
5212  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)
5213  {  {
5214  DEFINE_COMPILER;  DEFINE_COMPILER;
5215    BOOL ref = (*cc == OP_REF || *cc == OP_REFI);
5216  backtrack_common *backtrack;  backtrack_common *backtrack;
5217  pcre_uchar type;  pcre_uchar type;
5218    int offset = 0;
5219  struct sljit_label *label;  struct sljit_label *label;
5220  struct sljit_jump *zerolength;  struct sljit_jump *zerolength;
5221  struct sljit_jump *jump = NULL;  struct sljit_jump *jump = NULL;
# Line 5106  BOOL minimize; Line 5225  BOOL minimize;
5225    
5226  PUSH_BACKTRACK(sizeof(iterator_backtrack), cc, NULL);  PUSH_BACKTRACK(sizeof(iterator_backtrack), cc, NULL);
5227    
5228    if (ref)
5229      offset = GET2(cc, 1) << 1;
5230    else
5231      cc += IMM2_SIZE;
5232  type = cc[1 + IMM2_SIZE];  type = cc[1 + IMM2_SIZE];
5233    
5234    SLJIT_COMPILE_ASSERT((OP_CRSTAR & 0x1) == 0, crstar_opcode_must_be_even);
5235  minimize = (type & 0x1) != 0;  minimize = (type & 0x1) != 0;
5236  switch(type)  switch(type)
5237    {    {
# Line 5144  if (!minimize) Line 5269  if (!minimize)
5269    if (min == 0)    if (min == 0)
5270      {      {
5271      allocate_stack(common, 2);      allocate_stack(common, 2);
5272        if (ref)
5273          OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(offset));
5274      OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), STR_PTR, 0);      OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), STR_PTR, 0);
5275      OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(1), SLJIT_IMM, 0);      OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(1), SLJIT_IMM, 0);
5276      /* Temporary release of STR_PTR. */      /* Temporary release of STR_PTR. */
5277      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));
5278      zerolength = compile_ref_checks(common, ccbegin, NULL);      /* Handles both invalid and empty cases. Since the minimum repeat,
5279        is zero the invalid case is basically the same as an empty case. */
5280        if (ref)
5281          zerolength = CMP(SLJIT_C_EQUAL, TMP1, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(offset + 1));
5282        else
5283          {
5284          compile_dnref_search(common, ccbegin, NULL);
5285          OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(TMP2), 0);
5286          OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), POSSESSIVE1, TMP2, 0);
5287          zerolength = CMP(SLJIT_C_EQUAL, TMP1, 0, SLJIT_MEM1(TMP2), sizeof(sljit_sw));
5288          }
5289      /* Restore if not zero length. */      /* Restore if not zero length. */
5290      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));
5291      }      }
5292    else    else
5293      {      {
5294      allocate_stack(common, 1);      allocate_stack(common, 1);
5295        if (ref)
5296          OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(offset));
5297      OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), SLJIT_IMM, 0);      OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), SLJIT_IMM, 0);
5298      zerolength = compile_ref_checks(common, ccbegin, &backtrack->topbacktracks);      if (ref)
5299          {
5300          add_jump(compiler, &backtrack->topbacktracks, CMP(SLJIT_C_EQUAL, TMP1, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(1)));
5301          zerolength = CMP(SLJIT_C_EQUAL, TMP1, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(offset + 1));
5302          }
5303        else
5304          {
5305          compile_dnref_search(common, ccbegin, &backtrack->topbacktracks);
5306          OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(TMP2), 0);
5307          OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), POSSESSIVE1, TMP2, 0);
5308          zerolength = CMP(SLJIT_C_EQUAL, TMP1, 0, SLJIT_MEM1(TMP2), sizeof(sljit_sw));
5309          }
5310      }      }
5311    
5312    if (min > 1 || max > 1)    if (min > 1 || max > 1)
5313      OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), POSSESSIVE0, SLJIT_IMM, 0);      OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), POSSESSIVE0, SLJIT_IMM, 0);
5314    
5315    label = LABEL();    label = LABEL();
5316      if (!ref)
5317        OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), POSSESSIVE1);
5318    compile_ref_matchingpath(common, ccbegin, &backtrack->topbacktracks, FALSE, FALSE);    compile_ref_matchingpath(common, ccbegin, &backtrack->topbacktracks, FALSE, FALSE);
5319    
5320    if (min > 1 || max > 1)    if (min > 1 || max > 1)
# Line 5193  if (!minimize) Line 5345  if (!minimize)
5345    JUMPHERE(zerolength);    JUMPHERE(zerolength);
5346    BACKTRACK_AS(iterator_backtrack)->matchingpath = LABEL();    BACKTRACK_AS(iterator_backtrack)->matchingpath = LABEL();
5347    
5348    decrease_call_count(common);    count_match(common);
5349    return cc;    return cc;
5350    }    }
5351    
5352  allocate_stack(common, 2);  allocate_stack(common, ref ? 2 : 3);
5353    if (ref)
5354      OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(offset));
5355  OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), SLJIT_IMM, 0);  OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), SLJIT_IMM, 0);
5356  if (type != OP_CRMINSTAR)  if (type != OP_CRMINSTAR)
5357    OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(1), SLJIT_IMM, 0);    OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(1), SLJIT_IMM, 0);
5358    
5359  if (min == 0)  if (min == 0)
5360    {    {
5361    zerolength = compile_ref_checks(common, ccbegin, NULL);    /* Handles both invalid and empty cases. Since the minimum repeat,
5362      is zero the invalid case is basically the same as an empty case. */
5363      if (ref)
5364        zerolength = CMP(SLJIT_C_EQUAL, TMP1, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(offset + 1));
5365      else
5366        {
5367        compile_dnref_search(common, ccbegin, NULL);
5368        OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(TMP2), 0);
5369        OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(2), TMP2, 0);
5370        zerolength = CMP(SLJIT_C_EQUAL, TMP1, 0, SLJIT_MEM1(TMP2), sizeof(sljit_sw));
5371        }
5372      /* Length is non-zero, we can match real repeats. */
5373    OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), STR_PTR, 0);    OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), STR_PTR, 0);
5374    jump = JUMP(SLJIT_JUMP);    jump = JUMP(SLJIT_JUMP);
5375    }    }
5376  else  else
5377    zerolength = compile_ref_checks(common, ccbegin, &backtrack->topbacktracks);    {
5378      if (ref)
5379        {
5380        add_jump(compiler, &backtrack->topbacktracks, CMP(SLJIT_C_EQUAL, TMP1, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(1)));
5381        zerolength = CMP(SLJIT_C_EQUAL, TMP1, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(offset + 1));
5382        }
5383      else
5384        {
5385        compile_dnref_search(common, ccbegin, &backtrack->topbacktracks);
5386        OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(TMP2), 0);
5387        OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(2), TMP2, 0);
5388        zerolength = CMP(SLJIT_C_EQUAL, TMP1, 0, SLJIT_MEM1(TMP2), sizeof(sljit_sw));
5389        }
5390      }
5391    
5392  BACKTRACK_AS(iterator_backtrack)->matchingpath = LABEL();  BACKTRACK_AS(iterator_backtrack)->matchingpath = LABEL();
5393  if (max > 0)  if (max > 0)
5394    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));
5395    
5396    if (!ref)
5397      OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(STACK_TOP), STACK(2));
5398  compile_ref_matchingpath(common, ccbegin, &backtrack->topbacktracks, TRUE, TRUE);  compile_ref_matchingpath(common, ccbegin, &backtrack->topbacktracks, TRUE, TRUE);
5399  OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), STR_PTR, 0);  OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), STR_PTR, 0);
5400    
# Line 5232  if (jump != NULL) Line 5412  if (jump != NULL)
5412    JUMPHERE(jump);    JUMPHERE(jump);
5413  JUMPHERE(zerolength);  JUMPHERE(zerolength);
5414    
5415  decrease_call_count(common);  count_match(common);
5416  return cc;  return cc;
5417  }  }
5418    
# Line 5242  DEFINE_COMPILER; Line 5422  DEFINE_COMPILER;
5422  backtrack_common *backtrack;  backtrack_common *backtrack;
5423  recurse_entry *entry = common->entries;  recurse_entry *entry = common->entries;
5424  recurse_entry *prev = NULL;  recurse_entry *prev = NULL;
5425  int start = GET(cc, 1);  sljit_sw start = GET(cc, 1);
5426  pcre_uchar *start_cc;  pcre_uchar *start_cc;
5427    BOOL needs_control_head;
5428    
5429  PUSH_BACKTRACK(sizeof(recurse_backtrack), cc, NULL);  PUSH_BACKTRACK(sizeof(recurse_backtrack), cc, NULL);
5430    
5431  /* Inlining simple patterns. */  /* Inlining simple patterns. */
5432  if (get_framesize(common, common->start + start, TRUE) == no_stack)  if (get_framesize(common, common->start + start, NULL, TRUE, &needs_control_head) == no_stack)
5433    {    {
5434    start_cc = common->start + start;    start_cc = common->start + start;
5435    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 5589  static pcre_uchar *compile_assert_matchi
5589  DEFINE_COMPILER;  DEFINE_COMPILER;
5590  int framesize;  int framesize;
5591  int extrasize;  int extrasize;
5592  BOOL needs_control_head = common->control_head_ptr != 0;  BOOL needs_control_head;
5593  int private_data_ptr;  int private_data_ptr;
5594  backtrack_common altbacktrack;  backtrack_common altbacktrack;
5595  pcre_uchar *ccbegin;  pcre_uchar *ccbegin;
# Line 5418  jump_list *tmp = NULL; Line 5599  jump_list *tmp = NULL;
5599  jump_list **target = (conditional) ? &backtrack->condfailed : &backtrack->common.topbacktracks;  jump_list **target = (conditional) ? &backtrack->condfailed : &backtrack->common.topbacktracks;
5600  jump_list **found;  jump_list **found;
5601  /* Saving previous accept variables. */  /* Saving previous accept variables. */
5602    BOOL save_local_exit = common->local_exit;
5603    BOOL save_positive_assert = common->positive_assert;
5604    then_trap_backtrack *save_then_trap = common->then_trap;
5605  struct sljit_label *save_quit_label = common->quit_label;  struct sljit_label *save_quit_label = common->quit_label;
5606  struct sljit_label *save_accept_label = common->accept_label;  struct sljit_label *save_accept_label = common->accept_label;
5607  jump_list *save_quit = common->quit;  jump_list *save_quit = common->quit;
5608    jump_list *save_positive_assert_quit = common->positive_assert_quit;
5609  jump_list *save_accept = common->accept;  jump_list *save_accept = common->accept;
 BOOL save_local_exit = common->local_exit;  
5610  struct sljit_jump *jump;  struct sljit_jump *jump;
5611  struct sljit_jump *brajump = NULL;  struct sljit_jump *brajump = NULL;
5612    
5613    /* Assert captures then. */
5614    common->then_trap = NULL;
5615    
5616  if (*cc == OP_BRAZERO || *cc == OP_BRAMINZERO)  if (*cc == OP_BRAZERO || *cc == OP_BRAMINZERO)
5617    {    {
5618    SLJIT_ASSERT(!conditional);    SLJIT_ASSERT(!conditional);
# Line 5434  if (*cc == OP_BRAZERO || *cc == OP_BRAMI Line 5621  if (*cc == OP_BRAZERO || *cc == OP_BRAMI
5621    }    }
5622  private_data_ptr = PRIVATE_DATA(cc);  private_data_ptr = PRIVATE_DATA(cc);
5623  SLJIT_ASSERT(private_data_ptr != 0);  SLJIT_ASSERT(private_data_ptr != 0);
5624  framesize = get_framesize(common, cc, FALSE);  framesize = get_framesize(common, cc, NULL, FALSE, &needs_control_head);
5625  backtrack->framesize = framesize;  backtrack->framesize = framesize;
5626  backtrack->private_data_ptr = private_data_ptr;  backtrack->private_data_ptr = private_data_ptr;
5627  opcode = *cc;  opcode = *cc;
# Line 5454  if (bra == OP_BRAMINZERO) Line 5641  if (bra == OP_BRAMINZERO)
5641  if (framesize < 0)  if (framesize < 0)
5642    {    {
5643    extrasize = needs_control_head ? 2 : 1;    extrasize = needs_control_head ? 2 : 1;
5644    if (framesize != no_stack)    if (framesize == no_frame)
5645      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);
5646    allocate_stack(common, extrasize);    allocate_stack(common, extrasize);
5647    if (needs_control_head)    if (needs_control_head)
# Line 5484  else Line 5671  else
5671      }      }
5672    else    else
5673      OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(1), TMP1, 0);      OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(1), TMP1, 0);
5674    init_frame(common, ccbegin, framesize + extrasize - 1, extrasize, FALSE);    init_frame(common, ccbegin, NULL, framesize + extrasize - 1, extrasize, FALSE);
5675      }
5676    
5677    memset(&altbacktrack, 0, sizeof(backtrack_common));
5678    if (opcode == OP_ASSERT_NOT || opcode == OP_ASSERTBACK_NOT)
5679      {
5680      /* Negative assert is stronger than positive assert. */
5681      common->local_exit = TRUE;
5682      common->quit_label = NULL;
5683      common->quit = NULL;
5684      common->positive_assert = FALSE;
5685    }    }
5686    else
5687      common->positive_assert = TRUE;
5688    common->positive_assert_quit = NULL;
5689    
 memset(&altbacktrack, 0, sizeof(backtrack_common));  
 common->local_exit = TRUE;  
 common->quit_label = NULL;  
 common->quit = NULL;  
5690  while (1)  while (1)
5691    {    {
5692    common->accept_label = NULL;    common->accept_label = NULL;
# Line 5505  while (1) Line 5701  while (1)
5701    compile_matchingpath(common, ccbegin + 1 + LINK_SIZE, cc, &altbacktrack);    compile_matchingpath(common, ccbegin + 1 + LINK_SIZE, cc, &altbacktrack);
5702    if (SLJIT_UNLIKELY(sljit_get_compiler_error(compiler)))    if (SLJIT_UNLIKELY(sljit_get_compiler_error(compiler)))
5703      {      {
5704      common->local_exit = save_local_exit;      if (opcode == OP_ASSERT_NOT || opcode == OP_ASSERTBACK_NOT)
5705      common->quit_label = save_quit_label;        {
5706          common->local_exit = save_local_exit;
5707          common->quit_label = save_quit_label;
5708          common->quit = save_quit;
5709          }
5710        common->positive_assert = save_positive_assert;
5711        common->then_trap = save_then_trap;
5712      common->accept_label = save_accept_label;      common->accept_label = save_accept_label;
5713      common->quit = save_quit;      common->positive_assert_quit = save_positive_assert_quit;
5714      common->accept = save_accept;      common->accept = save_accept;
5715      return NULL;      return NULL;
5716      }      }
# Line 5519  while (1) Line 5721  while (1)
5721    /* Reset stack. */    /* Reset stack. */
5722    if (framesize < 0)    if (framesize < 0)
5723      {      {
5724      if (framesize != no_stack)      if (framesize == no_frame)
5725        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);
5726      else      else
5727        free_stack(common, extrasize);        free_stack(common, extrasize);
# Line 5573  while (1) Line 5775  while (1)
5775    compile_backtrackingpath(common, altbacktrack.top);    compile_backtrackingpath(common, altbacktrack.top);
5776    if (SLJIT_UNLIKELY(sljit_get_compiler_error(compiler)))    if (SLJIT_UNLIKELY(sljit_get_compiler_error(compiler)))
5777      {      {
5778      common->local_exit = save_local_exit;      if (opcode == OP_ASSERT_NOT || opcode == OP_ASSERTBACK_NOT)
5779      common->quit_label = save_quit_label;        {
5780          common->local_exit = save_local_exit;
5781          common->quit_label = save_quit_label;
5782          common->quit = save_quit;
5783          }
5784        common->positive_assert = save_positive_assert;
5785        common->then_trap = save_then_trap;
5786      common->accept_label = save_accept_label;      common->accept_label = save_accept_label;
5787      common->quit = save_quit;      common->positive_assert_quit = save_positive_assert_quit;
5788      common->accept = save_accept;      common->accept = save_accept;
5789      return NULL;      return NULL;
5790      }      }
# Line 5589  while (1) Line 5797  while (1)
5797    cc += GET(cc, 1);    cc += GET(cc, 1);
5798    }    }
5799    
5800    if (opcode == OP_ASSERT_NOT || opcode == OP_ASSERTBACK_NOT)
5801      {
5802      SLJIT_ASSERT(common->positive_assert_quit == NULL);
5803      /* Makes the check less complicated below. */
5804      common->positive_assert_quit = common->quit;
5805      }
5806    
5807  /* None of them matched. */  /* None of them matched. */
5808  if (common->quit != NULL)  if (common->positive_assert_quit != NULL)
5809    {    {
5810    jump = JUMP(SLJIT_JUMP);    jump = JUMP(SLJIT_JUMP);
5811    set_jumps(common->quit, LABEL());    set_jumps(common->positive_assert_quit, LABEL());
5812    SLJIT_ASSERT(framesize != no_stack);    SLJIT_ASSERT(framesize != no_stack);
5813    if (framesize < 0)    if (framesize < 0)
5814      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 5968  else
5968      }      }
5969    }    }
5970    
5971  common->local_exit = save_local_exit;  if (opcode == OP_ASSERT_NOT || opcode == OP_ASSERTBACK_NOT)
5972  common->quit_label = save_quit_label;    {
5973      common->local_exit = save_local_exit;
5974      common->quit_label = save_quit_label;
5975      common->quit = save_quit;
5976      }
5977    common->positive_assert = save_positive_assert;
5978    common->then_trap = save_then_trap;
5979  common->accept_label = save_accept_label;  common->accept_label = save_accept_label;
5980  common->quit = save_quit;  common->positive_assert_quit = save_positive_assert_quit;
5981  common->accept = save_accept;  common->accept = save_accept;
5982  return cc + 1 + LINK_SIZE;  return cc + 1 + LINK_SIZE;
5983  }  }
5984    
5985  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)
5986  {  {
5987  int condition = FALSE;  DEFINE_COMPILER;
5988  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];  
5989    
5990  for (i = 0; i < name_count; i++)  if (framesize < 0)
5991    {    {
5992    if (GET2(slotA, 0) == refno) break;    if (framesize == no_frame)
5993    slotA += name_entry_size;      OP1(SLJIT_MOV, STACK_TOP, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), private_data_ptr);
5994    }    else
5995        {
5996        stacksize = needs_control_head ? 1 : 0;
5997        if (ket != OP_KET || has_alternatives)
5998          stacksize++;
5999        free_stack(common, stacksize);
6000        }
6001    
6002  if (i < name_count)    if (needs_control_head)
6003    {      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. */  
6004    
6005    slotB = slotA;    /* TMP2 which is set here used by OP_KETRMAX below. */
6006    while (slotB > name_table)    if (ket == OP_KETRMAX)
6007        OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(STACK_TOP), 0);
6008      else if (ket == OP_KETRMIN)
6009      {      {
6010      slotB -= name_entry_size;      /* Move the STR_PTR to the private_data_ptr. */
6011      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;  
6012      }      }
6013      }
6014    else
6015      {
6016      stacksize = (ket != OP_KET || has_alternatives) ? 2 : 1;
6017      OP2(SLJIT_ADD, STACK_TOP, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), private_data_ptr, SLJIT_IMM, (framesize + stacksize) * sizeof(sljit_sw));
6018      if (needs_control_head)
6019        OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(STACK_TOP), 0);
6020    
6021    /* Scan up for duplicates */    if (ket == OP_KETRMAX)
   if (!condition)  
6022      {      {
6023      slotB = slotA;      /* TMP2 which is set here used by OP_KETRMAX below. */
6024      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;  
       }  
6025      }      }
6026    }    }
6027  return condition;  if (needs_control_head)
6028      OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), common->control_head_ptr, TMP1, 0);
6029  }  }
6030    
6031  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)
6032  {  {
6033  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;  
6034    
6035  for (i = 0; i < name_count; i++)  if (common->capture_last_ptr != 0)
6036    {    {
6037    if (GET2(slotA, 0) == recno) break;    OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), common->capture_last_ptr);
6038    slotA += name_entry_size;    OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), common->capture_last_ptr, SLJIT_IMM, offset >> 1);
6039      OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(stacksize), TMP1, 0);
6040      stacksize++;
6041    }    }
6042    if (common->optimized_cbracket[offset >> 1] == 0)
 if (i < name_count)  
6043    {    {
6044    /* Found a name for the number - there can be only one; duplicate    OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(offset));
6045    names for different numbers are allowed, but not vice versa. First    OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(offset + 1));
6046    scan down for duplicates. */    OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(stacksize), TMP1, 0);
6047      OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), private_data_ptr);
6048    slotB = slotA;    OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(stacksize + 1), TMP2, 0);
6049    while (slotB > name_table)    OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(offset + 1), STR_PTR, 0);
6050      {    OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(offset), TMP1, 0);
6051      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;  
       }  
     }  
6052    }    }
6053  return condition;  return stacksize;
6054  }  }
6055    
6056  /*  /*
# Line 5932  backtrack_common *backtrack; Line 6114  backtrack_common *backtrack;
6114  pcre_uchar opcode;  pcre_uchar opcode;
6115  int private_data_ptr = 0;  int private_data_ptr = 0;
6116  int offset = 0;  int offset = 0;
6117  int stacksize;  int i, stacksize;
6118    int repeat_ptr = 0, repeat_length = 0;
6119    int repeat_type = 0, repeat_count = 0;
6120  pcre_uchar *ccbegin;  pcre_uchar *ccbegin;
6121  pcre_uchar *matchingpath;  pcre_uchar *matchingpath;
6122    pcre_uchar *slot;
6123  pcre_uchar bra = OP_BRA;  pcre_uchar bra = OP_BRA;
6124  pcre_uchar ket;  pcre_uchar ket;
6125  assert_backtrack *assert;  assert_backtrack *assert;
6126  BOOL has_alternatives;  BOOL has_alternatives;
6127    BOOL needs_control_head = FALSE;
6128  struct sljit_jump *jump;  struct sljit_jump *jump;
6129  struct sljit_jump *skip;  struct sljit_jump *skip;
6130  struct sljit_label *rmaxlabel = NULL;  struct sljit_label *rmax_label = NULL;
6131  struct sljit_jump *braminzerojump = NULL;  struct sljit_jump *braminzero = NULL;
6132    
6133  PUSH_BACKTRACK(sizeof(bracket_backtrack), cc, NULL);  PUSH_BACKTRACK(sizeof(bracket_backtrack), cc, NULL);
6134    
# Line 5955  if (*cc == OP_BRAZERO || *cc == OP_BRAMI Line 6141  if (*cc == OP_BRAZERO || *cc == OP_BRAMI
6141    
6142  opcode = *cc;  opcode = *cc;
6143  ccbegin = cc;  ccbegin = cc;
6144  matchingpath = ccbegin + 1 + LINK_SIZE;  matchingpath = bracketend(cc) - 1 - LINK_SIZE;
6145    ket = *matchingpath;
6146    if (ket == OP_KET && PRIVATE_DATA(matchingpath) != 0)
6147      {
6148      repeat_ptr = PRIVATE_DATA(matchingpath);
6149      repeat_length = PRIVATE_DATA(matchingpath + 1);
6150      repeat_type = PRIVATE_DATA(matchingpath + 2);
6151      repeat_count = PRIVATE_DATA(matchingpath + 3);
6152      SLJIT_ASSERT(repeat_length != 0 && repeat_type != 0 && repeat_count != 0);
6153      if (repeat_type == OP_UPTO)
6154        ket = OP_KETRMAX;
6155      if (repeat_type == OP_MINUPTO)
6156        ket = OP_KETRMIN;
6157      }
6158    
6159  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)
6160    {    {
6161    /* Drop this bracket_backtrack. */    /* Drop this bracket_backtrack. */
6162    parent->top = backtrack->prev;    parent->top = backtrack->prev;
6163    return bracketend(cc);    return matchingpath + 1 + LINK_SIZE + repeat_length;
6164    }    }
6165    
6166  ket = *(bracketend(cc) - 1 - LINK_SIZE);  matchingpath = ccbegin + 1 + LINK_SIZE;
6167  SLJIT_ASSERT(ket == OP_KET || ket == OP_KETRMAX || ket == OP_KETRMIN);  SLJIT_ASSERT(ket == OP_KET || ket == OP_KETRMAX || ket == OP_KETRMIN);
6168  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)));
6169  cc += GET(cc, 1);  cc += GET(cc, 1);
6170    
6171  has_alternatives = *cc == OP_ALT;  has_alternatives = *cc == OP_ALT;
6172  if (SLJIT_UNLIKELY(opcode == OP_COND) || SLJIT_UNLIKELY(opcode == OP_SCOND))  if (SLJIT_UNLIKELY(opcode == OP_COND || opcode == OP_SCOND))
6173    {    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);  
     }  
   }  
6174    
6175  if (SLJIT_UNLIKELY(opcode == OP_COND) && (*cc == OP_KETRMAX || *cc == OP_KETRMIN))  if (SLJIT_UNLIKELY(opcode == OP_COND) && (*cc == OP_KETRMAX || *cc == OP_KETRMIN))
6176    opcode = OP_SCOND;    opcode = OP_SCOND;
# Line 6014  else if (opcode == OP_ONCE || opcode == Line 6201  else if (opcode == OP_ONCE || opcode ==
6201    SLJIT_ASSERT(private_data_ptr != 0);    SLJIT_ASSERT(private_data_ptr != 0);
6202    BACKTRACK_AS(bracket_backtrack)->private_data_ptr = private_data_ptr;    BACKTRACK_AS(bracket_backtrack)->private_data_ptr = private_data_ptr;
6203    if (opcode == OP_ONCE)    if (opcode == OP_ONCE)
6204      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);
6205    }    }
6206    
6207  /* Instructions before the first alternative. */  /* Instructions before the first alternative. */
6208  stacksize = 0;  stacksize = 0;
6209  if ((ket == OP_KETRMAX) || (ket == OP_KETRMIN && bra != OP_BRAMINZERO))  if (ket == OP_KETRMAX || (ket == OP_KETRMIN && bra != OP_BRAMINZERO))
6210    stacksize++;    stacksize++;
6211  if (bra == OP_BRAZERO)  if (bra == OP_BRAZERO)
6212    stacksize++;    stacksize++;
# Line 6028  if (stacksize > 0) Line 6215  if (stacksize > 0)
6215    allocate_stack(common, stacksize);    allocate_stack(common, stacksize);
6216    
6217  stacksize = 0;  stacksize = 0;
6218  if ((ket == OP_KETRMAX) || (ket == OP_KETRMIN && bra != OP_BRAMINZERO))  if (ket == OP_KETRMAX || (ket == OP_KETRMIN && bra != OP_BRAMINZERO))
6219    {    {
6220    OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(stacksize), SLJIT_IMM, 0);    OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(stacksize), SLJIT_IMM, 0);
6221    stacksize++;    stacksize++;
# Line 6044  if (bra == OP_BRAMINZERO) Line 6231  if (bra == OP_BRAMINZERO)
6231    if (ket != OP_KETRMIN)    if (ket != OP_KETRMIN)
6232      {      {
6233      free_stack(common, 1);      free_stack(common, 1);
6234      braminzerojump = CMP(SLJIT_C_EQUAL, STR_PTR, 0, SLJIT_IMM, 0);      braminzero = CMP(SLJIT_C_EQUAL, STR_PTR, 0, SLJIT_IMM, 0);
6235      }      }
6236    else    else
6237      {      {
# Line 6059  if (bra == OP_BRAMINZERO) Line 6246  if (bra == OP_BRAMINZERO)
6246        if (opcode != OP_ONCE || BACKTRACK_AS(bracket_backtrack)->u.framesize < 0)        if (opcode != OP_ONCE || BACKTRACK_AS(bracket_backtrack)->u.framesize < 0)
6247          {          {
6248          /* 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. */
6249          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);
6250          }          }
6251        else        else
6252          {          {
6253          /* Except when the whole stack frame must be saved. */          /* Except when the whole stack frame must be saved. */
6254          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);
6255          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));
6256          }          }
6257        JUMPHERE(skip);        JUMPHERE(skip);
6258        }        }
# Line 6078  if (bra == OP_BRAMINZERO) Line 6265  if (bra == OP_BRAMINZERO)
6265      }      }
6266    }    }
6267    
6268    if (repeat_type != 0)
6269      {
6270      OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), repeat_ptr, SLJIT_IMM, repeat_count);
6271      if (repeat_type == OP_EXACT)
6272        rmax_label = LABEL();
6273      }
6274    
6275  if (ket == OP_KETRMIN)  if (ket == OP_KETRMIN)
6276    BACKTRACK_AS(bracket_backtrack)->recursive_matchingpath = LABEL();    BACKTRACK_AS(bracket_backtrack)->recursive_matchingpath = LABEL();
6277    
6278  if (ket == OP_KETRMAX)  if (ket == OP_KETRMAX)
6279    {    {
6280    rmaxlabel = LABEL();    rmax_label = LABEL();
6281    if (has_alternatives && opcode != OP_ONCE && opcode < OP_SBRA)    if (has_alternatives && opcode != OP_ONCE && opcode < OP_SBRA && repeat_type == 0)
6282      BACKTRACK_AS(bracket_backtrack)->alternative_matchingpath = rmaxlabel;      BACKTRACK_AS(bracket_backtrack)->alternative_matchingpath = rmax_label;
6283    }    }
6284    
6285  /* Handling capturing brackets and alternatives. */  /* Handling capturing brackets and alternatives. */
6286  if (opcode == OP_ONCE)  if (opcode == OP_ONCE)
6287    {    {
6288      stacksize = 0;
6289      if (needs_control_head)
6290        {
6291        OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), common->control_head_ptr);
6292        stacksize++;
6293        }
6294    
6295    if (BACKTRACK_AS(bracket_backtrack)->u.framesize < 0)    if (BACKTRACK_AS(bracket_backtrack)->u.framesize < 0)
6296      {      {
6297      /* Neither capturing brackets nor recursions are not found in the block. */      /* Neither capturing brackets nor recursions are found in the block. */
6298      if (ket == OP_KETRMIN)      if (ket == OP_KETRMIN)
6299        {        {
6300        OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), private_data_ptr);        stacksize += 2;
6301        allocate_stack(common, 2);        if (!needs_control_head)
6302        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));  
6303        }        }
6304      else if (ket == OP_KETRMAX || has_alternatives)      else
6305        {        {
6306        OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), private_data_ptr, STACK_TOP, 0);        if (BACKTRACK_AS(bracket_backtrack)->u.framesize == no_frame)
6307        allocate_stack(common, 1);          OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), private_data_ptr, STACK_TOP, 0);
6308        OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), STR_PTR, 0);        if (ket == OP_KETRMAX || has_alternatives)
6309            stacksize++;
6310        }        }
6311      else  
6312        OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), private_data_ptr, STACK_TOP, 0);      if (stacksize > 0)
6313          allocate_stack(common, stacksize);
6314    
6315        stacksize = 0;
6316        if (needs_control_head)
6317          {
6318          stacksize++;
6319          OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), TMP2, 0);
6320          }
6321    
6322        if (ket == OP_KETRMIN)
6323          {
6324          if (needs_control_head)
6325            OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), private_data_ptr);
6326          OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(stacksize), STR_PTR, 0);
6327          if (BACKTRACK_AS(bracket_backtrack)->u.framesize == no_frame)
6328            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));
6329          OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(stacksize + 1), TMP2, 0);
6330          }
6331        else if (ket == OP_KETRMAX || has_alternatives)
6332          OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(stacksize), STR_PTR, 0);
6333      }      }
6334    else    else
6335      {      {
6336      if (ket == OP_KETRMIN || ket == OP_KETRMAX || has_alternatives)      if (ket != OP_KET || has_alternatives)
6337          stacksize++;
6338    
6339        stacksize += BACKTRACK_AS(bracket_backtrack)->u.framesize + 1;
6340        allocate_stack(common, stacksize);
6341    
6342        if (needs_control_head)
6343          OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), TMP2, 0);
6344    
6345        OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), private_data_ptr);
6346        OP2(SLJIT_SUB, TMP2, 0, STACK_TOP, 0, SLJIT_IMM, stacksize * sizeof(sljit_sw));
6347    
6348        stacksize = needs_control_head ? 1 : 0;
6349        if (ket != OP_KET || has_alternatives)
6350        {        {
6351        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);  
6352        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);
6353        OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(1), TMP1, 0);        stacksize++;
6354        init_frame(common, ccbegin, BACKTRACK_AS(bracket_backtrack)->u.framesize + 1, 2, FALSE);        OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(stacksize), TMP1, 0);
6355        }        }
6356      else      else
6357        {        {
       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));  
6358        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);
6359        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);  
6360        }        }
6361        init_frame(common, ccbegin, NULL, BACKTRACK_AS(bracket_backtrack)->u.framesize + stacksize, stacksize + 1, FALSE);
6362      }      }
6363    }    }
6364  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 6407  if (opcode == OP_COND || opcode == OP_SC
6407        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)));
6408      matchingpath += 1 + IMM2_SIZE;      matchingpath += 1 + IMM2_SIZE;
6409      }      }
6410    else if (*matchingpath == OP_NCREF)    else if (*matchingpath == OP_DNCREF)
6411      {      {
6412      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));  
6413    
6414      JUMPHERE(jump);      i = GET2(matchingpath, 1 + IMM2_SIZE);
6415      matchingpath += 1 + IMM2_SIZE;      slot = common->name_table + GET2(matchingpath, 1) * common->name_entry_size;
6416        OP1(SLJIT_MOV, TMP3, 0, STR_PTR, 0);
6417        OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(1));
6418        OP2(SLJIT_SUB | SLJIT_SET_E, TMP2, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(GET2(slot, 0) << 1), TMP1, 0);
6419        slot += common->name_entry_size;
6420        i--;
6421        while (i-- > 0)
6422          {
6423          OP2(SLJIT_SUB, STR_PTR, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(GET2(slot, 0) << 1), TMP1, 0);
6424          OP2(SLJIT_OR | SLJIT_SET_E, TMP2, 0, TMP2, 0, STR_PTR, 0);
6425          slot += common->name_entry_size;
6426          }
6427        OP1(SLJIT_MOV, STR_PTR, 0, TMP3, 0);
6428        add_jump(compiler, &(BACKTRACK_AS(bracket_backtrack)->u.condfailed), JUMP(SLJIT_C_ZERO));
6429        matchingpath += 1 + 2 * IMM2_SIZE;
6430      }      }
6431    else if (*matchingpath == OP_RREF || *matchingpath == OP_NRREF)    else if (*matchingpath == OP_RREF || *matchingpath == OP_DNRREF)
6432      {      {
6433      /* Never has other case. */      /* Never has other case. */
6434      BACKTRACK_AS(bracket_backtrack)->u.condfailed = NULL;      BACKTRACK_AS(bracket_backtrack)->u.condfailed = NULL;
6435        SLJIT_ASSERT(!has_alternatives);
6436    
6437      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)  
6438        {        {
6439        SLJIT_ASSERT(!has_alternatives);        stacksize = GET2(matchingpath, 1);
6440          if (common->currententry == NULL)
6441            stacksize = 0;
6442          else if (stacksize == RREF_ANY)
6443            stacksize = 1;
6444          else if (common->currententry->start == 0)
6445            stacksize = stacksize == 0;
6446          else
6447            stacksize = stacksize == (int)GET2(common->start, common->currententry->start + 1 + LINK_SIZE);
6448    
6449        if (stacksize != 0)        if (stacksize != 0)
6450          matchingpath += 1 + IMM2_SIZE;          matchingpath += 1 + IMM2_SIZE;
6451          }
6452        else
6453          {
6454          if (common->currententry == NULL || common->currententry->start == 0)
6455            stacksize = 0;
6456        else        else
6457          {          {
6458            stacksize = GET2(matchingpath, 1 + IMM2_SIZE);
6459            slot = common->name_table + GET2(matchingpath, 1) * common->name_entry_size;
6460            i = (int)GET2(common->start, common->currententry->start + 1 + LINK_SIZE);
6461            while (stacksize > 0)
6462              {
6463              if ((int)GET2(slot, 0) == i)
6464                break;
6465              slot += common->name_entry_size;
6466              stacksize--;
6467              }
6468            }
6469    
6470          if (stacksize != 0)
6471            matchingpath += 1 + 2 * IMM2_SIZE;
6472          }
6473    
6474          /* The stacksize == 0 is a common "else" case. */
6475          if (stacksize == 0)
6476            {
6477          if (*cc == OP_ALT)          if (*cc == OP_ALT)
6478            {            {
6479            matchingpath = cc + 1 + LINK_SIZE;            matchingpath = cc + 1 + LINK_SIZE;
# Line 6229  if (opcode == OP_COND || opcode == OP_SC Line 6482  if (opcode == OP_COND || opcode == OP_SC
6482          else          else
6483            matchingpath = cc;            matchingpath = cc;
6484          }          }
       }  
     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;  
       }  
6485      }      }
6486    else    else
6487      {      {
# Line 6267  if (SLJIT_UNLIKELY(sljit_get_compiler_er Line 6502  if (SLJIT_UNLIKELY(sljit_get_compiler_er
6502    return NULL;    return NULL;
6503    
6504  if (opcode == OP_ONCE)  if (opcode == OP_ONCE)
6505    {    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));  
       }  
     }  
   }  
6506    
6507  stacksize = 0;  stacksize = 0;
6508    if (repeat_type == OP_MINUPTO)
6509      {
6510      /* We need to preserve the counter. TMP2 will be used below. */
6511      OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), repeat_ptr);
6512      stacksize++;
6513      }
6514  if (ket != OP_KET || bra != OP_BRA)  if (ket != OP_KET || bra != OP_BRA)
6515    stacksize++;    stacksize++;
6516  if (offset != 0)  if (offset != 0)
# Line 6309  if (stacksize > 0) Line 6527  if (stacksize > 0)
6527    allocate_stack(common, stacksize);    allocate_stack(common, stacksize);
6528    
6529  stacksize = 0;  stacksize = 0;
6530    if (repeat_type == OP_MINUPTO)
6531      {
6532      /* TMP2 was set above. */
6533      OP2(SLJIT_SUB, SLJIT_MEM1(STACK_TOP), STACK(stacksize), TMP2, 0, SLJIT_IMM, 1);
6534      stacksize++;
6535      }
6536    
6537  if (ket != OP_KET || bra != OP_BRA)  if (ket != OP_KET || bra != OP_BRA)
6538    {    {
6539    if (ket != OP_KET)    if (ket != OP_KET)
# Line 6319  if (ket != OP_KET || bra != OP_BRA) Line 6544  if (ket != OP_KET || bra != OP_BRA)
6544    }    }
6545    
6546  if (offset != 0)  if (offset != 0)
6547    {    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;  
     }  
   }  
6548    
6549  if (has_alternatives)  if (has_alternatives)
6550    {    {
# Line 6357  if (offset != 0 && common->optimized_cbr Line 6563  if (offset != 0 && common->optimized_cbr
6563    
6564  if (ket == OP_KETRMAX)  if (ket == OP_KETRMAX)
6565    {    {
6566    if (opcode == OP_ONCE || opcode >= OP_SBRA)    if (repeat_type != 0)
6567        {
6568        if (has_alternatives)
6569          BACKTRACK_AS(bracket_backtrack)->alternative_matchingpath = LABEL();
6570        OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_MEM1(SLJIT_LOCALS_REG), repeat_ptr, SLJIT_MEM1(SLJIT_LOCALS_REG), repeat_ptr, SLJIT_IMM, 1);
6571        JUMPTO(SLJIT_C_NOT_ZERO, rmax_label);
6572        /* Drop STR_PTR for greedy plus quantifier. */
6573        if (opcode != OP_ONCE)
6574          free_stack(common, 1);
6575        }
6576      else if (opcode == OP_ONCE || opcode >= OP_SBRA)
6577      {      {
6578      if (has_alternatives)      if (has_alternatives)
6579        BACKTRACK_AS(bracket_backtrack)->alternative_matchingpath = LABEL();        BACKTRACK_AS(bracket_backtrack)->alternative_matchingpath = LABEL();
6580      /* Checking zero-length iteration. */      /* Checking zero-length iteration. */
6581      if (opcode != OP_ONCE)      if (opcode != OP_ONCE)
6582        {        {
6583        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);
6584        /* Drop STR_PTR for greedy plus quantifier. */        /* Drop STR_PTR for greedy plus quantifier. */
6585        if (bra != OP_BRAZERO)        if (bra != OP_BRAZERO)
6586          free_stack(common, 1);          free_stack(common, 1);
6587        }        }
6588      else      else
6589        /* TMP2 must contain the starting STR_PTR. */        /* TMP2 must contain the starting STR_PTR. */
6590        CMPTO(SLJIT_C_NOT_EQUAL, TMP2, 0, STR_PTR, 0, rmaxlabel);        CMPTO(SLJIT_C_NOT_EQUAL, TMP2, 0, STR_PTR, 0, rmax_label);
6591      }      }
6592    else    else
6593      JUMPTO(SLJIT_JUMP, rmaxlabel);      JUMPTO(SLJIT_JUMP, rmax_label);
6594    BACKTRACK_AS(bracket_backtrack)->recursive_matchingpath = LABEL();    BACKTRACK_AS(bracket_backtrack)->recursive_matchingpath = LABEL();
6595    }    }
6596    
6597    if (repeat_type == OP_EXACT)
6598      {
6599      OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_MEM1(SLJIT_LOCALS_REG), repeat_ptr, SLJIT_MEM1(SLJIT_LOCALS_REG), repeat_ptr, SLJIT_IMM, 1);
6600      JUMPTO(SLJIT_C_NOT_ZERO, rmax_label);
6601      }
6602    else if (repeat_type == OP_UPTO)
6603      {
6604      /* We need to preserve the counter. */
6605      OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), repeat_ptr);
6606      allocate_stack(common, 1);
6607      OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), TMP2, 0);
6608      }
6609    
6610  if (bra == OP_BRAZERO)  if (bra == OP_BRAZERO)
6611    BACKTRACK_AS(bracket_backtrack)->zero_matchingpath = LABEL();    BACKTRACK_AS(bracket_backtrack)->zero_matchingpath = LABEL();
6612    
# Line 6385  if (bra == OP_BRAMINZERO) Line 6614  if (bra == OP_BRAMINZERO)
6614    {    {
6615    /* This is a backtrack path! (From the viewpoint of OP_BRAMINZERO) */    /* This is a backtrack path! (From the viewpoint of OP_BRAMINZERO) */
6616    JUMPTO(SLJIT_JUMP, ((braminzero_backtrack *)parent)->matchingpath);    JUMPTO(SLJIT_JUMP, ((braminzero_backtrack *)parent)->matchingpath);
6617    if (braminzerojump != NULL)    if (braminzero != NULL)
6618      {      {
6619      JUMPHERE(braminzerojump);      JUMPHERE(braminzero);
6620      /* We need to release the end pointer to perform the      /* We need to release the end pointer to perform the
6621      backtrack for the zero-length iteration. When      backtrack for the zero-length iteration. When
6622      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 6632  if (bra == OP_BRAMINZERO)
6632    }    }
6633    
6634  if ((ket != OP_KET && bra != OP_BRAMINZERO) || bra == OP_BRAZERO)  if ((ket != OP_KET && bra != OP_BRAMINZERO) || bra == OP_BRAZERO)
6635    decrease_call_count(common);    count_match(common);
6636    
6637  /* Skip the other alternatives. */  /* Skip the other alternatives. */
6638  while (*cc == OP_ALT)  while (*cc == OP_ALT)
6639    cc += GET(cc, 1);    cc += GET(cc, 1);
6640  cc += 1 + LINK_SIZE;  cc += 1 + LINK_SIZE;
6641  return cc;  
6642    /* Temporarily encoding the needs_control_head in framesize. */
6643    if (opcode == OP_ONCE)
6644      BACKTRACK_AS(bracket_backtrack)->u.framesize = (BACKTRACK_AS(bracket_backtrack)->u.framesize << 1) | (needs_control_head ? 1 : 0);
6645    return cc + repeat_length;
6646  }  }
6647    
6648  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 6652  backtrack_common *backtrack;
6652  pcre_uchar opcode;  pcre_uchar opcode;
6653  int private_data_ptr;  int private_data_ptr;
6654  int cbraprivptr = 0;  int cbraprivptr = 0;
6655    BOOL needs_control_head;
6656  int framesize;  int framesize;
6657  int stacksize;  int stacksize;
6658  int offset = 0;  int offset = 0;
6659  BOOL zero = FALSE;  BOOL zero = FALSE;
6660  pcre_uchar *ccbegin = NULL;  pcre_uchar *ccbegin = NULL;
6661  int stack;  int stack; /* Also contains the offset of control head. */
6662  struct sljit_label *loop = NULL;  struct sljit_label *loop = NULL;
6663  struct jump_list *emptymatch = NULL;  struct jump_list *emptymatch = NULL;
6664    
# Line 6462  switch(opcode) Line 6696  switch(opcode)
6696    break;    break;
6697    }    }
6698    
6699  framesize = get_framesize(common, cc, FALSE);  framesize = get_framesize(common, cc, NULL, FALSE, &needs_control_head);
6700  BACKTRACK_AS(bracketpos_backtrack)->framesize = framesize;  BACKTRACK_AS(bracketpos_backtrack)->framesize = framesize;
6701  if (framesize < 0)  if (framesize < 0)
6702    {    {
# Line 6475  if (framesize < 0) Line 6709  if (framesize < 0)
6709    else    else
6710      stacksize = 1;      stacksize = 1;
6711    
6712      if (needs_control_head)
6713        stacksize++;
6714    if (!zero)    if (!zero)
6715      stacksize++;      stacksize++;
6716    
# Line 6483  if (framesize < 0) Line 6719  if (framesize < 0)
6719    if (framesize == no_frame)    if (framesize == no_frame)
6720      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);
6721    
6722      stack = 0;
6723    if (offset != 0)    if (offset != 0)
6724      {      {
6725        stack = 2;
6726      OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(offset));      OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(offset));
6727      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));
6728      OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), TMP1, 0);      OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), TMP1, 0);
6729      if (common->capture_last_ptr != 0)      if (common->capture_last_ptr != 0)
6730        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);
6731      OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(1), TMP2, 0);      OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(1), TMP2, 0);
6732        if (needs_control_head)
6733          OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), common->control_head_ptr);
6734      if (common->capture_last_ptr != 0)      if (common->capture_last_ptr != 0)
6735          {
6736        OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(2), TMP1, 0);        OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(2), TMP1, 0);
6737          stack = 3;
6738          }
6739      }      }
6740    else    else
6741        {
6742        if (needs_control_head)
6743          OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), common->control_head_ptr);
6744      OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), STR_PTR, 0);      OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), STR_PTR, 0);
6745        stack = 1;
6746        }
6747    
6748      if (needs_control_head)
6749        stack++;
6750    if (!zero)    if (!zero)
6751      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);
6752      if (needs_control_head)
6753        {
6754        stack--;
6755        OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(stack), TMP2, 0);
6756        }
6757    }    }
6758  else  else
6759    {    {
6760    stacksize = framesize + 1;    stacksize = framesize + 1;
6761    if (!zero)    if (!zero)
6762      stacksize++;      stacksize++;
6763    if (opcode == OP_BRAPOS || opcode == OP_SBRAPOS)    if (needs_control_head)
6764        stacksize++;
6765      if (offset == 0)
6766      stacksize++;      stacksize++;
6767    BACKTRACK_AS(bracketpos_backtrack)->stacksize = stacksize;    BACKTRACK_AS(bracketpos_backtrack)->stacksize = stacksize;
6768    
6769    allocate_stack(common, stacksize);    allocate_stack(common, stacksize);
6770    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);
6771    OP2(SLJIT_SUB, TMP2, 0, STACK_TOP, 0, SLJIT_IMM, -STACK(stacksize - 1));    if (needs_control_head)
6772    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);
6773      OP2(SLJIT_SUB, SLJIT_MEM1(SLJIT_LOCALS_REG), private_data_ptr, STACK_TOP, 0, SLJIT_IMM, -STACK(stacksize - 1));
6774    
6775    stack = 0;    stack = 0;
6776    if (!zero)    if (!zero)
6777      {      {
6778      OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), SLJIT_IMM, 1);      OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), SLJIT_IMM, 1);
6779        stack = 1;
6780        }
6781      if (needs_control_head)
6782        {
6783        OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(stack), TMP2, 0);
6784      stack++;      stack++;
6785      }      }
6786    if (opcode == OP_BRAPOS || opcode == OP_SBRAPOS)    if (offset == 0)
6787      {      {
6788      OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(stack), STR_PTR, 0);      OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(stack), STR_PTR, 0);
6789      stack++;      stack++;
6790      }      }
6791    OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(stack), TMP1, 0);    OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(stack), TMP1, 0);
6792    init_frame(common, cc, stacksize - 1, stacksize - framesize, FALSE);    init_frame(common, cc, NULL, stacksize - 1, stacksize - framesize, FALSE);
6793      stack -= 1 + (offset == 0);
6794    }    }
6795    
6796  if (offset != 0)  if (offset != 0)
# Line 6602  while (*cc != OP_KETRPOS) Line 6866  while (*cc != OP_KETRPOS)
6866          OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), SLJIT_IMM, 0);          OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), SLJIT_IMM, 0);
6867        }        }
6868      }      }
6869    
6870      if (needs_control_head)
6871        OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), common->control_head_ptr, SLJIT_MEM1(STACK_TOP), STACK(stack));
6872    
6873    JUMPTO(SLJIT_JUMP, loop);    JUMPTO(SLJIT_JUMP, loop);
6874    flush_stubs(common);    flush_stubs(common);
6875    
# Line 6638  while (*cc != OP_KETRPOS) Line 6906  while (*cc != OP_KETRPOS)
6906    ccbegin = cc + 1 + LINK_SIZE;    ccbegin = cc + 1 + LINK_SIZE;
6907    }    }
6908    
6909    /* We don't have to restore the control head in case of a failed match. */
6910    
6911  backtrack->topbacktracks = NULL;  backtrack->topbacktracks = NULL;
6912  if (!zero)  if (!zero)
6913    {    {
# Line 6649  if (!zero) Line 6919  if (!zero)
6919    
6920  /* None of them matched. */  /* None of them matched. */
6921  set_jumps(emptymatch, LABEL());  set_jumps(emptymatch, LABEL());
6922  decrease_call_count(common);  count_match(common);
6923  return cc + 1 + LINK_SIZE;  return cc + 1 + LINK_SIZE;
6924  }  }
6925    
6926  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)
6927  {  {
6928  int class_len;  int class_len;
6929    
# Line 6689  else if (*opcode >= OP_TYPESTAR && *opco Line 6959  else if (*opcode >= OP_TYPESTAR && *opco
6959    }    }
6960  else  else
6961    {    {
6962    SLJIT_ASSERT(*opcode >= OP_CLASS || *opcode <= OP_XCLASS);    SLJIT_ASSERT(*opcode == OP_CLASS || *opcode == OP_NCLASS || *opcode == OP_XCLASS);
6963    *type = *opcode;    *type = *opcode;
6964    cc++;    cc++;
6965    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 6970  else
6970      if (end != NULL)      if (end != NULL)
6971        *end = cc + class_len;        *end = cc + class_len;
6972      }      }
6973      else if (*opcode >= OP_CRPOSSTAR && *opcode <= OP_CRPOSQUERY)
6974        {
6975        *opcode -= OP_CRPOSSTAR - OP_POSSTAR;
6976        if (end != NULL)
6977          *end = cc + class_len;
6978        }
6979    else    else
6980      {      {
6981      SLJIT_ASSERT(*opcode == OP_CRRANGE || *opcode == OP_CRMINRANGE);      SLJIT_ASSERT(*opcode == OP_CRRANGE || *opcode == OP_CRMINRANGE || *opcode == OP_CRPOSRANGE);
6982      *arg1 = GET2(cc, (class_len + IMM2_SIZE));      *max = GET2(cc, (class_len + IMM2_SIZE));
6983      *arg2 = GET2(cc, class_len);      *min = GET2(cc, class_len);
6984    
6985      if (*arg2 == 0)      if (*min == 0)
6986        {        {
6987        SLJIT_ASSERT(*arg1 != 0);        SLJIT_ASSERT(*max != 0);
6988        *opcode = (*opcode == OP_CRRANGE) ? OP_UPTO : OP_MINUPTO;        *opcode = (*opcode == OP_CRRANGE) ? OP_UPTO : (*opcode == OP_CRMINRANGE ? OP_MINUPTO : OP_POSUPTO);
6989        }        }
6990      if (*arg1 == *arg2)      if (*max == *min)
6991        *opcode = OP_EXACT;        *opcode = OP_EXACT;
6992    
6993      if (end != NULL)      if (end != NULL)
# Line 6722  else Line 6998  else
6998    
6999  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)
7000    {    {
7001    *arg1 = GET2(cc, 0);    *max = GET2(cc, 0);
7002    cc += IMM2_SIZE;    cc += IMM2_SIZE;
7003    }    }
7004    
# Line 6751  DEFINE_COMPILER; Line 7027  DEFINE_COMPILER;
7027  backtrack_common *backtrack;  backtrack_common *backtrack;
7028  pcre_uchar opcode;  pcre_uchar opcode;
7029  pcre_uchar type;  pcre_uchar type;
7030  int arg1 = -1, arg2 = -1;  int max = -1, min = -1;
7031  pcre_uchar* end;  pcre_uchar* end;
7032  jump_list *nomatch = NULL;  jump_list *nomatch = NULL;
7033  struct sljit_jump *jump = NULL;  struct sljit_jump *jump = NULL;
# Line 6764  int tmp_base, tmp_offset; Line 7040  int tmp_base, tmp_offset;
7040    
7041  PUSH_BACKTRACK(sizeof(iterator_backtrack), cc, NULL);  PUSH_BACKTRACK(sizeof(iterator_backtrack), cc, NULL);
7042    
7043  cc = get_iterator_parameters(common, cc, &opcode, &type, &arg1, &arg2, &end);  cc = get_iterator_parameters(common, cc, &opcode, &type, &max, &min, &end);
7044    
7045  switch(type)  switch(type)
7046    {    {
# Line 6835  switch(opcode) Line 7111  switch(opcode)
7111        {        {
7112        OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), POSSESSIVE0);        OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), POSSESSIVE0);
7113        OP2(SLJIT_ADD, TMP1, 0, TMP1, 0, SLJIT_IMM, 1);        OP2(SLJIT_ADD, TMP1, 0, TMP1, 0, SLJIT_IMM, 1);
7114        if (opcode == OP_CRRANGE && arg2 > 0)        if (opcode == OP_CRRANGE && min > 0)
7115          CMPTO(SLJIT_C_LESS, TMP1, 0, SLJIT_IMM, arg2, label);          CMPTO(SLJIT_C_LESS, TMP1, 0, SLJIT_IMM, min, label);
7116        if (opcode == OP_UPTO || (opcode == OP_CRRANGE && arg1 > 0))        if (opcode == OP_UPTO || (opcode == OP_CRRANGE && max > 0))
7117          jump = CMP(SLJIT_C_GREATER_EQUAL, TMP1, 0, SLJIT_IMM, arg1);          jump = CMP(SLJIT_C_GREATER_EQUAL, TMP1, 0, SLJIT_IMM, max);
7118        OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), POSSESSIVE0, TMP1, 0);        OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), POSSESSIVE0, TMP1, 0);
7119        }        }
7120    
# Line 6865  switch(opcode) Line 7141  switch(opcode)
7141      OP1(SLJIT_MOV, base, offset0, STR_PTR, 0);      OP1(SLJIT_MOV, base, offset0, STR_PTR, 0);
7142      if (opcode <= OP_PLUS)      if (opcode <= OP_PLUS)
7143        JUMPTO(SLJIT_JUMP, label);        JUMPTO(SLJIT_JUMP, label);
7144      else if (opcode == OP_CRRANGE && arg1 == 0)      else if (opcode == OP_CRRANGE && max == 0)
7145        {        {
7146        OP2(SLJIT_ADD, base, offset1, base, offset1, SLJIT_IMM, 1);        OP2(SLJIT_ADD, base, offset1, base, offset1, SLJIT_IMM, 1);
7147        JUMPTO(SLJIT_JUMP, label);        JUMPTO(SLJIT_JUMP, label);
# Line 6875  switch(opcode) Line 7151  switch(opcode)
7151        OP1(SLJIT_MOV, TMP1, 0, base, offset1);        OP1(SLJIT_MOV, TMP1, 0, base, offset1);
7152        OP2(SLJIT_ADD, TMP1, 0, TMP1, 0, SLJIT_IMM, 1);        OP2(SLJIT_ADD, TMP1, 0, TMP1, 0, SLJIT_IMM, 1);
7153        OP1(SLJIT_MOV, base, offset1, TMP1, 0);        OP1(SLJIT_MOV, base, offset1, TMP1, 0);
7154        CMPTO(SLJIT_C_LESS, TMP1, 0, SLJIT_IMM, arg1 + 1, label);        CMPTO(SLJIT_C_LESS, TMP1, 0, SLJIT_IMM, max + 1, label);
7155        }        }
7156      set_jumps(nomatch, LABEL());      set_jumps(nomatch, LABEL());
7157      if (opcode == OP_CRRANGE)      if (opcode == OP_CRRANGE)
7158        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));
7159      OP1(SLJIT_MOV, STR_PTR, 0, base, offset0);      OP1(SLJIT_MOV, STR_PTR, 0, base, offset0);
7160      }      }
7161    BACKTRACK_AS(iterator_backtrack)->matchingpath = LABEL();    BACKTRACK_AS(iterator_backtrack)->matchingpath = LABEL();
# Line 6917  switch(opcode) Line 7193  switch(opcode)
7193    break;    break;
7194    
7195    case OP_EXACT:    case OP_EXACT:
7196    OP1(SLJIT_MOV, tmp_base, tmp_offset, SLJIT_IMM, arg1);    OP1(SLJIT_MOV, tmp_base, tmp_offset, SLJIT_IMM, max);
7197    label = LABEL();    label = LABEL();
7198    compile_char1_matchingpath(common, type, cc, &backtrack->topbacktracks);    compile_char1_matchingpath(common, type, cc, &backtrack->topbacktracks);
7199    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 7206  switch(opcode)
7206    if (opcode == OP_POSPLUS)    if (opcode == OP_POSPLUS)
7207      compile_char1_matchingpath(common, type, cc, &backtrack->topbacktracks);      compile_char1_matchingpath(common, type, cc, &backtrack->topbacktracks);
7208    if (opcode == OP_POSUPTO)    if (opcode == OP_POSUPTO)
7209      OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), POSSESSIVE1, SLJIT_IMM, arg1);      OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), POSSESSIVE1, SLJIT_IMM, max);
7210    OP1(SLJIT_MOV, tmp_base, tmp_offset, STR_PTR, 0);    OP1(SLJIT_MOV, tmp_base, tmp_offset, STR_PTR, 0);
7211    label = LABEL();    label = LABEL();
7212    compile_char1_matchingpath(common, type, cc, &nomatch);    compile_char1_matchingpath(common, type, cc, &nomatch);
# Line 6946  switch(opcode) Line 7222  switch(opcode)
7222    OP1(SLJIT_MOV, STR_PTR, 0, tmp_base, tmp_offset);    OP1(SLJIT_MOV, STR_PTR, 0, tmp_base, tmp_offset);
7223    break;    break;
7224    
7225    case OP_POSQUERY:    case OP_POSQUERY:
7226    OP1(SLJIT_MOV, tmp_base, tmp_offset, STR_PTR, 0);    OP1(SLJIT_MOV, tmp_base, tmp_offset, STR_PTR, 0);
7227    compile_char1_matchingpath(common, type, cc, &nomatch);    compile_char1_matchingpath(common, type, cc, &nomatch);
7228    OP1(SLJIT_MOV, tmp_base, tmp_offset, STR_PTR, 0);    OP1(SLJIT_MOV, tmp_base, tmp_offset, STR_PTR, 0);
7229    set_jumps(nomatch, LABEL());    set_jumps(nomatch, LABEL());
7230    OP1(SLJIT_MOV, STR_PTR, 0, tmp_base, tmp_offset);    OP1(SLJIT_MOV, STR_PTR, 0, tmp_base, tmp_offset);
7231    break;    break;
7232    
7233      case OP_CRPOSRANGE:
7234      /* Combination of OP_EXACT and OP_POSSTAR or OP_POSUPTO */
7235      OP1(SLJIT_MOV, tmp_base, tmp_offset, SLJIT_IMM, min);
7236      label = LABEL();
7237      compile_char1_matchingpath(common, type, cc, &backtrack->topbacktracks);
7238      OP2(SLJIT_SUB | SLJIT_SET_E, tmp_base, tmp_offset, tmp_base, tmp_offset, SLJIT_IMM, 1);
7239      JUMPTO(SLJIT_C_NOT_ZERO, label);
7240    
7241      if (max != 0)
7242        {
7243        SLJIT_ASSERT(max - min > 0);
7244        OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), POSSESSIVE1, SLJIT_IMM, max - min);
7245        }
7246      OP1(SLJIT_MOV, tmp_base, tmp_offset, STR_PTR, 0);
7247      label = LABEL();
7248      compile_char1_matchingpath(common, type, cc, &nomatch);
7249      OP1(SLJIT_MOV, tmp_base, tmp_offset, STR_PTR, 0);
7250      if (max == 0)
7251        JUMPTO(SLJIT_JUMP, label);
7252      else
7253        {
7254        OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_MEM1(SLJIT_LOCALS_REG), POSSESSIVE1, SLJIT_MEM1(SLJIT_LOCALS_REG), POSSESSIVE1, SLJIT_IMM, 1);
7255        JUMPTO(SLJIT_C_NOT_ZERO, label);
7256        }
7257      set_jumps(nomatch, LABEL());
7258      OP1(SLJIT_MOV, STR_PTR, 0, tmp_base, tmp_offset);
7259      break;
7260    
7261    default:    default:
7262    SLJIT_ASSERT_STOP();    SLJIT_ASSERT_STOP();
7263    break;    break;
7264    }    }
7265    
7266  decrease_call_count(common);  count_match(common);
7267  return end;  return end;
7268  }  }
7269    
# Line 7026  if (!optimized_cbracket) Line 7330  if (!optimized_cbracket)
7330  return cc + 1 + IMM2_SIZE;  return cc + 1 + IMM2_SIZE;
7331  }  }
7332    
7333    static SLJIT_INLINE pcre_uchar *compile_control_verb_matchingpath(compiler_common *common, pcre_uchar *cc, backtrack_common *parent)
7334    {
7335    DEFINE_COMPILER;
7336    backtrack_common *backtrack;
7337    pcre_uchar opcode = *cc;
7338    pcre_uchar *ccend = cc + 1;
7339    
7340    if (opcode == OP_PRUNE_ARG || opcode == OP_SKIP_ARG || opcode == OP_THEN_ARG)
7341      ccend += 2 + cc[1];
7342    
7343    PUSH_BACKTRACK(sizeof(backtrack_common), cc, NULL);
7344    
7345    if (opcode == OP_SKIP)
7346      {
7347      allocate_stack(common, 1);
7348      OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), STR_PTR, 0);
7349      return ccend;
7350      }
7351    
7352    if (opcode == OP_PRUNE_ARG || opcode == OP_THEN_ARG)
7353      {
7354      OP1(SLJIT_MOV, TMP1, 0, ARGUMENTS, 0);
7355      OP1(SLJIT_MOV, TMP2, 0, SLJIT_IMM, (sljit_sw)(cc + 2));
7356      OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), common->mark_ptr, TMP2, 0);
7357      OP1(SLJIT_MOV, SLJIT_MEM1(TMP1), SLJIT_OFFSETOF(jit_arguments, mark_ptr), TMP2, 0);
7358      }
7359    
7360    return ccend;
7361    }
7362    
7363    static pcre_uchar then_trap_opcode[1] = { OP_THEN_TRAP };
7364    
7365    static SLJIT_INLINE void compile_then_trap_matchingpath(compiler_common *common, pcre_uchar *cc, pcre_uchar *ccend, backtrack_common *parent)
7366    {
7367    DEFINE_COMPILER;
7368    backtrack_common *backtrack;
7369    BOOL needs_control_head;
7370    int size;
7371    
7372    PUSH_BACKTRACK_NOVALUE(sizeof(then_trap_backtrack), cc);
7373    common->then_trap = BACKTRACK_AS(then_trap_backtrack);
7374    BACKTRACK_AS(then_trap_backtrack)->common.cc = then_trap_opcode;
7375    BACKTRACK_AS(then_trap_backtrack)->start = (sljit_sw)(cc - common->start);
7376    BACKTRACK_AS(then_trap_backtrack)->framesize = get_framesize(common, cc, ccend, FALSE, &needs_control_head);
7377    
7378    size = BACKTRACK_AS(then_trap_backtrack)->framesize;
7379    size = 3 + (size < 0 ? 0 : size);
7380    
7381    OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), common->control_head_ptr);
7382    allocate_stack(common, size);
7383    if (size > 3)
7384      OP2(SLJIT_SUB, SLJIT_MEM1(SLJIT_LOCALS_REG), common->control_head_ptr, STACK_TOP, 0, SLJIT_IMM, (size - 3) * sizeof(sljit_sw));
7385    else
7386      OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), common->control_head_ptr, STACK_TOP, 0);
7387    OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(size - 1), SLJIT_IMM, BACKTRACK_AS(then_trap_backtrack)->start);
7388    OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(size - 2), SLJIT_IMM, type_then_trap);
7389    OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(size - 3), TMP2, 0);
7390    
7391    size = BACKTRACK_AS(then_trap_backtrack)->framesize;
7392    if (size >= 0)
7393      init_frame(common, cc, ccend, size - 1, 0, FALSE);
7394    }
7395    
7396  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)
7397  {  {
7398  DEFINE_COMPILER;  DEFINE_COMPILER;
7399  backtrack_common *backtrack;  backtrack_common *backtrack;
7400    BOOL has_then_trap = FALSE;
7401    then_trap_backtrack *save_then_trap = NULL;
7402    
7403    SLJIT_ASSERT(*ccend == OP_END || (*ccend >= OP_ALT && *ccend <= OP_KETRPOS));
7404    
7405    if (common->has_then && common->then_offsets[cc - common->start] != 0)
7406      {
7407      SLJIT_ASSERT(*ccend != OP_END && common->control_head_ptr != 0);
7408      has_then_trap = TRUE;
7409      save_then_trap = common->then_trap;
7410      /* Tail item on backtrack. */
7411      compile_then_trap_matchingpath(common, cc, ccend, parent);
7412      }
7413    
7414  while (cc < ccend)  while (cc < ccend)
7415    {    {
# Line 7155  while (cc < ccend) Line 7535  while (cc < ccend)
7535    
7536      case OP_CLASS:      case OP_CLASS:
7537      case OP_NCLASS:      case OP_NCLASS:
7538      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)
7539        cc = compile_iterator_matchingpath(common, cc, parent);        cc = compile_iterator_matchingpath(common, cc, parent);
7540      else      else
7541        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 7543  while (cc < ccend)
7543    
7544  #if defined SUPPORT_UTF || defined COMPILE_PCRE16 || defined COMPILE_PCRE32  #if defined SUPPORT_UTF || defined COMPILE_PCRE16 || defined COMPILE_PCRE32
7545      case OP_XCLASS:      case OP_XCLASS:
7546      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)
7547        cc = compile_iterator_matchingpath(common, cc, parent);        cc = compile_iterator_matchingpath(common, cc, parent);
7548      else      else
7549        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 7552  while (cc < ccend)
7552    
7553      case OP_REF:      case OP_REF:
7554      case OP_REFI:      case OP_REFI:
7555      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)
7556        cc = compile_ref_iterator_matchingpath(common, cc, parent);        cc = compile_ref_iterator_matchingpath(common, cc, parent);
7557      else      else
7558        cc = compile_ref_matchingpath(common, cc, parent->top != NULL ? &parent->top->nextbacktracks : &parent->topbacktracks, TRUE, FALSE);        {
7559          compile_ref_matchingpath(common, cc, parent->top != NULL ? &parent->top->nextbacktracks : &parent->topbacktracks, TRUE, FALSE);
7560          cc += 1 + IMM2_SIZE;
7561          }
7562        break;
7563    
7564        case OP_DNREF:
7565        case OP_DNREFI:
7566        if (cc[1 + 2 * IMM2_SIZE] >= OP_CRSTAR && cc[1 + 2 * IMM2_SIZE] <= OP_CRPOSRANGE)
7567          cc = compile_ref_iterator_matchingpath(common, cc, parent);
7568        else
7569          {
7570          compile_dnref_search(common, cc, parent->top != NULL ? &parent->top->nextbacktracks : &parent->topbacktracks);
7571          compile_ref_matchingpath(common, cc, parent->top != NULL ? &parent->top->nextbacktracks : &parent->topbacktracks, TRUE, FALSE);
7572          cc += 1 + 2 * IMM2_SIZE;
7573          }
7574      break;      break;
7575    
7576      case OP_RECURSE:      case OP_RECURSE:
# Line 7210  while (cc < ccend) Line 7605  while (cc < ccend)
7605        }        }
7606      BACKTRACK_AS(braminzero_backtrack)->matchingpath = LABEL();      BACKTRACK_AS(braminzero_backtrack)->matchingpath = LABEL();
7607      if (cc[1] > OP_ASSERTBACK_NOT)      if (cc[1] > OP_ASSERTBACK_NOT)
7608        decrease_call_count(common);        count_match(common);
7609      break;      break;
7610    
7611      case OP_ONCE:      case OP_ONCE:
# Line 7246  while (cc < ccend) Line 7641  while (cc < ccend)
7641      PUSH_BACKTRACK_NOVALUE(sizeof(backtrack_common), cc);      PUSH_BACKTRACK_NOVALUE(sizeof(backtrack_common), cc);
7642      SLJIT_ASSERT(common->mark_ptr != 0);      SLJIT_ASSERT(common->mark_ptr != 0);
7643      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);
7644      allocate_stack(common, 1);      allocate_stack(common, common->has_skip_arg ? 5 : 1);
7645      OP1(SLJIT_MOV, TMP1, 0, ARGUMENTS, 0);      OP1(SLJIT_MOV, TMP1, 0, ARGUMENTS, 0);
7646      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);
7647      OP1(SLJIT_MOV, TMP2, 0, SLJIT_IMM, (sljit_sw)(cc + 2));      OP1(SLJIT_MOV, TMP2, 0, SLJIT_IMM, (sljit_sw)(cc + 2));
7648      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);
7649      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);
7650        if (common->has_skip_arg)
7651          {
7652          OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), common->control_head_ptr);
7653          OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), common->control_head_ptr, STACK_TOP, 0);
7654          OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(1), SLJIT_IMM, type_mark);
7655          OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(2), SLJIT_IMM, (sljit_sw)(cc + 2));
7656          OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(3), STR_PTR, 0);
7657          OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), TMP1, 0);
7658          }
7659      cc += 1 + 2 + cc[1];      cc += 1 + 2 + cc[1];
7660      break;      break;
7661    
     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. */  
   
7662      case OP_PRUNE:      case OP_PRUNE:
7663      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;  
   
7664      case OP_SKIP:      case OP_SKIP:
7665      PUSH_BACKTRACK_NOVALUE(sizeof(backtrack_common), cc);      case OP_SKIP_ARG:
7666      OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), common->control_head_ptr);      case OP_THEN:
7667      allocate_stack(common, 3);      case OP_THEN_ARG:
7668      OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), common->control_head_ptr, STACK_TOP, 0);      case OP_COMMIT:
7669      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;  
7670      break;      break;
7671    
7672      case OP_FAIL:      case OP_FAIL:
# Line 7306  while (cc < ccend) Line 7690  while (cc < ccend)
7690    if (cc == NULL)    if (cc == NULL)
7691      return;      return;
7692    }    }
7693    
7694    if (has_then_trap)
7695      {
7696      /* Head item on backtrack. */
7697      PUSH_BACKTRACK_NOVALUE(sizeof(then_trap_backtrack), cc);
7698      BACKTRACK_AS(then_trap_backtrack)->common.cc = then_trap_opcode;
7699      BACKTRACK_AS(then_trap_backtrack)->then_trap = common->then_trap;
7700      common->then_trap = save_then_trap;
7701      }
7702  SLJIT_ASSERT(cc == ccend);  SLJIT_ASSERT(cc == ccend);
7703  }  }
7704    
# Line 7330  DEFINE_COMPILER; Line 7723  DEFINE_COMPILER;
7723  pcre_uchar *cc = current->cc;  pcre_uchar *cc = current->cc;
7724  pcre_uchar opcode;  pcre_uchar opcode;
7725  pcre_uchar type;  pcre_uchar type;
7726  int arg1 = -1, arg2 = -1;  int max = -1, min = -1;
7727  struct sljit_label *label = NULL;  struct sljit_label *label = NULL;
7728  struct sljit_jump *jump = NULL;  struct sljit_jump *jump = NULL;
7729  jump_list *jumplist = NULL;  jump_list *jumplist = NULL;
# Line 7339  int base = (private_data_ptr == 0) ? SLJ Line 7732  int base = (private_data_ptr == 0) ? SLJ
7732  int offset0 = (private_data_ptr == 0) ? STACK(0) : private_data_ptr;  int offset0 = (private_data_ptr == 0) ? STACK(0) : private_data_ptr;
7733  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);
7734    
7735  cc = get_iterator_parameters(common, cc, &opcode, &type, &arg1, &arg2, NULL);  cc = get_iterator_parameters(common, cc, &opcode, &type, &max, &min, NULL);
7736    
7737  switch(opcode)  switch(opcode)
7738    {    {
# Line 7358  switch(opcode) Line 7751  switch(opcode)
7751    else    else
7752      {      {
7753      if (opcode == OP_UPTO)      if (opcode == OP_UPTO)
7754        arg2 = 0;        min = 0;
7755      if (opcode <= OP_PLUS)      if (opcode <= OP_PLUS)
7756        {        {
7757        OP1(SLJIT_MOV, STR_PTR, 0, base, offset0);        OP1(SLJIT_MOV, STR_PTR, 0, base, offset0);
# Line 7368  switch(opcode) Line 7761  switch(opcode)
7761        {        {
7762        OP1(SLJIT_MOV, TMP1, 0, base, offset1);        OP1(SLJIT_MOV, TMP1, 0, base, offset1);
7763        OP1(SLJIT_MOV, STR_PTR, 0, base, offset0);        OP1(SLJIT_MOV, STR_PTR, 0, base, offset0);
7764        jump = CMP(SLJIT_C_LESS_EQUAL, TMP1, 0, SLJIT_IMM, arg2 + 1);        jump = CMP(SLJIT_C_LESS_EQUAL, TMP1, 0, SLJIT_IMM, min + 1);
7765        OP2(SLJIT_SUB, base, offset1, TMP1, 0, SLJIT_IMM, 1);        OP2(SLJIT_SUB, base, offset1, TMP1, 0, SLJIT_IMM, 1);
7766        }        }
7767      skip_char_back(common);      skip_char_back(common);
# Line 7413  switch(opcode) Line 7806  switch(opcode)
7806    OP1(SLJIT_MOV, base, offset1, TMP1, 0);    OP1(SLJIT_MOV, base, offset1, TMP1, 0);
7807    
7808    if (opcode == OP_CRMINRANGE)    if (opcode == OP_CRMINRANGE)
7809      CMPTO(SLJIT_C_LESS, TMP1, 0, SLJIT_IMM, arg2 + 1, label);      CMPTO(SLJIT_C_LESS, TMP1, 0, SLJIT_IMM, min + 1, label);
7810    
7811    if (opcode == OP_CRMINRANGE && arg1 == 0)    if (opcode == OP_CRMINRANGE && max == 0)
7812      JUMPTO(SLJIT_JUMP, CURRENT_AS(iterator_backtrack)->matchingpath);      JUMPTO(SLJIT_JUMP, CURRENT_AS(iterator_backtrack)->matchingpath);
7813    else    else
7814      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);
7815    
7816    set_jumps(jumplist, LABEL());    set_jumps(jumplist, LABEL());
7817    if (private_data_ptr == 0)    if (private_data_ptr == 0)
# Line 7453  switch(opcode) Line 7846  switch(opcode)
7846    
7847    case OP_EXACT:    case OP_EXACT:
7848    case OP_POSPLUS:    case OP_POSPLUS:
7849      case OP_CRPOSRANGE:
7850    set_jumps(current->topbacktracks, LABEL());    set_jumps(current->topbacktracks, LABEL());
7851    break;    break;
7852    
# Line 7467  switch(opcode) Line 7861  switch(opcode)
7861    }    }
7862  }  }
7863    
7864  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)
7865  {  {
7866  DEFINE_COMPILER;  DEFINE_COMPILER;
7867  pcre_uchar *cc = current->cc;  pcre_uchar *cc = current->cc;
7868    BOOL ref = (*cc == OP_REF || *cc == OP_REFI);
7869  pcre_uchar type;  pcre_uchar type;
7870    
7871  type = cc[1 + IMM2_SIZE];  type = cc[ref ? 1 + IMM2_SIZE : 1 + 2 * IMM2_SIZE];
7872    
7873  if ((type & 0x1) == 0)  if ((type & 0x1) == 0)
7874    {    {
7875      /* Maximize case. */
7876    set_jumps(current->topbacktracks, LABEL());    set_jumps(current->topbacktracks, LABEL());
7877    OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(STACK_TOP), STACK(0));    OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(STACK_TOP), STACK(0));
7878    free_stack(common, 1);    free_stack(common, 1);
# Line 7486  if ((type & 0x1) == 0) Line 7883  if ((type & 0x1) == 0)
7883  OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(STACK_TOP), STACK(0));  OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(STACK_TOP), STACK(0));
7884  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);
7885  set_jumps(current->topbacktracks, LABEL());  set_jumps(current->topbacktracks, LABEL());
7886  free_stack(common, 2);  free_stack(common, ref ? 2 : 3);
7887  }  }
7888    
7889  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)
7890  {  {
7891  DEFINE_COMPILER;  DEFINE_COMPILER;
7892    
# Line 7585  if (bra == OP_BRAZERO) Line 7982  if (bra == OP_BRAZERO)
7982  static void compile_bracket_backtrackingpath(compiler_common *common, struct backtrack_common *current)  static void compile_bracket_backtrackingpath(compiler_common *common, struct backtrack_common *current)
7983  {  {
7984  DEFINE_COMPILER;  DEFINE_COMPILER;
7985  int opcode;  int opcode, stacksize, count;
7986  int offset = 0;  int offset = 0;
7987  int private_data_ptr = CURRENT_AS(bracket_backtrack)->private_data_ptr;  int private_data_ptr = CURRENT_AS(bracket_backtrack)->private_data_ptr;
7988  int stacksize;  int repeat_ptr = 0, repeat_type = 0, repeat_count = 0;
 int count;  
7989  pcre_uchar *cc = current->cc;  pcre_uchar *cc = current->cc;
7990  pcre_uchar *ccbegin;  pcre_uchar *ccbegin;
7991  pcre_uchar *ccprev;  pcre_uchar *ccprev;
# Line 7599  pcre_uchar bra = OP_BRA; Line 7995  pcre_uchar bra = OP_BRA;
7995  pcre_uchar ket;  pcre_uchar ket;
7996  assert_backtrack *assert;  assert_backtrack *assert;
7997  BOOL has_alternatives;  BOOL has_alternatives;
7998    BOOL needs_control_head = FALSE;
7999  struct sljit_jump *brazero = NULL;  struct sljit_jump *brazero = NULL;
8000  struct sljit_jump *once = NULL;  struct sljit_jump *once = NULL;
8001  struct sljit_jump *cond = NULL;  struct sljit_jump *cond = NULL;
8002  struct sljit_label *rminlabel = NULL;  struct sljit_label *rmin_label = NULL;
8003    struct sljit_label *exact_label = NULL;
8004    
8005  if (*cc == OP_BRAZERO || *cc == OP_BRAMINZERO)  if (*cc == OP_BRAZERO || *cc == OP_BRAMINZERO)
8006    {    {
# Line 7611  if (*cc == OP_BRAZERO || *cc == OP_BRAMI Line 8009  if (*cc == OP_BRAZERO || *cc == OP_BRAMI
8009    }    }
8010    
8011  opcode = *cc;  opcode = *cc;
8012    ccbegin = bracketend(cc) - 1 - LINK_SIZE;
8013    ket = *ccbegin;
8014    if (ket == OP_KET && PRIVATE_DATA(ccbegin) != 0)
8015      {
8016      repeat_ptr = PRIVATE_DATA(ccbegin);
8017      repeat_type = PRIVATE_DATA(ccbegin + 2);
8018      repeat_count = PRIVATE_DATA(ccbegin + 3);
8019      SLJIT_ASSERT(repeat_type != 0 && repeat_count != 0);
8020      if (repeat_type == OP_UPTO)
8021        ket = OP_KETRMAX;
8022      if (repeat_type == OP_MINUPTO)
8023        ket = OP_KETRMIN;
8024      }
8025  ccbegin = cc;  ccbegin = cc;
 ket = *(bracketend(ccbegin) - 1 - LINK_SIZE);  
8026  cc += GET(cc, 1);  cc += GET(cc, 1);
8027  has_alternatives = *cc == OP_ALT;  has_alternatives = *cc == OP_ALT;
8028  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 8034  if (SLJIT_UNLIKELY(opcode == OP_COND) &&
8034  if (SLJIT_UNLIKELY(opcode == OP_ONCE_NC))  if (SLJIT_UNLIKELY(opcode == OP_ONCE_NC))
8035    opcode = OP_ONCE;    opcode = OP_ONCE;
8036    
8037    /* Decoding the needs_control_head in framesize. */
8038    if (opcode == OP_ONCE)
8039      {
8040      needs_control_head = (CURRENT_AS(bracket_backtrack)->u.framesize & 0x1) != 0;
8041      CURRENT_AS(bracket_backtrack)->u.framesize >>= 1;
8042      }
8043    
8044    if (ket != OP_KET && repeat_type != 0)
8045      {
8046      /* TMP1 is used in OP_KETRMIN below. */
8047      OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(STACK_TOP), STACK(0));
8048      free_stack(common, 1);
8049      if (repeat_type == OP_UPTO)
8050        OP2(SLJIT_ADD, SLJIT_MEM1(SLJIT_LOCALS_REG), repeat_ptr, TMP1, 0, SLJIT_IMM, 1);
8051      else
8052        OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), repeat_ptr, TMP1, 0);
8053      }
8054    
8055  if (ket == OP_KETRMAX)  if (ket == OP_KETRMAX)
8056    {    {
8057    if (bra == OP_BRAZERO)    if (bra == OP_BRAZERO)
# Line 7638  else if (ket == OP_KETRMIN) Line 8066  else if (ket == OP_KETRMIN)
8066    if (bra != OP_BRAMINZERO)    if (bra != OP_BRAMINZERO)
8067      {      {
8068      OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(STACK_TOP), STACK(0));      OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(STACK_TOP), STACK(0));
8069      if (opcode >= OP_SBRA || opcode == OP_ONCE)      if (repeat_type != 0)
8070          {
8071          /* TMP1 was set a few lines above. */
8072          CMPTO(SLJIT_C_NOT_EQUAL, TMP1, 0, SLJIT_IMM, 0, CURRENT_AS(bracket_backtrack)->recursive_matchingpath);
8073          /* Drop STR_PTR for non-greedy plus quantifier. */
8074          if (opcode != OP_ONCE)
8075            free_stack(common, 1);
8076          }
8077        else if (opcode >= OP_SBRA || opcode == OP_ONCE)
8078        {        {
8079        /* Checking zero-length iteration. */        /* Checking zero-length iteration. */
8080        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 8084  else if (ket == OP_KETRMIN)
8084          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);
8085          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);
8086          }          }
8087          /* Drop STR_PTR for non-greedy plus quantifier. */
8088        if (opcode != OP_ONCE)        if (opcode != OP_ONCE)
8089          free_stack(common, 1);          free_stack(common, 1);
8090        }        }
8091      else      else
8092        JUMPTO(SLJIT_JUMP, CURRENT_AS(bracket_backtrack)->recursive_matchingpath);        JUMPTO(SLJIT_JUMP, CURRENT_AS(bracket_backtrack)->recursive_matchingpath);
8093      }      }
8094    rminlabel = LABEL();    rmin_label = LABEL();
8095      if (repeat_type != 0)
8096        OP2(SLJIT_ADD, SLJIT_MEM1(SLJIT_LOCALS_REG), repeat_ptr, SLJIT_MEM1(SLJIT_LOCALS_REG), repeat_ptr, SLJIT_IMM, 1);
8097    }    }
8098  else if (bra == OP_BRAZERO)  else if (bra == OP_BRAZERO)
8099    {    {
# Line 7662  else if (bra == OP_BRAZERO) Line 8101  else if (bra == OP_BRAZERO)
8101    free_stack(common, 1);    free_stack(common, 1);
8102    brazero = CMP(SLJIT_C_NOT_EQUAL, TMP1, 0, SLJIT_IMM, 0);    brazero = CMP(SLJIT_C_NOT_EQUAL, TMP1, 0, SLJIT_IMM, 0);
8103    }    }
8104    else if (repeat_type == OP_EXACT)
8105      {
8106      OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), repeat_ptr, SLJIT_IMM, 1);
8107      exact_label = LABEL();
8108      }
8109    
8110  if (offset != 0)  if (offset != 0)
8111    {    {
# Line 7781  if (has_alternatives) Line 8225  if (has_alternatives)
8225      current->top = NULL;      current->top = NULL;
8226      current->topbacktracks = NULL;      current->topbacktracks = NULL;
8227      current->nextbacktracks = NULL;      current->nextbacktracks = NULL;
8228        /* Conditional blocks always have an additional alternative, even if it is empty. */
8229      if (*cc == OP_ALT)      if (*cc == OP_ALT)
8230        {        {
8231        ccprev = cc + 1 + LINK_SIZE;        ccprev = cc + 1 + LINK_SIZE;
8232        cc += GET(cc, 1);        cc += GET(cc, 1);
8233        if (opcode != OP_COND && opcode != OP_SCOND)        if (opcode != OP_COND && opcode != OP_SCOND)
8234          {          {
8235          if (private_data_ptr != 0 && opcode != OP_ONCE)          if (opcode != OP_ONCE)
8236            OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), private_data_ptr);            {
8237              if (private_data_ptr != 0)
8238                OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), private_data_ptr);
8239              else
8240                OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(STACK_TOP), STACK(0));
8241              }
8242          else          else
8243            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));
8244          }          }
8245        compile_matchingpath(common, ccprev, cc, current);        compile_matchingpath(common, ccprev, cc, current);
8246        if (SLJIT_UNLIKELY(sljit_get_compiler_error(compiler)))        if (SLJIT_UNLIKELY(sljit_get_compiler_error(compiler)))
8247          return;          return;
8248        }        }
8249    
8250      /* Instructions after the current alternative is succesfully matched. */      /* Instructions after the current alternative is successfully matched. */
8251      /* There is a similar code in compile_bracket_matchingpath. */      /* There is a similar code in compile_bracket_matchingpath. */
8252      if (opcode == OP_ONCE)      if (opcode == OP_ONCE)
8253        {        match_once_common(common, ket, CURRENT_AS(bracket_backtrack)->u.framesize, private_data_ptr, has_alternatives, needs_control_head);
       if (CURRENT_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  
         {  
         OP2(SLJIT_ADD, STACK_TOP, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), private_data_ptr, SLJIT_IMM, (CURRENT_AS(bracket_backtrack)->u.framesize + 2) * 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));  
           }  
         }  
       }  
8254    
8255      stacksize = 0;      stacksize = 0;
8256        if (repeat_type == OP_MINUPTO)
8257          {
8258          /* We need to preserve the counter. TMP2 will be used below. */
8259          OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), repeat_ptr);
8260          stacksize++;
8261          }
8262      if (ket != OP_KET || bra != OP_BRA)      if (ket != OP_KET || bra != OP_BRA)
8263        stacksize++;        stacksize++;
8264      if (offset != 0)      if (offset != 0)
# Line 7837  if (has_alternatives) Line 8271  if (has_alternatives)
8271      if (opcode != OP_ONCE)      if (opcode != OP_ONCE)
8272        stacksize++;        stacksize++;
8273    
8274      if (stacksize > 0) {      if (stacksize > 0)
8275        if (opcode != OP_ONCE || CURRENT_AS(bracket_backtrack)->u.framesize >= 0)        allocate_stack(common, stacksize);
         allocate_stack(common, stacksize);  
       else  
         {  
         /* We know we have place at least for one item on the top of the stack. */  
         SLJIT_ASSERT(stacksize == 1);  
         OP2(SLJIT_ADD, STACK_TOP, 0, STACK_TOP, 0, SLJIT_IMM, sizeof(sljit_sw));  
         }  
     }  
8276    
8277      stacksize = 0;      stacksize = 0;
8278        if (repeat_type == OP_MINUPTO)
8279          {
8280          /* TMP2 was set above. */
8281          OP2(SLJIT_SUB, SLJIT_MEM1(STACK_TOP), STACK(stacksize), TMP2, 0, SLJIT_IMM, 1);
8282          stacksize++;
8283          }
8284    
8285      if (ket != OP_KET || bra != OP_BRA)      if (ket != OP_KET || bra != OP_BRA)
8286        {        {
8287        if (ket != OP_KET)        if (ket != OP_KET)
# Line 7859  if (has_alternatives) Line 8292  if (has_alternatives)
8292        }        }
8293    
8294      if (offset != 0)      if (offset != 0)
8295        {        stacksize = match_capture_common(common, stacksize, offset, private_data_ptr);
       if (common->capture_last_ptr != 0)  
         {  
         OP1(SLJIT_MOV, TMP1, 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), TMP1, 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;  
         }  
       }  
8296    
8297      if (opcode != OP_ONCE)      if (opcode != OP_ONCE)
8298        OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(stacksize), SLJIT_IMM, count++);        OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(stacksize), SLJIT_IMM, count++);
# Line 7951  else if (opcode == OP_SBRA || opcode == Line 8365  else if (opcode == OP_SBRA || opcode ==
8365  else if (opcode == OP_ONCE)  else if (opcode == OP_ONCE)
8366    {    {
8367    cc = ccbegin + GET(ccbegin, 1);    cc = ccbegin + GET(ccbegin, 1);
8368      stacksize = needs_control_head ? 1 : 0;
8369    
8370    if (CURRENT_AS(bracket_backtrack)->u.framesize >= 0)    if (CURRENT_AS(bracket_backtrack)->u.framesize >= 0)
8371      {      {
8372      /* Reset head and drop saved frame. */      /* Reset head and drop saved frame. */
8373      stacksize = (ket == OP_KETRMAX || ket == OP_KETRMIN || *cc == OP_ALT) ? 2 : 1;      stacksize += CURRENT_AS(bracket_backtrack)->u.framesize + ((ket != OP_KET || *cc == OP_ALT) ? 2 : 1);
     free_stack(common, CURRENT_AS(bracket_backtrack)->u.framesize + stacksize);  
8374      }      }
8375    else if (ket == OP_KETRMAX || (*cc == OP_ALT && ket != OP_KETRMIN))    else if (ket == OP_KETRMAX || (*cc == OP_ALT && ket != OP_KETRMIN))
8376      {      {
8377      /* The STR_PTR must be released. */      /* The STR_PTR must be released. */
8378      free_stack(common, 1);      stacksize++;
8379      }      }
8380      free_stack(common, stacksize);
8381    
8382    JUMPHERE(once);    JUMPHERE(once);
8383    /* Restore previous private_data_ptr */    /* Restore previous private_data_ptr */
# Line 7976  else if (opcode == OP_ONCE) Line 8392  else if (opcode == OP_ONCE)
8392      }      }
8393    }    }
8394    
8395  if (ket == OP_KETRMAX)  if (repeat_type == OP_EXACT)
8396      {
8397      OP2(SLJIT_ADD, TMP1, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), repeat_ptr, SLJIT_IMM, 1);
8398      OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), repeat_ptr, TMP1, 0);
8399      CMPTO(SLJIT_C_LESS_EQUAL, TMP1, 0, SLJIT_IMM, repeat_count, exact_label);
8400      }
8401    else if (ket == OP_KETRMAX)
8402    {    {
8403    OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(STACK_TOP), STACK(0));    OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(STACK_TOP), STACK(0));
8404    if (bra != OP_BRAZERO)    if (bra != OP_BRAZERO)
8405      free_stack(common, 1);      free_stack(common, 1);
8406    
8407    CMPTO(SLJIT_C_NOT_EQUAL, STR_PTR, 0, SLJIT_IMM, 0, CURRENT_AS(bracket_backtrack)->recursive_matchingpath);    CMPTO(SLJIT_C_NOT_EQUAL, STR_PTR, 0, SLJIT_IMM, 0, CURRENT_AS(bracket_backtrack)->recursive_matchingpath);
8408    if (bra == OP_BRAZERO)    if (bra == OP_BRAZERO)
8409      {      {
# Line 7999  else if (ket == OP_KETRMIN) Line 8422  else if (ket == OP_KETRMIN)
8422    affect badly the free_stack(2) above. */    affect badly the free_stack(2) above. */
8423    if (opcode != OP_ONCE)    if (opcode != OP_ONCE)
8424      free_stack(common, 1);      free_stack(common, 1);
8425    CMPTO(SLJIT_C_NOT_EQUAL, TMP1, 0, SLJIT_IMM, 0, rminlabel);    CMPTO(SLJIT_C_NOT_EQUAL, TMP1, 0, SLJIT_IMM, 0, rmin_label);
8426    if (opcode == OP_ONCE)    if (opcode == OP_ONCE)
8427      free_stack(common, bra == OP_BRAMINZERO ? 2 : 1);      free_stack(common, bra == OP_BRAMINZERO ? 2 : 1);
8428    else if (bra == OP_BRAMINZERO)    else if (bra == OP_BRAMINZERO)
# Line 8013  else if (bra == OP_BRAZERO) Line 8436  else if (bra == OP_BRAZERO)
8436    }    }
8437  }  }
8438    
8439  static void compile_bracketpos_backtrackingpath(compiler_common *common, struct backtrack_common *current)  static SLJIT_INLINE void compile_bracketpos_backtrackingpath(compiler_common *common, struct backtrack_common *current)
8440  {  {
8441  DEFINE_COMPILER;  DEFINE_COMPILER;
8442  int offset;  int offset;
# Line 8052  if (current->topbacktracks) Line 8475  if (current->topbacktracks)
8475  OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), CURRENT_AS(bracketpos_backtrack)->private_data_ptr, SLJIT_MEM1(STACK_TOP), CURRENT_AS(bracketpos_backtrack)->framesize * sizeof(sljit_sw));  OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), CURRENT_AS(bracketpos_backtrack)->private_data_ptr, SLJIT_MEM1(STACK_TOP), CURRENT_AS(bracketpos_backtrack)->framesize * sizeof(sljit_sw));
8476  }  }
8477    
8478  static void compile_braminzero_backtrackingpath(compiler_common *common, struct backtrack_common *current)  static SLJIT_INLINE void compile_braminzero_backtrackingpath(compiler_common *common, struct backtrack_common *current)
8479  {  {
8480  assert_backtrack backtrack;  assert_backtrack backtrack;
8481    
# Line 8076  else Line 8499  else
8499  SLJIT_ASSERT(!current->nextbacktracks && !current->topbacktracks);  SLJIT_ASSERT(!current->nextbacktracks && !current->topbacktracks);
8500  }  }
8501    
8502    static SLJIT_INLINE void compile_control_verb_backtrackingpath(compiler_common *common, struct backtrack_common *current)
8503    {
8504    DEFINE_COMPILER;
8505    pcre_uchar opcode = *current->cc;
8506    struct sljit_label *loop;
8507    struct sljit_jump *jump;
8508    
8509    if (opcode == OP_THEN || opcode == OP_THEN_ARG)
8510      {
8511      if (common->then_trap != NULL)
8512        {
8513        SLJIT_ASSERT(common->control_head_ptr != 0);
8514    
8515        OP1(SLJIT_MOV, STACK_TOP, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), common->control_head_ptr);
8516        OP1(SLJIT_MOV, TMP1, 0, SLJIT_IMM, type_then_trap);
8517        OP1(SLJIT_MOV, TMP2, 0, SLJIT_IMM, common->then_trap->start);
8518        jump = JUMP(SLJIT_JUMP);
8519    
8520        loop = LABEL();
8521        OP1(SLJIT_MOV, STACK_TOP, 0, SLJIT_MEM1(STACK_TOP), -(int)sizeof(sljit_sw));
8522        JUMPHERE(jump);
8523        CMPTO(SLJIT_C_NOT_EQUAL, SLJIT_MEM1(STACK_TOP), -(int)(2 * sizeof(sljit_sw)), TMP1, 0, loop);
8524        CMPTO(SLJIT_C_NOT_EQUAL, SLJIT_MEM1(STACK_TOP), -(int)(3 * sizeof(sljit_sw)), TMP2, 0, loop);
8525        add_jump(compiler, &common->then_trap->quit, JUMP(SLJIT_JUMP));
8526        return;
8527        }
8528      else if (common->positive_assert)
8529        {
8530        add_jump(compiler, &common->positive_assert_quit, JUMP(SLJIT_JUMP));
8531        return;
8532        }
8533      }
8534    
8535    if (common->local_exit)
8536      {
8537      if (common->quit_label == NULL)
8538        add_jump(compiler, &common->quit, JUMP(SLJIT_JUMP));
8539      else
8540        JUMPTO(SLJIT_JUMP, common->quit_label);
8541      return;
8542      }
8543    
8544    if (opcode == OP_SKIP_ARG)
8545      {
8546      SLJIT_ASSERT(common->control_head_ptr != 0);
8547      OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), common->control_head_ptr);
8548      OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), LOCALS0, STACK_TOP, 0);
8549      OP1(SLJIT_MOV, STACK_TOP, 0, SLJIT_IMM, (sljit_sw)(current->cc + 2));
8550      sljit_emit_ijump(compiler, SLJIT_CALL2, SLJIT_IMM, SLJIT_FUNC_OFFSET(do_search_mark));
8551      OP1(SLJIT_MOV, STACK_TOP, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), LOCALS0);
8552    
8553      OP1(SLJIT_MOV, STR_PTR, 0, TMP1, 0);
8554      add_jump(compiler, &common->reset_match, CMP(SLJIT_C_NOT_EQUAL, STR_PTR, 0, SLJIT_IMM, -1));
8555      return;
8556      }
8557    
8558    if (opcode == OP_SKIP)
8559      OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(STACK_TOP), STACK(0));
8560    else
8561      OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_IMM, 0);
8562    add_jump(compiler, &common->reset_match, JUMP(SLJIT_JUMP));
8563    }
8564    
8565    static SLJIT_INLINE void compile_then_trap_backtrackingpath(compiler_common *common, struct backtrack_common *current)
8566    {
8567    DEFINE_COMPILER;
8568    struct sljit_jump *jump;
8569    int size;
8570    
8571    if (CURRENT_AS(then_trap_backtrack)->then_trap)
8572      {
8573      common->then_trap = CURRENT_AS(then_trap_backtrack)->then_trap;
8574      return;
8575      }
8576    
8577    size = CURRENT_AS(then_trap_backtrack)->framesize;
8578    size = 3 + (size < 0 ? 0 : size);
8579    
8580    OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(STACK_TOP), STACK(size - 3));
8581    free_stack(common, size);
8582    jump = JUMP(SLJIT_JUMP);
8583    
8584    set_jumps(CURRENT_AS(then_trap_backtrack)->quit, LABEL());
8585    /* STACK_TOP is set by THEN. */
8586    if (CURRENT_AS(then_trap_backtrack)->framesize >= 0)
8587      add_jump(compiler, &common->revertframes, JUMP(SLJIT_FAST_CALL));
8588    OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(STACK_TOP), STACK(0));
8589    free_stack(common, 3);
8590    
8591    JUMPHERE(jump);
8592    OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), common->control_head_ptr, TMP1, 0);
8593    }
8594    
8595  static void compile_backtrackingpath(compiler_common *common, struct backtrack_common *current)  static void compile_backtrackingpath(compiler_common *common, struct backtrack_common *current)
8596  {  {
8597  DEFINE_COMPILER;  DEFINE_COMPILER;
8598    then_trap_backtrack *save_then_trap = common->then_trap;
8599    
8600  while (current)  while (current)
8601    {    {
# Line 8167  while (current) Line 8684  while (current)
8684    
8685      case OP_REF:      case OP_REF:
8686      case OP_REFI:      case OP_REFI:
8687        case OP_DNREF:
8688        case OP_DNREFI:
8689      compile_ref_iterator_backtrackingpath(common, current);      compile_ref_iterator_backtrackingpath(common, current);
8690      break;      break;
8691    
# Line 8212  while (current) Line 8731  while (current)
8731      break;      break;
8732    
8733      case OP_MARK:      case OP_MARK:
8734      OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(STACK_TOP), STACK(0));      OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(STACK_TOP), STACK(common->has_skip_arg ? 4 : 0));
8735      free_stack(common, 1);      if (common->has_skip_arg)
8736          OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(STACK_TOP), STACK(0));
8737        free_stack(common, common->has_skip_arg ? 5 : 1);
8738      OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), common->mark_ptr, TMP1, 0);      OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), common->mark_ptr, TMP1, 0);
8739        if (common->has_skip_arg)
8740          OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), common->control_head_ptr, TMP2, 0);
8741      break;      break;
8742    
8743        case OP_THEN:
8744        case OP_THEN_ARG:
8745      case OP_PRUNE:      case OP_PRUNE:
8746      case OP_PRUNE_ARG:      case OP_PRUNE_ARG:
8747      case OP_SKIP:      case OP_SKIP:
8748      if (!common->local_exit)      case OP_SKIP_ARG:
8749        {      compile_control_verb_backtrackingpath(common, current);
       SLJIT_ASSERT(common->control_head_ptr != 0);  
       OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), common->control_head_ptr);  
       OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), LOCALS0, STACK_TOP, 0);  
       sljit_emit_ijump(compiler, SLJIT_CALL3, SLJIT_IMM, SLJIT_FUNC_OFFSET(do_check_control_chain));  
       OP1(SLJIT_MOV, STACK_TOP, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), LOCALS0);  
   
       OP1(SLJIT_MOV, STR_PTR, 0, TMP1, 0);  
       add_jump(compiler, &common->reset_match, CMP(SLJIT_C_NOT_EQUAL, STR_PTR, 0, SLJIT_IMM, -1));  
   
       OP1(SLJIT_MOV, SLJIT_RETURN_REG, 0, SLJIT_IMM, PCRE_ERROR_NOMATCH);  
       }  
   
     /* Commit or in recurse or accept. */  
     if (common->quit_label == NULL)  
       add_jump(compiler, &common->quit, JUMP(SLJIT_JUMP));  
     else  
       JUMPTO(SLJIT_JUMP, common->quit_label);  
8750      break;      break;
8751    
8752      case OP_COMMIT:      case OP_COMMIT:
# Line 8257  while (current) Line 8765  while (current)
8765      set_jumps(current->topbacktracks, LABEL());      set_jumps(current->topbacktracks, LABEL());
8766      break;      break;
8767    
8768        case OP_THEN_TRAP:
8769        /* A virtual opcode for then traps. */
8770        compile_then_trap_backtrackingpath(common, current);
8771        break;
8772    
8773      default:      default:
8774      SLJIT_ASSERT_STOP();      SLJIT_ASSERT_STOP();
8775      break;      break;
8776      }      }
8777    current = current->prev;    current = current->prev;
8778    }    }
8779    common->then_trap = save_then_trap;
8780  }  }
8781    
8782  static SLJIT_INLINE void compile_recurse(compiler_common *common)  static SLJIT_INLINE void compile_recurse(compiler_common *common)
# Line 8271  DEFINE_COMPILER; Line 8785  DEFINE_COMPILER;
8785  pcre_uchar *cc = common->start + common->currententry->start;  pcre_uchar *cc = common->start + common->currententry->start;
8786  pcre_uchar *ccbegin = cc + 1 + LINK_SIZE + (*cc == OP_BRA ? 0 : IMM2_SIZE);  pcre_uchar *ccbegin = cc + 1 + LINK_SIZE + (*cc == OP_BRA ? 0 : IMM2_SIZE);
8787  pcre_uchar *ccend = bracketend(cc);  pcre_uchar *ccend = bracketend(cc);
8788  int private_data_size = get_private_data_length_for_copy(common, ccbegin, ccend);  BOOL needs_control_head;
8789  int framesize = get_framesize(common, cc, TRUE);  int framesize = get_framesize(common, cc, NULL, TRUE, &needs_control_head);
8790    int private_data_size = get_private_data_copy_length(common, ccbegin, ccend, needs_control_head);
8791  int alternativesize;  int alternativesize;
8792  BOOL needs_frame;  BOOL needs_frame;
8793  backtrack_common altbacktrack;  backtrack_common altbacktrack;
8794  struct sljit_jump *jump;  struct sljit_jump *jump;
8795    
8796    /* Recurse captures then. */
8797    common->then_trap = NULL;
8798    
8799  SLJIT_ASSERT(*cc == OP_BRA || *cc == OP_CBRA || *cc == OP_CBRAPOS || *cc == OP_SCBRA || *cc == OP_SCBRAPOS);  SLJIT_ASSERT(*cc == OP_BRA || *cc == OP_CBRA || *cc == OP_CBRAPOS || *cc == OP_SCBRA || *cc == OP_SCBRAPOS);
8800  needs_frame = framesize >= 0;  needs_frame = framesize >= 0;
8801  if (!needs_frame)  if (!needs_frame)
# Line 8291  set_jumps(common->currententry->calls, c Line 8809  set_jumps(common->currententry->calls, c
8809  sljit_emit_fast_enter(compiler, TMP2, 0);  sljit_emit_fast_enter(compiler, TMP2, 0);
8810  allocate_stack(common, private_data_size + framesize + alternativesize);  allocate_stack(common, private_data_size + framesize + alternativesize);
8811  OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(private_data_size + framesize + alternativesize - 1), TMP2, 0);  OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(private_data_size + framesize + alternativesize - 1), TMP2, 0);
8812  copy_private_data(common, ccbegin, ccend, TRUE, private_data_size + framesize + alternativesize, framesize + alternativesize);  copy_private_data(common, ccbegin, ccend, TRUE, private_data_size + framesize + alternativesize, framesize + alternativesize, needs_control_head);
8813  if (common->control_head_ptr != 0)  if (needs_control_head)
8814    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);
8815  OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), common->recursive_head_ptr, STACK_TOP, 0);  OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), common->recursive_head_ptr, STACK_TOP, 0);
8816  if (needs_frame)  if (needs_frame)
8817    init_frame(common, cc, framesize + alternativesize - 1, alternativesize, TRUE);    init_frame(common, cc, NULL, framesize + alternativesize - 1, alternativesize, TRUE);
8818    
8819  if (alternativesize > 0)  if (alternativesize > 0)
8820    OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), STR_PTR, 0);    OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), STR_PTR, 0);
# Line 8366  OP1(SLJIT_MOV, TMP3, 0, SLJIT_IMM, 1); Line 8884  OP1(SLJIT_MOV, TMP3, 0, SLJIT_IMM, 1);
8884  JUMPHERE(jump);  JUMPHERE(jump);
8885  if (common->quit != NULL)  if (common->quit != NULL)
8886    set_jumps(common->quit, LABEL());    set_jumps(common->quit, LABEL());
8887  copy_private_data(common, ccbegin, ccend, FALSE, private_data_size + framesize + alternativesize, framesize + alternativesize);  copy_private_data(common, ccbegin, ccend, FALSE, private_data_size + framesize + alternativesize, framesize + alternativesize, needs_control_head);
8888  free_stack(common, private_data_size + framesize + alternativesize);  free_stack(common, private_data_size + framesize + alternativesize);
8889  if (common->control_head_ptr != 0)  if (needs_control_head)
8890    {    {
8891    OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(STACK_TOP), 2 * sizeof(sljit_sw));    OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(STACK_TOP), 2 * sizeof(sljit_sw));
8892    OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(STACK_TOP), sizeof(sljit_sw));    OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(STACK_TOP), sizeof(sljit_sw));
# Line 8462  else Line 8980  else
8980  common->endonly = (re->options & PCRE_DOLLAR_ENDONLY) != 0;  common->endonly = (re->options & PCRE_DOLLAR_ENDONLY) != 0;
8981  common->ctypes = (sljit_sw)(tables + ctypes_offset);  common->ctypes = (sljit_sw)(tables + ctypes_offset);
8982  common->digits[0] = -2;  common->digits[0] = -2;
8983  common->name_table = (sljit_sw)((pcre_uchar *)re + re->name_table_offset);  common->name_table = ((pcre_uchar *)re) + re->name_table_offset;
8984  common->name_count = re->name_count;  common->name_count = re->name_count;
8985  common->name_entry_size = re->name_entry_size;  common->name_entry_size = re->name_entry_size;
8986  common->jscript_compat = (re->options & PCRE_JAVASCRIPT_COMPAT) != 0;  common->jscript_compat = (re->options & PCRE_JAVASCRIPT_COMPAT) != 0;
# Line 8476  common->use_ucp = (re->options & PCRE_UC Line 8994  common->use_ucp = (re->options & PCRE_UC
8994  ccend = bracketend(rootbacktrack.cc);  ccend = bracketend(rootbacktrack.cc);
8995    
8996  /* Calculate the local space size on the stack. */  /* Calculate the local space size on the stack. */
8997  common->ovector_start = CALL_LIMIT + sizeof(sljit_sw);  common->ovector_start = LIMIT_MATCH + sizeof(sljit_sw);
8998  common->optimized_cbracket = (pcre_uint8 *)SLJIT_MALLOC(re->top_bracket + 1);  common->optimized_cbracket = (pcre_uint8 *)SLJIT_MALLOC(re->top_bracket + 1);
8999  if (!common->optimized_cbracket)  if (!common->optimized_cbracket)
9000    return;    return;
# Line 8491  SLJIT_ASSERT(*rootbacktrack.cc == OP_BRA Line 9009  SLJIT_ASSERT(*rootbacktrack.cc == OP_BRA
9009  common->capture_last_ptr = common->ovector_start;  common->capture_last_ptr = common->ovector_start;
9010  common->ovector_start += sizeof(sljit_sw);  common->ovector_start += sizeof(sljit_sw);
9011  #endif  #endif
9012  private_data_size = get_private_data_length(common, rootbacktrack.cc, ccend);  if (!check_opcode_types(common, rootbacktrack.cc, ccend))
 if (private_data_size < 0)  
9013    {    {
9014    SLJIT_FREE(common->optimized_cbracket);    SLJIT_FREE(common->optimized_cbracket);
9015    return;    return;
# Line 8554  if (common->capture_last_ptr != 0) Line 9071  if (common->capture_last_ptr != 0)
9071    
9072  SLJIT_ASSERT(!(common->req_char_ptr != 0 && common->start_used_ptr != 0));  SLJIT_ASSERT(!(common->req_char_ptr != 0 && common->start_used_ptr != 0));
9073  common->cbra_ptr = OVECTOR_START + (re->top_bracket + 1) * 2 * sizeof(sljit_sw);  common->cbra_ptr = OVECTOR_START + (re->top_bracket + 1) * 2 * sizeof(sljit_sw);
9074  private_data_size += common->cbra_ptr + (re->top_bracket + 1) * sizeof(sljit_sw);  
9075  if (private_data_size > SLJIT_MAX_LOCAL_SIZE)  common->private_data_ptrs = (int *)SLJIT_MALLOC((ccend - rootbacktrack.cc) * sizeof(sljit_si));
9076    if (!common->private_data_ptrs)
9077    {    {
9078    SLJIT_FREE(common->optimized_cbracket);    SLJIT_FREE(common->optimized_cbracket);
9079    return;    return;
9080    }    }
9081  common->private_data_ptrs = (int *)SLJIT_MALLOC((ccend - rootbacktrack.cc) * sizeof(int));  memset(common->private_data_ptrs, 0, (ccend - rootbacktrack.cc) * sizeof(int));
9082  if (!common->private_data_ptrs)  
9083    private_data_size = common->cbra_ptr + (re->top_bracket + 1) * sizeof(sljit_sw);
9084    set_private_data_ptrs(common, &private_data_size, ccend);
9085    if (private_data_size > SLJIT_MAX_LOCAL_SIZE)
9086    {    {
9087      SLJIT_FREE(common->private_data_ptrs);
9088    SLJIT_FREE(common->optimized_cbracket);    SLJIT_FREE(common->optimized_cbracket);
9089    return;    return;
9090    }    }
9091  memset(common->private_data_ptrs, 0, (ccend - rootbacktrack.cc) * sizeof(int));  
9092  set_private_data_ptrs(common, common->cbra_ptr + (re->top_bracket + 1) * sizeof(sljit_sw), ccend);  if (common->has_then)
9093      {
9094      common->then_offsets = (pcre_uint8 *)SLJIT_MALLOC(ccend - rootbacktrack.cc);
9095      if (!common->then_offsets)
9096        {
9097        SLJIT_FREE(common->optimized_cbracket);
9098        SLJIT_FREE(common->private_data_ptrs);
9099        return;
9100        }
9101      memset(common->then_offsets, 0, ccend - rootbacktrack.cc);
9102      set_then_offsets(common, rootbacktrack.cc, NULL);
9103      }
9104    
9105  compiler = sljit_create_compiler();  compiler = sljit_create_compiler();
9106  if (!compiler)  if (!compiler)
9107    {    {
9108    SLJIT_FREE(common->optimized_cbracket);    SLJIT_FREE(common->optimized_cbracket);
9109    SLJIT_FREE(common->private_data_ptrs);    SLJIT_FREE(common->private_data_ptrs);
9110      if (common->has_then)
9111        SLJIT_FREE(common->then_offsets);
9112    return;    return;
9113    }    }
9114  common->compiler = compiler;  common->compiler = compiler;
# Line 8591  OP1(SLJIT_MOV, TMP1, 0, SLJIT_SAVED_REG1 Line 9126  OP1(SLJIT_MOV, TMP1, 0, SLJIT_SAVED_REG1
9126  OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(TMP1), SLJIT_OFFSETOF(jit_arguments, str));  OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(TMP1), SLJIT_OFFSETOF(jit_arguments, str));
9127  OP1(SLJIT_MOV, STR_END, 0, SLJIT_MEM1(TMP1), SLJIT_OFFSETOF(jit_arguments, end));  OP1(SLJIT_MOV, STR_END, 0, SLJIT_MEM1(TMP1), SLJIT_OFFSETOF(jit_arguments, end));
9128  OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(TMP1), SLJIT_OFFSETOF(jit_arguments, stack));  OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(TMP1), SLJIT_OFFSETOF(jit_arguments, stack));
9129  OP1(SLJIT_MOV_SI, TMP1, 0, SLJIT_MEM1(TMP1), SLJIT_OFFSETOF(jit_arguments, call_limit));  OP1(SLJIT_MOV_UI, TMP1, 0, SLJIT_MEM1(TMP1), SLJIT_OFFSETOF(jit_arguments, limit_match));
9130  OP1(SLJIT_MOV, STACK_TOP, 0, SLJIT_MEM1(TMP2), SLJIT_OFFSETOF(struct sljit_stack, base));  OP1(SLJIT_MOV, STACK_TOP, 0, SLJIT_MEM1(TMP2), SLJIT_OFFSETOF(struct sljit_stack, base));
9131  OP1(SLJIT_MOV, STACK_LIMIT, 0, SLJIT_MEM1(TMP2), SLJIT_OFFSETOF(struct sljit_stack, limit));  OP1(SLJIT_MOV, STACK_LIMIT, 0, SLJIT_MEM1(TMP2), SLJIT_OFFSETOF(struct sljit_stack, limit));
9132  OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), CALL_LIMIT, TMP1, 0);  OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), LIMIT_MATCH, TMP1, 0);
9133    
9134  if (mode == JIT_PARTIAL_SOFT_COMPILE)  if (mode == JIT_PARTIAL_SOFT_COMPILE)
9135    OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), common->hit_start, SLJIT_IMM, -1);    OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), common->hit_start, SLJIT_IMM, -1);
# Line 8636  if (common->req_char_ptr != 0) Line 9171  if (common->req_char_ptr != 0)
9171  /* Store the current STR_PTR in OVECTOR(0). */  /* Store the current STR_PTR in OVECTOR(0). */
9172  OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(0), STR_PTR, 0);  OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(0), STR_PTR, 0);
9173  /* Copy the limit of allowed recursions. */  /* Copy the limit of allowed recursions. */
9174  OP1(SLJIT_MOV, CALL_COUNT, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), CALL_LIMIT);  OP1(SLJIT_MOV, COUNT_MATCH, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), LIMIT_MATCH);
9175  if (common->capture_last_ptr != 0)  if (common->capture_last_ptr != 0)
9176    OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), common->capture_last_ptr, SLJIT_IMM, -1);    OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), common->capture_last_ptr, SLJIT_IMM, -1);
9177    
# Line 8665  if (SLJIT_UNLIKELY(sljit_get_compiler_er Line 9200  if (SLJIT_UNLIKELY(sljit_get_compiler_er
9200    sljit_free_compiler(compiler);    sljit_free_compiler(compiler);
9201    SLJIT_FREE(common->optimized_cbracket);    SLJIT_FREE(common->optimized_cbracket);
9202    SLJIT_FREE(common->private_data_ptrs);    SLJIT_FREE(common->private_data_ptrs);
9203      if (common->has_then)
9204        SLJIT_FREE(common->then_offsets);
9205    return;    return;
9206    }    }
9207    
# Line 8700  if (SLJIT_UNLIKELY(sljit_get_compiler_er Line 9237  if (SLJIT_UNLIKELY(sljit_get_compiler_er
9237    sljit_free_compiler(compiler);    sljit_free_compiler(compiler);
9238    SLJIT_FREE(common->optimized_cbracket);    SLJIT_FREE(common->optimized_cbracket);
9239    SLJIT_FREE(common->private_data_ptrs);    SLJIT_FREE(common->private_data_ptrs);
9240      if (common->has_then)
9241        SLJIT_FREE(common->then_offsets);
9242    return;    return;
9243    }    }
9244    
# Line 8767  while (common->currententry != NULL) Line 9306  while (common->currententry != NULL)
9306      sljit_free_compiler(compiler);      sljit_free_compiler(compiler);
9307      SLJIT_FREE(common->optimized_cbracket);      SLJIT_FREE(common->optimized_cbracket);
9308      SLJIT_FREE(common->private_data_ptrs);      SLJIT_FREE(common->private_data_ptrs);
9309        if (common->has_then)
9310          SLJIT_FREE(common->then_offsets);
9311      return;      return;
9312      }      }
9313    flush_stubs(common);    flush_stubs(common);
# Line 8875  if (common->getucd != NULL) Line 9416  if (common->getucd != NULL)
9416    
9417  SLJIT_FREE(common->optimized_cbracket);  SLJIT_FREE(common->optimized_cbracket);
9418  SLJIT_FREE(common->private_data_ptrs);  SLJIT_FREE(common->private_data_ptrs);
9419    if (common->has_then)
9420      SLJIT_FREE(common->then_offsets);
9421    
9422  executable_func = sljit_generate_code(compiler);  executable_func = sljit_generate_code(compiler);
9423  executable_size = sljit_get_generated_code_size(compiler);  executable_size = sljit_get_generated_code_size(compiler);
9424  sljit_free_compiler(compiler);  sljit_free_compiler(compiler);
# Line 8905  else Line 9449  else
9449      }      }
9450    memset(functions, 0, sizeof(executable_functions));    memset(functions, 0, sizeof(executable_functions));
9451    functions->top_bracket = (re->top_bracket + 1) * 2;    functions->top_bracket = (re->top_bracket + 1) * 2;
9452      functions->limit_match = (re->flags & PCRE_MLSET) != 0 ? re->limit_match : 0;
9453    extra->executable_jit = functions;    extra->executable_jit = functions;
9454    extra->flags |= PCRE_EXTRA_EXECUTABLE_JIT;    extra->flags |= PCRE_EXTRA_EXECUTABLE_JIT;
9455    }    }
# Line 8959  arguments.begin = subject; Line 9504  arguments.begin = subject;
9504  arguments.end = subject + length;  arguments.end = subject + length;
9505  arguments.mark_ptr = NULL;  arguments.mark_ptr = NULL;
9506  /* JIT decreases this value less frequently than the interpreter. */  /* JIT decreases this value less frequently than the interpreter. */
9507  arguments.call_limit = ((extra_data->flags & PCRE_EXTRA_MATCH_LIMIT) == 0) ? MATCH_LIMIT : extra_data->match_limit;  arguments.limit_match = ((extra_data->flags & PCRE_EXTRA_MATCH_LIMIT) == 0) ? MATCH_LIMIT : (pcre_uint32)(extra_data->match_limit);
9508    if (functions->limit_match != 0 && functions->limit_match < arguments.limit_match)
9509      arguments.limit_match = functions->limit_match;
9510  arguments.notbol = (options & PCRE_NOTBOL) != 0;  arguments.notbol = (options & PCRE_NOTBOL) != 0;
9511  arguments.noteol = (options & PCRE_NOTEOL) != 0;  arguments.noteol = (options & PCRE_NOTEOL) != 0;
9512  arguments.notempty = (options & PCRE_NOTEMPTY) != 0;  arguments.notempty = (options & PCRE_NOTEMPTY) != 0;
# Line 9050  arguments.begin = subject_ptr; Line 9597  arguments.begin = subject_ptr;
9597  arguments.end = subject_ptr + length;  arguments.end = subject_ptr + length;
9598  arguments.mark_ptr = NULL;  arguments.mark_ptr = NULL;
9599  /* JIT decreases this value less frequently than the interpreter. */  /* JIT decreases this value less frequently than the interpreter. */
9600  arguments.call_limit = ((extra_data->flags & PCRE_EXTRA_MATCH_LIMIT) == 0) ? MATCH_LIMIT : extra_data->match_limit;  arguments.limit_match = ((extra_data->flags & PCRE_EXTRA_MATCH_LIMIT) == 0) ? MATCH_LIMIT : (pcre_uint32)(extra_data->match_limit);
9601    if (functions->limit_match != 0 && functions->limit_match < arguments.limit_match)
9602      arguments.limit_match = functions->limit_match;
9603  arguments.notbol = (options & PCRE_NOTBOL) != 0;  arguments.notbol = (options & PCRE_NOTBOL) != 0;
9604  arguments.noteol = (options & PCRE_NOTEOL) != 0;  arguments.noteol = (options & PCRE_NOTEOL) != 0;
9605  arguments.notempty = (options & PCRE_NOTEMPTY) != 0;  arguments.notempty = (options & PCRE_NOTEMPTY) != 0;
# Line 9169  if (extra != NULL && Line 9718  if (extra != NULL &&
9718    }    }
9719  }  }
9720    
9721    #if defined COMPILE_PCRE8
9722    PCRE_EXP_DECL void
9723    pcre_jit_free_unused_memory(void)
9724    #elif defined COMPILE_PCRE16
9725    PCRE_EXP_DECL void
9726    pcre16_jit_free_unused_memory(void)
9727    #elif defined COMPILE_PCRE32
9728    PCRE_EXP_DECL void
9729    pcre32_jit_free_unused_memory(void)
9730    #endif
9731    {
9732    sljit_free_unused_memory_exec();
9733    }
9734    
9735  #else  /* SUPPORT_JIT */  #else  /* SUPPORT_JIT */
9736    
9737  /* These are dummy functions to avoid linking errors when JIT support is not  /* These are dummy functions to avoid linking errors when JIT support is not
# Line 9220  pcre32_assign_jit_stack(pcre32_extra *ex Line 9783  pcre32_assign_jit_stack(pcre32_extra *ex
9783  (void)userdata;  (void)userdata;
9784  }  }
9785    
9786    #if defined COMPILE_PCRE8
9787    PCRE_EXP_DECL void
9788    pcre_jit_free_unused_memory(void)
9789    #elif defined COMPILE_PCRE16
9790    PCRE_EXP_DECL void
9791    pcre16_jit_free_unused_memory(void)
9792    #elif defined COMPILE_PCRE32
9793    PCRE_EXP_DECL void
9794    pcre32_jit_free_unused_memory(void)
9795    #endif
9796    {
9797    }
9798    
9799  #endif  #endif
9800    
9801  /* End of pcre_jit_compile.c */  /* End of pcre_jit_compile.c */

Legend:
Removed from v.1275  
changed lines
  Added in v.1379

  ViewVC Help
Powered by ViewVC 1.1.5