/[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 1277 by zherczeg, Mon Mar 11 09:50:29 2013 UTC revision 1420 by zherczeg, Sun Dec 29 11:43:10 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 283  typedef struct recurse_entry { Line 283  typedef struct recurse_entry {
283    /* Collects the calls until the function is not created. */    /* Collects the calls until the function is not created. */
284    jump_list *calls;    jump_list *calls;
285    /* Points to the starting opcode. */    /* Points to the starting opcode. */
286    int start;    sljit_sw start;
287  } recurse_entry;  } recurse_entry;
288    
289  typedef struct recurse_backtrack {  typedef struct recurse_backtrack {
# Line 291  typedef struct recurse_backtrack { Line 291  typedef struct recurse_backtrack {
291    BOOL inlined_pattern;    BOOL inlined_pattern;
292  } recurse_backtrack;  } recurse_backtrack;
293    
294  #define MAX_RANGE_SIZE 6  #define OP_THEN_TRAP OP_TABLE_LENGTH
295    
296    typedef struct then_trap_backtrack {
297      backtrack_common common;
298      /* If then_trap is not NULL, this structure contains the real
299      then_trap for the backtracking path. */
300      struct then_trap_backtrack *then_trap;
301      /* Points to the starting opcode. */
302      sljit_sw start;
303      /* Exit point for the then opcodes of this alternative. */
304      jump_list *quit;
305      /* Frame size of the current alternative. */
306      int framesize;
307    } then_trap_backtrack;
308    
309    #define MAX_RANGE_SIZE 4
310    
311  typedef struct compiler_common {  typedef struct compiler_common {
312    /* The sljit ceneric compiler. */    /* The sljit ceneric compiler. */
# Line 299  typedef struct compiler_common { Line 314  typedef struct compiler_common {
314    /* First byte code. */    /* First byte code. */
315    pcre_uchar *start;    pcre_uchar *start;
316    /* Maps private data offset to each opcode. */    /* Maps private data offset to each opcode. */
317    int *private_data_ptrs;    sljit_si *private_data_ptrs;
318    /* Tells whether the capturing bracket is optimized. */    /* Tells whether the capturing bracket is optimized. */
319    pcre_uint8 *optimized_cbracket;    pcre_uint8 *optimized_cbracket;
320      /* Tells whether the starting offset is a target of then. */
321      pcre_uint8 *then_offsets;
322      /* Current position where a THEN must jump. */
323      then_trap_backtrack *then_trap;
324    /* Starting offset of private data for capturing brackets. */    /* Starting offset of private data for capturing brackets. */
325    int cbra_ptr;    int cbra_ptr;
326    /* Output vector starting point. Must be divisible by 2. */    /* Output vector starting point. Must be divisible by 2. */
# Line 330  typedef struct compiler_common { Line 349  typedef struct compiler_common {
349    sljit_sw lcc;    sljit_sw lcc;
350    /* Mode can be PCRE_STUDY_JIT_COMPILE and others. */    /* Mode can be PCRE_STUDY_JIT_COMPILE and others. */
351    int mode;    int mode;
352    /* \K is in the pattern. */    /* \K is found in the pattern. */
353    BOOL has_set_som;    BOOL has_set_som;
354      /* (*SKIP:arg) is found in the pattern. */
355      BOOL has_skip_arg;
356      /* (*THEN) is found in the pattern. */
357      BOOL has_then;
358    /* Needs to know the start position anytime. */    /* Needs to know the start position anytime. */
359    BOOL needs_start_ptr;    BOOL needs_start_ptr;
360    /* Currently in recurse or assert. */    /* Currently in recurse or negative assert. */
361    BOOL local_exit;    BOOL local_exit;
362      /* Currently in a positive assert. */
363      BOOL positive_assert;
364    /* Newline control. */    /* Newline control. */
365    int nltype;    int nltype;
366    int newline;    int newline;
# Line 344  typedef struct compiler_common { Line 369  typedef struct compiler_common {
369    int endonly;    int endonly;
370    /* Tables. */    /* Tables. */
371    sljit_sw ctypes;    sljit_sw ctypes;
   int digits[2 + MAX_RANGE_SIZE];  
372    /* Named capturing brackets. */    /* Named capturing brackets. */
373    sljit_uw name_table;    pcre_uchar *name_table;
374    sljit_sw name_count;    sljit_sw name_count;
375    sljit_sw name_entry_size;    sljit_sw name_entry_size;
376    
# Line 360  typedef struct compiler_common { Line 384  typedef struct compiler_common {
384    recurse_entry *currententry;    recurse_entry *currententry;
385    jump_list *partialmatch;    jump_list *partialmatch;
386    jump_list *quit;    jump_list *quit;
387      jump_list *positive_assert_quit;
388    jump_list *forced_quit;    jump_list *forced_quit;
389    jump_list *accept;    jump_list *accept;
390    jump_list *calllimit;    jump_list *calllimit;
# Line 382  typedef struct compiler_common { Line 407  typedef struct compiler_common {
407    jump_list *utfreadchar;    jump_list *utfreadchar;
408  #endif  #endif
409  #ifdef COMPILE_PCRE8  #ifdef COMPILE_PCRE8
410      jump_list *utfreadchar8;
411    jump_list *utfreadtype8;    jump_list *utfreadtype8;
412  #endif  #endif
413  #endif /* SUPPORT_UTF */  #endif /* SUPPORT_UTF */
# Line 438  typedef struct compare_context { Line 464  typedef struct compare_context {
464  #define STACK_TOP     SLJIT_SCRATCH_REG2  #define STACK_TOP     SLJIT_SCRATCH_REG2
465  #define STACK_LIMIT   SLJIT_SAVED_REG3  #define STACK_LIMIT   SLJIT_SAVED_REG3
466  #define ARGUMENTS     SLJIT_SAVED_EREG1  #define ARGUMENTS     SLJIT_SAVED_EREG1
467  #define CALL_COUNT    SLJIT_SAVED_EREG2  #define COUNT_MATCH   SLJIT_SAVED_EREG2
468  #define RETURN_ADDR   SLJIT_TEMPORARY_EREG1  #define RETURN_ADDR   SLJIT_TEMPORARY_EREG1
469    
470  /* Local space layout. */  /* Local space layout. */
# Line 449  typedef struct compare_context { Line 475  typedef struct compare_context {
475  #define POSSESSIVE0      (2 * sizeof(sljit_sw))  #define POSSESSIVE0      (2 * sizeof(sljit_sw))
476  #define POSSESSIVE1      (3 * sizeof(sljit_sw))  #define POSSESSIVE1      (3 * sizeof(sljit_sw))
477  /* Max limit of recursions. */  /* Max limit of recursions. */
478  #define CALL_LIMIT       (4 * sizeof(sljit_sw))  #define LIMIT_MATCH      (4 * sizeof(sljit_sw))
479  /* The output vector is stored on the stack, and contains pointers  /* The output vector is stored on the stack, and contains pointers
480  to characters. The vector data is divided into two groups: the first  to characters. The vector data is divided into two groups: the first
481  group contains the start / end character pointers, and the second is  group contains the start / end character pointers, and the second is
482  the start pointers when the end of the capturing group has not yet reached. */  the start pointers when the end of the capturing group has not yet reached. */
483  #define OVECTOR_START    (common->ovector_start)  #define OVECTOR_START    (common->ovector_start)
484  #define OVECTOR(i)       (OVECTOR_START + (i) * sizeof(sljit_sw))  #define OVECTOR(i)       (OVECTOR_START + (i) * (sljit_sw)sizeof(sljit_sw))
485  #define OVECTOR_PRIV(i)  (common->cbra_ptr + (i) * sizeof(sljit_sw))  #define OVECTOR_PRIV(i)  (common->cbra_ptr + (i) * (sljit_sw)sizeof(sljit_sw))
486  #define PRIVATE_DATA(cc) (common->private_data_ptrs[(cc) - common->start])  #define PRIVATE_DATA(cc) (common->private_data_ptrs[(cc) - common->start])
487    
488  #if defined COMPILE_PCRE8  #if defined COMPILE_PCRE8
# Line 507  cc += 1 + LINK_SIZE; Line 533  cc += 1 + LINK_SIZE;
533  return cc;  return cc;
534  }  }
535    
536    static int ones_in_half_byte[16] = {
537      /* 0 */ 0, 1, 1, 2, /* 4 */ 1, 2, 2, 3,
538      /* 8 */ 1, 2, 2, 3, /* 12 */ 2, 3, 3, 4
539    };
540    
541  /* Functions whose might need modification for all new supported opcodes:  /* Functions whose might need modification for all new supported opcodes:
542   next_opcode   next_opcode
543   get_private_data_length   check_opcode_types
544   set_private_data_ptrs   set_private_data_ptrs
545   get_framesize   get_framesize
546   init_frame   init_frame
# Line 559  switch(*cc) Line 590  switch(*cc)
590    case OP_CRMINQUERY:    case OP_CRMINQUERY:
591    case OP_CRRANGE:    case OP_CRRANGE:
592    case OP_CRMINRANGE:    case OP_CRMINRANGE:
593      case OP_CRPOSSTAR:
594      case OP_CRPOSPLUS:
595      case OP_CRPOSQUERY:
596      case OP_CRPOSRANGE:
597    case OP_CLASS:    case OP_CLASS:
598    case OP_NCLASS:    case OP_NCLASS:
599    case OP_REF:    case OP_REF:
600    case OP_REFI:    case OP_REFI:
601      case OP_DNREF:
602      case OP_DNREFI:
603    case OP_RECURSE:    case OP_RECURSE:
604    case OP_CALLOUT:    case OP_CALLOUT:
605    case OP_ALT:    case OP_ALT:
# Line 588  switch(*cc) Line 625  switch(*cc)
625    case OP_SCBRAPOS:    case OP_SCBRAPOS:
626    case OP_SCOND:    case OP_SCOND:
627    case OP_CREF:    case OP_CREF:
628    case OP_NCREF:    case OP_DNCREF:
629    case OP_RREF:    case OP_RREF:
630    case OP_NRREF:    case OP_DNRREF:
631    case OP_DEF:    case OP_DEF:
632    case OP_BRAZERO:    case OP_BRAZERO:
633    case OP_BRAMINZERO:    case OP_BRAMINZERO:
634    case OP_BRAPOSZERO:    case OP_BRAPOSZERO:
635    case OP_PRUNE:    case OP_PRUNE:
636    case OP_SKIP:    case OP_SKIP:
637      case OP_THEN:
638    case OP_COMMIT:    case OP_COMMIT:
639    case OP_FAIL:    case OP_FAIL:
640    case OP_ACCEPT:    case OP_ACCEPT:
# Line 696  switch(*cc) Line 734  switch(*cc)
734    
735    case OP_MARK:    case OP_MARK:
736    case OP_PRUNE_ARG:    case OP_PRUNE_ARG:
737      case OP_SKIP_ARG:
738      case OP_THEN_ARG:
739    return cc + 1 + 2 + cc[1];    return cc + 1 + 2 + cc[1];
740    
741    default:    default:
742      /* All opcodes are supported now! */
743      SLJIT_ASSERT_STOP();
744    return NULL;    return NULL;
745    }    }
746  }  }
747    
748  #define CASE_ITERATOR_PRIVATE_DATA_1 \  static BOOL check_opcode_types(compiler_common *common, pcre_uchar *cc, pcre_uchar *ccend)
     case OP_MINSTAR: \  
     case OP_MINPLUS: \  
     case OP_QUERY: \  
     case OP_MINQUERY: \  
     case OP_MINSTARI: \  
     case OP_MINPLUSI: \  
     case OP_QUERYI: \  
     case OP_MINQUERYI: \  
     case OP_NOTMINSTAR: \  
     case OP_NOTMINPLUS: \  
     case OP_NOTQUERY: \  
     case OP_NOTMINQUERY: \  
     case OP_NOTMINSTARI: \  
     case OP_NOTMINPLUSI: \  
     case OP_NOTQUERYI: \  
     case OP_NOTMINQUERYI:  
   
 #define CASE_ITERATOR_PRIVATE_DATA_2A \  
     case OP_STAR: \  
     case OP_PLUS: \  
     case OP_STARI: \  
     case OP_PLUSI: \  
     case OP_NOTSTAR: \  
     case OP_NOTPLUS: \  
     case OP_NOTSTARI: \  
     case OP_NOTPLUSI:  
   
 #define CASE_ITERATOR_PRIVATE_DATA_2B \  
     case OP_UPTO: \  
     case OP_MINUPTO: \  
     case OP_UPTOI: \  
     case OP_MINUPTOI: \  
     case OP_NOTUPTO: \  
     case OP_NOTMINUPTO: \  
     case OP_NOTUPTOI: \  
     case OP_NOTMINUPTOI:  
   
 #define CASE_ITERATOR_TYPE_PRIVATE_DATA_1 \  
     case OP_TYPEMINSTAR: \  
     case OP_TYPEMINPLUS: \  
     case OP_TYPEQUERY: \  
     case OP_TYPEMINQUERY:  
   
 #define CASE_ITERATOR_TYPE_PRIVATE_DATA_2A \  
     case OP_TYPESTAR: \  
     case OP_TYPEPLUS:  
   
 #define CASE_ITERATOR_TYPE_PRIVATE_DATA_2B \  
     case OP_TYPEUPTO: \  
     case OP_TYPEMINUPTO:  
   
 static int get_class_iterator_size(pcre_uchar *cc)  
 {  
 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)  
749  {  {
750  int private_data_length = 0;  int count;
751  pcre_uchar *alternative;  pcre_uchar *slot;
 pcre_uchar *name;  
 pcre_uchar *end = NULL;  
 int space, size, i;  
 pcre_uint32 bracketlen;  
752    
753  /* Calculate important variables (like stack size) and checks whether all opcodes are supported. */  /* Calculate important variables (like stack size) and checks whether all opcodes are supported. */
754  while (cc < ccend)  while (cc < ccend)
755    {    {
   space = 0;  
   size = 0;  
   bracketlen = 0;  
756    switch(*cc)    switch(*cc)
757      {      {
758      case OP_SET_SOM:      case OP_SET_SOM:
# Line 808  while (cc < ccend) Line 766  while (cc < ccend)
766      cc += 1 + IMM2_SIZE;      cc += 1 + IMM2_SIZE;
767      break;      break;
768    
     case OP_ASSERT:  
     case OP_ASSERT_NOT:  
     case OP_ASSERTBACK:  
     case OP_ASSERTBACK_NOT:  
     case OP_ONCE:  
     case OP_ONCE_NC:  
     case OP_BRAPOS:  
     case OP_SBRA:  
     case OP_SBRAPOS:  
     private_data_length += sizeof(sljit_sw);  
     bracketlen = 1 + LINK_SIZE;  
     break;  
   
769      case OP_CBRAPOS:      case OP_CBRAPOS:
770      case OP_SCBRAPOS:      case OP_SCBRAPOS:
     private_data_length += sizeof(sljit_sw);  
771      common->optimized_cbracket[GET2(cc, 1 + LINK_SIZE)] = 0;      common->optimized_cbracket[GET2(cc, 1 + LINK_SIZE)] = 0;
772      bracketlen = 1 + LINK_SIZE + IMM2_SIZE;      cc += 1 + LINK_SIZE + IMM2_SIZE;
773      break;      break;
774    
775      case OP_COND:      case OP_COND:
# Line 833  while (cc < ccend) Line 777  while (cc < ccend)
777      /* Only AUTO_CALLOUT can insert this opcode. We do      /* Only AUTO_CALLOUT can insert this opcode. We do
778         not intend to support this case. */         not intend to support this case. */
779      if (cc[1 + LINK_SIZE] == OP_CALLOUT)      if (cc[1 + LINK_SIZE] == OP_CALLOUT)
780        return -1;        return FALSE;
781        cc += 1 + LINK_SIZE;
     if (*cc == OP_COND)  
       {  
       /* Might be a hidden SCOND. */  
       alternative = cc + GET(cc, 1);  
       if (*alternative == OP_KETRMAX || *alternative == OP_KETRMIN)  
         private_data_length += sizeof(sljit_sw);  
       }  
     else  
       private_data_length += sizeof(sljit_sw);  
     bracketlen = 1 + LINK_SIZE;  
782      break;      break;
783    
784      case OP_CREF:      case OP_CREF:
785      i = GET2(cc, 1);      common->optimized_cbracket[GET2(cc, 1)] = 0;
     common->optimized_cbracket[i] = 0;  
786      cc += 1 + IMM2_SIZE;      cc += 1 + IMM2_SIZE;
787      break;      break;
788    
789      case OP_NCREF:      case OP_DNREF:
790      bracketlen = GET2(cc, 1);      case OP_DNREFI:
791      name = (pcre_uchar *)common->name_table;      case OP_DNCREF:
792      alternative = name;      count = GET2(cc, 1 + IMM2_SIZE);
793      for (i = 0; i < common->name_count; i++)      slot = common->name_table + GET2(cc, 1) * common->name_entry_size;
794        {      while (count-- > 0)
       if (GET2(name, 0) == bracketlen) break;  
       name += common->name_entry_size;  
       }  
     SLJIT_ASSERT(i != common->name_count);  
   
     for (i = 0; i < common->name_count; i++)  
795        {        {
796        if (STRCMP_UC_UC(alternative + IMM2_SIZE, name + IMM2_SIZE) == 0)        common->optimized_cbracket[GET2(slot, 0)] = 0;
797          common->optimized_cbracket[GET2(alternative, 0)] = 0;        slot += common->name_entry_size;
       alternative += common->name_entry_size;  
798        }        }
799      bracketlen = 0;      cc += 1 + 2 * IMM2_SIZE;
     cc += 1 + IMM2_SIZE;  
     break;  
   
     case OP_BRA:  
     bracketlen = 1 + LINK_SIZE;  
     break;  
   
     case OP_CBRA:  
     case OP_SCBRA:  
     bracketlen = 1 + LINK_SIZE + IMM2_SIZE;  
     break;  
   
     CASE_ITERATOR_PRIVATE_DATA_1  
     space = 1;  
     size = -2;  
     break;  
   
     CASE_ITERATOR_PRIVATE_DATA_2A  
     space = 2;  
     size = -2;  
     break;  
   
     CASE_ITERATOR_PRIVATE_DATA_2B  
     space = 2;  
     size = -(2 + IMM2_SIZE);  
     break;  
   
     CASE_ITERATOR_TYPE_PRIVATE_DATA_1  
     space = 1;  
     size = 1;  
800      break;      break;
801    
     CASE_ITERATOR_TYPE_PRIVATE_DATA_2A  
     if (cc[1] != OP_ANYNL && cc[1] != OP_EXTUNI)  
       space = 2;  
     size = 1;  
     break;  
   
     CASE_ITERATOR_TYPE_PRIVATE_DATA_2B  
     if (cc[1 + IMM2_SIZE] != OP_ANYNL && cc[1 + IMM2_SIZE] != OP_EXTUNI)  
       space = 2;  
     size = 1 + IMM2_SIZE;  
     break;  
   
     case OP_CLASS:  
     case OP_NCLASS:  
     size += 1 + 32 / sizeof(pcre_uchar);  
     space = get_class_iterator_size(cc + size);  
     break;  
   
 #if defined SUPPORT_UTF || !defined COMPILE_PCRE8  
     case OP_XCLASS:  
     size = GET(cc, 1);  
     space = get_class_iterator_size(cc + size);  
     break;  
 #endif  
   
802      case OP_RECURSE:      case OP_RECURSE:
803      /* Set its value only once. */      /* Set its value only once. */
804      if (common->recursive_head_ptr == 0)      if (common->recursive_head_ptr == 0)
# Line 947  while (cc < ccend) Line 818  while (cc < ccend)
818      cc += 2 + 2 * LINK_SIZE;      cc += 2 + 2 * LINK_SIZE;
819      break;      break;
820    
821        case OP_THEN_ARG:
822        common->has_then = TRUE;
823        common->control_head_ptr = 1;
824        /* Fall through. */
825    
826      case OP_PRUNE_ARG:      case OP_PRUNE_ARG:
827      common->needs_start_ptr = TRUE;      common->needs_start_ptr = TRUE;
     common->control_head_ptr = 1;  
828      /* Fall through. */      /* Fall through. */
829    
830      case OP_MARK:      case OP_MARK:
# Line 961  while (cc < ccend) Line 836  while (cc < ccend)
836      cc += 1 + 2 + cc[1];      cc += 1 + 2 + cc[1];
837      break;      break;
838    
839        case OP_THEN:
840        common->has_then = TRUE;
841        common->control_head_ptr = 1;
842        /* Fall through. */
843    
844      case OP_PRUNE:      case OP_PRUNE:
845      case OP_SKIP:      case OP_SKIP:
846      common->needs_start_ptr = TRUE;      common->needs_start_ptr = TRUE;
847      /* Fall through. */      cc += 1;
848        break;
849    
850      case OP_COMMIT:      case OP_SKIP_ARG:
851      common->control_head_ptr = 1;      common->control_head_ptr = 1;
852      cc += 1;      common->has_skip_arg = TRUE;
853        cc += 1 + 2 + cc[1];
854      break;      break;
855    
856      default:      default:
857      cc = next_opcode(common, cc);      cc = next_opcode(common, cc);
858      if (cc == NULL)      if (cc == NULL)
859        return -1;        return FALSE;
860      break;      break;
861      }      }
862      }
863    return TRUE;
864    }
865    
866    if (space > 0 && cc >= end)  static int get_class_iterator_size(pcre_uchar *cc)
867      private_data_length += sizeof(sljit_sw) * space;  {
868    switch(*cc)
869      {
870      case OP_CRSTAR:
871      case OP_CRPLUS:
872      return 2;
873    
874    if (size != 0)    case OP_CRMINSTAR:
875      case OP_CRMINPLUS:
876      case OP_CRQUERY:
877      case OP_CRMINQUERY:
878      return 1;
879    
880      case OP_CRRANGE:
881      case OP_CRMINRANGE:
882      if (GET2(cc, 1) == GET2(cc, 1 + IMM2_SIZE))
883        return 0;
884      return 2;
885    
886      default:
887      return 0;
888      }
889    }
890    
891    static BOOL detect_repeat(compiler_common *common, pcre_uchar *begin)
892    {
893    pcre_uchar *end = bracketend(begin);
894    pcre_uchar *next;
895    pcre_uchar *next_end;
896    pcre_uchar *max_end;
897    pcre_uchar type;
898    sljit_sw length = end - begin;
899    int min, max, i;
900    
901    /* Detect fixed iterations first. */
902    if (end[-(1 + LINK_SIZE)] != OP_KET)
903      return FALSE;
904    
905    /* Already detected repeat. */
906    if (common->private_data_ptrs[end - common->start - LINK_SIZE] != 0)
907      return TRUE;
908    
909    next = end;
910    min = 1;
911    while (1)
912      {
913      if (*next != *begin)
914        break;
915      next_end = bracketend(next);
916      if (next_end - next != length || memcmp(begin, next, IN_UCHARS(length)) != 0)
917        break;
918      next = next_end;
919      min++;
920      }
921    
922    if (min == 2)
923      return FALSE;
924    
925    max = 0;
926    max_end = next;
927    if (*next == OP_BRAZERO || *next == OP_BRAMINZERO)
928      {
929      type = *next;
930      while (1)
931      {      {
932      if (size < 0)      if (next[0] != type || next[1] != OP_BRA || next[2 + LINK_SIZE] != *begin)
933        {        break;
934        cc += -size;      next_end = bracketend(next + 2 + LINK_SIZE);
935  #ifdef SUPPORT_UTF      if (next_end - next != (length + 2 + LINK_SIZE) || memcmp(begin, next + 2 + LINK_SIZE, IN_UCHARS(length)) != 0)
936        if (common->utf && HAS_EXTRALEN(cc[-1])) cc += GET_EXTRALEN(cc[-1]);        break;
937  #endif      next = next_end;
938        }      max++;
     else  
       cc += size;  
939      }      }
940    
941    if (bracketlen != 0)    if (next[0] == type && next[1] == *begin && max >= 1)
942      {      {
943      if (cc >= end)      next_end = bracketend(next + 1);
944        if (next_end - next == (length + 1) && memcmp(begin, next + 1, IN_UCHARS(length)) == 0)
945        {        {
946        end = bracketend(cc);        for (i = 0; i < max; i++, next_end += 1 + LINK_SIZE)
947        if (end[-1 - LINK_SIZE] == OP_KET)          if (*next_end != OP_KET)
948          end = NULL;            break;
949    
950          if (i == max)
951            {
952            common->private_data_ptrs[max_end - common->start - LINK_SIZE] = next_end - max_end;
953            common->private_data_ptrs[max_end - common->start - LINK_SIZE + 1] = (type == OP_BRAZERO) ? OP_UPTO : OP_MINUPTO;
954            /* +2 the original and the last. */
955            common->private_data_ptrs[max_end - common->start - LINK_SIZE + 2] = max + 2;
956            if (min == 1)
957              return TRUE;
958            min--;
959            max_end -= (1 + LINK_SIZE) + GET(max_end, -LINK_SIZE);
960            }
961        }        }
     cc += bracketlen;  
962      }      }
963    }    }
964  return private_data_length;  
965    if (min >= 3)
966      {
967      common->private_data_ptrs[end - common->start - LINK_SIZE] = max_end - end;
968      common->private_data_ptrs[end - common->start - LINK_SIZE + 1] = OP_EXACT;
969      common->private_data_ptrs[end - common->start - LINK_SIZE + 2] = min;
970      return TRUE;
971      }
972    
973    return FALSE;
974  }  }
975    
976  static void set_private_data_ptrs(compiler_common *common, int private_data_ptr, pcre_uchar *ccend)  #define CASE_ITERATOR_PRIVATE_DATA_1 \
977        case OP_MINSTAR: \
978        case OP_MINPLUS: \
979        case OP_QUERY: \
980        case OP_MINQUERY: \
981        case OP_MINSTARI: \
982        case OP_MINPLUSI: \
983        case OP_QUERYI: \
984        case OP_MINQUERYI: \
985        case OP_NOTMINSTAR: \
986        case OP_NOTMINPLUS: \
987        case OP_NOTQUERY: \
988        case OP_NOTMINQUERY: \
989        case OP_NOTMINSTARI: \
990        case OP_NOTMINPLUSI: \
991        case OP_NOTQUERYI: \
992        case OP_NOTMINQUERYI:
993    
994    #define CASE_ITERATOR_PRIVATE_DATA_2A \
995        case OP_STAR: \
996        case OP_PLUS: \
997        case OP_STARI: \
998        case OP_PLUSI: \
999        case OP_NOTSTAR: \
1000        case OP_NOTPLUS: \
1001        case OP_NOTSTARI: \
1002        case OP_NOTPLUSI:
1003    
1004    #define CASE_ITERATOR_PRIVATE_DATA_2B \
1005        case OP_UPTO: \
1006        case OP_MINUPTO: \
1007        case OP_UPTOI: \
1008        case OP_MINUPTOI: \
1009        case OP_NOTUPTO: \
1010        case OP_NOTMINUPTO: \
1011        case OP_NOTUPTOI: \
1012        case OP_NOTMINUPTOI:
1013    
1014    #define CASE_ITERATOR_TYPE_PRIVATE_DATA_1 \
1015        case OP_TYPEMINSTAR: \
1016        case OP_TYPEMINPLUS: \
1017        case OP_TYPEQUERY: \
1018        case OP_TYPEMINQUERY:
1019    
1020    #define CASE_ITERATOR_TYPE_PRIVATE_DATA_2A \
1021        case OP_TYPESTAR: \
1022        case OP_TYPEPLUS:
1023    
1024    #define CASE_ITERATOR_TYPE_PRIVATE_DATA_2B \
1025        case OP_TYPEUPTO: \
1026        case OP_TYPEMINUPTO:
1027    
1028    static void set_private_data_ptrs(compiler_common *common, int *private_data_start, pcre_uchar *ccend)
1029  {  {
1030  pcre_uchar *cc = common->start;  pcre_uchar *cc = common->start;
1031  pcre_uchar *alternative;  pcre_uchar *alternative;
1032  pcre_uchar *end = NULL;  pcre_uchar *end = NULL;
1033    int private_data_ptr = *private_data_start;
1034  int space, size, bracketlen;  int space, size, bracketlen;
1035    
1036  while (cc < ccend)  while (cc < ccend)
# Line 1020  while (cc < ccend) Line 1038  while (cc < ccend)
1038    space = 0;    space = 0;
1039    size = 0;    size = 0;
1040    bracketlen = 0;    bracketlen = 0;
1041      if (private_data_ptr > SLJIT_MAX_LOCAL_SIZE)
1042        return;
1043    
1044      if (*cc == OP_ONCE || *cc == OP_ONCE_NC || *cc == OP_BRA || *cc == OP_CBRA || *cc == OP_COND)
1045        if (detect_repeat(common, cc))
1046          {
1047          /* These brackets are converted to repeats, so no global
1048          based single character repeat is allowed. */
1049          if (cc >= end)
1050            end = bracketend(cc);
1051          }
1052    
1053    switch(*cc)    switch(*cc)
1054      {      {
1055        case OP_KET:
1056        if (common->private_data_ptrs[cc + 1 - common->start] != 0)
1057          {
1058          common->private_data_ptrs[cc - common->start] = private_data_ptr;
1059          private_data_ptr += sizeof(sljit_sw);
1060          cc += common->private_data_ptrs[cc + 1 - common->start];
1061          }
1062        cc += 1 + LINK_SIZE;
1063        break;
1064    
1065      case OP_ASSERT:      case OP_ASSERT:
1066      case OP_ASSERT_NOT:      case OP_ASSERT_NOT:
1067      case OP_ASSERTBACK:      case OP_ASSERTBACK:
# Line 1115  while (cc < ccend) Line 1155  while (cc < ccend)
1155      break;      break;
1156      }      }
1157    
1158      /* Character iterators, which are not inside a repeated bracket,
1159         gets a private slot instead of allocating it on the stack. */
1160    if (space > 0 && cc >= end)    if (space > 0 && cc >= end)
1161      {      {
1162      common->private_data_ptrs[cc - common->start] = private_data_ptr;      common->private_data_ptrs[cc - common->start] = private_data_ptr;
# Line 1145  while (cc < ccend) Line 1187  while (cc < ccend)
1187      cc += bracketlen;      cc += bracketlen;
1188      }      }
1189    }    }
1190    *private_data_start = private_data_ptr;
1191  }  }
1192    
1193  /* Returns with a frame_types (always < 0) if no need for frame. */  /* Returns with a frame_types (always < 0) if no need for frame. */
1194  static int get_framesize(compiler_common *common, pcre_uchar *cc, BOOL recursive, BOOL* needs_control_head)  static int get_framesize(compiler_common *common, pcre_uchar *cc, pcre_uchar *ccend, BOOL recursive, BOOL* needs_control_head)
1195  {  {
 pcre_uchar *ccend = bracketend(cc) - (1 + LINK_SIZE);  
1196  int length = 0;  int length = 0;
1197  int possessive = 0;  int possessive = 0;
1198  BOOL stack_restore = FALSE;  BOOL stack_restore = FALSE;
# Line 1166  SLJIT_ASSERT(common->control_head_ptr != Line 1208  SLJIT_ASSERT(common->control_head_ptr !=
1208  *needs_control_head = FALSE;  *needs_control_head = FALSE;
1209  #endif  #endif
1210    
1211  if (!recursive && (*cc == OP_CBRAPOS || *cc == OP_SCBRAPOS))  if (ccend == NULL)
1212    {    {
1213    possessive = length = (common->capture_last_ptr != 0) ? 5 : 3;    ccend = bracketend(cc) - (1 + LINK_SIZE);
1214    /* This is correct regardless of common->capture_last_ptr. */    if (!recursive && (*cc == OP_CBRAPOS || *cc == OP_SCBRAPOS))
1215    capture_last_found = TRUE;      {
1216        possessive = length = (common->capture_last_ptr != 0) ? 5 : 3;
1217        /* This is correct regardless of common->capture_last_ptr. */
1218        capture_last_found = TRUE;
1219        }
1220      cc = next_opcode(common, cc);
1221    }    }
1222    
 cc = next_opcode(common, cc);  
1223  SLJIT_ASSERT(cc != NULL);  SLJIT_ASSERT(cc != NULL);
1224  while (cc < ccend)  while (cc < ccend)
1225    switch(*cc)    switch(*cc)
# Line 1191  while (cc < ccend) Line 1237  while (cc < ccend)
1237    
1238      case OP_MARK:      case OP_MARK:
1239      case OP_PRUNE_ARG:      case OP_PRUNE_ARG:
1240        case OP_THEN_ARG:
1241      SLJIT_ASSERT(common->mark_ptr != 0);      SLJIT_ASSERT(common->mark_ptr != 0);
1242      stack_restore = TRUE;      stack_restore = TRUE;
1243      if (!setmark_found)      if (!setmark_found)
# Line 1237  while (cc < ccend) Line 1284  while (cc < ccend)
1284      cc += 1 + LINK_SIZE + IMM2_SIZE;      cc += 1 + LINK_SIZE + IMM2_SIZE;
1285      break;      break;
1286    
     case OP_PRUNE:  
     case OP_SKIP:  
     case OP_COMMIT:  
     if (common->control_head_ptr != 0)  
       *needs_control_head = TRUE;  
     /* Fall through. */  
   
1287      default:      default:
1288      stack_restore = TRUE;      stack_restore = TRUE;
1289      /* Fall through. */      /* Fall through. */
# Line 1326  if (length > 0) Line 1366  if (length > 0)
1366  return stack_restore ? no_frame : no_stack;  return stack_restore ? no_frame : no_stack;
1367  }  }
1368    
1369  static void init_frame(compiler_common *common, pcre_uchar *cc, int stackpos, int stacktop, BOOL recursive)  static void init_frame(compiler_common *common, pcre_uchar *cc, pcre_uchar *ccend, int stackpos, int stacktop, BOOL recursive)
1370  {  {
1371  DEFINE_COMPILER;  DEFINE_COMPILER;
 pcre_uchar *ccend = bracketend(cc) - (1 + LINK_SIZE);  
1372  BOOL setsom_found = recursive;  BOOL setsom_found = recursive;
1373  BOOL setmark_found = recursive;  BOOL setmark_found = recursive;
1374  /* The last capture is a local variable even for recursions. */  /* The last capture is a local variable even for recursions. */
# Line 1341  SLJIT_UNUSED_ARG(stacktop); Line 1380  SLJIT_UNUSED_ARG(stacktop);
1380  SLJIT_ASSERT(stackpos >= stacktop + 2);  SLJIT_ASSERT(stackpos >= stacktop + 2);
1381    
1382  stackpos = STACK(stackpos);  stackpos = STACK(stackpos);
1383  if (recursive || (*cc != OP_CBRAPOS && *cc != OP_SCBRAPOS))  if (ccend == NULL)
1384    cc = next_opcode(common, cc);    {
1385      ccend = bracketend(cc) - (1 + LINK_SIZE);
1386      if (recursive || (*cc != OP_CBRAPOS && *cc != OP_SCBRAPOS))
1387        cc = next_opcode(common, cc);
1388      }
1389    
1390  SLJIT_ASSERT(cc != NULL);  SLJIT_ASSERT(cc != NULL);
1391  while (cc < ccend)  while (cc < ccend)
1392    switch(*cc)    switch(*cc)
# Line 1363  while (cc < ccend) Line 1407  while (cc < ccend)
1407    
1408      case OP_MARK:      case OP_MARK:
1409      case OP_PRUNE_ARG:      case OP_PRUNE_ARG:
1410        case OP_THEN_ARG:
1411      SLJIT_ASSERT(common->mark_ptr != 0);      SLJIT_ASSERT(common->mark_ptr != 0);
1412      if (!setmark_found)      if (!setmark_found)
1413        {        {
# Line 1454  while (cc < ccend) Line 1499  while (cc < ccend)
1499    size = 0;    size = 0;
1500    switch(*cc)    switch(*cc)
1501      {      {
1502        case OP_KET:
1503        if (PRIVATE_DATA(cc) != 0)
1504          private_data_length++;
1505        cc += 1 + LINK_SIZE;
1506        break;
1507    
1508      case OP_ASSERT:      case OP_ASSERT:
1509      case OP_ASSERT_NOT:      case OP_ASSERT_NOT:
1510      case OP_ASSERTBACK:      case OP_ASSERTBACK:
# Line 1622  do Line 1673  do
1673    
1674      switch(*cc)      switch(*cc)
1675        {        {
1676          case OP_KET:
1677          if (PRIVATE_DATA(cc) != 0)
1678            {
1679            count = 1;
1680            srcw[0] = PRIVATE_DATA(cc);
1681            }
1682          cc += 1 + LINK_SIZE;
1683          break;
1684    
1685        case OP_ASSERT:        case OP_ASSERT:
1686        case OP_ASSERT_NOT:        case OP_ASSERT_NOT:
1687        case OP_ASSERTBACK:        case OP_ASSERTBACK:
# Line 1868  if (save) Line 1928  if (save)
1928  SLJIT_ASSERT(cc == ccend && stackptr == stacktop && (save || (tmp1empty && tmp2empty)));  SLJIT_ASSERT(cc == ccend && stackptr == stacktop && (save || (tmp1empty && tmp2empty)));
1929  }  }
1930    
1931    static SLJIT_INLINE pcre_uchar *set_then_offsets(compiler_common *common, pcre_uchar *cc, pcre_uint8 *current_offset)
1932    {
1933    pcre_uchar *end = bracketend(cc);
1934    BOOL has_alternatives = cc[GET(cc, 1)] == OP_ALT;
1935    
1936    /* Assert captures then. */
1937    if (*cc >= OP_ASSERT && *cc <= OP_ASSERTBACK_NOT)
1938      current_offset = NULL;
1939    /* Conditional block does not. */
1940    if (*cc == OP_COND || *cc == OP_SCOND)
1941      has_alternatives = FALSE;
1942    
1943    cc = next_opcode(common, cc);
1944    if (has_alternatives)
1945      current_offset = common->then_offsets + (cc - common->start);
1946    
1947    while (cc < end)
1948      {
1949      if ((*cc >= OP_ASSERT && *cc <= OP_ASSERTBACK_NOT) || (*cc >= OP_ONCE && *cc <= OP_SCOND))
1950        cc = set_then_offsets(common, cc, current_offset);
1951      else
1952        {
1953        if (*cc == OP_ALT && has_alternatives)
1954          current_offset = common->then_offsets + (cc + 1 + LINK_SIZE - common->start);
1955        if (*cc >= OP_THEN && *cc <= OP_THEN_ARG && current_offset != NULL)
1956          *current_offset = 1;
1957        cc = next_opcode(common, cc);
1958        }
1959      }
1960    
1961    return end;
1962    }
1963    
1964  #undef CASE_ITERATOR_PRIVATE_DATA_1  #undef CASE_ITERATOR_PRIVATE_DATA_1
1965  #undef CASE_ITERATOR_PRIVATE_DATA_2A  #undef CASE_ITERATOR_PRIVATE_DATA_2A
1966  #undef CASE_ITERATOR_PRIVATE_DATA_2B  #undef CASE_ITERATOR_PRIVATE_DATA_2B
# Line 1931  while (list_item) Line 2024  while (list_item)
2024  common->stubs = NULL;  common->stubs = NULL;
2025  }  }
2026    
2027  static SLJIT_INLINE void decrease_call_count(compiler_common *common)  static SLJIT_INLINE void count_match(compiler_common *common)
2028  {  {
2029  DEFINE_COMPILER;  DEFINE_COMPILER;
2030    
2031  OP2(SLJIT_SUB | SLJIT_SET_E, CALL_COUNT, 0, CALL_COUNT, 0, SLJIT_IMM, 1);  OP2(SLJIT_SUB | SLJIT_SET_E, COUNT_MATCH, 0, COUNT_MATCH, 0, SLJIT_IMM, 1);
2032  add_jump(compiler, &common->calllimit, JUMP(SLJIT_C_ZERO));  add_jump(compiler, &common->calllimit, JUMP(SLJIT_C_ZERO));
2033  }  }
2034    
# Line 2015  else Line 2108  else
2108  OP1(SLJIT_MOV, STACK_TOP, 0, ARGUMENTS, 0);  OP1(SLJIT_MOV, STACK_TOP, 0, ARGUMENTS, 0);
2109  if (common->mark_ptr != 0)  if (common->mark_ptr != 0)
2110    OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), common->mark_ptr, SLJIT_IMM, 0);    OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), common->mark_ptr, SLJIT_IMM, 0);
2111  SLJIT_ASSERT(common->control_head_ptr != 0);  if (common->control_head_ptr != 0)
2112  OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), common->control_head_ptr, SLJIT_IMM, 0);    OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), common->control_head_ptr, SLJIT_IMM, 0);
2113  OP1(SLJIT_MOV, STACK_TOP, 0, SLJIT_MEM1(STACK_TOP), SLJIT_OFFSETOF(jit_arguments, stack));  OP1(SLJIT_MOV, STACK_TOP, 0, SLJIT_MEM1(STACK_TOP), SLJIT_OFFSETOF(jit_arguments, stack));
2114  OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), common->start_ptr);  OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), common->start_ptr);
2115  OP1(SLJIT_MOV, STACK_TOP, 0, SLJIT_MEM1(STACK_TOP), SLJIT_OFFSETOF(struct sljit_stack, base));  OP1(SLJIT_MOV, STACK_TOP, 0, SLJIT_MEM1(STACK_TOP), SLJIT_OFFSETOF(struct sljit_stack, base));
2116  }  }
2117    
2118  static sljit_sw SLJIT_CALL do_check_control_chain(sljit_sw *current)  static sljit_sw SLJIT_CALL do_search_mark(sljit_sw *current, const pcre_uchar *skip_arg)
2119  {  {
2120  sljit_sw return_value = 0;  while (current != NULL)
   
 SLJIT_ASSERT(current != NULL);  
 do  
2121    {    {
2122    switch (current[-2])    switch (current[-2])
2123      {      {
2124      case type_commit:      case type_then_trap:
     /* Commit overwrites all. */  
     return -1;  
   
     case type_prune:  
2125      break;      break;
2126    
2127      case type_skip:      case type_mark:
2128      /* Overwrites prune, but not other skips. */      if (STRCMP_UC_UC(skip_arg, (pcre_uchar *)current[-3]) == 0)
2129      if (return_value == 0)        return current[-4];
       return_value = current[-3];  
2130      break;      break;
2131    
2132      default:      default:
# Line 2050  do Line 2135  do
2135      }      }
2136    current = (sljit_sw*)current[-1];    current = (sljit_sw*)current[-1];
2137    }    }
2138  while (current != NULL);  return -1;
 return return_value;  
2139  }  }
2140    
2141  static SLJIT_INLINE void copy_ovector(compiler_common *common, int topbracket)  static SLJIT_INLINE void copy_ovector(compiler_common *common, int topbracket)
# Line 2291  return (bit < 256) ? ((0 << 8) | bit) : Line 2375  return (bit < 256) ? ((0 << 8) | bit) :
2375    
2376  static void check_partial(compiler_common *common, BOOL force)  static void check_partial(compiler_common *common, BOOL force)
2377  {  {
2378  /* Checks whether a partial matching is occured. Does not modify registers. */  /* Checks whether a partial matching is occurred. Does not modify registers. */
2379  DEFINE_COMPILER;  DEFINE_COMPILER;
2380  struct sljit_jump *jump = NULL;  struct sljit_jump *jump = NULL;
2381    
# Line 2400  if (common->utf) Line 2484  if (common->utf)
2484    JUMPHERE(jump);    JUMPHERE(jump);
2485    }    }
2486  #endif /* SUPPORT_UTF && !COMPILE_PCRE32 */  #endif /* SUPPORT_UTF && !COMPILE_PCRE32 */
2487  OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));  OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
2488    }
2489    
2490    static void peek_char(compiler_common *common)
2491    {
2492    /* Reads the character into TMP1, keeps STR_PTR.
2493    Does not check STR_END. TMP2 Destroyed. */
2494    DEFINE_COMPILER;
2495    #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
2496    struct sljit_jump *jump;
2497    #endif
2498    
2499    OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), 0);
2500    #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
2501    if (common->utf)
2502      {
2503    #if defined COMPILE_PCRE8
2504      jump = CMP(SLJIT_C_LESS, TMP1, 0, SLJIT_IMM, 0xc0);
2505    #elif defined COMPILE_PCRE16
2506      jump = CMP(SLJIT_C_LESS, TMP1, 0, SLJIT_IMM, 0xd800);
2507    #endif /* COMPILE_PCRE[8|16] */
2508      add_jump(compiler, &common->utfreadchar, JUMP(SLJIT_FAST_CALL));
2509      OP2(SLJIT_SUB, STR_PTR, 0, STR_PTR, 0, TMP2, 0);
2510      JUMPHERE(jump);
2511      }
2512    #endif /* SUPPORT_UTF && !COMPILE_PCRE32 */
2513  }  }
2514    
2515  static void peek_char(compiler_common *common)  static void read_char8(compiler_common *common)
2516  {  {
2517  /* Reads the character into TMP1, keeps STR_PTR.  /* Reads the precise value of a character into TMP1, if the character is
2518  Does not check STR_END. TMP2 Destroyed. */  less than 256. Otherwise it returns with a value greater or equal than 256. */
2519  DEFINE_COMPILER;  DEFINE_COMPILER;
2520  #if defined SUPPORT_UTF && !defined COMPILE_PCRE32  #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
2521  struct sljit_jump *jump;  struct sljit_jump *jump;
2522  #endif  #endif
2523    
2524  OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), 0);  OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), 0);
2525    OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
2526    
2527  #if defined SUPPORT_UTF && !defined COMPILE_PCRE32  #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
2528  if (common->utf)  if (common->utf)
2529    {    {
2530  #if defined COMPILE_PCRE8  #if defined COMPILE_PCRE8
2531    jump = CMP(SLJIT_C_LESS, TMP1, 0, SLJIT_IMM, 0xc0);    jump = CMP(SLJIT_C_LESS, TMP1, 0, SLJIT_IMM, 0xc0);
2532      add_jump(compiler, &common->utfreadchar8, JUMP(SLJIT_FAST_CALL));
2533      JUMPHERE(jump);
2534  #elif defined COMPILE_PCRE16  #elif defined COMPILE_PCRE16
2535    jump = CMP(SLJIT_C_LESS, TMP1, 0, SLJIT_IMM, 0xd800);    /* Skip low surrogate if necessary. */
2536  #endif /* COMPILE_PCRE[8|16] */    OP2(SLJIT_SUB, TMP2, 0, TMP1, 0, SLJIT_IMM, 0xd800);
2537    add_jump(compiler, &common->utfreadchar, JUMP(SLJIT_FAST_CALL));    jump = CMP(SLJIT_C_GREATER, TMP2, 0, SLJIT_IMM, 0xdc00 - 0xd800 - 1);
2538    OP2(SLJIT_SUB, STR_PTR, 0, STR_PTR, 0, TMP2, 0);    OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
2539    JUMPHERE(jump);    JUMPHERE(jump);
2540    #endif /* COMPILE_PCRE[8|16] */
2541    }    }
2542  #endif /* SUPPORT_UTF && !COMPILE_PCRE32 */  #endif
2543  }  }
2544    
2545  static void read_char8_type(compiler_common *common)  static void read_char8_type(compiler_common *common)
# Line 2454  if (common->utf) Line 2568  if (common->utf)
2568    OP1(SLJIT_MOV_UB, TMP1, 0, SLJIT_MEM1(TMP2), common->ctypes);    OP1(SLJIT_MOV_UB, TMP1, 0, SLJIT_MEM1(TMP2), common->ctypes);
2569    JUMPHERE(jump);    JUMPHERE(jump);
2570    /* Skip low surrogate if necessary. */    /* Skip low surrogate if necessary. */
2571    OP2(SLJIT_AND, TMP2, 0, TMP2, 0, SLJIT_IMM, 0xfc00);    OP2(SLJIT_SUB, TMP2, 0, TMP2, 0, SLJIT_IMM, 0xd800);
2572    OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP2, 0, SLJIT_IMM, 0xd800);    jump = CMP(SLJIT_C_GREATER, TMP2, 0, SLJIT_IMM, 0xdc00 - 0xd800 - 1);
2573    OP_FLAGS(SLJIT_MOV, TMP2, 0, SLJIT_UNUSED, 0, SLJIT_C_EQUAL);    OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
2574    OP2(SLJIT_SHL, TMP2, 0, TMP2, 0, SLJIT_IMM, 1);    JUMPHERE(jump);
   OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, TMP2, 0);  
2575  #elif defined COMPILE_PCRE32  #elif defined COMPILE_PCRE32
2576    OP1(SLJIT_MOV, TMP1, 0, SLJIT_IMM, 0);    OP1(SLJIT_MOV, TMP1, 0, SLJIT_IMM, 0);
2577    jump = CMP(SLJIT_C_GREATER, TMP2, 0, SLJIT_IMM, 255);    jump = CMP(SLJIT_C_GREATER, TMP2, 0, SLJIT_IMM, 255);
# Line 2602  OP1(SLJIT_MOV, TMP2, 0, SLJIT_IMM, IN_UC Line 2715  OP1(SLJIT_MOV, TMP2, 0, SLJIT_IMM, IN_UC
2715  sljit_emit_fast_return(compiler, RETURN_ADDR, 0);  sljit_emit_fast_return(compiler, RETURN_ADDR, 0);
2716  }  }
2717    
2718    static void do_utfreadchar8(compiler_common *common)
2719    {
2720    /* Fast decoding a UTF-8 character. TMP1 contains the first byte
2721    of the character (>= 0xc0). Return value in TMP1. */
2722    DEFINE_COMPILER;
2723    struct sljit_jump *jump;
2724    
2725    sljit_emit_fast_enter(compiler, RETURN_ADDR, 0);
2726    
2727    OP2(SLJIT_AND | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x20);
2728    jump = JUMP(SLJIT_C_NOT_ZERO);
2729    /* Two byte sequence. */
2730    OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(0));
2731    OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
2732    OP2(SLJIT_AND, TMP1, 0, TMP1, 0, SLJIT_IMM, 0x1f);
2733    OP2(SLJIT_SHL, TMP1, 0, TMP1, 0, SLJIT_IMM, 6);
2734    OP2(SLJIT_AND, TMP2, 0, TMP2, 0, SLJIT_IMM, 0x3f);
2735    OP2(SLJIT_OR, TMP1, 0, TMP1, 0, TMP2, 0);
2736    sljit_emit_fast_return(compiler, RETURN_ADDR, 0);
2737    
2738    JUMPHERE(jump);
2739    OP1(SLJIT_MOV_UB, TMP2, 0, SLJIT_MEM1(TMP1), (sljit_sw)PRIV(utf8_table4) - 0xc0);
2740    OP1(SLJIT_MOV, TMP1, 0, SLJIT_IMM, 0x800);
2741    OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, TMP2, 0);
2742    sljit_emit_fast_return(compiler, RETURN_ADDR, 0);
2743    }
2744    
2745  static void do_utfreadtype8(compiler_common *common)  static void do_utfreadtype8(compiler_common *common)
2746  {  {
2747  /* Fast decoding a UTF-8 character type. TMP2 contains the first byte  /* Fast decoding a UTF-8 character type. TMP2 contains the first byte
# Line 2618  jump = JUMP(SLJIT_C_NOT_ZERO); Line 2758  jump = JUMP(SLJIT_C_NOT_ZERO);
2758  OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(0));  OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(0));
2759  OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));  OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
2760  OP2(SLJIT_AND, TMP2, 0, TMP2, 0, SLJIT_IMM, 0x1f);  OP2(SLJIT_AND, TMP2, 0, TMP2, 0, SLJIT_IMM, 0x1f);
2761    /* The upper 5 bits are known at this point. */
2762    compare = CMP(SLJIT_C_GREATER, TMP2, 0, SLJIT_IMM, 0x3);
2763  OP2(SLJIT_SHL, TMP2, 0, TMP2, 0, SLJIT_IMM, 6);  OP2(SLJIT_SHL, TMP2, 0, TMP2, 0, SLJIT_IMM, 6);
2764  OP2(SLJIT_AND, TMP1, 0, TMP1, 0, SLJIT_IMM, 0x3f);  OP2(SLJIT_AND, TMP1, 0, TMP1, 0, SLJIT_IMM, 0x3f);
2765  OP2(SLJIT_OR, TMP2, 0, TMP2, 0, TMP1, 0);  OP2(SLJIT_OR, TMP2, 0, TMP2, 0, TMP1, 0);
 compare = CMP(SLJIT_C_GREATER, TMP2, 0, SLJIT_IMM, 255);  
2766  OP1(SLJIT_MOV_UB, TMP1, 0, SLJIT_MEM1(TMP2), common->ctypes);  OP1(SLJIT_MOV_UB, TMP1, 0, SLJIT_MEM1(TMP2), common->ctypes);
2767  sljit_emit_fast_return(compiler, RETURN_ADDR, 0);  sljit_emit_fast_return(compiler, RETURN_ADDR, 0);
2768    
2769  JUMPHERE(compare);  JUMPHERE(compare);
2770  OP1(SLJIT_MOV, TMP1, 0, SLJIT_IMM, 0);  OP1(SLJIT_MOV, TMP1, 0, SLJIT_IMM, 0);
2771  sljit_emit_fast_return(compiler, RETURN_ADDR, 0);  sljit_emit_fast_return(compiler, RETURN_ADDR, 0);
 JUMPHERE(jump);  
2772    
2773  /* We only have types for characters less than 256. */  /* We only have types for characters less than 256. */
2774  OP1(SLJIT_MOV_UB, TMP1, 0, SLJIT_MEM1(TMP2), (sljit_sw)PRIV(utf8_table4) - 0xc0);  JUMPHERE(jump);
2775  OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, TMP1, 0);  OP1(SLJIT_MOV_UB, TMP2, 0, SLJIT_MEM1(TMP2), (sljit_sw)PRIV(utf8_table4) - 0xc0);
2776  OP1(SLJIT_MOV, TMP1, 0, SLJIT_IMM, 0);  OP1(SLJIT_MOV, TMP1, 0, SLJIT_IMM, 0);
2777    OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, TMP2, 0);
2778  sljit_emit_fast_return(compiler, RETURN_ADDR, 0);  sljit_emit_fast_return(compiler, RETURN_ADDR, 0);
2779  }  }
2780    
# Line 2815  if (newlinecheck) Line 2956  if (newlinecheck)
2956  return mainloop;  return mainloop;
2957  }  }
2958    
2959  #define MAX_N_CHARS 3  static int scan_prefix(compiler_common *common, pcre_uchar *cc, pcre_uint32 *chars, int max_chars)
   
 static SLJIT_INLINE BOOL fast_forward_first_n_chars(compiler_common *common, BOOL firstline)  
2960  {  {
2961  DEFINE_COMPILER;  /* Recursive function, which scans prefix literals. */
2962  struct sljit_label *start;  int len, repeat, len_save, consumed = 0;
2963  struct sljit_jump *quit;  pcre_int32 caseless, chr, mask;
2964  pcre_uint32 chars[MAX_N_CHARS * 2];  pcre_uchar *alternative, *cc_save;
2965  pcre_uchar *cc = common->start + 1 + LINK_SIZE;  BOOL last, any;
 int location = 0;  
 pcre_int32 len, c, bit, caseless;  
 int must_stop;  
   
 /* We do not support alternatives now. */  
 if (*(common->start + GET(common->start, 1)) == OP_ALT)  
   return FALSE;  
2966    
2967    repeat = 1;
2968  while (TRUE)  while (TRUE)
2969    {    {
2970      last = TRUE;
2971      any = FALSE;
2972    caseless = 0;    caseless = 0;
2973    must_stop = 1;    switch (*cc)
   switch(*cc)  
2974      {      {
     case OP_CHAR:  
     must_stop = 0;  
     cc++;  
     break;  
   
2975      case OP_CHARI:      case OP_CHARI:
2976      caseless = 1;      caseless = 1;
2977      must_stop = 0;      case OP_CHAR:
2978        last = FALSE;
2979      cc++;      cc++;
2980      break;      break;
2981    
# Line 2870  while (TRUE) Line 3000  while (TRUE)
3000      cc++;      cc++;
3001      break;      break;
3002    
3003        case OP_EXACTI:
3004        caseless = 1;
3005      case OP_EXACT:      case OP_EXACT:
3006        repeat = GET2(cc, 1);
3007        last = FALSE;
3008      cc += 1 + IMM2_SIZE;      cc += 1 + IMM2_SIZE;
3009      break;      break;
3010    
# Line 2881  while (TRUE) Line 3015  while (TRUE)
3015      cc++;      cc++;
3016      break;      break;
3017    
3018      case OP_EXACTI:      case OP_KET:
3019      caseless = 1;      cc += 1 + LINK_SIZE;
3020      cc += 1 + IMM2_SIZE;      continue;
3021    
3022        case OP_ALT:
3023        cc += GET(cc, 1);
3024        continue;
3025    
3026        case OP_ONCE:
3027        case OP_ONCE_NC:
3028        case OP_BRA:
3029        case OP_BRAPOS:
3030        case OP_CBRA:
3031        case OP_CBRAPOS:
3032        alternative = cc + GET(cc, 1);
3033        while (*alternative == OP_ALT)
3034          {
3035          max_chars = scan_prefix(common, alternative + 1 + LINK_SIZE, chars, max_chars);
3036          if (max_chars == 0)
3037            return consumed;
3038          alternative += GET(alternative, 1);
3039          }
3040    
3041        if (*cc == OP_CBRA || *cc == OP_CBRAPOS)
3042          cc += IMM2_SIZE;
3043        cc += 1 + LINK_SIZE;
3044        continue;
3045    
3046        case OP_CLASS:
3047        case OP_NCLASS:
3048        any = TRUE;
3049        cc += 1 + 32 / sizeof(pcre_uchar);
3050      break;      break;
3051    
3052      default:  #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
3053      must_stop = 2;      case OP_XCLASS:
3054        any = TRUE;
3055        cc += GET(cc, 1);
3056        break;
3057    #endif
3058    
3059        case OP_NOT_DIGIT:
3060        case OP_DIGIT:
3061        case OP_NOT_WHITESPACE:
3062        case OP_WHITESPACE:
3063        case OP_NOT_WORDCHAR:
3064        case OP_WORDCHAR:
3065        case OP_ANY:
3066        case OP_ALLANY:
3067        any = TRUE;
3068        cc++;
3069        break;
3070    
3071    #ifdef SUPPORT_UCP
3072        case OP_NOTPROP:
3073        case OP_PROP:
3074        any = TRUE;
3075        cc += 1 + 2;
3076      break;      break;
3077    #endif
3078    
3079        case OP_TYPEEXACT:
3080        repeat = GET2(cc, 1);
3081        cc += 1 + IMM2_SIZE;
3082        continue;
3083    
3084        default:
3085        return consumed;
3086      }      }
3087    
3088    if (must_stop == 2)    if (any)
3089        break;      {
3090    #ifdef SUPPORT_UTF
3091        if (common->utf) return consumed;
3092    #endif
3093    #if defined COMPILE_PCRE8
3094        mask = 0xff;
3095    #elif defined COMPILE_PCRE16
3096        mask = 0xffff;
3097    #elif defined COMPILE_PCRE32
3098        mask = 0xffffffff;
3099    #else
3100        SLJIT_ASSERT_STOP();
3101    #endif
3102    
3103        do
3104          {
3105          chars[0] = mask;
3106          chars[1] = mask;
3107    
3108          if (--max_chars == 0)
3109            return consumed;
3110          consumed++;
3111          chars += 2;
3112          }
3113        while (--repeat > 0);
3114    
3115        repeat = 1;
3116        continue;
3117        }
3118    
3119    len = 1;    len = 1;
3120  #ifdef SUPPORT_UTF  #ifdef SUPPORT_UTF
3121    if (common->utf && HAS_EXTRALEN(cc[0])) len += GET_EXTRALEN(cc[0]);    if (common->utf && HAS_EXTRALEN(*cc)) len += GET_EXTRALEN(*cc);
3122  #endif  #endif
3123    
3124    if (caseless && char_has_othercase(common, cc))    if (caseless != 0 && char_has_othercase(common, cc))
3125      {      {
3126      caseless = char_get_othercase_bit(common, cc);      caseless = char_get_othercase_bit(common, cc);
3127      if (caseless == 0)      if (caseless == 0)
3128        return FALSE;        return consumed;
3129  #ifdef COMPILE_PCRE8  #ifdef COMPILE_PCRE8
3130      caseless = ((caseless & 0xff) << 8) | (len - (caseless >> 8));      caseless = ((caseless & 0xff) << 8) | (len - (caseless >> 8));
3131  #else  #else
# Line 2916  while (TRUE) Line 3138  while (TRUE)
3138    else    else
3139      caseless = 0;      caseless = 0;
3140    
3141    while (len > 0 && location < MAX_N_CHARS * 2)    len_save = len;
3142      {    cc_save = cc;
3143      c = *cc;    while (TRUE)
3144      bit = 0;      {
3145      if (len == (caseless & 0xff))      do
3146        {        {
3147        bit = caseless >> 8;        chr = *cc;
3148        c |= bit;  #ifdef COMPILE_PCRE32
3149          if (SLJIT_UNLIKELY(chr == NOTACHAR))
3150            return consumed;
3151    #endif
3152          mask = 0;
3153          if (len == (caseless & 0xff))
3154            {
3155            mask = caseless >> 8;
3156            chr |= mask;
3157            }
3158    
3159          if (chars[0] == NOTACHAR)
3160            {
3161            chars[0] = chr;
3162            chars[1] = mask;
3163            }
3164          else
3165            {
3166            mask |= chars[0] ^ chr;
3167            chr |= mask;
3168            chars[0] = chr;
3169            chars[1] |= mask;
3170            }
3171    
3172          len--;
3173          if (--max_chars == 0)
3174            return consumed;
3175          consumed++;
3176          chars += 2;
3177          cc++;
3178        }        }
3179        while (len > 0);
3180    
3181        if (--repeat == 0)
3182          break;
3183    
3184      chars[location] = c;      len = len_save;
3185      chars[location + 1] = bit;      cc = cc_save;
3186        }
3187    
3188      len--;    repeat = 1;
3189      location += 2;    if (last)
3190      cc++;      return consumed;
3191      }
3192    }
3193    
3194    #define MAX_N_CHARS 16
3195    
3196    static SLJIT_INLINE BOOL fast_forward_first_n_chars(compiler_common *common, BOOL firstline)
3197    {
3198    DEFINE_COMPILER;
3199    struct sljit_label *start;
3200    struct sljit_jump *quit;
3201    pcre_uint32 chars[MAX_N_CHARS * 2];
3202    pcre_uint8 ones[MAX_N_CHARS];
3203    pcre_uint32 mask;
3204    int i, max;
3205    int offsets[3];
3206    
3207    for (i = 0; i < MAX_N_CHARS; i++)
3208      {
3209      chars[i << 1] = NOTACHAR;
3210      chars[(i << 1) + 1] = 0;
3211      }
3212    
3213    max = scan_prefix(common, common->start, chars, MAX_N_CHARS);
3214    
3215    if (max <= 1)
3216      return FALSE;
3217    
3218    for (i = 0; i < max; i++)
3219      {
3220      mask = chars[(i << 1) + 1];
3221      ones[i] = ones_in_half_byte[mask & 0xf];
3222      mask >>= 4;
3223      while (mask != 0)
3224        {
3225        ones[i] += ones_in_half_byte[mask & 0xf];
3226        mask >>= 4;
3227      }      }
3228      }
3229    
3230    if (location >= MAX_N_CHARS * 2 || must_stop != 0)  offsets[0] = -1;
3231    /* Scan forward. */
3232    for (i = 0; i < max; i++)
3233      if (ones[i] <= 2) {
3234        offsets[0] = i;
3235      break;      break;
3236    }    }
3237    
3238  /* At least two characters are required. */  if (offsets[0] == -1)
3239  if (location < 2 * 2)    return FALSE;
3240      return FALSE;  
3241    /* Scan backward. */
3242    offsets[1] = -1;
3243    for (i = max - 1; i > offsets[0]; i--)
3244      if (ones[i] <= 2) {
3245        offsets[1] = i;
3246        break;
3247      }
3248    
3249    offsets[2] = -1;
3250    if (offsets[1] >= 0)
3251      {
3252      /* Scan from middle. */
3253      for (i = (offsets[0] + offsets[1]) / 2 + 1; i < offsets[1]; i++)
3254        if (ones[i] <= 2)
3255          {
3256          offsets[2] = i;
3257          break;
3258          }
3259    
3260      if (offsets[2] == -1)
3261        {
3262        for (i = (offsets[0] + offsets[1]) / 2; i > offsets[0]; i--)
3263          if (ones[i] <= 2)
3264            {
3265            offsets[2] = i;
3266            break;
3267            }
3268        }
3269      }
3270    
3271    SLJIT_ASSERT(offsets[1] == -1 || (offsets[0] < offsets[1]));
3272    SLJIT_ASSERT(offsets[2] == -1 || (offsets[0] < offsets[2] && offsets[1] > offsets[2]));
3273    
3274    chars[0] = chars[offsets[0] << 1];
3275    chars[1] = chars[(offsets[0] << 1) + 1];
3276    if (offsets[2] >= 0)
3277      {
3278      chars[2] = chars[offsets[2] << 1];
3279      chars[3] = chars[(offsets[2] << 1) + 1];
3280      }
3281    if (offsets[1] >= 0)
3282      {
3283      chars[4] = chars[offsets[1] << 1];
3284      chars[5] = chars[(offsets[1] << 1) + 1];
3285      }
3286    
3287    max -= 1;
3288  if (firstline)  if (firstline)
3289    {    {
3290    SLJIT_ASSERT(common->first_line_end != 0);    SLJIT_ASSERT(common->first_line_end != 0);
3291    OP1(SLJIT_MOV, TMP3, 0, STR_END, 0);    OP1(SLJIT_MOV, TMP3, 0, STR_END, 0);
3292    OP2(SLJIT_SUB, STR_END, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), common->first_line_end, SLJIT_IMM, IN_UCHARS((location >> 1) - 1));    OP2(SLJIT_SUB, STR_END, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), common->first_line_end, SLJIT_IMM, IN_UCHARS(max));
3293    }    }
3294  else  else
3295    OP2(SLJIT_SUB, STR_END, 0, STR_END, 0, SLJIT_IMM, IN_UCHARS((location >> 1) - 1));    OP2(SLJIT_SUB, STR_END, 0, STR_END, 0, SLJIT_IMM, IN_UCHARS(max));
3296    
3297  start = LABEL();  start = LABEL();
3298  quit = CMP(SLJIT_C_GREATER_EQUAL, STR_PTR, 0, STR_END, 0);  quit = CMP(SLJIT_C_GREATER_EQUAL, STR_PTR, 0, STR_END, 0);
3299    
3300  OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(0));  OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(offsets[0]));
3301  OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(1));  if (offsets[1] >= 0)
3302      OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(offsets[1]));
3303  OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));  OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
3304    
3305  if (chars[1] != 0)  if (chars[1] != 0)
3306    OP2(SLJIT_OR, TMP1, 0, TMP1, 0, SLJIT_IMM, chars[1]);    OP2(SLJIT_OR, TMP1, 0, TMP1, 0, SLJIT_IMM, chars[1]);
3307  CMPTO(SLJIT_C_NOT_EQUAL, TMP1, 0, SLJIT_IMM, chars[0], start);  CMPTO(SLJIT_C_NOT_EQUAL, TMP1, 0, SLJIT_IMM, chars[0], start);
3308  if (location > 2 * 2)  if (offsets[2] >= 0)
3309    OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(1));    OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(offsets[2] - 1));
3310  if (chars[3] != 0)  
3311    OP2(SLJIT_OR, TMP2, 0, TMP2, 0, SLJIT_IMM, chars[3]);  if (offsets[1] >= 0)
 CMPTO(SLJIT_C_NOT_EQUAL, TMP2, 0, SLJIT_IMM, chars[2], start);  
 if (location > 2 * 2)  
3312    {    {
3313    if (chars[5] != 0)    if (chars[5] != 0)
3314      OP2(SLJIT_OR, TMP1, 0, TMP1, 0, SLJIT_IMM, chars[5]);      OP2(SLJIT_OR, TMP2, 0, TMP2, 0, SLJIT_IMM, chars[5]);
3315    CMPTO(SLJIT_C_NOT_EQUAL, TMP1, 0, SLJIT_IMM, chars[4], start);    CMPTO(SLJIT_C_NOT_EQUAL, TMP2, 0, SLJIT_IMM, chars[4], start);
3316      }
3317    
3318    if (offsets[2] >= 0)
3319      {
3320      if (chars[3] != 0)
3321        OP2(SLJIT_OR, TMP1, 0, TMP1, 0, SLJIT_IMM, chars[3]);
3322      CMPTO(SLJIT_C_NOT_EQUAL, TMP1, 0, SLJIT_IMM, chars[2], start);
3323    }    }
3324  OP2(SLJIT_SUB, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));  OP2(SLJIT_SUB, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
3325    
# Line 2978  JUMPHERE(quit); Line 3328  JUMPHERE(quit);
3328  if (firstline)  if (firstline)
3329    OP1(SLJIT_MOV, STR_END, 0, TMP3, 0);    OP1(SLJIT_MOV, STR_END, 0, TMP3, 0);
3330  else  else
3331    OP2(SLJIT_ADD, STR_END, 0, STR_END, 0, SLJIT_IMM, IN_UCHARS((location >> 1) - 1));    OP2(SLJIT_ADD, STR_END, 0, STR_END, 0, SLJIT_IMM, IN_UCHARS(max));
3332  return TRUE;  return TRUE;
3333  }  }
3334    
# Line 3127  if (firstline) Line 3477  if (firstline)
3477    OP1(SLJIT_MOV, STR_END, 0, TMP3, 0);    OP1(SLJIT_MOV, STR_END, 0, TMP3, 0);
3478  }  }
3479    
3480  static BOOL check_class_ranges(compiler_common *common, const pcre_uint8 *bits, BOOL nclass, jump_list **backtracks);  static BOOL check_class_ranges(compiler_common *common, const pcre_uint8 *bits, BOOL nclass, BOOL invert, jump_list **backtracks);
3481    
3482  static SLJIT_INLINE void fast_forward_start_bits(compiler_common *common, sljit_uw start_bits, BOOL firstline)  static SLJIT_INLINE void fast_forward_start_bits(compiler_common *common, pcre_uint8 *start_bits, BOOL firstline)
3483  {  {
3484  DEFINE_COMPILER;  DEFINE_COMPILER;
3485  struct sljit_label *start;  struct sljit_label *start;
3486  struct sljit_jump *quit;  struct sljit_jump *quit;
3487  struct sljit_jump *found = NULL;  struct sljit_jump *found = NULL;
3488  jump_list *matches = NULL;  jump_list *matches = NULL;
 pcre_uint8 inverted_start_bits[32];  
 int i;  
3489  #ifndef COMPILE_PCRE8  #ifndef COMPILE_PCRE8
3490  struct sljit_jump *jump;  struct sljit_jump *jump;
3491  #endif  #endif
3492    
 for (i = 0; i < 32; ++i)  
   inverted_start_bits[i] = ~(((pcre_uint8*)start_bits)[i]);  
   
3493  if (firstline)  if (firstline)
3494    {    {
3495    SLJIT_ASSERT(common->first_line_end != 0);    SLJIT_ASSERT(common->first_line_end != 0);
# Line 3160  if (common->utf) Line 3505  if (common->utf)
3505    OP1(SLJIT_MOV, TMP3, 0, TMP1, 0);    OP1(SLJIT_MOV, TMP3, 0, TMP1, 0);
3506  #endif  #endif
3507    
3508  if (!check_class_ranges(common, inverted_start_bits, (inverted_start_bits[31] & 0x80) != 0, &matches))  if (!check_class_ranges(common, start_bits, (start_bits[31] & 0x80) != 0, TRUE, &matches))
3509    {    {
3510  #ifndef COMPILE_PCRE8  #ifndef COMPILE_PCRE8
3511    jump = CMP(SLJIT_C_LESS, TMP1, 0, SLJIT_IMM, 255);    jump = CMP(SLJIT_C_LESS, TMP1, 0, SLJIT_IMM, 255);
# Line 3169  if (!check_class_ranges(common, inverted Line 3514  if (!check_class_ranges(common, inverted
3514  #endif  #endif
3515    OP2(SLJIT_AND, TMP2, 0, TMP1, 0, SLJIT_IMM, 0x7);    OP2(SLJIT_AND, TMP2, 0, TMP1, 0, SLJIT_IMM, 0x7);
3516    OP2(SLJIT_LSHR, TMP1, 0, TMP1, 0, SLJIT_IMM, 3);    OP2(SLJIT_LSHR, TMP1, 0, TMP1, 0, SLJIT_IMM, 3);
3517    OP1(SLJIT_MOV_UB, TMP1, 0, SLJIT_MEM1(TMP1), start_bits);    OP1(SLJIT_MOV_UB, TMP1, 0, SLJIT_MEM1(TMP1), (sljit_sw)start_bits);
3518    OP2(SLJIT_SHL, TMP2, 0, SLJIT_IMM, 1, TMP2, 0);    OP2(SLJIT_SHL, TMP2, 0, SLJIT_IMM, 1, TMP2, 0);
3519    OP2(SLJIT_AND | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, TMP2, 0);    OP2(SLJIT_AND | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, TMP2, 0);
3520    found = JUMP(SLJIT_C_NOT_ZERO);    found = JUMP(SLJIT_C_NOT_ZERO);
# Line 3429  sljit_emit_fast_return(compiler, SLJIT_M Line 3774  sljit_emit_fast_return(compiler, SLJIT_M
3774  static BOOL check_ranges(compiler_common *common, int *ranges, jump_list **backtracks, BOOL readch)  static BOOL check_ranges(compiler_common *common, int *ranges, jump_list **backtracks, BOOL readch)
3775  {  {
3776  DEFINE_COMPILER;  DEFINE_COMPILER;
3777  struct sljit_jump *jump;  int offset;
3778    
3779  if (ranges[0] < 0)  if (ranges[0] < 0 || ranges[0] > 4)
3780    return FALSE;    return FALSE;
3781    
3782    /* No character is accepted. */
3783    if (ranges[0] == 0 && ranges[1] == 0)
3784      add_jump(compiler, backtracks, JUMP(SLJIT_JUMP));
3785    
3786    if (readch)
3787      read_char8(common);
3788    
3789  switch(ranges[0])  switch(ranges[0])
3790    {    {
3791      case 0:
3792      /* When ranges[1] != 0, all characters are accepted. */
3793      return TRUE;
3794    
3795    case 1:    case 1:
   if (readch)  
     read_char(common);  
3796    add_jump(compiler, backtracks, CMP(ranges[1] == 0 ? SLJIT_C_LESS : SLJIT_C_GREATER_EQUAL, TMP1, 0, SLJIT_IMM, ranges[2]));    add_jump(compiler, backtracks, CMP(ranges[1] == 0 ? SLJIT_C_LESS : SLJIT_C_GREATER_EQUAL, TMP1, 0, SLJIT_IMM, ranges[2]));
3797    return TRUE;    return TRUE;
3798    
3799    case 2:    case 2:
3800    if (readch)    if (ranges[2] + 1 != ranges[3])
3801      read_char(common);      {
3802    OP2(SLJIT_SUB, TMP1, 0, TMP1, 0, SLJIT_IMM, ranges[2]);      OP2(SLJIT_SUB, TMP1, 0, TMP1, 0, SLJIT_IMM, ranges[2]);
3803    add_jump(compiler, backtracks, CMP(ranges[1] != 0 ? SLJIT_C_LESS : SLJIT_C_GREATER_EQUAL, TMP1, 0, SLJIT_IMM, ranges[3] - ranges[2]));      add_jump(compiler, backtracks, CMP(ranges[1] != 0 ? SLJIT_C_LESS : SLJIT_C_GREATER_EQUAL, TMP1, 0, SLJIT_IMM, ranges[3] - ranges[2]));
3804        }
3805      else
3806        add_jump(compiler, backtracks, CMP(ranges[1] != 0 ? SLJIT_C_EQUAL : SLJIT_C_NOT_EQUAL, TMP1, 0, SLJIT_IMM, ranges[2]));
3807    return TRUE;    return TRUE;
3808    
3809    case 4:    case 3:
3810    if (ranges[2] + 1 == ranges[3] && ranges[4] + 1 == ranges[5])    if (ranges[1] != 0)
3811      {      {
3812      if (readch)      add_jump(compiler, backtracks, CMP(SLJIT_C_GREATER_EQUAL, TMP1, 0, SLJIT_IMM, ranges[4]));
3813        read_char(common);      if (ranges[2] + 1 != ranges[3])
     if (ranges[1] != 0)  
3814        {        {
3815        add_jump(compiler, backtracks, CMP(SLJIT_C_EQUAL, TMP1, 0, SLJIT_IMM, ranges[2]));        OP2(SLJIT_SUB, TMP1, 0, TMP1, 0, SLJIT_IMM, ranges[2]);
3816        add_jump(compiler, backtracks, CMP(SLJIT_C_EQUAL, TMP1, 0, SLJIT_IMM, ranges[4]));        add_jump(compiler, backtracks, CMP(SLJIT_C_LESS, TMP1, 0, SLJIT_IMM, ranges[3] - ranges[2]));
3817        }        }
3818      else      else
3819        {        add_jump(compiler, backtracks, CMP(SLJIT_C_EQUAL, TMP1, 0, SLJIT_IMM, ranges[2]));
       jump = CMP(SLJIT_C_EQUAL, TMP1, 0, SLJIT_IMM, ranges[2]);  
       add_jump(compiler, backtracks, CMP(SLJIT_C_NOT_EQUAL, TMP1, 0, SLJIT_IMM, ranges[4]));  
       JUMPHERE(jump);  
       }  
3820      return TRUE;      return TRUE;
3821      }      }
3822    if ((ranges[3] - ranges[2]) == (ranges[5] - ranges[4]) && is_powerof2(ranges[4] - ranges[2]))  
3823      add_jump(compiler, backtracks, CMP(SLJIT_C_LESS, TMP1, 0, SLJIT_IMM, ranges[2]));
3824      if (ranges[3] + 1 != ranges[4])
3825        {
3826        OP2(SLJIT_SUB, TMP1, 0, TMP1, 0, SLJIT_IMM, ranges[3]);
3827        add_jump(compiler, backtracks, CMP(SLJIT_C_LESS, TMP1, 0, SLJIT_IMM, ranges[4] - ranges[3]));
3828        }
3829      else
3830        add_jump(compiler, backtracks, CMP(SLJIT_C_EQUAL, TMP1, 0, SLJIT_IMM, ranges[3]));
3831      return TRUE;
3832    
3833      case 4:
3834      if ((ranges[3] - ranges[2]) == (ranges[5] - ranges[4])
3835          && (ranges[2] | (ranges[4] - ranges[2])) == ranges[4]
3836          && is_powerof2(ranges[4] - ranges[2]))
3837      {      {
     if (readch)  
       read_char(common);  
3838      OP2(SLJIT_OR, TMP1, 0, TMP1, 0, SLJIT_IMM, ranges[4] - ranges[2]);      OP2(SLJIT_OR, TMP1, 0, TMP1, 0, SLJIT_IMM, ranges[4] - ranges[2]);
3839      OP2(SLJIT_SUB, TMP1, 0, TMP1, 0, SLJIT_IMM, ranges[4]);      if (ranges[4] + 1 != ranges[5])
3840      add_jump(compiler, backtracks, CMP(ranges[1] != 0 ? SLJIT_C_LESS : SLJIT_C_GREATER_EQUAL, TMP1, 0, SLJIT_IMM, ranges[5] - ranges[4]));        {
3841          OP2(SLJIT_SUB, TMP1, 0, TMP1, 0, SLJIT_IMM, ranges[4]);
3842          add_jump(compiler, backtracks, CMP(ranges[1] != 0 ? SLJIT_C_LESS : SLJIT_C_GREATER_EQUAL, TMP1, 0, SLJIT_IMM, ranges[5] - ranges[4]));
3843          }
3844        else
3845          add_jump(compiler, backtracks, CMP(ranges[1] != 0 ? SLJIT_C_EQUAL : SLJIT_C_NOT_EQUAL, TMP1, 0, SLJIT_IMM, ranges[4]));
3846      return TRUE;      return TRUE;
3847      }      }
   return FALSE;  
   
   default:  
   return FALSE;  
   }  
 }  
3848    
3849  static void get_ctype_ranges(compiler_common *common, int flag, int *ranges)    if (ranges[1] != 0)
3850  {      {
3851  int i, bit, length;      offset = 0;
3852  const pcre_uint8 *ctypes = (const pcre_uint8*)common->ctypes;      if (ranges[2] + 1 != ranges[3])
3853          {
3854          OP2(SLJIT_SUB, TMP1, 0, TMP1, 0, SLJIT_IMM, ranges[2]);
3855          add_jump(compiler, backtracks, CMP(SLJIT_C_LESS, TMP1, 0, SLJIT_IMM, ranges[3] - ranges[2]));
3856          offset = ranges[2];
3857          }
3858        else
3859          add_jump(compiler, backtracks, CMP(SLJIT_C_EQUAL, TMP1, 0, SLJIT_IMM, ranges[2]));
3860    
3861  bit = ctypes[0] & flag;      if (ranges[4] + 1 != ranges[5])
3862  ranges[0] = -1;        {
3863  ranges[1] = bit != 0 ? 1 : 0;        OP2(SLJIT_SUB, TMP1, 0, TMP1, 0, SLJIT_IMM, ranges[4] - offset);
3864  length = 0;        add_jump(compiler, backtracks, CMP(SLJIT_C_LESS, TMP1, 0, SLJIT_IMM, ranges[5] - ranges[4]));
3865          }
3866        else
3867          add_jump(compiler, backtracks, CMP(SLJIT_C_EQUAL, TMP1, 0, SLJIT_IMM, ranges[4] - offset));
3868        return TRUE;
3869        }
3870    
3871  for (i = 1; i < 256; i++)    OP2(SLJIT_SUB, TMP1, 0, TMP1, 0, SLJIT_IMM, ranges[2]);
3872    if ((ctypes[i] & flag) != bit)    add_jump(compiler, backtracks, CMP(SLJIT_C_GREATER_EQUAL, TMP1, 0, SLJIT_IMM, ranges[5] - ranges[2]));
3873      if (ranges[3] + 1 != ranges[4])
3874      {      {
3875      if (length >= MAX_RANGE_SIZE)      OP2(SLJIT_SUB, TMP1, 0, TMP1, 0, SLJIT_IMM, ranges[3] - ranges[2]);
3876        return;      add_jump(compiler, backtracks, CMP(SLJIT_C_LESS, TMP1, 0, SLJIT_IMM, ranges[4] - ranges[3]));
     ranges[2 + length] = i;  
     length++;  
     bit ^= flag;  
3877      }      }
3878      else
3879  if (bit != 0)      add_jump(compiler, backtracks, CMP(SLJIT_C_EQUAL, TMP1, 0, SLJIT_IMM, ranges[3] - ranges[2]));
3880    {    return TRUE;
3881    if (length >= MAX_RANGE_SIZE)  
3882      return;    default:
3883    ranges[2 + length] = 256;    SLJIT_ASSERT_STOP();
3884    length++;    return FALSE;
3885    }    }
 ranges[0] = length;  
3886  }  }
3887    
3888  static BOOL check_class_ranges(compiler_common *common, const pcre_uint8 *bits, BOOL nclass, jump_list **backtracks)  static BOOL check_class_ranges(compiler_common *common, const pcre_uint8 *bits, BOOL nclass, BOOL invert, jump_list **backtracks)
3889  {  {
3890  int ranges[2 + MAX_RANGE_SIZE];  int ranges[2 + MAX_RANGE_SIZE];
3891  pcre_uint8 bit, cbit, all;  pcre_uint8 bit, cbit, all;
3892  int i, byte, length = 0;  int i, byte, length = 0;
3893    
3894  bit = bits[0] & 0x1;  bit = bits[0] & 0x1;
3895  ranges[1] = bit;  ranges[1] = !invert ? bit : (bit ^ 0x1);
3896  /* Can be 0 or 255. */  /* All bits will be zero or one (since bit is zero or one). */
3897  all = -bit;  all = -bit;
3898    
3899  for (i = 0; i < 256; )  for (i = 0; i < 256; )
# Line 3942  static void compile_xclass_matchingpath( Line 4314  static void compile_xclass_matchingpath(
4314  {  {
4315  DEFINE_COMPILER;  DEFINE_COMPILER;
4316  jump_list *found = NULL;  jump_list *found = NULL;
4317  jump_list **list = (*cc & XCL_NOT) == 0 ? &found : backtracks;  jump_list **list = (cc[0] & XCL_NOT) == 0 ? &found : backtracks;
4318  pcre_int32 c, charoffset;  pcre_int32 c, charoffset;
 const pcre_uint32 *other_cases;  
4319  struct sljit_jump *jump = NULL;  struct sljit_jump *jump = NULL;
4320  pcre_uchar *ccbegin;  pcre_uchar *ccbegin;
4321  int compares, invertcmp, numberofcmps;  int compares, invertcmp, numberofcmps;
4322    
4323  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
4324  BOOL needstype = FALSE, needsscript = FALSE, needschar = FALSE;  BOOL needstype = FALSE, needsscript = FALSE, needschar = FALSE;
4325  BOOL charsaved = FALSE;  BOOL charsaved = FALSE;
4326  int typereg = TMP1, scriptreg = TMP1;  int typereg = TMP1, scriptreg = TMP1;
4327    const pcre_uint32 *other_cases;
4328  pcre_int32 typeoffset;  pcre_int32 typeoffset;
4329  #endif  #endif
4330    
# Line 3960  pcre_int32 typeoffset; Line 4333  pcre_int32 typeoffset;
4333  detect_partial_match(common, backtracks);  detect_partial_match(common, backtracks);
4334  read_char(common);  read_char(common);
4335    
4336  if ((*cc++ & XCL_MAP) != 0)  cc++;
4337    if ((cc[-1] & XCL_HASPROP) == 0)
4338    {    {
4339    OP1(SLJIT_MOV, TMP3, 0, TMP1, 0);    if ((cc[-1] & XCL_MAP) != 0)
4340  #ifndef COMPILE_PCRE8      {
4341    jump = CMP(SLJIT_C_GREATER, TMP1, 0, SLJIT_IMM, 255);      OP1(SLJIT_MOV, TMP3, 0, TMP1, 0);
4342  #elif defined SUPPORT_UTF  #ifdef SUPPORT_UCP
4343    if (common->utf)      charsaved = TRUE;
     jump = CMP(SLJIT_C_GREATER, TMP1, 0, SLJIT_IMM, 255);  
4344  #endif  #endif
4345        if (!check_class_ranges(common, (const pcre_uint8 *)cc, TRUE, FALSE, backtracks))
4346          {
4347          jump = CMP(SLJIT_C_GREATER, TMP1, 0, SLJIT_IMM, 255);
4348    
4349          OP2(SLJIT_AND, TMP2, 0, TMP1, 0, SLJIT_IMM, 0x7);
4350          OP2(SLJIT_LSHR, TMP1, 0, TMP1, 0, SLJIT_IMM, 3);
4351          OP1(SLJIT_MOV_UB, TMP1, 0, SLJIT_MEM1(TMP1), (sljit_sw)cc);
4352          OP2(SLJIT_SHL, TMP2, 0, SLJIT_IMM, 1, TMP2, 0);
4353          OP2(SLJIT_AND | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, TMP2, 0);
4354          add_jump(compiler, &found, JUMP(SLJIT_C_NOT_ZERO));
4355          add_jump(compiler, backtracks, JUMP(SLJIT_JUMP));
4356    
4357          JUMPHERE(jump);
4358          }
4359        else
4360          add_jump(compiler, &found, CMP(SLJIT_C_LESS_EQUAL, TMP3, 0, SLJIT_IMM, 0xff));
4361    
4362    if (!check_class_ranges(common, (const pcre_uint8 *)cc, TRUE, list))      OP1(SLJIT_MOV, TMP1, 0, TMP3, 0);
4363        cc += 32 / sizeof(pcre_uchar);
4364        }
4365      else
4366        add_jump(compiler, (cc[-1] & XCL_NOT) == 0 ? backtracks : &found, CMP(SLJIT_C_LESS_EQUAL, TMP1, 0, SLJIT_IMM, 0xff));
4367      }
4368    else if ((cc[-1] & XCL_MAP) != 0)
4369      {
4370      OP1(SLJIT_MOV, TMP3, 0, TMP1, 0);
4371    #ifdef SUPPORT_UCP
4372      charsaved = TRUE;
4373    #endif
4374      if (!check_class_ranges(common, (const pcre_uint8 *)cc, FALSE, TRUE, list))
4375      {      {
4376    #ifdef COMPILE_PCRE8
4377        SLJIT_ASSERT(common->utf);
4378    #endif
4379        jump = CMP(SLJIT_C_GREATER, TMP1, 0, SLJIT_IMM, 255);
4380    
4381      OP2(SLJIT_AND, TMP2, 0, TMP1, 0, SLJIT_IMM, 0x7);      OP2(SLJIT_AND, TMP2, 0, TMP1, 0, SLJIT_IMM, 0x7);
4382      OP2(SLJIT_LSHR, TMP1, 0, TMP1, 0, SLJIT_IMM, 3);      OP2(SLJIT_LSHR, TMP1, 0, TMP1, 0, SLJIT_IMM, 3);
4383      OP1(SLJIT_MOV_UB, TMP1, 0, SLJIT_MEM1(TMP1), (sljit_sw)cc);      OP1(SLJIT_MOV_UB, TMP1, 0, SLJIT_MEM1(TMP1), (sljit_sw)cc);
4384      OP2(SLJIT_SHL, TMP2, 0, SLJIT_IMM, 1, TMP2, 0);      OP2(SLJIT_SHL, TMP2, 0, SLJIT_IMM, 1, TMP2, 0);
4385      OP2(SLJIT_AND | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, TMP2, 0);      OP2(SLJIT_AND | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, TMP2, 0);
4386      add_jump(compiler, list, JUMP(SLJIT_C_NOT_ZERO));      add_jump(compiler, list, JUMP(SLJIT_C_NOT_ZERO));
     }  
4387    
 #ifndef COMPILE_PCRE8  
   JUMPHERE(jump);  
 #elif defined SUPPORT_UTF  
   if (common->utf)  
4388      JUMPHERE(jump);      JUMPHERE(jump);
4389  #endif      }
4390    
4391    OP1(SLJIT_MOV, TMP1, 0, TMP3, 0);    OP1(SLJIT_MOV, TMP1, 0, TMP3, 0);
 #ifdef SUPPORT_UCP  
   charsaved = TRUE;  
 #endif  
4392    cc += 32 / sizeof(pcre_uchar);    cc += 32 / sizeof(pcre_uchar);
4393    }    }
4394    
# Line 4047  while (*cc != XCL_END) Line 4446  while (*cc != XCL_END)
4446        case PT_SPACE:        case PT_SPACE:
4447        case PT_PXSPACE:        case PT_PXSPACE:
4448        case PT_WORD:        case PT_WORD:
4449          case PT_PXGRAPH:
4450          case PT_PXPRINT:
4451          case PT_PXPUNCT:
4452        needstype = TRUE;        needstype = TRUE;
4453        needschar = TRUE;        needschar = TRUE;
4454        break;        break;
# Line 4234  while (*cc != XCL_END) Line 4636  while (*cc != XCL_END)
4636    
4637        case PT_SPACE:        case PT_SPACE:
4638        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);  
         }  
4639        SET_CHAR_OFFSET(9);        SET_CHAR_OFFSET(9);
4640        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);
4641        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);
4642        if (*cc == PT_SPACE)  
4643          JUMPHERE(jump);        OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x85 - 0x9);
4644          OP_FLAGS(SLJIT_OR | SLJIT_SET_E, TMP2, 0, TMP2, 0, SLJIT_C_EQUAL);
4645    
4646          OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x180e - 0x9);
4647          OP_FLAGS(SLJIT_OR | SLJIT_SET_E, TMP2, 0, TMP2, 0, SLJIT_C_EQUAL);
4648    
4649        SET_TYPE_OFFSET(ucp_Zl);        SET_TYPE_OFFSET(ucp_Zl);
4650        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 4335  while (*cc != XCL_END) Line 4736  while (*cc != XCL_END)
4736        OP_FLAGS(SLJIT_OR | SLJIT_SET_E, TMP2, 0, TMP2, 0, SLJIT_C_GREATER_EQUAL);        OP_FLAGS(SLJIT_OR | SLJIT_SET_E, TMP2, 0, TMP2, 0, SLJIT_C_GREATER_EQUAL);
4737        jump = JUMP(SLJIT_C_NOT_ZERO ^ invertcmp);        jump = JUMP(SLJIT_C_NOT_ZERO ^ invertcmp);
4738        break;        break;
4739    
4740          case PT_PXGRAPH:
4741          /* C and Z groups are the farthest two groups. */
4742          SET_TYPE_OFFSET(ucp_Ll);
4743          OP2(SLJIT_SUB | SLJIT_SET_U, SLJIT_UNUSED, 0, typereg, 0, SLJIT_IMM, ucp_So - ucp_Ll);
4744          OP_FLAGS(SLJIT_MOV, TMP2, 0, SLJIT_UNUSED, 0, SLJIT_C_GREATER);
4745    
4746          jump = CMP(SLJIT_C_NOT_EQUAL, typereg, 0, SLJIT_IMM, ucp_Cf - ucp_Ll);
4747    
4748          /* In case of ucp_Cf, we overwrite the result. */
4749          SET_CHAR_OFFSET(0x2066);
4750          OP2(SLJIT_SUB | SLJIT_SET_U, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x2069 - 0x2066);
4751          OP_FLAGS(SLJIT_MOV, TMP2, 0, SLJIT_UNUSED, 0, SLJIT_C_LESS_EQUAL);
4752    
4753          OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x061c - 0x2066);
4754          OP_FLAGS(SLJIT_OR, TMP2, 0, TMP2, 0, SLJIT_C_EQUAL);
4755    
4756          OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x180e - 0x2066);
4757          OP_FLAGS(SLJIT_OR, TMP2, 0, TMP2, 0, SLJIT_C_EQUAL);
4758    
4759          JUMPHERE(jump);
4760          jump = CMP(SLJIT_C_ZERO ^ invertcmp, TMP2, 0, SLJIT_IMM, 0);
4761          break;
4762    
4763          case PT_PXPRINT:
4764          /* C and Z groups are the farthest two groups. */
4765          SET_TYPE_OFFSET(ucp_Ll);
4766          OP2(SLJIT_SUB | SLJIT_SET_U, SLJIT_UNUSED, 0, typereg, 0, SLJIT_IMM, ucp_So - ucp_Ll);
4767          OP_FLAGS(SLJIT_MOV, TMP2, 0, SLJIT_UNUSED, 0, SLJIT_C_GREATER);
4768    
4769          OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, typereg, 0, SLJIT_IMM, ucp_Zs - ucp_Ll);
4770          OP_FLAGS(SLJIT_AND, TMP2, 0, TMP2, 0, SLJIT_C_NOT_EQUAL);
4771    
4772          jump = CMP(SLJIT_C_NOT_EQUAL, typereg, 0, SLJIT_IMM, ucp_Cf - ucp_Ll);
4773    
4774          /* In case of ucp_Cf, we overwrite the result. */
4775          SET_CHAR_OFFSET(0x2066);
4776          OP2(SLJIT_SUB | SLJIT_SET_U, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x2069 - 0x2066);
4777          OP_FLAGS(SLJIT_MOV, TMP2, 0, SLJIT_UNUSED, 0, SLJIT_C_LESS_EQUAL);
4778    
4779          OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x061c - 0x2066);
4780          OP_FLAGS(SLJIT_OR, TMP2, 0, TMP2, 0, SLJIT_C_EQUAL);
4781    
4782          JUMPHERE(jump);
4783          jump = CMP(SLJIT_C_ZERO ^ invertcmp, TMP2, 0, SLJIT_IMM, 0);
4784          break;
4785    
4786          case PT_PXPUNCT:
4787          SET_TYPE_OFFSET(ucp_Sc);
4788          OP2(SLJIT_SUB | SLJIT_SET_U, SLJIT_UNUSED, 0, typereg, 0, SLJIT_IMM, ucp_So - ucp_Sc);
4789          OP_FLAGS(SLJIT_MOV, TMP2, 0, SLJIT_UNUSED, 0, SLJIT_C_LESS_EQUAL);
4790    
4791          SET_CHAR_OFFSET(0);
4792          OP2(SLJIT_SUB | SLJIT_SET_U, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0xff);
4793          OP_FLAGS(SLJIT_AND, TMP2, 0, TMP2, 0, SLJIT_C_LESS_EQUAL);
4794    
4795          SET_TYPE_OFFSET(ucp_Pc);
4796          OP2(SLJIT_SUB | SLJIT_SET_U, SLJIT_UNUSED, 0, typereg, 0, SLJIT_IMM, ucp_Ps - ucp_Pc);
4797          OP_FLAGS(SLJIT_OR | SLJIT_SET_E, TMP2, 0, TMP2, 0, SLJIT_C_LESS_EQUAL);
4798          jump = JUMP(SLJIT_C_NOT_ZERO ^ invertcmp);
4799          break;
4800        }        }
4801      cc += 2;      cc += 2;
4802      }      }
# Line 4391  switch(type) Line 4853  switch(type)
4853    case OP_NOT_DIGIT:    case OP_NOT_DIGIT:
4854    case OP_DIGIT:    case OP_DIGIT:
4855    /* Digits are usually 0-9, so it is worth to optimize them. */    /* Digits are usually 0-9, so it is worth to optimize them. */
   if (common->digits[0] == -2)  
     get_ctype_ranges(common, ctype_digit, common->digits);  
4856    detect_partial_match(common, backtracks);    detect_partial_match(common, backtracks);
4857    /* Flip the starting bit in the negative case. */    /* Flip the starting bit in the negative case. */
4858    if (type == OP_NOT_DIGIT)    read_char8_type(common);
4859      common->digits[1] ^= 1;    OP2(SLJIT_AND | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, ctype_digit);
4860    if (!check_ranges(common, common->digits, backtracks, TRUE))    add_jump(compiler, backtracks, JUMP(type == OP_DIGIT ? SLJIT_C_ZERO : SLJIT_C_NOT_ZERO));
     {  
     read_char8_type(common);  
     OP2(SLJIT_AND | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, ctype_digit);  
     add_jump(compiler, backtracks, JUMP(type == OP_DIGIT ? SLJIT_C_ZERO : SLJIT_C_NOT_ZERO));  
     }  
   if (type == OP_NOT_DIGIT)  
     common->digits[1] ^= 1;  
4861    return cc;    return cc;
4862    
4863    case OP_NOT_WHITESPACE:    case OP_NOT_WHITESPACE:
# Line 4481  switch(type) Line 4934  switch(type)
4934  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
4935    case OP_NOTPROP:    case OP_NOTPROP:
4936    case OP_PROP:    case OP_PROP:
4937    propdata[0] = 0;    propdata[0] = XCL_HASPROP;
4938    propdata[1] = type == OP_NOTPROP ? XCL_NOTPROP : XCL_PROP;    propdata[1] = type == OP_NOTPROP ? XCL_NOTPROP : XCL_PROP;
4939    propdata[2] = cc[0];    propdata[2] = cc[0];
4940    propdata[3] = cc[1];    propdata[3] = cc[1];
# Line 4839  switch(type) Line 5292  switch(type)
5292    case OP_CLASS:    case OP_CLASS:
5293    case OP_NCLASS:    case OP_NCLASS:
5294    detect_partial_match(common, backtracks);    detect_partial_match(common, backtracks);
5295    read_char(common);    read_char8(common);
5296    if (check_class_ranges(common, (const pcre_uint8 *)cc, type == OP_NCLASS, backtracks))    if (check_class_ranges(common, (const pcre_uint8 *)cc, type == OP_NCLASS, FALSE, backtracks))
5297      return cc + 32 / sizeof(pcre_uchar);      return cc + 32 / sizeof(pcre_uchar);
5298    
5299  #if defined SUPPORT_UTF || !defined COMPILE_PCRE8  #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
# Line 4973  if (context.length > 0) Line 5426  if (context.length > 0)
5426  return compile_char1_matchingpath(common, *cc, cc + 1, backtracks);  return compile_char1_matchingpath(common, *cc, cc + 1, backtracks);
5427  }  }
5428    
 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));  
 }  
   
5429  /* Forward definitions. */  /* Forward definitions. */
5430  static void compile_matchingpath(compiler_common *, pcre_uchar *, pcre_uchar *, backtrack_common *);  static void compile_matchingpath(compiler_common *, pcre_uchar *, pcre_uchar *, backtrack_common *);
5431  static void compile_backtrackingpath(compiler_common *, struct backtrack_common *);  static void compile_backtrackingpath(compiler_common *, struct backtrack_common *);
# Line 5027  static void compile_backtrackingpath(com Line 5458  static void compile_backtrackingpath(com
5458    
5459  #define BACKTRACK_AS(type) ((type *)backtrack)  #define BACKTRACK_AS(type) ((type *)backtrack)
5460    
5461  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)
5462    {
5463    /* The OVECTOR offset goes to TMP2. */
5464    DEFINE_COMPILER;
5465    int count = GET2(cc, 1 + IMM2_SIZE);
5466    pcre_uchar *slot = common->name_table + GET2(cc, 1) * common->name_entry_size;
5467    unsigned int offset;
5468    jump_list *found = NULL;
5469    
5470    SLJIT_ASSERT(*cc == OP_DNREF || *cc == OP_DNREFI);
5471    
5472    OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(1));
5473    
5474    count--;
5475    while (count-- > 0)
5476      {
5477      offset = GET2(slot, 0) << 1;
5478      GET_LOCAL_BASE(TMP2, 0, OVECTOR(offset));
5479      add_jump(compiler, &found, CMP(SLJIT_C_NOT_EQUAL, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(offset), TMP1, 0));
5480      slot += common->name_entry_size;
5481      }
5482    
5483    offset = GET2(slot, 0) << 1;
5484    GET_LOCAL_BASE(TMP2, 0, OVECTOR(offset));
5485    if (backtracks != NULL && !common->jscript_compat)
5486      add_jump(compiler, backtracks, CMP(SLJIT_C_EQUAL, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(offset), TMP1, 0));
5487    
5488    set_jumps(found, LABEL());
5489    }
5490    
5491    static void compile_ref_matchingpath(compiler_common *common, pcre_uchar *cc, jump_list **backtracks, BOOL withchecks, BOOL emptyfail)
5492  {  {
5493  DEFINE_COMPILER;  DEFINE_COMPILER;
5494  int offset = GET2(cc, 1) << 1;  BOOL ref = (*cc == OP_REF || *cc == OP_REFI);
5495    int offset = 0;
5496  struct sljit_jump *jump = NULL;  struct sljit_jump *jump = NULL;
5497  struct sljit_jump *partial;  struct sljit_jump *partial;
5498  struct sljit_jump *nopartial;  struct sljit_jump *nopartial;
5499    
5500  OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(offset));  if (ref)
5501  /* OVECTOR(1) contains the "string begin - 1" constant. */    {
5502  if (withchecks && !common->jscript_compat)    offset = GET2(cc, 1) << 1;
5503    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));
5504      /* OVECTOR(1) contains the "string begin - 1" constant. */
5505      if (withchecks && !common->jscript_compat)
5506        add_jump(compiler, backtracks, CMP(SLJIT_C_EQUAL, TMP1, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(1)));
5507      }
5508    else
5509      OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(TMP2), 0);
5510    
5511  #if defined SUPPORT_UTF && defined SUPPORT_UCP  #if defined SUPPORT_UTF && defined SUPPORT_UCP
5512  if (common->utf && *cc == OP_REFI)  if (common->utf && *cc == OP_REFI)
5513    {    {
5514    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);
5515    OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(offset + 1));    if (ref)
5516        OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(offset + 1));
5517      else
5518        OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(TMP2), sizeof(sljit_sw));
5519    
5520    if (withchecks)    if (withchecks)
5521      jump = CMP(SLJIT_C_EQUAL, TMP1, 0, TMP2, 0);      jump = CMP(SLJIT_C_EQUAL, TMP1, 0, TMP2, 0);
5522    
# Line 5069  if (common->utf && *cc == OP_REFI) Line 5541  if (common->utf && *cc == OP_REFI)
5541  else  else
5542  #endif /* SUPPORT_UTF && SUPPORT_UCP */  #endif /* SUPPORT_UTF && SUPPORT_UCP */
5543    {    {
5544    OP2(SLJIT_SUB | SLJIT_SET_E, TMP2, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(offset + 1), TMP1, 0);    if (ref)
5545        OP2(SLJIT_SUB | SLJIT_SET_E, TMP2, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(offset + 1), TMP1, 0);
5546      else
5547        OP2(SLJIT_SUB | SLJIT_SET_E, TMP2, 0, SLJIT_MEM1(TMP2), sizeof(sljit_sw), TMP1, 0);
5548    
5549    if (withchecks)    if (withchecks)
5550      jump = JUMP(SLJIT_C_ZERO);      jump = JUMP(SLJIT_C_ZERO);
5551    
# Line 5106  if (jump != NULL) Line 5582  if (jump != NULL)
5582    else    else
5583      JUMPHERE(jump);      JUMPHERE(jump);
5584    }    }
 return cc + 1 + IMM2_SIZE;  
5585  }  }
5586    
5587  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)
5588  {  {
5589  DEFINE_COMPILER;  DEFINE_COMPILER;
5590    BOOL ref = (*cc == OP_REF || *cc == OP_REFI);
5591  backtrack_common *backtrack;  backtrack_common *backtrack;
5592  pcre_uchar type;  pcre_uchar type;
5593    int offset = 0;
5594  struct sljit_label *label;  struct sljit_label *label;
5595  struct sljit_jump *zerolength;  struct sljit_jump *zerolength;
5596  struct sljit_jump *jump = NULL;  struct sljit_jump *jump = NULL;
# Line 5123  BOOL minimize; Line 5600  BOOL minimize;
5600    
5601  PUSH_BACKTRACK(sizeof(iterator_backtrack), cc, NULL);  PUSH_BACKTRACK(sizeof(iterator_backtrack), cc, NULL);
5602    
5603    if (ref)
5604      offset = GET2(cc, 1) << 1;
5605    else
5606      cc += IMM2_SIZE;
5607  type = cc[1 + IMM2_SIZE];  type = cc[1 + IMM2_SIZE];
5608    
5609    SLJIT_COMPILE_ASSERT((OP_CRSTAR & 0x1) == 0, crstar_opcode_must_be_even);
5610  minimize = (type & 0x1) != 0;  minimize = (type & 0x1) != 0;
5611  switch(type)  switch(type)
5612    {    {
# Line 5161  if (!minimize) Line 5644  if (!minimize)
5644    if (min == 0)    if (min == 0)
5645      {      {
5646      allocate_stack(common, 2);      allocate_stack(common, 2);
5647        if (ref)
5648          OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(offset));
5649      OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), STR_PTR, 0);      OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), STR_PTR, 0);
5650      OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(1), SLJIT_IMM, 0);      OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(1), SLJIT_IMM, 0);
5651      /* Temporary release of STR_PTR. */      /* Temporary release of STR_PTR. */
5652      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));
5653      zerolength = compile_ref_checks(common, ccbegin, NULL);      /* Handles both invalid and empty cases. Since the minimum repeat,
5654        is zero the invalid case is basically the same as an empty case. */
5655        if (ref)
5656          zerolength = CMP(SLJIT_C_EQUAL, TMP1, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(offset + 1));
5657        else
5658          {
5659          compile_dnref_search(common, ccbegin, NULL);
5660          OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(TMP2), 0);
5661          OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), POSSESSIVE1, TMP2, 0);
5662          zerolength = CMP(SLJIT_C_EQUAL, TMP1, 0, SLJIT_MEM1(TMP2), sizeof(sljit_sw));
5663          }
5664      /* Restore if not zero length. */      /* Restore if not zero length. */
5665      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));
5666      }      }
5667    else    else
5668      {      {
5669      allocate_stack(common, 1);      allocate_stack(common, 1);
5670        if (ref)
5671          OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(offset));
5672      OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), SLJIT_IMM, 0);      OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), SLJIT_IMM, 0);
5673      zerolength = compile_ref_checks(common, ccbegin, &backtrack->topbacktracks);      if (ref)
5674          {
5675          add_jump(compiler, &backtrack->topbacktracks, CMP(SLJIT_C_EQUAL, TMP1, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(1)));
5676          zerolength = CMP(SLJIT_C_EQUAL, TMP1, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(offset + 1));
5677          }
5678        else
5679          {
5680          compile_dnref_search(common, ccbegin, &backtrack->topbacktracks);
5681          OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(TMP2), 0);
5682          OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), POSSESSIVE1, TMP2, 0);
5683          zerolength = CMP(SLJIT_C_EQUAL, TMP1, 0, SLJIT_MEM1(TMP2), sizeof(sljit_sw));
5684          }
5685      }      }
5686    
5687    if (min > 1 || max > 1)    if (min > 1 || max > 1)
5688      OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), POSSESSIVE0, SLJIT_IMM, 0);      OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), POSSESSIVE0, SLJIT_IMM, 0);
5689    
5690    label = LABEL();    label = LABEL();
5691      if (!ref)
5692        OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), POSSESSIVE1);
5693    compile_ref_matchingpath(common, ccbegin, &backtrack->topbacktracks, FALSE, FALSE);    compile_ref_matchingpath(common, ccbegin, &backtrack->topbacktracks, FALSE, FALSE);
5694    
5695    if (min > 1 || max > 1)    if (min > 1 || max > 1)
# Line 5210  if (!minimize) Line 5720  if (!minimize)
5720    JUMPHERE(zerolength);    JUMPHERE(zerolength);
5721    BACKTRACK_AS(iterator_backtrack)->matchingpath = LABEL();    BACKTRACK_AS(iterator_backtrack)->matchingpath = LABEL();
5722    
5723    decrease_call_count(common);    count_match(common);
5724    return cc;    return cc;
5725    }    }
5726    
5727  allocate_stack(common, 2);  allocate_stack(common, ref ? 2 : 3);
5728    if (ref)
5729      OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(offset));
5730  OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), SLJIT_IMM, 0);  OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), SLJIT_IMM, 0);
5731  if (type != OP_CRMINSTAR)  if (type != OP_CRMINSTAR)
5732    OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(1), SLJIT_IMM, 0);    OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(1), SLJIT_IMM, 0);
5733    
5734  if (min == 0)  if (min == 0)
5735    {    {
5736    zerolength = compile_ref_checks(common, ccbegin, NULL);    /* Handles both invalid and empty cases. Since the minimum repeat,
5737      is zero the invalid case is basically the same as an empty case. */
5738      if (ref)
5739        zerolength = CMP(SLJIT_C_EQUAL, TMP1, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(offset + 1));
5740      else
5741        {
5742        compile_dnref_search(common, ccbegin, NULL);
5743        OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(TMP2), 0);
5744        OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(2), TMP2, 0);
5745        zerolength = CMP(SLJIT_C_EQUAL, TMP1, 0, SLJIT_MEM1(TMP2), sizeof(sljit_sw));
5746        }
5747      /* Length is non-zero, we can match real repeats. */
5748    OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), STR_PTR, 0);    OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), STR_PTR, 0);
5749    jump = JUMP(SLJIT_JUMP);    jump = JUMP(SLJIT_JUMP);
5750    }    }
5751  else  else
5752    zerolength = compile_ref_checks(common, ccbegin, &backtrack->topbacktracks);    {
5753      if (ref)
5754        {
5755        add_jump(compiler, &backtrack->topbacktracks, CMP(SLJIT_C_EQUAL, TMP1, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(1)));
5756        zerolength = CMP(SLJIT_C_EQUAL, TMP1, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(offset + 1));
5757        }
5758      else
5759        {
5760        compile_dnref_search(common, ccbegin, &backtrack->topbacktracks);
5761        OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(TMP2), 0);
5762        OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(2), TMP2, 0);
5763        zerolength = CMP(SLJIT_C_EQUAL, TMP1, 0, SLJIT_MEM1(TMP2), sizeof(sljit_sw));
5764        }
5765      }
5766    
5767  BACKTRACK_AS(iterator_backtrack)->matchingpath = LABEL();  BACKTRACK_AS(iterator_backtrack)->matchingpath = LABEL();
5768  if (max > 0)  if (max > 0)
5769    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));
5770    
5771    if (!ref)
5772      OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(STACK_TOP), STACK(2));
5773  compile_ref_matchingpath(common, ccbegin, &backtrack->topbacktracks, TRUE, TRUE);  compile_ref_matchingpath(common, ccbegin, &backtrack->topbacktracks, TRUE, TRUE);
5774  OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), STR_PTR, 0);  OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), STR_PTR, 0);
5775    
# Line 5249  if (jump != NULL) Line 5787  if (jump != NULL)
5787    JUMPHERE(jump);    JUMPHERE(jump);
5788  JUMPHERE(zerolength);  JUMPHERE(zerolength);
5789    
5790  decrease_call_count(common);  count_match(common);
5791  return cc;  return cc;
5792  }  }
5793    
# Line 5259  DEFINE_COMPILER; Line 5797  DEFINE_COMPILER;
5797  backtrack_common *backtrack;  backtrack_common *backtrack;
5798  recurse_entry *entry = common->entries;  recurse_entry *entry = common->entries;
5799  recurse_entry *prev = NULL;  recurse_entry *prev = NULL;
5800  int start = GET(cc, 1);  sljit_sw start = GET(cc, 1);
5801  pcre_uchar *start_cc;  pcre_uchar *start_cc;
5802  BOOL needs_control_head;  BOOL needs_control_head;
5803    
5804  PUSH_BACKTRACK(sizeof(recurse_backtrack), cc, NULL);  PUSH_BACKTRACK(sizeof(recurse_backtrack), cc, NULL);
5805    
5806  /* Inlining simple patterns. */  /* Inlining simple patterns. */
5807  if (get_framesize(common, common->start + start, TRUE, &needs_control_head) == no_stack)  if (get_framesize(common, common->start + start, NULL, TRUE, &needs_control_head) == no_stack)
5808    {    {
5809    start_cc = common->start + start;    start_cc = common->start + start;
5810    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 5436  jump_list *tmp = NULL; Line 5974  jump_list *tmp = NULL;
5974  jump_list **target = (conditional) ? &backtrack->condfailed : &backtrack->common.topbacktracks;  jump_list **target = (conditional) ? &backtrack->condfailed : &backtrack->common.topbacktracks;
5975  jump_list **found;  jump_list **found;
5976  /* Saving previous accept variables. */  /* Saving previous accept variables. */
5977    BOOL save_local_exit = common->local_exit;
5978    BOOL save_positive_assert = common->positive_assert;
5979    then_trap_backtrack *save_then_trap = common->then_trap;
5980  struct sljit_label *save_quit_label = common->quit_label;  struct sljit_label *save_quit_label = common->quit_label;
5981  struct sljit_label *save_accept_label = common->accept_label;  struct sljit_label *save_accept_label = common->accept_label;
5982  jump_list *save_quit = common->quit;  jump_list *save_quit = common->quit;
5983    jump_list *save_positive_assert_quit = common->positive_assert_quit;
5984  jump_list *save_accept = common->accept;  jump_list *save_accept = common->accept;
 BOOL save_local_exit = common->local_exit;  
5985  struct sljit_jump *jump;  struct sljit_jump *jump;
5986  struct sljit_jump *brajump = NULL;  struct sljit_jump *brajump = NULL;
5987    
5988    /* Assert captures then. */
5989    common->then_trap = NULL;
5990    
5991  if (*cc == OP_BRAZERO || *cc == OP_BRAMINZERO)  if (*cc == OP_BRAZERO || *cc == OP_BRAMINZERO)
5992    {    {
5993    SLJIT_ASSERT(!conditional);    SLJIT_ASSERT(!conditional);
# Line 5452  if (*cc == OP_BRAZERO || *cc == OP_BRAMI Line 5996  if (*cc == OP_BRAZERO || *cc == OP_BRAMI
5996    }    }
5997  private_data_ptr = PRIVATE_DATA(cc);  private_data_ptr = PRIVATE_DATA(cc);
5998  SLJIT_ASSERT(private_data_ptr != 0);  SLJIT_ASSERT(private_data_ptr != 0);
5999  framesize = get_framesize(common, cc, FALSE, &needs_control_head);  framesize = get_framesize(common, cc, NULL, FALSE, &needs_control_head);
6000  backtrack->framesize = framesize;  backtrack->framesize = framesize;
6001  backtrack->private_data_ptr = private_data_ptr;  backtrack->private_data_ptr = private_data_ptr;
6002  opcode = *cc;  opcode = *cc;
# Line 5502  else Line 6046  else
6046      }      }
6047    else    else
6048      OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(1), TMP1, 0);      OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(1), TMP1, 0);
6049    init_frame(common, ccbegin, framesize + extrasize - 1, extrasize, FALSE);    init_frame(common, ccbegin, NULL, framesize + extrasize - 1, extrasize, FALSE);
6050    }    }
6051    
6052  memset(&altbacktrack, 0, sizeof(backtrack_common));  memset(&altbacktrack, 0, sizeof(backtrack_common));
6053  common->local_exit = TRUE;  if (opcode == OP_ASSERT_NOT || opcode == OP_ASSERTBACK_NOT)
6054  common->quit_label = NULL;    {
6055  common->quit = NULL;    /* Negative assert is stronger than positive assert. */
6056      common->local_exit = TRUE;
6057      common->quit_label = NULL;
6058      common->quit = NULL;
6059      common->positive_assert = FALSE;
6060      }
6061    else
6062      common->positive_assert = TRUE;
6063    common->positive_assert_quit = NULL;
6064    
6065  while (1)  while (1)
6066    {    {
6067    common->accept_label = NULL;    common->accept_label = NULL;
# Line 5523  while (1) Line 6076  while (1)
6076    compile_matchingpath(common, ccbegin + 1 + LINK_SIZE, cc, &altbacktrack);    compile_matchingpath(common, ccbegin + 1 + LINK_SIZE, cc, &altbacktrack);
6077    if (SLJIT_UNLIKELY(sljit_get_compiler_error(compiler)))    if (SLJIT_UNLIKELY(sljit_get_compiler_error(compiler)))
6078      {      {
6079      common->local_exit = save_local_exit;      if (opcode == OP_ASSERT_NOT || opcode == OP_ASSERTBACK_NOT)
6080      common->quit_label = save_quit_label;        {
6081          common->local_exit = save_local_exit;
6082          common->quit_label = save_quit_label;
6083          common->quit = save_quit;
6084          }
6085        common->positive_assert = save_positive_assert;
6086        common->then_trap = save_then_trap;
6087      common->accept_label = save_accept_label;      common->accept_label = save_accept_label;
6088      common->quit = save_quit;      common->positive_assert_quit = save_positive_assert_quit;
6089      common->accept = save_accept;      common->accept = save_accept;
6090      return NULL;      return NULL;
6091      }      }
# Line 5591  while (1) Line 6150  while (1)
6150    compile_backtrackingpath(common, altbacktrack.top);    compile_backtrackingpath(common, altbacktrack.top);
6151    if (SLJIT_UNLIKELY(sljit_get_compiler_error(compiler)))    if (SLJIT_UNLIKELY(sljit_get_compiler_error(compiler)))
6152      {      {
6153      common->local_exit = save_local_exit;      if (opcode == OP_ASSERT_NOT || opcode == OP_ASSERTBACK_NOT)
6154      common->quit_label = save_quit_label;        {
6155          common->local_exit = save_local_exit;
6156          common->quit_label = save_quit_label;
6157          common->quit = save_quit;
6158          }
6159        common->positive_assert = save_positive_assert;
6160        common->then_trap = save_then_trap;
6161      common->accept_label = save_accept_label;      common->accept_label = save_accept_label;
6162      common->quit = save_quit;      common->positive_assert_quit = save_positive_assert_quit;
6163      common->accept = save_accept;      common->accept = save_accept;
6164      return NULL;      return NULL;
6165      }      }
# Line 5607  while (1) Line 6172  while (1)
6172    cc += GET(cc, 1);    cc += GET(cc, 1);
6173    }    }
6174    
6175    if (opcode == OP_ASSERT_NOT || opcode == OP_ASSERTBACK_NOT)
6176      {
6177      SLJIT_ASSERT(common->positive_assert_quit == NULL);
6178      /* Makes the check less complicated below. */
6179      common->positive_assert_quit = common->quit;
6180      }
6181    
6182  /* None of them matched. */  /* None of them matched. */
6183  if (common->quit != NULL)  if (common->positive_assert_quit != NULL)
6184    {    {
6185    jump = JUMP(SLJIT_JUMP);    jump = JUMP(SLJIT_JUMP);
6186    set_jumps(common->quit, LABEL());    set_jumps(common->positive_assert_quit, LABEL());
6187    SLJIT_ASSERT(framesize != no_stack);    SLJIT_ASSERT(framesize != no_stack);
6188    if (framesize < 0)    if (framesize < 0)
6189      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 5771  else Line 6343  else
6343      }      }
6344    }    }
6345    
6346  common->local_exit = save_local_exit;  if (opcode == OP_ASSERT_NOT || opcode == OP_ASSERTBACK_NOT)
6347  common->quit_label = save_quit_label;    {
6348      common->local_exit = save_local_exit;
6349      common->quit_label = save_quit_label;
6350      common->quit = save_quit;
6351      }
6352    common->positive_assert = save_positive_assert;
6353    common->then_trap = save_then_trap;
6354  common->accept_label = save_accept_label;  common->accept_label = save_accept_label;
6355  common->quit = save_quit;  common->positive_assert_quit = save_positive_assert_quit;
6356  common->accept = save_accept;  common->accept = save_accept;
6357  return cc + 1 + LINK_SIZE;  return cc + 1 + LINK_SIZE;
6358  }  }
6359    
 static sljit_sw SLJIT_CALL do_searchovector(sljit_uw refno, sljit_sw* locals, pcre_uchar *name_table)  
 {  
 int condition = FALSE;  
 pcre_uchar *slotA = name_table;  
 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];  
   
 for (i = 0; i < name_count; i++)  
   {  
   if (GET2(slotA, 0) == refno) break;  
   slotA += name_entry_size;  
   }  
   
 if (i < name_count)  
   {  
   /* 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. */  
   
   slotB = slotA;  
   while (slotB > name_table)  
     {  
     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;  
     }  
   
   /* 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 = locals[GET2(slotB, 0) << 1] != no_capture;  
         if (condition) break;  
         }  
       else break;  
       }  
     }  
   }  
 return condition;  
 }  
   
 static sljit_sw SLJIT_CALL do_searchgroups(sljit_uw recno, sljit_uw* locals, pcre_uchar *name_table)  
 {  
 int condition = FALSE;  
 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;  
   
 for (i = 0; i < name_count; i++)  
   {  
   if (GET2(slotA, 0) == recno) break;  
   slotA += name_entry_size;  
   }  
   
 if (i < name_count)  
   {  
   /* 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. */  
   
   slotB = slotA;  
   while (slotB > name_table)  
     {  
     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;  
     }  
   
   /* 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;  
       }  
     }  
   }  
 return condition;  
 }  
   
6360  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)  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)
6361  {  {
6362  DEFINE_COMPILER;  DEFINE_COMPILER;
# Line 6021  backtrack_common *backtrack; Line 6489  backtrack_common *backtrack;
6489  pcre_uchar opcode;  pcre_uchar opcode;
6490  int private_data_ptr = 0;  int private_data_ptr = 0;
6491  int offset = 0;  int offset = 0;
6492  int stacksize;  int i, stacksize;
6493    int repeat_ptr = 0, repeat_length = 0;
6494    int repeat_type = 0, repeat_count = 0;
6495  pcre_uchar *ccbegin;  pcre_uchar *ccbegin;
6496  pcre_uchar *matchingpath;  pcre_uchar *matchingpath;
6497    pcre_uchar *slot;
6498  pcre_uchar bra = OP_BRA;  pcre_uchar bra = OP_BRA;
6499  pcre_uchar ket;  pcre_uchar ket;
6500  assert_backtrack *assert;  assert_backtrack *assert;
# Line 6031  BOOL has_alternatives; Line 6502  BOOL has_alternatives;
6502  BOOL needs_control_head = FALSE;  BOOL needs_control_head = FALSE;
6503  struct sljit_jump *jump;  struct sljit_jump *jump;
6504  struct sljit_jump *skip;  struct sljit_jump *skip;
6505  struct sljit_label *rmaxlabel = NULL;  struct sljit_label *rmax_label = NULL;
6506  struct sljit_jump *braminzerojump = NULL;  struct sljit_jump *braminzero = NULL;
6507    
6508  PUSH_BACKTRACK(sizeof(bracket_backtrack), cc, NULL);  PUSH_BACKTRACK(sizeof(bracket_backtrack), cc, NULL);
6509    
# Line 6045  if (*cc == OP_BRAZERO || *cc == OP_BRAMI Line 6516  if (*cc == OP_BRAZERO || *cc == OP_BRAMI
6516    
6517  opcode = *cc;  opcode = *cc;
6518  ccbegin = cc;  ccbegin = cc;
6519  matchingpath = ccbegin + 1 + LINK_SIZE;  matchingpath = bracketend(cc) - 1 - LINK_SIZE;
6520    ket = *matchingpath;
6521    if (ket == OP_KET && PRIVATE_DATA(matchingpath) != 0)
6522      {
6523      repeat_ptr = PRIVATE_DATA(matchingpath);
6524      repeat_length = PRIVATE_DATA(matchingpath + 1);
6525      repeat_type = PRIVATE_DATA(matchingpath + 2);
6526      repeat_count = PRIVATE_DATA(matchingpath + 3);
6527      SLJIT_ASSERT(repeat_length != 0 && repeat_type != 0 && repeat_count != 0);
6528      if (repeat_type == OP_UPTO)
6529        ket = OP_KETRMAX;
6530      if (repeat_type == OP_MINUPTO)
6531        ket = OP_KETRMIN;
6532      }
6533    
6534  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)
6535    {    {
6536    /* Drop this bracket_backtrack. */    /* Drop this bracket_backtrack. */
6537    parent->top = backtrack->prev;    parent->top = backtrack->prev;
6538    return bracketend(cc);    return matchingpath + 1 + LINK_SIZE + repeat_length;
6539    }    }
6540    
6541  ket = *(bracketend(cc) - 1 - LINK_SIZE);  matchingpath = ccbegin + 1 + LINK_SIZE;
6542  SLJIT_ASSERT(ket == OP_KET || ket == OP_KETRMAX || ket == OP_KETRMIN);  SLJIT_ASSERT(ket == OP_KET || ket == OP_KETRMAX || ket == OP_KETRMIN);
6543  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)));
6544  cc += GET(cc, 1);  cc += GET(cc, 1);
6545    
6546  has_alternatives = *cc == OP_ALT;  has_alternatives = *cc == OP_ALT;
6547  if (SLJIT_UNLIKELY(opcode == OP_COND) || SLJIT_UNLIKELY(opcode == OP_SCOND))  if (SLJIT_UNLIKELY(opcode == OP_COND || opcode == OP_SCOND))
6548    {    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);  
     }  
   }  
6549    
6550  if (SLJIT_UNLIKELY(opcode == OP_COND) && (*cc == OP_KETRMAX || *cc == OP_KETRMIN))  if (SLJIT_UNLIKELY(opcode == OP_COND) && (*cc == OP_KETRMAX || *cc == OP_KETRMIN))
6551    opcode = OP_SCOND;    opcode = OP_SCOND;
# Line 6104  else if (opcode == OP_ONCE || opcode == Line 6576  else if (opcode == OP_ONCE || opcode ==
6576    SLJIT_ASSERT(private_data_ptr != 0);    SLJIT_ASSERT(private_data_ptr != 0);
6577    BACKTRACK_AS(bracket_backtrack)->private_data_ptr = private_data_ptr;    BACKTRACK_AS(bracket_backtrack)->private_data_ptr = private_data_ptr;
6578    if (opcode == OP_ONCE)    if (opcode == OP_ONCE)
6579      BACKTRACK_AS(bracket_backtrack)->u.framesize = get_framesize(common, ccbegin, FALSE, &needs_control_head);      BACKTRACK_AS(bracket_backtrack)->u.framesize = get_framesize(common, ccbegin, NULL, FALSE, &needs_control_head);
6580    }    }
6581    
6582  /* Instructions before the first alternative. */  /* Instructions before the first alternative. */
# Line 6134  if (bra == OP_BRAMINZERO) Line 6606  if (bra == OP_BRAMINZERO)
6606    if (ket != OP_KETRMIN)    if (ket != OP_KETRMIN)
6607      {      {
6608      free_stack(common, 1);      free_stack(common, 1);
6609      braminzerojump = CMP(SLJIT_C_EQUAL, STR_PTR, 0, SLJIT_IMM, 0);      braminzero = CMP(SLJIT_C_EQUAL, STR_PTR, 0, SLJIT_IMM, 0);
6610      }      }
6611    else    else
6612      {      {
# Line 6149  if (bra == OP_BRAMINZERO) Line 6621  if (bra == OP_BRAMINZERO)
6621        if (opcode != OP_ONCE || BACKTRACK_AS(bracket_backtrack)->u.framesize < 0)        if (opcode != OP_ONCE || BACKTRACK_AS(bracket_backtrack)->u.framesize < 0)
6622          {          {
6623          /* 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. */
6624          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);
6625          }          }
6626        else        else
6627          {          {
6628          /* Except when the whole stack frame must be saved. */          /* Except when the whole stack frame must be saved. */
6629          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);
6630          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));
6631          }          }
6632        JUMPHERE(skip);        JUMPHERE(skip);
6633        }        }
# Line 6168  if (bra == OP_BRAMINZERO) Line 6640  if (bra == OP_BRAMINZERO)
6640      }      }
6641    }    }
6642    
6643    if (repeat_type != 0)
6644      {
6645      OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), repeat_ptr, SLJIT_IMM, repeat_count);
6646      if (repeat_type == OP_EXACT)
6647        rmax_label = LABEL();
6648      }
6649    
6650  if (ket == OP_KETRMIN)  if (ket == OP_KETRMIN)
6651    BACKTRACK_AS(bracket_backtrack)->recursive_matchingpath = LABEL();    BACKTRACK_AS(bracket_backtrack)->recursive_matchingpath = LABEL();
6652    
6653  if (ket == OP_KETRMAX)  if (ket == OP_KETRMAX)
6654    {    {
6655    rmaxlabel = LABEL();    rmax_label = LABEL();
6656    if (has_alternatives && opcode != OP_ONCE && opcode < OP_SBRA)    if (has_alternatives && opcode != OP_ONCE && opcode < OP_SBRA && repeat_type == 0)
6657      BACKTRACK_AS(bracket_backtrack)->alternative_matchingpath = rmaxlabel;      BACKTRACK_AS(bracket_backtrack)->alternative_matchingpath = rmax_label;
6658    }    }
6659    
6660  /* Handling capturing brackets and alternatives. */  /* Handling capturing brackets and alternatives. */
# Line 6254  if (opcode == OP_ONCE) Line 6733  if (opcode == OP_ONCE)
6733        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);
6734        OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(stacksize), TMP1, 0);        OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(stacksize), TMP1, 0);
6735        }        }
6736      init_frame(common, ccbegin, BACKTRACK_AS(bracket_backtrack)->u.framesize + stacksize, stacksize + 1, FALSE);      init_frame(common, ccbegin, NULL, BACKTRACK_AS(bracket_backtrack)->u.framesize + stacksize, stacksize + 1, FALSE);
6737      }      }
6738    }    }
6739  else if (opcode == OP_CBRA || opcode == OP_SCBRA)  else if (opcode == OP_CBRA || opcode == OP_SCBRA)
# Line 6303  if (opcode == OP_COND || opcode == OP_SC Line 6782  if (opcode == OP_COND || opcode == OP_SC
6782        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)));
6783      matchingpath += 1 + IMM2_SIZE;      matchingpath += 1 + IMM2_SIZE;
6784      }      }
6785    else if (*matchingpath == OP_NCREF)    else if (*matchingpath == OP_DNCREF)
6786      {      {
6787      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));  
6788    
6789      JUMPHERE(jump);      i = GET2(matchingpath, 1 + IMM2_SIZE);
6790      matchingpath += 1 + IMM2_SIZE;      slot = common->name_table + GET2(matchingpath, 1) * common->name_entry_size;
6791        OP1(SLJIT_MOV, TMP3, 0, STR_PTR, 0);
6792        OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(1));
6793        OP2(SLJIT_SUB | SLJIT_SET_E, TMP2, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(GET2(slot, 0) << 1), TMP1, 0);
6794        slot += common->name_entry_size;
6795        i--;
6796        while (i-- > 0)
6797          {
6798          OP2(SLJIT_SUB, STR_PTR, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(GET2(slot, 0) << 1), TMP1, 0);
6799          OP2(SLJIT_OR | SLJIT_SET_E, TMP2, 0, TMP2, 0, STR_PTR, 0);
6800          slot += common->name_entry_size;
6801          }
6802        OP1(SLJIT_MOV, STR_PTR, 0, TMP3, 0);
6803        add_jump(compiler, &(BACKTRACK_AS(bracket_backtrack)->u.condfailed), JUMP(SLJIT_C_ZERO));
6804        matchingpath += 1 + 2 * IMM2_SIZE;
6805      }      }
6806    else if (*matchingpath == OP_RREF || *matchingpath == OP_NRREF)    else if (*matchingpath == OP_RREF || *matchingpath == OP_DNRREF)
6807      {      {
6808      /* Never has other case. */      /* Never has other case. */
6809      BACKTRACK_AS(bracket_backtrack)->u.condfailed = NULL;      BACKTRACK_AS(bracket_backtrack)->u.condfailed = NULL;
6810        SLJIT_ASSERT(!has_alternatives);
6811    
6812      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)  
6813        {        {
6814        SLJIT_ASSERT(!has_alternatives);        stacksize = GET2(matchingpath, 1);
6815          if (common->currententry == NULL)
6816            stacksize = 0;
6817          else if (stacksize == RREF_ANY)
6818            stacksize = 1;
6819          else if (common->currententry->start == 0)
6820            stacksize = stacksize == 0;
6821          else
6822            stacksize = stacksize == (int)GET2(common->start, common->currententry->start + 1 + LINK_SIZE);
6823    
6824        if (stacksize != 0)        if (stacksize != 0)
6825          matchingpath += 1 + IMM2_SIZE;          matchingpath += 1 + IMM2_SIZE;
6826          }
6827        else
6828          {
6829          if (common->currententry == NULL || common->currententry->start == 0)
6830            stacksize = 0;
6831        else        else
6832          {          {
6833            stacksize = GET2(matchingpath, 1 + IMM2_SIZE);
6834            slot = common->name_table + GET2(matchingpath, 1) * common->name_entry_size;
6835            i = (int)GET2(common->start, common->currententry->start + 1 + LINK_SIZE);
6836            while (stacksize > 0)
6837              {
6838              if ((int)GET2(slot, 0) == i)
6839                break;
6840              slot += common->name_entry_size;
6841              stacksize--;
6842              }
6843            }
6844    
6845          if (stacksize != 0)
6846            matchingpath += 1 + 2 * IMM2_SIZE;
6847          }
6848    
6849          /* The stacksize == 0 is a common "else" case. */
6850          if (stacksize == 0)
6851            {
6852          if (*cc == OP_ALT)          if (*cc == OP_ALT)
6853            {            {
6854            matchingpath = cc + 1 + LINK_SIZE;            matchingpath = cc + 1 + LINK_SIZE;
# Line 6352  if (opcode == OP_COND || opcode == OP_SC Line 6857  if (opcode == OP_COND || opcode == OP_SC
6857          else          else
6858            matchingpath = cc;            matchingpath = cc;
6859          }          }
       }  
     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;  
       }  
6860      }      }
6861    else    else
6862      {      {
# Line 6393  if (opcode == OP_ONCE) Line 6880  if (opcode == OP_ONCE)
6880    match_once_common(common, ket, BACKTRACK_AS(bracket_backtrack)->u.framesize, private_data_ptr, has_alternatives, needs_control_head);    match_once_common(common, ket, BACKTRACK_AS(bracket_backtrack)->u.framesize, private_data_ptr, has_alternatives, needs_control_head);
6881    
6882  stacksize = 0;  stacksize = 0;
6883    if (repeat_type == OP_MINUPTO)
6884      {
6885      /* We need to preserve the counter. TMP2 will be used below. */
6886      OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), repeat_ptr);
6887      stacksize++;
6888      }
6889  if (ket != OP_KET || bra != OP_BRA)  if (ket != OP_KET || bra != OP_BRA)
6890    stacksize++;    stacksize++;
6891  if (offset != 0)  if (offset != 0)
# Line 6409  if (stacksize > 0) Line 6902  if (stacksize > 0)
6902    allocate_stack(common, stacksize);    allocate_stack(common, stacksize);
6903    
6904  stacksize = 0;  stacksize = 0;
6905    if (repeat_type == OP_MINUPTO)
6906      {
6907      /* TMP2 was set above. */
6908      OP2(SLJIT_SUB, SLJIT_MEM1(STACK_TOP), STACK(stacksize), TMP2, 0, SLJIT_IMM, 1);
6909      stacksize++;
6910      }
6911    
6912  if (ket != OP_KET || bra != OP_BRA)  if (ket != OP_KET || bra != OP_BRA)
6913    {    {
6914    if (ket != OP_KET)    if (ket != OP_KET)
# Line 6438  if (offset != 0 && common->optimized_cbr Line 6938  if (offset != 0 && common->optimized_cbr
6938    
6939  if (ket == OP_KETRMAX)  if (ket == OP_KETRMAX)
6940    {    {
6941    if (opcode == OP_ONCE || opcode >= OP_SBRA)    if (repeat_type != 0)
6942        {
6943        if (has_alternatives)
6944          BACKTRACK_AS(bracket_backtrack)->alternative_matchingpath = LABEL();
6945        OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_MEM1(SLJIT_LOCALS_REG), repeat_ptr, SLJIT_MEM1(SLJIT_LOCALS_REG), repeat_ptr, SLJIT_IMM, 1);
6946        JUMPTO(SLJIT_C_NOT_ZERO, rmax_label);
6947        /* Drop STR_PTR for greedy plus quantifier. */
6948        if (opcode != OP_ONCE)
6949          free_stack(common, 1);
6950        }
6951      else if (opcode == OP_ONCE || opcode >= OP_SBRA)
6952      {      {
6953      if (has_alternatives)      if (has_alternatives)
6954        BACKTRACK_AS(bracket_backtrack)->alternative_matchingpath = LABEL();        BACKTRACK_AS(bracket_backtrack)->alternative_matchingpath = LABEL();
6955      /* Checking zero-length iteration. */      /* Checking zero-length iteration. */
6956      if (opcode != OP_ONCE)      if (opcode != OP_ONCE)
6957        {        {
6958        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);
6959        /* Drop STR_PTR for greedy plus quantifier. */        /* Drop STR_PTR for greedy plus quantifier. */
6960        if (bra != OP_BRAZERO)        if (bra != OP_BRAZERO)
6961          free_stack(common, 1);          free_stack(common, 1);
6962        }        }
6963      else      else
6964        /* TMP2 must contain the starting STR_PTR. */        /* TMP2 must contain the starting STR_PTR. */
6965        CMPTO(SLJIT_C_NOT_EQUAL, TMP2, 0, STR_PTR, 0, rmaxlabel);        CMPTO(SLJIT_C_NOT_EQUAL, TMP2, 0, STR_PTR, 0, rmax_label);
6966      }      }
6967    else    else
6968      JUMPTO(SLJIT_JUMP, rmaxlabel);      JUMPTO(SLJIT_JUMP, rmax_label);
6969    BACKTRACK_AS(bracket_backtrack)->recursive_matchingpath = LABEL();    BACKTRACK_AS(bracket_backtrack)->recursive_matchingpath = LABEL();
6970    }    }
6971    
6972    if (repeat_type == OP_EXACT)
6973      {
6974      OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_MEM1(SLJIT_LOCALS_REG), repeat_ptr, SLJIT_MEM1(SLJIT_LOCALS_REG), repeat_ptr, SLJIT_IMM, 1);
6975      JUMPTO(SLJIT_C_NOT_ZERO, rmax_label);
6976      }
6977    else if (repeat_type == OP_UPTO)
6978      {
6979      /* We need to preserve the counter. */
6980      OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), repeat_ptr);
6981      allocate_stack(common, 1);
6982      OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), TMP2, 0);
6983      }
6984    
6985  if (bra == OP_BRAZERO)  if (bra == OP_BRAZERO)
6986    BACKTRACK_AS(bracket_backtrack)->zero_matchingpath = LABEL();    BACKTRACK_AS(bracket_backtrack)->zero_matchingpath = LABEL();
6987    
# Line 6466  if (bra == OP_BRAMINZERO) Line 6989  if (bra == OP_BRAMINZERO)
6989    {    {
6990    /* This is a backtrack path! (From the viewpoint of OP_BRAMINZERO) */    /* This is a backtrack path! (From the viewpoint of OP_BRAMINZERO) */
6991    JUMPTO(SLJIT_JUMP, ((braminzero_backtrack *)parent)->matchingpath);    JUMPTO(SLJIT_JUMP, ((braminzero_backtrack *)parent)->matchingpath);
6992    if (braminzerojump != NULL)    if (braminzero != NULL)
6993      {      {
6994      JUMPHERE(braminzerojump);      JUMPHERE(braminzero);
6995      /* We need to release the end pointer to perform the      /* We need to release the end pointer to perform the
6996      backtrack for the zero-length iteration. When      backtrack for the zero-length iteration. When
6997      framesize is < 0, OP_ONCE will do the release itself. */      framesize is < 0, OP_ONCE will do the release itself. */
# Line 6484  if (bra == OP_BRAMINZERO) Line 7007  if (bra == OP_BRAMINZERO)
7007    }    }
7008    
7009  if ((ket != OP_KET && bra != OP_BRAMINZERO) || bra == OP_BRAZERO)  if ((ket != OP_KET && bra != OP_BRAMINZERO) || bra == OP_BRAZERO)
7010    decrease_call_count(common);    count_match(common);
7011    
7012  /* Skip the other alternatives. */  /* Skip the other alternatives. */
7013  while (*cc == OP_ALT)  while (*cc == OP_ALT)
# Line 6494  cc += 1 + LINK_SIZE; Line 7017  cc += 1 + LINK_SIZE;
7017  /* Temporarily encoding the needs_control_head in framesize. */  /* Temporarily encoding the needs_control_head in framesize. */
7018  if (opcode == OP_ONCE)  if (opcode == OP_ONCE)
7019    BACKTRACK_AS(bracket_backtrack)->u.framesize = (BACKTRACK_AS(bracket_backtrack)->u.framesize << 1) | (needs_control_head ? 1 : 0);    BACKTRACK_AS(bracket_backtrack)->u.framesize = (BACKTRACK_AS(bracket_backtrack)->u.framesize << 1) | (needs_control_head ? 1 : 0);
7020  return cc;  return cc + repeat_length;
7021  }  }
7022    
7023  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 6548  switch(opcode) Line 7071  switch(opcode)
7071    break;    break;
7072    }    }
7073    
7074  framesize = get_framesize(common, cc, FALSE, &needs_control_head);  framesize = get_framesize(common, cc, NULL, FALSE, &needs_control_head);
7075  BACKTRACK_AS(bracketpos_backtrack)->framesize = framesize;  BACKTRACK_AS(bracketpos_backtrack)->framesize = framesize;
7076  if (framesize < 0)  if (framesize < 0)
7077    {    {
# Line 6641  else Line 7164  else
7164      stack++;      stack++;
7165      }      }
7166    OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(stack), TMP1, 0);    OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(stack), TMP1, 0);
7167    init_frame(common, cc, stacksize - 1, stacksize - framesize, FALSE);    init_frame(common, cc, NULL, stacksize - 1, stacksize - framesize, FALSE);
7168    stack -= 1 + (offset == 0);    stack -= 1 + (offset == 0);
7169    }    }
7170    
# Line 6771  if (!zero) Line 7294  if (!zero)
7294    
7295  /* None of them matched. */  /* None of them matched. */
7296  set_jumps(emptymatch, LABEL());  set_jumps(emptymatch, LABEL());
7297  decrease_call_count(common);  count_match(common);
7298  return cc + 1 + LINK_SIZE;  return cc + 1 + LINK_SIZE;
7299  }  }
7300    
7301  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)
7302  {  {
7303  int class_len;  int class_len;
7304    
# Line 6811  else if (*opcode >= OP_TYPESTAR && *opco Line 7334  else if (*opcode >= OP_TYPESTAR && *opco
7334    }    }
7335  else  else
7336    {    {
7337    SLJIT_ASSERT(*opcode >= OP_CLASS || *opcode <= OP_XCLASS);    SLJIT_ASSERT(*opcode == OP_CLASS || *opcode == OP_NCLASS || *opcode == OP_XCLASS);
7338    *type = *opcode;    *type = *opcode;
7339    cc++;    cc++;
7340    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 6822  else Line 7345  else
7345      if (end != NULL)      if (end != NULL)
7346        *end = cc + class_len;        *end = cc + class_len;
7347      }      }
7348      else if (*opcode >= OP_CRPOSSTAR && *opcode <= OP_CRPOSQUERY)
7349        {
7350        *opcode -= OP_CRPOSSTAR - OP_POSSTAR;
7351        if (end != NULL)
7352          *end = cc + class_len;
7353        }
7354    else    else
7355      {      {
7356      SLJIT_ASSERT(*opcode == OP_CRRANGE || *opcode == OP_CRMINRANGE);      SLJIT_ASSERT(*opcode == OP_CRRANGE || *opcode == OP_CRMINRANGE || *opcode == OP_CRPOSRANGE);
7357      *arg1 = GET2(cc, (class_len + IMM2_SIZE));      *max = GET2(cc, (class_len + IMM2_SIZE));
7358      *arg2 = GET2(cc, class_len);      *min = GET2(cc, class_len);
7359    
7360      if (*arg2 == 0)      if (*min == 0)
7361        {        {
7362        SLJIT_ASSERT(*arg1 != 0);        SLJIT_ASSERT(*max != 0);
7363        *opcode = (*opcode == OP_CRRANGE) ? OP_UPTO : OP_MINUPTO;        *opcode = (*opcode == OP_CRRANGE) ? OP_UPTO : (*opcode == OP_CRMINRANGE ? OP_MINUPTO : OP_POSUPTO);
7364        }        }
7365      if (*arg1 == *arg2)      if (*max == *min)
7366        *opcode = OP_EXACT;        *opcode = OP_EXACT;
7367    
7368      if (end != NULL)      if (end != NULL)
# Line 6844  else Line 7373  else
7373    
7374  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)
7375    {    {
7376    *arg1 = GET2(cc, 0);    *max = GET2(cc, 0);
7377    cc += IMM2_SIZE;    cc += IMM2_SIZE;
7378    }    }
7379    
# Line 6873  DEFINE_COMPILER; Line 7402  DEFINE_COMPILER;
7402  backtrack_common *backtrack;  backtrack_common *backtrack;
7403  pcre_uchar opcode;  pcre_uchar opcode;
7404  pcre_uchar type;  pcre_uchar type;
7405  int arg1 = -1, arg2 = -1;  int max = -1, min = -1;
7406  pcre_uchar* end;  pcre_uchar* end;
7407  jump_list *nomatch = NULL;  jump_list *nomatch = NULL;
7408  struct sljit_jump *jump = NULL;  struct sljit_jump *jump = NULL;
# Line 6886  int tmp_base, tmp_offset; Line 7415  int tmp_base, tmp_offset;
7415    
7416  PUSH_BACKTRACK(sizeof(iterator_backtrack), cc, NULL);  PUSH_BACKTRACK(sizeof(iterator_backtrack), cc, NULL);
7417    
7418  cc = get_iterator_parameters(common, cc, &opcode, &type, &arg1, &arg2, &end);  cc = get_iterator_parameters(common, cc, &opcode, &type, &max, &min, &end);
7419    
7420  switch(type)  switch(type)
7421    {    {
# Line 6957  switch(opcode) Line 7486  switch(opcode)
7486        {        {
7487        OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), POSSESSIVE0);        OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), POSSESSIVE0);
7488        OP2(SLJIT_ADD, TMP1, 0, TMP1, 0, SLJIT_IMM, 1);        OP2(SLJIT_ADD, TMP1, 0, TMP1, 0, SLJIT_IMM, 1);
7489        if (opcode == OP_CRRANGE && arg2 > 0)        if (opcode == OP_CRRANGE && min > 0)
7490          CMPTO(SLJIT_C_LESS, TMP1, 0, SLJIT_IMM, arg2, label);          CMPTO(SLJIT_C_LESS, TMP1, 0, SLJIT_IMM, min, label);
7491        if (opcode == OP_UPTO || (opcode == OP_CRRANGE && arg1 > 0))        if (opcode == OP_UPTO || (opcode == OP_CRRANGE && max > 0))
7492          jump = CMP(SLJIT_C_GREATER_EQUAL, TMP1, 0, SLJIT_IMM, arg1);          jump = CMP(SLJIT_C_GREATER_EQUAL, TMP1, 0, SLJIT_IMM, max);
7493        OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), POSSESSIVE0, TMP1, 0);        OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), POSSESSIVE0, TMP1, 0);
7494        }        }
7495    
# Line 6987  switch(opcode) Line 7516  switch(opcode)
7516      OP1(SLJIT_MOV, base, offset0, STR_PTR, 0);      OP1(SLJIT_MOV, base, offset0, STR_PTR, 0);
7517      if (opcode <= OP_PLUS)      if (opcode <= OP_PLUS)
7518        JUMPTO(SLJIT_JUMP, label);        JUMPTO(SLJIT_JUMP, label);
7519      else if (opcode == OP_CRRANGE && arg1 == 0)      else if (opcode == OP_CRRANGE && max == 0)
7520        {        {
7521        OP2(SLJIT_ADD, base, offset1, base, offset1, SLJIT_IMM, 1);        OP2(SLJIT_ADD, base, offset1, base, offset1, SLJIT_IMM, 1);
7522        JUMPTO(SLJIT_JUMP, label);        JUMPTO(SLJIT_JUMP, label);
# Line 6997  switch(opcode) Line 7526  switch(opcode)
7526        OP1(SLJIT_MOV, TMP1, 0, base, offset1);        OP1(SLJIT_MOV, TMP1, 0, base, offset1);
7527        OP2(SLJIT_ADD, TMP1, 0, TMP1, 0, SLJIT_IMM, 1);        OP2(SLJIT_ADD, TMP1, 0, TMP1, 0, SLJIT_IMM, 1);
7528        OP1(SLJIT_MOV, base, offset1, TMP1, 0);        OP1(SLJIT_MOV, base, offset1, TMP1, 0);
7529        CMPTO(SLJIT_C_LESS, TMP1, 0, SLJIT_IMM, arg1 + 1, label);        CMPTO(SLJIT_C_LESS, TMP1, 0, SLJIT_IMM, max + 1, label);
7530        }        }
7531      set_jumps(nomatch, LABEL());      set_jumps(nomatch, LABEL());
7532      if (opcode == OP_CRRANGE)      if (opcode == OP_CRRANGE)
7533        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));
7534      OP1(SLJIT_MOV, STR_PTR, 0, base, offset0);      OP1(SLJIT_MOV, STR_PTR, 0, base, offset0);
7535      }      }
7536    BACKTRACK_AS(iterator_backtrack)->matchingpath = LABEL();    BACKTRACK_AS(iterator_backtrack)->matchingpath = LABEL();
# Line 7039  switch(opcode) Line 7568  switch(opcode)
7568    break;    break;
7569    
7570    case OP_EXACT:    case OP_EXACT:
7571    OP1(SLJIT_MOV, tmp_base, tmp_offset, SLJIT_IMM, arg1);    OP1(SLJIT_MOV, tmp_base, tmp_offset, SLJIT_IMM, max);
7572    label = LABEL();    label = LABEL();
7573    compile_char1_matchingpath(common, type, cc, &backtrack->topbacktracks);    compile_char1_matchingpath(common, type, cc, &backtrack->topbacktracks);
7574    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 7052  switch(opcode) Line 7581  switch(opcode)
7581    if (opcode == OP_POSPLUS)    if (opcode == OP_POSPLUS)
7582      compile_char1_matchingpath(common, type, cc, &backtrack->topbacktracks);      compile_char1_matchingpath(common, type, cc, &backtrack->topbacktracks);
7583    if (opcode == OP_POSUPTO)    if (opcode == OP_POSUPTO)
7584      OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), POSSESSIVE1, SLJIT_IMM, arg1);      OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), POSSESSIVE1, SLJIT_IMM, max);
7585    OP1(SLJIT_MOV, tmp_base, tmp_offset, STR_PTR, 0);    OP1(SLJIT_MOV, tmp_base, tmp_offset, STR_PTR, 0);
7586    label = LABEL();    label = LABEL();
7587    compile_char1_matchingpath(common, type, cc, &nomatch);    compile_char1_matchingpath(common, type, cc, &nomatch);
# Line 7076  switch(opcode) Line 7605  switch(opcode)
7605    OP1(SLJIT_MOV, STR_PTR, 0, tmp_base, tmp_offset);    OP1(SLJIT_MOV, STR_PTR, 0, tmp_base, tmp_offset);
7606    break;    break;
7607    
7608      case OP_CRPOSRANGE:
7609      /* Combination of OP_EXACT and OP_POSSTAR or OP_POSUPTO */
7610      OP1(SLJIT_MOV, tmp_base, tmp_offset, SLJIT_IMM, min);
7611      label = LABEL();
7612      compile_char1_matchingpath(common, type, cc, &backtrack->topbacktracks);
7613      OP2(SLJIT_SUB | SLJIT_SET_E, tmp_base, tmp_offset, tmp_base, tmp_offset, SLJIT_IMM, 1);
7614      JUMPTO(SLJIT_C_NOT_ZERO, label);
7615    
7616      if (max != 0)
7617        {
7618        SLJIT_ASSERT(max - min > 0);
7619        OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), POSSESSIVE1, SLJIT_IMM, max - min);
7620        }
7621      OP1(SLJIT_MOV, tmp_base, tmp_offset, STR_PTR, 0);
7622      label = LABEL();
7623      compile_char1_matchingpath(common, type, cc, &nomatch);
7624      OP1(SLJIT_MOV, tmp_base, tmp_offset, STR_PTR, 0);
7625      if (max == 0)
7626        JUMPTO(SLJIT_JUMP, label);
7627      else
7628        {
7629        OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_MEM1(SLJIT_LOCALS_REG), POSSESSIVE1, SLJIT_MEM1(SLJIT_LOCALS_REG), POSSESSIVE1, SLJIT_IMM, 1);
7630        JUMPTO(SLJIT_C_NOT_ZERO, label);
7631        }
7632      set_jumps(nomatch, LABEL());
7633      OP1(SLJIT_MOV, STR_PTR, 0, tmp_base, tmp_offset);
7634      break;
7635    
7636    default:    default:
7637    SLJIT_ASSERT_STOP();    SLJIT_ASSERT_STOP();
7638    break;    break;
7639    }    }
7640    
7641  decrease_call_count(common);  count_match(common);
7642  return end;  return end;
7643  }  }
7644    
# Line 7148  if (!optimized_cbracket) Line 7705  if (!optimized_cbracket)
7705  return cc + 1 + IMM2_SIZE;  return cc + 1 + IMM2_SIZE;
7706  }  }
7707    
7708    static SLJIT_INLINE pcre_uchar *compile_control_verb_matchingpath(compiler_common *common, pcre_uchar *cc, backtrack_common *parent)
7709    {
7710    DEFINE_COMPILER;
7711    backtrack_common *backtrack;
7712    pcre_uchar opcode = *cc;
7713    pcre_uchar *ccend = cc + 1;
7714    
7715    if (opcode == OP_PRUNE_ARG || opcode == OP_SKIP_ARG || opcode == OP_THEN_ARG)
7716      ccend += 2 + cc[1];
7717    
7718    PUSH_BACKTRACK(sizeof(backtrack_common), cc, NULL);
7719    
7720    if (opcode == OP_SKIP)
7721      {
7722      allocate_stack(common, 1);
7723      OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), STR_PTR, 0);
7724      return ccend;
7725      }
7726    
7727    if (opcode == OP_PRUNE_ARG || opcode == OP_THEN_ARG)
7728      {
7729      OP1(SLJIT_MOV, TMP1, 0, ARGUMENTS, 0);
7730      OP1(SLJIT_MOV, TMP2, 0, SLJIT_IMM, (sljit_sw)(cc + 2));
7731      OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), common->mark_ptr, TMP2, 0);
7732      OP1(SLJIT_MOV, SLJIT_MEM1(TMP1), SLJIT_OFFSETOF(jit_arguments, mark_ptr), TMP2, 0);
7733      }
7734    
7735    return ccend;
7736    }
7737    
7738    static pcre_uchar then_trap_opcode[1] = { OP_THEN_TRAP };
7739    
7740    static SLJIT_INLINE void compile_then_trap_matchingpath(compiler_common *common, pcre_uchar *cc, pcre_uchar *ccend, backtrack_common *parent)
7741    {
7742    DEFINE_COMPILER;
7743    backtrack_common *backtrack;
7744    BOOL needs_control_head;
7745    int size;
7746    
7747    PUSH_BACKTRACK_NOVALUE(sizeof(then_trap_backtrack), cc);
7748    common->then_trap = BACKTRACK_AS(then_trap_backtrack);
7749    BACKTRACK_AS(then_trap_backtrack)->common.cc = then_trap_opcode;
7750    BACKTRACK_AS(then_trap_backtrack)->start = (sljit_sw)(cc - common->start);
7751    BACKTRACK_AS(then_trap_backtrack)->framesize = get_framesize(common, cc, ccend, FALSE, &needs_control_head);
7752    
7753    size = BACKTRACK_AS(then_trap_backtrack)->framesize;
7754    size = 3 + (size < 0 ? 0 : size);
7755    
7756    OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), common->control_head_ptr);
7757    allocate_stack(common, size);
7758    if (size > 3)
7759      OP2(SLJIT_SUB, SLJIT_MEM1(SLJIT_LOCALS_REG), common->control_head_ptr, STACK_TOP, 0, SLJIT_IMM, (size - 3) * sizeof(sljit_sw));
7760    else
7761      OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), common->control_head_ptr, STACK_TOP, 0);
7762    OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(size - 1), SLJIT_IMM, BACKTRACK_AS(then_trap_backtrack)->start);
7763    OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(size - 2), SLJIT_IMM, type_then_trap);
7764    OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(size - 3), TMP2, 0);
7765    
7766    size = BACKTRACK_AS(then_trap_backtrack)->framesize;
7767    if (size >= 0)
7768      init_frame(common, cc, ccend, size - 1, 0, FALSE);
7769    }
7770    
7771  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)
7772  {  {
7773  DEFINE_COMPILER;  DEFINE_COMPILER;
7774  backtrack_common *backtrack;  backtrack_common *backtrack;
7775    BOOL has_then_trap = FALSE;
7776    then_trap_backtrack *save_then_trap = NULL;
7777    
7778    SLJIT_ASSERT(*ccend == OP_END || (*ccend >= OP_ALT && *ccend <= OP_KETRPOS));
7779    
7780    if (common->has_then && common->then_offsets[cc - common->start] != 0)
7781      {
7782      SLJIT_ASSERT(*ccend != OP_END && common->control_head_ptr != 0);
7783      has_then_trap = TRUE;
7784      save_then_trap = common->then_trap;
7785      /* Tail item on backtrack. */
7786      compile_then_trap_matchingpath(common, cc, ccend, parent);
7787      }
7788    
7789  while (cc < ccend)  while (cc < ccend)
7790    {    {
# Line 7277  while (cc < ccend) Line 7910  while (cc < ccend)
7910    
7911      case OP_CLASS:      case OP_CLASS:
7912      case OP_NCLASS:      case OP_NCLASS:
7913      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)
7914        cc = compile_iterator_matchingpath(common, cc, parent);        cc = compile_iterator_matchingpath(common, cc, parent);
7915      else      else
7916        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 7285  while (cc < ccend) Line 7918  while (cc < ccend)
7918    
7919  #if defined SUPPORT_UTF || defined COMPILE_PCRE16 || defined COMPILE_PCRE32  #if defined SUPPORT_UTF || defined COMPILE_PCRE16 || defined COMPILE_PCRE32
7920      case OP_XCLASS:      case OP_XCLASS:
7921      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)
7922        cc = compile_iterator_matchingpath(common, cc, parent);        cc = compile_iterator_matchingpath(common, cc, parent);
7923      else      else
7924        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 7294  while (cc < ccend) Line 7927  while (cc < ccend)
7927    
7928      case OP_REF:      case OP_REF:
7929      case OP_REFI:      case OP_REFI:
7930      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)
7931          cc = compile_ref_iterator_matchingpath(common, cc, parent);
7932        else
7933          {
7934          compile_ref_matchingpath(common, cc, parent->top != NULL ? &parent->top->nextbacktracks : &parent->topbacktracks, TRUE, FALSE);
7935          cc += 1 + IMM2_SIZE;
7936          }
7937        break;
7938    
7939        case OP_DNREF:
7940        case OP_DNREFI:
7941        if (cc[1 + 2 * IMM2_SIZE] >= OP_CRSTAR && cc[1 + 2 * IMM2_SIZE] <= OP_CRPOSRANGE)
7942        cc = compile_ref_iterator_matchingpath(common, cc, parent);        cc = compile_ref_iterator_matchingpath(common, cc, parent);
7943      else      else
7944        cc = compile_ref_matchingpath(common, cc, parent->top != NULL ? &parent->top->nextbacktracks : &parent->topbacktracks, TRUE, FALSE);        {
7945          compile_dnref_search(common, cc, parent->top != NULL ? &parent->top->nextbacktracks : &parent->topbacktracks);
7946          compile_ref_matchingpath(common, cc, parent->top != NULL ? &parent->top->nextbacktracks : &parent->topbacktracks, TRUE, FALSE);
7947          cc += 1 + 2 * IMM2_SIZE;
7948          }
7949      break;      break;
7950    
7951      case OP_RECURSE:      case OP_RECURSE:
# Line 7332  while (cc < ccend) Line 7980  while (cc < ccend)
7980        }        }
7981      BACKTRACK_AS(braminzero_backtrack)->matchingpath = LABEL();      BACKTRACK_AS(braminzero_backtrack)->matchingpath = LABEL();
7982      if (cc[1] > OP_ASSERTBACK_NOT)      if (cc[1] > OP_ASSERTBACK_NOT)
7983        decrease_call_count(common);        count_match(common);
7984      break;      break;
7985    
7986      case OP_ONCE:      case OP_ONCE:
# Line 7368  while (cc < ccend) Line 8016  while (cc < ccend)
8016      PUSH_BACKTRACK_NOVALUE(sizeof(backtrack_common), cc);      PUSH_BACKTRACK_NOVALUE(sizeof(backtrack_common), cc);
8017      SLJIT_ASSERT(common->mark_ptr != 0);      SLJIT_ASSERT(common->mark_ptr != 0);
8018      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);
8019      allocate_stack(common, 1);      allocate_stack(common, common->has_skip_arg ? 5 : 1);
8020      OP1(SLJIT_MOV, TMP1, 0, ARGUMENTS, 0);      OP1(SLJIT_MOV, TMP1, 0, ARGUMENTS, 0);
8021      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);
8022      OP1(SLJIT_MOV, TMP2, 0, SLJIT_IMM, (sljit_sw)(cc + 2));      OP1(SLJIT_MOV, TMP2, 0, SLJIT_IMM, (sljit_sw)(cc + 2));
8023      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);
8024      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);
8025        if (common->has_skip_arg)
8026          {
8027          OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), common->control_head_ptr);
8028          OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), common->control_head_ptr, STACK_TOP, 0);
8029          OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(1), SLJIT_IMM, type_mark);
8030          OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(2), SLJIT_IMM, (sljit_sw)(cc + 2));
8031          OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(3), STR_PTR, 0);
8032          OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), TMP1, 0);
8033          }
8034      cc += 1 + 2 + cc[1];      cc += 1 + 2 + cc[1];
8035      break;      break;
8036    
     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. */  
   
8037      case OP_PRUNE:      case OP_PRUNE:
8038      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;  
   
8039      case OP_SKIP:      case OP_SKIP:
8040      PUSH_BACKTRACK_NOVALUE(sizeof(backtrack_common), cc);      case OP_SKIP_ARG:
8041      OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), common->control_head_ptr);      case OP_THEN:
8042      allocate_stack(common, 3);      case OP_THEN_ARG:
8043      OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), common->control_head_ptr, STACK_TOP, 0);      case OP_COMMIT:
8044      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;  
8045      break;      break;
8046    
8047      case OP_FAIL:      case OP_FAIL:
# Line 7428  while (cc < ccend) Line 8065  while (cc < ccend)
8065    if (cc == NULL)    if (cc == NULL)
8066      return;      return;
8067    }    }
8068    
8069    if (has_then_trap)
8070      {
8071      /* Head item on backtrack. */
8072      PUSH_BACKTRACK_NOVALUE(sizeof(then_trap_backtrack), cc);
8073      BACKTRACK_AS(then_trap_backtrack)->common.cc = then_trap_opcode;
8074      BACKTRACK_AS(then_trap_backtrack)->then_trap = common->then_trap;
8075      common->then_trap = save_then_trap;
8076      }
8077  SLJIT_ASSERT(cc == ccend);  SLJIT_ASSERT(cc == ccend);
8078  }  }
8079    
# Line 7452  DEFINE_COMPILER; Line 8098  DEFINE_COMPILER;
8098  pcre_uchar *cc = current->cc;  pcre_uchar *cc = current->cc;
8099  pcre_uchar opcode;  pcre_uchar opcode;
8100  pcre_uchar type;  pcre_uchar type;
8101  int arg1 = -1, arg2 = -1;  int max = -1, min = -1;
8102  struct sljit_label *label = NULL;  struct sljit_label *label = NULL;
8103  struct sljit_jump *jump = NULL;  struct sljit_jump *jump = NULL;
8104  jump_list *jumplist = NULL;  jump_list *jumplist = NULL;
# Line 7461  int base = (private_data_ptr == 0) ? SLJ Line 8107  int base = (private_data_ptr == 0) ? SLJ
8107  int offset0 = (private_data_ptr == 0) ? STACK(0) : private_data_ptr;  int offset0 = (private_data_ptr == 0) ? STACK(0) : private_data_ptr;
8108  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);
8109    
8110  cc = get_iterator_parameters(common, cc, &opcode, &type, &arg1, &arg2, NULL);  cc = get_iterator_parameters(common, cc, &opcode, &type, &max, &min, NULL);
8111    
8112  switch(opcode)  switch(opcode)
8113    {    {
# Line 7480  switch(opcode) Line 8126  switch(opcode)
8126    else    else
8127      {      {
8128      if (opcode == OP_UPTO)      if (opcode == OP_UPTO)
8129        arg2 = 0;        min = 0;
8130      if (opcode <= OP_PLUS)      if (opcode <= OP_PLUS)
8131        {        {
8132        OP1(SLJIT_MOV, STR_PTR, 0, base, offset0);        OP1(SLJIT_MOV, STR_PTR, 0, base, offset0);
# Line 7490  switch(opcode) Line 8136  switch(opcode)
8136        {        {
8137        OP1(SLJIT_MOV, TMP1, 0, base, offset1);        OP1(SLJIT_MOV, TMP1, 0, base, offset1);
8138        OP1(SLJIT_MOV, STR_PTR, 0, base, offset0);        OP1(SLJIT_MOV, STR_PTR, 0, base, offset0);
8139        jump = CMP(SLJIT_C_LESS_EQUAL, TMP1, 0, SLJIT_IMM, arg2 + 1);        jump = CMP(SLJIT_C_LESS_EQUAL, TMP1, 0, SLJIT_IMM, min + 1);
8140        OP2(SLJIT_SUB, base, offset1, TMP1, 0, SLJIT_IMM, 1);        OP2(SLJIT_SUB, base, offset1, TMP1, 0, SLJIT_IMM, 1);
8141        }        }
8142      skip_char_back(common);      skip_char_back(common);
# Line 7535  switch(opcode) Line 8181  switch(opcode)
8181    OP1(SLJIT_MOV, base, offset1, TMP1, 0);    OP1(SLJIT_MOV, base, offset1, TMP1, 0);
8182    
8183    if (opcode == OP_CRMINRANGE)    if (opcode == OP_CRMINRANGE)
8184      CMPTO(SLJIT_C_LESS, TMP1, 0, SLJIT_IMM, arg2 + 1, label);      CMPTO(SLJIT_C_LESS, TMP1, 0, SLJIT_IMM, min + 1, label);
8185    
8186    if (opcode == OP_CRMINRANGE && arg1 == 0)    if (opcode == OP_CRMINRANGE && max == 0)
8187      JUMPTO(SLJIT_JUMP, CURRENT_AS(iterator_backtrack)->matchingpath);      JUMPTO(SLJIT_JUMP, CURRENT_AS(iterator_backtrack)->matchingpath);
8188    else    else
8189      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);
8190    
8191    set_jumps(jumplist, LABEL());    set_jumps(jumplist, LABEL());
8192    if (private_data_ptr == 0)    if (private_data_ptr == 0)
# Line 7575  switch(opcode) Line 8221  switch(opcode)
8221    
8222    case OP_EXACT:    case OP_EXACT:
8223    case OP_POSPLUS:    case OP_POSPLUS:
8224      case OP_CRPOSRANGE:
8225    set_jumps(current->topbacktracks, LABEL());    set_jumps(current->topbacktracks, LABEL());
8226    break;    break;
8227    
# Line 7589  switch(opcode) Line 8236  switch(opcode)
8236    }    }
8237  }  }
8238    
8239  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)
8240  {  {
8241  DEFINE_COMPILER;  DEFINE_COMPILER;
8242  pcre_uchar *cc = current->cc;  pcre_uchar *cc = current->cc;
8243    BOOL ref = (*cc == OP_REF || *cc == OP_REFI);
8244  pcre_uchar type;  pcre_uchar type;
8245    
8246  type = cc[1 + IMM2_SIZE];  type = cc[ref ? 1 + IMM2_SIZE : 1 + 2 * IMM2_SIZE];
8247    
8248  if ((type & 0x1) == 0)  if ((type & 0x1) == 0)
8249    {    {
8250      /* Maximize case. */
8251    set_jumps(current->topbacktracks, LABEL());    set_jumps(current->topbacktracks, LABEL());
8252    OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(STACK_TOP), STACK(0));    OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(STACK_TOP), STACK(0));
8253    free_stack(common, 1);    free_stack(common, 1);
# Line 7608  if ((type & 0x1) == 0) Line 8258  if ((type & 0x1) == 0)
8258  OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(STACK_TOP), STACK(0));  OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(STACK_TOP), STACK(0));
8259  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);
8260  set_jumps(current->topbacktracks, LABEL());  set_jumps(current->topbacktracks, LABEL());
8261  free_stack(common, 2);  free_stack(common, ref ? 2 : 3);
8262  }  }
8263    
8264  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)
8265  {  {
8266  DEFINE_COMPILER;  DEFINE_COMPILER;
8267    
# Line 7707  if (bra == OP_BRAZERO) Line 8357  if (bra == OP_BRAZERO)
8357  static void compile_bracket_backtrackingpath(compiler_common *common, struct backtrack_common *current)  static void compile_bracket_backtrackingpath(compiler_common *common, struct backtrack_common *current)
8358  {  {
8359  DEFINE_COMPILER;  DEFINE_COMPILER;
8360  int opcode;  int opcode, stacksize, count;
8361  int offset = 0;  int offset = 0;
8362  int private_data_ptr = CURRENT_AS(bracket_backtrack)->private_data_ptr;  int private_data_ptr = CURRENT_AS(bracket_backtrack)->private_data_ptr;
8363  int stacksize;  int repeat_ptr = 0, repeat_type = 0, repeat_count = 0;
 int count;  
8364  pcre_uchar *cc = current->cc;  pcre_uchar *cc = current->cc;
8365  pcre_uchar *ccbegin;  pcre_uchar *ccbegin;
8366  pcre_uchar *ccprev;  pcre_uchar *ccprev;
# Line 7725  BOOL needs_control_head = FALSE; Line 8374  BOOL needs_control_head = FALSE;
8374  struct sljit_jump *brazero = NULL;  struct sljit_jump *brazero = NULL;
8375  struct sljit_jump *once = NULL;  struct sljit_jump *once = NULL;
8376  struct sljit_jump *cond = NULL;  struct sljit_jump *cond = NULL;
8377  struct sljit_label *rminlabel = NULL;  struct sljit_label *rmin_label = NULL;
8378    struct sljit_label *exact_label = NULL;
8379    
8380  if (*cc == OP_BRAZERO || *cc == OP_BRAMINZERO)  if (*cc == OP_BRAZERO || *cc == OP_BRAMINZERO)
8381    {    {
# Line 7734  if (*cc == OP_BRAZERO || *cc == OP_BRAMI Line 8384  if (*cc == OP_BRAZERO || *cc == OP_BRAMI
8384    }    }
8385    
8386  opcode = *cc;  opcode = *cc;
8387    ccbegin = bracketend(cc) - 1 - LINK_SIZE;
8388    ket = *ccbegin;
8389    if (ket == OP_KET && PRIVATE_DATA(ccbegin) != 0)
8390      {
8391      repeat_ptr = PRIVATE_DATA(ccbegin);
8392      repeat_type = PRIVATE_DATA(ccbegin + 2);
8393      repeat_count = PRIVATE_DATA(ccbegin + 3);
8394      SLJIT_ASSERT(repeat_type != 0 && repeat_count != 0);
8395      if (repeat_type == OP_UPTO)
8396        ket = OP_KETRMAX;
8397      if (repeat_type == OP_MINUPTO)
8398        ket = OP_KETRMIN;
8399      }
8400  ccbegin = cc;  ccbegin = cc;
 ket = *(bracketend(ccbegin) - 1 - LINK_SIZE);  
8401  cc += GET(cc, 1);  cc += GET(cc, 1);
8402  has_alternatives = *cc == OP_ALT;  has_alternatives = *cc == OP_ALT;
8403  if (SLJIT_UNLIKELY(opcode == OP_COND) || SLJIT_UNLIKELY(opcode == OP_SCOND))  if (SLJIT_UNLIKELY(opcode == OP_COND) || SLJIT_UNLIKELY(opcode == OP_SCOND))
# Line 7754  if (opcode == OP_ONCE) Line 8416  if (opcode == OP_ONCE)
8416    CURRENT_AS(bracket_backtrack)->u.framesize >>= 1;    CURRENT_AS(bracket_backtrack)->u.framesize >>= 1;
8417    }    }
8418    
8419    if (ket != OP_KET && repeat_type != 0)
8420      {
8421      /* TMP1 is used in OP_KETRMIN below. */
8422      OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(STACK_TOP), STACK(0));
8423      free_stack(common, 1);
8424      if (repeat_type == OP_UPTO)
8425        OP2(SLJIT_ADD, SLJIT_MEM1(SLJIT_LOCALS_REG), repeat_ptr, TMP1, 0, SLJIT_IMM, 1);
8426      else
8427        OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), repeat_ptr, TMP1, 0);
8428      }
8429    
8430  if (ket == OP_KETRMAX)  if (ket == OP_KETRMAX)
8431    {    {
8432    if (bra == OP_BRAZERO)    if (bra == OP_BRAZERO)
# Line 7768  else if (ket == OP_KETRMIN) Line 8441  else if (ket == OP_KETRMIN)
8441    if (bra != OP_BRAMINZERO)    if (bra != OP_BRAMINZERO)
8442      {      {
8443      OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(STACK_TOP), STACK(0));      OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(STACK_TOP), STACK(0));
8444      if (opcode >= OP_SBRA || opcode == OP_ONCE)      if (repeat_type != 0)
8445          {
8446          /* TMP1 was set a few lines above. */
8447          CMPTO(SLJIT_C_NOT_EQUAL, TMP1, 0, SLJIT_IMM, 0, CURRENT_AS(bracket_backtrack)->recursive_matchingpath);
8448          /* Drop STR_PTR for non-greedy plus quantifier. */
8449          if (opcode != OP_ONCE)
8450            free_stack(common, 1);
8451          }
8452        else if (opcode >= OP_SBRA || opcode == OP_ONCE)
8453        {        {
8454        /* Checking zero-length iteration. */        /* Checking zero-length iteration. */
8455        if (opcode != OP_ONCE || CURRENT_AS(bracket_backtrack)->u.framesize < 0)        if (opcode != OP_ONCE || CURRENT_AS(bracket_backtrack)->u.framesize < 0)
# Line 7778  else if (ket == OP_KETRMIN) Line 8459  else if (ket == OP_KETRMIN)
8459          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);
8460          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);
8461          }          }
8462          /* Drop STR_PTR for non-greedy plus quantifier. */
8463        if (opcode != OP_ONCE)        if (opcode != OP_ONCE)
8464          free_stack(common, 1);          free_stack(common, 1);
8465        }        }
8466      else      else
8467        JUMPTO(SLJIT_JUMP, CURRENT_AS(bracket_backtrack)->recursive_matchingpath);        JUMPTO(SLJIT_JUMP, CURRENT_AS(bracket_backtrack)->recursive_matchingpath);
8468      }      }
8469    rminlabel = LABEL();    rmin_label = LABEL();
8470      if (repeat_type != 0)
8471        OP2(SLJIT_ADD, SLJIT_MEM1(SLJIT_LOCALS_REG), repeat_ptr, SLJIT_MEM1(SLJIT_LOCALS_REG), repeat_ptr, SLJIT_IMM, 1);
8472    }    }
8473  else if (bra == OP_BRAZERO)  else if (bra == OP_BRAZERO)
8474    {    {
# Line 7792  else if (bra == OP_BRAZERO) Line 8476  else if (bra == OP_BRAZERO)
8476    free_stack(common, 1);    free_stack(common, 1);
8477    brazero = CMP(SLJIT_C_NOT_EQUAL, TMP1, 0, SLJIT_IMM, 0);    brazero = CMP(SLJIT_C_NOT_EQUAL, TMP1, 0, SLJIT_IMM, 0);
8478    }    }
8479    else if (repeat_type == OP_EXACT)
8480      {
8481      OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), repeat_ptr, SLJIT_IMM, 1);
8482      exact_label = LABEL();
8483      }
8484    
8485  if (offset != 0)  if (offset != 0)
8486    {    {
# Line 7933  if (has_alternatives) Line 8622  if (has_alternatives)
8622          return;          return;
8623        }        }
8624    
8625      /* Instructions after the current alternative is succesfully matched. */      /* Instructions after the current alternative is successfully matched. */
8626      /* There is a similar code in compile_bracket_matchingpath. */      /* There is a similar code in compile_bracket_matchingpath. */
8627      if (opcode == OP_ONCE)      if (opcode == OP_ONCE)
8628        match_once_common(common, ket, CURRENT_AS(bracket_backtrack)->u.framesize, private_data_ptr, has_alternatives, needs_control_head);        match_once_common(common, ket, CURRENT_AS(bracket_backtrack)->u.framesize, private_data_ptr, has_alternatives, needs_control_head);
8629    
8630      stacksize = 0;      stacksize = 0;
8631        if (repeat_type == OP_MINUPTO)
8632          {
8633          /* We need to preserve the counter. TMP2 will be used below. */
8634          OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), repeat_ptr);
8635          stacksize++;
8636          }
8637      if (ket != OP_KET || bra != OP_BRA)      if (ket != OP_KET || bra != OP_BRA)
8638        stacksize++;        stacksize++;
8639      if (offset != 0)      if (offset != 0)
# Line 7952  if (has_alternatives) Line 8647  if (has_alternatives)
8647        stacksize++;        stacksize++;
8648    
8649      if (stacksize > 0)      if (stacksize > 0)
8650          allocate_stack(common, stacksize);
8651    
8652        stacksize = 0;
8653        if (repeat_type == OP_MINUPTO)
8654        {        {
8655        if (opcode != OP_ONCE || CURRENT_AS(bracket_backtrack)->u.framesize >= 0)        /* TMP2 was set above. */
8656          allocate_stack(common, stacksize);        OP2(SLJIT_SUB, SLJIT_MEM1(STACK_TOP), STACK(stacksize), TMP2, 0, SLJIT_IMM, 1);
8657        else        stacksize++;
         {  
         /* 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));  
         }  
8658        }        }
8659    
     stacksize = 0;  
8660      if (ket != OP_KET || bra != OP_BRA)      if (ket != OP_KET || bra != OP_BRA)
8661        {        {
8662        if (ket != OP_KET)        if (ket != OP_KET)
# Line 8074  else if (opcode == OP_ONCE) Line 8767  else if (opcode == OP_ONCE)
8767      }      }
8768    }    }
8769    
8770  if (ket == OP_KETRMAX)  if (repeat_type == OP_EXACT)
8771      {
8772      OP2(SLJIT_ADD, TMP1, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), repeat_ptr, SLJIT_IMM, 1);
8773      OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), repeat_ptr, TMP1, 0);
8774      CMPTO(SLJIT_C_LESS_EQUAL, TMP1, 0, SLJIT_IMM, repeat_count, exact_label);
8775      }
8776    else if (ket == OP_KETRMAX)
8777    {    {
8778    OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(STACK_TOP), STACK(0));    OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(STACK_TOP), STACK(0));
8779    if (bra != OP_BRAZERO)    if (bra != OP_BRAZERO)
8780      free_stack(common, 1);      free_stack(common, 1);
8781    
8782    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);
8783    if (bra == OP_BRAZERO)    if (bra == OP_BRAZERO)
8784      {      {
# Line 8097  else if (ket == OP_KETRMIN) Line 8797  else if (ket == OP_KETRMIN)
8797    affect badly the free_stack(2) above. */    affect badly the free_stack(2) above. */
8798    if (opcode != OP_ONCE)    if (opcode != OP_ONCE)
8799      free_stack(common, 1);      free_stack(common, 1);
8800    CMPTO(SLJIT_C_NOT_EQUAL, TMP1, 0, SLJIT_IMM, 0, rminlabel);    CMPTO(SLJIT_C_NOT_EQUAL, TMP1, 0, SLJIT_IMM, 0, rmin_label);
8801    if (opcode == OP_ONCE)    if (opcode == OP_ONCE)
8802      free_stack(common, bra == OP_BRAMINZERO ? 2 : 1);      free_stack(common, bra == OP_BRAMINZERO ? 2 : 1);
8803    else if (bra == OP_BRAMINZERO)    else if (bra == OP_BRAMINZERO)
# Line 8111  else if (bra == OP_BRAZERO) Line 8811  else if (bra == OP_BRAZERO)
8811    }    }
8812  }  }
8813    
8814  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)
8815  {  {
8816  DEFINE_COMPILER;  DEFINE_COMPILER;
8817  int offset;  int offset;
# Line 8150  if (current->topbacktracks) Line 8850  if (current->topbacktracks)
8850  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));
8851  }  }
8852    
8853  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)
8854  {  {
8855  assert_backtrack backtrack;  assert_backtrack backtrack;
8856    
# Line 8174  else Line 8874  else
8874  SLJIT_ASSERT(!current->nextbacktracks && !current->topbacktracks);  SLJIT_ASSERT(!current->nextbacktracks && !current->topbacktracks);
8875  }  }
8876    
8877    static SLJIT_INLINE void compile_control_verb_backtrackingpath(compiler_common *common, struct backtrack_common *current)
8878    {
8879    DEFINE_COMPILER;
8880    pcre_uchar opcode = *current->cc;
8881    struct sljit_label *loop;
8882    struct sljit_jump *jump;
8883    
8884    if (opcode == OP_THEN || opcode == OP_THEN_ARG)
8885      {
8886      if (common->then_trap != NULL)
8887        {
8888        SLJIT_ASSERT(common->control_head_ptr != 0);
8889    
8890        OP1(SLJIT_MOV, STACK_TOP, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), common->control_head_ptr);
8891        OP1(SLJIT_MOV, TMP1, 0, SLJIT_IMM, type_then_trap);
8892        OP1(SLJIT_MOV, TMP2, 0, SLJIT_IMM, common->then_trap->start);
8893        jump = JUMP(SLJIT_JUMP);
8894    
8895        loop = LABEL();
8896        OP1(SLJIT_MOV, STACK_TOP, 0, SLJIT_MEM1(STACK_TOP), -(int)sizeof(sljit_sw));
8897        JUMPHERE(jump);
8898        CMPTO(SLJIT_C_NOT_EQUAL, SLJIT_MEM1(STACK_TOP), -(int)(2 * sizeof(sljit_sw)), TMP1, 0, loop);
8899        CMPTO(SLJIT_C_NOT_EQUAL, SLJIT_MEM1(STACK_TOP), -(int)(3 * sizeof(sljit_sw)), TMP2, 0, loop);
8900        add_jump(compiler, &common->then_trap->quit, JUMP(SLJIT_JUMP));
8901        return;
8902        }
8903      else if (common->positive_assert)
8904        {
8905        add_jump(compiler, &common->positive_assert_quit, JUMP(SLJIT_JUMP));
8906        return;
8907        }
8908      }
8909    
8910    if (common->local_exit)
8911      {
8912      if (common->quit_label == NULL)
8913        add_jump(compiler, &common->quit, JUMP(SLJIT_JUMP));
8914      else
8915        JUMPTO(SLJIT_JUMP, common->quit_label);
8916      return;
8917      }
8918    
8919    if (opcode == OP_SKIP_ARG)
8920      {
8921      SLJIT_ASSERT(common->control_head_ptr != 0);
8922      OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), common->control_head_ptr);
8923      OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), LOCALS0, STACK_TOP, 0);
8924      OP1(SLJIT_MOV, STACK_TOP, 0, SLJIT_IMM, (sljit_sw)(current->cc + 2));
8925      sljit_emit_ijump(compiler, SLJIT_CALL2, SLJIT_IMM, SLJIT_FUNC_OFFSET(do_search_mark));
8926      OP1(SLJIT_MOV, STACK_TOP, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), LOCALS0);
8927    
8928      OP1(SLJIT_MOV, STR_PTR, 0, TMP1, 0);
8929      add_jump(compiler, &common->reset_match, CMP(SLJIT_C_NOT_EQUAL, STR_PTR, 0, SLJIT_IMM, -1));
8930      return;
8931      }
8932    
8933    if (opcode == OP_SKIP)
8934      OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(STACK_TOP), STACK(0));
8935    else
8936      OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_IMM, 0);
8937    add_jump(compiler, &common->reset_match, JUMP(SLJIT_JUMP));
8938    }
8939    
8940    static SLJIT_INLINE void compile_then_trap_backtrackingpath(compiler_common *common, struct backtrack_common *current)
8941    {
8942    DEFINE_COMPILER;
8943    struct sljit_jump *jump;
8944    int size;
8945    
8946    if (CURRENT_AS(then_trap_backtrack)->then_trap)
8947      {
8948      common->then_trap = CURRENT_AS(then_trap_backtrack)->then_trap;
8949      return;
8950      }
8951    
8952    size = CURRENT_AS(then_trap_backtrack)->framesize;
8953    size = 3 + (size < 0 ? 0 : size);
8954    
8955    OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(STACK_TOP), STACK(size - 3));
8956    free_stack(common, size);
8957    jump = JUMP(SLJIT_JUMP);
8958    
8959    set_jumps(CURRENT_AS(then_trap_backtrack)->quit, LABEL());
8960    /* STACK_TOP is set by THEN. */
8961    if (CURRENT_AS(then_trap_backtrack)->framesize >= 0)
8962      add_jump(compiler, &common->revertframes, JUMP(SLJIT_FAST_CALL));
8963    OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(STACK_TOP), STACK(0));
8964    free_stack(common, 3);
8965    
8966    JUMPHERE(jump);
8967    OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), common->control_head_ptr, TMP1, 0);
8968    }
8969    
8970  static void compile_backtrackingpath(compiler_common *common, struct backtrack_common *current)  static void compile_backtrackingpath(compiler_common *common, struct backtrack_common *current)
8971  {  {
8972  DEFINE_COMPILER;  DEFINE_COMPILER;
8973    then_trap_backtrack *save_then_trap = common->then_trap;
8974    
8975  while (current)  while (current)
8976    {    {
# Line 8265  while (current) Line 9059  while (current)
9059    
9060      case OP_REF:      case OP_REF:
9061      case OP_REFI:      case OP_REFI:
9062        case OP_DNREF:
9063        case OP_DNREFI:
9064      compile_ref_iterator_backtrackingpath(common, current);      compile_ref_iterator_backtrackingpath(common, current);
9065      break;      break;
9066    
# Line 8310  while (current) Line 9106  while (current)
9106      break;      break;
9107    
9108      case OP_MARK:      case OP_MARK:
9109      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));
9110      free_stack(common, 1);      if (common->has_skip_arg)
9111          OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(STACK_TOP), STACK(0));
9112        free_stack(common, common->has_skip_arg ? 5 : 1);
9113      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);
9114        if (common->has_skip_arg)
9115          OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), common->control_head_ptr, TMP2, 0);
9116      break;      break;
9117    
9118        case OP_THEN:
9119        case OP_THEN_ARG:
9120      case OP_PRUNE:      case OP_PRUNE:
9121      case OP_PRUNE_ARG:      case OP_PRUNE_ARG:
9122      case OP_SKIP:      case OP_SKIP:
9123      if (!common->local_exit)      case OP_SKIP_ARG:
9124        {      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_CALL1, 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);  
9125      break;      break;
9126    
9127      case OP_COMMIT:      case OP_COMMIT:
# Line 8355  while (current) Line 9140  while (current)
9140      set_jumps(current->topbacktracks, LABEL());      set_jumps(current->topbacktracks, LABEL());
9141      break;      break;
9142    
9143        case OP_THEN_TRAP:
9144        /* A virtual opcode for then traps. */
9145        compile_then_trap_backtrackingpath(common, current);
9146        break;
9147    
9148      default:      default:
9149      SLJIT_ASSERT_STOP();      SLJIT_ASSERT_STOP();
9150      break;      break;
9151      }      }
9152    current = current->prev;    current = current->prev;
9153    }    }
9154    common->then_trap = save_then_trap;
9155  }  }
9156    
9157  static SLJIT_INLINE void compile_recurse(compiler_common *common)  static SLJIT_INLINE void compile_recurse(compiler_common *common)
# Line 8370  pcre_uchar *cc = common->start + common- Line 9161  pcre_uchar *cc = common->start + common-
9161  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);
9162  pcre_uchar *ccend = bracketend(cc);  pcre_uchar *ccend = bracketend(cc);
9163  BOOL needs_control_head;  BOOL needs_control_head;
9164  int framesize = get_framesize(common, cc, TRUE, &needs_control_head);  int framesize = get_framesize(common, cc, NULL, TRUE, &needs_control_head);
9165  int private_data_size = get_private_data_copy_length(common, ccbegin, ccend, needs_control_head);  int private_data_size = get_private_data_copy_length(common, ccbegin, ccend, needs_control_head);
9166  int alternativesize;  int alternativesize;
9167  BOOL needs_frame;  BOOL needs_frame;
9168  backtrack_common altbacktrack;  backtrack_common altbacktrack;
9169  struct sljit_jump *jump;  struct sljit_jump *jump;
9170    
9171    /* Recurse captures then. */
9172    common->then_trap = NULL;
9173    
9174  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);
9175  needs_frame = framesize >= 0;  needs_frame = framesize >= 0;
9176  if (!needs_frame)  if (!needs_frame)
# Line 8395  if (needs_control_head) Line 9189  if (needs_control_head)
9189    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);
9190  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);
9191  if (needs_frame)  if (needs_frame)
9192    init_frame(common, cc, framesize + alternativesize - 1, alternativesize, TRUE);    init_frame(common, cc, NULL, framesize + alternativesize - 1, alternativesize, TRUE);
9193    
9194  if (alternativesize > 0)  if (alternativesize > 0)
9195    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 8560  else Line 9354  else
9354    }    }
9355  common->endonly = (re->options & PCRE_DOLLAR_ENDONLY) != 0;  common->endonly = (re->options & PCRE_DOLLAR_ENDONLY) != 0;
9356  common->ctypes = (sljit_sw)(tables + ctypes_offset);  common->ctypes = (sljit_sw)(tables + ctypes_offset);
9357  common->digits[0] = -2;  common->name_table = ((pcre_uchar *)re) + re->name_table_offset;
 common->name_table = (sljit_sw)((pcre_uchar *)re + re->name_table_offset);  
9358  common->name_count = re->name_count;  common->name_count = re->name_count;
9359  common->name_entry_size = re->name_entry_size;  common->name_entry_size = re->name_entry_size;
9360  common->jscript_compat = (re->options & PCRE_JAVASCRIPT_COMPAT) != 0;  common->jscript_compat = (re->options & PCRE_JAVASCRIPT_COMPAT) != 0;
# Line 8575  common->use_ucp = (re->options & PCRE_UC Line 9368  common->use_ucp = (re->options & PCRE_UC
9368  ccend = bracketend(rootbacktrack.cc);  ccend = bracketend(rootbacktrack.cc);
9369    
9370  /* Calculate the local space size on the stack. */  /* Calculate the local space size on the stack. */
9371  common->ovector_start = CALL_LIMIT + sizeof(sljit_sw);  common->ovector_start = LIMIT_MATCH + sizeof(sljit_sw);
9372  common->optimized_cbracket = (pcre_uint8 *)SLJIT_MALLOC(re->top_bracket + 1);  common->optimized_cbracket = (pcre_uint8 *)SLJIT_MALLOC(re->top_bracket + 1);
9373  if (!common->optimized_cbracket)  if (!common->optimized_cbracket)
9374    return;    return;
# Line 8590  SLJIT_ASSERT(*rootbacktrack.cc == OP_BRA Line 9383  SLJIT_ASSERT(*rootbacktrack.cc == OP_BRA
9383  common->capture_last_ptr = common->ovector_start;  common->capture_last_ptr = common->ovector_start;
9384  common->ovector_start += sizeof(sljit_sw);  common->ovector_start += sizeof(sljit_sw);
9385  #endif  #endif
9386  private_data_size = get_private_data_length(common, rootbacktrack.cc, ccend);  if (!check_opcode_types(common, rootbacktrack.cc, ccend))
 if (private_data_size < 0)  
9387    {    {