/[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 1268 by zherczeg, Mon Mar 4 08:42:15 2013 UTC revision 1442 by zherczeg, Sun Jan 12 17:17:29 2014 UTC
# Line 71  system files. */ Line 71  system files. */
71     2 - Enable capture_last_ptr (includes option 1). */     2 - Enable capture_last_ptr (includes option 1). */
72  /* #define DEBUG_FORCE_UNOPTIMIZED_CBRAS 2 */  /* #define DEBUG_FORCE_UNOPTIMIZED_CBRAS 2 */
73    
74    /* 1 - Always have a control head. */
75    /* #define DEBUG_FORCE_CONTROL_HEAD 1 */
76    
77  /* Allocate memory for the regex stack on the real machine stack.  /* Allocate memory for the regex stack on the real machine stack.
78  Fast, but limited size. */  Fast, but limited size. */
79  #define MACHINE_STACK_SIZE 32768  #define MACHINE_STACK_SIZE 32768
# Line 165  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 176  typedef struct jit_arguments { Line 179  typedef struct jit_arguments {
179    
180  typedef struct executable_functions {  typedef struct executable_functions {
181    void *executable_funcs[JIT_NUMBER_OF_COMPILE_MODES];    void *executable_funcs[JIT_NUMBER_OF_COMPILE_MODES];
182      sljit_uw *read_only_data[JIT_NUMBER_OF_COMPILE_MODES];
183      sljit_uw executable_sizes[JIT_NUMBER_OF_COMPILE_MODES];
184    PUBL(jit_callback) callback;    PUBL(jit_callback) callback;
185    void *userdata;    void *userdata;
186    pcre_uint32 top_bracket;    pcre_uint32 top_bracket;
187    sljit_uw executable_sizes[JIT_NUMBER_OF_COMPILE_MODES];    pcre_uint32 limit_match;
188  } executable_functions;  } executable_functions;
189    
190  typedef struct jump_list {  typedef struct jump_list {
# Line 193  typedef struct stub_list { Line 198  typedef struct stub_list {
198    struct stub_list *next;    struct stub_list *next;
199  } stub_list;  } stub_list;
200    
201  enum frame_types { no_frame = -1, no_stack = -2 };  typedef struct label_addr_list {
202      struct sljit_label *label;
203      sljit_uw *addr;
204      struct label_addr_list *next;
205    } label_addr_list;
206    
207    enum frame_types {
208      no_frame = -1,
209      no_stack = -2
210    };
211    
212    enum control_types {
213      type_mark = 0,
214      type_then_trap = 1
215    };
216    
217  typedef int (SLJIT_CALL *jit_function)(jit_arguments *args);  typedef int (SLJIT_CALL *jit_function)(jit_arguments *args);
218    
219  /* The following structure is the key data type for the recursive  /* The following structure is the key data type for the recursive
220  code generator. It is allocated by compile_matchingpath, and contains  code generator. It is allocated by compile_matchingpath, and contains
221  the aguments for compile_backtrackingpath. Must be the first member  the arguments for compile_backtrackingpath. Must be the first member
222  of its descendants. */  of its descendants. */
223  typedef struct backtrack_common {  typedef struct backtrack_common {
224    /* Concatenation stack. */    /* Concatenation stack. */
# Line 215  typedef struct backtrack_common { Line 234  typedef struct backtrack_common {
234  typedef struct assert_backtrack {  typedef struct assert_backtrack {
235    backtrack_common common;    backtrack_common common;
236    jump_list *condfailed;    jump_list *condfailed;
237    /* Less than 0 (-1) if a frame is not needed. */    /* Less than 0 if a frame is not needed. */
238    int framesize;    int framesize;
239    /* Points to our private memory word on the stack. */    /* Points to our private memory word on the stack. */
240    int private_data_ptr;    int private_data_ptr;
# Line 236  typedef struct bracket_backtrack { Line 255  typedef struct bracket_backtrack {
255      /* Both for OP_COND, OP_SCOND. */      /* Both for OP_COND, OP_SCOND. */
256      jump_list *condfailed;      jump_list *condfailed;
257      assert_backtrack *assert;      assert_backtrack *assert;
258      /* For OP_ONCE. -1 if not needed. */      /* For OP_ONCE. Less than 0 if not needed. */
259      int framesize;      int framesize;
260    } u;    } u;
261    /* Points to our private memory word on the stack. */    /* Points to our private memory word on the stack. */
# Line 271  typedef struct recurse_entry { Line 290  typedef struct recurse_entry {
290    /* Collects the calls until the function is not created. */    /* Collects the calls until the function is not created. */
291    jump_list *calls;    jump_list *calls;
292    /* Points to the starting opcode. */    /* Points to the starting opcode. */
293    int start;    sljit_sw start;
294  } recurse_entry;  } recurse_entry;
295    
296  typedef struct recurse_backtrack {  typedef struct recurse_backtrack {
# Line 279  typedef struct recurse_backtrack { Line 298  typedef struct recurse_backtrack {
298    BOOL inlined_pattern;    BOOL inlined_pattern;
299  } recurse_backtrack;  } recurse_backtrack;
300    
301  #define MAX_RANGE_SIZE 6  #define OP_THEN_TRAP OP_TABLE_LENGTH
302    
303    typedef struct then_trap_backtrack {
304      backtrack_common common;
305      /* If then_trap is not NULL, this structure contains the real
306      then_trap for the backtracking path. */
307      struct then_trap_backtrack *then_trap;
308      /* Points to the starting opcode. */
309      sljit_sw start;
310      /* Exit point for the then opcodes of this alternative. */
311      jump_list *quit;
312      /* Frame size of the current alternative. */
313      int framesize;
314    } then_trap_backtrack;
315    
316    #define MAX_RANGE_SIZE 4
317    
318  typedef struct compiler_common {  typedef struct compiler_common {
319      /* The sljit ceneric compiler. */
320    struct sljit_compiler *compiler;    struct sljit_compiler *compiler;
321      /* First byte code. */
322    pcre_uchar *start;    pcre_uchar *start;
   
323    /* Maps private data offset to each opcode. */    /* Maps private data offset to each opcode. */
324    int *private_data_ptrs;    sljit_si *private_data_ptrs;
325      /* This read-only data is available during runtime. */
326      sljit_uw *read_only_data;
327      /* The total size of the read-only data. */
328      sljit_uw read_only_data_size;
329      /* The next free entry of the read_only_data. */
330      sljit_uw *read_only_data_ptr;
331    /* Tells whether the capturing bracket is optimized. */    /* Tells whether the capturing bracket is optimized. */
332    pcre_uint8 *optimized_cbracket;    pcre_uint8 *optimized_cbracket;
333      /* Tells whether the starting offset is a target of then. */
334      pcre_uint8 *then_offsets;
335      /* Current position where a THEN must jump. */
336      then_trap_backtrack *then_trap;
337    /* Starting offset of private data for capturing brackets. */    /* Starting offset of private data for capturing brackets. */
338    int cbraptr;    int cbra_ptr;
339    /* OVector starting point. Must be divisible by 2. */    /* Output vector starting point. Must be divisible by 2. */
340    int ovector_start;    int ovector_start;
341    /* Last known position of the requested byte. */    /* Last known position of the requested byte. */
342    int req_char_ptr;    int req_char_ptr;
# Line 305  typedef struct compiler_common { Line 350  typedef struct compiler_common {
350    int first_line_end;    int first_line_end;
351    /* Points to the marked string. */    /* Points to the marked string. */
352    int mark_ptr;    int mark_ptr;
353      /* Recursive control verb management chain. */
354      int control_head_ptr;
355    /* Points to the last matched capture block index. */    /* Points to the last matched capture block index. */
356    int capture_last_ptr;    int capture_last_ptr;
357      /* Points to the starting position of the current match. */
358      int start_ptr;
359    
360    /* Flipped and lower case tables. */    /* Flipped and lower case tables. */
361    const pcre_uint8 *fcc;    const pcre_uint8 *fcc;
362    sljit_sw lcc;    sljit_sw lcc;
363    /* Mode can be PCRE_STUDY_JIT_COMPILE and others. */    /* Mode can be PCRE_STUDY_JIT_COMPILE and others. */
364    int mode;    int mode;
365      /* TRUE, when minlength is greater than 0. */
366      BOOL might_be_empty;
367      /* \K is found in the pattern. */
368      BOOL has_set_som;
369      /* (*SKIP:arg) is found in the pattern. */
370      BOOL has_skip_arg;
371      /* (*THEN) is found in the pattern. */
372      BOOL has_then;
373      /* Needs to know the start position anytime. */
374      BOOL needs_start_ptr;
375      /* Currently in recurse or negative assert. */
376      BOOL local_exit;
377      /* Currently in a positive assert. */
378      BOOL positive_assert;
379    /* Newline control. */    /* Newline control. */
380    int nltype;    int nltype;
381      pcre_uint32 nlmax;
382      pcre_uint32 nlmin;
383    int newline;    int newline;
384    int bsr_nltype;    int bsr_nltype;
385      pcre_uint32 bsr_nlmax;
386      pcre_uint32 bsr_nlmin;
387    /* Dollar endonly. */    /* Dollar endonly. */
388    int endonly;    int endonly;
   BOOL has_set_som;  
389    /* Tables. */    /* Tables. */
390    sljit_sw ctypes;    sljit_sw ctypes;
   int digits[2 + MAX_RANGE_SIZE];  
391    /* Named capturing brackets. */    /* Named capturing brackets. */
392    sljit_uw name_table;    pcre_uchar *name_table;
393    sljit_sw name_count;    sljit_sw name_count;
394    sljit_sw name_entry_size;    sljit_sw name_entry_size;
395    
# Line 334  typedef struct compiler_common { Line 399  typedef struct compiler_common {
399    struct sljit_label *forced_quit_label;    struct sljit_label *forced_quit_label;
400    struct sljit_label *accept_label;    struct sljit_label *accept_label;
401    stub_list *stubs;    stub_list *stubs;
402      label_addr_list *label_addrs;
403    recurse_entry *entries;    recurse_entry *entries;
404    recurse_entry *currententry;    recurse_entry *currententry;
405    jump_list *partialmatch;    jump_list *partialmatch;
406    jump_list *quit;    jump_list *quit;
407      jump_list *positive_assert_quit;
408    jump_list *forced_quit;    jump_list *forced_quit;
409    jump_list *accept;    jump_list *accept;
410    jump_list *calllimit;    jump_list *calllimit;
# Line 349  typedef struct compiler_common { Line 416  typedef struct compiler_common {
416    jump_list *vspace;    jump_list *vspace;
417    jump_list *casefulcmp;    jump_list *casefulcmp;
418    jump_list *caselesscmp;    jump_list *caselesscmp;
419      jump_list *reset_match;
420    BOOL jscript_compat;    BOOL jscript_compat;
421  #ifdef SUPPORT_UTF  #ifdef SUPPORT_UTF
422    BOOL utf;    BOOL utf;
423  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
424    BOOL use_ucp;    BOOL use_ucp;
425  #endif  #endif
 #ifndef COMPILE_PCRE32  
   jump_list *utfreadchar;  
 #endif  
426  #ifdef COMPILE_PCRE8  #ifdef COMPILE_PCRE8
427      jump_list *utfreadchar;
428      jump_list *utfreadchar16;
429    jump_list *utfreadtype8;    jump_list *utfreadtype8;
430  #endif  #endif
431  #endif /* SUPPORT_UTF */  #endif /* SUPPORT_UTF */
# Line 415  typedef struct compare_context { Line 482  typedef struct compare_context {
482  #define STACK_TOP     SLJIT_SCRATCH_REG2  #define STACK_TOP     SLJIT_SCRATCH_REG2
483  #define STACK_LIMIT   SLJIT_SAVED_REG3  #define STACK_LIMIT   SLJIT_SAVED_REG3
484  #define ARGUMENTS     SLJIT_SAVED_EREG1  #define ARGUMENTS     SLJIT_SAVED_EREG1
485  #define CALL_COUNT    SLJIT_SAVED_EREG2  #define COUNT_MATCH   SLJIT_SAVED_EREG2
486  #define RETURN_ADDR   SLJIT_TEMPORARY_EREG1  #define RETURN_ADDR   SLJIT_TEMPORARY_EREG1
487    
488  /* Local space layout. */  /* Local space layout. */
# Line 426  typedef struct compare_context { Line 493  typedef struct compare_context {
493  #define POSSESSIVE0      (2 * sizeof(sljit_sw))  #define POSSESSIVE0      (2 * sizeof(sljit_sw))
494  #define POSSESSIVE1      (3 * sizeof(sljit_sw))  #define POSSESSIVE1      (3 * sizeof(sljit_sw))
495  /* Max limit of recursions. */  /* Max limit of recursions. */
496  #define CALL_LIMIT       (4 * sizeof(sljit_sw))  #define LIMIT_MATCH      (4 * sizeof(sljit_sw))
497  /* The output vector is stored on the stack, and contains pointers  /* The output vector is stored on the stack, and contains pointers
498  to characters. The vector data is divided into two groups: the first  to characters. The vector data is divided into two groups: the first
499  group contains the start / end character pointers, and the second is  group contains the start / end character pointers, and the second is
500  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. */
501  #define OVECTOR_START    (common->ovector_start)  #define OVECTOR_START    (common->ovector_start)
502  #define OVECTOR(i)       (OVECTOR_START + (i) * sizeof(sljit_sw))  #define OVECTOR(i)       (OVECTOR_START + (i) * (sljit_sw)sizeof(sljit_sw))
503  #define OVECTOR_PRIV(i)  (common->cbraptr + (i) * sizeof(sljit_sw))  #define OVECTOR_PRIV(i)  (common->cbra_ptr + (i) * (sljit_sw)sizeof(sljit_sw))
504  #define PRIVATE_DATA(cc) (common->private_data_ptrs[(cc) - common->start])  #define PRIVATE_DATA(cc) (common->private_data_ptrs[(cc) - common->start])
505    
506  #if defined COMPILE_PCRE8  #if defined COMPILE_PCRE8
# Line 475  the start pointers when the end of the c Line 542  the start pointers when the end of the c
542  #define GET_LOCAL_BASE(dst, dstw, offset) \  #define GET_LOCAL_BASE(dst, dstw, offset) \
543    sljit_get_local_base(compiler, (dst), (dstw), (offset))    sljit_get_local_base(compiler, (dst), (dstw), (offset))
544    
545    #define READ_CHAR_MAX 0x7fffffff
546    
547  static pcre_uchar* bracketend(pcre_uchar* cc)  static pcre_uchar* bracketend(pcre_uchar* cc)
548  {  {
549  SLJIT_ASSERT((*cc >= OP_ASSERT && *cc <= OP_ASSERTBACK_NOT) || (*cc >= OP_ONCE && *cc <= OP_SCOND));  SLJIT_ASSERT((*cc >= OP_ASSERT && *cc <= OP_ASSERTBACK_NOT) || (*cc >= OP_ONCE && *cc <= OP_SCOND));
# Line 484  cc += 1 + LINK_SIZE; Line 553  cc += 1 + LINK_SIZE;
553  return cc;  return cc;
554  }  }
555    
556    static int no_alternatives(pcre_uchar* cc)
557    {
558    int count = 0;
559    SLJIT_ASSERT((*cc >= OP_ASSERT && *cc <= OP_ASSERTBACK_NOT) || (*cc >= OP_ONCE && *cc <= OP_SCOND));
560    do
561      {
562      cc += GET(cc, 1);
563      count++;
564      }
565    while (*cc == OP_ALT);
566    SLJIT_ASSERT(*cc >= OP_KET && *cc <= OP_KETRPOS);
567    return count;
568    }
569    
570    static int ones_in_half_byte[16] = {
571      /* 0 */ 0, 1, 1, 2, /* 4 */ 1, 2, 2, 3,
572      /* 8 */ 1, 2, 2, 3, /* 12 */ 2, 3, 3, 4
573    };
574    
575  /* Functions whose might need modification for all new supported opcodes:  /* Functions whose might need modification for all new supported opcodes:
576   next_opcode   next_opcode
577   get_private_data_length   check_opcode_types
578   set_private_data_ptrs   set_private_data_ptrs
579   get_framesize   get_framesize
580   init_frame   init_frame
581   get_private_data_length_for_copy   get_private_data_copy_length
582   copy_private_data   copy_private_data
583   compile_matchingpath   compile_matchingpath
584   compile_backtrackingpath   compile_backtrackingpath
# Line 536  switch(*cc) Line 624  switch(*cc)
624    case OP_CRMINQUERY:    case OP_CRMINQUERY:
625    case OP_CRRANGE:    case OP_CRRANGE:
626    case OP_CRMINRANGE:    case OP_CRMINRANGE:
627      case OP_CRPOSSTAR:
628      case OP_CRPOSPLUS:
629      case OP_CRPOSQUERY:
630      case OP_CRPOSRANGE:
631    case OP_CLASS:    case OP_CLASS:
632    case OP_NCLASS:    case OP_NCLASS:
633    case OP_REF:    case OP_REF:
634    case OP_REFI:    case OP_REFI:
635      case OP_DNREF:
636      case OP_DNREFI:
637    case OP_RECURSE:    case OP_RECURSE:
638    case OP_CALLOUT:    case OP_CALLOUT:
639    case OP_ALT:    case OP_ALT:
# Line 565  switch(*cc) Line 659  switch(*cc)
659    case OP_SCBRAPOS:    case OP_SCBRAPOS:
660    case OP_SCOND:    case OP_SCOND:
661    case OP_CREF:    case OP_CREF:
662    case OP_NCREF:    case OP_DNCREF:
663    case OP_RREF:    case OP_RREF:
664    case OP_NRREF:    case OP_DNRREF:
665    case OP_DEF:    case OP_DEF:
666    case OP_BRAZERO:    case OP_BRAZERO:
667    case OP_BRAMINZERO:    case OP_BRAMINZERO:
668    case OP_BRAPOSZERO:    case OP_BRAPOSZERO:
669      case OP_PRUNE:
670      case OP_SKIP:
671      case OP_THEN:
672    case OP_COMMIT:    case OP_COMMIT:
673    case OP_FAIL:    case OP_FAIL:
674    case OP_ACCEPT:    case OP_ACCEPT:
# Line 670  switch(*cc) Line 767  switch(*cc)
767  #endif  #endif
768    
769    case OP_MARK:    case OP_MARK:
770      case OP_PRUNE_ARG:
771      case OP_SKIP_ARG:
772      case OP_THEN_ARG:
773    return cc + 1 + 2 + cc[1];    return cc + 1 + 2 + cc[1];
774    
775    default:    default:
776      /* All opcodes are supported now! */
777      SLJIT_ASSERT_STOP();
778    return NULL;    return NULL;
779    }    }
780  }  }
781    
782    static BOOL check_opcode_types(compiler_common *common, pcre_uchar *cc, pcre_uchar *ccend)
783    {
784    int count;
785    pcre_uchar *slot;
786    
787    /* Calculate important variables (like stack size) and checks whether all opcodes are supported. */
788    while (cc < ccend)
789      {
790      switch(*cc)
791        {
792        case OP_SET_SOM:
793        common->has_set_som = TRUE;
794        common->might_be_empty = TRUE;
795        cc += 1;
796        break;
797    
798        case OP_REF:
799        case OP_REFI:
800        common->optimized_cbracket[GET2(cc, 1)] = 0;
801        cc += 1 + IMM2_SIZE;
802        break;
803    
804        case OP_BRA:
805        case OP_CBRA:
806        case OP_SBRA:
807        case OP_SCBRA:
808        count = no_alternatives(cc);
809        if (count > 4)
810          common->read_only_data_size += count * sizeof(sljit_uw);
811        cc += 1 + LINK_SIZE + (*cc == OP_CBRA || *cc == OP_SCBRA ? IMM2_SIZE : 0);
812        break;
813    
814        case OP_CBRAPOS:
815        case OP_SCBRAPOS:
816        common->optimized_cbracket[GET2(cc, 1 + LINK_SIZE)] = 0;
817        cc += 1 + LINK_SIZE + IMM2_SIZE;
818        break;
819    
820        case OP_COND:
821        case OP_SCOND:
822        /* Only AUTO_CALLOUT can insert this opcode. We do
823           not intend to support this case. */
824        if (cc[1 + LINK_SIZE] == OP_CALLOUT)
825          return FALSE;
826        cc += 1 + LINK_SIZE;
827        break;
828    
829        case OP_CREF:
830        common->optimized_cbracket[GET2(cc, 1)] = 0;
831        cc += 1 + IMM2_SIZE;
832        break;
833    
834        case OP_DNREF:
835        case OP_DNREFI:
836        case OP_DNCREF:
837        count = GET2(cc, 1 + IMM2_SIZE);
838        slot = common->name_table + GET2(cc, 1) * common->name_entry_size;
839        while (count-- > 0)
840          {
841          common->optimized_cbracket[GET2(slot, 0)] = 0;
842          slot += common->name_entry_size;
843          }
844        cc += 1 + 2 * IMM2_SIZE;
845        break;
846    
847        case OP_RECURSE:
848        /* Set its value only once. */
849        if (common->recursive_head_ptr == 0)
850          {
851          common->recursive_head_ptr = common->ovector_start;
852          common->ovector_start += sizeof(sljit_sw);
853          }
854        cc += 1 + LINK_SIZE;
855        break;
856    
857        case OP_CALLOUT:
858        if (common->capture_last_ptr == 0)
859          {
860          common->capture_last_ptr = common->ovector_start;
861          common->ovector_start += sizeof(sljit_sw);
862          }
863        cc += 2 + 2 * LINK_SIZE;
864        break;
865    
866        case OP_THEN_ARG:
867        common->has_then = TRUE;
868        common->control_head_ptr = 1;
869        /* Fall through. */
870    
871        case OP_PRUNE_ARG:
872        common->needs_start_ptr = TRUE;
873        /* Fall through. */
874    
875        case OP_MARK:
876        if (common->mark_ptr == 0)
877          {
878          common->mark_ptr = common->ovector_start;
879          common->ovector_start += sizeof(sljit_sw);
880          }
881        cc += 1 + 2 + cc[1];
882        break;
883    
884        case OP_THEN:
885        common->has_then = TRUE;
886        common->control_head_ptr = 1;
887        /* Fall through. */
888    
889        case OP_PRUNE:
890        case OP_SKIP:
891        common->needs_start_ptr = TRUE;
892        cc += 1;
893        break;
894    
895        case OP_SKIP_ARG:
896        common->control_head_ptr = 1;
897        common->has_skip_arg = TRUE;
898        cc += 1 + 2 + cc[1];
899        break;
900    
901        default:
902        cc = next_opcode(common, cc);
903        if (cc == NULL)
904          return FALSE;
905        break;
906        }
907      }
908    return TRUE;
909    }
910    
911    static int get_class_iterator_size(pcre_uchar *cc)
912    {
913    switch(*cc)
914      {
915      case OP_CRSTAR:
916      case OP_CRPLUS:
917      return 2;
918    
919      case OP_CRMINSTAR:
920      case OP_CRMINPLUS:
921      case OP_CRQUERY:
922      case OP_CRMINQUERY:
923      return 1;
924    
925      case OP_CRRANGE:
926      case OP_CRMINRANGE:
927      if (GET2(cc, 1) == GET2(cc, 1 + IMM2_SIZE))
928        return 0;
929      return 2;
930    
931      default:
932      return 0;
933      }
934    }
935    
936    static BOOL detect_repeat(compiler_common *common, pcre_uchar *begin)
937    {
938    pcre_uchar *end = bracketend(begin);
939    pcre_uchar *next;
940    pcre_uchar *next_end;
941    pcre_uchar *max_end;
942    pcre_uchar type;
943    sljit_sw length = end - begin;
944    int min, max, i;
945    
946    /* Detect fixed iterations first. */
947    if (end[-(1 + LINK_SIZE)] != OP_KET)
948      return FALSE;
949    
950    /* Already detected repeat. */
951    if (common->private_data_ptrs[end - common->start - LINK_SIZE] != 0)
952      return TRUE;
953    
954    next = end;
955    min = 1;
956    while (1)
957      {
958      if (*next != *begin)
959        break;
960      next_end = bracketend(next);
961      if (next_end - next != length || memcmp(begin, next, IN_UCHARS(length)) != 0)
962        break;
963      next = next_end;
964      min++;
965      }
966    
967    if (min == 2)
968      return FALSE;
969    
970    max = 0;
971    max_end = next;
972    if (*next == OP_BRAZERO || *next == OP_BRAMINZERO)
973      {
974      type = *next;
975      while (1)
976        {
977        if (next[0] != type || next[1] != OP_BRA || next[2 + LINK_SIZE] != *begin)
978          break;
979        next_end = bracketend(next + 2 + LINK_SIZE);
980        if (next_end - next != (length + 2 + LINK_SIZE) || memcmp(begin, next + 2 + LINK_SIZE, IN_UCHARS(length)) != 0)
981          break;
982        next = next_end;
983        max++;
984        }
985    
986      if (next[0] == type && next[1] == *begin && max >= 1)
987        {
988        next_end = bracketend(next + 1);
989        if (next_end - next == (length + 1) && memcmp(begin, next + 1, IN_UCHARS(length)) == 0)
990          {
991          for (i = 0; i < max; i++, next_end += 1 + LINK_SIZE)
992            if (*next_end != OP_KET)
993              break;
994    
995          if (i == max)
996            {
997            common->private_data_ptrs[max_end - common->start - LINK_SIZE] = next_end - max_end;
998            common->private_data_ptrs[max_end - common->start - LINK_SIZE + 1] = (type == OP_BRAZERO) ? OP_UPTO : OP_MINUPTO;
999            /* +2 the original and the last. */
1000            common->private_data_ptrs[max_end - common->start - LINK_SIZE + 2] = max + 2;
1001            if (min == 1)
1002              return TRUE;
1003            min--;
1004            max_end -= (1 + LINK_SIZE) + GET(max_end, -LINK_SIZE);
1005            }
1006          }
1007        }
1008      }
1009    
1010    if (min >= 3)
1011      {
1012      common->private_data_ptrs[end - common->start - LINK_SIZE] = max_end - end;
1013      common->private_data_ptrs[end - common->start - LINK_SIZE + 1] = OP_EXACT;
1014      common->private_data_ptrs[end - common->start - LINK_SIZE + 2] = min;
1015      return TRUE;
1016      }
1017    
1018    return FALSE;
1019    }
1020    
1021  #define CASE_ITERATOR_PRIVATE_DATA_1 \  #define CASE_ITERATOR_PRIVATE_DATA_1 \
1022      case OP_MINSTAR: \      case OP_MINSTAR: \
1023      case OP_MINPLUS: \      case OP_MINPLUS: \
# Line 729  switch(*cc) Line 1070  switch(*cc)
1070      case OP_TYPEUPTO: \      case OP_TYPEUPTO: \
1071      case OP_TYPEMINUPTO:      case OP_TYPEMINUPTO:
1072    
1073  static int get_class_iterator_size(pcre_uchar *cc)  static void set_private_data_ptrs(compiler_common *common, int *private_data_start, pcre_uchar *ccend)
 {  
 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)  
1074  {  {
1075  int private_data_length = 0;  pcre_uchar *cc = common->start;
1076  pcre_uchar *alternative;  pcre_uchar *alternative;
 pcre_uchar *name;  
1077  pcre_uchar *end = NULL;  pcre_uchar *end = NULL;
1078  int space, size, i;  int private_data_ptr = *private_data_start;
1079  pcre_uint32 bracketlen;  int space, size, bracketlen;
1080    
 /* Calculate important variables (like stack size) and checks whether all opcodes are supported. */  
1081  while (cc < ccend)  while (cc < ccend)
1082    {    {
1083    space = 0;    space = 0;
1084    size = 0;    size = 0;
1085    bracketlen = 0;    bracketlen = 0;
1086      if (private_data_ptr > SLJIT_MAX_LOCAL_SIZE)
1087        return;
1088    
1089      if (*cc == OP_ONCE || *cc == OP_ONCE_NC || *cc == OP_BRA || *cc == OP_CBRA || *cc == OP_COND)
1090        if (detect_repeat(common, cc))
1091          {
1092          /* These brackets are converted to repeats, so no global
1093          based single character repeat is allowed. */
1094          if (cc >= end)
1095            end = bracketend(cc);
1096          }
1097    
1098    switch(*cc)    switch(*cc)
1099      {      {
1100      case OP_SET_SOM:      case OP_KET:
1101      common->has_set_som = TRUE;      if (common->private_data_ptrs[cc + 1 - common->start] != 0)
1102      cc += 1;        {
1103      break;        common->private_data_ptrs[cc - common->start] = private_data_ptr;
1104          private_data_ptr += sizeof(sljit_sw);
1105      case OP_REF:        cc += common->private_data_ptrs[cc + 1 - common->start];
1106      case OP_REFI:        }
1107      common->optimized_cbracket[GET2(cc, 1)] = 0;      cc += 1 + LINK_SIZE;
     cc += 1 + IMM2_SIZE;  
1108      break;      break;
1109    
1110      case OP_ASSERT:      case OP_ASSERT:
# Line 791  while (cc < ccend) Line 1116  while (cc < ccend)
1116      case OP_BRAPOS:      case OP_BRAPOS:
1117      case OP_SBRA:      case OP_SBRA:
1118      case OP_SBRAPOS:      case OP_SBRAPOS:
1119      private_data_length += sizeof(sljit_sw);      case OP_SCOND:
1120        common->private_data_ptrs[cc - common->start] = private_data_ptr;
1121        private_data_ptr += sizeof(sljit_sw);
1122      bracketlen = 1 + LINK_SIZE;      bracketlen = 1 + LINK_SIZE;
1123      break;      break;
1124    
1125      case OP_CBRAPOS:      case OP_CBRAPOS:
1126      case OP_SCBRAPOS:      case OP_SCBRAPOS:
1127      private_data_length += sizeof(sljit_sw);      common->private_data_ptrs[cc - common->start] = private_data_ptr;
1128      common->optimized_cbracket[GET2(cc, 1 + LINK_SIZE)] = 0;      private_data_ptr += sizeof(sljit_sw);
1129      bracketlen = 1 + LINK_SIZE + IMM2_SIZE;      bracketlen = 1 + LINK_SIZE + IMM2_SIZE;
1130      break;      break;
1131    
1132      case OP_COND:      case OP_COND:
1133      case OP_SCOND:      /* Might be a hidden SCOND. */
1134      /* Only AUTO_CALLOUT can insert this opcode. We do      alternative = cc + GET(cc, 1);
1135         not intend to support this case. */      if (*alternative == OP_KETRMAX || *alternative == OP_KETRMIN)
     if (cc[1 + LINK_SIZE] == OP_CALLOUT)  
       return -1;  
   
     if (*cc == OP_COND)  
1136        {        {
1137        /* Might be a hidden SCOND. */        common->private_data_ptrs[cc - common->start] = private_data_ptr;
1138        alternative = cc + GET(cc, 1);        private_data_ptr += sizeof(sljit_sw);
       if (*alternative == OP_KETRMAX || *alternative == OP_KETRMIN)  
         private_data_length += sizeof(sljit_sw);  
       }  
     else  
       private_data_length += sizeof(sljit_sw);  
     bracketlen = 1 + LINK_SIZE;  
     break;  
   
     case OP_CREF:  
     i = GET2(cc, 1);  
     common->optimized_cbracket[i] = 0;  
     cc += 1 + IMM2_SIZE;  
     break;  
   
     case OP_NCREF:  
     bracketlen = GET2(cc, 1);  
     name = (pcre_uchar *)common->name_table;  
     alternative = name;  
     for (i = 0; i < common->name_count; i++)  
       {  
       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++)  
       {  
       if (STRCMP_UC_UC(alternative + IMM2_SIZE, name + IMM2_SIZE) == 0)  
         common->optimized_cbracket[GET2(alternative, 0)] = 0;  
       alternative += common->name_entry_size;  
       }  
     bracketlen = 0;  
     cc += 1 + IMM2_SIZE;  
     break;  
   
     case OP_BRA:  
     bracketlen = 1 + LINK_SIZE;  
     break;  
   
     case OP_CBRA:  
     case OP_SCBRA:  
     bracketlen = 1 + LINK_SIZE + IMM2_SIZE;  
     break;  
   
     CASE_ITERATOR_PRIVATE_DATA_1  
     space = 1;  
     size = -2;  
     break;  
   
     CASE_ITERATOR_PRIVATE_DATA_2A  
     space = 2;  
     size = -2;  
     break;  
   
     CASE_ITERATOR_PRIVATE_DATA_2B  
     space = 2;  
     size = -(2 + IMM2_SIZE);  
     break;  
   
     CASE_ITERATOR_TYPE_PRIVATE_DATA_1  
     space = 1;  
     size = 1;  
     break;  
   
     CASE_ITERATOR_TYPE_PRIVATE_DATA_2A  
     if (cc[1] != OP_ANYNL && cc[1] != OP_EXTUNI)  
       space = 2;  
     size = 1;  
     break;  
   
     CASE_ITERATOR_TYPE_PRIVATE_DATA_2B  
     if (cc[1 + IMM2_SIZE] != OP_ANYNL && cc[1 + IMM2_SIZE] != OP_EXTUNI)  
       space = 2;  
     size = 1 + IMM2_SIZE;  
     break;  
   
     case OP_CLASS:  
     case OP_NCLASS:  
     size += 1 + 32 / sizeof(pcre_uchar);  
     space = get_class_iterator_size(cc + size);  
     break;  
   
 #if defined SUPPORT_UTF || !defined COMPILE_PCRE8  
     case OP_XCLASS:  
     size = GET(cc, 1);  
     space = get_class_iterator_size(cc + size);  
     break;  
 #endif  
   
     case OP_RECURSE:  
     /* Set its value only once. */  
     if (common->recursive_head_ptr == 0)  
       {  
       common->recursive_head_ptr = common->ovector_start;  
       common->ovector_start += sizeof(sljit_sw);  
       }  
     cc += 1 + LINK_SIZE;  
     break;  
   
     case OP_CALLOUT:  
     if (common->capture_last_ptr == 0)  
       {  
       common->capture_last_ptr = common->ovector_start;  
       common->ovector_start += sizeof(sljit_sw);  
       }  
     cc += 2 + 2 * LINK_SIZE;  
     break;  
   
     case OP_MARK:  
     if (common->mark_ptr == 0)  
       {  
       common->mark_ptr = common->ovector_start;  
       common->ovector_start += sizeof(sljit_sw);  
       }  
     cc += 1 + 2 + cc[1];  
     break;  
   
     default:  
     cc = next_opcode(common, cc);  
     if (cc == NULL)  
       return -1;  
     break;  
     }  
   
   if (space > 0 && cc >= end)  
     private_data_length += sizeof(sljit_sw) * space;  
   
   if (size != 0)  
     {  
     if (size < 0)  
       {  
       cc += -size;  
 #ifdef SUPPORT_UTF  
       if (common->utf && HAS_EXTRALEN(cc[-1])) cc += GET_EXTRALEN(cc[-1]);  
 #endif  
       }  
     else  
       cc += size;  
     }  
   
   if (bracketlen != 0)  
     {  
     if (cc >= end)  
       {  
       end = bracketend(cc);  
       if (end[-1 - LINK_SIZE] == OP_KET)  
         end = NULL;  
       }  
     cc += bracketlen;  
     }  
   }  
 return private_data_length;  
 }  
   
 static void set_private_data_ptrs(compiler_common *common, int private_data_ptr, pcre_uchar *ccend)  
 {  
 pcre_uchar *cc = common->start;  
 pcre_uchar *alternative;  
 pcre_uchar *end = NULL;  
 int space, size, bracketlen;  
   
 while (cc < ccend)  
   {  
   space = 0;  
   size = 0;  
   bracketlen = 0;  
   switch(*cc)  
     {  
     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:  
     case OP_SCOND:  
     common->private_data_ptrs[cc - common->start] = private_data_ptr;  
     private_data_ptr += sizeof(sljit_sw);  
     bracketlen = 1 + LINK_SIZE;  
     break;  
   
     case OP_CBRAPOS:  
     case OP_SCBRAPOS:  
     common->private_data_ptrs[cc - common->start] = private_data_ptr;  
     private_data_ptr += sizeof(sljit_sw);  
     bracketlen = 1 + LINK_SIZE + IMM2_SIZE;  
     break;  
   
     case OP_COND:  
     /* Might be a hidden SCOND. */  
     alternative = cc + GET(cc, 1);  
     if (*alternative == OP_KETRMAX || *alternative == OP_KETRMIN)  
       {  
       common->private_data_ptrs[cc - common->start] = private_data_ptr;  
       private_data_ptr += sizeof(sljit_sw);  
1139        }        }
1140      bracketlen = 1 + LINK_SIZE;      bracketlen = 1 + LINK_SIZE;
1141      break;      break;
# Line 1074  while (cc < ccend) Line 1200  while (cc < ccend)
1200      break;      break;
1201      }      }
1202    
1203      /* Character iterators, which are not inside a repeated bracket,
1204         gets a private slot instead of allocating it on the stack. */
1205    if (space > 0 && cc >= end)    if (space > 0 && cc >= end)
1206      {      {
1207      common->private_data_ptrs[cc - common->start] = private_data_ptr;      common->private_data_ptrs[cc - common->start] = private_data_ptr;
# Line 1104  while (cc < ccend) Line 1232  while (cc < ccend)
1232      cc += bracketlen;      cc += bracketlen;
1233      }      }
1234    }    }
1235    *private_data_start = private_data_ptr;
1236  }  }
1237    
1238  /* Returns with a frame_types (always < 0) if no need for frame. */  /* Returns with a frame_types (always < 0) if no need for frame. */
1239  static int get_framesize(compiler_common *common, pcre_uchar *cc, BOOL recursive)  static int get_framesize(compiler_common *common, pcre_uchar *cc, pcre_uchar *ccend, BOOL recursive, BOOL* needs_control_head)
1240  {  {
 pcre_uchar *ccend = bracketend(cc) - (1 + LINK_SIZE);  
1241  int length = 0;  int length = 0;
1242  int possessive = 0;  int possessive = 0;
1243  BOOL stack_restore = FALSE;  BOOL stack_restore = FALSE;
# Line 1118  BOOL setmark_found = recursive; Line 1246  BOOL setmark_found = recursive;
1246  /* The last capture is a local variable even for recursions. */  /* The last capture is a local variable even for recursions. */
1247  BOOL capture_last_found = FALSE;  BOOL capture_last_found = FALSE;
1248    
1249  if (!recursive && (*cc == OP_CBRAPOS || *cc == OP_SCBRAPOS))  #if defined DEBUG_FORCE_CONTROL_HEAD && DEBUG_FORCE_CONTROL_HEAD
1250    SLJIT_ASSERT(common->control_head_ptr != 0);
1251    *needs_control_head = TRUE;
1252    #else
1253    *needs_control_head = FALSE;
1254    #endif
1255    
1256    if (ccend == NULL)
1257    {    {
1258    possessive = length = (common->capture_last_ptr != 0) ? 5 : 3;    ccend = bracketend(cc) - (1 + LINK_SIZE);
1259    /* This is correct regardless of common->capture_last_ptr. */    if (!recursive && (*cc == OP_CBRAPOS || *cc == OP_SCBRAPOS))
1260    capture_last_found = TRUE;      {
1261        possessive = length = (common->capture_last_ptr != 0) ? 5 : 3;
1262        /* This is correct regardless of common->capture_last_ptr. */
1263        capture_last_found = TRUE;
1264        }
1265      cc = next_opcode(common, cc);
1266    }    }
1267    
 cc = next_opcode(common, cc);  
1268  SLJIT_ASSERT(cc != NULL);  SLJIT_ASSERT(cc != NULL);
1269  while (cc < ccend)  while (cc < ccend)
1270    switch(*cc)    switch(*cc)
# Line 1142  while (cc < ccend) Line 1281  while (cc < ccend)
1281      break;      break;
1282    
1283      case OP_MARK:      case OP_MARK:
1284        case OP_PRUNE_ARG:
1285        case OP_THEN_ARG:
1286      SLJIT_ASSERT(common->mark_ptr != 0);      SLJIT_ASSERT(common->mark_ptr != 0);
1287      stack_restore = TRUE;      stack_restore = TRUE;
1288      if (!setmark_found)      if (!setmark_found)
# Line 1149  while (cc < ccend) Line 1290  while (cc < ccend)
1290        length += 2;        length += 2;
1291        setmark_found = TRUE;        setmark_found = TRUE;
1292        }        }
1293        if (common->control_head_ptr != 0)
1294          *needs_control_head = TRUE;
1295      cc += 1 + 2 + cc[1];      cc += 1 + 2 + cc[1];
1296      break;      break;
1297    
# Line 1268  if (length > 0) Line 1411  if (length > 0)
1411  return stack_restore ? no_frame : no_stack;  return stack_restore ? no_frame : no_stack;
1412  }  }
1413    
1414  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)
1415  {  {
1416  DEFINE_COMPILER;  DEFINE_COMPILER;
 pcre_uchar *ccend = bracketend(cc) - (1 + LINK_SIZE);  
1417  BOOL setsom_found = recursive;  BOOL setsom_found = recursive;
1418  BOOL setmark_found = recursive;  BOOL setmark_found = recursive;
1419  /* The last capture is a local variable even for recursions. */  /* The last capture is a local variable even for recursions. */
# Line 1283  SLJIT_UNUSED_ARG(stacktop); Line 1425  SLJIT_UNUSED_ARG(stacktop);
1425  SLJIT_ASSERT(stackpos >= stacktop + 2);  SLJIT_ASSERT(stackpos >= stacktop + 2);
1426    
1427  stackpos = STACK(stackpos);  stackpos = STACK(stackpos);
1428  if (recursive || (*cc != OP_CBRAPOS && *cc != OP_SCBRAPOS))  if (ccend == NULL)
1429    cc = next_opcode(common, cc);    {
1430      ccend = bracketend(cc) - (1 + LINK_SIZE);
1431      if (recursive || (*cc != OP_CBRAPOS && *cc != OP_SCBRAPOS))
1432        cc = next_opcode(common, cc);
1433      }
1434    
1435  SLJIT_ASSERT(cc != NULL);  SLJIT_ASSERT(cc != NULL);
1436  while (cc < ccend)  while (cc < ccend)
1437    switch(*cc)    switch(*cc)
# Line 1304  while (cc < ccend) Line 1451  while (cc < ccend)
1451      break;      break;
1452    
1453      case OP_MARK:      case OP_MARK:
1454        case OP_PRUNE_ARG:
1455        case OP_THEN_ARG:
1456      SLJIT_ASSERT(common->mark_ptr != 0);      SLJIT_ASSERT(common->mark_ptr != 0);
1457      if (!setmark_found)      if (!setmark_found)
1458        {        {
# Line 1384  OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), st Line 1533  OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), st
1533  SLJIT_ASSERT(stackpos == STACK(stacktop));  SLJIT_ASSERT(stackpos == STACK(stacktop));
1534  }  }
1535    
1536  static SLJIT_INLINE int get_private_data_length_for_copy(compiler_common *common, pcre_uchar *cc, pcre_uchar *ccend)  static SLJIT_INLINE int get_private_data_copy_length(compiler_common *common, pcre_uchar *cc, pcre_uchar *ccend, BOOL needs_control_head)
1537  {  {
1538  int private_data_length = 2;  int private_data_length = needs_control_head ? 3 : 2;
1539  int size;  int size;
1540  pcre_uchar *alternative;  pcre_uchar *alternative;
1541  /* Calculate the sum of the private machine words. */  /* Calculate the sum of the private machine words. */
# Line 1395  while (cc < ccend) Line 1544  while (cc < ccend)
1544    size = 0;    size = 0;
1545    switch(*cc)    switch(*cc)
1546      {      {
1547        case OP_KET:
1548        if (PRIVATE_DATA(cc) != 0)
1549          private_data_length++;
1550        cc += 1 + LINK_SIZE;
1551        break;
1552    
1553      case OP_ASSERT:      case OP_ASSERT:
1554      case OP_ASSERT_NOT:      case OP_ASSERT_NOT:
1555      case OP_ASSERTBACK:      case OP_ASSERTBACK:
# Line 1499  return private_data_length; Line 1654  return private_data_length;
1654  }  }
1655    
1656  static void copy_private_data(compiler_common *common, pcre_uchar *cc, pcre_uchar *ccend,  static void copy_private_data(compiler_common *common, pcre_uchar *cc, pcre_uchar *ccend,
1657    BOOL save, int stackptr, int stacktop)    BOOL save, int stackptr, int stacktop, BOOL needs_control_head)
1658  {  {
1659  DEFINE_COMPILER;  DEFINE_COMPILER;
1660  int srcw[2];  int srcw[2];
# Line 1520  stacktop = STACK(stacktop - 1); Line 1675  stacktop = STACK(stacktop - 1);
1675    
1676  if (!save)  if (!save)
1677    {    {
1678    stackptr += sizeof(sljit_sw);    stackptr += (needs_control_head ? 2 : 1) * sizeof(sljit_sw);
1679    if (stackptr < stacktop)    if (stackptr < stacktop)
1680      {      {
1681      OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(STACK_TOP), stackptr);      OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(STACK_TOP), stackptr);
# Line 1536  if (!save) Line 1691  if (!save)
1691    /* The tmp1next must be TRUE in either way. */    /* The tmp1next must be TRUE in either way. */
1692    }    }
1693    
1694  while (status != end)  do
1695    {    {
1696    count = 0;    count = 0;
1697    switch(status)    switch(status)
# Line 1545  while (status != end) Line 1700  while (status != end)
1700      SLJIT_ASSERT(save && common->recursive_head_ptr != 0);      SLJIT_ASSERT(save && common->recursive_head_ptr != 0);
1701      count = 1;      count = 1;
1702      srcw[0] = common->recursive_head_ptr;      srcw[0] = common->recursive_head_ptr;
1703        if (needs_control_head)
1704          {
1705          SLJIT_ASSERT(common->control_head_ptr != 0);
1706          count = 2;
1707          srcw[1] = common->control_head_ptr;
1708          }
1709      status = loop;      status = loop;
1710      break;      break;
1711    
# Line 1557  while (status != end) Line 1718  while (status != end)
1718    
1719      switch(*cc)      switch(*cc)
1720        {        {
1721          case OP_KET:
1722          if (PRIVATE_DATA(cc) != 0)
1723            {
1724            count = 1;
1725            srcw[0] = PRIVATE_DATA(cc);
1726            }
1727          cc += 1 + LINK_SIZE;
1728          break;
1729    
1730        case OP_ASSERT:        case OP_ASSERT:
1731        case OP_ASSERT_NOT:        case OP_ASSERT_NOT:
1732        case OP_ASSERTBACK:        case OP_ASSERTBACK:
# Line 1769  while (status != end) Line 1939  while (status != end)
1939        }        }
1940      }      }
1941    }    }
1942    while (status != end);
1943    
1944  if (save)  if (save)
1945    {    {
# Line 1802  if (save) Line 1973  if (save)
1973  SLJIT_ASSERT(cc == ccend && stackptr == stacktop && (save || (tmp1empty && tmp2empty)));  SLJIT_ASSERT(cc == ccend && stackptr == stacktop && (save || (tmp1empty && tmp2empty)));
1974  }  }
1975    
1976    static SLJIT_INLINE pcre_uchar *set_then_offsets(compiler_common *common, pcre_uchar *cc, pcre_uint8 *current_offset)
1977    {
1978    pcre_uchar *end = bracketend(cc);
1979    BOOL has_alternatives = cc[GET(cc, 1)] == OP_ALT;
1980    
1981    /* Assert captures then. */
1982    if (*cc >= OP_ASSERT && *cc <= OP_ASSERTBACK_NOT)
1983      current_offset = NULL;
1984    /* Conditional block does not. */
1985    if (*cc == OP_COND || *cc == OP_SCOND)
1986      has_alternatives = FALSE;
1987    
1988    cc = next_opcode(common, cc);
1989    if (has_alternatives)
1990      current_offset = common->then_offsets + (cc - common->start);
1991    
1992    while (cc < end)
1993      {
1994      if ((*cc >= OP_ASSERT && *cc <= OP_ASSERTBACK_NOT) || (*cc >= OP_ONCE && *cc <= OP_SCOND))
1995        cc = set_then_offsets(common, cc, current_offset);
1996      else
1997        {
1998        if (*cc == OP_ALT && has_alternatives)
1999          current_offset = common->then_offsets + (cc + 1 + LINK_SIZE - common->start);
2000        if (*cc >= OP_THEN && *cc <= OP_THEN_ARG && current_offset != NULL)
2001          *current_offset = 1;
2002        cc = next_opcode(common, cc);
2003        }
2004      }
2005    
2006    return end;
2007    }
2008    
2009  #undef CASE_ITERATOR_PRIVATE_DATA_1  #undef CASE_ITERATOR_PRIVATE_DATA_1
2010  #undef CASE_ITERATOR_PRIVATE_DATA_2A  #undef CASE_ITERATOR_PRIVATE_DATA_2A
2011  #undef CASE_ITERATOR_PRIVATE_DATA_2B  #undef CASE_ITERATOR_PRIVATE_DATA_2B
# Line 1865  while (list_item) Line 2069  while (list_item)
2069  common->stubs = NULL;  common->stubs = NULL;
2070  }  }
2071    
2072  static SLJIT_INLINE void decrease_call_count(compiler_common *common)  static void add_label_addr(compiler_common *common)
2073  {  {
2074  DEFINE_COMPILER;  DEFINE_COMPILER;
2075    label_addr_list *label_addr;
2076    
2077  OP2(SLJIT_SUB | SLJIT_SET_E, CALL_COUNT, 0, CALL_COUNT, 0, SLJIT_IMM, 1);  label_addr = sljit_alloc_memory(compiler, sizeof(label_addr_list));
2078    if (label_addr == NULL)
2079      return;
2080    label_addr->label = LABEL();
2081    label_addr->addr = common->read_only_data_ptr;
2082    label_addr->next = common->label_addrs;
2083    common->label_addrs = label_addr;
2084    common->read_only_data_ptr++;
2085    }
2086    
2087    static SLJIT_INLINE void count_match(compiler_common *common)
2088    {
2089    DEFINE_COMPILER;
2090    
2091    OP2(SLJIT_SUB | SLJIT_SET_E, COUNT_MATCH, 0, COUNT_MATCH, 0, SLJIT_IMM, 1);
2092  add_jump(compiler, &common->calllimit, JUMP(SLJIT_C_ZERO));  add_jump(compiler, &common->calllimit, JUMP(SLJIT_C_ZERO));
2093  }  }
2094    
# Line 1900  static SLJIT_INLINE void reset_ovector(c Line 2119  static SLJIT_INLINE void reset_ovector(c
2119  DEFINE_COMPILER;  DEFINE_COMPILER;
2120  struct sljit_label *loop;  struct sljit_label *loop;
2121  int i;  int i;
2122    
2123  /* At this point we can freely use all temporary registers. */  /* At this point we can freely use all temporary registers. */
2124    SLJIT_ASSERT(length > 1);
2125  /* TMP1 returns with begin - 1. */  /* TMP1 returns with begin - 1. */
2126  OP2(SLJIT_SUB, SLJIT_SCRATCH_REG1, 0, SLJIT_MEM1(SLJIT_SAVED_REG1), SLJIT_OFFSETOF(jit_arguments, begin), SLJIT_IMM, IN_UCHARS(1));  OP2(SLJIT_SUB, SLJIT_SCRATCH_REG1, 0, SLJIT_MEM1(SLJIT_SAVED_REG1), SLJIT_OFFSETOF(jit_arguments, begin), SLJIT_IMM, IN_UCHARS(1));
2127  if (length < 8)  if (length < 8)
2128    {    {
2129    for (i = 0; i < length; i++)    for (i = 1; i < length; i++)
2130      OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(i), SLJIT_SCRATCH_REG1, 0);      OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(i), SLJIT_SCRATCH_REG1, 0);
2131    }    }
2132  else  else
2133    {    {
2134    GET_LOCAL_BASE(SLJIT_SCRATCH_REG2, 0, OVECTOR_START - sizeof(sljit_sw));    GET_LOCAL_BASE(SLJIT_SCRATCH_REG2, 0, OVECTOR_START);
2135    OP1(SLJIT_MOV, SLJIT_SCRATCH_REG3, 0, SLJIT_IMM, length);    OP1(SLJIT_MOV, SLJIT_SCRATCH_REG3, 0, SLJIT_IMM, length - 1);
2136    loop = LABEL();    loop = LABEL();
2137    OP1(SLJIT_MOVU, SLJIT_MEM1(SLJIT_SCRATCH_REG2), sizeof(sljit_sw), SLJIT_SCRATCH_REG1, 0);    OP1(SLJIT_MOVU, SLJIT_MEM1(SLJIT_SCRATCH_REG2), sizeof(sljit_sw), SLJIT_SCRATCH_REG1, 0);
2138    OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_SCRATCH_REG3, 0, SLJIT_SCRATCH_REG3, 0, SLJIT_IMM, 1);    OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_SCRATCH_REG3, 0, SLJIT_SCRATCH_REG3, 0, SLJIT_IMM, 1);
# Line 1919  else Line 2140  else
2140    }    }
2141  }  }
2142    
2143    static SLJIT_INLINE void do_reset_match(compiler_common *common, int length)
2144    {
2145    DEFINE_COMPILER;
2146    struct sljit_label *loop;
2147    int i;
2148    
2149    SLJIT_ASSERT(length > 1);
2150    /* OVECTOR(1) contains the "string begin - 1" constant. */
2151    if (length > 2)
2152      OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(1));
2153    if (length < 8)
2154      {
2155      for (i = 2; i < length; i++)
2156        OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(i), TMP1, 0);
2157      }
2158    else
2159      {
2160      GET_LOCAL_BASE(TMP2, 0, OVECTOR_START + sizeof(sljit_sw));
2161      OP1(SLJIT_MOV, STACK_TOP, 0, SLJIT_IMM, length - 2);
2162      loop = LABEL();
2163      OP1(SLJIT_MOVU, SLJIT_MEM1(TMP2), sizeof(sljit_sw), TMP1, 0);
2164      OP2(SLJIT_SUB | SLJIT_SET_E, STACK_TOP, 0, STACK_TOP, 0, SLJIT_IMM, 1);
2165      JUMPTO(SLJIT_C_NOT_ZERO, loop);
2166      }
2167    
2168    OP1(SLJIT_MOV, STACK_TOP, 0, ARGUMENTS, 0);
2169    if (common->mark_ptr != 0)
2170      OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), common->mark_ptr, SLJIT_IMM, 0);
2171    if (common->control_head_ptr != 0)
2172      OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), common->control_head_ptr, SLJIT_IMM, 0);
2173    OP1(SLJIT_MOV, STACK_TOP, 0, SLJIT_MEM1(STACK_TOP), SLJIT_OFFSETOF(jit_arguments, stack));
2174    OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), common->start_ptr);
2175    OP1(SLJIT_MOV, STACK_TOP, 0, SLJIT_MEM1(STACK_TOP), SLJIT_OFFSETOF(struct sljit_stack, base));
2176    }
2177    
2178    static sljit_sw SLJIT_CALL do_search_mark(sljit_sw *current, const pcre_uchar *skip_arg)
2179    {
2180    while (current != NULL)
2181      {
2182      switch (current[-2])
2183        {
2184        case type_then_trap:
2185        break;
2186    
2187        case type_mark:
2188        if (STRCMP_UC_UC(skip_arg, (pcre_uchar *)current[-3]) == 0)
2189          return current[-4];
2190        break;
2191    
2192        default:
2193        SLJIT_ASSERT_STOP();
2194        break;
2195        }
2196      current = (sljit_sw*)current[-1];
2197      }
2198    return -1;
2199    }
2200    
2201  static SLJIT_INLINE void copy_ovector(compiler_common *common, int topbracket)  static SLJIT_INLINE void copy_ovector(compiler_common *common, int topbracket)
2202  {  {
2203  DEFINE_COMPILER;  DEFINE_COMPILER;
# Line 1975  DEFINE_COMPILER; Line 2254  DEFINE_COMPILER;
2254  struct sljit_jump *jump;  struct sljit_jump *jump;
2255    
2256  SLJIT_COMPILE_ASSERT(STR_END == SLJIT_SAVED_REG2, str_end_must_be_saved_reg2);  SLJIT_COMPILE_ASSERT(STR_END == SLJIT_SAVED_REG2, str_end_must_be_saved_reg2);
2257  SLJIT_ASSERT(common->start_used_ptr != 0 && (common->mode == JIT_PARTIAL_SOFT_COMPILE ? common->hit_start != 0 : common->hit_start == 0));  SLJIT_ASSERT(common->start_used_ptr != 0 && common->start_ptr != 0
2258      && (common->mode == JIT_PARTIAL_SOFT_COMPILE ? common->hit_start != 0 : common->hit_start == 0));
2259    
2260  OP1(SLJIT_MOV, SLJIT_SCRATCH_REG2, 0, ARGUMENTS, 0);  OP1(SLJIT_MOV, SLJIT_SCRATCH_REG2, 0, ARGUMENTS, 0);
2261  OP1(SLJIT_MOV, SLJIT_RETURN_REG, 0, SLJIT_IMM, PCRE_ERROR_PARTIAL);  OP1(SLJIT_MOV, SLJIT_RETURN_REG, 0, SLJIT_IMM, PCRE_ERROR_PARTIAL);
# Line 1987  OP1(SLJIT_MOV, SLJIT_SAVED_REG1, 0, SLJI Line 2267  OP1(SLJIT_MOV, SLJIT_SAVED_REG1, 0, SLJI
2267  OP1(SLJIT_MOV, SLJIT_SCRATCH_REG2, 0, SLJIT_MEM1(SLJIT_SCRATCH_REG2), SLJIT_OFFSETOF(jit_arguments, offsets));  OP1(SLJIT_MOV, SLJIT_SCRATCH_REG2, 0, SLJIT_MEM1(SLJIT_SCRATCH_REG2), SLJIT_OFFSETOF(jit_arguments, offsets));
2268    
2269  jump = CMP(SLJIT_C_SIG_LESS, SLJIT_SCRATCH_REG3, 0, SLJIT_IMM, 3);  jump = CMP(SLJIT_C_SIG_LESS, SLJIT_SCRATCH_REG3, 0, SLJIT_IMM, 3);
2270  OP2(SLJIT_SUB, SLJIT_SCRATCH_REG3, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), common->start_used_ptr + sizeof(sljit_sw), SLJIT_SAVED_REG1, 0);  OP2(SLJIT_SUB, SLJIT_SCRATCH_REG3, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), common->mode == JIT_PARTIAL_HARD_COMPILE ? common->start_ptr : (common->hit_start + (int)sizeof(sljit_sw)), SLJIT_SAVED_REG1, 0);
2271  #if defined COMPILE_PCRE16 || defined COMPILE_PCRE32  #if defined COMPILE_PCRE16 || defined COMPILE_PCRE32
2272  OP2(SLJIT_ASHR, SLJIT_SCRATCH_REG3, 0, SLJIT_SCRATCH_REG3, 0, SLJIT_IMM, UCHAR_SHIFT);  OP2(SLJIT_ASHR, SLJIT_SCRATCH_REG3, 0, SLJIT_SCRATCH_REG3, 0, SLJIT_IMM, UCHAR_SHIFT);
2273  #endif  #endif
# Line 2155  return (bit < 256) ? ((0 << 8) | bit) : Line 2435  return (bit < 256) ? ((0 << 8) | bit) :
2435    
2436  static void check_partial(compiler_common *common, BOOL force)  static void check_partial(compiler_common *common, BOOL force)
2437  {  {
2438  /* Checks whether a partial matching is occured. Does not modify registers. */  /* Checks whether a partial matching is occurred. Does not modify registers. */
2439  DEFINE_COMPILER;  DEFINE_COMPILER;
2440  struct sljit_jump *jump = NULL;  struct sljit_jump *jump = NULL;
2441    
# Line 2170  else if (common->mode == JIT_PARTIAL_SOF Line 2450  else if (common->mode == JIT_PARTIAL_SOF
2450    jump = CMP(SLJIT_C_EQUAL, SLJIT_MEM1(SLJIT_LOCALS_REG), common->start_used_ptr, SLJIT_IMM, -1);    jump = CMP(SLJIT_C_EQUAL, SLJIT_MEM1(SLJIT_LOCALS_REG), common->start_used_ptr, SLJIT_IMM, -1);
2451    
2452  if (common->mode == JIT_PARTIAL_SOFT_COMPILE)  if (common->mode == JIT_PARTIAL_SOFT_COMPILE)
2453    OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), common->hit_start, SLJIT_IMM, -1);    OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), common->hit_start, SLJIT_IMM, 0);
2454  else  else
2455    {    {
2456    if (common->partialmatchlabel != NULL)    if (common->partialmatchlabel != NULL)
# Line 2183  if (jump != NULL) Line 2463  if (jump != NULL)
2463    JUMPHERE(jump);    JUMPHERE(jump);
2464  }  }
2465    
2466  static struct sljit_jump *check_str_end(compiler_common *common)  static void check_str_end(compiler_common *common, jump_list **end_reached)
2467  {  {
2468  /* Does not affect registers. Usually used in a tight spot. */  /* Does not affect registers. Usually used in a tight spot. */
2469  DEFINE_COMPILER;  DEFINE_COMPILER;
2470  struct sljit_jump *jump;  struct sljit_jump *jump;
 struct sljit_jump *nohit;  
 struct sljit_jump *return_value;  
2471    
2472  if (common->mode == JIT_COMPILE)  if (common->mode == JIT_COMPILE)
2473    return CMP(SLJIT_C_GREATER_EQUAL, STR_PTR, 0, STR_END, 0);    {
2474      add_jump(compiler, end_reached, CMP(SLJIT_C_GREATER_EQUAL, STR_PTR, 0, STR_END, 0));
2475      return;
2476      }
2477    
2478  jump = CMP(SLJIT_C_LESS, STR_PTR, 0, STR_END, 0);  jump = CMP(SLJIT_C_LESS, STR_PTR, 0, STR_END, 0);
2479  if (common->mode == JIT_PARTIAL_SOFT_COMPILE)  if (common->mode == JIT_PARTIAL_SOFT_COMPILE)
2480    {    {
2481    nohit = CMP(SLJIT_C_GREATER_EQUAL, SLJIT_MEM1(SLJIT_LOCALS_REG), common->start_used_ptr, STR_PTR, 0);    add_jump(compiler, end_reached, CMP(SLJIT_C_GREATER_EQUAL, SLJIT_MEM1(SLJIT_LOCALS_REG), common->start_used_ptr, STR_PTR, 0));
2482    OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), common->hit_start, SLJIT_IMM, -1);    OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), common->hit_start, SLJIT_IMM, 0);
2483    JUMPHERE(nohit);    add_jump(compiler, end_reached, JUMP(SLJIT_JUMP));
   return_value = JUMP(SLJIT_JUMP);  
2484    }    }
2485  else  else
2486    {    {
2487    return_value = CMP(SLJIT_C_GREATER_EQUAL, SLJIT_MEM1(SLJIT_LOCALS_REG), common->start_used_ptr, STR_PTR, 0);    add_jump(compiler, end_reached, CMP(SLJIT_C_GREATER_EQUAL, SLJIT_MEM1(SLJIT_LOCALS_REG), common->start_used_ptr, STR_PTR, 0));
2488    if (common->partialmatchlabel != NULL)    if (common->partialmatchlabel != NULL)
2489      JUMPTO(SLJIT_JUMP, common->partialmatchlabel);      JUMPTO(SLJIT_JUMP, common->partialmatchlabel);
2490    else    else
2491      add_jump(compiler, &common->partialmatch, JUMP(SLJIT_JUMP));      add_jump(compiler, &common->partialmatch, JUMP(SLJIT_JUMP));
2492    }    }
2493  JUMPHERE(jump);  JUMPHERE(jump);
 return return_value;  
2494  }  }
2495    
2496  static void detect_partial_match(compiler_common *common, jump_list **backtracks)  static void detect_partial_match(compiler_common *common, jump_list **backtracks)
# Line 2230  jump = CMP(SLJIT_C_LESS, STR_PTR, 0, STR Line 2509  jump = CMP(SLJIT_C_LESS, STR_PTR, 0, STR
2509  add_jump(compiler, backtracks, CMP(SLJIT_C_GREATER_EQUAL, SLJIT_MEM1(SLJIT_LOCALS_REG), common->start_used_ptr, STR_PTR, 0));  add_jump(compiler, backtracks, CMP(SLJIT_C_GREATER_EQUAL, SLJIT_MEM1(SLJIT_LOCALS_REG), common->start_used_ptr, STR_PTR, 0));
2510  if (common->mode == JIT_PARTIAL_SOFT_COMPILE)  if (common->mode == JIT_PARTIAL_SOFT_COMPILE)
2511    {    {
2512    OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), common->hit_start, SLJIT_IMM, -1);    OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), common->hit_start, SLJIT_IMM, 0);
2513    add_jump(compiler, backtracks, JUMP(SLJIT_JUMP));    add_jump(compiler, backtracks, JUMP(SLJIT_JUMP));
2514    }    }
2515  else  else
# Line 2243  else Line 2522  else
2522  JUMPHERE(jump);  JUMPHERE(jump);
2523  }  }
2524    
2525  static void read_char(compiler_common *common)  static void peek_char(compiler_common *common, pcre_uint32 max)
2526  {  {
2527  /* Reads the character into TMP1, updates STR_PTR.  /* Reads the character into TMP1, keeps STR_PTR.
2528  Does not check STR_END. TMP2 Destroyed. */  Does not check STR_END. TMP2 Destroyed. */
2529  DEFINE_COMPILER;  DEFINE_COMPILER;
2530  #if defined SUPPORT_UTF && !defined COMPILE_PCRE32  #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
2531  struct sljit_jump *jump;  struct sljit_jump *jump;
2532  #endif  #endif
2533    
2534    SLJIT_UNUSED_ARG(max);
2535    
2536  OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), 0);  OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), 0);
2537  #if defined SUPPORT_UTF && !defined COMPILE_PCRE32  #if defined SUPPORT_UTF && defined COMPILE_PCRE8
2538  if (common->utf)  if (common->utf)
2539    {    {
2540  #if defined COMPILE_PCRE8    if (max < 128) return;
2541    
2542    jump = CMP(SLJIT_C_LESS, TMP1, 0, SLJIT_IMM, 0xc0);    jump = CMP(SLJIT_C_LESS, TMP1, 0, SLJIT_IMM, 0xc0);
2543  #elif defined COMPILE_PCRE16    OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
   jump = CMP(SLJIT_C_LESS, TMP1, 0, SLJIT_IMM, 0xd800);  
 #endif /* COMPILE_PCRE[8|16] */  
2544    add_jump(compiler, &common->utfreadchar, JUMP(SLJIT_FAST_CALL));    add_jump(compiler, &common->utfreadchar, JUMP(SLJIT_FAST_CALL));
2545      OP2(SLJIT_SUB, STR_PTR, 0, STR_PTR, 0, TMP2, 0);
2546    JUMPHERE(jump);    JUMPHERE(jump);
2547    }    }
2548  #endif /* SUPPORT_UTF && !COMPILE_PCRE32 */  #endif /* SUPPORT_UTF && !COMPILE_PCRE32 */
2549    
2550    #if defined SUPPORT_UTF && defined COMPILE_PCRE16
2551    if (common->utf)
2552      {
2553      if (max < 0xd800) return;
2554    
2555      OP2(SLJIT_SUB, TMP2, 0, TMP1, 0, SLJIT_IMM, 0xd800);
2556      jump = CMP(SLJIT_C_GREATER, TMP2, 0, SLJIT_IMM, 0xdc00 - 0xd800 - 1);
2557      /* TMP2 contains the high surrogate. */
2558      OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(0));
2559      OP2(SLJIT_ADD, TMP2, 0, TMP2, 0, SLJIT_IMM, 0x40);
2560      OP2(SLJIT_SHL, TMP2, 0, TMP2, 0, SLJIT_IMM, 10);
2561      OP2(SLJIT_AND, TMP1, 0, TMP1, 0, SLJIT_IMM, 0x3ff);
2562      OP2(SLJIT_OR, TMP1, 0, TMP1, 0, TMP2, 0);
2563      JUMPHERE(jump);
2564      }
2565    #endif
2566    }
2567    
2568    #if defined SUPPORT_UTF && defined COMPILE_PCRE8
2569    
2570    static BOOL is_char7_bitset(const pcre_uint8 *bitset, BOOL nclass)
2571    {
2572    /* Tells whether the character codes below 128 are enough
2573    to determine a match. */
2574    const pcre_uint8 value = nclass ? 0xff : 0;
2575    const pcre_uint8* end = bitset + 32;
2576    
2577    bitset += 16;
2578    do
2579      {
2580      if (*bitset++ != value)
2581        return FALSE;
2582      }
2583    while (bitset < end);
2584    return TRUE;
2585    }
2586    
2587    static void read_char7_type(compiler_common *common, BOOL full_read)
2588    {
2589    /* Reads the precise character type of a character into TMP1, if the character
2590    is less than 128. Otherwise it returns with zero. Does not check STR_END. The
2591    full_read argument tells whether characters above max are accepted or not. */
2592    DEFINE_COMPILER;
2593    struct sljit_jump *jump;
2594    
2595    SLJIT_ASSERT(common->utf);
2596    
2597    OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), 0);
2598  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));
2599    
2600    OP1(SLJIT_MOV_UB, TMP1, 0, SLJIT_MEM1(TMP2), common->ctypes);
2601    
2602    if (full_read)
2603      {
2604      jump = CMP(SLJIT_C_LESS, TMP2, 0, SLJIT_IMM, 0xc0);
2605      OP1(SLJIT_MOV_UB, TMP2, 0, SLJIT_MEM1(TMP2), (sljit_sw)PRIV(utf8_table4) - 0xc0);
2606      OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, TMP2, 0);
2607      JUMPHERE(jump);
2608      }
2609  }  }
2610    
2611  static void peek_char(compiler_common *common)  #endif /* SUPPORT_UTF && COMPILE_PCRE8 */
2612    
2613    static void read_char_range(compiler_common *common, pcre_uint32 min, pcre_uint32 max, BOOL update_str_ptr)
2614  {  {
2615  /* Reads the character into TMP1, keeps STR_PTR.  /* Reads the precise value of a character into TMP1, if the character is
2616  Does not check STR_END. TMP2 Destroyed. */  between min and max (c >= min && c <= max). Otherwise it returns with a value
2617    outside the range. Does not check STR_END. */
2618  DEFINE_COMPILER;  DEFINE_COMPILER;
2619  #if defined SUPPORT_UTF && !defined COMPILE_PCRE32  #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
2620  struct sljit_jump *jump;  struct sljit_jump *jump;
2621  #endif  #endif
2622    #if defined SUPPORT_UTF && defined COMPILE_PCRE8
2623    struct sljit_jump *jump2;
2624    #endif
2625    
2626  OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), 0);  SLJIT_UNUSED_ARG(update_str_ptr);
2627  #if defined SUPPORT_UTF && !defined COMPILE_PCRE32  SLJIT_UNUSED_ARG(min);
2628    SLJIT_UNUSED_ARG(max);
2629    SLJIT_ASSERT(min <= max);
2630    
2631    OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(0));
2632    OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
2633    
2634    #if defined SUPPORT_UTF && defined COMPILE_PCRE8
2635  if (common->utf)  if (common->utf)
2636    {    {
2637  #if defined COMPILE_PCRE8    if (max < 128 && !update_str_ptr) return;
2638    
2639    jump = CMP(SLJIT_C_LESS, TMP1, 0, SLJIT_IMM, 0xc0);    jump = CMP(SLJIT_C_LESS, TMP1, 0, SLJIT_IMM, 0xc0);
2640  #elif defined COMPILE_PCRE16    if (min >= 0x10000)
2641    jump = CMP(SLJIT_C_LESS, TMP1, 0, SLJIT_IMM, 0xd800);      {
2642  #endif /* COMPILE_PCRE[8|16] */      OP2(SLJIT_SUB, TMP2, 0, TMP1, 0, SLJIT_IMM, 0xf0);
2643    add_jump(compiler, &common->utfreadchar, JUMP(SLJIT_FAST_CALL));      if (update_str_ptr)
2644    OP2(SLJIT_SUB, STR_PTR, 0, STR_PTR, 0, TMP2, 0);        OP1(SLJIT_MOV_UB, RETURN_ADDR, 0, SLJIT_MEM1(TMP1), (sljit_sw)PRIV(utf8_table4) - 0xc0);
2645        OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(0));
2646        jump2 = CMP(SLJIT_C_GREATER, TMP2, 0, SLJIT_IMM, 0x7);
2647        OP2(SLJIT_SHL, TMP2, 0, TMP2, 0, SLJIT_IMM, 6);
2648        OP2(SLJIT_AND, TMP1, 0, TMP1, 0, SLJIT_IMM, 0x3f);
2649        OP2(SLJIT_OR, TMP1, 0, TMP1, 0, TMP2, 0);
2650        OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(1));
2651        OP2(SLJIT_SHL, TMP1, 0, TMP1, 0, SLJIT_IMM, 6);
2652        OP2(SLJIT_AND, TMP2, 0, TMP2, 0, SLJIT_IMM, 0x3f);
2653        OP2(SLJIT_OR, TMP1, 0, TMP1, 0, TMP2, 0);
2654        OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(2));
2655        if (!update_str_ptr)
2656          OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(3));
2657        OP2(SLJIT_SHL, TMP1, 0, TMP1, 0, SLJIT_IMM, 6);
2658        OP2(SLJIT_AND, TMP2, 0, TMP2, 0, SLJIT_IMM, 0x3f);
2659        OP2(SLJIT_OR, TMP1, 0, TMP1, 0, TMP2, 0);
2660        JUMPHERE(jump2);
2661        if (update_str_ptr)
2662          OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, RETURN_ADDR, 0);
2663        }
2664      else if (min >= 0x800 && max <= 0xffff)
2665        {
2666        OP2(SLJIT_SUB, TMP2, 0, TMP1, 0, SLJIT_IMM, 0xe0);
2667        if (update_str_ptr)
2668          OP1(SLJIT_MOV_UB, RETURN_ADDR, 0, SLJIT_MEM1(TMP1), (sljit_sw)PRIV(utf8_table4) - 0xc0);
2669        OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(0));
2670        jump2 = CMP(SLJIT_C_GREATER, TMP2, 0, SLJIT_IMM, 0xf);
2671        OP2(SLJIT_SHL, TMP2, 0, TMP2, 0, SLJIT_IMM, 6);
2672        OP2(SLJIT_AND, TMP1, 0, TMP1, 0, SLJIT_IMM, 0x3f);
2673        OP2(SLJIT_OR, TMP1, 0, TMP1, 0, TMP2, 0);
2674        OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(1));
2675        if (!update_str_ptr)
2676          OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(2));
2677        OP2(SLJIT_SHL, TMP1, 0, TMP1, 0, SLJIT_IMM, 6);
2678        OP2(SLJIT_AND, TMP2, 0, TMP2, 0, SLJIT_IMM, 0x3f);
2679        OP2(SLJIT_OR, TMP1, 0, TMP1, 0, TMP2, 0);
2680        JUMPHERE(jump2);
2681        if (update_str_ptr)
2682          OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, RETURN_ADDR, 0);
2683        }
2684      else if (max >= 0x800)
2685        add_jump(compiler, (max < 0x10000) ? &common->utfreadchar16 : &common->utfreadchar, JUMP(SLJIT_FAST_CALL));
2686      else if (max < 128)
2687        {
2688        OP1(SLJIT_MOV_UB, TMP2, 0, SLJIT_MEM1(TMP1), (sljit_sw)PRIV(utf8_table4) - 0xc0);
2689        OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, TMP2, 0);
2690        }
2691      else
2692        {
2693        OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(0));
2694        if (!update_str_ptr)
2695          OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
2696        else
2697          OP1(SLJIT_MOV_UB, RETURN_ADDR, 0, SLJIT_MEM1(TMP1), (sljit_sw)PRIV(utf8_table4) - 0xc0);
2698        OP2(SLJIT_AND, TMP1, 0, TMP1, 0, SLJIT_IMM, 0x3f);
2699        OP2(SLJIT_SHL, TMP1, 0, TMP1, 0, SLJIT_IMM, 6);
2700        OP2(SLJIT_AND, TMP2, 0, TMP2, 0, SLJIT_IMM, 0x3f);
2701        OP2(SLJIT_OR, TMP1, 0, TMP1, 0, TMP2, 0);
2702        if (update_str_ptr)
2703          OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, RETURN_ADDR, 0);
2704        }
2705    JUMPHERE(jump);    JUMPHERE(jump);
2706    }    }
2707  #endif /* SUPPORT_UTF && !COMPILE_PCRE32 */  #endif
2708    
2709    #if defined SUPPORT_UTF && defined COMPILE_PCRE16
2710    if (common->utf)
2711      {
2712      if (max >= 0x10000)
2713        {
2714        OP2(SLJIT_SUB, TMP2, 0, TMP1, 0, SLJIT_IMM, 0xd800);
2715        jump = CMP(SLJIT_C_GREATER, TMP2, 0, SLJIT_IMM, 0xdc00 - 0xd800 - 1);
2716        /* TMP2 contains the high surrogate. */
2717        OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(0));
2718        OP2(SLJIT_ADD, TMP2, 0, TMP2, 0, SLJIT_IMM, 0x40);
2719        OP2(SLJIT_SHL, TMP2, 0, TMP2, 0, SLJIT_IMM, 10);
2720        OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
2721        OP2(SLJIT_AND, TMP1, 0, TMP1, 0, SLJIT_IMM, 0x3ff);
2722        OP2(SLJIT_OR, TMP1, 0, TMP1, 0, TMP2, 0);
2723        JUMPHERE(jump);
2724        return;
2725        }
2726    
2727      if (max < 0xd800 && !update_str_ptr) return;
2728    
2729      /* Skip low surrogate if necessary. */
2730      OP2(SLJIT_SUB, TMP2, 0, TMP1, 0, SLJIT_IMM, 0xd800);
2731      jump = CMP(SLJIT_C_GREATER, TMP2, 0, SLJIT_IMM, 0xdc00 - 0xd800 - 1);
2732      if (update_str_ptr)
2733        OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
2734      if (max >= 0xd800)
2735        OP1(SLJIT_MOV, TMP1, 0, SLJIT_IMM, 0x10000);
2736      JUMPHERE(jump);
2737      }
2738    #endif
2739  }  }
2740    
2741  static void read_char8_type(compiler_common *common)  static SLJIT_INLINE void read_char(compiler_common *common)
2742    {
2743    read_char_range(common, 0, READ_CHAR_MAX, TRUE);
2744    }
2745    
2746    static void read_char8_type(compiler_common *common, BOOL update_str_ptr)
2747  {  {
2748  /* Reads the character type into TMP1, updates STR_PTR. Does not check STR_END. */  /* Reads the character type into TMP1, updates STR_PTR. Does not check STR_END. */
2749  DEFINE_COMPILER;  DEFINE_COMPILER;
2750  #if defined SUPPORT_UTF || defined COMPILE_PCRE16 || defined COMPILE_PCRE32  #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
2751  struct sljit_jump *jump;  struct sljit_jump *jump;
2752  #endif  #endif
2753    #if defined SUPPORT_UTF && defined COMPILE_PCRE8
2754    struct sljit_jump *jump2;
2755    #endif
2756    
2757  #ifdef SUPPORT_UTF  SLJIT_UNUSED_ARG(update_str_ptr);
2758    
2759    OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), 0);
2760    OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
2761    
2762    #if defined SUPPORT_UTF && defined COMPILE_PCRE8
2763  if (common->utf)  if (common->utf)
2764    {    {
   OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), 0);  
   OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));  
 #if defined COMPILE_PCRE8  
2765    /* This can be an extra read in some situations, but hopefully    /* This can be an extra read in some situations, but hopefully
2766    it is needed in most cases. */    it is needed in most cases. */
2767    OP1(SLJIT_MOV_UB, TMP1, 0, SLJIT_MEM1(TMP2), common->ctypes);    OP1(SLJIT_MOV_UB, TMP1, 0, SLJIT_MEM1(TMP2), common->ctypes);
2768    jump = CMP(SLJIT_C_LESS, TMP2, 0, SLJIT_IMM, 0xc0);    jump = CMP(SLJIT_C_LESS, TMP2, 0, SLJIT_IMM, 0xc0);
2769    add_jump(compiler, &common->utfreadtype8, JUMP(SLJIT_FAST_CALL));    if (!update_str_ptr)
2770    JUMPHERE(jump);      {
2771  #elif defined COMPILE_PCRE16      OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(0));
2772    OP1(SLJIT_MOV, TMP1, 0, SLJIT_IMM, 0);      OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
2773    jump = CMP(SLJIT_C_GREATER, TMP2, 0, SLJIT_IMM, 255);      OP2(SLJIT_AND, TMP2, 0, TMP2, 0, SLJIT_IMM, 0x3f);
2774    OP1(SLJIT_MOV_UB, TMP1, 0, SLJIT_MEM1(TMP2), common->ctypes);      OP2(SLJIT_SHL, TMP2, 0, TMP2, 0, SLJIT_IMM, 6);
2775    JUMPHERE(jump);      OP2(SLJIT_AND, TMP1, 0, TMP1, 0, SLJIT_IMM, 0x3f);
2776    /* Skip low surrogate if necessary. */      OP2(SLJIT_OR, TMP2, 0, TMP2, 0, TMP1, 0);
2777    OP2(SLJIT_AND, TMP2, 0, TMP2, 0, SLJIT_IMM, 0xfc00);      OP1(SLJIT_MOV, TMP1, 0, SLJIT_IMM, 0);
2778    OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP2, 0, SLJIT_IMM, 0xd800);      jump2 = CMP(SLJIT_C_GREATER, TMP2, 0, SLJIT_IMM, 255);
2779    OP_FLAGS(SLJIT_MOV, TMP2, 0, SLJIT_UNUSED, 0, SLJIT_C_EQUAL);      OP1(SLJIT_MOV_UB, TMP1, 0, SLJIT_MEM1(TMP2), common->ctypes);
2780    OP2(SLJIT_SHL, TMP2, 0, TMP2, 0, SLJIT_IMM, 1);      JUMPHERE(jump2);
2781    OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, TMP2, 0);      }
2782  #elif defined COMPILE_PCRE32    else
2783    OP1(SLJIT_MOV, TMP1, 0, SLJIT_IMM, 0);      add_jump(compiler, &common->utfreadtype8, JUMP(SLJIT_FAST_CALL));
   jump = CMP(SLJIT_C_GREATER, TMP2, 0, SLJIT_IMM, 255);  
   OP1(SLJIT_MOV_UB, TMP1, 0, SLJIT_MEM1(TMP2), common->ctypes);  
2784    JUMPHERE(jump);    JUMPHERE(jump);
 #endif /* COMPILE_PCRE[8|16|32] */  
2785    return;    return;
2786    }    }
2787  #endif /* SUPPORT_UTF */  #endif /* SUPPORT_UTF && COMPILE_PCRE8 */
2788  OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), 0);  
2789  OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));  #if !defined COMPILE_PCRE8
 #if defined COMPILE_PCRE16 || defined COMPILE_PCRE32  
2790  /* The ctypes array contains only 256 values. */  /* The ctypes array contains only 256 values. */
2791  OP1(SLJIT_MOV, TMP1, 0, SLJIT_IMM, 0);  OP1(SLJIT_MOV, TMP1, 0, SLJIT_IMM, 0);
2792  jump = CMP(SLJIT_C_GREATER, TMP2, 0, SLJIT_IMM, 255);  jump = CMP(SLJIT_C_GREATER, TMP2, 0, SLJIT_IMM, 255);
2793  #endif  #endif
2794  OP1(SLJIT_MOV_UB, TMP1, 0, SLJIT_MEM1(TMP2), common->ctypes);  OP1(SLJIT_MOV_UB, TMP1, 0, SLJIT_MEM1(TMP2), common->ctypes);
2795  #if defined COMPILE_PCRE16 || defined COMPILE_PCRE32  #if !defined COMPILE_PCRE8
2796  JUMPHERE(jump);  JUMPHERE(jump);
2797  #endif  #endif
2798    
2799    #if defined SUPPORT_UTF && defined COMPILE_PCRE16
2800    if (common->utf && update_str_ptr)
2801      {
2802      /* Skip low surrogate if necessary. */
2803      OP2(SLJIT_SUB, TMP2, 0, TMP2, 0, SLJIT_IMM, 0xd800);
2804      jump = CMP(SLJIT_C_GREATER, TMP2, 0, SLJIT_IMM, 0xdc00 - 0xd800 - 1);
2805      OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
2806      JUMPHERE(jump);
2807      }
2808    #endif /* SUPPORT_UTF && COMPILE_PCRE16 */
2809  }  }
2810    
2811  static void skip_char_back(compiler_common *common)  static void skip_char_back(compiler_common *common)
# Line 2381  if (common->utf) Line 2843  if (common->utf)
2843  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));
2844  }  }
2845    
2846  static void check_newlinechar(compiler_common *common, int nltype, jump_list **backtracks, BOOL jumpiftrue)  static void check_newlinechar(compiler_common *common, int nltype, jump_list **backtracks, BOOL jumpifmatch)
2847  {  {
2848  /* Character comes in TMP1. Checks if it is a newline. TMP2 may be destroyed. */  /* Character comes in TMP1. Checks if it is a newline. TMP2 may be destroyed. */
2849  DEFINE_COMPILER;  DEFINE_COMPILER;
2850    struct sljit_jump *jump;
2851    
2852  if (nltype == NLTYPE_ANY)  if (nltype == NLTYPE_ANY)
2853    {    {
2854    add_jump(compiler, &common->anynewline, JUMP(SLJIT_FAST_CALL));    add_jump(compiler, &common->anynewline, JUMP(SLJIT_FAST_CALL));
2855    add_jump(compiler, backtracks, JUMP(jumpiftrue ? SLJIT_C_NOT_ZERO : SLJIT_C_ZERO));    add_jump(compiler, backtracks, JUMP(jumpifmatch ? SLJIT_C_NOT_ZERO : SLJIT_C_ZERO));
2856    }    }
2857  else if (nltype == NLTYPE_ANYCRLF)  else if (nltype == NLTYPE_ANYCRLF)
2858    {    {
2859    OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, CHAR_CR);    if (jumpifmatch)
2860    OP_FLAGS(SLJIT_MOV, TMP2, 0, SLJIT_UNUSED, 0, SLJIT_C_EQUAL);      {
2861    OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, CHAR_NL);      add_jump(compiler, backtracks, CMP(SLJIT_C_EQUAL, TMP1, 0, SLJIT_IMM, CHAR_CR));
2862    OP_FLAGS(SLJIT_OR | SLJIT_SET_E, TMP2, 0, TMP2, 0, SLJIT_C_EQUAL);      add_jump(compiler, backtracks, CMP(SLJIT_C_EQUAL, TMP1, 0, SLJIT_IMM, CHAR_NL));
2863    add_jump(compiler, backtracks, JUMP(jumpiftrue ? SLJIT_C_NOT_ZERO : SLJIT_C_ZERO));      }
2864      else
2865        {
2866        jump = CMP(SLJIT_C_EQUAL, TMP1, 0, SLJIT_IMM, CHAR_CR);
2867        add_jump(compiler, backtracks, CMP(SLJIT_C_NOT_EQUAL, TMP1, 0, SLJIT_IMM, CHAR_NL));
2868        JUMPHERE(jump);
2869        }
2870    }    }
2871  else  else
2872    {    {
2873    SLJIT_ASSERT(nltype == NLTYPE_FIXED && common->newline < 256);    SLJIT_ASSERT(nltype == NLTYPE_FIXED && common->newline < 256);
2874    add_jump(compiler, backtracks, CMP(jumpiftrue ? SLJIT_C_EQUAL : SLJIT_C_NOT_EQUAL, TMP1, 0, SLJIT_IMM, common->newline));    add_jump(compiler, backtracks, CMP(jumpifmatch ? SLJIT_C_EQUAL : SLJIT_C_NOT_EQUAL, TMP1, 0, SLJIT_IMM, common->newline));
2875    }    }
2876  }  }
2877    
# Line 2412  else Line 2881  else
2881  static void do_utfreadchar(compiler_common *common)  static void do_utfreadchar(compiler_common *common)
2882  {  {
2883  /* Fast decoding a UTF-8 character. TMP1 contains the first byte  /* Fast decoding a UTF-8 character. TMP1 contains the first byte
2884  of the character (>= 0xc0). Return char value in TMP1, length - 1 in TMP2. */  of the character (>= 0xc0). Return char value in TMP1, length in TMP2. */
2885  DEFINE_COMPILER;  DEFINE_COMPILER;
2886  struct sljit_jump *jump;  struct sljit_jump *jump;
2887    
2888  sljit_emit_fast_enter(compiler, RETURN_ADDR, 0);  sljit_emit_fast_enter(compiler, RETURN_ADDR, 0);
2889    OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(0));
2890    OP2(SLJIT_AND, TMP1, 0, TMP1, 0, SLJIT_IMM, 0x3f);
2891    OP2(SLJIT_SHL, TMP1, 0, TMP1, 0, SLJIT_IMM, 6);
2892    OP2(SLJIT_AND, TMP2, 0, TMP2, 0, SLJIT_IMM, 0x3f);
2893    OP2(SLJIT_OR, TMP1, 0, TMP1, 0, TMP2, 0);
2894    
2895  /* Searching for the first zero. */  /* Searching for the first zero. */
2896  OP2(SLJIT_AND | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x20);  OP2(SLJIT_AND | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x800);
2897  jump = JUMP(SLJIT_C_NOT_ZERO);  jump = JUMP(SLJIT_C_NOT_ZERO);
2898  /* Two byte sequence. */  /* Two byte sequence. */
 OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(1));  
2899  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));
2900  OP2(SLJIT_AND, TMP1, 0, TMP1, 0, SLJIT_IMM, 0x1f);  OP1(SLJIT_MOV, TMP2, 0, SLJIT_IMM, IN_UCHARS(2));
2901    sljit_emit_fast_return(compiler, RETURN_ADDR, 0);
2902    
2903    JUMPHERE(jump);
2904    OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(1));
2905    OP2(SLJIT_XOR, TMP1, 0, TMP1, 0, SLJIT_IMM, 0x800);
2906  OP2(SLJIT_SHL, TMP1, 0, TMP1, 0, SLJIT_IMM, 6);  OP2(SLJIT_SHL, TMP1, 0, TMP1, 0, SLJIT_IMM, 6);
2907  OP2(SLJIT_AND, TMP2, 0, TMP2, 0, SLJIT_IMM, 0x3f);  OP2(SLJIT_AND, TMP2, 0, TMP2, 0, SLJIT_IMM, 0x3f);
2908  OP2(SLJIT_OR, TMP1, 0, TMP1, 0, TMP2, 0);  OP2(SLJIT_OR, TMP1, 0, TMP1, 0, TMP2, 0);
 OP1(SLJIT_MOV, TMP2, 0, SLJIT_IMM, IN_UCHARS(1));  
 sljit_emit_fast_return(compiler, RETURN_ADDR, 0);  
 JUMPHERE(jump);  
2909    
2910  OP2(SLJIT_AND | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x10);  OP2(SLJIT_AND | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x10000);
2911  jump = JUMP(SLJIT_C_NOT_ZERO);  jump = JUMP(SLJIT_C_NOT_ZERO);
2912  /* Three byte sequence. */  /* Three byte sequence. */
 OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(1));  
 OP2(SLJIT_AND, TMP1, 0, TMP1, 0, SLJIT_IMM, 0x0f);  
 OP2(SLJIT_SHL, TMP1, 0, TMP1, 0, SLJIT_IMM, 12);  
 OP2(SLJIT_AND, TMP2, 0, TMP2, 0, SLJIT_IMM, 0x3f);  
 OP2(SLJIT_SHL, TMP2, 0, TMP2, 0, SLJIT_IMM, 6);  
 OP2(SLJIT_OR, TMP1, 0, TMP1, 0, TMP2, 0);  
 OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(2));  
2913  OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(2));  OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(2));
2914  OP2(SLJIT_AND, TMP2, 0, TMP2, 0, SLJIT_IMM, 0x3f);  OP1(SLJIT_MOV, TMP2, 0, SLJIT_IMM, IN_UCHARS(3));
 OP2(SLJIT_OR, TMP1, 0, TMP1, 0, TMP2, 0);  
 OP1(SLJIT_MOV, TMP2, 0, SLJIT_IMM, IN_UCHARS(2));  
2915  sljit_emit_fast_return(compiler, RETURN_ADDR, 0);  sljit_emit_fast_return(compiler, RETURN_ADDR, 0);
 JUMPHERE(jump);  
2916    
2917  /* Four byte sequence. */  /* Four byte sequence. */
2918  OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(1));  JUMPHERE(jump);
2919  OP2(SLJIT_AND, TMP1, 0, TMP1, 0, SLJIT_IMM, 0x07);  OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(2));
2920  OP2(SLJIT_SHL, TMP1, 0, TMP1, 0, SLJIT_IMM, 18);  OP2(SLJIT_XOR, TMP1, 0, TMP1, 0, SLJIT_IMM, 0x10000);
2921    OP2(SLJIT_SHL, TMP1, 0, TMP1, 0, SLJIT_IMM, 6);
2922    OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(3));
2923  OP2(SLJIT_AND, TMP2, 0, TMP2, 0, SLJIT_IMM, 0x3f);  OP2(SLJIT_AND, TMP2, 0, TMP2, 0, SLJIT_IMM, 0x3f);
 OP2(SLJIT_SHL, TMP2, 0, TMP2, 0, SLJIT_IMM, 12);  
2924  OP2(SLJIT_OR, TMP1, 0, TMP1, 0, TMP2, 0);  OP2(SLJIT_OR, TMP1, 0, TMP1, 0, TMP2, 0);
2925  OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(2));  OP1(SLJIT_MOV, TMP2, 0, SLJIT_IMM, IN_UCHARS(4));
2926    sljit_emit_fast_return(compiler, RETURN_ADDR, 0);
2927    }
2928    
2929    static void do_utfreadchar16(compiler_common *common)
2930    {
2931    /* Fast decoding a UTF-8 character. TMP1 contains the first byte
2932    of the character (>= 0xc0). Return value in TMP1. */
2933    DEFINE_COMPILER;
2934    struct sljit_jump *jump;
2935    
2936    sljit_emit_fast_enter(compiler, RETURN_ADDR, 0);
2937    OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(0));
2938    OP2(SLJIT_AND, TMP1, 0, TMP1, 0, SLJIT_IMM, 0x3f);
2939    OP2(SLJIT_SHL, TMP1, 0, TMP1, 0, SLJIT_IMM, 6);
2940  OP2(SLJIT_AND, TMP2, 0, TMP2, 0, SLJIT_IMM, 0x3f);  OP2(SLJIT_AND, TMP2, 0, TMP2, 0, SLJIT_IMM, 0x3f);
 OP2(SLJIT_SHL, TMP2, 0, TMP2, 0, SLJIT_IMM, 6);  
2941  OP2(SLJIT_OR, TMP1, 0, TMP1, 0, TMP2, 0);  OP2(SLJIT_OR, TMP1, 0, TMP1, 0, TMP2, 0);
2942  OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(3));  
2943  OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(3));  /* Searching for the first zero. */
2944    OP2(SLJIT_AND | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x800);
2945    jump = JUMP(SLJIT_C_NOT_ZERO);
2946    /* Two byte sequence. */
2947    OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
2948    sljit_emit_fast_return(compiler, RETURN_ADDR, 0);
2949    
2950    JUMPHERE(jump);
2951    OP2(SLJIT_AND | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x400);
2952    OP_FLAGS(SLJIT_MOV, TMP2, 0, SLJIT_UNUSED, 0, SLJIT_C_NOT_ZERO);
2953    /* This code runs only in 8 bit mode. No need to shift the value. */
2954    OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, TMP2, 0);
2955    OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(1));
2956    OP2(SLJIT_XOR, TMP1, 0, TMP1, 0, SLJIT_IMM, 0x800);
2957    OP2(SLJIT_SHL, TMP1, 0, TMP1, 0, SLJIT_IMM, 6);
2958  OP2(SLJIT_AND, TMP2, 0, TMP2, 0, SLJIT_IMM, 0x3f);  OP2(SLJIT_AND, TMP2, 0, TMP2, 0, SLJIT_IMM, 0x3f);
2959  OP2(SLJIT_OR, TMP1, 0, TMP1, 0, TMP2, 0);  OP2(SLJIT_OR, TMP1, 0, TMP1, 0, TMP2, 0);
2960  OP1(SLJIT_MOV, TMP2, 0, SLJIT_IMM, IN_UCHARS(3));  /* Three byte sequence. */
2961    OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(2));
2962  sljit_emit_fast_return(compiler, RETURN_ADDR, 0);  sljit_emit_fast_return(compiler, RETURN_ADDR, 0);
2963  }  }
2964    
# Line 2483  jump = JUMP(SLJIT_C_NOT_ZERO); Line 2978  jump = JUMP(SLJIT_C_NOT_ZERO);
2978  OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(0));  OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(0));
2979  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));
2980  OP2(SLJIT_AND, TMP2, 0, TMP2, 0, SLJIT_IMM, 0x1f);  OP2(SLJIT_AND, TMP2, 0, TMP2, 0, SLJIT_IMM, 0x1f);
2981    /* The upper 5 bits are known at this point. */
2982    compare = CMP(SLJIT_C_GREATER, TMP2, 0, SLJIT_IMM, 0x3);
2983  OP2(SLJIT_SHL, TMP2, 0, TMP2, 0, SLJIT_IMM, 6);  OP2(SLJIT_SHL, TMP2, 0, TMP2, 0, SLJIT_IMM, 6);
2984  OP2(SLJIT_AND, TMP1, 0, TMP1, 0, SLJIT_IMM, 0x3f);  OP2(SLJIT_AND, TMP1, 0, TMP1, 0, SLJIT_IMM, 0x3f);
2985  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);  
2986  OP1(SLJIT_MOV_UB, TMP1, 0, SLJIT_MEM1(TMP2), common->ctypes);  OP1(SLJIT_MOV_UB, TMP1, 0, SLJIT_MEM1(TMP2), common->ctypes);
2987  sljit_emit_fast_return(compiler, RETURN_ADDR, 0);  sljit_emit_fast_return(compiler, RETURN_ADDR, 0);
2988    
2989  JUMPHERE(compare);  JUMPHERE(compare);
2990  OP1(SLJIT_MOV, TMP1, 0, SLJIT_IMM, 0);  OP1(SLJIT_MOV, TMP1, 0, SLJIT_IMM, 0);
2991  sljit_emit_fast_return(compiler, RETURN_ADDR, 0);  sljit_emit_fast_return(compiler, RETURN_ADDR, 0);
 JUMPHERE(jump);  
2992    
2993  /* We only have types for characters less than 256. */  /* We only have types for characters less than 256. */
 OP1(SLJIT_MOV_UB, TMP1, 0, SLJIT_MEM1(TMP2), (sljit_sw)PRIV(utf8_table4) - 0xc0);  
 OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, TMP1, 0);  
 OP1(SLJIT_MOV, TMP1, 0, SLJIT_IMM, 0);  
 sljit_emit_fast_return(compiler, RETURN_ADDR, 0);  
 }  
   
 #elif defined COMPILE_PCRE16  
   
 static void do_utfreadchar(compiler_common *common)  
 {  
 /* Fast decoding a UTF-16 character. TMP1 contains the first 16 bit char  
 of the character (>= 0xd800). Return char value in TMP1, length - 1 in TMP2. */  
 DEFINE_COMPILER;  
 struct sljit_jump *jump;  
   
 sljit_emit_fast_enter(compiler, RETURN_ADDR, 0);  
 jump = CMP(SLJIT_C_LESS, TMP1, 0, SLJIT_IMM, 0xdc00);  
 /* Do nothing, only return. */  
 sljit_emit_fast_return(compiler, RETURN_ADDR, 0);  
   
2994  JUMPHERE(jump);  JUMPHERE(jump);
2995  /* Combine two 16 bit characters. */  OP1(SLJIT_MOV_UB, TMP2, 0, SLJIT_MEM1(TMP2), (sljit_sw)PRIV(utf8_table4) - 0xc0);
2996  OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(1));  OP1(SLJIT_MOV, TMP1, 0, SLJIT_IMM, 0);
2997  OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));  OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, TMP2, 0);
 OP2(SLJIT_AND, TMP1, 0, TMP1, 0, SLJIT_IMM, 0x3ff);  
 OP2(SLJIT_SHL, TMP1, 0, TMP1, 0, SLJIT_IMM, 10);  
 OP2(SLJIT_AND, TMP2, 0, TMP2, 0, SLJIT_IMM, 0x3ff);  
 OP2(SLJIT_OR, TMP1, 0, TMP1, 0, TMP2, 0);  
 OP1(SLJIT_MOV, TMP2, 0, SLJIT_IMM, IN_UCHARS(1));  
 OP2(SLJIT_ADD, TMP1, 0, TMP1, 0, SLJIT_IMM, 0x10000);  
2998  sljit_emit_fast_return(compiler, RETURN_ADDR, 0);  sljit_emit_fast_return(compiler, RETURN_ADDR, 0);
2999  }  }
3000    
3001  #endif /* COMPILE_PCRE[8|16] */  #endif /* COMPILE_PCRE8 */
3002    
3003  #endif /* SUPPORT_UTF */  #endif /* SUPPORT_UTF */
3004    
# Line 2604  if (firstline) Line 3073  if (firstline)
3073      mainloop = LABEL();      mainloop = LABEL();
3074      /* Continual stores does not cause data dependency. */      /* Continual stores does not cause data dependency. */
3075      OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), common->first_line_end, STR_PTR, 0);      OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), common->first_line_end, STR_PTR, 0);
3076      read_char(common);      read_char_range(common, common->nlmin, common->nlmax, TRUE);
3077      check_newlinechar(common, common->nltype, &newline, TRUE);      check_newlinechar(common, common->nltype, &newline, TRUE);
3078      CMPTO(SLJIT_C_LESS, STR_PTR, 0, STR_END, 0, mainloop);      CMPTO(SLJIT_C_LESS, STR_PTR, 0, STR_END, 0, mainloop);
3079      JUMPHERE(end);      JUMPHERE(end);
# Line 2680  if (newlinecheck) Line 3149  if (newlinecheck)
3149  return mainloop;  return mainloop;
3150  }  }
3151    
3152  #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)  
3153  {  {
3154  DEFINE_COMPILER;  /* Recursive function, which scans prefix literals. */
3155  struct sljit_label *start;  int len, repeat, len_save, consumed = 0;
3156  struct sljit_jump *quit;  pcre_uint32 chr, mask;
3157  pcre_uint32 chars[MAX_N_CHARS * 2];  pcre_uchar *alternative, *cc_save, *oc;
3158  pcre_uchar *cc = common->start + 1 + LINK_SIZE;  BOOL last, any, caseless;
3159  int location = 0;  #if defined SUPPORT_UTF && defined COMPILE_PCRE8
3160  pcre_int32 len, c, bit, caseless;  pcre_uchar othercase[8];
3161  int must_stop;  #elif defined SUPPORT_UTF && defined COMPILE_PCRE16
3162    pcre_uchar othercase[2];
3163  /* We do not support alternatives now. */  #else
3164  if (*(common->start + GET(common->start, 1)) == OP_ALT)  pcre_uchar othercase[1];
3165    return FALSE;  #endif
3166    
3167    repeat = 1;
3168  while (TRUE)  while (TRUE)
3169    {    {
3170    caseless = 0;    last = TRUE;
3171    must_stop = 1;    any = FALSE;
3172    switch(*cc)    caseless = FALSE;
3173      switch (*cc)
3174      {      {
     case OP_CHAR:  
     must_stop = 0;  
     cc++;  
     break;  
   
3175      case OP_CHARI:      case OP_CHARI:
3176      caseless = 1;      caseless = TRUE;
3177      must_stop = 0;      case OP_CHAR:
3178        last = FALSE;
3179      cc++;      cc++;
3180      break;      break;
3181    
# Line 2729  while (TRUE) Line 3194  while (TRUE)
3194      cc++;      cc++;
3195      continue;      continue;
3196    
3197        case OP_ASSERT:
3198        case OP_ASSERT_NOT:
3199        case OP_ASSERTBACK:
3200        case OP_ASSERTBACK_NOT:
3201        cc = bracketend(cc);
3202        continue;
3203    
3204      case OP_PLUS:      case OP_PLUS:
3205      case OP_MINPLUS:      case OP_MINPLUS:
3206      case OP_POSPLUS:      case OP_POSPLUS:
3207      cc++;      cc++;
3208      break;      break;
3209    
3210        case OP_EXACTI:
3211        caseless = TRUE;
3212      case OP_EXACT:      case OP_EXACT:
3213        repeat = GET2(cc, 1);
3214        last = FALSE;
3215      cc += 1 + IMM2_SIZE;      cc += 1 + IMM2_SIZE;
3216      break;      break;
3217    
3218      case OP_PLUSI:      case OP_PLUSI:
3219      case OP_MINPLUSI:      case OP_MINPLUSI:
3220      case OP_POSPLUSI:      case OP_POSPLUSI:
3221      caseless = 1;      caseless = TRUE;
3222      cc++;      cc++;
3223      break;      break;
3224    
3225      case OP_EXACTI:      case OP_KET:
3226      caseless = 1;      cc += 1 + LINK_SIZE;
3227      cc += 1 + IMM2_SIZE;      continue;
3228    
3229        case OP_ALT:
3230        cc += GET(cc, 1);
3231        continue;
3232    
3233        case OP_ONCE:
3234        case OP_ONCE_NC:
3235        case OP_BRA:
3236        case OP_BRAPOS:
3237        case OP_CBRA:
3238        case OP_CBRAPOS:
3239        alternative = cc + GET(cc, 1);
3240        while (*alternative == OP_ALT)
3241          {
3242          max_chars = scan_prefix(common, alternative + 1 + LINK_SIZE, chars, max_chars);
3243          if (max_chars == 0)
3244            return consumed;
3245          alternative += GET(alternative, 1);
3246          }
3247    
3248        if (*cc == OP_CBRA || *cc == OP_CBRAPOS)
3249          cc += IMM2_SIZE;
3250        cc += 1 + LINK_SIZE;
3251        continue;
3252    
3253        case OP_CLASS:
3254    #if defined SUPPORT_UTF && defined COMPILE_PCRE8
3255        if (common->utf && !is_char7_bitset((const pcre_uint8 *)(cc + 1), FALSE)) return consumed;
3256    #endif
3257        any = TRUE;
3258        cc += 1 + 32 / sizeof(pcre_uchar);
3259      break;      break;
3260    
3261      default:      case OP_NCLASS:
3262      must_stop = 2;  #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
3263        if (common->utf) return consumed;
3264    #endif
3265        any = TRUE;
3266        cc += 1 + 32 / sizeof(pcre_uchar);
3267      break;      break;
3268    
3269    #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
3270        case OP_XCLASS:
3271    #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
3272        if (common->utf) return consumed;
3273    #endif
3274        any = TRUE;
3275        cc += GET(cc, 1);
3276        break;
3277    #endif
3278    
3279        case OP_DIGIT:
3280    #if defined SUPPORT_UTF && defined COMPILE_PCRE8
3281        if (common->utf && !is_char7_bitset((const pcre_uint8 *)common->ctypes - cbit_length + cbit_digit, FALSE))
3282          return consumed;
3283    #endif
3284        any = TRUE;
3285        cc++;
3286        break;
3287    
3288        case OP_WHITESPACE:
3289    #if defined SUPPORT_UTF && defined COMPILE_PCRE8
3290        if (common->utf && !is_char7_bitset((const pcre_uint8 *)common->ctypes - cbit_length + cbit_space, FALSE))
3291          return consumed;
3292    #endif
3293        any = TRUE;
3294        cc++;
3295        break;
3296    
3297        case OP_WORDCHAR:
3298    #if defined SUPPORT_UTF && defined COMPILE_PCRE8
3299        if (common->utf && !is_char7_bitset((const pcre_uint8 *)common->ctypes - cbit_length + cbit_word, FALSE))
3300          return consumed;
3301    #endif
3302        any = TRUE;
3303        cc++;
3304        break;
3305    
3306        case OP_NOT_DIGIT:
3307        case OP_NOT_WHITESPACE:
3308        case OP_NOT_WORDCHAR:
3309        case OP_ANY:
3310        case OP_ALLANY:
3311    #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
3312        if (common->utf) return consumed;
3313    #endif
3314        any = TRUE;
3315        cc++;
3316        break;
3317    
3318    #ifdef SUPPORT_UCP
3319        case OP_NOTPROP:
3320        case OP_PROP:
3321    #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
3322        if (common->utf) return consumed;
3323    #endif
3324        any = TRUE;
3325        cc += 1 + 2;
3326        break;
3327    #endif
3328    
3329        case OP_TYPEEXACT:
3330        repeat = GET2(cc, 1);
3331        cc += 1 + IMM2_SIZE;
3332        continue;
3333    
3334        default:
3335        return consumed;
3336      }      }
3337    
3338    if (must_stop == 2)    if (any)
3339        break;      {
3340    #if defined COMPILE_PCRE8
3341        mask = 0xff;
3342    #elif defined COMPILE_PCRE16
3343        mask = 0xffff;
3344    #elif defined COMPILE_PCRE32
3345        mask = 0xffffffff;
3346    #else
3347        SLJIT_ASSERT_STOP();
3348    #endif
3349    
3350        do
3351          {
3352          chars[0] = mask;
3353          chars[1] = mask;
3354    
3355          consumed++;
3356          if (--max_chars == 0)
3357            return consumed;
3358          chars += 2;
3359          }
3360        while (--repeat > 0);
3361    
3362        repeat = 1;
3363        continue;
3364        }
3365    
3366    len = 1;    len = 1;
3367  #ifdef SUPPORT_UTF  #ifdef SUPPORT_UTF
3368    if (common->utf && HAS_EXTRALEN(cc[0])) len += GET_EXTRALEN(cc[0]);    if (common->utf && HAS_EXTRALEN(*cc)) len += GET_EXTRALEN(*cc);
3369  #endif  #endif
3370    
3371    if (caseless && char_has_othercase(common, cc))    if (caseless && char_has_othercase(common, cc))
3372      {      {
3373      caseless = char_get_othercase_bit(common, cc);  #ifdef SUPPORT_UTF
3374      if (caseless == 0)      if (common->utf)
3375        return FALSE;        {
3376  #ifdef COMPILE_PCRE8        GETCHAR(chr, cc);
3377      caseless = ((caseless & 0xff) << 8) | (len - (caseless >> 8));        if (PRIV(ord2utf)(char_othercase(common, chr), othercase) != len)
3378  #else          return consumed;
3379      if ((caseless & 0x100) != 0)        }
       caseless = ((caseless & 0xff) << 16) | (len - (caseless >> 9));  
3380      else      else
       caseless = ((caseless & 0xff) << 8) | (len - (caseless >> 9));  
3381  #endif  #endif
3382          {
3383          chr = *cc;
3384          othercase[0] = TABLE_GET(chr, common->fcc, chr);
3385          }
3386      }      }
3387    else    else
3388      caseless = 0;      caseless = FALSE;
3389    
3390    while (len > 0 && location < MAX_N_CHARS * 2)    len_save = len;
3391      cc_save = cc;
3392      while (TRUE)
3393      {      {
3394      c = *cc;      oc = othercase;
3395      bit = 0;      do
     if (len == (caseless & 0xff))  
3396        {        {
3397        bit = caseless >> 8;        chr = *cc;
3398        c |= bit;  #ifdef COMPILE_PCRE32
3399          if (SLJIT_UNLIKELY(chr == NOTACHAR))
3400            return consumed;
3401    #endif
3402          mask = 0;
3403          if (caseless)
3404            {
3405            mask = *cc ^ *oc;
3406            chr |= mask;
3407            }
3408    
3409    #ifdef COMPILE_PCRE32
3410          if (chars[0] == NOTACHAR && chars[1] == 0)
3411    #else
3412          if (chars[0] == NOTACHAR)
3413    #endif
3414            {
3415            chars[0] = chr;
3416            chars[1] = mask;
3417            }
3418          else
3419            {
3420            mask |= chars[0] ^ chr;
3421            chr |= mask;
3422            chars[0] = chr;
3423            chars[1] |= mask;
3424            }
3425    
3426          len--;
3427          consumed++;
3428          if (--max_chars == 0)
3429            return consumed;
3430          chars += 2;
3431          cc++;
3432          oc++;
3433        }        }
3434        while (len > 0);
3435    
3436      chars[location] = c;      if (--repeat == 0)
3437      chars[location + 1] = bit;        break;
3438    
3439      len--;      len = len_save;
3440      location += 2;      cc = cc_save;
3441      cc++;      }
3442    
3443      repeat = 1;
3444      if (last)
3445        return consumed;
3446      }
3447    }
3448    
3449    #define MAX_N_CHARS 16
3450    
3451    static SLJIT_INLINE BOOL fast_forward_first_n_chars(compiler_common *common, BOOL firstline)
3452    {
3453    DEFINE_COMPILER;
3454    struct sljit_label *start;
3455    struct sljit_jump *quit;
3456    pcre_uint32 chars[MAX_N_CHARS * 2];
3457    pcre_uint8 ones[MAX_N_CHARS];
3458    int offsets[3];
3459    pcre_uint32 mask, byte;
3460    int i, max, from;
3461    int range_right = -1, range_len = 4 - 1;
3462    sljit_ub *update_table = NULL;
3463    BOOL in_range;
3464    
3465    /* This is even TRUE, if both are NULL. */
3466    SLJIT_ASSERT(common->read_only_data_ptr == common->read_only_data);
3467    
3468    for (i = 0; i < MAX_N_CHARS; i++)
3469      {
3470      chars[i << 1] = NOTACHAR;
3471      chars[(i << 1) + 1] = 0;
3472      }
3473    
3474    max = scan_prefix(common, common->start, chars, MAX_N_CHARS);
3475    
3476    if (max <= 1)
3477      return FALSE;
3478    
3479    for (i = 0; i < max; i++)
3480      {
3481      mask = chars[(i << 1) + 1];
3482      ones[i] = ones_in_half_byte[mask & 0xf];
3483      mask >>= 4;
3484      while (mask != 0)
3485        {
3486        ones[i] += ones_in_half_byte[mask & 0xf];
3487        mask >>= 4;
3488      }      }
3489      }
3490    
3491    if (location >= MAX_N_CHARS * 2 || must_stop != 0)  in_range = FALSE;
3492    for (i = 0; i <= max; i++)
3493      {
3494      if (i < max && ones[i] <= 1)
3495        {
3496        if (!in_range)
3497          {
3498          in_range = TRUE;
3499          from = i;
3500          }
3501        }
3502      else if (in_range)
3503        {
3504        if ((i - from) > range_len)
3505          {
3506          range_len = i - from;
3507          range_right = i - 1;
3508          }
3509        in_range = FALSE;
3510        }
3511      }
3512    
3513    if (range_right >= 0)
3514      {
3515      /* Since no data is consumed (see the assert in the beginning
3516      of this function), this space can be reallocated. */
3517      if (common->read_only_data)
3518        SLJIT_FREE(common->read_only_data);
3519    
3520      common->read_only_data_size += 256;
3521      common->read_only_data = (sljit_uw *)SLJIT_MALLOC(common->read_only_data_size);
3522      if (common->read_only_data == NULL)
3523        return TRUE;
3524    
3525      update_table = (sljit_ub *)common->read_only_data;
3526      common->read_only_data_ptr = (sljit_uw *)(update_table + 256);
3527      memset(update_table, IN_UCHARS(range_len), 256);
3528    
3529      for (i = 0; i < range_len; i++)
3530        {
3531        byte = chars[(range_right - i) << 1] & 0xff;
3532        if (update_table[byte] > IN_UCHARS(i))
3533          update_table[byte] = IN_UCHARS(i);
3534        mask = chars[((range_right - i) << 1) + 1] & 0xff;
3535        if (mask != 0)
3536          {
3537          byte ^= mask;
3538          if (update_table[byte] > IN_UCHARS(i))
3539            update_table[byte] = IN_UCHARS(i);
3540          }
3541        }
3542      }
3543    
3544    offsets[0] = -1;
3545    /* Scan forward. */
3546    for (i = 0; i < max; i++)
3547      if (ones[i] <= 2) {
3548        offsets[0] = i;
3549      break;      break;
3550    }    }
3551    
3552  /* At least two characters are required. */  if (offsets[0] == -1)
3553  if (location < 2 * 2)    return FALSE;
3554      return FALSE;  
3555    /* Scan backward. */
3556    offsets[1] = -1;
3557    for (i = max - 1; i > offsets[0]; i--)
3558      if (ones[i] <= 2 && i != range_right)
3559        {
3560        offsets[1] = i;
3561        break;
3562        }
3563    
3564    /* This case is handled better by fast_forward_first_char. */
3565    if (offsets[1] == -1 && offsets[0] == 0)
3566      return FALSE;
3567    
3568    offsets[2] = -1;
3569    if (offsets[1] >= 0 && range_right == -1)
3570      {
3571      /* Scan from middle. */
3572      for (i = (offsets[0] + offsets[1]) / 2 + 1; i < offsets[1]; i++)
3573        if (ones[i] <= 2)
3574          {
3575          offsets[2] = i;
3576          break;
3577          }
3578    
3579      if (offsets[2] == -1)
3580        {
3581        for (i = (offsets[0] + offsets[1]) / 2; i > offsets[0]; i--)
3582          if (ones[i] <= 2)
3583            {
3584            offsets[2] = i;
3585            break;
3586            }
3587        }
3588      }
3589    
3590    SLJIT_ASSERT(offsets[1] == -1 || (offsets[0] < offsets[1]));
3591    SLJIT_ASSERT(offsets[2] == -1 || (offsets[0] < offsets[2] && offsets[1] > offsets[2]));
3592    
3593    chars[0] = chars[offsets[0] << 1];
3594    chars[1] = chars[(offsets[0] << 1) + 1];
3595    if (offsets[2] >= 0)
3596      {
3597      chars[2] = chars[offsets[2] << 1];
3598      chars[3] = chars[(offsets[2] << 1) + 1];
3599      }
3600    if (offsets[1] >= 0)
3601      {
3602      chars[4] = chars[offsets[1] << 1];
3603      chars[5] = chars[(offsets[1] << 1) + 1];
3604      }
3605    
3606    max -= 1;
3607  if (firstline)  if (firstline)
3608    {    {
3609    SLJIT_ASSERT(common->first_line_end != 0);    SLJIT_ASSERT(common->first_line_end != 0);
3610      OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), common->first_line_end);
3611    OP1(SLJIT_MOV, TMP3, 0, STR_END, 0);    OP1(SLJIT_MOV, TMP3, 0, STR_END, 0);
3612    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, STR_END, 0, SLJIT_IMM, IN_UCHARS(max));
3613      quit = CMP(SLJIT_C_LESS_EQUAL, STR_END, 0, TMP1, 0);
3614      OP1(SLJIT_MOV, STR_END, 0, TMP1, 0);
3615      JUMPHERE(quit);
3616    }    }
3617  else  else
3618    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));
3619    
3620    #if !(defined SLJIT_CONFIG_X86_32 && SLJIT_CONFIG_X86_32)
3621    if (range_right >= 0)
3622      OP1(SLJIT_MOV, RETURN_ADDR, 0, SLJIT_IMM, (sljit_sw)update_table);
3623    #endif
3624    
3625  start = LABEL();  start = LABEL();
3626  quit = CMP(SLJIT_C_GREATER_EQUAL, STR_PTR, 0, STR_END, 0);  quit = CMP(SLJIT_C_GREATER_EQUAL, STR_PTR, 0, STR_END, 0);
3627    
3628  OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(0));  if (range_right >= 0)
3629  OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(1));    {
3630    #if defined COMPILE_PCRE8 || (defined SLJIT_LITTLE_ENDIAN && SLJIT_LITTLE_ENDIAN)
3631      OP1(SLJIT_MOV_UB, TMP1, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(range_right));
3632    #else
3633      OP1(SLJIT_MOV_UB, TMP1, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(range_right + 1) - 1);
3634    #endif
3635    
3636    #if !(defined SLJIT_CONFIG_X86_32 && SLJIT_CONFIG_X86_32)
3637      OP1(SLJIT_MOV_UB, TMP1, 0, SLJIT_MEM2(RETURN_ADDR, TMP1), 0);
3638    #else
3639      OP1(SLJIT_MOV_UB, TMP1, 0, SLJIT_MEM1(TMP1), (sljit_sw)update_table);
3640    #endif
3641      OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, TMP1, 0);
3642      CMPTO(SLJIT_C_NOT_EQUAL, TMP1, 0, SLJIT_IMM, 0, start);
3643      }
3644    
3645    OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(offsets[0]));
3646    if (offsets[1] >= 0)
3647      OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(offsets[1]));
3648  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));
3649    
3650  if (chars[1] != 0)  if (chars[1] != 0)
3651    OP2(SLJIT_OR, TMP1, 0, TMP1, 0, SLJIT_IMM, chars[1]);    OP2(SLJIT_OR, TMP1, 0, TMP1, 0, SLJIT_IMM, chars[1]);
3652  CMPTO(SLJIT_C_NOT_EQUAL, TMP1, 0, SLJIT_IMM, chars[0], start);  CMPTO(SLJIT_C_NOT_EQUAL, TMP1, 0, SLJIT_IMM, chars[0], start);
3653  if (location > 2 * 2)  if (offsets[2] >= 0)
3654    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));
3655  if (chars[3] != 0)  
3656    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)  
3657    {    {
3658    if (chars[5] != 0)    if (chars[5] != 0)
3659      OP2(SLJIT_OR, TMP1, 0, TMP1, 0, SLJIT_IMM, chars[5]);      OP2(SLJIT_OR, TMP2, 0, TMP2, 0, SLJIT_IMM, chars[5]);
3660    CMPTO(SLJIT_C_NOT_EQUAL, TMP1, 0, SLJIT_IMM, chars[4], start);    CMPTO(SLJIT_C_NOT_EQUAL, TMP2, 0, SLJIT_IMM, chars[4], start);
3661      }
3662    
3663    if (offsets[2] >= 0)
3664      {
3665      if (chars[3] != 0)
3666        OP2(SLJIT_OR, TMP1, 0, TMP1, 0, SLJIT_IMM, chars[3]);
3667      CMPTO(SLJIT_C_NOT_EQUAL, TMP1, 0, SLJIT_IMM, chars[2], start);
3668    }    }
3669  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));
3670    
3671  JUMPHERE(quit);  JUMPHERE(quit);
3672    
3673  if (firstline)  if (firstline)
3674      {
3675      if (range_right >= 0)
3676        OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), common->first_line_end);
3677    OP1(SLJIT_MOV, STR_END, 0, TMP3, 0);    OP1(SLJIT_MOV, STR_END, 0, TMP3, 0);
3678      if (range_right >= 0)
3679        {
3680        quit = CMP(SLJIT_C_LESS_EQUAL, STR_PTR, 0, TMP1, 0);
3681        OP1(SLJIT_MOV, STR_PTR, 0, TMP1, 0);
3682        JUMPHERE(quit);
3683        }
3684      }
3685  else  else
3686    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));
3687  return TRUE;  return TRUE;
3688  }  }
3689    
# Line 2963  firstchar = CMP(SLJIT_C_LESS_EQUAL, STR_ Line 3803  firstchar = CMP(SLJIT_C_LESS_EQUAL, STR_
3803  skip_char_back(common);  skip_char_back(common);
3804    
3805  loop = LABEL();  loop = LABEL();
3806  read_char(common);  read_char_range(common, common->nlmin, common->nlmax, TRUE);
3807  lastchar = CMP(SLJIT_C_GREATER_EQUAL, STR_PTR, 0, STR_END, 0);  lastchar = CMP(SLJIT_C_GREATER_EQUAL, STR_PTR, 0, STR_END, 0);
3808  if (common->nltype == NLTYPE_ANY || common->nltype == NLTYPE_ANYCRLF)  if (common->nltype == NLTYPE_ANY || common->nltype == NLTYPE_ANYCRLF)
3809    foundcr = CMP(SLJIT_C_EQUAL, TMP1, 0, SLJIT_IMM, CHAR_CR);    foundcr = CMP(SLJIT_C_EQUAL, TMP1, 0, SLJIT_IMM, CHAR_CR);
# Line 2992  if (firstline) Line 3832  if (firstline)
3832    OP1(SLJIT_MOV, STR_END, 0, TMP3, 0);    OP1(SLJIT_MOV, STR_END, 0, TMP3, 0);
3833  }  }
3834    
3835  static BOOL check_class_ranges(compiler_common *common, const pcre_uint8 *bits, BOOL nclass, jump_list **backtracks);  static BOOL check_class_ranges(compiler_common *common, const pcre_uint8 *bits, BOOL nclass, BOOL invert, jump_list **backtracks);
3836    
3837  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)
3838  {  {
3839  DEFINE_COMPILER;  DEFINE_COMPILER;
3840  struct sljit_label *start;  struct sljit_label *start;
3841  struct sljit_jump *quit;  struct sljit_jump *quit;
3842  struct sljit_jump *found = NULL;  struct sljit_jump *found = NULL;
3843  jump_list *matches = NULL;  jump_list *matches = NULL;
 pcre_uint8 inverted_start_bits[32];  
 int i;  
3844  #ifndef COMPILE_PCRE8  #ifndef COMPILE_PCRE8
3845  struct sljit_jump *jump;  struct sljit_jump *jump;
3846  #endif  #endif
3847    
 for (i = 0; i < 32; ++i)  
   inverted_start_bits[i] = ~(((pcre_uint8*)start_bits)[i]);  
   
3848  if (firstline)  if (firstline)
3849    {    {
3850    SLJIT_ASSERT(common->first_line_end != 0);    SLJIT_ASSERT(common->first_line_end != 0);
# Line 3025  if (common->utf) Line 3860  if (common->utf)
3860    OP1(SLJIT_MOV, TMP3, 0, TMP1, 0);    OP1(SLJIT_MOV, TMP3, 0, TMP1, 0);
3861  #endif  #endif
3862    
3863  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))
3864    {    {
3865  #ifndef COMPILE_PCRE8  #ifndef COMPILE_PCRE8
3866    jump = CMP(SLJIT_C_LESS, TMP1, 0, SLJIT_IMM, 255);    jump = CMP(SLJIT_C_LESS, TMP1, 0, SLJIT_IMM, 255);
# Line 3034  if (!check_class_ranges(common, inverted Line 3869  if (!check_class_ranges(common, inverted
3869  #endif  #endif
3870    OP2(SLJIT_AND, TMP2, 0, TMP1, 0, SLJIT_IMM, 0x7);    OP2(SLJIT_AND, TMP2, 0, TMP1, 0, SLJIT_IMM, 0x7);
3871    OP2(SLJIT_LSHR, TMP1, 0, TMP1, 0, SLJIT_IMM, 3);    OP2(SLJIT_LSHR, TMP1, 0, TMP1, 0, SLJIT_IMM, 3);
3872    OP1(SLJIT_MOV_UB, TMP1, 0, SLJIT_MEM1(TMP1), start_bits);    OP1(SLJIT_MOV_UB, TMP1, 0, SLJIT_MEM1(TMP1), (sljit_sw)start_bits);
3873    OP2(SLJIT_SHL, TMP2, 0, SLJIT_IMM, 1, TMP2, 0);    OP2(SLJIT_SHL, TMP2, 0, SLJIT_IMM, 1, TMP2, 0);
3874    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);
3875    found = JUMP(SLJIT_C_NOT_ZERO);    found = JUMP(SLJIT_C_NOT_ZERO);
# Line 3178  static void check_wordboundary(compiler_ Line 4013  static void check_wordboundary(compiler_
4013  {  {
4014  DEFINE_COMPILER;  DEFINE_COMPILER;
4015  struct sljit_jump *skipread;  struct sljit_jump *skipread;
4016    jump_list *skipread_list = NULL;
4017  #if !(defined COMPILE_PCRE8) || defined SUPPORT_UTF  #if !(defined COMPILE_PCRE8) || defined SUPPORT_UTF
4018  struct sljit_jump *jump;  struct sljit_jump *jump;
4019  #endif  #endif
# Line 3235  else Line 4071  else
4071  JUMPHERE(skipread);  JUMPHERE(skipread);
4072    
4073  OP1(SLJIT_MOV, TMP2, 0, SLJIT_IMM, 0);  OP1(SLJIT_MOV, TMP2, 0, SLJIT_IMM, 0);
4074  skipread = check_str_end(common);  check_str_end(common, &skipread_list);
4075  peek_char(common);  peek_char(common, READ_CHAR_MAX);
4076    
4077  /* Testing char type. This is a code duplication. */  /* Testing char type. This is a code duplication. */
4078  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
# Line 3276  else Line 4112  else
4112      JUMPHERE(jump);      JUMPHERE(jump);
4113  #endif /* COMPILE_PCRE8 */  #endif /* COMPILE_PCRE8 */
4114    }    }
4115  JUMPHERE(skipread);  set_jumps(skipread_list, LABEL());
4116    
4117  OP2(SLJIT_XOR | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP2, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), LOCALS1);  OP2(SLJIT_XOR | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP2, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), LOCALS1);
4118  sljit_emit_fast_return(compiler, SLJIT_MEM1(SLJIT_LOCALS_REG), LOCALS0);  sljit_emit_fast_return(compiler, SLJIT_MEM1(SLJIT_LOCALS_REG), LOCALS0);
4119  }  }
4120    
4121  /*  static BOOL check_class_ranges(compiler_common *common, const pcre_uint8 *bits, BOOL nclass, BOOL invert, jump_list **backtracks)
   range format:  
   
   ranges[0] = length of the range (max MAX_RANGE_SIZE, -1 means invalid range).  
   ranges[1] = first bit (0 or 1)  
   ranges[2-length] = position of the bit change (when the current bit is not equal to the previous)  
 */  
   
 static BOOL check_ranges(compiler_common *common, int *ranges, jump_list **backtracks, BOOL readch)  
4122  {  {
4123  DEFINE_COMPILER;  DEFINE_COMPILER;
4124  struct sljit_jump *jump;  int ranges[MAX_RANGE_SIZE];
   
 if (ranges[0] < 0)  
   return FALSE;  
   
 switch(ranges[0])  
   {  
   case 1:  
   if (readch)  
     read_char(common);  
   add_jump(compiler, backtracks, CMP(ranges[1] == 0 ? SLJIT_C_LESS : SLJIT_C_GREATER_EQUAL, TMP1, 0, SLJIT_IMM, ranges[2]));  
   return TRUE;  
   
   case 2:  
   if (readch)  
     read_char(common);  
   OP2(SLJIT_SUB, TMP1, 0, TMP1, 0, SLJIT_IMM, ranges[2]);  
   add_jump(compiler, backtracks, CMP(ranges[1] != 0 ? SLJIT_C_LESS : SLJIT_C_GREATER_EQUAL, TMP1, 0, SLJIT_IMM, ranges[3] - ranges[2]));  
   return TRUE;  
   
   case 4:  
   if (ranges[2] + 1 == ranges[3] && ranges[4] + 1 == ranges[5])  
     {  
     if (readch)  
       read_char(common);  
     if (ranges[1] != 0)  
       {  
       add_jump(compiler, backtracks, CMP(SLJIT_C_EQUAL, TMP1, 0, SLJIT_IMM, ranges[2]));  
       add_jump(compiler, backtracks, CMP(SLJIT_C_EQUAL, TMP1, 0, SLJIT_IMM, ranges[4]));  
       }  
     else  
       {  
       jump = CMP(SLJIT_C_EQUAL, TMP1, 0, SLJIT_IMM, ranges[2]);  
       add_jump(compiler, backtracks, CMP(SLJIT_C_NOT_EQUAL, TMP1, 0, SLJIT_IMM, ranges[4]));  
       JUMPHERE(jump);  
       }  
     return TRUE;  
     }  
   if ((ranges[3] - ranges[2]) == (ranges[5] - ranges[4]) && is_powerof2(ranges[4] - ranges[2]))  
     {  
     if (readch)  
       read_char(common);  
     OP2(SLJIT_OR, TMP1, 0, TMP1, 0, SLJIT_IMM, ranges[4] - ranges[2]);  
     OP2(SLJIT_SUB, TMP1, 0, TMP1, 0, SLJIT_IMM, ranges[4]);  
     add_jump(compiler, backtracks, CMP(ranges[1] != 0 ? SLJIT_C_LESS : SLJIT_C_GREATER_EQUAL, TMP1, 0, SLJIT_IMM, ranges[5] - ranges[4]));  
     return TRUE;  
     }  
   return FALSE;  
   
   default:  
   return FALSE;  
   }  
 }  
   
 static void get_ctype_ranges(compiler_common *common, int flag, int *ranges)  
 {  
 int i, bit, length;  
 const pcre_uint8 *ctypes = (const pcre_uint8*)common->ctypes;  
   
 bit = ctypes[0] & flag;  
 ranges[0] = -1;  
 ranges[1] = bit != 0 ? 1 : 0;  
 length = 0;  
   
 for (i = 1; i < 256; i++)  
   if ((ctypes[i] & flag) != bit)  
     {  
     if (length >= MAX_RANGE_SIZE)  
       return;  
     ranges[2 + length] = i;  
     length++;  
     bit ^= flag;  
     }  
   
 if (bit != 0)  
   {  
   if (length >= MAX_RANGE_SIZE)  
     return;  
   ranges[2 + length] = 256;  
   length++;  
   }  
 ranges[0] = length;  
 }  
   
 static BOOL check_class_ranges(compiler_common *common, const pcre_uint8 *bits, BOOL nclass, jump_list **backtracks)  
 {  
 int ranges[2 + MAX_RANGE_SIZE];  
4125  pcre_uint8 bit, cbit, all;  pcre_uint8 bit, cbit, all;
4126  int i, byte, length = 0;  int i, byte, length = 0;
4127    
4128  bit = bits[0] & 0x1;  bit = bits[0] & 0x1;
4129  ranges[1] = bit;  /* All bits will be zero or one (since bit is zero or one). */
 /* Can be 0 or 255. */  
4130  all = -bit;  all = -bit;
4131    
4132  for (i = 0; i < 256; )  for (i = 0; i < 256; )
# Line 3400  for (i = 0; i < 256; ) Line 4141  for (i = 0; i < 256; )
4141        {        {
4142        if (length >= MAX_RANGE_SIZE)        if (length >= MAX_RANGE_SIZE)
4143          return FALSE;          return FALSE;
4144        ranges[2 + length] = i;        ranges[length] = i;
4145        length++;        length++;
4146        bit = cbit;        bit = cbit;
4147        all = -cbit;        all = -cbit;
# Line 3413  if (((bit == 0) && nclass) || ((bit == 1 Line 4154  if (((bit == 0) && nclass) || ((bit == 1
4154    {    {
4155    if (length >= MAX_RANGE_SIZE)    if (length >= MAX_RANGE_SIZE)
4156      return FALSE;      return FALSE;
4157    ranges[2 + length] = 256;    ranges[length] = 256;
4158    length++;    length++;
4159    }    }
 ranges[0] = length;  
4160    
4161  return check_ranges(common, ranges, backtracks, FALSE);  if (length < 0 || length > 4)
4162      return FALSE;
4163    
4164    bit = bits[0] & 0x1;
4165    if (invert) bit ^= 0x1;
4166    
4167    /* No character is accepted. */
4168    if (length == 0 && bit == 0)
4169      add_jump(compiler, backtracks, JUMP(SLJIT_JUMP));
4170    
4171    switch(length)
4172      {
4173      case 0:
4174      /* When bit != 0, all characters are accepted. */
4175      return TRUE;
4176    
4177      case 1:
4178      add_jump(compiler, backtracks, CMP(bit == 0 ? SLJIT_C_LESS : SLJIT_C_GREATER_EQUAL, TMP1, 0, SLJIT_IMM, ranges[0]));
4179      return TRUE;
4180    
4181      case 2:
4182      if (ranges[0] + 1 != ranges[1])
4183        {
4184        OP2(SLJIT_SUB, TMP1, 0, TMP1, 0, SLJIT_IMM, ranges[0]);
4185        add_jump(compiler, backtracks, CMP(bit != 0 ? SLJIT_C_LESS : SLJIT_C_GREATER_EQUAL, TMP1, 0, SLJIT_IMM, ranges[1] - ranges[0]));
4186        }
4187      else
4188        add_jump(compiler, backtracks, CMP(bit != 0 ? SLJIT_C_EQUAL : SLJIT_C_NOT_EQUAL, TMP1, 0, SLJIT_IMM, ranges[0]));
4189      return TRUE;
4190    
4191      case 3:
4192      if (bit != 0)
4193        {
4194        add_jump(compiler, backtracks, CMP(SLJIT_C_GREATER_EQUAL, TMP1, 0, SLJIT_IMM, ranges[2]));
4195        if (ranges[0] + 1 != ranges[1])
4196          {
4197          OP2(SLJIT_SUB, TMP1, 0, TMP1, 0, SLJIT_IMM, ranges[0]);
4198          add_jump(compiler, backtracks, CMP(SLJIT_C_LESS, TMP1, 0, SLJIT_IMM, ranges[1] - ranges[0]));
4199          }
4200        else
4201          add_jump(compiler, backtracks, CMP(SLJIT_C_EQUAL, TMP1, 0, SLJIT_IMM, ranges[0]));
4202        return TRUE;
4203        }
4204    
4205      add_jump(compiler, backtracks, CMP(SLJIT_C_LESS, TMP1, 0, SLJIT_IMM, ranges[0]));
4206      if (ranges[1] + 1 != ranges[2])
4207        {
4208        OP2(SLJIT_SUB, TMP1, 0, TMP1, 0, SLJIT_IMM, ranges[1]);
4209        add_jump(compiler, backtracks, CMP(SLJIT_C_LESS, TMP1, 0, SLJIT_IMM, ranges[2] - ranges[1]));
4210        }
4211      else
4212        add_jump(compiler, backtracks, CMP(SLJIT_C_EQUAL, TMP1, 0, SLJIT_IMM, ranges[1]));
4213      return TRUE;
4214    
4215      case 4:
4216      if ((ranges[1] - ranges[0]) == (ranges[3] - ranges[2])
4217          && (ranges[0] | (ranges[2] - ranges[0])) == ranges[2]
4218          && is_powerof2(ranges[2] - ranges[0]))
4219        {
4220        OP2(SLJIT_OR, TMP1, 0, TMP1, 0, SLJIT_IMM, ranges[2] - ranges[0]);
4221        if (ranges[2] + 1 != ranges[3])
4222          {
4223          OP2(SLJIT_SUB, TMP1, 0, TMP1, 0, SLJIT_IMM, ranges[2]);
4224          add_jump(compiler, backtracks, CMP(bit != 0 ? SLJIT_C_LESS : SLJIT_C_GREATER_EQUAL, TMP1, 0, SLJIT_IMM, ranges[3] - ranges[2]));
4225          }
4226        else
4227          add_jump(compiler, backtracks, CMP(bit != 0 ? SLJIT_C_EQUAL : SLJIT_C_NOT_EQUAL, TMP1, 0, SLJIT_IMM, ranges[2]));
4228        return TRUE;
4229        }
4230    
4231      if (bit != 0)
4232        {
4233        i = 0;
4234        if (ranges[0] + 1 != ranges[1])
4235          {
4236          OP2(SLJIT_SUB, TMP1, 0, TMP1, 0, SLJIT_IMM, ranges[0]);
4237          add_jump(compiler, backtracks, CMP(SLJIT_C_LESS, TMP1, 0, SLJIT_IMM, ranges[1] - ranges[0]));
4238          i = ranges[0];
4239          }
4240        else
4241          add_jump(compiler, backtracks, CMP(SLJIT_C_EQUAL, TMP1, 0, SLJIT_IMM, ranges[0]));
4242    
4243        if (ranges[2] + 1 != ranges[3])
4244          {
4245          OP2(SLJIT_SUB, TMP1, 0, TMP1, 0, SLJIT_IMM, ranges[2] - i);
4246          add_jump(compiler, backtracks, CMP(SLJIT_C_LESS, TMP1, 0, SLJIT_IMM, ranges[3] - ranges[2]));
4247          }
4248        else
4249          add_jump(compiler, backtracks, CMP(SLJIT_C_EQUAL, TMP1, 0, SLJIT_IMM, ranges[2] - i));
4250        return TRUE;
4251        }
4252    
4253      OP2(SLJIT_SUB, TMP1, 0, TMP1, 0, SLJIT_IMM, ranges[0]);
4254      add_jump(compiler, backtracks, CMP(SLJIT_C_GREATER_EQUAL, TMP1, 0, SLJIT_IMM, ranges[3] - ranges[0]));
4255      if (ranges[1] + 1 != ranges[2])
4256        {
4257        OP2(SLJIT_SUB, TMP1, 0, TMP1, 0, SLJIT_IMM, ranges[1] - ranges[0]);
4258        add_jump(compiler, backtracks, CMP(SLJIT_C_LESS, TMP1, 0, SLJIT_IMM, ranges[2] - ranges[1]));
4259        }
4260      else
4261        add_jump(compiler, backtracks, CMP(SLJIT_C_EQUAL, TMP1, 0, SLJIT_IMM, ranges[1] - ranges[0]));
4262      return TRUE;
4263    
4264      default:
4265      SLJIT_ASSERT_STOP();
4266      return FALSE;
4267      }
4268  }  }
4269    
4270  static void check_anynewline(compiler_common *common)  static void check_anynewline(compiler_common *common)
# Line 3785  return cc; Line 4631  return cc;
4631  #define SET_TYPE_OFFSET(value) \  #define SET_TYPE_OFFSET(value) \
4632    if ((value) != typeoffset) \    if ((value) != typeoffset) \
4633      { \      { \
4634      if ((value) > typeoffset) \      if ((value) < typeoffset) \
       OP2(SLJIT_SUB, typereg, 0, typereg, 0, SLJIT_IMM, (value) - typeoffset); \  
     else \  
4635        OP2(SLJIT_ADD, typereg, 0, typereg, 0, SLJIT_IMM, typeoffset - (value)); \        OP2(SLJIT_ADD, typereg, 0, typereg, 0, SLJIT_IMM, typeoffset - (value)); \
4636        else \
4637          OP2(SLJIT_SUB, typereg, 0, typereg, 0, SLJIT_IMM, (value) - typeoffset); \
4638      } \      } \
4639    typeoffset = (value);    typeoffset = (value);
4640    
4641  #define SET_CHAR_OFFSET(value) \  #define SET_CHAR_OFFSET(value) \
4642    if ((value) != charoffset) \    if ((value) != charoffset) \
4643      { \      { \
4644      if ((value) > charoffset) \      if ((value) < charoffset) \
4645        OP2(SLJIT_SUB, TMP1, 0, TMP1, 0, SLJIT_IMM, (value) - charoffset); \        OP2(SLJIT_ADD, TMP1, 0, TMP1, 0, SLJIT_IMM, (sljit_sw)(charoffset - (value))); \
4646      else \      else \
4647        OP2(SLJIT_ADD, TMP1, 0, TMP1, 0, SLJIT_IMM, charoffset - (value)); \        OP2(SLJIT_SUB, TMP1, 0, TMP1, 0, SLJIT_IMM, (sljit_sw)((value) - charoffset)); \
4648      } \      } \
4649    charoffset = (value);    charoffset = (value);
4650    
# Line 3806  static void compile_xclass_matchingpath( Line 4652  static void compile_xclass_matchingpath(
4652  {  {
4653  DEFINE_COMPILER;  DEFINE_COMPILER;
4654  jump_list *found = NULL;  jump_list *found = NULL;
4655  jump_list **list = (*cc & XCL_NOT) == 0 ? &found : backtracks;  jump_list **list = (cc[0] & XCL_NOT) == 0 ? &found : backtracks;
4656  pcre_int32 c, charoffset;  sljit_uw c, charoffset, max = 256, min = READ_CHAR_MAX;
 const pcre_uint32 *other_cases;  
4657  struct sljit_jump *jump = NULL;  struct sljit_jump *jump = NULL;
4658  pcre_uchar *ccbegin;  pcre_uchar *ccbegin;
4659  int compares, invertcmp, numberofcmps;  int compares, invertcmp, numberofcmps;
4660    #if defined SUPPORT_UTF && (defined COMPILE_PCRE8 || defined COMPILE_PCRE16)
4661    BOOL utf = common->utf;
4662    #endif
4663    
4664  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
4665  BOOL needstype = FALSE, needsscript = FALSE, needschar = FALSE;  BOOL needstype = FALSE, needsscript = FALSE, needschar = FALSE;
4666  BOOL charsaved = FALSE;  BOOL charsaved = FALSE;
4667  int typereg = TMP1, scriptreg = TMP1;  int typereg = TMP1, scriptreg = TMP1;
4668  pcre_int32 typeoffset;  const pcre_uint32 *other_cases;
4669    sljit_uw typeoffset;
4670  #endif  #endif
4671    
4672  /* Although SUPPORT_UTF must be defined, we are  /* Scanning the necessary info. */
4673     not necessary in utf mode even in 8 bit mode. */  cc++;
4674  detect_partial_match(common, backtracks);  ccbegin = cc;
4675  read_char(common);  compares = 0;
4676    if (cc[-1] & XCL_MAP)
 if ((*cc++ & XCL_MAP) != 0)  
4677    {    {
4678    OP1(SLJIT_MOV, TMP3, 0, TMP1, 0);    min = 0;
 #ifndef COMPILE_PCRE8  
   jump = CMP(SLJIT_C_GREATER, TMP1, 0, SLJIT_IMM, 255);  
 #elif defined SUPPORT_UTF  
   if (common->utf)  
     jump = CMP(SLJIT_C_GREATER, TMP1, 0, SLJIT_IMM, 255);  
 #endif  
   
   if (!check_class_ranges(common, (const pcre_uint8 *)cc, TRUE, list))  
     {  
     OP2(SLJIT_AND, TMP2, 0, TMP1, 0, SLJIT_IMM, 0x7);  
     OP2(SLJIT_LSHR, TMP1, 0, TMP1, 0, SLJIT_IMM, 3);  
     OP1(SLJIT_MOV_UB, TMP1, 0, SLJIT_MEM1(TMP1), (sljit_sw)cc);  
     OP2(SLJIT_SHL, TMP2, 0, SLJIT_IMM, 1, TMP2, 0);  
     OP2(SLJIT_AND | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, TMP2, 0);  
     add_jump(compiler, list, JUMP(SLJIT_C_NOT_ZERO));  
     }  
   
 #ifndef COMPILE_PCRE8  
   JUMPHERE(jump);  
 #elif defined SUPPORT_UTF  
   if (common->utf)  
     JUMPHERE(jump);  
 #endif  
   OP1(SLJIT_MOV, TMP1, 0, TMP3, 0);  
 #ifdef SUPPORT_UCP  
   charsaved = TRUE;  
 #endif  
4679    cc += 32 / sizeof(pcre_uchar);    cc += 32 / sizeof(pcre_uchar);
4680    }    }
4681    
 /* Scanning the necessary info. */  
 ccbegin = cc;  
 compares = 0;  
4682  while (*cc != XCL_END)  while (*cc != XCL_END)
4683    {    {
4684    compares++;    compares++;
4685    if (*cc == XCL_SINGLE)    if (*cc == XCL_SINGLE)
4686      {      {
4687      cc += 2;      cc ++;
4688  #ifdef SUPPORT_UTF      GETCHARINCTEST(c, cc);
4689      if (common->utf && HAS_EXTRALEN(cc[-1])) cc += GET_EXTRALEN(cc[-1]);      if (c > max) max = c;
4690  #endif      if (c < min) min = c;
4691  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
4692      needschar = TRUE;      needschar = TRUE;
4693  #endif  #endif
4694      }      }
4695    else if (*cc == XCL_RANGE)    else if (*cc == XCL_RANGE)
4696      {      {
4697      cc += 2;      cc ++;
4698  #ifdef SUPPORT_UTF      GETCHARINCTEST(c, cc);
4699      if (common->utf && HAS_EXTRALEN(cc[-1])) cc += GET_EXTRALEN(cc[-1]);      if (c < min) min = c;
4700  #endif      GETCHARINCTEST(c, cc);
4701      cc++;      if (c > max) max = c;
 #ifdef SUPPORT_UTF  
     if (common->utf && HAS_EXTRALEN(cc[-1])) cc += GET_EXTRALEN(cc[-1]);  
 #endif  
4702  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
4703      needschar = TRUE;      needschar = TRUE;
4704  #endif  #endif
# Line 3892  while (*cc != XCL_END) Line 4708  while (*cc != XCL_END)
4708      {      {
4709      SLJIT_ASSERT(*cc == XCL_PROP || *cc == XCL_NOTPROP);      SLJIT_ASSERT(*cc == XCL_PROP || *cc == XCL_NOTPROP);
4710      cc++;      cc++;
4711        if (*cc == PT_CLIST)
4712          {
4713          other_cases = PRIV(ucd_caseless_sets) + cc[1];
4714          while (*other_cases != NOTACHAR)
4715            {
4716            if (*other_cases > max) max = *other_cases;
4717            if (*other_cases < min) min = *other_cases;
4718            other_cases++;
4719            }
4720          }
4721        else
4722          {
4723          max = READ_CHAR_MAX;
4724          min = 0;
4725          }
4726    
4727      switch(*cc)      switch(*cc)
4728        {        {
4729        case PT_ANY:        case PT_ANY:
# Line 3911  while (*cc != XCL_END) Line 4743  while (*cc != XCL_END)
4743        case PT_SPACE:        case PT_SPACE:
4744        case PT_PXSPACE:        case PT_PXSPACE:
4745        case PT_WORD:        case PT_WORD:
4746          case PT_PXGRAPH:
4747          case PT_PXPRINT:
4748          case PT_PXPUNCT:
4749        needstype = TRUE;        needstype = TRUE;
4750        needschar = TRUE;        needschar = TRUE;
4751        break;        break;
# Line 3929  while (*cc != XCL_END) Line 4764  while (*cc != XCL_END)
4764  #endif  #endif
4765    }    }
4766    
4767    /* We are not necessary in utf mode even in 8 bit mode. */
4768    cc = ccbegin;
4769    detect_partial_match(common, backtracks);
4770    read_char_range(common, min, max, (cc[-1] & XCL_NOT) != 0);
4771    
4772    if ((cc[-1] & XCL_HASPROP) == 0)
4773      {
4774      if ((cc[-1] & XCL_MAP) != 0)
4775        {
4776        jump = CMP(SLJIT_C_GREATER, TMP1, 0, SLJIT_IMM, 255);
4777        if (!check_class_ranges(common, (const pcre_uint8 *)cc, (((const pcre_uint8 *)cc)[31] & 0x80) != 0, TRUE, &found))
4778          {
4779          OP2(SLJIT_AND, TMP2, 0, TMP1, 0, SLJIT_IMM, 0x7);
4780          OP2(SLJIT_LSHR, TMP1, 0, TMP1, 0, SLJIT_IMM, 3);
4781          OP1(SLJIT_MOV_UB, TMP1, 0, SLJIT_MEM1(TMP1), (sljit_sw)cc);
4782          OP2(SLJIT_SHL, TMP2, 0, SLJIT_IMM, 1, TMP2, 0);
4783          OP2(SLJIT_AND | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, TMP2, 0);
4784          add_jump(compiler, &found, JUMP(SLJIT_C_NOT_ZERO));
4785          }
4786    
4787        add_jump(compiler, backtracks, JUMP(SLJIT_JUMP));
4788        JUMPHERE(jump);
4789    
4790        cc += 32 / sizeof(pcre_uchar);
4791        }
4792      else
4793        {
4794        OP2(SLJIT_SUB, TMP2, 0, TMP1, 0, SLJIT_IMM, min);
4795        add_jump(compiler, (cc[-1] & XCL_NOT) == 0 ? backtracks : &found, CMP(SLJIT_C_GREATER, TMP2, 0, SLJIT_IMM, max - min));
4796        }
4797      }
4798    else if ((cc[-1] & XCL_MAP) != 0)
4799      {
4800      OP1(SLJIT_MOV, TMP3, 0, TMP1, 0);
4801    #ifdef SUPPORT_UCP
4802      charsaved = TRUE;
4803    #endif
4804      if (!check_class_ranges(common, (const pcre_uint8 *)cc, FALSE, TRUE, list))
4805        {
4806    #ifdef COMPILE_PCRE8
4807        SLJIT_ASSERT(common->utf);
4808    #endif
4809        jump = CMP(SLJIT_C_GREATER, TMP1, 0, SLJIT_IMM, 255);
4810    
4811        OP2(SLJIT_AND, TMP2, 0, TMP1, 0, SLJIT_IMM, 0x7);
4812        OP2(SLJIT_LSHR, TMP1, 0, TMP1, 0, SLJIT_IMM, 3);
4813        OP1(SLJIT_MOV_UB, TMP1, 0, SLJIT_MEM1(TMP1), (sljit_sw)cc);
4814        OP2(SLJIT_SHL, TMP2, 0, SLJIT_IMM, 1, TMP2, 0);
4815        OP2(SLJIT_AND | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, TMP2, 0);
4816        add_jump(compiler, list, JUMP(SLJIT_C_NOT_ZERO));
4817    
4818        JUMPHERE(jump);
4819        }
4820    
4821      OP1(SLJIT_MOV, TMP1, 0, TMP3, 0);
4822      cc += 32 / sizeof(pcre_uchar);
4823      }
4824    
4825  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
4826  /* Simple register allocation. TMP1 is preferred if possible. */  /* Simple register allocation. TMP1 is preferred if possible. */
4827  if (needstype || needsscript)  if (needstype || needsscript)
# Line 3970  if (needstype || needsscript) Line 4863  if (needstype || needsscript)
4863  #endif  #endif
4864    
4865  /* Generating code. */  /* Generating code. */
 cc = ccbegin;  
4866  charoffset = 0;  charoffset = 0;
4867  numberofcmps = 0;  numberofcmps = 0;
4868  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
# Line 3986  while (*cc != XCL_END) Line 4878  while (*cc != XCL_END)
4878    if (*cc == XCL_SINGLE)    if (*cc == XCL_SINGLE)
4879      {      {
4880      cc ++;      cc ++;
4881  #ifdef SUPPORT_UTF      GETCHARINCTEST(c, cc);
     if (common->utf)  
       {  
       GETCHARINC(c, cc);  
       }  
     else  
 #endif  
       c = *cc++;  
4882    
4883      if (numberofcmps < 3 && (*cc == XCL_SINGLE || *cc == XCL_RANGE))      if (numberofcmps < 3 && (*cc == XCL_SINGLE || *cc == XCL_RANGE))
4884        {        {
4885        OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, c - charoffset);        OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, (sljit_sw)(c - charoffset));
4886        OP_FLAGS(numberofcmps == 0 ? SLJIT_MOV : SLJIT_OR, TMP2, 0, numberofcmps == 0 ? SLJIT_UNUSED : TMP2, 0, SLJIT_C_EQUAL);        OP_FLAGS(numberofcmps == 0 ? SLJIT_MOV : SLJIT_OR, TMP2, 0, numberofcmps == 0 ? SLJIT_UNUSED : TMP2, 0, SLJIT_C_EQUAL);
4887        numberofcmps++;        numberofcmps++;
4888        }        }
4889      else if (numberofcmps > 0)      else if (numberofcmps > 0)
4890        {        {
4891        OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, c - charoffset);        OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, (sljit_sw)(c - charoffset));
4892        OP_FLAGS(SLJIT_OR | SLJIT_SET_E, TMP2, 0, TMP2, 0, SLJIT_C_EQUAL);        OP_FLAGS(SLJIT_OR | SLJIT_SET_E, TMP2, 0, TMP2, 0, SLJIT_C_EQUAL);
4893        jump = JUMP(SLJIT_C_NOT_ZERO ^ invertcmp);        jump = JUMP(SLJIT_C_NOT_ZERO ^ invertcmp);
4894        numberofcmps = 0;        numberofcmps = 0;
4895        }        }
4896      else      else
4897        {        {
4898        jump = CMP(SLJIT_C_EQUAL ^ invertcmp, TMP1, 0, SLJIT_IMM, c - charoffset);        jump = CMP(SLJIT_C_EQUAL ^ invertcmp, TMP1, 0, SLJIT_IMM, (sljit_sw)(c - charoffset));
4899        numberofcmps = 0;        numberofcmps = 0;
       }  
     }  
   else if (*cc == XCL_RANGE)  
     {  
     cc ++;  
 #ifdef SUPPORT_UTF  
     if (common->utf)  
       {  
       GETCHARINC(c, cc);  
       }  
     else  
 #endif  
       c = *cc++;  
     SET_CHAR_OFFSET(c);  
 #ifdef SUPPORT_UTF  
     if (common->utf)  
       {  
       GETCHARINC(c, cc);  
4900        }        }
4901      else      }
4902  #endif    else if (*cc == XCL_RANGE)
4903        c = *cc++;      {
4904        cc ++;
4905        GETCHARINCTEST(c, cc);
4906        SET_CHAR_OFFSET(c);
4907        GETCHARINCTEST(c, cc);
4908    
4909      if (numberofcmps < 3 && (*cc == XCL_SINGLE || *cc == XCL_RANGE))      if (numberofcmps < 3 && (*cc == XCL_SINGLE || *cc == XCL_RANGE))
4910        {        {
4911        OP2(SLJIT_SUB | SLJIT_SET_U, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, c - charoffset);        OP2(SLJIT_SUB | SLJIT_SET_U, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, (sljit_sw)(c - charoffset));
4912        OP_FLAGS(numberofcmps == 0 ? SLJIT_MOV : SLJIT_OR, TMP2, 0, numberofcmps == 0 ? SLJIT_UNUSED : TMP2, 0, SLJIT_C_LESS_EQUAL);        OP_FLAGS(numberofcmps == 0 ? SLJIT_MOV : SLJIT_OR, TMP2, 0, numberofcmps == 0 ? SLJIT_UNUSED : TMP2, 0, SLJIT_C_LESS_EQUAL);
4913        numberofcmps++;        numberofcmps++;
4914        }        }
4915      else if (numberofcmps > 0)      else if (numberofcmps > 0)
4916        {        {
4917        OP2(SLJIT_SUB | SLJIT_SET_U, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, c - charoffset);        OP2(SLJIT_SUB | SLJIT_SET_U, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, (sljit_sw)(c - charoffset));
4918        OP_FLAGS(SLJIT_OR | SLJIT_SET_E, TMP2, 0, TMP2, 0, SLJIT_C_LESS_EQUAL);        OP_FLAGS(SLJIT_OR | SLJIT_SET_E, TMP2, 0, TMP2, 0, SLJIT_C_LESS_EQUAL);
4919        jump = JUMP(SLJIT_C_NOT_ZERO ^ invertcmp);        jump = JUMP(SLJIT_C_NOT_ZERO ^ invertcmp);
4920        numberofcmps = 0;        numberofcmps = 0;
4921        }        }
4922      else      else
4923        {        {
4924        jump = CMP(SLJIT_C_LESS_EQUAL ^ invertcmp, TMP1, 0, SLJIT_IMM, c - charoffset);        jump = CMP(SLJIT_C_LESS_EQUAL ^ invertcmp, TMP1, 0, SLJIT_IMM, (sljit_sw)(c - charoffset));
4925        numberofcmps = 0;        numberofcmps = 0;
4926        }        }
4927      }      }
# Line 4098  while (*cc != XCL_END) Line 4970  while (*cc != XCL_END)
4970    
4971        case PT_SPACE:        case PT_SPACE:
4972        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);  
         }  
4973        SET_CHAR_OFFSET(9);        SET_CHAR_OFFSET(9);
4974        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);
4975        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);
4976        if (*cc == PT_SPACE)  
4977          JUMPHERE(jump);        OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x85 - 0x9);
4978          OP_FLAGS(SLJIT_OR | SLJIT_SET_E, TMP2, 0, TMP2, 0, SLJIT_C_EQUAL);
4979    
4980          OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x180e - 0x9);
4981          OP_FLAGS(SLJIT_OR | SLJIT_SET_E, TMP2, 0, TMP2, 0, SLJIT_C_EQUAL);
4982    
4983        SET_TYPE_OFFSET(ucp_Zl);        SET_TYPE_OFFSET(ucp_Zl);
4984        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 4116  while (*cc != XCL_END) Line 4987  while (*cc != XCL_END)
4987        break;        break;
4988    
4989        case PT_WORD:        case PT_WORD:
4990        OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, CHAR_UNDERSCORE - charoffset);        OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, (sljit_sw)(CHAR_UNDERSCORE - charoffset));
4991        OP_FLAGS(SLJIT_MOV, TMP2, 0, SLJIT_UNUSED, 0, SLJIT_C_EQUAL);        OP_FLAGS(SLJIT_MOV, TMP2, 0, SLJIT_UNUSED, 0, SLJIT_C_EQUAL);
4992        /* Fall through. */        /* Fall through. */
4993    
# Line 4164  while (*cc != XCL_END) Line 5035  while (*cc != XCL_END)
5035          OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP2, 0, SLJIT_IMM, other_cases[2]);          OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP2, 0, SLJIT_IMM, other_cases[2]);
5036          OP_FLAGS(SLJIT_MOV, TMP2, 0, SLJIT_UNUSED, 0, SLJIT_C_EQUAL);          OP_FLAGS(SLJIT_MOV, TMP2, 0, SLJIT_UNUSED, 0, SLJIT_C_EQUAL);
5037    
5038          OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, other_cases[0] - charoffset);          OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, (sljit_sw)(other_cases[0] - charoffset));
5039          OP_FLAGS(SLJIT_OR | ((other_cases[3] == NOTACHAR) ? SLJIT_SET_E : 0), TMP2, 0, TMP2, 0, SLJIT_C_EQUAL);          OP_FLAGS(SLJIT_OR | ((other_cases[3] == NOTACHAR) ? SLJIT_SET_E : 0), TMP2, 0, TMP2, 0, SLJIT_C_EQUAL);
5040    
5041          other_cases += 3;          other_cases += 3;
5042          }          }
5043        else        else
5044          {          {
5045          OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, *other_cases++ - charoffset);          OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, (sljit_sw)(*other_cases++ - charoffset));
5046          OP_FLAGS(SLJIT_MOV, TMP2, 0, SLJIT_UNUSED, 0, SLJIT_C_EQUAL);          OP_FLAGS(SLJIT_MOV, TMP2, 0, SLJIT_UNUSED, 0, SLJIT_C_EQUAL);
5047          }          }
5048    
5049        while (*other_cases != NOTACHAR)        while (*other_cases != NOTACHAR)
5050          {          {
5051          OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, *other_cases++ - charoffset);          OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, (sljit_sw)(*other_cases++ - charoffset));
5052          OP_FLAGS(SLJIT_OR | ((*other_cases == NOTACHAR) ? SLJIT_SET_E : 0), TMP2, 0, TMP2, 0, SLJIT_C_EQUAL);          OP_FLAGS(SLJIT_OR | ((*other_cases == NOTACHAR) ? SLJIT_SET_E : 0), TMP2, 0, TMP2, 0, SLJIT_C_EQUAL);
5053          }          }
5054        jump = JUMP(SLJIT_C_NOT_ZERO ^ invertcmp);        jump = JUMP(SLJIT_C_NOT_ZERO ^ invertcmp);
5055        break;        break;
5056    
5057        case PT_UCNC:        case PT_UCNC:
5058        OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, CHAR_DOLLAR_SIGN - charoffset);        OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, (sljit_sw)(CHAR_DOLLAR_SIGN - charoffset));
5059        OP_FLAGS(SLJIT_MOV, TMP2, 0, SLJIT_UNUSED, 0, SLJIT_C_EQUAL);        OP_FLAGS(SLJIT_MOV, TMP2, 0, SLJIT_UNUSED, 0, SLJIT_C_EQUAL);
5060        OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, CHAR_COMMERCIAL_AT - charoffset);        OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, (sljit_sw)(CHAR_COMMERCIAL_AT - charoffset));
5061        OP_FLAGS(SLJIT_OR, TMP2, 0, TMP2, 0, SLJIT_C_EQUAL);        OP_FLAGS(SLJIT_OR, TMP2, 0, TMP2, 0, SLJIT_C_EQUAL);
5062        OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, CHAR_GRAVE_ACCENT - charoffset);        OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, (sljit_sw)(CHAR_GRAVE_ACCENT - charoffset));
5063        OP_FLAGS(SLJIT_OR, TMP2, 0, TMP2, 0, SLJIT_C_EQUAL);        OP_FLAGS(SLJIT_OR, TMP2, 0, TMP2, 0, SLJIT_C_EQUAL);
5064    
5065        SET_CHAR_OFFSET(0xa0);        SET_CHAR_OFFSET(0xa0);
5066        OP2(SLJIT_SUB | SLJIT_SET_U, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0xd7ff - charoffset);        OP2(SLJIT_SUB | SLJIT_SET_U, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, (sljit_sw)(0xd7ff - charoffset));
5067        OP_FLAGS(SLJIT_OR, TMP2, 0, TMP2, 0, SLJIT_C_LESS_EQUAL);        OP_FLAGS(SLJIT_OR, TMP2, 0, TMP2, 0, SLJIT_C_LESS_EQUAL);
5068        SET_CHAR_OFFSET(0);        SET_CHAR_OFFSET(0);
5069        OP2(SLJIT_SUB | SLJIT_SET_U, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0xe000 - 0);        OP2(SLJIT_SUB | SLJIT_SET_U, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0xe000 - 0);
5070        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);
5071        jump = JUMP(SLJIT_C_NOT_ZERO ^ invertcmp);        jump = JUMP(SLJIT_C_NOT_ZERO ^ invertcmp);
5072        break;        break;
5073    
5074          case PT_PXGRAPH:
5075          /* C and Z groups are the farthest two groups. */
5076          SET_TYPE_OFFSET(ucp_Ll);
5077          OP2(SLJIT_SUB | SLJIT_SET_U, SLJIT_UNUSED, 0, typereg, 0, SLJIT_IMM, ucp_So - ucp_Ll);
5078          OP_FLAGS(SLJIT_MOV, TMP2, 0, SLJIT_UNUSED, 0, SLJIT_C_GREATER);
5079    
5080          jump = CMP(SLJIT_C_NOT_EQUAL, typereg, 0, SLJIT_IMM, ucp_Cf - ucp_Ll);
5081    
5082          /* In case of ucp_Cf, we overwrite the result. */
5083          SET_CHAR_OFFSET(0x2066);
5084          OP2(SLJIT_SUB | SLJIT_SET_U, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x2069 - 0x2066);
5085          OP_FLAGS(SLJIT_MOV, TMP2, 0, SLJIT_UNUSED, 0, SLJIT_C_LESS_EQUAL);
5086    
5087          OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x061c - 0x2066);
5088          OP_FLAGS(SLJIT_OR, TMP2, 0, TMP2, 0, SLJIT_C_EQUAL);
5089    
5090          OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x180e - 0x2066);
5091          OP_FLAGS(SLJIT_OR, TMP2, 0, TMP2, 0, SLJIT_C_EQUAL);
5092    
5093          JUMPHERE(jump);
5094          jump = CMP(SLJIT_C_ZERO ^ invertcmp, TMP2, 0, SLJIT_IMM, 0);
5095          break;
5096    
5097          case PT_PXPRINT:
5098          /* C and Z groups are the farthest two groups. */
5099          SET_TYPE_OFFSET(ucp_Ll);
5100          OP2(SLJIT_SUB | SLJIT_SET_U, SLJIT_UNUSED, 0, typereg, 0, SLJIT_IMM, ucp_So - ucp_Ll);
5101          OP_FLAGS(SLJIT_MOV, TMP2, 0, SLJIT_UNUSED, 0, SLJIT_C_GREATER);
5102    
5103          OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, typereg, 0, SLJIT_IMM, ucp_Zs - ucp_Ll);
5104          OP_FLAGS(SLJIT_AND, TMP2, 0, TMP2, 0, SLJIT_C_NOT_EQUAL);
5105    
5106          jump = CMP(SLJIT_C_NOT_EQUAL, typereg, 0, SLJIT_IMM, ucp_Cf - ucp_Ll);
5107    
5108          /* In case of ucp_Cf, we overwrite the result. */
5109          SET_CHAR_OFFSET(0x2066);
5110          OP2(SLJIT_SUB | SLJIT_SET_U, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x2069 - 0x2066);
5111          OP_FLAGS(SLJIT_MOV, TMP2, 0, SLJIT_UNUSED, 0, SLJIT_C_LESS_EQUAL);
5112    
5113          OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x061c - 0x2066);
5114          OP_FLAGS(SLJIT_OR, TMP2, 0, TMP2, 0, SLJIT_C_EQUAL);
5115    
5116          JUMPHERE(jump);
5117          jump = CMP(SLJIT_C_ZERO ^ invertcmp, TMP2, 0, SLJIT_IMM, 0);
5118          break;
5119    
5120          case PT_PXPUNCT:
5121          SET_TYPE_OFFSET(ucp_Sc);
5122          OP2(SLJIT_SUB | SLJIT_SET_U, SLJIT_UNUSED, 0, typereg, 0, SLJIT_IMM, ucp_So - ucp_Sc);
5123          OP_FLAGS(SLJIT_MOV, TMP2, 0, SLJIT_UNUSED, 0, SLJIT_C_LESS_EQUAL);
5124    
5125          SET_CHAR_OFFSET(0);
5126          OP2(SLJIT_SUB | SLJIT_SET_U, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0xff);
5127          OP_FLAGS(SLJIT_AND, TMP2, 0, TMP2, 0, SLJIT_C_LESS_EQUAL);
5128    
5129          SET_TYPE_OFFSET(ucp_Pc);
5130          OP2(SLJIT_SUB | SLJIT_SET_U, SLJIT_UNUSED, 0, typereg, 0, SLJIT_IMM, ucp_Ps - ucp_Pc);
5131          OP_FLAGS(SLJIT_OR | SLJIT_SET_E, TMP2, 0, TMP2, 0, SLJIT_C_LESS_EQUAL);
5132          jump = JUMP(SLJIT_C_NOT_ZERO ^ invertcmp);
5133          break;
5134        }        }
5135      cc += 2;      cc += 2;
5136      }      }
# Line 4224  int length; Line 5156  int length;
5156  unsigned int c, oc, bit;  unsigned int c, oc, bit;
5157  compare_context context;  compare_context context;
5158  struct sljit_jump *jump[4];  struct sljit_jump *jump[4];
5159    jump_list *end_list;
5160  #ifdef SUPPORT_UTF  #ifdef SUPPORT_UTF
5161  struct sljit_label *label;  struct sljit_label *label;
5162  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
5163  pcre_uchar propdata[5];  pcre_uchar propdata[5];
5164  #endif  #endif
5165  #endif  #endif /* SUPPORT_UTF */
5166    
5167  switch(type)  switch(type)
5168    {    {
# Line 4254  switch(type) Line 5187  switch(type)
5187    case OP_NOT_DIGIT:    case OP_NOT_DIGIT:
5188    case OP_DIGIT:    case OP_DIGIT:
5189    /* 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);  
5190    detect_partial_match(common, backtracks);    detect_partial_match(common, backtracks);
5191    /* Flip the starting bit in the negative case. */  #if defined SUPPORT_UTF && defined COMPILE_PCRE8
5192    if (type == OP_NOT_DIGIT)    if (common->utf && is_char7_bitset((const pcre_uint8*)common->ctypes - cbit_length + cbit_digit, FALSE))
5193      common->digits[1] ^= 1;      read_char7_type(common, type == OP_NOT_DIGIT);
5194    if (!check_ranges(common, common->digits, backtracks, TRUE))    else
5195      {  #endif
5196      read_char8_type(common);      read_char8_type(common, type == OP_NOT_DIGIT);
5197      OP2(SLJIT_AND | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, ctype_digit);      /* Flip the starting bit in the negative case. */
5198      add_jump(compiler, backtracks, JUMP(type == OP_DIGIT ? SLJIT_C_ZERO : SLJIT_C_NOT_ZERO));    OP2(SLJIT_AND | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, ctype_digit);
5199      }    add_jump(compiler, backtracks, JUMP(type == OP_DIGIT ? SLJIT_C_ZERO : SLJIT_C_NOT_ZERO));
   if (type == OP_NOT_DIGIT)  
     common->digits[1] ^= 1;  
5200    return cc;    return cc;
5201    
5202    case OP_NOT_WHITESPACE:    case OP_NOT_WHITESPACE:
5203    case OP_WHITESPACE:    case OP_WHITESPACE:
5204    detect_partial_match(common, backtracks);    detect_partial_match(common, backtracks);
5205    read_char8_type(common);  #if defined SUPPORT_UTF && defined COMPILE_PCRE8
5206      if (common->utf && is_char7_bitset((const pcre_uint8*)common->ctypes - cbit_length + cbit_space, FALSE))
5207        read_char7_type(common, type == OP_NOT_WHITESPACE);
5208      else
5209    #endif
5210        read_char8_type(common, type == OP_NOT_WHITESPACE);
5211    OP2(SLJIT_AND | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, ctype_space);    OP2(SLJIT_AND | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, ctype_space);
5212    add_jump(compiler, backtracks, JUMP(type == OP_WHITESPACE ? SLJIT_C_ZERO : SLJIT_C_NOT_ZERO));    add_jump(compiler, backtracks, JUMP(type == OP_WHITESPACE ? SLJIT_C_ZERO : SLJIT_C_NOT_ZERO));
5213    return cc;    return cc;
# Line 4281  switch(type) Line 5215  switch(type)
5215    case OP_NOT_WORDCHAR:    case OP_NOT_WORDCHAR:
5216    case OP_WORDCHAR:    case OP_WORDCHAR:
5217    detect_partial_match(common, backtracks);    detect_partial_match(common, backtracks);
5218    read_char8_type(common);  #if defined SUPPORT_UTF && defined COMPILE_PCRE8
5219      if (common->utf && is_char7_bitset((const pcre_uint8*)common->ctypes - cbit_length + cbit_word, FALSE))
5220        read_char7_type(common, type == OP_NOT_WORDCHAR);
5221      else
5222    #endif
5223        read_char8_type(common, type == OP_NOT_WORDCHAR);
5224    OP2(SLJIT_AND | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, ctype_word);    OP2(SLJIT_AND | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, ctype_word);
5225    add_jump(compiler, backtracks, JUMP(type == OP_WORDCHAR ? SLJIT_C_ZERO : SLJIT_C_NOT_ZERO));    add_jump(compiler, backtracks, JUMP(type == OP_WORDCHAR ? SLJIT_C_ZERO : SLJIT_C_NOT_ZERO));
5226    return cc;    return cc;
5227    
5228    case OP_ANY:    case OP_ANY:
5229    detect_partial_match(common, backtracks);    detect_partial_match(common, backtracks);
5230    read_char(common);    read_char_range(common, common->nlmin, common->nlmax, TRUE);
5231    if (common->nltype == NLTYPE_FIXED && common->newline > 255)    if (common->nltype == NLTYPE_FIXED && common->newline > 255)
5232      {      {
5233      jump[0] = CMP(SLJIT_C_NOT_EQUAL, TMP1, 0, SLJIT_IMM, (common->newline >> 8) & 0xff);      jump[0] = CMP(SLJIT_C_NOT_EQUAL, TMP1, 0, SLJIT_IMM, (common->newline >> 8) & 0xff);
5234        end_list = NULL;
5235      if (common->mode != JIT_PARTIAL_HARD_COMPILE)      if (common->mode != JIT_PARTIAL_HARD_COMPILE)
5236        jump[1] = CMP(SLJIT_C_GREATER_EQUAL, STR_PTR, 0, STR_END, 0);        add_jump(compiler, &end_list, CMP(SLJIT_C_GREATER_EQUAL, STR_PTR, 0, STR_END, 0));
5237      else      else
5238        jump[1] = check_str_end(common);        check_str_end(common, &end_list);
5239    
5240      OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), 0);      OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), 0);
5241      add_jump(compiler, backtracks, CMP(SLJIT_C_EQUAL, TMP1, 0, SLJIT_IMM, common->newline & 0xff));      add_jump(compiler, backtracks, CMP(SLJIT_C_EQUAL, TMP1, 0, SLJIT_IMM, common->newline & 0xff));
5242      if (jump[1] != NULL)      set_jumps(end_list, LABEL());
       JUMPHERE(jump[1]);  
5243      JUMPHERE(jump[0]);      JUMPHERE(jump[0]);
5244      }      }
5245    else    else
# Line 4344  switch(type) Line 5283  switch(type)
5283  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
5284    case OP_NOTPROP:    case OP_NOTPROP:
5285    case OP_PROP:    case OP_PROP:
5286    propdata[0] = 0;    propdata[0] = XCL_HASPROP;
5287    propdata[1] = type == OP_NOTPROP ? XCL_NOTPROP : XCL_PROP;    propdata[1] = type == OP_NOTPROP ? XCL_NOTPROP : XCL_PROP;
5288    propdata[2] = cc[0];    propdata[2] = cc[0];
5289    propdata[3] = cc[1];    propdata[3] = cc[1];
# Line 4356  switch(type) Line 5295  switch(type)
5295    
5296    case OP_ANYNL:    case OP_ANYNL:
5297    detect_partial_match(common, backtracks);    detect_partial_match(common, backtracks);
5298    read_char(common);    read_char_range(common, common->bsr_nlmin, common->bsr_nlmax, FALSE);
5299    jump[0] = CMP(SLJIT_C_NOT_EQUAL, TMP1, 0, SLJIT_IMM, CHAR_CR);    jump[0] = CMP(SLJIT_C_NOT_EQUAL, TMP1, 0, SLJIT_IMM, CHAR_CR);
5300    /* We don't need to handle soft partial matching case. */    /* We don't need to handle soft partial matching case. */
5301      end_list = NULL;
5302    if (common->mode != JIT_PARTIAL_HARD_COMPILE)    if (common->mode != JIT_PARTIAL_HARD_COMPILE)
5303      jump[1] = CMP(SLJIT_C_GREATER_EQUAL, STR_PTR, 0, STR_END, 0);      add_jump(compiler, &end_list, CMP(SLJIT_C_GREATER_EQUAL, STR_PTR, 0, STR_END, 0));
5304    else    else
5305      jump[1] = check_str_end(common);      check_str_end(common, &end_list);
5306    OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), 0);    OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), 0);
5307    jump[2] = CMP(SLJIT_C_NOT_EQUAL, TMP1, 0, SLJIT_IMM, CHAR_NL);    jump[1] = CMP(SLJIT_C_NOT_EQUAL, TMP1, 0, SLJIT_IMM, CHAR_NL);
5308    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));
5309    jump[3] = JUMP(SLJIT_JUMP);    jump[2] = JUMP(SLJIT_JUMP);
5310    JUMPHERE(jump[0]);    JUMPHERE(jump[0]);
5311    check_newlinechar(common, common->bsr_nltype, backtracks, FALSE);    check_newlinechar(common, common->bsr_nltype, backtracks, FALSE);
5312      set_jumps(end_list, LABEL());
5313    JUMPHERE(jump[1]);    JUMPHERE(jump[1]);
5314    JUMPHERE(jump[2]);    JUMPHERE(jump[2]);
   JUMPHERE(jump[3]);  
5315    return cc;    return cc;
5316    
5317    case OP_NOT_HSPACE:    case OP_NOT_HSPACE:
5318    case OP_HSPACE:    case OP_HSPACE:
5319    detect_partial_match(common, backtracks);    detect_partial_match(common, backtracks);
5320    read_char(common);    read_char_range(common, 0x9, 0x3000, type == OP_NOT_HSPACE);
5321    add_jump(compiler, &common->hspace, JUMP(SLJIT_FAST_CALL));    add_jump(compiler, &common->hspace, JUMP(SLJIT_FAST_CALL));
5322    add_jump(compiler, backtracks, JUMP(type == OP_NOT_HSPACE ? SLJIT_C_NOT_ZERO : SLJIT_C_ZERO));    add_jump(compiler, backtracks, JUMP(type == OP_NOT_HSPACE ? SLJIT_C_NOT_ZERO : SLJIT_C_ZERO));
5323    return cc;    return cc;
# Line 4385  switch(type) Line 5325  switch(type)
5325    case OP_NOT_VSPACE:    case OP_NOT_VSPACE:
5326    case OP_VSPACE:    case OP_VSPACE:
5327    detect_partial_match(common, backtracks);    detect_partial_match(common, backtracks);
5328    read_char(common);    read_char_range(common, 0xa, 0x2029, type == OP_NOT_VSPACE);
5329    add_jump(compiler, &common->vspace, JUMP(SLJIT_FAST_CALL));    add_jump(compiler, &common->vspace, JUMP(SLJIT_FAST_CALL));
5330    add_jump(compiler, backtracks, JUMP(type == OP_NOT_VSPACE ? SLJIT_C_NOT_ZERO : SLJIT_C_ZERO));    add_jump(compiler, backtracks, JUMP(type == OP_NOT_VSPACE ? SLJIT_C_NOT_ZERO : SLJIT_C_ZERO));
5331    return cc;    return cc;
# Line 4484  switch(type) Line 5424  switch(type)
5424      else      else
5425        {        {
5426        OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), LOCALS1, STR_PTR, 0);        OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), LOCALS1, STR_PTR, 0);
5427        read_char(common);        read_char_range(common, common->nlmin, common->nlmax, TRUE);
5428        add_jump(compiler, backtracks, CMP(SLJIT_C_NOT_EQUAL, STR_PTR, 0, STR_END, 0));        add_jump(compiler, backtracks, CMP(SLJIT_C_NOT_EQUAL, STR_PTR, 0, STR_END, 0));
5429        add_jump(compiler, &common->anynewline, JUMP(SLJIT_FAST_CALL));        add_jump(compiler, &common->anynewline, JUMP(SLJIT_FAST_CALL));
5430        add_jump(compiler, backtracks, JUMP(SLJIT_C_ZERO));        add_jump(compiler, backtracks, JUMP(SLJIT_C_ZERO));
# Line 4532  switch(type) Line 5472  switch(type)
5472    else    else
5473      {      {
5474      skip_char_back(common);      skip_char_back(common);
5475      read_char(common);      read_char_range(common, common->nlmin, common->nlmax, TRUE);
5476      check_newlinechar(common, common->nltype, backtracks, FALSE);      check_newlinechar(common, common->nltype, backtracks, FALSE);
5477      }      }
5478    JUMPHERE(jump[0]);    JUMPHERE(jump[0]);
# Line 4583  switch(type) Line 5523  switch(type)
5523      }      }
5524    else    else
5525      {      {
5526      peek_char(common);      peek_char(common, common->nlmax);
5527      check_newlinechar(common, common->nltype, backtracks, FALSE);      check_newlinechar(common, common->nltype, backtracks, FALSE);
5528      }      }
5529    JUMPHERE(jump[0]);    JUMPHERE(jump[0]);
# Line 4607  switch(type) Line 5547  switch(type)
5547  #endif  #endif
5548      return byte_sequence_compare(common, type == OP_CHARI, cc, &context, backtracks);      return byte_sequence_compare(common, type == OP_CHARI, cc, &context, backtracks);
5549      }      }
5550    
5551    detect_partial_match(common, backtracks);    detect_partial_match(common, backtracks);
   read_char(common);  
5552  #ifdef SUPPORT_UTF  #ifdef SUPPORT_UTF
5553    if (common->utf)    if (common->utf)
5554      {      {
# Line 4617  switch(type) Line 5557  switch(type)
5557    else    else
5558  #endif  #endif
5559      c = *cc;      c = *cc;
5560    
5561    if (type == OP_CHAR || !char_has_othercase(common, cc))    if (type == OP_CHAR || !char_has_othercase(common, cc))
5562      {      {
5563        read_char_range(common, c, c, FALSE);
5564      add_jump(compiler, backtracks, CMP(SLJIT_C_NOT_EQUAL, TMP1, 0, SLJIT_IMM, c));      add_jump(compiler, backtracks, CMP(SLJIT_C_NOT_EQUAL, TMP1, 0, SLJIT_IMM, c));
5565      return cc + length;      return cc + length;
5566      }      }
5567    oc = char_othercase(common, c);    oc = char_othercase(common, c);
5568      read_char_range(common, c < oc ? c : oc, c > oc ? c : oc, FALSE);
5569    bit = c ^ oc;    bit = c ^ oc;
5570    if (is_powerof2(bit))    if (is_powerof2(bit))
5571      {      {
# Line 4630  switch(type) Line 5573  switch(type)
5573      add_jump(compiler, backtracks, CMP(SLJIT_C_NOT_EQUAL, TMP1, 0, SLJIT_IMM, c | bit));      add_jump(compiler, backtracks, CMP(SLJIT_C_NOT_EQUAL, TMP1, 0, SLJIT_IMM, c | bit));
5574      return cc + length;      return cc + length;
5575      }      }
5576    OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, c);    jump[0] = CMP(SLJIT_C_EQUAL, TMP1, 0, SLJIT_IMM, c);
5577    OP_FLAGS(SLJIT_MOV, TMP2, 0, SLJIT_UNUSED, 0, SLJIT_C_EQUAL);    add_jump(compiler, backtracks, CMP(SLJIT_C_NOT_EQUAL, TMP1, 0, SLJIT_IMM, oc));
5578    OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, oc);    JUMPHERE(jump[0]);
   OP_FLAGS(SLJIT_OR | SLJIT_SET_E, TMP2, 0, TMP2, 0, SLJIT_C_EQUAL);  
   add_jump(compiler, backtracks, JUMP(SLJIT_C_ZERO));  
5579    return cc + length;    return cc + length;
5580    
5581    case OP_NOT:    case OP_NOT:
# Line 4669  switch(type) Line 5610  switch(type)
5610  #endif /* COMPILE_PCRE8 */  #endif /* COMPILE_PCRE8 */
5611        {        {
5612        GETCHARLEN(c, cc, length);        GETCHARLEN(c, cc, length);
       read_char(common);  
5613        }        }
5614      }      }
5615    else    else
5616  #endif /* SUPPORT_UTF */  #endif /* SUPPORT_UTF */
     {  
     read_char(common);  
5617      c = *cc;      c = *cc;
     }  
5618    
5619    if (type == OP_NOT || !char_has_othercase(common, cc))    if (type == OP_NOT || !char_has_othercase(common, cc))
5620        {
5621        read_char_range(common, c, c, TRUE);
5622      add_jump(compiler, backtracks, CMP(SLJIT_C_EQUAL, TMP1, 0, SLJIT_IMM, c));      add_jump(compiler, backtracks, CMP(SLJIT_C_EQUAL, TMP1, 0, SLJIT_IMM, c));
5623        }
5624    else    else
5625      {      {
5626      oc = char_othercase(common, c);      oc = char_othercase(common, c);
5627        read_char_range(common, c < oc ? c : oc, c > oc ? c : oc, TRUE);
5628      bit = c ^ oc;      bit = c ^ oc;
5629      if (is_powerof2(bit))      if (is_powerof2(bit))
5630        {        {
# Line 4701  switch(type) Line 5642  switch(type)
5642    case OP_CLASS:    case OP_CLASS:
5643    case OP_NCLASS:    case OP_NCLASS:
5644    detect_partial_match(common, backtracks);    detect_partial_match(common, backtracks);
5645    read_char(common);  
5646    if (check_class_ranges(common, (const pcre_uint8 *)cc, type == OP_NCLASS, backtracks))  #if defined SUPPORT_UTF && defined COMPILE_PCRE8
5647      bit = (common->utf && is_char7_bitset((const pcre_uint8 *)cc, type == OP_NCLASS)) ? 127 : 255;
5648      read_char_range(common, 0, bit, type == OP_NCLASS);
5649    #else
5650      read_char_range(common, 0, 255, type == OP_NCLASS);
5651    #endif
5652    
5653      if (check_class_ranges(common, (const pcre_uint8 *)cc, type == OP_NCLASS, FALSE, backtracks))
5654      return cc + 32 / sizeof(pcre_uchar);      return cc + 32 / sizeof(pcre_uchar);
5655    
5656  #if defined SUPPORT_UTF || !defined COMPILE_PCRE8  #if defined SUPPORT_UTF && defined COMPILE_PCRE8
5657    jump[0] = NULL;    jump[0] = NULL;
 #ifdef COMPILE_PCRE8  
   /* This check only affects 8 bit mode. In other modes, we  
   always need to compare the value with 255. */  
5658    if (common->utf)    if (common->utf)
 #endif /* COMPILE_PCRE8 */  
5659      {      {
5660      jump[0] = CMP(SLJIT_C_GREATER, TMP1, 0, SLJIT_IMM, 255);      jump[0] = CMP(SLJIT_C_GREATER, TMP1, 0, SLJIT_IMM, bit);
5661      if (type == OP_CLASS)      if (type == OP_CLASS)
5662        {        {
5663        add_jump(compiler, backtracks, jump[0]);        add_jump(compiler, backtracks, jump[0]);
5664        jump[0] = NULL;        jump[0] = NULL;
5665        }        }
5666      }      }
5667  #endif /* SUPPORT_UTF || !COMPILE_PCRE8 */  #elif !defined COMPILE_PCRE8
5668      jump[0] = CMP(SLJIT_C_GREATER, TMP1, 0, SLJIT_IMM, 255);
5669      if (type == OP_CLASS)
5670        {
5671        add_jump(compiler, backtracks, jump[0]);
5672        jump[0] = NULL;
5673        }
5674    #endif /* SUPPORT_UTF && COMPILE_PCRE8 */
5675    
5676    OP2(SLJIT_AND, TMP2, 0, TMP1, 0, SLJIT_IMM, 0x7);    OP2(SLJIT_AND, TMP2, 0, TMP1, 0, SLJIT_IMM, 0x7);
5677    OP2(SLJIT_LSHR, TMP1, 0, TMP1, 0, SLJIT_IMM, 3);    OP2(SLJIT_LSHR, TMP1, 0, TMP1, 0, SLJIT_IMM, 3);
5678    OP1(SLJIT_MOV_UB, TMP1, 0, SLJIT_MEM1(TMP1), (sljit_sw)cc);    OP1(SLJIT_MOV_UB, TMP1, 0, SLJIT_MEM1(TMP1), (sljit_sw)cc);
5679    OP2(SLJIT_SHL, TMP2, 0, SLJIT_IMM, 1, TMP2, 0);    OP2(SLJIT_SHL, TMP2, 0, SLJIT_IMM, 1, TMP2, 0);
5680    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);
5681    add_jump(compiler, backtracks, JUMP(SLJIT_C_ZERO));    add_jump(compiler, backtracks, JUMP(SLJIT_C_ZERO));
5682    
5683  #if defined SUPPORT_UTF || !defined COMPILE_PCRE8  #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
5684    if (jump[0] != NULL)    if (jump[0] != NULL)
5685      JUMPHERE(jump[0]);      JUMPHERE(jump[0]);
5686  #endif /* SUPPORT_UTF || !COMPILE_PCRE8 */  #endif
5687    
5688    return cc + 32 / sizeof(pcre_uchar);    return cc + 32 / sizeof(pcre_uchar);
5689    
5690  #if defined SUPPORT_UTF || defined COMPILE_PCRE16 || defined COMPILE_PCRE32  #if defined SUPPORT_UTF || defined COMPILE_PCRE16 || defined COMPILE_PCRE32
# Line 4835  if (context.length > 0) Line 5789  if (context.length > 0)
5789  return compile_char1_matchingpath(common, *cc, cc + 1, backtracks);  return compile_char1_matchingpath(common, *cc, cc + 1, backtracks);
5790  }  }
5791    
 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));  
 }  
   
5792  /* Forward definitions. */  /* Forward definitions. */
5793  static void compile_matchingpath(compiler_common *, pcre_uchar *, pcre_uchar *, backtrack_common *);  static void compile_matchingpath(compiler_common *, pcre_uchar *, pcre_uchar *, backtrack_common *);
5794  static void compile_backtrackingpath(compiler_common *, struct backtrack_common *);  static void compile_backtrackingpath(compiler_common *, struct backtrack_common *);
# Line 4889  static void compile_backtrackingpath(com Line 5821  static void compile_backtrackingpath(com
5821    
5822  #define BACKTRACK_AS(type) ((type *)backtrack)  #define BACKTRACK_AS(type) ((type *)backtrack)
5823    
5824  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)
5825    {
5826    /* The OVECTOR offset goes to TMP2. */
5827    DEFINE_COMPILER;
5828    int count = GET2(cc, 1 + IMM2_SIZE);
5829    pcre_uchar *slot = common->name_table + GET2(cc, 1) * common->name_entry_size;
5830    unsigned int offset;
5831    jump_list *found = NULL;
5832    
5833    SLJIT_ASSERT(*cc == OP_DNREF || *cc == OP_DNREFI);
5834    
5835    OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(1));
5836    
5837    count--;
5838    while (count-- > 0)
5839      {
5840      offset = GET2(slot, 0) << 1;
5841      GET_LOCAL_BASE(TMP2, 0, OVECTOR(offset));
5842      add_jump(compiler, &found, CMP(SLJIT_C_NOT_EQUAL, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(offset), TMP1, 0));
5843      slot += common->name_entry_size;
5844      }
5845    
5846    offset = GET2(slot, 0) << 1;
5847    GET_LOCAL_BASE(TMP2, 0, OVECTOR(offset));
5848    if (backtracks != NULL && !common->jscript_compat)
5849      add_jump(compiler, backtracks, CMP(SLJIT_C_EQUAL, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(offset), TMP1, 0));
5850    
5851    set_jumps(found, LABEL());
5852    }
5853    
5854    static void compile_ref_matchingpath(compiler_common *common, pcre_uchar *cc, jump_list **backtracks, BOOL withchecks, BOOL emptyfail)
5855  {  {
5856  DEFINE_COMPILER;  DEFINE_COMPILER;
5857  int offset = GET2(cc, 1) << 1;  BOOL ref = (*cc == OP_REF || *cc == OP_REFI);
5858    int offset = 0;
5859  struct sljit_jump *jump = NULL;  struct sljit_jump *jump = NULL;
5860  struct sljit_jump *partial;  struct sljit_jump *partial;
5861  struct sljit_jump *nopartial;  struct sljit_jump *nopartial;
5862    
5863  OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(offset));  if (ref)
5864  /* OVECTOR(1) contains the "string begin - 1" constant. */    {
5865  if (withchecks && !common->jscript_compat)    offset = GET2(cc, 1) << 1;
5866    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));
5867      /* OVECTOR(1) contains the "string begin - 1" constant. */
5868      if (withchecks && !common->jscript_compat)
5869        add_jump(compiler, backtracks, CMP(SLJIT_C_EQUAL, TMP1, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(1)));
5870      }
5871    else
5872      OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(TMP2), 0);
5873    
5874  #if defined SUPPORT_UTF && defined SUPPORT_UCP  #if defined SUPPORT_UTF && defined SUPPORT_UCP
5875  if (common->utf && *cc == OP_REFI)  if (common->utf && *cc == OP_REFI)
5876    {    {
5877    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);
5878    OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(offset + 1));    if (ref)
5879        OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(offset + 1));
5880      else
5881        OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(TMP2), sizeof(sljit_sw));
5882    
5883    if (withchecks)    if (withchecks)
5884      jump = CMP(SLJIT_C_EQUAL, TMP1, 0, TMP2, 0);      jump = CMP(SLJIT_C_EQUAL, TMP1, 0, TMP2, 0);
5885    
# Line 4931  if (common->utf && *cc == OP_REFI) Line 5904  if (common->utf && *cc == OP_REFI)
5904  else  else
5905  #endif /* SUPPORT_UTF && SUPPORT_UCP */  #endif /* SUPPORT_UTF && SUPPORT_UCP */
5906    {    {
5907    OP2(SLJIT_SUB | SLJIT_SET_E, TMP2, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(offset + 1), TMP1, 0);    if (ref)
5908        OP2(SLJIT_SUB | SLJIT_SET_E, TMP2, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(offset + 1), TMP1, 0);
5909      else
5910        OP2(SLJIT_SUB | SLJIT_SET_E, TMP2, 0, SLJIT_MEM1(TMP2), sizeof(sljit_sw), TMP1, 0);
5911    
5912    if (withchecks)    if (withchecks)
5913      jump = JUMP(SLJIT_C_ZERO);      jump = JUMP(SLJIT_C_ZERO);
5914    
# Line 4968  if (jump != NULL) Line 5945  if (jump != NULL)
5945    else    else
5946      JUMPHERE(jump);      JUMPHERE(jump);
5947    }    }
 return cc + 1 + IMM2_SIZE;  
5948  }  }
5949    
5950  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)
5951  {  {
5952  DEFINE_COMPILER;  DEFINE_COMPILER;
5953    BOOL ref = (*cc == OP_REF || *cc == OP_REFI);
5954  backtrack_common *backtrack;  backtrack_common *backtrack;
5955  pcre_uchar type;  pcre_uchar type;
5956    int offset = 0;
5957  struct sljit_label *label;  struct sljit_label *label;
5958  struct sljit_jump *zerolength;  struct sljit_jump *zerolength;
5959  struct sljit_jump *jump = NULL;  struct sljit_jump *jump = NULL;
# Line 4985  BOOL minimize; Line 5963  BOOL minimize;
5963    
5964  PUSH_BACKTRACK(sizeof(iterator_backtrack), cc, NULL);  PUSH_BACKTRACK(sizeof(iterator_backtrack), cc, NULL);
5965    
5966    if (ref)
5967      offset = GET2(cc, 1) << 1;
5968    else
5969      cc += IMM2_SIZE;
5970  type = cc[1 + IMM2_SIZE];  type = cc[1 + IMM2_SIZE];
5971    
5972    SLJIT_COMPILE_ASSERT((OP_CRSTAR & 0x1) == 0, crstar_opcode_must_be_even);
5973  minimize = (type & 0x1) != 0;  minimize = (type & 0x1) != 0;
5974  switch(type)  switch(type)
5975    {    {
# Line 5023  if (!minimize) Line 6007  if (!minimize)
6007    if (min == 0)    if (min == 0)
6008      {      {
6009      allocate_stack(common, 2);      allocate_stack(common, 2);
6010        if (ref)
6011          OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(offset));
6012      OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), STR_PTR, 0);      OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), STR_PTR, 0);
6013      OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(1), SLJIT_IMM, 0);      OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(1), SLJIT_IMM, 0);
6014      /* Temporary release of STR_PTR. */      /* Temporary release of STR_PTR. */
6015      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));
6016      zerolength = compile_ref_checks(common, ccbegin, NULL);      /* Handles both invalid and empty cases. Since the minimum repeat,
6017        is zero the invalid case is basically the same as an empty case. */
6018        if (ref)
6019          zerolength = CMP(SLJIT_C_EQUAL, TMP1, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(offset + 1));
6020        else
6021          {
6022          compile_dnref_search(common, ccbegin, NULL);
6023          OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(TMP2), 0);
6024          OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), POSSESSIVE1, TMP2, 0);
6025          zerolength = CMP(SLJIT_C_EQUAL, TMP1, 0, SLJIT_MEM1(TMP2), sizeof(sljit_sw));
6026          }
6027      /* Restore if not zero length. */      /* Restore if not zero length. */
6028      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));
6029      }      }
6030    else    else
6031      {      {
6032      allocate_stack(common, 1);      allocate_stack(common, 1);
6033        if (ref)
6034          OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(offset));
6035      OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), SLJIT_IMM, 0);      OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), SLJIT_IMM, 0);
6036      zerolength = compile_ref_checks(common, ccbegin, &backtrack->topbacktracks);      if (ref)
6037          {
6038          add_jump(compiler, &backtrack->topbacktracks, CMP(SLJIT_C_EQUAL, TMP1, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(1)));
6039          zerolength = CMP(SLJIT_C_EQUAL, TMP1, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(offset + 1));
6040          }
6041        else
6042          {
6043          compile_dnref_search(common, ccbegin, &backtrack->topbacktracks);
6044          OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(TMP2), 0);
6045          OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), POSSESSIVE1, TMP2, 0);
6046          zerolength = CMP(SLJIT_C_EQUAL, TMP1, 0, SLJIT_MEM1(TMP2), sizeof(sljit_sw));
6047          }
6048      }      }
6049    
6050    if (min > 1 || max > 1)    if (min > 1 || max > 1)
6051      OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), POSSESSIVE0, SLJIT_IMM, 0);      OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), POSSESSIVE0, SLJIT_IMM, 0);
6052    
6053    label = LABEL();    label = LABEL();
6054      if (!ref)
6055        OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), POSSESSIVE1);
6056    compile_ref_matchingpath(common, ccbegin, &backtrack->topbacktracks, FALSE, FALSE);    compile_ref_matchingpath(common, ccbegin, &backtrack->topbacktracks, FALSE, FALSE);
6057    
6058    if (min > 1 || max > 1)    if (min > 1 || max > 1)
# Line 5072  if (!minimize) Line 6083  if (!minimize)
6083    JUMPHERE(zerolength);    JUMPHERE(zerolength);
6084    BACKTRACK_AS(iterator_backtrack)->matchingpath = LABEL();    BACKTRACK_AS(iterator_backtrack)->matchingpath = LABEL();
6085    
6086    decrease_call_count(common);    count_match(common);
6087    return cc;    return cc;
6088    }    }
6089    
6090  allocate_stack(common, 2);  allocate_stack(common, ref ? 2 : 3);
6091    if (ref)
6092      OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(offset));
6093  OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), SLJIT_IMM, 0);  OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), SLJIT_IMM, 0);
6094  if (type != OP_CRMINSTAR)  if (type != OP_CRMINSTAR)
6095    OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(1), SLJIT_IMM, 0);    OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(1), SLJIT_IMM, 0);
6096    
6097  if (min == 0)  if (min == 0)
6098    {    {
6099    zerolength = compile_ref_checks(common, ccbegin, NULL);    /* Handles both invalid and empty cases. Since the minimum repeat,
6100      is zero the invalid case is basically the same as an empty case. */
6101      if (ref)
6102        zerolength = CMP(SLJIT_C_EQUAL, TMP1, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(offset + 1));
6103      else
6104        {
6105        compile_dnref_search(common, ccbegin, NULL);
6106        OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(TMP2), 0);
6107        OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(2), TMP2, 0);
6108        zerolength = CMP(SLJIT_C_EQUAL, TMP1, 0, SLJIT_MEM1(TMP2), sizeof(sljit_sw));
6109        }
6110      /* Length is non-zero, we can match real repeats. */
6111    OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), STR_PTR, 0);    OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), STR_PTR, 0);
6112    jump = JUMP(SLJIT_JUMP);    jump = JUMP(SLJIT_JUMP);
6113    }    }
6114  else  else
6115    zerolength = compile_ref_checks(common, ccbegin, &backtrack->topbacktracks);    {
6116      if (ref)
6117        {
6118        add_jump(compiler, &backtrack->topbacktracks, CMP(SLJIT_C_EQUAL, TMP1, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(1)));
6119        zerolength = CMP(SLJIT_C_EQUAL, TMP1, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(offset + 1));
6120        }
6121      else
6122        {
6123        compile_dnref_search(common, ccbegin, &backtrack->topbacktracks);
6124        OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(TMP2), 0);
6125        OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(2), TMP2, 0);
6126        zerolength = CMP(SLJIT_C_EQUAL, TMP1, 0, SLJIT_MEM1(TMP2), sizeof(sljit_sw));
6127        }
6128      }
6129    
6130  BACKTRACK_AS(iterator_backtrack)->matchingpath = LABEL();  BACKTRACK_AS(iterator_backtrack)->matchingpath = LABEL();
6131  if (max > 0)  if (max > 0)
6132    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));
6133    
6134    if (!ref)
6135      OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(STACK_TOP), STACK(2));
6136  compile_ref_matchingpath(common, ccbegin, &backtrack->topbacktracks, TRUE, TRUE);  compile_ref_matchingpath(common, ccbegin, &backtrack->topbacktracks, TRUE, TRUE);
6137  OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), STR_PTR, 0);  OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), STR_PTR, 0);
6138    
# Line 5111  if (jump != NULL) Line 6150  if (jump != NULL)
6150    JUMPHERE(jump);    JUMPHERE(jump);
6151  JUMPHERE(zerolength);  JUMPHERE(zerolength);
6152    
6153  decrease_call_count(common);  count_match(common);
6154  return cc;  return cc;
6155  }  }
6156    
# Line 5121  DEFINE_COMPILER; Line 6160  DEFINE_COMPILER;
6160  backtrack_common *backtrack;  backtrack_common *backtrack;
6161  recurse_entry *entry = common->entries;  recurse_entry *entry = common->entries;
6162  recurse_entry *prev = NULL;  recurse_entry *prev = NULL;
6163  int start = GET(cc, 1);  sljit_sw start = GET(cc, 1);
6164  pcre_uchar *start_cc;  pcre_uchar *start_cc;
6165    BOOL needs_control_head;
6166    
6167  PUSH_BACKTRACK(sizeof(recurse_backtrack), cc, NULL);  PUSH_BACKTRACK(sizeof(recurse_backtrack), cc, NULL);
6168    
6169  /* Inlining simple patterns. */  /* Inlining simple patterns. */
6170  if (get_framesize(common, common->start + start, TRUE) == no_stack)  if (get_framesize(common, common->start + start, NULL, TRUE, &needs_control_head) == no_stack)
6171    {    {
6172    start_cc = common->start + start;    start_cc = common->start + start;
6173    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 5246  allocate_stack(common, CALLOUT_ARG_SIZE Line 6286  allocate_stack(common, CALLOUT_ARG_SIZE
6286  OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), common->capture_last_ptr);  OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), common->capture_last_ptr);
6287  OP1(SLJIT_MOV, TMP1, 0, ARGUMENTS, 0);  OP1(SLJIT_MOV, TMP1, 0, ARGUMENTS, 0);
6288  SLJIT_ASSERT(common->capture_last_ptr != 0);  SLJIT_ASSERT(common->capture_last_ptr != 0);
6289  OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), CALLOUT_ARG_OFFSET(callout_number), SLJIT_IMM, cc[1]);  OP1(SLJIT_MOV_SI, SLJIT_MEM1(STACK_TOP), CALLOUT_ARG_OFFSET(callout_number), SLJIT_IMM, cc[1]);
6290  OP1(SLJIT_MOV_UI, SLJIT_MEM1(STACK_TOP), CALLOUT_ARG_OFFSET(capture_last), TMP2, 0);  OP1(SLJIT_MOV_SI, SLJIT_MEM1(STACK_TOP), CALLOUT_ARG_OFFSET(capture_last), TMP2, 0);
6291    
6292  /* These pointer sized fields temporarly stores internal variables. */  /* These pointer sized fields temporarly stores internal variables. */
6293  OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(0));  OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(0));
# Line 5256  OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), CA Line 6296  OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), CA
6296    
6297  if (common->mark_ptr != 0)  if (common->mark_ptr != 0)
6298    OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(TMP1), SLJIT_OFFSETOF(jit_arguments, mark_ptr));    OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(TMP1), SLJIT_OFFSETOF(jit_arguments, mark_ptr));
6299  OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), CALLOUT_ARG_OFFSET(pattern_position), SLJIT_IMM, GET(cc, 2));  OP1(SLJIT_MOV_SI, SLJIT_MEM1(STACK_TOP), CALLOUT_ARG_OFFSET(pattern_position), SLJIT_IMM, GET(cc, 2));
6300  OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), CALLOUT_ARG_OFFSET(next_item_length), SLJIT_IMM, GET(cc, 2 + LINK_SIZE));  OP1(SLJIT_MOV_SI, SLJIT_MEM1(STACK_TOP), CALLOUT_ARG_OFFSET(next_item_length), SLJIT_IMM, GET(cc, 2 + LINK_SIZE));
6301  OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), CALLOUT_ARG_OFFSET(mark), (common->mark_ptr != 0) ? TMP2 : SLJIT_IMM, 0);  OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), CALLOUT_ARG_OFFSET(mark), (common->mark_ptr != 0) ? TMP2 : SLJIT_IMM, 0);
6302    
6303  /* Needed to save important temporary registers. */  /* Needed to save important temporary registers. */
# Line 5286  static pcre_uchar *compile_assert_matchi Line 6326  static pcre_uchar *compile_assert_matchi
6326  {  {
6327  DEFINE_COMPILER;  DEFINE_COMPILER;
6328  int framesize;  int framesize;
6329    int extrasize;
6330    BOOL needs_control_head;
6331  int private_data_ptr;  int private_data_ptr;