/[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 1012 by zherczeg, Sat Aug 25 15:34:13 2012 UTC revision 1624 by zherczeg, Fri Feb 5 13:47:43 2016 UTC
# Line 6  Line 6 
6  and semantics are as close as possible to those of the Perl 5 language.  and semantics are as close as possible to those of the Perl 5 language.
7    
8                         Written by Philip Hazel                         Written by Philip Hazel
9             Copyright (c) 1997-2012 University of Cambridge             Copyright (c) 1997-2013 University of Cambridge
10    
11    The machine code generator part (this module) was written by Zoltan Herczeg    The machine code generator part (this module) was written by Zoltan Herczeg
12                        Copyright (c) 2010-2012                        Copyright (c) 2010-2013
13    
14  -----------------------------------------------------------------------------  -----------------------------------------------------------------------------
15  Redistribution and use in source and binary forms, with or without  Redistribution and use in source and binary forms, with or without
# Line 46  POSSIBILITY OF SUCH DAMAGE. Line 46  POSSIBILITY OF SUCH DAMAGE.
46    
47  #include "pcre_internal.h"  #include "pcre_internal.h"
48    
49  #ifdef SUPPORT_JIT  #if defined SUPPORT_JIT
50    
51  /* All-in-one: Since we use the JIT compiler only from here,  /* All-in-one: Since we use the JIT compiler only from here,
52  we just include it. This way we don't need to touch the build  we just include it. This way we don't need to touch the build
53  system files. */  system files. */
54    
55  #define SLJIT_MALLOC(size) (PUBL(malloc))(size)  #define SLJIT_MALLOC(size, allocator_data) (PUBL(malloc))(size)
56  #define SLJIT_FREE(ptr) (PUBL(free))(ptr)  #define SLJIT_FREE(ptr, allocator_data) (PUBL(free))(ptr)
57  #define SLJIT_CONFIG_AUTO 1  #define SLJIT_CONFIG_AUTO 1
58  #define SLJIT_CONFIG_STATIC 1  #define SLJIT_CONFIG_STATIC 1
59  #define SLJIT_VERBOSE 0  #define SLJIT_VERBOSE 0
# Line 65  system files. */ Line 65  system files. */
65  #error Unsupported architecture  #error Unsupported architecture
66  #endif  #endif
67    
68    /* Defines for debugging purposes. */
69    
70    /* 1 - Use unoptimized capturing brackets.
71       2 - Enable capture_last_ptr (includes option 1). */
72    /* #define DEBUG_FORCE_UNOPTIMIZED_CBRAS 2 */
73    
74    /* 1 - Always have a control head. */
75    /* #define DEBUG_FORCE_CONTROL_HEAD 1 */
76    
77  /* Allocate memory for the regex stack on the real machine stack.  /* 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 157  typedef struct jit_arguments { Line 166  typedef struct jit_arguments {
166    int *offsets;    int *offsets;
167    pcre_uchar *uchar_ptr;    pcre_uchar *uchar_ptr;
168    pcre_uchar *mark_ptr;    pcre_uchar *mark_ptr;
169      void *callout_data;
170    /* Everything else after. */    /* Everything else after. */
171    int offsetcount;    pcre_uint32 limit_match;
172    int calllimit;    int real_offset_count;
173      int offset_count;
174    pcre_uint8 notbol;    pcre_uint8 notbol;
175    pcre_uint8 noteol;    pcre_uint8 noteol;
176    pcre_uint8 notempty;    pcre_uint8 notempty;
# Line 168  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      void *read_only_data_heads[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    sljit_uw executable_sizes[JIT_NUMBER_OF_COMPILE_MODES];    pcre_uint32 top_bracket;
187      pcre_uint32 limit_match;
188  } executable_functions;  } executable_functions;
189    
190  typedef struct jump_list {  typedef struct jump_list {
# Line 178  typedef struct jump_list { Line 192  typedef struct jump_list {
192    struct jump_list *next;    struct jump_list *next;
193  } jump_list;  } jump_list;
194    
 enum stub_types { stack_alloc };  
   
195  typedef struct stub_list {  typedef struct stub_list {
   enum stub_types type;  
   int data;  
196    struct sljit_jump *start;    struct sljit_jump *start;
197    struct sljit_label *quit;    struct sljit_label *quit;
198    struct stub_list *next;    struct stub_list *next;
199  } stub_list;  } stub_list;
200    
201    typedef struct label_addr_list {
202      struct sljit_label *label;
203      sljit_uw *update_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 208  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 229  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 264  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 {
297    backtrack_common common;    backtrack_common common;
298      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      /* Chain list of read-only data ptrs. */
326      void *read_only_data_head;
327    /* Tells whether the capturing bracket is optimized. */    /* Tells whether the capturing bracket is optimized. */
328    pcre_uint8 *optimized_cbracket;    pcre_uint8 *optimized_cbracket;
329      /* Tells whether the starting offset is a target of then. */
330      pcre_uint8 *then_offsets;
331      /* Current position where a THEN must jump. */
332      then_trap_backtrack *then_trap;
333    /* Starting offset of private data for capturing brackets. */    /* Starting offset of private data for capturing brackets. */
334    int cbraptr;    int cbra_ptr;
335    /* OVector starting point. Must be divisible by 2. */    /* Output vector starting point. Must be divisible by 2. */
336    int ovector_start;    int ovector_start;
337    /* Last known position of the requested byte. */    /* Last known position of the requested byte. */
338    int req_char_ptr;    int req_char_ptr;
339    /* Head of the last recursion. */    /* Head of the last recursion. */
340    int recursive_head;    int recursive_head_ptr;
341    /* First inspected character for partial matching. */    /* First inspected character for partial matching. */
342    int start_used_ptr;    int start_used_ptr;
343    /* Starting pointer for partial soft matches. */    /* Starting pointer for partial soft matches. */
# Line 297  typedef struct compiler_common { Line 346  typedef struct compiler_common {
346    int first_line_end;    int first_line_end;
347    /* Points to the marked string. */    /* Points to the marked string. */
348    int mark_ptr;    int mark_ptr;
349      /* Recursive control verb management chain. */
350      int control_head_ptr;
351      /* Points to the last matched capture block index. */
352      int capture_last_ptr;
353      /* Points to the starting position of the current match. */
354      int start_ptr;
355    
356    /* Flipped and lower case tables. */    /* Flipped and lower case tables. */
357    const pcre_uint8 *fcc;    const pcre_uint8 *fcc;
358    sljit_w lcc;    sljit_sw lcc;
359    /* Mode can be PCRE_STUDY_JIT_COMPILE and others. */    /* Mode can be PCRE_STUDY_JIT_COMPILE and others. */
360    int mode;    int mode;
361      /* TRUE, when minlength is greater than 0. */
362      BOOL might_be_empty;
363      /* \K is found in the pattern. */
364      BOOL has_set_som;
365      /* (*SKIP:arg) is found in the pattern. */
366      BOOL has_skip_arg;
367      /* (*THEN) is found in the pattern. */
368      BOOL has_then;
369      /* Needs to know the start position anytime. */
370      BOOL needs_start_ptr;
371      /* Currently in recurse or negative assert. */
372      BOOL local_exit;
373      /* Currently in a positive assert. */
374      BOOL positive_assert;
375    /* Newline control. */    /* Newline control. */
376    int nltype;    int nltype;
377      pcre_uint32 nlmax;
378      pcre_uint32 nlmin;
379    int newline;    int newline;
380    int bsr_nltype;    int bsr_nltype;
381      pcre_uint32 bsr_nlmax;
382      pcre_uint32 bsr_nlmin;
383    /* Dollar endonly. */    /* Dollar endonly. */
384    int endonly;    int endonly;
   BOOL has_set_som;  
385    /* Tables. */    /* Tables. */
386    sljit_w ctypes;    sljit_sw ctypes;
   int digits[2 + MAX_RANGE_SIZE];  
387    /* Named capturing brackets. */    /* Named capturing brackets. */
388    sljit_uw name_table;    pcre_uchar *name_table;
389    sljit_w name_count;    sljit_sw name_count;
390    sljit_w name_entry_size;    sljit_sw name_entry_size;
391    
392    /* Labels and jump lists. */    /* Labels and jump lists. */
393    struct sljit_label *partialmatchlabel;    struct sljit_label *partialmatchlabel;
394    struct sljit_label *quitlabel;    struct sljit_label *quit_label;
395    struct sljit_label *acceptlabel;    struct sljit_label *forced_quit_label;
396      struct sljit_label *accept_label;
397      struct sljit_label *ff_newline_shortcut;
398    stub_list *stubs;    stub_list *stubs;
399      label_addr_list *label_addrs;
400    recurse_entry *entries;    recurse_entry *entries;
401    recurse_entry *currententry;    recurse_entry *currententry;
402    jump_list *partialmatch;    jump_list *partialmatch;
403    jump_list *quit;    jump_list *quit;
404      jump_list *positive_assert_quit;
405      jump_list *forced_quit;
406    jump_list *accept;    jump_list *accept;
407    jump_list *calllimit;    jump_list *calllimit;
408    jump_list *stackalloc;    jump_list *stackalloc;
# Line 337  typedef struct compiler_common { Line 413  typedef struct compiler_common {
413    jump_list *vspace;    jump_list *vspace;
414    jump_list *casefulcmp;    jump_list *casefulcmp;
415    jump_list *caselesscmp;    jump_list *caselesscmp;
416      jump_list *reset_match;
417    BOOL jscript_compat;    BOOL jscript_compat;
418  #ifdef SUPPORT_UTF  #ifdef SUPPORT_UTF
419    BOOL utf;    BOOL utf;
420  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
421    BOOL use_ucp;    BOOL use_ucp;
422  #endif  #endif
   jump_list *utfreadchar;  
423  #ifdef COMPILE_PCRE8  #ifdef COMPILE_PCRE8
424      jump_list *utfreadchar;
425      jump_list *utfreadchar16;
426    jump_list *utfreadtype8;    jump_list *utfreadtype8;
427  #endif  #endif
428  #endif /* SUPPORT_UTF */  #endif /* SUPPORT_UTF */
# Line 361  typedef struct compare_context { Line 439  typedef struct compare_context {
439  #if defined SLJIT_UNALIGNED && SLJIT_UNALIGNED  #if defined SLJIT_UNALIGNED && SLJIT_UNALIGNED
440    int ucharptr;    int ucharptr;
441    union {    union {
442      sljit_i asint;      sljit_si asint;
443      sljit_uh asushort;      sljit_uh asushort;
444  #ifdef COMPILE_PCRE8  #if defined COMPILE_PCRE8
445      sljit_ub asbyte;      sljit_ub asbyte;
446      sljit_ub asuchars[4];      sljit_ub asuchars[4];
447  #else  #elif defined COMPILE_PCRE16
 #ifdef COMPILE_PCRE16  
448      sljit_uh asuchars[2];      sljit_uh asuchars[2];
449  #endif  #elif defined COMPILE_PCRE32
450        sljit_ui asuchars[1];
451  #endif  #endif
452    } c;    } c;
453    union {    union {
454      sljit_i asint;      sljit_si asint;
455      sljit_uh asushort;      sljit_uh asushort;
456  #ifdef COMPILE_PCRE8  #if defined COMPILE_PCRE8
457      sljit_ub asbyte;      sljit_ub asbyte;
458      sljit_ub asuchars[4];      sljit_ub asuchars[4];
459  #else  #elif defined COMPILE_PCRE16
 #ifdef COMPILE_PCRE16  
460      sljit_uh asuchars[2];      sljit_uh asuchars[2];
461  #endif  #elif defined COMPILE_PCRE32
462        sljit_ui asuchars[1];
463  #endif  #endif
464    } oc;    } oc;
465  #endif  #endif
466  } compare_context;  } compare_context;
467    
 enum {  
   frame_end = 0,  
   frame_setstrbegin = -1,  
   frame_setmark = -2  
 };  
   
468  /* Undefine sljit macros. */  /* Undefine sljit macros. */
469  #undef CMP  #undef CMP
470    
471  /* Used for accessing the elements of the stack. */  /* Used for accessing the elements of the stack. */
472  #define STACK(i)      ((-(i) - 1) * (int)sizeof(sljit_w))  #define STACK(i)      ((-(i) - 1) * (int)sizeof(sljit_sw))
473    
474  #define TMP1          SLJIT_TEMPORARY_REG1  #define TMP1          SLJIT_R0
475  #define TMP2          SLJIT_TEMPORARY_REG3  #define TMP2          SLJIT_R2
476  #define TMP3          SLJIT_TEMPORARY_EREG2  #define TMP3          SLJIT_R3
477  #define STR_PTR       SLJIT_SAVED_REG1  #define STR_PTR       SLJIT_S0
478  #define STR_END       SLJIT_SAVED_REG2  #define STR_END       SLJIT_S1
479  #define STACK_TOP     SLJIT_TEMPORARY_REG2  #define STACK_TOP     SLJIT_R1
480  #define STACK_LIMIT   SLJIT_SAVED_REG3  #define STACK_LIMIT   SLJIT_S2
481  #define ARGUMENTS     SLJIT_SAVED_EREG1  #define COUNT_MATCH   SLJIT_S3
482  #define CALL_COUNT    SLJIT_SAVED_EREG2  #define ARGUMENTS     SLJIT_S4
483  #define RETURN_ADDR   SLJIT_TEMPORARY_EREG1  #define RETURN_ADDR   SLJIT_R4
484    
485  /* Local space layout. */  /* Local space layout. */
486  /* These two locals can be used by the current opcode. */  /* These two locals can be used by the current opcode. */
487  #define LOCALS0          (0 * sizeof(sljit_w))  #define LOCALS0          (0 * sizeof(sljit_sw))
488  #define LOCALS1          (1 * sizeof(sljit_w))  #define LOCALS1          (1 * sizeof(sljit_sw))
489  /* Two local variables for possessive quantifiers (char1 cannot use them). */  /* Two local variables for possessive quantifiers (char1 cannot use them). */
490  #define POSSESSIVE0      (2 * sizeof(sljit_w))  #define POSSESSIVE0      (2 * sizeof(sljit_sw))
491  #define POSSESSIVE1      (3 * sizeof(sljit_w))  #define POSSESSIVE1      (3 * sizeof(sljit_sw))
492  /* Max limit of recursions. */  /* Max limit of recursions. */
493  #define CALL_LIMIT       (4 * sizeof(sljit_w))  #define LIMIT_MATCH      (4 * sizeof(sljit_sw))
494  /* The output vector is stored on the stack, and contains pointers  /* The output vector is stored on the stack, and contains pointers
495  to characters. The vector data is divided into two groups: the first  to characters. The vector data is divided into two groups: the first
496  group contains the start / end character pointers, and the second is  group contains the start / end character pointers, and the second is
497  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. */
498  #define OVECTOR_START    (common->ovector_start)  #define OVECTOR_START    (common->ovector_start)
499  #define OVECTOR(i)       (OVECTOR_START + (i) * sizeof(sljit_w))  #define OVECTOR(i)       (OVECTOR_START + (i) * (sljit_sw)sizeof(sljit_sw))
500  #define OVECTOR_PRIV(i)  (common->cbraptr + (i) * sizeof(sljit_w))  #define OVECTOR_PRIV(i)  (common->cbra_ptr + (i) * (sljit_sw)sizeof(sljit_sw))
501  #define PRIVATE_DATA(cc) (common->private_data_ptrs[(cc) - common->start])  #define PRIVATE_DATA(cc) (common->private_data_ptrs[(cc) - common->start])
502    
503  #ifdef COMPILE_PCRE8  #if defined COMPILE_PCRE8
504  #define MOV_UCHAR  SLJIT_MOV_UB  #define MOV_UCHAR  SLJIT_MOV_UB
505  #define MOVU_UCHAR SLJIT_MOVU_UB  #define MOVU_UCHAR SLJIT_MOVU_UB
506  #else  #elif defined COMPILE_PCRE16
 #ifdef COMPILE_PCRE16  
507  #define MOV_UCHAR  SLJIT_MOV_UH  #define MOV_UCHAR  SLJIT_MOV_UH
508  #define MOVU_UCHAR SLJIT_MOVU_UH  #define MOVU_UCHAR SLJIT_MOVU_UH
509    #elif defined COMPILE_PCRE32
510    #define MOV_UCHAR  SLJIT_MOV_UI
511    #define MOVU_UCHAR SLJIT_MOVU_UI
512  #else  #else
513  #error Unsupported compiling mode  #error Unsupported compiling mode
514  #endif  #endif
 #endif  
515    
516  /* Shortcuts. */  /* Shortcuts. */
517  #define DEFINE_COMPILER \  #define DEFINE_COMPILER \
# Line 455  the start pointers when the end of the c Line 528  the start pointers when the end of the c
528    sljit_set_label(sljit_emit_jump(compiler, (type)), (label))    sljit_set_label(sljit_emit_jump(compiler, (type)), (label))
529  #define JUMPHERE(jump) \  #define JUMPHERE(jump) \
530    sljit_set_label((jump), sljit_emit_label(compiler))    sljit_set_label((jump), sljit_emit_label(compiler))
531    #define SET_LABEL(jump, label) \
532      sljit_set_label((jump), (label))
533  #define CMP(type, src1, src1w, src2, src2w) \  #define CMP(type, src1, src1w, src2, src2w) \
534    sljit_emit_cmp(compiler, (type), (src1), (src1w), (src2), (src2w))    sljit_emit_cmp(compiler, (type), (src1), (src1w), (src2), (src2w))
535  #define CMPTO(type, src1, src1w, src2, src2w, label) \  #define CMPTO(type, src1, src1w, src2, src2w, label) \
536    sljit_set_label(sljit_emit_cmp(compiler, (type), (src1), (src1w), (src2), (src2w)), (label))    sljit_set_label(sljit_emit_cmp(compiler, (type), (src1), (src1w), (src2), (src2w)), (label))
537  #define COND_VALUE(op, dst, dstw, type) \  #define OP_FLAGS(op, dst, dstw, src, srcw, type) \
538    sljit_emit_cond_value(compiler, (op), (dst), (dstw), (type))    sljit_emit_op_flags(compiler, (op), (dst), (dstw), (src), (srcw), (type))
539  #define GET_LOCAL_BASE(dst, dstw, offset) \  #define GET_LOCAL_BASE(dst, dstw, offset) \
540    sljit_get_local_base(compiler, (dst), (dstw), (offset))    sljit_get_local_base(compiler, (dst), (dstw), (offset))
541    
542  static pcre_uchar* bracketend(pcre_uchar* cc)  #define READ_CHAR_MAX 0x7fffffff
543    
544    static pcre_uchar *bracketend(pcre_uchar *cc)
545  {  {
546  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));
547  do cc += GET(cc, 1); while (*cc == OP_ALT);  do cc += GET(cc, 1); while (*cc == OP_ALT);
# Line 473  cc += 1 + LINK_SIZE; Line 550  cc += 1 + LINK_SIZE;
550  return cc;  return cc;
551  }  }
552    
553    static int no_alternatives(pcre_uchar *cc)
554    {
555    int count = 0;
556    SLJIT_ASSERT((*cc >= OP_ASSERT && *cc <= OP_ASSERTBACK_NOT) || (*cc >= OP_ONCE && *cc <= OP_SCOND));
557    do
558      {
559      cc += GET(cc, 1);
560      count++;
561      }
562    while (*cc == OP_ALT);
563    SLJIT_ASSERT(*cc >= OP_KET && *cc <= OP_KETRPOS);
564    return count;
565    }
566    
567    static int ones_in_half_byte[16] = {
568      /* 0 */ 0, 1, 1, 2, /* 4 */ 1, 2, 2, 3,
569      /* 8 */ 1, 2, 2, 3, /* 12 */ 2, 3, 3, 4
570    };
571    
572  /* Functions whose might need modification for all new supported opcodes:  /* Functions whose might need modification for all new supported opcodes:
573   next_opcode   next_opcode
574   get_private_data_length   check_opcode_types
575   set_private_data_ptrs   set_private_data_ptrs
576   get_framesize   get_framesize
577   init_frame   init_frame
578   get_private_data_length_for_copy   get_private_data_copy_length
579   copy_private_data   copy_private_data
580   compile_matchingpath   compile_matchingpath
581   compile_backtrackingpath   compile_backtrackingpath
# Line 503  switch(*cc) Line 599  switch(*cc)
599    case OP_WORDCHAR:    case OP_WORDCHAR:
600    case OP_ANY:    case OP_ANY:
601    case OP_ALLANY:    case OP_ALLANY:
602      case OP_NOTPROP:
603      case OP_PROP:
604    case OP_ANYNL:    case OP_ANYNL:
605    case OP_NOT_HSPACE:    case OP_NOT_HSPACE:
606    case OP_HSPACE:    case OP_HSPACE:
# Line 515  switch(*cc) Line 613  switch(*cc)
613    case OP_CIRCM:    case OP_CIRCM:
614    case OP_DOLL:    case OP_DOLL:
615    case OP_DOLLM:    case OP_DOLLM:
   case OP_TYPESTAR:  
   case OP_TYPEMINSTAR:  
   case OP_TYPEPLUS:  
   case OP_TYPEMINPLUS:  
   case OP_TYPEQUERY:  
   case OP_TYPEMINQUERY:  
   case OP_TYPEPOSSTAR:  
   case OP_TYPEPOSPLUS:  
   case OP_TYPEPOSQUERY:  
616    case OP_CRSTAR:    case OP_CRSTAR:
617    case OP_CRMINSTAR:    case OP_CRMINSTAR:
618    case OP_CRPLUS:    case OP_CRPLUS:
619    case OP_CRMINPLUS:    case OP_CRMINPLUS:
620    case OP_CRQUERY:    case OP_CRQUERY:
621    case OP_CRMINQUERY:    case OP_CRMINQUERY:
622      case OP_CRRANGE:
623      case OP_CRMINRANGE:
624      case OP_CRPOSSTAR:
625      case OP_CRPOSPLUS:
626      case OP_CRPOSQUERY:
627      case OP_CRPOSRANGE:
628      case OP_CLASS:
629      case OP_NCLASS:
630      case OP_REF:
631      case OP_REFI:
632      case OP_DNREF:
633      case OP_DNREFI:
634      case OP_RECURSE:
635      case OP_CALLOUT:
636      case OP_ALT:
637      case OP_KET:
638      case OP_KETRMAX:
639      case OP_KETRMIN:
640      case OP_KETRPOS:
641      case OP_REVERSE:
642      case OP_ASSERT:
643      case OP_ASSERT_NOT:
644      case OP_ASSERTBACK:
645      case OP_ASSERTBACK_NOT:
646      case OP_ONCE:
647      case OP_ONCE_NC:
648      case OP_BRA:
649      case OP_BRAPOS:
650      case OP_CBRA:
651      case OP_CBRAPOS:
652      case OP_COND:
653      case OP_SBRA:
654      case OP_SBRAPOS:
655      case OP_SCBRA:
656      case OP_SCBRAPOS:
657      case OP_SCOND:
658      case OP_CREF:
659      case OP_DNCREF:
660      case OP_RREF:
661      case OP_DNRREF:
662    case OP_DEF:    case OP_DEF:
663    case OP_BRAZERO:    case OP_BRAZERO:
664    case OP_BRAMINZERO:    case OP_BRAMINZERO:
665    case OP_BRAPOSZERO:    case OP_BRAPOSZERO:
666      case OP_PRUNE:
667      case OP_SKIP:
668      case OP_THEN:
669    case OP_COMMIT:    case OP_COMMIT:
670    case OP_FAIL:    case OP_FAIL:
671    case OP_ACCEPT:    case OP_ACCEPT:
672    case OP_ASSERT_ACCEPT:    case OP_ASSERT_ACCEPT:
673      case OP_CLOSE:
674    case OP_SKIPZERO:    case OP_SKIPZERO:
675    return cc + 1;    return cc + PRIV(OP_lengths)[*cc];
   
   case OP_ANYBYTE:  
 #ifdef SUPPORT_UTF  
   if (common->utf) return NULL;  
 #endif  
   return cc + 1;  
676    
677    case OP_CHAR:    case OP_CHAR:
678    case OP_CHARI:    case OP_CHARI:
# Line 557  switch(*cc) Line 684  switch(*cc)
684    case OP_MINPLUS:    case OP_MINPLUS:
685    case OP_QUERY:    case OP_QUERY:
686    case OP_MINQUERY:    case OP_MINQUERY:
687      case OP_UPTO:
688      case OP_MINUPTO:
689      case OP_EXACT:
690    case OP_POSSTAR:    case OP_POSSTAR:
691    case OP_POSPLUS:    case OP_POSPLUS:
692    case OP_POSQUERY:    case OP_POSQUERY:
693      case OP_POSUPTO:
694    case OP_STARI:    case OP_STARI:
695    case OP_MINSTARI:    case OP_MINSTARI:
696    case OP_PLUSI:    case OP_PLUSI:
697    case OP_MINPLUSI:    case OP_MINPLUSI:
698    case OP_QUERYI:    case OP_QUERYI:
699    case OP_MINQUERYI:    case OP_MINQUERYI:
700      case OP_UPTOI:
701      case OP_MINUPTOI:
702      case OP_EXACTI:
703    case OP_POSSTARI:    case OP_POSSTARI:
704    case OP_POSPLUSI:    case OP_POSPLUSI:
705    case OP_POSQUERYI:    case OP_POSQUERYI:
706      case OP_POSUPTOI:
707    case OP_NOTSTAR:    case OP_NOTSTAR:
708    case OP_NOTMINSTAR:    case OP_NOTMINSTAR:
709    case OP_NOTPLUS:    case OP_NOTPLUS:
710    case OP_NOTMINPLUS:    case OP_NOTMINPLUS:
711    case OP_NOTQUERY:    case OP_NOTQUERY:
712    case OP_NOTMINQUERY:    case OP_NOTMINQUERY:
713      case OP_NOTUPTO:
714      case OP_NOTMINUPTO:
715      case OP_NOTEXACT:
716    case OP_NOTPOSSTAR:    case OP_NOTPOSSTAR:
717    case OP_NOTPOSPLUS:    case OP_NOTPOSPLUS:
718    case OP_NOTPOSQUERY:    case OP_NOTPOSQUERY:
719      case OP_NOTPOSUPTO:
720    case OP_NOTSTARI:    case OP_NOTSTARI:
721    case OP_NOTMINSTARI:    case OP_NOTMINSTARI:
722    case OP_NOTPLUSI:    case OP_NOTPLUSI:
723    case OP_NOTMINPLUSI:    case OP_NOTMINPLUSI:
724    case OP_NOTQUERYI:    case OP_NOTQUERYI:
725    case OP_NOTMINQUERYI:    case OP_NOTMINQUERYI:
   case OP_NOTPOSSTARI:  
   case OP_NOTPOSPLUSI:  
   case OP_NOTPOSQUERYI:  
   cc += 2;  
 #ifdef SUPPORT_UTF  
   if (common->utf && HAS_EXTRALEN(cc[-1])) cc += GET_EXTRALEN(cc[-1]);  
 #endif  
   return cc;  
   
   case OP_UPTO:  
   case OP_MINUPTO:  
   case OP_EXACT:  
   case OP_POSUPTO:  
   case OP_UPTOI:  
   case OP_MINUPTOI:  
   case OP_EXACTI:  
   case OP_POSUPTOI:  
   case OP_NOTUPTO:  
   case OP_NOTMINUPTO:  
   case OP_NOTEXACT:  
   case OP_NOTPOSUPTO:  
726    case OP_NOTUPTOI:    case OP_NOTUPTOI:
727    case OP_NOTMINUPTOI:    case OP_NOTMINUPTOI:
728    case OP_NOTEXACTI:    case OP_NOTEXACTI:
729      case OP_NOTPOSSTARI:
730      case OP_NOTPOSPLUSI:
731      case OP_NOTPOSQUERYI:
732    case OP_NOTPOSUPTOI:    case OP_NOTPOSUPTOI:
733    cc += 2 + IMM2_SIZE;    cc += PRIV(OP_lengths)[*cc];
734  #ifdef SUPPORT_UTF  #ifdef SUPPORT_UTF
735    if (common->utf && HAS_EXTRALEN(cc[-1])) cc += GET_EXTRALEN(cc[-1]);    if (common->utf && HAS_EXTRALEN(cc[-1])) cc += GET_EXTRALEN(cc[-1]);
736  #endif  #endif
737    return cc;    return cc;
738    
739    case OP_NOTPROP:    /* Special cases. */
740    case OP_PROP:    case OP_TYPESTAR:
741    return cc + 1 + 2;    case OP_TYPEMINSTAR:
742      case OP_TYPEPLUS:
743      case OP_TYPEMINPLUS:
744      case OP_TYPEQUERY:
745      case OP_TYPEMINQUERY:
746    case OP_TYPEUPTO:    case OP_TYPEUPTO:
747    case OP_TYPEMINUPTO:    case OP_TYPEMINUPTO:
748    case OP_TYPEEXACT:    case OP_TYPEEXACT:
749      case OP_TYPEPOSSTAR:
750      case OP_TYPEPOSPLUS:
751      case OP_TYPEPOSQUERY:
752    case OP_TYPEPOSUPTO:    case OP_TYPEPOSUPTO:
753    case OP_REF:    return cc + PRIV(OP_lengths)[*cc] - 1;
   case OP_REFI:  
   case OP_CREF:  
   case OP_NCREF:  
   case OP_RREF:  
   case OP_NRREF:  
   case OP_CLOSE:  
   cc += 1 + IMM2_SIZE;  
   return cc;  
754    
755    case OP_CRRANGE:    case OP_ANYBYTE:
756    case OP_CRMINRANGE:  #ifdef SUPPORT_UTF
757    return cc + 1 + 2 * IMM2_SIZE;    if (common->utf) return NULL;
758    #endif
759    case OP_CLASS:    return cc + 1;
   case OP_NCLASS:  
   return cc + 1 + 32 / sizeof(pcre_uchar);  
760    
761  #if defined SUPPORT_UTF || !defined COMPILE_PCRE8  #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
762    case OP_XCLASS:    case OP_XCLASS:
763    return cc + GET(cc, 1);    return cc + GET(cc, 1);
764  #endif  #endif
765    
   case OP_RECURSE:  
   case OP_ASSERT:  
   case OP_ASSERT_NOT:  
   case OP_ASSERTBACK:  
   case OP_ASSERTBACK_NOT:  
   case OP_REVERSE:  
   case OP_ONCE:  
   case OP_ONCE_NC:  
   case OP_BRA:  
   case OP_BRAPOS:  
   case OP_COND:  
   case OP_SBRA:  
   case OP_SBRAPOS:  
   case OP_SCOND:  
   case OP_ALT:  
   case OP_KET:  
   case OP_KETRMAX:  
   case OP_KETRMIN:  
   case OP_KETRPOS:  
   return cc + 1 + LINK_SIZE;  
   
   case OP_CBRA:  
   case OP_CBRAPOS:  
   case OP_SCBRA:  
   case OP_SCBRAPOS:  
   return cc + 1 + LINK_SIZE + IMM2_SIZE;  
   
766    case OP_MARK:    case OP_MARK:
767      case OP_PRUNE_ARG:
768      case OP_SKIP_ARG:
769      case OP_THEN_ARG:
770    return cc + 1 + 2 + cc[1];    return cc + 1 + 2 + cc[1];
771    
772    default:    default:
773      /* All opcodes are supported now! */
774      SLJIT_ASSERT_STOP();
775    return NULL;    return NULL;
776    }    }
777  }  }
778    
779  #define CASE_ITERATOR_PRIVATE_DATA_1 \  static BOOL check_opcode_types(compiler_common *common, pcre_uchar *cc, pcre_uchar *ccend)
780      case OP_MINSTAR: \  {
781      case OP_MINPLUS: \  int count;
782      case OP_QUERY: \  pcre_uchar *slot;
     case OP_MINQUERY: \  
     case OP_MINSTARI: \  
     case OP_MINPLUSI: \  
     case OP_QUERYI: \  
     case OP_MINQUERYI: \  
     case OP_NOTMINSTAR: \  
     case OP_NOTMINPLUS: \  
     case OP_NOTQUERY: \  
     case OP_NOTMINQUERY: \  
     case OP_NOTMINSTARI: \  
     case OP_NOTMINPLUSI: \  
     case OP_NOTQUERYI: \  
     case OP_NOTMINQUERYI:  
783    
784  #define CASE_ITERATOR_PRIVATE_DATA_2A \  /* Calculate important variables (like stack size) and checks whether all opcodes are supported. */
785      case OP_STAR: \  while (cc < ccend)
786      case OP_PLUS: \    {
787      case OP_STARI: \    switch(*cc)
788      case OP_PLUSI: \      {
789      case OP_NOTSTAR: \      case OP_SET_SOM:
790      case OP_NOTPLUS: \      common->has_set_som = TRUE;
791      case OP_NOTSTARI: \      common->might_be_empty = TRUE;
792      case OP_NOTPLUSI:      cc += 1;
793        break;
794    
795  #define CASE_ITERATOR_PRIVATE_DATA_2B \      case OP_REF:
796      case OP_UPTO: \      case OP_REFI:
797      case OP_MINUPTO: \      common->optimized_cbracket[GET2(cc, 1)] = 0;
798      case OP_UPTOI: \      cc += 1 + IMM2_SIZE;
799      case OP_MINUPTOI: \      break;
     case OP_NOTUPTO: \  
     case OP_NOTMINUPTO: \  
     case OP_NOTUPTOI: \  
     case OP_NOTMINUPTOI:  
800    
801  #define CASE_ITERATOR_TYPE_PRIVATE_DATA_1 \      case OP_CBRAPOS:
802      case OP_TYPEMINSTAR: \      case OP_SCBRAPOS:
803      case OP_TYPEMINPLUS: \      common->optimized_cbracket[GET2(cc, 1 + LINK_SIZE)] = 0;
804      case OP_TYPEQUERY: \      cc += 1 + LINK_SIZE + IMM2_SIZE;
805      case OP_TYPEMINQUERY:      break;
806    
807  #define CASE_ITERATOR_TYPE_PRIVATE_DATA_2A \      case OP_COND:
808      case OP_TYPESTAR: \      case OP_SCOND:
809      case OP_TYPEPLUS:      /* Only AUTO_CALLOUT can insert this opcode. We do
810           not intend to support this case. */
811        if (cc[1 + LINK_SIZE] == OP_CALLOUT)
812          return FALSE;
813        cc += 1 + LINK_SIZE;
814        break;
815    
816  #define CASE_ITERATOR_TYPE_PRIVATE_DATA_2B \      case OP_CREF:
817      case OP_TYPEUPTO: \      common->optimized_cbracket[GET2(cc, 1)] = 0;
818      case OP_TYPEMINUPTO:      cc += 1 + IMM2_SIZE;
819        break;
820    
821        case OP_DNREF:
822        case OP_DNREFI:
823        case OP_DNCREF:
824        count = GET2(cc, 1 + IMM2_SIZE);
825        slot = common->name_table + GET2(cc, 1) * common->name_entry_size;
826        while (count-- > 0)
827          {
828          common->optimized_cbracket[GET2(slot, 0)] = 0;
829          slot += common->name_entry_size;
830          }
831        cc += 1 + 2 * IMM2_SIZE;
832        break;
833    
834        case OP_RECURSE:
835        /* Set its value only once. */
836        if (common->recursive_head_ptr == 0)
837          {
838          common->recursive_head_ptr = common->ovector_start;
839          common->ovector_start += sizeof(sljit_sw);
840          }
841        cc += 1 + LINK_SIZE;
842        break;
843    
844        case OP_CALLOUT:
845        if (common->capture_last_ptr == 0)
846          {
847          common->capture_last_ptr = common->ovector_start;
848          common->ovector_start += sizeof(sljit_sw);
849          }
850        cc += 2 + 2 * LINK_SIZE;
851        break;
852    
853        case OP_THEN_ARG:
854        common->has_then = TRUE;
855        common->control_head_ptr = 1;
856        /* Fall through. */
857    
858        case OP_PRUNE_ARG:
859        common->needs_start_ptr = TRUE;
860        /* Fall through. */
861    
862        case OP_MARK:
863        if (common->mark_ptr == 0)
864          {
865          common->mark_ptr = common->ovector_start;
866          common->ovector_start += sizeof(sljit_sw);
867          }
868        cc += 1 + 2 + cc[1];
869        break;
870    
871        case OP_THEN:
872        common->has_then = TRUE;
873        common->control_head_ptr = 1;
874        /* Fall through. */
875    
876        case OP_PRUNE:
877        case OP_SKIP:
878        common->needs_start_ptr = TRUE;
879        cc += 1;
880        break;
881    
882        case OP_SKIP_ARG:
883        common->control_head_ptr = 1;
884        common->has_skip_arg = TRUE;
885        cc += 1 + 2 + cc[1];
886        break;
887    
888        default:
889        cc = next_opcode(common, cc);
890        if (cc == NULL)
891          return FALSE;
892        break;
893        }
894      }
895    return TRUE;
896    }
897    
898  static int get_class_iterator_size(pcre_uchar *cc)  static int get_class_iterator_size(pcre_uchar *cc)
899  {  {
# Line 758  switch(*cc) Line 920  switch(*cc)
920    }    }
921  }  }
922    
923  static int get_private_data_length(compiler_common *common, pcre_uchar *cc, pcre_uchar *ccend)  static BOOL detect_repeat(compiler_common *common, pcre_uchar *begin)
924    {
925    pcre_uchar *end = bracketend(begin);
926    pcre_uchar *next;
927    pcre_uchar *next_end;
928    pcre_uchar *max_end;
929    pcre_uchar type;
930    sljit_sw length = end - begin;
931    int min, max, i;
932    
933    /* Detect fixed iterations first. */
934    if (end[-(1 + LINK_SIZE)] != OP_KET)
935      return FALSE;
936    
937    /* Already detected repeat. */
938    if (common->private_data_ptrs[end - common->start - LINK_SIZE] != 0)
939      return TRUE;
940    
941    next = end;
942    min = 1;
943    while (1)
944      {
945      if (*next != *begin)
946        break;
947      next_end = bracketend(next);
948      if (next_end - next != length || memcmp(begin, next, IN_UCHARS(length)) != 0)
949        break;
950      next = next_end;
951      min++;
952      }
953    
954    if (min == 2)
955      return FALSE;
956    
957    max = 0;
958    max_end = next;
959    if (*next == OP_BRAZERO || *next == OP_BRAMINZERO)
960      {
961      type = *next;
962      while (1)
963        {
964        if (next[0] != type || next[1] != OP_BRA || next[2 + LINK_SIZE] != *begin)
965          break;
966        next_end = bracketend(next + 2 + LINK_SIZE);
967        if (next_end - next != (length + 2 + LINK_SIZE) || memcmp(begin, next + 2 + LINK_SIZE, IN_UCHARS(length)) != 0)
968          break;
969        next = next_end;
970        max++;
971        }
972    
973      if (next[0] == type && next[1] == *begin && max >= 1)
974        {
975        next_end = bracketend(next + 1);
976        if (next_end - next == (length + 1) && memcmp(begin, next + 1, IN_UCHARS(length)) == 0)
977          {
978          for (i = 0; i < max; i++, next_end += 1 + LINK_SIZE)
979            if (*next_end != OP_KET)
980              break;
981    
982          if (i == max)
983            {
984            common->private_data_ptrs[max_end - common->start - LINK_SIZE] = next_end - max_end;
985            common->private_data_ptrs[max_end - common->start - LINK_SIZE + 1] = (type == OP_BRAZERO) ? OP_UPTO : OP_MINUPTO;
986            /* +2 the original and the last. */
987            common->private_data_ptrs[max_end - common->start - LINK_SIZE + 2] = max + 2;
988            if (min == 1)
989              return TRUE;
990            min--;
991            max_end -= (1 + LINK_SIZE) + GET(max_end, -LINK_SIZE);
992            }
993          }
994        }
995      }
996    
997    if (min >= 3)
998      {
999      common->private_data_ptrs[end - common->start - LINK_SIZE] = max_end - end;
1000      common->private_data_ptrs[end - common->start - LINK_SIZE + 1] = OP_EXACT;
1001      common->private_data_ptrs[end - common->start - LINK_SIZE + 2] = min;
1002      return TRUE;
1003      }
1004    
1005    return FALSE;
1006    }
1007    
1008    #define CASE_ITERATOR_PRIVATE_DATA_1 \
1009        case OP_MINSTAR: \
1010        case OP_MINPLUS: \
1011        case OP_QUERY: \
1012        case OP_MINQUERY: \
1013        case OP_MINSTARI: \
1014        case OP_MINPLUSI: \
1015        case OP_QUERYI: \
1016        case OP_MINQUERYI: \
1017        case OP_NOTMINSTAR: \
1018        case OP_NOTMINPLUS: \
1019        case OP_NOTQUERY: \
1020        case OP_NOTMINQUERY: \
1021        case OP_NOTMINSTARI: \
1022        case OP_NOTMINPLUSI: \
1023        case OP_NOTQUERYI: \
1024        case OP_NOTMINQUERYI:
1025    
1026    #define CASE_ITERATOR_PRIVATE_DATA_2A \
1027        case OP_STAR: \
1028        case OP_PLUS: \
1029        case OP_STARI: \
1030        case OP_PLUSI: \
1031        case OP_NOTSTAR: \
1032        case OP_NOTPLUS: \
1033        case OP_NOTSTARI: \
1034        case OP_NOTPLUSI:
1035    
1036    #define CASE_ITERATOR_PRIVATE_DATA_2B \
1037        case OP_UPTO: \
1038        case OP_MINUPTO: \
1039        case OP_UPTOI: \
1040        case OP_MINUPTOI: \
1041        case OP_NOTUPTO: \
1042        case OP_NOTMINUPTO: \
1043        case OP_NOTUPTOI: \
1044        case OP_NOTMINUPTOI:
1045    
1046    #define CASE_ITERATOR_TYPE_PRIVATE_DATA_1 \
1047        case OP_TYPEMINSTAR: \
1048        case OP_TYPEMINPLUS: \
1049        case OP_TYPEQUERY: \
1050        case OP_TYPEMINQUERY:
1051    
1052    #define CASE_ITERATOR_TYPE_PRIVATE_DATA_2A \
1053        case OP_TYPESTAR: \
1054        case OP_TYPEPLUS:
1055    
1056    #define CASE_ITERATOR_TYPE_PRIVATE_DATA_2B \
1057        case OP_TYPEUPTO: \
1058        case OP_TYPEMINUPTO:
1059    
1060    static void set_private_data_ptrs(compiler_common *common, int *private_data_start, pcre_uchar *ccend)
1061  {  {
1062  int private_data_length = 0;  pcre_uchar *cc = common->start;
1063  pcre_uchar *alternative;  pcre_uchar *alternative;
 pcre_uchar *name;  
1064  pcre_uchar *end = NULL;  pcre_uchar *end = NULL;
1065  int space, size, bracketlen, i;  int private_data_ptr = *private_data_start;
1066    int space, size, bracketlen;
1067    BOOL repeat_check = TRUE;
1068    
 /* Calculate important variables (like stack size) and checks whether all opcodes are supported. */  
1069  while (cc < ccend)  while (cc < ccend)
1070    {    {
1071    space = 0;    space = 0;
1072    size = 0;    size = 0;
1073    bracketlen = 0;    bracketlen = 0;
1074    switch(*cc)    if (private_data_ptr > SLJIT_MAX_LOCAL_SIZE)
     {  
     case OP_SET_SOM:  
     common->has_set_som = TRUE;  
     cc += 1;  
1075      break;      break;
1076    
1077      case OP_REF:    if (repeat_check && (*cc == OP_ONCE || *cc == OP_ONCE_NC || *cc == OP_BRA || *cc == OP_CBRA || *cc == OP_COND))
1078      case OP_REFI:      {
1079      common->optimized_cbracket[GET2(cc, 1)] = 0;      if (detect_repeat(common, cc))
1080      cc += 1 + IMM2_SIZE;        {
1081          /* These brackets are converted to repeats, so no global
1082          based single character repeat is allowed. */
1083          if (cc >= end)
1084            end = bracketend(cc);
1085          }
1086        }
1087      repeat_check = TRUE;
1088    
1089      switch(*cc)
1090        {
1091        case OP_KET:
1092        if (common->private_data_ptrs[cc + 1 - common->start] != 0)
1093          {
1094          common->private_data_ptrs[cc - common->start] = private_data_ptr;
1095          private_data_ptr += sizeof(sljit_sw);
1096          cc += common->private_data_ptrs[cc + 1 - common->start];
1097          }
1098        cc += 1 + LINK_SIZE;
1099      break;      break;
1100    
1101      case OP_ASSERT:      case OP_ASSERT:
# Line 794  while (cc < ccend) Line 1107  while (cc < ccend)
1107      case OP_BRAPOS:      case OP_BRAPOS:
1108      case OP_SBRA:      case OP_SBRA:
1109      case OP_SBRAPOS:      case OP_SBRAPOS:
1110      private_data_length += sizeof(sljit_w);      case OP_SCOND:
1111        common->private_data_ptrs[cc - common->start] = private_data_ptr;
1112        private_data_ptr += sizeof(sljit_sw);
1113      bracketlen = 1 + LINK_SIZE;      bracketlen = 1 + LINK_SIZE;
1114      break;      break;
1115    
1116      case OP_CBRAPOS:      case OP_CBRAPOS:
1117      case OP_SCBRAPOS:      case OP_SCBRAPOS:
1118      private_data_length += sizeof(sljit_w);      common->private_data_ptrs[cc - common->start] = private_data_ptr;
1119      common->optimized_cbracket[GET2(cc, 1 + LINK_SIZE)] = 0;      private_data_ptr += sizeof(sljit_sw);
1120      bracketlen = 1 + LINK_SIZE + IMM2_SIZE;      bracketlen = 1 + LINK_SIZE + IMM2_SIZE;
1121      break;      break;
1122    
1123      case OP_COND:      case OP_COND:
1124      case OP_SCOND:      /* Might be a hidden SCOND. */
1125      bracketlen = cc[1 + LINK_SIZE];      alternative = cc + GET(cc, 1);
1126      if (bracketlen == OP_CREF)      if (*alternative == OP_KETRMAX || *alternative == OP_KETRMIN)
       {  
       bracketlen = GET2(cc, 1 + LINK_SIZE + 1);  
       common->optimized_cbracket[bracketlen] = 0;  
       }  
     else if (bracketlen == OP_NCREF)  
       {  
       bracketlen = GET2(cc, 1 + LINK_SIZE + 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;  
         }  
       }  
   
     if (*cc == OP_COND)  
1127        {        {
1128        /* Might be a hidden SCOND. */        common->private_data_ptrs[cc - common->start] = private_data_ptr;
1129        alternative = cc + GET(cc, 1);        private_data_ptr += sizeof(sljit_sw);
       if (*alternative == OP_KETRMAX || *alternative == OP_KETRMIN)  
         private_data_length += sizeof(sljit_w);  
1130        }        }
     else  
       private_data_length += sizeof(sljit_w);  
1131      bracketlen = 1 + LINK_SIZE;      bracketlen = 1 + LINK_SIZE;
1132      break;      break;
1133    
# Line 854  while (cc < ccend) Line 1140  while (cc < ccend)
1140      bracketlen = 1 + LINK_SIZE + IMM2_SIZE;      bracketlen = 1 + LINK_SIZE + IMM2_SIZE;
1141      break;      break;
1142    
1143        case OP_BRAZERO:
1144        case OP_BRAMINZERO:
1145        case OP_BRAPOSZERO:
1146        repeat_check = FALSE;
1147        size = 1;
1148        break;
1149    
1150      CASE_ITERATOR_PRIVATE_DATA_1      CASE_ITERATOR_PRIVATE_DATA_1
1151      space = 1;      space = 1;
1152      size = -2;      size = -2;
# Line 880  while (cc < ccend) Line 1173  while (cc < ccend)
1173      size = 1;      size = 1;
1174      break;      break;
1175    
1176      CASE_ITERATOR_TYPE_PRIVATE_DATA_2B      case OP_TYPEUPTO:
1177      if (cc[1 + IMM2_SIZE] != OP_ANYNL && cc[1 + IMM2_SIZE] != OP_EXTUNI)      if (cc[1 + IMM2_SIZE] != OP_ANYNL && cc[1 + IMM2_SIZE] != OP_EXTUNI)
1178        space = 2;        space = 2;
1179      size = 1 + IMM2_SIZE;      size = 1 + IMM2_SIZE;
1180      break;      break;
1181    
1182        case OP_TYPEMINUPTO:
1183        space = 2;
1184        size = 1 + IMM2_SIZE;
1185        break;
1186    
1187      case OP_CLASS:      case OP_CLASS:
1188      case OP_NCLASS:      case OP_NCLASS:
1189      size += 1 + 32 / sizeof(pcre_uchar);      size += 1 + 32 / sizeof(pcre_uchar);
# Line 899  while (cc < ccend) Line 1197  while (cc < ccend)
1197      break;      break;
1198  #endif  #endif
1199    
     case OP_RECURSE:  
     /* Set its value only once. */  
     if (common->recursive_head == 0)  
       {  
       common->recursive_head = common->ovector_start;  
       common->ovector_start += sizeof(sljit_w);  
       }  
     cc += 1 + LINK_SIZE;  
     break;  
   
     case OP_MARK:  
     if (common->mark_ptr == 0)  
       {  
       common->mark_ptr = common->ovector_start;  
       common->ovector_start += sizeof(sljit_w);  
       }  
     cc += 1 + 2 + cc[1];  
     break;  
   
1200      default:      default:
1201      cc = next_opcode(common, cc);      cc = next_opcode(common, cc);
1202      if (cc == NULL)      SLJIT_ASSERT(cc != NULL);
       return -1;  
1203      break;      break;
1204      }      }
1205    
1206      /* Character iterators, which are not inside a repeated bracket,
1207         gets a private slot instead of allocating it on the stack. */
1208    if (space > 0 && cc >= end)    if (space > 0 && cc >= end)
1209      private_data_length += sizeof(sljit_w) * space;      {
1210        common->private_data_ptrs[cc - common->start] = private_data_ptr;
1211        private_data_ptr += sizeof(sljit_sw) * space;
1212        }
1213    
1214    if (size != 0)    if (size != 0)
1215      {      {
# Line 952  while (cc < ccend) Line 1235  while (cc < ccend)
1235      cc += bracketlen;      cc += bracketlen;
1236      }      }
1237    }    }
1238  return private_data_length;  *private_data_start = private_data_ptr;
1239  }  }
1240    
1241  static void set_private_data_ptrs(compiler_common *common, int private_data_ptr, pcre_uchar *ccend)  /* Returns with a frame_types (always < 0) if no need for frame. */
1242    static int get_framesize(compiler_common *common, pcre_uchar *cc, pcre_uchar *ccend, BOOL recursive, BOOL *needs_control_head)
1243  {  {
1244  pcre_uchar *cc = common->start;  int length = 0;
1245  pcre_uchar *alternative;  int possessive = 0;
1246  pcre_uchar *end = NULL;  BOOL stack_restore = FALSE;
1247  int space, size, bracketlen;  BOOL setsom_found = recursive;
1248    BOOL setmark_found = recursive;
1249    /* The last capture is a local variable even for recursions. */
1250    BOOL capture_last_found = FALSE;
1251    
1252  while (cc < ccend)  #if defined DEBUG_FORCE_CONTROL_HEAD && DEBUG_FORCE_CONTROL_HEAD
1253    SLJIT_ASSERT(common->control_head_ptr != 0);
1254    *needs_control_head = TRUE;
1255    #else
1256    *needs_control_head = FALSE;
1257    #endif
1258    
1259    if (ccend == NULL)
1260    {    {
1261    space = 0;    ccend = bracketend(cc) - (1 + LINK_SIZE);
1262    size = 0;    if (!recursive && (*cc == OP_CBRAPOS || *cc == OP_SCBRAPOS))
1263    bracketlen = 0;      {
1264        possessive = length = (common->capture_last_ptr != 0) ? 5 : 3;
1265        /* This is correct regardless of common->capture_last_ptr. */
1266        capture_last_found = TRUE;
1267        }
1268      cc = next_opcode(common, cc);
1269      }
1270    
1271    SLJIT_ASSERT(cc != NULL);
1272    while (cc < ccend)
1273    switch(*cc)    switch(*cc)
1274      {      {
1275      case OP_ASSERT:      case OP_SET_SOM:
1276      case OP_ASSERT_NOT:      SLJIT_ASSERT(common->has_set_som);
1277      case OP_ASSERTBACK:      stack_restore = TRUE;
1278      case OP_ASSERTBACK_NOT:      if (!setsom_found)
1279      case OP_ONCE:        {
1280      case OP_ONCE_NC:        length += 2;
1281      case OP_BRAPOS:        setsom_found = TRUE;
1282      case OP_SBRA:        }
1283      case OP_SBRAPOS:      cc += 1;
     case OP_SCOND:  
     common->private_data_ptrs[cc - common->start] = private_data_ptr;  
     private_data_ptr += sizeof(sljit_w);  
     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_w);  
     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_w);  
       }  
     bracketlen = 1 + LINK_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  
   
     default:  
     cc = next_opcode(common, cc);  
     SLJIT_ASSERT(cc != NULL);  
     break;  
     }  
   
   if (space > 0 && cc >= end)  
     {  
     common->private_data_ptrs[cc - common->start] = private_data_ptr;  
     private_data_ptr += sizeof(sljit_w) * 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;  
     }  
   }  
 }  
   
 /* Returns with -1 if no need for frame. */  
 static int get_framesize(compiler_common *common, pcre_uchar *cc, BOOL recursive)  
 {  
 pcre_uchar *ccend = bracketend(cc);  
 int length = 0;  
 BOOL possessive = FALSE;  
 BOOL setsom_found = recursive;  
 BOOL setmark_found = recursive;  
   
 if (!recursive && (*cc == OP_CBRAPOS || *cc == OP_SCBRAPOS))  
   {  
   length = 3;  
   possessive = TRUE;  
   }  
   
 cc = next_opcode(common, cc);  
 SLJIT_ASSERT(cc != NULL);  
 while (cc < ccend)  
   switch(*cc)  
     {  
     case OP_SET_SOM:  
     SLJIT_ASSERT(common->has_set_som);  
     if (!setsom_found)  
       {  
       length += 2;  
       setsom_found = TRUE;  
       }  
     cc += 1;  
1284      break;      break;
1285    
1286      case OP_MARK:      case OP_MARK:
1287        case OP_PRUNE_ARG:
1288        case OP_THEN_ARG:
1289      SLJIT_ASSERT(common->mark_ptr != 0);      SLJIT_ASSERT(common->mark_ptr != 0);
1290        stack_restore = TRUE;
1291      if (!setmark_found)      if (!setmark_found)
1292        {        {
1293        length += 2;        length += 2;
1294        setmark_found = TRUE;        setmark_found = TRUE;
1295        }        }
1296        if (common->control_head_ptr != 0)
1297          *needs_control_head = TRUE;
1298      cc += 1 + 2 + cc[1];      cc += 1 + 2 + cc[1];
1299      break;      break;
1300    
1301      case OP_RECURSE:      case OP_RECURSE:
1302        stack_restore = TRUE;
1303      if (common->has_set_som && !setsom_found)      if (common->has_set_som && !setsom_found)
1304        {        {
1305        length += 2;        length += 2;
# Line 1145  while (cc < ccend) Line 1310  while (cc < ccend)
1310        length += 2;        length += 2;
1311        setmark_found = TRUE;        setmark_found = TRUE;
1312        }        }
1313        if (common->capture_last_ptr != 0 && !capture_last_found)
1314          {
1315          length += 2;
1316          capture_last_found = TRUE;
1317          }
1318      cc += 1 + LINK_SIZE;      cc += 1 + LINK_SIZE;
1319      break;      break;
1320    
# Line 1152  while (cc < ccend) Line 1322  while (cc < ccend)
1322      case OP_CBRAPOS:      case OP_CBRAPOS:
1323      case OP_SCBRA:      case OP_SCBRA:
1324      case OP_SCBRAPOS:      case OP_SCBRAPOS:
1325        stack_restore = TRUE;
1326        if (common->capture_last_ptr != 0 && !capture_last_found)
1327          {
1328          length += 2;
1329          capture_last_found = TRUE;
1330          }
1331      length += 3;      length += 3;
1332      cc += 1 + LINK_SIZE + IMM2_SIZE;      cc += 1 + LINK_SIZE + IMM2_SIZE;
1333      break;      break;
1334    
1335        case OP_THEN:
1336        stack_restore = TRUE;
1337        if (common->control_head_ptr != 0)
1338          *needs_control_head = TRUE;
1339        cc ++;
1340        break;
1341    
1342      default:      default:
1343        stack_restore = TRUE;
1344        /* Fall through. */
1345    
1346        case OP_NOT_WORD_BOUNDARY:
1347        case OP_WORD_BOUNDARY:
1348        case OP_NOT_DIGIT:
1349        case OP_DIGIT:
1350        case OP_NOT_WHITESPACE:
1351        case OP_WHITESPACE:
1352        case OP_NOT_WORDCHAR:
1353        case OP_WORDCHAR:
1354        case OP_ANY:
1355        case OP_ALLANY:
1356        case OP_ANYBYTE:
1357        case OP_NOTPROP:
1358        case OP_PROP:
1359        case OP_ANYNL:
1360        case OP_NOT_HSPACE:
1361        case OP_HSPACE:
1362        case OP_NOT_VSPACE:
1363        case OP_VSPACE:
1364        case OP_EXTUNI:
1365        case OP_EODN:
1366        case OP_EOD:
1367        case OP_CIRC:
1368        case OP_CIRCM:
1369        case OP_DOLL:
1370        case OP_DOLLM:
1371        case OP_CHAR:
1372        case OP_CHARI:
1373        case OP_NOT:
1374        case OP_NOTI:
1375    
1376        case OP_EXACT:
1377        case OP_POSSTAR:
1378        case OP_POSPLUS:
1379        case OP_POSQUERY:
1380        case OP_POSUPTO:
1381    
1382        case OP_EXACTI:
1383        case OP_POSSTARI:
1384        case OP_POSPLUSI:
1385        case OP_POSQUERYI:
1386        case OP_POSUPTOI:
1387    
1388        case OP_NOTEXACT:
1389        case OP_NOTPOSSTAR:
1390        case OP_NOTPOSPLUS:
1391        case OP_NOTPOSQUERY:
1392        case OP_NOTPOSUPTO:
1393    
1394        case OP_NOTEXACTI:
1395        case OP_NOTPOSSTARI:
1396        case OP_NOTPOSPLUSI:
1397        case OP_NOTPOSQUERYI:
1398        case OP_NOTPOSUPTOI:
1399    
1400        case OP_TYPEEXACT:
1401        case OP_TYPEPOSSTAR:
1402        case OP_TYPEPOSPLUS:
1403        case OP_TYPEPOSQUERY:
1404        case OP_TYPEPOSUPTO:
1405    
1406        case OP_CLASS:
1407        case OP_NCLASS:
1408        case OP_XCLASS:
1409        case OP_CALLOUT:
1410    
1411      cc = next_opcode(common, cc);      cc = next_opcode(common, cc);
1412      SLJIT_ASSERT(cc != NULL);      SLJIT_ASSERT(cc != NULL);
1413      break;      break;
1414      }      }
1415    
1416  /* Possessive quantifiers can use a special case. */  /* Possessive quantifiers can use a special case. */
1417  if (SLJIT_UNLIKELY(possessive) && length == 3)  if (SLJIT_UNLIKELY(possessive == length))
1418    return -1;    return stack_restore ? no_frame : no_stack;
1419    
1420  if (length > 0)  if (length > 0)
1421    return length + 1;    return length + 1;
1422  return -1;  return stack_restore ? no_frame : no_stack;
1423  }  }
1424    
1425  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)
1426  {  {
1427  DEFINE_COMPILER;  DEFINE_COMPILER;
 pcre_uchar *ccend = bracketend(cc);  
1428  BOOL setsom_found = recursive;  BOOL setsom_found = recursive;
1429  BOOL setmark_found = recursive;  BOOL setmark_found = recursive;
1430    /* The last capture is a local variable even for recursions. */
1431    BOOL capture_last_found = FALSE;
1432  int offset;  int offset;
1433    
1434  /* >= 1 + shortest item size (2) */  /* >= 1 + shortest item size (2) */
# Line 1184  SLJIT_UNUSED_ARG(stacktop); Line 1436  SLJIT_UNUSED_ARG(stacktop);
1436  SLJIT_ASSERT(stackpos >= stacktop + 2);  SLJIT_ASSERT(stackpos >= stacktop + 2);
1437    
1438  stackpos = STACK(stackpos);  stackpos = STACK(stackpos);
1439  if (recursive || (*cc != OP_CBRAPOS && *cc != OP_SCBRAPOS))  if (ccend == NULL)
1440    cc = next_opcode(common, cc);    {
1441      ccend = bracketend(cc) - (1 + LINK_SIZE);
1442      if (recursive || (*cc != OP_CBRAPOS && *cc != OP_SCBRAPOS))
1443        cc = next_opcode(common, cc);
1444      }
1445    
1446  SLJIT_ASSERT(cc != NULL);  SLJIT_ASSERT(cc != NULL);
1447  while (cc < ccend)  while (cc < ccend)
1448    switch(*cc)    switch(*cc)
# Line 1194  while (cc < ccend) Line 1451  while (cc < ccend)
1451      SLJIT_ASSERT(common->has_set_som);      SLJIT_ASSERT(common->has_set_som);
1452      if (!setsom_found)      if (!setsom_found)
1453        {        {
1454        OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(0));        OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), OVECTOR(0));
1455        OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, SLJIT_IMM, frame_setstrbegin);        OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, SLJIT_IMM, -OVECTOR(0));
1456        stackpos += (int)sizeof(sljit_w);        stackpos += (int)sizeof(sljit_sw);
1457        OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, TMP1, 0);        OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, TMP1, 0);
1458        stackpos += (int)sizeof(sljit_w);        stackpos += (int)sizeof(sljit_sw);
1459        setsom_found = TRUE;        setsom_found = TRUE;
1460        }        }
1461      cc += 1;      cc += 1;
1462      break;      break;
1463    
1464      case OP_MARK:      case OP_MARK:
1465        case OP_PRUNE_ARG:
1466        case OP_THEN_ARG:
1467      SLJIT_ASSERT(common->mark_ptr != 0);      SLJIT_ASSERT(common->mark_ptr != 0);
1468      if (!setmark_found)      if (!setmark_found)
1469        {        {
1470        OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), common->mark_ptr);        OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), common->mark_ptr);
1471        OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, SLJIT_IMM, frame_setmark);        OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, SLJIT_IMM, -common->mark_ptr);
1472        stackpos += (int)sizeof(sljit_w);        stackpos += (int)sizeof(sljit_sw);
1473        OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, TMP1, 0);        OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, TMP1, 0);
1474        stackpos += (int)sizeof(sljit_w);        stackpos += (int)sizeof(sljit_sw);
1475        setmark_found = TRUE;        setmark_found = TRUE;
1476        }        }
1477      cc += 1 + 2 + cc[1];      cc += 1 + 2 + cc[1];
# Line 1221  while (cc < ccend) Line 1480  while (cc < ccend)
1480      case OP_RECURSE:      case OP_RECURSE:
1481      if (common->has_set_som && !setsom_found)      if (common->has_set_som && !setsom_found)
1482        {        {
1483        OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(0));        OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), OVECTOR(0));
1484        OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, SLJIT_IMM, frame_setstrbegin);        OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, SLJIT_IMM, -OVECTOR(0));
1485        stackpos += (int)sizeof(sljit_w);        stackpos += (int)sizeof(sljit_sw);
1486        OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, TMP1, 0);        OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, TMP1, 0);
1487        stackpos += (int)sizeof(sljit_w);        stackpos += (int)sizeof(sljit_sw);
1488        setsom_found = TRUE;        setsom_found = TRUE;
1489        }        }
1490      if (common->mark_ptr != 0 && !setmark_found)      if (common->mark_ptr != 0 && !setmark_found)
1491        {        {
1492        OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), common->mark_ptr);        OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), common->mark_ptr);
1493        OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, SLJIT_IMM, frame_setmark);        OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, SLJIT_IMM, -common->mark_ptr);
1494        stackpos += (int)sizeof(sljit_w);        stackpos += (int)sizeof(sljit_sw);
1495        OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, TMP1, 0);        OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, TMP1, 0);
1496        stackpos += (int)sizeof(sljit_w);        stackpos += (int)sizeof(sljit_sw);
1497        setmark_found = TRUE;        setmark_found = TRUE;
1498        }        }
1499        if (common->capture_last_ptr != 0 && !capture_last_found)
1500          {
1501          OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), common->capture_last_ptr);
1502          OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, SLJIT_IMM, -common->capture_last_ptr);
1503          stackpos += (int)sizeof(sljit_sw);
1504          OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, TMP1, 0);
1505          stackpos += (int)sizeof(sljit_sw);
1506          capture_last_found = TRUE;
1507          }
1508      cc += 1 + LINK_SIZE;      cc += 1 + LINK_SIZE;
1509      break;      break;
1510    
# Line 1244  while (cc < ccend) Line 1512  while (cc < ccend)
1512      case OP_CBRAPOS:      case OP_CBRAPOS:
1513      case OP_SCBRA:      case OP_SCBRA:
1514      case OP_SCBRAPOS:      case OP_SCBRAPOS:
1515        if (common->capture_last_ptr != 0 && !capture_last_found)
1516          {
1517          OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), common->capture_last_ptr);
1518          OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, SLJIT_IMM, -common->capture_last_ptr);
1519          stackpos += (int)sizeof(sljit_sw);
1520          OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, TMP1, 0);
1521          stackpos += (int)sizeof(sljit_sw);
1522          capture_last_found = TRUE;
1523          }
1524      offset = (GET2(cc, 1 + LINK_SIZE)) << 1;      offset = (GET2(cc, 1 + LINK_SIZE)) << 1;
1525      OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, SLJIT_IMM, OVECTOR(offset));      OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, SLJIT_IMM, OVECTOR(offset));
1526      stackpos += (int)sizeof(sljit_w);      stackpos += (int)sizeof(sljit_sw);
1527      OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(offset));      OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), OVECTOR(offset));
1528      OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(offset + 1));      OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_SP), OVECTOR(offset + 1));
1529      OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, TMP1, 0);      OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, TMP1, 0);
1530      stackpos += (int)sizeof(sljit_w);      stackpos += (int)sizeof(sljit_sw);
1531      OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, TMP2, 0);      OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, TMP2, 0);
1532      stackpos += (int)sizeof(sljit_w);      stackpos += (int)sizeof(sljit_sw);
1533    
1534      cc += 1 + LINK_SIZE + IMM2_SIZE;      cc += 1 + LINK_SIZE + IMM2_SIZE;
1535      break;      break;
# Line 1263  while (cc < ccend) Line 1540  while (cc < ccend)
1540      break;      break;
1541      }      }
1542    
1543  OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, SLJIT_IMM, frame_end);  OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, SLJIT_IMM, 0);
1544  SLJIT_ASSERT(stackpos == STACK(stacktop));  SLJIT_ASSERT(stackpos == STACK(stacktop));
1545  }  }
1546    
1547  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)
1548  {  {
1549  int private_data_length = 2;  int private_data_length = needs_control_head ? 3 : 2;
1550  int size;  int size;
1551  pcre_uchar *alternative;  pcre_uchar *alternative;
1552  /* Calculate the sum of the private machine words. */  /* Calculate the sum of the private machine words. */
# Line 1278  while (cc < ccend) Line 1555  while (cc < ccend)
1555    size = 0;    size = 0;
1556    switch(*cc)    switch(*cc)
1557      {      {
1558        case OP_KET:
1559        if (PRIVATE_DATA(cc) != 0)
1560          {
1561          private_data_length++;
1562          SLJIT_ASSERT(PRIVATE_DATA(cc + 1) != 0);
1563          cc += PRIVATE_DATA(cc + 1);
1564          }
1565        cc += 1 + LINK_SIZE;
1566        break;
1567    
1568      case OP_ASSERT:      case OP_ASSERT:
1569      case OP_ASSERT_NOT:      case OP_ASSERT_NOT:
1570      case OP_ASSERTBACK:      case OP_ASSERTBACK:
# Line 1289  while (cc < ccend) Line 1576  while (cc < ccend)
1576      case OP_SBRAPOS:      case OP_SBRAPOS:
1577      case OP_SCOND:      case OP_SCOND:
1578      private_data_length++;      private_data_length++;
1579        SLJIT_ASSERT(PRIVATE_DATA(cc) != 0);
1580      cc += 1 + LINK_SIZE;      cc += 1 + LINK_SIZE;
1581      break;      break;
1582    
# Line 1382  return private_data_length; Line 1670  return private_data_length;
1670  }  }
1671    
1672  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,
1673    BOOL save, int stackptr, int stacktop)    BOOL save, int stackptr, int stacktop, BOOL needs_control_head)
1674  {  {
1675  DEFINE_COMPILER;  DEFINE_COMPILER;
1676  int srcw[2];  int srcw[2];
# Line 1403  stacktop = STACK(stacktop - 1); Line 1691  stacktop = STACK(stacktop - 1);
1691    
1692  if (!save)  if (!save)
1693    {    {
1694    stackptr += sizeof(sljit_w);    stackptr += (needs_control_head ? 2 : 1) * sizeof(sljit_sw);
1695    if (stackptr < stacktop)    if (stackptr < stacktop)
1696      {      {
1697      OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(STACK_TOP), stackptr);      OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(STACK_TOP), stackptr);
1698      stackptr += sizeof(sljit_w);      stackptr += sizeof(sljit_sw);
1699      tmp1empty = FALSE;      tmp1empty = FALSE;
1700      }      }
1701    if (stackptr < stacktop)    if (stackptr < stacktop)
1702      {      {
1703      OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(STACK_TOP), stackptr);      OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(STACK_TOP), stackptr);
1704      stackptr += sizeof(sljit_w);      stackptr += sizeof(sljit_sw);
1705      tmp2empty = FALSE;      tmp2empty = FALSE;
1706      }      }
1707    /* The tmp1next must be TRUE in either way. */    /* The tmp1next must be TRUE in either way. */
1708    }    }
1709    
1710  while (status != end)  do
1711    {    {
1712    count = 0;    count = 0;
1713    switch(status)    switch(status)
1714      {      {
1715      case start:      case start:
1716      SLJIT_ASSERT(save && common->recursive_head != 0);      SLJIT_ASSERT(save && common->recursive_head_ptr != 0);
1717      count = 1;      count = 1;
1718      srcw[0] = common->recursive_head;      srcw[0] = common->recursive_head_ptr;
1719        if (needs_control_head)
1720          {
1721          SLJIT_ASSERT(common->control_head_ptr != 0);
1722          count = 2;
1723          srcw[1] = common->control_head_ptr;
1724          }
1725      status = loop;      status = loop;
1726      break;      break;
1727    
# Line 1440  while (status != end) Line 1734  while (status != end)
1734    
1735      switch(*cc)      switch(*cc)
1736        {        {
1737          case OP_KET:
1738          if (PRIVATE_DATA(cc) != 0)
1739            {
1740            count = 1;
1741            srcw[0] = PRIVATE_DATA(cc);
1742            SLJIT_ASSERT(PRIVATE_DATA(cc + 1) != 0);
1743            cc += PRIVATE_DATA(cc + 1);
1744            }
1745          cc += 1 + LINK_SIZE;
1746          break;
1747    
1748        case OP_ASSERT:        case OP_ASSERT:
1749        case OP_ASSERT_NOT:        case OP_ASSERT_NOT:
1750        case OP_ASSERTBACK:        case OP_ASSERTBACK:
# Line 1504  while (status != end) Line 1809  while (status != end)
1809          {          {
1810          count = 2;          count = 2;
1811          srcw[0] = PRIVATE_DATA(cc);          srcw[0] = PRIVATE_DATA(cc);
1812          srcw[1] = PRIVATE_DATA(cc) + sizeof(sljit_w);          srcw[1] = PRIVATE_DATA(cc) + sizeof(sljit_sw);
1813          }          }
1814        cc += 2;        cc += 2;
1815  #ifdef SUPPORT_UTF  #ifdef SUPPORT_UTF
# Line 1517  while (status != end) Line 1822  while (status != end)
1822          {          {
1823          count = 2;          count = 2;
1824          srcw[0] = PRIVATE_DATA(cc);          srcw[0] = PRIVATE_DATA(cc);
1825          srcw[1] = PRIVATE_DATA(cc) + sizeof(sljit_w);          srcw[1] = PRIVATE_DATA(cc) + sizeof(sljit_sw);
1826          }          }
1827        cc += 2 + IMM2_SIZE;        cc += 2 + IMM2_SIZE;
1828  #ifdef SUPPORT_UTF  #ifdef SUPPORT_UTF
# Line 1539  while (status != end) Line 1844  while (status != end)
1844          {          {
1845          count = 2;          count = 2;
1846          srcw[0] = PRIVATE_DATA(cc);          srcw[0] = PRIVATE_DATA(cc);
1847          srcw[1] = srcw[0] + sizeof(sljit_w);          srcw[1] = srcw[0] + sizeof(sljit_sw);
1848          }          }
1849        cc += 1;        cc += 1;
1850        break;        break;
# Line 1549  while (status != end) Line 1854  while (status != end)
1854          {          {
1855          count = 2;          count = 2;
1856          srcw[0] = PRIVATE_DATA(cc);          srcw[0] = PRIVATE_DATA(cc);
1857          srcw[1] = srcw[0] + sizeof(sljit_w);          srcw[1] = srcw[0] + sizeof(sljit_sw);
1858          }          }
1859        cc += 1 + IMM2_SIZE;        cc += 1 + IMM2_SIZE;
1860        break;        break;
# Line 1573  while (status != end) Line 1878  while (status != end)
1878            case 2:            case 2:
1879            count = 2;            count = 2;
1880            srcw[0] = PRIVATE_DATA(cc);            srcw[0] = PRIVATE_DATA(cc);
1881            srcw[1] = srcw[0] + sizeof(sljit_w);            srcw[1] = srcw[0] + sizeof(sljit_sw);
1882            break;            break;
1883    
1884            default:            default:
# Line 1605  while (status != end) Line 1910  while (status != end)
1910          if (!tmp1empty)          if (!tmp1empty)
1911            {            {
1912            OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackptr, TMP1, 0);            OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackptr, TMP1, 0);
1913            stackptr += sizeof(sljit_w);            stackptr += sizeof(sljit_sw);
1914            }            }
1915          OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), srcw[count]);          OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), srcw[count]);
1916          tmp1empty = FALSE;          tmp1empty = FALSE;
1917          tmp1next = FALSE;          tmp1next = FALSE;
1918          }          }
# Line 1616  while (status != end) Line 1921  while (status != end)
1921          if (!tmp2empty)          if (!tmp2empty)
1922            {            {
1923            OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackptr, TMP2, 0);            OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackptr, TMP2, 0);
1924            stackptr += sizeof(sljit_w);            stackptr += sizeof(sljit_sw);
1925            }            }
1926          OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), srcw[count]);          OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_SP), srcw[count]);
1927          tmp2empty = FALSE;          tmp2empty = FALSE;
1928          tmp1next = TRUE;          tmp1next = TRUE;
1929          }          }
# Line 1628  while (status != end) Line 1933  while (status != end)
1933        if (tmp1next)        if (tmp1next)
1934          {          {
1935          SLJIT_ASSERT(!tmp1empty);          SLJIT_ASSERT(!tmp1empty);
1936          OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), srcw[count], TMP1, 0);          OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), srcw[count], TMP1, 0);
1937          tmp1empty = stackptr >= stacktop;          tmp1empty = stackptr >= stacktop;
1938          if (!tmp1empty)          if (!tmp1empty)
1939            {            {
1940            OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(STACK_TOP), stackptr);            OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(STACK_TOP), stackptr);
1941            stackptr += sizeof(sljit_w);            stackptr += sizeof(sljit_sw);
1942            }            }
1943          tmp1next = FALSE;          tmp1next = FALSE;
1944          }          }
1945        else        else
1946          {          {
1947          SLJIT_ASSERT(!tmp2empty);          SLJIT_ASSERT(!tmp2empty);
1948          OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), srcw[count], TMP2, 0);          OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), srcw[count], TMP2, 0);
1949          tmp2empty = stackptr >= stacktop;          tmp2empty = stackptr >= stacktop;
1950          if (!tmp2empty)          if (!tmp2empty)
1951            {            {
1952            OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(STACK_TOP), stackptr);            OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(STACK_TOP), stackptr);
1953            stackptr += sizeof(sljit_w);            stackptr += sizeof(sljit_sw);
1954            }            }
1955          tmp1next = TRUE;          tmp1next = TRUE;
1956          }          }
1957        }        }
1958      }      }
1959    }    }
1960    while (status != end);
1961    
1962  if (save)  if (save)
1963    {    {
# Line 1660  if (save) Line 1966  if (save)
1966      if (!tmp1empty)      if (!tmp1empty)
1967        {        {
1968        OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackptr, TMP1, 0);        OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackptr, TMP1, 0);
1969        stackptr += sizeof(sljit_w);        stackptr += sizeof(sljit_sw);
1970        }        }
1971      if (!tmp2empty)      if (!tmp2empty)
1972        {        {
1973        OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackptr, TMP2, 0);        OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackptr, TMP2, 0);
1974        stackptr += sizeof(sljit_w);        stackptr += sizeof(sljit_sw);
1975        }        }
1976      }      }
1977    else    else
# Line 1673  if (save) Line 1979  if (save)
1979      if (!tmp2empty)      if (!tmp2empty)
1980        {        {
1981        OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackptr, TMP2, 0);        OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackptr, TMP2, 0);
1982        stackptr += sizeof(sljit_w);        stackptr += sizeof(sljit_sw);
1983        }        }
1984      if (!tmp1empty)      if (!tmp1empty)
1985        {        {
1986        OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackptr, TMP1, 0);        OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackptr, TMP1, 0);
1987        stackptr += sizeof(sljit_w);        stackptr += sizeof(sljit_sw);
1988        }        }
1989      }      }
1990    }    }
1991  SLJIT_ASSERT(cc == ccend && stackptr == stacktop && (save || (tmp1empty && tmp2empty)));  SLJIT_ASSERT(cc == ccend && stackptr == stacktop && (save || (tmp1empty && tmp2empty)));
1992  }  }
1993    
1994    static SLJIT_INLINE pcre_uchar *set_then_offsets(compiler_common *common, pcre_uchar *cc, pcre_uint8 *current_offset)
1995    {
1996    pcre_uchar *end = bracketend(cc);
1997    BOOL has_alternatives = cc[GET(cc, 1)] == OP_ALT;
1998    
1999    /* Assert captures then. */
2000    if (*cc >= OP_ASSERT && *cc <= OP_ASSERTBACK_NOT)
2001      current_offset = NULL;
2002    /* Conditional block does not. */
2003    if (*cc == OP_COND || *cc == OP_SCOND)
2004      has_alternatives = FALSE;
2005    
2006    cc = next_opcode(common, cc);
2007    if (has_alternatives)
2008      current_offset = common->then_offsets + (cc - common->start);
2009    
2010    while (cc < end)
2011      {
2012      if ((*cc >= OP_ASSERT && *cc <= OP_ASSERTBACK_NOT) || (*cc >= OP_ONCE && *cc <= OP_SCOND))
2013        cc = set_then_offsets(common, cc, current_offset);
2014      else
2015        {
2016        if (*cc == OP_ALT && has_alternatives)
2017          current_offset = common->then_offsets + (cc + 1 + LINK_SIZE - common->start);
2018        if (*cc >= OP_THEN && *cc <= OP_THEN_ARG && current_offset != NULL)
2019          *current_offset = 1;
2020        cc = next_opcode(common, cc);
2021        }
2022      }
2023    
2024    return end;
2025    }
2026    
2027  #undef CASE_ITERATOR_PRIVATE_DATA_1  #undef CASE_ITERATOR_PRIVATE_DATA_1
2028  #undef CASE_ITERATOR_PRIVATE_DATA_2A  #undef CASE_ITERATOR_PRIVATE_DATA_2A
2029  #undef CASE_ITERATOR_PRIVATE_DATA_2B  #undef CASE_ITERATOR_PRIVATE_DATA_2B
# Line 1692  SLJIT_ASSERT(cc == ccend && stackptr == Line 2031  SLJIT_ASSERT(cc == ccend && stackptr ==
2031  #undef CASE_ITERATOR_TYPE_PRIVATE_DATA_2A  #undef CASE_ITERATOR_TYPE_PRIVATE_DATA_2A
2032  #undef CASE_ITERATOR_TYPE_PRIVATE_DATA_2B  #undef CASE_ITERATOR_TYPE_PRIVATE_DATA_2B
2033    
2034  static SLJIT_INLINE BOOL ispowerof2(unsigned int value)  static SLJIT_INLINE BOOL is_powerof2(unsigned int value)
2035  {  {
2036  return (value & (value - 1)) == 0;  return (value & (value - 1)) == 0;
2037  }  }
# Line 1703  while (list) Line 2042  while (list)
2042    {    {
2043    /* sljit_set_label is clever enough to do nothing    /* sljit_set_label is clever enough to do nothing
2044    if either the jump or the label is NULL. */    if either the jump or the label is NULL. */
2045    sljit_set_label(list->jump, label);    SET_LABEL(list->jump, label);
2046    list = list->next;    list = list->next;
2047    }    }
2048  }  }
2049    
2050  static SLJIT_INLINE void add_jump(struct sljit_compiler *compiler, jump_list **list, struct sljit_jump* jump)  static SLJIT_INLINE void add_jump(struct sljit_compiler *compiler, jump_list **list, struct sljit_jump *jump)
2051  {  {
2052  jump_list *list_item = sljit_alloc_memory(compiler, sizeof(jump_list));  jump_list *list_item = sljit_alloc_memory(compiler, sizeof(jump_list));
2053  if (list_item)  if (list_item)
# Line 1719  if (list_item) Line 2058  if (list_item)
2058    }    }
2059  }  }
2060    
2061  static void add_stub(compiler_common *common, enum stub_types type, int data, struct sljit_jump *start)  static void add_stub(compiler_common *common, struct sljit_jump *start)
2062  {  {
2063  DEFINE_COMPILER;  DEFINE_COMPILER;
2064  stub_list* list_item = sljit_alloc_memory(compiler, sizeof(stub_list));  stub_list *list_item = sljit_alloc_memory(compiler, sizeof(stub_list));
2065    
2066  if (list_item)  if (list_item)
2067    {    {
   list_item->type = type;  
   list_item->data = data;  
2068    list_item->start = start;    list_item->start = start;
2069    list_item->quit = LABEL();    list_item->quit = LABEL();
2070    list_item->next = common->stubs;    list_item->next = common->stubs;
# Line 1738  if (list_item) Line 2075  if (list_item)
2075  static void flush_stubs(compiler_common *common)  static void flush_stubs(compiler_common *common)
2076  {  {
2077  DEFINE_COMPILER;  DEFINE_COMPILER;
2078  stub_list* list_item = common->stubs;  stub_list *list_item = common->stubs;
2079    
2080  while (list_item)  while (list_item)
2081    {    {
2082    JUMPHERE(list_item->start);    JUMPHERE(list_item->start);
2083    switch(list_item->type)    add_jump(compiler, &common->stackalloc, JUMP(SLJIT_FAST_CALL));
     {  
     case stack_alloc:  
     add_jump(compiler, &common->stackalloc, JUMP(SLJIT_FAST_CALL));  
     break;  
     }  
2084    JUMPTO(SLJIT_JUMP, list_item->quit);    JUMPTO(SLJIT_JUMP, list_item->quit);
2085    list_item = list_item->next;    list_item = list_item->next;
2086    }    }
2087  common->stubs = NULL;  common->stubs = NULL;
2088  }  }
2089    
2090  static SLJIT_INLINE void decrease_call_count(compiler_common *common)  static void add_label_addr(compiler_common *common, sljit_uw *update_addr)
2091  {  {
2092  DEFINE_COMPILER;  DEFINE_COMPILER;
2093    label_addr_list *label_addr;
2094    
2095  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));
2096  add_jump(compiler, &common->calllimit, JUMP(SLJIT_C_ZERO));  if (label_addr == NULL)
2097      return;
2098    label_addr->label = LABEL();
2099    label_addr->update_addr = update_addr;
2100    label_addr->next = common->label_addrs;
2101    common->label_addrs = label_addr;
2102  }  }
2103    
2104  static SLJIT_INLINE void allocate_stack(compiler_common *common, int size)  static SLJIT_INLINE void count_match(compiler_common *common)
2105  {  {
 /* May destroy all locals and registers except TMP2. */  
2106  DEFINE_COMPILER;  DEFINE_COMPILER;
2107    
2108  OP2(SLJIT_ADD, STACK_TOP, 0, STACK_TOP, 0, SLJIT_IMM, size * sizeof(sljit_w));  OP2(SLJIT_SUB | SLJIT_SET_E, COUNT_MATCH, 0, COUNT_MATCH, 0, SLJIT_IMM, 1);
2109  #ifdef DESTROY_REGISTERS  add_jump(compiler, &common->calllimit, JUMP(SLJIT_ZERO));
2110  OP1(SLJIT_MOV, TMP1, 0, SLJIT_IMM, 12345);  }
2111    
2112    static SLJIT_INLINE void allocate_stack(compiler_common *common, int size)
2113    {
2114    /* May destroy all locals and registers except TMP2. */
2115    DEFINE_COMPILER;
2116    
2117    SLJIT_ASSERT(size > 0);
2118    OP2(SLJIT_ADD, STACK_TOP, 0, STACK_TOP, 0, SLJIT_IMM, size * sizeof(sljit_sw));
2119    #ifdef DESTROY_REGISTERS
2120    OP1(SLJIT_MOV, TMP1, 0, SLJIT_IMM, 12345);
2121  OP1(SLJIT_MOV, TMP3, 0, TMP1, 0);  OP1(SLJIT_MOV, TMP3, 0, TMP1, 0);
2122  OP1(SLJIT_MOV, RETURN_ADDR, 0, TMP1, 0);  OP1(SLJIT_MOV, RETURN_ADDR, 0, TMP1, 0);
2123  OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), LOCALS0, TMP1, 0);  OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), LOCALS0, TMP1, 0);
2124  OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), LOCALS1, TMP1, 0);  OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), LOCALS1, TMP1, 0);
2125  #endif  #endif
2126  add_stub(common, stack_alloc, 0, CMP(SLJIT_C_GREATER, STACK_TOP, 0, STACK_LIMIT, 0));  add_stub(common, CMP(SLJIT_GREATER, STACK_TOP, 0, STACK_LIMIT, 0));
2127  }  }
2128    
2129  static SLJIT_INLINE void free_stack(compiler_common *common, int size)  static SLJIT_INLINE void free_stack(compiler_common *common, int size)
2130  {  {
2131  DEFINE_COMPILER;  DEFINE_COMPILER;
2132  OP2(SLJIT_SUB, STACK_TOP, 0, STACK_TOP, 0, SLJIT_IMM, size * sizeof(sljit_w));  
2133    SLJIT_ASSERT(size > 0);
2134    OP2(SLJIT_SUB, STACK_TOP, 0, STACK_TOP, 0, SLJIT_IMM, size * sizeof(sljit_sw));
2135    }
2136    
2137    static sljit_uw * allocate_read_only_data(compiler_common *common, sljit_uw size)
2138    {
2139    DEFINE_COMPILER;
2140    sljit_uw *result;
2141    
2142    if (SLJIT_UNLIKELY(sljit_get_compiler_error(compiler)))
2143      return NULL;
2144    
2145    result = (sljit_uw *)SLJIT_MALLOC(size + sizeof(sljit_uw), compiler->allocator_data);
2146    if (SLJIT_UNLIKELY(result == NULL))
2147      {
2148      sljit_set_compiler_memory_error(compiler);
2149      return NULL;
2150      }
2151    
2152    *(void**)result = common->read_only_data_head;
2153    common->read_only_data_head = (void *)result;
2154    return result + 1;
2155    }
2156    
2157    static void free_read_only_data(void *current, void *allocator_data)
2158    {
2159    void *next;
2160    
2161    SLJIT_UNUSED_ARG(allocator_data);
2162    
2163    while (current != NULL)
2164      {
2165      next = *(void**)current;
2166      SLJIT_FREE(current, allocator_data);
2167      current = next;
2168      }
2169  }  }
2170    
2171  static SLJIT_INLINE void reset_ovector(compiler_common *common, int length)  static SLJIT_INLINE void reset_ovector(compiler_common *common, int length)
# Line 1790  static SLJIT_INLINE void reset_ovector(c Line 2173  static SLJIT_INLINE void reset_ovector(c
2173  DEFINE_COMPILER;  DEFINE_COMPILER;
2174  struct sljit_label *loop;  struct sljit_label *loop;
2175  int i;  int i;
2176    
2177  /* At this point we can freely use all temporary registers. */  /* At this point we can freely use all temporary registers. */
2178    SLJIT_ASSERT(length > 1);
2179  /* TMP1 returns with begin - 1. */  /* TMP1 returns with begin - 1. */
2180  OP2(SLJIT_SUB, SLJIT_TEMPORARY_REG1, 0, SLJIT_MEM1(SLJIT_SAVED_REG1), SLJIT_OFFSETOF(jit_arguments, begin), SLJIT_IMM, IN_UCHARS(1));  OP2(SLJIT_SUB, SLJIT_R0, 0, SLJIT_MEM1(SLJIT_S0), SLJIT_OFFSETOF(jit_arguments, begin), SLJIT_IMM, IN_UCHARS(1));
2181    if (length < 8)
2182      {
2183      for (i = 1; i < length; i++)
2184        OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), OVECTOR(i), SLJIT_R0, 0);
2185      }
2186    else
2187      {
2188      GET_LOCAL_BASE(SLJIT_R1, 0, OVECTOR_START);
2189      OP1(SLJIT_MOV, SLJIT_R2, 0, SLJIT_IMM, length - 1);
2190      loop = LABEL();
2191      OP1(SLJIT_MOVU, SLJIT_MEM1(SLJIT_R1), sizeof(sljit_sw), SLJIT_R0, 0);
2192      OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_R2, 0, SLJIT_R2, 0, SLJIT_IMM, 1);
2193      JUMPTO(SLJIT_NOT_ZERO, loop);
2194      }
2195    }
2196    
2197    static SLJIT_INLINE void do_reset_match(compiler_common *common, int length)
2198    {
2199    DEFINE_COMPILER;
2200    struct sljit_label *loop;
2201    int i;
2202    
2203    SLJIT_ASSERT(length > 1);
2204    /* OVECTOR(1) contains the "string begin - 1" constant. */
2205    if (length > 2)
2206      OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), OVECTOR(1));
2207  if (length < 8)  if (length < 8)
2208    {    {
2209    for (i = 0; i < length; i++)    for (i = 2; i < length; i++)
2210      OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(i), SLJIT_TEMPORARY_REG1, 0);      OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), OVECTOR(i), TMP1, 0);
2211    }    }
2212  else  else
2213    {    {
2214    GET_LOCAL_BASE(SLJIT_TEMPORARY_REG2, 0, OVECTOR_START - sizeof(sljit_w));    GET_LOCAL_BASE(TMP2, 0, OVECTOR_START + sizeof(sljit_sw));
2215    OP1(SLJIT_MOV, SLJIT_TEMPORARY_REG3, 0, SLJIT_IMM, length);    OP1(SLJIT_MOV, STACK_TOP, 0, SLJIT_IMM, length - 2);
2216    loop = LABEL();    loop = LABEL();
2217    OP1(SLJIT_MOVU, SLJIT_MEM1(SLJIT_TEMPORARY_REG2), sizeof(sljit_w), SLJIT_TEMPORARY_REG1, 0);    OP1(SLJIT_MOVU, SLJIT_MEM1(TMP2), sizeof(sljit_sw), TMP1, 0);
2218    OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_TEMPORARY_REG3, 0, SLJIT_TEMPORARY_REG3, 0, SLJIT_IMM, 1);    OP2(SLJIT_SUB | SLJIT_SET_E, STACK_TOP, 0, STACK_TOP, 0, SLJIT_IMM, 1);
2219    JUMPTO(SLJIT_C_NOT_ZERO, loop);    JUMPTO(SLJIT_NOT_ZERO, loop);
2220      }
2221    
2222    OP1(SLJIT_MOV, STACK_TOP, 0, ARGUMENTS, 0);
2223    if (common->mark_ptr != 0)
2224      OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->mark_ptr, SLJIT_IMM, 0);
2225    if (common->control_head_ptr != 0)
2226      OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->control_head_ptr, SLJIT_IMM, 0);
2227    OP1(SLJIT_MOV, STACK_TOP, 0, SLJIT_MEM1(STACK_TOP), SLJIT_OFFSETOF(jit_arguments, stack));
2228    OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), common->start_ptr);
2229    OP1(SLJIT_MOV, STACK_TOP, 0, SLJIT_MEM1(STACK_TOP), SLJIT_OFFSETOF(struct sljit_stack, base));
2230    }
2231    
2232    static sljit_sw SLJIT_CALL do_search_mark(sljit_sw *current, const pcre_uchar *skip_arg)
2233    {
2234    while (current != NULL)
2235      {
2236      switch (current[-2])
2237        {
2238        case type_then_trap:
2239        break;
2240    
2241        case type_mark:
2242        if (STRCMP_UC_UC(skip_arg, (pcre_uchar *)current[-3]) == 0)
2243          return current[-4];
2244        break;
2245    
2246        default:
2247        SLJIT_ASSERT_STOP();
2248        break;
2249        }
2250      SLJIT_ASSERT(current > (sljit_sw*)current[-1]);
2251      current = (sljit_sw*)current[-1];
2252    }    }
2253    return -1;
2254  }  }
2255    
2256  static SLJIT_INLINE void copy_ovector(compiler_common *common, int topbracket)  static SLJIT_INLINE void copy_ovector(compiler_common *common, int topbracket)
2257  {  {
2258  DEFINE_COMPILER;  DEFINE_COMPILER;
2259  struct sljit_label *loop;  struct sljit_label *loop;
2260  struct sljit_jump *earlyexit;  struct sljit_jump *early_quit;
2261    
2262  /* At this point we can freely use all registers. */  /* At this point we can freely use all registers. */
2263  OP1(SLJIT_MOV, SLJIT_SAVED_REG3, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(1));  OP1(SLJIT_MOV, SLJIT_S2, 0, SLJIT_MEM1(SLJIT_SP), OVECTOR(1));
2264  OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(1), STR_PTR, 0);  OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), OVECTOR(1), STR_PTR, 0);
2265    
2266  OP1(SLJIT_MOV, SLJIT_TEMPORARY_REG1, 0, ARGUMENTS, 0);  OP1(SLJIT_MOV, SLJIT_R0, 0, ARGUMENTS, 0);
2267  if (common->mark_ptr != 0)  if (common->mark_ptr != 0)
2268    OP1(SLJIT_MOV, SLJIT_TEMPORARY_REG3, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), common->mark_ptr);    OP1(SLJIT_MOV, SLJIT_R2, 0, SLJIT_MEM1(SLJIT_SP), common->mark_ptr);
2269  OP1(SLJIT_MOV_SI, SLJIT_TEMPORARY_REG2, 0, SLJIT_MEM1(SLJIT_TEMPORARY_REG1), SLJIT_OFFSETOF(jit_arguments, offsetcount));  OP1(SLJIT_MOV_SI, SLJIT_R1, 0, SLJIT_MEM1(SLJIT_R0), SLJIT_OFFSETOF(jit_arguments, offset_count));
2270  if (common->mark_ptr != 0)  if (common->mark_ptr != 0)
2271    OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_TEMPORARY_REG1), SLJIT_OFFSETOF(jit_arguments, mark_ptr), SLJIT_TEMPORARY_REG3, 0);    OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_R0), SLJIT_OFFSETOF(jit_arguments, mark_ptr), SLJIT_R2, 0);
2272  OP2(SLJIT_SUB, SLJIT_TEMPORARY_REG3, 0, SLJIT_MEM1(SLJIT_TEMPORARY_REG1), SLJIT_OFFSETOF(jit_arguments, offsets), SLJIT_IMM, sizeof(int));  OP2(SLJIT_SUB, SLJIT_R2, 0, SLJIT_MEM1(SLJIT_R0), SLJIT_OFFSETOF(jit_arguments, offsets), SLJIT_IMM, sizeof(int));
2273  OP1(SLJIT_MOV, SLJIT_TEMPORARY_REG1, 0, SLJIT_MEM1(SLJIT_TEMPORARY_REG1), SLJIT_OFFSETOF(jit_arguments, begin));  OP1(SLJIT_MOV, SLJIT_R0, 0, SLJIT_MEM1(SLJIT_R0), SLJIT_OFFSETOF(jit_arguments, begin));
2274  GET_LOCAL_BASE(SLJIT_SAVED_REG1, 0, OVECTOR_START);  GET_LOCAL_BASE(SLJIT_S0, 0, OVECTOR_START);
2275  /* Unlikely, but possible */  /* Unlikely, but possible */
2276  earlyexit = CMP(SLJIT_C_EQUAL, SLJIT_TEMPORARY_REG2, 0, SLJIT_IMM, 0);  early_quit = CMP(SLJIT_EQUAL, SLJIT_R1, 0, SLJIT_IMM, 0);
2277  loop = LABEL();  loop = LABEL();
2278  OP2(SLJIT_SUB, SLJIT_SAVED_REG2, 0, SLJIT_MEM1(SLJIT_SAVED_REG1), 0, SLJIT_TEMPORARY_REG1, 0);  OP2(SLJIT_SUB, SLJIT_S1, 0, SLJIT_MEM1(SLJIT_S0), 0, SLJIT_R0, 0);
2279  OP2(SLJIT_ADD, SLJIT_SAVED_REG1, 0, SLJIT_SAVED_REG1, 0, SLJIT_IMM, sizeof(sljit_w));  OP2(SLJIT_ADD, SLJIT_S0, 0, SLJIT_S0, 0, SLJIT_IMM, sizeof(sljit_sw));
2280  /* Copy the integer value to the output buffer */  /* Copy the integer value to the output buffer */
2281  #ifdef COMPILE_PCRE16  #if defined COMPILE_PCRE16 || defined COMPILE_PCRE32
2282  OP2(SLJIT_ASHR, SLJIT_SAVED_REG2, 0, SLJIT_SAVED_REG2, 0, SLJIT_IMM, 1);  OP2(SLJIT_ASHR, SLJIT_S1, 0, SLJIT_S1, 0, SLJIT_IMM, UCHAR_SHIFT);
2283  #endif  #endif
2284  OP1(SLJIT_MOVU_SI, SLJIT_MEM1(SLJIT_TEMPORARY_REG3), sizeof(int), SLJIT_SAVED_REG2, 0);  OP1(SLJIT_MOVU_SI, SLJIT_MEM1(SLJIT_R2), sizeof(int), SLJIT_S1, 0);
2285  OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_TEMPORARY_REG2, 0, SLJIT_TEMPORARY_REG2, 0, SLJIT_IMM, 1);  OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_R1, 0, SLJIT_R1, 0, SLJIT_IMM, 1);
2286  JUMPTO(SLJIT_C_NOT_ZERO, loop);  JUMPTO(SLJIT_NOT_ZERO, loop);
2287  JUMPHERE(earlyexit);  JUMPHERE(early_quit);
2288    
2289  /* Calculate the return value, which is the maximum ovector value. */  /* Calculate the return value, which is the maximum ovector value. */
2290  if (topbracket > 1)  if (topbracket > 1)
2291    {    {
2292    GET_LOCAL_BASE(SLJIT_TEMPORARY_REG1, 0, OVECTOR_START + topbracket * 2 * sizeof(sljit_w));    GET_LOCAL_BASE(SLJIT_R0, 0, OVECTOR_START + topbracket * 2 * sizeof(sljit_sw));
2293    OP1(SLJIT_MOV, SLJIT_TEMPORARY_REG2, 0, SLJIT_IMM, topbracket + 1);    OP1(SLJIT_MOV, SLJIT_R1, 0, SLJIT_IMM, topbracket + 1);
2294    
2295    /* OVECTOR(0) is never equal to SLJIT_SAVED_REG3. */    /* OVECTOR(0) is never equal to SLJIT_S2. */
2296    loop = LABEL();    loop = LABEL();
2297    OP1(SLJIT_MOVU, SLJIT_TEMPORARY_REG3, 0, SLJIT_MEM1(SLJIT_TEMPORARY_REG1), -(2 * (sljit_w)sizeof(sljit_w)));    OP1(SLJIT_MOVU, SLJIT_R2, 0, SLJIT_MEM1(SLJIT_R0), -(2 * (sljit_sw)sizeof(sljit_sw)));
2298    OP2(SLJIT_SUB, SLJIT_TEMPORARY_REG2, 0, SLJIT_TEMPORARY_REG2, 0, SLJIT_IMM, 1);    OP2(SLJIT_SUB, SLJIT_R1, 0, SLJIT_R1, 0, SLJIT_IMM, 1);
2299    CMPTO(SLJIT_C_EQUAL, SLJIT_TEMPORARY_REG3, 0, SLJIT_SAVED_REG3, 0, loop);    CMPTO(SLJIT_EQUAL, SLJIT_R2, 0, SLJIT_S2, 0, loop);
2300    OP1(SLJIT_MOV, SLJIT_RETURN_REG, 0, SLJIT_TEMPORARY_REG2, 0);    OP1(SLJIT_MOV, SLJIT_RETURN_REG, 0, SLJIT_R1, 0);
2301    }    }
2302  else  else
2303    OP1(SLJIT_MOV, SLJIT_RETURN_REG, 0, SLJIT_IMM, 1);    OP1(SLJIT_MOV, SLJIT_RETURN_REG, 0, SLJIT_IMM, 1);
# Line 1862  else Line 2306  else
2306  static SLJIT_INLINE void return_with_partial_match(compiler_common *common, struct sljit_label *quit)  static SLJIT_INLINE void return_with_partial_match(compiler_common *common, struct sljit_label *quit)
2307  {  {
2308  DEFINE_COMPILER;  DEFINE_COMPILER;
2309    struct sljit_jump *jump;
2310    
2311  SLJIT_COMPILE_ASSERT(STR_END == SLJIT_SAVED_REG2, str_end_must_be_saved_reg2);  SLJIT_COMPILE_ASSERT(STR_END == SLJIT_S1, str_end_must_be_saved_reg2);
2312  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
2313      && (common->mode == JIT_PARTIAL_SOFT_COMPILE ? common->hit_start != 0 : common->hit_start == 0));
2314    
2315  OP1(SLJIT_MOV, SLJIT_TEMPORARY_REG2, 0, ARGUMENTS, 0);  OP1(SLJIT_MOV, SLJIT_R1, 0, ARGUMENTS, 0);
2316  OP1(SLJIT_MOV, SLJIT_RETURN_REG, 0, SLJIT_IMM, PCRE_ERROR_PARTIAL);  OP1(SLJIT_MOV, SLJIT_RETURN_REG, 0, SLJIT_IMM, PCRE_ERROR_PARTIAL);
2317  OP1(SLJIT_MOV_SI, SLJIT_TEMPORARY_REG3, 0, SLJIT_MEM1(SLJIT_TEMPORARY_REG2), SLJIT_OFFSETOF(jit_arguments, offsetcount));  OP1(SLJIT_MOV_SI, SLJIT_R2, 0, SLJIT_MEM1(SLJIT_R1), SLJIT_OFFSETOF(jit_arguments, real_offset_count));
2318  CMPTO(SLJIT_C_LESS, SLJIT_TEMPORARY_REG3, 0, SLJIT_IMM, 2, quit);  CMPTO(SLJIT_SIG_LESS, SLJIT_R2, 0, SLJIT_IMM, 2, quit);
2319    
2320  /* Store match begin and end. */  /* Store match begin and end. */
2321  OP1(SLJIT_MOV, SLJIT_SAVED_REG1, 0, SLJIT_MEM1(SLJIT_TEMPORARY_REG2), SLJIT_OFFSETOF(jit_arguments, begin));  OP1(SLJIT_MOV, SLJIT_S0, 0, SLJIT_MEM1(SLJIT_R1), SLJIT_OFFSETOF(jit_arguments, begin));
2322  OP1(SLJIT_MOV, SLJIT_TEMPORARY_REG2, 0, SLJIT_MEM1(SLJIT_TEMPORARY_REG2), SLJIT_OFFSETOF(jit_arguments, offsets));  OP1(SLJIT_MOV, SLJIT_R1, 0, SLJIT_MEM1(SLJIT_R1), SLJIT_OFFSETOF(jit_arguments, offsets));
2323  OP1(SLJIT_MOV, SLJIT_TEMPORARY_REG3, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), common->mode == JIT_PARTIAL_HARD_COMPILE ? common->start_used_ptr : common->hit_start);  
2324  OP2(SLJIT_SUB, SLJIT_SAVED_REG2, 0, STR_END, 0, SLJIT_SAVED_REG1, 0);  jump = CMP(SLJIT_SIG_LESS, SLJIT_R2, 0, SLJIT_IMM, 3);
2325  #ifdef COMPILE_PCRE16  OP2(SLJIT_SUB, SLJIT_R2, 0, SLJIT_MEM1(SLJIT_SP), common->mode == JIT_PARTIAL_HARD_COMPILE ? common->start_ptr : (common->hit_start + (int)sizeof(sljit_sw)), SLJIT_S0, 0);
2326  OP2(SLJIT_ASHR, SLJIT_SAVED_REG2, 0, SLJIT_SAVED_REG2, 0, SLJIT_IMM, 1);  #if defined COMPILE_PCRE16 || defined COMPILE_PCRE32
2327  #endif  OP2(SLJIT_ASHR, SLJIT_R2, 0, SLJIT_R2, 0, SLJIT_IMM, UCHAR_SHIFT);
 OP1(SLJIT_MOV_SI, SLJIT_MEM1(SLJIT_TEMPORARY_REG2), sizeof(int), SLJIT_SAVED_REG2, 0);  
   
 OP2(SLJIT_SUB, SLJIT_TEMPORARY_REG3, 0, SLJIT_TEMPORARY_REG3, 0, SLJIT_SAVED_REG1, 0);  
 #ifdef COMPILE_PCRE16  
 OP2(SLJIT_ASHR, SLJIT_TEMPORARY_REG3, 0, SLJIT_TEMPORARY_REG3, 0, SLJIT_IMM, 1);  
2328  #endif  #endif
2329  OP1(SLJIT_MOV_SI, SLJIT_MEM1(SLJIT_TEMPORARY_REG2), 0, SLJIT_TEMPORARY_REG3, 0);  OP1(SLJIT_MOV_SI, SLJIT_MEM1(SLJIT_R1), 2 * sizeof(int), SLJIT_R2, 0);
2330    JUMPHERE(jump);
2331    
2332    OP1(SLJIT_MOV, SLJIT_R2, 0, SLJIT_MEM1(SLJIT_SP), common->mode == JIT_PARTIAL_HARD_COMPILE ? common->start_used_ptr : common->hit_start);
2333    OP2(SLJIT_SUB, SLJIT_S1, 0, STR_END, 0, SLJIT_S0, 0);
2334    #if defined COMPILE_PCRE16 || defined COMPILE_PCRE32
2335    OP2(SLJIT_ASHR, SLJIT_S1, 0, SLJIT_S1, 0, SLJIT_IMM, UCHAR_SHIFT);
2336    #endif
2337    OP1(SLJIT_MOV_SI, SLJIT_MEM1(SLJIT_R1), sizeof(int), SLJIT_S1, 0);
2338    
2339    OP2(SLJIT_SUB, SLJIT_R2, 0, SLJIT_R2, 0, SLJIT_S0, 0);
2340    #if defined COMPILE_PCRE16 || defined COMPILE_PCRE32
2341    OP2(SLJIT_ASHR, SLJIT_R2, 0, SLJIT_R2, 0, SLJIT_IMM, UCHAR_SHIFT);
2342    #endif
2343    OP1(SLJIT_MOV_SI, SLJIT_MEM1(SLJIT_R1), 0, SLJIT_R2, 0);
2344    
2345  JUMPTO(SLJIT_JUMP, quit);  JUMPTO(SLJIT_JUMP, quit);
2346  }  }
# Line 1899  struct sljit_jump *jump; Line 2354  struct sljit_jump *jump;
2354  if (common->mode == JIT_PARTIAL_SOFT_COMPILE)  if (common->mode == JIT_PARTIAL_SOFT_COMPILE)
2355    {    {
2356    /* The value of -1 must be kept for start_used_ptr! */    /* The value of -1 must be kept for start_used_ptr! */
2357    OP2(SLJIT_ADD, TMP1, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), common->start_used_ptr, SLJIT_IMM, 1);    OP2(SLJIT_ADD, TMP1, 0, SLJIT_MEM1(SLJIT_SP), common->start_used_ptr, SLJIT_IMM, 1);
2358    /* Jumps if start_used_ptr < STR_PTR, or start_used_ptr == -1. Although overwriting    /* Jumps if start_used_ptr < STR_PTR, or start_used_ptr == -1. Although overwriting
2359    is not necessary if start_used_ptr == STR_PTR, it does not hurt as well. */    is not necessary if start_used_ptr == STR_PTR, it does not hurt as well. */
2360    jump = CMP(SLJIT_C_LESS_EQUAL, TMP1, 0, STR_PTR, 0);    jump = CMP(SLJIT_LESS_EQUAL, TMP1, 0, STR_PTR, 0);
2361    OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), common->start_used_ptr, STR_PTR, 0);    OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->start_used_ptr, STR_PTR, 0);
2362    JUMPHERE(jump);    JUMPHERE(jump);
2363    }    }
2364  else if (common->mode == JIT_PARTIAL_HARD_COMPILE)  else if (common->mode == JIT_PARTIAL_HARD_COMPILE)
2365    {    {
2366    jump = CMP(SLJIT_C_LESS_EQUAL, SLJIT_MEM1(SLJIT_LOCALS_REG), common->start_used_ptr, STR_PTR, 0);    jump = CMP(SLJIT_LESS_EQUAL, SLJIT_MEM1(SLJIT_SP), common->start_used_ptr, STR_PTR, 0);
2367    OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), common->start_used_ptr, STR_PTR, 0);    OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->start_used_ptr, STR_PTR, 0);
2368    JUMPHERE(jump);    JUMPHERE(jump);
2369    }    }
2370  }  }
2371    
2372  static SLJIT_INLINE BOOL char_has_othercase(compiler_common *common, pcre_uchar* cc)  static SLJIT_INLINE BOOL char_has_othercase(compiler_common *common, pcre_uchar *cc)
2373  {  {
2374  /* Detects if the character has an othercase. */  /* Detects if the character has an othercase. */
2375  unsigned int c;  unsigned int c;
# Line 1957  if (common->utf && c > 127) Line 2412  if (common->utf && c > 127)
2412  return TABLE_GET(c, common->fcc, c);  return TABLE_GET(c, common->fcc, c);
2413  }  }
2414    
2415  static unsigned int char_get_othercase_bit(compiler_common *common, pcre_uchar* cc)  static unsigned int char_get_othercase_bit(compiler_common *common, pcre_uchar *cc)
2416  {  {
2417  /* Detects if the character and its othercase has only 1 bit difference. */  /* Detects if the character and its othercase has only 1 bit difference. */
2418  unsigned int c, oc, bit;  unsigned int c, oc, bit;
# Line 1998  if (c <= 127 && bit == 0x20) Line 2453  if (c <= 127 && bit == 0x20)
2453    return (0 << 8) | 0x20;    return (0 << 8) | 0x20;
2454    
2455  /* Since c != oc, they must have at least 1 bit difference. */  /* Since c != oc, they must have at least 1 bit difference. */
2456  if (!ispowerof2(bit))  if (!is_powerof2(bit))
2457    return 0;    return 0;
2458    
2459  #ifdef COMPILE_PCRE8  #if defined COMPILE_PCRE8
2460    
2461  #ifdef SUPPORT_UTF  #ifdef SUPPORT_UTF
2462  if (common->utf && c > 127)  if (common->utf && c > 127)
# Line 2017  if (common->utf && c > 127) Line 2472  if (common->utf && c > 127)
2472  #endif /* SUPPORT_UTF */  #endif /* SUPPORT_UTF */
2473  return (0 << 8) | bit;  return (0 << 8) | bit;
2474    
2475  #else /* COMPILE_PCRE8 */  #elif defined COMPILE_PCRE16 || defined COMPILE_PCRE32
2476    
 #ifdef COMPILE_PCRE16  
2477  #ifdef SUPPORT_UTF  #ifdef SUPPORT_UTF
2478  if (common->utf && c > 65535)  if (common->utf && c > 65535)
2479    {    {
# Line 2030  if (common->utf && c > 65535) Line 2484  if (common->utf && c > 65535)
2484    }    }
2485  #endif /* SUPPORT_UTF */  #endif /* SUPPORT_UTF */
2486  return (bit < 256) ? ((0 << 8) | bit) : ((1 << 8) | (bit >> 8));  return (bit < 256) ? ((0 << 8) | bit) : ((1 << 8) | (bit >> 8));
 #endif /* COMPILE_PCRE16 */  
2487    
2488  #endif /* COMPILE_PCRE8 */  #endif /* COMPILE_PCRE[8|16|32] */
2489  }  }
2490    
2491  static void check_partial(compiler_common *common, BOOL force)  static void check_partial(compiler_common *common, BOOL force)
2492  {  {
2493  /* Checks whether a partial matching is occured. Does not modify registers. */  /* Checks whether a partial matching is occurred. Does not modify registers. */
2494  DEFINE_COMPILER;  DEFINE_COMPILER;
2495  struct sljit_jump *jump = NULL;  struct sljit_jump *jump = NULL;
2496    
# Line 2047  if (common->mode == JIT_COMPILE) Line 2500  if (common->mode == JIT_COMPILE)
2500    return;    return;
2501    
2502  if (!force)  if (!force)
2503    jump = CMP(SLJIT_C_GREATER_EQUAL, SLJIT_MEM1(SLJIT_LOCALS_REG), common->start_used_ptr, STR_PTR, 0);    jump = CMP(SLJIT_GREATER_EQUAL, SLJIT_MEM1(SLJIT_SP), common->start_used_ptr, STR_PTR, 0);
2504  else if (common->mode == JIT_PARTIAL_SOFT_COMPILE)  else if (common->mode == JIT_PARTIAL_SOFT_COMPILE)
2505    jump = CMP(SLJIT_C_EQUAL, SLJIT_MEM1(SLJIT_LOCALS_REG), common->start_used_ptr, SLJIT_IMM, -1);    jump = CMP(SLJIT_EQUAL, SLJIT_MEM1(SLJIT_SP), common->start_used_ptr, SLJIT_IMM, -1);
2506    
2507  if (common->mode == JIT_PARTIAL_SOFT_COMPILE)  if (common->mode == JIT_PARTIAL_SOFT_COMPILE)
2508    OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), common->hit_start, SLJIT_IMM, -1);    OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->hit_start, SLJIT_IMM, 0);
2509  else  else
2510    {    {
2511    if (common->partialmatchlabel != NULL)    if (common->partialmatchlabel != NULL)
# Line 2065  if (jump != NULL) Line 2518  if (jump != NULL)
2518    JUMPHERE(jump);    JUMPHERE(jump);
2519  }  }
2520    
2521  static struct sljit_jump *check_str_end(compiler_common *common)  static void check_str_end(compiler_common *common, jump_list **end_reached)
2522  {  {
2523  /* Does not affect registers. Usually used in a tight spot. */  /* Does not affect registers. Usually used in a tight spot. */
2524  DEFINE_COMPILER;  DEFINE_COMPILER;
2525  struct sljit_jump *jump;  struct sljit_jump *jump;
 struct sljit_jump *nohit;  
 struct sljit_jump *return_value;  
2526    
2527  if (common->mode == JIT_COMPILE)  if (common->mode == JIT_COMPILE)
2528    return CMP(SLJIT_C_GREATER_EQUAL, STR_PTR, 0, STR_END, 0);    {
2529      add_jump(compiler, end_reached, CMP(SLJIT_GREATER_EQUAL, STR_PTR, 0, STR_END, 0));
2530      return;
2531      }
2532    
2533  jump = CMP(SLJIT_C_LESS, STR_PTR, 0, STR_END, 0);  jump = CMP(SLJIT_LESS, STR_PTR, 0, STR_END, 0);
2534  if (common->mode == JIT_PARTIAL_SOFT_COMPILE)  if (common->mode == JIT_PARTIAL_SOFT_COMPILE)
2535    {    {
2536    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_GREATER_EQUAL, SLJIT_MEM1(SLJIT_SP), common->start_used_ptr, STR_PTR, 0));
2537    OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), common->hit_start, SLJIT_IMM, -1);    OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->hit_start, SLJIT_IMM, 0);
2538    JUMPHERE(nohit);    add_jump(compiler, end_reached, JUMP(SLJIT_JUMP));
   return_value = JUMP(SLJIT_JUMP);  
2539    }    }
2540  else  else
2541    {    {
2542    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_GREATER_EQUAL, SLJIT_MEM1(SLJIT_SP), common->start_used_ptr, STR_PTR, 0));
2543    if (common->partialmatchlabel != NULL)    if (common->partialmatchlabel != NULL)
2544      JUMPTO(SLJIT_JUMP, common->partialmatchlabel);      JUMPTO(SLJIT_JUMP, common->partialmatchlabel);
2545    else    else
2546      add_jump(compiler, &common->partialmatch, JUMP(SLJIT_JUMP));      add_jump(compiler, &common->partialmatch, JUMP(SLJIT_JUMP));
2547    }    }
2548  JUMPHERE(jump);  JUMPHERE(jump);
 return return_value;  
2549  }  }
2550    
2551  static void detect_partial_match(compiler_common *common, jump_list **backtracks)  static void detect_partial_match(compiler_common *common, jump_list **backtracks)
# Line 2103  struct sljit_jump *jump; Line 2555  struct sljit_jump *jump;
2555    
2556  if (common->mode == JIT_COMPILE)  if (common->mode == JIT_COMPILE)
2557    {    {
2558    add_jump(compiler, backtracks, CMP(SLJIT_C_GREATER_EQUAL, STR_PTR, 0, STR_END, 0));    add_jump(compiler, backtracks, CMP(SLJIT_GREATER_EQUAL, STR_PTR, 0, STR_END, 0));
2559    return;    return;
2560    }    }
2561    
2562  /* Partial matching mode. */  /* Partial matching mode. */
2563  jump = CMP(SLJIT_C_LESS, STR_PTR, 0, STR_END, 0);  jump = CMP(SLJIT_LESS, STR_PTR, 0, STR_END, 0);
2564  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_GREATER_EQUAL, SLJIT_MEM1(SLJIT_SP), common->start_used_ptr, STR_PTR, 0));
2565  if (common->mode == JIT_PARTIAL_SOFT_COMPILE)  if (common->mode == JIT_PARTIAL_SOFT_COMPILE)
2566    {    {
2567    OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), common->hit_start, SLJIT_IMM, -1);    OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->hit_start, SLJIT_IMM, 0);
2568    add_jump(compiler, backtracks, JUMP(SLJIT_JUMP));    add_jump(compiler, backtracks, JUMP(SLJIT_JUMP));
2569    }    }
2570  else  else
# Line 2125  else Line 2577  else
2577  JUMPHERE(jump);  JUMPHERE(jump);
2578  }  }
2579    
2580  static void read_char(compiler_common *common)  static void peek_char(compiler_common *common, pcre_uint32 max)
2581  {  {
2582  /* Reads the character into TMP1, updates STR_PTR.  /* Reads the character into TMP1, keeps STR_PTR.
2583  Does not check STR_END. TMP2 Destroyed. */  Does not check STR_END. TMP2 Destroyed. */
2584  DEFINE_COMPILER;  DEFINE_COMPILER;
2585  #ifdef SUPPORT_UTF  #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
2586  struct sljit_jump *jump;  struct sljit_jump *jump;
2587  #endif  #endif
2588    
2589    SLJIT_UNUSED_ARG(max);
2590    
2591  OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), 0);  OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), 0);
2592  #ifdef SUPPORT_UTF  #if defined SUPPORT_UTF && defined COMPILE_PCRE8
2593  if (common->utf)  if (common->utf)
2594    {    {
2595  #ifdef COMPILE_PCRE8    if (max < 128) return;
2596    jump = CMP(SLJIT_C_LESS, TMP1, 0, SLJIT_IMM, 0xc0);  
2597  #else    jump = CMP(SLJIT_LESS, TMP1, 0, SLJIT_IMM, 0xc0);
2598  #ifdef 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  
 #endif /* COMPILE_PCRE8 */  
2599    add_jump(compiler, &common->utfreadchar, JUMP(SLJIT_FAST_CALL));    add_jump(compiler, &common->utfreadchar, JUMP(SLJIT_FAST_CALL));
2600      OP2(SLJIT_SUB, STR_PTR, 0, STR_PTR, 0, TMP2, 0);
2601      JUMPHERE(jump);
2602      }
2603    #endif /* SUPPORT_UTF && !COMPILE_PCRE32 */
2604    
2605    #if defined SUPPORT_UTF && defined COMPILE_PCRE16
2606    if (common->utf)
2607      {
2608      if (max < 0xd800) return;
2609    
2610      OP2(SLJIT_SUB, TMP2, 0, TMP1, 0, SLJIT_IMM, 0xd800);
2611      jump = CMP(SLJIT_GREATER, TMP2, 0, SLJIT_IMM, 0xdc00 - 0xd800 - 1);
2612      /* TMP2 contains the high surrogate. */
2613      OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(0));
2614      OP2(SLJIT_ADD, TMP2, 0, TMP2, 0, SLJIT_IMM, 0x40);
2615      OP2(SLJIT_SHL, TMP2, 0, TMP2, 0, SLJIT_IMM, 10);
2616      OP2(SLJIT_AND, TMP1, 0, TMP1, 0, SLJIT_IMM, 0x3ff);
2617      OP2(SLJIT_OR, TMP1, 0, TMP1, 0, TMP2, 0);
2618    JUMPHERE(jump);    JUMPHERE(jump);
2619    }    }
2620  #endif  #endif
2621    }
2622    
2623    #if defined SUPPORT_UTF && defined COMPILE_PCRE8
2624    
2625    static BOOL is_char7_bitset(const pcre_uint8 *bitset, BOOL nclass)
2626    {
2627    /* Tells whether the character codes below 128 are enough
2628    to determine a match. */
2629    const pcre_uint8 value = nclass ? 0xff : 0;
2630    const pcre_uint8 *end = bitset + 32;
2631    
2632    bitset += 16;
2633    do
2634      {
2635      if (*bitset++ != value)
2636        return FALSE;
2637      }
2638    while (bitset < end);
2639    return TRUE;
2640    }
2641    
2642    static void read_char7_type(compiler_common *common, BOOL full_read)
2643    {
2644    /* Reads the precise character type of a character into TMP1, if the character
2645    is less than 128. Otherwise it returns with zero. Does not check STR_END. The
2646    full_read argument tells whether characters above max are accepted or not. */
2647    DEFINE_COMPILER;
2648    struct sljit_jump *jump;
2649    
2650    SLJIT_ASSERT(common->utf);
2651    
2652    OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), 0);
2653  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));
2654    
2655    OP1(SLJIT_MOV_UB, TMP1, 0, SLJIT_MEM1(TMP2), common->ctypes);
2656    
2657    if (full_read)
2658      {
2659      jump = CMP(SLJIT_LESS, TMP2, 0, SLJIT_IMM, 0xc0);
2660      OP1(SLJIT_MOV_UB, TMP2, 0, SLJIT_MEM1(TMP2), (sljit_sw)PRIV(utf8_table4) - 0xc0);
2661      OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, TMP2, 0);
2662      JUMPHERE(jump);
2663      }
2664  }  }
2665    
2666  static void peek_char(compiler_common *common)  #endif /* SUPPORT_UTF && COMPILE_PCRE8 */
2667    
2668    static void read_char_range(compiler_common *common, pcre_uint32 min, pcre_uint32 max, BOOL update_str_ptr)
2669  {  {
2670  /* Reads the character into TMP1, keeps STR_PTR.  /* Reads the precise value of a character into TMP1, if the character is
2671  Does not check STR_END. TMP2 Destroyed. */  between min and max (c >= min && c <= max). Otherwise it returns with a value
2672    outside the range. Does not check STR_END. */
2673  DEFINE_COMPILER;  DEFINE_COMPILER;
2674  #ifdef SUPPORT_UTF  #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
2675  struct sljit_jump *jump;  struct sljit_jump *jump;
2676  #endif  #endif
2677    #if defined SUPPORT_UTF && defined COMPILE_PCRE8
2678    struct sljit_jump *jump2;
2679    #endif
2680    
2681  OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), 0);  SLJIT_UNUSED_ARG(update_str_ptr);
2682  #ifdef SUPPORT_UTF  SLJIT_UNUSED_ARG(min);
2683    SLJIT_UNUSED_ARG(max);
2684    SLJIT_ASSERT(min <= max);
2685    
2686    OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(0));
2687    OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
2688    
2689    #if defined SUPPORT_UTF && defined COMPILE_PCRE8
2690  if (common->utf)  if (common->utf)
2691    {    {
2692  #ifdef COMPILE_PCRE8    if (max < 128 && !update_str_ptr) return;
2693    jump = CMP(SLJIT_C_LESS, TMP1, 0, SLJIT_IMM, 0xc0);  
2694  #else    jump = CMP(SLJIT_LESS, TMP1, 0, SLJIT_IMM, 0xc0);
2695  #ifdef COMPILE_PCRE16    if (min >= 0x10000)
2696    jump = CMP(SLJIT_C_LESS, TMP1, 0, SLJIT_IMM, 0xd800);      {
2697        OP2(SLJIT_SUB, TMP2, 0, TMP1, 0, SLJIT_IMM, 0xf0);
2698        if (update_str_ptr)
2699          OP1(SLJIT_MOV_UB, RETURN_ADDR, 0, SLJIT_MEM1(TMP1), (sljit_sw)PRIV(utf8_table4) - 0xc0);
2700        OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(0));
2701        jump2 = CMP(SLJIT_GREATER, TMP2, 0, SLJIT_IMM, 0x7);
2702        OP2(SLJIT_SHL, TMP2, 0, TMP2, 0, SLJIT_IMM, 6);
2703        OP2(SLJIT_AND, TMP1, 0, TMP1, 0, SLJIT_IMM, 0x3f);
2704        OP2(SLJIT_OR, TMP1, 0, TMP1, 0, TMP2, 0);
2705        OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(1));
2706        OP2(SLJIT_SHL, TMP1, 0, TMP1, 0, SLJIT_IMM, 6);
2707        OP2(SLJIT_AND, TMP2, 0, TMP2, 0, SLJIT_IMM, 0x3f);
2708        OP2(SLJIT_OR, TMP1, 0, TMP1, 0, TMP2, 0);
2709        OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(2));
2710        if (!update_str_ptr)
2711          OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(3));
2712        OP2(SLJIT_SHL, TMP1, 0, TMP1, 0, SLJIT_IMM, 6);
2713        OP2(SLJIT_AND, TMP2, 0, TMP2, 0, SLJIT_IMM, 0x3f);
2714        OP2(SLJIT_OR, TMP1, 0, TMP1, 0, TMP2, 0);
2715        JUMPHERE(jump2);
2716        if (update_str_ptr)
2717          OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, RETURN_ADDR, 0);
2718        }
2719      else if (min >= 0x800 && max <= 0xffff)
2720        {
2721        OP2(SLJIT_SUB, TMP2, 0, TMP1, 0, SLJIT_IMM, 0xe0);
2722        if (update_str_ptr)
2723          OP1(SLJIT_MOV_UB, RETURN_ADDR, 0, SLJIT_MEM1(TMP1), (sljit_sw)PRIV(utf8_table4) - 0xc0);
2724        OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(0));
2725        jump2 = CMP(SLJIT_GREATER, TMP2, 0, SLJIT_IMM, 0xf);
2726        OP2(SLJIT_SHL, TMP2, 0, TMP2, 0, SLJIT_IMM, 6);
2727        OP2(SLJIT_AND, TMP1, 0, TMP1, 0, SLJIT_IMM, 0x3f);
2728        OP2(SLJIT_OR, TMP1, 0, TMP1, 0, TMP2, 0);
2729        OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(1));
2730        if (!update_str_ptr)
2731          OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(2));
2732        OP2(SLJIT_SHL, TMP1, 0, TMP1, 0, SLJIT_IMM, 6);
2733        OP2(SLJIT_AND, TMP2, 0, TMP2, 0, SLJIT_IMM, 0x3f);
2734        OP2(SLJIT_OR, TMP1, 0, TMP1, 0, TMP2, 0);
2735        JUMPHERE(jump2);
2736        if (update_str_ptr)
2737          OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, RETURN_ADDR, 0);
2738        }
2739      else if (max >= 0x800)
2740        add_jump(compiler, (max < 0x10000) ? &common->utfreadchar16 : &common->utfreadchar, JUMP(SLJIT_FAST_CALL));
2741      else if (max < 128)
2742        {
2743        OP1(SLJIT_MOV_UB, TMP2, 0, SLJIT_MEM1(TMP1), (sljit_sw)PRIV(utf8_table4) - 0xc0);
2744        OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, TMP2, 0);
2745        }
2746      else
2747        {
2748        OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(0));
2749        if (!update_str_ptr)
2750          OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
2751        else
2752          OP1(SLJIT_MOV_UB, RETURN_ADDR, 0, SLJIT_MEM1(TMP1), (sljit_sw)PRIV(utf8_table4) - 0xc0);
2753        OP2(SLJIT_AND, TMP1, 0, TMP1, 0, SLJIT_IMM, 0x3f);
2754        OP2(SLJIT_SHL, TMP1, 0, TMP1, 0, SLJIT_IMM, 6);
2755        OP2(SLJIT_AND, TMP2, 0, TMP2, 0, SLJIT_IMM, 0x3f);
2756        OP2(SLJIT_OR, TMP1, 0, TMP1, 0, TMP2, 0);
2757        if (update_str_ptr)
2758          OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, RETURN_ADDR, 0);
2759        }
2760      JUMPHERE(jump);
2761      }
2762  #endif  #endif
2763  #endif /* COMPILE_PCRE8 */  
2764    add_jump(compiler, &common->utfreadchar, JUMP(SLJIT_FAST_CALL));  #if defined SUPPORT_UTF && defined COMPILE_PCRE16
2765    OP2(SLJIT_SUB, STR_PTR, 0, STR_PTR, 0, TMP2, 0);  if (common->utf)
2766      {
2767      if (max >= 0x10000)
2768        {
2769        OP2(SLJIT_SUB, TMP2, 0, TMP1, 0, SLJIT_IMM, 0xd800);
2770        jump = CMP(SLJIT_GREATER, TMP2, 0, SLJIT_IMM, 0xdc00 - 0xd800 - 1);
2771        /* TMP2 contains the high surrogate. */
2772        OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(0));
2773        OP2(SLJIT_ADD, TMP2, 0, TMP2, 0, SLJIT_IMM, 0x40);
2774        OP2(SLJIT_SHL, TMP2, 0, TMP2, 0, SLJIT_IMM, 10);
2775        OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
2776        OP2(SLJIT_AND, TMP1, 0, TMP1, 0, SLJIT_IMM, 0x3ff);
2777        OP2(SLJIT_OR, TMP1, 0, TMP1, 0, TMP2, 0);
2778        JUMPHERE(jump);
2779        return;
2780        }
2781    
2782      if (max < 0xd800 && !update_str_ptr) return;
2783    
2784      /* Skip low surrogate if necessary. */
2785      OP2(SLJIT_SUB, TMP2, 0, TMP1, 0, SLJIT_IMM, 0xd800);
2786      jump = CMP(SLJIT_GREATER, TMP2, 0, SLJIT_IMM, 0xdc00 - 0xd800 - 1);
2787      if (update_str_ptr)
2788        OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
2789      if (max >= 0xd800)
2790        OP1(SLJIT_MOV, TMP1, 0, SLJIT_IMM, 0x10000);
2791    JUMPHERE(jump);    JUMPHERE(jump);
2792    }    }
2793  #endif  #endif
2794  }  }
2795    
2796  static void read_char8_type(compiler_common *common)  static SLJIT_INLINE void read_char(compiler_common *common)
2797    {
2798    read_char_range(common, 0, READ_CHAR_MAX, TRUE);
2799    }
2800    
2801    static void read_char8_type(compiler_common *common, BOOL update_str_ptr)
2802  {  {
2803  /* 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. */
2804  DEFINE_COMPILER;  DEFINE_COMPILER;
2805  #if defined SUPPORT_UTF || defined COMPILE_PCRE16  #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
2806  struct sljit_jump *jump;  struct sljit_jump *jump;
2807  #endif  #endif
2808    #if defined SUPPORT_UTF && defined COMPILE_PCRE8
2809    struct sljit_jump *jump2;
2810    #endif
2811    
2812  #ifdef SUPPORT_UTF  SLJIT_UNUSED_ARG(update_str_ptr);
2813    
2814    OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), 0);
2815    OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
2816    
2817    #if defined SUPPORT_UTF && defined COMPILE_PCRE8
2818  if (common->utf)  if (common->utf)
2819    {    {
   OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), 0);  
   OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));  
 #ifdef COMPILE_PCRE8  
2820    /* This can be an extra read in some situations, but hopefully    /* This can be an extra read in some situations, but hopefully
2821    it is needed in most cases. */    it is needed in most cases. */
2822    OP1(SLJIT_MOV_UB, TMP1, 0, SLJIT_MEM1(TMP2), common->ctypes);    OP1(SLJIT_MOV_UB, TMP1, 0, SLJIT_MEM1(TMP2), common->ctypes);
2823    jump = CMP(SLJIT_C_LESS, TMP2, 0, SLJIT_IMM, 0xc0);    jump = CMP(SLJIT_LESS, TMP2, 0, SLJIT_IMM, 0xc0);
2824    add_jump(compiler, &common->utfreadtype8, JUMP(SLJIT_FAST_CALL));    if (!update_str_ptr)
2825    JUMPHERE(jump);      {
2826  #else      OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(0));
2827  #ifdef COMPILE_PCRE16      OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
2828    OP1(SLJIT_MOV, TMP1, 0, SLJIT_IMM, 0);      OP2(SLJIT_AND, TMP2, 0, TMP2, 0, SLJIT_IMM, 0x3f);
2829    jump = CMP(SLJIT_C_GREATER, TMP2, 0, SLJIT_IMM, 255);      OP2(SLJIT_SHL, TMP2, 0, TMP2, 0, SLJIT_IMM, 6);
2830    OP1(SLJIT_MOV_UB, TMP1, 0, SLJIT_MEM1(TMP2), common->ctypes);      OP2(SLJIT_AND, TMP1, 0, TMP1, 0, SLJIT_IMM, 0x3f);
2831        OP2(SLJIT_OR, TMP2, 0, TMP2, 0, TMP1, 0);
2832        OP1(SLJIT_MOV, TMP1, 0, SLJIT_IMM, 0);
2833        jump2 = CMP(SLJIT_GREATER, TMP2, 0, SLJIT_IMM, 255);
2834        OP1(SLJIT_MOV_UB, TMP1, 0, SLJIT_MEM1(TMP2), common->ctypes);
2835        JUMPHERE(jump2);
2836        }
2837      else
2838        add_jump(compiler, &common->utfreadtype8, JUMP(SLJIT_FAST_CALL));
2839    JUMPHERE(jump);    JUMPHERE(jump);
   /* Skip low surrogate if necessary. */  
   OP2(SLJIT_AND, TMP2, 0, TMP2, 0, SLJIT_IMM, 0xfc00);  
   OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP2, 0, SLJIT_IMM, 0xd800);  
   COND_VALUE(SLJIT_MOV, TMP2, 0, SLJIT_C_EQUAL);  
   OP2(SLJIT_SHL, TMP2, 0, TMP2, 0, SLJIT_IMM, 1);  
   OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, TMP2, 0);  
 #endif  
 #endif /* COMPILE_PCRE8 */  
2840    return;    return;
2841    }    }
2842  #endif  #endif /* SUPPORT_UTF && COMPILE_PCRE8 */
2843  OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), 0);  
2844  OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));  #if !defined COMPILE_PCRE8
 #ifdef COMPILE_PCRE16  
2845  /* The ctypes array contains only 256 values. */  /* The ctypes array contains only 256 values. */
2846  OP1(SLJIT_MOV, TMP1, 0, SLJIT_IMM, 0);  OP1(SLJIT_MOV, TMP1, 0, SLJIT_IMM, 0);
2847  jump = CMP(SLJIT_C_GREATER, TMP2, 0, SLJIT_IMM, 255);  jump = CMP(SLJIT_GREATER, TMP2, 0, SLJIT_IMM, 255);
2848  #endif  #endif
2849  OP1(SLJIT_MOV_UB, TMP1, 0, SLJIT_MEM1(TMP2), common->ctypes);  OP1(SLJIT_MOV_UB, TMP1, 0, SLJIT_MEM1(TMP2), common->ctypes);
2850  #ifdef COMPILE_PCRE16  #if !defined COMPILE_PCRE8
2851  JUMPHERE(jump);  JUMPHERE(jump);
2852  #endif  #endif
2853    
2854    #if defined SUPPORT_UTF && defined COMPILE_PCRE16
2855    if (common->utf && update_str_ptr)
2856      {
2857      /* Skip low surrogate if necessary. */
2858      OP2(SLJIT_SUB, TMP2, 0, TMP2, 0, SLJIT_IMM, 0xd800);
2859      jump = CMP(SLJIT_GREATER, TMP2, 0, SLJIT_IMM, 0xdc00 - 0xd800 - 1);
2860      OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
2861      JUMPHERE(jump);
2862      }
2863    #endif /* SUPPORT_UTF && COMPILE_PCRE16 */
2864  }  }
2865    
2866  static void skip_char_back(compiler_common *common)  static void skip_char_back(compiler_common *common)
2867  {  {
2868  /* Goes one character back. Affects STR_PTR and TMP1. Does not check begin. */  /* Goes one character back. Affects STR_PTR and TMP1. Does not check begin. */
2869  DEFINE_COMPILER;  DEFINE_COMPILER;
2870  #if defined SUPPORT_UTF && defined COMPILE_PCRE8  #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
2871    #if defined COMPILE_PCRE8
2872  struct sljit_label *label;  struct sljit_label *label;
2873    
2874  if (common->utf)  if (common->utf)
# Line 2242  if (common->utf) Line 2877  if (common->utf)
2877    OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), -IN_UCHARS(1));    OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), -IN_UCHARS(1));
2878    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));
2879    OP2(SLJIT_AND, TMP1, 0, TMP1, 0, SLJIT_IMM, 0xc0);    OP2(SLJIT_AND, TMP1, 0, TMP1, 0, SLJIT_IMM, 0xc0);
2880    CMPTO(SLJIT_C_EQUAL, TMP1, 0, SLJIT_IMM, 0x80, label);    CMPTO(SLJIT_EQUAL, TMP1, 0, SLJIT_IMM, 0x80, label);
2881    return;    return;
2882    }    }
2883  #endif  #elif defined COMPILE_PCRE16
 #if defined SUPPORT_UTF && defined COMPILE_PCRE16  
2884  if (common->utf)  if (common->utf)
2885    {    {
2886    OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), -IN_UCHARS(1));    OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), -IN_UCHARS(1));
# Line 2254  if (common->utf) Line 2888  if (common->utf)
2888    /* Skip low surrogate if necessary. */    /* Skip low surrogate if necessary. */
2889    OP2(SLJIT_AND, TMP1, 0, TMP1, 0, SLJIT_IMM, 0xfc00);    OP2(SLJIT_AND, TMP1, 0, TMP1, 0, SLJIT_IMM, 0xfc00);
2890    OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0xdc00);    OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0xdc00);
2891    COND_VALUE(SLJIT_MOV, TMP1, 0, SLJIT_C_EQUAL);    OP_FLAGS(SLJIT_MOV, TMP1, 0, SLJIT_UNUSED, 0, SLJIT_EQUAL);
2892    OP2(SLJIT_SHL, TMP1, 0, TMP1, 0, SLJIT_IMM, 1);    OP2(SLJIT_SHL, TMP1, 0, TMP1, 0, SLJIT_IMM, 1);
2893    OP2(SLJIT_SUB, STR_PTR, 0, STR_PTR, 0, TMP1, 0);    OP2(SLJIT_SUB, STR_PTR, 0, STR_PTR, 0, TMP1, 0);
2894    return;    return;
2895    }    }
2896  #endif  #endif /* COMPILE_PCRE[8|16] */
2897    #endif /* SUPPORT_UTF && !COMPILE_PCRE32 */
2898  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));
2899  }  }
2900    
2901  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)
2902  {  {
2903  /* 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. */
2904  DEFINE_COMPILER;  DEFINE_COMPILER;
2905    struct sljit_jump *jump;
2906    
2907  if (nltype == NLTYPE_ANY)  if (nltype == NLTYPE_ANY)
2908    {    {
2909    add_jump(compiler, &common->anynewline, JUMP(SLJIT_FAST_CALL));    add_jump(compiler, &common->anynewline, JUMP(SLJIT_FAST_CALL));
2910    add_jump(compiler, backtracks, JUMP(jumpiftrue ? SLJIT_C_NOT_ZERO : SLJIT_C_ZERO));    add_jump(compiler, backtracks, JUMP(jumpifmatch ? SLJIT_NOT_ZERO : SLJIT_ZERO));
2911    }    }
2912  else if (nltype == NLTYPE_ANYCRLF)  else if (nltype == NLTYPE_ANYCRLF)
2913    {    {
2914    OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, CHAR_CR);    if (jumpifmatch)
2915    COND_VALUE(SLJIT_MOV, TMP2, 0, SLJIT_C_EQUAL);      {
2916    OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, CHAR_NL);      add_jump(compiler, backtracks, CMP(SLJIT_EQUAL, TMP1, 0, SLJIT_IMM, CHAR_CR));
2917    COND_VALUE(SLJIT_OR | SLJIT_SET_E, TMP2, 0, SLJIT_C_EQUAL);      add_jump(compiler, backtracks, CMP(SLJIT_EQUAL, TMP1, 0, SLJIT_IMM, CHAR_NL));
2918    add_jump(compiler, backtracks, JUMP(jumpiftrue ? SLJIT_C_NOT_ZERO : SLJIT_C_ZERO));      }
2919      else
2920        {
2921        jump = CMP(SLJIT_EQUAL, TMP1, 0, SLJIT_IMM, CHAR_CR);
2922        add_jump(compiler, backtracks, CMP(SLJIT_NOT_EQUAL, TMP1, 0, SLJIT_IMM, CHAR_NL));
2923        JUMPHERE(jump);
2924        }
2925    }    }
2926  else  else
2927    {    {
2928    SLJIT_ASSERT(nltype == NLTYPE_FIXED && common->newline < 256);    SLJIT_ASSERT(nltype == NLTYPE_FIXED && common->newline < 256);
2929    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_EQUAL : SLJIT_NOT_EQUAL, TMP1, 0, SLJIT_IMM, common->newline));
2930    }    }
2931  }  }
2932    
2933  #ifdef SUPPORT_UTF  #ifdef SUPPORT_UTF
2934    
2935  #ifdef COMPILE_PCRE8  #if defined COMPILE_PCRE8
2936  static void do_utfreadchar(compiler_common *common)  static void do_utfreadchar(compiler_common *common)
2937  {  {
2938  /* Fast decoding a UTF-8 character. TMP1 contains the first byte  /* Fast decoding a UTF-8 character. TMP1 contains the first byte
2939  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. */
2940  DEFINE_COMPILER;  DEFINE_COMPILER;
2941  struct sljit_jump *jump;  struct sljit_jump *jump;
2942    
2943  sljit_emit_fast_enter(compiler, RETURN_ADDR, 0);  sljit_emit_fast_enter(compiler, RETURN_ADDR, 0);
2944  /* Searching for the first zero. */  OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(0));
2945  OP2(SLJIT_AND | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x20);  OP2(SLJIT_AND, TMP1, 0, TMP1, 0, SLJIT_IMM, 0x3f);
 jump = JUMP(SLJIT_C_NOT_ZERO);  
 /* Two byte sequence. */  
 OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(1));  
 OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));  
 OP2(SLJIT_AND, TMP1, 0, TMP1, 0, SLJIT_IMM, 0x1f);  
2946  OP2(SLJIT_SHL, TMP1, 0, TMP1, 0, SLJIT_IMM, 6);  OP2(SLJIT_SHL, TMP1, 0, TMP1, 0, SLJIT_IMM, 6);
2947  OP2(SLJIT_AND, TMP2, 0, TMP2, 0, SLJIT_IMM, 0x3f);  OP2(SLJIT_AND, TMP2, 0, TMP2, 0, SLJIT_IMM, 0x3f);
2948  OP2(SLJIT_OR, TMP1, 0, TMP1, 0, TMP2, 0);  OP2(SLJIT_OR, TMP1, 0, TMP1, 0, TMP2, 0);
2949  OP1(SLJIT_MOV, TMP2, 0, SLJIT_IMM, IN_UCHARS(1));  
2950    /* Searching for the first zero. */
2951    OP2(SLJIT_AND | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x800);
2952    jump = JUMP(SLJIT_NOT_ZERO);
2953    /* Two byte sequence. */
2954    OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
2955    OP1(SLJIT_MOV, TMP2, 0, SLJIT_IMM, IN_UCHARS(2));
2956  sljit_emit_fast_return(compiler, RETURN_ADDR, 0);  sljit_emit_fast_return(compiler, RETURN_ADDR, 0);
 JUMPHERE(jump);  
2957    
2958  OP2(SLJIT_AND | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x10);  JUMPHERE(jump);
 jump = JUMP(SLJIT_C_NOT_ZERO);  
 /* Three byte sequence. */  
2959  OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(1));  OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(1));
2960  OP2(SLJIT_AND, TMP1, 0, TMP1, 0, SLJIT_IMM, 0x0f);  OP2(SLJIT_XOR, TMP1, 0, TMP1, 0, SLJIT_IMM, 0x800);
2961  OP2(SLJIT_SHL, TMP1, 0, TMP1, 0, SLJIT_IMM, 12);  OP2(SLJIT_SHL, TMP1, 0, TMP1, 0, SLJIT_IMM, 6);
2962  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);  
2963  OP2(SLJIT_OR, TMP1, 0, TMP1, 0, TMP2, 0);  OP2(SLJIT_OR, TMP1, 0, TMP1, 0, TMP2, 0);
2964  OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(2));  
2965    OP2(SLJIT_AND | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x10000);
2966    jump = JUMP(SLJIT_NOT_ZERO);
2967    /* Three byte sequence. */
2968  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));
2969  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));  
2970  sljit_emit_fast_return(compiler, RETURN_ADDR, 0);  sljit_emit_fast_return(compiler, RETURN_ADDR, 0);
 JUMPHERE(jump);  
2971    
2972  /* Four byte sequence. */  /* Four byte sequence. */
2973  OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(1));  JUMPHERE(jump);
 OP2(SLJIT_AND, TMP1, 0, TMP1, 0, SLJIT_IMM, 0x07);  
 OP2(SLJIT_SHL, TMP1, 0, TMP1, 0, SLJIT_IMM, 18);  
 OP2(SLJIT_AND, TMP2, 0, TMP2, 0, SLJIT_IMM, 0x3f);  
 OP2(SLJIT_SHL, TMP2, 0, TMP2, 0, SLJIT_IMM, 12);  
 OP2(SLJIT_OR, TMP1, 0, TMP1, 0, TMP2, 0);  
2974  OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(2));  OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(2));
2975  OP2(SLJIT_AND, TMP2, 0, TMP2, 0, SLJIT_IMM, 0x3f);  OP2(SLJIT_XOR, TMP1, 0, TMP1, 0, SLJIT_IMM, 0x10000);
2976  OP2(SLJIT_SHL, TMP2, 0, TMP2, 0, SLJIT_IMM, 6);  OP2(SLJIT_SHL, TMP1, 0, TMP1, 0, SLJIT_IMM, 6);
 OP2(SLJIT_OR, TMP1, 0, TMP1, 0, TMP2, 0);  
 OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(3));  
2977  OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(3));  OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(3));
2978  OP2(SLJIT_AND, TMP2, 0, TMP2, 0, SLJIT_IMM, 0x3f);  OP2(SLJIT_AND, TMP2, 0, TMP2, 0, SLJIT_IMM, 0x3f);
2979  OP2(SLJIT_OR, TMP1, 0, TMP1, 0, TMP2, 0);  OP2(SLJIT_OR, TMP1, 0, TMP1, 0, TMP2, 0);
2980  OP1(SLJIT_MOV, TMP2, 0, SLJIT_IMM, IN_UCHARS(3));  OP1(SLJIT_MOV, TMP2, 0, SLJIT_IMM, IN_UCHARS(4));
2981  sljit_emit_fast_return(compiler, RETURN_ADDR, 0);  sljit_emit_fast_return(compiler, RETURN_ADDR, 0);
2982  }  }
2983    
2984  static void do_utfreadtype8(compiler_common *common)  static void do_utfreadchar16(compiler_common *common)
2985  {  {
2986  /* Fast decoding a UTF-8 character type. TMP2 contains the first byte  /* Fast decoding a UTF-8 character. TMP1 contains the first byte
2987  of the character (>= 0xc0). Return value in TMP1. */  of the character (>= 0xc0). Return value in TMP1. */
2988  DEFINE_COMPILER;  DEFINE_COMPILER;
2989  struct sljit_jump *jump;  struct sljit_jump *jump;
 struct sljit_jump *compare;  
2990    
2991  sljit_emit_fast_enter(compiler, RETURN_ADDR, 0);  sljit_emit_fast_enter(compiler, RETURN_ADDR, 0);
2992    OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(0));
2993    OP2(SLJIT_AND, TMP1, 0, TMP1, 0, SLJIT_IMM, 0x3f);
2994    OP2(SLJIT_SHL, TMP1, 0, TMP1, 0, SLJIT_IMM, 6);
2995    OP2(SLJIT_AND, TMP2, 0, TMP2, 0, SLJIT_IMM, 0x3f);
2996    OP2(SLJIT_OR, TMP1, 0, TMP1, 0, TMP2, 0);
2997    
2998  OP2(SLJIT_AND | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP2, 0, SLJIT_IMM, 0x20);  /* Searching for the first zero. */
2999  jump = JUMP(SLJIT_C_NOT_ZERO);  OP2(SLJIT_AND | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x800);
3000    jump = JUMP(SLJIT_NOT_ZERO);
3001  /* Two byte sequence. */  /* Two byte sequence. */
 OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(0));  
3002  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));
 OP2(SLJIT_AND, TMP2, 0, TMP2, 0, SLJIT_IMM, 0x1f);  
 OP2(SLJIT_SHL, TMP2, 0, TMP2, 0, SLJIT_IMM, 6);  
 OP2(SLJIT_AND, TMP1, 0, TMP1, 0, SLJIT_IMM, 0x3f);  
 OP2(SLJIT_OR, TMP2, 0, TMP2, 0, TMP1, 0);  
 compare = CMP(SLJIT_C_GREATER, TMP2, 0, SLJIT_IMM, 255);  
 OP1(SLJIT_MOV_UB, TMP1, 0, SLJIT_MEM1(TMP2), common->ctypes);  
3003  sljit_emit_fast_return(compiler, RETURN_ADDR, 0);  sljit_emit_fast_return(compiler, RETURN_ADDR, 0);
3004    
 JUMPHERE(compare);  
 OP1(SLJIT_MOV, TMP1, 0, SLJIT_IMM, 0);  
 sljit_emit_fast_return(compiler, RETURN_ADDR, 0);  
3005  JUMPHERE(jump);  JUMPHERE(jump);
3006    OP2(SLJIT_AND | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x400);
3007  /* We only have types for characters less than 256. */  OP_FLAGS(SLJIT_MOV, TMP2, 0, SLJIT_UNUSED, 0, SLJIT_NOT_ZERO);
3008  OP1(SLJIT_MOV_UB, TMP1, 0, SLJIT_MEM1(TMP2), (sljit_w)PRIV(utf8_table4) - 0xc0);  /* This code runs only in 8 bit mode. No need to shift the value. */
3009  OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, TMP1, 0);  OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, TMP2, 0);
3010  OP1(SLJIT_MOV, TMP1, 0, SLJIT_IMM, 0);  OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(1));
3011    OP2(SLJIT_XOR, TMP1, 0, TMP1, 0, SLJIT_IMM, 0x800);
3012    OP2(SLJIT_SHL, TMP1, 0, TMP1, 0, SLJIT_IMM, 6);
3013    OP2(SLJIT_AND, TMP2, 0, TMP2, 0, SLJIT_IMM, 0x3f);
3014    OP2(SLJIT_OR, TMP1, 0, TMP1, 0, TMP2, 0);
3015    /* Three byte sequence. */
3016    OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(2));
3017  sljit_emit_fast_return(compiler, RETURN_ADDR, 0);  sljit_emit_fast_return(compiler, RETURN_ADDR, 0);
3018  }  }
3019    
3020  #else /* COMPILE_PCRE8 */  static void do_utfreadtype8(compiler_common *common)
   
 #ifdef COMPILE_PCRE16  
 static void do_utfreadchar(compiler_common *common)  
3021  {  {
3022  /* Fast decoding a UTF-16 character. TMP1 contains the first 16 bit char  /* Fast decoding a UTF-8 character type. TMP2 contains the first byte
3023  of the character (>= 0xd800). Return char value in TMP1, length - 1 in TMP2. */  of the character (>= 0xc0). Return value in TMP1. */
3024  DEFINE_COMPILER;  DEFINE_COMPILER;
3025  struct sljit_jump *jump;  struct sljit_jump *jump;
3026    struct sljit_jump *compare;
3027    
3028  sljit_emit_fast_enter(compiler, RETURN_ADDR, 0);  sljit_emit_fast_enter(compiler, RETURN_ADDR, 0);
3029  jump = CMP(SLJIT_C_LESS, TMP1, 0, SLJIT_IMM, 0xdc00);  
3030  /* Do nothing, only return. */  OP2(SLJIT_AND | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP2, 0, SLJIT_IMM, 0x20);
3031    jump = JUMP(SLJIT_NOT_ZERO);
3032    /* Two byte sequence. */
3033    OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(0));
3034    OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
3035    OP2(SLJIT_AND, TMP2, 0, TMP2, 0, SLJIT_IMM, 0x1f);
3036    /* The upper 5 bits are known at this point. */
3037    compare = CMP(SLJIT_GREATER, TMP2, 0, SLJIT_IMM, 0x3);
3038    OP2(SLJIT_SHL, TMP2, 0, TMP2, 0, SLJIT_IMM, 6);
3039    OP2(SLJIT_AND, TMP1, 0, TMP1, 0, SLJIT_IMM, 0x3f);
3040    OP2(SLJIT_OR, TMP2, 0, TMP2, 0, TMP1, 0);
3041    OP1(SLJIT_MOV_UB, TMP1, 0, SLJIT_MEM1(TMP2), common->ctypes);
3042    sljit_emit_fast_return(compiler, RETURN_ADDR, 0);
3043    
3044    JUMPHERE(compare);
3045    OP1(SLJIT_MOV, TMP1, 0, SLJIT_IMM, 0);
3046  sljit_emit_fast_return(compiler, RETURN_ADDR, 0);  sljit_emit_fast_return(compiler, RETURN_ADDR, 0);
3047    
3048    /* We only have types for characters less than 256. */
3049  JUMPHERE(jump);  JUMPHERE(jump);
3050  /* Combine two 16 bit characters. */  OP1(SLJIT_MOV_UB, TMP2, 0, SLJIT_MEM1(TMP2), (sljit_sw)PRIV(utf8_table4) - 0xc0);
3051  OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(1));  OP1(SLJIT_MOV, TMP1, 0, SLJIT_IMM, 0);
3052  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);  
3053  sljit_emit_fast_return(compiler, RETURN_ADDR, 0);  sljit_emit_fast_return(compiler, RETURN_ADDR, 0);
3054  }  }
 #endif /* COMPILE_PCRE16 */  
3055    
3056  #endif /* COMPILE_PCRE8 */  #endif /* COMPILE_PCRE8 */
3057    
# Line 2433  SLJIT_ASSERT(UCD_BLOCK_SIZE == 128 && si Line 3073  SLJIT_ASSERT(UCD_BLOCK_SIZE == 128 && si
3073    
3074  sljit_emit_fast_enter(compiler, RETURN_ADDR, 0);  sljit_emit_fast_enter(compiler, RETURN_ADDR, 0);
3075  OP2(SLJIT_LSHR, TMP2, 0, TMP1, 0, SLJIT_IMM, UCD_BLOCK_SHIFT);  OP2(SLJIT_LSHR, TMP2, 0, TMP1, 0, SLJIT_IMM, UCD_BLOCK_SHIFT);
3076  OP1(SLJIT_MOV_UB, TMP2, 0, SLJIT_MEM1(TMP2), (sljit_w)PRIV(ucd_stage1));  OP1(SLJIT_MOV_UB, TMP2, 0, SLJIT_MEM1(TMP2), (sljit_sw)PRIV(ucd_stage1));
3077  OP2(SLJIT_AND, TMP1, 0, TMP1, 0, SLJIT_IMM, UCD_BLOCK_MASK);  OP2(SLJIT_AND, TMP1, 0, TMP1, 0, SLJIT_IMM, UCD_BLOCK_MASK);
3078  OP2(SLJIT_SHL, TMP2, 0, TMP2, 0, SLJIT_IMM, UCD_BLOCK_SHIFT);  OP2(SLJIT_SHL, TMP2, 0, TMP2, 0, SLJIT_IMM, UCD_BLOCK_SHIFT);
3079  OP2(SLJIT_ADD, TMP1, 0, TMP1, 0, TMP2, 0);  OP2(SLJIT_ADD, TMP1, 0, TMP1, 0, TMP2, 0);
3080  OP1(SLJIT_MOV, TMP2, 0, SLJIT_IMM, (sljit_w)PRIV(ucd_stage2));  OP1(SLJIT_MOV, TMP2, 0, SLJIT_IMM, (sljit_sw)PRIV(ucd_stage2));
3081  OP1(SLJIT_MOV_UH, TMP2, 0, SLJIT_MEM2(TMP2, TMP1), 1);  OP1(SLJIT_MOV_UH, TMP2, 0, SLJIT_MEM2(TMP2, TMP1), 1);
3082  OP1(SLJIT_MOV, TMP1, 0, SLJIT_IMM, (sljit_w)PRIV(ucd_records) + SLJIT_OFFSETOF(ucd_record, chartype));  OP1(SLJIT_MOV, TMP1, 0, SLJIT_IMM, (sljit_sw)PRIV(ucd_records) + SLJIT_OFFSETOF(ucd_record, chartype));
3083  OP1(SLJIT_MOV_UB, TMP1, 0, SLJIT_MEM2(TMP1, TMP2), 3);  OP1(SLJIT_MOV_UB, TMP1, 0, SLJIT_MEM2(TMP1, TMP2), 3);
3084  sljit_emit_fast_return(compiler, RETURN_ADDR, 0);  sljit_emit_fast_return(compiler, RETURN_ADDR, 0);
3085  }  }
# Line 2453  struct sljit_label *newlinelabel = NULL; Line 3093  struct sljit_label *newlinelabel = NULL;
3093  struct sljit_jump *start;  struct sljit_jump *start;
3094  struct sljit_jump *end = NULL;  struct sljit_jump *end = NULL;
3095  struct sljit_jump *nl = NULL;  struct sljit_jump *nl = NULL;
3096  #ifdef SUPPORT_UTF  #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
3097  struct sljit_jump *singlechar;  struct sljit_jump *singlechar;
3098  #endif  #endif
3099  jump_list *newline = NULL;  jump_list *newline = NULL;
# Line 2474  if (firstline) Line 3114  if (firstline)
3114      {      {
3115      mainloop = LABEL();      mainloop = LABEL();
3116      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));
3117      end = CMP(SLJIT_C_GREATER_EQUAL, STR_PTR, 0, STR_END, 0);      end = CMP(SLJIT_GREATER_EQUAL, STR_PTR, 0, STR_END, 0);
3118      OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(-1));      OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(-1));
3119      OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(0));      OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(0));
3120      CMPTO(SLJIT_C_NOT_EQUAL, TMP1, 0, SLJIT_IMM, (common->newline >> 8) & 0xff, mainloop);      CMPTO(SLJIT_NOT_EQUAL, TMP1, 0, SLJIT_IMM, (common->newline >> 8) & 0xff, mainloop);
3121      CMPTO(SLJIT_C_NOT_EQUAL, TMP2, 0, SLJIT_IMM, common->newline & 0xff, mainloop);      CMPTO(SLJIT_NOT_EQUAL, TMP2, 0, SLJIT_IMM, common->newline & 0xff, mainloop);
3122      JUMPHERE(end);      JUMPHERE(end);
3123      OP2(SLJIT_SUB, SLJIT_MEM1(SLJIT_LOCALS_REG), common->first_line_end, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));      OP2(SLJIT_SUB, SLJIT_MEM1(SLJIT_SP), common->first_line_end, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
3124      }      }
3125    else    else
3126      {      {
3127      end = CMP(SLJIT_C_GREATER_EQUAL, STR_PTR, 0, STR_END, 0);      end = CMP(SLJIT_GREATER_EQUAL, STR_PTR, 0, STR_END, 0);
3128      mainloop = LABEL();      mainloop = LABEL();
3129      /* Continual stores does not cause data dependency. */      /* Continual stores does not cause data dependency. */
3130      OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), common->first_line_end, STR_PTR, 0);      OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->first_line_end, STR_PTR, 0);
3131      read_char(common);      read_char_range(common, common->nlmin, common->nlmax, TRUE);
3132      check_newlinechar(common, common->nltype, &newline, TRUE);      check_newlinechar(common, common->nltype, &newline, TRUE);
3133      CMPTO(SLJIT_C_LESS, STR_PTR, 0, STR_END, 0, mainloop);      CMPTO(SLJIT_LESS, STR_PTR, 0, STR_END, 0, mainloop);
3134      JUMPHERE(end);      JUMPHERE(end);
3135      OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), common->first_line_end, STR_PTR, 0);      OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->first_line_end, STR_PTR, 0);
3136      set_jumps(newline, LABEL());      set_jumps(newline, LABEL());
3137      }      }
3138    
# Line 2505  if (newlinecheck) Line 3145  if (newlinecheck)
3145    {    {
3146    newlinelabel = LABEL();    newlinelabel = LABEL();
3147    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));
3148    end = CMP(SLJIT_C_GREATER_EQUAL, STR_PTR, 0, STR_END, 0);    end = CMP(SLJIT_GREATER_EQUAL, STR_PTR, 0, STR_END, 0);
3149    OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), 0);    OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), 0);
3150    OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, common->newline & 0xff);    OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, common->newline & 0xff);
3151    COND_VALUE(SLJIT_MOV, TMP1, 0, SLJIT_C_EQUAL);    OP_FLAGS(SLJIT_MOV, TMP1, 0, SLJIT_UNUSED, 0, SLJIT_EQUAL);
3152  #ifdef COMPILE_PCRE16  #if defined COMPILE_PCRE16 || defined COMPILE_PCRE32
3153    OP2(SLJIT_SHL, TMP1, 0, TMP1, 0, SLJIT_IMM, 1);    OP2(SLJIT_SHL, TMP1, 0, TMP1, 0, SLJIT_IMM, UCHAR_SHIFT);
3154  #endif  #endif
3155    OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, TMP1, 0);    OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, TMP1, 0);
3156    nl = JUMP(SLJIT_JUMP);    nl = JUMP(SLJIT_JUMP);
# Line 2528  if (readuchar) Line 3168  if (readuchar)
3168    OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), 0);    OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), 0);
3169    
3170  if (newlinecheck)  if (newlinecheck)
3171    CMPTO(SLJIT_C_EQUAL, TMP1, 0, SLJIT_IMM, (common->newline >> 8) & 0xff, newlinelabel);    CMPTO(SLJIT_EQUAL, TMP1, 0, SLJIT_IMM, (common->newline >> 8) & 0xff, newlinelabel);
3172    
3173  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));
3174  #if defined SUPPORT_UTF && defined COMPILE_PCRE8  #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
3175    #if defined COMPILE_PCRE8
3176  if (common->utf)  if (common->utf)
3177    {    {
3178    singlechar = CMP(SLJIT_C_LESS, TMP1, 0, SLJIT_IMM, 0xc0);    singlechar = CMP(SLJIT_LESS, TMP1, 0, SLJIT_IMM, 0xc0);
3179    OP1(SLJIT_MOV_UB, TMP1, 0, SLJIT_MEM1(TMP1), (sljit_w)PRIV(utf8_table4) - 0xc0);    OP1(SLJIT_MOV_UB, TMP1, 0, SLJIT_MEM1(TMP1), (sljit_sw)PRIV(utf8_table4) - 0xc0);
3180    OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, TMP1, 0);    OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, TMP1, 0);
3181    JUMPHERE(singlechar);    JUMPHERE(singlechar);
3182    }    }
3183  #endif  #elif defined COMPILE_PCRE16
 #if defined SUPPORT_UTF && defined COMPILE_PCRE16  
3184  if (common->utf)  if (common->utf)
3185    {    {
3186    singlechar = CMP(SLJIT_C_LESS, TMP1, 0, SLJIT_IMM, 0xd800);    singlechar = CMP(SLJIT_LESS, TMP1, 0, SLJIT_IMM, 0xd800);
3187    OP2(SLJIT_AND, TMP1, 0, TMP1, 0, SLJIT_IMM, 0xfc00);    OP2(SLJIT_AND, TMP1, 0, TMP1, 0, SLJIT_IMM, 0xfc00);
3188    OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0xd800);    OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0xd800);
3189    COND_VALUE(SLJIT_MOV, TMP1, 0, SLJIT_C_EQUAL);    OP_FLAGS(SLJIT_MOV, TMP1, 0, SLJIT_UNUSED, 0, SLJIT_EQUAL);
3190    OP2(SLJIT_SHL, TMP1, 0, TMP1, 0, SLJIT_IMM, 1);    OP2(SLJIT_SHL, TMP1, 0, TMP1, 0, SLJIT_IMM, 1);
3191    OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, TMP1, 0);    OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, TMP1, 0);
3192    JUMPHERE(singlechar);    JUMPHERE(singlechar);
3193    }    }
3194  #endif  #endif /* COMPILE_PCRE[8|16] */
3195    #endif /* SUPPORT_UTF && !COMPILE_PCRE32 */
3196  JUMPHERE(start);  JUMPHERE(start);
3197    
3198  if (newlinecheck)  if (newlinecheck)
# Line 2563  if (newlinecheck) Line 3204  if (newlinecheck)
3204  return mainloop;  return mainloop;
3205  }  }
3206    
3207  static SLJIT_INLINE BOOL fast_forward_first_two_chars(compiler_common *common, BOOL firstline)  #define MAX_N_CHARS 16
3208    #define MAX_N_BYTES 8
3209    
3210    static SLJIT_INLINE void add_prefix_byte(pcre_uint8 byte, pcre_uint8 *bytes)
3211  {  {
3212  DEFINE_COMPILER;  pcre_uint8 len = bytes[0];
3213  struct sljit_label *start;  int i;
 struct sljit_jump *quit;  
 struct sljit_jump *found;  
 pcre_int32 chars[4];  
 pcre_uchar *cc = common->start + 1 + IMM2_SIZE;  
 int location = 0;  
 pcre_int32 len, c, bit, caseless;  
 BOOL must_end;  
3214    
3215  #ifdef COMPILE_PCRE8  if (len == 255)
3216  union {    return;
3217      sljit_uh ascombined;  
3218      sljit_ub asuchars[2];  if (len == 0)
3219  } pair;    {
3220      bytes[0] = 1;
3221      bytes[1] = byte;
3222      return;
3223      }
3224    
3225    for (i = len; i > 0; i--)
3226      if (bytes[i] == byte)
3227        return;
3228    
3229    if (len >= MAX_N_BYTES - 1)
3230      {
3231      bytes[0] = 255;
3232      return;
3233      }
3234    
3235    len++;
3236    bytes[len] = byte;
3237    bytes[0] = len;
3238    }
3239    
3240    static int scan_prefix(compiler_common *common, pcre_uchar *cc, pcre_uint32 *chars, pcre_uint8 *bytes, int max_chars, pcre_uint32 *rec_count)
3241    {
3242    /* Recursive function, which scans prefix literals. */
3243    BOOL last, any, caseless;
3244    int len, repeat, len_save, consumed = 0;
3245    pcre_uint32 chr, mask;
3246    pcre_uchar *alternative, *cc_save, *oc;
3247    #if defined SUPPORT_UTF && defined COMPILE_PCRE8
3248    pcre_uchar othercase[8];
3249    #elif defined SUPPORT_UTF && defined COMPILE_PCRE16
3250    pcre_uchar othercase[2];
3251  #else  #else
3252  union {  pcre_uchar othercase[1];
     sljit_ui ascombined;  
     sljit_uh asuchars[2];  
 } pair;  
3253  #endif  #endif
3254    
3255  if (*(common->start + GET(common->start, 1)) == OP_ALT)  repeat = 1;
   return FALSE;  
   
3256  while (TRUE)  while (TRUE)
3257    {    {
3258    caseless = 0;    if (*rec_count == 0)
3259    must_end = TRUE;      return 0;
3260    switch(*cc)    (*rec_count)--;
3261      {  
3262      case OP_CHAR:    last = TRUE;
3263      must_end = FALSE;    any = FALSE;
3264      cc++;    caseless = FALSE;
     break;  
3265    
3266      switch (*cc)
3267        {
3268      case OP_CHARI:      case OP_CHARI:
3269      caseless = 1;      caseless = TRUE;
3270      must_end = FALSE;      case OP_CHAR:
3271        last = FALSE;
3272      cc++;      cc++;
3273      break;      break;
3274    
# Line 2622  while (TRUE) Line 3287  while (TRUE)
3287      cc++;      cc++;
3288      continue;      continue;
3289    
3290        case OP_ASSERT:
3291        case OP_ASSERT_NOT:
3292        case OP_ASSERTBACK:
3293        case OP_ASSERTBACK_NOT:
3294        cc = bracketend(cc);
3295        continue;
3296    
3297        case OP_PLUSI:
3298        case OP_MINPLUSI:
3299        case OP_POSPLUSI:
3300        caseless = TRUE;
3301      case OP_PLUS:      case OP_PLUS:
3302      case OP_MINPLUS:      case OP_MINPLUS:
3303      case OP_POSPLUS:      case OP_POSPLUS:
3304      cc++;      cc++;
3305      break;      break;
3306    
3307        case OP_EXACTI:
3308        caseless = TRUE;
3309      case OP_EXACT:      case OP_EXACT:
3310        repeat = GET2(cc, 1);
3311        last = FALSE;
3312      cc += 1 + IMM2_SIZE;      cc += 1 + IMM2_SIZE;
3313      break;      break;
3314    
3315      case OP_PLUSI:      case OP_QUERYI:
3316      case OP_MINPLUSI:      case OP_MINQUERYI:
3317      case OP_POSPLUSI:      case OP_POSQUERYI:
3318      caseless = 1;      caseless = TRUE;
3319        case OP_QUERY:
3320        case OP_MINQUERY:
3321        case OP_POSQUERY:
3322        len = 1;
3323      cc++;      cc++;
3324    #ifdef SUPPORT_UTF
3325        if (common->utf && HAS_EXTRALEN(*cc)) len += GET_EXTRALEN(*cc);
3326    #endif
3327        max_chars = scan_prefix(common, cc + len, chars, bytes, max_chars, rec_count);
3328        if (max_chars == 0)
3329          return consumed;
3330        last = FALSE;
3331      break;      break;
3332    
3333      case OP_EXACTI:      case OP_KET:
3334      caseless = 1;      cc += 1 + LINK_SIZE;
3335        continue;
3336    
3337        case OP_ALT:
3338        cc += GET(cc, 1);
3339        continue;
3340    
3341        case OP_ONCE:
3342        case OP_ONCE_NC:
3343        case OP_BRA:
3344        case OP_BRAPOS:
3345        case OP_CBRA:
3346        case OP_CBRAPOS:
3347        alternative = cc + GET(cc, 1);
3348        while (*alternative == OP_ALT)
3349          {
3350          max_chars = scan_prefix(common, alternative + 1 + LINK_SIZE, chars, bytes, max_chars, rec_count);
3351          if (max_chars == 0)
3352            return consumed;
3353          alternative += GET(alternative, 1);
3354          }
3355    
3356        if (*cc == OP_CBRA || *cc == OP_CBRAPOS)
3357          cc += IMM2_SIZE;
3358        cc += 1 + LINK_SIZE;
3359        continue;
3360    
3361        case OP_CLASS:
3362    #if defined SUPPORT_UTF && defined COMPILE_PCRE8
3363        if (common->utf && !is_char7_bitset((const pcre_uint8 *)(cc + 1), FALSE)) return consumed;
3364    #endif
3365        any = TRUE;
3366        cc += 1 + 32 / sizeof(pcre_uchar);
3367        break;
3368    
3369        case OP_NCLASS:
3370    #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
3371        if (common->utf) return consumed;
3372    #endif
3373        any = TRUE;
3374        cc += 1 + 32 / sizeof(pcre_uchar);
3375        break;
3376    
3377    #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
3378        case OP_XCLASS:
3379    #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
3380        if (common->utf) return consumed;
3381    #endif
3382        any = TRUE;
3383        cc += GET(cc, 1);
3384        break;
3385    #endif
3386    
3387        case OP_DIGIT:
3388    #if defined SUPPORT_UTF && defined COMPILE_PCRE8
3389        if (common->utf && !is_char7_bitset((const pcre_uint8 *)common->ctypes - cbit_length + cbit_digit, FALSE))
3390          return consumed;
3391    #endif
3392        any = TRUE;
3393        cc++;
3394        break;
3395    
3396        case OP_WHITESPACE:
3397    #if defined SUPPORT_UTF && defined COMPILE_PCRE8
3398        if (common->utf && !is_char7_bitset((const pcre_uint8 *)common->ctypes - cbit_length + cbit_space, FALSE))
3399          return consumed;
3400    #endif
3401        any = TRUE;
3402        cc++;
3403        break;
3404    
3405        case OP_WORDCHAR:
3406    #if defined SUPPORT_UTF && defined COMPILE_PCRE8
3407        if (common->utf && !is_char7_bitset((const pcre_uint8 *)common->ctypes - cbit_length + cbit_word, FALSE))
3408          return consumed;
3409    #endif
3410        any = TRUE;
3411        cc++;
3412        break;
3413    
3414        case OP_NOT:
3415        case OP_NOTI:
3416        cc++;
3417        /* Fall through. */
3418        case OP_NOT_DIGIT:
3419        case OP_NOT_WHITESPACE:
3420        case OP_NOT_WORDCHAR:
3421        case OP_ANY:
3422        case OP_ALLANY:
3423    #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
3424        if (common->utf) return consumed;
3425    #endif
3426        any = TRUE;
3427        cc++;
3428        break;
3429    
3430    #ifdef SUPPORT_UCP
3431        case OP_NOTPROP:
3432        case OP_PROP:
3433    #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
3434        if (common->utf) return consumed;
3435    #endif
3436        any = TRUE;
3437        cc += 1 + 2;
3438        break;
3439    #endif
3440    
3441        case OP_TYPEEXACT:
3442        repeat = GET2(cc, 1);
3443      cc += 1 + IMM2_SIZE;      cc += 1 + IMM2_SIZE;
3444        continue;
3445    
3446        case OP_NOTEXACT:
3447        case OP_NOTEXACTI:
3448    #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
3449        if (common->utf) return consumed;
3450    #endif
3451        any = TRUE;
3452        repeat = GET2(cc, 1);
3453        cc += 1 + IMM2_SIZE + 1;
3454      break;      break;
3455    
3456      default:      default:
3457      return FALSE;      return consumed;
3458        }
3459    
3460      if (any)
3461        {
3462    #if defined COMPILE_PCRE8
3463        mask = 0xff;
3464    #elif defined COMPILE_PCRE16
3465        mask = 0xffff;
3466    #elif defined COMPILE_PCRE32
3467        mask = 0xffffffff;
3468    #else
3469        SLJIT_ASSERT_STOP();
3470    #endif
3471    
3472        do
3473          {
3474          chars[0] = mask;
3475          chars[1] = mask;
3476          bytes[0] = 255;
3477    
3478          consumed++;
3479          if (--max_chars == 0)
3480            return consumed;
3481          chars += 2;
3482          bytes += MAX_N_BYTES;
3483          }
3484        while (--repeat > 0);
3485    
3486        repeat = 1;
3487        continue;
3488      }      }
3489    
3490    len = 1;    len = 1;
3491  #ifdef SUPPORT_UTF  #ifdef SUPPORT_UTF
3492    if (common->utf && HAS_EXTRALEN(cc[0])) len += GET_EXTRALEN(cc[0]);    if (common->utf && HAS_EXTRALEN(*cc)) len += GET_EXTRALEN(*cc);
3493  #endif  #endif
3494    
3495    if (caseless && char_has_othercase(common, cc))    if (caseless && char_has_othercase(common, cc))
3496      {      {
3497      caseless = char_get_othercase_bit(common, cc);  #ifdef SUPPORT_UTF
3498      if (caseless == 0)      if (common->utf)
3499        return FALSE;        {
3500  #ifdef COMPILE_PCRE8        GETCHAR(chr, cc);
3501      caseless = ((caseless & 0xff) << 8) | (len - (caseless >> 8));        if ((int)PRIV(ord2utf)(char_othercase(common, chr), othercase) != len)
3502  #else          return consumed;
3503      if ((caseless & 0x100) != 0)        }
       caseless = ((caseless & 0xff) << 16) | (len - (caseless >> 9));  
3504      else      else
       caseless = ((caseless & 0xff) << 8) | (len - (caseless >> 9));  
3505  #endif  #endif
3506          {
3507          chr = *cc;
3508          othercase[0] = TABLE_GET(chr, common->fcc, chr);
3509          }
3510      }      }
3511    else    else
3512      caseless = 0;      caseless = FALSE;
3513    
3514    while (len > 0 && location < 2 * 2)    len_save = len;
3515      cc_save = cc;
3516      while (TRUE)
3517      {      {
3518      c = *cc;      oc = othercase;
3519      bit = 0;      do
     if (len == (caseless & 0xff))  
3520        {        {
3521        bit = caseless >> 8;        chr = *cc;
3522        c |= bit;  #ifdef COMPILE_PCRE32
3523          if (SLJIT_UNLIKELY(chr == NOTACHAR))
3524            return consumed;
3525    #endif
3526          add_prefix_byte((pcre_uint8)chr, bytes);
3527    
3528          mask = 0;
3529          if (caseless)
3530            {
3531            add_prefix_byte((pcre_uint8)*oc, bytes);
3532            mask = *cc ^ *oc;
3533            chr |= mask;
3534            }
3535    
3536    #ifdef COMPILE_PCRE32
3537          if (chars[0] == NOTACHAR && chars[1] == 0)
3538    #else
3539          if (chars[0] == NOTACHAR)
3540    #endif
3541            {
3542            chars[0] = chr;
3543            chars[1] = mask;
3544            }
3545          else
3546            {
3547            mask |= chars[0] ^ chr;
3548            chr |= mask;
3549            chars[0] = chr;
3550            chars[1] |= mask;
3551            }
3552    
3553          len--;
3554          consumed++;
3555          if (--max_chars == 0)
3556            return consumed;
3557          chars += 2;
3558          bytes += MAX_N_BYTES;
3559          cc++;
3560          oc++;
3561        }        }
3562        while (len > 0);
3563    
3564      chars[location] = c;      if (--repeat == 0)
3565      chars[location + 1] = bit;        break;
3566    
3567      len--;      len = len_save;
3568      location += 2;      cc = cc_save;
     cc++;  
3569      }      }
3570    
3571    if (location == 2 * 2)    repeat = 1;
3572      break;    if (last)
3573    else if (must_end)      return consumed;
     return FALSE;  
3574    }    }
3575    }
3576    
3577  if (firstline)  static SLJIT_INLINE BOOL fast_forward_first_n_chars(compiler_common *common, BOOL firstline)
3578    {  {
3579    SLJIT_ASSERT(common->first_line_end != 0);  DEFINE_COMPILER;
3580    OP1(SLJIT_MOV, TMP3, 0, STR_END, 0);  struct sljit_label *start;
3581    OP2(SLJIT_SUB, STR_END, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), common->first_line_end, SLJIT_IMM, 1);  struct sljit_jump *quit;
3582    pcre_uint32 chars[MAX_N_CHARS * 2];
3583    pcre_uint8 bytes[MAX_N_CHARS * MAX_N_BYTES];
3584    pcre_uint8 ones[MAX_N_CHARS];
3585    int offsets[3];
3586    pcre_uint32 mask;
3587    pcre_uint8 *byte_set, *byte_set_end;
3588    int i, max, from;
3589    int range_right = -1, range_len = 3 - 1;
3590    sljit_ub *update_table = NULL;
3591    BOOL in_range;
3592    pcre_uint32 rec_count;
3593    
3594    for (i = 0; i < MAX_N_CHARS; i++)
3595      {
3596      chars[i << 1] = NOTACHAR;
3597      chars[(i << 1) + 1] = 0;
3598      bytes[i * MAX_N_BYTES] = 0;
3599    }    }
 else  
   OP2(SLJIT_SUB, STR_END, 0, STR_END, 0, SLJIT_IMM, 1);  
   
 start = LABEL();  
 quit = CMP(SLJIT_C_GREATER_EQUAL, STR_PTR, 0, STR_END, 0);  
 #if defined SLJIT_UNALIGNED && SLJIT_UNALIGNED  
 #ifdef COMPILE_PCRE8  
 OP1(SLJIT_MOV_UH, TMP1, 0, SLJIT_MEM1(STR_PTR), 0);  
 #else /* COMPILE_PCRE8 */  
 OP1(SLJIT_MOV_UI, TMP1, 0, SLJIT_MEM1(STR_PTR), 0);  
 #endif  
   
 #else /* SLJIT_UNALIGNED */  
   
 #if defined SLJIT_BIG_ENDIAN && SLJIT_BIG_ENDIAN  
 OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), 0);  
 OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(1));  
 #else /* SLJIT_BIG_ENDIAN */  
 OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(1));  
 OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), 0);  
 #endif /* SLJIT_BIG_ENDIAN */  
3600    
3601  #ifdef COMPILE_PCRE8  rec_count = 10000;
3602  OP2(SLJIT_SHL, TMP2, 0, TMP2, 0, SLJIT_IMM, 8);  max = scan_prefix(common, common->start, chars, bytes, MAX_N_CHARS, &rec_count);
 #else /* COMPILE_PCRE8 */  
 OP2(SLJIT_SHL, TMP2, 0, TMP2, 0, SLJIT_IMM, 16);  
 #endif  
 OP2(SLJIT_OR, TMP1, 0, TMP1, 0, TMP2, 0);  
3603    
3604  #endif  if (max <= 1)
3605      return FALSE;
3606    
3607  if (chars[1] != 0 || chars[3] != 0)  for (i = 0; i < max; i++)
3608    {    {
3609    pair.asuchars[0] = chars[1];    mask = chars[(i << 1) + 1];
3610    pair.asuchars[1] = chars[3];    ones[i] = ones_in_half_byte[mask & 0xf];
3611    OP2(SLJIT_OR, TMP1, 0, TMP1, 0, SLJIT_IMM, pair.ascombined);    mask >>= 4;
3612      while (mask != 0)
3613        {
3614        ones[i] += ones_in_half_byte[mask & 0xf];
3615        mask >>= 4;
3616        }
3617    }    }
3618    
3619  pair.asuchars[0] = chars[0];  in_range = FALSE;
3620  pair.asuchars[1] = chars[2];  from = 0;   /* Prevent compiler "uninitialized" warning */
3621  found = CMP(SLJIT_C_EQUAL, TMP1, 0, SLJIT_IMM, pair.ascombined);  for (i = 0; i <= max; i++)
3622      {
3623      if (in_range && (i - from) > range_len && (bytes[(i - 1) * MAX_N_BYTES] <= 4))
3624        {
3625        range_len = i - from;
3626        range_right = i - 1;
3627        }
3628    
3629      if (i < max && bytes[i * MAX_N_BYTES] < 255)
3630        {
3631        if (!in_range)
3632          {
3633          in_range = TRUE;
3634          from = i;
3635          }
3636        }
3637      else if (in_range)
3638        in_range = FALSE;
3639      }
3640    
3641    if (range_right >= 0)
3642      {
3643      update_table = (sljit_ub *)allocate_read_only_data(common, 256);
3644      if (update_table == NULL)
3645        return TRUE;
3646      memset(update_table, IN_UCHARS(range_len), 256);
3647    
3648      for (i = 0; i < range_len; i++)
3649        {
3650        byte_set = bytes + ((range_right - i) * MAX_N_BYTES);
3651        SLJIT_ASSERT(byte_set[0] > 0 && byte_set[0] < 255);
3652        byte_set_end = byte_set + byte_set[0];
3653        byte_set++;
3654        while (byte_set <= byte_set_end)
3655          {
3656          if (update_table[*byte_set] > IN_UCHARS(i))
3657            update_table[*byte_set] = IN_UCHARS(i);
3658          byte_set++;
3659          }
3660        }
3661      }
3662    
3663    offsets[0] = -1;
3664    /* Scan forward. */
3665    for (i = 0; i < max; i++)
3666      if (ones[i] <= 2) {
3667        offsets[0] = i;
3668        break;
3669      }
3670    
3671    if (offsets[0] < 0 && range_right < 0)
3672      return FALSE;
3673    
3674    if (offsets[0] >= 0)
3675      {
3676      /* Scan backward. */
3677      offsets[1] = -1;
3678      for (i = max - 1; i > offsets[0]; i--)
3679        if (ones[i] <= 2 && i != range_right)
3680          {
3681          offsets[1] = i;
3682          break;
3683          }
3684    
3685      /* This case is handled better by fast_forward_first_char. */
3686      if (offsets[1] == -1 && offsets[0] == 0 && range_right < 0)
3687        return FALSE;
3688    
3689      offsets[2] = -1;
3690      /* We only search for a middle character if there is no range check. */
3691      if (offsets[1] >= 0 && range_right == -1)
3692        {
3693        /* Scan from middle. */
3694        for (i = (offsets[0] + offsets[1]) / 2 + 1; i < offsets[1]; i++)
3695          if (ones[i] <= 2)
3696            {
3697            offsets[2] = i;
3698            break;
3699            }
3700    
3701        if (offsets[2] == -1)
3702          {
3703          for (i = (offsets[0] + offsets[1]) / 2; i > offsets[0]; i--)
3704            if (ones[i] <= 2)
3705              {
3706              offsets[2] = i;
3707              break;
3708              }
3709          }
3710        }
3711    
3712      SLJIT_ASSERT(offsets[1] == -1 || (offsets[0] < offsets[1]));
3713      SLJIT_ASSERT(offsets[2] == -1 || (offsets[0] < offsets[2] && offsets[1] > offsets[2]));
3714    
3715      chars[0] = chars[offsets[0] << 1];
3716      chars[1] = chars[(offsets[0] << 1) + 1];
3717      if (offsets[2] >= 0)
3718        {
3719        chars[2] = chars[offsets[2] << 1];
3720        chars[3] = chars[(offsets[2] << 1) + 1];
3721        }
3722      if (offsets[1] >= 0)
3723        {
3724        chars[4] = chars[offsets[1] << 1];
3725        chars[5] = chars[(offsets[1] << 1) + 1];
3726        }
3727      }
3728    
3729    max -= 1;
3730    if (firstline)
3731      {
3732      SLJIT_ASSERT(common->first_line_end != 0);
3733      OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), common->first_line_end);
3734      OP1(SLJIT_MOV, TMP3, 0, STR_END, 0);
3735      OP2(SLJIT_SUB, STR_END, 0, STR_END, 0, SLJIT_IMM, IN_UCHARS(max));
3736      quit = CMP(SLJIT_LESS_EQUAL, STR_END, 0, TMP1, 0);
3737      OP1(SLJIT_MOV, STR_END, 0, TMP1, 0);
3738      JUMPHERE(quit);
3739      }
3740    else
3741      OP2(SLJIT_SUB, STR_END, 0, STR_END, 0, SLJIT_IMM, IN_UCHARS(max));
3742    
3743    #if !(defined SLJIT_CONFIG_X86_32 && SLJIT_CONFIG_X86_32)
3744    if (range_right >= 0)
3745      OP1(SLJIT_MOV, RETURN_ADDR, 0, SLJIT_IMM, (sljit_sw)update_table);
3746    #endif
3747    
3748    start = LABEL();
3749    quit = CMP(SLJIT_GREATER_EQUAL, STR_PTR, 0, STR_END, 0);
3750    
3751    SLJIT_ASSERT(range_right >= 0 || offsets[0] >= 0);
3752    
3753    if (range_right >= 0)
3754      {
3755    #if defined COMPILE_PCRE8 || (defined SLJIT_LITTLE_ENDIAN && SLJIT_LITTLE_ENDIAN)
3756      OP1(SLJIT_MOV_UB, TMP1, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(range_right));
3757    #else
3758      OP1(SLJIT_MOV_UB, TMP1, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(range_right + 1) - 1);
3759    #endif
3760    
3761    #if !(defined SLJIT_CONFIG_X86_32 && SLJIT_CONFIG_X86_32)
3762      OP1(SLJIT_MOV_UB, TMP1, 0, SLJIT_MEM2(RETURN_ADDR, TMP1), 0);
3763    #else
3764      OP1(SLJIT_MOV_UB, TMP1, 0, SLJIT_MEM1(TMP1), (sljit_sw)update_table);
3765    #endif
3766      OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, TMP1, 0);
3767      CMPTO(SLJIT_NOT_EQUAL, TMP1, 0, SLJIT_IMM, 0, start);
3768      }
3769    
3770    if (offsets[0] >= 0)
3771      {
3772      OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(offsets[0]));
3773      if (offsets[1] >= 0)
3774        OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(offsets[1]));
3775      OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
3776    
3777      if (chars[1] != 0)
3778        OP2(SLJIT_OR, TMP1, 0, TMP1, 0, SLJIT_IMM, chars[1]);
3779      CMPTO(SLJIT_NOT_EQUAL, TMP1, 0, SLJIT_IMM, chars[0], start);
3780      if (offsets[2] >= 0)
3781        OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(offsets[2] - 1));
3782    
3783      if (offsets[1] >= 0)
3784        {
3785        if (chars[5] != 0)
3786          OP2(SLJIT_OR, TMP2, 0, TMP2, 0, SLJIT_IMM, chars[5]);
3787        CMPTO(SLJIT_NOT_EQUAL, TMP2, 0, SLJIT_IMM, chars[4], start);
3788        }
3789    
3790      if (offsets[2] >= 0)
3791        {
3792        if (chars[3] != 0)
3793          OP2(SLJIT_OR, TMP1, 0, TMP1, 0, SLJIT_IMM, chars[3]);
3794        CMPTO(SLJIT_NOT_EQUAL, TMP1, 0, SLJIT_IMM, chars[2], start);
3795        }
3796      OP2(SLJIT_SUB, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
3797      }
3798    
 OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));  
 JUMPTO(SLJIT_JUMP, start);  
 JUMPHERE(found);  
3799  JUMPHERE(quit);  JUMPHERE(quit);
3800    
3801  if (firstline)  if (firstline)
3802      {
3803      if (range_right >= 0)
3804        OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), common->first_line_end);
3805    OP1(SLJIT_MOV, STR_END, 0, TMP3, 0);    OP1(SLJIT_MOV, STR_END, 0, TMP3, 0);
3806      if (range_right >= 0)
3807        {
3808        quit = CMP(SLJIT_LESS_EQUAL, STR_PTR, 0, TMP1, 0);
3809        OP1(SLJIT_MOV, STR_PTR, 0, TMP1, 0);
3810        JUMPHERE(quit);
3811        }
3812      }
3813  else  else
3814    OP2(SLJIT_ADD, STR_END, 0, STR_END, 0, SLJIT_IMM, 1);    OP2(SLJIT_ADD, STR_END, 0, STR_END, 0, SLJIT_IMM, IN_UCHARS(max));
3815  return TRUE;  return TRUE;
3816  }  }
3817    
3818    #undef MAX_N_CHARS
3819    #undef MAX_N_BYTES
3820    
3821  static SLJIT_INLINE void fast_forward_first_char(compiler_common *common, pcre_uchar first_char, BOOL caseless, BOOL firstline)  static SLJIT_INLINE void fast_forward_first_char(compiler_common *common, pcre_uchar first_char, BOOL caseless, BOOL firstline)
3822  {  {
3823  DEFINE_COMPILER;  DEFINE_COMPILER;
# Line 2766  if (firstline) Line 3830  if (firstline)
3830    {    {
3831    SLJIT_ASSERT(common->first_line_end != 0);    SLJIT_ASSERT(common->first_line_end != 0);
3832    OP1(SLJIT_MOV, TMP3, 0, STR_END, 0);    OP1(SLJIT_MOV, TMP3, 0, STR_END, 0);
3833    OP1(SLJIT_MOV, STR_END, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), common->first_line_end);    OP1(SLJIT_MOV, STR_END, 0, SLJIT_MEM1(SLJIT_SP), common->first_line_end);
3834    }    }
3835    
3836  start = LABEL();  start = LABEL();
3837  quit = CMP(SLJIT_C_GREATER_EQUAL, STR_PTR, 0, STR_END, 0);  quit = CMP(SLJIT_GREATER_EQUAL, STR_PTR, 0, STR_END, 0);
3838  OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), 0);  OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), 0);
3839    
3840  oc = first_char;  oc = first_char;
# Line 2783  if (caseless) Line 3847  if (caseless)
3847  #endif  #endif
3848 &n